iommu/amd: Add Generic IO Page Table Framework Support for v2 Page Table
Subsystems (3)
cover iommu/amd: Add Generic IO Page Table Framework Support for v2 Page Table
This series introduces a new usage model for the v2 page table, where it can be used to implement support for DMA-API by adopting the generic IO page table framework.
One of the target usecases is to support nested IO page tables where the guest uses the guest IO page table (v2) for translating GVA to GPA, and the hypervisor uses the host I/O page table (v1) for translating GPA to SPA. This is a pre-requisite for supporting the new HW-assisted vIOMMU presented at the KVM Forum 2020.
https://static.sched.com/hosted_files/kvmforum2020/26/vIOMMU%20KVM%20Forum%202020.pdf
The following components are introduced in this series:
- Part 1 (patch 1-4 and 6)
Refactor the current IOMMU page table code to adopt the generic IO page table framework, and add AMD IOMMU Guest (v2) page table management code.
- Part 2 (patch 5)
Add support for the AMD IOMMU Guest IO Protection feature (GIOV) where requests from the I/O device without a PASID are treated as if they have PASID of 0.
- Part 3 (patch 7)
Introduce new "amd_iommu_pgtable" command-line to allow users to select the mode of operation (v1 or v2).
See AMD I/O Virtualization Technology Specification for more detail.
http://www.amd.com/system/files/TechDocs/48882_IOMMU_3.05_PUB.pdf
Note:
This patchset is based on top of "iommu/amd: Add multiple PCI segments support" patchset [1].
[1] https://lore.kernel.org/linux-iommu/[email protected]/T/#t
Thanks,
Vasant
Changes from RFC -> v1:
- Addressed review comments from Joerg
- Reimplemented v2 page table
RFC patchset : https://lore.kernel.org/linux-iommu/[email protected]/T/#t
Suravee Suthikulpanit (5):
iommu/amd: Refactor amd_iommu_domain_enable_v2
iommu/amd: Update sanity check when enable PRI/ATS iommu/amd: Add support for Guest IO protection
iommu/amd: Add support for using AMD IOMMU v2 page table for DMA-API iommu/amd: Introduce amd_iommu_pgtable command-line option
Vasant Hegde (2):
iommu/amd: Fix sparse warning
iommu/amd: Initial support for AMD IOMMU v2 page table
.../admin-guide/kernel-parameters.txt | 6 + drivers/iommu/amd/Makefile | 2 +- drivers/iommu/amd/amd_iommu_types.h | 8 +- drivers/iommu/amd/init.c | 27 +- drivers/iommu/amd/io_pgtable_v2.c | 407 ++++++++++++++++++ drivers/iommu/amd/iommu.c | 90 ++-- drivers/iommu/io-pgtable.c | 1 + include/linux/io-pgtable.h | 2 + 8 files changed, 516 insertions(+), 27 deletions(-) create mode 100644 drivers/iommu/amd/io_pgtable_v2.c
Signature · 6 lines
2.27.0 _______________________________________________ iommu mailing list [email protected] https://lists.linuxfoundation.org/mailman/listinfo/iommu
PATCH 1/7 iommu/amd: Refactor amd_iommu_domain_enable_v2
From: Suravee Suthikulpanit <[email protected]>
The current function to enable IOMMU v2 also lock the domain. In order to reuse the same code in different code path, in which the domain has already been locked, refactor the function to separate the locking from the enabling logic.
--- drivers/iommu/amd/iommu.c | 46 +++++++++++++++++++++++---------------- 1 file changed, 27 insertions(+), 19 deletions(-)
drivers/iommu/amd/iommu.c +27 -19
@@ -85,6 +85,7 @@ struct iommu_cmd {struct kmem_cache *amd_iommu_irq_cache;static void detach_device (struct device *dev); +static int domain_enable_v2 (struct protection_domain *domain,int pasids,bool has_ppr);/**************************************************************************** *
@@ -2427,11 +2428,10 @@ void amd_iommu_domain_direct_map(struct iommu_domain *dom) }EXPORT_SYMBOL (amd_iommu_domain_direct_map); -int amd_iommu_domain_enable_v2 (struct iommu_domain *dom,int pasids) +/* Note: This function expects iommu_domain->lock to be held prior calling the function. */ +static int domain_enable_v2 (struct protection_domain *domain,int pasids,bool has_ppr) { -struct protection_domain *domain =to_pdomain (dom); -unsigned long flags; -int levels, ret; +int levels;/* Number of GCR3 table levels required */ for (levels =0 ; (pasids -1 ) & ~0x1ff ; pasids >>=9 )
@@ -2440,7 +2440,25 @@ int amd_iommu_domain_enable_v2(struct iommu_domain *dom, int pasids)if (levels > amd_iommu_max_glx_val)return -EINVAL ; -spin_lock_irqsave (&domain->lock, flags); + domain->gcr3_tbl = (void *)get_zeroed_page (GFP_ATOMIC ); +if (domain->gcr3_tbl ==NULL ) +return -ENOMEM ; + + domain->glx = levels; + domain->flags |=PD_IOMMUV2_MASK ; + +amd_iommu_domain_update (domain); + +return 0 ; +} + +int amd_iommu_domain_enable_v2 (struct iommu_domain *dom,int pasids) +{ +struct protection_domain *pdom =to_pdomain (dom); +unsigned long flags; +int ret; + +spin_lock_irqsave (&pdom->lock, flags);/* * Save us all sanity checks whether devices already in the
@@ -2448,24 +2466,14 @@ int amd_iommu_domain_enable_v2(struct iommu_domain *dom, int pasids)* devices attached when it is switched into IOMMUv2 mode. */ ret = -EBUSY ; -if (domain->dev_cnt >0 || domain->flags &PD_IOMMUV2_MASK ) - goto out; - - ret = -ENOMEM ; - domain->gcr3_tbl = (void *)get_zeroed_page (GFP_ATOMIC ); -if (domain->gcr3_tbl ==NULL ) +if (pdom->dev_cnt >0 || pdom->flags &PD_IOMMUV2_MASK ) goto out; - domain->glx = levels; - domain->flags |=PD_IOMMUV2_MASK ; - -amd_iommu_domain_update (domain); - - ret =0 ; +if (!pdom->gcr3_tbl) + ret =domain_enable_v2 (pdom, pasids, true); out: -spin_unlock_irqrestore (&domain->lock, flags); - +spin_unlock_irqrestore (&pdom->lock, flags);return ret; }EXPORT_SYMBOL (amd_iommu_domain_enable_v2);
Signature · 6 lines
2.27.0 _______________________________________________ iommu mailing list [email protected] https://lists.linuxfoundation.org/mailman/listinfo/iommu
PATCH 2/7 iommu/amd: Update sanity check when enable PRI/ATS
In reply to Vasant Hegde via iommuFrom: Suravee Suthikulpanit <[email protected]>
Currently, PPR/ATS can be enabled only if the domain is type identity mapping. However, when we allow the IOMMU v2 page table to be used for DMA-API, the sanity check needs to be updated to only apply for the case when using AMD_IOMMU_V1 page table mode.
--- drivers/iommu/amd/iommu.c | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-)
drivers/iommu/amd/iommu.c +11 -3
@@ -1682,7 +1682,7 @@ static void pdev_iommuv2_disable(struct pci_dev *pdev)pci_disable_pasid (pdev); } -static int pdev_iommuv2_enable (struct pci_dev *pdev) +static int pdev_pri_ats_enable (struct pci_dev *pdev) {int ret;
@@ -1745,11 +1745,19 @@ static int attach_device(struct device *dev,struct iommu_domain *def_domain =iommu_get_dma_domain (dev); ret = -EINVAL ; -if (def_domain->type !=IOMMU_DOMAIN_IDENTITY ) + +/* +* In case of using AMD_IOMMU_V1 page table mode and the device +* is enabling for PPR/ATS support (using v2 table), +* we need to make sure that the domain type is identity map. +*/ +if ((amd_iommu_pgtable ==AMD_IOMMU_V1 ) && + def_domain->type !=IOMMU_DOMAIN_IDENTITY ) { goto out; + }if (dev_data->iommu_v2) { -if (pdev_iommuv2_enable (pdev) !=0 ) +if (pdev_pri_ats_enable (pdev) !=0 ) goto out; dev_data->ats.enabled = true;
Signature · 6 lines
2.27.0 _______________________________________________ iommu mailing list [email protected] https://lists.linuxfoundation.org/mailman/listinfo/iommu
PATCH 3/7 iommu/amd: Fix sparse warning
In reply to Vasant Hegde via iommuFix below sparse warning:
CHECK drivers/iommu/amd/iommu.c
drivers/iommu/amd/iommu.c:73:24: warning: symbol 'amd_iommu_ops' was not declared. Should it be static?
Also we are going to introduce v2 page table which has different pgsize_bitmaps. Hence remove 'const' qualifier.
--- drivers/iommu/amd/init.c | 2 +- drivers/iommu/amd/iommu.c | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-)
drivers/iommu/amd/init.c +1 -1
@@ -96,7 +96,7 @@* out of it. */ -extern const struct iommu_ops amd_iommu_ops; +extern struct iommu_ops amd_iommu_ops;/* * structure describing one IOMMU in the ACPI table. Typically followed by one
drivers/iommu/amd/iommu.c +2 -2
@@ -70,7 +70,7 @@ LIST_HEAD(acpihid_map);* Domain for untranslated devices - only allocated * if iommu=pt passed on kernel cmd line. */ -const struct iommu_ops amd_iommu_ops; +struct iommu_ops amd_iommu_ops;static ATOMIC_NOTIFIER_HEAD (ppr_notifier);int amd_iommu_max_glx_val =-1 ;
@@ -2374,7 +2374,7 @@ static int amd_iommu_def_domain_type(struct device *dev)return 0 ; } -const struct iommu_ops amd_iommu_ops = { +struct iommu_ops amd_iommu_ops = { .capable = amd_iommu_capable, .domain_alloc = amd_iommu_domain_alloc, .probe_device = amd_iommu_probe_device,
Signature · 6 lines
2.27.0 _______________________________________________ iommu mailing list [email protected] https://lists.linuxfoundation.org/mailman/listinfo/iommu
Joerg Roedel
maintainerRe: [PATCH v1 3/7] iommu/amd: Fix sparse warning
Quoted 6 lines · Vasant Hegde
Fix below sparse warning:
CHECKdrivers/iommu/amd/iommu.c
drivers/iommu/amd/iommu.c:73:24: warning: symbol 'amd_iommu_ops' was not declared. Should it be static?Also we are going to introduce v2 page table which has different
pgsize_bitmaps. Hence remove 'const' qualifier.
I am not a fan of removing the consts. Please use separate ops structures for v2 page-tables and make then const as well. This probably also has some optimization potential in the future when we can make the ops call-back functions page-table specific.
Regards,
Joerg
_______________________________________________
iommu mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/iommu
Robin Murphy
reviewerRe: [PATCH v1 3/7] iommu/amd: Fix sparse warning
Quoted 5 lines · Joerg Roedel
Quoted 4 lines · Vasant Hegde
Fix below sparse warning:
CHECKdrivers/iommu/amd/iommu.c
drivers/iommu/amd/iommu.c:73:24: warning: symbol 'amd_iommu_ops' was not declared. Should it be static?
Quoted 7 lines
Quoted 2 lines
Also we are going to introduce v2 page table which has different
pgsize_bitmaps. Hence remove 'const' qualifier.I am not a fan of removing the consts. Please use separate ops structures for v2 page-tables and make then const as well. This probably also has some optimization potential in the future when we can make the ops call-back functions page-table specific.
TBH it's probably time to retire iommu_ops->pgsize_bitmap anyway. At the very least it would be logical to move it to iommu_domain_ops now, but maybe we could skip ahead and just rely on drivers initialising domain->pgsize_bitmap directly in their .domain_alloc?
(and FWIW I'm leaning towards the same for the domain->ops as well; the more I look at ops->default_domain_ops, the more it starts looking like a stupid mess... my stupid mess... sorry about that)
Robin.
_______________________________________________
iommu mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/iommu
Joerg Roedel
maintainerRe: [PATCH v1 3/7] iommu/amd: Fix sparse warning
Quoted 8 lines · Robin Murphy
TBH it's probably time to retire
iommu_ops->pgsize_bitmapanyway. At the very least it would be logical to move it toiommu_domain_opsnow, but maybe we could skip ahead and just rely on drivers initialisingdomain->pgsize_bitmapdirectly in their .domain_alloc?(and FWIW I'm leaning towards the same for the
domain->opsas well; the more I look atops->default_domain_ops, the more it starts looking like a stupid mess... my stupid mess... sorry about that)
Good idea, that would be even better.
Thanks,
Joerg
_______________________________________________
iommu mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/iommu
Re: [PATCH v1 3/7] iommu/amd: Fix sparse warning
In reply to Robin MurphyQuoted 9 lines · Robin Murphy
Quoted 8 lines · Joerg Roedel
Quoted 6 lines · Vasant Hegde
Fix below sparse warning:
CHECKdrivers/iommu/amd/iommu.c
drivers/iommu/amd/iommu.c:73:24: warning: symbol 'amd_iommu_ops' was not declared. Should it be static?Also we are going to introduce v2 page table which has different
pgsize_bitmaps. Hence remove 'const' qualifier.
Quoted 7 lines
Quoted 4 lines
I am not a fan of removing the consts. Please use separate ops structures for v2 page-tables and make then const as well. This probably also has some optimization potential in the future when we can make the ops call-back functions page-table specific.
TBH it's probably time to retire
iommu_ops->pgsize_bitmapanyway. At the very least it would be logical to move it toiommu_domain_opsnow, but maybe we could skip ahead and just rely on drivers initialisingdomain->pgsize_bitmapdirectly in their .domain_alloc?
Robin,
Something like below? If yes, I will cleanup and get proper fix.
-Vasant
drivers/iommu/amd/iommu.c +1 -1
@@ -1916,6 +1916,7 @@ static int protection_domain_init_v1(struct protection_domain *domain, int mode)return -ENOMEM ; } + domain->domain.pgsize_bitmap =AMD_IOMMU_PGSIZES ;amd_iommu_domain_set_pgtable (domain, pt_root, mode);return 0 ;
@@ -2282,7 +2283,6 @@ const struct iommu_ops amd_iommu_ops = { .get_resv_regions = amd_iommu_get_resv_regions, .put_resv_regions = generic_iommu_put_resv_regions, .is_attach_deferred = amd_iommu_is_attach_deferred, - .pgsize_bitmap = AMD_IOMMU_PGSIZES, .def_domain_type = amd_iommu_def_domain_type, .default_domain_ops = &(const struct iommu_domain_ops ) { .attach_dev = amd_iommu_attach_device,
drivers/iommu/iommu.c +0 -2
@@ -1915,8 +1915,6 @@ static struct iommu_domain *__iommu_domain_alloc(struct bus_type *bus,return NULL ; domain->type = type; -/* Assume all sizes by default; the driver may override this later */ - domain->pgsize_bitmap = bus->iommu_ops->pgsize_bitmap;if (!domain->ops) domain->ops = bus->iommu_ops->default_domain_ops;
include/linux/iommu.h +0 -1
@@ -255,7 +255,6 @@ struct iommu_ops {int (*def_domain_type)(struct device *dev);const struct iommu_domain_ops *default_domain_ops; -unsigned long pgsize_bitmap;struct module *owner; };
Quoted 3 lines
(and FWIW I'm leaning towards the same for the
domain->opsas well; the more I look atops->default_domain_ops, the more it starts looking like a stupid mess... my stupid mess... sorry about that)Robin.
_______________________________________________
iommu mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/iommu
Vasant Hegde
maintainerRe: [PATCH v1 3/7] iommu/amd: Fix sparse warning
Hi Robin,
Quoted 17 lines · Vasant Hegde
Quoted 14 lines · Robin Murphy
Quoted 12 lines · Joerg Roedel
Quoted 6 lines · Vasant Hegde
Fix below sparse warning:
CHECKdrivers/iommu/amd/iommu.c
drivers/iommu/amd/iommu.c:73:24: warning: symbol 'amd_iommu_ops' was not declared. Should it be static?Also we are going to introduce v2 page table which has different
pgsize_bitmaps. Hence remove 'const' qualifier.I am not a fan of removing the consts. Please use separate ops structures for v2 page-tables and make then const as well. This probably also has some optimization potential in the future when we can make the ops call-back functions page-table specific.
Quoted 6 lines
Quoted 2 lines
TBH it's probably time to retire
iommu_ops->pgsize_bitmapanyway. At the very least it would be logical to move it toiommu_domain_opsnow, but maybe we could skip ahead and just rely on drivers initialisingdomain->pgsize_bitmapdirectly in their .domain_alloc?Robin,
Something like below? If yes, I will cleanup and get proper fix.
Ping.
Will you be cleaning up pgsize_bitmap -OR- do you want me to cleanup (I can do that as separate series)?
-Vasant
Quoted 55 lines
(quoted diff) +0
-1-Vasantdrivers/iommu/amd/iommu.c +1
-1@@ -1916,6 +1916,7 @@ static int protection_domain_init_v1(struct protection_domain *domain, int mode)return -ENOMEM ; } + domain->domain.pgsize_bitmap =AMD_IOMMU_PGSIZES ;amd_iommu_domain_set_pgtable (domain, pt_root, mode);return 0 ;@@ -2282,7 +2283,6 @@ const struct iommu_ops amd_iommu_ops = { .get_resv_regions = amd_iommu_get_resv_regions, .put_resv_regions = generic_iommu_put_resv_regions, .is_attach_deferred = amd_iommu_is_attach_deferred, - .pgsize_bitmap = AMD_IOMMU_PGSIZES, .def_domain_type = amd_iommu_def_domain_type, .default_domain_ops = &(const struct iommu_domain_ops ) { .attach_dev = amd_iommu_attach_device,drivers/ iommu/iommu.c renamed from drivers/iommu/iommu.c +0
-2@@ -1915,8 +1915,6 @@ static struct iommu_domain *__iommu_domain_alloc(struct bus_type *bus,return NULL ; domain->type = type; -/* Assume all sizes by default; the driver may override this later */ - domain->pgsize_bitmap = bus->iommu_ops->pgsize_bitmap;if (!domain->ops) domain->ops = bus->iommu_ops->default_domain_ops;include/linux/iommu.h +0
-1@@ -255,7 +255,6 @@ struct iommu_ops {int (*def_domain_type)(struct device *dev);const struct iommu_domain_ops *default_domain_ops; -unsigned long pgsize_bitmap;struct module *owner; };Quoted 5 lines
(and FWIW I'm leaning towards the same for the
domain->opsas well; the more I look atops->default_domain_ops, the more it starts looking like a stupid mess... my stupid mess... sorry about that)Robin.
Robin Murphy
reviewerRe: [PATCH v1 3/7] iommu/amd: Fix sparse warning
Quoted 21 lines · Vasant Hegde
Hi Robin,
Quoted 17 lines · Vasant Hegde
Quoted 14 lines · Robin Murphy
Quoted 12 lines · Joerg Roedel
Quoted 6 lines · Vasant Hegde
Fix below sparse warning:
CHECKdrivers/iommu/amd/iommu.c
drivers/iommu/amd/iommu.c:73:24: warning: symbol 'amd_iommu_ops' was not declared. Should it be static?Also we are going to introduce v2 page table which has different
pgsize_bitmaps. Hence remove 'const' qualifier.I am not a fan of removing the consts. Please use separate ops structures for v2 page-tables and make then const as well. This probably also has some optimization potential in the future when we can make the ops call-back functions page-table specific.
Quoted 8 lines
Quoted 6 lines
Quoted 2 lines
TBH it's probably time to retire
iommu_ops->pgsize_bitmapanyway. At the very least it would be logical to move it toiommu_domain_opsnow, but maybe we could skip ahead and just rely on drivers initialisingdomain->pgsize_bitmapdirectly in their .domain_alloc?Robin,
Something like below? If yes, I will cleanup and get proper fix.
Ping.
Sorry for the delay :)
Quoted 2 lines
Will you be cleaning up
pgsize_bitmap-OR- do you want me to cleanup (I can do that as separate series)?
I started prototyping it too, and at least for the Arm SMMU drivers it turns out the change is a little bit fiddly, then the idea started steadily turned into moving *all* the data out of iommu_ops. That then ties directly into what I'm already working on, so I'm happy to pick it up. I think it should fit in quite neatly right after the bus_set_iommu() series, so I'll aim to get something ready for next cycle.
Thanks,
Robin.
PATCH 4/7 iommu/amd: Initial support for AMD IOMMU v2 page table
In reply to Vasant Hegde via iommuIntroduce IO page table framework support for AMD IOMMU v2 page table. This patch implements 4 level page table within iommu amd driver and supports 4K/2M/1G page sizes.
--- drivers/iommu/amd/Makefile | 2 +- drivers/iommu/amd/amd_iommu_types.h | 5 +- drivers/iommu/amd/io_pgtable_v2.c | 407 ++++++++++++++++++++++++++++ drivers/iommu/io-pgtable.c | 1 + include/linux/io-pgtable.h | 2 + 5 files changed, 415 insertions(+), 2 deletions(-) create mode 100644 drivers/iommu/amd/io_pgtable_v2.c
drivers/iommu/amd/Makefile +1 -1
@@ -1,4 +1,4 @@ # SPDX-License-Identifier: GPL-2.0-only -obj-$(CONFIG_AMD_IOMMU) += iommu.o init.o quirks.o io_pgtable.o +obj-$(CONFIG_AMD_IOMMU) += iommu.o init.o quirks.o io_pgtable.o io_pgtable_v2.o obj-$(CONFIG_AMD_IOMMU_DEBUGFS) += debugfs.o obj-$(CONFIG_AMD_IOMMU_V2) += iommu_v2.o
drivers/iommu/amd/amd_iommu_types.h +4 -1
@@ -268,6 +268,8 @@* 512GB Pages are not supported due to a hardware bug */ #define AMD_IOMMU_PGSIZES ((~0xFFFUL) & ~(2ULL << 38)) +/* 4K, 2MB, 1G page sizes are supported */ +#define AMD_IOMMU_PGSIZES_V2 (PAGE_SIZE | (1ULL << 21) | (1ULL << 30))/* Bit value definition for dte irq remapping fields*/ #define DTE_IRQ_PHYS_ADDR_MASK (((1ULL << 45)-1) << 6)
@@ -514,7 +516,8 @@ struct amd_io_pgtable {struct io_pgtable iop;int mode;u64 *root; -atomic64_t pt_root;/* pgtable root and pgtable mode */ +atomic64_t pt_root;/* pgtable root and pgtable mode */ +u64 *pgd;/* v2 pgtable pgd pointer */ };/*
drivers/iommu/amd/io_pgtable_v2.c new file +407 -0
407 lines not on this page · raw message
drivers/iommu/io-pgtable.c +1 -0
@@ -27,6 +27,7 @@ io_pgtable_init_table[IO_PGTABLE_NUM_FMTS] = {#endif #ifdef CONFIG_AMD_IOMMU [AMD_IOMMU_V1] = &io_pgtable_amd_iommu_v1_init_fns, + [AMD_IOMMU_V2] = &io_pgtable_amd_iommu_v2_init_fns,#endif };
include/linux/io-pgtable.h +2 -0
@@ -16,6 +16,7 @@ enum io_pgtable_fmt {ARM_V7S ,ARM_MALI_LPAE ,AMD_IOMMU_V1 , +AMD_IOMMU_V2 ,APPLE_DART , IO_PGTABLE_NUM_FMTS, };
@@ -255,6 +256,7 @@ extern struct io_pgtable_init_fns io_pgtable_arm_64_lpae_s2_init_fns;extern struct io_pgtable_init_fns io_pgtable_arm_v7s_init_fns;extern struct io_pgtable_init_fns io_pgtable_arm_mali_lpae_init_fns;extern struct io_pgtable_init_fns io_pgtable_amd_iommu_v1_init_fns; +extern struct io_pgtable_init_fns io_pgtable_amd_iommu_v2_init_fns;extern struct io_pgtable_init_fns io_pgtable_apple_dart_init_fns;#endif /* __IO_PGTABLE_H */
Signature · 6 lines
2.27.0 _______________________________________________ iommu mailing list [email protected] https://lists.linuxfoundation.org/mailman/listinfo/iommu
Joerg Roedel
maintainerRe: [PATCH v1 4/7] iommu/amd: Initial support for AMD IOMMU v2 page table
Quoted 35 lines · Vasant Hegde
drivers/iommu/amd/io_pgtable_v2.c new file +35
-0+/* Allocate page table */ +static u64 *v2_alloc_pte (u64 *pgd,unsigned long iova, +unsigned long pg_size,bool *updated) +{ +u64 *pte, *page; +int level, end_level; + +BUG_ON (!is_power_of_2 (pg_size)); + + level =get_pgtable_level () -1 ; + end_level =page_size_to_level (pg_size); + pte = &pgd[PM_LEVEL_INDEX (level, iova)]; + iova =PAGE_SIZE_ALIGN (iova,PAGE_SIZE ); + +while (level >= end_level) { +u64 __pte, __npte; + + __pte = *pte; + +if (IOMMU_PTE_PRESENT (__pte) &&is_large_pte (__pte)) { +/* Unmap large pte */ +cmpxchg64 (pte, *pte,0ULL ); + *updated = true; +continue ; + } + +if (!IOMMU_PTE_PRESENT (__pte)) { + page =alloc_pgtable_page (); +if (!page) +return NULL ; + + __npte =set_pgtable_attr (page); +/* pte could have been changed somewhere. */ +if (cmpxchg64 (pte, __pte, __npte) != __pte) +free_pgtable_page (page);
drivers/iommu/amd/io_pgtable_v2.c line 184
Quoted 10 lines
drivers/iommu/amd/io_pgtable_v2.c new file +10
-0+else if (IOMMU_PTE_PRESENT (__pte)) + *updated = true; + +continue ; + } + + level -=1 ; + pte =get_pgtable_pte (__pte); + pte = &pte[PM_LEVEL_INDEX (level, iova)]; + }
I know that the V1 page-table code also uses loops for the allocation path, but the main reason there is the variable amount of page-table levels. The v2 page-tables have a fixed amount levels, so it is better to unroll this loop here (and other loops iterating over page-table levels). This makes the code more clear.
_______________________________________________
iommu mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/iommu
PATCH 5/7 iommu/amd: Add support for Guest IO protection
In reply to Vasant Hegde via iommuFrom: Suravee Suthikulpanit <[email protected]>
AMD IOMMU introduces support for Guest I/O protection where the request from the I/O device without a PASID are treated as if they have PASID 0.
--- drivers/iommu/amd/amd_iommu_types.h | 3 +++ drivers/iommu/amd/init.c | 8 ++++++++ drivers/iommu/amd/iommu.c | 5 +++++ 3 files changed, 16 insertions(+)
drivers/iommu/amd/amd_iommu_types.h +3 -0
@@ -93,6 +93,7 @@#define FEATURE_HE (1ULL<<8)#define FEATURE_PC (1ULL<<9)#define FEATURE_GAM_VAPIC (1ULL<<21) +#define FEATURE_GIOSUP (1ULL<<48)#define FEATURE_EPHSUP (1ULL<<50)#define FEATURE_SNP (1ULL<<63)
@@ -370,6 +371,7 @@#define DTE_FLAG_IW (1ULL << 62)#define DTE_FLAG_IOTLB (1ULL << 32) +#define DTE_FLAG_GIOV (1ULL << 54)#define DTE_FLAG_GV (1ULL << 55)#define DTE_FLAG_MASK (0x3ffULL << 32)#define DTE_GLX_SHIFT (56)
@@ -427,6 +429,7 @@#define PD_PASSTHROUGH_MASK (1UL << 2)/* domain has no page translation */ #define PD_IOMMUV2_MASK (1UL << 3)/* domain has gcr3 table */ +#define PD_GIOV_MASK (1UL << 4)/* domain enable GIOV support */ extern bool amd_iommu_dump; #define DUMP_printk(format, arg...) \
drivers/iommu/amd/init.c +8 -0
@@ -2024,6 +2024,12 @@ static int __init iommu_init_pci(struct amd_iommu *iommu)init_iommu_perf_ctr (iommu); +if (amd_iommu_pgtable ==AMD_IOMMU_V2 && + !iommu_feature (iommu,FEATURE_GIOSUP )) { +pr_warn ("Cannot enable v2 page table for DMA-API. Fallback to v1.\n" ); + amd_iommu_pgtable =AMD_IOMMU_V1 ; + } +if (is_rd890_iommu (iommu->dev)) {int i, j;
@@ -2098,6 +2104,8 @@ static void print_iommu_info(void)if (amd_iommu_xt_mode ==IRQ_REMAP_X2APIC_MODE )pr_info ("X2APIC enabled\n" ); } +if (amd_iommu_pgtable ==AMD_IOMMU_V2 ) +pr_info ("V2 page table enabled\n" ); }static int __init amd_iommu_init_pci (void )
drivers/iommu/amd/iommu.c +5 -0
@@ -1553,6 +1553,11 @@ static void set_dte_entry(struct amd_iommu *iommu, u16 devid, pte_root |= (domain->iop.mode &DEV_ENTRY_MODE_MASK ) <<DEV_ENTRY_MODE_SHIFT ; + +if ((domain->flags &PD_IOMMUV2_MASK ) && + (domain->flags &PD_GIOV_MASK )) + pte_root |=DTE_FLAG_GIOV ; + pte_root |=DTE_FLAG_IR |DTE_FLAG_IW |DTE_FLAG_V |DTE_FLAG_TV ; flags = dev_table[devid].data[1 ];
Signature · 6 lines
2.27.0 _______________________________________________ iommu mailing list [email protected] https://lists.linuxfoundation.org/mailman/listinfo/iommu
PATCH 6/7 iommu/amd: Add support for using AMD IOMMU v2 page table for DMA-API
In reply to Vasant Hegde via iommuFrom: Suravee Suthikulpanit <[email protected]>
Introduce init function for setting up DMA domain for DMA-API with the IOMMU v2 page table.
--- drivers/iommu/amd/iommu.c | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+)
drivers/iommu/amd/iommu.c +21 -0
@@ -2029,6 +2029,24 @@ static int protection_domain_init_v1(struct protection_domain *domain, int mode)return 0 ; } +static int protection_domain_init_v2 (struct protection_domain *domain) +{ +spin_lock_init (&domain->lock); + domain->id =domain_id_alloc (); +if (!domain->id) +return -ENOMEM ; +INIT_LIST_HEAD (&domain->dev_list); + + domain->flags |=PD_GIOV_MASK ; + +if (domain_enable_v2 (domain,1 , false)) { +domain_id_free (domain->id); +return -ENOMEM ; + } + +return 0 ; +} +static struct protection_domain *protection_domain_alloc (unsigned int type) {struct io_pgtable_ops *pgtbl_ops;
@@ -2056,6 +2074,9 @@ static struct protection_domain *protection_domain_alloc(unsigned int type)case AMD_IOMMU_V1 : ret =protection_domain_init_v1 (domain, mode);break ; +case AMD_IOMMU_V2 : + ret =protection_domain_init_v2 (domain); +break ;default : ret = -EINVAL ; }
Signature · 6 lines
2.27.0 _______________________________________________ iommu mailing list [email protected] https://lists.linuxfoundation.org/mailman/listinfo/iommu
PATCH 7/7 iommu/amd: Introduce amd_iommu_pgtable command-line option
In reply to Vasant Hegde via iommuFrom: Suravee Suthikulpanit <[email protected]>
To allow specification whether to use v1 or v2 IOMMU pagetable for DMA remapping when calling kernel DMA-API.
--- Documentation/admin-guide/kernel-parameters.txt | 6 ++++++ drivers/iommu/amd/init.c | 17 +++++++++++++++++ 2 files changed, 23 insertions(+)
Documentation/admin-guide/kernel-parameters.txt +6 -0
@@ -337,6 +337,12 @@ This mode requires kvm-amd.avic=1. (Default when IOMMU HW support is present.) + amd_iommu_pgtable= [HW,X86-64] + Specifies one of the following AMD IOMMU page table to + be used for DMA remapping for DMA-API: + v1 - Use v1 page table (Default) + v2 - Use v2 page table + amijoy.map= [HW,JOY] Amiga joystick support Map of devices attached to JOY0DAT and JOY1DAT Format: <a>,<b>
drivers/iommu/amd/init.c +17 -0
@@ -3264,6 +3264,22 @@ static int __init parse_amd_iommu_dump(char *str)return 1 ; } +static int __init parse_amd_iommu_pgtable (char *str) +{ +for (; *str; ++str) { +if (strncmp (str,"v1" ,2 ) ==0 ) { + amd_iommu_pgtable =AMD_IOMMU_V1 ; + amd_iommu_ops.pgsize_bitmap =AMD_IOMMU_PGSIZES ; +break ; + }else if (strncmp (str,"v2" ,2 ) ==0 ) { + amd_iommu_pgtable =AMD_IOMMU_V2 ; + amd_iommu_ops.pgsize_bitmap =AMD_IOMMU_PGSIZES_V2 ; +break ; + } + } +return 1 ; +} +static int __init parse_amd_iommu_intr (char *str) {for (; *str; ++str) {
@@ -3397,6 +3413,7 @@ static int __init parse_ivrs_acpihid(char *str)__setup ("amd_iommu_dump" , parse_amd_iommu_dump);__setup ("amd_iommu=" , parse_amd_iommu_options); +__setup ("amd_iommu_pgtable=" , parse_amd_iommu_pgtable);__setup ("amd_iommu_intr=" , parse_amd_iommu_intr);__setup ("ivrs_ioapic" , parse_ivrs_ioapic);__setup ("ivrs_hpet" , parse_ivrs_hpet);
Signature · 6 lines
2.27.0 _______________________________________________ iommu mailing list [email protected] https://lists.linuxfoundation.org/mailman/listinfo/iommu
Joerg Roedel
maintainerRe: [PATCH v1 7/7] iommu/amd: Introduce amd_iommu_pgtable command-line option
Documentation/admin-guide/kernel-parameters.txt line 345
Quoted 6 lines · Vasant Hegde
Documentation/admin-guide/kernel-parameters.txt +6
-0+ amd_iommu_pgtable= [HW,X86-64] + Specifies one of the following AMD IOMMU page table to + be used for DMA remapping for DMA-API: + v1 - Use v1 page table (Default) + v2 - Use v2 page table +
Can we handle this somehow in the amd_iommu= option? Something like amd_iommu=pgtbl_v2|pgtbl_v1?
_______________________________________________
iommu mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/iommu
Re: [PATCH v1 7/7] iommu/amd: Introduce amd_iommu_pgtable command-line option
Hi Joerg,
Quoted 11 lines · Joerg Roedel
Documentation/admin-guide/kernel-parameters.txt line 345
Quoted 6 lines · Vasant Hegde
Documentation/admin-guide/kernel-parameters.txt +6
-0+ amd_iommu_pgtable= [HW,X86-64] + Specifies one of the following AMD IOMMU page table to + be used for DMA remapping for DMA-API: + v1 - Use v1 page table (Default) + v2 - Use v2 page table +Can we handle this somehow in the amd_iommu= option? Something like amd_iommu=pgtbl_v2|pgtbl_v1?
I think it will complicate the parsing logic. We do have amd_iommu=off option. How are we going to handle amd_iommu=off,[pgtable_v1/v2] ?
-Vasant
_______________________________________________
iommu mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/iommu
Joerg Roedel
maintainerRe: [PATCH v1 7/7] iommu/amd: Introduce amd_iommu_pgtable command-line option
Quoted 2 lines · Vasant Hegde
I think it will complicate the parsing logic. We do have
amd_iommu=offoption. How are we going to handleamd_iommu=off,[pgtable_v1/v2]?
In that case everything except 'off' will be ignored. The driver might set its internal variables, but this has no effect as the driver never initializes.
Regards,
Joerg
_______________________________________________
iommu mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/iommu
Joerg Roedel
maintainerQuoted 4 lines · Vasant Hegde
- Part 1 (patch 1-4 and 6)
Refactor the current IOMMU page table code to adopt the generic IO page table framework, and add AMD IOMMU Guest (v2) page table management code.
Quoted 8 lines
- Part 2 (patch 5)
Add support for the AMD IOMMU Guest IO Protection feature (GIOV) where requests from the I/O device without a PASID are treated as if they have PASID of 0.- Part 3 (patch 7)
Introduce new "amd_iommu_pgtable" command-line to allow users to select the mode of operation (v1 or v2).
Something I didn't get entirely from the review is support level of the amd_iommu_v2 driver. I think it will continue to work and the requirement that the device identity maps DMA requests without PASID is removed, right?
Regards,
Joerg
_______________________________________________
iommu mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/iommu
Hi Joerg,
Quoted 10 lines · Joerg Roedel
Quoted 9 lines · Vasant Hegde
- Part 1 (patch 1-4 and 6)
Refactor the current IOMMU page table code to adopt the generic IO page table framework, and add AMD IOMMU Guest (v2) page table management code.- Part 2 (patch 5)
Add support for the AMD IOMMU Guest IO Protection feature (GIOV) where requests from the I/O device without a PASID are treated as if they have PASID of 0.
Quoted 8 lines
Quoted 3 lines
- Part 3 (patch 7)
Introduce new "amd_iommu_pgtable" command-line to allow users to select the mode of operation (v1 or v2).Something I didn't get entirely from the review is support level of the
amd_iommu_v2driver. I think it will continue to work and the requirement that the device identity maps DMA requests without PASID is removed, right?
Sorry. I didn't get last statement ("device identity maps DMA requests without PASID"). Can you please elaborate?
-Vasant
_______________________________________________
iommu mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/iommu
Joerg Roedel
maintainerQuoted 2 lines · Vasant Hegde
Sorry. I didn't get last statement ("device identity maps DMA requests without PASID"). Can you please elaborate?
When using v1 page-tables, each device supporting ATS/PRI/PASID needs to be direct-mapped, because the v1 page-tables basically act as a stage-2 page table for the PASID ones.
But when the non-pasid case moves to the pasid==0 page-table, then there is not stage-2 anymore and a device can be used with ATS/PRI/PASID while non-PASID requests are translated too, no?
I didn't get how this is handled in the current patch-set.
Regards,
Joerg
_______________________________________________
iommu mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/iommu
Suthikulpanit, Suravee
reviewer(RESEND)
Joerg,
The goal of this series is to allow DMA-API to use IOMMUv2 page table with PASID=0 (instead of v1 only). The main use-case is to support nested IO page table for vIOMMU support (work in progress).
Quoted 7 lines · Joerg Roedel
Quoted 2 lines · Vasant Hegde
Sorry. I didn't get last statement ("device identity maps DMA requests without PASID"). Can you please elaborate?
When using v1 page-tables, each device supporting ATS/PRI/PASID needs to be direct-mapped, because the v1 page-tables basically act as a stage-2 page table for the PASID ones.
For ATS/PRI capable devices, it is currently handled by the existing AMD IOMMU v2 APIs, In this case, the v1 table (stage2) is setup with DTE[mode]=0, and we set up v2 table (stage1) to bind the process address space to the gCR3 table with the specified PASID.
Quoted 3 lines
But when the non-pasid case moves to the pasid==0 page-table, then there is not stage-2 anymore and a device can be used with ATS/PRI/PASID while non-PASID requests are translated too, no?
When we setup IOMMU v2 table w/ PASID=0, we also set the DTE[mode]=0 to setup v1 table (stage 2) as identity-map (see set_dte_entry()).
In this case, if the device doesn't support ATS and PRI capability, we don't set DTE[PPR] bit and/or DTE[I] (IOTLB enable for ATS)
Also, in case of booting w/ iommu=pt (i.e. identity mapping), we fallback to using v1 page table (setting DTE[Mode]=0).
Regards,
Suravee