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