blob: 9c0fb962c22b097ce185c6f1b3d946e962a93d10 [file] [log] [blame]
Rob Herringaf6074f2017-12-27 12:55:14 -06001// SPDX-License-Identifier: GPL-2.0+
Marek Szyprowski3f0c8202014-02-28 14:42:48 +01002/*
3 * Device tree based initialization code for reserved memory.
4 *
Mitchel Humpherysae1add22015-09-15 18:30:36 -07005 * Copyright (c) 2013, 2015 The Linux Foundation. All Rights Reserved.
Marek Szyprowski3f0c8202014-02-28 14:42:48 +01006 * 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 Szyprowski3f0c8202014-02-28 14:42:48 +010010 */
11
Rob Herring606ad422016-06-15 08:32:18 -050012#define pr_fmt(fmt) "OF: reserved mem: " fmt
13
Marek Szyprowski3f0c8202014-02-28 14:42:48 +010014#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 Humpherysae1add22015-09-15 18:30:36 -070021#include <linux/sort.h>
Marek Szyprowski59ce4032016-05-24 15:31:24 +020022#include <linux/slab.h>
Mike Rapoportaca52c32018-10-30 15:07:44 -070023#include <linux/memblock.h>
Mike Rapoport658aafc2021-10-21 10:09:29 +030024#include <linux/kmemleak.h>
Marek Szyprowski3f0c8202014-02-28 14:42:48 +010025
Rob Herring12d55d32021-05-27 14:38:41 -050026#include "of_private.h"
27
Patrick Daly632c9902020-02-24 10:02:32 -080028#define MAX_RESERVED_REGIONS 64
Marek Szyprowski3f0c8202014-02-28 14:42:48 +010029static struct reserved_mem reserved_mem[MAX_RESERVED_REGIONS];
30static int reserved_mem_count;
31
Christoph Hellwig221e1e02019-02-11 14:35:45 +010032static int __init early_init_dt_alloc_reserved_memory_arch(phys_addr_t size,
Marek Szyprowski3f0c8202014-02-28 14:42:48 +010033 phys_addr_t align, phys_addr_t start, phys_addr_t end, bool nomap,
34 phys_addr_t *res_base)
35{
Vinayak Menone53b50c2016-02-22 19:15:44 +053036 phys_addr_t base;
Mike Rapoporta7259df2021-09-02 15:00:26 -070037 int err = 0;
Mike Rapoport42b46ae2019-03-11 23:29:31 -070038
Vinayak Menone53b50c2016-02-22 19:15:44 +053039 end = !end ? MEMBLOCK_ALLOC_ANYWHERE : end;
Mike Rapoport7e1c4e22018-10-30 15:09:57 -070040 align = !align ? SMP_CACHE_BYTES : align;
Mike Rapoporta7259df2021-09-02 15:00:26 -070041 base = memblock_phys_alloc_range(size, align, start, end);
Marek Szyprowski3f0c8202014-02-28 14:42:48 +010042 if (!base)
43 return -ENOMEM;
44
Marek Szyprowski3f0c8202014-02-28 14:42:48 +010045 *res_base = base;
Mike Rapoporta7259df2021-09-02 15:00:26 -070046 if (nomap) {
47 err = memblock_mark_nomap(base, size);
48 if (err)
Mike Rapoport3ecc6832021-11-05 13:43:19 -070049 memblock_phys_free(base, size);
Mike Rapoport658aafc2021-10-21 10:09:29 +030050 kmemleak_ignore_phys(base);
Mike Rapoporta7259df2021-09-02 15:00:26 -070051 }
Mike Rapoport5c01a252019-03-11 23:30:58 -070052
Mike Rapoporta7259df2021-09-02 15:00:26 -070053 return err;
Marek Szyprowski3f0c8202014-02-28 14:42:48 +010054}
Marek Szyprowski3f0c8202014-02-28 14:42:48 +010055
Lee Jonesad1ce1a2021-03-18 10:40:36 +000056/*
chenqiwuc8813f72020-05-11 23:04:57 +080057 * fdt_reserved_mem_save_node() - save fdt node for second pass initialization
Marek Szyprowski3f0c8202014-02-28 14:42:48 +010058 */
59void __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 Lin418370f2020-06-03 22:49:00 -070065 pr_err("not enough space for all defined regions.\n");
Marek Szyprowski3f0c8202014-02-28 14:42:48 +010066 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 Jonesad1ce1a2021-03-18 10:40:36 +000078/*
chenqiwuc8813f72020-05-11 23:04:57 +080079 * __reserved_mem_alloc_size() - allocate reserved memory described by
Yue Hu6f1188b2020-07-30 17:23:53 +080080 * 'size', 'alignment' and 'alloc-ranges' properties.
Marek Szyprowski3f0c8202014-02-28 14:42:48 +010081 */
82static 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 Herring9d0c4df2014-04-01 23:49:03 -050088 int len;
89 const __be32 *prop;
Yue Hu6f1188b2020-07-30 17:23:53 +080090 bool nomap;
Marek Szyprowski3f0c8202014-02-28 14:42:48 +010091 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 Herring606ad422016-06-15 08:32:18 -050098 pr_err("invalid size property in '%s' node.\n", uname);
Marek Szyprowski3f0c8202014-02-28 14:42:48 +010099 return -EINVAL;
100 }
101 size = dt_mem_next_cell(dt_root_size_cells, &prop);
102
Marek Szyprowski3f0c8202014-02-28 14:42:48 +0100103 prop = of_get_flat_dt_prop(node, "alignment", &len);
104 if (prop) {
105 if (len != dt_root_addr_cells * sizeof(__be32)) {
Rob Herring606ad422016-06-15 08:32:18 -0500106 pr_err("invalid alignment property in '%s' node.\n",
Marek Szyprowski3f0c8202014-02-28 14:42:48 +0100107 uname);
108 return -EINVAL;
109 }
110 align = dt_mem_next_cell(dt_root_addr_cells, &prop);
111 }
112
Yue Hu6f1188b2020-07-30 17:23:53 +0800113 nomap = of_get_flat_dt_prop(node, "no-map", NULL) != NULL;
114
Jason Liu1cc8e342015-11-10 20:30:26 +0800115 /* Need adjust the alignment to satisfy the CMA requirement */
Jaewon7d482812016-05-25 13:29:50 +0900116 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 Hu6f1188b2020-07-30 17:23:53 +0800119 && !nomap) {
Stephen Rothwellaaaab562016-05-31 09:38:56 +1000120 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 Liu1cc8e342015-11-10 20:30:26 +0800125
Marek Szyprowski3f0c8202014-02-28 14:42:48 +0100126 prop = of_get_flat_dt_prop(node, "alloc-ranges", &len);
127 if (prop) {
128
129 if (len % t_len != 0) {
Rob Herring606ad422016-06-15 08:32:18 -0500130 pr_err("invalid alloc-ranges property in '%s', skipping node.\n",
Marek Szyprowski3f0c8202014-02-28 14:42:48 +0100131 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 Uytterhoeven2892d8a2021-06-16 11:27:44 +0200145 pr_debug("allocated memory for '%s' node: base %pa, size %lu MiB\n",
Marek Szyprowski3f0c8202014-02-28 14:42:48 +0100146 uname, &base,
Geert Uytterhoeven2892d8a2021-06-16 11:27:44 +0200147 (unsigned long)(size / SZ_1M));
Marek Szyprowski3f0c8202014-02-28 14:42:48 +0100148 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 Uytterhoeven2892d8a2021-06-16 11:27:44 +0200157 pr_debug("allocated memory for '%s' node: base %pa, size %lu MiB\n",
158 uname, &base, (unsigned long)(size / SZ_1M));
Marek Szyprowski3f0c8202014-02-28 14:42:48 +0100159 }
160
161 if (base == 0) {
Rob Herring606ad422016-06-15 08:32:18 -0500162 pr_info("failed to allocate memory for node '%s'\n", uname);
Marek Szyprowski3f0c8202014-02-28 14:42:48 +0100163 return -ENOMEM;
164 }
165
166 *res_base = base;
167 *res_size = size;
168
169 return 0;
170}
171
Marek Szyprowskif618c472014-02-28 14:42:49 +0100172static const struct of_device_id __rmem_of_table_sentinel
Joe Perches33def842020-10-21 19:36:07 -0700173 __used __section("__reservedmem_of_table_end");
Marek Szyprowskif618c472014-02-28 14:42:49 +0100174
Lee Jonesad1ce1a2021-03-18 10:40:36 +0000175/*
chenqiwuc8813f72020-05-11 23:04:57 +0800176 * __reserved_mem_init_node() - call region specific reserved memory init code
Marek Szyprowskif618c472014-02-28 14:42:49 +0100177 */
178static 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 Kuod0b8ed42019-02-19 15:45:00 +0800182 int ret = -ENOENT;
Marek Szyprowskif618c472014-02-28 14:42:49 +0100183
184 for (i = __reservedmem_of_table; i < &__rmem_of_table_sentinel; i++) {
Rob Herringdf3ed932017-05-11 10:15:10 -0500185 reservedmem_of_init_fn initfn = i->data;
Marek Szyprowskif618c472014-02-28 14:42:49 +0100186 const char *compat = i->compatible;
187
188 if (!of_flat_dt_is_compatible(rmem->fdt_node, compat))
189 continue;
190
pierre Kuod0b8ed42019-02-19 15:45:00 +0800191 ret = initfn(rmem);
192 if (ret == 0) {
Rob Herring606ad422016-06-15 08:32:18 -0500193 pr_info("initialized node %s, compatible id %s\n",
Marek Szyprowskif618c472014-02-28 14:42:49 +0100194 rmem->name, compat);
pierre Kuod0b8ed42019-02-19 15:45:00 +0800195 break;
Marek Szyprowskif618c472014-02-28 14:42:49 +0100196 }
197 }
pierre Kuod0b8ed42019-02-19 15:45:00 +0800198 return ret;
Marek Szyprowskif618c472014-02-28 14:42:49 +0100199}
200
Mitchel Humpherysae1add22015-09-15 18:30:36 -0700201static int __init __rmem_cmp(const void *a, const void *b)
202{
203 const struct reserved_mem *ra = a, *rb = b;
204
Michael Ellerman9eb8cd22015-11-18 21:46:38 +1100205 if (ra->base < rb->base)
206 return -1;
207
208 if (ra->base > rb->base)
209 return 1;
210
Vincent Whitchurchca05f332020-10-21 11:53:59 +0200211 /*
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 Ellerman9eb8cd22015-11-18 21:46:38 +1100221 return 0;
Mitchel Humpherysae1add22015-09-15 18:30:36 -0700222}
223
224static 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 Whitchurchca05f332020-10-21 11:53:59 +0200238
Mitchel Humpherysae1add22015-09-15 18:30:36 -0700239 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 Herring606ad422016-06-15 08:32:18 -0500244 pr_err("OVERLAP DETECTED!\n%s (%pa--%pa) overlaps with %s (%pa--%pa)\n",
Michael Ellerman85a1c772015-11-10 16:08:33 +1100245 this->name, &this->base, &this_end,
246 next->name, &next->base, &next_end);
Mitchel Humpherysae1add22015-09-15 18:30:36 -0700247 }
248 }
249}
250
Marek Szyprowski3f0c8202014-02-28 14:42:48 +0100251/**
chenqiwuc8813f72020-05-11 23:04:57 +0800252 * fdt_init_reserved_mem() - allocate and init all saved reserved memory regions
Marek Szyprowski3f0c8202014-02-28 14:42:48 +0100253 */
254void __init fdt_init_reserved_mem(void)
255{
256 int i;
Mitchel Humpherysae1add22015-09-15 18:30:36 -0700257
258 /* check for overlapping reserved regions */
259 __rmem_check_for_overlap();
260
Marek Szyprowski3f0c8202014-02-28 14:42:48 +0100261 for (i = 0; i < reserved_mem_count; i++) {
262 struct reserved_mem *rmem = &reserved_mem[i];
263 unsigned long node = rmem->fdt_node;
Marek Szyprowski9dcfee02014-07-14 10:28:04 +0200264 int len;
265 const __be32 *prop;
Marek Szyprowski3f0c8202014-02-28 14:42:48 +0100266 int err = 0;
Yue Hu6f1188b2020-07-30 17:23:53 +0800267 bool nomap;
Marek Szyprowski3f0c8202014-02-28 14:42:48 +0100268
pierre Kuod0b8ed42019-02-19 15:45:00 +0800269 nomap = of_get_flat_dt_prop(node, "no-map", NULL) != NULL;
Marek Szyprowski9dcfee02014-07-14 10:28:04 +0200270 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 Szyprowski3f0c8202014-02-28 14:42:48 +0100276 if (rmem->size == 0)
277 err = __reserved_mem_alloc_size(node, rmem->name,
278 &rmem->base, &rmem->size);
pierre Kuod0b8ed42019-02-19 15:45:00 +0800279 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 Kuod0b8ed42019-02-19 15:45:00 +0800284 if (nomap)
Dong Aisheng7b259952021-06-11 21:11:53 +0800285 memblock_clear_nomap(rmem->base, rmem->size);
Dong Aisheng3c6867a2021-06-11 21:11:52 +0800286 else
Mike Rapoport3ecc6832021-11-05 13:43:19 -0700287 memblock_phys_free(rmem->base,
288 rmem->size);
pierre Kuod0b8ed42019-02-19 15:45:00 +0800289 }
290 }
Marek Szyprowski3f0c8202014-02-28 14:42:48 +0100291 }
292}
Marek Szyprowski9dcfee02014-07-14 10:28:04 +0200293
294static 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 Szyprowski59ce4032016-05-24 15:31:24 +0200307struct rmem_assigned_device {
308 struct device *dev;
Marek Szyprowski9dcfee02014-07-14 10:28:04 +0200309 struct reserved_mem *rmem;
Marek Szyprowski59ce4032016-05-24 15:31:24 +0200310 struct list_head list;
311};
312
313static LIST_HEAD(of_rmem_assigned_device_list);
314static 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 */
331int 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 Szyprowski47f29df2014-10-29 14:50:29 -0700337 int ret;
Marek Szyprowski9dcfee02014-07-14 10:28:04 +0200338
Marek Szyprowski59ce4032016-05-24 15:31:24 +0200339 if (!np || !dev)
340 return -EINVAL;
Marek Szyprowski9dcfee02014-07-14 10:28:04 +0200341
Marek Szyprowski59ce4032016-05-24 15:31:24 +0200342 target = of_parse_phandle(np, "memory-region", idx);
343 if (!target)
Marek Szyprowski9f5a8022016-06-08 03:51:53 -0300344 return -ENODEV;
Marek Szyprowski59ce4032016-05-24 15:31:24 +0200345
Chris Goldsworthy5dba5172019-10-19 18:57:24 -0700346 if (!of_device_is_available(target)) {
347 of_node_put(target);
Krishna Reddyd698a382019-05-22 16:17:11 +0530348 return 0;
Chris Goldsworthy5dba5172019-10-19 18:57:24 -0700349 }
Krishna Reddyd698a382019-05-22 16:17:11 +0530350
Marek Szyprowski59ce4032016-05-24 15:31:24 +0200351 rmem = __find_rmem(target);
352 of_node_put(target);
Marek Szyprowski9dcfee02014-07-14 10:28:04 +0200353
354 if (!rmem || !rmem->ops || !rmem->ops->device_init)
Marek Szyprowski47f29df2014-10-29 14:50:29 -0700355 return -EINVAL;
Marek Szyprowski9dcfee02014-07-14 10:28:04 +0200356
Marek Szyprowski59ce4032016-05-24 15:31:24 +0200357 rd = kmalloc(sizeof(struct rmem_assigned_device), GFP_KERNEL);
358 if (!rd)
359 return -ENOMEM;
360
Marek Szyprowski47f29df2014-10-29 14:50:29 -0700361 ret = rmem->ops->device_init(rmem, dev);
Marek Szyprowski59ce4032016-05-24 15:31:24 +0200362 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 Szyprowski47f29df2014-10-29 14:50:29 -0700370 dev_info(dev, "assigned reserved memory node %s\n", rmem->name);
Marek Szyprowski59ce4032016-05-24 15:31:24 +0200371 } else {
372 kfree(rd);
373 }
Marek Szyprowski47f29df2014-10-29 14:50:29 -0700374
375 return ret;
Marek Szyprowski9dcfee02014-07-14 10:28:04 +0200376}
Marek Szyprowski59ce4032016-05-24 15:31:24 +0200377EXPORT_SYMBOL_GPL(of_reserved_mem_device_init_by_idx);
Marek Szyprowski9dcfee02014-07-14 10:28:04 +0200378
379/**
Thierry Reding0da0e312020-04-03 19:44:52 +0200380 * 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 */
388int 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}
396EXPORT_SYMBOL_GPL(of_reserved_mem_device_init_by_name);
397
398/**
Marek Szyprowski9dcfee02014-07-14 10:28:04 +0200399 * of_reserved_mem_device_release() - release reserved memory device structures
Marek Szyprowski59ce4032016-05-24 15:31:24 +0200400 * @dev: Pointer to the device to deconfigure
Marek Szyprowski9dcfee02014-07-14 10:28:04 +0200401 *
402 * This function releases structures allocated for memory region handling for
403 * the given device.
404 */
405void of_reserved_mem_device_release(struct device *dev)
406{
Thierry Reding081df762020-04-03 19:46:57 +0200407 struct rmem_assigned_device *rd, *tmp;
408 LIST_HEAD(release_list);
Marek Szyprowski9dcfee02014-07-14 10:28:04 +0200409
Marek Szyprowski59ce4032016-05-24 15:31:24 +0200410 mutex_lock(&of_rmem_assigned_device_mutex);
Thierry Reding081df762020-04-03 19:46:57 +0200411 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 Szyprowski59ce4032016-05-24 15:31:24 +0200414 }
415 mutex_unlock(&of_rmem_assigned_device_mutex);
Marek Szyprowski9dcfee02014-07-14 10:28:04 +0200416
Thierry Reding081df762020-04-03 19:46:57 +0200417 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 Szyprowski9dcfee02014-07-14 10:28:04 +0200420
Thierry Reding081df762020-04-03 19:46:57 +0200421 kfree(rd);
422 }
Marek Szyprowski9dcfee02014-07-14 10:28:04 +0200423}
George G. Davis615fde72015-01-09 09:29:05 -0500424EXPORT_SYMBOL_GPL(of_reserved_mem_device_release);
Bjorn Anderssoneb297bc2017-10-10 22:08:54 -0700425
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 */
435struct 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}
450EXPORT_SYMBOL_GPL(of_reserved_mem_lookup);