uio_pdrv_genirq.c 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * drivers/uio/uio_pdrv_genirq.c
  4. *
  5. * Userspace I/O platform driver with generic IRQ handling code.
  6. *
  7. * Copyright (C) 2008 Magnus Damm
  8. *
  9. * Based on uio_pdrv.c by Uwe Kleine-Koenig,
  10. * Copyright (C) 2008 by Digi International Inc.
  11. * All rights reserved.
  12. */
  13. #include <linux/platform_device.h>
  14. #include <linux/uio_driver.h>
  15. #include <linux/spinlock.h>
  16. #include <linux/bitops.h>
  17. #include <linux/module.h>
  18. #include <linux/interrupt.h>
  19. #include <linux/stringify.h>
  20. #include <linux/pm_runtime.h>
  21. #include <linux/slab.h>
  22. #include <linux/irq.h>
  23. #include <linux/of.h>
  24. #include <linux/of_platform.h>
  25. #include <linux/of_address.h>
  26. #define DRIVER_NAME "uio_pdrv_genirq"
  27. struct uio_pdrv_genirq_platdata {
  28. struct uio_info *uioinfo;
  29. spinlock_t lock;
  30. unsigned long flags;
  31. struct platform_device *pdev;
  32. };
  33. /* Bits in uio_pdrv_genirq_platdata.flags */
  34. enum {
  35. UIO_IRQ_DISABLED = 0,
  36. };
  37. static int uio_pdrv_genirq_open(struct uio_info *info, struct inode *inode)
  38. {
  39. struct uio_pdrv_genirq_platdata *priv = info->priv;
  40. /* Wait until the Runtime PM code has woken up the device */
  41. pm_runtime_get_sync(&priv->pdev->dev);
  42. return 0;
  43. }
  44. static int uio_pdrv_genirq_release(struct uio_info *info, struct inode *inode)
  45. {
  46. struct uio_pdrv_genirq_platdata *priv = info->priv;
  47. /* Tell the Runtime PM code that the device has become idle */
  48. pm_runtime_put_sync(&priv->pdev->dev);
  49. return 0;
  50. }
  51. static irqreturn_t uio_pdrv_genirq_handler(int irq, struct uio_info *dev_info)
  52. {
  53. struct uio_pdrv_genirq_platdata *priv = dev_info->priv;
  54. /* Just disable the interrupt in the interrupt controller, and
  55. * remember the state so we can allow user space to enable it later.
  56. */
  57. spin_lock(&priv->lock);
  58. if (!__test_and_set_bit(UIO_IRQ_DISABLED, &priv->flags))
  59. disable_irq_nosync(irq);
  60. spin_unlock(&priv->lock);
  61. return IRQ_HANDLED;
  62. }
  63. static int uio_pdrv_genirq_irqcontrol(struct uio_info *dev_info, s32 irq_on)
  64. {
  65. struct uio_pdrv_genirq_platdata *priv = dev_info->priv;
  66. unsigned long flags;
  67. /* Allow user space to enable and disable the interrupt
  68. * in the interrupt controller, but keep track of the
  69. * state to prevent per-irq depth damage.
  70. *
  71. * Serialize this operation to support multiple tasks and concurrency
  72. * with irq handler on SMP systems.
  73. */
  74. spin_lock_irqsave(&priv->lock, flags);
  75. if (irq_on) {
  76. if (__test_and_clear_bit(UIO_IRQ_DISABLED, &priv->flags))
  77. enable_irq(dev_info->irq);
  78. } else {
  79. if (!__test_and_set_bit(UIO_IRQ_DISABLED, &priv->flags))
  80. disable_irq_nosync(dev_info->irq);
  81. }
  82. spin_unlock_irqrestore(&priv->lock, flags);
  83. return 0;
  84. }
  85. static void uio_pdrv_genirq_cleanup(void *data)
  86. {
  87. struct device *dev = data;
  88. pm_runtime_disable(dev);
  89. }
  90. static int uio_pdrv_genirq_probe(struct platform_device *pdev)
  91. {
  92. struct uio_info *uioinfo = dev_get_platdata(&pdev->dev);
  93. struct device_node *node = pdev->dev.of_node;
  94. struct uio_pdrv_genirq_platdata *priv;
  95. struct uio_mem *uiomem;
  96. int ret = -EINVAL;
  97. int i;
  98. if (node) {
  99. const char *name;
  100. /* alloc uioinfo for one device */
  101. uioinfo = devm_kzalloc(&pdev->dev, sizeof(*uioinfo),
  102. GFP_KERNEL);
  103. if (!uioinfo) {
  104. dev_err(&pdev->dev, "unable to kmalloc\n");
  105. return -ENOMEM;
  106. }
  107. if (!of_property_read_string(node, "linux,uio-name", &name))
  108. uioinfo->name = devm_kstrdup(&pdev->dev, name, GFP_KERNEL);
  109. else
  110. uioinfo->name = devm_kasprintf(&pdev->dev, GFP_KERNEL,
  111. "%pOFn", node);
  112. uioinfo->version = "devicetree";
  113. /* Multiple IRQs are not supported */
  114. }
  115. if (!uioinfo || !uioinfo->name || !uioinfo->version) {
  116. dev_err(&pdev->dev, "missing platform_data\n");
  117. return ret;
  118. }
  119. if (uioinfo->handler || uioinfo->irqcontrol ||
  120. uioinfo->irq_flags & IRQF_SHARED) {
  121. dev_err(&pdev->dev, "interrupt configuration error\n");
  122. return ret;
  123. }
  124. priv = devm_kzalloc(&pdev->dev, sizeof(*priv), GFP_KERNEL);
  125. if (!priv) {
  126. dev_err(&pdev->dev, "unable to kmalloc\n");
  127. return -ENOMEM;
  128. }
  129. priv->uioinfo = uioinfo;
  130. spin_lock_init(&priv->lock);
  131. priv->flags = 0; /* interrupt is enabled to begin with */
  132. priv->pdev = pdev;
  133. if (!uioinfo->irq) {
  134. ret = platform_get_irq_optional(pdev, 0);
  135. uioinfo->irq = ret;
  136. if (ret == -ENXIO)
  137. uioinfo->irq = UIO_IRQ_NONE;
  138. else if (ret == -EPROBE_DEFER)
  139. return ret;
  140. else if (ret < 0) {
  141. dev_err(&pdev->dev, "failed to get IRQ\n");
  142. return ret;
  143. }
  144. }
  145. if (uioinfo->irq) {
  146. struct irq_data *irq_data = irq_get_irq_data(uioinfo->irq);
  147. /*
  148. * If a level interrupt, dont do lazy disable. Otherwise the
  149. * irq will fire again since clearing of the actual cause, on
  150. * device level, is done in userspace
  151. * irqd_is_level_type() isn't used since isn't valid until
  152. * irq is configured.
  153. */
  154. if (irq_data &&
  155. irqd_get_trigger_type(irq_data) & IRQ_TYPE_LEVEL_MASK) {
  156. dev_dbg(&pdev->dev, "disable lazy unmask\n");
  157. irq_set_status_flags(uioinfo->irq, IRQ_DISABLE_UNLAZY);
  158. }
  159. }
  160. uiomem = &uioinfo->mem[0];
  161. for (i = 0; i < pdev->num_resources; ++i) {
  162. struct resource *r = &pdev->resource[i];
  163. if (r->flags != IORESOURCE_MEM)
  164. continue;
  165. if (uiomem >= &uioinfo->mem[MAX_UIO_MAPS]) {
  166. dev_warn(&pdev->dev, "device has more than "
  167. __stringify(MAX_UIO_MAPS)
  168. " I/O memory resources.\n");
  169. break;
  170. }
  171. uiomem->memtype = UIO_MEM_PHYS;
  172. uiomem->addr = r->start & PAGE_MASK;
  173. uiomem->offs = r->start & ~PAGE_MASK;
  174. uiomem->size = (uiomem->offs + resource_size(r)
  175. + PAGE_SIZE - 1) & PAGE_MASK;
  176. uiomem->name = r->name;
  177. ++uiomem;
  178. }
  179. while (uiomem < &uioinfo->mem[MAX_UIO_MAPS]) {
  180. uiomem->size = 0;
  181. ++uiomem;
  182. }
  183. /* This driver requires no hardware specific kernel code to handle
  184. * interrupts. Instead, the interrupt handler simply disables the
  185. * interrupt in the interrupt controller. User space is responsible
  186. * for performing hardware specific acknowledge and re-enabling of
  187. * the interrupt in the interrupt controller.
  188. *
  189. * Interrupt sharing is not supported.
  190. */
  191. uioinfo->handler = uio_pdrv_genirq_handler;
  192. uioinfo->irqcontrol = uio_pdrv_genirq_irqcontrol;
  193. uioinfo->open = uio_pdrv_genirq_open;
  194. uioinfo->release = uio_pdrv_genirq_release;
  195. uioinfo->priv = priv;
  196. /* Enable Runtime PM for this device:
  197. * The device starts in suspended state to allow the hardware to be
  198. * turned off by default. The Runtime PM bus code should power on the
  199. * hardware and enable clocks at open().
  200. */
  201. pm_runtime_enable(&pdev->dev);
  202. ret = devm_add_action_or_reset(&pdev->dev, uio_pdrv_genirq_cleanup,
  203. &pdev->dev);
  204. if (ret)
  205. return ret;
  206. ret = devm_uio_register_device(&pdev->dev, priv->uioinfo);
  207. if (ret)
  208. dev_err(&pdev->dev, "unable to register uio device\n");
  209. return ret;
  210. }
  211. static int uio_pdrv_genirq_runtime_nop(struct device *dev)
  212. {
  213. /* Runtime PM callback shared between ->runtime_suspend()
  214. * and ->runtime_resume(). Simply returns success.
  215. *
  216. * In this driver pm_runtime_get_sync() and pm_runtime_put_sync()
  217. * are used at open() and release() time. This allows the
  218. * Runtime PM code to turn off power to the device while the
  219. * device is unused, ie before open() and after release().
  220. *
  221. * This Runtime PM callback does not need to save or restore
  222. * any registers since user space is responsbile for hardware
  223. * register reinitialization after open().
  224. */
  225. return 0;
  226. }
  227. static const struct dev_pm_ops uio_pdrv_genirq_dev_pm_ops = {
  228. .runtime_suspend = uio_pdrv_genirq_runtime_nop,
  229. .runtime_resume = uio_pdrv_genirq_runtime_nop,
  230. };
  231. #ifdef CONFIG_OF
  232. static struct of_device_id uio_of_genirq_match[] = {
  233. { /* This is filled with module_parm */ },
  234. { /* Sentinel */ },
  235. };
  236. MODULE_DEVICE_TABLE(of, uio_of_genirq_match);
  237. module_param_string(of_id, uio_of_genirq_match[0].compatible, 128, 0);
  238. MODULE_PARM_DESC(of_id, "Openfirmware id of the device to be handled by uio");
  239. #endif
  240. static struct platform_driver uio_pdrv_genirq = {
  241. .probe = uio_pdrv_genirq_probe,
  242. .driver = {
  243. .name = DRIVER_NAME,
  244. .pm = &uio_pdrv_genirq_dev_pm_ops,
  245. .of_match_table = of_match_ptr(uio_of_genirq_match),
  246. },
  247. };
  248. module_platform_driver(uio_pdrv_genirq);
  249. MODULE_AUTHOR("Magnus Damm");
  250. MODULE_DESCRIPTION("Userspace I/O platform driver with generic IRQ handling");
  251. MODULE_LICENSE("GPL v2");
  252. MODULE_ALIAS("platform:" DRIVER_NAME);