Yinghai Lu | 72ee9b0 | 2005-12-14 02:39:33 +0000 | [diff] [blame] | 1 | /* |
Stefan Reinauer | 1bfbbc0 | 2012-06-07 14:00:07 -0700 | [diff] [blame] | 2 | * 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 |
| 8 | * |
| 9 | * This program is free software; you can redistribute it and/or modify |
| 10 | * it under the terms of the GNU General Public License as published by |
| 11 | * the Free Software Foundation; version 2 of the License. |
| 12 | * |
| 13 | * This program is distributed in the hope that it will be useful, |
| 14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 16 | * GNU General Public License for more details. |
| 17 | * |
| 18 | * You should have received a copy of the GNU General Public License |
| 19 | * along with this program; if not, write to the Free Software |
Paul Menzel | a46a712 | 2013-02-23 18:37:27 +0100 | [diff] [blame] | 20 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
Stefan Reinauer | 1bfbbc0 | 2012-06-07 14:00:07 -0700 | [diff] [blame] | 21 | */ |
Yinghai Lu | 72ee9b0 | 2005-12-14 02:39:33 +0000 | [diff] [blame] | 22 | |
Eric Biederman | fcd5ace | 2004-10-14 19:29:29 +0000 | [diff] [blame] | 23 | #include <cpu/x86/lapic.h> |
| 24 | #include <delay.h> |
Stefan Reinauer | 75dbc38 | 2012-10-15 15:19:43 -0700 | [diff] [blame] | 25 | #include <lib.h> |
Eric Biederman | fcd5ace | 2004-10-14 19:29:29 +0000 | [diff] [blame] | 26 | #include <string.h> |
| 27 | #include <console/console.h> |
| 28 | #include <arch/hlt.h> |
| 29 | #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 Reinauer | 2bdfb48 | 2012-04-03 16:17:11 -0700 | [diff] [blame] | 34 | #include <cpu/intel/speedstep.h> |
Eric Biederman | fcd5ace | 2004-10-14 19:29:29 +0000 | [diff] [blame] | 35 | |
Ronald G. Minnich | 000bf83 | 2012-06-06 13:00:24 -0700 | [diff] [blame] | 36 | #if CONFIG_SMP && CONFIG_MAX_CPUS > 1 |
Eric Biederman | fcd5ace | 2004-10-14 19:29:29 +0000 | [diff] [blame] | 37 | /* This is a lot more paranoid now, since Linux can NOT handle |
Zheng Bao | d4c5c44 | 2010-02-20 09:38:16 +0000 | [diff] [blame] | 38 | * being told there is a CPU when none exists. So any errors |
| 39 | * will return 0, meaning no CPU. |
Eric Biederman | fcd5ace | 2004-10-14 19:29:29 +0000 | [diff] [blame] | 40 | * |
| 41 | * We actually handling that case by noting which cpus startup |
| 42 | * and not telling anyone about the ones that dont. |
Zheng Bao | d4c5c44 | 2010-02-20 09:38:16 +0000 | [diff] [blame] | 43 | */ |
Kyösti Mälkki | 63539bb | 2012-07-05 06:31:15 +0300 | [diff] [blame] | 44 | |
| 45 | /* Start-UP IPI vector must be 4kB aligned and below 1MB. */ |
| 46 | #define AP_SIPI_VECTOR 0x1000 |
Yinghai Lu | 72ee9b0 | 2005-12-14 02:39:33 +0000 | [diff] [blame] | 47 | |
Patrick Georgi | e166782 | 2012-05-05 15:29:32 +0200 | [diff] [blame] | 48 | #if CONFIG_HAVE_ACPI_RESUME |
Rudolf Marek | a572f83 | 2009-04-13 17:57:44 +0000 | [diff] [blame] | 49 | char *lowmem_backup; |
| 50 | char *lowmem_backup_ptr; |
| 51 | int lowmem_backup_size; |
| 52 | #endif |
| 53 | |
Myles Watson | 6e23576 | 2009-09-29 14:56:15 +0000 | [diff] [blame] | 54 | extern char _secondary_start[]; |
Aaron Durbin | a146d58 | 2013-02-08 16:56:51 -0600 | [diff] [blame] | 55 | extern char _secondary_gdt_addr[]; |
| 56 | extern char gdt[]; |
| 57 | extern char gdt_end[]; |
| 58 | |
| 59 | static inline void setup_secondary_gdt(void) |
| 60 | { |
| 61 | u16 *gdt_limit; |
| 62 | u32 *gdt_base; |
| 63 | |
| 64 | gdt_limit = (void *)&_secondary_gdt_addr; |
| 65 | gdt_base = (void *)&gdt_limit[1]; |
| 66 | |
| 67 | *gdt_limit = (u32)&gdt_end - (u32)&gdt - 1; |
| 68 | *gdt_base = (u32)&gdt; |
| 69 | } |
Myles Watson | 6e23576 | 2009-09-29 14:56:15 +0000 | [diff] [blame] | 70 | |
Stefan Reinauer | 1bfbbc0 | 2012-06-07 14:00:07 -0700 | [diff] [blame] | 71 | static void copy_secondary_start_to_lowest_1M(void) |
Yinghai Lu | 72ee9b0 | 2005-12-14 02:39:33 +0000 | [diff] [blame] | 72 | { |
Zheng Bao | d4c5c44 | 2010-02-20 09:38:16 +0000 | [diff] [blame] | 73 | extern char _secondary_start_end[]; |
| 74 | unsigned long code_size; |
Yinghai Lu | 72ee9b0 | 2005-12-14 02:39:33 +0000 | [diff] [blame] | 75 | |
Aaron Durbin | a146d58 | 2013-02-08 16:56:51 -0600 | [diff] [blame] | 76 | /* Fill in secondary_start's local gdt. */ |
| 77 | setup_secondary_gdt(); |
| 78 | |
Zheng Bao | d4c5c44 | 2010-02-20 09:38:16 +0000 | [diff] [blame] | 79 | code_size = (unsigned long)_secondary_start_end - (unsigned long)_secondary_start; |
Yinghai Lu | 72ee9b0 | 2005-12-14 02:39:33 +0000 | [diff] [blame] | 80 | |
Patrick Georgi | e166782 | 2012-05-05 15:29:32 +0200 | [diff] [blame] | 81 | #if CONFIG_HAVE_ACPI_RESUME |
Rudolf Marek | a572f83 | 2009-04-13 17:57:44 +0000 | [diff] [blame] | 82 | /* need to save it for RAM resume */ |
| 83 | lowmem_backup_size = code_size; |
| 84 | lowmem_backup = malloc(code_size); |
Kyösti Mälkki | 63539bb | 2012-07-05 06:31:15 +0300 | [diff] [blame] | 85 | lowmem_backup_ptr = (char *)AP_SIPI_VECTOR; |
Zheng Bao | d4c5c44 | 2010-02-20 09:38:16 +0000 | [diff] [blame] | 86 | |
Rudolf Marek | a572f83 | 2009-04-13 17:57:44 +0000 | [diff] [blame] | 87 | if (lowmem_backup == NULL) |
| 88 | die("Out of backup memory\n"); |
| 89 | |
Zheng Bao | d4c5c44 | 2010-02-20 09:38:16 +0000 | [diff] [blame] | 90 | memcpy(lowmem_backup, lowmem_backup_ptr, lowmem_backup_size); |
Rudolf Marek | a572f83 | 2009-04-13 17:57:44 +0000 | [diff] [blame] | 91 | #endif |
Zheng Bao | d4c5c44 | 2010-02-20 09:38:16 +0000 | [diff] [blame] | 92 | /* copy the _secondary_start to the ram below 1M*/ |
Kyösti Mälkki | 63539bb | 2012-07-05 06:31:15 +0300 | [diff] [blame] | 93 | memcpy((unsigned char *)AP_SIPI_VECTOR, (unsigned char *)_secondary_start, code_size); |
Yinghai Lu | 72ee9b0 | 2005-12-14 02:39:33 +0000 | [diff] [blame] | 94 | |
Stefan Reinauer | 1bfbbc0 | 2012-06-07 14:00:07 -0700 | [diff] [blame] | 95 | printk(BIOS_DEBUG, "start_eip=0x%08lx, code_size=0x%08lx\n", |
| 96 | (long unsigned int)AP_SIPI_VECTOR, code_size); |
Yinghai Lu | 72ee9b0 | 2005-12-14 02:39:33 +0000 | [diff] [blame] | 97 | } |
| 98 | |
Sven Schnelle | 51676b1 | 2012-07-29 19:18:03 +0200 | [diff] [blame] | 99 | static int lapic_start_cpu(unsigned long apicid) |
Eric Biederman | fcd5ace | 2004-10-14 19:29:29 +0000 | [diff] [blame] | 100 | { |
| 101 | int timeout; |
Kyösti Mälkki | 63539bb | 2012-07-05 06:31:15 +0300 | [diff] [blame] | 102 | unsigned long send_status, accept_status; |
Sven Schnelle | 51676b1 | 2012-07-29 19:18:03 +0200 | [diff] [blame] | 103 | int j, num_starts, maxlvt; |
Zheng Bao | d4c5c44 | 2010-02-20 09:38:16 +0000 | [diff] [blame] | 104 | |
Eric Biederman | fcd5ace | 2004-10-14 19:29:29 +0000 | [diff] [blame] | 105 | /* |
| 106 | * Starting actual IPI sequence... |
| 107 | */ |
| 108 | |
Stefan Reinauer | c02b4fc | 2010-03-22 11:42:32 +0000 | [diff] [blame] | 109 | printk(BIOS_SPEW, "Asserting INIT.\n"); |
Eric Biederman | fcd5ace | 2004-10-14 19:29:29 +0000 | [diff] [blame] | 110 | |
Sven Schnelle | 51676b1 | 2012-07-29 19:18:03 +0200 | [diff] [blame] | 111 | /* |
| 112 | * Turn INIT on target chip |
| 113 | */ |
| 114 | lapic_write_around(LAPIC_ICR2, SET_LAPIC_DEST_FIELD(apicid)); |
Eric Biederman | fcd5ace | 2004-10-14 19:29:29 +0000 | [diff] [blame] | 115 | |
Sven Schnelle | 51676b1 | 2012-07-29 19:18:03 +0200 | [diff] [blame] | 116 | /* |
| 117 | * Send IPI |
| 118 | */ |
| 119 | |
| 120 | lapic_write_around(LAPIC_ICR, LAPIC_INT_LEVELTRIG | LAPIC_INT_ASSERT |
| 121 | | LAPIC_DM_INIT); |
| 122 | |
| 123 | printk(BIOS_SPEW, "Waiting for send to finish...\n"); |
Eric Biederman | fcd5ace | 2004-10-14 19:29:29 +0000 | [diff] [blame] | 124 | timeout = 0; |
| 125 | do { |
Stefan Reinauer | c02b4fc | 2010-03-22 11:42:32 +0000 | [diff] [blame] | 126 | printk(BIOS_SPEW, "+"); |
Eric Biederman | fcd5ace | 2004-10-14 19:29:29 +0000 | [diff] [blame] | 127 | udelay(100); |
| 128 | send_status = lapic_read(LAPIC_ICR) & LAPIC_ICR_BUSY; |
| 129 | } while (send_status && (timeout++ < 1000)); |
| 130 | if (timeout >= 1000) { |
Stefan Reinauer | 1bfbbc0 | 2012-06-07 14:00:07 -0700 | [diff] [blame] | 131 | printk(BIOS_ERR, "CPU %ld: First APIC write timed out. " |
| 132 | "Disabling\n", apicid); |
Zheng Bao | d4c5c44 | 2010-02-20 09:38:16 +0000 | [diff] [blame] | 133 | // too bad. |
Sven Schnelle | 51676b1 | 2012-07-29 19:18:03 +0200 | [diff] [blame] | 134 | printk(BIOS_ERR, "ESR is 0x%lx\n", lapic_read(LAPIC_ESR)); |
Eric Biederman | fcd5ace | 2004-10-14 19:29:29 +0000 | [diff] [blame] | 135 | if (lapic_read(LAPIC_ESR)) { |
Sven Schnelle | 51676b1 | 2012-07-29 19:18:03 +0200 | [diff] [blame] | 136 | printk(BIOS_ERR, "Try to reset ESR\n"); |
Eric Biederman | fcd5ace | 2004-10-14 19:29:29 +0000 | [diff] [blame] | 137 | lapic_write_around(LAPIC_ESR, 0); |
Stefan Reinauer | 1bfbbc0 | 2012-06-07 14:00:07 -0700 | [diff] [blame] | 138 | printk(BIOS_ERR, "ESR is 0x%lx\n", |
| 139 | lapic_read(LAPIC_ESR)); |
Eric Biederman | fcd5ace | 2004-10-14 19:29:29 +0000 | [diff] [blame] | 140 | } |
| 141 | return 0; |
| 142 | } |
Sven Schnelle | 51676b1 | 2012-07-29 19:18:03 +0200 | [diff] [blame] | 143 | #if !CONFIG_CPU_AMD_MODEL_10XXX && !CONFIG_CPU_INTEL_MODEL_206AX |
| 144 | mdelay(10); |
| 145 | #endif |
Yinghai Lu | 9a791df | 2006-04-03 20:38:34 +0000 | [diff] [blame] | 146 | |
Sven Schnelle | 51676b1 | 2012-07-29 19:18:03 +0200 | [diff] [blame] | 147 | printk(BIOS_SPEW, "Deasserting INIT.\n"); |
Sven Schnelle | 042c146 | 2012-06-17 10:32:55 +0200 | [diff] [blame] | 148 | |
| 149 | /* Target chip */ |
Sven Schnelle | 51676b1 | 2012-07-29 19:18:03 +0200 | [diff] [blame] | 150 | lapic_write_around(LAPIC_ICR2, SET_LAPIC_DEST_FIELD(apicid)); |
Sven Schnelle | 042c146 | 2012-06-17 10:32:55 +0200 | [diff] [blame] | 151 | |
Sven Schnelle | 51676b1 | 2012-07-29 19:18:03 +0200 | [diff] [blame] | 152 | /* Send IPI */ |
| 153 | lapic_write_around(LAPIC_ICR, LAPIC_INT_LEVELTRIG | LAPIC_DM_INIT); |
Sven Schnelle | 042c146 | 2012-06-17 10:32:55 +0200 | [diff] [blame] | 154 | |
Sven Schnelle | 51676b1 | 2012-07-29 19:18:03 +0200 | [diff] [blame] | 155 | printk(BIOS_SPEW, "Waiting for send to finish...\n"); |
Sven Schnelle | 042c146 | 2012-06-17 10:32:55 +0200 | [diff] [blame] | 156 | timeout = 0; |
| 157 | do { |
Sven Schnelle | 51676b1 | 2012-07-29 19:18:03 +0200 | [diff] [blame] | 158 | printk(BIOS_SPEW, "+"); |
Sven Schnelle | 042c146 | 2012-06-17 10:32:55 +0200 | [diff] [blame] | 159 | udelay(100); |
| 160 | send_status = lapic_read(LAPIC_ICR) & LAPIC_ICR_BUSY; |
| 161 | } while (send_status && (timeout++ < 1000)); |
Sven Schnelle | 51676b1 | 2012-07-29 19:18:03 +0200 | [diff] [blame] | 162 | if (timeout >= 1000) { |
Stefan Reinauer | 1bfbbc0 | 2012-06-07 14:00:07 -0700 | [diff] [blame] | 163 | printk(BIOS_ERR, "CPU %ld: Second apic write timed out. " |
| 164 | "Disabling\n", apicid); |
Sven Schnelle | 51676b1 | 2012-07-29 19:18:03 +0200 | [diff] [blame] | 165 | // too bad. |
| 166 | return 0; |
| 167 | } |
| 168 | |
| 169 | #if !CONFIG_CPU_AMD_MODEL_10XXX |
| 170 | num_starts = 2; |
| 171 | #else |
| 172 | num_starts = 1; |
| 173 | #endif |
Sven Schnelle | 042c146 | 2012-06-17 10:32:55 +0200 | [diff] [blame] | 174 | |
| 175 | /* |
Sven Schnelle | 51676b1 | 2012-07-29 19:18:03 +0200 | [diff] [blame] | 176 | * Run STARTUP IPI loop. |
Sven Schnelle | 042c146 | 2012-06-17 10:32:55 +0200 | [diff] [blame] | 177 | */ |
Sven Schnelle | 51676b1 | 2012-07-29 19:18:03 +0200 | [diff] [blame] | 178 | printk(BIOS_SPEW, "#startup loops: %d.\n", num_starts); |
| 179 | |
| 180 | maxlvt = 4; |
| 181 | |
| 182 | for (j = 1; j <= num_starts; j++) { |
| 183 | printk(BIOS_SPEW, "Sending STARTUP #%d to %lu.\n", j, apicid); |
Eric Biederman | fcd5ace | 2004-10-14 19:29:29 +0000 | [diff] [blame] | 184 | lapic_read_around(LAPIC_SPIV); |
| 185 | lapic_write(LAPIC_ESR, 0); |
Sven Schnelle | 51676b1 | 2012-07-29 19:18:03 +0200 | [diff] [blame] | 186 | lapic_read(LAPIC_ESR); |
| 187 | printk(BIOS_SPEW, "After apic_write.\n"); |
Sven Schnelle | 042c146 | 2012-06-17 10:32:55 +0200 | [diff] [blame] | 188 | |
Sven Schnelle | 51676b1 | 2012-07-29 19:18:03 +0200 | [diff] [blame] | 189 | /* |
| 190 | * STARTUP IPI |
| 191 | */ |
| 192 | |
| 193 | /* Target chip */ |
| 194 | lapic_write_around(LAPIC_ICR2, SET_LAPIC_DEST_FIELD(apicid)); |
| 195 | |
| 196 | /* Boot on the stack */ |
| 197 | /* Kick the second */ |
| 198 | lapic_write_around(LAPIC_ICR, LAPIC_DM_STARTUP |
| 199 | | (AP_SIPI_VECTOR >> 12)); |
| 200 | |
| 201 | /* |
| 202 | * Give the other CPU some time to accept the IPI. |
| 203 | */ |
| 204 | udelay(300); |
| 205 | |
| 206 | printk(BIOS_SPEW, "Startup point 1.\n"); |
| 207 | |
| 208 | printk(BIOS_SPEW, "Waiting for send to finish...\n"); |
| 209 | timeout = 0; |
| 210 | do { |
| 211 | printk(BIOS_SPEW, "+"); |
| 212 | udelay(100); |
| 213 | send_status = lapic_read(LAPIC_ICR) & LAPIC_ICR_BUSY; |
| 214 | } while (send_status && (timeout++ < 1000)); |
| 215 | |
| 216 | /* |
| 217 | * Give the other CPU some time to accept the IPI. |
| 218 | */ |
| 219 | udelay(200); |
| 220 | /* |
| 221 | * Due to the Pentium erratum 3AP. |
| 222 | */ |
| 223 | if (maxlvt > 3) { |
| 224 | lapic_read_around(LAPIC_SPIV); |
| 225 | lapic_write(LAPIC_ESR, 0); |
| 226 | } |
| 227 | accept_status = (lapic_read(LAPIC_ESR) & 0xEF); |
| 228 | if (send_status || accept_status) |
| 229 | break; |
| 230 | } |
| 231 | printk(BIOS_SPEW, "After Startup.\n"); |
Eric Biederman | fcd5ace | 2004-10-14 19:29:29 +0000 | [diff] [blame] | 232 | if (send_status) |
Stefan Reinauer | c02b4fc | 2010-03-22 11:42:32 +0000 | [diff] [blame] | 233 | printk(BIOS_WARNING, "APIC never delivered???\n"); |
Eric Biederman | fcd5ace | 2004-10-14 19:29:29 +0000 | [diff] [blame] | 234 | if (accept_status) |
Stefan Reinauer | 1bfbbc0 | 2012-06-07 14:00:07 -0700 | [diff] [blame] | 235 | printk(BIOS_WARNING, "APIC delivery error (%lx).\n", |
| 236 | accept_status); |
Eric Biederman | fcd5ace | 2004-10-14 19:29:29 +0000 | [diff] [blame] | 237 | if (send_status || accept_status) |
| 238 | return 0; |
| 239 | return 1; |
| 240 | } |
| 241 | |
Stefan Reinauer | f8ee180 | 2008-01-18 15:08:58 +0000 | [diff] [blame] | 242 | /* Number of cpus that are currently running in coreboot */ |
Eric Biederman | fcd5ace | 2004-10-14 19:29:29 +0000 | [diff] [blame] | 243 | static atomic_t active_cpus = ATOMIC_INIT(1); |
| 244 | |
Sven Schnelle | 51676b1 | 2012-07-29 19:18:03 +0200 | [diff] [blame] | 245 | /* start_cpu_lock covers last_cpu_index and secondary_stack. |
| 246 | * Only starting one cpu at a time let's me remove the logic |
| 247 | * for select the stack from assembly language. |
| 248 | * |
| 249 | * In addition communicating by variables to the cpu I |
Ronald G. Minnich | 8b93059 | 2012-06-05 14:41:27 -0700 | [diff] [blame] | 250 | * am starting allows me to verify it has started before |
Sven Schnelle | 51676b1 | 2012-07-29 19:18:03 +0200 | [diff] [blame] | 251 | * start_cpu returns. |
| 252 | */ |
Eric Biederman | fcd5ace | 2004-10-14 19:29:29 +0000 | [diff] [blame] | 253 | |
Sven Schnelle | 51676b1 | 2012-07-29 19:18:03 +0200 | [diff] [blame] | 254 | static spinlock_t start_cpu_lock = SPIN_LOCK_UNLOCKED; |
Ronald G. Minnich | 8b93059 | 2012-06-05 14:41:27 -0700 | [diff] [blame] | 255 | static unsigned int last_cpu_index = 0; |
Stefan Reinauer | 1bfbbc0 | 2012-06-07 14:00:07 -0700 | [diff] [blame] | 256 | static void *stacks[CONFIG_MAX_CPUS]; |
Sven Schnelle | 51676b1 | 2012-07-29 19:18:03 +0200 | [diff] [blame] | 257 | volatile unsigned long secondary_stack; |
Ronald G. Minnich | 8b93059 | 2012-06-05 14:41:27 -0700 | [diff] [blame] | 258 | volatile unsigned int secondary_cpu_index; |
Sven Schnelle | 51676b1 | 2012-07-29 19:18:03 +0200 | [diff] [blame] | 259 | |
| 260 | int start_cpu(device_t cpu) |
Eric Biederman | fcd5ace | 2004-10-14 19:29:29 +0000 | [diff] [blame] | 261 | { |
Sven Schnelle | 51676b1 | 2012-07-29 19:18:03 +0200 | [diff] [blame] | 262 | struct cpu_info *info; |
| 263 | unsigned long stack_end; |
Ronald G. Minnich | 000bf83 | 2012-06-06 13:00:24 -0700 | [diff] [blame] | 264 | unsigned long stack_base; |
| 265 | unsigned long *stack; |
Sven Schnelle | 51676b1 | 2012-07-29 19:18:03 +0200 | [diff] [blame] | 266 | unsigned long apicid; |
Ronald G. Minnich | 8b93059 | 2012-06-05 14:41:27 -0700 | [diff] [blame] | 267 | unsigned int index; |
Sven Schnelle | 51676b1 | 2012-07-29 19:18:03 +0200 | [diff] [blame] | 268 | unsigned long count; |
Ronald G. Minnich | 000bf83 | 2012-06-06 13:00:24 -0700 | [diff] [blame] | 269 | int i; |
Sven Schnelle | 51676b1 | 2012-07-29 19:18:03 +0200 | [diff] [blame] | 270 | int result; |
| 271 | |
| 272 | spin_lock(&start_cpu_lock); |
| 273 | |
Stefan Reinauer | 1bfbbc0 | 2012-06-07 14:00:07 -0700 | [diff] [blame] | 274 | /* Get the CPU's apicid */ |
Sven Schnelle | 51676b1 | 2012-07-29 19:18:03 +0200 | [diff] [blame] | 275 | apicid = cpu->path.apic.apic_id; |
| 276 | |
| 277 | /* Get an index for the new processor */ |
| 278 | index = ++last_cpu_index; |
| 279 | |
Stefan Reinauer | 1bfbbc0 | 2012-06-07 14:00:07 -0700 | [diff] [blame] | 280 | /* Find end of the new processor's stack */ |
| 281 | stack_end = ((unsigned long)_estack) - (CONFIG_STACK_SIZE*index) - |
| 282 | sizeof(struct cpu_info); |
Sven Schnelle | 51676b1 | 2012-07-29 19:18:03 +0200 | [diff] [blame] | 283 | |
Ronald G. Minnich | 000bf83 | 2012-06-06 13:00:24 -0700 | [diff] [blame] | 284 | stack_base = ((unsigned long)_estack) - (CONFIG_STACK_SIZE*(index+1)); |
Ronald G. Minnich | 8b93059 | 2012-06-05 14:41:27 -0700 | [diff] [blame] | 285 | printk(BIOS_SPEW, "CPU%d: stack_base %p, stack_end %p\n", index, |
Ronald G. Minnich | 000bf83 | 2012-06-06 13:00:24 -0700 | [diff] [blame] | 286 | (void *)stack_base, (void *)stack_end); |
| 287 | /* poison the stack */ |
| 288 | for(stack = (void *)stack_base, i = 0; i < CONFIG_STACK_SIZE; i++) |
| 289 | stack[i/sizeof(*stack)] = 0xDEADBEEF; |
| 290 | stacks[index] = stack; |
Stefan Reinauer | 1bfbbc0 | 2012-06-07 14:00:07 -0700 | [diff] [blame] | 291 | /* Record the index and which CPU structure we are using */ |
Sven Schnelle | 51676b1 | 2012-07-29 19:18:03 +0200 | [diff] [blame] | 292 | info = (struct cpu_info *)stack_end; |
| 293 | info->index = index; |
| 294 | info->cpu = cpu; |
| 295 | |
Ronald G. Minnich | 8b93059 | 2012-06-05 14:41:27 -0700 | [diff] [blame] | 296 | /* Advertise the new stack and index to start_cpu */ |
Sven Schnelle | 51676b1 | 2012-07-29 19:18:03 +0200 | [diff] [blame] | 297 | secondary_stack = stack_end; |
Ronald G. Minnich | 8b93059 | 2012-06-05 14:41:27 -0700 | [diff] [blame] | 298 | secondary_cpu_index = index; |
Sven Schnelle | 51676b1 | 2012-07-29 19:18:03 +0200 | [diff] [blame] | 299 | |
Stefan Reinauer | 1bfbbc0 | 2012-06-07 14:00:07 -0700 | [diff] [blame] | 300 | /* Until the CPU starts up report the CPU is not enabled */ |
Sven Schnelle | 51676b1 | 2012-07-29 19:18:03 +0200 | [diff] [blame] | 301 | cpu->enabled = 0; |
| 302 | cpu->initialized = 0; |
| 303 | |
| 304 | /* Start the cpu */ |
| 305 | result = lapic_start_cpu(apicid); |
| 306 | |
| 307 | if (result) { |
| 308 | result = 0; |
| 309 | /* Wait 1s or until the new cpu calls in */ |
| 310 | for(count = 0; count < 100000 ; count++) { |
| 311 | if (secondary_stack == 0) { |
| 312 | result = 1; |
| 313 | break; |
| 314 | } |
| 315 | udelay(10); |
| 316 | } |
| 317 | } |
| 318 | secondary_stack = 0; |
| 319 | spin_unlock(&start_cpu_lock); |
| 320 | return result; |
| 321 | } |
| 322 | |
| 323 | #if CONFIG_AP_IN_SIPI_WAIT |
| 324 | |
| 325 | /** |
Stefan Reinauer | 1bfbbc0 | 2012-06-07 14:00:07 -0700 | [diff] [blame] | 326 | * Sending INIT IPI to self is equivalent of asserting #INIT with a bit of |
| 327 | * delay. |
Sven Schnelle | 51676b1 | 2012-07-29 19:18:03 +0200 | [diff] [blame] | 328 | * An undefined number of instruction cycles will complete. All global locks |
| 329 | * must be released before INIT IPI and no printk is allowed after this. |
| 330 | * De-asserting INIT IPI is a no-op on later Intel CPUs. |
| 331 | * |
| 332 | * If you set DEBUG_HALT_SELF to 1, printk's after INIT IPI are enabled |
| 333 | * but running thread may halt without releasing the lock and effectively |
| 334 | * deadlock other CPUs. |
| 335 | */ |
| 336 | #define DEBUG_HALT_SELF 0 |
| 337 | |
| 338 | /** |
| 339 | * Normally this function is defined in lapic.h as an always inline function |
| 340 | * that just keeps the CPU in a hlt() loop. This does not work on all CPUs. |
| 341 | * I think all hyperthreading CPUs might need this version, but I could only |
| 342 | * verify this on the Intel Core Duo |
| 343 | */ |
| 344 | void stop_this_cpu(void) |
| 345 | { |
Sven Schnelle | 042c146 | 2012-06-17 10:32:55 +0200 | [diff] [blame] | 346 | int timeout; |
Sven Schnelle | 51676b1 | 2012-07-29 19:18:03 +0200 | [diff] [blame] | 347 | unsigned long send_status; |
| 348 | unsigned long id; |
| 349 | |
| 350 | id = lapic_read(LAPIC_ID) >> 24; |
| 351 | |
| 352 | printk(BIOS_DEBUG, "CPU %ld going down...\n", id); |
| 353 | |
| 354 | /* send an LAPIC INIT to myself */ |
| 355 | lapic_write_around(LAPIC_ICR2, SET_LAPIC_DEST_FIELD(id)); |
Stefan Reinauer | 1bfbbc0 | 2012-06-07 14:00:07 -0700 | [diff] [blame] | 356 | lapic_write_around(LAPIC_ICR, LAPIC_INT_LEVELTRIG | |
| 357 | LAPIC_INT_ASSERT | LAPIC_DM_INIT); |
Stefan Reinauer | 6852406 | 2008-08-02 15:15:23 +0000 | [diff] [blame] | 358 | |
| 359 | /* wait for the ipi send to finish */ |
Sven Schnelle | 51676b1 | 2012-07-29 19:18:03 +0200 | [diff] [blame] | 360 | #if DEBUG_HALT_SELF |
Stefan Reinauer | c02b4fc | 2010-03-22 11:42:32 +0000 | [diff] [blame] | 361 | printk(BIOS_SPEW, "Waiting for send to finish...\n"); |
Sven Schnelle | 51676b1 | 2012-07-29 19:18:03 +0200 | [diff] [blame] | 362 | #endif |
Stefan Reinauer | 6852406 | 2008-08-02 15:15:23 +0000 | [diff] [blame] | 363 | timeout = 0; |
| 364 | do { |
Sven Schnelle | 51676b1 | 2012-07-29 19:18:03 +0200 | [diff] [blame] | 365 | #if DEBUG_HALT_SELF |
Stefan Reinauer | c02b4fc | 2010-03-22 11:42:32 +0000 | [diff] [blame] | 366 | printk(BIOS_SPEW, "+"); |
Sven Schnelle | 51676b1 | 2012-07-29 19:18:03 +0200 | [diff] [blame] | 367 | #endif |
Stefan Reinauer | 6852406 | 2008-08-02 15:15:23 +0000 | [diff] [blame] | 368 | udelay(100); |
| 369 | send_status = lapic_read(LAPIC_ICR) & LAPIC_ICR_BUSY; |
| 370 | } while (send_status && (timeout++ < 1000)); |
| 371 | if (timeout >= 1000) { |
Sven Schnelle | 51676b1 | 2012-07-29 19:18:03 +0200 | [diff] [blame] | 372 | #if DEBUG_HALT_SELF |
Stefan Reinauer | c02b4fc | 2010-03-22 11:42:32 +0000 | [diff] [blame] | 373 | printk(BIOS_ERR, "timed out\n"); |
Sven Schnelle | 51676b1 | 2012-07-29 19:18:03 +0200 | [diff] [blame] | 374 | #endif |
Stefan Reinauer | 6852406 | 2008-08-02 15:15:23 +0000 | [diff] [blame] | 375 | } |
| 376 | mdelay(10); |
Sven Schnelle | 51676b1 | 2012-07-29 19:18:03 +0200 | [diff] [blame] | 377 | |
| 378 | #if DEBUG_HALT_SELF |
| 379 | printk(BIOS_SPEW, "Deasserting INIT.\n"); |
| 380 | #endif |
| 381 | /* Deassert the LAPIC INIT */ |
| 382 | lapic_write_around(LAPIC_ICR2, SET_LAPIC_DEST_FIELD(id)); |
| 383 | lapic_write_around(LAPIC_ICR, LAPIC_INT_LEVELTRIG | LAPIC_DM_INIT); |
| 384 | |
| 385 | #if DEBUG_HALT_SELF |
| 386 | printk(BIOS_SPEW, "Waiting for send to finish...\n"); |
| 387 | #endif |
| 388 | timeout = 0; |
| 389 | do { |
| 390 | #if DEBUG_HALT_SELF |
| 391 | printk(BIOS_SPEW, "+"); |
| 392 | #endif |
| 393 | udelay(100); |
| 394 | send_status = lapic_read(LAPIC_ICR) & LAPIC_ICR_BUSY; |
| 395 | } while (send_status && (timeout++ < 1000)); |
| 396 | if (timeout >= 1000) { |
| 397 | #if DEBUG_HALT_SELF |
| 398 | printk(BIOS_ERR, "timed out\n"); |
| 399 | #endif |
| 400 | } |
| 401 | |
| 402 | while(1) { |
| 403 | hlt(); |
| 404 | } |
Stefan Reinauer | 6852406 | 2008-08-02 15:15:23 +0000 | [diff] [blame] | 405 | } |
Sven Schnelle | 51676b1 | 2012-07-29 19:18:03 +0200 | [diff] [blame] | 406 | #endif |
Stefan Reinauer | 6852406 | 2008-08-02 15:15:23 +0000 | [diff] [blame] | 407 | |
efdesign98 | 3cab93c | 2011-07-20 20:11:46 -0600 | [diff] [blame] | 408 | #ifdef __SSE3__ |
| 409 | static __inline__ __attribute__((always_inline)) unsigned long readcr4(void) |
| 410 | { |
| 411 | unsigned long value; |
| 412 | __asm__ __volatile__ ( |
| 413 | "mov %%cr4, %[value]" |
| 414 | : [value] "=a" (value)); |
| 415 | return value; |
| 416 | } |
| 417 | |
| 418 | static __inline__ __attribute__((always_inline)) void writecr4(unsigned long Data) |
| 419 | { |
| 420 | __asm__ __volatile__ ( |
| 421 | "mov %%eax, %%cr4" |
| 422 | : |
| 423 | : "a" (Data) |
| 424 | ); |
| 425 | } |
| 426 | #endif |
| 427 | |
Eric Biederman | fcd5ace | 2004-10-14 19:29:29 +0000 | [diff] [blame] | 428 | /* C entry point of secondary cpus */ |
Stefan Reinauer | 399486e | 2012-12-06 13:54:29 -0800 | [diff] [blame] | 429 | void asmlinkage secondary_cpu_init(unsigned int index) |
Eric Biederman | fcd5ace | 2004-10-14 19:29:29 +0000 | [diff] [blame] | 430 | { |
Sven Schnelle | 042c146 | 2012-06-17 10:32:55 +0200 | [diff] [blame] | 431 | atomic_inc(&active_cpus); |
Sven Schnelle | 51676b1 | 2012-07-29 19:18:03 +0200 | [diff] [blame] | 432 | #if CONFIG_SERIAL_CPU_INIT |
| 433 | spin_lock(&start_cpu_lock); |
| 434 | #endif |
| 435 | |
| 436 | #ifdef __SSE3__ |
| 437 | /* |
| 438 | * Seems that CR4 was cleared when AP start via lapic_start_cpu() |
| 439 | * Turn on CR4.OSFXSR and CR4.OSXMMEXCPT when SSE options enabled |
| 440 | */ |
| 441 | u32 cr4_val; |
| 442 | cr4_val = readcr4(); |
| 443 | cr4_val |= (1 << 9 | 1 << 10); |
| 444 | writecr4(cr4_val); |
| 445 | #endif |
Ronald G. Minnich | 8b93059 | 2012-06-05 14:41:27 -0700 | [diff] [blame] | 446 | cpu_initialize(index); |
Sven Schnelle | 51676b1 | 2012-07-29 19:18:03 +0200 | [diff] [blame] | 447 | #if CONFIG_SERIAL_CPU_INIT |
| 448 | spin_unlock(&start_cpu_lock); |
| 449 | #endif |
| 450 | |
Eric Biederman | fcd5ace | 2004-10-14 19:29:29 +0000 | [diff] [blame] | 451 | atomic_dec(&active_cpus); |
Sven Schnelle | 51676b1 | 2012-07-29 19:18:03 +0200 | [diff] [blame] | 452 | |
| 453 | stop_this_cpu(); |
| 454 | } |
| 455 | |
| 456 | static void start_other_cpus(struct bus *cpu_bus, device_t bsp_cpu) |
| 457 | { |
| 458 | device_t cpu; |
| 459 | /* Loop through the cpus once getting them started */ |
| 460 | |
| 461 | for(cpu = cpu_bus->children; cpu ; cpu = cpu->sibling) { |
| 462 | if (cpu->path.type != DEVICE_PATH_APIC) { |
| 463 | continue; |
| 464 | } |
| 465 | #if !CONFIG_SERIAL_CPU_INIT |
| 466 | if(cpu==bsp_cpu) { |
| 467 | continue; |
| 468 | } |
| 469 | #endif |
| 470 | |
| 471 | if (!cpu->enabled) { |
| 472 | continue; |
| 473 | } |
| 474 | |
| 475 | if (cpu->initialized) { |
| 476 | continue; |
| 477 | } |
| 478 | |
| 479 | if (!start_cpu(cpu)) { |
| 480 | /* Record the error in cpu? */ |
| 481 | printk(BIOS_ERR, "CPU 0x%02x would not start!\n", |
| 482 | cpu->path.apic.apic_id); |
| 483 | } |
| 484 | #if CONFIG_SERIAL_CPU_INIT |
| 485 | udelay(10); |
| 486 | #endif |
| 487 | } |
| 488 | |
Yinghai Lu | 9a8e36d | 2006-05-18 17:02:17 +0000 | [diff] [blame] | 489 | } |
| 490 | |
| 491 | static void wait_other_cpus_stop(struct bus *cpu_bus) |
| 492 | { |
| 493 | device_t cpu; |
| 494 | int old_active_count, active_count; |
Stefan Reinauer | 2bdfb48 | 2012-04-03 16:17:11 -0700 | [diff] [blame] | 495 | long loopcount = 0; |
Ronald G. Minnich | 000bf83 | 2012-06-06 13:00:24 -0700 | [diff] [blame] | 496 | int i; |
Stefan Reinauer | 2bdfb48 | 2012-04-03 16:17:11 -0700 | [diff] [blame] | 497 | |
Eric Biederman | fcd5ace | 2004-10-14 19:29:29 +0000 | [diff] [blame] | 498 | /* Now loop until the other cpus have finished initializing */ |
| 499 | old_active_count = 1; |
| 500 | active_count = atomic_read(&active_cpus); |
| 501 | while(active_count > 1) { |
| 502 | if (active_count != old_active_count) { |
Stefan Reinauer | 1bfbbc0 | 2012-06-07 14:00:07 -0700 | [diff] [blame] | 503 | printk(BIOS_INFO, "Waiting for %d CPUS to stop\n", |
| 504 | active_count - 1); |
Eric Biederman | fcd5ace | 2004-10-14 19:29:29 +0000 | [diff] [blame] | 505 | old_active_count = active_count; |
| 506 | } |
| 507 | udelay(10); |
| 508 | active_count = atomic_read(&active_cpus); |
Stefan Reinauer | 2bdfb48 | 2012-04-03 16:17:11 -0700 | [diff] [blame] | 509 | loopcount++; |
Eric Biederman | fcd5ace | 2004-10-14 19:29:29 +0000 | [diff] [blame] | 510 | } |
Eric Biederman | 7003ba4 | 2004-10-16 06:20:29 +0000 | [diff] [blame] | 511 | for(cpu = cpu_bus->children; cpu; cpu = cpu->sibling) { |
Eric Biederman | fcd5ace | 2004-10-14 19:29:29 +0000 | [diff] [blame] | 512 | if (cpu->path.type != DEVICE_PATH_APIC) { |
| 513 | continue; |
| 514 | } |
Stefan Reinauer | 2bdfb48 | 2012-04-03 16:17:11 -0700 | [diff] [blame] | 515 | if (cpu->path.apic.apic_id == SPEEDSTEP_APIC_MAGIC) { |
| 516 | continue; |
| 517 | } |
Eric Biederman | fcd5ace | 2004-10-14 19:29:29 +0000 | [diff] [blame] | 518 | if (!cpu->initialized) { |
Stefan Reinauer | c02b4fc | 2010-03-22 11:42:32 +0000 | [diff] [blame] | 519 | printk(BIOS_ERR, "CPU 0x%02x did not initialize!\n", |
Stefan Reinauer | 2b34db8 | 2009-02-28 20:10:20 +0000 | [diff] [blame] | 520 | cpu->path.apic.apic_id); |
Eric Biederman | fcd5ace | 2004-10-14 19:29:29 +0000 | [diff] [blame] | 521 | } |
| 522 | } |
Stefan Reinauer | 2bdfb48 | 2012-04-03 16:17:11 -0700 | [diff] [blame] | 523 | printk(BIOS_DEBUG, "All AP CPUs stopped (%ld loops)\n", loopcount); |
Stefan Reinauer | 75dbc38 | 2012-10-15 15:19:43 -0700 | [diff] [blame] | 524 | for(i = 1; i <= last_cpu_index; i++) |
| 525 | checkstack((void *)stacks[i] + CONFIG_STACK_SIZE, i); |
Eric Biederman | fcd5ace | 2004-10-14 19:29:29 +0000 | [diff] [blame] | 526 | } |
| 527 | |
Eric Biederman | fcd5ace | 2004-10-14 19:29:29 +0000 | [diff] [blame] | 528 | #endif /* CONFIG_SMP */ |
| 529 | |
Eric Biederman | 7003ba4 | 2004-10-16 06:20:29 +0000 | [diff] [blame] | 530 | void initialize_cpus(struct bus *cpu_bus) |
Eric Biederman | fcd5ace | 2004-10-14 19:29:29 +0000 | [diff] [blame] | 531 | { |
| 532 | struct device_path cpu_path; |
Sven Schnelle | 51676b1 | 2012-07-29 19:18:03 +0200 | [diff] [blame] | 533 | struct cpu_info *info; |
| 534 | |
| 535 | /* Find the info struct for this cpu */ |
| 536 | info = cpu_info(); |
Eric Biederman | fcd5ace | 2004-10-14 19:29:29 +0000 | [diff] [blame] | 537 | |
| 538 | #if NEED_LAPIC == 1 |
| 539 | /* Ensure the local apic is enabled */ |
| 540 | enable_lapic(); |
| 541 | |
| 542 | /* Get the device path of the boot cpu */ |
| 543 | cpu_path.type = DEVICE_PATH_APIC; |
Stefan Reinauer | 2b34db8 | 2009-02-28 20:10:20 +0000 | [diff] [blame] | 544 | cpu_path.apic.apic_id = lapicid(); |
Eric Biederman | fcd5ace | 2004-10-14 19:29:29 +0000 | [diff] [blame] | 545 | #else |
| 546 | /* Get the device path of the boot cpu */ |
Eric Biederman | a9e632c | 2004-11-18 22:38:08 +0000 | [diff] [blame] | 547 | cpu_path.type = DEVICE_PATH_CPU; |
Stefan Reinauer | 2b34db8 | 2009-02-28 20:10:20 +0000 | [diff] [blame] | 548 | cpu_path.cpu.id = 0; |
Eric Biederman | fcd5ace | 2004-10-14 19:29:29 +0000 | [diff] [blame] | 549 | #endif |
Stefan Reinauer | 00a889c | 2008-10-29 04:48:44 +0000 | [diff] [blame] | 550 | |
Sven Schnelle | 51676b1 | 2012-07-29 19:18:03 +0200 | [diff] [blame] | 551 | /* Find the device structure for the boot cpu */ |
| 552 | info->cpu = alloc_find_dev(cpu_bus, &cpu_path); |
| 553 | |
Ronald G. Minnich | 000bf83 | 2012-06-06 13:00:24 -0700 | [diff] [blame] | 554 | #if CONFIG_SMP && CONFIG_MAX_CPUS > 1 |
Stefan Reinauer | 1bfbbc0 | 2012-06-07 14:00:07 -0700 | [diff] [blame] | 555 | // why here? In case some day we can start core1 in amd_sibling_init |
| 556 | copy_secondary_start_to_lowest_1M(); |
Yinghai Lu | 3a68aeb | 2006-01-09 20:42:50 +0000 | [diff] [blame] | 557 | #endif |
Stefan Reinauer | 00a889c | 2008-10-29 04:48:44 +0000 | [diff] [blame] | 558 | |
Stefan Reinauer | 0867062 | 2009-06-30 15:17:49 +0000 | [diff] [blame] | 559 | #if CONFIG_HAVE_SMI_HANDLER |
Stefan Reinauer | 00a889c | 2008-10-29 04:48:44 +0000 | [diff] [blame] | 560 | smm_init(); |
| 561 | #endif |
| 562 | |
Ronald G. Minnich | 000bf83 | 2012-06-06 13:00:24 -0700 | [diff] [blame] | 563 | #if CONFIG_SMP && CONFIG_MAX_CPUS > 1 |
Sven Schnelle | 51676b1 | 2012-07-29 19:18:03 +0200 | [diff] [blame] | 564 | #if !CONFIG_SERIAL_CPU_INIT |
| 565 | /* start all aps at first, so we can init ECC all together */ |
| 566 | start_other_cpus(cpu_bus, info->cpu); |
| 567 | #endif |
| 568 | #endif |
| 569 | |
Zheng Bao | d4c5c44 | 2010-02-20 09:38:16 +0000 | [diff] [blame] | 570 | /* Initialize the bootstrap processor */ |
Ronald G. Minnich | 8b93059 | 2012-06-05 14:41:27 -0700 | [diff] [blame] | 571 | cpu_initialize(0); |
Yinghai Lu | 9a8e36d | 2006-05-18 17:02:17 +0000 | [diff] [blame] | 572 | |
Ronald G. Minnich | 000bf83 | 2012-06-06 13:00:24 -0700 | [diff] [blame] | 573 | #if CONFIG_SMP && CONFIG_MAX_CPUS > 1 |
Sven Schnelle | 51676b1 | 2012-07-29 19:18:03 +0200 | [diff] [blame] | 574 | #if CONFIG_SERIAL_CPU_INIT |
| 575 | start_other_cpus(cpu_bus, info->cpu); |
| 576 | #endif |
| 577 | |
Yinghai Lu | 9a8e36d | 2006-05-18 17:02:17 +0000 | [diff] [blame] | 578 | /* Now wait the rest of the cpus stop*/ |
| 579 | wait_other_cpus_stop(cpu_bus); |
| 580 | #endif |
Eric Biederman | fcd5ace | 2004-10-14 19:29:29 +0000 | [diff] [blame] | 581 | } |