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 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))
261 * Temporarily enforce global restriction to a single driver. This was
262 * already the de-facto behaviour, since any possible combination of
263 * existing drivers would compete for at least the PCI or platform bus.
265 if (iommu_buses[0]->iommu_ops && iommu_buses[0]->iommu_ops != ops)
270 iommu->fwnode = dev_fwnode(hwdev);
272 spin_lock(&iommu_device_lock);
273 list_add_tail(&iommu->list, &iommu_device_list);
274 spin_unlock(&iommu_device_lock);
276 for (int i = 0; i < ARRAY_SIZE(iommu_buses) && !err; i++) {
277 iommu_buses[i]->iommu_ops = ops;
278 err = bus_iommu_probe(iommu_buses[i]);
281 iommu_device_unregister(iommu);
284 EXPORT_SYMBOL_GPL(iommu_device_register);
286 void iommu_device_unregister(struct iommu_device *iommu)
288 for (int i = 0; i < ARRAY_SIZE(iommu_buses); i++)
289 bus_for_each_dev(iommu_buses[i], NULL, iommu, remove_iommu_group);
291 spin_lock(&iommu_device_lock);
292 list_del(&iommu->list);
293 spin_unlock(&iommu_device_lock);
295 /* Pairs with the alloc in generic_single_device_group() */
296 iommu_group_put(iommu->singleton_group);
297 iommu->singleton_group = NULL;
299 EXPORT_SYMBOL_GPL(iommu_device_unregister);
301 #if IS_ENABLED(CONFIG_IOMMUFD_TEST)
302 void iommu_device_unregister_bus(struct iommu_device *iommu,
303 struct bus_type *bus,
304 struct notifier_block *nb)
306 bus_unregister_notifier(bus, nb);
307 iommu_device_unregister(iommu);
309 EXPORT_SYMBOL_GPL(iommu_device_unregister_bus);
312 * Register an iommu driver against a single bus. This is only used by iommufd
313 * selftest to create a mock iommu driver. The caller must provide
314 * some memory to hold a notifier_block.
316 int iommu_device_register_bus(struct iommu_device *iommu,
317 const struct iommu_ops *ops, struct bus_type *bus,
318 struct notifier_block *nb)
323 nb->notifier_call = iommu_bus_notifier;
324 err = bus_register_notifier(bus, nb);
328 spin_lock(&iommu_device_lock);
329 list_add_tail(&iommu->list, &iommu_device_list);
330 spin_unlock(&iommu_device_lock);
332 bus->iommu_ops = ops;
333 err = bus_iommu_probe(bus);
335 iommu_device_unregister_bus(iommu, bus, nb);
340 EXPORT_SYMBOL_GPL(iommu_device_register_bus);
343 static struct dev_iommu *dev_iommu_get(struct device *dev)
345 struct dev_iommu *param = dev->iommu;
350 param = kzalloc(sizeof(*param), GFP_KERNEL);
354 mutex_init(¶m->lock);
359 static void dev_iommu_free(struct device *dev)
361 struct dev_iommu *param = dev->iommu;
365 fwnode_handle_put(param->fwspec->iommu_fwnode);
366 kfree(param->fwspec);
371 static u32 dev_iommu_get_max_pasids(struct device *dev)
373 u32 max_pasids = 0, bits = 0;
376 if (dev_is_pci(dev)) {
377 ret = pci_max_pasids(to_pci_dev(dev));
381 ret = device_property_read_u32(dev, "pasid-num-bits", &bits);
383 max_pasids = 1UL << bits;
386 return min_t(u32, max_pasids, dev->iommu->iommu_dev->max_pasids);
390 * Init the dev->iommu and dev->iommu_group in the struct device and get the
393 static int iommu_init_device(struct device *dev, const struct iommu_ops *ops)
395 struct iommu_device *iommu_dev;
396 struct iommu_group *group;
399 if (!dev_iommu_get(dev))
402 if (!try_module_get(ops->owner)) {
407 iommu_dev = ops->probe_device(dev);
408 if (IS_ERR(iommu_dev)) {
409 ret = PTR_ERR(iommu_dev);
412 dev->iommu->iommu_dev = iommu_dev;
414 ret = iommu_device_link(iommu_dev, dev);
418 group = ops->device_group(dev);
419 if (WARN_ON_ONCE(group == NULL))
420 group = ERR_PTR(-EINVAL);
422 ret = PTR_ERR(group);
425 dev->iommu_group = group;
427 dev->iommu->max_pasids = dev_iommu_get_max_pasids(dev);
428 if (ops->is_attach_deferred)
429 dev->iommu->attach_deferred = ops->is_attach_deferred(dev);
433 iommu_device_unlink(iommu_dev, dev);
435 if (ops->release_device)
436 ops->release_device(dev);
438 module_put(ops->owner);
440 dev->iommu->iommu_dev = NULL;
445 static void iommu_deinit_device(struct device *dev)
447 struct iommu_group *group = dev->iommu_group;
448 const struct iommu_ops *ops = dev_iommu_ops(dev);
450 lockdep_assert_held(&group->mutex);
452 iommu_device_unlink(dev->iommu->iommu_dev, dev);
455 * release_device() must stop using any attached domain on the device.
456 * If there are still other devices in the group they are not effected
459 * The IOMMU driver must set the device to either an identity or
460 * blocking translation and stop using any domain pointer, as it is
463 if (ops->release_device)
464 ops->release_device(dev);
467 * If this is the last driver to use the group then we must free the
468 * domains before we do the module_put().
470 if (list_empty(&group->devices)) {
471 if (group->default_domain) {
472 iommu_domain_free(group->default_domain);
473 group->default_domain = NULL;
475 if (group->blocking_domain) {
476 iommu_domain_free(group->blocking_domain);
477 group->blocking_domain = NULL;
479 group->domain = NULL;
482 /* Caller must put iommu_group */
483 dev->iommu_group = NULL;
484 module_put(ops->owner);
488 static int __iommu_probe_device(struct device *dev, struct list_head *group_list)
490 const struct iommu_ops *ops = dev->bus->iommu_ops;
491 struct iommu_group *group;
492 static DEFINE_MUTEX(iommu_probe_device_lock);
493 struct group_device *gdev;
499 * Serialise to avoid races between IOMMU drivers registering in
500 * parallel and/or the "replay" calls from ACPI/OF code via client
501 * driver probe. Once the latter have been cleaned up we should
502 * probably be able to use device_lock() here to minimise the scope,
503 * but for now enforcing a simple global ordering is fine.
505 mutex_lock(&iommu_probe_device_lock);
507 /* Device is probed already if in a group */
508 if (dev->iommu_group) {
513 ret = iommu_init_device(dev, ops);
517 group = dev->iommu_group;
518 gdev = iommu_group_alloc_device(group, dev);
519 mutex_lock(&group->mutex);
526 * The gdev must be in the list before calling
527 * iommu_setup_default_domain()
529 list_add_tail(&gdev->list, &group->devices);
530 WARN_ON(group->default_domain && !group->domain);
531 if (group->default_domain)
532 iommu_create_device_direct_mappings(group->default_domain, dev);
534 ret = __iommu_device_set_domain(group, dev, group->domain, 0);
536 goto err_remove_gdev;
537 } else if (!group->default_domain && !group_list) {
538 ret = iommu_setup_default_domain(group, 0);
540 goto err_remove_gdev;
541 } else if (!group->default_domain) {
543 * With a group_list argument we defer the default_domain setup
544 * to the caller by providing a de-duplicated list of groups
545 * that need further setup.
547 if (list_empty(&group->entry))
548 list_add_tail(&group->entry, group_list);
550 mutex_unlock(&group->mutex);
551 mutex_unlock(&iommu_probe_device_lock);
554 iommu_dma_set_pci_32bit_workaround(dev);
559 list_del(&gdev->list);
560 __iommu_group_free_device(group, gdev);
562 iommu_deinit_device(dev);
563 mutex_unlock(&group->mutex);
564 iommu_group_put(group);
566 mutex_unlock(&iommu_probe_device_lock);
571 int iommu_probe_device(struct device *dev)
573 const struct iommu_ops *ops;
576 ret = __iommu_probe_device(dev, NULL);
580 ops = dev_iommu_ops(dev);
581 if (ops->probe_finalize)
582 ops->probe_finalize(dev);
587 static void __iommu_group_free_device(struct iommu_group *group,
588 struct group_device *grp_dev)
590 struct device *dev = grp_dev->dev;
592 sysfs_remove_link(group->devices_kobj, grp_dev->name);
593 sysfs_remove_link(&dev->kobj, "iommu_group");
595 trace_remove_device_from_group(group->id, dev);
598 * If the group has become empty then ownership must have been
599 * released, and the current domain must be set back to NULL or
600 * the default domain.
602 if (list_empty(&group->devices))
603 WARN_ON(group->owner_cnt ||
604 group->domain != group->default_domain);
606 kfree(grp_dev->name);
610 /* Remove the iommu_group from the struct device. */
611 static void __iommu_group_remove_device(struct device *dev)
613 struct iommu_group *group = dev->iommu_group;
614 struct group_device *device;
616 mutex_lock(&group->mutex);
617 for_each_group_device(group, device) {
618 if (device->dev != dev)
621 list_del(&device->list);
622 __iommu_group_free_device(group, device);
623 if (dev->iommu && dev->iommu->iommu_dev)
624 iommu_deinit_device(dev);
626 dev->iommu_group = NULL;
629 mutex_unlock(&group->mutex);
632 * Pairs with the get in iommu_init_device() or
633 * iommu_group_add_device()
635 iommu_group_put(group);
638 static void iommu_release_device(struct device *dev)
640 struct iommu_group *group = dev->iommu_group;
643 __iommu_group_remove_device(dev);
645 /* Free any fwspec if no iommu_driver was ever attached */
650 static int __init iommu_set_def_domain_type(char *str)
655 ret = kstrtobool(str, &pt);
660 iommu_set_default_passthrough(true);
662 iommu_set_default_translated(true);
666 early_param("iommu.passthrough", iommu_set_def_domain_type);
668 static int __init iommu_dma_setup(char *str)
670 int ret = kstrtobool(str, &iommu_dma_strict);
673 iommu_cmd_line |= IOMMU_CMD_LINE_STRICT;
676 early_param("iommu.strict", iommu_dma_setup);
678 void iommu_set_dma_strict(void)
680 iommu_dma_strict = true;
681 if (iommu_def_domain_type == IOMMU_DOMAIN_DMA_FQ)
682 iommu_def_domain_type = IOMMU_DOMAIN_DMA;
685 static ssize_t iommu_group_attr_show(struct kobject *kobj,
686 struct attribute *__attr, char *buf)
688 struct iommu_group_attribute *attr = to_iommu_group_attr(__attr);
689 struct iommu_group *group = to_iommu_group(kobj);
693 ret = attr->show(group, buf);
697 static ssize_t iommu_group_attr_store(struct kobject *kobj,
698 struct attribute *__attr,
699 const char *buf, size_t count)
701 struct iommu_group_attribute *attr = to_iommu_group_attr(__attr);
702 struct iommu_group *group = to_iommu_group(kobj);
706 ret = attr->store(group, buf, count);
710 static const struct sysfs_ops iommu_group_sysfs_ops = {
711 .show = iommu_group_attr_show,
712 .store = iommu_group_attr_store,
715 static int iommu_group_create_file(struct iommu_group *group,
716 struct iommu_group_attribute *attr)
718 return sysfs_create_file(&group->kobj, &attr->attr);
721 static void iommu_group_remove_file(struct iommu_group *group,
722 struct iommu_group_attribute *attr)
724 sysfs_remove_file(&group->kobj, &attr->attr);
727 static ssize_t iommu_group_show_name(struct iommu_group *group, char *buf)
729 return sysfs_emit(buf, "%s\n", group->name);
733 * iommu_insert_resv_region - Insert a new region in the
734 * list of reserved regions.
735 * @new: new region to insert
736 * @regions: list of regions
738 * Elements are sorted by start address and overlapping segments
739 * of the same type are merged.
741 static int iommu_insert_resv_region(struct iommu_resv_region *new,
742 struct list_head *regions)
744 struct iommu_resv_region *iter, *tmp, *nr, *top;
747 nr = iommu_alloc_resv_region(new->start, new->length,
748 new->prot, new->type, GFP_KERNEL);
752 /* First add the new element based on start address sorting */
753 list_for_each_entry(iter, regions, list) {
754 if (nr->start < iter->start ||
755 (nr->start == iter->start && nr->type <= iter->type))
758 list_add_tail(&nr->list, &iter->list);
760 /* Merge overlapping segments of type nr->type in @regions, if any */
761 list_for_each_entry_safe(iter, tmp, regions, list) {
762 phys_addr_t top_end, iter_end = iter->start + iter->length - 1;
764 /* no merge needed on elements of different types than @new */
765 if (iter->type != new->type) {
766 list_move_tail(&iter->list, &stack);
770 /* look for the last stack element of same type as @iter */
771 list_for_each_entry_reverse(top, &stack, list)
772 if (top->type == iter->type)
775 list_move_tail(&iter->list, &stack);
779 top_end = top->start + top->length - 1;
781 if (iter->start > top_end + 1) {
782 list_move_tail(&iter->list, &stack);
784 top->length = max(top_end, iter_end) - top->start + 1;
785 list_del(&iter->list);
789 list_splice(&stack, regions);
794 iommu_insert_device_resv_regions(struct list_head *dev_resv_regions,
795 struct list_head *group_resv_regions)
797 struct iommu_resv_region *entry;
800 list_for_each_entry(entry, dev_resv_regions, list) {
801 ret = iommu_insert_resv_region(entry, group_resv_regions);
808 int iommu_get_group_resv_regions(struct iommu_group *group,
809 struct list_head *head)
811 struct group_device *device;
814 mutex_lock(&group->mutex);
815 for_each_group_device(group, device) {
816 struct list_head dev_resv_regions;
819 * Non-API groups still expose reserved_regions in sysfs,
820 * so filter out calls that get here that way.
822 if (!device->dev->iommu)
825 INIT_LIST_HEAD(&dev_resv_regions);
826 iommu_get_resv_regions(device->dev, &dev_resv_regions);
827 ret = iommu_insert_device_resv_regions(&dev_resv_regions, head);
828 iommu_put_resv_regions(device->dev, &dev_resv_regions);
832 mutex_unlock(&group->mutex);
835 EXPORT_SYMBOL_GPL(iommu_get_group_resv_regions);
837 static ssize_t iommu_group_show_resv_regions(struct iommu_group *group,
840 struct iommu_resv_region *region, *next;
841 struct list_head group_resv_regions;
844 INIT_LIST_HEAD(&group_resv_regions);
845 iommu_get_group_resv_regions(group, &group_resv_regions);
847 list_for_each_entry_safe(region, next, &group_resv_regions, list) {
848 offset += sysfs_emit_at(buf, offset, "0x%016llx 0x%016llx %s\n",
849 (long long)region->start,
850 (long long)(region->start +
852 iommu_group_resv_type_string[region->type]);
859 static ssize_t iommu_group_show_type(struct iommu_group *group,
862 char *type = "unknown";
864 mutex_lock(&group->mutex);
865 if (group->default_domain) {
866 switch (group->default_domain->type) {
867 case IOMMU_DOMAIN_BLOCKED:
870 case IOMMU_DOMAIN_IDENTITY:
873 case IOMMU_DOMAIN_UNMANAGED:
876 case IOMMU_DOMAIN_DMA:
879 case IOMMU_DOMAIN_DMA_FQ:
884 mutex_unlock(&group->mutex);
886 return sysfs_emit(buf, "%s\n", type);
889 static IOMMU_GROUP_ATTR(name, S_IRUGO, iommu_group_show_name, NULL);
891 static IOMMU_GROUP_ATTR(reserved_regions, 0444,
892 iommu_group_show_resv_regions, NULL);
894 static IOMMU_GROUP_ATTR(type, 0644, iommu_group_show_type,
895 iommu_group_store_type);
897 static void iommu_group_release(struct kobject *kobj)
899 struct iommu_group *group = to_iommu_group(kobj);
901 pr_debug("Releasing group %d\n", group->id);
903 if (group->iommu_data_release)
904 group->iommu_data_release(group->iommu_data);
906 ida_free(&iommu_group_ida, group->id);
908 /* Domains are free'd by iommu_deinit_device() */
909 WARN_ON(group->default_domain);
910 WARN_ON(group->blocking_domain);
916 static const struct kobj_type iommu_group_ktype = {
917 .sysfs_ops = &iommu_group_sysfs_ops,
918 .release = iommu_group_release,
922 * iommu_group_alloc - Allocate a new group
924 * This function is called by an iommu driver to allocate a new iommu
925 * group. The iommu group represents the minimum granularity of the iommu.
926 * Upon successful return, the caller holds a reference to the supplied
927 * group in order to hold the group until devices are added. Use
928 * iommu_group_put() to release this extra reference count, allowing the
929 * group to be automatically reclaimed once it has no devices or external
932 struct iommu_group *iommu_group_alloc(void)
934 struct iommu_group *group;
937 group = kzalloc(sizeof(*group), GFP_KERNEL);
939 return ERR_PTR(-ENOMEM);
941 group->kobj.kset = iommu_group_kset;
942 mutex_init(&group->mutex);
943 INIT_LIST_HEAD(&group->devices);
944 INIT_LIST_HEAD(&group->entry);
945 xa_init(&group->pasid_array);
947 ret = ida_alloc(&iommu_group_ida, GFP_KERNEL);
954 ret = kobject_init_and_add(&group->kobj, &iommu_group_ktype,
955 NULL, "%d", group->id);
957 kobject_put(&group->kobj);
961 group->devices_kobj = kobject_create_and_add("devices", &group->kobj);
962 if (!group->devices_kobj) {
963 kobject_put(&group->kobj); /* triggers .release & free */
964 return ERR_PTR(-ENOMEM);
968 * The devices_kobj holds a reference on the group kobject, so
969 * as long as that exists so will the group. We can therefore
970 * use the devices_kobj for reference counting.
972 kobject_put(&group->kobj);
974 ret = iommu_group_create_file(group,
975 &iommu_group_attr_reserved_regions);
977 kobject_put(group->devices_kobj);
981 ret = iommu_group_create_file(group, &iommu_group_attr_type);
983 kobject_put(group->devices_kobj);
987 pr_debug("Allocated group %d\n", group->id);
991 EXPORT_SYMBOL_GPL(iommu_group_alloc);
994 * iommu_group_get_iommudata - retrieve iommu_data registered for a group
997 * iommu drivers can store data in the group for use when doing iommu
998 * operations. This function provides a way to retrieve it. Caller
999 * should hold a group reference.
1001 void *iommu_group_get_iommudata(struct iommu_group *group)
1003 return group->iommu_data;
1005 EXPORT_SYMBOL_GPL(iommu_group_get_iommudata);
1008 * iommu_group_set_iommudata - set iommu_data for a group
1010 * @iommu_data: new data
1011 * @release: release function for iommu_data
1013 * iommu drivers can store data in the group for use when doing iommu
1014 * operations. This function provides a way to set the data after
1015 * the group has been allocated. Caller should hold a group reference.
1017 void iommu_group_set_iommudata(struct iommu_group *group, void *iommu_data,
1018 void (*release)(void *iommu_data))
1020 group->iommu_data = iommu_data;
1021 group->iommu_data_release = release;
1023 EXPORT_SYMBOL_GPL(iommu_group_set_iommudata);
1026 * iommu_group_set_name - set name for a group
1030 * Allow iommu driver to set a name for a group. When set it will
1031 * appear in a name attribute file under the group in sysfs.
1033 int iommu_group_set_name(struct iommu_group *group, const char *name)
1038 iommu_group_remove_file(group, &iommu_group_attr_name);
1045 group->name = kstrdup(name, GFP_KERNEL);
1049 ret = iommu_group_create_file(group, &iommu_group_attr_name);
1058 EXPORT_SYMBOL_GPL(iommu_group_set_name);
1060 static int iommu_create_device_direct_mappings(struct iommu_domain *domain,
1063 struct iommu_resv_region *entry;
1064 struct list_head mappings;
1065 unsigned long pg_size;
1068 pg_size = domain->pgsize_bitmap ? 1UL << __ffs(domain->pgsize_bitmap) : 0;
1069 INIT_LIST_HEAD(&mappings);
1071 if (WARN_ON_ONCE(iommu_is_dma_domain(domain) && !pg_size))
1074 iommu_get_resv_regions(dev, &mappings);
1076 /* We need to consider overlapping regions for different devices */
1077 list_for_each_entry(entry, &mappings, list) {
1078 dma_addr_t start, end, addr;
1079 size_t map_size = 0;
1081 if (entry->type == IOMMU_RESV_DIRECT)
1082 dev->iommu->require_direct = 1;
1084 if ((entry->type != IOMMU_RESV_DIRECT &&
1085 entry->type != IOMMU_RESV_DIRECT_RELAXABLE) ||
1086 !iommu_is_dma_domain(domain))
1089 start = ALIGN(entry->start, pg_size);
1090 end = ALIGN(entry->start + entry->length, pg_size);
1092 for (addr = start; addr <= end; addr += pg_size) {
1093 phys_addr_t phys_addr;
1098 phys_addr = iommu_iova_to_phys(domain, addr);
1100 map_size += pg_size;
1106 ret = iommu_map(domain, addr - map_size,
1107 addr - map_size, map_size,
1108 entry->prot, GFP_KERNEL);
1117 if (!list_empty(&mappings) && iommu_is_dma_domain(domain))
1118 iommu_flush_iotlb_all(domain);
1121 iommu_put_resv_regions(dev, &mappings);
1126 /* This is undone by __iommu_group_free_device() */
1127 static struct group_device *iommu_group_alloc_device(struct iommu_group *group,
1131 struct group_device *device;
1133 device = kzalloc(sizeof(*device), GFP_KERNEL);
1135 return ERR_PTR(-ENOMEM);
1139 ret = sysfs_create_link(&dev->kobj, &group->kobj, "iommu_group");
1141 goto err_free_device;
1143 device->name = kasprintf(GFP_KERNEL, "%s", kobject_name(&dev->kobj));
1145 if (!device->name) {
1147 goto err_remove_link;
1150 ret = sysfs_create_link_nowarn(group->devices_kobj,
1151 &dev->kobj, device->name);
1153 if (ret == -EEXIST && i >= 0) {
1155 * Account for the slim chance of collision
1156 * and append an instance to the name.
1158 kfree(device->name);
1159 device->name = kasprintf(GFP_KERNEL, "%s.%d",
1160 kobject_name(&dev->kobj), i++);
1166 trace_add_device_to_group(group->id, dev);
1168 dev_info(dev, "Adding to iommu group %d\n", group->id);
1173 kfree(device->name);
1175 sysfs_remove_link(&dev->kobj, "iommu_group");
1178 dev_err(dev, "Failed to add to iommu group %d: %d\n", group->id, ret);
1179 return ERR_PTR(ret);
1183 * iommu_group_add_device - add a device to an iommu group
1184 * @group: the group into which to add the device (reference should be held)
1187 * This function is called by an iommu driver to add a device into a
1188 * group. Adding a device increments the group reference count.
1190 int iommu_group_add_device(struct iommu_group *group, struct device *dev)
1192 struct group_device *gdev;
1194 gdev = iommu_group_alloc_device(group, dev);
1196 return PTR_ERR(gdev);
1198 iommu_group_ref_get(group);
1199 dev->iommu_group = group;
1201 mutex_lock(&group->mutex);
1202 list_add_tail(&gdev->list, &group->devices);
1203 mutex_unlock(&group->mutex);
1206 EXPORT_SYMBOL_GPL(iommu_group_add_device);
1209 * iommu_group_remove_device - remove a device from it's current group
1210 * @dev: device to be removed
1212 * This function is called by an iommu driver to remove the device from
1213 * it's current group. This decrements the iommu group reference count.
1215 void iommu_group_remove_device(struct device *dev)
1217 struct iommu_group *group = dev->iommu_group;
1222 dev_info(dev, "Removing from iommu group %d\n", group->id);
1224 __iommu_group_remove_device(dev);
1226 EXPORT_SYMBOL_GPL(iommu_group_remove_device);
1229 * iommu_group_for_each_dev - iterate over each device in the group
1231 * @data: caller opaque data to be passed to callback function
1232 * @fn: caller supplied callback function
1234 * This function is called by group users to iterate over group devices.
1235 * Callers should hold a reference count to the group during callback.
1236 * The group->mutex is held across callbacks, which will block calls to
1237 * iommu_group_add/remove_device.
1239 int iommu_group_for_each_dev(struct iommu_group *group, void *data,
1240 int (*fn)(struct device *, void *))
1242 struct group_device *device;
1245 mutex_lock(&group->mutex);
1246 for_each_group_device(group, device) {
1247 ret = fn(device->dev, data);
1251 mutex_unlock(&group->mutex);
1255 EXPORT_SYMBOL_GPL(iommu_group_for_each_dev);
1258 * iommu_group_get - Return the group for a device and increment reference
1259 * @dev: get the group that this device belongs to
1261 * This function is called by iommu drivers and users to get the group
1262 * for the specified device. If found, the group is returned and the group
1263 * reference in incremented, else NULL.
1265 struct iommu_group *iommu_group_get(struct device *dev)
1267 struct iommu_group *group = dev->iommu_group;
1270 kobject_get(group->devices_kobj);
1274 EXPORT_SYMBOL_GPL(iommu_group_get);
1277 * iommu_group_ref_get - Increment reference on a group
1278 * @group: the group to use, must not be NULL
1280 * This function is called by iommu drivers to take additional references on an
1281 * existing group. Returns the given group for convenience.
1283 struct iommu_group *iommu_group_ref_get(struct iommu_group *group)
1285 kobject_get(group->devices_kobj);
1288 EXPORT_SYMBOL_GPL(iommu_group_ref_get);
1291 * iommu_group_put - Decrement group reference
1292 * @group: the group to use
1294 * This function is called by iommu drivers and users to release the
1295 * iommu group. Once the reference count is zero, the group is released.
1297 void iommu_group_put(struct iommu_group *group)
1300 kobject_put(group->devices_kobj);
1302 EXPORT_SYMBOL_GPL(iommu_group_put);
1305 * iommu_register_device_fault_handler() - Register a device fault handler
1307 * @handler: the fault handler
1308 * @data: private data passed as argument to the handler
1310 * When an IOMMU fault event is received, this handler gets called with the
1311 * fault event and data as argument. The handler should return 0 on success. If
1312 * the fault is recoverable (IOMMU_FAULT_PAGE_REQ), the consumer should also
1313 * complete the fault by calling iommu_page_response() with one of the following
1315 * - IOMMU_PAGE_RESP_SUCCESS: retry the translation
1316 * - IOMMU_PAGE_RESP_INVALID: terminate the fault
1317 * - IOMMU_PAGE_RESP_FAILURE: terminate the fault and stop reporting
1318 * page faults if possible.
1320 * Return 0 if the fault handler was installed successfully, or an error.
1322 int iommu_register_device_fault_handler(struct device *dev,
1323 iommu_dev_fault_handler_t handler,
1326 struct dev_iommu *param = dev->iommu;
1332 mutex_lock(¶m->lock);
1333 /* Only allow one fault handler registered for each device */
1334 if (param->fault_param) {
1340 param->fault_param = kzalloc(sizeof(*param->fault_param), GFP_KERNEL);
1341 if (!param->fault_param) {
1346 param->fault_param->handler = handler;
1347 param->fault_param->data = data;
1348 mutex_init(¶m->fault_param->lock);
1349 INIT_LIST_HEAD(¶m->fault_param->faults);
1352 mutex_unlock(¶m->lock);
1356 EXPORT_SYMBOL_GPL(iommu_register_device_fault_handler);
1359 * iommu_unregister_device_fault_handler() - Unregister the device fault handler
1362 * Remove the device fault handler installed with
1363 * iommu_register_device_fault_handler().
1365 * Return 0 on success, or an error.
1367 int iommu_unregister_device_fault_handler(struct device *dev)
1369 struct dev_iommu *param = dev->iommu;
1375 mutex_lock(¶m->lock);
1377 if (!param->fault_param)
1380 /* we cannot unregister handler if there are pending faults */
1381 if (!list_empty(¶m->fault_param->faults)) {
1386 kfree(param->fault_param);
1387 param->fault_param = NULL;
1390 mutex_unlock(¶m->lock);
1394 EXPORT_SYMBOL_GPL(iommu_unregister_device_fault_handler);
1397 * iommu_report_device_fault() - Report fault event to device driver
1399 * @evt: fault event data
1401 * Called by IOMMU drivers when a fault is detected, typically in a threaded IRQ
1402 * handler. When this function fails and the fault is recoverable, it is the
1403 * caller's responsibility to complete the fault.
1405 * Return 0 on success, or an error.
1407 int iommu_report_device_fault(struct device *dev, struct iommu_fault_event *evt)
1409 struct dev_iommu *param = dev->iommu;
1410 struct iommu_fault_event *evt_pending = NULL;
1411 struct iommu_fault_param *fparam;
1417 /* we only report device fault if there is a handler registered */
1418 mutex_lock(¶m->lock);
1419 fparam = param->fault_param;
1420 if (!fparam || !fparam->handler) {
1425 if (evt->fault.type == IOMMU_FAULT_PAGE_REQ &&
1426 (evt->fault.prm.flags & IOMMU_FAULT_PAGE_REQUEST_LAST_PAGE)) {
1427 evt_pending = kmemdup(evt, sizeof(struct iommu_fault_event),
1433 mutex_lock(&fparam->lock);
1434 list_add_tail(&evt_pending->list, &fparam->faults);
1435 mutex_unlock(&fparam->lock);
1438 ret = fparam->handler(&evt->fault, fparam->data);
1439 if (ret && evt_pending) {
1440 mutex_lock(&fparam->lock);
1441 list_del(&evt_pending->list);
1442 mutex_unlock(&fparam->lock);
1446 mutex_unlock(¶m->lock);
1449 EXPORT_SYMBOL_GPL(iommu_report_device_fault);
1451 int iommu_page_response(struct device *dev,
1452 struct iommu_page_response *msg)
1456 struct iommu_fault_event *evt;
1457 struct iommu_fault_page_request *prm;
1458 struct dev_iommu *param = dev->iommu;
1459 const struct iommu_ops *ops = dev_iommu_ops(dev);
1460 bool has_pasid = msg->flags & IOMMU_PAGE_RESP_PASID_VALID;
1462 if (!ops->page_response)
1465 if (!param || !param->fault_param)
1468 if (msg->version != IOMMU_PAGE_RESP_VERSION_1 ||
1469 msg->flags & ~IOMMU_PAGE_RESP_PASID_VALID)
1472 /* Only send response if there is a fault report pending */
1473 mutex_lock(¶m->fault_param->lock);
1474 if (list_empty(¶m->fault_param->faults)) {
1475 dev_warn_ratelimited(dev, "no pending PRQ, drop response\n");
1479 * Check if we have a matching page request pending to respond,
1480 * otherwise return -EINVAL
1482 list_for_each_entry(evt, ¶m->fault_param->faults, list) {
1483 prm = &evt->fault.prm;
1484 if (prm->grpid != msg->grpid)
1488 * If the PASID is required, the corresponding request is
1489 * matched using the group ID, the PASID valid bit and the PASID
1490 * value. Otherwise only the group ID matches request and
1493 needs_pasid = prm->flags & IOMMU_FAULT_PAGE_RESPONSE_NEEDS_PASID;
1494 if (needs_pasid && (!has_pasid || msg->pasid != prm->pasid))
1497 if (!needs_pasid && has_pasid) {
1498 /* No big deal, just clear it. */
1499 msg->flags &= ~IOMMU_PAGE_RESP_PASID_VALID;
1503 ret = ops->page_response(dev, evt, msg);
1504 list_del(&evt->list);
1510 mutex_unlock(¶m->fault_param->lock);
1513 EXPORT_SYMBOL_GPL(iommu_page_response);
1516 * iommu_group_id - Return ID for a group
1517 * @group: the group to ID
1519 * Return the unique ID for the group matching the sysfs group number.
1521 int iommu_group_id(struct iommu_group *group)
1525 EXPORT_SYMBOL_GPL(iommu_group_id);
1527 static struct iommu_group *get_pci_alias_group(struct pci_dev *pdev,
1528 unsigned long *devfns);
1531 * To consider a PCI device isolated, we require ACS to support Source
1532 * Validation, Request Redirection, Completer Redirection, and Upstream
1533 * Forwarding. This effectively means that devices cannot spoof their
1534 * requester ID, requests and completions cannot be redirected, and all
1535 * transactions are forwarded upstream, even as it passes through a
1536 * bridge where the target device is downstream.
1538 #define REQ_ACS_FLAGS (PCI_ACS_SV | PCI_ACS_RR | PCI_ACS_CR | PCI_ACS_UF)
1541 * For multifunction devices which are not isolated from each other, find
1542 * all the other non-isolated functions and look for existing groups. For
1543 * each function, we also need to look for aliases to or from other devices
1544 * that may already have a group.
1546 static struct iommu_group *get_pci_function_alias_group(struct pci_dev *pdev,
1547 unsigned long *devfns)
1549 struct pci_dev *tmp = NULL;
1550 struct iommu_group *group;
1552 if (!pdev->multifunction || pci_acs_enabled(pdev, REQ_ACS_FLAGS))
1555 for_each_pci_dev(tmp) {
1556 if (tmp == pdev || tmp->bus != pdev->bus ||
1557 PCI_SLOT(tmp->devfn) != PCI_SLOT(pdev->devfn) ||
1558 pci_acs_enabled(tmp, REQ_ACS_FLAGS))
1561 group = get_pci_alias_group(tmp, devfns);
1572 * Look for aliases to or from the given device for existing groups. DMA
1573 * aliases are only supported on the same bus, therefore the search
1574 * space is quite small (especially since we're really only looking at pcie
1575 * device, and therefore only expect multiple slots on the root complex or
1576 * downstream switch ports). It's conceivable though that a pair of
1577 * multifunction devices could have aliases between them that would cause a
1578 * loop. To prevent this, we use a bitmap to track where we've been.
1580 static struct iommu_group *get_pci_alias_group(struct pci_dev *pdev,
1581 unsigned long *devfns)
1583 struct pci_dev *tmp = NULL;
1584 struct iommu_group *group;
1586 if (test_and_set_bit(pdev->devfn & 0xff, devfns))
1589 group = iommu_group_get(&pdev->dev);
1593 for_each_pci_dev(tmp) {
1594 if (tmp == pdev || tmp->bus != pdev->bus)
1597 /* We alias them or they alias us */
1598 if (pci_devs_are_dma_aliases(pdev, tmp)) {
1599 group = get_pci_alias_group(tmp, devfns);
1605 group = get_pci_function_alias_group(tmp, devfns);
1616 struct group_for_pci_data {
1617 struct pci_dev *pdev;
1618 struct iommu_group *group;
1622 * DMA alias iterator callback, return the last seen device. Stop and return
1623 * the IOMMU group if we find one along the way.
1625 static int get_pci_alias_or_group(struct pci_dev *pdev, u16 alias, void *opaque)
1627 struct group_for_pci_data *data = opaque;
1630 data->group = iommu_group_get(&pdev->dev);
1632 return data->group != NULL;
1636 * Generic device_group call-back function. It just allocates one
1637 * iommu-group per device.
1639 struct iommu_group *generic_device_group(struct device *dev)
1641 return iommu_group_alloc();
1643 EXPORT_SYMBOL_GPL(generic_device_group);
1646 * Generic device_group call-back function. It just allocates one
1647 * iommu-group per iommu driver instance shared by every device
1648 * probed by that iommu driver.
1650 struct iommu_group *generic_single_device_group(struct device *dev)
1652 struct iommu_device *iommu = dev->iommu->iommu_dev;
1654 if (!iommu->singleton_group) {
1655 struct iommu_group *group;
1657 group = iommu_group_alloc();
1660 iommu->singleton_group = group;
1662 return iommu_group_ref_get(iommu->singleton_group);
1664 EXPORT_SYMBOL_GPL(generic_single_device_group);
1667 * Use standard PCI bus topology, isolation features, and DMA alias quirks
1668 * to find or create an IOMMU group for a device.
1670 struct iommu_group *pci_device_group(struct device *dev)
1672 struct pci_dev *pdev = to_pci_dev(dev);
1673 struct group_for_pci_data data;
1674 struct pci_bus *bus;
1675 struct iommu_group *group = NULL;
1676 u64 devfns[4] = { 0 };
1678 if (WARN_ON(!dev_is_pci(dev)))
1679 return ERR_PTR(-EINVAL);
1682 * Find the upstream DMA alias for the device. A device must not
1683 * be aliased due to topology in order to have its own IOMMU group.
1684 * If we find an alias along the way that already belongs to a
1687 if (pci_for_each_dma_alias(pdev, get_pci_alias_or_group, &data))
1693 * Continue upstream from the point of minimum IOMMU granularity
1694 * due to aliases to the point where devices are protected from
1695 * peer-to-peer DMA by PCI ACS. Again, if we find an existing
1698 for (bus = pdev->bus; !pci_is_root_bus(bus); bus = bus->parent) {
1702 if (pci_acs_path_enabled(bus->self, NULL, REQ_ACS_FLAGS))
1707 group = iommu_group_get(&pdev->dev);
1713 * Look for existing groups on device aliases. If we alias another
1714 * device or another device aliases us, use the same group.
1716 group = get_pci_alias_group(pdev, (unsigned long *)devfns);
1721 * Look for existing groups on non-isolated functions on the same
1722 * slot and aliases of those funcions, if any. No need to clear
1723 * the search bitmap, the tested devfns are still valid.
1725 group = get_pci_function_alias_group(pdev, (unsigned long *)devfns);
1729 /* No shared group found, allocate new */
1730 return iommu_group_alloc();
1732 EXPORT_SYMBOL_GPL(pci_device_group);
1734 /* Get the IOMMU group for device on fsl-mc bus */
1735 struct iommu_group *fsl_mc_device_group(struct device *dev)
1737 struct device *cont_dev = fsl_mc_cont_dev(dev);
1738 struct iommu_group *group;
1740 group = iommu_group_get(cont_dev);
1742 group = iommu_group_alloc();
1745 EXPORT_SYMBOL_GPL(fsl_mc_device_group);
1747 static struct iommu_domain *
1748 __iommu_group_alloc_default_domain(struct iommu_group *group, int req_type)
1750 if (group->default_domain && group->default_domain->type == req_type)
1751 return group->default_domain;
1752 return __iommu_group_domain_alloc(group, req_type);
1756 * Returns the iommu_ops for the devices in an iommu group.
1758 * It is assumed that all devices in an iommu group are managed by a single
1759 * IOMMU unit. Therefore, this returns the dev_iommu_ops of the first device
1762 static const struct iommu_ops *group_iommu_ops(struct iommu_group *group)
1764 struct group_device *device =
1765 list_first_entry(&group->devices, struct group_device, list);
1767 lockdep_assert_held(&group->mutex);
1769 return dev_iommu_ops(device->dev);
1773 * req_type of 0 means "auto" which means to select a domain based on
1774 * iommu_def_domain_type or what the driver actually supports.
1776 static struct iommu_domain *
1777 iommu_group_alloc_default_domain(struct iommu_group *group, int req_type)
1779 const struct iommu_ops *ops = group_iommu_ops(group);
1780 struct iommu_domain *dom;
1782 lockdep_assert_held(&group->mutex);
1785 * Allow legacy drivers to specify the domain that will be the default
1786 * domain. This should always be either an IDENTITY/BLOCKED/PLATFORM
1787 * domain. Do not use in new drivers.
1789 if (ops->default_domain) {
1792 return ops->default_domain;
1796 return __iommu_group_alloc_default_domain(group, req_type);
1798 /* The driver gave no guidance on what type to use, try the default */
1799 dom = __iommu_group_alloc_default_domain(group, iommu_def_domain_type);
1803 /* Otherwise IDENTITY and DMA_FQ defaults will try DMA */
1804 if (iommu_def_domain_type == IOMMU_DOMAIN_DMA)
1806 dom = __iommu_group_alloc_default_domain(group, IOMMU_DOMAIN_DMA);
1810 pr_warn("Failed to allocate default IOMMU domain of type %u for group %s - Falling back to IOMMU_DOMAIN_DMA",
1811 iommu_def_domain_type, group->name);
1815 struct iommu_domain *iommu_group_default_domain(struct iommu_group *group)
1817 return group->default_domain;
1820 static int probe_iommu_group(struct device *dev, void *data)
1822 struct list_head *group_list = data;
1825 ret = __iommu_probe_device(dev, group_list);
1832 static int iommu_bus_notifier(struct notifier_block *nb,
1833 unsigned long action, void *data)
1835 struct device *dev = data;
1837 if (action == BUS_NOTIFY_ADD_DEVICE) {
1840 ret = iommu_probe_device(dev);
1841 return (ret) ? NOTIFY_DONE : NOTIFY_OK;
1842 } else if (action == BUS_NOTIFY_REMOVED_DEVICE) {
1843 iommu_release_device(dev);
1851 * Combine the driver's chosen def_domain_type across all the devices in a
1852 * group. Drivers must give a consistent result.
1854 static int iommu_get_def_domain_type(struct iommu_group *group,
1855 struct device *dev, int cur_type)
1857 const struct iommu_ops *ops = group_iommu_ops(group);
1860 if (!ops->def_domain_type)
1863 type = ops->def_domain_type(dev);
1864 if (!type || cur_type == type)
1869 dev_err_ratelimited(
1871 "IOMMU driver error, requesting conflicting def_domain_type, %s and %s, for devices in group %u.\n",
1872 iommu_domain_type_str(cur_type), iommu_domain_type_str(type),
1876 * Try to recover, drivers are allowed to force IDENITY or DMA, IDENTITY
1879 if (type == IOMMU_DOMAIN_IDENTITY)
1885 * A target_type of 0 will select the best domain type. 0 can be returned in
1886 * this case meaning the global default should be used.
1888 static int iommu_get_default_domain_type(struct iommu_group *group,
1891 struct device *untrusted = NULL;
1892 struct group_device *gdev;
1893 int driver_type = 0;
1895 lockdep_assert_held(&group->mutex);
1898 * ARM32 drivers supporting CONFIG_ARM_DMA_USE_IOMMU can declare an
1899 * identity_domain and it will automatically become their default
1900 * domain. Later on ARM_DMA_USE_IOMMU will install its UNMANAGED domain.
1901 * Override the selection to IDENTITY.
1903 if (IS_ENABLED(CONFIG_ARM_DMA_USE_IOMMU)) {
1904 static_assert(!(IS_ENABLED(CONFIG_ARM_DMA_USE_IOMMU) &&
1905 IS_ENABLED(CONFIG_IOMMU_DMA)));
1906 driver_type = IOMMU_DOMAIN_IDENTITY;
1909 for_each_group_device(group, gdev) {
1910 driver_type = iommu_get_def_domain_type(group, gdev->dev,
1913 if (dev_is_pci(gdev->dev) && to_pci_dev(gdev->dev)->untrusted) {
1915 * No ARM32 using systems will set untrusted, it cannot
1918 if (WARN_ON(IS_ENABLED(CONFIG_ARM_DMA_USE_IOMMU)))
1920 untrusted = gdev->dev;
1925 * If the common dma ops are not selected in kconfig then we cannot use
1926 * IOMMU_DOMAIN_DMA at all. Force IDENTITY if nothing else has been
1929 if (!IS_ENABLED(CONFIG_IOMMU_DMA)) {
1930 if (WARN_ON(driver_type == IOMMU_DOMAIN_DMA))
1933 driver_type = IOMMU_DOMAIN_IDENTITY;
1937 if (driver_type && driver_type != IOMMU_DOMAIN_DMA) {
1938 dev_err_ratelimited(
1940 "Device is not trusted, but driver is overriding group %u to %s, refusing to probe.\n",
1941 group->id, iommu_domain_type_str(driver_type));
1944 driver_type = IOMMU_DOMAIN_DMA;
1948 if (driver_type && target_type != driver_type)
1955 static void iommu_group_do_probe_finalize(struct device *dev)
1957 const struct iommu_ops *ops = dev_iommu_ops(dev);
1959 if (ops->probe_finalize)
1960 ops->probe_finalize(dev);
1963 int bus_iommu_probe(const struct bus_type *bus)
1965 struct iommu_group *group, *next;
1966 LIST_HEAD(group_list);
1969 ret = bus_for_each_dev(bus, NULL, &group_list, probe_iommu_group);
1973 list_for_each_entry_safe(group, next, &group_list, entry) {
1974 struct group_device *gdev;
1976 mutex_lock(&group->mutex);
1978 /* Remove item from the list */
1979 list_del_init(&group->entry);
1982 * We go to the trouble of deferred default domain creation so
1983 * that the cross-group default domain type and the setup of the
1984 * IOMMU_RESV_DIRECT will work correctly in non-hotpug scenarios.
1986 ret = iommu_setup_default_domain(group, 0);
1988 mutex_unlock(&group->mutex);
1991 mutex_unlock(&group->mutex);
1994 * FIXME: Mis-locked because the ops->probe_finalize() call-back
1995 * of some IOMMU drivers calls arm_iommu_attach_device() which
1996 * in-turn might call back into IOMMU core code, where it tries
1997 * to take group->mutex, resulting in a deadlock.
1999 for_each_group_device(group, gdev)
2000 iommu_group_do_probe_finalize(gdev->dev);
2006 bool iommu_present(const struct bus_type *bus)
2008 return bus->iommu_ops != NULL;
2010 EXPORT_SYMBOL_GPL(iommu_present);
2013 * device_iommu_capable() - check for a general IOMMU capability
2014 * @dev: device to which the capability would be relevant, if available
2015 * @cap: IOMMU capability
2017 * Return: true if an IOMMU is present and supports the given capability
2018 * for the given device, otherwise false.
2020 bool device_iommu_capable(struct device *dev, enum iommu_cap cap)
2022 const struct iommu_ops *ops;
2024 if (!dev->iommu || !dev->iommu->iommu_dev)
2027 ops = dev_iommu_ops(dev);
2031 return ops->capable(dev, cap);
2033 EXPORT_SYMBOL_GPL(device_iommu_capable);
2036 * iommu_group_has_isolated_msi() - Compute msi_device_has_isolated_msi()
2038 * @group: Group to query
2040 * IOMMU groups should not have differing values of
2041 * msi_device_has_isolated_msi() for devices in a group. However nothing
2042 * directly prevents this, so ensure mistakes don't result in isolation failures
2043 * by checking that all the devices are the same.
2045 bool iommu_group_has_isolated_msi(struct iommu_group *group)
2047 struct group_device *group_dev;
2050 mutex_lock(&group->mutex);
2051 for_each_group_device(group, group_dev)
2052 ret &= msi_device_has_isolated_msi(group_dev->dev);
2053 mutex_unlock(&group->mutex);
2056 EXPORT_SYMBOL_GPL(iommu_group_has_isolated_msi);
2059 * iommu_set_fault_handler() - set a fault handler for an iommu domain
2060 * @domain: iommu domain
2061 * @handler: fault handler
2062 * @token: user data, will be passed back to the fault handler
2064 * This function should be used by IOMMU users which want to be notified
2065 * whenever an IOMMU fault happens.
2067 * The fault handler itself should return 0 on success, and an appropriate
2068 * error code otherwise.
2070 void iommu_set_fault_handler(struct iommu_domain *domain,
2071 iommu_fault_handler_t handler,
2076 domain->handler = handler;
2077 domain->handler_token = token;
2079 EXPORT_SYMBOL_GPL(iommu_set_fault_handler);
2081 static struct iommu_domain *__iommu_domain_alloc(const struct iommu_ops *ops,
2085 struct iommu_domain *domain;
2086 unsigned int alloc_type = type & IOMMU_DOMAIN_ALLOC_FLAGS;
2088 if (alloc_type == IOMMU_DOMAIN_IDENTITY && ops->identity_domain)
2089 return ops->identity_domain;
2090 else if (alloc_type == IOMMU_DOMAIN_BLOCKED && ops->blocked_domain)
2091 return ops->blocked_domain;
2092 else if (type & __IOMMU_DOMAIN_PAGING && ops->domain_alloc_paging)
2093 domain = ops->domain_alloc_paging(dev);
2094 else if (ops->domain_alloc)
2095 domain = ops->domain_alloc(alloc_type);
2102 domain->type = type;
2104 * If not already set, assume all sizes by default; the driver
2105 * may override this later
2107 if (!domain->pgsize_bitmap)
2108 domain->pgsize_bitmap = ops->pgsize_bitmap;
2111 domain->ops = ops->default_domain_ops;
2113 if (iommu_is_dma_domain(domain) && iommu_get_dma_cookie(domain)) {
2114 iommu_domain_free(domain);
2120 static struct iommu_domain *
2121 __iommu_group_domain_alloc(struct iommu_group *group, unsigned int type)
2123 struct device *dev =
2124 list_first_entry(&group->devices, struct group_device, list)
2127 return __iommu_domain_alloc(group_iommu_ops(group), dev, type);
2130 struct iommu_domain *iommu_domain_alloc(const struct bus_type *bus)
2132 if (bus == NULL || bus->iommu_ops == NULL)
2134 return __iommu_domain_alloc(bus->iommu_ops, NULL,
2135 IOMMU_DOMAIN_UNMANAGED);
2137 EXPORT_SYMBOL_GPL(iommu_domain_alloc);
2139 void iommu_domain_free(struct iommu_domain *domain)
2141 if (domain->type == IOMMU_DOMAIN_SVA)
2143 iommu_put_dma_cookie(domain);
2144 if (domain->ops->free)
2145 domain->ops->free(domain);
2147 EXPORT_SYMBOL_GPL(iommu_domain_free);
2150 * Put the group's domain back to the appropriate core-owned domain - either the
2151 * standard kernel-mode DMA configuration or an all-DMA-blocked domain.
2153 static void __iommu_group_set_core_domain(struct iommu_group *group)
2155 struct iommu_domain *new_domain;
2158 new_domain = group->blocking_domain;
2160 new_domain = group->default_domain;
2162 __iommu_group_set_domain_nofail(group, new_domain);
2165 static int __iommu_attach_device(struct iommu_domain *domain,
2170 if (unlikely(domain->ops->attach_dev == NULL))
2173 ret = domain->ops->attach_dev(domain, dev);
2176 dev->iommu->attach_deferred = 0;
2177 trace_attach_device_to_domain(dev);
2182 * iommu_attach_device - Attach an IOMMU domain to a device
2183 * @domain: IOMMU domain to attach
2184 * @dev: Device that will be attached
2186 * Returns 0 on success and error code on failure
2188 * Note that EINVAL can be treated as a soft failure, indicating
2189 * that certain configuration of the domain is incompatible with
2190 * the device. In this case attaching a different domain to the
2191 * device may succeed.
2193 int iommu_attach_device(struct iommu_domain *domain, struct device *dev)
2195 /* Caller must be a probed driver on dev */
2196 struct iommu_group *group = dev->iommu_group;
2203 * Lock the group to make sure the device-count doesn't
2204 * change while we are attaching
2206 mutex_lock(&group->mutex);
2208 if (list_count_nodes(&group->devices) != 1)
2211 ret = __iommu_attach_group(domain, group);
2214 mutex_unlock(&group->mutex);
2217 EXPORT_SYMBOL_GPL(iommu_attach_device);
2219 int iommu_deferred_attach(struct device *dev, struct iommu_domain *domain)
2221 if (dev->iommu && dev->iommu->attach_deferred)
2222 return __iommu_attach_device(domain, dev);
2227 void iommu_detach_device(struct iommu_domain *domain, struct device *dev)
2229 /* Caller must be a probed driver on dev */
2230 struct iommu_group *group = dev->iommu_group;
2235 mutex_lock(&group->mutex);
2236 if (WARN_ON(domain != group->domain) ||
2237 WARN_ON(list_count_nodes(&group->devices) != 1))
2239 __iommu_group_set_core_domain(group);
2242 mutex_unlock(&group->mutex);
2244 EXPORT_SYMBOL_GPL(iommu_detach_device);
2246 struct iommu_domain *iommu_get_domain_for_dev(struct device *dev)
2248 /* Caller must be a probed driver on dev */
2249 struct iommu_group *group = dev->iommu_group;
2254 return group->domain;
2256 EXPORT_SYMBOL_GPL(iommu_get_domain_for_dev);
2259 * For IOMMU_DOMAIN_DMA implementations which already provide their own
2260 * guarantees that the group and its default domain are valid and correct.
2262 struct iommu_domain *iommu_get_dma_domain(struct device *dev)
2264 return dev->iommu_group->default_domain;
2267 static int __iommu_attach_group(struct iommu_domain *domain,
2268 struct iommu_group *group)
2270 if (group->domain && group->domain != group->default_domain &&
2271 group->domain != group->blocking_domain)
2274 return __iommu_group_set_domain(group, domain);
2278 * iommu_attach_group - Attach an IOMMU domain to an IOMMU group
2279 * @domain: IOMMU domain to attach
2280 * @group: IOMMU group that will be attached
2282 * Returns 0 on success and error code on failure
2284 * Note that EINVAL can be treated as a soft failure, indicating
2285 * that certain configuration of the domain is incompatible with
2286 * the group. In this case attaching a different domain to the
2287 * group may succeed.
2289 int iommu_attach_group(struct iommu_domain *domain, struct iommu_group *group)
2293 mutex_lock(&group->mutex);
2294 ret = __iommu_attach_group(domain, group);
2295 mutex_unlock(&group->mutex);
2299 EXPORT_SYMBOL_GPL(iommu_attach_group);
2302 * iommu_group_replace_domain - replace the domain that a group is attached to
2303 * @new_domain: new IOMMU domain to replace with
2304 * @group: IOMMU group that will be attached to the new domain
2306 * This API allows the group to switch domains without being forced to go to
2307 * the blocking domain in-between.
2309 * If the currently attached domain is a core domain (e.g. a default_domain),
2310 * it will act just like the iommu_attach_group().
2312 int iommu_group_replace_domain(struct iommu_group *group,
2313 struct iommu_domain *new_domain)
2320 mutex_lock(&group->mutex);
2321 ret = __iommu_group_set_domain(group, new_domain);
2322 mutex_unlock(&group->mutex);
2325 EXPORT_SYMBOL_NS_GPL(iommu_group_replace_domain, IOMMUFD_INTERNAL);
2327 static int __iommu_device_set_domain(struct iommu_group *group,
2329 struct iommu_domain *new_domain,
2335 * If the device requires IOMMU_RESV_DIRECT then we cannot allow
2336 * the blocking domain to be attached as it does not contain the
2337 * required 1:1 mapping. This test effectively excludes the device
2338 * being used with iommu_group_claim_dma_owner() which will block
2339 * vfio and iommufd as well.
2341 if (dev->iommu->require_direct &&
2342 (new_domain->type == IOMMU_DOMAIN_BLOCKED ||
2343 new_domain == group->blocking_domain)) {
2345 "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");
2349 if (dev->iommu->attach_deferred) {
2350 if (new_domain == group->default_domain)
2352 dev->iommu->attach_deferred = 0;
2355 ret = __iommu_attach_device(new_domain, dev);
2358 * If we have a blocking domain then try to attach that in hopes
2359 * of avoiding a UAF. Modern drivers should implement blocking
2360 * domains as global statics that cannot fail.
2362 if ((flags & IOMMU_SET_DOMAIN_MUST_SUCCEED) &&
2363 group->blocking_domain &&
2364 group->blocking_domain != new_domain)
2365 __iommu_attach_device(group->blocking_domain, dev);
2372 * If 0 is returned the group's domain is new_domain. If an error is returned
2373 * then the group's domain will be set back to the existing domain unless
2374 * IOMMU_SET_DOMAIN_MUST_SUCCEED, otherwise an error is returned and the group's
2375 * domains is left inconsistent. This is a driver bug to fail attach with a
2376 * previously good domain. We try to avoid a kernel UAF because of this.
2378 * IOMMU groups are really the natural working unit of the IOMMU, but the IOMMU
2379 * API works on domains and devices. Bridge that gap by iterating over the
2380 * devices in a group. Ideally we'd have a single device which represents the
2381 * requestor ID of the group, but we also allow IOMMU drivers to create policy
2382 * defined minimum sets, where the physical hardware may be able to distiguish
2383 * members, but we wish to group them at a higher level (ex. untrusted
2384 * multi-function PCI devices). Thus we attach each device.
2386 static int __iommu_group_set_domain_internal(struct iommu_group *group,
2387 struct iommu_domain *new_domain,
2390 struct group_device *last_gdev;
2391 struct group_device *gdev;
2395 lockdep_assert_held(&group->mutex);
2397 if (group->domain == new_domain)
2400 if (WARN_ON(!new_domain))
2404 * Changing the domain is done by calling attach_dev() on the new
2405 * domain. This switch does not have to be atomic and DMA can be
2406 * discarded during the transition. DMA must only be able to access
2407 * either new_domain or group->domain, never something else.
2410 for_each_group_device(group, gdev) {
2411 ret = __iommu_device_set_domain(group, gdev->dev, new_domain,
2416 * Keep trying the other devices in the group. If a
2417 * driver fails attach to an otherwise good domain, and
2418 * does not support blocking domains, it should at least
2419 * drop its reference on the current domain so we don't
2422 if (flags & IOMMU_SET_DOMAIN_MUST_SUCCEED)
2427 group->domain = new_domain;
2432 * This is called in error unwind paths. A well behaved driver should
2433 * always allow us to attach to a domain that was already attached.
2436 for_each_group_device(group, gdev) {
2438 * A NULL domain can happen only for first probe, in which case
2439 * we leave group->domain as NULL and let release clean
2443 WARN_ON(__iommu_device_set_domain(
2444 group, gdev->dev, group->domain,
2445 IOMMU_SET_DOMAIN_MUST_SUCCEED));
2446 if (gdev == last_gdev)
2452 void iommu_detach_group(struct iommu_domain *domain, struct iommu_group *group)
2454 mutex_lock(&group->mutex);
2455 __iommu_group_set_core_domain(group);
2456 mutex_unlock(&group->mutex);
2458 EXPORT_SYMBOL_GPL(iommu_detach_group);
2460 phys_addr_t iommu_iova_to_phys(struct iommu_domain *domain, dma_addr_t iova)
2462 if (domain->type == IOMMU_DOMAIN_IDENTITY)
2465 if (domain->type == IOMMU_DOMAIN_BLOCKED)
2468 return domain->ops->iova_to_phys(domain, iova);
2470 EXPORT_SYMBOL_GPL(iommu_iova_to_phys);
2472 static size_t iommu_pgsize(struct iommu_domain *domain, unsigned long iova,
2473 phys_addr_t paddr, size_t size, size_t *count)
2475 unsigned int pgsize_idx, pgsize_idx_next;
2476 unsigned long pgsizes;
2477 size_t offset, pgsize, pgsize_next;
2478 unsigned long addr_merge = paddr | iova;
2480 /* Page sizes supported by the hardware and small enough for @size */
2481 pgsizes = domain->pgsize_bitmap & GENMASK(__fls(size), 0);
2483 /* Constrain the page sizes further based on the maximum alignment */
2484 if (likely(addr_merge))
2485 pgsizes &= GENMASK(__ffs(addr_merge), 0);
2487 /* Make sure we have at least one suitable page size */
2490 /* Pick the biggest page size remaining */
2491 pgsize_idx = __fls(pgsizes);
2492 pgsize = BIT(pgsize_idx);
2496 /* Find the next biggest support page size, if it exists */
2497 pgsizes = domain->pgsize_bitmap & ~GENMASK(pgsize_idx, 0);
2501 pgsize_idx_next = __ffs(pgsizes);
2502 pgsize_next = BIT(pgsize_idx_next);
2505 * There's no point trying a bigger page size unless the virtual
2506 * and physical addresses are similarly offset within the larger page.
2508 if ((iova ^ paddr) & (pgsize_next - 1))
2511 /* Calculate the offset to the next page size alignment boundary */
2512 offset = pgsize_next - (addr_merge & (pgsize_next - 1));
2515 * If size is big enough to accommodate the larger page, reduce
2516 * the number of smaller pages.
2518 if (offset + pgsize_next <= size)
2522 *count = size >> pgsize_idx;
2526 static int __iommu_map(struct iommu_domain *domain, unsigned long iova,
2527 phys_addr_t paddr, size_t size, int prot, gfp_t gfp)
2529 const struct iommu_domain_ops *ops = domain->ops;
2530 unsigned long orig_iova = iova;
2531 unsigned int min_pagesz;
2532 size_t orig_size = size;
2533 phys_addr_t orig_paddr = paddr;
2536 if (unlikely(!(domain->type & __IOMMU_DOMAIN_PAGING)))
2539 if (WARN_ON(!ops->map_pages || domain->pgsize_bitmap == 0UL))
2542 /* find out the minimum page size supported */
2543 min_pagesz = 1 << __ffs(domain->pgsize_bitmap);
2546 * both the virtual address and the physical one, as well as
2547 * the size of the mapping, must be aligned (at least) to the
2548 * size of the smallest page supported by the hardware
2550 if (!IS_ALIGNED(iova | paddr | size, min_pagesz)) {
2551 pr_err("unaligned: iova 0x%lx pa %pa size 0x%zx min_pagesz 0x%x\n",
2552 iova, &paddr, size, min_pagesz);
2556 pr_debug("map: iova 0x%lx pa %pa size 0x%zx\n", iova, &paddr, size);
2559 size_t pgsize, count, mapped = 0;
2561 pgsize = iommu_pgsize(domain, iova, paddr, size, &count);
2563 pr_debug("mapping: iova 0x%lx pa %pa pgsize 0x%zx count %zu\n",
2564 iova, &paddr, pgsize, count);
2565 ret = ops->map_pages(domain, iova, paddr, pgsize, count, prot,
2568 * Some pages may have been mapped, even if an error occurred,
2569 * so we should account for those so they can be unmapped.
2580 /* unroll mapping in case something went wrong */
2582 iommu_unmap(domain, orig_iova, orig_size - size);
2584 trace_map(orig_iova, orig_paddr, orig_size);
2589 int iommu_map(struct iommu_domain *domain, unsigned long iova,
2590 phys_addr_t paddr, size_t size, int prot, gfp_t gfp)
2592 const struct iommu_domain_ops *ops = domain->ops;
2595 might_sleep_if(gfpflags_allow_blocking(gfp));
2597 /* Discourage passing strange GFP flags */
2598 if (WARN_ON_ONCE(gfp & (__GFP_COMP | __GFP_DMA | __GFP_DMA32 |
2602 ret = __iommu_map(domain, iova, paddr, size, prot, gfp);
2603 if (ret == 0 && ops->iotlb_sync_map) {
2604 ret = ops->iotlb_sync_map(domain, iova, size);
2612 /* undo mappings already done */
2613 iommu_unmap(domain, iova, size);
2617 EXPORT_SYMBOL_GPL(iommu_map);
2619 static size_t __iommu_unmap(struct iommu_domain *domain,
2620 unsigned long iova, size_t size,
2621 struct iommu_iotlb_gather *iotlb_gather)
2623 const struct iommu_domain_ops *ops = domain->ops;
2624 size_t unmapped_page, unmapped = 0;
2625 unsigned long orig_iova = iova;
2626 unsigned int min_pagesz;
2628 if (unlikely(!(domain->type & __IOMMU_DOMAIN_PAGING)))
2631 if (WARN_ON(!ops->unmap_pages || domain->pgsize_bitmap == 0UL))
2634 /* find out the minimum page size supported */
2635 min_pagesz = 1 << __ffs(domain->pgsize_bitmap);
2638 * The virtual address, as well as the size of the mapping, must be
2639 * aligned (at least) to the size of the smallest page supported
2642 if (!IS_ALIGNED(iova | size, min_pagesz)) {
2643 pr_err("unaligned: iova 0x%lx size 0x%zx min_pagesz 0x%x\n",
2644 iova, size, min_pagesz);
2648 pr_debug("unmap this: iova 0x%lx size 0x%zx\n", iova, size);
2651 * Keep iterating until we either unmap 'size' bytes (or more)
2652 * or we hit an area that isn't mapped.
2654 while (unmapped < size) {
2655 size_t pgsize, count;
2657 pgsize = iommu_pgsize(domain, iova, iova, size - unmapped, &count);
2658 unmapped_page = ops->unmap_pages(domain, iova, pgsize, count, iotlb_gather);
2662 pr_debug("unmapped: iova 0x%lx size 0x%zx\n",
2663 iova, unmapped_page);
2665 iova += unmapped_page;
2666 unmapped += unmapped_page;
2669 trace_unmap(orig_iova, size, unmapped);
2673 size_t iommu_unmap(struct iommu_domain *domain,
2674 unsigned long iova, size_t size)
2676 struct iommu_iotlb_gather iotlb_gather;
2679 iommu_iotlb_gather_init(&iotlb_gather);
2680 ret = __iommu_unmap(domain, iova, size, &iotlb_gather);
2681 iommu_iotlb_sync(domain, &iotlb_gather);
2685 EXPORT_SYMBOL_GPL(iommu_unmap);
2687 size_t iommu_unmap_fast(struct iommu_domain *domain,
2688 unsigned long iova, size_t size,
2689 struct iommu_iotlb_gather *iotlb_gather)
2691 return __iommu_unmap(domain, iova, size, iotlb_gather);
2693 EXPORT_SYMBOL_GPL(iommu_unmap_fast);
2695 ssize_t iommu_map_sg(struct iommu_domain *domain, unsigned long iova,
2696 struct scatterlist *sg, unsigned int nents, int prot,
2699 const struct iommu_domain_ops *ops = domain->ops;
2700 size_t len = 0, mapped = 0;
2705 might_sleep_if(gfpflags_allow_blocking(gfp));
2707 /* Discourage passing strange GFP flags */
2708 if (WARN_ON_ONCE(gfp & (__GFP_COMP | __GFP_DMA | __GFP_DMA32 |
2712 while (i <= nents) {
2713 phys_addr_t s_phys = sg_phys(sg);
2715 if (len && s_phys != start + len) {
2716 ret = __iommu_map(domain, iova + mapped, start,
2726 if (sg_dma_is_bus_address(sg))
2741 if (ops->iotlb_sync_map) {
2742 ret = ops->iotlb_sync_map(domain, iova, mapped);
2749 /* undo mappings already done */
2750 iommu_unmap(domain, iova, mapped);
2754 EXPORT_SYMBOL_GPL(iommu_map_sg);
2757 * report_iommu_fault() - report about an IOMMU fault to the IOMMU framework
2758 * @domain: the iommu domain where the fault has happened
2759 * @dev: the device where the fault has happened
2760 * @iova: the faulting address
2761 * @flags: mmu fault flags (e.g. IOMMU_FAULT_READ/IOMMU_FAULT_WRITE/...)
2763 * This function should be called by the low-level IOMMU implementations
2764 * whenever IOMMU faults happen, to allow high-level users, that are
2765 * interested in such events, to know about them.
2767 * This event may be useful for several possible use cases:
2768 * - mere logging of the event
2769 * - dynamic TLB/PTE loading
2770 * - if restarting of the faulting device is required
2772 * Returns 0 on success and an appropriate error code otherwise (if dynamic
2773 * PTE/TLB loading will one day be supported, implementations will be able
2774 * to tell whether it succeeded or not according to this return value).
2776 * Specifically, -ENOSYS is returned if a fault handler isn't installed
2777 * (though fault handlers can also return -ENOSYS, in case they want to
2778 * elicit the default behavior of the IOMMU drivers).
2780 int report_iommu_fault(struct iommu_domain *domain, struct device *dev,
2781 unsigned long iova, int flags)
2786 * if upper layers showed interest and installed a fault handler,
2789 if (domain->handler)
2790 ret = domain->handler(domain, dev, iova, flags,
2791 domain->handler_token);
2793 trace_io_page_fault(dev, iova, flags);
2796 EXPORT_SYMBOL_GPL(report_iommu_fault);
2798 static int __init iommu_init(void)
2800 iommu_group_kset = kset_create_and_add("iommu_groups",
2802 BUG_ON(!iommu_group_kset);
2804 iommu_debugfs_setup();
2808 core_initcall(iommu_init);
2810 int iommu_enable_nesting(struct iommu_domain *domain)
2812 if (domain->type != IOMMU_DOMAIN_UNMANAGED)
2814 if (!domain->ops->enable_nesting)
2816 return domain->ops->enable_nesting(domain);
2818 EXPORT_SYMBOL_GPL(iommu_enable_nesting);
2820 int iommu_set_pgtable_quirks(struct iommu_domain *domain,
2821 unsigned long quirk)
2823 if (domain->type != IOMMU_DOMAIN_UNMANAGED)
2825 if (!domain->ops->set_pgtable_quirks)
2827 return domain->ops->set_pgtable_quirks(domain, quirk);
2829 EXPORT_SYMBOL_GPL(iommu_set_pgtable_quirks);
2832 * iommu_get_resv_regions - get reserved regions
2833 * @dev: device for which to get reserved regions
2834 * @list: reserved region list for device
2836 * This returns a list of reserved IOVA regions specific to this device.
2837 * A domain user should not map IOVA in these ranges.
2839 void iommu_get_resv_regions(struct device *dev, struct list_head *list)
2841 const struct iommu_ops *ops = dev_iommu_ops(dev);
2843 if (ops->get_resv_regions)
2844 ops->get_resv_regions(dev, list);
2846 EXPORT_SYMBOL_GPL(iommu_get_resv_regions);
2849 * iommu_put_resv_regions - release reserved regions
2850 * @dev: device for which to free reserved regions
2851 * @list: reserved region list for device
2853 * This releases a reserved region list acquired by iommu_get_resv_regions().
2855 void iommu_put_resv_regions(struct device *dev, struct list_head *list)
2857 struct iommu_resv_region *entry, *next;
2859 list_for_each_entry_safe(entry, next, list, list) {
2861 entry->free(dev, entry);
2866 EXPORT_SYMBOL(iommu_put_resv_regions);
2868 struct iommu_resv_region *iommu_alloc_resv_region(phys_addr_t start,
2869 size_t length, int prot,
2870 enum iommu_resv_type type,
2873 struct iommu_resv_region *region;
2875 region = kzalloc(sizeof(*region), gfp);
2879 INIT_LIST_HEAD(®ion->list);
2880 region->start = start;
2881 region->length = length;
2882 region->prot = prot;
2883 region->type = type;
2886 EXPORT_SYMBOL_GPL(iommu_alloc_resv_region);
2888 void iommu_set_default_passthrough(bool cmd_line)
2891 iommu_cmd_line |= IOMMU_CMD_LINE_DMA_API;
2892 iommu_def_domain_type = IOMMU_DOMAIN_IDENTITY;
2895 void iommu_set_default_translated(bool cmd_line)
2898 iommu_cmd_line |= IOMMU_CMD_LINE_DMA_API;
2899 iommu_def_domain_type = IOMMU_DOMAIN_DMA;
2902 bool iommu_default_passthrough(void)
2904 return iommu_def_domain_type == IOMMU_DOMAIN_IDENTITY;
2906 EXPORT_SYMBOL_GPL(iommu_default_passthrough);
2908 const struct iommu_ops *iommu_ops_from_fwnode(struct fwnode_handle *fwnode)
2910 const struct iommu_ops *ops = NULL;
2911 struct iommu_device *iommu;
2913 spin_lock(&iommu_device_lock);
2914 list_for_each_entry(iommu, &iommu_device_list, list)
2915 if (iommu->fwnode == fwnode) {
2919 spin_unlock(&iommu_device_lock);
2923 int iommu_fwspec_init(struct device *dev, struct fwnode_handle *iommu_fwnode,
2924 const struct iommu_ops *ops)
2926 struct iommu_fwspec *fwspec = dev_iommu_fwspec_get(dev);
2929 return ops == fwspec->ops ? 0 : -EINVAL;
2931 if (!dev_iommu_get(dev))
2934 /* Preallocate for the overwhelmingly common case of 1 ID */
2935 fwspec = kzalloc(struct_size(fwspec, ids, 1), GFP_KERNEL);
2939 of_node_get(to_of_node(iommu_fwnode));
2940 fwspec->iommu_fwnode = iommu_fwnode;
2942 dev_iommu_fwspec_set(dev, fwspec);
2945 EXPORT_SYMBOL_GPL(iommu_fwspec_init);
2947 void iommu_fwspec_free(struct device *dev)
2949 struct iommu_fwspec *fwspec = dev_iommu_fwspec_get(dev);
2952 fwnode_handle_put(fwspec->iommu_fwnode);
2954 dev_iommu_fwspec_set(dev, NULL);
2957 EXPORT_SYMBOL_GPL(iommu_fwspec_free);
2959 int iommu_fwspec_add_ids(struct device *dev, u32 *ids, int num_ids)
2961 struct iommu_fwspec *fwspec = dev_iommu_fwspec_get(dev);
2967 new_num = fwspec->num_ids + num_ids;
2969 fwspec = krealloc(fwspec, struct_size(fwspec, ids, new_num),
2974 dev_iommu_fwspec_set(dev, fwspec);
2977 for (i = 0; i < num_ids; i++)
2978 fwspec->ids[fwspec->num_ids + i] = ids[i];
2980 fwspec->num_ids = new_num;
2983 EXPORT_SYMBOL_GPL(iommu_fwspec_add_ids);
2986 * Per device IOMMU features.
2988 int iommu_dev_enable_feature(struct device *dev, enum iommu_dev_features feat)
2990 if (dev->iommu && dev->iommu->iommu_dev) {
2991 const struct iommu_ops *ops = dev->iommu->iommu_dev->ops;
2993 if (ops->dev_enable_feat)
2994 return ops->dev_enable_feat(dev, feat);
2999 EXPORT_SYMBOL_GPL(iommu_dev_enable_feature);
3002 * The device drivers should do the necessary cleanups before calling this.
3004 int iommu_dev_disable_feature(struct device *dev, enum iommu_dev_features feat)
3006 if (dev->iommu && dev->iommu->iommu_dev) {
3007 const struct iommu_ops *ops = dev->iommu->iommu_dev->ops;
3009 if (ops->dev_disable_feat)
3010 return ops->dev_disable_feat(dev, feat);
3015 EXPORT_SYMBOL_GPL(iommu_dev_disable_feature);
3018 * iommu_setup_default_domain - Set the default_domain for the group
3019 * @group: Group to change
3020 * @target_type: Domain type to set as the default_domain
3022 * Allocate a default domain and set it as the current domain on the group. If
3023 * the group already has a default domain it will be changed to the target_type.
3024 * When target_type is 0 the default domain is selected based on driver and
3025 * system preferences.
3027 static int iommu_setup_default_domain(struct iommu_group *group,
3030 struct iommu_domain *old_dom = group->default_domain;
3031 struct group_device *gdev;
3032 struct iommu_domain *dom;
3037 lockdep_assert_held(&group->mutex);
3039 req_type = iommu_get_default_domain_type(group, target_type);
3043 dom = iommu_group_alloc_default_domain(group, req_type);
3047 if (group->default_domain == dom)
3051 * IOMMU_RESV_DIRECT and IOMMU_RESV_DIRECT_RELAXABLE regions must be
3052 * mapped before their device is attached, in order to guarantee
3053 * continuity with any FW activity
3055 direct_failed = false;
3056 for_each_group_device(group, gdev) {
3057 if (iommu_create_device_direct_mappings(dom, gdev->dev)) {
3058 direct_failed = true;
3060 gdev->dev->iommu->iommu_dev->dev,
3061 "IOMMU driver was not able to establish FW requested direct mapping.");
3065 /* We must set default_domain early for __iommu_device_set_domain */
3066 group->default_domain = dom;
3067 if (!group->domain) {
3069 * Drivers are not allowed to fail the first domain attach.
3070 * The only way to recover from this is to fail attaching the
3071 * iommu driver and call ops->release_device. Put the domain
3072 * in group->default_domain so it is freed after.
3074 ret = __iommu_group_set_domain_internal(
3075 group, dom, IOMMU_SET_DOMAIN_MUST_SUCCEED);
3079 ret = __iommu_group_set_domain(group, dom);
3081 goto err_restore_def_domain;
3085 * Drivers are supposed to allow mappings to be installed in a domain
3086 * before device attachment, but some don't. Hack around this defect by
3087 * trying again after attaching. If this happens it means the device
3088 * will not continuously have the IOMMU_RESV_DIRECT map.
3090 if (direct_failed) {
3091 for_each_group_device(group, gdev) {
3092 ret = iommu_create_device_direct_mappings(dom, gdev->dev);
3094 goto err_restore_domain;
3100 iommu_domain_free(old_dom);
3105 __iommu_group_set_domain_internal(
3106 group, old_dom, IOMMU_SET_DOMAIN_MUST_SUCCEED);
3107 err_restore_def_domain:
3109 iommu_domain_free(dom);
3110 group->default_domain = old_dom;
3116 * Changing the default domain through sysfs requires the users to unbind the
3117 * drivers from the devices in the iommu group, except for a DMA -> DMA-FQ
3118 * transition. Return failure if this isn't met.
3120 * We need to consider the race between this and the device release path.
3121 * group->mutex is used here to guarantee that the device release path
3122 * will not be entered at the same time.
3124 static ssize_t iommu_group_store_type(struct iommu_group *group,
3125 const char *buf, size_t count)
3127 struct group_device *gdev;
3130 if (!capable(CAP_SYS_ADMIN) || !capable(CAP_SYS_RAWIO))
3133 if (WARN_ON(!group) || !group->default_domain)
3136 if (sysfs_streq(buf, "identity"))
3137 req_type = IOMMU_DOMAIN_IDENTITY;
3138 else if (sysfs_streq(buf, "DMA"))
3139 req_type = IOMMU_DOMAIN_DMA;
3140 else if (sysfs_streq(buf, "DMA-FQ"))
3141 req_type = IOMMU_DOMAIN_DMA_FQ;
3142 else if (sysfs_streq(buf, "auto"))
3147 mutex_lock(&group->mutex);
3148 /* We can bring up a flush queue without tearing down the domain. */
3149 if (req_type == IOMMU_DOMAIN_DMA_FQ &&
3150 group->default_domain->type == IOMMU_DOMAIN_DMA) {
3151 ret = iommu_dma_init_fq(group->default_domain);
3155 group->default_domain->type = IOMMU_DOMAIN_DMA_FQ;
3160 /* Otherwise, ensure that device exists and no driver is bound. */
3161 if (list_empty(&group->devices) || group->owner_cnt) {
3166 ret = iommu_setup_default_domain(group, req_type);
3171 * Release the mutex here because ops->probe_finalize() call-back of
3172 * some vendor IOMMU drivers calls arm_iommu_attach_device() which
3173 * in-turn might call back into IOMMU core code, where it tries to take
3174 * group->mutex, resulting in a deadlock.
3176 mutex_unlock(&group->mutex);
3178 /* Make sure dma_ops is appropriatley set */
3179 for_each_group_device(group, gdev)
3180 iommu_group_do_probe_finalize(gdev->dev);
3184 mutex_unlock(&group->mutex);
3185 return ret ?: count;
3189 * iommu_device_use_default_domain() - Device driver wants to handle device
3190 * DMA through the kernel DMA API.
3193 * The device driver about to bind @dev wants to do DMA through the kernel
3194 * DMA API. Return 0 if it is allowed, otherwise an error.
3196 int iommu_device_use_default_domain(struct device *dev)
3198 /* Caller is the driver core during the pre-probe path */
3199 struct iommu_group *group = dev->iommu_group;
3205 mutex_lock(&group->mutex);
3206 if (group->owner_cnt) {
3207 if (group->domain != group->default_domain || group->owner ||
3208 !xa_empty(&group->pasid_array)) {
3217 mutex_unlock(&group->mutex);
3222 * iommu_device_unuse_default_domain() - Device driver stops handling device
3223 * DMA through the kernel DMA API.
3226 * The device driver doesn't want to do DMA through kernel DMA API anymore.
3227 * It must be called after iommu_device_use_default_domain().
3229 void iommu_device_unuse_default_domain(struct device *dev)
3231 /* Caller is the driver core during the post-probe path */
3232 struct iommu_group *group = dev->iommu_group;
3237 mutex_lock(&group->mutex);
3238 if (!WARN_ON(!group->owner_cnt || !xa_empty(&group->pasid_array)))
3241 mutex_unlock(&group->mutex);
3244 static int __iommu_group_alloc_blocking_domain(struct iommu_group *group)
3246 if (group->blocking_domain)
3249 group->blocking_domain =
3250 __iommu_group_domain_alloc(group, IOMMU_DOMAIN_BLOCKED);
3251 if (!group->blocking_domain) {
3253 * For drivers that do not yet understand IOMMU_DOMAIN_BLOCKED
3254 * create an empty domain instead.
3256 group->blocking_domain = __iommu_group_domain_alloc(
3257 group, IOMMU_DOMAIN_UNMANAGED);
3258 if (!group->blocking_domain)
3264 static int __iommu_take_dma_ownership(struct iommu_group *group, void *owner)
3268 if ((group->domain && group->domain != group->default_domain) ||
3269 !xa_empty(&group->pasid_array))
3272 ret = __iommu_group_alloc_blocking_domain(group);
3275 ret = __iommu_group_set_domain(group, group->blocking_domain);
3279 group->owner = owner;
3285 * iommu_group_claim_dma_owner() - Set DMA ownership of a group
3286 * @group: The group.
3287 * @owner: Caller specified pointer. Used for exclusive ownership.
3289 * This is to support backward compatibility for vfio which manages the dma
3290 * ownership in iommu_group level. New invocations on this interface should be
3291 * prohibited. Only a single owner may exist for a group.
3293 int iommu_group_claim_dma_owner(struct iommu_group *group, void *owner)
3297 if (WARN_ON(!owner))
3300 mutex_lock(&group->mutex);
3301 if (group->owner_cnt) {
3306 ret = __iommu_take_dma_ownership(group, owner);
3308 mutex_unlock(&group->mutex);
3312 EXPORT_SYMBOL_GPL(iommu_group_claim_dma_owner);
3315 * iommu_device_claim_dma_owner() - Set DMA ownership of a device
3317 * @owner: Caller specified pointer. Used for exclusive ownership.
3319 * Claim the DMA ownership of a device. Multiple devices in the same group may
3320 * concurrently claim ownership if they present the same owner value. Returns 0
3321 * on success and error code on failure
3323 int iommu_device_claim_dma_owner(struct device *dev, void *owner)
3325 /* Caller must be a probed driver on dev */
3326 struct iommu_group *group = dev->iommu_group;
3329 if (WARN_ON(!owner))
3335 mutex_lock(&group->mutex);
3336 if (group->owner_cnt) {
3337 if (group->owner != owner) {
3345 ret = __iommu_take_dma_ownership(group, owner);
3347 mutex_unlock(&group->mutex);
3350 EXPORT_SYMBOL_GPL(iommu_device_claim_dma_owner);
3352 static void __iommu_release_dma_ownership(struct iommu_group *group)
3354 if (WARN_ON(!group->owner_cnt || !group->owner ||
3355 !xa_empty(&group->pasid_array)))
3358 group->owner_cnt = 0;
3359 group->owner = NULL;
3360 __iommu_group_set_domain_nofail(group, group->default_domain);
3364 * iommu_group_release_dma_owner() - Release DMA ownership of a group
3367 * Release the DMA ownership claimed by iommu_group_claim_dma_owner().
3369 void iommu_group_release_dma_owner(struct iommu_group *group)
3371 mutex_lock(&group->mutex);
3372 __iommu_release_dma_ownership(group);
3373 mutex_unlock(&group->mutex);
3375 EXPORT_SYMBOL_GPL(iommu_group_release_dma_owner);
3378 * iommu_device_release_dma_owner() - Release DMA ownership of a device
3381 * Release the DMA ownership claimed by iommu_device_claim_dma_owner().
3383 void iommu_device_release_dma_owner(struct device *dev)
3385 /* Caller must be a probed driver on dev */
3386 struct iommu_group *group = dev->iommu_group;
3388 mutex_lock(&group->mutex);
3389 if (group->owner_cnt > 1)
3392 __iommu_release_dma_ownership(group);
3393 mutex_unlock(&group->mutex);
3395 EXPORT_SYMBOL_GPL(iommu_device_release_dma_owner);
3398 * iommu_group_dma_owner_claimed() - Query group dma ownership status
3399 * @group: The group.
3401 * This provides status query on a given group. It is racy and only for
3402 * non-binding status reporting.
3404 bool iommu_group_dma_owner_claimed(struct iommu_group *group)
3408 mutex_lock(&group->mutex);
3409 user = group->owner_cnt;
3410 mutex_unlock(&group->mutex);
3414 EXPORT_SYMBOL_GPL(iommu_group_dma_owner_claimed);
3416 static int __iommu_set_group_pasid(struct iommu_domain *domain,
3417 struct iommu_group *group, ioasid_t pasid)
3419 struct group_device *device;
3422 for_each_group_device(group, device) {
3423 ret = domain->ops->set_dev_pasid(domain, device->dev, pasid);
3431 static void __iommu_remove_group_pasid(struct iommu_group *group,
3434 struct group_device *device;
3435 const struct iommu_ops *ops;
3437 for_each_group_device(group, device) {
3438 ops = dev_iommu_ops(device->dev);
3439 ops->remove_dev_pasid(device->dev, pasid);
3444 * iommu_attach_device_pasid() - Attach a domain to pasid of device
3445 * @domain: the iommu domain.
3446 * @dev: the attached device.
3447 * @pasid: the pasid of the device.
3449 * Return: 0 on success, or an error.
3451 int iommu_attach_device_pasid(struct iommu_domain *domain,
3452 struct device *dev, ioasid_t pasid)
3454 /* Caller must be a probed driver on dev */
3455 struct iommu_group *group = dev->iommu_group;
3459 if (!domain->ops->set_dev_pasid)
3465 mutex_lock(&group->mutex);
3466 curr = xa_cmpxchg(&group->pasid_array, pasid, NULL, domain, GFP_KERNEL);
3468 ret = xa_err(curr) ? : -EBUSY;
3472 ret = __iommu_set_group_pasid(domain, group, pasid);
3474 __iommu_remove_group_pasid(group, pasid);
3475 xa_erase(&group->pasid_array, pasid);
3478 mutex_unlock(&group->mutex);
3481 EXPORT_SYMBOL_GPL(iommu_attach_device_pasid);
3484 * iommu_detach_device_pasid() - Detach the domain from pasid of device
3485 * @domain: the iommu domain.
3486 * @dev: the attached device.
3487 * @pasid: the pasid of the device.
3489 * The @domain must have been attached to @pasid of the @dev with
3490 * iommu_attach_device_pasid().
3492 void iommu_detach_device_pasid(struct iommu_domain *domain, struct device *dev,
3495 /* Caller must be a probed driver on dev */
3496 struct iommu_group *group = dev->iommu_group;
3498 mutex_lock(&group->mutex);
3499 __iommu_remove_group_pasid(group, pasid);
3500 WARN_ON(xa_erase(&group->pasid_array, pasid) != domain);
3501 mutex_unlock(&group->mutex);
3503 EXPORT_SYMBOL_GPL(iommu_detach_device_pasid);
3506 * iommu_get_domain_for_dev_pasid() - Retrieve domain for @pasid of @dev
3507 * @dev: the queried device
3508 * @pasid: the pasid of the device
3509 * @type: matched domain type, 0 for any match
3511 * This is a variant of iommu_get_domain_for_dev(). It returns the existing
3512 * domain attached to pasid of a device. Callers must hold a lock around this
3513 * function, and both iommu_attach/detach_dev_pasid() whenever a domain of
3514 * type is being manipulated. This API does not internally resolve races with
3517 * Return: attached domain on success, NULL otherwise.
3519 struct iommu_domain *iommu_get_domain_for_dev_pasid(struct device *dev,
3523 /* Caller must be a probed driver on dev */
3524 struct iommu_group *group = dev->iommu_group;
3525 struct iommu_domain *domain;
3530 xa_lock(&group->pasid_array);
3531 domain = xa_load(&group->pasid_array, pasid);
3532 if (type && domain && domain->type != type)
3533 domain = ERR_PTR(-EBUSY);
3534 xa_unlock(&group->pasid_array);
3538 EXPORT_SYMBOL_GPL(iommu_get_domain_for_dev_pasid);
3540 struct iommu_domain *iommu_sva_domain_alloc(struct device *dev,
3541 struct mm_struct *mm)
3543 const struct iommu_ops *ops = dev_iommu_ops(dev);
3544 struct iommu_domain *domain;
3546 domain = ops->domain_alloc(IOMMU_DOMAIN_SVA);
3550 domain->type = IOMMU_DOMAIN_SVA;
3553 domain->iopf_handler = iommu_sva_handle_iopf;
3554 domain->fault_data = mm;
3559 ioasid_t iommu_alloc_global_pasid(struct device *dev)
3563 /* max_pasids == 0 means that the device does not support PASID */
3564 if (!dev->iommu->max_pasids)
3565 return IOMMU_PASID_INVALID;
3568 * max_pasids is set up by vendor driver based on number of PASID bits
3569 * supported but the IDA allocation is inclusive.
3571 ret = ida_alloc_range(&iommu_global_pasid_ida, IOMMU_FIRST_GLOBAL_PASID,
3572 dev->iommu->max_pasids - 1, GFP_KERNEL);
3573 return ret < 0 ? IOMMU_PASID_INVALID : ret;
3575 EXPORT_SYMBOL_GPL(iommu_alloc_global_pasid);
3577 void iommu_free_global_pasid(ioasid_t pasid)
3579 if (WARN_ON(pasid == IOMMU_PASID_INVALID))
3582 ida_free(&iommu_global_pasid_ida, pasid);
3584 EXPORT_SYMBOL_GPL(iommu_free_global_pasid);