sun4d_irq.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569
  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. *
  22. * SBUS interrupts are encoded integers including the board number
  23. * (plus one), the SBUS level, and the SBUS slot number. Sun4D
  24. * IRQ dispatch is done by:
  25. *
  26. * 1) Reading the BW local interrupt table in order to get the bus
  27. * interrupt mask.
  28. *
  29. * This table is indexed by SBUS interrupt level which can be
  30. * derived from the PIL we got interrupted on.
  31. *
  32. * 2) For each bus showing interrupt pending from #1, read the
  33. * SBI interrupt state register. This will indicate which slots
  34. * have interrupts pending for that SBUS interrupt level.
  35. */
  36. struct sun4d_timer_regs {
  37. u32 l10_timer_limit;
  38. u32 l10_cur_countx;
  39. u32 l10_limit_noclear;
  40. u32 ctrl;
  41. u32 l10_cur_count;
  42. };
  43. static struct sun4d_timer_regs __iomem *sun4d_timers;
  44. #define TIMER_IRQ 10
  45. #define MAX_STATIC_ALLOC 4
  46. static unsigned char sbus_tid[32];
  47. static struct irqaction *irq_action[NR_IRQS];
  48. static struct sbus_action {
  49. struct irqaction *action;
  50. /* For SMP this needs to be extended */
  51. } *sbus_actions;
  52. static int pil_to_sbus[] = {
  53. 0,
  54. 0,
  55. 1,
  56. 2,
  57. 0,
  58. 3,
  59. 0,
  60. 4,
  61. 0,
  62. 5,
  63. 0,
  64. 6,
  65. 0,
  66. 7,
  67. 0,
  68. 0,
  69. };
  70. static int sbus_to_pil[] = {
  71. 0,
  72. 2,
  73. 3,
  74. 5,
  75. 7,
  76. 9,
  77. 11,
  78. 13,
  79. };
  80. static int nsbi;
  81. /* Exported for sun4d_smp.c */
  82. DEFINE_SPINLOCK(sun4d_imsk_lock);
  83. int show_sun4d_interrupts(struct seq_file *p, void *v)
  84. {
  85. int i = *(loff_t *) v, j = 0, k = 0, sbusl;
  86. struct irqaction *action;
  87. unsigned long flags;
  88. #ifdef CONFIG_SMP
  89. int x;
  90. #endif
  91. spin_lock_irqsave(&irq_action_lock, flags);
  92. if (i < NR_IRQS) {
  93. sbusl = pil_to_sbus[i];
  94. if (!sbusl) {
  95. action = *(i + irq_action);
  96. if (!action)
  97. goto out_unlock;
  98. } else {
  99. for (j = 0; j < nsbi; j++) {
  100. for (k = 0; k < 4; k++)
  101. action = sbus_actions[(j << 5) + (sbusl << 2) + k].action;
  102. if (action)
  103. goto found_it;
  104. }
  105. goto out_unlock;
  106. }
  107. found_it: seq_printf(p, "%3d: ", i);
  108. #ifndef CONFIG_SMP
  109. seq_printf(p, "%10u ", kstat_irqs(i));
  110. #else
  111. for_each_online_cpu(x)
  112. seq_printf(p, "%10u ",
  113. kstat_cpu(cpu_logical_map(x)).irqs[i]);
  114. #endif
  115. seq_printf(p, "%c %s",
  116. (action->flags & IRQF_DISABLED) ? '+' : ' ',
  117. action->name);
  118. action = action->next;
  119. for (;;) {
  120. for (; action; action = action->next) {
  121. seq_printf(p, ",%s %s",
  122. (action->flags & IRQF_DISABLED) ? " +" : "",
  123. action->name);
  124. }
  125. if (!sbusl)
  126. break;
  127. k++;
  128. if (k < 4) {
  129. action = sbus_actions[(j << 5) + (sbusl << 2) + k].action;
  130. } else {
  131. j++;
  132. if (j == nsbi)
  133. break;
  134. k = 0;
  135. action = sbus_actions[(j << 5) + (sbusl << 2)].action;
  136. }
  137. }
  138. seq_putc(p, '\n');
  139. }
  140. out_unlock:
  141. spin_unlock_irqrestore(&irq_action_lock, flags);
  142. return 0;
  143. }
  144. void sun4d_free_irq(unsigned int irq, void *dev_id)
  145. {
  146. struct irqaction *action, **actionp;
  147. struct irqaction *tmp = NULL;
  148. unsigned long flags;
  149. spin_lock_irqsave(&irq_action_lock, flags);
  150. if (irq < 15)
  151. actionp = irq + irq_action;
  152. else
  153. actionp = &(sbus_actions[irq - (1 << 5)].action);
  154. action = *actionp;
  155. if (!action) {
  156. printk(KERN_ERR "Trying to free free IRQ%d\n", irq);
  157. goto out_unlock;
  158. }
  159. if (dev_id) {
  160. for (; action; action = action->next) {
  161. if (action->dev_id == dev_id)
  162. break;
  163. tmp = action;
  164. }
  165. if (!action) {
  166. printk(KERN_ERR "Trying to free free shared IRQ%d\n",
  167. irq);
  168. goto out_unlock;
  169. }
  170. } else if (action->flags & IRQF_SHARED) {
  171. printk(KERN_ERR "Trying to free shared IRQ%d with NULL device ID\n",
  172. irq);
  173. goto out_unlock;
  174. }
  175. if (action->flags & SA_STATIC_ALLOC) {
  176. /*
  177. * This interrupt is marked as specially allocated
  178. * so it is a bad idea to free it.
  179. */
  180. printk(KERN_ERR "Attempt to free statically allocated IRQ%d (%s)\n",
  181. irq, action->name);
  182. goto out_unlock;
  183. }
  184. if (tmp)
  185. tmp->next = action->next;
  186. else
  187. *actionp = action->next;
  188. spin_unlock_irqrestore(&irq_action_lock, flags);
  189. synchronize_irq(irq);
  190. spin_lock_irqsave(&irq_action_lock, flags);
  191. kfree(action);
  192. if (!(*actionp))
  193. __disable_irq(irq);
  194. out_unlock:
  195. spin_unlock_irqrestore(&irq_action_lock, flags);
  196. }
  197. void sun4d_handler_irq(int pil, struct pt_regs *regs)
  198. {
  199. struct pt_regs *old_regs;
  200. struct irqaction *action;
  201. int cpu = smp_processor_id();
  202. /* SBUS IRQ level (1 - 7) */
  203. int sbusl = pil_to_sbus[pil];
  204. /* FIXME: Is this necessary?? */
  205. cc_get_ipen();
  206. cc_set_iclr(1 << pil);
  207. old_regs = set_irq_regs(regs);
  208. irq_enter();
  209. kstat_cpu(cpu).irqs[pil]++;
  210. if (!sbusl) {
  211. action = *(pil + irq_action);
  212. if (!action)
  213. unexpected_irq(pil, NULL, regs);
  214. do {
  215. action->handler(pil, action->dev_id);
  216. action = action->next;
  217. } while (action);
  218. } else {
  219. int bus_mask = bw_get_intr_mask(sbusl) & 0x3ffff;
  220. int sbino;
  221. struct sbus_action *actionp;
  222. unsigned mask, slot;
  223. int sbil = (sbusl << 2);
  224. bw_clear_intr_mask(sbusl, bus_mask);
  225. /* Loop for each pending SBI */
  226. for (sbino = 0; bus_mask; sbino++, bus_mask >>= 1)
  227. if (bus_mask & 1) {
  228. mask = acquire_sbi(SBI2DEVID(sbino), 0xf << sbil);
  229. mask &= (0xf << sbil);
  230. actionp = sbus_actions + (sbino << 5) + (sbil);
  231. /* Loop for each pending SBI slot */
  232. for (slot = (1 << sbil); mask; slot <<= 1, actionp++)
  233. if (mask & slot) {
  234. mask &= ~slot;
  235. action = actionp->action;
  236. if (!action)
  237. unexpected_irq(pil, NULL, regs);
  238. do {
  239. action->handler(pil, action->dev_id);
  240. action = action->next;
  241. } while (action);
  242. release_sbi(SBI2DEVID(sbino), slot);
  243. }
  244. }
  245. }
  246. irq_exit();
  247. set_irq_regs(old_regs);
  248. }
  249. int sun4d_request_irq(unsigned int irq,
  250. irq_handler_t handler,
  251. unsigned long irqflags, const char *devname, void *dev_id)
  252. {
  253. struct irqaction *action, *tmp = NULL, **actionp;
  254. unsigned long flags;
  255. int ret;
  256. if (irq > 14 && irq < (1 << 5)) {
  257. ret = -EINVAL;
  258. goto out;
  259. }
  260. if (!handler) {
  261. ret = -EINVAL;
  262. goto out;
  263. }
  264. spin_lock_irqsave(&irq_action_lock, flags);
  265. if (irq >= (1 << 5))
  266. actionp = &(sbus_actions[irq - (1 << 5)].action);
  267. else
  268. actionp = irq + irq_action;
  269. action = *actionp;
  270. if (action) {
  271. if ((action->flags & IRQF_SHARED) && (irqflags & IRQF_SHARED)) {
  272. for (tmp = action; tmp->next; tmp = tmp->next)
  273. /* find last entry - tmp used below */;
  274. } else {
  275. ret = -EBUSY;
  276. goto out_unlock;
  277. }
  278. if ((action->flags & IRQF_DISABLED) ^ (irqflags & IRQF_DISABLED)) {
  279. printk(KERN_ERR "Attempt to mix fast and slow interrupts on IRQ%d denied\n",
  280. irq);
  281. ret = -EBUSY;
  282. goto out_unlock;
  283. }
  284. action = NULL; /* Or else! */
  285. }
  286. /* If this is flagged as statically allocated then we use our
  287. * private struct which is never freed.
  288. */
  289. if (irqflags & SA_STATIC_ALLOC) {
  290. if (static_irq_count < MAX_STATIC_ALLOC)
  291. action = &static_irqaction[static_irq_count++];
  292. else
  293. printk(KERN_ERR "Request for IRQ%d (%s) SA_STATIC_ALLOC failed using kmalloc\n",
  294. irq, devname);
  295. }
  296. if (action == NULL)
  297. action = kmalloc(sizeof(struct irqaction), GFP_ATOMIC);
  298. if (!action) {
  299. ret = -ENOMEM;
  300. goto out_unlock;
  301. }
  302. action->handler = handler;
  303. action->flags = irqflags;
  304. action->name = devname;
  305. action->next = NULL;
  306. action->dev_id = dev_id;
  307. if (tmp)
  308. tmp->next = action;
  309. else
  310. *actionp = action;
  311. __enable_irq(irq);
  312. ret = 0;
  313. out_unlock:
  314. spin_unlock_irqrestore(&irq_action_lock, flags);
  315. out:
  316. return ret;
  317. }
  318. static void sun4d_disable_irq(unsigned int irq)
  319. {
  320. int tid = sbus_tid[(irq >> 5) - 1];
  321. unsigned long flags;
  322. if (irq < NR_IRQS)
  323. return;
  324. spin_lock_irqsave(&sun4d_imsk_lock, flags);
  325. cc_set_imsk_other(tid, cc_get_imsk_other(tid) | (1 << sbus_to_pil[(irq >> 2) & 7]));
  326. spin_unlock_irqrestore(&sun4d_imsk_lock, flags);
  327. }
  328. static void sun4d_enable_irq(unsigned int irq)
  329. {
  330. int tid = sbus_tid[(irq >> 5) - 1];
  331. unsigned long flags;
  332. if (irq < NR_IRQS)
  333. return;
  334. spin_lock_irqsave(&sun4d_imsk_lock, flags);
  335. cc_set_imsk_other(tid, cc_get_imsk_other(tid) & ~(1 << sbus_to_pil[(irq >> 2) & 7]));
  336. spin_unlock_irqrestore(&sun4d_imsk_lock, flags);
  337. }
  338. #ifdef CONFIG_SMP
  339. static void sun4d_set_cpu_int(int cpu, int level)
  340. {
  341. sun4d_send_ipi(cpu, level);
  342. }
  343. static void sun4d_clear_ipi(int cpu, int level)
  344. {
  345. }
  346. static void sun4d_set_udt(int cpu)
  347. {
  348. }
  349. /* Setup IRQ distribution scheme. */
  350. void __init sun4d_distribute_irqs(void)
  351. {
  352. struct device_node *dp;
  353. int cpuid = cpu_logical_map(1);
  354. if (cpuid == -1)
  355. cpuid = cpu_logical_map(0);
  356. for_each_node_by_name(dp, "sbi") {
  357. int devid = of_getintprop_default(dp, "device-id", 0);
  358. int board = of_getintprop_default(dp, "board#", 0);
  359. sbus_tid[board] = cpuid;
  360. set_sbi_tid(devid, cpuid << 3);
  361. }
  362. printk(KERN_ERR "All sbus IRQs directed to CPU%d\n", cpuid);
  363. }
  364. #endif
  365. static void sun4d_clear_clock_irq(void)
  366. {
  367. sbus_readl(&sun4d_timers->l10_timer_limit);
  368. }
  369. static void sun4d_load_profile_irq(int cpu, unsigned int limit)
  370. {
  371. bw_set_prof_limit(cpu, limit);
  372. }
  373. static void __init sun4d_load_profile_irqs(void)
  374. {
  375. int cpu = 0, mid;
  376. while (!cpu_find_by_instance(cpu, NULL, &mid)) {
  377. sun4d_load_profile_irq(mid >> 3, 0);
  378. cpu++;
  379. }
  380. }
  381. static void __init sun4d_fixup_trap_table(void)
  382. {
  383. #ifdef CONFIG_SMP
  384. unsigned long flags;
  385. struct tt_entry *trap_table = &sparc_ttable[SP_TRAP_IRQ1 + (14 - 1)];
  386. /* Adjust so that we jump directly to smp4d_ticker */
  387. lvl14_save[2] += smp4d_ticker - real_irq_entry;
  388. /* For SMP we use the level 14 ticker, however the bootup code
  389. * has copied the firmware's level 14 vector into the boot cpu's
  390. * trap table, we must fix this now or we get squashed.
  391. */
  392. local_irq_save(flags);
  393. patchme_maybe_smp_msg[0] = 0x01000000; /* NOP out the branch */
  394. trap_table->inst_one = lvl14_save[0];
  395. trap_table->inst_two = lvl14_save[1];
  396. trap_table->inst_three = lvl14_save[2];
  397. trap_table->inst_four = lvl14_save[3];
  398. local_flush_cache_all();
  399. local_irq_restore(flags);
  400. #endif
  401. }
  402. static void __init sun4d_init_timers(irq_handler_t counter_fn)
  403. {
  404. struct device_node *dp;
  405. struct resource res;
  406. const u32 *reg;
  407. int err;
  408. dp = of_find_node_by_name(NULL, "cpu-unit");
  409. if (!dp) {
  410. prom_printf("sun4d_init_timers: Unable to find cpu-unit\n");
  411. prom_halt();
  412. }
  413. /* Which cpu-unit we use is arbitrary, we can view the bootbus timer
  414. * registers via any cpu's mapping. The first 'reg' property is the
  415. * bootbus.
  416. */
  417. reg = of_get_property(dp, "reg", NULL);
  418. of_node_put(dp);
  419. if (!reg) {
  420. prom_printf("sun4d_init_timers: No reg property\n");
  421. prom_halt();
  422. }
  423. res.start = reg[1];
  424. res.end = reg[2] - 1;
  425. res.flags = reg[0] & 0xff;
  426. sun4d_timers = of_ioremap(&res, BW_TIMER_LIMIT,
  427. sizeof(struct sun4d_timer_regs), "user timer");
  428. if (!sun4d_timers) {
  429. prom_printf("sun4d_init_timers: Can't map timer regs\n");
  430. prom_halt();
  431. }
  432. sbus_writel((((1000000/HZ) + 1) << 10), &sun4d_timers->l10_timer_limit);
  433. master_l10_counter = &sun4d_timers->l10_cur_count;
  434. err = request_irq(TIMER_IRQ, counter_fn,
  435. (IRQF_DISABLED | SA_STATIC_ALLOC),
  436. "timer", NULL);
  437. if (err) {
  438. prom_printf("sun4d_init_timers: request_irq() failed with %d\n",
  439. err);
  440. prom_halt();
  441. }
  442. sun4d_load_profile_irqs();
  443. sun4d_fixup_trap_table();
  444. }
  445. void __init sun4d_init_sbi_irq(void)
  446. {
  447. struct device_node *dp;
  448. int target_cpu = 0;
  449. #ifdef CONFIG_SMP
  450. target_cpu = boot_cpu_id;
  451. #endif
  452. nsbi = 0;
  453. for_each_node_by_name(dp, "sbi")
  454. nsbi++;
  455. sbus_actions = kzalloc(nsbi * 8 * 4 * sizeof(struct sbus_action), GFP_ATOMIC);
  456. if (!sbus_actions) {
  457. prom_printf("SUN4D: Cannot allocate sbus_actions, halting.\n");
  458. prom_halt();
  459. }
  460. for_each_node_by_name(dp, "sbi") {
  461. int devid = of_getintprop_default(dp, "device-id", 0);
  462. int board = of_getintprop_default(dp, "board#", 0);
  463. unsigned int mask;
  464. set_sbi_tid(devid, target_cpu << 3);
  465. sbus_tid[board] = target_cpu;
  466. /* Get rid of pending irqs from PROM */
  467. mask = acquire_sbi(devid, 0xffffffff);
  468. if (mask) {
  469. printk(KERN_ERR "Clearing pending IRQs %08x on SBI %d\n",
  470. mask, board);
  471. release_sbi(devid, mask);
  472. }
  473. }
  474. }
  475. void __init sun4d_init_IRQ(void)
  476. {
  477. local_irq_disable();
  478. BTFIXUPSET_CALL(enable_irq, sun4d_enable_irq, BTFIXUPCALL_NORM);
  479. BTFIXUPSET_CALL(disable_irq, sun4d_disable_irq, BTFIXUPCALL_NORM);
  480. BTFIXUPSET_CALL(clear_clock_irq, sun4d_clear_clock_irq, BTFIXUPCALL_NORM);
  481. BTFIXUPSET_CALL(load_profile_irq, sun4d_load_profile_irq, BTFIXUPCALL_NORM);
  482. sparc_irq_config.init_timers = sun4d_init_timers;
  483. #ifdef CONFIG_SMP
  484. BTFIXUPSET_CALL(set_cpu_int, sun4d_set_cpu_int, BTFIXUPCALL_NORM);
  485. BTFIXUPSET_CALL(clear_cpu_int, sun4d_clear_ipi, BTFIXUPCALL_NOP);
  486. BTFIXUPSET_CALL(set_irq_udt, sun4d_set_udt, BTFIXUPCALL_NOP);
  487. #endif
  488. /* Cannot enable interrupts until OBP ticker is disabled. */
  489. }