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