S3C2440中的中断处理最终是通过IRQ实现的,在Linux驱动之异常处理体系结构简析已经介绍了IRQ异常的处理过程,最终分析到了一个C函数asm_do_IRQ,接下来继续分析asm_do_IRQ,目标是推导出中断的处理过程。

看到asm_do_irq函数,它位于arch\arm\kernel\Irq.c中。它先根据irq中断号从irq_desc 数组中取出这个中断对应的desc结构体,irq中断号是根据INTOFFSET寄存器的值来确定的,这个寄存器里的值根据中断的来源不同会置位相应的位,它在调用C函数asm_do_IRQ之前被存放在r0中,在C函数中即是irq。

asmlinkage void __exception asm_do_IRQ(unsigned int irq, struct pt_regs *regs)
{
struct pt_regs *old_regs = set_irq_regs(regs);
struct irq_desc *desc = irq_desc + irq;//根据irq中断号求出当前中断的desc结构 /*
* Some hardware gives randomly wrong interrupts. Rather
* than crashing, do something sensible.
*/
if (irq >= NR_IRQS)
desc = &bad_irq_desc; irq_enter();//中断次数的计数 desc_handle_irq(irq, desc);//处理函数 /* AT91 specific workaround */
irq_finish(irq); irq_exit();//退出中断
set_irq_regs(old_regs);
}

然后再调用中断处理函数为desc_handle_irq,接着看到desc_handle_irq函数,它是一个内联函数,它位于include\asm-arm\mach\irq.h

/*
* Obsolete inline function for calling irq descriptor handlers.
*/
static inline void desc_handle_irq(unsigned int irq, struct irq_desc *desc)
{
desc->handle_irq(irq, desc);//调用handle_irq处理中断
}

从中可以得出一个很重要的数据结构irq_desc ,它的结构如下

struct irq_desc {
irq_flow_handler_t handle_irq;//handle_irq处理函数
struct irq_chip *chip;
struct msi_desc *msi_desc;
void *handler_data;
void *chip_data;
struct irqaction *action; /* IRQ action list */action链表
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 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_internodealigned_in_smp;

接着搜索handle_irq函数看看它是在哪里被定义的,经过层层分析,最终找到了调用它的顶层函数init_IRQ,下面列出调用过程:其中init_arch_irq函数是由MACHINE_START这个宏定义的,它在start_kernel->setup_arch(init\Main.c)时已经初始化。它是跟单板结构相关的,详情参考Linux移植之内核启动过程引导阶段分析

init_IRQ
init_arch_irq();//init_arch_irq = mdesc->init_irq,这个是在machine_desc结构中定义的
s3c24xx_init_irq
set_irq_handler
__set_irq_handler
desc->handle_irq = handle;

接着看到init_arch_irq,即s3c24xx_init_irq(arch\arm\plat-s3c24xx\Irq.c)函数:这个函数设置了irq_desc的一些变量,后面就可以调用这些变量。比如说handle_edge_irq即是最终的中断处理函数

    void __init s3c24xx_init_irq(void)
{
...
...
/* 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); //设置irq_desc->chip = &s3c_irq_eint0t4
set_irq_handler(irqno, handle_edge_irq);//设置irq_desc->handler = handle_edge_irq,处理入口函数
set_irq_flags(irqno, IRQF_VALID); //设置irq_desc->status为IRQF_VALID表示可以使用前面定义的这几个函数
}
...
...
}

接着看到handle_edge_irq,它位于kernel\irq\Chip.c中,它的主要功能是首先是清0中断标志,然后运行中断处理函数handle_IRQ_event

    void fastcall
handle_edge_irq(unsigned int irq, struct irq_desc *desc)
{
...
...
/* Start handling the irq */
desc->chip->ack(irq);//清0中断标志,响应中断
...
...
      
if (unlikely((desc->status &
(IRQ_PENDING | IRQ_MASKED | IRQ_DISABLED)) ==
(IRQ_PENDING | IRQ_MASKED))) {
desc->chip->unmask(irq);//开启中断
desc->status &= ~IRQ_MASKED;
} action_ret = handle_IRQ_event(irq, action);//中断处理函数 ...
...
}

继续往下看handle_IRQ_event,它位于kernel\irq\Handle.c,它会不断的检查desc结构里的action结构链表,然后调用里面的处理函数action->handler,函数的参数为irq、atcion->dev_id,这个函数就是真正的处理函数,而这个函数可以由用户定义。

    irqreturn_t handle_IRQ_event(unsigned int irq, struct irqaction *action)
{
...
... do {
ret = action->handler(irq, action->dev_id);//运行handler函数,这个函数是自己构建的
if (ret == IRQ_HANDLED)
status |= action->flags;//设置状态
retval |= ret;
action = action->next;
} while (action);//检查action链表上是否还有函数未执行 ...
...
}

那么action->handler这个函数在哪里被定义呢,接着搜索action->handler被定义的地方,最终发现在request_irq,这个函数位于kernel\irq\Manage.c,

    int request_irq(unsigned int irq, irq_handler_t handler,
unsigned long irqflags, const char *devname, void *dev_id)
{
...
...
action = kmalloc(sizeof(struct irqaction), GFP_ATOMIC);//分配一个action结构体
if (!action)
return -ENOMEM; action->handler = handler;//设置处理函数handler
action->flags = irqflags; //设置标志irqflags。中断方式
cpus_clear(action->mask);
action->name = devname; //设置设备名称
action->next = NULL; //设置下一个action为空
action->dev_id = dev_id; //设置设备id
...
... retval = setup_irq(irq, action);//将上面设备的action结构放入action链表
if (retval)
kfree(action); return retval;
}

这个函数的主要作用是构建一个ation结构,然后用request_irq传入的四个参数初始化它,ation结构如下:

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

最终会调用setup_irq,将初始化的内容放入action链表,这个函数同样位于kernel\irq\Manage.c。它的功能简述为

1、将新的action放入链表

2、设置中断触发方式

3、启动中断

    int setup_irq(unsigned int irq, struct irqaction *new)
{
...
...
/*
286 * The following block of code has to be executed atomically
287 */
spin_lock_irqsave(&desc->lock, flags);
p = &desc->action;//获得当前desc结构的action结构
old = *p;
if (old) {//如果已经存在了action结构
if (!((old->flags & new->flags) & IRQF_SHARED) ||
((old->flags ^ new->flags) & IRQF_TRIGGER_MASK)) {//判断新的action结构与现有的action结构是否使用相同的触发方式,是否是可共享的
old_name = old->name;
goto mismatch;//如果不一样,则跳转到mismatch
} ...
... /* add new interrupt at end of irq queue */
do {//腾出空间,准备将新的结构放入当前的action链表
p = &old->next;
old = *p;
} while (old);
shared = ;
} *p = new;//将新的结构放入当前的action链表 ...
... if (!shared) {
irq_chip_set_defaults(desc->chip);//设置一些chip结构还没设置的指针 #if defined(CONFIG_IRQ_PER_CPU)
if (new->flags & IRQF_PERCPU)
desc->status |= IRQ_PER_CPU;
#endif /* Setup the type (level, edge polarity) if configured: */
if (new->flags & IRQF_TRIGGER_MASK) {
if (desc->chip && desc->chip->set_type)
desc->chip->set_type(irq,
new->flags & IRQF_TRIGGER_MASK);//设置中断的触发方式,配置EXTINTX寄存器以及GPXCON寄存器
...
... if (!(desc->status & IRQ_NOAUTOEN)) {
desc->depth = ;
desc->status &= ~IRQ_DISABLED;
if (desc->chip->startup)
desc->chip->startup(irq);//开始中断
else
desc->chip->enable(irq);//开始中断
} else
/* Undo nested disables: */
desc->depth = ;
}
...
...
}

所有如果要注册一个新的中断,那么可以调用request_irq(unsigned int irq, irq_handler_t handler,unsigned long irqflags, const char *devname, void *dev_id)来注册。

同样的,如果要消除已经注册的中断,需要调用free_irq(unsigned int irq,void *dev_id)函数来注销,它的执行过程与request刚好相反

1、根据中断号irq、dev_iq从action链表中找到表项,然后移除它

2、如果它是唯一的表项,,则调用desc->chip->shutdown(irq);或desc->chip->disable(irq);来关闭irq号的中断。

以上就是整个linux的中断体系结构的简单描述,更复杂的下半部机制后面深入了解后再去描述。

Linux驱动之中断处理体系结构简析的更多相关文章

  1. Linux驱动之异常处理体系结构简析

    异常的概念在单片机中也接触过,它的意思是让CPU可以暂停当前的事情,跳到异常处理程序去执行.以前写单片机裸机程序属于前后台程序,前台指的就是mian函数里的while(1)大循环,后台指的就是产生异常 ...

  2. Linux驱动之输入子系统简析

    输入子系统由驱动层.输入子系统核心.事件处理层三部分组成.一个输入事件,如鼠标移动.键盘按下等通过Driver->Inputcore->Event handler->userspac ...

  3. Linux网络性能优化方法简析

    Linux网络性能优化方法简析 2010-12-20 10:56 赵军 IBMDW 字号:T | T 性能问题永远是永恒的主题之一,而Linux在网络性能方面的优势则显而易见,这篇文章是对于Linux ...

  4. Linux 目录结构学习与简析 Part2

    linux目录结构学习与简析 by:授客 QQ:1033553122 ---------------接Part 1-------------- #1.查看CPU信息 #cat /proc/cpuinf ...

  5. Linux 目录结构学习与简析 Part1

    linux目录结构学习与简析 by:授客 QQ:1033553122 说明: /             linux系统目录树的起点 =============== /bin      User Bi ...

  6. linux驱动之中断处理过程C程序部分

    当发生中断之后,linux系统在汇编阶段经过一系列跳转,最终跳转到asm_do_IRQ()函数,开始C程序阶段的处理.在汇编阶段,程序已经计算出发生中断的中断号irq,这个关键参数最终传递给asm_d ...

  7. linux驱动之中断处理过程汇编部分

    linux系统下驱动中,中断异常的处理过程,与裸机开发中断处理过程非常类似.通过简单的回顾裸机开发中断处理部分,来参考学习linux系统下中断处理流程. 一.ARM裸机开发中断处理过程 以S3C244 ...

  8. Linux内核poll/select机制简析

    0 I/O多路复用机制 I/O多路复用 (I/O multiplexing),提供了同时监测若干个文件描述符是否可以执行IO操作的能力. select/poll/epoll 函数都提供了这样的机制,能 ...

  9. Linux Hugetlbfs内核源码简析-----(二)Hugetlbfs挂载

    本文只讨论执行"mount none /mnt/huge -t hugetlbfs"命令后,mount系统调用的执行过程(基于Linux-3.4.51),不涉及进程相关的细节. m ...

随机推荐

  1. expect脚本实现ssh自动登录

    1:简单的实现ssh登录 #!/usr/bin/expect set ip "10.0.0.142" set user "root" set password ...

  2. Day 12 开放封闭原则,装饰器初识

    nonlocal关键字 # 作用:将 L 与 E(E中的名字需要提前定义) 的名字统一​# 应用场景:如果想在被嵌套的函数中修改外部函数变量(名字)的值​# 案例:​def outer():    n ...

  3. mysqli字符编码

    mysqli 字符编码: 汉字编码: 1.gbk 最久的编码格式,不能写繁体: 2.国内的gb2312: 3.国际的标准:utf-8; 查看数据库的字符编码: show variables like ...

  4. VRay材质练习(一):水、玻璃、牛奶

    软件环境 a) 3ds max 2014b) V-Ray 3.60.03 渲染效果图集 玻璃杯 玻璃杯+水 玻璃杯+牛奶 材质详细参数 一.玻璃材质 Diffuse (0,0,0), Roughnes ...

  5. 学习 MeteoInfo二次开发教程(二)

    1.注意TSB_Select_Click等几个名称要改为toolStripButton2_Click等. 2.以下代码的位置与public Form1()函数并行. ToolStripButton _ ...

  6. JS高级-ES6

    let/const case1 { //js var a = 10 a = 20 // es6 let b = 10 b = 30 const c = 10 c = 40 //报错 } case2 { ...

  7. Vue中table合并单元格用法

    <table> <tr> <th>地名</th> <th>结果</th> <th>人名</th> < ...

  8. python第三方库Requests的基本使用

    Requests 是用python语言编写,基于 urllib,采用 Apache2 Licensed 开源协议的 HTTP 库.它比 urllib 更加方便,可以节约我们大量的工作,完全满足 HTT ...

  9. [持续交付实践] pipeline使用:项目样例

    项目说明 本文将以一个微服务项目的具体pipeline样例进行脚本编写说明.一条完整的pipeline交付流水线通常会包括代码获取.单元测试.静态检查.打包部署.接口层测试.UI层测试.性能专项测试( ...

  10. 实战ELK(4)Metricbeat 轻量型指标采集器

    一.介绍 用于从系统和服务收集指标.从 CPU 到内存,从 Redis 到 Nginx,Metricbeat 能够以一种轻量型的方式,输送各种系统和服务统计数据. 1.系统级监控,更简洁(轻量型指标采 ...