lib: sysmem: refactor code

- import memblk id to manage memory blocks;
- change "sysmem_property" to generic "memblock";
- use alloc instead of reserve for all memory blocks;
- clean up and fix some logic;
- add U-Boot cmd for sysmem;

Change-Id: I614223ce3bf97a7b3566412a9d1864fb30b68fd8
Signed-off-by: Joseph Chen <chenjh@rock-chips.com>
This commit is contained in:
Joseph Chen 2019-03-14 18:23:56 +08:00 committed by Jianhong Chen
parent 88bfa97963
commit 6e15146eff
16 changed files with 458 additions and 407 deletions

View File

@ -7,28 +7,28 @@
#ifndef __ROCKCHIP_PARAM_H_ #ifndef __ROCKCHIP_PARAM_H_
#define __ROCKCHIP_PARAM_H_ #define __ROCKCHIP_PARAM_H_
#include <sysmem.h> #include <memblk.h>
/** /**
* param_parse_atf_mem() - Parse atf memory region * param_parse_atf_mem() - Parse atf memory region
* *
* @return sysmem_property structure which contains base and size info. * @return memblock structure which contains base and size info.
*/ */
struct sysmem_property param_parse_atf_mem(void); struct memblock param_parse_atf_mem(void);
/** /**
* param_parse_atf_mem() - Parse op-tee memory region * param_parse_atf_mem() - Parse op-tee memory region
* *
* @return sysmem_property structure which contains base and size info. * @return memblock structure which contains base and size info.
*/ */
struct sysmem_property param_parse_optee_mem(void); struct memblock param_parse_optee_mem(void);
/** /**
* param_parse_atf_mem() - Parse platform common reserved memory region * param_parse_atf_mem() - Parse platform common reserved memory region
* *
* @return sysmem_property structure which contains base and size info. * @return memblock structure which contains base and size info.
*/ */
struct sysmem_property param_parse_common_resv_mem(void); struct memblock param_parse_common_resv_mem(void);
/** /**
* param_parse_bootdev() - Parse boot device info. * param_parse_bootdev() - Parse boot device info.

View File

@ -24,6 +24,7 @@ obj-y += boot_mode.o
obj-y += board.o obj-y += board.o
obj-y += chip_info.o obj-y += chip_info.o
obj-y += iomem.o obj-y += iomem.o
obj-y += memblk.o
obj-$(CONFIG_ROCKCHIP_CRC) += rockchip_crc.o obj-$(CONFIG_ROCKCHIP_CRC) += rockchip_crc.o
obj-$(CONFIG_ROCKCHIP_SMCCC) += rockchip_smccc.o obj-$(CONFIG_ROCKCHIP_SMCCC) += rockchip_smccc.o

View File

@ -442,34 +442,6 @@ void board_lmb_reserve(struct lmb *lmb)
} }
#endif #endif
#ifdef CONFIG_SYSMEM
int board_sysmem_reserve(struct sysmem *sysmem)
{
struct sysmem_property prop;
int ret;
/* ATF */
prop = param_parse_atf_mem();
ret = sysmem_reserve(prop.name, prop.base, prop.size);
if (ret)
return ret;
/* PSTORE/ATAGS/SHM */
prop = param_parse_common_resv_mem();
ret = sysmem_reserve(prop.name, prop.base, prop.size);
if (ret)
return ret;
/* OP-TEE */
prop = param_parse_optee_mem();
ret = sysmem_reserve(prop.name, prop.base, prop.size);
if (ret)
return ret;
return 0;
}
#endif
#if defined(CONFIG_ROCKCHIP_PRELOADER_SERIAL) && \ #if defined(CONFIG_ROCKCHIP_PRELOADER_SERIAL) && \
defined(CONFIG_ROCKCHIP_PRELOADER_ATAGS) defined(CONFIG_ROCKCHIP_PRELOADER_ATAGS)
int board_init_f_init_serial(void) int board_init_f_init_serial(void)

View File

@ -0,0 +1,66 @@
// SPDX-License-Identifier: GPL-2.0
/*
* Copyright (c) 2019 Fuzhou Rockchip Electronics Co., Ltd
*/
#include <common.h>
#include <memblk.h>
const static struct memblk_attr plat_mem_attr[MEMBLK_ID_MAX] = {
[MEMBLK_ID_DEMO] = {
.name = "DEMO",
.flags = M_ATTR_NONE,
},
[MEMBLK_ID_ATF] = {
.name = "ATF",
.flags = M_ATTR_NONE,
},
[MEMBLK_ID_OPTEE] = {
.name = "OP-TEE",
.flags = M_ATTR_NONE,
},
[MEMBLK_ID_SHM] = {
.name = "SHM",
.flags = M_ATTR_NONE,
},
[MEMBLK_ID_UBOOT] = {
.name = "U-Boot",
.flags = M_ATTR_OVERLAP,
},
[MEMBLK_ID_FASTBOOT] = {
.name = "FASTBOOT",
.flags = M_ATTR_OVERLAP,
},
[MEMBLK_ID_STACK] = {
.name = "STACK",
.flags = M_ATTR_HOFC | M_ATTR_OVERLAP,
},
[MEMBLK_ID_FDT] = {
.name = "FDT",
.flags = M_ATTR_OFC,
},
[MEMBLK_ID_FDT_DTBO] = {
.name = "FDT_DTBO",
.flags = M_ATTR_OFC,
},
[MEMBLK_ID_FDT_AOSP] = {
.name = "FDT_AOSP",
.flags = M_ATTR_OFC,
},
[MEMBLK_ID_RAMDISK] = {
.name = "RAMDISK",
.alias[0] = "BOOT",
.alias[1] = "RECOVERY",
.flags = M_ATTR_OFC,
},
[MEMBLK_ID_KERNEL] = {
.name = "KERNEL",
.flags = M_ATTR_OFC,
},
[MEMBLK_ID_ANDROID] = {
.name = "ANDROID",
.flags = M_ATTR_OFC,
},
};
const struct memblk_attr *mem_attr = plat_mem_attr;

View File

@ -4,6 +4,8 @@
*/ */
#include <common.h> #include <common.h>
#include <dm.h>
#include <ram.h>
#include <asm/io.h> #include <asm/io.h>
#include <asm/arch/rk_atags.h> #include <asm/arch/rk_atags.h>
#include <asm/arch/param.h> #include <asm/arch/param.h>
@ -47,13 +49,12 @@ static uint16_t trust_checksum(const uint8_t *buf, uint16_t len)
return checksum; return checksum;
} }
struct sysmem_property param_parse_atf_mem(void) struct memblock param_parse_atf_mem(void)
{ {
struct sysmem_property prop; struct memblock mem;
prop.name = "ATF"; mem.base = 0;
prop.base = 0; mem.size = 0;
prop.size = 0;
#ifdef CONFIG_ROCKCHIP_PRELOADER_ATAGS #ifdef CONFIG_ROCKCHIP_PRELOADER_ATAGS
struct tag *t = NULL; struct tag *t = NULL;
@ -66,41 +67,40 @@ struct sysmem_property param_parse_atf_mem(void)
*/ */
t = atags_get_tag(ATAG_ATF_MEM); t = atags_get_tag(ATAG_ATF_MEM);
if (t && t->u.atf_mem.size) { if (t && t->u.atf_mem.size) {
prop.base = t->u.atf_mem.phy_addr; mem.base = t->u.atf_mem.phy_addr;
prop.size = t->u.atf_mem.size; mem.size = t->u.atf_mem.size;
/* Sanity */ /* Sanity */
if (prop.base + prop.size > SDRAM_OFFSET(SZ_1M)) { if (mem.base + mem.size > SDRAM_OFFSET(SZ_1M)) {
printf("%s: ATF reserved region is not within 0-1MB " printf("%s: ATF reserved region is not within 0-1MB "
"offset(0x%08llx-0x%08llx)!\n", "offset(0x%08llx-0x%08llx)!\n",
__func__, (u64)prop.base, (u64)prop.base + prop.size); __func__, (u64)mem.base, (u64)mem.base + mem.size);
return prop; return mem;
} }
} }
#endif #endif
/* Legacy */ /* Legacy */
if (!prop.size) { if (!mem.size) {
if (IS_ENABLED(CONFIG_ARM64) || if (IS_ENABLED(CONFIG_ARM64) ||
IS_ENABLED(CONFIG_ARM64_BOOT_AARCH32)) { IS_ENABLED(CONFIG_ARM64_BOOT_AARCH32)) {
prop.base = SDRAM_OFFSET(0); mem.base = SDRAM_OFFSET(0);
prop.size = SZ_1M; mem.size = SZ_1M;
} }
} }
debug("ATF: 0x%llx - 0x%llx\n", (u64)prop.base, (u64)prop.base + prop.size); debug("ATF: 0x%llx - 0x%llx\n", (u64)mem.base, (u64)mem.base + mem.size);
return prop; return mem;
} }
struct sysmem_property param_parse_optee_mem(void) struct memblock param_parse_optee_mem(void)
{ {
struct tos_param_t *tos_parameter; struct tos_param_t *tos_parameter;
struct sysmem_property prop; struct memblock mem;
u32 checksum; u32 checksum;
prop.name = "OP-TEE"; mem.base = 0;
prop.base = 0; mem.size = 0;
prop.size = 0;
#ifdef CONFIG_ROCKCHIP_PRELOADER_ATAGS #ifdef CONFIG_ROCKCHIP_PRELOADER_ATAGS
struct tag *t = NULL; struct tag *t = NULL;
@ -113,13 +113,13 @@ struct sysmem_property param_parse_optee_mem(void)
*/ */
t = atags_get_tag(ATAG_TOS_MEM); t = atags_get_tag(ATAG_TOS_MEM);
if (t && (t->u.tos_mem.tee_mem.flags == 1)) { if (t && (t->u.tos_mem.tee_mem.flags == 1)) {
prop.base = t->u.tos_mem.tee_mem.phy_addr; mem.base = t->u.tos_mem.tee_mem.phy_addr;
prop.size = t->u.tos_mem.tee_mem.size; mem.size = t->u.tos_mem.tee_mem.size;
} }
#endif #endif
/* Legacy */ /* Legacy */
if (!prop.size) { if (!mem.size) {
tos_parameter = tos_parameter =
(struct tos_param_t *)(SDRAM_OFFSET(PARAM_OPTEE_INFO_OFFSET)); (struct tos_param_t *)(SDRAM_OFFSET(PARAM_OPTEE_INFO_OFFSET));
checksum = checksum =
@ -127,28 +127,27 @@ struct sysmem_property param_parse_optee_mem(void)
sizeof(struct tos_param_t) - 8); sizeof(struct tos_param_t) - 8);
if ((checksum == tos_parameter->checksum) && if ((checksum == tos_parameter->checksum) &&
(tos_parameter->tee_mem.flags == 1)) { (tos_parameter->tee_mem.flags == 1)) {
prop.base = tos_parameter->tee_mem.phy_addr; mem.base = tos_parameter->tee_mem.phy_addr;
prop.size = tos_parameter->tee_mem.size; mem.size = tos_parameter->tee_mem.size;
} }
} }
if (prop.size) if (mem.size)
gd->flags |= GD_FLG_BL32_ENABLED; gd->flags |= GD_FLG_BL32_ENABLED;
debug("TOS: 0x%llx - 0x%llx\n", (u64)prop.base, (u64)prop.base + prop.size); debug("TOS: 0x%llx - 0x%llx\n", (u64)mem.base, (u64)mem.base + mem.size);
return prop; return mem;
} }
struct sysmem_property param_parse_common_resv_mem(void) struct memblock param_parse_common_resv_mem(void)
{ {
struct sysmem_property prop; struct memblock mem;
prop.name = "PSTORE/ATAGS/SHM"; mem.base = SDRAM_OFFSET(SZ_1M);
prop.base = SDRAM_OFFSET(SZ_1M); mem.size = SZ_1M;
prop.size = SZ_1M;
return prop; return mem;
} }
int param_parse_bootdev(char **devtype, char **devnum) int param_parse_bootdev(char **devtype, char **devnum)

View File

@ -664,7 +664,7 @@ int rockchip_read_dtb_file(void *fdt_addr)
if (size < 0) if (size < 0)
return size; return size;
if (!sysmem_alloc_base("fdt", (phys_addr_t)fdt_addr, if (!sysmem_alloc_base(MEMBLK_ID_FDT, (phys_addr_t)fdt_addr,
ALIGN(size, RK_BLK_SIZE) + CONFIG_SYS_FDT_PAD)) ALIGN(size, RK_BLK_SIZE) + CONFIG_SYS_FDT_PAD))
return -ENOMEM; return -ENOMEM;

View File

@ -5,6 +5,7 @@
*/ */
#include <common.h> #include <common.h>
#include <memblk.h>
#include <dm.h> #include <dm.h>
#include <ram.h> #include <ram.h>
#include <asm/io.h> #include <asm/io.h>
@ -23,7 +24,7 @@ DECLARE_GLOBAL_DATA_PTR;
#define NOT_INITIAL -1 #define NOT_INITIAL -1
static int __dram_init_banksize(int resv_tee) static int __dram_init_banksize(int resv_tee)
{ {
struct sysmem_property prop; struct memblock prop;
size_t top = min((unsigned long)(gd->ram_size + CONFIG_SYS_SDRAM_BASE), size_t top = min((unsigned long)(gd->ram_size + CONFIG_SYS_SDRAM_BASE),
gd->ram_top); gd->ram_top);
u64 start[CONFIG_NR_DRAM_BANKS], size[CONFIG_NR_DRAM_BANKS]; u64 start[CONFIG_NR_DRAM_BANKS], size[CONFIG_NR_DRAM_BANKS];

View File

@ -65,7 +65,7 @@ static int do_fastboot(cmd_tbl_t *cmdtp, int flag, int argc, char *const argv[])
goto exit; goto exit;
} }
if (!sysmem_alloc_base("fastboot", if (!sysmem_alloc_base(MEMBLK_ID_FASTBOOT,
CONFIG_FASTBOOT_BUF_ADDR, CONFIG_FASTBOOT_BUF_ADDR,
CONFIG_FASTBOOT_BUF_SIZE)) { CONFIG_FASTBOOT_BUF_SIZE)) {
printf("The fastboot memory space is unusable!\n"); printf("The fastboot memory space is unusable!\n");

View File

@ -332,8 +332,6 @@ int android_bootloader_boot_kernel(unsigned long kernel_address)
printf("Booting kernel at %s with fdt at %s...\n\n\n", printf("Booting kernel at %s with fdt at %s...\n\n\n",
kernel_addr_r, fdt_addr); kernel_addr_r, fdt_addr);
sysmem_dump_check();
do_bootm(NULL, 0, 4, bootm_args); do_bootm(NULL, 0, 4, bootm_args);
return -1; return -1;
@ -813,7 +811,7 @@ int android_fdt_overlay_apply(void *fdt_addr)
if (sysmem_free((phys_addr_t)fdt_addr)) if (sysmem_free((phys_addr_t)fdt_addr))
goto out; goto out;
if (!sysmem_alloc_base("fdt(dtbo)", if (!sysmem_alloc_base(MEMBLK_ID_FDT_DTBO,
(phys_addr_t)fdt_addr, (phys_addr_t)fdt_addr,
fdt_size + CONFIG_SYS_FDT_PAD)) fdt_size + CONFIG_SYS_FDT_PAD))
goto out; goto out;

View File

@ -88,12 +88,10 @@ static void boot_lmb_init(bootm_headers_t *images)
* return the image size on success, and a * return the image size on success, and a
* negative value on error. * negative value on error.
*/ */
static int read_rockchip_image(struct blk_desc *dev_desc, int read_rockchip_image(struct blk_desc *dev_desc,
disk_partition_t *part_info, disk_partition_t *part_info, void *dst)
void *dst)
{ {
struct rockchip_image *img; struct rockchip_image *img;
const char *name;
int header_len = 8; int header_len = 8;
int cnt; int cnt;
int ret; int ret;
@ -101,14 +99,6 @@ static int read_rockchip_image(struct blk_desc *dev_desc,
u32 crc32; u32 crc32;
#endif #endif
if (!strcmp((char *)part_info->name, "kernel"))
name = "kernel";
else if (!strcmp((char *)part_info->name, "boot") ||
!strcmp((char *)part_info->name, "recovery"))
name = "ramdisk";
else
name = NULL;
img = memalign(ARCH_DMA_MINALIGN, RK_BLK_SIZE); img = memalign(ARCH_DMA_MINALIGN, RK_BLK_SIZE);
if (!img) { if (!img) {
printf("out of memory\n"); printf("out of memory\n");
@ -133,7 +123,9 @@ static int read_rockchip_image(struct blk_desc *dev_desc,
* total size = image size + 8 bytes header + 4 bytes crc32 * total size = image size + 8 bytes header + 4 bytes crc32
*/ */
cnt = DIV_ROUND_UP(img->size + 8 + 4, RK_BLK_SIZE); cnt = DIV_ROUND_UP(img->size + 8 + 4, RK_BLK_SIZE);
if (!sysmem_alloc_base(name, (phys_addr_t)dst, cnt * dev_desc->blksz)) { if (!sysmem_alloc_base_by_name((const char *)part_info->name,
(phys_addr_t)dst,
cnt * dev_desc->blksz)) {
ret = -ENXIO; ret = -ENXIO;
goto err; goto err;
} }
@ -524,7 +516,6 @@ int boot_rockchip_image(struct blk_desc *dev_desc, disk_partition_t *boot_part)
printf("ramdisk @ 0x%08lx (0x%08x)\n", ramdisk_addr_r, ramdisk_size); printf("ramdisk @ 0x%08lx (0x%08x)\n", ramdisk_addr_r, ramdisk_size);
fdt_ramdisk_skip_relocation(); fdt_ramdisk_skip_relocation();
sysmem_dump_check();
#if defined(CONFIG_ARM64) #if defined(CONFIG_ARM64)
char cmdbuf[64]; char cmdbuf[64];

View File

@ -290,7 +290,7 @@ static int android_image_load_separate(struct blk_desc *dev_desc,
size = hdr->kernel_size + hdr->page_size; size = hdr->kernel_size + hdr->page_size;
blk_start = part->start; blk_start = part->start;
blk_cnt = DIV_ROUND_UP(size, dev_desc->blksz); blk_cnt = DIV_ROUND_UP(size, dev_desc->blksz);
if (!sysmem_alloc_base("kernel", if (!sysmem_alloc_base(MEMBLK_ID_KERNEL,
(phys_addr_t)android_load_address, (phys_addr_t)android_load_address,
blk_cnt * dev_desc->blksz)) blk_cnt * dev_desc->blksz))
return -ENXIO; return -ENXIO;
@ -311,7 +311,7 @@ static int android_image_load_separate(struct blk_desc *dev_desc,
size = hdr->page_size + ALIGN(hdr->kernel_size, hdr->page_size); size = hdr->page_size + ALIGN(hdr->kernel_size, hdr->page_size);
blk_start = part->start + DIV_ROUND_UP(size, dev_desc->blksz); blk_start = part->start + DIV_ROUND_UP(size, dev_desc->blksz);
blk_cnt = DIV_ROUND_UP(hdr->ramdisk_size, dev_desc->blksz); blk_cnt = DIV_ROUND_UP(hdr->ramdisk_size, dev_desc->blksz);
if (!sysmem_alloc_base("ramdisk", if (!sysmem_alloc_base(MEMBLK_ID_RAMDISK,
ramdisk_addr_r, ramdisk_addr_r,
blk_cnt * dev_desc->blksz)) blk_cnt * dev_desc->blksz))
return -ENXIO; return -ENXIO;
@ -347,7 +347,7 @@ static int android_image_load_separate(struct blk_desc *dev_desc,
ALIGN(hdr->ramdisk_size, hdr->page_size); ALIGN(hdr->ramdisk_size, hdr->page_size);
blk_start = part->start + DIV_ROUND_UP(size, dev_desc->blksz); blk_start = part->start + DIV_ROUND_UP(size, dev_desc->blksz);
blk_cnt = DIV_ROUND_UP(hdr->second_size, dev_desc->blksz); blk_cnt = DIV_ROUND_UP(hdr->second_size, dev_desc->blksz);
if (!sysmem_alloc_base("fdt(AOSP)", if (!sysmem_alloc_base(MEMBLK_ID_FDT_AOSP,
fdt_addr_r, fdt_addr_r,
blk_cnt * dev_desc->blksz + blk_cnt * dev_desc->blksz +
CONFIG_SYS_FDT_PAD)) CONFIG_SYS_FDT_PAD))
@ -456,7 +456,7 @@ long android_image_load(struct blk_desc *dev_desc,
} else } else
#endif #endif
{ {
if (!sysmem_alloc_base("android", if (!sysmem_alloc_base(MEMBLK_ID_ANDROID,
(phys_addr_t)buf, (phys_addr_t)buf,
blk_cnt * part_info->blksz)) blk_cnt * part_info->blksz))
return -ENXIO; return -ENXIO;

View File

@ -143,7 +143,6 @@ int boot_fdt_add_sysmem_rsv_regions(void *fdt_blob)
static int rsv_done; static int rsv_done;
char resvname[32]; char resvname[32];
const void *prop; const void *prop;
int ret;
if (fdt_check_header(fdt_blob) != 0 || rsv_done) if (fdt_check_header(fdt_blob) != 0 || rsv_done)
return -EINVAL; return -EINVAL;
@ -157,9 +156,8 @@ int boot_fdt_add_sysmem_rsv_regions(void *fdt_blob)
debug(" sysmem: reserving fdt memory region: addr=%llx size=%llx\n", debug(" sysmem: reserving fdt memory region: addr=%llx size=%llx\n",
(unsigned long long)addr, (unsigned long long)size); (unsigned long long)addr, (unsigned long long)size);
sprintf(resvname, "fdt-memory-reserved%d", i); sprintf(resvname, "fdt-memory-reserved%d", i);
ret = sysmem_reserve(resvname, addr, size); if (!sysmem_fdt_reserve_alloc_base(resvname, addr, size))
if (ret) return -ENOMEM;
return ret;
} }
rsv_offset = fdt_subnode_offset(fdt_blob, 0, "reserved-memory"); rsv_offset = fdt_subnode_offset(fdt_blob, 0, "reserved-memory");
@ -181,10 +179,9 @@ int boot_fdt_add_sysmem_rsv_regions(void *fdt_blob)
debug(" sysmem: 'reserved-memory' %s: addr=%llx size=%llx\n", debug(" sysmem: 'reserved-memory' %s: addr=%llx size=%llx\n",
fdt_get_name(fdt_blob, offset, NULL), fdt_get_name(fdt_blob, offset, NULL),
(unsigned long long)rsv_addr, (unsigned long long)rsv_size); (unsigned long long)rsv_addr, (unsigned long long)rsv_size);
ret = sysmem_reserve(fdt_get_name(fdt_blob, offset, NULL), if (!sysmem_fdt_reserve_alloc_base(fdt_get_name(fdt_blob, offset, NULL),
rsv_addr, rsv_size); rsv_addr, rsv_size))
if (ret) return -ENOMEM;
return ret;
} }
return 0; return 0;

View File

@ -29,6 +29,9 @@ enum _boot_mode {
int rockchip_get_boot_mode(void); int rockchip_get_boot_mode(void);
int boot_rockchip_image(struct blk_desc *dev, disk_partition_t *boot_part); int boot_rockchip_image(struct blk_desc *dev, disk_partition_t *boot_part);
int read_rockchip_image(struct blk_desc *dev_desc,
disk_partition_t *part_info, void *dst);
struct blk_desc *rockchip_get_bootdev(void); struct blk_desc *rockchip_get_bootdev(void);
/* /*

View File

@ -693,6 +693,8 @@ int boot_get_fdt(int flag, int argc, char * const argv[], uint8_t arch,
void boot_fdt_add_mem_rsv_regions(struct lmb *lmb, void *fdt_blob); void boot_fdt_add_mem_rsv_regions(struct lmb *lmb, void *fdt_blob);
#ifdef CONFIG_SYSMEM #ifdef CONFIG_SYSMEM
int boot_fdt_add_sysmem_rsv_regions(void *fdt_blob); int boot_fdt_add_sysmem_rsv_regions(void *fdt_blob);
#else
static inline int boot_fdt_add_sysmem_rsv_regions(void *fdt_blob) { return 0; }
#endif #endif
int boot_relocate_fdt(struct lmb *lmb, char **of_flat_tree, ulong *of_size); int boot_relocate_fdt(struct lmb *lmb, char **of_flat_tree, ulong *of_size);

View File

@ -6,12 +6,7 @@
#ifndef _SYSMEM_H #ifndef _SYSMEM_H
#define _SYSMEM_H #define _SYSMEM_H
#include <asm/types.h> #include <memblk.h>
#define MAX_SYSMEM_REGIONS 64
#undef MAX_LMB_REGIONS
#define MAX_LMB_REGIONS MAX_SYSMEM_REGIONS
/* /*
* CONFIG_SYS_FDT_PAD default value is sync with bootm framework in: * CONFIG_SYS_FDT_PAD default value is sync with bootm framework in:
@ -21,21 +16,22 @@
#define CONFIG_SYS_FDT_PAD 0x3000 #define CONFIG_SYS_FDT_PAD 0x3000
#endif #endif
struct sysmem_property {
const char *name;
phys_addr_t base;
phys_size_t size;
struct list_head node;
};
struct sysmem { struct sysmem {
struct lmb lmb; struct lmb lmb;
struct list_head allocated_head; struct list_head allocated_head;
struct list_head reserved_head;
ulong allocated_cnt; ulong allocated_cnt;
bool has_init; bool has_initf;
bool has_initr;
}; };
#ifdef CONFIG_SYSMEM
/**
* sysmem_has_init() - Is sysmem initialized
*
* @return true or false
*/
bool sysmem_has_init(void);
/** /**
* sysmem_init() - Sysmem initialization * sysmem_init() - Sysmem initialization
* *
@ -44,73 +40,60 @@ struct sysmem {
int sysmem_init(void); int sysmem_init(void);
/** /**
* sysmem_add() - Add sysmem region * sysmem_initr() - Sysmem initialization after relocation
*
* @base: region base address
* @size: region size
* *
* @return 0 on success, otherwise error * @return 0 on success, otherwise error
*/ */
int sysmem_add(phys_addr_t base, phys_size_t size); int sysmem_initr(void);
/**
* sysmem_reserve() - Reserve sysmem region
*
* @name: region name
* @base: region base address
* @size: region size
*
* @return 0 on success, otherwise error
*/
int sysmem_reserve(const char *name, phys_addr_t base, phys_size_t size);
/** /**
* sysmem_alloc() - Alloc sysmem region at anywhere * sysmem_alloc() - Alloc sysmem region at anywhere
* *
* @name: region name * @id: memblk id
* @size: region size * @size: region size
* *
* @return NULL on error, otherwise the allocated region address ptr * @return NULL on error, otherwise the allocated region address ptr
*/ */
void *sysmem_alloc(const char *name, phys_size_t size); void *sysmem_alloc(enum memblk_id id, phys_size_t size);
/**
* sysmem_alloc_align() - Alloc sysmem region at anywhere with addr align down
*
* @name: region name
* @size: region size
* @align: region base address align (down)
*
* @return NULL on error, otherwise the allocated region address ptr
*/
void *sysmem_alloc_align(const char *name, phys_size_t size, ulong align);
/** /**
* sysmem_alloc_base() - Alloc sysmem region at the expect addr * sysmem_alloc_base() - Alloc sysmem region at the expect addr
* *
* @name: region name * @id: memblk id
* @base: region base * @base: region base
* @size: region size * @size: region size
* *
* @return NULL on error, otherwise the allocated region address ptr * @return NULL on error, otherwise the allocated region address ptr
*/ */
void *sysmem_alloc_base(const char *name, phys_addr_t base, phys_size_t size); void *sysmem_alloc_base(enum memblk_id id, phys_addr_t base, phys_size_t size);
/** /**
* sysmem_alloc_align_base() - Alloc sysmem region at the expect addr with align down * sysmem_alloc_base_by_name() - Alloc sysmem region at the expect addr by name
* *
* @name: region name * @name: memblk name
* @base: region base * @base: region base
* @size: region size * @size: region size
* @align: region base address align (down)
* *
* @return NULL on error, otherwise the allocated region address ptr * @return NULL on error, otherwise the allocated region address ptr
*/ */
void *sysmem_alloc_align_base(const char *name, phys_addr_t base, void *sysmem_alloc_base_by_name(const char *name,
phys_size_t size, ulong align); phys_addr_t base, phys_size_t size);
/** /**
* sysmem_free() - Free sysmem region * sysmem_fdt_reserve_alloc_base() - Alloc sysmem region at the expect addr by name,
* called only for reserve memory from fdt.
*
* @name: memblk name
* @base: region base
* @size: region size
*
* @return NULL on error, otherwise the allocated region address ptr
*/
void *sysmem_fdt_reserve_alloc_base(const char *name,
phys_addr_t base, phys_size_t size);
/**
* sysmem_free() - Free allocated sysmem region
* *
* @base: region base * @base: region base
* *
@ -119,22 +102,10 @@ void *sysmem_alloc_align_base(const char *name, phys_addr_t base,
int sysmem_free(phys_addr_t base); int sysmem_free(phys_addr_t base);
/** /**
* sysmem_check() - Check sysmem regions * sysmem_dump() - Dump all sysmem region state and check overflow
*
* @return 0 on okay, otherwise something wrong
*/
int sysmem_check(void);
/**
* sysmem_dump_all() - Dump all sysmem stat
*/ */
void sysmem_dump(void); void sysmem_dump(void);
/**
* sysmem_dump_check() - Dump all sysmem stat and check overflow
*/
int sysmem_dump_check(void);
/** /**
* board_sysmem_reserve() - Weak function for board to implement * board_sysmem_reserve() - Weak function for board to implement
* *
@ -143,14 +114,29 @@ int sysmem_dump_check(void);
* @return 0 on success, otherwise error * @return 0 on success, otherwise error
*/ */
int board_sysmem_reserve(struct sysmem *sysmem); int board_sysmem_reserve(struct sysmem *sysmem);
#else
/** static inline bool sysmem_has_init(void) { return false; }
* arch_sysmem_reserve() - Weak function for arch to implement static inline int sysmem_init(void) { return 0; }
* static inline int sysmem_initr(void) { return 0; }
* @sysmem: global sysmem point, ignored static inline int sysmem_free(phys_addr_t base) { return 0; }
* static inline void sysmem_dump(void) {}
* @return 0 on success, otherwise error __weak int board_sysmem_reserve(struct sysmem *sysmem) { return 0; }
*/ static inline void *sysmem_alloc_base(enum memblk_id id,
int arch_sysmem_reserve(struct sysmem *sysmem); phys_addr_t base, phys_size_t size)
{
return (void *)base;
}
static inline void *sysmem_alloc_base_by_name(const char *name,
phys_addr_t base,
phys_size_t size)
{
return (void *)base;
}
static inline void *sysmem_fdt_reserve_alloc_base(const char *name,
phys_addr_t base,
phys_size_t size)
{
return (void *)base;
}
#endif /* CONFIG_SYSMEM */
#endif /* _SYSMEM_H */ #endif /* _SYSMEM_H */

View File

@ -15,45 +15,38 @@ DECLARE_GLOBAL_DATA_PTR;
#define SYSMEM_ALLOC_ANYWHERE 0 #define SYSMEM_ALLOC_ANYWHERE 0
#define SYSMEM_ALLOC_NO_ALIGN 1 #define SYSMEM_ALLOC_NO_ALIGN 1
#ifndef CONFIG_SYS_STACK_SIZE
#define CONFIG_SYS_STACK_SIZE SZ_2M
#endif
#define SIZE_MB(len) ((len) >> 20)
#define SIZE_KB(len) (((len) % (1 << 20)) >> 10)
#define SYSMEM_I(fmt, args...) printf("Sysmem: "fmt, ##args) #define SYSMEM_I(fmt, args...) printf("Sysmem: "fmt, ##args)
#define SYSMEM_W(fmt, args...) printf("Sysmem Warn: "fmt, ##args) #define SYSMEM_W(fmt, args...) printf("Sysmem Warn: "fmt, ##args)
#define SYSMEM_E(fmt, args...) printf("Sysmem Error: "fmt, ##args) #define SYSMEM_E(fmt, args...) printf("Sysmem Error: "fmt, ##args)
#define SYSMEM_D(fmt, args...) debug("Sysmem Debug: "fmt, ##args) #define SYSMEM_D(fmt, args...) debug("Sysmem Debug: "fmt, ##args)
static struct sysmem plat_sysmem; /* Global for platform */ struct memcheck {
struct sysmem_check {
uint32_t magic; uint32_t magic;
}; };
static int sysmem_has_init(void) /* Global for platform, must in data section */
{ struct sysmem plat_sysmem __section(".data") = {
if (!plat_sysmem.has_init) { .has_initf = false,
SYSMEM_E("Framework is not initialized\n"); .has_initr = false,
return 0; };
}
return 1; bool sysmem_has_init(void)
{
return gd->flags & GD_FLG_RELOC ?
plat_sysmem.has_initr : plat_sysmem.has_initf;
} }
void sysmem_dump(void) void sysmem_dump(void)
{ {
#ifdef DEBUG
struct sysmem *sysmem = &plat_sysmem; struct sysmem *sysmem = &plat_sysmem;
struct lmb *lmb = &sysmem->lmb; struct lmb *lmb = &sysmem->lmb;
struct sysmem_property *prop; struct memblock *mem;
struct sysmem_check *check; struct memcheck *check;
struct list_head *node; struct list_head *node;
ulong memory_size = 0; ulong memory_size = 0;
ulong reserved_size = 0; ulong reserved_size = 0;
ulong allocated_size = 0; ulong allocated_size = 0;
bool overflow = false;
ulong i; ulong i;
if (!sysmem_has_init()) if (!sysmem_has_init())
@ -62,12 +55,13 @@ void sysmem_dump(void)
printf("\nsysmem_dump_all:\n"); printf("\nsysmem_dump_all:\n");
/* Memory pool */ /* Memory pool */
printf(" ------------------------------------------------------\n"); printf(" --------------------------------------------------------------------\n");
for (i = 0; i < lmb->memory.cnt; i++) { for (i = 0; i < lmb->memory.cnt; i++) {
memory_size += lmb->memory.region[i].size; memory_size += lmb->memory.region[i].size;
printf(" memory.rgn[%ld].base = 0x%08lx\n", i, printf(" memory.rgn[%ld].addr = 0x%08lx - 0x%08lx (size: 0x%08lx)\n", i,
(ulong)lmb->memory.region[i].base); (ulong)lmb->memory.region[i].base,
printf(" .size = 0x%08lx\n", (ulong)lmb->memory.region[i].base +
(ulong)lmb->memory.region[i].size,
(ulong)lmb->memory.region[i].size); (ulong)lmb->memory.region[i].size);
} }
printf("\n memory.total = 0x%08lx (%ld MiB. %ld KiB)\n", printf("\n memory.total = 0x%08lx (%ld MiB. %ld KiB)\n",
@ -75,54 +69,48 @@ void sysmem_dump(void)
SIZE_MB((ulong)memory_size), SIZE_MB((ulong)memory_size),
SIZE_KB((ulong)memory_size)); SIZE_KB((ulong)memory_size));
/* Reserved */
i = 0;
printf(" ------------------------------------------------------\n");
list_for_each(node, &sysmem->reserved_head) {
prop = list_entry(node, struct sysmem_property, node);
reserved_size += prop->size;
printf(" reserved.rgn[%ld].name = \"%s\"\n", i, prop->name);
printf(" .base = 0x%08lx\n",
(ulong)prop->base);
printf(" .size = 0x%08lx\n",
(ulong)prop->size);
i++;
}
printf("\n reserved.total = 0x%08lx (%ld MiB. %ld KiB)\n",
(ulong)reserved_size,
SIZE_MB((ulong)reserved_size),
SIZE_KB((ulong)reserved_size));
/* Allocated */ /* Allocated */
i = 0; i = 0;
printf(" ------------------------------------------------------\n"); printf(" --------------------------------------------------------------------\n");
list_for_each(node, &sysmem->allocated_head) { list_for_each(node, &sysmem->allocated_head) {
prop = list_entry(node, struct sysmem_property, node); mem = list_entry(node, struct memblock, node);
allocated_size += prop->size; allocated_size += mem->size;
check = (struct sysmem_check *) if (mem->attr.flags & M_ATTR_OFC) {
(prop->base + prop->size - sizeof(*check)); check = (struct memcheck *)
printf(" allocated.rgn[%ld].name = \"%s\"%s\n", (mem->base + mem->size - sizeof(*check));
i, prop->name, overflow = (check->magic != SYSMEM_MAGIC);
check->magic != SYSMEM_MAGIC ? " (Overflow)" : ""); } else if (mem->attr.flags & M_ATTR_HOFC) {
printf(" .base = 0x%08lx\n", check = (struct memcheck *)
(ulong)prop->base); (mem->base - sizeof(*check));
printf(" .size = 0x%08lx\n", overflow = (check->magic != SYSMEM_MAGIC);
(ulong)prop->size); } else {
overflow = false;
}
printf(" allocated.rgn[%ld].name = \"%s\" %s\n",
i, mem->attr.name, overflow ? " <Overflow!>" : "");
printf(" .addr = 0x%08lx - 0x%08lx (size: 0x%08lx)\n",
(ulong)mem->base, (ulong)(mem->base + mem->size),
(ulong)mem->size);
i++; i++;
} }
printf("\n malloc_r: %d MiB, malloc_f: %d KiB\n",
SIZE_MB(CONFIG_SYS_MALLOC_LEN), SIZE_KB(CONFIG_SYS_MALLOC_F_LEN));
printf("\n allocated.total = 0x%08lx (%ld MiB. %ld KiB)\n", printf("\n allocated.total = 0x%08lx (%ld MiB. %ld KiB)\n",
(ulong)allocated_size, (ulong)allocated_size,
SIZE_MB((ulong)allocated_size), SIZE_MB((ulong)allocated_size),
SIZE_KB((ulong)allocated_size)); SIZE_KB((ulong)allocated_size));
/* LMB core reserved */ /* LMB core reserved */
printf(" ------------------------------------------------------\n"); printf(" --------------------------------------------------------------------\n");
reserved_size = 0; reserved_size = 0;
for (i = 0; i < lmb->reserved.cnt; i++) { for (i = 0; i < lmb->reserved.cnt; i++) {
reserved_size += lmb->reserved.region[i].size; reserved_size += lmb->reserved.region[i].size;
printf(" LMB.reserved[%ld].base = 0x%08lx\n", i, printf(" LMB.reserved[%ld].addr = 0x%08lx - 0x%08lx (size: 0x%08lx)\n", i,
(ulong)lmb->reserved.region[i].base); (ulong)lmb->reserved.region[i].base,
printf(" .size = 0x%08lx\n", (ulong)lmb->reserved.region[i].base +
(ulong)lmb->reserved.region[i].size,
(ulong)lmb->reserved.region[i].size); (ulong)lmb->reserved.region[i].size);
} }
@ -130,167 +118,159 @@ void sysmem_dump(void)
(ulong)reserved_size, (ulong)reserved_size,
SIZE_MB((ulong)reserved_size), SIZE_MB((ulong)reserved_size),
SIZE_KB((ulong)reserved_size)); SIZE_KB((ulong)reserved_size));
printf(" ------------------------------------------------------\n\n"); printf(" --------------------------------------------------------------------\n\n");
#endif
} }
int sysmem_check(void) static inline int sysmem_is_overlap(phys_addr_t base1, phys_size_t size1,
{ phys_addr_t base2, phys_size_t size2)
struct sysmem *sysmem = &plat_sysmem;
struct sysmem_property *prop;
struct sysmem_check *check;
struct list_head *node;
int ret = 0;
if (!sysmem_has_init())
return -ENOSYS;
/* Check allocated */
list_for_each(node, &sysmem->allocated_head) {
prop = list_entry(node, struct sysmem_property, node);
check = (struct sysmem_check *)
(prop->base + prop->size - sizeof(*check));
if (check->magic != SYSMEM_MAGIC) {
ret = -EOVERFLOW;
SYSMEM_E("\"%s\" (base=0x%08lx, size=0x%lx) is Overflow!\n",
prop->name, (ulong)prop->base, (ulong)prop->size);
}
}
/* Check stack */
check = (struct sysmem_check *)(gd->start_addr_sp - CONFIG_SYS_STACK_SIZE);
if (check->magic != SYSMEM_MAGIC) {
ret = -EOVERFLOW;
SYSMEM_E("Runtime stack is Overflow!\n");
}
return ret;
}
int sysmem_dump_check(void)
{
sysmem_dump();
return sysmem_check();
}
static int sysmem_is_overlap(phys_addr_t base1, phys_size_t size1,
phys_addr_t base2, phys_size_t size2)
{ {
return ((base1 < (base2 + size2)) && (base2 < (base1 + size1))); return ((base1 < (base2 + size2)) && (base2 < (base1 + size1)));
} }
int sysmem_add(phys_addr_t base, phys_size_t size) static int sysmem_add(phys_addr_t base, phys_size_t size)
{ {
struct sysmem *sysmem = &plat_sysmem; struct sysmem *sysmem = &plat_sysmem;
int ret; int ret;
if (!sysmem_has_init()) if (!size)
return -ENOSYS; return -EINVAL;
ret = lmb_add(&sysmem->lmb, base, size); ret = lmb_add(&sysmem->lmb, base, size);
if (ret < 0) if (ret < 0)
SYSMEM_E("Failed to add sysmem at 0x%lx for 0x%lx size\n", SYSMEM_E("Failed to add sysmem at 0x%08lx for 0x%08lx size\n",
(ulong)base, (ulong)size); (ulong)base, (ulong)size);
return (ret >= 0) ? 0 : ret; return (ret >= 0) ? 0 : ret;
} }
int sysmem_reserve(const char *name, phys_addr_t base, phys_size_t size) static const char *sysmem_alias2name(const char *name, int *id)
{ {
struct sysmem *sysmem = &plat_sysmem; const char *alias;
struct sysmem_property *prop; int n, i, j;
struct list_head *node; int match = 0;
int ret = 0;
if (!sysmem_has_init()) for (i = 0; i < MEMBLK_ID_MAX; i++) {
return -ENOSYS; /* Pirmary name */
if (mem_attr[i].name && !strcasecmp(mem_attr[i].name, name)) {
match = 1;
goto finish;
}
if (!name) { /* Alias name */
SYSMEM_E("NULL name for reserved sysmem\n"); alias = mem_attr[i].alias[0];
return -EINVAL; if (!alias)
} continue;
/* Check overlap */ n = ARRAY_SIZE(mem_attr[i].alias);
list_for_each(node, &sysmem->reserved_head) { for (j = 0; j < n; j++, alias++) {
prop = list_entry(node, struct sysmem_property, node); if (alias && !strcasecmp(alias, name)) {
if (!strcmp(prop->name, name)) { match = 1;
SYSMEM_E("Failed to double reserve for existence \"%s\"\n", name); goto finish;
return -EEXIST; }
} else if (sysmem_is_overlap(prop->base, prop->size, base, size)) {
SYSMEM_D("\"%s\" (base=0x%08lx, size=0x%lx) reserve is "
"overlap with existence \"%s\" (base=0x%08lx, size=0x%lx)\n",
name, (ulong)base, (ulong)size, prop->name,
(ulong)prop->base, (ulong)prop->size);
} }
} }
ret = lmb_reserve(&sysmem->lmb, base, size); finish:
if (ret >= 0) { if (match) {
prop = malloc(sizeof(*prop)); *id = i;
if (!prop) { return mem_attr[i].name;
SYSMEM_E("No memory for \"%s\" reserve sysmem\n", name);
return -ENOMEM;
}
prop->name = name;
prop->base = base;
prop->size = size;
list_add_tail(&prop->node, &sysmem->reserved_head);
} else {
SYSMEM_E("Failed to reserve \"%s\" at 0x%lx\n", name, (ulong)base);
return -EINVAL;
} }
return 0; return name;
} }
void *sysmem_alloc_align_base(const char *name, static void *sysmem_alloc_align_base(enum memblk_id id,
phys_addr_t base, const char *mem_name,
phys_size_t size, phys_addr_t base,
ulong align) phys_size_t size,
ulong align)
{ {
struct sysmem *sysmem = &plat_sysmem; struct sysmem *sysmem = &plat_sysmem;
struct sysmem_property *prop; struct memblk_attr attr;
struct sysmem_check *check; struct memblock *mem;
struct memcheck *check;
struct list_head *node; struct list_head *node;
const char *name;
phys_addr_t paddr; phys_addr_t paddr;
phys_addr_t alloc_base; phys_addr_t alloc_base;
phys_size_t alloc_size; phys_size_t alloc_size;
bool req_overlap = false;
if (!sysmem_has_init()) if (!sysmem_has_init())
return NULL; return NULL;
if (!name) { if (id == MEMBLK_ID_BY_NAME || id == MEMBLK_ID_FDT_RESV) {
SYSMEM_E("NULL name for alloc sysmem\n"); if (!mem_name) {
SYSMEM_E("NULL name for alloc sysmem\n");
return NULL;
} else if (id == MEMBLK_ID_FDT_RESV) {
req_overlap = true;
if (base >= gd->ram_top)
return (void *)base;
}
name = sysmem_alias2name(mem_name, (int *)&id);
attr = mem_attr[id];
} else if (id > MEMBLK_ID_UNK && id < MEMBLK_ID_MAX) {
attr = mem_attr[id];
name = attr.name;
} else {
SYSMEM_E("Unsupport memblk id %d for alloc sysmem\n", id);
return NULL;
}
if (!size) {
SYSMEM_E("\"%s\" size is 0 for alloc sysmem\n", name);
return NULL; return NULL;
} }
if (!IS_ALIGNED(base, 4)) { if (!IS_ALIGNED(base, 4)) {
SYSMEM_E("\"%s\" base=0x%08lx is not 4-byte aligned\n", name, (ulong)base); SYSMEM_E("\"%s\" base=0x%08lx is not 4-byte aligned\n",
name, (ulong)base);
return NULL; return NULL;
} }
/* Must be 4-byte aligned */ /* Must be 4-byte aligned */
size = ALIGN(size, 4); size = ALIGN(size, 4);
SYSMEM_D("Enter alloc: \"%s\" 0x%08lx - 0x%08lx\n",
name, (ulong)base, (ulong)(base + size));
/* Already allocated ? */ /* Already allocated ? */
list_for_each(node, &sysmem->allocated_head) { list_for_each(node, &sysmem->allocated_head) {
prop = list_entry(node, struct sysmem_property, node); mem = list_entry(node, struct memblock, node);
if (!strcmp(prop->name, name)) { SYSMEM_D("Has allcated: %s, 0x%08lx - 0x%08lx\n",
mem->attr.name, (ulong)mem->base,
(ulong)(mem->base + mem->size));
if (!strcmp(mem->attr.name, name)) {
SYSMEM_E("Failed to double alloc for existence \"%s\"\n", name); SYSMEM_E("Failed to double alloc for existence \"%s\"\n", name);
return NULL; return NULL;
} else if (sysmem_is_overlap(prop->base, prop->size, base, size)) { } else if (sysmem_is_overlap(mem->base, mem->size, base, size)) {
SYSMEM_E("\"%s\" (base=0x%08lx, size=0x%lx) alloc is " if (req_overlap && mem->attr.flags & M_ATTR_OVERLAP) {
"overlap with existence \"%s\" (base=0x%08lx, size=0x%lx)\n", if (lmb_reserve(&sysmem->lmb, base, size))
name, (ulong)base, (ulong)size, SYSMEM_E("Failed to overlap alloc \"%s\" "
prop->name, (ulong)prop->base, "at 0x%08lx - 0x%08lx\n",
(ulong)prop->size); name, (ulong)base,
(ulong)(base + size));
return (void *)base;
}
SYSMEM_E("\"%s\" (0x%08lx - 0x%08lx) alloc is "
"overlap with existence \"%s\" (0x%08lx - "
"0x%08lx)\n",
name, (ulong)base, (ulong)(base + size),
mem->attr.name, (ulong)mem->base,
(ulong)(mem->base + mem->size));
return NULL; return NULL;
} }
} }
alloc_size = size + sizeof(*check); /* Add overflow check magic ? */
if (attr.flags & M_ATTR_OFC)
alloc_size = size + sizeof(*check);
else
alloc_size = size;
/* Alloc anywhere ? */
if (base == SYSMEM_ALLOC_ANYWHERE) if (base == SYSMEM_ALLOC_ANYWHERE)
alloc_base = base; alloc_base = base;
else else
@ -298,127 +278,170 @@ void *sysmem_alloc_align_base(const char *name,
paddr = lmb_alloc_base(&sysmem->lmb, alloc_size, align, alloc_base); paddr = lmb_alloc_base(&sysmem->lmb, alloc_size, align, alloc_base);
if (paddr) { if (paddr) {
if ((paddr == base) || (base == SYSMEM_ALLOC_ANYWHERE)) { if ((paddr == base) || (base == SYSMEM_ALLOC_ANYWHERE)) {
prop = malloc(sizeof(*prop)); mem = malloc(sizeof(*mem));
if (!prop) { if (!mem) {
SYSMEM_E("No memory for \"%s\" alloc sysmem\n", name); SYSMEM_E("No memory for \"%s\" alloc sysmem\n", name);
return NULL; return NULL;
} }
prop->name = name; mem->base = paddr;
prop->base = paddr; mem->size = alloc_size;
prop->size = alloc_size; mem->attr = attr;
sysmem->allocated_cnt++; sysmem->allocated_cnt++;
list_add_tail(&mem->node, &sysmem->allocated_head);
check = (struct sysmem_check *)(paddr + size); if (mem->attr.flags & M_ATTR_OFC) {
check->magic = SYSMEM_MAGIC; check = (struct memcheck *)(paddr + size);
check->magic = SYSMEM_MAGIC;
list_add_tail(&prop->node, &sysmem->allocated_head); } else if (mem->attr.flags & M_ATTR_HOFC) {
check = (struct memcheck *)(paddr - sizeof(*check));
check->magic = SYSMEM_MAGIC;
}
} else { } else {
SYSMEM_E("Failed to alloc \"%s\" at expect 0x%lx but " SYSMEM_E("Failed to alloc \"%s\" expect at 0x%08lx - 0x%08lx "
"alloc at 0x%lx\n", "but at 0x%08lx - x%08lx\n",
name, (ulong)base, (ulong)paddr); name, (ulong)base, (ulong)(base + size),
(ulong)paddr, (ulong)(paddr + size));
if (lmb_free(&sysmem->lmb, paddr, alloc_size))
SYSMEM_E("Failed to free \"%s\"\n", name);
return NULL; return NULL;
} }
} else { } else {
SYSMEM_E("Failed to alloc \"%s\" at 0x%lx\n", name, (ulong)base); SYSMEM_E("Failed to alloc \"%s\" at 0x%08lx - 0x%08lx\n",
name, (ulong)base, (ulong)(base + size));
} }
SYSMEM_D("Alloc: \"%s\", paddr=0x%lx, size=0x%lx, align=0x%x, anywhere=%d\n", SYSMEM_D("Exit alloc: \"%s\", paddr=0x%08lx, size=0x%08lx, align=0x%x, anywhere=%d\n",
name, (ulong)paddr, (ulong)size, (u32)align, !base); name, (ulong)paddr, (ulong)size, (u32)align, !base);
return (void *)paddr; return (void *)paddr;
} }
void *sysmem_alloc_align(const char *name, phys_size_t size, ulong align) void *sysmem_alloc_base(enum memblk_id id, phys_addr_t base, phys_size_t size)
{ {
return sysmem_alloc_align_base(name, void *paddr;
SYSMEM_ALLOC_ANYWHERE,
size, paddr = sysmem_alloc_align_base(id,
align); NULL,
base,
size,
SYSMEM_ALLOC_NO_ALIGN);
if (!paddr)
sysmem_dump();
return paddr;
} }
void *sysmem_alloc_base(const char *name, phys_addr_t base, phys_size_t size) void *sysmem_alloc_base_by_name(const char *name,
phys_addr_t base, phys_size_t size)
{ {
return sysmem_alloc_align_base(name, void *paddr;
base,
size, paddr = sysmem_alloc_align_base(MEMBLK_ID_BY_NAME,
SYSMEM_ALLOC_NO_ALIGN); name,
base,
size,
SYSMEM_ALLOC_NO_ALIGN);
if (!paddr)
sysmem_dump();
return paddr;
} }
void *sysmem_alloc(const char *name, phys_size_t size) void *sysmem_fdt_reserve_alloc_base(const char *name,
phys_addr_t base, phys_size_t size)
{ {
return sysmem_alloc_align_base(name, void *paddr;
SYSMEM_ALLOC_ANYWHERE,
size, paddr = sysmem_alloc_align_base(MEMBLK_ID_FDT_RESV,
SYSMEM_ALLOC_NO_ALIGN); name,
base,
size,
SYSMEM_ALLOC_NO_ALIGN);
if (!paddr)
sysmem_dump();
return paddr;
} }
int sysmem_free(phys_addr_t base) int sysmem_free(phys_addr_t base)
{ {
struct sysmem *sysmem = &plat_sysmem; struct sysmem *sysmem = &plat_sysmem;
struct sysmem_property *prop; struct memblock *mem;
struct list_head *node; struct list_head *node;
int found = 0; int ret, found = 0;
int ret;
if (!sysmem_has_init()) if (!sysmem_has_init())
return -ENOSYS; return -ENOSYS;
/* Find existence */ /* Find existence */
list_for_each(node, &sysmem->allocated_head) { list_for_each(node, &sysmem->allocated_head) {
prop = list_entry(node, struct sysmem_property, node); mem = list_entry(node, struct memblock, node);
if (prop->base == base) { if (mem->base == base) {
found = 1; found = 1;
break; break;
} }
} }
if (!found) { if (!found) {
SYSMEM_E("Failed to free no allocated sysmem at 0x%lx\n", (ulong)base); SYSMEM_E("Failed to free no allocated sysmem at 0x%08lx\n",
(ulong)base);
return -EINVAL; return -EINVAL;
} }
ret = lmb_free(&sysmem->lmb, prop->base, prop->size); ret = lmb_free(&sysmem->lmb, mem->base, mem->size);
if (ret >= 0) { if (ret >= 0) {
SYSMEM_D("Free: \"%s\", paddr=0x%lx, size=0x%lx\n", SYSMEM_D("Free: \"%s\" 0x%08lx - 0x%08lx\n",
prop->name, (ulong)prop->base, (ulong)prop->size); mem->attr.name, (ulong)mem->base,
(ulong)(mem->base + mem->size));
sysmem->allocated_cnt--; sysmem->allocated_cnt--;
list_del(&prop->node); list_del(&mem->node);
free(prop); free(mem);
} else { } else {
SYSMEM_E("Failed to free \"%s\" at 0x%lx\n", prop->name, (ulong)base); SYSMEM_E("Failed to free \"%s\" at 0x%08lx\n",
mem->attr.name, (ulong)base);
} }
return (ret >= 0) ? 0 : ret; return (ret >= 0) ? 0 : ret;
} }
int sysmem_initr(void)
{
return sysmem_init();
}
int sysmem_init(void) int sysmem_init(void)
{ {
struct sysmem *sysmem = &plat_sysmem; struct sysmem *sysmem = &plat_sysmem;
struct sysmem_check *check;
phys_addr_t mem_start; phys_addr_t mem_start;
phys_size_t mem_size; phys_size_t mem_size;
int ret; int ret;
SYSMEM_I("init\n");
lmb_init(&sysmem->lmb); lmb_init(&sysmem->lmb);
INIT_LIST_HEAD(&sysmem->allocated_head); INIT_LIST_HEAD(&sysmem->allocated_head);
INIT_LIST_HEAD(&sysmem->reserved_head);
sysmem->allocated_cnt = 0; sysmem->allocated_cnt = 0;
sysmem->has_init = true; if (gd->flags & GD_FLG_RELOC) {
sysmem->has_initr = true;
} else {
SYSMEM_I("init\n");
sysmem->has_initf = true;
}
/* Add all available system memory */ /* Add all available system memory */
#ifdef CONFIG_NR_DRAM_BANKS #ifdef CONFIG_NR_DRAM_BANKS
int i; int i;
for (i = 0; i < CONFIG_NR_DRAM_BANKS; i++) { for (i = 0; i < CONFIG_NR_DRAM_BANKS; i++) {
if (!gd->bd->bi_dram[i].size)
continue;
ret = sysmem_add(gd->bd->bi_dram[i].start, ret = sysmem_add(gd->bd->bi_dram[i].start,
gd->bd->bi_dram[i].size); gd->bd->bi_dram[i].size);
if (ret) { if (ret) {
SYSMEM_E("Failed to add sysmem from bi_dram[%d]\n", i); SYSMEM_E("Failed to add sysmem from bi_dram[%d]\n", i);
return ret; goto fail;
} }
} }
#else #else
@ -427,39 +450,44 @@ int sysmem_init(void)
ret = sysmem_add(mem_start, mem_size); ret = sysmem_add(mem_start, mem_size);
if (ret) { if (ret) {
SYSMEM_E("Failed to add sysmem from bootm_low/size\n"); SYSMEM_E("Failed to add sysmem from bootm_low/size\n");
return ret; goto fail;
} }
#endif #endif
/* Reserved for arch */
ret = arch_sysmem_reserve(sysmem);
if (ret) {
SYSMEM_E("Failed to reserve sysmem for arch\n");
return ret;
}
/* Reserved for board */ /* Reserved for board */
ret = board_sysmem_reserve(sysmem); ret = board_sysmem_reserve(sysmem);
if (ret) { if (ret) {
SYSMEM_E("Failed to reserve sysmem for board\n"); SYSMEM_E("Failed to reserve sysmem for board\n");
return ret; goto fail;
} }
/* Reserved for U-boot framework 'reserve_xxx()' */ /* Reserved for U-boot framework: 'reserve_xxx()' */
mem_start = gd->start_addr_sp - CONFIG_SYS_STACK_SIZE; mem_start = gd->start_addr_sp;
mem_size = gd->ram_top - mem_start; mem_size = gd->ram_top - mem_start;
check = (struct sysmem_check *)mem_start; if (!sysmem_alloc_base(MEMBLK_ID_UBOOT, mem_start, mem_size)) {
check->magic = SYSMEM_MAGIC;
ret = sysmem_reserve("U-Boot", mem_start, mem_size);
if (ret) {
SYSMEM_E("Failed to reserve sysmem for U-Boot framework\n"); SYSMEM_E("Failed to reserve sysmem for U-Boot framework\n");
return ret; ret = -ENOMEM;
goto fail;
} }
sysmem_dump(); /* Reserved for U-Boot stack */
mem_start = gd->start_addr_sp - CONFIG_SYS_STACK_SIZE;
mem_size = CONFIG_SYS_STACK_SIZE;
if (!sysmem_alloc_base(MEMBLK_ID_STACK, mem_start, mem_size)) {
SYSMEM_E("Failed to reserve sysmem for stack\n");
ret = -ENOMEM;
goto fail;
}
return 0; return 0;
fail:
if (ret && !(gd->flags & GD_FLG_RELOC)) {
sysmem_dump();
SYSMEM_W("Maybe malloc size %d MiB is too large?\n\n",
SIZE_MB(CONFIG_SYS_MALLOC_LEN));
}
return ret;
} }
__weak int board_sysmem_reserve(struct sysmem *sysmem) __weak int board_sysmem_reserve(struct sysmem *sysmem)
@ -468,8 +496,15 @@ __weak int board_sysmem_reserve(struct sysmem *sysmem)
return 0; return 0;
} }
__weak int arch_sysmem_reserve(struct sysmem *sysmem) static int do_dump_sysmem(cmd_tbl_t *cmdtp, int flag,
int argc, char *const argv[])
{ {
/* please define platform specific arch_sysmem_reserve() */ sysmem_dump();
return 0; return 0;
} }
U_BOOT_CMD(
dump_sysmem, 1, 1, do_dump_sysmem,
"Dump sysmem layout",
""
);