2 * Copyright (C) 2007-2008 Advanced Micro Devices, Inc.
3 * Author: Joerg Roedel <jroedel@suse.de>
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 as published
7 * by the Free Software Foundation.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 #define pr_fmt(fmt) "iommu: " fmt
21 #include <linux/device.h>
22 #include <linux/kernel.h>
23 #include <linux/bug.h>
24 #include <linux/types.h>
25 #include <linux/init.h>
26 #include <linux/export.h>
27 #include <linux/slab.h>
28 #include <linux/errno.h>
29 #include <linux/iommu.h>
30 #include <linux/idr.h>
31 #include <linux/notifier.h>
32 #include <linux/err.h>
33 #include <linux/pci.h>
34 #include <linux/bitops.h>
35 #include <linux/property.h>
36 #include <linux/fsl/mc.h>
37 #include <trace/events/iommu.h>
39 static struct kset *iommu_group_kset;
40 static DEFINE_IDA(iommu_group_ida);
41 #ifdef CONFIG_IOMMU_DEFAULT_PASSTHROUGH
42 static unsigned int iommu_def_domain_type = IOMMU_DOMAIN_IDENTITY;
44 static unsigned int iommu_def_domain_type = IOMMU_DOMAIN_DMA;
46 static bool iommu_dma_strict __read_mostly = true;
48 struct iommu_callback_data {
49 const struct iommu_ops *ops;
54 struct kobject *devices_kobj;
55 struct list_head devices;
57 struct blocking_notifier_head notifier;
59 void (*iommu_data_release)(void *iommu_data);
62 struct iommu_domain *default_domain;
63 struct iommu_domain *domain;
67 struct list_head list;
72 struct iommu_group_attribute {
73 struct attribute attr;
74 ssize_t (*show)(struct iommu_group *group, char *buf);
75 ssize_t (*store)(struct iommu_group *group,
76 const char *buf, size_t count);
79 static const char * const iommu_group_resv_type_string[] = {
80 [IOMMU_RESV_DIRECT] = "direct",
81 [IOMMU_RESV_RESERVED] = "reserved",
82 [IOMMU_RESV_MSI] = "msi",
83 [IOMMU_RESV_SW_MSI] = "msi",
86 #define IOMMU_GROUP_ATTR(_name, _mode, _show, _store) \
87 struct iommu_group_attribute iommu_group_attr_##_name = \
88 __ATTR(_name, _mode, _show, _store)
90 #define to_iommu_group_attr(_attr) \
91 container_of(_attr, struct iommu_group_attribute, attr)
92 #define to_iommu_group(_kobj) \
93 container_of(_kobj, struct iommu_group, kobj)
95 static LIST_HEAD(iommu_device_list);
96 static DEFINE_SPINLOCK(iommu_device_lock);
98 int iommu_device_register(struct iommu_device *iommu)
100 spin_lock(&iommu_device_lock);
101 list_add_tail(&iommu->list, &iommu_device_list);
102 spin_unlock(&iommu_device_lock);
107 void iommu_device_unregister(struct iommu_device *iommu)
109 spin_lock(&iommu_device_lock);
110 list_del(&iommu->list);
111 spin_unlock(&iommu_device_lock);
114 int iommu_probe_device(struct device *dev)
116 const struct iommu_ops *ops = dev->bus->iommu_ops;
119 WARN_ON(dev->iommu_group);
122 ret = ops->add_device(dev);
127 void iommu_release_device(struct device *dev)
129 const struct iommu_ops *ops = dev->bus->iommu_ops;
131 if (dev->iommu_group)
132 ops->remove_device(dev);
135 static struct iommu_domain *__iommu_domain_alloc(struct bus_type *bus,
137 static int __iommu_attach_device(struct iommu_domain *domain,
139 static int __iommu_attach_group(struct iommu_domain *domain,
140 struct iommu_group *group);
141 static void __iommu_detach_group(struct iommu_domain *domain,
142 struct iommu_group *group);
144 static int __init iommu_set_def_domain_type(char *str)
149 ret = kstrtobool(str, &pt);
153 iommu_def_domain_type = pt ? IOMMU_DOMAIN_IDENTITY : IOMMU_DOMAIN_DMA;
156 early_param("iommu.passthrough", iommu_set_def_domain_type);
158 static int __init iommu_dma_setup(char *str)
160 return kstrtobool(str, &iommu_dma_strict);
162 early_param("iommu.strict", iommu_dma_setup);
164 static ssize_t iommu_group_attr_show(struct kobject *kobj,
165 struct attribute *__attr, char *buf)
167 struct iommu_group_attribute *attr = to_iommu_group_attr(__attr);
168 struct iommu_group *group = to_iommu_group(kobj);
172 ret = attr->show(group, buf);
176 static ssize_t iommu_group_attr_store(struct kobject *kobj,
177 struct attribute *__attr,
178 const char *buf, size_t count)
180 struct iommu_group_attribute *attr = to_iommu_group_attr(__attr);
181 struct iommu_group *group = to_iommu_group(kobj);
185 ret = attr->store(group, buf, count);
189 static const struct sysfs_ops iommu_group_sysfs_ops = {
190 .show = iommu_group_attr_show,
191 .store = iommu_group_attr_store,
194 static int iommu_group_create_file(struct iommu_group *group,
195 struct iommu_group_attribute *attr)
197 return sysfs_create_file(&group->kobj, &attr->attr);
200 static void iommu_group_remove_file(struct iommu_group *group,
201 struct iommu_group_attribute *attr)
203 sysfs_remove_file(&group->kobj, &attr->attr);
206 static ssize_t iommu_group_show_name(struct iommu_group *group, char *buf)
208 return sprintf(buf, "%s\n", group->name);
212 * iommu_insert_resv_region - Insert a new region in the
213 * list of reserved regions.
214 * @new: new region to insert
215 * @regions: list of regions
217 * The new element is sorted by address with respect to the other
218 * regions of the same type. In case it overlaps with another
219 * region of the same type, regions are merged. In case it
220 * overlaps with another region of different type, regions are
223 static int iommu_insert_resv_region(struct iommu_resv_region *new,
224 struct list_head *regions)
226 struct iommu_resv_region *region;
227 phys_addr_t start = new->start;
228 phys_addr_t end = new->start + new->length - 1;
229 struct list_head *pos = regions->next;
231 while (pos != regions) {
232 struct iommu_resv_region *entry =
233 list_entry(pos, struct iommu_resv_region, list);
234 phys_addr_t a = entry->start;
235 phys_addr_t b = entry->start + entry->length - 1;
236 int type = entry->type;
240 } else if (start > b) {
242 } else if ((start >= a) && (end <= b)) {
243 if (new->type == type)
248 if (new->type == type) {
249 phys_addr_t new_start = min(a, start);
250 phys_addr_t new_end = max(b, end);
252 list_del(&entry->list);
253 entry->start = new_start;
254 entry->length = new_end - new_start + 1;
255 iommu_insert_resv_region(entry, regions);
262 region = iommu_alloc_resv_region(new->start, new->length,
263 new->prot, new->type);
267 list_add_tail(®ion->list, pos);
273 iommu_insert_device_resv_regions(struct list_head *dev_resv_regions,
274 struct list_head *group_resv_regions)
276 struct iommu_resv_region *entry;
279 list_for_each_entry(entry, dev_resv_regions, list) {
280 ret = iommu_insert_resv_region(entry, group_resv_regions);
287 int iommu_get_group_resv_regions(struct iommu_group *group,
288 struct list_head *head)
290 struct group_device *device;
293 mutex_lock(&group->mutex);
294 list_for_each_entry(device, &group->devices, list) {
295 struct list_head dev_resv_regions;
297 INIT_LIST_HEAD(&dev_resv_regions);
298 iommu_get_resv_regions(device->dev, &dev_resv_regions);
299 ret = iommu_insert_device_resv_regions(&dev_resv_regions, head);
300 iommu_put_resv_regions(device->dev, &dev_resv_regions);
304 mutex_unlock(&group->mutex);
307 EXPORT_SYMBOL_GPL(iommu_get_group_resv_regions);
309 static ssize_t iommu_group_show_resv_regions(struct iommu_group *group,
312 struct iommu_resv_region *region, *next;
313 struct list_head group_resv_regions;
316 INIT_LIST_HEAD(&group_resv_regions);
317 iommu_get_group_resv_regions(group, &group_resv_regions);
319 list_for_each_entry_safe(region, next, &group_resv_regions, list) {
320 str += sprintf(str, "0x%016llx 0x%016llx %s\n",
321 (long long int)region->start,
322 (long long int)(region->start +
324 iommu_group_resv_type_string[region->type]);
331 static ssize_t iommu_group_show_type(struct iommu_group *group,
334 char *type = "unknown\n";
336 if (group->default_domain) {
337 switch (group->default_domain->type) {
338 case IOMMU_DOMAIN_BLOCKED:
341 case IOMMU_DOMAIN_IDENTITY:
344 case IOMMU_DOMAIN_UNMANAGED:
345 type = "unmanaged\n";
347 case IOMMU_DOMAIN_DMA:
357 static IOMMU_GROUP_ATTR(name, S_IRUGO, iommu_group_show_name, NULL);
359 static IOMMU_GROUP_ATTR(reserved_regions, 0444,
360 iommu_group_show_resv_regions, NULL);
362 static IOMMU_GROUP_ATTR(type, 0444, iommu_group_show_type, NULL);
364 static void iommu_group_release(struct kobject *kobj)
366 struct iommu_group *group = to_iommu_group(kobj);
368 pr_debug("Releasing group %d\n", group->id);
370 if (group->iommu_data_release)
371 group->iommu_data_release(group->iommu_data);
373 ida_simple_remove(&iommu_group_ida, group->id);
375 if (group->default_domain)
376 iommu_domain_free(group->default_domain);
382 static struct kobj_type iommu_group_ktype = {
383 .sysfs_ops = &iommu_group_sysfs_ops,
384 .release = iommu_group_release,
388 * iommu_group_alloc - Allocate a new group
390 * This function is called by an iommu driver to allocate a new iommu
391 * group. The iommu group represents the minimum granularity of the iommu.
392 * Upon successful return, the caller holds a reference to the supplied
393 * group in order to hold the group until devices are added. Use
394 * iommu_group_put() to release this extra reference count, allowing the
395 * group to be automatically reclaimed once it has no devices or external
398 struct iommu_group *iommu_group_alloc(void)
400 struct iommu_group *group;
403 group = kzalloc(sizeof(*group), GFP_KERNEL);
405 return ERR_PTR(-ENOMEM);
407 group->kobj.kset = iommu_group_kset;
408 mutex_init(&group->mutex);
409 INIT_LIST_HEAD(&group->devices);
410 BLOCKING_INIT_NOTIFIER_HEAD(&group->notifier);
412 ret = ida_simple_get(&iommu_group_ida, 0, 0, GFP_KERNEL);
419 ret = kobject_init_and_add(&group->kobj, &iommu_group_ktype,
420 NULL, "%d", group->id);
422 ida_simple_remove(&iommu_group_ida, group->id);
427 group->devices_kobj = kobject_create_and_add("devices", &group->kobj);
428 if (!group->devices_kobj) {
429 kobject_put(&group->kobj); /* triggers .release & free */
430 return ERR_PTR(-ENOMEM);
434 * The devices_kobj holds a reference on the group kobject, so
435 * as long as that exists so will the group. We can therefore
436 * use the devices_kobj for reference counting.
438 kobject_put(&group->kobj);
440 ret = iommu_group_create_file(group,
441 &iommu_group_attr_reserved_regions);
445 ret = iommu_group_create_file(group, &iommu_group_attr_type);
449 pr_debug("Allocated group %d\n", group->id);
453 EXPORT_SYMBOL_GPL(iommu_group_alloc);
455 struct iommu_group *iommu_group_get_by_id(int id)
457 struct kobject *group_kobj;
458 struct iommu_group *group;
461 if (!iommu_group_kset)
464 name = kasprintf(GFP_KERNEL, "%d", id);
468 group_kobj = kset_find_obj(iommu_group_kset, name);
474 group = container_of(group_kobj, struct iommu_group, kobj);
475 BUG_ON(group->id != id);
477 kobject_get(group->devices_kobj);
478 kobject_put(&group->kobj);
482 EXPORT_SYMBOL_GPL(iommu_group_get_by_id);
485 * iommu_group_get_iommudata - retrieve iommu_data registered for a group
488 * iommu drivers can store data in the group for use when doing iommu
489 * operations. This function provides a way to retrieve it. Caller
490 * should hold a group reference.
492 void *iommu_group_get_iommudata(struct iommu_group *group)
494 return group->iommu_data;
496 EXPORT_SYMBOL_GPL(iommu_group_get_iommudata);
499 * iommu_group_set_iommudata - set iommu_data for a group
501 * @iommu_data: new data
502 * @release: release function for iommu_data
504 * iommu drivers can store data in the group for use when doing iommu
505 * operations. This function provides a way to set the data after
506 * the group has been allocated. Caller should hold a group reference.
508 void iommu_group_set_iommudata(struct iommu_group *group, void *iommu_data,
509 void (*release)(void *iommu_data))
511 group->iommu_data = iommu_data;
512 group->iommu_data_release = release;
514 EXPORT_SYMBOL_GPL(iommu_group_set_iommudata);
517 * iommu_group_set_name - set name for a group
521 * Allow iommu driver to set a name for a group. When set it will
522 * appear in a name attribute file under the group in sysfs.
524 int iommu_group_set_name(struct iommu_group *group, const char *name)
529 iommu_group_remove_file(group, &iommu_group_attr_name);
536 group->name = kstrdup(name, GFP_KERNEL);
540 ret = iommu_group_create_file(group, &iommu_group_attr_name);
549 EXPORT_SYMBOL_GPL(iommu_group_set_name);
551 static int iommu_group_create_direct_mappings(struct iommu_group *group,
554 struct iommu_domain *domain = group->default_domain;
555 struct iommu_resv_region *entry;
556 struct list_head mappings;
557 unsigned long pg_size;
560 if (!domain || domain->type != IOMMU_DOMAIN_DMA)
563 BUG_ON(!domain->pgsize_bitmap);
565 pg_size = 1UL << __ffs(domain->pgsize_bitmap);
566 INIT_LIST_HEAD(&mappings);
568 iommu_get_resv_regions(dev, &mappings);
570 /* We need to consider overlapping regions for different devices */
571 list_for_each_entry(entry, &mappings, list) {
572 dma_addr_t start, end, addr;
574 if (domain->ops->apply_resv_region)
575 domain->ops->apply_resv_region(dev, domain, entry);
577 start = ALIGN(entry->start, pg_size);
578 end = ALIGN(entry->start + entry->length, pg_size);
580 if (entry->type != IOMMU_RESV_DIRECT)
583 for (addr = start; addr < end; addr += pg_size) {
584 phys_addr_t phys_addr;
586 phys_addr = iommu_iova_to_phys(domain, addr);
590 ret = iommu_map(domain, addr, addr, pg_size, entry->prot);
597 iommu_flush_tlb_all(domain);
600 iommu_put_resv_regions(dev, &mappings);
606 * iommu_group_add_device - add a device to an iommu group
607 * @group: the group into which to add the device (reference should be held)
610 * This function is called by an iommu driver to add a device into a
611 * group. Adding a device increments the group reference count.
613 int iommu_group_add_device(struct iommu_group *group, struct device *dev)
616 struct group_device *device;
618 device = kzalloc(sizeof(*device), GFP_KERNEL);
624 ret = sysfs_create_link(&dev->kobj, &group->kobj, "iommu_group");
626 goto err_free_device;
628 device->name = kasprintf(GFP_KERNEL, "%s", kobject_name(&dev->kobj));
632 goto err_remove_link;
635 ret = sysfs_create_link_nowarn(group->devices_kobj,
636 &dev->kobj, device->name);
638 if (ret == -EEXIST && i >= 0) {
640 * Account for the slim chance of collision
641 * and append an instance to the name.
644 device->name = kasprintf(GFP_KERNEL, "%s.%d",
645 kobject_name(&dev->kobj), i++);
651 kobject_get(group->devices_kobj);
653 dev->iommu_group = group;
655 iommu_group_create_direct_mappings(group, dev);
657 mutex_lock(&group->mutex);
658 list_add_tail(&device->list, &group->devices);
660 ret = __iommu_attach_device(group->domain, dev);
661 mutex_unlock(&group->mutex);
665 /* Notify any listeners about change to group. */
666 blocking_notifier_call_chain(&group->notifier,
667 IOMMU_GROUP_NOTIFY_ADD_DEVICE, dev);
669 trace_add_device_to_group(group->id, dev);
671 pr_info("Adding device %s to group %d\n", dev_name(dev), group->id);
676 mutex_lock(&group->mutex);
677 list_del(&device->list);
678 mutex_unlock(&group->mutex);
679 dev->iommu_group = NULL;
680 kobject_put(group->devices_kobj);
684 sysfs_remove_link(&dev->kobj, "iommu_group");
687 pr_err("Failed to add device %s to group %d: %d\n", dev_name(dev), group->id, ret);
690 EXPORT_SYMBOL_GPL(iommu_group_add_device);
693 * iommu_group_remove_device - remove a device from it's current group
694 * @dev: device to be removed
696 * This function is called by an iommu driver to remove the device from
697 * it's current group. This decrements the iommu group reference count.
699 void iommu_group_remove_device(struct device *dev)
701 struct iommu_group *group = dev->iommu_group;
702 struct group_device *tmp_device, *device = NULL;
704 pr_info("Removing device %s from group %d\n", dev_name(dev), group->id);
706 /* Pre-notify listeners that a device is being removed. */
707 blocking_notifier_call_chain(&group->notifier,
708 IOMMU_GROUP_NOTIFY_DEL_DEVICE, dev);
710 mutex_lock(&group->mutex);
711 list_for_each_entry(tmp_device, &group->devices, list) {
712 if (tmp_device->dev == dev) {
714 list_del(&device->list);
718 mutex_unlock(&group->mutex);
723 sysfs_remove_link(group->devices_kobj, device->name);
724 sysfs_remove_link(&dev->kobj, "iommu_group");
726 trace_remove_device_from_group(group->id, dev);
730 dev->iommu_group = NULL;
731 kobject_put(group->devices_kobj);
733 EXPORT_SYMBOL_GPL(iommu_group_remove_device);
735 static int iommu_group_device_count(struct iommu_group *group)
737 struct group_device *entry;
740 list_for_each_entry(entry, &group->devices, list)
747 * iommu_group_for_each_dev - iterate over each device in the group
749 * @data: caller opaque data to be passed to callback function
750 * @fn: caller supplied callback function
752 * This function is called by group users to iterate over group devices.
753 * Callers should hold a reference count to the group during callback.
754 * The group->mutex is held across callbacks, which will block calls to
755 * iommu_group_add/remove_device.
757 static int __iommu_group_for_each_dev(struct iommu_group *group, void *data,
758 int (*fn)(struct device *, void *))
760 struct group_device *device;
763 list_for_each_entry(device, &group->devices, list) {
764 ret = fn(device->dev, data);
772 int iommu_group_for_each_dev(struct iommu_group *group, void *data,
773 int (*fn)(struct device *, void *))
777 mutex_lock(&group->mutex);
778 ret = __iommu_group_for_each_dev(group, data, fn);
779 mutex_unlock(&group->mutex);
783 EXPORT_SYMBOL_GPL(iommu_group_for_each_dev);
786 * iommu_group_get - Return the group for a device and increment reference
787 * @dev: get the group that this device belongs to
789 * This function is called by iommu drivers and users to get the group
790 * for the specified device. If found, the group is returned and the group
791 * reference in incremented, else NULL.
793 struct iommu_group *iommu_group_get(struct device *dev)
795 struct iommu_group *group = dev->iommu_group;
798 kobject_get(group->devices_kobj);
802 EXPORT_SYMBOL_GPL(iommu_group_get);
805 * iommu_group_ref_get - Increment reference on a group
806 * @group: the group to use, must not be NULL
808 * This function is called by iommu drivers to take additional references on an
809 * existing group. Returns the given group for convenience.
811 struct iommu_group *iommu_group_ref_get(struct iommu_group *group)
813 kobject_get(group->devices_kobj);
818 * iommu_group_put - Decrement group reference
819 * @group: the group to use
821 * This function is called by iommu drivers and users to release the
822 * iommu group. Once the reference count is zero, the group is released.
824 void iommu_group_put(struct iommu_group *group)
827 kobject_put(group->devices_kobj);
829 EXPORT_SYMBOL_GPL(iommu_group_put);
832 * iommu_group_register_notifier - Register a notifier for group changes
833 * @group: the group to watch
834 * @nb: notifier block to signal
836 * This function allows iommu group users to track changes in a group.
837 * See include/linux/iommu.h for actions sent via this notifier. Caller
838 * should hold a reference to the group throughout notifier registration.
840 int iommu_group_register_notifier(struct iommu_group *group,
841 struct notifier_block *nb)
843 return blocking_notifier_chain_register(&group->notifier, nb);
845 EXPORT_SYMBOL_GPL(iommu_group_register_notifier);
848 * iommu_group_unregister_notifier - Unregister a notifier
849 * @group: the group to watch
850 * @nb: notifier block to signal
852 * Unregister a previously registered group notifier block.
854 int iommu_group_unregister_notifier(struct iommu_group *group,
855 struct notifier_block *nb)
857 return blocking_notifier_chain_unregister(&group->notifier, nb);
859 EXPORT_SYMBOL_GPL(iommu_group_unregister_notifier);
862 * iommu_group_id - Return ID for a group
863 * @group: the group to ID
865 * Return the unique ID for the group matching the sysfs group number.
867 int iommu_group_id(struct iommu_group *group)
871 EXPORT_SYMBOL_GPL(iommu_group_id);
873 static struct iommu_group *get_pci_alias_group(struct pci_dev *pdev,
874 unsigned long *devfns);
877 * To consider a PCI device isolated, we require ACS to support Source
878 * Validation, Request Redirection, Completer Redirection, and Upstream
879 * Forwarding. This effectively means that devices cannot spoof their
880 * requester ID, requests and completions cannot be redirected, and all
881 * transactions are forwarded upstream, even as it passes through a
882 * bridge where the target device is downstream.
884 #define REQ_ACS_FLAGS (PCI_ACS_SV | PCI_ACS_RR | PCI_ACS_CR | PCI_ACS_UF)
887 * For multifunction devices which are not isolated from each other, find
888 * all the other non-isolated functions and look for existing groups. For
889 * each function, we also need to look for aliases to or from other devices
890 * that may already have a group.
892 static struct iommu_group *get_pci_function_alias_group(struct pci_dev *pdev,
893 unsigned long *devfns)
895 struct pci_dev *tmp = NULL;
896 struct iommu_group *group;
898 if (!pdev->multifunction || pci_acs_enabled(pdev, REQ_ACS_FLAGS))
901 for_each_pci_dev(tmp) {
902 if (tmp == pdev || tmp->bus != pdev->bus ||
903 PCI_SLOT(tmp->devfn) != PCI_SLOT(pdev->devfn) ||
904 pci_acs_enabled(tmp, REQ_ACS_FLAGS))
907 group = get_pci_alias_group(tmp, devfns);
918 * Look for aliases to or from the given device for existing groups. DMA
919 * aliases are only supported on the same bus, therefore the search
920 * space is quite small (especially since we're really only looking at pcie
921 * device, and therefore only expect multiple slots on the root complex or
922 * downstream switch ports). It's conceivable though that a pair of
923 * multifunction devices could have aliases between them that would cause a
924 * loop. To prevent this, we use a bitmap to track where we've been.
926 static struct iommu_group *get_pci_alias_group(struct pci_dev *pdev,
927 unsigned long *devfns)
929 struct pci_dev *tmp = NULL;
930 struct iommu_group *group;
932 if (test_and_set_bit(pdev->devfn & 0xff, devfns))
935 group = iommu_group_get(&pdev->dev);
939 for_each_pci_dev(tmp) {
940 if (tmp == pdev || tmp->bus != pdev->bus)
943 /* We alias them or they alias us */
944 if (pci_devs_are_dma_aliases(pdev, tmp)) {
945 group = get_pci_alias_group(tmp, devfns);
951 group = get_pci_function_alias_group(tmp, devfns);
962 struct group_for_pci_data {
963 struct pci_dev *pdev;
964 struct iommu_group *group;
968 * DMA alias iterator callback, return the last seen device. Stop and return
969 * the IOMMU group if we find one along the way.
971 static int get_pci_alias_or_group(struct pci_dev *pdev, u16 alias, void *opaque)
973 struct group_for_pci_data *data = opaque;
976 data->group = iommu_group_get(&pdev->dev);
978 return data->group != NULL;
982 * Generic device_group call-back function. It just allocates one
983 * iommu-group per device.
985 struct iommu_group *generic_device_group(struct device *dev)
987 return iommu_group_alloc();
991 * Use standard PCI bus topology, isolation features, and DMA alias quirks
992 * to find or create an IOMMU group for a device.
994 struct iommu_group *pci_device_group(struct device *dev)
996 struct pci_dev *pdev = to_pci_dev(dev);
997 struct group_for_pci_data data;
999 struct iommu_group *group = NULL;
1000 u64 devfns[4] = { 0 };
1002 if (WARN_ON(!dev_is_pci(dev)))
1003 return ERR_PTR(-EINVAL);
1006 * Find the upstream DMA alias for the device. A device must not
1007 * be aliased due to topology in order to have its own IOMMU group.
1008 * If we find an alias along the way that already belongs to a
1011 if (pci_for_each_dma_alias(pdev, get_pci_alias_or_group, &data))
1017 * Continue upstream from the point of minimum IOMMU granularity
1018 * due to aliases to the point where devices are protected from
1019 * peer-to-peer DMA by PCI ACS. Again, if we find an existing
1022 for (bus = pdev->bus; !pci_is_root_bus(bus); bus = bus->parent) {
1026 if (pci_acs_path_enabled(bus->self, NULL, REQ_ACS_FLAGS))
1031 group = iommu_group_get(&pdev->dev);
1037 * Look for existing groups on device aliases. If we alias another
1038 * device or another device aliases us, use the same group.
1040 group = get_pci_alias_group(pdev, (unsigned long *)devfns);
1045 * Look for existing groups on non-isolated functions on the same
1046 * slot and aliases of those funcions, if any. No need to clear
1047 * the search bitmap, the tested devfns are still valid.
1049 group = get_pci_function_alias_group(pdev, (unsigned long *)devfns);
1053 /* No shared group found, allocate new */
1054 return iommu_group_alloc();
1057 /* Get the IOMMU group for device on fsl-mc bus */
1058 struct iommu_group *fsl_mc_device_group(struct device *dev)
1060 struct device *cont_dev = fsl_mc_cont_dev(dev);
1061 struct iommu_group *group;
1063 group = iommu_group_get(cont_dev);
1065 group = iommu_group_alloc();
1070 * iommu_group_get_for_dev - Find or create the IOMMU group for a device
1071 * @dev: target device
1073 * This function is intended to be called by IOMMU drivers and extended to
1074 * support common, bus-defined algorithms when determining or creating the
1075 * IOMMU group for a device. On success, the caller will hold a reference
1076 * to the returned IOMMU group, which will already include the provided
1077 * device. The reference should be released with iommu_group_put().
1079 struct iommu_group *iommu_group_get_for_dev(struct device *dev)
1081 const struct iommu_ops *ops = dev->bus->iommu_ops;
1082 struct iommu_group *group;
1085 group = iommu_group_get(dev);
1090 return ERR_PTR(-EINVAL);
1092 group = ops->device_group(dev);
1093 if (WARN_ON_ONCE(group == NULL))
1094 return ERR_PTR(-EINVAL);
1100 * Try to allocate a default domain - needs support from the
1103 if (!group->default_domain) {
1104 struct iommu_domain *dom;
1106 dom = __iommu_domain_alloc(dev->bus, iommu_def_domain_type);
1107 if (!dom && iommu_def_domain_type != IOMMU_DOMAIN_DMA) {
1109 "failed to allocate default IOMMU domain of type %u; falling back to IOMMU_DOMAIN_DMA",
1110 iommu_def_domain_type);
1111 dom = __iommu_domain_alloc(dev->bus, IOMMU_DOMAIN_DMA);
1114 group->default_domain = dom;
1116 group->domain = dom;
1118 if (dom && !iommu_dma_strict) {
1120 iommu_domain_set_attr(dom,
1121 DOMAIN_ATTR_DMA_USE_FLUSH_QUEUE,
1126 ret = iommu_group_add_device(group, dev);
1128 iommu_group_put(group);
1129 return ERR_PTR(ret);
1135 struct iommu_domain *iommu_group_default_domain(struct iommu_group *group)
1137 return group->default_domain;
1140 static int add_iommu_group(struct device *dev, void *data)
1142 int ret = iommu_probe_device(dev);
1145 * We ignore -ENODEV errors for now, as they just mean that the
1146 * device is not translated by an IOMMU. We still care about
1147 * other errors and fail to initialize when they happen.
1155 static int remove_iommu_group(struct device *dev, void *data)
1157 iommu_release_device(dev);
1162 static int iommu_bus_notifier(struct notifier_block *nb,
1163 unsigned long action, void *data)
1165 unsigned long group_action = 0;
1166 struct device *dev = data;
1167 struct iommu_group *group;
1170 * ADD/DEL call into iommu driver ops if provided, which may
1171 * result in ADD/DEL notifiers to group->notifier
1173 if (action == BUS_NOTIFY_ADD_DEVICE) {
1176 ret = iommu_probe_device(dev);
1177 return (ret) ? NOTIFY_DONE : NOTIFY_OK;
1178 } else if (action == BUS_NOTIFY_REMOVED_DEVICE) {
1179 iommu_release_device(dev);
1184 * Remaining BUS_NOTIFYs get filtered and republished to the
1185 * group, if anyone is listening
1187 group = iommu_group_get(dev);
1192 case BUS_NOTIFY_BIND_DRIVER:
1193 group_action = IOMMU_GROUP_NOTIFY_BIND_DRIVER;
1195 case BUS_NOTIFY_BOUND_DRIVER:
1196 group_action = IOMMU_GROUP_NOTIFY_BOUND_DRIVER;
1198 case BUS_NOTIFY_UNBIND_DRIVER:
1199 group_action = IOMMU_GROUP_NOTIFY_UNBIND_DRIVER;
1201 case BUS_NOTIFY_UNBOUND_DRIVER:
1202 group_action = IOMMU_GROUP_NOTIFY_UNBOUND_DRIVER;
1207 blocking_notifier_call_chain(&group->notifier,
1210 iommu_group_put(group);
1214 static int iommu_bus_init(struct bus_type *bus, const struct iommu_ops *ops)
1217 struct notifier_block *nb;
1218 struct iommu_callback_data cb = {
1222 nb = kzalloc(sizeof(struct notifier_block), GFP_KERNEL);
1226 nb->notifier_call = iommu_bus_notifier;
1228 err = bus_register_notifier(bus, nb);
1232 err = bus_for_each_dev(bus, NULL, &cb, add_iommu_group);
1241 bus_for_each_dev(bus, NULL, &cb, remove_iommu_group);
1242 bus_unregister_notifier(bus, nb);
1251 * bus_set_iommu - set iommu-callbacks for the bus
1253 * @ops: the callbacks provided by the iommu-driver
1255 * This function is called by an iommu driver to set the iommu methods
1256 * used for a particular bus. Drivers for devices on that bus can use
1257 * the iommu-api after these ops are registered.
1258 * This special function is needed because IOMMUs are usually devices on
1259 * the bus itself, so the iommu drivers are not initialized when the bus
1260 * is set up. With this function the iommu-driver can set the iommu-ops
1263 int bus_set_iommu(struct bus_type *bus, const struct iommu_ops *ops)
1267 if (bus->iommu_ops != NULL)
1270 bus->iommu_ops = ops;
1272 /* Do IOMMU specific setup for this bus-type */
1273 err = iommu_bus_init(bus, ops);
1275 bus->iommu_ops = NULL;
1279 EXPORT_SYMBOL_GPL(bus_set_iommu);
1281 bool iommu_present(struct bus_type *bus)
1283 return bus->iommu_ops != NULL;
1285 EXPORT_SYMBOL_GPL(iommu_present);
1287 bool iommu_capable(struct bus_type *bus, enum iommu_cap cap)
1289 if (!bus->iommu_ops || !bus->iommu_ops->capable)
1292 return bus->iommu_ops->capable(cap);
1294 EXPORT_SYMBOL_GPL(iommu_capable);
1297 * iommu_set_fault_handler() - set a fault handler for an iommu domain
1298 * @domain: iommu domain
1299 * @handler: fault handler
1300 * @token: user data, will be passed back to the fault handler
1302 * This function should be used by IOMMU users which want to be notified
1303 * whenever an IOMMU fault happens.
1305 * The fault handler itself should return 0 on success, and an appropriate
1306 * error code otherwise.
1308 void iommu_set_fault_handler(struct iommu_domain *domain,
1309 iommu_fault_handler_t handler,
1314 domain->handler = handler;
1315 domain->handler_token = token;
1317 EXPORT_SYMBOL_GPL(iommu_set_fault_handler);
1319 static struct iommu_domain *__iommu_domain_alloc(struct bus_type *bus,
1322 struct iommu_domain *domain;
1324 if (bus == NULL || bus->iommu_ops == NULL)
1327 domain = bus->iommu_ops->domain_alloc(type);
1331 domain->ops = bus->iommu_ops;
1332 domain->type = type;
1333 /* Assume all sizes by default; the driver may override this later */
1334 domain->pgsize_bitmap = bus->iommu_ops->pgsize_bitmap;
1339 struct iommu_domain *iommu_domain_alloc(struct bus_type *bus)
1341 return __iommu_domain_alloc(bus, IOMMU_DOMAIN_UNMANAGED);
1343 EXPORT_SYMBOL_GPL(iommu_domain_alloc);
1345 void iommu_domain_free(struct iommu_domain *domain)
1347 domain->ops->domain_free(domain);
1349 EXPORT_SYMBOL_GPL(iommu_domain_free);
1351 static int __iommu_attach_device(struct iommu_domain *domain,
1355 if ((domain->ops->is_attach_deferred != NULL) &&
1356 domain->ops->is_attach_deferred(domain, dev))
1359 if (unlikely(domain->ops->attach_dev == NULL))
1362 ret = domain->ops->attach_dev(domain, dev);
1364 trace_attach_device_to_domain(dev);
1368 int iommu_attach_device(struct iommu_domain *domain, struct device *dev)
1370 struct iommu_group *group;
1373 group = iommu_group_get(dev);
1378 * Lock the group to make sure the device-count doesn't
1379 * change while we are attaching
1381 mutex_lock(&group->mutex);
1383 if (iommu_group_device_count(group) != 1)
1386 ret = __iommu_attach_group(domain, group);
1389 mutex_unlock(&group->mutex);
1390 iommu_group_put(group);
1394 EXPORT_SYMBOL_GPL(iommu_attach_device);
1396 static void __iommu_detach_device(struct iommu_domain *domain,
1399 if ((domain->ops->is_attach_deferred != NULL) &&
1400 domain->ops->is_attach_deferred(domain, dev))
1403 if (unlikely(domain->ops->detach_dev == NULL))
1406 domain->ops->detach_dev(domain, dev);
1407 trace_detach_device_from_domain(dev);
1410 void iommu_detach_device(struct iommu_domain *domain, struct device *dev)
1412 struct iommu_group *group;
1414 group = iommu_group_get(dev);
1418 mutex_lock(&group->mutex);
1419 if (iommu_group_device_count(group) != 1) {
1424 __iommu_detach_group(domain, group);
1427 mutex_unlock(&group->mutex);
1428 iommu_group_put(group);
1430 EXPORT_SYMBOL_GPL(iommu_detach_device);
1432 struct iommu_domain *iommu_get_domain_for_dev(struct device *dev)
1434 struct iommu_domain *domain;
1435 struct iommu_group *group;
1437 group = iommu_group_get(dev);
1441 domain = group->domain;
1443 iommu_group_put(group);
1447 EXPORT_SYMBOL_GPL(iommu_get_domain_for_dev);
1450 * For IOMMU_DOMAIN_DMA implementations which already provide their own
1451 * guarantees that the group and its default domain are valid and correct.
1453 struct iommu_domain *iommu_get_dma_domain(struct device *dev)
1455 return dev->iommu_group->default_domain;
1459 * IOMMU groups are really the natural working unit of the IOMMU, but
1460 * the IOMMU API works on domains and devices. Bridge that gap by
1461 * iterating over the devices in a group. Ideally we'd have a single
1462 * device which represents the requestor ID of the group, but we also
1463 * allow IOMMU drivers to create policy defined minimum sets, where
1464 * the physical hardware may be able to distiguish members, but we
1465 * wish to group them at a higher level (ex. untrusted multi-function
1466 * PCI devices). Thus we attach each device.
1468 static int iommu_group_do_attach_device(struct device *dev, void *data)
1470 struct iommu_domain *domain = data;
1472 return __iommu_attach_device(domain, dev);
1475 static int __iommu_attach_group(struct iommu_domain *domain,
1476 struct iommu_group *group)
1480 if (group->default_domain && group->domain != group->default_domain)
1483 ret = __iommu_group_for_each_dev(group, domain,
1484 iommu_group_do_attach_device);
1486 group->domain = domain;
1491 int iommu_attach_group(struct iommu_domain *domain, struct iommu_group *group)
1495 mutex_lock(&group->mutex);
1496 ret = __iommu_attach_group(domain, group);
1497 mutex_unlock(&group->mutex);
1501 EXPORT_SYMBOL_GPL(iommu_attach_group);
1503 static int iommu_group_do_detach_device(struct device *dev, void *data)
1505 struct iommu_domain *domain = data;
1507 __iommu_detach_device(domain, dev);
1512 static void __iommu_detach_group(struct iommu_domain *domain,
1513 struct iommu_group *group)
1517 if (!group->default_domain) {
1518 __iommu_group_for_each_dev(group, domain,
1519 iommu_group_do_detach_device);
1520 group->domain = NULL;
1524 if (group->domain == group->default_domain)
1527 /* Detach by re-attaching to the default domain */
1528 ret = __iommu_group_for_each_dev(group, group->default_domain,
1529 iommu_group_do_attach_device);
1533 group->domain = group->default_domain;
1536 void iommu_detach_group(struct iommu_domain *domain, struct iommu_group *group)
1538 mutex_lock(&group->mutex);
1539 __iommu_detach_group(domain, group);
1540 mutex_unlock(&group->mutex);
1542 EXPORT_SYMBOL_GPL(iommu_detach_group);
1544 phys_addr_t iommu_iova_to_phys(struct iommu_domain *domain, dma_addr_t iova)
1546 if (unlikely(domain->ops->iova_to_phys == NULL))
1549 return domain->ops->iova_to_phys(domain, iova);
1551 EXPORT_SYMBOL_GPL(iommu_iova_to_phys);
1553 static size_t iommu_pgsize(struct iommu_domain *domain,
1554 unsigned long addr_merge, size_t size)
1556 unsigned int pgsize_idx;
1559 /* Max page size that still fits into 'size' */
1560 pgsize_idx = __fls(size);
1562 /* need to consider alignment requirements ? */
1563 if (likely(addr_merge)) {
1564 /* Max page size allowed by address */
1565 unsigned int align_pgsize_idx = __ffs(addr_merge);
1566 pgsize_idx = min(pgsize_idx, align_pgsize_idx);
1569 /* build a mask of acceptable page sizes */
1570 pgsize = (1UL << (pgsize_idx + 1)) - 1;
1572 /* throw away page sizes not supported by the hardware */
1573 pgsize &= domain->pgsize_bitmap;
1575 /* make sure we're still sane */
1578 /* pick the biggest page */
1579 pgsize_idx = __fls(pgsize);
1580 pgsize = 1UL << pgsize_idx;
1585 int iommu_map(struct iommu_domain *domain, unsigned long iova,
1586 phys_addr_t paddr, size_t size, int prot)
1588 unsigned long orig_iova = iova;
1589 unsigned int min_pagesz;
1590 size_t orig_size = size;
1591 phys_addr_t orig_paddr = paddr;
1594 if (unlikely(domain->ops->map == NULL ||
1595 domain->pgsize_bitmap == 0UL))
1598 if (unlikely(!(domain->type & __IOMMU_DOMAIN_PAGING)))
1601 /* find out the minimum page size supported */
1602 min_pagesz = 1 << __ffs(domain->pgsize_bitmap);
1605 * both the virtual address and the physical one, as well as
1606 * the size of the mapping, must be aligned (at least) to the
1607 * size of the smallest page supported by the hardware
1609 if (!IS_ALIGNED(iova | paddr | size, min_pagesz)) {
1610 pr_err("unaligned: iova 0x%lx pa %pa size 0x%zx min_pagesz 0x%x\n",
1611 iova, &paddr, size, min_pagesz);
1615 pr_debug("map: iova 0x%lx pa %pa size 0x%zx\n", iova, &paddr, size);
1618 size_t pgsize = iommu_pgsize(domain, iova | paddr, size);
1620 pr_debug("mapping: iova 0x%lx pa %pa pgsize 0x%zx\n",
1621 iova, &paddr, pgsize);
1623 ret = domain->ops->map(domain, iova, paddr, pgsize, prot);
1632 /* unroll mapping in case something went wrong */
1634 iommu_unmap(domain, orig_iova, orig_size - size);
1636 trace_map(orig_iova, orig_paddr, orig_size);
1640 EXPORT_SYMBOL_GPL(iommu_map);
1642 static size_t __iommu_unmap(struct iommu_domain *domain,
1643 unsigned long iova, size_t size,
1646 const struct iommu_ops *ops = domain->ops;
1647 size_t unmapped_page, unmapped = 0;
1648 unsigned long orig_iova = iova;
1649 unsigned int min_pagesz;
1651 if (unlikely(ops->unmap == NULL ||
1652 domain->pgsize_bitmap == 0UL))
1655 if (unlikely(!(domain->type & __IOMMU_DOMAIN_PAGING)))
1658 /* find out the minimum page size supported */
1659 min_pagesz = 1 << __ffs(domain->pgsize_bitmap);
1662 * The virtual address, as well as the size of the mapping, must be
1663 * aligned (at least) to the size of the smallest page supported
1666 if (!IS_ALIGNED(iova | size, min_pagesz)) {
1667 pr_err("unaligned: iova 0x%lx size 0x%zx min_pagesz 0x%x\n",
1668 iova, size, min_pagesz);
1672 pr_debug("unmap this: iova 0x%lx size 0x%zx\n", iova, size);
1675 * Keep iterating until we either unmap 'size' bytes (or more)
1676 * or we hit an area that isn't mapped.
1678 while (unmapped < size) {
1679 size_t pgsize = iommu_pgsize(domain, iova, size - unmapped);
1681 unmapped_page = ops->unmap(domain, iova, pgsize);
1685 if (sync && ops->iotlb_range_add)
1686 ops->iotlb_range_add(domain, iova, pgsize);
1688 pr_debug("unmapped: iova 0x%lx size 0x%zx\n",
1689 iova, unmapped_page);
1691 iova += unmapped_page;
1692 unmapped += unmapped_page;
1695 if (sync && ops->iotlb_sync)
1696 ops->iotlb_sync(domain);
1698 trace_unmap(orig_iova, size, unmapped);
1702 size_t iommu_unmap(struct iommu_domain *domain,
1703 unsigned long iova, size_t size)
1705 return __iommu_unmap(domain, iova, size, true);
1707 EXPORT_SYMBOL_GPL(iommu_unmap);
1709 size_t iommu_unmap_fast(struct iommu_domain *domain,
1710 unsigned long iova, size_t size)
1712 return __iommu_unmap(domain, iova, size, false);
1714 EXPORT_SYMBOL_GPL(iommu_unmap_fast);
1716 size_t iommu_map_sg(struct iommu_domain *domain, unsigned long iova,
1717 struct scatterlist *sg, unsigned int nents, int prot)
1719 size_t len = 0, mapped = 0;
1724 while (i <= nents) {
1725 phys_addr_t s_phys = sg_phys(sg);
1727 if (len && s_phys != start + len) {
1728 ret = iommu_map(domain, iova + mapped, start, len, prot);
1750 /* undo mappings already done */
1751 iommu_unmap(domain, iova, mapped);
1756 EXPORT_SYMBOL_GPL(iommu_map_sg);
1758 int iommu_domain_window_enable(struct iommu_domain *domain, u32 wnd_nr,
1759 phys_addr_t paddr, u64 size, int prot)
1761 if (unlikely(domain->ops->domain_window_enable == NULL))
1764 return domain->ops->domain_window_enable(domain, wnd_nr, paddr, size,
1767 EXPORT_SYMBOL_GPL(iommu_domain_window_enable);
1769 void iommu_domain_window_disable(struct iommu_domain *domain, u32 wnd_nr)
1771 if (unlikely(domain->ops->domain_window_disable == NULL))
1774 return domain->ops->domain_window_disable(domain, wnd_nr);
1776 EXPORT_SYMBOL_GPL(iommu_domain_window_disable);
1779 * report_iommu_fault() - report about an IOMMU fault to the IOMMU framework
1780 * @domain: the iommu domain where the fault has happened
1781 * @dev: the device where the fault has happened
1782 * @iova: the faulting address
1783 * @flags: mmu fault flags (e.g. IOMMU_FAULT_READ/IOMMU_FAULT_WRITE/...)
1785 * This function should be called by the low-level IOMMU implementations
1786 * whenever IOMMU faults happen, to allow high-level users, that are
1787 * interested in such events, to know about them.
1789 * This event may be useful for several possible use cases:
1790 * - mere logging of the event
1791 * - dynamic TLB/PTE loading
1792 * - if restarting of the faulting device is required
1794 * Returns 0 on success and an appropriate error code otherwise (if dynamic
1795 * PTE/TLB loading will one day be supported, implementations will be able
1796 * to tell whether it succeeded or not according to this return value).
1798 * Specifically, -ENOSYS is returned if a fault handler isn't installed
1799 * (though fault handlers can also return -ENOSYS, in case they want to
1800 * elicit the default behavior of the IOMMU drivers).
1802 int report_iommu_fault(struct iommu_domain *domain, struct device *dev,
1803 unsigned long iova, int flags)
1808 * if upper layers showed interest and installed a fault handler,
1811 if (domain->handler)
1812 ret = domain->handler(domain, dev, iova, flags,
1813 domain->handler_token);
1815 trace_io_page_fault(dev, iova, flags);
1818 EXPORT_SYMBOL_GPL(report_iommu_fault);
1820 static int __init iommu_init(void)
1822 iommu_group_kset = kset_create_and_add("iommu_groups",
1824 BUG_ON(!iommu_group_kset);
1826 iommu_debugfs_setup();
1830 core_initcall(iommu_init);
1832 int iommu_domain_get_attr(struct iommu_domain *domain,
1833 enum iommu_attr attr, void *data)
1835 struct iommu_domain_geometry *geometry;
1840 case DOMAIN_ATTR_GEOMETRY:
1842 *geometry = domain->geometry;
1845 case DOMAIN_ATTR_PAGING:
1847 *paging = (domain->pgsize_bitmap != 0UL);
1850 if (!domain->ops->domain_get_attr)
1853 ret = domain->ops->domain_get_attr(domain, attr, data);
1858 EXPORT_SYMBOL_GPL(iommu_domain_get_attr);
1860 int iommu_domain_set_attr(struct iommu_domain *domain,
1861 enum iommu_attr attr, void *data)
1867 if (domain->ops->domain_set_attr == NULL)
1870 ret = domain->ops->domain_set_attr(domain, attr, data);
1875 EXPORT_SYMBOL_GPL(iommu_domain_set_attr);
1877 void iommu_get_resv_regions(struct device *dev, struct list_head *list)
1879 const struct iommu_ops *ops = dev->bus->iommu_ops;
1881 if (ops && ops->get_resv_regions)
1882 ops->get_resv_regions(dev, list);
1885 void iommu_put_resv_regions(struct device *dev, struct list_head *list)
1887 const struct iommu_ops *ops = dev->bus->iommu_ops;
1889 if (ops && ops->put_resv_regions)
1890 ops->put_resv_regions(dev, list);
1893 struct iommu_resv_region *iommu_alloc_resv_region(phys_addr_t start,
1894 size_t length, int prot,
1895 enum iommu_resv_type type)
1897 struct iommu_resv_region *region;
1899 region = kzalloc(sizeof(*region), GFP_KERNEL);
1903 INIT_LIST_HEAD(®ion->list);
1904 region->start = start;
1905 region->length = length;
1906 region->prot = prot;
1907 region->type = type;
1911 /* Request that a device is direct mapped by the IOMMU */
1912 int iommu_request_dm_for_dev(struct device *dev)
1914 struct iommu_domain *dm_domain;
1915 struct iommu_group *group;
1918 /* Device must already be in a group before calling this function */
1919 group = iommu_group_get_for_dev(dev);
1921 return PTR_ERR(group);
1923 mutex_lock(&group->mutex);
1925 /* Check if the default domain is already direct mapped */
1927 if (group->default_domain &&
1928 group->default_domain->type == IOMMU_DOMAIN_IDENTITY)
1931 /* Don't change mappings of existing devices */
1933 if (iommu_group_device_count(group) != 1)
1936 /* Allocate a direct mapped domain */
1938 dm_domain = __iommu_domain_alloc(dev->bus, IOMMU_DOMAIN_IDENTITY);
1942 /* Attach the device to the domain */
1943 ret = __iommu_attach_group(dm_domain, group);
1945 iommu_domain_free(dm_domain);
1949 /* Make the direct mapped domain the default for this group */
1950 if (group->default_domain)
1951 iommu_domain_free(group->default_domain);
1952 group->default_domain = dm_domain;
1954 pr_info("Using direct mapping for device %s\n", dev_name(dev));
1958 mutex_unlock(&group->mutex);
1959 iommu_group_put(group);
1964 const struct iommu_ops *iommu_ops_from_fwnode(struct fwnode_handle *fwnode)
1966 const struct iommu_ops *ops = NULL;
1967 struct iommu_device *iommu;
1969 spin_lock(&iommu_device_lock);
1970 list_for_each_entry(iommu, &iommu_device_list, list)
1971 if (iommu->fwnode == fwnode) {
1975 spin_unlock(&iommu_device_lock);
1979 int iommu_fwspec_init(struct device *dev, struct fwnode_handle *iommu_fwnode,
1980 const struct iommu_ops *ops)
1982 struct iommu_fwspec *fwspec = dev_iommu_fwspec_get(dev);
1985 return ops == fwspec->ops ? 0 : -EINVAL;
1987 fwspec = kzalloc(sizeof(*fwspec), GFP_KERNEL);
1991 of_node_get(to_of_node(iommu_fwnode));
1992 fwspec->iommu_fwnode = iommu_fwnode;
1994 dev_iommu_fwspec_set(dev, fwspec);
1997 EXPORT_SYMBOL_GPL(iommu_fwspec_init);
1999 void iommu_fwspec_free(struct device *dev)
2001 struct iommu_fwspec *fwspec = dev_iommu_fwspec_get(dev);
2004 fwnode_handle_put(fwspec->iommu_fwnode);
2006 dev_iommu_fwspec_set(dev, NULL);
2009 EXPORT_SYMBOL_GPL(iommu_fwspec_free);
2011 int iommu_fwspec_add_ids(struct device *dev, u32 *ids, int num_ids)
2013 struct iommu_fwspec *fwspec = dev_iommu_fwspec_get(dev);
2020 size = offsetof(struct iommu_fwspec, ids[fwspec->num_ids + num_ids]);
2021 if (size > sizeof(*fwspec)) {
2022 fwspec = krealloc(fwspec, size, GFP_KERNEL);
2026 dev_iommu_fwspec_set(dev, fwspec);
2029 for (i = 0; i < num_ids; i++)
2030 fwspec->ids[fwspec->num_ids + i] = ids[i];
2032 fwspec->num_ids += num_ids;
2035 EXPORT_SYMBOL_GPL(iommu_fwspec_add_ids);