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 | |
| 3 | #include <cpu/x86/mtrr.h> |
| 4 | #include <cpu/x86/cache.h> |
| 5 | #include <cpu/x86/post_code.h> |
| 6 | |
Kyösti Mälkki | 7522a8f | 2020-11-20 16:47:38 +0200 | [diff] [blame] | 7 | .section .init |
Kyösti Mälkki | c641f7e | 2018-12-28 16:54:54 +0200 | [diff] [blame] | 8 | .global bootblock_pre_c_entry |
| 9 | |
Patrick Rudolph | c439e07 | 2020-09-28 22:31:06 +0200 | [diff] [blame^] | 10 | #include <cpu/intel/car/cache_as_ram_symbols.inc> |
| 11 | |
Arthur Heymans | 7a8205b | 2018-06-03 10:29:07 +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 | 7a8205b | 2018-06-03 10:29:07 +0200 | [diff] [blame] | 16 | |
| 17 | cache_as_ram: |
| 18 | post_code(0x20) |
| 19 | |
| 20 | /* Send INIT IPI to all excluding ourself. */ |
| 21 | movl $0x000C4500, %eax |
| 22 | movl $0xFEE00300, %esi |
| 23 | movl %eax, (%esi) |
| 24 | |
Arthur Heymans | 3aa9adb | 2018-06-03 11:02:54 +0200 | [diff] [blame] | 25 | /* All CPUs need to be in Wait for SIPI state */ |
| 26 | wait_for_sipi: |
| 27 | movl (%esi), %eax |
| 28 | bt $12, %eax |
| 29 | jc wait_for_sipi |
| 30 | |
| 31 | post_code(0x22) |
| 32 | |
| 33 | /* Clear/disable fixed MTRRs */ |
| 34 | mov $fixed_mtrr_list_size, %ebx |
| 35 | xor %eax, %eax |
| 36 | xor %edx, %edx |
| 37 | |
| 38 | clear_fixed_mtrr: |
| 39 | add $-2, %ebx |
| 40 | movzwl fixed_mtrr_list(%ebx), %ecx |
Arthur Heymans | 7a8205b | 2018-06-03 10:29:07 +0200 | [diff] [blame] | 41 | wrmsr |
Arthur Heymans | 3aa9adb | 2018-06-03 11:02:54 +0200 | [diff] [blame] | 42 | jnz clear_fixed_mtrr |
| 43 | |
Elyes HAOUAS | 02820ca | 2018-09-30 07:44:39 +0200 | [diff] [blame] | 44 | /* Figure out how many MTRRs we have, and clear them out */ |
Arthur Heymans | 3aa9adb | 2018-06-03 11:02:54 +0200 | [diff] [blame] | 45 | mov $MTRR_CAP_MSR, %ecx |
| 46 | rdmsr |
| 47 | movzb %al, %ebx /* Number of variable MTRRs */ |
| 48 | mov $MTRR_PHYS_BASE(0), %ecx |
| 49 | xor %eax, %eax |
| 50 | xor %edx, %edx |
| 51 | |
| 52 | clear_var_mtrr: |
| 53 | wrmsr |
| 54 | inc %ecx |
| 55 | wrmsr |
| 56 | inc %ecx |
| 57 | dec %ebx |
| 58 | jnz clear_var_mtrr |
Arthur Heymans | 7a8205b | 2018-06-03 10:29:07 +0200 | [diff] [blame] | 59 | |
| 60 | post_code(0x22) |
| 61 | /* Configure the default memory type to uncacheable. */ |
| 62 | movl $MTRR_DEF_TYPE_MSR, %ecx |
| 63 | rdmsr |
| 64 | andl $(~0x00000cff), %eax |
| 65 | wrmsr |
| 66 | |
Arthur Heymans | 3aa9adb | 2018-06-03 11:02:54 +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 | addrsize_set_high: |
| 79 | xorl %eax, %eax |
| 80 | movl $MTRR_PHYS_MASK(0), %ecx |
| 81 | wrmsr |
| 82 | movl $MTRR_PHYS_MASK(1), %ecx |
| 83 | wrmsr |
| 84 | |
Arthur Heymans | 7a8205b | 2018-06-03 10:29:07 +0200 | [diff] [blame] | 85 | post_code(0x23) |
| 86 | /* Set Cache-as-RAM base address. */ |
| 87 | movl $(MTRR_PHYS_BASE(0)), %ecx |
Kyösti Mälkki | dc6bb6c | 2019-11-08 00:08:55 +0200 | [diff] [blame] | 88 | movl $_car_mtrr_start, %eax |
| 89 | orl $MTRR_TYPE_WRBACK, %eax |
Arthur Heymans | 7a8205b | 2018-06-03 10:29:07 +0200 | [diff] [blame] | 90 | xorl %edx, %edx |
| 91 | wrmsr |
| 92 | |
| 93 | post_code(0x24) |
| 94 | /* Set Cache-as-RAM mask. */ |
| 95 | movl $(MTRR_PHYS_MASK(0)), %ecx |
Arthur Heymans | 3aa9adb | 2018-06-03 11:02:54 +0200 | [diff] [blame] | 96 | rdmsr |
Patrick Rudolph | c439e07 | 2020-09-28 22:31:06 +0200 | [diff] [blame^] | 97 | movl car_mtrr_mask, %eax |
Kyösti Mälkki | dc6bb6c | 2019-11-08 00:08:55 +0200 | [diff] [blame] | 98 | orl $MTRR_PHYS_MASK_VALID, %eax |
Arthur Heymans | 7a8205b | 2018-06-03 10:29:07 +0200 | [diff] [blame] | 99 | wrmsr |
| 100 | |
| 101 | post_code(0x25) |
| 102 | |
| 103 | /* Enable MTRR. */ |
| 104 | movl $MTRR_DEF_TYPE_MSR, %ecx |
| 105 | rdmsr |
| 106 | orl $MTRR_DEF_TYPE_EN, %eax |
| 107 | wrmsr |
| 108 | |
| 109 | /* Enable L2 cache. */ |
| 110 | movl $0x11e, %ecx |
| 111 | rdmsr |
| 112 | orl $(1 << 8), %eax |
| 113 | wrmsr |
| 114 | |
| 115 | /* Enable cache (CR0.CD = 0, CR0.NW = 0). */ |
| 116 | movl %cr0, %eax |
| 117 | andl $(~(CR0_CacheDisable | CR0_NoWriteThrough)), %eax |
| 118 | invd |
| 119 | movl %eax, %cr0 |
| 120 | |
| 121 | /* 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] | 122 | cld |
Arthur Heymans | 7a8205b | 2018-06-03 10:29:07 +0200 | [diff] [blame] | 123 | xorl %eax, %eax |
Patrick Rudolph | c439e07 | 2020-09-28 22:31:06 +0200 | [diff] [blame^] | 124 | movl car_mtrr_start, %edi |
| 125 | movl car_mtrr_size, %ecx |
Kyösti Mälkki | dc6bb6c | 2019-11-08 00:08:55 +0200 | [diff] [blame] | 126 | shr $2, %ecx |
Arthur Heymans | 7a8205b | 2018-06-03 10:29:07 +0200 | [diff] [blame] | 127 | rep stosl |
| 128 | |
| 129 | post_code(0x26) |
| 130 | /* Enable Cache-as-RAM mode by disabling cache. */ |
| 131 | movl %cr0, %eax |
| 132 | orl $CR0_CacheDisable, %eax |
| 133 | movl %eax, %cr0 |
| 134 | |
| 135 | /* Enable cache for our code in Flash because we do XIP here */ |
| 136 | movl $MTRR_PHYS_BASE(1), %ecx |
| 137 | xorl %edx, %edx |
Kyösti Mälkki | ce9f422 | 2018-06-25 18:53:36 +0300 | [diff] [blame] | 138 | movl $_program, %eax |
Patrick Rudolph | c439e07 | 2020-09-28 22:31:06 +0200 | [diff] [blame^] | 139 | andl xip_mtrr_mask, %eax |
Arthur Heymans | 7a8205b | 2018-06-03 10:29:07 +0200 | [diff] [blame] | 140 | orl $MTRR_TYPE_WRPROT, %eax |
| 141 | wrmsr |
Arthur Heymans | 7a8205b | 2018-06-03 10:29:07 +0200 | [diff] [blame] | 142 | movl $MTRR_PHYS_MASK(1), %ecx |
Arthur Heymans | 3aa9adb | 2018-06-03 11:02:54 +0200 | [diff] [blame] | 143 | rdmsr |
Patrick Rudolph | c439e07 | 2020-09-28 22:31:06 +0200 | [diff] [blame^] | 144 | movl xip_mtrr_mask, %eax |
Kyösti Mälkki | dc6bb6c | 2019-11-08 00:08:55 +0200 | [diff] [blame] | 145 | orl $MTRR_PHYS_MASK_VALID, %eax |
Arthur Heymans | 7a8205b | 2018-06-03 10:29:07 +0200 | [diff] [blame] | 146 | wrmsr |
| 147 | |
| 148 | post_code(0x28) |
| 149 | /* Enable cache. */ |
| 150 | movl %cr0, %eax |
| 151 | andl $(~(CR0_CacheDisable | CR0_NoWriteThrough)), %eax |
| 152 | movl %eax, %cr0 |
| 153 | |
| 154 | /* Setup the stack. */ |
Arthur Heymans | df9cdcf | 2019-11-09 06:50:20 +0100 | [diff] [blame] | 155 | mov $_ecar_stack, %esp |
Kyösti Mälkki | c641f7e | 2018-12-28 16:54:54 +0200 | [diff] [blame] | 156 | |
| 157 | /* Need to align stack to 16 bytes at call instruction. Account for |
| 158 | the pushes below. */ |
Arthur Heymans | 348b79f | 2018-06-03 17:14:19 +0200 | [diff] [blame] | 159 | andl $0xfffffff0, %esp |
Kyösti Mälkki | c641f7e | 2018-12-28 16:54:54 +0200 | [diff] [blame] | 160 | subl $4, %esp |
Arthur Heymans | 7a8205b | 2018-06-03 10:29:07 +0200 | [diff] [blame] | 161 | |
Patrick Rudolph | c439e07 | 2020-09-28 22:31:06 +0200 | [diff] [blame^] | 162 | #if ENV_X86_64 |
| 163 | |
| 164 | #include <cpu/x86/64bit/entry64.inc> |
| 165 | |
| 166 | movd %mm2, %rdi |
| 167 | shlq $32, %rdi |
| 168 | movd %mm1, %rsi |
| 169 | or %rsi, %rdi |
| 170 | movd %mm0, %rsi |
| 171 | #else |
Kyösti Mälkki | c641f7e | 2018-12-28 16:54:54 +0200 | [diff] [blame] | 172 | /* push TSC and BIST to stack */ |
| 173 | movd %mm0, %eax |
Elyes HAOUAS | 87930b3 | 2019-01-16 12:41:57 +0100 | [diff] [blame] | 174 | pushl %eax /* BIST */ |
Kyösti Mälkki | c641f7e | 2018-12-28 16:54:54 +0200 | [diff] [blame] | 175 | movd %mm2, %eax |
| 176 | pushl %eax /* tsc[63:32] */ |
| 177 | movd %mm1, %eax |
Elyes HAOUAS | 87930b3 | 2019-01-16 12:41:57 +0100 | [diff] [blame] | 178 | pushl %eax /* tsc[31:0] */ |
Patrick Rudolph | c439e07 | 2020-09-28 22:31:06 +0200 | [diff] [blame^] | 179 | #endif |
Arthur Heymans | 7a8205b | 2018-06-03 10:29:07 +0200 | [diff] [blame] | 180 | |
Kyösti Mälkki | c641f7e | 2018-12-28 16:54:54 +0200 | [diff] [blame] | 181 | before_c_entry: |
Arthur Heymans | 7a8205b | 2018-06-03 10:29:07 +0200 | [diff] [blame] | 182 | post_code(0x29) |
Kyösti Mälkki | c641f7e | 2018-12-28 16:54:54 +0200 | [diff] [blame] | 183 | call bootblock_c_entry_bist |
Arthur Heymans | 7a8205b | 2018-06-03 10:29:07 +0200 | [diff] [blame] | 184 | |
| 185 | /* Should never see this postcode */ |
| 186 | post_code(POST_DEAD_CODE) |
| 187 | |
| 188 | .Lhlt: |
| 189 | hlt |
| 190 | jmp .Lhlt |
| 191 | |
Arthur Heymans | 3aa9adb | 2018-06-03 11:02:54 +0200 | [diff] [blame] | 192 | fixed_mtrr_list: |
| 193 | .word MTRR_FIX_64K_00000 |
| 194 | .word MTRR_FIX_16K_80000 |
| 195 | .word MTRR_FIX_16K_A0000 |
| 196 | .word MTRR_FIX_4K_C0000 |
| 197 | .word MTRR_FIX_4K_C8000 |
| 198 | .word MTRR_FIX_4K_D0000 |
| 199 | .word MTRR_FIX_4K_D8000 |
| 200 | .word MTRR_FIX_4K_E0000 |
| 201 | .word MTRR_FIX_4K_E8000 |
| 202 | .word MTRR_FIX_4K_F0000 |
| 203 | .word MTRR_FIX_4K_F8000 |
| 204 | fixed_mtrr_list_size = . - fixed_mtrr_list |
Arthur Heymans | 7a8205b | 2018-06-03 10:29:07 +0200 | [diff] [blame] | 205 | |
| 206 | _cache_as_ram_setup_end: |