interrupt.c 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395
  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/module.h>
  34. #include <linux/percpu.h>
  35. #include <linux/types.h>
  36. #include <linux/ioport.h>
  37. #include <asm/io.h>
  38. #include <asm/pgtable.h>
  39. #include <asm/prom.h>
  40. #include <asm/ptrace.h>
  41. #include <asm/machdep.h>
  42. #include "interrupt.h"
  43. #include "cbe_regs.h"
  44. struct iic {
  45. struct cbe_iic_thread_regs __iomem *regs;
  46. u8 target_id;
  47. u8 eoi_stack[16];
  48. int eoi_ptr;
  49. struct device_node *node;
  50. };
  51. static DEFINE_PER_CPU(struct iic, iic);
  52. #define IIC_NODE_COUNT 2
  53. static struct irq_host *iic_host;
  54. /* Convert between "pending" bits and hw irq number */
  55. static irq_hw_number_t iic_pending_to_hwnum(struct cbe_iic_pending_bits bits)
  56. {
  57. unsigned char unit = bits.source & 0xf;
  58. unsigned char node = bits.source >> 4;
  59. unsigned char class = bits.class & 3;
  60. /* Decode IPIs */
  61. if (bits.flags & CBE_IIC_IRQ_IPI)
  62. return IIC_IRQ_TYPE_IPI | (bits.prio >> 4);
  63. else
  64. return (node << IIC_IRQ_NODE_SHIFT) | (class << 4) | unit;
  65. }
  66. static void iic_mask(unsigned int irq)
  67. {
  68. }
  69. static void iic_unmask(unsigned int irq)
  70. {
  71. }
  72. static void iic_eoi(unsigned int irq)
  73. {
  74. struct iic *iic = &__get_cpu_var(iic);
  75. out_be64(&iic->regs->prio, iic->eoi_stack[--iic->eoi_ptr]);
  76. BUG_ON(iic->eoi_ptr < 0);
  77. }
  78. static struct irq_chip iic_chip = {
  79. .typename = " CELL-IIC ",
  80. .mask = iic_mask,
  81. .unmask = iic_unmask,
  82. .eoi = iic_eoi,
  83. };
  84. static void iic_ioexc_eoi(unsigned int irq)
  85. {
  86. }
  87. static void iic_ioexc_cascade(unsigned int irq, struct irq_desc *desc,
  88. struct pt_regs *regs)
  89. {
  90. struct cbe_iic_regs *node_iic = desc->handler_data;
  91. unsigned int base = (irq & 0xffffff00) | IIC_IRQ_TYPE_IOEXC;
  92. unsigned long bits, ack;
  93. int cascade;
  94. for (;;) {
  95. bits = in_be64(&node_iic->iic_is);
  96. if (bits == 0)
  97. break;
  98. /* pre-ack edge interrupts */
  99. ack = bits & IIC_ISR_EDGE_MASK;
  100. if (ack)
  101. out_be64(&node_iic->iic_is, ack);
  102. /* handle them */
  103. for (cascade = 63; cascade >= 0; cascade--)
  104. if (bits & (0x8000000000000000UL >> cascade)) {
  105. unsigned int cirq =
  106. irq_linear_revmap(iic_host,
  107. base | cascade);
  108. if (cirq != NO_IRQ)
  109. generic_handle_irq(cirq, regs);
  110. }
  111. /* post-ack level interrupts */
  112. ack = bits & ~IIC_ISR_EDGE_MASK;
  113. if (ack)
  114. out_be64(&node_iic->iic_is, ack);
  115. }
  116. desc->chip->eoi(irq);
  117. }
  118. static struct irq_chip iic_ioexc_chip = {
  119. .typename = " CELL-IOEX",
  120. .mask = iic_mask,
  121. .unmask = iic_unmask,
  122. .eoi = iic_ioexc_eoi,
  123. };
  124. /* Get an IRQ number from the pending state register of the IIC */
  125. static unsigned int iic_get_irq(struct pt_regs *regs)
  126. {
  127. struct cbe_iic_pending_bits pending;
  128. struct iic *iic;
  129. unsigned int virq;
  130. iic = &__get_cpu_var(iic);
  131. *(unsigned long *) &pending =
  132. in_be64((unsigned long __iomem *) &iic->regs->pending_destr);
  133. if (!(pending.flags & CBE_IIC_IRQ_VALID))
  134. return NO_IRQ;
  135. virq = irq_linear_revmap(iic_host, iic_pending_to_hwnum(pending));
  136. if (virq == NO_IRQ)
  137. return NO_IRQ;
  138. iic->eoi_stack[++iic->eoi_ptr] = pending.prio;
  139. BUG_ON(iic->eoi_ptr > 15);
  140. return virq;
  141. }
  142. #ifdef CONFIG_SMP
  143. /* Use the highest interrupt priorities for IPI */
  144. static inline int iic_ipi_to_irq(int ipi)
  145. {
  146. return IIC_IRQ_TYPE_IPI + 0xf - ipi;
  147. }
  148. void iic_setup_cpu(void)
  149. {
  150. out_be64(&__get_cpu_var(iic).regs->prio, 0xff);
  151. }
  152. void iic_cause_IPI(int cpu, int mesg)
  153. {
  154. out_be64(&per_cpu(iic, cpu).regs->generate, (0xf - mesg) << 4);
  155. }
  156. u8 iic_get_target_id(int cpu)
  157. {
  158. return per_cpu(iic, cpu).target_id;
  159. }
  160. EXPORT_SYMBOL_GPL(iic_get_target_id);
  161. struct irq_host *iic_get_irq_host(int node)
  162. {
  163. return iic_host;
  164. }
  165. EXPORT_SYMBOL_GPL(iic_get_irq_host);
  166. static irqreturn_t iic_ipi_action(int irq, void *dev_id, struct pt_regs *regs)
  167. {
  168. int ipi = (int)(long)dev_id;
  169. smp_message_recv(ipi, regs);
  170. return IRQ_HANDLED;
  171. }
  172. static void iic_request_ipi(int ipi, const char *name)
  173. {
  174. int virq;
  175. virq = irq_create_mapping(iic_host, iic_ipi_to_irq(ipi));
  176. if (virq == NO_IRQ) {
  177. printk(KERN_ERR
  178. "iic: failed to map IPI %s\n", name);
  179. return;
  180. }
  181. if (request_irq(virq, iic_ipi_action, IRQF_DISABLED, name,
  182. (void *)(long)ipi))
  183. printk(KERN_ERR
  184. "iic: failed to request IPI %s\n", name);
  185. }
  186. void iic_request_IPIs(void)
  187. {
  188. iic_request_ipi(PPC_MSG_CALL_FUNCTION, "IPI-call");
  189. iic_request_ipi(PPC_MSG_RESCHEDULE, "IPI-resched");
  190. #ifdef CONFIG_DEBUGGER
  191. iic_request_ipi(PPC_MSG_DEBUGGER_BREAK, "IPI-debug");
  192. #endif /* CONFIG_DEBUGGER */
  193. }
  194. #endif /* CONFIG_SMP */
  195. static int iic_host_match(struct irq_host *h, struct device_node *node)
  196. {
  197. return device_is_compatible(node,
  198. "IBM,CBEA-Internal-Interrupt-Controller");
  199. }
  200. static int iic_host_map(struct irq_host *h, unsigned int virq,
  201. irq_hw_number_t hw)
  202. {
  203. switch (hw & IIC_IRQ_TYPE_MASK) {
  204. case IIC_IRQ_TYPE_IPI:
  205. set_irq_chip_and_handler(virq, &iic_chip, handle_percpu_irq);
  206. break;
  207. case IIC_IRQ_TYPE_IOEXC:
  208. set_irq_chip_and_handler(virq, &iic_ioexc_chip,
  209. handle_fasteoi_irq);
  210. break;
  211. default:
  212. set_irq_chip_and_handler(virq, &iic_chip, handle_fasteoi_irq);
  213. }
  214. return 0;
  215. }
  216. static int iic_host_xlate(struct irq_host *h, struct device_node *ct,
  217. u32 *intspec, unsigned int intsize,
  218. irq_hw_number_t *out_hwirq, unsigned int *out_flags)
  219. {
  220. unsigned int node, ext, unit, class;
  221. const u32 *val;
  222. if (!device_is_compatible(ct,
  223. "IBM,CBEA-Internal-Interrupt-Controller"))
  224. return -ENODEV;
  225. if (intsize != 1)
  226. return -ENODEV;
  227. val = get_property(ct, "#interrupt-cells", NULL);
  228. if (val == NULL || *val != 1)
  229. return -ENODEV;
  230. node = intspec[0] >> 24;
  231. ext = (intspec[0] >> 16) & 0xff;
  232. class = (intspec[0] >> 8) & 0xff;
  233. unit = intspec[0] & 0xff;
  234. /* Check if node is in supported range */
  235. if (node > 1)
  236. return -EINVAL;
  237. /* Build up interrupt number, special case for IO exceptions */
  238. *out_hwirq = (node << IIC_IRQ_NODE_SHIFT);
  239. if (unit == IIC_UNIT_IIC && class == 1)
  240. *out_hwirq |= IIC_IRQ_TYPE_IOEXC | ext;
  241. else
  242. *out_hwirq |= IIC_IRQ_TYPE_NORMAL |
  243. (class << IIC_IRQ_CLASS_SHIFT) | unit;
  244. /* Dummy flags, ignored by iic code */
  245. *out_flags = IRQ_TYPE_EDGE_RISING;
  246. return 0;
  247. }
  248. static struct irq_host_ops iic_host_ops = {
  249. .match = iic_host_match,
  250. .map = iic_host_map,
  251. .xlate = iic_host_xlate,
  252. };
  253. static void __init init_one_iic(unsigned int hw_cpu, unsigned long addr,
  254. struct device_node *node)
  255. {
  256. /* XXX FIXME: should locate the linux CPU number from the HW cpu
  257. * number properly. We are lucky for now
  258. */
  259. struct iic *iic = &per_cpu(iic, hw_cpu);
  260. iic->regs = ioremap(addr, sizeof(struct cbe_iic_thread_regs));
  261. BUG_ON(iic->regs == NULL);
  262. iic->target_id = ((hw_cpu & 2) << 3) | ((hw_cpu & 1) ? 0xf : 0xe);
  263. iic->eoi_stack[0] = 0xff;
  264. iic->node = of_node_get(node);
  265. out_be64(&iic->regs->prio, 0);
  266. printk(KERN_INFO "IIC for CPU %d target id 0x%x : %s\n",
  267. hw_cpu, iic->target_id, node->full_name);
  268. }
  269. static int __init setup_iic(void)
  270. {
  271. struct device_node *dn;
  272. struct resource r0, r1;
  273. unsigned int node, cascade, found = 0;
  274. struct cbe_iic_regs *node_iic;
  275. const u32 *np;
  276. for (dn = NULL;
  277. (dn = of_find_node_by_name(dn,"interrupt-controller")) != NULL;) {
  278. if (!device_is_compatible(dn,
  279. "IBM,CBEA-Internal-Interrupt-Controller"))
  280. continue;
  281. np = get_property(dn, "ibm,interrupt-server-ranges", NULL);
  282. if (np == NULL) {
  283. printk(KERN_WARNING "IIC: CPU association not found\n");
  284. of_node_put(dn);
  285. return -ENODEV;
  286. }
  287. if (of_address_to_resource(dn, 0, &r0) ||
  288. of_address_to_resource(dn, 1, &r1)) {
  289. printk(KERN_WARNING "IIC: Can't resolve addresses\n");
  290. of_node_put(dn);
  291. return -ENODEV;
  292. }
  293. found++;
  294. init_one_iic(np[0], r0.start, dn);
  295. init_one_iic(np[1], r1.start, dn);
  296. /* Setup cascade for IO exceptions. XXX cleanup tricks to get
  297. * node vs CPU etc...
  298. * Note that we configure the IIC_IRR here with a hard coded
  299. * priority of 1. We might want to improve that later.
  300. */
  301. node = np[0] >> 1;
  302. node_iic = cbe_get_cpu_iic_regs(np[0]);
  303. cascade = node << IIC_IRQ_NODE_SHIFT;
  304. cascade |= 1 << IIC_IRQ_CLASS_SHIFT;
  305. cascade |= IIC_UNIT_IIC;
  306. cascade = irq_create_mapping(iic_host, cascade);
  307. if (cascade == NO_IRQ)
  308. continue;
  309. set_irq_data(cascade, node_iic);
  310. set_irq_chained_handler(cascade , iic_ioexc_cascade);
  311. out_be64(&node_iic->iic_ir,
  312. (1 << 12) /* priority */ |
  313. (node << 4) /* dest node */ |
  314. IIC_UNIT_THREAD_0 /* route them to thread 0 */);
  315. /* Flush pending (make sure it triggers if there is
  316. * anything pending
  317. */
  318. out_be64(&node_iic->iic_is, 0xfffffffffffffffful);
  319. }
  320. if (found)
  321. return 0;
  322. else
  323. return -ENODEV;
  324. }
  325. void __init iic_init_IRQ(void)
  326. {
  327. /* Setup an irq host data structure */
  328. iic_host = irq_alloc_host(IRQ_HOST_MAP_LINEAR, IIC_SOURCE_COUNT,
  329. &iic_host_ops, IIC_IRQ_INVALID);
  330. BUG_ON(iic_host == NULL);
  331. irq_set_default_host(iic_host);
  332. /* Discover and initialize iics */
  333. if (setup_iic() < 0)
  334. panic("IIC: Failed to initialize !\n");
  335. /* Set master interrupt handling function */
  336. ppc_md.get_irq = iic_get_irq;
  337. /* Enable on current CPU */
  338. iic_setup_cpu();
  339. }