转载:linux中断申请之request_threaded_irq
在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时,中断是关闭的