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