From nobody Tue Feb 10 04:17:19 2026 Delivered-To: importer@patchew.org Received-SPF: pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) client-ip=192.237.175.120; envelope-from=xen-devel-bounces@lists.xenproject.org; helo=lists.xenproject.org; Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) smtp.mailfrom=xen-devel-bounces@lists.xenproject.org ARC-Seal: i=1; a=rsa-sha256; t=1605878714; cv=none; d=zohomail.com; s=zohoarc; b=C7oNL46Fg1gh4trgJw02VMjZ5hOraAwECMUWGeLBlNYIhDh57g3ALiu/3mSBEknrdb+TsuH6rWbz/sp+HlJRnW1TJj9QVeE7T1kh5y8m9VwcQWVmisIgHseG5gAGN7xnfM4m1wKskFEgZYqV4hpCQwYH2TIXZDLigAKZy+6wTKY= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1605878714; h=Content-Transfer-Encoding:Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To; bh=m/FA9f+1NMog7KtW7VWghtLgO7OJlubNnvmswOBT8GE=; b=au4P/pf7DFX9EN0x7+6voJXilFG7fT4B/+BTmzNysjsYzy7eFauvEE3T5T1xjPEm9THSj1MOe3yHjHmA2IJX1nI4hwnWF3qpcnxbBlezivTdHxfZd/OcK2dEYy70YXP+cBlVsY4O0BEiLLiw/EwIPsbRw/saYxmyk7kqTAUFISM= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) smtp.mailfrom=xen-devel-bounces@lists.xenproject.org Return-Path: Received: from lists.xenproject.org (lists.xenproject.org [192.237.175.120]) by mx.zohomail.com with SMTPS id 1605878714024224.42853217707625; Fri, 20 Nov 2020 05:25:14 -0800 (PST) Received: from list by lists.xenproject.org with outflank-mailman.32191.63220 (Exim 4.92) (envelope-from ) id 1kg6PF-0001tQ-Mu; Fri, 20 Nov 2020 13:24:53 +0000 Received: by outflank-mailman (output) from mailman id 32191.63220; Fri, 20 Nov 2020 13:24:53 +0000 Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1kg6PF-0001sk-6v; Fri, 20 Nov 2020 13:24:53 +0000 Received: by outflank-mailman (input) for mailman id 32191; Fri, 20 Nov 2020 13:24:52 +0000 Received: from mail.xenproject.org ([104.130.215.37]) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1kg6PE-0001pm-3O for xen-devel@lists.xenproject.org; Fri, 20 Nov 2020 13:24:52 +0000 Received: from xenbits.xenproject.org ([104.239.192.120]) by mail.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1kg6PC-0007DU-Ki; Fri, 20 Nov 2020 13:24:50 +0000 Received: from host109-146-187-185.range109-146.btcentralplus.com ([109.146.187.185] helo=u2f063a87eabd5f.home) by xenbits.xenproject.org with esmtpsa (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1kg6PC-00028m-D4; Fri, 20 Nov 2020 13:24:50 +0000 Received: from mail.xenproject.org ([104.130.215.37]) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1kg6PE-0001pm-3O for xen-devel@lists.xenproject.org; Fri, 20 Nov 2020 13:24:52 +0000 Received: from xenbits.xenproject.org ([104.239.192.120]) by mail.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1kg6PC-0007DU-Ki; Fri, 20 Nov 2020 13:24:50 +0000 Received: from host109-146-187-185.range109-146.btcentralplus.com ([109.146.187.185] helo=u2f063a87eabd5f.home) by xenbits.xenproject.org with esmtpsa (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1kg6PC-00028m-D4; Fri, 20 Nov 2020 13:24:50 +0000 X-Outflank-Mailman: Message body and most headers restored to incoming version X-BeenThere: xen-devel@lists.xenproject.org List-Id: Xen developer discussion List-Unsubscribe: , List-Post: List-Help: List-Subscribe: , Errors-To: xen-devel-bounces@lists.xenproject.org Precedence: list Sender: "Xen-devel" DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=xen.org; s=20200302mail; h=Content-Transfer-Encoding:MIME-Version:References: In-Reply-To:Message-Id:Date:Subject:Cc:To:From; bh=m/FA9f+1NMog7KtW7VWghtLgO7OJlubNnvmswOBT8GE=; b=KNufUPXE/aiJSp3zDmY1xVcVhO 5Tx3Oo7U0YZNC3A6EzoDlIfdJAD2iepFY1bwqZJJ+EEaYyGciqS0aUwB+6Mbd3wONHciGrAp5IMRt qoGjnjnQXlUEIhNB4su0r/nDuYYGcPhS4/9nDqvj2xkuneJTyFwsH9uAF7mvPXf8I5Jg=; From: Paul Durrant To: xen-devel@lists.xenproject.org Cc: Paul Durrant , Kevin Tian Subject: [PATCH v10 6/7] vtd: use a bit field for context_entry Date: Fri, 20 Nov 2020 13:24:39 +0000 Message-Id: <20201120132440.1141-7-paul@xen.org> X-Mailer: git-send-email 2.20.1 In-Reply-To: <20201120132440.1141-1-paul@xen.org> References: <20201120132440.1141-1-paul@xen.org> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-ZohoMail-DKIM: pass (identity @xen.org) Content-Type: text/plain; charset="utf-8" From: Paul Durrant This removes the need for much shifting, masking and several magic numbers. On the whole it makes the code quite a bit more readable. Signed-off-by: Paul Durrant Reviewed-by: Jan Beulich Reviewed-by: Kevin Tian --- Cc: Kevin Tian v10: - Remove macros in favour of direct field access - Adjust field types - Add missing barriers v4: - New in v4 --- xen/drivers/passthrough/vtd/iommu.c | 36 +++++++++++---------- xen/drivers/passthrough/vtd/iommu.h | 45 +++++++++++++-------------- xen/drivers/passthrough/vtd/utils.c | 10 +++--- xen/drivers/passthrough/vtd/x86/ats.c | 6 ++-- 4 files changed, 47 insertions(+), 50 deletions(-) diff --git a/xen/drivers/passthrough/vtd/iommu.c b/xen/drivers/passthrough/= vtd/iommu.c index 1a038541f0a3..fdb472ad6515 100644 --- a/xen/drivers/passthrough/vtd/iommu.c +++ b/xen/drivers/passthrough/vtd/iommu.c @@ -86,8 +86,6 @@ static int domain_iommu_domid(struct domain *d, return -1; } =20 -#define DID_FIELD_WIDTH 16 -#define DID_HIGH_OFFSET 8 static int context_set_domain_id(struct context_entry *context, struct domain *d, struct vtd_iommu *iommu) @@ -121,21 +119,22 @@ static int context_set_domain_id(struct context_entry= *context, } =20 set_bit(i, iommu->domid_bitmap); - context->hi |=3D (i & ((1 << DID_FIELD_WIDTH) - 1)) << DID_HIGH_OFFSET; + context->did =3D i; + return 0; } =20 static int context_get_domain_id(struct context_entry *context, struct vtd_iommu *iommu) { - unsigned long dom_index, nr_dom; int domid =3D -1; =20 if (iommu && context) { - nr_dom =3D cap_ndoms(iommu->cap); + unsigned long dom_index, nr_dom; =20 - dom_index =3D context_domain_id(*context); + nr_dom =3D cap_ndoms(iommu->cap); + dom_index =3D context->did; =20 if ( dom_index < nr_dom && iommu->domid_map ) domid =3D iommu->domid_map[dom_index]; @@ -1338,7 +1337,7 @@ int domain_context_mapping_one( context_entries =3D (struct context_entry *)map_vtd_domain_page(maddr); context =3D &context_entries[devfn]; =20 - if ( context_present(*context) ) + if ( context->p ) { int res =3D 0; =20 @@ -1382,7 +1381,7 @@ int domain_context_mapping_one( =20 if ( iommu_hwdom_passthrough && is_hardware_domain(domain) ) { - context_set_translation_type(*context, CONTEXT_TT_PASS_THRU); + context->tt =3D CONTEXT_TT_PASS_THRU; } else { @@ -1397,11 +1396,11 @@ int domain_context_mapping_one( return -ENOMEM; } =20 - context_set_address_root(*context, pgd_maddr); + context->slptptr =3D paddr_to_pfn(pgd_maddr); if ( ats_enabled && ecap_dev_iotlb(iommu->ecap) ) - context_set_translation_type(*context, CONTEXT_TT_DEV_IOTLB); + context->tt =3D CONTEXT_TT_DEV_IOTLB; else - context_set_translation_type(*context, CONTEXT_TT_MULTI_LEVEL); + context->tt =3D CONTEXT_TT_MULTI_LEVEL; =20 spin_unlock(&hd->arch.mapping_lock); } @@ -1413,9 +1412,10 @@ int domain_context_mapping_one( return -EFAULT; } =20 - context_set_address_width(*context, level_to_agaw(iommu->nr_pt_levels)= ); - context_set_fault_enable(*context); - context_set_present(*context); + context->aw =3D level_to_agaw(iommu->nr_pt_levels); + context->fpd =3D false; + smp_wmb(); + context->p =3D true; iommu_sync_cache(context, sizeof(struct context_entry)); spin_unlock(&iommu->lock); =20 @@ -1567,17 +1567,19 @@ int domain_context_unmap_one( context_entries =3D (struct context_entry *)map_vtd_domain_page(maddr); context =3D &context_entries[devfn]; =20 - if ( !context_present(*context) ) + if ( !context->p ) { spin_unlock(&iommu->lock); unmap_vtd_domain_page(context_entries); return 0; } =20 - context_clear_present(*context); - context_clear_entry(*context); + context->p =3D false; + smp_wmb(); iommu_sync_cache(context, sizeof(struct context_entry)); =20 + context->val =3D 0; /* No need to sync; present bit is already cleared= */ + iommu_domid=3D domain_iommu_domid(domain, iommu); if ( iommu_domid =3D=3D -1 ) { diff --git a/xen/drivers/passthrough/vtd/iommu.h b/xen/drivers/passthrough/= vtd/iommu.h index b14628eec260..33b1abf98526 100644 --- a/xen/drivers/passthrough/vtd/iommu.h +++ b/xen/drivers/passthrough/vtd/iommu.h @@ -198,37 +198,34 @@ struct root_entry { }; }; }; +#define ROOT_ENTRY_NR (PAGE_SIZE_4K / sizeof(struct root_entry)) =20 struct context_entry { - u64 lo; - u64 hi; -}; -#define ROOT_ENTRY_NR (PAGE_SIZE_4K/sizeof(struct root_entry)) -#define context_present(c) ((c).lo & 1) -#define context_fault_disable(c) (((c).lo >> 1) & 1) -#define context_translation_type(c) (((c).lo >> 2) & 3) -#define context_address_root(c) ((c).lo & PAGE_MASK_4K) -#define context_address_width(c) ((c).hi & 7) -#define context_domain_id(c) (((c).hi >> 8) & ((1 << 16) - 1)) + union { + __uint128_t val; + struct { uint64_t lo, hi; }; + struct { + /* 0 - 63 */ + bool p:1; + bool fpd:1; + uint64_t tt:2; =20 -#define context_set_present(c) do {(c).lo |=3D 1;} while(0) -#define context_clear_present(c) do {(c).lo &=3D ~1;} while(0) -#define context_set_fault_enable(c) \ - do {(c).lo &=3D (((u64)-1) << 2) | 1;} while(0) - -#define context_set_translation_type(c, val) do { \ - (c).lo &=3D (((u64)-1) << 4) | 3; \ - (c).lo |=3D (val & 3) << 2; \ - } while(0) #define CONTEXT_TT_MULTI_LEVEL 0 #define CONTEXT_TT_DEV_IOTLB 1 #define CONTEXT_TT_PASS_THRU 2 =20 -#define context_set_address_root(c, val) \ - do {(c).lo &=3D 0xfff; (c).lo |=3D (val) & PAGE_MASK_4K ;} while(0) -#define context_set_address_width(c, val) \ - do {(c).hi &=3D 0xfffffff8; (c).hi |=3D (val) & 7;} while(0) -#define context_clear_entry(c) do {(c).lo =3D 0; (c).hi =3D 0;} while(0) + unsigned int reserved0:8; + uint64_t slptptr:52; + + /* 64 - 127 */ + unsigned int aw:3; + unsigned int ignored:4; + unsigned int reserved1:1; + unsigned int did:16; + uint64_t reserved2:40; + }; + }; +}; =20 /* page table handling */ #define LEVEL_STRIDE (9) diff --git a/xen/drivers/passthrough/vtd/utils.c b/xen/drivers/passthrough/= vtd/utils.c index 5f25a86a535c..4bca160bc663 100644 --- a/xen/drivers/passthrough/vtd/utils.c +++ b/xen/drivers/passthrough/vtd/utils.c @@ -129,17 +129,17 @@ void print_vtd_entries(struct vtd_iommu *iommu, int b= us, int devfn, u64 gmfn) return; } =20 - val =3D ctxt_entry[devfn].lo; - printk(" context[%02x] =3D %"PRIx64"_%"PRIx64"\n", - devfn, ctxt_entry[devfn].hi, val); - if ( !context_present(ctxt_entry[devfn]) ) + printk(" context[%02x] =3D %"PRIx64"_%"PRIx64"\n", devfn, + ctxt_entry[devfn].hi, ctxt_entry[devfn].lo); + if ( !ctxt_entry[devfn].p ) { unmap_vtd_domain_page(ctxt_entry); printk(" ctxt_entry[%02x] not present\n", devfn); return; } =20 - level =3D agaw_to_level(context_address_width(ctxt_entry[devfn])); + level =3D agaw_to_level(ctxt_entry[devfn].aw); + val =3D pfn_to_paddr(ctxt_entry[devfn].slptptr); unmap_vtd_domain_page(ctxt_entry); if ( level !=3D VTD_PAGE_TABLE_LEVEL_3 && level !=3D VTD_PAGE_TABLE_LEVEL_4) diff --git a/xen/drivers/passthrough/vtd/x86/ats.c b/xen/drivers/passthroug= h/vtd/x86/ats.c index fec969ef75bb..cb057ced3cf7 100644 --- a/xen/drivers/passthrough/vtd/x86/ats.c +++ b/xen/drivers/passthrough/vtd/x86/ats.c @@ -76,7 +76,6 @@ static bool device_in_domain(const struct vtd_iommu *iomm= u, { struct root_entry *root_entry, *root_entries; struct context_entry *context_entry, *context_entries =3D NULL; - unsigned int tt; bool found =3D false; =20 if ( unlikely(!iommu->root_maddr) ) @@ -92,11 +91,10 @@ static bool device_in_domain(const struct vtd_iommu *io= mmu, =20 context_entries =3D map_vtd_domain_page(root_entry->ctp); context_entry =3D &context_entries[pdev->devfn]; - if ( context_domain_id(*context_entry) !=3D did ) + if ( context_entry->did !=3D did ) goto out; =20 - tt =3D context_translation_type(*context_entry); - if ( tt !=3D CONTEXT_TT_DEV_IOTLB ) + if ( context_entry->tt !=3D CONTEXT_TT_DEV_IOTLB ) goto out; =20 found =3D true; --=20 2.20.1