1
// System Management Mode support (on emulators)
3
// Copyright (C) 2008 Kevin O'Connor <kevin@koconnor.net>
4
// Copyright (C) 2006 Fabrice Bellard
6
// This file may be distributed under the terms of the GNU LGPLv3 license.
8
#include "pci.h" // pci_config_writel
9
#include "pci_regs.h" // PCI_DEVICE_ID
10
#include "util.h" // wbinvd
11
#include "config.h" // CONFIG_*
12
#include "ioport.h" // outb
13
#include "pci_ids.h" // PCI_VENDOR_ID_INTEL
17
".global smm_relocation_start\n"
18
".global smm_relocation_end\n"
19
".global smm_code_start\n"
20
".global smm_code_end\n"
23
/* code to relocate SMBASE to 0xa0000 */
24
"smm_relocation_start:\n"
25
" mov $" __stringify(BUILD_SMM_INIT_ADDR) " + 0x7efc, %ebx\n"
26
" addr32 mov (%ebx), %al\n" /* revision ID to see if x86_64 or x86 */
29
" mov $" __stringify(BUILD_SMM_INIT_ADDR) " + 0x7ef8, %ebx\n"
32
" mov $" __stringify(BUILD_SMM_INIT_ADDR) " + 0x7f00, %ebx\n"
34
" movl $" __stringify(BUILD_SMM_ADDR) " - 0x8000, %eax\n"
35
" addr32 movl %eax, (%ebx)\n"
36
/* indicate to the BIOS that the SMM code was executed */
38
" movw $" __stringify(PORT_SMI_STATUS) ", %dx\n"
41
"smm_relocation_end:\n"
43
/* minimal SMM code to enable or disable ACPI */
45
" movw $" __stringify(PORT_SMI_CMD) ", %dx\n"
51
" mov $" __stringify(PORT_ACPI_PM_BASE) " + 0x04, %dx\n" /* PMCNTRL */
63
" mov $" __stringify(PORT_ACPI_PM_BASE) " + 0x04, %dx\n" /* PMCNTRL */
74
extern u8 smm_relocation_start, smm_relocation_end;
75
extern u8 smm_code_start, smm_code_end;
78
smm_save_and_copy(void)
80
/* save original memory content */
81
memcpy((void *)BUILD_SMM_ADDR, (void *)BUILD_SMM_INIT_ADDR, BUILD_SMM_SIZE);
83
/* copy the SMM relocation code */
84
memcpy((void *)BUILD_SMM_INIT_ADDR, &smm_relocation_start,
85
&smm_relocation_end - &smm_relocation_start);
89
smm_relocate_and_restore(void)
91
/* init APM status port */
92
outb(0x01, PORT_SMI_STATUS);
94
/* raise an SMI interrupt */
95
outb(0x00, PORT_SMI_CMD);
97
/* wait until SMM code executed */
98
while (inb(PORT_SMI_STATUS) != 0x00)
101
/* restore original memory content */
102
memcpy((void *)BUILD_SMM_INIT_ADDR, (void *)BUILD_SMM_ADDR, BUILD_SMM_SIZE);
104
/* copy the SMM code */
105
memcpy((void *)BUILD_SMM_ADDR, &smm_code_start
106
, &smm_code_end - &smm_code_start);
110
#define I440FX_SMRAM 0x72
111
#define PIIX_DEVACTB 0x58
112
#define PIIX_APMC_EN (1 << 25)
114
// This code is hardcoded for PIIX4 Power Management device.
115
static void piix4_apmc_smm_setup(int isabdf, int i440_bdf)
117
/* check if SMM init is already done */
118
u32 value = pci_config_readl(isabdf, PIIX_DEVACTB);
119
if (value & PIIX_APMC_EN)
122
/* enable the SMM memory window */
123
pci_config_writeb(i440_bdf, I440FX_SMRAM, 0x02 | 0x48);
127
/* enable SMI generation when writing to the APMC register */
128
pci_config_writel(isabdf, PIIX_DEVACTB, value | PIIX_APMC_EN);
130
smm_relocate_and_restore();
132
/* close the SMM memory window and enable normal SMM */
133
pci_config_writeb(i440_bdf, I440FX_SMRAM, 0x02 | 0x08);
136
/* PCI_VENDOR_ID_INTEL && PCI_DEVICE_ID_INTEL_ICH9_LPC */
137
void ich9_lpc_apmc_smm_setup(int isabdf, int mch_bdf)
139
/* check if SMM init is already done */
140
u32 value = inl(PORT_ACPI_PM_BASE + ICH9_PMIO_SMI_EN);
141
if (value & ICH9_PMIO_SMI_EN_APMC_EN)
144
/* enable the SMM memory window */
145
pci_config_writeb(mch_bdf, Q35_HOST_BRIDGE_SMRAM, 0x02 | 0x48);
149
/* enable SMI generation when writing to the APMC register */
150
outl(value | ICH9_PMIO_SMI_EN_APMC_EN,
151
PORT_ACPI_PM_BASE + ICH9_PMIO_SMI_EN);
153
smm_relocate_and_restore();
155
/* close the SMM memory window and enable normal SMM */
156
pci_config_writeb(mch_bdf, Q35_HOST_BRIDGE_SMRAM, 0x02 | 0x08);
159
static int SMMISADeviceBDF = -1, SMMPMDeviceBDF = -1;
162
smm_device_setup(void)
167
struct pci_device *isapci, *pmpci;
168
isapci = pci_find_device(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82371AB_3);
169
pmpci = pci_find_device(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82441);
170
if (isapci && pmpci) {
171
SMMISADeviceBDF = isapci->bdf;
172
SMMPMDeviceBDF = pmpci->bdf;
175
isapci = pci_find_device(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_ICH9_LPC);
176
pmpci = pci_find_device(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_Q35_MCH);
177
if (isapci && pmpci) {
178
SMMISADeviceBDF = isapci->bdf;
179
SMMPMDeviceBDF = pmpci->bdf;
186
if (!CONFIG_USE_SMM || SMMISADeviceBDF < 0)
189
dprintf(3, "init smm\n");
190
u16 device = pci_config_readw(SMMISADeviceBDF, PCI_DEVICE_ID);
191
if (device == PCI_DEVICE_ID_INTEL_82371AB_3)
192
piix4_apmc_smm_setup(SMMISADeviceBDF, SMMPMDeviceBDF);
194
ich9_lpc_apmc_smm_setup(SMMISADeviceBDF, SMMPMDeviceBDF);