ip27-irq.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428
  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. unsigned long flags;
  176. set_bit(bit, si->irq_enable_mask);
  177. local_irq_save(flags);
  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. local_irq_restore(flags);
  186. return 0;
  187. }
  188. static int intr_disconnect_level(int cpu, int bit)
  189. {
  190. nasid_t nasid = COMPACT_TO_NASID_NODEID(cpu_to_node(cpu));
  191. struct slice_data *si = cpu_data[cpu].data;
  192. clear_bit(bit, si->irq_enable_mask);
  193. if (!cputoslice(cpu)) {
  194. REMOTE_HUB_S(nasid, PI_INT_MASK0_A, si->irq_enable_mask[0]);
  195. REMOTE_HUB_S(nasid, PI_INT_MASK1_A, si->irq_enable_mask[1]);
  196. } else {
  197. REMOTE_HUB_S(nasid, PI_INT_MASK0_B, si->irq_enable_mask[0]);
  198. REMOTE_HUB_S(nasid, PI_INT_MASK1_B, si->irq_enable_mask[1]);
  199. }
  200. return 0;
  201. }
  202. /* Startup one of the (PCI ...) IRQs routes over a bridge. */
  203. static unsigned int startup_bridge_irq(unsigned int irq)
  204. {
  205. struct bridge_controller *bc;
  206. bridgereg_t device;
  207. bridge_t *bridge;
  208. int pin, swlevel;
  209. cpuid_t cpu;
  210. pin = SLOT_FROM_PCI_IRQ(irq);
  211. bc = IRQ_TO_BRIDGE(irq);
  212. bridge = bc->base;
  213. pr_debug("bridge_startup(): irq= 0x%x pin=%d\n", irq, pin);
  214. /*
  215. * "map" irq to a swlevel greater than 6 since the first 6 bits
  216. * of INT_PEND0 are taken
  217. */
  218. swlevel = find_level(&cpu, irq);
  219. bridge->b_int_addr[pin].addr = (0x20000 | swlevel | (bc->nasid << 8));
  220. bridge->b_int_enable |= (1 << pin);
  221. bridge->b_int_enable |= 0x7ffffe00; /* more stuff in int_enable */
  222. /*
  223. * Enable sending of an interrupt clear packt to the hub on a high to
  224. * low transition of the interrupt pin.
  225. *
  226. * IRIX sets additional bits in the address which are documented as
  227. * reserved in the bridge docs.
  228. */
  229. bridge->b_int_mode |= (1UL << pin);
  230. /*
  231. * We assume the bridge to have a 1:1 mapping between devices
  232. * (slots) and intr pins.
  233. */
  234. device = bridge->b_int_device;
  235. device &= ~(7 << (pin*3));
  236. device |= (pin << (pin*3));
  237. bridge->b_int_device = device;
  238. bridge->b_wid_tflush;
  239. return 0; /* Never anything pending. */
  240. }
  241. /* Shutdown one of the (PCI ...) IRQs routes over a bridge. */
  242. static void shutdown_bridge_irq(unsigned int irq)
  243. {
  244. struct bridge_controller *bc = IRQ_TO_BRIDGE(irq);
  245. struct hub_data *hub = hub_data(cpu_to_node(bc->irq_cpu));
  246. bridge_t *bridge = bc->base;
  247. int pin, swlevel;
  248. cpuid_t cpu;
  249. pr_debug("bridge_shutdown: irq 0x%x\n", irq);
  250. pin = SLOT_FROM_PCI_IRQ(irq);
  251. /*
  252. * map irq to a swlevel greater than 6 since the first 6 bits
  253. * of INT_PEND0 are taken
  254. */
  255. swlevel = find_level(&cpu, irq);
  256. intr_disconnect_level(cpu, swlevel);
  257. __clear_bit(swlevel, hub->irq_alloc_mask);
  258. bridge->b_int_enable &= ~(1 << pin);
  259. bridge->b_wid_tflush;
  260. }
  261. static inline void enable_bridge_irq(unsigned int irq)
  262. {
  263. cpuid_t cpu;
  264. int swlevel;
  265. swlevel = find_level(&cpu, irq); /* Criminal offence */
  266. intr_connect_level(cpu, swlevel);
  267. }
  268. static inline void disable_bridge_irq(unsigned int irq)
  269. {
  270. cpuid_t cpu;
  271. int swlevel;
  272. swlevel = find_level(&cpu, irq); /* Criminal offence */
  273. intr_disconnect_level(cpu, swlevel);
  274. }
  275. static struct irq_chip bridge_irq_type = {
  276. .name = "bridge",
  277. .startup = startup_bridge_irq,
  278. .shutdown = shutdown_bridge_irq,
  279. .ack = disable_bridge_irq,
  280. .mask = disable_bridge_irq,
  281. .mask_ack = disable_bridge_irq,
  282. .unmask = enable_bridge_irq,
  283. };
  284. void __devinit register_bridge_irq(unsigned int irq)
  285. {
  286. set_irq_chip_and_handler(irq, &bridge_irq_type, handle_level_irq);
  287. }
  288. int __devinit request_bridge_irq(struct bridge_controller *bc)
  289. {
  290. int irq = allocate_irqno();
  291. int swlevel, cpu;
  292. nasid_t nasid;
  293. if (irq < 0)
  294. return irq;
  295. /*
  296. * "map" irq to a swlevel greater than 6 since the first 6 bits
  297. * of INT_PEND0 are taken
  298. */
  299. cpu = bc->irq_cpu;
  300. swlevel = alloc_level(cpu, irq);
  301. if (unlikely(swlevel < 0)) {
  302. free_irqno(irq);
  303. return -EAGAIN;
  304. }
  305. /* Make sure it's not already pending when we connect it. */
  306. nasid = COMPACT_TO_NASID_NODEID(cpu_to_node(cpu));
  307. REMOTE_HUB_CLR_INTR(nasid, swlevel);
  308. intr_connect_level(cpu, swlevel);
  309. register_bridge_irq(irq);
  310. return irq;
  311. }
  312. extern void ip27_rt_timer_interrupt(void);
  313. asmlinkage void plat_irq_dispatch(void)
  314. {
  315. unsigned long pending = read_c0_cause() & read_c0_status();
  316. if (pending & CAUSEF_IP4)
  317. ip27_rt_timer_interrupt();
  318. else if (pending & CAUSEF_IP2) /* PI_INT_PEND_0 or CC_PEND_{A|B} */
  319. ip27_do_irq_mask0();
  320. else if (pending & CAUSEF_IP3) /* PI_INT_PEND_1 */
  321. ip27_do_irq_mask1();
  322. else if (pending & CAUSEF_IP5)
  323. ip27_prof_timer();
  324. else if (pending & CAUSEF_IP6)
  325. ip27_hub_error();
  326. }
  327. void __init arch_init_irq(void)
  328. {
  329. }
  330. void install_ipi(void)
  331. {
  332. int slice = LOCAL_HUB_L(PI_CPU_NUM);
  333. int cpu = smp_processor_id();
  334. struct slice_data *si = cpu_data[cpu].data;
  335. struct hub_data *hub = hub_data(cpu_to_node(cpu));
  336. int resched, call;
  337. resched = CPU_RESCHED_A_IRQ + slice;
  338. __set_bit(resched, hub->irq_alloc_mask);
  339. __set_bit(resched, si->irq_enable_mask);
  340. LOCAL_HUB_CLR_INTR(resched);
  341. call = CPU_CALL_A_IRQ + slice;
  342. __set_bit(call, hub->irq_alloc_mask);
  343. __set_bit(call, si->irq_enable_mask);
  344. LOCAL_HUB_CLR_INTR(call);
  345. if (slice == 0) {
  346. LOCAL_HUB_S(PI_INT_MASK0_A, si->irq_enable_mask[0]);
  347. LOCAL_HUB_S(PI_INT_MASK1_A, si->irq_enable_mask[1]);
  348. } else {
  349. LOCAL_HUB_S(PI_INT_MASK0_B, si->irq_enable_mask[0]);
  350. LOCAL_HUB_S(PI_INT_MASK1_B, si->irq_enable_mask[1]);
  351. }
  352. }