ºìÁªLinuxÃÅ»§
Linux°ïÖú

include/linuxÖеÄinterrupt.h

·¢²¼Ê±¼ä:2006-08-28 10:15:29À´Ô´:ºìÁª×÷Õß:syncode
include/linux ÖеÄinterrupt.hÖаüº¬ÁËÓëÖжÏÏà¹ØµÄ´ó²¿·Öºê¼°struct½á¹¹µÄ¶¨Ò壬ÒÔºóÓÐʱ¼äÔÙһһ˵Ã÷

ÔÚ2.6.12°æµÄlinux kernelÖÐÒѾ­Ã»ÓÐbottom half ºÍtask queueÕâÁ½ÖÖ»úÖÆ£¬¶øÊÇsoftirq£¬taskletºÍwork queue £¬softirqÖ÷ÒªÊÇͨ¹ýÔÚÿ´ÎÖжϷµ»ØÊÇÖ´ÐÐdo_softirqÕâ¸öº¯ÊýÍê³É£¬º¯ÊýµÄÖ÷Òª²¿·ÖÊÇÒ»¸öÑ­»·£º

do {
if (pending & 1) {
h->action(h);
rcu_bh_qsctr_inc(cpu);
}
h++;
pending >>= 1;
} while (pending);

ÔÚ×µÄÇé¿öÏ£¬Õâ¸öÑ­»·»á±»Ö´ÐÐ10´Î£¬Èç¹û´Ëʱpending»¹ÊDz»Îª0,Ò²¾ÍÊÇ˵»¹ÓÐÈíÖжÏûÓд¦Àí£¬¾Í»½ÐÑÒ»¸öÄÚºËÏß³ÌÈ¥´¦Àíʣϵģ¬Õâ¸öÏ߳̽Рsoftirqd£¬Ã¿¸öcpu»áÓÐÒ»¸ö£¬ËùÒÔÈç¹ûÖ´ÐÐps auxÕâÌõÃüÁ»áÔÚijһÐп´µ½[softirqd/0]£¬Õâ¸ö¾ÍÊÇ0ºÅcpuµÄÈíÖжÏÊغòÏ̡߳£
taskletʵ¼ÊÉÏÊÇÁ½¸öÈíÖжϣ¬ÎªÊ²Ã´ÊÇÁ½¸öÄØ£¿ÎªÁË´¦Àí²»Í¬ÓÅÏȼ¶µÄÊÂÇé¡£ÕâÁ½¸öÈíÖжÏÒ»¸öµÄÓÅÏȼ¶¸ßµã£¬ÁíÒ»¸öµÄµÍÒ»µã¡£Ä¿Ç°linuxÄÚºËÖÐÒѾ­ÓÃÁË6¸öÈíÖжϣ¬Õâ¸öÔÚÖÐÓж¨Ò壺
enum
{
HI_SOFTIRQ=0, ÓÅÏȼ¶¸ßµãµÄtasklet
TIMER_SOFTIRQ, ¶¨Ê±Æ÷
NET_TX_SOFTIRQ, ·¢ËÍÍøÂçÊý¾Ý
NET_RX_SOFTIRQ, ½ÓÊÕÍøÂçÊý¾Ý
SCSI_SOFTIRQ, SCSIÉ豸µÄÈíÖжÏ
TASKLET_SOFTIRQ ÓÅÏȼ¶µÍµÄtasklet
};
ÕâЩÊýʵ¼ÊÉÏÊÇÒ»¸öÊý£¬¾ÍÊÇÊý×ésoftirq_vec[32]µÄϱꡣ

¹¤×÷¶ÓÁÐÎÒ²»ÊǺÜÊìϤ£¬ÕâÀï¾Í²»ËµÁË£¬ÏÖÔÚÀ´¶Ôinterrupt.hÕâ¸öÎļþ×öÒ»ÏÂ×¢ÊÍ°É
ÒÔÏÂÄÚÈÝÈ«²¿ÊÇ´Óinterrupt.hÖп½±´³öÀ´µÄ£¬²¢°´Ô­À´µÄ˳ÐòÅÅÁУ¬³öÓÚƪ·ùµÄ¿¼ÂÇ£¬¼õÈ¥ÁËһЩÄÚÈÝ£¬ÒÔϵÄ˵Ã÷¶¼ÊÇÎÒÈÏΪºÜÖØÒª»òÕß²»ºÃÀí½âµÄ²¿·Ö¡£
typedef int irqreturn_t;
Õâ¸öÁ¿¼°ÏÂÃæµÄÈý¸öºê¶¼ÊÇΪÁË¿ÉÒÆÖ²ÐÔ£¬ÊÇÖжϴ¦Àíº¯ÊýµÄ·µ»ØÖµ£¬´úÂëµÄ×÷ÕßÓкÜÏêϸµÄ˵Ã÷£¬¿ÉÒÔÖ±½Ó¿´ÎļþÖеÄ×¢ÊÍ
#define IRQ_NONE (0)
#define IRQ_HANDLED (1)
#define IRQ_RETVAL(x) ((x) != 0)

struct irqaction {
irqreturn_t (*handler)(int, void *, struct pt_regs *);
unsigned long flags;
cpumask_t mask;
const char *name;
void *dev_id;
struct irqaction *next;
int irq;
struct proc_dir_entry *dir;
};
Õâ¸ö½á¹¹¾ÍÊÇÖжϴ¦Àí³ÌÐòºÜÖØÒªµÄ½á¹¹ÁË£¬Õâ¸ö¾ÍÊÇÔÚhandle_IRQ_event(); Öд¦ÀíµÄÖ÷Òª½á¹¹£¬ÓÃÓÚÖ´ÐÐÕæÕýµÄÖжϴ¦Àíº¯Êý£¬½á¹¹ÖÐhandlerÊÇÖжϴ¦Àíº¯Êý£¬Äãͨ¹ýrequest_irq() ×¢²áµÄº¯Êý£¬Æäʵrequest_irq()´«½øÀ´µÄ²ÎÊý¾ÍÊÇÓÃÀ´ÌîÕâ¸ö½á¹¹µÄ£¬Õâ¸ö½á¹¹ÊÇÒ»¸öµ¥ÏòÁ´±í£¬handle_IRQ_event()´¦ÀíʱÊDZéÀúÕâ¸ö±í

extern irqreturn_t no_action(int cpl, void *dev_id, struct pt_regs *regs);
Õâ¸öÊÇÒ»¸ö¿Õº¯Êý£¬ÔÚkernel/irq/handle.cÖУ¬Ö»ÓÐÒ»¾ä£ºreturn IRQ_NONE£»

extern int request_irq(unsigned int,
irqreturn_t (*handler)(int, void *, struct pt_regs *),
unsigned long, const char *, void *);
Õâ¸ö¾ÍÊÇ×¢²áÖжϴ¦Àí³ÌÐòµÄº¯Êý£¬¡¶linux kernel development¡·ÖÐÓÐ˵Ã÷¡£

extern void free_irq(unsigned int, void *);
ÊÍ·ÅÖжϴ¦Àí³ÌÐòµÄº¯Êý£¬Í¬ÉÏ¡£

#ifdef CONFIG_GENERIC_HARDIRQS
extern void disable_irq_nosync(unsigned int irq);
extern void disable_irq(unsigned int irq);
extern void enable_irq(unsigned int irq);
#endif
Õ⼸¸öÎÒ»¹²»ÖªµÀÊǸÉʲôµÄ¡£

/*
* Temporary defines for UP kernels, until all code gets fixed.
*/
#ifndef CONFIG_SMP
Ò»Ö±µ½ÏÂÃæµÄ#endif ¶¼ÊÇ¿ª¹ØcpuÖжϵÄÖ¸ÁîµÄ°ü×°£¬Êµ¼ÊÉϾÍÊÇcliºÍstiºÍһЩpushºÍpopÖ¸ÁÕâЩÔÚĿ¼ÏÂÓÃgrepһϾͳöÀ´ÁË¡£
static inline void __deprecated cli(void)
{
local_irq_disable();
}
static inline void __deprecated sti(void)
{
local_irq_enable();
}
static inline void __deprecated save_flags(unsigned long *x)
{
local_save_flags(*x);
}
#define save_flags(x) save_flags(&x);
static inline void __deprecated restore_flags(unsigned long x)
{
local_irq_restore(x);
}

static inline void __deprecated save_and_cli(unsigned long *x)
{
local_irq_save(*x);
}
#define save_and_cli(x) save_and_cli(&x)
#endif /* CONFIG_SMP */

/* SoftIRQ primitives. */
#define local_bh_disable() \
do { add_preempt_count(SOFTIRQ_OFFSET); barrier(); } while (0)
#define __local_bh_enable() \
do { barrier(); sub_preempt_count(SOFTIRQ_OFFSET); } while (0)

extern void local_bh_enable(void);

enum
{
HI_SOFTIRQ=0,
TIMER_SOFTIRQ,
NET_TX_SOFTIRQ,
NET_RX_SOFTIRQ,
SCSI_SOFTIRQ,
TASKLET_SOFTIRQ
};
Õâ¸ö¾ÍÊÇÇ°ÃæÌáµ½µÄÒѾ­Ê¹ÓõÄÈíÖжÏ

/* softirq mask and active fields moved to irq_cpustat_t in
* asm/hardirq.h to get better cache usage. KAO
*/

struct softirq_action
{
void (*action)(struct softirq_action *);
void *data;
};
Õâ¸ö¾ÍÊÇÈíÖжϵĽṹ£¬ºÍÓ²ÖжϵÄirqcation²î²»¶àµÄ

asmlinkage void do_softirq(void);
Õâ¸ö²»ÓÃ˵Ã÷£¬ÈíÖжϵÄÈë¿Ú£¬Õâ¸öº¯ÊýÔÚirq_exit()Öб»µ÷Óã¬Èç¹ûÓÐÈíÖжÏÒª´¦ÀíµÄ»°¡£
extern void open_softirq(int nr, void (*action)(struct
softirq_action*), void *data);
Õâ¸öº¯ÊýÓÐÓÚ×¢²áÒ»¸öÈíÖжϣ¬ºÍrequest_irq()²î²»¶à¡£

extern void softirq_init(void);

#define __raise_softirq_irqoff(nr) do { local_softirq_pending() |= 1UL << (nr); } while (0)
extern void FASTCALL(raise_softirq_irqoff(unsigned int nr));
extern void FASTCALL(raise_softirq(unsigned int nr));
Õ⼸¸öº¯Êý¾ÍÊÇÓÃÓÚ¼¤»îÈíÖжϣ¬Ï൱ÓÚÓ²ÖжÏÖÐÓ²¼þ²úÉúÁËÒ»¸öÓ²Öжϣ¬Êµ¼ÊÉϾÍÊǽ«typedef struct {
unsigned int __softirq_pending;
unsigned long idle_timestamp;
unsigned int __nmi_count; /* arch dependent */
unsigned int apic_timer_irqs; /* arch dependent */
} ____cacheline_aligned irq_cpustat_t;
Õâ¸ö½á¹¹ÖжÔÓ¦µÄ__softirq_pendingµÄijһλÖÃ룬ÊÇÄÄһλÓÐnr¾ö¶¨£¬Õâ¸ö½á¹¹ÔÚasm-i386/hardirq.hÖС£Ó²Öжϴ¦Àí³ÌÐò·µ»ØÊÇ»á¼ì²é Õâ¸ö½á¹¹ÖеÄ__softirq_pending¿´ÊÇ·ñÓÐÈíÖжÏÒª´¦Àí£¬ÕâÑùÈíÖжϴ¦Àí³ÌÐò¾Í±»¼¤»îÁË¡£

µ½ÕâÀïsoftirqÏà¹ØµÄ²¿·Ö¾ÍÍêÁË£¬ÏÂÃæµÄ¾ÍÊÇÒ»¸ö¾ßÌåµÄÈíÖжÏtaskletµÄÏà¹Ø½á¹¹¼°Ò»Ð©ÉùÃ÷£¬ºÍÉÏÃæµÄ×÷ÓÃÀíÄî²î²»¶à£¬¶ÔÓ¦µÄ´ó²¿·Öº¯ÊýµÄʵÏÖÔÚkernel/softirq.c
ÎļþÌ«³¤ÁË£¬½ñÌì¾Íµ½ÕâÀï°É£¬
/* Tasklets --- multithreaded analogue of BHs.

Main feature differing them of generic softirqs: tasklet
is running only on one CPU simultaneously.

Main feature differing them of BHs: different tasklets
may be run simultaneously on different CPUs.

Properties:
* If tasklet_schedule() is called, then tasklet is guaranteed
to be executed on some cpu at least once after this.
* If the tasklet is already scheduled, but its excecution is still not
started, it will be executed only once.
* If this tasklet is already running on another CPU (or schedule is called
from tasklet itself), it is rescheduled for later.
* Tasklet is strictly serialized wrt itself, but not
wrt another tasklets. If client needs some intertask synchronization,
he makes it with spinlocks.
*/

struct tasklet_struct
{
struct tasklet_struct *next;
unsigned long state;
atomic_t count;
void (*func)(unsigned long);
unsigned long data;
};

#define DECLARE_TASKLET(name, func, data) \
struct tasklet_struct name = { NULL, 0, ATOMIC_INIT(0), func, data }

#define DECLARE_TASKLET_DISABLED(name, func, data) \
struct tasklet_struct name = { NULL, 0, ATOMIC_INIT(1), func, data }


enum
{
TASKLET_STATE_SCHED, /* Tasklet is scheduled for execution */
TASKLET_STATE_RUN /* Tasklet is running (SMP only) */
};

#ifdef CONFIG_SMP
static inline int tasklet_trylock(struct tasklet_struct *t)
{
return !test_and_set_bit(TASKLET_STATE_RUN, &(t)->state);
}

static inline void tasklet_unlock(struct tasklet_struct *t)
{
smp_mb__before_clear_bit();
clear_bit(TASKLET_STATE_RUN, &(t)->state);
}

static inline void tasklet_unlock_wait(struct tasklet_struct *t)
{
while (test_bit(TASKLET_STATE_RUN, &(t)->state)) { barrier(); }
}
#else
#define tasklet_trylock(t) 1
#define tasklet_unlock_wait(t) do { } while (0)
#define tasklet_unlock(t) do { } while (0)
#endif

extern void FASTCALL(__tasklet_schedule(struct tasklet_struct *t));

static inline void tasklet_schedule(struct tasklet_struct *t)
{
if (!test_and_set_bit(TASKLET_STATE_SCHED, &t->state))
__tasklet_schedule(t);
}

extern void FASTCALL(__tasklet_hi_schedule(struct tasklet_struct *t));

static inline void tasklet_hi_schedule(struct tasklet_struct *t)
{
if (!test_and_set_bit(TASKLET_STATE_SCHED, &t->state))
__tasklet_hi_schedule(t);
}


static inline void tasklet_disable_nosync(struct tasklet_struct *t)
{
atomic_inc(&t->count);
smp_mb__after_atomic_inc();
}

static inline void tasklet_disable(struct tasklet_struct *t)
{
tasklet_disable_nosync(t);
tasklet_unlock_wait(t);
smp_mb();
}

static inline void tasklet_enable(struct tasklet_struct *t)
{
smp_mb__before_atomic_dec();
atomic_dec(&t->count);
}

static inline void tasklet_hi_enable(struct tasklet_struct *t)
{
smp_mb__before_atomic_dec();
atomic_dec(&t->count);
}

extern void tasklet_kill(struct tasklet_struct *t);
extern void tasklet_kill_immediate(struct tasklet_struct *t, unsigned int cpu);
extern void tasklet_init(struct tasklet_struct *t,
void (*func)(unsigned long), unsigned long data);

/*
* Autoprobing for irqs:
*
* probe_irq_on() and probe_irq_off() provide robust primitives
* for accurate IRQ probing during kernel initialization. They are
* reasonably simple to use, are not "fooled" by spurious interrupts,
* and, unlike other attempts at IRQ probing, they do not get hung on
* stuck interrupts (such as unused PS2 mouse interfaces on ASUS boards).
*
* For reasonably foolproof probing, use them as follows:
*
* 1. clear and/or mask the device's internal interrupt.
* 2. sti();
* 3. irqs = probe_irq_on(); // "take over" all unassigned idle IRQs
* 4. enable the device and cause it to trigger an interrupt.
* 5. wait for the device to interrupt, using non-intrusive polling or a delay.
* 6. irq = probe_irq_off(irqs); // get IRQ number, 0=none, negative=multiple
* 7. service the device to clear its pending interrupt.
* 8. loop again if paranoia is required.
*
* probe_irq_on() returns a mask of allocated irq's.
*
* probe_irq_off() takes the mask as a parameter,
* and returns the irq number which occurred,
* or zero if none occurred, or a negative irq number
* if more than one irq occurred.
*/

#if defined(CONFIG_GENERIC_HARDIRQS) && !defined(CONFIG_GENERIC_IRQ_PROBE)
static inline unsigned long probe_irq_on(void)
{
return 0;
}
static inline int probe_irq_off(unsigned long val)
{
return 0;
}
static inline unsigned int probe_irq_mask(unsigned long val)
{
return 0;
}
#else
extern unsigned long probe_irq_on(void); /* returns 0 on failure */
extern int probe_irq_off(unsigned long); /* returns 0 or negative on failure */
extern unsigned int probe_irq_mask(unsigned long); /* returns mask of ISA interrupts */
#endif

#endif
ÎÄÕÂÆÀÂÛ

¹²ÓÐ 0 ÌõÆÀÂÛ