icp-native.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  1. /*
  2. * Copyright 2011 IBM Corporation.
  3. *
  4. * This program is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU General Public License
  6. * as published by the Free Software Foundation; either version
  7. * 2 of the License, or (at your option) any later version.
  8. *
  9. */
  10. #include <linux/types.h>
  11. #include <linux/kernel.h>
  12. #include <linux/irq.h>
  13. #include <linux/smp.h>
  14. #include <linux/interrupt.h>
  15. #include <linux/init.h>
  16. #include <linux/cpu.h>
  17. #include <linux/of.h>
  18. #include <linux/spinlock.h>
  19. #include <asm/prom.h>
  20. #include <asm/io.h>
  21. #include <asm/smp.h>
  22. #include <asm/irq.h>
  23. #include <asm/errno.h>
  24. #include <asm/xics.h>
  25. struct icp_ipl {
  26. union {
  27. u32 word;
  28. u8 bytes[4];
  29. } xirr_poll;
  30. union {
  31. u32 word;
  32. u8 bytes[4];
  33. } xirr;
  34. u32 dummy;
  35. union {
  36. u32 word;
  37. u8 bytes[4];
  38. } qirr;
  39. u32 link_a;
  40. u32 link_b;
  41. u32 link_c;
  42. };
  43. static struct icp_ipl __iomem *icp_native_regs[NR_CPUS];
  44. static inline unsigned int icp_native_get_xirr(void)
  45. {
  46. int cpu = smp_processor_id();
  47. return in_be32(&icp_native_regs[cpu]->xirr.word);
  48. }
  49. static inline void icp_native_set_xirr(unsigned int value)
  50. {
  51. int cpu = smp_processor_id();
  52. out_be32(&icp_native_regs[cpu]->xirr.word, value);
  53. }
  54. static inline void icp_native_set_cppr(u8 value)
  55. {
  56. int cpu = smp_processor_id();
  57. out_8(&icp_native_regs[cpu]->xirr.bytes[0], value);
  58. }
  59. static inline void icp_native_set_qirr(int n_cpu, u8 value)
  60. {
  61. out_8(&icp_native_regs[n_cpu]->qirr.bytes[0], value);
  62. }
  63. static void icp_native_set_cpu_priority(unsigned char cppr)
  64. {
  65. xics_set_base_cppr(cppr);
  66. icp_native_set_cppr(cppr);
  67. iosync();
  68. }
  69. static void icp_native_eoi(struct irq_data *d)
  70. {
  71. unsigned int hw_irq = (unsigned int)irqd_to_hwirq(d);
  72. iosync();
  73. icp_native_set_xirr((xics_pop_cppr() << 24) | hw_irq);
  74. }
  75. static void icp_native_teardown_cpu(void)
  76. {
  77. int cpu = smp_processor_id();
  78. /* Clear any pending IPI */
  79. icp_native_set_qirr(cpu, 0xff);
  80. }
  81. static void icp_native_flush_ipi(void)
  82. {
  83. /* We take the ipi irq but and never return so we
  84. * need to EOI the IPI, but want to leave our priority 0
  85. *
  86. * should we check all the other interrupts too?
  87. * should we be flagging idle loop instead?
  88. * or creating some task to be scheduled?
  89. */
  90. icp_native_set_xirr((0x00 << 24) | XICS_IPI);
  91. }
  92. static unsigned int icp_native_get_irq(void)
  93. {
  94. unsigned int xirr = icp_native_get_xirr();
  95. unsigned int vec = xirr & 0x00ffffff;
  96. unsigned int irq;
  97. if (vec == XICS_IRQ_SPURIOUS)
  98. return NO_IRQ;
  99. irq = irq_radix_revmap_lookup(xics_host, vec);
  100. if (likely(irq != NO_IRQ)) {
  101. xics_push_cppr(vec);
  102. return irq;
  103. }
  104. /* We don't have a linux mapping, so have rtas mask it. */
  105. xics_mask_unknown_vec(vec);
  106. /* We might learn about it later, so EOI it */
  107. icp_native_set_xirr(xirr);
  108. return NO_IRQ;
  109. }
  110. #ifdef CONFIG_SMP
  111. static inline void icp_native_do_message(int cpu, int msg)
  112. {
  113. unsigned long *tgt = &per_cpu(xics_ipi_message, cpu);
  114. set_bit(msg, tgt);
  115. mb();
  116. icp_native_set_qirr(cpu, IPI_PRIORITY);
  117. }
  118. static void icp_native_message_pass(int target, int msg)
  119. {
  120. unsigned int i;
  121. if (target < NR_CPUS) {
  122. icp_native_do_message(target, msg);
  123. } else {
  124. for_each_online_cpu(i) {
  125. if (target == MSG_ALL_BUT_SELF
  126. && i == smp_processor_id())
  127. continue;
  128. icp_native_do_message(i, msg);
  129. }
  130. }
  131. }
  132. static irqreturn_t icp_native_ipi_action(int irq, void *dev_id)
  133. {
  134. int cpu = smp_processor_id();
  135. icp_native_set_qirr(cpu, 0xff);
  136. return xics_ipi_dispatch(cpu);
  137. }
  138. #endif /* CONFIG_SMP */
  139. static int __init icp_native_map_one_cpu(int hw_id, unsigned long addr,
  140. unsigned long size)
  141. {
  142. char *rname;
  143. int i, cpu = -1;
  144. /* This may look gross but it's good enough for now, we don't quite
  145. * have a hard -> linux processor id matching.
  146. */
  147. for_each_possible_cpu(i) {
  148. if (!cpu_present(i))
  149. continue;
  150. if (hw_id == get_hard_smp_processor_id(i)) {
  151. cpu = i;
  152. break;
  153. }
  154. }
  155. /* Fail, skip that CPU. Don't print, it's normal, some XICS come up
  156. * with way more entries in there than you have CPUs
  157. */
  158. if (cpu == -1)
  159. return 0;
  160. rname = kasprintf(GFP_KERNEL, "CPU %d [0x%x] Interrupt Presentation",
  161. cpu, hw_id);
  162. if (!request_mem_region(addr, size, rname)) {
  163. pr_warning("icp_native: Could not reserve ICP MMIO"
  164. " for CPU %d, interrupt server #0x%x\n",
  165. cpu, hw_id);
  166. return -EBUSY;
  167. }
  168. icp_native_regs[cpu] = ioremap(addr, size);
  169. if (!icp_native_regs[cpu]) {
  170. pr_warning("icp_native: Failed ioremap for CPU %d, "
  171. "interrupt server #0x%x, addr %#lx\n",
  172. cpu, hw_id, addr);
  173. release_mem_region(addr, size);
  174. return -ENOMEM;
  175. }
  176. return 0;
  177. }
  178. static int __init icp_native_init_one_node(struct device_node *np,
  179. unsigned int *indx)
  180. {
  181. unsigned int ilen;
  182. const u32 *ireg;
  183. int i;
  184. int reg_tuple_size;
  185. int num_servers = 0;
  186. /* This code does the theorically broken assumption that the interrupt
  187. * server numbers are the same as the hard CPU numbers.
  188. * This happens to be the case so far but we are playing with fire...
  189. * should be fixed one of these days. -BenH.
  190. */
  191. ireg = of_get_property(np, "ibm,interrupt-server-ranges", &ilen);
  192. /* Do that ever happen ? we'll know soon enough... but even good'old
  193. * f80 does have that property ..
  194. */
  195. WARN_ON((ireg == NULL) || (ilen != 2*sizeof(u32)));
  196. if (ireg) {
  197. *indx = of_read_number(ireg, 1);
  198. if (ilen >= 2*sizeof(u32))
  199. num_servers = of_read_number(ireg + 1, 1);
  200. }
  201. ireg = of_get_property(np, "reg", &ilen);
  202. if (!ireg) {
  203. pr_err("icp_native: Can't find interrupt reg property");
  204. return -1;
  205. }
  206. reg_tuple_size = (of_n_addr_cells(np) + of_n_size_cells(np)) * 4;
  207. if (((ilen % reg_tuple_size) != 0)
  208. || (num_servers && (num_servers != (ilen / reg_tuple_size)))) {
  209. pr_err("icp_native: ICP reg len (%d) != num servers (%d)",
  210. ilen / reg_tuple_size, num_servers);
  211. return -1;
  212. }
  213. for (i = 0; i < (ilen / reg_tuple_size); i++) {
  214. struct resource r;
  215. int err;
  216. err = of_address_to_resource(np, i, &r);
  217. if (err) {
  218. pr_err("icp_native: Could not translate ICP MMIO"
  219. " for interrupt server 0x%x (%d)\n", *indx, err);
  220. return -1;
  221. }
  222. if (icp_native_map_one_cpu(*indx, r.start, r.end - r.start))
  223. return -1;
  224. (*indx)++;
  225. }
  226. return 0;
  227. }
  228. static const struct icp_ops icp_native_ops = {
  229. .get_irq = icp_native_get_irq,
  230. .eoi = icp_native_eoi,
  231. .set_priority = icp_native_set_cpu_priority,
  232. .teardown_cpu = icp_native_teardown_cpu,
  233. .flush_ipi = icp_native_flush_ipi,
  234. #ifdef CONFIG_SMP
  235. .ipi_action = icp_native_ipi_action,
  236. .message_pass = icp_native_message_pass,
  237. #endif
  238. };
  239. int icp_native_init(void)
  240. {
  241. struct device_node *np;
  242. u32 indx = 0;
  243. int found = 0;
  244. for_each_compatible_node(np, NULL, "ibm,ppc-xicp")
  245. if (icp_native_init_one_node(np, &indx) == 0)
  246. found = 1;
  247. if (!found) {
  248. for_each_node_by_type(np,
  249. "PowerPC-External-Interrupt-Presentation") {
  250. if (icp_native_init_one_node(np, &indx) == 0)
  251. found = 1;
  252. }
  253. }
  254. if (found == 0)
  255. return -ENODEV;
  256. icp_ops = &icp_native_ops;
  257. return 0;
  258. }