interrupt.c 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  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. #include <linux/interrupt.h>
  26. #include <linux/irq.h>
  27. #include <linux/module.h>
  28. #include <linux/percpu.h>
  29. #include <linux/types.h>
  30. #include <linux/ioport.h>
  31. #include <asm/io.h>
  32. #include <asm/pgtable.h>
  33. #include <asm/prom.h>
  34. #include <asm/ptrace.h>
  35. #include <asm/machdep.h>
  36. #include "interrupt.h"
  37. #include "cbe_regs.h"
  38. struct iic {
  39. struct cbe_iic_thread_regs __iomem *regs;
  40. u8 target_id;
  41. u8 eoi_stack[16];
  42. int eoi_ptr;
  43. struct irq_host *host;
  44. };
  45. static DEFINE_PER_CPU(struct iic, iic);
  46. #define IIC_NODE_COUNT 2
  47. static struct irq_host *iic_hosts[IIC_NODE_COUNT];
  48. /* Convert between "pending" bits and hw irq number */
  49. static irq_hw_number_t iic_pending_to_hwnum(struct cbe_iic_pending_bits bits)
  50. {
  51. unsigned char unit = bits.source & 0xf;
  52. if (bits.flags & CBE_IIC_IRQ_IPI)
  53. return IIC_IRQ_IPI0 | (bits.prio >> 4);
  54. else if (bits.class <= 3)
  55. return (bits.class << 4) | unit;
  56. else
  57. return IIC_IRQ_INVALID;
  58. }
  59. static void iic_mask(unsigned int irq)
  60. {
  61. }
  62. static void iic_unmask(unsigned int irq)
  63. {
  64. }
  65. static void iic_eoi(unsigned int irq)
  66. {
  67. struct iic *iic = &__get_cpu_var(iic);
  68. out_be64(&iic->regs->prio, iic->eoi_stack[--iic->eoi_ptr]);
  69. BUG_ON(iic->eoi_ptr < 0);
  70. }
  71. static struct irq_chip iic_chip = {
  72. .typename = " CELL-IIC ",
  73. .mask = iic_mask,
  74. .unmask = iic_unmask,
  75. .eoi = iic_eoi,
  76. };
  77. /* Get an IRQ number from the pending state register of the IIC */
  78. static unsigned int iic_get_irq(struct pt_regs *regs)
  79. {
  80. struct cbe_iic_pending_bits pending;
  81. struct iic *iic;
  82. iic = &__get_cpu_var(iic);
  83. *(unsigned long *) &pending =
  84. in_be64((unsigned long __iomem *) &iic->regs->pending_destr);
  85. iic->eoi_stack[++iic->eoi_ptr] = pending.prio;
  86. BUG_ON(iic->eoi_ptr > 15);
  87. if (pending.flags & CBE_IIC_IRQ_VALID)
  88. return irq_linear_revmap(iic->host,
  89. iic_pending_to_hwnum(pending));
  90. return NO_IRQ;
  91. }
  92. #ifdef CONFIG_SMP
  93. /* Use the highest interrupt priorities for IPI */
  94. static inline int iic_ipi_to_irq(int ipi)
  95. {
  96. return IIC_IRQ_IPI0 + IIC_NUM_IPIS - 1 - ipi;
  97. }
  98. static inline int iic_irq_to_ipi(int irq)
  99. {
  100. return IIC_NUM_IPIS - 1 - (irq - IIC_IRQ_IPI0);
  101. }
  102. void iic_setup_cpu(void)
  103. {
  104. out_be64(&__get_cpu_var(iic).regs->prio, 0xff);
  105. }
  106. void iic_cause_IPI(int cpu, int mesg)
  107. {
  108. out_be64(&per_cpu(iic, cpu).regs->generate, (IIC_NUM_IPIS - 1 - mesg) << 4);
  109. }
  110. u8 iic_get_target_id(int cpu)
  111. {
  112. return per_cpu(iic, cpu).target_id;
  113. }
  114. EXPORT_SYMBOL_GPL(iic_get_target_id);
  115. struct irq_host *iic_get_irq_host(int node)
  116. {
  117. if (node < 0 || node >= IIC_NODE_COUNT)
  118. return NULL;
  119. return iic_hosts[node];
  120. }
  121. EXPORT_SYMBOL_GPL(iic_get_irq_host);
  122. static irqreturn_t iic_ipi_action(int irq, void *dev_id, struct pt_regs *regs)
  123. {
  124. int ipi = (int)(long)dev_id;
  125. smp_message_recv(ipi, regs);
  126. return IRQ_HANDLED;
  127. }
  128. static void iic_request_ipi(int ipi, const char *name)
  129. {
  130. int node, virq;
  131. for (node = 0; node < IIC_NODE_COUNT; node++) {
  132. char *rname;
  133. if (iic_hosts[node] == NULL)
  134. continue;
  135. virq = irq_create_mapping(iic_hosts[node],
  136. iic_ipi_to_irq(ipi));
  137. if (virq == NO_IRQ) {
  138. printk(KERN_ERR
  139. "iic: failed to map IPI %s on node %d\n",
  140. name, node);
  141. continue;
  142. }
  143. rname = kzalloc(strlen(name) + 16, GFP_KERNEL);
  144. if (rname)
  145. sprintf(rname, "%s node %d", name, node);
  146. else
  147. rname = (char *)name;
  148. if (request_irq(virq, iic_ipi_action, IRQF_DISABLED,
  149. rname, (void *)(long)ipi))
  150. printk(KERN_ERR
  151. "iic: failed to request IPI %s on node %d\n",
  152. name, node);
  153. }
  154. }
  155. void iic_request_IPIs(void)
  156. {
  157. iic_request_ipi(PPC_MSG_CALL_FUNCTION, "IPI-call");
  158. iic_request_ipi(PPC_MSG_RESCHEDULE, "IPI-resched");
  159. #ifdef CONFIG_DEBUGGER
  160. iic_request_ipi(PPC_MSG_DEBUGGER_BREAK, "IPI-debug");
  161. #endif /* CONFIG_DEBUGGER */
  162. }
  163. #endif /* CONFIG_SMP */
  164. static int iic_host_match(struct irq_host *h, struct device_node *node)
  165. {
  166. return h->host_data != NULL && node == h->host_data;
  167. }
  168. static int iic_host_map(struct irq_host *h, unsigned int virq,
  169. irq_hw_number_t hw)
  170. {
  171. if (hw < IIC_IRQ_IPI0)
  172. set_irq_chip_and_handler(virq, &iic_chip, handle_fasteoi_irq);
  173. else
  174. set_irq_chip_and_handler(virq, &iic_chip, handle_percpu_irq);
  175. return 0;
  176. }
  177. static int iic_host_xlate(struct irq_host *h, struct device_node *ct,
  178. u32 *intspec, unsigned int intsize,
  179. irq_hw_number_t *out_hwirq, unsigned int *out_flags)
  180. {
  181. /* Currently, we don't translate anything. That needs to be fixed as
  182. * we get better defined device-trees. iic interrupts have to be
  183. * explicitely mapped by whoever needs them
  184. */
  185. return -ENODEV;
  186. }
  187. static struct irq_host_ops iic_host_ops = {
  188. .match = iic_host_match,
  189. .map = iic_host_map,
  190. .xlate = iic_host_xlate,
  191. };
  192. static void __init init_one_iic(unsigned int hw_cpu, unsigned long addr,
  193. struct irq_host *host)
  194. {
  195. /* XXX FIXME: should locate the linux CPU number from the HW cpu
  196. * number properly. We are lucky for now
  197. */
  198. struct iic *iic = &per_cpu(iic, hw_cpu);
  199. iic->regs = ioremap(addr, sizeof(struct cbe_iic_thread_regs));
  200. BUG_ON(iic->regs == NULL);
  201. iic->target_id = ((hw_cpu & 2) << 3) | ((hw_cpu & 1) ? 0xf : 0xe);
  202. iic->eoi_stack[0] = 0xff;
  203. iic->host = host;
  204. out_be64(&iic->regs->prio, 0);
  205. printk(KERN_INFO "IIC for CPU %d at %lx mapped to %p, target id 0x%x\n",
  206. hw_cpu, addr, iic->regs, iic->target_id);
  207. }
  208. static int __init setup_iic(void)
  209. {
  210. struct device_node *dn;
  211. struct resource r0, r1;
  212. struct irq_host *host;
  213. int found = 0;
  214. const u32 *np;
  215. for (dn = NULL;
  216. (dn = of_find_node_by_name(dn,"interrupt-controller")) != NULL;) {
  217. if (!device_is_compatible(dn,
  218. "IBM,CBEA-Internal-Interrupt-Controller"))
  219. continue;
  220. np = get_property(dn, "ibm,interrupt-server-ranges", NULL);
  221. if (np == NULL) {
  222. printk(KERN_WARNING "IIC: CPU association not found\n");
  223. of_node_put(dn);
  224. return -ENODEV;
  225. }
  226. if (of_address_to_resource(dn, 0, &r0) ||
  227. of_address_to_resource(dn, 1, &r1)) {
  228. printk(KERN_WARNING "IIC: Can't resolve addresses\n");
  229. of_node_put(dn);
  230. return -ENODEV;
  231. }
  232. host = NULL;
  233. if (found < IIC_NODE_COUNT) {
  234. host = irq_alloc_host(IRQ_HOST_MAP_LINEAR,
  235. IIC_SOURCE_COUNT,
  236. &iic_host_ops,
  237. IIC_IRQ_INVALID);
  238. iic_hosts[found] = host;
  239. BUG_ON(iic_hosts[found] == NULL);
  240. iic_hosts[found]->host_data = of_node_get(dn);
  241. found++;
  242. }
  243. init_one_iic(np[0], r0.start, host);
  244. init_one_iic(np[1], r1.start, host);
  245. }
  246. if (found)
  247. return 0;
  248. else
  249. return -ENODEV;
  250. }
  251. void __init iic_init_IRQ(void)
  252. {
  253. /* Discover and initialize iics */
  254. if (setup_iic() < 0)
  255. panic("IIC: Failed to initialize !\n");
  256. /* Set master interrupt handling function */
  257. ppc_md.get_irq = iic_get_irq;
  258. /* Enable on current CPU */
  259. iic_setup_cpu();
  260. }