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