Lines Matching full:group
73 #define for_each_group_device(group, pos) \ argument
74 list_for_each_entry(pos, &(group)->devices, list)
78 ssize_t (*show)(struct iommu_group *group, char *buf);
79 ssize_t (*store)(struct iommu_group *group,
101 struct iommu_group *group);
110 static int __iommu_device_set_domain(struct iommu_group *group,
114 static int __iommu_group_set_domain_internal(struct iommu_group *group,
117 static int __iommu_group_set_domain(struct iommu_group *group, in __iommu_group_set_domain() argument
120 return __iommu_group_set_domain_internal(group, new_domain, 0); in __iommu_group_set_domain()
122 static void __iommu_group_set_domain_nofail(struct iommu_group *group, in __iommu_group_set_domain_nofail() argument
126 group, new_domain, IOMMU_SET_DOMAIN_MUST_SUCCEED)); in __iommu_group_set_domain_nofail()
129 static int iommu_setup_default_domain(struct iommu_group *group,
133 static ssize_t iommu_group_store_type(struct iommu_group *group,
135 static struct group_device *iommu_group_alloc_device(struct iommu_group *group,
137 static void __iommu_group_free_device(struct iommu_group *group,
410 struct iommu_group *group; in iommu_init_device() local
432 group = ops->device_group(dev); in iommu_init_device()
433 if (WARN_ON_ONCE(group == NULL)) in iommu_init_device()
434 group = ERR_PTR(-EINVAL); in iommu_init_device()
435 if (IS_ERR(group)) { in iommu_init_device()
436 ret = PTR_ERR(group); in iommu_init_device()
439 dev->iommu_group = group; in iommu_init_device()
461 struct iommu_group *group = dev->iommu_group; in iommu_deinit_device() local
464 lockdep_assert_held(&group->mutex); in iommu_deinit_device()
470 * If there are still other devices in the group, they are not affected in iommu_deinit_device()
492 * If this is the last driver to use the group then we must free the in iommu_deinit_device()
495 if (list_empty(&group->devices)) { in iommu_deinit_device()
496 if (group->default_domain) { in iommu_deinit_device()
497 iommu_domain_free(group->default_domain); in iommu_deinit_device()
498 group->default_domain = NULL; in iommu_deinit_device()
500 if (group->blocking_domain) { in iommu_deinit_device()
501 iommu_domain_free(group->blocking_domain); in iommu_deinit_device()
502 group->blocking_domain = NULL; in iommu_deinit_device()
504 group->domain = NULL; in iommu_deinit_device()
518 struct iommu_group *group; in __iommu_probe_device() local
542 /* Device is probed already if in a group */ in __iommu_probe_device()
550 group = dev->iommu_group; in __iommu_probe_device()
551 gdev = iommu_group_alloc_device(group, dev); in __iommu_probe_device()
552 mutex_lock(&group->mutex); in __iommu_probe_device()
562 list_add_tail(&gdev->list, &group->devices); in __iommu_probe_device()
563 WARN_ON(group->default_domain && !group->domain); in __iommu_probe_device()
564 if (group->default_domain) in __iommu_probe_device()
565 iommu_create_device_direct_mappings(group->default_domain, dev); in __iommu_probe_device()
566 if (group->domain) { in __iommu_probe_device()
567 ret = __iommu_device_set_domain(group, dev, group->domain, 0); in __iommu_probe_device()
570 } else if (!group->default_domain && !group_list) { in __iommu_probe_device()
571 ret = iommu_setup_default_domain(group, 0); in __iommu_probe_device()
574 } else if (!group->default_domain) { in __iommu_probe_device()
580 if (list_empty(&group->entry)) in __iommu_probe_device()
581 list_add_tail(&group->entry, group_list); in __iommu_probe_device()
584 if (group->default_domain) in __iommu_probe_device()
587 mutex_unlock(&group->mutex); in __iommu_probe_device()
593 __iommu_group_free_device(group, gdev); in __iommu_probe_device()
596 mutex_unlock(&group->mutex); in __iommu_probe_device()
597 iommu_group_put(group); in __iommu_probe_device()
620 static void __iommu_group_free_device(struct iommu_group *group, in __iommu_group_free_device() argument
625 sysfs_remove_link(group->devices_kobj, grp_dev->name); in __iommu_group_free_device()
628 trace_remove_device_from_group(group->id, dev); in __iommu_group_free_device()
631 * If the group has become empty then ownership must have been in __iommu_group_free_device()
635 if (list_empty(&group->devices)) in __iommu_group_free_device()
636 WARN_ON(group->owner_cnt || in __iommu_group_free_device()
637 group->domain != group->default_domain); in __iommu_group_free_device()
646 struct iommu_group *group = dev->iommu_group; in __iommu_group_remove_device() local
649 mutex_lock(&group->mutex); in __iommu_group_remove_device()
650 for_each_group_device(group, device) { in __iommu_group_remove_device()
655 __iommu_group_free_device(group, device); in __iommu_group_remove_device()
662 mutex_unlock(&group->mutex); in __iommu_group_remove_device()
668 iommu_group_put(group); in __iommu_group_remove_device()
673 struct iommu_group *group = dev->iommu_group; in iommu_release_device() local
675 if (group) in iommu_release_device()
722 struct iommu_group *group = to_iommu_group(kobj); in iommu_group_attr_show() local
726 ret = attr->show(group, buf); in iommu_group_attr_show()
735 struct iommu_group *group = to_iommu_group(kobj); in iommu_group_attr_store() local
739 ret = attr->store(group, buf, count); in iommu_group_attr_store()
748 static int iommu_group_create_file(struct iommu_group *group, in iommu_group_create_file() argument
751 return sysfs_create_file(&group->kobj, &attr->attr); in iommu_group_create_file()
754 static void iommu_group_remove_file(struct iommu_group *group, in iommu_group_remove_file() argument
757 sysfs_remove_file(&group->kobj, &attr->attr); in iommu_group_remove_file()
760 static ssize_t iommu_group_show_name(struct iommu_group *group, char *buf) in iommu_group_show_name() argument
762 return sysfs_emit(buf, "%s\n", group->name); in iommu_group_show_name()
841 int iommu_get_group_resv_regions(struct iommu_group *group, in iommu_get_group_resv_regions() argument
847 mutex_lock(&group->mutex); in iommu_get_group_resv_regions()
848 for_each_group_device(group, device) { in iommu_get_group_resv_regions()
865 mutex_unlock(&group->mutex); in iommu_get_group_resv_regions()
870 static ssize_t iommu_group_show_resv_regions(struct iommu_group *group, in iommu_group_show_resv_regions() argument
878 iommu_get_group_resv_regions(group, &group_resv_regions); in iommu_group_show_resv_regions()
892 static ssize_t iommu_group_show_type(struct iommu_group *group, in iommu_group_show_type() argument
897 mutex_lock(&group->mutex); in iommu_group_show_type()
898 if (group->default_domain) { in iommu_group_show_type()
899 switch (group->default_domain->type) { in iommu_group_show_type()
917 mutex_unlock(&group->mutex); in iommu_group_show_type()
932 struct iommu_group *group = to_iommu_group(kobj); in iommu_group_release() local
934 pr_debug("Releasing group %d\n", group->id); in iommu_group_release()
936 if (group->iommu_data_release) in iommu_group_release()
937 group->iommu_data_release(group->iommu_data); in iommu_group_release()
939 ida_free(&iommu_group_ida, group->id); in iommu_group_release()
942 WARN_ON(group->default_domain); in iommu_group_release()
943 WARN_ON(group->blocking_domain); in iommu_group_release()
945 kfree(group->name); in iommu_group_release()
946 kfree(group); in iommu_group_release()
955 * iommu_group_alloc - Allocate a new group
958 * group. The iommu group represents the minimum granularity of the iommu.
960 * group in order to hold the group until devices are added. Use
962 * group to be automatically reclaimed once it has no devices or external
967 struct iommu_group *group; in iommu_group_alloc() local
970 group = kzalloc(sizeof(*group), GFP_KERNEL); in iommu_group_alloc()
971 if (!group) in iommu_group_alloc()
974 group->kobj.kset = iommu_group_kset; in iommu_group_alloc()
975 mutex_init(&group->mutex); in iommu_group_alloc()
976 INIT_LIST_HEAD(&group->devices); in iommu_group_alloc()
977 INIT_LIST_HEAD(&group->entry); in iommu_group_alloc()
978 xa_init(&group->pasid_array); in iommu_group_alloc()
982 kfree(group); in iommu_group_alloc()
985 group->id = ret; in iommu_group_alloc()
987 ret = kobject_init_and_add(&group->kobj, &iommu_group_ktype, in iommu_group_alloc()
988 NULL, "%d", group->id); in iommu_group_alloc()
990 kobject_put(&group->kobj); in iommu_group_alloc()
994 group->devices_kobj = kobject_create_and_add("devices", &group->kobj); in iommu_group_alloc()
995 if (!group->devices_kobj) { in iommu_group_alloc()
996 kobject_put(&group->kobj); /* triggers .release & free */ in iommu_group_alloc()
1001 * The devices_kobj holds a reference on the group kobject, so in iommu_group_alloc()
1002 * as long as that exists so will the group. We can therefore in iommu_group_alloc()
1005 kobject_put(&group->kobj); in iommu_group_alloc()
1007 ret = iommu_group_create_file(group, in iommu_group_alloc()
1010 kobject_put(group->devices_kobj); in iommu_group_alloc()
1014 ret = iommu_group_create_file(group, &iommu_group_attr_type); in iommu_group_alloc()
1016 kobject_put(group->devices_kobj); in iommu_group_alloc()
1020 pr_debug("Allocated group %d\n", group->id); in iommu_group_alloc()
1022 return group; in iommu_group_alloc()
1027 * iommu_group_get_iommudata - retrieve iommu_data registered for a group
1028 * @group: the group
1030 * iommu drivers can store data in the group for use when doing iommu
1032 * should hold a group reference.
1034 void *iommu_group_get_iommudata(struct iommu_group *group) in iommu_group_get_iommudata() argument
1036 return group->iommu_data; in iommu_group_get_iommudata()
1041 * iommu_group_set_iommudata - set iommu_data for a group
1042 * @group: the group
1046 * iommu drivers can store data in the group for use when doing iommu
1048 * the group has been allocated. Caller should hold a group reference.
1050 void iommu_group_set_iommudata(struct iommu_group *group, void *iommu_data, in iommu_group_set_iommudata() argument
1053 group->iommu_data = iommu_data; in iommu_group_set_iommudata()
1054 group->iommu_data_release = release; in iommu_group_set_iommudata()
1059 * iommu_group_set_name - set name for a group
1060 * @group: the group
1063 * Allow iommu driver to set a name for a group. When set it will
1064 * appear in a name attribute file under the group in sysfs.
1066 int iommu_group_set_name(struct iommu_group *group, const char *name) in iommu_group_set_name() argument
1070 if (group->name) { in iommu_group_set_name()
1071 iommu_group_remove_file(group, &iommu_group_attr_name); in iommu_group_set_name()
1072 kfree(group->name); in iommu_group_set_name()
1073 group->name = NULL; in iommu_group_set_name()
1078 group->name = kstrdup(name, GFP_KERNEL); in iommu_group_set_name()
1079 if (!group->name) in iommu_group_set_name()
1082 ret = iommu_group_create_file(group, &iommu_group_attr_name); in iommu_group_set_name()
1084 kfree(group->name); in iommu_group_set_name()
1085 group->name = NULL; in iommu_group_set_name()
1156 static struct group_device *iommu_group_alloc_device(struct iommu_group *group, in iommu_group_alloc_device() argument
1168 ret = sysfs_create_link(&dev->kobj, &group->kobj, "iommu_group"); in iommu_group_alloc_device()
1179 ret = sysfs_create_link_nowarn(group->devices_kobj, in iommu_group_alloc_device()
1195 trace_add_device_to_group(group->id, dev); in iommu_group_alloc_device()
1197 dev_info(dev, "Adding to iommu group %d\n", group->id); in iommu_group_alloc_device()
1207 dev_err(dev, "Failed to add to iommu group %d: %d\n", group->id, ret); in iommu_group_alloc_device()
1212 * iommu_group_add_device - add a device to an iommu group
1213 * @group: the group into which to add the device (reference should be held)
1217 * group. Adding a device increments the group reference count.
1219 int iommu_group_add_device(struct iommu_group *group, struct device *dev) in iommu_group_add_device() argument
1223 gdev = iommu_group_alloc_device(group, dev); in iommu_group_add_device()
1227 iommu_group_ref_get(group); in iommu_group_add_device()
1228 dev->iommu_group = group; in iommu_group_add_device()
1230 mutex_lock(&group->mutex); in iommu_group_add_device()
1231 list_add_tail(&gdev->list, &group->devices); in iommu_group_add_device()
1232 mutex_unlock(&group->mutex); in iommu_group_add_device()
1238 * iommu_group_remove_device - remove a device from it's current group
1242 * it's current group. This decrements the iommu group reference count.
1246 struct iommu_group *group = dev->iommu_group; in iommu_group_remove_device() local
1248 if (!group) in iommu_group_remove_device()
1251 dev_info(dev, "Removing from iommu group %d\n", group->id); in iommu_group_remove_device()
1259 * iommu_group_mutex_assert - Check device group mutex lock
1260 * @dev: the device that has group param set
1263 * group mutex lock for the given device or not.
1265 * Note that this function must be called after device group param is set.
1269 struct iommu_group *group = dev->iommu_group; in iommu_group_mutex_assert() local
1271 lockdep_assert_held(&group->mutex); in iommu_group_mutex_assert()
1276 static struct device *iommu_group_first_dev(struct iommu_group *group) in iommu_group_first_dev() argument
1278 lockdep_assert_held(&group->mutex); in iommu_group_first_dev()
1279 return list_first_entry(&group->devices, struct group_device, list)->dev; in iommu_group_first_dev()
1283 * iommu_group_for_each_dev - iterate over each device in the group
1284 * @group: the group
1288 * This function is called by group users to iterate over group devices.
1289 * Callers should hold a reference count to the group during callback.
1290 * The group->mutex is held across callbacks, which will block calls to
1293 int iommu_group_for_each_dev(struct iommu_group *group, void *data, in iommu_group_for_each_dev() argument
1299 mutex_lock(&group->mutex); in iommu_group_for_each_dev()
1300 for_each_group_device(group, device) { in iommu_group_for_each_dev()
1305 mutex_unlock(&group->mutex); in iommu_group_for_each_dev()
1312 * iommu_group_get - Return the group for a device and increment reference
1313 * @dev: get the group that this device belongs to
1315 * This function is called by iommu drivers and users to get the group
1316 * for the specified device. If found, the group is returned and the group
1321 struct iommu_group *group = dev->iommu_group; in iommu_group_get() local
1323 if (group) in iommu_group_get()
1324 kobject_get(group->devices_kobj); in iommu_group_get()
1326 return group; in iommu_group_get()
1331 * iommu_group_ref_get - Increment reference on a group
1332 * @group: the group to use, must not be NULL
1335 * existing group. Returns the given group for convenience.
1337 struct iommu_group *iommu_group_ref_get(struct iommu_group *group) in iommu_group_ref_get() argument
1339 kobject_get(group->devices_kobj); in iommu_group_ref_get()
1340 return group; in iommu_group_ref_get()
1345 * iommu_group_put - Decrement group reference
1346 * @group: the group to use
1349 * iommu group. Once the reference count is zero, the group is released.
1351 void iommu_group_put(struct iommu_group *group) in iommu_group_put() argument
1353 if (group) in iommu_group_put()
1354 kobject_put(group->devices_kobj); in iommu_group_put()
1359 * iommu_group_id - Return ID for a group
1360 * @group: the group to ID
1362 * Return the unique ID for the group matching the sysfs group number.
1364 int iommu_group_id(struct iommu_group *group) in iommu_group_id() argument
1366 return group->id; in iommu_group_id()
1387 * that may already have a group.
1393 struct iommu_group *group; in get_pci_function_alias_group() local
1404 group = get_pci_alias_group(tmp, devfns); in get_pci_function_alias_group()
1405 if (group) { in get_pci_function_alias_group()
1407 return group; in get_pci_function_alias_group()
1427 struct iommu_group *group; in get_pci_alias_group() local
1432 group = iommu_group_get(&pdev->dev); in get_pci_alias_group()
1433 if (group) in get_pci_alias_group()
1434 return group; in get_pci_alias_group()
1442 group = get_pci_alias_group(tmp, devfns); in get_pci_alias_group()
1443 if (group) { in get_pci_alias_group()
1445 return group; in get_pci_alias_group()
1448 group = get_pci_function_alias_group(tmp, devfns); in get_pci_alias_group()
1449 if (group) { in get_pci_alias_group()
1451 return group; in get_pci_alias_group()
1461 struct iommu_group *group; member
1466 * the IOMMU group if we find one along the way.
1473 data->group = iommu_group_get(&pdev->dev); in get_pci_alias_or_group()
1475 return data->group != NULL; in get_pci_alias_or_group()
1480 * iommu-group per device.
1490 * iommu-group per iommu driver instance shared by every device
1498 struct iommu_group *group; in generic_single_device_group() local
1500 group = iommu_group_alloc(); in generic_single_device_group()
1501 if (IS_ERR(group)) in generic_single_device_group()
1502 return group; in generic_single_device_group()
1503 iommu->singleton_group = group; in generic_single_device_group()
1511 * to find or create an IOMMU group for a device.
1518 struct iommu_group *group = NULL; in pci_device_group() local
1526 * be aliased due to topology in order to have its own IOMMU group. in pci_device_group()
1528 * group, use it. in pci_device_group()
1531 return data.group; in pci_device_group()
1539 * group, use it. in pci_device_group()
1550 group = iommu_group_get(&pdev->dev); in pci_device_group()
1551 if (group) in pci_device_group()
1552 return group; in pci_device_group()
1557 * device or another device aliases us, use the same group. in pci_device_group()
1559 group = get_pci_alias_group(pdev, (unsigned long *)devfns); in pci_device_group()
1560 if (group) in pci_device_group()
1561 return group; in pci_device_group()
1568 group = get_pci_function_alias_group(pdev, (unsigned long *)devfns); in pci_device_group()
1569 if (group) in pci_device_group()
1570 return group; in pci_device_group()
1572 /* No shared group found, allocate new */ in pci_device_group()
1577 /* Get the IOMMU group for device on fsl-mc bus */
1581 struct iommu_group *group; in fsl_mc_device_group() local
1583 group = iommu_group_get(cont_dev); in fsl_mc_device_group()
1584 if (!group) in fsl_mc_device_group()
1585 group = iommu_group_alloc(); in fsl_mc_device_group()
1586 return group; in fsl_mc_device_group()
1613 __iommu_group_alloc_default_domain(struct iommu_group *group, int req_type) in __iommu_group_alloc_default_domain() argument
1615 struct device *dev = iommu_group_first_dev(group); in __iommu_group_alloc_default_domain()
1618 if (group->default_domain && group->default_domain->type == req_type) in __iommu_group_alloc_default_domain()
1619 return group->default_domain; in __iommu_group_alloc_default_domain()
1650 iommu_group_alloc_default_domain(struct iommu_group *group, int req_type) in iommu_group_alloc_default_domain() argument
1652 const struct iommu_ops *ops = dev_iommu_ops(iommu_group_first_dev(group)); in iommu_group_alloc_default_domain()
1655 lockdep_assert_held(&group->mutex); in iommu_group_alloc_default_domain()
1669 return __iommu_group_alloc_default_domain(group, req_type); in iommu_group_alloc_default_domain()
1672 dom = __iommu_group_alloc_default_domain(group, iommu_def_domain_type); in iommu_group_alloc_default_domain()
1679 dom = __iommu_group_alloc_default_domain(group, IOMMU_DOMAIN_DMA); in iommu_group_alloc_default_domain()
1683 …pr_warn("Failed to allocate default IOMMU domain of type %u for group %s - Falling back to IOMMU_D… in iommu_group_alloc_default_domain()
1684 iommu_def_domain_type, group->name); in iommu_group_alloc_default_domain()
1688 struct iommu_domain *iommu_group_default_domain(struct iommu_group *group) in iommu_group_default_domain() argument
1690 return group->default_domain; in iommu_group_default_domain()
1727 * group. Drivers must give a consistent result.
1729 static int iommu_get_def_domain_type(struct iommu_group *group, in iommu_get_def_domain_type() argument
1754 …"IOMMU driver error, requesting conflicting def_domain_type, %s and %s, for devices in group %u.\n… in iommu_get_def_domain_type()
1756 group->id); in iommu_get_def_domain_type()
1771 static int iommu_get_default_domain_type(struct iommu_group *group, in iommu_get_default_domain_type() argument
1778 lockdep_assert_held(&group->mutex); in iommu_get_default_domain_type()
1792 for_each_group_device(group, gdev) { in iommu_get_default_domain_type()
1793 driver_type = iommu_get_def_domain_type(group, gdev->dev, in iommu_get_default_domain_type()
1823 "Device is not trusted, but driver is overriding group %u to %s, refusing to probe.\n", in iommu_get_default_domain_type()
1824 group->id, iommu_domain_type_str(driver_type)); in iommu_get_default_domain_type()
1848 struct iommu_group *group, *next; in bus_iommu_probe() local
1856 list_for_each_entry_safe(group, next, &group_list, entry) { in bus_iommu_probe()
1859 mutex_lock(&group->mutex); in bus_iommu_probe()
1862 list_del_init(&group->entry); in bus_iommu_probe()
1866 * that the cross-group default domain type and the setup of the in bus_iommu_probe()
1869 ret = iommu_setup_default_domain(group, 0); in bus_iommu_probe()
1871 mutex_unlock(&group->mutex); in bus_iommu_probe()
1874 for_each_group_device(group, gdev) in bus_iommu_probe()
1876 mutex_unlock(&group->mutex); in bus_iommu_probe()
1882 * to take group->mutex, resulting in a deadlock. in bus_iommu_probe()
1884 for_each_group_device(group, gdev) in bus_iommu_probe()
1916 * for a group
1917 * @group: Group to query
1920 * msi_device_has_isolated_msi() for devices in a group. However nothing
1924 bool iommu_group_has_isolated_msi(struct iommu_group *group) in iommu_group_has_isolated_msi() argument
1929 mutex_lock(&group->mutex); in iommu_group_has_isolated_msi()
1930 for_each_group_device(group, group_dev) in iommu_group_has_isolated_msi()
1932 mutex_unlock(&group->mutex); in iommu_group_has_isolated_msi()
2033 * Put the group's domain back to the appropriate core-owned domain - either the
2036 static void __iommu_group_set_core_domain(struct iommu_group *group) in __iommu_group_set_core_domain() argument
2040 if (group->owner) in __iommu_group_set_core_domain()
2041 new_domain = group->blocking_domain; in __iommu_group_set_core_domain()
2043 new_domain = group->default_domain; in __iommu_group_set_core_domain()
2045 __iommu_group_set_domain_nofail(group, new_domain); in __iommu_group_set_core_domain()
2079 struct iommu_group *group = dev->iommu_group; in iommu_attach_device() local
2082 if (!group) in iommu_attach_device()
2086 * Lock the group to make sure the device-count doesn't in iommu_attach_device()
2089 mutex_lock(&group->mutex); in iommu_attach_device()
2091 if (list_count_nodes(&group->devices) != 1) in iommu_attach_device()
2094 ret = __iommu_attach_group(domain, group); in iommu_attach_device()
2097 mutex_unlock(&group->mutex); in iommu_attach_device()
2113 struct iommu_group *group = dev->iommu_group; in iommu_detach_device() local
2115 if (!group) in iommu_detach_device()
2118 mutex_lock(&group->mutex); in iommu_detach_device()
2119 if (WARN_ON(domain != group->domain) || in iommu_detach_device()
2120 WARN_ON(list_count_nodes(&group->devices) != 1)) in iommu_detach_device()
2122 __iommu_group_set_core_domain(group); in iommu_detach_device()
2125 mutex_unlock(&group->mutex); in iommu_detach_device()
2132 struct iommu_group *group = dev->iommu_group; in iommu_get_domain_for_dev() local
2134 if (!group) in iommu_get_domain_for_dev()
2137 return group->domain; in iommu_get_domain_for_dev()
2143 * guarantees that the group and its default domain are valid and correct.
2151 struct iommu_group *group) in __iommu_attach_group() argument
2155 if (group->domain && group->domain != group->default_domain && in __iommu_attach_group()
2156 group->domain != group->blocking_domain) in __iommu_attach_group()
2159 dev = iommu_group_first_dev(group); in __iommu_attach_group()
2163 return __iommu_group_set_domain(group, domain); in __iommu_attach_group()
2167 * iommu_attach_group - Attach an IOMMU domain to an IOMMU group
2169 * @group: IOMMU group that will be attached
2175 * the group. In this case attaching a different domain to the
2176 * group may succeed.
2178 int iommu_attach_group(struct iommu_domain *domain, struct iommu_group *group) in iommu_attach_group() argument
2182 mutex_lock(&group->mutex); in iommu_attach_group()
2183 ret = __iommu_attach_group(domain, group); in iommu_attach_group()
2184 mutex_unlock(&group->mutex); in iommu_attach_group()
2191 * iommu_group_replace_domain - replace the domain that a group is attached to
2192 * @group: IOMMU group that will be attached to the new domain
2195 * This API allows the group to switch domains without being forced to go to
2201 int iommu_group_replace_domain(struct iommu_group *group, in iommu_group_replace_domain() argument
2209 mutex_lock(&group->mutex); in iommu_group_replace_domain()
2210 ret = __iommu_group_set_domain(group, new_domain); in iommu_group_replace_domain()
2211 mutex_unlock(&group->mutex); in iommu_group_replace_domain()
2216 static int __iommu_device_set_domain(struct iommu_group *group, in __iommu_device_set_domain() argument
2232 new_domain == group->blocking_domain)) { in __iommu_device_set_domain()
2239 if (new_domain == group->default_domain) in __iommu_device_set_domain()
2252 group->blocking_domain && in __iommu_device_set_domain()
2253 group->blocking_domain != new_domain) in __iommu_device_set_domain()
2254 __iommu_attach_device(group->blocking_domain, dev); in __iommu_device_set_domain()
2261 * If 0 is returned the group's domain is new_domain. If an error is returned
2262 * then the group's domain will be set back to the existing domain unless
2263 * IOMMU_SET_DOMAIN_MUST_SUCCEED, otherwise an error is returned and the group's
2269 * devices in a group. Ideally we'd have a single device which represents the
2270 * requestor ID of the group, but we also allow IOMMU drivers to create policy
2272 * members, but we wish to group them at a higher level (ex. untrusted
2275 static int __iommu_group_set_domain_internal(struct iommu_group *group, in __iommu_group_set_domain_internal() argument
2284 lockdep_assert_held(&group->mutex); in __iommu_group_set_domain_internal()
2286 if (group->domain == new_domain) in __iommu_group_set_domain_internal()
2296 * either new_domain or group->domain, never something else. in __iommu_group_set_domain_internal()
2299 for_each_group_device(group, gdev) { in __iommu_group_set_domain_internal()
2300 ret = __iommu_device_set_domain(group, gdev->dev, new_domain, in __iommu_group_set_domain_internal()
2305 * Keep trying the other devices in the group. If a in __iommu_group_set_domain_internal()
2316 group->domain = new_domain; in __iommu_group_set_domain_internal()
2325 for_each_group_device(group, gdev) { in __iommu_group_set_domain_internal()
2328 * we leave group->domain as NULL and let release clean in __iommu_group_set_domain_internal()
2331 if (group->domain) in __iommu_group_set_domain_internal()
2333 group, gdev->dev, group->domain, in __iommu_group_set_domain_internal()
2341 void iommu_detach_group(struct iommu_domain *domain, struct iommu_group *group) in iommu_detach_group() argument
2343 mutex_lock(&group->mutex); in iommu_detach_group()
2344 __iommu_group_set_core_domain(group); in iommu_detach_group()
2345 mutex_unlock(&group->mutex); in iommu_detach_group()
2913 * iommu_setup_default_domain - Set the default_domain for the group
2914 * @group: Group to change
2917 * Allocate a default domain and set it as the current domain on the group. If
2918 * the group already has a default domain it will be changed to the target_type.
2922 static int iommu_setup_default_domain(struct iommu_group *group, in iommu_setup_default_domain() argument
2925 struct iommu_domain *old_dom = group->default_domain; in iommu_setup_default_domain()
2932 lockdep_assert_held(&group->mutex); in iommu_setup_default_domain()
2934 req_type = iommu_get_default_domain_type(group, target_type); in iommu_setup_default_domain()
2938 dom = iommu_group_alloc_default_domain(group, req_type); in iommu_setup_default_domain()
2942 if (group->default_domain == dom) in iommu_setup_default_domain()
2959 for_each_group_device(group, gdev) { in iommu_setup_default_domain()
2969 group->default_domain = dom; in iommu_setup_default_domain()
2970 if (!group->domain) { in iommu_setup_default_domain()
2975 * in group->default_domain so it is freed after. in iommu_setup_default_domain()
2978 group, dom, IOMMU_SET_DOMAIN_MUST_SUCCEED); in iommu_setup_default_domain()
2982 ret = __iommu_group_set_domain(group, dom); in iommu_setup_default_domain()
2994 for_each_group_device(group, gdev) { in iommu_setup_default_domain()
3009 group, old_dom, IOMMU_SET_DOMAIN_MUST_SUCCEED); in iommu_setup_default_domain()
3013 group->default_domain = old_dom; in iommu_setup_default_domain()
3020 * drivers from the devices in the iommu group, except for a DMA -> DMA-FQ
3024 * group->mutex is used here to guarantee that the device release path
3027 static ssize_t iommu_group_store_type(struct iommu_group *group, in iommu_group_store_type() argument
3036 if (WARN_ON(!group) || !group->default_domain) in iommu_group_store_type()
3050 mutex_lock(&group->mutex); in iommu_group_store_type()
3053 group->default_domain->type == IOMMU_DOMAIN_DMA) { in iommu_group_store_type()
3054 ret = iommu_dma_init_fq(group->default_domain); in iommu_group_store_type()
3058 group->default_domain->type = IOMMU_DOMAIN_DMA_FQ; in iommu_group_store_type()
3064 if (list_empty(&group->devices) || group->owner_cnt) { in iommu_group_store_type()
3069 ret = iommu_setup_default_domain(group, req_type); in iommu_group_store_type()
3074 for_each_group_device(group, gdev) in iommu_group_store_type()
3078 mutex_unlock(&group->mutex); in iommu_group_store_type()
3093 struct iommu_group *group = dev->iommu_group; in iommu_device_use_default_domain() local
3096 if (!group) in iommu_device_use_default_domain()
3099 mutex_lock(&group->mutex); in iommu_device_use_default_domain()
3101 if (!group->default_domain) { in iommu_device_use_default_domain()
3105 if (group->owner_cnt) { in iommu_device_use_default_domain()
3106 if (group->domain != group->default_domain || group->owner || in iommu_device_use_default_domain()
3107 !xa_empty(&group->pasid_array)) { in iommu_device_use_default_domain()
3113 group->owner_cnt++; in iommu_device_use_default_domain()
3116 mutex_unlock(&group->mutex); in iommu_device_use_default_domain()
3131 struct iommu_group *group = dev->iommu_group; in iommu_device_unuse_default_domain() local
3133 if (!group) in iommu_device_unuse_default_domain()
3136 mutex_lock(&group->mutex); in iommu_device_unuse_default_domain()
3137 if (!WARN_ON(!group->owner_cnt || !xa_empty(&group->pasid_array))) in iommu_device_unuse_default_domain()
3138 group->owner_cnt--; in iommu_device_unuse_default_domain()
3140 mutex_unlock(&group->mutex); in iommu_device_unuse_default_domain()
3143 static int __iommu_group_alloc_blocking_domain(struct iommu_group *group) in __iommu_group_alloc_blocking_domain() argument
3145 struct device *dev = iommu_group_first_dev(group); in __iommu_group_alloc_blocking_domain()
3149 if (group->blocking_domain) in __iommu_group_alloc_blocking_domain()
3153 group->blocking_domain = ops->blocked_domain; in __iommu_group_alloc_blocking_domain()
3164 group->blocking_domain = domain; in __iommu_group_alloc_blocking_domain()
3168 static int __iommu_take_dma_ownership(struct iommu_group *group, void *owner) in __iommu_take_dma_ownership() argument
3172 if ((group->domain && group->domain != group->default_domain) || in __iommu_take_dma_ownership()
3173 !xa_empty(&group->pasid_array)) in __iommu_take_dma_ownership()
3176 ret = __iommu_group_alloc_blocking_domain(group); in __iommu_take_dma_ownership()
3179 ret = __iommu_group_set_domain(group, group->blocking_domain); in __iommu_take_dma_ownership()
3183 group->owner = owner; in __iommu_take_dma_ownership()
3184 group->owner_cnt++; in __iommu_take_dma_ownership()
3189 * iommu_group_claim_dma_owner() - Set DMA ownership of a group
3190 * @group: The group.
3195 * prohibited. Only a single owner may exist for a group.
3197 int iommu_group_claim_dma_owner(struct iommu_group *group, void *owner) in iommu_group_claim_dma_owner() argument
3204 mutex_lock(&group->mutex); in iommu_group_claim_dma_owner()
3205 if (group->owner_cnt) { in iommu_group_claim_dma_owner()
3210 ret = __iommu_take_dma_ownership(group, owner); in iommu_group_claim_dma_owner()
3212 mutex_unlock(&group->mutex); in iommu_group_claim_dma_owner()
3223 * Claim the DMA ownership of a device. Multiple devices in the same group may
3230 struct iommu_group *group = dev->iommu_group; in iommu_device_claim_dma_owner() local
3236 if (!group) in iommu_device_claim_dma_owner()
3239 mutex_lock(&group->mutex); in iommu_device_claim_dma_owner()
3240 if (group->owner_cnt) { in iommu_device_claim_dma_owner()
3241 if (group->owner != owner) { in iommu_device_claim_dma_owner()
3245 group->owner_cnt++; in iommu_device_claim_dma_owner()
3249 ret = __iommu_take_dma_ownership(group, owner); in iommu_device_claim_dma_owner()
3251 mutex_unlock(&group->mutex); in iommu_device_claim_dma_owner()
3256 static void __iommu_release_dma_ownership(struct iommu_group *group) in __iommu_release_dma_ownership() argument
3258 if (WARN_ON(!group->owner_cnt || !group->owner || in __iommu_release_dma_ownership()
3259 !xa_empty(&group->pasid_array))) in __iommu_release_dma_ownership()
3262 group->owner_cnt = 0; in __iommu_release_dma_ownership()
3263 group->owner = NULL; in __iommu_release_dma_ownership()
3264 __iommu_group_set_domain_nofail(group, group->default_domain); in __iommu_release_dma_ownership()
3268 * iommu_group_release_dma_owner() - Release DMA ownership of a group
3269 * @group: The group
3273 void iommu_group_release_dma_owner(struct iommu_group *group) in iommu_group_release_dma_owner() argument
3275 mutex_lock(&group->mutex); in iommu_group_release_dma_owner()
3276 __iommu_release_dma_ownership(group); in iommu_group_release_dma_owner()
3277 mutex_unlock(&group->mutex); in iommu_group_release_dma_owner()
3290 struct iommu_group *group = dev->iommu_group; in iommu_device_release_dma_owner() local
3292 mutex_lock(&group->mutex); in iommu_device_release_dma_owner()
3293 if (group->owner_cnt > 1) in iommu_device_release_dma_owner()
3294 group->owner_cnt--; in iommu_device_release_dma_owner()
3296 __iommu_release_dma_ownership(group); in iommu_device_release_dma_owner()
3297 mutex_unlock(&group->mutex); in iommu_device_release_dma_owner()
3302 * iommu_group_dma_owner_claimed() - Query group dma ownership status
3303 * @group: The group.
3305 * This provides status query on a given group. It is racy and only for
3308 bool iommu_group_dma_owner_claimed(struct iommu_group *group) in iommu_group_dma_owner_claimed() argument
3312 mutex_lock(&group->mutex); in iommu_group_dma_owner_claimed()
3313 user = group->owner_cnt; in iommu_group_dma_owner_claimed()
3314 mutex_unlock(&group->mutex); in iommu_group_dma_owner_claimed()
3331 struct iommu_group *group, ioasid_t pasid) in __iommu_set_group_pasid() argument
3336 for_each_group_device(group, device) { in __iommu_set_group_pasid()
3347 for_each_group_device(group, device) { in __iommu_set_group_pasid()
3355 static void __iommu_remove_group_pasid(struct iommu_group *group, in __iommu_remove_group_pasid() argument
3361 for_each_group_device(group, device) in __iommu_remove_group_pasid()
3379 struct iommu_group *group = dev->iommu_group; in iommu_attach_device_pasid() local
3384 if (!group) in iommu_attach_device_pasid()
3397 mutex_lock(&group->mutex); in iommu_attach_device_pasid()
3398 for_each_group_device(group, device) { in iommu_attach_device_pasid()
3408 ret = xa_insert(&group->pasid_array, pasid, handle, GFP_KERNEL); in iommu_attach_device_pasid()
3412 ret = __iommu_set_group_pasid(domain, group, pasid); in iommu_attach_device_pasid()
3414 xa_erase(&group->pasid_array, pasid); in iommu_attach_device_pasid()
3416 mutex_unlock(&group->mutex); in iommu_attach_device_pasid()
3434 struct iommu_group *group = dev->iommu_group; in iommu_detach_device_pasid() local
3436 mutex_lock(&group->mutex); in iommu_detach_device_pasid()
3437 __iommu_remove_group_pasid(group, pasid, domain); in iommu_detach_device_pasid()
3438 xa_erase(&group->pasid_array, pasid); in iommu_detach_device_pasid()
3439 mutex_unlock(&group->mutex); in iommu_detach_device_pasid()
3472 * @group: the iommu group that domain was attached to
3473 * @pasid: the pasid within the group
3485 iommu_attach_handle_get(struct iommu_group *group, ioasid_t pasid, unsigned int type) in iommu_attach_handle_get() argument
3489 xa_lock(&group->pasid_array); in iommu_attach_handle_get()
3490 handle = xa_load(&group->pasid_array, pasid); in iommu_attach_handle_get()
3495 xa_unlock(&group->pasid_array); in iommu_attach_handle_get()
3502 * iommu_attach_group_handle - Attach an IOMMU domain to an IOMMU group
3504 * @group: IOMMU group that will be attached
3514 struct iommu_group *group, in iommu_attach_group_handle() argument
3522 mutex_lock(&group->mutex); in iommu_attach_group_handle()
3523 ret = xa_insert(&group->pasid_array, IOMMU_NO_PASID, handle, GFP_KERNEL); in iommu_attach_group_handle()
3527 ret = __iommu_attach_group(domain, group); in iommu_attach_group_handle()
3530 mutex_unlock(&group->mutex); in iommu_attach_group_handle()
3534 xa_erase(&group->pasid_array, IOMMU_NO_PASID); in iommu_attach_group_handle()
3536 mutex_unlock(&group->mutex); in iommu_attach_group_handle()
3542 * iommu_detach_group_handle - Detach an IOMMU domain from an IOMMU group
3544 * @group: IOMMU group that will be attached
3546 * Detach the specified IOMMU domain from the specified IOMMU group.
3550 struct iommu_group *group) in iommu_detach_group_handle() argument
3552 mutex_lock(&group->mutex); in iommu_detach_group_handle()
3553 __iommu_group_set_core_domain(group); in iommu_detach_group_handle()
3554 xa_erase(&group->pasid_array, IOMMU_NO_PASID); in iommu_detach_group_handle()
3555 mutex_unlock(&group->mutex); in iommu_detach_group_handle()
3560 * iommu_replace_group_handle - replace the domain that a group is attached to
3561 * @group: IOMMU group that will be attached to the new domain
3569 int iommu_replace_group_handle(struct iommu_group *group, in iommu_replace_group_handle() argument
3579 mutex_lock(&group->mutex); in iommu_replace_group_handle()
3581 ret = xa_reserve(&group->pasid_array, IOMMU_NO_PASID, GFP_KERNEL); in iommu_replace_group_handle()
3587 ret = __iommu_group_set_domain(group, new_domain); in iommu_replace_group_handle()
3591 curr = xa_store(&group->pasid_array, IOMMU_NO_PASID, handle, GFP_KERNEL); in iommu_replace_group_handle()
3594 mutex_unlock(&group->mutex); in iommu_replace_group_handle()
3598 xa_release(&group->pasid_array, IOMMU_NO_PASID); in iommu_replace_group_handle()
3600 mutex_unlock(&group->mutex); in iommu_replace_group_handle()