uio_dmem_genirq.c 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * drivers/uio/uio_dmem_genirq.c
  4. *
  5. * Userspace I/O platform driver with generic IRQ handling code.
  6. *
  7. * Copyright (C) 2012 Damian Hobson-Garcia
  8. *
  9. * Based on uio_pdrv_genirq.c by Magnus Damm
  10. */
  11. #include <linux/platform_device.h>
  12. #include <linux/uio_driver.h>
  13. #include <linux/spinlock.h>
  14. #include <linux/bitops.h>
  15. #include <linux/module.h>
  16. #include <linux/interrupt.h>
  17. #include <linux/platform_data/uio_dmem_genirq.h>
  18. #include <linux/stringify.h>
  19. #include <linux/pm_runtime.h>
  20. #include <linux/dma-mapping.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_dmem_genirq"
  27. #define DMEM_MAP_ERROR (~0)
  28. struct uio_dmem_genirq_platdata {
  29. struct uio_info *uioinfo;
  30. spinlock_t lock;
  31. unsigned long flags;
  32. struct platform_device *pdev;
  33. unsigned int dmem_region_start;
  34. unsigned int num_dmem_regions;
  35. void *dmem_region_vaddr[MAX_UIO_MAPS];
  36. struct mutex alloc_lock;
  37. unsigned int refcnt;
  38. };
  39. static int uio_dmem_genirq_open(struct uio_info *info, struct inode *inode)
  40. {
  41. struct uio_dmem_genirq_platdata *priv = info->priv;
  42. struct uio_mem *uiomem;
  43. int dmem_region = priv->dmem_region_start;
  44. uiomem = &priv->uioinfo->mem[priv->dmem_region_start];
  45. mutex_lock(&priv->alloc_lock);
  46. while (!priv->refcnt && uiomem < &priv->uioinfo->mem[MAX_UIO_MAPS]) {
  47. void *addr;
  48. if (!uiomem->size)
  49. break;
  50. addr = dma_alloc_coherent(&priv->pdev->dev, uiomem->size,
  51. (dma_addr_t *)&uiomem->addr, GFP_KERNEL);
  52. if (!addr) {
  53. uiomem->addr = DMEM_MAP_ERROR;
  54. }
  55. priv->dmem_region_vaddr[dmem_region++] = addr;
  56. ++uiomem;
  57. }
  58. priv->refcnt++;
  59. mutex_unlock(&priv->alloc_lock);
  60. /* Wait until the Runtime PM code has woken up the device */
  61. pm_runtime_get_sync(&priv->pdev->dev);
  62. return 0;
  63. }
  64. static int uio_dmem_genirq_release(struct uio_info *info, struct inode *inode)
  65. {
  66. struct uio_dmem_genirq_platdata *priv = info->priv;
  67. struct uio_mem *uiomem;
  68. int dmem_region = priv->dmem_region_start;
  69. /* Tell the Runtime PM code that the device has become idle */
  70. pm_runtime_put_sync(&priv->pdev->dev);
  71. uiomem = &priv->uioinfo->mem[priv->dmem_region_start];
  72. mutex_lock(&priv->alloc_lock);
  73. priv->refcnt--;
  74. while (!priv->refcnt && uiomem < &priv->uioinfo->mem[MAX_UIO_MAPS]) {
  75. if (!uiomem->size)
  76. break;
  77. if (priv->dmem_region_vaddr[dmem_region]) {
  78. dma_free_coherent(&priv->pdev->dev, uiomem->size,
  79. priv->dmem_region_vaddr[dmem_region],
  80. uiomem->addr);
  81. }
  82. uiomem->addr = DMEM_MAP_ERROR;
  83. ++dmem_region;
  84. ++uiomem;
  85. }
  86. mutex_unlock(&priv->alloc_lock);
  87. return 0;
  88. }
  89. static irqreturn_t uio_dmem_genirq_handler(int irq, struct uio_info *dev_info)
  90. {
  91. struct uio_dmem_genirq_platdata *priv = dev_info->priv;
  92. /* Just disable the interrupt in the interrupt controller, and
  93. * remember the state so we can allow user space to enable it later.
  94. */
  95. spin_lock(&priv->lock);
  96. if (!test_and_set_bit(0, &priv->flags))
  97. disable_irq_nosync(irq);
  98. spin_unlock(&priv->lock);
  99. return IRQ_HANDLED;
  100. }
  101. static int uio_dmem_genirq_irqcontrol(struct uio_info *dev_info, s32 irq_on)
  102. {
  103. struct uio_dmem_genirq_platdata *priv = dev_info->priv;
  104. unsigned long flags;
  105. /* Allow user space to enable and disable the interrupt
  106. * in the interrupt controller, but keep track of the
  107. * state to prevent per-irq depth damage.
  108. *
  109. * Serialize this operation to support multiple tasks and concurrency
  110. * with irq handler on SMP systems.
  111. */
  112. spin_lock_irqsave(&priv->lock, flags);
  113. if (irq_on) {
  114. if (test_and_clear_bit(0, &priv->flags))
  115. enable_irq(dev_info->irq);
  116. } else {
  117. if (!test_and_set_bit(0, &priv->flags))
  118. disable_irq_nosync(dev_info->irq);
  119. }
  120. spin_unlock_irqrestore(&priv->lock, flags);
  121. return 0;
  122. }
  123. static void uio_dmem_genirq_pm_disable(void *data)
  124. {
  125. struct device *dev = data;
  126. pm_runtime_disable(dev);
  127. }
  128. static int uio_dmem_genirq_probe(struct platform_device *pdev)
  129. {
  130. struct uio_dmem_genirq_pdata *pdata = dev_get_platdata(&pdev->dev);
  131. struct uio_info *uioinfo = &pdata->uioinfo;
  132. struct uio_dmem_genirq_platdata *priv;
  133. struct uio_mem *uiomem;
  134. int ret = -EINVAL;
  135. int i;
  136. if (pdev->dev.of_node) {
  137. /* alloc uioinfo for one device */
  138. uioinfo = devm_kzalloc(&pdev->dev, sizeof(*uioinfo), GFP_KERNEL);
  139. if (!uioinfo) {
  140. dev_err(&pdev->dev, "unable to kmalloc\n");
  141. return -ENOMEM;
  142. }
  143. uioinfo->name = devm_kasprintf(&pdev->dev, GFP_KERNEL, "%pOFn",
  144. pdev->dev.of_node);
  145. uioinfo->version = "devicetree";
  146. }
  147. if (!uioinfo || !uioinfo->name || !uioinfo->version) {
  148. dev_err(&pdev->dev, "missing platform_data\n");
  149. return -EINVAL;
  150. }
  151. if (uioinfo->handler || uioinfo->irqcontrol ||
  152. uioinfo->irq_flags & IRQF_SHARED) {
  153. dev_err(&pdev->dev, "interrupt configuration error\n");
  154. return -EINVAL;
  155. }
  156. priv = devm_kzalloc(&pdev->dev, sizeof(*priv), GFP_KERNEL);
  157. if (!priv) {
  158. dev_err(&pdev->dev, "unable to kmalloc\n");
  159. return -ENOMEM;
  160. }
  161. ret = dma_set_coherent_mask(&pdev->dev, DMA_BIT_MASK(32));
  162. if (ret) {
  163. dev_err(&pdev->dev, "DMA enable failed\n");
  164. return ret;
  165. }
  166. priv->uioinfo = uioinfo;
  167. spin_lock_init(&priv->lock);
  168. priv->flags = 0; /* interrupt is enabled to begin with */
  169. priv->pdev = pdev;
  170. mutex_init(&priv->alloc_lock);
  171. if (!uioinfo->irq) {
  172. /* Multiple IRQs are not supported */
  173. ret = platform_get_irq(pdev, 0);
  174. if (ret == -ENXIO && pdev->dev.of_node)
  175. ret = UIO_IRQ_NONE;
  176. else if (ret < 0)
  177. return ret;
  178. uioinfo->irq = ret;
  179. }
  180. if (uioinfo->irq) {
  181. struct irq_data *irq_data = irq_get_irq_data(uioinfo->irq);
  182. /*
  183. * If a level interrupt, dont do lazy disable. Otherwise the
  184. * irq will fire again since clearing of the actual cause, on
  185. * device level, is done in userspace
  186. * irqd_is_level_type() isn't used since isn't valid until
  187. * irq is configured.
  188. */
  189. if (irq_data &&
  190. irqd_get_trigger_type(irq_data) & IRQ_TYPE_LEVEL_MASK) {
  191. dev_dbg(&pdev->dev, "disable lazy unmask\n");
  192. irq_set_status_flags(uioinfo->irq, IRQ_DISABLE_UNLAZY);
  193. }
  194. }
  195. uiomem = &uioinfo->mem[0];
  196. for (i = 0; i < pdev->num_resources; ++i) {
  197. struct resource *r = &pdev->resource[i];
  198. if (r->flags != IORESOURCE_MEM)
  199. continue;
  200. if (uiomem >= &uioinfo->mem[MAX_UIO_MAPS]) {
  201. dev_warn(&pdev->dev, "device has more than "
  202. __stringify(MAX_UIO_MAPS)
  203. " I/O memory resources.\n");
  204. break;
  205. }
  206. uiomem->memtype = UIO_MEM_PHYS;
  207. uiomem->addr = r->start;
  208. uiomem->size = resource_size(r);
  209. ++uiomem;
  210. }
  211. priv->dmem_region_start = uiomem - &uioinfo->mem[0];
  212. priv->num_dmem_regions = pdata->num_dynamic_regions;
  213. for (i = 0; i < pdata->num_dynamic_regions; ++i) {
  214. if (uiomem >= &uioinfo->mem[MAX_UIO_MAPS]) {
  215. dev_warn(&pdev->dev, "device has more than "
  216. __stringify(MAX_UIO_MAPS)
  217. " dynamic and fixed memory regions.\n");
  218. break;
  219. }
  220. uiomem->memtype = UIO_MEM_PHYS;
  221. uiomem->addr = DMEM_MAP_ERROR;
  222. uiomem->size = pdata->dynamic_region_sizes[i];
  223. ++uiomem;
  224. }
  225. while (uiomem < &uioinfo->mem[MAX_UIO_MAPS]) {
  226. uiomem->size = 0;
  227. ++uiomem;
  228. }
  229. /* This driver requires no hardware specific kernel code to handle
  230. * interrupts. Instead, the interrupt handler simply disables the
  231. * interrupt in the interrupt controller. User space is responsible
  232. * for performing hardware specific acknowledge and re-enabling of
  233. * the interrupt in the interrupt controller.
  234. *
  235. * Interrupt sharing is not supported.
  236. */
  237. uioinfo->handler = uio_dmem_genirq_handler;
  238. uioinfo->irqcontrol = uio_dmem_genirq_irqcontrol;
  239. uioinfo->open = uio_dmem_genirq_open;
  240. uioinfo->release = uio_dmem_genirq_release;
  241. uioinfo->priv = priv;
  242. /* Enable Runtime PM for this device:
  243. * The device starts in suspended state to allow the hardware to be
  244. * turned off by default. The Runtime PM bus code should power on the
  245. * hardware and enable clocks at open().
  246. */
  247. pm_runtime_enable(&pdev->dev);
  248. ret = devm_add_action_or_reset(&pdev->dev, uio_dmem_genirq_pm_disable, &pdev->dev);
  249. if (ret)
  250. return ret;
  251. return devm_uio_register_device(&pdev->dev, priv->uioinfo);
  252. }
  253. static int uio_dmem_genirq_runtime_nop(struct device *dev)
  254. {
  255. /* Runtime PM callback shared between ->runtime_suspend()
  256. * and ->runtime_resume(). Simply returns success.
  257. *
  258. * In this driver pm_runtime_get_sync() and pm_runtime_put_sync()
  259. * are used at open() and release() time. This allows the
  260. * Runtime PM code to turn off power to the device while the
  261. * device is unused, ie before open() and after release().
  262. *
  263. * This Runtime PM callback does not need to save or restore
  264. * any registers since user space is responsbile for hardware
  265. * register reinitialization after open().
  266. */
  267. return 0;
  268. }
  269. static const struct dev_pm_ops uio_dmem_genirq_dev_pm_ops = {
  270. .runtime_suspend = uio_dmem_genirq_runtime_nop,
  271. .runtime_resume = uio_dmem_genirq_runtime_nop,
  272. };
  273. #ifdef CONFIG_OF
  274. static const struct of_device_id uio_of_genirq_match[] = {
  275. { /* empty for now */ },
  276. };
  277. MODULE_DEVICE_TABLE(of, uio_of_genirq_match);
  278. #endif
  279. static struct platform_driver uio_dmem_genirq = {
  280. .probe = uio_dmem_genirq_probe,
  281. .driver = {
  282. .name = DRIVER_NAME,
  283. .pm = &uio_dmem_genirq_dev_pm_ops,
  284. .of_match_table = of_match_ptr(uio_of_genirq_match),
  285. },
  286. };
  287. module_platform_driver(uio_dmem_genirq);
  288. MODULE_AUTHOR("Damian Hobson-Garcia");
  289. MODULE_DESCRIPTION("Userspace I/O platform driver with dynamic memory.");
  290. MODULE_LICENSE("GPL v2");
  291. MODULE_ALIAS("platform:" DRIVER_NAME);