sun4d_irq.c 15 KB

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