interrupt.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408
  1. /*
  2. * Cell Internal Interrupt Controller
  3. *
  4. * Copyright (C) 2006 Benjamin Herrenschmidt (benh@kernel.crashing.org)
  5. * IBM, Corp.
  6. *
  7. * (C) Copyright IBM Deutschland Entwicklung GmbH 2005
  8. *
  9. * Author: Arnd Bergmann <arndb@de.ibm.com>
  10. *
  11. * This program is free software; you can redistribute it and/or modify
  12. * it under the terms of the GNU General Public License as published by
  13. * the Free Software Foundation; either version 2, or (at your option)
  14. * any later version.
  15. *
  16. * This program is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. * GNU General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU General Public License
  22. * along with this program; if not, write to the Free Software
  23. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  24. *
  25. * TODO:
  26. * - Fix various assumptions related to HW CPU numbers vs. linux CPU numbers
  27. * vs node numbers in the setup code
  28. * - Implement proper handling of maxcpus=1/2 (that is, routing of irqs from
  29. * a non-active node to the active node)
  30. */
  31. #include <linux/interrupt.h>
  32. #include <linux/irq.h>
  33. #include <linux/export.h>
  34. #include <linux/percpu.h>
  35. #include <linux/types.h>
  36. #include <linux/ioport.h>
  37. #include <linux/kernel_stat.h>
  38. #include <asm/io.h>
  39. #include <asm/pgtable.h>
  40. #include <asm/prom.h>
  41. #include <asm/ptrace.h>
  42. #include <asm/machdep.h>
  43. #include <asm/cell-regs.h>
  44. #include "interrupt.h"
  45. struct iic {
  46. struct cbe_iic_thread_regs __iomem *regs;
  47. u8 target_id;
  48. u8 eoi_stack[16];
  49. int eoi_ptr;
  50. struct device_node *node;
  51. };
  52. static DEFINE_PER_CPU(struct iic, cpu_iic);
  53. #define IIC_NODE_COUNT 2
  54. static struct irq_domain *iic_host;
  55. /* Convert between "pending" bits and hw irq number */
  56. static irq_hw_number_t iic_pending_to_hwnum(struct cbe_iic_pending_bits bits)
  57. {
  58. unsigned char unit = bits.source & 0xf;
  59. unsigned char node = bits.source >> 4;
  60. unsigned char class = bits.class & 3;
  61. /* Decode IPIs */
  62. if (bits.flags & CBE_IIC_IRQ_IPI)
  63. return IIC_IRQ_TYPE_IPI | (bits.prio >> 4);
  64. else
  65. return (node << IIC_IRQ_NODE_SHIFT) | (class << 4) | unit;
  66. }
  67. static void iic_mask(struct irq_data *d)
  68. {
  69. }
  70. static void iic_unmask(struct irq_data *d)
  71. {
  72. }
  73. static void iic_eoi(struct irq_data *d)
  74. {
  75. struct iic *iic = this_cpu_ptr(&cpu_iic);
  76. out_be64(&iic->regs->prio, iic->eoi_stack[--iic->eoi_ptr]);
  77. BUG_ON(iic->eoi_ptr < 0);
  78. }
  79. static struct irq_chip iic_chip = {
  80. .name = "CELL-IIC",
  81. .irq_mask = iic_mask,
  82. .irq_unmask = iic_unmask,
  83. .irq_eoi = iic_eoi,
  84. };
  85. static void iic_ioexc_eoi(struct irq_data *d)
  86. {
  87. }
  88. static void iic_ioexc_cascade(struct irq_desc *desc)
  89. {
  90. struct irq_chip *chip = irq_desc_get_chip(desc);
  91. struct cbe_iic_regs __iomem *node_iic =
  92. (void __iomem *)irq_desc_get_handler_data(desc);
  93. unsigned int irq = irq_desc_get_irq(desc);
  94. unsigned int base = (irq & 0xffffff00) | IIC_IRQ_TYPE_IOEXC;
  95. unsigned long bits, ack;
  96. int cascade;
  97. for (;;) {
  98. bits = in_be64(&node_iic->iic_is);
  99. if (bits == 0)
  100. break;
  101. /* pre-ack edge interrupts */
  102. ack = bits & IIC_ISR_EDGE_MASK;
  103. if (ack)
  104. out_be64(&node_iic->iic_is, ack);
  105. /* handle them */
  106. for (cascade = 63; cascade >= 0; cascade--)
  107. if (bits & (0x8000000000000000UL >> cascade)) {
  108. unsigned int cirq =
  109. irq_linear_revmap(iic_host,
  110. base | cascade);
  111. if (cirq)
  112. generic_handle_irq(cirq);
  113. }
  114. /* post-ack level interrupts */
  115. ack = bits & ~IIC_ISR_EDGE_MASK;
  116. if (ack)
  117. out_be64(&node_iic->iic_is, ack);
  118. }
  119. chip->irq_eoi(&desc->irq_data);
  120. }
  121. static struct irq_chip iic_ioexc_chip = {
  122. .name = "CELL-IOEX",
  123. .irq_mask = iic_mask,
  124. .irq_unmask = iic_unmask,
  125. .irq_eoi = iic_ioexc_eoi,
  126. };
  127. /* Get an IRQ number from the pending state register of the IIC */
  128. static unsigned int iic_get_irq(void)
  129. {
  130. struct cbe_iic_pending_bits pending;
  131. struct iic *iic;
  132. unsigned int virq;
  133. iic = this_cpu_ptr(&cpu_iic);
  134. *(unsigned long *) &pending =
  135. in_be64((u64 __iomem *) &iic->regs->pending_destr);
  136. if (!(pending.flags & CBE_IIC_IRQ_VALID))
  137. return 0;
  138. virq = irq_linear_revmap(iic_host, iic_pending_to_hwnum(pending));
  139. if (!virq)
  140. return 0;
  141. iic->eoi_stack[++iic->eoi_ptr] = pending.prio;
  142. BUG_ON(iic->eoi_ptr > 15);
  143. return virq;
  144. }
  145. void iic_setup_cpu(void)
  146. {
  147. out_be64(&this_cpu_ptr(&cpu_iic)->regs->prio, 0xff);
  148. }
  149. u8 iic_get_target_id(int cpu)
  150. {
  151. return per_cpu(cpu_iic, cpu).target_id;
  152. }
  153. EXPORT_SYMBOL_GPL(iic_get_target_id);
  154. #ifdef CONFIG_SMP
  155. /* Use the highest interrupt priorities for IPI */
  156. static inline int iic_msg_to_irq(int msg)
  157. {
  158. return IIC_IRQ_TYPE_IPI + 0xf - msg;
  159. }
  160. void iic_message_pass(int cpu, int msg)
  161. {
  162. out_be64(&per_cpu(cpu_iic, cpu).regs->generate, (0xf - msg) << 4);
  163. }
  164. static void iic_request_ipi(int msg)
  165. {
  166. int virq;
  167. virq = irq_create_mapping(iic_host, iic_msg_to_irq(msg));
  168. if (!virq) {
  169. printk(KERN_ERR
  170. "iic: failed to map IPI %s\n", smp_ipi_name[msg]);
  171. return;
  172. }
  173. /*
  174. * If smp_request_message_ipi encounters an error it will notify
  175. * the error. If a message is not needed it will return non-zero.
  176. */
  177. if (smp_request_message_ipi(virq, msg))
  178. irq_dispose_mapping(virq);
  179. }
  180. void iic_request_IPIs(void)
  181. {
  182. iic_request_ipi(PPC_MSG_CALL_FUNCTION);
  183. iic_request_ipi(PPC_MSG_RESCHEDULE);
  184. iic_request_ipi(PPC_MSG_TICK_BROADCAST);
  185. iic_request_ipi(PPC_MSG_DEBUGGER_BREAK);
  186. }
  187. #endif /* CONFIG_SMP */
  188. static int iic_host_match(struct irq_domain *h, struct device_node *node,
  189. enum irq_domain_bus_token bus_token)
  190. {
  191. return of_device_is_compatible(node,
  192. "IBM,CBEA-Internal-Interrupt-Controller");
  193. }
  194. static int iic_host_map(struct irq_domain *h, unsigned int virq,
  195. irq_hw_number_t hw)
  196. {
  197. switch (hw & IIC_IRQ_TYPE_MASK) {
  198. case IIC_IRQ_TYPE_IPI:
  199. irq_set_chip_and_handler(virq, &iic_chip, handle_percpu_irq);
  200. break;
  201. case IIC_IRQ_TYPE_IOEXC:
  202. irq_set_chip_and_handler(virq, &iic_ioexc_chip,
  203. handle_edge_eoi_irq);
  204. break;
  205. default:
  206. irq_set_chip_and_handler(virq, &iic_chip, handle_edge_eoi_irq);
  207. }
  208. return 0;
  209. }
  210. static int iic_host_xlate(struct irq_domain *h, struct device_node *ct,
  211. const u32 *intspec, unsigned int intsize,
  212. irq_hw_number_t *out_hwirq, unsigned int *out_flags)
  213. {
  214. unsigned int node, ext, unit, class;
  215. const u32 *val;
  216. if (!of_device_is_compatible(ct,
  217. "IBM,CBEA-Internal-Interrupt-Controller"))
  218. return -ENODEV;
  219. if (intsize != 1)
  220. return -ENODEV;
  221. val = of_get_property(ct, "#interrupt-cells", NULL);
  222. if (val == NULL || *val != 1)
  223. return -ENODEV;
  224. node = intspec[0] >> 24;
  225. ext = (intspec[0] >> 16) & 0xff;
  226. class = (intspec[0] >> 8) & 0xff;
  227. unit = intspec[0] & 0xff;
  228. /* Check if node is in supported range */
  229. if (node > 1)
  230. return -EINVAL;
  231. /* Build up interrupt number, special case for IO exceptions */
  232. *out_hwirq = (node << IIC_IRQ_NODE_SHIFT);
  233. if (unit == IIC_UNIT_IIC && class == 1)
  234. *out_hwirq |= IIC_IRQ_TYPE_IOEXC | ext;
  235. else
  236. *out_hwirq |= IIC_IRQ_TYPE_NORMAL |
  237. (class << IIC_IRQ_CLASS_SHIFT) | unit;
  238. /* Dummy flags, ignored by iic code */
  239. *out_flags = IRQ_TYPE_EDGE_RISING;
  240. return 0;
  241. }
  242. static const struct irq_domain_ops iic_host_ops = {
  243. .match = iic_host_match,
  244. .map = iic_host_map,
  245. .xlate = iic_host_xlate,
  246. };
  247. static void __init init_one_iic(unsigned int hw_cpu, unsigned long addr,
  248. struct device_node *node)
  249. {
  250. /* XXX FIXME: should locate the linux CPU number from the HW cpu
  251. * number properly. We are lucky for now
  252. */
  253. struct iic *iic = &per_cpu(cpu_iic, hw_cpu);
  254. iic->regs = ioremap(addr, sizeof(struct cbe_iic_thread_regs));
  255. BUG_ON(iic->regs == NULL);
  256. iic->target_id = ((hw_cpu & 2) << 3) | ((hw_cpu & 1) ? 0xf : 0xe);
  257. iic->eoi_stack[0] = 0xff;
  258. iic->node = of_node_get(node);
  259. out_be64(&iic->regs->prio, 0);
  260. printk(KERN_INFO "IIC for CPU %d target id 0x%x : %s\n",
  261. hw_cpu, iic->target_id, node->full_name);
  262. }
  263. static int __init setup_iic(void)
  264. {
  265. struct device_node *dn;
  266. struct resource r0, r1;
  267. unsigned int node, cascade, found = 0;
  268. struct cbe_iic_regs __iomem *node_iic;
  269. const u32 *np;
  270. for (dn = NULL;
  271. (dn = of_find_node_by_name(dn,"interrupt-controller")) != NULL;) {
  272. if (!of_device_is_compatible(dn,
  273. "IBM,CBEA-Internal-Interrupt-Controller"))
  274. continue;
  275. np = of_get_property(dn, "ibm,interrupt-server-ranges", NULL);
  276. if (np == NULL) {
  277. printk(KERN_WARNING "IIC: CPU association not found\n");
  278. of_node_put(dn);
  279. return -ENODEV;
  280. }
  281. if (of_address_to_resource(dn, 0, &r0) ||
  282. of_address_to_resource(dn, 1, &r1)) {
  283. printk(KERN_WARNING "IIC: Can't resolve addresses\n");
  284. of_node_put(dn);
  285. return -ENODEV;
  286. }
  287. found++;
  288. init_one_iic(np[0], r0.start, dn);
  289. init_one_iic(np[1], r1.start, dn);
  290. /* Setup cascade for IO exceptions. XXX cleanup tricks to get
  291. * node vs CPU etc...
  292. * Note that we configure the IIC_IRR here with a hard coded
  293. * priority of 1. We might want to improve that later.
  294. */
  295. node = np[0] >> 1;
  296. node_iic = cbe_get_cpu_iic_regs(np[0]);
  297. cascade = node << IIC_IRQ_NODE_SHIFT;
  298. cascade |= 1 << IIC_IRQ_CLASS_SHIFT;
  299. cascade |= IIC_UNIT_IIC;
  300. cascade = irq_create_mapping(iic_host, cascade);
  301. if (!cascade)
  302. continue;
  303. /*
  304. * irq_data is a generic pointer that gets passed back
  305. * to us later, so the forced cast is fine.
  306. */
  307. irq_set_handler_data(cascade, (void __force *)node_iic);
  308. irq_set_chained_handler(cascade, iic_ioexc_cascade);
  309. out_be64(&node_iic->iic_ir,
  310. (1 << 12) /* priority */ |
  311. (node << 4) /* dest node */ |
  312. IIC_UNIT_THREAD_0 /* route them to thread 0 */);
  313. /* Flush pending (make sure it triggers if there is
  314. * anything pending
  315. */
  316. out_be64(&node_iic->iic_is, 0xfffffffffffffffful);
  317. }
  318. if (found)
  319. return 0;
  320. else
  321. return -ENODEV;
  322. }
  323. void __init iic_init_IRQ(void)
  324. {
  325. /* Setup an irq host data structure */
  326. iic_host = irq_domain_add_linear(NULL, IIC_SOURCE_COUNT, &iic_host_ops,
  327. NULL);
  328. BUG_ON(iic_host == NULL);
  329. irq_set_default_host(iic_host);
  330. /* Discover and initialize iics */
  331. if (setup_iic() < 0)
  332. panic("IIC: Failed to initialize !\n");
  333. /* Set master interrupt handling function */
  334. ppc_md.get_irq = iic_get_irq;
  335. /* Enable on current CPU */
  336. iic_setup_cpu();
  337. }
  338. void iic_set_interrupt_routing(int cpu, int thread, int priority)
  339. {
  340. struct cbe_iic_regs __iomem *iic_regs = cbe_get_cpu_iic_regs(cpu);
  341. u64 iic_ir = 0;
  342. int node = cpu >> 1;
  343. /* Set which node and thread will handle the next interrupt */
  344. iic_ir |= CBE_IIC_IR_PRIO(priority) |
  345. CBE_IIC_IR_DEST_NODE(node);
  346. if (thread == 0)
  347. iic_ir |= CBE_IIC_IR_DEST_UNIT(CBE_IIC_IR_PT_0);
  348. else
  349. iic_ir |= CBE_IIC_IR_DEST_UNIT(CBE_IIC_IR_PT_1);
  350. out_be64(&iic_regs->iic_ir, iic_ir);
  351. }