irq.c 25 KB

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