irq-ompic.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. /*
  2. * Open Multi-Processor Interrupt Controller driver
  3. *
  4. * Copyright (C) 2014 Stefan Kristiansson <stefan.kristiansson@saunalahti.fi>
  5. * Copyright (C) 2017 Stafford Horne <shorne@gmail.com>
  6. *
  7. * This file is licensed under the terms of the GNU General Public License
  8. * version 2. This program is licensed "as is" without any warranty of any
  9. * kind, whether express or implied.
  10. *
  11. * The ompic device handles IPI communication between cores in multi-core
  12. * OpenRISC systems.
  13. *
  14. * Registers
  15. *
  16. * For each CPU the ompic has 2 registers. The control register for sending
  17. * and acking IPIs and the status register for receiving IPIs. The register
  18. * layouts are as follows:
  19. *
  20. * Control register
  21. * +---------+---------+----------+---------+
  22. * | 31 | 30 | 29 .. 16 | 15 .. 0 |
  23. * ----------+---------+----------+----------
  24. * | IRQ ACK | IRQ GEN | DST CORE | DATA |
  25. * +---------+---------+----------+---------+
  26. *
  27. * Status register
  28. * +----------+-------------+----------+---------+
  29. * | 31 | 30 | 29 .. 16 | 15 .. 0 |
  30. * -----------+-------------+----------+---------+
  31. * | Reserved | IRQ Pending | SRC CORE | DATA |
  32. * +----------+-------------+----------+---------+
  33. *
  34. * Architecture
  35. *
  36. * - The ompic generates a level interrupt to the CPU PIC when a message is
  37. * ready. Messages are delivered via the memory bus.
  38. * - The ompic does not have any interrupt input lines.
  39. * - The ompic is wired to the same irq line on each core.
  40. * - Devices are wired to the same irq line on each core.
  41. *
  42. * +---------+ +---------+
  43. * | CPU | | CPU |
  44. * | Core 0 |<==\ (memory access) /==>| Core 1 |
  45. * | [ PIC ]| | | | [ PIC ]|
  46. * +----^-^--+ | | +----^-^--+
  47. * | | v v | |
  48. * <====|=|=================================|=|==> (memory bus)
  49. * | | ^ ^ | |
  50. * (ipi | +------|---------+--------|-------|-+ (device irq)
  51. * irq | | | | |
  52. * core0)| +------|---------|--------|-------+ (ipi irq core1)
  53. * | | | | |
  54. * +----o-o-+ | +--------+ |
  55. * | ompic |<===/ | Device |<===/
  56. * | IPI | +--------+
  57. * +--------+*
  58. *
  59. */
  60. #include <linux/io.h>
  61. #include <linux/ioport.h>
  62. #include <linux/interrupt.h>
  63. #include <linux/smp.h>
  64. #include <linux/of.h>
  65. #include <linux/of_irq.h>
  66. #include <linux/of_address.h>
  67. #include <linux/irqchip.h>
  68. #define OMPIC_CPUBYTES 8
  69. #define OMPIC_CTRL(cpu) (0x0 + (cpu * OMPIC_CPUBYTES))
  70. #define OMPIC_STAT(cpu) (0x4 + (cpu * OMPIC_CPUBYTES))
  71. #define OMPIC_CTRL_IRQ_ACK (1 << 31)
  72. #define OMPIC_CTRL_IRQ_GEN (1 << 30)
  73. #define OMPIC_CTRL_DST(cpu) (((cpu) & 0x3fff) << 16)
  74. #define OMPIC_STAT_IRQ_PENDING (1 << 30)
  75. #define OMPIC_DATA(x) ((x) & 0xffff)
  76. DEFINE_PER_CPU(unsigned long, ops);
  77. static void __iomem *ompic_base;
  78. static DEFINE_PER_CPU_READ_MOSTLY(int, ipi_dummy_dev);
  79. static inline u32 ompic_readreg(void __iomem *base, loff_t offset)
  80. {
  81. return ioread32be(base + offset);
  82. }
  83. static void ompic_writereg(void __iomem *base, loff_t offset, u32 data)
  84. {
  85. iowrite32be(data, base + offset);
  86. }
  87. static void ompic_raise_softirq(const struct cpumask *mask,
  88. unsigned int ipi_msg)
  89. {
  90. unsigned int dst_cpu;
  91. unsigned int src_cpu = smp_processor_id();
  92. for_each_cpu(dst_cpu, mask) {
  93. set_bit(ipi_msg, &per_cpu(ops, dst_cpu));
  94. /*
  95. * On OpenRISC the atomic set_bit() call implies a memory
  96. * barrier. Otherwise we would need: smp_wmb(); paired
  97. * with the read in ompic_ipi_handler.
  98. */
  99. ompic_writereg(ompic_base, OMPIC_CTRL(src_cpu),
  100. OMPIC_CTRL_IRQ_GEN |
  101. OMPIC_CTRL_DST(dst_cpu) |
  102. OMPIC_DATA(1));
  103. }
  104. }
  105. static irqreturn_t ompic_ipi_handler(int irq, void *dev_id)
  106. {
  107. unsigned int cpu = smp_processor_id();
  108. unsigned long *pending_ops = &per_cpu(ops, cpu);
  109. unsigned long ops;
  110. ompic_writereg(ompic_base, OMPIC_CTRL(cpu), OMPIC_CTRL_IRQ_ACK);
  111. while ((ops = xchg(pending_ops, 0)) != 0) {
  112. /*
  113. * On OpenRISC the atomic xchg() call implies a memory
  114. * barrier. Otherwise we may need an smp_rmb(); paired
  115. * with the write in ompic_raise_softirq.
  116. */
  117. do {
  118. unsigned long ipi_msg;
  119. ipi_msg = __ffs(ops);
  120. ops &= ~(1UL << ipi_msg);
  121. handle_IPI(ipi_msg);
  122. } while (ops);
  123. }
  124. return IRQ_HANDLED;
  125. }
  126. static int __init ompic_of_init(struct device_node *node,
  127. struct device_node *parent)
  128. {
  129. struct resource res;
  130. int irq;
  131. int ret;
  132. /* Validate the DT */
  133. if (ompic_base) {
  134. pr_err("ompic: duplicate ompic's are not supported");
  135. return -EEXIST;
  136. }
  137. if (of_address_to_resource(node, 0, &res)) {
  138. pr_err("ompic: reg property requires an address and size");
  139. return -EINVAL;
  140. }
  141. if (resource_size(&res) < (num_possible_cpus() * OMPIC_CPUBYTES)) {
  142. pr_err("ompic: reg size, currently %d must be at least %d",
  143. resource_size(&res),
  144. (num_possible_cpus() * OMPIC_CPUBYTES));
  145. return -EINVAL;
  146. }
  147. /* Setup the device */
  148. ompic_base = ioremap(res.start, resource_size(&res));
  149. if (!ompic_base) {
  150. pr_err("ompic: unable to map registers");
  151. return -ENOMEM;
  152. }
  153. irq = irq_of_parse_and_map(node, 0);
  154. if (irq <= 0) {
  155. pr_err("ompic: unable to parse device irq");
  156. ret = -EINVAL;
  157. goto out_unmap;
  158. }
  159. irq_set_percpu_devid(irq);
  160. ret = request_percpu_irq(irq, ompic_ipi_handler, "ompic_ipi",
  161. &ipi_dummy_dev);
  162. if (ret) {
  163. pr_err("ompic: failed to request irq %d, error: %d",
  164. irq, ret);
  165. goto out_irq_disp;
  166. }
  167. set_smp_cross_call(ompic_raise_softirq, irq);
  168. return 0;
  169. out_irq_disp:
  170. irq_dispose_mapping(irq);
  171. out_unmap:
  172. iounmap(ompic_base);
  173. ompic_base = NULL;
  174. return ret;
  175. }
  176. IRQCHIP_DECLARE(ompic, "openrisc,ompic", ompic_of_init);