Angel Pons | f23ae0b | 2020-04-02 23:48:12 +0200 | [diff] [blame] | 1 | /* SPDX-License-Identifier: GPL-2.0-only */ |
Arthur Heymans | dd4d895 | 2018-06-03 12:04:26 +0200 | [diff] [blame] | 2 | |
| 3 | #include <cpu/x86/mtrr.h> |
| 4 | #include <cpu/x86/cache.h> |
| 5 | #include <cpu/x86/post_code.h> |
| 6 | |
Arthur Heymans | dd4d895 | 2018-06-03 12:04:26 +0200 | [diff] [blame] | 7 | #define NoEvictMod_MSR 0x2e0 |
Arthur Heymans | 19e7273 | 2019-01-11 23:56:51 +0100 | [diff] [blame] | 8 | #define BBL_CR_CTL3_MSR 0x11e |
Arthur Heymans | dd4d895 | 2018-06-03 12:04:26 +0200 | [diff] [blame] | 9 | |
Kyösti Mälkki | c641f7e | 2018-12-28 16:54:54 +0200 | [diff] [blame] | 10 | .global bootblock_pre_c_entry |
| 11 | |
Patrick Rudolph | 49da0cf | 2020-09-24 18:57:43 +0200 | [diff] [blame] | 12 | #include <cpu/intel/car/cache_as_ram_symbols.inc> |
| 13 | |
Arthur Heymans | dd4d895 | 2018-06-03 12:04:26 +0200 | [diff] [blame] | 14 | .code32 |
| 15 | _cache_as_ram_setup: |
| 16 | |
Kyösti Mälkki | c641f7e | 2018-12-28 16:54:54 +0200 | [diff] [blame] | 17 | bootblock_pre_c_entry: |
Arthur Heymans | 8e646e7 | 2018-06-05 11:19:22 +0200 | [diff] [blame] | 18 | movl $cache_as_ram, %esp /* return address */ |
| 19 | jmp check_mtrr /* Check if CPU properly reset */ |
Arthur Heymans | 8e646e7 | 2018-06-05 11:19:22 +0200 | [diff] [blame] | 20 | |
Arthur Heymans | dd4d895 | 2018-06-03 12:04:26 +0200 | [diff] [blame] | 21 | cache_as_ram: |
| 22 | post_code(0x20) |
| 23 | |
| 24 | /* Send INIT IPI to all excluding ourself. */ |
| 25 | movl $0x000C4500, %eax |
| 26 | movl $0xFEE00300, %esi |
| 27 | movl %eax, (%esi) |
| 28 | |
| 29 | /* All CPUs need to be in Wait for SIPI state */ |
| 30 | wait_for_sipi: |
| 31 | movl (%esi), %eax |
| 32 | bt $12, %eax |
| 33 | jc wait_for_sipi |
| 34 | |
| 35 | post_code(0x21) |
| 36 | /* Clean-up MTRR_DEF_TYPE_MSR. */ |
| 37 | movl $MTRR_DEF_TYPE_MSR, %ecx |
| 38 | xorl %eax, %eax |
| 39 | xorl %edx, %edx |
| 40 | wrmsr |
| 41 | |
| 42 | post_code(0x22) |
Arthur Heymans | c2ccc97 | 2018-06-03 12:09:52 +0200 | [diff] [blame] | 43 | /* Clear/disable fixed MTRRs */ |
| 44 | mov $fixed_mtrr_list_size, %ebx |
| 45 | xor %eax, %eax |
| 46 | xor %edx, %edx |
| 47 | |
| 48 | clear_fixed_mtrr: |
| 49 | add $-2, %ebx |
| 50 | movzwl fixed_mtrr_list(%ebx), %ecx |
Arthur Heymans | dd4d895 | 2018-06-03 12:04:26 +0200 | [diff] [blame] | 51 | wrmsr |
Arthur Heymans | c2ccc97 | 2018-06-03 12:09:52 +0200 | [diff] [blame] | 52 | jnz clear_fixed_mtrr |
Arthur Heymans | dd4d895 | 2018-06-03 12:04:26 +0200 | [diff] [blame] | 53 | |
| 54 | /* Zero out all variable range MTRRs. */ |
| 55 | movl $MTRR_CAP_MSR, %ecx |
| 56 | rdmsr |
| 57 | andl $0xff, %eax |
| 58 | shl $1, %eax |
| 59 | movl %eax, %edi |
| 60 | movl $0x200, %ecx |
| 61 | xorl %eax, %eax |
| 62 | xorl %edx, %edx |
| 63 | clear_var_mtrrs: |
| 64 | wrmsr |
| 65 | add $1, %ecx |
| 66 | dec %edi |
| 67 | jnz clear_var_mtrrs |
| 68 | |
Arthur Heymans | c2ccc97 | 2018-06-03 12:09:52 +0200 | [diff] [blame] | 69 | /* Determine CPU_ADDR_BITS and load PHYSMASK high word to %edx. */ |
| 70 | movl $0x80000008, %eax |
| 71 | cpuid |
| 72 | movb %al, %cl |
| 73 | sub $32, %cl |
| 74 | movl $1, %edx |
| 75 | shl %cl, %edx |
| 76 | subl $1, %edx |
| 77 | |
| 78 | /* Preload high word of address mask (in %edx) for Variable |
| 79 | * MTRRs 0 and 1. |
| 80 | */ |
| 81 | addrsize_set_high: |
| 82 | xorl %eax, %eax |
| 83 | movl $MTRR_PHYS_MASK(0), %ecx |
| 84 | wrmsr |
| 85 | movl $MTRR_PHYS_MASK(1), %ecx |
| 86 | wrmsr |
| 87 | |
Arthur Heymans | dd4d895 | 2018-06-03 12:04:26 +0200 | [diff] [blame] | 88 | post_code(0x23) |
| 89 | /* Set Cache-as-RAM base address. */ |
| 90 | movl $(MTRR_PHYS_BASE(0)), %ecx |
Patrick Rudolph | 49da0cf | 2020-09-24 18:57:43 +0200 | [diff] [blame] | 91 | movl car_mtrr_start, %eax |
Kyösti Mälkki | dc6bb6c | 2019-11-08 00:08:55 +0200 | [diff] [blame] | 92 | orl $MTRR_TYPE_WRBACK, %eax |
Arthur Heymans | dd4d895 | 2018-06-03 12:04:26 +0200 | [diff] [blame] | 93 | xorl %edx, %edx |
| 94 | wrmsr |
| 95 | |
| 96 | post_code(0x24) |
| 97 | /* Set Cache-as-RAM mask. */ |
| 98 | movl $(MTRR_PHYS_MASK(0)), %ecx |
Arthur Heymans | c2ccc97 | 2018-06-03 12:09:52 +0200 | [diff] [blame] | 99 | rdmsr |
Patrick Rudolph | 49da0cf | 2020-09-24 18:57:43 +0200 | [diff] [blame] | 100 | mov car_mtrr_mask, %eax |
Kyösti Mälkki | dc6bb6c | 2019-11-08 00:08:55 +0200 | [diff] [blame] | 101 | orl $MTRR_PHYS_MASK_VALID, %eax |
Arthur Heymans | dd4d895 | 2018-06-03 12:04:26 +0200 | [diff] [blame] | 102 | wrmsr |
| 103 | |
Arthur Heymans | 48bf712 | 2019-01-05 17:18:11 +0100 | [diff] [blame] | 104 | /* Enable cache for our code in Flash because we do XIP here */ |
| 105 | movl $MTRR_PHYS_BASE(1), %ecx |
| 106 | xorl %edx, %edx |
Patrick Rudolph | 49da0cf | 2020-09-24 18:57:43 +0200 | [diff] [blame] | 107 | mov rom_mtrr_base, %eax |
Kyösti Mälkki | dc6bb6c | 2019-11-08 00:08:55 +0200 | [diff] [blame] | 108 | orl $MTRR_TYPE_WRPROT, %eax |
Arthur Heymans | 48bf712 | 2019-01-05 17:18:11 +0100 | [diff] [blame] | 109 | wrmsr |
| 110 | |
| 111 | movl $MTRR_PHYS_MASK(1), %ecx |
| 112 | rdmsr |
Patrick Rudolph | 49da0cf | 2020-09-24 18:57:43 +0200 | [diff] [blame] | 113 | mov rom_mtrr_mask, %eax |
Kyösti Mälkki | dc6bb6c | 2019-11-08 00:08:55 +0200 | [diff] [blame] | 114 | orl $MTRR_PHYS_MASK_VALID, %eax |
Arthur Heymans | 48bf712 | 2019-01-05 17:18:11 +0100 | [diff] [blame] | 115 | wrmsr |
| 116 | |
Arthur Heymans | dd4d895 | 2018-06-03 12:04:26 +0200 | [diff] [blame] | 117 | post_code(0x25) |
| 118 | |
| 119 | /* Enable MTRR. */ |
| 120 | movl $MTRR_DEF_TYPE_MSR, %ecx |
| 121 | rdmsr |
| 122 | orl $MTRR_DEF_TYPE_EN, %eax |
| 123 | wrmsr |
| 124 | |
Julius Werner | cd49cce | 2019-03-05 16:53:33 -0800 | [diff] [blame] | 125 | #if CONFIG(CPU_HAS_L2_ENABLE_MSR) |
Arthur Heymans | 19e7273 | 2019-01-11 23:56:51 +0100 | [diff] [blame] | 126 | /* |
| 127 | * Enable the L2 cache. Currently this assumes that this |
| 128 | * only affect socketed CPU's for which this is always valid, |
| 129 | * hence the static preprocesser. |
| 130 | */ |
| 131 | movl $BBL_CR_CTL3_MSR, %ecx |
| 132 | rdmsr |
| 133 | orl $0x100, %eax |
| 134 | wrmsr |
| 135 | #endif |
| 136 | |
Arthur Heymans | dd4d895 | 2018-06-03 12:04:26 +0200 | [diff] [blame] | 137 | /* Enable cache (CR0.CD = 0, CR0.NW = 0). */ |
| 138 | movl %cr0, %eax |
| 139 | andl $(~(CR0_CacheDisable | CR0_NoWriteThrough)), %eax |
| 140 | invd |
| 141 | movl %eax, %cr0 |
| 142 | |
Julius Werner | cd49cce | 2019-03-05 16:53:33 -0800 | [diff] [blame] | 143 | #if CONFIG(MICROCODE_UPDATE_PRE_RAM) |
Arthur Heymans | 48bf712 | 2019-01-05 17:18:11 +0100 | [diff] [blame] | 144 | update_microcode: |
| 145 | /* put the return address in %esp */ |
| 146 | movl $end_microcode_update, %esp |
| 147 | jmp update_bsp_microcode |
| 148 | end_microcode_update: |
| 149 | #endif |
| 150 | /* Disable caching to change MTRR's. */ |
| 151 | movl %cr0, %eax |
| 152 | orl $CR0_CacheDisable, %eax |
| 153 | movl %eax, %cr0 |
| 154 | |
| 155 | /* Clear the mask valid to disable the MTRR */ |
| 156 | movl $MTRR_PHYS_MASK(1), %ecx |
| 157 | rdmsr |
| 158 | andl $(~MTRR_PHYS_MASK_VALID), %eax |
| 159 | wrmsr |
| 160 | |
| 161 | /* Enable cache. */ |
| 162 | movl %cr0, %eax |
| 163 | andl $(~(CR0_CacheDisable | CR0_NoWriteThrough)), %eax |
| 164 | invd |
| 165 | movl %eax, %cr0 |
| 166 | |
Arthur Heymans | dd4d895 | 2018-06-03 12:04:26 +0200 | [diff] [blame] | 167 | /* enable the 'no eviction' mode */ |
Arthur Heymans | a28befd | 2018-12-20 13:59:34 +0100 | [diff] [blame] | 168 | movl $NoEvictMod_MSR, %ecx |
Arthur Heymans | dd4d895 | 2018-06-03 12:04:26 +0200 | [diff] [blame] | 169 | rdmsr |
Arthur Heymans | a28befd | 2018-12-20 13:59:34 +0100 | [diff] [blame] | 170 | orl $1, %eax |
| 171 | andl $~2, %eax |
Arthur Heymans | dd4d895 | 2018-06-03 12:04:26 +0200 | [diff] [blame] | 172 | wrmsr |
| 173 | |
| 174 | /* Clear the cache memory region. This will also fill up the cache. */ |
Kyösti Mälkki | dc6bb6c | 2019-11-08 00:08:55 +0200 | [diff] [blame] | 175 | cld |
Arthur Heymans | dd4d895 | 2018-06-03 12:04:26 +0200 | [diff] [blame] | 176 | xorl %eax, %eax |
Kyösti Mälkki | dc6bb6c | 2019-11-08 00:08:55 +0200 | [diff] [blame] | 177 | movl $_car_mtrr_start, %edi |
| 178 | movl $_car_mtrr_size, %ecx |
| 179 | shr $2, %ecx |
Arthur Heymans | dd4d895 | 2018-06-03 12:04:26 +0200 | [diff] [blame] | 180 | rep stosl |
| 181 | |
| 182 | /* enable the 'no eviction run' state */ |
Arthur Heymans | a28befd | 2018-12-20 13:59:34 +0100 | [diff] [blame] | 183 | movl $NoEvictMod_MSR, %ecx |
Arthur Heymans | dd4d895 | 2018-06-03 12:04:26 +0200 | [diff] [blame] | 184 | rdmsr |
Arthur Heymans | a28befd | 2018-12-20 13:59:34 +0100 | [diff] [blame] | 185 | orl $3, %eax |
Arthur Heymans | dd4d895 | 2018-06-03 12:04:26 +0200 | [diff] [blame] | 186 | wrmsr |
| 187 | |
| 188 | post_code(0x26) |
| 189 | /* Enable Cache-as-RAM mode by disabling cache. */ |
| 190 | movl %cr0, %eax |
| 191 | orl $CR0_CacheDisable, %eax |
| 192 | movl %eax, %cr0 |
| 193 | |
Arthur Heymans | dd4d895 | 2018-06-03 12:04:26 +0200 | [diff] [blame] | 194 | movl $MTRR_PHYS_MASK(1), %ecx |
Arthur Heymans | c2ccc97 | 2018-06-03 12:09:52 +0200 | [diff] [blame] | 195 | rdmsr |
Arthur Heymans | 48bf712 | 2019-01-05 17:18:11 +0100 | [diff] [blame] | 196 | orl $MTRR_PHYS_MASK_VALID, %eax |
Arthur Heymans | dd4d895 | 2018-06-03 12:04:26 +0200 | [diff] [blame] | 197 | wrmsr |
| 198 | |
| 199 | post_code(0x28) |
| 200 | /* Enable cache. */ |
| 201 | movl %cr0, %eax |
| 202 | andl $(~(CR0_CacheDisable | CR0_NoWriteThrough)), %eax |
| 203 | movl %eax, %cr0 |
| 204 | |
| 205 | /* Setup the stack. */ |
Arthur Heymans | df9cdcf | 2019-11-09 06:50:20 +0100 | [diff] [blame] | 206 | mov $_ecar_stack, %esp |
Arthur Heymans | dd4d895 | 2018-06-03 12:04:26 +0200 | [diff] [blame] | 207 | |
Kyösti Mälkki | c641f7e | 2018-12-28 16:54:54 +0200 | [diff] [blame] | 208 | /* Need to align stack to 16 bytes at call instruction. Account for |
| 209 | the pushes below. */ |
Arthur Heymans | 348b79f | 2018-06-03 17:14:19 +0200 | [diff] [blame] | 210 | andl $0xfffffff0, %esp |
Arthur Heymans | 348b79f | 2018-06-03 17:14:19 +0200 | [diff] [blame] | 211 | |
Patrick Rudolph | 49da0cf | 2020-09-24 18:57:43 +0200 | [diff] [blame] | 212 | #if ENV_X86_64 |
| 213 | |
| 214 | #include <cpu/x86/64bit/entry64.inc> |
| 215 | |
| 216 | movd %mm2, %rdi |
| 217 | shlq $32, %rdi |
| 218 | movd %mm1, %rsi |
| 219 | or %rsi, %rdi |
| 220 | movd %mm0, %rsi |
| 221 | |
| 222 | #else |
| 223 | subl $4, %esp |
Kyösti Mälkki | c641f7e | 2018-12-28 16:54:54 +0200 | [diff] [blame] | 224 | /* push TSC and BIST to stack */ |
| 225 | movd %mm0, %eax |
Elyes HAOUAS | 87930b3 | 2019-01-16 12:41:57 +0100 | [diff] [blame] | 226 | pushl %eax /* BIST */ |
Kyösti Mälkki | c641f7e | 2018-12-28 16:54:54 +0200 | [diff] [blame] | 227 | movd %mm2, %eax |
| 228 | pushl %eax /* tsc[63:32] */ |
| 229 | movd %mm1, %eax |
Elyes HAOUAS | 87930b3 | 2019-01-16 12:41:57 +0100 | [diff] [blame] | 230 | pushl %eax /* tsc[31:0] */ |
Patrick Rudolph | 49da0cf | 2020-09-24 18:57:43 +0200 | [diff] [blame] | 231 | #endif |
Arthur Heymans | dd4d895 | 2018-06-03 12:04:26 +0200 | [diff] [blame] | 232 | |
Kyösti Mälkki | c641f7e | 2018-12-28 16:54:54 +0200 | [diff] [blame] | 233 | before_c_entry: |
Arthur Heymans | dd4d895 | 2018-06-03 12:04:26 +0200 | [diff] [blame] | 234 | post_code(0x29) |
Kyösti Mälkki | c641f7e | 2018-12-28 16:54:54 +0200 | [diff] [blame] | 235 | call bootblock_c_entry_bist |
Arthur Heymans | dd4d895 | 2018-06-03 12:04:26 +0200 | [diff] [blame] | 236 | |
| 237 | /* Should never see this postcode */ |
| 238 | post_code(POST_DEAD_CODE) |
| 239 | |
| 240 | |
| 241 | .Lhlt: |
| 242 | hlt |
| 243 | jmp .Lhlt |
| 244 | |
Arthur Heymans | c2ccc97 | 2018-06-03 12:09:52 +0200 | [diff] [blame] | 245 | fixed_mtrr_list: |
| 246 | .word MTRR_FIX_64K_00000 |
| 247 | .word MTRR_FIX_16K_80000 |
| 248 | .word MTRR_FIX_16K_A0000 |
| 249 | .word MTRR_FIX_4K_C0000 |
| 250 | .word MTRR_FIX_4K_C8000 |
| 251 | .word MTRR_FIX_4K_D0000 |
| 252 | .word MTRR_FIX_4K_D8000 |
| 253 | .word MTRR_FIX_4K_E0000 |
| 254 | .word MTRR_FIX_4K_E8000 |
| 255 | .word MTRR_FIX_4K_F0000 |
| 256 | .word MTRR_FIX_4K_F8000 |
| 257 | fixed_mtrr_list_size = . - fixed_mtrr_list |
Arthur Heymans | dd4d895 | 2018-06-03 12:04:26 +0200 | [diff] [blame] | 258 | |
| 259 | _cache_as_ram_setup_end: |