Merge branch 'for-3.9' of git://linux-nfs.org/~bfields/linux
[firefly-linux-kernel-4.4.55.git] / arch / arm / kernel / devtree.c
1 /*
2  *  linux/arch/arm/kernel/devtree.c
3  *
4  *  Copyright (C) 2009 Canonical Ltd. <jeremy.kerr@canonical.com>
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License version 2 as
8  * published by the Free Software Foundation.
9  */
10
11 #include <linux/init.h>
12 #include <linux/export.h>
13 #include <linux/errno.h>
14 #include <linux/types.h>
15 #include <linux/bootmem.h>
16 #include <linux/memblock.h>
17 #include <linux/of.h>
18 #include <linux/of_fdt.h>
19 #include <linux/of_irq.h>
20 #include <linux/of_platform.h>
21
22 #include <asm/cputype.h>
23 #include <asm/setup.h>
24 #include <asm/page.h>
25 #include <asm/smp_plat.h>
26 #include <asm/mach/arch.h>
27 #include <asm/mach-types.h>
28
29 void __init early_init_dt_add_memory_arch(u64 base, u64 size)
30 {
31         arm_add_memory(base, size);
32 }
33
34 void * __init early_init_dt_alloc_memory_arch(u64 size, u64 align)
35 {
36         return alloc_bootmem_align(size, align);
37 }
38
39 void __init arm_dt_memblock_reserve(void)
40 {
41         u64 *reserve_map, base, size;
42
43         if (!initial_boot_params)
44                 return;
45
46         /* Reserve the dtb region */
47         memblock_reserve(virt_to_phys(initial_boot_params),
48                          be32_to_cpu(initial_boot_params->totalsize));
49
50         /*
51          * Process the reserve map.  This will probably overlap the initrd
52          * and dtb locations which are already reserved, but overlaping
53          * doesn't hurt anything
54          */
55         reserve_map = ((void*)initial_boot_params) +
56                         be32_to_cpu(initial_boot_params->off_mem_rsvmap);
57         while (1) {
58                 base = be64_to_cpup(reserve_map++);
59                 size = be64_to_cpup(reserve_map++);
60                 if (!size)
61                         break;
62                 memblock_reserve(base, size);
63         }
64 }
65
66 /*
67  * arm_dt_init_cpu_maps - Function retrieves cpu nodes from the device tree
68  * and builds the cpu logical map array containing MPIDR values related to
69  * logical cpus
70  *
71  * Updates the cpu possible mask with the number of parsed cpu nodes
72  */
73 void __init arm_dt_init_cpu_maps(void)
74 {
75         /*
76          * Temp logical map is initialized with UINT_MAX values that are
77          * considered invalid logical map entries since the logical map must
78          * contain a list of MPIDR[23:0] values where MPIDR[31:24] must
79          * read as 0.
80          */
81         struct device_node *cpu, *cpus;
82         u32 i, j, cpuidx = 1;
83         u32 mpidr = is_smp() ? read_cpuid_mpidr() & MPIDR_HWID_BITMASK : 0;
84
85         u32 tmp_map[NR_CPUS] = { [0 ... NR_CPUS-1] = UINT_MAX };
86         bool bootcpu_valid = false;
87         cpus = of_find_node_by_path("/cpus");
88
89         if (!cpus)
90                 return;
91
92         for_each_child_of_node(cpus, cpu) {
93                 u32 hwid;
94
95                 pr_debug(" * %s...\n", cpu->full_name);
96                 /*
97                  * A device tree containing CPU nodes with missing "reg"
98                  * properties is considered invalid to build the
99                  * cpu_logical_map.
100                  */
101                 if (of_property_read_u32(cpu, "reg", &hwid)) {
102                         pr_debug(" * %s missing reg property\n",
103                                      cpu->full_name);
104                         return;
105                 }
106
107                 /*
108                  * 8 MSBs must be set to 0 in the DT since the reg property
109                  * defines the MPIDR[23:0].
110                  */
111                 if (hwid & ~MPIDR_HWID_BITMASK)
112                         return;
113
114                 /*
115                  * Duplicate MPIDRs are a recipe for disaster.
116                  * Scan all initialized entries and check for
117                  * duplicates. If any is found just bail out.
118                  * temp values were initialized to UINT_MAX
119                  * to avoid matching valid MPIDR[23:0] values.
120                  */
121                 for (j = 0; j < cpuidx; j++)
122                         if (WARN(tmp_map[j] == hwid, "Duplicate /cpu reg "
123                                                      "properties in the DT\n"))
124                                 return;
125
126                 /*
127                  * Build a stashed array of MPIDR values. Numbering scheme
128                  * requires that if detected the boot CPU must be assigned
129                  * logical id 0. Other CPUs get sequential indexes starting
130                  * from 1. If a CPU node with a reg property matching the
131                  * boot CPU MPIDR is detected, this is recorded so that the
132                  * logical map built from DT is validated and can be used
133                  * to override the map created in smp_setup_processor_id().
134                  */
135                 if (hwid == mpidr) {
136                         i = 0;
137                         bootcpu_valid = true;
138                 } else {
139                         i = cpuidx++;
140                 }
141
142                 if (WARN(cpuidx > nr_cpu_ids, "DT /cpu %u nodes greater than "
143                                                "max cores %u, capping them\n",
144                                                cpuidx, nr_cpu_ids)) {
145                         cpuidx = nr_cpu_ids;
146                         break;
147                 }
148
149                 tmp_map[i] = hwid;
150         }
151
152         if (WARN(!bootcpu_valid, "DT missing boot CPU MPIDR[23:0], "
153                                  "fall back to default cpu_logical_map\n"))
154                 return;
155
156         /*
157          * Since the boot CPU node contains proper data, and all nodes have
158          * a reg property, the DT CPU list can be considered valid and the
159          * logical map created in smp_setup_processor_id() can be overridden
160          */
161         for (i = 0; i < cpuidx; i++) {
162                 set_cpu_possible(i, true);
163                 cpu_logical_map(i) = tmp_map[i];
164                 pr_debug("cpu logical map 0x%x\n", cpu_logical_map(i));
165         }
166 }
167
168 /**
169  * setup_machine_fdt - Machine setup when an dtb was passed to the kernel
170  * @dt_phys: physical address of dt blob
171  *
172  * If a dtb was passed to the kernel in r2, then use it to choose the
173  * correct machine_desc and to setup the system.
174  */
175 struct machine_desc * __init setup_machine_fdt(unsigned int dt_phys)
176 {
177         struct boot_param_header *devtree;
178         struct machine_desc *mdesc, *mdesc_best = NULL;
179         unsigned int score, mdesc_score = ~1;
180         unsigned long dt_root;
181         const char *model;
182
183         if (!dt_phys)
184                 return NULL;
185
186         devtree = phys_to_virt(dt_phys);
187
188         /* check device tree validity */
189         if (be32_to_cpu(devtree->magic) != OF_DT_HEADER)
190                 return NULL;
191
192         /* Search the mdescs for the 'best' compatible value match */
193         initial_boot_params = devtree;
194         dt_root = of_get_flat_dt_root();
195         for_each_machine_desc(mdesc) {
196                 score = of_flat_dt_match(dt_root, mdesc->dt_compat);
197                 if (score > 0 && score < mdesc_score) {
198                         mdesc_best = mdesc;
199                         mdesc_score = score;
200                 }
201         }
202         if (!mdesc_best) {
203                 const char *prop;
204                 long size;
205
206                 early_print("\nError: unrecognized/unsupported "
207                             "device tree compatible list:\n[ ");
208
209                 prop = of_get_flat_dt_prop(dt_root, "compatible", &size);
210                 while (size > 0) {
211                         early_print("'%s' ", prop);
212                         size -= strlen(prop) + 1;
213                         prop += strlen(prop) + 1;
214                 }
215                 early_print("]\n\n");
216
217                 dump_machine_table(); /* does not return */
218         }
219
220         model = of_get_flat_dt_prop(dt_root, "model", NULL);
221         if (!model)
222                 model = of_get_flat_dt_prop(dt_root, "compatible", NULL);
223         if (!model)
224                 model = "<unknown>";
225         pr_info("Machine: %s, model: %s\n", mdesc_best->name, model);
226
227         /* Retrieve various information from the /chosen node */
228         of_scan_flat_dt(early_init_dt_scan_chosen, boot_command_line);
229         /* Initialize {size,address}-cells info */
230         of_scan_flat_dt(early_init_dt_scan_root, NULL);
231         /* Setup memory, calling early_init_dt_add_memory_arch */
232         of_scan_flat_dt(early_init_dt_scan_memory, NULL);
233
234         /* Change machine number to match the mdesc we're using */
235         __machine_arch_type = mdesc_best->nr;
236
237         return mdesc_best;
238 }