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 | |
| 28 | .code32 |
| 29 | _cache_as_ram_setup: |
| 30 | |
| 31 | /* Save the BIST result. */ |
| 32 | movl %eax, %ebp |
| 33 | |
| 34 | cache_as_ram: |
| 35 | post_code(0x20) |
| 36 | |
| 37 | /* Send INIT IPI to all excluding ourself. */ |
| 38 | movl $0x000C4500, %eax |
| 39 | movl $0xFEE00300, %esi |
| 40 | movl %eax, (%esi) |
| 41 | |
| 42 | /* All CPUs need to be in Wait for SIPI state */ |
| 43 | wait_for_sipi: |
| 44 | movl (%esi), %eax |
| 45 | bt $12, %eax |
| 46 | jc wait_for_sipi |
| 47 | |
| 48 | post_code(0x21) |
| 49 | /* Clean-up MTRR_DEF_TYPE_MSR. */ |
| 50 | movl $MTRR_DEF_TYPE_MSR, %ecx |
| 51 | xorl %eax, %eax |
| 52 | xorl %edx, %edx |
| 53 | wrmsr |
| 54 | |
| 55 | post_code(0x22) |
Arthur Heymans | c2ccc97 | 2018-06-03 12:09:52 +0200 | [diff] [blame] | 56 | /* Clear/disable fixed MTRRs */ |
| 57 | mov $fixed_mtrr_list_size, %ebx |
| 58 | xor %eax, %eax |
| 59 | xor %edx, %edx |
| 60 | |
| 61 | clear_fixed_mtrr: |
| 62 | add $-2, %ebx |
| 63 | movzwl fixed_mtrr_list(%ebx), %ecx |
Arthur Heymans | dd4d895 | 2018-06-03 12:04:26 +0200 | [diff] [blame] | 64 | wrmsr |
Arthur Heymans | c2ccc97 | 2018-06-03 12:09:52 +0200 | [diff] [blame] | 65 | jnz clear_fixed_mtrr |
Arthur Heymans | dd4d895 | 2018-06-03 12:04:26 +0200 | [diff] [blame] | 66 | |
| 67 | /* Zero out all variable range MTRRs. */ |
| 68 | movl $MTRR_CAP_MSR, %ecx |
| 69 | rdmsr |
| 70 | andl $0xff, %eax |
| 71 | shl $1, %eax |
| 72 | movl %eax, %edi |
| 73 | movl $0x200, %ecx |
| 74 | xorl %eax, %eax |
| 75 | xorl %edx, %edx |
| 76 | clear_var_mtrrs: |
| 77 | wrmsr |
| 78 | add $1, %ecx |
| 79 | dec %edi |
| 80 | jnz clear_var_mtrrs |
| 81 | |
Arthur Heymans | c2ccc97 | 2018-06-03 12:09:52 +0200 | [diff] [blame] | 82 | /* Determine CPU_ADDR_BITS and load PHYSMASK high word to %edx. */ |
| 83 | movl $0x80000008, %eax |
| 84 | cpuid |
| 85 | movb %al, %cl |
| 86 | sub $32, %cl |
| 87 | movl $1, %edx |
| 88 | shl %cl, %edx |
| 89 | subl $1, %edx |
| 90 | |
| 91 | /* Preload high word of address mask (in %edx) for Variable |
| 92 | * MTRRs 0 and 1. |
| 93 | */ |
| 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 | |
| 101 | |
Arthur Heymans | dd4d895 | 2018-06-03 12:04:26 +0200 | [diff] [blame] | 102 | post_code(0x23) |
| 103 | /* Set Cache-as-RAM base address. */ |
| 104 | movl $(MTRR_PHYS_BASE(0)), %ecx |
| 105 | movl $(CACHE_AS_RAM_BASE | MTRR_TYPE_WRBACK), %eax |
| 106 | xorl %edx, %edx |
| 107 | wrmsr |
| 108 | |
| 109 | post_code(0x24) |
| 110 | /* Set Cache-as-RAM mask. */ |
| 111 | movl $(MTRR_PHYS_MASK(0)), %ecx |
Arthur Heymans | c2ccc97 | 2018-06-03 12:09:52 +0200 | [diff] [blame] | 112 | rdmsr |
Arthur Heymans | dd4d895 | 2018-06-03 12:04:26 +0200 | [diff] [blame] | 113 | movl $(~(CACHE_AS_RAM_SIZE - 1) | MTRR_PHYS_MASK_VALID), %eax |
Arthur Heymans | dd4d895 | 2018-06-03 12:04:26 +0200 | [diff] [blame] | 114 | wrmsr |
| 115 | |
| 116 | post_code(0x25) |
| 117 | |
| 118 | /* Enable MTRR. */ |
| 119 | movl $MTRR_DEF_TYPE_MSR, %ecx |
| 120 | rdmsr |
| 121 | orl $MTRR_DEF_TYPE_EN, %eax |
| 122 | wrmsr |
| 123 | |
| 124 | /* Enable cache (CR0.CD = 0, CR0.NW = 0). */ |
| 125 | movl %cr0, %eax |
| 126 | andl $(~(CR0_CacheDisable | CR0_NoWriteThrough)), %eax |
| 127 | invd |
| 128 | movl %eax, %cr0 |
| 129 | |
| 130 | /* enable the 'no eviction' mode */ |
| 131 | movl $NoEvictMod_MSR, %ecx |
| 132 | rdmsr |
| 133 | orl $1, %eax |
| 134 | andl $~2, %eax |
| 135 | wrmsr |
| 136 | |
| 137 | /* Clear the cache memory region. This will also fill up the cache. */ |
| 138 | movl $CACHE_AS_RAM_BASE, %esi |
| 139 | movl %esi, %edi |
| 140 | movl $(CACHE_AS_RAM_SIZE >> 2), %ecx |
Arthur Heymans | dd4d895 | 2018-06-03 12:04:26 +0200 | [diff] [blame] | 141 | xorl %eax, %eax |
| 142 | rep stosl |
| 143 | |
| 144 | /* enable the 'no eviction run' state */ |
| 145 | movl $NoEvictMod_MSR, %ecx |
| 146 | rdmsr |
| 147 | orl $3, %eax |
| 148 | wrmsr |
| 149 | |
| 150 | post_code(0x26) |
| 151 | /* Enable Cache-as-RAM mode by disabling cache. */ |
| 152 | movl %cr0, %eax |
| 153 | orl $CR0_CacheDisable, %eax |
| 154 | movl %eax, %cr0 |
| 155 | |
| 156 | /* Enable cache for our code in Flash because we do XIP here */ |
| 157 | movl $MTRR_PHYS_BASE(1), %ecx |
| 158 | xorl %edx, %edx |
Arthur Heymans | c2ccc97 | 2018-06-03 12:09:52 +0200 | [diff] [blame] | 159 | movl $CACHE_ROM_BASE | MTRR_TYPE_WRPROT, %eax |
Arthur Heymans | dd4d895 | 2018-06-03 12:04:26 +0200 | [diff] [blame] | 160 | wrmsr |
| 161 | |
| 162 | movl $MTRR_PHYS_MASK(1), %ecx |
Arthur Heymans | c2ccc97 | 2018-06-03 12:09:52 +0200 | [diff] [blame] | 163 | rdmsr |
| 164 | movl $(~(CACHE_ROM_SIZE - 1) | MTRR_PHYS_MASK_VALID), %eax |
Arthur Heymans | dd4d895 | 2018-06-03 12:04:26 +0200 | [diff] [blame] | 165 | wrmsr |
| 166 | |
| 167 | post_code(0x28) |
| 168 | /* Enable cache. */ |
| 169 | movl %cr0, %eax |
| 170 | andl $(~(CR0_CacheDisable | CR0_NoWriteThrough)), %eax |
| 171 | movl %eax, %cr0 |
| 172 | |
| 173 | /* Setup the stack. */ |
| 174 | movl $(CONFIG_DCACHE_RAM_BASE + CONFIG_DCACHE_RAM_SIZE), %eax |
| 175 | movl %eax, %esp |
| 176 | |
| 177 | /* Restore the BIST result. */ |
| 178 | movl %ebp, %eax |
| 179 | movl %esp, %ebp |
| 180 | pushl %eax |
| 181 | |
| 182 | before_romstage: |
| 183 | post_code(0x29) |
| 184 | /* Call romstage.c main function. */ |
| 185 | call romstage_main |
| 186 | |
| 187 | /* Should never see this postcode */ |
| 188 | post_code(POST_DEAD_CODE) |
| 189 | |
| 190 | |
| 191 | .Lhlt: |
| 192 | hlt |
| 193 | jmp .Lhlt |
| 194 | |
Arthur Heymans | c2ccc97 | 2018-06-03 12:09:52 +0200 | [diff] [blame] | 195 | fixed_mtrr_list: |
| 196 | .word MTRR_FIX_64K_00000 |
| 197 | .word MTRR_FIX_16K_80000 |
| 198 | .word MTRR_FIX_16K_A0000 |
| 199 | .word MTRR_FIX_4K_C0000 |
| 200 | .word MTRR_FIX_4K_C8000 |
| 201 | .word MTRR_FIX_4K_D0000 |
| 202 | .word MTRR_FIX_4K_D8000 |
| 203 | .word MTRR_FIX_4K_E0000 |
| 204 | .word MTRR_FIX_4K_E8000 |
| 205 | .word MTRR_FIX_4K_F0000 |
| 206 | .word MTRR_FIX_4K_F8000 |
| 207 | fixed_mtrr_list_size = . - fixed_mtrr_list |
Arthur Heymans | dd4d895 | 2018-06-03 12:04:26 +0200 | [diff] [blame] | 208 | |
| 209 | _cache_as_ram_setup_end: |