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/device.h>
10 #include <linux/kernel.h>
11 #include <linux/bug.h>
12 #include <linux/types.h>
13 #include <linux/init.h>
14 #include <linux/export.h>
15 #include <linux/slab.h>
16 #include <linux/errno.h>
17 #include <linux/iommu.h>
18 #include <linux/idr.h>
19 #include <linux/notifier.h>
20 #include <linux/err.h>
21 #include <linux/pci.h>
22 #include <linux/bitops.h>
23 #include <linux/property.h>
24 #include <linux/fsl/mc.h>
25 #include <trace/events/iommu.h>
27 static struct kset *iommu_group_kset;
28 static DEFINE_IDA(iommu_group_ida);
29 #ifdef CONFIG_IOMMU_DEFAULT_PASSTHROUGH
30 static unsigned int iommu_def_domain_type = IOMMU_DOMAIN_IDENTITY;
32 static unsigned int iommu_def_domain_type = IOMMU_DOMAIN_DMA;
34 static bool iommu_dma_strict __read_mostly = true;
38 struct kobject *devices_kobj;
39 struct list_head devices;
41 struct blocking_notifier_head notifier;
43 void (*iommu_data_release)(void *iommu_data);
46 struct iommu_domain *default_domain;
47 struct iommu_domain *domain;
51 struct list_head list;
56 struct iommu_group_attribute {
57 struct attribute attr;
58 ssize_t (*show)(struct iommu_group *group, char *buf);
59 ssize_t (*store)(struct iommu_group *group,
60 const char *buf, size_t count);
63 static const char * const iommu_group_resv_type_string[] = {
64 [IOMMU_RESV_DIRECT] = "direct",
65 [IOMMU_RESV_RESERVED] = "reserved",
66 [IOMMU_RESV_MSI] = "msi",
67 [IOMMU_RESV_SW_MSI] = "msi",
70 #define IOMMU_GROUP_ATTR(_name, _mode, _show, _store) \
71 struct iommu_group_attribute iommu_group_attr_##_name = \
72 __ATTR(_name, _mode, _show, _store)
74 #define to_iommu_group_attr(_attr) \
75 container_of(_attr, struct iommu_group_attribute, attr)
76 #define to_iommu_group(_kobj) \
77 container_of(_kobj, struct iommu_group, kobj)
79 static LIST_HEAD(iommu_device_list);
80 static DEFINE_SPINLOCK(iommu_device_lock);
82 int iommu_device_register(struct iommu_device *iommu)
84 spin_lock(&iommu_device_lock);
85 list_add_tail(&iommu->list, &iommu_device_list);
86 spin_unlock(&iommu_device_lock);
91 void iommu_device_unregister(struct iommu_device *iommu)
93 spin_lock(&iommu_device_lock);
94 list_del(&iommu->list);
95 spin_unlock(&iommu_device_lock);
98 int iommu_probe_device(struct device *dev)
100 const struct iommu_ops *ops = dev->bus->iommu_ops;
103 WARN_ON(dev->iommu_group);
106 ret = ops->add_device(dev);
111 void iommu_release_device(struct device *dev)
113 const struct iommu_ops *ops = dev->bus->iommu_ops;
115 if (dev->iommu_group)
116 ops->remove_device(dev);
119 static struct iommu_domain *__iommu_domain_alloc(struct bus_type *bus,
121 static int __iommu_attach_device(struct iommu_domain *domain,
123 static int __iommu_attach_group(struct iommu_domain *domain,
124 struct iommu_group *group);
125 static void __iommu_detach_group(struct iommu_domain *domain,
126 struct iommu_group *group);
128 static int __init iommu_set_def_domain_type(char *str)
133 ret = kstrtobool(str, &pt);
137 iommu_def_domain_type = pt ? IOMMU_DOMAIN_IDENTITY : IOMMU_DOMAIN_DMA;
140 early_param("iommu.passthrough", iommu_set_def_domain_type);
142 static int __init iommu_dma_setup(char *str)
144 return kstrtobool(str, &iommu_dma_strict);
146 early_param("iommu.strict", iommu_dma_setup);
148 static ssize_t iommu_group_attr_show(struct kobject *kobj,
149 struct attribute *__attr, char *buf)
151 struct iommu_group_attribute *attr = to_iommu_group_attr(__attr);
152 struct iommu_group *group = to_iommu_group(kobj);
156 ret = attr->show(group, buf);
160 static ssize_t iommu_group_attr_store(struct kobject *kobj,
161 struct attribute *__attr,
162 const char *buf, size_t count)
164 struct iommu_group_attribute *attr = to_iommu_group_attr(__attr);
165 struct iommu_group *group = to_iommu_group(kobj);
169 ret = attr->store(group, buf, count);
173 static const struct sysfs_ops iommu_group_sysfs_ops = {
174 .show = iommu_group_attr_show,
175 .store = iommu_group_attr_store,
178 static int iommu_group_create_file(struct iommu_group *group,
179 struct iommu_group_attribute *attr)
181 return sysfs_create_file(&group->kobj, &attr->attr);
184 static void iommu_group_remove_file(struct iommu_group *group,
185 struct iommu_group_attribute *attr)
187 sysfs_remove_file(&group->kobj, &attr->attr);
190 static ssize_t iommu_group_show_name(struct iommu_group *group, char *buf)
192 return sprintf(buf, "%s\n", group->name);
196 * iommu_insert_resv_region - Insert a new region in the
197 * list of reserved regions.
198 * @new: new region to insert
199 * @regions: list of regions
201 * The new element is sorted by address with respect to the other
202 * regions of the same type. In case it overlaps with another
203 * region of the same type, regions are merged. In case it
204 * overlaps with another region of different type, regions are
207 static int iommu_insert_resv_region(struct iommu_resv_region *new,
208 struct list_head *regions)
210 struct iommu_resv_region *region;
211 phys_addr_t start = new->start;
212 phys_addr_t end = new->start + new->length - 1;
213 struct list_head *pos = regions->next;
215 while (pos != regions) {
216 struct iommu_resv_region *entry =
217 list_entry(pos, struct iommu_resv_region, list);
218 phys_addr_t a = entry->start;
219 phys_addr_t b = entry->start + entry->length - 1;
220 int type = entry->type;
224 } else if (start > b) {
226 } else if ((start >= a) && (end <= b)) {
227 if (new->type == type)
232 if (new->type == type) {
233 phys_addr_t new_start = min(a, start);
234 phys_addr_t new_end = max(b, end);
236 list_del(&entry->list);
237 entry->start = new_start;
238 entry->length = new_end - new_start + 1;
239 iommu_insert_resv_region(entry, regions);
246 region = iommu_alloc_resv_region(new->start, new->length,
247 new->prot, new->type);
251 list_add_tail(®ion->list, pos);
257 iommu_insert_device_resv_regions(struct list_head *dev_resv_regions,
258 struct list_head *group_resv_regions)
260 struct iommu_resv_region *entry;
263 list_for_each_entry(entry, dev_resv_regions, list) {
264 ret = iommu_insert_resv_region(entry, group_resv_regions);
271 int iommu_get_group_resv_regions(struct iommu_group *group,
272 struct list_head *head)
274 struct group_device *device;
277 mutex_lock(&group->mutex);
278 list_for_each_entry(device, &group->devices, list) {
279 struct list_head dev_resv_regions;
281 INIT_LIST_HEAD(&dev_resv_regions);
282 iommu_get_resv_regions(device->dev, &dev_resv_regions);
283 ret = iommu_insert_device_resv_regions(&dev_resv_regions, head);
284 iommu_put_resv_regions(device->dev, &dev_resv_regions);
288 mutex_unlock(&group->mutex);
291 EXPORT_SYMBOL_GPL(iommu_get_group_resv_regions);
293 static ssize_t iommu_group_show_resv_regions(struct iommu_group *group,
296 struct iommu_resv_region *region, *next;
297 struct list_head group_resv_regions;
300 INIT_LIST_HEAD(&group_resv_regions);
301 iommu_get_group_resv_regions(group, &group_resv_regions);
303 list_for_each_entry_safe(region, next, &group_resv_regions, list) {
304 str += sprintf(str, "0x%016llx 0x%016llx %s\n",
305 (long long int)region->start,
306 (long long int)(region->start +
308 iommu_group_resv_type_string[region->type]);
315 static ssize_t iommu_group_show_type(struct iommu_group *group,
318 char *type = "unknown\n";
320 if (group->default_domain) {
321 switch (group->default_domain->type) {
322 case IOMMU_DOMAIN_BLOCKED:
325 case IOMMU_DOMAIN_IDENTITY:
328 case IOMMU_DOMAIN_UNMANAGED:
329 type = "unmanaged\n";
331 case IOMMU_DOMAIN_DMA:
341 static IOMMU_GROUP_ATTR(name, S_IRUGO, iommu_group_show_name, NULL);
343 static IOMMU_GROUP_ATTR(reserved_regions, 0444,
344 iommu_group_show_resv_regions, NULL);
346 static IOMMU_GROUP_ATTR(type, 0444, iommu_group_show_type, NULL);
348 static void iommu_group_release(struct kobject *kobj)
350 struct iommu_group *group = to_iommu_group(kobj);
352 pr_debug("Releasing group %d\n", group->id);
354 if (group->iommu_data_release)
355 group->iommu_data_release(group->iommu_data);
357 ida_simple_remove(&iommu_group_ida, group->id);
359 if (group->default_domain)
360 iommu_domain_free(group->default_domain);
366 static struct kobj_type iommu_group_ktype = {
367 .sysfs_ops = &iommu_group_sysfs_ops,
368 .release = iommu_group_release,
372 * iommu_group_alloc - Allocate a new group
374 * This function is called by an iommu driver to allocate a new iommu
375 * group. The iommu group represents the minimum granularity of the iommu.
376 * Upon successful return, the caller holds a reference to the supplied
377 * group in order to hold the group until devices are added. Use
378 * iommu_group_put() to release this extra reference count, allowing the
379 * group to be automatically reclaimed once it has no devices or external
382 struct iommu_group *iommu_group_alloc(void)
384 struct iommu_group *group;
387 group = kzalloc(sizeof(*group), GFP_KERNEL);
389 return ERR_PTR(-ENOMEM);
391 group->kobj.kset = iommu_group_kset;
392 mutex_init(&group->mutex);
393 INIT_LIST_HEAD(&group->devices);
394 BLOCKING_INIT_NOTIFIER_HEAD(&group->notifier);
396 ret = ida_simple_get(&iommu_group_ida, 0, 0, GFP_KERNEL);
403 ret = kobject_init_and_add(&group->kobj, &iommu_group_ktype,
404 NULL, "%d", group->id);
406 ida_simple_remove(&iommu_group_ida, group->id);
411 group->devices_kobj = kobject_create_and_add("devices", &group->kobj);
412 if (!group->devices_kobj) {
413 kobject_put(&group->kobj); /* triggers .release & free */
414 return ERR_PTR(-ENOMEM);
418 * The devices_kobj holds a reference on the group kobject, so
419 * as long as that exists so will the group. We can therefore
420 * use the devices_kobj for reference counting.
422 kobject_put(&group->kobj);
424 ret = iommu_group_create_file(group,
425 &iommu_group_attr_reserved_regions);
429 ret = iommu_group_create_file(group, &iommu_group_attr_type);
433 pr_debug("Allocated group %d\n", group->id);
437 EXPORT_SYMBOL_GPL(iommu_group_alloc);
439 struct iommu_group *iommu_group_get_by_id(int id)
441 struct kobject *group_kobj;
442 struct iommu_group *group;
445 if (!iommu_group_kset)
448 name = kasprintf(GFP_KERNEL, "%d", id);
452 group_kobj = kset_find_obj(iommu_group_kset, name);
458 group = container_of(group_kobj, struct iommu_group, kobj);
459 BUG_ON(group->id != id);
461 kobject_get(group->devices_kobj);
462 kobject_put(&group->kobj);
466 EXPORT_SYMBOL_GPL(iommu_group_get_by_id);
469 * iommu_group_get_iommudata - retrieve iommu_data registered for a group
472 * iommu drivers can store data in the group for use when doing iommu
473 * operations. This function provides a way to retrieve it. Caller
474 * should hold a group reference.
476 void *iommu_group_get_iommudata(struct iommu_group *group)
478 return group->iommu_data;
480 EXPORT_SYMBOL_GPL(iommu_group_get_iommudata);
483 * iommu_group_set_iommudata - set iommu_data for a group
485 * @iommu_data: new data
486 * @release: release function for iommu_data
488 * iommu drivers can store data in the group for use when doing iommu
489 * operations. This function provides a way to set the data after
490 * the group has been allocated. Caller should hold a group reference.
492 void iommu_group_set_iommudata(struct iommu_group *group, void *iommu_data,
493 void (*release)(void *iommu_data))
495 group->iommu_data = iommu_data;
496 group->iommu_data_release = release;
498 EXPORT_SYMBOL_GPL(iommu_group_set_iommudata);
501 * iommu_group_set_name - set name for a group
505 * Allow iommu driver to set a name for a group. When set it will
506 * appear in a name attribute file under the group in sysfs.
508 int iommu_group_set_name(struct iommu_group *group, const char *name)
513 iommu_group_remove_file(group, &iommu_group_attr_name);
520 group->name = kstrdup(name, GFP_KERNEL);
524 ret = iommu_group_create_file(group, &iommu_group_attr_name);
533 EXPORT_SYMBOL_GPL(iommu_group_set_name);
535 static int iommu_group_create_direct_mappings(struct iommu_group *group,
538 struct iommu_domain *domain = group->default_domain;
539 struct iommu_resv_region *entry;
540 struct list_head mappings;
541 unsigned long pg_size;
544 if (!domain || domain->type != IOMMU_DOMAIN_DMA)
547 BUG_ON(!domain->pgsize_bitmap);
549 pg_size = 1UL << __ffs(domain->pgsize_bitmap);
550 INIT_LIST_HEAD(&mappings);
552 iommu_get_resv_regions(dev, &mappings);
554 /* We need to consider overlapping regions for different devices */
555 list_for_each_entry(entry, &mappings, list) {
556 dma_addr_t start, end, addr;
558 if (domain->ops->apply_resv_region)
559 domain->ops->apply_resv_region(dev, domain, entry);
561 start = ALIGN(entry->start, pg_size);
562 end = ALIGN(entry->start + entry->length, pg_size);
564 if (entry->type != IOMMU_RESV_DIRECT)
567 for (addr = start; addr < end; addr += pg_size) {
568 phys_addr_t phys_addr;
570 phys_addr = iommu_iova_to_phys(domain, addr);
574 ret = iommu_map(domain, addr, addr, pg_size, entry->prot);
581 iommu_flush_tlb_all(domain);
584 iommu_put_resv_regions(dev, &mappings);
590 * iommu_group_add_device - add a device to an iommu group
591 * @group: the group into which to add the device (reference should be held)
594 * This function is called by an iommu driver to add a device into a
595 * group. Adding a device increments the group reference count.
597 int iommu_group_add_device(struct iommu_group *group, struct device *dev)
600 struct group_device *device;
602 device = kzalloc(sizeof(*device), GFP_KERNEL);
608 ret = sysfs_create_link(&dev->kobj, &group->kobj, "iommu_group");
610 goto err_free_device;
612 device->name = kasprintf(GFP_KERNEL, "%s", kobject_name(&dev->kobj));
616 goto err_remove_link;
619 ret = sysfs_create_link_nowarn(group->devices_kobj,
620 &dev->kobj, device->name);
622 if (ret == -EEXIST && i >= 0) {
624 * Account for the slim chance of collision
625 * and append an instance to the name.
628 device->name = kasprintf(GFP_KERNEL, "%s.%d",
629 kobject_name(&dev->kobj), i++);
635 kobject_get(group->devices_kobj);
637 dev->iommu_group = group;
639 iommu_group_create_direct_mappings(group, dev);
641 mutex_lock(&group->mutex);
642 list_add_tail(&device->list, &group->devices);
644 ret = __iommu_attach_device(group->domain, dev);
645 mutex_unlock(&group->mutex);
649 /* Notify any listeners about change to group. */
650 blocking_notifier_call_chain(&group->notifier,
651 IOMMU_GROUP_NOTIFY_ADD_DEVICE, dev);
653 trace_add_device_to_group(group->id, dev);
655 dev_info(dev, "Adding to iommu group %d\n", group->id);
660 mutex_lock(&group->mutex);
661 list_del(&device->list);
662 mutex_unlock(&group->mutex);
663 dev->iommu_group = NULL;
664 kobject_put(group->devices_kobj);
668 sysfs_remove_link(&dev->kobj, "iommu_group");
671 dev_err(dev, "Failed to add to iommu group %d: %d\n", group->id, ret);
674 EXPORT_SYMBOL_GPL(iommu_group_add_device);
677 * iommu_group_remove_device - remove a device from it's current group
678 * @dev: device to be removed
680 * This function is called by an iommu driver to remove the device from
681 * it's current group. This decrements the iommu group reference count.
683 void iommu_group_remove_device(struct device *dev)
685 struct iommu_group *group = dev->iommu_group;
686 struct group_device *tmp_device, *device = NULL;
688 dev_info(dev, "Removing from iommu group %d\n", group->id);
690 /* Pre-notify listeners that a device is being removed. */
691 blocking_notifier_call_chain(&group->notifier,
692 IOMMU_GROUP_NOTIFY_DEL_DEVICE, dev);
694 mutex_lock(&group->mutex);
695 list_for_each_entry(tmp_device, &group->devices, list) {
696 if (tmp_device->dev == dev) {
698 list_del(&device->list);
702 mutex_unlock(&group->mutex);
707 sysfs_remove_link(group->devices_kobj, device->name);
708 sysfs_remove_link(&dev->kobj, "iommu_group");
710 trace_remove_device_from_group(group->id, dev);
714 dev->iommu_group = NULL;
715 kobject_put(group->devices_kobj);
717 EXPORT_SYMBOL_GPL(iommu_group_remove_device);
719 static int iommu_group_device_count(struct iommu_group *group)
721 struct group_device *entry;
724 list_for_each_entry(entry, &group->devices, list)
731 * iommu_group_for_each_dev - iterate over each device in the group
733 * @data: caller opaque data to be passed to callback function
734 * @fn: caller supplied callback function
736 * This function is called by group users to iterate over group devices.
737 * Callers should hold a reference count to the group during callback.
738 * The group->mutex is held across callbacks, which will block calls to
739 * iommu_group_add/remove_device.
741 static int __iommu_group_for_each_dev(struct iommu_group *group, void *data,
742 int (*fn)(struct device *, void *))
744 struct group_device *device;
747 list_for_each_entry(device, &group->devices, list) {
748 ret = fn(device->dev, data);
756 int iommu_group_for_each_dev(struct iommu_group *group, void *data,
757 int (*fn)(struct device *, void *))
761 mutex_lock(&group->mutex);
762 ret = __iommu_group_for_each_dev(group, data, fn);
763 mutex_unlock(&group->mutex);
767 EXPORT_SYMBOL_GPL(iommu_group_for_each_dev);
770 * iommu_group_get - Return the group for a device and increment reference
771 * @dev: get the group that this device belongs to
773 * This function is called by iommu drivers and users to get the group
774 * for the specified device. If found, the group is returned and the group
775 * reference in incremented, else NULL.
777 struct iommu_group *iommu_group_get(struct device *dev)
779 struct iommu_group *group = dev->iommu_group;
782 kobject_get(group->devices_kobj);
786 EXPORT_SYMBOL_GPL(iommu_group_get);
789 * iommu_group_ref_get - Increment reference on a group
790 * @group: the group to use, must not be NULL
792 * This function is called by iommu drivers to take additional references on an
793 * existing group. Returns the given group for convenience.
795 struct iommu_group *iommu_group_ref_get(struct iommu_group *group)
797 kobject_get(group->devices_kobj);
802 * iommu_group_put - Decrement group reference
803 * @group: the group to use
805 * This function is called by iommu drivers and users to release the
806 * iommu group. Once the reference count is zero, the group is released.
808 void iommu_group_put(struct iommu_group *group)
811 kobject_put(group->devices_kobj);
813 EXPORT_SYMBOL_GPL(iommu_group_put);
816 * iommu_group_register_notifier - Register a notifier for group changes
817 * @group: the group to watch
818 * @nb: notifier block to signal
820 * This function allows iommu group users to track changes in a group.
821 * See include/linux/iommu.h for actions sent via this notifier. Caller
822 * should hold a reference to the group throughout notifier registration.
824 int iommu_group_register_notifier(struct iommu_group *group,
825 struct notifier_block *nb)
827 return blocking_notifier_chain_register(&group->notifier, nb);
829 EXPORT_SYMBOL_GPL(iommu_group_register_notifier);
832 * iommu_group_unregister_notifier - Unregister a notifier
833 * @group: the group to watch
834 * @nb: notifier block to signal
836 * Unregister a previously registered group notifier block.
838 int iommu_group_unregister_notifier(struct iommu_group *group,
839 struct notifier_block *nb)
841 return blocking_notifier_chain_unregister(&group->notifier, nb);
843 EXPORT_SYMBOL_GPL(iommu_group_unregister_notifier);
846 * iommu_group_id - Return ID for a group
847 * @group: the group to ID
849 * Return the unique ID for the group matching the sysfs group number.
851 int iommu_group_id(struct iommu_group *group)
855 EXPORT_SYMBOL_GPL(iommu_group_id);
857 static struct iommu_group *get_pci_alias_group(struct pci_dev *pdev,
858 unsigned long *devfns);
861 * To consider a PCI device isolated, we require ACS to support Source
862 * Validation, Request Redirection, Completer Redirection, and Upstream
863 * Forwarding. This effectively means that devices cannot spoof their
864 * requester ID, requests and completions cannot be redirected, and all
865 * transactions are forwarded upstream, even as it passes through a
866 * bridge where the target device is downstream.
868 #define REQ_ACS_FLAGS (PCI_ACS_SV | PCI_ACS_RR | PCI_ACS_CR | PCI_ACS_UF)
871 * For multifunction devices which are not isolated from each other, find
872 * all the other non-isolated functions and look for existing groups. For
873 * each function, we also need to look for aliases to or from other devices
874 * that may already have a group.
876 static struct iommu_group *get_pci_function_alias_group(struct pci_dev *pdev,
877 unsigned long *devfns)
879 struct pci_dev *tmp = NULL;
880 struct iommu_group *group;
882 if (!pdev->multifunction || pci_acs_enabled(pdev, REQ_ACS_FLAGS))
885 for_each_pci_dev(tmp) {
886 if (tmp == pdev || tmp->bus != pdev->bus ||
887 PCI_SLOT(tmp->devfn) != PCI_SLOT(pdev->devfn) ||
888 pci_acs_enabled(tmp, REQ_ACS_FLAGS))
891 group = get_pci_alias_group(tmp, devfns);
902 * Look for aliases to or from the given device for existing groups. DMA
903 * aliases are only supported on the same bus, therefore the search
904 * space is quite small (especially since we're really only looking at pcie
905 * device, and therefore only expect multiple slots on the root complex or
906 * downstream switch ports). It's conceivable though that a pair of
907 * multifunction devices could have aliases between them that would cause a
908 * loop. To prevent this, we use a bitmap to track where we've been.
910 static struct iommu_group *get_pci_alias_group(struct pci_dev *pdev,
911 unsigned long *devfns)
913 struct pci_dev *tmp = NULL;
914 struct iommu_group *group;
916 if (test_and_set_bit(pdev->devfn & 0xff, devfns))
919 group = iommu_group_get(&pdev->dev);
923 for_each_pci_dev(tmp) {
924 if (tmp == pdev || tmp->bus != pdev->bus)
927 /* We alias them or they alias us */
928 if (pci_devs_are_dma_aliases(pdev, tmp)) {
929 group = get_pci_alias_group(tmp, devfns);
935 group = get_pci_function_alias_group(tmp, devfns);
946 struct group_for_pci_data {
947 struct pci_dev *pdev;
948 struct iommu_group *group;
952 * DMA alias iterator callback, return the last seen device. Stop and return
953 * the IOMMU group if we find one along the way.
955 static int get_pci_alias_or_group(struct pci_dev *pdev, u16 alias, void *opaque)
957 struct group_for_pci_data *data = opaque;
960 data->group = iommu_group_get(&pdev->dev);
962 return data->group != NULL;
966 * Generic device_group call-back function. It just allocates one
967 * iommu-group per device.
969 struct iommu_group *generic_device_group(struct device *dev)
971 return iommu_group_alloc();
975 * Use standard PCI bus topology, isolation features, and DMA alias quirks
976 * to find or create an IOMMU group for a device.
978 struct iommu_group *pci_device_group(struct device *dev)
980 struct pci_dev *pdev = to_pci_dev(dev);
981 struct group_for_pci_data data;
983 struct iommu_group *group = NULL;
984 u64 devfns[4] = { 0 };
986 if (WARN_ON(!dev_is_pci(dev)))
987 return ERR_PTR(-EINVAL);
990 * Find the upstream DMA alias for the device. A device must not
991 * be aliased due to topology in order to have its own IOMMU group.
992 * If we find an alias along the way that already belongs to a
995 if (pci_for_each_dma_alias(pdev, get_pci_alias_or_group, &data))
1001 * Continue upstream from the point of minimum IOMMU granularity
1002 * due to aliases to the point where devices are protected from
1003 * peer-to-peer DMA by PCI ACS. Again, if we find an existing
1006 for (bus = pdev->bus; !pci_is_root_bus(bus); bus = bus->parent) {
1010 if (pci_acs_path_enabled(bus->self, NULL, REQ_ACS_FLAGS))
1015 group = iommu_group_get(&pdev->dev);
1021 * Look for existing groups on device aliases. If we alias another
1022 * device or another device aliases us, use the same group.
1024 group = get_pci_alias_group(pdev, (unsigned long *)devfns);
1029 * Look for existing groups on non-isolated functions on the same
1030 * slot and aliases of those funcions, if any. No need to clear
1031 * the search bitmap, the tested devfns are still valid.
1033 group = get_pci_function_alias_group(pdev, (unsigned long *)devfns);
1037 /* No shared group found, allocate new */
1038 return iommu_group_alloc();
1041 /* Get the IOMMU group for device on fsl-mc bus */
1042 struct iommu_group *fsl_mc_device_group(struct device *dev)
1044 struct device *cont_dev = fsl_mc_cont_dev(dev);
1045 struct iommu_group *group;
1047 group = iommu_group_get(cont_dev);
1049 group = iommu_group_alloc();
1054 * iommu_group_get_for_dev - Find or create the IOMMU group for a device
1055 * @dev: target device
1057 * This function is intended to be called by IOMMU drivers and extended to
1058 * support common, bus-defined algorithms when determining or creating the
1059 * IOMMU group for a device. On success, the caller will hold a reference
1060 * to the returned IOMMU group, which will already include the provided
1061 * device. The reference should be released with iommu_group_put().
1063 struct iommu_group *iommu_group_get_for_dev(struct device *dev)
1065 const struct iommu_ops *ops = dev->bus->iommu_ops;
1066 struct iommu_group *group;
1069 group = iommu_group_get(dev);
1074 return ERR_PTR(-EINVAL);
1076 group = ops->device_group(dev);
1077 if (WARN_ON_ONCE(group == NULL))
1078 return ERR_PTR(-EINVAL);
1084 * Try to allocate a default domain - needs support from the
1087 if (!group->default_domain) {
1088 struct iommu_domain *dom;
1090 dom = __iommu_domain_alloc(dev->bus, iommu_def_domain_type);
1091 if (!dom && iommu_def_domain_type != IOMMU_DOMAIN_DMA) {
1092 dom = __iommu_domain_alloc(dev->bus, IOMMU_DOMAIN_DMA);
1095 "failed to allocate default IOMMU domain of type %u; falling back to IOMMU_DOMAIN_DMA",
1096 iommu_def_domain_type);
1100 group->default_domain = dom;
1102 group->domain = dom;
1104 if (dom && !iommu_dma_strict) {
1106 iommu_domain_set_attr(dom,
1107 DOMAIN_ATTR_DMA_USE_FLUSH_QUEUE,
1112 ret = iommu_group_add_device(group, dev);
1114 iommu_group_put(group);
1115 return ERR_PTR(ret);
1121 struct iommu_domain *iommu_group_default_domain(struct iommu_group *group)
1123 return group->default_domain;
1126 static int add_iommu_group(struct device *dev, void *data)
1128 int ret = iommu_probe_device(dev);
1131 * We ignore -ENODEV errors for now, as they just mean that the
1132 * device is not translated by an IOMMU. We still care about
1133 * other errors and fail to initialize when they happen.
1141 static int remove_iommu_group(struct device *dev, void *data)
1143 iommu_release_device(dev);
1148 static int iommu_bus_notifier(struct notifier_block *nb,
1149 unsigned long action, void *data)
1151 unsigned long group_action = 0;
1152 struct device *dev = data;
1153 struct iommu_group *group;
1156 * ADD/DEL call into iommu driver ops if provided, which may
1157 * result in ADD/DEL notifiers to group->notifier
1159 if (action == BUS_NOTIFY_ADD_DEVICE) {
1162 ret = iommu_probe_device(dev);
1163 return (ret) ? NOTIFY_DONE : NOTIFY_OK;
1164 } else if (action == BUS_NOTIFY_REMOVED_DEVICE) {
1165 iommu_release_device(dev);
1170 * Remaining BUS_NOTIFYs get filtered and republished to the
1171 * group, if anyone is listening
1173 group = iommu_group_get(dev);
1178 case BUS_NOTIFY_BIND_DRIVER:
1179 group_action = IOMMU_GROUP_NOTIFY_BIND_DRIVER;
1181 case BUS_NOTIFY_BOUND_DRIVER:
1182 group_action = IOMMU_GROUP_NOTIFY_BOUND_DRIVER;
1184 case BUS_NOTIFY_UNBIND_DRIVER:
1185 group_action = IOMMU_GROUP_NOTIFY_UNBIND_DRIVER;
1187 case BUS_NOTIFY_UNBOUND_DRIVER:
1188 group_action = IOMMU_GROUP_NOTIFY_UNBOUND_DRIVER;
1193 blocking_notifier_call_chain(&group->notifier,
1196 iommu_group_put(group);
1200 static int iommu_bus_init(struct bus_type *bus, const struct iommu_ops *ops)
1203 struct notifier_block *nb;
1205 nb = kzalloc(sizeof(struct notifier_block), GFP_KERNEL);
1209 nb->notifier_call = iommu_bus_notifier;
1211 err = bus_register_notifier(bus, nb);
1215 err = bus_for_each_dev(bus, NULL, NULL, add_iommu_group);
1224 bus_for_each_dev(bus, NULL, NULL, remove_iommu_group);
1225 bus_unregister_notifier(bus, nb);
1234 * bus_set_iommu - set iommu-callbacks for the bus
1236 * @ops: the callbacks provided by the iommu-driver
1238 * This function is called by an iommu driver to set the iommu methods
1239 * used for a particular bus. Drivers for devices on that bus can use
1240 * the iommu-api after these ops are registered.
1241 * This special function is needed because IOMMUs are usually devices on
1242 * the bus itself, so the iommu drivers are not initialized when the bus
1243 * is set up. With this function the iommu-driver can set the iommu-ops
1246 int bus_set_iommu(struct bus_type *bus, const struct iommu_ops *ops)
1250 if (bus->iommu_ops != NULL)
1253 bus->iommu_ops = ops;
1255 /* Do IOMMU specific setup for this bus-type */
1256 err = iommu_bus_init(bus, ops);
1258 bus->iommu_ops = NULL;
1262 EXPORT_SYMBOL_GPL(bus_set_iommu);
1264 bool iommu_present(struct bus_type *bus)
1266 return bus->iommu_ops != NULL;
1268 EXPORT_SYMBOL_GPL(iommu_present);
1270 bool iommu_capable(struct bus_type *bus, enum iommu_cap cap)
1272 if (!bus->iommu_ops || !bus->iommu_ops->capable)
1275 return bus->iommu_ops->capable(cap);
1277 EXPORT_SYMBOL_GPL(iommu_capable);
1280 * iommu_set_fault_handler() - set a fault handler for an iommu domain
1281 * @domain: iommu domain
1282 * @handler: fault handler
1283 * @token: user data, will be passed back to the fault handler
1285 * This function should be used by IOMMU users which want to be notified
1286 * whenever an IOMMU fault happens.
1288 * The fault handler itself should return 0 on success, and an appropriate
1289 * error code otherwise.
1291 void iommu_set_fault_handler(struct iommu_domain *domain,
1292 iommu_fault_handler_t handler,
1297 domain->handler = handler;
1298 domain->handler_token = token;
1300 EXPORT_SYMBOL_GPL(iommu_set_fault_handler);
1302 static struct iommu_domain *__iommu_domain_alloc(struct bus_type *bus,
1305 struct iommu_domain *domain;
1307 if (bus == NULL || bus->iommu_ops == NULL)
1310 domain = bus->iommu_ops->domain_alloc(type);
1314 domain->ops = bus->iommu_ops;
1315 domain->type = type;
1316 /* Assume all sizes by default; the driver may override this later */
1317 domain->pgsize_bitmap = bus->iommu_ops->pgsize_bitmap;
1322 struct iommu_domain *iommu_domain_alloc(struct bus_type *bus)
1324 return __iommu_domain_alloc(bus, IOMMU_DOMAIN_UNMANAGED);
1326 EXPORT_SYMBOL_GPL(iommu_domain_alloc);
1328 void iommu_domain_free(struct iommu_domain *domain)
1330 domain->ops->domain_free(domain);
1332 EXPORT_SYMBOL_GPL(iommu_domain_free);
1334 static int __iommu_attach_device(struct iommu_domain *domain,
1338 if ((domain->ops->is_attach_deferred != NULL) &&
1339 domain->ops->is_attach_deferred(domain, dev))
1342 if (unlikely(domain->ops->attach_dev == NULL))
1345 ret = domain->ops->attach_dev(domain, dev);
1347 trace_attach_device_to_domain(dev);
1351 int iommu_attach_device(struct iommu_domain *domain, struct device *dev)
1353 struct iommu_group *group;
1356 group = iommu_group_get(dev);
1361 * Lock the group to make sure the device-count doesn't
1362 * change while we are attaching
1364 mutex_lock(&group->mutex);
1366 if (iommu_group_device_count(group) != 1)
1369 ret = __iommu_attach_group(domain, group);
1372 mutex_unlock(&group->mutex);
1373 iommu_group_put(group);
1377 EXPORT_SYMBOL_GPL(iommu_attach_device);
1379 static void __iommu_detach_device(struct iommu_domain *domain,
1382 if ((domain->ops->is_attach_deferred != NULL) &&
1383 domain->ops->is_attach_deferred(domain, dev))
1386 if (unlikely(domain->ops->detach_dev == NULL))
1389 domain->ops->detach_dev(domain, dev);
1390 trace_detach_device_from_domain(dev);
1393 void iommu_detach_device(struct iommu_domain *domain, struct device *dev)
1395 struct iommu_group *group;
1397 group = iommu_group_get(dev);
1401 mutex_lock(&group->mutex);
1402 if (iommu_group_device_count(group) != 1) {
1407 __iommu_detach_group(domain, group);
1410 mutex_unlock(&group->mutex);
1411 iommu_group_put(group);
1413 EXPORT_SYMBOL_GPL(iommu_detach_device);
1415 struct iommu_domain *iommu_get_domain_for_dev(struct device *dev)
1417 struct iommu_domain *domain;
1418 struct iommu_group *group;
1420 group = iommu_group_get(dev);
1424 domain = group->domain;
1426 iommu_group_put(group);
1430 EXPORT_SYMBOL_GPL(iommu_get_domain_for_dev);
1433 * For IOMMU_DOMAIN_DMA implementations which already provide their own
1434 * guarantees that the group and its default domain are valid and correct.
1436 struct iommu_domain *iommu_get_dma_domain(struct device *dev)
1438 return dev->iommu_group->default_domain;
1442 * IOMMU groups are really the natural working unit of the IOMMU, but
1443 * the IOMMU API works on domains and devices. Bridge that gap by
1444 * iterating over the devices in a group. Ideally we'd have a single
1445 * device which represents the requestor ID of the group, but we also
1446 * allow IOMMU drivers to create policy defined minimum sets, where
1447 * the physical hardware may be able to distiguish members, but we
1448 * wish to group them at a higher level (ex. untrusted multi-function
1449 * PCI devices). Thus we attach each device.
1451 static int iommu_group_do_attach_device(struct device *dev, void *data)
1453 struct iommu_domain *domain = data;
1455 return __iommu_attach_device(domain, dev);
1458 static int __iommu_attach_group(struct iommu_domain *domain,
1459 struct iommu_group *group)
1463 if (group->default_domain && group->domain != group->default_domain)
1466 ret = __iommu_group_for_each_dev(group, domain,
1467 iommu_group_do_attach_device);
1469 group->domain = domain;
1474 int iommu_attach_group(struct iommu_domain *domain, struct iommu_group *group)
1478 mutex_lock(&group->mutex);
1479 ret = __iommu_attach_group(domain, group);
1480 mutex_unlock(&group->mutex);
1484 EXPORT_SYMBOL_GPL(iommu_attach_group);
1486 static int iommu_group_do_detach_device(struct device *dev, void *data)
1488 struct iommu_domain *domain = data;
1490 __iommu_detach_device(domain, dev);
1495 static void __iommu_detach_group(struct iommu_domain *domain,
1496 struct iommu_group *group)
1500 if (!group->default_domain) {
1501 __iommu_group_for_each_dev(group, domain,
1502 iommu_group_do_detach_device);
1503 group->domain = NULL;
1507 if (group->domain == group->default_domain)
1510 /* Detach by re-attaching to the default domain */
1511 ret = __iommu_group_for_each_dev(group, group->default_domain,
1512 iommu_group_do_attach_device);
1516 group->domain = group->default_domain;
1519 void iommu_detach_group(struct iommu_domain *domain, struct iommu_group *group)
1521 mutex_lock(&group->mutex);
1522 __iommu_detach_group(domain, group);
1523 mutex_unlock(&group->mutex);
1525 EXPORT_SYMBOL_GPL(iommu_detach_group);
1527 phys_addr_t iommu_iova_to_phys(struct iommu_domain *domain, dma_addr_t iova)
1529 if (unlikely(domain->ops->iova_to_phys == NULL))
1532 return domain->ops->iova_to_phys(domain, iova);
1534 EXPORT_SYMBOL_GPL(iommu_iova_to_phys);
1536 static size_t iommu_pgsize(struct iommu_domain *domain,
1537 unsigned long addr_merge, size_t size)
1539 unsigned int pgsize_idx;
1542 /* Max page size that still fits into 'size' */
1543 pgsize_idx = __fls(size);
1545 /* need to consider alignment requirements ? */
1546 if (likely(addr_merge)) {
1547 /* Max page size allowed by address */
1548 unsigned int align_pgsize_idx = __ffs(addr_merge);
1549 pgsize_idx = min(pgsize_idx, align_pgsize_idx);
1552 /* build a mask of acceptable page sizes */
1553 pgsize = (1UL << (pgsize_idx + 1)) - 1;
1555 /* throw away page sizes not supported by the hardware */
1556 pgsize &= domain->pgsize_bitmap;
1558 /* make sure we're still sane */
1561 /* pick the biggest page */
1562 pgsize_idx = __fls(pgsize);
1563 pgsize = 1UL << pgsize_idx;
1568 int iommu_map(struct iommu_domain *domain, unsigned long iova,
1569 phys_addr_t paddr, size_t size, int prot)
1571 const struct iommu_ops *ops = domain->ops;
1572 unsigned long orig_iova = iova;
1573 unsigned int min_pagesz;
1574 size_t orig_size = size;
1575 phys_addr_t orig_paddr = paddr;
1578 if (unlikely(ops->map == NULL ||
1579 domain->pgsize_bitmap == 0UL))
1582 if (unlikely(!(domain->type & __IOMMU_DOMAIN_PAGING)))
1585 /* find out the minimum page size supported */
1586 min_pagesz = 1 << __ffs(domain->pgsize_bitmap);
1589 * both the virtual address and the physical one, as well as
1590 * the size of the mapping, must be aligned (at least) to the
1591 * size of the smallest page supported by the hardware
1593 if (!IS_ALIGNED(iova | paddr | size, min_pagesz)) {
1594 pr_err("unaligned: iova 0x%lx pa %pa size 0x%zx min_pagesz 0x%x\n",
1595 iova, &paddr, size, min_pagesz);
1599 pr_debug("map: iova 0x%lx pa %pa size 0x%zx\n", iova, &paddr, size);
1602 size_t pgsize = iommu_pgsize(domain, iova | paddr, size);
1604 pr_debug("mapping: iova 0x%lx pa %pa pgsize 0x%zx\n",
1605 iova, &paddr, pgsize);
1607 ret = ops->map(domain, iova, paddr, pgsize, prot);
1616 if (ops->iotlb_sync_map)
1617 ops->iotlb_sync_map(domain);
1619 /* unroll mapping in case something went wrong */
1621 iommu_unmap(domain, orig_iova, orig_size - size);
1623 trace_map(orig_iova, orig_paddr, orig_size);
1627 EXPORT_SYMBOL_GPL(iommu_map);
1629 static size_t __iommu_unmap(struct iommu_domain *domain,
1630 unsigned long iova, size_t size,
1633 const struct iommu_ops *ops = domain->ops;
1634 size_t unmapped_page, unmapped = 0;
1635 unsigned long orig_iova = iova;
1636 unsigned int min_pagesz;
1638 if (unlikely(ops->unmap == NULL ||
1639 domain->pgsize_bitmap == 0UL))
1642 if (unlikely(!(domain->type & __IOMMU_DOMAIN_PAGING)))
1645 /* find out the minimum page size supported */
1646 min_pagesz = 1 << __ffs(domain->pgsize_bitmap);
1649 * The virtual address, as well as the size of the mapping, must be
1650 * aligned (at least) to the size of the smallest page supported
1653 if (!IS_ALIGNED(iova | size, min_pagesz)) {
1654 pr_err("unaligned: iova 0x%lx size 0x%zx min_pagesz 0x%x\n",
1655 iova, size, min_pagesz);
1659 pr_debug("unmap this: iova 0x%lx size 0x%zx\n", iova, size);
1662 * Keep iterating until we either unmap 'size' bytes (or more)
1663 * or we hit an area that isn't mapped.
1665 while (unmapped < size) {
1666 size_t pgsize = iommu_pgsize(domain, iova, size - unmapped);
1668 unmapped_page = ops->unmap(domain, iova, pgsize);
1672 if (sync && ops->iotlb_range_add)
1673 ops->iotlb_range_add(domain, iova, pgsize);
1675 pr_debug("unmapped: iova 0x%lx size 0x%zx\n",
1676 iova, unmapped_page);
1678 iova += unmapped_page;
1679 unmapped += unmapped_page;
1682 if (sync && ops->iotlb_sync)
1683 ops->iotlb_sync(domain);
1685 trace_unmap(orig_iova, size, unmapped);
1689 size_t iommu_unmap(struct iommu_domain *domain,
1690 unsigned long iova, size_t size)
1692 return __iommu_unmap(domain, iova, size, true);
1694 EXPORT_SYMBOL_GPL(iommu_unmap);
1696 size_t iommu_unmap_fast(struct iommu_domain *domain,
1697 unsigned long iova, size_t size)
1699 return __iommu_unmap(domain, iova, size, false);
1701 EXPORT_SYMBOL_GPL(iommu_unmap_fast);
1703 size_t iommu_map_sg(struct iommu_domain *domain, unsigned long iova,
1704 struct scatterlist *sg, unsigned int nents, int prot)
1706 size_t len = 0, mapped = 0;
1711 while (i <= nents) {
1712 phys_addr_t s_phys = sg_phys(sg);
1714 if (len && s_phys != start + len) {
1715 ret = iommu_map(domain, iova + mapped, start, len, prot);
1737 /* undo mappings already done */
1738 iommu_unmap(domain, iova, mapped);
1743 EXPORT_SYMBOL_GPL(iommu_map_sg);
1745 int iommu_domain_window_enable(struct iommu_domain *domain, u32 wnd_nr,
1746 phys_addr_t paddr, u64 size, int prot)
1748 if (unlikely(domain->ops->domain_window_enable == NULL))
1751 return domain->ops->domain_window_enable(domain, wnd_nr, paddr, size,
1754 EXPORT_SYMBOL_GPL(iommu_domain_window_enable);
1756 void iommu_domain_window_disable(struct iommu_domain *domain, u32 wnd_nr)
1758 if (unlikely(domain->ops->domain_window_disable == NULL))
1761 return domain->ops->domain_window_disable(domain, wnd_nr);
1763 EXPORT_SYMBOL_GPL(iommu_domain_window_disable);
1766 * report_iommu_fault() - report about an IOMMU fault to the IOMMU framework
1767 * @domain: the iommu domain where the fault has happened
1768 * @dev: the device where the fault has happened
1769 * @iova: the faulting address
1770 * @flags: mmu fault flags (e.g. IOMMU_FAULT_READ/IOMMU_FAULT_WRITE/...)
1772 * This function should be called by the low-level IOMMU implementations
1773 * whenever IOMMU faults happen, to allow high-level users, that are
1774 * interested in such events, to know about them.
1776 * This event may be useful for several possible use cases:
1777 * - mere logging of the event
1778 * - dynamic TLB/PTE loading
1779 * - if restarting of the faulting device is required
1781 * Returns 0 on success and an appropriate error code otherwise (if dynamic
1782 * PTE/TLB loading will one day be supported, implementations will be able
1783 * to tell whether it succeeded or not according to this return value).
1785 * Specifically, -ENOSYS is returned if a fault handler isn't installed
1786 * (though fault handlers can also return -ENOSYS, in case they want to
1787 * elicit the default behavior of the IOMMU drivers).
1789 int report_iommu_fault(struct iommu_domain *domain, struct device *dev,
1790 unsigned long iova, int flags)
1795 * if upper layers showed interest and installed a fault handler,
1798 if (domain->handler)
1799 ret = domain->handler(domain, dev, iova, flags,
1800 domain->handler_token);
1802 trace_io_page_fault(dev, iova, flags);
1805 EXPORT_SYMBOL_GPL(report_iommu_fault);
1807 static int __init iommu_init(void)
1809 iommu_group_kset = kset_create_and_add("iommu_groups",
1811 BUG_ON(!iommu_group_kset);
1813 iommu_debugfs_setup();
1817 core_initcall(iommu_init);
1819 int iommu_domain_get_attr(struct iommu_domain *domain,
1820 enum iommu_attr attr, void *data)
1822 struct iommu_domain_geometry *geometry;
1827 case DOMAIN_ATTR_GEOMETRY:
1829 *geometry = domain->geometry;
1832 case DOMAIN_ATTR_PAGING:
1834 *paging = (domain->pgsize_bitmap != 0UL);
1837 if (!domain->ops->domain_get_attr)
1840 ret = domain->ops->domain_get_attr(domain, attr, data);
1845 EXPORT_SYMBOL_GPL(iommu_domain_get_attr);
1847 int iommu_domain_set_attr(struct iommu_domain *domain,
1848 enum iommu_attr attr, void *data)
1854 if (domain->ops->domain_set_attr == NULL)
1857 ret = domain->ops->domain_set_attr(domain, attr, data);
1862 EXPORT_SYMBOL_GPL(iommu_domain_set_attr);
1864 void iommu_get_resv_regions(struct device *dev, struct list_head *list)
1866 const struct iommu_ops *ops = dev->bus->iommu_ops;
1868 if (ops && ops->get_resv_regions)
1869 ops->get_resv_regions(dev, list);
1872 void iommu_put_resv_regions(struct device *dev, struct list_head *list)
1874 const struct iommu_ops *ops = dev->bus->iommu_ops;
1876 if (ops && ops->put_resv_regions)
1877 ops->put_resv_regions(dev, list);
1880 struct iommu_resv_region *iommu_alloc_resv_region(phys_addr_t start,
1881 size_t length, int prot,
1882 enum iommu_resv_type type)
1884 struct iommu_resv_region *region;
1886 region = kzalloc(sizeof(*region), GFP_KERNEL);
1890 INIT_LIST_HEAD(®ion->list);
1891 region->start = start;
1892 region->length = length;
1893 region->prot = prot;
1894 region->type = type;
1898 /* Request that a device is direct mapped by the IOMMU */
1899 int iommu_request_dm_for_dev(struct device *dev)
1901 struct iommu_domain *dm_domain;
1902 struct iommu_group *group;
1905 /* Device must already be in a group before calling this function */
1906 group = iommu_group_get_for_dev(dev);
1908 return PTR_ERR(group);
1910 mutex_lock(&group->mutex);
1912 /* Check if the default domain is already direct mapped */
1914 if (group->default_domain &&
1915 group->default_domain->type == IOMMU_DOMAIN_IDENTITY)
1918 /* Don't change mappings of existing devices */
1920 if (iommu_group_device_count(group) != 1)
1923 /* Allocate a direct mapped domain */
1925 dm_domain = __iommu_domain_alloc(dev->bus, IOMMU_DOMAIN_IDENTITY);
1929 /* Attach the device to the domain */
1930 ret = __iommu_attach_group(dm_domain, group);
1932 iommu_domain_free(dm_domain);
1936 /* Make the direct mapped domain the default for this group */
1937 if (group->default_domain)
1938 iommu_domain_free(group->default_domain);
1939 group->default_domain = dm_domain;
1941 dev_info(dev, "Using iommu direct mapping\n");
1945 mutex_unlock(&group->mutex);
1946 iommu_group_put(group);
1951 const struct iommu_ops *iommu_ops_from_fwnode(struct fwnode_handle *fwnode)
1953 const struct iommu_ops *ops = NULL;
1954 struct iommu_device *iommu;
1956 spin_lock(&iommu_device_lock);
1957 list_for_each_entry(iommu, &iommu_device_list, list)
1958 if (iommu->fwnode == fwnode) {
1962 spin_unlock(&iommu_device_lock);
1966 int iommu_fwspec_init(struct device *dev, struct fwnode_handle *iommu_fwnode,
1967 const struct iommu_ops *ops)
1969 struct iommu_fwspec *fwspec = dev_iommu_fwspec_get(dev);
1972 return ops == fwspec->ops ? 0 : -EINVAL;
1974 fwspec = kzalloc(sizeof(*fwspec), GFP_KERNEL);
1978 of_node_get(to_of_node(iommu_fwnode));
1979 fwspec->iommu_fwnode = iommu_fwnode;
1981 dev_iommu_fwspec_set(dev, fwspec);
1984 EXPORT_SYMBOL_GPL(iommu_fwspec_init);
1986 void iommu_fwspec_free(struct device *dev)
1988 struct iommu_fwspec *fwspec = dev_iommu_fwspec_get(dev);
1991 fwnode_handle_put(fwspec->iommu_fwnode);
1993 dev_iommu_fwspec_set(dev, NULL);
1996 EXPORT_SYMBOL_GPL(iommu_fwspec_free);
1998 int iommu_fwspec_add_ids(struct device *dev, u32 *ids, int num_ids)
2000 struct iommu_fwspec *fwspec = dev_iommu_fwspec_get(dev);
2007 size = offsetof(struct iommu_fwspec, ids[fwspec->num_ids + num_ids]);
2008 if (size > sizeof(*fwspec)) {
2009 fwspec = krealloc(fwspec, size, GFP_KERNEL);
2013 dev_iommu_fwspec_set(dev, fwspec);
2016 for (i = 0; i < num_ids; i++)
2017 fwspec->ids[fwspec->num_ids + i] = ids[i];
2019 fwspec->num_ids += num_ids;
2022 EXPORT_SYMBOL_GPL(iommu_fwspec_add_ids);
2025 * Per device IOMMU features.
2027 bool iommu_dev_has_feature(struct device *dev, enum iommu_dev_features feat)
2029 const struct iommu_ops *ops = dev->bus->iommu_ops;
2031 if (ops && ops->dev_has_feat)
2032 return ops->dev_has_feat(dev, feat);
2036 EXPORT_SYMBOL_GPL(iommu_dev_has_feature);
2038 int iommu_dev_enable_feature(struct device *dev, enum iommu_dev_features feat)
2040 const struct iommu_ops *ops = dev->bus->iommu_ops;
2042 if (ops && ops->dev_enable_feat)
2043 return ops->dev_enable_feat(dev, feat);
2047 EXPORT_SYMBOL_GPL(iommu_dev_enable_feature);
2050 * The device drivers should do the necessary cleanups before calling this.
2051 * For example, before disabling the aux-domain feature, the device driver
2052 * should detach all aux-domains. Otherwise, this will return -EBUSY.
2054 int iommu_dev_disable_feature(struct device *dev, enum iommu_dev_features feat)
2056 const struct iommu_ops *ops = dev->bus->iommu_ops;
2058 if (ops && ops->dev_disable_feat)
2059 return ops->dev_disable_feat(dev, feat);
2063 EXPORT_SYMBOL_GPL(iommu_dev_disable_feature);
2065 bool iommu_dev_feature_enabled(struct device *dev, enum iommu_dev_features feat)
2067 const struct iommu_ops *ops = dev->bus->iommu_ops;
2069 if (ops && ops->dev_feat_enabled)
2070 return ops->dev_feat_enabled(dev, feat);
2074 EXPORT_SYMBOL_GPL(iommu_dev_feature_enabled);
2077 * Aux-domain specific attach/detach.
2079 * Only works if iommu_dev_feature_enabled(dev, IOMMU_DEV_FEAT_AUX) returns
2080 * true. Also, as long as domains are attached to a device through this
2081 * interface, any tries to call iommu_attach_device() should fail
2082 * (iommu_detach_device() can't fail, so we fail when trying to re-attach).
2083 * This should make us safe against a device being attached to a guest as a
2084 * whole while there are still pasid users on it (aux and sva).
2086 int iommu_aux_attach_device(struct iommu_domain *domain, struct device *dev)
2090 if (domain->ops->aux_attach_dev)
2091 ret = domain->ops->aux_attach_dev(domain, dev);
2094 trace_attach_device_to_domain(dev);
2098 EXPORT_SYMBOL_GPL(iommu_aux_attach_device);
2100 void iommu_aux_detach_device(struct iommu_domain *domain, struct device *dev)
2102 if (domain->ops->aux_detach_dev) {
2103 domain->ops->aux_detach_dev(domain, dev);
2104 trace_detach_device_from_domain(dev);
2107 EXPORT_SYMBOL_GPL(iommu_aux_detach_device);
2109 int iommu_aux_get_pasid(struct iommu_domain *domain, struct device *dev)
2113 if (domain->ops->aux_get_pasid)
2114 ret = domain->ops->aux_get_pasid(domain, dev);
2118 EXPORT_SYMBOL_GPL(iommu_aux_get_pasid);
2121 * iommu_sva_bind_device() - Bind a process address space to a device
2123 * @mm: the mm to bind, caller must hold a reference to it
2125 * Create a bond between device and address space, allowing the device to access
2126 * the mm using the returned PASID. If a bond already exists between @device and
2127 * @mm, it is returned and an additional reference is taken. Caller must call
2128 * iommu_sva_unbind_device() to release each reference.
2130 * iommu_dev_enable_feature(dev, IOMMU_DEV_FEAT_SVA) must be called first, to
2131 * initialize the required SVA features.
2133 * On error, returns an ERR_PTR value.
2136 iommu_sva_bind_device(struct device *dev, struct mm_struct *mm, void *drvdata)
2138 struct iommu_group *group;
2139 struct iommu_sva *handle = ERR_PTR(-EINVAL);
2140 const struct iommu_ops *ops = dev->bus->iommu_ops;
2142 if (!ops || !ops->sva_bind)
2143 return ERR_PTR(-ENODEV);
2145 group = iommu_group_get(dev);
2147 return ERR_PTR(-ENODEV);
2149 /* Ensure device count and domain don't change while we're binding */
2150 mutex_lock(&group->mutex);
2153 * To keep things simple, SVA currently doesn't support IOMMU groups
2154 * with more than one device. Existing SVA-capable systems are not
2155 * affected by the problems that required IOMMU groups (lack of ACS
2156 * isolation, device ID aliasing and other hardware issues).
2158 if (iommu_group_device_count(group) != 1)
2161 handle = ops->sva_bind(dev, mm, drvdata);
2164 mutex_unlock(&group->mutex);
2165 iommu_group_put(group);
2169 EXPORT_SYMBOL_GPL(iommu_sva_bind_device);
2172 * iommu_sva_unbind_device() - Remove a bond created with iommu_sva_bind_device
2173 * @handle: the handle returned by iommu_sva_bind_device()
2175 * Put reference to a bond between device and address space. The device should
2176 * not be issuing any more transaction for this PASID. All outstanding page
2177 * requests for this PASID must have been flushed to the IOMMU.
2179 * Returns 0 on success, or an error value
2181 void iommu_sva_unbind_device(struct iommu_sva *handle)
2183 struct iommu_group *group;
2184 struct device *dev = handle->dev;
2185 const struct iommu_ops *ops = dev->bus->iommu_ops;
2187 if (!ops || !ops->sva_unbind)
2190 group = iommu_group_get(dev);
2194 mutex_lock(&group->mutex);
2195 ops->sva_unbind(handle);
2196 mutex_unlock(&group->mutex);
2198 iommu_group_put(group);
2200 EXPORT_SYMBOL_GPL(iommu_sva_unbind_device);
2202 int iommu_sva_set_ops(struct iommu_sva *handle,
2203 const struct iommu_sva_ops *sva_ops)
2205 if (handle->ops && handle->ops != sva_ops)
2208 handle->ops = sva_ops;
2211 EXPORT_SYMBOL_GPL(iommu_sva_set_ops);
2213 int iommu_sva_get_pasid(struct iommu_sva *handle)
2215 const struct iommu_ops *ops = handle->dev->bus->iommu_ops;
2217 if (!ops || !ops->sva_get_pasid)
2218 return IOMMU_PASID_INVALID;
2220 return ops->sva_get_pasid(handle);
2222 EXPORT_SYMBOL_GPL(iommu_sva_get_pasid);