1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright (C) 2007-2008 Advanced Micro Devices, Inc.
4 * Author: Joerg Roedel <jroedel@suse.de>
7 #define pr_fmt(fmt) "iommu: " fmt
9 #include <linux/amba/bus.h>
10 #include <linux/device.h>
11 #include <linux/kernel.h>
12 #include <linux/bits.h>
13 #include <linux/bug.h>
14 #include <linux/types.h>
15 #include <linux/init.h>
16 #include <linux/export.h>
17 #include <linux/slab.h>
18 #include <linux/errno.h>
19 #include <linux/host1x_context_bus.h>
20 #include <linux/iommu.h>
21 #include <linux/idr.h>
22 #include <linux/err.h>
23 #include <linux/pci.h>
24 #include <linux/pci-ats.h>
25 #include <linux/bitops.h>
26 #include <linux/platform_device.h>
27 #include <linux/property.h>
28 #include <linux/fsl/mc.h>
29 #include <linux/module.h>
30 #include <linux/cc_platform.h>
31 #include <linux/cdx/cdx_bus.h>
32 #include <trace/events/iommu.h>
33 #include <linux/sched/mm.h>
34 #include <linux/msi.h>
36 #include "dma-iommu.h"
37 #include "iommu-priv.h"
39 #include "iommu-sva.h"
41 static struct kset *iommu_group_kset;
42 static DEFINE_IDA(iommu_group_ida);
43 static DEFINE_IDA(iommu_global_pasid_ida);
45 static unsigned int iommu_def_domain_type __read_mostly;
46 static bool iommu_dma_strict __read_mostly = IS_ENABLED(CONFIG_IOMMU_DEFAULT_DMA_STRICT);
47 static u32 iommu_cmd_line __read_mostly;
51 struct kobject *devices_kobj;
52 struct list_head devices;
53 struct xarray pasid_array;
56 void (*iommu_data_release)(void *iommu_data);
59 struct iommu_domain *default_domain;
60 struct iommu_domain *blocking_domain;
61 struct iommu_domain *domain;
62 struct list_head entry;
63 unsigned int owner_cnt;
68 struct list_head list;
73 /* Iterate over each struct group_device in a struct iommu_group */
74 #define for_each_group_device(group, pos) \
75 list_for_each_entry(pos, &(group)->devices, list)
77 struct iommu_group_attribute {
78 struct attribute attr;
79 ssize_t (*show)(struct iommu_group *group, char *buf);
80 ssize_t (*store)(struct iommu_group *group,
81 const char *buf, size_t count);
84 static const char * const iommu_group_resv_type_string[] = {
85 [IOMMU_RESV_DIRECT] = "direct",
86 [IOMMU_RESV_DIRECT_RELAXABLE] = "direct-relaxable",
87 [IOMMU_RESV_RESERVED] = "reserved",
88 [IOMMU_RESV_MSI] = "msi",
89 [IOMMU_RESV_SW_MSI] = "msi",
92 #define IOMMU_CMD_LINE_DMA_API BIT(0)
93 #define IOMMU_CMD_LINE_STRICT BIT(1)
95 static int iommu_bus_notifier(struct notifier_block *nb,
96 unsigned long action, void *data);
97 static void iommu_release_device(struct device *dev);
98 static struct iommu_domain *
99 __iommu_group_domain_alloc(struct iommu_group *group, unsigned int type);
100 static int __iommu_attach_device(struct iommu_domain *domain,
102 static int __iommu_attach_group(struct iommu_domain *domain,
103 struct iommu_group *group);
106 IOMMU_SET_DOMAIN_MUST_SUCCEED = 1 << 0,
109 static int __iommu_device_set_domain(struct iommu_group *group,
111 struct iommu_domain *new_domain,
113 static int __iommu_group_set_domain_internal(struct iommu_group *group,
114 struct iommu_domain *new_domain,
116 static int __iommu_group_set_domain(struct iommu_group *group,
117 struct iommu_domain *new_domain)
119 return __iommu_group_set_domain_internal(group, new_domain, 0);
121 static void __iommu_group_set_domain_nofail(struct iommu_group *group,
122 struct iommu_domain *new_domain)
124 WARN_ON(__iommu_group_set_domain_internal(
125 group, new_domain, IOMMU_SET_DOMAIN_MUST_SUCCEED));
128 static int iommu_setup_default_domain(struct iommu_group *group,
130 static int iommu_create_device_direct_mappings(struct iommu_domain *domain,
132 static ssize_t iommu_group_store_type(struct iommu_group *group,
133 const char *buf, size_t count);
134 static struct group_device *iommu_group_alloc_device(struct iommu_group *group,
136 static void __iommu_group_free_device(struct iommu_group *group,
137 struct group_device *grp_dev);
139 #define IOMMU_GROUP_ATTR(_name, _mode, _show, _store) \
140 struct iommu_group_attribute iommu_group_attr_##_name = \
141 __ATTR(_name, _mode, _show, _store)
143 #define to_iommu_group_attr(_attr) \
144 container_of(_attr, struct iommu_group_attribute, attr)
145 #define to_iommu_group(_kobj) \
146 container_of(_kobj, struct iommu_group, kobj)
148 static LIST_HEAD(iommu_device_list);
149 static DEFINE_SPINLOCK(iommu_device_lock);
151 static const struct bus_type * const iommu_buses[] = {
156 #ifdef CONFIG_ARM_AMBA
159 #ifdef CONFIG_FSL_MC_BUS
162 #ifdef CONFIG_TEGRA_HOST1X_CONTEXT_BUS
163 &host1x_context_device_bus_type,
165 #ifdef CONFIG_CDX_BUS
171 * Use a function instead of an array here because the domain-type is a
172 * bit-field, so an array would waste memory.
174 static const char *iommu_domain_type_str(unsigned int t)
177 case IOMMU_DOMAIN_BLOCKED:
179 case IOMMU_DOMAIN_IDENTITY:
180 return "Passthrough";
181 case IOMMU_DOMAIN_UNMANAGED:
183 case IOMMU_DOMAIN_DMA:
184 case IOMMU_DOMAIN_DMA_FQ:
186 case IOMMU_DOMAIN_PLATFORM:
193 static int __init iommu_subsys_init(void)
195 struct notifier_block *nb;
197 if (!(iommu_cmd_line & IOMMU_CMD_LINE_DMA_API)) {
198 if (IS_ENABLED(CONFIG_IOMMU_DEFAULT_PASSTHROUGH))
199 iommu_set_default_passthrough(false);
201 iommu_set_default_translated(false);
203 if (iommu_default_passthrough() && cc_platform_has(CC_ATTR_MEM_ENCRYPT)) {
204 pr_info("Memory encryption detected - Disabling default IOMMU Passthrough\n");
205 iommu_set_default_translated(false);
209 if (!iommu_default_passthrough() && !iommu_dma_strict)
210 iommu_def_domain_type = IOMMU_DOMAIN_DMA_FQ;
212 pr_info("Default domain type: %s%s\n",
213 iommu_domain_type_str(iommu_def_domain_type),
214 (iommu_cmd_line & IOMMU_CMD_LINE_DMA_API) ?
215 " (set via kernel command line)" : "");
217 if (!iommu_default_passthrough())
218 pr_info("DMA domain TLB invalidation policy: %s mode%s\n",
219 iommu_dma_strict ? "strict" : "lazy",
220 (iommu_cmd_line & IOMMU_CMD_LINE_STRICT) ?
221 " (set via kernel command line)" : "");
223 nb = kcalloc(ARRAY_SIZE(iommu_buses), sizeof(*nb), GFP_KERNEL);
227 for (int i = 0; i < ARRAY_SIZE(iommu_buses); i++) {
228 nb[i].notifier_call = iommu_bus_notifier;
229 bus_register_notifier(iommu_buses[i], &nb[i]);
234 subsys_initcall(iommu_subsys_init);
236 static int remove_iommu_group(struct device *dev, void *data)
238 if (dev->iommu && dev->iommu->iommu_dev == data)
239 iommu_release_device(dev);
245 * iommu_device_register() - Register an IOMMU hardware instance
246 * @iommu: IOMMU handle for the instance
247 * @ops: IOMMU ops to associate with the instance
248 * @hwdev: (optional) actual instance device, used for fwnode lookup
250 * Return: 0 on success, or an error.
252 int iommu_device_register(struct iommu_device *iommu,
253 const struct iommu_ops *ops, struct device *hwdev)
257 /* We need to be able to take module references appropriately */
258 if (WARN_ON(is_module_address((unsigned long)ops) && !ops->owner))
263 iommu->fwnode = dev_fwnode(hwdev);
265 spin_lock(&iommu_device_lock);
266 list_add_tail(&iommu->list, &iommu_device_list);
267 spin_unlock(&iommu_device_lock);
269 for (int i = 0; i < ARRAY_SIZE(iommu_buses) && !err; i++)
270 err = bus_iommu_probe(iommu_buses[i]);
272 iommu_device_unregister(iommu);
275 EXPORT_SYMBOL_GPL(iommu_device_register);
277 void iommu_device_unregister(struct iommu_device *iommu)
279 for (int i = 0; i < ARRAY_SIZE(iommu_buses); i++)
280 bus_for_each_dev(iommu_buses[i], NULL, iommu, remove_iommu_group);
282 spin_lock(&iommu_device_lock);
283 list_del(&iommu->list);
284 spin_unlock(&iommu_device_lock);
286 /* Pairs with the alloc in generic_single_device_group() */
287 iommu_group_put(iommu->singleton_group);
288 iommu->singleton_group = NULL;
290 EXPORT_SYMBOL_GPL(iommu_device_unregister);
292 #if IS_ENABLED(CONFIG_IOMMUFD_TEST)
293 void iommu_device_unregister_bus(struct iommu_device *iommu,
294 struct bus_type *bus,
295 struct notifier_block *nb)
297 bus_unregister_notifier(bus, nb);
298 iommu_device_unregister(iommu);
300 EXPORT_SYMBOL_GPL(iommu_device_unregister_bus);
303 * Register an iommu driver against a single bus. This is only used by iommufd
304 * selftest to create a mock iommu driver. The caller must provide
305 * some memory to hold a notifier_block.
307 int iommu_device_register_bus(struct iommu_device *iommu,
308 const struct iommu_ops *ops, struct bus_type *bus,
309 struct notifier_block *nb)
314 nb->notifier_call = iommu_bus_notifier;
315 err = bus_register_notifier(bus, nb);
319 spin_lock(&iommu_device_lock);
320 list_add_tail(&iommu->list, &iommu_device_list);
321 spin_unlock(&iommu_device_lock);
323 err = bus_iommu_probe(bus);
325 iommu_device_unregister_bus(iommu, bus, nb);
330 EXPORT_SYMBOL_GPL(iommu_device_register_bus);
333 static struct dev_iommu *dev_iommu_get(struct device *dev)
335 struct dev_iommu *param = dev->iommu;
340 param = kzalloc(sizeof(*param), GFP_KERNEL);
344 mutex_init(¶m->lock);
349 static void dev_iommu_free(struct device *dev)
351 struct dev_iommu *param = dev->iommu;
355 fwnode_handle_put(param->fwspec->iommu_fwnode);
356 kfree(param->fwspec);
362 * Internal equivalent of device_iommu_mapped() for when we care that a device
363 * actually has API ops, and don't want false positives from VFIO-only groups.
365 static bool dev_has_iommu(struct device *dev)
367 return dev->iommu && dev->iommu->iommu_dev;
370 static u32 dev_iommu_get_max_pasids(struct device *dev)
372 u32 max_pasids = 0, bits = 0;
375 if (dev_is_pci(dev)) {
376 ret = pci_max_pasids(to_pci_dev(dev));
380 ret = device_property_read_u32(dev, "pasid-num-bits", &bits);
382 max_pasids = 1UL << bits;
385 return min_t(u32, max_pasids, dev->iommu->iommu_dev->max_pasids);
389 * Init the dev->iommu and dev->iommu_group in the struct device and get the
392 static int iommu_init_device(struct device *dev, const struct iommu_ops *ops)
394 struct iommu_device *iommu_dev;
395 struct iommu_group *group;
398 if (!dev_iommu_get(dev))
401 if (!try_module_get(ops->owner)) {
406 iommu_dev = ops->probe_device(dev);
407 if (IS_ERR(iommu_dev)) {
408 ret = PTR_ERR(iommu_dev);
411 dev->iommu->iommu_dev = iommu_dev;
413 ret = iommu_device_link(iommu_dev, dev);
417 group = ops->device_group(dev);
418 if (WARN_ON_ONCE(group == NULL))
419 group = ERR_PTR(-EINVAL);
421 ret = PTR_ERR(group);
424 dev->iommu_group = group;
426 dev->iommu->max_pasids = dev_iommu_get_max_pasids(dev);
427 if (ops->is_attach_deferred)
428 dev->iommu->attach_deferred = ops->is_attach_deferred(dev);
432 iommu_device_unlink(iommu_dev, dev);
434 if (ops->release_device)
435 ops->release_device(dev);
437 module_put(ops->owner);
439 dev->iommu->iommu_dev = NULL;
444 static void iommu_deinit_device(struct device *dev)
446 struct iommu_group *group = dev->iommu_group;
447 const struct iommu_ops *ops = dev_iommu_ops(dev);
449 lockdep_assert_held(&group->mutex);
451 iommu_device_unlink(dev->iommu->iommu_dev, dev);
454 * release_device() must stop using any attached domain on the device.
455 * If there are still other devices in the group they are not effected
458 * The IOMMU driver must set the device to either an identity or
459 * blocking translation and stop using any domain pointer, as it is
462 if (ops->release_device)
463 ops->release_device(dev);
466 * If this is the last driver to use the group then we must free the
467 * domains before we do the module_put().
469 if (list_empty(&group->devices)) {
470 if (group->default_domain) {
471 iommu_domain_free(group->default_domain);
472 group->default_domain = NULL;
474 if (group->blocking_domain) {
475 iommu_domain_free(group->blocking_domain);
476 group->blocking_domain = NULL;
478 group->domain = NULL;
481 /* Caller must put iommu_group */
482 dev->iommu_group = NULL;
483 module_put(ops->owner);
487 static int __iommu_probe_device(struct device *dev, struct list_head *group_list)
489 const struct iommu_ops *ops;
490 struct iommu_fwspec *fwspec;
491 struct iommu_group *group;
492 static DEFINE_MUTEX(iommu_probe_device_lock);
493 struct group_device *gdev;
497 * For FDT-based systems and ACPI IORT/VIOT, drivers register IOMMU
498 * instances with non-NULL fwnodes, and client devices should have been
499 * identified with a fwspec by this point. Otherwise, we can currently
500 * assume that only one of Intel, AMD, s390, PAMU or legacy SMMUv2 can
501 * be present, and that any of their registered instances has suitable
502 * ops for probing, and thus cheekily co-opt the same mechanism.
504 fwspec = dev_iommu_fwspec_get(dev);
505 if (fwspec && fwspec->ops)
508 ops = iommu_ops_from_fwnode(NULL);
513 * Serialise to avoid races between IOMMU drivers registering in
514 * parallel and/or the "replay" calls from ACPI/OF code via client
515 * driver probe. Once the latter have been cleaned up we should
516 * probably be able to use device_lock() here to minimise the scope,
517 * but for now enforcing a simple global ordering is fine.
519 mutex_lock(&iommu_probe_device_lock);
521 /* Device is probed already if in a group */
522 if (dev->iommu_group) {
527 ret = iommu_init_device(dev, ops);
531 group = dev->iommu_group;
532 gdev = iommu_group_alloc_device(group, dev);
533 mutex_lock(&group->mutex);
540 * The gdev must be in the list before calling
541 * iommu_setup_default_domain()
543 list_add_tail(&gdev->list, &group->devices);
544 WARN_ON(group->default_domain && !group->domain);
545 if (group->default_domain)
546 iommu_create_device_direct_mappings(group->default_domain, dev);
548 ret = __iommu_device_set_domain(group, dev, group->domain, 0);
550 goto err_remove_gdev;
551 } else if (!group->default_domain && !group_list) {
552 ret = iommu_setup_default_domain(group, 0);
554 goto err_remove_gdev;
555 } else if (!group->default_domain) {
557 * With a group_list argument we defer the default_domain setup
558 * to the caller by providing a de-duplicated list of groups
559 * that need further setup.
561 if (list_empty(&group->entry))
562 list_add_tail(&group->entry, group_list);
564 mutex_unlock(&group->mutex);
565 mutex_unlock(&iommu_probe_device_lock);
568 iommu_dma_set_pci_32bit_workaround(dev);
573 list_del(&gdev->list);
574 __iommu_group_free_device(group, gdev);
576 iommu_deinit_device(dev);
577 mutex_unlock(&group->mutex);
578 iommu_group_put(group);
580 mutex_unlock(&iommu_probe_device_lock);
585 int iommu_probe_device(struct device *dev)
587 const struct iommu_ops *ops;
590 ret = __iommu_probe_device(dev, NULL);
594 ops = dev_iommu_ops(dev);
595 if (ops->probe_finalize)
596 ops->probe_finalize(dev);
601 static void __iommu_group_free_device(struct iommu_group *group,
602 struct group_device *grp_dev)
604 struct device *dev = grp_dev->dev;
606 sysfs_remove_link(group->devices_kobj, grp_dev->name);
607 sysfs_remove_link(&dev->kobj, "iommu_group");
609 trace_remove_device_from_group(group->id, dev);
612 * If the group has become empty then ownership must have been
613 * released, and the current domain must be set back to NULL or
614 * the default domain.
616 if (list_empty(&group->devices))
617 WARN_ON(group->owner_cnt ||
618 group->domain != group->default_domain);
620 kfree(grp_dev->name);
624 /* Remove the iommu_group from the struct device. */
625 static void __iommu_group_remove_device(struct device *dev)
627 struct iommu_group *group = dev->iommu_group;
628 struct group_device *device;
630 mutex_lock(&group->mutex);
631 for_each_group_device(group, device) {
632 if (device->dev != dev)
635 list_del(&device->list);
636 __iommu_group_free_device(group, device);
637 if (dev_has_iommu(dev))
638 iommu_deinit_device(dev);
640 dev->iommu_group = NULL;
643 mutex_unlock(&group->mutex);
646 * Pairs with the get in iommu_init_device() or
647 * iommu_group_add_device()
649 iommu_group_put(group);
652 static void iommu_release_device(struct device *dev)
654 struct iommu_group *group = dev->iommu_group;
657 __iommu_group_remove_device(dev);
659 /* Free any fwspec if no iommu_driver was ever attached */
664 static int __init iommu_set_def_domain_type(char *str)
669 ret = kstrtobool(str, &pt);
674 iommu_set_default_passthrough(true);
676 iommu_set_default_translated(true);
680 early_param("iommu.passthrough", iommu_set_def_domain_type);
682 static int __init iommu_dma_setup(char *str)
684 int ret = kstrtobool(str, &iommu_dma_strict);
687 iommu_cmd_line |= IOMMU_CMD_LINE_STRICT;
690 early_param("iommu.strict", iommu_dma_setup);
692 void iommu_set_dma_strict(void)
694 iommu_dma_strict = true;
695 if (iommu_def_domain_type == IOMMU_DOMAIN_DMA_FQ)
696 iommu_def_domain_type = IOMMU_DOMAIN_DMA;
699 static ssize_t iommu_group_attr_show(struct kobject *kobj,
700 struct attribute *__attr, char *buf)
702 struct iommu_group_attribute *attr = to_iommu_group_attr(__attr);
703 struct iommu_group *group = to_iommu_group(kobj);
707 ret = attr->show(group, buf);
711 static ssize_t iommu_group_attr_store(struct kobject *kobj,
712 struct attribute *__attr,
713 const char *buf, size_t count)
715 struct iommu_group_attribute *attr = to_iommu_group_attr(__attr);
716 struct iommu_group *group = to_iommu_group(kobj);
720 ret = attr->store(group, buf, count);
724 static const struct sysfs_ops iommu_group_sysfs_ops = {
725 .show = iommu_group_attr_show,
726 .store = iommu_group_attr_store,
729 static int iommu_group_create_file(struct iommu_group *group,
730 struct iommu_group_attribute *attr)
732 return sysfs_create_file(&group->kobj, &attr->attr);
735 static void iommu_group_remove_file(struct iommu_group *group,
736 struct iommu_group_attribute *attr)
738 sysfs_remove_file(&group->kobj, &attr->attr);
741 static ssize_t iommu_group_show_name(struct iommu_group *group, char *buf)
743 return sysfs_emit(buf, "%s\n", group->name);
747 * iommu_insert_resv_region - Insert a new region in the
748 * list of reserved regions.
749 * @new: new region to insert
750 * @regions: list of regions
752 * Elements are sorted by start address and overlapping segments
753 * of the same type are merged.
755 static int iommu_insert_resv_region(struct iommu_resv_region *new,
756 struct list_head *regions)
758 struct iommu_resv_region *iter, *tmp, *nr, *top;
761 nr = iommu_alloc_resv_region(new->start, new->length,
762 new->prot, new->type, GFP_KERNEL);
766 /* First add the new element based on start address sorting */
767 list_for_each_entry(iter, regions, list) {
768 if (nr->start < iter->start ||
769 (nr->start == iter->start && nr->type <= iter->type))
772 list_add_tail(&nr->list, &iter->list);
774 /* Merge overlapping segments of type nr->type in @regions, if any */
775 list_for_each_entry_safe(iter, tmp, regions, list) {
776 phys_addr_t top_end, iter_end = iter->start + iter->length - 1;
778 /* no merge needed on elements of different types than @new */
779 if (iter->type != new->type) {
780 list_move_tail(&iter->list, &stack);
784 /* look for the last stack element of same type as @iter */
785 list_for_each_entry_reverse(top, &stack, list)
786 if (top->type == iter->type)
789 list_move_tail(&iter->list, &stack);
793 top_end = top->start + top->length - 1;
795 if (iter->start > top_end + 1) {
796 list_move_tail(&iter->list, &stack);
798 top->length = max(top_end, iter_end) - top->start + 1;
799 list_del(&iter->list);
803 list_splice(&stack, regions);
808 iommu_insert_device_resv_regions(struct list_head *dev_resv_regions,
809 struct list_head *group_resv_regions)
811 struct iommu_resv_region *entry;
814 list_for_each_entry(entry, dev_resv_regions, list) {
815 ret = iommu_insert_resv_region(entry, group_resv_regions);
822 int iommu_get_group_resv_regions(struct iommu_group *group,
823 struct list_head *head)
825 struct group_device *device;
828 mutex_lock(&group->mutex);
829 for_each_group_device(group, device) {
830 struct list_head dev_resv_regions;
833 * Non-API groups still expose reserved_regions in sysfs,
834 * so filter out calls that get here that way.
836 if (!dev_has_iommu(device->dev))
839 INIT_LIST_HEAD(&dev_resv_regions);
840 iommu_get_resv_regions(device->dev, &dev_resv_regions);
841 ret = iommu_insert_device_resv_regions(&dev_resv_regions, head);
842 iommu_put_resv_regions(device->dev, &dev_resv_regions);
846 mutex_unlock(&group->mutex);
849 EXPORT_SYMBOL_GPL(iommu_get_group_resv_regions);
851 static ssize_t iommu_group_show_resv_regions(struct iommu_group *group,
854 struct iommu_resv_region *region, *next;
855 struct list_head group_resv_regions;
858 INIT_LIST_HEAD(&group_resv_regions);
859 iommu_get_group_resv_regions(group, &group_resv_regions);
861 list_for_each_entry_safe(region, next, &group_resv_regions, list) {
862 offset += sysfs_emit_at(buf, offset, "0x%016llx 0x%016llx %s\n",
863 (long long)region->start,
864 (long long)(region->start +
866 iommu_group_resv_type_string[region->type]);
873 static ssize_t iommu_group_show_type(struct iommu_group *group,
876 char *type = "unknown";
878 mutex_lock(&group->mutex);
879 if (group->default_domain) {
880 switch (group->default_domain->type) {
881 case IOMMU_DOMAIN_BLOCKED:
884 case IOMMU_DOMAIN_IDENTITY:
887 case IOMMU_DOMAIN_UNMANAGED:
890 case IOMMU_DOMAIN_DMA:
893 case IOMMU_DOMAIN_DMA_FQ:
898 mutex_unlock(&group->mutex);
900 return sysfs_emit(buf, "%s\n", type);
903 static IOMMU_GROUP_ATTR(name, S_IRUGO, iommu_group_show_name, NULL);
905 static IOMMU_GROUP_ATTR(reserved_regions, 0444,
906 iommu_group_show_resv_regions, NULL);
908 static IOMMU_GROUP_ATTR(type, 0644, iommu_group_show_type,
909 iommu_group_store_type);
911 static void iommu_group_release(struct kobject *kobj)
913 struct iommu_group *group = to_iommu_group(kobj);
915 pr_debug("Releasing group %d\n", group->id);
917 if (group->iommu_data_release)
918 group->iommu_data_release(group->iommu_data);
920 ida_free(&iommu_group_ida, group->id);
922 /* Domains are free'd by iommu_deinit_device() */
923 WARN_ON(group->default_domain);
924 WARN_ON(group->blocking_domain);
930 static const struct kobj_type iommu_group_ktype = {
931 .sysfs_ops = &iommu_group_sysfs_ops,
932 .release = iommu_group_release,
936 * iommu_group_alloc - Allocate a new group
938 * This function is called by an iommu driver to allocate a new iommu
939 * group. The iommu group represents the minimum granularity of the iommu.
940 * Upon successful return, the caller holds a reference to the supplied
941 * group in order to hold the group until devices are added. Use
942 * iommu_group_put() to release this extra reference count, allowing the
943 * group to be automatically reclaimed once it has no devices or external
946 struct iommu_group *iommu_group_alloc(void)
948 struct iommu_group *group;
951 group = kzalloc(sizeof(*group), GFP_KERNEL);
953 return ERR_PTR(-ENOMEM);
955 group->kobj.kset = iommu_group_kset;
956 mutex_init(&group->mutex);
957 INIT_LIST_HEAD(&group->devices);
958 INIT_LIST_HEAD(&group->entry);
959 xa_init(&group->pasid_array);
961 ret = ida_alloc(&iommu_group_ida, GFP_KERNEL);
968 ret = kobject_init_and_add(&group->kobj, &iommu_group_ktype,
969 NULL, "%d", group->id);
971 kobject_put(&group->kobj);
975 group->devices_kobj = kobject_create_and_add("devices", &group->kobj);
976 if (!group->devices_kobj) {
977 kobject_put(&group->kobj); /* triggers .release & free */
978 return ERR_PTR(-ENOMEM);
982 * The devices_kobj holds a reference on the group kobject, so
983 * as long as that exists so will the group. We can therefore
984 * use the devices_kobj for reference counting.
986 kobject_put(&group->kobj);
988 ret = iommu_group_create_file(group,
989 &iommu_group_attr_reserved_regions);
991 kobject_put(group->devices_kobj);
995 ret = iommu_group_create_file(group, &iommu_group_attr_type);
997 kobject_put(group->devices_kobj);
1001 pr_debug("Allocated group %d\n", group->id);
1005 EXPORT_SYMBOL_GPL(iommu_group_alloc);
1008 * iommu_group_get_iommudata - retrieve iommu_data registered for a group
1011 * iommu drivers can store data in the group for use when doing iommu
1012 * operations. This function provides a way to retrieve it. Caller
1013 * should hold a group reference.
1015 void *iommu_group_get_iommudata(struct iommu_group *group)
1017 return group->iommu_data;
1019 EXPORT_SYMBOL_GPL(iommu_group_get_iommudata);
1022 * iommu_group_set_iommudata - set iommu_data for a group
1024 * @iommu_data: new data
1025 * @release: release function for iommu_data
1027 * iommu drivers can store data in the group for use when doing iommu
1028 * operations. This function provides a way to set the data after
1029 * the group has been allocated. Caller should hold a group reference.
1031 void iommu_group_set_iommudata(struct iommu_group *group, void *iommu_data,
1032 void (*release)(void *iommu_data))
1034 group->iommu_data = iommu_data;
1035 group->iommu_data_release = release;
1037 EXPORT_SYMBOL_GPL(iommu_group_set_iommudata);
1040 * iommu_group_set_name - set name for a group
1044 * Allow iommu driver to set a name for a group. When set it will
1045 * appear in a name attribute file under the group in sysfs.
1047 int iommu_group_set_name(struct iommu_group *group, const char *name)
1052 iommu_group_remove_file(group, &iommu_group_attr_name);
1059 group->name = kstrdup(name, GFP_KERNEL);
1063 ret = iommu_group_create_file(group, &iommu_group_attr_name);
1072 EXPORT_SYMBOL_GPL(iommu_group_set_name);
1074 static int iommu_create_device_direct_mappings(struct iommu_domain *domain,
1077 struct iommu_resv_region *entry;
1078 struct list_head mappings;
1079 unsigned long pg_size;
1082 pg_size = domain->pgsize_bitmap ? 1UL << __ffs(domain->pgsize_bitmap) : 0;
1083 INIT_LIST_HEAD(&mappings);
1085 if (WARN_ON_ONCE(iommu_is_dma_domain(domain) && !pg_size))
1088 iommu_get_resv_regions(dev, &mappings);
1090 /* We need to consider overlapping regions for different devices */
1091 list_for_each_entry(entry, &mappings, list) {
1092 dma_addr_t start, end, addr;
1093 size_t map_size = 0;
1095 if (entry->type == IOMMU_RESV_DIRECT)
1096 dev->iommu->require_direct = 1;
1098 if ((entry->type != IOMMU_RESV_DIRECT &&
1099 entry->type != IOMMU_RESV_DIRECT_RELAXABLE) ||
1100 !iommu_is_dma_domain(domain))
1103 start = ALIGN(entry->start, pg_size);
1104 end = ALIGN(entry->start + entry->length, pg_size);
1106 for (addr = start; addr <= end; addr += pg_size) {
1107 phys_addr_t phys_addr;
1112 phys_addr = iommu_iova_to_phys(domain, addr);
1114 map_size += pg_size;
1120 ret = iommu_map(domain, addr - map_size,
1121 addr - map_size, map_size,
1122 entry->prot, GFP_KERNEL);
1131 if (!list_empty(&mappings) && iommu_is_dma_domain(domain))
1132 iommu_flush_iotlb_all(domain);
1135 iommu_put_resv_regions(dev, &mappings);
1140 /* This is undone by __iommu_group_free_device() */
1141 static struct group_device *iommu_group_alloc_device(struct iommu_group *group,
1145 struct group_device *device;
1147 device = kzalloc(sizeof(*device), GFP_KERNEL);
1149 return ERR_PTR(-ENOMEM);
1153 ret = sysfs_create_link(&dev->kobj, &group->kobj, "iommu_group");
1155 goto err_free_device;
1157 device->name = kasprintf(GFP_KERNEL, "%s", kobject_name(&dev->kobj));
1159 if (!device->name) {
1161 goto err_remove_link;
1164 ret = sysfs_create_link_nowarn(group->devices_kobj,
1165 &dev->kobj, device->name);
1167 if (ret == -EEXIST && i >= 0) {
1169 * Account for the slim chance of collision
1170 * and append an instance to the name.
1172 kfree(device->name);
1173 device->name = kasprintf(GFP_KERNEL, "%s.%d",
1174 kobject_name(&dev->kobj), i++);
1180 trace_add_device_to_group(group->id, dev);
1182 dev_info(dev, "Adding to iommu group %d\n", group->id);
1187 kfree(device->name);
1189 sysfs_remove_link(&dev->kobj, "iommu_group");
1192 dev_err(dev, "Failed to add to iommu group %d: %d\n", group->id, ret);
1193 return ERR_PTR(ret);
1197 * iommu_group_add_device - add a device to an iommu group
1198 * @group: the group into which to add the device (reference should be held)
1201 * This function is called by an iommu driver to add a device into a
1202 * group. Adding a device increments the group reference count.
1204 int iommu_group_add_device(struct iommu_group *group, struct device *dev)
1206 struct group_device *gdev;
1208 gdev = iommu_group_alloc_device(group, dev);
1210 return PTR_ERR(gdev);
1212 iommu_group_ref_get(group);
1213 dev->iommu_group = group;
1215 mutex_lock(&group->mutex);
1216 list_add_tail(&gdev->list, &group->devices);
1217 mutex_unlock(&group->mutex);
1220 EXPORT_SYMBOL_GPL(iommu_group_add_device);
1223 * iommu_group_remove_device - remove a device from it's current group
1224 * @dev: device to be removed
1226 * This function is called by an iommu driver to remove the device from
1227 * it's current group. This decrements the iommu group reference count.
1229 void iommu_group_remove_device(struct device *dev)
1231 struct iommu_group *group = dev->iommu_group;
1236 dev_info(dev, "Removing from iommu group %d\n", group->id);
1238 __iommu_group_remove_device(dev);
1240 EXPORT_SYMBOL_GPL(iommu_group_remove_device);
1242 static struct device *iommu_group_first_dev(struct iommu_group *group)
1244 lockdep_assert_held(&group->mutex);
1245 return list_first_entry(&group->devices, struct group_device, list)->dev;
1249 * iommu_group_for_each_dev - iterate over each device in the group
1251 * @data: caller opaque data to be passed to callback function
1252 * @fn: caller supplied callback function
1254 * This function is called by group users to iterate over group devices.
1255 * Callers should hold a reference count to the group during callback.
1256 * The group->mutex is held across callbacks, which will block calls to
1257 * iommu_group_add/remove_device.
1259 int iommu_group_for_each_dev(struct iommu_group *group, void *data,
1260 int (*fn)(struct device *, void *))
1262 struct group_device *device;
1265 mutex_lock(&group->mutex);
1266 for_each_group_device(group, device) {
1267 ret = fn(device->dev, data);
1271 mutex_unlock(&group->mutex);
1275 EXPORT_SYMBOL_GPL(iommu_group_for_each_dev);
1278 * iommu_group_get - Return the group for a device and increment reference
1279 * @dev: get the group that this device belongs to
1281 * This function is called by iommu drivers and users to get the group
1282 * for the specified device. If found, the group is returned and the group
1283 * reference in incremented, else NULL.
1285 struct iommu_group *iommu_group_get(struct device *dev)
1287 struct iommu_group *group = dev->iommu_group;
1290 kobject_get(group->devices_kobj);
1294 EXPORT_SYMBOL_GPL(iommu_group_get);
1297 * iommu_group_ref_get - Increment reference on a group
1298 * @group: the group to use, must not be NULL
1300 * This function is called by iommu drivers to take additional references on an
1301 * existing group. Returns the given group for convenience.
1303 struct iommu_group *iommu_group_ref_get(struct iommu_group *group)
1305 kobject_get(group->devices_kobj);
1308 EXPORT_SYMBOL_GPL(iommu_group_ref_get);
1311 * iommu_group_put - Decrement group reference
1312 * @group: the group to use
1314 * This function is called by iommu drivers and users to release the
1315 * iommu group. Once the reference count is zero, the group is released.
1317 void iommu_group_put(struct iommu_group *group)
1320 kobject_put(group->devices_kobj);
1322 EXPORT_SYMBOL_GPL(iommu_group_put);
1325 * iommu_register_device_fault_handler() - Register a device fault handler
1327 * @handler: the fault handler
1328 * @data: private data passed as argument to the handler
1330 * When an IOMMU fault event is received, this handler gets called with the
1331 * fault event and data as argument. The handler should return 0 on success. If
1332 * the fault is recoverable (IOMMU_FAULT_PAGE_REQ), the consumer should also
1333 * complete the fault by calling iommu_page_response() with one of the following
1335 * - IOMMU_PAGE_RESP_SUCCESS: retry the translation
1336 * - IOMMU_PAGE_RESP_INVALID: terminate the fault
1337 * - IOMMU_PAGE_RESP_FAILURE: terminate the fault and stop reporting
1338 * page faults if possible.
1340 * Return 0 if the fault handler was installed successfully, or an error.
1342 int iommu_register_device_fault_handler(struct device *dev,
1343 iommu_dev_fault_handler_t handler,
1346 struct dev_iommu *param = dev->iommu;
1352 mutex_lock(¶m->lock);
1353 /* Only allow one fault handler registered for each device */
1354 if (param->fault_param) {
1360 param->fault_param = kzalloc(sizeof(*param->fault_param), GFP_KERNEL);
1361 if (!param->fault_param) {
1366 param->fault_param->handler = handler;
1367 param->fault_param->data = data;
1368 mutex_init(¶m->fault_param->lock);
1369 INIT_LIST_HEAD(¶m->fault_param->faults);
1372 mutex_unlock(¶m->lock);
1376 EXPORT_SYMBOL_GPL(iommu_register_device_fault_handler);
1379 * iommu_unregister_device_fault_handler() - Unregister the device fault handler
1382 * Remove the device fault handler installed with
1383 * iommu_register_device_fault_handler().
1385 * Return 0 on success, or an error.
1387 int iommu_unregister_device_fault_handler(struct device *dev)
1389 struct dev_iommu *param = dev->iommu;
1395 mutex_lock(¶m->lock);
1397 if (!param->fault_param)
1400 /* we cannot unregister handler if there are pending faults */
1401 if (!list_empty(¶m->fault_param->faults)) {
1406 kfree(param->fault_param);
1407 param->fault_param = NULL;
1410 mutex_unlock(¶m->lock);
1414 EXPORT_SYMBOL_GPL(iommu_unregister_device_fault_handler);
1417 * iommu_report_device_fault() - Report fault event to device driver
1419 * @evt: fault event data
1421 * Called by IOMMU drivers when a fault is detected, typically in a threaded IRQ
1422 * handler. When this function fails and the fault is recoverable, it is the
1423 * caller's responsibility to complete the fault.
1425 * Return 0 on success, or an error.
1427 int iommu_report_device_fault(struct device *dev, struct iommu_fault_event *evt)
1429 struct dev_iommu *param = dev->iommu;
1430 struct iommu_fault_event *evt_pending = NULL;
1431 struct iommu_fault_param *fparam;
1437 /* we only report device fault if there is a handler registered */
1438 mutex_lock(¶m->lock);
1439 fparam = param->fault_param;
1440 if (!fparam || !fparam->handler) {
1445 if (evt->fault.type == IOMMU_FAULT_PAGE_REQ &&
1446 (evt->fault.prm.flags & IOMMU_FAULT_PAGE_REQUEST_LAST_PAGE)) {
1447 evt_pending = kmemdup(evt, sizeof(struct iommu_fault_event),
1453 mutex_lock(&fparam->lock);
1454 list_add_tail(&evt_pending->list, &fparam->faults);
1455 mutex_unlock(&fparam->lock);
1458 ret = fparam->handler(&evt->fault, fparam->data);
1459 if (ret && evt_pending) {
1460 mutex_lock(&fparam->lock);
1461 list_del(&evt_pending->list);
1462 mutex_unlock(&fparam->lock);
1466 mutex_unlock(¶m->lock);
1469 EXPORT_SYMBOL_GPL(iommu_report_device_fault);
1471 int iommu_page_response(struct device *dev,
1472 struct iommu_page_response *msg)
1476 struct iommu_fault_event *evt;
1477 struct iommu_fault_page_request *prm;
1478 struct dev_iommu *param = dev->iommu;
1479 const struct iommu_ops *ops = dev_iommu_ops(dev);
1480 bool has_pasid = msg->flags & IOMMU_PAGE_RESP_PASID_VALID;
1482 if (!ops->page_response)
1485 if (!param || !param->fault_param)
1488 if (msg->version != IOMMU_PAGE_RESP_VERSION_1 ||
1489 msg->flags & ~IOMMU_PAGE_RESP_PASID_VALID)
1492 /* Only send response if there is a fault report pending */
1493 mutex_lock(¶m->fault_param->lock);
1494 if (list_empty(¶m->fault_param->faults)) {
1495 dev_warn_ratelimited(dev, "no pending PRQ, drop response\n");
1499 * Check if we have a matching page request pending to respond,
1500 * otherwise return -EINVAL
1502 list_for_each_entry(evt, ¶m->fault_param->faults, list) {
1503 prm = &evt->fault.prm;
1504 if (prm->grpid != msg->grpid)
1508 * If the PASID is required, the corresponding request is
1509 * matched using the group ID, the PASID valid bit and the PASID
1510 * value. Otherwise only the group ID matches request and
1513 needs_pasid = prm->flags & IOMMU_FAULT_PAGE_RESPONSE_NEEDS_PASID;
1514 if (needs_pasid && (!has_pasid || msg->pasid != prm->pasid))
1517 if (!needs_pasid && has_pasid) {
1518 /* No big deal, just clear it. */
1519 msg->flags &= ~IOMMU_PAGE_RESP_PASID_VALID;
1523 ret = ops->page_response(dev, evt, msg);
1524 list_del(&evt->list);
1530 mutex_unlock(¶m->fault_param->lock);
1533 EXPORT_SYMBOL_GPL(iommu_page_response);
1536 * iommu_group_id - Return ID for a group
1537 * @group: the group to ID
1539 * Return the unique ID for the group matching the sysfs group number.
1541 int iommu_group_id(struct iommu_group *group)
1545 EXPORT_SYMBOL_GPL(iommu_group_id);
1547 static struct iommu_group *get_pci_alias_group(struct pci_dev *pdev,
1548 unsigned long *devfns);
1551 * To consider a PCI device isolated, we require ACS to support Source
1552 * Validation, Request Redirection, Completer Redirection, and Upstream
1553 * Forwarding. This effectively means that devices cannot spoof their
1554 * requester ID, requests and completions cannot be redirected, and all
1555 * transactions are forwarded upstream, even as it passes through a
1556 * bridge where the target device is downstream.
1558 #define REQ_ACS_FLAGS (PCI_ACS_SV | PCI_ACS_RR | PCI_ACS_CR | PCI_ACS_UF)
1561 * For multifunction devices which are not isolated from each other, find
1562 * all the other non-isolated functions and look for existing groups. For
1563 * each function, we also need to look for aliases to or from other devices
1564 * that may already have a group.
1566 static struct iommu_group *get_pci_function_alias_group(struct pci_dev *pdev,
1567 unsigned long *devfns)
1569 struct pci_dev *tmp = NULL;
1570 struct iommu_group *group;
1572 if (!pdev->multifunction || pci_acs_enabled(pdev, REQ_ACS_FLAGS))
1575 for_each_pci_dev(tmp) {
1576 if (tmp == pdev || tmp->bus != pdev->bus ||
1577 PCI_SLOT(tmp->devfn) != PCI_SLOT(pdev->devfn) ||
1578 pci_acs_enabled(tmp, REQ_ACS_FLAGS))
1581 group = get_pci_alias_group(tmp, devfns);
1592 * Look for aliases to or from the given device for existing groups. DMA
1593 * aliases are only supported on the same bus, therefore the search
1594 * space is quite small (especially since we're really only looking at pcie
1595 * device, and therefore only expect multiple slots on the root complex or
1596 * downstream switch ports). It's conceivable though that a pair of
1597 * multifunction devices could have aliases between them that would cause a
1598 * loop. To prevent this, we use a bitmap to track where we've been.
1600 static struct iommu_group *get_pci_alias_group(struct pci_dev *pdev,
1601 unsigned long *devfns)
1603 struct pci_dev *tmp = NULL;
1604 struct iommu_group *group;
1606 if (test_and_set_bit(pdev->devfn & 0xff, devfns))
1609 group = iommu_group_get(&pdev->dev);
1613 for_each_pci_dev(tmp) {
1614 if (tmp == pdev || tmp->bus != pdev->bus)
1617 /* We alias them or they alias us */
1618 if (pci_devs_are_dma_aliases(pdev, tmp)) {
1619 group = get_pci_alias_group(tmp, devfns);
1625 group = get_pci_function_alias_group(tmp, devfns);
1636 struct group_for_pci_data {
1637 struct pci_dev *pdev;
1638 struct iommu_group *group;
1642 * DMA alias iterator callback, return the last seen device. Stop and return
1643 * the IOMMU group if we find one along the way.
1645 static int get_pci_alias_or_group(struct pci_dev *pdev, u16 alias, void *opaque)
1647 struct group_for_pci_data *data = opaque;
1650 data->group = iommu_group_get(&pdev->dev);
1652 return data->group != NULL;
1656 * Generic device_group call-back function. It just allocates one
1657 * iommu-group per device.
1659 struct iommu_group *generic_device_group(struct device *dev)
1661 return iommu_group_alloc();
1663 EXPORT_SYMBOL_GPL(generic_device_group);
1666 * Generic device_group call-back function. It just allocates one
1667 * iommu-group per iommu driver instance shared by every device
1668 * probed by that iommu driver.
1670 struct iommu_group *generic_single_device_group(struct device *dev)
1672 struct iommu_device *iommu = dev->iommu->iommu_dev;
1674 if (!iommu->singleton_group) {
1675 struct iommu_group *group;
1677 group = iommu_group_alloc();
1680 iommu->singleton_group = group;
1682 return iommu_group_ref_get(iommu->singleton_group);
1684 EXPORT_SYMBOL_GPL(generic_single_device_group);
1687 * Use standard PCI bus topology, isolation features, and DMA alias quirks
1688 * to find or create an IOMMU group for a device.
1690 struct iommu_group *pci_device_group(struct device *dev)
1692 struct pci_dev *pdev = to_pci_dev(dev);
1693 struct group_for_pci_data data;
1694 struct pci_bus *bus;
1695 struct iommu_group *group = NULL;
1696 u64 devfns[4] = { 0 };
1698 if (WARN_ON(!dev_is_pci(dev)))
1699 return ERR_PTR(-EINVAL);
1702 * Find the upstream DMA alias for the device. A device must not
1703 * be aliased due to topology in order to have its own IOMMU group.
1704 * If we find an alias along the way that already belongs to a
1707 if (pci_for_each_dma_alias(pdev, get_pci_alias_or_group, &data))
1713 * Continue upstream from the point of minimum IOMMU granularity
1714 * due to aliases to the point where devices are protected from
1715 * peer-to-peer DMA by PCI ACS. Again, if we find an existing
1718 for (bus = pdev->bus; !pci_is_root_bus(bus); bus = bus->parent) {
1722 if (pci_acs_path_enabled(bus->self, NULL, REQ_ACS_FLAGS))
1727 group = iommu_group_get(&pdev->dev);
1733 * Look for existing groups on device aliases. If we alias another
1734 * device or another device aliases us, use the same group.
1736 group = get_pci_alias_group(pdev, (unsigned long *)devfns);
1741 * Look for existing groups on non-isolated functions on the same
1742 * slot and aliases of those funcions, if any. No need to clear
1743 * the search bitmap, the tested devfns are still valid.
1745 group = get_pci_function_alias_group(pdev, (unsigned long *)devfns);
1749 /* No shared group found, allocate new */
1750 return iommu_group_alloc();
1752 EXPORT_SYMBOL_GPL(pci_device_group);
1754 /* Get the IOMMU group for device on fsl-mc bus */
1755 struct iommu_group *fsl_mc_device_group(struct device *dev)
1757 struct device *cont_dev = fsl_mc_cont_dev(dev);
1758 struct iommu_group *group;
1760 group = iommu_group_get(cont_dev);
1762 group = iommu_group_alloc();
1765 EXPORT_SYMBOL_GPL(fsl_mc_device_group);
1767 static struct iommu_domain *
1768 __iommu_group_alloc_default_domain(struct iommu_group *group, int req_type)
1770 if (group->default_domain && group->default_domain->type == req_type)
1771 return group->default_domain;
1772 return __iommu_group_domain_alloc(group, req_type);
1776 * req_type of 0 means "auto" which means to select a domain based on
1777 * iommu_def_domain_type or what the driver actually supports.
1779 static struct iommu_domain *
1780 iommu_group_alloc_default_domain(struct iommu_group *group, int req_type)
1782 const struct iommu_ops *ops = dev_iommu_ops(iommu_group_first_dev(group));
1783 struct iommu_domain *dom;
1785 lockdep_assert_held(&group->mutex);
1788 * Allow legacy drivers to specify the domain that will be the default
1789 * domain. This should always be either an IDENTITY/BLOCKED/PLATFORM
1790 * domain. Do not use in new drivers.
1792 if (ops->default_domain) {
1795 return ops->default_domain;
1799 return __iommu_group_alloc_default_domain(group, req_type);
1801 /* The driver gave no guidance on what type to use, try the default */
1802 dom = __iommu_group_alloc_default_domain(group, iommu_def_domain_type);
1806 /* Otherwise IDENTITY and DMA_FQ defaults will try DMA */
1807 if (iommu_def_domain_type == IOMMU_DOMAIN_DMA)
1809 dom = __iommu_group_alloc_default_domain(group, IOMMU_DOMAIN_DMA);
1813 pr_warn("Failed to allocate default IOMMU domain of type %u for group %s - Falling back to IOMMU_DOMAIN_DMA",
1814 iommu_def_domain_type, group->name);
1818 struct iommu_domain *iommu_group_default_domain(struct iommu_group *group)
1820 return group->default_domain;
1823 static int probe_iommu_group(struct device *dev, void *data)
1825 struct list_head *group_list = data;
1828 ret = __iommu_probe_device(dev, group_list);
1835 static int iommu_bus_notifier(struct notifier_block *nb,
1836 unsigned long action, void *data)
1838 struct device *dev = data;
1840 if (action == BUS_NOTIFY_ADD_DEVICE) {
1843 ret = iommu_probe_device(dev);
1844 return (ret) ? NOTIFY_DONE : NOTIFY_OK;
1845 } else if (action == BUS_NOTIFY_REMOVED_DEVICE) {
1846 iommu_release_device(dev);
1854 * Combine the driver's chosen def_domain_type across all the devices in a
1855 * group. Drivers must give a consistent result.
1857 static int iommu_get_def_domain_type(struct iommu_group *group,
1858 struct device *dev, int cur_type)
1860 const struct iommu_ops *ops = dev_iommu_ops(dev);
1863 if (!ops->def_domain_type)
1866 type = ops->def_domain_type(dev);
1867 if (!type || cur_type == type)
1872 dev_err_ratelimited(
1874 "IOMMU driver error, requesting conflicting def_domain_type, %s and %s, for devices in group %u.\n",
1875 iommu_domain_type_str(cur_type), iommu_domain_type_str(type),
1879 * Try to recover, drivers are allowed to force IDENITY or DMA, IDENTITY
1882 if (type == IOMMU_DOMAIN_IDENTITY)
1888 * A target_type of 0 will select the best domain type. 0 can be returned in
1889 * this case meaning the global default should be used.
1891 static int iommu_get_default_domain_type(struct iommu_group *group,
1894 struct device *untrusted = NULL;
1895 struct group_device *gdev;
1896 int driver_type = 0;
1898 lockdep_assert_held(&group->mutex);
1901 * ARM32 drivers supporting CONFIG_ARM_DMA_USE_IOMMU can declare an
1902 * identity_domain and it will automatically become their default
1903 * domain. Later on ARM_DMA_USE_IOMMU will install its UNMANAGED domain.
1904 * Override the selection to IDENTITY.
1906 if (IS_ENABLED(CONFIG_ARM_DMA_USE_IOMMU)) {
1907 static_assert(!(IS_ENABLED(CONFIG_ARM_DMA_USE_IOMMU) &&
1908 IS_ENABLED(CONFIG_IOMMU_DMA)));
1909 driver_type = IOMMU_DOMAIN_IDENTITY;
1912 for_each_group_device(group, gdev) {
1913 driver_type = iommu_get_def_domain_type(group, gdev->dev,
1916 if (dev_is_pci(gdev->dev) && to_pci_dev(gdev->dev)->untrusted) {
1918 * No ARM32 using systems will set untrusted, it cannot
1921 if (WARN_ON(IS_ENABLED(CONFIG_ARM_DMA_USE_IOMMU)))
1923 untrusted = gdev->dev;
1928 * If the common dma ops are not selected in kconfig then we cannot use
1929 * IOMMU_DOMAIN_DMA at all. Force IDENTITY if nothing else has been
1932 if (!IS_ENABLED(CONFIG_IOMMU_DMA)) {
1933 if (WARN_ON(driver_type == IOMMU_DOMAIN_DMA))
1936 driver_type = IOMMU_DOMAIN_IDENTITY;
1940 if (driver_type && driver_type != IOMMU_DOMAIN_DMA) {
1941 dev_err_ratelimited(
1943 "Device is not trusted, but driver is overriding group %u to %s, refusing to probe.\n",
1944 group->id, iommu_domain_type_str(driver_type));
1947 driver_type = IOMMU_DOMAIN_DMA;
1951 if (driver_type && target_type != driver_type)
1958 static void iommu_group_do_probe_finalize(struct device *dev)
1960 const struct iommu_ops *ops = dev_iommu_ops(dev);
1962 if (ops->probe_finalize)
1963 ops->probe_finalize(dev);
1966 int bus_iommu_probe(const struct bus_type *bus)
1968 struct iommu_group *group, *next;
1969 LIST_HEAD(group_list);
1972 ret = bus_for_each_dev(bus, NULL, &group_list, probe_iommu_group);
1976 list_for_each_entry_safe(group, next, &group_list, entry) {
1977 struct group_device *gdev;
1979 mutex_lock(&group->mutex);
1981 /* Remove item from the list */
1982 list_del_init(&group->entry);
1985 * We go to the trouble of deferred default domain creation so
1986 * that the cross-group default domain type and the setup of the
1987 * IOMMU_RESV_DIRECT will work correctly in non-hotpug scenarios.
1989 ret = iommu_setup_default_domain(group, 0);
1991 mutex_unlock(&group->mutex);
1994 mutex_unlock(&group->mutex);
1997 * FIXME: Mis-locked because the ops->probe_finalize() call-back
1998 * of some IOMMU drivers calls arm_iommu_attach_device() which
1999 * in-turn might call back into IOMMU core code, where it tries
2000 * to take group->mutex, resulting in a deadlock.
2002 for_each_group_device(group, gdev)
2003 iommu_group_do_probe_finalize(gdev->dev);
2010 * iommu_present() - make platform-specific assumptions about an IOMMU
2011 * @bus: bus to check
2013 * Do not use this function. You want device_iommu_mapped() instead.
2015 * Return: true if some IOMMU is present and aware of devices on the given bus;
2016 * in general it may not be the only IOMMU, and it may not have anything to do
2017 * with whatever device you are ultimately interested in.
2019 bool iommu_present(const struct bus_type *bus)
2023 for (int i = 0; i < ARRAY_SIZE(iommu_buses); i++) {
2024 if (iommu_buses[i] == bus) {
2025 spin_lock(&iommu_device_lock);
2026 ret = !list_empty(&iommu_device_list);
2027 spin_unlock(&iommu_device_lock);
2032 EXPORT_SYMBOL_GPL(iommu_present);
2035 * device_iommu_capable() - check for a general IOMMU capability
2036 * @dev: device to which the capability would be relevant, if available
2037 * @cap: IOMMU capability
2039 * Return: true if an IOMMU is present and supports the given capability
2040 * for the given device, otherwise false.
2042 bool device_iommu_capable(struct device *dev, enum iommu_cap cap)
2044 const struct iommu_ops *ops;
2046 if (!dev_has_iommu(dev))
2049 ops = dev_iommu_ops(dev);
2053 return ops->capable(dev, cap);
2055 EXPORT_SYMBOL_GPL(device_iommu_capable);
2058 * iommu_group_has_isolated_msi() - Compute msi_device_has_isolated_msi()
2060 * @group: Group to query
2062 * IOMMU groups should not have differing values of
2063 * msi_device_has_isolated_msi() for devices in a group. However nothing
2064 * directly prevents this, so ensure mistakes don't result in isolation failures
2065 * by checking that all the devices are the same.
2067 bool iommu_group_has_isolated_msi(struct iommu_group *group)
2069 struct group_device *group_dev;
2072 mutex_lock(&group->mutex);
2073 for_each_group_device(group, group_dev)
2074 ret &= msi_device_has_isolated_msi(group_dev->dev);
2075 mutex_unlock(&group->mutex);
2078 EXPORT_SYMBOL_GPL(iommu_group_has_isolated_msi);
2081 * iommu_set_fault_handler() - set a fault handler for an iommu domain
2082 * @domain: iommu domain
2083 * @handler: fault handler
2084 * @token: user data, will be passed back to the fault handler
2086 * This function should be used by IOMMU users which want to be notified
2087 * whenever an IOMMU fault happens.
2089 * The fault handler itself should return 0 on success, and an appropriate
2090 * error code otherwise.
2092 void iommu_set_fault_handler(struct iommu_domain *domain,
2093 iommu_fault_handler_t handler,
2098 domain->handler = handler;
2099 domain->handler_token = token;
2101 EXPORT_SYMBOL_GPL(iommu_set_fault_handler);
2103 static struct iommu_domain *__iommu_domain_alloc(const struct iommu_ops *ops,
2107 struct iommu_domain *domain;
2108 unsigned int alloc_type = type & IOMMU_DOMAIN_ALLOC_FLAGS;
2110 if (alloc_type == IOMMU_DOMAIN_IDENTITY && ops->identity_domain)
2111 return ops->identity_domain;
2112 else if (alloc_type == IOMMU_DOMAIN_BLOCKED && ops->blocked_domain)
2113 return ops->blocked_domain;
2114 else if (type & __IOMMU_DOMAIN_PAGING && ops->domain_alloc_paging)
2115 domain = ops->domain_alloc_paging(dev);
2116 else if (ops->domain_alloc)
2117 domain = ops->domain_alloc(alloc_type);
2124 domain->type = type;
2125 domain->owner = ops;
2127 * If not already set, assume all sizes by default; the driver
2128 * may override this later
2130 if (!domain->pgsize_bitmap)
2131 domain->pgsize_bitmap = ops->pgsize_bitmap;
2134 domain->ops = ops->default_domain_ops;
2136 if (iommu_is_dma_domain(domain) && iommu_get_dma_cookie(domain)) {
2137 iommu_domain_free(domain);
2143 static struct iommu_domain *
2144 __iommu_group_domain_alloc(struct iommu_group *group, unsigned int type)
2146 struct device *dev = iommu_group_first_dev(group);
2148 return __iommu_domain_alloc(dev_iommu_ops(dev), dev, type);
2151 static int __iommu_domain_alloc_dev(struct device *dev, void *data)
2153 const struct iommu_ops **ops = data;
2155 if (!dev_has_iommu(dev))
2158 if (WARN_ONCE(*ops && *ops != dev_iommu_ops(dev),
2159 "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",
2163 *ops = dev_iommu_ops(dev);
2167 struct iommu_domain *iommu_domain_alloc(const struct bus_type *bus)
2169 const struct iommu_ops *ops = NULL;
2170 int err = bus_for_each_dev(bus, NULL, &ops, __iommu_domain_alloc_dev);
2175 return __iommu_domain_alloc(ops, NULL, IOMMU_DOMAIN_UNMANAGED);
2177 EXPORT_SYMBOL_GPL(iommu_domain_alloc);
2179 void iommu_domain_free(struct iommu_domain *domain)
2181 if (domain->type == IOMMU_DOMAIN_SVA)
2183 iommu_put_dma_cookie(domain);
2184 if (domain->ops->free)
2185 domain->ops->free(domain);
2187 EXPORT_SYMBOL_GPL(iommu_domain_free);
2190 * Put the group's domain back to the appropriate core-owned domain - either the
2191 * standard kernel-mode DMA configuration or an all-DMA-blocked domain.
2193 static void __iommu_group_set_core_domain(struct iommu_group *group)
2195 struct iommu_domain *new_domain;
2198 new_domain = group->blocking_domain;
2200 new_domain = group->default_domain;
2202 __iommu_group_set_domain_nofail(group, new_domain);
2205 static int __iommu_attach_device(struct iommu_domain *domain,
2210 if (unlikely(domain->ops->attach_dev == NULL))
2213 ret = domain->ops->attach_dev(domain, dev);
2216 dev->iommu->attach_deferred = 0;
2217 trace_attach_device_to_domain(dev);
2222 * iommu_attach_device - Attach an IOMMU domain to a device
2223 * @domain: IOMMU domain to attach
2224 * @dev: Device that will be attached
2226 * Returns 0 on success and error code on failure
2228 * Note that EINVAL can be treated as a soft failure, indicating
2229 * that certain configuration of the domain is incompatible with
2230 * the device. In this case attaching a different domain to the
2231 * device may succeed.
2233 int iommu_attach_device(struct iommu_domain *domain, struct device *dev)
2235 /* Caller must be a probed driver on dev */
2236 struct iommu_group *group = dev->iommu_group;
2243 * Lock the group to make sure the device-count doesn't
2244 * change while we are attaching
2246 mutex_lock(&group->mutex);
2248 if (list_count_nodes(&group->devices) != 1)
2251 ret = __iommu_attach_group(domain, group);
2254 mutex_unlock(&group->mutex);
2257 EXPORT_SYMBOL_GPL(iommu_attach_device);
2259 int iommu_deferred_attach(struct device *dev, struct iommu_domain *domain)
2261 if (dev->iommu && dev->iommu->attach_deferred)
2262 return __iommu_attach_device(domain, dev);
2267 void iommu_detach_device(struct iommu_domain *domain, struct device *dev)
2269 /* Caller must be a probed driver on dev */
2270 struct iommu_group *group = dev->iommu_group;
2275 mutex_lock(&group->mutex);
2276 if (WARN_ON(domain != group->domain) ||
2277 WARN_ON(list_count_nodes(&group->devices) != 1))
2279 __iommu_group_set_core_domain(group);
2282 mutex_unlock(&group->mutex);
2284 EXPORT_SYMBOL_GPL(iommu_detach_device);
2286 struct iommu_domain *iommu_get_domain_for_dev(struct device *dev)
2288 /* Caller must be a probed driver on dev */
2289 struct iommu_group *group = dev->iommu_group;
2294 return group->domain;
2296 EXPORT_SYMBOL_GPL(iommu_get_domain_for_dev);
2299 * For IOMMU_DOMAIN_DMA implementations which already provide their own
2300 * guarantees that the group and its default domain are valid and correct.
2302 struct iommu_domain *iommu_get_dma_domain(struct device *dev)
2304 return dev->iommu_group->default_domain;
2307 static int __iommu_attach_group(struct iommu_domain *domain,
2308 struct iommu_group *group)
2312 if (group->domain && group->domain != group->default_domain &&
2313 group->domain != group->blocking_domain)
2316 dev = iommu_group_first_dev(group);
2317 if (!dev_has_iommu(dev) || dev_iommu_ops(dev) != domain->owner)
2320 return __iommu_group_set_domain(group, domain);
2324 * iommu_attach_group - Attach an IOMMU domain to an IOMMU group
2325 * @domain: IOMMU domain to attach
2326 * @group: IOMMU group that will be attached
2328 * Returns 0 on success and error code on failure
2330 * Note that EINVAL can be treated as a soft failure, indicating
2331 * that certain configuration of the domain is incompatible with
2332 * the group. In this case attaching a different domain to the
2333 * group may succeed.
2335 int iommu_attach_group(struct iommu_domain *domain, struct iommu_group *group)
2339 mutex_lock(&group->mutex);
2340 ret = __iommu_attach_group(domain, group);
2341 mutex_unlock(&group->mutex);
2345 EXPORT_SYMBOL_GPL(iommu_attach_group);
2348 * iommu_group_replace_domain - replace the domain that a group is attached to
2349 * @new_domain: new IOMMU domain to replace with
2350 * @group: IOMMU group that will be attached to the new domain
2352 * This API allows the group to switch domains without being forced to go to
2353 * the blocking domain in-between.
2355 * If the currently attached domain is a core domain (e.g. a default_domain),
2356 * it will act just like the iommu_attach_group().
2358 int iommu_group_replace_domain(struct iommu_group *group,
2359 struct iommu_domain *new_domain)
2366 mutex_lock(&group->mutex);
2367 ret = __iommu_group_set_domain(group, new_domain);
2368 mutex_unlock(&group->mutex);
2371 EXPORT_SYMBOL_NS_GPL(iommu_group_replace_domain, IOMMUFD_INTERNAL);
2373 static int __iommu_device_set_domain(struct iommu_group *group,
2375 struct iommu_domain *new_domain,
2381 * If the device requires IOMMU_RESV_DIRECT then we cannot allow
2382 * the blocking domain to be attached as it does not contain the
2383 * required 1:1 mapping. This test effectively excludes the device
2384 * being used with iommu_group_claim_dma_owner() which will block
2385 * vfio and iommufd as well.
2387 if (dev->iommu->require_direct &&
2388 (new_domain->type == IOMMU_DOMAIN_BLOCKED ||
2389 new_domain == group->blocking_domain)) {
2391 "Firmware has requested this device have a 1:1 IOMMU mapping, rejecting configuring the device without a 1:1 mapping. Contact your platform vendor.\n");
2395 if (dev->iommu->attach_deferred) {
2396 if (new_domain == group->default_domain)
2398 dev->iommu->attach_deferred = 0;
2401 ret = __iommu_attach_device(new_domain, dev);
2404 * If we have a blocking domain then try to attach that in hopes
2405 * of avoiding a UAF. Modern drivers should implement blocking
2406 * domains as global statics that cannot fail.
2408 if ((flags & IOMMU_SET_DOMAIN_MUST_SUCCEED) &&
2409 group->blocking_domain &&
2410 group->blocking_domain != new_domain)
2411 __iommu_attach_device(group->blocking_domain, dev);
2418 * If 0 is returned the group's domain is new_domain. If an error is returned
2419 * then the group's domain will be set back to the existing domain unless
2420 * IOMMU_SET_DOMAIN_MUST_SUCCEED, otherwise an error is returned and the group's
2421 * domains is left inconsistent. This is a driver bug to fail attach with a
2422 * previously good domain. We try to avoid a kernel UAF because of this.
2424 * IOMMU groups are really the natural working unit of the IOMMU, but the IOMMU
2425 * API works on domains and devices. Bridge that gap by iterating over the
2426 * devices in a group. Ideally we'd have a single device which represents the
2427 * requestor ID of the group, but we also allow IOMMU drivers to create policy
2428 * defined minimum sets, where the physical hardware may be able to distiguish
2429 * members, but we wish to group them at a higher level (ex. untrusted
2430 * multi-function PCI devices). Thus we attach each device.
2432 static int __iommu_group_set_domain_internal(struct iommu_group *group,
2433 struct iommu_domain *new_domain,
2436 struct group_device *last_gdev;
2437 struct group_device *gdev;
2441 lockdep_assert_held(&group->mutex);
2443 if (group->domain == new_domain)
2446 if (WARN_ON(!new_domain))
2450 * Changing the domain is done by calling attach_dev() on the new
2451 * domain. This switch does not have to be atomic and DMA can be
2452 * discarded during the transition. DMA must only be able to access
2453 * either new_domain or group->domain, never something else.
2456 for_each_group_device(group, gdev) {
2457 ret = __iommu_device_set_domain(group, gdev->dev, new_domain,
2462 * Keep trying the other devices in the group. If a
2463 * driver fails attach to an otherwise good domain, and
2464 * does not support blocking domains, it should at least
2465 * drop its reference on the current domain so we don't
2468 if (flags & IOMMU_SET_DOMAIN_MUST_SUCCEED)
2473 group->domain = new_domain;
2478 * This is called in error unwind paths. A well behaved driver should
2479 * always allow us to attach to a domain that was already attached.
2482 for_each_group_device(group, gdev) {
2484 * A NULL domain can happen only for first probe, in which case
2485 * we leave group->domain as NULL and let release clean
2489 WARN_ON(__iommu_device_set_domain(
2490 group, gdev->dev, group->domain,
2491 IOMMU_SET_DOMAIN_MUST_SUCCEED));
2492 if (gdev == last_gdev)
2498 void iommu_detach_group(struct iommu_domain *domain, struct iommu_group *group)
2500 mutex_lock(&group->mutex);
2501 __iommu_group_set_core_domain(group);
2502 mutex_unlock(&group->mutex);
2504 EXPORT_SYMBOL_GPL(iommu_detach_group);
2506 phys_addr_t iommu_iova_to_phys(struct iommu_domain *domain, dma_addr_t iova)
2508 if (domain->type == IOMMU_DOMAIN_IDENTITY)
2511 if (domain->type == IOMMU_DOMAIN_BLOCKED)
2514 return domain->ops->iova_to_phys(domain, iova);
2516 EXPORT_SYMBOL_GPL(iommu_iova_to_phys);
2518 static size_t iommu_pgsize(struct iommu_domain *domain, unsigned long iova,
2519 phys_addr_t paddr, size_t size, size_t *count)
2521 unsigned int pgsize_idx, pgsize_idx_next;
2522 unsigned long pgsizes;
2523 size_t offset, pgsize, pgsize_next;
2524 unsigned long addr_merge = paddr | iova;
2526 /* Page sizes supported by the hardware and small enough for @size */
2527 pgsizes = domain->pgsize_bitmap & GENMASK(__fls(size), 0);
2529 /* Constrain the page sizes further based on the maximum alignment */
2530 if (likely(addr_merge))
2531 pgsizes &= GENMASK(__ffs(addr_merge), 0);
2533 /* Make sure we have at least one suitable page size */
2536 /* Pick the biggest page size remaining */
2537 pgsize_idx = __fls(pgsizes);
2538 pgsize = BIT(pgsize_idx);
2542 /* Find the next biggest support page size, if it exists */
2543 pgsizes = domain->pgsize_bitmap & ~GENMASK(pgsize_idx, 0);
2547 pgsize_idx_next = __ffs(pgsizes);
2548 pgsize_next = BIT(pgsize_idx_next);
2551 * There's no point trying a bigger page size unless the virtual
2552 * and physical addresses are similarly offset within the larger page.
2554 if ((iova ^ paddr) & (pgsize_next - 1))
2557 /* Calculate the offset to the next page size alignment boundary */
2558 offset = pgsize_next - (addr_merge & (pgsize_next - 1));
2561 * If size is big enough to accommodate the larger page, reduce
2562 * the number of smaller pages.
2564 if (offset + pgsize_next <= size)
2568 *count = size >> pgsize_idx;
2572 static int __iommu_map(struct iommu_domain *domain, unsigned long iova,
2573 phys_addr_t paddr, size_t size, int prot, gfp_t gfp)
2575 const struct iommu_domain_ops *ops = domain->ops;
2576 unsigned long orig_iova = iova;
2577 unsigned int min_pagesz;
2578 size_t orig_size = size;
2579 phys_addr_t orig_paddr = paddr;
2582 if (unlikely(!(domain->type & __IOMMU_DOMAIN_PAGING)))
2585 if (WARN_ON(!ops->map_pages || domain->pgsize_bitmap == 0UL))
2588 /* find out the minimum page size supported */
2589 min_pagesz = 1 << __ffs(domain->pgsize_bitmap);
2592 * both the virtual address and the physical one, as well as
2593 * the size of the mapping, must be aligned (at least) to the
2594 * size of the smallest page supported by the hardware
2596 if (!IS_ALIGNED(iova | paddr | size, min_pagesz)) {
2597 pr_err("unaligned: iova 0x%lx pa %pa size 0x%zx min_pagesz 0x%x\n",
2598 iova, &paddr, size, min_pagesz);
2602 pr_debug("map: iova 0x%lx pa %pa size 0x%zx\n", iova, &paddr, size);
2605 size_t pgsize, count, mapped = 0;
2607 pgsize = iommu_pgsize(domain, iova, paddr, size, &count);
2609 pr_debug("mapping: iova 0x%lx pa %pa pgsize 0x%zx count %zu\n",
2610 iova, &paddr, pgsize, count);
2611 ret = ops->map_pages(domain, iova, paddr, pgsize, count, prot,
2614 * Some pages may have been mapped, even if an error occurred,
2615 * so we should account for those so they can be unmapped.
2626 /* unroll mapping in case something went wrong */
2628 iommu_unmap(domain, orig_iova, orig_size - size);
2630 trace_map(orig_iova, orig_paddr, orig_size);
2635 int iommu_map(struct iommu_domain *domain, unsigned long iova,
2636 phys_addr_t paddr, size_t size, int prot, gfp_t gfp)
2638 const struct iommu_domain_ops *ops = domain->ops;
2641 might_sleep_if(gfpflags_allow_blocking(gfp));
2643 /* Discourage passing strange GFP flags */
2644 if (WARN_ON_ONCE(gfp & (__GFP_COMP | __GFP_DMA | __GFP_DMA32 |
2648 ret = __iommu_map(domain, iova, paddr, size, prot, gfp);
2649 if (ret == 0 && ops->iotlb_sync_map) {
2650 ret = ops->iotlb_sync_map(domain, iova, size);
2658 /* undo mappings already done */
2659 iommu_unmap(domain, iova, size);
2663 EXPORT_SYMBOL_GPL(iommu_map);
2665 static size_t __iommu_unmap(struct iommu_domain *domain,
2666 unsigned long iova, size_t size,
2667 struct iommu_iotlb_gather *iotlb_gather)
2669 const struct iommu_domain_ops *ops = domain->ops;
2670 size_t unmapped_page, unmapped = 0;
2671 unsigned long orig_iova = iova;
2672 unsigned int min_pagesz;
2674 if (unlikely(!(domain->type & __IOMMU_DOMAIN_PAGING)))
2677 if (WARN_ON(!ops->unmap_pages || domain->pgsize_bitmap == 0UL))
2680 /* find out the minimum page size supported */
2681 min_pagesz = 1 << __ffs(domain->pgsize_bitmap);
2684 * The virtual address, as well as the size of the mapping, must be
2685 * aligned (at least) to the size of the smallest page supported
2688 if (!IS_ALIGNED(iova | size, min_pagesz)) {
2689 pr_err("unaligned: iova 0x%lx size 0x%zx min_pagesz 0x%x\n",
2690 iova, size, min_pagesz);
2694 pr_debug("unmap this: iova 0x%lx size 0x%zx\n", iova, size);
2697 * Keep iterating until we either unmap 'size' bytes (or more)
2698 * or we hit an area that isn't mapped.
2700 while (unmapped < size) {
2701 size_t pgsize, count;
2703 pgsize = iommu_pgsize(domain, iova, iova, size - unmapped, &count);
2704 unmapped_page = ops->unmap_pages(domain, iova, pgsize, count, iotlb_gather);
2708 pr_debug("unmapped: iova 0x%lx size 0x%zx\n",
2709 iova, unmapped_page);
2711 iova += unmapped_page;
2712 unmapped += unmapped_page;
2715 trace_unmap(orig_iova, size, unmapped);
2719 size_t iommu_unmap(struct iommu_domain *domain,
2720 unsigned long iova, size_t size)
2722 struct iommu_iotlb_gather iotlb_gather;
2725 iommu_iotlb_gather_init(&iotlb_gather);
2726 ret = __iommu_unmap(domain, iova, size, &iotlb_gather);
2727 iommu_iotlb_sync(domain, &iotlb_gather);
2731 EXPORT_SYMBOL_GPL(iommu_unmap);
2733 size_t iommu_unmap_fast(struct iommu_domain *domain,
2734 unsigned long iova, size_t size,
2735 struct iommu_iotlb_gather *iotlb_gather)
2737 return __iommu_unmap(domain, iova, size, iotlb_gather);
2739 EXPORT_SYMBOL_GPL(iommu_unmap_fast);
2741 ssize_t iommu_map_sg(struct iommu_domain *domain, unsigned long iova,
2742 struct scatterlist *sg, unsigned int nents, int prot,
2745 const struct iommu_domain_ops *ops = domain->ops;
2746 size_t len = 0, mapped = 0;
2751 might_sleep_if(gfpflags_allow_blocking(gfp));
2753 /* Discourage passing strange GFP flags */
2754 if (WARN_ON_ONCE(gfp & (__GFP_COMP | __GFP_DMA | __GFP_DMA32 |
2758 while (i <= nents) {
2759 phys_addr_t s_phys = sg_phys(sg);
2761 if (len && s_phys != start + len) {
2762 ret = __iommu_map(domain, iova + mapped, start,
2772 if (sg_dma_is_bus_address(sg))
2787 if (ops->iotlb_sync_map) {
2788 ret = ops->iotlb_sync_map(domain, iova, mapped);
2795 /* undo mappings already done */
2796 iommu_unmap(domain, iova, mapped);
2800 EXPORT_SYMBOL_GPL(iommu_map_sg);
2803 * report_iommu_fault() - report about an IOMMU fault to the IOMMU framework
2804 * @domain: the iommu domain where the fault has happened
2805 * @dev: the device where the fault has happened
2806 * @iova: the faulting address
2807 * @flags: mmu fault flags (e.g. IOMMU_FAULT_READ/IOMMU_FAULT_WRITE/...)
2809 * This function should be called by the low-level IOMMU implementations
2810 * whenever IOMMU faults happen, to allow high-level users, that are
2811 * interested in such events, to know about them.
2813 * This event may be useful for several possible use cases:
2814 * - mere logging of the event
2815 * - dynamic TLB/PTE loading
2816 * - if restarting of the faulting device is required
2818 * Returns 0 on success and an appropriate error code otherwise (if dynamic
2819 * PTE/TLB loading will one day be supported, implementations will be able
2820 * to tell whether it succeeded or not according to this return value).
2822 * Specifically, -ENOSYS is returned if a fault handler isn't installed
2823 * (though fault handlers can also return -ENOSYS, in case they want to
2824 * elicit the default behavior of the IOMMU drivers).
2826 int report_iommu_fault(struct iommu_domain *domain, struct device *dev,
2827 unsigned long iova, int flags)
2832 * if upper layers showed interest and installed a fault handler,
2835 if (domain->handler)
2836 ret = domain->handler(domain, dev, iova, flags,
2837 domain->handler_token);
2839 trace_io_page_fault(dev, iova, flags);
2842 EXPORT_SYMBOL_GPL(report_iommu_fault);
2844 static int __init iommu_init(void)
2846 iommu_group_kset = kset_create_and_add("iommu_groups",
2848 BUG_ON(!iommu_group_kset);
2850 iommu_debugfs_setup();
2854 core_initcall(iommu_init);
2856 int iommu_enable_nesting(struct iommu_domain *domain)
2858 if (domain->type != IOMMU_DOMAIN_UNMANAGED)
2860 if (!domain->ops->enable_nesting)
2862 return domain->ops->enable_nesting(domain);
2864 EXPORT_SYMBOL_GPL(iommu_enable_nesting);
2866 int iommu_set_pgtable_quirks(struct iommu_domain *domain,
2867 unsigned long quirk)
2869 if (domain->type != IOMMU_DOMAIN_UNMANAGED)
2871 if (!domain->ops->set_pgtable_quirks)
2873 return domain->ops->set_pgtable_quirks(domain, quirk);
2875 EXPORT_SYMBOL_GPL(iommu_set_pgtable_quirks);
2878 * iommu_get_resv_regions - get reserved regions
2879 * @dev: device for which to get reserved regions
2880 * @list: reserved region list for device
2882 * This returns a list of reserved IOVA regions specific to this device.
2883 * A domain user should not map IOVA in these ranges.
2885 void iommu_get_resv_regions(struct device *dev, struct list_head *list)
2887 const struct iommu_ops *ops = dev_iommu_ops(dev);
2889 if (ops->get_resv_regions)
2890 ops->get_resv_regions(dev, list);
2892 EXPORT_SYMBOL_GPL(iommu_get_resv_regions);
2895 * iommu_put_resv_regions - release reserved regions
2896 * @dev: device for which to free reserved regions
2897 * @list: reserved region list for device
2899 * This releases a reserved region list acquired by iommu_get_resv_regions().
2901 void iommu_put_resv_regions(struct device *dev, struct list_head *list)
2903 struct iommu_resv_region *entry, *next;
2905 list_for_each_entry_safe(entry, next, list, list) {
2907 entry->free(dev, entry);
2912 EXPORT_SYMBOL(iommu_put_resv_regions);
2914 struct iommu_resv_region *iommu_alloc_resv_region(phys_addr_t start,
2915 size_t length, int prot,
2916 enum iommu_resv_type type,
2919 struct iommu_resv_region *region;
2921 region = kzalloc(sizeof(*region), gfp);
2925 INIT_LIST_HEAD(®ion->list);
2926 region->start = start;
2927 region->length = length;
2928 region->prot = prot;
2929 region->type = type;
2932 EXPORT_SYMBOL_GPL(iommu_alloc_resv_region);
2934 void iommu_set_default_passthrough(bool cmd_line)
2937 iommu_cmd_line |= IOMMU_CMD_LINE_DMA_API;
2938 iommu_def_domain_type = IOMMU_DOMAIN_IDENTITY;
2941 void iommu_set_default_translated(bool cmd_line)
2944 iommu_cmd_line |= IOMMU_CMD_LINE_DMA_API;
2945 iommu_def_domain_type = IOMMU_DOMAIN_DMA;
2948 bool iommu_default_passthrough(void)
2950 return iommu_def_domain_type == IOMMU_DOMAIN_IDENTITY;
2952 EXPORT_SYMBOL_GPL(iommu_default_passthrough);
2954 const struct iommu_ops *iommu_ops_from_fwnode(struct fwnode_handle *fwnode)
2956 const struct iommu_ops *ops = NULL;
2957 struct iommu_device *iommu;
2959 spin_lock(&iommu_device_lock);
2960 list_for_each_entry(iommu, &iommu_device_list, list)
2961 if (iommu->fwnode == fwnode) {
2965 spin_unlock(&iommu_device_lock);
2969 int iommu_fwspec_init(struct device *dev, struct fwnode_handle *iommu_fwnode,
2970 const struct iommu_ops *ops)
2972 struct iommu_fwspec *fwspec = dev_iommu_fwspec_get(dev);
2975 return ops == fwspec->ops ? 0 : -EINVAL;
2977 if (!dev_iommu_get(dev))
2980 /* Preallocate for the overwhelmingly common case of 1 ID */
2981 fwspec = kzalloc(struct_size(fwspec, ids, 1), GFP_KERNEL);
2985 of_node_get(to_of_node(iommu_fwnode));
2986 fwspec->iommu_fwnode = iommu_fwnode;
2988 dev_iommu_fwspec_set(dev, fwspec);
2991 EXPORT_SYMBOL_GPL(iommu_fwspec_init);
2993 void iommu_fwspec_free(struct device *dev)
2995 struct iommu_fwspec *fwspec = dev_iommu_fwspec_get(dev);
2998 fwnode_handle_put(fwspec->iommu_fwnode);
3000 dev_iommu_fwspec_set(dev, NULL);
3003 EXPORT_SYMBOL_GPL(iommu_fwspec_free);
3005 int iommu_fwspec_add_ids(struct device *dev, u32 *ids, int num_ids)
3007 struct iommu_fwspec *fwspec = dev_iommu_fwspec_get(dev);
3013 new_num = fwspec->num_ids + num_ids;
3015 fwspec = krealloc(fwspec, struct_size(fwspec, ids, new_num),
3020 dev_iommu_fwspec_set(dev, fwspec);
3023 for (i = 0; i < num_ids; i++)
3024 fwspec->ids[fwspec->num_ids + i] = ids[i];
3026 fwspec->num_ids = new_num;
3029 EXPORT_SYMBOL_GPL(iommu_fwspec_add_ids);
3032 * Per device IOMMU features.
3034 int iommu_dev_enable_feature(struct device *dev, enum iommu_dev_features feat)
3036 if (dev_has_iommu(dev)) {
3037 const struct iommu_ops *ops = dev_iommu_ops(dev);
3039 if (ops->dev_enable_feat)
3040 return ops->dev_enable_feat(dev, feat);
3045 EXPORT_SYMBOL_GPL(iommu_dev_enable_feature);
3048 * The device drivers should do the necessary cleanups before calling this.
3050 int iommu_dev_disable_feature(struct device *dev, enum iommu_dev_features feat)
3052 if (dev_has_iommu(dev)) {
3053 const struct iommu_ops *ops = dev_iommu_ops(dev);
3055 if (ops->dev_disable_feat)
3056 return ops->dev_disable_feat(dev, feat);
3061 EXPORT_SYMBOL_GPL(iommu_dev_disable_feature);
3064 * iommu_setup_default_domain - Set the default_domain for the group
3065 * @group: Group to change
3066 * @target_type: Domain type to set as the default_domain
3068 * Allocate a default domain and set it as the current domain on the group. If
3069 * the group already has a default domain it will be changed to the target_type.
3070 * When target_type is 0 the default domain is selected based on driver and
3071 * system preferences.
3073 static int iommu_setup_default_domain(struct iommu_group *group,
3076 struct iommu_domain *old_dom = group->default_domain;
3077 struct group_device *gdev;
3078 struct iommu_domain *dom;
3083 lockdep_assert_held(&group->mutex);
3085 req_type = iommu_get_default_domain_type(group, target_type);
3089 dom = iommu_group_alloc_default_domain(group, req_type);
3093 if (group->default_domain == dom)
3097 * IOMMU_RESV_DIRECT and IOMMU_RESV_DIRECT_RELAXABLE regions must be
3098 * mapped before their device is attached, in order to guarantee
3099 * continuity with any FW activity
3101 direct_failed = false;
3102 for_each_group_device(group, gdev) {
3103 if (iommu_create_device_direct_mappings(dom, gdev->dev)) {
3104 direct_failed = true;
3106 gdev->dev->iommu->iommu_dev->dev,
3107 "IOMMU driver was not able to establish FW requested direct mapping.");
3111 /* We must set default_domain early for __iommu_device_set_domain */
3112 group->default_domain = dom;
3113 if (!group->domain) {
3115 * Drivers are not allowed to fail the first domain attach.
3116 * The only way to recover from this is to fail attaching the
3117 * iommu driver and call ops->release_device. Put the domain
3118 * in group->default_domain so it is freed after.
3120 ret = __iommu_group_set_domain_internal(
3121 group, dom, IOMMU_SET_DOMAIN_MUST_SUCCEED);
3125 ret = __iommu_group_set_domain(group, dom);
3127 goto err_restore_def_domain;
3131 * Drivers are supposed to allow mappings to be installed in a domain
3132 * before device attachment, but some don't. Hack around this defect by
3133 * trying again after attaching. If this happens it means the device
3134 * will not continuously have the IOMMU_RESV_DIRECT map.
3136 if (direct_failed) {
3137 for_each_group_device(group, gdev) {
3138 ret = iommu_create_device_direct_mappings(dom, gdev->dev);
3140 goto err_restore_domain;
3146 iommu_domain_free(old_dom);
3151 __iommu_group_set_domain_internal(
3152 group, old_dom, IOMMU_SET_DOMAIN_MUST_SUCCEED);
3153 err_restore_def_domain:
3155 iommu_domain_free(dom);
3156 group->default_domain = old_dom;
3162 * Changing the default domain through sysfs requires the users to unbind the
3163 * drivers from the devices in the iommu group, except for a DMA -> DMA-FQ
3164 * transition. Return failure if this isn't met.
3166 * We need to consider the race between this and the device release path.
3167 * group->mutex is used here to guarantee that the device release path
3168 * will not be entered at the same time.
3170 static ssize_t iommu_group_store_type(struct iommu_group *group,
3171 const char *buf, size_t count)
3173 struct group_device *gdev;
3176 if (!capable(CAP_SYS_ADMIN) || !capable(CAP_SYS_RAWIO))
3179 if (WARN_ON(!group) || !group->default_domain)
3182 if (sysfs_streq(buf, "identity"))
3183 req_type = IOMMU_DOMAIN_IDENTITY;
3184 else if (sysfs_streq(buf, "DMA"))
3185 req_type = IOMMU_DOMAIN_DMA;
3186 else if (sysfs_streq(buf, "DMA-FQ"))
3187 req_type = IOMMU_DOMAIN_DMA_FQ;
3188 else if (sysfs_streq(buf, "auto"))
3193 mutex_lock(&group->mutex);
3194 /* We can bring up a flush queue without tearing down the domain. */
3195 if (req_type == IOMMU_DOMAIN_DMA_FQ &&
3196 group->default_domain->type == IOMMU_DOMAIN_DMA) {
3197 ret = iommu_dma_init_fq(group->default_domain);
3201 group->default_domain->type = IOMMU_DOMAIN_DMA_FQ;
3206 /* Otherwise, ensure that device exists and no driver is bound. */
3207 if (list_empty(&group->devices) || group->owner_cnt) {
3212 ret = iommu_setup_default_domain(group, req_type);
3217 * Release the mutex here because ops->probe_finalize() call-back of
3218 * some vendor IOMMU drivers calls arm_iommu_attach_device() which
3219 * in-turn might call back into IOMMU core code, where it tries to take
3220 * group->mutex, resulting in a deadlock.
3222 mutex_unlock(&group->mutex);
3224 /* Make sure dma_ops is appropriatley set */
3225 for_each_group_device(group, gdev)
3226 iommu_group_do_probe_finalize(gdev->dev);
3230 mutex_unlock(&group->mutex);
3231 return ret ?: count;
3235 * iommu_device_use_default_domain() - Device driver wants to handle device
3236 * DMA through the kernel DMA API.
3239 * The device driver about to bind @dev wants to do DMA through the kernel
3240 * DMA API. Return 0 if it is allowed, otherwise an error.
3242 int iommu_device_use_default_domain(struct device *dev)
3244 /* Caller is the driver core during the pre-probe path */
3245 struct iommu_group *group = dev->iommu_group;
3251 mutex_lock(&group->mutex);
3252 if (group->owner_cnt) {
3253 if (group->domain != group->default_domain || group->owner ||
3254 !xa_empty(&group->pasid_array)) {
3263 mutex_unlock(&group->mutex);
3268 * iommu_device_unuse_default_domain() - Device driver stops handling device
3269 * DMA through the kernel DMA API.
3272 * The device driver doesn't want to do DMA through kernel DMA API anymore.
3273 * It must be called after iommu_device_use_default_domain().
3275 void iommu_device_unuse_default_domain(struct device *dev)
3277 /* Caller is the driver core during the post-probe path */
3278 struct iommu_group *group = dev->iommu_group;
3283 mutex_lock(&group->mutex);
3284 if (!WARN_ON(!group->owner_cnt || !xa_empty(&group->pasid_array)))
3287 mutex_unlock(&group->mutex);
3290 static int __iommu_group_alloc_blocking_domain(struct iommu_group *group)
3292 if (group->blocking_domain)
3295 group->blocking_domain =
3296 __iommu_group_domain_alloc(group, IOMMU_DOMAIN_BLOCKED);
3297 if (!group->blocking_domain) {
3299 * For drivers that do not yet understand IOMMU_DOMAIN_BLOCKED
3300 * create an empty domain instead.
3302 group->blocking_domain = __iommu_group_domain_alloc(
3303 group, IOMMU_DOMAIN_UNMANAGED);
3304 if (!group->blocking_domain)
3310 static int __iommu_take_dma_ownership(struct iommu_group *group, void *owner)
3314 if ((group->domain && group->domain != group->default_domain) ||
3315 !xa_empty(&group->pasid_array))
3318 ret = __iommu_group_alloc_blocking_domain(group);
3321 ret = __iommu_group_set_domain(group, group->blocking_domain);
3325 group->owner = owner;
3331 * iommu_group_claim_dma_owner() - Set DMA ownership of a group
3332 * @group: The group.
3333 * @owner: Caller specified pointer. Used for exclusive ownership.
3335 * This is to support backward compatibility for vfio which manages the dma
3336 * ownership in iommu_group level. New invocations on this interface should be
3337 * prohibited. Only a single owner may exist for a group.
3339 int iommu_group_claim_dma_owner(struct iommu_group *group, void *owner)
3343 if (WARN_ON(!owner))
3346 mutex_lock(&group->mutex);
3347 if (group->owner_cnt) {
3352 ret = __iommu_take_dma_ownership(group, owner);
3354 mutex_unlock(&group->mutex);
3358 EXPORT_SYMBOL_GPL(iommu_group_claim_dma_owner);
3361 * iommu_device_claim_dma_owner() - Set DMA ownership of a device
3363 * @owner: Caller specified pointer. Used for exclusive ownership.
3365 * Claim the DMA ownership of a device. Multiple devices in the same group may
3366 * concurrently claim ownership if they present the same owner value. Returns 0
3367 * on success and error code on failure
3369 int iommu_device_claim_dma_owner(struct device *dev, void *owner)
3371 /* Caller must be a probed driver on dev */
3372 struct iommu_group *group = dev->iommu_group;
3375 if (WARN_ON(!owner))
3381 mutex_lock(&group->mutex);
3382 if (group->owner_cnt) {
3383 if (group->owner != owner) {
3391 ret = __iommu_take_dma_ownership(group, owner);
3393 mutex_unlock(&group->mutex);
3396 EXPORT_SYMBOL_GPL(iommu_device_claim_dma_owner);
3398 static void __iommu_release_dma_ownership(struct iommu_group *group)
3400 if (WARN_ON(!group->owner_cnt || !group->owner ||
3401 !xa_empty(&group->pasid_array)))
3404 group->owner_cnt = 0;
3405 group->owner = NULL;
3406 __iommu_group_set_domain_nofail(group, group->default_domain);
3410 * iommu_group_release_dma_owner() - Release DMA ownership of a group
3413 * Release the DMA ownership claimed by iommu_group_claim_dma_owner().
3415 void iommu_group_release_dma_owner(struct iommu_group *group)
3417 mutex_lock(&group->mutex);
3418 __iommu_release_dma_ownership(group);
3419 mutex_unlock(&group->mutex);
3421 EXPORT_SYMBOL_GPL(iommu_group_release_dma_owner);
3424 * iommu_device_release_dma_owner() - Release DMA ownership of a device
3427 * Release the DMA ownership claimed by iommu_device_claim_dma_owner().
3429 void iommu_device_release_dma_owner(struct device *dev)
3431 /* Caller must be a probed driver on dev */
3432 struct iommu_group *group = dev->iommu_group;
3434 mutex_lock(&group->mutex);
3435 if (group->owner_cnt > 1)
3438 __iommu_release_dma_ownership(group);
3439 mutex_unlock(&group->mutex);
3441 EXPORT_SYMBOL_GPL(iommu_device_release_dma_owner);
3444 * iommu_group_dma_owner_claimed() - Query group dma ownership status
3445 * @group: The group.
3447 * This provides status query on a given group. It is racy and only for
3448 * non-binding status reporting.
3450 bool iommu_group_dma_owner_claimed(struct iommu_group *group)
3454 mutex_lock(&group->mutex);
3455 user = group->owner_cnt;
3456 mutex_unlock(&group->mutex);
3460 EXPORT_SYMBOL_GPL(iommu_group_dma_owner_claimed);
3462 static int __iommu_set_group_pasid(struct iommu_domain *domain,
3463 struct iommu_group *group, ioasid_t pasid)
3465 struct group_device *device;
3468 for_each_group_device(group, device) {
3469 ret = domain->ops->set_dev_pasid(domain, device->dev, pasid);
3477 static void __iommu_remove_group_pasid(struct iommu_group *group,
3480 struct group_device *device;
3481 const struct iommu_ops *ops;
3483 for_each_group_device(group, device) {
3484 ops = dev_iommu_ops(device->dev);
3485 ops->remove_dev_pasid(device->dev, pasid);
3490 * iommu_attach_device_pasid() - Attach a domain to pasid of device
3491 * @domain: the iommu domain.
3492 * @dev: the attached device.
3493 * @pasid: the pasid of the device.
3495 * Return: 0 on success, or an error.
3497 int iommu_attach_device_pasid(struct iommu_domain *domain,
3498 struct device *dev, ioasid_t pasid)
3500 /* Caller must be a probed driver on dev */
3501 struct iommu_group *group = dev->iommu_group;
3505 if (!domain->ops->set_dev_pasid)
3511 if (!dev_has_iommu(dev) || dev_iommu_ops(dev) != domain->owner)
3514 mutex_lock(&group->mutex);
3515 curr = xa_cmpxchg(&group->pasid_array, pasid, NULL, domain, GFP_KERNEL);
3517 ret = xa_err(curr) ? : -EBUSY;
3521 ret = __iommu_set_group_pasid(domain, group, pasid);
3523 __iommu_remove_group_pasid(group, pasid);
3524 xa_erase(&group->pasid_array, pasid);
3527 mutex_unlock(&group->mutex);
3530 EXPORT_SYMBOL_GPL(iommu_attach_device_pasid);
3533 * iommu_detach_device_pasid() - Detach the domain from pasid of device
3534 * @domain: the iommu domain.
3535 * @dev: the attached device.
3536 * @pasid: the pasid of the device.
3538 * The @domain must have been attached to @pasid of the @dev with
3539 * iommu_attach_device_pasid().
3541 void iommu_detach_device_pasid(struct iommu_domain *domain, struct device *dev,
3544 /* Caller must be a probed driver on dev */
3545 struct iommu_group *group = dev->iommu_group;
3547 mutex_lock(&group->mutex);
3548 __iommu_remove_group_pasid(group, pasid);
3549 WARN_ON(xa_erase(&group->pasid_array, pasid) != domain);
3550 mutex_unlock(&group->mutex);
3552 EXPORT_SYMBOL_GPL(iommu_detach_device_pasid);
3555 * iommu_get_domain_for_dev_pasid() - Retrieve domain for @pasid of @dev
3556 * @dev: the queried device
3557 * @pasid: the pasid of the device
3558 * @type: matched domain type, 0 for any match
3560 * This is a variant of iommu_get_domain_for_dev(). It returns the existing
3561 * domain attached to pasid of a device. Callers must hold a lock around this
3562 * function, and both iommu_attach/detach_dev_pasid() whenever a domain of
3563 * type is being manipulated. This API does not internally resolve races with
3566 * Return: attached domain on success, NULL otherwise.
3568 struct iommu_domain *iommu_get_domain_for_dev_pasid(struct device *dev,
3572 /* Caller must be a probed driver on dev */
3573 struct iommu_group *group = dev->iommu_group;
3574 struct iommu_domain *domain;
3579 xa_lock(&group->pasid_array);
3580 domain = xa_load(&group->pasid_array, pasid);
3581 if (type && domain && domain->type != type)
3582 domain = ERR_PTR(-EBUSY);
3583 xa_unlock(&group->pasid_array);
3587 EXPORT_SYMBOL_GPL(iommu_get_domain_for_dev_pasid);
3589 struct iommu_domain *iommu_sva_domain_alloc(struct device *dev,
3590 struct mm_struct *mm)
3592 const struct iommu_ops *ops = dev_iommu_ops(dev);
3593 struct iommu_domain *domain;
3595 domain = ops->domain_alloc(IOMMU_DOMAIN_SVA);
3599 domain->type = IOMMU_DOMAIN_SVA;
3602 domain->iopf_handler = iommu_sva_handle_iopf;
3603 domain->fault_data = mm;
3608 ioasid_t iommu_alloc_global_pasid(struct device *dev)
3612 /* max_pasids == 0 means that the device does not support PASID */
3613 if (!dev->iommu->max_pasids)
3614 return IOMMU_PASID_INVALID;
3617 * max_pasids is set up by vendor driver based on number of PASID bits
3618 * supported but the IDA allocation is inclusive.
3620 ret = ida_alloc_range(&iommu_global_pasid_ida, IOMMU_FIRST_GLOBAL_PASID,
3621 dev->iommu->max_pasids - 1, GFP_KERNEL);
3622 return ret < 0 ? IOMMU_PASID_INVALID : ret;
3624 EXPORT_SYMBOL_GPL(iommu_alloc_global_pasid);
3626 void iommu_free_global_pasid(ioasid_t pasid)
3628 if (WARN_ON(pasid == IOMMU_PASID_INVALID))
3631 ida_free(&iommu_global_pasid_ida, pasid);
3633 EXPORT_SYMBOL_GPL(iommu_free_global_pasid);