manage.c 43 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695
  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. 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. struct irqaction *action = data;
  730. struct irq_desc *desc = irq_to_desc(action->irq);
  731. irqreturn_t (*handler_fn)(struct irq_desc *desc,
  732. struct irqaction *action);
  733. if (force_irqthreads && test_bit(IRQTF_FORCED_THREAD,
  734. &action->thread_flags))
  735. handler_fn = irq_forced_thread_fn;
  736. else
  737. handler_fn = irq_thread_fn;
  738. init_task_work(&on_exit_work, irq_thread_dtor);
  739. task_work_add(current, &on_exit_work, false);
  740. irq_thread_check_affinity(desc, action);
  741. while (!irq_wait_for_interrupt(action)) {
  742. irqreturn_t action_ret;
  743. irq_thread_check_affinity(desc, action);
  744. action_ret = handler_fn(desc, action);
  745. if (!noirqdebug)
  746. note_interrupt(action->irq, desc, action_ret);
  747. wake_threads_waitq(desc);
  748. }
  749. /*
  750. * This is the regular exit path. __free_irq() is stopping the
  751. * thread via kthread_stop() after calling
  752. * synchronize_irq(). So neither IRQTF_RUNTHREAD nor the
  753. * oneshot mask bit can be set. We cannot verify that as we
  754. * cannot touch the oneshot mask at this point anymore as
  755. * __setup_irq() might have given out currents thread_mask
  756. * again.
  757. */
  758. task_work_cancel(current, irq_thread_dtor);
  759. return 0;
  760. }
  761. static void irq_setup_forced_threading(struct irqaction *new)
  762. {
  763. if (!force_irqthreads)
  764. return;
  765. if (new->flags & (IRQF_NO_THREAD | IRQF_PERCPU | IRQF_ONESHOT))
  766. return;
  767. new->flags |= IRQF_ONESHOT;
  768. if (!new->thread_fn) {
  769. set_bit(IRQTF_FORCED_THREAD, &new->thread_flags);
  770. new->thread_fn = new->handler;
  771. new->handler = irq_default_primary_handler;
  772. }
  773. }
  774. /*
  775. * Internal function to register an irqaction - typically used to
  776. * allocate special interrupts that are part of the architecture.
  777. */
  778. static int
  779. __setup_irq(unsigned int irq, struct irq_desc *desc, struct irqaction *new)
  780. {
  781. struct irqaction *old, **old_ptr;
  782. unsigned long flags, thread_mask = 0;
  783. int ret, nested, shared = 0;
  784. cpumask_var_t mask;
  785. if (!desc)
  786. return -EINVAL;
  787. if (desc->irq_data.chip == &no_irq_chip)
  788. return -ENOSYS;
  789. if (!try_module_get(desc->owner))
  790. return -ENODEV;
  791. /*
  792. * Check whether the interrupt nests into another interrupt
  793. * thread.
  794. */
  795. nested = irq_settings_is_nested_thread(desc);
  796. if (nested) {
  797. if (!new->thread_fn) {
  798. ret = -EINVAL;
  799. goto out_mput;
  800. }
  801. /*
  802. * Replace the primary handler which was provided from
  803. * the driver for non nested interrupt handling by the
  804. * dummy function which warns when called.
  805. */
  806. new->handler = irq_nested_primary_handler;
  807. } else {
  808. if (irq_settings_can_thread(desc))
  809. irq_setup_forced_threading(new);
  810. }
  811. /*
  812. * Create a handler thread when a thread function is supplied
  813. * and the interrupt does not nest into another interrupt
  814. * thread.
  815. */
  816. if (new->thread_fn && !nested) {
  817. struct task_struct *t;
  818. static const struct sched_param param = {
  819. .sched_priority = MAX_USER_RT_PRIO/2,
  820. };
  821. t = kthread_create(irq_thread, new, "irq/%d-%s", irq,
  822. new->name);
  823. if (IS_ERR(t)) {
  824. ret = PTR_ERR(t);
  825. goto out_mput;
  826. }
  827. sched_setscheduler(t, SCHED_FIFO, &param);
  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. }