kernel: update 3.14 to 3.14.18
[openwrt.git] / target / linux / ipq806x / patches / 0049-drivers-of-add-initialization-code-for-static-reserv.patch
1 From 60ee5b21ce14a7d5c269115b0751b904d0e005ed Mon Sep 17 00:00:00 2001
2 From: Marek Szyprowski <m.szyprowski@samsung.com>
3 Date: Fri, 28 Feb 2014 14:42:47 +0100
4 Subject: [PATCH 049/182] drivers: of: add initialization code for static
5  reserved memory
6
7 This patch adds support for static (defined by 'reg' property) reserved
8 memory regions declared in device tree.
9
10 Memory blocks can be reliably reserved only during early boot. This must
11 happen before the whole memory management subsystem is initialized,
12 because we need to ensure that the given contiguous blocks are not yet
13 allocated by kernel. Also it must happen before kernel mappings for the
14 whole low memory are created, to ensure that there will be no mappings
15 (for reserved blocks). Typically, all this happens before device tree
16 structures are unflattened, so we need to get reserved memory layout
17 directly from fdt.
18
19 Based on previous code provided by Josh Cartwright <joshc@codeaurora.org>
20
21 Signed-off-by: Marek Szyprowski <m.szyprowski@samsung.com>
22 Signed-off-by: Grant Likely <grant.likely@linaro.org>
23 ---
24  drivers/of/fdt.c       |  131 ++++++++++++++++++++++++++++++++++++++++++++++++
25  include/linux/of_fdt.h |    4 ++
26  2 files changed, 135 insertions(+)
27
28 --- a/drivers/of/fdt.c
29 +++ b/drivers/of/fdt.c
30 @@ -15,6 +15,7 @@
31  #include <linux/module.h>
32  #include <linux/of.h>
33  #include <linux/of_fdt.h>
34 +#include <linux/sizes.h>
35  #include <linux/string.h>
36  #include <linux/errno.h>
37  #include <linux/slab.h>
38 @@ -440,6 +441,118 @@ struct boot_param_header *initial_boot_p
39  #ifdef CONFIG_OF_EARLY_FLATTREE
40  
41  /**
42 + * res_mem_reserve_reg() - reserve all memory described in 'reg' property
43 + */
44 +static int __init __reserved_mem_reserve_reg(unsigned long node,
45 +                                            const char *uname)
46 +{
47 +       int t_len = (dt_root_addr_cells + dt_root_size_cells) * sizeof(__be32);
48 +       phys_addr_t base, size;
49 +       unsigned long len;
50 +       __be32 *prop;
51 +       int nomap;
52 +
53 +       prop = of_get_flat_dt_prop(node, "reg", &len);
54 +       if (!prop)
55 +               return -ENOENT;
56 +
57 +       if (len && len % t_len != 0) {
58 +               pr_err("Reserved memory: invalid reg property in '%s', skipping node.\n",
59 +                      uname);
60 +               return -EINVAL;
61 +       }
62 +
63 +       nomap = of_get_flat_dt_prop(node, "no-map", NULL) != NULL;
64 +
65 +       while (len >= t_len) {
66 +               base = dt_mem_next_cell(dt_root_addr_cells, &prop);
67 +               size = dt_mem_next_cell(dt_root_size_cells, &prop);
68 +
69 +               if (base && size &&
70 +                   early_init_dt_reserve_memory_arch(base, size, nomap) == 0)
71 +                       pr_debug("Reserved memory: reserved region for node '%s': base %pa, size %ld MiB\n",
72 +                               uname, &base, (unsigned long)size / SZ_1M);
73 +               else
74 +                       pr_info("Reserved memory: failed to reserve memory for node '%s': base %pa, size %ld MiB\n",
75 +                               uname, &base, (unsigned long)size / SZ_1M);
76 +
77 +               len -= t_len;
78 +       }
79 +       return 0;
80 +}
81 +
82 +/**
83 + * __reserved_mem_check_root() - check if #size-cells, #address-cells provided
84 + * in /reserved-memory matches the values supported by the current implementation,
85 + * also check if ranges property has been provided
86 + */
87 +static int __reserved_mem_check_root(unsigned long node)
88 +{
89 +       __be32 *prop;
90 +
91 +       prop = of_get_flat_dt_prop(node, "#size-cells", NULL);
92 +       if (!prop || be32_to_cpup(prop) != dt_root_size_cells)
93 +               return -EINVAL;
94 +
95 +       prop = of_get_flat_dt_prop(node, "#address-cells", NULL);
96 +       if (!prop || be32_to_cpup(prop) != dt_root_addr_cells)
97 +               return -EINVAL;
98 +
99 +       prop = of_get_flat_dt_prop(node, "ranges", NULL);
100 +       if (!prop)
101 +               return -EINVAL;
102 +       return 0;
103 +}
104 +
105 +/**
106 + * fdt_scan_reserved_mem() - scan a single FDT node for reserved memory
107 + */
108 +static int __init __fdt_scan_reserved_mem(unsigned long node, const char *uname,
109 +                                         int depth, void *data)
110 +{
111 +       static int found;
112 +       const char *status;
113 +
114 +       if (!found && depth == 1 && strcmp(uname, "reserved-memory") == 0) {
115 +               if (__reserved_mem_check_root(node) != 0) {
116 +                       pr_err("Reserved memory: unsupported node format, ignoring\n");
117 +                       /* break scan */
118 +                       return 1;
119 +               }
120 +               found = 1;
121 +               /* scan next node */
122 +               return 0;
123 +       } else if (!found) {
124 +               /* scan next node */
125 +               return 0;
126 +       } else if (found && depth < 2) {
127 +               /* scanning of /reserved-memory has been finished */
128 +               return 1;
129 +       }
130 +
131 +       status = of_get_flat_dt_prop(node, "status", NULL);
132 +       if (status && strcmp(status, "okay") != 0 && strcmp(status, "ok") != 0)
133 +               return 0;
134 +
135 +       __reserved_mem_reserve_reg(node, uname);
136 +
137 +       /* scan next node */
138 +       return 0;
139 +}
140 +
141 +/**
142 + * early_init_fdt_scan_reserved_mem() - create reserved memory regions
143 + *
144 + * This function grabs memory from early allocator for device exclusive use
145 + * defined in device tree structures. It should be called by arch specific code
146 + * once the early allocator (i.e. memblock) has been fully activated.
147 + */
148 +void __init early_init_fdt_scan_reserved_mem(void)
149 +{
150 +       of_scan_flat_dt(__fdt_scan_reserved_mem, NULL);
151 +}
152 +
153 +/**
154   * of_scan_flat_dt - scan flattened tree blob and call callback on each.
155   * @it: callback function
156   * @data: context data pointer
157 @@ -859,6 +972,16 @@ void __init __weak early_init_dt_add_mem
158         memblock_add(base, size);
159  }
160  
161 +int __init __weak early_init_dt_reserve_memory_arch(phys_addr_t base,
162 +                                       phys_addr_t size, bool nomap)
163 +{
164 +       if (memblock_is_region_reserved(base, size))
165 +               return -EBUSY;
166 +       if (nomap)
167 +               return memblock_remove(base, size);
168 +       return memblock_reserve(base, size);
169 +}
170 +
171  /*
172   * called from unflatten_device_tree() to bootstrap devicetree itself
173   * Architectures can override this definition if memblock isn't used
174 @@ -867,6 +990,14 @@ void * __init __weak early_init_dt_alloc
175  {
176         return __va(memblock_alloc(size, align));
177  }
178 +#else
179 +int __init __weak early_init_dt_reserve_memory_arch(phys_addr_t base,
180 +                                       phys_addr_t size, bool nomap)
181 +{
182 +       pr_err("Reserved memory not supported, ignoring range 0x%llx - 0x%llx%s\n",
183 +                 base, size, nomap ? " (nomap)" : "");
184 +       return -ENOSYS;
185 +}
186  #endif
187  
188  bool __init early_init_dt_scan(void *params)
189 --- a/include/linux/of_fdt.h
190 +++ b/include/linux/of_fdt.h
191 @@ -98,7 +98,10 @@ extern int early_init_dt_scan_chosen(uns
192                                      int depth, void *data);
193  extern int early_init_dt_scan_memory(unsigned long node, const char *uname,
194                                      int depth, void *data);
195 +extern void early_init_fdt_scan_reserved_mem(void);
196  extern void early_init_dt_add_memory_arch(u64 base, u64 size);
197 +extern int early_init_dt_reserve_memory_arch(phys_addr_t base, phys_addr_t size,
198 +                                            bool no_map);
199  extern void * early_init_dt_alloc_memory_arch(u64 size, u64 align);
200  extern u64 dt_mem_next_cell(int s, __be32 **cellp);
201  
202 @@ -118,6 +121,7 @@ extern void unflatten_and_copy_device_tr
203  extern void early_init_devtree(void *);
204  extern void early_get_first_memblock_info(void *, phys_addr_t *);
205  #else /* CONFIG_OF_FLATTREE */
206 +static inline void early_init_fdt_scan_reserved_mem(void) {}
207  static inline const char *of_flat_dt_get_machine_name(void) { return NULL; }
208  static inline void unflatten_device_tree(void) {}
209  static inline void unflatten_and_copy_device_tree(void) {}