xen/arm: Add support for SMMUv3 driver
Re: [PATCH v2 7/8] xen/arm: Remove Linux specific code that is not usable in XEN
In reply to Julien GrallHello Julien,
Quoted 11 lines · Julien Grall
Quoted 2 lines · Rahul Singh
struct
io_pgtable_ops, structio_pgtable_cfg, structiommu_flush_ops, and structiommu_opsrelated code are linux specific.So the assumption is we are going to support only sharing with the P2M and the IOMMU. That's probably fine short term, but long-term we are going to need unsharing the page-tables (there are issues on some platforms if the ITS doorbell is accessed by the processors).
I am ok with removing anything related to the unsharing code. But I think it should be clarified here.
Ok I will add in commit message what is removed from the code.
Quoted 14 lines
Quoted 14 lines
Remove code related to above struct as code is dead code in XEN.
Signed-off-by Rahul Singh <[email protected]>--- xen/drivers/passthrough/arm/smmu-v3.c | 457 -------------------------- 1 file changed, 457 deletions(-)xen/drivers/passthrough/arm/smmu-v3.c +0
-0@@ -402,13 +402,7 @@#define ARM_SMMU_CMDQ_SYNC_TIMEOUT_US 1000000/* 1s! */ #define ARM_SMMU_CMDQ_SYNC_SPIN_COUNT 10 -#define MSI_IOVA_BASE 0x8000000
Quoted 8 lines
xen/drivers/passthrough/arm/smmu-v3.c line 411
Quoted 6 lines
xen/drivers/passthrough/arm/smmu-v3.c +0
-5-#define MSI_IOVA_LENGTH 0x100000 -static bool disable_bypass =1 ; -module_param_named (disable_bypass, disable_bypass, bool,S_IRUGO ); -MODULE_PARM_DESC (disable_bypass, -"Disable bypass streams such that incoming transactions from devices that are not attached to an iommu domain will report an abort back to the device and will not be allowed to pass through the SMMU." );Per your commit message, this doesn't look related to what you intend to remove. Maybe your commit message should be updated?
Ok.
Quoted 87 lines
Quoted 87 lines
xen/drivers/passthrough/arm/smmu-v3.c +0
-68enum pri_resp {PRI_RESP_DENY =0 ,@@ -599,7 +593,6 @@ struct arm_smmu_domain {struct arm_smmu_device *smmu;struct mutex init_mutex;/* Protects smmu pointer */ - struct io_pgtable_ops *pgtbl_ops;bool non_strict;enum arm_smmu_domain_stage stage;@@ -1297,74 +1290,6 @@ static void arm_smmu_tlb_inv_context(void *cookie)arm_smmu_cmdq_issue_sync (smmu); } -static void arm_smmu_tlb_inv_range_nosync(unsigned long iova, size_t size, -size_t granule,bool leaf,void *cookie) -{ -struct arm_smmu_domain *smmu_domain = cookie; -struct arm_smmu_device *smmu = smmu_domain->smmu; -struct arm_smmu_cmdq_ent cmd = { - .tlbi = { - .leaf = leaf, - .addr = iova, - }, - }; - - cmd.opcode =CMDQ_OP_TLBI_S2_IPA ; - cmd.tlbi.vmid = smmu_domain->s2_cfg.vmid; - -do { -arm_smmu_cmdq_issue_cmd (smmu, &cmd); - cmd.tlbi.addr += granule; - }while (size -= granule); -} - -static void arm_smmu_tlb_inv_page_nosync (struct iommu_iotlb_gather *gather, -unsigned long iova,size_t granule, -void *cookie) -{ -arm_smmu_tlb_inv_range_nosync (iova, granule, granule, true, cookie); -} - -static void arm_smmu_tlb_inv_walk (unsigned long iova,size_t size, -size_t granule,void *cookie) -{ -struct arm_smmu_domain *smmu_domain = cookie; -struct arm_smmu_device *smmu = smmu_domain->smmu; - -arm_smmu_tlb_inv_range_nosync (iova, size, granule, false, cookie); -arm_smmu_cmdq_issue_sync (smmu); -} - -static void arm_smmu_tlb_inv_leaf (unsigned long iova,size_t size, -size_t granule,void *cookie) -{ -struct arm_smmu_domain *smmu_domain = cookie; -struct arm_smmu_device *smmu = smmu_domain->smmu; - -arm_smmu_tlb_inv_range_nosync (iova, size, granule, true, cookie); -arm_smmu_cmdq_issue_sync (smmu); -} - -static const struct iommu_flush_ops arm_smmu_flush_ops = { - .tlb_flush_all = arm_smmu_tlb_inv_context, - .tlb_flush_walk = arm_smmu_tlb_inv_walk, - .tlb_flush_leaf = arm_smmu_tlb_inv_leaf, - .tlb_add_page = arm_smmu_tlb_inv_page_nosync, -}; - -/* IOMMU API */ -static bool arm_smmu_capable (enum iommu_cap cap) -{ -switch (cap) { -case IOMMU_CAP_CACHE_COHERENCY : -return true; -case IOMMU_CAP_NOEXEC : -return true; -default : -return false; - } -} - static struct iommu_domain *arm_smmu_domain_alloc(unsigned type) {struct arm_smmu_domain *smmu_domain;@@ -1421,7 +1346,6 @@ static void arm_smmu_domain_free(struct iommu_domain *domain)struct arm_smmu_s2_cfg *cfg = &smmu_domain->s2_cfg;iommu_put_dma_cookie (domain); -free_io_pgtable_ops (smmu_domain->pgtbl_ops);
Quoted 8 lines
xen/drivers/passthrough/arm/smmu-v3.c static void arm_smmu_domain_free(struct iommu_domain *domain) · line 1355
Quoted 6 lines
xen/drivers/passthrough/arm/smmu-v3.c +0
-0if (cfg->vmid)arm_smmu_bitmap_free (smmu->vmid_map, cfg->vmid);@@ -1429,7 +1353,6 @@ static void arm_smmu_domain_free(struct iommu_domain *domain)kfree (smmu_domain); } -Looks like a spurious change.
Ack.
Quoted 6 lines
xen/drivers/passthrough/arm/smmu-v3.c static void arm_smmu_domain_free(struct iommu_domain *domain) · line 1358
Quoted 4 lines
xen/drivers/passthrough/arm/smmu-v3.c +0
-0static int arm_smmu_domain_finalise_s2 (struct arm_smmu_domain *smmu_domain,struct arm_smmu_master *master,struct io_pgtable_cfg *pgtbl_cfg)You commit message leads to think that all the use of struct
io_pgtable_cfgwill be removed.
Ok I will remove it.
Quoted 8 lines
xen/drivers/passthrough/arm/smmu-v3.c static int arm_smmu_domain_finalise_s2(struct arm_smmu_domain *smmu_domain, · line 1440
Quoted 6 lines
xen/drivers/passthrough/arm/smmu-v3.c +0
-1@@ -1437,7 +1360,6 @@ static int arm_smmu_domain_finalise_s2(struct arm_smmu_domain *smmu_domain,int vmid;struct arm_smmu_device *smmu = smmu_domain->smmu;struct arm_smmu_s2_cfg *cfg = &smmu_domain->s2_cfg; - typeof(&pgtbl_cfg->arm_lpae_s2_cfg.vtcr) vtcr;It feels a bit odd that the definition of 'vtcr' is removed but there are still users.
Ack. Will fix that.
Quoted 14 lines
Quoted 14 lines
xen/drivers/passthrough/arm/smmu-v3.c +0
-3vmid =arm_smmu_bitmap_alloc (smmu->vmid_map, smmu->vmid_bits);if (vmid <0 )@@ -1461,20 +1383,12 @@ static int arm_smmu_domain_finalise(struct iommu_domain *domain, {int ret;unsigned long ias, oas; -enum io_pgtable_fmt fmt; -struct io_pgtable_cfg pgtbl_cfg; -struct io_pgtable_ops *pgtbl_ops;int (*finalise_stage_fn)(struct arm_smmu_domain *,struct arm_smmu_master *,struct io_pgtable_cfg *);struct arm_smmu_domain *smmu_domain =to_smmu_domain (domain);
Quoted 8 lines
xen/drivers/passthrough/arm/smmu-v3.c static int arm_smmu_domain_finalise(struct iommu_domain *domain, · line 1477
Quoted 6 lines
xen/drivers/passthrough/arm/smmu-v3.c +0
-4struct arm_smmu_device *smmu = smmu_domain->smmu; - if (domain->type == IOMMU_DOMAIN_IDENTITY) { - smmu_domain->stage =ARM_SMMU_DOMAIN_BYPASS ; -return 0 ; - } -Per your commit message, this doesn't look related to what you intend to remove. Maybe your commit message should be updated?
Ok I will update the commit message.
Quoted 113 lines
Quoted 113 lines
xen/drivers/passthrough/arm/smmu-v3.c +0
-85/* Restrict the stage to what we can actually support */ smmu_domain->stage =ARM_SMMU_DOMAIN_S2 ; @@ -1483,40 +1397,17 @@ static int arm_smmu_domain_finalise(struct iommu_domain *domain, case ARM_SMMU_DOMAIN_S2: ias = smmu->ias; oas = smmu->oas; - fmt =ARM_64_LPAE_S2 ; finalise_stage_fn = arm_smmu_domain_finalise_s2;break ;default :return -EINVAL ; } - pgtbl_cfg = (struct io_pgtable_cfg ) { - .pgsize_bitmap = smmu->pgsize_bitmap, - .ias = ias, - .oas = oas, - .coherent_walk = smmu->features &ARM_SMMU_FEAT_COHERENCY , - .tlb = &arm_smmu_flush_ops, - .iommu_dev = smmu->dev, - }; - -if (smmu_domain->non_strict) - pgtbl_cfg.quirks |=IO_PGTABLE_QUIRK_NON_STRICT ; - - pgtbl_ops =alloc_io_pgtable_ops (fmt, &pgtbl_cfg, smmu_domain); -if (!pgtbl_ops) -return -ENOMEM ; - - domain->pgsize_bitmap = pgtbl_cfg.pgsize_bitmap; - domain->geometry.aperture_end = (1UL << pgtbl_cfg.ias) -1 ; - domain->geometry.force_aperture = true; - ret = finalise_stage_fn(smmu_domain, master, &pgtbl_cfg);if (ret <0 ) { -free_io_pgtable_ops (pgtbl_ops);return ret; } - smmu_domain->pgtbl_ops = pgtbl_ops;return 0 ; } @@ -1626,71 +1517,6 @@ out_unlock:return ret; } -static int arm_smmu_map(struct iommu_domain *domain, unsigned long iova, -phys_addr_t paddr,size_t size,int prot,gfp_t gfp) -{ -struct io_pgtable_ops *ops =to_smmu_domain (domain)->pgtbl_ops; - -if (!ops) -return -ENODEV ; - -return ops->map (ops, iova, paddr, size, prot, gfp); -} - -static size_t arm_smmu_unmap (struct iommu_domain *domain,unsigned long iova, -size_t size,struct iommu_iotlb_gather *gather) -{ -struct arm_smmu_domain *smmu_domain =to_smmu_domain (domain); -struct io_pgtable_ops *ops = smmu_domain->pgtbl_ops; - -if (!ops) -return 0 ; - -return ops->unmap (ops, iova, size, gather); -} - -static void arm_smmu_flush_iotlb_all (struct iommu_domain *domain) -{ -struct arm_smmu_domain *smmu_domain =to_smmu_domain (domain); - -if (smmu_domain->smmu) -arm_smmu_tlb_inv_context (smmu_domain); -} - -static void arm_smmu_iotlb_sync (struct iommu_domain *domain, -struct iommu_iotlb_gather *gather) -{ -struct arm_smmu_device *smmu =to_smmu_domain (domain)->smmu; - -if (smmu) -arm_smmu_cmdq_issue_sync (smmu); -} - -static phys_addr_t -arm_smmu_iova_to_phys (struct iommu_domain *domain,dma_addr_t iova) -{ -struct io_pgtable_ops *ops =to_smmu_domain (domain)->pgtbl_ops; - -if (domain->type ==IOMMU_DOMAIN_IDENTITY ) -return iova; - -if (!ops) -return 0 ; - -return ops->iova_to_phys (ops, iova); -} - -static struct platform_driver arm_smmu_driver; - -static -struct arm_smmu_device *arm_smmu_get_by_fwnode (struct fwnode_handle *fwnode) -{ -struct device *dev =driver_find_device_by_fwnode (&arm_smmu_driver.driver, - fwnode); -put_device (dev); -return dev ?dev_get_drvdata (dev) :NULL ; -} - static bool arm_smmu_sid_in_range(struct arm_smmu_device *smmu, u32 sid) {unsigned long limit = smmu->strtab_cfg.num_l1_ents;@@ -1701,206 +1527,6 @@ static bool arm_smmu_sid_in_range(struct arm_smmu_device *smmu, u32 sid)
Quoted 8 lines
xen/drivers/passthrough/arm/smmu-v3.c static bool arm_smmu_sid_in_range(struct arm_smmu_device *smmu, u32 sid) · line 1707
Quoted 6 lines
xen/drivers/passthrough/arm/smmu-v3.c +0
-3return sid < limit; } -static struct iommu_ops arm_smmu_ops; - -static struct iommu_device *arm_smmu_probe_device (struct device *dev) -{Most of the code here looks useful to Xen. I think you want to keep the code and re-use it afterwards.
Ok. I removed the code here and added the XEN compatible code to add devices in next patch. I will keep it in this patch and will modifying the code to make XEN compatible.
Quoted 199 lines
Quoted 199 lines
xen/drivers/passthrough/arm/smmu-v3.c +0
-196-int i, ret; -struct arm_smmu_device *smmu; -struct arm_smmu_master *master; -struct iommu_fwspec *fwspec =dev_iommu_fwspec_get (dev); - -if (!fwspec || fwspec->ops != &arm_smmu_ops) -return ERR_PTR (-ENODEV ); - -if (WARN_ON_ONCE (dev_iommu_priv_get (dev))) -return ERR_PTR (-EBUSY ); - - smmu =arm_smmu_get_by_fwnode (fwspec->iommu_fwnode); -if (!smmu) -return ERR_PTR (-ENODEV ); - - master =kzalloc (sizeof (*master),GFP_KERNEL ); -if (!master) -return ERR_PTR (-ENOMEM ); - - master->dev = dev; - master->smmu = smmu; - master->sids = fwspec->ids; - master->num_sids = fwspec->num_ids; -dev_iommu_priv_set (dev, master); - -/* Check the SIDs are in range of the SMMU and our stream table */ -for (i =0 ; i < master->num_sids; i++) { -u32 sid = master->sids[i]; - -if (!arm_smmu_sid_in_range (smmu, sid)) { - ret = -ERANGE ; - goto err_free_master; - } - -/* Ensure l2 strtab is initialised */ -if (smmu->features &ARM_SMMU_FEAT_2_LVL_STRTAB ) { - ret =arm_smmu_init_l2_strtab (smmu, sid); -if (ret) - goto err_free_master; - } - } - -return &smmu->iommu; - -err_free_master: -kfree (master); -dev_iommu_priv_set (dev,NULL ); -return ERR_PTR (ret); -} - -static void arm_smmu_release_device (struct device *dev) -{ -struct iommu_fwspec *fwspec =dev_iommu_fwspec_get (dev); -struct arm_smmu_master *master; - -if (!fwspec || fwspec->ops != &arm_smmu_ops) -return ; - - master =dev_iommu_priv_get (dev); -arm_smmu_detach_dev (master); -kfree (master); -iommu_fwspec_free (dev); -} - -static struct iommu_group *arm_smmu_device_group (struct device *dev) -{ -struct iommu_group *group; - -/* -* We don't support devices sharing stream IDs other than PCI RID -* aliases, since the necessary ID-to-device lookup becomes rather -* impractical given a potential sparse 32-bit stream ID space. -*/ -if (dev_is_pci (dev)) - group =pci_device_group (dev); -else - group =generic_device_group (dev); - -return group; -} - -static int arm_smmu_domain_get_attr (struct iommu_domain *domain, -enum iommu_attr attr,void *data) -{ -struct arm_smmu_domain *smmu_domain =to_smmu_domain (domain); - -switch (domain->type) { -case IOMMU_DOMAIN_UNMANAGED : -switch (attr) { -case DOMAIN_ATTR_NESTING : - *(int *)data = (smmu_domain->stage ==ARM_SMMU_DOMAIN_NESTED ); -return 0 ; -default : -return -ENODEV ; - } -break ; -case IOMMU_DOMAIN_DMA : -switch (attr) { -case DOMAIN_ATTR_DMA_USE_FLUSH_QUEUE : - *(int *)data = smmu_domain->non_strict; -return 0 ; -default : -return -ENODEV ; - } -break ; -default : -return -EINVAL ; - } -} - -static int arm_smmu_domain_set_attr (struct iommu_domain *domain, -enum iommu_attr attr,void *data) -{ -int ret =0 ; -struct arm_smmu_domain *smmu_domain =to_smmu_domain (domain); - -mutex_lock (&smmu_domain->init_mutex); - -switch (domain->type) { -case IOMMU_DOMAIN_UNMANAGED : -switch (attr) { -case DOMAIN_ATTR_NESTING : -if (smmu_domain->smmu) { - ret = -EPERM ; - goto out_unlock; - } - -if (*(int *)data) - smmu_domain->stage =ARM_SMMU_DOMAIN_NESTED ; -else - smmu_domain->stage =ARM_SMMU_DOMAIN_S1 ; -break ; -default : - ret = -ENODEV ; - } -break ; -case IOMMU_DOMAIN_DMA : -switch (attr) { -case DOMAIN_ATTR_DMA_USE_FLUSH_QUEUE : - smmu_domain->non_strict = *(int *)data; -break ; -default : - ret = -ENODEV ; - } -break ; -default : - ret = -EINVAL ; - } - -out_unlock: -mutex_unlock (&smmu_domain->init_mutex); -return ret; -} - -static int arm_smmu_of_xlate (struct device *dev,struct of_phandle_args *args) -{ -return iommu_fwspec_add_ids (dev, args->args,1 ); -} - -static void arm_smmu_get_resv_regions (struct device *dev, -struct list_head *head) -{ -struct iommu_resv_region *region; -int prot =IOMMU_WRITE |IOMMU_NOEXEC |IOMMU_MMIO ; - - region =iommu_alloc_resv_region (MSI_IOVA_BASE ,MSI_IOVA_LENGTH , - prot,IOMMU_RESV_SW_MSI ); -if (!region) -return ; - -list_add_tail (®ion->list, head); - -iommu_dma_get_resv_regions (dev, head); -} - -static struct iommu_ops arm_smmu_ops = { - .capable = arm_smmu_capable, - .domain_alloc = arm_smmu_domain_alloc, - .domain_free = arm_smmu_domain_free, - .attach_dev = arm_smmu_attach_dev, - .map = arm_smmu_map, - .unmap = arm_smmu_unmap, - .flush_iotlb_all = arm_smmu_flush_iotlb_all, - .iotlb_sync = arm_smmu_iotlb_sync, - .iova_to_phys = arm_smmu_iova_to_phys, - .probe_device = arm_smmu_probe_device, - .release_device = arm_smmu_release_device, - .device_group = arm_smmu_device_group, - .domain_get_attr = arm_smmu_domain_get_attr, - .domain_set_attr = arm_smmu_domain_set_attr, - .of_xlate = arm_smmu_of_xlate, - .get_resv_regions = arm_smmu_get_resv_regions, - .put_resv_regions = generic_iommu_put_resv_regions, - .pgsize_bitmap =-1UL ,/* Restricted during device attach */ -}; -/* Probing and initialisation functions */ static int arm_smmu_init_one_queue(struct arm_smmu_device *smmu,
Quoted 8 lines
xen/drivers/passthrough/arm/smmu-v3.c static int arm_smmu_device_hw_probe(struct arm_smmu_device *smmu) · line 2409
Quoted 6 lines
xen/drivers/passthrough/arm/smmu-v3.c +0
-1struct arm_smmu_queue *q,@@ -2406,7 +2032,6 @@ static int arm_smmu_device_hw_probe(struct arm_smmu_device *smmu)switch (FIELD_GET (IDR0_STALL_MODEL , reg)) {case IDR0_STALL_MODEL_FORCE : smmu->features |=ARM_SMMU_FEAT_STALL_FORCE ; - fallthrough;We should keep all the fallthrough documented. So I think we want to introduce the fallthrough in Xen as well.
Ok I will keep fallthrough documented in this patch.
fallthrough implementation in XEN should be another patch. I am not sure when we can implement but we will try to implement.
Quoted 75 lines
Quoted 75 lines
xen/drivers/passthrough/arm/smmu-v3.c +0
-45case IDR0_STALL_MODEL_STALL : smmu->features |=ARM_SMMU_FEAT_STALLS ; }@@ -2426,7 +2051,6 @@ static int arm_smmu_device_hw_probe(struct arm_smmu_device *smmu)switch (FIELD_GET (IDR0_TTF , reg)) {case IDR0_TTF_AARCH32_64 : smmu->ias =40 ; - fallthrough;case IDR0_TTF_AARCH64 :break ;default :@@ -2515,21 +2139,10 @@ static int arm_smmu_device_hw_probe(struct arm_smmu_device *smmu)default :dev_info (smmu->dev,"unknown output address size. Truncating to 48-bit\n" ); - fallthrough;case IDR5_OAS_48_BIT : smmu->oas =48 ; } - if (arm_smmu_ops.pgsize_bitmap == -1UL) - arm_smmu_ops.pgsize_bitmap = smmu->pgsize_bitmap; - else - arm_smmu_ops.pgsize_bitmap |= smmu->pgsize_bitmap; - -/* Set the DMA mask for our table walker */ -if (dma_set_mask_and_coherent (smmu->dev,DMA_BIT_MASK (smmu->oas))) -dev_warn (smmu->dev, -"failed to set DMA mask for table walker\n" ); - smmu->ias = max(smmu->ias, smmu->oas); dev_info(smmu->dev, "ias %lu-bit, oas %lu-bit (features 0x%08x)\n",@@ -2595,9 +2208,6 @@ static int arm_smmu_device_dt_probe(struct platform_device *pdev,parse_driver_options (smmu); - if (of_dma_is_coherent(dev->of_node)) - smmu->features |= ARM_SMMU_FEAT_COHERENCY; -return ret; } @@ -2609,55 +2219,6 @@ static unsigned long arm_smmu_resource_size(struct arm_smmu_device *smmu) return SZ_128K; } -static int arm_smmu_set_bus_ops(struct iommu_ops *ops) -{ -int err; - -#ifdef CONFIG_PCI -if (pci_bus_type.iommu_ops != ops) { - err =bus_set_iommu (&pci_bus_type, ops); -if (err) -return err; - } -#endif -#ifdef CONFIG_ARM_AMBA -if (amba_bustype.iommu_ops != ops) { - err =bus_set_iommu (&amba_bustype, ops); -if (err) - goto err_reset_pci_ops; - } -#endif -if (platform_bus_type.iommu_ops != ops) { - err =bus_set_iommu (&platform_bus_type, ops); -if (err) - goto err_reset_amba_ops; - } - -return 0 ; - -err_reset_amba_ops: -#ifdef CONFIG_ARM_AMBA -bus_set_iommu (&amba_bustype,NULL ); -#endif -err_reset_pci_ops:__maybe_unused ; -#ifdef CONFIG_PCI -bus_set_iommu (&pci_bus_type,NULL );
Quoted 9 lines
xen/drivers/passthrough/arm/smmu-v3.c static unsigned long arm_smmu_resource_size(struct arm_smmu_device *smmu) · line 2650
Quoted 6 lines
xen/drivers/passthrough/arm/smmu-v3.c +0
-6-#endif -return err; -} - -static void __iomem *arm_smmu_ioremap (struct device *dev,resource_size_t start, -resource_size_t size)This seems a bit odd that you are removing the function but not the callers. Shouldn't you keep this function until next patch?
Ok I will keep this in this patch and will remove in next patch.
Quoted 38 lines
Quoted 35 lines
xen/drivers/passthrough/arm/smmu-v3.c +0
-28-{ -struct resource res = { - .flags =IORESOURCE_MEM , - .start = start, - .end = start + size -1 , - }; - -return devm_ioremap_resource (dev, &res); -} -static int arm_smmu_device_probe (struct platform_device *pdev) {int irq, ret;@@ -2785,21 +2346,3 @@ static const struct of_device_id arm_smmu_of_match[] = { { .compatible = "arm,smmu-v3", }, { }, }; -MODULE_DEVICE_TABLE (of, arm_smmu_of_match); - -static struct platform_driver arm_smmu_driver = { - .driver = { - .name ="arm-smmu-v3" , - .of_match_table = arm_smmu_of_match, - .suppress_bind_attrs = true, - }, - .probe = arm_smmu_device_probe, - .remove = arm_smmu_device_remove, - .shutdown = arm_smmu_device_shutdown, -}; -module_platform_driver (arm_smmu_driver); - -MODULE_DESCRIPTION ("IOMMU API for ARM architected SMMUv3 implementations" ); -MODULE_AUTHOR ("Will Deacon <[email protected]>" ); -MODULE_ALIAS ("platform:arm-smmu-v3" ); -MODULE_LICENSE ("GPL v2" );Signature · 1 line
Julien Grall
Re: [PATCH v2 7/8] xen/arm: Remove Linux specific code that is not usable in XEN
Hi Rahul,
Quoted 9 lines · Rahul Singh
Quoted 6 lines · Julien Grall
xen/drivers/passthrough/arm/smmu-v3.c static bool arm_smmu_sid_in_range(struct arm_smmu_device *smmu, u32 sid) · line 1707
Quoted 3 lines
xen/drivers/passthrough/arm/smmu-v3.c +0
-3- -static struct iommu_device *arm_smmu_probe_device (struct device *dev) -{Most of the code here looks useful to Xen. I think you want to keep the code and re-use it afterwards.
Ok. I removed the code here and added the XEN compatible code to add devices in next patch. I will keep it in this patch and will modifying the code to make XEN compatible.
In general, it is prefer if the code the code rather than dropping in patch A and then add it again differently patch B. This makes easier to check that the code outcome of the function is mostly the same.
Quoted 32 lines
Quoted 23 lines
Quoted 23 lines
xen/drivers/passthrough/arm/smmu-v3.c +0
-21-static struct iommu_ops arm_smmu_ops = { - .capable = arm_smmu_capable, - .domain_alloc = arm_smmu_domain_alloc, - .domain_free = arm_smmu_domain_free, - .attach_dev = arm_smmu_attach_dev, - .map = arm_smmu_map, - .unmap = arm_smmu_unmap, - .flush_iotlb_all = arm_smmu_flush_iotlb_all, - .iotlb_sync = arm_smmu_iotlb_sync, - .iova_to_phys = arm_smmu_iova_to_phys, - .probe_device = arm_smmu_probe_device, - .release_device = arm_smmu_release_device, - .device_group = arm_smmu_device_group, - .domain_get_attr = arm_smmu_domain_get_attr, - .domain_set_attr = arm_smmu_domain_set_attr, - .of_xlate = arm_smmu_of_xlate, - .get_resv_regions = arm_smmu_get_resv_regions, - .put_resv_regions = generic_iommu_put_resv_regions, - .pgsize_bitmap =-1UL ,/* Restricted during device attach */ -}; -/* Probing and initialisation functions */ static int arm_smmu_init_one_queue(struct arm_smmu_device *smmu,Quoted 8 lines
xen/drivers/passthrough/arm/smmu-v3.c static int arm_smmu_device_hw_probe(struct arm_smmu_device *smmu) · line 2409
Quoted 6 lines
struct
arm_smmu_queue*q,xen/drivers/passthrough/arm/smmu-v3.c +0
-1@@ -2406,7 +2032,6 @@ static int arm_smmu_device_hw_probe(struct arm_smmu_device *smmu)switch (FIELD_GET (IDR0_STALL_MODEL , reg)) {case IDR0_STALL_MODEL_FORCE : smmu->features |=ARM_SMMU_FEAT_STALL_FORCE ; - fallthrough;We should keep all the fallthrough documented. So I think we want to introduce the fallthrough in Xen as well.
Quoted 3 lines
Ok I will keep fallthrough documented in this patch.
fallthrough implementation in XEN should be another patch. I am not sure when we can implement but we will try to implement.
Yes, I didn't ask to implement "fallthrough" in this patch, but instead as a pre-requirement patch.
I would implement it in include/xen/compiler.h.
Cheers,
Signature · 1 line
Julien Grall
Re: [PATCH v2 7/8] xen/arm: Remove Linux specific code that is not usable in XEN
Hello Julien,
Quoted 11 lines · Julien Grall
Hi Rahul,
Quoted 6 lines · Rahul Singh
Quoted 6 lines · Julien Grall
xen/drivers/passthrough/arm/smmu-v3.c static bool arm_smmu_sid_in_range(struct arm_smmu_device *smmu, u32 sid) · line 1707
Quoted 3 lines
xen/drivers/passthrough/arm/smmu-v3.c +0
-3- -static struct iommu_device *arm_smmu_probe_device (struct device *dev) -{Most of the code here looks useful to Xen. I think you want to keep the code and re-use it afterwards.
Quoted 5 lines
Quoted 2 lines
Ok. I removed the code here and added the XEN compatible code to add devices in next patch. I will keep it in this patch and will modifying the code to make XEN compatible.
In general, it is prefer if the code the code rather than dropping in patch A and then add it again differently patch B. This makes easier to check that the code outcome of the function is mostly the same.
Ok.
Quoted 31 lines
Quoted 31 lines
Quoted 31 lines
Quoted 25 lines
xen/drivers/passthrough/arm/smmu-v3.c +0
-21-static struct iommu_ops arm_smmu_ops = { - .capable = arm_smmu_capable, - .domain_alloc = arm_smmu_domain_alloc, - .domain_free = arm_smmu_domain_free, - .attach_dev = arm_smmu_attach_dev, - .map = arm_smmu_map, - .unmap = arm_smmu_unmap, - .flush_iotlb_all = arm_smmu_flush_iotlb_all, - .iotlb_sync = arm_smmu_iotlb_sync, - .iova_to_phys = arm_smmu_iova_to_phys, - .probe_device = arm_smmu_probe_device, - .release_device = arm_smmu_release_device, - .device_group = arm_smmu_device_group, - .domain_get_attr = arm_smmu_domain_get_attr, - .domain_set_attr = arm_smmu_domain_set_attr, - .of_xlate = arm_smmu_of_xlate, - .get_resv_regions = arm_smmu_get_resv_regions, - .put_resv_regions = generic_iommu_put_resv_regions, - .pgsize_bitmap =-1UL ,/* Restricted during device attach */ -}; -/* Probing and initialisation functions */ static int arm_smmu_init_one_queue(struct arm_smmu_device *smmu, struct arm_smmu_queue *q,xen/drivers/passthrough/arm/smmu-v3.c static int arm_smmu_device_hw_probe(struct arm_smmu_device *smmu) · line 2409
Quoted 4 lines
xen/drivers/passthrough/arm/smmu-v3.c +0
-1@@ -2406,7 +2032,6 @@ static int arm_smmu_device_hw_probe(struct arm_smmu_device *smmu)switch (FIELD_GET (IDR0_STALL_MODEL , reg)) {case IDR0_STALL_MODEL_FORCE : smmu->features |=ARM_SMMU_FEAT_STALL_FORCE ; - fallthrough;We should keep all the fallthrough documented. So I think we want to introduce the fallthrough in Xen as well.
Quoted 6 lines
Quoted 2 lines
Ok I will keep fallthrough documented in this patch.
fallthrough implementation in XEN should be another patch. I am not sure when we can implement but we will try to implement.Yes, I didn't ask to implement "fallthrough" in this patch, but instead as a pre-requirement patch.
I would implement it in
include/xen/compiler.h.
Ok. I will implement and will share the patch for “__attribute__((__fallthrough__)) ” but for SMMUv3 is that ok if we can proceed with “ /* fallthrough */ ".
As “fallthorugh” implementation is common for all architecture it required review for all stakeholder as per my understanding. I don’t want to block SMMUv3 because of this.
Regards,
Rahul
Quoted 5 lines
Cheers,
Signature · 1 line
Julien Grall
Re: [PATCH v2 7/8] xen/arm: Remove Linux specific code that is not usable in XEN
Quoted 3 lines · Rahul Singh
Quoted 1 line
I would implement it in
include/xen/compiler.h.Ok. I will implement and will share the patch for “__attribute__((__fallthrough__)) ” but for SMMUv3 is that ok if we can proceed with “ /* fallthrough */ ".
The first approach should always be to work with the community to introduce such functionality tree-wide. Note that I am not suggesting to replace existing "/* fallthrough */" with "fallthrough".
If it is taking too long or the task is significant, then we can discuss about way to temporally work around it.
I don't believe we are at that stage yet here.
Quoted 1 line
As “fallthorugh” implementation is common for all architecture it required review for all stakeholder as per my understanding. I don’t want to block SMMUv3 because of this.
It only requires review from "THE REST" maintainers.
Cheers,
Signature · 1 line
Julien Grall
PATCH v2 8/8 xen/arm: Add support for SMMUv3 driver
In reply to Rahul SinghAdd support for ARM architected SMMUv3 implementation. It is based on the Linux SMMUv3 driver.
Major differences with regard to Linux driver are as follows:
1. Only Stage-2 translation is supported as compared to the Linux driver that supports both Stage-1 and Stage-2 translations.
2. Use P2M page table instead of creating one as SMMUv3 has the capability to share the page tables with the CPU.
3. Tasklets are used in place of threaded IRQ's in Linux for event queue and priority queue IRQ handling.
4. Latest version of the Linux SMMUv3 code implements the commands queue access functions based on atomic operations implemented in Linux. Atomic functions used by the commands queue access functions are not implemented in XEN therefore we decided to port the earlier version of the code. Once the proper atomic operations will be available in XEN the driver can be updated.
5. Driver is currently supported as Tech Preview.
--- MAINTAINERS | 6 + SUPPORT.md | 1 + xen/drivers/passthrough/Kconfig | 10 + xen/drivers/passthrough/arm/Makefile | 1 + xen/drivers/passthrough/arm/smmu-v3.c | 986 +++++++++++++++++++++----- 5 files changed, 814 insertions(+), 190 deletions(-)
MAINTAINERS +6 -0
@@ -249,6 +249,12 @@ F: xen/include/asm-arm/ F: xen/include/public/arch-arm/ F: xen/include/public/arch-arm.h +ARM SMMUv3 +M: Bertrand Marquis <[email protected]> +M: Rahul Singh <[email protected]> +S: Supported +F: xen/drivers/passthrough/arm/smmu-v3.c + Change Log M: Paul Durrant <[email protected]> R: Community Manager <[email protected]>
SUPPORT.md +1 -0
@@ -68,6 +68,7 @@ For the Cortex A57 r0p0 - r1p1, see Errata 832075. Status, ARM SMMUv1: Supported, not security supported Status, ARM SMMUv2: Supported, not security supported Status, Renesas IPMMU-VMSA: Supported, not security supported + Status, ARM SMMUv3: Tech Preview ### ARM/GICv3 ITS
xen/drivers/passthrough/Kconfig +10 -0
@@ -13,6 +13,16 @@ config ARM_SMMU Say Y here if your SoC includes an IOMMU device implementing the ARM SMMU architecture. +config ARM_SMMU_V3 + bool "ARM Ltd. System MMU Version 3 (SMMUv3) Support" if EXPERT + depends on ARM_64 + ---help--- + Support for implementations of the ARM System MMU architecture + version 3. + + Say Y here if your system includes an IOMMU device implementing + the ARM SMMUv3 architecture. + config IPMMU_VMSA bool "Renesas IPMMU-VMSA found in R-Car Gen3 SoCs" depends on ARM_64
xen/drivers/passthrough/arm/Makefile +1 -0
@@ -1,3 +1,4 @@ obj-y += iommu.o iommu_helpers.o iommu_fwspec.o obj-$(CONFIG_ARM_SMMU) += smmu.o obj-$(CONFIG_IPMMU_VMSA) += ipmmu-vmsa.o +obj-$(CONFIG_ARM_SMMU_V3) += smmu-v3.o
xen/drivers/passthrough/arm/smmu-v3.c +796 -190
1,165 lines not on this page · raw message
Signature · 1 line
2.17.1
Quoted 17 lines · Rahul Singh
Add support for ARM architected SMMUv3 implementation. It is based on the Linux SMMUv3 driver.
Major differences with regard to Linux driver are as follows:
1. Only Stage-2 translation is supported as compared to the Linux driver that supports both Stage-1 and Stage-2 translations.
2. Use P2M page table instead of creating one as SMMUv3 has the capability to share the page tables with the CPU.
3. Tasklets are used in place of threaded IRQ's in Linux for event queue and priority queue IRQ handling.
4. Latest version of the Linux SMMUv3 code implements the commands queue access functions based on atomic operations implemented in Linux. Atomic functions used by the commands queue access functions are not implemented in XEN therefore we decided to port the earlier version of the code. Once the proper atomic operations will be available in XEN the driver can be updated.
5. Driver is currently supported as Tech Preview.
This patch is big and was difficult to review, nonetheless I tried :-)
That said, the code is self-contained, marked as Tech Preview, and not at risk of making other things unstable, so it is low risk to accept it.
Comments below.
Quoted 128 lines
Signed-off-by Rahul Singh <[email protected]>--- MAINTAINERS | 6 + SUPPORT.md | 1 + xen/drivers/passthrough/Kconfig | 10 + xen/drivers/passthrough/arm/Makefile | 1 + xen/drivers/passthrough/arm/smmu-v3.c | 986 +++++++++++++++++++++----- 5 files changed, 814 insertions(+), 190 deletions(-)MAINTAINERS +6
-0@@ -249,6 +249,12 @@ F: xen/include/asm-arm/ F: xen/include/public/arch-arm/ F: xen/include/public/arch-arm.h +ARM SMMUv3 +M: Bertrand Marquis <[email protected]> +M: Rahul Singh <[email protected]> +S: Supported +F: xen/drivers/passthrough/arm/smmu-v3.c + Change Log M: Paul Durrant <[email protected]> R: Community Manager <[email protected]>SUPPORT.md +1
-0@@ -68,6 +68,7 @@ For the Cortex A57 r0p0 - r1p1, see Errata 832075. Status, ARM SMMUv1: Supported, not security supported Status, ARM SMMUv2: Supported, not security supported Status, Renesas IPMMU-VMSA: Supported, not security supported + Status, ARM SMMUv3: Tech Preview ### ARM/GICv3 ITSxen/drivers/passthrough/Kconfig +10
-0@@ -13,6 +13,16 @@ config ARM_SMMU Say Y here if your SoC includes an IOMMU device implementing the ARM SMMU architecture. +config ARM_SMMU_V3 + bool "ARM Ltd. System MMU Version 3 (SMMUv3) Support" if EXPERT + depends on ARM_64 + ---help--- + Support for implementations of the ARM System MMU architecture + version 3. + + Say Y here if your system includes an IOMMU device implementing + the ARM SMMUv3 architecture. + config IPMMU_VMSA bool "Renesas IPMMU-VMSA found in R-Car Gen3 SoCs" depends on ARM_64xen/drivers/passthrough/arm/Makefile +1
-0@@ -1,3 +1,4 @@ obj-y += iommu.o iommu_helpers.o iommu_fwspec.o obj-$(CONFIG_ARM_SMMU) += smmu.o obj-$(CONFIG_IPMMU_VMSA) += ipmmu-vmsa.o +obj-$(CONFIG_ARM_SMMU_V3) += smmu-v3.oxen/drivers/passthrough/arm/smmu-v3.c +47
-3@@ -2,36 +2,280 @@/* * IOMMU API for ARM architected SMMUv3 implementations. * -* Copyright (C) 2015 ARM Limited +* Based on Linux's SMMUv3 driver: +* drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c +* commit: 951cbbc386ff01b50da4f46387e994e81d9ab431 +* and Xen's SMMU driver: +* xen/drivers/passthrough/arm/smmu.c * -* Author: Will Deacon <[email protected]> +* Copyright (C) 2015 ARM Limited Will Deacon <[email protected]> * -* This driver is powered by bad coffee and bombay mix. +* Copyright (C) 2020 Arm Ltd. +* +* This program is free software; you can redistribute it and/or modify +* it under the terms of the GNU General Public License version 2 as +* published by the Free Software Foundation. +* +* This program is distributed in the hope that it will be useful, +* but WITHOUT ANY WARRANTY; without even the implied warranty of +* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +* GNU General Public License for more details. +* +* You should have received a copy of the GNU General Public License +* along with this program. If not, see <http://www.gnu.org/licenses/>. +* +*/ + +#include <xen/acpi.h> +#include <xen/config.h> +#include <xen/delay.h> +#include <xen/errno.h> +#include <xen/err.h> +#include <xen/irq.h> +#include <xen/lib.h> +#include <xen/list.h> +#include <xen/mm.h> +#include <xen/rbtree.h> +#include <xen/sched.h> +#include <xen/sizes.h> +#include <xen/vmap.h> +#include <asm/atomic.h> +#include <asm/device.h> +#include <asm/io.h> +#include <asm/platform.h> +#include <asm/iommu_fwspec.h> + +/* Linux compatibility functions. */ +typedef paddr_t dma_addr_t ; +typedef unsigned int gfp_t ; + +#define platform_device device +
xen/drivers/passthrough/arm/smmu-v3.c line 63
Quoted 10 lines
xen/drivers/passthrough/arm/smmu-v3.c +10
-0+#define GFP_KERNEL 0 + +/* Alias to Xen device tree helpers */ +#define device_node dt_device_node +#define of_phandle_args dt_phandle_args +#define of_device_id dt_device_match +#define of_match_node dt_match_node +#define of_property_read_u32 (np, pname, out) (!dt_property_read_u32(np, pname, out)) +#define of_property_read_bool dt_property_read_bool +#define of_parse_phandle_with_args dt_parse_phandle_with_args
Given all the changes to the file by the previous patches we are basically fully (or almost fully) adapting this code to Xen.
So at that point I wonder if we should just as well make these changes (e.g. s/of_phandle_args/dt_phandle_args/g) to the code too.
Julien, Rahul, what do you guys think?
xen/drivers/passthrough/arm/smmu-v3.c line 65
Quoted 1 line
xen/drivers/passthrough/arm/smmu-v3.c +1
-0+/* Alias to Xen lock functions */
I think this deserves at least one statement to explain why it is OK.
Also, a similar comment to the one above: maybe we should add a couple of more preparation patches to s/mutex/spinlock/g and change the time and allocation functions too.
Quoted 24 lines
xen/drivers/passthrough/arm/smmu-v3.c +24
-0+#define mutex spinlock +#define mutex_init spin_lock_init +#define mutex_lock spin_lock +#define mutex_unlock spin_unlock + +/* Alias to Xen time functions */ +#define ktime_t s_time_t +#define ktime_get () (NOW()) +#define ktime_add_us (t,i) (t + MICROSECS(i)) +#define ktime_compare (t,i) (t > (i)) + +/* Alias to Xen allocation helpers */ +#define kzalloc (size, flags) _xzalloc(size, sizeof(void *)) +#define kfree xfree +#define devm_kzalloc (dev, size, flags) _xzalloc(size, sizeof(void *)) + +/* Device logger functions */ +#define dev_name (dev) dt_node_full_name(dev->of_node) +#define dev_dbg (dev, fmt, ...) \ + printk(XENLOG_DEBUG "SMMUv3: %s: " fmt, dev_name(dev), ## __VA_ARGS__) +#define dev_notice (dev, fmt, ...) \ + printk(XENLOG_INFO "SMMUv3: %s: " fmt, dev_name(dev), ## __VA_ARGS__) +#define dev_warn (dev, fmt, ...) \ + printk(XENLOG_WARNING "SMMUv3: %s: " fmt, dev_name(dev), ## __VA_ARGS__)
xen/drivers/passthrough/arm/smmu-v3.c line 99
Quoted 10 lines
xen/drivers/passthrough/arm/smmu-v3.c +10
-0+#define dev_err (dev, fmt, ...) \ + printk(XENLOG_ERR "SMMUv3: %s: " fmt, dev_name(dev), ## __VA_ARGS__) +#define dev_info (dev, fmt, ...) \ + printk(XENLOG_INFO "SMMUv3: %s: " fmt, dev_name(dev), ## __VA_ARGS__) +#define dev_err_ratelimited (dev, fmt, ...) \ + printk(XENLOG_ERR "SMMUv3: %s: " fmt, dev_name(dev), ## __VA_ARGS__) + +/* +* Periodically poll an address and wait between reads in us until a +* condition is met or a timeout occurs.
It would be good to add a statement to point out what the return value is going to be.
Quoted 12 lines
xen/drivers/passthrough/arm/smmu-v3.c +12
-0+*/ +#define readx_poll_timeout (op, addr, val, cond, sleep_us, timeout_us) \ +({ \ + s_time_t deadline = NOW() + MICROSECS(timeout_us); \ + for (;;) { \ + (val) = op(addr); \ + if (cond) \ + break; \ + if (NOW() > deadline) { \ + (val) = op(addr); \ + break; \ + } \
xen/drivers/passthrough/arm/smmu-v3.c line 121
Quoted 10 lines
xen/drivers/passthrough/arm/smmu-v3.c +10
-0+udelay (sleep_us); \ + } \ + (cond) ?0 : -ETIMEDOUT ; \ +}) + +#define readl_relaxed_poll_timeout (addr, val, cond, delay_us, timeout_us) \ + readx_poll_timeout(readl_relaxed, addr, val, cond, delay_us, timeout_us) + +#define FIELD_PREP (_mask, _val) \ + (((typeof(_mask))(_val) << (__builtin_ffsll(_mask) - 1)) & (_mask))
Let's add the definition of ffsll to bitops.h
xen/drivers/passthrough/arm/smmu-v3.c line 129
Quoted 7 lines
xen/drivers/passthrough/arm/smmu-v3.c +7
-0+#define FIELD_GET (_mask, _reg) \ + (typeof(_mask))(((_reg) & (_mask)) >> (__builtin_ffsll(_mask) - 1)) + +#define WRITE_ONCE (x, val) \ +do { \ + *(volatile typeof(x) *)&(x) = (val); \ +} while (0)
maybe we should define this in xen/include/xen/lib.h
xen/drivers/passthrough/arm/smmu-v3.c line 133
Quoted 4 lines
xen/drivers/passthrough/arm/smmu-v3.c +4
-0+ +/* Xen: Stub out DMA domain related functions */ +#define iommu_get_dma_cookie (dom) 0 +#define iommu_put_dma_cookie (dom)
Shouldn't we remove any call to iommu_get_dma_cookie and iommu_put_dma_cookie in one of the previous patches?
Quoted 56 lines
xen/drivers/passthrough/arm/smmu-v3.c +55
-0+/* +* Helpers for DMA allocation. Just the function name is reused for +* porting code, these allocation are not managed allocations */ +static void *dmam_alloc_coherent (struct device *dev,size_t size, +paddr_t *dma_handle,gfp_t gfp) +{ +void *vaddr; +unsigned long alignment = size; + +/* +* _xzalloc requires that the (align & (align -1)) = 0. Most of the +* allocations in SMMU code should send the right value for size. In +* case this is not true print a warning and align to the size of a +* (void *) +*/ +if ( size & (size -1 ) ) + { +printk (XENLOG_WARNING "SMMUv3: Fixing alignment for the DMA buffer\n" ); + alignment =sizeof (void *); + } + + vaddr =_xzalloc (size, alignment); +if ( !vaddr ) + { +printk (XENLOG_ERR "SMMUv3: DMA allocation failed\n" ); +return NULL ; + } + + *dma_handle =virt_to_maddr (vaddr); + +return vaddr; +} + +/* Xen: Type definitions for iommu_domain */ +#define IOMMU_DOMAIN_UNMANAGED 0 +#define IOMMU_DOMAIN_DMA 1 +#define IOMMU_DOMAIN_IDENTITY 2 + +/* Xen specific code. */ +struct iommu_domain { +/* Runtime SMMU configuration for this iommu_domain */ +atomic_t ref; +/* +* Used to link iommu_domain contexts for a same domain. +* There is at least one per-SMMU to used by the domain. +*/ +struct list_head list; +}; + +/* Describes information required for a Xen domain */ +struct arm_smmu_xen_domain { +spinlock_t lock; + +/* List of iommu domains associated to this domain */ +struct list_head contexts;
xen/drivers/passthrough/arm/smmu-v3.c line 200
Quoted 10 lines
xen/drivers/passthrough/arm/smmu-v3.c +10
-0+}; + +/* +* Information about each device stored in dev->archdata.iommu +* The dev->archdata.iommu stores the iommu_domain (runtime configuration of +* the SMMU). +*/ +struct arm_smmu_xen_device { +struct iommu_domain *domain; +};
Do we need both struct arm_smmu_xen_device and struct iommu_domain?
Quoted 39 lines
xen/drivers/passthrough/arm/smmu-v3.c +39
-0+/* Keep a list of devices associated with this driver */ +static DEFINE_SPINLOCK (arm_smmu_devices_lock); +static LIST_HEAD (arm_smmu_devices); + + +static inline void *dev_iommu_priv_get (struct device *dev) +{ +struct iommu_fwspec *fwspec =dev_iommu_fwspec_get (dev); + +return fwspec && fwspec->iommu_priv ? fwspec->iommu_priv :NULL ; +} + +static inline void dev_iommu_priv_set (struct device *dev,void *priv) +{ +struct iommu_fwspec *fwspec =dev_iommu_fwspec_get (dev); + + fwspec->iommu_priv = priv; +} + +int dt_property_match_string (const struct dt_device_node *np, +const char *propname,const char *string) +{ +const struct dt_property *dtprop =dt_find_property (np, propname,NULL ); +size_t l; +int i; +const char *p, *end; + +if ( !dtprop ) +return -EINVAL ; + +if ( !dtprop->value ) +return -ENODATA ; + + p = dtprop->value; + end = p + dtprop->length; + +for ( i =0 ; p < end; i++, p += l ) + { + l =strnlen (p, end - p) +1 ;
xen/drivers/passthrough/arm/smmu-v3.c line 250
Quoted 10 lines
xen/drivers/passthrough/arm/smmu-v3.c +10
-0+ +if ( p + l > end ) +return -EILSEQ ; + +if (strcmp (string, p) ==0 ) +return i;/* Found it; return index */ + } + +return -ENODATA ; +}
I think you should either use dt_property_read_string or move the implementation of dt_property_match_string to xen/common/device_tree.c (in which case I suggest to do it in a separate patch.)
I'd just use dt_property_read_string.
Quoted 78 lines
xen/drivers/passthrough/arm/smmu-v3.c +34
-25+static int platform_get_irq_byname_optional (struct device *dev, +const char *name) +{ +int index, ret; +struct dt_device_node *np =dev_to_dt (dev); + +if (unlikely (!name) ) +return -EINVAL ; + + index =dt_property_match_string (np,"interrupt-names" , name); +if ( index <0 ) + { +dev_info (dev,"IRQ %s not found\n" , name); +return index; + } -#include <linux/acpi.h> -#include <linux/acpi_iort.h> -#include <linux/bitfield.h> -#include <linux/bitops.h> -#include <linux/crash_dump.h> -#include <linux/delay.h> -#include <linux/dma-iommu.h> -#include <linux/err.h> -#include <linux/interrupt.h> -#include <linux/io-pgtable.h> -#include <linux/iommu.h> -#include <linux/iopoll.h> -#include <linux/module.h> -#include <linux/msi.h> -#include <linux/of.h> -#include <linux/of_address.h> -#include <linux/of_iommu.h> -#include <linux/of_platform.h> -#include <linux/pci.h> -#include <linux/pci-ats.h> -#include <linux/platform_device.h> - -#include <linux/amba/bus.h> + ret =platform_get_irq (np, index); +if ( ret <0 ) + { +dev_err (dev,"failed to get irq index %d\n" , index); +return -ENODEV ; + } + +return ret; +} + +/* Start of Linux SMMUv3 code */ /* MMIO registers */ #define ARM_SMMU_IDR0 0x0@@ -507,6 +751,7 @@ struct arm_smmu_s2_cfg {u16 vmid;u64 vttbr;u64 vtcr; +struct domain *domain; };struct arm_smmu_strtab_cfg {@@ -567,8 +812,13 @@ struct arm_smmu_device {struct arm_smmu_strtab_cfg strtab_cfg; -/* IOMMU core code handle */ -struct iommu_device iommu; +/* Need to keep a list of SMMU devices */ +struct list_head devices; + +/* Tasklets for handling evts/faults and pci page request IRQs*/ +struct tasklet evtq_irq_tasklet; +struct tasklet priq_irq_tasklet; +struct tasklet combined_irq_tasklet; };/* SMMU private data for each master */
xen/drivers/passthrough/arm/smmu-v3.c static int arm_smmu_init_l2_strtab(struct arm_smmu_device *smmu, u32 sid) · line 1363
Quoted 5 lines
xen/drivers/passthrough/arm/smmu-v3.c +1
-1@@ -1110,7 +1360,7 @@ static int arm_smmu_init_l2_strtab(struct arm_smmu_device *smmu, u32 sid) }/* IRQ and event handlers */ -static irqreturn_t arm_smmu_evtq_thread (int irq,void *dev) +static void arm_smmu_evtq_thread (void *dev)
I think we shouldn't call it a thread given that's a tasklet. We should rename the function or it will be confusing.
Quoted 12 lines
xen/drivers/passthrough/arm/smmu-v3.c +0
-1{int i;struct arm_smmu_device *smmu = dev;@@ -1140,7 +1390,6 @@ static irqreturn_t arm_smmu_evtq_thread(int irq, void *dev)/* Sync our overflow flag, as we believe we're up to speed */ llq->cons =Q_OVF (llq->prod) |Q_WRP (llq, llq->cons) |Q_IDX (llq, llq->cons); -return IRQ_HANDLED ; }static void arm_smmu_handle_ppr (struct arm_smmu_device *smmu,u64 *evt)
xen/drivers/passthrough/arm/smmu-v3.c static void arm_smmu_handle_ppr(struct arm_smmu_device *smmu, u64 *evt) · line 1433
Quoted 5 lines
xen/drivers/passthrough/arm/smmu-v3.c +1
-1@@ -1181,7 +1430,7 @@ static void arm_smmu_handle_ppr(struct arm_smmu_device *smmu, u64 *evt) } } -static irqreturn_t arm_smmu_priq_thread (int irq,void *dev) +static void arm_smmu_priq_thread (void *dev)
same here
Quoted 192 lines
xen/drivers/passthrough/arm/smmu-v3.c +83
-41{struct arm_smmu_device *smmu = dev;struct arm_smmu_queue *q = &smmu->priq.q;@@ -1200,12 +1449,12 @@ static irqreturn_t arm_smmu_priq_thread(int irq, void *dev) llq->cons =Q_OVF (llq->prod) |Q_WRP (llq, llq->cons) |Q_IDX (llq, llq->cons);queue_sync_cons_out (q); -return IRQ_HANDLED ; }static int arm_smmu_device_disable (struct arm_smmu_device *smmu); -static irqreturn_t arm_smmu_gerror_handler (int irq,void *dev) +static void arm_smmu_gerror_handler (int irq,void *dev, +struct cpu_user_regs *regs) {u32 gerror, gerrorn, active;struct arm_smmu_device *smmu = dev;@@ -1215,7 +1464,7 @@ static irqreturn_t arm_smmu_gerror_handler(int irq, void *dev) active = gerror ^ gerrorn;if (!(active &GERROR_ERR_MASK )) -return IRQ_NONE ;/* No errors pending */ +return ;/* No errors pending */ dev_warn(smmu->dev, "unexpected global error reported (0x%08x), this could be serious\n",@@ -1248,26 +1497,42 @@ static irqreturn_t arm_smmu_gerror_handler(int irq, void *dev)arm_smmu_cmdq_skip_err (smmu);writel (gerror, smmu->base +ARM_SMMU_GERRORN ); -return IRQ_HANDLED ; } -static irqreturn_t arm_smmu_combined_irq_thread (int irq,void *dev) +static void arm_smmu_combined_irq_handler (int irq,void *dev, +struct cpu_user_regs *regs) +{ +struct arm_smmu_device *smmu = (struct arm_smmu_device *)dev; + +arm_smmu_gerror_handler (irq, dev, regs); + +tasklet_schedule (&(smmu->combined_irq_tasklet)); +} + +static void arm_smmu_combined_irq_thread (void *dev) {struct arm_smmu_device *smmu = dev; -arm_smmu_evtq_thread (irq, dev); +arm_smmu_evtq_thread (dev);if (smmu->features &ARM_SMMU_FEAT_PRI ) -arm_smmu_priq_thread (irq, dev); - -return IRQ_HANDLED ; +arm_smmu_priq_thread (dev); } -static irqreturn_t arm_smmu_combined_irq_handler (int irq,void *dev) +static void arm_smmu_evtq_irq_tasklet (int irq,void *dev, +struct cpu_user_regs *regs) { -arm_smmu_gerror_handler (irq, dev); -return IRQ_WAKE_THREAD ; +struct arm_smmu_device *smmu = (struct arm_smmu_device *)dev; + +tasklet_schedule (&(smmu->evtq_irq_tasklet)); } +static void arm_smmu_priq_irq_tasklet (int irq,void *dev, +struct cpu_user_regs *regs) +{ +struct arm_smmu_device *smmu = (struct arm_smmu_device *)dev; + +tasklet_schedule (&(smmu->priq_irq_tasklet)); +}/* IO_PGTABLE API */ static void arm_smmu_tlb_inv_context (void *cookie)@@ -1354,27 +1619,69 @@ static void arm_smmu_domain_free(struct iommu_domain *domain) }static int arm_smmu_domain_finalise_s2 (struct arm_smmu_domain *smmu_domain, -struct arm_smmu_master *master, -struct io_pgtable_cfg *pgtbl_cfg) +struct arm_smmu_master *master) {int vmid; +u64 reg;struct arm_smmu_device *smmu = smmu_domain->smmu;struct arm_smmu_s2_cfg *cfg = &smmu_domain->s2_cfg; +/* VTCR */ + reg =VTCR_RES1 |VTCR_SH0_IS |VTCR_IRGN0_WBWA |VTCR_ORGN0_WBWA ; + +switch (PAGE_SIZE ) { +case SZ_4K : + reg |=VTCR_TG0_4K ; +break ; +case SZ_16K : + reg |=VTCR_TG0_16K ; +break ; +case SZ_64K : + reg |=VTCR_TG0_4K ; +break ; + } + +switch (smmu->oas) { +case 32 : + reg |=VTCR_PS (_AC (0x0 ,ULL )); +break ; +case 36 : + reg |=VTCR_PS (_AC (0x1 ,ULL )); +break ; +case 40 : + reg |=VTCR_PS (_AC (0x2 ,ULL )); +break ; +case 42 : + reg |=VTCR_PS (_AC (0x3 ,ULL )); +break ; +case 44 : + reg |=VTCR_PS (_AC (0x4 ,ULL )); +break ; +case 48 : + reg |=VTCR_PS (_AC (0x5 ,ULL )); +break ; +case 52 : + reg |=VTCR_PS (_AC (0x6 ,ULL )); +break ; + } + + reg |=VTCR_T0SZ (64ULL - smmu->ias); + reg |=VTCR_SL0 (0x2 ); + reg |=VTCR_VS ; + + cfg->vtcr = reg; + vmid =arm_smmu_bitmap_alloc (smmu->vmid_map, smmu->vmid_bits);if (vmid <0 )return vmid; + cfg->vmid = (u16 )vmid; + + cfg->vttbr =page_to_maddr (cfg->domain->arch.p2m.root); + +printk (XENLOG_DEBUG +"SMMUv3: d%u: vmid 0x%x vtcr 0x%" PRIpaddr" p2maddr 0x%" PRIpaddr"\n" , + cfg->domain->domain_id, cfg->vmid, cfg->vtcr, cfg->vttbr); - vtcr = &pgtbl_cfg->arm_lpae_s2_cfg.vtcr; - cfg->vmid = (u16 )vmid; - cfg->vttbr = pgtbl_cfg->arm_lpae_s2_cfg.vttbr; - cfg->vtcr =FIELD_PREP (STRTAB_STE_2_VTCR_S2T0SZ , vtcr->tsz) | -FIELD_PREP (STRTAB_STE_2_VTCR_S2SL0 , vtcr->sl) | -FIELD_PREP (STRTAB_STE_2_VTCR_S2IR0 , vtcr->irgn) | -FIELD_PREP (STRTAB_STE_2_VTCR_S2OR0 , vtcr->orgn) | -FIELD_PREP (STRTAB_STE_2_VTCR_S2SH0 , vtcr->sh) | -FIELD_PREP (STRTAB_STE_2_VTCR_S2TG , vtcr->tg) | -FIELD_PREP (STRTAB_STE_2_VTCR_S2PS , vtcr->ps);return 0 ; }@@ -1382,28 +1689,12 @@ static int arm_smmu_domain_finalise(struct iommu_domain *domain, struct arm_smmu_master *master) {int ret; -unsigned long ias, oas; -int (*finalise_stage_fn)(struct arm_smmu_domain *, -struct arm_smmu_master *, -struct io_pgtable_cfg *);struct arm_smmu_domain *smmu_domain =to_smmu_domain (domain); -struct arm_smmu_device *smmu = smmu_domain->smmu;/* Restrict the stage to what we can actually support */ smmu_domain->stage =ARM_SMMU_DOMAIN_S2 ; -switch (smmu_domain->stage) { -case ARM_SMMU_DOMAIN_NESTED : -case ARM_SMMU_DOMAIN_S2 : - ias = smmu->ias; - oas = smmu->oas; - finalise_stage_fn = arm_smmu_domain_finalise_s2; -break ; -default : -return -EINVAL ; - } - - ret =finalise_stage_fn (smmu_domain, master, &pgtbl_cfg); + ret =arm_smmu_domain_finalise_s2 (smmu_domain, master);if (ret <0 ) {return ret; }
xen/drivers/passthrough/arm/smmu-v3.c static int arm_smmu_init_one_queue(struct arm_smmu_device *smmu, · line 1850
Quoted 8 lines
xen/drivers/passthrough/arm/smmu-v3.c +2
-1@@ -1553,7 +1844,8 @@ static int arm_smmu_init_one_queue(struct arm_smmu_device *smmu,return -ENOMEM ; } -if (!WARN_ON (q->base_dma & (qsz -1 ))) { +WARN_ON (q->base_dma & (qsz -1 )); +if (unlikely (q->base_dma & (qsz -1 ))) {dev_info (smmu->dev,"allocated %u entries for %s\n" ,1 << q->llq.max_n_shift, name);
We don't need both the WARNING and the dev_info. you could turn the dev_warn / XENLOG_WARNING.
Quoted 104 lines
xen/drivers/passthrough/arm/smmu-v3.c +23
-25}@@ -1758,9 +2050,7 @@ static void arm_smmu_setup_unique_irqs(struct arm_smmu_device *smmu)/* Request interrupt lines */ irq = smmu->evtq.q.irq;if (irq) { - ret =devm_request_threaded_irq (smmu->dev, irq,NULL , - arm_smmu_evtq_thread, -IRQF_ONESHOT , + ret =request_irq (irq,0 , arm_smmu_evtq_irq_tasklet,"arm-smmu-v3-evtq" , smmu);if (ret <0 )dev_warn (smmu->dev,"failed to enable evtq irq\n" );@@ -1770,8 +2060,8 @@ static void arm_smmu_setup_unique_irqs(struct arm_smmu_device *smmu) irq = smmu->gerr_irq;if (irq) { - ret =devm_request_irq (smmu->dev, irq, arm_smmu_gerror_handler, -0 ,"arm-smmu-v3-gerror" , smmu); + ret =request_irq (irq,0 , arm_smmu_gerror_handler, +"arm-smmu-v3-gerror" , smmu);if (ret <0 )dev_warn (smmu->dev,"failed to enable gerror irq\n" ); }else {@@ -1781,11 +2071,8 @@ static void arm_smmu_setup_unique_irqs(struct arm_smmu_device *smmu)if (smmu->features &ARM_SMMU_FEAT_PRI ) { irq = smmu->priq.q.irq;if (irq) { - ret =devm_request_threaded_irq (smmu->dev, irq,NULL , - arm_smmu_priq_thread, -IRQF_ONESHOT , -"arm-smmu-v3-priq" , - smmu); + ret =request_irq (irq,0 , arm_smmu_priq_irq_tasklet, +"arm-smmu-v3-priq" , smmu);if (ret <0 )dev_warn (smmu->dev,"failed to enable priq irq\n" );@@ -1814,11 +2101,8 @@ static int arm_smmu_setup_irqs(struct arm_smmu_device *smmu)* Cavium ThunderX2 implementation doesn't support unique irq * lines. Use a single irq line for all the SMMUv3 interrupts. */ - ret =devm_request_threaded_irq (smmu->dev, irq, - arm_smmu_combined_irq_handler, - arm_smmu_combined_irq_thread, -IRQF_ONESHOT , -"arm-smmu-v3-combined-irq" , smmu); + ret =request_irq (irq,0 , arm_smmu_combined_irq_handler, +"arm-smmu-v3-combined-irq" , smmu);if (ret <0 )dev_warn (smmu->dev,"failed to enable combined irq\n" ); }else @@ -1857,7 +2141,7 @@ static int arm_smmu_device_reset(struct arm_smmu_device *smmu, bool bypass) reg =readl_relaxed (smmu->base +ARM_SMMU_CR0 );if (reg &CR0_SMMUEN ) {dev_warn (smmu->dev,"SMMU currently enabled! Resetting...\n" ); -WARN_ON (is_kdump_kernel () && !disable_bypass); +WARN_ON (!disable_bypass);arm_smmu_update_gbpa (smmu,GBPA_ABORT ,0 ); }@@ -1952,8 +2236,11 @@ static int arm_smmu_device_reset(struct arm_smmu_device *smmu, bool bypass)return ret; } -if (is_kdump_kernel ()) - enables &= ~(CR0_EVTQEN |CR0_PRIQEN ); +/* Initialize tasklets for threaded IRQs*/ +tasklet_init (&smmu->evtq_irq_tasklet, arm_smmu_evtq_thread, smmu); +tasklet_init (&smmu->priq_irq_tasklet, arm_smmu_priq_thread, smmu); +tasklet_init (&smmu->combined_irq_tasklet, arm_smmu_combined_irq_thread, + smmu);/* Enable the SMMU interface, or ensure bypass */ if (!bypass || disable_bypass) {@@ -2195,7 +2482,7 @@ static inline int arm_smmu_device_acpi_probe(struct platform_device *pdev,static int arm_smmu_device_dt_probe (struct platform_device *pdev,struct arm_smmu_device *smmu) { -struct device *dev = &pdev->dev; +struct device *dev = pdev;u32 cells;int ret = -EINVAL ;@@ -2219,130 +2506,449 @@ static unsigned long arm_smmu_resource_size(struct arm_smmu_device *smmu)return SZ_128K ; } +/* Start of Xen specific code. */ static int arm_smmu_device_probe (struct platform_device *pdev) { -int irq, ret; -struct resource *res; -resource_size_t ioaddr; -struct arm_smmu_device *smmu; -struct device *dev = &pdev->dev; -bool bypass; +int irq, ret; +paddr_t ioaddr, iosize; +struct arm_smmu_device *smmu; +bool bypass; + + smmu =devm_kzalloc (dev,sizeof (*smmu),GFP_KERNEL ); +if ( !smmu ) + {
xen/drivers/passthrough/arm/smmu-v3.c static unsigned long arm_smmu_resource_size(struct arm_smmu_device *smmu) · line 2529
Quoted 10 lines
xen/drivers/passthrough/arm/smmu-v3.c +10
-0+dev_err (pdev,"failed to allocate arm_smmu_device\n" ); +return -ENOMEM ; + } + smmu->dev = pdev; + +if ( pdev->of_node ) + { + ret =arm_smmu_device_dt_probe (pdev, smmu); + }else + {
coding style
Quoted 12 lines
xen/drivers/passthrough/arm/smmu-v3.c +12
-0+ ret =arm_smmu_device_acpi_probe (pdev, smmu); +if ( ret == -ENODEV ) +return ret; + } + +/* Set bypass mode according to firmware probing result */ + bypass = !!ret; + +/* Base address */ + ret =dt_device_get_address (dev_to_dt (pdev),0 , &ioaddr, &iosize); +if ( ret ) +return -ENODEV ;
xen/drivers/passthrough/arm/smmu-v3.c static unsigned long arm_smmu_resource_size(struct arm_smmu_device *smmu) · line 2551
Quoted 10 lines
xen/drivers/passthrough/arm/smmu-v3.c +10
-0+ +if ( iosize <arm_smmu_resource_size (smmu) ) + { +dev_err (pdev,"MMIO region too small (%lx)\n" , iosize); +return -EINVAL ; + } + +/* +* Don't map the IMPLEMENTATION DEFINED regions, since they may contain +* the PMCG registers which are reserved by the PMU driver.
Does this apply to Xen too?
Quoted 62 lines
xen/drivers/passthrough/arm/smmu-v3.c +62
-0+*/ + smmu->base =ioremap_nocache (ioaddr, iosize); +if (IS_ERR (smmu->base) ) +return PTR_ERR (smmu->base); + +if ( iosize >SZ_64K ) + { + smmu->page1 =ioremap_nocache (ioaddr +SZ_64K ,ARM_SMMU_REG_SZ ); +if (IS_ERR (smmu->page1) ) +return PTR_ERR (smmu->page1); + } +else + { + smmu->page1 = smmu->base; + } + +/* Interrupt lines */ + + irq =platform_get_irq_byname_optional (pdev,"combined" ); +if ( irq >0 ) + smmu->combined_irq = irq; +else + { + irq =platform_get_irq_byname_optional (pdev,"eventq" ); +if ( irq >0 ) + smmu->evtq.q.irq = irq; + + irq =platform_get_irq_byname_optional (pdev,"priq" ); +if ( irq >0 ) + smmu->priq.q.irq = irq; + + irq =platform_get_irq_byname_optional (pdev,"gerror" ); +if ( irq >0 ) + smmu->gerr_irq = irq; + } +/* Probe the h/w */ + ret =arm_smmu_device_hw_probe (smmu); +if ( ret ) +return ret; + +/* Initialise in-memory data structures */ + ret =arm_smmu_init_structures (smmu); +if ( ret ) +return ret; + +/* Reset the device */ + ret =arm_smmu_device_reset (smmu, bypass); +if ( ret ) +return ret; + +/* +* Keep a list of all probed devices. This will be used to query +* the smmu devices based on the fwnode. +*/ +INIT_LIST_HEAD (&smmu->devices); + +spin_lock (&arm_smmu_devices_lock); +list_add (&smmu->devices, &arm_smmu_devices); +spin_unlock (&arm_smmu_devices_lock); + +return 0 ; +}
Hello Stefano,
Quoted 21 lines · Stefano Stabellini
Quoted 17 lines · Rahul Singh
Add support for ARM architected SMMUv3 implementation. It is based on the Linux SMMUv3 driver.
Major differences with regard to Linux driver are as follows:
1. Only Stage-2 translation is supported as compared to the Linux driver that supports both Stage-1 and Stage-2 translations.
2. Use P2M page table instead of creating one as SMMUv3 has the capability to share the page tables with the CPU.
3. Tasklets are used in place of threaded IRQ's in Linux for event queue and priority queue IRQ handling.
4. Latest version of the Linux SMMUv3 code implements the commands queue access functions based on atomic operations implemented in Linux. Atomic functions used by the commands queue access functions are not implemented in XEN therefore we decided to port the earlier version of the code. Once the proper atomic operations will be available in XEN the driver can be updated.
5. Driver is currently supported as Tech Preview.
Quoted 1 line
This patch is big and was difficult to review, nonetheless I tried :-)
Thanks again for reviewing the code.
Quoted 149 lines
That said, the code is self-contained, marked as Tech Preview, and not at risk of making other things unstable, so it is low risk to accept it.
Comments below.
Quoted 128 lines
Signed-off-by Rahul Singh <[email protected]>---
MAINTAINERS | 6 + SUPPORT.md | 1 + xen/drivers/passthrough/Kconfig | 10 + xen/drivers/passthrough/arm/Makefile | 1 + xen/drivers/passthrough/arm/smmu-v3.c | 986 +++++++++++++++++++++----- 5 files changed, 814 insertions(+), 190 deletions(-)MAINTAINERS +0
-0@@ -249,6 +249,12 @@ F: xen/include/asm-arm/F: xen/include/public/arch-arm/
F:xen/include/public/arch-arm.hMAINTAINERS +6
-0+ARM SMMUv3 +M: Bertrand Marquis <[email protected]> +M: Rahul Singh <[email protected]> +S: Supported +F: xen/drivers/passthrough/arm/smmu-v3.c +Change Log
M: Paul Durrant <[email protected]>
R: Community Manager <[email protected]>SUPPORT.md +1
-0@@ -68,6 +68,7 @@ For the Cortex A57 r0p0 - r1p1, see Errata 832075. Status, ARM SMMUv1: Supported, not security supported Status, ARM SMMUv2: Supported, not security supported Status, Renesas IPMMU-VMSA: Supported, not security supported + Status, ARM SMMUv3: Tech Preview ### ARM/GICv3 ITSxen/drivers/passthrough/Kconfig +10
-0@@ -13,6 +13,16 @@ config ARM_SMMU Say Y here if your SoC includes an IOMMU device implementing the ARM SMMU architecture. +config ARM_SMMU_V3 + bool "ARM Ltd. System MMU Version 3 (SMMUv3) Support" if EXPERT + depends on ARM_64 + ---help--- + Support for implementations of the ARM System MMU architecture + version 3. + + Say Y here if your system includes an IOMMU device implementing + the ARM SMMUv3 architecture. +config
IPMMU_VMSA
bool "Renesas IPMMU-VMSA found in R-Car Gen3 SoCs" depends onARM_64xen/drivers/passthrough/arm/Makefile +0
-0@@ -1,3 +1,4 @@obj-y += iommu.o iommu_helpers.o iommu_fwspec.o
obj-$(CONFIG_ARM_SMMU) += smmu.o
obj-$(CONFIG_IPMMU_VMSA) += ipmmu-vmsa.oxen/drivers/passthrough/arm/Makefile +1
-0+obj-$(CONFIG_ARM_SMMU_V3) += smmu-v3.oxen/drivers/passthrough/arm/smmu-v3.c +47
-3@@ -2,36 +2,280 @@/* * IOMMU API for ARM architected SMMUv3 implementations. * -* Copyright (C) 2015 ARM Limited +* Based on Linux's SMMUv3 driver: +* drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c +* commit: 951cbbc386ff01b50da4f46387e994e81d9ab431 +* and Xen's SMMU driver: +* xen/drivers/passthrough/arm/smmu.c * -* Author: Will Deacon <[email protected]> +* Copyright (C) 2015 ARM Limited Will Deacon <[email protected]> * -* This driver is powered by bad coffee and bombay mix. +* Copyright (C) 2020 Arm Ltd. +* +* This program is free software; you can redistribute it and/or modify +* it under the terms of the GNU General Public License version 2 as +* published by the Free Software Foundation. +* +* This program is distributed in the hope that it will be useful, +* but WITHOUT ANY WARRANTY; without even the implied warranty of +* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +* GNU General Public License for more details. +* +* You should have received a copy of the GNU General Public License +* along with this program. If not, see <http://www.gnu.org/licenses/>. +* +*/ + +#include <xen/acpi.h> +#include <xen/config.h> +#include <xen/delay.h> +#include <xen/errno.h> +#include <xen/err.h> +#include <xen/irq.h> +#include <xen/lib.h> +#include <xen/list.h> +#include <xen/mm.h> +#include <xen/rbtree.h> +#include <xen/sched.h> +#include <xen/sizes.h> +#include <xen/vmap.h> +#include <asm/atomic.h> +#include <asm/device.h> +#include <asm/io.h> +#include <asm/platform.h> +#include <asm/iommu_fwspec.h> + +/* Linux compatibility functions. */ +typedef paddr_t dma_addr_t ; +typedef unsigned int gfp_t ; + +#define platform_device device +xen/drivers/passthrough/arm/smmu-v3.c line 63
Quoted 10 lines
xen/drivers/passthrough/arm/smmu-v3.c +10
-0+#define GFP_KERNEL 0 + +/* Alias to Xen device tree helpers */ +#define device_node dt_device_node +#define of_phandle_args dt_phandle_args +#define of_device_id dt_device_match +#define of_match_node dt_match_node +#define of_property_read_u32 (np, pname, out) (!dt_property_read_u32(np, pname, out)) +#define of_property_read_bool dt_property_read_bool +#define of_parse_phandle_with_args dt_parse_phandle_with_argsGiven all the changes to the file by the previous patches we are basically fully (or almost fully) adapting this code to Xen.
Quoted 2 lines
So at that point I wonder if we should just as well make these changes (e.g. s/of_phandle_args/dt_phandle_args/g) to the code too.
Yes, you are right that is why in the first version of the patch I modifying the code to make it fully XEN compatible. But in this patch series I use the Linux compatibility function to have Linux code unmodified.I also prefer to make changes (s/of_phandle_args/dt_phandle_args/g).
Quoted 7 lines
Julien, Rahul, what do you guys think?
xen/drivers/passthrough/arm/smmu-v3.c line 65
Quoted 1 line
xen/drivers/passthrough/arm/smmu-v3.c +1
-0+/* Alias to Xen lock functions */ I think this deserves at least one statement to explain why it is OK.
Ack. I Will add the comment.
Quoted 35 lines
Also, a similar comment to the one above: maybe we should add a couple of more preparation patches to s/mutex/spinlock/g and change the time and allocation functions too.
Quoted 29 lines
xen/drivers/passthrough/arm/smmu-v3.c +29
-0+#define mutex spinlock +#define mutex_init spin_lock_init +#define mutex_lock spin_lock +#define mutex_unlock spin_unlock + +/* Alias to Xen time functions */ +#define ktime_t s_time_t +#define ktime_get () (NOW()) +#define ktime_add_us (t,i) (t + MICROSECS(i)) +#define ktime_compare (t,i) (t > (i)) + +/* Alias to Xen allocation helpers */ +#define kzalloc (size, flags) _xzalloc(size, sizeof(void *)) +#define kfree xfree +#define devm_kzalloc (dev, size, flags) _xzalloc(size, sizeof(void *)) + +/* Device logger functions */ +#define dev_name (dev) dt_node_full_name(dev->of_node) +#define dev_dbg (dev, fmt, ...) \ + printk(XENLOG_DEBUG "SMMUv3: %s: " fmt, dev_name(dev), ## __VA_ARGS__) +#define dev_notice (dev, fmt, ...) \ + printk(XENLOG_INFO "SMMUv3: %s: " fmt, dev_name(dev), ## __VA_ARGS__) +#define dev_warn (dev, fmt, ...) \ + printk(XENLOG_WARNING "SMMUv3: %s: " fmt, dev_name(dev), ## __VA_ARGS__) +#define dev_err (dev, fmt, ...) \ + printk(XENLOG_ERR "SMMUv3: %s: " fmt, dev_name(dev), ## __VA_ARGS__) +#define dev_info (dev, fmt, ...) \ + printk(XENLOG_INFO "SMMUv3: %s: " fmt, dev_name(dev), ## __VA_ARGS__) +#define dev_err_ratelimited (dev, fmt, ...) \
Quoted 8 lines
xen/drivers/passthrough/arm/smmu-v3.c line 99
Quoted 5 lines
xen/drivers/passthrough/arm/smmu-v3.c +5
-0+ printk(XENLOG_ERR "SMMUv3: %s: " fmt, dev_name(dev), ## __VA_ARGS__) + +/* +* Periodically poll an address and wait between reads in us until a +* condition is met or a timeout occurs. It would be good to add a statement to point out what the return value is going to be.
Ack.
Quoted 18 lines
Quoted 18 lines
xen/drivers/passthrough/arm/smmu-v3.c +16
-0+*/ +#define readx_poll_timeout (op, addr, val, cond, sleep_us, timeout_us) \ +({ \ + s_time_t deadline = NOW() + MICROSECS(timeout_us); \ + for (;;) { \ + (val) = op(addr); \ + if (cond) \ + break; \ + if (NOW() > deadline) { \ + (val) = op(addr); \ + break; \ + } \ + udelay(sleep_us); \ + } \ + (cond) ? 0 : -ETIMEDOUT; \ +})
Quoted 8 lines
xen/drivers/passthrough/arm/smmu-v3.c line 121
Quoted 6 lines
xen/drivers/passthrough/arm/smmu-v3.c +6
-0+ +#define readl_relaxed_poll_timeout (addr, val, cond, delay_us, timeout_us) \ + readx_poll_timeout(readl_relaxed, addr, val, cond, delay_us, timeout_us) + +#define FIELD_PREP (_mask, _val) \ + (((typeof(_mask))(_val) << (__builtin_ffsll(_mask) - 1)) & (_mask))Let's add the definition of ffsll to
bitops.h
Ok.
Quoted 11 lines
xen/drivers/passthrough/arm/smmu-v3.c line 129
Quoted 9 lines
xen/drivers/passthrough/arm/smmu-v3.c +7
-0+#define FIELD_GET (_mask, _reg) \ + (typeof(_mask))(((_reg) & (_mask)) >> (__builtin_ffsll(_mask) - 1)) + +#define WRITE_ONCE (x, val) \ +do { \ + *(volatile typeof(x) *)&(x) = (val); \ +} while (0)maybe we should define this in
xen/include/xen/lib.h
Ok.
Quoted 9 lines
xen/drivers/passthrough/arm/smmu-v3.c line 133
Quoted 6 lines
xen/drivers/passthrough/arm/smmu-v3.c +4
-0+ +/* Xen: Stub out DMA domain related functions */ +#define iommu_get_dma_cookie (dom) 0 +#define iommu_put_dma_cookie (dom)Shouldn't we remove any call to
iommu_get_dma_cookieandiommu_put_dma_cookiein one of the previous patches?
Ack.
Quoted 62 lines
Quoted 62 lines
xen/drivers/passthrough/arm/smmu-v3.c +59
-0+/* +* Helpers for DMA allocation. Just the function name is reused for +* porting code, these allocation are not managed allocations */ +static void *dmam_alloc_coherent (struct device *dev,size_t size, +paddr_t *dma_handle,gfp_t gfp) +{ +void *vaddr; +unsigned long alignment = size; + +/* +* _xzalloc requires that the (align & (align -1)) = 0. Most of the +* allocations in SMMU code should send the right value for size. In +* case this is not true print a warning and align to the size of a +* (void *) +*/ +if ( size & (size -1 ) ) + { +printk (XENLOG_WARNING "SMMUv3: Fixing alignment for the DMA buffer\n" ); + alignment =sizeof (void *); + } + + vaddr =_xzalloc (size, alignment); +if ( !vaddr ) + { +printk (XENLOG_ERR "SMMUv3: DMA allocation failed\n" ); +return NULL ; + } + + *dma_handle =virt_to_maddr (vaddr); + +return vaddr; +} + +/* Xen: Type definitions for iommu_domain */ +#define IOMMU_DOMAIN_UNMANAGED 0 +#define IOMMU_DOMAIN_DMA 1 +#define IOMMU_DOMAIN_IDENTITY 2 + +/* Xen specific code. */ +struct iommu_domain { +/* Runtime SMMU configuration for this iommu_domain */ +atomic_t ref; +/* +* Used to link iommu_domain contexts for a same domain. +* There is at least one per-SMMU to used by the domain. +*/ +struct list_head list; +}; + +/* Describes information required for a Xen domain */ +struct arm_smmu_xen_domain { +spinlock_t lock; + +/* List of iommu domains associated to this domain */ +struct list_head contexts; +}; + +/* +* Information about each device stored in dev->archdata.iommu
Quoted 9 lines
xen/drivers/passthrough/arm/smmu-v3.c line 200
Quoted 6 lines
xen/drivers/passthrough/arm/smmu-v3.c +6
-0+* The dev->archdata.iommu stores the iommu_domain (runtime configuration of +* the SMMU). +*/ +struct arm_smmu_xen_device { +struct iommu_domain *domain; +};Do we need both struct
arm_smmu_xen_deviceand structiommu_domain?
No we don’t need both. I will remove the struct arm_smmu_xen_device.
Quoted 46 lines
Quoted 46 lines
xen/drivers/passthrough/arm/smmu-v3.c +45
-0+/* Keep a list of devices associated with this driver */ +static DEFINE_SPINLOCK (arm_smmu_devices_lock); +static LIST_HEAD (arm_smmu_devices); + + +static inline void *dev_iommu_priv_get (struct device *dev) +{ +struct iommu_fwspec *fwspec =dev_iommu_fwspec_get (dev); + +return fwspec && fwspec->iommu_priv ? fwspec->iommu_priv :NULL ; +} + +static inline void dev_iommu_priv_set (struct device *dev,void *priv) +{ +struct iommu_fwspec *fwspec =dev_iommu_fwspec_get (dev); + + fwspec->iommu_priv = priv; +} + +int dt_property_match_string (const struct dt_device_node *np, +const char *propname,const char *string) +{ +const struct dt_property *dtprop =dt_find_property (np, propname,NULL ); +size_t l; +int i; +const char *p, *end; + +if ( !dtprop ) +return -EINVAL ; + +if ( !dtprop->value ) +return -ENODATA ; + + p = dtprop->value; + end = p + dtprop->length; + +for ( i =0 ; p < end; i++, p += l ) + { + l =strnlen (p, end - p) +1 ; + +if ( p + l > end ) +return -EILSEQ ; + +if (strcmp (string, p) ==0 ) +return i;/* Found it; return index */
Quoted 8 lines
xen/drivers/passthrough/arm/smmu-v3.c line 247
Quoted 4 lines
xen/drivers/passthrough/arm/smmu-v3.c +4
-0+ } + +return -ENODATA ; +}I think you should either use
dt_property_read_stringor move the implementation ofdt_property_match_stringtoxen/common/device_tree.c(in which case I suggest to do it in a separate patch.)
I will prefer to move the code to xen/common/device_tree.c as it might help in future to use.
Quoted 86 lines
I'd just use
dt_property_read_string.Quoted 80 lines
xen/drivers/passthrough/arm/smmu-v3.c +34
-25+static int platform_get_irq_byname_optional (struct device *dev, +const char *name) +{ +int index, ret; +struct dt_device_node *np =dev_to_dt (dev); + +if (unlikely (!name) ) +return -EINVAL ; + + index =dt_property_match_string (np,"interrupt-names" , name); +if ( index <0 ) + { +dev_info (dev,"IRQ %s not found\n" , name); +return index; + } -#include <linux/acpi.h> -#include <linux/acpi_iort.h> -#include <linux/bitfield.h> -#include <linux/bitops.h> -#include <linux/crash_dump.h> -#include <linux/delay.h> -#include <linux/dma-iommu.h> -#include <linux/err.h> -#include <linux/interrupt.h> -#include <linux/io-pgtable.h> -#include <linux/iommu.h> -#include <linux/iopoll.h> -#include <linux/module.h> -#include <linux/msi.h> -#include <linux/of.h> -#include <linux/of_address.h> -#include <linux/of_iommu.h> -#include <linux/of_platform.h> -#include <linux/pci.h> -#include <linux/pci-ats.h> -#include <linux/platform_device.h> - -#include <linux/amba/bus.h> + ret =platform_get_irq (np, index); +if ( ret <0 ) + { +dev_err (dev,"failed to get irq index %d\n" , index); +return -ENODEV ; + } + +return ret; +} + +/* Start of Linux SMMUv3 code */ /* MMIO registers */ #define ARM_SMMU_IDR0 0x0@@ -507,6 +751,7 @@ struct arm_smmu_s2_cfg {u16 vmid;u64 vttbr;u64 vtcr; +struct domain *domain; };struct arm_smmu_strtab_cfg {@@ -567,8 +812,13 @@ struct arm_smmu_device {struct arm_smmu_strtab_cfg strtab_cfg; -/* IOMMU core code handle */ -struct iommu_device iommu; +/* Need to keep a list of SMMU devices */ +struct list_head devices; + +/* Tasklets for handling evts/faults and pci page request IRQs*/ +struct tasklet evtq_irq_tasklet; +struct tasklet priq_irq_tasklet; +struct tasklet combined_irq_tasklet; };/* SMMU private data for each master */ @@ -1110,7 +1360,7 @@ static int arm_smmu_init_l2_strtab(struct arm_smmu_device *smmu, u32 sid) }
Quoted 6 lines
xen/drivers/passthrough/arm/smmu-v3.c static int arm_smmu_init_l2_strtab(struct arm_smmu_device *smmu, u32 sid) · line 1363
Quoted 3 lines
xen/drivers/passthrough/arm/smmu-v3.c +1
-1/* IRQ and event handlers */ -static irqreturn_t arm_smmu_evtq_thread (int irq,void *dev) +static void arm_smmu_evtq_thread (void *dev)I think we shouldn't call it a thread given that's a tasklet. We should rename the function or it will be confusing
Ok.
Quoted 16 lines
Quoted 16 lines
xen/drivers/passthrough/arm/smmu-v3.c +0
-1{int i;struct arm_smmu_device *smmu = dev;@@ -1140,7 +1390,6 @@ static irqreturn_t arm_smmu_evtq_thread(int irq, void *dev)/* Sync our overflow flag, as we believe we're up to speed */ llq->cons =Q_OVF (llq->prod) |Q_WRP (llq, llq->cons) |Q_IDX (llq, llq->cons); -return IRQ_HANDLED ; }static void arm_smmu_handle_ppr (struct arm_smmu_device *smmu,u64 *evt)@@ -1181,7 +1430,7 @@ static void arm_smmu_handle_ppr(struct arm_smmu_device *smmu, u64 *evt) } }
Quoted 4 lines
xen/drivers/passthrough/arm/smmu-v3.c static void arm_smmu_handle_ppr(struct arm_smmu_device *smmu, u64 *evt) · line 1433
Quoted 2 lines
xen/drivers/passthrough/arm/smmu-v3.c +1
-1-static irqreturn_t arm_smmu_priq_thread (int irq,void *dev) +static void arm_smmu_priq_thread (void *dev)same here
Ok.
Quoted 197 lines
Quoted 197 lines
xen/drivers/passthrough/arm/smmu-v3.c +83
-41{struct arm_smmu_device *smmu = dev;struct arm_smmu_queue *q = &smmu->priq.q;@@ -1200,12 +1449,12 @@ static irqreturn_t arm_smmu_priq_thread(int irq, void *dev) llq->cons =Q_OVF (llq->prod) |Q_WRP (llq, llq->cons) |Q_IDX (llq, llq->cons);queue_sync_cons_out (q); -return IRQ_HANDLED ; }static int arm_smmu_device_disable (struct arm_smmu_device *smmu); -static irqreturn_t arm_smmu_gerror_handler (int irq,void *dev) +static void arm_smmu_gerror_handler (int irq,void *dev, +struct cpu_user_regs *regs) {u32 gerror, gerrorn, active;struct arm_smmu_device *smmu = dev;@@ -1215,7 +1464,7 @@ static irqreturn_t arm_smmu_gerror_handler(int irq, void *dev) active = gerror ^ gerrorn;if (!(active &GERROR_ERR_MASK )) -return IRQ_NONE ;/* No errors pending */ +return ;/* No errors pending */ dev_warn(smmu->dev, "unexpected global error reported (0x%08x), this could be serious\n",@@ -1248,26 +1497,42 @@ static irqreturn_t arm_smmu_gerror_handler(int irq, void *dev)arm_smmu_cmdq_skip_err (smmu);writel (gerror, smmu->base +ARM_SMMU_GERRORN ); -return IRQ_HANDLED ; } -static irqreturn_t arm_smmu_combined_irq_thread (int irq,void *dev) +static void arm_smmu_combined_irq_handler (int irq,void *dev, +struct cpu_user_regs *regs) +{ +struct arm_smmu_device *smmu = (struct arm_smmu_device *)dev; + +arm_smmu_gerror_handler (irq, dev, regs); + +tasklet_schedule (&(smmu->combined_irq_tasklet)); +} + +static void arm_smmu_combined_irq_thread (void *dev) {struct arm_smmu_device *smmu = dev; -arm_smmu_evtq_thread (irq, dev); +arm_smmu_evtq_thread (dev);if (smmu->features &ARM_SMMU_FEAT_PRI ) -arm_smmu_priq_thread (irq, dev); - -return IRQ_HANDLED ; +arm_smmu_priq_thread (dev); } -static irqreturn_t arm_smmu_combined_irq_handler (int irq,void *dev) +static void arm_smmu_evtq_irq_tasklet (int irq,void *dev, +struct cpu_user_regs *regs) { -arm_smmu_gerror_handler (irq, dev); -return IRQ_WAKE_THREAD ; +struct arm_smmu_device *smmu = (struct arm_smmu_device *)dev; + +tasklet_schedule (&(smmu->evtq_irq_tasklet)); } +static void arm_smmu_priq_irq_tasklet (int irq,void *dev, +struct cpu_user_regs *regs) +{ +struct arm_smmu_device *smmu = (struct arm_smmu_device *)dev; + +tasklet_schedule (&(smmu->priq_irq_tasklet)); +}/* IO_PGTABLE API */ static void arm_smmu_tlb_inv_context (void *cookie)@@ -1354,27 +1619,69 @@ static void arm_smmu_domain_free(struct iommu_domain *domain) }static int arm_smmu_domain_finalise_s2 (struct arm_smmu_domain *smmu_domain, -struct arm_smmu_master *master, -struct io_pgtable_cfg *pgtbl_cfg) +struct arm_smmu_master *master) {int vmid; +u64 reg;struct arm_smmu_device *smmu = smmu_domain->smmu;struct arm_smmu_s2_cfg *cfg = &smmu_domain->s2_cfg; +/* VTCR */ + reg =VTCR_RES1 |VTCR_SH0_IS |VTCR_IRGN0_WBWA |VTCR_ORGN0_WBWA ; + +switch (PAGE_SIZE ) { +case SZ_4K : + reg |=VTCR_TG0_4K ; +break ; +case SZ_16K : + reg |=VTCR_TG0_16K ; +break ; +case SZ_64K : + reg |=VTCR_TG0_4K ; +break ; + } + +switch (smmu->oas) { +case 32 : + reg |=VTCR_PS (_AC (0x0 ,ULL )); +break ; +case 36 : + reg |=VTCR_PS (_AC (0x1 ,ULL )); +break ; +case 40 : + reg |=VTCR_PS (_AC (0x2 ,ULL )); +break ; +case 42 : + reg |=VTCR_PS (_AC (0x3 ,ULL )); +break ; +case 44 : + reg |=VTCR_PS (_AC (0x4 ,ULL )); +break ; +case 48 : + reg |=VTCR_PS (_AC (0x5 ,ULL )); +break ; +case 52 : + reg |=VTCR_PS (_AC (0x6 ,ULL )); +break ; + } + + reg |=VTCR_T0SZ (64ULL - smmu->ias); + reg |=VTCR_SL0 (0x2 ); + reg |=VTCR_VS ; + + cfg->vtcr = reg; + vmid =arm_smmu_bitmap_alloc (smmu->vmid_map, smmu->vmid_bits);if (vmid <0 )return vmid; + cfg->vmid = (u16 )vmid; + + cfg->vttbr =page_to_maddr (cfg->domain->arch.p2m.root); + +printk (XENLOG_DEBUG +"SMMUv3: d%u: vmid 0x%x vtcr 0x%" PRIpaddr" p2maddr 0x%" PRIpaddr"\n" , + cfg->domain->domain_id, cfg->vmid, cfg->vtcr, cfg->vttbr); - vtcr = &pgtbl_cfg->arm_lpae_s2_cfg.vtcr; - cfg->vmid = (u16 )vmid; - cfg->vttbr = pgtbl_cfg->arm_lpae_s2_cfg.vttbr; - cfg->vtcr =FIELD_PREP (STRTAB_STE_2_VTCR_S2T0SZ , vtcr->tsz) | -FIELD_PREP (STRTAB_STE_2_VTCR_S2SL0 , vtcr->sl) | -FIELD_PREP (STRTAB_STE_2_VTCR_S2IR0 , vtcr->irgn) | -FIELD_PREP (STRTAB_STE_2_VTCR_S2OR0 , vtcr->orgn) | -FIELD_PREP (STRTAB_STE_2_VTCR_S2SH0 , vtcr->sh) | -FIELD_PREP (STRTAB_STE_2_VTCR_S2TG , vtcr->tg) | -FIELD_PREP (STRTAB_STE_2_VTCR_S2PS , vtcr->ps);return 0 ; }@@ -1382,28 +1689,12 @@ static int arm_smmu_domain_finalise(struct iommu_domain *domain, struct arm_smmu_master *master) {int ret; -unsigned long ias, oas; -int (*finalise_stage_fn)(struct arm_smmu_domain *, -struct arm_smmu_master *, -struct io_pgtable_cfg *);struct arm_smmu_domain *smmu_domain =to_smmu_domain (domain); -struct arm_smmu_device *smmu = smmu_domain->smmu;/* Restrict the stage to what we can actually support */ smmu_domain->stage =ARM_SMMU_DOMAIN_S2 ; -switch (smmu_domain->stage) { -case ARM_SMMU_DOMAIN_NESTED : -case ARM_SMMU_DOMAIN_S2 : - ias = smmu->ias; - oas = smmu->oas; - finalise_stage_fn = arm_smmu_domain_finalise_s2; -break ; -default : -return -EINVAL ; - } - - ret =finalise_stage_fn (smmu_domain, master, &pgtbl_cfg); + ret =arm_smmu_domain_finalise_s2 (smmu_domain, master);if (ret <0 ) {return ret; }@@ -1553,7 +1844,8 @@ static int arm_smmu_init_one_queue(struct arm_smmu_device *smmu,return -ENOMEM ; }
Quoted 9 lines
xen/drivers/passthrough/arm/smmu-v3.c static int arm_smmu_init_one_queue(struct arm_smmu_device *smmu, · line 1850
Quoted 5 lines
xen/drivers/passthrough/arm/smmu-v3.c +2
-1-if (!WARN_ON (q->base_dma & (qsz -1 ))) { +WARN_ON (q->base_dma & (qsz -1 )); +if (unlikely (q->base_dma & (qsz -1 ))) {dev_info (smmu->dev,"allocated %u entries for %s\n" ,1 << q->llq.max_n_shift, name);We don't need both the WARNING and the
dev_info. you could turn thedev_warn/XENLOG_WARNING.
Ack.
Quoted 109 lines
Quoted 109 lines
xen/drivers/passthrough/arm/smmu-v3.c +27
-25}@@ -1758,9 +2050,7 @@ static void arm_smmu_setup_unique_irqs(struct arm_smmu_device *smmu)/* Request interrupt lines */ irq = smmu->evtq.q.irq;if (irq) { - ret =devm_request_threaded_irq (smmu->dev, irq,NULL , - arm_smmu_evtq_thread, -IRQF_ONESHOT , + ret =request_irq (irq,0 , arm_smmu_evtq_irq_tasklet,"arm-smmu-v3-evtq" , smmu);if (ret <0 )dev_warn (smmu->dev,"failed to enable evtq irq\n" );@@ -1770,8 +2060,8 @@ static void arm_smmu_setup_unique_irqs(struct arm_smmu_device *smmu) irq = smmu->gerr_irq;if (irq) { - ret =devm_request_irq (smmu->dev, irq, arm_smmu_gerror_handler, -0 ,"arm-smmu-v3-gerror" , smmu); + ret =request_irq (irq,0 , arm_smmu_gerror_handler, +"arm-smmu-v3-gerror" , smmu);if (ret <0 )dev_warn (smmu->dev,"failed to enable gerror irq\n" ); }else {@@ -1781,11 +2071,8 @@ static void arm_smmu_setup_unique_irqs(struct arm_smmu_device *smmu)if (smmu->features &ARM_SMMU_FEAT_PRI ) { irq = smmu->priq.q.irq;if (irq) { - ret =devm_request_threaded_irq (smmu->dev, irq,NULL , - arm_smmu_priq_thread, -IRQF_ONESHOT , -"arm-smmu-v3-priq" , - smmu); + ret =request_irq (irq,0 , arm_smmu_priq_irq_tasklet, +"arm-smmu-v3-priq" , smmu);if (ret <0 )dev_warn (smmu->dev,"failed to enable priq irq\n" );@@ -1814,11 +2101,8 @@ static int arm_smmu_setup_irqs(struct arm_smmu_device *smmu)* Cavium ThunderX2 implementation doesn't support unique irq * lines. Use a single irq line for all the SMMUv3 interrupts. */ - ret =devm_request_threaded_irq (smmu->dev, irq, - arm_smmu_combined_irq_handler, - arm_smmu_combined_irq_thread, -IRQF_ONESHOT , -"arm-smmu-v3-combined-irq" , smmu); + ret =request_irq (irq,0 , arm_smmu_combined_irq_handler, +"arm-smmu-v3-combined-irq" , smmu);if (ret <0 )dev_warn (smmu->dev,"failed to enable combined irq\n" ); }else @@ -1857,7 +2141,7 @@ static int arm_smmu_device_reset(struct arm_smmu_device *smmu, bool bypass) reg =readl_relaxed (smmu->base +ARM_SMMU_CR0 );if (reg &CR0_SMMUEN ) {dev_warn (smmu->dev,"SMMU currently enabled! Resetting...\n" ); -WARN_ON (is_kdump_kernel () && !disable_bypass); +WARN_ON (!disable_bypass);arm_smmu_update_gbpa (smmu,GBPA_ABORT ,0 ); }@@ -1952,8 +2236,11 @@ static int arm_smmu_device_reset(struct arm_smmu_device *smmu, bool bypass)return ret; } -if (is_kdump_kernel ()) - enables &= ~(CR0_EVTQEN |CR0_PRIQEN ); +/* Initialize tasklets for threaded IRQs*/ +tasklet_init (&smmu->evtq_irq_tasklet, arm_smmu_evtq_thread, smmu); +tasklet_init (&smmu->priq_irq_tasklet, arm_smmu_priq_thread, smmu); +tasklet_init (&smmu->combined_irq_tasklet, arm_smmu_combined_irq_thread, + smmu);/* Enable the SMMU interface, or ensure bypass */ if (!bypass || disable_bypass) {@@ -2195,7 +2482,7 @@ static inline int arm_smmu_device_acpi_probe(struct platform_device *pdev,static int arm_smmu_device_dt_probe (struct platform_device *pdev,struct arm_smmu_device *smmu) { -struct device *dev = &pdev->dev; +struct device *dev = pdev;u32 cells;int ret = -EINVAL ;@@ -2219,130 +2506,449 @@ static unsigned long arm_smmu_resource_size(struct arm_smmu_device *smmu)return SZ_128K ; } +/* Start of Xen specific code. */ static int arm_smmu_device_probe (struct platform_device *pdev) { -int irq, ret; -struct resource *res; -resource_size_t ioaddr; -struct arm_smmu_device *smmu; -struct device *dev = &pdev->dev; -bool bypass; +int irq, ret; +paddr_t ioaddr, iosize; +struct arm_smmu_device *smmu; +bool bypass; + + smmu =devm_kzalloc (dev,sizeof (*smmu),GFP_KERNEL ); +if ( !smmu ) + { +dev_err (pdev,"failed to allocate arm_smmu_device\n" ); +return -ENOMEM ; + } + smmu->dev = pdev;
Quoted 8 lines
xen/drivers/passthrough/arm/smmu-v3.c static unsigned long arm_smmu_resource_size(struct arm_smmu_device *smmu) · line 2529
Quoted 6 lines
xen/drivers/passthrough/arm/smmu-v3.c +6
-0+ +if ( pdev->of_node ) + { + ret =arm_smmu_device_dt_probe (pdev, smmu); + }else + {coding style
Ack.
Quoted 18 lines
Quoted 18 lines
xen/drivers/passthrough/arm/smmu-v3.c +16
-0+ ret =arm_smmu_device_acpi_probe (pdev, smmu); +if ( ret == -ENODEV ) +return ret; + } + +/* Set bypass mode according to firmware probing result */ + bypass = !!ret; + +/* Base address */ + ret =dt_device_get_address (dev_to_dt (pdev),0 , &ioaddr, &iosize); +if ( ret ) +return -ENODEV ; + +if ( iosize <arm_smmu_resource_size (smmu) ) + { +dev_err (pdev,"MMIO region too small (%lx)\n" , iosize);
Quoted 8 lines
xen/drivers/passthrough/arm/smmu-v3.c static unsigned long arm_smmu_resource_size(struct arm_smmu_device *smmu) · line 2551
Quoted 6 lines
xen/drivers/passthrough/arm/smmu-v3.c +6
-0+return -EINVAL ; + } + +/* +* Don't map the IMPLEMENTATION DEFINED regions, since they may contain +* the PMCG registers which are reserved by the PMU driver. Does this apply to Xen too?
Yes. Performance monitoring facilities are optional. Currently there is no plan to support also in XEN.
Quoted 64 lines
Quoted 64 lines
xen/drivers/passthrough/arm/smmu-v3.c +62
-0+*/ + smmu->base =ioremap_nocache (ioaddr, iosize); +if (IS_ERR (smmu->base) ) +return PTR_ERR (smmu->base); + +if ( iosize >SZ_64K ) + { + smmu->page1 =ioremap_nocache (ioaddr +SZ_64K ,ARM_SMMU_REG_SZ ); +if (IS_ERR (smmu->page1) ) +return PTR_ERR (smmu->page1); + } +else + { + smmu->page1 = smmu->base; + } + +/* Interrupt lines */ + + irq =platform_get_irq_byname_optional (pdev,"combined" ); +if ( irq >0 ) + smmu->combined_irq = irq; +else + { + irq =platform_get_irq_byname_optional (pdev,"eventq" ); +if ( irq >0 ) + smmu->evtq.q.irq = irq; + + irq =platform_get_irq_byname_optional (pdev,"priq" ); +if ( irq >0 ) + smmu->priq.q.irq = irq; + + irq =platform_get_irq_byname_optional (pdev,"gerror" ); +if ( irq >0 ) + smmu->gerr_irq = irq; + } +/* Probe the h/w */ + ret =arm_smmu_device_hw_probe (smmu); +if ( ret ) +return ret; + +/* Initialise in-memory data structures */ + ret =arm_smmu_init_structures (smmu); +if ( ret ) +return ret; + +/* Reset the device */ + ret =arm_smmu_device_reset (smmu, bypass); +if ( ret ) +return ret; + +/* +* Keep a list of all probed devices. This will be used to query +* the smmu devices based on the fwnode. +*/ +INIT_LIST_HEAD (&smmu->devices); + +spin_lock (&arm_smmu_devices_lock); +list_add (&smmu->devices, &arm_smmu_devices); +spin_unlock (&arm_smmu_devices_lock); + +return 0 ; +}
Hi Stefano,
Quoted 888 lines · Rahul Singh
Hello Stefano,
Quoted 21 lines · Stefano Stabellini
Quoted 17 lines · Rahul Singh
Add support for ARM architected SMMUv3 implementation. It is based on the Linux SMMUv3 driver.
Major differences with regard to Linux driver are as follows:
1. Only Stage-2 translation is supported as compared to the Linux driver that supports both Stage-1 and Stage-2 translations.
2. Use P2M page table instead of creating one as SMMUv3 has the capability to share the page tables with the CPU.
3. Tasklets are used in place of threaded IRQ's in Linux for event queue and priority queue IRQ handling.
4. Latest version of the Linux SMMUv3 code implements the commands queue access functions based on atomic operations implemented in Linux. Atomic functions used by the commands queue access functions are not implemented in XEN therefore we decided to port the earlier version of the code. Once the proper atomic operations will be available in XEN the driver can be updated.
5. Driver is currently supported as Tech Preview.Quoted 1 line
This patch is big and was difficult to review, nonetheless I tried :-)
Thanks again for reviewing the code.
Quoted 149 lines
That said, the code is self-contained, marked as Tech Preview, and not at risk of making other things unstable, so it is low risk to accept it.
Comments below.
Quoted 128 lines
Signed-off-by Rahul Singh <[email protected]>---
MAINTAINERS | 6 + SUPPORT.md | 1 + xen/drivers/passthrough/Kconfig | 10 + xen/drivers/passthrough/arm/Makefile | 1 + xen/drivers/passthrough/arm/smmu-v3.c | 986 +++++++++++++++++++++----- 5 files changed, 814 insertions(+), 190 deletions(-)MAINTAINERS +0
-0@@ -249,6 +249,12 @@ F: xen/include/asm-arm/F: xen/include/public/arch-arm/
F:xen/include/public/arch-arm.hMAINTAINERS +6
-0+ARM SMMUv3 +M: Bertrand Marquis <[email protected]> +M: Rahul Singh <[email protected]> +S: Supported +F: xen/drivers/passthrough/arm/smmu-v3.c +Change Log
M: Paul Durrant <[email protected]>
R: Community Manager <[email protected]>SUPPORT.md +1
-0@@ -68,6 +68,7 @@ For the Cortex A57 r0p0 - r1p1, see Errata 832075. Status, ARM SMMUv1: Supported, not security supported Status, ARM SMMUv2: Supported, not security supported Status, Renesas IPMMU-VMSA: Supported, not security supported + Status, ARM SMMUv3: Tech Preview ### ARM/GICv3 ITSxen/drivers/passthrough/Kconfig +10
-0@@ -13,6 +13,16 @@ config ARM_SMMU Say Y here if your SoC includes an IOMMU device implementing the ARM SMMU architecture. +config ARM_SMMU_V3 + bool "ARM Ltd. System MMU Version 3 (SMMUv3) Support" if EXPERT + depends on ARM_64 + ---help--- + Support for implementations of the ARM System MMU architecture + version 3. + + Say Y here if your system includes an IOMMU device implementing + the ARM SMMUv3 architecture. +config
IPMMU_VMSA
bool "Renesas IPMMU-VMSA found in R-Car Gen3 SoCs" depends onARM_64xen/drivers/passthrough/arm/Makefile +0
-0@@ -1,3 +1,4 @@obj-y += iommu.o iommu_helpers.o iommu_fwspec.o
obj-$(CONFIG_ARM_SMMU) += smmu.o
obj-$(CONFIG_IPMMU_VMSA) += ipmmu-vmsa.oxen/drivers/passthrough/arm/Makefile +1
-0+obj-$(CONFIG_ARM_SMMU_V3) += smmu-v3.oxen/drivers/passthrough/arm/smmu-v3.c +47
-3@@ -2,36 +2,280 @@/* * IOMMU API for ARM architected SMMUv3 implementations. * -* Copyright (C) 2015 ARM Limited +* Based on Linux's SMMUv3 driver: +* drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c +* commit: 951cbbc386ff01b50da4f46387e994e81d9ab431 +* and Xen's SMMU driver: +* xen/drivers/passthrough/arm/smmu.c * -* Author: Will Deacon <[email protected]> +* Copyright (C) 2015 ARM Limited Will Deacon <[email protected]> * -* This driver is powered by bad coffee and bombay mix. +* Copyright (C) 2020 Arm Ltd. +* +* This program is free software; you can redistribute it and/or modify +* it under the terms of the GNU General Public License version 2 as +* published by the Free Software Foundation. +* +* This program is distributed in the hope that it will be useful, +* but WITHOUT ANY WARRANTY; without even the implied warranty of +* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +* GNU General Public License for more details. +* +* You should have received a copy of the GNU General Public License +* along with this program. If not, see <http://www.gnu.org/licenses/>. +* +*/ + +#include <xen/acpi.h> +#include <xen/config.h> +#include <xen/delay.h> +#include <xen/errno.h> +#include <xen/err.h> +#include <xen/irq.h> +#include <xen/lib.h> +#include <xen/list.h> +#include <xen/mm.h> +#include <xen/rbtree.h> +#include <xen/sched.h> +#include <xen/sizes.h> +#include <xen/vmap.h> +#include <asm/atomic.h> +#include <asm/device.h> +#include <asm/io.h> +#include <asm/platform.h> +#include <asm/iommu_fwspec.h> + +/* Linux compatibility functions. */ +typedef paddr_t dma_addr_t ; +typedef unsigned int gfp_t ; + +#define platform_device device +xen/drivers/passthrough/arm/smmu-v3.c line 63
Quoted 10 lines
xen/drivers/passthrough/arm/smmu-v3.c +10
-0+#define GFP_KERNEL 0 + +/* Alias to Xen device tree helpers */ +#define device_node dt_device_node +#define of_phandle_args dt_phandle_args +#define of_device_id dt_device_match +#define of_match_node dt_match_node +#define of_property_read_u32 (np, pname, out) (!dt_property_read_u32(np, pname, out)) +#define of_property_read_bool dt_property_read_bool +#define of_parse_phandle_with_args dt_parse_phandle_with_argsGiven all the changes to the file by the previous patches we are basically fully (or almost fully) adapting this code to Xen.
Quoted 2 lines
So at that point I wonder if we should just as well make these changes (e.g. s/of_phandle_args/dt_phandle_args/g) to the code too.
Yes, you are right that is why in the first version of the patch I modifying the code to make it fully XEN compatible. But in this patch series I use the Linux compatibility function to have Linux code unmodified.I also prefer to make changes (s/of_phandle_args/dt_phandle_args/g).
Quoted 7 lines
Julien, Rahul, what do you guys think?
xen/drivers/passthrough/arm/smmu-v3.c line 65
Quoted 1 line
xen/drivers/passthrough/arm/smmu-v3.c +1
-0+/* Alias to Xen lock functions */ I think this deserves at least one statement to explain why it is OK.
Ack. I Will add the comment.
Quoted 35 lines
Also, a similar comment to the one above: maybe we should add a couple of more preparation patches to s/mutex/spinlock/g and change the time and allocation functions too.
Quoted 29 lines
xen/drivers/passthrough/arm/smmu-v3.c +29
-0+#define mutex spinlock +#define mutex_init spin_lock_init +#define mutex_lock spin_lock +#define mutex_unlock spin_unlock + +/* Alias to Xen time functions */ +#define ktime_t s_time_t +#define ktime_get () (NOW()) +#define ktime_add_us (t,i) (t + MICROSECS(i)) +#define ktime_compare (t,i) (t > (i)) + +/* Alias to Xen allocation helpers */ +#define kzalloc (size, flags) _xzalloc(size, sizeof(void *)) +#define kfree xfree +#define devm_kzalloc (dev, size, flags) _xzalloc(size, sizeof(void *)) + +/* Device logger functions */ +#define dev_name (dev) dt_node_full_name(dev->of_node) +#define dev_dbg (dev, fmt, ...) \ + printk(XENLOG_DEBUG "SMMUv3: %s: " fmt, dev_name(dev), ## __VA_ARGS__) +#define dev_notice (dev, fmt, ...) \ + printk(XENLOG_INFO "SMMUv3: %s: " fmt, dev_name(dev), ## __VA_ARGS__) +#define dev_warn (dev, fmt, ...) \ + printk(XENLOG_WARNING "SMMUv3: %s: " fmt, dev_name(dev), ## __VA_ARGS__) +#define dev_err (dev, fmt, ...) \ + printk(XENLOG_ERR "SMMUv3: %s: " fmt, dev_name(dev), ## __VA_ARGS__) +#define dev_info (dev, fmt, ...) \ + printk(XENLOG_INFO "SMMUv3: %s: " fmt, dev_name(dev), ## __VA_ARGS__) +#define dev_err_ratelimited (dev, fmt, ...) \Quoted 8 lines
xen/drivers/passthrough/arm/smmu-v3.c line 99
Quoted 5 lines
xen/drivers/passthrough/arm/smmu-v3.c +5
-0+ printk(XENLOG_ERR "SMMUv3: %s: " fmt, dev_name(dev), ## __VA_ARGS__) + +/* +* Periodically poll an address and wait between reads in us until a +* condition is met or a timeout occurs. It would be good to add a statement to point out what the return value is going to be.
Ack.
Quoted 18 lines
Quoted 18 lines
xen/drivers/passthrough/arm/smmu-v3.c +16
-0+*/ +#define readx_poll_timeout (op, addr, val, cond, sleep_us, timeout_us) \ +({ \ + s_time_t deadline = NOW() + MICROSECS(timeout_us); \ + for (;;) { \ + (val) = op(addr); \ + if (cond) \ + break; \ + if (NOW() > deadline) { \ + (val) = op(addr); \ + break; \ + } \ + udelay(sleep_us); \ + } \ + (cond) ? 0 : -ETIMEDOUT; \ +})Quoted 8 lines
xen/drivers/passthrough/arm/smmu-v3.c line 121
Quoted 6 lines
xen/drivers/passthrough/arm/smmu-v3.c +6
-0+ +#define readl_relaxed_poll_timeout (addr, val, cond, delay_us, timeout_us) \ + readx_poll_timeout(readl_relaxed, addr, val, cond, delay_us, timeout_us) + +#define FIELD_PREP (_mask, _val) \ + (((typeof(_mask))(_val) << (__builtin_ffsll(_mask) - 1)) & (_mask))Let's add the definition of ffsll to
bitops.hOk.
Quoted 11 lines
xen/drivers/passthrough/arm/smmu-v3.c line 129
Quoted 9 lines
xen/drivers/passthrough/arm/smmu-v3.c +7
-0+#define FIELD_GET (_mask, _reg) \ + (typeof(_mask))(((_reg) & (_mask)) >> (__builtin_ffsll(_mask) - 1)) + +#define WRITE_ONCE (x, val) \ +do { \ + *(volatile typeof(x) *)&(x) = (val); \ +} while (0)maybe we should define this in
xen/include/xen/lib.hOk.
Quoted 9 lines
xen/drivers/passthrough/arm/smmu-v3.c line 133
Quoted 6 lines
xen/drivers/passthrough/arm/smmu-v3.c +4
-0+ +/* Xen: Stub out DMA domain related functions */ +#define iommu_get_dma_cookie (dom) 0 +#define iommu_put_dma_cookie (dom)Shouldn't we remove any call to
iommu_get_dma_cookieandiommu_put_dma_cookiein one of the previous patches?Ack.
Quoted 62 lines
Quoted 62 lines
xen/drivers/passthrough/arm/smmu-v3.c +59
-0+/* +* Helpers for DMA allocation. Just the function name is reused for +* porting code, these allocation are not managed allocations */ +static void *dmam_alloc_coherent (struct device *dev,size_t size, +paddr_t *dma_handle,gfp_t gfp) +{ +void *vaddr; +unsigned long alignment = size; + +/* +* _xzalloc requires that the (align & (align -1)) = 0. Most of the +* allocations in SMMU code should send the right value for size. In +* case this is not true print a warning and align to the size of a +* (void *) +*/ +if ( size & (size -1 ) ) + { +printk (XENLOG_WARNING "SMMUv3: Fixing alignment for the DMA buffer\n" ); + alignment =sizeof (void *); + } + + vaddr =_xzalloc (size, alignment); +if ( !vaddr ) + { +printk (XENLOG_ERR "SMMUv3: DMA allocation failed\n" ); +return NULL ; + } + + *dma_handle =virt_to_maddr (vaddr); + +return vaddr; +} + +/* Xen: Type definitions for iommu_domain */ +#define IOMMU_DOMAIN_UNMANAGED 0 +#define IOMMU_DOMAIN_DMA 1 +#define IOMMU_DOMAIN_IDENTITY 2 + +/* Xen specific code. */ +struct iommu_domain { +/* Runtime SMMU configuration for this iommu_domain */ +atomic_t ref; +/* +* Used to link iommu_domain contexts for a same domain. +* There is at least one per-SMMU to used by the domain. +*/ +struct list_head list; +}; + +/* Describes information required for a Xen domain */ +struct arm_smmu_xen_domain { +spinlock_t lock; + +/* List of iommu domains associated to this domain */ +struct list_head contexts; +}; + +/* +* Information about each device stored in dev->archdata.iommu Quoted 9 lines
xen/drivers/passthrough/arm/smmu-v3.c line 200
Quoted 6 lines
xen/drivers/passthrough/arm/smmu-v3.c +6
-0+* The dev->archdata.iommu stores the iommu_domain (runtime configuration of +* the SMMU). +*/ +struct arm_smmu_xen_device { +struct iommu_domain *domain; +};Do we need both struct
arm_smmu_xen_deviceand structiommu_domain?No we don’t need both. I will remove the struct
arm_smmu_xen_device.Quoted 46 lines
Quoted 46 lines
xen/drivers/passthrough/arm/smmu-v3.c +45
-0+/* Keep a list of devices associated with this driver */ +static DEFINE_SPINLOCK (arm_smmu_devices_lock); +static LIST_HEAD (arm_smmu_devices); + + +static inline void *dev_iommu_priv_get (struct device *dev) +{ +struct iommu_fwspec *fwspec =dev_iommu_fwspec_get (dev); + +return fwspec && fwspec->iommu_priv ? fwspec->iommu_priv :NULL ; +} + +static inline void dev_iommu_priv_set (struct device *dev,void *priv) +{ +struct iommu_fwspec *fwspec =dev_iommu_fwspec_get (dev); + + fwspec->iommu_priv = priv; +} + +int dt_property_match_string (const struct dt_device_node *np, +const char *propname,const char *string) +{ +const struct dt_property *dtprop =dt_find_property (np, propname,NULL ); +size_t l; +int i; +const char *p, *end; + +if ( !dtprop ) +return -EINVAL ; + +if ( !dtprop->value ) +return -ENODATA ; + + p = dtprop->value; + end = p + dtprop->length; + +for ( i =0 ; p < end; i++, p += l ) + { + l =strnlen (p, end - p) +1 ; + +if ( p + l > end ) +return -EILSEQ ; + +if (strcmp (string, p) ==0 ) +return i;/* Found it; return index */ Quoted 8 lines
xen/drivers/passthrough/arm/smmu-v3.c line 247
Quoted 4 lines
xen/drivers/passthrough/arm/smmu-v3.c +4
-0+ } + +return -ENODATA ; +}I think you should either use
dt_property_read_stringor move the implementation ofdt_property_match_stringtoxen/common/device_tree.c(in which case I suggest to do it in a separate patch.)I will prefer to move the code to
xen/common/device_tree.cas it might help in future to use.Quoted 86 lines
I'd just use
dt_property_read_string.Quoted 80 lines
xen/drivers/passthrough/arm/smmu-v3.c +34
-25+static int platform_get_irq_byname_optional (struct device *dev, +const char *name) +{ +int index, ret; +struct dt_device_node *np =dev_to_dt (dev); + +if (unlikely (!name) ) +return -EINVAL ; + + index =dt_property_match_string (np,"interrupt-names" , name); +if ( index <0 ) + { +dev_info (dev,"IRQ %s not found\n" , name); +return index; + } -#include <linux/acpi.h> -#include <linux/acpi_iort.h> -#include <linux/bitfield.h> -#include <linux/bitops.h> -#include <linux/crash_dump.h> -#include <linux/delay.h> -#include <linux/dma-iommu.h> -#include <linux/err.h> -#include <linux/interrupt.h> -#include <linux/io-pgtable.h> -#include <linux/iommu.h> -#include <linux/iopoll.h> -#include <linux/module.h> -#include <linux/msi.h> -#include <linux/of.h> -#include <linux/of_address.h> -#include <linux/of_iommu.h> -#include <linux/of_platform.h> -#include <linux/pci.h> -#include <linux/pci-ats.h> -#include <linux/platform_device.h> - -#include <linux/amba/bus.h> + ret =platform_get_irq (np, index); +if ( ret <0 ) + { +dev_err (dev,"failed to get irq index %d\n" , index); +return -ENODEV ; + } + +return ret; +} + +/* Start of Linux SMMUv3 code */ /* MMIO registers */ #define ARM_SMMU_IDR0 0x0@@ -507,6 +751,7 @@ struct arm_smmu_s2_cfg {u16 vmid;u64 vttbr;u64 vtcr; +struct domain *domain; };struct arm_smmu_strtab_cfg {@@ -567,8 +812,13 @@ struct arm_smmu_device {struct arm_smmu_strtab_cfg strtab_cfg; -/* IOMMU core code handle */ -struct iommu_device iommu; +/* Need to keep a list of SMMU devices */ +struct list_head devices; + +/* Tasklets for handling evts/faults and pci page request IRQs*/ +struct tasklet evtq_irq_tasklet; +struct tasklet priq_irq_tasklet; +struct tasklet combined_irq_tasklet; };/* SMMU private data for each master */ @@ -1110,7 +1360,7 @@ static int arm_smmu_init_l2_strtab(struct arm_smmu_device *smmu, u32 sid) }Quoted 6 lines
xen/drivers/passthrough/arm/smmu-v3.c static int arm_smmu_init_l2_strtab(struct arm_smmu_device *smmu, u32 sid) · line 1363
Quoted 3 lines
xen/drivers/passthrough/arm/smmu-v3.c +1
-1/* IRQ and event handlers */ -static irqreturn_t arm_smmu_evtq_thread (int irq,void *dev) +static void arm_smmu_evtq_thread (void *dev)I think we shouldn't call it a thread given that's a tasklet. We should rename the function or it will be confusing
Ok.
Quoted 16 lines
Quoted 16 lines
xen/drivers/passthrough/arm/smmu-v3.c +0
-1{int i;struct arm_smmu_device *smmu = dev;@@ -1140,7 +1390,6 @@ static irqreturn_t arm_smmu_evtq_thread(int irq, void *dev)/* Sync our overflow flag, as we believe we're up to speed */ llq->cons =Q_OVF (llq->prod) |Q_WRP (llq, llq->cons) |Q_IDX (llq, llq->cons); -return IRQ_HANDLED ; }static void arm_smmu_handle_ppr (struct arm_smmu_device *smmu,u64 *evt)@@ -1181,7 +1430,7 @@ static void arm_smmu_handle_ppr(struct arm_smmu_device *smmu, u64 *evt) } }Quoted 4 lines
xen/drivers/passthrough/arm/smmu-v3.c static void arm_smmu_handle_ppr(struct arm_smmu_device *smmu, u64 *evt) · line 1433
Quoted 2 lines
xen/drivers/passthrough/arm/smmu-v3.c +1
-1-static irqreturn_t arm_smmu_priq_thread (int irq,void *dev) +static void arm_smmu_priq_thread (void *dev)same here
Ok.
Quoted 197 lines
Quoted 197 lines
xen/drivers/passthrough/arm/smmu-v3.c +83
-41{struct arm_smmu_device *smmu = dev;struct arm_smmu_queue *q = &smmu->priq.q;@@ -1200,12 +1449,12 @@ static irqreturn_t arm_smmu_priq_thread(int irq, void *dev) llq->cons =Q_OVF (llq->prod) |Q_WRP (llq, llq->cons) |Q_IDX (llq, llq->cons);queue_sync_cons_out (q); -return IRQ_HANDLED ; }static int arm_smmu_device_disable (struct arm_smmu_device *smmu); -static irqreturn_t arm_smmu_gerror_handler (int irq,void *dev) +static void arm_smmu_gerror_handler (int irq,void *dev, +struct cpu_user_regs *regs) {u32 gerror, gerrorn, active;struct arm_smmu_device *smmu = dev;@@ -1215,7 +1464,7 @@ static irqreturn_t arm_smmu_gerror_handler(int irq, void *dev) active = gerror ^ gerrorn;if (!(active &GERROR_ERR_MASK )) -return IRQ_NONE ;/* No errors pending */ +return ;/* No errors pending */ dev_warn(smmu->dev, "unexpected global error reported (0x%08x), this could be serious\n",@@ -1248,26 +1497,42 @@ static irqreturn_t arm_smmu_gerror_handler(int irq, void *dev)arm_smmu_cmdq_skip_err (smmu);writel (gerror, smmu->base +ARM_SMMU_GERRORN ); -return IRQ_HANDLED ; } -static irqreturn_t arm_smmu_combined_irq_thread (int irq,void *dev) +static void arm_smmu_combined_irq_handler (int irq,void *dev, +struct cpu_user_regs *regs) +{ +struct arm_smmu_device *smmu = (struct arm_smmu_device *)dev; + +arm_smmu_gerror_handler (irq, dev, regs); + +tasklet_schedule (&(smmu->combined_irq_tasklet)); +} + +static void arm_smmu_combined_irq_thread (void *dev) {struct arm_smmu_device *smmu = dev; -arm_smmu_evtq_thread (irq, dev); +arm_smmu_evtq_thread (dev);if (smmu->features &ARM_SMMU_FEAT_PRI ) -arm_smmu_priq_thread (irq, dev); - -return IRQ_HANDLED ; +arm_smmu_priq_thread (dev); } -static irqreturn_t arm_smmu_combined_irq_handler (int irq,void *dev) +static void arm_smmu_evtq_irq_tasklet (int irq,void *dev, +struct cpu_user_regs *regs) { -arm_smmu_gerror_handler (irq, dev); -return IRQ_WAKE_THREAD ; +struct arm_smmu_device *smmu = (struct arm_smmu_device *)dev; + +tasklet_schedule (&(smmu->evtq_irq_tasklet)); } +static void arm_smmu_priq_irq_tasklet (int irq,void *dev, +struct cpu_user_regs *regs) +{ +struct arm_smmu_device *smmu = (struct arm_smmu_device *)dev; + +tasklet_schedule (&(smmu->priq_irq_tasklet)); +}/* IO_PGTABLE API */ static void arm_smmu_tlb_inv_context (void *cookie)@@ -1354,27 +1619,69 @@ static void arm_smmu_domain_free(struct iommu_domain *domain) }static int arm_smmu_domain_finalise_s2 (struct arm_smmu_domain *smmu_domain, -struct arm_smmu_master *master, -struct io_pgtable_cfg *pgtbl_cfg) +struct arm_smmu_master *master) {int vmid; +u64 reg;struct arm_smmu_device *smmu = smmu_domain->smmu;struct arm_smmu_s2_cfg *cfg = &smmu_domain->s2_cfg; +/* VTCR */ + reg =VTCR_RES1 |VTCR_SH0_IS |VTCR_IRGN0_WBWA |VTCR_ORGN0_WBWA ; + +switch (PAGE_SIZE ) { +case SZ_4K : + reg |=VTCR_TG0_4K ; +break ; +case SZ_16K : + reg |=VTCR_TG0_16K ; +break ; +case SZ_64K : + reg |=VTCR_TG0_4K ; +break ; + } + +switch (smmu->oas) { +case 32 : + reg |=VTCR_PS (_AC (0x0 ,ULL )); +break ; +case 36 : + reg |=VTCR_PS (_AC (0x1 ,ULL )); +break ; +case 40 : + reg |=VTCR_PS (_AC (0x2 ,ULL )); +break ; +case 42 : + reg |=VTCR_PS (_AC (0x3 ,ULL )); +break ; +case 44 : + reg |=VTCR_PS (_AC (0x4 ,ULL )); +break ; +case 48 : + reg |=VTCR_PS (_AC (0x5 ,ULL )); +break ; +case 52 : + reg |=VTCR_PS (_AC (0x6 ,ULL )); +break ; + } + + reg |=VTCR_T0SZ (64ULL - smmu->ias); + reg |=VTCR_SL0 (0x2 ); + reg |=VTCR_VS ; + + cfg->vtcr = reg; + vmid =arm_smmu_bitmap_alloc (smmu->vmid_map, smmu->vmid_bits);if (vmid <0 )return vmid; + cfg->vmid = (u16 )vmid; + + cfg->vttbr =page_to_maddr (cfg->domain->arch.p2m.root); + +printk (XENLOG_DEBUG +"SMMUv3: d%u: vmid 0x%x vtcr 0x%" PRIpaddr" p2maddr 0x%" PRIpaddr"\n" , + cfg->domain->domain_id, cfg->vmid, cfg->vtcr, cfg->vttbr); - vtcr = &pgtbl_cfg->arm_lpae_s2_cfg.vtcr; - cfg->vmid = (u16 )vmid; - cfg->vttbr = pgtbl_cfg->arm_lpae_s2_cfg.vttbr; - cfg->vtcr =FIELD_PREP (STRTAB_STE_2_VTCR_S2T0SZ , vtcr->tsz) | -FIELD_PREP (STRTAB_STE_2_VTCR_S2SL0 , vtcr->sl) | -FIELD_PREP (STRTAB_STE_2_VTCR_S2IR0 , vtcr->irgn) | -FIELD_PREP (STRTAB_STE_2_VTCR_S2OR0 , vtcr->orgn) | -FIELD_PREP (STRTAB_STE_2_VTCR_S2SH0 , vtcr->sh) | -FIELD_PREP (STRTAB_STE_2_VTCR_S2TG , vtcr->tg) | -FIELD_PREP (STRTAB_STE_2_VTCR_S2PS , vtcr->ps);return 0 ; }@@ -1382,28 +1689,12 @@ static int arm_smmu_domain_finalise(struct iommu_domain *domain, struct arm_smmu_master *master) {int ret; -unsigned long ias, oas; -int (*finalise_stage_fn)(struct arm_smmu_domain *, -struct arm_smmu_master *, -struct io_pgtable_cfg *);struct arm_smmu_domain *smmu_domain =to_smmu_domain (domain); -struct arm_smmu_device *smmu = smmu_domain->smmu;/* Restrict the stage to what we can actually support */ smmu_domain->stage =ARM_SMMU_DOMAIN_S2 ; -switch (smmu_domain->stage) { -case ARM_SMMU_DOMAIN_NESTED : -case ARM_SMMU_DOMAIN_S2 : - ias = smmu->ias; - oas = smmu->oas; - finalise_stage_fn = arm_smmu_domain_finalise_s2; -break ; -default : -return -EINVAL ; - } - - ret =finalise_stage_fn (smmu_domain, master, &pgtbl_cfg); + ret =arm_smmu_domain_finalise_s2 (smmu_domain, master);if (ret <0 ) {return ret; }@@ -1553,7 +1844,8 @@ static int arm_smmu_init_one_queue(struct arm_smmu_device *smmu,return -ENOMEM ; }Quoted 9 lines
xen/drivers/passthrough/arm/smmu-v3.c static int arm_smmu_init_one_queue(struct arm_smmu_device *smmu, · line 1850
Quoted 5 lines
xen/drivers/passthrough/arm/smmu-v3.c +2
-1-if (!WARN_ON (q->base_dma & (qsz -1 ))) { +WARN_ON (q->base_dma & (qsz -1 )); +if (unlikely (q->base_dma & (qsz -1 ))) {dev_info (smmu->dev,"allocated %u entries for %s\n" ,1 << q->llq.max_n_shift, name);We don't need both the WARNING and the
dev_info. you could turn thedev_warn/XENLOG_WARNING.Ack.
Quoted 109 lines
Quoted 109 lines
xen/drivers/passthrough/arm/smmu-v3.c +27
-25}@@ -1758,9 +2050,7 @@ static void arm_smmu_setup_unique_irqs(struct arm_smmu_device *smmu)/* Request interrupt lines */ irq = smmu->evtq.q.irq;if (irq) { - ret =devm_request_threaded_irq (smmu->dev, irq,NULL , - arm_smmu_evtq_thread, -IRQF_ONESHOT , + ret =request_irq (irq,0 , arm_smmu_evtq_irq_tasklet,"arm-smmu-v3-evtq" , smmu);if (ret <0 )dev_warn (smmu->dev,"failed to enable evtq irq\n" );@@ -1770,8 +2060,8 @@ static void arm_smmu_setup_unique_irqs(struct arm_smmu_device *smmu) irq = smmu->gerr_irq;if (irq) { - ret =devm_request_irq (smmu->dev, irq, arm_smmu_gerror_handler, -0 ,"arm-smmu-v3-gerror" , smmu); + ret =request_irq (irq,0 , arm_smmu_gerror_handler, +"arm-smmu-v3-gerror" , smmu);if (ret <0 )dev_warn (smmu->dev,"failed to enable gerror irq\n" ); }else {@@ -1781,11 +2071,8 @@ static void arm_smmu_setup_unique_irqs(struct arm_smmu_device *smmu)if (smmu->features &ARM_SMMU_FEAT_PRI ) { irq = smmu->priq.q.irq;if (irq) { - ret =devm_request_threaded_irq (smmu->dev, irq,NULL , - arm_smmu_priq_thread, -IRQF_ONESHOT , -"arm-smmu-v3-priq" , - smmu); + ret =request_irq (irq,0 , arm_smmu_priq_irq_tasklet, +"arm-smmu-v3-priq" , smmu);if (ret <0 )dev_warn (smmu->dev,"failed to enable priq irq\n" );@@ -1814,11 +2101,8 @@ static int arm_smmu_setup_irqs(struct arm_smmu_device *smmu)* Cavium ThunderX2 implementation doesn't support unique irq * lines. Use a single irq line for all the SMMUv3 interrupts. */ - ret =devm_request_threaded_irq (smmu->dev, irq, - arm_smmu_combined_irq_handler, - arm_smmu_combined_irq_thread, -IRQF_ONESHOT , -"arm-smmu-v3-combined-irq" , smmu); + ret =request_irq (irq,0 , arm_smmu_combined_irq_handler, +"arm-smmu-v3-combined-irq" , smmu);if (ret <0 )dev_warn (smmu->dev,"failed to enable combined irq\n" ); }else @@ -1857,7 +2141,7 @@ static int arm_smmu_device_reset(struct arm_smmu_device *smmu, bool bypass) reg =readl_relaxed (smmu->base +ARM_SMMU_CR0 );if (reg &CR0_SMMUEN ) {dev_warn (smmu->dev,"SMMU currently enabled! Resetting...\n" ); -WARN_ON (is_kdump_kernel () && !disable_bypass); +WARN_ON (!disable_bypass);arm_smmu_update_gbpa (smmu,GBPA_ABORT ,0 ); }@@ -1952,8 +2236,11 @@ static int arm_smmu_device_reset(struct arm_smmu_device *smmu, bool bypass)return ret; } -if (is_kdump_kernel ()) - enables &= ~(CR0_EVTQEN |CR0_PRIQEN ); +/* Initialize tasklets for threaded IRQs*/ +tasklet_init (&smmu->evtq_irq_tasklet, arm_smmu_evtq_thread, smmu); +tasklet_init (&smmu->priq_irq_tasklet, arm_smmu_priq_thread, smmu); +tasklet_init (&smmu->combined_irq_tasklet, arm_smmu_combined_irq_thread, + smmu);/* Enable the SMMU interface, or ensure bypass */ if (!bypass || disable_bypass) {@@ -2195,7 +2482,7 @@ static inline int arm_smmu_device_acpi_probe(struct platform_device *pdev,static int arm_smmu_device_dt_probe (struct platform_device *pdev,struct arm_smmu_device *smmu) { -struct device *dev = &pdev->dev; +struct device *dev = pdev;u32 cells;int ret = -EINVAL ;@@ -2219,130 +2506,449 @@ static unsigned long arm_smmu_resource_size(struct arm_smmu_device *smmu)return SZ_128K ; } +/* Start of Xen specific code. */ static int arm_smmu_device_probe (struct platform_device *pdev) { -int irq, ret; -struct resource *res; -resource_size_t ioaddr; -struct arm_smmu_device *smmu; -struct device *dev = &pdev->dev; -bool bypass; +int irq, ret; +paddr_t ioaddr, iosize; +struct arm_smmu_device *smmu; +bool bypass; + + smmu =devm_kzalloc (dev,sizeof (*smmu),GFP_KERNEL ); +if ( !smmu ) + { +dev_err (pdev,"failed to allocate arm_smmu_device\n" ); +return -ENOMEM ; + } + smmu->dev = pdev;Quoted 8 lines
xen/drivers/passthrough/arm/smmu-v3.c static unsigned long arm_smmu_resource_size(struct arm_smmu_device *smmu) · line 2529
Quoted 6 lines
xen/drivers/passthrough/arm/smmu-v3.c +6
-0+ +if ( pdev->of_node ) + { + ret =arm_smmu_device_dt_probe (pdev, smmu); + }else + {coding style
Ack.
Quoted 18 lines
Quoted 18 lines
xen/drivers/passthrough/arm/smmu-v3.c +16
-0+ ret =arm_smmu_device_acpi_probe (pdev, smmu); +if ( ret == -ENODEV ) +return ret; + } + +/* Set bypass mode according to firmware probing result */ + bypass = !!ret; + +/* Base address */ + ret =dt_device_get_address (dev_to_dt (pdev),0 , &ioaddr, &iosize); +if ( ret ) +return -ENODEV ; + +if ( iosize <arm_smmu_resource_size (smmu) ) + { +dev_err (pdev,"MMIO region too small (%lx)\n" , iosize);Quoted 8 lines
xen/drivers/passthrough/arm/smmu-v3.c static unsigned long arm_smmu_resource_size(struct arm_smmu_device *smmu) · line 2551
Quoted 6 lines
xen/drivers/passthrough/arm/smmu-v3.c +6
-0+return -EINVAL ; + } + +/* +* Don't map the IMPLEMENTATION DEFINED regions, since they may contain +* the PMCG registers which are reserved by the PMU driver. Does this apply to Xen too?
Quoted 1 line
Yes. Performance monitoring facilities are optional. Currently there is no plan to support also in XEN.
I missed to add that in next version of the patch I will use the ARM_SMMU_REG_SZ as size as also suggested by Julien.
Regards,
Rahul
Quoted 64 lines
Quoted 64 lines
Quoted 64 lines
xen/drivers/passthrough/arm/smmu-v3.c +62
-0+*/ + smmu->base =ioremap_nocache (ioaddr, iosize); +if (IS_ERR (smmu->base) ) +return PTR_ERR (smmu->base); + +if ( iosize >SZ_64K ) + { + smmu->page1 =ioremap_nocache (ioaddr +SZ_64K ,ARM_SMMU_REG_SZ ); +if (IS_ERR (smmu->page1) ) +return PTR_ERR (smmu->page1); + } +else + { + smmu->page1 = smmu->base; + } + +/* Interrupt lines */ + + irq =platform_get_irq_byname_optional (pdev,"combined" ); +if ( irq >0 ) + smmu->combined_irq = irq; +else + { + irq =platform_get_irq_byname_optional (pdev,"eventq" ); +if ( irq >0 ) + smmu->evtq.q.irq = irq; + + irq =platform_get_irq_byname_optional (pdev,"priq" ); +if ( irq >0 ) + smmu->priq.q.irq = irq; + + irq =platform_get_irq_byname_optional (pdev,"gerror" ); +if ( irq >0 ) + smmu->gerr_irq = irq; + } +/* Probe the h/w */ + ret =arm_smmu_device_hw_probe (smmu); +if ( ret ) +return ret; + +/* Initialise in-memory data structures */ + ret =arm_smmu_init_structures (smmu); +if ( ret ) +return ret; + +/* Reset the device */ + ret =arm_smmu_device_reset (smmu, bypass); +if ( ret ) +return ret; + +/* +* Keep a list of all probed devices. This will be used to query +* the smmu devices based on the fwnode. +*/ +INIT_LIST_HEAD (&smmu->devices); + +spin_lock (&arm_smmu_devices_lock); +list_add (&smmu->devices, &arm_smmu_devices); +spin_unlock (&arm_smmu_devices_lock); + +return 0 ; +}
Hi Stefano,
Quoted 14 lines · Stefano Stabellini
xen/drivers/passthrough/arm/smmu-v3.c line 63
Quoted 8 lines · Rahul Singh
xen/drivers/passthrough/arm/smmu-v3.c +8
-0+/* Alias to Xen device tree helpers */ +#define device_node dt_device_node +#define of_phandle_args dt_phandle_args +#define of_device_id dt_device_match +#define of_match_node dt_match_node +#define of_property_read_u32 (np, pname, out) (!dt_property_read_u32(np, pname, out)) +#define of_property_read_bool dt_property_read_bool +#define of_parse_phandle_with_args dt_parse_phandle_with_argsGiven all the changes to the file by the previous patches we are basically fully (or almost fully) adapting this code to Xen.
Quoted 2 lines
So at that point I wonder if we should just as well make these changes (e.g. s/of_phandle_args/dt_phandle_args/g) to the code too.
I have already accepted the fact that keeping Linux code as-is is nearly impossible without much workaround :). The benefits tends to also limited as we noticed for the SMMU driver.
I would like to point out that this may make quite difficult (if not impossible) to revert the previous patches which remove support for some features (e.g. atomic, MSI, ATS).
If we are going to adapt the code to Xen (I'd like to keep Linux code style though), then I think we should consider to keep code that may be useful in the near future (at least MSI, ATS).
Quoted 10 lines
xen/drivers/passthrough/arm/smmu-v3.c line 129
Quoted 8 lines
xen/drivers/passthrough/arm/smmu-v3.c +7
-0+#define FIELD_GET (_mask, _reg) \ + (typeof(_mask))(((_reg) & (_mask)) >> (__builtin_ffsll(_mask) - 1)) + +#define WRITE_ONCE (x, val) \ +do { \ + *(volatile typeof(x) *)&(x) = (val); \ +} while (0)maybe we should define this in
xen/include/xen/lib.h
I have attempted such discussion in the past and this resulted to more bikeshed than it is worth it. So I would suggest to re-implement WRITE_ONCE() as write_atomic() for now.
Cheers,
Signature · 1 line
Julien Grall
Quoted 22 lines · Julien Grall
Quoted 14 lines · Stefano Stabellini
xen/drivers/passthrough/arm/smmu-v3.c line 63
Quoted 9 lines · Rahul Singh
xen/drivers/passthrough/arm/smmu-v3.c +6
-0+/* Alias to Xen device tree helpers */ +#define device_node dt_device_node +#define of_phandle_args dt_phandle_args +#define of_device_id dt_device_match +#define of_match_node dt_match_node +#define of_property_read_u32 (np, pname, out) (!dt_property_read_u32(np,pname, out))
xen/drivers/passthrough/arm/smmu-v3.c +2
-0+#define of_property_read_bool dt_property_read_bool +#define of_parse_phandle_with_args dt_parse_phandle_with_argsGiven all the changes to the file by the previous patches we are basically fully (or almost fully) adapting this code to Xen.
Quoted 2 lines
So at that point I wonder if we should just as well make these changes (e.g. s/of_phandle_args/dt_phandle_args/g) to the code too.
I have already accepted the fact that keeping Linux code as-is is nearly impossible without much workaround :). The benefits tends to also limited as we noticed for the SMMU driver.
Quoted 7 lines
I would like to point out that this may make quite difficult (if not impossible) to revert the previous patches which remove support for some features (e.g. atomic, MSI, ATS).
If we are going to adapt the code to Xen (I'd like to keep Linux code style though), then I think we should consider to keep code that may be useful in the near future (at least MSI, ATS).
(I am fine with keeping the Linux code style.)
We could try to keep the code as similar to Linux as possible. This didn't work out in the past.
Otherwise, we could fully adapt the driver to Xen. If we fully adapt the driver to Xen (code style aside) it is better to be consistent and also do substitutions like s/of_phandle_args/dt_phandle_args/g. Then the policy becomes clear: the code comes from Linux but it is 100% adapted to Xen.
Now the question about what to do about the MSI and ATS code is a good one. We know that we are going to want that code at some point in the next 2 years. Like you wrote, if we fully adapt the code to Xen and remove MSI and ATS code, then it is going to be harder to add it back.
So maybe keeping the MSI and ATS code for now, even if it cannot work, would be better. I think this strategy works well if the MSI and ATS code can be disabled easily, i.e. with a couple of lines of code in the init function rather than #ifdef everywhere. It doesn't work well if we have to add #ifdef everywhere.
It looks like MSI could be disabled adding a couple of lines to arm_smmu_setup_msis.
Similarly ATS seems to be easy to disable by forcing ats_enabled to false.
So yes, this looks like a good way forward. Rahul, what do you think?
Quoted 10 lines
Quoted 9 lines
xen/drivers/passthrough/arm/smmu-v3.c line 129
Quoted 7 lines
xen/drivers/passthrough/arm/smmu-v3.c +7
-0+#define FIELD_GET (_mask, _reg) \ + (typeof(_mask))(((_reg) & (_mask)) >> (__builtin_ffsll(_mask) - 1)) + +#define WRITE_ONCE (x, val) \ +do { \ + *(volatile typeof(x) *)&(x) = (val); \ +} while (0)maybe we should define this in
xen/include/xen/lib.h
Quoted 3 lines
I have attempted such discussion in the past and this resulted to more bikeshed than it is worth it. So I would suggest to re-implement
WRITE_ONCE()aswrite_atomic()for now.
Good suggestion, less discussions more code :-)
Hello Stefano,
Quoted 56 lines · Stefano Stabellini
Quoted 22 lines · Julien Grall
Quoted 14 lines · Stefano Stabellini
xen/drivers/passthrough/arm/smmu-v3.c line 63
Quoted 9 lines · Rahul Singh
xen/drivers/passthrough/arm/smmu-v3.c +6
-0+/* Alias to Xen device tree helpers */ +#define device_node dt_device_node +#define of_phandle_args dt_phandle_args +#define of_device_id dt_device_match +#define of_match_node dt_match_node +#define of_property_read_u32 (np, pname, out) (!dt_property_read_u32(np,pname, out))
xen/drivers/passthrough/arm/smmu-v3.c +2
-0+#define of_property_read_bool dt_property_read_bool +#define of_parse_phandle_with_args dt_parse_phandle_with_argsGiven all the changes to the file by the previous patches we are basically fully (or almost fully) adapting this code to Xen.
Quoted 2 lines
So at that point I wonder if we should just as well make these changes (e.g. s/of_phandle_args/dt_phandle_args/g) to the code too.
I have already accepted the fact that keeping Linux code as-is is nearly impossible without much workaround :). The benefits tends to also limited as we noticed for the SMMU driver.
Quoted 7 lines
I would like to point out that this may make quite difficult (if not impossible) to revert the previous patches which remove support for some features (e.g. atomic, MSI, ATS).
If we are going to adapt the code to Xen (I'd like to keep Linux code style though), then I think we should consider to keep code that may be useful in the near future (at least MSI, ATS).
(I am fine with keeping the Linux code style.)
We could try to keep the code as similar to Linux as possible. This didn't work out in the past.
Otherwise, we could fully adapt the driver to Xen. If we fully adapt the driver to Xen (code style aside) it is better to be consistent and also do substitutions like s/of_phandle_args/dt_phandle_args/g. Then the policy becomes clear: the code comes from Linux but it is 100% adapted to Xen.
Now the question about what to do about the MSI and ATS code is a good one. We know that we are going to want that code at some point in the next 2 years. Like you wrote, if we fully adapt the code to Xen and remove MSI and ATS code, then it is going to be harder to add it back.
So maybe keeping the MSI and ATS code for now, even if it cannot work, would be better. I think this strategy works well if the MSI and ATS code can be disabled easily, i.e. with a couple of lines of code in the init function rather than #ifdef everywhere. It doesn't work well if we have to add #ifdef everywhere.
Quoted 7 lines
It looks like MSI could be disabled adding a couple of lines to
arm_smmu_setup_msis.Similarly ATS seems to be easy to disable by forcing
ats_enabledto false.So yes, this looks like a good way forward. Rahul, what do you think?
I am ok to have the PCI ATS and MSI functionality in the code.
As per the discussion next version of the patch will include below modification:Please let me know if there are any suggestion overall that should be added in next version.
1. Keep the PCI ATS and MSI functionality code.
2. Make the code with XEN compatible ( remove linux compatibility functions)
3. Keep the Linux coding style for code imported from Linux.
4. Fix all comments.
I have one query what will be coding style for new code to make driver work for XEN ?
Quoted 13 lines
Quoted 13 lines
Quoted 12 lines
xen/drivers/passthrough/arm/smmu-v3.c line 129
Quoted 10 lines
xen/drivers/passthrough/arm/smmu-v3.c +7
-0+#define FIELD_GET (_mask, _reg) \ + (typeof(_mask))(((_reg) & (_mask)) >> (__builtin_ffsll(_mask) - 1)) + +#define WRITE_ONCE (x, val) \ +do { \ + *(volatile typeof(x) *)&(x) = (val); \ +} while (0)maybe we should define this in
xen/include/xen/lib.h
Quoted 5 lines
Quoted 3 lines
I have attempted such discussion in the past and this resulted to more bikeshed than it is worth it. So I would suggest to re-implement
WRITE_ONCE()aswrite_atomic()for now.Good suggestion, less discussions more code :-)
I will use the write_atomic.
Regards,
Rahul
Quoted 61 lines · Rahul Singh
Quoted 61 lines · Stefano Stabellini
Quoted 22 lines · Julien Grall
Quoted 14 lines · Stefano Stabellini
xen/drivers/passthrough/arm/smmu-v3.c line 63
Quoted 9 lines · Rahul Singh
xen/drivers/passthrough/arm/smmu-v3.c +6
-0+/* Alias to Xen device tree helpers */ +#define device_node dt_device_node +#define of_phandle_args dt_phandle_args +#define of_device_id dt_device_match +#define of_match_node dt_match_node +#define of_property_read_u32 (np, pname, out) (!dt_property_read_u32(np,pname, out))
xen/drivers/passthrough/arm/smmu-v3.c +2
-0+#define of_property_read_bool dt_property_read_bool +#define of_parse_phandle_with_args dt_parse_phandle_with_argsGiven all the changes to the file by the previous patches we are basically fully (or almost fully) adapting this code to Xen.
Quoted 2 lines
So at that point I wonder if we should just as well make these changes (e.g. s/of_phandle_args/dt_phandle_args/g) to the code too.
I have already accepted the fact that keeping Linux code as-is is nearly impossible without much workaround :). The benefits tends to also limited as we noticed for the SMMU driver.
Quoted 7 lines
I would like to point out that this may make quite difficult (if not impossible) to revert the previous patches which remove support for some features (e.g. atomic, MSI, ATS).
If we are going to adapt the code to Xen (I'd like to keep Linux code style though), then I think we should consider to keep code that may be useful in the near future (at least MSI, ATS).
(I am fine with keeping the Linux code style.)
We could try to keep the code as similar to Linux as possible. This didn't work out in the past.
Otherwise, we could fully adapt the driver to Xen. If we fully adapt the driver to Xen (code style aside) it is better to be consistent and also do substitutions like s/of_phandle_args/dt_phandle_args/g. Then the policy becomes clear: the code comes from Linux but it is 100% adapted to Xen.
Now the question about what to do about the MSI and ATS code is a good one. We know that we are going to want that code at some point in the next 2 years. Like you wrote, if we fully adapt the code to Xen and remove MSI and ATS code, then it is going to be harder to add it back.
So maybe keeping the MSI and ATS code for now, even if it cannot work, would be better. I think this strategy works well if the MSI and ATS code can be disabled easily, i.e. with a couple of lines of code in the init function rather than #ifdef everywhere. It doesn't work well if we have to add #ifdef everywhere.
It looks like MSI could be disabled adding a couple of lines to
arm_smmu_setup_msis.Similarly ATS seems to be easy to disable by forcing
ats_enabledto false.
Quoted 7 lines
Quoted 1 line
So yes, this looks like a good way forward. Rahul, what do you think?
I am ok to have the PCI ATS and MSI functionality in the code.
As per the discussion next version of the patch will include below modification:Please let me know if there are any suggestion overall that should be added in next version.1. Keep the PCI ATS and MSI functionality code.
I'll repeat one point I wrote above that I think it is important: please try to disable ATS and MSI without invasive changes, ideally just a couple of lines to force-disable each feature.
Quoted 3 lines
2. Make the code with XEN compatible ( remove linux compatibility functions)
3. Keep the Linux coding style for code imported from Linux.
4. Fix all comments.
Sounds good.
Quoted 1 line
I have one query what will be coding style for new code to make driver work for XEN ?
We try to keep the same code style for the entirety of a source file. In this case, the whole driver should use Linux code style (both imported code and new code).
Hello Stefano,
Quoted 67 lines · Stefano Stabellini
Quoted 64 lines · Rahul Singh
Quoted 62 lines · Stefano Stabellini
Quoted 22 lines · Julien Grall
Quoted 14 lines · Stefano Stabellini
xen/drivers/passthrough/arm/smmu-v3.c line 63
Quoted 9 lines · Rahul Singh
xen/drivers/passthrough/arm/smmu-v3.c +6
-0+/* Alias to Xen device tree helpers */ +#define device_node dt_device_node +#define of_phandle_args dt_phandle_args +#define of_device_id dt_device_match +#define of_match_node dt_match_node +#define of_property_read_u32 (np, pname, out) (!dt_property_read_u32(np,pname, out))
xen/drivers/passthrough/arm/smmu-v3.c +2
-0+#define of_property_read_bool dt_property_read_bool +#define of_parse_phandle_with_args dt_parse_phandle_with_argsGiven all the changes to the file by the previous patches we are basically fully (or almost fully) adapting this code to Xen.
Quoted 2 lines
So at that point I wonder if we should just as well make these changes (e.g. s/of_phandle_args/dt_phandle_args/g) to the code too.
I have already accepted the fact that keeping Linux code as-is is nearly impossible without much workaround :). The benefits tends to also limited as we noticed for the SMMU driver.
Quoted 7 lines
I would like to point out that this may make quite difficult (if not impossible) to revert the previous patches which remove support for some features (e.g. atomic, MSI, ATS).
If we are going to adapt the code to Xen (I'd like to keep Linux code style though), then I think we should consider to keep code that may be useful in the near future (at least MSI, ATS).
(I am fine with keeping the Linux code style.)
We could try to keep the code as similar to Linux as possible. This didn't work out in the past.
Otherwise, we could fully adapt the driver to Xen. If we fully adapt the driver to Xen (code style aside) it is better to be consistent and also do substitutions like s/of_phandle_args/dt_phandle_args/g. Then the policy becomes clear: the code comes from Linux but it is 100% adapted to Xen.
Now the question about what to do about the MSI and ATS code is a good one. We know that we are going to want that code at some point in the next 2 years. Like you wrote, if we fully adapt the code to Xen and remove MSI and ATS code, then it is going to be harder to add it back.
So maybe keeping the MSI and ATS code for now, even if it cannot work, would be better. I think this strategy works well if the MSI and ATS code can be disabled easily, i.e. with a couple of lines of code in the init function rather than #ifdef everywhere. It doesn't work well if we have to add #ifdef everywhere.
It looks like MSI could be disabled adding a couple of lines to
arm_smmu_setup_msis.Similarly ATS seems to be easy to disable by forcing
ats_enabledto false.So yes, this looks like a good way forward. Rahul, what do you think?
Quoted 8 lines
Quoted 4 lines
I am ok to have the PCI ATS and MSI functionality in the code.
As per the discussion next version of the patch will include below modification:Please let me know if there are any suggestion overall that should be added in next version.1. Keep the PCI ATS and MSI functionality code.
I'll repeat one point I wrote above that I think it is important: please try to disable ATS and MSI without invasive changes, ideally just a couple of lines to force-disable each feature
Yes I will disable the feature.
Quoted 6 lines
Quoted 5 lines
2. Make the code with XEN compatible ( remove linux compatibility functions)
3. Keep the Linux coding style for code imported from Linux.
4. Fix all comments.
Quoted 8 lines
Sounds good.
Quoted 1 line
I have one query what will be coding style for new code to make driver work for XEN ?
We try to keep the same code style for the entirety of a source file. In this case, the whole driver should use Linux code style (both imported code and new code).
Ok.
Regards,
Rahul
Hi Rahul,
Quoted 10 lines · Rahul Singh
Add support for ARM architected SMMUv3 implementation. It is based on the Linux SMMUv3 driver.
Major differences with regard to Linux driver are as follows:
1. Only Stage-2 translation is supported as compared to the Linux driver that supports both Stage-1 and Stage-2 translations.
2. Use P2M page table instead of creating one as SMMUv3 has the capability to share the page tables with the CPU.
3. Tasklets are used in place of threaded IRQ's in Linux for event queue and priority queue IRQ handling.
On the previous version, we discussed that using tasklets is not a suitable replacement for threaded IRQs. What's the plan to address it?
Quoted 39 lines
4. Latest version of the Linux SMMUv3 code implements the commands queue access functions based on atomic operations implemented in Linux. Atomic functions used by the commands queue access functions are not implemented in XEN therefore we decided to port the earlier version of the code. Once the proper atomic operations will be available in XEN the driver can be updated.
5. Driver is currently supported as Tech Preview.Signed-off-by Rahul Singh <[email protected]>--- MAINTAINERS | 6 + SUPPORT.md | 1 + xen/drivers/passthrough/Kconfig | 10 + xen/drivers/passthrough/arm/Makefile | 1 + xen/drivers/passthrough/arm/smmu-v3.c | 986 +++++++++++++++++++++----- 5 files changed, 814 insertions(+), 190 deletions(-)MAINTAINERS +6
-0@@ -249,6 +249,12 @@ F: xen/include/asm-arm/ F: xen/include/public/arch-arm/ F: xen/include/public/arch-arm.h +ARM SMMUv3 +M: Bertrand Marquis <[email protected]> +M: Rahul Singh <[email protected]> +S: Supported +F: xen/drivers/passthrough/arm/smmu-v3.c + Change Log M: Paul Durrant <[email protected]> R: Community Manager <[email protected]>SUPPORT.md +0
-0
SUPPORT.md For the Cortex A57 r0p0 - r1p1, see Errata 832075. · line 71
Quoted 4 lines
SUPPORT.md +1
-0@@ -68,6 +68,7 @@ For the Cortex A57 r0p0 - r1p1, see Errata 832075. Status, ARM SMMUv1: Supported, not security supported Status, ARM SMMUv2: Supported, not security supported Status, Renesas IPMMU-VMSA: Supported, not security supported + Status, ARM SMMUv3: Tech Preview
Please move this right after "ARM SMMUv2".
Quoted 38 lines
SUPPORT.md +0
-0### ARM/GICv3 ITSxen/drivers/passthrough/Kconfig +10
-0@@ -13,6 +13,16 @@ config ARM_SMMU Say Y here if your SoC includes an IOMMU device implementing the ARM SMMU architecture. +config ARM_SMMU_V3 + bool "ARM Ltd. System MMU Version 3 (SMMUv3) Support" if EXPERT + depends on ARM_64 + ---help--- + Support for implementations of the ARM System MMU architecture + version 3. + + Say Y here if your system includes an IOMMU device implementing + the ARM SMMUv3 architecture. + config IPMMU_VMSA bool "Renesas IPMMU-VMSA found in R-Car Gen3 SoCs" depends on ARM_64xen/drivers/passthrough/arm/Makefile +1
-0@@ -1,3 +1,4 @@ obj-y += iommu.o iommu_helpers.o iommu_fwspec.o obj-$(CONFIG_ARM_SMMU) += smmu.o obj-$(CONFIG_IPMMU_VMSA) += ipmmu-vmsa.o +obj-$(CONFIG_ARM_SMMU_V3) += smmu-v3.oxen/drivers/passthrough/arm/smmu-v3.c +0
-0
xen/drivers/passthrough/arm/smmu-v3.c line 9
Quoted 9 lines
xen/drivers/passthrough/arm/smmu-v3.c +5
-1@@ -2,36 +2,280 @@/* * IOMMU API for ARM architected SMMUv3 implementations. * -* Copyright (C) 2015 ARM Limited +* Based on Linux's SMMUv3 driver: +* drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c +* commit: 951cbbc386ff01b50da4f46387e994e81d9ab431 +* and Xen's SMMU driver: +* xen/drivers/passthrough/arm/smmu.c
I would suggest to list the major differences here as well.
xen/drivers/passthrough/arm/smmu-v3.c line 11
Quoted 3 lines
xen/drivers/passthrough/arm/smmu-v3.c +1
-1* -* Author: Will Deacon <[email protected]> +* Copyright (C) 2015 ARM Limited Will Deacon <[email protected]>
Why did you merge the Author and copyright line?
Quoted 26 lines
xen/drivers/passthrough/arm/smmu-v3.c +24
-1* -* This driver is powered by bad coffee and bombay mix. +* Copyright (C) 2020 Arm Ltd. +* +* This program is free software; you can redistribute it and/or modify +* it under the terms of the GNU General Public License version 2 as +* published by the Free Software Foundation. +* +* This program is distributed in the hope that it will be useful, +* but WITHOUT ANY WARRANTY; without even the implied warranty of +* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +* GNU General Public License for more details. +* +* You should have received a copy of the GNU General Public License +* along with this program. If not, see <http://www.gnu.org/licenses/>. +* +*/ + +#include <xen/acpi.h> +#include <xen/config.h> +#include <xen/delay.h> +#include <xen/errno.h> +#include <xen/err.h> +#include <xen/irq.h> +#include <xen/lib.h> +#include <xen/list.h>
xen/drivers/passthrough/arm/smmu-v3.c line 46
Quoted 10 lines
xen/drivers/passthrough/arm/smmu-v3.c +10
-0+#include <xen/mm.h> +#include <xen/rbtree.h> +#include <xen/sched.h> +#include <xen/sizes.h> +#include <xen/vmap.h> +#include <asm/atomic.h> +#include <asm/device.h> +#include <asm/io.h> +#include <asm/platform.h> +#include <asm/iommu_fwspec.h>
All the headers seem to be alphabetically ordered but this one.
xen/drivers/passthrough/arm/smmu-v3.c line 48
Quoted 2 lines
xen/drivers/passthrough/arm/smmu-v3.c +2
-0+ +/* Linux compatibility functions. */
Some of the helpers here seem to be similar to the SMMU driver. Can we have an header that can be shared between the two?
Quoted 11 lines
xen/drivers/passthrough/arm/smmu-v3.c +11
-0+typedef paddr_t dma_addr_t ; +typedef unsigned int gfp_t ; + +#define platform_device device + +#define GFP_KERNEL 0 + +/* Alias to Xen device tree helpers */ +#define device_node dt_device_node +#define of_phandle_args dt_phandle_args +#define of_device_id dt_device_match
xen/drivers/passthrough/arm/smmu-v3.c line 69
Quoted 10 lines
xen/drivers/passthrough/arm/smmu-v3.c +10
-0+#define of_match_node dt_match_node +#define of_property_read_u32 (np, pname, out) (!dt_property_read_u32(np, pname, out)) +#define of_property_read_bool dt_property_read_bool +#define of_parse_phandle_with_args dt_parse_phandle_with_args + +/* Alias to Xen lock functions */ +#define mutex spinlock +#define mutex_init spin_lock_init +#define mutex_lock spin_lock +#define mutex_unlock spin_unlock
Hmm... mutex are not spinlock. Can you explain why this is fine to switch to spinlock?
Quoted 50 lines
xen/drivers/passthrough/arm/smmu-v3.c +50
-0+ +/* Alias to Xen time functions */ +#define ktime_t s_time_t +#define ktime_get () (NOW()) +#define ktime_add_us (t,i) (t + MICROSECS(i)) +#define ktime_compare (t,i) (t > (i)) + +/* Alias to Xen allocation helpers */ +#define kzalloc (size, flags) _xzalloc(size, sizeof(void *)) +#define kfree xfree +#define devm_kzalloc (dev, size, flags) _xzalloc(size, sizeof(void *)) + +/* Device logger functions */ +#define dev_name (dev) dt_node_full_name(dev->of_node) +#define dev_dbg (dev, fmt, ...) \ + printk(XENLOG_DEBUG "SMMUv3: %s: " fmt, dev_name(dev), ## __VA_ARGS__) +#define dev_notice (dev, fmt, ...) \ + printk(XENLOG_INFO "SMMUv3: %s: " fmt, dev_name(dev), ## __VA_ARGS__) +#define dev_warn (dev, fmt, ...) \ + printk(XENLOG_WARNING "SMMUv3: %s: " fmt, dev_name(dev), ## __VA_ARGS__) +#define dev_err (dev, fmt, ...) \ + printk(XENLOG_ERR "SMMUv3: %s: " fmt, dev_name(dev), ## __VA_ARGS__) +#define dev_info (dev, fmt, ...) \ + printk(XENLOG_INFO "SMMUv3: %s: " fmt, dev_name(dev), ## __VA_ARGS__) +#define dev_err_ratelimited (dev, fmt, ...) \ + printk(XENLOG_ERR "SMMUv3: %s: " fmt, dev_name(dev), ## __VA_ARGS__) + +/* +* Periodically poll an address and wait between reads in us until a +* condition is met or a timeout occurs. +*/ +#define readx_poll_timeout (op, addr, val, cond, sleep_us, timeout_us) \ +({ \ + s_time_t deadline = NOW() + MICROSECS(timeout_us); \ + for (;;) { \ + (val) = op(addr); \ + if (cond) \ + break; \ + if (NOW() > deadline) { \ + (val) = op(addr); \ + break; \ + } \ + udelay(sleep_us); \ + } \ + (cond) ? 0 : -ETIMEDOUT; \ +}) + +#define readl_relaxed_poll_timeout (addr, val, cond, delay_us, timeout_us) \ + readx_poll_timeout(readl_relaxed, addr, val, cond, delay_us, timeout_us) +
xen/drivers/passthrough/arm/smmu-v3.c line 129
Quoted 10 lines
xen/drivers/passthrough/arm/smmu-v3.c +10
-0+#define FIELD_PREP (_mask, _val) \ + (((typeof(_mask))(_val) << (__builtin_ffsll(_mask) - 1)) & (_mask)) + +#define FIELD_GET (_mask, _reg) \ + (typeof(_mask))(((_reg) & (_mask)) >> (__builtin_ffsll(_mask) - 1)) + +#define WRITE_ONCE (x, val) \ +do { \ + *(volatile typeof(x) *)&(x) = (val); \ +} while (0)
Please implement it with write_atomic() or ACCESS_ONCE().
Quoted 12 lines
xen/drivers/passthrough/arm/smmu-v3.c +11
-0+ +/* Xen: Stub out DMA domain related functions */ +#define iommu_get_dma_cookie (dom) 0 +#define iommu_put_dma_cookie (dom) + +/* +* Helpers for DMA allocation. Just the function name is reused for +* porting code, these allocation are not managed allocations */ +static void *dmam_alloc_coherent (struct device *dev,size_t size, +paddr_t *dma_handle,gfp_t gfp) +{
xen/drivers/passthrough/arm/smmu-v3.c line 151
Quoted 10 lines
xen/drivers/passthrough/arm/smmu-v3.c +10
-0+void *vaddr; +unsigned long alignment = size; + +/* +* _xzalloc requires that the (align & (align -1)) = 0. Most of the +* allocations in SMMU code should send the right value for size. In +* case this is not true print a warning and align to the size of a +* (void *) +*/ +if ( size & (size -1 ) )
We should use the same coding style within the file. As the file is imported from Linux, new code should follow Linux coding style.
Quoted 61 lines
xen/drivers/passthrough/arm/smmu-v3.c +61
-0+ { +printk (XENLOG_WARNING "SMMUv3: Fixing alignment for the DMA buffer\n" ); + alignment =sizeof (void *); + } + + vaddr =_xzalloc (size, alignment); +if ( !vaddr ) + { +printk (XENLOG_ERR "SMMUv3: DMA allocation failed\n" ); +return NULL ; + } + + *dma_handle =virt_to_maddr (vaddr); + +return vaddr; +} + +/* Xen: Type definitions for iommu_domain */ +#define IOMMU_DOMAIN_UNMANAGED 0 +#define IOMMU_DOMAIN_DMA 1 +#define IOMMU_DOMAIN_IDENTITY 2 + +/* Xen specific code. */ +struct iommu_domain { +/* Runtime SMMU configuration for this iommu_domain */ +atomic_t ref; +/* +* Used to link iommu_domain contexts for a same domain. +* There is at least one per-SMMU to used by the domain. +*/ +struct list_head list; +}; + +/* Describes information required for a Xen domain */ +struct arm_smmu_xen_domain { +spinlock_t lock; + +/* List of iommu domains associated to this domain */ +struct list_head contexts; +}; + +/* +* Information about each device stored in dev->archdata.iommu +* The dev->archdata.iommu stores the iommu_domain (runtime configuration of +* the SMMU). +*/ +struct arm_smmu_xen_device { +struct iommu_domain *domain; +}; + +/* Keep a list of devices associated with this driver */ +static DEFINE_SPINLOCK (arm_smmu_devices_lock); +static LIST_HEAD (arm_smmu_devices); + + +static inline void *dev_iommu_priv_get (struct device *dev) +{ +struct iommu_fwspec *fwspec =dev_iommu_fwspec_get (dev); + +return fwspec && fwspec->iommu_priv ? fwspec->iommu_priv :NULL ; +}
xen/drivers/passthrough/arm/smmu-v3.c line 222
Quoted 10 lines
xen/drivers/passthrough/arm/smmu-v3.c +10
-0+ +static inline void dev_iommu_priv_set (struct device *dev,void *priv) +{ +struct iommu_fwspec *fwspec =dev_iommu_fwspec_get (dev); + + fwspec->iommu_priv = priv; +} + +int dt_property_match_string (const struct dt_device_node *np, +const char *propname,const char *string)
I think this should be implemented in device_tree.c
Quoted 158 lines
xen/drivers/passthrough/arm/smmu-v3.c +68
-31+{ +const struct dt_property *dtprop =dt_find_property (np, propname,NULL ); +size_t l; +int i; +const char *p, *end; + +if ( !dtprop ) +return -EINVAL ; + +if ( !dtprop->value ) +return -ENODATA ; + + p = dtprop->value; + end = p + dtprop->length; + +for ( i =0 ; p < end; i++, p += l ) + { + l =strnlen (p, end - p) +1 ; + +if ( p + l > end ) +return -EILSEQ ; + +if (strcmp (string, p) ==0 ) +return i;/* Found it; return index */ + } + +return -ENODATA ; +} + +static int platform_get_irq_byname_optional (struct device *dev, +const char *name) +{ +int index, ret; +struct dt_device_node *np =dev_to_dt (dev); + +if (unlikely (!name) ) +return -EINVAL ; + + index =dt_property_match_string (np,"interrupt-names" , name); +if ( index <0 ) + { +dev_info (dev,"IRQ %s not found\n" , name); +return index; + } -#include <linux/acpi.h> -#include <linux/acpi_iort.h> -#include <linux/bitfield.h> -#include <linux/bitops.h> -#include <linux/crash_dump.h> -#include <linux/delay.h> -#include <linux/dma-iommu.h> -#include <linux/err.h> -#include <linux/interrupt.h> -#include <linux/io-pgtable.h> -#include <linux/iommu.h> -#include <linux/iopoll.h> -#include <linux/module.h> -#include <linux/msi.h> -#include <linux/of.h> -#include <linux/of_address.h> -#include <linux/of_iommu.h> -#include <linux/of_platform.h> -#include <linux/pci.h> -#include <linux/pci-ats.h> -#include <linux/platform_device.h> - -#include <linux/amba/bus.h> + ret =platform_get_irq (np, index); +if ( ret <0 ) + { +dev_err (dev,"failed to get irq index %d\n" , index); +return -ENODEV ; + } + +return ret; +} + +/* Start of Linux SMMUv3 code */ /* MMIO registers */ #define ARM_SMMU_IDR0 0x0@@ -507,6 +751,7 @@ struct arm_smmu_s2_cfg {u16 vmid;u64 vttbr;u64 vtcr; +struct domain *domain; };struct arm_smmu_strtab_cfg {@@ -567,8 +812,13 @@ struct arm_smmu_device {struct arm_smmu_strtab_cfg strtab_cfg; -/* IOMMU core code handle */ -struct iommu_device iommu; +/* Need to keep a list of SMMU devices */ +struct list_head devices; + +/* Tasklets for handling evts/faults and pci page request IRQs*/ +struct tasklet evtq_irq_tasklet; +struct tasklet priq_irq_tasklet; +struct tasklet combined_irq_tasklet; };/* SMMU private data for each master */ @@ -1110,7 +1360,7 @@ static int arm_smmu_init_l2_strtab(struct arm_smmu_device *smmu, u32 sid) }/* IRQ and event handlers */ -static irqreturn_t arm_smmu_evtq_thread (int irq,void *dev) +static void arm_smmu_evtq_thread (void *dev) {int i;struct arm_smmu_device *smmu = dev;@@ -1140,7 +1390,6 @@ static irqreturn_t arm_smmu_evtq_thread(int irq, void *dev)/* Sync our overflow flag, as we believe we're up to speed */ llq->cons =Q_OVF (llq->prod) |Q_WRP (llq, llq->cons) |Q_IDX (llq, llq->cons); -return IRQ_HANDLED ; }static void arm_smmu_handle_ppr (struct arm_smmu_device *smmu,u64 *evt)@@ -1181,7 +1430,7 @@ static void arm_smmu_handle_ppr(struct arm_smmu_device *smmu, u64 *evt) } } -static irqreturn_t arm_smmu_priq_thread (int irq,void *dev) +static void arm_smmu_priq_thread (void *dev) {struct arm_smmu_device *smmu = dev;struct arm_smmu_queue *q = &smmu->priq.q;@@ -1200,12 +1449,12 @@ static irqreturn_t arm_smmu_priq_thread(int irq, void *dev) llq->cons =Q_OVF (llq->prod) |Q_WRP (llq, llq->cons) |Q_IDX (llq, llq->cons);queue_sync_cons_out (q); -return IRQ_HANDLED ; }static int arm_smmu_device_disable (struct arm_smmu_device *smmu); -static irqreturn_t arm_smmu_gerror_handler (int irq,void *dev) +static void arm_smmu_gerror_handler (int irq,void *dev, +struct cpu_user_regs *regs) {u32 gerror, gerrorn, active;struct arm_smmu_device *smmu = dev;@@ -1215,7 +1464,7 @@ static irqreturn_t arm_smmu_gerror_handler(int irq, void *dev) active = gerror ^ gerrorn;if (!(active &GERROR_ERR_MASK )) -return IRQ_NONE ;/* No errors pending */ +return ;/* No errors pending */ dev_warn(smmu->dev, "unexpected global error reported (0x%08x), this could be serious\n",@@ -1248,26 +1497,42 @@ static irqreturn_t arm_smmu_gerror_handler(int irq, void *dev)arm_smmu_cmdq_skip_err (smmu);
xen/drivers/passthrough/arm/smmu-v3.c static irqreturn_t arm_smmu_gerror_handler(int irq, void *dev) · line 1505
Quoted 10 lines
xen/drivers/passthrough/arm/smmu-v3.c +4
-2writel (gerror, smmu->base +ARM_SMMU_GERRORN ); -return IRQ_HANDLED ; } -static irqreturn_t arm_smmu_combined_irq_thread (int irq,void *dev) +static void arm_smmu_combined_irq_handler (int irq,void *dev, +struct cpu_user_regs *regs) +{ +struct arm_smmu_device *smmu = (struct arm_smmu_device *)dev;
The cast is not necessary.
Quoted 16 lines
xen/drivers/passthrough/arm/smmu-v3.c +8
-4+ +arm_smmu_gerror_handler (irq, dev, regs); + +tasklet_schedule (&(smmu->combined_irq_tasklet)); +} + +static void arm_smmu_combined_irq_thread (void *dev) {struct arm_smmu_device *smmu = dev; -arm_smmu_evtq_thread (irq, dev); +arm_smmu_evtq_thread (dev);if (smmu->features &ARM_SMMU_FEAT_PRI ) -arm_smmu_priq_thread (irq, dev); - -return IRQ_HANDLED ;
xen/drivers/passthrough/arm/smmu-v3.c static irqreturn_t arm_smmu_gerror_handler(int irq, void *dev) · line 1524
Quoted 10 lines
xen/drivers/passthrough/arm/smmu-v3.c +4
-3+arm_smmu_priq_thread (dev); } -static irqreturn_t arm_smmu_combined_irq_handler (int irq,void *dev) +static void arm_smmu_evtq_irq_tasklet (int irq,void *dev, +struct cpu_user_regs *regs) { -arm_smmu_gerror_handler (irq, dev); -return IRQ_WAKE_THREAD ; +struct arm_smmu_device *smmu = (struct arm_smmu_device *)dev;
Ditto.
xen/drivers/passthrough/arm/smmu-v3.c static irqreturn_t arm_smmu_gerror_handler(int irq, void *dev) · line 1532
Quoted 8 lines
xen/drivers/passthrough/arm/smmu-v3.c +6
-0+ +tasklet_schedule (&(smmu->evtq_irq_tasklet)); } +static void arm_smmu_priq_irq_tasklet (int irq,void *dev, +struct cpu_user_regs *regs) +{ +struct arm_smmu_device *smmu = (struct arm_smmu_device *)dev;
Ditto.
Quoted 7 lines
xen/drivers/passthrough/arm/smmu-v3.c +3
-0+ +tasklet_schedule (&(smmu->priq_irq_tasklet)); +}/* IO_PGTABLE API */ static void arm_smmu_tlb_inv_context (void *cookie)
xen/drivers/passthrough/arm/smmu-v3.c static void arm_smmu_domain_free(struct iommu_domain *domain) · line 1625
Quoted 9 lines
xen/drivers/passthrough/arm/smmu-v3.c +2
-2@@ -1354,27 +1619,69 @@ static void arm_smmu_domain_free(struct iommu_domain *domain) }static int arm_smmu_domain_finalise_s2 (struct arm_smmu_domain *smmu_domain, -struct arm_smmu_master *master, -struct io_pgtable_cfg *pgtbl_cfg) +struct arm_smmu_master *master) {int vmid; +u64 reg;
I think uint32_t is sufficient here.
xen/drivers/passthrough/arm/smmu-v3.c static void arm_smmu_domain_free(struct iommu_domain *domain) · line 1630
Quoted 5 lines
xen/drivers/passthrough/arm/smmu-v3.c +2
-0struct arm_smmu_device *smmu = smmu_domain->smmu;struct arm_smmu_s2_cfg *cfg = &smmu_domain->s2_cfg; +/* VTCR */ + reg =VTCR_RES1 |VTCR_SH0_IS |VTCR_IRGN0_WBWA |VTCR_ORGN0_WBWA ;
VTCR_RES1 will set bit 31 to 1. However, from the spec it looks like the equivalent bit in the entry (see 5.2 in ARM IHI 0070C.a) will be RES0.
xen/drivers/passthrough/arm/smmu-v3.c static void arm_smmu_domain_free(struct iommu_domain *domain) · line 1642
Quoted 12 lines
xen/drivers/passthrough/arm/smmu-v3.c +12
-0+ +switch (PAGE_SIZE ) { +case SZ_4K : + reg |=VTCR_TG0_4K ; +break ; +case SZ_16K : + reg |=VTCR_TG0_16K ; +break ; +case SZ_64K : + reg |=VTCR_TG0_4K ; +break ; + }
I would just handle 4K here and add a BUILD_BUG_ON(PAGE_SIZE != SZ_4K).
xen/drivers/passthrough/arm/smmu-v3.c static void arm_smmu_domain_free(struct iommu_domain *domain) · line 1643
Quoted 1 line
xen/drivers/passthrough/arm/smmu-v3.c +1
-0+ > + switch (smmu->oas) {
AFAICT smmu->oas and ...
Quoted 14 lines
xen/drivers/passthrough/arm/smmu-v3.c +14
-0+case 32 : + reg |=VTCR_PS (_AC (0x0 ,ULL )); +break ; +case 36 : + reg |=VTCR_PS (_AC (0x1 ,ULL )); +break ; +case 40 : + reg |=VTCR_PS (_AC (0x2 ,ULL )); +break ; +case 42 : + reg |=VTCR_PS (_AC (0x3 ,ULL )); +break ; +case 44 : + reg |=VTCR_PS (_AC (0x4 ,ULL ));
xen/drivers/passthrough/arm/smmu-v3.c static void arm_smmu_domain_free(struct iommu_domain *domain) · line 1668
Quoted 10 lines
xen/drivers/passthrough/arm/smmu-v3.c +10
-0+break ; +case 48 : + reg |=VTCR_PS (_AC (0x5 ,ULL )); +break ; +case 52 : + reg |=VTCR_PS (_AC (0x6 ,ULL )); +break ; + } + + reg |=VTCR_T0SZ (64ULL - smmu->ias);
... are directly taken from the SMMU configuration. However, as we share the P2M, we need to make sure the value match what the CPU is using.
For the IAS, you will want to use p2m_ipa_bits and for the output, we will want to cap to PADDR_BITS.
xen/drivers/passthrough/arm/smmu-v3.c static void arm_smmu_domain_free(struct iommu_domain *domain) · line 1669
Quoted 1 line
xen/drivers/passthrough/arm/smmu-v3.c +1
-0+ reg |=VTCR_SL0 (0x2 );
Similar to above, the starting level will depend on how the P2M was configured.
xen/drivers/passthrough/arm/smmu-v3.c static void arm_smmu_domain_free(struct iommu_domain *domain) · line 1670
Quoted 1 line
xen/drivers/passthrough/arm/smmu-v3.c +1
-0+ reg |=VTCR_VS ;
AFAICT, the bit 179 (bit 19 in the word) is indicating whether AArch32 or AArch64 translation table is used. However, bit 19 in VTCR_EL2 indicates whether we are using 8-bit or 16-bit VMID.
xen/drivers/passthrough/arm/smmu-v3.c static void arm_smmu_domain_free(struct iommu_domain *domain) · line 1672
Quoted 2 lines
xen/drivers/passthrough/arm/smmu-v3.c +2
-0+ + cfg->vtcr = reg;
It would be better to initialize vtcr exactly at the same place as Linux does. This would make easier to match the code.
Quoted 41 lines
xen/drivers/passthrough/arm/smmu-v3.c +8
-17+ vmid =arm_smmu_bitmap_alloc (smmu->vmid_map, smmu->vmid_bits);if (vmid <0 )return vmid; + cfg->vmid = (u16 )vmid; + + cfg->vttbr =page_to_maddr (cfg->domain->arch.p2m.root); + +printk (XENLOG_DEBUG +"SMMUv3: d%u: vmid 0x%x vtcr 0x%" PRIpaddr" p2maddr 0x%" PRIpaddr"\n" , + cfg->domain->domain_id, cfg->vmid, cfg->vtcr, cfg->vttbr); - vtcr = &pgtbl_cfg->arm_lpae_s2_cfg.vtcr; - cfg->vmid = (u16 )vmid; - cfg->vttbr = pgtbl_cfg->arm_lpae_s2_cfg.vttbr; - cfg->vtcr =FIELD_PREP (STRTAB_STE_2_VTCR_S2T0SZ , vtcr->tsz) | -FIELD_PREP (STRTAB_STE_2_VTCR_S2SL0 , vtcr->sl) | -FIELD_PREP (STRTAB_STE_2_VTCR_S2IR0 , vtcr->irgn) | -FIELD_PREP (STRTAB_STE_2_VTCR_S2OR0 , vtcr->orgn) | -FIELD_PREP (STRTAB_STE_2_VTCR_S2SH0 , vtcr->sh) | -FIELD_PREP (STRTAB_STE_2_VTCR_S2TG , vtcr->tg) | -FIELD_PREP (STRTAB_STE_2_VTCR_S2PS , vtcr->ps);return 0 ; }@@ -1382,28 +1689,12 @@ static int arm_smmu_domain_finalise(struct iommu_domain *domain, struct arm_smmu_master *master) {int ret; -unsigned long ias, oas; -int (*finalise_stage_fn)(struct arm_smmu_domain *, -struct arm_smmu_master *, -struct io_pgtable_cfg *);struct arm_smmu_domain *smmu_domain =to_smmu_domain (domain); -struct arm_smmu_device *smmu = smmu_domain->smmu;/* Restrict the stage to what we can actually support */ smmu_domain->stage =ARM_SMMU_DOMAIN_S2 ; -switch (smmu_domain->stage) { -case ARM_SMMU_DOMAIN_NESTED :
xen/drivers/passthrough/arm/smmu-v3.c static int arm_smmu_domain_finalise(struct iommu_domain *domain, · line 1406
Quoted 10 lines
xen/drivers/passthrough/arm/smmu-v3.c +0
-10-case ARM_SMMU_DOMAIN_S2 : - ias = smmu->ias; - oas = smmu->oas; - finalise_stage_fn = arm_smmu_domain_finalise_s2; -break ; -default : -return -EINVAL ; - } - - ret =finalise_stage_fn (smmu_domain, master, &pgtbl_cfg);
It is not entirely clear why this code is removed here and not the previous patch?
xen/drivers/passthrough/arm/smmu-v3.c static int arm_smmu_init_one_queue(struct arm_smmu_device *smmu, · line 1847
Quoted 10 lines
xen/drivers/passthrough/arm/smmu-v3.c +2
-1+ ret =arm_smmu_domain_finalise_s2 (smmu_domain, master);if (ret <0 ) {return ret; }@@ -1553,7 +1844,8 @@ static int arm_smmu_init_one_queue(struct arm_smmu_device *smmu,return -ENOMEM ; } -if (!WARN_ON (q->base_dma & (qsz -1 ))) { +WARN_ON (q->base_dma & (qsz -1 ));
This is a call to rework how our WARN_ON() in Xen.
Quoted 114 lines
xen/drivers/passthrough/arm/smmu-v3.c +31
-25+if (unlikely (q->base_dma & (qsz -1 ))) {dev_info (smmu->dev,"allocated %u entries for %s\n" ,1 << q->llq.max_n_shift, name); }@@ -1758,9 +2050,7 @@ static void arm_smmu_setup_unique_irqs(struct arm_smmu_device *smmu)/* Request interrupt lines */ irq = smmu->evtq.q.irq;if (irq) { - ret =devm_request_threaded_irq (smmu->dev, irq,NULL , - arm_smmu_evtq_thread, -IRQF_ONESHOT , + ret =request_irq (irq,0 , arm_smmu_evtq_irq_tasklet,"arm-smmu-v3-evtq" , smmu);if (ret <0 )dev_warn (smmu->dev,"failed to enable evtq irq\n" );@@ -1770,8 +2060,8 @@ static void arm_smmu_setup_unique_irqs(struct arm_smmu_device *smmu) irq = smmu->gerr_irq;if (irq) { - ret =devm_request_irq (smmu->dev, irq, arm_smmu_gerror_handler, -0 ,"arm-smmu-v3-gerror" , smmu); + ret =request_irq (irq,0 , arm_smmu_gerror_handler, +"arm-smmu-v3-gerror" , smmu);if (ret <0 )dev_warn (smmu->dev,"failed to enable gerror irq\n" ); }else {@@ -1781,11 +2071,8 @@ static void arm_smmu_setup_unique_irqs(struct arm_smmu_device *smmu)if (smmu->features &ARM_SMMU_FEAT_PRI ) { irq = smmu->priq.q.irq;if (irq) { - ret =devm_request_threaded_irq (smmu->dev, irq,NULL , - arm_smmu_priq_thread, -IRQF_ONESHOT , -"arm-smmu-v3-priq" , - smmu); + ret =request_irq (irq,0 , arm_smmu_priq_irq_tasklet, +"arm-smmu-v3-priq" , smmu);if (ret <0 )dev_warn (smmu->dev,"failed to enable priq irq\n" );@@ -1814,11 +2101,8 @@ static int arm_smmu_setup_irqs(struct arm_smmu_device *smmu)* Cavium ThunderX2 implementation doesn't support unique irq * lines. Use a single irq line for all the SMMUv3 interrupts. */ - ret =devm_request_threaded_irq (smmu->dev, irq, - arm_smmu_combined_irq_handler, - arm_smmu_combined_irq_thread, -IRQF_ONESHOT , -"arm-smmu-v3-combined-irq" , smmu); + ret =request_irq (irq,0 , arm_smmu_combined_irq_handler, +"arm-smmu-v3-combined-irq" , smmu);if (ret <0 )dev_warn (smmu->dev,"failed to enable combined irq\n" ); }else @@ -1857,7 +2141,7 @@ static int arm_smmu_device_reset(struct arm_smmu_device *smmu, bool bypass) reg =readl_relaxed (smmu->base +ARM_SMMU_CR0 );if (reg &CR0_SMMUEN ) {dev_warn (smmu->dev,"SMMU currently enabled! Resetting...\n" ); -WARN_ON (is_kdump_kernel () && !disable_bypass); +WARN_ON (!disable_bypass);arm_smmu_update_gbpa (smmu,GBPA_ABORT ,0 ); }@@ -1952,8 +2236,11 @@ static int arm_smmu_device_reset(struct arm_smmu_device *smmu, bool bypass)return ret; } -if (is_kdump_kernel ()) - enables &= ~(CR0_EVTQEN |CR0_PRIQEN ); +/* Initialize tasklets for threaded IRQs*/ +tasklet_init (&smmu->evtq_irq_tasklet, arm_smmu_evtq_thread, smmu); +tasklet_init (&smmu->priq_irq_tasklet, arm_smmu_priq_thread, smmu); +tasklet_init (&smmu->combined_irq_tasklet, arm_smmu_combined_irq_thread, + smmu);/* Enable the SMMU interface, or ensure bypass */ if (!bypass || disable_bypass) {@@ -2195,7 +2482,7 @@ static inline int arm_smmu_device_acpi_probe(struct platform_device *pdev,static int arm_smmu_device_dt_probe (struct platform_device *pdev,struct arm_smmu_device *smmu) { -struct device *dev = &pdev->dev; +struct device *dev = pdev;u32 cells;int ret = -EINVAL ;@@ -2219,130 +2506,449 @@ static unsigned long arm_smmu_resource_size(struct arm_smmu_device *smmu)return SZ_128K ; } +/* Start of Xen specific code. */ static int arm_smmu_device_probe (struct platform_device *pdev) { -int irq, ret; -struct resource *res; -resource_size_t ioaddr; -struct arm_smmu_device *smmu; -struct device *dev = &pdev->dev; -bool bypass; +int irq, ret; +paddr_t ioaddr, iosize; +struct arm_smmu_device *smmu; +bool bypass; + + smmu =devm_kzalloc (dev,sizeof (*smmu),GFP_KERNEL ); +if ( !smmu ) + { +dev_err (pdev,"failed to allocate arm_smmu_device\n" ); +return -ENOMEM ; + } + smmu->dev = pdev; + +if ( pdev->of_node ) + {
xen/drivers/passthrough/arm/smmu-v3.c static unsigned long arm_smmu_resource_size(struct arm_smmu_device *smmu) · line 2536
Quoted 10 lines
xen/drivers/passthrough/arm/smmu-v3.c +10
-0+ ret =arm_smmu_device_dt_probe (pdev, smmu); + }else + { + ret =arm_smmu_device_acpi_probe (pdev, smmu); +if ( ret == -ENODEV ) +return ret; + } + +/* Set bypass mode according to firmware probing result */ + bypass = !!ret;
AFAICT, bypass would be set if the device-tree is buggy. For Xen, I think it would be saner to not continue as this would break isolation.
Quoted 6 lines
xen/drivers/passthrough/arm/smmu-v3.c +6
-0+ +/* Base address */ + ret =dt_device_get_address (dev_to_dt (pdev),0 , &ioaddr, &iosize); +if ( ret ) +return -ENODEV ; +
xen/drivers/passthrough/arm/smmu-v3.c static unsigned long arm_smmu_resource_size(struct arm_smmu_device *smmu) · line 2552
Quoted 10 lines
xen/drivers/passthrough/arm/smmu-v3.c +10
-0+if ( iosize <arm_smmu_resource_size (smmu) ) + { +dev_err (pdev,"MMIO region too small (%lx)\n" , iosize); +return -EINVAL ; + } + +/* +* Don't map the IMPLEMENTATION DEFINED regions, since they may contain +* the PMCG registers which are reserved by the PMU driver. +*/
This comment doesn't seem to match the code. Did you intend to...
xen/drivers/passthrough/arm/smmu-v3.c static unsigned long arm_smmu_resource_size(struct arm_smmu_device *smmu) · line 2553
Quoted 1 line
xen/drivers/passthrough/arm/smmu-v3.c +1
-0+ smmu->base =ioremap_nocache (ioaddr, iosize);
... use ARM_SMMU_REG_SZ which would only map the necessary region?
Quoted 120 lines
xen/drivers/passthrough/arm/smmu-v3.c +102
-15+if (IS_ERR (smmu->base) ) +return PTR_ERR (smmu->base); + +if ( iosize >SZ_64K ) + { + smmu->page1 =ioremap_nocache (ioaddr +SZ_64K ,ARM_SMMU_REG_SZ ); +if (IS_ERR (smmu->page1) ) +return PTR_ERR (smmu->page1); + } +else + { + smmu->page1 = smmu->base; + } + +/* Interrupt lines */ + + irq =platform_get_irq_byname_optional (pdev,"combined" ); +if ( irq >0 ) + smmu->combined_irq = irq; +else + { + irq =platform_get_irq_byname_optional (pdev,"eventq" ); +if ( irq >0 ) + smmu->evtq.q.irq = irq; + + irq =platform_get_irq_byname_optional (pdev,"priq" ); +if ( irq >0 ) + smmu->priq.q.irq = irq; + + irq =platform_get_irq_byname_optional (pdev,"gerror" ); +if ( irq >0 ) + smmu->gerr_irq = irq; + } +/* Probe the h/w */ + ret =arm_smmu_device_hw_probe (smmu); +if ( ret ) +return ret; + +/* Initialise in-memory data structures */ + ret =arm_smmu_init_structures (smmu); +if ( ret ) +return ret; + +/* Reset the device */ + ret =arm_smmu_device_reset (smmu, bypass); +if ( ret ) +return ret; + +/* +* Keep a list of all probed devices. This will be used to query +* the smmu devices based on the fwnode. +*/ +INIT_LIST_HEAD (&smmu->devices); + +spin_lock (&arm_smmu_devices_lock); +list_add (&smmu->devices, &arm_smmu_devices); +spin_unlock (&arm_smmu_devices_lock); + +return 0 ; +} + +static int __must_check arm_smmu_iotlb_flush_all (struct domain *d) +{ +struct arm_smmu_xen_domain *xen_domain =dom_iommu (d)->arch.priv; +struct iommu_domain *io_domain; + +spin_lock (&xen_domain->lock); + +list_for_each_entry ( io_domain, &xen_domain->contexts, list ) + { +/* +* Only invalidate the context when SMMU is present. +* This is because the context initialization is delayed +* until a master has been added. +*/ +if (unlikely (!ACCESS_ONCE (to_smmu_domain (io_domain)->smmu)) ) +continue ; + +arm_smmu_tlb_inv_context (to_smmu_domain (io_domain)); + } - smmu =devm_kzalloc (dev,sizeof (*smmu),GFP_KERNEL ); -if (!smmu) { -dev_err (dev,"failed to allocate arm_smmu_device\n" ); -return -ENOMEM ; - } - smmu->dev = dev; +spin_unlock (&xen_domain->lock); + +return 0 ; +} + +static int __must_check arm_smmu_iotlb_flush (struct domain *d,dfn_t dfn, +unsigned long page_count, +unsigned int flush_flags) +{ +return arm_smmu_iotlb_flush_all (d); +} -if (dev->of_node) { - ret =arm_smmu_device_dt_probe (pdev, smmu); - }else { - ret =arm_smmu_device_acpi_probe (pdev, smmu); -if (ret == -ENODEV ) -return ret; - } +static struct arm_smmu_device *arm_smmu_get_by_dev (struct device *dev) +{ +struct arm_smmu_device *smmu =NULL ; -/* Set bypass mode according to firmware probing result */ - bypass = !!ret; +spin_lock (&arm_smmu_devices_lock); +list_for_each_entry ( smmu, &arm_smmu_devices, devices ) + { +if ( smmu->dev == dev ) + { +spin_unlock (&arm_smmu_devices_lock); +return smmu; + }
xen/drivers/passthrough/arm/smmu-v3.c static unsigned long arm_smmu_resource_size(struct arm_smmu_device *smmu) · line 2255
Quoted 10 lines
xen/drivers/passthrough/arm/smmu-v3.c +2
-7+ } +spin_unlock (&arm_smmu_devices_lock); -/* Base address */ - res =platform_get_resource (pdev,IORESOURCE_MEM ,0 ); -if (resource_size (res) <arm_smmu_resource_size (smmu)) { -dev_err (dev,"MMIO region too small (%pr)\n" , res); -return -EINVAL ; - } - ioaddr = res->start;
The code removed is basically the same as the one you added except the coding style change. This patch is already quite long to review, so can we please keep the change to the strict minimum?
If you want to do to clean-up then they should be done before/after.
Quoted 253 lines
xen/drivers/passthrough/arm/smmu-v3.c +171
-65+return NULL ; +} -/* -* Don't map the IMPLEMENTATION DEFINED regions, since they may contain -* the PMCG registers which are reserved by the PMU driver. -*/ - smmu->base =arm_smmu_ioremap (dev, ioaddr,ARM_SMMU_REG_SZ ); -if (IS_ERR (smmu->base)) -return PTR_ERR (smmu->base); - -if (arm_smmu_resource_size (smmu) >SZ_64K ) { - smmu->page1 =arm_smmu_ioremap (dev, ioaddr +SZ_64K , -ARM_SMMU_REG_SZ ); -if (IS_ERR (smmu->page1)) -return PTR_ERR (smmu->page1); - }else { - smmu->page1 = smmu->base; - } +/* Probing and initialisation functions */ +static struct iommu_domain *arm_smmu_get_domain (struct domain *d, +struct device *dev) +{ +struct iommu_domain *io_domain; +struct arm_smmu_domain *smmu_domain; +struct iommu_fwspec *fwspec =dev_iommu_fwspec_get (dev); +struct arm_smmu_xen_domain *xen_domain =dom_iommu (d)->arch.priv; +struct arm_smmu_device *smmu =arm_smmu_get_by_dev (fwspec->iommu_dev); + +if ( !smmu ) +return NULL ; -/* Interrupt lines */ +/* +* Loop through the &xen_domain->contexts to locate a context +* assigned to this SMMU +*/ +list_for_each_entry ( io_domain, &xen_domain->contexts, list ) + { + smmu_domain =to_smmu_domain (io_domain); +if ( smmu_domain->smmu == smmu ) +return io_domain; + } - irq =platform_get_irq_byname_optional (pdev,"combined" ); -if (irq >0 ) - smmu->combined_irq = irq; -else { - irq =platform_get_irq_byname_optional (pdev,"eventq" ); -if (irq >0 ) - smmu->evtq.q.irq = irq; +return NULL ; +} - irq =platform_get_irq_byname_optional (pdev,"priq" ); -if (irq >0 ) - smmu->priq.q.irq = irq; +static void arm_smmu_destroy_iommu_domain (struct iommu_domain *io_domain) +{ +list_del (&io_domain->list); +arm_smmu_domain_free (io_domain); +} - irq =platform_get_irq_byname_optional (pdev,"gerror" ); -if (irq >0 ) - smmu->gerr_irq = irq; - } -/* Probe the h/w */ - ret =arm_smmu_device_hw_probe (smmu); -if (ret) -return ret; +static int arm_smmu_assign_dev (struct domain *d,u8 devfn, +struct device *dev,u32 flag) +{ +int ret =0 ; +struct iommu_domain *io_domain; +struct arm_smmu_domain *smmu_domain; +struct arm_smmu_xen_domain *xen_domain =dom_iommu (d)->arch.priv; -/* Initialise in-memory data structures */ - ret =arm_smmu_init_structures (smmu); -if (ret) -return ret; +if ( !dev->archdata.iommu ) + { + dev->archdata.iommu = xzalloc(struct arm_smmu_xen_device); +if ( !dev->archdata.iommu ) +return -ENOMEM ; + } -/* Record our private device structure */ -platform_set_drvdata (pdev, smmu); +spin_lock (&xen_domain->lock); -/* Reset the device */ - ret =arm_smmu_device_reset (smmu, bypass); -if (ret) -return ret; +/* +* Check to see if an iommu_domain already exists for this xen domain +* under the same SMMU +*/ + io_domain =arm_smmu_get_domain (d, dev); +if ( !io_domain ) + { + io_domain =arm_smmu_domain_alloc (IOMMU_DOMAIN_DMA ); +if ( !io_domain ) + { + ret = -ENOMEM ; + goto out; + } -/* And we're up. Go go go! */ - ret =iommu_device_sysfs_add (&smmu->iommu, dev,NULL , -"smmu3.%pa" , &ioaddr); -if (ret) -return ret; + smmu_domain =to_smmu_domain (io_domain); + smmu_domain->s2_cfg.domain = d; -iommu_device_set_ops (&smmu->iommu, &arm_smmu_ops); -iommu_device_set_fwnode (&smmu->iommu, dev->fwnode); +/* Chain the new context to the domain */ +list_add (&io_domain->list, &xen_domain->contexts); - ret =iommu_device_register (&smmu->iommu); -if (ret) { -dev_err (dev,"Failed to register iommu\n" ); -return ret; - } + } + + ret =arm_smmu_attach_dev (io_domain, dev); +if ( ret ) + { +if ( io_domain->ref.counter ==0 ) +arm_smmu_destroy_iommu_domain (io_domain); + } +else + { +atomic_inc (&io_domain->ref); + } -return arm_smmu_set_bus_ops (&arm_smmu_ops); +out: +spin_unlock (&xen_domain->lock); +return ret; } -static int arm_smmu_device_remove (struct platform_device *pdev) +static int arm_smmu_deassign_dev (struct domain *d,struct device *dev) { -struct arm_smmu_device *smmu =platform_get_drvdata (pdev); +struct iommu_domain *io_domain =arm_smmu_get_domain (d, dev); +struct arm_smmu_xen_domain *xen_domain =dom_iommu (d)->arch.priv; +struct arm_smmu_domain *arm_smmu =to_smmu_domain (io_domain); +struct arm_smmu_master *master =dev_iommu_priv_get (dev); -arm_smmu_set_bus_ops (NULL ); -iommu_device_unregister (&smmu->iommu); -iommu_device_sysfs_remove (&smmu->iommu); -arm_smmu_device_disable (smmu); +if ( !arm_smmu || arm_smmu->s2_cfg.domain != d ) + { +dev_err (dev," not attached to domain %d\n" , d->domain_id); +return -ESRCH ; + } -return 0 ; +spin_lock (&xen_domain->lock); + +arm_smmu_detach_dev (master); +atomic_dec (&io_domain->ref); + +if ( io_domain->ref.counter ==0 ) +arm_smmu_destroy_iommu_domain (io_domain); + +spin_unlock (&xen_domain->lock); + +return 0 ; +} + +static int arm_smmu_reassign_dev (struct domain *s,struct domain *t, +u8 devfn,struct device *dev) +{ +int ret =0 ; + +/* Don't allow remapping on other domain than hwdom */ +if ( t && t != hardware_domain ) +return -EPERM ; + +if ( t == s ) +return 0 ; + + ret =arm_smmu_deassign_dev (s, dev); +if ( ret ) +return ret; + +if ( t ) + { +/* No flags are defined for ARM. */ + ret =arm_smmu_assign_dev (t, devfn, dev,0 ); +if ( ret ) +return ret; + } + +return 0 ; +} + +static int arm_smmu_iommu_xen_domain_init (struct domain *d) +{ +struct arm_smmu_xen_domain *xen_domain; + + xen_domain = xzalloc(struct arm_smmu_xen_domain); +if ( !xen_domain ) +return -ENOMEM ; + +spin_lock_init (&xen_domain->lock); +INIT_LIST_HEAD (&xen_domain->contexts); + +dom_iommu (d)->arch.priv = xen_domain; + +return 0 ; +} + +static void __hwdom_init arm_smmu_iommu_hwdom_init(struct domain *d) +{ +} + +static void arm_smmu_iommu_xen_domain_teardown (struct domain *d) +{ +struct arm_smmu_xen_domain *xen_domain =dom_iommu (d)->arch.priv; + +ASSERT (list_empty (&xen_domain->contexts)); +xfree (xen_domain); +} + +static int arm_smmu_dt_xlate (struct device *dev, +const struct dt_phandle_args *args) +{ +int ret; +struct iommu_fwspec *fwspec =dev_iommu_fwspec_get (dev); + + ret =iommu_fwspec_add_ids (dev, args->args,1 ); +if ( ret ) +return ret; + +if (dt_device_is_protected (dev_to_dt (dev)) ) + { +dev_err (dev,"Already added to SMMUv3\n" ); +return -EEXIST ; + } +
xen/drivers/passthrough/arm/smmu-v3.c static unsigned long arm_smmu_resource_size(struct arm_smmu_device *smmu) · line 2340
Quoted 10 lines
xen/drivers/passthrough/arm/smmu-v3.c +7
-1+/* Let Xen know that the master device is protected by an IOMMU. */ +dt_device_set_protected (dev_to_dt (dev)); + +dev_info (dev,"Added master device (SMMUv3 %s StreamIds %u)\n" , +dev_name (fwspec->iommu_dev), fwspec->num_ids); + +return 0 ; } -static void arm_smmu_device_shutdown (struct platform_device *pdev)
I think this function should have been dropped in the previous patch.
Quoted 91 lines
xen/drivers/passthrough/arm/smmu-v3.c +82
-4+static int arm_smmu_add_device (u8 devfn,struct device *dev) { -arm_smmu_device_remove (pdev); +int i, ret; +struct arm_smmu_device *smmu; +struct arm_smmu_master *master; +struct iommu_fwspec *fwspec; + + fwspec =dev_iommu_fwspec_get (dev); +if ( !fwspec ) +return -ENODEV ; + + smmu =arm_smmu_get_by_dev (fwspec->iommu_dev); +if ( !smmu ) +return -ENODEV ; + + master = xzalloc(struct arm_smmu_master); +if ( !master ) +return -ENOMEM ; + + master->dev = dev; + master->smmu = smmu; + master->sids = fwspec->ids; + master->num_sids = fwspec->num_ids; + +dev_iommu_priv_set (dev, master); + +/* Check the SIDs are in range of the SMMU and our stream table */ +for ( i =0 ; i < master->num_sids; i++ ) + { +u32 sid = master->sids[i]; + +if ( !arm_smmu_sid_in_range (smmu, sid) ) + { + ret = -ERANGE ; + goto err_free_master; + } + +/* Ensure l2 strtab is initialised */ +if ( smmu->features &ARM_SMMU_FEAT_2_LVL_STRTAB ) + { + ret =arm_smmu_init_l2_strtab (smmu, sid); +if ( ret ) + goto err_free_master; + } + } + +return 0 ; + +err_free_master: +xfree (master); +dev_iommu_priv_set (dev,NULL ); +return ret; } -static const struct of_device_id arm_smmu_of_match[] = { - { .compatible ="arm,smmu-v3" , }, - { }, +static const struct iommu_ops arm_smmu_iommu_ops = { + .init = arm_smmu_iommu_xen_domain_init, + .hwdom_init = arm_smmu_iommu_hwdom_init, + .teardown = arm_smmu_iommu_xen_domain_teardown, + .iotlb_flush = arm_smmu_iotlb_flush, + .iotlb_flush_all = arm_smmu_iotlb_flush_all, + .assign_device = arm_smmu_assign_dev, + .reassign_device = arm_smmu_reassign_dev, + .map_page = arm_iommu_map_page, + .unmap_page = arm_iommu_unmap_page, + .dt_xlate = arm_smmu_dt_xlate, + .add_device = arm_smmu_add_device, +}; + +static const struct dt_device_match arm_smmu_of_match[] = { + { .compatible ="arm,smmu-v3" , }, + { }, }; + +static __init int arm_smmu_dt_init (struct dt_device_node *dev, +const void *data) +{ +int rc; + +/* +* Even if the device can't be initialized, we don't want to +* give the SMMU device to dom0. +*/ +dt_device_set_used_by (dev,DOMID_XEN ); + + rc =arm_smmu_device_probe (dt_to_dev (dev)); +if ( rc )
xen/drivers/passthrough/arm/smmu-v3.c static unsigned long arm_smmu_resource_size(struct arm_smmu_device *smmu) · line 2954
Quoted 10 lines
xen/drivers/passthrough/arm/smmu-v3.c +10
-0+return rc; + +iommu_set_ops (&arm_smmu_iommu_ops); +return 0 ; +} + +DT_DEVICE_START (smmuv3,"ARM SMMU V3" ,DEVICE_IOMMU ) + .dt_match = arm_smmu_of_match, + .init = arm_smmu_dt_init, +DT_DEVICE_END
Cheers,
Signature · 1 line
Julien Grall
Hello Julien,
Quoted 15 lines · Julien Grall
Hi Rahul,
Quoted 9 lines · Rahul Singh
Add support for ARM architected SMMUv3 implementation. It is based on the Linux SMMUv3 driver.
Major differences with regard to Linux driver are as follows:
1. Only Stage-2 translation is supported as compared to the Linux driver that supports both Stage-1 and Stage-2 translations.
2. Use P2M page table instead of creating one as SMMUv3 has the capability to share the page tables with the CPU.
3. Tasklets are used in place of threaded IRQ's in Linux for event queue and priority queue IRQ handling.
Quoted 1 line
On the previous version, we discussed that using tasklets is not a suitable replacement for threaded IRQs. What's the plan to address it?
As suggested by you in earlier discussions I will try to use the timer to schedule the work and will share the findings.
Quoted 35 lines
Quoted 35 lines
4. Latest version of the Linux SMMUv3 code implements the commands queue access functions based on atomic operations implemented in Linux. Atomic functions used by the commands queue access functions are not implemented in XEN therefore we decided to port the earlier version of the code. Once the proper atomic operations will be available in XEN the driver can be updated.
5. Driver is currently supported as Tech Preview.Signed-off-by Rahul Singh <[email protected]>--- MAINTAINERS | 6 + SUPPORT.md | 1 + xen/drivers/passthrough/Kconfig | 10 + xen/drivers/passthrough/arm/Makefile | 1 + xen/drivers/passthrough/arm/smmu-v3.c | 986 +++++++++++++++++++++----- 5 files changed, 814 insertions(+), 190 deletions(-)MAINTAINERS +5
-0@@ -249,6 +249,12 @@ F: xen/include/asm-arm/ F: xen/include/public/arch-arm/ F: xen/include/public/arch-arm.h +ARM SMMUv3 +M: Bertrand Marquis <[email protected]> +M: Rahul Singh <[email protected]> +S: Supported +F: xen/drivers/passthrough/arm/smmu-v3.c + Change Log M: Paul Durrant <[email protected]> R: Community Manager <[email protected]>SUPPORT.md +0
-1--- a/SUPPORT.md
Quoted 8 lines
SUPPORT.md For the Cortex A57 r0p0 - r1p1, see Errata 832075. · line 71
Quoted 6 lines
+++
b/SUPPORT.mdSUPPORT.md +1
-0@@ -68,6 +68,7 @@ For the Cortex A57 r0p0 - r1p1, see Errata 832075. Status, ARM SMMUv1: Supported, not security supported Status, ARM SMMUv2: Supported, not security supported Status, Renesas IPMMU-VMSA: Supported, not security supported + Status, ARM SMMUv3: Tech PreviewPlease move this right after "ARM SMMUv2”.
Ok.
Quoted 39 lines
Quoted 39 lines
SUPPORT.md +0
-0### ARM/GICv3 ITS diff --git a/xen/drivers/passthrough/Kconfig b/xen/drivers/passthrough/Kconfigindex
0036007ec4..5b71c59f47100644xen/drivers/passthrough/Kconfig +9
-0@@ -13,6 +13,16 @@ config ARM_SMMU Say Y here if your SoC includes an IOMMU device implementing the ARM SMMU architecture. +config ARM_SMMU_V3 + bool "ARM Ltd. System MMU Version 3 (SMMUv3) Support" if EXPERT + depends on ARM_64 + ---help--- + Support for implementations of the ARM System MMU architecture + version 3. + + Say Y here if your system includes an IOMMU device implementing + the ARM SMMUv3 architecture. + config IPMMU_VMSA bool "Renesas IPMMU-VMSA found in R-Car Gen3 SoCs" depends on ARM_64xen/drivers/passthrough/arm/Makefile +1
-0@@ -1,3 +1,4 @@ obj-y += iommu.o iommu_helpers.o iommu_fwspec.o obj-$(CONFIG_ARM_SMMU) += smmu.o obj-$(CONFIG_IPMMU_VMSA) += ipmmu-vmsa.o +obj-$(CONFIG_ARM_SMMU_V3) += smmu-v3.oxen/drivers/passthrough/arm/smmu-v3.c +0
-0@@ -2,36 +2,280 @@/* * IOMMU API for ARM architected SMMUv3 implementations. *
Quoted 8 lines
xen/drivers/passthrough/arm/smmu-v3.c line 9
Quoted 6 lines
xen/drivers/passthrough/arm/smmu-v3.c +5
-1-* Copyright (C) 2015 ARM Limited +* Based on Linux's SMMUv3 driver: +* drivers/iommu/arm/arm-smmu-v3/arm-smmu-v3.c +* commit: 951cbbc386ff01b50da4f46387e994e81d9ab431 +* and Xen's SMMU driver: +* xen/drivers/passthrough/arm/smmu.c I would suggest to list the major differences here as well.
Ok.
Quoted 6 lines
xen/drivers/passthrough/arm/smmu-v3.c line 11
Quoted 4 lines
xen/drivers/passthrough/arm/smmu-v3.c +1
-1* -* Author: Will Deacon <[email protected]> +* Copyright (C) 2015 ARM Limited Will Deacon <[email protected]> Why did you merge the Author and copyright line?
I Will fix this in next version.
Quoted 30 lines
Quoted 30 lines
xen/drivers/passthrough/arm/smmu-v3.c +28
-1* -* This driver is powered by bad coffee and bombay mix. +* Copyright (C) 2020 Arm Ltd. +* +* This program is free software; you can redistribute it and/or modify +* it under the terms of the GNU General Public License version 2 as +* published by the Free Software Foundation. +* +* This program is distributed in the hope that it will be useful, +* but WITHOUT ANY WARRANTY; without even the implied warranty of +* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +* GNU General Public License for more details. +* +* You should have received a copy of the GNU General Public License +* along with this program. If not, see <http://www.gnu.org/licenses/>. +* +*/ + +#include <xen/acpi.h> +#include <xen/config.h> +#include <xen/delay.h> +#include <xen/errno.h> +#include <xen/err.h> +#include <xen/irq.h> +#include <xen/lib.h> +#include <xen/list.h> +#include <xen/mm.h> +#include <xen/rbtree.h> +#include <xen/sched.h> +#include <xen/sizes.h>
Quoted 8 lines
xen/drivers/passthrough/arm/smmu-v3.c line 46
Quoted 6 lines
xen/drivers/passthrough/arm/smmu-v3.c +6
-0+#include <xen/vmap.h> +#include <asm/atomic.h> +#include <asm/device.h> +#include <asm/io.h> +#include <asm/platform.h> +#include <asm/iommu_fwspec.h> All the headers seem to be alphabetically ordered but this one.
Ack.
Quoted 5 lines
xen/drivers/passthrough/arm/smmu-v3.c line 48
Quoted 3 lines
xen/drivers/passthrough/arm/smmu-v3.c +2
-0+ +/* Linux compatibility functions. */ Some of the helpers here seem to be similar to the SMMU driver. Can we have an header that can be shared between the two?
As we agreed that I will make the driver XEN compatible and I will remove the compatibility functions from here. If there is any helper that is common I will create the common header file.
Quoted 16 lines
Quoted 16 lines
xen/drivers/passthrough/arm/smmu-v3.c +15
-0+typedef paddr_t dma_addr_t ; +typedef unsigned int gfp_t ; + +#define platform_device device + +#define GFP_KERNEL 0 + +/* Alias to Xen device tree helpers */ +#define device_node dt_device_node +#define of_phandle_args dt_phandle_args +#define of_device_id dt_device_match +#define of_match_node dt_match_node +#define of_property_read_u32 (np, pname, out) (!dt_property_read_u32(np, pname, out)) +#define of_property_read_bool dt_property_read_bool +#define of_parse_phandle_with_args dt_parse_phandle_with_args
Quoted 8 lines
xen/drivers/passthrough/arm/smmu-v3.c line 69
Quoted 6 lines
xen/drivers/passthrough/arm/smmu-v3.c +6
-0+ +/* Alias to Xen lock functions */ +#define mutex spinlock +#define mutex_init spin_lock_init +#define mutex_lock spin_lock +#define mutex_unlock spin_unlockHmm... mutex are not spinlock. Can you explain why this is fine to switch to spinlock?
Yes mutex are not spinlock. As mutex is not implemented in XEN I thought of using spinlock in place of mutex as this is the only locking mechanism available in XEN. Let me know if there is another blocking lock available in XEN. I will check if we can use that.
Quoted 55 lines
Quoted 55 lines
xen/drivers/passthrough/arm/smmu-v3.c +54
-0+ +/* Alias to Xen time functions */ +#define ktime_t s_time_t +#define ktime_get () (NOW()) +#define ktime_add_us (t,i) (t + MICROSECS(i)) +#define ktime_compare (t,i) (t > (i)) + +/* Alias to Xen allocation helpers */ +#define kzalloc (size, flags) _xzalloc(size, sizeof(void *)) +#define kfree xfree +#define devm_kzalloc (dev, size, flags) _xzalloc(size, sizeof(void *)) + +/* Device logger functions */ +#define dev_name (dev) dt_node_full_name(dev->of_node) +#define dev_dbg (dev, fmt, ...) \ + printk(XENLOG_DEBUG "SMMUv3: %s: " fmt, dev_name(dev), ## __VA_ARGS__) +#define dev_notice (dev, fmt, ...) \ + printk(XENLOG_INFO "SMMUv3: %s: " fmt, dev_name(dev), ## __VA_ARGS__) +#define dev_warn (dev, fmt, ...) \ + printk(XENLOG_WARNING "SMMUv3: %s: " fmt, dev_name(dev), ## __VA_ARGS__) +#define dev_err (dev, fmt, ...) \ + printk(XENLOG_ERR "SMMUv3: %s: " fmt, dev_name(dev), ## __VA_ARGS__) +#define dev_info (dev, fmt, ...) \ + printk(XENLOG_INFO "SMMUv3: %s: " fmt, dev_name(dev), ## __VA_ARGS__) +#define dev_err_ratelimited (dev, fmt, ...) \ + printk(XENLOG_ERR "SMMUv3: %s: " fmt, dev_name(dev), ## __VA_ARGS__) + +/* +* Periodically poll an address and wait between reads in us until a +* condition is met or a timeout occurs. +*/ +#define readx_poll_timeout (op, addr, val, cond, sleep_us, timeout_us) \ +({ \ + s_time_t deadline = NOW() + MICROSECS(timeout_us); \ + for (;;) { \ + (val) = op(addr); \ + if (cond) \ + break; \ + if (NOW() > deadline) { \ + (val) = op(addr); \ + break; \ + } \ + udelay(sleep_us); \ + } \ + (cond) ? 0 : -ETIMEDOUT; \ +}) + +#define readl_relaxed_poll_timeout (addr, val, cond, delay_us, timeout_us) \ + readx_poll_timeout(readl_relaxed, addr, val, cond, delay_us, timeout_us) + +#define FIELD_PREP (_mask, _val) \ + (((typeof(_mask))(_val) << (__builtin_ffsll(_mask) - 1)) & (_mask)) + +#define FIELD_GET (_mask, _reg) \
Quoted 8 lines
xen/drivers/passthrough/arm/smmu-v3.c line 129
Quoted 6 lines
xen/drivers/passthrough/arm/smmu-v3.c +6
-0+ (typeof(_mask))(((_reg) & (_mask)) >> (__builtin_ffsll(_mask) - 1)) + +#define WRITE_ONCE (x, val) \ +do { \ + *(volatile typeof(x) *)&(x) = (val); \ +} while (0)Please implement it with
write_atomic()orACCESS_ONCE().
OK.
Quoted 17 lines
Quoted 17 lines
xen/drivers/passthrough/arm/smmu-v3.c +15
-0+ +/* Xen: Stub out DMA domain related functions */ +#define iommu_get_dma_cookie (dom) 0 +#define iommu_put_dma_cookie (dom) + +/* +* Helpers for DMA allocation. Just the function name is reused for +* porting code, these allocation are not managed allocations */ +static void *dmam_alloc_coherent (struct device *dev,size_t size, +paddr_t *dma_handle,gfp_t gfp) +{ +void *vaddr; +unsigned long alignment = size; + +/*
Quoted 8 lines
xen/drivers/passthrough/arm/smmu-v3.c line 151
Quoted 6 lines
xen/drivers/passthrough/arm/smmu-v3.c +6
-0+* _xzalloc requires that the (align & (align -1)) = 0. Most of the +* allocations in SMMU code should send the right value for size. In +* case this is not true print a warning and align to the size of a +* (void *) +*/ +if ( size & (size -1 ) )We should use the same coding style within the file. As the file is imported from Linux, new code should follow Linux coding style.
Ok.
Quoted 66 lines
Quoted 66 lines
xen/drivers/passthrough/arm/smmu-v3.c +65
-0+ { +printk (XENLOG_WARNING "SMMUv3: Fixing alignment for the DMA buffer\n" ); + alignment =sizeof (void *); + } + + vaddr =_xzalloc (size, alignment); +if ( !vaddr ) + { +printk (XENLOG_ERR "SMMUv3: DMA allocation failed\n" ); +return NULL ; + } + + *dma_handle =virt_to_maddr (vaddr); + +return vaddr; +} + +/* Xen: Type definitions for iommu_domain */ +#define IOMMU_DOMAIN_UNMANAGED 0 +#define IOMMU_DOMAIN_DMA 1 +#define IOMMU_DOMAIN_IDENTITY 2 + +/* Xen specific code. */ +struct iommu_domain { +/* Runtime SMMU configuration for this iommu_domain */ +atomic_t ref; +/* +* Used to link iommu_domain contexts for a same domain. +* There is at least one per-SMMU to used by the domain. +*/ +struct list_head list; +}; + +/* Describes information required for a Xen domain */ +struct arm_smmu_xen_domain { +spinlock_t lock; + +/* List of iommu domains associated to this domain */ +struct list_head contexts; +}; + +/* +* Information about each device stored in dev->archdata.iommu +* The dev->archdata.iommu stores the iommu_domain (runtime configuration of +* the SMMU). +*/ +struct arm_smmu_xen_device { +struct iommu_domain *domain; +}; + +/* Keep a list of devices associated with this driver */ +static DEFINE_SPINLOCK (arm_smmu_devices_lock); +static LIST_HEAD (arm_smmu_devices); + + +static inline void *dev_iommu_priv_get (struct device *dev) +{ +struct iommu_fwspec *fwspec =dev_iommu_fwspec_get (dev); + +return fwspec && fwspec->iommu_priv ? fwspec->iommu_priv :NULL ; +} + +static inline void dev_iommu_priv_set (struct device *dev,void *priv) +{ +struct iommu_fwspec *fwspec =dev_iommu_fwspec_get (dev);
Quoted 8 lines
xen/drivers/passthrough/arm/smmu-v3.c line 222
Quoted 6 lines
xen/drivers/passthrough/arm/smmu-v3.c +6
-0+ + fwspec->iommu_priv = priv; +} + +int dt_property_match_string (const struct dt_device_node *np, +const char *propname,const char *string)I think this should be implemented in
device_tree.c
Ok I will move this to device_tree.c file
Quoted 148 lines
Quoted 148 lines
xen/drivers/passthrough/arm/smmu-v3.c +68
-28+{ +const struct dt_property *dtprop =dt_find_property (np, propname,NULL ); +size_t l; +int i; +const char *p, *end; + +if ( !dtprop ) +return -EINVAL ; + +if ( !dtprop->value ) +return -ENODATA ; + + p = dtprop->value; + end = p + dtprop->length; + +for ( i =0 ; p < end; i++, p += l ) + { + l =strnlen (p, end - p) +1 ; + +if ( p + l > end ) +return -EILSEQ ; + +if (strcmp (string, p) ==0 ) +return i;/* Found it; return index */ + } + +return -ENODATA ; +} + +static int platform_get_irq_byname_optional (struct device *dev, +const char *name) +{ +int index, ret; +struct dt_device_node *np =dev_to_dt (dev); + +if (unlikely (!name) ) +return -EINVAL ; + + index =dt_property_match_string (np,"interrupt-names" , name); +if ( index <0 ) + { +dev_info (dev,"IRQ %s not found\n" , name); +return index; + } -#include <linux/acpi.h> -#include <linux/acpi_iort.h> -#include <linux/bitfield.h> -#include <linux/bitops.h> -#include <linux/crash_dump.h> -#include <linux/delay.h> -#include <linux/dma-iommu.h> -#include <linux/err.h> -#include <linux/interrupt.h> -#include <linux/io-pgtable.h> -#include <linux/iommu.h> -#include <linux/iopoll.h> -#include <linux/module.h> -#include <linux/msi.h> -#include <linux/of.h> -#include <linux/of_address.h> -#include <linux/of_iommu.h> -#include <linux/of_platform.h> -#include <linux/pci.h> -#include <linux/pci-ats.h> -#include <linux/platform_device.h> - -#include <linux/amba/bus.h> + ret =platform_get_irq (np, index); +if ( ret <0 ) + { +dev_err (dev,"failed to get irq index %d\n" , index); +return -ENODEV ; + } + +return ret; +} + +/* Start of Linux SMMUv3 code */ /* MMIO registers */ #define ARM_SMMU_IDR0 0x0@@ -507,6 +751,7 @@ struct arm_smmu_s2_cfg {u16 vmid;u64 vttbr;u64 vtcr; +struct domain *domain; };struct arm_smmu_strtab_cfg {@@ -567,8 +812,13 @@ struct arm_smmu_device {struct arm_smmu_strtab_cfg strtab_cfg; -/* IOMMU core code handle */ - struct iommu_device iommu; +/* Need to keep a list of SMMU devices */ + struct list_head devices; + +/* Tasklets for handling evts/faults and pci page request IRQs*/ +struct tasklet evtq_irq_tasklet; +struct tasklet priq_irq_tasklet; +struct tasklet combined_irq_tasklet; };/* SMMU private data for each master */ @@ -1110,7 +1360,7 @@ static int arm_smmu_init_l2_strtab(struct arm_smmu_device *smmu, u32 sid) }/* IRQ and event handlers */ -static irqreturn_t arm_smmu_evtq_thread (int irq,void *dev) +static void arm_smmu_evtq_thread (void *dev) {int i;struct arm_smmu_device *smmu = dev;@@ -1140,7 +1390,6 @@ static irqreturn_t arm_smmu_evtq_thread(int irq, void *dev)/* Sync our overflow flag, as we believe we're up to speed */ llq->cons =Q_OVF (llq->prod) |Q_WRP (llq, llq->cons) |Q_IDX (llq, llq->cons); -return IRQ_HANDLED ; }static void arm_smmu_handle_ppr (struct arm_smmu_device *smmu,u64 *evt)@@ -1181,7 +1430,7 @@ static void arm_smmu_handle_ppr(struct arm_smmu_device *smmu, u64 *evt) } } -static irqreturn_t arm_smmu_priq_thread(int irq, void *dev) +static voidarm_smmu_priq_thread (void *dev) {struct arm_smmu_device *smmu = dev;struct arm_smmu_queue *q = &smmu->priq.q;@@ -1200,12 +1449,12 @@ static irqreturn_t arm_smmu_priq_thread(int irq, void *dev) llq->cons =Q_OVF (llq->prod) |Q_WRP (llq, llq->cons) |Q_IDX (llq, llq->cons);queue_sync_cons_out (q); -return IRQ_HANDLED ; }static int arm_smmu_device_disable (struct arm_smmu_device *smmu); -static irqreturn_t arm_smmu_gerror_handler(int irq, void *dev) +static void arm_smmu_gerror_handler(int irq, void *dev, + struct cpu_user_regs *regs) {u32 gerror, gerrorn, active;struct arm_smmu_device *smmu = dev;@@ -1215,7 +1464,7 @@ static irqreturn_t arm_smmu_gerror_handler(int irq, void *dev) active = gerror ^ gerrorn;if (!(active &GERROR_ERR_MASK )) -return IRQ_NONE ;/* No errors pending */ +return ;/* No errors pending */ dev_warn(smmu->dev, "unexpected global error reported (0x%08x), this could be serious\n",@@ -1248,26 +1497,42 @@ static irqreturn_t arm_smmu_gerror_handler(int irq, void *dev)arm_smmu_cmdq_skip_err (smmu);writel (gerror, smmu->base +ARM_SMMU_GERRORN ); -return IRQ_HANDLED ;
Quoted 8 lines
xen/drivers/passthrough/arm/smmu-v3.c static irqreturn_t arm_smmu_gerror_handler(int irq, void *dev) · line 1505
Quoted 6 lines
xen/drivers/passthrough/arm/smmu-v3.c +4
-0} -static irqreturn_t arm_smmu_combined_irq_thread(int irq, void *dev) +static void arm_smmu_combined_irq_handler(int irq, void *dev, + struct cpu_user_regs *regs) +{ +struct arm_smmu_device *smmu = (struct arm_smmu_device *)dev;The cast is not necessary.
Ok.
Quoted 38 lines
Quoted 15 lines
xen/drivers/passthrough/arm/smmu-v3.c +8
-2+ +arm_smmu_gerror_handler (irq, dev, regs); + +tasklet_schedule (&(smmu->combined_irq_tasklet)); +} + +static void arm_smmu_combined_irq_thread (void *dev) {struct arm_smmu_device *smmu = dev; -arm_smmu_evtq_thread (irq, dev); +arm_smmu_evtq_thread (dev);if (smmu->features &ARM_SMMU_FEAT_PRI ) -arm_smmu_priq_thread (irq, dev); -xen/drivers/passthrough/arm/smmu-v3.c static irqreturn_t arm_smmu_gerror_handler(int irq, void *dev) · line 1524
Quoted 10 lines
xen/drivers/passthrough/arm/smmu-v3.c +4
-3-return IRQ_HANDLED ; +arm_smmu_priq_thread (dev); } -static irqreturn_t arm_smmu_combined_irq_handler(int irq, void *dev) +static void arm_smmu_evtq_irq_tasklet(int irq, void *dev, + struct cpu_user_regs *regs) { -arm_smmu_gerror_handler (irq, dev); -return IRQ_WAKE_THREAD ; +struct arm_smmu_device *smmu = (struct arm_smmu_device *)dev;Ditto.
xen/drivers/passthrough/arm/smmu-v3.c static irqreturn_t arm_smmu_gerror_handler(int irq, void *dev) · line 1532
Quoted 7 lines
xen/drivers/passthrough/arm/smmu-v3.c +5
-0+ +tasklet_schedule (&(smmu->evtq_irq_tasklet)); } +static void arm_smmu_priq_irq_tasklet(int irq, void *dev, +struct cpu_user_regs *regs) +{ +struct arm_smmu_device *smmu = (struct arm_smmu_device *)dev;Ditto.
Quoted 16 lines
Quoted 6 lines
xen/drivers/passthrough/arm/smmu-v3.c +3
-0+ +tasklet_schedule (&(smmu->priq_irq_tasklet)); +}/* IO_PGTABLE API */ static void arm_smmu_tlb_inv_context (void *cookie)xen/drivers/passthrough/arm/smmu-v3.c static void arm_smmu_domain_free(struct iommu_domain *domain) · line 1624
Quoted 8 lines
xen/drivers/passthrough/arm/smmu-v3.c +2
-2@@ -1354,27 +1619,69 @@ static void arm_smmu_domain_free(struct iommu_domain *domain) }static int arm_smmu_domain_finalise_s2 (struct arm_smmu_domain *smmu_domain, -struct arm_smmu_master *master, -struct io_pgtable_cfg *pgtbl_cfg) +struct arm_smmu_master *master) {int vmid; +u64 reg;I think
uint32_tis sufficient here.
Ok.
Quoted 7 lines
xen/drivers/passthrough/arm/smmu-v3.c static void arm_smmu_domain_free(struct iommu_domain *domain) · line 1630
Quoted 5 lines
xen/drivers/passthrough/arm/smmu-v3.c +1
-0struct arm_smmu_device *smmu = smmu_domain->smmu;struct arm_smmu_s2_cfg *cfg = &smmu_domain->s2_cfg; +/* VTCR */ + reg =VTCR_RES1 |VTCR_SH0_IS |VTCR_IRGN0_WBWA |VTCR_ORGN0_WBWA ;
VTCR_RES1will set bit 31 to 1. However, from the spec it looks like the equivalent bit in the entry (see 5.2 in ARM IHI 0070C.a) will be RES0.
Yes I will fix this.
Quoted 15 lines
xen/drivers/passthrough/arm/smmu-v3.c static void arm_smmu_domain_free(struct iommu_domain *domain) · line 1642
Quoted 13 lines
xen/drivers/passthrough/arm/smmu-v3.c +12
-0+ +switch (PAGE_SIZE ) { +case SZ_4K : + reg |=VTCR_TG0_4K ; +break ; +case SZ_16K : + reg |=VTCR_TG0_16K ; +break ; +case SZ_64K : + reg |=VTCR_TG0_4K ; +break ; + }I would just handle 4K here and add a
BUILD_BUG_ON(PAGE_SIZE != SZ_4K).
Ok.
Quoted 23 lines
xen/drivers/passthrough/arm/smmu-v3.c static void arm_smmu_domain_free(struct iommu_domain *domain) · line 1641
Quoted 2 lines
xen/drivers/passthrough/arm/smmu-v3.c +1
-0+ > + switch (smmu->oas) {AFAICT
smmu->oasand ...Quoted 18 lines
xen/drivers/passthrough/arm/smmu-v3.c +18
-0+case 32 : + reg |=VTCR_PS (_AC (0x0 ,ULL )); +break ; +case 36 : + reg |=VTCR_PS (_AC (0x1 ,ULL )); +break ; +case 40 : + reg |=VTCR_PS (_AC (0x2 ,ULL )); +break ; +case 42 : + reg |=VTCR_PS (_AC (0x3 ,ULL )); +break ; +case 44 : + reg |=VTCR_PS (_AC (0x4 ,ULL )); +break ; +case 48 : + reg |=VTCR_PS (_AC (0x5 ,ULL )); +break ;
Quoted 8 lines
xen/drivers/passthrough/arm/smmu-v3.c static void arm_smmu_domain_free(struct iommu_domain *domain) · line 1668
Quoted 6 lines
xen/drivers/passthrough/arm/smmu-v3.c +6
-0+case 52 : + reg |=VTCR_PS (_AC (0x6 ,ULL )); +break ; + } + + reg |=VTCR_T0SZ (64ULL - smmu->ias);... are directly taken from the SMMU configuration. However, as we share the P2M, we need to make sure the value match what the CPU is using.
Ok let me double check again on this. As I have tested SMMUv3 driver on N1SDP board it is observed as same.
Quoted 10 lines
For the IAS, you will want to use
p2m_ipa_bitsand for the output, we will want to cap toPADDR_BITS.xen/drivers/passthrough/arm/smmu-v3.c static void arm_smmu_domain_free(struct iommu_domain *domain) · line 1669
Quoted 1 line
xen/drivers/passthrough/arm/smmu-v3.c +1
-0+ reg |=VTCR_SL0 (0x2 );Similar to above, the starting level will depend on how the P2M was configured.
xen/drivers/passthrough/arm/smmu-v3.c static void arm_smmu_domain_free(struct iommu_domain *domain) · line 1670
Quoted 1 line
xen/drivers/passthrough/arm/smmu-v3.c +1
-0+ reg |=VTCR_VS ;AFAICT, the bit 179 (bit 19 in the word) is indicating whether AArch32 or AArch64 translation table is used. However, bit 19 in
VTCR_EL2indicates whether we are using 8-bit or 16-bit VMID.
Ok let me double check all the configuration for Stream Table Entry.
Quoted 5 lines
xen/drivers/passthrough/arm/smmu-v3.c static void arm_smmu_domain_free(struct iommu_domain *domain) · line 1672
Quoted 3 lines
xen/drivers/passthrough/arm/smmu-v3.c +2
-0+ + cfg->vtcr = reg;It would be better to initialize vtcr exactly at the same place as Linux does. This would make easier to match the code.
Ok.
Quoted 41 lines
Quoted 41 lines
xen/drivers/passthrough/arm/smmu-v3.c +8
-19+ vmid =arm_smmu_bitmap_alloc (smmu->vmid_map, smmu->vmid_bits);if (vmid <0 )return vmid; + cfg->vmid = (u16 )vmid; + + cfg->vttbr =page_to_maddr (cfg->domain->arch.p2m.root); + +printk (XENLOG_DEBUG +"SMMUv3: d%u: vmid 0x%x vtcr 0x%" PRIpaddr" p2maddr 0x%" PRIpaddr"\n" , + cfg->domain->domain_id, cfg->vmid, cfg->vtcr, cfg->vttbr); - vtcr = &pgtbl_cfg->arm_lpae_s2_cfg.vtcr; - cfg->vmid = (u16 )vmid; - cfg->vttbr = pgtbl_cfg->arm_lpae_s2_cfg.vttbr; - cfg->vtcr =FIELD_PREP (STRTAB_STE_2_VTCR_S2T0SZ , vtcr->tsz) | -FIELD_PREP (STRTAB_STE_2_VTCR_S2SL0 , vtcr->sl) | -FIELD_PREP (STRTAB_STE_2_VTCR_S2IR0 , vtcr->irgn) | -FIELD_PREP (STRTAB_STE_2_VTCR_S2OR0 , vtcr->orgn) | -FIELD_PREP (STRTAB_STE_2_VTCR_S2SH0 , vtcr->sh) | -FIELD_PREP (STRTAB_STE_2_VTCR_S2TG , vtcr->tg) | -FIELD_PREP (STRTAB_STE_2_VTCR_S2PS , vtcr->ps);return 0 ; } @@ -1382,28 +1689,12 @@ static int arm_smmu_domain_finalise(struct iommu_domain *domain,struct arm_smmu_master *master) {int ret; -unsigned long ias, oas; -int (*finalise_stage_fn)(struct arm_smmu_domain *, -struct arm_smmu_master *, -struct io_pgtable_cfg *);struct arm_smmu_domain *smmu_domain =to_smmu_domain (domain); -struct arm_smmu_device *smmu = smmu_domain->smmu;/* Restrict the stage to what we can actually support */ smmu_domain->stage =ARM_SMMU_DOMAIN_S2 ; - switch (smmu_domain->stage) { -case ARM_SMMU_DOMAIN_NESTED : -case ARM_SMMU_DOMAIN_S2 : - ias = smmu->ias; - oas = smmu->oas; - finalise_stage_fn = arm_smmu_domain_finalise_s2;
Quoted 8 lines
xen/drivers/passthrough/arm/smmu-v3.c static int arm_smmu_domain_finalise(struct iommu_domain *domain, · line 1406
Quoted 6 lines
xen/drivers/passthrough/arm/smmu-v3.c +0
-6-break ; -default : -return -EINVAL ; - } - - ret =finalise_stage_fn (smmu_domain, master, &pgtbl_cfg);It is not entirely clear why this code is removed here and not the previous patch?
I will removed this code in previous patch. Somehow I missed.
Quoted 12 lines
xen/drivers/passthrough/arm/smmu-v3.c static int arm_smmu_init_one_queue(struct arm_smmu_device *smmu, · line 1847
Quoted 10 lines
xen/drivers/passthrough/arm/smmu-v3.c +2
-0+ ret =arm_smmu_domain_finalise_s2 (smmu_domain, master);if (ret <0 ) {return ret; }@@ -1553,7 +1844,8 @@ static int arm_smmu_init_one_queue(struct arm_smmu_device *smmu,return -ENOMEM ; } - if (!WARN_ON(q->base_dma & (qsz - 1))) { +WARN_ON (q->base_dma & (qsz -1 ));This is a call to rework how our
WARN_ON()in Xen.
Yes.
Quoted 113 lines
Quoted 113 lines
xen/drivers/passthrough/arm/smmu-v3.c +9
-16+if (unlikely (q->base_dma & (qsz -1 ))) {dev_info (smmu->dev,"allocated %u entries for %s\n" ,1 << q->llq.max_n_shift, name); }@@ -1758,9 +2050,7 @@ static void arm_smmu_setup_unique_irqs(struct arm_smmu_device *smmu)/* Request interrupt lines */ irq = smmu->evtq.q.irq;if (irq) { - ret =devm_request_threaded_irq (smmu->dev, irq,NULL , - arm_smmu_evtq_thread, -IRQF_ONESHOT , + ret =request_irq (irq,0 , arm_smmu_evtq_irq_tasklet,"arm-smmu-v3-evtq" , smmu);if (ret <0 )dev_warn (smmu->dev,"failed to enable evtq irq\n" );@@ -1770,8 +2060,8 @@ static void arm_smmu_setup_unique_irqs(struct arm_smmu_device *smmu) irq = smmu->gerr_irq;if (irq) { - ret =devm_request_irq (smmu->dev, irq, arm_smmu_gerror_handler, -0 ,"arm-smmu-v3-gerror" , smmu); + ret =request_irq (irq,0 , arm_smmu_gerror_handler, +"arm-smmu-v3-gerror" , smmu);if (ret <0 )dev_warn (smmu->dev,"failed to enable gerror irq\n" ); }else {@@ -1781,11 +2071,8 @@ static void arm_smmu_setup_unique_irqs(struct arm_smmu_device *smmu)if (smmu->features &ARM_SMMU_FEAT_PRI ) { irq = smmu->priq.q.irq;if (irq) { - ret =devm_request_threaded_irq (smmu->dev, irq,NULL , - arm_smmu_priq_thread, -IRQF_ONESHOT , -"arm-smmu-v3-priq" , - smmu); + ret =request_irq (irq,0 , arm_smmu_priq_irq_tasklet, +"arm-smmu-v3-priq" , smmu);if (ret <0 )dev_warn (smmu->dev,"failed to enable priq irq\n" );@@ -1814,11 +2101,8 @@ static int arm_smmu_setup_irqs(struct arm_smmu_device *smmu)* Cavium ThunderX2 implementation doesn't support unique irq * lines. Use a single irq line for all the SMMUv3 interrupts. */ - ret =devm_request_threaded_irq (smmu->dev, irq, - arm_smmu_combined_irq_handler, - arm_smmu_combined_irq_thread, -IRQF_ONESHOT , -"arm-smmu-v3-combined-irq" , smmu); + ret =request_irq (irq,0 , arm_smmu_combined_irq_handler, +"arm-smmu-v3-combined-irq" , smmu);if (ret <0 )dev_warn (smmu->dev,"failed to enable combined irq\n" ); }else @@ -1857,7 +2141,7 @@ static int arm_smmu_device_reset(struct arm_smmu_device *smmu, bool bypass) reg =readl_relaxed (smmu->base +ARM_SMMU_CR0 );if (reg &CR0_SMMUEN ) {dev_warn (smmu->dev,"SMMU currently enabled! Resetting...\n" ); -WARN_ON (is_kdump_kernel () && !disable_bypass); +WARN_ON (!disable_bypass);arm_smmu_update_gbpa (smmu,GBPA_ABORT ,0 ); } @@ -1952,8 +2236,11 @@ static int arm_smmu_device_reset(struct arm_smmu_device *smmu, bool bypass)xen/drivers/passthrough/arm/smmu-v3.c +6
-2return ret; } - if (is_kdump_kernel()) - enables &= ~(CR0_EVTQEN | CR0_PRIQEN); + /* Initialize tasklets for threaded IRQs*/ +tasklet_init (&smmu->evtq_irq_tasklet, arm_smmu_evtq_thread, smmu); +tasklet_init (&smmu->priq_irq_tasklet, arm_smmu_priq_thread, smmu); +tasklet_init (&smmu->combined_irq_tasklet, arm_smmu_combined_irq_thread, + smmu);/* Enable the SMMU interface, or ensure bypass */ if (!bypass || disable_bypass) {@@ -2195,7 +2482,7 @@ static inline int arm_smmu_device_acpi_probe(struct platform_device *pdev,static int arm_smmu_device_dt_probe (struct platform_device *pdev,struct arm_smmu_device *smmu) { -struct device *dev = &pdev->dev; +struct device *dev = pdev;u32 cells;int ret = -EINVAL ; @@ -2219,130 +2506,449 @@ static unsigned long arm_smmu_resource_size(struct arm_smmu_device *smmu)xen/drivers/passthrough/arm/smmu-v3.c +19
-6return SZ_128K ; } +/* Start of Xen specific code. */ static int arm_smmu_device_probe(struct platform_device *pdev) { -int irq, ret; -struct resource *res; -resource_size_t ioaddr; -struct arm_smmu_device *smmu; -struct device *dev = &pdev->dev; -bool bypass; +int irq, ret; +paddr_t ioaddr, iosize; +struct arm_smmu_device *smmu; +bool bypass; + + smmu =devm_kzalloc (dev,sizeof (*smmu),GFP_KERNEL ); +if ( !smmu ) + { +dev_err (pdev,"failed to allocate arm_smmu_device\n" ); +return -ENOMEM ; + } + smmu->dev = pdev; + +if ( pdev->of_node ) + { + ret =arm_smmu_device_dt_probe (pdev, smmu); + }else + { + ret =arm_smmu_device_acpi_probe (pdev, smmu);
Quoted 8 lines
xen/drivers/passthrough/arm/smmu-v3.c static unsigned long arm_smmu_resource_size(struct arm_smmu_device *smmu) · line 2536
Quoted 6 lines
xen/drivers/passthrough/arm/smmu-v3.c +6
-0+if ( ret == -ENODEV ) +return ret; + } + +/* Set bypass mode according to firmware probing result */ + bypass = !!ret;AFAICT, bypass would be set if the device-tree is buggy. For Xen, I think it would be saner to not continue as this would break isolation.
Ok . I will not configure the SMMU in bypass mode if device-tree probe is failed. I will report error to the user.
Quoted 11 lines
Quoted 11 lines
xen/drivers/passthrough/arm/smmu-v3.c +10
-0+ +/* Base address */ + ret =dt_device_get_address (dev_to_dt (pdev),0 , &ioaddr, &iosize); +if ( ret ) +return -ENODEV ; + +if ( iosize <arm_smmu_resource_size (smmu) ) + { +dev_err (pdev,"MMIO region too small (%lx)\n" , iosize); +return -EINVAL ;
Quoted 8 lines
xen/drivers/passthrough/arm/smmu-v3.c static unsigned long arm_smmu_resource_size(struct arm_smmu_device *smmu) · line 2552
Quoted 6 lines
xen/drivers/passthrough/arm/smmu-v3.c +6
-0+ } + +/* +* Don't map the IMPLEMENTATION DEFINED regions, since they may contain +* the PMCG registers which are reserved by the PMU driver. +*/ This comment doesn't seem to match the code. Did you intend to...
Yes I will fix the code and will use ARM_SMMU_REG_SZ as size.
Quoted 6 lines
Quoted 4 lines
+
smmu->base=ioremap_nocache(ioaddr, iosize);... use
ARM_SMMU_REG_SZwhich would only map the necessary region?
Yes I will use ARM_SMMU_REG_SZ .
Quoted 131 lines
Quoted 118 lines
xen/drivers/passthrough/arm/smmu-v3.c +101
-12+if (IS_ERR (smmu->base) ) +return PTR_ERR (smmu->base); + +if ( iosize >SZ_64K ) + { + smmu->page1 =ioremap_nocache (ioaddr +SZ_64K ,ARM_SMMU_REG_SZ ); +if (IS_ERR (smmu->page1) ) +return PTR_ERR (smmu->page1); + } +else + { + smmu->page1 = smmu->base; + } + +/* Interrupt lines */ + + irq =platform_get_irq_byname_optional (pdev,"combined" ); +if ( irq >0 ) + smmu->combined_irq = irq; +else + { + irq =platform_get_irq_byname_optional (pdev,"eventq" ); +if ( irq >0 ) + smmu->evtq.q.irq = irq; + + irq =platform_get_irq_byname_optional (pdev,"priq" ); +if ( irq >0 ) + smmu->priq.q.irq = irq; + + irq =platform_get_irq_byname_optional (pdev,"gerror" ); +if ( irq >0 ) + smmu->gerr_irq = irq; + } +/* Probe the h/w */ + ret =arm_smmu_device_hw_probe (smmu); +if ( ret ) +return ret; + +/* Initialise in-memory data structures */ + ret =arm_smmu_init_structures (smmu); +if ( ret ) +return ret; + +/* Reset the device */ + ret =arm_smmu_device_reset (smmu, bypass); +if ( ret ) +return ret; + +/* +* Keep a list of all probed devices. This will be used to query +* the smmu devices based on the fwnode. +*/ +INIT_LIST_HEAD (&smmu->devices); + +spin_lock (&arm_smmu_devices_lock); +list_add (&smmu->devices, &arm_smmu_devices); +spin_unlock (&arm_smmu_devices_lock); + +return 0 ; +} + +static int __must_check arm_smmu_iotlb_flush_all (struct domain *d) +{ +struct arm_smmu_xen_domain *xen_domain =dom_iommu (d)->arch.priv; +struct iommu_domain *io_domain; + +spin_lock (&xen_domain->lock); + +list_for_each_entry ( io_domain, &xen_domain->contexts, list ) + { +/* +* Only invalidate the context when SMMU is present. +* This is because the context initialization is delayed +* until a master has been added. +*/ +if (unlikely (!ACCESS_ONCE (to_smmu_domain (io_domain)->smmu)) ) +continue ; + +arm_smmu_tlb_inv_context (to_smmu_domain (io_domain)); + } - smmu =devm_kzalloc (dev,sizeof (*smmu),GFP_KERNEL ); -if (!smmu) { -dev_err (dev,"failed to allocate arm_smmu_device\n" ); -return -ENOMEM ; - } - smmu->dev = dev; +spin_unlock (&xen_domain->lock); + +return 0 ; +} + +static int __must_check arm_smmu_iotlb_flush (struct domain *d,dfn_t dfn, +unsigned long page_count, +unsigned int flush_flags) +{ +return arm_smmu_iotlb_flush_all (d); +} - if (dev->of_node) { - ret =arm_smmu_device_dt_probe (pdev, smmu); - } else { - ret =arm_smmu_device_acpi_probe (pdev, smmu); -if (ret == -ENODEV ) -return ret; - } +static struct arm_smmu_device *arm_smmu_get_by_dev (struct device *dev) +{ +struct arm_smmu_device *smmu =NULL ; -/* Set bypass mode according to firmware probing result */ - bypass = !!ret; +spin_lock (&arm_smmu_devices_lock); +list_for_each_entry ( smmu, &arm_smmu_devices, devices ) + { +if ( smmu->dev == dev ) + { +spin_unlock (&arm_smmu_devices_lock); +return smmu;xen/drivers/passthrough/arm/smmu-v3.c static unsigned long arm_smmu_resource_size(struct arm_smmu_device *smmu) · line 2255
Quoted 10 lines
xen/drivers/passthrough/arm/smmu-v3.c +3
-6+ } + } +spin_unlock (&arm_smmu_devices_lock); - /* Base address */ - res =platform_get_resource (pdev,IORESOURCE_MEM ,0 ); -if (resource_size (res) <arm_smmu_resource_size (smmu)) { -dev_err (dev,"MMIO region too small (%pr)\n" , res); -return -EINVAL ; - } - ioaddr = res->start;The code removed is basically the same as the one you added except the coding style change. This patch is already quite long to review, so can we please keep the change to the strict minimum?
Quoted 1 line
If you want to do to clean-up then they should be done before/after.
Ok.
Quoted 242 lines
Quoted 242 lines
xen/drivers/passthrough/arm/smmu-v3.c +174
-50+return NULL ; +} -/* -* Don't map the IMPLEMENTATION DEFINED regions, since they may contain -* the PMCG registers which are reserved by the PMU driver. -*/ - smmu->base =arm_smmu_ioremap (dev, ioaddr,ARM_SMMU_REG_SZ ); -if (IS_ERR (smmu->base)) -return PTR_ERR (smmu->base); - -if (arm_smmu_resource_size (smmu) >SZ_64K ) { - smmu->page1 =arm_smmu_ioremap (dev, ioaddr +SZ_64K , -ARM_SMMU_REG_SZ ); -if (IS_ERR (smmu->page1)) -return PTR_ERR (smmu->page1); - }else { - smmu->page1 = smmu->base; - } +/* Probing and initialisation functions */ +static struct iommu_domain *arm_smmu_get_domain(struct domain *d, +struct device *dev) +{ +struct iommu_domain *io_domain; +struct arm_smmu_domain *smmu_domain; +struct iommu_fwspec *fwspec =dev_iommu_fwspec_get (dev); +struct arm_smmu_xen_domain *xen_domain =dom_iommu (d)->arch.priv; +struct arm_smmu_device *smmu =arm_smmu_get_by_dev (fwspec->iommu_dev); + +if ( !smmu ) +return NULL ; -/* Interrupt lines */ +/* +* Loop through the &xen_domain->contexts to locate a context +* assigned to this SMMU +*/ + list_for_each_entry( io_domain, &xen_domain->contexts, list ) + { + smmu_domain =to_smmu_domain (io_domain); +if ( smmu_domain->smmu == smmu ) +return io_domain; + } - irq =platform_get_irq_byname_optional (pdev,"combined" ); -if (irq >0 ) - smmu->combined_irq = irq; -else { - irq =platform_get_irq_byname_optional (pdev,"eventq" ); -if (irq >0 ) - smmu->evtq.q.irq = irq; +return NULL ; +} - irq =platform_get_irq_byname_optional (pdev,"priq" ); -if (irq >0 ) - smmu->priq.q.irq = irq; +static void arm_smmu_destroy_iommu_domain (struct iommu_domain *io_domain) +{ +list_del (&io_domain->list); +arm_smmu_domain_free (io_domain); +} - irq =platform_get_irq_byname_optional (pdev,"gerror" ); -if (irq >0 ) - smmu->gerr_irq = irq; - } -/* Probe the h/w */ - ret =arm_smmu_device_hw_probe (smmu); -if (ret) -return ret; +static int arm_smmu_assign_dev (struct domain *d,u8 devfn, +struct device *dev,u32 flag) +{ +int ret =0 ; +struct iommu_domain *io_domain; +struct arm_smmu_domain *smmu_domain; +struct arm_smmu_xen_domain *xen_domain =dom_iommu (d)->arch.priv; -/* Initialise in-memory data structures */ - ret =arm_smmu_init_structures (smmu); -if (ret) -return ret; + if ( !dev->archdata.iommu ) + { + dev->archdata.iommu = xzalloc(struct arm_smmu_xen_device); +if ( !dev->archdata.iommu ) +return -ENOMEM ; + } -/* Record our private device structure */ -platform_set_drvdata (pdev, smmu); +spin_lock (&xen_domain->lock); -/* Reset the device */ - ret =arm_smmu_device_reset (smmu, bypass); -if (ret) -return ret; +/* +* Check to see if an iommu_domain already exists for this xen domain +* under the same SMMU +*/ + io_domain =arm_smmu_get_domain (d, dev); +if ( !io_domain ) + { + io_domain =arm_smmu_domain_alloc (IOMMU_DOMAIN_DMA ); +if ( !io_domain ) + { + ret = -ENOMEM ; + goto out; + } -/* And we're up. Go go go! */ - ret =iommu_device_sysfs_add (&smmu->iommu, dev,NULL , -"smmu3.%pa" , &ioaddr); -if (ret) -return ret; + smmu_domain =to_smmu_domain (io_domain); + smmu_domain->s2_cfg.domain = d; -iommu_device_set_ops (&smmu->iommu, &arm_smmu_ops); -iommu_device_set_fwnode (&smmu->iommu, dev->fwnode); +/* Chain the new context to the domain */ +list_add (&io_domain->list, &xen_domain->contexts); - ret =iommu_device_register (&smmu->iommu); -if (ret) { -dev_err (dev,"Failed to register iommu\n" ); -return ret; - } + } + + ret =arm_smmu_attach_dev (io_domain, dev); +if ( ret ) + { +if ( io_domain->ref.counter ==0 ) +arm_smmu_destroy_iommu_domain (io_domain); + } +else + { +atomic_inc (&io_domain->ref); + } - return arm_smmu_set_bus_ops(&arm_smmu_ops); +out: +spin_unlock (&xen_domain->lock); +return ret; } -static int arm_smmu_device_remove(struct platform_device *pdev) +static int arm_smmu_deassign_dev(struct domain *d, struct device *dev) { -struct arm_smmu_device *smmu =platform_get_drvdata (pdev); +struct iommu_domain *io_domain =arm_smmu_get_domain (d, dev); +struct arm_smmu_xen_domain *xen_domain =dom_iommu (d)->arch.priv; +struct arm_smmu_domain *arm_smmu =to_smmu_domain (io_domain); +struct arm_smmu_master *master =dev_iommu_priv_get (dev); -arm_smmu_set_bus_ops (NULL ); -iommu_device_unregister (&smmu->iommu); -iommu_device_sysfs_remove (&smmu->iommu); -arm_smmu_device_disable (smmu); +if ( !arm_smmu || arm_smmu->s2_cfg.domain != d ) + { +dev_err (dev," not attached to domain %d\n" , d->domain_id); +return -ESRCH ; + } - return 0; +spin_lock (&xen_domain->lock); + +arm_smmu_detach_dev (master); +atomic_dec (&io_domain->ref); + +if ( io_domain->ref.counter ==0 ) +arm_smmu_destroy_iommu_domain (io_domain); + +spin_unlock (&xen_domain->lock); + +return 0 ; +} + +static int arm_smmu_reassign_dev (struct domain *s,struct domain *t, +u8 devfn,struct device *dev) +{ +int ret =0 ; + +/* Don't allow remapping on other domain than hwdom */ +if ( t && t != hardware_domain ) +return -EPERM ; + +if ( t == s ) +return 0 ; + + ret =arm_smmu_deassign_dev (s, dev); +if ( ret ) +return ret; + +if ( t ) + { +/* No flags are defined for ARM. */ + ret =arm_smmu_assign_dev (t, devfn, dev,0 ); +if ( ret ) +return ret; + } + +return 0 ; +} + +static int arm_smmu_iommu_xen_domain_init (struct domain *d) +{ +struct arm_smmu_xen_domain *xen_domain; + + xen_domain = xzalloc(struct arm_smmu_xen_domain); +if ( !xen_domain ) +return -ENOMEM ; + +spin_lock_init (&xen_domain->lock); +INIT_LIST_HEAD (&xen_domain->contexts); + +dom_iommu (d)->arch.priv = xen_domain; + +return 0 ; +} + +static void __hwdom_init arm_smmu_iommu_hwdom_init(struct domain *d) +{ +} + +static void arm_smmu_iommu_xen_domain_teardown (struct domain *d) +{ +struct arm_smmu_xen_domain *xen_domain =dom_iommu (d)->arch.priv; + +ASSERT (list_empty (&xen_domain->contexts)); +xfree (xen_domain); +} + +static int arm_smmu_dt_xlate (struct device *dev, +const struct dt_phandle_args *args) +{ +int ret; +struct iommu_fwspec *fwspec =dev_iommu_fwspec_get (dev); + + ret =iommu_fwspec_add_ids (dev, args->args,1 ); +if ( ret ) +return ret; + +if (dt_device_is_protected (dev_to_dt (dev)) ) + { +dev_err (dev,"Already added to SMMUv3\n" ); +return -EEXIST ; + } + +/* Let Xen know that the master device is protected by an IOMMU. */ +dt_device_set_protected (dev_to_dt (dev)); +
Quoted 8 lines
xen/drivers/passthrough/arm/smmu-v3.c static unsigned long arm_smmu_resource_size(struct arm_smmu_device *smmu)
Quoted 6 lines
xen/drivers/passthrough/arm/smmu-v3.c +4
-0+dev_info (dev,"Added master device (SMMUv3 %s StreamIds %u)\n" , +dev_name (fwspec->iommu_dev), fwspec->num_ids); + +return 0 ; } -static void arm_smmu_device_shutdown(struct platform_device *pdev)I think this function should have been dropped in the previous patch.
OK.
Quoted 105 lines
Quoted 90 lines
xen/drivers/passthrough/arm/smmu-v3.c +82
-3+static int arm_smmu_add_device (u8 devfn,struct device *dev) { -arm_smmu_device_remove (pdev); +int i, ret; +struct arm_smmu_device *smmu; +struct arm_smmu_master *master; +struct iommu_fwspec *fwspec; + + fwspec =dev_iommu_fwspec_get (dev); +if ( !fwspec ) +return -ENODEV ; + + smmu =arm_smmu_get_by_dev (fwspec->iommu_dev); +if ( !smmu ) +return -ENODEV ; + + master = xzalloc(struct arm_smmu_master); +if ( !master ) +return -ENOMEM ; + + master->dev = dev; + master->smmu = smmu; + master->sids = fwspec->ids; + master->num_sids = fwspec->num_ids; + +dev_iommu_priv_set (dev, master); + +/* Check the SIDs are in range of the SMMU and our stream table */ +for ( i =0 ; i < master->num_sids; i++ ) + { +u32 sid = master->sids[i]; + +if ( !arm_smmu_sid_in_range (smmu, sid) ) + { + ret = -ERANGE ; + goto err_free_master; + } + +/* Ensure l2 strtab is initialised */ +if ( smmu->features &ARM_SMMU_FEAT_2_LVL_STRTAB ) + { + ret =arm_smmu_init_l2_strtab (smmu, sid); +if ( ret ) + goto err_free_master; + } + } + +return 0 ; + +err_free_master: +xfree (master); +dev_iommu_priv_set (dev,NULL ); +return ret; } -static const struct of_device_id arm_smmu_of_match[] = { - { .compatible ="arm,smmu-v3" , }, - { }, +static const struct iommu_ops arm_smmu_iommu_ops = { + .init = arm_smmu_iommu_xen_domain_init, + .hwdom_init = arm_smmu_iommu_hwdom_init, + .teardown = arm_smmu_iommu_xen_domain_teardown, + .iotlb_flush = arm_smmu_iotlb_flush, + .iotlb_flush_all = arm_smmu_iotlb_flush_all, + .assign_device = arm_smmu_assign_dev, + .reassign_device = arm_smmu_reassign_dev, + .map_page = arm_iommu_map_page, + .unmap_page = arm_iommu_unmap_page, + .dt_xlate = arm_smmu_dt_xlate, + .add_device = arm_smmu_add_device, +}; + +static const struct dt_device_match arm_smmu_of_match[] = { + { .compatible ="arm,smmu-v3" , }, + { }, }; + +static __init int arm_smmu_dt_init (struct dt_device_node *dev, +const void *data) +{ +int rc; + +/* +* Even if the device can't be initialized, we don't want to +* give the SMMU device to dom0. +*/ +dt_device_set_used_by (dev,DOMID_XEN ); + + rc =arm_smmu_device_probe (dt_to_dev (dev)); +if ( rc )xen/drivers/passthrough/arm/smmu-v3.c static unsigned long arm_smmu_resource_size(struct arm_smmu_device *smmu) · line 2954
Quoted 10 lines
xen/drivers/passthrough/arm/smmu-v3.c +10
-0+return rc; + +iommu_set_ops (&arm_smmu_iommu_ops); +return 0 ; +} + +DT_DEVICE_START (smmuv3,"ARM SMMU V3" ,DEVICE_IOMMU ) + .dt_match = arm_smmu_of_match, + .init = arm_smmu_dt_init, +DT_DEVICE_END Cheers,
Signature · 1 line
Julien Grall
Quoted 24 lines · Rahul Singh
Quoted 15 lines
Quoted 15 lines
xen/drivers/passthrough/arm/smmu-v3.c +15
-0+typedef paddr_t dma_addr_t ; +typedef unsigned int gfp_t ; + +#define platform_device device + +#define GFP_KERNEL 0 + +/* Alias to Xen device tree helpers */ +#define device_node dt_device_node +#define of_phandle_args dt_phandle_args +#define of_device_id dt_device_match +#define of_match_node dt_match_node +#define of_property_read_u32 (np, pname, out) (!dt_property_read_u32(np, pname, out)) +#define of_property_read_bool dt_property_read_bool +#define of_parse_phandle_with_args dt_parse_phandle_with_argsQuoted 8 lines
xen/drivers/passthrough/arm/smmu-v3.c line 69
Quoted 6 lines
xen/drivers/passthrough/arm/smmu-v3.c +6
-0+ +/* Alias to Xen lock functions */ +#define mutex spinlock +#define mutex_init spin_lock_init +#define mutex_lock spin_lock +#define mutex_unlock spin_unlockHmm... mutex are not spinlock. Can you explain why this is fine to switch to spinlock?
Quoted 2 lines
Yes mutex are not spinlock. As mutex is not implemented in XEN I thought of using spinlock in place of mutex as this is the only locking mechanism available in XEN. Let me know if there is another blocking lock available in XEN. I will check if we can use that.
There are no blocking lock available in Xen so far. However, if Linux were using mutex instead of spinlock, then it likely means they operations in the critical section can be long running.
How did you came to the conclusion that using spinlock in the SMMU driver would be fine?
Cheers,
Signature · 1 line
Julien Grall
Hello Julien,
Quoted 28 lines · Julien Grall
Quoted 23 lines · Rahul Singh
Quoted 23 lines
Quoted 11 lines
xen/drivers/passthrough/arm/smmu-v3.c +11
-0+typedef paddr_t dma_addr_t ; +typedef unsigned int gfp_t ; + +#define platform_device device + +#define GFP_KERNEL 0 + +/* Alias to Xen device tree helpers */ +#define device_node dt_device_node +#define of_phandle_args dt_phandle_args +#define of_device_id dt_device_matchxen/drivers/passthrough/arm/smmu-v3.c line 69
Quoted 10 lines
xen/drivers/passthrough/arm/smmu-v3.c +10
-0+#define of_match_node dt_match_node +#define of_property_read_u32 (np, pname, out) (!dt_property_read_u32(np, pname, out)) +#define of_property_read_bool dt_property_read_bool +#define of_parse_phandle_with_args dt_parse_phandle_with_args + +/* Alias to Xen lock functions */ +#define mutex spinlock +#define mutex_init spin_lock_init +#define mutex_lock spin_lock +#define mutex_unlock spin_unlockHmm... mutex are not spinlock. Can you explain why this is fine to switch to spinlock?
Quoted 4 lines
Quoted 2 lines
Yes mutex are not spinlock. As mutex is not implemented in XEN I thought of using spinlock in place of mutex as this is the only locking mechanism available in XEN. Let me know if there is another blocking lock available in XEN. I will check if we can use that.
There are no blocking lock available in Xen so far. However, if Linux were using mutex instead of spinlock, then it likely means they operations in the critical section can be long running.
Yes you are right Linux is using mutex when attaching a device to the SMMU as this operation might take longer time.
Quoted 2 lines
How did you came to the conclusion that using spinlock in the SMMU driver would be fine?
Mutex is replaced by spinlock in the SMMU driver when there is a request to assign a device to the guest. As we are in user context at that time its ok to use spinlock. As per my understanding there is one scenario when CPU will spin when there is a request from the user at the same time to assign another device to the SMMU and I think that is very rare.
Please suggest how we can proceed on this.
Regards
Rahul
Quoted 5 lines
Cheers,
Signature · 1 line
Julien Grall
Quoted 1 line · Rahul Singh
Hello Julien,
Hi,
Quoted 32 lines
Quoted 32 lines · Julien Grall
Quoted 25 lines · Rahul Singh
Quoted 15 lines
Quoted 15 lines
xen/drivers/passthrough/arm/smmu-v3.c +15
-0+typedef paddr_t dma_addr_t ; +typedef unsigned int gfp_t ; + +#define platform_device device + +#define GFP_KERNEL 0 + +/* Alias to Xen device tree helpers */ +#define device_node dt_device_node +#define of_phandle_args dt_phandle_args +#define of_device_id dt_device_match +#define of_match_node dt_match_node +#define of_property_read_u32 (np, pname, out) (!dt_property_read_u32(np, pname, out)) +#define of_property_read_bool dt_property_read_bool +#define of_parse_phandle_with_args dt_parse_phandle_with_argsQuoted 8 lines
xen/drivers/passthrough/arm/smmu-v3.c line 69
Quoted 6 lines
xen/drivers/passthrough/arm/smmu-v3.c +6
-0+ +/* Alias to Xen lock functions */ +#define mutex spinlock +#define mutex_init spin_lock_init +#define mutex_lock spin_lock +#define mutex_unlock spin_unlockHmm... mutex are not spinlock. Can you explain why this is fine to switch to spinlock?
Yes mutex are not spinlock. As mutex is not implemented in XEN I thought of using spinlock in place of mutex as this is the only locking mechanism available in XEN. Let me know if there is another blocking lock available in XEN. I will check if we can use that.
Quoted 7 lines
Quoted 1 line
There are no blocking lock available in Xen so far. However, if Linux were using mutex instead of spinlock, then it likely means they operations in the critical section can be long running.
Yes you are right Linux is using mutex when attaching a device to the SMMU as this operation might take longer time.
Quoted 2 lines
How did you came to the conclusion that using spinlock in the SMMU driver would be fine?
Mutex is replaced by spinlock in the SMMU driver when there is a request to assign a device to the guest. As we are in user context at that time its ok to use spinlock.
I am not sure to understand what you mean by "user context" here. Can you clarify it?
Quoted 1 line
As per my understanding there is one scenario when CPU will spin when there is a request from the user at the same time to assign another device to the SMMU and I think that is very rare.
What "user" are you referring to?
Quoted 2 lines
Please suggest how we can proceed on this.
I am guessing that what you are saying is the request to assign/de-assign device will be issued by the toolstack and therefore they should be trusted.
My concern here is not about someone waiting on the lock to be released. It is more the fact that using a mutex() is an insight that the operation protected can be long. Depending on the length, this may result to unwanted side effect (e.g. other vCPU not scheduled, RCU stall in dom0, watchdog hit...).
I recall a discussion from a couple of years ago mentioning that STE programming operations can take quite a long time. So I would like to understand how long the operation is meant to last.
For a tech preview, this is probably OK to replace the mutex with an spinlock. But I would not want this to go past the tech preview stage without a proper analysis.
Stefano, what do you think?
Cheers,
Signature · 1 line
Julien Grall
Quoted 72 lines · Julien Grall
Quoted 40 lines · Rahul Singh
Quoted 33 lines · Julien Grall
Quoted 25 lines · Rahul Singh
Quoted 17 lines
Quoted 17 lines
xen/drivers/passthrough/arm/smmu-v3.c +13
-0+typedef paddr_t dma_addr_t ; +typedef unsigned int gfp_t ; + +#define platform_device device + +#define GFP_KERNEL 0 + +/* Alias to Xen device tree helpers */ +#define device_node dt_device_node +#define of_phandle_args dt_phandle_args +#define of_device_id dt_device_match +#define of_match_node dt_match_node +#define of_property_read_u32 (np, pname, out)(!dt_property_read_u32(np, pname, out))
xen/drivers/passthrough/arm/smmu-v3.c +3
-0+#define of_property_read_bool dt_property_read_bool +#define of_parse_phandle_with_args dt_parse_phandle_with_args +Quoted 8 lines
xen/drivers/passthrough/arm/smmu-v3.c line 69
Quoted 5 lines
xen/drivers/passthrough/arm/smmu-v3.c +5
-0+/* Alias to Xen lock functions */ +#define mutex spinlock +#define mutex_init spin_lock_init +#define mutex_lock spin_lock +#define mutex_unlock spin_unlockHmm... mutex are not spinlock. Can you explain why this is fine to switch to spinlock?
Quoted 5 lines
Yes mutex are not spinlock. As mutex is not implemented in XEN I thought of using spinlock in place of mutex as this is the only locking mechanism available in XEN.
Let me know if there is another blocking lock available in XEN. I will check if we can use that.Quoted 3 lines
There are no blocking lock available in Xen so far. However, if Linux were using mutex instead of spinlock, then it likely means they operations in the critical section can be long running.
Yes you are right Linux is using mutex when attaching a device to the SMMU as this operation might take longer time.
Quoted 1 line
Quoted 6 lines
Quoted 2 lines
How did you came to the conclusion that using spinlock in the SMMU driver would be fine?
Mutex is replaced by spinlock in the SMMU driver when there is a request to assign a device to the guest. As we are in user context at that time its ok to use spinlock.
I am not sure to understand what you mean by "user context" here. Can you clarify it?
Quoted 3 lines
As per my understanding there is one scenario when CPU will spin when there is a request from the user at the same time to assign another device to the SMMU and I think that is very rare.
What "user" are you referring to?
Quoted 2 lines
Please suggest how we can proceed on this.
I am guessing that what you are saying is the request to assign/de-assign device will be issued by the toolstack and therefore they should be trusted.
My concern here is not about someone waiting on the lock to be released. It is more the fact that using a
mutex()is an insight that the operation protected can be long. Depending on the length, this may result to unwanted side effect (e.g. other vCPU not scheduled, RCU stall in dom0, watchdog hit...).I recall a discussion from a couple of years ago mentioning that STE programming operations can take quite a long time. So I would like to understand how long the operation is meant to last.
Quoted 5 lines
For a tech preview, this is probably OK to replace the mutex with an spinlock. But I would not want this to go past the tech preview stage without a proper analysis.
Stefano, what do you think?
In short, I agree.
We need to be very careful replacing mutexes with spinlocks. We need to look closely at the ways the spinlocks could introduce unwanted latencies. Concurrent assign_device operations are possible but rare and, more importantly, they are user-driven so they could be mitigated. I am more worried about other possible scenarios, e.g. STE or other operations.
Rahul clearly put a lot of work into this series already and I think it is better to take this incrementally, which will allow us to do better testing and also move faster overall. So I am fine to take the series as is now, pending an investigation on the spinlocks later.
Hi,
Quoted 91 lines · Stefano Stabellini
Quoted 72 lines · Julien Grall
Quoted 40 lines · Rahul Singh
Quoted 33 lines · Julien Grall
Quoted 25 lines · Rahul Singh
Quoted 17 lines
Quoted 17 lines
(quoted diff) +13
-0+typedef paddr_t dma_addr_t; +typedef unsigned int gfp_t; + +#define platform_device device + +#define GFP_KERNEL 0 + +/* Alias to Xen device tree helpers */ +#define device_node dt_device_node +#define of_phandle_args dt_phandle_args +#define of_device_id dt_device_match +#define of_match_node dt_match_node +#define of_property_read_u32(np, pname, out)(!dt_property_read_u32(np, pname, out))
(quoted diff) +3
-0+#define of_property_read_bool dt_property_read_bool +#define of_parse_phandle_with_args dt_parse_phandle_with_args +Quoted 8 lines
Review
Quoted 5 lines
(quoted diff) +5
-0+/* Alias to Xen lock functions */ +#define mutex spinlock +#define mutex_init spin_lock_init +#define mutex_lock spin_lock +#define mutex_unlock spin_unlockHmm... mutex are not spinlock. Can you explain why this is fine to switch to spinlock?
Quoted 5 lines
Yes mutex are not spinlock. As mutex is not implemented in XEN I thought of using spinlock in place of mutex as this is the only locking mechanism available in XEN.
Let me know if there is another blocking lock available in XEN. I will check if we can use that.Quoted 3 lines
There are no blocking lock available in Xen so far. However, if Linux were using mutex instead of spinlock, then it likely means they operations in the critical section can be long running.
Yes you are right Linux is using mutex when attaching a device to the SMMU as this operation might take longer time.
Quoted 1 line
Quoted 6 lines
Quoted 2 lines
How did you came to the conclusion that using spinlock in the SMMU driver would be fine?
Mutex is replaced by spinlock in the SMMU driver when there is a request to assign a device to the guest. As we are in user context at that time its ok to use spinlock.
I am not sure to understand what you mean by "user context" here. Can you clarify it?
Quoted 3 lines
As per my understanding there is one scenario when CPU will spin when there is a request from the user at the same time to assign another device to the SMMU and I think that is very rare.
What "user" are you referring to?
Quoted 2 lines
Please suggest how we can proceed on this.
I am guessing that what you are saying is the request to assign/de-assign device will be issued by the toolstack and therefore they should be trusted.
My concern here is not about someone waiting on the lock to be released. It is more the fact that using a
mutex()is an insight that the operation protected can be long. Depending on the length, this may result to unwanted side effect (e.g. other vCPU not scheduled, RCU stall in dom0, watchdog hit...).I recall a discussion from a couple of years ago mentioning that STE programming operations can take quite a long time. So I would like to understand how long the operation is meant to last.
Quoted 5 lines
For a tech preview, this is probably OK to replace the mutex with an spinlock. But I would not want this to go past the tech preview stage without a proper analysis.
Stefano, what do you think?
In short, I agree.
We need to be very careful replacing mutexes with spinlocks. We need to look closely at the ways the spinlocks could introduce unwanted latencies. Concurrent
assign_deviceoperations are possible but rare and, more importantly, they are user-driven so they could be mitigated. I am more worried about other possible scenarios, e.g. STE or other operations.
Quoted 4 lines
Rahul clearly put a lot of work into this series already and I think it is better to take this incrementally, which will allow us to do better testing and also move faster overall. So I am fine to take the series as is now, pending an investigation on the spinlocks later.
I also agree with the issue on the spinlock but we have no equivalent of something looking like a mutex for now in Xen so this would require some major redesign and will take us far from the linux driver.
I would suggest to add a comment before this part of the code with a “TODO” so that it is clear inside the code.
We could also add some comment in Kconfig to mention this possible “faulty” behaviour.
Would you agree on going forward like this ?
Regards
Bertrand
Quoted 1 line · Bertrand Marquis
Hi,
Hi,
Quoted 3 lines
I also agree with the issue on the spinlock but we have no equivalent of something looking like a mutex for now in Xen so this would require some major redesign and will take us far from the linux driver.
I agree that keeping the Xen implementation close to the Linux one is important. However, I view this has a the secondary goal. The primary goal is to have a safe and secure driver.
If it means introducing a new set of lock or diverging from Linux, then so it be.
Quoted 4 lines
I would suggest to add a comment before this part of the code with a “TODO” so that it is clear inside the code.
We could also add some comment inKconfigto mention this possible “faulty” behaviour.
I think it would be enough to write in the Kconfig that the driver is "experimental and should not be used in production".
Ideally, I would like a list of known issues for the driver (could be in the cover letter or/and at the top of the source file) so we can track what's missing to get it supported.
Cheers,
Signature · 1 line
Julien Grall
Hello Julien,
Quoted 13 lines · Julien Grall
Quoted 1 line · Bertrand Marquis
Hi,
Hi,
Quoted 3 lines
I also agree with the issue on the spinlock but we have no equivalent of something looking like a mutex for now in Xen so this would require some major redesign and will take us far from the linux driver.
Quoted 8 lines
I agree that keeping the Xen implementation close to the Linux one is important. However, I view this has a the secondary goal. The primary goal is to have a safe and secure driver.
If it means introducing a new set of lock or diverging from Linux, then so it be.
Quoted 3 lines
I would suggest to add a comment before this part of the code with a “TODO” so that it is clear inside the code.
We could also add some comment inKconfigto mention this possible “faulty” behaviour.I think it would be enough to write in the
Kconfigthat the driver is "experimental and should not be used in production”.
Ok . I will add in Kconfig.
Quoted 2 lines
Ideally, I would like a list of known issues for the driver (could be in the cover letter or/and at the top of the source file) so we can track what's missing to get it supported.
I will list all known issues and short coming of the driver at the top of the source file.
Regards,
Rahul
Quoted 5 lines
Cheers,
Signature · 1 line
Julien Grall