blob: 72e5e88c554b1603d67d1a8c332a5269f13aeaf4 [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
Kevin O'Connorf7ba6d72008-07-04 05:05:54 -040013
Kevin O'Connor52a300f2009-12-26 23:32:57 -050014ASM32FLAT(
Kevin O'Connorf7ba6d72008-07-04 05:05:54 -040015 ".global smm_relocation_start\n"
16 ".global smm_relocation_end\n"
17 ".global smm_code_start\n"
18 ".global smm_code_end\n"
19 " .code16\n"
20
21 /* code to relocate SMBASE to 0xa0000 */
22 "smm_relocation_start:\n"
Kevin O'Connore682cbc2008-12-06 23:11:56 -050023 " mov $" __stringify(BUILD_SMM_INIT_ADDR) " + 0x7efc, %ebx\n"
Kevin O'Connorf7ba6d72008-07-04 05:05:54 -040024 " addr32 mov (%ebx), %al\n" /* revision ID to see if x86_64 or x86 */
25 " cmp $0x64, %al\n"
26 " je 1f\n"
Kevin O'Connore682cbc2008-12-06 23:11:56 -050027 " mov $" __stringify(BUILD_SMM_INIT_ADDR) " + 0x7ef8, %ebx\n"
Kevin O'Connorf7ba6d72008-07-04 05:05:54 -040028 " jmp 2f\n"
29 "1:\n"
Kevin O'Connore682cbc2008-12-06 23:11:56 -050030 " mov $" __stringify(BUILD_SMM_INIT_ADDR) " + 0x7f00, %ebx\n"
Kevin O'Connorf7ba6d72008-07-04 05:05:54 -040031 "2:\n"
Kevin O'Connore682cbc2008-12-06 23:11:56 -050032 " movl $" __stringify(BUILD_SMM_ADDR) " - 0x8000, %eax\n"
Kevin O'Connorf7ba6d72008-07-04 05:05:54 -040033 " addr32 movl %eax, (%ebx)\n"
34 /* indicate to the BIOS that the SMM code was executed */
35 " mov $0x00, %al\n"
Kevin O'Connore682cbc2008-12-06 23:11:56 -050036 " movw $" __stringify(PORT_SMI_STATUS) ", %dx\n"
Kevin O'Connorf7ba6d72008-07-04 05:05:54 -040037 " outb %al, %dx\n"
38 " rsm\n"
39 "smm_relocation_end:\n"
40
41 /* minimal SMM code to enable or disable ACPI */
42 "smm_code_start:\n"
Kevin O'Connore682cbc2008-12-06 23:11:56 -050043 " movw $" __stringify(PORT_SMI_CMD) ", %dx\n"
Kevin O'Connorf7ba6d72008-07-04 05:05:54 -040044 " inb %dx, %al\n"
45 " cmp $0xf0, %al\n"
46 " jne 1f\n"
47
48 /* ACPI disable */
Kevin O'Connore682cbc2008-12-06 23:11:56 -050049 " mov $" __stringify(PORT_ACPI_PM_BASE) " + 0x04, %dx\n" /* PMCNTRL */
Kevin O'Connorf7ba6d72008-07-04 05:05:54 -040050 " inw %dx, %ax\n"
51 " andw $~1, %ax\n"
52 " outw %ax, %dx\n"
53
54 " jmp 2f\n"
55
56 "1:\n"
57 " cmp $0xf1, %al\n"
58 " jne 2f\n"
59
60 /* ACPI enable */
Kevin O'Connore682cbc2008-12-06 23:11:56 -050061 " mov $" __stringify(PORT_ACPI_PM_BASE) " + 0x04, %dx\n" /* PMCNTRL */
Kevin O'Connorf7ba6d72008-07-04 05:05:54 -040062 " inw %dx, %ax\n"
63 " orw $1, %ax\n"
64 " outw %ax, %dx\n"
65
66 "2:\n"
67 " rsm\n"
68 "smm_code_end:\n"
69 " .code32\n"
70 );
Kevin O'Connorf7ba6d72008-07-04 05:05:54 -040071
72extern u8 smm_relocation_start, smm_relocation_end;
73extern u8 smm_code_start, smm_code_end;
74
Kevin O'Connor6e4583c2011-06-19 10:09:26 -040075static void
Isaku Yamahataaec19c92010-07-20 16:50:46 +090076smm_save_and_copy(void)
Kevin O'Connorf7ba6d72008-07-04 05:05:54 -040077{
Kevin O'Connore682cbc2008-12-06 23:11:56 -050078 /* save original memory content */
79 memcpy((void *)BUILD_SMM_ADDR, (void *)BUILD_SMM_INIT_ADDR, BUILD_SMM_SIZE);
80
Kevin O'Connorf7ba6d72008-07-04 05:05:54 -040081 /* copy the SMM relocation code */
Kevin O'Connore682cbc2008-12-06 23:11:56 -050082 memcpy((void *)BUILD_SMM_INIT_ADDR, &smm_relocation_start,
Kevin O'Connorf7ba6d72008-07-04 05:05:54 -040083 &smm_relocation_end - &smm_relocation_start);
Isaku Yamahataaec19c92010-07-20 16:50:46 +090084}
Kevin O'Connorf7ba6d72008-07-04 05:05:54 -040085
Kevin O'Connor6e4583c2011-06-19 10:09:26 -040086static void
Isaku Yamahataaec19c92010-07-20 16:50:46 +090087smm_relocate_and_restore(void)
88{
Kevin O'Connorf7ba6d72008-07-04 05:05:54 -040089 /* init APM status port */
Kevin O'Connore682cbc2008-12-06 23:11:56 -050090 outb(0x01, PORT_SMI_STATUS);
Kevin O'Connorf7ba6d72008-07-04 05:05:54 -040091
92 /* raise an SMI interrupt */
Kevin O'Connore682cbc2008-12-06 23:11:56 -050093 outb(0x00, PORT_SMI_CMD);
Kevin O'Connorf7ba6d72008-07-04 05:05:54 -040094
95 /* wait until SMM code executed */
Kevin O'Connore682cbc2008-12-06 23:11:56 -050096 while (inb(PORT_SMI_STATUS) != 0x00)
Kevin O'Connorf7ba6d72008-07-04 05:05:54 -040097 ;
98
Kevin O'Connore682cbc2008-12-06 23:11:56 -050099 /* restore original memory content */
100 memcpy((void *)BUILD_SMM_INIT_ADDR, (void *)BUILD_SMM_ADDR, BUILD_SMM_SIZE);
Kevin O'Connorf7ba6d72008-07-04 05:05:54 -0400101
102 /* copy the SMM code */
Kevin O'Connore682cbc2008-12-06 23:11:56 -0500103 memcpy((void *)BUILD_SMM_ADDR, &smm_code_start
104 , &smm_code_end - &smm_code_start);
Kevin O'Connorf7ba6d72008-07-04 05:05:54 -0400105 wbinvd();
Isaku Yamahataaec19c92010-07-20 16:50:46 +0900106}
Kevin O'Connorf7ba6d72008-07-04 05:05:54 -0400107
Kevin O'Connor6e4583c2011-06-19 10:09:26 -0400108#define I440FX_SMRAM 0x72
109#define PIIX_DEVACTB 0x58
110#define PIIX_APMC_EN (1 << 25)
111
112// This code is hardcoded for PIIX4 Power Management device.
Kevin O'Connor278b19f2011-06-21 22:41:15 -0400113static void piix4_apmc_smm_init(struct pci_device *pci, void *arg)
Kevin O'Connor6e4583c2011-06-19 10:09:26 -0400114{
Kevin O'Connor0cd70052011-07-02 14:04:19 -0400115 struct pci_device *i440_pci = pci_find_device(PCI_VENDOR_ID_INTEL
116 , PCI_DEVICE_ID_INTEL_82441);
117 if (!i440_pci)
Kevin O'Connor6e4583c2011-06-19 10:09:26 -0400118 return;
119
120 /* check if SMM init is already done */
Kevin O'Connor278b19f2011-06-21 22:41:15 -0400121 u32 value = pci_config_readl(pci->bdf, PIIX_DEVACTB);
Kevin O'Connor6e4583c2011-06-19 10:09:26 -0400122 if (value & PIIX_APMC_EN)
123 return;
124
125 /* enable the SMM memory window */
Kevin O'Connor0cd70052011-07-02 14:04:19 -0400126 pci_config_writeb(i440_pci->bdf, I440FX_SMRAM, 0x02 | 0x48);
Kevin O'Connor6e4583c2011-06-19 10:09:26 -0400127
128 smm_save_and_copy();
129
130 /* enable SMI generation when writing to the APMC register */
Kevin O'Connor278b19f2011-06-21 22:41:15 -0400131 pci_config_writel(pci->bdf, PIIX_DEVACTB, value | PIIX_APMC_EN);
Kevin O'Connor6e4583c2011-06-19 10:09:26 -0400132
133 smm_relocate_and_restore();
134
135 /* close the SMM memory window and enable normal SMM */
Kevin O'Connor0cd70052011-07-02 14:04:19 -0400136 pci_config_writeb(i440_pci->bdf, I440FX_SMRAM, 0x02 | 0x08);
Kevin O'Connor6e4583c2011-06-19 10:09:26 -0400137}
138
Isaku Yamahataaec19c92010-07-20 16:50:46 +0900139static 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),
142
143 PCI_DEVICE_END,
144};
145
146void
147smm_init(void)
148{
149 if (CONFIG_COREBOOT)
150 // SMM only supported on emulators.
151 return;
152 if (!CONFIG_USE_SMM)
153 return;
154
155 dprintf(3, "init smm\n");
156 pci_find_init_device(smm_init_tbl, NULL);
Kevin O'Connorf7ba6d72008-07-04 05:05:54 -0400157}