irq.c 25 KB

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