ip27-irq.c 11 KB

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