sun4d_irq.c 11 KB

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