1 From 05375244c8e74f90239db92646a771905fdfc0db Mon Sep 17 00:00:00 2001
2 From: Biwen Li <biwen.li@nxp.com>
3 Date: Tue, 30 Oct 2018 18:28:22 +0800
4 Subject: [PATCH 38/40] smmu: support layerscape
5 This is an integrated patch of smmu for layerscape
7 Signed-off-by: Nipun Gupta <nipun.gupta@nxp.com>
8 Signed-off-by: Stuart Yoder <stuart.yoder@nxp.com>
9 Signed-off-by: Zhao Qiang <qiang.zhao@nxp.com>
10 Signed-off-by: Biwen Li <biwen.li@nxp.com>
12 .../devicetree/bindings/misc/fsl,qoriq-mc.txt | 39 ++++++
13 drivers/iommu/arm-smmu.c | 7 +
14 drivers/iommu/iommu.c | 21 +++
15 drivers/iommu/of_iommu.c | 126 +++++++++++++++++-
16 drivers/of/irq.c | 6 +-
17 drivers/of/of_pci.c | 101 --------------
18 include/linux/iommu.h | 2 +
19 include/linux/of_iommu.h | 10 ++
20 include/linux/of_pci.h | 10 --
21 9 files changed, 205 insertions(+), 117 deletions(-)
23 --- a/Documentation/devicetree/bindings/misc/fsl,qoriq-mc.txt
24 +++ b/Documentation/devicetree/bindings/misc/fsl,qoriq-mc.txt
25 @@ -9,6 +9,25 @@ blocks that can be used to create functi
26 such as network interfaces, crypto accelerator instances, L2 switches,
29 +For an overview of the DPAA2 architecture and fsl-mc bus see:
30 +drivers/staging/fsl-mc/README.txt
32 +As described in the above overview, all DPAA2 objects in a DPRC share the
33 +same hardware "isolation context" and a 10-bit value called an ICID
34 +(isolation context id) is expressed by the hardware to identify
37 +The generic 'iommus' property is insufficient to describe the relationship
38 +between ICIDs and IOMMUs, so an iommu-map property is used to define
39 +the set of possible ICIDs under a root DPRC and how they map to
42 +For generic IOMMU bindings, see
43 +Documentation/devicetree/bindings/iommu/iommu.txt.
45 +For arm-smmu binding, see:
46 +Documentation/devicetree/bindings/iommu/arm,smmu.txt.
51 @@ -88,14 +107,34 @@ Sub-nodes:
53 Definition: Specifies the phandle to the PHY device node associated
57 +- iommu-map: Maps an ICID to an IOMMU and associated iommu-specifier
60 + The property is an arbitrary number of tuples of
61 + (icid-base,iommu,iommu-base,length).
63 + Any ICID i in the interval [icid-base, icid-base + length) is
64 + associated with the listed IOMMU, with the iommu-specifier
65 + (i - icid-base + iommu-base).
69 + smmu: iommu@5000000 {
70 + compatible = "arm,mmu-500";
72 + stream-match-mask = <0x7C00>;
76 fsl_mc: fsl-mc@80c000000 {
77 compatible = "fsl,qoriq-mc";
78 reg = <0x00000008 0x0c000000 0 0x40>, /* MC portal base */
79 <0x00000000 0x08340000 0 0x40000>; /* MC control reg */
81 + /* define map for ICIDs 23-64 */
82 + iommu-map = <23 &smmu 23 41>;
86 --- a/drivers/iommu/arm-smmu.c
87 +++ b/drivers/iommu/arm-smmu.c
89 #include <linux/spinlock.h>
91 #include <linux/amba/bus.h>
92 +#include <linux/fsl/mc.h>
94 #include "io-pgtable.h"
95 #include "arm-smmu-regs.h"
96 @@ -1474,6 +1475,8 @@ static struct iommu_group *arm_smmu_devi
99 group = pci_device_group(dev);
100 + else if (dev_is_fsl_mc(dev))
101 + group = fsl_mc_device_group(dev);
103 group = generic_device_group(dev);
105 @@ -2052,6 +2055,10 @@ static void arm_smmu_bus_init(void)
106 bus_set_iommu(&pci_bus_type, &arm_smmu_ops);
109 +#ifdef CONFIG_FSL_MC_BUS
110 + if (!iommu_present(&fsl_mc_bus_type))
111 + bus_set_iommu(&fsl_mc_bus_type, &arm_smmu_ops);
115 static int arm_smmu_device_probe(struct platform_device *pdev)
116 --- a/drivers/iommu/iommu.c
117 +++ b/drivers/iommu/iommu.c
119 #include <linux/bitops.h>
120 #include <linux/property.h>
121 #include <trace/events/iommu.h>
122 +#include <linux/fsl/mc.h>
124 static struct kset *iommu_group_kset;
125 static DEFINE_IDA(iommu_group_ida);
126 @@ -987,6 +988,26 @@ struct iommu_group *pci_device_group(str
127 return iommu_group_alloc();
130 +/* Get the IOMMU group for device on fsl-mc bus */
131 +struct iommu_group *fsl_mc_device_group(struct device *dev)
133 + struct device *cont_dev = fsl_mc_cont_dev(dev);
134 + struct iommu_group *group;
136 + /* Container device is responsible for creating the iommu group */
137 + if (fsl_mc_is_cont_dev(dev)) {
138 + group = iommu_group_alloc();
142 + get_device(cont_dev);
143 + group = iommu_group_get(cont_dev);
144 + put_device(cont_dev);
151 * iommu_group_get_for_dev - Find or create the IOMMU group for a device
152 * @dev: target device
153 --- a/drivers/iommu/of_iommu.c
154 +++ b/drivers/iommu/of_iommu.c
156 #include <linux/of_iommu.h>
157 #include <linux/of_pci.h>
158 #include <linux/slab.h>
159 +#include <linux/fsl/mc.h>
163 @@ -143,15 +144,115 @@ struct of_pci_iommu_alias_info {
164 struct device_node *np;
168 + * of_map_rid - Translate a requester ID through a downstream mapping.
169 + * @np: root complex device node.
170 + * @rid: Requester ID to map.
171 + * @map_name: property name of the map to use.
172 + * @map_mask_name: optional property name of the mask to use.
173 + * @target: optional pointer to a target device node.
174 + * @id_out: optional pointer to receive the translated ID.
176 + * Given PCI/MC requester ID, look up the appropriate implementation-defined
177 + * platform ID and/or the target device which receives transactions on that
178 + * ID, as per the "iommu-map" and "msi-map" bindings. Either of @target or
179 + * @id_out may be NULL if only the other is required. If @target points to
180 + * a non-NULL device node pointer, only entries targeting that node will be
181 + * matched; if it points to a NULL value, it will receive the device node of
182 + * the first matching target phandle, with a reference held.
184 + * Return: 0 on success or a standard error code on failure.
186 +int of_map_rid(struct device_node *np, u32 rid,
187 + const char *map_name, const char *map_mask_name,
188 + struct device_node **target, u32 *id_out)
190 + u32 map_mask, masked_rid;
192 + const __be32 *map = NULL;
194 + if (!np || !map_name || (!target && !id_out))
197 + map = of_get_property(np, map_name, &map_len);
201 + /* Otherwise, no map implies no translation */
206 + if (!map_len || map_len % (4 * sizeof(*map))) {
207 + pr_err("%pOF: Error: Bad %s length: %d\n", np,
208 + map_name, map_len);
212 + /* The default is to select all bits. */
213 + map_mask = 0xffffffff;
216 + * Can be overridden by "{iommu,msi}-map-mask" property.
217 + * If of_property_read_u32() fails, the default is used.
220 + of_property_read_u32(np, map_mask_name, &map_mask);
222 + masked_rid = map_mask & rid;
223 + for ( ; map_len > 0; map_len -= 4 * sizeof(*map), map += 4) {
224 + struct device_node *phandle_node;
225 + u32 rid_base = be32_to_cpup(map + 0);
226 + u32 phandle = be32_to_cpup(map + 1);
227 + u32 out_base = be32_to_cpup(map + 2);
228 + u32 rid_len = be32_to_cpup(map + 3);
230 + if (rid_base & ~map_mask) {
231 + pr_err("%pOF: Invalid %s translation - %s-mask (0x%x) ignores rid-base (0x%x)\n",
232 + np, map_name, map_name,
233 + map_mask, rid_base);
237 + if (masked_rid < rid_base || masked_rid >= rid_base + rid_len)
240 + phandle_node = of_find_node_by_phandle(phandle);
246 + of_node_put(phandle_node);
248 + *target = phandle_node;
250 + if (*target != phandle_node)
255 + *id_out = masked_rid - rid_base + out_base;
257 + pr_debug("%pOF: %s, using mask %08x, rid-base: %08x, out-base: %08x, length: %08x, rid: %08x -> %08x\n",
258 + np, map_name, map_mask, rid_base, out_base,
259 + rid_len, rid, *id_out);
263 + pr_err("%pOF: Invalid %s translation - no match for rid 0x%x on %pOF\n",
264 + np, map_name, rid, target && *target ? *target : NULL);
267 static int of_pci_iommu_init(struct pci_dev *pdev, u16 alias, void *data)
269 struct of_pci_iommu_alias_info *info = data;
270 struct of_phandle_args iommu_spec = { .args_count = 1 };
273 - err = of_pci_map_rid(info->np, alias, "iommu-map",
274 - "iommu-map-mask", &iommu_spec.np,
276 + err = of_map_rid(info->np, alias, "iommu-map",
277 + "iommu-map-mask", &iommu_spec.np,
280 return err == -ENODEV ? NO_IOMMU : err;
282 @@ -160,6 +261,23 @@ static int of_pci_iommu_init(struct pci_
286 +static int of_fsl_mc_iommu_init(struct fsl_mc_device *mc_dev,
287 + struct device_node *master_np)
289 + struct of_phandle_args iommu_spec = { .args_count = 1 };
292 + err = of_map_rid(master_np, mc_dev->icid, "iommu-map",
293 + "iommu-map-mask", &iommu_spec.np,
296 + return err == -ENODEV ? NO_IOMMU : err;
298 + err = of_iommu_xlate(&mc_dev->dev, &iommu_spec);
299 + of_node_put(iommu_spec.np);
303 const struct iommu_ops *of_iommu_configure(struct device *dev,
304 struct device_node *master_np)
306 @@ -191,6 +309,8 @@ const struct iommu_ops *of_iommu_configu
308 err = pci_for_each_dma_alias(to_pci_dev(dev),
309 of_pci_iommu_init, &info);
310 + } else if (dev_is_fsl_mc(dev)) {
311 + err = of_fsl_mc_iommu_init(to_fsl_mc_device(dev), master_np);
313 struct of_phandle_args iommu_spec;
315 --- a/drivers/of/irq.c
316 +++ b/drivers/of/irq.c
318 #include <linux/module.h>
319 #include <linux/of.h>
320 #include <linux/of_irq.h>
321 -#include <linux/of_pci.h>
322 +#include <linux/of_iommu.h>
323 #include <linux/string.h>
324 #include <linux/slab.h>
326 @@ -593,8 +593,8 @@ static u32 __of_msi_map_rid(struct devic
327 * "msi-map" property.
329 for (parent_dev = dev; parent_dev; parent_dev = parent_dev->parent)
330 - if (!of_pci_map_rid(parent_dev->of_node, rid_in, "msi-map",
331 - "msi-map-mask", np, &rid_out))
332 + if (!of_map_rid(parent_dev->of_node, rid_in, "msi-map",
333 + "msi-map-mask", np, &rid_out))
337 --- a/drivers/of/of_pci.c
338 +++ b/drivers/of/of_pci.c
339 @@ -281,104 +281,3 @@ parse_failed:
341 EXPORT_SYMBOL_GPL(of_pci_get_host_bridge_resources);
342 #endif /* CONFIG_OF_ADDRESS */
345 - * of_pci_map_rid - Translate a requester ID through a downstream mapping.
346 - * @np: root complex device node.
347 - * @rid: PCI requester ID to map.
348 - * @map_name: property name of the map to use.
349 - * @map_mask_name: optional property name of the mask to use.
350 - * @target: optional pointer to a target device node.
351 - * @id_out: optional pointer to receive the translated ID.
353 - * Given a PCI requester ID, look up the appropriate implementation-defined
354 - * platform ID and/or the target device which receives transactions on that
355 - * ID, as per the "iommu-map" and "msi-map" bindings. Either of @target or
356 - * @id_out may be NULL if only the other is required. If @target points to
357 - * a non-NULL device node pointer, only entries targeting that node will be
358 - * matched; if it points to a NULL value, it will receive the device node of
359 - * the first matching target phandle, with a reference held.
361 - * Return: 0 on success or a standard error code on failure.
363 -int of_pci_map_rid(struct device_node *np, u32 rid,
364 - const char *map_name, const char *map_mask_name,
365 - struct device_node **target, u32 *id_out)
367 - u32 map_mask, masked_rid;
369 - const __be32 *map = NULL;
371 - if (!np || !map_name || (!target && !id_out))
374 - map = of_get_property(np, map_name, &map_len);
378 - /* Otherwise, no map implies no translation */
383 - if (!map_len || map_len % (4 * sizeof(*map))) {
384 - pr_err("%pOF: Error: Bad %s length: %d\n", np,
385 - map_name, map_len);
389 - /* The default is to select all bits. */
390 - map_mask = 0xffffffff;
393 - * Can be overridden by "{iommu,msi}-map-mask" property.
394 - * If of_property_read_u32() fails, the default is used.
397 - of_property_read_u32(np, map_mask_name, &map_mask);
399 - masked_rid = map_mask & rid;
400 - for ( ; map_len > 0; map_len -= 4 * sizeof(*map), map += 4) {
401 - struct device_node *phandle_node;
402 - u32 rid_base = be32_to_cpup(map + 0);
403 - u32 phandle = be32_to_cpup(map + 1);
404 - u32 out_base = be32_to_cpup(map + 2);
405 - u32 rid_len = be32_to_cpup(map + 3);
407 - if (rid_base & ~map_mask) {
408 - pr_err("%pOF: Invalid %s translation - %s-mask (0x%x) ignores rid-base (0x%x)\n",
409 - np, map_name, map_name,
410 - map_mask, rid_base);
414 - if (masked_rid < rid_base || masked_rid >= rid_base + rid_len)
417 - phandle_node = of_find_node_by_phandle(phandle);
423 - of_node_put(phandle_node);
425 - *target = phandle_node;
427 - if (*target != phandle_node)
432 - *id_out = masked_rid - rid_base + out_base;
434 - pr_debug("%pOF: %s, using mask %08x, rid-base: %08x, out-base: %08x, length: %08x, rid: %08x -> %08x\n",
435 - np, map_name, map_mask, rid_base, out_base,
436 - rid_len, rid, *id_out);
440 - pr_err("%pOF: Invalid %s translation - no match for rid 0x%x on %pOF\n",
441 - np, map_name, rid, target && *target ? *target : NULL);
444 --- a/include/linux/iommu.h
445 +++ b/include/linux/iommu.h
446 @@ -389,6 +389,8 @@ static inline size_t iommu_map_sg(struct
447 extern struct iommu_group *pci_device_group(struct device *dev);
448 /* Generic device grouping function */
449 extern struct iommu_group *generic_device_group(struct device *dev);
450 +/* FSL-MC device grouping function */
451 +struct iommu_group *fsl_mc_device_group(struct device *dev);
454 * struct iommu_fwspec - per-device IOMMU instance data
455 --- a/include/linux/of_iommu.h
456 +++ b/include/linux/of_iommu.h
457 @@ -15,6 +15,9 @@ extern int of_get_dma_window(struct devi
458 extern const struct iommu_ops *of_iommu_configure(struct device *dev,
459 struct device_node *master_np);
461 +int of_map_rid(struct device_node *np, u32 rid,
462 + const char *map_name, const char *map_mask_name,
463 + struct device_node **target, u32 *id_out);
466 static inline int of_get_dma_window(struct device_node *dn, const char *prefix,
467 @@ -30,6 +33,13 @@ static inline const struct iommu_ops *of
471 +static inline int of_map_rid(struct device_node *np, u32 rid,
472 + const char *map_name, const char *map_mask_name,
473 + struct device_node **target, u32 *id_out)
478 #endif /* CONFIG_OF_IOMMU */
480 extern struct of_device_id __iommu_of_table;
481 --- a/include/linux/of_pci.h
482 +++ b/include/linux/of_pci.h
483 @@ -19,9 +19,6 @@ int of_pci_parse_bus_range(struct device
484 int of_get_pci_domain_nr(struct device_node *node);
485 int of_pci_get_max_link_speed(struct device_node *node);
486 void of_pci_check_probe_only(void);
487 -int of_pci_map_rid(struct device_node *np, u32 rid,
488 - const char *map_name, const char *map_mask_name,
489 - struct device_node **target, u32 *id_out);
491 static inline int of_irq_parse_pci(const struct pci_dev *pdev, struct of_phandle_args *out_irq)
493 @@ -57,13 +54,6 @@ of_get_pci_domain_nr(struct device_node
497 -static inline int of_pci_map_rid(struct device_node *np, u32 rid,
498 - const char *map_name, const char *map_mask_name,
499 - struct device_node **target, u32 *id_out)
505 of_pci_get_max_link_speed(struct device_node *node)