irq.c 25 KB

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