arm linux 下中断流程简要分析

arm linux 下中断流程简要分析

author: aaron ([email protected])

这篇文档准备简要的分析下arm平台上linux下的中断是如何运行的,本文将先分析初始化时的中断是如何建立的,然后以一个例子来注册一个中断,并详细分析中断触发到调用我们自己的中断例程的整个流程。不对之处,欢迎指正!

   本文linux2.6.18的源码和s3c2410CPUsmdk2410的板子为例来分析代码。

  

中断初始化:

   大家都知道arm下规定,在0x000000000xffff0000的地址处必须存放一张跳转表, 其格式如下:

arm linux 下中断流程简要分析_第1张图片

<!--[if !vml]--><!--[endif]-->

上面的这个表我们称之为异常中断向量表”, 表中的IRQFIQ位置就是用来存放处理中断函数的地址。至于选择何处存放该表,可由CPU的协处理器完成。 s3c2410下由CP15中寄存器1的位13来决定,我们可以通过设置该位来告诉系统我们的向量表在哪。 具体可参考<<ARM ArchitectureReference Manual>>

因此,在中断初始化的时候我们要做的就是在IRQFIQ的位置处放置我们的中断处理函数地址或跳转语句跳转到我们的中断处理函数。这个过程是在trap_init中完成的, 而他由start_kernel()调用。

arch/arm/kernel/traps.c:

void __init trap_init(void)

{

    unsigned long vectors = CONFIG_VECTORS_BASE;/*跳转表的存放位置(即上面那表的存放位置)*/

 

    /*这些都在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.

     */

    /*复制跳转表内容到指定的位置*/

    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);

}

   上面这个函数主要就是在CONFIG_VECTORS_BASE处设置好那张跳转表, CONFIG_VECTORS_BASEautoconf.h中定义(该文件自动成生),值为0xffff0000, CP15下的r1[13]在系统启动的时候在汇编部分就已经设置好了。

接下来我们就看下__vectors_start__vectors_end__stubs_start__stubs_end之间的内容。

arch/arm/kernel/entry-armv.S:

 .globl __vectors_start

__vectors_start:

    swi SYS_ERROR0

    b   vector_und + stubs_offset

    ldr pc, .LCvswi + stubs_offset

    b   vector_pabt + stubs_offset

    b   vector_dabt + stubs_offset

    b   vector_addrexcptn + stubs_offset

    b   vector_irq + stubs_offset

    b   vector_fiq + stubs_offset

 

    .globl  __vectors_end

__vectors_end:

 

    .data

呵呵看到了吧,就是那张跳转表。vector_irqvector_fiq等函数我们后面在分析,他们就定义在__stubs_start__stubs_end中。

至此经过traps_init后,在0xffff0000处的跳转表就形成了。当产生IRQ时,将调用b    vector_irq + stubs_offset

 

在系统初始化的时候还会调用init_IRQ函数(也由start_kernel调用),它初始化了一个全局中断描述符表(该表保存了每个中断的所有属性信息)。并调用特定平台的中断初始化函数。

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();  /*特定平台的中断初始化*/

}

系统中总共有NR_IRQS个中断,并且每个中断都有一个中断描述符,保存在irq_desc中,该描述符保存了该中断的所有属性信息。

对于平台smdk2410来说init_arch_irq()就是s3c24xx_init_irq()函数, 这是在setup_arch()里面赋值的。

后面的内容我们都以中断号: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标志位,该位代表是否系统中触发了一个中断*/

    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); /*为中断号设置chip*/

            set_irq_handler(irqno, do_edge_IRQ); /*设置中断例程*/

            set_irq_flags(irqno, IRQF_VALID);    /*设置中断ready的标记*/

        }

    }

 

    /* 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");

}

上面这个函数结合s3c2410data sheet很好理解,就是注册各个必要的中断,注意这里为每个中断号注册的中断例程只是个整体的函数,该函数只是处理一些共性的操作如清中断标记位等,他会进一步调用我们注册的中断例程来处理特定的中断。如何注册中断会在后面分析。

这个初始化函数调用了很多与中断相关的函数,我们逐个分析:

先看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

 */

/*为某个中断号设置一个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设置一些默认的enable,disable函数*/

    desc->chip = chip;  /*为中断保存chip对象*/

    /*

     * For compatibility only:

     */

    desc->chip = chip;

    spin_unlock_irqrestore(&desc->lock, flags);

 

    return 0;

}

为特定中断号初始化好chip对象,表示该中断号由这个chip控制,后面会调用到该中断号所属chip的相关函数,各个中断的chip是不同的,IRQ_WDT为例,它的chips3c_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没有相应的操作函数,则就给chip赋默认的操作函数。

 

我们接着看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) { /*参数检查*/

        printk(KERN_ERR

               "Trying to install type control for IRQ%d/n", irq);

        return;

    }

 

    desc = irq_desc + irq; /*获取中断描述符的存储地址*/

 

    if (!handle)

        handle = handle_bad_irq;  /*赋默认的中断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;  /*赋默认的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 */

 

    /*由上面的调用可知,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);

}

上面这个函数就是为特定的中断设置好一个中断处理例程(这里的例程可不是我们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 标记*/

    if (iflags & IRQF_PROBE)

        desc->status &= ~IRQ_NOPROBE;

    if (!(iflags & IRQF_NOAUTOEN))

        desc->status &= ~IRQ_NOAUTOEN;

    spin_unlock_irqrestore(&desc->lock, flags);

}

该函数主要是为特定的中断设置相应的状态标记,而这里我们调用它的目的就是清掉IRQ_NOREQUEST标记,告诉系统该中断已经可以被申请使用了,中断在申请的时候会查看是否有IRQ_NOREQUEST标记,如有则表面该中断还不能使用。而初始化的时候所有的中断都有这个标记。

 

 

 

这部分我们以3sc2410下的watchdog的中断为例来讲解中断的注册及调用过程。

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_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 |= SA_INTERRUPT;

#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 %d/n", irq);

        dump_stack();

    }

    return -EBUSY;

}

该函数主要是安装了一个中断,并使能它。我们先看使能函数,它是平台相关的,对于s3c2410IRQ_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 %d/n", irqno);

 

    irqno -= IRQ_EINT0;

 

    mask = __raw_readl(S3C2410_INTMSK);

    mask &= ~(1UL << irqno);

    __raw_writel(mask, S3C2410_INTMSK);

}

对着s3c2410data sheet一看就知道了, 就是打开相应中断。

至此中断处理函数安装好了,中断也打开了,系统就可以正确的响应中断了。

Ok,到此为止IRQ_WDT的中断注册过程已完成,此时的中断描述符如下所示:

arm linux 下中断流程简要分析_第2张图片

<!--[if !vml]--><!--[endif]-->

你可能感兴趣的:(c,linux,vector,struct,function,action)