sun4d_irq.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479
  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. old_regs = set_irq_regs(regs);
  134. irq_enter();
  135. if (sbusl == 0) {
  136. /* cpu interrupt */
  137. struct irq_bucket *p;
  138. p = irq_map[pil];
  139. while (p) {
  140. struct irq_bucket *next;
  141. next = p->next;
  142. generic_handle_irq(p->irq);
  143. p = next;
  144. }
  145. } else {
  146. /* SBUS interrupt */
  147. sun4d_sbus_handler_irq(sbusl);
  148. }
  149. irq_exit();
  150. set_irq_regs(old_regs);
  151. }
  152. static void sun4d_mask_irq(struct irq_data *data)
  153. {
  154. struct sun4d_handler_data *handler_data = data->handler_data;
  155. unsigned int real_irq;
  156. #ifdef CONFIG_SMP
  157. int cpuid = handler_data->cpuid;
  158. unsigned long flags;
  159. #endif
  160. real_irq = handler_data->real_irq;
  161. #ifdef CONFIG_SMP
  162. spin_lock_irqsave(&sun4d_imsk_lock, flags);
  163. cc_set_imsk_other(cpuid, cc_get_imsk_other(cpuid) | (1 << real_irq));
  164. spin_unlock_irqrestore(&sun4d_imsk_lock, flags);
  165. #else
  166. cc_set_imsk(cc_get_imsk() | (1 << real_irq));
  167. #endif
  168. }
  169. static void sun4d_unmask_irq(struct irq_data *data)
  170. {
  171. struct sun4d_handler_data *handler_data = data->handler_data;
  172. unsigned int real_irq;
  173. #ifdef CONFIG_SMP
  174. int cpuid = handler_data->cpuid;
  175. unsigned long flags;
  176. #endif
  177. real_irq = handler_data->real_irq;
  178. #ifdef CONFIG_SMP
  179. spin_lock_irqsave(&sun4d_imsk_lock, flags);
  180. cc_set_imsk_other(cpuid, cc_get_imsk_other(cpuid) | ~(1 << real_irq));
  181. spin_unlock_irqrestore(&sun4d_imsk_lock, flags);
  182. #else
  183. cc_set_imsk(cc_get_imsk() | ~(1 << real_irq));
  184. #endif
  185. }
  186. static unsigned int sun4d_startup_irq(struct irq_data *data)
  187. {
  188. irq_link(data->irq);
  189. sun4d_unmask_irq(data);
  190. return 0;
  191. }
  192. static void sun4d_shutdown_irq(struct irq_data *data)
  193. {
  194. sun4d_mask_irq(data);
  195. irq_unlink(data->irq);
  196. }
  197. struct irq_chip sun4d_irq = {
  198. .name = "sun4d",
  199. .irq_startup = sun4d_startup_irq,
  200. .irq_shutdown = sun4d_shutdown_irq,
  201. .irq_unmask = sun4d_unmask_irq,
  202. .irq_mask = sun4d_mask_irq,
  203. };
  204. #ifdef CONFIG_SMP
  205. static void sun4d_set_cpu_int(int cpu, int level)
  206. {
  207. sun4d_send_ipi(cpu, level);
  208. }
  209. static void sun4d_clear_ipi(int cpu, int level)
  210. {
  211. }
  212. static void sun4d_set_udt(int cpu)
  213. {
  214. }
  215. /* Setup IRQ distribution scheme. */
  216. void __init sun4d_distribute_irqs(void)
  217. {
  218. struct device_node *dp;
  219. int cpuid = cpu_logical_map(1);
  220. if (cpuid == -1)
  221. cpuid = cpu_logical_map(0);
  222. for_each_node_by_name(dp, "sbi") {
  223. int devid = of_getintprop_default(dp, "device-id", 0);
  224. int board = of_getintprop_default(dp, "board#", 0);
  225. board_to_cpu[board] = cpuid;
  226. set_sbi_tid(devid, cpuid << 3);
  227. }
  228. printk(KERN_ERR "All sbus IRQs directed to CPU%d\n", cpuid);
  229. }
  230. #endif
  231. static void sun4d_clear_clock_irq(void)
  232. {
  233. sbus_readl(&sun4d_timers->l10_timer_limit);
  234. }
  235. static void sun4d_load_profile_irq(int cpu, unsigned int limit)
  236. {
  237. bw_set_prof_limit(cpu, limit);
  238. }
  239. static void __init sun4d_load_profile_irqs(void)
  240. {
  241. int cpu = 0, mid;
  242. while (!cpu_find_by_instance(cpu, NULL, &mid)) {
  243. sun4d_load_profile_irq(mid >> 3, 0);
  244. cpu++;
  245. }
  246. }
  247. unsigned int sun4d_build_device_irq(struct platform_device *op,
  248. unsigned int real_irq)
  249. {
  250. struct device_node *dp = op->dev.of_node;
  251. struct device_node *io_unit, *sbi = dp->parent;
  252. const struct linux_prom_registers *regs;
  253. struct sun4d_handler_data *handler_data;
  254. unsigned int pil;
  255. unsigned int irq;
  256. int board, slot;
  257. int sbusl;
  258. irq = 0;
  259. while (sbi) {
  260. if (!strcmp(sbi->name, "sbi"))
  261. break;
  262. sbi = sbi->parent;
  263. }
  264. if (!sbi)
  265. goto err_out;
  266. regs = of_get_property(dp, "reg", NULL);
  267. if (!regs)
  268. goto err_out;
  269. slot = regs->which_io;
  270. /*
  271. * If SBI's parent is not io-unit or the io-unit lacks
  272. * a "board#" property, something is very wrong.
  273. */
  274. if (!sbi->parent || strcmp(sbi->parent->name, "io-unit")) {
  275. printk("%s: Error, parent is not io-unit.\n", sbi->full_name);
  276. goto err_out;
  277. }
  278. io_unit = sbi->parent;
  279. board = of_getintprop_default(io_unit, "board#", -1);
  280. if (board == -1) {
  281. printk("%s: Error, lacks board# property.\n", io_unit->full_name);
  282. goto err_out;
  283. }
  284. sbusl = pil_to_sbus[real_irq];
  285. if (sbusl)
  286. pil = sun4d_encode_irq(board, sbusl, slot);
  287. else
  288. pil = real_irq;
  289. irq = irq_alloc(real_irq, pil);
  290. if (irq == 0)
  291. goto err_out;
  292. handler_data = irq_get_handler_data(irq);
  293. if (unlikely(handler_data))
  294. goto err_out;
  295. handler_data = kzalloc(sizeof(struct sun4d_handler_data), GFP_ATOMIC);
  296. if (unlikely(!handler_data)) {
  297. prom_printf("IRQ: kzalloc(sun4d_handler_data) failed.\n");
  298. prom_halt();
  299. }
  300. handler_data->cpuid = board_to_cpu[board];
  301. handler_data->real_irq = real_irq;
  302. irq_set_chip_and_handler_name(irq, &sun4d_irq,
  303. handle_level_irq, "level");
  304. irq_set_handler_data(irq, handler_data);
  305. err_out:
  306. return real_irq;
  307. }
  308. static void __init sun4d_fixup_trap_table(void)
  309. {
  310. #ifdef CONFIG_SMP
  311. unsigned long flags;
  312. struct tt_entry *trap_table = &sparc_ttable[SP_TRAP_IRQ1 + (14 - 1)];
  313. /* Adjust so that we jump directly to smp4d_ticker */
  314. lvl14_save[2] += smp4d_ticker - real_irq_entry;
  315. /* For SMP we use the level 14 ticker, however the bootup code
  316. * has copied the firmware's level 14 vector into the boot cpu's
  317. * trap table, we must fix this now or we get squashed.
  318. */
  319. local_irq_save(flags);
  320. patchme_maybe_smp_msg[0] = 0x01000000; /* NOP out the branch */
  321. trap_table->inst_one = lvl14_save[0];
  322. trap_table->inst_two = lvl14_save[1];
  323. trap_table->inst_three = lvl14_save[2];
  324. trap_table->inst_four = lvl14_save[3];
  325. local_flush_cache_all();
  326. local_irq_restore(flags);
  327. #endif
  328. }
  329. static void __init sun4d_init_timers(irq_handler_t counter_fn)
  330. {
  331. struct device_node *dp;
  332. struct resource res;
  333. unsigned int irq;
  334. const u32 *reg;
  335. int err;
  336. dp = of_find_node_by_name(NULL, "cpu-unit");
  337. if (!dp) {
  338. prom_printf("sun4d_init_timers: Unable to find cpu-unit\n");
  339. prom_halt();
  340. }
  341. /* Which cpu-unit we use is arbitrary, we can view the bootbus timer
  342. * registers via any cpu's mapping. The first 'reg' property is the
  343. * bootbus.
  344. */
  345. reg = of_get_property(dp, "reg", NULL);
  346. of_node_put(dp);
  347. if (!reg) {
  348. prom_printf("sun4d_init_timers: No reg property\n");
  349. prom_halt();
  350. }
  351. res.start = reg[1];
  352. res.end = reg[2] - 1;
  353. res.flags = reg[0] & 0xff;
  354. sun4d_timers = of_ioremap(&res, BW_TIMER_LIMIT,
  355. sizeof(struct sun4d_timer_regs), "user timer");
  356. if (!sun4d_timers) {
  357. prom_printf("sun4d_init_timers: Can't map timer regs\n");
  358. prom_halt();
  359. }
  360. sbus_writel((((1000000/HZ) + 1) << 10), &sun4d_timers->l10_timer_limit);
  361. master_l10_counter = &sun4d_timers->l10_cur_count;
  362. irq = sun4d_build_device_irq(NULL, SUN4D_TIMER_IRQ);
  363. err = request_irq(irq, counter_fn, IRQF_TIMER, "timer", NULL);
  364. if (err) {
  365. prom_printf("sun4d_init_timers: request_irq() failed with %d\n",
  366. err);
  367. prom_halt();
  368. }
  369. sun4d_load_profile_irqs();
  370. sun4d_fixup_trap_table();
  371. }
  372. void __init sun4d_init_sbi_irq(void)
  373. {
  374. struct device_node *dp;
  375. int target_cpu;
  376. target_cpu = boot_cpu_id;
  377. for_each_node_by_name(dp, "sbi") {
  378. int devid = of_getintprop_default(dp, "device-id", 0);
  379. int board = of_getintprop_default(dp, "board#", 0);
  380. unsigned int mask;
  381. set_sbi_tid(devid, target_cpu << 3);
  382. board_to_cpu[board] = target_cpu;
  383. /* Get rid of pending irqs from PROM */
  384. mask = acquire_sbi(devid, 0xffffffff);
  385. if (mask) {
  386. printk(KERN_ERR "Clearing pending IRQs %08x on SBI %d\n",
  387. mask, board);
  388. release_sbi(devid, mask);
  389. }
  390. }
  391. }
  392. void __init sun4d_init_IRQ(void)
  393. {
  394. local_irq_disable();
  395. BTFIXUPSET_CALL(clear_clock_irq, sun4d_clear_clock_irq, BTFIXUPCALL_NORM);
  396. BTFIXUPSET_CALL(load_profile_irq, sun4d_load_profile_irq, BTFIXUPCALL_NORM);
  397. sparc_irq_config.init_timers = sun4d_init_timers;
  398. sparc_irq_config.build_device_irq = sun4d_build_device_irq;
  399. #ifdef CONFIG_SMP
  400. BTFIXUPSET_CALL(set_cpu_int, sun4d_set_cpu_int, BTFIXUPCALL_NORM);
  401. BTFIXUPSET_CALL(clear_cpu_int, sun4d_clear_ipi, BTFIXUPCALL_NOP);
  402. BTFIXUPSET_CALL(set_irq_udt, sun4d_set_udt, BTFIXUPCALL_NOP);
  403. #endif
  404. /* Cannot enable interrupts until OBP ticker is disabled. */
  405. }