linux中断申请之request_threaded_irq【转】
转自:http://blog.chinaunix.net/xmlrpc.php?r=blog/article&uid=21977330&id=3755609
在linux里,中断处理分为顶半(top half),底半(bottom half),在顶半里处理优先级比较高的事情,要求占用中断时间尽量的短,在处理完成后,就激活底半,有底半处理其余任务。底半的处理方式主要有soft_irq, tasklet, workqueue三种,他们在使用方式和适用情况上各有不同。soft_irq用在对底半执行时间要求比较紧急或者非常重要的场合,主要为一些subsystem用,一般driver基本上用不上。 tasklet和work queue在普通的driver里用的相对较多,主要区别是tasklet是在中断上下文执行,而work queue是在process上下文,因此可以执行可能sleep的操作。
request_threaded_irq()是Linux kernel 2.6.30 之后新加的irq handler API
如何确定可以用到 request_threaded_irq() ?
Linux kernel config 需要定义CONFIG_GENERIC_HARDIQS
kernel config 才有支援threaded irq
Moving interrupts to threads 介绍request_threaded_irq() 的由来
http://lwn.net/Articles/302043/
从realtime tree 移植而来,为了减少kernel 因为要等待每一个硬件中断处理的时间
,就另外交给kernel thread 处理中断后续工作。
优点:
1 减少 kernel 延迟时间
2 避免处理中断时要分辨是在硬体中断或软体中断?
3 更容易为kernel 中断处理除错,可能可完全取代tasklet
原本的中断处理分上半部(硬体中断处理,必须关闭中断无法处理新的中断)跟下半部(
软体中断处理),因此上半部的硬体中断处理必须尽可能简短,让系统反应速度更快。
request_threaded_irq 是在将上半部的硬件中断处理缩短为只确定硬体中断来
自我们要处理的装置,唤醒kernel thread 执行后续中断任务。
缺点:
对于非irq 中断的kernel threads ,需要在原本task_struct 新增struct
irqaction 多占 4/8 bytes 记忆体空间
linux kernel 2.6.29 之后(2.6.30)加入request_threaded_irq
跟传统top/bottom havles 的差异是threaded_irq 受Linux kernel system
的 process scheduling 控制,不会因为写错的bottom half 代码造成整个系统
延迟的问题。
也可以透过RT/non RT 跟nice 等工具调整各个thread 优先权,丢给使用率较低的
cpu 以及受惠于kernel 原本可以对threads 做的各种控制,包括但不限于sleep,
lock, allocate 新的记忆体区块。
受惠最大的是shared irq line 的多个中断处理。除了可以加速共享中断造成的延迟
,threaded_irq 也可以降低在同一段程式码处理多个装置中断的复杂度。
threaded irq 在使用性上也比tasklet(接着top half 直接执行,无法sleep)
/workqueue(kernel context?) 等需要在top half 增加跟bottom half 连结与沟通
的麻烦。
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)
IRQF_SHARED 共享中断时,dev_id不能为空,因为释放irq时要区分哪个共享中断
irq:中断号
handler:发生中断时首先要执行的硬中断处理函数,这个函数可以通过返回 IRQ_WAKE_THREADED唤醒中断线程,也可
返回IRQ_HANDLE不执行中断线程
thread_fn : 中断线程,类似于中断下半部
后三个参数与request_irq中的一致
关于IRQF_ONESHOT, 直到线程函数执行完毕才会开启该中断
IRQF_ONESHOT:Interrupt is not reenabled after the hardirq handler finished.
Used by threaded interrupts which need to keep the irq line disabled until
the threaded handler has been run. 这里linus在邮件列表里指明IRQF_ONESHOT 的原因
Making the IRQF_ONESHOT explicit does two things:
- it makes people who read the code *aware* of things
- if/when you have irq conflicts and two drivers want to attach to
the same interrupt, at least you can see directly from the source what
flags they used (and again, not have to even *think* about it). IRQF_ONESHOT 与 IRQF_SHARED 不能同时使用
当多个设备共享中断时,由于IRQF_ONESHOT会关闭中断线程的中断,而线程一般执行时间会比较长,所以是不允许的
当hardirq函数为NULL时,必须声明IRQF_ONESHOT, 表示threadirq线程中关闭该中断,在某些情况下,这个标志会非常有用
例如:设备是低电平产生中断,而硬中断函数为NULL,如果不使用IRQF_ONESHOT,就会一直产生中断执行NULL函数,中断线程
得不到执行,声明IRQF_ONESHOT后,会执行完线程才使能该中断
点击(此处)折叠或打开
- /*
- * gpio_irqTest.c
- * PB27 receive this signal as IRQ and make the LED linking on PB17 turn on or turn off
- *
- */
- #include <linux/types.h>
- #include <linux/kernel.h>
- #include <linux/module.h>
- #include <linux/init.h>
- #include <linux/platform_device.h>
- #include <linux/cdev.h>
- #include <linux/ioctl.h>
- #include <linux/fs.h>
- #include <linux/gpio.h>
- #include <linux/delay.h>
- #include <linux/cdev.h>
- #include <linux/interrupt.h>
- #include <asm/io.h>
- #include <asm/io.h>
- #include <mach/gpio.h>
- #include <mach/hardware.h>
- #include <mach/board.h>
- #include <mach/gpio.h>
- #include <mach/at91_pio.h>
- #include <mach/at91_aic.h>
- #include <mach/at91_pmc.h>
- void led_on()
- {
- // at91_set_gpio_output(AT91_PIN_PB17,1);
- printk("led on\n");
- }
- void led_off()
- {
- // at91_set_gpio_output(AT91_PIN_PB17 ,0);
- printk("led off.\n");
- }
- struct light_dev *light_devp;
- int light_major = 200;
- struct light_dev
- {
- struct cdev cdev;
- unsigned char value;
- };
- static void io_init(void)
- {
- at91_set_GPIO_periph(AT91_PIN_PB27, 0);
- at91_set_gpio_input(AT91_PIN_PB27, 1);
- at91_set_deglitch(AT91_PIN_PB27, 1);
- }
- struct gpio_irq_desc
- {
- int pin;
- int irq;
- unsigned long flags;
- char *name;
- };
- static struct gpio_irq_desc gpio_irq={AT91_PIN_PB27, AT91_PIN_PB27,IRQF_TRIGGER_FALLING|IRQF_TRIGGER_RISING|IRQF_ONESHOT,"PB27"};
- static irqreturn_t gpio_irqhandler(int irq, void *dev_id)
- {
- printk(KERN_INFO "In hard irq handler.\n");
- return IRQ_WAKE_THREAD;
- }
- static irqreturn_t gpio_threadhandler(int irq, void *dev_id)
- {
- int rst;
- rst = at91_get_gpio_value(gpio_irq.pin);
- printk(KERN_INFO "gpio stat: %d\n", rst);
- if(rst == 0){
- led_on();
- }else{
- led_off();
- }
- printk(KERN_INFO "sleep 3000ms\n");
- msleep(3000);
- printk(KERN_INFO "awake after sleep\n");
- return IRQ_HANDLED;
- }
- int light_open(struct inode *inode,struct file *filp)
- {
- int err;
- struct light_dev *dev;
- dev = container_of(inode->i_cdev,struct light_dev,cdev);
- filp->private_data = dev;
- printk(KERN_DEBUG "%s", __FUNCTION__);
- io_init();
- // err = request_threaded_irq(gpio_irq.irq,gpio_irqhandler,gpio_threadhandler,gpio_irq.flags,gpio_irq.name,(void*)0);
- err = request_threaded_irq(gpio_irq.irq,NULL,gpio_threadhandler,gpio_irq.flags,gpio_irq.name,(void*)0);
- if(err)
- {
- // free_irq(gpio_irq.irq,(void*)0);
- printk(KERN_DEBUG "request irq failed.\n");
- return -EBUSY;
- }
- return 0;
- }
- int light_release(struct inode *inode,struct file *filp)
- {
- free_irq(gpio_irq.irq,(void*)0);
- return 0;
- }
- int light_ioctl(struct inode *inode,struct file *filp,unsigned int cmd, unsigned long arg)
- {
- struct light_dev *dev = filp->private_data;
- switch(cmd)
- {
- case 0:
- at91_set_gpio_output(AT91_PIN_PB19,0);
- break;
- case 1:
- at91_set_gpio_output(AT91_PIN_PB19,1);
- led_off();
- break;
- default:
- return -ENOTTY;
- // break;
- }
- return 0;
- }
- struct file_operations light_fops =
- {
- .owner = THIS_MODULE,
- .open = light_open,
- .release = light_release,
- .unlocked_ioctl = light_ioctl,
- };
- static void light_setup_cdev(struct light_dev *dev,int index)
- {
- int err,devno = MKDEV(light_major,index);
- cdev_init(&dev->cdev,&light_fops);
- dev->cdev.owner = THIS_MODULE;
- dev->cdev.ops = &light_fops;
- err = cdev_add(&dev->cdev,devno,1);
- if(err)
- {
- printk(KERN_NOTICE "Error %d adding LED%d",err,index);
- }
- }
- int __init light_init(void)
- {
- int result;
- dev_t dev = MKDEV(light_major,0);
- if(light_major)
- {
- result = register_chrdev_region(dev,1,"gpio");
- }
- if(result < 0)
- {
- printk(KERN_DEBUG "%s: register char dev failed.\n", __FUNCTION__);
- return result;
- }
- light_devp = kmalloc(sizeof(struct light_dev),GFP_KERNEL);
- if(!light_devp)
- {
- result = - ENOMEM;
- goto fail_malloc;
- }
- memset(light_devp,0,sizeof(struct light_dev));
- light_setup_cdev(light_devp,0);
- printk(KERN_DEBUG "%s done\n", __FUNCTION__);
- return 0;
- fail_malloc:unregister_chrdev_region(dev,light_devp);
- return result;
- }
- void __exit light_cleanup(void)
- {
- cdev_del(&light_devp->cdev);
- kfree(light_devp);
- unregister_chrdev_region(MKDEV(light_major,0),1);
- }
- module_init(light_init);
- module_exit(light_cleanup);
- MODULE_AUTHOR("Enzo Fang");
- MODULE_LICENSE("Dual BSD/GPL");
结论:
使用
request_threaded_irq(gpio_irq.irq,gpio_irqhandler,gpio_threadhandler,gpio_irq.flags,gpio_irq.name,(void*)0);
hardirq和thread_fn同时出现时,处理thread_fn时该中断是打开的
err = request_threaded_irq(gpio_irq.irq,NULL,gpio_threadhandler,gpio_irq.flags,gpio_irq.name,(void*)0);
但hardirq和thread_fn只有一个存在时,处理thread_fn时,中断是关闭的
linux中断申请之request_threaded_irq【转】的更多相关文章
- linux中断申请之request_threaded_irq
转自:http://blog.chinaunix.net/xmlrpc.php?r=blog/article&uid=21977330&id=3755609 在linux里,中断处理分 ...
- linux中断申请之request_threaded_irq 【转】
转自:http://blog.chinaunix.net/xmlrpc.php?r=blog/article&uid=21977330&id=3755609 在linux里,中断处理分 ...
- Linux中断管理 (1)Linux中断管理机制
目录: <Linux中断管理> <Linux中断管理 (1)Linux中断管理机制> <Linux中断管理 (2)软中断和tasklet> <Linux中断管 ...
- Linux中断(interrupt)子系统之四:驱动程序接口层 & 中断通用逻辑层【转】
转自:http://blog.csdn.net/droidphone/article/details/7497787 在本系列文章的第一篇:Linux中断(interrupt)子系统之一:中断系统基本 ...
- Linux中断(interrupt)子系统之一:中断系统基本原理【转】
转自:http://blog.csdn.net/droidphone/article/details/7445825 这个中断系列文章主要针对移动设备中的Linux进行讨论,文中的例子基本都是基于AR ...
- Linux中断 - High level irq event handler
一.前言 当外设触发一次中断后,一个大概的处理过程是: 1.具体CPU architecture相关的模块会进行现场保护,然后调用machine driver对应的中断处理handler 2.mach ...
- linux中断线程化分析【转】
转自:http://blog.csdn.net/qq405180763/article/details/24120895 版权声明:本文为博主原创文章,未经博主允许不得转载. 最近在为3.8版本的Li ...
- Linux中断(interrupt)子系统之一:中断系统基本原理
这个中断系列文章主要针对移动设备中的Linux进行讨论,文中的例子基本都是基于ARM这一体系架构,其他架构的原理其实也差不多,区别只是其中的硬件抽象层.内核版本基于3.3.虽然内核的版本不断地提升,不 ...
- Linux中断管理 (1)Linux中断管理机制【转】
转自:https://www.cnblogs.com/arnoldlu/p/8659981.html 目录: <Linux中断管理> <Linux中断管理 (1)Linux中断管理机 ...
随机推荐
- msgpack生成lib,vs新建lib等
记录导师交给的任务 新建一个c++项目,运行老师的msgpack的cpp文件,然后会生成相应的lib,我做的东西需要调用到它(这是老师改写后的msgpack的lib) 我的任务是建一个静态库,将客户端 ...
- 深入理解es6的promise
一.promise入门 1. Promise对象是什么 回调函数的另一种原生实现,比之前回调函数的写法机构清晰,功能强大, 2.以前回调这么写 function a(fn){ let h = 1; s ...
- #pragma once 与 #ifndef 的使用
为了防止头文件被重复包含,主要有两种方式: 方式一:使用 #ifndef #ifndef OPTIONAL_TEST_H #define OPTIONAL_TEST_H //............. ...
- Luogu4717 【模板】快速沃尔什变换(FWT)
https://www.cnblogs.com/RabbitHu/p/9182047.html 完全没有学证明的欲望因为这个实在太好写了而且FFT就算学过也忘得差不多了只会写板子 #include&l ...
- native2ascii -reverse -encoding UTF-8 validation_msg.properties > validation_msg_src.properties
native2ascii -reverse -encoding UTF-8 validation_msg.properties > validation_msg_src.properties
- MT【165】分段函数
(2018浙江省赛12题改编)设$a\in R$,且对任意的实数$b$均有$\max\limits_{x\in[0,1]}|x^2+ax+b|\ge\dfrac{1}{4}$求$a$ 的范围. 提示: ...
- MT【83】三个等号
分析:此类三个等式的一般做法先记为$t$,则有如下做法:
- 【刷题】HDU 5883 The Best Path
Problem Description Alice is planning her travel route in a beautiful valley. In this valley, there ...
- C++ pbds 库平衡树(tree)
头文件 #include <ext/pb_ds/assoc_container.hpp> #include <ext/pb_ds/tree_policy.hpp> //或者直接 ...
- 洛谷P3950 部落冲突(LCT)
洛谷题目传送门 最无脑LCT题解,Dalao们的各种算法都比这个好多啦... 唯一的好处就是只管码代码就好了 开战cut,停战link,询问findroot判连通性 太无脑,应该不用打注释了.常数大就 ...