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