2010年12月29日 星期三

Linux interrupt flow and data structure overview

__irq_usr or __irq_svc in arcn\arm\kernl\entry-armv.S
->
irq_handler in .macro    irq_handler in arcn\arm\kernl\entry-armv.S
->asm_do_IRQ()->
generic_handle_irq()->generic_handle_irq_desc()->
desc->handle_irq(irq, desc)->handle_IRQ_event()->
action->handler(irq, action->dev_id);

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

data structure

interrupt flow

2010年12月27日 星期一

modify irq_desc

\include\linux\irq.h
## provide extern IRQ handler interface of irq_desc
## provide extern Handling irq action chains of irq_desc
struct irq_desc {};
extern struct irq_desc irq_desc[NR_IRQS];
extern void handle_simple_irq(unsigned int irq, struct irq_desc *desc);
static inline void set_irq_handler(unsigned int irq, irq_flow_handler_t handle);
set_irq_chip_and_handler(unsigned int irq, struct irq_chip *chip, irq_flow_handler_t handle);
set_irq_chip_and_handler_name(unsigned int irq, struct irq_chip *chip, irq_flow_handler_t handle, const char *name);
extern int set_irq_chip(unsigned int irq, struct irq_chip *chip);
extern int set_irq_data(unsigned int irq, void *data);
extern int set_irq_chip_data(unsigned int irq, void *data);
extern int setup_irq(unsigned int irq, struct irqaction *new);
extern void remove_irq(unsigned int irq, struct irqaction *act);


\include\linux\irqnr.h
extern struct irq_desc *irq_to_desc(unsigned int irq);
 
\kernel\irq\chip.c

## irq_handler (handle_irq of irq_desc)
## irq chip (chip of irq_desc)
## irq handler flow function (handle_irq of irq_desc)
int set_irq_chip(unsigned int irq, struct irq_chip *chip);
__set_irq_handler(unsigned int irq, irq_flow_handler_t handle, int is_chained,const char *name);
void set_irq_probe(unsigned int irq);
int set_irq_type(unsigned int irq, unsigned int type);
int set_irq_data(unsigned int irq, void *data);
static inline void mask_ack_irq(struct irq_desc *desc, int irq);
static inline void mask_irq(struct irq_desc *desc, int irq);
static inline void unmask_irq(struct irq_desc *desc, int irq);


void handle_simple_irq(unsigned int irq, struct irq_desc *desc);
void handle_level_irq(unsigned int irq, struct irq_desc *desc);



\kernel\irq\handle.c
## interrupt action flow
## __do_IRQ flow
irqreturn_t handle_IRQ_event(unsigned int irq, struct irqaction *action);
unsigned int __do_IRQ(unsigned int irq);


\kernel\irq\irqdesc.c in v2.6.37
struct irq_desc irq_desc[NR_IRQS] __cacheline_aligned_in_smp = {initial data};
// do action->handler(irq, action->dev_id);
struct irq_desc *irq_to_desc(unsigned int irq);
struct irq_desc *irq_to_desc_alloc_node(unsigned int irq, int node);

kernel\irq\manage.c
## provide operation interface irq_desc
## add/remove irqaction list of irq_desc table
setup_irq(unsigned int irq, struct irqaction* act);
void remove_irq(unsigned int irq, struct irqaction *act);
void disable_irq(unsigned int irq);
void enable_irq(unsigned int irq);

include\linux\interrupt.h
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);
}

2010年12月23日 星期四

USB knowledge

http://stenlyho.blogspot.com/2008/08/uhciohciehci.html

ADD/REMOVE IRQaction list of irq_desc

kernel/irq/manage.c
## add/remove irqaction list of irq_desc table
setup_irq(unsigned int irq, struct irqaction* act);
__setup_irq(unsigned int irq, struct irq_desc *desc, struct irqaction *new);

 void remove_irq(unsigned int irq, struct irqaction *act);
static struct irqaction *__free_irq(unsigned int irq, void *dev_id);

spinlock

#if defined(CONFIG_SMP)
//spinlock_types.h (D:\linux-2.6.36.2\arch\arm\include\asm)
# include <asm/spinlock_types.h>
typedef struct {
    volatile unsigned int lock;
} arch_spinlock_t;
#else
# include <linux/spinlock_types_up.h>
#ifdef CONFIG_DEBUG_SPINLOCK

typedef struct {
    volatile unsigned int slock;
} arch_spinlock_t;

#define __ARCH_SPIN_LOCK_UNLOCKED { 1 }

#else

typedef struct { } arch_spinlock_t;

#define __ARCH_SPIN_LOCK_UNLOCKED { }

#endif

#endif

ARM CONFIG_SPARSE_IRQ: Support sparse irq numbering

arch/arm/Kconfig

The configuration item CONFIG_SPARSE_IRQ:
  • prompt:
  • type: tristate
  • depends on: (none)
  • defined in arch/arm/Kconfig
  • found in Linux Kernels: from 2.6.29 release still available on 2.6.36 release

Help text

This enables support for sparse irqs. This is useful in general as most CPUs have a fairly sparse array of IRQ vectors, which the irq_desc then maps directly on to. Systems with a high number of off-chip IRQs will want to treat this as experimental until they have been independently verified.

linux PIC

http://lxr.linux.no/#linux+v2.6.36/kernel/irq/handle.c#L138 
/*
  59 * Linux has a controller-independent interrupt architecture.
  60 * Every controller has a 'controller-template', that is used
  61 * by the main code to do the right thing. Each driver-visible
  62 * interrupt source is transparently wired to the appropriate
  63 * controller. Thus drivers need not be aware of the
  64 * interrupt-controller.
  65 *
  66 * The code is designed to be easily extended with new/different
  67 * interrupt controllers, without having to do assembly magic or
  68 * having to touch the generic code.
  69 *
  70 * Controller mappings for all interrupt sources:
  71 */

2010年12月22日 星期三

Linux boot over arm

1./arch/arm/kernel/entry-armv.S
(general HAL of linux kernel)
## 存放vector_table
## include dependent platform specific
...
#include <mach/entry-macro.S>
...
// get_irqnr_preamble and get_irqnr_and_base def in enrty-macro.S
get_irqnr_preamble
//取得irq number from HAL level 
get_irqnr_and_base
... 
//進入os level之處裡中斷 
asm_do_IRQ
 
2./arch/arm/mach-aaec2000/include/mach/entry-macro.S
(platform HAL for linux kernel)
## 取得IRQ NR的MACRO 
## 取得IRQ NR的MACRO
include <mach/irqs.h>...
.macro  get_irqnr_and_base, irqnr, irqstat, base, tmp
...
 
3./arch/arm/mach-aaec2000/include/mach/irqs.h
## IRQ table
 
4. arch/arm/mach-aaec2000/include/mach/aaec2000.h
## Device Memory Mapping
## especially Interrupt controller
5.arch/arm/kernel/irq.c
...
asmlinkage void __exception asm_do_IRQ(unsigned int irq, struct pt_regs *regs)
...

linux over arm - entry-armv.S

http://lxr.linux.no/#linux+v2.6.35.9/arch/arm/kernel/entry-armv.S#L1224

2010年12月21日 星期二

bss section

The computer program memory is organized into the following:

    * Data Segment (Data + BSS + Heap)
    * Stack
    * Code segment
Typically, the program loader initializes the memory allocated
for the bss section when it loads the program. Operating systems
may use a technique called zero-fill-on-demand to efficiently
implement the bss segment (McKusick & Karels 1986).
In embedded software, the bss segment is mapped into memory that
is initialized to zero by the C run-time system before main()
is entered.
 
#include <stdio.h>

extern int __bss_start, _end;

int a, b, c, d;  /* un-initialized */

int main()
{
        int *ptr;
        a = 1, b = 2, c = 3, d = 4;

        for (ptr = &__bss_start; ptr != &_end; ptr++) {
                printf("%d\n", *ptr);
        }
        return 0;
}

Semaphore

function V(semaphore S):
    Atomically increment S
    [S ← S + 1]

function P(semaphore S):
    repeat:
        Between repetitions of the loop other processes may operate on the semaphore
        [if S > 0:
            Atomically decrement S - note that S cannot become negative
            S ← S - 1
            break]

SeqLock - writers-preference

It is a reader-writer consistent mechanism which
avoids the problem of writer starvation. 
 
The reader never blocks, but it may have to retry
if a write is in progress; this speeds up the readers
in the case where the data was not modified, since they
do not have to acquire the lock as they would with a
traditional read-write lock. Also, writers do not wait
for readers, whereas with traditional read-write locks
they do, leading to potential resource starvation in a
situation where there are a number of readers (because
the writer must wait for there to be no readers). 
 
Because of these two factors, seqlocks are more efficient than
traditional read-write locks for the situation where there are
many readers and few writers. The drawback is that if there is
too much write activity or the reader is too slow, they might
livelock (and the readers may starve). 
 
 
 
 
int readcount, writecount; (initial value = 0)
semaphore mutex 1, mutex 2, mutex 3, w, r ; (initial value = 1)
 
READER
  P(mutex 3);
    P(r);
      P(mutex 1);
        readcount := readcount + 1;
        if readcount = 1 then P(w);
      V(mutex 1);
    V(r);
  V(mutex 3);
 
  reading is done
 
  P(mutex 1);
    readcount := readcount - 1;
    if readcount = 0 then V(w);
  V(mutex 1);
 
WRITER
    P(mutex 2);
      writecount := writecount + 1;
      if writecount = 1 then P(r);
    V(mutex 2);
 
  P(w);
    writing is performed
  V(w);
 
  P(mutex 2);
    writecount := writecount - 1;
    if writecount = 0 then V(r);
  V(mutex 2);
reference:http://en.wikipedia.org/wiki/Readers-writer_lock 

Monitor

The trend in programming language development is towards more structured forms of synchronization, such as monitors. These synchronization abstractions usually employ semaphores or mutexes internally, but do not expose the semaphore interface directly to the programmer. This is motivated partly by the serious, hard-to-diagnose problems that often arise when semaphores are used incorrectly, a risk much reduced when synchronization is tightly coupled to the resource it controls and is managed automatically by the language implementation rather than manually by the programmer.