sun4d_irq.c 12 KB

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