From nobody Sun May 5 03:18:11 2024 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=fail; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=fail(p=none dis=none) header.from=redhat.com Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1641356737330383.1990250649807; Tue, 4 Jan 2022 20:25:37 -0800 (PST) Received: from localhost ([::1]:43228 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1n4xrk-0002Sg-EP for importer@patchew.org; Tue, 04 Jan 2022 23:25:36 -0500 Received: from eggs.gnu.org ([209.51.188.92]:44318) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1n4xmW-00041w-7Y for qemu-devel@nongnu.org; Tue, 04 Jan 2022 23:20:13 -0500 Received: from us-smtp-delivery-124.mimecast.com ([170.10.133.124]:46271) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1n4xmT-0002b9-TX for qemu-devel@nongnu.org; Tue, 04 Jan 2022 23:20:11 -0500 Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-148-kRL-wlbQN4KRJXdPAdKy_A-1; Tue, 04 Jan 2022 23:20:07 -0500 Received: from smtp.corp.redhat.com (int-mx06.intmail.prod.int.phx2.redhat.com [10.5.11.16]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id F117C1023F4F; Wed, 5 Jan 2022 04:20:06 +0000 (UTC) Received: from localhost.localdomain (ovpn-13-255.pek2.redhat.com [10.72.13.255]) by smtp.corp.redhat.com (Postfix) with ESMTP id 829946ABBE; Wed, 5 Jan 2022 04:20:04 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1641356409; 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=ogIG11Cmk79KzJIdGn67TpFD2Y91Zt6eZXmZTOq2u48=; b=ZVfRmdTW0j/pFUHUOxeBIw57KKlXLJM5jB4HY8fmt+CYJChl8Xqsfm+37QD3b3fIspb4aJ 2fZSQRYkwR/bitApX3OuhjR24m30VhyUIYlq7azml/bPqfgMHsPuNIuO409O91GXBAQAAz NkocjsjxGWWqi7+6Pcw5XUT3sptPU2M= X-MC-Unique: kRL-wlbQN4KRJXdPAdKy_A-1 From: Jason Wang To: mst@redhat.com, peterx@redhat.com Subject: [PATCH 1/3] intel-iommu: don't warn guest errors when getting rid2pasid entry Date: Wed, 5 Jan 2022 12:19:43 +0800 Message-Id: <20220105041945.13459-3-jasowang@redhat.com> In-Reply-To: <20220105041945.13459-1-jasowang@redhat.com> References: <20220105041945.13459-1-jasowang@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.16 Authentication-Results: relay.mimecast.com; auth=pass smtp.auth=CUSA124A263 smtp.mailfrom=jasowang@redhat.com X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Transfer-Encoding: quoted-printable Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: pass client-ip=170.10.133.124; envelope-from=jasowang@redhat.com; helo=us-smtp-delivery-124.mimecast.com X-Spam_score_int: -31 X-Spam_score: -3.2 X-Spam_bar: --- X-Spam_report: (-3.2 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-0.37, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_LOW=-0.7, RCVD_IN_MSPIKE_H3=0.001, RCVD_IN_MSPIKE_WL=0.001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Jason Wang , yi.l.liu@intel.com, yi.y.sun@linux.intel.com, qemu-devel@nongnu.org Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail-DKIM: fail (Header signature does not verify) X-ZM-MESSAGEID: 1641356737959100001 Content-Type: text/plain; charset="utf-8" We use to warn on wrong rid2pasid entry. But this error could be triggered by the guest and could happens during initialization. So let's don't warn in this case. Signed-off-by: Jason Wang --- hw/i386/intel_iommu.c | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/hw/i386/intel_iommu.c b/hw/i386/intel_iommu.c index 4c6c016388..f2c7a23712 100644 --- a/hw/i386/intel_iommu.c +++ b/hw/i386/intel_iommu.c @@ -1524,8 +1524,10 @@ static bool vtd_dev_pt_enabled(IntelIOMMUState *s, V= TDContextEntry *ce) if (s->root_scalable) { ret =3D vtd_ce_get_rid2pasid_entry(s, ce, &pe); if (ret) { - error_report_once("%s: vtd_ce_get_rid2pasid_entry error: %"PRI= d32, - __func__, ret); + /* + * This error is guest triggerable. We should assumt PT + * not enabled for safety. + */ return false; } return (VTD_PE_GET_TYPE(&pe) =3D=3D VTD_SM_PASID_ENTRY_PT); --=20 2.25.1 From nobody Sun May 5 03:18:11 2024 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=fail; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=fail(p=none dis=none) header.from=redhat.com Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1641356850556789.0598922866039; Tue, 4 Jan 2022 20:27:30 -0800 (PST) Received: from localhost ([::1]:46740 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1n4xtY-00050Q-1O for importer@patchew.org; Tue, 04 Jan 2022 23:27:28 -0500 Received: from eggs.gnu.org ([209.51.188.92]:44332) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1n4xmb-00043x-Pj for qemu-devel@nongnu.org; Tue, 04 Jan 2022 23:20:18 -0500 Received: from us-smtp-delivery-124.mimecast.com ([170.10.133.124]:35928) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1n4xmZ-0002c4-45 for qemu-devel@nongnu.org; Tue, 04 Jan 2022 23:20:17 -0500 Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-280-ecCBCGkXME-JC-SE_kLkDA-1; Tue, 04 Jan 2022 23:20:11 -0500 Received: from smtp.corp.redhat.com (int-mx06.intmail.prod.int.phx2.redhat.com [10.5.11.16]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id 23827180830C; Wed, 5 Jan 2022 04:20:10 +0000 (UTC) Received: from localhost.localdomain (ovpn-13-255.pek2.redhat.com [10.72.13.255]) by smtp.corp.redhat.com (Postfix) with ESMTP id C16F971C80; Wed, 5 Jan 2022 04:20:07 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1641356414; 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=3jEBFCHGl+LRIBGdPZkPGYe+hx2ST0Kigx8++uiLUQQ=; b=LwDjgWX10mFtpVBVFiMfpMga4lYFxAAy/gWmDmIgiVYznuWeN091MRDMrUZwWnSeclcsvP Bf4Gq1RkABlMLggSCC8tqW2VZsC/g0woI/RFIYLwSjGoqrLctvntw3k9pUsUh5NAp8Z3Gs fFTsrycwlWI0zQQ1BPDN4oYAzedjZic= X-MC-Unique: ecCBCGkXME-JC-SE_kLkDA-1 From: Jason Wang To: mst@redhat.com, peterx@redhat.com Subject: [PATCH 2/3] intel-iommu: drop VTDBus Date: Wed, 5 Jan 2022 12:19:44 +0800 Message-Id: <20220105041945.13459-4-jasowang@redhat.com> In-Reply-To: <20220105041945.13459-1-jasowang@redhat.com> References: <20220105041945.13459-1-jasowang@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.16 Authentication-Results: relay.mimecast.com; auth=pass smtp.auth=CUSA124A263 smtp.mailfrom=jasowang@redhat.com X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Transfer-Encoding: quoted-printable Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: pass client-ip=170.10.133.124; envelope-from=jasowang@redhat.com; helo=us-smtp-delivery-124.mimecast.com X-Spam_score_int: -31 X-Spam_score: -3.2 X-Spam_bar: --- X-Spam_report: (-3.2 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-0.37, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_LOW=-0.7, RCVD_IN_MSPIKE_H3=0.001, RCVD_IN_MSPIKE_WL=0.001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Jason Wang , yi.l.liu@intel.com, yi.y.sun@linux.intel.com, qemu-devel@nongnu.org Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail-DKIM: fail (Header signature does not verify) X-ZM-MESSAGEID: 1641356851409100001 Content-Type: text/plain; charset="utf-8" We introduce VTDBus structure as an intermediate step for searching the address space. This works well with SID based matching/lookup. But when we want to support SID plus PASID based address space lookup, this intermediate steps turns out to be a burden. So the patch simply drops the VTDBus structure and use the PCIBus and devfn as the key for the g_hash_table(). This simplifies the codes and the future PASID extension. This may case slight slower for the vtd_find_as_from_bus_num() callers but since they are all called from the control path, we can afford it. Signed-off-by: Jason Wang --- hw/i386/intel_iommu.c | 183 +++++++++++++--------------------- include/hw/i386/intel_iommu.h | 10 +- 2 files changed, 69 insertions(+), 124 deletions(-) diff --git a/hw/i386/intel_iommu.c b/hw/i386/intel_iommu.c index f2c7a23712..58c682097b 100644 --- a/hw/i386/intel_iommu.c +++ b/hw/i386/intel_iommu.c @@ -61,6 +61,11 @@ } = \ } =20 +struct vtd_as_key { + PCIBus *bus; + uint8_t devfn; +}; + static void vtd_address_space_refresh_all(IntelIOMMUState *s); static void vtd_address_space_unmap(VTDAddressSpace *as, IOMMUNotifier *n); =20 @@ -190,12 +195,18 @@ static inline gboolean vtd_as_has_map_notifier(VTDAdd= ressSpace *as) /* GHashTable functions */ static gboolean vtd_uint64_equal(gconstpointer v1, gconstpointer v2) { - return *((const uint64_t *)v1) =3D=3D *((const uint64_t *)v2); + const struct vtd_as_key *key1 =3D v1; + const struct vtd_as_key *key2 =3D v2; + + return (key1->bus =3D=3D key2->bus) && (key1->devfn =3D=3D key2->devfn= ); } =20 static guint vtd_uint64_hash(gconstpointer v) { - return (guint)*(const uint64_t *)v; + const struct vtd_as_key *key =3D v; + guint value =3D (guint)(uintptr_t)key->bus; + + return (guint)(value << 8 | key->devfn); } =20 static gboolean vtd_hash_remove_by_domain(gpointer key, gpointer value, @@ -236,22 +247,14 @@ static gboolean vtd_hash_remove_by_page(gpointer key,= gpointer value, static void vtd_reset_context_cache_locked(IntelIOMMUState *s) { VTDAddressSpace *vtd_as; - VTDBus *vtd_bus; - GHashTableIter bus_it; - uint32_t devfn_it; + GHashTableIter as_it; =20 trace_vtd_context_cache_reset(); =20 - g_hash_table_iter_init(&bus_it, s->vtd_as_by_busptr); + g_hash_table_iter_init(&as_it, s->vtd_as); =20 - while (g_hash_table_iter_next (&bus_it, NULL, (void**)&vtd_bus)) { - for (devfn_it =3D 0; devfn_it < PCI_DEVFN_MAX; ++devfn_it) { - vtd_as =3D vtd_bus->dev_as[devfn_it]; - if (!vtd_as) { - continue; - } - vtd_as->context_cache_entry.context_cache_gen =3D 0; - } + while (g_hash_table_iter_next (&as_it, NULL, (void**)&vtd_as)) { + vtd_as->context_cache_entry.context_cache_gen =3D 0; } s->context_cache_gen =3D 1; } @@ -986,32 +989,6 @@ static bool vtd_slpte_nonzero_rsvd(uint64_t slpte, uin= t32_t level) return slpte & rsvd_mask; } =20 -/* Find the VTD address space associated with a given bus number */ -static VTDBus *vtd_find_as_from_bus_num(IntelIOMMUState *s, uint8_t bus_nu= m) -{ - VTDBus *vtd_bus =3D s->vtd_as_by_bus_num[bus_num]; - GHashTableIter iter; - - if (vtd_bus) { - return vtd_bus; - } - - /* - * Iterate over the registered buses to find the one which - * currently holds this bus number and update the bus_num - * lookup table. - */ - g_hash_table_iter_init(&iter, s->vtd_as_by_busptr); - while (g_hash_table_iter_next(&iter, NULL, (void **)&vtd_bus)) { - if (pci_bus_num(vtd_bus->bus) =3D=3D bus_num) { - s->vtd_as_by_bus_num[bus_num] =3D vtd_bus; - return vtd_bus; - } - } - - return NULL; -} - /* Given the @iova, get relevant @slptep. @slpte_level will be the last le= vel * of the translation, can be used for deciding the size of large page. */ @@ -1604,18 +1581,12 @@ static bool vtd_switch_address_space(VTDAddressSpac= e *as) =20 static void vtd_switch_address_space_all(IntelIOMMUState *s) { + VTDAddressSpace *vtd_as; GHashTableIter iter; - VTDBus *vtd_bus; - int i; - - g_hash_table_iter_init(&iter, s->vtd_as_by_busptr); - while (g_hash_table_iter_next(&iter, NULL, (void **)&vtd_bus)) { - for (i =3D 0; i < PCI_DEVFN_MAX; i++) { - if (!vtd_bus->dev_as[i]) { - continue; - } - vtd_switch_address_space(vtd_bus->dev_as[i]); - } + + g_hash_table_iter_init(&iter, s->vtd_as); + while (g_hash_table_iter_next(&iter, NULL, (void **)&vtd_as)) { + vtd_switch_address_space(vtd_as); } } =20 @@ -1659,16 +1630,11 @@ static inline bool vtd_is_interrupt_addr(hwaddr add= r) =20 static void vtd_pt_enable_fast_path(IntelIOMMUState *s, uint16_t source_id) { - VTDBus *vtd_bus; VTDAddressSpace *vtd_as; bool success =3D false; + uintptr_t key =3D (uintptr_t)source_id; =20 - vtd_bus =3D vtd_find_as_from_bus_num(s, VTD_SID_TO_BUS(source_id)); - if (!vtd_bus) { - goto out; - } - - vtd_as =3D vtd_bus->dev_as[VTD_SID_TO_DEVFN(source_id)]; + vtd_as =3D g_hash_table_lookup(s->vtd_as, &key); if (!vtd_as) { goto out; } @@ -1876,11 +1842,10 @@ static void vtd_context_device_invalidate(IntelIOMM= UState *s, uint16_t source_id, uint16_t func_mask) { + GHashTableIter as_it; uint16_t mask; - VTDBus *vtd_bus; VTDAddressSpace *vtd_as; uint8_t bus_n, devfn; - uint16_t devfn_it; =20 trace_vtd_inv_desc_cc_devices(source_id, func_mask); =20 @@ -1903,32 +1868,31 @@ static void vtd_context_device_invalidate(IntelIOMM= UState *s, mask =3D ~mask; =20 bus_n =3D VTD_SID_TO_BUS(source_id); - vtd_bus =3D vtd_find_as_from_bus_num(s, bus_n); - if (vtd_bus) { - devfn =3D VTD_SID_TO_DEVFN(source_id); - for (devfn_it =3D 0; devfn_it < PCI_DEVFN_MAX; ++devfn_it) { - vtd_as =3D vtd_bus->dev_as[devfn_it]; - if (vtd_as && ((devfn_it & mask) =3D=3D (devfn & mask))) { - trace_vtd_inv_desc_cc_device(bus_n, VTD_PCI_SLOT(devfn_it), - VTD_PCI_FUNC(devfn_it)); - vtd_iommu_lock(s); - vtd_as->context_cache_entry.context_cache_gen =3D 0; - vtd_iommu_unlock(s); - /* - * Do switch address space when needed, in case if the - * device passthrough bit is switched. - */ - vtd_switch_address_space(vtd_as); - /* - * So a device is moving out of (or moving into) a - * domain, resync the shadow page table. - * This won't bring bad even if we have no such - * notifier registered - the IOMMU notification - * framework will skip MAP notifications if that - * happened. - */ - vtd_sync_shadow_page_table(vtd_as); - } + devfn =3D VTD_SID_TO_DEVFN(source_id); + + g_hash_table_iter_init(&as_it, s->vtd_as); + while (g_hash_table_iter_next(&as_it, NULL, (void**)&vtd_as)) { + if ((pci_bus_num(vtd_as->bus) =3D=3D bus_n) && + (vtd_as->devfn & mask) =3D=3D (devfn & mask)) { + trace_vtd_inv_desc_cc_device(bus_n, VTD_PCI_SLOT(vtd_as->devfn= ), + VTD_PCI_FUNC(vtd_as->devfn)); + vtd_iommu_lock(s); + vtd_as->context_cache_entry.context_cache_gen =3D 0; + vtd_iommu_unlock(s); + /* + * Do switch address space when needed, in case if the + * device passthrough bit is switched. + */ + vtd_switch_address_space(vtd_as); + /* + * So a device is moving out of (or moving into) a + * domain, resync the shadow page table. + * This won't bring bad even if we have no such + * notifier registered - the IOMMU notification + * framework will skip MAP notifications if that + * happened. + */ + vtd_sync_shadow_page_table(vtd_as); } } } @@ -2442,18 +2406,14 @@ static bool vtd_process_device_iotlb_desc(IntelIOMM= UState *s, { VTDAddressSpace *vtd_dev_as; IOMMUTLBEvent event; - struct VTDBus *vtd_bus; + uintptr_t key; hwaddr addr; uint64_t sz; uint16_t sid; - uint8_t devfn; bool size; - uint8_t bus_num; =20 addr =3D VTD_INV_DESC_DEVICE_IOTLB_ADDR(inv_desc->hi); sid =3D VTD_INV_DESC_DEVICE_IOTLB_SID(inv_desc->lo); - devfn =3D sid & 0xff; - bus_num =3D sid >> 8; size =3D VTD_INV_DESC_DEVICE_IOTLB_SIZE(inv_desc->hi); =20 if ((inv_desc->lo & VTD_INV_DESC_DEVICE_IOTLB_RSVD_LO) || @@ -2464,12 +2424,8 @@ static bool vtd_process_device_iotlb_desc(IntelIOMMU= State *s, return false; } =20 - vtd_bus =3D vtd_find_as_from_bus_num(s, bus_num); - if (!vtd_bus) { - goto done; - } - - vtd_dev_as =3D vtd_bus->dev_as[devfn]; + key =3D (uintptr_t)sid; + vtd_dev_as =3D g_hash_table_lookup(s->vtd_as, &key); if (!vtd_dev_as) { goto done; } @@ -3390,27 +3346,22 @@ static const MemoryRegionOps vtd_mem_ir_ops =3D { =20 VTDAddressSpace *vtd_find_add_as(IntelIOMMUState *s, PCIBus *bus, int devf= n) { - uintptr_t key =3D (uintptr_t)bus; - VTDBus *vtd_bus =3D g_hash_table_lookup(s->vtd_as_by_busptr, &key); - VTDAddressSpace *vtd_dev_as; + struct vtd_as_key key =3D { + .bus =3D bus, + .devfn =3D devfn, + }; + VTDAddressSpace *vtd_dev_as =3D g_hash_table_lookup(s->vtd_as, &key); char name[128]; =20 - if (!vtd_bus) { - uintptr_t *new_key =3D g_malloc(sizeof(*new_key)); - *new_key =3D (uintptr_t)bus; - /* No corresponding free() */ - vtd_bus =3D g_malloc0(sizeof(VTDBus) + sizeof(VTDAddressSpace *) *= \ - PCI_DEVFN_MAX); - vtd_bus->bus =3D bus; - g_hash_table_insert(s->vtd_as_by_busptr, new_key, vtd_bus); - } + if (!vtd_dev_as) { + struct vtd_as_key *new_key =3D g_malloc(sizeof(*new_key)); =20 - vtd_dev_as =3D vtd_bus->dev_as[devfn]; + new_key->bus =3D bus; + new_key->devfn =3D devfn; =20 - if (!vtd_dev_as) { snprintf(name, sizeof(name), "vtd-%02x.%x", PCI_SLOT(devfn), PCI_FUNC(devfn)); - vtd_bus->dev_as[devfn] =3D vtd_dev_as =3D g_malloc0(sizeof(VTDAddr= essSpace)); + vtd_dev_as =3D g_malloc0(sizeof(VTDAddressSpace)); =20 vtd_dev_as->bus =3D bus; vtd_dev_as->devfn =3D (uint8_t)devfn; @@ -3466,6 +3417,8 @@ VTDAddressSpace *vtd_find_add_as(IntelIOMMUState *s, = PCIBus *bus, int devfn) &vtd_dev_as->nodmar, 0); =20 vtd_switch_address_space(vtd_dev_as); + + g_hash_table_insert(s->vtd_as, new_key, vtd_dev_as); } return vtd_dev_as; } @@ -3750,6 +3703,7 @@ static AddressSpace *vtd_host_dma_iommu(PCIBus *bus, = void *opaque, int devfn) assert(0 <=3D devfn && devfn < PCI_DEVFN_MAX); =20 vtd_as =3D vtd_find_add_as(s, bus, devfn); + return &vtd_as->as; } =20 @@ -3835,7 +3789,6 @@ static void vtd_realize(DeviceState *dev, Error **err= p) =20 QLIST_INIT(&s->vtd_as_with_notifiers); qemu_mutex_init(&s->iommu_lock); - memset(s->vtd_as_by_bus_num, 0, sizeof(s->vtd_as_by_bus_num)); memory_region_init_io(&s->csrmem, OBJECT(s), &vtd_mem_ops, s, "intel_iommu", DMAR_REG_SIZE); =20 @@ -3857,8 +3810,8 @@ static void vtd_realize(DeviceState *dev, Error **err= p) /* No corresponding destroy */ s->iotlb =3D g_hash_table_new_full(vtd_uint64_hash, vtd_uint64_equal, g_free, g_free); - s->vtd_as_by_busptr =3D g_hash_table_new_full(vtd_uint64_hash, vtd_uin= t64_equal, - g_free, g_free); + s->vtd_as =3D g_hash_table_new_full(vtd_uint64_hash, vtd_uint64_equal, + g_free, g_free); vtd_init(s); sysbus_mmio_map(SYS_BUS_DEVICE(s), 0, Q35_HOST_BRIDGE_IOMMU_ADDR); pci_setup_iommu(bus, vtd_host_dma_iommu, dev); diff --git a/include/hw/i386/intel_iommu.h b/include/hw/i386/intel_iommu.h index 41783ee46d..014d77dc2a 100644 --- a/include/hw/i386/intel_iommu.h +++ b/include/hw/i386/intel_iommu.h @@ -58,7 +58,6 @@ typedef struct VTDContextEntry VTDContextEntry; typedef struct VTDContextCacheEntry VTDContextCacheEntry; typedef struct VTDAddressSpace VTDAddressSpace; typedef struct VTDIOTLBEntry VTDIOTLBEntry; -typedef struct VTDBus VTDBus; typedef union VTD_IR_TableEntry VTD_IR_TableEntry; typedef union VTD_IR_MSIAddress VTD_IR_MSIAddress; typedef struct VTDPASIDDirEntry VTDPASIDDirEntry; @@ -111,12 +110,6 @@ struct VTDAddressSpace { IOVATree *iova_tree; /* Traces mapped IOVA ranges */ }; =20 -struct VTDBus { - PCIBus* bus; /* A reference to the bus to provide translation for */ - /* A table of VTDAddressSpace objects indexed by devfn */ - VTDAddressSpace *dev_as[]; -}; - struct VTDIOTLBEntry { uint64_t gfn; uint16_t domain_id; @@ -252,8 +245,7 @@ struct IntelIOMMUState { uint32_t context_cache_gen; /* Should be in [1,MAX] */ GHashTable *iotlb; /* IOTLB */ =20 - GHashTable *vtd_as_by_busptr; /* VTDBus objects indexed by PCIBus* r= eference */ - VTDBus *vtd_as_by_bus_num[VTD_PCI_BUS_MAX]; /* VTDBus objects indexed = by bus number */ + GHashTable *vtd_as; /* VTD address space indexed by source= id*/ /* list of registered notifiers */ QLIST_HEAD(, VTDAddressSpace) vtd_as_with_notifiers; =20 --=20 2.25.1 From nobody Sun May 5 03:18:11 2024 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=fail; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=fail(p=none dis=none) header.from=redhat.com Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1641356981641211.60016783188735; Tue, 4 Jan 2022 20:29:41 -0800 (PST) Received: from localhost ([::1]:48892 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1n4xvg-0006T9-FW for importer@patchew.org; Tue, 04 Jan 2022 23:29:40 -0500 Received: from eggs.gnu.org ([209.51.188.92]:44360) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1n4xmu-0004Jx-Mc for qemu-devel@nongnu.org; Tue, 04 Jan 2022 23:20:36 -0500 Received: from us-smtp-delivery-124.mimecast.com ([170.10.129.124]:40676) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1n4xmr-0002eF-8S for qemu-devel@nongnu.org; Tue, 04 Jan 2022 23:20:36 -0500 Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-591-ihfTJekeNtiPWs9DFOdu7Q-1; Tue, 04 Jan 2022 23:20:29 -0500 Received: from smtp.corp.redhat.com (int-mx06.intmail.prod.int.phx2.redhat.com [10.5.11.16]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id 7C0401023F4D; Wed, 5 Jan 2022 04:20:28 +0000 (UTC) Received: from localhost.localdomain (ovpn-13-255.pek2.redhat.com [10.72.13.255]) by smtp.corp.redhat.com (Postfix) with ESMTP id 9F18E6ABAF; Wed, 5 Jan 2022 04:20:10 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1641356432; 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=DCmDNpVvtT0pyKj2wmQUmyT+5oPdaTSlJRNlvwrEyUo=; b=IdT7TQnJ7iG1v50e5G5ELxzLIEvgtj0Nollq48RsFPdz2VS9llLHj7rTlXdnaamG7gDwKo xryK54gI+snMeE9fiRFpUuVaJyB2zl6R9htyfBrJeIxxvyzNF1cB4w5p0fXeEI1PnShueP Ja4P3/8qY0fGo3VXD4voE0QoRMXQUVA= X-MC-Unique: ihfTJekeNtiPWs9DFOdu7Q-1 From: Jason Wang To: mst@redhat.com, peterx@redhat.com Subject: [PATCH 3/3] intel-iommu: PASID support Date: Wed, 5 Jan 2022 12:19:45 +0800 Message-Id: <20220105041945.13459-5-jasowang@redhat.com> In-Reply-To: <20220105041945.13459-1-jasowang@redhat.com> References: <20220105041945.13459-1-jasowang@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.16 Authentication-Results: relay.mimecast.com; auth=pass smtp.auth=CUSA124A263 smtp.mailfrom=jasowang@redhat.com X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Transfer-Encoding: quoted-printable Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: pass client-ip=170.10.129.124; envelope-from=jasowang@redhat.com; helo=us-smtp-delivery-124.mimecast.com X-Spam_score_int: -31 X-Spam_score: -3.2 X-Spam_bar: --- X-Spam_report: (-3.2 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-0.37, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_LOW=-0.7, RCVD_IN_MSPIKE_H3=0.001, RCVD_IN_MSPIKE_WL=0.001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Jason Wang , yi.l.liu@intel.com, yi.y.sun@linux.intel.com, qemu-devel@nongnu.org Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail-DKIM: fail (Header signature does not verify) X-ZM-MESSAGEID: 1641356982903100001 Content-Type: text/plain; charset="utf-8" This patch introduce ECAP_PASID via "x-pasid-mode". Based on the existing support for scalable mode, we need to implement the following missing parts: 1) tag VTDAddressSpace with PASID and support IOMMU/DMA translation with PASID 2) tag IOTLB with PASID 3) PASID cache and its flush For simplicity, PASID cache is not implemented so we can simply implement the PASID cache flush as a nop. This could be added in the future. Note that though PASID based IOMMU translation is ready but no device can issue PASID DMA right now. In this case, PCI_NO_PASID is used as PASID to identify the address w/ PASID. vtd_find_add_as() has been extended to provision address space with PASID which could be utilized by the future extension of PCI core to allow device model to use PASID based DMA translation. This feature would be useful for: 1) prototyping PASID support for devices like virtio 2) future vPASID work Signed-off-by: Jason Wang --- hw/i386/intel_iommu.c | 355 +++++++++++++++++++++++++-------- hw/i386/intel_iommu_internal.h | 14 +- hw/i386/trace-events | 2 + include/hw/i386/intel_iommu.h | 6 +- include/hw/pci/pci_bus.h | 2 + 5 files changed, 289 insertions(+), 90 deletions(-) diff --git a/hw/i386/intel_iommu.c b/hw/i386/intel_iommu.c index 58c682097b..04ae604ea1 100644 --- a/hw/i386/intel_iommu.c +++ b/hw/i386/intel_iommu.c @@ -64,6 +64,14 @@ struct vtd_as_key { PCIBus *bus; uint8_t devfn; + uint32_t pasid; +}; + +struct vtd_iotlb_key { + uint16_t sid; + uint32_t pasid; + uint64_t gfn; + uint32_t level; }; =20 static void vtd_address_space_refresh_all(IntelIOMMUState *s); @@ -193,15 +201,36 @@ static inline gboolean vtd_as_has_map_notifier(VTDAdd= ressSpace *as) } =20 /* GHashTable functions */ -static gboolean vtd_uint64_equal(gconstpointer v1, gconstpointer v2) +static gboolean vtd_iotlb_equal(gconstpointer v1, gconstpointer v2) +{ + const struct vtd_iotlb_key *key1 =3D v1; + const struct vtd_iotlb_key *key2 =3D v2; + + return key1->sid =3D=3D key2->sid && + key1->pasid =3D=3D key2->pasid && + key1->level =3D=3D key2->level && + key1->gfn =3D=3D key2->gfn; +} + +static guint vtd_iotlb_hash(gconstpointer v) +{ + const struct vtd_iotlb_key *key =3D v; + + return key->gfn | ((key->sid) << VTD_IOTLB_SID_SHIFT) | + (key->level) << VTD_IOTLB_LVL_SHIFT | + (key->pasid) << VTD_IOTLB_PASID_SHIFT; +} + +static gboolean vtd_as_equal(gconstpointer v1, gconstpointer v2) { const struct vtd_as_key *key1 =3D v1; const struct vtd_as_key *key2 =3D v2; =20 - return (key1->bus =3D=3D key2->bus) && (key1->devfn =3D=3D key2->devfn= ); + return (key1->bus =3D=3D key2->bus) && (key1->devfn =3D=3D key2->devfn= ) && + (key1->pasid =3D=3D key2->pasid); } =20 -static guint vtd_uint64_hash(gconstpointer v) +static guint vtd_as_hash(gconstpointer v) { const struct vtd_as_key *key =3D v; guint value =3D (guint)(uintptr_t)key->bus; @@ -241,6 +270,29 @@ static gboolean vtd_hash_remove_by_page(gpointer key, = gpointer value, (entry->gfn =3D=3D gfn_tlb)); } =20 +static gboolean vtd_hash_remove_by_page_pasid(gpointer key, gpointer value, + gpointer user_data) +{ + VTDIOTLBEntry *entry =3D (VTDIOTLBEntry *)value; + VTDIOTLBPageInvInfo *info =3D (VTDIOTLBPageInvInfo *)user_data; + uint64_t gfn =3D (info->addr >> VTD_PAGE_SHIFT_4K) & info->mask; + uint64_t gfn_tlb =3D (info->addr & entry->mask) >> VTD_PAGE_SHIFT_4K; + return (entry->domain_id =3D=3D info->domain_id) && + (entry->pasid =3D=3D info->pasid) && + (((entry->gfn & info->mask) =3D=3D gfn) || + (entry->gfn =3D=3D gfn_tlb)); +} + +static gboolean vtd_hash_remove_by_pasid(gpointer key, gpointer value, + gpointer user_data) +{ + VTDIOTLBEntry *entry =3D (VTDIOTLBEntry *)value; + VTDIOTLBPageInvInfo *info =3D (VTDIOTLBPageInvInfo *)user_data; + + return (entry->domain_id =3D=3D info->domain_id) && + (entry->pasid =3D=3D info->pasid); +} + /* Reset all the gen of VTDAddressSpace to zero and set the gen of * IntelIOMMUState to 1. Must be called with IOMMU lock held. */ @@ -281,13 +333,6 @@ static void vtd_reset_caches(IntelIOMMUState *s) vtd_iommu_unlock(s); } =20 -static uint64_t vtd_get_iotlb_key(uint64_t gfn, uint16_t source_id, - uint32_t level) -{ - return gfn | ((uint64_t)(source_id) << VTD_IOTLB_SID_SHIFT) | - ((uint64_t)(level) << VTD_IOTLB_LVL_SHIFT); -} - static uint64_t vtd_get_iotlb_gfn(hwaddr addr, uint32_t level) { return (addr & vtd_slpt_level_page_mask(level)) >> VTD_PAGE_SHIFT_4K; @@ -295,15 +340,17 @@ static uint64_t vtd_get_iotlb_gfn(hwaddr addr, uint32= _t level) =20 /* Must be called with IOMMU lock held */ static VTDIOTLBEntry *vtd_lookup_iotlb(IntelIOMMUState *s, uint16_t source= _id, - hwaddr addr) + hwaddr addr, uint32_t pasid) { + struct vtd_iotlb_key key; VTDIOTLBEntry *entry; - uint64_t key; int level; =20 for (level =3D VTD_SL_PT_LEVEL; level < VTD_SL_PML4_LEVEL; level++) { - key =3D vtd_get_iotlb_key(vtd_get_iotlb_gfn(addr, level), - source_id, level); + key.gfn =3D vtd_get_iotlb_gfn(addr, level); + key.level =3D level; + key.sid =3D source_id; + key.pasid =3D pasid; entry =3D g_hash_table_lookup(s->iotlb, &key); if (entry) { goto out; @@ -317,10 +364,11 @@ out: /* Must be with IOMMU lock held */ static void vtd_update_iotlb(IntelIOMMUState *s, uint16_t source_id, uint16_t domain_id, hwaddr addr, uint64_t slp= te, - uint8_t access_flags, uint32_t level) + uint8_t access_flags, uint32_t level, + uint32_t pasid) { VTDIOTLBEntry *entry =3D g_malloc(sizeof(*entry)); - uint64_t *key =3D g_malloc(sizeof(*key)); + struct vtd_iotlb_key *key =3D g_malloc(sizeof(*key)); uint64_t gfn =3D vtd_get_iotlb_gfn(addr, level); =20 trace_vtd_iotlb_page_update(source_id, addr, slpte, domain_id); @@ -334,7 +382,13 @@ static void vtd_update_iotlb(IntelIOMMUState *s, uint1= 6_t source_id, entry->slpte =3D slpte; entry->access_flags =3D access_flags; entry->mask =3D vtd_slpt_level_page_mask(level); - *key =3D vtd_get_iotlb_key(gfn, source_id, level); + entry->pasid =3D pasid; + + key->gfn =3D gfn; + key->sid =3D source_id; + key->level =3D level; + key->pasid =3D pasid; + g_hash_table_replace(s->iotlb, key, entry); } =20 @@ -803,13 +857,15 @@ static int vtd_get_pe_from_pasid_table(IntelIOMMUStat= e *s, =20 static int vtd_ce_get_rid2pasid_entry(IntelIOMMUState *s, VTDContextEntry *ce, - VTDPASIDEntry *pe) + VTDPASIDEntry *pe, + uint32_t pasid) { - uint32_t pasid; dma_addr_t pasid_dir_base; int ret =3D 0; =20 - pasid =3D VTD_CE_GET_RID2PASID(ce); + if (pasid =3D=3D PCI_NO_PASID) { + pasid =3D VTD_CE_GET_RID2PASID(ce); + } pasid_dir_base =3D VTD_CE_GET_PASID_DIR_TABLE(ce); ret =3D vtd_get_pe_from_pasid_table(s, pasid_dir_base, pasid, pe); =20 @@ -818,15 +874,17 @@ static int vtd_ce_get_rid2pasid_entry(IntelIOMMUState= *s, =20 static int vtd_ce_get_pasid_fpd(IntelIOMMUState *s, VTDContextEntry *ce, - bool *pe_fpd_set) + bool *pe_fpd_set, + uint32_t pasid) { int ret; - uint32_t pasid; dma_addr_t pasid_dir_base; VTDPASIDDirEntry pdire; VTDPASIDEntry pe; =20 - pasid =3D VTD_CE_GET_RID2PASID(ce); + if (pasid =3D=3D PCI_NO_PASID) { + pasid =3D VTD_CE_GET_RID2PASID(ce); + } pasid_dir_base =3D VTD_CE_GET_PASID_DIR_TABLE(ce); =20 /* @@ -872,12 +930,13 @@ static inline uint32_t vtd_ce_get_level(VTDContextEnt= ry *ce) } =20 static uint32_t vtd_get_iova_level(IntelIOMMUState *s, - VTDContextEntry *ce) + VTDContextEntry *ce, + uint32_t pasid) { VTDPASIDEntry pe; =20 if (s->root_scalable) { - vtd_ce_get_rid2pasid_entry(s, ce, &pe); + vtd_ce_get_rid2pasid_entry(s, ce, &pe, pasid); return VTD_PE_GET_LEVEL(&pe); } =20 @@ -890,12 +949,13 @@ static inline uint32_t vtd_ce_get_agaw(VTDContextEntr= y *ce) } =20 static uint32_t vtd_get_iova_agaw(IntelIOMMUState *s, - VTDContextEntry *ce) + VTDContextEntry *ce, + uint32_t pasid) { VTDPASIDEntry pe; =20 if (s->root_scalable) { - vtd_ce_get_rid2pasid_entry(s, ce, &pe); + vtd_ce_get_rid2pasid_entry(s, ce, &pe, pasid); return 30 + ((pe.val[0] >> 2) & VTD_SM_PASID_ENTRY_AW) * 9; } =20 @@ -937,31 +997,33 @@ static inline bool vtd_ce_type_check(X86IOMMUState *x= 86_iommu, } =20 static inline uint64_t vtd_iova_limit(IntelIOMMUState *s, - VTDContextEntry *ce, uint8_t aw) + VTDContextEntry *ce, uint8_t aw, + uint32_t pasid) { - uint32_t ce_agaw =3D vtd_get_iova_agaw(s, ce); + uint32_t ce_agaw =3D vtd_get_iova_agaw(s, ce, pasid); return 1ULL << MIN(ce_agaw, aw); } =20 /* Return true if IOVA passes range check, otherwise false. */ static inline bool vtd_iova_range_check(IntelIOMMUState *s, uint64_t iova, VTDContextEntry *ce, - uint8_t aw) + uint8_t aw, uint32_t pasid) { /* * Check if @iova is above 2^X-1, where X is the minimum of MGAW * in CAP_REG and AW in context-entry. */ - return !(iova & ~(vtd_iova_limit(s, ce, aw) - 1)); + return !(iova & ~(vtd_iova_limit(s, ce, aw, pasid) - 1)); } =20 static dma_addr_t vtd_get_iova_pgtbl_base(IntelIOMMUState *s, - VTDContextEntry *ce) + VTDContextEntry *ce, + uint32_t pasid) { VTDPASIDEntry pe; =20 if (s->root_scalable) { - vtd_ce_get_rid2pasid_entry(s, ce, &pe); + vtd_ce_get_rid2pasid_entry(s, ce, &pe, pasid); return pe.val[0] & VTD_SM_PASID_ENTRY_SLPTPTR; } =20 @@ -995,15 +1057,16 @@ static bool vtd_slpte_nonzero_rsvd(uint64_t slpte, u= int32_t level) static int vtd_iova_to_slpte(IntelIOMMUState *s, VTDContextEntry *ce, uint64_t iova, bool is_write, uint64_t *slptep, uint32_t *slpte_level, - bool *reads, bool *writes, uint8_t aw_bits) + bool *reads, bool *writes, uint8_t aw_bits, + uint32_t pasid) { - dma_addr_t addr =3D vtd_get_iova_pgtbl_base(s, ce); - uint32_t level =3D vtd_get_iova_level(s, ce); + dma_addr_t addr =3D vtd_get_iova_pgtbl_base(s, ce, pasid); + uint32_t level =3D vtd_get_iova_level(s, ce, pasid); uint32_t offset; uint64_t slpte; uint64_t access_right_check; =20 - if (!vtd_iova_range_check(s, iova, ce, aw_bits)) { + if (!vtd_iova_range_check(s, iova, ce, aw_bits, pasid)) { error_report_once("%s: detected IOVA overflow (iova=3D0x%" PRIx64 = ")", __func__, iova); return -VTD_FR_ADDR_BEYOND_MGAW; @@ -1019,7 +1082,7 @@ static int vtd_iova_to_slpte(IntelIOMMUState *s, VTDC= ontextEntry *ce, if (slpte =3D=3D (uint64_t)-1) { error_report_once("%s: detected read error on DMAR slpte " "(iova=3D0x%" PRIx64 ")", __func__, iova); - if (level =3D=3D vtd_get_iova_level(s, ce)) { + if (level =3D=3D vtd_get_iova_level(s, ce, pasid)) { /* Invalid programming of context-entry */ return -VTD_FR_CONTEXT_ENTRY_INV; } else { @@ -1261,18 +1324,19 @@ next: */ static int vtd_page_walk(IntelIOMMUState *s, VTDContextEntry *ce, uint64_t start, uint64_t end, - vtd_page_walk_info *info) + vtd_page_walk_info *info, + uint32_t pasid) { - dma_addr_t addr =3D vtd_get_iova_pgtbl_base(s, ce); - uint32_t level =3D vtd_get_iova_level(s, ce); + dma_addr_t addr =3D vtd_get_iova_pgtbl_base(s, ce, pasid); + uint32_t level =3D vtd_get_iova_level(s, ce, pasid); =20 - if (!vtd_iova_range_check(s, start, ce, info->aw)) { + if (!vtd_iova_range_check(s, start, ce, info->aw, pasid)) { return -VTD_FR_ADDR_BEYOND_MGAW; } =20 - if (!vtd_iova_range_check(s, end, ce, info->aw)) { + if (!vtd_iova_range_check(s, end, ce, info->aw, pasid)) { /* Fix end so that it reaches the maximum */ - end =3D vtd_iova_limit(s, ce, info->aw); + end =3D vtd_iova_limit(s, ce, info->aw, pasid); } =20 return vtd_page_walk_level(addr, start, end, level, true, true, info); @@ -1340,7 +1404,7 @@ static int vtd_ce_rid2pasid_check(IntelIOMMUState *s, * has valid rid2pasid setting, which includes valid * rid2pasid field and corresponding pasid entry setting */ - return vtd_ce_get_rid2pasid_entry(s, ce, &pe); + return vtd_ce_get_rid2pasid_entry(s, ce, &pe, PCI_NO_PASID); } =20 /* Map a device to its corresponding domain (context-entry) */ @@ -1423,12 +1487,13 @@ static int vtd_sync_shadow_page_hook(IOMMUTLBEvent = *event, } =20 static uint16_t vtd_get_domain_id(IntelIOMMUState *s, - VTDContextEntry *ce) + VTDContextEntry *ce, + uint32_t pasid) { VTDPASIDEntry pe; =20 if (s->root_scalable) { - vtd_ce_get_rid2pasid_entry(s, ce, &pe); + vtd_ce_get_rid2pasid_entry(s, ce, &pe, pasid); return VTD_SM_PASID_ENTRY_DID(pe.val[1]); } =20 @@ -1446,10 +1511,10 @@ static int vtd_sync_shadow_page_table_range(VTDAddr= essSpace *vtd_as, .notify_unmap =3D true, .aw =3D s->aw_bits, .as =3D vtd_as, - .domain_id =3D vtd_get_domain_id(s, ce), + .domain_id =3D vtd_get_domain_id(s, ce, vtd_as->pasid), }; =20 - return vtd_page_walk(s, ce, addr, addr + size, &info); + return vtd_page_walk(s, ce, addr, addr + size, &info, vtd_as->pasid); } =20 static int vtd_sync_shadow_page_table(VTDAddressSpace *vtd_as) @@ -1493,13 +1558,14 @@ static int vtd_sync_shadow_page_table(VTDAddressSpa= ce *vtd_as) * 1st-level translation or 2nd-level translation, it depends * on PGTT setting. */ -static bool vtd_dev_pt_enabled(IntelIOMMUState *s, VTDContextEntry *ce) +static bool vtd_dev_pt_enabled(IntelIOMMUState *s, VTDContextEntry *ce, + uint32_t pasid) { VTDPASIDEntry pe; int ret; =20 if (s->root_scalable) { - ret =3D vtd_ce_get_rid2pasid_entry(s, ce, &pe); + ret =3D vtd_ce_get_rid2pasid_entry(s, ce, &pe, pasid); if (ret) { /* * This error is guest triggerable. We should assumt PT @@ -1535,7 +1601,7 @@ static bool vtd_as_pt_enabled(VTDAddressSpace *as) return false; } =20 - return vtd_dev_pt_enabled(s, &ce); + return vtd_dev_pt_enabled(s, &ce, as->pasid); } =20 /* Return whether the device is using IOMMU translation. */ @@ -1669,7 +1735,7 @@ static bool vtd_do_iommu_translate(VTDAddressSpace *v= td_as, PCIBus *bus, uint8_t bus_num =3D pci_bus_num(bus); VTDContextCacheEntry *cc_entry; uint64_t slpte, page_mask; - uint32_t level; + uint32_t level, pasid =3D vtd_as->pasid; uint16_t source_id =3D vtd_make_source_id(bus_num, devfn); int ret_fr; bool is_fpd_set =3D false; @@ -1688,17 +1754,6 @@ static bool vtd_do_iommu_translate(VTDAddressSpace *= vtd_as, PCIBus *bus, =20 cc_entry =3D &vtd_as->context_cache_entry; =20 - /* Try to fetch slpte form IOTLB */ - iotlb_entry =3D vtd_lookup_iotlb(s, source_id, addr); - if (iotlb_entry) { - trace_vtd_iotlb_page_hit(source_id, addr, iotlb_entry->slpte, - iotlb_entry->domain_id); - slpte =3D iotlb_entry->slpte; - access_flags =3D iotlb_entry->access_flags; - page_mask =3D iotlb_entry->mask; - goto out; - } - /* Try to fetch context-entry from cache first */ if (cc_entry->context_cache_gen =3D=3D s->context_cache_gen) { trace_vtd_iotlb_cc_hit(bus_num, devfn, cc_entry->context_entry.hi, @@ -1707,14 +1762,14 @@ static bool vtd_do_iommu_translate(VTDAddressSpace = *vtd_as, PCIBus *bus, ce =3D cc_entry->context_entry; is_fpd_set =3D ce.lo & VTD_CONTEXT_ENTRY_FPD; if (!is_fpd_set && s->root_scalable) { - ret_fr =3D vtd_ce_get_pasid_fpd(s, &ce, &is_fpd_set); + ret_fr =3D vtd_ce_get_pasid_fpd(s, &ce, &is_fpd_set, pasid); VTD_PE_GET_FPD_ERR(ret_fr, is_fpd_set, s, source_id, addr, is_= write); } } else { ret_fr =3D vtd_dev_to_context_entry(s, bus_num, devfn, &ce); is_fpd_set =3D ce.lo & VTD_CONTEXT_ENTRY_FPD; if (!ret_fr && !is_fpd_set && s->root_scalable) { - ret_fr =3D vtd_ce_get_pasid_fpd(s, &ce, &is_fpd_set); + ret_fr =3D vtd_ce_get_pasid_fpd(s, &ce, &is_fpd_set, pasid); } VTD_PE_GET_FPD_ERR(ret_fr, is_fpd_set, s, source_id, addr, is_writ= e); /* Update context-cache */ @@ -1725,11 +1780,16 @@ static bool vtd_do_iommu_translate(VTDAddressSpace = *vtd_as, PCIBus *bus, cc_entry->context_cache_gen =3D s->context_cache_gen; } =20 + /* Try to fetch slpte form IOTLB */ + if ((pasid =3D=3D PCI_NO_PASID) && s->root_scalable) { + pasid =3D VTD_CE_GET_RID2PASID(&ce); + } + /* * We don't need to translate for pass-through context entries. * Also, let's ignore IOTLB caching as well for PT devices. */ - if (vtd_dev_pt_enabled(s, &ce)) { + if (vtd_dev_pt_enabled(s, &ce, pasid)) { entry->iova =3D addr & VTD_PAGE_MASK_4K; entry->translated_addr =3D entry->iova; entry->addr_mask =3D ~VTD_PAGE_MASK_4K; @@ -1750,14 +1810,24 @@ static bool vtd_do_iommu_translate(VTDAddressSpace = *vtd_as, PCIBus *bus, return true; } =20 + iotlb_entry =3D vtd_lookup_iotlb(s, source_id, addr, pasid); + if (iotlb_entry) { + trace_vtd_iotlb_page_hit(source_id, addr, iotlb_entry->slpte, + iotlb_entry->domain_id); + slpte =3D iotlb_entry->slpte; + access_flags =3D iotlb_entry->access_flags; + page_mask =3D iotlb_entry->mask; + goto out; + } + ret_fr =3D vtd_iova_to_slpte(s, &ce, addr, is_write, &slpte, &level, - &reads, &writes, s->aw_bits); + &reads, &writes, s->aw_bits, pasid); VTD_PE_GET_FPD_ERR(ret_fr, is_fpd_set, s, source_id, addr, is_write); =20 page_mask =3D vtd_slpt_level_page_mask(level); access_flags =3D IOMMU_ACCESS_FLAG(reads, writes); - vtd_update_iotlb(s, source_id, vtd_get_domain_id(s, &ce), addr, slpte, - access_flags, level); + vtd_update_iotlb(s, source_id, vtd_get_domain_id(s, &ce, pasid), + addr, slpte, access_flags, level, pasid); out: vtd_iommu_unlock(s); entry->iova =3D addr & page_mask; @@ -1949,7 +2019,7 @@ static void vtd_iotlb_domain_invalidate(IntelIOMMUSta= te *s, uint16_t domain_id) QLIST_FOREACH(vtd_as, &s->vtd_as_with_notifiers, next) { if (!vtd_dev_to_context_entry(s, pci_bus_num(vtd_as->bus), vtd_as->devfn, &ce) && - domain_id =3D=3D vtd_get_domain_id(s, &ce)) { + domain_id =3D=3D vtd_get_domain_id(s, &ce, vtd_as->pasid)) { vtd_sync_shadow_page_table(vtd_as); } } @@ -1957,7 +2027,7 @@ static void vtd_iotlb_domain_invalidate(IntelIOMMUSta= te *s, uint16_t domain_id) =20 static void vtd_iotlb_page_invalidate_notify(IntelIOMMUState *s, uint16_t domain_id, hwaddr addr, - uint8_t am) + uint8_t am, uint32_t pasid) { VTDAddressSpace *vtd_as; VTDContextEntry ce; @@ -1965,9 +2035,11 @@ static void vtd_iotlb_page_invalidate_notify(IntelIO= MMUState *s, hwaddr size =3D (1 << am) * VTD_PAGE_SIZE; =20 QLIST_FOREACH(vtd_as, &(s->vtd_as_with_notifiers), next) { + if (pasid !=3D PCI_NO_PASID && pasid !=3D vtd_as->pasid) + continue; ret =3D vtd_dev_to_context_entry(s, pci_bus_num(vtd_as->bus), vtd_as->devfn, &ce); - if (!ret && domain_id =3D=3D vtd_get_domain_id(s, &ce)) { + if (!ret && domain_id =3D=3D vtd_get_domain_id(s, &ce, vtd_as->pas= id)) { if (vtd_as_has_map_notifier(vtd_as)) { /* * As long as we have MAP notifications registered in @@ -2011,7 +2083,52 @@ static void vtd_iotlb_page_invalidate(IntelIOMMUStat= e *s, uint16_t domain_id, vtd_iommu_lock(s); g_hash_table_foreach_remove(s->iotlb, vtd_hash_remove_by_page, &info); vtd_iommu_unlock(s); - vtd_iotlb_page_invalidate_notify(s, domain_id, addr, am); + vtd_iotlb_page_invalidate_notify(s, domain_id, addr, am, PCI_NO_PASID); +} + +static void vtd_iotlb_page_pasid_invalidate(IntelIOMMUState *s, + uint16_t domain_id, + hwaddr addr, uint8_t am, + uint32_t pasid) +{ + VTDIOTLBPageInvInfo info; + + trace_vtd_inv_desc_iotlb_pasid_pages(domain_id, addr, am, pasid); + + assert(am <=3D VTD_MAMV); + info.domain_id =3D domain_id; + info.addr =3D addr; + info.mask =3D ~((1 << am) - 1); + info.pasid =3D pasid; + vtd_iommu_lock(s); + g_hash_table_foreach_remove(s->iotlb, vtd_hash_remove_by_page_pasid, &= info); + vtd_iommu_unlock(s); + vtd_iotlb_page_invalidate_notify(s, domain_id, addr, am, pasid); +} + +static void vtd_iotlb_pasid_invalidate(IntelIOMMUState *s, uint16_t domain= _id, + uint32_t pasid) +{ + VTDIOTLBPageInvInfo info; + VTDAddressSpace *vtd_as; + VTDContextEntry ce; + + trace_vtd_inv_desc_iotlb_pasid(domain_id, pasid); + + info.domain_id =3D domain_id; + info.pasid =3D pasid; + vtd_iommu_lock(s); + g_hash_table_foreach_remove(s->iotlb, vtd_hash_remove_by_pasid, &info); + vtd_iommu_unlock(s); + + QLIST_FOREACH(vtd_as, &s->vtd_as_with_notifiers, next) { + if (!vtd_dev_to_context_entry(s, pci_bus_num(vtd_as->bus), + vtd_as->devfn, &ce) && + domain_id =3D=3D vtd_get_domain_id(s, &ce, vtd_as->pasid) && + pasid =3D=3D vtd_as->pasid) { + vtd_sync_shadow_page_table(vtd_as); + } + } } =20 /* Flush IOTLB @@ -2388,6 +2505,52 @@ static bool vtd_process_iotlb_desc(IntelIOMMUState *= s, VTDInvDesc *inv_desc) return true; } =20 +static bool vtd_process_iotlb_pasid_desc(IntelIOMMUState *s, + VTDInvDesc *inv_desc) +{ + uint32_t pasid; + uint16_t domain_id; + uint8_t am; + hwaddr addr; + + if ((inv_desc->lo & VTD_INV_DESC_IOTLB_PASID_RSVD_LO) || + (inv_desc->hi & VTD_INV_DESC_IOTLB_PASID_RSVD_HI)) { + error_report_once("%s: invalid iotlb pasid inv desc: hi=3D0x%"PRIx= 64 + ", lo=3D0x%"PRIx64" (reserved bits unzero)", + __func__, inv_desc->hi, inv_desc->lo); + return false; + } + + switch (inv_desc->lo & VTD_INV_DESC_IOTLB_G) { + case VTD_INV_DESC_IOTLB_PASID_PASID: + domain_id =3D VTD_INV_DESC_IOTLB_DID(inv_desc->lo); + pasid =3D VTD_INV_DESC_IOTLB_PASID(inv_desc->lo); + vtd_iotlb_pasid_invalidate(s, domain_id, pasid); + break; + case VTD_INV_DESC_IOTLB_PASID_PAGE: + domain_id =3D VTD_INV_DESC_IOTLB_DID(inv_desc->lo); + pasid =3D VTD_INV_DESC_IOTLB_PASID(inv_desc->lo); + addr =3D VTD_INV_DESC_IOTLB_ADDR(inv_desc->hi); + am =3D VTD_INV_DESC_IOTLB_AM(inv_desc->hi); + if (am > VTD_MAMV) { + error_report_once("%s: invalid iotlb inv desc: hi=3D0x%"PRIx64 + ", lo=3D0x%"PRIx64" (am=3D%u > VTD_MAMV=3D%u= )", + __func__, inv_desc->hi, inv_desc->lo, + am, (unsigned)VTD_MAMV); + return false; + } + vtd_iotlb_page_pasid_invalidate(s, domain_id, addr, am, pasid); + break; + default: + error_report_once("%s: invalid iotlb pasid inv desc: hi=3D0x%"PRIx= 64 + ", lo=3D0x%"PRIx64" (type mismatch: 0x%llx)", + __func__, inv_desc->hi, inv_desc->lo, + inv_desc->lo & VTD_INV_DESC_IOTLB_G); + return false; + } + return true; +} + static bool vtd_process_inv_iec_desc(IntelIOMMUState *s, VTDInvDesc *inv_desc) { @@ -2487,15 +2650,19 @@ static bool vtd_process_inv_desc(IntelIOMMUState *s) } break; =20 - /* - * TODO: the entity of below two cases will be implemented in future s= eries. - * To make guest (which integrates scalable mode support patch set in - * iommu driver) work, just return true is enough so far. - */ case VTD_INV_DESC_PC: + trace_vtd_inv_desc("pc", inv_desc.hi, inv_desc.lo); + /* We don't implement PASID cache, so it's fine to simply check iq= a.dw */ + if (!s->iq_dw) { + return false; + } break; =20 case VTD_INV_DESC_PIOTLB: + trace_vtd_inv_desc("pasid-iotlb", inv_desc.hi, inv_desc.lo); + if (!s->iq_dw || !vtd_process_iotlb_pasid_desc(s, &inv_desc)) { + return false; + } break; =20 case VTD_INV_DESC_WAIT: @@ -3071,6 +3238,7 @@ static Property vtd_properties[] =3D { VTD_HOST_ADDRESS_WIDTH), DEFINE_PROP_BOOL("caching-mode", IntelIOMMUState, caching_mode, FALSE), DEFINE_PROP_BOOL("x-scalable-mode", IntelIOMMUState, scalable_mode, FA= LSE), + DEFINE_PROP_BOOL("x-pasid-mode", IntelIOMMUState, pasid, false), DEFINE_PROP_BOOL("dma-drain", IntelIOMMUState, dma_drain, true), DEFINE_PROP_END_OF_LIST(), }; @@ -3344,11 +3512,13 @@ static const MemoryRegionOps vtd_mem_ir_ops =3D { }, }; =20 -VTDAddressSpace *vtd_find_add_as(IntelIOMMUState *s, PCIBus *bus, int devf= n) +VTDAddressSpace *vtd_find_add_as(IntelIOMMUState *s, PCIBus *bus, + int devfn, unsigned int pasid) { struct vtd_as_key key =3D { .bus =3D bus, .devfn =3D devfn, + .pasid =3D pasid, }; VTDAddressSpace *vtd_dev_as =3D g_hash_table_lookup(s->vtd_as, &key); char name[128]; @@ -3358,6 +3528,7 @@ VTDAddressSpace *vtd_find_add_as(IntelIOMMUState *s, = PCIBus *bus, int devfn) =20 new_key->bus =3D bus; new_key->devfn =3D devfn; + new_key->pasid =3D pasid; =20 snprintf(name, sizeof(name), "vtd-%02x.%x", PCI_SLOT(devfn), PCI_FUNC(devfn)); @@ -3365,6 +3536,7 @@ VTDAddressSpace *vtd_find_add_as(IntelIOMMUState *s, = PCIBus *bus, int devfn) =20 vtd_dev_as->bus =3D bus; vtd_dev_as->devfn =3D (uint8_t)devfn; + vtd_dev_as->pasid =3D pasid; vtd_dev_as->iommu_state =3D s; vtd_dev_as->context_cache_entry.context_cache_gen =3D 0; vtd_dev_as->iova_tree =3D iova_tree_new(); @@ -3420,6 +3592,7 @@ VTDAddressSpace *vtd_find_add_as(IntelIOMMUState *s, = PCIBus *bus, int devfn) =20 g_hash_table_insert(s->vtd_as, new_key, vtd_dev_as); } + return vtd_dev_as; } =20 @@ -3525,7 +3698,7 @@ static void vtd_iommu_replay(IOMMUMemoryRegion *iommu= _mr, IOMMUNotifier *n) "legacy mode", bus_n, PCI_SLOT(vtd_as->devfn), PCI_FUNC(vtd_as->devfn), - vtd_get_domain_id(s, &ce), + vtd_get_domain_id(s, &ce, vtd_as->pasid), ce.hi, ce.lo); if (vtd_as_has_map_notifier(vtd_as)) { /* This is required only for MAP typed notifiers */ @@ -3535,10 +3708,10 @@ static void vtd_iommu_replay(IOMMUMemoryRegion *iom= mu_mr, IOMMUNotifier *n) .notify_unmap =3D false, .aw =3D s->aw_bits, .as =3D vtd_as, - .domain_id =3D vtd_get_domain_id(s, &ce), + .domain_id =3D vtd_get_domain_id(s, &ce, vtd_as->pasid), }; =20 - vtd_page_walk(s, &ce, 0, ~0ULL, &info); + vtd_page_walk(s, &ce, 0, ~0ULL, &info, vtd_as->pasid); } } else { trace_vtd_replay_ce_invalid(bus_n, PCI_SLOT(vtd_as->devfn), @@ -3629,6 +3802,10 @@ static void vtd_init(IntelIOMMUState *s) s->ecap |=3D VTD_ECAP_SMTS | VTD_ECAP_SRS | VTD_ECAP_SLTS; } =20 + if (s->pasid) { + s->ecap |=3D VTD_ECAP_PASID; + } + vtd_reset_caches(s); =20 /* Define registers with default values and bit semantics */ @@ -3702,8 +3879,7 @@ static AddressSpace *vtd_host_dma_iommu(PCIBus *bus, = void *opaque, int devfn) =20 assert(0 <=3D devfn && devfn < PCI_DEVFN_MAX); =20 - vtd_as =3D vtd_find_add_as(s, bus, devfn); - + vtd_as =3D vtd_find_add_as(s, bus, devfn, PCI_NO_PASID); return &vtd_as->as; } =20 @@ -3746,6 +3922,11 @@ static bool vtd_decide_config(IntelIOMMUState *s, Er= ror **errp) return false; } =20 + if (s->pasid && !s->scalable_mode) { + error_setg(errp, "Need to set PASID for scalable mode"); + return false; + } + return true; } =20 @@ -3808,9 +3989,9 @@ static void vtd_realize(DeviceState *dev, Error **err= p) =20 sysbus_init_mmio(SYS_BUS_DEVICE(s), &s->csrmem); /* No corresponding destroy */ - s->iotlb =3D g_hash_table_new_full(vtd_uint64_hash, vtd_uint64_equal, + s->iotlb =3D g_hash_table_new_full(vtd_iotlb_hash, vtd_iotlb_equal, g_free, g_free); - s->vtd_as =3D g_hash_table_new_full(vtd_uint64_hash, vtd_uint64_equal, + s->vtd_as =3D g_hash_table_new_full(vtd_as_hash, vtd_as_equal, g_free, g_free); vtd_init(s); sysbus_mmio_map(SYS_BUS_DEVICE(s), 0, Q35_HOST_BRIDGE_IOMMU_ADDR); diff --git a/hw/i386/intel_iommu_internal.h b/hw/i386/intel_iommu_internal.h index a6c788049b..c0402113e2 100644 --- a/hw/i386/intel_iommu_internal.h +++ b/hw/i386/intel_iommu_internal.h @@ -114,8 +114,9 @@ VTD_INTERRUPT_ADDR_FIRST + 1) =20 /* The shift of source_id in the key of IOTLB hash table */ -#define VTD_IOTLB_SID_SHIFT 36 -#define VTD_IOTLB_LVL_SHIFT 52 +#define VTD_IOTLB_SID_SHIFT 20 +#define VTD_IOTLB_LVL_SHIFT 28 +#define VTD_IOTLB_PASID_SHIFT 30 #define VTD_IOTLB_MAX_SIZE 1024 /* Max size of the hash table = */ =20 /* IOTLB_REG */ @@ -190,6 +191,7 @@ #define VTD_ECAP_PT (1ULL << 6) #define VTD_ECAP_MHMV (15ULL << 20) #define VTD_ECAP_SRS (1ULL << 31) +#define VTD_ECAP_PASID (1ULL << 40) #define VTD_ECAP_SMTS (1ULL << 43) #define VTD_ECAP_SLTS (1ULL << 46) =20 @@ -210,6 +212,8 @@ #define VTD_CAP_DRAIN_READ (1ULL << 55) #define VTD_CAP_DRAIN (VTD_CAP_DRAIN_READ | VTD_CAP_DRAIN_WR= ITE) #define VTD_CAP_CM (1ULL << 7) +#define VTD_PASID_ID_SHIFT 20 +#define VTD_PASID_ID_MASK ((1ULL << VTD_PASID_ID_SHIFT) - 1) =20 /* Supported Adjusted Guest Address Widths */ #define VTD_CAP_SAGAW_SHIFT 8 @@ -379,6 +383,11 @@ typedef union VTDInvDesc VTDInvDesc; #define VTD_INV_DESC_IOTLB_AM(val) ((val) & 0x3fULL) #define VTD_INV_DESC_IOTLB_RSVD_LO 0xffffffff0000ff00ULL #define VTD_INV_DESC_IOTLB_RSVD_HI 0xf80ULL +#define VTD_INV_DESC_IOTLB_PASID_PASID (2ULL << 4) +#define VTD_INV_DESC_IOTLB_PASID_PAGE (3ULL << 4) +#define VTD_INV_DESC_IOTLB_PASID(val) (((val) >> 32) & VTD_PASID_ID_MASK) +#define VTD_INV_DESC_IOTLB_PASID_RSVD_LO 0xfff00000000001c0ULL +#define VTD_INV_DESC_IOTLB_PASID_RSVD_HI 0xf80ULL =20 /* Mask for Device IOTLB Invalidate Descriptor */ #define VTD_INV_DESC_DEVICE_IOTLB_ADDR(val) ((val) & 0xfffffffffffff000ULL) @@ -413,6 +422,7 @@ typedef union VTDInvDesc VTDInvDesc; /* Information about page-selective IOTLB invalidate */ struct VTDIOTLBPageInvInfo { uint16_t domain_id; + uint32_t pasid; uint64_t addr; uint8_t mask; }; diff --git a/hw/i386/trace-events b/hw/i386/trace-events index 5bf7e52bf5..57beff0c17 100644 --- a/hw/i386/trace-events +++ b/hw/i386/trace-events @@ -12,6 +12,8 @@ vtd_inv_desc_cc_devices(uint16_t sid, uint16_t fmask) "co= ntext invalidate device vtd_inv_desc_iotlb_global(void) "iotlb invalidate global" vtd_inv_desc_iotlb_domain(uint16_t domain) "iotlb invalidate whole domain = 0x%"PRIx16 vtd_inv_desc_iotlb_pages(uint16_t domain, uint64_t addr, uint8_t mask) "io= tlb invalidate domain 0x%"PRIx16" addr 0x%"PRIx64" mask 0x%"PRIx8 +vtd_inv_desc_iotlb_pasid_pages(uint16_t domain, uint64_t addr, uint8_t mas= k, uint32_t pasid) "iotlb invalidate domain 0x%"PRIx16" addr 0x%"PRIx64" ma= sk 0x%"PRIx8" pasid 0x%"PRIx32 +vtd_inv_desc_iotlb_pasid(uint16_t domain, uint32_t pasid) "iotlb invalidat= e domain 0x%"PRIx16" pasid 0x%"PRIx32 vtd_inv_desc_wait_sw(uint64_t addr, uint32_t data) "wait invalidate status= write addr 0x%"PRIx64" data 0x%"PRIx32 vtd_inv_desc_wait_irq(const char *msg) "%s" vtd_inv_desc_wait_write_fail(uint64_t hi, uint64_t lo) "write fail for wai= t desc hi 0x%"PRIx64" lo 0x%"PRIx64 diff --git a/include/hw/i386/intel_iommu.h b/include/hw/i386/intel_iommu.h index 014d77dc2a..2fadbf6033 100644 --- a/include/hw/i386/intel_iommu.h +++ b/include/hw/i386/intel_iommu.h @@ -97,6 +97,7 @@ struct VTDPASIDEntry { struct VTDAddressSpace { PCIBus *bus; uint8_t devfn; + uint32_t pasid; AddressSpace as; IOMMUMemoryRegion iommu; MemoryRegion root; /* The root container of the device */ @@ -113,6 +114,7 @@ struct VTDAddressSpace { struct VTDIOTLBEntry { uint64_t gfn; uint16_t domain_id; + uint32_t pasid; uint64_t slpte; uint64_t mask; uint8_t access_flags; @@ -258,6 +260,7 @@ struct IntelIOMMUState { bool buggy_eim; /* Force buggy EIM unless eim=3Doff */ uint8_t aw_bits; /* Host/IOVA address width (in bits) */ bool dma_drain; /* Whether DMA r/w draining enabled */ + bool pasid; /* Whether to support PASID */ =20 /* * Protects IOMMU states in general. Currently it protects the @@ -269,6 +272,7 @@ struct IntelIOMMUState { /* Find the VTD Address space associated with the given bus pointer, * create a new one if none exists */ -VTDAddressSpace *vtd_find_add_as(IntelIOMMUState *s, PCIBus *bus, int devf= n); +VTDAddressSpace *vtd_find_add_as(IntelIOMMUState *s, PCIBus *bus, + int devfn, unsigned int pasid); =20 #endif diff --git a/include/hw/pci/pci_bus.h b/include/hw/pci/pci_bus.h index 347440d42c..cbfcf0b770 100644 --- a/include/hw/pci/pci_bus.h +++ b/include/hw/pci/pci_bus.h @@ -26,6 +26,8 @@ enum PCIBusFlags { PCI_BUS_EXTENDED_CONFIG_SPACE =3D 0x0002, }; =20 +#define PCI_NO_PASID UINT32_MAX + struct PCIBus { BusState qbus; enum PCIBusFlags flags; --=20 2.25.1 From nobody Sun May 5 03:18:11 2024 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=fail; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=fail(p=none dis=none) header.from=redhat.com Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1641356567250565.746909543581; Tue, 4 Jan 2022 20:22:47 -0800 (PST) Received: from localhost ([::1]:36876 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1n4xp0-0006gV-8W for importer@patchew.org; Tue, 04 Jan 2022 23:22:46 -0500 Received: from eggs.gnu.org ([209.51.188.92]:44302) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1n4xmU-00041v-Gu for qemu-devel@nongnu.org; Tue, 04 Jan 2022 23:20:13 -0500 Received: from us-smtp-delivery-124.mimecast.com ([170.10.133.124]:50654) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1n4xmQ-0002ab-VM for qemu-devel@nongnu.org; Tue, 04 Jan 2022 23:20:09 -0500 Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-498-QENUZUKhPM68ThBNBKDIlw-1; Tue, 04 Jan 2022 23:20:04 -0500 Received: from smtp.corp.redhat.com (int-mx06.intmail.prod.int.phx2.redhat.com [10.5.11.16]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id 03F0A1006AA6; Wed, 5 Jan 2022 04:20:04 +0000 (UTC) Received: from localhost.localdomain (ovpn-13-255.pek2.redhat.com [10.72.13.255]) by smtp.corp.redhat.com (Postfix) with ESMTP id C72D16ABB6; Wed, 5 Jan 2022 04:19:58 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1641356406; 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=GCsShkGNTOHYe/jzI4mAbQV4tOhTyhlj3BBfbJymxV8=; b=ZKkS04IdCOYhdLknE94Dk+AGwwfYH0JSZqvY5C4dGLaLwTxqJcEtJakq6N9qnb/Ue2uaIA eiIgUw36xTATnDteS88HIKhiObNdOvpwEYZENgg0VB1xeG7iyKj3g8TQ36lWCwmz9jakzi NX8F1BcdoWpLt1J92LSeN0vncsZH1LQ= X-MC-Unique: QENUZUKhPM68ThBNBKDIlw-1 From: Jason Wang To: mst@redhat.com, peterx@redhat.com Subject: [PATCH] intel-iommu: correctly check passthrough during translation Date: Wed, 5 Jan 2022 12:19:42 +0800 Message-Id: <20220105041945.13459-2-jasowang@redhat.com> In-Reply-To: <20220105041945.13459-1-jasowang@redhat.com> References: <20220105041945.13459-1-jasowang@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.16 Authentication-Results: relay.mimecast.com; auth=pass smtp.auth=CUSA124A263 smtp.mailfrom=jasowang@redhat.com X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Transfer-Encoding: quoted-printable Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: pass client-ip=170.10.133.124; envelope-from=jasowang@redhat.com; helo=us-smtp-delivery-124.mimecast.com X-Spam_score_int: -31 X-Spam_score: -3.2 X-Spam_bar: --- X-Spam_report: (-3.2 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-0.37, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_LOW=-0.7, RCVD_IN_MSPIKE_H3=0.001, RCVD_IN_MSPIKE_WL=0.001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Jason Wang , yi.l.liu@intel.com, yi.y.sun@linux.intel.com, qemu-devel@nongnu.org Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail-DKIM: fail (Header signature does not verify) X-ZM-MESSAGEID: 1641356568757100001 Content-Type: text/plain; charset="utf-8" When scsalable mode is enabled, the passthrough more is not determined by the context entry but PASID entry, so switch to use the logic of vtd_dev_pt_enabled() to determine the passthrough mode in vtd_do_iommu_translate(). Signed-off-by: Jason Wang --- hw/i386/intel_iommu.c | 38 +++++++++++++++++++++++--------------- 1 file changed, 23 insertions(+), 15 deletions(-) diff --git a/hw/i386/intel_iommu.c b/hw/i386/intel_iommu.c index f584449d8d..f346a82652 100644 --- a/hw/i386/intel_iommu.c +++ b/hw/i386/intel_iommu.c @@ -1512,11 +1512,29 @@ static int vtd_sync_shadow_page_table(VTDAddressSpa= ce *vtd_as) * 1st-level translation or 2nd-level translation, it depends * on PGTT setting. */ -static bool vtd_dev_pt_enabled(VTDAddressSpace *as) +static bool vtd_dev_pt_enabled(IntelIOMMUState *s, VTDContextEntry *ce) +{ + VTDPASIDEntry pe; + int ret; + + if (s->root_scalable) { + ret =3D vtd_ce_get_rid2pasid_entry(s, ce, &pe); + if (ret) { + error_report_once("%s: vtd_ce_get_rid2pasid_entry error: %"PRI= d32, + __func__, ret); + return false; + } + return (VTD_PE_GET_TYPE(&pe) =3D=3D VTD_SM_PASID_ENTRY_PT); + } + + return (vtd_ce_get_type(ce) =3D=3D VTD_CONTEXT_TT_PASS_THROUGH); + +} + +static bool vtd_as_pt_enabled(VTDAddressSpace *as) { IntelIOMMUState *s; VTDContextEntry ce; - VTDPASIDEntry pe; int ret; =20 assert(as); @@ -1534,17 +1552,7 @@ static bool vtd_dev_pt_enabled(VTDAddressSpace *as) return false; } =20 - if (s->root_scalable) { - ret =3D vtd_ce_get_rid2pasid_entry(s, &ce, &pe); - if (ret) { - error_report_once("%s: vtd_ce_get_rid2pasid_entry error: %"PRI= d32, - __func__, ret); - return false; - } - return (VTD_PE_GET_TYPE(&pe) =3D=3D VTD_SM_PASID_ENTRY_PT); - } - - return (vtd_ce_get_type(&ce) =3D=3D VTD_CONTEXT_TT_PASS_THROUGH); + return vtd_dev_pt_enabled(s, &ce); } =20 /* Return whether the device is using IOMMU translation. */ @@ -1556,7 +1564,7 @@ static bool vtd_switch_address_space(VTDAddressSpace = *as) =20 assert(as); =20 - use_iommu =3D as->iommu_state->dmar_enabled && !vtd_dev_pt_enabled(as); + use_iommu =3D as->iommu_state->dmar_enabled && !vtd_as_pt_enabled(as); =20 trace_vtd_switch_address_space(pci_bus_num(as->bus), VTD_PCI_SLOT(as->devfn), @@ -1749,7 +1757,7 @@ static bool vtd_do_iommu_translate(VTDAddressSpace *v= td_as, PCIBus *bus, * We don't need to translate for pass-through context entries. * Also, let's ignore IOTLB caching as well for PT devices. */ - if (vtd_ce_get_type(&ce) =3D=3D VTD_CONTEXT_TT_PASS_THROUGH) { + if (vtd_dev_pt_enabled(s, &ce)) { entry->iova =3D addr & VTD_PAGE_MASK_4K; entry->translated_addr =3D entry->iova; entry->addr_mask =3D ~VTD_PAGE_MASK_4K; --=20 2.25.1