Arthur Heymans | dd4d895 | 2018-06-03 12:04:26 +0200 | [diff] [blame] | 1 | /* |
| 2 | * This file is part of the coreboot project. |
| 3 | * |
| 4 | * Copyright (C) 2000,2007 Ronald G. Minnich <rminnich@gmail.com> |
| 5 | * Copyright (C) 2007-2008 coresystems GmbH |
Arthur Heymans | c2ccc97 | 2018-06-03 12:09:52 +0200 | [diff] [blame] | 6 | * Copyright (C) 2012 Kyösti Mälkki <kyosti.malkki@gmail.com> |
Arthur Heymans | dd4d895 | 2018-06-03 12:04:26 +0200 | [diff] [blame] | 7 | * |
| 8 | * This program is free software; you can redistribute it and/or modify |
| 9 | * it under the terms of the GNU General Public License as published by |
| 10 | * the Free Software Foundation; version 2 of the License. |
| 11 | * |
| 12 | * This program is distributed in the hope that it will be useful, |
| 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 | * GNU General Public License for more details. |
| 16 | */ |
| 17 | |
| 18 | #include <cpu/x86/mtrr.h> |
| 19 | #include <cpu/x86/cache.h> |
| 20 | #include <cpu/x86/post_code.h> |
| 21 | |
| 22 | #define CACHE_AS_RAM_SIZE (CONFIG_DCACHE_RAM_SIZE \ |
| 23 | + CONFIG_DCACHE_RAM_MRC_VAR_SIZE) |
| 24 | #define CACHE_AS_RAM_BASE CONFIG_DCACHE_RAM_BASE |
| 25 | |
Arthur Heymans | dd4d895 | 2018-06-03 12:04:26 +0200 | [diff] [blame] | 26 | #define NoEvictMod_MSR 0x2e0 |
| 27 | |
Kyösti Mälkki | c641f7e | 2018-12-28 16:54:54 +0200 | [diff] [blame] | 28 | .global bootblock_pre_c_entry |
| 29 | |
Arthur Heymans | dd4d895 | 2018-06-03 12:04:26 +0200 | [diff] [blame] | 30 | .code32 |
| 31 | _cache_as_ram_setup: |
| 32 | |
Kyösti Mälkki | c641f7e | 2018-12-28 16:54:54 +0200 | [diff] [blame] | 33 | bootblock_pre_c_entry: |
Arthur Heymans | dd4d895 | 2018-06-03 12:04:26 +0200 | [diff] [blame] | 34 | |
| 35 | cache_as_ram: |
| 36 | post_code(0x20) |
| 37 | |
| 38 | /* Send INIT IPI to all excluding ourself. */ |
| 39 | movl $0x000C4500, %eax |
| 40 | movl $0xFEE00300, %esi |
| 41 | movl %eax, (%esi) |
| 42 | |
| 43 | /* All CPUs need to be in Wait for SIPI state */ |
| 44 | wait_for_sipi: |
| 45 | movl (%esi), %eax |
| 46 | bt $12, %eax |
| 47 | jc wait_for_sipi |
| 48 | |
| 49 | post_code(0x21) |
| 50 | /* Clean-up MTRR_DEF_TYPE_MSR. */ |
| 51 | movl $MTRR_DEF_TYPE_MSR, %ecx |
| 52 | xorl %eax, %eax |
| 53 | xorl %edx, %edx |
| 54 | wrmsr |
| 55 | |
| 56 | post_code(0x22) |
Arthur Heymans | c2ccc97 | 2018-06-03 12:09:52 +0200 | [diff] [blame] | 57 | /* Clear/disable fixed MTRRs */ |
| 58 | mov $fixed_mtrr_list_size, %ebx |
| 59 | xor %eax, %eax |
| 60 | xor %edx, %edx |
| 61 | |
| 62 | clear_fixed_mtrr: |
| 63 | add $-2, %ebx |
| 64 | movzwl fixed_mtrr_list(%ebx), %ecx |
Arthur Heymans | dd4d895 | 2018-06-03 12:04:26 +0200 | [diff] [blame] | 65 | wrmsr |
Arthur Heymans | c2ccc97 | 2018-06-03 12:09:52 +0200 | [diff] [blame] | 66 | jnz clear_fixed_mtrr |
Arthur Heymans | dd4d895 | 2018-06-03 12:04:26 +0200 | [diff] [blame] | 67 | |
| 68 | /* Zero out all variable range MTRRs. */ |
| 69 | movl $MTRR_CAP_MSR, %ecx |
| 70 | rdmsr |
| 71 | andl $0xff, %eax |
| 72 | shl $1, %eax |
| 73 | movl %eax, %edi |
| 74 | movl $0x200, %ecx |
| 75 | xorl %eax, %eax |
| 76 | xorl %edx, %edx |
| 77 | clear_var_mtrrs: |
| 78 | wrmsr |
| 79 | add $1, %ecx |
| 80 | dec %edi |
| 81 | jnz clear_var_mtrrs |
| 82 | |
Arthur Heymans | c2ccc97 | 2018-06-03 12:09:52 +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 | */ |
| 95 | addrsize_set_high: |
| 96 | xorl %eax, %eax |
| 97 | movl $MTRR_PHYS_MASK(0), %ecx |
| 98 | wrmsr |
| 99 | movl $MTRR_PHYS_MASK(1), %ecx |
| 100 | wrmsr |
| 101 | |
| 102 | |
Arthur Heymans | dd4d895 | 2018-06-03 12:04:26 +0200 | [diff] [blame] | 103 | post_code(0x23) |
| 104 | /* Set Cache-as-RAM base address. */ |
| 105 | movl $(MTRR_PHYS_BASE(0)), %ecx |
| 106 | movl $(CACHE_AS_RAM_BASE | MTRR_TYPE_WRBACK), %eax |
| 107 | xorl %edx, %edx |
| 108 | wrmsr |
| 109 | |
| 110 | post_code(0x24) |
| 111 | /* Set Cache-as-RAM mask. */ |
| 112 | movl $(MTRR_PHYS_MASK(0)), %ecx |
Arthur Heymans | c2ccc97 | 2018-06-03 12:09:52 +0200 | [diff] [blame] | 113 | rdmsr |
Arthur Heymans | dd4d895 | 2018-06-03 12:04:26 +0200 | [diff] [blame] | 114 | movl $(~(CACHE_AS_RAM_SIZE - 1) | MTRR_PHYS_MASK_VALID), %eax |
Arthur Heymans | dd4d895 | 2018-06-03 12:04:26 +0200 | [diff] [blame] | 115 | wrmsr |
| 116 | |
| 117 | post_code(0x25) |
| 118 | |
| 119 | /* Enable MTRR. */ |
| 120 | movl $MTRR_DEF_TYPE_MSR, %ecx |
| 121 | rdmsr |
| 122 | orl $MTRR_DEF_TYPE_EN, %eax |
| 123 | wrmsr |
| 124 | |
| 125 | /* Enable cache (CR0.CD = 0, CR0.NW = 0). */ |
| 126 | movl %cr0, %eax |
| 127 | andl $(~(CR0_CacheDisable | CR0_NoWriteThrough)), %eax |
| 128 | invd |
| 129 | movl %eax, %cr0 |
| 130 | |
| 131 | /* enable the 'no eviction' mode */ |
Arthur Heymans | a28befd | 2018-12-20 13:59:34 +0100 | [diff] [blame] | 132 | movl $NoEvictMod_MSR, %ecx |
Arthur Heymans | dd4d895 | 2018-06-03 12:04:26 +0200 | [diff] [blame] | 133 | rdmsr |
Arthur Heymans | a28befd | 2018-12-20 13:59:34 +0100 | [diff] [blame] | 134 | orl $1, %eax |
| 135 | andl $~2, %eax |
Arthur Heymans | dd4d895 | 2018-06-03 12:04:26 +0200 | [diff] [blame] | 136 | wrmsr |
| 137 | |
| 138 | /* Clear the cache memory region. This will also fill up the cache. */ |
| 139 | movl $CACHE_AS_RAM_BASE, %esi |
| 140 | movl %esi, %edi |
| 141 | movl $(CACHE_AS_RAM_SIZE >> 2), %ecx |
Arthur Heymans | dd4d895 | 2018-06-03 12:04:26 +0200 | [diff] [blame] | 142 | xorl %eax, %eax |
| 143 | rep stosl |
| 144 | |
| 145 | /* enable the 'no eviction run' state */ |
Arthur Heymans | a28befd | 2018-12-20 13:59:34 +0100 | [diff] [blame] | 146 | movl $NoEvictMod_MSR, %ecx |
Arthur Heymans | dd4d895 | 2018-06-03 12:04:26 +0200 | [diff] [blame] | 147 | rdmsr |
Arthur Heymans | a28befd | 2018-12-20 13:59:34 +0100 | [diff] [blame] | 148 | orl $3, %eax |
Arthur Heymans | dd4d895 | 2018-06-03 12:04:26 +0200 | [diff] [blame] | 149 | wrmsr |
| 150 | |
| 151 | post_code(0x26) |
| 152 | /* Enable Cache-as-RAM mode by disabling cache. */ |
| 153 | movl %cr0, %eax |
| 154 | orl $CR0_CacheDisable, %eax |
| 155 | movl %eax, %cr0 |
| 156 | |
| 157 | /* Enable cache for our code in Flash because we do XIP here */ |
| 158 | movl $MTRR_PHYS_BASE(1), %ecx |
| 159 | xorl %edx, %edx |
Arthur Heymans | c2ccc97 | 2018-06-03 12:09:52 +0200 | [diff] [blame] | 160 | movl $CACHE_ROM_BASE | MTRR_TYPE_WRPROT, %eax |
Arthur Heymans | dd4d895 | 2018-06-03 12:04:26 +0200 | [diff] [blame] | 161 | wrmsr |
| 162 | |
| 163 | movl $MTRR_PHYS_MASK(1), %ecx |
Arthur Heymans | c2ccc97 | 2018-06-03 12:09:52 +0200 | [diff] [blame] | 164 | rdmsr |
| 165 | movl $(~(CACHE_ROM_SIZE - 1) | MTRR_PHYS_MASK_VALID), %eax |
Arthur Heymans | dd4d895 | 2018-06-03 12:04:26 +0200 | [diff] [blame] | 166 | wrmsr |
| 167 | |
| 168 | post_code(0x28) |
| 169 | /* Enable cache. */ |
| 170 | movl %cr0, %eax |
| 171 | andl $(~(CR0_CacheDisable | CR0_NoWriteThrough)), %eax |
| 172 | movl %eax, %cr0 |
| 173 | |
| 174 | /* Setup the stack. */ |
Kyösti Mälkki | c641f7e | 2018-12-28 16:54:54 +0200 | [diff] [blame] | 175 | mov $_car_stack_end, %esp |
Arthur Heymans | dd4d895 | 2018-06-03 12:04:26 +0200 | [diff] [blame] | 176 | |
Kyösti Mälkki | c641f7e | 2018-12-28 16:54:54 +0200 | [diff] [blame] | 177 | /* Need to align stack to 16 bytes at call instruction. Account for |
| 178 | the pushes below. */ |
Arthur Heymans | 348b79f | 2018-06-03 17:14:19 +0200 | [diff] [blame] | 179 | andl $0xfffffff0, %esp |
Kyösti Mälkki | c641f7e | 2018-12-28 16:54:54 +0200 | [diff] [blame] | 180 | subl $4, %esp |
Arthur Heymans | 348b79f | 2018-06-03 17:14:19 +0200 | [diff] [blame] | 181 | |
Kyösti Mälkki | c641f7e | 2018-12-28 16:54:54 +0200 | [diff] [blame] | 182 | /* push TSC and BIST to stack */ |
| 183 | movd %mm0, %eax |
| 184 | pushl %eax /* BIST */ |
| 185 | movd %mm2, %eax |
| 186 | pushl %eax /* tsc[63:32] */ |
| 187 | movd %mm1, %eax |
| 188 | pushl %eax /* tsc[31:0] */ |
Arthur Heymans | dd4d895 | 2018-06-03 12:04:26 +0200 | [diff] [blame] | 189 | |
Kyösti Mälkki | c641f7e | 2018-12-28 16:54:54 +0200 | [diff] [blame] | 190 | before_c_entry: |
Arthur Heymans | dd4d895 | 2018-06-03 12:04:26 +0200 | [diff] [blame] | 191 | post_code(0x29) |
Kyösti Mälkki | c641f7e | 2018-12-28 16:54:54 +0200 | [diff] [blame] | 192 | call bootblock_c_entry_bist |
Arthur Heymans | dd4d895 | 2018-06-03 12:04:26 +0200 | [diff] [blame] | 193 | |
| 194 | /* Should never see this postcode */ |
| 195 | post_code(POST_DEAD_CODE) |
| 196 | |
| 197 | |
| 198 | .Lhlt: |
| 199 | hlt |
| 200 | jmp .Lhlt |
| 201 | |
Arthur Heymans | c2ccc97 | 2018-06-03 12:09:52 +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 | dd4d895 | 2018-06-03 12:04:26 +0200 | [diff] [blame] | 215 | |
| 216 | _cache_as_ram_setup_end: |