interrupt.c 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373
  1. /*
  2. * Cell Internal Interrupt Controller
  3. *
  4. * (C) Copyright IBM Deutschland Entwicklung GmbH 2005
  5. *
  6. * Author: Arnd Bergmann <arndb@de.ibm.com>
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation; either version 2, or (at your option)
  11. * any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program; if not, write to the Free Software
  20. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  21. */
  22. #include <linux/config.h>
  23. #include <linux/interrupt.h>
  24. #include <linux/irq.h>
  25. #include <linux/module.h>
  26. #include <linux/percpu.h>
  27. #include <linux/types.h>
  28. #include <asm/io.h>
  29. #include <asm/pgtable.h>
  30. #include <asm/prom.h>
  31. #include <asm/ptrace.h>
  32. #include "interrupt.h"
  33. struct iic_pending_bits {
  34. u32 data;
  35. u8 flags;
  36. u8 class;
  37. u8 source;
  38. u8 prio;
  39. };
  40. enum iic_pending_flags {
  41. IIC_VALID = 0x80,
  42. IIC_IPI = 0x40,
  43. };
  44. struct iic_regs {
  45. struct iic_pending_bits pending;
  46. struct iic_pending_bits pending_destr;
  47. u64 generate;
  48. u64 prio;
  49. };
  50. struct iic {
  51. struct iic_regs __iomem *regs;
  52. u8 target_id;
  53. };
  54. static DEFINE_PER_CPU(struct iic, iic);
  55. void iic_local_enable(void)
  56. {
  57. struct iic *iic = &__get_cpu_var(iic);
  58. u64 tmp;
  59. /*
  60. * There seems to be a bug that is present in DD2.x CPUs
  61. * and still only partially fixed in DD3.1.
  62. * This bug causes a value written to the priority register
  63. * not to make it there, resulting in a system hang unless we
  64. * write it again.
  65. * Masking with 0xf0 is done because the Cell BE does not
  66. * implement the lower four bits of the interrupt priority,
  67. * they always read back as zeroes, although future CPUs
  68. * might implement different bits.
  69. */
  70. do {
  71. out_be64(&iic->regs->prio, 0xff);
  72. tmp = in_be64(&iic->regs->prio);
  73. } while ((tmp & 0xf0) != 0xf0);
  74. }
  75. void iic_local_disable(void)
  76. {
  77. out_be64(&__get_cpu_var(iic).regs->prio, 0x0);
  78. }
  79. static unsigned int iic_startup(unsigned int irq)
  80. {
  81. return 0;
  82. }
  83. static void iic_enable(unsigned int irq)
  84. {
  85. iic_local_enable();
  86. }
  87. static void iic_disable(unsigned int irq)
  88. {
  89. }
  90. static void iic_end(unsigned int irq)
  91. {
  92. iic_local_enable();
  93. }
  94. static struct hw_interrupt_type iic_pic = {
  95. .typename = " CELL-IIC ",
  96. .startup = iic_startup,
  97. .enable = iic_enable,
  98. .disable = iic_disable,
  99. .end = iic_end,
  100. };
  101. static int iic_external_get_irq(struct iic_pending_bits pending)
  102. {
  103. int irq;
  104. unsigned char node, unit;
  105. node = pending.source >> 4;
  106. unit = pending.source & 0xf;
  107. irq = -1;
  108. /*
  109. * This mapping is specific to the Cell Broadband
  110. * Engine. We might need to get the numbers
  111. * from the device tree to support future CPUs.
  112. */
  113. switch (unit) {
  114. case 0x00:
  115. case 0x0b:
  116. /*
  117. * One of these units can be connected
  118. * to an external interrupt controller.
  119. */
  120. if (pending.prio > 0x3f ||
  121. pending.class != 2)
  122. break;
  123. irq = IIC_EXT_OFFSET
  124. + spider_get_irq(node)
  125. + node * IIC_NODE_STRIDE;
  126. break;
  127. case 0x01 ... 0x04:
  128. case 0x07 ... 0x0a:
  129. /*
  130. * These units are connected to the SPEs
  131. */
  132. if (pending.class > 2)
  133. break;
  134. irq = IIC_SPE_OFFSET
  135. + pending.class * IIC_CLASS_STRIDE
  136. + node * IIC_NODE_STRIDE
  137. + unit;
  138. break;
  139. }
  140. if (irq == -1)
  141. printk(KERN_WARNING "Unexpected interrupt class %02x, "
  142. "source %02x, prio %02x, cpu %02x\n", pending.class,
  143. pending.source, pending.prio, smp_processor_id());
  144. return irq;
  145. }
  146. /* Get an IRQ number from the pending state register of the IIC */
  147. int iic_get_irq(struct pt_regs *regs)
  148. {
  149. struct iic *iic;
  150. int irq;
  151. struct iic_pending_bits pending;
  152. iic = &__get_cpu_var(iic);
  153. *(unsigned long *) &pending =
  154. in_be64((unsigned long __iomem *) &iic->regs->pending_destr);
  155. irq = -1;
  156. if (pending.flags & IIC_VALID) {
  157. if (pending.flags & IIC_IPI) {
  158. irq = IIC_IPI_OFFSET + (pending.prio >> 4);
  159. /*
  160. if (irq > 0x80)
  161. printk(KERN_WARNING "Unexpected IPI prio %02x"
  162. "on CPU %02x\n", pending.prio,
  163. smp_processor_id());
  164. */
  165. } else {
  166. irq = iic_external_get_irq(pending);
  167. }
  168. }
  169. return irq;
  170. }
  171. /* hardcoded part to be compatible with older firmware */
  172. static int setup_iic_hardcoded(void)
  173. {
  174. struct device_node *np;
  175. int nodeid, cpu;
  176. unsigned long regs;
  177. struct iic *iic;
  178. for_each_cpu(cpu) {
  179. iic = &per_cpu(iic, cpu);
  180. nodeid = cpu/2;
  181. for (np = of_find_node_by_type(NULL, "cpu");
  182. np;
  183. np = of_find_node_by_type(np, "cpu")) {
  184. if (nodeid == *(int *)get_property(np, "node-id", NULL))
  185. break;
  186. }
  187. if (!np) {
  188. printk(KERN_WARNING "IIC: CPU %d not found\n", cpu);
  189. iic->regs = NULL;
  190. iic->target_id = 0xff;
  191. return -ENODEV;
  192. }
  193. regs = *(long *)get_property(np, "iic", NULL);
  194. /* hack until we have decided on the devtree info */
  195. regs += 0x400;
  196. if (cpu & 1)
  197. regs += 0x20;
  198. printk(KERN_INFO "IIC for CPU %d at %lx\n", cpu, regs);
  199. iic->regs = ioremap(regs, sizeof(struct iic_regs));
  200. iic->target_id = (nodeid << 4) + ((cpu & 1) ? 0xf : 0xe);
  201. }
  202. return 0;
  203. }
  204. static int setup_iic(void)
  205. {
  206. struct device_node *dn;
  207. unsigned long *regs;
  208. char *compatible;
  209. unsigned *np, found = 0;
  210. struct iic *iic = NULL;
  211. for (dn = NULL; (dn = of_find_node_by_name(dn, "interrupt-controller"));) {
  212. compatible = (char *)get_property(dn, "compatible", NULL);
  213. if (!compatible) {
  214. printk(KERN_WARNING "no compatible property found !\n");
  215. continue;
  216. }
  217. if (strstr(compatible, "IBM,CBEA-Internal-Interrupt-Controller"))
  218. regs = (unsigned long *)get_property(dn,"reg", NULL);
  219. else
  220. continue;
  221. if (!regs)
  222. printk(KERN_WARNING "IIC: no reg property\n");
  223. np = (unsigned int *)get_property(dn, "ibm,interrupt-server-ranges", NULL);
  224. if (!np) {
  225. printk(KERN_WARNING "IIC: CPU association not found\n");
  226. iic->regs = NULL;
  227. iic->target_id = 0xff;
  228. return -ENODEV;
  229. }
  230. iic = &per_cpu(iic, np[0]);
  231. iic->regs = ioremap(regs[0], sizeof(struct iic_regs));
  232. iic->target_id = ((np[0] & 2) << 3) + ((np[0] & 1) ? 0xf : 0xe);
  233. printk("IIC for CPU %d at %lx mapped to %p\n", np[0], regs[0], iic->regs);
  234. iic = &per_cpu(iic, np[1]);
  235. iic->regs = ioremap(regs[2], sizeof(struct iic_regs));
  236. iic->target_id = ((np[1] & 2) << 3) + ((np[1] & 1) ? 0xf : 0xe);
  237. printk("IIC for CPU %d at %lx mapped to %p\n", np[1], regs[2], iic->regs);
  238. found++;
  239. }
  240. if (found)
  241. return 0;
  242. else
  243. return -ENODEV;
  244. }
  245. #ifdef CONFIG_SMP
  246. /* Use the highest interrupt priorities for IPI */
  247. static inline int iic_ipi_to_irq(int ipi)
  248. {
  249. return IIC_IPI_OFFSET + IIC_NUM_IPIS - 1 - ipi;
  250. }
  251. static inline int iic_irq_to_ipi(int irq)
  252. {
  253. return IIC_NUM_IPIS - 1 - (irq - IIC_IPI_OFFSET);
  254. }
  255. void iic_setup_cpu(void)
  256. {
  257. out_be64(&__get_cpu_var(iic).regs->prio, 0xff);
  258. }
  259. void iic_cause_IPI(int cpu, int mesg)
  260. {
  261. out_be64(&per_cpu(iic, cpu).regs->generate, (IIC_NUM_IPIS - 1 - mesg) << 4);
  262. }
  263. u8 iic_get_target_id(int cpu)
  264. {
  265. return per_cpu(iic, cpu).target_id;
  266. }
  267. EXPORT_SYMBOL_GPL(iic_get_target_id);
  268. static irqreturn_t iic_ipi_action(int irq, void *dev_id, struct pt_regs *regs)
  269. {
  270. smp_message_recv(iic_irq_to_ipi(irq), regs);
  271. return IRQ_HANDLED;
  272. }
  273. static void iic_request_ipi(int ipi, const char *name)
  274. {
  275. int irq;
  276. irq = iic_ipi_to_irq(ipi);
  277. /* IPIs are marked SA_INTERRUPT as they must run with irqs
  278. * disabled */
  279. get_irq_desc(irq)->handler = &iic_pic;
  280. get_irq_desc(irq)->status |= IRQ_PER_CPU;
  281. request_irq(irq, iic_ipi_action, SA_INTERRUPT, name, NULL);
  282. }
  283. void iic_request_IPIs(void)
  284. {
  285. iic_request_ipi(PPC_MSG_CALL_FUNCTION, "IPI-call");
  286. iic_request_ipi(PPC_MSG_RESCHEDULE, "IPI-resched");
  287. #ifdef CONFIG_DEBUGGER
  288. iic_request_ipi(PPC_MSG_DEBUGGER_BREAK, "IPI-debug");
  289. #endif /* CONFIG_DEBUGGER */
  290. }
  291. #endif /* CONFIG_SMP */
  292. static void iic_setup_spe_handlers(void)
  293. {
  294. int be, isrc;
  295. /* Assume two threads per BE are present */
  296. for (be=0; be < num_present_cpus() / 2; be++) {
  297. for (isrc = 0; isrc < IIC_CLASS_STRIDE * 3; isrc++) {
  298. int irq = IIC_NODE_STRIDE * be + IIC_SPE_OFFSET + isrc;
  299. get_irq_desc(irq)->handler = &iic_pic;
  300. }
  301. }
  302. }
  303. void iic_init_IRQ(void)
  304. {
  305. int cpu, irq_offset;
  306. struct iic *iic;
  307. if (setup_iic() < 0)
  308. setup_iic_hardcoded();
  309. irq_offset = 0;
  310. for_each_possible_cpu(cpu) {
  311. iic = &per_cpu(iic, cpu);
  312. if (iic->regs)
  313. out_be64(&iic->regs->prio, 0xff);
  314. }
  315. iic_setup_spe_handlers();
  316. }