1 // SPDX-License-Identifier: GPL-2.0+ 2 /* 3 * Device tree based initialization code for reserved memory. 4 * 5 * Copyright (c) 2013, 2015 The Linux Foundation. All Rights Reserved. 6 * Copyright (c) 2013,2014 Samsung Electronics Co., Ltd. 7 * http://www.samsung.com 8 * Author: Marek Szyprowski <m.szyprowski@samsung.com> 9 * Author: Josh Cartwright <joshc@codeaurora.org> 10 */ 11 12 #define pr_fmt(fmt) "OF: reserved mem: " fmt 13 14 #include <linux/err.h> 15 #include <linux/of.h> 16 #include <linux/of_fdt.h> 17 #include <linux/of_platform.h> 18 #include <linux/mm.h> 19 #include <linux/sizes.h> 20 #include <linux/of_reserved_mem.h> 21 #include <linux/sort.h> 22 #include <linux/slab.h> 23 #include <linux/memblock.h> 24 #include <linux/kmemleak.h> 25 #include <linux/cma.h> 26 27 #include "of_private.h" 28 29 #define MAX_RESERVED_REGIONS 64 30 static struct reserved_mem reserved_mem[MAX_RESERVED_REGIONS]; 31 static int reserved_mem_count; 32 33 static int __init early_init_dt_alloc_reserved_memory_arch(phys_addr_t size, 34 phys_addr_t align, phys_addr_t start, phys_addr_t end, bool nomap, 35 phys_addr_t *res_base) 36 { 37 phys_addr_t base; 38 int err = 0; 39 40 end = !end ? MEMBLOCK_ALLOC_ANYWHERE : end; 41 align = !align ? SMP_CACHE_BYTES : align; 42 base = memblock_phys_alloc_range(size, align, start, end); 43 if (!base) 44 return -ENOMEM; 45 46 *res_base = base; 47 if (nomap) { 48 err = memblock_mark_nomap(base, size); 49 if (err) 50 memblock_phys_free(base, size); 51 kmemleak_ignore_phys(base); 52 } 53 54 return err; 55 } 56 57 /* 58 * fdt_reserved_mem_save_node() - save fdt node for second pass initialization 59 */ 60 void __init fdt_reserved_mem_save_node(unsigned long node, const char *uname, 61 phys_addr_t base, phys_addr_t size) 62 { 63 struct reserved_mem *rmem = &reserved_mem[reserved_mem_count]; 64 65 if (reserved_mem_count == ARRAY_SIZE(reserved_mem)) { 66 pr_err("not enough space for all defined regions.\n"); 67 return; 68 } 69 70 rmem->fdt_node = node; 71 rmem->name = uname; 72 rmem->base = base; 73 rmem->size = size; 74 75 reserved_mem_count++; 76 return; 77 } 78 79 /* 80 * __reserved_mem_alloc_size() - allocate reserved memory described by 81 * 'size', 'alignment' and 'alloc-ranges' properties. 82 */ 83 static int __init __reserved_mem_alloc_size(unsigned long node, 84 const char *uname, phys_addr_t *res_base, phys_addr_t *res_size) 85 { 86 int t_len = (dt_root_addr_cells + dt_root_size_cells) * sizeof(__be32); 87 phys_addr_t start = 0, end = 0; 88 phys_addr_t base = 0, align = 0, size; 89 int len; 90 const __be32 *prop; 91 bool nomap; 92 int ret; 93 94 prop = of_get_flat_dt_prop(node, "size", &len); 95 if (!prop) 96 return -EINVAL; 97 98 if (len != dt_root_size_cells * sizeof(__be32)) { 99 pr_err("invalid size property in '%s' node.\n", uname); 100 return -EINVAL; 101 } 102 size = dt_mem_next_cell(dt_root_size_cells, &prop); 103 104 prop = of_get_flat_dt_prop(node, "alignment", &len); 105 if (prop) { 106 if (len != dt_root_addr_cells * sizeof(__be32)) { 107 pr_err("invalid alignment property in '%s' node.\n", 108 uname); 109 return -EINVAL; 110 } 111 align = dt_mem_next_cell(dt_root_addr_cells, &prop); 112 } 113 114 nomap = of_get_flat_dt_prop(node, "no-map", NULL) != NULL; 115 116 /* Need adjust the alignment to satisfy the CMA requirement */ 117 if (IS_ENABLED(CONFIG_CMA) 118 && of_flat_dt_is_compatible(node, "shared-dma-pool") 119 && of_get_flat_dt_prop(node, "reusable", NULL) 120 && !nomap) 121 align = max_t(phys_addr_t, align, CMA_MIN_ALIGNMENT_BYTES); 122 123 prop = of_get_flat_dt_prop(node, "alloc-ranges", &len); 124 if (prop) { 125 126 if (len % t_len != 0) { 127 pr_err("invalid alloc-ranges property in '%s', skipping node.\n", 128 uname); 129 return -EINVAL; 130 } 131 132 base = 0; 133 134 while (len > 0) { 135 start = dt_mem_next_cell(dt_root_addr_cells, &prop); 136 end = start + dt_mem_next_cell(dt_root_size_cells, 137 &prop); 138 139 ret = early_init_dt_alloc_reserved_memory_arch(size, 140 align, start, end, nomap, &base); 141 if (ret == 0) { 142 pr_debug("allocated memory for '%s' node: base %pa, size %lu MiB\n", 143 uname, &base, 144 (unsigned long)(size / SZ_1M)); 145 break; 146 } 147 len -= t_len; 148 } 149 150 } else { 151 ret = early_init_dt_alloc_reserved_memory_arch(size, align, 152 0, 0, nomap, &base); 153 if (ret == 0) 154 pr_debug("allocated memory for '%s' node: base %pa, size %lu MiB\n", 155 uname, &base, (unsigned long)(size / SZ_1M)); 156 } 157 158 if (base == 0) { 159 pr_err("failed to allocate memory for node '%s': size %lu MiB\n", 160 uname, (unsigned long)(size / SZ_1M)); 161 return -ENOMEM; 162 } 163 164 *res_base = base; 165 *res_size = size; 166 167 return 0; 168 } 169 170 static const struct of_device_id __rmem_of_table_sentinel 171 __used __section("__reservedmem_of_table_end"); 172 173 /* 174 * __reserved_mem_init_node() - call region specific reserved memory init code 175 */ 176 static int __init __reserved_mem_init_node(struct reserved_mem *rmem) 177 { 178 extern const struct of_device_id __reservedmem_of_table[]; 179 const struct of_device_id *i; 180 int ret = -ENOENT; 181 182 for (i = __reservedmem_of_table; i < &__rmem_of_table_sentinel; i++) { 183 reservedmem_of_init_fn initfn = i->data; 184 const char *compat = i->compatible; 185 186 if (!of_flat_dt_is_compatible(rmem->fdt_node, compat)) 187 continue; 188 189 ret = initfn(rmem); 190 if (ret == 0) { 191 pr_info("initialized node %s, compatible id %s\n", 192 rmem->name, compat); 193 break; 194 } 195 } 196 return ret; 197 } 198 199 static int __init __rmem_cmp(const void *a, const void *b) 200 { 201 const struct reserved_mem *ra = a, *rb = b; 202 203 if (ra->base < rb->base) 204 return -1; 205 206 if (ra->base > rb->base) 207 return 1; 208 209 /* 210 * Put the dynamic allocations (address == 0, size == 0) before static 211 * allocations at address 0x0 so that overlap detection works 212 * correctly. 213 */ 214 if (ra->size < rb->size) 215 return -1; 216 if (ra->size > rb->size) 217 return 1; 218 219 return 0; 220 } 221 222 static void __init __rmem_check_for_overlap(void) 223 { 224 int i; 225 226 if (reserved_mem_count < 2) 227 return; 228 229 sort(reserved_mem, reserved_mem_count, sizeof(reserved_mem[0]), 230 __rmem_cmp, NULL); 231 for (i = 0; i < reserved_mem_count - 1; i++) { 232 struct reserved_mem *this, *next; 233 234 this = &reserved_mem[i]; 235 next = &reserved_mem[i + 1]; 236 237 if (this->base + this->size > next->base) { 238 phys_addr_t this_end, next_end; 239 240 this_end = this->base + this->size; 241 next_end = next->base + next->size; 242 pr_err("OVERLAP DETECTED!\n%s (%pa--%pa) overlaps with %s (%pa--%pa)\n", 243 this->name, &this->base, &this_end, 244 next->name, &next->base, &next_end); 245 } 246 } 247 } 248 249 /** 250 * fdt_init_reserved_mem() - allocate and init all saved reserved memory regions 251 */ 252 void __init fdt_init_reserved_mem(void) 253 { 254 int i; 255 256 /* check for overlapping reserved regions */ 257 __rmem_check_for_overlap(); 258 259 for (i = 0; i < reserved_mem_count; i++) { 260 struct reserved_mem *rmem = &reserved_mem[i]; 261 unsigned long node = rmem->fdt_node; 262 int len; 263 const __be32 *prop; 264 int err = 0; 265 bool nomap; 266 267 nomap = of_get_flat_dt_prop(node, "no-map", NULL) != NULL; 268 prop = of_get_flat_dt_prop(node, "phandle", &len); 269 if (!prop) 270 prop = of_get_flat_dt_prop(node, "linux,phandle", &len); 271 if (prop) 272 rmem->phandle = of_read_number(prop, len/4); 273 274 if (rmem->size == 0) 275 err = __reserved_mem_alloc_size(node, rmem->name, 276 &rmem->base, &rmem->size); 277 if (err == 0) { 278 err = __reserved_mem_init_node(rmem); 279 if (err != 0 && err != -ENOENT) { 280 pr_info("node %s compatible matching fail\n", 281 rmem->name); 282 if (nomap) 283 memblock_clear_nomap(rmem->base, rmem->size); 284 else 285 memblock_phys_free(rmem->base, 286 rmem->size); 287 } 288 } 289 } 290 } 291 292 static inline struct reserved_mem *__find_rmem(struct device_node *node) 293 { 294 unsigned int i; 295 296 if (!node->phandle) 297 return NULL; 298 299 for (i = 0; i < reserved_mem_count; i++) 300 if (reserved_mem[i].phandle == node->phandle) 301 return &reserved_mem[i]; 302 return NULL; 303 } 304 305 struct rmem_assigned_device { 306 struct device *dev; 307 struct reserved_mem *rmem; 308 struct list_head list; 309 }; 310 311 static LIST_HEAD(of_rmem_assigned_device_list); 312 static DEFINE_MUTEX(of_rmem_assigned_device_mutex); 313 314 /** 315 * of_reserved_mem_device_init_by_idx() - assign reserved memory region to 316 * given device 317 * @dev: Pointer to the device to configure 318 * @np: Pointer to the device_node with 'reserved-memory' property 319 * @idx: Index of selected region 320 * 321 * This function assigns respective DMA-mapping operations based on reserved 322 * memory region specified by 'memory-region' property in @np node to the @dev 323 * device. When driver needs to use more than one reserved memory region, it 324 * should allocate child devices and initialize regions by name for each of 325 * child device. 326 * 327 * Returns error code or zero on success. 328 */ 329 int of_reserved_mem_device_init_by_idx(struct device *dev, 330 struct device_node *np, int idx) 331 { 332 struct rmem_assigned_device *rd; 333 struct device_node *target; 334 struct reserved_mem *rmem; 335 int ret; 336 337 if (!np || !dev) 338 return -EINVAL; 339 340 target = of_parse_phandle(np, "memory-region", idx); 341 if (!target) 342 return -ENODEV; 343 344 if (!of_device_is_available(target)) { 345 of_node_put(target); 346 return 0; 347 } 348 349 rmem = __find_rmem(target); 350 of_node_put(target); 351 352 if (!rmem || !rmem->ops || !rmem->ops->device_init) 353 return -EINVAL; 354 355 rd = kmalloc(sizeof(struct rmem_assigned_device), GFP_KERNEL); 356 if (!rd) 357 return -ENOMEM; 358 359 ret = rmem->ops->device_init(rmem, dev); 360 if (ret == 0) { 361 rd->dev = dev; 362 rd->rmem = rmem; 363 364 mutex_lock(&of_rmem_assigned_device_mutex); 365 list_add(&rd->list, &of_rmem_assigned_device_list); 366 mutex_unlock(&of_rmem_assigned_device_mutex); 367 368 dev_info(dev, "assigned reserved memory node %s\n", rmem->name); 369 } else { 370 kfree(rd); 371 } 372 373 return ret; 374 } 375 EXPORT_SYMBOL_GPL(of_reserved_mem_device_init_by_idx); 376 377 /** 378 * of_reserved_mem_device_init_by_name() - assign named reserved memory region 379 * to given device 380 * @dev: pointer to the device to configure 381 * @np: pointer to the device node with 'memory-region' property 382 * @name: name of the selected memory region 383 * 384 * Returns: 0 on success or a negative error-code on failure. 385 */ 386 int of_reserved_mem_device_init_by_name(struct device *dev, 387 struct device_node *np, 388 const char *name) 389 { 390 int idx = of_property_match_string(np, "memory-region-names", name); 391 392 return of_reserved_mem_device_init_by_idx(dev, np, idx); 393 } 394 EXPORT_SYMBOL_GPL(of_reserved_mem_device_init_by_name); 395 396 /** 397 * of_reserved_mem_device_release() - release reserved memory device structures 398 * @dev: Pointer to the device to deconfigure 399 * 400 * This function releases structures allocated for memory region handling for 401 * the given device. 402 */ 403 void of_reserved_mem_device_release(struct device *dev) 404 { 405 struct rmem_assigned_device *rd, *tmp; 406 LIST_HEAD(release_list); 407 408 mutex_lock(&of_rmem_assigned_device_mutex); 409 list_for_each_entry_safe(rd, tmp, &of_rmem_assigned_device_list, list) { 410 if (rd->dev == dev) 411 list_move_tail(&rd->list, &release_list); 412 } 413 mutex_unlock(&of_rmem_assigned_device_mutex); 414 415 list_for_each_entry_safe(rd, tmp, &release_list, list) { 416 if (rd->rmem && rd->rmem->ops && rd->rmem->ops->device_release) 417 rd->rmem->ops->device_release(rd->rmem, dev); 418 419 kfree(rd); 420 } 421 } 422 EXPORT_SYMBOL_GPL(of_reserved_mem_device_release); 423 424 /** 425 * of_reserved_mem_lookup() - acquire reserved_mem from a device node 426 * @np: node pointer of the desired reserved-memory region 427 * 428 * This function allows drivers to acquire a reference to the reserved_mem 429 * struct based on a device node handle. 430 * 431 * Returns a reserved_mem reference, or NULL on error. 432 */ 433 struct reserved_mem *of_reserved_mem_lookup(struct device_node *np) 434 { 435 const char *name; 436 int i; 437 438 if (!np->full_name) 439 return NULL; 440 441 name = kbasename(np->full_name); 442 for (i = 0; i < reserved_mem_count; i++) 443 if (!strcmp(reserved_mem[i].name, name)) 444 return &reserved_mem[i]; 445 446 return NULL; 447 } 448 EXPORT_SYMBOL_GPL(of_reserved_mem_lookup); 449