dpio-driver.c 8.0 KB

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