Skip to content

Commit c13dbc1

Browse files
rmurphy-armjoergroedel
authored andcommitted
iommu: Always register bus notifiers
The number of bus types that the IOMMU subsystem deals with is small and manageable, so pull that list into core code as a first step towards cleaning up all the boilerplate bus-awareness from drivers. Calling iommu_probe_device() before bus->iommu_ops is set will simply return -ENODEV and not break the notifier call chain, so there should be no harm in proactively registering all our bus notifiers at init time. Tested-by: Marek Szyprowski <[email protected]> Tested-by: Matthew Rosato <[email protected]> # s390 Tested-by: Niklas Schnelle <[email protected]> # s390 Signed-off-by: Robin Murphy <[email protected]> Reviewed-by: Kevin Tian <[email protected]> Link: https://lore.kernel.org/r/7462347bf938bd6eedb629a3a318434f6516e712.1660572783.git.robin.murphy@arm.com Signed-off-by: Joerg Roedel <[email protected]>
1 parent 927a5fd commit c13dbc1

File tree

1 file changed

+37
-35
lines changed

1 file changed

+37
-35
lines changed

drivers/iommu/iommu.c

Lines changed: 37 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
#define pr_fmt(fmt) "iommu: " fmt
88

9+
#include <linux/amba/bus.h>
910
#include <linux/device.h>
1011
#include <linux/dma-iommu.h>
1112
#include <linux/kernel.h>
@@ -16,11 +17,13 @@
1617
#include <linux/export.h>
1718
#include <linux/slab.h>
1819
#include <linux/errno.h>
20+
#include <linux/host1x_context_bus.h>
1921
#include <linux/iommu.h>
2022
#include <linux/idr.h>
2123
#include <linux/err.h>
2224
#include <linux/pci.h>
2325
#include <linux/bitops.h>
26+
#include <linux/platform_device.h>
2427
#include <linux/property.h>
2528
#include <linux/fsl/mc.h>
2629
#include <linux/module.h>
@@ -75,6 +78,8 @@ static const char * const iommu_group_resv_type_string[] = {
7578
#define IOMMU_CMD_LINE_DMA_API BIT(0)
7679
#define IOMMU_CMD_LINE_STRICT BIT(1)
7780

81+
static int iommu_bus_notifier(struct notifier_block *nb,
82+
unsigned long action, void *data);
7883
static int iommu_alloc_default_domain(struct iommu_group *group,
7984
struct device *dev);
8085
static struct iommu_domain *__iommu_domain_alloc(struct bus_type *bus,
@@ -103,6 +108,22 @@ struct iommu_group_attribute iommu_group_attr_##_name = \
103108
static LIST_HEAD(iommu_device_list);
104109
static DEFINE_SPINLOCK(iommu_device_lock);
105110

111+
static struct bus_type * const iommu_buses[] = {
112+
&platform_bus_type,
113+
#ifdef CONFIG_PCI
114+
&pci_bus_type,
115+
#endif
116+
#ifdef CONFIG_ARM_AMBA
117+
&amba_bustype,
118+
#endif
119+
#ifdef CONFIG_FSL_MC_BUS
120+
&fsl_mc_bus_type,
121+
#endif
122+
#ifdef CONFIG_TEGRA_HOST1X_CONTEXT_BUS
123+
&host1x_context_device_bus_type,
124+
#endif
125+
};
126+
106127
/*
107128
* Use a function instead of an array here because the domain-type is a
108129
* bit-field, so an array would waste memory.
@@ -126,6 +147,8 @@ static const char *iommu_domain_type_str(unsigned int t)
126147

127148
static int __init iommu_subsys_init(void)
128149
{
150+
struct notifier_block *nb;
151+
129152
if (!(iommu_cmd_line & IOMMU_CMD_LINE_DMA_API)) {
130153
if (IS_ENABLED(CONFIG_IOMMU_DEFAULT_PASSTHROUGH))
131154
iommu_set_default_passthrough(false);
@@ -152,6 +175,15 @@ static int __init iommu_subsys_init(void)
152175
(iommu_cmd_line & IOMMU_CMD_LINE_STRICT) ?
153176
"(set via kernel command line)" : "");
154177

178+
nb = kcalloc(ARRAY_SIZE(iommu_buses), sizeof(*nb), GFP_KERNEL);
179+
if (!nb)
180+
return -ENOMEM;
181+
182+
for (int i = 0; i < ARRAY_SIZE(iommu_buses); i++) {
183+
nb[i].notifier_call = iommu_bus_notifier;
184+
bus_register_notifier(iommu_buses[i], &nb[i]);
185+
}
186+
155187
return 0;
156188
}
157189
subsys_initcall(iommu_subsys_init);
@@ -1774,39 +1806,6 @@ int bus_iommu_probe(struct bus_type *bus)
17741806
return ret;
17751807
}
17761808

1777-
static int iommu_bus_init(struct bus_type *bus, const struct iommu_ops *ops)
1778-
{
1779-
struct notifier_block *nb;
1780-
int err;
1781-
1782-
nb = kzalloc(sizeof(struct notifier_block), GFP_KERNEL);
1783-
if (!nb)
1784-
return -ENOMEM;
1785-
1786-
nb->notifier_call = iommu_bus_notifier;
1787-
1788-
err = bus_register_notifier(bus, nb);
1789-
if (err)
1790-
goto out_free;
1791-
1792-
err = bus_iommu_probe(bus);
1793-
if (err)
1794-
goto out_err;
1795-
1796-
1797-
return 0;
1798-
1799-
out_err:
1800-
/* Clean up */
1801-
bus_for_each_dev(bus, NULL, NULL, remove_iommu_group);
1802-
bus_unregister_notifier(bus, nb);
1803-
1804-
out_free:
1805-
kfree(nb);
1806-
1807-
return err;
1808-
}
1809-
18101809
/**
18111810
* bus_set_iommu - set iommu-callbacks for the bus
18121811
* @bus: bus.
@@ -1835,9 +1834,12 @@ int bus_set_iommu(struct bus_type *bus, const struct iommu_ops *ops)
18351834
bus->iommu_ops = ops;
18361835

18371836
/* Do IOMMU specific setup for this bus-type */
1838-
err = iommu_bus_init(bus, ops);
1839-
if (err)
1837+
err = bus_iommu_probe(bus);
1838+
if (err) {
1839+
/* Clean up */
1840+
bus_for_each_dev(bus, NULL, NULL, remove_iommu_group);
18401841
bus->iommu_ops = NULL;
1842+
}
18411843

18421844
return err;
18431845
}

0 commit comments

Comments
 (0)