Linux 4.19.133
[linux/fpc-iii.git] / drivers / of / of_reserved_mem.c
blob895c83e0c7b6c4c816b1aad72c67e2a1a5235a88
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3 * Device tree based initialization code for reserved memory.
5 * Copyright (c) 2013, 2015 The Linux Foundation. All Rights Reserved.
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>
12 #define pr_fmt(fmt) "OF: reserved mem: " fmt
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>
21 #include <linux/sort.h>
22 #include <linux/slab.h>
24 #define MAX_RESERVED_REGIONS 32
25 static struct reserved_mem reserved_mem[MAX_RESERVED_REGIONS];
26 static int reserved_mem_count;
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)
34 phys_addr_t base;
36 * We use __memblock_alloc_base() because memblock_alloc_base()
37 * panic()s on allocation failure.
39 end = !end ? MEMBLOCK_ALLOC_ANYWHERE : end;
40 base = __memblock_alloc_base(size, align, end);
41 if (!base)
42 return -ENOMEM;
45 * Check if the allocated region fits in to start..end window
47 if (base < start) {
48 memblock_free(base, size);
49 return -ENOMEM;
52 *res_base = base;
53 if (nomap)
54 return memblock_remove(base, size);
55 return 0;
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)
62 pr_err("Reserved memory not supported, ignoring region 0x%llx%s\n",
63 size, nomap ? " (nomap)" : "");
64 return -ENOSYS;
66 #endif
68 /**
69 * res_mem_save_node() - save fdt node for second pass initialization
71 void __init fdt_reserved_mem_save_node(unsigned long node, const char *uname,
72 phys_addr_t base, phys_addr_t size)
74 struct reserved_mem *rmem = &reserved_mem[reserved_mem_count];
76 if (reserved_mem_count == ARRAY_SIZE(reserved_mem)) {
77 pr_err("not enough space all defined regions.\n");
78 return;
81 rmem->fdt_node = node;
82 rmem->name = uname;
83 rmem->base = base;
84 rmem->size = size;
86 reserved_mem_count++;
87 return;
90 /**
91 * res_mem_alloc_size() - allocate reserved memory described by 'size', 'align'
92 * and 'alloc-ranges' properties
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)
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;
100 int len;
101 const __be32 *prop;
102 int nomap;
103 int ret;
105 prop = of_get_flat_dt_prop(node, "size", &len);
106 if (!prop)
107 return -EINVAL;
109 if (len != dt_root_size_cells * sizeof(__be32)) {
110 pr_err("invalid size property in '%s' node.\n", uname);
111 return -EINVAL;
113 size = dt_mem_next_cell(dt_root_size_cells, &prop);
115 nomap = of_get_flat_dt_prop(node, "no-map", NULL) != NULL;
117 prop = of_get_flat_dt_prop(node, "alignment", &len);
118 if (prop) {
119 if (len != dt_root_addr_cells * sizeof(__be32)) {
120 pr_err("invalid alignment property in '%s' node.\n",
121 uname);
122 return -EINVAL;
124 align = dt_mem_next_cell(dt_root_addr_cells, &prop);
127 /* Need adjust the alignment to satisfy the CMA requirement */
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)
131 && !of_get_flat_dt_prop(node, "no-map", NULL)) {
132 unsigned long order =
133 max_t(unsigned long, MAX_ORDER - 1, pageblock_order);
135 align = max(align, (phys_addr_t)PAGE_SIZE << order);
138 prop = of_get_flat_dt_prop(node, "alloc-ranges", &len);
139 if (prop) {
141 if (len % t_len != 0) {
142 pr_err("invalid alloc-ranges property in '%s', skipping node.\n",
143 uname);
144 return -EINVAL;
147 base = 0;
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);
154 ret = early_init_dt_alloc_reserved_memory_arch(size,
155 align, start, end, nomap, &base);
156 if (ret == 0) {
157 pr_debug("allocated memory for '%s' node: base %pa, size %ld MiB\n",
158 uname, &base,
159 (unsigned long)size / SZ_1M);
160 break;
162 len -= t_len;
165 } else {
166 ret = early_init_dt_alloc_reserved_memory_arch(size, align,
167 0, 0, nomap, &base);
168 if (ret == 0)
169 pr_debug("allocated memory for '%s' node: base %pa, size %ld MiB\n",
170 uname, &base, (unsigned long)size / SZ_1M);
173 if (base == 0) {
174 pr_info("failed to allocate memory for node '%s'\n", uname);
175 return -ENOMEM;
178 *res_base = base;
179 *res_size = size;
181 return 0;
184 static const struct of_device_id __rmem_of_table_sentinel
185 __used __section(__reservedmem_of_table_end);
188 * res_mem_init_node() - call region specific reserved memory init code
190 static int __init __reserved_mem_init_node(struct reserved_mem *rmem)
192 extern const struct of_device_id __reservedmem_of_table[];
193 const struct of_device_id *i;
195 for (i = __reservedmem_of_table; i < &__rmem_of_table_sentinel; i++) {
196 reservedmem_of_init_fn initfn = i->data;
197 const char *compat = i->compatible;
199 if (!of_flat_dt_is_compatible(rmem->fdt_node, compat))
200 continue;
202 if (initfn(rmem) == 0) {
203 pr_info("initialized node %s, compatible id %s\n",
204 rmem->name, compat);
205 return 0;
208 return -ENOENT;
211 static int __init __rmem_cmp(const void *a, const void *b)
213 const struct reserved_mem *ra = a, *rb = b;
215 if (ra->base < rb->base)
216 return -1;
218 if (ra->base > rb->base)
219 return 1;
221 return 0;
224 static void __init __rmem_check_for_overlap(void)
226 int i;
228 if (reserved_mem_count < 2)
229 return;
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;
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;
243 this_end = this->base + this->size;
244 next_end = next->base + next->size;
245 pr_err("OVERLAP DETECTED!\n%s (%pa--%pa) overlaps with %s (%pa--%pa)\n",
246 this->name, &this->base, &this_end,
247 next->name, &next->base, &next_end);
253 * fdt_init_reserved_mem - allocate and init all saved reserved memory regions
255 void __init fdt_init_reserved_mem(void)
257 int i;
259 /* check for overlapping reserved regions */
260 __rmem_check_for_overlap();
262 for (i = 0; i < reserved_mem_count; i++) {
263 struct reserved_mem *rmem = &reserved_mem[i];
264 unsigned long node = rmem->fdt_node;
265 int len;
266 const __be32 *prop;
267 int err = 0;
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);
275 if (rmem->size == 0)
276 err = __reserved_mem_alloc_size(node, rmem->name,
277 &rmem->base, &rmem->size);
278 if (err == 0)
279 __reserved_mem_init_node(rmem);
283 static inline struct reserved_mem *__find_rmem(struct device_node *node)
285 unsigned int i;
287 if (!node->phandle)
288 return NULL;
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;
296 struct rmem_assigned_device {
297 struct device *dev;
298 struct reserved_mem *rmem;
299 struct list_head list;
302 static LIST_HEAD(of_rmem_assigned_device_list);
303 static DEFINE_MUTEX(of_rmem_assigned_device_mutex);
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
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.
318 * Returns error code or zero on success.
320 int of_reserved_mem_device_init_by_idx(struct device *dev,
321 struct device_node *np, int idx)
323 struct rmem_assigned_device *rd;
324 struct device_node *target;
325 struct reserved_mem *rmem;
326 int ret;
328 if (!np || !dev)
329 return -EINVAL;
331 target = of_parse_phandle(np, "memory-region", idx);
332 if (!target)
333 return -ENODEV;
335 rmem = __find_rmem(target);
336 of_node_put(target);
338 if (!rmem || !rmem->ops || !rmem->ops->device_init)
339 return -EINVAL;
341 rd = kmalloc(sizeof(struct rmem_assigned_device), GFP_KERNEL);
342 if (!rd)
343 return -ENOMEM;
345 ret = rmem->ops->device_init(rmem, dev);
346 if (ret == 0) {
347 rd->dev = dev;
348 rd->rmem = rmem;
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);
353 /* ensure that dma_ops is set for virtual devices
354 * using reserved memory
356 of_dma_configure(dev, np, true);
358 dev_info(dev, "assigned reserved memory node %s\n", rmem->name);
359 } else {
360 kfree(rd);
363 return ret;
365 EXPORT_SYMBOL_GPL(of_reserved_mem_device_init_by_idx);
368 * of_reserved_mem_device_release() - release reserved memory device structures
369 * @dev: Pointer to the device to deconfigure
371 * This function releases structures allocated for memory region handling for
372 * the given device.
374 void of_reserved_mem_device_release(struct device *dev)
376 struct rmem_assigned_device *rd;
377 struct reserved_mem *rmem = NULL;
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;
388 mutex_unlock(&of_rmem_assigned_device_mutex);
390 if (!rmem || !rmem->ops || !rmem->ops->device_release)
391 return;
393 rmem->ops->device_release(rmem, dev);
395 EXPORT_SYMBOL_GPL(of_reserved_mem_device_release);
398 * of_reserved_mem_lookup() - acquire reserved_mem from a device node
399 * @np: node pointer of the desired reserved-memory region
401 * This function allows drivers to acquire a reference to the reserved_mem
402 * struct based on a device node handle.
404 * Returns a reserved_mem reference, or NULL on error.
406 struct reserved_mem *of_reserved_mem_lookup(struct device_node *np)
408 const char *name;
409 int i;
411 if (!np->full_name)
412 return NULL;
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];
419 return NULL;
421 EXPORT_SYMBOL_GPL(of_reserved_mem_lookup);