嵌入式中断(stm32+linux)详解

一开始学单片机时 就知道中断这一个概念 看些视频学了大半天 也是似懂非懂 看代码也就是设置下寄存器写下中断服务程序而已 学的不是很深入 一直到现在 经过了<微机原理>这门课的学习 才对中断有了一定的深入了解 发现中断也就是一门说复杂也能很复杂 只要封装好了api 使用也很简单的事 但是身为一名学习者 还是有必要学习中断的使用和原理

身为一个学习嵌入式的小白 一开始我学习的当然是单片机 所学的单片机是stm32 相信大部分嵌入式的人都会 所学的中断 是这样使用的
(这里主要是记载以下如何使用中断 至于中断的什么优先级,什么优先级分组,使能之类的原理,就不再赘述)
第一步:将一个I/O口配置成中断输入模式。
注意点:一般中断分为外部中断和内部中断 外部中断一般是指由计算机外设发出的中断请求,如:键盘中断、打印机中断、定时器中断等。外部中断是可以屏蔽的中断,也就是说,利用中断控制器可以屏蔽这些外部设备 的中断请求。内部中断是指因硬件出错(如突然掉电、奇偶校验错等)或运算出错(除数为零、运算溢出、单步中断等)所引起的中断。
这里使用的是按键中断 用的是GPIO引脚 板子上是PD^11,PD^12两个端口作为中断输入的 所以这里要做的是
1)初始化I/O口为输入;
2)开启I/O复用时钟,并设置外部事件映射关联。

为啥GPIO口使用中断方式进行工作的时候就必须要映射到外部事件上去,而其他就不呢?参照下网友的解析是:比如USART产生的中断,是没有经过EXTI,而是直接将中断放入了NVIC;但是GPIO它作为中断源,是要经过EXTI的。仔细参看下面两个图,其实就会恍然大悟:
这里写图片描述这里写图片描述

所以GPIO口的中断方式进行工作要映射到外部事件上去

 void BUTTON_Configuration(void)
 {
     /* 初始化PD^11 PD^12为中断输入模式 */
    GPIO_InitTypeDef    GPIO_InitStructure;
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;  
    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_11 | GPIO_Pin_12;
    GPIO_Init(GPIOD, &GPIO_InitStructure);
    /* 开启复用时钟 */
    RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOD | RCC_APB2Periph_AFIO,ENABLE);
    /* 将PD^11,PD^12映射到外部事件线上去 */
    GPIO_EXTILineConfig(GPIO_PortSourceGPIOD , GPIO_PinSource11);
    GPIO_EXTILineConfig(GPIO_PortSourceGPIOD , GPIO_PinSource12);
 }

第一步是将外部GPIO口映射到某外部事件上去。那么接下来,就该对该外部事件进行配置了,包括外部事件线路的选择、触发条件、使能。这里需要理解清楚的是,GPIO口和外部事件是各自独立的,它们并不是一体的—详细理解第一步,将GPIO口映射到某外部事件,可以看出GPIO和外部事件这个东西是两个不同的东西,在这里,GPIO的映射,无非就是GPIO口搭了外部事件的一趟顺风车。也所以,外部事件依然是要配置和使能的,不能说,将GPIO口映射到外部事件就可以产生中断了。

void EXTI_Configuration(void)
{
    EXTI_InitTypeDef EXTI_InitStructure;
    /*PD11外部中断输入 下降沿触发*/
    EXTI_InitStructure.EXTI_Line = EXTI_Line11; 
    EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt;
    EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Falling;
    EXTI_InitStructure.EXTI_LineCmd    = ENABLE;
    EXTI_Init(&EXTI_InitStructure);

    /*PD12外部中断输入 下降沿触发*/
    EXTI_InitStructure.EXTI_Line = EXTI_Line12;
    EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt;
    EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Falling;
    EXTI_InitStructure.EXTI_LineCmd    = ENABLE; 
    EXTI_Init(&EXTI_InitStructure);
}

第三步,现在就该配置中断了。也即是配置中断分组,以及中断优先级。当然,这并不是最后的工作。

void NVIC_Configuration(void)
{
    NVIC_InitTypeDef NVIC_InitStructure;
    /* 中断分组 影响到了后面优先级的分配 */
    NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);

    /*外部中断线 使能中断*/
    NVIC_InitStructure.NVIC_IRQChannel = EXTI15_10_IRQn ;      
    NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0 ;
    NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;
    NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE ;
    NVIC_Init(&NVIC_InitStructure);
}

不管是IO的配置和外部事件还有中断的配置都配置完毕后 最后一步就是写中断服务函数
第四步:中断服务函数
这里有个重点必须注意:所有中断服务函数的名字,ST官方已经取好了,而且还放在了中断向量表中了<也即是启动文件里>,如果你不自己写启动文件的话,那么你的中断服务函数的名字必须和ST官方的一样,不然,一个中断来了,找不到负责任的函数,它就只有悲剧去了。

void EXTI15_10_IRQHandler(void)
{
    if(EXTI_GetITStatus(EXTI_Line11)!= RESET)  
    {  
        EXTI_ClearITPendingBit(EXTI_Line11);
        Flag = 0x01;
    }

    if(EXTI_GetITStatus(EXTI_Line12)!= RESET)  
    {  
        EXTI_ClearITPendingBit(EXTI_Line12);
        Flag = 0x02;
    }    
}
int main(void)
{
    /* Add your application code here
       */
    SystemInit();              /*系统初始化*/
    LED_Configuration();       /* LED初始化 也就是GPIO的简单配置而已 */
    BUTTON_Configuration();     
    NVIC_Configuration();
    EXTI_Configuration();
    /* Infinite loop */
    while (1)
    {          
        switch(Flag)
        {
            case 0x01:
            {
                LED2(1); 
                Delay();
                LED2(0);
                Delay();
                break;                
            }
            case 0x02:
            {
                LED3(1);
                Delay();
                LED3(0);
                Delay();
                break;                
            }
            default   :
            {
                 LED1(1);
                Delay();
                LED1(0);
                Delay();
                break;
            }          
        }
    }
}

主函数主要是调用一些初始化函数然后定义了一个全局变量Flag,每次中断,都影响Flag的值,然后main函数判断该值,就这么简单。完了

将GPIO作为中断的处理流程
1. GPIO初始化。包括外设时钟,管脚,速度,模式等。
2. 将GPIO脚连接到EXTI line。这是通过写AFIO下的EXTICR寄存器实现的。
3. EXTI初始化,使能该条EXTI line,并做上升下降沿设置。通过写EXTI下IMR, EMR, PTSR, FTSR实现。
4. NVIC初始化。包括优先级的计算和使能。
当中断到来,ISR做相应处理后:
1. 清除GPIO寄存器中的信号量。
2. 清除EXTI上的信号量。

这就是一个stm32的中断使用方式

这里穿插一个小知识 也就是怎么找到中断服务程序的地址呢 当一个中断发生时 开发板就会根据对应的中断找到中断服务程序 然后运行 运行完后就恢复原样 返回
其实这里是通过中断向量表实现的
拿x86的的中断系统举例
每个中断源对应一个确定的8位中断类型码,cpu在响应中断后,会根据中断类型码查询中断向量表转入的中断服务程序
一般的做法是
1.将中断类型码*4 作为中断向量表的地址指针
2.将cpu的标志寄存器入栈保护
3.清楚IF和TF标志位,屏蔽信的INTR中断和单步中断
4.保存断点,即把断点处的IP和CS值压入堆栈,先压入CS值,再压入IP值
5.从中断向量表中取中断服务程序的入口地址,分别送至CS和IP中
6.从新的CS:IP执行中断程序
CS:段地址 IP:偏移量

下面我们就来讲解下Linux下的中断 linux作为一个嵌入式操作系统 中断机制的代码很庞大和复杂 这里也只是稍微的讲解下大概的框架 全部讲完估计要讲三天三夜也未必讲的清楚
在中断机制中 会有一个叫中断控制器的东西 负责将外设的中断请求经过一下处理后发送到CPU的中断输入 外设是不能直接中断通知CPU的 同时CPU也不能一直轮询外设 这样太耗精力和时间 所以这工作由中断控制器来完成
比如最经典的就是8259A中断控制器
这里写图片描述
IRQ0-IRQ7是8个中断输入信号引脚 来接收中断输入信号

但是中断控制器硬件平台多种多样 为了屏蔽各种硬件平台的区别,Linux提供了一个统一抽像的平台来实现中断子系统。irq_chip结构用于描述一个硬件中断控制器,它封装了控制器的名称(如XTPIC或IO-APIC)和控制器相应的操作:

中断控制器

struct irq_chip {
        const char        *name;                                                          //控制器名称
        unsigned int        (*startup)(unsigned int irq);          //第一次激活时调用,用于第一次初始化IRQ
        void                (*shutdown)(unsigned int irq);                        //对应的关闭操作
        void                (*enable)(unsigned int irq);                        //激活IRQ
        void                (*disable)(unsigned int irq);                        //禁用IRQ

        void                (*ack)(unsigned int irq);                                //显示的中断确认操作
        void                (*mask)(unsigned int irq);                                //屏蔽中断
        void                (*mask_ack)(unsigned int irq);                        //屏幕并确认
        void                (*unmask)(unsigned int irq);                        //屏蔽的反向操作
        void                (*eoi)(unsigned int irq);  //end of interrupt,提供处理中断时一个到硬件的回调

        void                (*end)(unsigned int irq);              //end操作表示中断处理在电流层结束
        int                (*set_affinity)(unsigned int irq,                        //设置中断亲和
                                        const struct cpumask *dest);
        int                (*retrigger)(unsigned int irq);
        int                (*set_type)(unsigned int irq, unsigned int flow_type);     //设IRQ电流类型
        int                (*set_wake)(unsigned int irq, unsigned int on);                    //设置唤醒???

        /* Currently used only by UML, might disappear one day.*/
#ifdef CONFIG_IRQ_RELEASE_METHOD
        void                (*release)(unsigned int irq, void *dev_id);
#endif
        /*
         * For compatibility, ->typename is copied into ->name.
         * Will disappear.
         */
        const char        *typename;
};

大多数的操作可以根据名称了解一二。该结构考虑到了各种不同的体系结构,所以一个特系结构的使用,通常仅是它的一个子集,甚至是很小的一个子系,以s3c举例

static struct irq_chip s3c_irq_chip = {
    .name       = "s3c",
    .ack        = s3c_irq_ack,
    .mask       = s3c_irq_mask,
    .unmask     = s3c_irq_unmask,
    .set_wake   = s3c_irq_wake
};

中断描述符
每个中断都有一个编号,系统可以根据编号很容易地区分来访者,是鼠标,还是键盘,或者是网卡。只是很可惜,出于很多原因(例如短视或成本考虑),在很多体系结构上,提供的编号是很少的,例如图1中显示的,两个8259A芯片,总共提供了16个中断槽位。虽然曾经看来,对于个人计算机这已经足够了,只是时过境迁,又到了改变的时候,例如,多个外设共享一个中断,称之为中断共享,有过PCI驱动编写经验的都接触过,当然,这需要硬件和内核同时支持。

在IA-32 CPU上,为外围设备都供了16个中断号,从32-47,不过如果看一下/proc/interrupts就会发现,外围设备的IRQ编号是从0开始到15的,这意味着,中断控制器的一个重要任务,就是对IRQ编号和中断号进行映射,在IA-32上,这个映射,就需要加上32即可。

struct irq_desc {
        unsigned int                irq;
        ……
        irq_flow_handler_t        handle_irq;                //指向上述控制芯片的电流处理程序
        struct irq_chip                *chip;                        //指向上述的控制芯片
        ……
        struct irqaction        *action;        /* IRQ action list */            //指向IRQ的中断action列表
        ……
} ____cacheline_internodealigned_in_smp;

IRQ相关信息的管理的关键之处在于,内核引入一个irq_desc 类型的全局数组来记录之,每个数组的项对应一个IRQ编号,数组槽位与中断号一一对应,IRQ0在位置0,诸如此类。

数组irq_desc_ptrs的初始化在kernel/irq/handle.c

struct irq_desc **irq_desc_ptrs __read_mostly;

irq_desc_legacy是一个用于初始化的临时中介:
static struct irq_desc irq_desc_legacy[NR_IRQS_LEGACY] __cacheline_aligned_in_smp = {
        [0 ... NR_IRQS_LEGACY-1] = {
                .irq            = -1,
                .status            = IRQ_DISABLED,
                .chip            = &no_irq_chip,
                .handle_irq = handle_bad_irq,
                .depth            = 1,
                .lock            = __SPIN_LOCK_UNLOCKED(irq_desc_init.lock),
        }
};

这里是用了GNU C的扩展语法 就是将数组里的所有元素irq=-1,其handle_irq都指向handle_bad_irq。

当你要获得中断描述符的话 内核也提供了一个接口
可以根据irq号获得对应的中断描述符

struct irq_desc *irq_to_desc(unsigned int irq)
{
        if (irq_desc_ptrs && irq < nr_irqs)
                return irq_desc_ptrs[irq];

        return NULL;
}

中断描述符中,其最后一个成员action指向中断处理程序。这将在后文描述,先来看中断描述符的初始化,这在early_irq_init函数中完成:

int __init early_irq_init(void)
{
        struct irq_desc *desc;

        desc = irq_desc_legacy;

        //为中断描述符分配槽位
        irq_desc_ptrs = kcalloc(nr_irqs, sizeof(void *), GFP_NOWAIT);

        legacy_count = ARRAY_SIZE(irq_desc_legacy);

        //初始化之        
        for (i = 0; i < legacy_count; i++) {
                desc[i].irq = i;
                desc[i].kstat_irqs = kstat_irqs_legacy + i * nr_cpu_ids;
                lockdep_set_class(&desc[i].lock, &irq_desc_lock_class);
                alloc_desc_masks(&desc[i], node, true);
                init_desc_masks(&desc[i]);
                irq_desc_ptrs[i] = desc + i;
        }

        //初始化余下的
        for (i = legacy_count; i < nr_irqs; i++)
                irq_desc_ptrs[i] = NULL;                
}

这样,每个irq_desc_ptrs的槽位的初始化工作就完成了。值得注意的是,这里并没有初始化中断描述符的电流处理句柄handle_irq成员。这是留到具体的控制器中去完成的,还是以8259A为例:

void make_8259A_irq(unsigned int irq)
{
        disable_irq_nosync(irq);
        io_apic_irqs &= ~(1<<irq);
        set_irq_chip_and_handler_name(irq, &i8259A_chip, handle_level_irq,
                                      "XT");
        enable_irq(irq);
}

set_irq_chip_and_handler_name函数是内核提供的处理注册irq_chip和设置电流处理程序的API之一:

void
set_irq_chip_and_handler_name(unsigned int irq, struct irq_chip *chip,
                              irq_flow_handler_t handle, const char *name)
{
        //取得IRQ对应的中断描述符,设置其chip成员
        set_irq_chip(irq, chip);
        //设置IRQ对应的中断描述符的handle_irq成员
        __set_irq_handler(irq, handle, 0, name);
}

这样,i8259A_chip控制器的电流处理程序被注册为handle_level_irq,即为电平触发中断,对应的,边沿触发中断的处理程序是handle_edge_irq。

void
handle_level_irq(unsigned int irq, struct irq_desc *desc)
{
        struct irqaction *action;
        irqreturn_t action_ret;

        spin_lock(&desc->lock);
        mask_ack_irq(desc, irq);

        //后面的代码在应答的中断后,会调置IRQ_INPROGRESS标志,这里做一个简单检查
        if (unlikely(desc->status & IRQ_INPROGRESS))
                goto out_unlock;
        //清除IRQ_REPLAY | IRQ_WAITING标志位
        desc->status &= ~(IRQ_REPLAY | IRQ_WAITING);
        //统计
        kstat_incr_irqs_this_cpu(irq, desc);

        /*
         * If its disabled or no action available
         * keep it masked and get out of here
         */
        //从中断描述符中取得action 
        action = desc->action;
        //如果没有action,或者是中断被关闭,退出
        if (unlikely(!action || (desc->status & IRQ_DISABLED)))
                goto out_unlock;

        //设置IRQ_INPROGRESS,表示正在处理
        desc->status |= IRQ_INPROGRESS;
        spin_unlock(&desc->lock);

        //调用高层的中断处理程序handle_IRQ_event进一步处理
        action_ret = handle_IRQ_event(irq, action);
        if (!noirqdebug)
                note_interrupt(irq, desc, action_ret);

        spin_lock(&desc->lock);
        //处理完毕,清除正在处理标志
        desc->status &= ~IRQ_INPROGRESS;
        //如果IRQ没有被禁用,调用chip的unmask
        if (!(desc->status & IRQ_DISABLED) && desc->chip->unmask)
                desc->chip->unmask(irq);
out_unlock:
        spin_unlock(&desc->lock);
}

中断处理程序函数
每个中断处理程序函数都由结构struct irqaction表示,也就是上述中断描述符的最后一个成员:

struct irqaction {
        irq_handler_t handler;
        unsigned long flags;
        cpumask_t mask;
        const char *name;
        void *dev_id;
        struct irqaction *next;
        int irq;
        struct proc_dir_entry *dir;
        irq_handler_t thread_fn;
        struct task_struct *thread;
        unsigned long thread_flags;
};

该结构中,最重要的成员就是处理函数本身,也就是其第一个成员。
flags包含一些标志信,例如IRQF_SHARED/IRQF_TIMER等。
mask存储其CPU位图掩码;
name和dev_id唯一地标识一个中断处理程序;
next成员用于实现共享的IRQ处理程序,相同irq号的一个或几个irqaction汇聚在一个链表中。

小结一下,上述三个重要数据结构的关系就很清楚了:

irq_desc数组包含若干成员,每个成员都一个chip指针,指向对应的中断控制器结构,action指向,指向中断处理函数结构irqaction,若干个具体相同irq的中断处理函数结构串在一个链表上。

irqaction是中断子系统面向驱动程序界面提供的接口,驱动程序在初始化的时候向内核注册,调用request_irq向中断子系统注册,request_irq函数会构造一个action,并将其关联到相应的中断描述符上。

IDT表与中断的触发
中断的触发,或者称之为中断路由,表示一个中断如何达到上述的中断处理函数中。
科普下 在实模式下 cpu用8位的中断类型码通过中断向量表查询到相应的中断服务程序入口 而进入了保护模式后 中断向量表也改叫做中断描述符表IDT(InterruptDescriptor Table)。其中的每个表项叫做一个门描述符(gate descriptor),“门”的含义是当中断发生时必须先通过这些门,然后才能进入相应的处理程序。

IDT(Interrupt Descriptor Table)中断描述表,IDT是个有256个入口的线形表,每个中断向量关联了一个中断处理过程。当计算机运行在实模式时,IDT被初始化并由BIOS使用。然而,一旦真正进入了Linux内核,IDT就被移到内存的另一个区域,并进行进入实模式的初步初始化。内核的初始化流程如下:

start_kernel
->init_IRQ
->native_init_IRQ
void __init native_init_IRQ(void)
{
        ……
        //更新外部中断(IRQ)的IDT表项
        for (i = FIRST_EXTERNAL_VECTOR; i < NR_VECTORS; i++) {
                /* IA32_SYSCALL_VECTOR could be used in trap_init already. */
                //跳过系统调用(trap)使用过的槽位
                if (!test_bit(i, used_vectors))
                        set_intr_gate(i, interrupt[i-FIRST_EXTERNAL_VECTOR]);
        }
}

set_intr_gate在IDT的第i个表项插入一个中断门。门中的段选择符设置为内核代码的段选择符,基偏移量为中断处理程序的地址,
即为第二个参数interrupt[i-FIRST_EXTERNAL_VECTOR]。

interrupt数组在entry_32.S中定义,它本质上都会跳转到common_interrupt:

.section .init.rodata,"a"
ENTRY(interrupt)
.text
        .p2align 5
        .p2align CONFIG_X86_L1_CACHE_SHIFT
ENTRY(irq_entries_start)
        RING0_INT_FRAME
vector=FIRST_EXTERNAL_VECTOR
.rept (NR_VECTORS-FIRST_EXTERNAL_VECTOR+6)/7
        .balign 32
  .rept        7
    .if vector < NR_VECTORS
      .if vector <> FIRST_EXTERNAL_VECTOR
        CFI_ADJUST_CFA_OFFSET -4
      .endif
1:        pushl $(~vector+0x80)        /* Note: always in signed byte range */
        CFI_ADJUST_CFA_OFFSET 4
      .if ((vector-FIRST_EXTERNAL_VECTOR)%7) <> 6
        jmp 2f
      .endif
      .previous
        .long 1b
      .text
vector=vector+1
    .endif
  .endr
2:        jmp common_interrupt
.endr
END(irq_entries_start)

.previous
END(interrupt)
.previous

common_interrupt是所有外部中断的统一入口:

/*
 * the CPU automatically disables interrupts when executing an IRQ vector,
 * so IRQ-flags tracing has to follow that:
 */
        .p2align CONFIG_X86_L1_CACHE_SHIFT
common_interrupt:
        //将中断向量号减256。内核用负数表示所有的中断
        addl $-0x80,(%esp)        /* Adjust vector into the [-256,-1] range */
        //调用SAVE_ALL宏保存寄存器的值
        SAVE_ALL
        TRACE_IRQS_OFF
        //保存栈顶地址
        movl %esp,%eax
        //调用do_IRQ函数
        call do_IRQ
        //从中断返回
        jmp ret_from_intr
ENDPROC(common_interrupt)
        CFI_ENDPROC

这样,就进入了著名的do_IRQ函数了,到这里,基本上有平台相关的汇编代码的处理流程就结束了,相对而言,我还是更喜欢C语言:

/*
 * do_IRQ handles all normal device IRQ's (the special
 * SMP cross-CPU interrupts have their own specific
 * handlers).
 */
unsigned int __irq_entry do_IRQ(struct pt_regs *regs)
{
        //取得原来的寄存器
        struct pt_regs *old_regs = set_irq_regs(regs);

        /* high bit used in ret_from_ code  */
        //取得中断向量号
        unsigned vector = ~regs->orig_ax;
        unsigned irq;

        //退出idle进程
        exit_idle();
        //进入中断
        irq_enter();

        //中断线号与设备的中断号之间对应关系,由系统分派,分派表是一个per-cpu变量vector_irq
        irq = __get_cpu_var(vector_irq)[vector];

        //处理之
        if (!handle_irq(irq, regs)) {
                //应答APIC
                ack_APIC_irq();

                if (printk_ratelimit())
                        pr_emerg("%s: %d.%d No irq handler for vector (irq %d)\n",
                                __func__, smp_processor_id(), vector, irq);
        }

        //结束中断
        irq_exit();

        set_irq_regs(old_regs);
        return 1;
}

handle_irq函数根据中断号,查找相应的desc结构,调用其handle_irq:

bool handle_irq(unsigned irq, struct pt_regs *regs)
{
        struct irq_desc *desc;
        int overflow;

        overflow = check_stack_overflow();

        desc = irq_to_desc(irq);  //取得irq对应的中断描述符,irq_to_desc函数一开始就已经分析过了
        if (unlikely(!desc))
                return false;

        if (!execute_on_irq_stack(overflow, desc, irq)) {
                if (unlikely(overflow))
                        print_stack_overflow();
                desc->handle_irq(irq, desc);
        }

        return true;
}

如前所述,handle_irq函数指针,指向了handle_level_irq,或者是handle_edge_irq。不论是哪一种,中断电流处理函数在会调用handle_IRQ_event进一步处理,handle_IRQ_event函数的本质是遍历中断号上所有的action,调用其handler。这是在设备驱动初始化时向中断子系统注册的:

/**
* handle_IRQ_event - irq action chain handler
* @irq:        the interrupt number
* @action:        the interrupt action chain for this irq
*
* Handles the action chain of an irq event
*/
irqreturn_t handle_IRQ_event(unsigned int irq, struct irqaction *action)
{
        irqreturn_t ret, retval = IRQ_NONE;
        unsigned int status = 0;

        //因为CPU会禁止中断,这里将其打开,如果没有指定IRQF_DISABLED标志的话,它表示处理程序在中断禁止情况下运行
        if (!(action->flags & IRQF_DISABLED))
                local_irq_enable_in_hardirq();

        //遍历当前irq的action链表中的所有action,调用之
        do {
                //打开中断跟踪
                trace_irq_handler_entry(irq, action);
                //调用中断处理函数
                ret = action->handler(irq, action->dev_id);
                //结束跟踪
                trace_irq_handler_exit(irq, action, ret);

                switch (ret) {
                case IRQ_WAKE_THREAD:
                        /*
                         * Set result to handled so the spurious check
                         * does not trigger.
                         */
                        ret = IRQ_HANDLED;

                        /*
                         * Catch drivers which return WAKE_THREAD but
                         * did not set up a thread function
                         */
                        if (unlikely(!action->thread_fn)) {
                                warn_no_thread(irq, action);
                                break;
                        }

                        /*
                         * Wake up the handler thread for this
                         * action. In case the thread crashed and was
                         * killed we just pretend that we handled the
                         * interrupt. The hardirq handler above has
                         * disabled the device interrupt, so no irq
                         * storm is lurking.
                         */
                        if (likely(!test_bit(IRQTF_DIED,
                                             &action->thread_flags))) {
                                set_bit(IRQTF_RUNTHREAD, &action->thread_flags);
                                wake_up_process(action->thread);
                        }

                        /* Fall through to add to randomness */
                case IRQ_HANDLED:
                        status |= action->flags;
                        break;

                default:
                        break;
                }

                retval |= ret;
                //取得链表中的下一个action,如果有的话
                action = action->next;
        } while (action);

                //如果指定了标志,则使用中断间隔时间为随机数产生器产生熵
        if (status & IRQF_SAMPLE_RANDOM)
                add_interrupt_randomness(irq);
                //关闭中断,do_IRQ进入下一轮循环——等待新的中断到来
        local_irq_disable();

        return retval;
}

中断处理函数的注册 request_irq

很显然,如果驱动程序需要处理与中断相关的工作,它就应该注册一个中断处理程序。也就是构造一个前文所述irqaction,
并挂到前文描述中,中断描述符的链表中去,request_irq API函数完成这一工作,其原型如下:

@irq:要分配的中断号
@hander: 中断处理函数指针,这是工作的核心
@flags:中断标志位,可以是IRQF_DISABLED,IRQF_SAMPLE_RANDOM,IRQF_TIMER,IRQF_SHARED等;
@name:中断设备的文件描述;
@dev:用于中断共享,它提供设备的唯一标识信息

static inline int __must_check
request_irq(unsigned int irq, irq_handler_t handler, unsigned long flags,
            const char *name, void *dev)
{
        return request_threaded_irq(irq, handler, NULL, flags, name, dev);
}

例如,e100驱动中注册其中断处理函数:

static int e100_up(struct nic *nic)
{
        ……
        if ((err = request_irq(nic->pdev->irq, e100_intr, IRQF_SHARED,
                nic->netdev->name, nic->netdev)))
        ……
}

与老的request_irq不同在于,request_irq调用了request_threaded_irq,而不再是setup_irq函数。
这一改变的的理由在于,前者允许传递一个线程处理函数thread_fn,不过request_irq使用传递为NULL:


int request_threaded_irq(unsigned int irq, irq_handler_t handler,
                         irq_handler_t thread_fn, unsigned long irqflags,
                         const char *devname, void *dev_id)
{
        struct irqaction *action;
        struct irq_desc *desc;
        int retval;

        /*
         * handle_IRQ_event() always ignores IRQF_DISABLED except for
         * the _first_ irqaction (sigh).  That can cause oopsing, but
         * the behavior is classified as "will not fix" so we need to
         * start nudging drivers away from using that idiom.
         */
        //标志位检查 
        if ((irqflags & (IRQF_SHARED|IRQF_DISABLED)) ==
                                        (IRQF_SHARED|IRQF_DISABLED)) {
                pr_warning(
                  "IRQ %d/%s: IRQF_DISABLED is not guaranteed on shared IRQs\n",
                        irq, devname);
        }

#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).
         */
        //共享中断,需要指定设备ID 
        if ((irqflags & IRQF_SHARED) && !dev_id)
                return -EINVAL;

        //获取对应的中断描述符
        desc = irq_to_desc(irq);
        if (!desc)
                return -EINVAL;

        //IRQ_NOREQUEST标志意味着中断不能被请求注册
        if (desc->status & IRQ_NOREQUEST)
                return -EINVAL;
        if (!handler)
                return -EINVAL;

        //分配一个irqaction
        action = kzalloc(sizeof(struct irqaction), GFP_KERNEL);
        if (!action)
                return -ENOMEM;

        //初始化之
        action->handler = handler;
        action->thread_fn = thread_fn;
        action->flags = irqflags;
        action->name = devname;
        action->dev_id = dev_id;

        //注册IRQ
        retval = __setup_irq(irq, desc, action);
        if (retval)
                kfree(action);

        //调试操作
#ifdef CONFIG_DEBUG_SHIRQ
        if (irqflags & IRQF_SHARED) {
                /*
                 * It's a shared IRQ -- the driver ought to be prepared for it
                 * to happen immediately, so let's make sure....
                 * We disable the irq to make sure that a 'real' IRQ doesn't
                 * run in parallel with our fake.
                 */
                unsigned long flags;

                disable_irq(irq);
                local_irq_save(flags);

                handler(irq, dev_id);

                local_irq_restore(flags);
                enable_irq(irq);
        }
#endif
        return retval;
}

具体的注册工作落实到了__setup_irq函数:


/*
 * Internal function to register an irqaction - typically used to
 * allocate special interrupts that are part of the architecture.
 */
static int
__setup_irq(unsigned int irq, struct irq_desc *desc, struct irqaction *new)
{
        struct irqaction *old, **old_ptr;
        const char *old_name = NULL;
        unsigned long flags;
        int shared = 0;
        int ret;

        //检查中断描述符其及对应用中断控制器
        if (!desc)
                return -EINVAL;

        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.
         */
        //如果指定了IRQF_SAMPLE_RANDOM,意味着设备将对内核随机数熵池有所贡献,rand_initialize_irq
        //函数处理相应的工作 
        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);
        }

        /*
         * Threaded handler ?
         */
        //如果指定了线程函数,则创建内核线程,并将其thread工作队列指针指向新创建的线程 
        if (new->thread_fn) {
                struct task_struct *t;

                t = kthread_create(irq_thread, new, "irq/%d-%s", irq,
                                   new->name);
                if (IS_ERR(t))
                        return PTR_ERR(t);
                /*
                 * We keep the reference to the task struct even if
                 * the thread dies to avoid that the interrupt code
                 * references an already freed task_struct.
                 */
                get_task_struct(t);
                new->thread = t;
        }

        /*
         * The following block of code has to be executed atomically
         */
        spin_lock_irqsave(&desc->lock, flags);
        old_ptr = &desc->action;
        old = *old_ptr;
        //考虑到一个事实,中断描述符的action链上,可能一个也没有,可能已经注册了一个或多个
        //如果是后者,则需要判断新伙伴是否是允许共享 
        if (old) {
                /*
                 * 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.
                 */
                //这里的验证表明,它使终使用第一个old来匹备,这意味着action链上的所有节点,都拥有相同的类型
                //后面的IRQF_PERCPU也是同样的道理 
                if (!((old->flags & new->flags) & IRQF_SHARED) ||
                    ((old->flags ^ new->flags) & IRQF_TRIGGER_MASK)) {
                        old_name = old->name;
                        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 */
                //遍历到action链末尾,等待注册,这里循环也是使用了指向指针的指针,主要是为了添加新元素
                do {
                        old_ptr = &old->next;
                        old = *old_ptr;
                } while (old);
                //置共享标志,必须的
                shared = 1;
        }

        //如果是共享,则仅需要验证新的action的类型与中断描述符是否一致即可。
        //否则,这意味着中断描述符的action上一无所有,这是一个新伙计,则需要通过新的action,为中断描符述设置一些标志位、状态位等诸如此类
        if (!shared) {

                irq_chip_set_defaults(desc->chip);

                init_waitqueue_head(&desc->wait_for_threads);

                /* Setup the type (level, edge polarity) if configured: */
                if (new->flags & IRQF_TRIGGER_MASK) {
                        ret = __irq_set_trigger(desc, irq,
                                        new->flags & IRQF_TRIGGER_MASK);

                        if (ret)
                                goto out_thread;
                } else
                        compat_irq_chip_set_default_handler(desc);
#if defined(CONFIG_IRQ_PER_CPU)
                if (new->flags & IRQF_PERCPU)
                        desc->status |= IRQ_PER_CPU;
#endif

                desc->status &= ~(IRQ_AUTODETECT | IRQ_WAITING |
                                  IRQ_INPROGRESS | IRQ_SPURIOUS_DISABLED);

                if (!(desc->status & IRQ_NOAUTOEN)) {
                        desc->depth = 0;
                        desc->status &= ~IRQ_DISABLED;
                        desc->chip->startup(irq);
                } else
                        /* Undo nested disables: */
                        desc->depth = 1;

                /* Exclude IRQ from balancing if requested */
                if (new->flags & IRQF_NOBALANCING)
                        desc->status |= IRQ_NO_BALANCING;

                /* Set default affinity mask once everything is setup */
                setup_affinity(irq, desc);

        } else if ((new->flags & IRQF_TRIGGER_MASK)
                        && (new->flags & IRQF_TRIGGER_MASK)
                                != (desc->status & IRQ_TYPE_SENSE_MASK)) {
                /* hope the handler works with the actual trigger mode... */
                pr_warning("IRQ %d uses trigger mode %d; requested %d\n",
                                irq, (int)(desc->status & IRQ_TYPE_SENSE_MASK),
                                (int)(new->flags & IRQF_TRIGGER_MASK));
        }

        //设置对中断号
        new->irq = irq;
        //注册之
        *old_ptr = new;

        /* Reset broken irq detection when installing new handler */
        desc->irq_count = 0;
        desc->irqs_unhandled = 0;

        /*
         * Check whether we disabled the irq via the spurious handler
         * before. Reenable it and give it another chance.
         */
        if (shared && (desc->status & IRQ_SPURIOUS_DISABLED)) {
                desc->status &= ~IRQ_SPURIOUS_DISABLED;
                __enable_irq(desc, irq, false);
        }

        spin_unlock_irqrestore(&desc->lock, flags);

        /*
         * Strictly no need to wake it up, but hung_task complains
         * when no hard interrupt wakes the thread up.
         */
        //如果有内核线程,唤醒之 
        if (new->thread)
                wake_up_process(new->thread);

        //注册proc
        register_irq_proc(irq, desc);
        new->dir = NULL;
        register_handler_proc(irq, new);

        return 0;

mismatch:
#ifdef CONFIG_DEBUG_SHIRQ
        if (!(new->flags & IRQF_PROBE_SHARED)) {
                printk(KERN_ERR "IRQ handler type mismatch for IRQ %d\n", irq);
                if (old_name)
                        printk(KERN_ERR "current handler: %s\n", old_name);
                dump_stack();
        }
#endif
        ret = -EBUSY;

out_thread:
        spin_unlock_irqrestore(&desc->lock, flags);
        if (new->thread) {
                struct task_struct *t = new->thread;

                new->thread = NULL;
                if (likely(!test_bit(IRQTF_DIED, &new->thread_flags)))
                        kthread_stop(t);
                put_task_struct(t);
        }
        return ret;
}

相应的free_irq()也是类似的操作 注册了卸载之类

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章