From patchwork Fri May 27 11:17:35 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Jan Beulich X-Patchwork-Id: 12863301 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from lists.xenproject.org (lists.xenproject.org [192.237.175.120]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id F2A0EC433FE for ; Fri, 27 May 2022 11:17:54 +0000 (UTC) Received: from list by lists.xenproject.org with outflank-mailman.337986.562717 (Exim 4.92) (envelope-from ) id 1nuXyR-0002li-Qm; Fri, 27 May 2022 11:17:43 +0000 X-Outflank-Mailman: Message body and most headers restored to incoming version Received: by outflank-mailman (output) from mailman id 337986.562717; Fri, 27 May 2022 11:17:43 +0000 Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1nuXyR-0002lb-Nq; Fri, 27 May 2022 11:17:43 +0000 Received: by outflank-mailman (input) for mailman id 337986; Fri, 27 May 2022 11:17:42 +0000 Received: from se1-gles-sth1-in.inumbo.com ([159.253.27.254] helo=se1-gles-sth1.inumbo.com) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1nuXyQ-0001xA-G4 for xen-devel@lists.xenproject.org; Fri, 27 May 2022 11:17:42 +0000 Received: from de-smtp-delivery-102.mimecast.com (de-smtp-delivery-102.mimecast.com [194.104.111.102]) by se1-gles-sth1.inumbo.com (Halon) with ESMTPS id 9f73572c-ddae-11ec-bd2c-47488cf2e6aa; Fri, 27 May 2022 13:17:41 +0200 (CEST) Received: from EUR05-DB8-obe.outbound.protection.outlook.com (mail-db8eur05lp2111.outbound.protection.outlook.com [104.47.17.111]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id de-mta-40-U5vlanUjPPKZrKiI8f-M4g-1; Fri, 27 May 2022 13:17:39 +0200 Received: from VE1PR04MB6560.eurprd04.prod.outlook.com (2603:10a6:803:122::25) by AM6PR04MB5831.eurprd04.prod.outlook.com (2603:10a6:20b:a8::18) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.5293.13; Fri, 27 May 2022 11:17:37 +0000 Received: from VE1PR04MB6560.eurprd04.prod.outlook.com ([fe80::dfa:a64a:432f:e26b]) by VE1PR04MB6560.eurprd04.prod.outlook.com ([fe80::dfa:a64a:432f:e26b%7]) with mapi id 15.20.5293.013; Fri, 27 May 2022 11:17:37 +0000 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" X-Inumbo-ID: 9f73572c-ddae-11ec-bd2c-47488cf2e6aa DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.com; s=mimecast20200619; t=1653650261; 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=yOmd6nlV3KqMUXfqufTnFOwdi1OjszdzmaKj6+ae9bI=; b=co63amCtMSIcFcYCVdR0FBOQz7md9iwAmFLWYa7UPtoq+8oOFdZEHp9baaZXFqw1cwnvcl +7RJyJT7L3wyEMuJQrd8EJ2Mls5ddIDlTfeRiKWguclzOeoak1GJryAASXs5J5kXajYhmV FM1DVY2nxDpRKbWo54Yvidhxzwyh2LY= X-MC-Unique: U5vlanUjPPKZrKiI8f-M4g-1 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=cycgSMFvQJJguyJomSb4NqgXvqeY+u/O30zML15NWW5Glux3Y8sfj6ReU5ZgdDAj1Q2flBeC/HmYfAokeM/hVwLA/ZbRU0pXa36UGXxwTSWGS18b/R8n+KbrJmGH4ND5XMB9d4kp3BXDOJ9bKmTN99a6op2w7K09XsPOQ43rggXOjS+4gbyhJWMif8Xuwu5CIkyKvgHoMQG+s2f2FlFZIA27TpLAeW/Wjh/gshWP3z3VaOVtUvYg44fixX+aK19UZwazgqjY3jEu94OdekKWWhCmJGFOSEpqkdq14YiN8oIfXzu5cpiXdyg9ExkaVm2ac1oEfmCSnMvhCR0z+SYq1A== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector9901; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1; bh=yOmd6nlV3KqMUXfqufTnFOwdi1OjszdzmaKj6+ae9bI=; b=Oj7xDNdwkx7zT/+mG8fJQS6gKGyWDmGPk1FFfMmv4qvGrH7ZQUPCseG+EyfjliPlcjdpMMJ1ALW7ka6776izvPYke3BXc5KbLNUeXaLNVHXQ0jK86fbJoKXEhmbpqFwa6kWmsNuUU4ZepSSBJhOY0fqC7L0r64ag1bxp/Y4UuPo0pjg1emqqepywphrxiOLxNibLA5NdEpY/jCxJfr8gN8Xt+0rGI8Pvv3qhKx1vCaauK1OqchVzdPXT5/ofQ6kksnPaXplOl0fbJrH3SY6VSr8weaTwskvLGohyN4iLIxapuin3GkRMzoSHJ4Uvv4zF+vBVQ1UV3UzxLQgpIOOEyg== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass smtp.mailfrom=suse.com; dmarc=pass action=none header.from=suse.com; dkim=pass header.d=suse.com; arc=none Authentication-Results: dkim=none (message not signed) header.d=none;dmarc=none action=none header.from=suse.com; Message-ID: <1df469a9-ddf2-2036-105a-f303f0554f06@suse.com> Date: Fri, 27 May 2022 13:17:35 +0200 User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:91.0) Gecko/20100101 Thunderbird/91.9.1 Subject: [PATCH v5 08/15] IOMMU/x86: prefill newly allocate page tables Content-Language: en-US From: Jan Beulich To: "xen-devel@lists.xenproject.org" Cc: Andrew Cooper , Paul Durrant , =?utf-8?q?Roger_Pau_Monn=C3=A9?= References: <80448822-bc1c-9f7d-ade5-fdf7c46421fe@suse.com> In-Reply-To: <80448822-bc1c-9f7d-ade5-fdf7c46421fe@suse.com> X-ClientProxiedBy: AS9PR04CA0125.eurprd04.prod.outlook.com (2603:10a6:20b:531::16) To VE1PR04MB6560.eurprd04.prod.outlook.com (2603:10a6:803:122::25) MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-Office365-Filtering-Correlation-Id: 5c85d85b-2366-4447-e41b-08da3fd28184 X-MS-TrafficTypeDiagnostic: AM6PR04MB5831:EE_ X-Microsoft-Antispam-PRVS: X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: DCW/+L1wfhFMsE1TCd+QyiYZjHhlmKkANqwjQywJ6vex93MOk7PteQcygzBqzluK30h5p0illniaC2v4vbhoJH5AvwKaItTxBXHqdzSw1XTnVlcy7FxZire6oYEHofhCjWshkrfWhXyY0hx5cQ8E/CLTK7dDqyF+36sOVpwuyVho4yx2LTpgc4nUvk1RTc34eEYYpJSRIORAeeyZPhDN5Ek+wMyAY9vzdr/nyuiA49IumpGDGTiN+45r2YOujR3lboAWxZVwWJGXAboZM0vcCOgM1/yPN9GBeqSLt5o+NfJJjA88Y/SDdYuTpm0Wj4iF2mhhtbjOWrHlsUBpcv/KTgkM3xnCz6/Ix8RL93a2IZDSmUnJhtE1p5oH6d79mx4vdswAGKPhvxuQKiwuX1CATs5FdQ1s+eBn6UrpB3lZA9YuAona+4w5lkziGG2fZHeXckYZPTvlN9b3FnB++s3wjt9xP5Pux7BRMOcI2eEtyCWmJGpz5jgEGS9rMhokbiaigQQ+PN3pMQ/iyn1etDVkeh6fr1mO5KvXCHeFrGkiASCNOa6onuupXAVtRdGbTpp7Wy6f9nd8QZy41mZa+kYfRfa+Hyh/mjSZMY0KU3OcPdxYE0vSepPSte9m5dbc29CtyzzGdsp9D66nD/sEtFGWib9kPZA2n2y+3YPk47OMUekfvEttgPPZEOGE6LGsMNQRsMgP4guph4FjrrocUQQR1/8aBTy/vCNBQgYCiYcsqiGdDvdP+t4gGIG9dXOizKSZ X-Forefront-Antispam-Report: CIP:255.255.255.255;CTRY:;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:VE1PR04MB6560.eurprd04.prod.outlook.com;PTR:;CAT:NONE;SFS:(13230001)(366004)(6512007)(2616005)(26005)(6506007)(316002)(66556008)(66946007)(54906003)(508600001)(8676002)(4326008)(66476007)(6486002)(86362001)(38100700002)(31696002)(186003)(83380400001)(5660300002)(31686004)(2906002)(8936002)(36756003)(6916009)(45980500001)(43740500002);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: =?utf-8?q?lVDMe0cNhKuYihPI2LtzX0BTZ5VB?= =?utf-8?q?wPbF1f8Ri0B04caYXM76Z4Xg/HfbKL5eLvK/k4OtfpeZkU1yNaqcR96WflwVHEwr2?= =?utf-8?q?r4nUw5k4VGHKop1QplpYH7lQqV+LsNub/V4CVhgIM0WRwfWDlH1LytE6+7qYFcgxj?= =?utf-8?q?dee6Neqpo98rzAeneIzgc5P46YnuIBmImcjN3j3eVf3SzN3qHmk7Jzp5bRQxrBkrK?= =?utf-8?q?kzQ2OPXNUfq+zJ/cF3M9oH2RHAWJXmdkZaDieyDiX+cGBxLrEPxnmakmvOknahmLD?= =?utf-8?q?PAXXPlzDzkPw31Me/Cvm5VxfUPjyVaQUUdGA7D1xkGVWURJP6ZyeRZIEYG5VsCSik?= =?utf-8?q?YkNkUf1XEq6yyG7AQxPrOiptXVUt9hKyZhZGRfmZd42opidY0tPt+dPUY9JyNVpLq?= =?utf-8?q?a53oXbJxfQCqnGkqUdAIRyzXAMwjzjudmptnAYp3mj4r2AxCGXvc1RrJ5RqKEqfNg?= =?utf-8?q?mTIfk1gLMbpfXMyQm0kxyr3K3imOnLMg3vOuQXOPGx9b4Xjt7dSmAzBdWS75L/uOl?= =?utf-8?q?e1VCXrp/bq3q9eHzEG3f5eYmzmftGaV1STaVSNsJi0D5wF4jG/qYXsGuMlEv20c8O?= =?utf-8?q?CHLdbQvLAFIGHr4XpEycFakS33yc1UI4Hh8CnoeklBszkLyS1wleF+7llV3LuWTLO?= =?utf-8?q?VhGT5leNucBK5kO38bWjQ0jCSeUrXB3ncD4W5XQuWnP/tdRuKMU//Rlvz6dEZ/+T8?= =?utf-8?q?zqKaMhFWpsric0Pfa+7p2DPwmlwBKwRIq2vIYJfY3mRreD6IN7XzekIdt2O9GlAXR?= =?utf-8?q?WOTEd5RVPSn2N5yvDPJPtsUcm57KXCoPFSdx31iH/jhbfnC9hu+CDykMvOT+aRNUm?= =?utf-8?q?dCjsSo9Gp+8GtPkLFXXdDz/BG0+3POec3MaLYhdMt9n6xQMz1mGClFNkDdVNK3p2N?= =?utf-8?q?DLm/eOwb/N6s7XZF28+xs6iwc2ITOB8LQz1WnQmTyYY88FTAi+rPYVDS6A5YvJ8Kf?= =?utf-8?q?3yb9c/uLDRDk6un/Fzm+X4B/Z18wON7fCwOqoS/eqoYVGypSnmBs2bilzWQVRQjip?= =?utf-8?q?d83+96vqRXaTOXdEfEDIJrYbXfpkueE8fZEoB5eI2QsUdycTX+fTARmdqwDgRbbhm?= =?utf-8?q?/JHQh5he29aLirtUIb5xZB3OerAlP5IAF47K64i4GES0LzwZk3nZldmQpP/JUUwPq?= =?utf-8?q?d03YgspOBl2AeQOLdZNwCNMQwsaYTqN0D1kubh0Rd8U4swheYwGSVSrGJjxCt2AlS?= =?utf-8?q?Q6TSXmc/hSAOcRv491Gae6/3oDUGE3hvVX3QYpI5Z/RDoxyGKRCnH0Ekts9/GWgRz?= =?utf-8?q?LVJNC1+HXcZytJowkRZJ+1F845bTKJ80f4cALXRz15Ww5Ju9PEGKSBmN4D9ut3X7W?= =?utf-8?q?10P5+6ItehAlKV8FaxJtThE+A8nIZiwccTa/R/DsVx1sVEtrlGYzv8vHB3Wo8mHoR?= =?utf-8?q?Ps3EML0BhsFeuitYvUpxs9wOW7X/9C4o7bUDARaMRIkBX8r6wQGIH2Ox7RMw6zQjb?= =?utf-8?q?Mkibkj3QfK2wUahm0js1X46wmv5obEoxorS526X7zgcKtb7++dDKoVT6ZkVo1Kebc?= =?utf-8?q?iCz8BJdxdspEdhGgh8e+51xeJYDpphfKEUxxm9aMxc7dmGlZnr/UOitIgJVQ1Pxr2?= =?utf-8?q?hvZspY/iRkOLj9eWkXObV8mz4jE3RbuqqNTfJ3v0opEpBfUOnevLuOLFAJTYYodvw?= =?utf-8?q?Tryto5+p+q2lu3PmrsqDnW8bOvKn4tAA=3D=3D?= X-OriginatorOrg: suse.com X-MS-Exchange-CrossTenant-Network-Message-Id: 5c85d85b-2366-4447-e41b-08da3fd28184 X-MS-Exchange-CrossTenant-AuthSource: VE1PR04MB6560.eurprd04.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 27 May 2022 11:17:37.6248 (UTC) X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted X-MS-Exchange-CrossTenant-Id: f7a17af6-1c5c-4a36-aa8b-f5be247aa4ba X-MS-Exchange-CrossTenant-MailboxType: HOSTED X-MS-Exchange-CrossTenant-UserPrincipalName: Xen5K9JNHfQeTxp7SGpY3wvV6jDteF7P5UormBq3gGRLo/EfYSc2d+KrrXbeCIxYDk2DbC9Yoq4qx+BgarlPbA== X-MS-Exchange-Transport-CrossTenantHeadersStamped: AM6PR04MB5831 Page tables are used for two purposes after allocation: They either start out all empty, or they are filled to replace a superpage. Subsequently, to replace all empty or fully contiguous page tables, contiguous sub-regions will be recorded within individual page tables. Install the initial set of markers immediately after allocation. Make sure to retain these markers when further populating a page table in preparation for it to replace a superpage. The markers are simply 4-bit fields holding the order value of contiguous entries. To demonstrate this, if a page table had just 16 entries, this would be the initial (fully contiguous) set of markers: index 0 1 2 3 4 5 6 7 8 9 A B C D E F marker 4 0 1 0 2 0 1 0 3 0 1 0 2 0 1 0 "Contiguous" here means not only present entries with successively increasing MFNs, each one suitably aligned for its slot, and identical attributes, but also a respective number of all non-present (zero except for the markers) entries. Signed-off-by: Jan Beulich Reviewed-by: Kevin Tian Reviewed-by: Roger Pau Monné --- An alternative to the ASSERT()s added to set_iommu_ptes_present() would be to make the function less general-purpose; it's used in a single place only after all (i.e. it might as well be folded into its only caller). While in VT-d's comment ahead of struct dma_pte I'm adjusting the description of the high bits, I'd like to note that the description of some of the lower bits isn't correct either. Yet I don't think adjusting that belongs here. --- v5: Assert next_mfn is suitably aligned in set_iommu_ptes_present(). Use CONTIG_LEVEL_SHIFT in favor of PAGE_SHIFT-3. v4: Add another comment referring to pt-contig-markers.h. Re-base. v3: Add comments. Re-base. v2: New. --- a/xen/arch/x86/include/asm/iommu.h +++ b/xen/arch/x86/include/asm/iommu.h @@ -146,7 +146,8 @@ void iommu_free_domid(domid_t domid, uns int __must_check iommu_free_pgtables(struct domain *d); struct domain_iommu; -struct page_info *__must_check iommu_alloc_pgtable(struct domain_iommu *hd); +struct page_info *__must_check iommu_alloc_pgtable(struct domain_iommu *hd, + uint64_t contig_mask); void iommu_queue_free_pgtable(struct domain_iommu *hd, struct page_info *pg); #endif /* !__ARCH_X86_IOMMU_H__ */ --- a/xen/drivers/passthrough/amd/iommu-defs.h +++ b/xen/drivers/passthrough/amd/iommu-defs.h @@ -446,11 +446,13 @@ union amd_iommu_x2apic_control { #define IOMMU_PAGE_TABLE_U32_PER_ENTRY (IOMMU_PAGE_TABLE_ENTRY_SIZE / 4) #define IOMMU_PAGE_TABLE_ALIGNMENT 4096 +#define IOMMU_PTE_CONTIG_MASK 0x1e /* The ign0 field below. */ + union amd_iommu_pte { uint64_t raw; struct { bool pr:1; - unsigned int ign0:4; + unsigned int ign0:4; /* Covered by IOMMU_PTE_CONTIG_MASK. */ bool a:1; bool d:1; unsigned int ign1:2; --- a/xen/drivers/passthrough/amd/iommu_map.c +++ b/xen/drivers/passthrough/amd/iommu_map.c @@ -21,6 +21,8 @@ #include "iommu.h" +#include + /* Given pfn and page table level, return pde index */ static unsigned int pfn_to_pde_idx(unsigned long pfn, unsigned int level) { @@ -113,9 +115,23 @@ static void set_iommu_ptes_present(unsig return; } + ASSERT(!(next_mfn & (page_sz - 1))); + while ( nr_ptes-- ) { - set_iommu_pde_present(pde, next_mfn, 0, iw, ir); + ASSERT(!pde->next_level); + ASSERT(!pde->u); + + if ( pde > table ) + ASSERT(pde->ign0 == find_first_set_bit(pde - table)); + else + ASSERT(pde->ign0 == CONTIG_LEVEL_SHIFT); + + pde->iw = iw; + pde->ir = ir; + pde->fc = true; /* See set_iommu_pde_present(). */ + pde->mfn = next_mfn; + pde->pr = true; ++pde; next_mfn += page_sz; @@ -295,7 +311,7 @@ static int iommu_pde_from_dfn(struct dom mfn = next_table_mfn; /* allocate lower level page table */ - table = iommu_alloc_pgtable(hd); + table = iommu_alloc_pgtable(hd, IOMMU_PTE_CONTIG_MASK); if ( table == NULL ) { AMD_IOMMU_ERROR("cannot allocate I/O page table\n"); @@ -325,7 +341,7 @@ static int iommu_pde_from_dfn(struct dom if ( next_table_mfn == 0 ) { - table = iommu_alloc_pgtable(hd); + table = iommu_alloc_pgtable(hd, IOMMU_PTE_CONTIG_MASK); if ( table == NULL ) { AMD_IOMMU_ERROR("cannot allocate I/O page table\n"); @@ -726,7 +742,7 @@ static int fill_qpt(union amd_iommu_pte * page table pages, and the resulting allocations are always * zeroed. */ - pgs[level] = iommu_alloc_pgtable(hd); + pgs[level] = iommu_alloc_pgtable(hd, 0); if ( !pgs[level] ) { rc = -ENOMEM; @@ -784,7 +800,7 @@ int cf_check amd_iommu_quarantine_init(s return 0; } - pdev->arch.amd.root_table = iommu_alloc_pgtable(hd); + pdev->arch.amd.root_table = iommu_alloc_pgtable(hd, 0); if ( !pdev->arch.amd.root_table ) return -ENOMEM; --- a/xen/drivers/passthrough/amd/pci_amd_iommu.c +++ b/xen/drivers/passthrough/amd/pci_amd_iommu.c @@ -342,7 +342,7 @@ int amd_iommu_alloc_root(struct domain * if ( unlikely(!hd->arch.amd.root_table) && d != dom_io ) { - hd->arch.amd.root_table = iommu_alloc_pgtable(hd); + hd->arch.amd.root_table = iommu_alloc_pgtable(hd, 0); if ( !hd->arch.amd.root_table ) return -ENOMEM; } --- a/xen/drivers/passthrough/vtd/iommu.c +++ b/xen/drivers/passthrough/vtd/iommu.c @@ -334,7 +334,7 @@ static uint64_t addr_to_dma_page_maddr(s goto out; pte_maddr = level; - if ( !(pg = iommu_alloc_pgtable(hd)) ) + if ( !(pg = iommu_alloc_pgtable(hd, 0)) ) goto out; hd->arch.vtd.pgd_maddr = page_to_maddr(pg); @@ -376,7 +376,7 @@ static uint64_t addr_to_dma_page_maddr(s } pte_maddr = level - 1; - pg = iommu_alloc_pgtable(hd); + pg = iommu_alloc_pgtable(hd, DMA_PTE_CONTIG_MASK); if ( !pg ) break; @@ -388,12 +388,13 @@ static uint64_t addr_to_dma_page_maddr(s struct dma_pte *split = map_vtd_domain_page(pte_maddr); unsigned long inc = 1UL << level_to_offset_bits(level - 1); - split[0].val = pte->val; + split[0].val |= pte->val & ~DMA_PTE_CONTIG_MASK; if ( inc == PAGE_SIZE ) split[0].val &= ~DMA_PTE_SP; for ( offset = 1; offset < PTE_NUM; ++offset ) - split[offset].val = split[offset - 1].val + inc; + split[offset].val |= + (split[offset - 1].val & ~DMA_PTE_CONTIG_MASK) + inc; iommu_sync_cache(split, PAGE_SIZE); unmap_vtd_domain_page(split); @@ -2176,7 +2177,7 @@ static int __must_check cf_check intel_i if ( iommu_snoop ) dma_set_pte_snp(new); - if ( old.val == new.val ) + if ( !((old.val ^ new.val) & ~DMA_PTE_CONTIG_MASK) ) { spin_unlock(&hd->arch.mapping_lock); unmap_vtd_domain_page(page); @@ -3064,7 +3065,7 @@ static int fill_qpt(struct dma_pte *this * page table pages, and the resulting allocations are always * zeroed. */ - pgs[level] = iommu_alloc_pgtable(hd); + pgs[level] = iommu_alloc_pgtable(hd, 0); if ( !pgs[level] ) { rc = -ENOMEM; @@ -3121,7 +3122,7 @@ static int cf_check intel_iommu_quaranti if ( !drhd ) return -ENODEV; - pg = iommu_alloc_pgtable(hd); + pg = iommu_alloc_pgtable(hd, 0); if ( !pg ) return -ENOMEM; --- a/xen/drivers/passthrough/vtd/iommu.h +++ b/xen/drivers/passthrough/vtd/iommu.h @@ -253,7 +253,10 @@ struct context_entry { * 2-6: reserved * 7: super page * 8-11: available - * 12-63: Host physcial address + * 12-51: Host physcial address + * 52-61: available (52-55 used for DMA_PTE_CONTIG_MASK) + * 62: reserved + * 63: available */ struct dma_pte { u64 val; @@ -263,6 +266,7 @@ struct dma_pte { #define DMA_PTE_PROT (DMA_PTE_READ | DMA_PTE_WRITE) #define DMA_PTE_SP (1 << 7) #define DMA_PTE_SNP (1 << 11) +#define DMA_PTE_CONTIG_MASK (0xfull << PADDR_BITS) #define dma_clear_pte(p) do {(p).val = 0;} while(0) #define dma_set_pte_readable(p) do {(p).val |= DMA_PTE_READ;} while(0) #define dma_set_pte_writable(p) do {(p).val |= DMA_PTE_WRITE;} while(0) @@ -276,7 +280,7 @@ struct dma_pte { #define dma_pte_write(p) (dma_pte_prot(p) & DMA_PTE_WRITE) #define dma_pte_addr(p) ((p).val & PADDR_MASK & PAGE_MASK_4K) #define dma_set_pte_addr(p, addr) do {\ - (p).val |= ((addr) & PAGE_MASK_4K); } while (0) + (p).val |= ((addr) & PADDR_MASK & PAGE_MASK_4K); } while (0) #define dma_pte_present(p) (((p).val & DMA_PTE_PROT) != 0) #define dma_pte_superpage(p) (((p).val & DMA_PTE_SP) != 0) --- a/xen/drivers/passthrough/x86/iommu.c +++ b/xen/drivers/passthrough/x86/iommu.c @@ -26,6 +26,7 @@ #include #include #include +#include #include const struct iommu_init_ops *__initdata iommu_init_ops; @@ -538,11 +539,12 @@ int iommu_free_pgtables(struct domain *d return 0; } -struct page_info *iommu_alloc_pgtable(struct domain_iommu *hd) +struct page_info *iommu_alloc_pgtable(struct domain_iommu *hd, + uint64_t contig_mask) { unsigned int memflags = 0; struct page_info *pg; - void *p; + uint64_t *p; #ifdef CONFIG_NUMA if ( hd->node != NUMA_NO_NODE ) @@ -554,7 +556,29 @@ struct page_info *iommu_alloc_pgtable(st return NULL; p = __map_domain_page(pg); - clear_page(p); + + if ( contig_mask ) + { + /* See pt-contig-markers.h for a description of the marker scheme. */ + unsigned int i, shift = find_first_set_bit(contig_mask); + + ASSERT((CONTIG_LEVEL_SHIFT & (contig_mask >> shift)) == CONTIG_LEVEL_SHIFT); + + p[0] = (CONTIG_LEVEL_SHIFT + 0ull) << shift; + p[1] = 0; + p[2] = 1ull << shift; + p[3] = 0; + + for ( i = 4; i < PAGE_SIZE / 8; i += 4 ) + { + p[i + 0] = (find_first_set_bit(i) + 0ull) << shift; + p[i + 1] = 0; + p[i + 2] = 1ull << shift; + p[i + 3] = 0; + } + } + else + clear_page(p); iommu_sync_cache(p, PAGE_SIZE);