irq_sim.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * Copyright (C) 2017 Bartosz Golaszewski <brgl@bgdev.pl>
  4. *
  5. * This program is free software; you can redistribute it and/or modify it
  6. * under the terms of the GNU General Public License as published by the
  7. * Free Software Foundation; either version 2 of the License, or (at your
  8. * option) any later version.
  9. */
  10. #include <linux/slab.h>
  11. #include <linux/irq_sim.h>
  12. #include <linux/irq.h>
  13. struct irq_sim_devres {
  14. struct irq_sim *sim;
  15. };
  16. static void irq_sim_irqmask(struct irq_data *data)
  17. {
  18. struct irq_sim_irq_ctx *irq_ctx = irq_data_get_irq_chip_data(data);
  19. irq_ctx->enabled = false;
  20. }
  21. static void irq_sim_irqunmask(struct irq_data *data)
  22. {
  23. struct irq_sim_irq_ctx *irq_ctx = irq_data_get_irq_chip_data(data);
  24. irq_ctx->enabled = true;
  25. }
  26. static struct irq_chip irq_sim_irqchip = {
  27. .name = "irq_sim",
  28. .irq_mask = irq_sim_irqmask,
  29. .irq_unmask = irq_sim_irqunmask,
  30. };
  31. static void irq_sim_handle_irq(struct irq_work *work)
  32. {
  33. struct irq_sim_work_ctx *work_ctx;
  34. work_ctx = container_of(work, struct irq_sim_work_ctx, work);
  35. handle_simple_irq(irq_to_desc(work_ctx->irq));
  36. }
  37. /**
  38. * irq_sim_init - Initialize the interrupt simulator: allocate a range of
  39. * dummy interrupts.
  40. *
  41. * @sim: The interrupt simulator object to initialize.
  42. * @num_irqs: Number of interrupts to allocate
  43. *
  44. * On success: return the base of the allocated interrupt range.
  45. * On failure: a negative errno.
  46. */
  47. int irq_sim_init(struct irq_sim *sim, unsigned int num_irqs)
  48. {
  49. int i;
  50. sim->irqs = kmalloc_array(num_irqs, sizeof(*sim->irqs), GFP_KERNEL);
  51. if (!sim->irqs)
  52. return -ENOMEM;
  53. sim->irq_base = irq_alloc_descs(-1, 0, num_irqs, 0);
  54. if (sim->irq_base < 0) {
  55. kfree(sim->irqs);
  56. return sim->irq_base;
  57. }
  58. for (i = 0; i < num_irqs; i++) {
  59. sim->irqs[i].irqnum = sim->irq_base + i;
  60. sim->irqs[i].enabled = false;
  61. irq_set_chip(sim->irq_base + i, &irq_sim_irqchip);
  62. irq_set_chip_data(sim->irq_base + i, &sim->irqs[i]);
  63. irq_set_handler(sim->irq_base + i, &handle_simple_irq);
  64. irq_modify_status(sim->irq_base + i,
  65. IRQ_NOREQUEST | IRQ_NOAUTOEN, IRQ_NOPROBE);
  66. }
  67. init_irq_work(&sim->work_ctx.work, irq_sim_handle_irq);
  68. sim->irq_count = num_irqs;
  69. return sim->irq_base;
  70. }
  71. EXPORT_SYMBOL_GPL(irq_sim_init);
  72. /**
  73. * irq_sim_fini - Deinitialize the interrupt simulator: free the interrupt
  74. * descriptors and allocated memory.
  75. *
  76. * @sim: The interrupt simulator to tear down.
  77. */
  78. void irq_sim_fini(struct irq_sim *sim)
  79. {
  80. irq_work_sync(&sim->work_ctx.work);
  81. irq_free_descs(sim->irq_base, sim->irq_count);
  82. kfree(sim->irqs);
  83. }
  84. EXPORT_SYMBOL_GPL(irq_sim_fini);
  85. static void devm_irq_sim_release(struct device *dev, void *res)
  86. {
  87. struct irq_sim_devres *this = res;
  88. irq_sim_fini(this->sim);
  89. }
  90. /**
  91. * irq_sim_init - Initialize the interrupt simulator for a managed device.
  92. *
  93. * @dev: Device to initialize the simulator object for.
  94. * @sim: The interrupt simulator object to initialize.
  95. * @num_irqs: Number of interrupts to allocate
  96. *
  97. * On success: return the base of the allocated interrupt range.
  98. * On failure: a negative errno.
  99. */
  100. int devm_irq_sim_init(struct device *dev, struct irq_sim *sim,
  101. unsigned int num_irqs)
  102. {
  103. struct irq_sim_devres *dr;
  104. int rv;
  105. dr = devres_alloc(devm_irq_sim_release, sizeof(*dr), GFP_KERNEL);
  106. if (!dr)
  107. return -ENOMEM;
  108. rv = irq_sim_init(sim, num_irqs);
  109. if (rv < 0) {
  110. devres_free(dr);
  111. return rv;
  112. }
  113. dr->sim = sim;
  114. devres_add(dev, dr);
  115. return rv;
  116. }
  117. EXPORT_SYMBOL_GPL(devm_irq_sim_init);
  118. /**
  119. * irq_sim_fire - Enqueue an interrupt.
  120. *
  121. * @sim: The interrupt simulator object.
  122. * @offset: Offset of the simulated interrupt which should be fired.
  123. */
  124. void irq_sim_fire(struct irq_sim *sim, unsigned int offset)
  125. {
  126. if (sim->irqs[offset].enabled) {
  127. sim->work_ctx.irq = irq_sim_irqnum(sim, offset);
  128. irq_work_queue(&sim->work_ctx.work);
  129. }
  130. }
  131. EXPORT_SYMBOL_GPL(irq_sim_fire);
  132. /**
  133. * irq_sim_irqnum - Get the allocated number of a dummy interrupt.
  134. *
  135. * @sim: The interrupt simulator object.
  136. * @offset: Offset of the simulated interrupt for which to retrieve
  137. * the number.
  138. */
  139. int irq_sim_irqnum(struct irq_sim *sim, unsigned int offset)
  140. {
  141. return sim->irqs[offset].irqnum;
  142. }
  143. EXPORT_SYMBOL_GPL(irq_sim_irqnum);