summaryrefslogtreecommitdiff
path: root/src/arch/arm64/cpu.c
blob: a263e1b873e2eadc79314a6bea17bbd268f587da (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
/*
 * This file is part of the coreboot project.
 *
 * Copyright 2013 Google Inc.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; version 2 of the License.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA, 02110-1301 USA
 */

#include <stdint.h>
#include <stdlib.h>
#include <arch/barrier.h>
#include <arch/lib_helpers.h>
#include <cpu/cpu.h>
#include <console/console.h>
#include <gic.h>
#include "cpu-internal.h"

static struct cpu_info cpu_infos[CONFIG_MAX_CPUS];
struct cpu_info *bsp_cpu_info;

static inline struct cpu_info *cpu_info_for_cpu(unsigned int id)
{
	return &cpu_infos[id];
}

struct cpu_info *cpu_info(void)
{
	return cpu_info_for_cpu(smp_processor_id());
}

static int cpu_online(struct cpu_info *ci)
{
	return load_acquire(&ci->online) != 0;
}

static void cpu_mark_online(struct cpu_info *ci)
{
	store_release(&ci->online, 1);
}

static inline void cpu_disable_dev(device_t dev)
{
	dev->enabled = 0;
}

static struct cpu_driver *locate_cpu_driver(uint32_t midr)
{
	struct cpu_driver *cur;

	for (cur = cpu_drivers; cur != ecpu_drivers; cur++) {
		const struct cpu_device_id *id_table = cur->id_table;

		for (; id_table->midr != CPU_ID_END; id_table++) {
			if (id_table->midr == midr)
				return cur;
		}
	}
	return NULL;
}

static int cpu_set_device_operations(device_t dev)
{
	uint32_t midr;
	struct cpu_driver *driver;

	midr = raw_read_midr_el1();
	driver = locate_cpu_driver(midr);

	if (driver == NULL) {
		printk(BIOS_WARNING, "No CPU driver for MIDR %08x\n", midr);
		return -1;
	}
	dev->ops = driver->ops;
	return 0;
}

/* Set up default SCR values. */
static void el3_init(void)
{
	uint32_t scr;

	if (get_current_el() != EL3)
		return;

	scr = raw_read_scr_el3();
	/* Default to non-secure EL1 and EL0. */
	scr &= ~(SCR_NS_MASK);
	scr |= SCR_NS_ENABLE;
	/* Disable IRQ, FIQ, and external abort interrupt routing. */
	scr &= ~(SCR_IRQ_MASK | SCR_FIQ_MASK | SCR_EA_MASK);
	scr |= SCR_IRQ_DISABLE | SCR_FIQ_DISABLE | SCR_EA_DISABLE;
	/* Enable HVC */
	scr &= ~(SCR_HVC_MASK);
	scr |= SCR_HVC_ENABLE;
	/* Disable SMC */
	scr &= ~(SCR_SMC_MASK);
	scr |= SCR_SMC_DISABLE;
	/* Disable secure instruction fetches. */
	scr &= ~(SCR_SIF_MASK);
	scr |= SCR_SIF_DISABLE;
	/* All lower exception levels 64-bit by default. */
	scr &= ~(SCR_RW_MASK);
	scr |= SCR_LOWER_AARCH64;
	/* Disable secure EL1 access to secure timer. */
	scr &= ~(SCR_ST_MASK);
	scr |= SCR_ST_DISABLE;
	/* Don't trap on WFE or WFI instructions. */
	scr &= ~(SCR_TWI_MASK | SCR_TWE_MASK);
	scr |= SCR_TWI_DISABLE | SCR_TWE_DISABLE;
	raw_write_scr_el3(scr);
	isb();
}

static void init_this_cpu(void *arg)
{
	struct cpu_info *ci = arg;
	device_t dev = ci->cpu;

	cpu_set_device_operations(dev);

	el3_init();

	/* Initialize the GIC. */
	gic_init();

	if (dev->ops != NULL && dev->ops->init != NULL) {
		dev->initialized = 1;
		printk(BIOS_DEBUG, "%s init\n", dev_path(dev));
		dev->ops->init(dev);
	}
}

/* Fill in cpu_info structures according to device tree. */
static void init_cpu_info(struct bus *bus)
{
	device_t cur;

	for (cur = bus->children; cur != NULL; cur = cur->sibling) {
		struct cpu_info *ci;
		unsigned int id = cur->path.cpu.id;

		if (cur->path.type != DEVICE_PATH_CPU)
			continue;

		/* IDs are currently mapped 1:1 with logical CPU numbers. */
		if (id >= CONFIG_MAX_CPUS) {
			printk(BIOS_WARNING,
				"CPU id %x too large. Disabling.\n", id);
			cpu_disable_dev(cur);
			continue;
		}

		ci = cpu_info_for_cpu(id);
		if (ci->cpu != NULL) {
			printk(BIOS_WARNING,
				"Duplicate ID %x in device tree.\n", id);
			cpu_disable_dev(cur);
		}

		ci->cpu = cur;
		ci->id = cur->path.cpu.id;
	}

	/* Mark current cpu online. */
	cpu_mark_online(cpu_info());
}

static inline int action_queue_empty(struct cpu_action_queue *q)
{
	return load_acquire_exclusive(&q->todo) == NULL;
}

static inline int action_completed(struct cpu_action_queue *q,
					struct cpu_action *action)
{
	return load_acquire(&q->completed) == action;
}

static inline void wait_for_action_queue_slot(struct cpu_action_queue *q)
{
	while (!action_queue_empty(q))
		wfe();
}

static void wait_for_action_complete(struct cpu_action_queue *q,
					struct cpu_action *a)
{
	while (!action_completed(q, a))
		wfe();
}

static struct cpu_action *wait_for_action(struct cpu_action_queue *q,
						struct cpu_action *local)
{
	struct cpu_action *action;

	while (action_queue_empty(q))
		wfe();

	/*
	 * Keep original address, but use a local copy for async processing.
	 */
	do {
		action = load_acquire_exclusive(&q->todo);
		*local = *action;
	} while (!store_release_exclusive(&q->todo, NULL));

	return action;
}

static void queue_action(struct cpu_action_queue *q, struct cpu_action *action)
{
	do {
		wait_for_action_queue_slot(q);
		if (load_acquire_exclusive(&q->todo) != NULL)
			continue;
	} while (!store_release_exclusive(&q->todo, action));
}

static void action_queue_complete(struct cpu_action_queue *q,
					struct cpu_action *action)
{
	/* Mark completion and send events to waiters. */
	store_release(&q->completed, action);
	sev();
}

static void action_run(struct cpu_action *action)
{
	action->run(action->arg);
}

static void action_run_on_cpu(struct cpu_info *ci, struct cpu_action *action,
				int sync)
{
	struct cpu_action_queue *q = &ci->action_queue;

	/* Don't run actions on non-online or enabled devices. */
	if (!cpu_online(ci) || ci->cpu == NULL || !ci->cpu->enabled)
		return;

	if (ci->id == smp_processor_id()) {
		action->run(action->arg);
		return;
	}

	queue_action(q, action);
	/* Wait for CPU to pick it up. Empty slot means it was picked up. */
	wait_for_action_queue_slot(q);
	/* Wait for completion if requested. */
	if (sync)
		wait_for_action_complete(q, action);
}

static int __arch_run_on_cpu(unsigned int cpu, struct cpu_action *action,
				int sync)
{
	struct cpu_info *ci;

	if (cpu >= CONFIG_MAX_CPUS)
		return -1;

	ci = cpu_info_for_cpu(cpu);

	action_run_on_cpu(ci, action, sync);

	return 0;
}

int arch_run_on_cpu(unsigned int cpu, struct cpu_action *action)
{
	return __arch_run_on_cpu(cpu, action, 1);
}

int arch_run_on_cpu_async(unsigned int cpu, struct cpu_action *action)
{
	return __arch_run_on_cpu(cpu, action, 0);
}

static int __arch_run_on_all_cpus(struct cpu_action *action, int sync)
{
	int i;

	for (i = 0; i < CONFIG_MAX_CPUS; i++)
		action_run_on_cpu(cpu_info_for_cpu(i), action, sync);

	return 0;
}

static int __arch_run_on_all_cpus_but_self(struct cpu_action *action, int sync)
{
	int i;
	struct cpu_info *me = cpu_info();

	for (i = 0; i < CONFIG_MAX_CPUS; i++) {
		struct cpu_info *ci = cpu_info_for_cpu(i);
		if (ci == me)
			continue;
		action_run_on_cpu(ci, action, sync);
	}

	return 0;
}

int arch_run_on_all_cpus(struct cpu_action *action)
{
	return __arch_run_on_all_cpus(action, 1);
}

int arch_run_on_all_cpus_async(struct cpu_action *action)
{
	return __arch_run_on_all_cpus(action, 0);
}

int arch_run_on_all_cpus_but_self(struct cpu_action *action)
{
	return __arch_run_on_all_cpus_but_self(action, 1);
}

int arch_run_on_all_cpus_but_self_async(struct cpu_action *action)
{
	return __arch_run_on_all_cpus_but_self(action, 0);
}

void arch_secondary_cpu_init(void)
{
	struct cpu_info *ci = cpu_info();
	struct cpu_action_queue *q = &ci->action_queue;

	/* Mark this CPU online. */
	cpu_mark_online(ci);

	while (1) {
		struct cpu_action *orig;
		struct cpu_action action;

		orig = wait_for_action(q, &action);

		action_run(&action);
		action_queue_complete(q, orig);
	}
}

void arch_initialize_cpus(device_t cluster, struct cpu_control_ops *cntrl_ops)
{
	size_t max_cpus;
	size_t i;
	struct cpu_info *ci;
	void (*entry)(void);
	struct bus *bus;

	if (cluster->path.type != DEVICE_PATH_CPU_CLUSTER) {
		printk(BIOS_ERR,
			"CPU init failed. Device is not a CPU_CLUSTER: %s\n",
			dev_path(cluster));
		return;
	}

	bus = cluster->link_list;

	/* Check if no children under this device. */
	if (bus == NULL)
		return;

	entry = prepare_secondary_cpu_startup();

	/* Initialize the cpu_info structures. */
	init_cpu_info(bus);
	max_cpus = cntrl_ops->total_cpus();

	if (max_cpus > CONFIG_MAX_CPUS) {
		printk(BIOS_WARNING,
			"max_cpus (%zu) exceeds CONFIG_MAX_CPUS (%zu).\n",
			max_cpus, (size_t)CONFIG_MAX_CPUS);
		max_cpus = CONFIG_MAX_CPUS;
	}

	for (i = 0; i < max_cpus; i++) {
		device_t dev;
		struct cpu_action action;

		ci = cpu_info_for_cpu(i);
		dev = ci->cpu;

		/* Disregard CPUs not in device tree. */
		if (dev == NULL)
			continue;

		/* Skip disabled CPUs. */
		if (!dev->enabled)
			continue;

		if (!cpu_online(ci)) {
			/* Start the CPU. */
			printk(BIOS_DEBUG, "Starting CPU%x\n", ci->id);
			if (cntrl_ops->start_cpu(ci->id, entry)) {
				printk(BIOS_ERR,
					"Failed to start CPU%x\n", ci->id);
				continue;
			}
			/* Wait for CPU to come online. */
			while (!cpu_online(ci));
			printk(BIOS_DEBUG, "CPU%x online.\n", ci->id);
		}

		/* Send it the init action. */
		action.run = init_this_cpu;
		action.arg = ci;
		action_run_on_cpu(ci, &action, 1);
	}
}