1/*
2 * Device tree based initialization code for reserved memory.
3 *
4 * Copyright (c) 2013, 2015 The Linux Foundation. All Rights Reserved.
5 * Copyright (c) 2013,2014 Samsung Electronics Co., Ltd.
6 *		http://www.samsung.com
7 * Author: Marek Szyprowski <m.szyprowski@samsung.com>
8 * Author: Josh Cartwright <joshc@codeaurora.org>
9 *
10 * This program is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU General Public License as
12 * published by the Free Software Foundation; either version 2 of the
13 * License or (at your optional) any later version of the license.
14 */
15
16#include <linux/err.h>
17#include <linux/of.h>
18#include <linux/of_fdt.h>
19#include <linux/of_platform.h>
20#include <linux/mm.h>
21#include <linux/sizes.h>
22#include <linux/of_reserved_mem.h>
23#include <linux/sort.h>
24
25#define MAX_RESERVED_REGIONS	16
26static struct reserved_mem reserved_mem[MAX_RESERVED_REGIONS];
27static int reserved_mem_count;
28
29#if defined(CONFIG_HAVE_MEMBLOCK)
30#include <linux/memblock.h>
31int __init __weak early_init_dt_alloc_reserved_memory_arch(phys_addr_t size,
32	phys_addr_t align, phys_addr_t start, phys_addr_t end, bool nomap,
33	phys_addr_t *res_base)
34{
35	phys_addr_t base;
36	/*
37	 * We use __memblock_alloc_base() because memblock_alloc_base()
38	 * panic()s on allocation failure.
39	 */
40	end = !end ? MEMBLOCK_ALLOC_ANYWHERE : end;
41	base = __memblock_alloc_base(size, align, end);
42	if (!base)
43		return -ENOMEM;
44
45	/*
46	 * Check if the allocated region fits in to start..end window
47	 */
48	if (base < start) {
49		memblock_free(base, size);
50		return -ENOMEM;
51	}
52
53	*res_base = base;
54	if (nomap)
55		return memblock_remove(base, size);
56	return 0;
57}
58#else
59int __init __weak early_init_dt_alloc_reserved_memory_arch(phys_addr_t size,
60	phys_addr_t align, phys_addr_t start, phys_addr_t end, bool nomap,
61	phys_addr_t *res_base)
62{
63	pr_err("Reserved memory not supported, ignoring region 0x%llx%s\n",
64		  size, nomap ? " (nomap)" : "");
65	return -ENOSYS;
66}
67#endif
68
69/**
70 * res_mem_save_node() - save fdt node for second pass initialization
71 */
72void __init fdt_reserved_mem_save_node(unsigned long node, const char *uname,
73				      phys_addr_t base, phys_addr_t size)
74{
75	struct reserved_mem *rmem = &reserved_mem[reserved_mem_count];
76
77	if (reserved_mem_count == ARRAY_SIZE(reserved_mem)) {
78		pr_err("Reserved memory: not enough space all defined regions.\n");
79		return;
80	}
81
82	rmem->fdt_node = node;
83	rmem->name = uname;
84	rmem->base = base;
85	rmem->size = size;
86
87	reserved_mem_count++;
88	return;
89}
90
91/**
92 * res_mem_alloc_size() - allocate reserved memory described by 'size', 'align'
93 *			  and 'alloc-ranges' properties
94 */
95static int __init __reserved_mem_alloc_size(unsigned long node,
96	const char *uname, phys_addr_t *res_base, phys_addr_t *res_size)
97{
98	int t_len = (dt_root_addr_cells + dt_root_size_cells) * sizeof(__be32);
99	phys_addr_t start = 0, end = 0;
100	phys_addr_t base = 0, align = 0, size;
101	int len;
102	const __be32 *prop;
103	int nomap;
104	int ret;
105
106	prop = of_get_flat_dt_prop(node, "size", &len);
107	if (!prop)
108		return -EINVAL;
109
110	if (len != dt_root_size_cells * sizeof(__be32)) {
111		pr_err("Reserved memory: invalid size property in '%s' node.\n",
112				uname);
113		return -EINVAL;
114	}
115	size = dt_mem_next_cell(dt_root_size_cells, &prop);
116
117	nomap = of_get_flat_dt_prop(node, "no-map", NULL) != NULL;
118
119	prop = of_get_flat_dt_prop(node, "alignment", &len);
120	if (prop) {
121		if (len != dt_root_addr_cells * sizeof(__be32)) {
122			pr_err("Reserved memory: invalid alignment property in '%s' node.\n",
123				uname);
124			return -EINVAL;
125		}
126		align = dt_mem_next_cell(dt_root_addr_cells, &prop);
127	}
128
129	/* Need adjust the alignment to satisfy the CMA requirement */
130	if (IS_ENABLED(CONFIG_CMA) && of_flat_dt_is_compatible(node, "shared-dma-pool"))
131		align = max(align, (phys_addr_t)PAGE_SIZE << max(MAX_ORDER - 1, pageblock_order));
132
133	prop = of_get_flat_dt_prop(node, "alloc-ranges", &len);
134	if (prop) {
135
136		if (len % t_len != 0) {
137			pr_err("Reserved memory: invalid alloc-ranges property in '%s', skipping node.\n",
138			       uname);
139			return -EINVAL;
140		}
141
142		base = 0;
143
144		while (len > 0) {
145			start = dt_mem_next_cell(dt_root_addr_cells, &prop);
146			end = start + dt_mem_next_cell(dt_root_size_cells,
147						       &prop);
148
149			ret = early_init_dt_alloc_reserved_memory_arch(size,
150					align, start, end, nomap, &base);
151			if (ret == 0) {
152				pr_debug("Reserved memory: allocated memory for '%s' node: base %pa, size %ld MiB\n",
153					uname, &base,
154					(unsigned long)size / SZ_1M);
155				break;
156			}
157			len -= t_len;
158		}
159
160	} else {
161		ret = early_init_dt_alloc_reserved_memory_arch(size, align,
162							0, 0, nomap, &base);
163		if (ret == 0)
164			pr_debug("Reserved memory: allocated memory for '%s' node: base %pa, size %ld MiB\n",
165				uname, &base, (unsigned long)size / SZ_1M);
166	}
167
168	if (base == 0) {
169		pr_info("Reserved memory: failed to allocate memory for node '%s'\n",
170			uname);
171		return -ENOMEM;
172	}
173
174	*res_base = base;
175	*res_size = size;
176
177	return 0;
178}
179
180static const struct of_device_id __rmem_of_table_sentinel
181	__used __section(__reservedmem_of_table_end);
182
183/**
184 * res_mem_init_node() - call region specific reserved memory init code
185 */
186static int __init __reserved_mem_init_node(struct reserved_mem *rmem)
187{
188	extern const struct of_device_id __reservedmem_of_table[];
189	const struct of_device_id *i;
190
191	for (i = __reservedmem_of_table; i < &__rmem_of_table_sentinel; i++) {
192		reservedmem_of_init_fn initfn = i->data;
193		const char *compat = i->compatible;
194
195		if (!of_flat_dt_is_compatible(rmem->fdt_node, compat))
196			continue;
197
198		if (initfn(rmem) == 0) {
199			pr_info("Reserved memory: initialized node %s, compatible id %s\n",
200				rmem->name, compat);
201			return 0;
202		}
203	}
204	return -ENOENT;
205}
206
207static int __init __rmem_cmp(const void *a, const void *b)
208{
209	const struct reserved_mem *ra = a, *rb = b;
210
211	if (ra->base < rb->base)
212		return -1;
213
214	if (ra->base > rb->base)
215		return 1;
216
217	return 0;
218}
219
220static void __init __rmem_check_for_overlap(void)
221{
222	int i;
223
224	if (reserved_mem_count < 2)
225		return;
226
227	sort(reserved_mem, reserved_mem_count, sizeof(reserved_mem[0]),
228	     __rmem_cmp, NULL);
229	for (i = 0; i < reserved_mem_count - 1; i++) {
230		struct reserved_mem *this, *next;
231
232		this = &reserved_mem[i];
233		next = &reserved_mem[i + 1];
234		if (!(this->base && next->base))
235			continue;
236		if (this->base + this->size > next->base) {
237			phys_addr_t this_end, next_end;
238
239			this_end = this->base + this->size;
240			next_end = next->base + next->size;
241			pr_err("Reserved memory: OVERLAP DETECTED!\n%s (%pa--%pa) overlaps with %s (%pa--%pa)\n",
242			       this->name, &this->base, &this_end,
243			       next->name, &next->base, &next_end);
244		}
245	}
246}
247
248/**
249 * fdt_init_reserved_mem - allocate and init all saved reserved memory regions
250 */
251void __init fdt_init_reserved_mem(void)
252{
253	int i;
254
255	/* check for overlapping reserved regions */
256	__rmem_check_for_overlap();
257
258	for (i = 0; i < reserved_mem_count; i++) {
259		struct reserved_mem *rmem = &reserved_mem[i];
260		unsigned long node = rmem->fdt_node;
261		int len;
262		const __be32 *prop;
263		int err = 0;
264
265		prop = of_get_flat_dt_prop(node, "phandle", &len);
266		if (!prop)
267			prop = of_get_flat_dt_prop(node, "linux,phandle", &len);
268		if (prop)
269			rmem->phandle = of_read_number(prop, len/4);
270
271		if (rmem->size == 0)
272			err = __reserved_mem_alloc_size(node, rmem->name,
273						 &rmem->base, &rmem->size);
274		if (err == 0)
275			__reserved_mem_init_node(rmem);
276	}
277}
278
279static inline struct reserved_mem *__find_rmem(struct device_node *node)
280{
281	unsigned int i;
282
283	if (!node->phandle)
284		return NULL;
285
286	for (i = 0; i < reserved_mem_count; i++)
287		if (reserved_mem[i].phandle == node->phandle)
288			return &reserved_mem[i];
289	return NULL;
290}
291
292/**
293 * of_reserved_mem_device_init() - assign reserved memory region to given device
294 *
295 * This function assign memory region pointed by "memory-region" device tree
296 * property to the given device.
297 */
298int of_reserved_mem_device_init(struct device *dev)
299{
300	struct reserved_mem *rmem;
301	struct device_node *np;
302	int ret;
303
304	np = of_parse_phandle(dev->of_node, "memory-region", 0);
305	if (!np)
306		return -ENODEV;
307
308	rmem = __find_rmem(np);
309	of_node_put(np);
310
311	if (!rmem || !rmem->ops || !rmem->ops->device_init)
312		return -EINVAL;
313
314	ret = rmem->ops->device_init(rmem, dev);
315	if (ret == 0)
316		dev_info(dev, "assigned reserved memory node %s\n", rmem->name);
317
318	return ret;
319}
320EXPORT_SYMBOL_GPL(of_reserved_mem_device_init);
321
322/**
323 * of_reserved_mem_device_release() - release reserved memory device structures
324 *
325 * This function releases structures allocated for memory region handling for
326 * the given device.
327 */
328void of_reserved_mem_device_release(struct device *dev)
329{
330	struct reserved_mem *rmem;
331	struct device_node *np;
332
333	np = of_parse_phandle(dev->of_node, "memory-region", 0);
334	if (!np)
335		return;
336
337	rmem = __find_rmem(np);
338	of_node_put(np);
339
340	if (!rmem || !rmem->ops || !rmem->ops->device_release)
341		return;
342
343	rmem->ops->device_release(rmem, dev);
344}
345EXPORT_SYMBOL_GPL(of_reserved_mem_device_release);
346