/*1* linux/kernel/irq/handle.c2*3* Copyright (C) 1992, 1998-2006 Linus Torvalds, Ingo Molnar4* Copyright (C) 2005-2006, Thomas Gleixner, Russell King5*6* This file contains the core interrupt handling code.7*8* Detailed information is available in Documentation/DocBook/genericirq9*10*/1112#include <linux/irq.h>13#include <linux/random.h>14#include <linux/sched.h>15#include <linux/interrupt.h>16#include <linux/kernel_stat.h>1718#include <trace/events/irq.h>1920#include "internals.h"2122/**23* handle_bad_irq - handle spurious and unhandled irqs24* @irq: the interrupt number25* @desc: description of the interrupt26*27* Handles spurious and unhandled IRQ's. It also prints a debugmessage.28*/29void handle_bad_irq(unsigned int irq, struct irq_desc *desc)30{31print_irq_desc(irq, desc);32kstat_incr_irqs_this_cpu(irq, desc);33ack_bad_irq(irq);34}3536/*37* Special, empty irq handler:38*/39irqreturn_t no_action(int cpl, void *dev_id)40{41return IRQ_NONE;42}4344static void warn_no_thread(unsigned int irq, struct irqaction *action)45{46if (test_and_set_bit(IRQTF_WARNED, &action->thread_flags))47return;4849printk(KERN_WARNING "IRQ %d device %s returned IRQ_WAKE_THREAD "50"but no thread function available.", irq, action->name);51}5253static void irq_wake_thread(struct irq_desc *desc, struct irqaction *action)54{55/*56* Wake up the handler thread for this action. In case the57* thread crashed and was killed we just pretend that we58* handled the interrupt. The hardirq handler has disabled the59* device interrupt, so no irq storm is lurking. If the60* RUNTHREAD bit is already set, nothing to do.61*/62if (test_bit(IRQTF_DIED, &action->thread_flags) ||63test_and_set_bit(IRQTF_RUNTHREAD, &action->thread_flags))64return;6566/*67* It's safe to OR the mask lockless here. We have only two68* places which write to threads_oneshot: This code and the69* irq thread.70*71* This code is the hard irq context and can never run on two72* cpus in parallel. If it ever does we have more serious73* problems than this bitmask.74*75* The irq threads of this irq which clear their "running" bit76* in threads_oneshot are serialized via desc->lock against77* each other and they are serialized against this code by78* IRQS_INPROGRESS.79*80* Hard irq handler:81*82* spin_lock(desc->lock);83* desc->state |= IRQS_INPROGRESS;84* spin_unlock(desc->lock);85* set_bit(IRQTF_RUNTHREAD, &action->thread_flags);86* desc->threads_oneshot |= mask;87* spin_lock(desc->lock);88* desc->state &= ~IRQS_INPROGRESS;89* spin_unlock(desc->lock);90*91* irq thread:92*93* again:94* spin_lock(desc->lock);95* if (desc->state & IRQS_INPROGRESS) {96* spin_unlock(desc->lock);97* while(desc->state & IRQS_INPROGRESS)98* cpu_relax();99* goto again;100* }101* if (!test_bit(IRQTF_RUNTHREAD, &action->thread_flags))102* desc->threads_oneshot &= ~mask;103* spin_unlock(desc->lock);104*105* So either the thread waits for us to clear IRQS_INPROGRESS106* or we are waiting in the flow handler for desc->lock to be107* released before we reach this point. The thread also checks108* IRQTF_RUNTHREAD under desc->lock. If set it leaves109* threads_oneshot untouched and runs the thread another time.110*/111desc->threads_oneshot |= action->thread_mask;112wake_up_process(action->thread);113}114115irqreturn_t116handle_irq_event_percpu(struct irq_desc *desc, struct irqaction *action)117{118irqreturn_t retval = IRQ_NONE;119unsigned int random = 0, irq = desc->irq_data.irq;120121do {122irqreturn_t res;123124trace_irq_handler_entry(irq, action);125res = action->handler(irq, action->dev_id);126trace_irq_handler_exit(irq, action, res);127128if (WARN_ONCE(!irqs_disabled(),"irq %u handler %pF enabled interrupts\n",129irq, action->handler))130local_irq_disable();131132switch (res) {133case IRQ_WAKE_THREAD:134/*135* Catch drivers which return WAKE_THREAD but136* did not set up a thread function137*/138if (unlikely(!action->thread_fn)) {139warn_no_thread(irq, action);140break;141}142143irq_wake_thread(desc, action);144145/* Fall through to add to randomness */146case IRQ_HANDLED:147random |= action->flags;148break;149150default:151break;152}153154retval |= res;155action = action->next;156} while (action);157158if (random & IRQF_SAMPLE_RANDOM)159add_interrupt_randomness(irq);160161if (!noirqdebug)162note_interrupt(irq, desc, retval);163return retval;164}165166irqreturn_t handle_irq_event(struct irq_desc *desc)167{168struct irqaction *action = desc->action;169irqreturn_t ret;170171desc->istate &= ~IRQS_PENDING;172irqd_set(&desc->irq_data, IRQD_IRQ_INPROGRESS);173raw_spin_unlock(&desc->lock);174175ret = handle_irq_event_percpu(desc, action);176177raw_spin_lock(&desc->lock);178irqd_clear(&desc->irq_data, IRQD_IRQ_INPROGRESS);179return ret;180}181182183