Linux子系统之中断机制研究

欢迎进入Linux社区论坛,与200万技术人员互动交流 >>进入

1 Linux中断机制

1.1 认识三个重要的数据结构

1.1.1 中断描述符结构irq_desc

irq_desc:中断全局描述符

struct irq_desc {

unsigned int

irq;注释:表示此中断描述符的中断号

struct timer_rand_state *timer_rand_state;注释:timer伪随机数状态结构指针

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;注释:中断的硬件chip级描述符,提供底层的硬件访问。具体见下面结构描述

struct msi_desc

*msi_desc;

void

*handler_data;

void

*chip_data;

struct irqaction

*action;

/* IRQ action list */

注释:标识当出现IRQ时要调用的中断服务例程。该字段指向IRQ的irqaction链表的第一个元素。

unsigned int

status;

/* IRQ status */

注释:描述IRQ线状态的一组标志

unsigned int

depth;

/* nested irq disables */

注释:如果IRQ线被激活,则显示0,如果IRQ线被禁止了不止一次,则显示一个正数。

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;

Linux内核将所有的中断统一编号,使用一个irq_desc结构数组来描述这些中断;每个数组项对应一个中断,也可能是一组中断,它们共用相同的中断号,里面记录了中断的名称、中断状态、中断标记(比如中断类型、是否共享中断等),并提供了中断的低层硬件访问函数(清除、屏蔽、使能中断),提供了这个中断的处理函数入口,通过它可以调用用户注册的中断处理函数。

1.1.2

中断服务描述符irqactionirqaction:每中断服务描述符

struct irqaction {

irq_handler_t handler;注释:用户定义的当前中断的驱动中断处理函数

unsigned long flags;注释:中断标志

const char *name;

void *dev_id;

struct irqaction *next;

int irq;

struct proc_dir_entry *dir;

irq_handler_t thread_fn;

struct task_struct *thread;

unsigned long thread_flags;

};

irq_desc结构中的irqaction结构类型在include/linux/iterrupt.h中定义。用户注册的每个中断

处理函数用一个irqaction结构来表示,一个中断比如共享中断可以有多个处理函数,它们的irqaction结构链接成一个链表,以action为表头。他是在用户request_irq的时候第一次创建并被初始化。

1.1.3

中断硬件描述符irq_chipirq_chip结构类型是在include/linux/irq.h中定义,其中的成员大多用于操作底层硬件,比如设置寄存器以屏蔽中断,使能中断,清除中断等。所以我在这里称他为中断硬件描述符,主要是提供硬件操作的API。

irq_chip:

struct irq_chip {

const char

*name; 注释:定义名字,通过 /proc/interrupts可以查看这个名字

unsigned int

(*startup)(unsigned int irq);

void

(*shutdown)(unsigned int irq);

void

(*enable)(unsigned int irq); ; 注释:使能IRQ,默认使能

void

(*disable)(unsigned int irq);

void

(*ack)(unsigned int irq); 注释:irq的全局ack,需要根据硬件设置必须实现通常是清除当前中断使得可以接收下一个中断。

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

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;

};

1.2

Request_irq开始说起我们知道用户驱动程序通过request_irq函数向内核注册中断处理函数,request_irq函数根据中断号找到irq_desc数组项,然后在它的action链表添加一个表项。

error = request_irq(keypad->irq, keypad_irq_handler,

IRQF_DISABLED, pdev->name, keypad);

if (error) {

dev_err(&pdev->dev, “failed to request IRQ\n”);

goto failed_put_clk;

}

看上面这段代码是我们在写驱动的时候需要申请IRQ号的时候需要写的一段标准code。request_irq的原型为:

static inline int __must_check

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

}

所以我们写驱动的时候对应的各个参数:

keypad->irq à unsigned int irq,中断号

keypad_irq_handler à irq_handler_t handler用户自定义的中断处理函数

IRQF_DISABLED à IRQF_DISABLED, linux定义的中断flags如下:

#define IRQF_DISABLED

0x00000020注释:当前irq处理的时候保持irq禁止

#define IRQF_SAMPLE_RANDOM

0x00000040

#define IRQF_SHARED

0x00000080注释:允许在多个设备之间共享irq号

#define IRQF_PROBE_SHARED

0x00000100

#define IRQF_TIMER

0x00000200注释: 标志该中断是timer interrupt

#define IRQF_PERCPU

0x00000400

#define IRQF_NOBALANCING

0x00000800

#define IRQF_IRQPOLL

0x00001000

#define IRQF_ONESHOT

0x00002000

pdev->name à const char *name

keypad à void *dev

request_irq调用了函数request_threaded_irq,他的原型为:

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;

注释:前面的log已经说的很明确,当多个设备共享irq的时候,我们必须通过dev_id来识别是哪一个设备来的中断,所以当flag被标志为:RQF_SHARED共享的时候,我们必须确保dev_id非空。

desc = irq_to_desc(irq);

irq_to_desc的原型如下:

注释:

struct irq_desc *irq_to_desc(unsigned int irq)

{

return (irq < NR_IRQS) ? irq_desc + irq : NULL;

}

是通过irq号查找irq_desc数组,来确定对应irq的irq_desc[irq].

irq_desc全局数组对应的定义如下:

struct irq_desc irq_desc[NR_IRQS] __cacheline_aligned_in_smp = {

[0 … NR_IRQS-1] = {

.status = IRQ_DISABLED,

.chip = &no_irq_chip,

.handle_irq = handle_bad_irq,

.depth = 1,

.lock = __RAW_SPIN_LOCK_UNLOCKED(irq_desc->lock),

}

};

NR_IRQS一般会重定义在arch./arm/mach-xxxx/include/mach/irqs.h下面。

if (!desc)

return -EINVAL;注释:如果没找到相应的irq_desc表示出错了。

if (desc->status & IRQ_NOREQUEST)

return -EINVAL;

注释:irq_desc.status初始化的时候设置为:IRQ_NOREQUEST不可被request状态,则这里做判断确保不能被request。

if (!handler) {

if (!thread_fn)

return -EINVAL;

handler = irq_default_primary_handler;

}

注释:如果在驱动中request irq的时候没定义自己的handler,则至少thread_fn应该被定义否则报错返回EINVAL。同时这段code还赋值一个某人handler给驱动程序。

action = kzalloc(sizeof(struct irqaction), GFP_KERNEL);

if (!action)

return -ENOMEM;

action->handler = handler;

action->thread_fn = thread_fn;

action->flags = irqflags;

action->name = devname;

action->dev_id = dev_id;

注释:kzalloc一个irqaction结构体,并用驱动调用request_irq时候传进去的handler,irqflags,devname和dev_id初始化这个结构体。

chip_bus_lock(irq, desc);

注释:

这个函数的原型:

/* Inline functions for support of irq chips on slow busses */

static inline void chip_bus_lock(unsigned int irq, struct irq_desc *desc)

{

if (unlikely(desc->chip->bus_lock))

desc->chip->bus_lock(irq);

}

从linux给出的解释看出是为了在低速设备上的irq用的,就是实现了mutex。具体实现是在

在定义arch/arm/mach-xxx/irq.c中定义:

static struct irq_chip xxx_irq_chip = {

.ack

= xxx_irq_ack,

.mask

= xxx_irq_mask,

.unmask

= xxx_irq_unmask,

};

的时候有:

void

(*bus_lock)(unsigned int irq);

void

(*bus_sync_unlock)(unsigned int irq);

这两个函数的实现。

retval = __setup_irq(irq, desc, action);

注释:setup irq函数是是一个重要的函数,我们将在下面进行具体讲解。

chip_bus_sync_unlock(irq, desc);

if (retval)

kfree(action);

#ifdef CONFIG_DEBUG_SHIRQ

if (!retval && (irqflags & IRQF_SHARED)) {

/*

* It’s a shared IRQ — the driver ought to be prepared for it

* to happen immediately, so let’s make sure…

* We disable the irq to make sure that a ‘real’ IRQ doesn’t

* run in parallel with our fake.

*/

unsigned long flags;

disable_irq(irq);

local_irq_save(flags);

handler(irq, dev_id);

local_irq_restore(flags);

enable_irq(irq);

}

#endif

return retval;

}

EXPORT_SYMBOL(request_threaded_irq);

__setup_irq函数:

/*

* Internal function to register an irqaction – typically used to

* allocate special interrupts that are part of the architecture.

*/

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 = 0;

int ret;

if (!desc)

return -EINVAL;

if (desc->chip == &no_irq_chip)

return -ENOSYS;

注释:这里判断chip是否被初始化,一般在arch/arm/mach-xxx/irq.c的xxx_init_irq set_irq_chip(irqno, &xxx_irq_chip); 中初始化这个desc->chip指针,xxx_irq_chip定义的结构类似:

static struct irq_chip xxx_irq_chip = {

.ack

= xxx_irq_ack,

.mask

= xxx_irq_mask,

.unmask

= xxx_irq_unmask,

};

/*

* 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))

return -EINVAL;

注释:Oneshot类型的中断不允许共享

/*

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

}

/*

* Create a handler thread when a thread function is supplied

* and the interrupt does not nest into another interrupt

* thread.

*/

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;

}

注释:由于new->thread_fn在驱动调用的request irq函数中设置为NULL,所以以上代码执行不到,我们暂时不做解释。

/*

* The following block of code has to be executed atomically

*/

raw_spin_lock_irqsave(&desc->lock, flags);

old_ptr = &desc->action;

注释:old_ptr是irqaction结构类型指针的指针。这里把old_ptr指向当前irq的irq_desc成员action的指针的地址。

old = *old_ptr;

old是irqaction结构类型指针,这里是把old指向当前irq的irq_desc成员action的指针。

[1][2][3]

放下一种执着,收获一种自在。放下既是一种理性抉择,也是一种豁达美。

Linux子系统之中断机制研究

相关文章:

你感兴趣的文章:

标签云: