821-smmu-support-layerscape.patch 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506
  1. From 05375244c8e74f90239db92646a771905fdfc0db Mon Sep 17 00:00:00 2001
  2. From: Biwen Li <[email protected]>
  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. Signed-off-by: Nipun Gupta <[email protected]>
  7. Signed-off-by: Stuart Yoder <[email protected]>
  8. Signed-off-by: Zhao Qiang <[email protected]>
  9. Signed-off-by: Biwen Li <[email protected]>
  10. ---
  11. .../devicetree/bindings/misc/fsl,qoriq-mc.txt | 39 ++++++
  12. drivers/iommu/arm-smmu.c | 7 +
  13. drivers/iommu/iommu.c | 21 +++
  14. drivers/iommu/of_iommu.c | 126 +++++++++++++++++-
  15. drivers/of/irq.c | 6 +-
  16. drivers/of/of_pci.c | 101 --------------
  17. include/linux/iommu.h | 2 +
  18. include/linux/of_iommu.h | 10 ++
  19. include/linux/of_pci.h | 10 --
  20. 9 files changed, 205 insertions(+), 117 deletions(-)
  21. --- a/Documentation/devicetree/bindings/misc/fsl,qoriq-mc.txt
  22. +++ b/Documentation/devicetree/bindings/misc/fsl,qoriq-mc.txt
  23. @@ -9,6 +9,25 @@ blocks that can be used to create functi
  24. such as network interfaces, crypto accelerator instances, L2 switches,
  25. etc.
  26. +For an overview of the DPAA2 architecture and fsl-mc bus see:
  27. +drivers/staging/fsl-mc/README.txt
  28. +
  29. +As described in the above overview, all DPAA2 objects in a DPRC share the
  30. +same hardware "isolation context" and a 10-bit value called an ICID
  31. +(isolation context id) is expressed by the hardware to identify
  32. +the requester.
  33. +
  34. +The generic 'iommus' property is insufficient to describe the relationship
  35. +between ICIDs and IOMMUs, so an iommu-map property is used to define
  36. +the set of possible ICIDs under a root DPRC and how they map to
  37. +an IOMMU.
  38. +
  39. +For generic IOMMU bindings, see
  40. +Documentation/devicetree/bindings/iommu/iommu.txt.
  41. +
  42. +For arm-smmu binding, see:
  43. +Documentation/devicetree/bindings/iommu/arm,smmu.txt.
  44. +
  45. Required properties:
  46. - compatible
  47. @@ -88,14 +107,34 @@ Sub-nodes:
  48. Value type: <phandle>
  49. Definition: Specifies the phandle to the PHY device node associated
  50. with the this dpmac.
  51. +Optional properties:
  52. +
  53. +- iommu-map: Maps an ICID to an IOMMU and associated iommu-specifier
  54. + data.
  55. +
  56. + The property is an arbitrary number of tuples of
  57. + (icid-base,iommu,iommu-base,length).
  58. +
  59. + Any ICID i in the interval [icid-base, icid-base + length) is
  60. + associated with the listed IOMMU, with the iommu-specifier
  61. + (i - icid-base + iommu-base).
  62. Example:
  63. + smmu: iommu@5000000 {
  64. + compatible = "arm,mmu-500";
  65. + #iommu-cells = <2>;
  66. + stream-match-mask = <0x7C00>;
  67. + ...
  68. + };
  69. +
  70. fsl_mc: fsl-mc@80c000000 {
  71. compatible = "fsl,qoriq-mc";
  72. reg = <0x00000008 0x0c000000 0 0x40>, /* MC portal base */
  73. <0x00000000 0x08340000 0 0x40000>; /* MC control reg */
  74. msi-parent = <&its>;
  75. + /* define map for ICIDs 23-64 */
  76. + iommu-map = <23 &smmu 23 41>;
  77. #address-cells = <3>;
  78. #size-cells = <1>;
  79. --- a/drivers/iommu/arm-smmu.c
  80. +++ b/drivers/iommu/arm-smmu.c
  81. @@ -52,6 +52,7 @@
  82. #include <linux/spinlock.h>
  83. #include <linux/amba/bus.h>
  84. +#include <linux/fsl/mc.h>
  85. #include "io-pgtable.h"
  86. #include "arm-smmu-regs.h"
  87. @@ -1465,6 +1466,8 @@ static struct iommu_group *arm_smmu_devi
  88. if (dev_is_pci(dev))
  89. group = pci_device_group(dev);
  90. + else if (dev_is_fsl_mc(dev))
  91. + group = fsl_mc_device_group(dev);
  92. else
  93. group = generic_device_group(dev);
  94. @@ -2043,6 +2046,10 @@ static void arm_smmu_bus_init(void)
  95. bus_set_iommu(&pci_bus_type, &arm_smmu_ops);
  96. }
  97. #endif
  98. +#ifdef CONFIG_FSL_MC_BUS
  99. + if (!iommu_present(&fsl_mc_bus_type))
  100. + bus_set_iommu(&fsl_mc_bus_type, &arm_smmu_ops);
  101. +#endif
  102. }
  103. static int arm_smmu_device_probe(struct platform_device *pdev)
  104. --- a/drivers/iommu/iommu.c
  105. +++ b/drivers/iommu/iommu.c
  106. @@ -33,6 +33,7 @@
  107. #include <linux/bitops.h>
  108. #include <linux/property.h>
  109. #include <trace/events/iommu.h>
  110. +#include <linux/fsl/mc.h>
  111. static struct kset *iommu_group_kset;
  112. static DEFINE_IDA(iommu_group_ida);
  113. @@ -987,6 +988,26 @@ struct iommu_group *pci_device_group(str
  114. return iommu_group_alloc();
  115. }
  116. +/* Get the IOMMU group for device on fsl-mc bus */
  117. +struct iommu_group *fsl_mc_device_group(struct device *dev)
  118. +{
  119. + struct device *cont_dev = fsl_mc_cont_dev(dev);
  120. + struct iommu_group *group;
  121. +
  122. + /* Container device is responsible for creating the iommu group */
  123. + if (fsl_mc_is_cont_dev(dev)) {
  124. + group = iommu_group_alloc();
  125. + if (IS_ERR(group))
  126. + return NULL;
  127. + } else {
  128. + get_device(cont_dev);
  129. + group = iommu_group_get(cont_dev);
  130. + put_device(cont_dev);
  131. + }
  132. +
  133. + return group;
  134. +}
  135. +
  136. /**
  137. * iommu_group_get_for_dev - Find or create the IOMMU group for a device
  138. * @dev: target device
  139. --- a/drivers/iommu/of_iommu.c
  140. +++ b/drivers/iommu/of_iommu.c
  141. @@ -24,6 +24,7 @@
  142. #include <linux/of_iommu.h>
  143. #include <linux/of_pci.h>
  144. #include <linux/slab.h>
  145. +#include <linux/fsl/mc.h>
  146. #define NO_IOMMU 1
  147. @@ -143,15 +144,115 @@ struct of_pci_iommu_alias_info {
  148. struct device_node *np;
  149. };
  150. +/**
  151. + * of_map_rid - Translate a requester ID through a downstream mapping.
  152. + * @np: root complex device node.
  153. + * @rid: Requester ID to map.
  154. + * @map_name: property name of the map to use.
  155. + * @map_mask_name: optional property name of the mask to use.
  156. + * @target: optional pointer to a target device node.
  157. + * @id_out: optional pointer to receive the translated ID.
  158. + *
  159. + * Given PCI/MC requester ID, look up the appropriate implementation-defined
  160. + * platform ID and/or the target device which receives transactions on that
  161. + * ID, as per the "iommu-map" and "msi-map" bindings. Either of @target or
  162. + * @id_out may be NULL if only the other is required. If @target points to
  163. + * a non-NULL device node pointer, only entries targeting that node will be
  164. + * matched; if it points to a NULL value, it will receive the device node of
  165. + * the first matching target phandle, with a reference held.
  166. + *
  167. + * Return: 0 on success or a standard error code on failure.
  168. + */
  169. +int of_map_rid(struct device_node *np, u32 rid,
  170. + const char *map_name, const char *map_mask_name,
  171. + struct device_node **target, u32 *id_out)
  172. +{
  173. + u32 map_mask, masked_rid;
  174. + int map_len;
  175. + const __be32 *map = NULL;
  176. +
  177. + if (!np || !map_name || (!target && !id_out))
  178. + return -EINVAL;
  179. +
  180. + map = of_get_property(np, map_name, &map_len);
  181. + if (!map) {
  182. + if (target)
  183. + return -ENODEV;
  184. + /* Otherwise, no map implies no translation */
  185. + *id_out = rid;
  186. + return 0;
  187. + }
  188. +
  189. + if (!map_len || map_len % (4 * sizeof(*map))) {
  190. + pr_err("%pOF: Error: Bad %s length: %d\n", np,
  191. + map_name, map_len);
  192. + return -EINVAL;
  193. + }
  194. +
  195. + /* The default is to select all bits. */
  196. + map_mask = 0xffffffff;
  197. +
  198. + /*
  199. + * Can be overridden by "{iommu,msi}-map-mask" property.
  200. + * If of_property_read_u32() fails, the default is used.
  201. + */
  202. + if (map_mask_name)
  203. + of_property_read_u32(np, map_mask_name, &map_mask);
  204. +
  205. + masked_rid = map_mask & rid;
  206. + for ( ; map_len > 0; map_len -= 4 * sizeof(*map), map += 4) {
  207. + struct device_node *phandle_node;
  208. + u32 rid_base = be32_to_cpup(map + 0);
  209. + u32 phandle = be32_to_cpup(map + 1);
  210. + u32 out_base = be32_to_cpup(map + 2);
  211. + u32 rid_len = be32_to_cpup(map + 3);
  212. +
  213. + if (rid_base & ~map_mask) {
  214. + pr_err("%pOF: Invalid %s translation - %s-mask (0x%x) ignores rid-base (0x%x)\n",
  215. + np, map_name, map_name,
  216. + map_mask, rid_base);
  217. + return -EFAULT;
  218. + }
  219. +
  220. + if (masked_rid < rid_base || masked_rid >= rid_base + rid_len)
  221. + continue;
  222. +
  223. + phandle_node = of_find_node_by_phandle(phandle);
  224. + if (!phandle_node)
  225. + return -ENODEV;
  226. +
  227. + if (target) {
  228. + if (*target)
  229. + of_node_put(phandle_node);
  230. + else
  231. + *target = phandle_node;
  232. +
  233. + if (*target != phandle_node)
  234. + continue;
  235. + }
  236. +
  237. + if (id_out)
  238. + *id_out = masked_rid - rid_base + out_base;
  239. +
  240. + pr_debug("%pOF: %s, using mask %08x, rid-base: %08x, out-base: %08x, length: %08x, rid: %08x -> %08x\n",
  241. + np, map_name, map_mask, rid_base, out_base,
  242. + rid_len, rid, *id_out);
  243. + return 0;
  244. + }
  245. +
  246. + pr_err("%pOF: Invalid %s translation - no match for rid 0x%x on %pOF\n",
  247. + np, map_name, rid, target && *target ? *target : NULL);
  248. + return -EFAULT;
  249. +}
  250. static int of_pci_iommu_init(struct pci_dev *pdev, u16 alias, void *data)
  251. {
  252. struct of_pci_iommu_alias_info *info = data;
  253. struct of_phandle_args iommu_spec = { .args_count = 1 };
  254. int err;
  255. - err = of_pci_map_rid(info->np, alias, "iommu-map",
  256. - "iommu-map-mask", &iommu_spec.np,
  257. - iommu_spec.args);
  258. + err = of_map_rid(info->np, alias, "iommu-map",
  259. + "iommu-map-mask", &iommu_spec.np,
  260. + iommu_spec.args);
  261. if (err)
  262. return err == -ENODEV ? NO_IOMMU : err;
  263. @@ -160,6 +261,23 @@ static int of_pci_iommu_init(struct pci_
  264. return err;
  265. }
  266. +static int of_fsl_mc_iommu_init(struct fsl_mc_device *mc_dev,
  267. + struct device_node *master_np)
  268. +{
  269. + struct of_phandle_args iommu_spec = { .args_count = 1 };
  270. + int err;
  271. +
  272. + err = of_map_rid(master_np, mc_dev->icid, "iommu-map",
  273. + "iommu-map-mask", &iommu_spec.np,
  274. + iommu_spec.args);
  275. + if (err)
  276. + return err == -ENODEV ? NO_IOMMU : err;
  277. +
  278. + err = of_iommu_xlate(&mc_dev->dev, &iommu_spec);
  279. + of_node_put(iommu_spec.np);
  280. + return err;
  281. +}
  282. +
  283. const struct iommu_ops *of_iommu_configure(struct device *dev,
  284. struct device_node *master_np)
  285. {
  286. @@ -191,6 +309,8 @@ const struct iommu_ops *of_iommu_configu
  287. err = pci_for_each_dma_alias(to_pci_dev(dev),
  288. of_pci_iommu_init, &info);
  289. + } else if (dev_is_fsl_mc(dev)) {
  290. + err = of_fsl_mc_iommu_init(to_fsl_mc_device(dev), master_np);
  291. } else {
  292. struct of_phandle_args iommu_spec;
  293. int idx = 0;
  294. --- a/drivers/of/irq.c
  295. +++ b/drivers/of/irq.c
  296. @@ -26,7 +26,7 @@
  297. #include <linux/module.h>
  298. #include <linux/of.h>
  299. #include <linux/of_irq.h>
  300. -#include <linux/of_pci.h>
  301. +#include <linux/of_iommu.h>
  302. #include <linux/string.h>
  303. #include <linux/slab.h>
  304. @@ -593,8 +593,8 @@ static u32 __of_msi_map_rid(struct devic
  305. * "msi-map" property.
  306. */
  307. for (parent_dev = dev; parent_dev; parent_dev = parent_dev->parent)
  308. - if (!of_pci_map_rid(parent_dev->of_node, rid_in, "msi-map",
  309. - "msi-map-mask", np, &rid_out))
  310. + if (!of_map_rid(parent_dev->of_node, rid_in, "msi-map",
  311. + "msi-map-mask", np, &rid_out))
  312. break;
  313. return rid_out;
  314. }
  315. --- a/drivers/of/of_pci.c
  316. +++ b/drivers/of/of_pci.c
  317. @@ -281,104 +281,3 @@ parse_failed:
  318. }
  319. EXPORT_SYMBOL_GPL(of_pci_get_host_bridge_resources);
  320. #endif /* CONFIG_OF_ADDRESS */
  321. -
  322. -/**
  323. - * of_pci_map_rid - Translate a requester ID through a downstream mapping.
  324. - * @np: root complex device node.
  325. - * @rid: PCI requester ID to map.
  326. - * @map_name: property name of the map to use.
  327. - * @map_mask_name: optional property name of the mask to use.
  328. - * @target: optional pointer to a target device node.
  329. - * @id_out: optional pointer to receive the translated ID.
  330. - *
  331. - * Given a PCI requester ID, look up the appropriate implementation-defined
  332. - * platform ID and/or the target device which receives transactions on that
  333. - * ID, as per the "iommu-map" and "msi-map" bindings. Either of @target or
  334. - * @id_out may be NULL if only the other is required. If @target points to
  335. - * a non-NULL device node pointer, only entries targeting that node will be
  336. - * matched; if it points to a NULL value, it will receive the device node of
  337. - * the first matching target phandle, with a reference held.
  338. - *
  339. - * Return: 0 on success or a standard error code on failure.
  340. - */
  341. -int of_pci_map_rid(struct device_node *np, u32 rid,
  342. - const char *map_name, const char *map_mask_name,
  343. - struct device_node **target, u32 *id_out)
  344. -{
  345. - u32 map_mask, masked_rid;
  346. - int map_len;
  347. - const __be32 *map = NULL;
  348. -
  349. - if (!np || !map_name || (!target && !id_out))
  350. - return -EINVAL;
  351. -
  352. - map = of_get_property(np, map_name, &map_len);
  353. - if (!map) {
  354. - if (target)
  355. - return -ENODEV;
  356. - /* Otherwise, no map implies no translation */
  357. - *id_out = rid;
  358. - return 0;
  359. - }
  360. -
  361. - if (!map_len || map_len % (4 * sizeof(*map))) {
  362. - pr_err("%pOF: Error: Bad %s length: %d\n", np,
  363. - map_name, map_len);
  364. - return -EINVAL;
  365. - }
  366. -
  367. - /* The default is to select all bits. */
  368. - map_mask = 0xffffffff;
  369. -
  370. - /*
  371. - * Can be overridden by "{iommu,msi}-map-mask" property.
  372. - * If of_property_read_u32() fails, the default is used.
  373. - */
  374. - if (map_mask_name)
  375. - of_property_read_u32(np, map_mask_name, &map_mask);
  376. -
  377. - masked_rid = map_mask & rid;
  378. - for ( ; map_len > 0; map_len -= 4 * sizeof(*map), map += 4) {
  379. - struct device_node *phandle_node;
  380. - u32 rid_base = be32_to_cpup(map + 0);
  381. - u32 phandle = be32_to_cpup(map + 1);
  382. - u32 out_base = be32_to_cpup(map + 2);
  383. - u32 rid_len = be32_to_cpup(map + 3);
  384. -
  385. - if (rid_base & ~map_mask) {
  386. - pr_err("%pOF: Invalid %s translation - %s-mask (0x%x) ignores rid-base (0x%x)\n",
  387. - np, map_name, map_name,
  388. - map_mask, rid_base);
  389. - return -EFAULT;
  390. - }
  391. -
  392. - if (masked_rid < rid_base || masked_rid >= rid_base + rid_len)
  393. - continue;
  394. -
  395. - phandle_node = of_find_node_by_phandle(phandle);
  396. - if (!phandle_node)
  397. - return -ENODEV;
  398. -
  399. - if (target) {
  400. - if (*target)
  401. - of_node_put(phandle_node);
  402. - else
  403. - *target = phandle_node;
  404. -
  405. - if (*target != phandle_node)
  406. - continue;
  407. - }
  408. -
  409. - if (id_out)
  410. - *id_out = masked_rid - rid_base + out_base;
  411. -
  412. - pr_debug("%pOF: %s, using mask %08x, rid-base: %08x, out-base: %08x, length: %08x, rid: %08x -> %08x\n",
  413. - np, map_name, map_mask, rid_base, out_base,
  414. - rid_len, rid, *id_out);
  415. - return 0;
  416. - }
  417. -
  418. - pr_err("%pOF: Invalid %s translation - no match for rid 0x%x on %pOF\n",
  419. - np, map_name, rid, target && *target ? *target : NULL);
  420. - return -EFAULT;
  421. -}
  422. --- a/include/linux/iommu.h
  423. +++ b/include/linux/iommu.h
  424. @@ -389,6 +389,8 @@ static inline size_t iommu_map_sg(struct
  425. extern struct iommu_group *pci_device_group(struct device *dev);
  426. /* Generic device grouping function */
  427. extern struct iommu_group *generic_device_group(struct device *dev);
  428. +/* FSL-MC device grouping function */
  429. +struct iommu_group *fsl_mc_device_group(struct device *dev);
  430. /**
  431. * struct iommu_fwspec - per-device IOMMU instance data
  432. --- a/include/linux/of_iommu.h
  433. +++ b/include/linux/of_iommu.h
  434. @@ -15,6 +15,9 @@ extern int of_get_dma_window(struct devi
  435. extern const struct iommu_ops *of_iommu_configure(struct device *dev,
  436. struct device_node *master_np);
  437. +int of_map_rid(struct device_node *np, u32 rid,
  438. + const char *map_name, const char *map_mask_name,
  439. + struct device_node **target, u32 *id_out);
  440. #else
  441. static inline int of_get_dma_window(struct device_node *dn, const char *prefix,
  442. @@ -30,6 +33,13 @@ static inline const struct iommu_ops *of
  443. return NULL;
  444. }
  445. +static inline int of_map_rid(struct device_node *np, u32 rid,
  446. + const char *map_name, const char *map_mask_name,
  447. + struct device_node **target, u32 *id_out)
  448. +{
  449. + return -EINVAL;
  450. +}
  451. +
  452. #endif /* CONFIG_OF_IOMMU */
  453. extern struct of_device_id __iommu_of_table;
  454. --- a/include/linux/of_pci.h
  455. +++ b/include/linux/of_pci.h
  456. @@ -19,9 +19,6 @@ int of_pci_parse_bus_range(struct device
  457. int of_get_pci_domain_nr(struct device_node *node);
  458. int of_pci_get_max_link_speed(struct device_node *node);
  459. void of_pci_check_probe_only(void);
  460. -int of_pci_map_rid(struct device_node *np, u32 rid,
  461. - const char *map_name, const char *map_mask_name,
  462. - struct device_node **target, u32 *id_out);
  463. #else
  464. static inline int of_irq_parse_pci(const struct pci_dev *pdev, struct of_phandle_args *out_irq)
  465. {
  466. @@ -57,13 +54,6 @@ of_get_pci_domain_nr(struct device_node
  467. return -1;
  468. }
  469. -static inline int of_pci_map_rid(struct device_node *np, u32 rid,
  470. - const char *map_name, const char *map_mask_name,
  471. - struct device_node **target, u32 *id_out)
  472. -{
  473. - return -EINVAL;
  474. -}
  475. -
  476. static inline int
  477. of_pci_get_max_link_speed(struct device_node *node)
  478. {