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;
337 lockdep_assert_held(&iommu_probe_device_lock);
342 param = kzalloc(sizeof(*param), GFP_KERNEL);
346 mutex_init(¶m->lock);
351 static void dev_iommu_free(struct device *dev)
353 struct dev_iommu *param = dev->iommu;
357 fwnode_handle_put(param->fwspec->iommu_fwnode);
358 kfree(param->fwspec);
364 * Internal equivalent of device_iommu_mapped() for when we care that a device
365 * actually has API ops, and don't want false positives from VFIO-only groups.
367 static bool dev_has_iommu(struct device *dev)
369 return dev->iommu && dev->iommu->iommu_dev;
372 static u32 dev_iommu_get_max_pasids(struct device *dev)
374 u32 max_pasids = 0, bits = 0;
377 if (dev_is_pci(dev)) {
378 ret = pci_max_pasids(to_pci_dev(dev));
382 ret = device_property_read_u32(dev, "pasid-num-bits", &bits);
384 max_pasids = 1UL << bits;
387 return min_t(u32, max_pasids, dev->iommu->iommu_dev->max_pasids);
390 void dev_iommu_priv_set(struct device *dev, void *priv)
392 /* FSL_PAMU does something weird */
393 if (!IS_ENABLED(CONFIG_FSL_PAMU))
394 lockdep_assert_held(&iommu_probe_device_lock);
395 dev->iommu->priv = priv;
397 EXPORT_SYMBOL_GPL(dev_iommu_priv_set);
400 * Init the dev->iommu and dev->iommu_group in the struct device and get the
403 static int iommu_init_device(struct device *dev, const struct iommu_ops *ops)
405 struct iommu_device *iommu_dev;
406 struct iommu_group *group;
409 if (!dev_iommu_get(dev))
412 if (!try_module_get(ops->owner)) {
417 iommu_dev = ops->probe_device(dev);
418 if (IS_ERR(iommu_dev)) {
419 ret = PTR_ERR(iommu_dev);
422 dev->iommu->iommu_dev = iommu_dev;
424 ret = iommu_device_link(iommu_dev, dev);
428 group = ops->device_group(dev);
429 if (WARN_ON_ONCE(group == NULL))
430 group = ERR_PTR(-EINVAL);
432 ret = PTR_ERR(group);
435 dev->iommu_group = group;
437 dev->iommu->max_pasids = dev_iommu_get_max_pasids(dev);
438 if (ops->is_attach_deferred)
439 dev->iommu->attach_deferred = ops->is_attach_deferred(dev);
443 iommu_device_unlink(iommu_dev, dev);
445 if (ops->release_device)
446 ops->release_device(dev);
448 module_put(ops->owner);
450 dev->iommu->iommu_dev = NULL;
455 static void iommu_deinit_device(struct device *dev)
457 struct iommu_group *group = dev->iommu_group;
458 const struct iommu_ops *ops = dev_iommu_ops(dev);
460 lockdep_assert_held(&group->mutex);
462 iommu_device_unlink(dev->iommu->iommu_dev, dev);
465 * release_device() must stop using any attached domain on the device.
466 * If there are still other devices in the group they are not effected
469 * The IOMMU driver must set the device to either an identity or
470 * blocking translation and stop using any domain pointer, as it is
473 if (ops->release_device)
474 ops->release_device(dev);
477 * If this is the last driver to use the group then we must free the
478 * domains before we do the module_put().
480 if (list_empty(&group->devices)) {
481 if (group->default_domain) {
482 iommu_domain_free(group->default_domain);
483 group->default_domain = NULL;
485 if (group->blocking_domain) {
486 iommu_domain_free(group->blocking_domain);
487 group->blocking_domain = NULL;
489 group->domain = NULL;
492 /* Caller must put iommu_group */
493 dev->iommu_group = NULL;
494 module_put(ops->owner);
498 DEFINE_MUTEX(iommu_probe_device_lock);
500 static int __iommu_probe_device(struct device *dev, struct list_head *group_list)
502 const struct iommu_ops *ops;
503 struct iommu_fwspec *fwspec;
504 struct iommu_group *group;
505 struct group_device *gdev;
509 * For FDT-based systems and ACPI IORT/VIOT, drivers register IOMMU
510 * instances with non-NULL fwnodes, and client devices should have been
511 * identified with a fwspec by this point. Otherwise, we can currently
512 * assume that only one of Intel, AMD, s390, PAMU or legacy SMMUv2 can
513 * be present, and that any of their registered instances has suitable
514 * ops for probing, and thus cheekily co-opt the same mechanism.
516 fwspec = dev_iommu_fwspec_get(dev);
517 if (fwspec && fwspec->ops)
520 ops = iommu_ops_from_fwnode(NULL);
525 * Serialise to avoid races between IOMMU drivers registering in
526 * parallel and/or the "replay" calls from ACPI/OF code via client
527 * driver probe. Once the latter have been cleaned up we should
528 * probably be able to use device_lock() here to minimise the scope,
529 * but for now enforcing a simple global ordering is fine.
531 lockdep_assert_held(&iommu_probe_device_lock);
533 /* Device is probed already if in a group */
534 if (dev->iommu_group)
537 ret = iommu_init_device(dev, ops);
541 group = dev->iommu_group;
542 gdev = iommu_group_alloc_device(group, dev);
543 mutex_lock(&group->mutex);
550 * The gdev must be in the list before calling
551 * iommu_setup_default_domain()
553 list_add_tail(&gdev->list, &group->devices);
554 WARN_ON(group->default_domain && !group->domain);
555 if (group->default_domain)
556 iommu_create_device_direct_mappings(group->default_domain, dev);
558 ret = __iommu_device_set_domain(group, dev, group->domain, 0);
560 goto err_remove_gdev;
561 } else if (!group->default_domain && !group_list) {
562 ret = iommu_setup_default_domain(group, 0);
564 goto err_remove_gdev;
565 } else if (!group->default_domain) {
567 * With a group_list argument we defer the default_domain setup
568 * to the caller by providing a de-duplicated list of groups
569 * that need further setup.
571 if (list_empty(&group->entry))
572 list_add_tail(&group->entry, group_list);
574 mutex_unlock(&group->mutex);
577 iommu_dma_set_pci_32bit_workaround(dev);
582 list_del(&gdev->list);
583 __iommu_group_free_device(group, gdev);
585 iommu_deinit_device(dev);
586 mutex_unlock(&group->mutex);
587 iommu_group_put(group);
592 int iommu_probe_device(struct device *dev)
594 const struct iommu_ops *ops;
597 mutex_lock(&iommu_probe_device_lock);
598 ret = __iommu_probe_device(dev, NULL);
599 mutex_unlock(&iommu_probe_device_lock);
603 ops = dev_iommu_ops(dev);
604 if (ops->probe_finalize)
605 ops->probe_finalize(dev);
610 static void __iommu_group_free_device(struct iommu_group *group,
611 struct group_device *grp_dev)
613 struct device *dev = grp_dev->dev;
615 sysfs_remove_link(group->devices_kobj, grp_dev->name);
616 sysfs_remove_link(&dev->kobj, "iommu_group");
618 trace_remove_device_from_group(group->id, dev);
621 * If the group has become empty then ownership must have been
622 * released, and the current domain must be set back to NULL or
623 * the default domain.
625 if (list_empty(&group->devices))
626 WARN_ON(group->owner_cnt ||
627 group->domain != group->default_domain);
629 kfree(grp_dev->name);
633 /* Remove the iommu_group from the struct device. */
634 static void __iommu_group_remove_device(struct device *dev)
636 struct iommu_group *group = dev->iommu_group;
637 struct group_device *device;
639 mutex_lock(&group->mutex);
640 for_each_group_device(group, device) {
641 if (device->dev != dev)
644 list_del(&device->list);
645 __iommu_group_free_device(group, device);
646 if (dev_has_iommu(dev))
647 iommu_deinit_device(dev);
649 dev->iommu_group = NULL;
652 mutex_unlock(&group->mutex);
655 * Pairs with the get in iommu_init_device() or
656 * iommu_group_add_device()
658 iommu_group_put(group);
661 static void iommu_release_device(struct device *dev)
663 struct iommu_group *group = dev->iommu_group;
666 __iommu_group_remove_device(dev);
668 /* Free any fwspec if no iommu_driver was ever attached */
673 static int __init iommu_set_def_domain_type(char *str)
678 ret = kstrtobool(str, &pt);
683 iommu_set_default_passthrough(true);
685 iommu_set_default_translated(true);
689 early_param("iommu.passthrough", iommu_set_def_domain_type);
691 static int __init iommu_dma_setup(char *str)
693 int ret = kstrtobool(str, &iommu_dma_strict);
696 iommu_cmd_line |= IOMMU_CMD_LINE_STRICT;
699 early_param("iommu.strict", iommu_dma_setup);
701 void iommu_set_dma_strict(void)
703 iommu_dma_strict = true;
704 if (iommu_def_domain_type == IOMMU_DOMAIN_DMA_FQ)
705 iommu_def_domain_type = IOMMU_DOMAIN_DMA;
708 static ssize_t iommu_group_attr_show(struct kobject *kobj,
709 struct attribute *__attr, char *buf)
711 struct iommu_group_attribute *attr = to_iommu_group_attr(__attr);
712 struct iommu_group *group = to_iommu_group(kobj);
716 ret = attr->show(group, buf);
720 static ssize_t iommu_group_attr_store(struct kobject *kobj,
721 struct attribute *__attr,
722 const char *buf, size_t count)
724 struct iommu_group_attribute *attr = to_iommu_group_attr(__attr);
725 struct iommu_group *group = to_iommu_group(kobj);
729 ret = attr->store(group, buf, count);
733 static const struct sysfs_ops iommu_group_sysfs_ops = {
734 .show = iommu_group_attr_show,
735 .store = iommu_group_attr_store,
738 static int iommu_group_create_file(struct iommu_group *group,
739 struct iommu_group_attribute *attr)
741 return sysfs_create_file(&group->kobj, &attr->attr);
744 static void iommu_group_remove_file(struct iommu_group *group,
745 struct iommu_group_attribute *attr)
747 sysfs_remove_file(&group->kobj, &attr->attr);
750 static ssize_t iommu_group_show_name(struct iommu_group *group, char *buf)
752 return sysfs_emit(buf, "%s\n", group->name);
756 * iommu_insert_resv_region - Insert a new region in the
757 * list of reserved regions.
758 * @new: new region to insert
759 * @regions: list of regions
761 * Elements are sorted by start address and overlapping segments
762 * of the same type are merged.
764 static int iommu_insert_resv_region(struct iommu_resv_region *new,
765 struct list_head *regions)
767 struct iommu_resv_region *iter, *tmp, *nr, *top;
770 nr = iommu_alloc_resv_region(new->start, new->length,
771 new->prot, new->type, GFP_KERNEL);
775 /* First add the new element based on start address sorting */
776 list_for_each_entry(iter, regions, list) {
777 if (nr->start < iter->start ||
778 (nr->start == iter->start && nr->type <= iter->type))
781 list_add_tail(&nr->list, &iter->list);
783 /* Merge overlapping segments of type nr->type in @regions, if any */
784 list_for_each_entry_safe(iter, tmp, regions, list) {
785 phys_addr_t top_end, iter_end = iter->start + iter->length - 1;
787 /* no merge needed on elements of different types than @new */
788 if (iter->type != new->type) {
789 list_move_tail(&iter->list, &stack);
793 /* look for the last stack element of same type as @iter */
794 list_for_each_entry_reverse(top, &stack, list)
795 if (top->type == iter->type)
798 list_move_tail(&iter->list, &stack);
802 top_end = top->start + top->length - 1;
804 if (iter->start > top_end + 1) {
805 list_move_tail(&iter->list, &stack);
807 top->length = max(top_end, iter_end) - top->start + 1;
808 list_del(&iter->list);
812 list_splice(&stack, regions);
817 iommu_insert_device_resv_regions(struct list_head *dev_resv_regions,
818 struct list_head *group_resv_regions)
820 struct iommu_resv_region *entry;
823 list_for_each_entry(entry, dev_resv_regions, list) {
824 ret = iommu_insert_resv_region(entry, group_resv_regions);
831 int iommu_get_group_resv_regions(struct iommu_group *group,
832 struct list_head *head)
834 struct group_device *device;
837 mutex_lock(&group->mutex);
838 for_each_group_device(group, device) {
839 struct list_head dev_resv_regions;
842 * Non-API groups still expose reserved_regions in sysfs,
843 * so filter out calls that get here that way.
845 if (!dev_has_iommu(device->dev))
848 INIT_LIST_HEAD(&dev_resv_regions);
849 iommu_get_resv_regions(device->dev, &dev_resv_regions);
850 ret = iommu_insert_device_resv_regions(&dev_resv_regions, head);
851 iommu_put_resv_regions(device->dev, &dev_resv_regions);
855 mutex_unlock(&group->mutex);
858 EXPORT_SYMBOL_GPL(iommu_get_group_resv_regions);
860 static ssize_t iommu_group_show_resv_regions(struct iommu_group *group,
863 struct iommu_resv_region *region, *next;
864 struct list_head group_resv_regions;
867 INIT_LIST_HEAD(&group_resv_regions);
868 iommu_get_group_resv_regions(group, &group_resv_regions);
870 list_for_each_entry_safe(region, next, &group_resv_regions, list) {
871 offset += sysfs_emit_at(buf, offset, "0x%016llx 0x%016llx %s\n",
872 (long long)region->start,
873 (long long)(region->start +
875 iommu_group_resv_type_string[region->type]);
882 static ssize_t iommu_group_show_type(struct iommu_group *group,
885 char *type = "unknown";
887 mutex_lock(&group->mutex);
888 if (group->default_domain) {
889 switch (group->default_domain->type) {
890 case IOMMU_DOMAIN_BLOCKED:
893 case IOMMU_DOMAIN_IDENTITY:
896 case IOMMU_DOMAIN_UNMANAGED:
899 case IOMMU_DOMAIN_DMA:
902 case IOMMU_DOMAIN_DMA_FQ:
907 mutex_unlock(&group->mutex);
909 return sysfs_emit(buf, "%s\n", type);
912 static IOMMU_GROUP_ATTR(name, S_IRUGO, iommu_group_show_name, NULL);
914 static IOMMU_GROUP_ATTR(reserved_regions, 0444,
915 iommu_group_show_resv_regions, NULL);
917 static IOMMU_GROUP_ATTR(type, 0644, iommu_group_show_type,
918 iommu_group_store_type);
920 static void iommu_group_release(struct kobject *kobj)
922 struct iommu_group *group = to_iommu_group(kobj);
924 pr_debug("Releasing group %d\n", group->id);
926 if (group->iommu_data_release)
927 group->iommu_data_release(group->iommu_data);
929 ida_free(&iommu_group_ida, group->id);
931 /* Domains are free'd by iommu_deinit_device() */
932 WARN_ON(group->default_domain);
933 WARN_ON(group->blocking_domain);
939 static const struct kobj_type iommu_group_ktype = {
940 .sysfs_ops = &iommu_group_sysfs_ops,
941 .release = iommu_group_release,
945 * iommu_group_alloc - Allocate a new group
947 * This function is called by an iommu driver to allocate a new iommu
948 * group. The iommu group represents the minimum granularity of the iommu.
949 * Upon successful return, the caller holds a reference to the supplied
950 * group in order to hold the group until devices are added. Use
951 * iommu_group_put() to release this extra reference count, allowing the
952 * group to be automatically reclaimed once it has no devices or external
955 struct iommu_group *iommu_group_alloc(void)
957 struct iommu_group *group;
960 group = kzalloc(sizeof(*group), GFP_KERNEL);
962 return ERR_PTR(-ENOMEM);
964 group->kobj.kset = iommu_group_kset;
965 mutex_init(&group->mutex);
966 INIT_LIST_HEAD(&group->devices);
967 INIT_LIST_HEAD(&group->entry);
968 xa_init(&group->pasid_array);
970 ret = ida_alloc(&iommu_group_ida, GFP_KERNEL);
977 ret = kobject_init_and_add(&group->kobj, &iommu_group_ktype,
978 NULL, "%d", group->id);
980 kobject_put(&group->kobj);
984 group->devices_kobj = kobject_create_and_add("devices", &group->kobj);
985 if (!group->devices_kobj) {
986 kobject_put(&group->kobj); /* triggers .release & free */
987 return ERR_PTR(-ENOMEM);
991 * The devices_kobj holds a reference on the group kobject, so
992 * as long as that exists so will the group. We can therefore
993 * use the devices_kobj for reference counting.
995 kobject_put(&group->kobj);
997 ret = iommu_group_create_file(group,
998 &iommu_group_attr_reserved_regions);
1000 kobject_put(group->devices_kobj);
1001 return ERR_PTR(ret);
1004 ret = iommu_group_create_file(group, &iommu_group_attr_type);
1006 kobject_put(group->devices_kobj);
1007 return ERR_PTR(ret);
1010 pr_debug("Allocated group %d\n", group->id);
1014 EXPORT_SYMBOL_GPL(iommu_group_alloc);
1017 * iommu_group_get_iommudata - retrieve iommu_data registered for a group
1020 * iommu drivers can store data in the group for use when doing iommu
1021 * operations. This function provides a way to retrieve it. Caller
1022 * should hold a group reference.
1024 void *iommu_group_get_iommudata(struct iommu_group *group)
1026 return group->iommu_data;
1028 EXPORT_SYMBOL_GPL(iommu_group_get_iommudata);
1031 * iommu_group_set_iommudata - set iommu_data for a group
1033 * @iommu_data: new data
1034 * @release: release function for iommu_data
1036 * iommu drivers can store data in the group for use when doing iommu
1037 * operations. This function provides a way to set the data after
1038 * the group has been allocated. Caller should hold a group reference.
1040 void iommu_group_set_iommudata(struct iommu_group *group, void *iommu_data,
1041 void (*release)(void *iommu_data))
1043 group->iommu_data = iommu_data;
1044 group->iommu_data_release = release;
1046 EXPORT_SYMBOL_GPL(iommu_group_set_iommudata);
1049 * iommu_group_set_name - set name for a group
1053 * Allow iommu driver to set a name for a group. When set it will
1054 * appear in a name attribute file under the group in sysfs.
1056 int iommu_group_set_name(struct iommu_group *group, const char *name)
1061 iommu_group_remove_file(group, &iommu_group_attr_name);
1068 group->name = kstrdup(name, GFP_KERNEL);
1072 ret = iommu_group_create_file(group, &iommu_group_attr_name);
1081 EXPORT_SYMBOL_GPL(iommu_group_set_name);
1083 static int iommu_create_device_direct_mappings(struct iommu_domain *domain,
1086 struct iommu_resv_region *entry;
1087 struct list_head mappings;
1088 unsigned long pg_size;
1091 pg_size = domain->pgsize_bitmap ? 1UL << __ffs(domain->pgsize_bitmap) : 0;
1092 INIT_LIST_HEAD(&mappings);
1094 if (WARN_ON_ONCE(iommu_is_dma_domain(domain) && !pg_size))
1097 iommu_get_resv_regions(dev, &mappings);
1099 /* We need to consider overlapping regions for different devices */
1100 list_for_each_entry(entry, &mappings, list) {
1101 dma_addr_t start, end, addr;
1102 size_t map_size = 0;
1104 if (entry->type == IOMMU_RESV_DIRECT)
1105 dev->iommu->require_direct = 1;
1107 if ((entry->type != IOMMU_RESV_DIRECT &&
1108 entry->type != IOMMU_RESV_DIRECT_RELAXABLE) ||
1109 !iommu_is_dma_domain(domain))
1112 start = ALIGN(entry->start, pg_size);
1113 end = ALIGN(entry->start + entry->length, pg_size);
1115 for (addr = start; addr <= end; addr += pg_size) {
1116 phys_addr_t phys_addr;
1121 phys_addr = iommu_iova_to_phys(domain, addr);
1123 map_size += pg_size;
1129 ret = iommu_map(domain, addr - map_size,
1130 addr - map_size, map_size,
1131 entry->prot, GFP_KERNEL);
1140 if (!list_empty(&mappings) && iommu_is_dma_domain(domain))
1141 iommu_flush_iotlb_all(domain);
1144 iommu_put_resv_regions(dev, &mappings);
1149 /* This is undone by __iommu_group_free_device() */
1150 static struct group_device *iommu_group_alloc_device(struct iommu_group *group,
1154 struct group_device *device;
1156 device = kzalloc(sizeof(*device), GFP_KERNEL);
1158 return ERR_PTR(-ENOMEM);
1162 ret = sysfs_create_link(&dev->kobj, &group->kobj, "iommu_group");
1164 goto err_free_device;
1166 device->name = kasprintf(GFP_KERNEL, "%s", kobject_name(&dev->kobj));
1168 if (!device->name) {
1170 goto err_remove_link;
1173 ret = sysfs_create_link_nowarn(group->devices_kobj,
1174 &dev->kobj, device->name);
1176 if (ret == -EEXIST && i >= 0) {
1178 * Account for the slim chance of collision
1179 * and append an instance to the name.
1181 kfree(device->name);
1182 device->name = kasprintf(GFP_KERNEL, "%s.%d",
1183 kobject_name(&dev->kobj), i++);
1189 trace_add_device_to_group(group->id, dev);
1191 dev_info(dev, "Adding to iommu group %d\n", group->id);
1196 kfree(device->name);
1198 sysfs_remove_link(&dev->kobj, "iommu_group");
1201 dev_err(dev, "Failed to add to iommu group %d: %d\n", group->id, ret);
1202 return ERR_PTR(ret);
1206 * iommu_group_add_device - add a device to an iommu group
1207 * @group: the group into which to add the device (reference should be held)
1210 * This function is called by an iommu driver to add a device into a
1211 * group. Adding a device increments the group reference count.
1213 int iommu_group_add_device(struct iommu_group *group, struct device *dev)
1215 struct group_device *gdev;
1217 gdev = iommu_group_alloc_device(group, dev);
1219 return PTR_ERR(gdev);
1221 iommu_group_ref_get(group);
1222 dev->iommu_group = group;
1224 mutex_lock(&group->mutex);
1225 list_add_tail(&gdev->list, &group->devices);
1226 mutex_unlock(&group->mutex);
1229 EXPORT_SYMBOL_GPL(iommu_group_add_device);
1232 * iommu_group_remove_device - remove a device from it's current group
1233 * @dev: device to be removed
1235 * This function is called by an iommu driver to remove the device from
1236 * it's current group. This decrements the iommu group reference count.
1238 void iommu_group_remove_device(struct device *dev)
1240 struct iommu_group *group = dev->iommu_group;
1245 dev_info(dev, "Removing from iommu group %d\n", group->id);
1247 __iommu_group_remove_device(dev);
1249 EXPORT_SYMBOL_GPL(iommu_group_remove_device);
1251 static struct device *iommu_group_first_dev(struct iommu_group *group)
1253 lockdep_assert_held(&group->mutex);
1254 return list_first_entry(&group->devices, struct group_device, list)->dev;
1258 * iommu_group_for_each_dev - iterate over each device in the group
1260 * @data: caller opaque data to be passed to callback function
1261 * @fn: caller supplied callback function
1263 * This function is called by group users to iterate over group devices.
1264 * Callers should hold a reference count to the group during callback.
1265 * The group->mutex is held across callbacks, which will block calls to
1266 * iommu_group_add/remove_device.
1268 int iommu_group_for_each_dev(struct iommu_group *group, void *data,
1269 int (*fn)(struct device *, void *))
1271 struct group_device *device;
1274 mutex_lock(&group->mutex);
1275 for_each_group_device(group, device) {
1276 ret = fn(device->dev, data);
1280 mutex_unlock(&group->mutex);
1284 EXPORT_SYMBOL_GPL(iommu_group_for_each_dev);
1287 * iommu_group_get - Return the group for a device and increment reference
1288 * @dev: get the group that this device belongs to
1290 * This function is called by iommu drivers and users to get the group
1291 * for the specified device. If found, the group is returned and the group
1292 * reference in incremented, else NULL.
1294 struct iommu_group *iommu_group_get(struct device *dev)
1296 struct iommu_group *group = dev->iommu_group;
1299 kobject_get(group->devices_kobj);
1303 EXPORT_SYMBOL_GPL(iommu_group_get);
1306 * iommu_group_ref_get - Increment reference on a group
1307 * @group: the group to use, must not be NULL
1309 * This function is called by iommu drivers to take additional references on an
1310 * existing group. Returns the given group for convenience.
1312 struct iommu_group *iommu_group_ref_get(struct iommu_group *group)
1314 kobject_get(group->devices_kobj);
1317 EXPORT_SYMBOL_GPL(iommu_group_ref_get);
1320 * iommu_group_put - Decrement group reference
1321 * @group: the group to use
1323 * This function is called by iommu drivers and users to release the
1324 * iommu group. Once the reference count is zero, the group is released.
1326 void iommu_group_put(struct iommu_group *group)
1329 kobject_put(group->devices_kobj);
1331 EXPORT_SYMBOL_GPL(iommu_group_put);
1334 * iommu_register_device_fault_handler() - Register a device fault handler
1336 * @handler: the fault handler
1337 * @data: private data passed as argument to the handler
1339 * When an IOMMU fault event is received, this handler gets called with the
1340 * fault event and data as argument. The handler should return 0 on success. If
1341 * the fault is recoverable (IOMMU_FAULT_PAGE_REQ), the consumer should also
1342 * complete the fault by calling iommu_page_response() with one of the following
1344 * - IOMMU_PAGE_RESP_SUCCESS: retry the translation
1345 * - IOMMU_PAGE_RESP_INVALID: terminate the fault
1346 * - IOMMU_PAGE_RESP_FAILURE: terminate the fault and stop reporting
1347 * page faults if possible.
1349 * Return 0 if the fault handler was installed successfully, or an error.
1351 int iommu_register_device_fault_handler(struct device *dev,
1352 iommu_dev_fault_handler_t handler,
1355 struct dev_iommu *param = dev->iommu;
1361 mutex_lock(¶m->lock);
1362 /* Only allow one fault handler registered for each device */
1363 if (param->fault_param) {
1369 param->fault_param = kzalloc(sizeof(*param->fault_param), GFP_KERNEL);
1370 if (!param->fault_param) {
1375 param->fault_param->handler = handler;
1376 param->fault_param->data = data;
1377 mutex_init(¶m->fault_param->lock);
1378 INIT_LIST_HEAD(¶m->fault_param->faults);
1381 mutex_unlock(¶m->lock);
1385 EXPORT_SYMBOL_GPL(iommu_register_device_fault_handler);
1388 * iommu_unregister_device_fault_handler() - Unregister the device fault handler
1391 * Remove the device fault handler installed with
1392 * iommu_register_device_fault_handler().
1394 * Return 0 on success, or an error.
1396 int iommu_unregister_device_fault_handler(struct device *dev)
1398 struct dev_iommu *param = dev->iommu;
1404 mutex_lock(¶m->lock);
1406 if (!param->fault_param)
1409 /* we cannot unregister handler if there are pending faults */
1410 if (!list_empty(¶m->fault_param->faults)) {
1415 kfree(param->fault_param);
1416 param->fault_param = NULL;
1419 mutex_unlock(¶m->lock);
1423 EXPORT_SYMBOL_GPL(iommu_unregister_device_fault_handler);
1426 * iommu_report_device_fault() - Report fault event to device driver
1428 * @evt: fault event data
1430 * Called by IOMMU drivers when a fault is detected, typically in a threaded IRQ
1431 * handler. When this function fails and the fault is recoverable, it is the
1432 * caller's responsibility to complete the fault.
1434 * Return 0 on success, or an error.
1436 int iommu_report_device_fault(struct device *dev, struct iommu_fault_event *evt)
1438 struct dev_iommu *param = dev->iommu;
1439 struct iommu_fault_event *evt_pending = NULL;
1440 struct iommu_fault_param *fparam;
1446 /* we only report device fault if there is a handler registered */
1447 mutex_lock(¶m->lock);
1448 fparam = param->fault_param;
1449 if (!fparam || !fparam->handler) {
1454 if (evt->fault.type == IOMMU_FAULT_PAGE_REQ &&
1455 (evt->fault.prm.flags & IOMMU_FAULT_PAGE_REQUEST_LAST_PAGE)) {
1456 evt_pending = kmemdup(evt, sizeof(struct iommu_fault_event),
1462 mutex_lock(&fparam->lock);
1463 list_add_tail(&evt_pending->list, &fparam->faults);
1464 mutex_unlock(&fparam->lock);
1467 ret = fparam->handler(&evt->fault, fparam->data);
1468 if (ret && evt_pending) {
1469 mutex_lock(&fparam->lock);
1470 list_del(&evt_pending->list);
1471 mutex_unlock(&fparam->lock);
1475 mutex_unlock(¶m->lock);
1478 EXPORT_SYMBOL_GPL(iommu_report_device_fault);
1480 int iommu_page_response(struct device *dev,
1481 struct iommu_page_response *msg)
1485 struct iommu_fault_event *evt;
1486 struct iommu_fault_page_request *prm;
1487 struct dev_iommu *param = dev->iommu;
1488 const struct iommu_ops *ops = dev_iommu_ops(dev);
1489 bool has_pasid = msg->flags & IOMMU_PAGE_RESP_PASID_VALID;
1491 if (!ops->page_response)
1494 if (!param || !param->fault_param)
1497 if (msg->version != IOMMU_PAGE_RESP_VERSION_1 ||
1498 msg->flags & ~IOMMU_PAGE_RESP_PASID_VALID)
1501 /* Only send response if there is a fault report pending */
1502 mutex_lock(¶m->fault_param->lock);
1503 if (list_empty(¶m->fault_param->faults)) {
1504 dev_warn_ratelimited(dev, "no pending PRQ, drop response\n");
1508 * Check if we have a matching page request pending to respond,
1509 * otherwise return -EINVAL
1511 list_for_each_entry(evt, ¶m->fault_param->faults, list) {
1512 prm = &evt->fault.prm;
1513 if (prm->grpid != msg->grpid)
1517 * If the PASID is required, the corresponding request is
1518 * matched using the group ID, the PASID valid bit and the PASID
1519 * value. Otherwise only the group ID matches request and
1522 needs_pasid = prm->flags & IOMMU_FAULT_PAGE_RESPONSE_NEEDS_PASID;
1523 if (needs_pasid && (!has_pasid || msg->pasid != prm->pasid))
1526 if (!needs_pasid && has_pasid) {
1527 /* No big deal, just clear it. */
1528 msg->flags &= ~IOMMU_PAGE_RESP_PASID_VALID;
1532 ret = ops->page_response(dev, evt, msg);
1533 list_del(&evt->list);
1539 mutex_unlock(¶m->fault_param->lock);
1542 EXPORT_SYMBOL_GPL(iommu_page_response);
1545 * iommu_group_id - Return ID for a group
1546 * @group: the group to ID
1548 * Return the unique ID for the group matching the sysfs group number.
1550 int iommu_group_id(struct iommu_group *group)
1554 EXPORT_SYMBOL_GPL(iommu_group_id);
1556 static struct iommu_group *get_pci_alias_group(struct pci_dev *pdev,
1557 unsigned long *devfns);
1560 * To consider a PCI device isolated, we require ACS to support Source
1561 * Validation, Request Redirection, Completer Redirection, and Upstream
1562 * Forwarding. This effectively means that devices cannot spoof their
1563 * requester ID, requests and completions cannot be redirected, and all
1564 * transactions are forwarded upstream, even as it passes through a
1565 * bridge where the target device is downstream.
1567 #define REQ_ACS_FLAGS (PCI_ACS_SV | PCI_ACS_RR | PCI_ACS_CR | PCI_ACS_UF)
1570 * For multifunction devices which are not isolated from each other, find
1571 * all the other non-isolated functions and look for existing groups. For
1572 * each function, we also need to look for aliases to or from other devices
1573 * that may already have a group.
1575 static struct iommu_group *get_pci_function_alias_group(struct pci_dev *pdev,
1576 unsigned long *devfns)
1578 struct pci_dev *tmp = NULL;
1579 struct iommu_group *group;
1581 if (!pdev->multifunction || pci_acs_enabled(pdev, REQ_ACS_FLAGS))
1584 for_each_pci_dev(tmp) {
1585 if (tmp == pdev || tmp->bus != pdev->bus ||
1586 PCI_SLOT(tmp->devfn) != PCI_SLOT(pdev->devfn) ||
1587 pci_acs_enabled(tmp, REQ_ACS_FLAGS))
1590 group = get_pci_alias_group(tmp, devfns);
1601 * Look for aliases to or from the given device for existing groups. DMA
1602 * aliases are only supported on the same bus, therefore the search
1603 * space is quite small (especially since we're really only looking at pcie
1604 * device, and therefore only expect multiple slots on the root complex or
1605 * downstream switch ports). It's conceivable though that a pair of
1606 * multifunction devices could have aliases between them that would cause a
1607 * loop. To prevent this, we use a bitmap to track where we've been.
1609 static struct iommu_group *get_pci_alias_group(struct pci_dev *pdev,
1610 unsigned long *devfns)
1612 struct pci_dev *tmp = NULL;
1613 struct iommu_group *group;
1615 if (test_and_set_bit(pdev->devfn & 0xff, devfns))
1618 group = iommu_group_get(&pdev->dev);
1622 for_each_pci_dev(tmp) {
1623 if (tmp == pdev || tmp->bus != pdev->bus)
1626 /* We alias them or they alias us */
1627 if (pci_devs_are_dma_aliases(pdev, tmp)) {
1628 group = get_pci_alias_group(tmp, devfns);
1634 group = get_pci_function_alias_group(tmp, devfns);
1645 struct group_for_pci_data {
1646 struct pci_dev *pdev;
1647 struct iommu_group *group;
1651 * DMA alias iterator callback, return the last seen device. Stop and return
1652 * the IOMMU group if we find one along the way.
1654 static int get_pci_alias_or_group(struct pci_dev *pdev, u16 alias, void *opaque)
1656 struct group_for_pci_data *data = opaque;
1659 data->group = iommu_group_get(&pdev->dev);
1661 return data->group != NULL;
1665 * Generic device_group call-back function. It just allocates one
1666 * iommu-group per device.
1668 struct iommu_group *generic_device_group(struct device *dev)
1670 return iommu_group_alloc();
1672 EXPORT_SYMBOL_GPL(generic_device_group);
1675 * Generic device_group call-back function. It just allocates one
1676 * iommu-group per iommu driver instance shared by every device
1677 * probed by that iommu driver.
1679 struct iommu_group *generic_single_device_group(struct device *dev)
1681 struct iommu_device *iommu = dev->iommu->iommu_dev;
1683 if (!iommu->singleton_group) {
1684 struct iommu_group *group;
1686 group = iommu_group_alloc();
1689 iommu->singleton_group = group;
1691 return iommu_group_ref_get(iommu->singleton_group);
1693 EXPORT_SYMBOL_GPL(generic_single_device_group);
1696 * Use standard PCI bus topology, isolation features, and DMA alias quirks
1697 * to find or create an IOMMU group for a device.
1699 struct iommu_group *pci_device_group(struct device *dev)
1701 struct pci_dev *pdev = to_pci_dev(dev);
1702 struct group_for_pci_data data;
1703 struct pci_bus *bus;
1704 struct iommu_group *group = NULL;
1705 u64 devfns[4] = { 0 };
1707 if (WARN_ON(!dev_is_pci(dev)))
1708 return ERR_PTR(-EINVAL);
1711 * Find the upstream DMA alias for the device. A device must not
1712 * be aliased due to topology in order to have its own IOMMU group.
1713 * If we find an alias along the way that already belongs to a
1716 if (pci_for_each_dma_alias(pdev, get_pci_alias_or_group, &data))
1722 * Continue upstream from the point of minimum IOMMU granularity
1723 * due to aliases to the point where devices are protected from
1724 * peer-to-peer DMA by PCI ACS. Again, if we find an existing
1727 for (bus = pdev->bus; !pci_is_root_bus(bus); bus = bus->parent) {
1731 if (pci_acs_path_enabled(bus->self, NULL, REQ_ACS_FLAGS))
1736 group = iommu_group_get(&pdev->dev);
1742 * Look for existing groups on device aliases. If we alias another
1743 * device or another device aliases us, use the same group.
1745 group = get_pci_alias_group(pdev, (unsigned long *)devfns);
1750 * Look for existing groups on non-isolated functions on the same
1751 * slot and aliases of those funcions, if any. No need to clear
1752 * the search bitmap, the tested devfns are still valid.
1754 group = get_pci_function_alias_group(pdev, (unsigned long *)devfns);
1758 /* No shared group found, allocate new */
1759 return iommu_group_alloc();
1761 EXPORT_SYMBOL_GPL(pci_device_group);
1763 /* Get the IOMMU group for device on fsl-mc bus */
1764 struct iommu_group *fsl_mc_device_group(struct device *dev)
1766 struct device *cont_dev = fsl_mc_cont_dev(dev);
1767 struct iommu_group *group;
1769 group = iommu_group_get(cont_dev);
1771 group = iommu_group_alloc();
1774 EXPORT_SYMBOL_GPL(fsl_mc_device_group);
1776 static struct iommu_domain *
1777 __iommu_group_alloc_default_domain(struct iommu_group *group, int req_type)
1779 if (group->default_domain && group->default_domain->type == req_type)
1780 return group->default_domain;
1781 return __iommu_group_domain_alloc(group, req_type);
1785 * req_type of 0 means "auto" which means to select a domain based on
1786 * iommu_def_domain_type or what the driver actually supports.
1788 static struct iommu_domain *
1789 iommu_group_alloc_default_domain(struct iommu_group *group, int req_type)
1791 const struct iommu_ops *ops = dev_iommu_ops(iommu_group_first_dev(group));
1792 struct iommu_domain *dom;
1794 lockdep_assert_held(&group->mutex);
1797 * Allow legacy drivers to specify the domain that will be the default
1798 * domain. This should always be either an IDENTITY/BLOCKED/PLATFORM
1799 * domain. Do not use in new drivers.
1801 if (ops->default_domain) {
1802 if (req_type != ops->default_domain->type)
1803 return ERR_PTR(-EINVAL);
1804 return ops->default_domain;
1808 return __iommu_group_alloc_default_domain(group, req_type);
1810 /* The driver gave no guidance on what type to use, try the default */
1811 dom = __iommu_group_alloc_default_domain(group, iommu_def_domain_type);
1815 /* Otherwise IDENTITY and DMA_FQ defaults will try DMA */
1816 if (iommu_def_domain_type == IOMMU_DOMAIN_DMA)
1817 return ERR_PTR(-EINVAL);
1818 dom = __iommu_group_alloc_default_domain(group, IOMMU_DOMAIN_DMA);
1822 pr_warn("Failed to allocate default IOMMU domain of type %u for group %s - Falling back to IOMMU_DOMAIN_DMA",
1823 iommu_def_domain_type, group->name);
1827 struct iommu_domain *iommu_group_default_domain(struct iommu_group *group)
1829 return group->default_domain;
1832 static int probe_iommu_group(struct device *dev, void *data)
1834 struct list_head *group_list = data;
1837 mutex_lock(&iommu_probe_device_lock);
1838 ret = __iommu_probe_device(dev, group_list);
1839 mutex_unlock(&iommu_probe_device_lock);
1846 static int iommu_bus_notifier(struct notifier_block *nb,
1847 unsigned long action, void *data)
1849 struct device *dev = data;
1851 if (action == BUS_NOTIFY_ADD_DEVICE) {
1854 ret = iommu_probe_device(dev);
1855 return (ret) ? NOTIFY_DONE : NOTIFY_OK;
1856 } else if (action == BUS_NOTIFY_REMOVED_DEVICE) {
1857 iommu_release_device(dev);
1865 * Combine the driver's chosen def_domain_type across all the devices in a
1866 * group. Drivers must give a consistent result.
1868 static int iommu_get_def_domain_type(struct iommu_group *group,
1869 struct device *dev, int cur_type)
1871 const struct iommu_ops *ops = dev_iommu_ops(dev);
1874 if (ops->default_domain) {
1876 * Drivers that declare a global static default_domain will
1877 * always choose that.
1879 type = ops->default_domain->type;
1881 if (ops->def_domain_type)
1882 type = ops->def_domain_type(dev);
1886 if (!type || cur_type == type)
1891 dev_err_ratelimited(
1893 "IOMMU driver error, requesting conflicting def_domain_type, %s and %s, for devices in group %u.\n",
1894 iommu_domain_type_str(cur_type), iommu_domain_type_str(type),
1898 * Try to recover, drivers are allowed to force IDENITY or DMA, IDENTITY
1901 if (type == IOMMU_DOMAIN_IDENTITY)
1907 * A target_type of 0 will select the best domain type. 0 can be returned in
1908 * this case meaning the global default should be used.
1910 static int iommu_get_default_domain_type(struct iommu_group *group,
1913 struct device *untrusted = NULL;
1914 struct group_device *gdev;
1915 int driver_type = 0;
1917 lockdep_assert_held(&group->mutex);
1920 * ARM32 drivers supporting CONFIG_ARM_DMA_USE_IOMMU can declare an
1921 * identity_domain and it will automatically become their default
1922 * domain. Later on ARM_DMA_USE_IOMMU will install its UNMANAGED domain.
1923 * Override the selection to IDENTITY.
1925 if (IS_ENABLED(CONFIG_ARM_DMA_USE_IOMMU)) {
1926 static_assert(!(IS_ENABLED(CONFIG_ARM_DMA_USE_IOMMU) &&
1927 IS_ENABLED(CONFIG_IOMMU_DMA)));
1928 driver_type = IOMMU_DOMAIN_IDENTITY;
1931 for_each_group_device(group, gdev) {
1932 driver_type = iommu_get_def_domain_type(group, gdev->dev,
1935 if (dev_is_pci(gdev->dev) && to_pci_dev(gdev->dev)->untrusted) {
1937 * No ARM32 using systems will set untrusted, it cannot
1940 if (WARN_ON(IS_ENABLED(CONFIG_ARM_DMA_USE_IOMMU)))
1942 untrusted = gdev->dev;
1947 * If the common dma ops are not selected in kconfig then we cannot use
1948 * IOMMU_DOMAIN_DMA at all. Force IDENTITY if nothing else has been
1951 if (!IS_ENABLED(CONFIG_IOMMU_DMA)) {
1952 if (WARN_ON(driver_type == IOMMU_DOMAIN_DMA))
1955 driver_type = IOMMU_DOMAIN_IDENTITY;
1959 if (driver_type && driver_type != IOMMU_DOMAIN_DMA) {
1960 dev_err_ratelimited(
1962 "Device is not trusted, but driver is overriding group %u to %s, refusing to probe.\n",
1963 group->id, iommu_domain_type_str(driver_type));
1966 driver_type = IOMMU_DOMAIN_DMA;
1970 if (driver_type && target_type != driver_type)
1977 static void iommu_group_do_probe_finalize(struct device *dev)
1979 const struct iommu_ops *ops = dev_iommu_ops(dev);
1981 if (ops->probe_finalize)
1982 ops->probe_finalize(dev);
1985 int bus_iommu_probe(const struct bus_type *bus)
1987 struct iommu_group *group, *next;
1988 LIST_HEAD(group_list);
1991 ret = bus_for_each_dev(bus, NULL, &group_list, probe_iommu_group);
1995 list_for_each_entry_safe(group, next, &group_list, entry) {
1996 struct group_device *gdev;
1998 mutex_lock(&group->mutex);
2000 /* Remove item from the list */
2001 list_del_init(&group->entry);
2004 * We go to the trouble of deferred default domain creation so
2005 * that the cross-group default domain type and the setup of the
2006 * IOMMU_RESV_DIRECT will work correctly in non-hotpug scenarios.
2008 ret = iommu_setup_default_domain(group, 0);
2010 mutex_unlock(&group->mutex);
2013 mutex_unlock(&group->mutex);
2016 * FIXME: Mis-locked because the ops->probe_finalize() call-back
2017 * of some IOMMU drivers calls arm_iommu_attach_device() which
2018 * in-turn might call back into IOMMU core code, where it tries
2019 * to take group->mutex, resulting in a deadlock.
2021 for_each_group_device(group, gdev)
2022 iommu_group_do_probe_finalize(gdev->dev);
2029 * iommu_present() - make platform-specific assumptions about an IOMMU
2030 * @bus: bus to check
2032 * Do not use this function. You want device_iommu_mapped() instead.
2034 * Return: true if some IOMMU is present and aware of devices on the given bus;
2035 * in general it may not be the only IOMMU, and it may not have anything to do
2036 * with whatever device you are ultimately interested in.
2038 bool iommu_present(const struct bus_type *bus)
2042 for (int i = 0; i < ARRAY_SIZE(iommu_buses); i++) {
2043 if (iommu_buses[i] == bus) {
2044 spin_lock(&iommu_device_lock);
2045 ret = !list_empty(&iommu_device_list);
2046 spin_unlock(&iommu_device_lock);
2051 EXPORT_SYMBOL_GPL(iommu_present);
2054 * device_iommu_capable() - check for a general IOMMU capability
2055 * @dev: device to which the capability would be relevant, if available
2056 * @cap: IOMMU capability
2058 * Return: true if an IOMMU is present and supports the given capability
2059 * for the given device, otherwise false.
2061 bool device_iommu_capable(struct device *dev, enum iommu_cap cap)
2063 const struct iommu_ops *ops;
2065 if (!dev_has_iommu(dev))
2068 ops = dev_iommu_ops(dev);
2072 return ops->capable(dev, cap);
2074 EXPORT_SYMBOL_GPL(device_iommu_capable);
2077 * iommu_group_has_isolated_msi() - Compute msi_device_has_isolated_msi()
2079 * @group: Group to query
2081 * IOMMU groups should not have differing values of
2082 * msi_device_has_isolated_msi() for devices in a group. However nothing
2083 * directly prevents this, so ensure mistakes don't result in isolation failures
2084 * by checking that all the devices are the same.
2086 bool iommu_group_has_isolated_msi(struct iommu_group *group)
2088 struct group_device *group_dev;
2091 mutex_lock(&group->mutex);
2092 for_each_group_device(group, group_dev)
2093 ret &= msi_device_has_isolated_msi(group_dev->dev);
2094 mutex_unlock(&group->mutex);
2097 EXPORT_SYMBOL_GPL(iommu_group_has_isolated_msi);
2100 * iommu_set_fault_handler() - set a fault handler for an iommu domain
2101 * @domain: iommu domain
2102 * @handler: fault handler
2103 * @token: user data, will be passed back to the fault handler
2105 * This function should be used by IOMMU users which want to be notified
2106 * whenever an IOMMU fault happens.
2108 * The fault handler itself should return 0 on success, and an appropriate
2109 * error code otherwise.
2111 void iommu_set_fault_handler(struct iommu_domain *domain,
2112 iommu_fault_handler_t handler,
2117 domain->handler = handler;
2118 domain->handler_token = token;
2120 EXPORT_SYMBOL_GPL(iommu_set_fault_handler);
2122 static struct iommu_domain *__iommu_domain_alloc(const struct iommu_ops *ops,
2126 struct iommu_domain *domain;
2127 unsigned int alloc_type = type & IOMMU_DOMAIN_ALLOC_FLAGS;
2129 if (alloc_type == IOMMU_DOMAIN_IDENTITY && ops->identity_domain)
2130 return ops->identity_domain;
2131 else if (alloc_type == IOMMU_DOMAIN_BLOCKED && ops->blocked_domain)
2132 return ops->blocked_domain;
2133 else if (type & __IOMMU_DOMAIN_PAGING && ops->domain_alloc_paging)
2134 domain = ops->domain_alloc_paging(dev);
2135 else if (ops->domain_alloc)
2136 domain = ops->domain_alloc(alloc_type);
2138 return ERR_PTR(-EOPNOTSUPP);
2141 * Many domain_alloc ops now return ERR_PTR, make things easier for the
2142 * driver by accepting ERR_PTR from all domain_alloc ops instead of
2148 return ERR_PTR(-ENOMEM);
2150 domain->type = type;
2151 domain->owner = ops;
2153 * If not already set, assume all sizes by default; the driver
2154 * may override this later
2156 if (!domain->pgsize_bitmap)
2157 domain->pgsize_bitmap = ops->pgsize_bitmap;
2160 domain->ops = ops->default_domain_ops;
2162 if (iommu_is_dma_domain(domain)) {
2165 rc = iommu_get_dma_cookie(domain);
2167 iommu_domain_free(domain);
2174 static struct iommu_domain *
2175 __iommu_group_domain_alloc(struct iommu_group *group, unsigned int type)
2177 struct device *dev = iommu_group_first_dev(group);
2179 return __iommu_domain_alloc(dev_iommu_ops(dev), dev, type);
2182 static int __iommu_domain_alloc_dev(struct device *dev, void *data)
2184 const struct iommu_ops **ops = data;
2186 if (!dev_has_iommu(dev))
2189 if (WARN_ONCE(*ops && *ops != dev_iommu_ops(dev),
2190 "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",
2194 *ops = dev_iommu_ops(dev);
2198 struct iommu_domain *iommu_domain_alloc(const struct bus_type *bus)
2200 const struct iommu_ops *ops = NULL;
2201 int err = bus_for_each_dev(bus, NULL, &ops, __iommu_domain_alloc_dev);
2202 struct iommu_domain *domain;
2207 domain = __iommu_domain_alloc(ops, NULL, IOMMU_DOMAIN_UNMANAGED);
2212 EXPORT_SYMBOL_GPL(iommu_domain_alloc);
2214 void iommu_domain_free(struct iommu_domain *domain)
2216 if (domain->type == IOMMU_DOMAIN_SVA)
2218 iommu_put_dma_cookie(domain);
2219 if (domain->ops->free)
2220 domain->ops->free(domain);
2222 EXPORT_SYMBOL_GPL(iommu_domain_free);
2225 * Put the group's domain back to the appropriate core-owned domain - either the
2226 * standard kernel-mode DMA configuration or an all-DMA-blocked domain.
2228 static void __iommu_group_set_core_domain(struct iommu_group *group)
2230 struct iommu_domain *new_domain;
2233 new_domain = group->blocking_domain;
2235 new_domain = group->default_domain;
2237 __iommu_group_set_domain_nofail(group, new_domain);
2240 static int __iommu_attach_device(struct iommu_domain *domain,
2245 if (unlikely(domain->ops->attach_dev == NULL))
2248 ret = domain->ops->attach_dev(domain, dev);
2251 dev->iommu->attach_deferred = 0;
2252 trace_attach_device_to_domain(dev);
2257 * iommu_attach_device - Attach an IOMMU domain to a device
2258 * @domain: IOMMU domain to attach
2259 * @dev: Device that will be attached
2261 * Returns 0 on success and error code on failure
2263 * Note that EINVAL can be treated as a soft failure, indicating
2264 * that certain configuration of the domain is incompatible with
2265 * the device. In this case attaching a different domain to the
2266 * device may succeed.
2268 int iommu_attach_device(struct iommu_domain *domain, struct device *dev)
2270 /* Caller must be a probed driver on dev */
2271 struct iommu_group *group = dev->iommu_group;
2278 * Lock the group to make sure the device-count doesn't
2279 * change while we are attaching
2281 mutex_lock(&group->mutex);
2283 if (list_count_nodes(&group->devices) != 1)
2286 ret = __iommu_attach_group(domain, group);
2289 mutex_unlock(&group->mutex);
2292 EXPORT_SYMBOL_GPL(iommu_attach_device);
2294 int iommu_deferred_attach(struct device *dev, struct iommu_domain *domain)
2296 if (dev->iommu && dev->iommu->attach_deferred)
2297 return __iommu_attach_device(domain, dev);
2302 void iommu_detach_device(struct iommu_domain *domain, struct device *dev)
2304 /* Caller must be a probed driver on dev */
2305 struct iommu_group *group = dev->iommu_group;
2310 mutex_lock(&group->mutex);
2311 if (WARN_ON(domain != group->domain) ||
2312 WARN_ON(list_count_nodes(&group->devices) != 1))
2314 __iommu_group_set_core_domain(group);
2317 mutex_unlock(&group->mutex);
2319 EXPORT_SYMBOL_GPL(iommu_detach_device);
2321 struct iommu_domain *iommu_get_domain_for_dev(struct device *dev)
2323 /* Caller must be a probed driver on dev */
2324 struct iommu_group *group = dev->iommu_group;
2329 return group->domain;
2331 EXPORT_SYMBOL_GPL(iommu_get_domain_for_dev);
2334 * For IOMMU_DOMAIN_DMA implementations which already provide their own
2335 * guarantees that the group and its default domain are valid and correct.
2337 struct iommu_domain *iommu_get_dma_domain(struct device *dev)
2339 return dev->iommu_group->default_domain;
2342 static int __iommu_attach_group(struct iommu_domain *domain,
2343 struct iommu_group *group)
2347 if (group->domain && group->domain != group->default_domain &&
2348 group->domain != group->blocking_domain)
2351 dev = iommu_group_first_dev(group);
2352 if (!dev_has_iommu(dev) || dev_iommu_ops(dev) != domain->owner)
2355 return __iommu_group_set_domain(group, domain);
2359 * iommu_attach_group - Attach an IOMMU domain to an IOMMU group
2360 * @domain: IOMMU domain to attach
2361 * @group: IOMMU group that will be attached
2363 * Returns 0 on success and error code on failure
2365 * Note that EINVAL can be treated as a soft failure, indicating
2366 * that certain configuration of the domain is incompatible with
2367 * the group. In this case attaching a different domain to the
2368 * group may succeed.
2370 int iommu_attach_group(struct iommu_domain *domain, struct iommu_group *group)
2374 mutex_lock(&group->mutex);
2375 ret = __iommu_attach_group(domain, group);
2376 mutex_unlock(&group->mutex);
2380 EXPORT_SYMBOL_GPL(iommu_attach_group);
2383 * iommu_group_replace_domain - replace the domain that a group is attached to
2384 * @new_domain: new IOMMU domain to replace with
2385 * @group: IOMMU group that will be attached to the new domain
2387 * This API allows the group to switch domains without being forced to go to
2388 * the blocking domain in-between.
2390 * If the currently attached domain is a core domain (e.g. a default_domain),
2391 * it will act just like the iommu_attach_group().
2393 int iommu_group_replace_domain(struct iommu_group *group,
2394 struct iommu_domain *new_domain)
2401 mutex_lock(&group->mutex);
2402 ret = __iommu_group_set_domain(group, new_domain);
2403 mutex_unlock(&group->mutex);
2406 EXPORT_SYMBOL_NS_GPL(iommu_group_replace_domain, IOMMUFD_INTERNAL);
2408 static int __iommu_device_set_domain(struct iommu_group *group,
2410 struct iommu_domain *new_domain,
2416 * If the device requires IOMMU_RESV_DIRECT then we cannot allow
2417 * the blocking domain to be attached as it does not contain the
2418 * required 1:1 mapping. This test effectively excludes the device
2419 * being used with iommu_group_claim_dma_owner() which will block
2420 * vfio and iommufd as well.
2422 if (dev->iommu->require_direct &&
2423 (new_domain->type == IOMMU_DOMAIN_BLOCKED ||
2424 new_domain == group->blocking_domain)) {
2426 "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");
2430 if (dev->iommu->attach_deferred) {
2431 if (new_domain == group->default_domain)
2433 dev->iommu->attach_deferred = 0;
2436 ret = __iommu_attach_device(new_domain, dev);
2439 * If we have a blocking domain then try to attach that in hopes
2440 * of avoiding a UAF. Modern drivers should implement blocking
2441 * domains as global statics that cannot fail.
2443 if ((flags & IOMMU_SET_DOMAIN_MUST_SUCCEED) &&
2444 group->blocking_domain &&
2445 group->blocking_domain != new_domain)
2446 __iommu_attach_device(group->blocking_domain, dev);
2453 * If 0 is returned the group's domain is new_domain. If an error is returned
2454 * then the group's domain will be set back to the existing domain unless
2455 * IOMMU_SET_DOMAIN_MUST_SUCCEED, otherwise an error is returned and the group's
2456 * domains is left inconsistent. This is a driver bug to fail attach with a
2457 * previously good domain. We try to avoid a kernel UAF because of this.
2459 * IOMMU groups are really the natural working unit of the IOMMU, but the IOMMU
2460 * API works on domains and devices. Bridge that gap by iterating over the
2461 * devices in a group. Ideally we'd have a single device which represents the
2462 * requestor ID of the group, but we also allow IOMMU drivers to create policy
2463 * defined minimum sets, where the physical hardware may be able to distiguish
2464 * members, but we wish to group them at a higher level (ex. untrusted
2465 * multi-function PCI devices). Thus we attach each device.
2467 static int __iommu_group_set_domain_internal(struct iommu_group *group,
2468 struct iommu_domain *new_domain,
2471 struct group_device *last_gdev;
2472 struct group_device *gdev;
2476 lockdep_assert_held(&group->mutex);
2478 if (group->domain == new_domain)
2481 if (WARN_ON(!new_domain))
2485 * Changing the domain is done by calling attach_dev() on the new
2486 * domain. This switch does not have to be atomic and DMA can be
2487 * discarded during the transition. DMA must only be able to access
2488 * either new_domain or group->domain, never something else.
2491 for_each_group_device(group, gdev) {
2492 ret = __iommu_device_set_domain(group, gdev->dev, new_domain,
2497 * Keep trying the other devices in the group. If a
2498 * driver fails attach to an otherwise good domain, and
2499 * does not support blocking domains, it should at least
2500 * drop its reference on the current domain so we don't
2503 if (flags & IOMMU_SET_DOMAIN_MUST_SUCCEED)
2508 group->domain = new_domain;
2513 * This is called in error unwind paths. A well behaved driver should
2514 * always allow us to attach to a domain that was already attached.
2517 for_each_group_device(group, gdev) {
2519 * A NULL domain can happen only for first probe, in which case
2520 * we leave group->domain as NULL and let release clean
2524 WARN_ON(__iommu_device_set_domain(
2525 group, gdev->dev, group->domain,
2526 IOMMU_SET_DOMAIN_MUST_SUCCEED));
2527 if (gdev == last_gdev)
2533 void iommu_detach_group(struct iommu_domain *domain, struct iommu_group *group)
2535 mutex_lock(&group->mutex);
2536 __iommu_group_set_core_domain(group);
2537 mutex_unlock(&group->mutex);
2539 EXPORT_SYMBOL_GPL(iommu_detach_group);
2541 phys_addr_t iommu_iova_to_phys(struct iommu_domain *domain, dma_addr_t iova)
2543 if (domain->type == IOMMU_DOMAIN_IDENTITY)
2546 if (domain->type == IOMMU_DOMAIN_BLOCKED)
2549 return domain->ops->iova_to_phys(domain, iova);
2551 EXPORT_SYMBOL_GPL(iommu_iova_to_phys);
2553 static size_t iommu_pgsize(struct iommu_domain *domain, unsigned long iova,
2554 phys_addr_t paddr, size_t size, size_t *count)
2556 unsigned int pgsize_idx, pgsize_idx_next;
2557 unsigned long pgsizes;
2558 size_t offset, pgsize, pgsize_next;
2559 unsigned long addr_merge = paddr | iova;
2561 /* Page sizes supported by the hardware and small enough for @size */
2562 pgsizes = domain->pgsize_bitmap & GENMASK(__fls(size), 0);
2564 /* Constrain the page sizes further based on the maximum alignment */
2565 if (likely(addr_merge))
2566 pgsizes &= GENMASK(__ffs(addr_merge), 0);
2568 /* Make sure we have at least one suitable page size */
2571 /* Pick the biggest page size remaining */
2572 pgsize_idx = __fls(pgsizes);
2573 pgsize = BIT(pgsize_idx);
2577 /* Find the next biggest support page size, if it exists */
2578 pgsizes = domain->pgsize_bitmap & ~GENMASK(pgsize_idx, 0);
2582 pgsize_idx_next = __ffs(pgsizes);
2583 pgsize_next = BIT(pgsize_idx_next);
2586 * There's no point trying a bigger page size unless the virtual
2587 * and physical addresses are similarly offset within the larger page.
2589 if ((iova ^ paddr) & (pgsize_next - 1))
2592 /* Calculate the offset to the next page size alignment boundary */
2593 offset = pgsize_next - (addr_merge & (pgsize_next - 1));
2596 * If size is big enough to accommodate the larger page, reduce
2597 * the number of smaller pages.
2599 if (offset + pgsize_next <= size)
2603 *count = size >> pgsize_idx;
2607 static int __iommu_map(struct iommu_domain *domain, unsigned long iova,
2608 phys_addr_t paddr, size_t size, int prot, gfp_t gfp)
2610 const struct iommu_domain_ops *ops = domain->ops;
2611 unsigned long orig_iova = iova;
2612 unsigned int min_pagesz;
2613 size_t orig_size = size;
2614 phys_addr_t orig_paddr = paddr;
2617 if (unlikely(!(domain->type & __IOMMU_DOMAIN_PAGING)))
2620 if (WARN_ON(!ops->map_pages || domain->pgsize_bitmap == 0UL))
2623 /* find out the minimum page size supported */
2624 min_pagesz = 1 << __ffs(domain->pgsize_bitmap);
2627 * both the virtual address and the physical one, as well as
2628 * the size of the mapping, must be aligned (at least) to the
2629 * size of the smallest page supported by the hardware
2631 if (!IS_ALIGNED(iova | paddr | size, min_pagesz)) {
2632 pr_err("unaligned: iova 0x%lx pa %pa size 0x%zx min_pagesz 0x%x\n",
2633 iova, &paddr, size, min_pagesz);
2637 pr_debug("map: iova 0x%lx pa %pa size 0x%zx\n", iova, &paddr, size);
2640 size_t pgsize, count, mapped = 0;
2642 pgsize = iommu_pgsize(domain, iova, paddr, size, &count);
2644 pr_debug("mapping: iova 0x%lx pa %pa pgsize 0x%zx count %zu\n",
2645 iova, &paddr, pgsize, count);
2646 ret = ops->map_pages(domain, iova, paddr, pgsize, count, prot,
2649 * Some pages may have been mapped, even if an error occurred,
2650 * so we should account for those so they can be unmapped.
2661 /* unroll mapping in case something went wrong */
2663 iommu_unmap(domain, orig_iova, orig_size - size);
2665 trace_map(orig_iova, orig_paddr, orig_size);
2670 int iommu_map(struct iommu_domain *domain, unsigned long iova,
2671 phys_addr_t paddr, size_t size, int prot, gfp_t gfp)
2673 const struct iommu_domain_ops *ops = domain->ops;
2676 might_sleep_if(gfpflags_allow_blocking(gfp));
2678 /* Discourage passing strange GFP flags */
2679 if (WARN_ON_ONCE(gfp & (__GFP_COMP | __GFP_DMA | __GFP_DMA32 |
2683 ret = __iommu_map(domain, iova, paddr, size, prot, gfp);
2684 if (ret == 0 && ops->iotlb_sync_map) {
2685 ret = ops->iotlb_sync_map(domain, iova, size);
2693 /* undo mappings already done */
2694 iommu_unmap(domain, iova, size);
2698 EXPORT_SYMBOL_GPL(iommu_map);
2700 static size_t __iommu_unmap(struct iommu_domain *domain,
2701 unsigned long iova, size_t size,
2702 struct iommu_iotlb_gather *iotlb_gather)
2704 const struct iommu_domain_ops *ops = domain->ops;
2705 size_t unmapped_page, unmapped = 0;
2706 unsigned long orig_iova = iova;
2707 unsigned int min_pagesz;
2709 if (unlikely(!(domain->type & __IOMMU_DOMAIN_PAGING)))
2712 if (WARN_ON(!ops->unmap_pages || domain->pgsize_bitmap == 0UL))
2715 /* find out the minimum page size supported */
2716 min_pagesz = 1 << __ffs(domain->pgsize_bitmap);
2719 * The virtual address, as well as the size of the mapping, must be
2720 * aligned (at least) to the size of the smallest page supported
2723 if (!IS_ALIGNED(iova | size, min_pagesz)) {
2724 pr_err("unaligned: iova 0x%lx size 0x%zx min_pagesz 0x%x\n",
2725 iova, size, min_pagesz);
2729 pr_debug("unmap this: iova 0x%lx size 0x%zx\n", iova, size);
2732 * Keep iterating until we either unmap 'size' bytes (or more)
2733 * or we hit an area that isn't mapped.
2735 while (unmapped < size) {
2736 size_t pgsize, count;
2738 pgsize = iommu_pgsize(domain, iova, iova, size - unmapped, &count);
2739 unmapped_page = ops->unmap_pages(domain, iova, pgsize, count, iotlb_gather);
2743 pr_debug("unmapped: iova 0x%lx size 0x%zx\n",
2744 iova, unmapped_page);
2746 iova += unmapped_page;
2747 unmapped += unmapped_page;
2750 trace_unmap(orig_iova, size, unmapped);
2754 size_t iommu_unmap(struct iommu_domain *domain,
2755 unsigned long iova, size_t size)
2757 struct iommu_iotlb_gather iotlb_gather;
2760 iommu_iotlb_gather_init(&iotlb_gather);
2761 ret = __iommu_unmap(domain, iova, size, &iotlb_gather);
2762 iommu_iotlb_sync(domain, &iotlb_gather);
2766 EXPORT_SYMBOL_GPL(iommu_unmap);
2768 size_t iommu_unmap_fast(struct iommu_domain *domain,
2769 unsigned long iova, size_t size,
2770 struct iommu_iotlb_gather *iotlb_gather)
2772 return __iommu_unmap(domain, iova, size, iotlb_gather);
2774 EXPORT_SYMBOL_GPL(iommu_unmap_fast);
2776 ssize_t iommu_map_sg(struct iommu_domain *domain, unsigned long iova,
2777 struct scatterlist *sg, unsigned int nents, int prot,
2780 const struct iommu_domain_ops *ops = domain->ops;
2781 size_t len = 0, mapped = 0;
2786 might_sleep_if(gfpflags_allow_blocking(gfp));
2788 /* Discourage passing strange GFP flags */
2789 if (WARN_ON_ONCE(gfp & (__GFP_COMP | __GFP_DMA | __GFP_DMA32 |
2793 while (i <= nents) {
2794 phys_addr_t s_phys = sg_phys(sg);
2796 if (len && s_phys != start + len) {
2797 ret = __iommu_map(domain, iova + mapped, start,
2807 if (sg_dma_is_bus_address(sg))
2822 if (ops->iotlb_sync_map) {
2823 ret = ops->iotlb_sync_map(domain, iova, mapped);
2830 /* undo mappings already done */
2831 iommu_unmap(domain, iova, mapped);
2835 EXPORT_SYMBOL_GPL(iommu_map_sg);
2838 * report_iommu_fault() - report about an IOMMU fault to the IOMMU framework
2839 * @domain: the iommu domain where the fault has happened
2840 * @dev: the device where the fault has happened
2841 * @iova: the faulting address
2842 * @flags: mmu fault flags (e.g. IOMMU_FAULT_READ/IOMMU_FAULT_WRITE/...)
2844 * This function should be called by the low-level IOMMU implementations
2845 * whenever IOMMU faults happen, to allow high-level users, that are
2846 * interested in such events, to know about them.
2848 * This event may be useful for several possible use cases:
2849 * - mere logging of the event
2850 * - dynamic TLB/PTE loading
2851 * - if restarting of the faulting device is required
2853 * Returns 0 on success and an appropriate error code otherwise (if dynamic
2854 * PTE/TLB loading will one day be supported, implementations will be able
2855 * to tell whether it succeeded or not according to this return value).
2857 * Specifically, -ENOSYS is returned if a fault handler isn't installed
2858 * (though fault handlers can also return -ENOSYS, in case they want to
2859 * elicit the default behavior of the IOMMU drivers).
2861 int report_iommu_fault(struct iommu_domain *domain, struct device *dev,
2862 unsigned long iova, int flags)
2867 * if upper layers showed interest and installed a fault handler,
2870 if (domain->handler)
2871 ret = domain->handler(domain, dev, iova, flags,
2872 domain->handler_token);
2874 trace_io_page_fault(dev, iova, flags);
2877 EXPORT_SYMBOL_GPL(report_iommu_fault);
2879 static int __init iommu_init(void)
2881 iommu_group_kset = kset_create_and_add("iommu_groups",
2883 BUG_ON(!iommu_group_kset);
2885 iommu_debugfs_setup();
2889 core_initcall(iommu_init);
2891 int iommu_enable_nesting(struct iommu_domain *domain)
2893 if (domain->type != IOMMU_DOMAIN_UNMANAGED)
2895 if (!domain->ops->enable_nesting)
2897 return domain->ops->enable_nesting(domain);
2899 EXPORT_SYMBOL_GPL(iommu_enable_nesting);
2901 int iommu_set_pgtable_quirks(struct iommu_domain *domain,
2902 unsigned long quirk)
2904 if (domain->type != IOMMU_DOMAIN_UNMANAGED)
2906 if (!domain->ops->set_pgtable_quirks)
2908 return domain->ops->set_pgtable_quirks(domain, quirk);
2910 EXPORT_SYMBOL_GPL(iommu_set_pgtable_quirks);
2913 * iommu_get_resv_regions - get reserved regions
2914 * @dev: device for which to get reserved regions
2915 * @list: reserved region list for device
2917 * This returns a list of reserved IOVA regions specific to this device.
2918 * A domain user should not map IOVA in these ranges.
2920 void iommu_get_resv_regions(struct device *dev, struct list_head *list)
2922 const struct iommu_ops *ops = dev_iommu_ops(dev);
2924 if (ops->get_resv_regions)
2925 ops->get_resv_regions(dev, list);
2927 EXPORT_SYMBOL_GPL(iommu_get_resv_regions);
2930 * iommu_put_resv_regions - release reserved regions
2931 * @dev: device for which to free reserved regions
2932 * @list: reserved region list for device
2934 * This releases a reserved region list acquired by iommu_get_resv_regions().
2936 void iommu_put_resv_regions(struct device *dev, struct list_head *list)
2938 struct iommu_resv_region *entry, *next;
2940 list_for_each_entry_safe(entry, next, list, list) {
2942 entry->free(dev, entry);
2947 EXPORT_SYMBOL(iommu_put_resv_regions);
2949 struct iommu_resv_region *iommu_alloc_resv_region(phys_addr_t start,
2950 size_t length, int prot,
2951 enum iommu_resv_type type,
2954 struct iommu_resv_region *region;
2956 region = kzalloc(sizeof(*region), gfp);
2960 INIT_LIST_HEAD(®ion->list);
2961 region->start = start;
2962 region->length = length;
2963 region->prot = prot;
2964 region->type = type;
2967 EXPORT_SYMBOL_GPL(iommu_alloc_resv_region);
2969 void iommu_set_default_passthrough(bool cmd_line)
2972 iommu_cmd_line |= IOMMU_CMD_LINE_DMA_API;
2973 iommu_def_domain_type = IOMMU_DOMAIN_IDENTITY;
2976 void iommu_set_default_translated(bool cmd_line)
2979 iommu_cmd_line |= IOMMU_CMD_LINE_DMA_API;
2980 iommu_def_domain_type = IOMMU_DOMAIN_DMA;
2983 bool iommu_default_passthrough(void)
2985 return iommu_def_domain_type == IOMMU_DOMAIN_IDENTITY;
2987 EXPORT_SYMBOL_GPL(iommu_default_passthrough);
2989 const struct iommu_ops *iommu_ops_from_fwnode(struct fwnode_handle *fwnode)
2991 const struct iommu_ops *ops = NULL;
2992 struct iommu_device *iommu;
2994 spin_lock(&iommu_device_lock);
2995 list_for_each_entry(iommu, &iommu_device_list, list)
2996 if (iommu->fwnode == fwnode) {
3000 spin_unlock(&iommu_device_lock);
3004 int iommu_fwspec_init(struct device *dev, struct fwnode_handle *iommu_fwnode,
3005 const struct iommu_ops *ops)
3007 struct iommu_fwspec *fwspec = dev_iommu_fwspec_get(dev);
3010 return ops == fwspec->ops ? 0 : -EINVAL;
3012 if (!dev_iommu_get(dev))
3015 /* Preallocate for the overwhelmingly common case of 1 ID */
3016 fwspec = kzalloc(struct_size(fwspec, ids, 1), GFP_KERNEL);
3020 of_node_get(to_of_node(iommu_fwnode));
3021 fwspec->iommu_fwnode = iommu_fwnode;
3023 dev_iommu_fwspec_set(dev, fwspec);
3026 EXPORT_SYMBOL_GPL(iommu_fwspec_init);
3028 void iommu_fwspec_free(struct device *dev)
3030 struct iommu_fwspec *fwspec = dev_iommu_fwspec_get(dev);
3033 fwnode_handle_put(fwspec->iommu_fwnode);
3035 dev_iommu_fwspec_set(dev, NULL);
3038 EXPORT_SYMBOL_GPL(iommu_fwspec_free);
3040 int iommu_fwspec_add_ids(struct device *dev, u32 *ids, int num_ids)
3042 struct iommu_fwspec *fwspec = dev_iommu_fwspec_get(dev);
3048 new_num = fwspec->num_ids + num_ids;
3050 fwspec = krealloc(fwspec, struct_size(fwspec, ids, new_num),
3055 dev_iommu_fwspec_set(dev, fwspec);
3058 for (i = 0; i < num_ids; i++)
3059 fwspec->ids[fwspec->num_ids + i] = ids[i];
3061 fwspec->num_ids = new_num;
3064 EXPORT_SYMBOL_GPL(iommu_fwspec_add_ids);
3067 * Per device IOMMU features.
3069 int iommu_dev_enable_feature(struct device *dev, enum iommu_dev_features feat)
3071 if (dev_has_iommu(dev)) {
3072 const struct iommu_ops *ops = dev_iommu_ops(dev);
3074 if (ops->dev_enable_feat)
3075 return ops->dev_enable_feat(dev, feat);
3080 EXPORT_SYMBOL_GPL(iommu_dev_enable_feature);
3083 * The device drivers should do the necessary cleanups before calling this.
3085 int iommu_dev_disable_feature(struct device *dev, enum iommu_dev_features feat)
3087 if (dev_has_iommu(dev)) {
3088 const struct iommu_ops *ops = dev_iommu_ops(dev);
3090 if (ops->dev_disable_feat)
3091 return ops->dev_disable_feat(dev, feat);
3096 EXPORT_SYMBOL_GPL(iommu_dev_disable_feature);
3099 * iommu_setup_default_domain - Set the default_domain for the group
3100 * @group: Group to change
3101 * @target_type: Domain type to set as the default_domain
3103 * Allocate a default domain and set it as the current domain on the group. If
3104 * the group already has a default domain it will be changed to the target_type.
3105 * When target_type is 0 the default domain is selected based on driver and
3106 * system preferences.
3108 static int iommu_setup_default_domain(struct iommu_group *group,
3111 struct iommu_domain *old_dom = group->default_domain;
3112 struct group_device *gdev;
3113 struct iommu_domain *dom;
3118 lockdep_assert_held(&group->mutex);
3120 req_type = iommu_get_default_domain_type(group, target_type);
3124 dom = iommu_group_alloc_default_domain(group, req_type);
3126 return PTR_ERR(dom);
3128 if (group->default_domain == dom)
3132 * IOMMU_RESV_DIRECT and IOMMU_RESV_DIRECT_RELAXABLE regions must be
3133 * mapped before their device is attached, in order to guarantee
3134 * continuity with any FW activity
3136 direct_failed = false;
3137 for_each_group_device(group, gdev) {
3138 if (iommu_create_device_direct_mappings(dom, gdev->dev)) {
3139 direct_failed = true;
3141 gdev->dev->iommu->iommu_dev->dev,
3142 "IOMMU driver was not able to establish FW requested direct mapping.");
3146 /* We must set default_domain early for __iommu_device_set_domain */
3147 group->default_domain = dom;
3148 if (!group->domain) {
3150 * Drivers are not allowed to fail the first domain attach.
3151 * The only way to recover from this is to fail attaching the
3152 * iommu driver and call ops->release_device. Put the domain
3153 * in group->default_domain so it is freed after.
3155 ret = __iommu_group_set_domain_internal(
3156 group, dom, IOMMU_SET_DOMAIN_MUST_SUCCEED);
3160 ret = __iommu_group_set_domain(group, dom);
3162 goto err_restore_def_domain;
3166 * Drivers are supposed to allow mappings to be installed in a domain
3167 * before device attachment, but some don't. Hack around this defect by
3168 * trying again after attaching. If this happens it means the device
3169 * will not continuously have the IOMMU_RESV_DIRECT map.
3171 if (direct_failed) {
3172 for_each_group_device(group, gdev) {
3173 ret = iommu_create_device_direct_mappings(dom, gdev->dev);
3175 goto err_restore_domain;
3181 iommu_domain_free(old_dom);
3186 __iommu_group_set_domain_internal(
3187 group, old_dom, IOMMU_SET_DOMAIN_MUST_SUCCEED);
3188 err_restore_def_domain:
3190 iommu_domain_free(dom);
3191 group->default_domain = old_dom;
3197 * Changing the default domain through sysfs requires the users to unbind the
3198 * drivers from the devices in the iommu group, except for a DMA -> DMA-FQ
3199 * transition. Return failure if this isn't met.
3201 * We need to consider the race between this and the device release path.
3202 * group->mutex is used here to guarantee that the device release path
3203 * will not be entered at the same time.
3205 static ssize_t iommu_group_store_type(struct iommu_group *group,
3206 const char *buf, size_t count)
3208 struct group_device *gdev;
3211 if (!capable(CAP_SYS_ADMIN) || !capable(CAP_SYS_RAWIO))
3214 if (WARN_ON(!group) || !group->default_domain)
3217 if (sysfs_streq(buf, "identity"))
3218 req_type = IOMMU_DOMAIN_IDENTITY;
3219 else if (sysfs_streq(buf, "DMA"))
3220 req_type = IOMMU_DOMAIN_DMA;
3221 else if (sysfs_streq(buf, "DMA-FQ"))
3222 req_type = IOMMU_DOMAIN_DMA_FQ;
3223 else if (sysfs_streq(buf, "auto"))
3228 mutex_lock(&group->mutex);
3229 /* We can bring up a flush queue without tearing down the domain. */
3230 if (req_type == IOMMU_DOMAIN_DMA_FQ &&
3231 group->default_domain->type == IOMMU_DOMAIN_DMA) {
3232 ret = iommu_dma_init_fq(group->default_domain);
3236 group->default_domain->type = IOMMU_DOMAIN_DMA_FQ;
3241 /* Otherwise, ensure that device exists and no driver is bound. */
3242 if (list_empty(&group->devices) || group->owner_cnt) {
3247 ret = iommu_setup_default_domain(group, req_type);
3252 * Release the mutex here because ops->probe_finalize() call-back of
3253 * some vendor IOMMU drivers calls arm_iommu_attach_device() which
3254 * in-turn might call back into IOMMU core code, where it tries to take
3255 * group->mutex, resulting in a deadlock.
3257 mutex_unlock(&group->mutex);
3259 /* Make sure dma_ops is appropriatley set */
3260 for_each_group_device(group, gdev)
3261 iommu_group_do_probe_finalize(gdev->dev);
3265 mutex_unlock(&group->mutex);
3266 return ret ?: count;
3270 * iommu_device_use_default_domain() - Device driver wants to handle device
3271 * DMA through the kernel DMA API.
3274 * The device driver about to bind @dev wants to do DMA through the kernel
3275 * DMA API. Return 0 if it is allowed, otherwise an error.
3277 int iommu_device_use_default_domain(struct device *dev)
3279 /* Caller is the driver core during the pre-probe path */
3280 struct iommu_group *group = dev->iommu_group;
3286 mutex_lock(&group->mutex);
3287 if (group->owner_cnt) {
3288 if (group->domain != group->default_domain || group->owner ||
3289 !xa_empty(&group->pasid_array)) {
3298 mutex_unlock(&group->mutex);
3303 * iommu_device_unuse_default_domain() - Device driver stops handling device
3304 * DMA through the kernel DMA API.
3307 * The device driver doesn't want to do DMA through kernel DMA API anymore.
3308 * It must be called after iommu_device_use_default_domain().
3310 void iommu_device_unuse_default_domain(struct device *dev)
3312 /* Caller is the driver core during the post-probe path */
3313 struct iommu_group *group = dev->iommu_group;
3318 mutex_lock(&group->mutex);
3319 if (!WARN_ON(!group->owner_cnt || !xa_empty(&group->pasid_array)))
3322 mutex_unlock(&group->mutex);
3325 static int __iommu_group_alloc_blocking_domain(struct iommu_group *group)
3327 struct iommu_domain *domain;
3329 if (group->blocking_domain)
3332 domain = __iommu_group_domain_alloc(group, IOMMU_DOMAIN_BLOCKED);
3333 if (IS_ERR(domain)) {
3335 * For drivers that do not yet understand IOMMU_DOMAIN_BLOCKED
3336 * create an empty domain instead.
3338 domain = __iommu_group_domain_alloc(group,
3339 IOMMU_DOMAIN_UNMANAGED);
3341 return PTR_ERR(domain);
3343 group->blocking_domain = domain;
3347 static int __iommu_take_dma_ownership(struct iommu_group *group, void *owner)
3351 if ((group->domain && group->domain != group->default_domain) ||
3352 !xa_empty(&group->pasid_array))
3355 ret = __iommu_group_alloc_blocking_domain(group);
3358 ret = __iommu_group_set_domain(group, group->blocking_domain);
3362 group->owner = owner;
3368 * iommu_group_claim_dma_owner() - Set DMA ownership of a group
3369 * @group: The group.
3370 * @owner: Caller specified pointer. Used for exclusive ownership.
3372 * This is to support backward compatibility for vfio which manages the dma
3373 * ownership in iommu_group level. New invocations on this interface should be
3374 * prohibited. Only a single owner may exist for a group.
3376 int iommu_group_claim_dma_owner(struct iommu_group *group, void *owner)
3380 if (WARN_ON(!owner))
3383 mutex_lock(&group->mutex);
3384 if (group->owner_cnt) {
3389 ret = __iommu_take_dma_ownership(group, owner);
3391 mutex_unlock(&group->mutex);
3395 EXPORT_SYMBOL_GPL(iommu_group_claim_dma_owner);
3398 * iommu_device_claim_dma_owner() - Set DMA ownership of a device
3400 * @owner: Caller specified pointer. Used for exclusive ownership.
3402 * Claim the DMA ownership of a device. Multiple devices in the same group may
3403 * concurrently claim ownership if they present the same owner value. Returns 0
3404 * on success and error code on failure
3406 int iommu_device_claim_dma_owner(struct device *dev, void *owner)
3408 /* Caller must be a probed driver on dev */
3409 struct iommu_group *group = dev->iommu_group;
3412 if (WARN_ON(!owner))
3418 mutex_lock(&group->mutex);
3419 if (group->owner_cnt) {
3420 if (group->owner != owner) {
3428 ret = __iommu_take_dma_ownership(group, owner);
3430 mutex_unlock(&group->mutex);
3433 EXPORT_SYMBOL_GPL(iommu_device_claim_dma_owner);
3435 static void __iommu_release_dma_ownership(struct iommu_group *group)
3437 if (WARN_ON(!group->owner_cnt || !group->owner ||
3438 !xa_empty(&group->pasid_array)))
3441 group->owner_cnt = 0;
3442 group->owner = NULL;
3443 __iommu_group_set_domain_nofail(group, group->default_domain);
3447 * iommu_group_release_dma_owner() - Release DMA ownership of a group
3450 * Release the DMA ownership claimed by iommu_group_claim_dma_owner().
3452 void iommu_group_release_dma_owner(struct iommu_group *group)
3454 mutex_lock(&group->mutex);
3455 __iommu_release_dma_ownership(group);
3456 mutex_unlock(&group->mutex);
3458 EXPORT_SYMBOL_GPL(iommu_group_release_dma_owner);
3461 * iommu_device_release_dma_owner() - Release DMA ownership of a device
3464 * Release the DMA ownership claimed by iommu_device_claim_dma_owner().
3466 void iommu_device_release_dma_owner(struct device *dev)
3468 /* Caller must be a probed driver on dev */
3469 struct iommu_group *group = dev->iommu_group;
3471 mutex_lock(&group->mutex);
3472 if (group->owner_cnt > 1)
3475 __iommu_release_dma_ownership(group);
3476 mutex_unlock(&group->mutex);
3478 EXPORT_SYMBOL_GPL(iommu_device_release_dma_owner);
3481 * iommu_group_dma_owner_claimed() - Query group dma ownership status
3482 * @group: The group.
3484 * This provides status query on a given group. It is racy and only for
3485 * non-binding status reporting.
3487 bool iommu_group_dma_owner_claimed(struct iommu_group *group)
3491 mutex_lock(&group->mutex);
3492 user = group->owner_cnt;
3493 mutex_unlock(&group->mutex);
3497 EXPORT_SYMBOL_GPL(iommu_group_dma_owner_claimed);
3499 static int __iommu_set_group_pasid(struct iommu_domain *domain,
3500 struct iommu_group *group, ioasid_t pasid)
3502 struct group_device *device;
3505 for_each_group_device(group, device) {
3506 ret = domain->ops->set_dev_pasid(domain, device->dev, pasid);
3514 static void __iommu_remove_group_pasid(struct iommu_group *group,
3517 struct group_device *device;
3518 const struct iommu_ops *ops;
3520 for_each_group_device(group, device) {
3521 ops = dev_iommu_ops(device->dev);
3522 ops->remove_dev_pasid(device->dev, pasid);
3527 * iommu_attach_device_pasid() - Attach a domain to pasid of device
3528 * @domain: the iommu domain.
3529 * @dev: the attached device.
3530 * @pasid: the pasid of the device.
3532 * Return: 0 on success, or an error.
3534 int iommu_attach_device_pasid(struct iommu_domain *domain,
3535 struct device *dev, ioasid_t pasid)
3537 /* Caller must be a probed driver on dev */
3538 struct iommu_group *group = dev->iommu_group;
3542 if (!domain->ops->set_dev_pasid)
3548 if (!dev_has_iommu(dev) || dev_iommu_ops(dev) != domain->owner)
3551 mutex_lock(&group->mutex);
3552 curr = xa_cmpxchg(&group->pasid_array, pasid, NULL, domain, GFP_KERNEL);
3554 ret = xa_err(curr) ? : -EBUSY;
3558 ret = __iommu_set_group_pasid(domain, group, pasid);
3560 __iommu_remove_group_pasid(group, pasid);
3561 xa_erase(&group->pasid_array, pasid);
3564 mutex_unlock(&group->mutex);
3567 EXPORT_SYMBOL_GPL(iommu_attach_device_pasid);
3570 * iommu_detach_device_pasid() - Detach the domain from pasid of device
3571 * @domain: the iommu domain.
3572 * @dev: the attached device.
3573 * @pasid: the pasid of the device.
3575 * The @domain must have been attached to @pasid of the @dev with
3576 * iommu_attach_device_pasid().
3578 void iommu_detach_device_pasid(struct iommu_domain *domain, struct device *dev,
3581 /* Caller must be a probed driver on dev */
3582 struct iommu_group *group = dev->iommu_group;
3584 mutex_lock(&group->mutex);
3585 __iommu_remove_group_pasid(group, pasid);
3586 WARN_ON(xa_erase(&group->pasid_array, pasid) != domain);
3587 mutex_unlock(&group->mutex);
3589 EXPORT_SYMBOL_GPL(iommu_detach_device_pasid);
3592 * iommu_get_domain_for_dev_pasid() - Retrieve domain for @pasid of @dev
3593 * @dev: the queried device
3594 * @pasid: the pasid of the device
3595 * @type: matched domain type, 0 for any match
3597 * This is a variant of iommu_get_domain_for_dev(). It returns the existing
3598 * domain attached to pasid of a device. Callers must hold a lock around this
3599 * function, and both iommu_attach/detach_dev_pasid() whenever a domain of
3600 * type is being manipulated. This API does not internally resolve races with
3603 * Return: attached domain on success, NULL otherwise.
3605 struct iommu_domain *iommu_get_domain_for_dev_pasid(struct device *dev,
3609 /* Caller must be a probed driver on dev */
3610 struct iommu_group *group = dev->iommu_group;
3611 struct iommu_domain *domain;
3616 xa_lock(&group->pasid_array);
3617 domain = xa_load(&group->pasid_array, pasid);
3618 if (type && domain && domain->type != type)
3619 domain = ERR_PTR(-EBUSY);
3620 xa_unlock(&group->pasid_array);
3624 EXPORT_SYMBOL_GPL(iommu_get_domain_for_dev_pasid);
3626 struct iommu_domain *iommu_sva_domain_alloc(struct device *dev,
3627 struct mm_struct *mm)
3629 const struct iommu_ops *ops = dev_iommu_ops(dev);
3630 struct iommu_domain *domain;
3632 domain = ops->domain_alloc(IOMMU_DOMAIN_SVA);
3636 domain->type = IOMMU_DOMAIN_SVA;
3639 domain->owner = ops;
3640 domain->iopf_handler = iommu_sva_handle_iopf;
3641 domain->fault_data = mm;
3646 ioasid_t iommu_alloc_global_pasid(struct device *dev)
3650 /* max_pasids == 0 means that the device does not support PASID */
3651 if (!dev->iommu->max_pasids)
3652 return IOMMU_PASID_INVALID;
3655 * max_pasids is set up by vendor driver based on number of PASID bits
3656 * supported but the IDA allocation is inclusive.
3658 ret = ida_alloc_range(&iommu_global_pasid_ida, IOMMU_FIRST_GLOBAL_PASID,
3659 dev->iommu->max_pasids - 1, GFP_KERNEL);
3660 return ret < 0 ? IOMMU_PASID_INVALID : ret;
3662 EXPORT_SYMBOL_GPL(iommu_alloc_global_pasid);
3664 void iommu_free_global_pasid(ioasid_t pasid)
3666 if (WARN_ON(pasid == IOMMU_PASID_INVALID))
3669 ida_free(&iommu_global_pasid_ida, pasid);
3671 EXPORT_SYMBOL_GPL(iommu_free_global_pasid);