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/module.h>
26 #include <linux/slab.h>
27 #include <linux/errno.h>
28 #include <linux/iommu.h>
29 #include <linux/idr.h>
30 #include <linux/notifier.h>
31 #include <linux/err.h>
32 #include <linux/pci.h>
33 #include <linux/bitops.h>
34 #include <linux/property.h>
35 #include <trace/events/iommu.h>
37 static struct kset *iommu_group_kset;
38 static DEFINE_IDA(iommu_group_ida);
40 struct iommu_callback_data {
41 const struct iommu_ops *ops;
46 struct kobject *devices_kobj;
47 struct list_head devices;
49 struct blocking_notifier_head notifier;
51 void (*iommu_data_release)(void *iommu_data);
54 struct iommu_domain *default_domain;
55 struct iommu_domain *domain;
59 struct list_head list;
64 struct iommu_group_attribute {
65 struct attribute attr;
66 ssize_t (*show)(struct iommu_group *group, char *buf);
67 ssize_t (*store)(struct iommu_group *group,
68 const char *buf, size_t count);
71 static const char * const iommu_group_resv_type_string[] = {
72 [IOMMU_RESV_DIRECT] = "direct",
73 [IOMMU_RESV_RESERVED] = "reserved",
74 [IOMMU_RESV_MSI] = "msi",
77 #define IOMMU_GROUP_ATTR(_name, _mode, _show, _store) \
78 struct iommu_group_attribute iommu_group_attr_##_name = \
79 __ATTR(_name, _mode, _show, _store)
81 #define to_iommu_group_attr(_attr) \
82 container_of(_attr, struct iommu_group_attribute, attr)
83 #define to_iommu_group(_kobj) \
84 container_of(_kobj, struct iommu_group, kobj)
86 static struct iommu_domain *__iommu_domain_alloc(struct bus_type *bus,
88 static int __iommu_attach_device(struct iommu_domain *domain,
90 static int __iommu_attach_group(struct iommu_domain *domain,
91 struct iommu_group *group);
92 static void __iommu_detach_group(struct iommu_domain *domain,
93 struct iommu_group *group);
95 static ssize_t iommu_group_attr_show(struct kobject *kobj,
96 struct attribute *__attr, char *buf)
98 struct iommu_group_attribute *attr = to_iommu_group_attr(__attr);
99 struct iommu_group *group = to_iommu_group(kobj);
103 ret = attr->show(group, buf);
107 static ssize_t iommu_group_attr_store(struct kobject *kobj,
108 struct attribute *__attr,
109 const char *buf, size_t count)
111 struct iommu_group_attribute *attr = to_iommu_group_attr(__attr);
112 struct iommu_group *group = to_iommu_group(kobj);
116 ret = attr->store(group, buf, count);
120 static const struct sysfs_ops iommu_group_sysfs_ops = {
121 .show = iommu_group_attr_show,
122 .store = iommu_group_attr_store,
125 static int iommu_group_create_file(struct iommu_group *group,
126 struct iommu_group_attribute *attr)
128 return sysfs_create_file(&group->kobj, &attr->attr);
131 static void iommu_group_remove_file(struct iommu_group *group,
132 struct iommu_group_attribute *attr)
134 sysfs_remove_file(&group->kobj, &attr->attr);
137 static ssize_t iommu_group_show_name(struct iommu_group *group, char *buf)
139 return sprintf(buf, "%s\n", group->name);
143 * iommu_insert_resv_region - Insert a new region in the
144 * list of reserved regions.
145 * @new: new region to insert
146 * @regions: list of regions
148 * The new element is sorted by address with respect to the other
149 * regions of the same type. In case it overlaps with another
150 * region of the same type, regions are merged. In case it
151 * overlaps with another region of different type, regions are
154 static int iommu_insert_resv_region(struct iommu_resv_region *new,
155 struct list_head *regions)
157 struct iommu_resv_region *region;
158 phys_addr_t start = new->start;
159 phys_addr_t end = new->start + new->length - 1;
160 struct list_head *pos = regions->next;
162 while (pos != regions) {
163 struct iommu_resv_region *entry =
164 list_entry(pos, struct iommu_resv_region, list);
165 phys_addr_t a = entry->start;
166 phys_addr_t b = entry->start + entry->length - 1;
167 int type = entry->type;
171 } else if (start > b) {
173 } else if ((start >= a) && (end <= b)) {
174 if (new->type == type)
179 if (new->type == type) {
180 phys_addr_t new_start = min(a, start);
181 phys_addr_t new_end = max(b, end);
183 list_del(&entry->list);
184 entry->start = new_start;
185 entry->length = new_end - new_start + 1;
186 iommu_insert_resv_region(entry, regions);
193 region = iommu_alloc_resv_region(new->start, new->length,
194 new->prot, new->type);
198 list_add_tail(®ion->list, pos);
204 iommu_insert_device_resv_regions(struct list_head *dev_resv_regions,
205 struct list_head *group_resv_regions)
207 struct iommu_resv_region *entry;
210 list_for_each_entry(entry, dev_resv_regions, list) {
211 ret = iommu_insert_resv_region(entry, group_resv_regions);
218 int iommu_get_group_resv_regions(struct iommu_group *group,
219 struct list_head *head)
221 struct iommu_device *device;
224 mutex_lock(&group->mutex);
225 list_for_each_entry(device, &group->devices, list) {
226 struct list_head dev_resv_regions;
228 INIT_LIST_HEAD(&dev_resv_regions);
229 iommu_get_resv_regions(device->dev, &dev_resv_regions);
230 ret = iommu_insert_device_resv_regions(&dev_resv_regions, head);
231 iommu_put_resv_regions(device->dev, &dev_resv_regions);
235 mutex_unlock(&group->mutex);
238 EXPORT_SYMBOL_GPL(iommu_get_group_resv_regions);
240 static ssize_t iommu_group_show_resv_regions(struct iommu_group *group,
243 struct iommu_resv_region *region, *next;
244 struct list_head group_resv_regions;
247 INIT_LIST_HEAD(&group_resv_regions);
248 iommu_get_group_resv_regions(group, &group_resv_regions);
250 list_for_each_entry_safe(region, next, &group_resv_regions, list) {
251 str += sprintf(str, "0x%016llx 0x%016llx %s\n",
252 (long long int)region->start,
253 (long long int)(region->start +
255 iommu_group_resv_type_string[region->type]);
262 static IOMMU_GROUP_ATTR(name, S_IRUGO, iommu_group_show_name, NULL);
264 static IOMMU_GROUP_ATTR(reserved_regions, 0444,
265 iommu_group_show_resv_regions, NULL);
267 static void iommu_group_release(struct kobject *kobj)
269 struct iommu_group *group = to_iommu_group(kobj);
271 pr_debug("Releasing group %d\n", group->id);
273 if (group->iommu_data_release)
274 group->iommu_data_release(group->iommu_data);
276 ida_simple_remove(&iommu_group_ida, group->id);
278 if (group->default_domain)
279 iommu_domain_free(group->default_domain);
285 static struct kobj_type iommu_group_ktype = {
286 .sysfs_ops = &iommu_group_sysfs_ops,
287 .release = iommu_group_release,
291 * iommu_group_alloc - Allocate a new group
292 * @name: Optional name to associate with group, visible in sysfs
294 * This function is called by an iommu driver to allocate a new iommu
295 * group. The iommu group represents the minimum granularity of the iommu.
296 * Upon successful return, the caller holds a reference to the supplied
297 * group in order to hold the group until devices are added. Use
298 * iommu_group_put() to release this extra reference count, allowing the
299 * group to be automatically reclaimed once it has no devices or external
302 struct iommu_group *iommu_group_alloc(void)
304 struct iommu_group *group;
307 group = kzalloc(sizeof(*group), GFP_KERNEL);
309 return ERR_PTR(-ENOMEM);
311 group->kobj.kset = iommu_group_kset;
312 mutex_init(&group->mutex);
313 INIT_LIST_HEAD(&group->devices);
314 BLOCKING_INIT_NOTIFIER_HEAD(&group->notifier);
316 ret = ida_simple_get(&iommu_group_ida, 0, 0, GFP_KERNEL);
323 ret = kobject_init_and_add(&group->kobj, &iommu_group_ktype,
324 NULL, "%d", group->id);
326 ida_simple_remove(&iommu_group_ida, group->id);
331 group->devices_kobj = kobject_create_and_add("devices", &group->kobj);
332 if (!group->devices_kobj) {
333 kobject_put(&group->kobj); /* triggers .release & free */
334 return ERR_PTR(-ENOMEM);
338 * The devices_kobj holds a reference on the group kobject, so
339 * as long as that exists so will the group. We can therefore
340 * use the devices_kobj for reference counting.
342 kobject_put(&group->kobj);
344 ret = iommu_group_create_file(group,
345 &iommu_group_attr_reserved_regions);
349 pr_debug("Allocated group %d\n", group->id);
353 EXPORT_SYMBOL_GPL(iommu_group_alloc);
355 struct iommu_group *iommu_group_get_by_id(int id)
357 struct kobject *group_kobj;
358 struct iommu_group *group;
361 if (!iommu_group_kset)
364 name = kasprintf(GFP_KERNEL, "%d", id);
368 group_kobj = kset_find_obj(iommu_group_kset, name);
374 group = container_of(group_kobj, struct iommu_group, kobj);
375 BUG_ON(group->id != id);
377 kobject_get(group->devices_kobj);
378 kobject_put(&group->kobj);
382 EXPORT_SYMBOL_GPL(iommu_group_get_by_id);
385 * iommu_group_get_iommudata - retrieve iommu_data registered for a group
388 * iommu drivers can store data in the group for use when doing iommu
389 * operations. This function provides a way to retrieve it. Caller
390 * should hold a group reference.
392 void *iommu_group_get_iommudata(struct iommu_group *group)
394 return group->iommu_data;
396 EXPORT_SYMBOL_GPL(iommu_group_get_iommudata);
399 * iommu_group_set_iommudata - set iommu_data for a group
401 * @iommu_data: new data
402 * @release: release function for iommu_data
404 * iommu drivers can store data in the group for use when doing iommu
405 * operations. This function provides a way to set the data after
406 * the group has been allocated. Caller should hold a group reference.
408 void iommu_group_set_iommudata(struct iommu_group *group, void *iommu_data,
409 void (*release)(void *iommu_data))
411 group->iommu_data = iommu_data;
412 group->iommu_data_release = release;
414 EXPORT_SYMBOL_GPL(iommu_group_set_iommudata);
417 * iommu_group_set_name - set name for a group
421 * Allow iommu driver to set a name for a group. When set it will
422 * appear in a name attribute file under the group in sysfs.
424 int iommu_group_set_name(struct iommu_group *group, const char *name)
429 iommu_group_remove_file(group, &iommu_group_attr_name);
436 group->name = kstrdup(name, GFP_KERNEL);
440 ret = iommu_group_create_file(group, &iommu_group_attr_name);
449 EXPORT_SYMBOL_GPL(iommu_group_set_name);
451 static int iommu_group_create_direct_mappings(struct iommu_group *group,
454 struct iommu_domain *domain = group->default_domain;
455 struct iommu_resv_region *entry;
456 struct list_head mappings;
457 unsigned long pg_size;
460 if (!domain || domain->type != IOMMU_DOMAIN_DMA)
463 BUG_ON(!domain->pgsize_bitmap);
465 pg_size = 1UL << __ffs(domain->pgsize_bitmap);
466 INIT_LIST_HEAD(&mappings);
468 iommu_get_resv_regions(dev, &mappings);
470 /* We need to consider overlapping regions for different devices */
471 list_for_each_entry(entry, &mappings, list) {
472 dma_addr_t start, end, addr;
474 if (domain->ops->apply_resv_region)
475 domain->ops->apply_resv_region(dev, domain, entry);
477 start = ALIGN(entry->start, pg_size);
478 end = ALIGN(entry->start + entry->length, pg_size);
480 if (entry->type != IOMMU_RESV_DIRECT)
483 for (addr = start; addr < end; addr += pg_size) {
484 phys_addr_t phys_addr;
486 phys_addr = iommu_iova_to_phys(domain, addr);
490 ret = iommu_map(domain, addr, addr, pg_size, entry->prot);
498 iommu_put_resv_regions(dev, &mappings);
504 * iommu_group_add_device - add a device to an iommu group
505 * @group: the group into which to add the device (reference should be held)
508 * This function is called by an iommu driver to add a device into a
509 * group. Adding a device increments the group reference count.
511 int iommu_group_add_device(struct iommu_group *group, struct device *dev)
514 struct iommu_device *device;
516 device = kzalloc(sizeof(*device), GFP_KERNEL);
522 ret = sysfs_create_link(&dev->kobj, &group->kobj, "iommu_group");
528 device->name = kasprintf(GFP_KERNEL, "%s", kobject_name(&dev->kobj));
531 sysfs_remove_link(&dev->kobj, "iommu_group");
536 ret = sysfs_create_link_nowarn(group->devices_kobj,
537 &dev->kobj, device->name);
540 if (ret == -EEXIST && i >= 0) {
542 * Account for the slim chance of collision
543 * and append an instance to the name.
545 device->name = kasprintf(GFP_KERNEL, "%s.%d",
546 kobject_name(&dev->kobj), i++);
550 sysfs_remove_link(&dev->kobj, "iommu_group");
555 kobject_get(group->devices_kobj);
557 dev->iommu_group = group;
559 iommu_group_create_direct_mappings(group, dev);
561 mutex_lock(&group->mutex);
562 list_add_tail(&device->list, &group->devices);
564 __iommu_attach_device(group->domain, dev);
565 mutex_unlock(&group->mutex);
567 /* Notify any listeners about change to group. */
568 blocking_notifier_call_chain(&group->notifier,
569 IOMMU_GROUP_NOTIFY_ADD_DEVICE, dev);
571 trace_add_device_to_group(group->id, dev);
573 pr_info("Adding device %s to group %d\n", dev_name(dev), group->id);
577 EXPORT_SYMBOL_GPL(iommu_group_add_device);
580 * iommu_group_remove_device - remove a device from it's current group
581 * @dev: device to be removed
583 * This function is called by an iommu driver to remove the device from
584 * it's current group. This decrements the iommu group reference count.
586 void iommu_group_remove_device(struct device *dev)
588 struct iommu_group *group = dev->iommu_group;
589 struct iommu_device *tmp_device, *device = NULL;
591 pr_info("Removing device %s from group %d\n", dev_name(dev), group->id);
593 /* Pre-notify listeners that a device is being removed. */
594 blocking_notifier_call_chain(&group->notifier,
595 IOMMU_GROUP_NOTIFY_DEL_DEVICE, dev);
597 mutex_lock(&group->mutex);
598 list_for_each_entry(tmp_device, &group->devices, list) {
599 if (tmp_device->dev == dev) {
601 list_del(&device->list);
605 mutex_unlock(&group->mutex);
610 sysfs_remove_link(group->devices_kobj, device->name);
611 sysfs_remove_link(&dev->kobj, "iommu_group");
613 trace_remove_device_from_group(group->id, dev);
617 dev->iommu_group = NULL;
618 kobject_put(group->devices_kobj);
620 EXPORT_SYMBOL_GPL(iommu_group_remove_device);
622 static int iommu_group_device_count(struct iommu_group *group)
624 struct iommu_device *entry;
627 list_for_each_entry(entry, &group->devices, list)
634 * iommu_group_for_each_dev - iterate over each device in the group
636 * @data: caller opaque data to be passed to callback function
637 * @fn: caller supplied callback function
639 * This function is called by group users to iterate over group devices.
640 * Callers should hold a reference count to the group during callback.
641 * The group->mutex is held across callbacks, which will block calls to
642 * iommu_group_add/remove_device.
644 static int __iommu_group_for_each_dev(struct iommu_group *group, void *data,
645 int (*fn)(struct device *, void *))
647 struct iommu_device *device;
650 list_for_each_entry(device, &group->devices, list) {
651 ret = fn(device->dev, data);
659 int iommu_group_for_each_dev(struct iommu_group *group, void *data,
660 int (*fn)(struct device *, void *))
664 mutex_lock(&group->mutex);
665 ret = __iommu_group_for_each_dev(group, data, fn);
666 mutex_unlock(&group->mutex);
670 EXPORT_SYMBOL_GPL(iommu_group_for_each_dev);
673 * iommu_group_get - Return the group for a device and increment reference
674 * @dev: get the group that this device belongs to
676 * This function is called by iommu drivers and users to get the group
677 * for the specified device. If found, the group is returned and the group
678 * reference in incremented, else NULL.
680 struct iommu_group *iommu_group_get(struct device *dev)
682 struct iommu_group *group = dev->iommu_group;
685 kobject_get(group->devices_kobj);
689 EXPORT_SYMBOL_GPL(iommu_group_get);
692 * iommu_group_ref_get - Increment reference on a group
693 * @group: the group to use, must not be NULL
695 * This function is called by iommu drivers to take additional references on an
696 * existing group. Returns the given group for convenience.
698 struct iommu_group *iommu_group_ref_get(struct iommu_group *group)
700 kobject_get(group->devices_kobj);
705 * iommu_group_put - Decrement group reference
706 * @group: the group to use
708 * This function is called by iommu drivers and users to release the
709 * iommu group. Once the reference count is zero, the group is released.
711 void iommu_group_put(struct iommu_group *group)
714 kobject_put(group->devices_kobj);
716 EXPORT_SYMBOL_GPL(iommu_group_put);
719 * iommu_group_register_notifier - Register a notifier for group changes
720 * @group: the group to watch
721 * @nb: notifier block to signal
723 * This function allows iommu group users to track changes in a group.
724 * See include/linux/iommu.h for actions sent via this notifier. Caller
725 * should hold a reference to the group throughout notifier registration.
727 int iommu_group_register_notifier(struct iommu_group *group,
728 struct notifier_block *nb)
730 return blocking_notifier_chain_register(&group->notifier, nb);
732 EXPORT_SYMBOL_GPL(iommu_group_register_notifier);
735 * iommu_group_unregister_notifier - Unregister a notifier
736 * @group: the group to watch
737 * @nb: notifier block to signal
739 * Unregister a previously registered group notifier block.
741 int iommu_group_unregister_notifier(struct iommu_group *group,
742 struct notifier_block *nb)
744 return blocking_notifier_chain_unregister(&group->notifier, nb);
746 EXPORT_SYMBOL_GPL(iommu_group_unregister_notifier);
749 * iommu_group_id - Return ID for a group
750 * @group: the group to ID
752 * Return the unique ID for the group matching the sysfs group number.
754 int iommu_group_id(struct iommu_group *group)
758 EXPORT_SYMBOL_GPL(iommu_group_id);
760 static struct iommu_group *get_pci_alias_group(struct pci_dev *pdev,
761 unsigned long *devfns);
764 * To consider a PCI device isolated, we require ACS to support Source
765 * Validation, Request Redirection, Completer Redirection, and Upstream
766 * Forwarding. This effectively means that devices cannot spoof their
767 * requester ID, requests and completions cannot be redirected, and all
768 * transactions are forwarded upstream, even as it passes through a
769 * bridge where the target device is downstream.
771 #define REQ_ACS_FLAGS (PCI_ACS_SV | PCI_ACS_RR | PCI_ACS_CR | PCI_ACS_UF)
774 * For multifunction devices which are not isolated from each other, find
775 * all the other non-isolated functions and look for existing groups. For
776 * each function, we also need to look for aliases to or from other devices
777 * that may already have a group.
779 static struct iommu_group *get_pci_function_alias_group(struct pci_dev *pdev,
780 unsigned long *devfns)
782 struct pci_dev *tmp = NULL;
783 struct iommu_group *group;
785 if (!pdev->multifunction || pci_acs_enabled(pdev, REQ_ACS_FLAGS))
788 for_each_pci_dev(tmp) {
789 if (tmp == pdev || tmp->bus != pdev->bus ||
790 PCI_SLOT(tmp->devfn) != PCI_SLOT(pdev->devfn) ||
791 pci_acs_enabled(tmp, REQ_ACS_FLAGS))
794 group = get_pci_alias_group(tmp, devfns);
805 * Look for aliases to or from the given device for existing groups. DMA
806 * aliases are only supported on the same bus, therefore the search
807 * space is quite small (especially since we're really only looking at pcie
808 * device, and therefore only expect multiple slots on the root complex or
809 * downstream switch ports). It's conceivable though that a pair of
810 * multifunction devices could have aliases between them that would cause a
811 * loop. To prevent this, we use a bitmap to track where we've been.
813 static struct iommu_group *get_pci_alias_group(struct pci_dev *pdev,
814 unsigned long *devfns)
816 struct pci_dev *tmp = NULL;
817 struct iommu_group *group;
819 if (test_and_set_bit(pdev->devfn & 0xff, devfns))
822 group = iommu_group_get(&pdev->dev);
826 for_each_pci_dev(tmp) {
827 if (tmp == pdev || tmp->bus != pdev->bus)
830 /* We alias them or they alias us */
831 if (pci_devs_are_dma_aliases(pdev, tmp)) {
832 group = get_pci_alias_group(tmp, devfns);
838 group = get_pci_function_alias_group(tmp, devfns);
849 struct group_for_pci_data {
850 struct pci_dev *pdev;
851 struct iommu_group *group;
855 * DMA alias iterator callback, return the last seen device. Stop and return
856 * the IOMMU group if we find one along the way.
858 static int get_pci_alias_or_group(struct pci_dev *pdev, u16 alias, void *opaque)
860 struct group_for_pci_data *data = opaque;
863 data->group = iommu_group_get(&pdev->dev);
865 return data->group != NULL;
869 * Generic device_group call-back function. It just allocates one
870 * iommu-group per device.
872 struct iommu_group *generic_device_group(struct device *dev)
874 struct iommu_group *group;
876 group = iommu_group_alloc();
884 * Use standard PCI bus topology, isolation features, and DMA alias quirks
885 * to find or create an IOMMU group for a device.
887 struct iommu_group *pci_device_group(struct device *dev)
889 struct pci_dev *pdev = to_pci_dev(dev);
890 struct group_for_pci_data data;
892 struct iommu_group *group = NULL;
893 u64 devfns[4] = { 0 };
895 if (WARN_ON(!dev_is_pci(dev)))
896 return ERR_PTR(-EINVAL);
899 * Find the upstream DMA alias for the device. A device must not
900 * be aliased due to topology in order to have its own IOMMU group.
901 * If we find an alias along the way that already belongs to a
904 if (pci_for_each_dma_alias(pdev, get_pci_alias_or_group, &data))
910 * Continue upstream from the point of minimum IOMMU granularity
911 * due to aliases to the point where devices are protected from
912 * peer-to-peer DMA by PCI ACS. Again, if we find an existing
915 for (bus = pdev->bus; !pci_is_root_bus(bus); bus = bus->parent) {
919 if (pci_acs_path_enabled(bus->self, NULL, REQ_ACS_FLAGS))
924 group = iommu_group_get(&pdev->dev);
930 * Look for existing groups on device aliases. If we alias another
931 * device or another device aliases us, use the same group.
933 group = get_pci_alias_group(pdev, (unsigned long *)devfns);
938 * Look for existing groups on non-isolated functions on the same
939 * slot and aliases of those funcions, if any. No need to clear
940 * the search bitmap, the tested devfns are still valid.
942 group = get_pci_function_alias_group(pdev, (unsigned long *)devfns);
946 /* No shared group found, allocate new */
947 group = iommu_group_alloc();
955 * iommu_group_get_for_dev - Find or create the IOMMU group for a device
956 * @dev: target device
958 * This function is intended to be called by IOMMU drivers and extended to
959 * support common, bus-defined algorithms when determining or creating the
960 * IOMMU group for a device. On success, the caller will hold a reference
961 * to the returned IOMMU group, which will already include the provided
962 * device. The reference should be released with iommu_group_put().
964 struct iommu_group *iommu_group_get_for_dev(struct device *dev)
966 const struct iommu_ops *ops = dev->bus->iommu_ops;
967 struct iommu_group *group;
970 group = iommu_group_get(dev);
974 group = ERR_PTR(-EINVAL);
976 if (ops && ops->device_group)
977 group = ops->device_group(dev);
983 * Try to allocate a default domain - needs support from the
986 if (!group->default_domain) {
987 group->default_domain = __iommu_domain_alloc(dev->bus,
990 group->domain = group->default_domain;
993 ret = iommu_group_add_device(group, dev);
995 iommu_group_put(group);
1002 struct iommu_domain *iommu_group_default_domain(struct iommu_group *group)
1004 return group->default_domain;
1007 static int add_iommu_group(struct device *dev, void *data)
1009 struct iommu_callback_data *cb = data;
1010 const struct iommu_ops *ops = cb->ops;
1013 if (!ops->add_device)
1016 WARN_ON(dev->iommu_group);
1018 ret = ops->add_device(dev);
1021 * We ignore -ENODEV errors for now, as they just mean that the
1022 * device is not translated by an IOMMU. We still care about
1023 * other errors and fail to initialize when they happen.
1031 static int remove_iommu_group(struct device *dev, void *data)
1033 struct iommu_callback_data *cb = data;
1034 const struct iommu_ops *ops = cb->ops;
1036 if (ops->remove_device && dev->iommu_group)
1037 ops->remove_device(dev);
1042 static int iommu_bus_notifier(struct notifier_block *nb,
1043 unsigned long action, void *data)
1045 struct device *dev = data;
1046 const struct iommu_ops *ops = dev->bus->iommu_ops;
1047 struct iommu_group *group;
1048 unsigned long group_action = 0;
1051 * ADD/DEL call into iommu driver ops if provided, which may
1052 * result in ADD/DEL notifiers to group->notifier
1054 if (action == BUS_NOTIFY_ADD_DEVICE) {
1055 if (ops->add_device)
1056 return ops->add_device(dev);
1057 } else if (action == BUS_NOTIFY_REMOVED_DEVICE) {
1058 if (ops->remove_device && dev->iommu_group) {
1059 ops->remove_device(dev);
1065 * Remaining BUS_NOTIFYs get filtered and republished to the
1066 * group, if anyone is listening
1068 group = iommu_group_get(dev);
1073 case BUS_NOTIFY_BIND_DRIVER:
1074 group_action = IOMMU_GROUP_NOTIFY_BIND_DRIVER;
1076 case BUS_NOTIFY_BOUND_DRIVER:
1077 group_action = IOMMU_GROUP_NOTIFY_BOUND_DRIVER;
1079 case BUS_NOTIFY_UNBIND_DRIVER:
1080 group_action = IOMMU_GROUP_NOTIFY_UNBIND_DRIVER;
1082 case BUS_NOTIFY_UNBOUND_DRIVER:
1083 group_action = IOMMU_GROUP_NOTIFY_UNBOUND_DRIVER;
1088 blocking_notifier_call_chain(&group->notifier,
1091 iommu_group_put(group);
1095 static int iommu_bus_init(struct bus_type *bus, const struct iommu_ops *ops)
1098 struct notifier_block *nb;
1099 struct iommu_callback_data cb = {
1103 nb = kzalloc(sizeof(struct notifier_block), GFP_KERNEL);
1107 nb->notifier_call = iommu_bus_notifier;
1109 err = bus_register_notifier(bus, nb);
1113 err = bus_for_each_dev(bus, NULL, &cb, add_iommu_group);
1122 bus_for_each_dev(bus, NULL, &cb, remove_iommu_group);
1123 bus_unregister_notifier(bus, nb);
1132 * bus_set_iommu - set iommu-callbacks for the bus
1134 * @ops: the callbacks provided by the iommu-driver
1136 * This function is called by an iommu driver to set the iommu methods
1137 * used for a particular bus. Drivers for devices on that bus can use
1138 * the iommu-api after these ops are registered.
1139 * This special function is needed because IOMMUs are usually devices on
1140 * the bus itself, so the iommu drivers are not initialized when the bus
1141 * is set up. With this function the iommu-driver can set the iommu-ops
1144 int bus_set_iommu(struct bus_type *bus, const struct iommu_ops *ops)
1148 if (bus->iommu_ops != NULL)
1151 bus->iommu_ops = ops;
1153 /* Do IOMMU specific setup for this bus-type */
1154 err = iommu_bus_init(bus, ops);
1156 bus->iommu_ops = NULL;
1160 EXPORT_SYMBOL_GPL(bus_set_iommu);
1162 bool iommu_present(struct bus_type *bus)
1164 return bus->iommu_ops != NULL;
1166 EXPORT_SYMBOL_GPL(iommu_present);
1168 bool iommu_capable(struct bus_type *bus, enum iommu_cap cap)
1170 if (!bus->iommu_ops || !bus->iommu_ops->capable)
1173 return bus->iommu_ops->capable(cap);
1175 EXPORT_SYMBOL_GPL(iommu_capable);
1178 * iommu_set_fault_handler() - set a fault handler for an iommu domain
1179 * @domain: iommu domain
1180 * @handler: fault handler
1181 * @token: user data, will be passed back to the fault handler
1183 * This function should be used by IOMMU users which want to be notified
1184 * whenever an IOMMU fault happens.
1186 * The fault handler itself should return 0 on success, and an appropriate
1187 * error code otherwise.
1189 void iommu_set_fault_handler(struct iommu_domain *domain,
1190 iommu_fault_handler_t handler,
1195 domain->handler = handler;
1196 domain->handler_token = token;
1198 EXPORT_SYMBOL_GPL(iommu_set_fault_handler);
1200 static struct iommu_domain *__iommu_domain_alloc(struct bus_type *bus,
1203 struct iommu_domain *domain;
1205 if (bus == NULL || bus->iommu_ops == NULL)
1208 domain = bus->iommu_ops->domain_alloc(type);
1212 domain->ops = bus->iommu_ops;
1213 domain->type = type;
1214 /* Assume all sizes by default; the driver may override this later */
1215 domain->pgsize_bitmap = bus->iommu_ops->pgsize_bitmap;
1220 struct iommu_domain *iommu_domain_alloc(struct bus_type *bus)
1222 return __iommu_domain_alloc(bus, IOMMU_DOMAIN_UNMANAGED);
1224 EXPORT_SYMBOL_GPL(iommu_domain_alloc);
1226 void iommu_domain_free(struct iommu_domain *domain)
1228 domain->ops->domain_free(domain);
1230 EXPORT_SYMBOL_GPL(iommu_domain_free);
1232 static int __iommu_attach_device(struct iommu_domain *domain,
1236 if (unlikely(domain->ops->attach_dev == NULL))
1239 ret = domain->ops->attach_dev(domain, dev);
1241 trace_attach_device_to_domain(dev);
1245 int iommu_attach_device(struct iommu_domain *domain, struct device *dev)
1247 struct iommu_group *group;
1250 group = iommu_group_get(dev);
1251 /* FIXME: Remove this when groups a mandatory for iommu drivers */
1253 return __iommu_attach_device(domain, dev);
1256 * We have a group - lock it to make sure the device-count doesn't
1257 * change while we are attaching
1259 mutex_lock(&group->mutex);
1261 if (iommu_group_device_count(group) != 1)
1264 ret = __iommu_attach_group(domain, group);
1267 mutex_unlock(&group->mutex);
1268 iommu_group_put(group);
1272 EXPORT_SYMBOL_GPL(iommu_attach_device);
1274 static void __iommu_detach_device(struct iommu_domain *domain,
1277 if (unlikely(domain->ops->detach_dev == NULL))
1280 domain->ops->detach_dev(domain, dev);
1281 trace_detach_device_from_domain(dev);
1284 void iommu_detach_device(struct iommu_domain *domain, struct device *dev)
1286 struct iommu_group *group;
1288 group = iommu_group_get(dev);
1289 /* FIXME: Remove this when groups a mandatory for iommu drivers */
1291 return __iommu_detach_device(domain, dev);
1293 mutex_lock(&group->mutex);
1294 if (iommu_group_device_count(group) != 1) {
1299 __iommu_detach_group(domain, group);
1302 mutex_unlock(&group->mutex);
1303 iommu_group_put(group);
1305 EXPORT_SYMBOL_GPL(iommu_detach_device);
1307 struct iommu_domain *iommu_get_domain_for_dev(struct device *dev)
1309 struct iommu_domain *domain;
1310 struct iommu_group *group;
1312 group = iommu_group_get(dev);
1313 /* FIXME: Remove this when groups a mandatory for iommu drivers */
1317 domain = group->domain;
1319 iommu_group_put(group);
1323 EXPORT_SYMBOL_GPL(iommu_get_domain_for_dev);
1326 * IOMMU groups are really the natrual working unit of the IOMMU, but
1327 * the IOMMU API works on domains and devices. Bridge that gap by
1328 * iterating over the devices in a group. Ideally we'd have a single
1329 * device which represents the requestor ID of the group, but we also
1330 * allow IOMMU drivers to create policy defined minimum sets, where
1331 * the physical hardware may be able to distiguish members, but we
1332 * wish to group them at a higher level (ex. untrusted multi-function
1333 * PCI devices). Thus we attach each device.
1335 static int iommu_group_do_attach_device(struct device *dev, void *data)
1337 struct iommu_domain *domain = data;
1339 return __iommu_attach_device(domain, dev);
1342 static int __iommu_attach_group(struct iommu_domain *domain,
1343 struct iommu_group *group)
1347 if (group->default_domain && group->domain != group->default_domain)
1350 ret = __iommu_group_for_each_dev(group, domain,
1351 iommu_group_do_attach_device);
1353 group->domain = domain;
1358 int iommu_attach_group(struct iommu_domain *domain, struct iommu_group *group)
1362 mutex_lock(&group->mutex);
1363 ret = __iommu_attach_group(domain, group);
1364 mutex_unlock(&group->mutex);
1368 EXPORT_SYMBOL_GPL(iommu_attach_group);
1370 static int iommu_group_do_detach_device(struct device *dev, void *data)
1372 struct iommu_domain *domain = data;
1374 __iommu_detach_device(domain, dev);
1379 static void __iommu_detach_group(struct iommu_domain *domain,
1380 struct iommu_group *group)
1384 if (!group->default_domain) {
1385 __iommu_group_for_each_dev(group, domain,
1386 iommu_group_do_detach_device);
1387 group->domain = NULL;
1391 if (group->domain == group->default_domain)
1394 /* Detach by re-attaching to the default domain */
1395 ret = __iommu_group_for_each_dev(group, group->default_domain,
1396 iommu_group_do_attach_device);
1400 group->domain = group->default_domain;
1403 void iommu_detach_group(struct iommu_domain *domain, struct iommu_group *group)
1405 mutex_lock(&group->mutex);
1406 __iommu_detach_group(domain, group);
1407 mutex_unlock(&group->mutex);
1409 EXPORT_SYMBOL_GPL(iommu_detach_group);
1411 phys_addr_t iommu_iova_to_phys(struct iommu_domain *domain, dma_addr_t iova)
1413 if (unlikely(domain->ops->iova_to_phys == NULL))
1416 return domain->ops->iova_to_phys(domain, iova);
1418 EXPORT_SYMBOL_GPL(iommu_iova_to_phys);
1420 static size_t iommu_pgsize(struct iommu_domain *domain,
1421 unsigned long addr_merge, size_t size)
1423 unsigned int pgsize_idx;
1426 /* Max page size that still fits into 'size' */
1427 pgsize_idx = __fls(size);
1429 /* need to consider alignment requirements ? */
1430 if (likely(addr_merge)) {
1431 /* Max page size allowed by address */
1432 unsigned int align_pgsize_idx = __ffs(addr_merge);
1433 pgsize_idx = min(pgsize_idx, align_pgsize_idx);
1436 /* build a mask of acceptable page sizes */
1437 pgsize = (1UL << (pgsize_idx + 1)) - 1;
1439 /* throw away page sizes not supported by the hardware */
1440 pgsize &= domain->pgsize_bitmap;
1442 /* make sure we're still sane */
1445 /* pick the biggest page */
1446 pgsize_idx = __fls(pgsize);
1447 pgsize = 1UL << pgsize_idx;
1452 int iommu_map(struct iommu_domain *domain, unsigned long iova,
1453 phys_addr_t paddr, size_t size, int prot)
1455 unsigned long orig_iova = iova;
1456 unsigned int min_pagesz;
1457 size_t orig_size = size;
1458 phys_addr_t orig_paddr = paddr;
1461 if (unlikely(domain->ops->map == NULL ||
1462 domain->pgsize_bitmap == 0UL))
1465 if (unlikely(!(domain->type & __IOMMU_DOMAIN_PAGING)))
1468 /* find out the minimum page size supported */
1469 min_pagesz = 1 << __ffs(domain->pgsize_bitmap);
1472 * both the virtual address and the physical one, as well as
1473 * the size of the mapping, must be aligned (at least) to the
1474 * size of the smallest page supported by the hardware
1476 if (!IS_ALIGNED(iova | paddr | size, min_pagesz)) {
1477 pr_err("unaligned: iova 0x%lx pa %pa size 0x%zx min_pagesz 0x%x\n",
1478 iova, &paddr, size, min_pagesz);
1482 pr_debug("map: iova 0x%lx pa %pa size 0x%zx\n", iova, &paddr, size);
1485 size_t pgsize = iommu_pgsize(domain, iova | paddr, size);
1487 pr_debug("mapping: iova 0x%lx pa %pa pgsize 0x%zx\n",
1488 iova, &paddr, pgsize);
1490 ret = domain->ops->map(domain, iova, paddr, pgsize, prot);
1499 /* unroll mapping in case something went wrong */
1501 iommu_unmap(domain, orig_iova, orig_size - size);
1503 trace_map(orig_iova, orig_paddr, orig_size);
1507 EXPORT_SYMBOL_GPL(iommu_map);
1509 size_t iommu_unmap(struct iommu_domain *domain, unsigned long iova, size_t size)
1511 size_t unmapped_page, unmapped = 0;
1512 unsigned int min_pagesz;
1513 unsigned long orig_iova = iova;
1515 if (unlikely(domain->ops->unmap == NULL ||
1516 domain->pgsize_bitmap == 0UL))
1519 if (unlikely(!(domain->type & __IOMMU_DOMAIN_PAGING)))
1522 /* find out the minimum page size supported */
1523 min_pagesz = 1 << __ffs(domain->pgsize_bitmap);
1526 * The virtual address, as well as the size of the mapping, must be
1527 * aligned (at least) to the size of the smallest page supported
1530 if (!IS_ALIGNED(iova | size, min_pagesz)) {
1531 pr_err("unaligned: iova 0x%lx size 0x%zx min_pagesz 0x%x\n",
1532 iova, size, min_pagesz);
1536 pr_debug("unmap this: iova 0x%lx size 0x%zx\n", iova, size);
1539 * Keep iterating until we either unmap 'size' bytes (or more)
1540 * or we hit an area that isn't mapped.
1542 while (unmapped < size) {
1543 size_t pgsize = iommu_pgsize(domain, iova, size - unmapped);
1545 unmapped_page = domain->ops->unmap(domain, iova, pgsize);
1549 pr_debug("unmapped: iova 0x%lx size 0x%zx\n",
1550 iova, unmapped_page);
1552 iova += unmapped_page;
1553 unmapped += unmapped_page;
1556 trace_unmap(orig_iova, size, unmapped);
1559 EXPORT_SYMBOL_GPL(iommu_unmap);
1561 size_t default_iommu_map_sg(struct iommu_domain *domain, unsigned long iova,
1562 struct scatterlist *sg, unsigned int nents, int prot)
1564 struct scatterlist *s;
1566 unsigned int i, min_pagesz;
1569 if (unlikely(domain->pgsize_bitmap == 0UL))
1572 min_pagesz = 1 << __ffs(domain->pgsize_bitmap);
1574 for_each_sg(sg, s, nents, i) {
1575 phys_addr_t phys = page_to_phys(sg_page(s)) + s->offset;
1578 * We are mapping on IOMMU page boundaries, so offset within
1579 * the page must be 0. However, the IOMMU may support pages
1580 * smaller than PAGE_SIZE, so s->offset may still represent
1581 * an offset of that boundary within the CPU page.
1583 if (!IS_ALIGNED(s->offset, min_pagesz))
1586 ret = iommu_map(domain, iova + mapped, phys, s->length, prot);
1590 mapped += s->length;
1596 /* undo mappings already done */
1597 iommu_unmap(domain, iova, mapped);
1602 EXPORT_SYMBOL_GPL(default_iommu_map_sg);
1604 int iommu_domain_window_enable(struct iommu_domain *domain, u32 wnd_nr,
1605 phys_addr_t paddr, u64 size, int prot)
1607 if (unlikely(domain->ops->domain_window_enable == NULL))
1610 return domain->ops->domain_window_enable(domain, wnd_nr, paddr, size,
1613 EXPORT_SYMBOL_GPL(iommu_domain_window_enable);
1615 void iommu_domain_window_disable(struct iommu_domain *domain, u32 wnd_nr)
1617 if (unlikely(domain->ops->domain_window_disable == NULL))
1620 return domain->ops->domain_window_disable(domain, wnd_nr);
1622 EXPORT_SYMBOL_GPL(iommu_domain_window_disable);
1624 static int __init iommu_init(void)
1626 iommu_group_kset = kset_create_and_add("iommu_groups",
1628 BUG_ON(!iommu_group_kset);
1632 core_initcall(iommu_init);
1634 int iommu_domain_get_attr(struct iommu_domain *domain,
1635 enum iommu_attr attr, void *data)
1637 struct iommu_domain_geometry *geometry;
1643 case DOMAIN_ATTR_GEOMETRY:
1645 *geometry = domain->geometry;
1648 case DOMAIN_ATTR_PAGING:
1650 *paging = (domain->pgsize_bitmap != 0UL);
1652 case DOMAIN_ATTR_WINDOWS:
1655 if (domain->ops->domain_get_windows != NULL)
1656 *count = domain->ops->domain_get_windows(domain);
1662 if (!domain->ops->domain_get_attr)
1665 ret = domain->ops->domain_get_attr(domain, attr, data);
1670 EXPORT_SYMBOL_GPL(iommu_domain_get_attr);
1672 int iommu_domain_set_attr(struct iommu_domain *domain,
1673 enum iommu_attr attr, void *data)
1679 case DOMAIN_ATTR_WINDOWS:
1682 if (domain->ops->domain_set_windows != NULL)
1683 ret = domain->ops->domain_set_windows(domain, *count);
1689 if (domain->ops->domain_set_attr == NULL)
1692 ret = domain->ops->domain_set_attr(domain, attr, data);
1697 EXPORT_SYMBOL_GPL(iommu_domain_set_attr);
1699 void iommu_get_resv_regions(struct device *dev, struct list_head *list)
1701 const struct iommu_ops *ops = dev->bus->iommu_ops;
1703 if (ops && ops->get_resv_regions)
1704 ops->get_resv_regions(dev, list);
1707 void iommu_put_resv_regions(struct device *dev, struct list_head *list)
1709 const struct iommu_ops *ops = dev->bus->iommu_ops;
1711 if (ops && ops->put_resv_regions)
1712 ops->put_resv_regions(dev, list);
1715 struct iommu_resv_region *iommu_alloc_resv_region(phys_addr_t start,
1719 struct iommu_resv_region *region;
1721 region = kzalloc(sizeof(*region), GFP_KERNEL);
1725 INIT_LIST_HEAD(®ion->list);
1726 region->start = start;
1727 region->length = length;
1728 region->prot = prot;
1729 region->type = type;
1733 /* Request that a device is direct mapped by the IOMMU */
1734 int iommu_request_dm_for_dev(struct device *dev)
1736 struct iommu_domain *dm_domain;
1737 struct iommu_group *group;
1740 /* Device must already be in a group before calling this function */
1741 group = iommu_group_get_for_dev(dev);
1743 return PTR_ERR(group);
1745 mutex_lock(&group->mutex);
1747 /* Check if the default domain is already direct mapped */
1749 if (group->default_domain &&
1750 group->default_domain->type == IOMMU_DOMAIN_IDENTITY)
1753 /* Don't change mappings of existing devices */
1755 if (iommu_group_device_count(group) != 1)
1758 /* Allocate a direct mapped domain */
1760 dm_domain = __iommu_domain_alloc(dev->bus, IOMMU_DOMAIN_IDENTITY);
1764 /* Attach the device to the domain */
1765 ret = __iommu_attach_group(dm_domain, group);
1767 iommu_domain_free(dm_domain);
1771 /* Make the direct mapped domain the default for this group */
1772 if (group->default_domain)
1773 iommu_domain_free(group->default_domain);
1774 group->default_domain = dm_domain;
1776 pr_info("Using direct mapping for device %s\n", dev_name(dev));
1780 mutex_unlock(&group->mutex);
1781 iommu_group_put(group);
1786 struct iommu_instance {
1787 struct list_head list;
1788 struct fwnode_handle *fwnode;
1789 const struct iommu_ops *ops;
1791 static LIST_HEAD(iommu_instance_list);
1792 static DEFINE_SPINLOCK(iommu_instance_lock);
1794 void iommu_register_instance(struct fwnode_handle *fwnode,
1795 const struct iommu_ops *ops)
1797 struct iommu_instance *iommu = kzalloc(sizeof(*iommu), GFP_KERNEL);
1799 if (WARN_ON(!iommu))
1802 of_node_get(to_of_node(fwnode));
1803 INIT_LIST_HEAD(&iommu->list);
1804 iommu->fwnode = fwnode;
1806 spin_lock(&iommu_instance_lock);
1807 list_add_tail(&iommu->list, &iommu_instance_list);
1808 spin_unlock(&iommu_instance_lock);
1811 const struct iommu_ops *iommu_get_instance(struct fwnode_handle *fwnode)
1813 struct iommu_instance *instance;
1814 const struct iommu_ops *ops = NULL;
1816 spin_lock(&iommu_instance_lock);
1817 list_for_each_entry(instance, &iommu_instance_list, list)
1818 if (instance->fwnode == fwnode) {
1819 ops = instance->ops;
1822 spin_unlock(&iommu_instance_lock);
1826 int iommu_fwspec_init(struct device *dev, struct fwnode_handle *iommu_fwnode,
1827 const struct iommu_ops *ops)
1829 struct iommu_fwspec *fwspec = dev->iommu_fwspec;
1832 return ops == fwspec->ops ? 0 : -EINVAL;
1834 fwspec = kzalloc(sizeof(*fwspec), GFP_KERNEL);
1838 of_node_get(to_of_node(iommu_fwnode));
1839 fwspec->iommu_fwnode = iommu_fwnode;
1841 dev->iommu_fwspec = fwspec;
1844 EXPORT_SYMBOL_GPL(iommu_fwspec_init);
1846 void iommu_fwspec_free(struct device *dev)
1848 struct iommu_fwspec *fwspec = dev->iommu_fwspec;
1851 fwnode_handle_put(fwspec->iommu_fwnode);
1853 dev->iommu_fwspec = NULL;
1856 EXPORT_SYMBOL_GPL(iommu_fwspec_free);
1858 int iommu_fwspec_add_ids(struct device *dev, u32 *ids, int num_ids)
1860 struct iommu_fwspec *fwspec = dev->iommu_fwspec;
1867 size = offsetof(struct iommu_fwspec, ids[fwspec->num_ids + num_ids]);
1868 if (size > sizeof(*fwspec)) {
1869 fwspec = krealloc(dev->iommu_fwspec, size, GFP_KERNEL);
1874 for (i = 0; i < num_ids; i++)
1875 fwspec->ids[fwspec->num_ids + i] = ids[i];
1877 fwspec->num_ids += num_ids;
1878 dev->iommu_fwspec = fwspec;
1881 EXPORT_SYMBOL_GPL(iommu_fwspec_add_ids);