irq.c

来自「Linux Kernel 2.6.9 for OMAP1710」· C语言 代码 · 共 875 行 · 第 1/2 页

C
875
字号
/* *  linux/arch/arm/kernel/irq.c * *  Copyright (C) 1992 Linus Torvalds *  Modifications for ARM processor Copyright (C) 1995-2000 Russell King. * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License version 2 as * published by the Free Software Foundation. * *  This file contains the code used by various IRQ handling routines: *  asking for different IRQ's should be done through these routines *  instead of just grabbing them. Thus setups with different IRQ numbers *  shouldn't result in any weird surprises, and installing new handlers *  should be easier. * *  IRQ's are in fact implemented a bit like signal handlers for the kernel. *  Naturally it's not a 1:1 relation, but there are similarities. */#include <linux/config.h>#include <linux/kernel_stat.h>#include <linux/module.h>#include <linux/signal.h>#include <linux/ioport.h>#include <linux/interrupt.h>#include <linux/ptrace.h>#include <linux/slab.h>#include <linux/random.h>#include <linux/smp.h>#include <linux/init.h>#include <linux/seq_file.h>#include <linux/errno.h>#include <linux/list.h>#include <linux/kallsyms.h>#include <asm/irq.h>#include <asm/system.h>#include <asm/mach/irq.h>/* * Maximum IRQ count.  Currently, this is arbitary.  However, it should * not be set too low to prevent false triggering.  Conversely, if it * is set too high, then you could miss a stuck IRQ. * * Maybe we ought to set a timer and re-enable the IRQ at a later time? */#define MAX_IRQ_CNT	100000static int noirqdebug;static volatile unsigned long irq_err_count;static spinlock_t irq_controller_lock = SPIN_LOCK_UNLOCKED;static LIST_HEAD(irq_pending);struct irqdesc irq_desc[NR_IRQS];void (*init_arch_irq)(void) __initdata = NULL;/* * Dummy mask/unmask handler */void dummy_mask_unmask_irq(unsigned int irq){}irqreturn_t no_action(int irq, void *dev_id, struct pt_regs *regs){	return IRQ_NONE;}void do_bad_IRQ(unsigned int irq, struct irqdesc *desc, struct pt_regs *regs){	irq_err_count += 1;	printk(KERN_ERR "IRQ: spurious interrupt %d\n", irq);}static struct irqchip bad_chip = {	.ack	= dummy_mask_unmask_irq,	.mask	= dummy_mask_unmask_irq,	.unmask = dummy_mask_unmask_irq,};static struct irqdesc bad_irq_desc = {	.chip		= &bad_chip,	.handle		= do_bad_IRQ,	.pend		= LIST_HEAD_INIT(bad_irq_desc.pend),	.disable_depth	= 1,};/** *	disable_irq - disable an irq and wait for completion *	@irq: Interrupt to disable * *	Disable the selected interrupt line.  Enables and disables *	are nested.  We do this lazily. * *	This function may be called from IRQ context. */void disable_irq(unsigned int irq){	struct irqdesc *desc = irq_desc + irq;	unsigned long flags;	spin_lock_irqsave(&irq_controller_lock, flags);	desc->disable_depth++;	list_del_init(&desc->pend);	spin_unlock_irqrestore(&irq_controller_lock, flags);}EXPORT_SYMBOL(disable_irq);/** *	enable_irq - enable interrupt handling on an irq *	@irq: Interrupt to enable * *	Re-enables the processing of interrupts on this IRQ line. *	Note that this may call the interrupt handler, so you may *	get unexpected results if you hold IRQs disabled. * *	This function may be called from IRQ context. */void enable_irq(unsigned int irq){	struct irqdesc *desc = irq_desc + irq;	unsigned long flags;	spin_lock_irqsave(&irq_controller_lock, flags);	if (unlikely(!desc->disable_depth)) {		printk("enable_irq(%u) unbalanced from %p\n", irq,			__builtin_return_address(0));	} else if (!--desc->disable_depth) {		desc->probing = 0;		desc->chip->unmask(irq);		/*		 * If the interrupt is waiting to be processed,		 * try to re-run it.  We can't directly run it		 * from here since the caller might be in an		 * interrupt-protected region.		 */		if (desc->pending && list_empty(&desc->pend)) {			desc->pending = 0;			if (!desc->chip->retrigger ||			    desc->chip->retrigger(irq))				list_add(&desc->pend, &irq_pending);		}	}	spin_unlock_irqrestore(&irq_controller_lock, flags);}EXPORT_SYMBOL(enable_irq);/* * Enable wake on selected irq */void enable_irq_wake(unsigned int irq){	struct irqdesc *desc = irq_desc + irq;	unsigned long flags;	spin_lock_irqsave(&irq_controller_lock, flags);	if (desc->chip->wake)		desc->chip->wake(irq, 1);	spin_unlock_irqrestore(&irq_controller_lock, flags);}EXPORT_SYMBOL(enable_irq_wake);void disable_irq_wake(unsigned int irq){	struct irqdesc *desc = irq_desc + irq;	unsigned long flags;	spin_lock_irqsave(&irq_controller_lock, flags);	if (desc->chip->wake)		desc->chip->wake(irq, 0);	spin_unlock_irqrestore(&irq_controller_lock, flags);}EXPORT_SYMBOL(disable_irq_wake);int show_interrupts(struct seq_file *p, void *v){	int i = *(loff_t *) v;	struct irqaction * action;	unsigned long flags;	if (i < NR_IRQS) {		spin_lock_irqsave(&irq_controller_lock, flags);	    	action = irq_desc[i].action;		if (!action)			goto unlock;		seq_printf(p, "%3d: %10u ", i, kstat_irqs(i));		seq_printf(p, "  %s", action->name);		for (action = action->next; action; action = action->next)			seq_printf(p, ", %s", action->name);		seq_putc(p, '\n');unlock:		spin_unlock_irqrestore(&irq_controller_lock, flags);	} else if (i == NR_IRQS) {#ifdef CONFIG_ARCH_ACORN		show_fiq_list(p, v);#endif		seq_printf(p, "Err: %10lu\n", irq_err_count);	}	return 0;}/* * IRQ lock detection. * * Hopefully, this should get us out of a few locked situations. * However, it may take a while for this to happen, since we need * a large number if IRQs to appear in the same jiffie with the * same instruction pointer (or within 2 instructions). */static int check_irq_lock(struct irqdesc *desc, int irq, struct pt_regs *regs){	unsigned long instr_ptr = instruction_pointer(regs);	if (desc->lck_jif == jiffies &&	    desc->lck_pc >= instr_ptr && desc->lck_pc < instr_ptr + 8) {		desc->lck_cnt += 1;		if (desc->lck_cnt > MAX_IRQ_CNT) {			printk(KERN_ERR "IRQ LOCK: IRQ%d is locking the system, disabled\n", irq);			return 1;		}	} else {		desc->lck_cnt = 0;		desc->lck_pc  = instruction_pointer(regs);		desc->lck_jif = jiffies;	}	return 0;}static voidreport_bad_irq(unsigned int irq, struct pt_regs *regs, struct irqdesc *desc, int ret){	static int count = 100;	struct irqaction *action;	if (!count || noirqdebug)		return;	count--;	if (ret != IRQ_HANDLED && ret != IRQ_NONE) {		printk("irq%u: bogus retval mask %x\n", irq, ret);	} else {		printk("irq%u: nobody cared\n", irq);	}	show_regs(regs);	dump_stack();	printk(KERN_ERR "handlers:");	action = desc->action;	do {		printk("\n" KERN_ERR "[<%p>]", action->handler);		print_symbol(" (%s)", (unsigned long)action->handler);		action = action->next;	} while (action);	printk("\n");}static int__do_irq(unsigned int irq, struct irqaction *action, struct pt_regs *regs){	unsigned int status;	int ret, retval = 0;	spin_unlock(&irq_controller_lock);	if (!(action->flags & SA_INTERRUPT))		local_irq_enable();	status = 0;	do {		ret = action->handler(irq, action->dev_id, regs);		if (ret == IRQ_HANDLED)			status |= action->flags;		retval |= ret;		action = action->next;	} while (action);	if (status & SA_SAMPLE_RANDOM)		add_interrupt_randomness(irq);	spin_lock_irq(&irq_controller_lock);	return retval;}/* * This is for software-decoded IRQs.  The caller is expected to * handle the ack, clear, mask and unmask issues. */voiddo_simple_IRQ(unsigned int irq, struct irqdesc *desc, struct pt_regs *regs){	struct irqaction *action;	const int cpu = smp_processor_id();	desc->triggered = 1;	kstat_cpu(cpu).irqs[irq]++;	action = desc->action;	if (action) {		int ret = __do_irq(irq, action, regs);		if (ret != IRQ_HANDLED)			report_bad_irq(irq, regs, desc, ret);	}}/* * Most edge-triggered IRQ implementations seem to take a broken * approach to this.  Hence the complexity. */voiddo_edge_IRQ(unsigned int irq, struct irqdesc *desc, struct pt_regs *regs){	const int cpu = smp_processor_id();	desc->triggered = 1;	/*	 * If we're currently running this IRQ, or its disabled,	 * we shouldn't process the IRQ.  Instead, turn on the	 * hardware masks.	 */	if (unlikely(desc->running || desc->disable_depth))		goto running;	/*	 * Acknowledge and clear the IRQ, but don't mask it.	 */	desc->chip->ack(irq);	/*	 * Mark the IRQ currently in progress.	 */	desc->running = 1;	kstat_cpu(cpu).irqs[irq]++;	do {		struct irqaction *action;		int ret;		action = desc->action;		if (!action)			break;		if (desc->pending && !desc->disable_depth) {			desc->pending = 0;			desc->chip->unmask(irq);		}		ret = __do_irq(irq, action, regs);		if (ret != IRQ_HANDLED)			report_bad_irq(irq, regs, desc, ret);	} while (desc->pending && !desc->disable_depth);	desc->running = 0;	/*	 * If we were disabled or freed, shut down the handler.	 */	if (likely(desc->action && !check_irq_lock(desc, irq, regs)))		return; running:	/*	 * We got another IRQ while this one was masked or	 * currently running.  Delay it.	 */	desc->pending = 1;	desc->chip->mask(irq);	desc->chip->ack(irq);}/* * Level-based IRQ handler.  Nice and simple. */voiddo_level_IRQ(unsigned int irq, struct irqdesc *desc, struct pt_regs *regs){	struct irqaction *action;	const int cpu = smp_processor_id();	desc->triggered = 1;	/*	 * Acknowledge, clear _AND_ disable the interrupt.	 */	desc->chip->ack(irq);	if (likely(!desc->disable_depth)) {		kstat_cpu(cpu).irqs[irq]++;		/*		 * Return with this interrupt masked if no action		 */		action = desc->action;		if (action) {			int ret = __do_irq(irq, desc->action, regs);			if (ret != IRQ_HANDLED)				report_bad_irq(irq, regs, desc, ret);			if (likely(!desc->disable_depth &&				   !check_irq_lock(desc, irq, regs)))				desc->chip->unmask(irq);		}	}}static void do_pending_irqs(struct pt_regs *regs){	struct list_head head, *l, *n;	do {		struct irqdesc *desc;		/*		 * First, take the pending interrupts off the list.		 * The act of calling the handlers may add some IRQs		 * back onto the list.		 */		head = irq_pending;		INIT_LIST_HEAD(&irq_pending);		head.next->prev = &head;		head.prev->next = &head;		/*		 * Now run each entry.  We must delete it from our		 * list before calling the handler.		 */		list_for_each_safe(l, n, &head) {			desc = list_entry(l, struct irqdesc, pend);			list_del_init(&desc->pend);			desc->handle(desc - irq_desc, desc, regs);

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?