arm-linux注册中断

原创文章,转载请注明出处.转载自: Li Haifeng's Blog
本文链接地址: arm-linux注册中断

二 注册中断
这部分我们仍以3sc2410下 的watchdog的中断为例来讲解中断的注册及调用过程。
drivers/char/watchdog/s3c2410_wdt.c:


static int s3c2410wdt_probe(struct platform_device *pdev)
{
struct resource *res;
int started = 0;
int ret;
int size;

DBG("%s: probe=%pn", __FUNCTION__, pdev);

/* get the memory region for the watchdog timer */

res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
if (res == NULL) {
printk(KERN_INFO PFX "failed to get memory region resoucen");
return -ENOENT;
}

size = (res->end-res->start)+1;
wdt_mem = request_mem_region(res->start, size, pdev->name);
if (wdt_mem == NULL) {
printk(KERN_INFO PFX "failed to get memory regionn");
ret = -ENOENT;
goto err_req;
}

wdt_base = ioremap(res->start, size);
if (wdt_base == 0) {
printk(KERN_INFO PFX "failed to ioremap() regionn");
ret = -EINVAL;
goto err_req;
}

DBG("probe: mapped wdt_base=%pn", wdt_base);

res = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
if (res == NULL) {
printk(KERN_INFO PFX "failed to get irq resourcen");
ret = -ENOENT;
goto err_map;
}

ret = request_irq(res->start, s3c2410wdt_irq, 0, pdev->name, pdev);
if (ret != 0) {
printk(KERN_INFO PFX "failed to install irq (%d)n", ret);
goto err_map;
}

wdt_clock = clk_get(&pdev->dev, "watchdog");
if (IS_ERR(wdt_clock)) {
printk(KERN_INFO PFX "failed to find watchdog clock sourcen");
ret = PTR_ERR(wdt_clock);
goto err_irq;
}

clk_enable(wdt_clock);

/* see if we can actually set the requested timer margin, and if
* not, try the default value */

if (s3c2410wdt_set_heartbeat(tmr_margin)) {
started = s3c2410wdt_set_heartbeat(CONFIG_S3C2410_WATCHDOG_DEFAULT_TIME);

if (started == 0) {
printk(KERN_INFO PFX "tmr_margin value out of range, default %d usedn",
CONFIG_S3C2410_WATCHDOG_DEFAULT_TIME);
} else {
printk(KERN_INFO PFX "default timer value is out of range, cannot startn");
}
}

ret = misc_register(&s3c2410wdt_miscdev);
if (ret) {
printk (KERN_ERR PFX "cannot register miscdev on minor=%d (%d)n",
WATCHDOG_MINOR, ret);
goto err_clk;
}

if (tmr_atboot && started == 0) {
printk(KERN_INFO PFX "Starting Watchdog Timern");
s3c2410wdt_start();
} else if (!tmr_atboot) {
/* if we're not enabling the watchdog, then ensure it is
* disabled if it has been left running from the bootloader
* or other source */

s3c2410wdt_stop();
}

return 0;

err_clk:
clk_disable(wdt_clock);
clk_put(wdt_clock);

err_irq:
free_irq(wdt_irq->start, pdev);

err_map:
iounmap(wdt_base);

err_req:
release_resource(wdt_mem);
kfree(wdt_mem);

return ret;
}


在s3c2410wdt_probe函数中为watchdog注册了一个中断,中断号为IRQ_WDT,
#define IRQ_WDT S3C2410_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_SHARED Interrupt is shared
* IRQF_DISABLED Disable local interrupts while processing
* IRQF_SAMPLE_RANDOM The 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
/*
* Lockdep wants atomic interrupt handlers:
*/
irqflags |= IRQF_DISABLED;
#endif
/*
* Sanity-check: shared interrupts must pass in a real dev-ID,
* otherwise we'll have trouble later trying to figure out
* which interrupt is which (messes up the interrupt freeing
* logic etc).
*/
/*允许共享的中断必须要有一个独一无二的dev_id, 看上面函数的解释
if ((irqflags & IRQF_SHARED) && !dev_id)
return -EINVAL;
if (irq >= NR_IRQS) /*中断号是否合法*/
return -EINVAL;
/*这个标记对s3c2410来 说在s3c24xx_init_irq里通过调用set_irq_flags()被 去掉了*/
if (irq_desc[irq].status & IRQ_NOREQUEST)
return -EINVAL;
if (!handler) /*中断例程*/
return -EINVAL;

/*分配一个irqaction对 象,来保存这个中断信息*/
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:


/*
* Internal function to register an irqaction - typically used to
* allocate special interrupts that are part of the architecture.
*/
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;

/*对于s3c2410的 中断在s3c24xx_init_irq里已经初始化过了*/
if (desc->chip == &no_irq_chip)
return -ENOSYS;
/*
* Some drivers like serial.c use request_irq() heavily,
* so we have to be careful not to interfere with a
* running system.
*/
if (new->flags & IRQF_SAMPLE_RANDOM) {
/*
* This function might sleep, we want to call it first,
* outside of the atomic block.
* Yes, this might clear the entropy pool if the wrong
* driver is attempted to be loaded, without actually
* installing a new handler, but is this really a problem,
* only the sysadmin is able to do this.
*/
rand_initialize_irq(irq);
}

/*
* The following block of code has to be executed atomically
*/
/*
* 下面if代码段主要是查看该中断是否可以共享,如可以,则把中断例程链入中断例程list中, 至于中断共享的条件有: 1 触发方式相同, 2 都允许中断共享*/
spin_lock_irqsave(&desc->lock, flags);
p = &desc->action;
old = *p;
if (old) { /*对于IRQ_WDT,这个if不成立,但它已经设置了handle_irq 喔*/
/*
* Can't share interrupts unless both agree to and are
* the same type (level, edge, polarity). So both flag
* fields must have IRQF_SHARED set and the bits which
* set the trigger type must match.
*/
/*判断能否共享中断*/
if (!((old->flags & new->flags) & IRQF_SHARED) ||
((old->flags ^ new->flags) & IRQF_TRIGGER_MASK))
goto mismatch;

#if defined(CONFIG_IRQ_PER_CPU)
/* All handlers must agree on per-cpuness */
if ((old->flags & IRQF_PERCPU) !=
(new->flags & IRQF_PERCPU))
goto mismatch;
#endif
/* add new interrupt at end of irq queue */
/*把中断例程加入list中*/
do {
p = &old->next;
old = *p;
} while (old);
shared = 1;
}

*p = new; /*该行很关键, 它把中断的处理函数添加到该中断描述符的中断例程list里*/
#if defined(CONFIG_IRQ_PER_CPU)
if (new->flags & IRQF_PERCPU)
desc->status |= IRQ_PER_CPU;
#endif
if (!shared) {
/*对于IRQ_WDT来说这步是多余的,初始化 的时候已做过*/
irq_chip_set_defaults(desc->chip);

/* Setup the type (level, edge polarity) if configured: */
/*对于IRQ_WDT来说,没有定义触发方式, 即默认触发方式*/
if (new->flags & IRQF_TRIGGER_MASK) {
/*如果要设触发方式,则调用平台特定的函数,因此如果我们的平台要实现该功能则必*须要在irq_chip对象里加入对set_type 函数的支持*/
if (desc->chip && desc->chip->set_type)
desc->chip->set_type(irq, new->flags & IRQF_TRIGGER_MASK);
else
/*
* IRQF_TRIGGER_* but the PIC does not support
* multiple flow-types?
*/
printk(KERN_WARNING "No IRQF_TRIGGER set_type "
"function for IRQ %d (%s)n", irq,
desc->chip ? desc->chip->name :
"unknown");
} else
/*这函数只是简单的检查是否有handle_irq*/
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
/* Undo nested disables: */
desc->depth = 1;
}
spin_unlock_irqrestore(&desc->lock, flags);
new->irq = irq;
register_irq_proc(irq); /*在/proc/irq/下创建该中断的一个文件*/
new->dir = NULL;
register_handler_proc(irq, new); /*为/proc/irq/下 创建的文件设置处理函数*/

return 0;

mismatch:
spin_unlock_irqrestore(&desc->lock, flags);
if (!(new->flags & IRQF_PROBE_SHARED)) {
printk(KERN_ERR "IRQ handler type mismatch for IRQ %dn", 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); /*unmask该中断*/
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 %dn", irqno);
irqno = IRQ_EINT0;
mask = __raw_readl(S3C2410_INTMSK);
mask &= ~(1UL << irqno);
__raw_writel(mask, S3C2410_INTMSK);
}


对着s3c2410的data sheet一看就知道了, 就是打开相应中断。至此中断处理函数安装好了,中断也打开了,系统就可以正确的响应中断了。Ok,到此为止IRQ_WDT的中断注册过程已完 成,此时的中断描述符如下所示:

From Li Haifeng's Blog, post arm-linux注册中断

Post Footer automatically generated by wp-posturl plugin for wordpress.

分享到: