| 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 | extern char default_command_line[COMMAND_LINE_SIZE]; |
| 30 | |
| 31 | void __init early_init_dt_add_memory_arch(u64 base, u64 size) |
| 32 | { |
| 33 | arm_add_memory(base, size); |
| 34 | } |
| 35 | |
| 36 | void * __init early_init_dt_alloc_memory_arch(u64 size, u64 align) |
| 37 | { |
| 38 | return alloc_bootmem_align(size, align); |
| 39 | } |
| 40 | |
| 41 | void __init arm_dt_memblock_reserve(void) |
| 42 | { |
| 43 | u64 *reserve_map, base, size; |
| 44 | |
| 45 | if (!initial_boot_params) |
| 46 | return; |
| 47 | |
| 48 | /* Reserve the dtb region */ |
| 49 | memblock_reserve(virt_to_phys(initial_boot_params), |
| 50 | be32_to_cpu(initial_boot_params->totalsize)); |
| 51 | |
| 52 | /* |
| 53 | * Process the reserve map. This will probably overlap the initrd |
| 54 | * and dtb locations which are already reserved, but overlaping |
| 55 | * doesn't hurt anything |
| 56 | */ |
| 57 | reserve_map = ((void*)initial_boot_params) + |
| 58 | be32_to_cpu(initial_boot_params->off_mem_rsvmap); |
| 59 | while (1) { |
| 60 | base = be64_to_cpup(reserve_map++); |
| 61 | size = be64_to_cpup(reserve_map++); |
| 62 | if (!size) |
| 63 | break; |
| 64 | memblock_reserve(base, size); |
| 65 | } |
| 66 | } |
| 67 | |
| 68 | /* |
| 69 | * arm_dt_init_cpu_maps - Function retrieves cpu nodes from the device tree |
| 70 | * and builds the cpu logical map array containing MPIDR values related to |
| 71 | * logical cpus |
| 72 | * |
| 73 | * Updates the cpu possible mask with the number of parsed cpu nodes |
| 74 | */ |
| 75 | void __init arm_dt_init_cpu_maps(void) |
| 76 | { |
| 77 | /* |
| 78 | * Temp logical map is initialized with UINT_MAX values that are |
| 79 | * considered invalid logical map entries since the logical map must |
| 80 | * contain a list of MPIDR[23:0] values where MPIDR[31:24] must |
| 81 | * read as 0. |
| 82 | */ |
| 83 | struct device_node *cpu, *cpus; |
| 84 | u32 i, j, cpuidx = 1; |
| 85 | u32 mpidr = is_smp() ? read_cpuid_mpidr() & MPIDR_HWID_BITMASK : 0; |
| 86 | |
| 87 | u32 tmp_map[NR_CPUS] = { [0 ... NR_CPUS-1] = MPIDR_INVALID }; |
| 88 | bool bootcpu_valid = false; |
| 89 | cpus = of_find_node_by_path("/cpus"); |
| 90 | |
| 91 | if (!cpus) |
| 92 | return; |
| 93 | |
| 94 | for_each_child_of_node(cpus, cpu) { |
| 95 | const __be32 *cell; |
| 96 | int prop_bytes; |
| 97 | u32 hwid; |
| 98 | |
| 99 | if (of_node_cmp(cpu->type, "cpu")) |
| 100 | continue; |
| 101 | |
| 102 | pr_debug(" * %s...\n", cpu->full_name); |
| 103 | /* |
| 104 | * A device tree containing CPU nodes with missing "reg" |
| 105 | * properties is considered invalid to build the |
| 106 | * cpu_logical_map. |
| 107 | */ |
| 108 | cell = of_get_property(cpu, "reg", &prop_bytes); |
| 109 | if (!cell || prop_bytes < sizeof(*cell)) { |
| 110 | pr_debug(" * %s missing reg property\n", |
| 111 | cpu->full_name); |
| 112 | return; |
| 113 | } |
| 114 | |
| 115 | /* |
| 116 | * Bits n:24 must be set to 0 in the DT since the reg property |
| 117 | * defines the MPIDR[23:0]. |
| 118 | */ |
| 119 | do { |
| 120 | hwid = be32_to_cpu(*cell++); |
| 121 | prop_bytes -= sizeof(*cell); |
| 122 | } while (!hwid && prop_bytes > 0); |
| 123 | |
| 124 | if (prop_bytes || (hwid & ~MPIDR_HWID_BITMASK)) |
| 125 | return; |
| 126 | |
| 127 | /* |
| 128 | * Duplicate MPIDRs are a recipe for disaster. |
| 129 | * Scan all initialized entries and check for |
| 130 | * duplicates. If any is found just bail out. |
| 131 | * temp values were initialized to UINT_MAX |
| 132 | * to avoid matching valid MPIDR[23:0] values. |
| 133 | */ |
| 134 | for (j = 0; j < cpuidx; j++) |
| 135 | if (WARN(tmp_map[j] == hwid, "Duplicate /cpu reg " |
| 136 | "properties in the DT\n")) |
| 137 | return; |
| 138 | |
| 139 | /* |
| 140 | * Build a stashed array of MPIDR values. Numbering scheme |
| 141 | * requires that if detected the boot CPU must be assigned |
| 142 | * logical id 0. Other CPUs get sequential indexes starting |
| 143 | * from 1. If a CPU node with a reg property matching the |
| 144 | * boot CPU MPIDR is detected, this is recorded so that the |
| 145 | * logical map built from DT is validated and can be used |
| 146 | * to override the map created in smp_setup_processor_id(). |
| 147 | */ |
| 148 | if (hwid == mpidr) { |
| 149 | i = 0; |
| 150 | bootcpu_valid = true; |
| 151 | } else { |
| 152 | i = cpuidx++; |
| 153 | } |
| 154 | |
| 155 | if (WARN(cpuidx > nr_cpu_ids, "DT /cpu %u nodes greater than " |
| 156 | "max cores %u, capping them\n", |
| 157 | cpuidx, nr_cpu_ids)) { |
| 158 | cpuidx = nr_cpu_ids; |
| 159 | break; |
| 160 | } |
| 161 | |
| 162 | tmp_map[i] = hwid; |
| 163 | } |
| 164 | |
| 165 | if (!bootcpu_valid) { |
| 166 | pr_warn("DT missing boot CPU MPIDR[23:0], fall back to default cpu_logical_map\n"); |
| 167 | return; |
| 168 | } |
| 169 | |
| 170 | /* |
| 171 | * Since the boot CPU node contains proper data, and all nodes have |
| 172 | * a reg property, the DT CPU list can be considered valid and the |
| 173 | * logical map created in smp_setup_processor_id() can be overridden |
| 174 | */ |
| 175 | for (i = 0; i < cpuidx; i++) { |
| 176 | set_cpu_possible(i, true); |
| 177 | cpu_logical_map(i) = tmp_map[i]; |
| 178 | pr_debug("cpu logical map 0x%x\n", cpu_logical_map(i)); |
| 179 | } |
| 180 | } |
| 181 | |
| 182 | /** |
| 183 | * setup_machine_fdt - Machine setup when an dtb was passed to the kernel |
| 184 | * @dt_phys: physical address of dt blob |
| 185 | * |
| 186 | * If a dtb was passed to the kernel in r2, then use it to choose the |
| 187 | * correct machine_desc and to setup the system. |
| 188 | */ |
| 189 | struct machine_desc * __init setup_machine_fdt(unsigned int dt_phys) |
| 190 | { |
| 191 | struct boot_param_header *devtree; |
| 192 | struct machine_desc *mdesc, *mdesc_best = NULL; |
| 193 | unsigned int score, mdesc_score = ~1; |
| 194 | unsigned long dt_root; |
| 195 | const char *model; |
| 196 | char *from = default_command_line; |
| 197 | |
| 198 | #ifdef CONFIG_ARCH_MULTIPLATFORM |
| 199 | DT_MACHINE_START(GENERIC_DT, "Generic DT based system") |
| 200 | MACHINE_END |
| 201 | |
| 202 | mdesc_best = (struct machine_desc *)&__mach_desc_GENERIC_DT; |
| 203 | #endif |
| 204 | |
| 205 | if (!dt_phys) |
| 206 | return NULL; |
| 207 | |
| 208 | devtree = phys_to_virt(dt_phys); |
| 209 | |
| 210 | /* check device tree validity */ |
| 211 | if (be32_to_cpu(devtree->magic) != OF_DT_HEADER) |
| 212 | return NULL; |
| 213 | |
| 214 | /* Search the mdescs for the 'best' compatible value match */ |
| 215 | initial_boot_params = devtree; |
| 216 | dt_root = of_get_flat_dt_root(); |
| 217 | for_each_machine_desc(mdesc) { |
| 218 | score = of_flat_dt_match(dt_root, mdesc->dt_compat); |
| 219 | if (score > 0 && score < mdesc_score) { |
| 220 | mdesc_best = mdesc; |
| 221 | mdesc_score = score; |
| 222 | } |
| 223 | } |
| 224 | if (!mdesc_best) { |
| 225 | const char *prop; |
| 226 | long size; |
| 227 | |
| 228 | early_print("\nError: unrecognized/unsupported " |
| 229 | "device tree compatible list:\n[ "); |
| 230 | |
| 231 | prop = of_get_flat_dt_prop(dt_root, "compatible", &size); |
| 232 | while (size > 0) { |
| 233 | early_print("'%s' ", prop); |
| 234 | size -= strlen(prop) + 1; |
| 235 | prop += strlen(prop) + 1; |
| 236 | } |
| 237 | early_print("]\n\n"); |
| 238 | |
| 239 | dump_machine_table(); /* does not return */ |
| 240 | } |
| 241 | |
| 242 | model = of_get_flat_dt_prop(dt_root, "model", NULL); |
| 243 | if (!model) |
| 244 | model = of_get_flat_dt_prop(dt_root, "compatible", NULL); |
| 245 | if (!model) |
| 246 | model = "<unknown>"; |
| 247 | pr_info("Machine: %s, model: %s\n", mdesc_best->name, model); |
| 248 | |
| 249 | /* Retrieve various information from the /chosen node */ |
| 250 | of_scan_flat_dt(early_init_dt_scan_chosen, boot_command_line); |
| 251 | /* Initialize {size,address}-cells info */ |
| 252 | of_scan_flat_dt(early_init_dt_scan_root, NULL); |
| 253 | /* Setup memory, calling early_init_dt_add_memory_arch */ |
| 254 | of_scan_flat_dt(early_init_dt_scan_memory, NULL); |
| 255 | |
| 256 | /* Change machine number to match the mdesc we're using */ |
| 257 | __machine_arch_type = mdesc_best->nr; |
| 258 | |
| 259 | if (mdesc_best->fixup) { |
| 260 | mdesc_best->fixup((void *)dt_root, &from, &meminfo); |
| 261 | strlcpy(boot_command_line, from, COMMAND_LINE_SIZE); |
| 262 | } |
| 263 | |
| 264 | return mdesc_best; |
| 265 | } |