Received: by 2002:a19:5509:0:0:0:0:0 with SMTP id n9csp614724lfe; Wed, 18 Mar 2020 03:14:12 -0700 (PDT) X-Google-Smtp-Source: ADFU+vunSlocYKLUOXBEDJs/UwUpJwgGrShvb0DR7oJeteqZu9s5izRxA5I9qzypPEgcvriCll3J X-Received: by 2002:ac8:6f6e:: with SMTP id u14mr3554427qtv.304.1584526452490; Wed, 18 Mar 2020 03:14:12 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1584526452; cv=none; d=google.com; s=arc-20160816; b=m824f/syIY0RmWXe7kSbtklvYLFrS4vzxzB6j0rNCz1WaA7OwZNFMqPUxXbL+UCRJl LTVl4BiGWde7rr/XekgY+SvPgAVFGHaDgF8+Q5ry3X/JVFjJj9bR82dcD36KUgV23lF2 3wll6AiDUvZiiIMVlYHskadQFnBsEJLbRS1oOaJ3XKtOnl/8M1c7+D4PhmEgVK+eloj0 hh8Lj00rGHGhr8Q2D4imlctQwoISJUSzMAGLXnu8xuZjppeQ81spQuyxVOgect1Mok8X kVt7Uqa5DKC3RLN5M5+RS9FPqPzBu31h093tgIhCqpw1Wh3NtfnSU2xmm69FD+pzdE+3 5utg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:cc:list-subscribe:list-help:list-post:list-archive :list-unsubscribe:list-id:precedence:mime-version:references :in-reply-to:message-id:date:subject:to:from:dkim-signature; bh=XN0LjRF171QI39m3Xi7A3dpjV78hP1L4kv6vlWXZQ1s=; b=ftl/Quw/z54c2GIswOCSFBc1DzueSepFcPJtKE0Oazed1ryJG9cWLhZ8H+Cm2g04Qd pUoxjOrqOL7i0plDLDE4+73kKYzf0w8g+vLQNYFaCg1RV+ChfxsMfz+OURLgj9oDSrms 9ehGXmBfnLNVQouZgvRmMz/UHhWbHYRjsD1W6tNlq7A9c6TKHOxX6gAkEJy2K1g0CuXE R9eGJpf9EOquRmNJKiTeiwTXm32W8BowxY+C1x8VxRQL2go8c7bF35hPWHq+PpO/Zqr2 sH5T9ibe1bT5NcYNXnIkLH/DoX/vNnRp3pmmKgBdqTezMa+qQ27SqY1g+e/AHxMSV67h Wxiw== ARC-Authentication-Results: i=1; mx.google.com; dkim=fail header.i=@marvell.com header.s=pfpt0818 header.b=kAWHuSTF; spf=pass (google.com: domain of qemu-arm-bounces+alex.bennee=linaro.org@nongnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom="qemu-arm-bounces+alex.bennee=linaro.org@nongnu.org"; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=marvell.com Return-Path: Received: from lists.gnu.org (lists.gnu.org. [209.51.188.17]) by mx.google.com with ESMTPS id 136si3900215qkj.196.2020.03.18.03.14.12 for (version=TLS1_2 cipher=ECDHE-RSA-CHACHA20-POLY1305 bits=256/256); Wed, 18 Mar 2020 03:14:12 -0700 (PDT) Received-SPF: pass (google.com: domain of qemu-arm-bounces+alex.bennee=linaro.org@nongnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; Authentication-Results: mx.google.com; dkim=fail header.i=@marvell.com header.s=pfpt0818 header.b=kAWHuSTF; spf=pass (google.com: domain of qemu-arm-bounces+alex.bennee=linaro.org@nongnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom="qemu-arm-bounces+alex.bennee=linaro.org@nongnu.org"; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=marvell.com Received: from localhost ([::1]:48018 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1jEViF-0005Pr-U1 for alex.bennee@linaro.org; Wed, 18 Mar 2020 06:14:11 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:37321) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1jEVgp-0002Yq-Nn for qemu-arm@nongnu.org; Wed, 18 Mar 2020 06:12:45 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1jEVgo-0004rP-6P for qemu-arm@nongnu.org; Wed, 18 Mar 2020 06:12:43 -0400 Received: from mx0a-0016f401.pphosted.com ([67.231.148.174]:34020 helo=mx0b-0016f401.pphosted.com) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1jEVgn-0004pz-Tq; Wed, 18 Mar 2020 06:12:42 -0400 Received: from pps.filterd (m0045849.ppops.net [127.0.0.1]) by mx0a-0016f401.pphosted.com (8.16.0.42/8.16.0.42) with SMTP id 02IAAjAF008560; Wed, 18 Mar 2020 03:12:38 -0700 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=marvell.com; h=from : to : cc : subject : date : message-id : in-reply-to : references : mime-version : content-type; s=pfpt0818; bh=XN0LjRF171QI39m3Xi7A3dpjV78hP1L4kv6vlWXZQ1s=; b=kAWHuSTFuGBqDl+qT7hAMZwLkcamCRIKJP79gBKaXX6Q77wZdgoFverwikGTSh8rNmpZ vJDZz4FAZlL/uCOwKj9EQckZy90NTLhJkIV69JQsXvO3+llKh4osvJSrRcW3OrkL9lui 0Ud8gfEYZ2OjYUQXhRivsLQKHmjIT87X/somwVC+rcpfXh8FNdyk1lBY7Rzr8GR2p+oz tPaS7Y2CHuxlmPbr9OLRT0iKi/iGbABywckQK3Em4Bv5ouTiqJ+wrnrzU5gSsok2vcIz tH6JNU+exbuz2lGDLK1ceYlaHhMmkjbK21Kr3yuHNfGByEFG/cFXsBPMUnWMprkrnSgU Yw== Received: from sc-exch01.marvell.com ([199.233.58.181]) by mx0a-0016f401.pphosted.com with ESMTP id 2yu8pqj4pj-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-SHA384 bits=256 verify=NOT); Wed, 18 Mar 2020 03:12:38 -0700 Received: from DC5-EXCH02.marvell.com (10.69.176.39) by SC-EXCH01.marvell.com (10.93.176.81) with Microsoft SMTP Server (TLS) id 15.0.1497.2; Wed, 18 Mar 2020 03:12:36 -0700 Received: from SC-EXCH03.marvell.com (10.93.176.83) by DC5-EXCH02.marvell.com (10.69.176.39) with Microsoft SMTP Server (TLS) id 15.0.1497.2; Wed, 18 Mar 2020 03:12:36 -0700 Received: from bbhushan2.marvell.com (10.93.176.43) by SC-EXCH03.marvell.com (10.93.176.83) with Microsoft SMTP Server id 15.0.1497.2 via Frontend Transport; Wed, 18 Mar 2020 03:12:30 -0700 From: Bharat Bhushan To: , , , , , , , , , , , , , Subject: [PATCH v8 5/8] virtio-iommu: Add iommu notifier for map/unmap Date: Wed, 18 Mar 2020 15:41:56 +0530 Message-ID: <20200318101159.8767-6-bbhushan2@marvell.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20200318101159.8767-1-bbhushan2@marvell.com> References: <20200318101159.8767-1-bbhushan2@marvell.com> MIME-Version: 1.0 Content-Type: text/plain X-Proofpoint-Virus-Version: vendor=fsecure engine=2.50.10434:6.0.138, 18.0.645 definitions=2020-03-18_04:2020-03-18, 2020-03-18 signatures=0 X-detected-operating-system: by eggs.gnu.org: GNU/Linux 3.x [generic] X-Received-From: 67.231.148.174 X-BeenThere: qemu-arm@nongnu.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Eric Auger , Bharat Bhushan , Bharat Bhushan Errors-To: qemu-arm-bounces+alex.bennee=linaro.org@nongnu.org Sender: "Qemu-arm" X-TUID: MFOEWy/3TGyE This patch extends VIRTIO_IOMMU_T_MAP/UNMAP request to notify registered iommu-notifier. Which will call vfio notifier to map/unmap region in iommu. Signed-off-by: Bharat Bhushan Signed-off-by: Eric Auger --- include/hw/virtio/virtio-iommu.h | 2 + hw/virtio/virtio-iommu.c | 67 +++++++++++++++++++++++++++++++- hw/virtio/trace-events | 2 + 3 files changed, 70 insertions(+), 1 deletion(-) diff --git a/include/hw/virtio/virtio-iommu.h b/include/hw/virtio/virtio-iommu.h index 6f67f1020a..65ad3bf4ee 100644 --- a/include/hw/virtio/virtio-iommu.h +++ b/include/hw/virtio/virtio-iommu.h @@ -37,6 +37,7 @@ typedef struct IOMMUDevice { int devfn; IOMMUMemoryRegion iommu_mr; AddressSpace as; + QLIST_ENTRY(IOMMUDevice) next; } IOMMUDevice; typedef struct IOMMUPciBus { @@ -56,6 +57,7 @@ typedef struct VirtIOIOMMU { GTree *domains; QemuMutex mutex; GTree *endpoints; + QLIST_HEAD(, IOMMUDevice) notifiers_list; } VirtIOIOMMU; #endif diff --git a/hw/virtio/virtio-iommu.c b/hw/virtio/virtio-iommu.c index c00a55348d..623b477b9c 100644 --- a/hw/virtio/virtio-iommu.c +++ b/hw/virtio/virtio-iommu.c @@ -123,6 +123,38 @@ static gint interval_cmp(gconstpointer a, gconstpointer b, gpointer user_data) } } +static void virtio_iommu_notify_map(IOMMUMemoryRegion *mr, hwaddr iova, + hwaddr paddr, hwaddr size) +{ + IOMMUTLBEntry entry; + + entry.target_as = &address_space_memory; + entry.addr_mask = size - 1; + + entry.iova = iova; + trace_virtio_iommu_notify_map(mr->parent_obj.name, iova, paddr, size); + entry.perm = IOMMU_RW; + entry.translated_addr = paddr; + + memory_region_notify_iommu(mr, 0, entry); +} + +static void virtio_iommu_notify_unmap(IOMMUMemoryRegion *mr, hwaddr iova, + hwaddr size) +{ + IOMMUTLBEntry entry; + + entry.target_as = &address_space_memory; + entry.addr_mask = size - 1; + + entry.iova = iova; + trace_virtio_iommu_notify_unmap(mr->parent_obj.name, iova, size); + entry.perm = IOMMU_NONE; + entry.translated_addr = 0; + + memory_region_notify_iommu(mr, 0, entry); +} + static void virtio_iommu_detach_endpoint_from_domain(VirtIOIOMMUEndpoint *ep) { if (!ep->domain) { @@ -307,9 +339,12 @@ static int virtio_iommu_map(VirtIOIOMMU *s, uint64_t virt_start = le64_to_cpu(req->virt_start); uint64_t virt_end = le64_to_cpu(req->virt_end); uint32_t flags = le32_to_cpu(req->flags); + hwaddr size = virt_end - virt_start + 1; VirtIOIOMMUDomain *domain; VirtIOIOMMUInterval *interval; VirtIOIOMMUMapping *mapping; + VirtIOIOMMUEndpoint *ep; + IOMMUDevice *sdev; if (flags & ~VIRTIO_IOMMU_MAP_F_MASK) { return VIRTIO_IOMMU_S_INVAL; @@ -339,9 +374,38 @@ static int virtio_iommu_map(VirtIOIOMMU *s, g_tree_insert(domain->mappings, interval, mapping); + /* All devices in an address-space share mapping */ + QLIST_FOREACH(sdev, &s->notifiers_list, next) { + QLIST_FOREACH(ep, &domain->endpoint_list, next) { + if (ep->id == sdev->devfn) { + virtio_iommu_notify_map(&sdev->iommu_mr, + virt_start, phys_start, size); + } + } + } + return VIRTIO_IOMMU_S_OK; } +static void virtio_iommu_remove_mapping(VirtIOIOMMU *s, + VirtIOIOMMUDomain *domain, + VirtIOIOMMUInterval *interval) +{ + VirtIOIOMMUEndpoint *ep; + IOMMUDevice *sdev; + + QLIST_FOREACH(sdev, &s->notifiers_list, next) { + QLIST_FOREACH(ep, &domain->endpoint_list, next) { + if (ep->id == sdev->devfn) { + virtio_iommu_notify_unmap(&sdev->iommu_mr, + interval->low, + interval->high - interval->low + 1); + } + } + } + g_tree_remove(domain->mappings, (gpointer)(interval)); +} + static int virtio_iommu_unmap(VirtIOIOMMU *s, struct virtio_iommu_req_unmap *req) { @@ -368,7 +432,7 @@ static int virtio_iommu_unmap(VirtIOIOMMU *s, uint64_t current_high = iter_key->high; if (interval.low <= current_low && interval.high >= current_high) { - g_tree_remove(domain->mappings, iter_key); + virtio_iommu_remove_mapping(s, domain, iter_key); trace_virtio_iommu_unmap_done(domain_id, current_low, current_high); } else { ret = VIRTIO_IOMMU_S_RANGE; @@ -664,6 +728,7 @@ static void virtio_iommu_device_realize(DeviceState *dev, Error **errp) VirtIODevice *vdev = VIRTIO_DEVICE(dev); VirtIOIOMMU *s = VIRTIO_IOMMU(dev); + QLIST_INIT(&s->notifiers_list); virtio_init(vdev, "virtio-iommu", VIRTIO_ID_IOMMU, sizeof(struct virtio_iommu_config)); diff --git a/hw/virtio/trace-events b/hw/virtio/trace-events index e83500bee9..d94a1cd8a3 100644 --- a/hw/virtio/trace-events +++ b/hw/virtio/trace-events @@ -73,3 +73,5 @@ 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" virtio_iommu_report_fault(uint8_t reason, uint32_t flags, uint32_t endpoint, uint64_t addr) "FAULT reason=%d flags=%d endpoint=%d address =0x%"PRIx64 +virtio_iommu_notify_map(const char *name, uint64_t iova, uint64_t paddr, uint64_t map_size) "mr=%s iova=0x%"PRIx64" pa=0x%" PRIx64" size=0x%"PRIx64 +virtio_iommu_notify_unmap(const char *name, uint64_t iova, uint64_t map_size) "mr=%s iova=0x%"PRIx64" size=0x%"PRIx64 -- 2.17.1