AGESA F15: AMD family15 AGESA code
[coreboot.git] / src / vendorcode / amd / agesa / f15 / gcccar.inc
1 /*
2  * Copyright (C) 2012 Advanced Micro Devices, Inc.
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are met:
7  *     * Redistributions of source code must retain the above copyright
8  *       notice, this list of conditions and the following disclaimer.
9  *     * Redistributions in binary form must reproduce the above copyright
10  *       notice, this list of conditions and the following disclaimer in the
11  *       documentation and/or other materials provided with the distribution.
12  *     * Neither the name of Advanced Micro Devices, Inc. nor the names of
13  *       its contributors may be used to endorse or promote products derived
14  *       from this software without specific prior written permission.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
17  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19  * DISCLAIMED. IN NO EVENT SHALL ADVANCED MICRO DEVICES, INC. BE LIABLE FOR ANY
20  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
21  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
23  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
25  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26  *
27  */
28
29 /******************************************************************************
30 * AMD Generic Encapsulated Software Architecture
31 *
32 * $Workfile:: GccCar.inc    $Revision:: 32932   $
33 *
34 * Description: GccCar.inc - AGESA cache-as-RAM setup Include File for GCC complier
35 *
36 ******************************************************************************/
37
38 .altmacro
39
40 BSP_STACK_BASE_ADDR     =       0x30000         /* Base address for primary cores stack   */
41 BSP_STACK_SIZE          =       0x10000         /* 64KB for BSP core                      */
42 CORE0_STACK_BASE_ADDR   =       0x80000         /* Base address for primary cores stack   */
43 CORE0_STACK_SIZE        =       0x4000          /* 16KB for primary cores                 */
44 CORE1_STACK_BASE_ADDR   =       0x40000         /* Base address for AP cores              */
45 CORE1_STACK_SIZE        =       0x1000          /* 4KB for each AP cores                  */
46
47 APIC_BASE_ADDRESS       =       0x0000001B
48   APIC_BSC              =       8               /* Boot Strap Core  */
49
50 AMD_MTRR_VARIABLE_BASE0  =      0x0200
51 AMD_MTRR_VARIABLE_BASE6  =      0x020C
52 AMD_MTRR_FIX64k_00000    =      0x0250
53 AMD_MTRR_FIX16k_80000    =      0x0258
54 AMD_MTRR_FIX16k_A0000    =      0x0259
55 AMD_MTRR_FIX4k_C0000     =      0x0268
56 AMD_MTRR_FIX4k_C8000     =      0x0269
57 AMD_MTRR_FIX4k_D0000     =      0x026A
58 AMD_MTRR_FIX4k_D8000     =      0x026B
59 AMD_MTRR_FIX4k_E0000     =      0x026C
60 AMD_MTRR_FIX4k_E8000     =      0x026D
61 AMD_MTRR_FIX4k_F0000     =      0x026E
62 AMD_MTRR_FIX4k_F8000     =      0x026F
63
64 AMD_MTRR_DEFTYPE         =      0x02FF
65     WB_DRAM_TYPE         =      0x1E             /* MemType - memory type */
66     MTRR_DEF_TYPE_EN     =      11               /* MtrrDefTypeEn - variable and fixed MTRRs default enabled */
67     MTRR_DEF_TYPE_FIX_EN =      10               /* MtrrDefTypeEn - fixed MTRRs default enabled */
68
69 HWCR                     =      0x0C0010015      /* Hardware Configuration                                                                                                                                       */
70     INVD_WBINVD          =      0x04             /* INVD to WBINVD conversion */
71
72 IORR_BASE                =      0x0C0010016      /* IO Range Regusters Base/Mask, 2 pairs */
73                                                  /*  uses 16h - 19h                                                                                                                                                             */
74 TOP_MEM                  =      0x0C001001A      /* Top of Memory                                                                                                                                                                                */
75 TOP_MEM2                 =      0x0C001001D      /* Top of Memory2                                                                                                                                                                       */
76
77 LS_CFG                   =      0x0C0011020      /* Load-Store Configuration                                                                                                                             */
78     DIS_SS               =     28                /* Family 10h,12h,15h:Disable Streng Store functionality */
79     DIS_STREAM_ST        =     28                /* Family 14h:DisStreamSt - Disable Streaming Store functionality */
80
81 IC_CFG                   =      0x0C0011021      /* Instruction Cache Config Register  */
82     IC_DIS_SPEC_TLB_RLD  =      9                /*   Disable speculative TLB reloads  */
83     DIS_IND              =      14               /*   Family 10-14h:Disable Indirect Branch Predictor */
84     DIS_I_CACHE          =      14               /*   Family 15h:DisICache - Disable Indirect Branch Predictor */
85
86 DC_CFG                   =      0x0C0011022      /* Data Cache Configuration                                                                                                                                   */
87     DC_DIS_SPEC_TLB_RLD      =  4                /*   Disable speculative TLB reloads */
88     DIS_CLR_WBTOL2_SMC_HIT   =  8                /*   self modifying code check buffer bit */
89     DIS_HW_PF                =  13               /*   Hardware prefetches bit                                                                                                                                    */
90
91 DE_CFG                   =      0x0C0011029      /* Decode Configuration */
92     CL_FLUSH_SERIALIZE   =      23               /*   Family 12h,15h: CL Flush Serialization */
93
94 BU_CFG2                  =      0x0C001102A      /* Family 10h: Bus Unit Configuration 2 */
95 CU_CFG2                  =      0x0C001102A      /* Family 15h: Combined Unit Configuration 2 */
96     F10_CL_LINES_TO_NB_DIS  =   15               /*   ClLinesToNbDis - allows WP code to be cached in L2 */
97     IC_DIS_SPEC_TLB_WR      =   35               /*   IcDisSpecTlbWr - ITLB speculative writes */
98
99 CU_CFG3                  =      0x0C001102B      /* Combined Unit Configuration 3 */
100     COMBINE_CR0_CD       =      49               /*   Combine CR0.CD for both cores of a compute unit */
101
102
103 CR0_PE                  = 1           # Protection Enable
104 CR0_NW                  = 29          # Not Write-through
105 CR0_CD                  = 30          # Cache Disable
106 CR0_PG                  = 31          # Paging Enable
107
108 /* CPUID Functions */
109
110 CPUID_MODEL              =      1
111 AMD_CPUID_FMF            =      0x80000001       /* Family Model Features information */
112 AMD_CPUID_APIC           =      0x80000008       /* Long Mode and APIC info., core count */
113
114 NB_CFG                   =      0x0C001001F      /* Northbridge Configuration Register */
115     INIT_APIC_ID_CPU_ID_LO    = 54               /*  InitApicIdCpuIdLo - is core# in high or low half of APIC ID? */
116
117 MTRR_SYS_CFG             =      0x0C0010010      /* System Configuration Register */
118   CHX_TO_DIRTY_DIS       =      16               /*   ChxToDirtyDis    Change to dirty disable  */
119   SYS_UC_LOCK_EN         =      17               /*   SysUcLockEn      System lock command enable */
120   MTRR_FIX_DRAM_EN       =      18               /*   MtrrFixDramEn    MTRR fixed RdDram and WrDram attributes enable */
121   MTRR_FIX_DRAM_MOD_EN   =      19               /*   MtrrFixDramModEn MTRR fixed RdDram and WrDram modification enable */
122   MTRR_VAR_DRAM_EN       =      20               /*   MtrrVarDramEn    MTRR variable DRAM enable */
123   MTRR_TOM2_EN           =      21               /*   MtrrTom2En       MTRR top of memory 2 enable */
124
125 PERF_CONTROL3            =      0x0C0010003      /* Performance event control three */
126     PERF_CONTROL3_RESERVE_L  =  0x00200000       /* Preserve the reserved bits */
127     PERF_CONTROL3_RESERVE_H  =  0x0FCF0          /* Preserve the reserved bits */
128     CONFIG_EVENT_L           =  0x0F0E2          /* All cores with level detection */
129     CONFIG_EVENT_H           =  4                /* Increment count by number of event */
130                                                  /* occured in clock cycle */
131     EVENT_ENABLE             =  22               /* Enable the event */
132 PERF_COUNTER3            =      0x0C0010007      /* Performance event counter three */
133
134 # Local use flags, in upper most byte if ESI
135 FLAG_UNKNOWN_FAMILY     = 24          # Signals that the family# of the installed processor is not recognized
136 FLAG_STACK_REENTRY      = 25          # Signals that the environment has made a re-entry (2nd) call to set up the stack
137 FLAG_IS_PRIMARY         = 26          # Signals that this core is the primary within the comoute unit
138
139 CR0_MASK    = ((1 << CR0_CD) | (1 << CR0_NW))
140 MSR_MASK    = ((1 << MTRR_DEF_TYPE_EN)+(1 << MTRR_DEF_TYPE_FIX_EN))
141
142 /****************************************************************************
143  *
144  *                      CPU MACROS - PUBLIC
145  *
146  ****************************************************************************/
147 .macro   _WRMSR
148         .byte 0x0f, 0x30
149 .endm
150
151 .macro  _RDMSR
152         .byte   0x0F, 0x32
153 .endm
154
155 .macro AMD_CPUID arg0
156   .ifb \arg0
157     mov   $0x1, %eax
158     .byte 0x0F, 0x0A2                     /* Execute instruction */
159     bswap %eax
160     xchg  %ah, %al                        /* Ext model in al now */
161     rol   $0x08, %eax                     /* Ext model in ah, model in al */
162     and   $0x0FFCF, ax                    /* Keep 23:16, 7:6, 3:0 */
163   .else
164     mov   \arg0, %eax
165     .byte 0x0F, 0x0A2
166   .endif
167 .endm
168
169 /****************************************************************************
170 *
171 * AMD_ENABLE_STACK_FAMILY_HOOK Macro - Stackless
172 *
173 *   Set any family specific controls needed to enable the use of
174 *   cache as general storage before main memory is available.
175 *
176 * Inputs:
177 *       none
178 * Outputs:
179 *       none
180  ****************************************************************************/
181 .macro  AMD_ENABLE_STACK_FAMILY_HOOK
182
183     AMD_ENABLE_STACK_FAMILY_HOOK_F10
184     AMD_ENABLE_STACK_FAMILY_HOOK_F12
185     AMD_ENABLE_STACK_FAMILY_HOOK_F14
186     AMD_ENABLE_STACK_FAMILY_HOOK_F15
187 .endm
188
189 /****************************************************************************
190 *
191 * AMD_DISABLE_STACK_FAMILY_HOOK Macro - Stackless
192 *
193 *   Return any family specific controls to their 'standard'
194 *   settings for using cache with main memory.
195 *
196 * Inputs:
197 *       none
198 * Outputs:
199 *       none
200  ****************************************************************************/
201 .macro  AMD_DISABLE_STACK_FAMILY_HOOK
202
203     AMD_DISABLE_STACK_FAMILY_HOOK_F10
204     AMD_DISABLE_STACK_FAMILY_HOOK_F12
205     AMD_DISABLE_STACK_FAMILY_HOOK_F14
206     AMD_DISABLE_STACK_FAMILY_HOOK_F15
207
208 .endm
209
210 /****************************************************************************
211 *
212 * GET_NODE_ID_CORE_ID Macro - Stackless
213 *
214 *   Read family specific values to determine the node and core
215 *   numbers for the core executing this code.
216 *
217 * Inputs:
218 *     none
219 * Outputs:
220 *     SI[7:0] = Core# (0..N, relative to node)
221 *     SI[15:8]= Node# (0..N)
222 *     SI[23:16]= reserved
223 *     SI[24]=   flag: 1=Family Unrecognized
224 *     SI[25]=   flag: 1=Interface re-entry call
225 *     SI[26]=   flag: 1=Core is primary of compute unit
226 *     SI[31:27]= reserved, =0
227 ****************************************************************************/
228 .macro  GET_NODE_ID_CORE_ID
229     LOCAL  node_core_exit
230
231     mov     $-1, %si
232     GET_NODE_ID_CORE_ID_F10
233     GET_NODE_ID_CORE_ID_F12
234     GET_NODE_ID_CORE_ID_F14
235     GET_NODE_ID_CORE_ID_F15
236     /*
237      * Check for unrecognized Family
238      */
239     cmp     $-1, %si                      # Has family (node/core) already been discovered?
240     jnz     node_core_exit              # Br if yes
241
242    mov     $((1 << FLAG_UNKNOWN_FAMILY)+(1 << FLAG_IS_PRIMARY)), %esi # No, Set error code, Only let BSP continue
243
244     mov     $APIC_BASE_ADDRESS, %ecx      # MSR:0000_001B
245     _RDMSR
246     bt      $APIC_BSC, %eax               # Is this the BSC?
247     jc      node_core_exit              # Br if yes
248     hlt                                 # Kill APs
249 node_core_exit:
250
251 .endm
252
253 /****************************************************************************
254 ##                      Family 10h MACROS
255 ##***************************************************************************
256 #---------------------------------------------------
257 #
258 # AMD_ENABLE_STACK_FAMILY_HOOK_F10 Macro - Stackless
259 #
260 #   Set any family specific controls needed to enable the use of
261 #   cache as general storage before main memory is available.
262 #
263 # Inputs:
264 #       ESI - node#, core#, flags from GET_NODE_ID_CORE_ID
265 # Outputs:
266 #       none
267 #
268 # Family 10h requirements (BKDG section 2.3.3):
269 #   * Paging disabled
270 #   * MSRC001_0015[INVDWBINVD]=0
271 #   * MSRC001_1021[DIS_IND]=1
272 #   * MSRC001_1021[DIS_SPEC_TLB_RLD]=1
273 #   * MSRC001_1022[DIS_SPEC_TLB_RLD]=1
274 #   * MSRC001_1022[DIS_CLR_WBTOL2_SMC_HIT]=1
275 #   * MSRC001_1022[DIS_HW_PF]=1
276 #   * MSRC001_102A[IcDisSpecTlbWr]=1
277 #   * MSRC001_102A[ClLinesToNbDis]=1
278 #   * No INVD or WBINVD, no exceptions, page faults or interrupts
279 ****************************************************************************/
280 .macro AMD_ENABLE_STACK_FAMILY_HOOK_F10
281     LOCAL   fam10_enable_stack_hook_exit
282
283     AMD_CPUID   $CPUID_MODEL
284     shr     $20, %eax                     # AL = cpu extended family
285     cmp     $0x01, %al                      # Is this family 10h?
286     jnz     fam10_enable_stack_hook_exit # Br if no
287
288     mov     $DC_CFG, %ecx                 # MSR:C001_1022
289     _RDMSR
290     bts     $DC_DIS_SPEC_TLB_RLD, %eax     # Turn on Disable speculative DTLB reloads bit
291     bts     $DIS_CLR_WBTOL2_SMC_HIT, %eax  # Turn on Disable the self modifying code check buffer bit
292     bts     $DIS_HW_PF, %eax               # Turn on Disable hardware prefetches bit
293     _WRMSR
294
295     dec     %cx                          # MSR:C001_1021
296     _RDMSR
297     bts     $IC_DIS_SPEC_TLB_RLD, %eax     # Turn on Disable speculative TLB reloads bit
298     bts     $DIS_IND, %eax                 # Turn on Disable indirect branch predictor
299     _WRMSR
300
301     mov     $BU_CFG2, %ecx                # MSR C001_102A
302     _RDMSR
303     bts     $F10_CL_LINES_TO_NB_DIS, %eax   # Allow BIOS ROM to be cached in the IC
304     bts     $(IC_DIS_SPEC_TLB_WR-32), %edx  #Disable speculative writes to the ITLB
305     _WRMSR
306
307     mov     $HWCR, %ecx                    # MSR C001_0015
308     _RDMSR
309     bt      $FLAG_STACK_REENTRY, %esi                 # Check if stack has already been set
310     jc      fam10_skipClearingBit4
311     btr     $INVD_WBINVD, %eax             # disable INVD -> WBINVD conversion
312     _WRMSR
313
314 fam10_skipClearingBit4:
315     mov %esi, %eax                         # load core#
316     or %al, %al                       # If (BSP)
317     jne  fam10_enable_stack_hook_exit
318     mov     $PERF_COUNTER3, %ecx       #   Select performance counter three
319                                         #   to count number of CAR evictions
320     xor     %eax, %eax                #   Initialize the lower part of the counter to zero
321     xor     %edx, %edx                #   Initializa the upper part of the counter to zero
322     _WRMSR                          #   Save it
323     mov     $PERF_CONTROL3, %ecx      #   Select the event control three
324     _RDMSR                          #   Get the current setting
325     and     $PERF_CONTROL3_RESERVE_L, %eax   # Preserve the reserved bits
326     or      $CONFIG_EVENT_L, %eax      #   Set the lower part of event register to
327                                         #   select CAR Corruption occurred by any cores
328     and     $PERF_CONTROL3_RESERVE_H, %dx   # Preserve the reserved bits
329     or      $CONFIG_EVENT_H, %dx       #   Set the upper part of event register
330     _WRMSR                          #   Save it
331     bts     $EVENT_ENABLE, %eax        #   Enable it
332     _WRMSR                          #   Save it
333
334 fam10_enable_stack_hook_exit:
335 .endm
336
337 /****************************************************************************
338 *
339 * AMD_DISABLE_STACK_FAMILY_HOOK_F10 Macro - Stackless
340 *
341 *   Return any family specific controls to their 'standard'
342 *   settings for using cache with main memory.
343 *
344 * Inputs:
345 *       ESI - [31:24] flags; [15,8]= Node#; [7,0]= core#
346 * Outputs:
347 *       none
348 *
349 * Family 10h requirements:
350 *   * INVD or WBINVD
351 *   * MSRC001_0015[INVD_WBINVD]=1
352 *   * MSRC001_1021[DIS_IND]=0
353 *   * MSRC001_1021[DIS_SPEC_TLB_RLD]=0
354 *   * MSRC001_1022[DIS_SPEC_TLB_RLD]=0
355 *   * MSRC001_1022[DIS_CLR_WBTOL2_SMC_HIT]=0
356 *   * MSRC001_1022[DIS_HW_PF]=0
357 *   * MSRC001_102A[IcDisSpecTlbWr]=0
358 *   * MSRC001_102A[ClLinesToNbDis]=0
359 *****************************************************************************/
360
361 .macro  AMD_DISABLE_STACK_FAMILY_HOOK_F10
362     LOCAL   fam10_disable_stack_hook_exit
363
364     AMD_CPUID   $CPUID_MODEL
365     shr     $20, %eax                     # AL = cpu extended family
366     cmp     $0x01, %al                     # Is this family 10h?
367     jnz     fam10_disable_stack_hook_exit # Br if no
368
369     mov     $DC_CFG, %ecx                 # MSR:C001_1022
370     _RDMSR
371     btr     $DC_DIS_SPEC_TLB_RLD, %eax    # Enable speculative TLB reloads
372     btr     $DIS_CLR_WBTOL2_SMC_HIT, %eax # Allow self modifying code check buffer
373     btr     $DIS_HW_PF, %eax              # Allow hardware prefetches
374     _WRMSR
375
376     dec     %cx                          # MSR:C001_1021
377     _RDMSR
378     btr     $DIS_IND, %eax                # Turn on indirect branch predictor
379     btr     $IC_DIS_SPEC_TLB_RLD, %eax    # Turn on speculative TLB reloads
380     _WRMSR
381
382     mov     $BU_CFG2, %ecx                # MSR:C001_102A
383     _RDMSR
384 /*
385  * BTS error if SBIOS `allows WP code to be cached', but copying
386  * ramstage/payloads from ROM to RAM will be very slow if disable it here.
387  *
388  * TODO: disable `allows WP code to be cached' after the ROM to RAM copying.
389  */
390 //    btr     $F10_CL_LINES_TO_NB_DIS, %eax  # Return L3 to normal mode
391     btr     $(IC_DIS_SPEC_TLB_WR-32), %edx #Re-enable speculative writes to the ITLB
392     _WRMSR
393
394     #--------------------------------------------------------------------------
395     # Begin critical sequence in which EAX, BX, ECX, and EDX must be preserved.
396     #--------------------------------------------------------------------------
397
398     mov     $HWCR, %ecx                    # MSR:0000_0015
399     _RDMSR
400     mov     %ax, %bx                      # Save INVD -> WBINVD bit
401     btr    $INVD_WBINVD, %eax            # Disable INVD -> WBINVD conversion for the invd instruction.
402     _WRMSR
403     wbinvd                                # Clear the cache tag RAMs
404     mov    %bx, %ax                      # Restore INVD -> WBINVD bit
405     _WRMSR
406
407     #--------------------------------------------------------------------------
408     # End critical sequence in which EAX, BX, ECX, and EDX must be preserved.
409     #--------------------------------------------------------------------------
410
411     mov     $PERF_CONTROL3, %ecx          # Select the event control three
412     _RDMSR                              # Retrieve the current value
413     btc     $EVENT_ENABLE, %eax           # Is event enable, complement it as well
414     jnc     fam10_disable_stack_hook_exit # No
415     cmp     $CONFIG_EVENT_L, %ax          # Is the lower part of event set to capture the CAR Corruption
416     jne     fam10_disable_stack_hook_exit # No
417     cmp     $CONFIG_EVENT_H, %dl        # Is the upper part of event set to capture the CAR Corruption
418     jne     fam10_disable_stack_hook_exit # No
419     _WRMSR                              # Disable the event
420
421 fam10_disable_stack_hook_exit:
422 .endm
423
424 /****************************************************************************
425 *
426 * GET_NODE_ID_CORE_ID_F10 Macro - Stackless
427 *
428 *   Read family specific values to determine the node and core
429 *   numbers for the core executing this code.
430 *
431 * Inputs:
432 *     none
433 * Outputs:
434 *     SI = core#, node# & flags (see GET_NODE_ID_CORE_ID macro above)
435 *****************************************************************************/
436 .macro  GET_NODE_ID_CORE_ID_F10
437
438     LOCAL   node_core_f10_exit
439     LOCAL   node_core_f10_AP
440
441     cmp     $-1, %si                      # Has node/core already been discovered?
442     jnz     node_core_f10_exit          # Br if yes
443
444     AMD_CPUID   $CPUID_MODEL
445     shr     $20, %eax                     # AL = cpu extended family
446     cmp     $0x01, %al                     # Is this family 10h?
447     jnz     node_core_f10_exit          # Br if no
448
449     xor     %esi, %esi                    # Assume BSC, clear flags
450     mov     $APIC_BASE_ADDRESS, %ecx      # MSR:0000_001B
451     _RDMSR
452     bt      $APIC_BSC, %eax             # Is this the BSC?
453     jnc      node_core_f10_AP            # Br if no
454
455         # This is the BSP.
456     # Enable routing tables on BSP (just in case the HT init code has not yet enabled them)
457     mov     $0x8000C06C, %eax             # PCI address for D18F0x6C Link Initialization Control Register
458     mov     $0x0CF8, %dx
459     out     %eax, %dx
460     add     $4, %dx
461     in      %dx, %eax
462     btr     $0, %eax                      # Set LinkInitializationControl[RouteTblDis] = 0
463     out     %eax, %dx
464     jmp     1f          #
465
466 node_core_f10_AP:
467     #
468     # This is an AP. Routing tables have been enabled by the HT Init process.
469     # Also, the MailBox register was set by the BSP during early init
470     #   The Mailbox register content is formatted as follows:
471     #         UINT32 Node:4#          // The node id of Core's node.
472     #         UINT32 Socket:4#        // The socket of this Core's node.
473     #         UINT32 Module:2#        // The internal module number for Core's node.
474     #         UINT32 ModuleType:2#    // Single Module = 0, Multi-module = 1.
475     #         UINT32 :20#             // Reserved
476     #
477     mov     $0x0C0000408, %ecx             # Read the family 10h mailbox
478     _RDMSR                              #        MC4_MISC1[63:32]
479     mov     %dx, %si                      # SI = raw mailbox contents (will extract node# from this)
480     shr     $24, %ebx                     # BL = CPUID Fn0000_0001_EBX[LocalApicId]
481     mov     %bx, %di                      # DI = Initial APIC ID (will extract core# from this)
482
483     AMD_CPUID   $AMD_CPUID_APIC          #
484     shr     $4, %ch                       # CH = ApicIdSize, #bits in APIC ID that show core#
485     inc     %cl                          # CL = Number of enabled cores in the socket
486     mov     %cx, %bx
487
488     mov     $NB_CFG, %ecx                 # MSR:C001_001F
489     _RDMSR                              # EDX has InitApicIdCpuIdLo bit
490
491     mov     %bh, %cl                      # CL = APIC ID size
492     mov     $1, %al                       # Convert APIC ID size to an AND mask
493     shl     %cl, %al                      # AL = 2^APIC ID size
494     dec     %al                          # AL = mask for relative core number
495     xor     %ah, %ah                      # AX = mask for relative core number
496     bt      $(INIT_APIC_ID_CPU_ID_LO-32), %edx # InitApicIdCpuIdLo == 1?
497     #.if (!carry?)                       # Br if yes
498     jc      0f
499         mov     $8, %ch                   # Calculate core number shift count
500         sub     %cl, %ch                  # CH = core shift count
501         mov     %ch, %cl
502         shr     %cl, %di                  # Right justify core number
503     #.endif
504    0:
505     and     %ax, %di                      # DI = socket-relative core number
506
507     mov     %si, %cx                      # CX = raw mailbox value
508     shr     $10, %cx                      # CL[1:0] = ModuleType or #nodes per socket (0-SCM, 1-MCM)
509     and     $3, %cl                       # Isolate ModuleType
510     xor     %bh, %bh                      # BX = Number of enabled cores in the socket
511     shr     %cl, %bx                      # BX = Number of enabled cores per node
512     xor     %dx, %dx                      # Clear upper word for div
513     mov     %di, %ax                      # AX = socket-relative core number
514     div     %bx                          # DX = node-relative core number
515     movzx   %si, %eax                     # prepare return value, [23:16]=shared Core# (=0, not shared)
516     and     $0x000F, %ax                   # AX = node number
517     shl     $8, %ax                       # [15:8]=node#
518     mov     %dl, %al                      # [7:0]=core# (relative to node)
519     mov     %eax, %esi                    # ESI = return value
520 1:
521  bts     $FLAG_IS_PRIMARY, %esi        # all Family 10h cores are primary
522 node_core_f10_exit:
523 .endm
524
525
526 /*****************************************************************************
527 **                      Family 12h MACROS
528 *****************************************************************************/
529 /*****************************************************************************
530 *
531 * AMD_ENABLE_STACK_FAMILY_HOOK_F12 Macro - Stackless
532 *
533 *   Set any family specific controls needed to enable the use of
534 *   cache as general storage before main memory is available.
535 *
536 * Inputs:
537 *       ESI - node#, core#, flags from GET_NODE_ID_CORE_ID
538 * Outputs:
539 *       none
540 *
541 * Family 12h requirements (BKDG section 2.3.3):
542 *   The following requirements must be satisfied prior to using the cache as general storage:
543 *   * Paging must be disabled.
544 *   * MSRC001_0015[INVD_WBINVD]=0
545 *   * MSRC001_1020[DIS_SS]=1
546 *   * MSRC001_1021[DIS_SPEC_TLB_RLD]=1
547 *   * MSRC001_1022[DIS_SPEC_TLB_RLD]=1
548 *   * MSRC001_1022[DIS_CLR_WBTOL2_SMC_HIT]=1
549 *   * MSRC001_1022[DIS_HW_PF]=1
550 *   * MSRC001_1029[ClflushSerialize]=1
551 *   * No INVD or WBINVD, no exceptions, page faults or interrupts
552 *****************************************************************************/
553 .macro  AMD_ENABLE_STACK_FAMILY_HOOK_F12
554     LOCAL   fam12_enable_stack_hook_exit
555
556     AMD_CPUID   $CPUID_MODEL
557     shr     $20, %eax                     # AL = cpu extended family
558     cmp     $0x03, %al                     # Is this family 12h?
559     jnz     fam12_enable_stack_hook_exit # Br if no
560
561     mov     $DC_CFG, %ecx                 # MSR:C001_1022
562     _RDMSR
563     bts     $DC_DIS_SPEC_TLB_RLD, %eax    # Disable speculative DC-TLB reloads
564     bts     $DIS_CLR_WBTOL2_SMC_HIT, %eax # Disable self modifying code check buffer
565     bts     $DIS_HW_PF, %eax              # Disable hardware prefetches
566     _WRMSR
567
568     dec     %cx   #IC_CFG                # MSR:C001_1021
569     _RDMSR
570     bts     $IC_DIS_SPEC_TLB_RLD, %eax    # Disable speculative IC-TLB reloads
571     _WRMSR
572
573     dec     %cx   #LS_CFG                # MSR:C001_1020
574     _RDMSR
575     bts     $DIS_SS, %eax                 # Disabled Streaming store functionality
576     _WRMSR
577
578     mov     $HWCR, %ecx                   # MSR C001_0015
579     _RDMSR
580     bt      $FLAG_STACK_REENTRY , %esi                 # Check if stack has already been set
581     jc      fam12_skipClearingBit4
582     btr     $INVD_WBINVD, %eax            # disable INVD -> WBINVD conversion
583     _WRMSR
584
585 fam12_skipClearingBit4:
586     mov     $DE_CFG, %ecx                 # MSR:C001_1029
587     _RDMSR
588     bts     $CL_FLUSH_SERIALIZE, %eax     # Serialize all CL Flush actions
589     _WRMSR
590
591 fam12_enable_stack_hook_exit:
592 .endm
593
594 /*****************************************************************************
595 *
596 * AMD_DISABLE_STACK_FAMILY_HOOK_F12 Macro - Stackless
597 *
598 *   Return any family specific controls to their 'standard'
599 *   settings for using cache with main memory.
600 *
601 * Inputs:
602 *       ESI - [31:24] flags; [15,8]= Node#; [7,0]= core#
603 * Outputs:
604 *       none
605 *
606 * Family 12h requirements:
607 *   * INVD or WBINVD
608 *   * MSRC001_0015[INVD_WBINVD]=1
609 *   * MSRC001_1020[DIS_SS]=0
610 *   * MSRC001_1021[IC_DIS_SPEC_TLB_RLD]=0
611 *   * MSRC001_1022[DC_DIS_SPEC_TLB_RLD]=0
612 *   * MSRC001_1022[DIS_CLR_WBTOL2_SMC_HIT]=0
613 *   * MSRC001_1022[DIS_HW_PF]=0
614 *   * MSRC001_1029[ClflushSerialize]=0
615 *****************************************************************************/
616 .macro  AMD_DISABLE_STACK_FAMILY_HOOK_F12
617     LOCAL   fam12_disable_stack_hook_exit
618
619     AMD_CPUID   $CPUID_MODEL
620     shr     $20, %eax                     # AL = cpu extended family
621     cmp     $0x03, %al                     # Is this family 12h?
622     jnz     fam12_disable_stack_hook_exit # Br if no
623
624     mov     $DC_CFG, %ecx                 # MSR:C001_1022
625     _RDMSR
626     btr     $DC_DIS_SPEC_TLB_RLD, %eax    # Turn on speculative DC-TLB reloads
627     btr     $DIS_CLR_WBTOL2_SMC_HIT, %eax # Enable self modifying code check buffer
628     btr     $DIS_HW_PF, %eax              # Enable Hardware prefetches
629     _WRMSR
630
631     dec     %cx   #IC_CFG                # MSR:C001_1021
632     _RDMSR
633     btr     $IC_DIS_SPEC_TLB_RLD, %eax    # Turn on speculative IC-TLB reloads
634     _WRMSR
635
636     dec     %cx   #LS_CFG                # MSR:C001_1020
637     _RDMSR
638     btr     $DIS_SS, %eax                 # Turn on Streaming store functionality
639     _WRMSR
640
641     mov     $DE_CFG, %ecx                 # MSR:C001_1029
642     _RDMSR
643     btr     $CL_FLUSH_SERIALIZE, %eax
644     _WRMSR
645
646     #--------------------------------------------------------------------------
647     # Begin critical sequence in which EAX, BX, ECX, and EDX must be preserved.
648     #--------------------------------------------------------------------------
649
650     mov     $HWCR, %ecx                    # MSR:0000_0015h
651     _RDMSR
652     mov     %ax, %bx                      # Save INVD -> WBINVD bit
653     btr     $INVD_WBINVD, %eax            # Disable INVD -> WBINVD conversion
654     _WRMSR
655     invd                                # Clear the cache tag RAMs
656     mov     %bx, %ax                      # Restore INVD -> WBINVD bit
657     _WRMSR
658
659     #--------------------------------------------------------------------------
660     # End critical sequence in which EAX, BX, ECX, and EDX must be preserved.
661     #--------------------------------------------------------------------------
662
663 fam12_disable_stack_hook_exit:
664 .endm
665
666 /*****************************************************************************
667 *
668 * GET_NODE_ID_CORE_ID_F12 Macro - Stackless
669 *
670 *   Read family specific values to determine the node and core
671 *   numbers for the core executing this code.
672 *
673 * Inputs:
674 *     none
675 * Outputs:
676 *     SI = core#, node# & flags (see GET_NODE_ID_CORE_ID macro above)
677 *****************************************************************************/
678 .macro  GET_NODE_ID_CORE_ID_F12
679
680     LOCAL   node_core_f12_exit
681
682     cmp     $-1, %si                      # Has node/core already been discovered?
683     jnz     node_core_f12_exit          # Br if yes
684
685     AMD_CPUID   $CPUID_MODEL
686     shr     $20, %eax                     # AL = cpu extended family
687     cmp     $0x03, %al                     # Is this family 12h?
688     jnz     node_core_f12_exit          # Br if no
689
690     shr     $24, %ebx                     # CPUID_0000_0001_EBX[31:24]: initial local APIC physical ID
691     bts     $FLAG_IS_PRIMARY, %ebx        # all family 12h cores are primary
692     mov     %ebx, %esi                    # ESI = Node#=0, core number
693 node_core_f12_exit:
694 .endm
695
696 /*****************************************************************************
697 **                      Family 14h MACROS
698 *****************************************************************************/
699 /*****************************************************************************
700 *
701 * AMD_ENABLE_STACK_FAMILY_HOOK_F14 Macro - Stackless
702 *
703 *   Set any family specific controls needed to enable the use of
704 *   cache as general storage before main memory is available.
705 *
706 * Inputs:
707 *       ESI - node#, core#, flags from GET_NODE_ID_CORE_ID
708 * Outputs:
709 *       none
710 *
711 * Family 14h requirements (BKDG section 2.3.3):
712 *   * Paging must be disabled.
713 *   * MSRC001_0015[INVD_WBINVD]=0.
714 *   * MSRC001_1020[DisStreamSt]=1.
715 *   * MSRC001_1021[DIS_SPEC_TLB_RLD]=1. Disable speculative ITLB reloads.
716 *   * MSRC001_1022[DIS_HW_PF]=1.
717 *   * No INVD or WBINVD, no exceptions, page faults or interrupts
718 *****************************************************************************/
719 .macro  AMD_ENABLE_STACK_FAMILY_HOOK_F14
720     LOCAL   fam14_enable_stack_hook_exit
721
722     AMD_CPUID   $CPUID_MODEL
723     shr     $20, %eax                     # AL = cpu extended family
724     cmp     $0x05, %al                     # Is this family 14h?
725     jnz     fam14_enable_stack_hook_exit # Br if no
726
727     mov     $DC_CFG, %ecx                 # MSR:C001_1022
728     _RDMSR
729     bts     $DIS_HW_PF, %eax              # Disable hardware prefetches
730     _WRMSR
731
732     dec     %cx  #IC_CFG                 # MSR:C001_1021
733     _RDMSR
734     bts     $IC_DIS_SPEC_TLB_RLD, %eax    # Disable speculative TLB reloads
735     _WRMSR
736
737     dec     %cx  #LS_CFG                 # MSR:C001_1020
738     _RDMSR
739     bts     $DIS_STREAM_ST, %eax          # Disabled Streaming store functionality
740     _WRMSR
741
742     mov     $HWCR, %ecx                   # MSR C001_0015
743     _RDMSR
744     bt      $FLAG_STACK_REENTRY, %esi     # Check if stack has already been set
745     jc      fam14_skipClearingBit4
746     btr     $INVD_WBINVD, %eax            # Disable INVD -> WBINVD conversion
747     _WRMSR
748 fam14_skipClearingBit4:                 # Keeping this label
749
750 fam14_enable_stack_hook_exit:
751 .endm
752
753 /*****************************************************************************
754 *
755 * AMD_DISABLE_STACK_FAMILY_HOOK_F14 Macro - Stackless
756 *
757 *   Return any family specific controls to their 'standard'
758 *   settings for using cache with main memory.
759 *
760 * Inputs:
761 *       ESI - [31:24] flags; [15,8]= Node#; [7,0]= core#
762 * Outputs:
763 *       none
764 *
765 * Family 14h requirements:
766 *   * INVD or WBINVD
767 *   * MSRC001_0015[INVD_WBINVD]=1.
768 *   * MSRC001_1020[DisStreamSt]=0.
769 *   * MSRC001_1021[DIS_SPEC_TLB_RLD]=0.
770 *   * MSRC001_1022[DIS_HW_PF]=0.
771 *****************************************************************************/
772 .macro  AMD_DISABLE_STACK_FAMILY_HOOK_F14
773     LOCAL   fam14_disable_stack_hook_exit
774
775     AMD_CPUID   $CPUID_MODEL
776     shr     $20, %eax                     # AL = cpu extended family
777     cmp     $0x05, %al                     # Is this family 14h?
778     jnz     fam14_disable_stack_hook_exit # Br if no
779
780     mov     $LS_CFG, %ecx                 # MSR:C001_1020
781     _RDMSR
782     btr     $DIS_STREAM_ST, %eax          # Turn on Streaming store functionality
783     _WRMSR
784
785     inc     %cx  #IC_CFG                 # MSR:C001_1021
786     _RDMSR
787     btr     $IC_DIS_SPEC_TLB_RLD, %eax    # Turn on speculative DC-TLB reloads
788     _WRMSR
789
790     inc     %cx  #DC_CFG                 # MSR:C001_1022
791     _RDMSR
792     btr     $DIS_HW_PF, %eax              # Turn on hardware prefetches
793     _WRMSR
794
795     #--------------------------------------------------------------------------
796     # Begin critical sequence in which EAX, BX, ECX, and EDX must be preserved.
797     #--------------------------------------------------------------------------
798
799     mov     $HWCR, %ecx                    # MSR:C001_0015h
800     _RDMSR
801     btr     $INVD_WBINVD, %eax            # Disable INVD -> WBINVD conversion
802     _WRMSR
803     invd                                # Clear the cache tag RAMs
804     bts     $INVD_WBINVD, %eax            # Turn on Conversion of INVD to WBINVD
805     _WRMSR
806
807     #--------------------------------------------------------------------------
808     # End critical sequence in which EAX, BX, ECX, and EDX must be preserved.
809     #--------------------------------------------------------------------------
810
811 fam14_disable_stack_hook_exit:
812 .endm
813
814 /*****************************************************************************
815 *
816 * GET_NODE_ID_CORE_ID_F14 Macro - Stackless
817 *
818 *   Read family specific values to determine the node and core
819 *   numbers for the core executing this code.
820 *
821 * Inputs:
822 *     none
823 * Outputs:
824 *     SI = core#, node# & flags (see GET_NODE_ID_CORE_ID macro above)
825 *****************************************************************************/
826 .macro  GET_NODE_ID_CORE_ID_F14
827
828     LOCAL   node_core_f14_exit
829
830     cmp     $0x-1, %si                      # Has node/core already been discovered?
831     jnz     node_core_f14_exit          # Br if yes
832
833     AMD_CPUID   $CPUID_MODEL
834     shr     $20, %eax                     # AL = cpu extended family
835     cmp     $0x05, %al                     # Is this family 14h?
836     jnz     node_core_f14_exit          # Br if no
837
838     xor     %esi, %esi                    # Node must be 0
839     bts     $FLAG_IS_PRIMARY, %esi        # all family 14h cores are primary
840     mov     $APIC_BASE_ADDRESS, %ecx      # MSR:0000_001B
841     _RDMSR
842     bt      $APIC_BSC, %eax               # Is this the BSC?
843     jc      node_core_f14_exit          # Br if yes
844     inc     %si                          # Set core to 1
845 node_core_f14_exit:
846 .endm
847
848
849
850 /*****************************************************************************
851 **                      Family 15h MACROS
852 *****************************************************************************/
853 /*****************************************************************************
854 *
855 * AMD_ENABLE_STACK_FAMILY_HOOK_F15 Macro - Stackless
856 *
857 *   Set any family specific controls needed to enable the use of
858 *   cache as general storage before main memory is available.
859 *
860 * Inputs:
861 *       ESI - node#, core#, flags from GET_NODE_ID_CORE_ID
862 * Outputs:
863 *       none
864 *
865 * Family 15h requirements (BKDG #42301 section 2.3.3):
866 *   * Paging must be disabled.
867 *   * MSRC001_0015[INVD_WBINVD]=0
868 *   * MSRC001_1020[DisSS]=1
869 *   * MSRC001_1021[DIS_SPEC_TLB_RLD]=1
870 *   * MSRC001_1022[DIS_SPEC_TLB_RLD]=1
871 *   * MSRC001_1022[DisHwPf]=1
872 *   * No INVD or WBINVD, no exceptions, page faults or interrupts
873 *****************************************************************************/
874 .macro  AMD_ENABLE_STACK_FAMILY_HOOK_F15
875     LOCAL   fam15_enable_stack_hook_exit
876
877     AMD_CPUID   $CPUID_MODEL
878     shr     $20, %eax                     # AL = cpu extended family
879     cmp     $0x06, %al                     # Is this family 15h?
880     jnz     fam15_enable_stack_hook_exit # Br if no
881
882     bt      $FLAG_STACK_REENTRY , %esi                 # Check if stack has already been set
883     jc      fam15_skipClearingBit4
884     mov     $HWCR, %ecx                   # MSR C001_0015
885     _RDMSR
886     btr     $INVD_WBINVD, %eax            # disable INVD -> WBINVD conversion
887     _WRMSR
888
889 fam15_skipClearingBit4:
890     mov     $LS_CFG, %ecx                 # MSR:C001_1020
891     _RDMSR
892     bts     $DIS_SS, %eax                 # Turn on Streaming store functionality disabled bit
893     _WRMSR
894
895     inc     %ecx  #IC_CFG                # MSR:C001_1021
896     _RDMSR
897     bts     $IC_DIS_SPEC_TLB_RLD, %eax    # Turn on Disable speculative IC-TLB reloads bit
898     _WRMSR
899
900     inc     %ecx  #DC_CFG                # MSR:C001_1022
901     _RDMSR
902     bts     $DC_DIS_SPEC_TLB_RLD, %eax    # Turn on Disable speculative DC-TLB reloads bit
903     bts     $DIS_HW_PF, %eax              # Turn on Disable hardware prefetches bit
904     _WRMSR
905
906     mov     $CU_CFG3, %ecx                # MSR:C001_102B
907     _RDMSR
908     btr     $(COMBINE_CR0_CD - 32), %edx         # Clear CombineCr0Cd bit
909     _WRMSR
910
911 fam15_enable_stack_hook_exit:
912 .endm
913
914
915 /*****************************************************************************
916 *
917 * AMD_DISABLE_STACK_FAMILY_HOOK_F15 Macro - Stackless
918 *
919 *   Return any family specific controls to their 'standard'
920 *   settings for using cache with main memory.
921 *
922 * Inputs:
923 *       ESI - [31:24] flags; [15,8]= Node#; [7,0]= core#
924 * Outputs:
925 *       none
926 *
927 * Family 15h requirements:
928 *   * INVD or WBINVD
929 *   * MSRC001_0015[INVD_WBINVD]=1
930 *   * MSRC001_1020[DisSS]=0
931 *   * MSRC001_1021[DIS_SPEC_TLB_RLD]=0
932 *   * MSRC001_1022[DIS_SPEC_TLB_RLD]=0
933 *   * MSRC001_1022[DIS_HW_PF]=0
934 *****************************************************************************/
935 .macro  AMD_DISABLE_STACK_FAMILY_HOOK_F15
936     LOCAL   fam15_disable_stack_hook_exit
937
938     AMD_CPUID   $CPUID_MODEL
939     mov     %eax, %ebx                    # Save revision info to EBX
940     shr     $20, %eax                     # AL = cpu extended family
941     cmp     $0x06, %al                     # Is this family 15h?
942     jnz     fam15_disable_stack_hook_exit # Br if no
943
944     mov     $LS_CFG, %ecx                 # MSR:C001_1020
945     #.if (ebx != 00600F00h)              ; Is this rev A0?
946     cmp $0x00600F00, %ebx
947     jz  0f
948     _RDMSR
949     btr     $DIS_SS, %eax                 # Turn on Streaming store functionality
950     _WRMSR
951     #.endif
952     0:                              # End workaround for errata 495 and 496
953
954     inc     %ecx  #IC_CFG                # MSR:C001_1021
955     _RDMSR
956     btr     $IC_DIS_SPEC_TLB_RLD, %eax    # Turn on speculative TLB reloads
957     _WRMSR
958
959     inc     %ecx  #DC_CFG                # MSR:C001_1022
960     _RDMSR
961     btr     $DC_DIS_SPEC_TLB_RLD, %eax    # Turn on speculative TLB reloads
962     #.if (ebx != 00600F00h)              # Is this rev A0?
963     cmp $0x00600F00, %ebx
964     jz  0f
965     btr     $DIS_HW_PF, %eax              # Turn on hardware prefetches
966     #.endif                              # End workaround for erratum 498
967     0:
968     _WRMSR
969     #--------------------------------------------------------------------------
970     # Begin critical sequence in which EAX, BX, ECX, and EDX must be preserved.
971     #--------------------------------------------------------------------------
972
973     bt      $FLAG_IS_PRIMARY, %esi
974     #.if (carry?)                        # Only clear cache from primary core
975     jnc 0f
976     mov     $HWCR, %ecx                    # MSR:C001_0015h
977     _RDMSR
978     btr     $INVD_WBINVD, %eax            # Disable INVD -> WBINVD conversion
979     _WRMSR
980     wbinvd                                # Clear the cache tag RAMs
981     bts     $INVD_WBINVD, %eax            # Turn on Conversion of INVD to WBINVD
982     _WRMSR
983     #.endif                              # end
984     0:
985
986     #--------------------------------------------------------------------------
987     # End critical sequence in which EAX, BX, ECX, and EDX must be preserved.
988     #--------------------------------------------------------------------------
989
990     mov     $CU_CFG3, %ecx                # MSR:C001_102B
991     _RDMSR
992     bts     $(COMBINE_CR0_CD - 32), %eax         # Set CombineCr0Cd bit
993     _WRMSR
994
995 fam15_disable_stack_hook_exit:
996 .endm
997
998
999 /*****************************************************************************
1000 *
1001 * GET_NODE_ID_CORE_ID_F15 Macro - Stackless
1002 *
1003 *   Read family specific values to determine the node and core
1004 *   numbers for the core executing this code.
1005 *
1006 * Inputs:
1007 *     none
1008 * Outputs:
1009 *     SI = core#, node# & flags (see GET_NODE_ID_CORE_ID macro above)
1010 *****************************************************************************/
1011 .macro  GET_NODE_ID_CORE_ID_F15
1012
1013     LOCAL   node_core_f15_exit
1014     LOCAL   node_core_f15_AP
1015     LOCAL   node_core_f15_shared
1016
1017     cmp     $-1, %si                      # Has node/core already been discovered?
1018     jnz     node_core_f15_exit          # Br if yes
1019
1020     AMD_CPUID   $CPUID_MODEL
1021     shr     $20, %eax                     # AL = cpu extended family
1022     cmp     $06, %al                     # Is this family 15h?
1023     jnz     node_core_f15_exit          # Br if no
1024
1025     xor     %esi, %esi                    # Assume BSC, clear local flags
1026     mov     $APIC_BASE_ADDRESS, %ecx      # MSR:0000_001B
1027     _RDMSR
1028     bt      $APIC_BSC, %eax               # Is this the BSC?
1029     jnc      node_core_f15_AP            # Br if no
1030
1031     # This is the BSP.
1032     # Enable routing tables on BSP (just in case the HT init code has not yet enabled them)
1033     mov     $0x8000C06C, %eax              # PCI address for D18F0x6C Link Initialization Control Register
1034     mov     $0x0CF8, %dx
1035     out     %eax, %dx
1036     add     $4, %dx
1037     in      %dx, %eax
1038     btr     $0, %eax                      # Set LinkInitializationControl[RouteTblDis] = 0
1039     out     %eax, %dx
1040     jmp     node_core_f15_shared        #
1041
1042 node_core_f15_AP:
1043     #
1044     # This is an AP. Routing tables have been enabled by the HT Init process.
1045     # Also, the MailBox register was set by the BSP during early init
1046     #   The Mailbox register content is formatted as follows:
1047     #         UINT32 Node:4;          // The node id of Core's node.
1048     #         UINT32 Socket:4;        // The socket of this Core's node.
1049     #         UINT32 Module:2;        // The internal module number for Core's node.
1050     #         UINT32 ModuleType:2;    // Single Module = 0, Multi-module = 1.
1051     #         UINT32 :20;             // Reserved
1052     #
1053     mov     $0x0C0000408, %ecx             # Read the family 15h mailbox
1054     _RDMSR                              #      MC4_MISC1[63:32]
1055     mov     %dx, %si                      # SI = raw mailbox contents (will extract node# from this)
1056     shr     $24, %ebx                     # BL = CPUID Fn0000_0001_EBX[LocalApicId]
1057     mov     %bx, %di                      # DI = Initial APIC ID (will extract core# from this)
1058
1059     AMD_CPUID   $AMD_CPUID_APIC          #
1060     shr     $4, %ch                       # CH = ApicIdSize, #bits in APIC ID that show core#
1061     inc     %cl                          # CL = Number of enabled cores in the socket
1062     mov     %cx, %bx
1063
1064     mov     $NB_CFG, %ecx
1065     _RDMSR                              # EDX has InitApicIdCpuIdLo bit
1066
1067     mov     %bh, %cl                      # CL = APIC ID size
1068     mov     $1, %al                       # Convert APIC ID size to an AND mask
1069     shl     %cl, %al                      # AL = 2^APIC ID size
1070     dec     %al                          # AL = mask for relative core number
1071     xor     %ah, %ah                      # AX = mask for relative core number
1072     bt      $(INIT_APIC_ID_CPU_ID_LO-32), %edx # InitApicIdCpuIdLo == 1?
1073     #.if (!carry?)                       # Br if yes
1074     jc      0f
1075         mov     $8, %ch                   # Calculate core number shift count
1076         sub     %cl, %ch                  # CH = core shift count
1077         mov     %ch, %cl
1078         shr     %cl, %di                  # Right justify core number
1079     #.endif
1080     0:
1081     and     %ax, %di                      # DI = socket-relative core number
1082
1083     mov     %si, %cx                      # CX = raw mailbox value
1084     shr     $10, %cx                      # CL[1:0] = ModuleType or #nodes per socket (0-SCM, 1-MCM)
1085     and     $3, %cl                       # Isolate ModuleType
1086     xor     %bh, %bh                      # BX = Number of enabled cores in the socket
1087     shr     %cl, %bx                      # BX = Number of enabled cores per node
1088     xor     %dx, %dx                      # Clear upper word for div
1089     mov     %di, %ax                      # AX = socket-relative core number
1090     div     %bx                          # DX = node-relative core number
1091     movzx   %si, %eax                     # Prepare return value
1092     and     $0x000F, %ax                   # AX = node number
1093     shl     $8,%ax                       # [15:8]=node#
1094     mov     %dl, %al                      # [7:0]=core# (relative to node)
1095     mov     %eax, %esi                    # ESI = node-relative core number
1096
1097       #
1098       #   determine if this core shares MTRRs
1099       #
1100 node_core_f15_shared:
1101     mov     $0x8000C580, %eax              # Compute Unit Status
1102     mov     %si, %bx
1103     shl     $3, %bh                       # Move node# to PCI Dev# field
1104     add     %bh, %ah                      # Adjust for node number
1105     mov     $0x0CF8, %dx
1106     out     %eax, %dx
1107     add     $4, %dx
1108     in      %dx, %eax                     # [3:0]=Enabled# [19:16]=DualCore
1109
1110                                         # BL is MyCore#
1111     mov     $0x06, %cx                     # Use CH as 'first of pair' core#
1112     #.while (cl > 0)
1113     jmp  0f
1114     8:
1115         bt      $0, %eax                  # Is pair enabled?
1116         #.if (carry?)                    #
1117         jnc     1f
1118             mov     $0x01, %bh             #   flag core as primary
1119             bt      $16, %eax             # Is there a 2nd in the pair?
1120             #.if (carry?)                #
1121             jnc 4f
1122                 #.break .if (ch == bl)   # Does 1st match MyCore#?
1123                 cmp     %bl, %ch
1124                 je      9f
1125                 inc     %ch
1126                 xor     %bh, %bh          #     flag core as NOT primary
1127                 #.break .if (ch == bl)   # Does 2nd match MyCore#?
1128                 cmp     %bl, %ch
1129                 je      9f
1130                 jmp     2f
1131             #.else                       # No 2nd core
1132             4:
1133                 #.break .if (ch == bl)   # Does 1st match MyCore#?
1134                 cmp     %bl, %ch
1135                 je      9f
1136             #.endif
1137             2:
1138             inc     %ch
1139         #.endif
1140         1:
1141         shr     $1, %eax
1142         dec     %cl
1143     #.endw
1144     0:
1145     #.if (cl == 0)
1146     cmp $0x0, %cl
1147     ja  8b
1148     9:
1149     or  %cl, %cl
1150     jne 1f
1151         #Error - core# didn't match Compute Unit Status content
1152         bts     $FLAG_UNKNOWN_FAMILY, %esi
1153         bts     $FLAG_IS_PRIMARY, %esi    #   Set Is_Primary for unknowns
1154     #.endif
1155     1:
1156     #.if (bh != 0)                       # Check state of primary for the matched core
1157     or %bh, %bh
1158     je 2f
1159         bts     $FLAG_IS_PRIMARY, %esi    #   Set shared flag into return value
1160     #.endif
1161     2:
1162
1163 node_core_f15_exit:
1164
1165 .endm
1166
1167 /*****************************************************************************
1168 * AMD_ENABLE_STACK:  Setup a stack
1169 *
1170 *   In:
1171 *       EBX  = Return address (preserved)
1172 *
1173 *   Out:
1174 *       SS:ESP - Our new private stack location
1175 *
1176 *       EAX = AGESA_STATUS
1177 *
1178 *       ECX = Stack size in bytes
1179 *
1180 *   Requirements:
1181 *       * This routine presently is limited to a max of 64 processor cores
1182 *   Preserved:
1183 *       ebx ebp
1184 *   Destroyed:
1185 *       eax, ecx, edx, edi, esi, ds, es, ss, esp
1186 *       mmx0, mmx1
1187 *
1188 *   Description:
1189 * Fixed MTRR address allocation to cores:
1190 * The BSP gets 64K of stack, Core0 of each node gets 16K of stack, all other cores get 4K.
1191 * There is a max of 1 BSP, 7 core0s and 56 other cores.
1192 * Although each core has it's own cache storage, they share the address space. Each core must
1193 * be assigned a private and unique address space for its stack. To support legacy systems,
1194 * the stack needs to be within the legacy address space (1st 1Meg). Room must also be reserved
1195 * for the other legacy elements (Interrupt vectors, BIOS ROM, video buffer, etc.)
1196 *
1197 * 80000h                                        40000h                                      00000h
1198 *     +----------+----------+----------+----------+----------+----------+----------+----------+
1199 * 64K |          |          |          |          |          |          |          |          |  64K  ea
1200 *  ea +----------+----------+----------+----------+----------+----------+----------+----------+
1201 *     |                             MTRR 0000_0250 MTRRfix64K_00000                           |
1202 *     +----------+----------+----------+----------+----------+----------+----------+----------+
1203 *     |  7 ,  6  |  5 ,  4  |  3 ,  2  |  1 ,  0  |     0    |          |          |          | <-node
1204 *     |7..1,7..1 |7..1,7..1 |7..1,7..1 |7..1,7..1 |     0    |          |          |          | <-core
1205 *     +----------+----------+----------+----------+----------+----------+----------+----------+
1206 *
1207 * C0000h                       B0000h                      A0000h                      90000h                      80000h
1208 *     +------+------+------+------+------+------+------+------+------+------+------+------+------+------+------+------+
1209 *16K  |      |      |      |      |      |      |      |      |      |      |      |      |      |      |      |      |
1210 * ea  +------+------+------+------+------+------+------+------+------+------+------+------+------+------+------+------+
1211 *     |              MTRR 0259 MTRRfix16K_A0000               |             MTRR 0258 MTRRfix16K_80000                |
1212 *     +------+------+------+------+------+------+------+------+------+------+------+------+------+------+------+------+
1213 *     | > Dis|play B|uffer |   <  |      |      |      |      |   7  |  6   |  5   |  4   |  3   |  2   |  1   |      | <-node
1214 *     | >   T| e  m |p o r |a r y |  B u |f f e |r   A |r e a<|   0  |  0   |  0   |  0   |  0   |  0   |  0   |      | <-core
1215 *     +------+------+------+------+------+------+------+------+------+------+------+------+------+------+------+------+
1216 *
1217 * E0000h                                            D0000h                                         C0000h
1218 *     +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
1219 * 4K  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  4K  ea
1220 *  ea +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
1221 *     |  026B MTRRfix4K_D8000 | 026A MTRRfix4K_D0000  | 0269 MTRRfix4K_C8000  | 0268 MTRRfix4K_C0000  |
1222 *     +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
1223 *     |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  | >| V| I| D| E| O|  |B |I |O |S |  |A |r |e |a<|
1224 *     +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
1225 *
1226 * 100000h                                           F0000h                                          E0000h
1227 *     +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
1228 *     |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  4K  ea
1229 *     +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
1230 *     |  026F MTRRfix4K_F8000 | 026E MTRRfix4K_F0000  | 026D MTRRfix4K_E8000  | 026C MTRRfix4K_E0000  |
1231 *     +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
1232 *     | >|MA|IN| B|IO|S |RA|NG|E |  |  |  |  |  |  |< | >|EX|TE|ND|ED| B|IO|S |ZO|NE|  |  |  |  |  |< |
1233 *     +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
1234 *****************************************************************************/
1235 .macro  AMD_ENABLE_STACK
1236
1237 # These are local labels. Declared so linker doesn't cause 'redefined label' errors
1238     LOCAL   SetupStack
1239     LOCAL   Real16bMode
1240     LOCAL   Protected32Mode
1241     LOCAL   ClearTheStack
1242
1243 #   Note that SS:ESP will be default stack.  Note that this stack
1244 #   routine will not be used after memory has been initialized.  Because
1245 #   of its limited lifetime, it will not conflict with typical PCI devices.
1246     movd    %ebx, %mm0                    # Put return address in a safe place
1247     movd    %ebp, %mm1                    # Save some other user registers
1248
1249     # get node id and core id of current executing core
1250     GET_NODE_ID_CORE_ID                 # Sets ESI[23:16]=Shared core## SI[15,8]= Node## SI[7,0]= core# (relative to node)
1251     # Note: ESI[31:24] are used for flags:  Unrecognized Family,  Is_Primary core,  Stack already established
1252
1253     # determine if stack is already enabled. We are using the DefType MSR for this determination.
1254     # It is =0 after reset; CAR setup sets it to enable the MTRRs
1255     mov     %cr0, %eax
1256     test    $CR0_MASK, %eax              # Is cache disabled? (CD & NW bits)
1257     jnz     SetupStack                  # Jump if yes
1258     mov     $AMD_MTRR_DEFTYPE, %ecx       # MSR:0000_02FF
1259     _RDMSR
1260     test    $MSR_MASK, %eax                     # Are the default types enabled? (MTRR_DEF_TYPE_EN + MTRR_DEF_TYPE_FIX_EN)
1261     jz      SetupStack                  # Jump if no
1262     or      $FLAG_STACK_REENTRY, %esi             # Bit25, indicate stack has already been initialized
1263
1264 SetupStack:
1265     # Set node to map the first 16MB to node 0# 0000_0000 to 00FF_FFFF as DRAM
1266     mov     %esi, %ebx                    # Get my Node/Core info
1267     xor     %bl, %bl
1268     shl     $3, %bh                       # Isolate my node#, match alignment for PCI Dev#
1269     mov     $0x8000C144, %eax              # D18F1x44:DRAM Base/Limit# N is Base, N+4 is Limit
1270     add     %bh, %ah
1271     mov     %eax, %ebx                    # Save PCI address for Base/Limit pair
1272
1273     mov     $0x0CF8, %dx
1274     out     %eax, %dx
1275     add     $4, %dx
1276     xor     %eax, %eax                    # Least Significant bit is AD24 so 0 sets mask of 00FF_FFFF (16MB)
1277     out     %eax, %dx                     # DRAM Limit = node0, no interleave
1278
1279     mov     %ebx, %eax
1280     sub     $4, %eax                      # Now point to the Base register
1281     mov     $0x0CF8, %dx
1282     out     %eax, %dx
1283     add     $4, %dx
1284     mov     $0x00000003, %eax              # Set the read and write enable bits
1285     out     %eax, %dx                     # DRAM Base = 0x0000, R/W
1286
1287     AMD_ENABLE_STACK_FAMILY_HOOK
1288
1289     # Init CPU MSRs for our init routines
1290     mov     $MTRR_SYS_CFG, %ecx           # SYS_CFG
1291     _RDMSR
1292     bts     $MTRR_FIX_DRAM_MOD_EN, %eax   # Turn on modification enable bit
1293     _WRMSR
1294
1295     mov     %esi, %eax
1296     bt      $FLAG_STACK_REENTRY, %eax     # Is this a 2nd entry?
1297     #.if (!carry?)                       #   On a re-entry, do not clear MTRRs or reset TOM; just reset the stack SS:ESP
1298     jc 0f
1299         bt      $FLAG_IS_PRIMARY, %eax    #   Is this core the primary in a compute unit?
1300         #.if (carry?)                    #     Families using shared groups do not need to clear the MTRRs since that is done at power-on reset
1301             #  Note: Relying on MSRs to be cleared to 0's at reset for families w/shared cores
1302             # Clear all variable and Fixed MTRRs for non-shared cores
1303         jnc 0f
1304         mov     $AMD_MTRR_VARIABLE_BASE0, %ecx
1305         xor     %eax, %eax
1306         xor     %edx, %edx
1307         #.while (cl != 10h)                  # Variable MTRRphysBase[n] and MTRRphysMask[n]
1308         jmp     1f
1309         2:
1310             _WRMSR
1311             inc     %cl
1312         #.endw
1313         1:
1314         cmp     $0x10, %cl
1315         jne     2b
1316         mov     $AMD_MTRR_FIX64k_00000, %cx   # MSR:0000_0250
1317         _WRMSR
1318         mov     $AMD_MTRR_FIX16k_80000, %cx   # MSR:0000_0258
1319         _WRMSR
1320         mov     $AMD_MTRR_FIX16k_A0000, %cx   # MSR:0000_0259
1321         _WRMSR
1322         mov     $AMD_MTRR_FIX4k_C0000, %cx    # Fixed 4Ks: MTRRfix4K_C0000 to MTRRfix4K_F8000
1323         #.while (cl != 70h)
1324         jmp 3f
1325         4:
1326             _WRMSR
1327             inc     %cl
1328         #.endw
1329         3:
1330         cmp $0x70, %cl
1331         jne  4b
1332         # Set TOP_MEM (C001_001A) for non-shared cores to 16M. This will be increased at heap init.
1333         #  - not strictly needed since the FixedMTRRs take presedence.
1334         mov     $(16 * 1024 * 1024), %eax
1335         mov     $TOP_MEM, %ecx            # MSR:C001_001A
1336         _WRMSR
1337         #.endif                          #   End Is_Primary
1338     #.endif                              # End Stack_ReEntry
1339     0:
1340     # Clear IORRs (C001_0016-19) and TOM2(C001_001D) for all cores
1341     xor     %eax, %eax
1342     xor     %edx, %edx
1343     mov     $IORR_BASE, %ecx              # MSR:C001_0016 - 0019
1344     #.while (cl != 1Ah)
1345     jmp  1f
1346     2:
1347         _WRMSR
1348         inc     %cl
1349     #.endw
1350     1:
1351     cmp $0x1A, %cl
1352     jne  2b
1353     mov     $TOP_MEM2, %ecx               # MSR:C001_001D
1354     _WRMSR
1355
1356     # setup MTTRs for stacks
1357     #   A speculative read can be generated by a speculative fetch mis-aligned in a code zone
1358     #    or due to a data zone being interpreted as code. When a speculative read occurs outside a
1359     #    controlled region (intentionally used by software), it could cause an unwanted cache eviction.
1360     #   To prevent speculative reads from causing an eviction, the unused cache ranges are set
1361     #    to UC type. Only the actively used regions (stack, heap) are reflected in the MTRRs.
1362     #    Note: some core stack regions will share an MTRR since the control granularity is much
1363     #    larger than the allocated stack zone. The allocation algorithm must account for this 'extra'
1364     #    space covered by the MTRR when parseling out cache space for the various uses. In some cases
1365     #    this could reduce the amount of EXE cache available to a core. see cpuCacheInit.c
1366     #
1367     # Outcome of this block is that:   (Note the MTRR map at the top of the file)
1368     #   ebp - start address of stack block
1369     #   ebx - [31:16] - MTRR MSR address
1370     #       - [15:8]  - slot# in MTRR register
1371     #       - [7:0]   - block size in #4K blocks
1372     # review: ESI[31:24]=Flags; SI[15,8]= Node#; SI[7,0]= core# (relative to node)
1373     #
1374
1375     mov     %si, %ax                      # Load node, core
1376     #.if (al == 0)                       # Is a core 0?
1377     or %al, %al
1378     jne 1f
1379         #.if (ah == 0)                   # Is Node 0? (BSP)
1380         or %ah, %ah
1381         jne 2f
1382             # Is BSP, assign a 64K stack
1383             mov     $((AMD_MTRR_FIX64k_00000 << 16) + (3 << 8) + (BSP_STACK_SIZE  / 0x1000)), %ebx
1384             mov     $BSP_STACK_BASE_ADDR, %ebp
1385             jmp     0f
1386         #.else   # node 1 to 7, core0
1387         2:
1388             # Is a Core0 of secondary node, assign 16K stacks
1389             mov     $AMD_MTRR_FIX16k_80000, %bx
1390             shl     $16, %ebx             #
1391             mov     %ah, %bh              # Node# is used as slot#
1392             mov     $(CORE0_STACK_SIZE / 0x1000), %bl
1393             mov     %ah, %al              # Base = (Node# * Size)#
1394             mul     %bl                  #
1395             movzx   %ax, %eax             #
1396             shl     $12, %eax             # Expand back to full byte count (* 4K)
1397             add     $CORE0_STACK_BASE_ADDR, %eax
1398             mov     %eax, %ebp
1399         #.endif
1400         jmp 0f
1401     #.else    #core 1 thru core 7
1402     1:
1403         # Is core 1-7 of any node, assign 4K stacks
1404         mov     $8, %al                   # CoreIndex = ( (Node# * 8) ...
1405         mul     %ah                      #
1406         mov     %si, %bx                  #
1407         add     %bl, %al                  #         ...  + Core#)#
1408
1409         mov     $AMD_MTRR_FIX64k_00000, %bx
1410         shl     $16, %ebx                 #
1411         mov     %al, %bh                  # Slot# = (CoreIndex / 16) + 4#
1412         shr     $4, %bh                   #
1413         add     $4, %bh                   #
1414         mov     $(CORE1_STACK_SIZE / 0x1000), %bl
1415
1416         mul     %bl                      # Base = ( (CoreIndex * Size) ...
1417         movzx   %ax, %eax                 #
1418         shl     $12, %eax                 # Expand back to full byte count (* 4K)
1419         add     $CORE1_STACK_BASE_ADDR, %eax #     ...   + Base_Addr)#
1420         mov     %eax, %ebp
1421     #.endif
1422     0:
1423
1424     # Now set the MTRR. Add this to already existing settings (don't clear any MTRR)
1425     mov     $WB_DRAM_TYPE, %edi           # Load Cache type in 1st slot
1426     mov     %bh, %cl                      # ShiftCount =  ((slot#   ...
1427     and     $0x03, %cl                     #   ...  % 4)             ...
1428     shl     $0x03, %cl                       #   ...  * 8)#
1429     shl     %cl, %edi                     # Cache type is now in correct position
1430     ror     $16, %ebx                     # Get the MTRR address
1431     movzx   %bx, %ecx                     #
1432     rol     $16, %ebx                     # Put slot# & size back in BX
1433     _RDMSR                              # Read-modify-write the MSR
1434     #.if (bh < 4)                        # Is value in lower or upper half of MSR?
1435     cmp $4, %bh
1436     jae 1f
1437         or      %edi, %eax                #
1438         jmp     0f
1439     #.else
1440     1:                               #
1441         or      %edi, %edx                #
1442     #.endif                              #
1443     0:
1444     _WRMSR                              #
1445
1446     # Enable MTRR defaults as UC type
1447     mov     $AMD_MTRR_DEFTYPE, %ecx       # MSR:0000_02FF
1448     _RDMSR                              # Read-modify-write the MSR
1449     bts     $MTRR_DEF_TYPE_EN, %eax       # MtrrDefTypeEn
1450     bts     $MTRR_DEF_TYPE_FIX_EN, %eax   # MtrrDefTypeFixEn
1451     _WRMSR
1452
1453     # Close the modification window on the Fixed MTRRs
1454     mov     $MTRR_SYS_CFG, %ecx           # MSR:0C001_0010
1455     _RDMSR
1456     bts     $MTRR_FIX_DRAM_EN, %eax       # MtrrFixDramEn
1457     bts     $MTRR_VAR_DRAM_EN, %eax       # variable MTRR enable bit
1458     btr     $MTRR_FIX_DRAM_MOD_EN, %eax   # Turn off modification enable bit
1459     _WRMSR
1460
1461     # Enable caching in CR0
1462     mov     %cr0, %eax                    # Enable WT/WB cache
1463     btr     $CR0_PG, %eax                     # Make sure paging is disabled
1464     btr     $CR0_CD, %eax                     # Clear CR0 NW and CD
1465     btr     $CR0_NW, %eax
1466     mov     %eax, %cr0
1467
1468     # Use the Stack Base & size to calculate SS and ESP values
1469     # review:
1470     #       esi[31:24]=Flags; esi[15,8]= Node#; esi[7,0]= core# (relative to node)
1471     #       ebp - start address of stack block
1472     #       ebx - [31:16] - MTRR MSR address
1473     #           - [15:8]  - slot# in MTRR register
1474     #           - [7:0]   - block size in #4K blocks
1475     #
1476     mov     %ebp, %esp                    # Initialize the stack pointer
1477     mov     %esp, %edi                    # Copy the stack start to edi
1478     movzx   %bl, %bx
1479     movzx   %bx, %ebx                     # Clear upper ebx, don't need MSR addr anymore
1480     shl     $12, %ebx                     # Make size full byte count (* 4K)
1481     add     %ebx, %esp                    # Set the Stack Pointer as full linear address
1482     sub     $4, %esp
1483     #
1484     # review:
1485     #       esi[31:24]=Flags; esi[15,8]= Node#; esi[7,0]= core# (relative to node)
1486     #       edi - 32b start address of stack block
1487     #       ebx - size of stack block
1488     #       esp - 32b linear stack pointer
1489     #
1490
1491     # Determine mode for SS base;
1492     mov     %cr0, %ecx                    # Check for 32-bit protect mode
1493     bt      $CR0_PE, %ecx                 #
1494     #.if (!carry?)                       # PE=0 means real mode
1495     jc      Protected32Mode
1496     mov     %cs, %cx                      # PE=1
1497     cmp     $0x0D000, %cx                  # Check for CS
1498     jb      Protected32Mode             # If CS < D000, it is a selector instead of a segment
1499             # alter SS:ESP for 16b Real Mode:
1500 Real16bMode:
1501     mov     %edi, %eax
1502     shr     $4, %eax                      # Create a Real Mode segment for ss, ds, es
1503     mov     %ax, %ss
1504     mov     %ax, %ds
1505     mov     %ax, %es
1506     shl     $4, %eax
1507     sub     %eax, %edi                    # Adjust the clearing pointer for Seg:Offset mode
1508     mov     %ebx, %esp                    # Make SP an offset from SS
1509     sub     $4, %esp              #
1510     #    .endif                          # endif
1511     # #else
1512     #   Default is to use Protected 32b Mode
1513     #.endif
1514     ;
1515 Protected32Mode:
1516     #
1517     # Clear The Stack
1518     #   Now that we have set the location and the MTRRs, initialize the cache by
1519     #   reading then writing to zero all of the stack area.
1520     # review:
1521     #       ss  - Stack base
1522     #       esp - stack pointer
1523     #       ebx - size of stack block
1524     #       esi[31:24]=Flags; esi[15,8]= Node#; esi[7,0]= core# (relative to node)
1525     #       edi -  address of start of stack block
1526     #
1527
1528 ClearTheStack:                          # Stack base is in SS, stack pointer is in ESP
1529     shr     $2, %ebx                      # ebx = stack block size in dwords
1530     mov     %bx, %cx                      #
1531     # Check our flags - Don't clear an existing stack
1532     #.if ( !(esi & 0FF000000h))          # Check our flags
1533     test    $(1 << FLAG_STACK_REENTRY), %esi
1534     jne 1f
1535         cld
1536         mov     %edi, %esi
1537         rep     lodsl (%esi)    # Pre-load the range
1538         xor     %eax, %eax
1539         mov     %bx, %cx
1540         mov     %edi, %esi                # Preserve base for push on stack
1541         rep     stosl (%edi)    # Clear the range
1542         movl     $0x0ABCDDCBA, (%esp) # Put marker in top stack dword
1543         shl     $2, %ebx                  # Put stack size and base
1544         push    %ebx                     #  in top of stack
1545         push    %esi
1546
1547         mov     %ebx, %ecx                # Return size of stack in bytes
1548         xor     %eax, %eax                # eax = 0 : no error return code
1549         jmp     0f
1550     #.else
1551     1:
1552         movzx   %cx, %ecx
1553         shl     $2, %ecx                  # Return size of stack in bytes
1554         mov     %esi, %eax
1555         shr     $24, %eax                 # Keep the flags as part of the error report
1556         or      $0x40000000, %eax          # eax = AGESA_WARNING (Stack has already been set up)
1557     #.endif
1558     0:
1559
1560     movd        %mm0, %ebx                # Restore return address
1561     movd        %mm1, %ebp
1562 .endm
1563
1564 /*****************************************************************************
1565 * AMD_DISABLE_STACK:  Destroy the stack inside the cache. This routine
1566 *                     should only be executed on the BSP
1567 *
1568 *   In:
1569 *       none
1570 *
1571 *   Out:
1572 *       EAX = AGESA_SUCCESS
1573 *
1574 *   Preserved:
1575 *       ebx
1576 *   Destroyed:
1577 *       eax, ecx, edx, esp
1578 *****************************************************************************/
1579 .macro  AMD_DISABLE_STACK
1580
1581     mov     %ebx, %esp                    # Save return address
1582
1583     # get node/core/flags of current executing core
1584     GET_NODE_ID_CORE_ID                 # Sets ESI[15,8]= Node#; ESI[7,0]= core# (relative to node)
1585
1586     # Turn on modification enable bit
1587     mov     $MTRR_SYS_CFG, %ecx           # MSR:C001_0010
1588     _RDMSR
1589     bts     $MTRR_FIX_DRAM_MOD_EN, %eax   # Enable modifications
1590     _WRMSR
1591
1592     # Set lower 640K MTRRs for Write-Back memory caching
1593     mov     $AMD_MTRR_FIX64k_00000, %ecx
1594     mov     $0x1E1E1E1E, %eax
1595     mov     %eax, %edx
1596     _WRMSR                              # 0 - 512K = WB Mem
1597     mov     $AMD_MTRR_FIX16k_80000, %ecx
1598     _WRMSR                              # 512K - 640K = WB Mem
1599
1600     # Turn off modification enable bit
1601     mov     $MTRR_SYS_CFG, %ecx           # MSR:C001_0010
1602     _RDMSR
1603     btr     $MTRR_FIX_DRAM_MOD_EN, %eax   # Disable modification
1604     _WRMSR
1605
1606     AMD_DISABLE_STACK_FAMILY_HOOK       # Re-Enable 'normal' cache operations
1607
1608     mov     %esp, %ebx
1609     xor     %eax, %eax
1610
1611 .endm
1612