1 /* SPDX-License-Identifier: GPL-2.0-only */
3 #include <cpu/x86/cr.h>
4 #include <cpu/amd/mtrr.h>
5 #include <cpu/x86/msr.h>
6 #include <arch/ram_segs.h>
9 #include <cpu/x86/64bit/entry64.inc>
11 /* The SIPI vector is responsible for initializing the APs in the system. It
12 * loads microcode, sets up MSRs, and enables caching before calling into
15 .section ".module_parameters", "aw", @progbits
23 per_cpu_segment_descriptors:
25 per_cpu_segment_selector:
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)
57 movl %eax, %cr3 /* Invalidate TLB*/
59 /* On hyper threaded cpus, invalidating the cache here is
60 * very very bad. Don't.
63 /* setup the data segment */
67 /* The gdtaddr needs to be relative to the data segment in order
68 * to properly dereference it. The .text section comes first in an
69 * rmodule so _start can be used as a proxy for the load address. */
76 andl $~CR0_CLEAR_FLAGS, %eax
77 orl $CR0_SET_FLAGS, %eax
80 ljmpl $RAM_CODE_SEG, $1f
83 movw $RAM_DATA_SEG, %ax
87 xor %ax, %ax /* zero out the gs and fs segment index */
89 movw %ax, %gs /* Will be used for cpu_info */
91 /* Load the Interrupt descriptor table */
96 /* Obtain CPU number. */
99 lock cmpxchg %ecx, ap_count
102 /* Setup stacks for each CPU. */
103 movl stack_size, %eax
109 /* Save CPU number for calling the AP entry */
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.
119 /* Determine if one should check microcode versions. */
120 mov microcode_ptr, %edi
122 jz microcode_done /* Bypass if no microde exists. */
124 /* Get the Microcode version. */
127 movl $IA32_BIOS_SIGN_ID, %ecx
131 mov $IA32_BIOS_SIGN_ID, %ecx
133 /* If something already loaded skip loading again. */
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.
147 /* Determine if parallel microcode loading is allowed. */
148 cmpl $0xffffffff, microcode_lock
151 /* Protect microcode loading. */
153 lock btsl $0, microcode_lock
157 /* Load new microcode. */
158 mov $IA32_BIOS_UPDT_TRIG, %ecx
161 /* The microcode pointer is passed in pointing to the header. Adjust
162 * pointer to reflect the payload (header size is 48 bytes). */
168 /* Unconditionally unlock microcode loading. */
169 cmpl $0xffffffff, microcode_lock
173 mov %eax, microcode_lock
177 * Load MSRs. Each entry in the table consists of:
182 mov msr_table_ptr, %edi
187 #if CONFIG(X86_AMD_FIXED_MTRRS)
188 /* Allow modification of RdDram and WrDram bits */
189 mov $SYSCFG_MSR, %ecx
191 or $SYSCFG_MSR_MtrrFixDramModEn, %eax
204 #if CONFIG(X86_AMD_FIXED_MTRRS)
205 mov $SYSCFG_MSR, %ecx
207 and $~SYSCFG_MSR_MtrrFixDramModEn, %eax
212 /* Enable caching. */
214 and $~(CR0_CLEAR_FLAGS_CACHE_ENABLE), %eax
218 /* Enable sse instructions. */
220 orl $(CR4_OSFXSR | CR4_OSXMMEXCPT), %eax
224 pop %edi /* Retrieve cpu index */
225 andl $0xfffffff0, %esp /* ensure stack alignment */
228 /* entry64.inc preserves ebx, esi, edi, ebp */
231 movabs c_handler, %eax