Angel Pons | f23ae0b | 2020-04-02 23:48:12 +0200 | [diff] [blame] | 1 | /* SPDX-License-Identifier: GPL-2.0-only */ |
Aaron Durbin | e0785c0 | 2013-10-21 12:15:29 -0500 | [diff] [blame] | 2 | |
Aaron Durbin | 154d209 | 2017-06-16 14:20:10 -0500 | [diff] [blame] | 3 | #include <cpu/x86/cr.h> |
Marshall Dawson | c0dbeda | 2017-10-19 09:45:16 -0600 | [diff] [blame] | 4 | #include <cpu/amd/mtrr.h> |
Elyes HAOUAS | 419bfbc | 2018-10-01 08:47:51 +0200 | [diff] [blame] | 5 | #include <cpu/x86/msr.h> |
Patrick Rudolph | 776da08 | 2019-10-25 08:09:33 +0200 | [diff] [blame] | 6 | #include <arch/ram_segs.h> |
Aaron Durbin | 154d209 | 2017-06-16 14:20:10 -0500 | [diff] [blame] | 7 | |
Patrick Rudolph | a169550 | 2019-12-01 07:23:59 +0100 | [diff] [blame] | 8 | #define __RAMSTAGE__ |
Arthur Heymans | 3edf840 | 2021-07-05 21:18:50 +0200 | [diff] [blame] | 9 | #include <cpu/x86/64bit/entry64.inc> |
Patrick Rudolph | a169550 | 2019-12-01 07:23:59 +0100 | [diff] [blame] | 10 | |
Elyes HAOUAS | ece2696 | 2018-08-07 12:24:16 +0200 | [diff] [blame] | 11 | /* The SIPI vector is responsible for initializing the APs in the system. It |
Aaron Durbin | e0785c0 | 2013-10-21 12:15:29 -0500 | [diff] [blame] | 12 | * loads microcode, sets up MSRs, and enables caching before calling into |
| 13 | * C code. */ |
| 14 | |
Aaron Durbin | e0785c0 | 2013-10-21 12:15:29 -0500 | [diff] [blame] | 15 | .section ".module_parameters", "aw", @progbits |
| 16 | ap_start_params: |
| 17 | gdtaddr: |
| 18 | .word 0 /* limit */ |
| 19 | .long 0 /* table */ |
| 20 | .word 0 /* unused */ |
| 21 | idt_ptr: |
| 22 | .long 0 |
Raul E Rangel | b2346a5 | 2021-09-22 14:56:51 -0600 | [diff] [blame] | 23 | per_cpu_segment_descriptors: |
| 24 | .long 0 |
| 25 | per_cpu_segment_selector: |
| 26 | .long 0 |
Aaron Durbin | e0785c0 | 2013-10-21 12:15:29 -0500 | [diff] [blame] | 27 | stack_top: |
| 28 | .long 0 |
| 29 | stack_size: |
| 30 | .long 0 |
| 31 | microcode_lock: |
| 32 | .long 0 |
| 33 | microcode_ptr: |
| 34 | .long 0 |
| 35 | msr_table_ptr: |
| 36 | .long 0 |
| 37 | msr_count: |
| 38 | .long 0 |
| 39 | c_handler: |
| 40 | .long 0 |
Arthur Heymans | 3cfcffe | 2024-02-02 17:37:42 +0100 | [diff] [blame] | 41 | cr3: |
| 42 | .long 0 |
Aaron Durbin | e0785c0 | 2013-10-21 12:15:29 -0500 | [diff] [blame] | 43 | ap_count: |
| 44 | .long 0 |
| 45 | |
Aaron Durbin | 154d209 | 2017-06-16 14:20:10 -0500 | [diff] [blame] | 46 | #define CR0_CLEAR_FLAGS_CACHE_ENABLE (CR0_CD | CR0_NW) |
| 47 | #define CR0_SET_FLAGS (CR0_CLEAR_FLAGS_CACHE_ENABLE | CR0_PE) |
| 48 | #define CR0_CLEAR_FLAGS \ |
| 49 | (CR0_PG | CR0_AM | CR0_WP | CR0_NE | CR0_TS | CR0_EM | CR0_MP) |
| 50 | |
Aaron Durbin | e0785c0 | 2013-10-21 12:15:29 -0500 | [diff] [blame] | 51 | .text |
| 52 | .code16 |
Aaron Durbin | dde7629 | 2015-09-05 12:59:26 -0500 | [diff] [blame] | 53 | .global _start |
| 54 | _start: |
Aaron Durbin | e0785c0 | 2013-10-21 12:15:29 -0500 | [diff] [blame] | 55 | cli |
| 56 | xorl %eax, %eax |
| 57 | movl %eax, %cr3 /* Invalidate TLB*/ |
| 58 | |
| 59 | /* On hyper threaded cpus, invalidating the cache here is |
| 60 | * very very bad. Don't. |
| 61 | */ |
| 62 | |
| 63 | /* setup the data segment */ |
| 64 | movw %cs, %ax |
| 65 | movw %ax, %ds |
| 66 | |
Martin Roth | 0949e73 | 2021-10-01 14:28:22 -0600 | [diff] [blame] | 67 | /* The gdtaddr needs to be relative to the data segment in order |
Aaron Durbin | e0785c0 | 2013-10-21 12:15:29 -0500 | [diff] [blame] | 68 | * to properly dereference it. The .text section comes first in an |
Aaron Durbin | dde7629 | 2015-09-05 12:59:26 -0500 | [diff] [blame] | 69 | * rmodule so _start can be used as a proxy for the load address. */ |
Aaron Durbin | e0785c0 | 2013-10-21 12:15:29 -0500 | [diff] [blame] | 70 | movl $(gdtaddr), %ebx |
Aaron Durbin | dde7629 | 2015-09-05 12:59:26 -0500 | [diff] [blame] | 71 | sub $(_start), %ebx |
Aaron Durbin | e0785c0 | 2013-10-21 12:15:29 -0500 | [diff] [blame] | 72 | |
Edward O'Callaghan | 4e2294b | 2017-01-08 19:14:42 +1100 | [diff] [blame] | 73 | lgdtl (%ebx) |
Aaron Durbin | e0785c0 | 2013-10-21 12:15:29 -0500 | [diff] [blame] | 74 | |
| 75 | movl %cr0, %eax |
Aaron Durbin | 154d209 | 2017-06-16 14:20:10 -0500 | [diff] [blame] | 76 | andl $~CR0_CLEAR_FLAGS, %eax |
| 77 | orl $CR0_SET_FLAGS, %eax |
Aaron Durbin | e0785c0 | 2013-10-21 12:15:29 -0500 | [diff] [blame] | 78 | movl %eax, %cr0 |
| 79 | |
Patrick Rudolph | 776da08 | 2019-10-25 08:09:33 +0200 | [diff] [blame] | 80 | ljmpl $RAM_CODE_SEG, $1f |
Aaron Durbin | e0785c0 | 2013-10-21 12:15:29 -0500 | [diff] [blame] | 81 | 1: |
| 82 | .code32 |
Patrick Rudolph | 776da08 | 2019-10-25 08:09:33 +0200 | [diff] [blame] | 83 | movw $RAM_DATA_SEG, %ax |
Aaron Durbin | e0785c0 | 2013-10-21 12:15:29 -0500 | [diff] [blame] | 84 | movw %ax, %ds |
| 85 | movw %ax, %es |
| 86 | movw %ax, %ss |
Raul E Rangel | ea5c311 | 2021-09-21 10:17:24 -0600 | [diff] [blame] | 87 | xor %ax, %ax /* zero out the gs and fs segment index */ |
Aaron Durbin | e0785c0 | 2013-10-21 12:15:29 -0500 | [diff] [blame] | 88 | movw %ax, %fs |
Raul E Rangel | ea5c311 | 2021-09-21 10:17:24 -0600 | [diff] [blame] | 89 | movw %ax, %gs /* Will be used for cpu_info */ |
Aaron Durbin | e0785c0 | 2013-10-21 12:15:29 -0500 | [diff] [blame] | 90 | |
| 91 | /* Load the Interrupt descriptor table */ |
| 92 | mov idt_ptr, %ebx |
| 93 | lidt (%ebx) |
| 94 | |
Aaron Durbin | e0785c0 | 2013-10-21 12:15:29 -0500 | [diff] [blame] | 95 | 1: |
Patrick Rudolph | 72c6071 | 2020-11-18 13:26:10 +0100 | [diff] [blame] | 96 | /* Obtain CPU number. */ |
| 97 | movl ap_count, %ecx |
Aaron Durbin | e0785c0 | 2013-10-21 12:15:29 -0500 | [diff] [blame] | 98 | inc %ecx |
| 99 | lock cmpxchg %ecx, ap_count |
| 100 | jnz 1b |
| 101 | |
| 102 | /* Setup stacks for each CPU. */ |
| 103 | movl stack_size, %eax |
| 104 | mul %ecx |
| 105 | movl stack_top, %edx |
| 106 | subl %eax, %edx |
| 107 | mov %edx, %esp |
Raul E Rangel | b2346a5 | 2021-09-22 14:56:51 -0600 | [diff] [blame] | 108 | |
Arthur Heymans | d4dfc21 | 2022-11-10 13:34:49 +0100 | [diff] [blame] | 109 | /* Save CPU number for calling the AP entry */ |
| 110 | push %ecx |
Raul E Rangel | b2346a5 | 2021-09-22 14:56:51 -0600 | [diff] [blame] | 111 | |
Patrick Rudolph | ce51b34 | 2021-01-11 09:21:58 +0100 | [diff] [blame] | 112 | /* |
| 113 | * The following code only needs to run on Intel platforms and thus the caller |
| 114 | * doesn't provide a microcode_ptr if not on Intel. |
| 115 | * On Intel platforms which update microcode using FIT the version check will |
| 116 | * also skip the microcode update. |
| 117 | */ |
| 118 | |
Aaron Durbin | e0785c0 | 2013-10-21 12:15:29 -0500 | [diff] [blame] | 119 | /* Determine if one should check microcode versions. */ |
| 120 | mov microcode_ptr, %edi |
| 121 | test %edi, %edi |
| 122 | jz microcode_done /* Bypass if no microde exists. */ |
| 123 | |
| 124 | /* Get the Microcode version. */ |
Arthur Heymans | 193aac8 | 2021-01-18 19:50:26 +0100 | [diff] [blame] | 125 | xorl %eax, %eax |
| 126 | xorl %edx, %edx |
| 127 | movl $IA32_BIOS_SIGN_ID, %ecx |
| 128 | wrmsr |
Aaron Durbin | e0785c0 | 2013-10-21 12:15:29 -0500 | [diff] [blame] | 129 | mov $1, %eax |
| 130 | cpuid |
| 131 | mov $IA32_BIOS_SIGN_ID, %ecx |
| 132 | rdmsr |
| 133 | /* If something already loaded skip loading again. */ |
| 134 | test %edx, %edx |
| 135 | jnz microcode_done |
| 136 | |
Patrick Rudolph | ce51b34 | 2021-01-11 09:21:58 +0100 | [diff] [blame] | 137 | /* |
| 138 | * Intel SDM and various BWGs specify to use a semaphore to update microcode |
| 139 | * on one thread per core on Hyper-Threading enabled CPUs. Due to this complex |
| 140 | * code would be necessary to determine the core #ID, initializing and picking |
| 141 | * the right semaphore out of CONFIG_MAX_CPUS / 2. |
| 142 | * Instead of the per core approachm, as recommended, use one global spinlock. |
| 143 | * Assuming that only pre-FIT platforms with Hyper-Threading enabled and at |
| 144 | * most 8 threads will ever run into this condition, the boot delay is negligible. |
| 145 | */ |
| 146 | |
Aaron Durbin | e0785c0 | 2013-10-21 12:15:29 -0500 | [diff] [blame] | 147 | /* Determine if parallel microcode loading is allowed. */ |
Damien Zammit | 6933151 | 2017-09-02 20:30:39 +1000 | [diff] [blame] | 148 | cmpl $0xffffffff, microcode_lock |
Aaron Durbin | e0785c0 | 2013-10-21 12:15:29 -0500 | [diff] [blame] | 149 | je load_microcode |
| 150 | |
| 151 | /* Protect microcode loading. */ |
| 152 | lock_microcode: |
Jacob Garber | 1627e2f | 2020-10-28 20:03:53 -0600 | [diff] [blame] | 153 | lock btsl $0, microcode_lock |
Aaron Durbin | e0785c0 | 2013-10-21 12:15:29 -0500 | [diff] [blame] | 154 | jc lock_microcode |
| 155 | |
| 156 | load_microcode: |
| 157 | /* Load new microcode. */ |
Elyes HAOUAS | 419bfbc | 2018-10-01 08:47:51 +0200 | [diff] [blame] | 158 | mov $IA32_BIOS_UPDT_TRIG, %ecx |
Aaron Durbin | e0785c0 | 2013-10-21 12:15:29 -0500 | [diff] [blame] | 159 | xor %edx, %edx |
| 160 | mov %edi, %eax |
| 161 | /* The microcode pointer is passed in pointing to the header. Adjust |
| 162 | * pointer to reflect the payload (header size is 48 bytes). */ |
| 163 | add $48, %eax |
| 164 | pusha |
| 165 | wrmsr |
| 166 | popa |
| 167 | |
| 168 | /* Unconditionally unlock microcode loading. */ |
Damien Zammit | 6933151 | 2017-09-02 20:30:39 +1000 | [diff] [blame] | 169 | cmpl $0xffffffff, microcode_lock |
Aaron Durbin | e0785c0 | 2013-10-21 12:15:29 -0500 | [diff] [blame] | 170 | je microcode_done |
| 171 | |
| 172 | xor %eax, %eax |
| 173 | mov %eax, microcode_lock |
| 174 | |
| 175 | microcode_done: |
| 176 | /* |
| 177 | * Load MSRs. Each entry in the table consists of: |
| 178 | * 0: index, |
| 179 | * 4: value[31:0] |
| 180 | * 8: value[63:32] |
| 181 | */ |
| 182 | mov msr_table_ptr, %edi |
| 183 | mov msr_count, %ebx |
| 184 | test %ebx, %ebx |
| 185 | jz 1f |
Marshall Dawson | c0dbeda | 2017-10-19 09:45:16 -0600 | [diff] [blame] | 186 | |
Julius Werner | cd49cce | 2019-03-05 16:53:33 -0800 | [diff] [blame] | 187 | #if CONFIG(X86_AMD_FIXED_MTRRS) |
Marshall Dawson | c0dbeda | 2017-10-19 09:45:16 -0600 | [diff] [blame] | 188 | /* Allow modification of RdDram and WrDram bits */ |
| 189 | mov $SYSCFG_MSR, %ecx |
| 190 | rdmsr |
| 191 | or $SYSCFG_MSR_MtrrFixDramModEn, %eax |
| 192 | wrmsr |
| 193 | #endif |
| 194 | |
Aaron Durbin | e0785c0 | 2013-10-21 12:15:29 -0500 | [diff] [blame] | 195 | load_msr: |
| 196 | mov (%edi), %ecx |
| 197 | mov 4(%edi), %eax |
| 198 | mov 8(%edi), %edx |
| 199 | wrmsr |
| 200 | add $12, %edi |
| 201 | dec %ebx |
| 202 | jnz load_msr |
| 203 | |
Julius Werner | cd49cce | 2019-03-05 16:53:33 -0800 | [diff] [blame] | 204 | #if CONFIG(X86_AMD_FIXED_MTRRS) |
Marshall Dawson | c0dbeda | 2017-10-19 09:45:16 -0600 | [diff] [blame] | 205 | mov $SYSCFG_MSR, %ecx |
| 206 | rdmsr |
| 207 | and $~SYSCFG_MSR_MtrrFixDramModEn, %eax |
| 208 | wrmsr |
| 209 | #endif |
| 210 | |
Aaron Durbin | e0785c0 | 2013-10-21 12:15:29 -0500 | [diff] [blame] | 211 | 1: |
| 212 | /* Enable caching. */ |
| 213 | mov %cr0, %eax |
Aaron Durbin | 154d209 | 2017-06-16 14:20:10 -0500 | [diff] [blame] | 214 | and $~(CR0_CLEAR_FLAGS_CACHE_ENABLE), %eax |
Aaron Durbin | e0785c0 | 2013-10-21 12:15:29 -0500 | [diff] [blame] | 215 | mov %eax, %cr0 |
| 216 | |
Julius Werner | cd49cce | 2019-03-05 16:53:33 -0800 | [diff] [blame] | 217 | #if CONFIG(SSE) |
Aaron Durbin | 8ade68a | 2017-06-16 15:16:13 -0500 | [diff] [blame] | 218 | /* Enable sse instructions. */ |
| 219 | mov %cr4, %eax |
| 220 | orl $(CR4_OSFXSR | CR4_OSXMMEXCPT), %eax |
| 221 | mov %eax, %cr4 |
| 222 | #endif |
| 223 | |
Arthur Heymans | d4dfc21 | 2022-11-10 13:34:49 +0100 | [diff] [blame] | 224 | pop %edi /* Retrieve cpu index */ |
Raul E Rangel | 3671597 | 2021-10-08 13:10:38 -0600 | [diff] [blame] | 225 | andl $0xfffffff0, %esp /* ensure stack alignment */ |
| 226 | |
Patrick Rudolph | adcf782 | 2020-08-27 20:50:18 +0200 | [diff] [blame] | 227 | #if ENV_X86_64 |
Arthur Heymans | d4dfc21 | 2022-11-10 13:34:49 +0100 | [diff] [blame] | 228 | /* entry64.inc preserves ebx, esi, edi, ebp */ |
Arthur Heymans | 3cfcffe | 2024-02-02 17:37:42 +0100 | [diff] [blame] | 229 | setup_longmode cr3 |
Arthur Heymans | 3edf840 | 2021-07-05 21:18:50 +0200 | [diff] [blame] | 230 | |
Patrick Rudolph | d023909 | 2021-06-11 21:24:10 +0200 | [diff] [blame] | 231 | movabs c_handler, %eax |
Patrick Rudolph | a169550 | 2019-12-01 07:23:59 +0100 | [diff] [blame] | 232 | call *%rax |
| 233 | #else |
Arthur Heymans | d4dfc21 | 2022-11-10 13:34:49 +0100 | [diff] [blame] | 234 | push $0 |
| 235 | push $0 |
| 236 | push $0 |
| 237 | push %edi |
Aaron Durbin | e0785c0 | 2013-10-21 12:15:29 -0500 | [diff] [blame] | 238 | mov c_handler, %eax |
| 239 | call *%eax |
Patrick Rudolph | a169550 | 2019-12-01 07:23:59 +0100 | [diff] [blame] | 240 | #endif |
| 241 | |
| 242 | |
Aaron Durbin | e0785c0 | 2013-10-21 12:15:29 -0500 | [diff] [blame] | 243 | halt_jump: |
| 244 | hlt |
| 245 | jmp halt_jump |