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
16 ".global smm_relocation_start\n"
17 ".global smm_relocation_end\n"
18 ".global smm_code_start\n"
19 ".global smm_code_end\n"
22 /* code to relocate SMBASE to 0xa0000 */
23 "smm_relocation_start:\n"
24 " mov $" __stringify(BUILD_SMM_INIT_ADDR) " + 0x7efc, %ebx\n"
25 " addr32 mov (%ebx), %al\n" /* revision ID to see if x86_64 or x86 */
28 " mov $" __stringify(BUILD_SMM_INIT_ADDR) " + 0x7ef8, %ebx\n"
31 " mov $" __stringify(BUILD_SMM_INIT_ADDR) " + 0x7f00, %ebx\n"
33 " movl $" __stringify(BUILD_SMM_ADDR) " - 0x8000, %eax\n"
34 " addr32 movl %eax, (%ebx)\n"
35 /* indicate to the BIOS that the SMM code was executed */
37 " movw $" __stringify(PORT_SMI_STATUS) ", %dx\n"
40 "smm_relocation_end:\n"
42 /* minimal SMM code to enable or disable ACPI */
44 " movw $" __stringify(PORT_SMI_CMD) ", %dx\n"
50 " mov $" __stringify(PORT_ACPI_PM_BASE) " + 0x04, %dx\n" /* PMCNTRL */
62 " 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;
81 // SMM only supported on emulators.
86 dprintf(3, "init smm\n");
88 // This code is hardcoded for PIIX4 Power Management device.
89 int bdf = pci_find_device(PCI_VENDOR_ID_INTEL
90 , PCI_DEVICE_ID_INTEL_82371AB_3);
94 int i440_bdf = pci_find_device(PCI_VENDOR_ID_INTEL
95 , PCI_DEVICE_ID_INTEL_82441);
99 /* check if SMM init is already done */
100 u32 value = pci_config_readl(bdf, 0x58);
101 if (value & (1 << 25))
104 /* enable the SMM memory window */
105 pci_config_writeb(i440_bdf, 0x72, 0x02 | 0x48);
107 /* save original memory content */
108 memcpy((void *)BUILD_SMM_ADDR, (void *)BUILD_SMM_INIT_ADDR, BUILD_SMM_SIZE);
110 /* copy the SMM relocation code */
111 memcpy((void *)BUILD_SMM_INIT_ADDR, &smm_relocation_start,
112 &smm_relocation_end - &smm_relocation_start);
114 /* enable SMI generation when writing to the APMC register */
115 pci_config_writel(bdf, 0x58, value | (1 << 25));
117 /* init APM status port */
118 outb(0x01, PORT_SMI_STATUS);
120 /* raise an SMI interrupt */
121 outb(0x00, PORT_SMI_CMD);
123 /* wait until SMM code executed */
124 while (inb(PORT_SMI_STATUS) != 0x00)
127 /* restore original memory content */
128 memcpy((void *)BUILD_SMM_INIT_ADDR, (void *)BUILD_SMM_ADDR, BUILD_SMM_SIZE);
130 /* copy the SMM code */
131 memcpy((void *)BUILD_SMM_ADDR, &smm_code_start
132 , &smm_code_end - &smm_code_start);
135 /* close the SMM memory window and enable normal SMM */
136 pci_config_writeb(i440_bdf, 0x72, 0x02 | 0x08);