ip27-irq.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423
  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/slab.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 apropriate
  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. } else if (pend0 & (1UL << CPU_RESCHED_B_IRQ)) {
  126. LOCAL_HUB_CLR_INTR(CPU_RESCHED_B_IRQ);
  127. } else if (pend0 & (1UL << CPU_CALL_A_IRQ)) {
  128. LOCAL_HUB_CLR_INTR(CPU_CALL_A_IRQ);
  129. smp_call_function_interrupt();
  130. } else if (pend0 & (1UL << CPU_CALL_B_IRQ)) {
  131. LOCAL_HUB_CLR_INTR(CPU_CALL_B_IRQ);
  132. smp_call_function_interrupt();
  133. } else
  134. #endif
  135. {
  136. /* "map" swlevel to irq */
  137. struct slice_data *si = cpu_data[cpu].data;
  138. irq = si->level_to_irq[swlevel];
  139. do_IRQ(irq);
  140. }
  141. LOCAL_HUB_L(PI_INT_PEND0);
  142. }
  143. static void ip27_do_irq_mask1(void)
  144. {
  145. int irq, swlevel;
  146. hubreg_t pend1, mask1;
  147. cpuid_t cpu = smp_processor_id();
  148. int pi_int_mask1 = (cputoslice(cpu) == 0) ? PI_INT_MASK1_A : PI_INT_MASK1_B;
  149. struct slice_data *si = cpu_data[cpu].data;
  150. /* copied from Irix intpend0() */
  151. pend1 = LOCAL_HUB_L(PI_INT_PEND1);
  152. mask1 = LOCAL_HUB_L(pi_int_mask1);
  153. pend1 &= mask1; /* Pick intrs we should look at */
  154. if (!pend1)
  155. return;
  156. swlevel = ms1bit(pend1);
  157. /* "map" swlevel to irq */
  158. irq = si->level_to_irq[swlevel];
  159. LOCAL_HUB_CLR_INTR(swlevel);
  160. do_IRQ(irq);
  161. LOCAL_HUB_L(PI_INT_PEND1);
  162. }
  163. static void ip27_prof_timer(void)
  164. {
  165. panic("CPU %d got a profiling interrupt", smp_processor_id());
  166. }
  167. static void ip27_hub_error(void)
  168. {
  169. panic("CPU %d got a hub error interrupt", smp_processor_id());
  170. }
  171. static int intr_connect_level(int cpu, int bit)
  172. {
  173. nasid_t nasid = COMPACT_TO_NASID_NODEID(cpu_to_node(cpu));
  174. struct slice_data *si = cpu_data[cpu].data;
  175. set_bit(bit, si->irq_enable_mask);
  176. if (!cputoslice(cpu)) {
  177. REMOTE_HUB_S(nasid, PI_INT_MASK0_A, si->irq_enable_mask[0]);
  178. REMOTE_HUB_S(nasid, PI_INT_MASK1_A, si->irq_enable_mask[1]);
  179. } else {
  180. REMOTE_HUB_S(nasid, PI_INT_MASK0_B, si->irq_enable_mask[0]);
  181. REMOTE_HUB_S(nasid, PI_INT_MASK1_B, si->irq_enable_mask[1]);
  182. }
  183. return 0;
  184. }
  185. static int intr_disconnect_level(int cpu, int bit)
  186. {
  187. nasid_t nasid = COMPACT_TO_NASID_NODEID(cpu_to_node(cpu));
  188. struct slice_data *si = cpu_data[cpu].data;
  189. clear_bit(bit, si->irq_enable_mask);
  190. if (!cputoslice(cpu)) {
  191. REMOTE_HUB_S(nasid, PI_INT_MASK0_A, si->irq_enable_mask[0]);
  192. REMOTE_HUB_S(nasid, PI_INT_MASK1_A, si->irq_enable_mask[1]);
  193. } else {
  194. REMOTE_HUB_S(nasid, PI_INT_MASK0_B, si->irq_enable_mask[0]);
  195. REMOTE_HUB_S(nasid, PI_INT_MASK1_B, si->irq_enable_mask[1]);
  196. }
  197. return 0;
  198. }
  199. /* Startup one of the (PCI ...) IRQs routes over a bridge. */
  200. static unsigned int startup_bridge_irq(unsigned int irq)
  201. {
  202. struct bridge_controller *bc;
  203. bridgereg_t device;
  204. bridge_t *bridge;
  205. int pin, swlevel;
  206. cpuid_t cpu;
  207. pin = SLOT_FROM_PCI_IRQ(irq);
  208. bc = IRQ_TO_BRIDGE(irq);
  209. bridge = bc->base;
  210. pr_debug("bridge_startup(): irq= 0x%x pin=%d\n", irq, pin);
  211. /*
  212. * "map" irq to a swlevel greater than 6 since the first 6 bits
  213. * of INT_PEND0 are taken
  214. */
  215. swlevel = find_level(&cpu, irq);
  216. bridge->b_int_addr[pin].addr = (0x20000 | swlevel | (bc->nasid << 8));
  217. bridge->b_int_enable |= (1 << pin);
  218. bridge->b_int_enable |= 0x7ffffe00; /* more stuff in int_enable */
  219. /*
  220. * Enable sending of an interrupt clear packt to the hub on a high to
  221. * low transition of the interrupt pin.
  222. *
  223. * IRIX sets additional bits in the address which are documented as
  224. * reserved in the bridge docs.
  225. */
  226. bridge->b_int_mode |= (1UL << pin);
  227. /*
  228. * We assume the bridge to have a 1:1 mapping between devices
  229. * (slots) and intr pins.
  230. */
  231. device = bridge->b_int_device;
  232. device &= ~(7 << (pin*3));
  233. device |= (pin << (pin*3));
  234. bridge->b_int_device = device;
  235. bridge->b_wid_tflush;
  236. intr_connect_level(cpu, swlevel);
  237. return 0; /* Never anything pending. */
  238. }
  239. /* Shutdown one of the (PCI ...) IRQs routes over a bridge. */
  240. static void shutdown_bridge_irq(unsigned int irq)
  241. {
  242. struct bridge_controller *bc = IRQ_TO_BRIDGE(irq);
  243. bridge_t *bridge = bc->base;
  244. int pin, swlevel;
  245. cpuid_t cpu;
  246. pr_debug("bridge_shutdown: irq 0x%x\n", irq);
  247. pin = SLOT_FROM_PCI_IRQ(irq);
  248. /*
  249. * map irq to a swlevel greater than 6 since the first 6 bits
  250. * of INT_PEND0 are taken
  251. */
  252. swlevel = find_level(&cpu, irq);
  253. intr_disconnect_level(cpu, swlevel);
  254. bridge->b_int_enable &= ~(1 << pin);
  255. bridge->b_wid_tflush;
  256. }
  257. static inline void enable_bridge_irq(unsigned int irq)
  258. {
  259. cpuid_t cpu;
  260. int swlevel;
  261. swlevel = find_level(&cpu, irq); /* Criminal offence */
  262. intr_connect_level(cpu, swlevel);
  263. }
  264. static inline void disable_bridge_irq(unsigned int irq)
  265. {
  266. cpuid_t cpu;
  267. int swlevel;
  268. swlevel = find_level(&cpu, irq); /* Criminal offence */
  269. intr_disconnect_level(cpu, swlevel);
  270. }
  271. static struct irq_chip bridge_irq_type = {
  272. .name = "bridge",
  273. .startup = startup_bridge_irq,
  274. .shutdown = shutdown_bridge_irq,
  275. .ack = disable_bridge_irq,
  276. .mask = disable_bridge_irq,
  277. .mask_ack = disable_bridge_irq,
  278. .unmask = enable_bridge_irq,
  279. };
  280. void __devinit register_bridge_irq(unsigned int irq)
  281. {
  282. set_irq_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. }