irq.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620
  1. /* $Id: irq.c,v 1.114 2001/12/11 04:55:51 davem Exp $
  2. * arch/sparc/kernel/irq.c: Interrupt request handling routines. On the
  3. * Sparc the IRQs are basically 'cast in stone'
  4. * and you are supposed to probe the prom's device
  5. * node trees to find out who's got which IRQ.
  6. *
  7. * Copyright (C) 1995 David S. Miller (davem@caip.rutgers.edu)
  8. * Copyright (C) 1995 Miguel de Icaza (miguel@nuclecu.unam.mx)
  9. * Copyright (C) 1995,2002 Pete A. Zaitcev (zaitcev@yahoo.com)
  10. * Copyright (C) 1996 Dave Redman (djhr@tadpole.co.uk)
  11. * Copyright (C) 1998-2000 Anton Blanchard (anton@samba.org)
  12. */
  13. #include <linux/module.h>
  14. #include <linux/sched.h>
  15. #include <linux/ptrace.h>
  16. #include <linux/errno.h>
  17. #include <linux/linkage.h>
  18. #include <linux/kernel_stat.h>
  19. #include <linux/signal.h>
  20. #include <linux/sched.h>
  21. #include <linux/interrupt.h>
  22. #include <linux/slab.h>
  23. #include <linux/random.h>
  24. #include <linux/init.h>
  25. #include <linux/smp.h>
  26. #include <linux/delay.h>
  27. #include <linux/threads.h>
  28. #include <linux/spinlock.h>
  29. #include <linux/seq_file.h>
  30. #include <asm/ptrace.h>
  31. #include <asm/processor.h>
  32. #include <asm/system.h>
  33. #include <asm/psr.h>
  34. #include <asm/smp.h>
  35. #include <asm/vaddrs.h>
  36. #include <asm/timer.h>
  37. #include <asm/openprom.h>
  38. #include <asm/oplib.h>
  39. #include <asm/traps.h>
  40. #include <asm/irq.h>
  41. #include <asm/io.h>
  42. #include <asm/pgalloc.h>
  43. #include <asm/pgtable.h>
  44. #include <asm/pcic.h>
  45. #include <asm/cacheflush.h>
  46. #include <asm/irq_regs.h>
  47. #ifdef CONFIG_SMP
  48. #define SMP_NOP2 "nop; nop;\n\t"
  49. #define SMP_NOP3 "nop; nop; nop;\n\t"
  50. #else
  51. #define SMP_NOP2
  52. #define SMP_NOP3
  53. #endif /* SMP */
  54. unsigned long __local_irq_save(void)
  55. {
  56. unsigned long retval;
  57. unsigned long tmp;
  58. __asm__ __volatile__(
  59. "rd %%psr, %0\n\t"
  60. SMP_NOP3 /* Sun4m + Cypress + SMP bug */
  61. "or %0, %2, %1\n\t"
  62. "wr %1, 0, %%psr\n\t"
  63. "nop; nop; nop\n"
  64. : "=&r" (retval), "=r" (tmp)
  65. : "i" (PSR_PIL)
  66. : "memory");
  67. return retval;
  68. }
  69. void local_irq_enable(void)
  70. {
  71. unsigned long tmp;
  72. __asm__ __volatile__(
  73. "rd %%psr, %0\n\t"
  74. SMP_NOP3 /* Sun4m + Cypress + SMP bug */
  75. "andn %0, %1, %0\n\t"
  76. "wr %0, 0, %%psr\n\t"
  77. "nop; nop; nop\n"
  78. : "=&r" (tmp)
  79. : "i" (PSR_PIL)
  80. : "memory");
  81. }
  82. void local_irq_restore(unsigned long old_psr)
  83. {
  84. unsigned long tmp;
  85. __asm__ __volatile__(
  86. "rd %%psr, %0\n\t"
  87. "and %2, %1, %2\n\t"
  88. SMP_NOP2 /* Sun4m + Cypress + SMP bug */
  89. "andn %0, %1, %0\n\t"
  90. "wr %0, %2, %%psr\n\t"
  91. "nop; nop; nop\n"
  92. : "=&r" (tmp)
  93. : "i" (PSR_PIL), "r" (old_psr)
  94. : "memory");
  95. }
  96. EXPORT_SYMBOL(__local_irq_save);
  97. EXPORT_SYMBOL(local_irq_enable);
  98. EXPORT_SYMBOL(local_irq_restore);
  99. /*
  100. * Dave Redman (djhr@tadpole.co.uk)
  101. *
  102. * IRQ numbers.. These are no longer restricted to 15..
  103. *
  104. * this is done to enable SBUS cards and onboard IO to be masked
  105. * correctly. using the interrupt level isn't good enough.
  106. *
  107. * For example:
  108. * A device interrupting at sbus level6 and the Floppy both come in
  109. * at IRQ11, but enabling and disabling them requires writing to
  110. * different bits in the SLAVIO/SEC.
  111. *
  112. * As a result of these changes sun4m machines could now support
  113. * directed CPU interrupts using the existing enable/disable irq code
  114. * with tweaks.
  115. *
  116. */
  117. static void irq_panic(void)
  118. {
  119. extern char *cputypval;
  120. prom_printf("machine: %s doesn't have irq handlers defined!\n",cputypval);
  121. prom_halt();
  122. }
  123. void (*sparc_init_timers)(irq_handler_t ) =
  124. (void (*)(irq_handler_t )) irq_panic;
  125. /*
  126. * Dave Redman (djhr@tadpole.co.uk)
  127. *
  128. * There used to be extern calls and hard coded values here.. very sucky!
  129. * instead, because some of the devices attach very early, I do something
  130. * equally sucky but at least we'll never try to free statically allocated
  131. * space or call kmalloc before kmalloc_init :(.
  132. *
  133. * In fact it's the timer10 that attaches first.. then timer14
  134. * then kmalloc_init is called.. then the tty interrupts attach.
  135. * hmmm....
  136. *
  137. */
  138. #define MAX_STATIC_ALLOC 4
  139. struct irqaction static_irqaction[MAX_STATIC_ALLOC];
  140. int static_irq_count;
  141. struct {
  142. struct irqaction *action;
  143. int flags;
  144. } sparc_irq[NR_IRQS];
  145. #define SPARC_IRQ_INPROGRESS 1
  146. /* Used to protect the IRQ action lists */
  147. DEFINE_SPINLOCK(irq_action_lock);
  148. int show_interrupts(struct seq_file *p, void *v)
  149. {
  150. int i = *(loff_t *) v;
  151. struct irqaction * action;
  152. unsigned long flags;
  153. #ifdef CONFIG_SMP
  154. int j;
  155. #endif
  156. if (sparc_cpu_model == sun4d) {
  157. extern int show_sun4d_interrupts(struct seq_file *, void *);
  158. return show_sun4d_interrupts(p, v);
  159. }
  160. spin_lock_irqsave(&irq_action_lock, flags);
  161. if (i < NR_IRQS) {
  162. action = sparc_irq[i].action;
  163. if (!action)
  164. goto out_unlock;
  165. seq_printf(p, "%3d: ", i);
  166. #ifndef CONFIG_SMP
  167. seq_printf(p, "%10u ", kstat_irqs(i));
  168. #else
  169. for_each_online_cpu(j) {
  170. seq_printf(p, "%10u ",
  171. kstat_cpu(j).irqs[i]);
  172. }
  173. #endif
  174. seq_printf(p, " %c %s",
  175. (action->flags & IRQF_DISABLED) ? '+' : ' ',
  176. action->name);
  177. for (action=action->next; action; action = action->next) {
  178. seq_printf(p, ",%s %s",
  179. (action->flags & IRQF_DISABLED) ? " +" : "",
  180. action->name);
  181. }
  182. seq_putc(p, '\n');
  183. }
  184. out_unlock:
  185. spin_unlock_irqrestore(&irq_action_lock, flags);
  186. return 0;
  187. }
  188. void free_irq(unsigned int irq, void *dev_id)
  189. {
  190. struct irqaction * action;
  191. struct irqaction **actionp;
  192. unsigned long flags;
  193. unsigned int cpu_irq;
  194. if (sparc_cpu_model == sun4d) {
  195. extern void sun4d_free_irq(unsigned int, void *);
  196. sun4d_free_irq(irq, dev_id);
  197. return;
  198. }
  199. cpu_irq = irq & (NR_IRQS - 1);
  200. if (cpu_irq > 14) { /* 14 irq levels on the sparc */
  201. printk("Trying to free bogus IRQ %d\n", irq);
  202. return;
  203. }
  204. spin_lock_irqsave(&irq_action_lock, flags);
  205. actionp = &sparc_irq[cpu_irq].action;
  206. action = *actionp;
  207. if (!action->handler) {
  208. printk("Trying to free free IRQ%d\n",irq);
  209. goto out_unlock;
  210. }
  211. if (dev_id) {
  212. for (; action; action = action->next) {
  213. if (action->dev_id == dev_id)
  214. break;
  215. actionp = &action->next;
  216. }
  217. if (!action) {
  218. printk("Trying to free free shared IRQ%d\n",irq);
  219. goto out_unlock;
  220. }
  221. } else if (action->flags & IRQF_SHARED) {
  222. printk("Trying to free shared IRQ%d with NULL device ID\n", irq);
  223. goto out_unlock;
  224. }
  225. if (action->flags & SA_STATIC_ALLOC)
  226. {
  227. /* This interrupt is marked as specially allocated
  228. * so it is a bad idea to free it.
  229. */
  230. printk("Attempt to free statically allocated IRQ%d (%s)\n",
  231. irq, action->name);
  232. goto out_unlock;
  233. }
  234. *actionp = action->next;
  235. spin_unlock_irqrestore(&irq_action_lock, flags);
  236. synchronize_irq(irq);
  237. spin_lock_irqsave(&irq_action_lock, flags);
  238. kfree(action);
  239. if (!sparc_irq[cpu_irq].action)
  240. disable_irq(irq);
  241. out_unlock:
  242. spin_unlock_irqrestore(&irq_action_lock, flags);
  243. }
  244. EXPORT_SYMBOL(free_irq);
  245. /*
  246. * This is called when we want to synchronize with
  247. * interrupts. We may for example tell a device to
  248. * stop sending interrupts: but to make sure there
  249. * are no interrupts that are executing on another
  250. * CPU we need to call this function.
  251. */
  252. #ifdef CONFIG_SMP
  253. void synchronize_irq(unsigned int irq)
  254. {
  255. unsigned int cpu_irq;
  256. cpu_irq = irq & (NR_IRQS - 1);
  257. while (sparc_irq[cpu_irq].flags & SPARC_IRQ_INPROGRESS)
  258. cpu_relax();
  259. }
  260. #endif /* SMP */
  261. void unexpected_irq(int irq, void *dev_id, struct pt_regs * regs)
  262. {
  263. int i;
  264. struct irqaction * action;
  265. unsigned int cpu_irq;
  266. cpu_irq = irq & (NR_IRQS - 1);
  267. action = sparc_irq[cpu_irq].action;
  268. printk("IO device interrupt, irq = %d\n", irq);
  269. printk("PC = %08lx NPC = %08lx FP=%08lx\n", regs->pc,
  270. regs->npc, regs->u_regs[14]);
  271. if (action) {
  272. printk("Expecting: ");
  273. for (i = 0; i < 16; i++)
  274. if (action->handler)
  275. printk("[%s:%d:0x%x] ", action->name,
  276. (int) i, (unsigned int) action->handler);
  277. }
  278. printk("AIEEE\n");
  279. panic("bogus interrupt received");
  280. }
  281. void handler_irq(int irq, struct pt_regs * regs)
  282. {
  283. struct pt_regs *old_regs;
  284. struct irqaction * action;
  285. int cpu = smp_processor_id();
  286. #ifdef CONFIG_SMP
  287. extern void smp4m_irq_rotate(int cpu);
  288. #endif
  289. old_regs = set_irq_regs(regs);
  290. irq_enter();
  291. disable_pil_irq(irq);
  292. #ifdef CONFIG_SMP
  293. /* Only rotate on lower priority IRQs (scsi, ethernet, etc.). */
  294. if((sparc_cpu_model==sun4m) && (irq < 10))
  295. smp4m_irq_rotate(cpu);
  296. #endif
  297. action = sparc_irq[irq].action;
  298. sparc_irq[irq].flags |= SPARC_IRQ_INPROGRESS;
  299. kstat_cpu(cpu).irqs[irq]++;
  300. do {
  301. if (!action || !action->handler)
  302. unexpected_irq(irq, NULL, regs);
  303. action->handler(irq, action->dev_id);
  304. action = action->next;
  305. } while (action);
  306. sparc_irq[irq].flags &= ~SPARC_IRQ_INPROGRESS;
  307. enable_pil_irq(irq);
  308. irq_exit();
  309. set_irq_regs(old_regs);
  310. }
  311. #ifdef CONFIG_BLK_DEV_FD
  312. extern void floppy_interrupt(int irq, void *dev_id);
  313. void sparc_floppy_irq(int irq, void *dev_id, struct pt_regs *regs)
  314. {
  315. struct pt_regs *old_regs;
  316. int cpu = smp_processor_id();
  317. old_regs = set_irq_regs(regs);
  318. disable_pil_irq(irq);
  319. irq_enter();
  320. kstat_cpu(cpu).irqs[irq]++;
  321. floppy_interrupt(irq, dev_id);
  322. irq_exit();
  323. enable_pil_irq(irq);
  324. set_irq_regs(old_regs);
  325. // XXX Eek, it's totally changed with preempt_count() and such
  326. // if (softirq_pending(cpu))
  327. // do_softirq();
  328. }
  329. #endif
  330. /* Fast IRQs on the Sparc can only have one routine attached to them,
  331. * thus no sharing possible.
  332. */
  333. int request_fast_irq(unsigned int irq,
  334. irq_handler_t handler,
  335. unsigned long irqflags, const char *devname)
  336. {
  337. struct irqaction *action;
  338. unsigned long flags;
  339. unsigned int cpu_irq;
  340. int ret;
  341. #ifdef CONFIG_SMP
  342. struct tt_entry *trap_table;
  343. extern struct tt_entry trapbase_cpu1, trapbase_cpu2, trapbase_cpu3;
  344. #endif
  345. cpu_irq = irq & (NR_IRQS - 1);
  346. if(cpu_irq > 14) {
  347. ret = -EINVAL;
  348. goto out;
  349. }
  350. if(!handler) {
  351. ret = -EINVAL;
  352. goto out;
  353. }
  354. spin_lock_irqsave(&irq_action_lock, flags);
  355. action = sparc_irq[cpu_irq].action;
  356. if(action) {
  357. if(action->flags & IRQF_SHARED)
  358. panic("Trying to register fast irq when already shared.\n");
  359. if(irqflags & IRQF_SHARED)
  360. panic("Trying to register fast irq as shared.\n");
  361. /* Anyway, someone already owns it so cannot be made fast. */
  362. printk("request_fast_irq: Trying to register yet already owned.\n");
  363. ret = -EBUSY;
  364. goto out_unlock;
  365. }
  366. /* If this is flagged as statically allocated then we use our
  367. * private struct which is never freed.
  368. */
  369. if (irqflags & SA_STATIC_ALLOC) {
  370. if (static_irq_count < MAX_STATIC_ALLOC)
  371. action = &static_irqaction[static_irq_count++];
  372. else
  373. printk("Fast IRQ%d (%s) SA_STATIC_ALLOC failed using kmalloc\n",
  374. irq, devname);
  375. }
  376. if (action == NULL)
  377. action = kmalloc(sizeof(struct irqaction),
  378. GFP_ATOMIC);
  379. if (!action) {
  380. ret = -ENOMEM;
  381. goto out_unlock;
  382. }
  383. /* Dork with trap table if we get this far. */
  384. #define INSTANTIATE(table) \
  385. table[SP_TRAP_IRQ1+(cpu_irq-1)].inst_one = SPARC_RD_PSR_L0; \
  386. table[SP_TRAP_IRQ1+(cpu_irq-1)].inst_two = \
  387. SPARC_BRANCH((unsigned long) handler, \
  388. (unsigned long) &table[SP_TRAP_IRQ1+(cpu_irq-1)].inst_two);\
  389. table[SP_TRAP_IRQ1+(cpu_irq-1)].inst_three = SPARC_RD_WIM_L3; \
  390. table[SP_TRAP_IRQ1+(cpu_irq-1)].inst_four = SPARC_NOP;
  391. INSTANTIATE(sparc_ttable)
  392. #ifdef CONFIG_SMP
  393. trap_table = &trapbase_cpu1; INSTANTIATE(trap_table)
  394. trap_table = &trapbase_cpu2; INSTANTIATE(trap_table)
  395. trap_table = &trapbase_cpu3; INSTANTIATE(trap_table)
  396. #endif
  397. #undef INSTANTIATE
  398. /*
  399. * XXX Correct thing whould be to flush only I- and D-cache lines
  400. * which contain the handler in question. But as of time of the
  401. * writing we have no CPU-neutral interface to fine-grained flushes.
  402. */
  403. flush_cache_all();
  404. action->handler = handler;
  405. action->flags = irqflags;
  406. cpus_clear(action->mask);
  407. action->name = devname;
  408. action->dev_id = NULL;
  409. action->next = NULL;
  410. sparc_irq[cpu_irq].action = action;
  411. enable_irq(irq);
  412. ret = 0;
  413. out_unlock:
  414. spin_unlock_irqrestore(&irq_action_lock, flags);
  415. out:
  416. return ret;
  417. }
  418. int request_irq(unsigned int irq,
  419. irq_handler_t handler,
  420. unsigned long irqflags, const char * devname, void *dev_id)
  421. {
  422. struct irqaction * action, **actionp;
  423. unsigned long flags;
  424. unsigned int cpu_irq;
  425. int ret;
  426. if (sparc_cpu_model == sun4d) {
  427. extern int sun4d_request_irq(unsigned int,
  428. irq_handler_t ,
  429. unsigned long, const char *, void *);
  430. return sun4d_request_irq(irq, handler, irqflags, devname, dev_id);
  431. }
  432. cpu_irq = irq & (NR_IRQS - 1);
  433. if(cpu_irq > 14) {
  434. ret = -EINVAL;
  435. goto out;
  436. }
  437. if (!handler) {
  438. ret = -EINVAL;
  439. goto out;
  440. }
  441. spin_lock_irqsave(&irq_action_lock, flags);
  442. actionp = &sparc_irq[cpu_irq].action;
  443. action = *actionp;
  444. if (action) {
  445. if (!(action->flags & IRQF_SHARED) || !(irqflags & IRQF_SHARED)) {
  446. ret = -EBUSY;
  447. goto out_unlock;
  448. }
  449. if ((action->flags & IRQF_DISABLED) != (irqflags & IRQF_DISABLED)) {
  450. printk("Attempt to mix fast and slow interrupts on IRQ%d denied\n", irq);
  451. ret = -EBUSY;
  452. goto out_unlock;
  453. }
  454. for ( ; action; action = *actionp)
  455. actionp = &action->next;
  456. }
  457. /* If this is flagged as statically allocated then we use our
  458. * private struct which is never freed.
  459. */
  460. if (irqflags & SA_STATIC_ALLOC) {
  461. if (static_irq_count < MAX_STATIC_ALLOC)
  462. action = &static_irqaction[static_irq_count++];
  463. else
  464. printk("Request for IRQ%d (%s) SA_STATIC_ALLOC failed using kmalloc\n", irq, devname);
  465. }
  466. if (action == NULL)
  467. action = kmalloc(sizeof(struct irqaction),
  468. GFP_ATOMIC);
  469. if (!action) {
  470. ret = -ENOMEM;
  471. goto out_unlock;
  472. }
  473. action->handler = handler;
  474. action->flags = irqflags;
  475. cpus_clear(action->mask);
  476. action->name = devname;
  477. action->next = NULL;
  478. action->dev_id = dev_id;
  479. *actionp = action;
  480. enable_irq(irq);
  481. ret = 0;
  482. out_unlock:
  483. spin_unlock_irqrestore(&irq_action_lock, flags);
  484. out:
  485. return ret;
  486. }
  487. EXPORT_SYMBOL(request_irq);
  488. /* We really don't need these at all on the Sparc. We only have
  489. * stubs here because they are exported to modules.
  490. */
  491. unsigned long probe_irq_on(void)
  492. {
  493. return 0;
  494. }
  495. EXPORT_SYMBOL(probe_irq_on);
  496. int probe_irq_off(unsigned long mask)
  497. {
  498. return 0;
  499. }
  500. EXPORT_SYMBOL(probe_irq_off);
  501. /* djhr
  502. * This could probably be made indirect too and assigned in the CPU
  503. * bits of the code. That would be much nicer I think and would also
  504. * fit in with the idea of being able to tune your kernel for your machine
  505. * by removing unrequired machine and device support.
  506. *
  507. */
  508. void __init init_IRQ(void)
  509. {
  510. extern void sun4c_init_IRQ( void );
  511. extern void sun4m_init_IRQ( void );
  512. extern void sun4d_init_IRQ( void );
  513. switch(sparc_cpu_model) {
  514. case sun4c:
  515. case sun4:
  516. sun4c_init_IRQ();
  517. break;
  518. case sun4m:
  519. #ifdef CONFIG_PCI
  520. pcic_probe();
  521. if (pcic_present()) {
  522. sun4m_pci_init_IRQ();
  523. break;
  524. }
  525. #endif
  526. sun4m_init_IRQ();
  527. break;
  528. case sun4d:
  529. sun4d_init_IRQ();
  530. break;
  531. default:
  532. prom_printf("Cannot initialize IRQs on this Sun machine...");
  533. break;
  534. }
  535. btfixup();
  536. }
  537. void init_irq_proc(void)
  538. {
  539. /* For now, nothing... */
  540. }