···301301config ARCH_HAS_CPU_FINALIZE_INIT302302 bool303303304304+# The architecture has a per-task state that includes the mm's PASID305305+config ARCH_HAS_CPU_PASID306306+ bool307307+ select IOMMU_MM_DATA308308+304309# Select if arch init_task must go in the __init_task_data section305310config ARCH_TASK_STRUCT_ON_STACK306311 bool
+1-1
arch/arc/mm/dma.c
···9191 * Plug in direct dma map ops.9292 */9393void arch_setup_dma_ops(struct device *dev, u64 dma_base, u64 size,9494- const struct iommu_ops *iommu, bool coherent)9494+ bool coherent)9595{9696 /*9797 * IOC hardware snoops all DMA traffic keeping the caches consistent
···488488 * Hyper-V does not offer a vIOMMU in the guest489489 * VM, so pass 0/NULL for the IOMMU settings490490 */491491- arch_setup_dma_ops(dev, 0, 0, NULL, coherent);491491+ arch_setup_dma_ops(dev, 0, 0, coherent);492492}493493EXPORT_SYMBOL_GPL(hv_setup_dma_ops);494494
···5353void amd_iommu_pdev_disable_cap_pri(struct pci_dev *pdev);54545555int amd_iommu_flush_page(struct iommu_domain *dom, u32 pasid, u64 address);5656+/*5757+ * This function flushes all internal caches of5858+ * the IOMMU used by this driver.5959+ */6060+void amd_iommu_flush_all_caches(struct amd_iommu *iommu);5661void amd_iommu_update_and_flush_device_table(struct protection_domain *domain);5762void amd_iommu_domain_update(struct protection_domain *domain);5863void amd_iommu_domain_flush_complete(struct protection_domain *domain);5959-void amd_iommu_domain_flush_tlb_pde(struct protection_domain *domain);6464+void amd_iommu_domain_flush_pages(struct protection_domain *domain,6565+ u64 address, size_t size);6066int amd_iommu_flush_tlb(struct iommu_domain *dom, u32 pasid);6167int amd_iommu_domain_set_gcr3(struct iommu_domain *dom, u32 pasid,6268 unsigned long cr3);
-6
drivers/iommu/amd/amd_iommu_types.h
···902902extern u64 amd_iommu_efr;903903extern u64 amd_iommu_efr2;904904905905-/*906906- * This function flushes all internal caches of907907- * the IOMMU used by this driver.908908- */909909-void iommu_flush_all_caches(struct amd_iommu *iommu);910910-911905static inline int get_ioapic_devid(int id)912906{913907 struct devid_map *entry;
···3434#endif3535};36363737+static int check_custom_allocator(enum io_pgtable_fmt fmt,3838+ struct io_pgtable_cfg *cfg)3939+{4040+ /* No custom allocator, no need to check the format. */4141+ if (!cfg->alloc && !cfg->free)4242+ return 0;4343+4444+ /* When passing a custom allocator, both the alloc and free4545+ * functions should be provided.4646+ */4747+ if (!cfg->alloc || !cfg->free)4848+ return -EINVAL;4949+5050+ /* Make sure the format supports custom allocators. */5151+ if (io_pgtable_init_table[fmt]->caps & IO_PGTABLE_CAP_CUSTOM_ALLOCATOR)5252+ return 0;5353+5454+ return -EINVAL;5555+}5656+3757struct io_pgtable_ops *alloc_io_pgtable_ops(enum io_pgtable_fmt fmt,3858 struct io_pgtable_cfg *cfg,3959 void *cookie)···6242 const struct io_pgtable_init_fns *fns;63436444 if (fmt >= IO_PGTABLE_NUM_FMTS)4545+ return NULL;4646+4747+ if (check_custom_allocator(fmt, cfg))6548 return NULL;66496750 fns = io_pgtable_init_table[fmt];
+59-42
drivers/iommu/iommu-sva.c
···1212static DEFINE_MUTEX(iommu_sva_lock);13131414/* Allocate a PASID for the mm within range (inclusive) */1515-static int iommu_sva_alloc_pasid(struct mm_struct *mm, struct device *dev)1515+static struct iommu_mm_data *iommu_alloc_mm_data(struct mm_struct *mm, struct device *dev)1616{1717+ struct iommu_mm_data *iommu_mm;1718 ioasid_t pasid;1818- int ret = 0;1919+2020+ lockdep_assert_held(&iommu_sva_lock);19212022 if (!arch_pgtable_dma_compat(mm))2121- return -EBUSY;2323+ return ERR_PTR(-EBUSY);22242323- mutex_lock(&iommu_sva_lock);2525+ iommu_mm = mm->iommu_mm;2426 /* Is a PASID already associated with this mm? */2525- if (mm_valid_pasid(mm)) {2626- if (mm->pasid >= dev->iommu->max_pasids)2727- ret = -EOVERFLOW;2828- goto out;2727+ if (iommu_mm) {2828+ if (iommu_mm->pasid >= dev->iommu->max_pasids)2929+ return ERR_PTR(-EOVERFLOW);3030+ return iommu_mm;2931 }3232+3333+ iommu_mm = kzalloc(sizeof(struct iommu_mm_data), GFP_KERNEL);3434+ if (!iommu_mm)3535+ return ERR_PTR(-ENOMEM);30363137 pasid = iommu_alloc_global_pasid(dev);3238 if (pasid == IOMMU_PASID_INVALID) {3333- ret = -ENOSPC;3434- goto out;3939+ kfree(iommu_mm);4040+ return ERR_PTR(-ENOSPC);3541 }3636- mm->pasid = pasid;3737- ret = 0;3838-out:3939- mutex_unlock(&iommu_sva_lock);4040- return ret;4242+ iommu_mm->pasid = pasid;4343+ INIT_LIST_HEAD(&iommu_mm->sva_domains);4444+ /*4545+ * Make sure the write to mm->iommu_mm is not reordered in front of4646+ * initialization to iommu_mm fields. If it does, readers may see a4747+ * valid iommu_mm with uninitialized values.4848+ */4949+ smp_store_release(&mm->iommu_mm, iommu_mm);5050+ return iommu_mm;4151}42524353/**···6858 */6959struct iommu_sva *iommu_sva_bind_device(struct device *dev, struct mm_struct *mm)7060{6161+ struct iommu_mm_data *iommu_mm;7162 struct iommu_domain *domain;7263 struct iommu_sva *handle;7364 int ret;74657575- /* Allocate mm->pasid if necessary. */7676- ret = iommu_sva_alloc_pasid(mm, dev);7777- if (ret)7878- return ERR_PTR(ret);7979-8080- handle = kzalloc(sizeof(*handle), GFP_KERNEL);8181- if (!handle)8282- return ERR_PTR(-ENOMEM);8383-8466 mutex_lock(&iommu_sva_lock);8585- /* Search for an existing domain. */8686- domain = iommu_get_domain_for_dev_pasid(dev, mm->pasid,8787- IOMMU_DOMAIN_SVA);8888- if (IS_ERR(domain)) {8989- ret = PTR_ERR(domain);6767+6868+ /* Allocate mm->pasid if necessary. */6969+ iommu_mm = iommu_alloc_mm_data(mm, dev);7070+ if (IS_ERR(iommu_mm)) {7171+ ret = PTR_ERR(iommu_mm);9072 goto out_unlock;9173 }92749393- if (domain) {9494- domain->users++;9595- goto out;7575+ handle = kzalloc(sizeof(*handle), GFP_KERNEL);7676+ if (!handle) {7777+ ret = -ENOMEM;7878+ goto out_unlock;7979+ }8080+8181+ /* Search for an existing domain. */8282+ list_for_each_entry(domain, &mm->iommu_mm->sva_domains, next) {8383+ ret = iommu_attach_device_pasid(domain, dev, iommu_mm->pasid);8484+ if (!ret) {8585+ domain->users++;8686+ goto out;8787+ }9688 }97899890 /* Allocate a new domain and set it on device pasid. */9991 domain = iommu_sva_domain_alloc(dev, mm);10092 if (!domain) {10193 ret = -ENOMEM;102102- goto out_unlock;9494+ goto out_free_handle;10395 }10496105105- ret = iommu_attach_device_pasid(domain, dev, mm->pasid);9797+ ret = iommu_attach_device_pasid(domain, dev, iommu_mm->pasid);10698 if (ret)10799 goto out_free_domain;108100 domain->users = 1;101101+ list_add(&domain->next, &mm->iommu_mm->sva_domains);102102+109103out:110104 mutex_unlock(&iommu_sva_lock);111105 handle->dev = dev;112106 handle->domain = domain;113113-114107 return handle;115108116109out_free_domain:117110 iommu_domain_free(domain);111111+out_free_handle:112112+ kfree(handle);118113out_unlock:119114 mutex_unlock(&iommu_sva_lock);120120- kfree(handle);121121-122115 return ERR_PTR(ret);123116}124117EXPORT_SYMBOL_GPL(iommu_sva_bind_device);···137124void iommu_sva_unbind_device(struct iommu_sva *handle)138125{139126 struct iommu_domain *domain = handle->domain;140140- ioasid_t pasid = domain->mm->pasid;127127+ struct iommu_mm_data *iommu_mm = domain->mm->iommu_mm;141128 struct device *dev = handle->dev;142129143130 mutex_lock(&iommu_sva_lock);131131+ iommu_detach_device_pasid(domain, dev, iommu_mm->pasid);144132 if (--domain->users == 0) {145145- iommu_detach_device_pasid(domain, dev, pasid);133133+ list_del(&domain->next);146134 iommu_domain_free(domain);147135 }148136 mutex_unlock(&iommu_sva_lock);···155141{156142 struct iommu_domain *domain = handle->domain;157143158158- return domain->mm->pasid;144144+ return mm_get_enqcmd_pasid(domain->mm);159145}160146EXPORT_SYMBOL_GPL(iommu_sva_get_pasid);161147···219205220206void mm_pasid_drop(struct mm_struct *mm)221207{222222- if (likely(!mm_valid_pasid(mm)))208208+ struct iommu_mm_data *iommu_mm = mm->iommu_mm;209209+210210+ if (!iommu_mm)223211 return;224212225225- iommu_free_global_pasid(mm->pasid);213213+ iommu_free_global_pasid(iommu_mm->pasid);214214+ kfree(iommu_mm);226215}
+107-47
drivers/iommu/iommu.c
···148148static LIST_HEAD(iommu_device_list);149149static DEFINE_SPINLOCK(iommu_device_lock);150150151151-static struct bus_type * const iommu_buses[] = {151151+static const struct bus_type * const iommu_buses[] = {152152 &platform_bus_type,153153#ifdef CONFIG_PCI154154 &pci_bus_type,···257257 /* We need to be able to take module references appropriately */258258 if (WARN_ON(is_module_address((unsigned long)ops) && !ops->owner))259259 return -EINVAL;260260- /*261261- * Temporarily enforce global restriction to a single driver. This was262262- * already the de-facto behaviour, since any possible combination of263263- * existing drivers would compete for at least the PCI or platform bus.264264- */265265- if (iommu_buses[0]->iommu_ops && iommu_buses[0]->iommu_ops != ops)266266- return -EBUSY;267260268261 iommu->ops = ops;269262 if (hwdev)···266273 list_add_tail(&iommu->list, &iommu_device_list);267274 spin_unlock(&iommu_device_lock);268275269269- for (int i = 0; i < ARRAY_SIZE(iommu_buses) && !err; i++) {270270- iommu_buses[i]->iommu_ops = ops;276276+ for (int i = 0; i < ARRAY_SIZE(iommu_buses) && !err; i++)271277 err = bus_iommu_probe(iommu_buses[i]);272272- }273278 if (err)274279 iommu_device_unregister(iommu);275280 return err;···320329 list_add_tail(&iommu->list, &iommu_device_list);321330 spin_unlock(&iommu_device_lock);322331323323- bus->iommu_ops = ops;324332 err = bus_iommu_probe(bus);325333 if (err) {326334 iommu_device_unregister_bus(iommu, bus, nb);···333343static struct dev_iommu *dev_iommu_get(struct device *dev)334344{335345 struct dev_iommu *param = dev->iommu;346346+347347+ lockdep_assert_held(&iommu_probe_device_lock);336348337349 if (param)338350 return param;···360368 kfree(param);361369}362370371371+/*372372+ * Internal equivalent of device_iommu_mapped() for when we care that a device373373+ * actually has API ops, and don't want false positives from VFIO-only groups.374374+ */375375+static bool dev_has_iommu(struct device *dev)376376+{377377+ return dev->iommu && dev->iommu->iommu_dev;378378+}379379+363380static u32 dev_iommu_get_max_pasids(struct device *dev)364381{365382 u32 max_pasids = 0, bits = 0;···386385387386 return min_t(u32, max_pasids, dev->iommu->iommu_dev->max_pasids);388387}388388+389389+void dev_iommu_priv_set(struct device *dev, void *priv)390390+{391391+ /* FSL_PAMU does something weird */392392+ if (!IS_ENABLED(CONFIG_FSL_PAMU))393393+ lockdep_assert_held(&iommu_probe_device_lock);394394+ dev->iommu->priv = priv;395395+}396396+EXPORT_SYMBOL_GPL(dev_iommu_priv_set);389397390398/*391399 * Init the dev->iommu and dev->iommu_group in the struct device and get the···499489500490static int __iommu_probe_device(struct device *dev, struct list_head *group_list)501491{502502- const struct iommu_ops *ops = dev->bus->iommu_ops;492492+ const struct iommu_ops *ops;493493+ struct iommu_fwspec *fwspec;503494 struct iommu_group *group;504495 struct group_device *gdev;505496 int ret;497497+498498+ /*499499+ * For FDT-based systems and ACPI IORT/VIOT, drivers register IOMMU500500+ * instances with non-NULL fwnodes, and client devices should have been501501+ * identified with a fwspec by this point. Otherwise, we can currently502502+ * assume that only one of Intel, AMD, s390, PAMU or legacy SMMUv2 can503503+ * be present, and that any of their registered instances has suitable504504+ * ops for probing, and thus cheekily co-opt the same mechanism.505505+ */506506+ fwspec = dev_iommu_fwspec_get(dev);507507+ if (fwspec && fwspec->ops)508508+ ops = fwspec->ops;509509+ else510510+ ops = iommu_ops_from_fwnode(NULL);506511507512 if (!ops)508513 return -ENODEV;···643618644619 list_del(&device->list);645620 __iommu_group_free_device(group, device);646646- if (dev->iommu && dev->iommu->iommu_dev)621621+ if (dev_has_iommu(dev))647622 iommu_deinit_device(dev);648623 else649624 dev->iommu_group = NULL;···842817 * Non-API groups still expose reserved_regions in sysfs,843818 * so filter out calls that get here that way.844819 */845845- if (!device->dev->iommu)820820+ if (!dev_has_iommu(device->dev))846821 break;847822848823 INIT_LIST_HEAD(&dev_resv_regions);···12471222 __iommu_group_remove_device(dev);12481223}12491224EXPORT_SYMBOL_GPL(iommu_group_remove_device);12251225+12261226+static struct device *iommu_group_first_dev(struct iommu_group *group)12271227+{12281228+ lockdep_assert_held(&group->mutex);12291229+ return list_first_entry(&group->devices, struct group_device, list)->dev;12301230+}1250123112511232/**12521233 * iommu_group_for_each_dev - iterate over each device in the group···17821751}1783175217841753/*17851785- * Returns the iommu_ops for the devices in an iommu group.17861786- *17871787- * It is assumed that all devices in an iommu group are managed by a single17881788- * IOMMU unit. Therefore, this returns the dev_iommu_ops of the first device17891789- * in the group.17901790- */17911791-static const struct iommu_ops *group_iommu_ops(struct iommu_group *group)17921792-{17931793- struct group_device *device =17941794- list_first_entry(&group->devices, struct group_device, list);17951795-17961796- lockdep_assert_held(&group->mutex);17971797-17981798- return dev_iommu_ops(device->dev);17991799-}18001800-18011801-/*18021754 * req_type of 0 means "auto" which means to select a domain based on18031755 * iommu_def_domain_type or what the driver actually supports.18041756 */18051757static struct iommu_domain *18061758iommu_group_alloc_default_domain(struct iommu_group *group, int req_type)18071759{18081808- const struct iommu_ops *ops = group_iommu_ops(group);17601760+ const struct iommu_ops *ops = dev_iommu_ops(iommu_group_first_dev(group));18091761 struct iommu_domain *dom;1810176218111763 lockdep_assert_held(&group->mutex);···18681854static int iommu_get_def_domain_type(struct iommu_group *group,18691855 struct device *dev, int cur_type)18701856{18711871- const struct iommu_ops *ops = group_iommu_ops(group);18571857+ const struct iommu_ops *ops = dev_iommu_ops(dev);18721858 int type;1873185918741860 if (!ops->def_domain_type)···20172003 return 0;20182004}2019200520062006+/**20072007+ * iommu_present() - make platform-specific assumptions about an IOMMU20082008+ * @bus: bus to check20092009+ *20102010+ * Do not use this function. You want device_iommu_mapped() instead.20112011+ *20122012+ * Return: true if some IOMMU is present and aware of devices on the given bus;20132013+ * in general it may not be the only IOMMU, and it may not have anything to do20142014+ * with whatever device you are ultimately interested in.20152015+ */20202016bool iommu_present(const struct bus_type *bus)20212017{20222022- return bus->iommu_ops != NULL;20182018+ bool ret = false;20192019+20202020+ for (int i = 0; i < ARRAY_SIZE(iommu_buses); i++) {20212021+ if (iommu_buses[i] == bus) {20222022+ spin_lock(&iommu_device_lock);20232023+ ret = !list_empty(&iommu_device_list);20242024+ spin_unlock(&iommu_device_lock);20252025+ }20262026+ }20272027+ return ret;20232028}20242029EXPORT_SYMBOL_GPL(iommu_present);20252030···20542021{20552022 const struct iommu_ops *ops;2056202320572057- if (!dev->iommu || !dev->iommu->iommu_dev)20242024+ if (!dev_has_iommu(dev))20582025 return false;2059202620602027 ops = dev_iommu_ops(dev);···21402107 return ERR_PTR(-ENOMEM);2141210821422109 domain->type = type;21102110+ domain->owner = ops;21432111 /*21442112 * If not already set, assume all sizes by default; the driver21452113 * may override this later···21662132static struct iommu_domain *21672133__iommu_group_domain_alloc(struct iommu_group *group, unsigned int type)21682134{21692169- struct device *dev =21702170- list_first_entry(&group->devices, struct group_device, list)21712171- ->dev;21352135+ struct device *dev = iommu_group_first_dev(group);2172213621732173- return __iommu_domain_alloc(group_iommu_ops(group), dev, type);21372137+ return __iommu_domain_alloc(dev_iommu_ops(dev), dev, type);21382138+}21392139+21402140+static int __iommu_domain_alloc_dev(struct device *dev, void *data)21412141+{21422142+ const struct iommu_ops **ops = data;21432143+21442144+ if (!dev_has_iommu(dev))21452145+ return 0;21462146+21472147+ if (WARN_ONCE(*ops && *ops != dev_iommu_ops(dev),21482148+ "Multiple IOMMU drivers present for bus %s, which the public IOMMU API can't fully support yet. You will still need to disable one or more for this to work, sorry!\n",21492149+ dev_bus_name(dev)))21502150+ return -EBUSY;21512151+21522152+ *ops = dev_iommu_ops(dev);21532153+ return 0;21742154}2175215521762156struct iommu_domain *iommu_domain_alloc(const struct bus_type *bus)21772157{21582158+ const struct iommu_ops *ops = NULL;21592159+ int err = bus_for_each_dev(bus, NULL, &ops, __iommu_domain_alloc_dev);21782160 struct iommu_domain *domain;2179216121802180- if (bus == NULL || bus->iommu_ops == NULL)21622162+ if (err || !ops)21812163 return NULL;21822182- domain = __iommu_domain_alloc(bus->iommu_ops, NULL,21832183- IOMMU_DOMAIN_UNMANAGED);21642164+21652165+ domain = __iommu_domain_alloc(ops, NULL, IOMMU_DOMAIN_UNMANAGED);21842166 if (IS_ERR(domain))21852167 return NULL;21862168 return domain;···23342284static int __iommu_attach_group(struct iommu_domain *domain,23352285 struct iommu_group *group)23362286{22872287+ struct device *dev;22882288+23372289 if (group->domain && group->domain != group->default_domain &&23382290 group->domain != group->blocking_domain)23392291 return -EBUSY;22922292+22932293+ dev = iommu_group_first_dev(group);22942294+ if (!dev_has_iommu(dev) || dev_iommu_ops(dev) != domain->owner)22952295+ return -EINVAL;2340229623412297 return __iommu_group_set_domain(group, domain);23422298}···30603004 */30613005int iommu_dev_enable_feature(struct device *dev, enum iommu_dev_features feat)30623006{30633063- if (dev->iommu && dev->iommu->iommu_dev) {30643064- const struct iommu_ops *ops = dev->iommu->iommu_dev->ops;30073007+ if (dev_has_iommu(dev)) {30083008+ const struct iommu_ops *ops = dev_iommu_ops(dev);3065300930663010 if (ops->dev_enable_feat)30673011 return ops->dev_enable_feat(dev, feat);···30763020 */30773021int iommu_dev_disable_feature(struct device *dev, enum iommu_dev_features feat)30783022{30793079- if (dev->iommu && dev->iommu->iommu_dev) {30803080- const struct iommu_ops *ops = dev->iommu->iommu_dev->ops;30233023+ if (dev_has_iommu(dev)) {30243024+ const struct iommu_ops *ops = dev_iommu_ops(dev);3081302530823026 if (ops->dev_disable_feat)30833027 return ops->dev_disable_feat(dev, feat);···35373481 if (!group)35383482 return -ENODEV;3539348334843484+ if (!dev_has_iommu(dev) || dev_iommu_ops(dev) != domain->owner)34853485+ return -EINVAL;34863486+35403487 mutex_lock(&group->mutex);35413488 curr = xa_cmpxchg(&group->pasid_array, pasid, NULL, domain, GFP_KERNEL);35423489 if (curr) {···36283569 domain->type = IOMMU_DOMAIN_SVA;36293570 mmgrab(mm);36303571 domain->mm = mm;35723572+ domain->owner = ops;36313573 domain->iopf_handler = iommu_sva_handle_iopf;36323574 domain->fault_data = mm;36333575
···863863static struct iommu_device *mtk_iommu_probe_device(struct device *dev)864864{865865 struct iommu_fwspec *fwspec = dev_iommu_fwspec_get(dev);866866- struct mtk_iommu_data *data;866866+ struct mtk_iommu_data *data = dev_iommu_priv_get(dev);867867 struct device_link *link;868868 struct device *larbdev;869869 unsigned int larbid, larbidx, i;870870-871871- if (!fwspec || fwspec->ops != &mtk_iommu_ops)872872- return ERR_PTR(-ENODEV); /* Not a iommu client device */873873-874874- data = dev_iommu_priv_get(dev);875870876871 if (!MTK_IOMMU_IS_TYPE(data->plat_data, MTK_IOMMU_TYPE_MM))877872 return &data->iommu;
-3
drivers/iommu/mtk_iommu_v1.c
···481481 idx++;482482 }483483484484- if (!fwspec || fwspec->ops != &mtk_iommu_v1_ops)485485- return ERR_PTR(-ENODEV); /* Not a iommu client device */486486-487484 data = dev_iommu_priv_get(dev);488485489486 /* Link the consumer device with the smi-larb device(supplier) */
+33-38
drivers/iommu/of_iommu.c
···1717#include <linux/slab.h>1818#include <linux/fsl/mc.h>19192020-#define NO_IOMMU 12121-2220static int of_iommu_xlate(struct device *dev,2321 struct of_phandle_args *iommu_spec)2422{···2729 ops = iommu_ops_from_fwnode(fwnode);2830 if ((ops && !ops->of_xlate) ||2931 !of_device_is_available(iommu_spec->np))3030- return NO_IOMMU;3232+ return -ENODEV;31333234 ret = iommu_fwspec_init(dev, &iommu_spec->np->fwnode, ops);3335 if (ret)···5961 "iommu-map-mask", &iommu_spec.np,6062 iommu_spec.args);6163 if (err)6262- return err == -ENODEV ? NO_IOMMU : err;6464+ return err;63656466 err = of_iommu_xlate(dev, &iommu_spec);6567 of_node_put(iommu_spec.np);···7072 struct device *dev)7173{7274 struct of_phandle_args iommu_spec;7373- int err = NO_IOMMU, idx = 0;7575+ int err = -ENODEV, idx = 0;74767577 while (!of_parse_phandle_with_args(master_np, "iommus",7678 "#iommu-cells",···105107 of_iommu_configure_dev(master_np, dev);106108}107109108108-const struct iommu_ops *of_iommu_configure(struct device *dev,109109- struct device_node *master_np,110110- const u32 *id)110110+/*111111+ * Returns:112112+ * 0 on success, an iommu was configured113113+ * -ENODEV if the device does not have any IOMMU114114+ * -EPROBEDEFER if probing should be tried again115115+ * -errno fatal errors116116+ */117117+int of_iommu_configure(struct device *dev, struct device_node *master_np,118118+ const u32 *id)111119{112112- const struct iommu_ops *ops = NULL;113120 struct iommu_fwspec *fwspec;114114- int err = NO_IOMMU;121121+ int err;115122116123 if (!master_np)117117- return NULL;124124+ return -ENODEV;118125119126 /* Serialise to make dev->iommu stable under our potential fwspec */120127 mutex_lock(&iommu_probe_device_lock);···127124 if (fwspec) {128125 if (fwspec->ops) {129126 mutex_unlock(&iommu_probe_device_lock);130130- return fwspec->ops;127127+ return 0;131128 }132129 /* In the deferred case, start again from scratch */133130 iommu_fwspec_free(dev);···150147 } else {151148 err = of_iommu_configure_device(master_np, dev, id);152149 }153153-154154- /*155155- * Two success conditions can be represented by non-negative err here:156156- * >0 : there is no IOMMU, or one was unavailable for non-fatal reasons157157- * 0 : we found an IOMMU, and dev->fwspec is initialised appropriately158158- * <0 : any actual error159159- */160160- if (!err) {161161- /* The fwspec pointer changed, read it again */162162- fwspec = dev_iommu_fwspec_get(dev);163163- ops = fwspec->ops;164164- }165150 mutex_unlock(&iommu_probe_device_lock);166151167167- /*168168- * If we have reason to believe the IOMMU driver missed the initial169169- * probe for dev, replay it to get things in order.170170- */171171- if (!err && dev->bus)172172- err = iommu_probe_device(dev);152152+ if (err == -ENODEV || err == -EPROBE_DEFER)153153+ return err;154154+ if (err)155155+ goto err_log;173156174174- /* Ignore all other errors apart from EPROBE_DEFER */175175- if (err == -EPROBE_DEFER) {176176- ops = ERR_PTR(err);177177- } else if (err < 0) {178178- dev_dbg(dev, "Adding to IOMMU failed: %d\n", err);179179- ops = NULL;180180- }157157+ err = iommu_probe_device(dev);158158+ if (err)159159+ goto err_log;160160+ return 0;181161182182- return ops;162162+err_log:163163+ dev_dbg(dev, "Adding to IOMMU failed: %pe\n", ERR_PTR(err));164164+ return err;183165}184166185167static enum iommu_resv_type __maybe_unused···248260 phys_addr_t iova;249261 size_t length;250262263263+ if (of_dma_is_coherent(dev->of_node))264264+ prot |= IOMMU_CACHE;265265+251266 maps = of_translate_dma_region(np, maps, &iova, &length);267267+ if (length == 0) {268268+ dev_warn(dev, "Cannot reserve IOVA region of 0 size\n");269269+ continue;270270+ }252271 type = iommu_resv_region_get_type(dev, &phys, iova, length);253272254273 region = iommu_alloc_resv_region(iova, length, prot, type,
···6262 * this bus.6363 * @pm: Power management operations of this bus, callback the specific6464 * device driver's pm-ops.6565- * @iommu_ops: IOMMU specific operations for this bus, used to attach IOMMU6666- * driver implementations to a bus and allow the driver to do6767- * bus-specific setup6865 * @need_parent_lock: When probing or removing a device on this bus, the6966 * device core should lock the device's parent.7067 *···100103 void (*dma_cleanup)(struct device *dev);101104102105 const struct dev_pm_ops *pm;103103-104104- const struct iommu_ops *iommu_ops;105106106107 bool need_parent_lock;107108};
···100100 const struct iommu_flush_ops *tlb;101101 struct device *iommu_dev;102102103103+ /**104104+ * @alloc: Custom page allocator.105105+ *106106+ * Optional hook used to allocate page tables. If this function is NULL,107107+ * @free must be NULL too.108108+ *109109+ * Memory returned should be zeroed and suitable for dma_map_single() and110110+ * virt_to_phys().111111+ *112112+ * Not all formats support custom page allocators. Before considering113113+ * passing a non-NULL value, make sure the chosen page format supports114114+ * this feature.115115+ */116116+ void *(*alloc)(void *cookie, size_t size, gfp_t gfp);117117+118118+ /**119119+ * @free: Custom page de-allocator.120120+ *121121+ * Optional hook used to free page tables allocated with the @alloc122122+ * hook. Must be non-NULL if @alloc is not NULL, must be NULL123123+ * otherwise.124124+ */125125+ void (*free)(void *cookie, void *pages, size_t size);126126+103127 /* Low-level data specific to the table format */104128 union {105129 struct {···266242}267243268244/**245245+ * enum io_pgtable_caps - IO page table backend capabilities.246246+ */247247+enum io_pgtable_caps {248248+ /** @IO_PGTABLE_CAP_CUSTOM_ALLOCATOR: Backend accepts custom page table allocators. */249249+ IO_PGTABLE_CAP_CUSTOM_ALLOCATOR = BIT(0),250250+};251251+252252+/**269253 * struct io_pgtable_init_fns - Alloc/free a set of page tables for a270254 * particular format.271255 *272256 * @alloc: Allocate a set of page tables described by cfg.273257 * @free: Free the page tables associated with iop.258258+ * @caps: Combination of @io_pgtable_caps flags encoding the backend capabilities.274259 */275260struct io_pgtable_init_fns {276261 struct io_pgtable *(*alloc)(struct io_pgtable_cfg *cfg, void *cookie);277262 void (*free)(struct io_pgtable *iop);263263+ u32 caps;278264};279265280266extern struct io_pgtable_init_fns io_pgtable_arm_32_lpae_s1_init_fns;
+39-8
include/linux/iommu.h
···106106 unsigned type;107107 const struct iommu_domain_ops *ops;108108 const struct iommu_dirty_ops *dirty_ops;109109-109109+ const struct iommu_ops *owner; /* Whose domain_alloc we came from */110110 unsigned long pgsize_bitmap; /* Bitmap of page sizes in use */111111 struct iommu_domain_geometry geometry;112112 struct iommu_dma_cookie *iova_cookie;···121121 struct { /* IOMMU_DOMAIN_SVA */122122 struct mm_struct *mm;123123 int users;124124+ /*125125+ * Next iommu_domain in mm->iommu_mm->sva-domains list126126+ * protected by iommu_sva_lock.127127+ */128128+ struct list_head next;124129 };125130 };126131};···817812 struct iommu_domain *domain;818813};819814815815+struct iommu_mm_data {816816+ u32 pasid;817817+ struct list_head sva_domains;818818+};819819+820820int iommu_fwspec_init(struct device *dev, struct fwnode_handle *iommu_fwnode,821821 const struct iommu_ops *ops);822822void iommu_fwspec_free(struct device *dev);···850840 return NULL;851841}852842853853-static inline void dev_iommu_priv_set(struct device *dev, void *priv)854854-{855855- dev->iommu->priv = priv;856856-}843843+void dev_iommu_priv_set(struct device *dev, void *priv);857844858845extern struct mutex iommu_probe_device_lock;859846int iommu_probe_device(struct device *dev);···13441337 return false;13451338}1346133913471347-#ifdef CONFIG_IOMMU_SVA13401340+#ifdef CONFIG_IOMMU_MM_DATA13481341static inline void mm_pasid_init(struct mm_struct *mm)13491342{13501350- mm->pasid = IOMMU_PASID_INVALID;13431343+ /*13441344+ * During dup_mm(), a new mm will be memcpy'd from an old one and that makes13451345+ * the new mm and the old one point to a same iommu_mm instance. When either13461346+ * one of the two mms gets released, the iommu_mm instance is freed, leaving13471347+ * the other mm running into a use-after-free/double-free problem. To avoid13481348+ * the problem, zeroing the iommu_mm pointer of a new mm is needed here.13491349+ */13501350+ mm->iommu_mm = NULL;13511351}13521352+13521353static inline bool mm_valid_pasid(struct mm_struct *mm)13531354{13541354- return mm->pasid != IOMMU_PASID_INVALID;13551355+ return READ_ONCE(mm->iommu_mm);13551356}13571357+13581358+static inline u32 mm_get_enqcmd_pasid(struct mm_struct *mm)13591359+{13601360+ struct iommu_mm_data *iommu_mm = READ_ONCE(mm->iommu_mm);13611361+13621362+ if (!iommu_mm)13631363+ return IOMMU_PASID_INVALID;13641364+ return iommu_mm->pasid;13651365+}13661366+13561367void mm_pasid_drop(struct mm_struct *mm);13571368struct iommu_sva *iommu_sva_bind_device(struct device *dev,13581369 struct mm_struct *mm);···13931368}13941369static inline void mm_pasid_init(struct mm_struct *mm) {}13951370static inline bool mm_valid_pasid(struct mm_struct *mm) { return false; }13711371+13721372+static inline u32 mm_get_enqcmd_pasid(struct mm_struct *mm)13731373+{13741374+ return IOMMU_PASID_INVALID;13751375+}13761376+13961377static inline void mm_pasid_drop(struct mm_struct *mm) {}13971378#endif /* CONFIG_IOMMU_SVA */13981379