天天看點

irq中斷子系統

參考:http://blog.csdn.net/adaptiver/article/details/6834337

1 因為當中斷發生的時候系統由中斷門 進入時自動關中斷(對于x86平台就是将eflags寄存器的if位置為0),在irq_exit中恢複中斷

2 貌似在handle_edge_irq中也有對中斷關閉和打開的操作

3 兩點說明:一是因為linux不支援 中斷優先級,是以任何中斷都可以搶占其他中斷,但是同種類型的中斷(即定義使用同一個 中斷線的中斷)不會發生搶占,他們會在執行本類型中斷的時候依次被調用執行。二是所謂 “隻要發生中斷,就可以搶占核心”這句是有一定限制的,因為當中斷發生的時候系統由中斷門 進入時自動關中斷(對于x86平台就是将eflags寄存器的if位置為0),隻有當中斷函數被執行 (handle_IRQ_event)的過程中開中斷之後才能有搶占。

對于linux系統而言,中斷發生過程為:

根據中斷号跳轉至規定的中斷向量表的位置–>保護現場,并且轉換模式至SVC32–>根據向量表得訓示進一步跳轉至處理函數處–>進一步跳轉__irq_svc,并儲存那些寄存器供中斷傳回時使用–>irq_handler中斷處理–>

進入c語言的asm_do_IRQ–>generic_handle_irq(irq)–> desc->handle_irq(irq, desc)即handle_edge_irq等–>屏蔽同一個中斷,清中斷,使能本中斷–>handle_IRQ_event–>action->handler(irq, action->dev_id)注冊的中斷處理函數–>中斷傳回irq_exit–>如果還有軟中斷沒執行則invoke_softirq()–>現場恢複

通過中斷産生結合函數來說明中斷發生過程:

1、重要結構

/include/linux/irq.h

irq_desc 核心中記錄一個irq_desc的數組,數組的每一項對應一個中斷或者一組中斷使用同一個中斷号,一句話irq_desc幾乎記錄所有中斷相關的東西,這個結構是中斷的核心。其中包括倆個重要的結構irq_chip 和irqaction 。

這個結構體作用與gpio_desc很像,都是一個數組,然後通過索引找到相應的chip等資訊。

struct irq_desc {
        unsigned int            irq;                                              中斷号
        struct timer_rand_state *timer_rand_state;
        unsigned int            *kstat_irqs;
#ifdef CONFIG_INTR_REMAP
        struct irq_2_iommu      *irq_2_iommu;
#endif
        irq_flow_handler_t      handle_irq;                                          //中斷處理入口函數
        struct irq_chip         *chip;                                               //irq中斷子系統接口函數
        struct msi_desc         *msi_desc;
        void                    *handler_data;                                        
        void                    *chip_data;
        struct irqaction        *action;        /* IRQ action list */                 //注冊中斷時将中斷函數挂在這個連結清單上面
        unsigned int            status;         /* IRQ status */

        unsigned int            depth;          /* nested irq disables */
        unsigned int            wake_depth;     /* nested wake enables */
        unsigned int            irq_count;      /* For detecting broken IRQs */
        unsigned long           last_unhandled; /* Aging timer for unhandled count */
        unsigned int            irqs_unhandled;
        raw_spinlock_t          lock;
#ifdef CONFIG_SMP
        cpumask_var_t           affinity;
        const struct cpumask    *affinity_hint;
        unsigned int            node;
#ifdef CONFIG_GENERIC_PENDING_IRQ
        cpumask_var_t           pending_mask;
#endif
#endif
        atomic_t                threads_active;
        wait_queue_head_t       wait_for_threads;
#ifdef CONFIG_PROC_FS
        struct proc_dir_entry   *dir;
#endif
        const char              *name;
} ____cacheline_internodealigned_in_smp;

接下來看看irq中斷子系統接口函數
struct irq_chip {  
    const char  *name;  
    unsigned int    (*startup)(unsigned int irq); 啟動中斷  
    void        (*shutdown)(unsigned int irq); 關閉中斷  
    void        (*enable)(unsigned int irq);   使能中斷  
    void        (*disable)(unsigned int 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);  

    void        (*end)(unsigned int irq);  
    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); 設定中斷類型,其中包括設定GPIO口為中斷輸入  
    int     (*set_wake)(unsigned int irq, unsigned int on);  

    void        (*bus_lock)(unsigned int irq);  上鎖函數  
    void        (*bus_sync_unlock)(unsigned int irq); 解鎖  

    /* 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;  
};  
           

我們可以看到這裡實作的是一個架構,需要我們進一步的填充裡面的函數。我們在分析另一個結構irqaction

include/linux/interrupt.h

struct irqaction {  
    irq_handler_t handler;  使用者注冊的中斷處理函數  
    unsigned long flags;    中斷辨別  
    const char *name;       使用者注冊的中斷名字,cat/proc/interrupts時可以看到  
    void *dev_id;           可以是使用者傳遞的參數或者用來區分共享中斷  
    struct irqaction *next; irqaction結構鍊,一個共享中斷可以有多個中斷處理函數  
    int irq;                中斷号  
    struct proc_dir_entry *dir;  
    irq_handler_t thread_fn;  
    struct task_struct *thread;  
    unsigned long thread_flags;  
};  

一步一步分析request_irq
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);
}

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;

        /*
         * 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).
         */
        if ((irqflags & IRQF_SHARED) && !dev_id)
                return -EINVAL;

        desc = irq_to_desc(irq);                                            根據irq找到初始化的irq_desc結構體,後面有這個結構體的初始化程式講解
        if (!desc)
                return -EINVAL;

        if (desc->status & IRQ_NOREQUEST)                                  如果不能被請求了
                return -EINVAL;

        if (!handler) {                                                        如果沒有定義中斷處理函數
                if (!thread_fn)                                                                     
                        return -EINVAL;
                handler = irq_default_primary_handler;                          則給一個預設的函數給handler
        }

        action = kzalloc(sizeof(struct irqaction), GFP_KERNEL);                 申請一個action結構體
        if (!action)
                return -ENOMEM;

        action->handler = handler;                                              以下對結構體的各個成員進行指派
        action->thread_fn = thread_fn;
        action->flags = irqflags;
        action->name = devname;
        action->dev_id = dev_id;

        chip_bus_lock(irq, desc);
        retval = __setup_irq(irq, desc, action);                               見下講解
        chip_bus_sync_unlock(irq, desc);

        if (retval)
                kfree(action);
        return retval;
}
EXPORT_SYMBOL(request_threaded_irq);



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 nested, shared = ;
        int ret;

        if (!desc)                                                             
                return -EINVAL;

        if (desc->chip == &no_irq_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);
        }

        /* Oneshot interrupts are not allowed with shared */
        if ((new->flags & IRQF_ONESHOT) && (new->flags & IRQF_SHARED))        Oneshot不允許共享中斷   
                return -EINVAL;

        /*
         * Check whether the interrupt nests into another interrupt
         * thread.
         */
        nested = desc->status & IRQ_NESTED_THREAD;                               是否定義了嵌套标志,顯然不執行
        if (nested) {
                if (!new->thread_fn)
                        return -EINVAL;
                /*
                 * Replace the primary handler which was provided from
                 * the driver for non nested interrupt handling by the
                 * dummy function which warns when called.
                 */
                new->handler = irq_nested_primary_handler;
        }
        if (new->thread_fn && !nested) {                                          是否有線程函數定義 //不執行
                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
         */
        raw_spin_lock_irqsave(&desc->lock, flags);
        old_ptr = &desc->action;                                                    從舊的desc結構體中取出原來的action
        old = *old_ptr;
        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.
                 */
                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 */
                do {
                        old_ptr = &old->next;                                      在已經挂載action連結清單末尾還沒最後為空的位置,供新的action來挂載
                        old = *old_ptr;
                } while (old);
                shared = ;
        }

        if (!shared) {
                irq_chip_set_defaults(desc->chip);                                     如果個别沒有設定的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_ONESHOT |
                                  IRQ_INPROGRESS | IRQ_SPURIOUS_DISABLED);

                if (new->flags & IRQF_ONESHOT)
                        desc->status |= IRQ_ONESHOT;

                if (!(desc->status & IRQ_NOAUTOEN)) {
                        desc->depth = ;
                        desc->status &= ~IRQ_DISABLED;                             設定狀态
                        desc->chip->startup(irq);                                   開啟中斷
                } else
                        /* Undo nested disables: */
                        desc->depth = ;

                /* 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;                                                            在irq所屬的desc結構體将新的action 挂到連結清單中

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

         */
        if (shared && (desc->status & IRQ_SPURIOUS_DISABLED)) {
                desc->status &= ~IRQ_SPURIOUS_DISABLED;
                __enable_irq(desc, irq, false);
        }

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

        register_irq_proc(irq, desc);                                            在proc下建立irq檔案節點         
        new->dir = NULL;
        register_handler_proc(irq, new);                                           在proc下建立handler檔案節點

        return ;

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:
        raw_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;
}
           

以上一共做了:建立action結構體,從desc結構體中取出原來的daction結構體,将新的action結構體挂載至原來的acton連結清單中。

__setup_irq内容比較多,主要完成幾個方面的工作

1.添加irqaction結構到irq_desc的action連結清單中,需要判斷是否為共享中斷,隻有共享中斷可以添加多個中斷處理函數,如果是共享中斷,則要檢查中斷處理函數是否和連結清單中其他函數的觸發方式等是否相同,隻有一緻才可以添加到連結清單中。

2.設定一些irq_chip結構中的函數指針指向預設函數

3.設定中斷的觸發方式和啟動中斷

<一>中斷初始化流程

下面内容轉載他人部落格,原文位址 http://blog.chinaunix.net/space.php?uid=15193587&do=blog&cuid=2194431

下面我們分析核心中斷初始化的過程以及如何調用到一個新平台的irq初始化函數。

這裡我們以s3c2410平台為例,他的中斷初始化函數定義在:

void __init s3c24xx_init_irq(void)

{

……

}

在arch/arm/mach-s3c2410/mach-smdk2410.c内通過MACHINE_START宏将s3c24xx_init_irq指派給mach_desc結構體的.init_irq成員。

MACHINE_START(SMDK2410,"SMDK2410")/* @TODO: request a new identifier and switch
        * to SMDK2410 */
 /* Maintainer: Jonas Dietsche */
 .phys_io = S3C2410_PA_UART,
 .io_pg_offst =(((u32)S3C24XX_VA_UART)>> )& ,
 .boot_params = S3C2410_SDRAM_PA+ ,
 .map_io = smdk2410_map_io,
 .init_irq = s3c24xx_init_irq,
 .init_machine = smdk_machine_init,
 .timer = &s3c24xx_timer,
MACHINE_END

注:MACHINE_START宏的作用是對mach_desc結構體進行初始化。mach_desc裡定義了一些關鍵的體系架構相關的函數。Porting kernel到新平台時,這個結構體是非常關鍵的。

init_irq這個成員在系統初始化的時候會被指派給init_arch_irq全局變量,如下:
/* arch/arm/kernel/setup.c */
void __init setup_arch(char**cmdline_p)
{
 ……
 cpu_init();
 /*
  * Set up various architecture-specific pointers
  */
 init_arch_irq = mdesc->init_irq;
 system_timer = mdesc->timer;
 init_machine = mdesc->init_machine;
 ……
}
           

注:可以看到這裡不僅初始化了init_arch_irq 全局變量,同時初始化了system_timer,init_machine等全局變量。這是kernel支援多平台的一種機制。當然這裡system_timer和init_machine我不多描述,有興趣的可以大家自己去看。機制和init_arch_irq大同小異。

init_arch_irq函數指針定義在體系架構無關的arch/arm/kernel/irq.c内

void (*init_arch_irq)(void) __initdata = NULL;

并且在init_IRQ函數内會去執行它。

/* arch/arm/kernel/irq.c */
void __init init_IRQ(void)
{
 int irq;
 for (irq = ; irq < NR_IRQS; irq++)
  irq_desc[irq].status|= IRQ_NOREQUEST| IRQ_DELAYED_DISABLE|                  //desc狀态進行标志
   IRQ_NOPROBE;
#ifdef CONFIG_SMP
 bad_irq_desc.affinity = CPU_MASK_ALL;
 bad_irq_desc.cpu = smp_processor_id();
#endif
 init_arch_irq();
}
           

那init_IRQ在哪裡被調用呢? 我們猜想肯定是在系統初始化的時會調用它。

實際結果也正是,init_IRQ會在init/main.c裡的start_kernel函數内被調用:

asmlinkage void __init start_kernel(void)
{
 ……
 trap_init();
 rcu_init();
 init_IRQ();
 pidhash_init();
 clockevents_init();
 init_timers();
 ……
}
           

init_arch_irq 函數指針,說明的是這是個全局的函數指針,跟體系結構相關。我接着上面的繼續分析一下s3c24xx_init_irq()這個函數

void __init s3c24xx_init_irq(void)
{
        unsigned long pend;
        unsigned long last;
        int irqno;
        int i;

#ifdef CONFIG_FIQ
        init_FIQ();
#endif

        irqdbf("s3c2410_init_irq: clearing interrupt status flags\n");

        /* first, clear all interrupts pending... */

        last = ;
        for (i = ; i < ; i++) {
                pend = __raw_readl(S3C24XX_EINTPEND);                                     清中斷操作

                if (pend ==  || pend == last)
                        break;

                __raw_writel(pend, S3C24XX_EINTPEND);
                printk("irq: clearing pending ext status %08x\n", (int)pend);
                last = pend;
        }

        last = ;
        for (i = ; i < ; i++) {
                pend = __raw_readl(S3C2410_INTPND);

                if (pend ==  || 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 = ;
        for (i = ; i < ; i++) {
                pend = __raw_readl(S3C2410_SUBSRCPND);

                if (pend ==  || pend == last)
                        break;

                printk("irq: clearing subpending status %08x\n", (int)pend);
                __raw_writel(pend, S3C2410_SUBSRCPND);
                last = pend;
        }

        /* register the main interrupts */
        for (irqno = IRQ_EINT; irqno <= IRQ_EINT3; irqno++) {
                irqdbf("registering irq %d (ext int)\n", irqno);
                set_irq_chip(irqno, &s3c_irq_eint0t4);                          将chip結構體初始化進desc中
                set_irq_handler(irqno, handle_edge_irq);                         将handler函數初始化進desc中
                set_irq_flags(irqno, IRQF_VALID);                                 清除狀态标志位IRQ_NOREQUEST
        }
。。。。。
}
           

1 上面的for循環,循環清除中斷辨別,而且設定了一個last變量,不知道是不是為了防止一次擦除中斷失敗。

2 首先是填充irq_chip結構,然後是設定中斷的處理函數入口,這個響應中斷的時候就通過函數入口調用使用者注冊的中斷處理函數,中斷辨別設定為可以使用。

以上參考:http://blog.csdn.net/yimu13/article/details/6803957?locationNum=5

接下來看看中斷處理過程,即當發生中斷時開始一系列過程。

參考:https://wenku.baidu.com/view/6c6e30634b35eefdc9d33300.html

繼續閱讀