thermal/int340x_thermal: Add additional UUIDs
[linux/fpc-iii.git] / drivers / of / of_reserved_mem.c
blob366d8c3c79893b04261644f8064266b8e41be82e
1 /*
2 * Device tree based initialization code for reserved memory.
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>
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.
16 #define pr_fmt(fmt) "OF: reserved mem: " fmt
18 #include <linux/err.h>
19 #include <linux/of.h>
20 #include <linux/of_fdt.h>
21 #include <linux/of_platform.h>
22 #include <linux/mm.h>
23 #include <linux/sizes.h>
24 #include <linux/of_reserved_mem.h>
25 #include <linux/sort.h>
26 #include <linux/slab.h>
28 #define MAX_RESERVED_REGIONS 16
29 static struct reserved_mem reserved_mem[MAX_RESERVED_REGIONS];
30 static int reserved_mem_count;
32 #if defined(CONFIG_HAVE_MEMBLOCK)
33 #include <linux/memblock.h>
34 int __init __weak early_init_dt_alloc_reserved_memory_arch(phys_addr_t size,
35 phys_addr_t align, phys_addr_t start, phys_addr_t end, bool nomap,
36 phys_addr_t *res_base)
38 phys_addr_t base;
40 * We use __memblock_alloc_base() because memblock_alloc_base()
41 * panic()s on allocation failure.
43 end = !end ? MEMBLOCK_ALLOC_ANYWHERE : end;
44 base = __memblock_alloc_base(size, align, end);
45 if (!base)
46 return -ENOMEM;
49 * Check if the allocated region fits in to start..end window
51 if (base < start) {
52 memblock_free(base, size);
53 return -ENOMEM;
56 *res_base = base;
57 if (nomap)
58 return memblock_remove(base, size);
59 return 0;
61 #else
62 int __init __weak early_init_dt_alloc_reserved_memory_arch(phys_addr_t size,
63 phys_addr_t align, phys_addr_t start, phys_addr_t end, bool nomap,
64 phys_addr_t *res_base)
66 pr_err("Reserved memory not supported, ignoring region 0x%llx%s\n",
67 size, nomap ? " (nomap)" : "");
68 return -ENOSYS;
70 #endif
72 /**
73 * res_mem_save_node() - save fdt node for second pass initialization
75 void __init fdt_reserved_mem_save_node(unsigned long node, const char *uname,
76 phys_addr_t base, phys_addr_t size)
78 struct reserved_mem *rmem = &reserved_mem[reserved_mem_count];
80 if (reserved_mem_count == ARRAY_SIZE(reserved_mem)) {
81 pr_err("not enough space all defined regions.\n");
82 return;
85 rmem->fdt_node = node;
86 rmem->name = uname;
87 rmem->base = base;
88 rmem->size = size;
90 reserved_mem_count++;
91 return;
94 /**
95 * res_mem_alloc_size() - allocate reserved memory described by 'size', 'align'
96 * and 'alloc-ranges' properties
98 static int __init __reserved_mem_alloc_size(unsigned long node,
99 const char *uname, phys_addr_t *res_base, phys_addr_t *res_size)
101 int t_len = (dt_root_addr_cells + dt_root_size_cells) * sizeof(__be32);
102 phys_addr_t start = 0, end = 0;
103 phys_addr_t base = 0, align = 0, size;
104 int len;
105 const __be32 *prop;
106 int nomap;
107 int ret;
109 prop = of_get_flat_dt_prop(node, "size", &len);
110 if (!prop)
111 return -EINVAL;
113 if (len != dt_root_size_cells * sizeof(__be32)) {
114 pr_err("invalid size property in '%s' node.\n", uname);
115 return -EINVAL;
117 size = dt_mem_next_cell(dt_root_size_cells, &prop);
119 nomap = of_get_flat_dt_prop(node, "no-map", NULL) != NULL;
121 prop = of_get_flat_dt_prop(node, "alignment", &len);
122 if (prop) {
123 if (len != dt_root_addr_cells * sizeof(__be32)) {
124 pr_err("invalid alignment property in '%s' node.\n",
125 uname);
126 return -EINVAL;
128 align = dt_mem_next_cell(dt_root_addr_cells, &prop);
131 /* Need adjust the alignment to satisfy the CMA requirement */
132 if (IS_ENABLED(CONFIG_CMA)
133 && of_flat_dt_is_compatible(node, "shared-dma-pool")
134 && of_get_flat_dt_prop(node, "reusable", NULL)
135 && !of_get_flat_dt_prop(node, "no-map", NULL)) {
136 unsigned long order =
137 max_t(unsigned long, MAX_ORDER - 1, pageblock_order);
139 align = max(align, (phys_addr_t)PAGE_SIZE << order);
142 prop = of_get_flat_dt_prop(node, "alloc-ranges", &len);
143 if (prop) {
145 if (len % t_len != 0) {
146 pr_err("invalid alloc-ranges property in '%s', skipping node.\n",
147 uname);
148 return -EINVAL;
151 base = 0;
153 while (len > 0) {
154 start = dt_mem_next_cell(dt_root_addr_cells, &prop);
155 end = start + dt_mem_next_cell(dt_root_size_cells,
156 &prop);
158 ret = early_init_dt_alloc_reserved_memory_arch(size,
159 align, start, end, nomap, &base);
160 if (ret == 0) {
161 pr_debug("allocated memory for '%s' node: base %pa, size %ld MiB\n",
162 uname, &base,
163 (unsigned long)size / SZ_1M);
164 break;
166 len -= t_len;
169 } else {
170 ret = early_init_dt_alloc_reserved_memory_arch(size, align,
171 0, 0, nomap, &base);
172 if (ret == 0)
173 pr_debug("allocated memory for '%s' node: base %pa, size %ld MiB\n",
174 uname, &base, (unsigned long)size / SZ_1M);
177 if (base == 0) {
178 pr_info("failed to allocate memory for node '%s'\n", uname);
179 return -ENOMEM;
182 *res_base = base;
183 *res_size = size;
185 return 0;
188 static const struct of_device_id __rmem_of_table_sentinel
189 __used __section(__reservedmem_of_table_end);
192 * res_mem_init_node() - call region specific reserved memory init code
194 static int __init __reserved_mem_init_node(struct reserved_mem *rmem)
196 extern const struct of_device_id __reservedmem_of_table[];
197 const struct of_device_id *i;
199 for (i = __reservedmem_of_table; i < &__rmem_of_table_sentinel; i++) {
200 reservedmem_of_init_fn initfn = i->data;
201 const char *compat = i->compatible;
203 if (!of_flat_dt_is_compatible(rmem->fdt_node, compat))
204 continue;
206 if (initfn(rmem) == 0) {
207 pr_info("initialized node %s, compatible id %s\n",
208 rmem->name, compat);
209 return 0;
212 return -ENOENT;
215 static int __init __rmem_cmp(const void *a, const void *b)
217 const struct reserved_mem *ra = a, *rb = b;
219 if (ra->base < rb->base)
220 return -1;
222 if (ra->base > rb->base)
223 return 1;
225 return 0;
228 static void __init __rmem_check_for_overlap(void)
230 int i;
232 if (reserved_mem_count < 2)
233 return;
235 sort(reserved_mem, reserved_mem_count, sizeof(reserved_mem[0]),
236 __rmem_cmp, NULL);
237 for (i = 0; i < reserved_mem_count - 1; i++) {
238 struct reserved_mem *this, *next;
240 this = &reserved_mem[i];
241 next = &reserved_mem[i + 1];
242 if (!(this->base && next->base))
243 continue;
244 if (this->base + this->size > next->base) {
245 phys_addr_t this_end, next_end;
247 this_end = this->base + this->size;
248 next_end = next->base + next->size;
249 pr_err("OVERLAP DETECTED!\n%s (%pa--%pa) overlaps with %s (%pa--%pa)\n",
250 this->name, &this->base, &this_end,
251 next->name, &next->base, &next_end);
257 * fdt_init_reserved_mem - allocate and init all saved reserved memory regions
259 void __init fdt_init_reserved_mem(void)
261 int i;
263 /* check for overlapping reserved regions */
264 __rmem_check_for_overlap();
266 for (i = 0; i < reserved_mem_count; i++) {
267 struct reserved_mem *rmem = &reserved_mem[i];
268 unsigned long node = rmem->fdt_node;
269 int len;
270 const __be32 *prop;
271 int err = 0;
273 prop = of_get_flat_dt_prop(node, "phandle", &len);
274 if (!prop)
275 prop = of_get_flat_dt_prop(node, "linux,phandle", &len);
276 if (prop)
277 rmem->phandle = of_read_number(prop, len/4);
279 if (rmem->size == 0)
280 err = __reserved_mem_alloc_size(node, rmem->name,
281 &rmem->base, &rmem->size);
282 if (err == 0)
283 __reserved_mem_init_node(rmem);
287 static inline struct reserved_mem *__find_rmem(struct device_node *node)
289 unsigned int i;
291 if (!node->phandle)
292 return NULL;
294 for (i = 0; i < reserved_mem_count; i++)
295 if (reserved_mem[i].phandle == node->phandle)
296 return &reserved_mem[i];
297 return NULL;
300 struct rmem_assigned_device {
301 struct device *dev;
302 struct reserved_mem *rmem;
303 struct list_head list;
306 static LIST_HEAD(of_rmem_assigned_device_list);
307 static DEFINE_MUTEX(of_rmem_assigned_device_mutex);
310 * of_reserved_mem_device_init_by_idx() - assign reserved memory region to
311 * given device
312 * @dev: Pointer to the device to configure
313 * @np: Pointer to the device_node with 'reserved-memory' property
314 * @idx: Index of selected region
316 * This function assigns respective DMA-mapping operations based on reserved
317 * memory region specified by 'memory-region' property in @np node to the @dev
318 * device. When driver needs to use more than one reserved memory region, it
319 * should allocate child devices and initialize regions by name for each of
320 * child device.
322 * Returns error code or zero on success.
324 int of_reserved_mem_device_init_by_idx(struct device *dev,
325 struct device_node *np, int idx)
327 struct rmem_assigned_device *rd;
328 struct device_node *target;
329 struct reserved_mem *rmem;
330 int ret;
332 if (!np || !dev)
333 return -EINVAL;
335 target = of_parse_phandle(np, "memory-region", idx);
336 if (!target)
337 return -ENODEV;
339 rmem = __find_rmem(target);
340 of_node_put(target);
342 if (!rmem || !rmem->ops || !rmem->ops->device_init)
343 return -EINVAL;
345 rd = kmalloc(sizeof(struct rmem_assigned_device), GFP_KERNEL);
346 if (!rd)
347 return -ENOMEM;
349 ret = rmem->ops->device_init(rmem, dev);
350 if (ret == 0) {
351 rd->dev = dev;
352 rd->rmem = rmem;
354 mutex_lock(&of_rmem_assigned_device_mutex);
355 list_add(&rd->list, &of_rmem_assigned_device_list);
356 mutex_unlock(&of_rmem_assigned_device_mutex);
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);