Grant Likely | 9eb8f67 | 2011-04-28 14:27:20 -0600 | [diff] [blame] | 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> |
Paul Gortmaker | ecea4ab | 2011-07-22 10:58:34 -0400 | [diff] [blame] | 12 | #include <linux/export.h> |
Grant Likely | 9eb8f67 | 2011-04-28 14:27:20 -0600 | [diff] [blame] | 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 | |
Lorenzo Pieralisi | a0ae0240 | 2011-11-17 17:31:51 +0000 | [diff] [blame] | 22 | #include <asm/cputype.h> |
Grant Likely | 9eb8f67 | 2011-04-28 14:27:20 -0600 | [diff] [blame] | 23 | #include <asm/setup.h> |
| 24 | #include <asm/page.h> |
Lorenzo Pieralisi | a0ae0240 | 2011-11-17 17:31:51 +0000 | [diff] [blame] | 25 | #include <asm/smp_plat.h> |
Grant Likely | 93c02ab | 2011-04-28 14:27:21 -0600 | [diff] [blame] | 26 | #include <asm/mach/arch.h> |
| 27 | #include <asm/mach-types.h> |
Grant Likely | 9eb8f67 | 2011-04-28 14:27:20 -0600 | [diff] [blame] | 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 | |
Grant Likely | 93c02ab | 2011-04-28 14:27:21 -0600 | [diff] [blame] | 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 | |
Lorenzo Pieralisi | a0ae0240 | 2011-11-17 17:31:51 +0000 | [diff] [blame] | 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 | |
Lorenzo Pieralisi | 18d7f15 | 2013-06-19 10:40:48 +0100 | [diff] [blame] | 85 | u32 tmp_map[NR_CPUS] = { [0 ... NR_CPUS-1] = MPIDR_INVALID }; |
Lorenzo Pieralisi | a0ae0240 | 2011-11-17 17:31:51 +0000 | [diff] [blame] | 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 | |
Lorenzo Pieralisi | 1ba9bf0 | 2013-06-19 10:36:26 +0100 | [diff] [blame] | 95 | if (of_node_cmp(cpu->type, "cpu")) |
| 96 | continue; |
| 97 | |
Lorenzo Pieralisi | a0ae0240 | 2011-11-17 17:31:51 +0000 | [diff] [blame] | 98 | pr_debug(" * %s...\n", cpu->full_name); |
| 99 | /* |
| 100 | * A device tree containing CPU nodes with missing "reg" |
| 101 | * properties is considered invalid to build the |
| 102 | * cpu_logical_map. |
| 103 | */ |
| 104 | if (of_property_read_u32(cpu, "reg", &hwid)) { |
| 105 | pr_debug(" * %s missing reg property\n", |
| 106 | cpu->full_name); |
| 107 | return; |
| 108 | } |
| 109 | |
| 110 | /* |
| 111 | * 8 MSBs must be set to 0 in the DT since the reg property |
| 112 | * defines the MPIDR[23:0]. |
| 113 | */ |
| 114 | if (hwid & ~MPIDR_HWID_BITMASK) |
| 115 | return; |
| 116 | |
| 117 | /* |
| 118 | * Duplicate MPIDRs are a recipe for disaster. |
| 119 | * Scan all initialized entries and check for |
| 120 | * duplicates. If any is found just bail out. |
| 121 | * temp values were initialized to UINT_MAX |
| 122 | * to avoid matching valid MPIDR[23:0] values. |
| 123 | */ |
| 124 | for (j = 0; j < cpuidx; j++) |
| 125 | if (WARN(tmp_map[j] == hwid, "Duplicate /cpu reg " |
| 126 | "properties in the DT\n")) |
| 127 | return; |
| 128 | |
| 129 | /* |
| 130 | * Build a stashed array of MPIDR values. Numbering scheme |
| 131 | * requires that if detected the boot CPU must be assigned |
| 132 | * logical id 0. Other CPUs get sequential indexes starting |
| 133 | * from 1. If a CPU node with a reg property matching the |
| 134 | * boot CPU MPIDR is detected, this is recorded so that the |
| 135 | * logical map built from DT is validated and can be used |
| 136 | * to override the map created in smp_setup_processor_id(). |
| 137 | */ |
| 138 | if (hwid == mpidr) { |
| 139 | i = 0; |
| 140 | bootcpu_valid = true; |
| 141 | } else { |
| 142 | i = cpuidx++; |
| 143 | } |
| 144 | |
Lorenzo Pieralisi | ce7b175 | 2012-11-22 18:02:54 +0100 | [diff] [blame] | 145 | if (WARN(cpuidx > nr_cpu_ids, "DT /cpu %u nodes greater than " |
| 146 | "max cores %u, capping them\n", |
| 147 | cpuidx, nr_cpu_ids)) { |
| 148 | cpuidx = nr_cpu_ids; |
Lorenzo Pieralisi | a0ae0240 | 2011-11-17 17:31:51 +0000 | [diff] [blame] | 149 | break; |
Lorenzo Pieralisi | ce7b175 | 2012-11-22 18:02:54 +0100 | [diff] [blame] | 150 | } |
| 151 | |
| 152 | tmp_map[i] = hwid; |
Lorenzo Pieralisi | a0ae0240 | 2011-11-17 17:31:51 +0000 | [diff] [blame] | 153 | } |
| 154 | |
Olof Johansson | 8d5bc1a | 2013-06-29 16:25:14 -0700 | [diff] [blame] | 155 | if (!bootcpu_valid) { |
| 156 | pr_warn("DT missing boot CPU MPIDR[23:0], fall back to default cpu_logical_map\n"); |
Lorenzo Pieralisi | a0ae0240 | 2011-11-17 17:31:51 +0000 | [diff] [blame] | 157 | return; |
Olof Johansson | 8d5bc1a | 2013-06-29 16:25:14 -0700 | [diff] [blame] | 158 | } |
Lorenzo Pieralisi | a0ae0240 | 2011-11-17 17:31:51 +0000 | [diff] [blame] | 159 | |
| 160 | /* |
| 161 | * Since the boot CPU node contains proper data, and all nodes have |
| 162 | * a reg property, the DT CPU list can be considered valid and the |
| 163 | * logical map created in smp_setup_processor_id() can be overridden |
| 164 | */ |
| 165 | for (i = 0; i < cpuidx; i++) { |
| 166 | set_cpu_possible(i, true); |
| 167 | cpu_logical_map(i) = tmp_map[i]; |
| 168 | pr_debug("cpu logical map 0x%x\n", cpu_logical_map(i)); |
| 169 | } |
| 170 | } |
| 171 | |
Grant Likely | 93c02ab | 2011-04-28 14:27:21 -0600 | [diff] [blame] | 172 | /** |
| 173 | * setup_machine_fdt - Machine setup when an dtb was passed to the kernel |
| 174 | * @dt_phys: physical address of dt blob |
| 175 | * |
| 176 | * If a dtb was passed to the kernel in r2, then use it to choose the |
| 177 | * correct machine_desc and to setup the system. |
| 178 | */ |
| 179 | struct machine_desc * __init setup_machine_fdt(unsigned int dt_phys) |
| 180 | { |
| 181 | struct boot_param_header *devtree; |
| 182 | struct machine_desc *mdesc, *mdesc_best = NULL; |
| 183 | unsigned int score, mdesc_score = ~1; |
| 184 | unsigned long dt_root; |
| 185 | const char *model; |
| 186 | |
Arnd Bergmann | 883a106 | 2013-01-31 17:51:18 +0000 | [diff] [blame] | 187 | #ifdef CONFIG_ARCH_MULTIPLATFORM |
| 188 | DT_MACHINE_START(GENERIC_DT, "Generic DT based system") |
| 189 | MACHINE_END |
| 190 | |
| 191 | mdesc_best = (struct machine_desc *)&__mach_desc_GENERIC_DT; |
| 192 | #endif |
| 193 | |
Nicolas Pitre | f506cd4 | 2011-06-09 04:58:36 +0100 | [diff] [blame] | 194 | if (!dt_phys) |
| 195 | return NULL; |
| 196 | |
Grant Likely | 93c02ab | 2011-04-28 14:27:21 -0600 | [diff] [blame] | 197 | devtree = phys_to_virt(dt_phys); |
| 198 | |
| 199 | /* check device tree validity */ |
| 200 | if (be32_to_cpu(devtree->magic) != OF_DT_HEADER) |
| 201 | return NULL; |
| 202 | |
| 203 | /* Search the mdescs for the 'best' compatible value match */ |
| 204 | initial_boot_params = devtree; |
| 205 | dt_root = of_get_flat_dt_root(); |
| 206 | for_each_machine_desc(mdesc) { |
| 207 | score = of_flat_dt_match(dt_root, mdesc->dt_compat); |
| 208 | if (score > 0 && score < mdesc_score) { |
| 209 | mdesc_best = mdesc; |
| 210 | mdesc_score = score; |
| 211 | } |
| 212 | } |
| 213 | if (!mdesc_best) { |
| 214 | const char *prop; |
| 215 | long size; |
| 216 | |
| 217 | early_print("\nError: unrecognized/unsupported " |
| 218 | "device tree compatible list:\n[ "); |
| 219 | |
| 220 | prop = of_get_flat_dt_prop(dt_root, "compatible", &size); |
| 221 | while (size > 0) { |
| 222 | early_print("'%s' ", prop); |
| 223 | size -= strlen(prop) + 1; |
| 224 | prop += strlen(prop) + 1; |
| 225 | } |
| 226 | early_print("]\n\n"); |
| 227 | |
| 228 | dump_machine_table(); /* does not return */ |
| 229 | } |
| 230 | |
| 231 | model = of_get_flat_dt_prop(dt_root, "model", NULL); |
| 232 | if (!model) |
| 233 | model = of_get_flat_dt_prop(dt_root, "compatible", NULL); |
| 234 | if (!model) |
| 235 | model = "<unknown>"; |
| 236 | pr_info("Machine: %s, model: %s\n", mdesc_best->name, model); |
| 237 | |
| 238 | /* Retrieve various information from the /chosen node */ |
| 239 | of_scan_flat_dt(early_init_dt_scan_chosen, boot_command_line); |
| 240 | /* Initialize {size,address}-cells info */ |
| 241 | of_scan_flat_dt(early_init_dt_scan_root, NULL); |
| 242 | /* Setup memory, calling early_init_dt_add_memory_arch */ |
| 243 | of_scan_flat_dt(early_init_dt_scan_memory, NULL); |
| 244 | |
| 245 | /* Change machine number to match the mdesc we're using */ |
| 246 | __machine_arch_type = mdesc_best->nr; |
| 247 | |
| 248 | return mdesc_best; |
| 249 | } |