内核中的中断处理模型

内核版本: Linux 2.6.19 

Kernel中断处理模型结构图如下:

下面简单介绍一下:

1. Linux定义了名字为irq_desc的中断例程描述符表:(include/linux/irq.h)

    struct irqdesc irq_desc[NR_IRQS];

NR_IRQS表示中断源的数目。

2. irq_desc[]是一个指向irq_desc结构的数组, irq_desc结构是各个设备中断服务例程的描述符。

 

struct irq_desc {
     irq_flow_handler_t handle_irq;
     struct irq_chip  *chip;
     void   *handler_data;
     void   *chip_data;
     struct irqaction *action; 
     unsigned int  status;  

     unsigned int  depth;  
     unsigned int  wake_depth; 
     unsigned int  irq_count; 
     unsigned int  irqs_unhandled;
     spinlock_t  lock;
#ifdef CONFIG_SMP
     cpumask_t  affinity;
     unsigned int  cpu;
#endif
#if defined(CONFIG_GENERIC_PENDING_IRQ) || defined(CONFIG_IRQBALANCE)
     cpumask_t  pending_mask;
#endif
#ifdef CONFIG_PROC_FS
     struct proc_dir_entry *dir;
#endif
     const char  *name;
} ____cacheline_aligned;

 

 

 

Irq_desc结构体中的成员action指向该中断号对应的irqaction结构体链表。Irqaction结构体定义如下:

 

// include/linux/interrupt.h
struct irqaction {
    irq_handler_t handler;   // 指向中断服务程序 
    unsigned long flags;     // 中断标志 
    unsigned long mask;      // 中断掩码 
    const char *name;        // I/O设备名

     void *dev_id;            // 设备标识
    struct irqaction *next;  // 指向下一个描述符

    int irq;                 // IRQ线 
    struct proc_dir_entry *dir; // 指向IRQn相关的/proc/irq/n目录的描述符
};

 

  

   其中关键的handler成员指向了该设备的中断服务程序,由执行request_irq时建立。

 

3.  在驱动程序初始化时,若使用到中断,通常调用函数request_irq()建立该驱动程序对应的irqaction结构体,并把它登记到irq_desc [irq_num]->action链表中。Iqr_num为驱动程序申请的中断号。

request_irq()函数的原型如下:

 

// kernel/irq/manage.c
int request_irq(unsigned int irq,
        irqreturn_t (*handler)(int, void *, struct pt_regs *),
        unsigned long irqflags,
        const char *devname,
        void *dev_id);

 

 

    参数irq是设备中断求号,在向irq_desc []数组登记时,它做为数组的下标。把中断号为irq的irqaction结构体的首地址写入irq_desc [irq]->action。这样就把设备的中断请求号与该设备的中断服务例程irqaction联系在一起了。

    这样当CPU接收到中断请求后,就可以根据中断号通过irq_desc []找到该设备的中断服务程序。流程如上图所示。

 

4. 关于共享中断

共享中断的不同设备的iqraction结构体都会添加进该中断号对应的irq_desc结构体的action成员所指向的irqaction链表内。当内核发生中断时,它会依次调用该链表内所有的handler函数。因此,若驱动程序需要使用共享中断机制,其中断处理函数必须有能力识别是否是自己的硬件产生了中断。通常是通过读取该硬件设备提供的中断flag标志位进行判断。

原文地址:https://www.cnblogs.com/ranson7zop/p/7567168.html