Lines Matching refs:group

59     ssize_t (*show)(struct iommu_group *group, char *buf);

60 ssize_t (*store)(struct iommu_group *group, const char *buf, size_t count);
81 static int iommu_alloc_default_domain(struct iommu_group *group, struct device *dev);
84 static int iommu_attach_group_ext(struct iommu_domain *domain, struct iommu_group *group);
85 static void iommu_detach_group_ext(struct iommu_domain *domain, struct iommu_group *group);
86 static int iommu_create_device_direct_mappings(struct iommu_group *group, struct device *dev);
193 struct iommu_group *group;
217 group = iommu_group_get_for_dev(dev);
218 if (IS_ERR(group)) {
219 ret = PTR_ERR(group);
222 iommu_group_put(group);
224 if (group_list && !group->default_domain && list_empty(&group->entry)) {
225 list_add_tail(&group->entry, group_list);
247 struct iommu_group *group;
255 group = iommu_group_get(dev);
256 if (!group) {
266 iommu_alloc_default_domain(group, dev);
268 if (group->default_domain) {
269 ret = iommu_attach_device_ext(group->default_domain, dev);
271 iommu_group_put(group);
276 iommu_create_device_direct_mappings(group, dev);
278 iommu_group_put(group);
339 struct iommu_group *group = to_iommu_group(kobj);
343 ret = attr->show(group, buf);
351 struct iommu_group *group = to_iommu_group(kobj);
355 ret = attr->store(group, buf, count);
365 static int iommu_group_create_file(struct iommu_group *group, struct iommu_group_attribute *attr)
367 return sysfs_create_file(&group->kobj, &attr->attr);
370 static void iommu_group_remove_file(struct iommu_group *group, struct iommu_group_attribute *attr)
372 sysfs_remove_file(&group->kobj, &attr->attr);
375 static ssize_t iommu_group_show_name(struct iommu_group *group, char *buf)
377 return sprintf(buf, "%s\n", group->name);
455 int iommu_get_group_resv_regions(struct iommu_group *group, struct list_head *head)
460 mutex_lock(&group->mutex);
461 list_for_each_entry(device, &group->devices, list)
473 mutex_unlock(&group->mutex);
478 static ssize_t iommu_group_show_resv_regions(struct iommu_group *group, char *buf)
485 iommu_get_group_resv_regions(group, &group_resv_regions);
497 static ssize_t iommu_group_show_type(struct iommu_group *group, char *buf)
501 if (group->default_domain) {
502 switch (group->default_domain->type) {
530 struct iommu_group *group = to_iommu_group(kobj);
532 pr_debug("Releasing group %d\n", group->id);
534 if (group->iommu_data_release) {
535 group->iommu_data_release(group->iommu_data);
538 ida_simple_remove(&iommu_group_ida, group->id);
540 if (group->default_domain) {
541 iommu_domain_free(group->default_domain);
544 kfree(group->name);
545 kfree(group);
554 * iommu_group_alloc - Allocate a new group
557 * group. The iommu group represents the minimum granularity of the iommu.
559 * group in order to hold the group until devices are added. Use
561 * group to be automatically reclaimed once it has no devices or external
566 struct iommu_group *group;
569 group = kzalloc(sizeof(*group), GFP_KERNEL);
570 if (!group) {
574 group->kobj.kset = iommu_group_kset;
575 mutex_init(&group->mutex);
576 INIT_LIST_HEAD(&group->devices);
577 INIT_LIST_HEAD(&group->entry);
578 BLOCKING_INIT_NOTIFIER_HEAD(&group->notifier);
582 kfree(group);
585 group->id = ret;
587 ret = kobject_init_and_add(&group->kobj, &iommu_group_ktype, NULL, "%d", group->id);
589 ida_simple_remove(&iommu_group_ida, group->id);
590 kobject_put(&group->kobj);
594 group->devices_kobj = kobject_create_and_add("devices", &group->kobj);
595 if (!group->devices_kobj) {
596 kobject_put(&group->kobj); /* triggers .release & free */
601 * The devices_kobj holds a reference on the group kobject, so
602 * as long as that exists so will the group. We can therefore
605 kobject_put(&group->kobj);
607 ret = iommu_group_create_file(group, &iommu_group_attr_reserved_regions);
612 ret = iommu_group_create_file(group, &iommu_group_attr_type);
617 pr_debug("Allocated group %d\n", group->id);
619 return group;
626 struct iommu_group *group;
645 group = container_of(group_kobj, struct iommu_group, kobj);
646 BUG_ON(group->id != id);
648 kobject_get(group->devices_kobj);
649 kobject_put(&group->kobj);
651 return group;
656 * iommu_group_get_iommudata - retrieve iommu_data registered for a group
657 * @group: the group
659 * iommu drivers can store data in the group for use when doing iommu
661 * should hold a group reference.
663 void *iommu_group_get_iommudata(struct iommu_group *group)
665 return group->iommu_data;
670 * iommu_group_set_iommudata - set iommu_data for a group
671 * @group: the group
675 * iommu drivers can store data in the group for use when doing iommu
677 * the group has been allocated. Caller should hold a group reference.
679 void iommu_group_set_iommudata(struct iommu_group *group, void *iommu_data, void (*release)(void *iommu_data))
681 group->iommu_data = iommu_data;
682 group->iommu_data_release = release;
687 * iommu_group_set_name - set name for a group
688 * @group: the group
691 * Allow iommu driver to set a name for a group. When set it will
692 * appear in a name attribute file under the group in sysfs.
694 int iommu_group_set_name(struct iommu_group *group, const char *name)
698 if (group->name) {
699 iommu_group_remove_file(group, &iommu_group_attr_name);
700 kfree(group->name);
701 group->name = NULL;
707 group->name = kstrdup(name, GFP_KERNEL);
708 if (!group->name) {
712 ret = iommu_group_create_file(group, &iommu_group_attr_name);
714 kfree(group->name);
715 group->name = NULL;
723 static int iommu_create_device_direct_mappings(struct iommu_group *group, struct device *dev)
725 struct iommu_domain *domain = group->default_domain;
791 * iommu_group_add_device - add a device to an iommu group
792 * @group: the group into which to add the device (reference should be held)
796 * group. Adding a device increments the group reference count.
798 int iommu_group_add_device(struct iommu_group *group, struct device *dev)
810 ret = sysfs_create_link(&dev->kobj, &group->kobj, "iommu_group");
822 ret = sysfs_create_link_nowarn(group->devices_kobj, &dev->kobj, device->name);
838 kobject_get(group->devices_kobj);
840 dev->iommu_group = group;
842 mutex_lock(&group->mutex);
843 list_add_tail(&device->list, &group->devices);
844 if (group->domain && !iommu_is_attach_deferred(group->domain, dev)) {
845 ret = iommu_attach_device_ext(group->domain, dev);
847 mutex_unlock(&group->mutex);
852 /* Notify any listeners about change to group. */
853 blocking_notifier_call_chain(&group->notifier, IOMMU_GROUP_NOTIFY_ADD_DEVICE, dev);
855 trace_add_device_to_group(group->id, dev);
857 dev_info(dev, "Adding to iommu group %d\n", group->id);
862 mutex_lock(&group->mutex);
864 mutex_unlock(&group->mutex);
866 kobject_put(group->devices_kobj);
867 sysfs_remove_link(group->devices_kobj, device->name);
874 dev_err(dev, "Failed to add to iommu group %d: %d\n", group->id, ret);
880 * iommu_group_remove_device - remove a device from it's current group
884 * it's current group. This decrements the iommu group reference count.
888 struct iommu_group *group = dev->iommu_group;
891 if (!group) {
895 dev_info(dev, "Removing from iommu group %d\n", group->id);
898 blocking_notifier_call_chain(&group->notifier, IOMMU_GROUP_NOTIFY_DEL_DEVICE, dev);
900 mutex_lock(&group->mutex);
901 list_for_each_entry(tmp_device, &group->devices, list)
909 mutex_unlock(&group->mutex);
915 sysfs_remove_link(group->devices_kobj, device->name);
918 trace_remove_device_from_group(group->id, dev);
923 kobject_put(group->devices_kobj);
927 static int iommu_group_device_count(struct iommu_group *group)
932 list_for_each_entry(entry, &group->devices, list) ret++;
938 * iommu_group_for_each_dev - iterate over each device in the group
939 * @group: the group
943 * This function is called by group users to iterate over group devices.
944 * Callers should hold a reference count to the group during callback.
945 * The group->mutex is held across callbacks, which will block calls to
948 static int iommu_group_for_each_dev_ext(struct iommu_group *group, void *data, int (*fn)(struct device *, void *))
953 list_for_each_entry(device, &group->devices, list)
963 int iommu_group_for_each_dev(struct iommu_group *group, void *data, int (*fn)(struct device *, void *))
967 mutex_lock(&group->mutex);
968 ret = iommu_group_for_each_dev_ext(group, data, fn);
969 mutex_unlock(&group->mutex);
976 * iommu_group_get - Return the group for a device and increment reference
977 * @dev: get the group that this device belongs to
979 * This function is called by iommu drivers and users to get the group
980 * for the specified device. If found, the group is returned and the group
985 struct iommu_group *group = dev->iommu_group;
987 if (group) {
988 kobject_get(group->devices_kobj);
991 return group;
996 * iommu_group_ref_get - Increment reference on a group
997 * @group: the group to use, must not be NULL
1000 * existing group. Returns the given group for convenience.
1002 struct iommu_group *iommu_group_ref_get(struct iommu_group *group)
1004 kobject_get(group->devices_kobj);
1005 return group;
1010 * iommu_group_put - Decrement group reference
1011 * @group: the group to use
1014 * iommu group. Once the reference count is zero, the group is released.
1016 void iommu_group_put(struct iommu_group *group)
1018 if (group) {
1019 kobject_put(group->devices_kobj);
1025 * iommu_group_register_notifier - Register a notifier for group changes
1026 * @group: the group to watch
1029 * This function allows iommu group users to track changes in a group.
1031 * should hold a reference to the group throughout notifier registration.
1033 int iommu_group_register_notifier(struct iommu_group *group, struct notifier_block *nb)
1035 return blocking_notifier_chain_register(&group->notifier, nb);
1041 * @group: the group to watch
1044 * Unregister a previously registered group notifier block.
1046 int iommu_group_unregister_notifier(struct iommu_group *group, struct notifier_block *nb)
1048 return blocking_notifier_chain_unregister(&group->notifier, nb);
1240 * matched using the group ID, the PASID valid bit and the PASID
1241 * value. Otherwise only the group ID matches request and
1268 * iommu_group_id - Return ID for a group
1269 * @group: the group to ID
1271 * Return the unique ID for the group matching the sysfs group number.
1273 int iommu_group_id(struct iommu_group *group)
1275 return group->id;
1295 * that may already have a group.
1300 struct iommu_group *group;
1313 group = get_pci_alias_group(tmp, devfns);
1314 if (group) {
1316 return group;
1335 struct iommu_group *group;
1341 group = iommu_group_get(&pdev->dev);
1342 if (group) {
1343 return group;
1354 group = get_pci_alias_group(tmp, devfns);
1355 if (group) {
1357 return group;
1360 group = get_pci_function_alias_group(tmp, devfns);
1361 if (group) {
1363 return group;
1373 struct iommu_group *group;
1378 * the IOMMU group if we find one along the way.
1385 data->group = iommu_group_get(&pdev->dev);
1387 return data->group != NULL;
1392 * iommu-group per device.
1402 * to find or create an IOMMU group for a device.
1409 struct iommu_group *group = NULL;
1418 * be aliased due to topology in order to have its own IOMMU group.
1420 * group, use it.
1423 return data.group;
1432 * group, use it.
1445 group = iommu_group_get(&pdev->dev);
1446 if (group) {
1447 return group;
1453 * device or another device aliases us, use the same group.
1455 group = get_pci_alias_group(pdev, (unsigned long *)devfns);
1456 if (group) {
1457 return group;
1465 group = get_pci_function_alias_group(pdev, (unsigned long *)devfns);
1466 if (group) {
1467 return group;
1470 /* No shared group found, allocate new */
1475 /* Get the IOMMU group for device on fsl-mc bus */
1479 struct iommu_group *group;
1481 group = iommu_group_get(cont_dev);
1482 if (!group) {
1483 group = iommu_group_alloc();
1485 return group;
1501 static int iommu_group_alloc_default_domain(struct bus_type *bus, struct iommu_group *group, unsigned int type)
1510 "Failed to allocate default IOMMU domain of type %u for group %s - Falling back to IOMMU_DOMAIN_DMA",
1511 type, group->name);
1519 group->default_domain = dom;
1520 if (!group->domain) {
1521 group->domain = dom;
1532 static int iommu_alloc_default_domain(struct iommu_group *group, struct device *dev)
1536 if (group->default_domain) {
1542 return iommu_group_alloc_default_domain(dev->bus, group, type);
1546 * iommu_group_get_for_dev - Find or create the IOMMU group for a device
1551 * IOMMU group for a device. On success, the caller will hold a reference
1552 * to the returned IOMMU group, which will already include the provided
1558 struct iommu_group *group;
1561 group = iommu_group_get(dev);
1562 if (group) {
1563 return group;
1570 group = ops->device_group(dev);
1571 if (WARN_ON_ONCE(group == NULL)) {
1575 if (IS_ERR(group)) {
1576 return group;
1579 ret = iommu_group_add_device(group, dev);
1584 return group;
1587 iommu_group_put(group);
1592 struct iommu_domain *iommu_group_default_domain(struct iommu_group *group)
1594 return group->default_domain;
1600 struct iommu_group *group;
1603 /* Device is probed already if in a group */
1604 group = iommu_group_get(dev);
1605 if (group) {
1606 iommu_group_put(group);
1629 struct iommu_group *group;
1633 * result in ADD/DEL notifiers to group->notifier
1647 * group, if anyone is listening
1649 group = iommu_group_get(dev);
1650 if (!group) {
1672 blocking_notifier_call_chain(&group->notifier, group_action, dev);
1675 iommu_group_put(group);
1698 "Device needs domain type %s, but device %s in the same iommu group requires type %s - using default\n",
1712 static void probe_alloc_default_domain(struct bus_type *bus, struct iommu_group *group)
1718 /* Ask for default domain requirements of all devices in the group */
1719 iommu_group_for_each_dev_ext(group, &gtype, probe_get_default_domain_type);
1725 iommu_group_alloc_default_domain(bus, group, gtype.type);
1740 static int iommu_group_dma_attach_ext(struct iommu_group *group)
1742 return iommu_group_for_each_dev_ext(group, group->default_domain, iommu_group_do_dma_attach);
1756 static void iommu_group_dma_finalize_ext(struct iommu_group *group)
1758 iommu_group_for_each_dev_ext(group, group->default_domain, iommu_group_do_probe_finalize);
1763 struct iommu_group *group = data;
1765 iommu_create_device_direct_mappings(group, dev);
1770 static int iommu_group_create_direct_mappings(struct iommu_group *group)
1772 return iommu_group_for_each_dev_ext(group, group, iommu_do_create_direct_mappings);
1777 struct iommu_group *group, *next;
1783 * creating the iommu group, so do it after the groups are
1791 list_for_each_entry_safe(group, next, &group_list, entry)
1794 list_del_init(&group->entry);
1796 mutex_lock(&group->mutex);
1799 probe_alloc_default_domain(bus, group);
1801 if (!group->default_domain) {
1802 mutex_unlock(&group->mutex);
1806 iommu_group_create_direct_mappings(group);
1808 ret = iommu_group_dma_attach_ext(group);
1810 mutex_unlock(&group->mutex);
1816 iommu_group_dma_finalize_ext(group);
1982 struct iommu_group *group;
1985 group = iommu_group_get(dev);
1986 if (!group) {
1991 * Lock the group to make sure the device-count doesn't
1994 mutex_lock(&group->mutex);
1996 if (iommu_group_device_count(group) != 1) {
2000 ret = iommu_attach_group_ext(domain, group);
2003 mutex_unlock(&group->mutex);
2004 iommu_group_put(group);
2254 struct iommu_group *group;
2256 group = iommu_group_get(dev);
2257 if (!group) {
2261 mutex_lock(&group->mutex);
2263 if (iommu_group_device_count(group) < 1) {
2268 iommu_detach_group_ext(domain, group);
2271 mutex_unlock(&group->mutex);
2272 iommu_group_put(group);
2279 struct iommu_group *group;
2281 group = iommu_group_get(dev);
2282 if (!group) {
2286 domain = group->domain;
2288 iommu_group_put(group);
2296 * guarantees that the group and its default domain are valid and correct.
2306 * iterating over the devices in a group. Ideally we'd have a single
2307 * device which represents the requestor ID of the group, but we also
2310 * wish to group them at a higher level (ex. untrusted multi-function
2320 static int iommu_attach_group_ext(struct iommu_domain *domain, struct iommu_group *group)
2324 if (group->default_domain && group->domain != group->default_domain) {
2328 ret = iommu_group_for_each_dev_ext(group, domain, iommu_group_do_attach_device);
2330 group->domain = domain;
2336 int iommu_attach_group(struct iommu_domain *domain, struct iommu_group *group)
2340 mutex_lock(&group->mutex);
2341 ret = iommu_attach_group_ext(domain, group);
2342 mutex_unlock(&group->mutex);
2357 static void iommu_detach_group_ext(struct iommu_domain *domain, struct iommu_group *group)
2361 if (!group->default_domain) {
2362 iommu_group_for_each_dev_ext(group, domain, iommu_group_do_detach_device);
2363 group->domain = NULL;
2367 if (group->domain == group->default_domain) {
2372 ret = iommu_group_for_each_dev_ext(group, group->default_domain, iommu_group_do_attach_device);
2376 group->domain = group->default_domain;
2380 void iommu_detach_group(struct iommu_domain *domain, struct iommu_group *group)
2382 mutex_lock(&group->mutex);
2383 iommu_detach_group_ext(domain, group);
2384 mutex_unlock(&group->mutex);
3117 struct iommu_group *group;
3125 group = iommu_group_get(dev);
3126 if (!group) {
3131 mutex_lock(&group->mutex);
3139 if (iommu_group_device_count(group) != 1) {
3146 mutex_unlock(&group->mutex);
3147 iommu_group_put(group);
3165 struct iommu_group *group;
3173 group = iommu_group_get(dev);
3174 if (!group) {
3178 mutex_lock(&group->mutex);
3180 mutex_unlock(&group->mutex);
3182 iommu_group_put(group);