manage.c 32 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259
  1. /*
  2. * linux/kernel/irq/manage.c
  3. *
  4. * Copyright (C) 1992, 1998-2006 Linus Torvalds, Ingo Molnar
  5. * Copyright (C) 2005-2006 Thomas Gleixner
  6. *
  7. * This file contains driver APIs to the irq subsystem.
  8. */
  9. #include <linux/irq.h>
  10. #include <linux/kthread.h>
  11. #include <linux/module.h>
  12. #include <linux/random.h>
  13. #include <linux/interrupt.h>
  14. #include <linux/slab.h>
  15. #include <linux/sched.h>
  16. #include "internals.h"
  17. /**
  18. * synchronize_irq - wait for pending IRQ handlers (on other CPUs)
  19. * @irq: interrupt number to wait for
  20. *
  21. * This function waits for any pending IRQ handlers for this interrupt
  22. * to complete before returning. If you use this function while
  23. * holding a resource the IRQ handler may need you will deadlock.
  24. *
  25. * This function may be called - with care - from IRQ context.
  26. */
  27. void synchronize_irq(unsigned int irq)
  28. {
  29. struct irq_desc *desc = irq_to_desc(irq);
  30. unsigned int status;
  31. if (!desc)
  32. return;
  33. do {
  34. unsigned long flags;
  35. /*
  36. * Wait until we're out of the critical section. This might
  37. * give the wrong answer due to the lack of memory barriers.
  38. */
  39. while (desc->status & IRQ_INPROGRESS)
  40. cpu_relax();
  41. /* Ok, that indicated we're done: double-check carefully. */
  42. raw_spin_lock_irqsave(&desc->lock, flags);
  43. status = desc->status;
  44. raw_spin_unlock_irqrestore(&desc->lock, flags);
  45. /* Oops, that failed? */
  46. } while (status & IRQ_INPROGRESS);
  47. /*
  48. * We made sure that no hardirq handler is running. Now verify
  49. * that no threaded handlers are active.
  50. */
  51. wait_event(desc->wait_for_threads, !atomic_read(&desc->threads_active));
  52. }
  53. EXPORT_SYMBOL(synchronize_irq);
  54. #ifdef CONFIG_SMP
  55. cpumask_var_t irq_default_affinity;
  56. /**
  57. * irq_can_set_affinity - Check if the affinity of a given irq can be set
  58. * @irq: Interrupt to check
  59. *
  60. */
  61. int irq_can_set_affinity(unsigned int irq)
  62. {
  63. struct irq_desc *desc = irq_to_desc(irq);
  64. if (CHECK_IRQ_PER_CPU(desc->status) || !desc->irq_data.chip ||
  65. !desc->irq_data.chip->irq_set_affinity)
  66. return 0;
  67. return 1;
  68. }
  69. /**
  70. * irq_set_thread_affinity - Notify irq threads to adjust affinity
  71. * @desc: irq descriptor which has affitnity changed
  72. *
  73. * We just set IRQTF_AFFINITY and delegate the affinity setting
  74. * to the interrupt thread itself. We can not call
  75. * set_cpus_allowed_ptr() here as we hold desc->lock and this
  76. * code can be called from hard interrupt context.
  77. */
  78. void irq_set_thread_affinity(struct irq_desc *desc)
  79. {
  80. struct irqaction *action = desc->action;
  81. while (action) {
  82. if (action->thread)
  83. set_bit(IRQTF_AFFINITY, &action->thread_flags);
  84. action = action->next;
  85. }
  86. }
  87. /**
  88. * irq_set_affinity - Set the irq affinity of a given irq
  89. * @irq: Interrupt to set affinity
  90. * @cpumask: cpumask
  91. *
  92. */
  93. int irq_set_affinity(unsigned int irq, const struct cpumask *cpumask)
  94. {
  95. struct irq_desc *desc = irq_to_desc(irq);
  96. struct irq_chip *chip = desc->irq_data.chip;
  97. unsigned long flags;
  98. if (!chip->irq_set_affinity)
  99. return -EINVAL;
  100. raw_spin_lock_irqsave(&desc->lock, flags);
  101. #ifdef CONFIG_GENERIC_PENDING_IRQ
  102. if (desc->status & IRQ_MOVE_PCNTXT) {
  103. if (!chip->irq_set_affinity(&desc->irq_data, cpumask, false)) {
  104. cpumask_copy(desc->irq_data.affinity, cpumask);
  105. irq_set_thread_affinity(desc);
  106. }
  107. }
  108. else {
  109. desc->status |= IRQ_MOVE_PENDING;
  110. cpumask_copy(desc->pending_mask, cpumask);
  111. }
  112. #else
  113. if (!chip->irq_set_affinity(&desc->irq_data, cpumask, false)) {
  114. cpumask_copy(desc->irq_data.affinity, cpumask);
  115. irq_set_thread_affinity(desc);
  116. }
  117. #endif
  118. if (desc->affinity_notify) {
  119. kref_get(&desc->affinity_notify->kref);
  120. schedule_work(&desc->affinity_notify->work);
  121. }
  122. desc->status |= IRQ_AFFINITY_SET;
  123. raw_spin_unlock_irqrestore(&desc->lock, flags);
  124. return 0;
  125. }
  126. int irq_set_affinity_hint(unsigned int irq, const struct cpumask *m)
  127. {
  128. struct irq_desc *desc = irq_to_desc(irq);
  129. unsigned long flags;
  130. if (!desc)
  131. return -EINVAL;
  132. raw_spin_lock_irqsave(&desc->lock, flags);
  133. desc->affinity_hint = m;
  134. raw_spin_unlock_irqrestore(&desc->lock, flags);
  135. return 0;
  136. }
  137. EXPORT_SYMBOL_GPL(irq_set_affinity_hint);
  138. static void irq_affinity_notify(struct work_struct *work)
  139. {
  140. struct irq_affinity_notify *notify =
  141. container_of(work, struct irq_affinity_notify, work);
  142. struct irq_desc *desc = irq_to_desc(notify->irq);
  143. cpumask_var_t cpumask;
  144. unsigned long flags;
  145. if (!desc)
  146. goto out;
  147. if (!alloc_cpumask_var(&cpumask, GFP_KERNEL))
  148. goto out;
  149. raw_spin_lock_irqsave(&desc->lock, flags);
  150. #ifdef CONFIG_GENERIC_PENDING_IRQ
  151. if (desc->status & IRQ_MOVE_PENDING)
  152. cpumask_copy(cpumask, desc->pending_mask);
  153. else
  154. #endif
  155. cpumask_copy(cpumask, desc->irq_data.affinity);
  156. raw_spin_unlock_irqrestore(&desc->lock, flags);
  157. notify->notify(notify, cpumask);
  158. free_cpumask_var(cpumask);
  159. out:
  160. kref_put(&notify->kref, notify->release);
  161. }
  162. /**
  163. * irq_set_affinity_notifier - control notification of IRQ affinity changes
  164. * @irq: Interrupt for which to enable/disable notification
  165. * @notify: Context for notification, or %NULL to disable
  166. * notification. Function pointers must be initialised;
  167. * the other fields will be initialised by this function.
  168. *
  169. * Must be called in process context. Notification may only be enabled
  170. * after the IRQ is allocated and must be disabled before the IRQ is
  171. * freed using free_irq().
  172. */
  173. int
  174. irq_set_affinity_notifier(unsigned int irq, struct irq_affinity_notify *notify)
  175. {
  176. struct irq_desc *desc = irq_to_desc(irq);
  177. struct irq_affinity_notify *old_notify;
  178. unsigned long flags;
  179. /* The release function is promised process context */
  180. might_sleep();
  181. if (!desc)
  182. return -EINVAL;
  183. /* Complete initialisation of *notify */
  184. if (notify) {
  185. notify->irq = irq;
  186. kref_init(&notify->kref);
  187. INIT_WORK(&notify->work, irq_affinity_notify);
  188. }
  189. raw_spin_lock_irqsave(&desc->lock, flags);
  190. old_notify = desc->affinity_notify;
  191. desc->affinity_notify = notify;
  192. raw_spin_unlock_irqrestore(&desc->lock, flags);
  193. if (old_notify)
  194. kref_put(&old_notify->kref, old_notify->release);
  195. return 0;
  196. }
  197. EXPORT_SYMBOL_GPL(irq_set_affinity_notifier);
  198. #ifndef CONFIG_AUTO_IRQ_AFFINITY
  199. /*
  200. * Generic version of the affinity autoselector.
  201. */
  202. static int setup_affinity(unsigned int irq, struct irq_desc *desc)
  203. {
  204. if (!irq_can_set_affinity(irq))
  205. return 0;
  206. /*
  207. * Preserve an userspace affinity setup, but make sure that
  208. * one of the targets is online.
  209. */
  210. if (desc->status & (IRQ_AFFINITY_SET | IRQ_NO_BALANCING)) {
  211. if (cpumask_any_and(desc->irq_data.affinity, cpu_online_mask)
  212. < nr_cpu_ids)
  213. goto set_affinity;
  214. else
  215. desc->status &= ~IRQ_AFFINITY_SET;
  216. }
  217. cpumask_and(desc->irq_data.affinity, cpu_online_mask, irq_default_affinity);
  218. set_affinity:
  219. desc->irq_data.chip->irq_set_affinity(&desc->irq_data, desc->irq_data.affinity, false);
  220. return 0;
  221. }
  222. #else
  223. static inline int setup_affinity(unsigned int irq, struct irq_desc *d)
  224. {
  225. return irq_select_affinity(irq);
  226. }
  227. #endif
  228. /*
  229. * Called when affinity is set via /proc/irq
  230. */
  231. int irq_select_affinity_usr(unsigned int irq)
  232. {
  233. struct irq_desc *desc = irq_to_desc(irq);
  234. unsigned long flags;
  235. int ret;
  236. raw_spin_lock_irqsave(&desc->lock, flags);
  237. ret = setup_affinity(irq, desc);
  238. if (!ret)
  239. irq_set_thread_affinity(desc);
  240. raw_spin_unlock_irqrestore(&desc->lock, flags);
  241. return ret;
  242. }
  243. #else
  244. static inline int setup_affinity(unsigned int irq, struct irq_desc *desc)
  245. {
  246. return 0;
  247. }
  248. #endif
  249. void __disable_irq(struct irq_desc *desc, unsigned int irq, bool suspend)
  250. {
  251. if (suspend) {
  252. if (!desc->action || (desc->action->flags & IRQF_NO_SUSPEND))
  253. return;
  254. desc->status |= IRQ_SUSPENDED;
  255. }
  256. if (!desc->depth++) {
  257. desc->status |= IRQ_DISABLED;
  258. desc->irq_data.chip->irq_disable(&desc->irq_data);
  259. }
  260. }
  261. /**
  262. * disable_irq_nosync - disable an irq without waiting
  263. * @irq: Interrupt to disable
  264. *
  265. * Disable the selected interrupt line. Disables and Enables are
  266. * nested.
  267. * Unlike disable_irq(), this function does not ensure existing
  268. * instances of the IRQ handler have completed before returning.
  269. *
  270. * This function may be called from IRQ context.
  271. */
  272. void disable_irq_nosync(unsigned int irq)
  273. {
  274. struct irq_desc *desc = irq_to_desc(irq);
  275. unsigned long flags;
  276. if (!desc)
  277. return;
  278. chip_bus_lock(desc);
  279. raw_spin_lock_irqsave(&desc->lock, flags);
  280. __disable_irq(desc, irq, false);
  281. raw_spin_unlock_irqrestore(&desc->lock, flags);
  282. chip_bus_sync_unlock(desc);
  283. }
  284. EXPORT_SYMBOL(disable_irq_nosync);
  285. /**
  286. * disable_irq - disable an irq and wait for completion
  287. * @irq: Interrupt to disable
  288. *
  289. * Disable the selected interrupt line. Enables and Disables are
  290. * nested.
  291. * This function waits for any pending IRQ handlers for this interrupt
  292. * to complete before returning. If you use this function while
  293. * holding a resource the IRQ handler may need you will deadlock.
  294. *
  295. * This function may be called - with care - from IRQ context.
  296. */
  297. void disable_irq(unsigned int irq)
  298. {
  299. struct irq_desc *desc = irq_to_desc(irq);
  300. if (!desc)
  301. return;
  302. disable_irq_nosync(irq);
  303. if (desc->action)
  304. synchronize_irq(irq);
  305. }
  306. EXPORT_SYMBOL(disable_irq);
  307. void __enable_irq(struct irq_desc *desc, unsigned int irq, bool resume)
  308. {
  309. if (resume) {
  310. if (!(desc->status & IRQ_SUSPENDED)) {
  311. if (!desc->action)
  312. return;
  313. if (!(desc->action->flags & IRQF_FORCE_RESUME))
  314. return;
  315. /* Pretend that it got disabled ! */
  316. desc->depth++;
  317. }
  318. desc->status &= ~IRQ_SUSPENDED;
  319. }
  320. switch (desc->depth) {
  321. case 0:
  322. err_out:
  323. WARN(1, KERN_WARNING "Unbalanced enable for IRQ %d\n", irq);
  324. break;
  325. case 1: {
  326. unsigned int status = desc->status & ~IRQ_DISABLED;
  327. if (desc->status & IRQ_SUSPENDED)
  328. goto err_out;
  329. /* Prevent probing on this irq: */
  330. desc->status = status | IRQ_NOPROBE;
  331. check_irq_resend(desc, irq);
  332. /* fall-through */
  333. }
  334. default:
  335. desc->depth--;
  336. }
  337. }
  338. /**
  339. * enable_irq - enable handling of an irq
  340. * @irq: Interrupt to enable
  341. *
  342. * Undoes the effect of one call to disable_irq(). If this
  343. * matches the last disable, processing of interrupts on this
  344. * IRQ line is re-enabled.
  345. *
  346. * This function may be called from IRQ context only when
  347. * desc->irq_data.chip->bus_lock and desc->chip->bus_sync_unlock are NULL !
  348. */
  349. void enable_irq(unsigned int irq)
  350. {
  351. struct irq_desc *desc = irq_to_desc(irq);
  352. unsigned long flags;
  353. if (!desc)
  354. return;
  355. if (WARN(!desc->irq_data.chip || !desc->irq_data.chip->irq_enable,
  356. KERN_ERR "enable_irq before setup/request_irq: irq %u\n", irq))
  357. return;
  358. chip_bus_lock(desc);
  359. raw_spin_lock_irqsave(&desc->lock, flags);
  360. __enable_irq(desc, irq, false);
  361. raw_spin_unlock_irqrestore(&desc->lock, flags);
  362. chip_bus_sync_unlock(desc);
  363. }
  364. EXPORT_SYMBOL(enable_irq);
  365. static int set_irq_wake_real(unsigned int irq, unsigned int on)
  366. {
  367. struct irq_desc *desc = irq_to_desc(irq);
  368. int ret = -ENXIO;
  369. if (desc->irq_data.chip->irq_set_wake)
  370. ret = desc->irq_data.chip->irq_set_wake(&desc->irq_data, on);
  371. return ret;
  372. }
  373. /**
  374. * set_irq_wake - control irq power management wakeup
  375. * @irq: interrupt to control
  376. * @on: enable/disable power management wakeup
  377. *
  378. * Enable/disable power management wakeup mode, which is
  379. * disabled by default. Enables and disables must match,
  380. * just as they match for non-wakeup mode support.
  381. *
  382. * Wakeup mode lets this IRQ wake the system from sleep
  383. * states like "suspend to RAM".
  384. */
  385. int set_irq_wake(unsigned int irq, unsigned int on)
  386. {
  387. struct irq_desc *desc = irq_to_desc(irq);
  388. unsigned long flags;
  389. int ret = 0;
  390. /* wakeup-capable irqs can be shared between drivers that
  391. * don't need to have the same sleep mode behaviors.
  392. */
  393. chip_bus_lock(desc);
  394. raw_spin_lock_irqsave(&desc->lock, flags);
  395. if (on) {
  396. if (desc->wake_depth++ == 0) {
  397. ret = set_irq_wake_real(irq, on);
  398. if (ret)
  399. desc->wake_depth = 0;
  400. else
  401. desc->status |= IRQ_WAKEUP;
  402. }
  403. } else {
  404. if (desc->wake_depth == 0) {
  405. WARN(1, "Unbalanced IRQ %d wake disable\n", irq);
  406. } else if (--desc->wake_depth == 0) {
  407. ret = set_irq_wake_real(irq, on);
  408. if (ret)
  409. desc->wake_depth = 1;
  410. else
  411. desc->status &= ~IRQ_WAKEUP;
  412. }
  413. }
  414. raw_spin_unlock_irqrestore(&desc->lock, flags);
  415. chip_bus_sync_unlock(desc);
  416. return ret;
  417. }
  418. EXPORT_SYMBOL(set_irq_wake);
  419. /*
  420. * Internal function that tells the architecture code whether a
  421. * particular irq has been exclusively allocated or is available
  422. * for driver use.
  423. */
  424. int can_request_irq(unsigned int irq, unsigned long irqflags)
  425. {
  426. struct irq_desc *desc = irq_to_desc(irq);
  427. struct irqaction *action;
  428. unsigned long flags;
  429. if (!desc)
  430. return 0;
  431. if (desc->status & IRQ_NOREQUEST)
  432. return 0;
  433. raw_spin_lock_irqsave(&desc->lock, flags);
  434. action = desc->action;
  435. if (action)
  436. if (irqflags & action->flags & IRQF_SHARED)
  437. action = NULL;
  438. raw_spin_unlock_irqrestore(&desc->lock, flags);
  439. return !action;
  440. }
  441. void compat_irq_chip_set_default_handler(struct irq_desc *desc)
  442. {
  443. /*
  444. * If the architecture still has not overriden
  445. * the flow handler then zap the default. This
  446. * should catch incorrect flow-type setting.
  447. */
  448. if (desc->handle_irq == &handle_bad_irq)
  449. desc->handle_irq = NULL;
  450. }
  451. int __irq_set_trigger(struct irq_desc *desc, unsigned int irq,
  452. unsigned long flags)
  453. {
  454. int ret;
  455. struct irq_chip *chip = desc->irq_data.chip;
  456. if (!chip || !chip->irq_set_type) {
  457. /*
  458. * IRQF_TRIGGER_* but the PIC does not support multiple
  459. * flow-types?
  460. */
  461. pr_debug("No set_type function for IRQ %d (%s)\n", irq,
  462. chip ? (chip->name ? : "unknown") : "unknown");
  463. return 0;
  464. }
  465. /* caller masked out all except trigger mode flags */
  466. ret = chip->irq_set_type(&desc->irq_data, flags);
  467. if (ret)
  468. pr_err("setting trigger mode %lu for irq %u failed (%pF)\n",
  469. flags, irq, chip->irq_set_type);
  470. else {
  471. if (flags & (IRQ_TYPE_LEVEL_LOW | IRQ_TYPE_LEVEL_HIGH))
  472. flags |= IRQ_LEVEL;
  473. /* note that IRQF_TRIGGER_MASK == IRQ_TYPE_SENSE_MASK */
  474. desc->status &= ~(IRQ_LEVEL | IRQ_TYPE_SENSE_MASK);
  475. desc->status |= flags;
  476. if (chip != desc->irq_data.chip)
  477. irq_chip_set_defaults(desc->irq_data.chip);
  478. }
  479. return ret;
  480. }
  481. /*
  482. * Default primary interrupt handler for threaded interrupts. Is
  483. * assigned as primary handler when request_threaded_irq is called
  484. * with handler == NULL. Useful for oneshot interrupts.
  485. */
  486. static irqreturn_t irq_default_primary_handler(int irq, void *dev_id)
  487. {
  488. return IRQ_WAKE_THREAD;
  489. }
  490. /*
  491. * Primary handler for nested threaded interrupts. Should never be
  492. * called.
  493. */
  494. static irqreturn_t irq_nested_primary_handler(int irq, void *dev_id)
  495. {
  496. WARN(1, "Primary handler called for nested irq %d\n", irq);
  497. return IRQ_NONE;
  498. }
  499. static int irq_wait_for_interrupt(struct irqaction *action)
  500. {
  501. while (!kthread_should_stop()) {
  502. set_current_state(TASK_INTERRUPTIBLE);
  503. if (test_and_clear_bit(IRQTF_RUNTHREAD,
  504. &action->thread_flags)) {
  505. __set_current_state(TASK_RUNNING);
  506. return 0;
  507. }
  508. schedule();
  509. }
  510. return -1;
  511. }
  512. /*
  513. * Oneshot interrupts keep the irq line masked until the threaded
  514. * handler finished. unmask if the interrupt has not been disabled and
  515. * is marked MASKED.
  516. */
  517. static void irq_finalize_oneshot(unsigned int irq, struct irq_desc *desc)
  518. {
  519. again:
  520. chip_bus_lock(desc);
  521. raw_spin_lock_irq(&desc->lock);
  522. /*
  523. * Implausible though it may be we need to protect us against
  524. * the following scenario:
  525. *
  526. * The thread is faster done than the hard interrupt handler
  527. * on the other CPU. If we unmask the irq line then the
  528. * interrupt can come in again and masks the line, leaves due
  529. * to IRQ_INPROGRESS and the irq line is masked forever.
  530. */
  531. if (unlikely(desc->status & IRQ_INPROGRESS)) {
  532. raw_spin_unlock_irq(&desc->lock);
  533. chip_bus_sync_unlock(desc);
  534. cpu_relax();
  535. goto again;
  536. }
  537. if (!(desc->status & IRQ_DISABLED) && (desc->status & IRQ_MASKED)) {
  538. desc->status &= ~IRQ_MASKED;
  539. desc->irq_data.chip->irq_unmask(&desc->irq_data);
  540. }
  541. raw_spin_unlock_irq(&desc->lock);
  542. chip_bus_sync_unlock(desc);
  543. }
  544. #ifdef CONFIG_SMP
  545. /*
  546. * Check whether we need to change the affinity of the interrupt thread.
  547. */
  548. static void
  549. irq_thread_check_affinity(struct irq_desc *desc, struct irqaction *action)
  550. {
  551. cpumask_var_t mask;
  552. if (!test_and_clear_bit(IRQTF_AFFINITY, &action->thread_flags))
  553. return;
  554. /*
  555. * In case we are out of memory we set IRQTF_AFFINITY again and
  556. * try again next time
  557. */
  558. if (!alloc_cpumask_var(&mask, GFP_KERNEL)) {
  559. set_bit(IRQTF_AFFINITY, &action->thread_flags);
  560. return;
  561. }
  562. raw_spin_lock_irq(&desc->lock);
  563. cpumask_copy(mask, desc->irq_data.affinity);
  564. raw_spin_unlock_irq(&desc->lock);
  565. set_cpus_allowed_ptr(current, mask);
  566. free_cpumask_var(mask);
  567. }
  568. #else
  569. static inline void
  570. irq_thread_check_affinity(struct irq_desc *desc, struct irqaction *action) { }
  571. #endif
  572. /*
  573. * Interrupt handler thread
  574. */
  575. static int irq_thread(void *data)
  576. {
  577. static const struct sched_param param = {
  578. .sched_priority = MAX_USER_RT_PRIO/2,
  579. };
  580. struct irqaction *action = data;
  581. struct irq_desc *desc = irq_to_desc(action->irq);
  582. int wake, oneshot = desc->status & IRQ_ONESHOT;
  583. sched_setscheduler(current, SCHED_FIFO, &param);
  584. current->irqaction = action;
  585. while (!irq_wait_for_interrupt(action)) {
  586. irq_thread_check_affinity(desc, action);
  587. atomic_inc(&desc->threads_active);
  588. raw_spin_lock_irq(&desc->lock);
  589. if (unlikely(desc->status & IRQ_DISABLED)) {
  590. /*
  591. * CHECKME: We might need a dedicated
  592. * IRQ_THREAD_PENDING flag here, which
  593. * retriggers the thread in check_irq_resend()
  594. * but AFAICT IRQ_PENDING should be fine as it
  595. * retriggers the interrupt itself --- tglx
  596. */
  597. desc->status |= IRQ_PENDING;
  598. raw_spin_unlock_irq(&desc->lock);
  599. } else {
  600. raw_spin_unlock_irq(&desc->lock);
  601. action->thread_fn(action->irq, action->dev_id);
  602. if (oneshot)
  603. irq_finalize_oneshot(action->irq, desc);
  604. }
  605. wake = atomic_dec_and_test(&desc->threads_active);
  606. if (wake && waitqueue_active(&desc->wait_for_threads))
  607. wake_up(&desc->wait_for_threads);
  608. }
  609. /*
  610. * Clear irqaction. Otherwise exit_irq_thread() would make
  611. * fuzz about an active irq thread going into nirvana.
  612. */
  613. current->irqaction = NULL;
  614. return 0;
  615. }
  616. /*
  617. * Called from do_exit()
  618. */
  619. void exit_irq_thread(void)
  620. {
  621. struct task_struct *tsk = current;
  622. if (!tsk->irqaction)
  623. return;
  624. printk(KERN_ERR
  625. "exiting task \"%s\" (%d) is an active IRQ thread (irq %d)\n",
  626. tsk->comm ? tsk->comm : "", tsk->pid, tsk->irqaction->irq);
  627. /*
  628. * Set the THREAD DIED flag to prevent further wakeups of the
  629. * soon to be gone threaded handler.
  630. */
  631. set_bit(IRQTF_DIED, &tsk->irqaction->flags);
  632. }
  633. /*
  634. * Internal function to register an irqaction - typically used to
  635. * allocate special interrupts that are part of the architecture.
  636. */
  637. static int
  638. __setup_irq(unsigned int irq, struct irq_desc *desc, struct irqaction *new)
  639. {
  640. struct irqaction *old, **old_ptr;
  641. const char *old_name = NULL;
  642. unsigned long flags;
  643. int nested, shared = 0;
  644. int ret;
  645. if (!desc)
  646. return -EINVAL;
  647. if (desc->irq_data.chip == &no_irq_chip)
  648. return -ENOSYS;
  649. /*
  650. * Some drivers like serial.c use request_irq() heavily,
  651. * so we have to be careful not to interfere with a
  652. * running system.
  653. */
  654. if (new->flags & IRQF_SAMPLE_RANDOM) {
  655. /*
  656. * This function might sleep, we want to call it first,
  657. * outside of the atomic block.
  658. * Yes, this might clear the entropy pool if the wrong
  659. * driver is attempted to be loaded, without actually
  660. * installing a new handler, but is this really a problem,
  661. * only the sysadmin is able to do this.
  662. */
  663. rand_initialize_irq(irq);
  664. }
  665. /* Oneshot interrupts are not allowed with shared */
  666. if ((new->flags & IRQF_ONESHOT) && (new->flags & IRQF_SHARED))
  667. return -EINVAL;
  668. /*
  669. * Check whether the interrupt nests into another interrupt
  670. * thread.
  671. */
  672. nested = desc->status & IRQ_NESTED_THREAD;
  673. if (nested) {
  674. if (!new->thread_fn)
  675. return -EINVAL;
  676. /*
  677. * Replace the primary handler which was provided from
  678. * the driver for non nested interrupt handling by the
  679. * dummy function which warns when called.
  680. */
  681. new->handler = irq_nested_primary_handler;
  682. }
  683. /*
  684. * Create a handler thread when a thread function is supplied
  685. * and the interrupt does not nest into another interrupt
  686. * thread.
  687. */
  688. if (new->thread_fn && !nested) {
  689. struct task_struct *t;
  690. t = kthread_create(irq_thread, new, "irq/%d-%s", irq,
  691. new->name);
  692. if (IS_ERR(t))
  693. return PTR_ERR(t);
  694. /*
  695. * We keep the reference to the task struct even if
  696. * the thread dies to avoid that the interrupt code
  697. * references an already freed task_struct.
  698. */
  699. get_task_struct(t);
  700. new->thread = t;
  701. }
  702. /*
  703. * The following block of code has to be executed atomically
  704. */
  705. raw_spin_lock_irqsave(&desc->lock, flags);
  706. old_ptr = &desc->action;
  707. old = *old_ptr;
  708. if (old) {
  709. /*
  710. * Can't share interrupts unless both agree to and are
  711. * the same type (level, edge, polarity). So both flag
  712. * fields must have IRQF_SHARED set and the bits which
  713. * set the trigger type must match.
  714. */
  715. if (!((old->flags & new->flags) & IRQF_SHARED) ||
  716. ((old->flags ^ new->flags) & IRQF_TRIGGER_MASK)) {
  717. old_name = old->name;
  718. goto mismatch;
  719. }
  720. #if defined(CONFIG_IRQ_PER_CPU)
  721. /* All handlers must agree on per-cpuness */
  722. if ((old->flags & IRQF_PERCPU) !=
  723. (new->flags & IRQF_PERCPU))
  724. goto mismatch;
  725. #endif
  726. /* add new interrupt at end of irq queue */
  727. do {
  728. old_ptr = &old->next;
  729. old = *old_ptr;
  730. } while (old);
  731. shared = 1;
  732. }
  733. if (!shared) {
  734. irq_chip_set_defaults(desc->irq_data.chip);
  735. init_waitqueue_head(&desc->wait_for_threads);
  736. /* Setup the type (level, edge polarity) if configured: */
  737. if (new->flags & IRQF_TRIGGER_MASK) {
  738. ret = __irq_set_trigger(desc, irq,
  739. new->flags & IRQF_TRIGGER_MASK);
  740. if (ret)
  741. goto out_thread;
  742. } else
  743. compat_irq_chip_set_default_handler(desc);
  744. #if defined(CONFIG_IRQ_PER_CPU)
  745. if (new->flags & IRQF_PERCPU)
  746. desc->status |= IRQ_PER_CPU;
  747. #endif
  748. desc->status &= ~(IRQ_AUTODETECT | IRQ_WAITING | IRQ_ONESHOT |
  749. IRQ_INPROGRESS | IRQ_SPURIOUS_DISABLED);
  750. if (new->flags & IRQF_ONESHOT)
  751. desc->status |= IRQ_ONESHOT;
  752. if (!(desc->status & IRQ_NOAUTOEN)) {
  753. desc->depth = 0;
  754. desc->status &= ~IRQ_DISABLED;
  755. desc->irq_data.chip->irq_startup(&desc->irq_data);
  756. } else
  757. /* Undo nested disables: */
  758. desc->depth = 1;
  759. /* Exclude IRQ from balancing if requested */
  760. if (new->flags & IRQF_NOBALANCING)
  761. desc->status |= IRQ_NO_BALANCING;
  762. /* Set default affinity mask once everything is setup */
  763. setup_affinity(irq, desc);
  764. } else if ((new->flags & IRQF_TRIGGER_MASK)
  765. && (new->flags & IRQF_TRIGGER_MASK)
  766. != (desc->status & IRQ_TYPE_SENSE_MASK)) {
  767. /* hope the handler works with the actual trigger mode... */
  768. pr_warning("IRQ %d uses trigger mode %d; requested %d\n",
  769. irq, (int)(desc->status & IRQ_TYPE_SENSE_MASK),
  770. (int)(new->flags & IRQF_TRIGGER_MASK));
  771. }
  772. new->irq = irq;
  773. *old_ptr = new;
  774. /* Reset broken irq detection when installing new handler */
  775. desc->irq_count = 0;
  776. desc->irqs_unhandled = 0;
  777. /*
  778. * Check whether we disabled the irq via the spurious handler
  779. * before. Reenable it and give it another chance.
  780. */
  781. if (shared && (desc->status & IRQ_SPURIOUS_DISABLED)) {
  782. desc->status &= ~IRQ_SPURIOUS_DISABLED;
  783. __enable_irq(desc, irq, false);
  784. }
  785. raw_spin_unlock_irqrestore(&desc->lock, flags);
  786. /*
  787. * Strictly no need to wake it up, but hung_task complains
  788. * when no hard interrupt wakes the thread up.
  789. */
  790. if (new->thread)
  791. wake_up_process(new->thread);
  792. register_irq_proc(irq, desc);
  793. new->dir = NULL;
  794. register_handler_proc(irq, new);
  795. return 0;
  796. mismatch:
  797. #ifdef CONFIG_DEBUG_SHIRQ
  798. if (!(new->flags & IRQF_PROBE_SHARED)) {
  799. printk(KERN_ERR "IRQ handler type mismatch for IRQ %d\n", irq);
  800. if (old_name)
  801. printk(KERN_ERR "current handler: %s\n", old_name);
  802. dump_stack();
  803. }
  804. #endif
  805. ret = -EBUSY;
  806. out_thread:
  807. raw_spin_unlock_irqrestore(&desc->lock, flags);
  808. if (new->thread) {
  809. struct task_struct *t = new->thread;
  810. new->thread = NULL;
  811. if (likely(!test_bit(IRQTF_DIED, &new->thread_flags)))
  812. kthread_stop(t);
  813. put_task_struct(t);
  814. }
  815. return ret;
  816. }
  817. /**
  818. * setup_irq - setup an interrupt
  819. * @irq: Interrupt line to setup
  820. * @act: irqaction for the interrupt
  821. *
  822. * Used to statically setup interrupts in the early boot process.
  823. */
  824. int setup_irq(unsigned int irq, struct irqaction *act)
  825. {
  826. int retval;
  827. struct irq_desc *desc = irq_to_desc(irq);
  828. chip_bus_lock(desc);
  829. retval = __setup_irq(irq, desc, act);
  830. chip_bus_sync_unlock(desc);
  831. return retval;
  832. }
  833. EXPORT_SYMBOL_GPL(setup_irq);
  834. /*
  835. * Internal function to unregister an irqaction - used to free
  836. * regular and special interrupts that are part of the architecture.
  837. */
  838. static struct irqaction *__free_irq(unsigned int irq, void *dev_id)
  839. {
  840. struct irq_desc *desc = irq_to_desc(irq);
  841. struct irqaction *action, **action_ptr;
  842. unsigned long flags;
  843. WARN(in_interrupt(), "Trying to free IRQ %d from IRQ context!\n", irq);
  844. if (!desc)
  845. return NULL;
  846. raw_spin_lock_irqsave(&desc->lock, flags);
  847. /*
  848. * There can be multiple actions per IRQ descriptor, find the right
  849. * one based on the dev_id:
  850. */
  851. action_ptr = &desc->action;
  852. for (;;) {
  853. action = *action_ptr;
  854. if (!action) {
  855. WARN(1, "Trying to free already-free IRQ %d\n", irq);
  856. raw_spin_unlock_irqrestore(&desc->lock, flags);
  857. return NULL;
  858. }
  859. if (action->dev_id == dev_id)
  860. break;
  861. action_ptr = &action->next;
  862. }
  863. /* Found it - now remove it from the list of entries: */
  864. *action_ptr = action->next;
  865. /* Currently used only by UML, might disappear one day: */
  866. #ifdef CONFIG_IRQ_RELEASE_METHOD
  867. if (desc->irq_data.chip->release)
  868. desc->irq_data.chip->release(irq, dev_id);
  869. #endif
  870. /* If this was the last handler, shut down the IRQ line: */
  871. if (!desc->action) {
  872. desc->status |= IRQ_DISABLED;
  873. if (desc->irq_data.chip->irq_shutdown)
  874. desc->irq_data.chip->irq_shutdown(&desc->irq_data);
  875. else
  876. desc->irq_data.chip->irq_disable(&desc->irq_data);
  877. }
  878. #ifdef CONFIG_SMP
  879. /* make sure affinity_hint is cleaned up */
  880. if (WARN_ON_ONCE(desc->affinity_hint))
  881. desc->affinity_hint = NULL;
  882. #endif
  883. raw_spin_unlock_irqrestore(&desc->lock, flags);
  884. unregister_handler_proc(irq, action);
  885. /* Make sure it's not being used on another CPU: */
  886. synchronize_irq(irq);
  887. #ifdef CONFIG_DEBUG_SHIRQ
  888. /*
  889. * It's a shared IRQ -- the driver ought to be prepared for an IRQ
  890. * event to happen even now it's being freed, so let's make sure that
  891. * is so by doing an extra call to the handler ....
  892. *
  893. * ( We do this after actually deregistering it, to make sure that a
  894. * 'real' IRQ doesn't run in * parallel with our fake. )
  895. */
  896. if (action->flags & IRQF_SHARED) {
  897. local_irq_save(flags);
  898. action->handler(irq, dev_id);
  899. local_irq_restore(flags);
  900. }
  901. #endif
  902. if (action->thread) {
  903. if (!test_bit(IRQTF_DIED, &action->thread_flags))
  904. kthread_stop(action->thread);
  905. put_task_struct(action->thread);
  906. }
  907. return action;
  908. }
  909. /**
  910. * remove_irq - free an interrupt
  911. * @irq: Interrupt line to free
  912. * @act: irqaction for the interrupt
  913. *
  914. * Used to remove interrupts statically setup by the early boot process.
  915. */
  916. void remove_irq(unsigned int irq, struct irqaction *act)
  917. {
  918. __free_irq(irq, act->dev_id);
  919. }
  920. EXPORT_SYMBOL_GPL(remove_irq);
  921. /**
  922. * free_irq - free an interrupt allocated with request_irq
  923. * @irq: Interrupt line to free
  924. * @dev_id: Device identity to free
  925. *
  926. * Remove an interrupt handler. The handler is removed and if the
  927. * interrupt line is no longer in use by any driver it is disabled.
  928. * On a shared IRQ the caller must ensure the interrupt is disabled
  929. * on the card it drives before calling this function. The function
  930. * does not return until any executing interrupts for this IRQ
  931. * have completed.
  932. *
  933. * This function must not be called from interrupt context.
  934. */
  935. void free_irq(unsigned int irq, void *dev_id)
  936. {
  937. struct irq_desc *desc = irq_to_desc(irq);
  938. if (!desc)
  939. return;
  940. #ifdef CONFIG_SMP
  941. if (WARN_ON(desc->affinity_notify))
  942. desc->affinity_notify = NULL;
  943. #endif
  944. chip_bus_lock(desc);
  945. kfree(__free_irq(irq, dev_id));
  946. chip_bus_sync_unlock(desc);
  947. }
  948. EXPORT_SYMBOL(free_irq);
  949. /**
  950. * request_threaded_irq - allocate an interrupt line
  951. * @irq: Interrupt line to allocate
  952. * @handler: Function to be called when the IRQ occurs.
  953. * Primary handler for threaded interrupts
  954. * If NULL and thread_fn != NULL the default
  955. * primary handler is installed
  956. * @thread_fn: Function called from the irq handler thread
  957. * If NULL, no irq thread is created
  958. * @irqflags: Interrupt type flags
  959. * @devname: An ascii name for the claiming device
  960. * @dev_id: A cookie passed back to the handler function
  961. *
  962. * This call allocates interrupt resources and enables the
  963. * interrupt line and IRQ handling. From the point this
  964. * call is made your handler function may be invoked. Since
  965. * your handler function must clear any interrupt the board
  966. * raises, you must take care both to initialise your hardware
  967. * and to set up the interrupt handler in the right order.
  968. *
  969. * If you want to set up a threaded irq handler for your device
  970. * then you need to supply @handler and @thread_fn. @handler ist
  971. * still called in hard interrupt context and has to check
  972. * whether the interrupt originates from the device. If yes it
  973. * needs to disable the interrupt on the device and return
  974. * IRQ_WAKE_THREAD which will wake up the handler thread and run
  975. * @thread_fn. This split handler design is necessary to support
  976. * shared interrupts.
  977. *
  978. * Dev_id must be globally unique. Normally the address of the
  979. * device data structure is used as the cookie. Since the handler
  980. * receives this value it makes sense to use it.
  981. *
  982. * If your interrupt is shared you must pass a non NULL dev_id
  983. * as this is required when freeing the interrupt.
  984. *
  985. * Flags:
  986. *
  987. * IRQF_SHARED Interrupt is shared
  988. * IRQF_SAMPLE_RANDOM The interrupt can be used for entropy
  989. * IRQF_TRIGGER_* Specify active edge(s) or level
  990. *
  991. */
  992. int request_threaded_irq(unsigned int irq, irq_handler_t handler,
  993. irq_handler_t thread_fn, unsigned long irqflags,
  994. const char *devname, void *dev_id)
  995. {
  996. struct irqaction *action;
  997. struct irq_desc *desc;
  998. int retval;
  999. /*
  1000. * Sanity-check: shared interrupts must pass in a real dev-ID,
  1001. * otherwise we'll have trouble later trying to figure out
  1002. * which interrupt is which (messes up the interrupt freeing
  1003. * logic etc).
  1004. */
  1005. if ((irqflags & IRQF_SHARED) && !dev_id)
  1006. return -EINVAL;
  1007. desc = irq_to_desc(irq);
  1008. if (!desc)
  1009. return -EINVAL;
  1010. if (desc->status & IRQ_NOREQUEST)
  1011. return -EINVAL;
  1012. if (!handler) {
  1013. if (!thread_fn)
  1014. return -EINVAL;
  1015. handler = irq_default_primary_handler;
  1016. }
  1017. action = kzalloc(sizeof(struct irqaction), GFP_KERNEL);
  1018. if (!action)
  1019. return -ENOMEM;
  1020. action->handler = handler;
  1021. action->thread_fn = thread_fn;
  1022. action->flags = irqflags;
  1023. action->name = devname;
  1024. action->dev_id = dev_id;
  1025. chip_bus_lock(desc);
  1026. retval = __setup_irq(irq, desc, action);
  1027. chip_bus_sync_unlock(desc);
  1028. if (retval)
  1029. kfree(action);
  1030. #ifdef CONFIG_DEBUG_SHIRQ_FIXME
  1031. if (!retval && (irqflags & IRQF_SHARED)) {
  1032. /*
  1033. * It's a shared IRQ -- the driver ought to be prepared for it
  1034. * to happen immediately, so let's make sure....
  1035. * We disable the irq to make sure that a 'real' IRQ doesn't
  1036. * run in parallel with our fake.
  1037. */
  1038. unsigned long flags;
  1039. disable_irq(irq);
  1040. local_irq_save(flags);
  1041. handler(irq, dev_id);
  1042. local_irq_restore(flags);
  1043. enable_irq(irq);
  1044. }
  1045. #endif
  1046. return retval;
  1047. }
  1048. EXPORT_SYMBOL(request_threaded_irq);
  1049. /**
  1050. * request_any_context_irq - allocate an interrupt line
  1051. * @irq: Interrupt line to allocate
  1052. * @handler: Function to be called when the IRQ occurs.
  1053. * Threaded handler for threaded interrupts.
  1054. * @flags: Interrupt type flags
  1055. * @name: An ascii name for the claiming device
  1056. * @dev_id: A cookie passed back to the handler function
  1057. *
  1058. * This call allocates interrupt resources and enables the
  1059. * interrupt line and IRQ handling. It selects either a
  1060. * hardirq or threaded handling method depending on the
  1061. * context.
  1062. *
  1063. * On failure, it returns a negative value. On success,
  1064. * it returns either IRQC_IS_HARDIRQ or IRQC_IS_NESTED.
  1065. */
  1066. int request_any_context_irq(unsigned int irq, irq_handler_t handler,
  1067. unsigned long flags, const char *name, void *dev_id)
  1068. {
  1069. struct irq_desc *desc = irq_to_desc(irq);
  1070. int ret;
  1071. if (!desc)
  1072. return -EINVAL;
  1073. if (desc->status & IRQ_NESTED_THREAD) {
  1074. ret = request_threaded_irq(irq, NULL, handler,
  1075. flags, name, dev_id);
  1076. return !ret ? IRQC_IS_NESTED : ret;
  1077. }
  1078. ret = request_irq(irq, handler, flags, name, dev_id);
  1079. return !ret ? IRQC_IS_HARDIRQ : ret;
  1080. }
  1081. EXPORT_SYMBOL_GPL(request_any_context_irq);