irq.c 15 KB

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