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