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",
75 [IOMMU_RESV_SW_MSI] = "msi",
78 #define IOMMU_GROUP_ATTR(_name, _mode, _show, _store) \
79 struct iommu_group_attribute iommu_group_attr_##_name = \
80 __ATTR(_name, _mode, _show, _store)
82 #define to_iommu_group_attr(_attr) \
83 container_of(_attr, struct iommu_group_attribute, attr)
84 #define to_iommu_group(_kobj) \
85 container_of(_kobj, struct iommu_group, kobj)
87 static LIST_HEAD(iommu_device_list);
88 static DEFINE_SPINLOCK(iommu_device_lock);
90 int iommu_device_register(struct iommu_device *iommu)
92 spin_lock(&iommu_device_lock);
93 list_add_tail(&iommu->list, &iommu_device_list);
94 spin_unlock(&iommu_device_lock);
99 void iommu_device_unregister(struct iommu_device *iommu)
101 spin_lock(&iommu_device_lock);
102 list_del(&iommu->list);
103 spin_unlock(&iommu_device_lock);
106 static struct iommu_domain *__iommu_domain_alloc(struct bus_type *bus,
108 static int __iommu_attach_device(struct iommu_domain *domain,
110 static int __iommu_attach_group(struct iommu_domain *domain,
111 struct iommu_group *group);
112 static void __iommu_detach_group(struct iommu_domain *domain,
113 struct iommu_group *group);
115 static ssize_t iommu_group_attr_show(struct kobject *kobj,
116 struct attribute *__attr, char *buf)
118 struct iommu_group_attribute *attr = to_iommu_group_attr(__attr);
119 struct iommu_group *group = to_iommu_group(kobj);
123 ret = attr->show(group, buf);
127 static ssize_t iommu_group_attr_store(struct kobject *kobj,
128 struct attribute *__attr,
129 const char *buf, size_t count)
131 struct iommu_group_attribute *attr = to_iommu_group_attr(__attr);
132 struct iommu_group *group = to_iommu_group(kobj);
136 ret = attr->store(group, buf, count);
140 static const struct sysfs_ops iommu_group_sysfs_ops = {
141 .show = iommu_group_attr_show,
142 .store = iommu_group_attr_store,
145 static int iommu_group_create_file(struct iommu_group *group,
146 struct iommu_group_attribute *attr)
148 return sysfs_create_file(&group->kobj, &attr->attr);
151 static void iommu_group_remove_file(struct iommu_group *group,
152 struct iommu_group_attribute *attr)
154 sysfs_remove_file(&group->kobj, &attr->attr);
157 static ssize_t iommu_group_show_name(struct iommu_group *group, char *buf)
159 return sprintf(buf, "%s\n", group->name);
163 * iommu_insert_resv_region - Insert a new region in the
164 * list of reserved regions.
165 * @new: new region to insert
166 * @regions: list of regions
168 * The new element is sorted by address with respect to the other
169 * regions of the same type. In case it overlaps with another
170 * region of the same type, regions are merged. In case it
171 * overlaps with another region of different type, regions are
174 static int iommu_insert_resv_region(struct iommu_resv_region *new,
175 struct list_head *regions)
177 struct iommu_resv_region *region;
178 phys_addr_t start = new->start;
179 phys_addr_t end = new->start + new->length - 1;
180 struct list_head *pos = regions->next;
182 while (pos != regions) {
183 struct iommu_resv_region *entry =
184 list_entry(pos, struct iommu_resv_region, list);
185 phys_addr_t a = entry->start;
186 phys_addr_t b = entry->start + entry->length - 1;
187 int type = entry->type;
191 } else if (start > b) {
193 } else if ((start >= a) && (end <= b)) {
194 if (new->type == type)
199 if (new->type == type) {
200 phys_addr_t new_start = min(a, start);
201 phys_addr_t new_end = max(b, end);
203 list_del(&entry->list);
204 entry->start = new_start;
205 entry->length = new_end - new_start + 1;
206 iommu_insert_resv_region(entry, regions);
213 region = iommu_alloc_resv_region(new->start, new->length,
214 new->prot, new->type);
218 list_add_tail(®ion->list, pos);
224 iommu_insert_device_resv_regions(struct list_head *dev_resv_regions,
225 struct list_head *group_resv_regions)
227 struct iommu_resv_region *entry;
230 list_for_each_entry(entry, dev_resv_regions, list) {
231 ret = iommu_insert_resv_region(entry, group_resv_regions);
238 int iommu_get_group_resv_regions(struct iommu_group *group,
239 struct list_head *head)
241 struct group_device *device;
244 mutex_lock(&group->mutex);
245 list_for_each_entry(device, &group->devices, list) {
246 struct list_head dev_resv_regions;
248 INIT_LIST_HEAD(&dev_resv_regions);
249 iommu_get_resv_regions(device->dev, &dev_resv_regions);
250 ret = iommu_insert_device_resv_regions(&dev_resv_regions, head);
251 iommu_put_resv_regions(device->dev, &dev_resv_regions);
255 mutex_unlock(&group->mutex);
258 EXPORT_SYMBOL_GPL(iommu_get_group_resv_regions);
260 static ssize_t iommu_group_show_resv_regions(struct iommu_group *group,
263 struct iommu_resv_region *region, *next;
264 struct list_head group_resv_regions;
267 INIT_LIST_HEAD(&group_resv_regions);
268 iommu_get_group_resv_regions(group, &group_resv_regions);
270 list_for_each_entry_safe(region, next, &group_resv_regions, list) {
271 str += sprintf(str, "0x%016llx 0x%016llx %s\n",
272 (long long int)region->start,
273 (long long int)(region->start +
275 iommu_group_resv_type_string[region->type]);
282 static IOMMU_GROUP_ATTR(name, S_IRUGO, iommu_group_show_name, NULL);
284 static IOMMU_GROUP_ATTR(reserved_regions, 0444,
285 iommu_group_show_resv_regions, NULL);
287 static void iommu_group_release(struct kobject *kobj)
289 struct iommu_group *group = to_iommu_group(kobj);
291 pr_debug("Releasing group %d\n", group->id);
293 if (group->iommu_data_release)
294 group->iommu_data_release(group->iommu_data);
296 ida_simple_remove(&iommu_group_ida, group->id);
298 if (group->default_domain)
299 iommu_domain_free(group->default_domain);
305 static struct kobj_type iommu_group_ktype = {
306 .sysfs_ops = &iommu_group_sysfs_ops,
307 .release = iommu_group_release,
311 * iommu_group_alloc - Allocate a new group
312 * @name: Optional name to associate with group, visible in sysfs
314 * This function is called by an iommu driver to allocate a new iommu
315 * group. The iommu group represents the minimum granularity of the iommu.
316 * Upon successful return, the caller holds a reference to the supplied
317 * group in order to hold the group until devices are added. Use
318 * iommu_group_put() to release this extra reference count, allowing the
319 * group to be automatically reclaimed once it has no devices or external
322 struct iommu_group *iommu_group_alloc(void)
324 struct iommu_group *group;
327 group = kzalloc(sizeof(*group), GFP_KERNEL);
329 return ERR_PTR(-ENOMEM);
331 group->kobj.kset = iommu_group_kset;
332 mutex_init(&group->mutex);
333 INIT_LIST_HEAD(&group->devices);
334 BLOCKING_INIT_NOTIFIER_HEAD(&group->notifier);
336 ret = ida_simple_get(&iommu_group_ida, 0, 0, GFP_KERNEL);
343 ret = kobject_init_and_add(&group->kobj, &iommu_group_ktype,
344 NULL, "%d", group->id);
346 ida_simple_remove(&iommu_group_ida, group->id);
351 group->devices_kobj = kobject_create_and_add("devices", &group->kobj);
352 if (!group->devices_kobj) {
353 kobject_put(&group->kobj); /* triggers .release & free */
354 return ERR_PTR(-ENOMEM);
358 * The devices_kobj holds a reference on the group kobject, so
359 * as long as that exists so will the group. We can therefore
360 * use the devices_kobj for reference counting.
362 kobject_put(&group->kobj);
364 ret = iommu_group_create_file(group,
365 &iommu_group_attr_reserved_regions);
369 pr_debug("Allocated group %d\n", group->id);
373 EXPORT_SYMBOL_GPL(iommu_group_alloc);
375 struct iommu_group *iommu_group_get_by_id(int id)
377 struct kobject *group_kobj;
378 struct iommu_group *group;
381 if (!iommu_group_kset)
384 name = kasprintf(GFP_KERNEL, "%d", id);
388 group_kobj = kset_find_obj(iommu_group_kset, name);
394 group = container_of(group_kobj, struct iommu_group, kobj);
395 BUG_ON(group->id != id);
397 kobject_get(group->devices_kobj);
398 kobject_put(&group->kobj);
402 EXPORT_SYMBOL_GPL(iommu_group_get_by_id);
405 * iommu_group_get_iommudata - retrieve iommu_data registered for a group
408 * iommu drivers can store data in the group for use when doing iommu
409 * operations. This function provides a way to retrieve it. Caller
410 * should hold a group reference.
412 void *iommu_group_get_iommudata(struct iommu_group *group)
414 return group->iommu_data;
416 EXPORT_SYMBOL_GPL(iommu_group_get_iommudata);
419 * iommu_group_set_iommudata - set iommu_data for a group
421 * @iommu_data: new data
422 * @release: release function for iommu_data
424 * iommu drivers can store data in the group for use when doing iommu
425 * operations. This function provides a way to set the data after
426 * the group has been allocated. Caller should hold a group reference.
428 void iommu_group_set_iommudata(struct iommu_group *group, void *iommu_data,
429 void (*release)(void *iommu_data))
431 group->iommu_data = iommu_data;
432 group->iommu_data_release = release;
434 EXPORT_SYMBOL_GPL(iommu_group_set_iommudata);
437 * iommu_group_set_name - set name for a group
441 * Allow iommu driver to set a name for a group. When set it will
442 * appear in a name attribute file under the group in sysfs.
444 int iommu_group_set_name(struct iommu_group *group, const char *name)
449 iommu_group_remove_file(group, &iommu_group_attr_name);
456 group->name = kstrdup(name, GFP_KERNEL);
460 ret = iommu_group_create_file(group, &iommu_group_attr_name);
469 EXPORT_SYMBOL_GPL(iommu_group_set_name);
471 static int iommu_group_create_direct_mappings(struct iommu_group *group,
474 struct iommu_domain *domain = group->default_domain;
475 struct iommu_resv_region *entry;
476 struct list_head mappings;
477 unsigned long pg_size;
480 if (!domain || domain->type != IOMMU_DOMAIN_DMA)
483 BUG_ON(!domain->pgsize_bitmap);
485 pg_size = 1UL << __ffs(domain->pgsize_bitmap);
486 INIT_LIST_HEAD(&mappings);
488 iommu_get_resv_regions(dev, &mappings);
490 /* We need to consider overlapping regions for different devices */
491 list_for_each_entry(entry, &mappings, list) {
492 dma_addr_t start, end, addr;
494 if (domain->ops->apply_resv_region)
495 domain->ops->apply_resv_region(dev, domain, entry);
497 start = ALIGN(entry->start, pg_size);
498 end = ALIGN(entry->start + entry->length, pg_size);
500 if (entry->type != IOMMU_RESV_DIRECT)
503 for (addr = start; addr < end; addr += pg_size) {
504 phys_addr_t phys_addr;
506 phys_addr = iommu_iova_to_phys(domain, addr);
510 ret = iommu_map(domain, addr, addr, pg_size, entry->prot);
518 iommu_put_resv_regions(dev, &mappings);
524 * iommu_group_add_device - add a device to an iommu group
525 * @group: the group into which to add the device (reference should be held)
528 * This function is called by an iommu driver to add a device into a
529 * group. Adding a device increments the group reference count.
531 int iommu_group_add_device(struct iommu_group *group, struct device *dev)
534 struct group_device *device;
536 device = kzalloc(sizeof(*device), GFP_KERNEL);
542 ret = sysfs_create_link(&dev->kobj, &group->kobj, "iommu_group");
544 goto err_free_device;
546 device->name = kasprintf(GFP_KERNEL, "%s", kobject_name(&dev->kobj));
550 goto err_remove_link;
553 ret = sysfs_create_link_nowarn(group->devices_kobj,
554 &dev->kobj, device->name);
556 if (ret == -EEXIST && i >= 0) {
558 * Account for the slim chance of collision
559 * and append an instance to the name.
562 device->name = kasprintf(GFP_KERNEL, "%s.%d",
563 kobject_name(&dev->kobj), i++);
569 kobject_get(group->devices_kobj);
571 dev->iommu_group = group;
573 iommu_group_create_direct_mappings(group, dev);
575 mutex_lock(&group->mutex);
576 list_add_tail(&device->list, &group->devices);
578 ret = __iommu_attach_device(group->domain, dev);
579 mutex_unlock(&group->mutex);
583 /* Notify any listeners about change to group. */
584 blocking_notifier_call_chain(&group->notifier,
585 IOMMU_GROUP_NOTIFY_ADD_DEVICE, dev);
587 trace_add_device_to_group(group->id, dev);
589 pr_info("Adding device %s to group %d\n", dev_name(dev), group->id);
594 mutex_lock(&group->mutex);
595 list_del(&device->list);
596 mutex_unlock(&group->mutex);
597 dev->iommu_group = NULL;
598 kobject_put(group->devices_kobj);
602 sysfs_remove_link(&dev->kobj, "iommu_group");
605 pr_err("Failed to add device %s to group %d: %d\n", dev_name(dev), group->id, ret);
608 EXPORT_SYMBOL_GPL(iommu_group_add_device);
611 * iommu_group_remove_device - remove a device from it's current group
612 * @dev: device to be removed
614 * This function is called by an iommu driver to remove the device from
615 * it's current group. This decrements the iommu group reference count.
617 void iommu_group_remove_device(struct device *dev)
619 struct iommu_group *group = dev->iommu_group;
620 struct group_device *tmp_device, *device = NULL;
622 pr_info("Removing device %s from group %d\n", dev_name(dev), group->id);
624 /* Pre-notify listeners that a device is being removed. */
625 blocking_notifier_call_chain(&group->notifier,
626 IOMMU_GROUP_NOTIFY_DEL_DEVICE, dev);
628 mutex_lock(&group->mutex);
629 list_for_each_entry(tmp_device, &group->devices, list) {
630 if (tmp_device->dev == dev) {
632 list_del(&device->list);
636 mutex_unlock(&group->mutex);
641 sysfs_remove_link(group->devices_kobj, device->name);
642 sysfs_remove_link(&dev->kobj, "iommu_group");
644 trace_remove_device_from_group(group->id, dev);
648 dev->iommu_group = NULL;
649 kobject_put(group->devices_kobj);
651 EXPORT_SYMBOL_GPL(iommu_group_remove_device);
653 static int iommu_group_device_count(struct iommu_group *group)
655 struct group_device *entry;
658 list_for_each_entry(entry, &group->devices, list)
665 * iommu_group_for_each_dev - iterate over each device in the group
667 * @data: caller opaque data to be passed to callback function
668 * @fn: caller supplied callback function
670 * This function is called by group users to iterate over group devices.
671 * Callers should hold a reference count to the group during callback.
672 * The group->mutex is held across callbacks, which will block calls to
673 * iommu_group_add/remove_device.
675 static int __iommu_group_for_each_dev(struct iommu_group *group, void *data,
676 int (*fn)(struct device *, void *))
678 struct group_device *device;
681 list_for_each_entry(device, &group->devices, list) {
682 ret = fn(device->dev, data);
690 int iommu_group_for_each_dev(struct iommu_group *group, void *data,
691 int (*fn)(struct device *, void *))
695 mutex_lock(&group->mutex);
696 ret = __iommu_group_for_each_dev(group, data, fn);
697 mutex_unlock(&group->mutex);
701 EXPORT_SYMBOL_GPL(iommu_group_for_each_dev);
704 * iommu_group_get - Return the group for a device and increment reference
705 * @dev: get the group that this device belongs to
707 * This function is called by iommu drivers and users to get the group
708 * for the specified device. If found, the group is returned and the group
709 * reference in incremented, else NULL.
711 struct iommu_group *iommu_group_get(struct device *dev)
713 struct iommu_group *group = dev->iommu_group;
716 kobject_get(group->devices_kobj);
720 EXPORT_SYMBOL_GPL(iommu_group_get);
723 * iommu_group_ref_get - Increment reference on a group
724 * @group: the group to use, must not be NULL
726 * This function is called by iommu drivers to take additional references on an
727 * existing group. Returns the given group for convenience.
729 struct iommu_group *iommu_group_ref_get(struct iommu_group *group)
731 kobject_get(group->devices_kobj);
736 * iommu_group_put - Decrement group reference
737 * @group: the group to use
739 * This function is called by iommu drivers and users to release the
740 * iommu group. Once the reference count is zero, the group is released.
742 void iommu_group_put(struct iommu_group *group)
745 kobject_put(group->devices_kobj);
747 EXPORT_SYMBOL_GPL(iommu_group_put);
750 * iommu_group_register_notifier - Register a notifier for group changes
751 * @group: the group to watch
752 * @nb: notifier block to signal
754 * This function allows iommu group users to track changes in a group.
755 * See include/linux/iommu.h for actions sent via this notifier. Caller
756 * should hold a reference to the group throughout notifier registration.
758 int iommu_group_register_notifier(struct iommu_group *group,
759 struct notifier_block *nb)
761 return blocking_notifier_chain_register(&group->notifier, nb);
763 EXPORT_SYMBOL_GPL(iommu_group_register_notifier);
766 * iommu_group_unregister_notifier - Unregister a notifier
767 * @group: the group to watch
768 * @nb: notifier block to signal
770 * Unregister a previously registered group notifier block.
772 int iommu_group_unregister_notifier(struct iommu_group *group,
773 struct notifier_block *nb)
775 return blocking_notifier_chain_unregister(&group->notifier, nb);
777 EXPORT_SYMBOL_GPL(iommu_group_unregister_notifier);
780 * iommu_group_id - Return ID for a group
781 * @group: the group to ID
783 * Return the unique ID for the group matching the sysfs group number.
785 int iommu_group_id(struct iommu_group *group)
789 EXPORT_SYMBOL_GPL(iommu_group_id);
791 static struct iommu_group *get_pci_alias_group(struct pci_dev *pdev,
792 unsigned long *devfns);
795 * To consider a PCI device isolated, we require ACS to support Source
796 * Validation, Request Redirection, Completer Redirection, and Upstream
797 * Forwarding. This effectively means that devices cannot spoof their
798 * requester ID, requests and completions cannot be redirected, and all
799 * transactions are forwarded upstream, even as it passes through a
800 * bridge where the target device is downstream.
802 #define REQ_ACS_FLAGS (PCI_ACS_SV | PCI_ACS_RR | PCI_ACS_CR | PCI_ACS_UF)
805 * For multifunction devices which are not isolated from each other, find
806 * all the other non-isolated functions and look for existing groups. For
807 * each function, we also need to look for aliases to or from other devices
808 * that may already have a group.
810 static struct iommu_group *get_pci_function_alias_group(struct pci_dev *pdev,
811 unsigned long *devfns)
813 struct pci_dev *tmp = NULL;
814 struct iommu_group *group;
816 if (!pdev->multifunction || pci_acs_enabled(pdev, REQ_ACS_FLAGS))
819 for_each_pci_dev(tmp) {
820 if (tmp == pdev || tmp->bus != pdev->bus ||
821 PCI_SLOT(tmp->devfn) != PCI_SLOT(pdev->devfn) ||
822 pci_acs_enabled(tmp, REQ_ACS_FLAGS))
825 group = get_pci_alias_group(tmp, devfns);
836 * Look for aliases to or from the given device for existing groups. DMA
837 * aliases are only supported on the same bus, therefore the search
838 * space is quite small (especially since we're really only looking at pcie
839 * device, and therefore only expect multiple slots on the root complex or
840 * downstream switch ports). It's conceivable though that a pair of
841 * multifunction devices could have aliases between them that would cause a
842 * loop. To prevent this, we use a bitmap to track where we've been.
844 static struct iommu_group *get_pci_alias_group(struct pci_dev *pdev,
845 unsigned long *devfns)
847 struct pci_dev *tmp = NULL;
848 struct iommu_group *group;
850 if (test_and_set_bit(pdev->devfn & 0xff, devfns))
853 group = iommu_group_get(&pdev->dev);
857 for_each_pci_dev(tmp) {
858 if (tmp == pdev || tmp->bus != pdev->bus)
861 /* We alias them or they alias us */
862 if (pci_devs_are_dma_aliases(pdev, tmp)) {
863 group = get_pci_alias_group(tmp, devfns);
869 group = get_pci_function_alias_group(tmp, devfns);
880 struct group_for_pci_data {
881 struct pci_dev *pdev;
882 struct iommu_group *group;
886 * DMA alias iterator callback, return the last seen device. Stop and return
887 * the IOMMU group if we find one along the way.
889 static int get_pci_alias_or_group(struct pci_dev *pdev, u16 alias, void *opaque)
891 struct group_for_pci_data *data = opaque;
894 data->group = iommu_group_get(&pdev->dev);
896 return data->group != NULL;
900 * Generic device_group call-back function. It just allocates one
901 * iommu-group per device.
903 struct iommu_group *generic_device_group(struct device *dev)
905 struct iommu_group *group;
907 group = iommu_group_alloc();
915 * Use standard PCI bus topology, isolation features, and DMA alias quirks
916 * to find or create an IOMMU group for a device.
918 struct iommu_group *pci_device_group(struct device *dev)
920 struct pci_dev *pdev = to_pci_dev(dev);
921 struct group_for_pci_data data;
923 struct iommu_group *group = NULL;
924 u64 devfns[4] = { 0 };
926 if (WARN_ON(!dev_is_pci(dev)))
927 return ERR_PTR(-EINVAL);
930 * Find the upstream DMA alias for the device. A device must not
931 * be aliased due to topology in order to have its own IOMMU group.
932 * If we find an alias along the way that already belongs to a
935 if (pci_for_each_dma_alias(pdev, get_pci_alias_or_group, &data))
941 * Continue upstream from the point of minimum IOMMU granularity
942 * due to aliases to the point where devices are protected from
943 * peer-to-peer DMA by PCI ACS. Again, if we find an existing
946 for (bus = pdev->bus; !pci_is_root_bus(bus); bus = bus->parent) {
950 if (pci_acs_path_enabled(bus->self, NULL, REQ_ACS_FLAGS))
955 group = iommu_group_get(&pdev->dev);
961 * Look for existing groups on device aliases. If we alias another
962 * device or another device aliases us, use the same group.
964 group = get_pci_alias_group(pdev, (unsigned long *)devfns);
969 * Look for existing groups on non-isolated functions on the same
970 * slot and aliases of those funcions, if any. No need to clear
971 * the search bitmap, the tested devfns are still valid.
973 group = get_pci_function_alias_group(pdev, (unsigned long *)devfns);
977 /* No shared group found, allocate new */
978 group = iommu_group_alloc();
986 * iommu_group_get_for_dev - Find or create the IOMMU group for a device
987 * @dev: target device
989 * This function is intended to be called by IOMMU drivers and extended to
990 * support common, bus-defined algorithms when determining or creating the
991 * IOMMU group for a device. On success, the caller will hold a reference
992 * to the returned IOMMU group, which will already include the provided
993 * device. The reference should be released with iommu_group_put().
995 struct iommu_group *iommu_group_get_for_dev(struct device *dev)
997 const struct iommu_ops *ops = dev->bus->iommu_ops;
998 struct iommu_group *group;
1001 group = iommu_group_get(dev);
1005 group = ERR_PTR(-EINVAL);
1007 if (ops && ops->device_group)
1008 group = ops->device_group(dev);
1014 * Try to allocate a default domain - needs support from the
1017 if (!group->default_domain) {
1018 group->default_domain = __iommu_domain_alloc(dev->bus,
1021 group->domain = group->default_domain;
1024 ret = iommu_group_add_device(group, dev);
1026 iommu_group_put(group);
1027 return ERR_PTR(ret);
1033 struct iommu_domain *iommu_group_default_domain(struct iommu_group *group)
1035 return group->default_domain;
1038 static int add_iommu_group(struct device *dev, void *data)
1040 struct iommu_callback_data *cb = data;
1041 const struct iommu_ops *ops = cb->ops;
1044 if (!ops->add_device)
1047 WARN_ON(dev->iommu_group);
1049 ret = ops->add_device(dev);
1052 * We ignore -ENODEV errors for now, as they just mean that the
1053 * device is not translated by an IOMMU. We still care about
1054 * other errors and fail to initialize when they happen.
1062 static int remove_iommu_group(struct device *dev, void *data)
1064 struct iommu_callback_data *cb = data;
1065 const struct iommu_ops *ops = cb->ops;
1067 if (ops->remove_device && dev->iommu_group)
1068 ops->remove_device(dev);
1073 static int iommu_bus_notifier(struct notifier_block *nb,
1074 unsigned long action, void *data)
1076 struct device *dev = data;
1077 const struct iommu_ops *ops = dev->bus->iommu_ops;
1078 struct iommu_group *group;
1079 unsigned long group_action = 0;
1082 * ADD/DEL call into iommu driver ops if provided, which may
1083 * result in ADD/DEL notifiers to group->notifier
1085 if (action == BUS_NOTIFY_ADD_DEVICE) {
1086 if (ops->add_device)
1087 return ops->add_device(dev);
1088 } else if (action == BUS_NOTIFY_REMOVED_DEVICE) {
1089 if (ops->remove_device && dev->iommu_group) {
1090 ops->remove_device(dev);
1096 * Remaining BUS_NOTIFYs get filtered and republished to the
1097 * group, if anyone is listening
1099 group = iommu_group_get(dev);
1104 case BUS_NOTIFY_BIND_DRIVER:
1105 group_action = IOMMU_GROUP_NOTIFY_BIND_DRIVER;
1107 case BUS_NOTIFY_BOUND_DRIVER:
1108 group_action = IOMMU_GROUP_NOTIFY_BOUND_DRIVER;
1110 case BUS_NOTIFY_UNBIND_DRIVER:
1111 group_action = IOMMU_GROUP_NOTIFY_UNBIND_DRIVER;
1113 case BUS_NOTIFY_UNBOUND_DRIVER:
1114 group_action = IOMMU_GROUP_NOTIFY_UNBOUND_DRIVER;
1119 blocking_notifier_call_chain(&group->notifier,
1122 iommu_group_put(group);
1126 static int iommu_bus_init(struct bus_type *bus, const struct iommu_ops *ops)
1129 struct notifier_block *nb;
1130 struct iommu_callback_data cb = {
1134 nb = kzalloc(sizeof(struct notifier_block), GFP_KERNEL);
1138 nb->notifier_call = iommu_bus_notifier;
1140 err = bus_register_notifier(bus, nb);
1144 err = bus_for_each_dev(bus, NULL, &cb, add_iommu_group);
1153 bus_for_each_dev(bus, NULL, &cb, remove_iommu_group);
1154 bus_unregister_notifier(bus, nb);
1163 * bus_set_iommu - set iommu-callbacks for the bus
1165 * @ops: the callbacks provided by the iommu-driver
1167 * This function is called by an iommu driver to set the iommu methods
1168 * used for a particular bus. Drivers for devices on that bus can use
1169 * the iommu-api after these ops are registered.
1170 * This special function is needed because IOMMUs are usually devices on
1171 * the bus itself, so the iommu drivers are not initialized when the bus
1172 * is set up. With this function the iommu-driver can set the iommu-ops
1175 int bus_set_iommu(struct bus_type *bus, const struct iommu_ops *ops)
1179 if (bus->iommu_ops != NULL)
1182 bus->iommu_ops = ops;
1184 /* Do IOMMU specific setup for this bus-type */
1185 err = iommu_bus_init(bus, ops);
1187 bus->iommu_ops = NULL;
1191 EXPORT_SYMBOL_GPL(bus_set_iommu);
1193 bool iommu_present(struct bus_type *bus)
1195 return bus->iommu_ops != NULL;
1197 EXPORT_SYMBOL_GPL(iommu_present);
1199 bool iommu_capable(struct bus_type *bus, enum iommu_cap cap)
1201 if (!bus->iommu_ops || !bus->iommu_ops->capable)
1204 return bus->iommu_ops->capable(cap);
1206 EXPORT_SYMBOL_GPL(iommu_capable);
1209 * iommu_set_fault_handler() - set a fault handler for an iommu domain
1210 * @domain: iommu domain
1211 * @handler: fault handler
1212 * @token: user data, will be passed back to the fault handler
1214 * This function should be used by IOMMU users which want to be notified
1215 * whenever an IOMMU fault happens.
1217 * The fault handler itself should return 0 on success, and an appropriate
1218 * error code otherwise.
1220 void iommu_set_fault_handler(struct iommu_domain *domain,
1221 iommu_fault_handler_t handler,
1226 domain->handler = handler;
1227 domain->handler_token = token;
1229 EXPORT_SYMBOL_GPL(iommu_set_fault_handler);
1231 static struct iommu_domain *__iommu_domain_alloc(struct bus_type *bus,
1234 struct iommu_domain *domain;
1236 if (bus == NULL || bus->iommu_ops == NULL)
1239 domain = bus->iommu_ops->domain_alloc(type);
1243 domain->ops = bus->iommu_ops;
1244 domain->type = type;
1245 /* Assume all sizes by default; the driver may override this later */
1246 domain->pgsize_bitmap = bus->iommu_ops->pgsize_bitmap;
1251 struct iommu_domain *iommu_domain_alloc(struct bus_type *bus)
1253 return __iommu_domain_alloc(bus, IOMMU_DOMAIN_UNMANAGED);
1255 EXPORT_SYMBOL_GPL(iommu_domain_alloc);
1257 void iommu_domain_free(struct iommu_domain *domain)
1259 domain->ops->domain_free(domain);
1261 EXPORT_SYMBOL_GPL(iommu_domain_free);
1263 static int __iommu_attach_device(struct iommu_domain *domain,
1267 if (unlikely(domain->ops->attach_dev == NULL))
1270 ret = domain->ops->attach_dev(domain, dev);
1272 trace_attach_device_to_domain(dev);
1276 int iommu_attach_device(struct iommu_domain *domain, struct device *dev)
1278 struct iommu_group *group;
1281 group = iommu_group_get(dev);
1282 /* FIXME: Remove this when groups a mandatory for iommu drivers */
1284 return __iommu_attach_device(domain, dev);
1287 * We have a group - lock it to make sure the device-count doesn't
1288 * change while we are attaching
1290 mutex_lock(&group->mutex);
1292 if (iommu_group_device_count(group) != 1)
1295 ret = __iommu_attach_group(domain, group);
1298 mutex_unlock(&group->mutex);
1299 iommu_group_put(group);
1303 EXPORT_SYMBOL_GPL(iommu_attach_device);
1305 static void __iommu_detach_device(struct iommu_domain *domain,
1308 if (unlikely(domain->ops->detach_dev == NULL))
1311 domain->ops->detach_dev(domain, dev);
1312 trace_detach_device_from_domain(dev);
1315 void iommu_detach_device(struct iommu_domain *domain, struct device *dev)
1317 struct iommu_group *group;
1319 group = iommu_group_get(dev);
1320 /* FIXME: Remove this when groups a mandatory for iommu drivers */
1322 return __iommu_detach_device(domain, dev);
1324 mutex_lock(&group->mutex);
1325 if (iommu_group_device_count(group) != 1) {
1330 __iommu_detach_group(domain, group);
1333 mutex_unlock(&group->mutex);
1334 iommu_group_put(group);
1336 EXPORT_SYMBOL_GPL(iommu_detach_device);
1338 struct iommu_domain *iommu_get_domain_for_dev(struct device *dev)
1340 struct iommu_domain *domain;
1341 struct iommu_group *group;
1343 group = iommu_group_get(dev);
1344 /* FIXME: Remove this when groups a mandatory for iommu drivers */
1348 domain = group->domain;
1350 iommu_group_put(group);
1354 EXPORT_SYMBOL_GPL(iommu_get_domain_for_dev);
1357 * IOMMU groups are really the natrual working unit of the IOMMU, but
1358 * the IOMMU API works on domains and devices. Bridge that gap by
1359 * iterating over the devices in a group. Ideally we'd have a single
1360 * device which represents the requestor ID of the group, but we also
1361 * allow IOMMU drivers to create policy defined minimum sets, where
1362 * the physical hardware may be able to distiguish members, but we
1363 * wish to group them at a higher level (ex. untrusted multi-function
1364 * PCI devices). Thus we attach each device.
1366 static int iommu_group_do_attach_device(struct device *dev, void *data)
1368 struct iommu_domain *domain = data;
1370 return __iommu_attach_device(domain, dev);
1373 static int __iommu_attach_group(struct iommu_domain *domain,
1374 struct iommu_group *group)
1378 if (group->default_domain && group->domain != group->default_domain)
1381 ret = __iommu_group_for_each_dev(group, domain,
1382 iommu_group_do_attach_device);
1384 group->domain = domain;
1389 int iommu_attach_group(struct iommu_domain *domain, struct iommu_group *group)
1393 mutex_lock(&group->mutex);
1394 ret = __iommu_attach_group(domain, group);
1395 mutex_unlock(&group->mutex);
1399 EXPORT_SYMBOL_GPL(iommu_attach_group);
1401 static int iommu_group_do_detach_device(struct device *dev, void *data)
1403 struct iommu_domain *domain = data;
1405 __iommu_detach_device(domain, dev);
1410 static void __iommu_detach_group(struct iommu_domain *domain,
1411 struct iommu_group *group)
1415 if (!group->default_domain) {
1416 __iommu_group_for_each_dev(group, domain,
1417 iommu_group_do_detach_device);
1418 group->domain = NULL;
1422 if (group->domain == group->default_domain)
1425 /* Detach by re-attaching to the default domain */
1426 ret = __iommu_group_for_each_dev(group, group->default_domain,
1427 iommu_group_do_attach_device);
1431 group->domain = group->default_domain;
1434 void iommu_detach_group(struct iommu_domain *domain, struct iommu_group *group)
1436 mutex_lock(&group->mutex);
1437 __iommu_detach_group(domain, group);
1438 mutex_unlock(&group->mutex);
1440 EXPORT_SYMBOL_GPL(iommu_detach_group);
1442 phys_addr_t iommu_iova_to_phys(struct iommu_domain *domain, dma_addr_t iova)
1444 if (unlikely(domain->ops->iova_to_phys == NULL))
1447 return domain->ops->iova_to_phys(domain, iova);
1449 EXPORT_SYMBOL_GPL(iommu_iova_to_phys);
1451 static size_t iommu_pgsize(struct iommu_domain *domain,
1452 unsigned long addr_merge, size_t size)
1454 unsigned int pgsize_idx;
1457 /* Max page size that still fits into 'size' */
1458 pgsize_idx = __fls(size);
1460 /* need to consider alignment requirements ? */
1461 if (likely(addr_merge)) {
1462 /* Max page size allowed by address */
1463 unsigned int align_pgsize_idx = __ffs(addr_merge);
1464 pgsize_idx = min(pgsize_idx, align_pgsize_idx);
1467 /* build a mask of acceptable page sizes */
1468 pgsize = (1UL << (pgsize_idx + 1)) - 1;
1470 /* throw away page sizes not supported by the hardware */
1471 pgsize &= domain->pgsize_bitmap;
1473 /* make sure we're still sane */
1476 /* pick the biggest page */
1477 pgsize_idx = __fls(pgsize);
1478 pgsize = 1UL << pgsize_idx;
1483 int iommu_map(struct iommu_domain *domain, unsigned long iova,
1484 phys_addr_t paddr, size_t size, int prot)
1486 unsigned long orig_iova = iova;
1487 unsigned int min_pagesz;
1488 size_t orig_size = size;
1489 phys_addr_t orig_paddr = paddr;
1492 if (unlikely(domain->ops->map == NULL ||
1493 domain->pgsize_bitmap == 0UL))
1496 if (unlikely(!(domain->type & __IOMMU_DOMAIN_PAGING)))
1499 /* find out the minimum page size supported */
1500 min_pagesz = 1 << __ffs(domain->pgsize_bitmap);
1503 * both the virtual address and the physical one, as well as
1504 * the size of the mapping, must be aligned (at least) to the
1505 * size of the smallest page supported by the hardware
1507 if (!IS_ALIGNED(iova | paddr | size, min_pagesz)) {
1508 pr_err("unaligned: iova 0x%lx pa %pa size 0x%zx min_pagesz 0x%x\n",
1509 iova, &paddr, size, min_pagesz);
1513 pr_debug("map: iova 0x%lx pa %pa size 0x%zx\n", iova, &paddr, size);
1516 size_t pgsize = iommu_pgsize(domain, iova | paddr, size);
1518 pr_debug("mapping: iova 0x%lx pa %pa pgsize 0x%zx\n",
1519 iova, &paddr, pgsize);
1521 ret = domain->ops->map(domain, iova, paddr, pgsize, prot);
1530 /* unroll mapping in case something went wrong */
1532 iommu_unmap(domain, orig_iova, orig_size - size);
1534 trace_map(orig_iova, orig_paddr, orig_size);
1538 EXPORT_SYMBOL_GPL(iommu_map);
1540 size_t iommu_unmap(struct iommu_domain *domain, unsigned long iova, size_t size)
1542 size_t unmapped_page, unmapped = 0;
1543 unsigned int min_pagesz;
1544 unsigned long orig_iova = iova;
1546 if (unlikely(domain->ops->unmap == NULL ||
1547 domain->pgsize_bitmap == 0UL))
1550 if (unlikely(!(domain->type & __IOMMU_DOMAIN_PAGING)))
1553 /* find out the minimum page size supported */
1554 min_pagesz = 1 << __ffs(domain->pgsize_bitmap);
1557 * The virtual address, as well as the size of the mapping, must be
1558 * aligned (at least) to the size of the smallest page supported
1561 if (!IS_ALIGNED(iova | size, min_pagesz)) {
1562 pr_err("unaligned: iova 0x%lx size 0x%zx min_pagesz 0x%x\n",
1563 iova, size, min_pagesz);
1567 pr_debug("unmap this: iova 0x%lx size 0x%zx\n", iova, size);
1570 * Keep iterating until we either unmap 'size' bytes (or more)
1571 * or we hit an area that isn't mapped.
1573 while (unmapped < size) {
1574 size_t pgsize = iommu_pgsize(domain, iova, size - unmapped);
1576 unmapped_page = domain->ops->unmap(domain, iova, pgsize);
1580 pr_debug("unmapped: iova 0x%lx size 0x%zx\n",
1581 iova, unmapped_page);
1583 iova += unmapped_page;
1584 unmapped += unmapped_page;
1587 trace_unmap(orig_iova, size, unmapped);
1590 EXPORT_SYMBOL_GPL(iommu_unmap);
1592 size_t default_iommu_map_sg(struct iommu_domain *domain, unsigned long iova,
1593 struct scatterlist *sg, unsigned int nents, int prot)
1595 struct scatterlist *s;
1597 unsigned int i, min_pagesz;
1600 if (unlikely(domain->pgsize_bitmap == 0UL))
1603 min_pagesz = 1 << __ffs(domain->pgsize_bitmap);
1605 for_each_sg(sg, s, nents, i) {
1606 phys_addr_t phys = page_to_phys(sg_page(s)) + s->offset;
1609 * We are mapping on IOMMU page boundaries, so offset within
1610 * the page must be 0. However, the IOMMU may support pages
1611 * smaller than PAGE_SIZE, so s->offset may still represent
1612 * an offset of that boundary within the CPU page.
1614 if (!IS_ALIGNED(s->offset, min_pagesz))
1617 ret = iommu_map(domain, iova + mapped, phys, s->length, prot);
1621 mapped += s->length;
1627 /* undo mappings already done */
1628 iommu_unmap(domain, iova, mapped);
1633 EXPORT_SYMBOL_GPL(default_iommu_map_sg);
1635 int iommu_domain_window_enable(struct iommu_domain *domain, u32 wnd_nr,
1636 phys_addr_t paddr, u64 size, int prot)
1638 if (unlikely(domain->ops->domain_window_enable == NULL))
1641 return domain->ops->domain_window_enable(domain, wnd_nr, paddr, size,
1644 EXPORT_SYMBOL_GPL(iommu_domain_window_enable);
1646 void iommu_domain_window_disable(struct iommu_domain *domain, u32 wnd_nr)
1648 if (unlikely(domain->ops->domain_window_disable == NULL))
1651 return domain->ops->domain_window_disable(domain, wnd_nr);
1653 EXPORT_SYMBOL_GPL(iommu_domain_window_disable);
1655 static int __init iommu_init(void)
1657 iommu_group_kset = kset_create_and_add("iommu_groups",
1659 BUG_ON(!iommu_group_kset);
1663 core_initcall(iommu_init);
1665 int iommu_domain_get_attr(struct iommu_domain *domain,
1666 enum iommu_attr attr, void *data)
1668 struct iommu_domain_geometry *geometry;
1674 case DOMAIN_ATTR_GEOMETRY:
1676 *geometry = domain->geometry;
1679 case DOMAIN_ATTR_PAGING:
1681 *paging = (domain->pgsize_bitmap != 0UL);
1683 case DOMAIN_ATTR_WINDOWS:
1686 if (domain->ops->domain_get_windows != NULL)
1687 *count = domain->ops->domain_get_windows(domain);
1693 if (!domain->ops->domain_get_attr)
1696 ret = domain->ops->domain_get_attr(domain, attr, data);
1701 EXPORT_SYMBOL_GPL(iommu_domain_get_attr);
1703 int iommu_domain_set_attr(struct iommu_domain *domain,
1704 enum iommu_attr attr, void *data)
1710 case DOMAIN_ATTR_WINDOWS:
1713 if (domain->ops->domain_set_windows != NULL)
1714 ret = domain->ops->domain_set_windows(domain, *count);
1720 if (domain->ops->domain_set_attr == NULL)
1723 ret = domain->ops->domain_set_attr(domain, attr, data);
1728 EXPORT_SYMBOL_GPL(iommu_domain_set_attr);
1730 void iommu_get_resv_regions(struct device *dev, struct list_head *list)
1732 const struct iommu_ops *ops = dev->bus->iommu_ops;
1734 if (ops && ops->get_resv_regions)
1735 ops->get_resv_regions(dev, list);
1738 void iommu_put_resv_regions(struct device *dev, struct list_head *list)
1740 const struct iommu_ops *ops = dev->bus->iommu_ops;
1742 if (ops && ops->put_resv_regions)
1743 ops->put_resv_regions(dev, list);
1746 struct iommu_resv_region *iommu_alloc_resv_region(phys_addr_t start,
1747 size_t length, int prot,
1748 enum iommu_resv_type type)
1750 struct iommu_resv_region *region;
1752 region = kzalloc(sizeof(*region), GFP_KERNEL);
1756 INIT_LIST_HEAD(®ion->list);
1757 region->start = start;
1758 region->length = length;
1759 region->prot = prot;
1760 region->type = type;
1764 /* Request that a device is direct mapped by the IOMMU */
1765 int iommu_request_dm_for_dev(struct device *dev)
1767 struct iommu_domain *dm_domain;
1768 struct iommu_group *group;
1771 /* Device must already be in a group before calling this function */
1772 group = iommu_group_get_for_dev(dev);
1774 return PTR_ERR(group);
1776 mutex_lock(&group->mutex);
1778 /* Check if the default domain is already direct mapped */
1780 if (group->default_domain &&
1781 group->default_domain->type == IOMMU_DOMAIN_IDENTITY)
1784 /* Don't change mappings of existing devices */
1786 if (iommu_group_device_count(group) != 1)
1789 /* Allocate a direct mapped domain */
1791 dm_domain = __iommu_domain_alloc(dev->bus, IOMMU_DOMAIN_IDENTITY);
1795 /* Attach the device to the domain */
1796 ret = __iommu_attach_group(dm_domain, group);
1798 iommu_domain_free(dm_domain);
1802 /* Make the direct mapped domain the default for this group */
1803 if (group->default_domain)
1804 iommu_domain_free(group->default_domain);
1805 group->default_domain = dm_domain;
1807 pr_info("Using direct mapping for device %s\n", dev_name(dev));
1811 mutex_unlock(&group->mutex);
1812 iommu_group_put(group);
1817 const struct iommu_ops *iommu_ops_from_fwnode(struct fwnode_handle *fwnode)
1819 const struct iommu_ops *ops = NULL;
1820 struct iommu_device *iommu;
1822 spin_lock(&iommu_device_lock);
1823 list_for_each_entry(iommu, &iommu_device_list, list)
1824 if (iommu->fwnode == fwnode) {
1828 spin_unlock(&iommu_device_lock);
1832 int iommu_fwspec_init(struct device *dev, struct fwnode_handle *iommu_fwnode,
1833 const struct iommu_ops *ops)
1835 struct iommu_fwspec *fwspec = dev->iommu_fwspec;
1838 return ops == fwspec->ops ? 0 : -EINVAL;
1840 fwspec = kzalloc(sizeof(*fwspec), GFP_KERNEL);
1844 of_node_get(to_of_node(iommu_fwnode));
1845 fwspec->iommu_fwnode = iommu_fwnode;
1847 dev->iommu_fwspec = fwspec;
1850 EXPORT_SYMBOL_GPL(iommu_fwspec_init);
1852 void iommu_fwspec_free(struct device *dev)
1854 struct iommu_fwspec *fwspec = dev->iommu_fwspec;
1857 fwnode_handle_put(fwspec->iommu_fwnode);
1859 dev->iommu_fwspec = NULL;
1862 EXPORT_SYMBOL_GPL(iommu_fwspec_free);
1864 int iommu_fwspec_add_ids(struct device *dev, u32 *ids, int num_ids)
1866 struct iommu_fwspec *fwspec = dev->iommu_fwspec;
1873 size = offsetof(struct iommu_fwspec, ids[fwspec->num_ids + num_ids]);
1874 if (size > sizeof(*fwspec)) {
1875 fwspec = krealloc(dev->iommu_fwspec, size, GFP_KERNEL);
1879 dev->iommu_fwspec = fwspec;
1882 for (i = 0; i < num_ids; i++)
1883 fwspec->ids[fwspec->num_ids + i] = ids[i];
1885 fwspec->num_ids += num_ids;
1888 EXPORT_SYMBOL_GPL(iommu_fwspec_add_ids);