分类: LINUX
2009-05-13 17:05:14
------------------------------------------------
本文系本站原创,欢迎转载!
我们在写linux驱动时估计大家都遇到过io访问的,这是可能就要用到request_region,ioremap,ioport_map,
其实我们为甚么要这个呢,因为Linux采用了mmu,即我们使用的是虚拟地址,所以如果我们要访问内存必须要将其映射,然后再访问它:
而在arm平台上外设的io映射有两种方式。
1.在具体的平台的cpu.c的s
static struct map_desc s
IODESC_ENT(GPIO),//定义了I/o映射后的地址
IODESC_ENT(IRQ),//定义了中断相关的地址
IODESC_ENT(MEMCTRL),//定义了mem相关寄存器的地址
IODESC_ENT(UART)
};
然后在系统启动的时候调用mach的map_io时会调用
iotable_init(s
这个然后就执行mmu映射,将io映射到高端内核虚拟地址.
2.通过ioremap
这个函数执行其实也是通过mmu映射将io映射到虚拟地址。
只不过这个是你随时可以调用,所以它可以看成是动态分配io资源。
正因为是动态分配,就有一个冲突问题,如果你开始在一个驱动中使用了一个Io资源,然后其他人也想用这个资源,那么如果都调用了这个函数,就会出现,两个驱动访问同一资源的现象,可能引起错误,当然你们如果确实是想都操作这些io那没问题。
因此对于独享io,一般要先调用request_region注册这个资源,以让别人知道你在使用这个Io,同样如果别人已经用了这个资源就会调用不成功,这样就可达到避免从突,当然这个是君子协议,如果你request_region返回错误后还是可以使用ioremap的,这时候你自己就得承担可能的后果了。
然后映射后就可以访问了, 大家都知道writeb,writel,readb,readl,ioread这些操作io内存的函数.
其实这些主要是为了屏蔽平台的差异性,向用户提供统一的接口,不同平台编译成不同的指令,对于arm,writel(start,offset,value)等价于*(volatile usigned int *)=value;
从上面我们就可以看出如果你的外设的Io资源已经在s3c_iodesc注册,你在使用io前就不需要ioremap,直接使用相应io的虚拟地址即可,在linux中一般都定义了的,像s3c2410_GPIOA等等。
也正因为如此,相使用gpio的驱动就不用使用Ioremap了,因为它们都属于gpio,已经在map_io时就初始化了.
其实在x86下还有一个和ioremap对应的,就是ioport_map
void __iomem *ioport_map(unsigned long port, unsigned int nr)
{
if (port > PIO_MASK)
return NULL;
return (void __iomem *) (unsigned long) (port + PIO_OFFSET);
}
void ioport_unmap(void __iomem *addr)
{
/* Nothing to do */
}
它只是简单地把I/O端口号加上PIO_OFFSET(64K),作为一个“假”的内存地址返回,而unmap则什么也不做。之所以这样做,是基于这样一个事实:真正的I/O内存地址经过映射成为虚拟地址后,由于是在内核空间,其值肯定大于
unsigned int fastcall ioread8(void __iomem *addr)
{
unsigned long port = (unsigned long __force)addr;
if( port < 0x40000UL ) {
BUG_ON( (port & ~PIO_MASK) != PIO_OFFSET );
port &= PIO_MASK;
return inb(port);
}else{
return readb(addr);
}
}
既然上面说到mmu.
下面我们具体叙述进入linux后的空间映射过程。此时内核没有解压缩,根据$(TOPDIR)/arch/arm/boot中的Makefile和其子目录compressed目录下的文件,我们可以知道head.S是整个压缩镜像的入口。从compressed/vmlinux.lds文件中我们得知解压后的内核起始地址为0x30008000,从相应的Makefile中也知道LOAD_ADDR=ZRELADDR=0x30008000。在 head.S中进行了内核解压,同时调用解压的内核开始运行内核。
mov pc, r4 @其中r4就是0x30008000
大家注意在head.S中的技巧,在进入head.S时寄存器pc的值为0x30008000,在整个程序中adr,ldr伪指令,以及 ldr,str,b,bl等指令中对于标号或是符号的寻址都是基于pc的,所以整个压缩镜像的程序的标号虽然是以0开始,但还是能够在 0x30008000后的空间中运行的很好。代码中多处涉及到空间的修正,请大家仔细斟酌,这个特性在更重要的内核启动的空间切换中至关重要。
adr r0, LC0 @LC0本身是以0为基址的偏移,而adr的基于pc寻址导致r0是相对于0x30008000的
ldmia r0, {r1, r2, r3, r4, r5, r6, ip, sp} @r1,r2等的值都是对于0的偏移
subs r0, r0, r1 @ calculate the delta offset
teq r0, #0 @ if delta is zero, we're,一定不等于0
beq not_relocated
.type LC0, #object
LC0: .word LC0 @ r1
.word __bss_start @ r2
.word _end @ r3
.word _load_addr @ r4
.word _start @ r5
.word _got_start @ r6
.word _got_end @ ip
.word user_stack+4096 @ sp
我们再看进入内核的映射过程,进入内核后,pc=0x30008000,而代码连接地址是以TEXTADDR=0xc0008000开始的,这就意味着上述情况依然存在,实际程序中标号和符号都是基于0xc0008000的,但是通过pc我们可以使程序运行的很好,这称作location indepence。也就是说,不管程序的链接地址如何,只要用基于pc寻址的指令(ldr,str,b,bl,adr等),就没有问题。
进入$(TOPDIR)/arch/arm/kernel/head_armv.S
stext=0xc0008000,pc=0x30008000
ENTRY(stext)
mov r12, r0
mov r0, #0
mov r1, #MACH_TYPE_S
mov r0, #F_BIT | I_BIT | MODE_SVC @ make sure svc mode
msr cpsr_c, r0 @ and all irqs disabled
bl __lookup_processor_type @利用pc寻址
teq r10, #0 @ invalid processor?
moveq r0, #'p' @ yes, error 'p'
beq __error @利用pc寻址
bl __lookup_architecture_type @利用pc寻址
teq r7, #0 @ invalid architecture?
moveq r0, #'a' @ yes, error 'a'
beq __error @利用pc寻址
bl __create_page_tables @利用pc寻址
adr lr, __ret @ return address @页表建立,MMU未用,利用pc寻址,lr在0x30008000之中偏移
add pc, r10, #12 @ initialise processor,执行__arm920_setup,在文件$(TOPDIR)/arch/arm/mm/proc-arm920.S
.type __switch_data, %object
__switch_data: .long __mmap_switched @__mmap_switched是以0xc0008000为偏移的
.type __ret, %function
__ret: ldr lr, __switch_data @此时lr=0xc0008000中__mmap_switched的偏移
mcr p15, 0, r0, c1, c0 @开启MMU
mrc p15, 0, r0, c1, c0, 0 @ 此时pc还在0x30008000的空间中,通过0x
mov r0, r0
mov r0, r0
mov pc, lr @质的飞越,真正跳入内核虚空间,pc=0xc0008000+__mmap_switched的偏移
/*
* The following fragment of code is executed with the MMU on, and uses
* absolute addresses; this is not position independent.
*
* r0 = processor control register
* r1 = machine ID
* r9 = processor ID
*/
.align 5
__mmap_switched:
adr r3, __switch_data + 4 @此时所有相对于pc寻址的指令都会在0xc0000000的虚空间中
ldmia r3, {r4, r5, r6, r7, r8, sp}@ r2 = compat
@ sp = stack pointer
mov fp, #0 @ Clear BSS (and zero fp)
1: cmp r4, r5
strcc fp, [r4],#4
bcc 1b
str r9, [r6] @ Save processor ID
str r1, [r7] @ Save machine type
#ifdef CONFIG_ALIGNMENT_TRAP
orr r0, r0, #2 @ ...........A.
#endif
bic r2, r0, #2 @ Clear 'A' bit
stmia r8, {r0, r2} @ Save control register values
b SYMBOL_NAME(start_kernel)
__create_page_tables:
pgtbl r4, r5 @ page table address宏,返回页表物理地址r4=0x30004000
/*
* Clear the 16K level 1 swapper page table
*/
mov r0, r4
mov r3, #0
add r2, r0, #0x4000
1: str r3, [r0], #4
str r3, [r0], #4
str r3, [r0], #4
str r3, [r0], #4
teq r0, r2
bne 1b
/*
* Create identity mapping for first MB of kernel to
* cater for the MMU enable. This identity mapping
* will be removed by paging_init()
*/
krnladr r2, r4, r5 @ start of kernel宏,返回kernel空间的物理起始地址r2=0x30000000
add r3, r8, r2 @ flags + kernel base,r3=0x
str r3, [r4, r2, lsr #18] @ identity mapping,为了使得MMU开启后,pc在未转换到虚地址0xc0008000的空间中之前,还能够继续映射原空间,即在0x
/*
* Now setup the pagetables for our kernel direct
* mapped region. We round TEXTADDR down to the
* nearest megabyte boundary.
*/
add r0, r4, #(TEXTADDR & 0xff000000) >> 18 @ start of kernel,r0=0x30007000,计算第一级入口地址
bic r2, r3, #0x
str r2, [r0] @ PAGE_OFFSET + 0MB
add r0, r0, #(TEXTADDR & 0x
str r3, [r0], #4 @ KERNEL + 0MB @在0x30007000填入第
add r3, r3, #1 << 20
str r3, [r0], #4 @ KERNEL + 1MB @在0x30007004填入第
add r3, r3, #1 << 20
str r3, [r0], #4 @ KERNEL + 2MB @在0x30007008填入第
add r3, r3, #1 << 20
str r3, [r0], #4 @ KERNEL + 3MB @在0x
bic r8, r8, #0x
mov pc, lr
__arm920_setup:
mov r0, #0
mcr p15, 0, r0, c7, c7 @ invalidate I,D caches on v4
mcr p15, 0, r0, c7, c10, 4 @ drain write buffer on v4
mcr p15, 0, r0, c8, c7 @ invalidate I,D TLBs on v4
mcr p15, 0, r4, c2, c0 @ load page table pointer
mov r0, #0x
mcr p15, 0, r0, c3, c0 @ load domain access register
mrc p15, 0, r0, c1, c0 @ get control register v4
/*
* Clear out 'unwanted' bits (then put them in if we need them)
*/
@ VI ZFRS BLDP WCAM
bic r0, r0, #0x0e00
bic r0, r0, #0x0002
bic r0, r0, #0x
bic r0, r0, #0x1000 @ ...0 000. .... 000.
/*
* Turn on what we want
*/
orr r0, r0, #0x0031
orr r0, r0, #0x2100 @ ..1. ...1 ..11 ...1
#ifndef CONFIG_CPU_DCACHE_DISABLE
orr r0, r0, #0x0004 @ .... .... .... .1..
#endif
#ifndef CONFIG_CPU_ICACHE_DISABLE
orr r0, r0, #0x1000 @ ...1 .... .... ....
#endif
mov pc, lr
上面这个只是建立必备的最少的mmu映射,中断及其他ram还有外设的io此时都还没有映射,为什么要这样设置呢,其实应该是为了增加灵活性。
由于此时已经开启mmu,以后要修改或添加mmu就得要使用mmu_base的虚拟地址了,这个就是
.globl swapper_pg_dir
.equ swapper_pg_dir, KERNEL_RAM_VADDR - 0x4000
然后我们进入start_kernel看其他部分的mmu初始化
其实在start_kernel里与mmu映射有关的就是
asmlinkage void __init start_kernel(void)
{
char * command_line;
extern struct kernel_param __start___param[], __stop___param[];
smp_setup_processor_id();
/*
* Need to run as early as possible, to initialize the
* lockdep hash:
*/
lockdep_init();
debug_objects_early_init();
cgroup_init_early();
local_irq_disable();
early_boot_irqs_off();
early_init_irq_lock_class();
/*
* Interrupts are still disabled. Do necessary setups, then
* enable them
*/
lock_kernel();
tick_init();
boot_cpu_init();
page_address_init();
printk(KERN_NOTICE);
printk(linux_banner);
setup_arch(&command_line);
mm_init_owner(&init_mm, &init_task);
就是setup_arch
这个文件在arch/arm/kernel/setup.c中
void __init setup_arch(char **cmdline_p)
{
struct tag *tags = (struct tag *)&init_tags;
struct machine_desc *mdesc;
char *from = default_command_line;
setup_processor();
mdesc = setup_machine(machine_arch_type);
machine_name = mdesc->name;
if (mdesc->soft_reboot)
reboot_setup("s");
if (__atags_pointer)
tags = phys_to_virt(__atags_pointer);
else if (mdesc->boot_params)
tags = phys_to_virt(mdesc->boot_params);
/*
* If we have the old style parameters, convert them to
* a tag list.
*/
if (tags->hdr.tag != ATAG_CORE)
convert_to_tag_list(tags);
if (tags->hdr.tag != ATAG_CORE)
tags = (struct tag *)&init_tags;
if (mdesc->fixup)
mdesc->fixup(mdesc, tags, &from, &meminfo);
if (tags->hdr.tag == ATAG_CORE) {
if (meminfo.nr_banks != 0)
squash_mem_tags(tags);
save_atags(tags);
parse_tags(tags);
}
init_mm.start_code = (unsigned long) _text;
init_mm.end_code = (unsigned long) _etext;
init_mm.end_data = (unsigned long) _edata;
init_mm.brk = (unsigned long) _end;
memcpy(boot_command_line, from, COMMAND_LINE_SIZE);
boot_command_line[COMMAND_LINE_SIZE-1] = '\0';
parse_cmdline(cmdline_p, from);
paging_init(mdesc);
这个paging_init就是和mmu_init有关的
void __init paging_init(struct machine_desc *mdesc)
{
void *zero_page;
build_mem_type_table();
sanity_check_meminfo();
prepare_page_table();
bootmem_init();
devicemaps_init(mdesc);
top_pmd = pmd_off_k(0xffff0000);
/*
* allocate the zero page. Note that this always succeeds and
* returns a zeroed result.
*/
zero_page = alloc_bootmem_low_pages(PAGE_SIZE);
empty_zero_page = virt_to_page(zero_page);
flush_dcache_page(empty_zero_page);
}
devicemaps_init就是和io mmap直接相关的了.
static void __init devicemaps_init(struct machine_desc *mdesc)
{
struct map_desc map;
unsigned long addr;
void *vectors;
/*
* Allocate the vector page early.
*/
vectors = alloc_bootmem_low_pages(PAGE_SIZE);
for (addr = VMALLOC_END; addr; addr += PGDIR_SIZE)
pmd_clear(pmd_off_k(addr));
/*
* Map the kernel if it is XIP.
* It is always first in the modulearea.
*/
#ifdef CONFIG_XIP_KERNEL
map.pfn = __phys_to_pfn(CONFIG_XIP_PHYS_ADDR & SECTION_MASK);
map.virtual = MODULES_VADDR;
map.length = ((unsigned long)_etext - map.virtual + ~SECTION_MASK) & SECTION_MASK;
map.type = MT_ROM;
create_mapping(&map);
#endif
/*
* Map the cache flushing regions.
*/
#ifdef FLUSH_BASE
map.pfn = __phys_to_pfn(FLUSH_BASE_PHYS);
map.virtual = FLUSH_BASE;
map.length = SZ_
map.type = MT_CACHECLEAN;
create_mapping(&map);
#endif
#ifdef FLUSH_BASE_MINICACHE
map.pfn = __phys_to_pfn(FLUSH_BASE_PHYS + SZ_
map.virtual = FLUSH_BASE_MINICACHE;
map.length = SZ_
map.type = MT_MINICLEAN;
create_mapping(&map);
#endif
/*
* Create a mapping for the machine vectors at the high-vectors
* location (0xffff0000). If we aren't using high-vectors, also
* create a mapping at the low-vectors virtual address.
*/
map.pfn = __phys_to_pfn(virt_to_phys(vectors));
map.virtual = 0xffff0000;
map.length = PAGE_SIZE;
map.type = MT_HIGH_VECTORS;
create_mapping(&map);//创建中断向量的映射,将起映射到高端向量地址0xffff0000
if (!vectors_high()) {
map.virtual = 0;
map.type = MT_LOW_VECTORS;
create_mapping(&map);
}
/*
* Ask the machine support to map in the statically mapped devices.
*/
if (mdesc->map_io)
mdesc->map_io();//调用mach的map_io来映射外设的io
/*
* Finally flush the caches and tlb to ensure that we're in a
* consistent state wrt the writebuffer. This also ensures that
* any write-allocated cache lines in the vector page are written
* back. After this point, we can start to touch devices again.
*/
local_flush_tlb_all();
flush_cache_all();
}