blob: 0f9e874a3fb40a0715b3ab580e4e9f5bd9c8fe6c [file] [log] [blame]
Furquan Shaikh2af76f42014-04-28 16:39:40 -07001/*
2 * This file is part of the coreboot project.
3 *
4 * Copyright 2013 Google Inc.
5 *
Aaron Durbin0b0a1e32014-09-06 01:28:54 -05006 * 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.
Furquan Shaikh2af76f42014-04-28 16:39:40 -07009 *
Aaron Durbin0b0a1e32014-09-06 01:28:54 -050010 * 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.
Furquan Shaikh2af76f42014-04-28 16:39:40 -070014 *
Aaron Durbin0b0a1e32014-09-06 01:28:54 -050015 * 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
Furquan Shaikh2af76f42014-04-28 16:39:40 -070018 */
Aaron Durbin0b0a1e32014-09-06 01:28:54 -050019
Aaron Durbin9fd4dc72014-09-06 02:31:30 -050020#include <stdint.h>
Furquan Shaikh2af76f42014-04-28 16:39:40 -070021#include <stdlib.h>
Aaron Durbin9fd4dc72014-09-06 02:31:30 -050022#include <arch/barrier.h>
23#include <arch/lib_helpers.h>
24#include <cpu/cpu.h>
25#include <console/console.h>
Aaron Durbinb9b8ebc2014-09-11 21:57:41 -050026#include <gic.h>
Aaron Durbin9fd4dc72014-09-06 02:31:30 -050027#include "cpu-internal.h"
Furquan Shaikh2af76f42014-04-28 16:39:40 -070028
Aaron Durbin1b315d02014-08-27 10:30:39 -050029static struct cpu_info cpu_infos[CONFIG_MAX_CPUS];
30
Aaron Durbin9fd4dc72014-09-06 02:31:30 -050031static inline struct cpu_info *cpu_info_for_cpu(unsigned int id)
32{
33 return &cpu_infos[id];
34}
35
Furquan Shaikh2af76f42014-04-28 16:39:40 -070036struct cpu_info *cpu_info(void)
37{
Aaron Durbin9fd4dc72014-09-06 02:31:30 -050038 return cpu_info_for_cpu(smp_processor_id());
39}
40
41static int cpu_online(struct cpu_info *ci)
42{
43 return load_acquire(&ci->online) != 0;
44}
45
46static void cpu_mark_online(struct cpu_info *ci)
47{
48 store_release(&ci->online, 1);
49}
50
51static inline void cpu_disable_dev(device_t dev)
52{
53 dev->enabled = 0;
54}
55
56static struct cpu_driver *locate_cpu_driver(uint32_t midr)
57{
58 struct cpu_driver *cur;
59
60 for (cur = cpu_drivers; cur != ecpu_drivers; cur++) {
61 const struct cpu_device_id *id_table = cur->id_table;
62
63 for (; id_table->midr != CPU_ID_END; id_table++) {
64 if (id_table->midr == midr)
65 return cur;
66 }
67 }
68 return NULL;
69}
70
71static int cpu_set_device_operations(device_t dev)
72{
73 uint32_t midr;
74 struct cpu_driver *driver;
75
76 midr = raw_read_midr_el1();
77 driver = locate_cpu_driver(midr);
78
79 if (driver == NULL) {
80 printk(BIOS_WARNING, "No CPU driver for MIDR %08x\n", midr);
81 return -1;
82 }
83 dev->ops = driver->ops;
84 return 0;
85}
86
87static void init_this_cpu(void *arg)
88{
89 struct cpu_info *ci = arg;
90 device_t dev = ci->cpu;
91
92 cpu_set_device_operations(dev);
93
Aaron Durbinb9b8ebc2014-09-11 21:57:41 -050094 /* Initialize the GIC. */
95 gic_init();
96
Aaron Durbin9fd4dc72014-09-06 02:31:30 -050097 if (dev->ops != NULL && dev->ops->init != NULL) {
98 dev->initialized = 1;
99 printk(BIOS_DEBUG, "%s init\n", dev_path(dev));
100 dev->ops->init(dev);
101 }
102}
103
104/* Fill in cpu_info structures according to device tree. */
105static void init_cpu_info(struct bus *bus)
106{
107 device_t cur;
108
109 for (cur = bus->children; cur != NULL; cur = cur->sibling) {
110 struct cpu_info *ci;
111 unsigned int id = cur->path.cpu.id;
112
113 if (cur->path.type != DEVICE_PATH_CPU)
114 continue;
115
116 /* IDs are currently mapped 1:1 with logical CPU numbers. */
117 if (id >= CONFIG_MAX_CPUS) {
118 printk(BIOS_WARNING,
119 "CPU id %x too large. Disabling.\n", id);
120 cpu_disable_dev(cur);
121 continue;
122 }
123
124 ci = cpu_info_for_cpu(id);
125 if (ci->cpu != NULL) {
126 printk(BIOS_WARNING,
127 "Duplicate ID %x in device tree.\n", id);
128 cpu_disable_dev(cur);
129 }
130
131 ci->cpu = cur;
132 ci->id = cur->path.cpu.id;
133 }
134
135 /* Mark current cpu online. */
136 cpu_mark_online(cpu_info());
137}
138
139static inline int action_queue_empty(struct cpu_action_queue *q)
140{
141 return load_acquire_exclusive(&q->todo) == NULL;
142}
143
144static inline int action_completed(struct cpu_action_queue *q,
145 struct cpu_action *action)
146{
147 return load_acquire(&q->completed) == action;
148}
149
150static inline void wait_for_action_queue_slot(struct cpu_action_queue *q)
151{
152 while (!action_queue_empty(q))
153 wfe();
154}
155
156static void wait_for_action_complete(struct cpu_action_queue *q,
157 struct cpu_action *a)
158{
159 while (!action_completed(q, a))
160 wfe();
161}
162
163static struct cpu_action *wait_for_action(struct cpu_action_queue *q,
164 struct cpu_action *local)
165{
166 struct cpu_action *action;
167
168 while (action_queue_empty(q))
169 wfe();
170
171 /*
172 * Keep original address, but use a local copy for async processing.
173 */
174 do {
175 action = load_acquire_exclusive(&q->todo);
176 *local = *action;
177 } while (!store_release_exclusive(&q->todo, NULL));
178
179 return action;
180}
181
182static void queue_action(struct cpu_action_queue *q, struct cpu_action *action)
183{
184 do {
185 wait_for_action_queue_slot(q);
186 if (load_acquire_exclusive(&q->todo) != NULL)
187 continue;
188 } while (!store_release_exclusive(&q->todo, action));
189}
190
191static void action_queue_complete(struct cpu_action_queue *q,
192 struct cpu_action *action)
193{
194 /* Mark completion and send events to waiters. */
195 store_release(&q->completed, action);
196 sev();
197}
198
199static void action_run(struct cpu_action *action)
200{
201 action->run(action->arg);
202}
203
204static void action_run_on_cpu(struct cpu_info *ci, struct cpu_action *action,
205 int sync)
206{
207 struct cpu_action_queue *q = &ci->action_queue;
208
209 /* Don't run actions on non-online or enabled devices. */
210 if (!cpu_online(ci) || ci->cpu == NULL || !ci->cpu->enabled)
211 return;
212
213 if (ci->id == smp_processor_id()) {
214 action->run(action->arg);
215 return;
216 }
217
218 queue_action(q, action);
219 /* Wait for CPU to pick it up. Empty slot means it was picked up. */
220 wait_for_action_queue_slot(q);
221 /* Wait for completion if requested. */
222 if (sync)
223 wait_for_action_complete(q, action);
224}
225
226static int __arch_run_on_cpu(unsigned int cpu, struct cpu_action *action,
227 int sync)
228{
229 struct cpu_info *ci;
230
231 if (cpu >= CONFIG_MAX_CPUS)
232 return -1;
233
234 ci = cpu_info_for_cpu(cpu);
235
236 action_run_on_cpu(ci, action, sync);
237
238 return 0;
239}
240
241int arch_run_on_cpu(unsigned int cpu, struct cpu_action *action)
242{
243 return __arch_run_on_cpu(cpu, action, 1);
244}
245
246int arch_run_on_cpu_async(unsigned int cpu, struct cpu_action *action)
247{
248 return __arch_run_on_cpu(cpu, action, 0);
249}
250
251static int __arch_run_on_all_cpus(struct cpu_action *action, int sync)
252{
253 int i;
254
255 for (i = 0; i < CONFIG_MAX_CPUS; i++)
256 action_run_on_cpu(cpu_info_for_cpu(i), action, sync);
257
258 return 0;
259}
260
261int arch_run_on_all_cpus(struct cpu_action *action)
262{
263 return __arch_run_on_all_cpus(action, 1);
264}
265
266int arch_run_on_all_cpus_async(struct cpu_action *action)
267{
268 return __arch_run_on_all_cpus(action, 0);
269}
270
271void arch_secondary_cpu_init(void)
272{
273 struct cpu_info *ci = cpu_info();
274 struct cpu_action_queue *q = &ci->action_queue;
275
276 /* Mark this CPU online. */
277 cpu_mark_online(ci);
278
279 while (1) {
280 struct cpu_action *orig;
281 struct cpu_action action;
282
283 orig = wait_for_action(q, &action);
284
285 action_run(&action);
286 action_queue_complete(q, orig);
287 }
288}
289
290void arch_initialize_cpus(device_t cluster, struct cpu_control_ops *cntrl_ops)
291{
292 size_t max_cpus;
293 size_t i;
294 struct cpu_info *ci;
295 void (*entry)(void);
296 struct bus *bus;
297
298 if (cluster->path.type != DEVICE_PATH_CPU_CLUSTER) {
299 printk(BIOS_ERR,
300 "CPU init failed. Device is not a CPU_CLUSTER: %s\n",
301 dev_path(cluster));
302 return;
303 }
304
305 bus = cluster->link_list;
Aaron Durbin0ea07042014-09-11 16:03:17 -0500306
307 /* Check if no children under this device. */
308 if (bus == NULL)
309 return;
310
Aaron Durbin9fd4dc72014-09-06 02:31:30 -0500311 entry = prepare_secondary_cpu_startup();
312
313 /* Initialize the cpu_info structures. */
314 init_cpu_info(bus);
315 max_cpus = cntrl_ops->total_cpus();
316
317 if (max_cpus > CONFIG_MAX_CPUS) {
318 printk(BIOS_WARNING,
319 "max_cpus (%zu) exceeds CONFIG_MAX_CPUS (%zu).\n",
320 max_cpus, (size_t)CONFIG_MAX_CPUS);
321 max_cpus = CONFIG_MAX_CPUS;
322 }
323
324 for (i = 0; i < max_cpus; i++) {
325 device_t dev;
326 struct cpu_action action;
327
328 ci = cpu_info_for_cpu(i);
329 dev = ci->cpu;
330
331 /* Disregard CPUs not in device tree. */
332 if (dev == NULL)
333 continue;
334
335 /* Skip disabled CPUs. */
336 if (!dev->enabled)
337 continue;
338
339 if (!cpu_online(ci)) {
340 /* Start the CPU. */
341 printk(BIOS_DEBUG, "Starting CPU%x\n", ci->id);
342 if (cntrl_ops->start_cpu(ci->id, entry)) {
343 printk(BIOS_ERR,
344 "Failed to start CPU%x\n", ci->id);
345 continue;
346 }
347 /* Wait for CPU to come online. */
348 while (!cpu_online(ci));
349 printk(BIOS_DEBUG, "CPU%x online.\n", ci->id);
350 }
351
352 /* Send it the init action. */
353 action.run = init_this_cpu;
354 action.arg = ci;
355 action_run_on_cpu(ci, &action, 1);
356 }
Furquan Shaikh2af76f42014-04-28 16:39:40 -0700357}