arm linux 下中斷流程簡要分析注冊中斷
這部分我們以3sc2410下的watchdog的中斷為例來講解中斷的注冊及調(diào)用過程。
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函數(shù)中為watchdog注冊了一個中斷,中斷號為IRQ_WDT,
#define IRQ_WDTS3C2410_IRQ(9)
中斷處理函數(shù)是s3c2410wdt_irq。
我們來看request_irq是如何實現(xiàn)的:
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
/*
* Lockdep wants atomic interrupt handlers:
*/
irqflags |= SA_INTERRUPT;
#endif
/*
* Sanity-check: shared interrupts must pass in a real dev-ID,
* otherwise well have trouble later trying to figure out
* which interrupt is which (messes up the interrupt freeing
* logic etc).
*/
/*允許共享的中斷必須要有一個獨一無二的dev_id,看上面函數(shù)的解釋
if ((irqflags & IRQF_SHARED) && !dev_id)
return -EINVAL;
if (irq >= NR_IRQS) /*中斷號是否合法*/
return -EINVAL;
/*這個標記對s3c2410來說在s3c24xx_init_irq里通過調(diào)用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);/*清除相應(yīng)位*/
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;
}
該函數(shù)的功能及參數(shù)含義在函數(shù)頭有詳細的說明,這里就不多介紹了,值得注意的是我們在申請中斷之前,必須要去掉該中斷的IRQ_NOREQUEST標記(系統(tǒng)初始化的時候賦了這個標記), 具體方法是調(diào)用set_irq_flags()函數(shù)。
接著看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里已經(jīng)初始化過了*/
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觸發(fā)方式相同,2都允許中斷共享*/
spin_lock_irqsave(&desc->lock, flags);
p = &desc->action;
old = *p;
if (old) { /*對于IRQ_WDT,這個if不成立,但它已經(jīng)設(shè)置了handle_irq喔*/
/*
* Cant 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;/*該行很關(guān)鍵, 它把中斷的處理函數(shù)添加到該中斷描述符的中斷例程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來說,沒有定義觸發(fā)方式, 即默認觸發(fā)方式*/
if (new->flags & IRQF_TRIGGER_MASK) {
/*如果要設(shè)觸發(fā)方式,則調(diào)用平臺特定的函數(shù),因此如果我們的平臺要實現(xiàn)該功能則必*須要在irq_chip對象里加入對set_type函數(shù)的支持*/
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
/*這函數(shù)只是簡單的檢查是否有handle_irq*/
compat_irq_chip_set_default_handler(desc);
/*去掉中斷的相關(guān)狀態(tài),讓它就緒*/
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); /*平臺相關(guān)函數(shù)*/
else
desc->chip->enable(irq);/*平臺相關(guān)函數(shù)*/
} else
/* Undo nested disables: */
desc->depth = 1;
}
spin_unlock_irqrestore(&desc->lock, flags);
new->irq = irq;
register_irq_proc(irq);/*在/proc/irq/下創(chuàng)建該中斷的一個文件*/
new->dir = NULL;
register_handler_proc(irq, new); /*為/proc/irq/下創(chuàng)建的文件設(shè)置處理函數(shù)*/
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;
}
該函數(shù)主要是安裝了一個中斷,并使能它。 我們先看使能函數(shù),它是平臺相關(guān)的,對于s3c2410的IRQ_WDT來說使用的是默認的使能函數(shù)(default_startup(),初始化賦值),如果我們要使用自己的使能函數(shù),只要在chip對象里添加就行了。
kernel/irq/Chip.c:
static unsigned int default_startup(unsigned int irq)
{
irq_desc[irq].chip->enable(irq);
return 0;
}
對于IRQ_WDT來說調(diào)用的irq_desc[irq].chip->enable(irq)仍是默認函數(shù):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來說這次的調(diào)用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一看就知道了, 就是打開相應(yīng)中斷。
至此中斷處理函數(shù)安裝好了,中斷也打開了,系統(tǒng)就可以正確的響應(yīng)中斷了。
Ok,到此為止IRQ_WDT的中斷注冊過程已完成,此時的中斷描述符如下所示:
評論