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