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/ioport.h> 16 #include <linux/libfdt.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 #include <linux/slab.h> 25 #include <linux/memblock.h> 26 #include <linux/kmemleak.h> 27 28 #include "of_private.h" 29 30 static struct reserved_mem reserved_mem_array[MAX_RESERVED_REGIONS] __initdata; 31 static struct reserved_mem *reserved_mem __refdata = reserved_mem_array; 32 static int total_reserved_mem_cnt = MAX_RESERVED_REGIONS; 33 static int reserved_mem_count; 34 35 static int __init early_init_dt_alloc_reserved_memory_arch(phys_addr_t size, 36 phys_addr_t align, phys_addr_t start, phys_addr_t end, bool nomap, 37 phys_addr_t *res_base) 38 { 39 phys_addr_t base; 40 int err = 0; 41 42 end = !end ? MEMBLOCK_ALLOC_ANYWHERE : end; 43 align = !align ? SMP_CACHE_BYTES : align; 44 base = memblock_phys_alloc_range(size, align, start, end); 45 if (!base) 46 return -ENOMEM; 47 48 *res_base = base; 49 if (nomap) { 50 err = memblock_mark_nomap(base, size); 51 if (err) 52 memblock_phys_free(base, size); 53 } 54 55 if (!err) 56 kmemleak_ignore_phys(base); 57 58 return err; 59 } 60 61 /* 62 * alloc_reserved_mem_array() - allocate memory for the reserved_mem 63 * array using memblock 64 * 65 * This function is used to allocate memory for the reserved_mem 66 * array according to the total number of reserved memory regions 67 * defined in the DT. 68 * After the new array is allocated, the information stored in 69 * the initial static array is copied over to this new array and 70 * the new array is used from this point on. 71 */ 72 static int __init alloc_reserved_mem_array(void) 73 { 74 struct reserved_mem *new_array; 75 size_t alloc_size, copy_size, memset_size; 76 int ret; 77 78 if (!total_reserved_mem_cnt) 79 return 0; 80 81 alloc_size = array_size(total_reserved_mem_cnt, sizeof(*new_array)); 82 if (alloc_size == SIZE_MAX) { 83 ret = -EOVERFLOW; 84 goto fail; 85 } 86 87 new_array = memblock_alloc(alloc_size, SMP_CACHE_BYTES); 88 if (!new_array) { 89 ret = -ENOMEM; 90 goto fail; 91 } 92 93 copy_size = array_size(reserved_mem_count, sizeof(*new_array)); 94 if (copy_size == SIZE_MAX) { 95 memblock_free(new_array, alloc_size); 96 ret = -EOVERFLOW; 97 goto fail; 98 } 99 100 memset_size = alloc_size - copy_size; 101 102 memcpy(new_array, reserved_mem, copy_size); 103 memset(new_array + reserved_mem_count, 0, memset_size); 104 105 reserved_mem = new_array; 106 return 0; 107 108 fail: 109 pr_err("Failed to allocate memory for reserved_mem array with err: %d", ret); 110 reserved_mem_count = 0; 111 return ret; 112 } 113 114 static void fdt_init_reserved_mem_node(unsigned long node, const char *uname, 115 phys_addr_t base, phys_addr_t size); 116 static int fdt_validate_reserved_mem_node(unsigned long node, 117 phys_addr_t *align); 118 static int fdt_fixup_reserved_mem_node(unsigned long node, 119 phys_addr_t base, phys_addr_t size); 120 121 static int __init early_init_dt_reserve_memory(phys_addr_t base, 122 phys_addr_t size, bool nomap) 123 { 124 if (nomap) { 125 /* 126 * If the memory is already reserved (by another region), we 127 * should not allow it to be marked nomap, but don't worry 128 * if the region isn't memory as it won't be mapped. 129 */ 130 if (memblock_overlaps_region(&memblock.memory, base, size) && 131 memblock_is_region_reserved(base, size)) 132 return -EBUSY; 133 134 return memblock_mark_nomap(base, size); 135 } 136 return memblock_reserve(base, size); 137 } 138 139 /* 140 * __reserved_mem_reserve_reg() - reserve memory described in the 141 * first entry in 'reg' property 142 */ 143 static int __init __reserved_mem_reserve_reg(unsigned long node, 144 const char *uname) 145 { 146 phys_addr_t base, size; 147 int len, err; 148 const __be32 *prop; 149 bool nomap; 150 u64 b, s; 151 152 prop = of_flat_dt_get_addr_size_prop(node, "reg", &len); 153 if (!prop || !len) 154 return -ENOENT; 155 156 if (len > 1) 157 pr_warn("Reserved memory: node '%s' has %d <base size> entries, only the first is used\n", 158 uname, len); 159 160 nomap = of_get_flat_dt_prop(node, "no-map", NULL) != NULL; 161 162 err = fdt_validate_reserved_mem_node(node, NULL); 163 if (err && err != -ENODEV) 164 return err; 165 166 of_flat_dt_read_addr_size(prop, 0, &b, &s); 167 base = b; 168 size = s; 169 170 if (size && early_init_dt_reserve_memory(base, size, nomap) == 0) { 171 fdt_fixup_reserved_mem_node(node, base, size); 172 pr_debug("Reserved memory: reserved region for node '%s': base %pa, size %lu MiB\n", 173 uname, &base, (unsigned long)(size / SZ_1M)); 174 } else { 175 pr_err("Reserved memory: failed to reserve memory for node '%s': base %pa, size %lu MiB\n", 176 uname, &base, (unsigned long)(size / SZ_1M)); 177 } 178 return 0; 179 } 180 181 /* 182 * __reserved_mem_check_root() - check if #size-cells, #address-cells provided 183 * in /reserved-memory matches the values supported by the current implementation, 184 * also check if ranges property has been provided 185 */ 186 static int __init __reserved_mem_check_root(unsigned long node) 187 { 188 const __be32 *prop; 189 190 prop = of_get_flat_dt_prop(node, "#size-cells", NULL); 191 if (!prop || be32_to_cpup(prop) != dt_root_size_cells) 192 return -EINVAL; 193 194 prop = of_get_flat_dt_prop(node, "#address-cells", NULL); 195 if (!prop || be32_to_cpup(prop) != dt_root_addr_cells) 196 return -EINVAL; 197 198 prop = of_get_flat_dt_prop(node, "ranges", NULL); 199 if (!prop) 200 return -EINVAL; 201 return 0; 202 } 203 204 static int __init __rmem_cmp(const void *a, const void *b) 205 { 206 const struct reserved_mem *ra = a, *rb = b; 207 208 if (ra->base < rb->base) 209 return -1; 210 211 if (ra->base > rb->base) 212 return 1; 213 214 /* 215 * Put the dynamic allocations (address == 0, size == 0) before static 216 * allocations at address 0x0 so that overlap detection works 217 * correctly. 218 */ 219 if (ra->size < rb->size) 220 return -1; 221 if (ra->size > rb->size) 222 return 1; 223 224 return 0; 225 } 226 227 static void __init __rmem_check_for_overlap(void) 228 { 229 int i; 230 231 if (reserved_mem_count < 2) 232 return; 233 234 sort(reserved_mem, reserved_mem_count, sizeof(reserved_mem[0]), 235 __rmem_cmp, NULL); 236 for (i = 0; i < reserved_mem_count - 1; i++) { 237 struct reserved_mem *this, *next; 238 239 this = &reserved_mem[i]; 240 next = &reserved_mem[i + 1]; 241 242 if (this->base + this->size > next->base) { 243 phys_addr_t this_end, next_end; 244 245 this_end = this->base + this->size; 246 next_end = next->base + next->size; 247 pr_err("OVERLAP DETECTED!\n%s (%pa--%pa) overlaps with %s (%pa--%pa)\n", 248 this->name, &this->base, &this_end, 249 next->name, &next->base, &next_end); 250 } 251 } 252 } 253 254 /** 255 * fdt_scan_reserved_mem_late() - Scan FDT and initialize remaining reserved 256 * memory regions. 257 * 258 * This function is used to scan again through the DT and initialize the 259 * "static" reserved memory regions, that are defined using the "reg" 260 * property. Each such region is then initialized with its specific init 261 * function and stored in the global reserved_mem array. 262 */ 263 void __init fdt_scan_reserved_mem_late(void) 264 { 265 const void *fdt = initial_boot_params; 266 phys_addr_t base, size; 267 int node, child; 268 269 if (!fdt) 270 return; 271 272 node = fdt_path_offset(fdt, "/reserved-memory"); 273 if (node < 0) { 274 pr_info("Reserved memory: No reserved-memory node in the DT\n"); 275 return; 276 } 277 278 /* Attempt dynamic allocation of a new reserved_mem array */ 279 if (alloc_reserved_mem_array()) 280 return; 281 282 if (__reserved_mem_check_root(node)) { 283 pr_err("Reserved memory: unsupported node format, ignoring\n"); 284 return; 285 } 286 287 fdt_for_each_subnode(child, fdt, node) { 288 const __be32 *prop; 289 const char *uname; 290 u64 b, s; 291 int ret; 292 int len; 293 294 if (!of_fdt_device_is_available(fdt, child)) 295 continue; 296 297 prop = of_flat_dt_get_addr_size_prop(child, "reg", &len); 298 if (!prop || !len) 299 continue; 300 301 ret = fdt_validate_reserved_mem_node(child, NULL); 302 if (ret && ret != -ENODEV) 303 continue; 304 305 of_flat_dt_read_addr_size(prop, 0, &b, &s); 306 base = b; 307 size = s; 308 309 if (size) { 310 uname = fdt_get_name(fdt, child, NULL); 311 fdt_init_reserved_mem_node(child, uname, base, size); 312 } 313 } 314 315 /* check for overlapping reserved regions */ 316 __rmem_check_for_overlap(); 317 } 318 319 static int __init __reserved_mem_alloc_size(unsigned long node, const char *uname); 320 321 /* 322 * fdt_scan_reserved_mem() - reserve and allocate memory occupied by 323 * reserved memory regions. 324 * 325 * This function is used to scan through the FDT and mark memory occupied 326 * by all static (defined by the "reg" property) reserved memory regions. 327 * Then memory for all dynamic regions (defined by size & alignment) is 328 * allocated, a region specific init function is called and region information 329 * is stored in the reserved_mem array. 330 */ 331 int __init fdt_scan_reserved_mem(void) 332 { 333 int node, child; 334 int dynamic_nodes_cnt = 0, count = 0; 335 int dynamic_nodes[MAX_RESERVED_REGIONS]; 336 const void *fdt = initial_boot_params; 337 338 node = fdt_path_offset(fdt, "/reserved-memory"); 339 if (node < 0) { 340 total_reserved_mem_cnt = 0; 341 return -ENODEV; 342 } 343 344 if (__reserved_mem_check_root(node) != 0) { 345 pr_err("Reserved memory: unsupported node format, ignoring\n"); 346 total_reserved_mem_cnt = 0; 347 return -EINVAL; 348 } 349 350 fdt_for_each_subnode(child, fdt, node) { 351 const char *uname; 352 int err; 353 354 if (!of_fdt_device_is_available(fdt, child)) 355 continue; 356 357 uname = fdt_get_name(fdt, child, NULL); 358 359 err = __reserved_mem_reserve_reg(child, uname); 360 if (!err) 361 count++; 362 /* 363 * Save the nodes for the dynamically-placed regions 364 * into an array which will be used for allocation right 365 * after all the statically-placed regions are reserved 366 * or marked as no-map. This is done to avoid dynamically 367 * allocating from one of the statically-placed regions. 368 */ 369 if (err == -ENOENT && of_get_flat_dt_prop(child, "size", NULL)) { 370 dynamic_nodes[dynamic_nodes_cnt] = child; 371 dynamic_nodes_cnt++; 372 } 373 } 374 for (int i = 0; i < dynamic_nodes_cnt; i++) { 375 const char *uname; 376 int err; 377 378 child = dynamic_nodes[i]; 379 uname = fdt_get_name(fdt, child, NULL); 380 err = __reserved_mem_alloc_size(child, uname); 381 if (!err) 382 count++; 383 } 384 total_reserved_mem_cnt = count; 385 return 0; 386 } 387 388 /* 389 * __reserved_mem_alloc_in_range() - allocate reserved memory described with 390 * 'alloc-ranges'. Choose bottom-up/top-down depending on nearby existing 391 * reserved regions to keep the reserved memory contiguous if possible. 392 */ 393 static int __init __reserved_mem_alloc_in_range(phys_addr_t size, 394 phys_addr_t align, phys_addr_t start, phys_addr_t end, bool nomap, 395 phys_addr_t *res_base) 396 { 397 bool prev_bottom_up = memblock_bottom_up(); 398 bool bottom_up = false, top_down = false; 399 int ret, i; 400 401 for (i = 0; i < reserved_mem_count; i++) { 402 struct reserved_mem *rmem = &reserved_mem[i]; 403 404 /* Skip regions that were not reserved yet */ 405 if (rmem->size == 0) 406 continue; 407 408 /* 409 * If range starts next to an existing reservation, use bottom-up: 410 * |....RRRR................RRRRRRRR..............| 411 * --RRRR------ 412 */ 413 if (start >= rmem->base && start <= (rmem->base + rmem->size)) 414 bottom_up = true; 415 416 /* 417 * If range ends next to an existing reservation, use top-down: 418 * |....RRRR................RRRRRRRR..............| 419 * -------RRRR----- 420 */ 421 if (end >= rmem->base && end <= (rmem->base + rmem->size)) 422 top_down = true; 423 } 424 425 /* Change setting only if either bottom-up or top-down was selected */ 426 if (bottom_up != top_down) 427 memblock_set_bottom_up(bottom_up); 428 429 ret = early_init_dt_alloc_reserved_memory_arch(size, align, 430 start, end, nomap, res_base); 431 432 /* Restore old setting if needed */ 433 if (bottom_up != top_down) 434 memblock_set_bottom_up(prev_bottom_up); 435 436 return ret; 437 } 438 439 /* 440 * __reserved_mem_alloc_size() - allocate reserved memory described by 441 * 'size', 'alignment' and 'alloc-ranges' properties. 442 */ 443 static int __init __reserved_mem_alloc_size(unsigned long node, const char *uname) 444 { 445 phys_addr_t start = 0, end = 0; 446 phys_addr_t base = 0, align = 0, size; 447 int i, len; 448 const __be32 *prop; 449 bool nomap; 450 int ret; 451 452 prop = of_get_flat_dt_prop(node, "size", &len); 453 if (!prop) 454 return -EINVAL; 455 456 if (len != dt_root_size_cells * sizeof(__be32)) { 457 pr_err("invalid size property in '%s' node.\n", uname); 458 return -EINVAL; 459 } 460 size = dt_mem_next_cell(dt_root_size_cells, &prop); 461 462 prop = of_get_flat_dt_prop(node, "alignment", &len); 463 if (prop) { 464 if (len != dt_root_addr_cells * sizeof(__be32)) { 465 pr_err("invalid alignment property in '%s' node.\n", 466 uname); 467 return -EINVAL; 468 } 469 align = dt_mem_next_cell(dt_root_addr_cells, &prop); 470 } 471 472 nomap = of_get_flat_dt_prop(node, "no-map", NULL) != NULL; 473 474 ret = fdt_validate_reserved_mem_node(node, &align); 475 if (ret && ret != -ENODEV) 476 return ret; 477 478 prop = of_flat_dt_get_addr_size_prop(node, "alloc-ranges", &len); 479 if (prop) { 480 for (i = 0; i < len; i++) { 481 u64 b, s; 482 483 of_flat_dt_read_addr_size(prop, i, &b, &s); 484 485 start = b; 486 end = b + s; 487 488 base = 0; 489 ret = __reserved_mem_alloc_in_range(size, align, 490 start, end, nomap, &base); 491 if (ret == 0) { 492 pr_debug("allocated memory for '%s' node: base %pa, size %lu MiB\n", 493 uname, &base, 494 (unsigned long)(size / SZ_1M)); 495 break; 496 } 497 } 498 } else { 499 ret = early_init_dt_alloc_reserved_memory_arch(size, align, 500 0, 0, nomap, &base); 501 if (ret == 0) 502 pr_debug("allocated memory for '%s' node: base %pa, size %lu MiB\n", 503 uname, &base, (unsigned long)(size / SZ_1M)); 504 } 505 506 if (base == 0) { 507 pr_err("failed to allocate memory for node '%s': size %lu MiB\n", 508 uname, (unsigned long)(size / SZ_1M)); 509 return -ENOMEM; 510 } 511 512 fdt_fixup_reserved_mem_node(node, base, size); 513 fdt_init_reserved_mem_node(node, uname, base, size); 514 515 return 0; 516 } 517 518 extern const struct of_device_id __reservedmem_of_table[]; 519 static const struct of_device_id __rmem_of_table_sentinel 520 __used __section("__reservedmem_of_table_end"); 521 522 /** 523 * fdt_fixup_reserved_mem_node() - call fixup function for a reserved memory node 524 * @node: FDT node to fixup 525 * @base: base address of the reserved memory region 526 * @size: size of the reserved memory region 527 * 528 * This function iterates through the reserved memory drivers and calls 529 * the node_fixup callback for the compatible entry matching the node. 530 * 531 * Return: 0 on success, -ENODEV if no compatible match found 532 */ 533 static int __init fdt_fixup_reserved_mem_node(unsigned long node, 534 phys_addr_t base, phys_addr_t size) 535 { 536 const struct of_device_id *i; 537 int ret = -ENODEV; 538 539 for (i = __reservedmem_of_table; ret == -ENODEV && 540 i < &__rmem_of_table_sentinel; i++) { 541 const struct reserved_mem_ops *ops = i->data; 542 543 if (!of_flat_dt_is_compatible(node, i->compatible)) 544 continue; 545 546 if (ops->node_fixup) 547 ret = ops->node_fixup(node, base, size); 548 } 549 return ret; 550 } 551 552 /** 553 * fdt_validate_reserved_mem_node() - validate a reserved memory node 554 * @node: FDT node to validate 555 * @align: pointer to store the validated alignment (may be modified by callback) 556 * 557 * This function iterates through the reserved memory drivers and calls 558 * the node_validate callback for the compatible entry matching the node. 559 * 560 * Return: 0 on success, -ENODEV if no compatible match found 561 */ 562 static int __init fdt_validate_reserved_mem_node(unsigned long node, phys_addr_t *align) 563 { 564 const struct of_device_id *i; 565 int ret = -ENODEV; 566 567 for (i = __reservedmem_of_table; ret == -ENODEV && 568 i < &__rmem_of_table_sentinel; i++) { 569 const struct reserved_mem_ops *ops = i->data; 570 571 if (!of_flat_dt_is_compatible(node, i->compatible)) 572 continue; 573 574 if (ops->node_validate) 575 ret = ops->node_validate(node, align); 576 } 577 return ret; 578 } 579 580 /** 581 * __reserved_mem_init_node() - initialize a reserved memory region 582 * @rmem: reserved_mem structure to initialize 583 * @node: FDT node describing the reserved memory region 584 * 585 * This function iterates through the reserved memory drivers and calls the 586 * node_init callback for the compatible entry matching the node. On success, 587 * the operations pointer is stored in the reserved_mem structure. 588 * 589 * Return: 0 on success, -ENODEV if no compatible match found 590 */ 591 static int __init __reserved_mem_init_node(struct reserved_mem *rmem, 592 unsigned long node) 593 { 594 const struct of_device_id *i; 595 int ret = -ENODEV; 596 597 for (i = __reservedmem_of_table; ret == -ENODEV && 598 i < &__rmem_of_table_sentinel; i++) { 599 const struct reserved_mem_ops *ops = i->data; 600 const char *compat = i->compatible; 601 602 if (!of_flat_dt_is_compatible(node, compat)) 603 continue; 604 605 ret = ops->node_init(node, rmem); 606 if (ret == 0) { 607 rmem->ops = ops; 608 pr_info("initialized node %s, compatible id %s\n", 609 rmem->name, compat); 610 return ret; 611 } 612 } 613 return ret; 614 } 615 616 /** 617 * fdt_init_reserved_mem_node() - Initialize a reserved memory region 618 * @node: fdt node of the initialized region 619 * @uname: name of the reserved memory node 620 * @base: base address of the reserved memory region 621 * @size: size of the reserved memory region 622 * 623 * This function calls the region-specific initialization function for a 624 * reserved memory region and saves all region-specific data to the 625 * reserved_mem array to allow of_reserved_mem_lookup() to find it. 626 */ 627 static void __init fdt_init_reserved_mem_node(unsigned long node, const char *uname, 628 phys_addr_t base, phys_addr_t size) 629 { 630 int err = 0; 631 bool nomap; 632 633 struct reserved_mem *rmem = &reserved_mem[reserved_mem_count]; 634 635 if (reserved_mem_count == total_reserved_mem_cnt) { 636 pr_err("not enough space for all defined regions.\n"); 637 return; 638 } 639 640 rmem->name = uname; 641 rmem->base = base; 642 rmem->size = size; 643 644 nomap = of_get_flat_dt_prop(node, "no-map", NULL) != NULL; 645 646 err = __reserved_mem_init_node(rmem, node); 647 if (err != 0 && err != -ENODEV) { 648 pr_info("node %s compatible matching fail\n", rmem->name); 649 rmem->name = NULL; 650 651 if (nomap) 652 memblock_clear_nomap(rmem->base, rmem->size); 653 else 654 memblock_phys_free(rmem->base, rmem->size); 655 return; 656 } else { 657 phys_addr_t end = rmem->base + rmem->size - 1; 658 bool reusable = 659 (of_get_flat_dt_prop(node, "reusable", NULL)) != NULL; 660 661 pr_info("%pa..%pa (%lu KiB) %s %s %s\n", 662 &rmem->base, &end, (unsigned long)(rmem->size / SZ_1K), 663 nomap ? "nomap" : "map", 664 reusable ? "reusable" : "non-reusable", 665 rmem->name ? rmem->name : "unknown"); 666 } 667 668 reserved_mem_count++; 669 } 670 671 struct rmem_assigned_device { 672 struct device *dev; 673 struct reserved_mem *rmem; 674 struct list_head list; 675 }; 676 677 static LIST_HEAD(of_rmem_assigned_device_list); 678 static DEFINE_MUTEX(of_rmem_assigned_device_mutex); 679 680 /** 681 * of_reserved_mem_device_init_by_idx() - assign reserved memory region to 682 * given device 683 * @dev: Pointer to the device to configure 684 * @np: Pointer to the device_node with 'reserved-memory' property 685 * @idx: Index of selected region 686 * 687 * This function assigns respective DMA-mapping operations based on reserved 688 * memory region specified by 'memory-region' property in @np node to the @dev 689 * device. When driver needs to use more than one reserved memory region, it 690 * should allocate child devices and initialize regions by name for each of 691 * child device. 692 * 693 * Returns error code or zero on success. 694 */ 695 int of_reserved_mem_device_init_by_idx(struct device *dev, 696 struct device_node *np, int idx) 697 { 698 struct rmem_assigned_device *rd; 699 struct device_node *target; 700 struct reserved_mem *rmem; 701 int ret; 702 703 if (!np || !dev) 704 return -EINVAL; 705 706 target = of_parse_phandle(np, "memory-region", idx); 707 if (!target) 708 return -ENODEV; 709 710 if (!of_device_is_available(target)) { 711 of_node_put(target); 712 return 0; 713 } 714 715 rmem = of_reserved_mem_lookup(target); 716 of_node_put(target); 717 718 if (!rmem || !rmem->ops || !rmem->ops->device_init) 719 return -EINVAL; 720 721 rd = kmalloc_obj(struct rmem_assigned_device); 722 if (!rd) 723 return -ENOMEM; 724 725 ret = rmem->ops->device_init(rmem, dev); 726 if (ret == 0) { 727 rd->dev = dev; 728 rd->rmem = rmem; 729 730 mutex_lock(&of_rmem_assigned_device_mutex); 731 list_add(&rd->list, &of_rmem_assigned_device_list); 732 mutex_unlock(&of_rmem_assigned_device_mutex); 733 734 dev_info(dev, "assigned reserved memory node %s\n", rmem->name); 735 } else { 736 kfree(rd); 737 } 738 739 return ret; 740 } 741 EXPORT_SYMBOL_GPL(of_reserved_mem_device_init_by_idx); 742 743 /** 744 * of_reserved_mem_device_init_by_name() - assign named reserved memory region 745 * to given device 746 * @dev: pointer to the device to configure 747 * @np: pointer to the device node with 'memory-region' property 748 * @name: name of the selected memory region 749 * 750 * Returns: 0 on success or a negative error-code on failure. 751 */ 752 int of_reserved_mem_device_init_by_name(struct device *dev, 753 struct device_node *np, 754 const char *name) 755 { 756 int idx = of_property_match_string(np, "memory-region-names", name); 757 758 return of_reserved_mem_device_init_by_idx(dev, np, idx); 759 } 760 EXPORT_SYMBOL_GPL(of_reserved_mem_device_init_by_name); 761 762 /** 763 * of_reserved_mem_device_release() - release reserved memory device structures 764 * @dev: Pointer to the device to deconfigure 765 * 766 * This function releases structures allocated for memory region handling for 767 * the given device. 768 */ 769 void of_reserved_mem_device_release(struct device *dev) 770 { 771 struct rmem_assigned_device *rd, *tmp; 772 LIST_HEAD(release_list); 773 774 mutex_lock(&of_rmem_assigned_device_mutex); 775 list_for_each_entry_safe(rd, tmp, &of_rmem_assigned_device_list, list) { 776 if (rd->dev == dev) 777 list_move_tail(&rd->list, &release_list); 778 } 779 mutex_unlock(&of_rmem_assigned_device_mutex); 780 781 list_for_each_entry_safe(rd, tmp, &release_list, list) { 782 if (rd->rmem && rd->rmem->ops && rd->rmem->ops->device_release) 783 rd->rmem->ops->device_release(rd->rmem, dev); 784 785 kfree(rd); 786 } 787 } 788 EXPORT_SYMBOL_GPL(of_reserved_mem_device_release); 789 790 /** 791 * of_reserved_mem_lookup() - acquire reserved_mem from a device node 792 * @np: node pointer of the desired reserved-memory region 793 * 794 * This function allows drivers to acquire a reference to the reserved_mem 795 * struct based on a device node handle. 796 * 797 * Returns a reserved_mem reference, or NULL on error. 798 */ 799 struct reserved_mem *of_reserved_mem_lookup(struct device_node *np) 800 { 801 const char *name; 802 int i; 803 804 if (!np->full_name) 805 return NULL; 806 807 name = kbasename(np->full_name); 808 for (i = 0; i < reserved_mem_count; i++) 809 if (reserved_mem[i].name && 810 !strcmp(reserved_mem[i].name, name)) 811 return &reserved_mem[i]; 812 813 return NULL; 814 } 815 EXPORT_SYMBOL_GPL(of_reserved_mem_lookup); 816 817 /** 818 * of_reserved_mem_region_to_resource() - Get a reserved memory region as a resource 819 * @np: node containing 'memory-region' property 820 * @idx: index of 'memory-region' property to lookup 821 * @res: Pointer to a struct resource to fill in with reserved region 822 * 823 * This function allows drivers to lookup a node's 'memory-region' property 824 * entries by index and return a struct resource for the entry. 825 * 826 * Returns 0 on success with @res filled in. Returns -ENODEV if 'memory-region' 827 * is missing or unavailable, -EINVAL for any other error. 828 */ 829 int of_reserved_mem_region_to_resource(const struct device_node *np, 830 unsigned int idx, struct resource *res) 831 { 832 struct reserved_mem *rmem; 833 834 if (!np) 835 return -EINVAL; 836 837 struct device_node *target __free(device_node) = of_parse_phandle(np, "memory-region", idx); 838 if (!target || !of_device_is_available(target)) 839 return -ENODEV; 840 841 rmem = of_reserved_mem_lookup(target); 842 if (!rmem) 843 return -EINVAL; 844 845 resource_set_range(res, rmem->base, rmem->size); 846 res->flags = IORESOURCE_MEM; 847 res->name = rmem->name; 848 return 0; 849 } 850 EXPORT_SYMBOL_GPL(of_reserved_mem_region_to_resource); 851 852 /** 853 * of_reserved_mem_region_to_resource_byname() - Get a reserved memory region as a resource 854 * @np: node containing 'memory-region' property 855 * @name: name of 'memory-region' property entry to lookup 856 * @res: Pointer to a struct resource to fill in with reserved region 857 * 858 * This function allows drivers to lookup a node's 'memory-region' property 859 * entries by name and return a struct resource for the entry. 860 * 861 * Returns 0 on success with @res filled in, or a negative error-code on 862 * failure. 863 */ 864 int of_reserved_mem_region_to_resource_byname(const struct device_node *np, 865 const char *name, 866 struct resource *res) 867 { 868 int idx; 869 870 if (!name) 871 return -EINVAL; 872 873 idx = of_property_match_string(np, "memory-region-names", name); 874 if (idx < 0) 875 return idx; 876 877 return of_reserved_mem_region_to_resource(np, idx, res); 878 } 879 EXPORT_SYMBOL_GPL(of_reserved_mem_region_to_resource_byname); 880 881 /** 882 * of_reserved_mem_region_count() - Return the number of 'memory-region' entries 883 * @np: node containing 'memory-region' property 884 * 885 * This function allows drivers to retrieve the number of entries for a node's 886 * 'memory-region' property. 887 * 888 * Returns the number of entries on success, or negative error code on a 889 * malformed property. 890 */ 891 int of_reserved_mem_region_count(const struct device_node *np) 892 { 893 return of_count_phandle_with_args(np, "memory-region", NULL); 894 } 895 EXPORT_SYMBOL_GPL(of_reserved_mem_region_count); 896