irq.c 25 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093
  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. /*
  584. * Can't share interrupts unless both agree to and are
  585. * the same type.
  586. */
  587. if (!(old->flags & new->flags & SA_SHIRQ) ||
  588. (~old->flags & new->flags) & SA_TRIGGER_MASK) {
  589. spin_unlock_irqrestore(&irq_controller_lock, flags);
  590. return -EBUSY;
  591. }
  592. /* add new interrupt at end of irq queue */
  593. do {
  594. p = &old->next;
  595. old = *p;
  596. } while (old);
  597. shared = 1;
  598. }
  599. *p = new;
  600. if (!shared) {
  601. desc->probing = 0;
  602. desc->running = 0;
  603. desc->pending = 0;
  604. desc->disable_depth = 1;
  605. if (new->flags & SA_TRIGGER_MASK &&
  606. desc->chip->set_type) {
  607. unsigned int type = new->flags & SA_TRIGGER_MASK;
  608. desc->chip->set_type(irq, type);
  609. }
  610. if (!desc->noautoenable) {
  611. desc->disable_depth = 0;
  612. desc->chip->unmask(irq);
  613. }
  614. }
  615. spin_unlock_irqrestore(&irq_controller_lock, flags);
  616. return 0;
  617. }
  618. /**
  619. * request_irq - allocate an interrupt line
  620. * @irq: Interrupt line to allocate
  621. * @handler: Function to be called when the IRQ occurs
  622. * @irqflags: Interrupt type flags
  623. * @devname: An ascii name for the claiming device
  624. * @dev_id: A cookie passed back to the handler function
  625. *
  626. * This call allocates interrupt resources and enables the
  627. * interrupt line and IRQ handling. From the point this
  628. * call is made your handler function may be invoked. Since
  629. * your handler function must clear any interrupt the board
  630. * raises, you must take care both to initialise your hardware
  631. * and to set up the interrupt handler in the right order.
  632. *
  633. * Dev_id must be globally unique. Normally the address of the
  634. * device data structure is used as the cookie. Since the handler
  635. * receives this value it makes sense to use it.
  636. *
  637. * If your interrupt is shared you must pass a non NULL dev_id
  638. * as this is required when freeing the interrupt.
  639. *
  640. * Flags:
  641. *
  642. * SA_SHIRQ Interrupt is shared
  643. *
  644. * SA_INTERRUPT Disable local interrupts while processing
  645. *
  646. * SA_SAMPLE_RANDOM The interrupt can be used for entropy
  647. *
  648. */
  649. int request_irq(unsigned int irq, irqreturn_t (*handler)(int, void *, struct pt_regs *),
  650. unsigned long irq_flags, const char * devname, void *dev_id)
  651. {
  652. unsigned long retval;
  653. struct irqaction *action;
  654. if (irq >= NR_IRQS || !irq_desc[irq].valid || !handler ||
  655. (irq_flags & SA_SHIRQ && !dev_id))
  656. return -EINVAL;
  657. action = (struct irqaction *)kmalloc(sizeof(struct irqaction), GFP_KERNEL);
  658. if (!action)
  659. return -ENOMEM;
  660. action->handler = handler;
  661. action->flags = irq_flags;
  662. cpus_clear(action->mask);
  663. action->name = devname;
  664. action->next = NULL;
  665. action->dev_id = dev_id;
  666. retval = setup_irq(irq, action);
  667. if (retval)
  668. kfree(action);
  669. return retval;
  670. }
  671. EXPORT_SYMBOL(request_irq);
  672. /**
  673. * free_irq - free an interrupt
  674. * @irq: Interrupt line to free
  675. * @dev_id: Device identity to free
  676. *
  677. * Remove an interrupt handler. The handler is removed and if the
  678. * interrupt line is no longer in use by any driver it is disabled.
  679. * On a shared IRQ the caller must ensure the interrupt is disabled
  680. * on the card it drives before calling this function.
  681. *
  682. * This function must not be called from interrupt context.
  683. */
  684. void free_irq(unsigned int irq, void *dev_id)
  685. {
  686. struct irqaction * action, **p;
  687. unsigned long flags;
  688. if (irq >= NR_IRQS || !irq_desc[irq].valid) {
  689. printk(KERN_ERR "Trying to free IRQ%d\n",irq);
  690. dump_stack();
  691. return;
  692. }
  693. spin_lock_irqsave(&irq_controller_lock, flags);
  694. for (p = &irq_desc[irq].action; (action = *p) != NULL; p = &action->next) {
  695. if (action->dev_id != dev_id)
  696. continue;
  697. /* Found it - now free it */
  698. *p = action->next;
  699. break;
  700. }
  701. spin_unlock_irqrestore(&irq_controller_lock, flags);
  702. if (!action) {
  703. printk(KERN_ERR "Trying to free free IRQ%d\n",irq);
  704. dump_stack();
  705. } else {
  706. synchronize_irq(irq);
  707. kfree(action);
  708. }
  709. }
  710. EXPORT_SYMBOL(free_irq);
  711. static DECLARE_MUTEX(probe_sem);
  712. /* Start the interrupt probing. Unlike other architectures,
  713. * we don't return a mask of interrupts from probe_irq_on,
  714. * but return the number of interrupts enabled for the probe.
  715. * The interrupts which have been enabled for probing is
  716. * instead recorded in the irq_desc structure.
  717. */
  718. unsigned long probe_irq_on(void)
  719. {
  720. unsigned int i, irqs = 0;
  721. unsigned long delay;
  722. down(&probe_sem);
  723. /*
  724. * first snaffle up any unassigned but
  725. * probe-able interrupts
  726. */
  727. spin_lock_irq(&irq_controller_lock);
  728. for (i = 0; i < NR_IRQS; i++) {
  729. if (!irq_desc[i].probe_ok || irq_desc[i].action)
  730. continue;
  731. irq_desc[i].probing = 1;
  732. irq_desc[i].triggered = 0;
  733. if (irq_desc[i].chip->set_type)
  734. irq_desc[i].chip->set_type(i, IRQT_PROBE);
  735. irq_desc[i].chip->unmask(i);
  736. irqs += 1;
  737. }
  738. spin_unlock_irq(&irq_controller_lock);
  739. /*
  740. * wait for spurious interrupts to mask themselves out again
  741. */
  742. for (delay = jiffies + HZ/10; time_before(jiffies, delay); )
  743. /* min 100ms delay */;
  744. /*
  745. * now filter out any obviously spurious interrupts
  746. */
  747. spin_lock_irq(&irq_controller_lock);
  748. for (i = 0; i < NR_IRQS; i++) {
  749. if (irq_desc[i].probing && irq_desc[i].triggered) {
  750. irq_desc[i].probing = 0;
  751. irqs -= 1;
  752. }
  753. }
  754. spin_unlock_irq(&irq_controller_lock);
  755. return irqs;
  756. }
  757. EXPORT_SYMBOL(probe_irq_on);
  758. unsigned int probe_irq_mask(unsigned long irqs)
  759. {
  760. unsigned int mask = 0, i;
  761. spin_lock_irq(&irq_controller_lock);
  762. for (i = 0; i < 16 && i < NR_IRQS; i++)
  763. if (irq_desc[i].probing && irq_desc[i].triggered)
  764. mask |= 1 << i;
  765. spin_unlock_irq(&irq_controller_lock);
  766. up(&probe_sem);
  767. return mask;
  768. }
  769. EXPORT_SYMBOL(probe_irq_mask);
  770. /*
  771. * Possible return values:
  772. * >= 0 - interrupt number
  773. * -1 - no interrupt/many interrupts
  774. */
  775. int probe_irq_off(unsigned long irqs)
  776. {
  777. unsigned int i;
  778. int irq_found = NO_IRQ;
  779. /*
  780. * look at the interrupts, and find exactly one
  781. * that we were probing has been triggered
  782. */
  783. spin_lock_irq(&irq_controller_lock);
  784. for (i = 0; i < NR_IRQS; i++) {
  785. if (irq_desc[i].probing &&
  786. irq_desc[i].triggered) {
  787. if (irq_found != NO_IRQ) {
  788. irq_found = NO_IRQ;
  789. goto out;
  790. }
  791. irq_found = i;
  792. }
  793. }
  794. if (irq_found == -1)
  795. irq_found = NO_IRQ;
  796. out:
  797. spin_unlock_irq(&irq_controller_lock);
  798. up(&probe_sem);
  799. return irq_found;
  800. }
  801. EXPORT_SYMBOL(probe_irq_off);
  802. #ifdef CONFIG_SMP
  803. static void route_irq(struct irqdesc *desc, unsigned int irq, unsigned int cpu)
  804. {
  805. pr_debug("IRQ%u: moving from cpu%u to cpu%u\n", irq, desc->cpu, cpu);
  806. spin_lock_irq(&irq_controller_lock);
  807. desc->cpu = cpu;
  808. desc->chip->set_cpu(desc, irq, cpu);
  809. spin_unlock_irq(&irq_controller_lock);
  810. }
  811. #ifdef CONFIG_PROC_FS
  812. static int
  813. irq_affinity_read_proc(char *page, char **start, off_t off, int count,
  814. int *eof, void *data)
  815. {
  816. struct irqdesc *desc = irq_desc + ((int)data);
  817. int len = cpumask_scnprintf(page, count, desc->affinity);
  818. if (count - len < 2)
  819. return -EINVAL;
  820. page[len++] = '\n';
  821. page[len] = '\0';
  822. return len;
  823. }
  824. static int
  825. irq_affinity_write_proc(struct file *file, const char __user *buffer,
  826. unsigned long count, void *data)
  827. {
  828. unsigned int irq = (unsigned int)data;
  829. struct irqdesc *desc = irq_desc + irq;
  830. cpumask_t affinity, tmp;
  831. int ret = -EIO;
  832. if (!desc->chip->set_cpu)
  833. goto out;
  834. ret = cpumask_parse(buffer, count, affinity);
  835. if (ret)
  836. goto out;
  837. cpus_and(tmp, affinity, cpu_online_map);
  838. if (cpus_empty(tmp)) {
  839. ret = -EINVAL;
  840. goto out;
  841. }
  842. desc->affinity = affinity;
  843. route_irq(desc, irq, first_cpu(tmp));
  844. ret = count;
  845. out:
  846. return ret;
  847. }
  848. #endif
  849. #endif
  850. void __init init_irq_proc(void)
  851. {
  852. #if defined(CONFIG_SMP) && defined(CONFIG_PROC_FS)
  853. struct proc_dir_entry *dir;
  854. int irq;
  855. dir = proc_mkdir("irq", NULL);
  856. if (!dir)
  857. return;
  858. for (irq = 0; irq < NR_IRQS; irq++) {
  859. struct proc_dir_entry *entry;
  860. struct irqdesc *desc;
  861. char name[16];
  862. desc = irq_desc + irq;
  863. memset(name, 0, sizeof(name));
  864. snprintf(name, sizeof(name) - 1, "%u", irq);
  865. desc->procdir = proc_mkdir(name, dir);
  866. if (!desc->procdir)
  867. continue;
  868. entry = create_proc_entry("smp_affinity", 0600, desc->procdir);
  869. if (entry) {
  870. entry->nlink = 1;
  871. entry->data = (void *)irq;
  872. entry->read_proc = irq_affinity_read_proc;
  873. entry->write_proc = irq_affinity_write_proc;
  874. }
  875. }
  876. #endif
  877. }
  878. void __init init_IRQ(void)
  879. {
  880. struct irqdesc *desc;
  881. int irq;
  882. #ifdef CONFIG_SMP
  883. bad_irq_desc.affinity = CPU_MASK_ALL;
  884. bad_irq_desc.cpu = smp_processor_id();
  885. #endif
  886. for (irq = 0, desc = irq_desc; irq < NR_IRQS; irq++, desc++) {
  887. *desc = bad_irq_desc;
  888. INIT_LIST_HEAD(&desc->pend);
  889. }
  890. init_arch_irq();
  891. }
  892. static int __init noirqdebug_setup(char *str)
  893. {
  894. noirqdebug = 1;
  895. return 1;
  896. }
  897. __setup("noirqdebug", noirqdebug_setup);
  898. #ifdef CONFIG_HOTPLUG_CPU
  899. /*
  900. * The CPU has been marked offline. Migrate IRQs off this CPU. If
  901. * the affinity settings do not allow other CPUs, force them onto any
  902. * available CPU.
  903. */
  904. void migrate_irqs(void)
  905. {
  906. unsigned int i, cpu = smp_processor_id();
  907. for (i = 0; i < NR_IRQS; i++) {
  908. struct irqdesc *desc = irq_desc + i;
  909. if (desc->cpu == cpu) {
  910. unsigned int newcpu = any_online_cpu(desc->affinity);
  911. if (newcpu == NR_CPUS) {
  912. if (printk_ratelimit())
  913. printk(KERN_INFO "IRQ%u no longer affine to CPU%u\n",
  914. i, cpu);
  915. cpus_setall(desc->affinity);
  916. newcpu = any_online_cpu(desc->affinity);
  917. }
  918. route_irq(desc, i, newcpu);
  919. }
  920. }
  921. }
  922. #endif /* CONFIG_HOTPLUG_CPU */