1ea9e1926e470f34900abb4cdbdd38f118192afe
[coreboot.git] / src / cpu / x86 / smm / smihandler.c
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 #include <arch/io.h>
23 #include <arch/romcc_io.h>
24 #include <console/console.h>
25 #include <cpu/x86/cache.h>
26 #include <cpu/x86/smm.h>
27
28 void southbridge_smi_set_eos(void);
29
30 #define DEBUG_SMI
31
32 typedef enum { SMI_LOCKED, SMI_UNLOCKED } smi_semaphore;
33
34 /* SMI multiprocessing semaphore */
35 static volatile smi_semaphore smi_handler_status = SMI_UNLOCKED;
36
37 static int smi_obtain_lock(void)
38 {
39         u8 ret = SMI_LOCKED;
40
41         asm volatile (
42                 "movb %2, %%al\n"
43                 "xchgb %%al, %1\n"
44                 "movb %%al, %0\n"
45                 : "=g" (ret), "=m" (smi_handler_status)
46                 : "g" (SMI_LOCKED)
47                 : "eax"
48         );
49
50         return (ret == SMI_UNLOCKED);
51 }
52
53 static void smi_release_lock(void)
54 {
55         asm volatile (
56                 "movb %1, %%al\n"
57                 "xchgb %%al, %0\n"
58                 : "=m" (smi_handler_status)
59                 : "g" (SMI_UNLOCKED)
60                 : "eax"
61         );
62 }
63
64 #define LAPIC_ID 0xfee00020
65 static inline __attribute__((always_inline)) unsigned long nodeid(void)
66 {
67         return (*((volatile unsigned long *)(LAPIC_ID)) >> 24);
68 }
69
70 /* ********************* smi_util ************************* */
71
72 /* Data */
73 #define UART_RBR 0x00
74 #define UART_TBR 0x00
75
76 /* Control */
77 #define UART_IER 0x01
78 #define UART_IIR 0x02
79 #define UART_FCR 0x02
80 #define UART_LCR 0x03
81 #define UART_MCR 0x04
82 #define UART_DLL 0x00
83 #define UART_DLM 0x01
84
85 /* Status */
86 #define UART_LSR 0x05
87 #define UART_MSR 0x06
88 #define UART_SCR 0x07
89
90 static int uart_can_tx_byte(void)
91 {
92         return inb(TTYS0_BASE + UART_LSR) & 0x20;
93 }
94
95 static void uart_wait_to_tx_byte(void)
96 {
97         while(!uart_can_tx_byte()) 
98         ;
99 }
100
101 static void uart_wait_until_sent(void)
102 {
103         while(!(inb(TTYS0_BASE + UART_LSR) & 0x40))
104         ; 
105 }
106
107 static void uart_tx_byte(unsigned char data)
108 {
109         uart_wait_to_tx_byte();
110         outb(data, TTYS0_BASE + UART_TBR);
111         /* Make certain the data clears the fifos */
112         uart_wait_until_sent();
113 }
114
115 void console_tx_flush(void)
116 {
117         uart_wait_to_tx_byte();
118 }
119
120 void console_tx_byte(unsigned char byte)
121 {
122         if (byte == '\n')
123                 uart_tx_byte('\r');
124         uart_tx_byte(byte);
125 }
126
127 /* ********************* smi_util ************************* */
128
129
130 void io_trap_handler(int smif)
131 {
132         /* If a handler function handled a given IO trap, it
133          * shall return a non-zero value
134          */
135         printk_debug("SMI function trap 0x%x: ", smif);
136
137         if (southbridge_io_trap_handler(smif))
138                 return;
139
140         if (mainboard_io_trap_handler(smif))
141                 return;
142
143         printk_debug("Unknown function\n");
144 }
145
146 /**
147  * @brief Set the EOS bit
148  */
149 static void smi_set_eos(void)
150 {
151         southbridge_smi_set_eos();
152 }
153
154 /**
155  * @brief Interrupt handler for SMI#
156  *
157  * @param smm_revision revision of the smm state save map
158  */
159
160 void smi_handler(u32 smm_revision)
161 {
162         unsigned int node;
163         smm_state_save_area_t state_save;
164
165         /* Are we ok to execute the handler? */
166         if (!smi_obtain_lock())
167                 return;
168
169         node=nodeid();
170
171 #ifdef DEBUG_SMI
172         console_loglevel = DEFAULT_CONSOLE_LOGLEVEL;
173 #else
174         console_loglevel = 1;
175 #endif
176
177         printk_debug("\nSMI# #%d\n", node);
178
179         switch (smm_revision) {
180         case 0x00030007:
181                 state_save.type = LEGACY;
182                 state_save.legacy_state_save = (legacy_smm_state_save_area_t *)
183                         (0xa8000 + 0x7e00 - (node * 0x400));
184                 break;
185         case 0x00030100:
186                 state_save.type = EM64T;
187                 state_save.em64t_state_save = (em64t_smm_state_save_area_t *)
188                         (0xa8000 + 0x7d00 - (node * 0x400));
189                 break;
190         case 0x00030064:
191                 state_save.type = AMD64;
192                 state_save.amd64_state_save = (amd64_smm_state_save_area_t *)
193                         (0xa8000 + 0x7e00 - (node * 0x400));
194                 break;
195         default:
196                 printk_debug("smm_revision: 0x%08x\n", smm_revision);
197                 printk_debug("SMI# not supported on your CPU\n");
198                 /* Don't release lock, so no further SMI will happen,
199                  * if we don't handle it anyways.
200                  */
201                 return;
202         }
203
204         southbridge_smi_handler(node, &state_save);
205
206         smi_release_lock();
207
208         /* De-assert SMI# signal to allow another SMI */
209         smi_set_eos();
210 }