blob: 0f26206683b1542f174614dad7ed5c61cc768608 [file] [log] [blame]
Aaron Durbinb30c9b12014-09-18 11:52:16 -05001/*
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
28static inline void cpu_disable_dev(device_t dev)
29{
30 dev->enabled = 0;
31}
32
33static 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
48static 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. */
65static 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
101static 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
108 el3_init();
109
110 /* Initialize the GIC. */
111 gic_init();
112
113 if (dev->ops != NULL && dev->ops->init != NULL) {
114 dev->initialized = 1;
115 printk(BIOS_DEBUG, "%s init\n", dev_path(dev));
116 dev->ops->init(dev);
117 }
118}
119
120/* Fill in cpu_info structures according to device tree. */
121static void init_cpu_info(struct bus *bus)
122{
123 device_t cur;
124
125 for (cur = bus->children; cur != NULL; cur = cur->sibling) {
126 struct cpu_info *ci;
127 unsigned int id = cur->path.cpu.id;
128
129 if (cur->path.type != DEVICE_PATH_CPU)
130 continue;
131
132 /* IDs are currently mapped 1:1 with logical CPU numbers. */
133 if (id >= CONFIG_MAX_CPUS) {
134 printk(BIOS_WARNING,
135 "CPU id %x too large. Disabling.\n", id);
136 cpu_disable_dev(cur);
137 continue;
138 }
139
140 ci = cpu_info_for_cpu(id);
141 if (ci->cpu != NULL) {
142 printk(BIOS_WARNING,
143 "Duplicate ID %x in device tree.\n", id);
144 cpu_disable_dev(cur);
145 }
146
147 ci->cpu = cur;
148 ci->id = cur->path.cpu.id;
149 }
150
151 /* Mark current cpu online. */
152 cpu_mark_online(cpu_info());
153}
154
155void arch_initialize_cpus(device_t cluster, struct cpu_control_ops *cntrl_ops)
156{
157 size_t max_cpus;
158 size_t i;
159 struct cpu_info *ci;
160 void (*entry)(void);
161 struct bus *bus;
162
163 if (cluster->path.type != DEVICE_PATH_CPU_CLUSTER) {
164 printk(BIOS_ERR,
165 "CPU init failed. Device is not a CPU_CLUSTER: %s\n",
166 dev_path(cluster));
167 return;
168 }
169
170 bus = cluster->link_list;
171
172 /* Check if no children under this device. */
173 if (bus == NULL)
174 return;
175
176 entry = prepare_secondary_cpu_startup();
177
178 /* Initialize the cpu_info structures. */
179 init_cpu_info(bus);
180 max_cpus = cntrl_ops->total_cpus();
181
182 if (max_cpus > CONFIG_MAX_CPUS) {
183 printk(BIOS_WARNING,
184 "max_cpus (%zu) exceeds CONFIG_MAX_CPUS (%zu).\n",
185 max_cpus, (size_t)CONFIG_MAX_CPUS);
186 max_cpus = CONFIG_MAX_CPUS;
187 }
188
189 for (i = 0; i < max_cpus; i++) {
190 device_t dev;
191 struct cpu_action action;
192
193 ci = cpu_info_for_cpu(i);
194 dev = ci->cpu;
195
196 /* Disregard CPUs not in device tree. */
197 if (dev == NULL)
198 continue;
199
200 /* Skip disabled CPUs. */
201 if (!dev->enabled)
202 continue;
203
204 if (!cpu_online(ci)) {
205 /* Start the CPU. */
206 printk(BIOS_DEBUG, "Starting CPU%x\n", ci->id);
207 if (cntrl_ops->start_cpu(ci->id, entry)) {
208 printk(BIOS_ERR,
209 "Failed to start CPU%x\n", ci->id);
210 continue;
211 }
212 /* Wait for CPU to come online. */
213 while (!cpu_online(ci));
214 printk(BIOS_DEBUG, "CPU%x online.\n", ci->id);
215 }
216
217 /* Send it the init action. */
218 action.run = init_this_cpu;
219 action.arg = ci;
220 arch_run_on_cpu(ci->id, &action);
221 }
222}
223
224void arch_secondary_cpu_init(void)
225{
226 /* Mark this CPU online. */
227 cpu_mark_online(cpu_info());
228
229 arch_cpu_wait_for_action();
230}