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