Stefan Reinauer | debb11f | 2008-10-29 04:46:52 +0000 | [diff] [blame] | 1 | /* |
| 2 | * This file is part of the coreboot project. |
| 3 | * |
| 4 | * Copyright (C) 2008 coresystems GmbH |
| 5 | * |
| 6 | * This program is free software; you can redistribute it and/or |
| 7 | * modify it under the terms of the GNU General Public License as |
| 8 | * published by the Free Software Foundation; version 2 of |
| 9 | * the License. |
| 10 | * |
| 11 | * This program is distributed in the hope that it will be useful, |
| 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 | * GNU General Public License for more details. |
| 15 | * |
| 16 | * You should have received a copy of the GNU General Public License |
| 17 | * along with this program; if not, write to the Free Software |
| 18 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, |
| 19 | * MA 02110-1301 USA |
| 20 | */ |
| 21 | |
| 22 | /* NOTE: This handler assumes the SMM window goes from 0xa0000 |
| 23 | * to 0xaffff. In fact, at least on Intel Core CPUs (i945 chipset) |
| 24 | * the SMM window is 128K big, covering 0xa0000 to 0xbffff. |
| 25 | * So there is a lot of potential for growth in here. Let's stick |
| 26 | * to 64k if we can though. |
| 27 | */ |
| 28 | |
| 29 | /* |
| 30 | * +--------------------------------+ 0xaffff |
| 31 | * | Save State Map Node 0 | |
| 32 | * | Save State Map Node 1 | |
| 33 | * | Save State Map Node 2 | |
| 34 | * | Save State Map Node 3 | |
| 35 | * | ... | |
| 36 | * +--------------------------------+ 0xaf000 |
| 37 | * | | |
| 38 | * | | |
| 39 | * | | |
| 40 | * +--------------------------------+ 0xa8400 |
Stefan Reinauer | 14e2277 | 2010-04-27 06:56:47 +0000 | [diff] [blame] | 41 | * | SMM Entry Node 0 (+ stack) | |
Stefan Reinauer | debb11f | 2008-10-29 04:46:52 +0000 | [diff] [blame] | 42 | * +--------------------------------+ 0xa8000 |
Stefan Reinauer | 14e2277 | 2010-04-27 06:56:47 +0000 | [diff] [blame] | 43 | * | SMM Entry Node 1 (+ stack) | |
| 44 | * | SMM Entry Node 2 (+ stack) | |
| 45 | * | SMM Entry Node 3 (+ stack) | |
Stefan Reinauer | debb11f | 2008-10-29 04:46:52 +0000 | [diff] [blame] | 46 | * | ... | |
| 47 | * +--------------------------------+ 0xa7400 |
| 48 | * | | |
| 49 | * | SMM Handler | |
| 50 | * | | |
| 51 | * +--------------------------------+ 0xa0000 |
| 52 | * |
| 53 | */ |
| 54 | |
Stefan Reinauer | debb11f | 2008-10-29 04:46:52 +0000 | [diff] [blame] | 55 | #define LAPIC_ID 0xfee00020 |
| 56 | |
| 57 | /* SMM_HANDLER_OFFSET is the 16bit offset within the ASEG |
| 58 | * at which smm_handler_start lives. At the moment the handler |
Stefan Reinauer | 14e2277 | 2010-04-27 06:56:47 +0000 | [diff] [blame] | 59 | * lives right at 0xa0000, so the offset is 0. |
Stefan Reinauer | debb11f | 2008-10-29 04:46:52 +0000 | [diff] [blame] | 60 | */ |
| 61 | |
| 62 | #define SMM_HANDLER_OFFSET 0x0000 |
| 63 | |
| 64 | /* initially SMM is some sort of real mode. Let gcc know |
| 65 | * how to treat the SMM handler stub |
| 66 | */ |
| 67 | |
| 68 | .section ".handler", "a", @progbits |
| 69 | |
| 70 | .code16 |
| 71 | |
| 72 | /** |
| 73 | * SMM code to enable protected mode and jump to the |
| 74 | * C-written function void smi_handler(u32 smm_revision) |
| 75 | * |
| 76 | * All the bad magic is not all that bad after all. |
| 77 | */ |
Aaron Durbin | e73dae4 | 2015-03-29 22:16:55 -0500 | [diff] [blame^] | 78 | .global smm_handler_start |
Stefan Reinauer | debb11f | 2008-10-29 04:46:52 +0000 | [diff] [blame] | 79 | smm_handler_start: |
| 80 | movw $(smm_gdtptr16 - smm_handler_start + SMM_HANDLER_OFFSET), %bx |
| 81 | data32 lgdt %cs:(%bx) |
| 82 | |
| 83 | movl %cr0, %eax |
| 84 | andl $0x7FFAFFD1, %eax /* PG,AM,WP,NE,TS,EM,MP = 0 */ |
| 85 | orl $0x60000001, %eax /* CD, NW, PE = 1 */ |
| 86 | movl %eax, %cr0 |
| 87 | |
| 88 | /* Enable protected mode */ |
| 89 | data32 ljmp $0x08, $1f |
| 90 | |
| 91 | .code32 |
| 92 | 1: |
Stefan Reinauer | 3128685 | 2011-10-15 11:23:04 -0700 | [diff] [blame] | 93 | /* flush the cache after disabling it */ |
| 94 | wbinvd |
| 95 | |
Stefan Reinauer | debb11f | 2008-10-29 04:46:52 +0000 | [diff] [blame] | 96 | /* Use flat data segment */ |
| 97 | movw $0x10, %ax |
| 98 | movw %ax, %ds |
| 99 | movw %ax, %es |
| 100 | movw %ax, %ss |
| 101 | movw %ax, %fs |
| 102 | movw %ax, %gs |
| 103 | |
| 104 | /* Get this CPU's LAPIC ID */ |
| 105 | movl $LAPIC_ID, %esi |
| 106 | movl (%esi), %ecx |
| 107 | shr $24, %ecx |
Stefan Reinauer | 14e2277 | 2010-04-27 06:56:47 +0000 | [diff] [blame] | 108 | |
Alexandru Gagniuc | 53072d8 | 2014-04-12 21:57:18 -0500 | [diff] [blame] | 109 | /* This is an ugly hack, and we should find a way to read the CPU index |
| 110 | * without relying on the LAPIC ID. |
| 111 | */ |
Edward O'Callaghan | eaab630 | 2014-11-21 03:31:02 +1100 | [diff] [blame] | 112 | #if IS_ENABLED(CONFIG_CPU_AMD_AGESA_FAMILY15_TN) || IS_ENABLED(CONFIG_CPU_AMD_AGESA_FAMILY15_RL) |
Alexandru Gagniuc | 53072d8 | 2014-04-12 21:57:18 -0500 | [diff] [blame] | 113 | /* LAPIC IDs start from 0x10; map that to the proper core index */ |
| 114 | subl $0x10, %ecx |
| 115 | #endif |
| 116 | |
Stefan Reinauer | debb11f | 2008-10-29 04:46:52 +0000 | [diff] [blame] | 117 | /* calculate stack offset by multiplying the APIC ID |
| 118 | * by 1024 (0x400), and save that offset in ebp. |
| 119 | */ |
| 120 | shl $10, %ecx |
| 121 | movl %ecx, %ebp |
| 122 | |
Stefan Reinauer | 14e2277 | 2010-04-27 06:56:47 +0000 | [diff] [blame] | 123 | /* We put the stack for each core right above |
| 124 | * its SMM entry point. Core 0 starts at 0xa8000, |
Stefan Reinauer | debb11f | 2008-10-29 04:46:52 +0000 | [diff] [blame] | 125 | * we spare 0x10 bytes for the jump to be sure. |
| 126 | */ |
| 127 | movl $0xa8010, %eax |
| 128 | subl %ecx, %eax /* subtract offset, see above */ |
| 129 | movl %eax, %ebx /* Save bottom of stack in ebx */ |
| 130 | |
| 131 | #define SMM_STACK_SIZE (0x400 - 0x10) |
| 132 | /* clear stack */ |
| 133 | cld |
| 134 | movl %eax, %edi |
| 135 | movl $(SMM_STACK_SIZE >> 2), %ecx |
| 136 | xorl %eax, %eax |
| 137 | rep stosl |
| 138 | |
| 139 | /* set new stack */ |
| 140 | addl $SMM_STACK_SIZE, %ebx |
| 141 | movl %ebx, %esp |
| 142 | |
| 143 | /* Get SMM revision */ |
| 144 | movl $0xa8000 + 0x7efc, %ebx /* core 0 address */ |
| 145 | subl %ebp, %ebx /* subtract core X offset */ |
| 146 | movl (%ebx), %eax |
| 147 | pushl %eax |
| 148 | |
| 149 | /* Call 32bit C handler */ |
| 150 | call smi_handler |
| 151 | |
| 152 | /* To return, just do rsm. It will "clean up" protected mode */ |
| 153 | rsm |
| 154 | |
| 155 | .code16 |
| 156 | |
| 157 | .align 4, 0xff |
| 158 | |
| 159 | smm_gdtptr16: |
| 160 | .word smm_gdt_end - smm_gdt - 1 |
| 161 | .long smm_gdt - smm_handler_start + 0xa0000 + SMM_HANDLER_OFFSET |
| 162 | |
| 163 | .code32 |
| 164 | |
| 165 | smm_gdt: |
| 166 | /* The first GDT entry can not be used. Keep it zero */ |
| 167 | .long 0x00000000, 0x00000000 |
| 168 | |
| 169 | /* gdt selector 0x08, flat code segment */ |
Stefan Reinauer | 14e2277 | 2010-04-27 06:56:47 +0000 | [diff] [blame] | 170 | .word 0xffff, 0x0000 |
| 171 | .byte 0x00, 0x9b, 0xcf, 0x00 /* G=1 and 0x0f, 4GB limit */ |
Stefan Reinauer | debb11f | 2008-10-29 04:46:52 +0000 | [diff] [blame] | 172 | |
| 173 | /* gdt selector 0x10, flat data segment */ |
Stefan Reinauer | 14e2277 | 2010-04-27 06:56:47 +0000 | [diff] [blame] | 174 | .word 0xffff, 0x0000 |
Stefan Reinauer | debb11f | 2008-10-29 04:46:52 +0000 | [diff] [blame] | 175 | .byte 0x00, 0x93, 0xcf, 0x00 |
| 176 | |
| 177 | smm_gdt_end: |
| 178 | |
| 179 | |
| 180 | .section ".jumptable", "a", @progbits |
| 181 | |
| 182 | /* This is the SMM jump table. All cores use the same SMM handler |
Stefan Reinauer | 14e2277 | 2010-04-27 06:56:47 +0000 | [diff] [blame] | 183 | * for simplicity. But SMM Entry needs to be different due to the |
Stefan Reinauer | debb11f | 2008-10-29 04:46:52 +0000 | [diff] [blame] | 184 | * save state area. The jump table makes sure all CPUs jump into the |
| 185 | * real handler on SMM entry. |
| 186 | */ |
| 187 | |
| 188 | /* This code currently supports up to 4 CPU cores. If more than 4 CPU cores |
| 189 | * shall be used, below table has to be updated, as well as smm.ld |
| 190 | */ |
| 191 | |
| 192 | /* GNU AS/LD will always generate code that assumes CS is 0xa000. In reality |
| 193 | * CS will be set to SMM_BASE[19:4] though. Knowing that the smm handler is the |
| 194 | * first thing in the ASEG, we do a far jump here, to set CS to 0xa000. |
| 195 | */ |
| 196 | |
| 197 | .code16 |
| 198 | jumptable: |
| 199 | /* core 3 */ |
Stefan Reinauer | 14e2277 | 2010-04-27 06:56:47 +0000 | [diff] [blame] | 200 | ljmp $0xa000, $SMM_HANDLER_OFFSET |
Stefan Reinauer | debb11f | 2008-10-29 04:46:52 +0000 | [diff] [blame] | 201 | .align 1024, 0x00 |
| 202 | /* core 2 */ |
Stefan Reinauer | 14e2277 | 2010-04-27 06:56:47 +0000 | [diff] [blame] | 203 | ljmp $0xa000, $SMM_HANDLER_OFFSET |
Stefan Reinauer | debb11f | 2008-10-29 04:46:52 +0000 | [diff] [blame] | 204 | .align 1024, 0x00 |
| 205 | /* core 1 */ |
Stefan Reinauer | 14e2277 | 2010-04-27 06:56:47 +0000 | [diff] [blame] | 206 | ljmp $0xa000, $SMM_HANDLER_OFFSET |
Stefan Reinauer | debb11f | 2008-10-29 04:46:52 +0000 | [diff] [blame] | 207 | .align 1024, 0x00 |
| 208 | /* core 0 */ |
| 209 | ljmp $0xa000, $SMM_HANDLER_OFFSET |
| 210 | .align 1024, 0x00 |
| 211 | |