Angel Pons | f23ae0b | 2020-04-02 23:48:12 +0200 | [diff] [blame] | 1 | /* SPDX-License-Identifier: GPL-2.0-only */ |
Kyösti Mälkki | 5a660ca | 2012-02-28 00:15:30 +0200 | [diff] [blame] | 2 | |
Kyösti Mälkki | 5a660ca | 2012-02-28 00:15:30 +0200 | [diff] [blame] | 3 | #include <cpu/x86/mtrr.h> |
Patrick Georgi | 05e740f | 2012-03-31 12:52:21 +0200 | [diff] [blame] | 4 | #include <cpu/x86/cache.h> |
Kyösti Mälkki | 5a660ca | 2012-02-28 00:15:30 +0200 | [diff] [blame] | 5 | #include <cpu/x86/post_code.h> |
| 6 | |
Kyösti Mälkki | 5a660ca | 2012-02-28 00:15:30 +0200 | [diff] [blame] | 7 | #define CACHE_AS_RAM_SIZE CONFIG_DCACHE_RAM_SIZE |
| 8 | #define CACHE_AS_RAM_BASE CONFIG_DCACHE_RAM_BASE |
| 9 | |
Arthur Heymans | 942ad6a | 2019-10-12 18:06:46 +0200 | [diff] [blame] | 10 | #if ((CONFIG_C_ENV_BOOTBLOCK_SIZE & (CONFIG_C_ENV_BOOTBLOCK_SIZE - 1)) != 0) |
| 11 | #error "CONFIG_C_ENV_BOOTBLOCK_SIZE must be a power of 2!" |
| 12 | #endif |
| 13 | #define XIP_ROM_SIZE CONFIG_C_ENV_BOOTBLOCK_SIZE |
Arthur Heymans | 942ad6a | 2019-10-12 18:06:46 +0200 | [diff] [blame] | 14 | |
Kyösti Mälkki | c641f7e | 2018-12-28 16:54:54 +0200 | [diff] [blame] | 15 | .global bootblock_pre_c_entry |
| 16 | |
Kyösti Mälkki | aea8eec | 2018-06-04 08:49:17 +0300 | [diff] [blame] | 17 | .code32 |
| 18 | _cache_as_ram_setup: |
| 19 | |
Kyösti Mälkki | c641f7e | 2018-12-28 16:54:54 +0200 | [diff] [blame] | 20 | bootblock_pre_c_entry: |
Kyösti Mälkki | 5a660ca | 2012-02-28 00:15:30 +0200 | [diff] [blame] | 21 | |
| 22 | cache_as_ram: |
| 23 | post_code(0x20) |
| 24 | |
Kyösti Mälkki | 5bc46d8 | 2018-06-14 06:21:53 +0300 | [diff] [blame] | 25 | /* Clear/disable fixed MTRRs */ |
| 26 | mov $fixed_mtrr_list_size, %ebx |
| 27 | xor %eax, %eax |
| 28 | xor %edx, %edx |
Kyösti Mälkki | 5a660ca | 2012-02-28 00:15:30 +0200 | [diff] [blame] | 29 | |
Kyösti Mälkki | 5bc46d8 | 2018-06-14 06:21:53 +0300 | [diff] [blame] | 30 | clear_fixed_mtrr: |
| 31 | add $-2, %ebx |
| 32 | movzwl fixed_mtrr_list(%ebx), %ecx |
| 33 | wrmsr |
| 34 | jnz clear_fixed_mtrr |
| 35 | |
Elyes HAOUAS | 02820ca | 2018-09-30 07:44:39 +0200 | [diff] [blame] | 36 | /* Figure out how many MTRRs we have, and clear them out */ |
Kyösti Mälkki | 5bc46d8 | 2018-06-14 06:21:53 +0300 | [diff] [blame] | 37 | mov $MTRR_CAP_MSR, %ecx |
| 38 | rdmsr |
| 39 | movzb %al, %ebx /* Number of variable MTRRs */ |
| 40 | mov $MTRR_PHYS_BASE(0), %ecx |
| 41 | xor %eax, %eax |
| 42 | xor %edx, %edx |
| 43 | |
| 44 | clear_var_mtrr: |
| 45 | wrmsr |
| 46 | inc %ecx |
| 47 | wrmsr |
| 48 | inc %ecx |
| 49 | dec %ebx |
| 50 | jnz clear_var_mtrr |
Kyösti Mälkki | 0078ceb | 2012-02-28 02:02:27 +0200 | [diff] [blame] | 51 | post_code(0x21) |
| 52 | |
Kyösti Mälkki | 5a660ca | 2012-02-28 00:15:30 +0200 | [diff] [blame] | 53 | /* Configure the default memory type to uncacheable. */ |
Alexandru Gagniuc | 86091f9 | 2015-09-30 20:23:09 -0700 | [diff] [blame] | 54 | movl $MTRR_DEF_TYPE_MSR, %ecx |
Kyösti Mälkki | 5a660ca | 2012-02-28 00:15:30 +0200 | [diff] [blame] | 55 | rdmsr |
| 56 | andl $(~0x00000cff), %eax |
| 57 | wrmsr |
| 58 | |
Kyösti Mälkki | 0078ceb | 2012-02-28 02:02:27 +0200 | [diff] [blame] | 59 | post_code(0x22) |
| 60 | |
Kyösti Mälkki | 54d6a28 | 2018-05-25 06:03:14 +0300 | [diff] [blame] | 61 | /* Determine CPU_ADDR_BITS and load PHYSMASK high word to %edx. */ |
Kyösti Mälkki | a860c68 | 2012-02-28 02:06:45 +0200 | [diff] [blame] | 62 | movl $1, %eax |
| 63 | cpuid |
Elyes HAOUAS | 168ef39 | 2017-06-27 22:54:42 +0200 | [diff] [blame] | 64 | andl $(1 << 6 | 1 << 17), %edx /* PAE or PSE36 */ |
Kyösti Mälkki | a860c68 | 2012-02-28 02:06:45 +0200 | [diff] [blame] | 65 | jz addrsize_set_high |
| 66 | movl $0x0f, %edx |
| 67 | |
| 68 | /* Preload high word of address mask (in %edx) for Variable |
Kyösti Mälkki | 54d6a28 | 2018-05-25 06:03:14 +0300 | [diff] [blame] | 69 | MTRRs 0 and 1. */ |
Kyösti Mälkki | a860c68 | 2012-02-28 02:06:45 +0200 | [diff] [blame] | 70 | addrsize_set_high: |
| 71 | xorl %eax, %eax |
Alexandru Gagniuc | 86091f9 | 2015-09-30 20:23:09 -0700 | [diff] [blame] | 72 | movl $MTRR_PHYS_MASK(0), %ecx |
Kyösti Mälkki | a860c68 | 2012-02-28 02:06:45 +0200 | [diff] [blame] | 73 | wrmsr |
Alexandru Gagniuc | 86091f9 | 2015-09-30 20:23:09 -0700 | [diff] [blame] | 74 | movl $MTRR_PHYS_MASK(1), %ecx |
Kyösti Mälkki | a860c68 | 2012-02-28 02:06:45 +0200 | [diff] [blame] | 75 | wrmsr |
Kyösti Mälkki | 0078ceb | 2012-02-28 02:02:27 +0200 | [diff] [blame] | 76 | |
| 77 | post_code(0x2a) |
| 78 | |
Kyösti Mälkki | 5a660ca | 2012-02-28 00:15:30 +0200 | [diff] [blame] | 79 | /* Set Cache-as-RAM base address. */ |
Alexandru Gagniuc | 86091f9 | 2015-09-30 20:23:09 -0700 | [diff] [blame] | 80 | movl $(MTRR_PHYS_BASE(0)), %ecx |
Kyösti Mälkki | 5a660ca | 2012-02-28 00:15:30 +0200 | [diff] [blame] | 81 | movl $(CACHE_AS_RAM_BASE | MTRR_TYPE_WRBACK), %eax |
| 82 | xorl %edx, %edx |
| 83 | wrmsr |
| 84 | |
| 85 | /* Set Cache-as-RAM mask. */ |
Alexandru Gagniuc | 86091f9 | 2015-09-30 20:23:09 -0700 | [diff] [blame] | 86 | movl $(MTRR_PHYS_MASK(0)), %ecx |
Kyösti Mälkki | a860c68 | 2012-02-28 02:06:45 +0200 | [diff] [blame] | 87 | rdmsr |
Alexandru Gagniuc | 86091f9 | 2015-09-30 20:23:09 -0700 | [diff] [blame] | 88 | movl $(~(CACHE_AS_RAM_SIZE - 1) | MTRR_PHYS_MASK_VALID), %eax |
Kyösti Mälkki | 5a660ca | 2012-02-28 00:15:30 +0200 | [diff] [blame] | 89 | wrmsr |
| 90 | |
Kyösti Mälkki | 8a2f167 | 2016-07-20 13:29:59 +0300 | [diff] [blame] | 91 | post_code(0x2b) |
| 92 | |
Kyösti Mälkki | 5a660ca | 2012-02-28 00:15:30 +0200 | [diff] [blame] | 93 | /* Enable MTRR. */ |
Alexandru Gagniuc | 86091f9 | 2015-09-30 20:23:09 -0700 | [diff] [blame] | 94 | movl $MTRR_DEF_TYPE_MSR, %ecx |
Kyösti Mälkki | 5a660ca | 2012-02-28 00:15:30 +0200 | [diff] [blame] | 95 | rdmsr |
Alexandru Gagniuc | 86091f9 | 2015-09-30 20:23:09 -0700 | [diff] [blame] | 96 | orl $MTRR_DEF_TYPE_EN, %eax |
Kyösti Mälkki | 5a660ca | 2012-02-28 00:15:30 +0200 | [diff] [blame] | 97 | wrmsr |
| 98 | |
Kyösti Mälkki | 0078ceb | 2012-02-28 02:02:27 +0200 | [diff] [blame] | 99 | post_code(0x2c) |
| 100 | |
Kyösti Mälkki | 5a660ca | 2012-02-28 00:15:30 +0200 | [diff] [blame] | 101 | /* Enable cache (CR0.CD = 0, CR0.NW = 0). */ |
Kyösti Mälkki | f9d1a42 | 2012-02-28 01:45:44 +0200 | [diff] [blame] | 102 | movl %cr0, %eax |
Patrick Georgi | 05e740f | 2012-03-31 12:52:21 +0200 | [diff] [blame] | 103 | andl $(~(CR0_CacheDisable | CR0_NoWriteThrough)), %eax |
Kyösti Mälkki | 5a660ca | 2012-02-28 00:15:30 +0200 | [diff] [blame] | 104 | invd |
| 105 | movl %eax, %cr0 |
| 106 | |
Kyösti Mälkki | 54d6a28 | 2018-05-25 06:03:14 +0300 | [diff] [blame] | 107 | /* Read then clear the CAR region. This will also fill up the cache. |
| 108 | * IMPORTANT: The read is mandatory. |
| 109 | */ |
| 110 | movl $CACHE_AS_RAM_BASE, %esi |
| 111 | movl %esi, %edi |
Kyösti Mälkki | f9d1a42 | 2012-02-28 01:45:44 +0200 | [diff] [blame] | 112 | cld |
Stefan Reinauer | 4a45ec4 | 2015-07-07 00:54:05 +0200 | [diff] [blame] | 113 | movl $(CACHE_AS_RAM_SIZE >> 2), %ecx |
Kyösti Mälkki | 54d6a28 | 2018-05-25 06:03:14 +0300 | [diff] [blame] | 114 | rep lodsl |
| 115 | movl $(CACHE_AS_RAM_SIZE >> 2), %ecx |
| 116 | xorl %eax, %eax |
Kyösti Mälkki | 5a660ca | 2012-02-28 00:15:30 +0200 | [diff] [blame] | 117 | rep stosl |
| 118 | |
Kyösti Mälkki | 8a2f167 | 2016-07-20 13:29:59 +0300 | [diff] [blame] | 119 | post_code(0x2d) |
Kyösti Mälkki | 5a660ca | 2012-02-28 00:15:30 +0200 | [diff] [blame] | 120 | /* Enable Cache-as-RAM mode by disabling cache. */ |
| 121 | movl %cr0, %eax |
Patrick Georgi | 05e740f | 2012-03-31 12:52:21 +0200 | [diff] [blame] | 122 | orl $CR0_CacheDisable, %eax |
Kyösti Mälkki | 5a660ca | 2012-02-28 00:15:30 +0200 | [diff] [blame] | 123 | movl %eax, %cr0 |
| 124 | |
Kyösti Mälkki | 5a660ca | 2012-02-28 00:15:30 +0200 | [diff] [blame] | 125 | /* Enable cache for our code in Flash because we do XIP here */ |
Alexandru Gagniuc | 86091f9 | 2015-09-30 20:23:09 -0700 | [diff] [blame] | 126 | movl $MTRR_PHYS_BASE(1), %ecx |
Kyösti Mälkki | 5a660ca | 2012-02-28 00:15:30 +0200 | [diff] [blame] | 127 | xorl %edx, %edx |
| 128 | /* |
| 129 | * IMPORTANT: The following calculation _must_ be done at runtime. See |
Stefan Tauner | de02878 | 2018-08-19 20:02:05 +0200 | [diff] [blame] | 130 | * https://mail.coreboot.org/pipermail/coreboot/2010-October/060922.html |
Kyösti Mälkki | 5a660ca | 2012-02-28 00:15:30 +0200 | [diff] [blame] | 131 | */ |
Kyösti Mälkki | ce9f422 | 2018-06-25 18:53:36 +0300 | [diff] [blame] | 132 | movl $_program, %eax |
Arthur Heymans | 942ad6a | 2019-10-12 18:06:46 +0200 | [diff] [blame] | 133 | andl $(~(XIP_ROM_SIZE - 1)), %eax |
Kyösti Mälkki | dc4820b | 2016-07-21 19:51:01 +0300 | [diff] [blame] | 134 | orl $MTRR_TYPE_WRPROT, %eax |
Kyösti Mälkki | 5a660ca | 2012-02-28 00:15:30 +0200 | [diff] [blame] | 135 | wrmsr |
| 136 | |
Alexandru Gagniuc | 86091f9 | 2015-09-30 20:23:09 -0700 | [diff] [blame] | 137 | movl $MTRR_PHYS_MASK(1), %ecx |
Kyösti Mälkki | a860c68 | 2012-02-28 02:06:45 +0200 | [diff] [blame] | 138 | rdmsr |
Arthur Heymans | 942ad6a | 2019-10-12 18:06:46 +0200 | [diff] [blame] | 139 | movl $(~(XIP_ROM_SIZE - 1) | MTRR_PHYS_MASK_VALID), %eax |
Kyösti Mälkki | 5a660ca | 2012-02-28 00:15:30 +0200 | [diff] [blame] | 140 | wrmsr |
Kyösti Mälkki | 5a660ca | 2012-02-28 00:15:30 +0200 | [diff] [blame] | 141 | |
Kyösti Mälkki | 8a2f167 | 2016-07-20 13:29:59 +0300 | [diff] [blame] | 142 | post_code(0x2e) |
Kyösti Mälkki | 5a660ca | 2012-02-28 00:15:30 +0200 | [diff] [blame] | 143 | /* Enable cache. */ |
| 144 | movl %cr0, %eax |
Patrick Georgi | 05e740f | 2012-03-31 12:52:21 +0200 | [diff] [blame] | 145 | andl $(~(CR0_CacheDisable | CR0_NoWriteThrough)), %eax |
Kyösti Mälkki | 5a660ca | 2012-02-28 00:15:30 +0200 | [diff] [blame] | 146 | movl %eax, %cr0 |
| 147 | |
Kyösti Mälkki | 39915bc | 2016-11-08 12:13:15 +0200 | [diff] [blame] | 148 | /* Setup the stack. */ |
Arthur Heymans | df9cdcf | 2019-11-09 06:50:20 +0100 | [diff] [blame] | 149 | mov $_ecar_stack, %esp |
Kyösti Mälkki | 5a660ca | 2012-02-28 00:15:30 +0200 | [diff] [blame] | 150 | |
Kyösti Mälkki | c641f7e | 2018-12-28 16:54:54 +0200 | [diff] [blame] | 151 | /* Need to align stack to 16 bytes at call instruction. Account for |
| 152 | the pushes below. */ |
| 153 | andl $0xfffffff0, %esp |
| 154 | subl $4, %esp |
Kyösti Mälkki | 5a660ca | 2012-02-28 00:15:30 +0200 | [diff] [blame] | 155 | |
Kyösti Mälkki | c641f7e | 2018-12-28 16:54:54 +0200 | [diff] [blame] | 156 | /* push TSC and BIST to stack */ |
| 157 | movd %mm0, %eax |
Elyes HAOUAS | 87930b3 | 2019-01-16 12:41:57 +0100 | [diff] [blame] | 158 | pushl %eax /* BIST */ |
Kyösti Mälkki | c641f7e | 2018-12-28 16:54:54 +0200 | [diff] [blame] | 159 | movd %mm2, %eax |
| 160 | pushl %eax /* tsc[63:32] */ |
| 161 | movd %mm1, %eax |
Elyes HAOUAS | 87930b3 | 2019-01-16 12:41:57 +0100 | [diff] [blame] | 162 | pushl %eax /* tsc[31:0] */ |
Kyösti Mälkki | c641f7e | 2018-12-28 16:54:54 +0200 | [diff] [blame] | 163 | |
| 164 | before_c_entry: |
| 165 | post_code(0x29) |
| 166 | call bootblock_c_entry_bist |
Kyösti Mälkki | 5a660ca | 2012-02-28 00:15:30 +0200 | [diff] [blame] | 167 | |
Kyösti Mälkki | aea8eec | 2018-06-04 08:49:17 +0300 | [diff] [blame] | 168 | /* Should never see this postcode */ |
| 169 | post_code(POST_DEAD_CODE) |
Kyösti Mälkki | 5a660ca | 2012-02-28 00:15:30 +0200 | [diff] [blame] | 170 | |
| 171 | .Lhlt: |
Kyösti Mälkki | 5a660ca | 2012-02-28 00:15:30 +0200 | [diff] [blame] | 172 | hlt |
| 173 | jmp .Lhlt |
| 174 | |
Kyösti Mälkki | 5bc46d8 | 2018-06-14 06:21:53 +0300 | [diff] [blame] | 175 | fixed_mtrr_list: |
| 176 | .word MTRR_FIX_64K_00000 |
| 177 | .word MTRR_FIX_16K_80000 |
| 178 | .word MTRR_FIX_16K_A0000 |
| 179 | .word MTRR_FIX_4K_C0000 |
| 180 | .word MTRR_FIX_4K_C8000 |
| 181 | .word MTRR_FIX_4K_D0000 |
| 182 | .word MTRR_FIX_4K_D8000 |
| 183 | .word MTRR_FIX_4K_E0000 |
| 184 | .word MTRR_FIX_4K_E8000 |
| 185 | .word MTRR_FIX_4K_F0000 |
| 186 | .word MTRR_FIX_4K_F8000 |
| 187 | fixed_mtrr_list_size = . - fixed_mtrr_list |
Kyösti Mälkki | aea8eec | 2018-06-04 08:49:17 +0300 | [diff] [blame] | 188 | |
| 189 | _cache_as_ram_setup_end: |