1 /* 2 * Device tree based initialization code for reserved memory. 3 * 4 * Copyright (c) 2013, 2015 The Linux Foundation. All Rights Reserved. 5 * Copyright (c) 2013,2014 Samsung Electronics Co., Ltd. 6 * http://www.samsung.com 7 * Author: Marek Szyprowski <m.szyprowski@samsung.com> 8 * Author: Josh Cartwright <joshc@codeaurora.org> 9 * 10 * This program is free software; you can redistribute it and/or 11 * modify it under the terms of the GNU General Public License as 12 * published by the Free Software Foundation; either version 2 of the 13 * License or (at your optional) any later version of the license. 14 */ 15 16 #include <linux/err.h> 17 #include <linux/of.h> 18 #include <linux/of_fdt.h> 19 #include <linux/of_platform.h> 20 #include <linux/mm.h> 21 #include <linux/sizes.h> 22 #include <linux/of_reserved_mem.h> 23 #include <linux/sort.h> 24 25 #define MAX_RESERVED_REGIONS 16 26 static struct reserved_mem reserved_mem[MAX_RESERVED_REGIONS]; 27 static int reserved_mem_count; 28 29 #if defined(CONFIG_HAVE_MEMBLOCK) 30 #include <linux/memblock.h> 31 int __init __weak early_init_dt_alloc_reserved_memory_arch(phys_addr_t size, 32 phys_addr_t align, phys_addr_t start, phys_addr_t end, bool nomap, 33 phys_addr_t *res_base) 34 { 35 /* 36 * We use __memblock_alloc_base() because memblock_alloc_base() 37 * panic()s on allocation failure. 38 */ 39 phys_addr_t base = __memblock_alloc_base(size, align, end); 40 if (!base) 41 return -ENOMEM; 42 43 /* 44 * Check if the allocated region fits in to start..end window 45 */ 46 if (base < start) { 47 memblock_free(base, size); 48 return -ENOMEM; 49 } 50 51 *res_base = base; 52 if (nomap) 53 return memblock_remove(base, size); 54 return 0; 55 } 56 #else 57 int __init __weak early_init_dt_alloc_reserved_memory_arch(phys_addr_t size, 58 phys_addr_t align, phys_addr_t start, phys_addr_t end, bool nomap, 59 phys_addr_t *res_base) 60 { 61 pr_err("Reserved memory not supported, ignoring region 0x%llx%s\n", 62 size, nomap ? " (nomap)" : ""); 63 return -ENOSYS; 64 } 65 #endif 66 67 /** 68 * res_mem_save_node() - save fdt node for second pass initialization 69 */ 70 void __init fdt_reserved_mem_save_node(unsigned long node, const char *uname, 71 phys_addr_t base, phys_addr_t size) 72 { 73 struct reserved_mem *rmem = &reserved_mem[reserved_mem_count]; 74 75 if (reserved_mem_count == ARRAY_SIZE(reserved_mem)) { 76 pr_err("Reserved memory: not enough space all defined regions.\n"); 77 return; 78 } 79 80 rmem->fdt_node = node; 81 rmem->name = uname; 82 rmem->base = base; 83 rmem->size = size; 84 85 reserved_mem_count++; 86 return; 87 } 88 89 /** 90 * res_mem_alloc_size() - allocate reserved memory described by 'size', 'align' 91 * and 'alloc-ranges' properties 92 */ 93 static int __init __reserved_mem_alloc_size(unsigned long node, 94 const char *uname, phys_addr_t *res_base, phys_addr_t *res_size) 95 { 96 int t_len = (dt_root_addr_cells + dt_root_size_cells) * sizeof(__be32); 97 phys_addr_t start = 0, end = 0; 98 phys_addr_t base = 0, align = 0, size; 99 int len; 100 const __be32 *prop; 101 int nomap; 102 int ret; 103 104 prop = of_get_flat_dt_prop(node, "size", &len); 105 if (!prop) 106 return -EINVAL; 107 108 if (len != dt_root_size_cells * sizeof(__be32)) { 109 pr_err("Reserved memory: invalid size property in '%s' node.\n", 110 uname); 111 return -EINVAL; 112 } 113 size = dt_mem_next_cell(dt_root_size_cells, &prop); 114 115 nomap = of_get_flat_dt_prop(node, "no-map", NULL) != NULL; 116 117 prop = of_get_flat_dt_prop(node, "alignment", &len); 118 if (prop) { 119 if (len != dt_root_addr_cells * sizeof(__be32)) { 120 pr_err("Reserved memory: invalid alignment property in '%s' node.\n", 121 uname); 122 return -EINVAL; 123 } 124 align = dt_mem_next_cell(dt_root_addr_cells, &prop); 125 } 126 127 /* Need adjust the alignment to satisfy the CMA requirement */ 128 if (IS_ENABLED(CONFIG_CMA) && of_flat_dt_is_compatible(node, "shared-dma-pool")) 129 align = max(align, (phys_addr_t)PAGE_SIZE << max(MAX_ORDER - 1, pageblock_order)); 130 131 prop = of_get_flat_dt_prop(node, "alloc-ranges", &len); 132 if (prop) { 133 134 if (len % t_len != 0) { 135 pr_err("Reserved memory: invalid alloc-ranges property in '%s', skipping node.\n", 136 uname); 137 return -EINVAL; 138 } 139 140 base = 0; 141 142 while (len > 0) { 143 start = dt_mem_next_cell(dt_root_addr_cells, &prop); 144 end = start + dt_mem_next_cell(dt_root_size_cells, 145 &prop); 146 147 ret = early_init_dt_alloc_reserved_memory_arch(size, 148 align, start, end, nomap, &base); 149 if (ret == 0) { 150 pr_debug("Reserved memory: allocated memory for '%s' node: base %pa, size %ld MiB\n", 151 uname, &base, 152 (unsigned long)size / SZ_1M); 153 break; 154 } 155 len -= t_len; 156 } 157 158 } else { 159 ret = early_init_dt_alloc_reserved_memory_arch(size, align, 160 0, 0, nomap, &base); 161 if (ret == 0) 162 pr_debug("Reserved memory: allocated memory for '%s' node: base %pa, size %ld MiB\n", 163 uname, &base, (unsigned long)size / SZ_1M); 164 } 165 166 if (base == 0) { 167 pr_info("Reserved memory: failed to allocate memory for node '%s'\n", 168 uname); 169 return -ENOMEM; 170 } 171 172 *res_base = base; 173 *res_size = size; 174 175 return 0; 176 } 177 178 static const struct of_device_id __rmem_of_table_sentinel 179 __used __section(__reservedmem_of_table_end); 180 181 /** 182 * res_mem_init_node() - call region specific reserved memory init code 183 */ 184 static int __init __reserved_mem_init_node(struct reserved_mem *rmem) 185 { 186 extern const struct of_device_id __reservedmem_of_table[]; 187 const struct of_device_id *i; 188 189 for (i = __reservedmem_of_table; i < &__rmem_of_table_sentinel; i++) { 190 reservedmem_of_init_fn initfn = i->data; 191 const char *compat = i->compatible; 192 193 if (!of_flat_dt_is_compatible(rmem->fdt_node, compat)) 194 continue; 195 196 if (initfn(rmem) == 0) { 197 pr_info("Reserved memory: initialized node %s, compatible id %s\n", 198 rmem->name, compat); 199 return 0; 200 } 201 } 202 return -ENOENT; 203 } 204 205 static int __init __rmem_cmp(const void *a, const void *b) 206 { 207 const struct reserved_mem *ra = a, *rb = b; 208 209 if (ra->base < rb->base) 210 return -1; 211 212 if (ra->base > rb->base) 213 return 1; 214 215 return 0; 216 } 217 218 static void __init __rmem_check_for_overlap(void) 219 { 220 int i; 221 222 if (reserved_mem_count < 2) 223 return; 224 225 sort(reserved_mem, reserved_mem_count, sizeof(reserved_mem[0]), 226 __rmem_cmp, NULL); 227 for (i = 0; i < reserved_mem_count - 1; i++) { 228 struct reserved_mem *this, *next; 229 230 this = &reserved_mem[i]; 231 next = &reserved_mem[i + 1]; 232 if (!(this->base && next->base)) 233 continue; 234 if (this->base + this->size > next->base) { 235 phys_addr_t this_end, next_end; 236 237 this_end = this->base + this->size; 238 next_end = next->base + next->size; 239 pr_err("Reserved memory: OVERLAP DETECTED!\n%s (%pa--%pa) overlaps with %s (%pa--%pa)\n", 240 this->name, &this->base, &this_end, 241 next->name, &next->base, &next_end); 242 } 243 } 244 } 245 246 /** 247 * fdt_init_reserved_mem - allocate and init all saved reserved memory regions 248 */ 249 void __init fdt_init_reserved_mem(void) 250 { 251 int i; 252 253 /* check for overlapping reserved regions */ 254 __rmem_check_for_overlap(); 255 256 for (i = 0; i < reserved_mem_count; i++) { 257 struct reserved_mem *rmem = &reserved_mem[i]; 258 unsigned long node = rmem->fdt_node; 259 int len; 260 const __be32 *prop; 261 int err = 0; 262 263 prop = of_get_flat_dt_prop(node, "phandle", &len); 264 if (!prop) 265 prop = of_get_flat_dt_prop(node, "linux,phandle", &len); 266 if (prop) 267 rmem->phandle = of_read_number(prop, len/4); 268 269 if (rmem->size == 0) 270 err = __reserved_mem_alloc_size(node, rmem->name, 271 &rmem->base, &rmem->size); 272 if (err == 0) 273 __reserved_mem_init_node(rmem); 274 } 275 } 276 277 static inline struct reserved_mem *__find_rmem(struct device_node *node) 278 { 279 unsigned int i; 280 281 if (!node->phandle) 282 return NULL; 283 284 for (i = 0; i < reserved_mem_count; i++) 285 if (reserved_mem[i].phandle == node->phandle) 286 return &reserved_mem[i]; 287 return NULL; 288 } 289 290 /** 291 * of_reserved_mem_device_init() - assign reserved memory region to given device 292 * 293 * This function assign memory region pointed by "memory-region" device tree 294 * property to the given device. 295 */ 296 int of_reserved_mem_device_init(struct device *dev) 297 { 298 struct reserved_mem *rmem; 299 struct device_node *np; 300 int ret; 301 302 np = of_parse_phandle(dev->of_node, "memory-region", 0); 303 if (!np) 304 return -ENODEV; 305 306 rmem = __find_rmem(np); 307 of_node_put(np); 308 309 if (!rmem || !rmem->ops || !rmem->ops->device_init) 310 return -EINVAL; 311 312 ret = rmem->ops->device_init(rmem, dev); 313 if (ret == 0) 314 dev_info(dev, "assigned reserved memory node %s\n", rmem->name); 315 316 return ret; 317 } 318 EXPORT_SYMBOL_GPL(of_reserved_mem_device_init); 319 320 /** 321 * of_reserved_mem_device_release() - release reserved memory device structures 322 * 323 * This function releases structures allocated for memory region handling for 324 * the given device. 325 */ 326 void of_reserved_mem_device_release(struct device *dev) 327 { 328 struct reserved_mem *rmem; 329 struct device_node *np; 330 331 np = of_parse_phandle(dev->of_node, "memory-region", 0); 332 if (!np) 333 return; 334 335 rmem = __find_rmem(np); 336 of_node_put(np); 337 338 if (!rmem || !rmem->ops || !rmem->ops->device_release) 339 return; 340 341 rmem->ops->device_release(rmem, dev); 342 } 343 EXPORT_SYMBOL_GPL(of_reserved_mem_device_release); 344