dpio-driver.c 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337
  1. // SPDX-License-Identifier: (GPL-2.0+ OR BSD-3-Clause)
  2. /*
  3. * Copyright 2014-2016 Freescale Semiconductor Inc.
  4. * Copyright NXP 2016
  5. *
  6. */
  7. #include <linux/types.h>
  8. #include <linux/init.h>
  9. #include <linux/module.h>
  10. #include <linux/platform_device.h>
  11. #include <linux/interrupt.h>
  12. #include <linux/dma-mapping.h>
  13. #include <linux/delay.h>
  14. #include <linux/io.h>
  15. #include <linux/sys_soc.h>
  16. #include <linux/fsl/mc.h>
  17. #include <soc/fsl/dpaa2-io.h>
  18. #include "qbman-portal.h"
  19. #include "dpio.h"
  20. #include "dpio-cmd.h"
  21. MODULE_LICENSE("Dual BSD/GPL");
  22. MODULE_AUTHOR("Freescale Semiconductor, Inc");
  23. MODULE_DESCRIPTION("DPIO Driver");
  24. struct dpio_priv {
  25. struct dpaa2_io *io;
  26. };
  27. static cpumask_var_t cpus_unused_mask;
  28. static const struct soc_device_attribute ls1088a_soc[] = {
  29. {.family = "QorIQ LS1088A"},
  30. { /* sentinel */ }
  31. };
  32. static const struct soc_device_attribute ls2080a_soc[] = {
  33. {.family = "QorIQ LS2080A"},
  34. { /* sentinel */ }
  35. };
  36. static const struct soc_device_attribute ls2088a_soc[] = {
  37. {.family = "QorIQ LS2088A"},
  38. { /* sentinel */ }
  39. };
  40. static const struct soc_device_attribute lx2160a_soc[] = {
  41. {.family = "QorIQ LX2160A"},
  42. { /* sentinel */ }
  43. };
  44. static int dpaa2_dpio_get_cluster_sdest(struct fsl_mc_device *dpio_dev, int cpu)
  45. {
  46. int cluster_base, cluster_size;
  47. if (soc_device_match(ls1088a_soc)) {
  48. cluster_base = 2;
  49. cluster_size = 4;
  50. } else if (soc_device_match(ls2080a_soc) ||
  51. soc_device_match(ls2088a_soc) ||
  52. soc_device_match(lx2160a_soc)) {
  53. cluster_base = 0;
  54. cluster_size = 2;
  55. } else {
  56. dev_err(&dpio_dev->dev, "unknown SoC version\n");
  57. return -1;
  58. }
  59. return cluster_base + cpu / cluster_size;
  60. }
  61. static irqreturn_t dpio_irq_handler(int irq_num, void *arg)
  62. {
  63. struct device *dev = (struct device *)arg;
  64. struct dpio_priv *priv = dev_get_drvdata(dev);
  65. return dpaa2_io_irq(priv->io);
  66. }
  67. static void unregister_dpio_irq_handlers(struct fsl_mc_device *dpio_dev)
  68. {
  69. struct fsl_mc_device_irq *irq;
  70. irq = dpio_dev->irqs[0];
  71. /* clear the affinity hint */
  72. irq_set_affinity_hint(irq->virq, NULL);
  73. }
  74. static int register_dpio_irq_handlers(struct fsl_mc_device *dpio_dev, int cpu)
  75. {
  76. int error;
  77. struct fsl_mc_device_irq *irq;
  78. irq = dpio_dev->irqs[0];
  79. error = devm_request_irq(&dpio_dev->dev,
  80. irq->virq,
  81. dpio_irq_handler,
  82. 0,
  83. dev_name(&dpio_dev->dev),
  84. &dpio_dev->dev);
  85. if (error < 0) {
  86. dev_err(&dpio_dev->dev,
  87. "devm_request_irq() failed: %d\n",
  88. error);
  89. return error;
  90. }
  91. /* set the affinity hint */
  92. if (irq_set_affinity_hint(irq->virq, cpumask_of(cpu)))
  93. dev_err(&dpio_dev->dev,
  94. "irq_set_affinity failed irq %d cpu %d\n",
  95. irq->virq, cpu);
  96. return 0;
  97. }
  98. static int dpaa2_dpio_probe(struct fsl_mc_device *dpio_dev)
  99. {
  100. struct dpio_attr dpio_attrs;
  101. struct dpaa2_io_desc desc;
  102. struct dpio_priv *priv;
  103. int err = -ENOMEM;
  104. struct device *dev = &dpio_dev->dev;
  105. int possible_next_cpu;
  106. int sdest;
  107. priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
  108. if (!priv)
  109. goto err_priv_alloc;
  110. dev_set_drvdata(dev, priv);
  111. err = fsl_mc_portal_allocate(dpio_dev, 0, &dpio_dev->mc_io);
  112. if (err) {
  113. dev_dbg(dev, "MC portal allocation failed\n");
  114. err = -EPROBE_DEFER;
  115. goto err_priv_alloc;
  116. }
  117. err = dpio_open(dpio_dev->mc_io, 0, dpio_dev->obj_desc.id,
  118. &dpio_dev->mc_handle);
  119. if (err) {
  120. dev_err(dev, "dpio_open() failed\n");
  121. goto err_open;
  122. }
  123. err = dpio_reset(dpio_dev->mc_io, 0, dpio_dev->mc_handle);
  124. if (err) {
  125. dev_err(dev, "dpio_reset() failed\n");
  126. goto err_reset;
  127. }
  128. err = dpio_get_attributes(dpio_dev->mc_io, 0, dpio_dev->mc_handle,
  129. &dpio_attrs);
  130. if (err) {
  131. dev_err(dev, "dpio_get_attributes() failed %d\n", err);
  132. goto err_get_attr;
  133. }
  134. desc.qman_version = dpio_attrs.qbman_version;
  135. desc.qman_clk = dpio_attrs.clk;
  136. err = dpio_enable(dpio_dev->mc_io, 0, dpio_dev->mc_handle);
  137. if (err) {
  138. dev_err(dev, "dpio_enable() failed %d\n", err);
  139. goto err_get_attr;
  140. }
  141. /* initialize DPIO descriptor */
  142. desc.receives_notifications = dpio_attrs.num_priorities ? 1 : 0;
  143. desc.has_8prio = dpio_attrs.num_priorities == 8 ? 1 : 0;
  144. desc.dpio_id = dpio_dev->obj_desc.id;
  145. /* get the cpu to use for the affinity hint */
  146. possible_next_cpu = cpumask_first(cpus_unused_mask);
  147. if (possible_next_cpu >= nr_cpu_ids) {
  148. dev_err(dev, "probe failed. Number of DPIOs exceeds NR_CPUS.\n");
  149. err = -ERANGE;
  150. goto err_allocate_irqs;
  151. }
  152. desc.cpu = possible_next_cpu;
  153. cpumask_clear_cpu(possible_next_cpu, cpus_unused_mask);
  154. sdest = dpaa2_dpio_get_cluster_sdest(dpio_dev, desc.cpu);
  155. if (sdest >= 0) {
  156. err = dpio_set_stashing_destination(dpio_dev->mc_io, 0,
  157. dpio_dev->mc_handle,
  158. sdest);
  159. if (err)
  160. dev_err(dev, "dpio_set_stashing_destination failed for cpu%d\n",
  161. desc.cpu);
  162. }
  163. if (dpio_dev->obj_desc.region_count < 3) {
  164. /* No support for DDR backed portals, use classic mapping */
  165. /*
  166. * Set the CENA regs to be the cache inhibited area of the
  167. * portal to avoid coherency issues if a user migrates to
  168. * another core.
  169. */
  170. desc.regs_cena = devm_memremap(dev, dpio_dev->regions[1].start,
  171. resource_size(&dpio_dev->regions[1]),
  172. MEMREMAP_WC);
  173. } else {
  174. desc.regs_cena = devm_memremap(dev, dpio_dev->regions[2].start,
  175. resource_size(&dpio_dev->regions[2]),
  176. MEMREMAP_WB);
  177. }
  178. if (IS_ERR(desc.regs_cena)) {
  179. dev_err(dev, "devm_memremap failed\n");
  180. err = PTR_ERR(desc.regs_cena);
  181. goto err_allocate_irqs;
  182. }
  183. desc.regs_cinh = devm_ioremap(dev, dpio_dev->regions[1].start,
  184. resource_size(&dpio_dev->regions[1]));
  185. if (!desc.regs_cinh) {
  186. err = -ENOMEM;
  187. dev_err(dev, "devm_ioremap failed\n");
  188. goto err_allocate_irqs;
  189. }
  190. err = fsl_mc_allocate_irqs(dpio_dev);
  191. if (err) {
  192. dev_err(dev, "fsl_mc_allocate_irqs failed. err=%d\n", err);
  193. goto err_allocate_irqs;
  194. }
  195. priv->io = dpaa2_io_create(&desc, dev);
  196. if (!priv->io) {
  197. dev_err(dev, "dpaa2_io_create failed\n");
  198. err = -ENOMEM;
  199. goto err_dpaa2_io_create;
  200. }
  201. err = register_dpio_irq_handlers(dpio_dev, desc.cpu);
  202. if (err)
  203. goto err_register_dpio_irq;
  204. dev_info(dev, "probed\n");
  205. dev_dbg(dev, " receives_notifications = %d\n",
  206. desc.receives_notifications);
  207. dpio_close(dpio_dev->mc_io, 0, dpio_dev->mc_handle);
  208. return 0;
  209. err_dpaa2_io_create:
  210. unregister_dpio_irq_handlers(dpio_dev);
  211. err_register_dpio_irq:
  212. fsl_mc_free_irqs(dpio_dev);
  213. err_allocate_irqs:
  214. dpio_disable(dpio_dev->mc_io, 0, dpio_dev->mc_handle);
  215. err_get_attr:
  216. err_reset:
  217. dpio_close(dpio_dev->mc_io, 0, dpio_dev->mc_handle);
  218. err_open:
  219. fsl_mc_portal_free(dpio_dev->mc_io);
  220. err_priv_alloc:
  221. return err;
  222. }
  223. /* Tear down interrupts for a given DPIO object */
  224. static void dpio_teardown_irqs(struct fsl_mc_device *dpio_dev)
  225. {
  226. unregister_dpio_irq_handlers(dpio_dev);
  227. fsl_mc_free_irqs(dpio_dev);
  228. }
  229. static void dpaa2_dpio_remove(struct fsl_mc_device *dpio_dev)
  230. {
  231. struct device *dev;
  232. struct dpio_priv *priv;
  233. int err = 0, cpu;
  234. dev = &dpio_dev->dev;
  235. priv = dev_get_drvdata(dev);
  236. cpu = dpaa2_io_get_cpu(priv->io);
  237. dpaa2_io_down(priv->io);
  238. dpio_teardown_irqs(dpio_dev);
  239. cpumask_set_cpu(cpu, cpus_unused_mask);
  240. err = dpio_open(dpio_dev->mc_io, 0, dpio_dev->obj_desc.id,
  241. &dpio_dev->mc_handle);
  242. if (err) {
  243. dev_err(dev, "dpio_open() failed\n");
  244. goto err_open;
  245. }
  246. dpio_disable(dpio_dev->mc_io, 0, dpio_dev->mc_handle);
  247. dpio_close(dpio_dev->mc_io, 0, dpio_dev->mc_handle);
  248. err_open:
  249. fsl_mc_portal_free(dpio_dev->mc_io);
  250. }
  251. static const struct fsl_mc_device_id dpaa2_dpio_match_id_table[] = {
  252. {
  253. .vendor = FSL_MC_VENDOR_FREESCALE,
  254. .obj_type = "dpio",
  255. },
  256. { .vendor = 0x0 }
  257. };
  258. static struct fsl_mc_driver dpaa2_dpio_driver = {
  259. .driver = {
  260. .name = KBUILD_MODNAME,
  261. .owner = THIS_MODULE,
  262. },
  263. .probe = dpaa2_dpio_probe,
  264. .remove = dpaa2_dpio_remove,
  265. .match_id_table = dpaa2_dpio_match_id_table
  266. };
  267. static int dpio_driver_init(void)
  268. {
  269. if (!zalloc_cpumask_var(&cpus_unused_mask, GFP_KERNEL))
  270. return -ENOMEM;
  271. cpumask_copy(cpus_unused_mask, cpu_online_mask);
  272. return fsl_mc_driver_register(&dpaa2_dpio_driver);
  273. }
  274. static void dpio_driver_exit(void)
  275. {
  276. free_cpumask_var(cpus_unused_mask);
  277. fsl_mc_driver_unregister(&dpaa2_dpio_driver);
  278. }
  279. module_init(dpio_driver_init);
  280. module_exit(dpio_driver_exit);