ip27-irq.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424
  1. /*
  2. * ip27-irq.c: Highlevel interrupt handling for IP27 architecture.
  3. *
  4. * Copyright (C) 1999, 2000 Ralf Baechle (ralf@gnu.org)
  5. * Copyright (C) 1999, 2000 Silicon Graphics, Inc.
  6. * Copyright (C) 1999 - 2001 Kanoj Sarcar
  7. */
  8. #undef DEBUG
  9. #include <linux/init.h>
  10. #include <linux/irq.h>
  11. #include <linux/errno.h>
  12. #include <linux/signal.h>
  13. #include <linux/sched.h>
  14. #include <linux/types.h>
  15. #include <linux/interrupt.h>
  16. #include <linux/ioport.h>
  17. #include <linux/timex.h>
  18. #include <linux/smp.h>
  19. #include <linux/random.h>
  20. #include <linux/kernel.h>
  21. #include <linux/kernel_stat.h>
  22. #include <linux/delay.h>
  23. #include <linux/bitops.h>
  24. #include <asm/bootinfo.h>
  25. #include <asm/io.h>
  26. #include <asm/mipsregs.h>
  27. #include <asm/system.h>
  28. #include <asm/processor.h>
  29. #include <asm/pci/bridge.h>
  30. #include <asm/sn/addrs.h>
  31. #include <asm/sn/agent.h>
  32. #include <asm/sn/arch.h>
  33. #include <asm/sn/hub.h>
  34. #include <asm/sn/intr.h>
  35. /*
  36. * Linux has a controller-independent x86 interrupt architecture.
  37. * every controller has a 'controller-template', that is used
  38. * by the main code to do the right thing. Each driver-visible
  39. * interrupt source is transparently wired to the appropriate
  40. * controller. Thus drivers need not be aware of the
  41. * interrupt-controller.
  42. *
  43. * Various interrupt controllers we handle: 8259 PIC, SMP IO-APIC,
  44. * PIIX4's internal 8259 PIC and SGI's Visual Workstation Cobalt (IO-)APIC.
  45. * (IO-APICs assumed to be messaging to Pentium local-APICs)
  46. *
  47. * the code is designed to be easily extended with new/different
  48. * interrupt controllers, without having to do assembly magic.
  49. */
  50. extern asmlinkage void ip27_irq(void);
  51. extern struct bridge_controller *irq_to_bridge[];
  52. extern int irq_to_slot[];
  53. /*
  54. * use these macros to get the encoded nasid and widget id
  55. * from the irq value
  56. */
  57. #define IRQ_TO_BRIDGE(i) irq_to_bridge[(i)]
  58. #define SLOT_FROM_PCI_IRQ(i) irq_to_slot[i]
  59. static inline int alloc_level(int cpu, int irq)
  60. {
  61. struct hub_data *hub = hub_data(cpu_to_node(cpu));
  62. struct slice_data *si = cpu_data[cpu].data;
  63. int level;
  64. level = find_first_zero_bit(hub->irq_alloc_mask, LEVELS_PER_SLICE);
  65. if (level >= LEVELS_PER_SLICE)
  66. panic("Cpu %d flooded with devices\n", cpu);
  67. __set_bit(level, hub->irq_alloc_mask);
  68. si->level_to_irq[level] = irq;
  69. return level;
  70. }
  71. static inline int find_level(cpuid_t *cpunum, int irq)
  72. {
  73. int cpu, i;
  74. for_each_online_cpu(cpu) {
  75. struct slice_data *si = cpu_data[cpu].data;
  76. for (i = BASE_PCI_IRQ; i < LEVELS_PER_SLICE; i++)
  77. if (si->level_to_irq[i] == irq) {
  78. *cpunum = cpu;
  79. return i;
  80. }
  81. }
  82. panic("Could not identify cpu/level for irq %d\n", irq);
  83. }
  84. /*
  85. * Find first bit set
  86. */
  87. static int ms1bit(unsigned long x)
  88. {
  89. int b = 0, s;
  90. s = 16; if (x >> 16 == 0) s = 0; b += s; x >>= s;
  91. s = 8; if (x >> 8 == 0) s = 0; b += s; x >>= s;
  92. s = 4; if (x >> 4 == 0) s = 0; b += s; x >>= s;
  93. s = 2; if (x >> 2 == 0) s = 0; b += s; x >>= s;
  94. s = 1; if (x >> 1 == 0) s = 0; b += s;
  95. return b;
  96. }
  97. /*
  98. * This code is unnecessarily complex, because we do IRQF_DISABLED
  99. * intr enabling. Basically, once we grab the set of intrs we need
  100. * to service, we must mask _all_ these interrupts; firstly, to make
  101. * sure the same intr does not intr again, causing recursion that
  102. * can lead to stack overflow. Secondly, we can not just mask the
  103. * one intr we are do_IRQing, because the non-masked intrs in the
  104. * first set might intr again, causing multiple servicings of the
  105. * same intr. This effect is mostly seen for intercpu intrs.
  106. * Kanoj 05.13.00
  107. */
  108. static void ip27_do_irq_mask0(void)
  109. {
  110. int irq, swlevel;
  111. hubreg_t pend0, mask0;
  112. cpuid_t cpu = smp_processor_id();
  113. int pi_int_mask0 =
  114. (cputoslice(cpu) == 0) ? PI_INT_MASK0_A : PI_INT_MASK0_B;
  115. /* copied from Irix intpend0() */
  116. pend0 = LOCAL_HUB_L(PI_INT_PEND0);
  117. mask0 = LOCAL_HUB_L(pi_int_mask0);
  118. pend0 &= mask0; /* Pick intrs we should look at */
  119. if (!pend0)
  120. return;
  121. swlevel = ms1bit(pend0);
  122. #ifdef CONFIG_SMP
  123. if (pend0 & (1UL << CPU_RESCHED_A_IRQ)) {
  124. LOCAL_HUB_CLR_INTR(CPU_RESCHED_A_IRQ);
  125. scheduler_ipi();
  126. } else if (pend0 & (1UL << CPU_RESCHED_B_IRQ)) {
  127. LOCAL_HUB_CLR_INTR(CPU_RESCHED_B_IRQ);
  128. scheduler_ipi();
  129. } else if (pend0 & (1UL << CPU_CALL_A_IRQ)) {
  130. LOCAL_HUB_CLR_INTR(CPU_CALL_A_IRQ);
  131. smp_call_function_interrupt();
  132. } else if (pend0 & (1UL << CPU_CALL_B_IRQ)) {
  133. LOCAL_HUB_CLR_INTR(CPU_CALL_B_IRQ);
  134. smp_call_function_interrupt();
  135. } else
  136. #endif
  137. {
  138. /* "map" swlevel to irq */
  139. struct slice_data *si = cpu_data[cpu].data;
  140. irq = si->level_to_irq[swlevel];
  141. do_IRQ(irq);
  142. }
  143. LOCAL_HUB_L(PI_INT_PEND0);
  144. }
  145. static void ip27_do_irq_mask1(void)
  146. {
  147. int irq, swlevel;
  148. hubreg_t pend1, mask1;
  149. cpuid_t cpu = smp_processor_id();
  150. int pi_int_mask1 = (cputoslice(cpu) == 0) ? PI_INT_MASK1_A : PI_INT_MASK1_B;
  151. struct slice_data *si = cpu_data[cpu].data;
  152. /* copied from Irix intpend0() */
  153. pend1 = LOCAL_HUB_L(PI_INT_PEND1);
  154. mask1 = LOCAL_HUB_L(pi_int_mask1);
  155. pend1 &= mask1; /* Pick intrs we should look at */
  156. if (!pend1)
  157. return;
  158. swlevel = ms1bit(pend1);
  159. /* "map" swlevel to irq */
  160. irq = si->level_to_irq[swlevel];
  161. LOCAL_HUB_CLR_INTR(swlevel);
  162. do_IRQ(irq);
  163. LOCAL_HUB_L(PI_INT_PEND1);
  164. }
  165. static void ip27_prof_timer(void)
  166. {
  167. panic("CPU %d got a profiling interrupt", smp_processor_id());
  168. }
  169. static void ip27_hub_error(void)
  170. {
  171. panic("CPU %d got a hub error interrupt", smp_processor_id());
  172. }
  173. static int intr_connect_level(int cpu, int bit)
  174. {
  175. nasid_t nasid = COMPACT_TO_NASID_NODEID(cpu_to_node(cpu));
  176. struct slice_data *si = cpu_data[cpu].data;
  177. set_bit(bit, si->irq_enable_mask);
  178. if (!cputoslice(cpu)) {
  179. REMOTE_HUB_S(nasid, PI_INT_MASK0_A, si->irq_enable_mask[0]);
  180. REMOTE_HUB_S(nasid, PI_INT_MASK1_A, si->irq_enable_mask[1]);
  181. } else {
  182. REMOTE_HUB_S(nasid, PI_INT_MASK0_B, si->irq_enable_mask[0]);
  183. REMOTE_HUB_S(nasid, PI_INT_MASK1_B, si->irq_enable_mask[1]);
  184. }
  185. return 0;
  186. }
  187. static int intr_disconnect_level(int cpu, int bit)
  188. {
  189. nasid_t nasid = COMPACT_TO_NASID_NODEID(cpu_to_node(cpu));
  190. struct slice_data *si = cpu_data[cpu].data;
  191. clear_bit(bit, si->irq_enable_mask);
  192. if (!cputoslice(cpu)) {
  193. REMOTE_HUB_S(nasid, PI_INT_MASK0_A, si->irq_enable_mask[0]);
  194. REMOTE_HUB_S(nasid, PI_INT_MASK1_A, si->irq_enable_mask[1]);
  195. } else {
  196. REMOTE_HUB_S(nasid, PI_INT_MASK0_B, si->irq_enable_mask[0]);
  197. REMOTE_HUB_S(nasid, PI_INT_MASK1_B, si->irq_enable_mask[1]);
  198. }
  199. return 0;
  200. }
  201. /* Startup one of the (PCI ...) IRQs routes over a bridge. */
  202. static unsigned int startup_bridge_irq(struct irq_data *d)
  203. {
  204. struct bridge_controller *bc;
  205. bridgereg_t device;
  206. bridge_t *bridge;
  207. int pin, swlevel;
  208. cpuid_t cpu;
  209. pin = SLOT_FROM_PCI_IRQ(d->irq);
  210. bc = IRQ_TO_BRIDGE(d->irq);
  211. bridge = bc->base;
  212. pr_debug("bridge_startup(): irq= 0x%x pin=%d\n", d->irq, pin);
  213. /*
  214. * "map" irq to a swlevel greater than 6 since the first 6 bits
  215. * of INT_PEND0 are taken
  216. */
  217. swlevel = find_level(&cpu, d->irq);
  218. bridge->b_int_addr[pin].addr = (0x20000 | swlevel | (bc->nasid << 8));
  219. bridge->b_int_enable |= (1 << pin);
  220. bridge->b_int_enable |= 0x7ffffe00; /* more stuff in int_enable */
  221. /*
  222. * Enable sending of an interrupt clear packt to the hub on a high to
  223. * low transition of the interrupt pin.
  224. *
  225. * IRIX sets additional bits in the address which are documented as
  226. * reserved in the bridge docs.
  227. */
  228. bridge->b_int_mode |= (1UL << pin);
  229. /*
  230. * We assume the bridge to have a 1:1 mapping between devices
  231. * (slots) and intr pins.
  232. */
  233. device = bridge->b_int_device;
  234. device &= ~(7 << (pin*3));
  235. device |= (pin << (pin*3));
  236. bridge->b_int_device = device;
  237. bridge->b_wid_tflush;
  238. intr_connect_level(cpu, swlevel);
  239. return 0; /* Never anything pending. */
  240. }
  241. /* Shutdown one of the (PCI ...) IRQs routes over a bridge. */
  242. static void shutdown_bridge_irq(struct irq_data *d)
  243. {
  244. struct bridge_controller *bc = IRQ_TO_BRIDGE(d->irq);
  245. bridge_t *bridge = bc->base;
  246. int pin, swlevel;
  247. cpuid_t cpu;
  248. pr_debug("bridge_shutdown: irq 0x%x\n", d->irq);
  249. pin = SLOT_FROM_PCI_IRQ(d->irq);
  250. /*
  251. * map irq to a swlevel greater than 6 since the first 6 bits
  252. * of INT_PEND0 are taken
  253. */
  254. swlevel = find_level(&cpu, d->irq);
  255. intr_disconnect_level(cpu, swlevel);
  256. bridge->b_int_enable &= ~(1 << pin);
  257. bridge->b_wid_tflush;
  258. }
  259. static inline void enable_bridge_irq(struct irq_data *d)
  260. {
  261. cpuid_t cpu;
  262. int swlevel;
  263. swlevel = find_level(&cpu, d->irq); /* Criminal offence */
  264. intr_connect_level(cpu, swlevel);
  265. }
  266. static inline void disable_bridge_irq(struct irq_data *d)
  267. {
  268. cpuid_t cpu;
  269. int swlevel;
  270. swlevel = find_level(&cpu, d->irq); /* Criminal offence */
  271. intr_disconnect_level(cpu, swlevel);
  272. }
  273. static struct irq_chip bridge_irq_type = {
  274. .name = "bridge",
  275. .irq_startup = startup_bridge_irq,
  276. .irq_shutdown = shutdown_bridge_irq,
  277. .irq_mask = disable_bridge_irq,
  278. .irq_unmask = enable_bridge_irq,
  279. };
  280. void __devinit register_bridge_irq(unsigned int irq)
  281. {
  282. irq_set_chip_and_handler(irq, &bridge_irq_type, handle_level_irq);
  283. }
  284. int __devinit request_bridge_irq(struct bridge_controller *bc)
  285. {
  286. int irq = allocate_irqno();
  287. int swlevel, cpu;
  288. nasid_t nasid;
  289. if (irq < 0)
  290. return irq;
  291. /*
  292. * "map" irq to a swlevel greater than 6 since the first 6 bits
  293. * of INT_PEND0 are taken
  294. */
  295. cpu = bc->irq_cpu;
  296. swlevel = alloc_level(cpu, irq);
  297. if (unlikely(swlevel < 0)) {
  298. free_irqno(irq);
  299. return -EAGAIN;
  300. }
  301. /* Make sure it's not already pending when we connect it. */
  302. nasid = COMPACT_TO_NASID_NODEID(cpu_to_node(cpu));
  303. REMOTE_HUB_CLR_INTR(nasid, swlevel);
  304. intr_connect_level(cpu, swlevel);
  305. register_bridge_irq(irq);
  306. return irq;
  307. }
  308. asmlinkage void plat_irq_dispatch(void)
  309. {
  310. unsigned long pending = read_c0_cause() & read_c0_status();
  311. extern unsigned int rt_timer_irq;
  312. if (pending & CAUSEF_IP4)
  313. do_IRQ(rt_timer_irq);
  314. else if (pending & CAUSEF_IP2) /* PI_INT_PEND_0 or CC_PEND_{A|B} */
  315. ip27_do_irq_mask0();
  316. else if (pending & CAUSEF_IP3) /* PI_INT_PEND_1 */
  317. ip27_do_irq_mask1();
  318. else if (pending & CAUSEF_IP5)
  319. ip27_prof_timer();
  320. else if (pending & CAUSEF_IP6)
  321. ip27_hub_error();
  322. }
  323. void __init arch_init_irq(void)
  324. {
  325. }
  326. void install_ipi(void)
  327. {
  328. int slice = LOCAL_HUB_L(PI_CPU_NUM);
  329. int cpu = smp_processor_id();
  330. struct slice_data *si = cpu_data[cpu].data;
  331. struct hub_data *hub = hub_data(cpu_to_node(cpu));
  332. int resched, call;
  333. resched = CPU_RESCHED_A_IRQ + slice;
  334. __set_bit(resched, hub->irq_alloc_mask);
  335. __set_bit(resched, si->irq_enable_mask);
  336. LOCAL_HUB_CLR_INTR(resched);
  337. call = CPU_CALL_A_IRQ + slice;
  338. __set_bit(call, hub->irq_alloc_mask);
  339. __set_bit(call, si->irq_enable_mask);
  340. LOCAL_HUB_CLR_INTR(call);
  341. if (slice == 0) {
  342. LOCAL_HUB_S(PI_INT_MASK0_A, si->irq_enable_mask[0]);
  343. LOCAL_HUB_S(PI_INT_MASK1_A, si->irq_enable_mask[1]);
  344. } else {
  345. LOCAL_HUB_S(PI_INT_MASK0_B, si->irq_enable_mask[0]);
  346. LOCAL_HUB_S(PI_INT_MASK1_B, si->irq_enable_mask[1]);
  347. }
  348. }