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