Patrick Georgi | 11f0079 | 2020-03-04 15:10:45 +0100 | [diff] [blame] | 1 | /* SPDX-License-Identifier: GPL-2.0-only */ |
Aaron Durbin | 7f8afe0 | 2016-03-18 12:21:23 -0500 | [diff] [blame] | 2 | |
| 3 | #include <cpu/x86/mtrr.h> |
| 4 | #include <cpu/x86/cr.h> |
Arthur Heymans | a6a2f93 | 2019-11-25 19:58:36 +0100 | [diff] [blame] | 5 | #include <cpu/x86/cache.h> |
Aaron Durbin | 7f8afe0 | 2016-03-18 12:21:23 -0500 | [diff] [blame] | 6 | |
| 7 | .section ".module_parameters", "aw", @progbits |
| 8 | /* stack_top indicates the stack to pull MTRR information from. */ |
Brenton Dong | c9b3981 | 2016-10-18 13:57:54 -0700 | [diff] [blame] | 9 | .global post_car_stack_top |
| 10 | post_car_stack_top: |
Aaron Durbin | 7f8afe0 | 2016-03-18 12:21:23 -0500 | [diff] [blame] | 11 | .long 0 |
| 12 | .long 0 |
| 13 | |
Patrick Rudolph | 8daa12f | 2018-12-26 15:12:32 +0100 | [diff] [blame] | 14 | #if defined(__x86_64__) |
| 15 | .code64 |
| 16 | .macro pop_eax_edx |
| 17 | pop %rax |
| 18 | mov %rax, %rdx |
| 19 | shr $32, %rdx |
| 20 | .endm |
| 21 | .macro pop_ebx_esi |
| 22 | pop %rbx |
| 23 | mov %rbx, %rsi |
| 24 | shr $32, %rsi |
| 25 | .endm |
| 26 | #else |
| 27 | .code32 |
| 28 | .macro pop_eax_edx |
| 29 | pop %eax |
| 30 | pop %edx |
| 31 | .endm |
| 32 | .macro pop_ebx_esi |
| 33 | pop %ebx |
| 34 | pop %esi |
| 35 | .endm |
| 36 | #endif |
| 37 | |
Aaron Durbin | 7f8afe0 | 2016-03-18 12:21:23 -0500 | [diff] [blame] | 38 | .text |
| 39 | .global _start |
| 40 | _start: |
Aaron Durbin | 028e18f | 2017-06-23 11:14:58 -0500 | [diff] [blame] | 41 | /* Assume stack alignment doesn't matter here as chipset_teardown_car |
| 42 | is expected to be implemented in assembly. */ |
| 43 | |
Hannah Williams | d3c0c0c | 2018-04-27 09:09:04 -0700 | [diff] [blame] | 44 | /* Migrate GDT to this text segment */ |
Patrick Rudolph | 8daa12f | 2018-12-26 15:12:32 +0100 | [diff] [blame] | 45 | #if defined(__x86_64__) |
| 46 | call gdt_init64 |
| 47 | #else |
Hannah Williams | d3c0c0c | 2018-04-27 09:09:04 -0700 | [diff] [blame] | 48 | call gdt_init |
Patrick Rudolph | 8daa12f | 2018-12-26 15:12:32 +0100 | [diff] [blame] | 49 | #endif |
Hannah Williams | d3c0c0c | 2018-04-27 09:09:04 -0700 | [diff] [blame] | 50 | |
Arthur Heymans | 7c9a0e8 | 2019-10-23 17:02:50 +0200 | [diff] [blame] | 51 | #ifdef __x86_64__ |
| 52 | mov %rdi, _cbmem_top_ptr |
| 53 | #else |
| 54 | /* The return argument is at 0(%esp), the calling argument at 4(%esp) */ |
| 55 | movl 4(%esp), %eax |
| 56 | movl %eax, _cbmem_top_ptr |
| 57 | #endif |
Arthur Heymans | a6a2f93 | 2019-11-25 19:58:36 +0100 | [diff] [blame] | 58 | /* Make sure _cbmem_top_ptr hits dram before invd */ |
| 59 | movl $1, %eax |
| 60 | cpuid |
| 61 | btl $CPUID_FEATURE_CLFLUSH_BIT, %edx |
Arthur Heymans | 014c889 | 2020-08-29 08:21:49 +0200 | [diff] [blame] | 62 | jnc skip_clflush |
Arthur Heymans | a6a2f93 | 2019-11-25 19:58:36 +0100 | [diff] [blame] | 63 | clflush _cbmem_top_ptr |
Arthur Heymans | 7c9a0e8 | 2019-10-23 17:02:50 +0200 | [diff] [blame] | 64 | |
Arthur Heymans | a6a2f93 | 2019-11-25 19:58:36 +0100 | [diff] [blame] | 65 | skip_clflush: |
Aaron Durbin | 7f8afe0 | 2016-03-18 12:21:23 -0500 | [diff] [blame] | 66 | /* chipset_teardown_car() is expected to disable cache-as-ram. */ |
| 67 | call chipset_teardown_car |
| 68 | |
| 69 | /* Enable caching if not already enabled. */ |
Patrick Rudolph | 8daa12f | 2018-12-26 15:12:32 +0100 | [diff] [blame] | 70 | #ifdef __x86_64__ |
| 71 | mov %cr0, %rax |
| 72 | and $(~(CR0_CD | CR0_NW)), %eax |
| 73 | mov %rax, %cr0 |
| 74 | #else |
Aaron Durbin | 7f8afe0 | 2016-03-18 12:21:23 -0500 | [diff] [blame] | 75 | mov %cr0, %eax |
| 76 | and $(~(CR0_CD | CR0_NW)), %eax |
| 77 | mov %eax, %cr0 |
Patrick Rudolph | 8daa12f | 2018-12-26 15:12:32 +0100 | [diff] [blame] | 78 | #endif |
Aaron Durbin | 7f8afe0 | 2016-03-18 12:21:23 -0500 | [diff] [blame] | 79 | /* Ensure cache is clean. */ |
| 80 | invd |
| 81 | |
| 82 | /* Set up new stack. */ |
Brenton Dong | c9b3981 | 2016-10-18 13:57:54 -0700 | [diff] [blame] | 83 | mov post_car_stack_top, %esp |
Aaron Durbin | 7f8afe0 | 2016-03-18 12:21:23 -0500 | [diff] [blame] | 84 | |
| 85 | /* |
| 86 | * Honor variable MTRR information pushed on the stack with the |
| 87 | * following layout: |
| 88 | * |
| 89 | * Offset: Value |
| 90 | * ... |
| 91 | * 0x14: MTRR mask 0 63:32 |
| 92 | * 0x10: MTRR mask 0 31:0 |
| 93 | * 0x0c: MTRR base 0 63:32 |
| 94 | * 0x08: MTRR base 0 31:0 |
| 95 | * 0x04: Number of variable MTRRs to set |
| 96 | * 0x00: Number of variable MTRRs to clear |
| 97 | */ |
| 98 | |
Julius Werner | cd49cce | 2019-03-05 16:53:33 -0800 | [diff] [blame] | 99 | #if CONFIG(SOC_SETS_MSRS) |
Aaron Durbin | 028e18f | 2017-06-23 11:14:58 -0500 | [diff] [blame] | 100 | |
| 101 | mov %esp, %ebp |
| 102 | /* Need to align stack to 16 bytes at the call instruction. Therefore |
| 103 | account for the 1 push. */ |
| 104 | andl $0xfffffff0, %esp |
Patrick Rudolph | 8daa12f | 2018-12-26 15:12:32 +0100 | [diff] [blame] | 105 | #if defined(__x86_64__) |
| 106 | mov %rbp, %rdi |
| 107 | #else |
Aaron Durbin | 028e18f | 2017-06-23 11:14:58 -0500 | [diff] [blame] | 108 | sub $12, %esp |
| 109 | push %ebp |
Patrick Rudolph | 8daa12f | 2018-12-26 15:12:32 +0100 | [diff] [blame] | 110 | #endif |
| 111 | |
Lee Leahy | 5f4b4c4 | 2016-07-24 08:09:40 -0700 | [diff] [blame] | 112 | call soc_set_mtrrs |
Aaron Durbin | 028e18f | 2017-06-23 11:14:58 -0500 | [diff] [blame] | 113 | /* Ignore fixing up %esp since we're setting it a new value. */ |
Lee Leahy | 5f4b4c4 | 2016-07-24 08:09:40 -0700 | [diff] [blame] | 114 | |
| 115 | /* eax: new top_of_stack with setup_stack_and_mtrrs data removed */ |
| 116 | movl %eax, %esp |
Aaron Durbin | 028e18f | 2017-06-23 11:14:58 -0500 | [diff] [blame] | 117 | /* Align stack to 16 bytes at call instruction. */ |
| 118 | andl $0xfffffff0, %esp |
Lee Leahy | 5f4b4c4 | 2016-07-24 08:09:40 -0700 | [diff] [blame] | 119 | call soc_enable_mtrrs |
| 120 | #else /* CONFIG_SOC_SETS_MSRS */ |
Aaron Durbin | 7f8afe0 | 2016-03-18 12:21:23 -0500 | [diff] [blame] | 121 | /* Clear variable MTRRs. */ |
Patrick Rudolph | 8daa12f | 2018-12-26 15:12:32 +0100 | [diff] [blame] | 122 | pop_ebx_esi /* ebx: Number to clear, esi: Number to set */ |
Aaron Durbin | 7f8afe0 | 2016-03-18 12:21:23 -0500 | [diff] [blame] | 123 | test %ebx, %ebx |
| 124 | jz 2f |
| 125 | xor %eax, %eax |
| 126 | xor %edx, %edx |
| 127 | mov $(MTRR_PHYS_BASE(0)), %ecx |
| 128 | 1: |
| 129 | wrmsr |
| 130 | inc %ecx |
| 131 | wrmsr |
| 132 | inc %ecx |
| 133 | dec %ebx |
| 134 | jnz 1b |
| 135 | 2: |
| 136 | |
| 137 | /* Set Variable MTRRs based on stack contents. */ |
Patrick Rudolph | 8daa12f | 2018-12-26 15:12:32 +0100 | [diff] [blame] | 138 | test %esi, %esi |
Aaron Durbin | 7f8afe0 | 2016-03-18 12:21:23 -0500 | [diff] [blame] | 139 | jz 2f |
| 140 | mov $(MTRR_PHYS_BASE(0)), %ecx |
| 141 | 1: |
| 142 | /* Write MTRR base. */ |
Patrick Rudolph | 8daa12f | 2018-12-26 15:12:32 +0100 | [diff] [blame] | 143 | pop_eax_edx |
Aaron Durbin | 7f8afe0 | 2016-03-18 12:21:23 -0500 | [diff] [blame] | 144 | wrmsr |
| 145 | inc %ecx |
| 146 | /* Write MTRR mask. */ |
Patrick Rudolph | 8daa12f | 2018-12-26 15:12:32 +0100 | [diff] [blame] | 147 | pop_eax_edx |
Aaron Durbin | 7f8afe0 | 2016-03-18 12:21:23 -0500 | [diff] [blame] | 148 | wrmsr |
| 149 | inc %ecx |
| 150 | |
Patrick Rudolph | 8daa12f | 2018-12-26 15:12:32 +0100 | [diff] [blame] | 151 | dec %esi |
Aaron Durbin | 7f8afe0 | 2016-03-18 12:21:23 -0500 | [diff] [blame] | 152 | jnz 1b |
| 153 | 2: |
| 154 | |
| 155 | /* Enable MTRR. */ |
| 156 | mov $(MTRR_DEF_TYPE_MSR), %ecx |
| 157 | rdmsr |
| 158 | /* Make default type uncacheable. */ |
| 159 | and $(~(MTRR_DEF_TYPE_MASK)), %eax |
| 160 | or $(MTRR_DEF_TYPE_EN), %eax |
| 161 | wrmsr |
Lee Leahy | 5f4b4c4 | 2016-07-24 08:09:40 -0700 | [diff] [blame] | 162 | #endif /* CONFIG_SOC_SETS_MSRS */ |
Aaron Durbin | 7f8afe0 | 2016-03-18 12:21:23 -0500 | [diff] [blame] | 163 | |
Aaron Durbin | 028e18f | 2017-06-23 11:14:58 -0500 | [diff] [blame] | 164 | /* Align stack to 16 bytes at call instruction. */ |
| 165 | andl $0xfffffff0, %esp |
Aaron Durbin | 6b0cebc | 2016-09-16 16:15:14 -0500 | [diff] [blame] | 166 | /* Call into main for postcar. */ |
| 167 | call main |
Aaron Durbin | 7f8afe0 | 2016-03-18 12:21:23 -0500 | [diff] [blame] | 168 | /* Should never return. */ |
| 169 | 1: |
| 170 | jmp 1b |