From 4c79d069a1cd449648380f580dd1c9aa8fdd6519 Mon Sep 17 00:00:00 2001 From: Julien Grall Date: Tue, 28 Apr 2015 15:32:31 +0100 Subject: [PATCH] xen/passthrough: Introduce iommu_construct This new function will correctly initialize the IOMMU page table for the current domain. Also use it in iommu_assign_dt_device even though the current IOMMU implementation on ARM shares P2M with the processor. Signed-off-by: Julien Grall Acked-by: Jan Beulich Acked-by: Ian Campbell --- xen/drivers/passthrough/arm/iommu.c | 6 ++++++ xen/drivers/passthrough/device_tree.c | 12 ++++++++++++ xen/drivers/passthrough/iommu.c | 26 ++++++++++++++++++++++++++ xen/drivers/passthrough/pci.c | 22 ++++------------------ xen/include/xen/iommu.h | 2 ++ 5 files changed, 50 insertions(+), 18 deletions(-) diff --git a/xen/drivers/passthrough/arm/iommu.c b/xen/drivers/passthrough/arm/iommu.c index 3007b992e7..92346579a9 100644 --- a/xen/drivers/passthrough/arm/iommu.c +++ b/xen/drivers/passthrough/arm/iommu.c @@ -68,3 +68,9 @@ void arch_iommu_domain_destroy(struct domain *d) { iommu_dt_domain_destroy(d); } + +int arch_iommu_populate_page_table(struct domain *d) +{ + /* The IOMMU shares the p2m with the CPU */ + return -ENOSYS; +} diff --git a/xen/drivers/passthrough/device_tree.c b/xen/drivers/passthrough/device_tree.c index 377d41d43b..4d82a09759 100644 --- a/xen/drivers/passthrough/device_tree.c +++ b/xen/drivers/passthrough/device_tree.c @@ -41,6 +41,18 @@ int iommu_assign_dt_device(struct domain *d, struct dt_device_node *dev) if ( !list_empty(&dev->domain_list) ) goto fail; + if ( need_iommu(d) <= 0 ) + { + /* + * The hwdom is forced to use IOMMU for protecting assigned + * device. Therefore the IOMMU data is already set up. + */ + ASSERT(!is_hardware_domain(d)); + rc = iommu_construct(d); + if ( rc ) + goto fail; + } + rc = hd->platform_ops->assign_device(d, 0, dt_to_dev(dev)); if ( rc ) diff --git a/xen/drivers/passthrough/iommu.c b/xen/drivers/passthrough/iommu.c index 92ea26f8b4..ae42aaefc4 100644 --- a/xen/drivers/passthrough/iommu.c +++ b/xen/drivers/passthrough/iommu.c @@ -187,6 +187,32 @@ void iommu_teardown(struct domain *d) tasklet_schedule(&iommu_pt_cleanup_tasklet); } +int iommu_construct(struct domain *d) +{ + if ( need_iommu(d) > 0 ) + return 0; + + if ( !iommu_use_hap_pt(d) ) + { + int rc; + + rc = arch_iommu_populate_page_table(d); + if ( rc ) + return rc; + } + + d->need_iommu = 1; + /* + * There may be dirty cache lines when a device is assigned + * and before need_iommu(d) becoming true, this will cause + * memory_type_changed lose effect if memory type changes. + * Call memory_type_changed here to amend this. + */ + memory_type_changed(d); + + return 0; +} + void iommu_domain_destroy(struct domain *d) { struct hvm_iommu *hd = domain_hvm_iommu(d); diff --git a/xen/drivers/passthrough/pci.c b/xen/drivers/passthrough/pci.c index 9f3413c63f..af2642393f 100644 --- a/xen/drivers/passthrough/pci.c +++ b/xen/drivers/passthrough/pci.c @@ -1358,25 +1358,11 @@ static int assign_device(struct domain *d, u16 seg, u8 bus, u8 devfn) if ( !spin_trylock(&pcidevs_lock) ) return -ERESTART; - if ( need_iommu(d) <= 0 ) + rc = iommu_construct(d); + if ( rc ) { - if ( !iommu_use_hap_pt(d) ) - { - rc = arch_iommu_populate_page_table(d); - if ( rc ) - { - spin_unlock(&pcidevs_lock); - return rc; - } - } - d->need_iommu = 1; - /* - * There may be dirty cache lines when a device is assigned - * and before need_iommu(d) becoming true, this will cause - * memory_type_changed lose effect if memory type changes. - * Call memory_type_changed here to amend this. - */ - memory_type_changed(d); + spin_unlock(&pcidevs_lock); + return rc; } pdev = pci_get_pdev_by_domain(hardware_domain, seg, bus, devfn); diff --git a/xen/include/xen/iommu.h b/xen/include/xen/iommu.h index bf4aff06d1..e9d2d5c280 100644 --- a/xen/include/xen/iommu.h +++ b/xen/include/xen/iommu.h @@ -65,6 +65,8 @@ int arch_iommu_domain_init(struct domain *d); int arch_iommu_populate_page_table(struct domain *d); void arch_iommu_check_autotranslated_hwdom(struct domain *d); +int iommu_construct(struct domain *d); + /* Function used internally, use iommu_domain_destroy */ void iommu_teardown(struct domain *d); -- 2.30.2