Aaron Durbin | b30c9b1 | 2014-09-18 11:52:16 -0500 | [diff] [blame] | 1 | /* |
| 2 | * This file is part of the coreboot project. |
| 3 | * |
| 4 | * Copyright 2014 Google Inc. |
| 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 as published by |
| 8 | * the Free Software Foundation; version 2 of the License. |
| 9 | * |
| 10 | * This program is distributed in the hope that it will be useful, |
| 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | * GNU General Public License for more details. |
| 14 | * |
| 15 | * You should have received a copy of the GNU General Public License |
| 16 | * along with this program; if not, write to the Free Software |
| 17 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA, 02110-1301 USA |
| 18 | */ |
| 19 | |
| 20 | #include <stdint.h> |
| 21 | #include <stdlib.h> |
| 22 | #include <arch/lib_helpers.h> |
| 23 | #include <cpu/cpu.h> |
| 24 | #include <console/console.h> |
| 25 | #include <gic.h> |
| 26 | #include "cpu-internal.h" |
| 27 | |
| 28 | static inline void cpu_disable_dev(device_t dev) |
| 29 | { |
| 30 | dev->enabled = 0; |
| 31 | } |
| 32 | |
| 33 | static struct cpu_driver *locate_cpu_driver(uint32_t midr) |
| 34 | { |
| 35 | struct cpu_driver *cur; |
| 36 | |
| 37 | for (cur = cpu_drivers; cur != ecpu_drivers; cur++) { |
| 38 | const struct cpu_device_id *id_table = cur->id_table; |
| 39 | |
| 40 | for (; id_table->midr != CPU_ID_END; id_table++) { |
| 41 | if (id_table->midr == midr) |
| 42 | return cur; |
| 43 | } |
| 44 | } |
| 45 | return NULL; |
| 46 | } |
| 47 | |
| 48 | static int cpu_set_device_operations(device_t dev) |
| 49 | { |
| 50 | uint32_t midr; |
| 51 | struct cpu_driver *driver; |
| 52 | |
| 53 | midr = raw_read_midr_el1(); |
| 54 | driver = locate_cpu_driver(midr); |
| 55 | |
| 56 | if (driver == NULL) { |
| 57 | printk(BIOS_WARNING, "No CPU driver for MIDR %08x\n", midr); |
| 58 | return -1; |
| 59 | } |
| 60 | dev->ops = driver->ops; |
| 61 | return 0; |
| 62 | } |
| 63 | |
| 64 | /* Set up default SCR values. */ |
| 65 | static void el3_init(void) |
| 66 | { |
| 67 | uint32_t scr; |
| 68 | |
| 69 | if (get_current_el() != EL3) |
| 70 | return; |
| 71 | |
| 72 | scr = raw_read_scr_el3(); |
| 73 | /* Default to non-secure EL1 and EL0. */ |
| 74 | scr &= ~(SCR_NS_MASK); |
| 75 | scr |= SCR_NS_ENABLE; |
| 76 | /* Disable IRQ, FIQ, and external abort interrupt routing. */ |
| 77 | scr &= ~(SCR_IRQ_MASK | SCR_FIQ_MASK | SCR_EA_MASK); |
| 78 | scr |= SCR_IRQ_DISABLE | SCR_FIQ_DISABLE | SCR_EA_DISABLE; |
| 79 | /* Enable HVC */ |
| 80 | scr &= ~(SCR_HVC_MASK); |
| 81 | scr |= SCR_HVC_ENABLE; |
| 82 | /* Disable SMC */ |
| 83 | scr &= ~(SCR_SMC_MASK); |
| 84 | scr |= SCR_SMC_DISABLE; |
| 85 | /* Disable secure instruction fetches. */ |
| 86 | scr &= ~(SCR_SIF_MASK); |
| 87 | scr |= SCR_SIF_DISABLE; |
| 88 | /* All lower exception levels 64-bit by default. */ |
| 89 | scr &= ~(SCR_RW_MASK); |
| 90 | scr |= SCR_LOWER_AARCH64; |
| 91 | /* Disable secure EL1 access to secure timer. */ |
| 92 | scr &= ~(SCR_ST_MASK); |
| 93 | scr |= SCR_ST_DISABLE; |
| 94 | /* Don't trap on WFE or WFI instructions. */ |
| 95 | scr &= ~(SCR_TWI_MASK | SCR_TWE_MASK); |
| 96 | scr |= SCR_TWI_DISABLE | SCR_TWE_DISABLE; |
| 97 | raw_write_scr_el3(scr); |
| 98 | isb(); |
| 99 | } |
| 100 | |
| 101 | static void init_this_cpu(void *arg) |
| 102 | { |
| 103 | struct cpu_info *ci = arg; |
| 104 | device_t dev = ci->cpu; |
| 105 | |
| 106 | cpu_set_device_operations(dev); |
| 107 | |
Aaron Durbin | 03c657f | 2014-10-28 17:01:28 -0500 | [diff] [blame^] | 108 | printk(BIOS_DEBUG, "CPU%x: MPIDR: %llx\n", ci->id, ci->mpidr); |
| 109 | |
Aaron Durbin | b30c9b1 | 2014-09-18 11:52:16 -0500 | [diff] [blame] | 110 | el3_init(); |
| 111 | |
| 112 | /* Initialize the GIC. */ |
| 113 | gic_init(); |
| 114 | |
| 115 | if (dev->ops != NULL && dev->ops->init != NULL) { |
| 116 | dev->initialized = 1; |
| 117 | printk(BIOS_DEBUG, "%s init\n", dev_path(dev)); |
| 118 | dev->ops->init(dev); |
| 119 | } |
| 120 | } |
| 121 | |
| 122 | /* Fill in cpu_info structures according to device tree. */ |
| 123 | static void init_cpu_info(struct bus *bus) |
| 124 | { |
| 125 | device_t cur; |
| 126 | |
| 127 | for (cur = bus->children; cur != NULL; cur = cur->sibling) { |
| 128 | struct cpu_info *ci; |
| 129 | unsigned int id = cur->path.cpu.id; |
| 130 | |
| 131 | if (cur->path.type != DEVICE_PATH_CPU) |
| 132 | continue; |
| 133 | |
| 134 | /* IDs are currently mapped 1:1 with logical CPU numbers. */ |
| 135 | if (id >= CONFIG_MAX_CPUS) { |
| 136 | printk(BIOS_WARNING, |
| 137 | "CPU id %x too large. Disabling.\n", id); |
| 138 | cpu_disable_dev(cur); |
| 139 | continue; |
| 140 | } |
| 141 | |
| 142 | ci = cpu_info_for_cpu(id); |
| 143 | if (ci->cpu != NULL) { |
| 144 | printk(BIOS_WARNING, |
| 145 | "Duplicate ID %x in device tree.\n", id); |
| 146 | cpu_disable_dev(cur); |
| 147 | } |
| 148 | |
| 149 | ci->cpu = cur; |
| 150 | ci->id = cur->path.cpu.id; |
| 151 | } |
| 152 | |
| 153 | /* Mark current cpu online. */ |
| 154 | cpu_mark_online(cpu_info()); |
| 155 | } |
| 156 | |
| 157 | void arch_initialize_cpus(device_t cluster, struct cpu_control_ops *cntrl_ops) |
| 158 | { |
| 159 | size_t max_cpus; |
| 160 | size_t i; |
| 161 | struct cpu_info *ci; |
| 162 | void (*entry)(void); |
| 163 | struct bus *bus; |
| 164 | |
| 165 | if (cluster->path.type != DEVICE_PATH_CPU_CLUSTER) { |
| 166 | printk(BIOS_ERR, |
| 167 | "CPU init failed. Device is not a CPU_CLUSTER: %s\n", |
| 168 | dev_path(cluster)); |
| 169 | return; |
| 170 | } |
| 171 | |
| 172 | bus = cluster->link_list; |
| 173 | |
| 174 | /* Check if no children under this device. */ |
| 175 | if (bus == NULL) |
| 176 | return; |
| 177 | |
| 178 | entry = prepare_secondary_cpu_startup(); |
| 179 | |
| 180 | /* Initialize the cpu_info structures. */ |
| 181 | init_cpu_info(bus); |
| 182 | max_cpus = cntrl_ops->total_cpus(); |
| 183 | |
| 184 | if (max_cpus > CONFIG_MAX_CPUS) { |
| 185 | printk(BIOS_WARNING, |
| 186 | "max_cpus (%zu) exceeds CONFIG_MAX_CPUS (%zu).\n", |
| 187 | max_cpus, (size_t)CONFIG_MAX_CPUS); |
| 188 | max_cpus = CONFIG_MAX_CPUS; |
| 189 | } |
| 190 | |
| 191 | for (i = 0; i < max_cpus; i++) { |
| 192 | device_t dev; |
| 193 | struct cpu_action action; |
| 194 | |
| 195 | ci = cpu_info_for_cpu(i); |
| 196 | dev = ci->cpu; |
| 197 | |
| 198 | /* Disregard CPUs not in device tree. */ |
| 199 | if (dev == NULL) |
| 200 | continue; |
| 201 | |
| 202 | /* Skip disabled CPUs. */ |
| 203 | if (!dev->enabled) |
| 204 | continue; |
| 205 | |
| 206 | if (!cpu_online(ci)) { |
| 207 | /* Start the CPU. */ |
| 208 | printk(BIOS_DEBUG, "Starting CPU%x\n", ci->id); |
| 209 | if (cntrl_ops->start_cpu(ci->id, entry)) { |
| 210 | printk(BIOS_ERR, |
| 211 | "Failed to start CPU%x\n", ci->id); |
| 212 | continue; |
| 213 | } |
| 214 | /* Wait for CPU to come online. */ |
| 215 | while (!cpu_online(ci)); |
| 216 | printk(BIOS_DEBUG, "CPU%x online.\n", ci->id); |
| 217 | } |
| 218 | |
| 219 | /* Send it the init action. */ |
| 220 | action.run = init_this_cpu; |
| 221 | action.arg = ci; |
| 222 | arch_run_on_cpu(ci->id, &action); |
| 223 | } |
| 224 | } |
| 225 | |
| 226 | void arch_secondary_cpu_init(void) |
| 227 | { |
| 228 | /* Mark this CPU online. */ |
| 229 | cpu_mark_online(cpu_info()); |
| 230 | |
| 231 | arch_cpu_wait_for_action(); |
| 232 | } |