^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) // SPDX-License-Identifier: GPL-2.0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) * Copyright (C) 1992, 1998-2004 Linus Torvalds, Ingo Molnar
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) * This file contains the interrupt probing code and driver APIs.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) #include <linux/irq.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) #include <linux/module.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) #include <linux/interrupt.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) #include <linux/delay.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) #include <linux/async.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) #include "internals.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) * Autodetection depends on the fact that any interrupt that
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) * comes in on to an unassigned handler will get stuck with
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) * "IRQS_WAITING" cleared and the interrupt disabled.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) static DEFINE_MUTEX(probing_active);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) * probe_irq_on - begin an interrupt autodetect
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) * Commence probing for an interrupt. The interrupts are scanned
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) * and a mask of potential interrupt lines is returned.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) unsigned long probe_irq_on(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) struct irq_desc *desc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) unsigned long mask = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) * quiesce the kernel, or at least the asynchronous portion
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) async_synchronize_full();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) mutex_lock(&probing_active);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) * something may have generated an irq long ago and we want to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) * flush such a longstanding irq before considering it as spurious.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) for_each_irq_desc_reverse(i, desc) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) raw_spin_lock_irq(&desc->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) if (!desc->action && irq_settings_can_probe(desc)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) * Some chips need to know about probing in
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) * progress:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) if (desc->irq_data.chip->irq_set_type)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) desc->irq_data.chip->irq_set_type(&desc->irq_data,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) IRQ_TYPE_PROBE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) irq_activate_and_startup(desc, IRQ_NORESEND);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) raw_spin_unlock_irq(&desc->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) /* Wait for longstanding interrupts to trigger. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) msleep(20);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) * enable any unassigned irqs
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) * (we must startup again here because if a longstanding irq
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) * happened in the previous stage, it may have masked itself)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) for_each_irq_desc_reverse(i, desc) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) raw_spin_lock_irq(&desc->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) if (!desc->action && irq_settings_can_probe(desc)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) desc->istate |= IRQS_AUTODETECT | IRQS_WAITING;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) if (irq_activate_and_startup(desc, IRQ_NORESEND))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) desc->istate |= IRQS_PENDING;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) raw_spin_unlock_irq(&desc->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) * Wait for spurious interrupts to trigger
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) msleep(100);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) * Now filter out any obviously spurious interrupts
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) for_each_irq_desc(i, desc) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) raw_spin_lock_irq(&desc->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) if (desc->istate & IRQS_AUTODETECT) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) /* It triggered already - consider it spurious. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) if (!(desc->istate & IRQS_WAITING)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92) desc->istate &= ~IRQS_AUTODETECT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) irq_shutdown_and_deactivate(desc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) } else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95) if (i < 32)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96) mask |= 1 << i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) raw_spin_unlock_irq(&desc->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) return mask;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) EXPORT_SYMBOL(probe_irq_on);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) * probe_irq_mask - scan a bitmap of interrupt lines
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) * @val: mask of interrupts to consider
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) * Scan the interrupt lines and return a bitmap of active
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) * autodetect interrupts. The interrupt probe logic state
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) * is then returned to its previous value.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) * Note: we need to scan all the irq's even though we will
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) * only return autodetect irq numbers - just so that we reset
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) * them all to a known state.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) unsigned int probe_irq_mask(unsigned long val)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) unsigned int mask = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) struct irq_desc *desc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) for_each_irq_desc(i, desc) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) raw_spin_lock_irq(&desc->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) if (desc->istate & IRQS_AUTODETECT) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) if (i < 16 && !(desc->istate & IRQS_WAITING))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) mask |= 1 << i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) desc->istate &= ~IRQS_AUTODETECT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) irq_shutdown_and_deactivate(desc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) raw_spin_unlock_irq(&desc->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) mutex_unlock(&probing_active);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) return mask & val;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) EXPORT_SYMBOL(probe_irq_mask);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) * probe_irq_off - end an interrupt autodetect
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) * @val: mask of potential interrupts (unused)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) * Scans the unused interrupt lines and returns the line which
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) * appears to have triggered the interrupt. If no interrupt was
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) * found then zero is returned. If more than one interrupt is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) * found then minus the first candidate is returned to indicate
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) * their is doubt.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) * The interrupt probe logic state is returned to its previous
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) * value.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) * BUGS: When used in a module (which arguably shouldn't happen)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) * nothing prevents two IRQ probe callers from overlapping. The
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) * results of this are non-optimal.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) int probe_irq_off(unsigned long val)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) int i, irq_found = 0, nr_of_irqs = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) struct irq_desc *desc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) for_each_irq_desc(i, desc) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) raw_spin_lock_irq(&desc->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) if (desc->istate & IRQS_AUTODETECT) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) if (!(desc->istate & IRQS_WAITING)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) if (!nr_of_irqs)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) irq_found = i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) nr_of_irqs++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) desc->istate &= ~IRQS_AUTODETECT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) irq_shutdown_and_deactivate(desc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) raw_spin_unlock_irq(&desc->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) mutex_unlock(&probing_active);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) if (nr_of_irqs > 1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) irq_found = -irq_found;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) return irq_found;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) EXPORT_SYMBOL(probe_irq_off);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184)