blob: 7977ac777d9ef31680c649534ccfe682f778cf3d [file] [log] [blame]
Kevin O'Connorf7ba6d72008-07-04 05:05:54 -04001// System Management Mode support (on emulators)
2//
3// Copyright (C) 2008 Kevin O'Connor <kevin@koconnor.net>
4// Copyright (C) 2006 Fabrice Bellard
5//
Kevin O'Connorb1b7c2a2009-01-15 20:52:58 -05006// This file may be distributed under the terms of the GNU LGPLv3 license.
Kevin O'Connorf7ba6d72008-07-04 05:05:54 -04007
Kevin O'Connorbe19cdc2008-11-09 15:33:47 -05008#include "pci.h" // pci_config_writel
Kevin O'Connorf7ba6d72008-07-04 05:05:54 -04009#include "util.h" // wbinvd
Kevin O'Connor9521e262008-07-04 13:04:29 -040010#include "config.h" // CONFIG_*
11#include "ioport.h" // outb
Kevin O'Connor2ed2f582008-11-08 15:53:36 -050012#include "pci_ids.h" // PCI_VENDOR_ID_INTEL
Ian Campbell12c991e2012-06-28 11:08:33 +010013#include "xen.h" // usingXen
Isaku Yamahata72a590e2012-11-28 10:17:33 +010014#include "dev-q35.h"
Kevin O'Connorf7ba6d72008-07-04 05:05:54 -040015
Kevin O'Connor52a300f2009-12-26 23:32:57 -050016ASM32FLAT(
Kevin O'Connorf7ba6d72008-07-04 05:05:54 -040017 ".global smm_relocation_start\n"
18 ".global smm_relocation_end\n"
19 ".global smm_code_start\n"
20 ".global smm_code_end\n"
21 " .code16\n"
22
23 /* code to relocate SMBASE to 0xa0000 */
24 "smm_relocation_start:\n"
Kevin O'Connore682cbc2008-12-06 23:11:56 -050025 " mov $" __stringify(BUILD_SMM_INIT_ADDR) " + 0x7efc, %ebx\n"
Kevin O'Connorf7ba6d72008-07-04 05:05:54 -040026 " addr32 mov (%ebx), %al\n" /* revision ID to see if x86_64 or x86 */
27 " cmp $0x64, %al\n"
28 " je 1f\n"
Kevin O'Connore682cbc2008-12-06 23:11:56 -050029 " mov $" __stringify(BUILD_SMM_INIT_ADDR) " + 0x7ef8, %ebx\n"
Kevin O'Connorf7ba6d72008-07-04 05:05:54 -040030 " jmp 2f\n"
31 "1:\n"
Kevin O'Connore682cbc2008-12-06 23:11:56 -050032 " mov $" __stringify(BUILD_SMM_INIT_ADDR) " + 0x7f00, %ebx\n"
Kevin O'Connorf7ba6d72008-07-04 05:05:54 -040033 "2:\n"
Kevin O'Connore682cbc2008-12-06 23:11:56 -050034 " movl $" __stringify(BUILD_SMM_ADDR) " - 0x8000, %eax\n"
Kevin O'Connorf7ba6d72008-07-04 05:05:54 -040035 " addr32 movl %eax, (%ebx)\n"
36 /* indicate to the BIOS that the SMM code was executed */
37 " mov $0x00, %al\n"
Kevin O'Connore682cbc2008-12-06 23:11:56 -050038 " movw $" __stringify(PORT_SMI_STATUS) ", %dx\n"
Kevin O'Connorf7ba6d72008-07-04 05:05:54 -040039 " outb %al, %dx\n"
40 " rsm\n"
41 "smm_relocation_end:\n"
42
43 /* minimal SMM code to enable or disable ACPI */
44 "smm_code_start:\n"
Kevin O'Connore682cbc2008-12-06 23:11:56 -050045 " movw $" __stringify(PORT_SMI_CMD) ", %dx\n"
Kevin O'Connorf7ba6d72008-07-04 05:05:54 -040046 " inb %dx, %al\n"
47 " cmp $0xf0, %al\n"
48 " jne 1f\n"
49
50 /* ACPI disable */
Kevin O'Connore682cbc2008-12-06 23:11:56 -050051 " mov $" __stringify(PORT_ACPI_PM_BASE) " + 0x04, %dx\n" /* PMCNTRL */
Kevin O'Connorf7ba6d72008-07-04 05:05:54 -040052 " inw %dx, %ax\n"
53 " andw $~1, %ax\n"
54 " outw %ax, %dx\n"
55
56 " jmp 2f\n"
57
58 "1:\n"
59 " cmp $0xf1, %al\n"
60 " jne 2f\n"
61
62 /* ACPI enable */
Kevin O'Connore682cbc2008-12-06 23:11:56 -050063 " mov $" __stringify(PORT_ACPI_PM_BASE) " + 0x04, %dx\n" /* PMCNTRL */
Kevin O'Connorf7ba6d72008-07-04 05:05:54 -040064 " inw %dx, %ax\n"
65 " orw $1, %ax\n"
66 " outw %ax, %dx\n"
67
68 "2:\n"
69 " rsm\n"
70 "smm_code_end:\n"
71 " .code32\n"
72 );
Kevin O'Connorf7ba6d72008-07-04 05:05:54 -040073
74extern u8 smm_relocation_start, smm_relocation_end;
75extern u8 smm_code_start, smm_code_end;
76
Kevin O'Connor6e4583c2011-06-19 10:09:26 -040077static void
Isaku Yamahataaec19c92010-07-20 16:50:46 +090078smm_save_and_copy(void)
Kevin O'Connorf7ba6d72008-07-04 05:05:54 -040079{
Kevin O'Connore682cbc2008-12-06 23:11:56 -050080 /* save original memory content */
81 memcpy((void *)BUILD_SMM_ADDR, (void *)BUILD_SMM_INIT_ADDR, BUILD_SMM_SIZE);
82
Kevin O'Connorf7ba6d72008-07-04 05:05:54 -040083 /* copy the SMM relocation code */
Kevin O'Connore682cbc2008-12-06 23:11:56 -050084 memcpy((void *)BUILD_SMM_INIT_ADDR, &smm_relocation_start,
Kevin O'Connorf7ba6d72008-07-04 05:05:54 -040085 &smm_relocation_end - &smm_relocation_start);
Isaku Yamahataaec19c92010-07-20 16:50:46 +090086}
Kevin O'Connorf7ba6d72008-07-04 05:05:54 -040087
Kevin O'Connor6e4583c2011-06-19 10:09:26 -040088static void
Isaku Yamahataaec19c92010-07-20 16:50:46 +090089smm_relocate_and_restore(void)
90{
Kevin O'Connorf7ba6d72008-07-04 05:05:54 -040091 /* init APM status port */
Kevin O'Connore682cbc2008-12-06 23:11:56 -050092 outb(0x01, PORT_SMI_STATUS);
Kevin O'Connorf7ba6d72008-07-04 05:05:54 -040093
94 /* raise an SMI interrupt */
Kevin O'Connore682cbc2008-12-06 23:11:56 -050095 outb(0x00, PORT_SMI_CMD);
Kevin O'Connorf7ba6d72008-07-04 05:05:54 -040096
97 /* wait until SMM code executed */
Kevin O'Connore682cbc2008-12-06 23:11:56 -050098 while (inb(PORT_SMI_STATUS) != 0x00)
Kevin O'Connorf7ba6d72008-07-04 05:05:54 -040099 ;
100
Kevin O'Connore682cbc2008-12-06 23:11:56 -0500101 /* restore original memory content */
102 memcpy((void *)BUILD_SMM_INIT_ADDR, (void *)BUILD_SMM_ADDR, BUILD_SMM_SIZE);
Kevin O'Connorf7ba6d72008-07-04 05:05:54 -0400103
104 /* copy the SMM code */
Kevin O'Connore682cbc2008-12-06 23:11:56 -0500105 memcpy((void *)BUILD_SMM_ADDR, &smm_code_start
106 , &smm_code_end - &smm_code_start);
Kevin O'Connorf7ba6d72008-07-04 05:05:54 -0400107 wbinvd();
Isaku Yamahataaec19c92010-07-20 16:50:46 +0900108}
Kevin O'Connorf7ba6d72008-07-04 05:05:54 -0400109
Kevin O'Connor6e4583c2011-06-19 10:09:26 -0400110#define I440FX_SMRAM 0x72
111#define PIIX_DEVACTB 0x58
112#define PIIX_APMC_EN (1 << 25)
113
114// This code is hardcoded for PIIX4 Power Management device.
Kevin O'Connor278b19f2011-06-21 22:41:15 -0400115static void piix4_apmc_smm_init(struct pci_device *pci, void *arg)
Kevin O'Connor6e4583c2011-06-19 10:09:26 -0400116{
Kevin O'Connor0cd70052011-07-02 14:04:19 -0400117 struct pci_device *i440_pci = pci_find_device(PCI_VENDOR_ID_INTEL
118 , PCI_DEVICE_ID_INTEL_82441);
119 if (!i440_pci)
Kevin O'Connor6e4583c2011-06-19 10:09:26 -0400120 return;
121
122 /* check if SMM init is already done */
Kevin O'Connor278b19f2011-06-21 22:41:15 -0400123 u32 value = pci_config_readl(pci->bdf, PIIX_DEVACTB);
Kevin O'Connor6e4583c2011-06-19 10:09:26 -0400124 if (value & PIIX_APMC_EN)
125 return;
126
127 /* enable the SMM memory window */
Kevin O'Connor0cd70052011-07-02 14:04:19 -0400128 pci_config_writeb(i440_pci->bdf, I440FX_SMRAM, 0x02 | 0x48);
Kevin O'Connor6e4583c2011-06-19 10:09:26 -0400129
130 smm_save_and_copy();
131
132 /* enable SMI generation when writing to the APMC register */
Kevin O'Connor278b19f2011-06-21 22:41:15 -0400133 pci_config_writel(pci->bdf, PIIX_DEVACTB, value | PIIX_APMC_EN);
Kevin O'Connor6e4583c2011-06-19 10:09:26 -0400134
135 smm_relocate_and_restore();
136
137 /* close the SMM memory window and enable normal SMM */
Kevin O'Connor0cd70052011-07-02 14:04:19 -0400138 pci_config_writeb(i440_pci->bdf, I440FX_SMRAM, 0x02 | 0x08);
Kevin O'Connor6e4583c2011-06-19 10:09:26 -0400139}
140
Isaku Yamahata72a590e2012-11-28 10:17:33 +0100141/* PCI_VENDOR_ID_INTEL && PCI_DEVICE_ID_INTEL_ICH9_LPC */
142void ich9_lpc_apmc_smm_init(struct pci_device *dev, void *arg)
143{
144 struct pci_device *mch_dev;
145 int mch_bdf;
146
147 // This code is hardcoded for Q35 Power Management device.
148 mch_dev = pci_find_device(PCI_VENDOR_ID_INTEL,
149 PCI_DEVICE_ID_INTEL_Q35_MCH);
150 mch_bdf = mch_dev->bdf;
151
152 if (mch_bdf < 0)
153 return;
154
155 /* check if SMM init is already done */
156 u32 value = inl(PORT_ACPI_PM_BASE + ICH9_PMIO_SMI_EN);
157 if (value & ICH9_PMIO_SMI_EN_APMC_EN)
158 return;
159
160 /* enable the SMM memory window */
161 pci_config_writeb(mch_bdf, Q35_HOST_BRIDGE_SMRAM, 0x02 | 0x48);
162
163 smm_save_and_copy();
164
165 /* enable SMI generation when writing to the APMC register */
166 outl(value | ICH9_PMIO_SMI_EN_APMC_EN,
167 PORT_ACPI_PM_BASE + ICH9_PMIO_SMI_EN);
168
169 smm_relocate_and_restore();
170
171 /* close the SMM memory window and enable normal SMM */
172 pci_config_writeb(mch_bdf, Q35_HOST_BRIDGE_SMRAM, 0x02 | 0x08);
173}
174
Isaku Yamahataaec19c92010-07-20 16:50:46 +0900175static const struct pci_device_id smm_init_tbl[] = {
176 PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82371AB_3,
177 piix4_apmc_smm_init),
Isaku Yamahata72a590e2012-11-28 10:17:33 +0100178 PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_ICH9_LPC,
179 ich9_lpc_apmc_smm_init),
Isaku Yamahataaec19c92010-07-20 16:50:46 +0900180
181 PCI_DEVICE_END,
182};
183
184void
185smm_init(void)
186{
187 if (CONFIG_COREBOOT)
188 // SMM only supported on emulators.
189 return;
190 if (!CONFIG_USE_SMM)
191 return;
Ian Campbell12c991e2012-06-28 11:08:33 +0100192 if (usingXen())
193 return;
Isaku Yamahataaec19c92010-07-20 16:50:46 +0900194
195 dprintf(3, "init smm\n");
196 pci_find_init_device(smm_init_tbl, NULL);
Kevin O'Connorf7ba6d72008-07-04 05:05:54 -0400197}