对于ioremap的使用需要通过early_memremap和early_iounmap进行。由于对应于ioremap的内存空间是有限的,所以对于ioremap空间的使用遵照使用结束马上释放的原则。这就是说early_memremap和early_iounmap必须配对使用并且访问结束必须马上执行unmap。
- static void __init __iomem *
- __early_ioremap(resource_size_t phys_addr, unsigned long size, pgprot_t prot)
- {
- unsigned long offset;
- resource_size_t last_addr;
- unsigned int nrpages;
- enum fixed_addresses idx0, idx;
- int i, slot;
- WARN_ON(system_state != SYSTEM_BOOTING);
- slot = -1;
- /*pre_map[]是一个索引与slot_virt[]一一对应,这段for
- 的含义在于找到一个没有被使用过的slot_virt[i]的页面,
- 该slot_virt[i]所指向的虚拟页面地址就是将会和实际物
- 理地址phys_addr相绑定的虚拟地址。*/
- for (i = 0; i < FIX_BTMAPS_SLOTS; i++) {
- if (!prev_map[i]) {
- slot = i;
- break;
- }
- }
- if (slot < 0) {
- printk(KERN_INFO "early_iomap(%08llx, %08lx) not found slot\n",
- (u64)phys_addr, size);
- WARN_ON(1);
- return NULL;
- }
- if (early_ioremap_debug) {
- printk(KERN_INFO "early_ioremap(%08llx, %08lx) [%d] => ",
- (u64)phys_addr, size, slot);
- dump_stack();
- }
- /* Don't allow wraparound or zero size */
- last_addr = phys_addr + size - 1;
- if (!size || last_addr < phys_addr) {
- WARN_ON(1);
- return NULL;
- }
- prev_size[slot] = size;
- /*
- * Mappings have to be page-aligned
- */
- offset = phys_addr & ~PAGE_MASK;/*offset是页内的偏移*/
- phys_addr &= PAGE_MASK;/*现在phys_addr就是起始页面的地址*/
- /*现在size就是指出了到底占据了多少个页面的大小*/
- size = PAGE_ALIGN(last_addr + 1) - phys_addr;
- /*
- * Mappings have to fit in the FIX_BTMAP area.
- */
- /*到底我们需要多少页面*/
- nrpages = size >> PAGE_SHIFT;
- if (nrpages > NR_FIX_BTMAPS) {
- WARN_ON(1);
- return NULL;
- }
- /*
- * Ok, go for it..
- */
- /*找到空闲slot所对应的fixed_address中的索引号*/
- idx0 = FIX_BTMAP_BEGIN - NR_FIX_BTMAPS*slot;
- idx = idx0;
- while (nrpages > 0) {
- /*在bm_ptes中将指定的idx索引的页表项填充为对应的物理地址使得bm_pte[idx]指向正确的物理页面地址*/
- early_set_fixmap(idx, phys_addr, prot);
- phys_addr += PAGE_SIZE;
- --idx;
- --nrpages;
- }
- if (early_ioremap_debug)
- printk(KERN_CONT "%08lx + %08lx\n", offset, slot_virt[slot]);
- /*返回phys_addr所指向的虚拟地址*/
- prev_map[slot] = (void __iomem *)(offset + slot_virt[slot]);
- return prev_map[slot];
- }
