irq.c 25 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084
  1. /*
  2. * linux/arch/arm/kernel/irq.c
  3. *
  4. * Copyright (C) 1992 Linus Torvalds
  5. * Modifications for ARM processor Copyright (C) 1995-2000 Russell King.
  6. *
  7. * Support for Dynamic Tick Timer Copyright (C) 2004-2005 Nokia Corporation.
  8. * Dynamic Tick Timer written by Tony Lindgren <tony@atomide.com> and
  9. * Tuukka Tikkanen <tuukka.tikkanen@elektrobit.com>.
  10. *
  11. * This program is free software; you can redistribute it and/or modify
  12. * it under the terms of the GNU General Public License version 2 as
  13. * published by the Free Software Foundation.
  14. *
  15. * This file contains the code used by various IRQ handling routines:
  16. * asking for different IRQ's should be done through these routines
  17. * instead of just grabbing them. Thus setups with different IRQ numbers
  18. * shouldn't result in any weird surprises, and installing new handlers
  19. * should be easier.
  20. *
  21. * IRQ's are in fact implemented a bit like signal handlers for the kernel.
  22. * Naturally it's not a 1:1 relation, but there are similarities.
  23. */
  24. #include <linux/config.h>
  25. #include <linux/kernel_stat.h>
  26. #include <linux/module.h>
  27. #include <linux/signal.h>
  28. #include <linux/ioport.h>
  29. #include <linux/interrupt.h>
  30. #include <linux/ptrace.h>
  31. #include <linux/slab.h>
  32. #include <linux/random.h>
  33. #include <linux/smp.h>
  34. #include <linux/init.h>
  35. #include <linux/seq_file.h>
  36. #include <linux/errno.h>
  37. #include <linux/list.h>
  38. #include <linux/kallsyms.h>
  39. #include <linux/proc_fs.h>
  40. #include <asm/irq.h>
  41. #include <asm/system.h>
  42. #include <asm/mach/irq.h>
  43. #include <asm/mach/time.h>
  44. /*
  45. * Maximum IRQ count. Currently, this is arbitary. However, it should
  46. * not be set too low to prevent false triggering. Conversely, if it
  47. * is set too high, then you could miss a stuck IRQ.
  48. *
  49. * Maybe we ought to set a timer and re-enable the IRQ at a later time?
  50. */
  51. #define MAX_IRQ_CNT 100000
  52. static int noirqdebug;
  53. static volatile unsigned long irq_err_count;
  54. static DEFINE_SPINLOCK(irq_controller_lock);
  55. static LIST_HEAD(irq_pending);
  56. struct irqdesc irq_desc[NR_IRQS];
  57. void (*init_arch_irq)(void) __initdata = NULL;
  58. /*
  59. * No architecture-specific irq_finish function defined in arm/arch/irqs.h.
  60. */
  61. #ifndef irq_finish
  62. #define irq_finish(irq) do { } while (0)
  63. #endif
  64. /*
  65. * Dummy mask/unmask handler
  66. */
  67. void dummy_mask_unmask_irq(unsigned int irq)
  68. {
  69. }
  70. irqreturn_t no_action(int irq, void *dev_id, struct pt_regs *regs)
  71. {
  72. return IRQ_NONE;
  73. }
  74. void do_bad_IRQ(unsigned int irq, struct irqdesc *desc, struct pt_regs *regs)
  75. {
  76. irq_err_count += 1;
  77. printk(KERN_ERR "IRQ: spurious interrupt %d\n", irq);
  78. }
  79. static struct irqchip bad_chip = {
  80. .ack = dummy_mask_unmask_irq,
  81. .mask = dummy_mask_unmask_irq,
  82. .unmask = dummy_mask_unmask_irq,
  83. };
  84. static struct irqdesc bad_irq_desc = {
  85. .chip = &bad_chip,
  86. .handle = do_bad_IRQ,
  87. .pend = LIST_HEAD_INIT(bad_irq_desc.pend),
  88. .disable_depth = 1,
  89. };
  90. #ifdef CONFIG_SMP
  91. void synchronize_irq(unsigned int irq)
  92. {
  93. struct irqdesc *desc = irq_desc + irq;
  94. while (desc->running)
  95. barrier();
  96. }
  97. EXPORT_SYMBOL(synchronize_irq);
  98. #define smp_set_running(desc) do { desc->running = 1; } while (0)
  99. #define smp_clear_running(desc) do { desc->running = 0; } while (0)
  100. #else
  101. #define smp_set_running(desc) do { } while (0)
  102. #define smp_clear_running(desc) do { } while (0)
  103. #endif
  104. /**
  105. * disable_irq_nosync - disable an irq without waiting
  106. * @irq: Interrupt to disable
  107. *
  108. * Disable the selected interrupt line. Enables and disables
  109. * are nested. We do this lazily.
  110. *
  111. * This function may be called from IRQ context.
  112. */
  113. void disable_irq_nosync(unsigned int irq)
  114. {
  115. struct irqdesc *desc = irq_desc + irq;
  116. unsigned long flags;
  117. spin_lock_irqsave(&irq_controller_lock, flags);
  118. desc->disable_depth++;
  119. list_del_init(&desc->pend);
  120. spin_unlock_irqrestore(&irq_controller_lock, flags);
  121. }
  122. EXPORT_SYMBOL(disable_irq_nosync);
  123. /**
  124. * disable_irq - disable an irq and wait for completion
  125. * @irq: Interrupt to disable
  126. *
  127. * Disable the selected interrupt line. Enables and disables
  128. * are nested. This functions waits for any pending IRQ
  129. * handlers for this interrupt to complete before returning.
  130. * If you use this function while holding a resource the IRQ
  131. * handler may need you will deadlock.
  132. *
  133. * This function may be called - with care - from IRQ context.
  134. */
  135. void disable_irq(unsigned int irq)
  136. {
  137. struct irqdesc *desc = irq_desc + irq;
  138. disable_irq_nosync(irq);
  139. if (desc->action)
  140. synchronize_irq(irq);
  141. }
  142. EXPORT_SYMBOL(disable_irq);
  143. /**
  144. * enable_irq - enable interrupt handling on an irq
  145. * @irq: Interrupt to enable
  146. *
  147. * Re-enables the processing of interrupts on this IRQ line.
  148. * Note that this may call the interrupt handler, so you may
  149. * get unexpected results if you hold IRQs disabled.
  150. *
  151. * This function may be called from IRQ context.
  152. */
  153. void enable_irq(unsigned int irq)
  154. {
  155. struct irqdesc *desc = irq_desc + irq;
  156. unsigned long flags;
  157. spin_lock_irqsave(&irq_controller_lock, flags);
  158. if (unlikely(!desc->disable_depth)) {
  159. printk("enable_irq(%u) unbalanced from %p\n", irq,
  160. __builtin_return_address(0));
  161. } else if (!--desc->disable_depth) {
  162. desc->probing = 0;
  163. desc->chip->unmask(irq);
  164. /*
  165. * If the interrupt is waiting to be processed,
  166. * try to re-run it. We can't directly run it
  167. * from here since the caller might be in an
  168. * interrupt-protected region.
  169. */
  170. if (desc->pending && list_empty(&desc->pend)) {
  171. desc->pending = 0;
  172. if (!desc->chip->retrigger ||
  173. desc->chip->retrigger(irq))
  174. list_add(&desc->pend, &irq_pending);
  175. }
  176. }
  177. spin_unlock_irqrestore(&irq_controller_lock, flags);
  178. }
  179. EXPORT_SYMBOL(enable_irq);
  180. /*
  181. * Enable wake on selected irq
  182. */
  183. void enable_irq_wake(unsigned int irq)
  184. {
  185. struct irqdesc *desc = irq_desc + irq;
  186. unsigned long flags;
  187. spin_lock_irqsave(&irq_controller_lock, flags);
  188. if (desc->chip->set_wake)
  189. desc->chip->set_wake(irq, 1);
  190. spin_unlock_irqrestore(&irq_controller_lock, flags);
  191. }
  192. EXPORT_SYMBOL(enable_irq_wake);
  193. void disable_irq_wake(unsigned int irq)
  194. {
  195. struct irqdesc *desc = irq_desc + irq;
  196. unsigned long flags;
  197. spin_lock_irqsave(&irq_controller_lock, flags);
  198. if (desc->chip->set_wake)
  199. desc->chip->set_wake(irq, 0);
  200. spin_unlock_irqrestore(&irq_controller_lock, flags);
  201. }
  202. EXPORT_SYMBOL(disable_irq_wake);
  203. int show_interrupts(struct seq_file *p, void *v)
  204. {
  205. int i = *(loff_t *) v, cpu;
  206. struct irqaction * action;
  207. unsigned long flags;
  208. if (i == 0) {
  209. char cpuname[12];
  210. seq_printf(p, " ");
  211. for_each_present_cpu(cpu) {
  212. sprintf(cpuname, "CPU%d", cpu);
  213. seq_printf(p, " %10s", cpuname);
  214. }
  215. seq_putc(p, '\n');
  216. }
  217. if (i < NR_IRQS) {
  218. spin_lock_irqsave(&irq_controller_lock, flags);
  219. action = irq_desc[i].action;
  220. if (!action)
  221. goto unlock;
  222. seq_printf(p, "%3d: ", i);
  223. for_each_present_cpu(cpu)
  224. seq_printf(p, "%10u ", kstat_cpu(cpu).irqs[i]);
  225. seq_printf(p, " %s", action->name);
  226. for (action = action->next; action; action = action->next)
  227. seq_printf(p, ", %s", action->name);
  228. seq_putc(p, '\n');
  229. unlock:
  230. spin_unlock_irqrestore(&irq_controller_lock, flags);
  231. } else if (i == NR_IRQS) {
  232. #ifdef CONFIG_ARCH_ACORN
  233. show_fiq_list(p, v);
  234. #endif
  235. #ifdef CONFIG_SMP
  236. show_ipi_list(p);
  237. show_local_irqs(p);
  238. #endif
  239. seq_printf(p, "Err: %10lu\n", irq_err_count);
  240. }
  241. return 0;
  242. }
  243. /*
  244. * IRQ lock detection.
  245. *
  246. * Hopefully, this should get us out of a few locked situations.
  247. * However, it may take a while for this to happen, since we need
  248. * a large number if IRQs to appear in the same jiffie with the
  249. * same instruction pointer (or within 2 instructions).
  250. */
  251. static int check_irq_lock(struct irqdesc *desc, int irq, struct pt_regs *regs)
  252. {
  253. unsigned long instr_ptr = instruction_pointer(regs);
  254. if (desc->lck_jif == jiffies &&
  255. desc->lck_pc >= instr_ptr && desc->lck_pc < instr_ptr + 8) {
  256. desc->lck_cnt += 1;
  257. if (desc->lck_cnt > MAX_IRQ_CNT) {
  258. printk(KERN_ERR "IRQ LOCK: IRQ%d is locking the system, disabled\n", irq);
  259. return 1;
  260. }
  261. } else {
  262. desc->lck_cnt = 0;
  263. desc->lck_pc = instruction_pointer(regs);
  264. desc->lck_jif = jiffies;
  265. }
  266. return 0;
  267. }
  268. static void
  269. report_bad_irq(unsigned int irq, struct pt_regs *regs, struct irqdesc *desc, int ret)
  270. {
  271. static int count = 100;
  272. struct irqaction *action;
  273. if (!count || noirqdebug)
  274. return;
  275. count--;
  276. if (ret != IRQ_HANDLED && ret != IRQ_NONE) {
  277. printk("irq%u: bogus retval mask %x\n", irq, ret);
  278. } else {
  279. printk("irq%u: nobody cared\n", irq);
  280. }
  281. show_regs(regs);
  282. dump_stack();
  283. printk(KERN_ERR "handlers:");
  284. action = desc->action;
  285. do {
  286. printk("\n" KERN_ERR "[<%p>]", action->handler);
  287. print_symbol(" (%s)", (unsigned long)action->handler);
  288. action = action->next;
  289. } while (action);
  290. printk("\n");
  291. }
  292. static int
  293. __do_irq(unsigned int irq, struct irqaction *action, struct pt_regs *regs)
  294. {
  295. unsigned int status;
  296. int ret, retval = 0;
  297. spin_unlock(&irq_controller_lock);
  298. #ifdef CONFIG_NO_IDLE_HZ
  299. if (!(action->flags & SA_TIMER) && system_timer->dyn_tick != NULL) {
  300. write_seqlock(&xtime_lock);
  301. if (system_timer->dyn_tick->state & DYN_TICK_ENABLED)
  302. system_timer->dyn_tick->handler(irq, 0, regs);
  303. write_sequnlock(&xtime_lock);
  304. }
  305. #endif
  306. if (!(action->flags & SA_INTERRUPT))
  307. local_irq_enable();
  308. status = 0;
  309. do {
  310. ret = action->handler(irq, action->dev_id, regs);
  311. if (ret == IRQ_HANDLED)
  312. status |= action->flags;
  313. retval |= ret;
  314. action = action->next;
  315. } while (action);
  316. if (status & SA_SAMPLE_RANDOM)
  317. add_interrupt_randomness(irq);
  318. spin_lock_irq(&irq_controller_lock);
  319. return retval;
  320. }
  321. /*
  322. * This is for software-decoded IRQs. The caller is expected to
  323. * handle the ack, clear, mask and unmask issues.
  324. */
  325. void
  326. do_simple_IRQ(unsigned int irq, struct irqdesc *desc, struct pt_regs *regs)
  327. {
  328. struct irqaction *action;
  329. const unsigned int cpu = smp_processor_id();
  330. desc->triggered = 1;
  331. kstat_cpu(cpu).irqs[irq]++;
  332. smp_set_running(desc);
  333. action = desc->action;
  334. if (action) {
  335. int ret = __do_irq(irq, action, regs);
  336. if (ret != IRQ_HANDLED)
  337. report_bad_irq(irq, regs, desc, ret);
  338. }
  339. smp_clear_running(desc);
  340. }
  341. /*
  342. * Most edge-triggered IRQ implementations seem to take a broken
  343. * approach to this. Hence the complexity.
  344. */
  345. void
  346. do_edge_IRQ(unsigned int irq, struct irqdesc *desc, struct pt_regs *regs)
  347. {
  348. const unsigned int cpu = smp_processor_id();
  349. desc->triggered = 1;
  350. /*
  351. * If we're currently running this IRQ, or its disabled,
  352. * we shouldn't process the IRQ. Instead, turn on the
  353. * hardware masks.
  354. */
  355. if (unlikely(desc->running || desc->disable_depth))
  356. goto running;
  357. /*
  358. * Acknowledge and clear the IRQ, but don't mask it.
  359. */
  360. desc->chip->ack(irq);
  361. /*
  362. * Mark the IRQ currently in progress.
  363. */
  364. desc->running = 1;
  365. kstat_cpu(cpu).irqs[irq]++;
  366. do {
  367. struct irqaction *action;
  368. action = desc->action;
  369. if (!action)
  370. break;
  371. if (desc->pending && !desc->disable_depth) {
  372. desc->pending = 0;
  373. desc->chip->unmask(irq);
  374. }
  375. __do_irq(irq, action, regs);
  376. } while (desc->pending && !desc->disable_depth);
  377. desc->running = 0;
  378. /*
  379. * If we were disabled or freed, shut down the handler.
  380. */
  381. if (likely(desc->action && !check_irq_lock(desc, irq, regs)))
  382. return;
  383. running:
  384. /*
  385. * We got another IRQ while this one was masked or
  386. * currently running. Delay it.
  387. */
  388. desc->pending = 1;
  389. desc->chip->mask(irq);
  390. desc->chip->ack(irq);
  391. }
  392. /*
  393. * Level-based IRQ handler. Nice and simple.
  394. */
  395. void
  396. do_level_IRQ(unsigned int irq, struct irqdesc *desc, struct pt_regs *regs)
  397. {
  398. struct irqaction *action;
  399. const unsigned int cpu = smp_processor_id();
  400. desc->triggered = 1;
  401. /*
  402. * Acknowledge, clear _AND_ disable the interrupt.
  403. */
  404. desc->chip->ack(irq);
  405. if (likely(!desc->disable_depth)) {
  406. kstat_cpu(cpu).irqs[irq]++;
  407. smp_set_running(desc);
  408. /*
  409. * Return with this interrupt masked if no action
  410. */
  411. action = desc->action;
  412. if (action) {
  413. int ret = __do_irq(irq, desc->action, regs);
  414. if (ret != IRQ_HANDLED)
  415. report_bad_irq(irq, regs, desc, ret);
  416. if (likely(!desc->disable_depth &&
  417. !check_irq_lock(desc, irq, regs)))
  418. desc->chip->unmask(irq);
  419. }
  420. smp_clear_running(desc);
  421. }
  422. }
  423. static void do_pending_irqs(struct pt_regs *regs)
  424. {
  425. struct list_head head, *l, *n;
  426. do {
  427. struct irqdesc *desc;
  428. /*
  429. * First, take the pending interrupts off the list.
  430. * The act of calling the handlers may add some IRQs
  431. * back onto the list.
  432. */
  433. head = irq_pending;
  434. INIT_LIST_HEAD(&irq_pending);
  435. head.next->prev = &head;
  436. head.prev->next = &head;
  437. /*
  438. * Now run each entry. We must delete it from our
  439. * list before calling the handler.
  440. */
  441. list_for_each_safe(l, n, &head) {
  442. desc = list_entry(l, struct irqdesc, pend);
  443. list_del_init(&desc->pend);
  444. desc_handle_irq(desc - irq_desc, desc, regs);
  445. }
  446. /*
  447. * The list must be empty.
  448. */
  449. BUG_ON(!list_empty(&head));
  450. } while (!list_empty(&irq_pending));
  451. }
  452. /*
  453. * do_IRQ handles all hardware IRQ's. Decoded IRQs should not
  454. * come via this function. Instead, they should provide their
  455. * own 'handler'
  456. */
  457. asmlinkage void asm_do_IRQ(unsigned int irq, struct pt_regs *regs)
  458. {
  459. struct irqdesc *desc = irq_desc + irq;
  460. /*
  461. * Some hardware gives randomly wrong interrupts. Rather
  462. * than crashing, do something sensible.
  463. */
  464. if (irq >= NR_IRQS)
  465. desc = &bad_irq_desc;
  466. irq_enter();
  467. spin_lock(&irq_controller_lock);
  468. desc_handle_irq(irq, desc, regs);
  469. /*
  470. * Now re-run any pending interrupts.
  471. */
  472. if (!list_empty(&irq_pending))
  473. do_pending_irqs(regs);
  474. irq_finish(irq);
  475. spin_unlock(&irq_controller_lock);
  476. irq_exit();
  477. }
  478. void __set_irq_handler(unsigned int irq, irq_handler_t handle, int is_chained)
  479. {
  480. struct irqdesc *desc;
  481. unsigned long flags;
  482. if (irq >= NR_IRQS) {
  483. printk(KERN_ERR "Trying to install handler for IRQ%d\n", irq);
  484. return;
  485. }
  486. if (handle == NULL)
  487. handle = do_bad_IRQ;
  488. desc = irq_desc + irq;
  489. if (is_chained && desc->chip == &bad_chip)
  490. printk(KERN_WARNING "Trying to install chained handler for IRQ%d\n", irq);
  491. spin_lock_irqsave(&irq_controller_lock, flags);
  492. if (handle == do_bad_IRQ) {
  493. desc->chip->mask(irq);
  494. desc->chip->ack(irq);
  495. desc->disable_depth = 1;
  496. }
  497. desc->handle = handle;
  498. if (handle != do_bad_IRQ && is_chained) {
  499. desc->valid = 0;
  500. desc->probe_ok = 0;
  501. desc->disable_depth = 0;
  502. desc->chip->unmask(irq);
  503. }
  504. spin_unlock_irqrestore(&irq_controller_lock, flags);
  505. }
  506. void set_irq_chip(unsigned int irq, struct irqchip *chip)
  507. {
  508. struct irqdesc *desc;
  509. unsigned long flags;
  510. if (irq >= NR_IRQS) {
  511. printk(KERN_ERR "Trying to install chip for IRQ%d\n", irq);
  512. return;
  513. }
  514. if (chip == NULL)
  515. chip = &bad_chip;
  516. desc = irq_desc + irq;
  517. spin_lock_irqsave(&irq_controller_lock, flags);
  518. desc->chip = chip;
  519. spin_unlock_irqrestore(&irq_controller_lock, flags);
  520. }
  521. int set_irq_type(unsigned int irq, unsigned int type)
  522. {
  523. struct irqdesc *desc;
  524. unsigned long flags;
  525. int ret = -ENXIO;
  526. if (irq >= NR_IRQS) {
  527. printk(KERN_ERR "Trying to set irq type for IRQ%d\n", irq);
  528. return -ENODEV;
  529. }
  530. desc = irq_desc + irq;
  531. if (desc->chip->set_type) {
  532. spin_lock_irqsave(&irq_controller_lock, flags);
  533. ret = desc->chip->set_type(irq, type);
  534. spin_unlock_irqrestore(&irq_controller_lock, flags);
  535. }
  536. return ret;
  537. }
  538. EXPORT_SYMBOL(set_irq_type);
  539. void set_irq_flags(unsigned int irq, unsigned int iflags)
  540. {
  541. struct irqdesc *desc;
  542. unsigned long flags;
  543. if (irq >= NR_IRQS) {
  544. printk(KERN_ERR "Trying to set irq flags for IRQ%d\n", irq);
  545. return;
  546. }
  547. desc = irq_desc + irq;
  548. spin_lock_irqsave(&irq_controller_lock, flags);
  549. desc->valid = (iflags & IRQF_VALID) != 0;
  550. desc->probe_ok = (iflags & IRQF_PROBE) != 0;
  551. desc->noautoenable = (iflags & IRQF_NOAUTOEN) != 0;
  552. spin_unlock_irqrestore(&irq_controller_lock, flags);
  553. }
  554. int setup_irq(unsigned int irq, struct irqaction *new)
  555. {
  556. int shared = 0;
  557. struct irqaction *old, **p;
  558. unsigned long flags;
  559. struct irqdesc *desc;
  560. /*
  561. * Some drivers like serial.c use request_irq() heavily,
  562. * so we have to be careful not to interfere with a
  563. * running system.
  564. */
  565. if (new->flags & SA_SAMPLE_RANDOM) {
  566. /*
  567. * This function might sleep, we want to call it first,
  568. * outside of the atomic block.
  569. * Yes, this might clear the entropy pool if the wrong
  570. * driver is attempted to be loaded, without actually
  571. * installing a new handler, but is this really a problem,
  572. * only the sysadmin is able to do this.
  573. */
  574. rand_initialize_irq(irq);
  575. }
  576. /*
  577. * The following block of code has to be executed atomically
  578. */
  579. desc = irq_desc + irq;
  580. spin_lock_irqsave(&irq_controller_lock, flags);
  581. p = &desc->action;
  582. if ((old = *p) != NULL) {
  583. /* Can't share interrupts unless both agree to */
  584. if (!(old->flags & new->flags & SA_SHIRQ)) {
  585. spin_unlock_irqrestore(&irq_controller_lock, flags);
  586. return -EBUSY;
  587. }
  588. /* add new interrupt at end of irq queue */
  589. do {
  590. p = &old->next;
  591. old = *p;
  592. } while (old);
  593. shared = 1;
  594. }
  595. *p = new;
  596. if (!shared) {
  597. desc->probing = 0;
  598. desc->running = 0;
  599. desc->pending = 0;
  600. desc->disable_depth = 1;
  601. if (!desc->noautoenable) {
  602. desc->disable_depth = 0;
  603. desc->chip->unmask(irq);
  604. }
  605. }
  606. spin_unlock_irqrestore(&irq_controller_lock, flags);
  607. return 0;
  608. }
  609. /**
  610. * request_irq - allocate an interrupt line
  611. * @irq: Interrupt line to allocate
  612. * @handler: Function to be called when the IRQ occurs
  613. * @irqflags: Interrupt type flags
  614. * @devname: An ascii name for the claiming device
  615. * @dev_id: A cookie passed back to the handler function
  616. *
  617. * This call allocates interrupt resources and enables the
  618. * interrupt line and IRQ handling. From the point this
  619. * call is made your handler function may be invoked. Since
  620. * your handler function must clear any interrupt the board
  621. * raises, you must take care both to initialise your hardware
  622. * and to set up the interrupt handler in the right order.
  623. *
  624. * Dev_id must be globally unique. Normally the address of the
  625. * device data structure is used as the cookie. Since the handler
  626. * receives this value it makes sense to use it.
  627. *
  628. * If your interrupt is shared you must pass a non NULL dev_id
  629. * as this is required when freeing the interrupt.
  630. *
  631. * Flags:
  632. *
  633. * SA_SHIRQ Interrupt is shared
  634. *
  635. * SA_INTERRUPT Disable local interrupts while processing
  636. *
  637. * SA_SAMPLE_RANDOM The interrupt can be used for entropy
  638. *
  639. */
  640. int request_irq(unsigned int irq, irqreturn_t (*handler)(int, void *, struct pt_regs *),
  641. unsigned long irq_flags, const char * devname, void *dev_id)
  642. {
  643. unsigned long retval;
  644. struct irqaction *action;
  645. if (irq >= NR_IRQS || !irq_desc[irq].valid || !handler ||
  646. (irq_flags & SA_SHIRQ && !dev_id))
  647. return -EINVAL;
  648. action = (struct irqaction *)kmalloc(sizeof(struct irqaction), GFP_KERNEL);
  649. if (!action)
  650. return -ENOMEM;
  651. action->handler = handler;
  652. action->flags = irq_flags;
  653. cpus_clear(action->mask);
  654. action->name = devname;
  655. action->next = NULL;
  656. action->dev_id = dev_id;
  657. retval = setup_irq(irq, action);
  658. if (retval)
  659. kfree(action);
  660. return retval;
  661. }
  662. EXPORT_SYMBOL(request_irq);
  663. /**
  664. * free_irq - free an interrupt
  665. * @irq: Interrupt line to free
  666. * @dev_id: Device identity to free
  667. *
  668. * Remove an interrupt handler. The handler is removed and if the
  669. * interrupt line is no longer in use by any driver it is disabled.
  670. * On a shared IRQ the caller must ensure the interrupt is disabled
  671. * on the card it drives before calling this function.
  672. *
  673. * This function must not be called from interrupt context.
  674. */
  675. void free_irq(unsigned int irq, void *dev_id)
  676. {
  677. struct irqaction * action, **p;
  678. unsigned long flags;
  679. if (irq >= NR_IRQS || !irq_desc[irq].valid) {
  680. printk(KERN_ERR "Trying to free IRQ%d\n",irq);
  681. dump_stack();
  682. return;
  683. }
  684. spin_lock_irqsave(&irq_controller_lock, flags);
  685. for (p = &irq_desc[irq].action; (action = *p) != NULL; p = &action->next) {
  686. if (action->dev_id != dev_id)
  687. continue;
  688. /* Found it - now free it */
  689. *p = action->next;
  690. break;
  691. }
  692. spin_unlock_irqrestore(&irq_controller_lock, flags);
  693. if (!action) {
  694. printk(KERN_ERR "Trying to free free IRQ%d\n",irq);
  695. dump_stack();
  696. } else {
  697. synchronize_irq(irq);
  698. kfree(action);
  699. }
  700. }
  701. EXPORT_SYMBOL(free_irq);
  702. static DECLARE_MUTEX(probe_sem);
  703. /* Start the interrupt probing. Unlike other architectures,
  704. * we don't return a mask of interrupts from probe_irq_on,
  705. * but return the number of interrupts enabled for the probe.
  706. * The interrupts which have been enabled for probing is
  707. * instead recorded in the irq_desc structure.
  708. */
  709. unsigned long probe_irq_on(void)
  710. {
  711. unsigned int i, irqs = 0;
  712. unsigned long delay;
  713. down(&probe_sem);
  714. /*
  715. * first snaffle up any unassigned but
  716. * probe-able interrupts
  717. */
  718. spin_lock_irq(&irq_controller_lock);
  719. for (i = 0; i < NR_IRQS; i++) {
  720. if (!irq_desc[i].probe_ok || irq_desc[i].action)
  721. continue;
  722. irq_desc[i].probing = 1;
  723. irq_desc[i].triggered = 0;
  724. if (irq_desc[i].chip->set_type)
  725. irq_desc[i].chip->set_type(i, IRQT_PROBE);
  726. irq_desc[i].chip->unmask(i);
  727. irqs += 1;
  728. }
  729. spin_unlock_irq(&irq_controller_lock);
  730. /*
  731. * wait for spurious interrupts to mask themselves out again
  732. */
  733. for (delay = jiffies + HZ/10; time_before(jiffies, delay); )
  734. /* min 100ms delay */;
  735. /*
  736. * now filter out any obviously spurious interrupts
  737. */
  738. spin_lock_irq(&irq_controller_lock);
  739. for (i = 0; i < NR_IRQS; i++) {
  740. if (irq_desc[i].probing && irq_desc[i].triggered) {
  741. irq_desc[i].probing = 0;
  742. irqs -= 1;
  743. }
  744. }
  745. spin_unlock_irq(&irq_controller_lock);
  746. return irqs;
  747. }
  748. EXPORT_SYMBOL(probe_irq_on);
  749. unsigned int probe_irq_mask(unsigned long irqs)
  750. {
  751. unsigned int mask = 0, i;
  752. spin_lock_irq(&irq_controller_lock);
  753. for (i = 0; i < 16 && i < NR_IRQS; i++)
  754. if (irq_desc[i].probing && irq_desc[i].triggered)
  755. mask |= 1 << i;
  756. spin_unlock_irq(&irq_controller_lock);
  757. up(&probe_sem);
  758. return mask;
  759. }
  760. EXPORT_SYMBOL(probe_irq_mask);
  761. /*
  762. * Possible return values:
  763. * >= 0 - interrupt number
  764. * -1 - no interrupt/many interrupts
  765. */
  766. int probe_irq_off(unsigned long irqs)
  767. {
  768. unsigned int i;
  769. int irq_found = NO_IRQ;
  770. /*
  771. * look at the interrupts, and find exactly one
  772. * that we were probing has been triggered
  773. */
  774. spin_lock_irq(&irq_controller_lock);
  775. for (i = 0; i < NR_IRQS; i++) {
  776. if (irq_desc[i].probing &&
  777. irq_desc[i].triggered) {
  778. if (irq_found != NO_IRQ) {
  779. irq_found = NO_IRQ;
  780. goto out;
  781. }
  782. irq_found = i;
  783. }
  784. }
  785. if (irq_found == -1)
  786. irq_found = NO_IRQ;
  787. out:
  788. spin_unlock_irq(&irq_controller_lock);
  789. up(&probe_sem);
  790. return irq_found;
  791. }
  792. EXPORT_SYMBOL(probe_irq_off);
  793. #ifdef CONFIG_SMP
  794. static void route_irq(struct irqdesc *desc, unsigned int irq, unsigned int cpu)
  795. {
  796. pr_debug("IRQ%u: moving from cpu%u to cpu%u\n", irq, desc->cpu, cpu);
  797. spin_lock_irq(&irq_controller_lock);
  798. desc->cpu = cpu;
  799. desc->chip->set_cpu(desc, irq, cpu);
  800. spin_unlock_irq(&irq_controller_lock);
  801. }
  802. #ifdef CONFIG_PROC_FS
  803. static int
  804. irq_affinity_read_proc(char *page, char **start, off_t off, int count,
  805. int *eof, void *data)
  806. {
  807. struct irqdesc *desc = irq_desc + ((int)data);
  808. int len = cpumask_scnprintf(page, count, desc->affinity);
  809. if (count - len < 2)
  810. return -EINVAL;
  811. page[len++] = '\n';
  812. page[len] = '\0';
  813. return len;
  814. }
  815. static int
  816. irq_affinity_write_proc(struct file *file, const char __user *buffer,
  817. unsigned long count, void *data)
  818. {
  819. unsigned int irq = (unsigned int)data;
  820. struct irqdesc *desc = irq_desc + irq;
  821. cpumask_t affinity, tmp;
  822. int ret = -EIO;
  823. if (!desc->chip->set_cpu)
  824. goto out;
  825. ret = cpumask_parse(buffer, count, affinity);
  826. if (ret)
  827. goto out;
  828. cpus_and(tmp, affinity, cpu_online_map);
  829. if (cpus_empty(tmp)) {
  830. ret = -EINVAL;
  831. goto out;
  832. }
  833. desc->affinity = affinity;
  834. route_irq(desc, irq, first_cpu(tmp));
  835. ret = count;
  836. out:
  837. return ret;
  838. }
  839. #endif
  840. #endif
  841. void __init init_irq_proc(void)
  842. {
  843. #if defined(CONFIG_SMP) && defined(CONFIG_PROC_FS)
  844. struct proc_dir_entry *dir;
  845. int irq;
  846. dir = proc_mkdir("irq", NULL);
  847. if (!dir)
  848. return;
  849. for (irq = 0; irq < NR_IRQS; irq++) {
  850. struct proc_dir_entry *entry;
  851. struct irqdesc *desc;
  852. char name[16];
  853. desc = irq_desc + irq;
  854. memset(name, 0, sizeof(name));
  855. snprintf(name, sizeof(name) - 1, "%u", irq);
  856. desc->procdir = proc_mkdir(name, dir);
  857. if (!desc->procdir)
  858. continue;
  859. entry = create_proc_entry("smp_affinity", 0600, desc->procdir);
  860. if (entry) {
  861. entry->nlink = 1;
  862. entry->data = (void *)irq;
  863. entry->read_proc = irq_affinity_read_proc;
  864. entry->write_proc = irq_affinity_write_proc;
  865. }
  866. }
  867. #endif
  868. }
  869. void __init init_IRQ(void)
  870. {
  871. struct irqdesc *desc;
  872. extern void init_dma(void);
  873. int irq;
  874. #ifdef CONFIG_SMP
  875. bad_irq_desc.affinity = CPU_MASK_ALL;
  876. bad_irq_desc.cpu = smp_processor_id();
  877. #endif
  878. for (irq = 0, desc = irq_desc; irq < NR_IRQS; irq++, desc++) {
  879. *desc = bad_irq_desc;
  880. INIT_LIST_HEAD(&desc->pend);
  881. }
  882. init_arch_irq();
  883. init_dma();
  884. }
  885. static int __init noirqdebug_setup(char *str)
  886. {
  887. noirqdebug = 1;
  888. return 1;
  889. }
  890. __setup("noirqdebug", noirqdebug_setup);
  891. #ifdef CONFIG_HOTPLUG_CPU
  892. /*
  893. * The CPU has been marked offline. Migrate IRQs off this CPU. If
  894. * the affinity settings do not allow other CPUs, force them onto any
  895. * available CPU.
  896. */
  897. void migrate_irqs(void)
  898. {
  899. unsigned int i, cpu = smp_processor_id();
  900. for (i = 0; i < NR_IRQS; i++) {
  901. struct irqdesc *desc = irq_desc + i;
  902. if (desc->cpu == cpu) {
  903. unsigned int newcpu = any_online_cpu(desc->affinity);
  904. if (newcpu == NR_CPUS) {
  905. if (printk_ratelimit())
  906. printk(KERN_INFO "IRQ%u no longer affine to CPU%u\n",
  907. i, cpu);
  908. cpus_setall(desc->affinity);
  909. newcpu = any_online_cpu(desc->affinity);
  910. }
  911. route_irq(desc, i, newcpu);
  912. }
  913. }
  914. }
  915. #endif /* CONFIG_HOTPLUG_CPU */