ip27-irq.c 11 KB

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