kernel: bump 4.14 to 4.14.166
[openwrt/openwrt.git] / target / linux / layerscape / patches-4.14 / 821-smmu-support-layerscape.patch
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
6
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>
11 ---
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(-)
22
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,
27 etc.
28
29 +For an overview of the DPAA2 architecture and fsl-mc bus see:
30 +drivers/staging/fsl-mc/README.txt
31 +
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
35 +the requester.
36 +
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
40 +an IOMMU.
41 +
42 +For generic IOMMU bindings, see
43 +Documentation/devicetree/bindings/iommu/iommu.txt.
44 +
45 +For arm-smmu binding, see:
46 +Documentation/devicetree/bindings/iommu/arm,smmu.txt.
47 +
48 Required properties:
49
50 - compatible
51 @@ -88,14 +107,34 @@ Sub-nodes:
52 Value type: <phandle>
53 Definition: Specifies the phandle to the PHY device node associated
54 with the this dpmac.
55 +Optional properties:
56 +
57 +- iommu-map: Maps an ICID to an IOMMU and associated iommu-specifier
58 + data.
59 +
60 + The property is an arbitrary number of tuples of
61 + (icid-base,iommu,iommu-base,length).
62 +
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).
66
67 Example:
68
69 + smmu: iommu@5000000 {
70 + compatible = "arm,mmu-500";
71 + #iommu-cells = <2>;
72 + stream-match-mask = <0x7C00>;
73 + ...
74 + };
75 +
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 */
80 msi-parent = <&its>;
81 + /* define map for ICIDs 23-64 */
82 + iommu-map = <23 &smmu 23 41>;
83 #address-cells = <3>;
84 #size-cells = <1>;
85
86 --- a/drivers/iommu/arm-smmu.c
87 +++ b/drivers/iommu/arm-smmu.c
88 @@ -52,6 +52,7 @@
89 #include <linux/spinlock.h>
90
91 #include <linux/amba/bus.h>
92 +#include <linux/fsl/mc.h>
93
94 #include "io-pgtable.h"
95 #include "arm-smmu-regs.h"
96 @@ -1474,6 +1475,8 @@ static struct iommu_group *arm_smmu_devi
97
98 if (dev_is_pci(dev))
99 group = pci_device_group(dev);
100 + else if (dev_is_fsl_mc(dev))
101 + group = fsl_mc_device_group(dev);
102 else
103 group = generic_device_group(dev);
104
105 @@ -2052,6 +2055,10 @@ static void arm_smmu_bus_init(void)
106 bus_set_iommu(&pci_bus_type, &arm_smmu_ops);
107 }
108 #endif
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);
112 +#endif
113 }
114
115 static int arm_smmu_device_probe(struct platform_device *pdev)
116 --- a/drivers/iommu/iommu.c
117 +++ b/drivers/iommu/iommu.c
118 @@ -33,6 +33,7 @@
119 #include <linux/bitops.h>
120 #include <linux/property.h>
121 #include <trace/events/iommu.h>
122 +#include <linux/fsl/mc.h>
123
124 static struct kset *iommu_group_kset;
125 static DEFINE_IDA(iommu_group_ida);
126 @@ -990,6 +991,26 @@ struct iommu_group *pci_device_group(str
127 return iommu_group_alloc();
128 }
129
130 +/* Get the IOMMU group for device on fsl-mc bus */
131 +struct iommu_group *fsl_mc_device_group(struct device *dev)
132 +{
133 + struct device *cont_dev = fsl_mc_cont_dev(dev);
134 + struct iommu_group *group;
135 +
136 + /* Container device is responsible for creating the iommu group */
137 + if (fsl_mc_is_cont_dev(dev)) {
138 + group = iommu_group_alloc();
139 + if (IS_ERR(group))
140 + return NULL;
141 + } else {
142 + get_device(cont_dev);
143 + group = iommu_group_get(cont_dev);
144 + put_device(cont_dev);
145 + }
146 +
147 + return group;
148 +}
149 +
150 /**
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
155 @@ -24,6 +24,7 @@
156 #include <linux/of_iommu.h>
157 #include <linux/of_pci.h>
158 #include <linux/slab.h>
159 +#include <linux/fsl/mc.h>
160
161 #define NO_IOMMU 1
162
163 @@ -143,15 +144,115 @@ struct of_pci_iommu_alias_info {
164 struct device_node *np;
165 };
166
167 +/**
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.
175 + *
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.
183 + *
184 + * Return: 0 on success or a standard error code on failure.
185 + */
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)
189 +{
190 + u32 map_mask, masked_rid;
191 + int map_len;
192 + const __be32 *map = NULL;
193 +
194 + if (!np || !map_name || (!target && !id_out))
195 + return -EINVAL;
196 +
197 + map = of_get_property(np, map_name, &map_len);
198 + if (!map) {
199 + if (target)
200 + return -ENODEV;
201 + /* Otherwise, no map implies no translation */
202 + *id_out = rid;
203 + return 0;
204 + }
205 +
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);
209 + return -EINVAL;
210 + }
211 +
212 + /* The default is to select all bits. */
213 + map_mask = 0xffffffff;
214 +
215 + /*
216 + * Can be overridden by "{iommu,msi}-map-mask" property.
217 + * If of_property_read_u32() fails, the default is used.
218 + */
219 + if (map_mask_name)
220 + of_property_read_u32(np, map_mask_name, &map_mask);
221 +
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);
229 +
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);
234 + return -EFAULT;
235 + }
236 +
237 + if (masked_rid < rid_base || masked_rid >= rid_base + rid_len)
238 + continue;
239 +
240 + phandle_node = of_find_node_by_phandle(phandle);
241 + if (!phandle_node)
242 + return -ENODEV;
243 +
244 + if (target) {
245 + if (*target)
246 + of_node_put(phandle_node);
247 + else
248 + *target = phandle_node;
249 +
250 + if (*target != phandle_node)
251 + continue;
252 + }
253 +
254 + if (id_out)
255 + *id_out = masked_rid - rid_base + out_base;
256 +
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);
260 + return 0;
261 + }
262 +
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);
265 + return -EFAULT;
266 +}
267 static int of_pci_iommu_init(struct pci_dev *pdev, u16 alias, void *data)
268 {
269 struct of_pci_iommu_alias_info *info = data;
270 struct of_phandle_args iommu_spec = { .args_count = 1 };
271 int err;
272
273 - err = of_pci_map_rid(info->np, alias, "iommu-map",
274 - "iommu-map-mask", &iommu_spec.np,
275 - iommu_spec.args);
276 + err = of_map_rid(info->np, alias, "iommu-map",
277 + "iommu-map-mask", &iommu_spec.np,
278 + iommu_spec.args);
279 if (err)
280 return err == -ENODEV ? NO_IOMMU : err;
281
282 @@ -160,6 +261,23 @@ static int of_pci_iommu_init(struct pci_
283 return err;
284 }
285
286 +static int of_fsl_mc_iommu_init(struct fsl_mc_device *mc_dev,
287 + struct device_node *master_np)
288 +{
289 + struct of_phandle_args iommu_spec = { .args_count = 1 };
290 + int err;
291 +
292 + err = of_map_rid(master_np, mc_dev->icid, "iommu-map",
293 + "iommu-map-mask", &iommu_spec.np,
294 + iommu_spec.args);
295 + if (err)
296 + return err == -ENODEV ? NO_IOMMU : err;
297 +
298 + err = of_iommu_xlate(&mc_dev->dev, &iommu_spec);
299 + of_node_put(iommu_spec.np);
300 + return err;
301 +}
302 +
303 const struct iommu_ops *of_iommu_configure(struct device *dev,
304 struct device_node *master_np)
305 {
306 @@ -191,6 +309,8 @@ const struct iommu_ops *of_iommu_configu
307
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);
312 } else {
313 struct of_phandle_args iommu_spec;
314 int idx = 0;
315 --- a/drivers/of/irq.c
316 +++ b/drivers/of/irq.c
317 @@ -26,7 +26,7 @@
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>
325
326 @@ -593,8 +593,8 @@ static u32 __of_msi_map_rid(struct devic
327 * "msi-map" property.
328 */
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))
334 break;
335 return rid_out;
336 }
337 --- a/drivers/of/of_pci.c
338 +++ b/drivers/of/of_pci.c
339 @@ -281,104 +281,3 @@ parse_failed:
340 }
341 EXPORT_SYMBOL_GPL(of_pci_get_host_bridge_resources);
342 #endif /* CONFIG_OF_ADDRESS */
343 -
344 -/**
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.
352 - *
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.
360 - *
361 - * Return: 0 on success or a standard error code on failure.
362 - */
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)
366 -{
367 - u32 map_mask, masked_rid;
368 - int map_len;
369 - const __be32 *map = NULL;
370 -
371 - if (!np || !map_name || (!target && !id_out))
372 - return -EINVAL;
373 -
374 - map = of_get_property(np, map_name, &map_len);
375 - if (!map) {
376 - if (target)
377 - return -ENODEV;
378 - /* Otherwise, no map implies no translation */
379 - *id_out = rid;
380 - return 0;
381 - }
382 -
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);
386 - return -EINVAL;
387 - }
388 -
389 - /* The default is to select all bits. */
390 - map_mask = 0xffffffff;
391 -
392 - /*
393 - * Can be overridden by "{iommu,msi}-map-mask" property.
394 - * If of_property_read_u32() fails, the default is used.
395 - */
396 - if (map_mask_name)
397 - of_property_read_u32(np, map_mask_name, &map_mask);
398 -
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);
406 -
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);
411 - return -EFAULT;
412 - }
413 -
414 - if (masked_rid < rid_base || masked_rid >= rid_base + rid_len)
415 - continue;
416 -
417 - phandle_node = of_find_node_by_phandle(phandle);
418 - if (!phandle_node)
419 - return -ENODEV;
420 -
421 - if (target) {
422 - if (*target)
423 - of_node_put(phandle_node);
424 - else
425 - *target = phandle_node;
426 -
427 - if (*target != phandle_node)
428 - continue;
429 - }
430 -
431 - if (id_out)
432 - *id_out = masked_rid - rid_base + out_base;
433 -
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);
437 - return 0;
438 - }
439 -
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);
442 - return -EFAULT;
443 -}
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);
452
453 /**
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);
460
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);
464 #else
465
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
468 return NULL;
469 }
470
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)
474 +{
475 + return -EINVAL;
476 +}
477 +
478 #endif /* CONFIG_OF_IOMMU */
479
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);
490 #else
491 static inline int of_irq_parse_pci(const struct pci_dev *pdev, struct of_phandle_args *out_irq)
492 {
493 @@ -57,13 +54,6 @@ of_get_pci_domain_nr(struct device_node
494 return -1;
495 }
496
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)
500 -{
501 - return -EINVAL;
502 -}
503 -
504 static inline int
505 of_pci_get_max_link_speed(struct device_node *node)
506 {