irq.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098
  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 (noirqdebug)
  274. return;
  275. if (ret != IRQ_HANDLED && ret != IRQ_NONE) {
  276. if (!count)
  277. return;
  278. count--;
  279. printk("irq%u: bogus retval mask %x\n", irq, ret);
  280. } else {
  281. desc->irqs_unhandled++;
  282. if (desc->irqs_unhandled <= 99900)
  283. return;
  284. desc->irqs_unhandled = 0;
  285. printk("irq%u: nobody cared\n", irq);
  286. }
  287. show_regs(regs);
  288. dump_stack();
  289. printk(KERN_ERR "handlers:");
  290. action = desc->action;
  291. do {
  292. printk("\n" KERN_ERR "[<%p>]", action->handler);
  293. print_symbol(" (%s)", (unsigned long)action->handler);
  294. action = action->next;
  295. } while (action);
  296. printk("\n");
  297. }
  298. static int
  299. __do_irq(unsigned int irq, struct irqaction *action, struct pt_regs *regs)
  300. {
  301. unsigned int status;
  302. int ret, retval = 0;
  303. spin_unlock(&irq_controller_lock);
  304. #ifdef CONFIG_NO_IDLE_HZ
  305. if (!(action->flags & SA_TIMER) && system_timer->dyn_tick != NULL) {
  306. write_seqlock(&xtime_lock);
  307. if (system_timer->dyn_tick->state & DYN_TICK_ENABLED)
  308. system_timer->dyn_tick->handler(irq, 0, regs);
  309. write_sequnlock(&xtime_lock);
  310. }
  311. #endif
  312. if (!(action->flags & SA_INTERRUPT))
  313. local_irq_enable();
  314. status = 0;
  315. do {
  316. ret = action->handler(irq, action->dev_id, regs);
  317. if (ret == IRQ_HANDLED)
  318. status |= action->flags;
  319. retval |= ret;
  320. action = action->next;
  321. } while (action);
  322. if (status & SA_SAMPLE_RANDOM)
  323. add_interrupt_randomness(irq);
  324. spin_lock_irq(&irq_controller_lock);
  325. return retval;
  326. }
  327. /*
  328. * This is for software-decoded IRQs. The caller is expected to
  329. * handle the ack, clear, mask and unmask issues.
  330. */
  331. void
  332. do_simple_IRQ(unsigned int irq, struct irqdesc *desc, struct pt_regs *regs)
  333. {
  334. struct irqaction *action;
  335. const unsigned int cpu = smp_processor_id();
  336. desc->triggered = 1;
  337. kstat_cpu(cpu).irqs[irq]++;
  338. smp_set_running(desc);
  339. action = desc->action;
  340. if (action) {
  341. int ret = __do_irq(irq, action, regs);
  342. if (ret != IRQ_HANDLED)
  343. report_bad_irq(irq, regs, desc, ret);
  344. }
  345. smp_clear_running(desc);
  346. }
  347. /*
  348. * Most edge-triggered IRQ implementations seem to take a broken
  349. * approach to this. Hence the complexity.
  350. */
  351. void
  352. do_edge_IRQ(unsigned int irq, struct irqdesc *desc, struct pt_regs *regs)
  353. {
  354. const unsigned int cpu = smp_processor_id();
  355. desc->triggered = 1;
  356. /*
  357. * If we're currently running this IRQ, or its disabled,
  358. * we shouldn't process the IRQ. Instead, turn on the
  359. * hardware masks.
  360. */
  361. if (unlikely(desc->running || desc->disable_depth))
  362. goto running;
  363. /*
  364. * Acknowledge and clear the IRQ, but don't mask it.
  365. */
  366. desc->chip->ack(irq);
  367. /*
  368. * Mark the IRQ currently in progress.
  369. */
  370. desc->running = 1;
  371. kstat_cpu(cpu).irqs[irq]++;
  372. do {
  373. struct irqaction *action;
  374. action = desc->action;
  375. if (!action)
  376. break;
  377. if (desc->pending && !desc->disable_depth) {
  378. desc->pending = 0;
  379. desc->chip->unmask(irq);
  380. }
  381. __do_irq(irq, action, regs);
  382. } while (desc->pending && !desc->disable_depth);
  383. desc->running = 0;
  384. /*
  385. * If we were disabled or freed, shut down the handler.
  386. */
  387. if (likely(desc->action && !check_irq_lock(desc, irq, regs)))
  388. return;
  389. running:
  390. /*
  391. * We got another IRQ while this one was masked or
  392. * currently running. Delay it.
  393. */
  394. desc->pending = 1;
  395. desc->chip->mask(irq);
  396. desc->chip->ack(irq);
  397. }
  398. /*
  399. * Level-based IRQ handler. Nice and simple.
  400. */
  401. void
  402. do_level_IRQ(unsigned int irq, struct irqdesc *desc, struct pt_regs *regs)
  403. {
  404. struct irqaction *action;
  405. const unsigned int cpu = smp_processor_id();
  406. desc->triggered = 1;
  407. /*
  408. * Acknowledge, clear _AND_ disable the interrupt.
  409. */
  410. desc->chip->ack(irq);
  411. if (likely(!desc->disable_depth)) {
  412. kstat_cpu(cpu).irqs[irq]++;
  413. smp_set_running(desc);
  414. /*
  415. * Return with this interrupt masked if no action
  416. */
  417. action = desc->action;
  418. if (action) {
  419. int ret = __do_irq(irq, desc->action, regs);
  420. if (ret != IRQ_HANDLED)
  421. report_bad_irq(irq, regs, desc, ret);
  422. if (likely(!desc->disable_depth &&
  423. !check_irq_lock(desc, irq, regs)))
  424. desc->chip->unmask(irq);
  425. }
  426. smp_clear_running(desc);
  427. }
  428. }
  429. static void do_pending_irqs(struct pt_regs *regs)
  430. {
  431. struct list_head head, *l, *n;
  432. do {
  433. struct irqdesc *desc;
  434. /*
  435. * First, take the pending interrupts off the list.
  436. * The act of calling the handlers may add some IRQs
  437. * back onto the list.
  438. */
  439. head = irq_pending;
  440. INIT_LIST_HEAD(&irq_pending);
  441. head.next->prev = &head;
  442. head.prev->next = &head;
  443. /*
  444. * Now run each entry. We must delete it from our
  445. * list before calling the handler.
  446. */
  447. list_for_each_safe(l, n, &head) {
  448. desc = list_entry(l, struct irqdesc, pend);
  449. list_del_init(&desc->pend);
  450. desc_handle_irq(desc - irq_desc, desc, regs);
  451. }
  452. /*
  453. * The list must be empty.
  454. */
  455. BUG_ON(!list_empty(&head));
  456. } while (!list_empty(&irq_pending));
  457. }
  458. /*
  459. * do_IRQ handles all hardware IRQ's. Decoded IRQs should not
  460. * come via this function. Instead, they should provide their
  461. * own 'handler'
  462. */
  463. asmlinkage void asm_do_IRQ(unsigned int irq, struct pt_regs *regs)
  464. {
  465. struct irqdesc *desc = irq_desc + irq;
  466. /*
  467. * Some hardware gives randomly wrong interrupts. Rather
  468. * than crashing, do something sensible.
  469. */
  470. if (irq >= NR_IRQS)
  471. desc = &bad_irq_desc;
  472. irq_enter();
  473. spin_lock(&irq_controller_lock);
  474. desc_handle_irq(irq, desc, regs);
  475. /*
  476. * Now re-run any pending interrupts.
  477. */
  478. if (!list_empty(&irq_pending))
  479. do_pending_irqs(regs);
  480. irq_finish(irq);
  481. spin_unlock(&irq_controller_lock);
  482. irq_exit();
  483. }
  484. void __set_irq_handler(unsigned int irq, irq_handler_t handle, int is_chained)
  485. {
  486. struct irqdesc *desc;
  487. unsigned long flags;
  488. if (irq >= NR_IRQS) {
  489. printk(KERN_ERR "Trying to install handler for IRQ%d\n", irq);
  490. return;
  491. }
  492. if (handle == NULL)
  493. handle = do_bad_IRQ;
  494. desc = irq_desc + irq;
  495. if (is_chained && desc->chip == &bad_chip)
  496. printk(KERN_WARNING "Trying to install chained handler for IRQ%d\n", irq);
  497. spin_lock_irqsave(&irq_controller_lock, flags);
  498. if (handle == do_bad_IRQ) {
  499. desc->chip->mask(irq);
  500. desc->chip->ack(irq);
  501. desc->disable_depth = 1;
  502. }
  503. desc->handle = handle;
  504. if (handle != do_bad_IRQ && is_chained) {
  505. desc->valid = 0;
  506. desc->probe_ok = 0;
  507. desc->disable_depth = 0;
  508. desc->chip->unmask(irq);
  509. }
  510. spin_unlock_irqrestore(&irq_controller_lock, flags);
  511. }
  512. void set_irq_chip(unsigned int irq, struct irqchip *chip)
  513. {
  514. struct irqdesc *desc;
  515. unsigned long flags;
  516. if (irq >= NR_IRQS) {
  517. printk(KERN_ERR "Trying to install chip for IRQ%d\n", irq);
  518. return;
  519. }
  520. if (chip == NULL)
  521. chip = &bad_chip;
  522. desc = irq_desc + irq;
  523. spin_lock_irqsave(&irq_controller_lock, flags);
  524. desc->chip = chip;
  525. spin_unlock_irqrestore(&irq_controller_lock, flags);
  526. }
  527. int set_irq_type(unsigned int irq, unsigned int type)
  528. {
  529. struct irqdesc *desc;
  530. unsigned long flags;
  531. int ret = -ENXIO;
  532. if (irq >= NR_IRQS) {
  533. printk(KERN_ERR "Trying to set irq type for IRQ%d\n", irq);
  534. return -ENODEV;
  535. }
  536. desc = irq_desc + irq;
  537. if (desc->chip->set_type) {
  538. spin_lock_irqsave(&irq_controller_lock, flags);
  539. ret = desc->chip->set_type(irq, type);
  540. spin_unlock_irqrestore(&irq_controller_lock, flags);
  541. }
  542. return ret;
  543. }
  544. EXPORT_SYMBOL(set_irq_type);
  545. void set_irq_flags(unsigned int irq, unsigned int iflags)
  546. {
  547. struct irqdesc *desc;
  548. unsigned long flags;
  549. if (irq >= NR_IRQS) {
  550. printk(KERN_ERR "Trying to set irq flags for IRQ%d\n", irq);
  551. return;
  552. }
  553. desc = irq_desc + irq;
  554. spin_lock_irqsave(&irq_controller_lock, flags);
  555. desc->valid = (iflags & IRQF_VALID) != 0;
  556. desc->probe_ok = (iflags & IRQF_PROBE) != 0;
  557. desc->noautoenable = (iflags & IRQF_NOAUTOEN) != 0;
  558. spin_unlock_irqrestore(&irq_controller_lock, flags);
  559. }
  560. int setup_irq(unsigned int irq, struct irqaction *new)
  561. {
  562. int shared = 0;
  563. struct irqaction *old, **p;
  564. unsigned long flags;
  565. struct irqdesc *desc;
  566. /*
  567. * Some drivers like serial.c use request_irq() heavily,
  568. * so we have to be careful not to interfere with a
  569. * running system.
  570. */
  571. if (new->flags & SA_SAMPLE_RANDOM) {
  572. /*
  573. * This function might sleep, we want to call it first,
  574. * outside of the atomic block.
  575. * Yes, this might clear the entropy pool if the wrong
  576. * driver is attempted to be loaded, without actually
  577. * installing a new handler, but is this really a problem,
  578. * only the sysadmin is able to do this.
  579. */
  580. rand_initialize_irq(irq);
  581. }
  582. /*
  583. * The following block of code has to be executed atomically
  584. */
  585. desc = irq_desc + irq;
  586. spin_lock_irqsave(&irq_controller_lock, flags);
  587. p = &desc->action;
  588. if ((old = *p) != NULL) {
  589. /*
  590. * Can't share interrupts unless both agree to and are
  591. * the same type.
  592. */
  593. if (!(old->flags & new->flags & SA_SHIRQ) ||
  594. (~old->flags & new->flags) & SA_TRIGGER_MASK) {
  595. spin_unlock_irqrestore(&irq_controller_lock, flags);
  596. return -EBUSY;
  597. }
  598. /* add new interrupt at end of irq queue */
  599. do {
  600. p = &old->next;
  601. old = *p;
  602. } while (old);
  603. shared = 1;
  604. }
  605. *p = new;
  606. if (!shared) {
  607. desc->probing = 0;
  608. desc->running = 0;
  609. desc->pending = 0;
  610. desc->disable_depth = 1;
  611. if (new->flags & SA_TRIGGER_MASK &&
  612. desc->chip->set_type) {
  613. unsigned int type = new->flags & SA_TRIGGER_MASK;
  614. desc->chip->set_type(irq, type);
  615. }
  616. if (!desc->noautoenable) {
  617. desc->disable_depth = 0;
  618. desc->chip->unmask(irq);
  619. }
  620. }
  621. spin_unlock_irqrestore(&irq_controller_lock, flags);
  622. return 0;
  623. }
  624. /**
  625. * request_irq - allocate an interrupt line
  626. * @irq: Interrupt line to allocate
  627. * @handler: Function to be called when the IRQ occurs
  628. * @irqflags: Interrupt type flags
  629. * @devname: An ascii name for the claiming device
  630. * @dev_id: A cookie passed back to the handler function
  631. *
  632. * This call allocates interrupt resources and enables the
  633. * interrupt line and IRQ handling. From the point this
  634. * call is made your handler function may be invoked. Since
  635. * your handler function must clear any interrupt the board
  636. * raises, you must take care both to initialise your hardware
  637. * and to set up the interrupt handler in the right order.
  638. *
  639. * Dev_id must be globally unique. Normally the address of the
  640. * device data structure is used as the cookie. Since the handler
  641. * receives this value it makes sense to use it.
  642. *
  643. * If your interrupt is shared you must pass a non NULL dev_id
  644. * as this is required when freeing the interrupt.
  645. *
  646. * Flags:
  647. *
  648. * SA_SHIRQ Interrupt is shared
  649. *
  650. * SA_INTERRUPT Disable local interrupts while processing
  651. *
  652. * SA_SAMPLE_RANDOM The interrupt can be used for entropy
  653. *
  654. */
  655. int request_irq(unsigned int irq, irqreturn_t (*handler)(int, void *, struct pt_regs *),
  656. unsigned long irq_flags, const char * devname, void *dev_id)
  657. {
  658. unsigned long retval;
  659. struct irqaction *action;
  660. if (irq >= NR_IRQS || !irq_desc[irq].valid || !handler ||
  661. (irq_flags & SA_SHIRQ && !dev_id))
  662. return -EINVAL;
  663. action = (struct irqaction *)kmalloc(sizeof(struct irqaction), GFP_KERNEL);
  664. if (!action)
  665. return -ENOMEM;
  666. action->handler = handler;
  667. action->flags = irq_flags;
  668. cpus_clear(action->mask);
  669. action->name = devname;
  670. action->next = NULL;
  671. action->dev_id = dev_id;
  672. retval = setup_irq(irq, action);
  673. if (retval)
  674. kfree(action);
  675. return retval;
  676. }
  677. EXPORT_SYMBOL(request_irq);
  678. /**
  679. * free_irq - free an interrupt
  680. * @irq: Interrupt line to free
  681. * @dev_id: Device identity to free
  682. *
  683. * Remove an interrupt handler. The handler is removed and if the
  684. * interrupt line is no longer in use by any driver it is disabled.
  685. * On a shared IRQ the caller must ensure the interrupt is disabled
  686. * on the card it drives before calling this function.
  687. *
  688. * This function must not be called from interrupt context.
  689. */
  690. void free_irq(unsigned int irq, void *dev_id)
  691. {
  692. struct irqaction * action, **p;
  693. unsigned long flags;
  694. if (irq >= NR_IRQS || !irq_desc[irq].valid) {
  695. printk(KERN_ERR "Trying to free IRQ%d\n",irq);
  696. dump_stack();
  697. return;
  698. }
  699. spin_lock_irqsave(&irq_controller_lock, flags);
  700. for (p = &irq_desc[irq].action; (action = *p) != NULL; p = &action->next) {
  701. if (action->dev_id != dev_id)
  702. continue;
  703. /* Found it - now free it */
  704. *p = action->next;
  705. break;
  706. }
  707. spin_unlock_irqrestore(&irq_controller_lock, flags);
  708. if (!action) {
  709. printk(KERN_ERR "Trying to free free IRQ%d\n",irq);
  710. dump_stack();
  711. } else {
  712. synchronize_irq(irq);
  713. kfree(action);
  714. }
  715. }
  716. EXPORT_SYMBOL(free_irq);
  717. static DECLARE_MUTEX(probe_sem);
  718. /* Start the interrupt probing. Unlike other architectures,
  719. * we don't return a mask of interrupts from probe_irq_on,
  720. * but return the number of interrupts enabled for the probe.
  721. * The interrupts which have been enabled for probing is
  722. * instead recorded in the irq_desc structure.
  723. */
  724. unsigned long probe_irq_on(void)
  725. {
  726. unsigned int i, irqs = 0;
  727. unsigned long delay;
  728. down(&probe_sem);
  729. /*
  730. * first snaffle up any unassigned but
  731. * probe-able interrupts
  732. */
  733. spin_lock_irq(&irq_controller_lock);
  734. for (i = 0; i < NR_IRQS; i++) {
  735. if (!irq_desc[i].probe_ok || irq_desc[i].action)
  736. continue;
  737. irq_desc[i].probing = 1;
  738. irq_desc[i].triggered = 0;
  739. if (irq_desc[i].chip->set_type)
  740. irq_desc[i].chip->set_type(i, IRQT_PROBE);
  741. irq_desc[i].chip->unmask(i);
  742. irqs += 1;
  743. }
  744. spin_unlock_irq(&irq_controller_lock);
  745. /*
  746. * wait for spurious interrupts to mask themselves out again
  747. */
  748. for (delay = jiffies + HZ/10; time_before(jiffies, delay); )
  749. /* min 100ms delay */;
  750. /*
  751. * now filter out any obviously spurious interrupts
  752. */
  753. spin_lock_irq(&irq_controller_lock);
  754. for (i = 0; i < NR_IRQS; i++) {
  755. if (irq_desc[i].probing && irq_desc[i].triggered) {
  756. irq_desc[i].probing = 0;
  757. irqs -= 1;
  758. }
  759. }
  760. spin_unlock_irq(&irq_controller_lock);
  761. return irqs;
  762. }
  763. EXPORT_SYMBOL(probe_irq_on);
  764. unsigned int probe_irq_mask(unsigned long irqs)
  765. {
  766. unsigned int mask = 0, i;
  767. spin_lock_irq(&irq_controller_lock);
  768. for (i = 0; i < 16 && i < NR_IRQS; i++)
  769. if (irq_desc[i].probing && irq_desc[i].triggered)
  770. mask |= 1 << i;
  771. spin_unlock_irq(&irq_controller_lock);
  772. up(&probe_sem);
  773. return mask;
  774. }
  775. EXPORT_SYMBOL(probe_irq_mask);
  776. /*
  777. * Possible return values:
  778. * >= 0 - interrupt number
  779. * -1 - no interrupt/many interrupts
  780. */
  781. int probe_irq_off(unsigned long irqs)
  782. {
  783. unsigned int i;
  784. int irq_found = NO_IRQ;
  785. /*
  786. * look at the interrupts, and find exactly one
  787. * that we were probing has been triggered
  788. */
  789. spin_lock_irq(&irq_controller_lock);
  790. for (i = 0; i < NR_IRQS; i++) {
  791. if (irq_desc[i].probing &&
  792. irq_desc[i].triggered) {
  793. if (irq_found != NO_IRQ) {
  794. irq_found = NO_IRQ;
  795. goto out;
  796. }
  797. irq_found = i;
  798. }
  799. }
  800. if (irq_found == -1)
  801. irq_found = NO_IRQ;
  802. out:
  803. spin_unlock_irq(&irq_controller_lock);
  804. up(&probe_sem);
  805. return irq_found;
  806. }
  807. EXPORT_SYMBOL(probe_irq_off);
  808. #ifdef CONFIG_SMP
  809. static void route_irq(struct irqdesc *desc, unsigned int irq, unsigned int cpu)
  810. {
  811. pr_debug("IRQ%u: moving from cpu%u to cpu%u\n", irq, desc->cpu, cpu);
  812. spin_lock_irq(&irq_controller_lock);
  813. desc->cpu = cpu;
  814. desc->chip->set_cpu(desc, irq, cpu);
  815. spin_unlock_irq(&irq_controller_lock);
  816. }
  817. #ifdef CONFIG_PROC_FS
  818. static int
  819. irq_affinity_read_proc(char *page, char **start, off_t off, int count,
  820. int *eof, void *data)
  821. {
  822. struct irqdesc *desc = irq_desc + ((int)data);
  823. int len = cpumask_scnprintf(page, count, desc->affinity);
  824. if (count - len < 2)
  825. return -EINVAL;
  826. page[len++] = '\n';
  827. page[len] = '\0';
  828. return len;
  829. }
  830. static int
  831. irq_affinity_write_proc(struct file *file, const char __user *buffer,
  832. unsigned long count, void *data)
  833. {
  834. unsigned int irq = (unsigned int)data;
  835. struct irqdesc *desc = irq_desc + irq;
  836. cpumask_t affinity, tmp;
  837. int ret = -EIO;
  838. if (!desc->chip->set_cpu)
  839. goto out;
  840. ret = cpumask_parse(buffer, count, affinity);
  841. if (ret)
  842. goto out;
  843. cpus_and(tmp, affinity, cpu_online_map);
  844. if (cpus_empty(tmp)) {
  845. ret = -EINVAL;
  846. goto out;
  847. }
  848. desc->affinity = affinity;
  849. route_irq(desc, irq, first_cpu(tmp));
  850. ret = count;
  851. out:
  852. return ret;
  853. }
  854. #endif
  855. #endif
  856. void __init init_irq_proc(void)
  857. {
  858. #if defined(CONFIG_SMP) && defined(CONFIG_PROC_FS)
  859. struct proc_dir_entry *dir;
  860. int irq;
  861. dir = proc_mkdir("irq", NULL);
  862. if (!dir)
  863. return;
  864. for (irq = 0; irq < NR_IRQS; irq++) {
  865. struct proc_dir_entry *entry;
  866. struct irqdesc *desc;
  867. char name[16];
  868. desc = irq_desc + irq;
  869. memset(name, 0, sizeof(name));
  870. snprintf(name, sizeof(name) - 1, "%u", irq);
  871. desc->procdir = proc_mkdir(name, dir);
  872. if (!desc->procdir)
  873. continue;
  874. entry = create_proc_entry("smp_affinity", 0600, desc->procdir);
  875. if (entry) {
  876. entry->nlink = 1;
  877. entry->data = (void *)irq;
  878. entry->read_proc = irq_affinity_read_proc;
  879. entry->write_proc = irq_affinity_write_proc;
  880. }
  881. }
  882. #endif
  883. }
  884. void __init init_IRQ(void)
  885. {
  886. struct irqdesc *desc;
  887. int irq;
  888. #ifdef CONFIG_SMP
  889. bad_irq_desc.affinity = CPU_MASK_ALL;
  890. bad_irq_desc.cpu = smp_processor_id();
  891. #endif
  892. for (irq = 0, desc = irq_desc; irq < NR_IRQS; irq++, desc++) {
  893. *desc = bad_irq_desc;
  894. INIT_LIST_HEAD(&desc->pend);
  895. }
  896. init_arch_irq();
  897. }
  898. static int __init noirqdebug_setup(char *str)
  899. {
  900. noirqdebug = 1;
  901. return 1;
  902. }
  903. __setup("noirqdebug", noirqdebug_setup);
  904. #ifdef CONFIG_HOTPLUG_CPU
  905. /*
  906. * The CPU has been marked offline. Migrate IRQs off this CPU. If
  907. * the affinity settings do not allow other CPUs, force them onto any
  908. * available CPU.
  909. */
  910. void migrate_irqs(void)
  911. {
  912. unsigned int i, cpu = smp_processor_id();
  913. for (i = 0; i < NR_IRQS; i++) {
  914. struct irqdesc *desc = irq_desc + i;
  915. if (desc->cpu == cpu) {
  916. unsigned int newcpu = any_online_cpu(desc->affinity);
  917. if (newcpu == NR_CPUS) {
  918. if (printk_ratelimit())
  919. printk(KERN_INFO "IRQ%u no longer affine to CPU%u\n",
  920. i, cpu);
  921. cpus_setall(desc->affinity);
  922. newcpu = any_online_cpu(desc->affinity);
  923. }
  924. route_irq(desc, i, newcpu);
  925. }
  926. }
  927. }
  928. #endif /* CONFIG_HOTPLUG_CPU */