当前位置: 首页 > 工具软件 > FDT > 使用案例 >

early_init_fdt_scan_reserved_mem

唐泳
2023-12-01

kenrel: 5.10
arch: arm64

early_init_fdt_scan_reserved_mem查找到“reserved-memory”的节点,将每个子节点中的区域加到memblock.reserverd中,同时根据nomap属性如果有定义,则还要从memblock.memory中删除

/**
 * early_init_fdt_scan_reserved_mem() - create reserved memory regions
 *
 * This function grabs memory from early allocator for device exclusive use
 * defined in device tree structures. It should be called by arch specific code
 * once the early allocator (i.e. memblock) has been fully activated.
 */
void __init early_init_fdt_scan_reserved_mem(void)
{
        int n;
        u64 base, size;

        if (!initial_boot_params)
                return;

        /* Process header /memreserve/ fields */
        for (n = 0; ; n++) {
                fdt_get_mem_rsv(initial_boot_params, n, &base, &size);
                if (!size)
                        break;
                early_init_dt_reserve_memory_arch(base, size, false);
        }    

        of_scan_flat_dt(__fdt_scan_reserved_mem, NULL);
        fdt_init_reserved_mem();
}
  1. 遍历reserved memory节点的每个child node,通过fdt_get_mem_rsv获取预留的物理地址和大小,通过early_init_dt_reserve_memory_arch插入到memblock.reserved中
int fdt_get_mem_rsv(const void *fdt, int n, uint64_t *address, uint64_t *size)                                                                   
{                                                                                                                                                
        const struct fdt_reserve_entry *re;                                                                                                      
                                                                                                                                                 
        FDT_RO_PROBE(fdt);                                                                                                                       
        re = fdt_mem_rsv(fdt, n);                                                                                                                
        if (!can_assume(VALID_INPUT) && !re)                                                                                                     
                return -FDT_ERR_BADOFFSET;                                                                                                       
                                                                                                                                                 
        *address = fdt64_ld(&re->address);                                                                                                       
        *size = fdt64_ld(&re->size);                                                                                                             
        return 0;                                                                                                                                
}

fdt代表dtb所在的虚拟地址,fdt_get_mem_rsv将fdt中第n个reserved memory节点的物理地址和大小保存在address和size中


int __init __weak early_init_dt_reserve_memory_arch(phys_addr_t base,
                                        phys_addr_t size, bool nomap)
{
        if (nomap)
                return memblock_remove(base, size);
        return memblock_reserve(base, size);
}

对于reserved memory节点下带有nomap属性的child node,通过memblock_remove将预留的物理地址区域从memblock.memory中删除,并添加到memblock.reserved区域

  1. 通过of_scan_flat_dt遍历fdt的每个node,对其执行__fdt_scan_reserved_mem,它会查找到“reserved-memory”的节点,将每个子节点中的区域加到memblock.reserverd中,同时根据nomap属性如果有定义,则还要从memblock.memory中删除

  2. fdt_init_reserved_mem
    reserved memory节点下的每一个子节点会对应一个reserved_mem,fdt_init_reserved_mem会遍历每一个reserved_mem,对其执行__reserved_mem_init_node。__reserved_mem_init_node通过将RESERVEDMEM_OF_DECLARE声明的of_device_id与reserved_mem 的compatible属性进行匹配,匹配成功则执行声明的回调,对于cma这个回调函数就是rmem_cma_setup,他会通过reserved_mem来初始化一个cma

/**
 * fdt_init_reserved_mem() - allocate and init all saved reserved memory regions                                                                                                                               
 */
void __init fdt_init_reserved_mem(void)                                                                                                                                                                        
{       
        int i;                                                                                                                                                                                                 
        
        /* check for overlapping reserved regions */                                                                                                                                                           
        __rmem_check_for_overlap();                                                                                                                                                                            
        
        for (i = 0; i < reserved_mem_count; i++) {
                struct reserved_mem *rmem = &reserved_mem[i];                                                                                                                                                  
                unsigned long node = rmem->fdt_node;                                                                                                                                                           
                int len;
                const __be32 *prop;                                                                                                                                                                            
                int err = 0;                                                                                                                                                                                   
                bool nomap;                                                                                                                                                                                    
                
                nomap = of_get_flat_dt_prop(node, "no-map", NULL) != NULL;                                                                                                                                     
                prop = of_get_flat_dt_prop(node, "phandle", &len);                                                                                                                                             
                if (!prop)
                        prop = of_get_flat_dt_prop(node, "linux,phandle", &len);                                                                                                                               
                if (prop)
                        rmem->phandle = of_read_number(prop, len/4);                                                                                                                                           
                
                if (rmem->size == 0)
                        err = __reserved_mem_alloc_size(node, rmem->name,
                                                 &rmem->base, &rmem->size);                                                                                                                                    
                if (err == 0) {
                        err = __reserved_mem_init_node(rmem);                                                                                                                                                  
                        if (err != 0 && err != -ENOENT) {
                                pr_info("node %s compatible matching fail\n",                                                                                                                                  
                                        rmem->name);
                                memblock_free(rmem->base, rmem->size);                                                                                                                                         
                                if (nomap)
                                        memblock_add(rmem->base, rmem->size);                                                                                                                                  
                        }                                                                                                                                                                                      
                }                                                                                                                                                                                              
        }                                                                                                                                                                                                      
} 
 类似资料:

相关阅读

相关文章

相关问答