dpio-driver.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  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/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 irqreturn_t dpio_irq_handler(int irq_num, void *arg)
  28. {
  29. struct device *dev = (struct device *)arg;
  30. struct dpio_priv *priv = dev_get_drvdata(dev);
  31. return dpaa2_io_irq(priv->io);
  32. }
  33. static void unregister_dpio_irq_handlers(struct fsl_mc_device *dpio_dev)
  34. {
  35. struct fsl_mc_device_irq *irq;
  36. irq = dpio_dev->irqs[0];
  37. /* clear the affinity hint */
  38. irq_set_affinity_hint(irq->msi_desc->irq, NULL);
  39. }
  40. static int register_dpio_irq_handlers(struct fsl_mc_device *dpio_dev, int cpu)
  41. {
  42. struct dpio_priv *priv;
  43. int error;
  44. struct fsl_mc_device_irq *irq;
  45. cpumask_t mask;
  46. priv = dev_get_drvdata(&dpio_dev->dev);
  47. irq = dpio_dev->irqs[0];
  48. error = devm_request_irq(&dpio_dev->dev,
  49. irq->msi_desc->irq,
  50. dpio_irq_handler,
  51. 0,
  52. dev_name(&dpio_dev->dev),
  53. &dpio_dev->dev);
  54. if (error < 0) {
  55. dev_err(&dpio_dev->dev,
  56. "devm_request_irq() failed: %d\n",
  57. error);
  58. return error;
  59. }
  60. /* set the affinity hint */
  61. cpumask_clear(&mask);
  62. cpumask_set_cpu(cpu, &mask);
  63. if (irq_set_affinity_hint(irq->msi_desc->irq, &mask))
  64. dev_err(&dpio_dev->dev,
  65. "irq_set_affinity failed irq %d cpu %d\n",
  66. irq->msi_desc->irq, cpu);
  67. return 0;
  68. }
  69. static int dpaa2_dpio_probe(struct fsl_mc_device *dpio_dev)
  70. {
  71. struct dpio_attr dpio_attrs;
  72. struct dpaa2_io_desc desc;
  73. struct dpio_priv *priv;
  74. int err = -ENOMEM;
  75. struct device *dev = &dpio_dev->dev;
  76. static int next_cpu = -1;
  77. priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
  78. if (!priv)
  79. goto err_priv_alloc;
  80. dev_set_drvdata(dev, priv);
  81. err = fsl_mc_portal_allocate(dpio_dev, 0, &dpio_dev->mc_io);
  82. if (err) {
  83. dev_dbg(dev, "MC portal allocation failed\n");
  84. err = -EPROBE_DEFER;
  85. goto err_priv_alloc;
  86. }
  87. err = dpio_open(dpio_dev->mc_io, 0, dpio_dev->obj_desc.id,
  88. &dpio_dev->mc_handle);
  89. if (err) {
  90. dev_err(dev, "dpio_open() failed\n");
  91. goto err_open;
  92. }
  93. err = dpio_get_attributes(dpio_dev->mc_io, 0, dpio_dev->mc_handle,
  94. &dpio_attrs);
  95. if (err) {
  96. dev_err(dev, "dpio_get_attributes() failed %d\n", err);
  97. goto err_get_attr;
  98. }
  99. desc.qman_version = dpio_attrs.qbman_version;
  100. err = dpio_enable(dpio_dev->mc_io, 0, dpio_dev->mc_handle);
  101. if (err) {
  102. dev_err(dev, "dpio_enable() failed %d\n", err);
  103. goto err_get_attr;
  104. }
  105. /* initialize DPIO descriptor */
  106. desc.receives_notifications = dpio_attrs.num_priorities ? 1 : 0;
  107. desc.has_8prio = dpio_attrs.num_priorities == 8 ? 1 : 0;
  108. desc.dpio_id = dpio_dev->obj_desc.id;
  109. /* get the cpu to use for the affinity hint */
  110. if (next_cpu == -1)
  111. next_cpu = cpumask_first(cpu_online_mask);
  112. else
  113. next_cpu = cpumask_next(next_cpu, cpu_online_mask);
  114. if (!cpu_possible(next_cpu)) {
  115. dev_err(dev, "probe failed. Number of DPIOs exceeds NR_CPUS.\n");
  116. err = -ERANGE;
  117. goto err_allocate_irqs;
  118. }
  119. desc.cpu = next_cpu;
  120. /*
  121. * Set the CENA regs to be the cache inhibited area of the portal to
  122. * avoid coherency issues if a user migrates to another core.
  123. */
  124. desc.regs_cena = devm_memremap(dev, dpio_dev->regions[1].start,
  125. resource_size(&dpio_dev->regions[1]),
  126. MEMREMAP_WC);
  127. if (IS_ERR(desc.regs_cena)) {
  128. dev_err(dev, "devm_memremap failed\n");
  129. err = PTR_ERR(desc.regs_cena);
  130. goto err_allocate_irqs;
  131. }
  132. desc.regs_cinh = devm_ioremap(dev, dpio_dev->regions[1].start,
  133. resource_size(&dpio_dev->regions[1]));
  134. if (!desc.regs_cinh) {
  135. err = -ENOMEM;
  136. dev_err(dev, "devm_ioremap failed\n");
  137. goto err_allocate_irqs;
  138. }
  139. err = fsl_mc_allocate_irqs(dpio_dev);
  140. if (err) {
  141. dev_err(dev, "fsl_mc_allocate_irqs failed. err=%d\n", err);
  142. goto err_allocate_irqs;
  143. }
  144. err = register_dpio_irq_handlers(dpio_dev, desc.cpu);
  145. if (err)
  146. goto err_register_dpio_irq;
  147. priv->io = dpaa2_io_create(&desc);
  148. if (!priv->io) {
  149. dev_err(dev, "dpaa2_io_create failed\n");
  150. err = -ENOMEM;
  151. goto err_dpaa2_io_create;
  152. }
  153. dev_info(dev, "probed\n");
  154. dev_dbg(dev, " receives_notifications = %d\n",
  155. desc.receives_notifications);
  156. dpio_close(dpio_dev->mc_io, 0, dpio_dev->mc_handle);
  157. fsl_mc_portal_free(dpio_dev->mc_io);
  158. return 0;
  159. err_dpaa2_io_create:
  160. unregister_dpio_irq_handlers(dpio_dev);
  161. err_register_dpio_irq:
  162. fsl_mc_free_irqs(dpio_dev);
  163. err_allocate_irqs:
  164. dpio_disable(dpio_dev->mc_io, 0, dpio_dev->mc_handle);
  165. err_get_attr:
  166. dpio_close(dpio_dev->mc_io, 0, dpio_dev->mc_handle);
  167. err_open:
  168. fsl_mc_portal_free(dpio_dev->mc_io);
  169. err_priv_alloc:
  170. return err;
  171. }
  172. /* Tear down interrupts for a given DPIO object */
  173. static void dpio_teardown_irqs(struct fsl_mc_device *dpio_dev)
  174. {
  175. unregister_dpio_irq_handlers(dpio_dev);
  176. fsl_mc_free_irqs(dpio_dev);
  177. }
  178. static int dpaa2_dpio_remove(struct fsl_mc_device *dpio_dev)
  179. {
  180. struct device *dev;
  181. struct dpio_priv *priv;
  182. int err;
  183. dev = &dpio_dev->dev;
  184. priv = dev_get_drvdata(dev);
  185. dpaa2_io_down(priv->io);
  186. dpio_teardown_irqs(dpio_dev);
  187. err = fsl_mc_portal_allocate(dpio_dev, 0, &dpio_dev->mc_io);
  188. if (err) {
  189. dev_err(dev, "MC portal allocation failed\n");
  190. goto err_mcportal;
  191. }
  192. err = dpio_open(dpio_dev->mc_io, 0, dpio_dev->obj_desc.id,
  193. &dpio_dev->mc_handle);
  194. if (err) {
  195. dev_err(dev, "dpio_open() failed\n");
  196. goto err_open;
  197. }
  198. dpio_disable(dpio_dev->mc_io, 0, dpio_dev->mc_handle);
  199. dpio_close(dpio_dev->mc_io, 0, dpio_dev->mc_handle);
  200. fsl_mc_portal_free(dpio_dev->mc_io);
  201. return 0;
  202. err_open:
  203. fsl_mc_portal_free(dpio_dev->mc_io);
  204. err_mcportal:
  205. return err;
  206. }
  207. static const struct fsl_mc_device_id dpaa2_dpio_match_id_table[] = {
  208. {
  209. .vendor = FSL_MC_VENDOR_FREESCALE,
  210. .obj_type = "dpio",
  211. },
  212. { .vendor = 0x0 }
  213. };
  214. static struct fsl_mc_driver dpaa2_dpio_driver = {
  215. .driver = {
  216. .name = KBUILD_MODNAME,
  217. .owner = THIS_MODULE,
  218. },
  219. .probe = dpaa2_dpio_probe,
  220. .remove = dpaa2_dpio_remove,
  221. .match_id_table = dpaa2_dpio_match_id_table
  222. };
  223. static int dpio_driver_init(void)
  224. {
  225. return fsl_mc_driver_register(&dpaa2_dpio_driver);
  226. }
  227. static void dpio_driver_exit(void)
  228. {
  229. fsl_mc_driver_unregister(&dpaa2_dpio_driver);
  230. }
  231. module_init(dpio_driver_init);
  232. module_exit(dpio_driver_exit);