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