sun4d_irq.c 12 KB

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