sun4d_irq.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488
  1. /*
  2. * SS1000/SC2000 interrupt handling.
  3. *
  4. * Copyright (C) 1997,1998 Jakub Jelinek (jj@sunsite.mff.cuni.cz)
  5. * Heavily based on arch/sparc/kernel/irq.c.
  6. */
  7. #include <linux/kernel_stat.h>
  8. #include <linux/seq_file.h>
  9. #include <asm/timer.h>
  10. #include <asm/traps.h>
  11. #include <asm/irq.h>
  12. #include <asm/io.h>
  13. #include <asm/sbi.h>
  14. #include <asm/cacheflush.h>
  15. #include <asm/setup.h>
  16. #include "kernel.h"
  17. #include "irq.h"
  18. /* Sun4d interrupts fall roughly into two categories. SBUS and
  19. * cpu local. CPU local interrupts cover the timer interrupts
  20. * and whatnot, and we encode those as normal PILs between
  21. * 0 and 15.
  22. * SBUS interrupts are encodes as a combination of board, level and slot.
  23. */
  24. struct sun4d_handler_data {
  25. unsigned int cpuid; /* target cpu */
  26. unsigned int real_irq; /* interrupt level */
  27. };
  28. static unsigned int sun4d_encode_irq(int board, int lvl, int slot)
  29. {
  30. return (board + 1) << 5 | (lvl << 2) | slot;
  31. }
  32. struct sun4d_timer_regs {
  33. u32 l10_timer_limit;
  34. u32 l10_cur_countx;
  35. u32 l10_limit_noclear;
  36. u32 ctrl;
  37. u32 l10_cur_count;
  38. };
  39. static struct sun4d_timer_regs __iomem *sun4d_timers;
  40. #define SUN4D_TIMER_IRQ 10
  41. /* Specify which cpu handle interrupts from which board.
  42. * Index is board - value is cpu.
  43. */
  44. static unsigned char board_to_cpu[32];
  45. static int pil_to_sbus[] = {
  46. 0,
  47. 0,
  48. 1,
  49. 2,
  50. 0,
  51. 3,
  52. 0,
  53. 4,
  54. 0,
  55. 5,
  56. 0,
  57. 6,
  58. 0,
  59. 7,
  60. 0,
  61. 0,
  62. };
  63. /* Exported for sun4d_smp.c */
  64. DEFINE_SPINLOCK(sun4d_imsk_lock);
  65. /* SBUS interrupts are encoded integers including the board number
  66. * (plus one), the SBUS level, and the SBUS slot number. Sun4D
  67. * IRQ dispatch is done by:
  68. *
  69. * 1) Reading the BW local interrupt table in order to get the bus
  70. * interrupt mask.
  71. *
  72. * This table is indexed by SBUS interrupt level which can be
  73. * derived from the PIL we got interrupted on.
  74. *
  75. * 2) For each bus showing interrupt pending from #1, read the
  76. * SBI interrupt state register. This will indicate which slots
  77. * have interrupts pending for that SBUS interrupt level.
  78. *
  79. * 3) Call the genreric IRQ support.
  80. */
  81. static void sun4d_sbus_handler_irq(int sbusl)
  82. {
  83. unsigned int bus_mask;
  84. unsigned int sbino, slot;
  85. unsigned int sbil;
  86. bus_mask = bw_get_intr_mask(sbusl) & 0x3ffff;
  87. bw_clear_intr_mask(sbusl, bus_mask);
  88. sbil = (sbusl << 2);
  89. /* Loop for each pending SBI */
  90. for (sbino = 0; bus_mask; sbino++) {
  91. unsigned int idx, mask;
  92. bus_mask >>= 1;
  93. if (!(bus_mask & 1))
  94. continue;
  95. /* XXX This seems to ACK the irq twice. acquire_sbi()
  96. * XXX uses swap, therefore this writes 0xf << sbil,
  97. * XXX then later release_sbi() will write the individual
  98. * XXX bits which were set again.
  99. */
  100. mask = acquire_sbi(SBI2DEVID(sbino), 0xf << sbil);
  101. mask &= (0xf << sbil);
  102. /* Loop for each pending SBI slot */
  103. idx = 0;
  104. slot = (1 << sbil);
  105. while (mask != 0) {
  106. unsigned int pil;
  107. struct irq_bucket *p;
  108. idx++;
  109. slot <<= 1;
  110. if (!(mask & slot))
  111. continue;
  112. mask &= ~slot;
  113. pil = sun4d_encode_irq(sbino, sbil, idx);
  114. p = irq_map[pil];
  115. while (p) {
  116. struct irq_bucket *next;
  117. next = p->next;
  118. generic_handle_irq(p->irq);
  119. p = next;
  120. }
  121. release_sbi(SBI2DEVID(sbino), slot);
  122. }
  123. }
  124. }
  125. void sun4d_handler_irq(int pil, struct pt_regs *regs)
  126. {
  127. struct pt_regs *old_regs;
  128. /* SBUS IRQ level (1 - 7) */
  129. int sbusl = pil_to_sbus[pil];
  130. /* FIXME: Is this necessary?? */
  131. cc_get_ipen();
  132. cc_set_iclr(1 << pil);
  133. #ifdef CONFIG_SMP
  134. /*
  135. * Check IPI data structures after IRQ has been cleared. Hard and Soft
  136. * IRQ can happen at the same time, so both cases are always handled.
  137. */
  138. if (pil == SUN4D_IPI_IRQ)
  139. sun4d_ipi_interrupt();
  140. #endif
  141. old_regs = set_irq_regs(regs);
  142. irq_enter();
  143. if (sbusl == 0) {
  144. /* cpu interrupt */
  145. struct irq_bucket *p;
  146. p = irq_map[pil];
  147. while (p) {
  148. struct irq_bucket *next;
  149. next = p->next;
  150. generic_handle_irq(p->irq);
  151. p = next;
  152. }
  153. } else {
  154. /* SBUS interrupt */
  155. sun4d_sbus_handler_irq(sbusl);
  156. }
  157. irq_exit();
  158. set_irq_regs(old_regs);
  159. }
  160. static void sun4d_mask_irq(struct irq_data *data)
  161. {
  162. struct sun4d_handler_data *handler_data = data->handler_data;
  163. unsigned int real_irq;
  164. #ifdef CONFIG_SMP
  165. int cpuid = handler_data->cpuid;
  166. unsigned long flags;
  167. #endif
  168. real_irq = handler_data->real_irq;
  169. #ifdef CONFIG_SMP
  170. spin_lock_irqsave(&sun4d_imsk_lock, flags);
  171. cc_set_imsk_other(cpuid, cc_get_imsk_other(cpuid) | (1 << real_irq));
  172. spin_unlock_irqrestore(&sun4d_imsk_lock, flags);
  173. #else
  174. cc_set_imsk(cc_get_imsk() | (1 << real_irq));
  175. #endif
  176. }
  177. static void sun4d_unmask_irq(struct irq_data *data)
  178. {
  179. struct sun4d_handler_data *handler_data = data->handler_data;
  180. unsigned int real_irq;
  181. #ifdef CONFIG_SMP
  182. int cpuid = handler_data->cpuid;
  183. unsigned long flags;
  184. #endif
  185. real_irq = handler_data->real_irq;
  186. #ifdef CONFIG_SMP
  187. spin_lock_irqsave(&sun4d_imsk_lock, flags);
  188. cc_set_imsk_other(cpuid, cc_get_imsk_other(cpuid) | ~(1 << real_irq));
  189. spin_unlock_irqrestore(&sun4d_imsk_lock, flags);
  190. #else
  191. cc_set_imsk(cc_get_imsk() | ~(1 << real_irq));
  192. #endif
  193. }
  194. static unsigned int sun4d_startup_irq(struct irq_data *data)
  195. {
  196. irq_link(data->irq);
  197. sun4d_unmask_irq(data);
  198. return 0;
  199. }
  200. static void sun4d_shutdown_irq(struct irq_data *data)
  201. {
  202. sun4d_mask_irq(data);
  203. irq_unlink(data->irq);
  204. }
  205. struct irq_chip sun4d_irq = {
  206. .name = "sun4d",
  207. .irq_startup = sun4d_startup_irq,
  208. .irq_shutdown = sun4d_shutdown_irq,
  209. .irq_unmask = sun4d_unmask_irq,
  210. .irq_mask = sun4d_mask_irq,
  211. };
  212. #ifdef CONFIG_SMP
  213. static void sun4d_set_cpu_int(int cpu, int level)
  214. {
  215. sun4d_send_ipi(cpu, level);
  216. }
  217. static void sun4d_clear_ipi(int cpu, int level)
  218. {
  219. }
  220. static void sun4d_set_udt(int cpu)
  221. {
  222. }
  223. /* Setup IRQ distribution scheme. */
  224. void __init sun4d_distribute_irqs(void)
  225. {
  226. struct device_node *dp;
  227. int cpuid = cpu_logical_map(1);
  228. if (cpuid == -1)
  229. cpuid = cpu_logical_map(0);
  230. for_each_node_by_name(dp, "sbi") {
  231. int devid = of_getintprop_default(dp, "device-id", 0);
  232. int board = of_getintprop_default(dp, "board#", 0);
  233. board_to_cpu[board] = cpuid;
  234. set_sbi_tid(devid, cpuid << 3);
  235. }
  236. printk(KERN_ERR "All sbus IRQs directed to CPU%d\n", cpuid);
  237. }
  238. #endif
  239. static void sun4d_clear_clock_irq(void)
  240. {
  241. sbus_readl(&sun4d_timers->l10_timer_limit);
  242. }
  243. static void sun4d_load_profile_irq(int cpu, unsigned int limit)
  244. {
  245. bw_set_prof_limit(cpu, limit);
  246. }
  247. static void __init sun4d_load_profile_irqs(void)
  248. {
  249. int cpu = 0, mid;
  250. while (!cpu_find_by_instance(cpu, NULL, &mid)) {
  251. sun4d_load_profile_irq(mid >> 3, 0);
  252. cpu++;
  253. }
  254. }
  255. unsigned int sun4d_build_device_irq(struct platform_device *op,
  256. unsigned int real_irq)
  257. {
  258. struct device_node *dp = op->dev.of_node;
  259. struct device_node *io_unit, *sbi = dp->parent;
  260. const struct linux_prom_registers *regs;
  261. struct sun4d_handler_data *handler_data;
  262. unsigned int pil;
  263. unsigned int irq;
  264. int board, slot;
  265. int sbusl;
  266. irq = 0;
  267. while (sbi) {
  268. if (!strcmp(sbi->name, "sbi"))
  269. break;
  270. sbi = sbi->parent;
  271. }
  272. if (!sbi)
  273. goto err_out;
  274. regs = of_get_property(dp, "reg", NULL);
  275. if (!regs)
  276. goto err_out;
  277. slot = regs->which_io;
  278. /*
  279. * If SBI's parent is not io-unit or the io-unit lacks
  280. * a "board#" property, something is very wrong.
  281. */
  282. if (!sbi->parent || strcmp(sbi->parent->name, "io-unit")) {
  283. printk("%s: Error, parent is not io-unit.\n", sbi->full_name);
  284. goto err_out;
  285. }
  286. io_unit = sbi->parent;
  287. board = of_getintprop_default(io_unit, "board#", -1);
  288. if (board == -1) {
  289. printk("%s: Error, lacks board# property.\n", io_unit->full_name);
  290. goto err_out;
  291. }
  292. sbusl = pil_to_sbus[real_irq];
  293. if (sbusl)
  294. pil = sun4d_encode_irq(board, sbusl, slot);
  295. else
  296. pil = real_irq;
  297. irq = irq_alloc(real_irq, pil);
  298. if (irq == 0)
  299. goto err_out;
  300. handler_data = irq_get_handler_data(irq);
  301. if (unlikely(handler_data))
  302. goto err_out;
  303. handler_data = kzalloc(sizeof(struct sun4d_handler_data), GFP_ATOMIC);
  304. if (unlikely(!handler_data)) {
  305. prom_printf("IRQ: kzalloc(sun4d_handler_data) failed.\n");
  306. prom_halt();
  307. }
  308. handler_data->cpuid = board_to_cpu[board];
  309. handler_data->real_irq = real_irq;
  310. irq_set_chip_and_handler_name(irq, &sun4d_irq,
  311. handle_level_irq, "level");
  312. irq_set_handler_data(irq, handler_data);
  313. err_out:
  314. return real_irq;
  315. }
  316. static void __init sun4d_fixup_trap_table(void)
  317. {
  318. #ifdef CONFIG_SMP
  319. unsigned long flags;
  320. struct tt_entry *trap_table = &sparc_ttable[SP_TRAP_IRQ1 + (14 - 1)];
  321. /* Adjust so that we jump directly to smp4d_ticker */
  322. lvl14_save[2] += smp4d_ticker - real_irq_entry;
  323. /* For SMP we use the level 14 ticker, however the bootup code
  324. * has copied the firmware's level 14 vector into the boot cpu's
  325. * trap table, we must fix this now or we get squashed.
  326. */
  327. local_irq_save(flags);
  328. patchme_maybe_smp_msg[0] = 0x01000000; /* NOP out the branch */
  329. trap_table->inst_one = lvl14_save[0];
  330. trap_table->inst_two = lvl14_save[1];
  331. trap_table->inst_three = lvl14_save[2];
  332. trap_table->inst_four = lvl14_save[3];
  333. local_flush_cache_all();
  334. local_irq_restore(flags);
  335. #endif
  336. }
  337. static void __init sun4d_init_timers(irq_handler_t counter_fn)
  338. {
  339. struct device_node *dp;
  340. struct resource res;
  341. unsigned int irq;
  342. const u32 *reg;
  343. int err;
  344. dp = of_find_node_by_name(NULL, "cpu-unit");
  345. if (!dp) {
  346. prom_printf("sun4d_init_timers: Unable to find cpu-unit\n");
  347. prom_halt();
  348. }
  349. /* Which cpu-unit we use is arbitrary, we can view the bootbus timer
  350. * registers via any cpu's mapping. The first 'reg' property is the
  351. * bootbus.
  352. */
  353. reg = of_get_property(dp, "reg", NULL);
  354. of_node_put(dp);
  355. if (!reg) {
  356. prom_printf("sun4d_init_timers: No reg property\n");
  357. prom_halt();
  358. }
  359. res.start = reg[1];
  360. res.end = reg[2] - 1;
  361. res.flags = reg[0] & 0xff;
  362. sun4d_timers = of_ioremap(&res, BW_TIMER_LIMIT,
  363. sizeof(struct sun4d_timer_regs), "user timer");
  364. if (!sun4d_timers) {
  365. prom_printf("sun4d_init_timers: Can't map timer regs\n");
  366. prom_halt();
  367. }
  368. sbus_writel((((1000000/HZ) + 1) << 10), &sun4d_timers->l10_timer_limit);
  369. master_l10_counter = &sun4d_timers->l10_cur_count;
  370. irq = sun4d_build_device_irq(NULL, SUN4D_TIMER_IRQ);
  371. err = request_irq(irq, counter_fn, IRQF_TIMER, "timer", NULL);
  372. if (err) {
  373. prom_printf("sun4d_init_timers: request_irq() failed with %d\n",
  374. err);
  375. prom_halt();
  376. }
  377. sun4d_load_profile_irqs();
  378. sun4d_fixup_trap_table();
  379. }
  380. void __init sun4d_init_sbi_irq(void)
  381. {
  382. struct device_node *dp;
  383. int target_cpu;
  384. target_cpu = boot_cpu_id;
  385. for_each_node_by_name(dp, "sbi") {
  386. int devid = of_getintprop_default(dp, "device-id", 0);
  387. int board = of_getintprop_default(dp, "board#", 0);
  388. unsigned int mask;
  389. set_sbi_tid(devid, target_cpu << 3);
  390. board_to_cpu[board] = target_cpu;
  391. /* Get rid of pending irqs from PROM */
  392. mask = acquire_sbi(devid, 0xffffffff);
  393. if (mask) {
  394. printk(KERN_ERR "Clearing pending IRQs %08x on SBI %d\n",
  395. mask, board);
  396. release_sbi(devid, mask);
  397. }
  398. }
  399. }
  400. void __init sun4d_init_IRQ(void)
  401. {
  402. local_irq_disable();
  403. BTFIXUPSET_CALL(clear_clock_irq, sun4d_clear_clock_irq, BTFIXUPCALL_NORM);
  404. BTFIXUPSET_CALL(load_profile_irq, sun4d_load_profile_irq, BTFIXUPCALL_NORM);
  405. sparc_irq_config.init_timers = sun4d_init_timers;
  406. sparc_irq_config.build_device_irq = sun4d_build_device_irq;
  407. #ifdef CONFIG_SMP
  408. BTFIXUPSET_CALL(set_cpu_int, sun4d_set_cpu_int, BTFIXUPCALL_NORM);
  409. BTFIXUPSET_CALL(clear_cpu_int, sun4d_clear_ipi, BTFIXUPCALL_NOP);
  410. BTFIXUPSET_CALL(set_irq_udt, sun4d_set_udt, BTFIXUPCALL_NOP);
  411. #endif
  412. /* Cannot enable interrupts until OBP ticker is disabled. */
  413. }