dpio-driver.c 6.4 KB

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