Arthur Heymans | 7a8205b | 2018-06-03 10:29:07 +0200 | [diff] [blame] | 1 | /* |
| 2 | * This file is part of the coreboot project. |
| 3 | * |
Arthur Heymans | 7a8205b | 2018-06-03 10:29:07 +0200 | [diff] [blame] | 4 | * This program is free software; you can redistribute it and/or modify |
| 5 | * it under the terms of the GNU General Public License as published by |
| 6 | * the Free Software Foundation; version 2 of the License. |
| 7 | * |
| 8 | * This program is distributed in the hope that it will be useful, |
| 9 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 10 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 11 | * GNU General Public License for more details. |
| 12 | */ |
| 13 | |
| 14 | #include <cpu/x86/mtrr.h> |
| 15 | #include <cpu/x86/cache.h> |
| 16 | #include <cpu/x86/post_code.h> |
| 17 | |
Arthur Heymans | 7a8205b | 2018-06-03 10:29:07 +0200 | [diff] [blame] | 18 | #define CACHE_AS_RAM_SIZE CONFIG_DCACHE_RAM_SIZE |
| 19 | #define CACHE_AS_RAM_BASE CONFIG_DCACHE_RAM_BASE |
| 20 | |
Arthur Heymans | 942ad6a | 2019-10-12 18:06:46 +0200 | [diff] [blame] | 21 | #if CONFIG(C_ENVIRONMENT_BOOTBLOCK) |
| 22 | #if ((CONFIG_C_ENV_BOOTBLOCK_SIZE & (CONFIG_C_ENV_BOOTBLOCK_SIZE - 1)) != 0) |
| 23 | #error "CONFIG_C_ENV_BOOTBLOCK_SIZE must be a power of 2!" |
| 24 | #endif |
| 25 | #define XIP_ROM_SIZE CONFIG_C_ENV_BOOTBLOCK_SIZE |
| 26 | #else |
| 27 | #define XIP_ROM_SIZE CONFIG_XIP_ROM_SIZE |
| 28 | #endif |
| 29 | |
Kyösti Mälkki | c641f7e | 2018-12-28 16:54:54 +0200 | [diff] [blame] | 30 | .global bootblock_pre_c_entry |
| 31 | |
Arthur Heymans | 7a8205b | 2018-06-03 10:29:07 +0200 | [diff] [blame] | 32 | .code32 |
| 33 | _cache_as_ram_setup: |
| 34 | |
Kyösti Mälkki | c641f7e | 2018-12-28 16:54:54 +0200 | [diff] [blame] | 35 | bootblock_pre_c_entry: |
Arthur Heymans | 7a8205b | 2018-06-03 10:29:07 +0200 | [diff] [blame] | 36 | |
| 37 | cache_as_ram: |
| 38 | post_code(0x20) |
| 39 | |
| 40 | /* Send INIT IPI to all excluding ourself. */ |
| 41 | movl $0x000C4500, %eax |
| 42 | movl $0xFEE00300, %esi |
| 43 | movl %eax, (%esi) |
| 44 | |
Arthur Heymans | 3aa9adb | 2018-06-03 11:02:54 +0200 | [diff] [blame] | 45 | /* All CPUs need to be in Wait for SIPI state */ |
| 46 | wait_for_sipi: |
| 47 | movl (%esi), %eax |
| 48 | bt $12, %eax |
| 49 | jc wait_for_sipi |
| 50 | |
| 51 | post_code(0x22) |
| 52 | |
| 53 | /* Clear/disable fixed MTRRs */ |
| 54 | mov $fixed_mtrr_list_size, %ebx |
| 55 | xor %eax, %eax |
| 56 | xor %edx, %edx |
| 57 | |
| 58 | clear_fixed_mtrr: |
| 59 | add $-2, %ebx |
| 60 | movzwl fixed_mtrr_list(%ebx), %ecx |
Arthur Heymans | 7a8205b | 2018-06-03 10:29:07 +0200 | [diff] [blame] | 61 | wrmsr |
Arthur Heymans | 3aa9adb | 2018-06-03 11:02:54 +0200 | [diff] [blame] | 62 | jnz clear_fixed_mtrr |
| 63 | |
Elyes HAOUAS | 02820ca | 2018-09-30 07:44:39 +0200 | [diff] [blame] | 64 | /* Figure out how many MTRRs we have, and clear them out */ |
Arthur Heymans | 3aa9adb | 2018-06-03 11:02:54 +0200 | [diff] [blame] | 65 | mov $MTRR_CAP_MSR, %ecx |
| 66 | rdmsr |
| 67 | movzb %al, %ebx /* Number of variable MTRRs */ |
| 68 | mov $MTRR_PHYS_BASE(0), %ecx |
| 69 | xor %eax, %eax |
| 70 | xor %edx, %edx |
| 71 | |
| 72 | clear_var_mtrr: |
| 73 | wrmsr |
| 74 | inc %ecx |
| 75 | wrmsr |
| 76 | inc %ecx |
| 77 | dec %ebx |
| 78 | jnz clear_var_mtrr |
Arthur Heymans | 7a8205b | 2018-06-03 10:29:07 +0200 | [diff] [blame] | 79 | |
| 80 | post_code(0x22) |
| 81 | /* Configure the default memory type to uncacheable. */ |
| 82 | movl $MTRR_DEF_TYPE_MSR, %ecx |
| 83 | rdmsr |
| 84 | andl $(~0x00000cff), %eax |
| 85 | wrmsr |
| 86 | |
Arthur Heymans | 3aa9adb | 2018-06-03 11:02:54 +0200 | [diff] [blame] | 87 | /* Determine CPU_ADDR_BITS and load PHYSMASK high word to %edx. */ |
| 88 | movl $0x80000008, %eax |
| 89 | cpuid |
| 90 | movb %al, %cl |
| 91 | sub $32, %cl |
| 92 | movl $1, %edx |
| 93 | shl %cl, %edx |
| 94 | subl $1, %edx |
| 95 | |
| 96 | /* Preload high word of address mask (in %edx) for Variable |
| 97 | MTRRs 0 and 1. */ |
| 98 | addrsize_set_high: |
| 99 | xorl %eax, %eax |
| 100 | movl $MTRR_PHYS_MASK(0), %ecx |
| 101 | wrmsr |
| 102 | movl $MTRR_PHYS_MASK(1), %ecx |
| 103 | wrmsr |
| 104 | |
Arthur Heymans | 7a8205b | 2018-06-03 10:29:07 +0200 | [diff] [blame] | 105 | post_code(0x23) |
| 106 | /* Set Cache-as-RAM base address. */ |
| 107 | movl $(MTRR_PHYS_BASE(0)), %ecx |
| 108 | movl $(CACHE_AS_RAM_BASE | MTRR_TYPE_WRBACK), %eax |
| 109 | xorl %edx, %edx |
| 110 | wrmsr |
| 111 | |
| 112 | post_code(0x24) |
| 113 | /* Set Cache-as-RAM mask. */ |
| 114 | movl $(MTRR_PHYS_MASK(0)), %ecx |
Arthur Heymans | 3aa9adb | 2018-06-03 11:02:54 +0200 | [diff] [blame] | 115 | rdmsr |
Arthur Heymans | 7a8205b | 2018-06-03 10:29:07 +0200 | [diff] [blame] | 116 | movl $(~(CACHE_AS_RAM_SIZE - 1) | MTRR_PHYS_MASK_VALID), %eax |
Arthur Heymans | 7a8205b | 2018-06-03 10:29:07 +0200 | [diff] [blame] | 117 | wrmsr |
| 118 | |
| 119 | post_code(0x25) |
| 120 | |
| 121 | /* Enable MTRR. */ |
| 122 | movl $MTRR_DEF_TYPE_MSR, %ecx |
| 123 | rdmsr |
| 124 | orl $MTRR_DEF_TYPE_EN, %eax |
| 125 | wrmsr |
| 126 | |
| 127 | /* Enable L2 cache. */ |
| 128 | movl $0x11e, %ecx |
| 129 | rdmsr |
| 130 | orl $(1 << 8), %eax |
| 131 | wrmsr |
| 132 | |
| 133 | /* Enable cache (CR0.CD = 0, CR0.NW = 0). */ |
| 134 | movl %cr0, %eax |
| 135 | andl $(~(CR0_CacheDisable | CR0_NoWriteThrough)), %eax |
| 136 | invd |
| 137 | movl %eax, %cr0 |
| 138 | |
| 139 | /* Clear the cache memory region. This will also fill up the cache. */ |
| 140 | movl $CACHE_AS_RAM_BASE, %esi |
| 141 | movl %esi, %edi |
| 142 | movl $(CACHE_AS_RAM_SIZE >> 2), %ecx |
Arthur Heymans | 7a8205b | 2018-06-03 10:29:07 +0200 | [diff] [blame] | 143 | xorl %eax, %eax |
| 144 | rep stosl |
| 145 | |
| 146 | post_code(0x26) |
| 147 | /* Enable Cache-as-RAM mode by disabling cache. */ |
| 148 | movl %cr0, %eax |
| 149 | orl $CR0_CacheDisable, %eax |
| 150 | movl %eax, %cr0 |
| 151 | |
| 152 | /* Enable cache for our code in Flash because we do XIP here */ |
| 153 | movl $MTRR_PHYS_BASE(1), %ecx |
| 154 | xorl %edx, %edx |
| 155 | /* |
| 156 | * IMPORTANT: The following calculation _must_ be done at runtime. See |
Stefan Tauner | de02878 | 2018-08-19 20:02:05 +0200 | [diff] [blame] | 157 | * https://mail.coreboot.org/pipermail/coreboot/2010-October/060922.html |
Arthur Heymans | 7a8205b | 2018-06-03 10:29:07 +0200 | [diff] [blame] | 158 | */ |
Kyösti Mälkki | ce9f422 | 2018-06-25 18:53:36 +0300 | [diff] [blame] | 159 | movl $_program, %eax |
Arthur Heymans | 942ad6a | 2019-10-12 18:06:46 +0200 | [diff] [blame] | 160 | andl $(~(XIP_ROM_SIZE - 1)), %eax |
Arthur Heymans | 7a8205b | 2018-06-03 10:29:07 +0200 | [diff] [blame] | 161 | orl $MTRR_TYPE_WRPROT, %eax |
| 162 | wrmsr |
| 163 | |
| 164 | movl $MTRR_PHYS_MASK(1), %ecx |
Arthur Heymans | 3aa9adb | 2018-06-03 11:02:54 +0200 | [diff] [blame] | 165 | rdmsr |
Arthur Heymans | 942ad6a | 2019-10-12 18:06:46 +0200 | [diff] [blame] | 166 | movl $(~(XIP_ROM_SIZE - 1) | MTRR_PHYS_MASK_VALID), %eax |
Arthur Heymans | 7a8205b | 2018-06-03 10:29:07 +0200 | [diff] [blame] | 167 | wrmsr |
| 168 | |
| 169 | post_code(0x28) |
| 170 | /* Enable cache. */ |
| 171 | movl %cr0, %eax |
| 172 | andl $(~(CR0_CacheDisable | CR0_NoWriteThrough)), %eax |
| 173 | movl %eax, %cr0 |
| 174 | |
| 175 | /* Setup the stack. */ |
Arthur Heymans | df9cdcf | 2019-11-09 06:50:20 +0100 | [diff] [blame^] | 176 | mov $_ecar_stack, %esp |
Kyösti Mälkki | c641f7e | 2018-12-28 16:54:54 +0200 | [diff] [blame] | 177 | |
| 178 | /* Need to align stack to 16 bytes at call instruction. Account for |
| 179 | the pushes below. */ |
Arthur Heymans | 348b79f | 2018-06-03 17:14:19 +0200 | [diff] [blame] | 180 | andl $0xfffffff0, %esp |
Kyösti Mälkki | c641f7e | 2018-12-28 16:54:54 +0200 | [diff] [blame] | 181 | subl $4, %esp |
Arthur Heymans | 7a8205b | 2018-06-03 10:29:07 +0200 | [diff] [blame] | 182 | |
Kyösti Mälkki | c641f7e | 2018-12-28 16:54:54 +0200 | [diff] [blame] | 183 | /* push TSC and BIST to stack */ |
| 184 | movd %mm0, %eax |
Elyes HAOUAS | 87930b3 | 2019-01-16 12:41:57 +0100 | [diff] [blame] | 185 | pushl %eax /* BIST */ |
Kyösti Mälkki | c641f7e | 2018-12-28 16:54:54 +0200 | [diff] [blame] | 186 | movd %mm2, %eax |
| 187 | pushl %eax /* tsc[63:32] */ |
| 188 | movd %mm1, %eax |
Elyes HAOUAS | 87930b3 | 2019-01-16 12:41:57 +0100 | [diff] [blame] | 189 | pushl %eax /* tsc[31:0] */ |
Arthur Heymans | 7a8205b | 2018-06-03 10:29:07 +0200 | [diff] [blame] | 190 | |
Kyösti Mälkki | c641f7e | 2018-12-28 16:54:54 +0200 | [diff] [blame] | 191 | before_c_entry: |
Arthur Heymans | 7a8205b | 2018-06-03 10:29:07 +0200 | [diff] [blame] | 192 | post_code(0x29) |
Kyösti Mälkki | c641f7e | 2018-12-28 16:54:54 +0200 | [diff] [blame] | 193 | call bootblock_c_entry_bist |
Arthur Heymans | 7a8205b | 2018-06-03 10:29:07 +0200 | [diff] [blame] | 194 | |
| 195 | /* Should never see this postcode */ |
| 196 | post_code(POST_DEAD_CODE) |
| 197 | |
| 198 | .Lhlt: |
| 199 | hlt |
| 200 | jmp .Lhlt |
| 201 | |
Arthur Heymans | 3aa9adb | 2018-06-03 11:02:54 +0200 | [diff] [blame] | 202 | fixed_mtrr_list: |
| 203 | .word MTRR_FIX_64K_00000 |
| 204 | .word MTRR_FIX_16K_80000 |
| 205 | .word MTRR_FIX_16K_A0000 |
| 206 | .word MTRR_FIX_4K_C0000 |
| 207 | .word MTRR_FIX_4K_C8000 |
| 208 | .word MTRR_FIX_4K_D0000 |
| 209 | .word MTRR_FIX_4K_D8000 |
| 210 | .word MTRR_FIX_4K_E0000 |
| 211 | .word MTRR_FIX_4K_E8000 |
| 212 | .word MTRR_FIX_4K_F0000 |
| 213 | .word MTRR_FIX_4K_F8000 |
| 214 | fixed_mtrr_list_size = . - fixed_mtrr_list |
Arthur Heymans | 7a8205b | 2018-06-03 10:29:07 +0200 | [diff] [blame] | 215 | |
| 216 | _cache_as_ram_setup_end: |