Angel Pons | f23ae0b | 2020-04-02 23:48:12 +0200 | [diff] [blame] | 1 | /* SPDX-License-Identifier: GPL-2.0-only */ |
Arthur Heymans | 7a8205b | 2018-06-03 10:29:07 +0200 | [diff] [blame] | 2 | |
Martin Roth | c87ab01 | 2022-11-20 19:32:51 -0700 | [diff] [blame] | 3 | #include <cpu/intel/post_codes.h> |
Arthur Heymans | 7a8205b | 2018-06-03 10:29:07 +0200 | [diff] [blame] | 4 | #include <cpu/x86/mtrr.h> |
| 5 | #include <cpu/x86/cache.h> |
| 6 | #include <cpu/x86/post_code.h> |
Arthur Heymans | 3edf840 | 2021-07-05 21:18:50 +0200 | [diff] [blame^] | 7 | #include <cpu/x86/64bit/entry64.inc> |
Arthur Heymans | 7a8205b | 2018-06-03 10:29:07 +0200 | [diff] [blame] | 8 | |
Kyösti Mälkki | 7522a8f | 2020-11-20 16:47:38 +0200 | [diff] [blame] | 9 | .section .init |
Kyösti Mälkki | c641f7e | 2018-12-28 16:54:54 +0200 | [diff] [blame] | 10 | .global bootblock_pre_c_entry |
| 11 | |
Patrick Rudolph | c439e07 | 2020-09-28 22:31:06 +0200 | [diff] [blame] | 12 | #include <cpu/intel/car/cache_as_ram_symbols.inc> |
| 13 | |
Arthur Heymans | 7a8205b | 2018-06-03 10:29:07 +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 | 7a8205b | 2018-06-03 10:29:07 +0200 | [diff] [blame] | 18 | |
| 19 | cache_as_ram: |
lilacious | 40cb3fe | 2023-06-21 23:24:14 +0200 | [diff] [blame] | 20 | post_code(POSTCODE_BOOTBLOCK_CAR) |
Arthur Heymans | 7a8205b | 2018-06-03 10:29:07 +0200 | [diff] [blame] | 21 | |
| 22 | /* Send INIT IPI to all excluding ourself. */ |
| 23 | movl $0x000C4500, %eax |
| 24 | movl $0xFEE00300, %esi |
| 25 | movl %eax, (%esi) |
| 26 | |
Arthur Heymans | 3aa9adb | 2018-06-03 11:02:54 +0200 | [diff] [blame] | 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 | |
Yuchen He | 1e67adb | 2023-07-25 21:28:36 +0200 | [diff] [blame] | 33 | post_code(POSTCODE_SOC_CLEAR_FIXED_MTRRS) |
Arthur Heymans | 3aa9adb | 2018-06-03 11:02:54 +0200 | [diff] [blame] | 34 | |
| 35 | /* Clear/disable fixed MTRRs */ |
Arthur Heymans | 2834d98 | 2022-11-08 15:06:42 +0100 | [diff] [blame] | 36 | mov $fixed_mtrr_list, %ebx |
Arthur Heymans | 3aa9adb | 2018-06-03 11:02:54 +0200 | [diff] [blame] | 37 | xor %eax, %eax |
| 38 | xor %edx, %edx |
| 39 | |
| 40 | clear_fixed_mtrr: |
Arthur Heymans | 2834d98 | 2022-11-08 15:06:42 +0100 | [diff] [blame] | 41 | movzwl (%ebx), %ecx |
Arthur Heymans | 7a8205b | 2018-06-03 10:29:07 +0200 | [diff] [blame] | 42 | wrmsr |
Arthur Heymans | 2834d98 | 2022-11-08 15:06:42 +0100 | [diff] [blame] | 43 | add $2, %ebx |
| 44 | cmp $fixed_mtrr_list_end, %ebx |
| 45 | jl clear_fixed_mtrr |
Arthur Heymans | 3aa9adb | 2018-06-03 11:02:54 +0200 | [diff] [blame] | 46 | |
Elyes HAOUAS | 02820ca | 2018-09-30 07:44:39 +0200 | [diff] [blame] | 47 | /* Figure out how many MTRRs we have, and clear them out */ |
Arthur Heymans | 3aa9adb | 2018-06-03 11:02:54 +0200 | [diff] [blame] | 48 | mov $MTRR_CAP_MSR, %ecx |
| 49 | rdmsr |
| 50 | movzb %al, %ebx /* Number of variable MTRRs */ |
| 51 | mov $MTRR_PHYS_BASE(0), %ecx |
| 52 | xor %eax, %eax |
| 53 | xor %edx, %edx |
| 54 | |
| 55 | clear_var_mtrr: |
| 56 | wrmsr |
| 57 | inc %ecx |
| 58 | wrmsr |
| 59 | inc %ecx |
| 60 | dec %ebx |
| 61 | jnz clear_var_mtrr |
Arthur Heymans | 7a8205b | 2018-06-03 10:29:07 +0200 | [diff] [blame] | 62 | |
Yuchen He | 1e67adb | 2023-07-25 21:28:36 +0200 | [diff] [blame] | 63 | post_code(POSTCODE_SOC_SET_DEF_MTRR_TYPE) |
Arthur Heymans | 7a8205b | 2018-06-03 10:29:07 +0200 | [diff] [blame] | 64 | /* Configure the default memory type to uncacheable. */ |
| 65 | movl $MTRR_DEF_TYPE_MSR, %ecx |
| 66 | rdmsr |
| 67 | andl $(~0x00000cff), %eax |
| 68 | wrmsr |
| 69 | |
Arthur Heymans | 3aa9adb | 2018-06-03 11:02:54 +0200 | [diff] [blame] | 70 | /* Determine CPU_ADDR_BITS and load PHYSMASK high word to %edx. */ |
| 71 | movl $0x80000008, %eax |
| 72 | cpuid |
| 73 | movb %al, %cl |
| 74 | sub $32, %cl |
| 75 | movl $1, %edx |
| 76 | shl %cl, %edx |
| 77 | subl $1, %edx |
| 78 | |
| 79 | /* Preload high word of address mask (in %edx) for Variable |
| 80 | MTRRs 0 and 1. */ |
| 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 | |
Yuchen He | 1e67adb | 2023-07-25 21:28:36 +0200 | [diff] [blame] | 88 | post_code(POSTCODE_SOC_SET_MTRR_BASE) |
Arthur Heymans | 7a8205b | 2018-06-03 10:29:07 +0200 | [diff] [blame] | 89 | /* Set Cache-as-RAM base address. */ |
| 90 | movl $(MTRR_PHYS_BASE(0)), %ecx |
Kyösti Mälkki | dc6bb6c | 2019-11-08 00:08:55 +0200 | [diff] [blame] | 91 | movl $_car_mtrr_start, %eax |
| 92 | orl $MTRR_TYPE_WRBACK, %eax |
Arthur Heymans | 7a8205b | 2018-06-03 10:29:07 +0200 | [diff] [blame] | 93 | xorl %edx, %edx |
| 94 | wrmsr |
| 95 | |
Yuchen He | 1e67adb | 2023-07-25 21:28:36 +0200 | [diff] [blame] | 96 | post_code(POSTCODE_SOC_SET_MTRR_MASK) |
Arthur Heymans | 7a8205b | 2018-06-03 10:29:07 +0200 | [diff] [blame] | 97 | /* Set Cache-as-RAM mask. */ |
| 98 | movl $(MTRR_PHYS_MASK(0)), %ecx |
Arthur Heymans | 3aa9adb | 2018-06-03 11:02:54 +0200 | [diff] [blame] | 99 | rdmsr |
Patrick Rudolph | c439e07 | 2020-09-28 22:31:06 +0200 | [diff] [blame] | 100 | movl 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 | 7a8205b | 2018-06-03 10:29:07 +0200 | [diff] [blame] | 102 | wrmsr |
| 103 | |
Yuchen He | 1e67adb | 2023-07-25 21:28:36 +0200 | [diff] [blame] | 104 | post_code(POSTCODE_SOC_ENABLE_MTRRS) |
Arthur Heymans | 7a8205b | 2018-06-03 10:29:07 +0200 | [diff] [blame] | 105 | |
| 106 | /* Enable MTRR. */ |
| 107 | movl $MTRR_DEF_TYPE_MSR, %ecx |
| 108 | rdmsr |
| 109 | orl $MTRR_DEF_TYPE_EN, %eax |
| 110 | wrmsr |
| 111 | |
| 112 | /* Enable L2 cache. */ |
| 113 | movl $0x11e, %ecx |
| 114 | rdmsr |
| 115 | orl $(1 << 8), %eax |
| 116 | wrmsr |
| 117 | |
| 118 | /* Enable cache (CR0.CD = 0, CR0.NW = 0). */ |
| 119 | movl %cr0, %eax |
| 120 | andl $(~(CR0_CacheDisable | CR0_NoWriteThrough)), %eax |
| 121 | invd |
| 122 | movl %eax, %cr0 |
| 123 | |
| 124 | /* 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] | 125 | cld |
Arthur Heymans | 7a8205b | 2018-06-03 10:29:07 +0200 | [diff] [blame] | 126 | xorl %eax, %eax |
Patrick Rudolph | c439e07 | 2020-09-28 22:31:06 +0200 | [diff] [blame] | 127 | movl car_mtrr_start, %edi |
| 128 | movl car_mtrr_size, %ecx |
Kyösti Mälkki | dc6bb6c | 2019-11-08 00:08:55 +0200 | [diff] [blame] | 129 | shr $2, %ecx |
Arthur Heymans | 7a8205b | 2018-06-03 10:29:07 +0200 | [diff] [blame] | 130 | rep stosl |
| 131 | |
Yuchen He | 1e67adb | 2023-07-25 21:28:36 +0200 | [diff] [blame] | 132 | post_code(POSTCODE_SOC_DISABLE_CACHE) |
Arthur Heymans | 7a8205b | 2018-06-03 10:29:07 +0200 | [diff] [blame] | 133 | /* Enable Cache-as-RAM mode by disabling cache. */ |
| 134 | movl %cr0, %eax |
| 135 | orl $CR0_CacheDisable, %eax |
| 136 | movl %eax, %cr0 |
| 137 | |
| 138 | /* Enable cache for our code in Flash because we do XIP here */ |
| 139 | movl $MTRR_PHYS_BASE(1), %ecx |
| 140 | xorl %edx, %edx |
Kyösti Mälkki | ce9f422 | 2018-06-25 18:53:36 +0300 | [diff] [blame] | 141 | movl $_program, %eax |
Patrick Rudolph | c439e07 | 2020-09-28 22:31:06 +0200 | [diff] [blame] | 142 | andl xip_mtrr_mask, %eax |
Arthur Heymans | 7a8205b | 2018-06-03 10:29:07 +0200 | [diff] [blame] | 143 | orl $MTRR_TYPE_WRPROT, %eax |
| 144 | wrmsr |
Arthur Heymans | 7a8205b | 2018-06-03 10:29:07 +0200 | [diff] [blame] | 145 | movl $MTRR_PHYS_MASK(1), %ecx |
Arthur Heymans | 3aa9adb | 2018-06-03 11:02:54 +0200 | [diff] [blame] | 146 | rdmsr |
Patrick Rudolph | c439e07 | 2020-09-28 22:31:06 +0200 | [diff] [blame] | 147 | movl xip_mtrr_mask, %eax |
Kyösti Mälkki | dc6bb6c | 2019-11-08 00:08:55 +0200 | [diff] [blame] | 148 | orl $MTRR_PHYS_MASK_VALID, %eax |
Arthur Heymans | 7a8205b | 2018-06-03 10:29:07 +0200 | [diff] [blame] | 149 | wrmsr |
| 150 | |
Yuchen He | 1e67adb | 2023-07-25 21:28:36 +0200 | [diff] [blame] | 151 | post_code(POSTCODE_SOC_ENABLE_CACHE) |
Arthur Heymans | 7a8205b | 2018-06-03 10:29:07 +0200 | [diff] [blame] | 152 | /* Enable cache. */ |
| 153 | movl %cr0, %eax |
| 154 | andl $(~(CR0_CacheDisable | CR0_NoWriteThrough)), %eax |
| 155 | movl %eax, %cr0 |
| 156 | |
| 157 | /* Setup the stack. */ |
Arthur Heymans | df9cdcf | 2019-11-09 06:50:20 +0100 | [diff] [blame] | 158 | mov $_ecar_stack, %esp |
Kyösti Mälkki | c641f7e | 2018-12-28 16:54:54 +0200 | [diff] [blame] | 159 | |
| 160 | /* Need to align stack to 16 bytes at call instruction. Account for |
| 161 | the pushes below. */ |
Arthur Heymans | 348b79f | 2018-06-03 17:14:19 +0200 | [diff] [blame] | 162 | andl $0xfffffff0, %esp |
Kyösti Mälkki | c641f7e | 2018-12-28 16:54:54 +0200 | [diff] [blame] | 163 | subl $4, %esp |
Arthur Heymans | 7a8205b | 2018-06-03 10:29:07 +0200 | [diff] [blame] | 164 | |
Patrick Rudolph | c439e07 | 2020-09-28 22:31:06 +0200 | [diff] [blame] | 165 | #if ENV_X86_64 |
Arthur Heymans | 3edf840 | 2021-07-05 21:18:50 +0200 | [diff] [blame^] | 166 | setup_longmode $(CONFIG_ARCH_X86_64_PGTBL_LOC) |
Patrick Rudolph | c439e07 | 2020-09-28 22:31:06 +0200 | [diff] [blame] | 167 | |
| 168 | movd %mm2, %rdi |
| 169 | shlq $32, %rdi |
| 170 | movd %mm1, %rsi |
| 171 | or %rsi, %rdi |
| 172 | movd %mm0, %rsi |
| 173 | #else |
Kyösti Mälkki | c641f7e | 2018-12-28 16:54:54 +0200 | [diff] [blame] | 174 | /* push TSC and BIST to stack */ |
| 175 | movd %mm0, %eax |
Elyes HAOUAS | 87930b3 | 2019-01-16 12:41:57 +0100 | [diff] [blame] | 176 | pushl %eax /* BIST */ |
Kyösti Mälkki | c641f7e | 2018-12-28 16:54:54 +0200 | [diff] [blame] | 177 | movd %mm2, %eax |
| 178 | pushl %eax /* tsc[63:32] */ |
| 179 | movd %mm1, %eax |
Elyes HAOUAS | 87930b3 | 2019-01-16 12:41:57 +0100 | [diff] [blame] | 180 | pushl %eax /* tsc[31:0] */ |
Patrick Rudolph | c439e07 | 2020-09-28 22:31:06 +0200 | [diff] [blame] | 181 | #endif |
Arthur Heymans | 7a8205b | 2018-06-03 10:29:07 +0200 | [diff] [blame] | 182 | |
Jeremy Compostella | b7832de | 2023-08-30 15:42:09 -0700 | [diff] [blame] | 183 | /* Copy .data section content to Cache-As-Ram */ |
| 184 | #include <cpu/x86/copy_data_section.inc> |
| 185 | |
Kyösti Mälkki | c641f7e | 2018-12-28 16:54:54 +0200 | [diff] [blame] | 186 | before_c_entry: |
Yuchen He | 1e67adb | 2023-07-25 21:28:36 +0200 | [diff] [blame] | 187 | post_code(POSTCODE_BOOTBLOCK_BEFORE_C_ENTRY) |
Kyösti Mälkki | c641f7e | 2018-12-28 16:54:54 +0200 | [diff] [blame] | 188 | call bootblock_c_entry_bist |
Arthur Heymans | 7a8205b | 2018-06-03 10:29:07 +0200 | [diff] [blame] | 189 | |
| 190 | /* Should never see this postcode */ |
lilacious | 40cb3fe | 2023-06-21 23:24:14 +0200 | [diff] [blame] | 191 | post_code(POSTCODE_DEAD_CODE) |
Arthur Heymans | 7a8205b | 2018-06-03 10:29:07 +0200 | [diff] [blame] | 192 | |
| 193 | .Lhlt: |
| 194 | hlt |
| 195 | jmp .Lhlt |
| 196 | |
Arthur Heymans | 3aa9adb | 2018-06-03 11:02:54 +0200 | [diff] [blame] | 197 | fixed_mtrr_list: |
| 198 | .word MTRR_FIX_64K_00000 |
| 199 | .word MTRR_FIX_16K_80000 |
| 200 | .word MTRR_FIX_16K_A0000 |
| 201 | .word MTRR_FIX_4K_C0000 |
| 202 | .word MTRR_FIX_4K_C8000 |
| 203 | .word MTRR_FIX_4K_D0000 |
| 204 | .word MTRR_FIX_4K_D8000 |
| 205 | .word MTRR_FIX_4K_E0000 |
| 206 | .word MTRR_FIX_4K_E8000 |
| 207 | .word MTRR_FIX_4K_F0000 |
| 208 | .word MTRR_FIX_4K_F8000 |
Arthur Heymans | 2834d98 | 2022-11-08 15:06:42 +0100 | [diff] [blame] | 209 | fixed_mtrr_list_end: |
Arthur Heymans | 7a8205b | 2018-06-03 10:29:07 +0200 | [diff] [blame] | 210 | |
| 211 | _cache_as_ram_setup_end: |