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