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 "util.h" // wbinvd
10 #include "config.h" // CONFIG_*
11 #include "ioport.h" // outb
12 #include "pci_ids.h" // PCI_VENDOR_ID_INTEL
15 ".global smm_relocation_start\n"
16 ".global smm_relocation_end\n"
17 ".global smm_code_start\n"
18 ".global smm_code_end\n"
21 /* code to relocate SMBASE to 0xa0000 */
22 "smm_relocation_start:\n"
23 " mov $" __stringify(BUILD_SMM_INIT_ADDR) " + 0x7efc, %ebx\n"
24 " addr32 mov (%ebx), %al\n" /* revision ID to see if x86_64 or x86 */
27 " mov $" __stringify(BUILD_SMM_INIT_ADDR) " + 0x7ef8, %ebx\n"
30 " mov $" __stringify(BUILD_SMM_INIT_ADDR) " + 0x7f00, %ebx\n"
32 " movl $" __stringify(BUILD_SMM_ADDR) " - 0x8000, %eax\n"
33 " addr32 movl %eax, (%ebx)\n"
34 /* indicate to the BIOS that the SMM code was executed */
36 " movw $" __stringify(PORT_SMI_STATUS) ", %dx\n"
39 "smm_relocation_end:\n"
41 /* minimal SMM code to enable or disable ACPI */
43 " movw $" __stringify(PORT_SMI_CMD) ", %dx\n"
49 " mov $" __stringify(PORT_ACPI_PM_BASE) " + 0x04, %dx\n" /* PMCNTRL */
61 " mov $" __stringify(PORT_ACPI_PM_BASE) " + 0x04, %dx\n" /* PMCNTRL */
72 extern u8 smm_relocation_start, smm_relocation_end;
73 extern u8 smm_code_start, smm_code_end;
76 smm_save_and_copy(void)
78 /* save original memory content */
79 memcpy((void *)BUILD_SMM_ADDR, (void *)BUILD_SMM_INIT_ADDR, BUILD_SMM_SIZE);
81 /* copy the SMM relocation code */
82 memcpy((void *)BUILD_SMM_INIT_ADDR, &smm_relocation_start,
83 &smm_relocation_end - &smm_relocation_start);
87 smm_relocate_and_restore(void)
89 /* init APM status port */
90 outb(0x01, PORT_SMI_STATUS);
92 /* raise an SMI interrupt */
93 outb(0x00, PORT_SMI_CMD);
95 /* wait until SMM code executed */
96 while (inb(PORT_SMI_STATUS) != 0x00)
99 /* restore original memory content */
100 memcpy((void *)BUILD_SMM_INIT_ADDR, (void *)BUILD_SMM_ADDR, BUILD_SMM_SIZE);
102 /* copy the SMM code */
103 memcpy((void *)BUILD_SMM_ADDR, &smm_code_start
104 , &smm_code_end - &smm_code_start);
108 #define I440FX_SMRAM 0x72
109 #define PIIX_DEVACTB 0x58
110 #define PIIX_APMC_EN (1 << 25)
112 // This code is hardcoded for PIIX4 Power Management device.
113 static void piix4_apmc_smm_init(struct pci_device *pci, void *arg)
115 struct pci_device *i440_pci = pci_find_device(PCI_VENDOR_ID_INTEL
116 , PCI_DEVICE_ID_INTEL_82441);
120 /* check if SMM init is already done */
121 u32 value = pci_config_readl(pci->bdf, PIIX_DEVACTB);
122 if (value & PIIX_APMC_EN)
125 /* enable the SMM memory window */
126 pci_config_writeb(i440_pci->bdf, I440FX_SMRAM, 0x02 | 0x48);
130 /* enable SMI generation when writing to the APMC register */
131 pci_config_writel(pci->bdf, PIIX_DEVACTB, value | PIIX_APMC_EN);
133 smm_relocate_and_restore();
135 /* close the SMM memory window and enable normal SMM */
136 pci_config_writeb(i440_pci->bdf, I440FX_SMRAM, 0x02 | 0x08);
139 static const struct pci_device_id smm_init_tbl[] = {
140 PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82371AB_3,
141 piix4_apmc_smm_init),
150 // SMM only supported on emulators.
155 dprintf(3, "init smm\n");
156 pci_find_init_device(smm_init_tbl, NULL);