flashrom: Fix ICH9 locking register address and add important debug output.
[coreboot.git] / src / southbridge / intel / i82801gx / smmhandler.S
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
41  * | SMM Entry Node 0 (+ stack)     | 
42  * +--------------------------------+ 0xa8000
43  * | SMM Entry Node 1 (+ stack)     | 
44  * | SMM Entry Node 2 (+ stack)     | 
45  * | SMM Entry Node 3 (+ stack)     | 
46  * | ...                            |
47  * +--------------------------------+ 0xa7400
48  * |                                |
49  * | SMM Handler                    |
50  * |                                |
51  * +--------------------------------+ 0xa0000
52  *
53  */
54
55 #include <arch/asm.h>
56
57 #define LAPIC_ID 0xfee00020
58
59 /* SMM_HANDLER_OFFSET is the 16bit offset within the ASEG
60  * at which smm_handler_start lives. At the moment the handler
61  * lives right at 0xa0000, so the offset is 0. 
62  */
63
64 #define SMM_HANDLER_OFFSET 0x0000
65
66 /* initially SMM is some sort of real mode. Let gcc know
67  * how to treat the SMM handler stub
68  */
69
70 .section ".handler", "a", @progbits
71
72 .code16
73
74 /**
75  * SMM code to enable protected mode and jump to the
76  * C-written function void smi_handler(u32 smm_revision)
77  *
78  * All the bad magic is not all that bad after all.
79  */
80 smm_handler_start:
81         movw    $(smm_gdtptr16 - smm_handler_start + SMM_HANDLER_OFFSET), %bx
82         data32  lgdt %cs:(%bx)
83
84         movl    %cr0, %eax
85         andl    $0x7FFAFFD1, %eax /* PG,AM,WP,NE,TS,EM,MP = 0 */
86         orl     $0x60000001, %eax /* CD, NW, PE = 1 */
87         movl    %eax, %cr0
88
89         /* Enable protected mode */
90         data32  ljmp    $0x08, $1f
91
92 .code32
93 1:
94         /* Use flat data segment */
95         movw    $0x10, %ax
96         movw    %ax, %ds
97         movw    %ax, %es
98         movw    %ax, %ss
99         movw    %ax, %fs
100         movw    %ax, %gs
101
102         /* Get this CPU's LAPIC ID */
103         movl $LAPIC_ID, %esi
104         movl (%esi), %ecx
105         shr  $24, %ecx
106         
107         /* calculate stack offset by multiplying the APIC ID
108          * by 1024 (0x400), and save that offset in ebp.
109          */
110         shl $10, %ecx
111         movl %ecx, %ebp
112
113         /* We put the stack for each core right above 
114          * its SMM entry point. Core 0 starts at 0xa8000, 
115          * we spare 0x10 bytes for the jump to be sure.
116          */
117         movl $0xa8010, %eax
118         subl %ecx, %eax         /* subtract offset, see above */
119         movl %eax, %ebx         /* Save bottom of stack in ebx */
120
121 #define SMM_STACK_SIZE  (0x400 - 0x10)
122         /* clear stack */
123         cld
124         movl    %eax, %edi
125         movl    $(SMM_STACK_SIZE >> 2), %ecx
126         xorl    %eax, %eax
127         rep     stosl
128
129         /* set new stack */
130         addl    $SMM_STACK_SIZE, %ebx
131         movl    %ebx, %esp
132
133         /* Get SMM revision */
134         movl $0xa8000 + 0x7efc, %ebx    /* core 0 address */
135         subl %ebp, %ebx                 /* subtract core X offset */
136         movl (%ebx), %eax
137         pushl %eax
138
139         /* Call 32bit C handler */
140         call smi_handler
141
142         /* To return, just do rsm. It will "clean up" protected mode */
143         rsm
144
145 .code16
146
147 .align  4, 0xff
148
149 smm_gdtptr16:
150         .word   smm_gdt_end - smm_gdt - 1
151         .long   smm_gdt - smm_handler_start + 0xa0000 + SMM_HANDLER_OFFSET
152
153 .code32
154
155 smm_gdt:
156         /* The first GDT entry can not be used. Keep it zero */
157         .long   0x00000000, 0x00000000
158
159         /* gdt selector 0x08, flat code segment */
160         .word   0xffff, 0x0000          
161         .byte   0x00, 0x9b, 0xcf, 0x00 /* G=1 and 0x0f, 4GB limit */    
162
163         /* gdt selector 0x10, flat data segment */
164         .word   0xffff, 0x0000          
165         .byte   0x00, 0x93, 0xcf, 0x00
166
167 smm_gdt_end:
168
169
170 .section ".jumptable", "a", @progbits
171
172 /* This is the SMM jump table. All cores use the same SMM handler
173  * for simplicity. But SMM Entry needs to be different due to the 
174  * save state area. The jump table makes sure all CPUs jump into the
175  * real handler on SMM entry.
176  */
177
178 /* This code currently supports up to 4 CPU cores. If more than 4 CPU cores
179  * shall be used, below table has to be updated, as well as smm.ld
180  */
181
182 /* GNU AS/LD will always generate code that assumes CS is 0xa000. In reality
183  * CS will be set to SMM_BASE[19:4] though. Knowing that the smm handler is the
184  * first thing in the ASEG, we do a far jump here, to set CS to 0xa000.
185  */
186
187 .code16
188 jumptable:
189         /* core 3 */
190         ljmp $0xa000, $SMM_HANDLER_OFFSET 
191 .align 1024, 0x00
192         /* core 2 */
193         ljmp $0xa000, $SMM_HANDLER_OFFSET 
194 .align 1024, 0x00
195         /* core 1 */
196         ljmp $0xa000, $SMM_HANDLER_OFFSET 
197 .align 1024, 0x00
198         /* core 0 */
199         ljmp $0xa000, $SMM_HANDLER_OFFSET
200 .align 1024, 0x00
201