blob: bf635173848412ce10766f386c4becf3f52d8b2a [file] [log] [blame]
Yinghai Lu72ee9b02005-12-14 02:39:33 +00001/*
Stefan Reinauer1bfbbc02012-06-07 14:00:07 -07002 * This file is part of the coreboot project.
3 *
4 * Copyright (C) 2001 Eric Biederman
5 * Copyright (C) 2001 Ronald G. Minnich
6 * Copyright (C) 2005 Yinghai Lu
7 * Copyright (C) 2008 coresystems GmbH
Timothy Pearson6b6c6532015-09-05 19:31:20 -05008 * Copyright (C) 2015 Timothy Pearson <tpearson@raptorengineeringinc.com>, Raptor Engineering
Stefan Reinauer1bfbbc02012-06-07 14:00:07 -07009 *
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; version 2 of the License.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
Stefan Reinauer1bfbbc02012-06-07 14:00:07 -070018 */
Yinghai Lu72ee9b02005-12-14 02:39:33 +000019
Aaron Durbin029aaf62013-10-10 12:41:49 -050020#include <cpu/x86/cr.h>
Kyösti Mälkkibae775a2014-12-18 10:36:33 +020021#include <cpu/x86/gdt.h>
Eric Biedermanfcd5ace2004-10-14 19:29:29 +000022#include <cpu/x86/lapic.h>
23#include <delay.h>
Patrick Georgibd79c5e2014-11-28 22:35:36 +010024#include <halt.h>
Stefan Reinauer75dbc382012-10-15 15:19:43 -070025#include <lib.h>
Eric Biedermanfcd5ace2004-10-14 19:29:29 +000026#include <string.h>
Julius Wernerec5e5e02014-08-20 15:29:56 -070027#include <symbols.h>
Eric Biedermanfcd5ace2004-10-14 19:29:29 +000028#include <console/console.h>
Eric Biedermanfcd5ace2004-10-14 19:29:29 +000029#include <device/device.h>
30#include <device/path.h>
31#include <smp/atomic.h>
32#include <smp/spinlock.h>
33#include <cpu/cpu.h>
Stefan Reinauer2bdfb482012-04-03 16:17:11 -070034#include <cpu/intel/speedstep.h>
Aaron Durbin38c326d2013-05-06 12:22:23 -050035#include <thread.h>
Eric Biedermanfcd5ace2004-10-14 19:29:29 +000036
Ronald G. Minnich000bf83c2012-06-06 13:00:24 -070037#if CONFIG_SMP && CONFIG_MAX_CPUS > 1
Eric Biedermanfcd5ace2004-10-14 19:29:29 +000038/* This is a lot more paranoid now, since Linux can NOT handle
Zheng Baod4c5c442010-02-20 09:38:16 +000039 * being told there is a CPU when none exists. So any errors
40 * will return 0, meaning no CPU.
Eric Biedermanfcd5ace2004-10-14 19:29:29 +000041 *
42 * We actually handling that case by noting which cpus startup
Martin Roth4c3ab732013-07-08 16:23:54 -060043 * and not telling anyone about the ones that don't.
Zheng Baod4c5c442010-02-20 09:38:16 +000044 */
Kyösti Mälkki63539bb2012-07-05 06:31:15 +030045
46/* Start-UP IPI vector must be 4kB aligned and below 1MB. */
47#define AP_SIPI_VECTOR 0x1000
Yinghai Lu72ee9b02005-12-14 02:39:33 +000048
Patrick Georgie1667822012-05-05 15:29:32 +020049#if CONFIG_HAVE_ACPI_RESUME
Rudolf Mareka572f832009-04-13 17:57:44 +000050char *lowmem_backup;
51char *lowmem_backup_ptr;
52int lowmem_backup_size;
53#endif
54
Aaron Durbina146d582013-02-08 16:56:51 -060055static inline void setup_secondary_gdt(void)
56{
57 u16 *gdt_limit;
Stefan Reinauere021dea2015-06-18 01:15:27 -070058#ifdef __x86_64__
59 u64 *gdt_base;
60#else
Aaron Durbina146d582013-02-08 16:56:51 -060061 u32 *gdt_base;
Stefan Reinauere021dea2015-06-18 01:15:27 -070062#endif
Aaron Durbina146d582013-02-08 16:56:51 -060063
64 gdt_limit = (void *)&_secondary_gdt_addr;
65 gdt_base = (void *)&gdt_limit[1];
66
Stefan Reinauere021dea2015-06-18 01:15:27 -070067 *gdt_limit = (uintptr_t)&gdt_end - (uintptr_t)&gdt - 1;
68 *gdt_base = (uintptr_t)&gdt;
Aaron Durbina146d582013-02-08 16:56:51 -060069}
Myles Watson6e235762009-09-29 14:56:15 +000070
Stefan Reinauer1bfbbc02012-06-07 14:00:07 -070071static void copy_secondary_start_to_lowest_1M(void)
Yinghai Lu72ee9b02005-12-14 02:39:33 +000072{
Zheng Baod4c5c442010-02-20 09:38:16 +000073 unsigned long code_size;
Yinghai Lu72ee9b02005-12-14 02:39:33 +000074
Aaron Durbina146d582013-02-08 16:56:51 -060075 /* Fill in secondary_start's local gdt. */
76 setup_secondary_gdt();
77
Zheng Baod4c5c442010-02-20 09:38:16 +000078 code_size = (unsigned long)_secondary_start_end - (unsigned long)_secondary_start;
Yinghai Lu72ee9b02005-12-14 02:39:33 +000079
Patrick Georgie1667822012-05-05 15:29:32 +020080#if CONFIG_HAVE_ACPI_RESUME
Rudolf Mareka572f832009-04-13 17:57:44 +000081 /* need to save it for RAM resume */
82 lowmem_backup_size = code_size;
83 lowmem_backup = malloc(code_size);
Kyösti Mälkki63539bb2012-07-05 06:31:15 +030084 lowmem_backup_ptr = (char *)AP_SIPI_VECTOR;
Zheng Baod4c5c442010-02-20 09:38:16 +000085
Rudolf Mareka572f832009-04-13 17:57:44 +000086 if (lowmem_backup == NULL)
87 die("Out of backup memory\n");
88
Zheng Baod4c5c442010-02-20 09:38:16 +000089 memcpy(lowmem_backup, lowmem_backup_ptr, lowmem_backup_size);
Rudolf Mareka572f832009-04-13 17:57:44 +000090#endif
Zheng Baod4c5c442010-02-20 09:38:16 +000091 /* copy the _secondary_start to the ram below 1M*/
Kyösti Mälkki63539bb2012-07-05 06:31:15 +030092 memcpy((unsigned char *)AP_SIPI_VECTOR, (unsigned char *)_secondary_start, code_size);
Yinghai Lu72ee9b02005-12-14 02:39:33 +000093
Stefan Reinauer1bfbbc02012-06-07 14:00:07 -070094 printk(BIOS_DEBUG, "start_eip=0x%08lx, code_size=0x%08lx\n",
95 (long unsigned int)AP_SIPI_VECTOR, code_size);
Yinghai Lu72ee9b02005-12-14 02:39:33 +000096}
97
Sven Schnelle51676b12012-07-29 19:18:03 +020098static int lapic_start_cpu(unsigned long apicid)
Eric Biedermanfcd5ace2004-10-14 19:29:29 +000099{
100 int timeout;
Kyösti Mälkki63539bb2012-07-05 06:31:15 +0300101 unsigned long send_status, accept_status;
Ronald G. Minnichb5e777c2013-07-22 20:17:18 +0200102 int j, maxlvt;
Zheng Baod4c5c442010-02-20 09:38:16 +0000103
Eric Biedermanfcd5ace2004-10-14 19:29:29 +0000104 /*
105 * Starting actual IPI sequence...
106 */
107
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000108 printk(BIOS_SPEW, "Asserting INIT.\n");
Eric Biedermanfcd5ace2004-10-14 19:29:29 +0000109
Sven Schnelle51676b12012-07-29 19:18:03 +0200110 /*
111 * Turn INIT on target chip
112 */
113 lapic_write_around(LAPIC_ICR2, SET_LAPIC_DEST_FIELD(apicid));
Eric Biedermanfcd5ace2004-10-14 19:29:29 +0000114
Sven Schnelle51676b12012-07-29 19:18:03 +0200115 /*
116 * Send IPI
117 */
118
119 lapic_write_around(LAPIC_ICR, LAPIC_INT_LEVELTRIG | LAPIC_INT_ASSERT
120 | LAPIC_DM_INIT);
121
122 printk(BIOS_SPEW, "Waiting for send to finish...\n");
Eric Biedermanfcd5ace2004-10-14 19:29:29 +0000123 timeout = 0;
124 do {
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000125 printk(BIOS_SPEW, "+");
Eric Biedermanfcd5ace2004-10-14 19:29:29 +0000126 udelay(100);
127 send_status = lapic_read(LAPIC_ICR) & LAPIC_ICR_BUSY;
128 } while (send_status && (timeout++ < 1000));
129 if (timeout >= 1000) {
Stefan Reinauer1bfbbc02012-06-07 14:00:07 -0700130 printk(BIOS_ERR, "CPU %ld: First APIC write timed out. "
131 "Disabling\n", apicid);
Zheng Baod4c5c442010-02-20 09:38:16 +0000132 // too bad.
Sven Schnelle51676b12012-07-29 19:18:03 +0200133 printk(BIOS_ERR, "ESR is 0x%lx\n", lapic_read(LAPIC_ESR));
Eric Biedermanfcd5ace2004-10-14 19:29:29 +0000134 if (lapic_read(LAPIC_ESR)) {
Sven Schnelle51676b12012-07-29 19:18:03 +0200135 printk(BIOS_ERR, "Try to reset ESR\n");
Eric Biedermanfcd5ace2004-10-14 19:29:29 +0000136 lapic_write_around(LAPIC_ESR, 0);
Stefan Reinauer1bfbbc02012-06-07 14:00:07 -0700137 printk(BIOS_ERR, "ESR is 0x%lx\n",
138 lapic_read(LAPIC_ESR));
Eric Biedermanfcd5ace2004-10-14 19:29:29 +0000139 }
140 return 0;
141 }
Vladimir Serbinenko533ad102013-11-14 19:23:30 +0100142#if !CONFIG_CPU_AMD_MODEL_10XXX && !CONFIG_CPU_INTEL_MODEL_206AX && !CONFIG_CPU_INTEL_MODEL_2065X
Sven Schnelle51676b12012-07-29 19:18:03 +0200143 mdelay(10);
144#endif
Yinghai Lu9a791df2006-04-03 20:38:34 +0000145
Sven Schnelle51676b12012-07-29 19:18:03 +0200146 printk(BIOS_SPEW, "Deasserting INIT.\n");
Sven Schnelle042c1462012-06-17 10:32:55 +0200147
148 /* Target chip */
Sven Schnelle51676b12012-07-29 19:18:03 +0200149 lapic_write_around(LAPIC_ICR2, SET_LAPIC_DEST_FIELD(apicid));
Sven Schnelle042c1462012-06-17 10:32:55 +0200150
Sven Schnelle51676b12012-07-29 19:18:03 +0200151 /* Send IPI */
152 lapic_write_around(LAPIC_ICR, LAPIC_INT_LEVELTRIG | LAPIC_DM_INIT);
Sven Schnelle042c1462012-06-17 10:32:55 +0200153
Sven Schnelle51676b12012-07-29 19:18:03 +0200154 printk(BIOS_SPEW, "Waiting for send to finish...\n");
Sven Schnelle042c1462012-06-17 10:32:55 +0200155 timeout = 0;
156 do {
Sven Schnelle51676b12012-07-29 19:18:03 +0200157 printk(BIOS_SPEW, "+");
Sven Schnelle042c1462012-06-17 10:32:55 +0200158 udelay(100);
159 send_status = lapic_read(LAPIC_ICR) & LAPIC_ICR_BUSY;
160 } while (send_status && (timeout++ < 1000));
Sven Schnelle51676b12012-07-29 19:18:03 +0200161 if (timeout >= 1000) {
Stefan Reinauer1bfbbc02012-06-07 14:00:07 -0700162 printk(BIOS_ERR, "CPU %ld: Second apic write timed out. "
163 "Disabling\n", apicid);
Sven Schnelle51676b12012-07-29 19:18:03 +0200164 // too bad.
165 return 0;
166 }
167
Sven Schnelle042c1462012-06-17 10:32:55 +0200168 /*
Sven Schnelle51676b12012-07-29 19:18:03 +0200169 * Run STARTUP IPI loop.
Sven Schnelle042c1462012-06-17 10:32:55 +0200170 */
Ronald G. Minnichb5e777c2013-07-22 20:17:18 +0200171 printk(BIOS_SPEW, "#startup loops: %d.\n", CONFIG_NUM_IPI_STARTS);
Sven Schnelle51676b12012-07-29 19:18:03 +0200172
173 maxlvt = 4;
174
Ronald G. Minnichb5e777c2013-07-22 20:17:18 +0200175 for (j = 1; j <= CONFIG_NUM_IPI_STARTS; j++) {
Sven Schnelle51676b12012-07-29 19:18:03 +0200176 printk(BIOS_SPEW, "Sending STARTUP #%d to %lu.\n", j, apicid);
Eric Biedermanfcd5ace2004-10-14 19:29:29 +0000177 lapic_read_around(LAPIC_SPIV);
178 lapic_write(LAPIC_ESR, 0);
Sven Schnelle51676b12012-07-29 19:18:03 +0200179 lapic_read(LAPIC_ESR);
180 printk(BIOS_SPEW, "After apic_write.\n");
Sven Schnelle042c1462012-06-17 10:32:55 +0200181
Sven Schnelle51676b12012-07-29 19:18:03 +0200182 /*
183 * STARTUP IPI
184 */
185
186 /* Target chip */
187 lapic_write_around(LAPIC_ICR2, SET_LAPIC_DEST_FIELD(apicid));
188
189 /* Boot on the stack */
190 /* Kick the second */
191 lapic_write_around(LAPIC_ICR, LAPIC_DM_STARTUP
192 | (AP_SIPI_VECTOR >> 12));
193
194 /*
195 * Give the other CPU some time to accept the IPI.
196 */
197 udelay(300);
198
199 printk(BIOS_SPEW, "Startup point 1.\n");
200
201 printk(BIOS_SPEW, "Waiting for send to finish...\n");
202 timeout = 0;
203 do {
204 printk(BIOS_SPEW, "+");
205 udelay(100);
206 send_status = lapic_read(LAPIC_ICR) & LAPIC_ICR_BUSY;
207 } while (send_status && (timeout++ < 1000));
208
209 /*
210 * Give the other CPU some time to accept the IPI.
211 */
212 udelay(200);
213 /*
214 * Due to the Pentium erratum 3AP.
215 */
216 if (maxlvt > 3) {
217 lapic_read_around(LAPIC_SPIV);
218 lapic_write(LAPIC_ESR, 0);
219 }
220 accept_status = (lapic_read(LAPIC_ESR) & 0xEF);
221 if (send_status || accept_status)
222 break;
223 }
224 printk(BIOS_SPEW, "After Startup.\n");
Eric Biedermanfcd5ace2004-10-14 19:29:29 +0000225 if (send_status)
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000226 printk(BIOS_WARNING, "APIC never delivered???\n");
Eric Biedermanfcd5ace2004-10-14 19:29:29 +0000227 if (accept_status)
Stefan Reinauer1bfbbc02012-06-07 14:00:07 -0700228 printk(BIOS_WARNING, "APIC delivery error (%lx).\n",
229 accept_status);
Eric Biedermanfcd5ace2004-10-14 19:29:29 +0000230 if (send_status || accept_status)
231 return 0;
232 return 1;
233}
234
Stefan Reinauerf8ee1802008-01-18 15:08:58 +0000235/* Number of cpus that are currently running in coreboot */
Eric Biedermanfcd5ace2004-10-14 19:29:29 +0000236static atomic_t active_cpus = ATOMIC_INIT(1);
237
Sven Schnelle51676b12012-07-29 19:18:03 +0200238/* start_cpu_lock covers last_cpu_index and secondary_stack.
239 * Only starting one cpu at a time let's me remove the logic
240 * for select the stack from assembly language.
241 *
242 * In addition communicating by variables to the cpu I
Ronald G. Minnich8b930592012-06-05 14:41:27 -0700243 * am starting allows me to verify it has started before
Sven Schnelle51676b12012-07-29 19:18:03 +0200244 * start_cpu returns.
245 */
Eric Biedermanfcd5ace2004-10-14 19:29:29 +0000246
Sven Schnelle51676b12012-07-29 19:18:03 +0200247static spinlock_t start_cpu_lock = SPIN_LOCK_UNLOCKED;
Ronald G. Minnich8b930592012-06-05 14:41:27 -0700248static unsigned int last_cpu_index = 0;
Stefan Reinauer1bfbbc02012-06-07 14:00:07 -0700249static void *stacks[CONFIG_MAX_CPUS];
Sven Schnelle51676b12012-07-29 19:18:03 +0200250volatile unsigned long secondary_stack;
Ronald G. Minnich8b930592012-06-05 14:41:27 -0700251volatile unsigned int secondary_cpu_index;
Sven Schnelle51676b12012-07-29 19:18:03 +0200252
Edward O'Callaghan2c9d2cf2014-10-27 23:29:29 +1100253int start_cpu(struct device *cpu)
Eric Biedermanfcd5ace2004-10-14 19:29:29 +0000254{
Sven Schnelle51676b12012-07-29 19:18:03 +0200255 struct cpu_info *info;
256 unsigned long stack_end;
Ronald G. Minnich000bf83c2012-06-06 13:00:24 -0700257 unsigned long stack_base;
258 unsigned long *stack;
Sven Schnelle51676b12012-07-29 19:18:03 +0200259 unsigned long apicid;
Ronald G. Minnich8b930592012-06-05 14:41:27 -0700260 unsigned int index;
Sven Schnelle51676b12012-07-29 19:18:03 +0200261 unsigned long count;
Ronald G. Minnich000bf83c2012-06-06 13:00:24 -0700262 int i;
Sven Schnelle51676b12012-07-29 19:18:03 +0200263 int result;
264
265 spin_lock(&start_cpu_lock);
266
Stefan Reinauer1bfbbc02012-06-07 14:00:07 -0700267 /* Get the CPU's apicid */
Sven Schnelle51676b12012-07-29 19:18:03 +0200268 apicid = cpu->path.apic.apic_id;
269
270 /* Get an index for the new processor */
271 index = ++last_cpu_index;
272
Stefan Reinauer1bfbbc02012-06-07 14:00:07 -0700273 /* Find end of the new processor's stack */
274 stack_end = ((unsigned long)_estack) - (CONFIG_STACK_SIZE*index) -
275 sizeof(struct cpu_info);
Sven Schnelle51676b12012-07-29 19:18:03 +0200276
Ronald G. Minnich000bf83c2012-06-06 13:00:24 -0700277 stack_base = ((unsigned long)_estack) - (CONFIG_STACK_SIZE*(index+1));
Ronald G. Minnich8b930592012-06-05 14:41:27 -0700278 printk(BIOS_SPEW, "CPU%d: stack_base %p, stack_end %p\n", index,
Ronald G. Minnich000bf83c2012-06-06 13:00:24 -0700279 (void *)stack_base, (void *)stack_end);
280 /* poison the stack */
281 for(stack = (void *)stack_base, i = 0; i < CONFIG_STACK_SIZE; i++)
282 stack[i/sizeof(*stack)] = 0xDEADBEEF;
283 stacks[index] = stack;
Stefan Reinauer1bfbbc02012-06-07 14:00:07 -0700284 /* Record the index and which CPU structure we are using */
Sven Schnelle51676b12012-07-29 19:18:03 +0200285 info = (struct cpu_info *)stack_end;
286 info->index = index;
287 info->cpu = cpu;
Aaron Durbin38c326d2013-05-06 12:22:23 -0500288 thread_init_cpu_info_non_bsp(info);
Sven Schnelle51676b12012-07-29 19:18:03 +0200289
Ronald G. Minnich8b930592012-06-05 14:41:27 -0700290 /* Advertise the new stack and index to start_cpu */
Sven Schnelle51676b12012-07-29 19:18:03 +0200291 secondary_stack = stack_end;
Ronald G. Minnich8b930592012-06-05 14:41:27 -0700292 secondary_cpu_index = index;
Sven Schnelle51676b12012-07-29 19:18:03 +0200293
Stefan Reinauer1bfbbc02012-06-07 14:00:07 -0700294 /* Until the CPU starts up report the CPU is not enabled */
Sven Schnelle51676b12012-07-29 19:18:03 +0200295 cpu->enabled = 0;
296 cpu->initialized = 0;
297
298 /* Start the cpu */
299 result = lapic_start_cpu(apicid);
300
301 if (result) {
302 result = 0;
303 /* Wait 1s or until the new cpu calls in */
304 for(count = 0; count < 100000 ; count++) {
305 if (secondary_stack == 0) {
306 result = 1;
307 break;
308 }
309 udelay(10);
Jonathan A. Kollasch98fc2e92015-07-15 11:27:18 -0500310 }
Sven Schnelle51676b12012-07-29 19:18:03 +0200311 }
312 secondary_stack = 0;
313 spin_unlock(&start_cpu_lock);
314 return result;
315}
316
317#if CONFIG_AP_IN_SIPI_WAIT
318
319/**
Stefan Reinauer1bfbbc02012-06-07 14:00:07 -0700320 * Sending INIT IPI to self is equivalent of asserting #INIT with a bit of
321 * delay.
Sven Schnelle51676b12012-07-29 19:18:03 +0200322 * An undefined number of instruction cycles will complete. All global locks
323 * must be released before INIT IPI and no printk is allowed after this.
324 * De-asserting INIT IPI is a no-op on later Intel CPUs.
325 *
326 * If you set DEBUG_HALT_SELF to 1, printk's after INIT IPI are enabled
327 * but running thread may halt without releasing the lock and effectively
328 * deadlock other CPUs.
329 */
330#define DEBUG_HALT_SELF 0
331
332/**
333 * Normally this function is defined in lapic.h as an always inline function
334 * that just keeps the CPU in a hlt() loop. This does not work on all CPUs.
335 * I think all hyperthreading CPUs might need this version, but I could only
336 * verify this on the Intel Core Duo
337 */
338void stop_this_cpu(void)
339{
Sven Schnelle042c1462012-06-17 10:32:55 +0200340 int timeout;
Sven Schnelle51676b12012-07-29 19:18:03 +0200341 unsigned long send_status;
342 unsigned long id;
343
344 id = lapic_read(LAPIC_ID) >> 24;
345
346 printk(BIOS_DEBUG, "CPU %ld going down...\n", id);
347
348 /* send an LAPIC INIT to myself */
349 lapic_write_around(LAPIC_ICR2, SET_LAPIC_DEST_FIELD(id));
Stefan Reinauer1bfbbc02012-06-07 14:00:07 -0700350 lapic_write_around(LAPIC_ICR, LAPIC_INT_LEVELTRIG |
351 LAPIC_INT_ASSERT | LAPIC_DM_INIT);
Stefan Reinauer68524062008-08-02 15:15:23 +0000352
353 /* wait for the ipi send to finish */
Sven Schnelle51676b12012-07-29 19:18:03 +0200354#if DEBUG_HALT_SELF
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000355 printk(BIOS_SPEW, "Waiting for send to finish...\n");
Sven Schnelle51676b12012-07-29 19:18:03 +0200356#endif
Stefan Reinauer68524062008-08-02 15:15:23 +0000357 timeout = 0;
358 do {
Sven Schnelle51676b12012-07-29 19:18:03 +0200359#if DEBUG_HALT_SELF
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000360 printk(BIOS_SPEW, "+");
Sven Schnelle51676b12012-07-29 19:18:03 +0200361#endif
Stefan Reinauer68524062008-08-02 15:15:23 +0000362 udelay(100);
363 send_status = lapic_read(LAPIC_ICR) & LAPIC_ICR_BUSY;
364 } while (send_status && (timeout++ < 1000));
365 if (timeout >= 1000) {
Sven Schnelle51676b12012-07-29 19:18:03 +0200366#if DEBUG_HALT_SELF
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000367 printk(BIOS_ERR, "timed out\n");
Sven Schnelle51676b12012-07-29 19:18:03 +0200368#endif
Stefan Reinauer68524062008-08-02 15:15:23 +0000369 }
370 mdelay(10);
Sven Schnelle51676b12012-07-29 19:18:03 +0200371
372#if DEBUG_HALT_SELF
373 printk(BIOS_SPEW, "Deasserting INIT.\n");
374#endif
375 /* Deassert the LAPIC INIT */
376 lapic_write_around(LAPIC_ICR2, SET_LAPIC_DEST_FIELD(id));
377 lapic_write_around(LAPIC_ICR, LAPIC_INT_LEVELTRIG | LAPIC_DM_INIT);
378
379#if DEBUG_HALT_SELF
380 printk(BIOS_SPEW, "Waiting for send to finish...\n");
381#endif
382 timeout = 0;
383 do {
384#if DEBUG_HALT_SELF
385 printk(BIOS_SPEW, "+");
386#endif
387 udelay(100);
388 send_status = lapic_read(LAPIC_ICR) & LAPIC_ICR_BUSY;
389 } while (send_status && (timeout++ < 1000));
390 if (timeout >= 1000) {
391#if DEBUG_HALT_SELF
392 printk(BIOS_ERR, "timed out\n");
393#endif
394 }
395
Patrick Georgibd79c5e2014-11-28 22:35:36 +0100396 halt();
Stefan Reinauer68524062008-08-02 15:15:23 +0000397}
Sven Schnelle51676b12012-07-29 19:18:03 +0200398#endif
Stefan Reinauer68524062008-08-02 15:15:23 +0000399
Eric Biedermanfcd5ace2004-10-14 19:29:29 +0000400/* C entry point of secondary cpus */
Stefan Reinauer399486e2012-12-06 13:54:29 -0800401void asmlinkage secondary_cpu_init(unsigned int index)
Eric Biedermanfcd5ace2004-10-14 19:29:29 +0000402{
Sven Schnelle042c1462012-06-17 10:32:55 +0200403 atomic_inc(&active_cpus);
Kyösti Mälkki5c3f3842014-05-08 15:27:15 +0300404
405 if (!IS_ENABLED(CONFIG_PARALLEL_CPU_INIT))
406 spin_lock(&start_cpu_lock);
Sven Schnelle51676b12012-07-29 19:18:03 +0200407
408#ifdef __SSE3__
409 /*
410 * Seems that CR4 was cleared when AP start via lapic_start_cpu()
411 * Turn on CR4.OSFXSR and CR4.OSXMMEXCPT when SSE options enabled
412 */
413 u32 cr4_val;
Aaron Durbin029aaf62013-10-10 12:41:49 -0500414 cr4_val = read_cr4();
415 cr4_val |= (CR4_OSFXSR | CR4_OSXMMEXCPT);
416 write_cr4(cr4_val);
Sven Schnelle51676b12012-07-29 19:18:03 +0200417#endif
Ronald G. Minnich8b930592012-06-05 14:41:27 -0700418 cpu_initialize(index);
Kyösti Mälkki5c3f3842014-05-08 15:27:15 +0300419
420 if (!IS_ENABLED(CONFIG_PARALLEL_CPU_INIT))
421 spin_unlock(&start_cpu_lock);
Sven Schnelle51676b12012-07-29 19:18:03 +0200422
Eric Biedermanfcd5ace2004-10-14 19:29:29 +0000423 atomic_dec(&active_cpus);
Sven Schnelle51676b12012-07-29 19:18:03 +0200424
425 stop_this_cpu();
426}
427
Edward O'Callaghan2c9d2cf2014-10-27 23:29:29 +1100428static void start_other_cpus(struct bus *cpu_bus, struct device *bsp_cpu)
Sven Schnelle51676b12012-07-29 19:18:03 +0200429{
Edward O'Callaghan2c9d2cf2014-10-27 23:29:29 +1100430 struct device *cpu;
Sven Schnelle51676b12012-07-29 19:18:03 +0200431 /* Loop through the cpus once getting them started */
432
433 for(cpu = cpu_bus->children; cpu ; cpu = cpu->sibling) {
434 if (cpu->path.type != DEVICE_PATH_APIC) {
435 continue;
436 }
Kyösti Mälkki5c3f3842014-05-08 15:27:15 +0300437
438 if (IS_ENABLED(CONFIG_PARALLEL_CPU_INIT) && (cpu==bsp_cpu))
Sven Schnelle51676b12012-07-29 19:18:03 +0200439 continue;
Sven Schnelle51676b12012-07-29 19:18:03 +0200440
441 if (!cpu->enabled) {
442 continue;
443 }
444
445 if (cpu->initialized) {
446 continue;
447 }
448
449 if (!start_cpu(cpu)) {
450 /* Record the error in cpu? */
451 printk(BIOS_ERR, "CPU 0x%02x would not start!\n",
452 cpu->path.apic.apic_id);
453 }
Kyösti Mälkki5c3f3842014-05-08 15:27:15 +0300454
455 if (!IS_ENABLED(CONFIG_PARALLEL_CPU_INIT))
456 udelay(10);
Sven Schnelle51676b12012-07-29 19:18:03 +0200457 }
458
Yinghai Lu9a8e36d2006-05-18 17:02:17 +0000459}
460
Damien Zammit149c4c52015-11-28 21:27:05 +1100461static void smm_other_cpus(struct bus *cpu_bus, device_t bsp_cpu)
462{
463 device_t cpu;
464 int pre_count = atomic_read(&active_cpus);
465
466 /* Loop through the cpus once to let them run through SMM relocator */
467
468 for(cpu = cpu_bus->children; cpu ; cpu = cpu->sibling) {
469 if (cpu->path.type != DEVICE_PATH_APIC) {
470 continue;
471 }
472
473 printk(BIOS_ERR, "considering CPU 0x%02x for SMM init\n",
474 cpu->path.apic.apic_id);
475
476 if (cpu == bsp_cpu)
477 continue;
478
479 if (!cpu->enabled) {
480 continue;
481 }
482
483 if (!start_cpu(cpu)) {
484 /* Record the error in cpu? */
485 printk(BIOS_ERR, "CPU 0x%02x would not start!\n",
486 cpu->path.apic.apic_id);
487 }
488
489 /* FIXME: endless loop */
490 while (atomic_read(&active_cpus) != pre_count) ;
491 }
492}
493
Yinghai Lu9a8e36d2006-05-18 17:02:17 +0000494static void wait_other_cpus_stop(struct bus *cpu_bus)
495{
Edward O'Callaghan2c9d2cf2014-10-27 23:29:29 +1100496 struct device *cpu;
Yinghai Lu9a8e36d2006-05-18 17:02:17 +0000497 int old_active_count, active_count;
Stefan Reinauer2bdfb482012-04-03 16:17:11 -0700498 long loopcount = 0;
Ronald G. Minnich000bf83c2012-06-06 13:00:24 -0700499 int i;
Stefan Reinauer2bdfb482012-04-03 16:17:11 -0700500
Eric Biedermanfcd5ace2004-10-14 19:29:29 +0000501 /* Now loop until the other cpus have finished initializing */
502 old_active_count = 1;
503 active_count = atomic_read(&active_cpus);
504 while(active_count > 1) {
505 if (active_count != old_active_count) {
Stefan Reinauer1bfbbc02012-06-07 14:00:07 -0700506 printk(BIOS_INFO, "Waiting for %d CPUS to stop\n",
507 active_count - 1);
Eric Biedermanfcd5ace2004-10-14 19:29:29 +0000508 old_active_count = active_count;
509 }
510 udelay(10);
511 active_count = atomic_read(&active_cpus);
Stefan Reinauer2bdfb482012-04-03 16:17:11 -0700512 loopcount++;
Eric Biedermanfcd5ace2004-10-14 19:29:29 +0000513 }
Eric Biederman7003ba42004-10-16 06:20:29 +0000514 for(cpu = cpu_bus->children; cpu; cpu = cpu->sibling) {
Eric Biedermanfcd5ace2004-10-14 19:29:29 +0000515 if (cpu->path.type != DEVICE_PATH_APIC) {
516 continue;
517 }
Stefan Reinauer2bdfb482012-04-03 16:17:11 -0700518 if (cpu->path.apic.apic_id == SPEEDSTEP_APIC_MAGIC) {
519 continue;
520 }
Eric Biedermanfcd5ace2004-10-14 19:29:29 +0000521 if (!cpu->initialized) {
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000522 printk(BIOS_ERR, "CPU 0x%02x did not initialize!\n",
Stefan Reinauer2b34db82009-02-28 20:10:20 +0000523 cpu->path.apic.apic_id);
Eric Biedermanfcd5ace2004-10-14 19:29:29 +0000524 }
525 }
Stefan Reinauer2bdfb482012-04-03 16:17:11 -0700526 printk(BIOS_DEBUG, "All AP CPUs stopped (%ld loops)\n", loopcount);
Timothy Pearson6b6c6532015-09-05 19:31:20 -0500527 checkstack(_estack, 0);
Stefan Reinauer75dbc382012-10-15 15:19:43 -0700528 for(i = 1; i <= last_cpu_index; i++)
529 checkstack((void *)stacks[i] + CONFIG_STACK_SIZE, i);
Eric Biedermanfcd5ace2004-10-14 19:29:29 +0000530}
531
Eric Biedermanfcd5ace2004-10-14 19:29:29 +0000532#endif /* CONFIG_SMP */
533
Eric Biederman7003ba42004-10-16 06:20:29 +0000534void initialize_cpus(struct bus *cpu_bus)
Eric Biedermanfcd5ace2004-10-14 19:29:29 +0000535{
536 struct device_path cpu_path;
Sven Schnelle51676b12012-07-29 19:18:03 +0200537 struct cpu_info *info;
538
539 /* Find the info struct for this cpu */
540 info = cpu_info();
Eric Biedermanfcd5ace2004-10-14 19:29:29 +0000541
542#if NEED_LAPIC == 1
543 /* Ensure the local apic is enabled */
544 enable_lapic();
545
546 /* Get the device path of the boot cpu */
547 cpu_path.type = DEVICE_PATH_APIC;
Stefan Reinauer2b34db82009-02-28 20:10:20 +0000548 cpu_path.apic.apic_id = lapicid();
Eric Biedermanfcd5ace2004-10-14 19:29:29 +0000549#else
550 /* Get the device path of the boot cpu */
Eric Biedermana9e632c2004-11-18 22:38:08 +0000551 cpu_path.type = DEVICE_PATH_CPU;
Stefan Reinauer2b34db82009-02-28 20:10:20 +0000552 cpu_path.cpu.id = 0;
Eric Biedermanfcd5ace2004-10-14 19:29:29 +0000553#endif
Stefan Reinauer00a889c2008-10-29 04:48:44 +0000554
Sven Schnelle51676b12012-07-29 19:18:03 +0200555 /* Find the device structure for the boot cpu */
556 info->cpu = alloc_find_dev(cpu_bus, &cpu_path);
557
Ronald G. Minnich000bf83c2012-06-06 13:00:24 -0700558#if CONFIG_SMP && CONFIG_MAX_CPUS > 1
Stefan Reinauer1bfbbc02012-06-07 14:00:07 -0700559 // why here? In case some day we can start core1 in amd_sibling_init
560 copy_secondary_start_to_lowest_1M();
Yinghai Lu3a68aeb2006-01-09 20:42:50 +0000561#endif
Stefan Reinauer00a889c2008-10-29 04:48:44 +0000562
Stefan Reinauer08670622009-06-30 15:17:49 +0000563#if CONFIG_HAVE_SMI_HANDLER
Damien Zammit149c4c52015-11-28 21:27:05 +1100564 if (!IS_ENABLED(CONFIG_SERIALIZED_SMM_INITIALIZATION))
565 smm_init();
Stefan Reinauer00a889c2008-10-29 04:48:44 +0000566#endif
567
Ronald G. Minnich000bf83c2012-06-06 13:00:24 -0700568#if CONFIG_SMP && CONFIG_MAX_CPUS > 1
Sven Schnelle51676b12012-07-29 19:18:03 +0200569 /* start all aps at first, so we can init ECC all together */
Kyösti Mälkki5c3f3842014-05-08 15:27:15 +0300570 if (IS_ENABLED(CONFIG_PARALLEL_CPU_INIT))
571 start_other_cpus(cpu_bus, info->cpu);
Sven Schnelle51676b12012-07-29 19:18:03 +0200572#endif
573
Zheng Baod4c5c442010-02-20 09:38:16 +0000574 /* Initialize the bootstrap processor */
Ronald G. Minnich8b930592012-06-05 14:41:27 -0700575 cpu_initialize(0);
Yinghai Lu9a8e36d2006-05-18 17:02:17 +0000576
Ronald G. Minnich000bf83c2012-06-06 13:00:24 -0700577#if CONFIG_SMP && CONFIG_MAX_CPUS > 1
Kyösti Mälkki5c3f3842014-05-08 15:27:15 +0300578 if (!IS_ENABLED(CONFIG_PARALLEL_CPU_INIT))
579 start_other_cpus(cpu_bus, info->cpu);
Sven Schnelle51676b12012-07-29 19:18:03 +0200580
Yinghai Lu9a8e36d2006-05-18 17:02:17 +0000581 /* Now wait the rest of the cpus stop*/
582 wait_other_cpus_stop(cpu_bus);
583#endif
Damien Zammit149c4c52015-11-28 21:27:05 +1100584
585 if (IS_ENABLED(CONFIG_SERIALIZED_SMM_INITIALIZATION)) {
586 /* At this point, all APs are sleeping:
587 * smm_init() will queue a pending SMI on all cpus
588 * and smm_other_cpus() will start them one by one */
589 smm_init();
590#if CONFIG_SMP && CONFIG_MAX_CPUS > 1
591 last_cpu_index = 0;
592 smm_other_cpus(cpu_bus, info->cpu);
593#endif
594 }
Eric Biedermanfcd5ace2004-10-14 19:29:29 +0000595}