2 * linux/arch/arm/kernel/devtree.c
4 * Copyright (C) 2009 Canonical Ltd. <jeremy.kerr@canonical.com>
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.
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>
18 #include <linux/of_fdt.h>
19 #include <linux/of_irq.h>
20 #include <linux/of_platform.h>
22 #include <asm/cputype.h>
23 #include <asm/setup.h>
25 #include <asm/smp_plat.h>
26 #include <asm/mach/arch.h>
27 #include <asm/mach-types.h>
29 void __init
early_init_dt_add_memory_arch(u64 base
, u64 size
)
31 arm_add_memory(base
, size
);
34 void * __init
early_init_dt_alloc_memory_arch(u64 size
, u64 align
)
36 return alloc_bootmem_align(size
, align
);
39 void __init
arm_dt_memblock_reserve(void)
41 u64
*reserve_map
, base
, size
;
43 if (!initial_boot_params
)
46 /* Reserve the dtb region */
47 memblock_reserve(virt_to_phys(initial_boot_params
),
48 be32_to_cpu(initial_boot_params
->totalsize
));
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
55 reserve_map
= ((void*)initial_boot_params
) +
56 be32_to_cpu(initial_boot_params
->off_mem_rsvmap
);
58 base
= be64_to_cpup(reserve_map
++);
59 size
= be64_to_cpup(reserve_map
++);
62 memblock_reserve(base
, size
);
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
71 * Updates the cpu possible mask with the number of parsed cpu nodes
73 void __init
arm_dt_init_cpu_maps(void)
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
81 struct device_node
*cpu
, *cpus
;
83 u32 mpidr
= is_smp() ? read_cpuid_mpidr() & MPIDR_HWID_BITMASK
: 0;
85 u32 tmp_map
[NR_CPUS
] = { [0 ... NR_CPUS
-1] = MPIDR_INVALID
};
86 bool bootcpu_valid
= false;
87 cpus
= of_find_node_by_path("/cpus");
92 for_each_child_of_node(cpus
, cpu
) {
97 if (of_node_cmp(cpu
->type
, "cpu"))
100 pr_debug(" * %s...\n", cpu
->full_name
);
102 * A device tree containing CPU nodes with missing "reg"
103 * properties is considered invalid to build the
106 cell
= of_get_property(cpu
, "reg", &prop_bytes
);
107 if (!cell
|| prop_bytes
< sizeof(*cell
)) {
108 pr_debug(" * %s missing reg property\n",
114 * Bits n:24 must be set to 0 in the DT since the reg property
115 * defines the MPIDR[23:0].
118 hwid
= be32_to_cpu(*cell
++);
119 prop_bytes
-= sizeof(*cell
);
120 } while (!hwid
&& prop_bytes
> 0);
122 if (prop_bytes
|| (hwid
& ~MPIDR_HWID_BITMASK
))
126 * Duplicate MPIDRs are a recipe for disaster.
127 * Scan all initialized entries and check for
128 * duplicates. If any is found just bail out.
129 * temp values were initialized to UINT_MAX
130 * to avoid matching valid MPIDR[23:0] values.
132 for (j
= 0; j
< cpuidx
; j
++)
133 if (WARN(tmp_map
[j
] == hwid
, "Duplicate /cpu reg "
134 "properties in the DT\n"))
138 * Build a stashed array of MPIDR values. Numbering scheme
139 * requires that if detected the boot CPU must be assigned
140 * logical id 0. Other CPUs get sequential indexes starting
141 * from 1. If a CPU node with a reg property matching the
142 * boot CPU MPIDR is detected, this is recorded so that the
143 * logical map built from DT is validated and can be used
144 * to override the map created in smp_setup_processor_id().
148 bootcpu_valid
= true;
153 if (WARN(cpuidx
> nr_cpu_ids
, "DT /cpu %u nodes greater than "
154 "max cores %u, capping them\n",
155 cpuidx
, nr_cpu_ids
)) {
163 if (!bootcpu_valid
) {
164 pr_warn("DT missing boot CPU MPIDR[23:0], fall back to default cpu_logical_map\n");
169 * Since the boot CPU node contains proper data, and all nodes have
170 * a reg property, the DT CPU list can be considered valid and the
171 * logical map created in smp_setup_processor_id() can be overridden
173 for (i
= 0; i
< cpuidx
; i
++) {
174 set_cpu_possible(i
, true);
175 cpu_logical_map(i
) = tmp_map
[i
];
176 pr_debug("cpu logical map 0x%x\n", cpu_logical_map(i
));
180 bool arch_match_cpu_phys_id(int cpu
, u64 phys_id
)
182 return phys_id
== cpu_logical_map(cpu
);
186 * setup_machine_fdt - Machine setup when an dtb was passed to the kernel
187 * @dt_phys: physical address of dt blob
189 * If a dtb was passed to the kernel in r2, then use it to choose the
190 * correct machine_desc and to setup the system.
192 const struct machine_desc
* __init
setup_machine_fdt(unsigned int dt_phys
)
194 struct boot_param_header
*devtree
;
195 const struct machine_desc
*mdesc
, *mdesc_best
= NULL
;
196 unsigned int score
, mdesc_score
= ~1;
197 unsigned long dt_root
;
200 #ifdef CONFIG_ARCH_MULTIPLATFORM
201 DT_MACHINE_START(GENERIC_DT
, "Generic DT based system")
204 mdesc_best
= &__mach_desc_GENERIC_DT
;
210 devtree
= phys_to_virt(dt_phys
);
212 /* check device tree validity */
213 if (be32_to_cpu(devtree
->magic
) != OF_DT_HEADER
)
216 /* Search the mdescs for the 'best' compatible value match */
217 initial_boot_params
= devtree
;
218 dt_root
= of_get_flat_dt_root();
219 for_each_machine_desc(mdesc
) {
220 score
= of_flat_dt_match(dt_root
, mdesc
->dt_compat
);
221 if (score
> 0 && score
< mdesc_score
) {
230 early_print("\nError: unrecognized/unsupported "
231 "device tree compatible list:\n[ ");
233 prop
= of_get_flat_dt_prop(dt_root
, "compatible", &size
);
235 early_print("'%s' ", prop
);
236 size
-= strlen(prop
) + 1;
237 prop
+= strlen(prop
) + 1;
239 early_print("]\n\n");
241 dump_machine_table(); /* does not return */
244 model
= of_get_flat_dt_prop(dt_root
, "model", NULL
);
246 model
= of_get_flat_dt_prop(dt_root
, "compatible", NULL
);
249 pr_info("Machine: %s, model: %s\n", mdesc_best
->name
, model
);
251 /* Retrieve various information from the /chosen node */
252 of_scan_flat_dt(early_init_dt_scan_chosen
, boot_command_line
);
253 /* Initialize {size,address}-cells info */
254 of_scan_flat_dt(early_init_dt_scan_root
, NULL
);
255 /* Setup memory, calling early_init_dt_add_memory_arch */
256 of_scan_flat_dt(early_init_dt_scan_memory
, NULL
);
258 /* Change machine number to match the mdesc we're using */
259 __machine_arch_type
= mdesc_best
->nr
;