sun4d_irq.c 12 KB

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