Skip to content

Commit 39ab955

Browse files
committed
iommu: Add sysfs bindings for struct iommu_device
There is currently support for iommu sysfs bindings, but those need to be implemented in the IOMMU drivers. Add a more generic version of this by adding a struct device to struct iommu_device and use that for the sysfs bindings. Also convert the AMD and Intel IOMMU driver to make use of it. Signed-off-by: Joerg Roedel <jroedel@suse.de>
1 parent b0119e8 commit 39ab955

File tree

8 files changed

+61
-69
lines changed

8 files changed

+61
-69
lines changed

drivers/iommu/amd_iommu.c

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -445,6 +445,7 @@ static void init_iommu_group(struct device *dev)
445445
static int iommu_init_device(struct device *dev)
446446
{
447447
struct iommu_dev_data *dev_data;
448+
struct amd_iommu *iommu;
448449
int devid;
449450

450451
if (dev->archdata.iommu)
@@ -454,6 +455,8 @@ static int iommu_init_device(struct device *dev)
454455
if (devid < 0)
455456
return devid;
456457

458+
iommu = amd_iommu_rlookup_table[devid];
459+
457460
dev_data = find_dev_data(devid);
458461
if (!dev_data)
459462
return -ENOMEM;
@@ -469,8 +472,7 @@ static int iommu_init_device(struct device *dev)
469472

470473
dev->archdata.iommu = dev_data;
471474

472-
iommu_device_link(amd_iommu_rlookup_table[dev_data->devid]->iommu_dev,
473-
dev);
475+
iommu_device_link(&iommu->iommu.dev, dev);
474476

475477
return 0;
476478
}
@@ -495,22 +497,24 @@ static void iommu_ignore_device(struct device *dev)
495497

496498
static void iommu_uninit_device(struct device *dev)
497499
{
498-
int devid;
499500
struct iommu_dev_data *dev_data;
501+
struct amd_iommu *iommu;
502+
int devid;
500503

501504
devid = get_device_id(dev);
502505
if (devid < 0)
503506
return;
504507

508+
iommu = amd_iommu_rlookup_table[devid];
509+
505510
dev_data = search_dev_data(devid);
506511
if (!dev_data)
507512
return;
508513

509514
if (dev_data->domain)
510515
detach_device(dev);
511516

512-
iommu_device_unlink(amd_iommu_rlookup_table[dev_data->devid]->iommu_dev,
513-
dev);
517+
iommu_device_unlink(&iommu->iommu.dev, dev);
514518

515519
iommu_group_remove_device(dev);
516520

drivers/iommu/amd_iommu_init.c

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1637,10 +1637,8 @@ static int iommu_init_pci(struct amd_iommu *iommu)
16371637
amd_iommu_erratum_746_workaround(iommu);
16381638
amd_iommu_ats_write_check_workaround(iommu);
16391639

1640-
iommu->iommu_dev = iommu_device_create(&iommu->dev->dev, iommu,
1641-
amd_iommu_groups, "ivhd%d",
1642-
iommu->index);
1643-
1640+
iommu_device_sysfs_add(&iommu->iommu, &iommu->dev->dev,
1641+
amd_iommu_groups, "ivhd%d", iommu->index);
16441642
iommu_device_set_ops(&iommu->iommu, &amd_iommu_ops);
16451643
iommu_device_register(&iommu->iommu);
16461644

drivers/iommu/amd_iommu_types.h

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -535,9 +535,6 @@ struct amd_iommu {
535535
/* if one, we need to send a completion wait command */
536536
bool need_sync;
537537

538-
/* IOMMU sysfs device */
539-
struct device *iommu_dev;
540-
541538
/* Handle for IOMMU core code */
542539
struct iommu_device iommu;
543540

drivers/iommu/dmar.c

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1078,14 +1078,11 @@ static int alloc_iommu(struct dmar_drhd_unit *drhd)
10781078
raw_spin_lock_init(&iommu->register_lock);
10791079

10801080
if (intel_iommu_enabled) {
1081-
iommu->iommu_dev = iommu_device_create(NULL, iommu,
1082-
intel_iommu_groups,
1083-
"%s", iommu->name);
1084-
1085-
if (IS_ERR(iommu->iommu_dev)) {
1086-
err = PTR_ERR(iommu->iommu_dev);
1081+
err = iommu_device_sysfs_add(&iommu->iommu, NULL,
1082+
intel_iommu_groups,
1083+
"%s", iommu->name);
1084+
if (err)
10871085
goto err_unmap;
1088-
}
10891086

10901087
iommu_device_set_ops(&iommu->iommu, &intel_iommu_ops);
10911088

@@ -1109,7 +1106,7 @@ static int alloc_iommu(struct dmar_drhd_unit *drhd)
11091106

11101107
static void free_iommu(struct intel_iommu *iommu)
11111108
{
1112-
iommu_device_destroy(iommu->iommu_dev);
1109+
iommu_device_sysfs_remove(&iommu->iommu);
11131110
iommu_device_unregister(&iommu->iommu);
11141111

11151112
if (iommu->irq) {

drivers/iommu/intel-iommu.c

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4834,10 +4834,13 @@ int __init intel_iommu_init(void)
48344834

48354835
init_iommu_pm_ops();
48364836

4837-
for_each_active_iommu(iommu, drhd)
4838-
iommu->iommu_dev = iommu_device_create(NULL, iommu,
4839-
intel_iommu_groups,
4840-
"%s", iommu->name);
4837+
for_each_active_iommu(iommu, drhd) {
4838+
iommu_device_sysfs_add(&iommu->iommu, NULL,
4839+
intel_iommu_groups,
4840+
"%s", iommu->name);
4841+
iommu_device_set_ops(&iommu->iommu, &intel_iommu_ops);
4842+
iommu_device_register(&iommu->iommu);
4843+
}
48414844

48424845
bus_set_iommu(&pci_bus_type, &intel_iommu_ops);
48434846
bus_register_notifier(&pci_bus_type, &device_nb);
@@ -5159,7 +5162,7 @@ static int intel_iommu_add_device(struct device *dev)
51595162
if (!iommu)
51605163
return -ENODEV;
51615164

5162-
iommu_device_link(iommu->iommu_dev, dev);
5165+
iommu_device_link(&iommu->iommu.dev, dev);
51635166

51645167
group = iommu_group_get_for_dev(dev);
51655168

@@ -5181,7 +5184,7 @@ static void intel_iommu_remove_device(struct device *dev)
51815184

51825185
iommu_group_remove_device(dev);
51835186

5184-
iommu_device_unlink(iommu->iommu_dev, dev);
5187+
iommu_device_unlink(&iommu->iommu.dev, dev);
51855188
}
51865189

51875190
#ifdef CONFIG_INTEL_IOMMU_SVM

drivers/iommu/iommu-sysfs.c

Lines changed: 18 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -50,54 +50,45 @@ static int __init iommu_dev_init(void)
5050
postcore_initcall(iommu_dev_init);
5151

5252
/*
53-
* Create an IOMMU device and return a pointer to it. IOMMU specific
54-
* attributes can be provided as an attribute group, allowing a unique
55-
* namespace per IOMMU type.
53+
* Init the struct device for the IOMMU. IOMMU specific attributes can
54+
* be provided as an attribute group, allowing a unique namespace per
55+
* IOMMU type.
5656
*/
57-
struct device *iommu_device_create(struct device *parent, void *drvdata,
58-
const struct attribute_group **groups,
59-
const char *fmt, ...)
57+
int iommu_device_sysfs_add(struct iommu_device *iommu,
58+
struct device *parent,
59+
const struct attribute_group **groups,
60+
const char *fmt, ...)
6061
{
61-
struct device *dev;
6262
va_list vargs;
6363
int ret;
6464

65-
dev = kzalloc(sizeof(*dev), GFP_KERNEL);
66-
if (!dev)
67-
return ERR_PTR(-ENOMEM);
65+
device_initialize(&iommu->dev);
6866

69-
device_initialize(dev);
70-
71-
dev->class = &iommu_class;
72-
dev->parent = parent;
73-
dev->groups = groups;
74-
dev_set_drvdata(dev, drvdata);
67+
iommu->dev.class = &iommu_class;
68+
iommu->dev.parent = parent;
69+
iommu->dev.groups = groups;
7570

7671
va_start(vargs, fmt);
77-
ret = kobject_set_name_vargs(&dev->kobj, fmt, vargs);
72+
ret = kobject_set_name_vargs(&iommu->dev.kobj, fmt, vargs);
7873
va_end(vargs);
7974
if (ret)
8075
goto error;
8176

82-
ret = device_add(dev);
77+
ret = device_add(&iommu->dev);
8378
if (ret)
8479
goto error;
8580

86-
return dev;
81+
return 0;
8782

8883
error:
89-
put_device(dev);
90-
return ERR_PTR(ret);
84+
put_device(&iommu->dev);
85+
return ret;
9186
}
9287

93-
void iommu_device_destroy(struct device *dev)
88+
void iommu_device_sysfs_remove(struct iommu_device *iommu)
9489
{
95-
if (!dev || IS_ERR(dev))
96-
return;
97-
98-
device_unregister(dev);
90+
device_unregister(&iommu->dev);
9991
}
100-
10192
/*
10293
* IOMMU drivers can indicate a device is managed by a given IOMMU using
10394
* this interface. A link to the device will be created in the "devices"

include/linux/intel-iommu.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -440,7 +440,6 @@ struct intel_iommu {
440440
struct irq_domain *ir_domain;
441441
struct irq_domain *ir_msi_domain;
442442
#endif
443-
struct device *iommu_dev; /* IOMMU-sysfs device */
444443
struct iommu_device iommu; /* IOMMU core code handle */
445444
int node;
446445
u32 flags; /* Software defined flags */

include/linux/iommu.h

Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -209,14 +209,21 @@ struct iommu_ops {
209209
* instance
210210
* @list: Used by the iommu-core to keep a list of registered iommus
211211
* @ops: iommu-ops for talking to this iommu
212+
* @dev: struct device for sysfs handling
212213
*/
213214
struct iommu_device {
214215
struct list_head list;
215216
const struct iommu_ops *ops;
217+
struct device dev;
216218
};
217219

218220
int iommu_device_register(struct iommu_device *iommu);
219221
void iommu_device_unregister(struct iommu_device *iommu);
222+
int iommu_device_sysfs_add(struct iommu_device *iommu,
223+
struct device *parent,
224+
const struct attribute_group **groups,
225+
const char *fmt, ...) __printf(4, 5);
226+
void iommu_device_sysfs_remove(struct iommu_device *iommu);
220227

221228
static inline void iommu_device_set_ops(struct iommu_device *iommu,
222229
const struct iommu_ops *ops)
@@ -287,10 +294,6 @@ extern int iommu_domain_get_attr(struct iommu_domain *domain, enum iommu_attr,
287294
void *data);
288295
extern int iommu_domain_set_attr(struct iommu_domain *domain, enum iommu_attr,
289296
void *data);
290-
struct device *iommu_device_create(struct device *parent, void *drvdata,
291-
const struct attribute_group **groups,
292-
const char *fmt, ...) __printf(4, 5);
293-
void iommu_device_destroy(struct device *dev);
294297
int iommu_device_link(struct device *dev, struct device *link);
295298
void iommu_device_unlink(struct device *dev, struct device *link);
296299

@@ -567,29 +570,29 @@ static inline int iommu_domain_set_attr(struct iommu_domain *domain,
567570
return -EINVAL;
568571
}
569572

570-
static inline struct device *iommu_device_create(struct device *parent,
571-
void *drvdata,
572-
const struct attribute_group **groups,
573-
const char *fmt, ...)
573+
static inline int iommu_device_register(struct iommu_device *iommu)
574574
{
575-
return ERR_PTR(-ENODEV);
575+
return -ENODEV;
576576
}
577577

578-
static inline void iommu_device_destroy(struct device *dev)
578+
static inline void iommu_device_set_ops(struct iommu_device *iommu,
579+
const struct iommu_ops *ops)
579580
{
580581
}
581582

582-
static inline int iommu_device_register(struct iommu_device *iommu)
583+
static inline void iommu_device_unregister(struct iommu_device *iommu)
583584
{
584-
return -ENODEV;
585585
}
586586

587-
static inline void iommu_device_set_ops(struct iommu_device *iommu,
588-
const struct iommu_ops *ops)
587+
static inline int iommu_device_sysfs_add(struct iommu_device *iommu,
588+
struct device *parent,
589+
const struct attribute_group **groups,
590+
const char *fmt, ...)
589591
{
592+
return -ENODEV;
590593
}
591594

592-
static inline void iommu_device_unregister(struct iommu_device *iommu)
595+
static inline void iommu_device_sysfs_remove(struct iommu_device *iommu)
593596
{
594597
}
595598

0 commit comments

Comments
 (0)