Blame view

drivers/iommu/iommu-sysfs.c 3.08 KB
d2912cb15   Thomas Gleixner   treewide: Replace...
1
  // SPDX-License-Identifier: GPL-2.0-only
c61959ecb   Alex Williamson   iommu: Add sysfs ...
2
3
4
5
6
  /*
   * IOMMU sysfs class support
   *
   * Copyright (C) 2014 Red Hat, Inc.  All rights reserved.
   *     Author: Alex Williamson <alex.williamson@redhat.com>
c61959ecb   Alex Williamson   iommu: Add sysfs ...
7
8
9
10
   */
  
  #include <linux/device.h>
  #include <linux/iommu.h>
c1af7b401   Paul Gortmaker   iommu: Audit and ...
11
  #include <linux/init.h>
ffd78f009   Joerg Roedel   iommu: Fix compil...
12
  #include <linux/slab.h>
c61959ecb   Alex Williamson   iommu: Add sysfs ...
13
14
15
16
17
18
19
20
  
  /*
   * We provide a common class "devices" group which initially has no attributes.
   * As devices are added to the IOMMU, we'll add links to the group.
   */
  static struct attribute *devices_attr[] = {
  	NULL,
  };
6954cf9bf   Joerg Roedel   iommu/sysfs: Rena...
21
  static const struct attribute_group devices_attr_group = {
c61959ecb   Alex Williamson   iommu: Add sysfs ...
22
23
24
  	.name = "devices",
  	.attrs = devices_attr,
  };
6954cf9bf   Joerg Roedel   iommu/sysfs: Rena...
25
26
  static const struct attribute_group *dev_groups[] = {
  	&devices_attr_group,
c61959ecb   Alex Williamson   iommu: Add sysfs ...
27
28
  	NULL,
  };
6954cf9bf   Joerg Roedel   iommu/sysfs: Rena...
29
  static void release_device(struct device *dev)
c61959ecb   Alex Williamson   iommu: Add sysfs ...
30
31
32
33
34
35
  {
  	kfree(dev);
  }
  
  static struct class iommu_class = {
  	.name = "iommu",
6954cf9bf   Joerg Roedel   iommu/sysfs: Rena...
36
37
  	.dev_release = release_device,
  	.dev_groups = dev_groups,
c61959ecb   Alex Williamson   iommu: Add sysfs ...
38
39
40
41
42
43
44
45
46
  };
  
  static int __init iommu_dev_init(void)
  {
  	return class_register(&iommu_class);
  }
  postcore_initcall(iommu_dev_init);
  
  /*
39ab9555c   Joerg Roedel   iommu: Add sysfs ...
47
48
49
   * Init the struct device for the IOMMU. IOMMU specific attributes can
   * be provided as an attribute group, allowing a unique namespace per
   * IOMMU type.
c61959ecb   Alex Williamson   iommu: Add sysfs ...
50
   */
39ab9555c   Joerg Roedel   iommu: Add sysfs ...
51
52
53
54
  int iommu_device_sysfs_add(struct iommu_device *iommu,
  			   struct device *parent,
  			   const struct attribute_group **groups,
  			   const char *fmt, ...)
c61959ecb   Alex Williamson   iommu: Add sysfs ...
55
  {
c61959ecb   Alex Williamson   iommu: Add sysfs ...
56
57
  	va_list vargs;
  	int ret;
2926a2aa5   Joerg Roedel   iommu: Fix wrong ...
58
59
60
  	iommu->dev = kzalloc(sizeof(*iommu->dev), GFP_KERNEL);
  	if (!iommu->dev)
  		return -ENOMEM;
c61959ecb   Alex Williamson   iommu: Add sysfs ...
61

2926a2aa5   Joerg Roedel   iommu: Fix wrong ...
62
63
64
65
66
  	device_initialize(iommu->dev);
  
  	iommu->dev->class = &iommu_class;
  	iommu->dev->parent = parent;
  	iommu->dev->groups = groups;
c61959ecb   Alex Williamson   iommu: Add sysfs ...
67
68
  
  	va_start(vargs, fmt);
2926a2aa5   Joerg Roedel   iommu: Fix wrong ...
69
  	ret = kobject_set_name_vargs(&iommu->dev->kobj, fmt, vargs);
c61959ecb   Alex Williamson   iommu: Add sysfs ...
70
71
72
  	va_end(vargs);
  	if (ret)
  		goto error;
2926a2aa5   Joerg Roedel   iommu: Fix wrong ...
73
  	ret = device_add(iommu->dev);
c61959ecb   Alex Williamson   iommu: Add sysfs ...
74
75
  	if (ret)
  		goto error;
2926a2aa5   Joerg Roedel   iommu: Fix wrong ...
76
  	dev_set_drvdata(iommu->dev, iommu);
39ab9555c   Joerg Roedel   iommu: Add sysfs ...
77
  	return 0;
c61959ecb   Alex Williamson   iommu: Add sysfs ...
78
79
  
  error:
2926a2aa5   Joerg Roedel   iommu: Fix wrong ...
80
  	put_device(iommu->dev);
39ab9555c   Joerg Roedel   iommu: Add sysfs ...
81
  	return ret;
c61959ecb   Alex Williamson   iommu: Add sysfs ...
82
  }
d93d63249   Will Deacon   FROMLIST: drivers...
83
  EXPORT_SYMBOL_GPL(iommu_device_sysfs_add);
c61959ecb   Alex Williamson   iommu: Add sysfs ...
84

39ab9555c   Joerg Roedel   iommu: Add sysfs ...
85
  void iommu_device_sysfs_remove(struct iommu_device *iommu)
c61959ecb   Alex Williamson   iommu: Add sysfs ...
86
  {
2926a2aa5   Joerg Roedel   iommu: Fix wrong ...
87
88
89
  	dev_set_drvdata(iommu->dev, NULL);
  	device_unregister(iommu->dev);
  	iommu->dev = NULL;
c61959ecb   Alex Williamson   iommu: Add sysfs ...
90
  }
d93d63249   Will Deacon   FROMLIST: drivers...
91
  EXPORT_SYMBOL_GPL(iommu_device_sysfs_remove);
c61959ecb   Alex Williamson   iommu: Add sysfs ...
92
93
94
95
96
97
  /*
   * IOMMU drivers can indicate a device is managed by a given IOMMU using
   * this interface.  A link to the device will be created in the "devices"
   * directory of the IOMMU device in sysfs and an "iommu" link will be
   * created under the linked device, pointing back at the IOMMU device.
   */
e3d10af11   Joerg Roedel   iommu: Make iommu...
98
  int iommu_device_link(struct iommu_device *iommu, struct device *link)
c61959ecb   Alex Williamson   iommu: Add sysfs ...
99
100
  {
  	int ret;
e3d10af11   Joerg Roedel   iommu: Make iommu...
101
  	if (!iommu || IS_ERR(iommu))
c61959ecb   Alex Williamson   iommu: Add sysfs ...
102
  		return -ENODEV;
2926a2aa5   Joerg Roedel   iommu: Fix wrong ...
103
  	ret = sysfs_add_link_to_group(&iommu->dev->kobj, "devices",
c61959ecb   Alex Williamson   iommu: Add sysfs ...
104
105
106
  				      &link->kobj, dev_name(link));
  	if (ret)
  		return ret;
2926a2aa5   Joerg Roedel   iommu: Fix wrong ...
107
  	ret = sysfs_create_link_nowarn(&link->kobj, &iommu->dev->kobj, "iommu");
c61959ecb   Alex Williamson   iommu: Add sysfs ...
108
  	if (ret)
2926a2aa5   Joerg Roedel   iommu: Fix wrong ...
109
  		sysfs_remove_link_from_group(&iommu->dev->kobj, "devices",
c61959ecb   Alex Williamson   iommu: Add sysfs ...
110
111
112
113
  					     dev_name(link));
  
  	return ret;
  }
d93d63249   Will Deacon   FROMLIST: drivers...
114
  EXPORT_SYMBOL_GPL(iommu_device_link);
c61959ecb   Alex Williamson   iommu: Add sysfs ...
115

e3d10af11   Joerg Roedel   iommu: Make iommu...
116
  void iommu_device_unlink(struct iommu_device *iommu, struct device *link)
c61959ecb   Alex Williamson   iommu: Add sysfs ...
117
  {
e3d10af11   Joerg Roedel   iommu: Make iommu...
118
  	if (!iommu || IS_ERR(iommu))
c61959ecb   Alex Williamson   iommu: Add sysfs ...
119
120
121
  		return;
  
  	sysfs_remove_link(&link->kobj, "iommu");
2926a2aa5   Joerg Roedel   iommu: Fix wrong ...
122
  	sysfs_remove_link_from_group(&iommu->dev->kobj, "devices", dev_name(link));
c61959ecb   Alex Williamson   iommu: Add sysfs ...
123
  }
d93d63249   Will Deacon   FROMLIST: drivers...
124
  EXPORT_SYMBOL_GPL(iommu_device_unlink);