chip.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878
  1. /*
  2. * linux/kernel/irq/chip.c
  3. *
  4. * Copyright (C) 1992, 1998-2006 Linus Torvalds, Ingo Molnar
  5. * Copyright (C) 2005-2006, Thomas Gleixner, Russell King
  6. *
  7. * This file contains the core interrupt handling code, for irq-chip
  8. * based architectures.
  9. *
  10. * Detailed information is available in Documentation/DocBook/genericirq
  11. */
  12. #include <linux/irq.h>
  13. #include <linux/msi.h>
  14. #include <linux/module.h>
  15. #include <linux/interrupt.h>
  16. #include <linux/kernel_stat.h>
  17. #include "internals.h"
  18. static void dynamic_irq_init_x(unsigned int irq, bool keep_chip_data)
  19. {
  20. struct irq_desc *desc;
  21. unsigned long flags;
  22. desc = irq_to_desc(irq);
  23. if (!desc) {
  24. WARN(1, KERN_ERR "Trying to initialize invalid IRQ%d\n", irq);
  25. return;
  26. }
  27. /* Ensure we don't have left over values from a previous use of this irq */
  28. raw_spin_lock_irqsave(&desc->lock, flags);
  29. desc->status = IRQ_DEFAULT_INIT_FLAGS;
  30. desc->irq_data.chip = &no_irq_chip;
  31. desc->handle_irq = handle_bad_irq;
  32. desc->depth = 1;
  33. desc->irq_data.msi_desc = NULL;
  34. desc->irq_data.handler_data = NULL;
  35. if (!keep_chip_data)
  36. desc->irq_data.chip_data = NULL;
  37. desc->action = NULL;
  38. desc->irq_count = 0;
  39. desc->irqs_unhandled = 0;
  40. #ifdef CONFIG_SMP
  41. cpumask_setall(desc->irq_data.affinity);
  42. #ifdef CONFIG_GENERIC_PENDING_IRQ
  43. cpumask_clear(desc->pending_mask);
  44. #endif
  45. #endif
  46. raw_spin_unlock_irqrestore(&desc->lock, flags);
  47. }
  48. /**
  49. * dynamic_irq_init - initialize a dynamically allocated irq
  50. * @irq: irq number to initialize
  51. */
  52. void dynamic_irq_init(unsigned int irq)
  53. {
  54. dynamic_irq_init_x(irq, false);
  55. }
  56. /**
  57. * dynamic_irq_init_keep_chip_data - initialize a dynamically allocated irq
  58. * @irq: irq number to initialize
  59. *
  60. * does not set irq_to_desc(irq)->irq_data.chip_data to NULL
  61. */
  62. void dynamic_irq_init_keep_chip_data(unsigned int irq)
  63. {
  64. dynamic_irq_init_x(irq, true);
  65. }
  66. static void dynamic_irq_cleanup_x(unsigned int irq, bool keep_chip_data)
  67. {
  68. struct irq_desc *desc = irq_to_desc(irq);
  69. unsigned long flags;
  70. if (!desc) {
  71. WARN(1, KERN_ERR "Trying to cleanup invalid IRQ%d\n", irq);
  72. return;
  73. }
  74. raw_spin_lock_irqsave(&desc->lock, flags);
  75. if (desc->action) {
  76. raw_spin_unlock_irqrestore(&desc->lock, flags);
  77. WARN(1, KERN_ERR "Destroying IRQ%d without calling free_irq\n",
  78. irq);
  79. return;
  80. }
  81. desc->irq_data.msi_desc = NULL;
  82. desc->irq_data.handler_data = NULL;
  83. if (!keep_chip_data)
  84. desc->irq_data.chip_data = NULL;
  85. desc->handle_irq = handle_bad_irq;
  86. desc->irq_data.chip = &no_irq_chip;
  87. desc->name = NULL;
  88. clear_kstat_irqs(desc);
  89. raw_spin_unlock_irqrestore(&desc->lock, flags);
  90. }
  91. /**
  92. * dynamic_irq_cleanup - cleanup a dynamically allocated irq
  93. * @irq: irq number to initialize
  94. */
  95. void dynamic_irq_cleanup(unsigned int irq)
  96. {
  97. dynamic_irq_cleanup_x(irq, false);
  98. }
  99. /**
  100. * dynamic_irq_cleanup_keep_chip_data - cleanup a dynamically allocated irq
  101. * @irq: irq number to initialize
  102. *
  103. * does not set irq_to_desc(irq)->irq_data.chip_data to NULL
  104. */
  105. void dynamic_irq_cleanup_keep_chip_data(unsigned int irq)
  106. {
  107. dynamic_irq_cleanup_x(irq, true);
  108. }
  109. /**
  110. * set_irq_chip - set the irq chip for an irq
  111. * @irq: irq number
  112. * @chip: pointer to irq chip description structure
  113. */
  114. int set_irq_chip(unsigned int irq, struct irq_chip *chip)
  115. {
  116. struct irq_desc *desc = irq_to_desc(irq);
  117. unsigned long flags;
  118. if (!desc) {
  119. WARN(1, KERN_ERR "Trying to install chip for IRQ%d\n", irq);
  120. return -EINVAL;
  121. }
  122. if (!chip)
  123. chip = &no_irq_chip;
  124. raw_spin_lock_irqsave(&desc->lock, flags);
  125. irq_chip_set_defaults(chip);
  126. desc->irq_data.chip = chip;
  127. raw_spin_unlock_irqrestore(&desc->lock, flags);
  128. return 0;
  129. }
  130. EXPORT_SYMBOL(set_irq_chip);
  131. /**
  132. * set_irq_type - set the irq trigger type for an irq
  133. * @irq: irq number
  134. * @type: IRQ_TYPE_{LEVEL,EDGE}_* value - see include/linux/irq.h
  135. */
  136. int set_irq_type(unsigned int irq, unsigned int type)
  137. {
  138. struct irq_desc *desc = irq_to_desc(irq);
  139. unsigned long flags;
  140. int ret = -ENXIO;
  141. if (!desc) {
  142. printk(KERN_ERR "Trying to set irq type for IRQ%d\n", irq);
  143. return -ENODEV;
  144. }
  145. type &= IRQ_TYPE_SENSE_MASK;
  146. if (type == IRQ_TYPE_NONE)
  147. return 0;
  148. raw_spin_lock_irqsave(&desc->lock, flags);
  149. ret = __irq_set_trigger(desc, irq, type);
  150. raw_spin_unlock_irqrestore(&desc->lock, flags);
  151. return ret;
  152. }
  153. EXPORT_SYMBOL(set_irq_type);
  154. /**
  155. * set_irq_data - set irq type data for an irq
  156. * @irq: Interrupt number
  157. * @data: Pointer to interrupt specific data
  158. *
  159. * Set the hardware irq controller data for an irq
  160. */
  161. int set_irq_data(unsigned int irq, void *data)
  162. {
  163. struct irq_desc *desc = irq_to_desc(irq);
  164. unsigned long flags;
  165. if (!desc) {
  166. printk(KERN_ERR
  167. "Trying to install controller data for IRQ%d\n", irq);
  168. return -EINVAL;
  169. }
  170. raw_spin_lock_irqsave(&desc->lock, flags);
  171. desc->irq_data.handler_data = data;
  172. raw_spin_unlock_irqrestore(&desc->lock, flags);
  173. return 0;
  174. }
  175. EXPORT_SYMBOL(set_irq_data);
  176. /**
  177. * set_irq_msi - set MSI descriptor data for an irq
  178. * @irq: Interrupt number
  179. * @entry: Pointer to MSI descriptor data
  180. *
  181. * Set the MSI descriptor entry for an irq
  182. */
  183. int set_irq_msi(unsigned int irq, struct msi_desc *entry)
  184. {
  185. struct irq_desc *desc = irq_to_desc(irq);
  186. unsigned long flags;
  187. if (!desc) {
  188. printk(KERN_ERR
  189. "Trying to install msi data for IRQ%d\n", irq);
  190. return -EINVAL;
  191. }
  192. raw_spin_lock_irqsave(&desc->lock, flags);
  193. desc->irq_data.msi_desc = entry;
  194. if (entry)
  195. entry->irq = irq;
  196. raw_spin_unlock_irqrestore(&desc->lock, flags);
  197. return 0;
  198. }
  199. /**
  200. * set_irq_chip_data - set irq chip data for an irq
  201. * @irq: Interrupt number
  202. * @data: Pointer to chip specific data
  203. *
  204. * Set the hardware irq chip data for an irq
  205. */
  206. int set_irq_chip_data(unsigned int irq, void *data)
  207. {
  208. struct irq_desc *desc = irq_to_desc(irq);
  209. unsigned long flags;
  210. if (!desc) {
  211. printk(KERN_ERR
  212. "Trying to install chip data for IRQ%d\n", irq);
  213. return -EINVAL;
  214. }
  215. if (!desc->irq_data.chip) {
  216. printk(KERN_ERR "BUG: bad set_irq_chip_data(IRQ#%d)\n", irq);
  217. return -EINVAL;
  218. }
  219. raw_spin_lock_irqsave(&desc->lock, flags);
  220. desc->irq_data.chip_data = data;
  221. raw_spin_unlock_irqrestore(&desc->lock, flags);
  222. return 0;
  223. }
  224. EXPORT_SYMBOL(set_irq_chip_data);
  225. struct irq_data *irq_get_irq_data(unsigned int irq)
  226. {
  227. struct irq_desc *desc = irq_to_desc(irq);
  228. return desc ? &desc->irq_data : NULL;
  229. }
  230. EXPORT_SYMBOL_GPL(irq_get_irq_data);
  231. /**
  232. * set_irq_nested_thread - Set/Reset the IRQ_NESTED_THREAD flag of an irq
  233. *
  234. * @irq: Interrupt number
  235. * @nest: 0 to clear / 1 to set the IRQ_NESTED_THREAD flag
  236. *
  237. * The IRQ_NESTED_THREAD flag indicates that on
  238. * request_threaded_irq() no separate interrupt thread should be
  239. * created for the irq as the handler are called nested in the
  240. * context of a demultiplexing interrupt handler thread.
  241. */
  242. void set_irq_nested_thread(unsigned int irq, int nest)
  243. {
  244. struct irq_desc *desc = irq_to_desc(irq);
  245. unsigned long flags;
  246. if (!desc)
  247. return;
  248. raw_spin_lock_irqsave(&desc->lock, flags);
  249. if (nest)
  250. desc->status |= IRQ_NESTED_THREAD;
  251. else
  252. desc->status &= ~IRQ_NESTED_THREAD;
  253. raw_spin_unlock_irqrestore(&desc->lock, flags);
  254. }
  255. EXPORT_SYMBOL_GPL(set_irq_nested_thread);
  256. /*
  257. * default enable function
  258. */
  259. static void default_enable(struct irq_data *data)
  260. {
  261. struct irq_desc *desc = irq_data_to_desc(data);
  262. desc->irq_data.chip->irq_unmask(&desc->irq_data);
  263. desc->status &= ~IRQ_MASKED;
  264. }
  265. /*
  266. * default disable function
  267. */
  268. static void default_disable(struct irq_data *data)
  269. {
  270. }
  271. /*
  272. * default startup function
  273. */
  274. static unsigned int default_startup(struct irq_data *data)
  275. {
  276. struct irq_desc *desc = irq_data_to_desc(data);
  277. desc->irq_data.chip->irq_enable(data);
  278. return 0;
  279. }
  280. /*
  281. * default shutdown function
  282. */
  283. static void default_shutdown(struct irq_data *data)
  284. {
  285. struct irq_desc *desc = irq_data_to_desc(data);
  286. desc->irq_data.chip->irq_mask(&desc->irq_data);
  287. desc->status |= IRQ_MASKED;
  288. }
  289. #ifndef CONFIG_GENERIC_HARDIRQS_NO_DEPRECATED
  290. /* Temporary migration helpers */
  291. static void compat_irq_mask(struct irq_data *data)
  292. {
  293. data->chip->mask(data->irq);
  294. }
  295. static void compat_irq_unmask(struct irq_data *data)
  296. {
  297. data->chip->unmask(data->irq);
  298. }
  299. static void compat_irq_ack(struct irq_data *data)
  300. {
  301. data->chip->ack(data->irq);
  302. }
  303. static void compat_irq_mask_ack(struct irq_data *data)
  304. {
  305. data->chip->mask_ack(data->irq);
  306. }
  307. static void compat_irq_eoi(struct irq_data *data)
  308. {
  309. data->chip->eoi(data->irq);
  310. }
  311. static void compat_irq_enable(struct irq_data *data)
  312. {
  313. data->chip->enable(data->irq);
  314. }
  315. static void compat_irq_disable(struct irq_data *data)
  316. {
  317. data->chip->disable(data->irq);
  318. }
  319. static void compat_irq_shutdown(struct irq_data *data)
  320. {
  321. data->chip->shutdown(data->irq);
  322. }
  323. static unsigned int compat_irq_startup(struct irq_data *data)
  324. {
  325. return data->chip->startup(data->irq);
  326. }
  327. static int compat_irq_set_affinity(struct irq_data *data,
  328. const struct cpumask *dest, bool force)
  329. {
  330. return data->chip->set_affinity(data->irq, dest);
  331. }
  332. static int compat_irq_set_type(struct irq_data *data, unsigned int type)
  333. {
  334. return data->chip->set_type(data->irq, type);
  335. }
  336. static int compat_irq_set_wake(struct irq_data *data, unsigned int on)
  337. {
  338. return data->chip->set_wake(data->irq, on);
  339. }
  340. static int compat_irq_retrigger(struct irq_data *data)
  341. {
  342. return data->chip->retrigger(data->irq);
  343. }
  344. static void compat_bus_lock(struct irq_data *data)
  345. {
  346. data->chip->bus_lock(data->irq);
  347. }
  348. static void compat_bus_sync_unlock(struct irq_data *data)
  349. {
  350. data->chip->bus_sync_unlock(data->irq);
  351. }
  352. #endif
  353. /*
  354. * Fixup enable/disable function pointers
  355. */
  356. void irq_chip_set_defaults(struct irq_chip *chip)
  357. {
  358. #ifndef CONFIG_GENERIC_HARDIRQS_NO_DEPRECATED
  359. /*
  360. * Compat fixup functions need to be before we set the
  361. * defaults for enable/disable/startup/shutdown
  362. */
  363. if (chip->enable)
  364. chip->irq_enable = compat_irq_enable;
  365. if (chip->disable)
  366. chip->irq_disable = compat_irq_disable;
  367. if (chip->shutdown)
  368. chip->irq_shutdown = compat_irq_shutdown;
  369. if (chip->startup)
  370. chip->irq_startup = compat_irq_startup;
  371. #endif
  372. /*
  373. * The real defaults
  374. */
  375. if (!chip->irq_enable)
  376. chip->irq_enable = default_enable;
  377. if (!chip->irq_disable)
  378. chip->irq_disable = default_disable;
  379. if (!chip->irq_startup)
  380. chip->irq_startup = default_startup;
  381. /*
  382. * We use chip->irq_disable, when the user provided its own. When
  383. * we have default_disable set for chip->irq_disable, then we need
  384. * to use default_shutdown, otherwise the irq line is not
  385. * disabled on free_irq():
  386. */
  387. if (!chip->irq_shutdown)
  388. chip->irq_shutdown = chip->irq_disable != default_disable ?
  389. chip->irq_disable : default_shutdown;
  390. #ifndef CONFIG_GENERIC_HARDIRQS_NO_DEPRECATED
  391. if (!chip->end)
  392. chip->end = dummy_irq_chip.end;
  393. /*
  394. * Now fix up the remaining compat handlers
  395. */
  396. if (chip->bus_lock)
  397. chip->irq_bus_lock = compat_bus_lock;
  398. if (chip->bus_sync_unlock)
  399. chip->irq_bus_sync_unlock = compat_bus_sync_unlock;
  400. if (chip->mask)
  401. chip->irq_mask = compat_irq_mask;
  402. if (chip->unmask)
  403. chip->irq_unmask = compat_irq_unmask;
  404. if (chip->ack)
  405. chip->irq_ack = compat_irq_ack;
  406. if (chip->mask_ack)
  407. chip->irq_mask_ack = compat_irq_mask_ack;
  408. if (chip->eoi)
  409. chip->irq_eoi = compat_irq_eoi;
  410. if (chip->set_affinity)
  411. chip->irq_set_affinity = compat_irq_set_affinity;
  412. if (chip->set_type)
  413. chip->irq_set_type = compat_irq_set_type;
  414. if (chip->set_wake)
  415. chip->irq_set_wake = compat_irq_set_wake;
  416. if (chip->retrigger)
  417. chip->irq_retrigger = compat_irq_retrigger;
  418. #endif
  419. }
  420. static inline void mask_ack_irq(struct irq_desc *desc)
  421. {
  422. if (desc->irq_data.chip->irq_mask_ack)
  423. desc->irq_data.chip->irq_mask_ack(&desc->irq_data);
  424. else {
  425. desc->irq_data.chip->irq_mask(&desc->irq_data);
  426. if (desc->irq_data.chip->irq_ack)
  427. desc->irq_data.chip->irq_ack(&desc->irq_data);
  428. }
  429. desc->status |= IRQ_MASKED;
  430. }
  431. static inline void mask_irq(struct irq_desc *desc)
  432. {
  433. if (desc->irq_data.chip->irq_mask) {
  434. desc->irq_data.chip->irq_mask(&desc->irq_data);
  435. desc->status |= IRQ_MASKED;
  436. }
  437. }
  438. static inline void unmask_irq(struct irq_desc *desc)
  439. {
  440. if (desc->irq_data.chip->irq_unmask) {
  441. desc->irq_data.chip->irq_unmask(&desc->irq_data);
  442. desc->status &= ~IRQ_MASKED;
  443. }
  444. }
  445. /*
  446. * handle_nested_irq - Handle a nested irq from a irq thread
  447. * @irq: the interrupt number
  448. *
  449. * Handle interrupts which are nested into a threaded interrupt
  450. * handler. The handler function is called inside the calling
  451. * threads context.
  452. */
  453. void handle_nested_irq(unsigned int irq)
  454. {
  455. struct irq_desc *desc = irq_to_desc(irq);
  456. struct irqaction *action;
  457. irqreturn_t action_ret;
  458. might_sleep();
  459. raw_spin_lock_irq(&desc->lock);
  460. kstat_incr_irqs_this_cpu(irq, desc);
  461. action = desc->action;
  462. if (unlikely(!action || (desc->status & IRQ_DISABLED)))
  463. goto out_unlock;
  464. desc->status |= IRQ_INPROGRESS;
  465. raw_spin_unlock_irq(&desc->lock);
  466. action_ret = action->thread_fn(action->irq, action->dev_id);
  467. if (!noirqdebug)
  468. note_interrupt(irq, desc, action_ret);
  469. raw_spin_lock_irq(&desc->lock);
  470. desc->status &= ~IRQ_INPROGRESS;
  471. out_unlock:
  472. raw_spin_unlock_irq(&desc->lock);
  473. }
  474. EXPORT_SYMBOL_GPL(handle_nested_irq);
  475. /**
  476. * handle_simple_irq - Simple and software-decoded IRQs.
  477. * @irq: the interrupt number
  478. * @desc: the interrupt description structure for this irq
  479. *
  480. * Simple interrupts are either sent from a demultiplexing interrupt
  481. * handler or come from hardware, where no interrupt hardware control
  482. * is necessary.
  483. *
  484. * Note: The caller is expected to handle the ack, clear, mask and
  485. * unmask issues if necessary.
  486. */
  487. void
  488. handle_simple_irq(unsigned int irq, struct irq_desc *desc)
  489. {
  490. struct irqaction *action;
  491. irqreturn_t action_ret;
  492. raw_spin_lock(&desc->lock);
  493. if (unlikely(desc->status & IRQ_INPROGRESS))
  494. goto out_unlock;
  495. desc->status &= ~(IRQ_REPLAY | IRQ_WAITING);
  496. kstat_incr_irqs_this_cpu(irq, desc);
  497. action = desc->action;
  498. if (unlikely(!action || (desc->status & IRQ_DISABLED)))
  499. goto out_unlock;
  500. desc->status |= IRQ_INPROGRESS;
  501. raw_spin_unlock(&desc->lock);
  502. action_ret = handle_IRQ_event(irq, action);
  503. if (!noirqdebug)
  504. note_interrupt(irq, desc, action_ret);
  505. raw_spin_lock(&desc->lock);
  506. desc->status &= ~IRQ_INPROGRESS;
  507. out_unlock:
  508. raw_spin_unlock(&desc->lock);
  509. }
  510. /**
  511. * handle_level_irq - Level type irq handler
  512. * @irq: the interrupt number
  513. * @desc: the interrupt description structure for this irq
  514. *
  515. * Level type interrupts are active as long as the hardware line has
  516. * the active level. This may require to mask the interrupt and unmask
  517. * it after the associated handler has acknowledged the device, so the
  518. * interrupt line is back to inactive.
  519. */
  520. void
  521. handle_level_irq(unsigned int irq, struct irq_desc *desc)
  522. {
  523. struct irqaction *action;
  524. irqreturn_t action_ret;
  525. raw_spin_lock(&desc->lock);
  526. mask_ack_irq(desc);
  527. if (unlikely(desc->status & IRQ_INPROGRESS))
  528. goto out_unlock;
  529. desc->status &= ~(IRQ_REPLAY | IRQ_WAITING);
  530. kstat_incr_irqs_this_cpu(irq, desc);
  531. /*
  532. * If its disabled or no action available
  533. * keep it masked and get out of here
  534. */
  535. action = desc->action;
  536. if (unlikely(!action || (desc->status & IRQ_DISABLED)))
  537. goto out_unlock;
  538. desc->status |= IRQ_INPROGRESS;
  539. raw_spin_unlock(&desc->lock);
  540. action_ret = handle_IRQ_event(irq, action);
  541. if (!noirqdebug)
  542. note_interrupt(irq, desc, action_ret);
  543. raw_spin_lock(&desc->lock);
  544. desc->status &= ~IRQ_INPROGRESS;
  545. if (!(desc->status & (IRQ_DISABLED | IRQ_ONESHOT)))
  546. unmask_irq(desc);
  547. out_unlock:
  548. raw_spin_unlock(&desc->lock);
  549. }
  550. EXPORT_SYMBOL_GPL(handle_level_irq);
  551. /**
  552. * handle_fasteoi_irq - irq handler for transparent controllers
  553. * @irq: the interrupt number
  554. * @desc: the interrupt description structure for this irq
  555. *
  556. * Only a single callback will be issued to the chip: an ->eoi()
  557. * call when the interrupt has been serviced. This enables support
  558. * for modern forms of interrupt handlers, which handle the flow
  559. * details in hardware, transparently.
  560. */
  561. void
  562. handle_fasteoi_irq(unsigned int irq, struct irq_desc *desc)
  563. {
  564. struct irqaction *action;
  565. irqreturn_t action_ret;
  566. raw_spin_lock(&desc->lock);
  567. if (unlikely(desc->status & IRQ_INPROGRESS))
  568. goto out;
  569. desc->status &= ~(IRQ_REPLAY | IRQ_WAITING);
  570. kstat_incr_irqs_this_cpu(irq, desc);
  571. /*
  572. * If its disabled or no action available
  573. * then mask it and get out of here:
  574. */
  575. action = desc->action;
  576. if (unlikely(!action || (desc->status & IRQ_DISABLED))) {
  577. desc->status |= IRQ_PENDING;
  578. mask_irq(desc);
  579. goto out;
  580. }
  581. desc->status |= IRQ_INPROGRESS;
  582. desc->status &= ~IRQ_PENDING;
  583. raw_spin_unlock(&desc->lock);
  584. action_ret = handle_IRQ_event(irq, action);
  585. if (!noirqdebug)
  586. note_interrupt(irq, desc, action_ret);
  587. raw_spin_lock(&desc->lock);
  588. desc->status &= ~IRQ_INPROGRESS;
  589. out:
  590. desc->irq_data.chip->irq_eoi(&desc->irq_data);
  591. raw_spin_unlock(&desc->lock);
  592. }
  593. /**
  594. * handle_edge_irq - edge type IRQ handler
  595. * @irq: the interrupt number
  596. * @desc: the interrupt description structure for this irq
  597. *
  598. * Interrupt occures on the falling and/or rising edge of a hardware
  599. * signal. The occurence is latched into the irq controller hardware
  600. * and must be acked in order to be reenabled. After the ack another
  601. * interrupt can happen on the same source even before the first one
  602. * is handled by the associated event handler. If this happens it
  603. * might be necessary to disable (mask) the interrupt depending on the
  604. * controller hardware. This requires to reenable the interrupt inside
  605. * of the loop which handles the interrupts which have arrived while
  606. * the handler was running. If all pending interrupts are handled, the
  607. * loop is left.
  608. */
  609. void
  610. handle_edge_irq(unsigned int irq, struct irq_desc *desc)
  611. {
  612. raw_spin_lock(&desc->lock);
  613. desc->status &= ~(IRQ_REPLAY | IRQ_WAITING);
  614. /*
  615. * If we're currently running this IRQ, or its disabled,
  616. * we shouldn't process the IRQ. Mark it pending, handle
  617. * the necessary masking and go out
  618. */
  619. if (unlikely((desc->status & (IRQ_INPROGRESS | IRQ_DISABLED)) ||
  620. !desc->action)) {
  621. desc->status |= (IRQ_PENDING | IRQ_MASKED);
  622. mask_ack_irq(desc);
  623. goto out_unlock;
  624. }
  625. kstat_incr_irqs_this_cpu(irq, desc);
  626. /* Start handling the irq */
  627. desc->irq_data.chip->irq_ack(&desc->irq_data);
  628. /* Mark the IRQ currently in progress.*/
  629. desc->status |= IRQ_INPROGRESS;
  630. do {
  631. struct irqaction *action = desc->action;
  632. irqreturn_t action_ret;
  633. if (unlikely(!action)) {
  634. mask_irq(desc);
  635. goto out_unlock;
  636. }
  637. /*
  638. * When another irq arrived while we were handling
  639. * one, we could have masked the irq.
  640. * Renable it, if it was not disabled in meantime.
  641. */
  642. if (unlikely((desc->status &
  643. (IRQ_PENDING | IRQ_MASKED | IRQ_DISABLED)) ==
  644. (IRQ_PENDING | IRQ_MASKED))) {
  645. unmask_irq(desc);
  646. }
  647. desc->status &= ~IRQ_PENDING;
  648. raw_spin_unlock(&desc->lock);
  649. action_ret = handle_IRQ_event(irq, action);
  650. if (!noirqdebug)
  651. note_interrupt(irq, desc, action_ret);
  652. raw_spin_lock(&desc->lock);
  653. } while ((desc->status & (IRQ_PENDING | IRQ_DISABLED)) == IRQ_PENDING);
  654. desc->status &= ~IRQ_INPROGRESS;
  655. out_unlock:
  656. raw_spin_unlock(&desc->lock);
  657. }
  658. /**
  659. * handle_percpu_irq - Per CPU local irq handler
  660. * @irq: the interrupt number
  661. * @desc: the interrupt description structure for this irq
  662. *
  663. * Per CPU interrupts on SMP machines without locking requirements
  664. */
  665. void
  666. handle_percpu_irq(unsigned int irq, struct irq_desc *desc)
  667. {
  668. irqreturn_t action_ret;
  669. kstat_incr_irqs_this_cpu(irq, desc);
  670. if (desc->irq_data.chip->irq_ack)
  671. desc->irq_data.chip->irq_ack(&desc->irq_data);
  672. action_ret = handle_IRQ_event(irq, desc->action);
  673. if (!noirqdebug)
  674. note_interrupt(irq, desc, action_ret);
  675. if (desc->irq_data.chip->irq_eoi)
  676. desc->irq_data.chip->irq_eoi(&desc->irq_data);
  677. }
  678. void
  679. __set_irq_handler(unsigned int irq, irq_flow_handler_t handle, int is_chained,
  680. const char *name)
  681. {
  682. struct irq_desc *desc = irq_to_desc(irq);
  683. unsigned long flags;
  684. if (!desc) {
  685. printk(KERN_ERR
  686. "Trying to install type control for IRQ%d\n", irq);
  687. return;
  688. }
  689. if (!handle)
  690. handle = handle_bad_irq;
  691. else if (desc->irq_data.chip == &no_irq_chip) {
  692. printk(KERN_WARNING "Trying to install %sinterrupt handler "
  693. "for IRQ%d\n", is_chained ? "chained " : "", irq);
  694. /*
  695. * Some ARM implementations install a handler for really dumb
  696. * interrupt hardware without setting an irq_chip. This worked
  697. * with the ARM no_irq_chip but the check in setup_irq would
  698. * prevent us to setup the interrupt at all. Switch it to
  699. * dummy_irq_chip for easy transition.
  700. */
  701. desc->irq_data.chip = &dummy_irq_chip;
  702. }
  703. chip_bus_lock(desc);
  704. raw_spin_lock_irqsave(&desc->lock, flags);
  705. /* Uninstall? */
  706. if (handle == handle_bad_irq) {
  707. if (desc->irq_data.chip != &no_irq_chip)
  708. mask_ack_irq(desc);
  709. desc->status |= IRQ_DISABLED;
  710. desc->depth = 1;
  711. }
  712. desc->handle_irq = handle;
  713. desc->name = name;
  714. if (handle != handle_bad_irq && is_chained) {
  715. desc->status &= ~IRQ_DISABLED;
  716. desc->status |= IRQ_NOREQUEST | IRQ_NOPROBE;
  717. desc->depth = 0;
  718. desc->irq_data.chip->irq_startup(&desc->irq_data);
  719. }
  720. raw_spin_unlock_irqrestore(&desc->lock, flags);
  721. chip_bus_sync_unlock(desc);
  722. }
  723. EXPORT_SYMBOL_GPL(__set_irq_handler);
  724. void
  725. set_irq_chip_and_handler(unsigned int irq, struct irq_chip *chip,
  726. irq_flow_handler_t handle)
  727. {
  728. set_irq_chip(irq, chip);
  729. __set_irq_handler(irq, handle, 0, NULL);
  730. }
  731. void
  732. set_irq_chip_and_handler_name(unsigned int irq, struct irq_chip *chip,
  733. irq_flow_handler_t handle, const char *name)
  734. {
  735. set_irq_chip(irq, chip);
  736. __set_irq_handler(irq, handle, 0, name);
  737. }
  738. void irq_modify_status(unsigned int irq, unsigned long clr, unsigned long set)
  739. {
  740. struct irq_desc *desc = irq_to_desc(irq);
  741. unsigned long flags;
  742. if (!desc)
  743. return;
  744. /* Sanitize flags */
  745. set &= IRQF_MODIFY_MASK;
  746. clr &= IRQF_MODIFY_MASK;
  747. raw_spin_lock_irqsave(&desc->lock, flags);
  748. desc->status &= ~clr;
  749. desc->status |= set;
  750. raw_spin_unlock_irqrestore(&desc->lock, flags);
  751. }