hc
2024-01-04 1543e317f1da31b75942316931e8f491a8920811
kernel/kernel/irq/manage.c
....@@ -18,13 +18,13 @@
1818 #include <linux/sched.h>
1919 #include <linux/sched/rt.h>
2020 #include <linux/sched/task.h>
21
+#include <linux/sched/isolation.h>
2122 #include <uapi/linux/sched/types.h>
2223 #include <linux/task_work.h>
2324
2425 #include "internals.h"
2526
26
-#ifdef CONFIG_IRQ_FORCED_THREADING
27
-# ifndef CONFIG_PREEMPT_RT_BASE
27
+#if defined(CONFIG_IRQ_FORCED_THREADING) && !defined(CONFIG_PREEMPT_RT)
2828 __read_mostly bool force_irqthreads;
2929 EXPORT_SYMBOL_GPL(force_irqthreads);
3030
....@@ -34,7 +34,6 @@
3434 return 0;
3535 }
3636 early_param("threadirqs", setup_forced_irqthreads);
37
-# endif
3837 #endif
3938
4039 static void __synchronize_hardirq(struct irq_desc *desc, bool sync_chip)
....@@ -224,16 +223,70 @@
224223 {
225224 struct irq_desc *desc = irq_data_to_desc(data);
226225 struct irq_chip *chip = irq_data_get_irq_chip(data);
226
+ const struct cpumask *prog_mask;
227227 int ret;
228
+
229
+ static DEFINE_RAW_SPINLOCK(tmp_mask_lock);
230
+ static struct cpumask tmp_mask;
228231
229232 if (!chip || !chip->irq_set_affinity)
230233 return -EINVAL;
231234
232
- ret = chip->irq_set_affinity(data, mask, force);
235
+ raw_spin_lock(&tmp_mask_lock);
236
+ /*
237
+ * If this is a managed interrupt and housekeeping is enabled on
238
+ * it check whether the requested affinity mask intersects with
239
+ * a housekeeping CPU. If so, then remove the isolated CPUs from
240
+ * the mask and just keep the housekeeping CPU(s). This prevents
241
+ * the affinity setter from routing the interrupt to an isolated
242
+ * CPU to avoid that I/O submitted from a housekeeping CPU causes
243
+ * interrupts on an isolated one.
244
+ *
245
+ * If the masks do not intersect or include online CPU(s) then
246
+ * keep the requested mask. The isolated target CPUs are only
247
+ * receiving interrupts when the I/O operation was submitted
248
+ * directly from them.
249
+ *
250
+ * If all housekeeping CPUs in the affinity mask are offline, the
251
+ * interrupt will be migrated by the CPU hotplug code once a
252
+ * housekeeping CPU which belongs to the affinity mask comes
253
+ * online.
254
+ */
255
+ if (irqd_affinity_is_managed(data) &&
256
+ housekeeping_enabled(HK_FLAG_MANAGED_IRQ)) {
257
+ const struct cpumask *hk_mask;
258
+
259
+ hk_mask = housekeeping_cpumask(HK_FLAG_MANAGED_IRQ);
260
+
261
+ cpumask_and(&tmp_mask, mask, hk_mask);
262
+ if (!cpumask_intersects(&tmp_mask, cpu_online_mask))
263
+ prog_mask = mask;
264
+ else
265
+ prog_mask = &tmp_mask;
266
+ } else {
267
+ prog_mask = mask;
268
+ }
269
+
270
+ /*
271
+ * Make sure we only provide online CPUs to the irqchip,
272
+ * unless we are being asked to force the affinity (in which
273
+ * case we do as we are told).
274
+ */
275
+ cpumask_and(&tmp_mask, prog_mask, cpu_online_mask);
276
+ if (!force && !cpumask_empty(&tmp_mask))
277
+ ret = chip->irq_set_affinity(data, &tmp_mask, force);
278
+ else if (force)
279
+ ret = chip->irq_set_affinity(data, mask, force);
280
+ else
281
+ ret = -EINVAL;
282
+
283
+ raw_spin_unlock(&tmp_mask_lock);
284
+
233285 switch (ret) {
234286 case IRQ_SET_MASK_OK:
235287 case IRQ_SET_MASK_OK_DONE:
236288 cpumask_copy(desc->irq_common_data.affinity, mask);
289
+ fallthrough;
237290 case IRQ_SET_MASK_OK_NOCOPY:
238291 irq_validate_effective_affinity(data);
239292 irq_set_thread_affinity(desc);
....@@ -242,6 +295,7 @@
242295
243296 return ret;
244297 }
298
+EXPORT_SYMBOL_GPL(irq_do_set_affinity);
245299
246300 #ifdef CONFIG_GENERIC_PENDING_IRQ
247301 static inline int irq_set_affinity_pending(struct irq_data *data,
....@@ -288,7 +342,7 @@
288342 * If the interrupt is not yet activated, just store the affinity
289343 * mask and do not call the chip driver at all. On activation the
290344 * driver has to make sure anyway that the interrupt is in a
291
- * useable state so startup works.
345
+ * usable state so startup works.
292346 */
293347 if (!IS_ENABLED(CONFIG_IRQ_DOMAIN_HIERARCHY) ||
294348 irqd_is_activated(data) || !irqd_affinity_on_activate(data))
....@@ -347,7 +401,6 @@
347401 raw_spin_unlock_irqrestore(&desc->lock, flags);
348402 return ret;
349403 }
350
-EXPORT_SYMBOL_GPL(__irq_set_affinity);
351404
352405 int irq_set_affinity_hint(unsigned int irq, const struct cpumask *m)
353406 {
....@@ -411,7 +464,7 @@
411464 /* The release function is promised process context */
412465 might_sleep();
413466
414
- if (!desc)
467
+ if (!desc || desc->istate & IRQS_NMI)
415468 return -EINVAL;
416469
417470 /* Complete initialisation of *notify */
....@@ -614,6 +667,21 @@
614667 }
615668 EXPORT_SYMBOL_GPL(disable_hardirq);
616669
670
+/**
671
+ * disable_nmi_nosync - disable an nmi without waiting
672
+ * @irq: Interrupt to disable
673
+ *
674
+ * Disable the selected interrupt line. Disables and enables are
675
+ * nested.
676
+ * The interrupt to disable must have been requested through request_nmi.
677
+ * Unlike disable_nmi(), this function does not ensure existing
678
+ * instances of the IRQ handler have completed before returning.
679
+ */
680
+void disable_nmi_nosync(unsigned int irq)
681
+{
682
+ disable_irq_nosync(irq);
683
+}
684
+
617685 void __enable_irq(struct irq_desc *desc)
618686 {
619687 switch (desc->depth) {
....@@ -670,6 +738,20 @@
670738 }
671739 EXPORT_SYMBOL(enable_irq);
672740
741
+/**
742
+ * enable_nmi - enable handling of an nmi
743
+ * @irq: Interrupt to enable
744
+ *
745
+ * The interrupt to enable must have been requested through request_nmi.
746
+ * Undoes the effect of one call to disable_nmi(). If this
747
+ * matches the last disable, processing of interrupts on this
748
+ * IRQ line is re-enabled.
749
+ */
750
+void enable_nmi(unsigned int irq)
751
+{
752
+ enable_irq(irq);
753
+}
754
+
673755 static int set_irq_wake_real(unsigned int irq, unsigned int on)
674756 {
675757 struct irq_desc *desc = irq_to_desc(irq);
....@@ -695,6 +777,13 @@
695777 *
696778 * Wakeup mode lets this IRQ wake the system from sleep
697779 * states like "suspend to RAM".
780
+ *
781
+ * Note: irq enable/disable state is completely orthogonal
782
+ * to the enable/disable state of irq wake. An irq can be
783
+ * disabled with disable_irq() and still wake the system as
784
+ * long as the irq has wake enabled. If this does not hold,
785
+ * then the underlying irq chip and the related driver need
786
+ * to be investigated.
698787 */
699788 int irq_set_irq_wake(unsigned int irq, unsigned int on)
700789 {
....@@ -704,6 +793,12 @@
704793
705794 if (!desc)
706795 return -EINVAL;
796
+
797
+ /* Don't use NMIs as wake up interrupts please */
798
+ if (desc->istate & IRQS_NMI) {
799
+ ret = -EINVAL;
800
+ goto out_unlock;
801
+ }
707802
708803 /* wakeup-capable irqs can be shared between drivers that
709804 * don't need to have the same sleep mode behaviors.
....@@ -727,6 +822,8 @@
727822 irqd_clear(&desc->irq_data, IRQD_WAKEUP_STATE);
728823 }
729824 }
825
+
826
+out_unlock:
730827 irq_put_desc_busunlock(desc, flags);
731828 return ret;
732829 }
....@@ -787,6 +884,7 @@
787884 case IRQ_SET_MASK_OK_DONE:
788885 irqd_clear(&desc->irq_data, IRQD_TRIGGER_MASK);
789886 irqd_set(&desc->irq_data, flags);
887
+ fallthrough;
790888
791889 case IRQ_SET_MASK_OK_NOCOPY:
792890 flags = irqd_get_trigger_type(&desc->irq_data);
....@@ -801,7 +899,7 @@
801899 ret = 0;
802900 break;
803901 default:
804
- pr_err("Setting trigger mode %lu for irq %u failed (%pF)\n",
902
+ pr_err("Setting trigger mode %lu for irq %u failed (%pS)\n",
805903 flags, irq_desc_get_irq(desc), chip->irq_set_type);
806904 }
807905 if (unmask)
....@@ -902,7 +1000,7 @@
9021000 * to IRQS_INPROGRESS and the irq line is masked forever.
9031001 *
9041002 * This also serializes the state of shared oneshot handlers
905
- * versus "desc->threads_onehsot |= action->thread_mask;" in
1003
+ * versus "desc->threads_oneshot |= action->thread_mask;" in
9061004 * irq_wake_thread(). See the comment there which explains the
9071005 * serialization.
9081006 */
....@@ -979,7 +1077,7 @@
9791077 #endif
9801078
9811079 /*
982
- * Interrupts which are not explicitely requested as threaded
1080
+ * Interrupts which are not explicitly requested as threaded
9831081 * interrupts rely on the implicit bh/preempt disable of the hard irq
9841082 * context. So we need to disable bh here to avoid deadlocks and other
9851083 * side effects.
....@@ -990,14 +1088,14 @@
9901088 irqreturn_t ret;
9911089
9921090 local_bh_disable();
993
- if (!IS_ENABLED(CONFIG_PREEMPT_RT_BASE))
1091
+ if (!IS_ENABLED(CONFIG_PREEMPT_RT))
9941092 local_irq_disable();
9951093 ret = action->thread_fn(action->irq, action->dev_id);
9961094 if (ret == IRQ_HANDLED)
9971095 atomic_inc(&desc->threads_handled);
9981096
9991097 irq_finalize_oneshot(desc, action);
1000
- if (!IS_ENABLED(CONFIG_PREEMPT_RT_BASE))
1098
+ if (!IS_ENABLED(CONFIG_PREEMPT_RT))
10011099 local_irq_enable();
10021100 local_bh_enable();
10031101 return ret;
....@@ -1067,6 +1165,31 @@
10671165 }
10681166
10691167 /*
1168
+ * Internal function to notify that a interrupt thread is ready.
1169
+ */
1170
+static void irq_thread_set_ready(struct irq_desc *desc,
1171
+ struct irqaction *action)
1172
+{
1173
+ set_bit(IRQTF_READY, &action->thread_flags);
1174
+ wake_up(&desc->wait_for_threads);
1175
+}
1176
+
1177
+/*
1178
+ * Internal function to wake up a interrupt thread and wait until it is
1179
+ * ready.
1180
+ */
1181
+static void wake_up_and_wait_for_irq_thread_ready(struct irq_desc *desc,
1182
+ struct irqaction *action)
1183
+{
1184
+ if (!action || !action->thread)
1185
+ return;
1186
+
1187
+ wake_up_process(action->thread);
1188
+ wait_event(desc->wait_for_threads,
1189
+ test_bit(IRQTF_READY, &action->thread_flags));
1190
+}
1191
+
1192
+/*
10701193 * Interrupt handler thread
10711194 */
10721195 static int irq_thread(void *data)
....@@ -1077,6 +1200,8 @@
10771200 irqreturn_t (*handler_fn)(struct irq_desc *desc,
10781201 struct irqaction *action);
10791202
1203
+ irq_thread_set_ready(desc, action);
1204
+
10801205 if (force_irqthreads && test_bit(IRQTF_FORCED_THREAD,
10811206 &action->thread_flags))
10821207 handler_fn = irq_forced_thread_fn;
....@@ -1084,7 +1209,7 @@
10841209 handler_fn = irq_thread_fn;
10851210
10861211 init_task_work(&on_exit_work, irq_thread_dtor);
1087
- task_work_add(current, &on_exit_work, false);
1212
+ task_work_add(current, &on_exit_work, TWA_NONE);
10881213
10891214 irq_thread_check_affinity(desc, action);
10901215
....@@ -1097,12 +1222,6 @@
10971222 if (action_ret == IRQ_WAKE_THREAD)
10981223 irq_wake_secondary(desc, action);
10991224
1100
-#ifdef CONFIG_PREEMPT_RT_FULL
1101
- migrate_disable();
1102
- add_interrupt_randomness(action->irq, 0,
1103
- desc->random_ip ^ (unsigned long) action);
1104
- migrate_enable();
1105
-#endif
11061225 wake_threads_waitq(desc);
11071226 }
11081227
....@@ -1199,13 +1318,43 @@
11991318 c->irq_release_resources(d);
12001319 }
12011320
1321
+static bool irq_supports_nmi(struct irq_desc *desc)
1322
+{
1323
+ struct irq_data *d = irq_desc_get_irq_data(desc);
1324
+
1325
+#ifdef CONFIG_IRQ_DOMAIN_HIERARCHY
1326
+ /* Only IRQs directly managed by the root irqchip can be set as NMI */
1327
+ if (d->parent_data)
1328
+ return false;
1329
+#endif
1330
+ /* Don't support NMIs for chips behind a slow bus */
1331
+ if (d->chip->irq_bus_lock || d->chip->irq_bus_sync_unlock)
1332
+ return false;
1333
+
1334
+ return d->chip->flags & IRQCHIP_SUPPORTS_NMI;
1335
+}
1336
+
1337
+static int irq_nmi_setup(struct irq_desc *desc)
1338
+{
1339
+ struct irq_data *d = irq_desc_get_irq_data(desc);
1340
+ struct irq_chip *c = d->chip;
1341
+
1342
+ return c->irq_nmi_setup ? c->irq_nmi_setup(d) : -EINVAL;
1343
+}
1344
+
1345
+static void irq_nmi_teardown(struct irq_desc *desc)
1346
+{
1347
+ struct irq_data *d = irq_desc_get_irq_data(desc);
1348
+ struct irq_chip *c = d->chip;
1349
+
1350
+ if (c->irq_nmi_teardown)
1351
+ c->irq_nmi_teardown(d);
1352
+}
1353
+
12021354 static int
12031355 setup_irq_thread(struct irqaction *new, unsigned int irq, bool secondary)
12041356 {
12051357 struct task_struct *t;
1206
- struct sched_param param = {
1207
- .sched_priority = MAX_USER_RT_PRIO/2,
1208
- };
12091358
12101359 if (!secondary) {
12111360 t = kthread_create(irq_thread, new, "irq/%d-%s", irq,
....@@ -1213,21 +1362,19 @@
12131362 } else {
12141363 t = kthread_create(irq_thread, new, "irq/%d-s-%s", irq,
12151364 new->name);
1216
- param.sched_priority -= 1;
12171365 }
12181366
12191367 if (IS_ERR(t))
12201368 return PTR_ERR(t);
12211369
1222
- sched_setscheduler_nocheck(t, SCHED_FIFO, &param);
1370
+ sched_set_fifo(t);
12231371
12241372 /*
12251373 * We keep the reference to the task struct even if
12261374 * the thread dies to avoid that the interrupt code
12271375 * references an already freed task_struct.
12281376 */
1229
- get_task_struct(t);
1230
- new->thread = t;
1377
+ new->thread = get_task_struct(t);
12311378 /*
12321379 * Tell the thread to set its affinity. This is
12331380 * important for shared interrupt handlers as we do
....@@ -1373,8 +1520,16 @@
13731520 * fields must have IRQF_SHARED set and the bits which
13741521 * set the trigger type must match. Also all must
13751522 * agree on ONESHOT.
1523
+ * Interrupt lines used for NMIs cannot be shared.
13761524 */
13771525 unsigned int oldtype;
1526
+
1527
+ if (desc->istate & IRQS_NMI) {
1528
+ pr_err("Invalid attempt to share NMI for %s (irq %d) on irqchip %s.\n",
1529
+ new->name, irq, desc->irq_data.chip->name);
1530
+ ret = -EINVAL;
1531
+ goto out_unlock;
1532
+ }
13781533
13791534 /*
13801535 * If nobody did set the configuration before, inherit
....@@ -1464,15 +1619,13 @@
14641619 * has. The type flags are unreliable as the
14651620 * underlying chip implementation can override them.
14661621 */
1467
- pr_err("Threaded irq requested with handler=NULL and !ONESHOT for irq %d\n",
1468
- irq);
1622
+ pr_err("Threaded irq requested with handler=NULL and !ONESHOT for %s (irq %d)\n",
1623
+ new->name, irq);
14691624 ret = -EINVAL;
14701625 goto out_unlock;
14711626 }
14721627
14731628 if (!shared) {
1474
- init_waitqueue_head(&desc->wait_for_threads);
1475
-
14761629 /* Setup the type (level, edge polarity) if configured: */
14771630 if (new->flags & IRQF_TRIGGER_MASK) {
14781631 ret = __irq_set_trigger(desc,
....@@ -1515,10 +1668,8 @@
15151668 irqd_set(&desc->irq_data, IRQD_NO_BALANCING);
15161669 }
15171670
1518
- if (new->flags & IRQF_NO_SOFTIRQ_CALL)
1519
- irq_settings_set_no_softirq_call(desc);
1520
-
1521
- if (irq_settings_can_autoenable(desc)) {
1671
+ if (!(new->flags & IRQF_NO_AUTOEN) &&
1672
+ irq_settings_can_autoenable(desc)) {
15221673 irq_startup(desc, IRQ_RESEND, IRQ_START_COND);
15231674 } else {
15241675 /*
....@@ -1565,14 +1716,8 @@
15651716
15661717 irq_setup_timings(desc, new);
15671718
1568
- /*
1569
- * Strictly no need to wake it up, but hung_task complains
1570
- * when no hard interrupt wakes the thread up.
1571
- */
1572
- if (new->thread)
1573
- wake_up_process(new->thread);
1574
- if (new->secondary)
1575
- wake_up_process(new->secondary->thread);
1719
+ wake_up_and_wait_for_irq_thread_ready(desc, new);
1720
+ wake_up_and_wait_for_irq_thread_ready(desc, new->secondary);
15761721
15771722 register_irq_proc(irq, desc);
15781723 new->dir = NULL;
....@@ -1617,34 +1762,6 @@
16171762 module_put(desc->owner);
16181763 return ret;
16191764 }
1620
-
1621
-/**
1622
- * setup_irq - setup an interrupt
1623
- * @irq: Interrupt line to setup
1624
- * @act: irqaction for the interrupt
1625
- *
1626
- * Used to statically setup interrupts in the early boot process.
1627
- */
1628
-int setup_irq(unsigned int irq, struct irqaction *act)
1629
-{
1630
- int retval;
1631
- struct irq_desc *desc = irq_to_desc(irq);
1632
-
1633
- if (!desc || WARN_ON(irq_settings_is_per_cpu_devid(desc)))
1634
- return -EINVAL;
1635
-
1636
- retval = irq_chip_pm_get(&desc->irq_data);
1637
- if (retval < 0)
1638
- return retval;
1639
-
1640
- retval = __setup_irq(irq, desc, act);
1641
-
1642
- if (retval)
1643
- irq_chip_pm_put(&desc->irq_data);
1644
-
1645
- return retval;
1646
-}
1647
-EXPORT_SYMBOL_GPL(setup_irq);
16481765
16491766 /*
16501767 * Internal function to unregister an irqaction - used to free
....@@ -1761,7 +1878,7 @@
17611878 /* Last action releases resources */
17621879 if (!desc->action) {
17631880 /*
1764
- * Reaquire bus lock as irq_release_resources() might
1881
+ * Reacquire bus lock as irq_release_resources() might
17651882 * require it to deallocate resources over the slow bus.
17661883 */
17671884 chip_bus_lock(desc);
....@@ -1785,22 +1902,6 @@
17851902 kfree(action->secondary);
17861903 return action;
17871904 }
1788
-
1789
-/**
1790
- * remove_irq - free an interrupt
1791
- * @irq: Interrupt line to free
1792
- * @act: irqaction for the interrupt
1793
- *
1794
- * Used to remove interrupts statically setup by the early boot process.
1795
- */
1796
-void remove_irq(unsigned int irq, struct irqaction *act)
1797
-{
1798
- struct irq_desc *desc = irq_to_desc(irq);
1799
-
1800
- if (desc && !WARN_ON(irq_settings_is_per_cpu_devid(desc)))
1801
- __free_irq(desc, act->dev_id);
1802
-}
1803
-EXPORT_SYMBOL_GPL(remove_irq);
18041905
18051906 /**
18061907 * free_irq - free an interrupt allocated with request_irq
....@@ -1842,6 +1943,59 @@
18421943 return devname;
18431944 }
18441945 EXPORT_SYMBOL(free_irq);
1946
+
1947
+/* This function must be called with desc->lock held */
1948
+static const void *__cleanup_nmi(unsigned int irq, struct irq_desc *desc)
1949
+{
1950
+ const char *devname = NULL;
1951
+
1952
+ desc->istate &= ~IRQS_NMI;
1953
+
1954
+ if (!WARN_ON(desc->action == NULL)) {
1955
+ irq_pm_remove_action(desc, desc->action);
1956
+ devname = desc->action->name;
1957
+ unregister_handler_proc(irq, desc->action);
1958
+
1959
+ kfree(desc->action);
1960
+ desc->action = NULL;
1961
+ }
1962
+
1963
+ irq_settings_clr_disable_unlazy(desc);
1964
+ irq_shutdown_and_deactivate(desc);
1965
+
1966
+ irq_release_resources(desc);
1967
+
1968
+ irq_chip_pm_put(&desc->irq_data);
1969
+ module_put(desc->owner);
1970
+
1971
+ return devname;
1972
+}
1973
+
1974
+const void *free_nmi(unsigned int irq, void *dev_id)
1975
+{
1976
+ struct irq_desc *desc = irq_to_desc(irq);
1977
+ unsigned long flags;
1978
+ const void *devname;
1979
+
1980
+ if (!desc || WARN_ON(!(desc->istate & IRQS_NMI)))
1981
+ return NULL;
1982
+
1983
+ if (WARN_ON(irq_settings_is_per_cpu_devid(desc)))
1984
+ return NULL;
1985
+
1986
+ /* NMI still enabled */
1987
+ if (WARN_ON(desc->depth == 0))
1988
+ disable_nmi_nosync(irq);
1989
+
1990
+ raw_spin_lock_irqsave(&desc->lock, flags);
1991
+
1992
+ irq_nmi_teardown(desc);
1993
+ devname = __cleanup_nmi(irq, desc);
1994
+
1995
+ raw_spin_unlock_irqrestore(&desc->lock, flags);
1996
+
1997
+ return devname;
1998
+}
18451999
18462000 /**
18472001 * request_threaded_irq - allocate an interrupt line
....@@ -1902,10 +2056,15 @@
19022056 * which interrupt is which (messes up the interrupt freeing
19032057 * logic etc).
19042058 *
2059
+ * Also shared interrupts do not go well with disabling auto enable.
2060
+ * The sharing interrupt might request it while it's still disabled
2061
+ * and then wait for interrupts forever.
2062
+ *
19052063 * Also IRQF_COND_SUSPEND only makes sense for shared interrupts and
19062064 * it cannot be set along with IRQF_NO_SUSPEND.
19072065 */
19082066 if (((irqflags & IRQF_SHARED) && !dev_id) ||
2067
+ ((irqflags & IRQF_SHARED) && (irqflags & IRQF_NO_AUTOEN)) ||
19092068 (!(irqflags & IRQF_SHARED) && (irqflags & IRQF_COND_SUSPEND)) ||
19102069 ((irqflags & IRQF_NO_SUSPEND) && (irqflags & IRQF_COND_SUSPEND)))
19112070 return -EINVAL;
....@@ -2012,6 +2171,102 @@
20122171 }
20132172 EXPORT_SYMBOL_GPL(request_any_context_irq);
20142173
2174
+/**
2175
+ * request_nmi - allocate an interrupt line for NMI delivery
2176
+ * @irq: Interrupt line to allocate
2177
+ * @handler: Function to be called when the IRQ occurs.
2178
+ * Threaded handler for threaded interrupts.
2179
+ * @irqflags: Interrupt type flags
2180
+ * @name: An ascii name for the claiming device
2181
+ * @dev_id: A cookie passed back to the handler function
2182
+ *
2183
+ * This call allocates interrupt resources and enables the
2184
+ * interrupt line and IRQ handling. It sets up the IRQ line
2185
+ * to be handled as an NMI.
2186
+ *
2187
+ * An interrupt line delivering NMIs cannot be shared and IRQ handling
2188
+ * cannot be threaded.
2189
+ *
2190
+ * Interrupt lines requested for NMI delivering must produce per cpu
2191
+ * interrupts and have auto enabling setting disabled.
2192
+ *
2193
+ * Dev_id must be globally unique. Normally the address of the
2194
+ * device data structure is used as the cookie. Since the handler
2195
+ * receives this value it makes sense to use it.
2196
+ *
2197
+ * If the interrupt line cannot be used to deliver NMIs, function
2198
+ * will fail and return a negative value.
2199
+ */
2200
+int request_nmi(unsigned int irq, irq_handler_t handler,
2201
+ unsigned long irqflags, const char *name, void *dev_id)
2202
+{
2203
+ struct irqaction *action;
2204
+ struct irq_desc *desc;
2205
+ unsigned long flags;
2206
+ int retval;
2207
+
2208
+ if (irq == IRQ_NOTCONNECTED)
2209
+ return -ENOTCONN;
2210
+
2211
+ /* NMI cannot be shared, used for Polling */
2212
+ if (irqflags & (IRQF_SHARED | IRQF_COND_SUSPEND | IRQF_IRQPOLL))
2213
+ return -EINVAL;
2214
+
2215
+ if (!(irqflags & IRQF_PERCPU))
2216
+ return -EINVAL;
2217
+
2218
+ if (!handler)
2219
+ return -EINVAL;
2220
+
2221
+ desc = irq_to_desc(irq);
2222
+
2223
+ if (!desc || (irq_settings_can_autoenable(desc) &&
2224
+ !(irqflags & IRQF_NO_AUTOEN)) ||
2225
+ !irq_settings_can_request(desc) ||
2226
+ WARN_ON(irq_settings_is_per_cpu_devid(desc)) ||
2227
+ !irq_supports_nmi(desc))
2228
+ return -EINVAL;
2229
+
2230
+ action = kzalloc(sizeof(struct irqaction), GFP_KERNEL);
2231
+ if (!action)
2232
+ return -ENOMEM;
2233
+
2234
+ action->handler = handler;
2235
+ action->flags = irqflags | IRQF_NO_THREAD | IRQF_NOBALANCING;
2236
+ action->name = name;
2237
+ action->dev_id = dev_id;
2238
+
2239
+ retval = irq_chip_pm_get(&desc->irq_data);
2240
+ if (retval < 0)
2241
+ goto err_out;
2242
+
2243
+ retval = __setup_irq(irq, desc, action);
2244
+ if (retval)
2245
+ goto err_irq_setup;
2246
+
2247
+ raw_spin_lock_irqsave(&desc->lock, flags);
2248
+
2249
+ /* Setup NMI state */
2250
+ desc->istate |= IRQS_NMI;
2251
+ retval = irq_nmi_setup(desc);
2252
+ if (retval) {
2253
+ __cleanup_nmi(irq, desc);
2254
+ raw_spin_unlock_irqrestore(&desc->lock, flags);
2255
+ return -EINVAL;
2256
+ }
2257
+
2258
+ raw_spin_unlock_irqrestore(&desc->lock, flags);
2259
+
2260
+ return 0;
2261
+
2262
+err_irq_setup:
2263
+ irq_chip_pm_put(&desc->irq_data);
2264
+err_out:
2265
+ kfree(action);
2266
+
2267
+ return retval;
2268
+}
2269
+
20152270 void enable_percpu_irq(unsigned int irq, unsigned int type)
20162271 {
20172272 unsigned int cpu = smp_processor_id();
....@@ -2045,6 +2300,11 @@
20452300 irq_put_desc_unlock(desc, flags);
20462301 }
20472302 EXPORT_SYMBOL_GPL(enable_percpu_irq);
2303
+
2304
+void enable_percpu_nmi(unsigned int irq, unsigned int type)
2305
+{
2306
+ enable_percpu_irq(irq, type);
2307
+}
20482308
20492309 /**
20502310 * irq_percpu_is_enabled - Check whether the per cpu irq is enabled
....@@ -2085,6 +2345,11 @@
20852345 }
20862346 EXPORT_SYMBOL_GPL(disable_percpu_irq);
20872347
2348
+void disable_percpu_nmi(unsigned int irq)
2349
+{
2350
+ disable_percpu_irq(irq);
2351
+}
2352
+
20882353 /*
20892354 * Internal function to unregister a percpu irqaction.
20902355 */
....@@ -2115,6 +2380,8 @@
21152380
21162381 /* Found it - now remove it from the list of entries: */
21172382 desc->action = NULL;
2383
+
2384
+ desc->istate &= ~IRQS_NMI;
21182385
21192386 raw_spin_unlock_irqrestore(&desc->lock, flags);
21202387
....@@ -2168,6 +2435,19 @@
21682435 chip_bus_sync_unlock(desc);
21692436 }
21702437 EXPORT_SYMBOL_GPL(free_percpu_irq);
2438
+
2439
+void free_percpu_nmi(unsigned int irq, void __percpu *dev_id)
2440
+{
2441
+ struct irq_desc *desc = irq_to_desc(irq);
2442
+
2443
+ if (!desc || !irq_settings_is_per_cpu_devid(desc))
2444
+ return;
2445
+
2446
+ if (WARN_ON(!(desc->istate & IRQS_NMI)))
2447
+ return;
2448
+
2449
+ kfree(__free_percpu_irq(irq, dev_id));
2450
+}
21712451
21722452 /**
21732453 * setup_percpu_irq - setup a per-cpu interrupt
....@@ -2258,6 +2538,158 @@
22582538 }
22592539 EXPORT_SYMBOL_GPL(__request_percpu_irq);
22602540
2541
+/**
2542
+ * request_percpu_nmi - allocate a percpu interrupt line for NMI delivery
2543
+ * @irq: Interrupt line to allocate
2544
+ * @handler: Function to be called when the IRQ occurs.
2545
+ * @name: An ascii name for the claiming device
2546
+ * @dev_id: A percpu cookie passed back to the handler function
2547
+ *
2548
+ * This call allocates interrupt resources for a per CPU NMI. Per CPU NMIs
2549
+ * have to be setup on each CPU by calling prepare_percpu_nmi() before
2550
+ * being enabled on the same CPU by using enable_percpu_nmi().
2551
+ *
2552
+ * Dev_id must be globally unique. It is a per-cpu variable, and
2553
+ * the handler gets called with the interrupted CPU's instance of
2554
+ * that variable.
2555
+ *
2556
+ * Interrupt lines requested for NMI delivering should have auto enabling
2557
+ * setting disabled.
2558
+ *
2559
+ * If the interrupt line cannot be used to deliver NMIs, function
2560
+ * will fail returning a negative value.
2561
+ */
2562
+int request_percpu_nmi(unsigned int irq, irq_handler_t handler,
2563
+ const char *name, void __percpu *dev_id)
2564
+{
2565
+ struct irqaction *action;
2566
+ struct irq_desc *desc;
2567
+ unsigned long flags;
2568
+ int retval;
2569
+
2570
+ if (!handler)
2571
+ return -EINVAL;
2572
+
2573
+ desc = irq_to_desc(irq);
2574
+
2575
+ if (!desc || !irq_settings_can_request(desc) ||
2576
+ !irq_settings_is_per_cpu_devid(desc) ||
2577
+ irq_settings_can_autoenable(desc) ||
2578
+ !irq_supports_nmi(desc))
2579
+ return -EINVAL;
2580
+
2581
+ /* The line cannot already be NMI */
2582
+ if (desc->istate & IRQS_NMI)
2583
+ return -EINVAL;
2584
+
2585
+ action = kzalloc(sizeof(struct irqaction), GFP_KERNEL);
2586
+ if (!action)
2587
+ return -ENOMEM;
2588
+
2589
+ action->handler = handler;
2590
+ action->flags = IRQF_PERCPU | IRQF_NO_SUSPEND | IRQF_NO_THREAD
2591
+ | IRQF_NOBALANCING;
2592
+ action->name = name;
2593
+ action->percpu_dev_id = dev_id;
2594
+
2595
+ retval = irq_chip_pm_get(&desc->irq_data);
2596
+ if (retval < 0)
2597
+ goto err_out;
2598
+
2599
+ retval = __setup_irq(irq, desc, action);
2600
+ if (retval)
2601
+ goto err_irq_setup;
2602
+
2603
+ raw_spin_lock_irqsave(&desc->lock, flags);
2604
+ desc->istate |= IRQS_NMI;
2605
+ raw_spin_unlock_irqrestore(&desc->lock, flags);
2606
+
2607
+ return 0;
2608
+
2609
+err_irq_setup:
2610
+ irq_chip_pm_put(&desc->irq_data);
2611
+err_out:
2612
+ kfree(action);
2613
+
2614
+ return retval;
2615
+}
2616
+
2617
+/**
2618
+ * prepare_percpu_nmi - performs CPU local setup for NMI delivery
2619
+ * @irq: Interrupt line to prepare for NMI delivery
2620
+ *
2621
+ * This call prepares an interrupt line to deliver NMI on the current CPU,
2622
+ * before that interrupt line gets enabled with enable_percpu_nmi().
2623
+ *
2624
+ * As a CPU local operation, this should be called from non-preemptible
2625
+ * context.
2626
+ *
2627
+ * If the interrupt line cannot be used to deliver NMIs, function
2628
+ * will fail returning a negative value.
2629
+ */
2630
+int prepare_percpu_nmi(unsigned int irq)
2631
+{
2632
+ unsigned long flags;
2633
+ struct irq_desc *desc;
2634
+ int ret = 0;
2635
+
2636
+ WARN_ON(preemptible());
2637
+
2638
+ desc = irq_get_desc_lock(irq, &flags,
2639
+ IRQ_GET_DESC_CHECK_PERCPU);
2640
+ if (!desc)
2641
+ return -EINVAL;
2642
+
2643
+ if (WARN(!(desc->istate & IRQS_NMI),
2644
+ KERN_ERR "prepare_percpu_nmi called for a non-NMI interrupt: irq %u\n",
2645
+ irq)) {
2646
+ ret = -EINVAL;
2647
+ goto out;
2648
+ }
2649
+
2650
+ ret = irq_nmi_setup(desc);
2651
+ if (ret) {
2652
+ pr_err("Failed to setup NMI delivery: irq %u\n", irq);
2653
+ goto out;
2654
+ }
2655
+
2656
+out:
2657
+ irq_put_desc_unlock(desc, flags);
2658
+ return ret;
2659
+}
2660
+
2661
+/**
2662
+ * teardown_percpu_nmi - undoes NMI setup of IRQ line
2663
+ * @irq: Interrupt line from which CPU local NMI configuration should be
2664
+ * removed
2665
+ *
2666
+ * This call undoes the setup done by prepare_percpu_nmi().
2667
+ *
2668
+ * IRQ line should not be enabled for the current CPU.
2669
+ *
2670
+ * As a CPU local operation, this should be called from non-preemptible
2671
+ * context.
2672
+ */
2673
+void teardown_percpu_nmi(unsigned int irq)
2674
+{
2675
+ unsigned long flags;
2676
+ struct irq_desc *desc;
2677
+
2678
+ WARN_ON(preemptible());
2679
+
2680
+ desc = irq_get_desc_lock(irq, &flags,
2681
+ IRQ_GET_DESC_CHECK_PERCPU);
2682
+ if (!desc)
2683
+ return;
2684
+
2685
+ if (WARN_ON(!(desc->istate & IRQS_NMI)))
2686
+ goto out;
2687
+
2688
+ irq_nmi_teardown(desc);
2689
+out:
2690
+ irq_put_desc_unlock(desc, flags);
2691
+}
2692
+
22612693 int __irq_get_irqchip_state(struct irq_data *data, enum irqchip_irq_state which,
22622694 bool *state)
22632695 {
....@@ -2266,6 +2698,8 @@
22662698
22672699 do {
22682700 chip = irq_data_get_irq_chip(data);
2701
+ if (WARN_ON_ONCE(!chip))
2702
+ return -ENODEV;
22692703 if (chip->irq_get_irqchip_state)
22702704 break;
22712705 #ifdef CONFIG_IRQ_DOMAIN_HIERARCHY
....@@ -2323,7 +2757,7 @@
23232757 * This call sets the internal irqchip state of an interrupt,
23242758 * depending on the value of @which.
23252759 *
2326
- * This function should be called with migration disabled if the
2760
+ * This function should be called with preemption disabled if the
23272761 * interrupt controller has per-cpu registers.
23282762 */
23292763 int irq_set_irqchip_state(unsigned int irq, enum irqchip_irq_state which,
....@@ -2343,6 +2777,10 @@
23432777
23442778 do {
23452779 chip = irq_data_get_irq_chip(data);
2780
+ if (WARN_ON_ONCE(!chip)) {
2781
+ err = -ENODEV;
2782
+ goto out_unlock;
2783
+ }
23462784 if (chip->irq_set_irqchip_state)
23472785 break;
23482786 #ifdef CONFIG_IRQ_DOMAIN_HIERARCHY
....@@ -2355,6 +2793,7 @@
23552793 if (data)
23562794 err = chip->irq_set_irqchip_state(data, which, val);
23572795
2796
+out_unlock:
23582797 irq_put_desc_busunlock(desc, flags);
23592798 return err;
23602799 }