1 /* SPDX-License-Identifier: GPL-2.0+ */ 2 /* 3 * (C) Copyright 2019 Rockchip Electronics Co., Ltd 4 */ 5 6 #ifndef _SYSMEM_H 7 #define _SYSMEM_H 8 9 #include <memblk.h> 10 #include <malloc.h> 11 12 /* 13 * CONFIG_SYS_FDT_PAD default value is sync with bootm framework in: 14 * common/image-fdt.c 15 */ 16 #ifndef CONFIG_SYS_FDT_PAD 17 #define CONFIG_SYS_FDT_PAD 0x3000 18 #endif 19 20 struct sysmem { 21 struct lmb lmb; 22 struct list_head allocated_head; 23 ulong allocated_cnt; 24 bool has_initf; 25 bool has_initr; 26 }; 27 28 #ifdef CONFIG_SYSMEM 29 /** 30 * sysmem_has_init() - Is sysmem initialized 31 * 32 * @return true or false 33 */ 34 bool sysmem_has_init(void); 35 36 /** 37 * sysmem_init() - Sysmem initialization 38 * 39 * @return 0 on success, otherwise error 40 */ 41 int sysmem_init(void); 42 43 /** 44 * sysmem_initr() - Sysmem initialization after relocation 45 * 46 * @return 0 on success, otherwise error 47 */ 48 int sysmem_initr(void); 49 50 /** 51 * sysmem_alloc() - Alloc sysmem region at anywhere 52 * 53 * @id: memblk id 54 * @size: region size 55 * 56 * @return NULL on error, otherwise the allocated region address ptr 57 */ 58 void *sysmem_alloc(enum memblk_id id, phys_size_t size); 59 60 /** 61 * sysmem_alloc_base() - Alloc sysmem region at the expect addr 62 * 63 * @id: memblk id 64 * @base: region base 65 * @size: region size 66 * 67 * @return NULL on error, otherwise the allocated region address ptr 68 */ 69 void *sysmem_alloc_base(enum memblk_id id, phys_addr_t base, phys_size_t size); 70 71 /** 72 * sysmem_alloc_base_by_name() - Alloc sysmem region at the expect addr by name 73 * 74 * @name: memblk name 75 * @base: region base 76 * @size: region size 77 * 78 * @return NULL on error, otherwise the allocated region address ptr 79 */ 80 void *sysmem_alloc_base_by_name(const char *name, 81 phys_addr_t base, phys_size_t size); 82 83 /** 84 * sysmem_fdt_reserve_alloc_base() - Alloc sysmem region at the expect addr by name, 85 * called only for reserve memory from fdt. 86 * 87 * @name: memblk name 88 * @base: region base 89 * @size: region size 90 * 91 * @return NULL on error, otherwise the allocated region address ptr 92 */ 93 void *sysmem_fdt_reserve_alloc_base(const char *name, 94 phys_addr_t base, phys_size_t size); 95 96 /** 97 * sysmem_can_alloc() - Check if the region can be allocated 98 * 99 * @base: region base 100 * @size: region size 101 * 102 * @return true on okay. 103 */ 104 bool sysmem_can_alloc(phys_size_t base, phys_size_t size); 105 106 /** 107 * sysmem_free() - Free allocated sysmem region 108 * 109 * @base: region base 110 * 111 * @return 0 on success, otherwise error 112 */ 113 int sysmem_free(phys_addr_t base); 114 115 /** 116 * sysmem_dump() - Dump all sysmem region state 117 */ 118 void sysmem_dump(void); 119 120 /** 121 * sysmem_overflow_check() - Sysmem regions overflow check 122 */ 123 void sysmem_overflow_check(void); 124 125 /** 126 * board_sysmem_reserve() - Weak function for board to implement 127 * 128 * @sysmem: global sysmem point, ignored 129 * 130 * @return 0 on success, otherwise error 131 */ 132 int board_sysmem_reserve(struct sysmem *sysmem); 133 #else 134 static inline bool sysmem_has_init(void) { return false; } 135 static inline int sysmem_init(void) { return 0; } 136 static inline int sysmem_initr(void) { return 0; } 137 static inline int sysmem_free(phys_addr_t base) { return 0; } 138 static inline void sysmem_dump(void) {} 139 void sysmem_overflow_check(void) {} 140 141 __weak int board_sysmem_reserve(struct sysmem *sysmem) { return 0; } 142 143 static inline void *sysmem_alloc(enum memblk_id id, phys_size_t size) 144 { 145 return malloc(size); 146 } 147 148 static inline bool sysmem_can_alloc(phys_size_t base, phys_size_t size) 149 { 150 return true; 151 } 152 153 static inline void *sysmem_alloc_base(enum memblk_id id, 154 phys_addr_t base, phys_size_t size) 155 { 156 return (void *)base; 157 } 158 static inline void *sysmem_alloc_base_by_name(const char *name, 159 phys_addr_t base, 160 phys_size_t size) 161 { 162 return (void *)base; 163 } 164 static inline void *sysmem_fdt_reserve_alloc_base(const char *name, 165 phys_addr_t base, 166 phys_size_t size) 167 { 168 return (void *)base; 169 } 170 #endif /* CONFIG_SYSMEM */ 171 #endif /* _SYSMEM_H */ 172