From patchwork Tue Feb 25 15:13:45 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Michael S. Tsirkin" X-Patchwork-Id: 1244352 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=nongnu.org (client-ip=209.51.188.17; helo=lists.gnu.org; envelope-from=qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org; receiver=) Authentication-Results: ozlabs.org; dmarc=fail (p=none dis=none) header.from=redhat.com Authentication-Results: ozlabs.org; dkim=fail reason="signature verification failed" (1024-bit key; unprotected) header.d=redhat.com header.i=@redhat.com header.a=rsa-sha256 header.s=mimecast20190719 header.b=QMqIaKra; dkim-atps=neutral Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 48RjLs4s2vz9sPk for ; Wed, 26 Feb 2020 02:21:09 +1100 (AEDT) Received: from localhost ([::1]:58660 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1j6c1D-0003VF-AM for incoming@patchwork.ozlabs.org; Tue, 25 Feb 2020 10:21:07 -0500 Received: from eggs.gnu.org ([2001:470:142:3::10]:55522) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1j6buE-0000Jz-ES for qemu-devel@nongnu.org; Tue, 25 Feb 2020 10:13:55 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1j6buD-0001Jz-7k for qemu-devel@nongnu.org; Tue, 25 Feb 2020 10:13:54 -0500 Received: from us-smtp-delivery-1.mimecast.com ([207.211.31.120]:43702 helo=us-smtp-1.mimecast.com) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1j6buD-0001Jc-2n for qemu-devel@nongnu.org; Tue, 25 Feb 2020 10:13:53 -0500 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1582643632; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=2Hdj7mMudGk1On0DfbPW0ZE9c8URLY6uwbzuI/qZJHc=; b=QMqIaKrayFywYXUFHmcrzaFMHVEwSr1rh5eFa7xdZoOZjVxxTwWvLh930sdAdXZ8T+gZra WFFViH90l3qgWknYPYQYlU/yEHRt+hDdce77pEiQsN9064XuDkBPhWf6w01GxSRbjj5Sz/ eKRhOwc9+uwhAjlWYPuc3aN8BZNGb+Y= Received: from mail-qt1-f200.google.com (mail-qt1-f200.google.com [209.85.160.200]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-457-HRWsKFzuOciHhzTA-vSiqg-1; Tue, 25 Feb 2020 10:13:50 -0500 X-MC-Unique: HRWsKFzuOciHhzTA-vSiqg-1 Received: by mail-qt1-f200.google.com with SMTP id o24so15208192qtr.17 for ; Tue, 25 Feb 2020 07:13:50 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:date:from:to:cc:subject:message-id:references :mime-version:content-disposition:in-reply-to; bh=24gCc88fSkegGYSyr5CnQiCms+AAwtwlqsX/zbg/vTA=; b=UbTFrNGsQdbHvDt05EdwYacQIYPH24Ogb2EAe7swIOBlmfML+6v+99ngvLRUPWd2Oq 7TtpR+9sguGtyBJ12/g41229yZ79dHQlr2wut0SI7xXqT+ww4Yzn175RsveVd+LntWnE 4pkEbtCe0MLCwtC35ogqXvbmLKY9QSNbF77aam9Wc3HmB9wceX336Sb19acYmhPLSPYB QsAoflT9/NIKsbTkexQn5FHBLBIKOn9HOtNDWf84oNjsbBTMcM/NElokV5jXmb4Q8zH5 wp64UXL6xSsX94KH6Ug+c3GJ6uq+thlISC07kCvtludUVgveWwOQDetmJnSBDO2VGgjQ 8xFw== X-Gm-Message-State: APjAAAX86PxQtdUibEdyojYHw/T0c4ZTht9niaVXFkRJe6AX19j+F+fJ Q5cFqmFn9Z9V9/Bg5u0TJJAg0hFCpHblTFNsBv0qEfmFLqyUl3cQ+7DSTHKCJ/C/dKYLI3yyqg2 7ejupiQFxP5cO+q4= X-Received: by 2002:a37:66c8:: with SMTP id a191mr13320071qkc.0.1582643629451; Tue, 25 Feb 2020 07:13:49 -0800 (PST) X-Google-Smtp-Source: APXvYqxEmiANRPLpoRJld98WMa5sG0h0/Gd5C64IcqE8o38i2boTgLwdCWz1iD/PmtLdop/fy30Hyg== X-Received: by 2002:a37:66c8:: with SMTP id a191mr13320048qkc.0.1582643629254; Tue, 25 Feb 2020 07:13:49 -0800 (PST) Received: from redhat.com (bzq-79-178-2-214.red.bezeqint.net. [79.178.2.214]) by smtp.gmail.com with ESMTPSA id w53sm7684910qtb.91.2020.02.25.07.13.47 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 25 Feb 2020 07:13:48 -0800 (PST) Date: Tue, 25 Feb 2020 10:13:45 -0500 From: "Michael S. Tsirkin" To: qemu-devel@nongnu.org Subject: [PULL 16/32] virtio-iommu: Implement translate Message-ID: <20200225151210.647797-17-mst@redhat.com> References: <20200225151210.647797-1-mst@redhat.com> MIME-Version: 1.0 In-Reply-To: <20200225151210.647797-1-mst@redhat.com> X-Mailer: git-send-email 2.24.1.751.gd10ce2899c X-Mutt-Fcc: =sent X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Disposition: inline X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] [fuzzy] X-Received-From: 207.211.31.120 X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Peter Maydell , Jean-Philippe Brucker , Eric Auger Errors-To: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org Sender: "Qemu-devel" From: Eric Auger This patch implements the translate callback Signed-off-by: Eric Auger Reviewed-by: Jean-Philippe Brucker Reviewed-by: Michael S. Tsirkin Message-Id: <20200214132745.23392-6-eric.auger@redhat.com> Reviewed-by: Michael S. Tsirkin Signed-off-by: Michael S. Tsirkin --- hw/virtio/virtio-iommu.c | 60 +++++++++++++++++++++++++++++++++++++++- hw/virtio/trace-events | 1 + 2 files changed, 60 insertions(+), 1 deletion(-) diff --git a/hw/virtio/virtio-iommu.c b/hw/virtio/virtio-iommu.c index 844d34c270..59e9cd3d9a 100644 --- a/hw/virtio/virtio-iommu.c +++ b/hw/virtio/virtio-iommu.c @@ -473,19 +473,77 @@ static IOMMUTLBEntry virtio_iommu_translate(IOMMUMemoryRegion *mr, hwaddr addr, int iommu_idx) { IOMMUDevice *sdev = container_of(mr, IOMMUDevice, iommu_mr); + VirtIOIOMMUInterval interval, *mapping_key; + VirtIOIOMMUMapping *mapping_value; + VirtIOIOMMU *s = sdev->viommu; + VirtIOIOMMUEndpoint *ep; + bool bypass_allowed; uint32_t sid; + bool found; + + interval.low = addr; + interval.high = addr + 1; IOMMUTLBEntry entry = { .target_as = &address_space_memory, .iova = addr, .translated_addr = addr, - .addr_mask = ~(hwaddr)0, + .addr_mask = (1 << ctz32(s->config.page_size_mask)) - 1, .perm = IOMMU_NONE, }; + bypass_allowed = virtio_vdev_has_feature(&s->parent_obj, + VIRTIO_IOMMU_F_BYPASS); + sid = virtio_iommu_get_bdf(sdev); trace_virtio_iommu_translate(mr->parent_obj.name, sid, addr, flag); + qemu_mutex_lock(&s->mutex); + + ep = g_tree_lookup(s->endpoints, GUINT_TO_POINTER(sid)); + if (!ep) { + if (!bypass_allowed) { + error_report_once("%s sid=%d is not known!!", __func__, sid); + } else { + entry.perm = flag; + } + goto unlock; + } + + if (!ep->domain) { + if (!bypass_allowed) { + error_report_once("%s %02x:%02x.%01x not attached to any domain", + __func__, PCI_BUS_NUM(sid), + PCI_SLOT(sid), PCI_FUNC(sid)); + } else { + entry.perm = flag; + } + goto unlock; + } + + found = g_tree_lookup_extended(ep->domain->mappings, (gpointer)(&interval), + (void **)&mapping_key, + (void **)&mapping_value); + if (!found) { + error_report_once("%s no mapping for 0x%"PRIx64" for sid=%d", + __func__, addr, sid); + goto unlock; + } + + if (((flag & IOMMU_RO) && + !(mapping_value->flags & VIRTIO_IOMMU_MAP_F_READ)) || + ((flag & IOMMU_WO) && + !(mapping_value->flags & VIRTIO_IOMMU_MAP_F_WRITE))) { + error_report_once("%s permission error on 0x%"PRIx64"(%d): allowed=%d", + __func__, addr, flag, mapping_value->flags); + goto unlock; + } + entry.translated_addr = addr - mapping_key->low + mapping_value->phys_addr; + entry.perm = flag; + trace_virtio_iommu_translate_out(addr, entry.translated_addr, sid); + +unlock: + qemu_mutex_unlock(&s->mutex); return entry; } diff --git a/hw/virtio/trace-events b/hw/virtio/trace-events index 22162d6583..095aa8b509 100644 --- a/hw/virtio/trace-events +++ b/hw/virtio/trace-events @@ -71,3 +71,4 @@ virtio_iommu_get_endpoint(uint32_t ep_id) "Alloc endpoint=%d" virtio_iommu_put_endpoint(uint32_t ep_id) "Free endpoint=%d" virtio_iommu_get_domain(uint32_t domain_id) "Alloc domain=%d" virtio_iommu_put_domain(uint32_t domain_id) "Free domain=%d" +virtio_iommu_translate_out(uint64_t virt_addr, uint64_t phys_addr, uint32_t sid) "0x%"PRIx64" -> 0x%"PRIx64 " for sid=%d"