manage.c 43 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694
  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. #define pr_fmt(fmt) "genirq: " fmt
  10. #include <linux/irq.h>
  11. #include <linux/kthread.h>
  12. #include <linux/module.h>
  13. #include <linux/random.h>
  14. #include <linux/interrupt.h>
  15. #include <linux/slab.h>
  16. #include <linux/sched.h>
  17. #include <linux/sched/rt.h>
  18. #include <linux/task_work.h>
  19. #include "internals.h"
  20. #ifdef CONFIG_IRQ_FORCED_THREADING
  21. __read_mostly bool force_irqthreads;
  22. static int __init setup_forced_irqthreads(char *arg)
  23. {
  24. force_irqthreads = true;
  25. return 0;
  26. }
  27. early_param("threadirqs", setup_forced_irqthreads);
  28. #endif
  29. /**
  30. * synchronize_irq - wait for pending IRQ handlers (on other CPUs)
  31. * @irq: interrupt number to wait for
  32. *
  33. * This function waits for any pending IRQ handlers for this interrupt
  34. * to complete before returning. If you use this function while
  35. * holding a resource the IRQ handler may need you will deadlock.
  36. *
  37. * This function may be called - with care - from IRQ context.
  38. */
  39. void synchronize_irq(unsigned int irq)
  40. {
  41. struct irq_desc *desc = irq_to_desc(irq);
  42. bool inprogress;
  43. if (!desc)
  44. return;
  45. do {
  46. unsigned long flags;
  47. /*
  48. * Wait until we're out of the critical section. This might
  49. * give the wrong answer due to the lack of memory barriers.
  50. */
  51. while (irqd_irq_inprogress(&desc->irq_data))
  52. cpu_relax();
  53. /* Ok, that indicated we're done: double-check carefully. */
  54. raw_spin_lock_irqsave(&desc->lock, flags);
  55. inprogress = irqd_irq_inprogress(&desc->irq_data);
  56. raw_spin_unlock_irqrestore(&desc->lock, flags);
  57. /* Oops, that failed? */
  58. } while (inprogress);
  59. /*
  60. * We made sure that no hardirq handler is running. Now verify
  61. * that no threaded handlers are active.
  62. */
  63. wait_event(desc->wait_for_threads, !atomic_read(&desc->threads_active));
  64. }
  65. EXPORT_SYMBOL(synchronize_irq);
  66. #ifdef CONFIG_SMP
  67. cpumask_var_t irq_default_affinity;
  68. /**
  69. * irq_can_set_affinity - Check if the affinity of a given irq can be set
  70. * @irq: Interrupt to check
  71. *
  72. */
  73. int irq_can_set_affinity(unsigned int irq)
  74. {
  75. struct irq_desc *desc = irq_to_desc(irq);
  76. if (!desc || !irqd_can_balance(&desc->irq_data) ||
  77. !desc->irq_data.chip || !desc->irq_data.chip->irq_set_affinity)
  78. return 0;
  79. return 1;
  80. }
  81. /**
  82. * irq_set_thread_affinity - Notify irq threads to adjust affinity
  83. * @desc: irq descriptor which has affitnity changed
  84. *
  85. * We just set IRQTF_AFFINITY and delegate the affinity setting
  86. * to the interrupt thread itself. We can not call
  87. * set_cpus_allowed_ptr() here as we hold desc->lock and this
  88. * code can be called from hard interrupt context.
  89. */
  90. void irq_set_thread_affinity(struct irq_desc *desc)
  91. {
  92. struct irqaction *action = desc->action;
  93. while (action) {
  94. if (action->thread)
  95. set_bit(IRQTF_AFFINITY, &action->thread_flags);
  96. action = action->next;
  97. }
  98. }
  99. #ifdef CONFIG_GENERIC_PENDING_IRQ
  100. static inline bool irq_can_move_pcntxt(struct irq_data *data)
  101. {
  102. return irqd_can_move_in_process_context(data);
  103. }
  104. static inline bool irq_move_pending(struct irq_data *data)
  105. {
  106. return irqd_is_setaffinity_pending(data);
  107. }
  108. static inline void
  109. irq_copy_pending(struct irq_desc *desc, const struct cpumask *mask)
  110. {
  111. cpumask_copy(desc->pending_mask, mask);
  112. }
  113. static inline void
  114. irq_get_pending(struct cpumask *mask, struct irq_desc *desc)
  115. {
  116. cpumask_copy(mask, desc->pending_mask);
  117. }
  118. #else
  119. static inline bool irq_can_move_pcntxt(struct irq_data *data) { return true; }
  120. static inline bool irq_move_pending(struct irq_data *data) { return false; }
  121. static inline void
  122. irq_copy_pending(struct irq_desc *desc, const struct cpumask *mask) { }
  123. static inline void
  124. irq_get_pending(struct cpumask *mask, struct irq_desc *desc) { }
  125. #endif
  126. int irq_do_set_affinity(struct irq_data *data, const struct cpumask *mask,
  127. bool force)
  128. {
  129. struct irq_desc *desc = irq_data_to_desc(data);
  130. struct irq_chip *chip = irq_data_get_irq_chip(data);
  131. int ret;
  132. ret = chip->irq_set_affinity(data, mask, false);
  133. switch (ret) {
  134. case IRQ_SET_MASK_OK:
  135. cpumask_copy(data->affinity, mask);
  136. case IRQ_SET_MASK_OK_NOCOPY:
  137. irq_set_thread_affinity(desc);
  138. ret = 0;
  139. }
  140. return ret;
  141. }
  142. int __irq_set_affinity_locked(struct irq_data *data, const struct cpumask *mask)
  143. {
  144. struct irq_chip *chip = irq_data_get_irq_chip(data);
  145. struct irq_desc *desc = irq_data_to_desc(data);
  146. int ret = 0;
  147. if (!chip || !chip->irq_set_affinity)
  148. return -EINVAL;
  149. if (irq_can_move_pcntxt(data)) {
  150. ret = irq_do_set_affinity(data, mask, false);
  151. } else {
  152. irqd_set_move_pending(data);
  153. irq_copy_pending(desc, mask);
  154. }
  155. if (desc->affinity_notify) {
  156. kref_get(&desc->affinity_notify->kref);
  157. schedule_work(&desc->affinity_notify->work);
  158. }
  159. irqd_set(data, IRQD_AFFINITY_SET);
  160. return ret;
  161. }
  162. /**
  163. * irq_set_affinity - Set the irq affinity of a given irq
  164. * @irq: Interrupt to set affinity
  165. * @mask: cpumask
  166. *
  167. */
  168. int irq_set_affinity(unsigned int irq, const struct cpumask *mask)
  169. {
  170. struct irq_desc *desc = irq_to_desc(irq);
  171. unsigned long flags;
  172. int ret;
  173. if (!desc)
  174. return -EINVAL;
  175. raw_spin_lock_irqsave(&desc->lock, flags);
  176. ret = __irq_set_affinity_locked(irq_desc_get_irq_data(desc), mask);
  177. raw_spin_unlock_irqrestore(&desc->lock, flags);
  178. return ret;
  179. }
  180. int irq_set_affinity_hint(unsigned int irq, const struct cpumask *m)
  181. {
  182. unsigned long flags;
  183. struct irq_desc *desc = irq_get_desc_lock(irq, &flags, IRQ_GET_DESC_CHECK_GLOBAL);
  184. if (!desc)
  185. return -EINVAL;
  186. desc->affinity_hint = m;
  187. irq_put_desc_unlock(desc, flags);
  188. return 0;
  189. }
  190. EXPORT_SYMBOL_GPL(irq_set_affinity_hint);
  191. static void irq_affinity_notify(struct work_struct *work)
  192. {
  193. struct irq_affinity_notify *notify =
  194. container_of(work, struct irq_affinity_notify, work);
  195. struct irq_desc *desc = irq_to_desc(notify->irq);
  196. cpumask_var_t cpumask;
  197. unsigned long flags;
  198. if (!desc || !alloc_cpumask_var(&cpumask, GFP_KERNEL))
  199. goto out;
  200. raw_spin_lock_irqsave(&desc->lock, flags);
  201. if (irq_move_pending(&desc->irq_data))
  202. irq_get_pending(cpumask, desc);
  203. else
  204. cpumask_copy(cpumask, desc->irq_data.affinity);
  205. raw_spin_unlock_irqrestore(&desc->lock, flags);
  206. notify->notify(notify, cpumask);
  207. free_cpumask_var(cpumask);
  208. out:
  209. kref_put(&notify->kref, notify->release);
  210. }
  211. /**
  212. * irq_set_affinity_notifier - control notification of IRQ affinity changes
  213. * @irq: Interrupt for which to enable/disable notification
  214. * @notify: Context for notification, or %NULL to disable
  215. * notification. Function pointers must be initialised;
  216. * the other fields will be initialised by this function.
  217. *
  218. * Must be called in process context. Notification may only be enabled
  219. * after the IRQ is allocated and must be disabled before the IRQ is
  220. * freed using free_irq().
  221. */
  222. int
  223. irq_set_affinity_notifier(unsigned int irq, struct irq_affinity_notify *notify)
  224. {
  225. struct irq_desc *desc = irq_to_desc(irq);
  226. struct irq_affinity_notify *old_notify;
  227. unsigned long flags;
  228. /* The release function is promised process context */
  229. might_sleep();
  230. if (!desc)
  231. return -EINVAL;
  232. /* Complete initialisation of *notify */
  233. if (notify) {
  234. notify->irq = irq;
  235. kref_init(&notify->kref);
  236. INIT_WORK(&notify->work, irq_affinity_notify);
  237. }
  238. raw_spin_lock_irqsave(&desc->lock, flags);
  239. old_notify = desc->affinity_notify;
  240. desc->affinity_notify = notify;
  241. raw_spin_unlock_irqrestore(&desc->lock, flags);
  242. if (old_notify)
  243. kref_put(&old_notify->kref, old_notify->release);
  244. return 0;
  245. }
  246. EXPORT_SYMBOL_GPL(irq_set_affinity_notifier);
  247. #ifndef CONFIG_AUTO_IRQ_AFFINITY
  248. /*
  249. * Generic version of the affinity autoselector.
  250. */
  251. static int
  252. setup_affinity(unsigned int irq, struct irq_desc *desc, struct cpumask *mask)
  253. {
  254. struct cpumask *set = irq_default_affinity;
  255. int node = desc->irq_data.node;
  256. /* Excludes PER_CPU and NO_BALANCE interrupts */
  257. if (!irq_can_set_affinity(irq))
  258. return 0;
  259. /*
  260. * Preserve an userspace affinity setup, but make sure that
  261. * one of the targets is online.
  262. */
  263. if (irqd_has_set(&desc->irq_data, IRQD_AFFINITY_SET)) {
  264. if (cpumask_intersects(desc->irq_data.affinity,
  265. cpu_online_mask))
  266. set = desc->irq_data.affinity;
  267. else
  268. irqd_clear(&desc->irq_data, IRQD_AFFINITY_SET);
  269. }
  270. cpumask_and(mask, cpu_online_mask, set);
  271. if (node != NUMA_NO_NODE) {
  272. const struct cpumask *nodemask = cpumask_of_node(node);
  273. /* make sure at least one of the cpus in nodemask is online */
  274. if (cpumask_intersects(mask, nodemask))
  275. cpumask_and(mask, mask, nodemask);
  276. }
  277. irq_do_set_affinity(&desc->irq_data, mask, false);
  278. return 0;
  279. }
  280. #else
  281. static inline int
  282. setup_affinity(unsigned int irq, struct irq_desc *d, struct cpumask *mask)
  283. {
  284. return irq_select_affinity(irq);
  285. }
  286. #endif
  287. /*
  288. * Called when affinity is set via /proc/irq
  289. */
  290. int irq_select_affinity_usr(unsigned int irq, struct cpumask *mask)
  291. {
  292. struct irq_desc *desc = irq_to_desc(irq);
  293. unsigned long flags;
  294. int ret;
  295. raw_spin_lock_irqsave(&desc->lock, flags);
  296. ret = setup_affinity(irq, desc, mask);
  297. raw_spin_unlock_irqrestore(&desc->lock, flags);
  298. return ret;
  299. }
  300. #else
  301. static inline int
  302. setup_affinity(unsigned int irq, struct irq_desc *desc, struct cpumask *mask)
  303. {
  304. return 0;
  305. }
  306. #endif
  307. void __disable_irq(struct irq_desc *desc, unsigned int irq, bool suspend)
  308. {
  309. if (suspend) {
  310. if (!desc->action || (desc->action->flags & IRQF_NO_SUSPEND))
  311. return;
  312. desc->istate |= IRQS_SUSPENDED;
  313. }
  314. if (!desc->depth++)
  315. irq_disable(desc);
  316. }
  317. static int __disable_irq_nosync(unsigned int irq)
  318. {
  319. unsigned long flags;
  320. struct irq_desc *desc = irq_get_desc_buslock(irq, &flags, IRQ_GET_DESC_CHECK_GLOBAL);
  321. if (!desc)
  322. return -EINVAL;
  323. __disable_irq(desc, irq, false);
  324. irq_put_desc_busunlock(desc, flags);
  325. return 0;
  326. }
  327. /**
  328. * disable_irq_nosync - disable an irq without waiting
  329. * @irq: Interrupt to disable
  330. *
  331. * Disable the selected interrupt line. Disables and Enables are
  332. * nested.
  333. * Unlike disable_irq(), this function does not ensure existing
  334. * instances of the IRQ handler have completed before returning.
  335. *
  336. * This function may be called from IRQ context.
  337. */
  338. void disable_irq_nosync(unsigned int irq)
  339. {
  340. __disable_irq_nosync(irq);
  341. }
  342. EXPORT_SYMBOL(disable_irq_nosync);
  343. /**
  344. * disable_irq - disable an irq and wait for completion
  345. * @irq: Interrupt to disable
  346. *
  347. * Disable the selected interrupt line. Enables and Disables are
  348. * nested.
  349. * This function waits for any pending IRQ handlers for this interrupt
  350. * to complete before returning. If you use this function while
  351. * holding a resource the IRQ handler may need you will deadlock.
  352. *
  353. * This function may be called - with care - from IRQ context.
  354. */
  355. void disable_irq(unsigned int irq)
  356. {
  357. if (!__disable_irq_nosync(irq))
  358. synchronize_irq(irq);
  359. }
  360. EXPORT_SYMBOL(disable_irq);
  361. void __enable_irq(struct irq_desc *desc, unsigned int irq, bool resume)
  362. {
  363. if (resume) {
  364. if (!(desc->istate & IRQS_SUSPENDED)) {
  365. if (!desc->action)
  366. return;
  367. if (!(desc->action->flags & IRQF_FORCE_RESUME))
  368. return;
  369. /* Pretend that it got disabled ! */
  370. desc->depth++;
  371. }
  372. desc->istate &= ~IRQS_SUSPENDED;
  373. }
  374. switch (desc->depth) {
  375. case 0:
  376. err_out:
  377. WARN(1, KERN_WARNING "Unbalanced enable for IRQ %d\n", irq);
  378. break;
  379. case 1: {
  380. if (desc->istate & IRQS_SUSPENDED)
  381. goto err_out;
  382. /* Prevent probing on this irq: */
  383. irq_settings_set_noprobe(desc);
  384. irq_enable(desc);
  385. check_irq_resend(desc, irq);
  386. /* fall-through */
  387. }
  388. default:
  389. desc->depth--;
  390. }
  391. }
  392. /**
  393. * enable_irq - enable handling of an irq
  394. * @irq: Interrupt to enable
  395. *
  396. * Undoes the effect of one call to disable_irq(). If this
  397. * matches the last disable, processing of interrupts on this
  398. * IRQ line is re-enabled.
  399. *
  400. * This function may be called from IRQ context only when
  401. * desc->irq_data.chip->bus_lock and desc->chip->bus_sync_unlock are NULL !
  402. */
  403. void enable_irq(unsigned int irq)
  404. {
  405. unsigned long flags;
  406. struct irq_desc *desc = irq_get_desc_buslock(irq, &flags, IRQ_GET_DESC_CHECK_GLOBAL);
  407. if (!desc)
  408. return;
  409. if (WARN(!desc->irq_data.chip,
  410. KERN_ERR "enable_irq before setup/request_irq: irq %u\n", irq))
  411. goto out;
  412. __enable_irq(desc, irq, false);
  413. out:
  414. irq_put_desc_busunlock(desc, flags);
  415. }
  416. EXPORT_SYMBOL(enable_irq);
  417. static int set_irq_wake_real(unsigned int irq, unsigned int on)
  418. {
  419. struct irq_desc *desc = irq_to_desc(irq);
  420. int ret = -ENXIO;
  421. if (irq_desc_get_chip(desc)->flags & IRQCHIP_SKIP_SET_WAKE)
  422. return 0;
  423. if (desc->irq_data.chip->irq_set_wake)
  424. ret = desc->irq_data.chip->irq_set_wake(&desc->irq_data, on);
  425. return ret;
  426. }
  427. /**
  428. * irq_set_irq_wake - control irq power management wakeup
  429. * @irq: interrupt to control
  430. * @on: enable/disable power management wakeup
  431. *
  432. * Enable/disable power management wakeup mode, which is
  433. * disabled by default. Enables and disables must match,
  434. * just as they match for non-wakeup mode support.
  435. *
  436. * Wakeup mode lets this IRQ wake the system from sleep
  437. * states like "suspend to RAM".
  438. */
  439. int irq_set_irq_wake(unsigned int irq, unsigned int on)
  440. {
  441. unsigned long flags;
  442. struct irq_desc *desc = irq_get_desc_buslock(irq, &flags, IRQ_GET_DESC_CHECK_GLOBAL);
  443. int ret = 0;
  444. if (!desc)
  445. return -EINVAL;
  446. /* wakeup-capable irqs can be shared between drivers that
  447. * don't need to have the same sleep mode behaviors.
  448. */
  449. if (on) {
  450. if (desc->wake_depth++ == 0) {
  451. ret = set_irq_wake_real(irq, on);
  452. if (ret)
  453. desc->wake_depth = 0;
  454. else
  455. irqd_set(&desc->irq_data, IRQD_WAKEUP_STATE);
  456. }
  457. } else {
  458. if (desc->wake_depth == 0) {
  459. WARN(1, "Unbalanced IRQ %d wake disable\n", irq);
  460. } else if (--desc->wake_depth == 0) {
  461. ret = set_irq_wake_real(irq, on);
  462. if (ret)
  463. desc->wake_depth = 1;
  464. else
  465. irqd_clear(&desc->irq_data, IRQD_WAKEUP_STATE);
  466. }
  467. }
  468. irq_put_desc_busunlock(desc, flags);
  469. return ret;
  470. }
  471. EXPORT_SYMBOL(irq_set_irq_wake);
  472. /*
  473. * Internal function that tells the architecture code whether a
  474. * particular irq has been exclusively allocated or is available
  475. * for driver use.
  476. */
  477. int can_request_irq(unsigned int irq, unsigned long irqflags)
  478. {
  479. unsigned long flags;
  480. struct irq_desc *desc = irq_get_desc_lock(irq, &flags, 0);
  481. int canrequest = 0;
  482. if (!desc)
  483. return 0;
  484. if (irq_settings_can_request(desc)) {
  485. if (desc->action)
  486. if (irqflags & desc->action->flags & IRQF_SHARED)
  487. canrequest =1;
  488. }
  489. irq_put_desc_unlock(desc, flags);
  490. return canrequest;
  491. }
  492. int __irq_set_trigger(struct irq_desc *desc, unsigned int irq,
  493. unsigned long flags)
  494. {
  495. struct irq_chip *chip = desc->irq_data.chip;
  496. int ret, unmask = 0;
  497. if (!chip || !chip->irq_set_type) {
  498. /*
  499. * IRQF_TRIGGER_* but the PIC does not support multiple
  500. * flow-types?
  501. */
  502. pr_debug("No set_type function for IRQ %d (%s)\n", irq,
  503. chip ? (chip->name ? : "unknown") : "unknown");
  504. return 0;
  505. }
  506. flags &= IRQ_TYPE_SENSE_MASK;
  507. if (chip->flags & IRQCHIP_SET_TYPE_MASKED) {
  508. if (!irqd_irq_masked(&desc->irq_data))
  509. mask_irq(desc);
  510. if (!irqd_irq_disabled(&desc->irq_data))
  511. unmask = 1;
  512. }
  513. /* caller masked out all except trigger mode flags */
  514. ret = chip->irq_set_type(&desc->irq_data, flags);
  515. switch (ret) {
  516. case IRQ_SET_MASK_OK:
  517. irqd_clear(&desc->irq_data, IRQD_TRIGGER_MASK);
  518. irqd_set(&desc->irq_data, flags);
  519. case IRQ_SET_MASK_OK_NOCOPY:
  520. flags = irqd_get_trigger_type(&desc->irq_data);
  521. irq_settings_set_trigger_mask(desc, flags);
  522. irqd_clear(&desc->irq_data, IRQD_LEVEL);
  523. irq_settings_clr_level(desc);
  524. if (flags & IRQ_TYPE_LEVEL_MASK) {
  525. irq_settings_set_level(desc);
  526. irqd_set(&desc->irq_data, IRQD_LEVEL);
  527. }
  528. ret = 0;
  529. break;
  530. default:
  531. pr_err("Setting trigger mode %lu for irq %u failed (%pF)\n",
  532. flags, irq, chip->irq_set_type);
  533. }
  534. if (unmask)
  535. unmask_irq(desc);
  536. return ret;
  537. }
  538. #ifdef CONFIG_HARDIRQS_SW_RESEND
  539. int irq_set_parent(int irq, int parent_irq)
  540. {
  541. unsigned long flags;
  542. struct irq_desc *desc = irq_get_desc_lock(irq, &flags, 0);
  543. if (!desc)
  544. return -EINVAL;
  545. desc->parent_irq = parent_irq;
  546. irq_put_desc_unlock(desc, flags);
  547. return 0;
  548. }
  549. #endif
  550. /*
  551. * Default primary interrupt handler for threaded interrupts. Is
  552. * assigned as primary handler when request_threaded_irq is called
  553. * with handler == NULL. Useful for oneshot interrupts.
  554. */
  555. static irqreturn_t irq_default_primary_handler(int irq, void *dev_id)
  556. {
  557. return IRQ_WAKE_THREAD;
  558. }
  559. /*
  560. * Primary handler for nested threaded interrupts. Should never be
  561. * called.
  562. */
  563. static irqreturn_t irq_nested_primary_handler(int irq, void *dev_id)
  564. {
  565. WARN(1, "Primary handler called for nested irq %d\n", irq);
  566. return IRQ_NONE;
  567. }
  568. static int irq_wait_for_interrupt(struct irqaction *action)
  569. {
  570. set_current_state(TASK_INTERRUPTIBLE);
  571. while (!kthread_should_stop()) {
  572. if (test_and_clear_bit(IRQTF_RUNTHREAD,
  573. &action->thread_flags)) {
  574. __set_current_state(TASK_RUNNING);
  575. return 0;
  576. }
  577. schedule();
  578. set_current_state(TASK_INTERRUPTIBLE);
  579. }
  580. __set_current_state(TASK_RUNNING);
  581. return -1;
  582. }
  583. /*
  584. * Oneshot interrupts keep the irq line masked until the threaded
  585. * handler finished. unmask if the interrupt has not been disabled and
  586. * is marked MASKED.
  587. */
  588. static void irq_finalize_oneshot(struct irq_desc *desc,
  589. struct irqaction *action)
  590. {
  591. if (!(desc->istate & IRQS_ONESHOT))
  592. return;
  593. again:
  594. chip_bus_lock(desc);
  595. raw_spin_lock_irq(&desc->lock);
  596. /*
  597. * Implausible though it may be we need to protect us against
  598. * the following scenario:
  599. *
  600. * The thread is faster done than the hard interrupt handler
  601. * on the other CPU. If we unmask the irq line then the
  602. * interrupt can come in again and masks the line, leaves due
  603. * to IRQS_INPROGRESS and the irq line is masked forever.
  604. *
  605. * This also serializes the state of shared oneshot handlers
  606. * versus "desc->threads_onehsot |= action->thread_mask;" in
  607. * irq_wake_thread(). See the comment there which explains the
  608. * serialization.
  609. */
  610. if (unlikely(irqd_irq_inprogress(&desc->irq_data))) {
  611. raw_spin_unlock_irq(&desc->lock);
  612. chip_bus_sync_unlock(desc);
  613. cpu_relax();
  614. goto again;
  615. }
  616. /*
  617. * Now check again, whether the thread should run. Otherwise
  618. * we would clear the threads_oneshot bit of this thread which
  619. * was just set.
  620. */
  621. if (test_bit(IRQTF_RUNTHREAD, &action->thread_flags))
  622. goto out_unlock;
  623. desc->threads_oneshot &= ~action->thread_mask;
  624. if (!desc->threads_oneshot && !irqd_irq_disabled(&desc->irq_data) &&
  625. irqd_irq_masked(&desc->irq_data))
  626. unmask_irq(desc);
  627. out_unlock:
  628. raw_spin_unlock_irq(&desc->lock);
  629. chip_bus_sync_unlock(desc);
  630. }
  631. #ifdef CONFIG_SMP
  632. /*
  633. * Check whether we need to chasnge the affinity of the interrupt thread.
  634. */
  635. static void
  636. irq_thread_check_affinity(struct irq_desc *desc, struct irqaction *action)
  637. {
  638. cpumask_var_t mask;
  639. bool valid = true;
  640. if (!test_and_clear_bit(IRQTF_AFFINITY, &action->thread_flags))
  641. return;
  642. /*
  643. * In case we are out of memory we set IRQTF_AFFINITY again and
  644. * try again next time
  645. */
  646. if (!alloc_cpumask_var(&mask, GFP_KERNEL)) {
  647. set_bit(IRQTF_AFFINITY, &action->thread_flags);
  648. return;
  649. }
  650. raw_spin_lock_irq(&desc->lock);
  651. /*
  652. * This code is triggered unconditionally. Check the affinity
  653. * mask pointer. For CPU_MASK_OFFSTACK=n this is optimized out.
  654. */
  655. if (desc->irq_data.affinity)
  656. cpumask_copy(mask, desc->irq_data.affinity);
  657. else
  658. valid = false;
  659. raw_spin_unlock_irq(&desc->lock);
  660. if (valid)
  661. set_cpus_allowed_ptr(current, mask);
  662. free_cpumask_var(mask);
  663. }
  664. #else
  665. static inline void
  666. irq_thread_check_affinity(struct irq_desc *desc, struct irqaction *action) { }
  667. #endif
  668. /*
  669. * Interrupts which are not explicitely requested as threaded
  670. * interrupts rely on the implicit bh/preempt disable of the hard irq
  671. * context. So we need to disable bh here to avoid deadlocks and other
  672. * side effects.
  673. */
  674. static irqreturn_t
  675. irq_forced_thread_fn(struct irq_desc *desc, struct irqaction *action)
  676. {
  677. irqreturn_t ret;
  678. local_bh_disable();
  679. ret = action->thread_fn(action->irq, action->dev_id);
  680. irq_finalize_oneshot(desc, action);
  681. local_bh_enable();
  682. return ret;
  683. }
  684. /*
  685. * Interrupts explicitely requested as threaded interupts want to be
  686. * preemtible - many of them need to sleep and wait for slow busses to
  687. * complete.
  688. */
  689. static irqreturn_t irq_thread_fn(struct irq_desc *desc,
  690. struct irqaction *action)
  691. {
  692. irqreturn_t ret;
  693. ret = action->thread_fn(action->irq, action->dev_id);
  694. irq_finalize_oneshot(desc, action);
  695. return ret;
  696. }
  697. static void wake_threads_waitq(struct irq_desc *desc)
  698. {
  699. if (atomic_dec_and_test(&desc->threads_active) &&
  700. waitqueue_active(&desc->wait_for_threads))
  701. wake_up(&desc->wait_for_threads);
  702. }
  703. static void irq_thread_dtor(struct callback_head *unused)
  704. {
  705. struct task_struct *tsk = current;
  706. struct irq_desc *desc;
  707. struct irqaction *action;
  708. if (WARN_ON_ONCE(!(current->flags & PF_EXITING)))
  709. return;
  710. action = kthread_data(tsk);
  711. pr_err("exiting task \"%s\" (%d) is an active IRQ thread (irq %d)\n",
  712. tsk->comm, tsk->pid, action->irq);
  713. desc = irq_to_desc(action->irq);
  714. /*
  715. * If IRQTF_RUNTHREAD is set, we need to decrement
  716. * desc->threads_active and wake possible waiters.
  717. */
  718. if (test_and_clear_bit(IRQTF_RUNTHREAD, &action->thread_flags))
  719. wake_threads_waitq(desc);
  720. /* Prevent a stale desc->threads_oneshot */
  721. irq_finalize_oneshot(desc, action);
  722. }
  723. /*
  724. * Interrupt handler thread
  725. */
  726. static int irq_thread(void *data)
  727. {
  728. struct callback_head on_exit_work;
  729. static const struct sched_param param = {
  730. .sched_priority = MAX_USER_RT_PRIO/2,
  731. };
  732. struct irqaction *action = data;
  733. struct irq_desc *desc = irq_to_desc(action->irq);
  734. irqreturn_t (*handler_fn)(struct irq_desc *desc,
  735. struct irqaction *action);
  736. if (force_irqthreads && test_bit(IRQTF_FORCED_THREAD,
  737. &action->thread_flags))
  738. handler_fn = irq_forced_thread_fn;
  739. else
  740. handler_fn = irq_thread_fn;
  741. sched_setscheduler(current, SCHED_FIFO, &param);
  742. init_task_work(&on_exit_work, irq_thread_dtor);
  743. task_work_add(current, &on_exit_work, false);
  744. irq_thread_check_affinity(desc, action);
  745. while (!irq_wait_for_interrupt(action)) {
  746. irqreturn_t action_ret;
  747. irq_thread_check_affinity(desc, action);
  748. action_ret = handler_fn(desc, action);
  749. if (!noirqdebug)
  750. note_interrupt(action->irq, desc, action_ret);
  751. wake_threads_waitq(desc);
  752. }
  753. /*
  754. * This is the regular exit path. __free_irq() is stopping the
  755. * thread via kthread_stop() after calling
  756. * synchronize_irq(). So neither IRQTF_RUNTHREAD nor the
  757. * oneshot mask bit can be set. We cannot verify that as we
  758. * cannot touch the oneshot mask at this point anymore as
  759. * __setup_irq() might have given out currents thread_mask
  760. * again.
  761. */
  762. task_work_cancel(current, irq_thread_dtor);
  763. return 0;
  764. }
  765. static void irq_setup_forced_threading(struct irqaction *new)
  766. {
  767. if (!force_irqthreads)
  768. return;
  769. if (new->flags & (IRQF_NO_THREAD | IRQF_PERCPU | IRQF_ONESHOT))
  770. return;
  771. new->flags |= IRQF_ONESHOT;
  772. if (!new->thread_fn) {
  773. set_bit(IRQTF_FORCED_THREAD, &new->thread_flags);
  774. new->thread_fn = new->handler;
  775. new->handler = irq_default_primary_handler;
  776. }
  777. }
  778. /*
  779. * Internal function to register an irqaction - typically used to
  780. * allocate special interrupts that are part of the architecture.
  781. */
  782. static int
  783. __setup_irq(unsigned int irq, struct irq_desc *desc, struct irqaction *new)
  784. {
  785. struct irqaction *old, **old_ptr;
  786. unsigned long flags, thread_mask = 0;
  787. int ret, nested, shared = 0;
  788. cpumask_var_t mask;
  789. if (!desc)
  790. return -EINVAL;
  791. if (desc->irq_data.chip == &no_irq_chip)
  792. return -ENOSYS;
  793. if (!try_module_get(desc->owner))
  794. return -ENODEV;
  795. /*
  796. * Check whether the interrupt nests into another interrupt
  797. * thread.
  798. */
  799. nested = irq_settings_is_nested_thread(desc);
  800. if (nested) {
  801. if (!new->thread_fn) {
  802. ret = -EINVAL;
  803. goto out_mput;
  804. }
  805. /*
  806. * Replace the primary handler which was provided from
  807. * the driver for non nested interrupt handling by the
  808. * dummy function which warns when called.
  809. */
  810. new->handler = irq_nested_primary_handler;
  811. } else {
  812. if (irq_settings_can_thread(desc))
  813. irq_setup_forced_threading(new);
  814. }
  815. /*
  816. * Create a handler thread when a thread function is supplied
  817. * and the interrupt does not nest into another interrupt
  818. * thread.
  819. */
  820. if (new->thread_fn && !nested) {
  821. struct task_struct *t;
  822. t = kthread_create(irq_thread, new, "irq/%d-%s", irq,
  823. new->name);
  824. if (IS_ERR(t)) {
  825. ret = PTR_ERR(t);
  826. goto out_mput;
  827. }
  828. /*
  829. * We keep the reference to the task struct even if
  830. * the thread dies to avoid that the interrupt code
  831. * references an already freed task_struct.
  832. */
  833. get_task_struct(t);
  834. new->thread = t;
  835. /*
  836. * Tell the thread to set its affinity. This is
  837. * important for shared interrupt handlers as we do
  838. * not invoke setup_affinity() for the secondary
  839. * handlers as everything is already set up. Even for
  840. * interrupts marked with IRQF_NO_BALANCE this is
  841. * correct as we want the thread to move to the cpu(s)
  842. * on which the requesting code placed the interrupt.
  843. */
  844. set_bit(IRQTF_AFFINITY, &new->thread_flags);
  845. }
  846. if (!alloc_cpumask_var(&mask, GFP_KERNEL)) {
  847. ret = -ENOMEM;
  848. goto out_thread;
  849. }
  850. /*
  851. * Drivers are often written to work w/o knowledge about the
  852. * underlying irq chip implementation, so a request for a
  853. * threaded irq without a primary hard irq context handler
  854. * requires the ONESHOT flag to be set. Some irq chips like
  855. * MSI based interrupts are per se one shot safe. Check the
  856. * chip flags, so we can avoid the unmask dance at the end of
  857. * the threaded handler for those.
  858. */
  859. if (desc->irq_data.chip->flags & IRQCHIP_ONESHOT_SAFE)
  860. new->flags &= ~IRQF_ONESHOT;
  861. /*
  862. * The following block of code has to be executed atomically
  863. */
  864. raw_spin_lock_irqsave(&desc->lock, flags);
  865. old_ptr = &desc->action;
  866. old = *old_ptr;
  867. if (old) {
  868. /*
  869. * Can't share interrupts unless both agree to and are
  870. * the same type (level, edge, polarity). So both flag
  871. * fields must have IRQF_SHARED set and the bits which
  872. * set the trigger type must match. Also all must
  873. * agree on ONESHOT.
  874. */
  875. if (!((old->flags & new->flags) & IRQF_SHARED) ||
  876. ((old->flags ^ new->flags) & IRQF_TRIGGER_MASK) ||
  877. ((old->flags ^ new->flags) & IRQF_ONESHOT))
  878. goto mismatch;
  879. /* All handlers must agree on per-cpuness */
  880. if ((old->flags & IRQF_PERCPU) !=
  881. (new->flags & IRQF_PERCPU))
  882. goto mismatch;
  883. /* add new interrupt at end of irq queue */
  884. do {
  885. /*
  886. * Or all existing action->thread_mask bits,
  887. * so we can find the next zero bit for this
  888. * new action.
  889. */
  890. thread_mask |= old->thread_mask;
  891. old_ptr = &old->next;
  892. old = *old_ptr;
  893. } while (old);
  894. shared = 1;
  895. }
  896. /*
  897. * Setup the thread mask for this irqaction for ONESHOT. For
  898. * !ONESHOT irqs the thread mask is 0 so we can avoid a
  899. * conditional in irq_wake_thread().
  900. */
  901. if (new->flags & IRQF_ONESHOT) {
  902. /*
  903. * Unlikely to have 32 resp 64 irqs sharing one line,
  904. * but who knows.
  905. */
  906. if (thread_mask == ~0UL) {
  907. ret = -EBUSY;
  908. goto out_mask;
  909. }
  910. /*
  911. * The thread_mask for the action is or'ed to
  912. * desc->thread_active to indicate that the
  913. * IRQF_ONESHOT thread handler has been woken, but not
  914. * yet finished. The bit is cleared when a thread
  915. * completes. When all threads of a shared interrupt
  916. * line have completed desc->threads_active becomes
  917. * zero and the interrupt line is unmasked. See
  918. * handle.c:irq_wake_thread() for further information.
  919. *
  920. * If no thread is woken by primary (hard irq context)
  921. * interrupt handlers, then desc->threads_active is
  922. * also checked for zero to unmask the irq line in the
  923. * affected hard irq flow handlers
  924. * (handle_[fasteoi|level]_irq).
  925. *
  926. * The new action gets the first zero bit of
  927. * thread_mask assigned. See the loop above which or's
  928. * all existing action->thread_mask bits.
  929. */
  930. new->thread_mask = 1 << ffz(thread_mask);
  931. } else if (new->handler == irq_default_primary_handler &&
  932. !(desc->irq_data.chip->flags & IRQCHIP_ONESHOT_SAFE)) {
  933. /*
  934. * The interrupt was requested with handler = NULL, so
  935. * we use the default primary handler for it. But it
  936. * does not have the oneshot flag set. In combination
  937. * with level interrupts this is deadly, because the
  938. * default primary handler just wakes the thread, then
  939. * the irq lines is reenabled, but the device still
  940. * has the level irq asserted. Rinse and repeat....
  941. *
  942. * While this works for edge type interrupts, we play
  943. * it safe and reject unconditionally because we can't
  944. * say for sure which type this interrupt really
  945. * has. The type flags are unreliable as the
  946. * underlying chip implementation can override them.
  947. */
  948. pr_err("Threaded irq requested with handler=NULL and !ONESHOT for irq %d\n",
  949. irq);
  950. ret = -EINVAL;
  951. goto out_mask;
  952. }
  953. if (!shared) {
  954. init_waitqueue_head(&desc->wait_for_threads);
  955. /* Setup the type (level, edge polarity) if configured: */
  956. if (new->flags & IRQF_TRIGGER_MASK) {
  957. ret = __irq_set_trigger(desc, irq,
  958. new->flags & IRQF_TRIGGER_MASK);
  959. if (ret)
  960. goto out_mask;
  961. }
  962. desc->istate &= ~(IRQS_AUTODETECT | IRQS_SPURIOUS_DISABLED | \
  963. IRQS_ONESHOT | IRQS_WAITING);
  964. irqd_clear(&desc->irq_data, IRQD_IRQ_INPROGRESS);
  965. if (new->flags & IRQF_PERCPU) {
  966. irqd_set(&desc->irq_data, IRQD_PER_CPU);
  967. irq_settings_set_per_cpu(desc);
  968. }
  969. if (new->flags & IRQF_ONESHOT)
  970. desc->istate |= IRQS_ONESHOT;
  971. if (irq_settings_can_autoenable(desc))
  972. irq_startup(desc, true);
  973. else
  974. /* Undo nested disables: */
  975. desc->depth = 1;
  976. /* Exclude IRQ from balancing if requested */
  977. if (new->flags & IRQF_NOBALANCING) {
  978. irq_settings_set_no_balancing(desc);
  979. irqd_set(&desc->irq_data, IRQD_NO_BALANCING);
  980. }
  981. /* Set default affinity mask once everything is setup */
  982. setup_affinity(irq, desc, mask);
  983. } else if (new->flags & IRQF_TRIGGER_MASK) {
  984. unsigned int nmsk = new->flags & IRQF_TRIGGER_MASK;
  985. unsigned int omsk = irq_settings_get_trigger_mask(desc);
  986. if (nmsk != omsk)
  987. /* hope the handler works with current trigger mode */
  988. pr_warning("irq %d uses trigger mode %u; requested %u\n",
  989. irq, nmsk, omsk);
  990. }
  991. new->irq = irq;
  992. *old_ptr = new;
  993. /* Reset broken irq detection when installing new handler */
  994. desc->irq_count = 0;
  995. desc->irqs_unhandled = 0;
  996. /*
  997. * Check whether we disabled the irq via the spurious handler
  998. * before. Reenable it and give it another chance.
  999. */
  1000. if (shared && (desc->istate & IRQS_SPURIOUS_DISABLED)) {
  1001. desc->istate &= ~IRQS_SPURIOUS_DISABLED;
  1002. __enable_irq(desc, irq, false);
  1003. }
  1004. raw_spin_unlock_irqrestore(&desc->lock, flags);
  1005. /*
  1006. * Strictly no need to wake it up, but hung_task complains
  1007. * when no hard interrupt wakes the thread up.
  1008. */
  1009. if (new->thread)
  1010. wake_up_process(new->thread);
  1011. register_irq_proc(irq, desc);
  1012. new->dir = NULL;
  1013. register_handler_proc(irq, new);
  1014. free_cpumask_var(mask);
  1015. return 0;
  1016. mismatch:
  1017. if (!(new->flags & IRQF_PROBE_SHARED)) {
  1018. pr_err("Flags mismatch irq %d. %08x (%s) vs. %08x (%s)\n",
  1019. irq, new->flags, new->name, old->flags, old->name);
  1020. #ifdef CONFIG_DEBUG_SHIRQ
  1021. dump_stack();
  1022. #endif
  1023. }
  1024. ret = -EBUSY;
  1025. out_mask:
  1026. raw_spin_unlock_irqrestore(&desc->lock, flags);
  1027. free_cpumask_var(mask);
  1028. out_thread:
  1029. if (new->thread) {
  1030. struct task_struct *t = new->thread;
  1031. new->thread = NULL;
  1032. kthread_stop(t);
  1033. put_task_struct(t);
  1034. }
  1035. out_mput:
  1036. module_put(desc->owner);
  1037. return ret;
  1038. }
  1039. /**
  1040. * setup_irq - setup an interrupt
  1041. * @irq: Interrupt line to setup
  1042. * @act: irqaction for the interrupt
  1043. *
  1044. * Used to statically setup interrupts in the early boot process.
  1045. */
  1046. int setup_irq(unsigned int irq, struct irqaction *act)
  1047. {
  1048. int retval;
  1049. struct irq_desc *desc = irq_to_desc(irq);
  1050. if (WARN_ON(irq_settings_is_per_cpu_devid(desc)))
  1051. return -EINVAL;
  1052. chip_bus_lock(desc);
  1053. retval = __setup_irq(irq, desc, act);
  1054. chip_bus_sync_unlock(desc);
  1055. return retval;
  1056. }
  1057. EXPORT_SYMBOL_GPL(setup_irq);
  1058. /*
  1059. * Internal function to unregister an irqaction - used to free
  1060. * regular and special interrupts that are part of the architecture.
  1061. */
  1062. static struct irqaction *__free_irq(unsigned int irq, void *dev_id)
  1063. {
  1064. struct irq_desc *desc = irq_to_desc(irq);
  1065. struct irqaction *action, **action_ptr;
  1066. unsigned long flags;
  1067. WARN(in_interrupt(), "Trying to free IRQ %d from IRQ context!\n", irq);
  1068. if (!desc)
  1069. return NULL;
  1070. raw_spin_lock_irqsave(&desc->lock, flags);
  1071. /*
  1072. * There can be multiple actions per IRQ descriptor, find the right
  1073. * one based on the dev_id:
  1074. */
  1075. action_ptr = &desc->action;
  1076. for (;;) {
  1077. action = *action_ptr;
  1078. if (!action) {
  1079. WARN(1, "Trying to free already-free IRQ %d\n", irq);
  1080. raw_spin_unlock_irqrestore(&desc->lock, flags);
  1081. return NULL;
  1082. }
  1083. if (action->dev_id == dev_id)
  1084. break;
  1085. action_ptr = &action->next;
  1086. }
  1087. /* Found it - now remove it from the list of entries: */
  1088. *action_ptr = action->next;
  1089. /* If this was the last handler, shut down the IRQ line: */
  1090. if (!desc->action)
  1091. irq_shutdown(desc);
  1092. #ifdef CONFIG_SMP
  1093. /* make sure affinity_hint is cleaned up */
  1094. if (WARN_ON_ONCE(desc->affinity_hint))
  1095. desc->affinity_hint = NULL;
  1096. #endif
  1097. raw_spin_unlock_irqrestore(&desc->lock, flags);
  1098. unregister_handler_proc(irq, action);
  1099. /* Make sure it's not being used on another CPU: */
  1100. synchronize_irq(irq);
  1101. #ifdef CONFIG_DEBUG_SHIRQ
  1102. /*
  1103. * It's a shared IRQ -- the driver ought to be prepared for an IRQ
  1104. * event to happen even now it's being freed, so let's make sure that
  1105. * is so by doing an extra call to the handler ....
  1106. *
  1107. * ( We do this after actually deregistering it, to make sure that a
  1108. * 'real' IRQ doesn't run in * parallel with our fake. )
  1109. */
  1110. if (action->flags & IRQF_SHARED) {
  1111. local_irq_save(flags);
  1112. action->handler(irq, dev_id);
  1113. local_irq_restore(flags);
  1114. }
  1115. #endif
  1116. if (action->thread) {
  1117. kthread_stop(action->thread);
  1118. put_task_struct(action->thread);
  1119. }
  1120. module_put(desc->owner);
  1121. return action;
  1122. }
  1123. /**
  1124. * remove_irq - free an interrupt
  1125. * @irq: Interrupt line to free
  1126. * @act: irqaction for the interrupt
  1127. *
  1128. * Used to remove interrupts statically setup by the early boot process.
  1129. */
  1130. void remove_irq(unsigned int irq, struct irqaction *act)
  1131. {
  1132. struct irq_desc *desc = irq_to_desc(irq);
  1133. if (desc && !WARN_ON(irq_settings_is_per_cpu_devid(desc)))
  1134. __free_irq(irq, act->dev_id);
  1135. }
  1136. EXPORT_SYMBOL_GPL(remove_irq);
  1137. /**
  1138. * free_irq - free an interrupt allocated with request_irq
  1139. * @irq: Interrupt line to free
  1140. * @dev_id: Device identity to free
  1141. *
  1142. * Remove an interrupt handler. The handler is removed and if the
  1143. * interrupt line is no longer in use by any driver it is disabled.
  1144. * On a shared IRQ the caller must ensure the interrupt is disabled
  1145. * on the card it drives before calling this function. The function
  1146. * does not return until any executing interrupts for this IRQ
  1147. * have completed.
  1148. *
  1149. * This function must not be called from interrupt context.
  1150. */
  1151. void free_irq(unsigned int irq, void *dev_id)
  1152. {
  1153. struct irq_desc *desc = irq_to_desc(irq);
  1154. if (!desc || WARN_ON(irq_settings_is_per_cpu_devid(desc)))
  1155. return;
  1156. #ifdef CONFIG_SMP
  1157. if (WARN_ON(desc->affinity_notify))
  1158. desc->affinity_notify = NULL;
  1159. #endif
  1160. chip_bus_lock(desc);
  1161. kfree(__free_irq(irq, dev_id));
  1162. chip_bus_sync_unlock(desc);
  1163. }
  1164. EXPORT_SYMBOL(free_irq);
  1165. /**
  1166. * request_threaded_irq - allocate an interrupt line
  1167. * @irq: Interrupt line to allocate
  1168. * @handler: Function to be called when the IRQ occurs.
  1169. * Primary handler for threaded interrupts
  1170. * If NULL and thread_fn != NULL the default
  1171. * primary handler is installed
  1172. * @thread_fn: Function called from the irq handler thread
  1173. * If NULL, no irq thread is created
  1174. * @irqflags: Interrupt type flags
  1175. * @devname: An ascii name for the claiming device
  1176. * @dev_id: A cookie passed back to the handler function
  1177. *
  1178. * This call allocates interrupt resources and enables the
  1179. * interrupt line and IRQ handling. From the point this
  1180. * call is made your handler function may be invoked. Since
  1181. * your handler function must clear any interrupt the board
  1182. * raises, you must take care both to initialise your hardware
  1183. * and to set up the interrupt handler in the right order.
  1184. *
  1185. * If you want to set up a threaded irq handler for your device
  1186. * then you need to supply @handler and @thread_fn. @handler is
  1187. * still called in hard interrupt context and has to check
  1188. * whether the interrupt originates from the device. If yes it
  1189. * needs to disable the interrupt on the device and return
  1190. * IRQ_WAKE_THREAD which will wake up the handler thread and run
  1191. * @thread_fn. This split handler design is necessary to support
  1192. * shared interrupts.
  1193. *
  1194. * Dev_id must be globally unique. Normally the address of the
  1195. * device data structure is used as the cookie. Since the handler
  1196. * receives this value it makes sense to use it.
  1197. *
  1198. * If your interrupt is shared you must pass a non NULL dev_id
  1199. * as this is required when freeing the interrupt.
  1200. *
  1201. * Flags:
  1202. *
  1203. * IRQF_SHARED Interrupt is shared
  1204. * IRQF_TRIGGER_* Specify active edge(s) or level
  1205. *
  1206. */
  1207. int request_threaded_irq(unsigned int irq, irq_handler_t handler,
  1208. irq_handler_t thread_fn, unsigned long irqflags,
  1209. const char *devname, void *dev_id)
  1210. {
  1211. struct irqaction *action;
  1212. struct irq_desc *desc;
  1213. int retval;
  1214. /*
  1215. * Sanity-check: shared interrupts must pass in a real dev-ID,
  1216. * otherwise we'll have trouble later trying to figure out
  1217. * which interrupt is which (messes up the interrupt freeing
  1218. * logic etc).
  1219. */
  1220. if ((irqflags & IRQF_SHARED) && !dev_id)
  1221. return -EINVAL;
  1222. desc = irq_to_desc(irq);
  1223. if (!desc)
  1224. return -EINVAL;
  1225. if (!irq_settings_can_request(desc) ||
  1226. WARN_ON(irq_settings_is_per_cpu_devid(desc)))
  1227. return -EINVAL;
  1228. if (!handler) {
  1229. if (!thread_fn)
  1230. return -EINVAL;
  1231. handler = irq_default_primary_handler;
  1232. }
  1233. action = kzalloc(sizeof(struct irqaction), GFP_KERNEL);
  1234. if (!action)
  1235. return -ENOMEM;
  1236. action->handler = handler;
  1237. action->thread_fn = thread_fn;
  1238. action->flags = irqflags;
  1239. action->name = devname;
  1240. action->dev_id = dev_id;
  1241. chip_bus_lock(desc);
  1242. retval = __setup_irq(irq, desc, action);
  1243. chip_bus_sync_unlock(desc);
  1244. if (retval)
  1245. kfree(action);
  1246. #ifdef CONFIG_DEBUG_SHIRQ_FIXME
  1247. if (!retval && (irqflags & IRQF_SHARED)) {
  1248. /*
  1249. * It's a shared IRQ -- the driver ought to be prepared for it
  1250. * to happen immediately, so let's make sure....
  1251. * We disable the irq to make sure that a 'real' IRQ doesn't
  1252. * run in parallel with our fake.
  1253. */
  1254. unsigned long flags;
  1255. disable_irq(irq);
  1256. local_irq_save(flags);
  1257. handler(irq, dev_id);
  1258. local_irq_restore(flags);
  1259. enable_irq(irq);
  1260. }
  1261. #endif
  1262. return retval;
  1263. }
  1264. EXPORT_SYMBOL(request_threaded_irq);
  1265. /**
  1266. * request_any_context_irq - allocate an interrupt line
  1267. * @irq: Interrupt line to allocate
  1268. * @handler: Function to be called when the IRQ occurs.
  1269. * Threaded handler for threaded interrupts.
  1270. * @flags: Interrupt type flags
  1271. * @name: An ascii name for the claiming device
  1272. * @dev_id: A cookie passed back to the handler function
  1273. *
  1274. * This call allocates interrupt resources and enables the
  1275. * interrupt line and IRQ handling. It selects either a
  1276. * hardirq or threaded handling method depending on the
  1277. * context.
  1278. *
  1279. * On failure, it returns a negative value. On success,
  1280. * it returns either IRQC_IS_HARDIRQ or IRQC_IS_NESTED.
  1281. */
  1282. int request_any_context_irq(unsigned int irq, irq_handler_t handler,
  1283. unsigned long flags, const char *name, void *dev_id)
  1284. {
  1285. struct irq_desc *desc = irq_to_desc(irq);
  1286. int ret;
  1287. if (!desc)
  1288. return -EINVAL;
  1289. if (irq_settings_is_nested_thread(desc)) {
  1290. ret = request_threaded_irq(irq, NULL, handler,
  1291. flags, name, dev_id);
  1292. return !ret ? IRQC_IS_NESTED : ret;
  1293. }
  1294. ret = request_irq(irq, handler, flags, name, dev_id);
  1295. return !ret ? IRQC_IS_HARDIRQ : ret;
  1296. }
  1297. EXPORT_SYMBOL_GPL(request_any_context_irq);
  1298. void enable_percpu_irq(unsigned int irq, unsigned int type)
  1299. {
  1300. unsigned int cpu = smp_processor_id();
  1301. unsigned long flags;
  1302. struct irq_desc *desc = irq_get_desc_lock(irq, &flags, IRQ_GET_DESC_CHECK_PERCPU);
  1303. if (!desc)
  1304. return;
  1305. type &= IRQ_TYPE_SENSE_MASK;
  1306. if (type != IRQ_TYPE_NONE) {
  1307. int ret;
  1308. ret = __irq_set_trigger(desc, irq, type);
  1309. if (ret) {
  1310. WARN(1, "failed to set type for IRQ%d\n", irq);
  1311. goto out;
  1312. }
  1313. }
  1314. irq_percpu_enable(desc, cpu);
  1315. out:
  1316. irq_put_desc_unlock(desc, flags);
  1317. }
  1318. EXPORT_SYMBOL_GPL(enable_percpu_irq);
  1319. void disable_percpu_irq(unsigned int irq)
  1320. {
  1321. unsigned int cpu = smp_processor_id();
  1322. unsigned long flags;
  1323. struct irq_desc *desc = irq_get_desc_lock(irq, &flags, IRQ_GET_DESC_CHECK_PERCPU);
  1324. if (!desc)
  1325. return;
  1326. irq_percpu_disable(desc, cpu);
  1327. irq_put_desc_unlock(desc, flags);
  1328. }
  1329. EXPORT_SYMBOL_GPL(disable_percpu_irq);
  1330. /*
  1331. * Internal function to unregister a percpu irqaction.
  1332. */
  1333. static struct irqaction *__free_percpu_irq(unsigned int irq, void __percpu *dev_id)
  1334. {
  1335. struct irq_desc *desc = irq_to_desc(irq);
  1336. struct irqaction *action;
  1337. unsigned long flags;
  1338. WARN(in_interrupt(), "Trying to free IRQ %d from IRQ context!\n", irq);
  1339. if (!desc)
  1340. return NULL;
  1341. raw_spin_lock_irqsave(&desc->lock, flags);
  1342. action = desc->action;
  1343. if (!action || action->percpu_dev_id != dev_id) {
  1344. WARN(1, "Trying to free already-free IRQ %d\n", irq);
  1345. goto bad;
  1346. }
  1347. if (!cpumask_empty(desc->percpu_enabled)) {
  1348. WARN(1, "percpu IRQ %d still enabled on CPU%d!\n",
  1349. irq, cpumask_first(desc->percpu_enabled));
  1350. goto bad;
  1351. }
  1352. /* Found it - now remove it from the list of entries: */
  1353. desc->action = NULL;
  1354. raw_spin_unlock_irqrestore(&desc->lock, flags);
  1355. unregister_handler_proc(irq, action);
  1356. module_put(desc->owner);
  1357. return action;
  1358. bad:
  1359. raw_spin_unlock_irqrestore(&desc->lock, flags);
  1360. return NULL;
  1361. }
  1362. /**
  1363. * remove_percpu_irq - free a per-cpu interrupt
  1364. * @irq: Interrupt line to free
  1365. * @act: irqaction for the interrupt
  1366. *
  1367. * Used to remove interrupts statically setup by the early boot process.
  1368. */
  1369. void remove_percpu_irq(unsigned int irq, struct irqaction *act)
  1370. {
  1371. struct irq_desc *desc = irq_to_desc(irq);
  1372. if (desc && irq_settings_is_per_cpu_devid(desc))
  1373. __free_percpu_irq(irq, act->percpu_dev_id);
  1374. }
  1375. /**
  1376. * free_percpu_irq - free an interrupt allocated with request_percpu_irq
  1377. * @irq: Interrupt line to free
  1378. * @dev_id: Device identity to free
  1379. *
  1380. * Remove a percpu interrupt handler. The handler is removed, but
  1381. * the interrupt line is not disabled. This must be done on each
  1382. * CPU before calling this function. The function does not return
  1383. * until any executing interrupts for this IRQ have completed.
  1384. *
  1385. * This function must not be called from interrupt context.
  1386. */
  1387. void free_percpu_irq(unsigned int irq, void __percpu *dev_id)
  1388. {
  1389. struct irq_desc *desc = irq_to_desc(irq);
  1390. if (!desc || !irq_settings_is_per_cpu_devid(desc))
  1391. return;
  1392. chip_bus_lock(desc);
  1393. kfree(__free_percpu_irq(irq, dev_id));
  1394. chip_bus_sync_unlock(desc);
  1395. }
  1396. /**
  1397. * setup_percpu_irq - setup a per-cpu interrupt
  1398. * @irq: Interrupt line to setup
  1399. * @act: irqaction for the interrupt
  1400. *
  1401. * Used to statically setup per-cpu interrupts in the early boot process.
  1402. */
  1403. int setup_percpu_irq(unsigned int irq, struct irqaction *act)
  1404. {
  1405. struct irq_desc *desc = irq_to_desc(irq);
  1406. int retval;
  1407. if (!desc || !irq_settings_is_per_cpu_devid(desc))
  1408. return -EINVAL;
  1409. chip_bus_lock(desc);
  1410. retval = __setup_irq(irq, desc, act);
  1411. chip_bus_sync_unlock(desc);
  1412. return retval;
  1413. }
  1414. /**
  1415. * request_percpu_irq - allocate a percpu interrupt line
  1416. * @irq: Interrupt line to allocate
  1417. * @handler: Function to be called when the IRQ occurs.
  1418. * @devname: An ascii name for the claiming device
  1419. * @dev_id: A percpu cookie passed back to the handler function
  1420. *
  1421. * This call allocates interrupt resources, but doesn't
  1422. * automatically enable the interrupt. It has to be done on each
  1423. * CPU using enable_percpu_irq().
  1424. *
  1425. * Dev_id must be globally unique. It is a per-cpu variable, and
  1426. * the handler gets called with the interrupted CPU's instance of
  1427. * that variable.
  1428. */
  1429. int request_percpu_irq(unsigned int irq, irq_handler_t handler,
  1430. const char *devname, void __percpu *dev_id)
  1431. {
  1432. struct irqaction *action;
  1433. struct irq_desc *desc;
  1434. int retval;
  1435. if (!dev_id)
  1436. return -EINVAL;
  1437. desc = irq_to_desc(irq);
  1438. if (!desc || !irq_settings_can_request(desc) ||
  1439. !irq_settings_is_per_cpu_devid(desc))
  1440. return -EINVAL;
  1441. action = kzalloc(sizeof(struct irqaction), GFP_KERNEL);
  1442. if (!action)
  1443. return -ENOMEM;
  1444. action->handler = handler;
  1445. action->flags = IRQF_PERCPU | IRQF_NO_SUSPEND;
  1446. action->name = devname;
  1447. action->percpu_dev_id = dev_id;
  1448. chip_bus_lock(desc);
  1449. retval = __setup_irq(irq, desc, action);
  1450. chip_bus_sync_unlock(desc);
  1451. if (retval)
  1452. kfree(action);
  1453. return retval;
  1454. }