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