二 注册中断
这部分我们以3sc2410下的watchdog的中断为例来讲解中断的注册及调用过程。
本文引用地址:http://www.eepw.com.cn/article/201611/317832.htm
drivers/char/watchdog/s3c2410_wdt.c:
static int s3c2410wdt_probe(struct platform_device *pdev)
{
……
ret = request_irq(res->start, s3c2410wdt_irq, 0, pdev->name, pdev);
……
}
在s3c2410wdt_probe函数中为watchdog注册了一个中断,中断号为IRQ_WDT,
#define IRQ_WDTS3C2410_IRQ(9)
中断处理函数是s3c2410wdt_irq。
我们来看request_irq是如何实现的:
kernel/irq/Manage.c:
/
*request_irq - allocate an interrupt line
*@irq: Interrupt line to allocate
*@handler: Function to be called when the IRQ occurs
*@irqflags: Interrupt type flags
*@devname: An ascii name for the claiming device
*@dev_id: A cookie passed back to the handler function
*
*This call allocates interrupt resources and enables the
*interrupt line and IRQ handling. From the point this
*call is made your handler function may be invoked. Since
*your handler function must clear any interrupt the board
*raises, you must take care both to initialise your hardware
*and to set up the interrupt handler in the right order.
*
*Dev_id must be globally unique. Normally the address of the
*device data structure is used as the cookie. Since the handler
*receives this value it makes sense to use it.
*
*If your interrupt is shared you must pass a non NULL dev_id
*as this is required when freeing the interrupt.
*
*Flags:
*
*IRQF_SHAREDInterrupt is shared
*IRQF_DISABLEDDisable local interrupts while processing
*IRQF_SAMPLE_RANDOMThe interrupt can be used for entropy
*
*/
int request_irq(unsigned int irq,
irqreturn_t (*handler)(int, void *, struct pt_regs *),
unsigned long irqflags, const char *devname, void *dev_id)
{
struct irqaction *action;
int retval;
#ifdef CONFIG_LOCKDEP
irqflags |= SA_INTERRUPT;
#endif
return -EINVAL;
if (irq_desc[irq].status & IRQ_NOREQUEST)
return -EINVAL;
if (!handler)
return -EINVAL;
action = kmalloc(sizeof(struct irqaction), GFP_ATOMIC);
if (!action)
return -ENOMEM;
action->handler = handler;
action->flags = irqflags;
cpus_clear(action->mask);
action->name = devname;
action->next = NULL;
action->dev_id = dev_id;
select_smp_affinity(irq);
retval = setup_irq(irq, action);
if (retval)
kfree(action);
return retval;
}
该函数的功能及参数含义在函数头有详细的说明,这里就不多介绍了,值得注意的是我们在申请中断之前,必须要去掉该中断的IRQ_NOREQUEST标记(系统初始化的时候赋了这个标记), 具体方法是调用set_irq_flags()函数。
接着看setup_irq
kernel/irq/Manage.c:
int setup_irq(unsigned int irq, struct irqaction *new)
{
struct irq_desc *desc = irq_desc + irq;
struct irqaction *old, p;
unsigned long flags;
int shared = 0;
if (irq >= NR_IRQS)
return -EINVAL;
if (desc->chip == &no_irq_chip)
return -ENOSYS;
if (new->flags & IRQF_SAMPLE_RANDOM) {
rand_initialize_irq(irq);
}
spin_lock_irqsave(&desc->lock, flags);
p = &desc->action;
old = *p;
if (old) {
if (!((old->flags & new->flags) & IRQF_SHARED) ||
((old->flags ^ new->flags) & IRQF_TRIGGER_MASK))
goto mismatch;
#if defined(CONFIG_IRQ_PER_CPU)
if ((old->flags & IRQF_PERCPU) !=
(new->flags & IRQF_PERCPU))
goto mismatch;
#endif
do {
p = &old->next;
old = *p;
} while (old);
shared = 1;
}
*p = new;
#if defined(CONFIG_IRQ_PER_CPU)
if (new->flags & IRQF_PERCPU)
desc->status |= IRQ_PER_CPU;
#endif
if (!shared) {
irq_chip_set_defaults(desc->chip);
if (new->flags & IRQF_TRIGGER_MASK) {
if (desc->chip && desc->chip->set_type)
desc->chip->set_type(irq,
new->flags & IRQF_TRIGGER_MASK);
else
printk(KERN_WARNING "No IRQF_TRIGGER set_type "
"function for IRQ %d (%s)/n", irq,
desc->chip ? desc->chip->name :
"unknown");
} else
compat_irq_chip_set_default_handler(desc);
desc->status &= ~(IRQ_AUTODETECT | IRQ_WAITING |
IRQ_INPROGRESS);
if (!(desc->status & IRQ_NOAUTOEN)) {
desc->depth = 0;
desc->status &= ~IRQ_DISABLED;
if (desc->chip->startup)
desc->chip->startup(irq);
else
desc->chip->enable(irq);
} else
desc->depth = 1;
}
spin_unlock_irqrestore(&desc->lock, flags);
new->irq = irq;
register_irq_proc(irq);
new->dir = NULL;
register_handler_proc(irq, new);
return 0;
mismatch:
spin_unlock_irqrestore(&desc->lock, flags);
if (!(new->flags & IRQF_PROBE_SHARED)) {
printk(KERN_ERR "IRQ handler type mismatch for IRQ %d/n", irq);
dump_stack();
}
return -EBUSY;
}
该函数主要是安装了一个中断,并使能它。 我们先看使能函数,它是平台相关的,对于s3c2410的IRQ_WDT来说使用的是默认的使能函数(default_startup(),初始化赋值),如果我们要使用自己的使能函数,只要在chip对象里添加就行了。
kernel/irq/Chip.c:
static unsigned int default_startup(unsigned int irq)
{
irq_desc[irq].chip->enable(irq);
return 0;
}
对于IRQ_WDT来说调用的irq_desc[irq].chip->enable(irq)仍是默认函数:default_enable()
kernel/irq/Chip.c:
static void default_enable(unsigned int irq)
{
struct irq_desc *desc = irq_desc + irq;
desc->chip->unmask(irq);
desc->status &= ~IRQ_MASKED;
}
,对于IRQ_WDT来说这次的调用desc->chip->unmask(irq),实际上是s3c_irq_unmask,具体可查看前面分析的chip对象。
arch/arm/mach-s3c2410/Irq.c:
static void
s3c_irq_unmask(unsigned int irqno)
{
unsigned long mask;
if (irqno != IRQ_TIMER4 && irqno != IRQ_EINT8t23)
irqdbf2("s3c_irq_unmask %d/n", irqno);
irqno -= IRQ_EINT0;
mask = __raw_readl(S3C2410_INTMSK);
mask &= ~(1UL << irqno);
__raw_writel(mask, S3C2410_INTMSK);
}
对着s3c2410的data sheet一看就知道了, 就是打开相应中断。
至此中断处理函数安装好了,中断也打开了,系统就可以正确的响应中断了。
Ok,到此为止IRQ_WDT的中断注册过程已完成,此时的中断描述符如下所示: