arm linux 下中斷流程簡要分析初始化
因此,在中斷初始化的時候我們要做的就是在IRQ和FIQ的位置處放置我們的中斷處理函數(shù)地址或跳轉(zhuǎn)語句跳轉(zhuǎn)到我們的中斷處理函數(shù)。這個過程是在trap_init中完成的,而他由start_kernel()調(diào)用。
本文引用地址:http://www.ex-cimer.com/article/201611/317932.htmarch/arm/kernel/traps.c:
void __init trap_init(void)
{
unsigned long vectors = CONFIG_VECTORS_BASE;/*跳轉(zhuǎn)表的存放位置(即上面那表的存放位置)*/
/*這些都在entry-armv.S下定義*/
extern char __stubs_start[], __stubs_end[];
extern char __vectors_start[], __vectors_end[];
extern char __kuser_helper_start[], __kuser_helper_end[];
int kuser_sz = __kuser_helper_end - __kuser_helper_start;
/*
* Copy the vectors, stubs and kuser helpers (in entry-armv.S)
* into the vector page, mapped at 0xffff0000, and ensure these
* are visible to the instruction stream.
*/
/*跳轉(zhuǎn)表內(nèi)容到指定的位置*/
memcpy((void *)vectors, __vectors_start, __vectors_end - __vectors_start);
memcpy((void *)vectors + 0x200, __stubs_start, __stubs_end - __stubs_start);
memcpy((void *)vectors + 0x1000 - kuser_sz, __kuser_helper_start, kuser_sz);
/*
* Copy signal return handlers into the vector page, and
* set sigreturn to be a pointer to these.
*/
memcpy((void *)KERN_SIGRETURN_CODE, sigreturn_codes,
sizeof(sigreturn_codes));
flush_icache_range(vectors, vectors + PAGE_SIZE);
modify_domain(DOMAIN_USER, DOMAIN_CLIENT);
}
上面這個函數(shù)主要就是在CONFIG_VECTORS_BASE處設(shè)置好那張?zhí)D(zhuǎn)表,CONFIG_VECTORS_BASE在autoconf.h中定義(該文件自動成生),值為0xffff0000,而CP15下的r1[13]在系統(tǒng)啟動的時候在匯編部分就已經(jīng)設(shè)置好了。
接下來我們就看下__vectors_start,__vectors_end,__stubs_start,__stubs_end之間的內(nèi)容。
arch/arm/kernel/entry-armv.S:
.globl__vectors_start
__vectors_start:
swiSYS_ERROR0
bvector_und + stubs_offset
ldrpc, .LCvswi + stubs_offset
bvector_pabt + stubs_offset
bvector_dabt + stubs_offset
bvector_addrexcptn + stubs_offset
bvector_irq + stubs_offset
bvector_fiq + stubs_offset
.globl__vectors_end
__vectors_end:
.data
看到了吧, 就是那張?zhí)D(zhuǎn)表。vector_irq,vector_fiq等函數(shù)我們后面在分析,他們就定義在__stubs_start,__stubs_end中。
至此經(jīng)過traps_init后,在0xffff0000處的跳轉(zhuǎn)表就形成了。當(dāng)產(chǎn)生IRQ時,將調(diào)用bvector_irq + stubs_offset
在系統(tǒng)初始化的時候還會調(diào)用init_IRQ函數(shù)(也由start_kernel調(diào)用),它初始化了一個全局中斷描述符表(該表保存了每個中斷的所有屬性信息)。并調(diào)用特定平臺的中斷初始化函數(shù)。
arm/arm/kernel/Irq.c:
void __init init_IRQ(void)
{
int irq;
/*初始化中斷描述符表*/
for (irq = 0; irq < NR_IRQS; irq++)
irq_desc[irq].status |= IRQ_NOREQUEST | IRQ_DELAYED_DISABLE |
IRQ_NOPROBE;
#ifdef CONFIG_SMP
bad_irq_desc.affinity = CPU_MASK_ALL;
bad_irq_desc.cpu = smp_processor_id();
#endif
init_arch_irq();/*特定平臺的中斷初始化*/
}
系統(tǒng)中總共有NR_IRQS個中斷,并且每個中斷都有一個中斷描述符,保存在irq_desc中,該描述符保存了該中斷的所有屬性信息。
對于平臺smdk2410來說init_arch_irq()就是s3c24xx_init_irq()函數(shù),這是在setup_arch()里面賦值的。
后面的內(nèi)容我們都以中斷號:IRQ_WDT為例來講解:
arch/arm/mach-s3c2410/Irq.c:
/* s3c24xx_init_irq
*
* Initialise S3C2410 IRQ system
*/
void __init s3c24xx_init_irq(void)
{
unsigned long pend;
unsigned long last;
int irqno;
int i;
irqdbf("s3c2410_init_irq: clearing interrupt status flags/n");
/* first, clear all interrupts pending... */
/*先清掉所有的pending標(biāo)志位,該位代表是否系統(tǒng)中觸發(fā)了一個中斷*/
last = 0;
for (i = 0; i < 4; i++) {
pend = __raw_readl(S3C24XX_EINTPEND);
if (pend == 0 || pend == last)
break;
__raw_writel(pend, S3C24XX_EINTPEND);
printk("irq: clearing pending ext status %08x/n", (int)pend);
last = pend;
}
last = 0;
for (i = 0; i < 4; i++) {
pend = __raw_readl(S3C2410_INTPND);
if (pend == 0 || pend == last)
break;
__raw_writel(pend, S3C2410_SRCPND);
__raw_writel(pend, S3C2410_INTPND);
printk("irq: clearing pending status %08x/n", (int)pend);
last = pend;
}
last = 0;
for (i = 0; i < 4; i++) {
pend = __raw_readl(S3C2410_SUBSRCPND);
if (pend == 0 || pend == last)
break;
printk("irq: clearing subpending status %08x/n", (int)pend);
__raw_writel(pend, S3C2410_SUBSRCPND);
last = pend;
}
/* register the main interrupts */
/*注冊主要的中斷*/
irqdbf("s3c2410_init_irq: registering s3c2410 interrupt handlers/n");
for (irqno = IRQ_EINT4t7; irqno <= IRQ_ADCPARENT; irqno++) {
/* set all the s3c2410 internal irqs */
switch (irqno) {
/* deal with the special IRQs (cascaded) */
case IRQ_EINT4t7:
case IRQ_EINT8t23:
case IRQ_UART0:
case IRQ_UART1:
case IRQ_UART2:
case IRQ_ADCPARENT:
set_irq_chip(irqno, &s3c_irq_level_chip);
set_irq_handler(irqno, do_level_IRQ);
break;
case IRQ_RESERVED6:
case IRQ_RESERVED24:
/* no IRQ here */
break;
default:/*IRQ_WDT就是這條通路*/
//irqdbf("registering irq %d (s3c irq)/n", irqno);
set_irq_chip(irqno, &s3c_irq_chip); /*為中斷號設(shè)置chip*/
set_irq_handler(irqno, do_edge_IRQ); /*設(shè)置中斷例程*/
set_irq_flags(irqno, IRQF_VALID);/*設(shè)置中斷ready的標(biāo)記*/
}
}
/* setup the cascade irq handlers */
set_irq_chained_handler(IRQ_EINT4t7, s3c_irq_demux_extint);
set_irq_chained_handler(IRQ_EINT8t23, s3c_irq_demux_extint);
set_irq_chained_handler(IRQ_UART0, s3c_irq_demux_uart0);
set_irq_chained_handler(IRQ_UART1, s3c_irq_demux_uart1);
set_irq_chained_handler(IRQ_UART2, s3c_irq_demux_uart2);
set_irq_chained_handler(IRQ_ADCPARENT, s3c_irq_demux_adc);
/* external interrupts */
for (irqno = IRQ_EINT0; irqno <= IRQ_EINT3; irqno++) {
irqdbf("registering irq %d (ext int)/n", irqno);
set_irq_chip(irqno, &s3c_irq_eint0t4);
set_irq_handler(irqno, do_edge_IRQ);
set_irq_flags(irqno, IRQF_VALID);
}
for (irqno = IRQ_EINT4; irqno <= IRQ_EINT23; irqno++) {
irqdbf("registering irq %d (extended s3c irq)/n", irqno);
set_irq_chip(irqno, &s3c_irqext_chip);
set_irq_handler(irqno, do_edge_IRQ);
set_irq_flags(irqno, IRQF_VALID);
}
/* register the uart interrupts */
irqdbf("s3c2410: registering external interrupts/n");
for (irqno = IRQ_S3CUART_RX0; irqno <= IRQ_S3CUART_ERR0; irqno++) {
irqdbf("registering irq %d (s3c uart0 irq)/n", irqno);
set_irq_chip(irqno, &s3c_irq_uart0);
set_irq_handler(irqno, do_level_IRQ);
set_irq_flags(irqno, IRQF_VALID);
}
for (irqno = IRQ_S3CUART_RX1; irqno <= IRQ_S3CUART_ERR1; irqno++) {
irqdbf("registering irq %d (s3c uart1 irq)/n", irqno);
set_irq_chip(irqno, &s3c_irq_uart1);
set_irq_handler(irqno, do_level_IRQ);
set_irq_flags(irqno, IRQF_VALID);
}
for (irqno = IRQ_S3CUART_RX2; irqno <= IRQ_S3CUART_ERR2; irqno++) {
irqdbf("registering irq %d (s3c uart2 irq)/n", irqno);
set_irq_chip(irqno, &s3c_irq_uart2);
set_irq_handler(irqno, do_level_IRQ);
set_irq_flags(irqno, IRQF_VALID);
}
for (irqno = IRQ_TC; irqno <= IRQ_ADC; irqno++) {
irqdbf("registering irq %d (s3c adc irq)/n", irqno);
set_irq_chip(irqno, &s3c_irq_adc);
set_irq_handler(irqno, do_edge_IRQ);
set_irq_flags(irqno, IRQF_VALID);
}
irqdbf("s3c2410: registered interrupt handlers/n");
}
上面這個函數(shù)結(jié)合s3c2410的data sheet很好理解,就是注冊各個必要的中斷,注意這里為每個中斷號注冊的中斷例程只是個整體的函數(shù),該函數(shù)只是處理一些共性的操作如清中斷標(biāo)記位等,他會進(jìn)一步調(diào)用我們注冊的中斷例程來處理特定的中斷。如何注冊中斷會在后面分析。
這個初始化函數(shù)調(diào)用了很多與中斷相關(guān)的函數(shù),我們逐個分析:
先看set_irq_chip
kernel/irq/chip.c:
/
*set_irq_chip - set the irq chip for an irq
*@irq:irq number
*@chip:pointer to irq chip description structure
*/
/*為某個中斷號設(shè)置一個chip*/
int set_irq_chip(unsigned int irq, struct irq_chip *chip)
{
struct irq_desc *desc;
unsigned long flags;
if (irq >= NR_IRQS) {
printk(KERN_ERR "Trying to install chip for IRQ%d/n", irq);
WARN_ON(1);
return -EINVAL;
}
if (!chip)
chip = &no_irq_chip;
desc = irq_desc + irq;/*獲取保存該中斷的中斷描述符*/
spin_lock_irqsave(&desc->lock, flags);
irq_chip_set_defaults(chip); /*為chip設(shè)置一些默認(rèn)的enable,disable函數(shù)*/
desc->chip = chip;/*為中斷保存chip對象*/
/*
* For compatibility only:
*/
desc->chip = chip;
spin_unlock_irqrestore(&desc->lock, flags);
return 0;
}
為特定中斷號初始化好chip對象,表示該中斷號由這個chip控制,后面會調(diào)用到該中斷號所屬chip的相關(guān)函數(shù),各個中斷的chip是不同的,以IRQ_WDT為例,它的chip是s3c_irq_chip。
arch/arm/mach-s3c2410/Irq.c:
static struct irqchip s3c_irq_chip = {
.ack= s3c_irq_ack,
.mask= s3c_irq_mask,
.unmask= s3c_irq_unmask,
.set_wake= s3c_irq_wake
};
在看irq_chip_set_defaults
kernel/irq/Chip.c:
/*
* Fixup enable/disable function pointers
*/
void irq_chip_set_defaults(struct irq_chip *chip)
{
if (!chip->enable)
chip->enable = default_enable;
if (!chip->disable)
chip->disable = default_disable;
if (!chip->startup)
chip->startup = default_startup;
if (!chip->shutdown)
chip->shutdown = chip->disable;
if (!chip->name)
chip->name = chip->typename;
}
很顯然,如果chip沒有相應(yīng)的操作函數(shù),則就給chip賦默認(rèn)的操作函數(shù)。
我們接著看set_irq_handler()
include/linux/Irq.h:
static inline void
set_irq_handler(unsigned int irq,
void fastcall (*handle)(unsigned int, struct irq_desc *,
struct pt_regs *))
{
__set_irq_handler(irq, handle, 0);
}
kernel/irq/Chip.c:
void
__set_irq_handler(unsigned int irq,
void fastcall (*handle)(unsigned int, irq_desc_t *,
struct pt_regs *),
int is_chained)
{
struct irq_desc *desc;
unsigned long flags;
if (irq >= NR_IRQS) { /*參數(shù)檢查*/
printk(KERN_ERR
"Trying to install type control for IRQ%d/n", irq);
return;
}
desc = irq_desc + irq; /*獲取中斷描述符的存儲地址*/
if (!handle)
handle = handle_bad_irq;/*賦默認(rèn)的中斷handle*/
if (desc->chip == &no_irq_chip) {
printk(KERN_WARNING "Trying to install %sinterrupt handler "
"for IRQ%d/n", is_chained ? "chained " : " ", irq);
/*
* Some ARM implementations install a handler for really dumb
* interrupt hardware without setting an irq_chip. This worked
* with the ARM no_irq_chip but the check in setup_irq would
* prevent us to setup the interrupt at all. Switch it to
* dummy_irq_chip for easy transition.
*/
desc->chip = &dummy_irq_chip;/*賦默認(rèn)的chip*/
}
spin_lock_irqsave(&desc->lock, flags);
/* Uninstall? */
if (handle == handle_bad_irq) {
if (desc->chip != &no_irq_chip) {
desc->chip->mask(irq);
desc->chip->ack(irq);
}
desc->status |= IRQ_DISABLED;/*沒有中斷例程則disable掉該中斷*/
desc->depth = 1;
}
desc->handle_irq = handle;/*保存中斷例程,對于IRQ_WDT來說則是do_edge_IRQ */
/*由上面的調(diào)用可知,is_chained始終等于0*/
if (handle != handle_bad_irq && is_chained) {
desc->status &= ~IRQ_DISABLED;
desc->status |= IRQ_NOREQUEST | IRQ_NOPROBE;
desc->depth = 0;
desc->chip->unmask(irq);
}
spin_unlock_irqrestore(&desc->lock, flags);
}
上面這個函數(shù)就是為特定的中斷設(shè)置好一個中斷處理例程(這里的例程可不是我們request_irq注冊的例程喔)。
接著看set_irq_flags()
arch/arm/kernel/Irq.c:
void set_irq_flags(unsigned int irq, unsigned int iflags)
{
struct irqdesc *desc;
unsigned long flags;
if (irq >= NR_IRQS) {
printk(KERN_ERR "Trying to set irq flags for IRQ%d/n", irq);
return;
}
desc = irq_desc + irq;
spin_lock_irqsave(&desc->lock, flags);
desc->status |= IRQ_NOREQUEST | IRQ_NOPROBE | IRQ_NOAUTOEN;
if (iflags & IRQF_VALID)
desc->status &= ~IRQ_NOREQUEST;/*清掉IRQ_NOREQUEST標(biāo)記*/
if (iflags & IRQF_PROBE)
desc->status &= ~IRQ_NOPROBE;
if (!(iflags & IRQF_NOAUTOEN))
desc->status &= ~IRQ_NOAUTOEN;
spin_unlock_irqrestore(&desc->lock, flags);
}
該函數(shù)主要是為特定的中斷設(shè)置相應(yīng)的狀態(tài)標(biāo)記, 而這里我們調(diào)用它的目的就是清掉IRQ_NOREQUEST標(biāo)記,告訴系統(tǒng)該中斷已經(jīng)可以被申請使用了,中斷在申請的時候會查看是否有IRQ_NOREQUEST標(biāo)記,如有則表面該中斷還不能使用。而初始化的時候所有的中斷都有這個標(biāo)記。
評論