blob: b2140b4a787eb181df2b757c3b6df54a0b101c83 [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//
6// This file may be distributed under the terms of the GNU GPLv3 license.
7
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'Connor40967022008-07-21 22:23:05 -040014#if CONFIG_USE_SMM
Kevin O'Connorf7ba6d72008-07-04 05:05:54 -040015asm(
16 ".global smm_relocation_start\n"
17 ".global smm_relocation_end\n"
18 ".global smm_code_start\n"
19 ".global smm_code_end\n"
20 " .code16\n"
21
22 /* code to relocate SMBASE to 0xa0000 */
23 "smm_relocation_start:\n"
24 " mov $0x38000 + 0x7efc, %ebx\n"
25 " addr32 mov (%ebx), %al\n" /* revision ID to see if x86_64 or x86 */
26 " cmp $0x64, %al\n"
27 " je 1f\n"
28 " mov $0x38000 + 0x7ef8, %ebx\n"
29 " jmp 2f\n"
30 "1:\n"
31 " mov $0x38000 + 0x7f00, %ebx\n"
32 "2:\n"
33 " movl $0xa0000, %eax\n"
34 " addr32 movl %eax, (%ebx)\n"
35 /* indicate to the BIOS that the SMM code was executed */
36 " mov $0x00, %al\n"
37 " movw $0xb3, %dx\n"
38 " outb %al, %dx\n"
39 " rsm\n"
40 "smm_relocation_end:\n"
41
42 /* minimal SMM code to enable or disable ACPI */
43 "smm_code_start:\n"
44 " movw $0xb2, %dx\n"
45 " inb %dx, %al\n"
46 " cmp $0xf0, %al\n"
47 " jne 1f\n"
48
49 /* ACPI disable */
50 " mov $" __stringify(BUILD_PM_IO_BASE) " + 0x04, %dx\n" /* PMCNTRL */
51 " inw %dx, %ax\n"
52 " andw $~1, %ax\n"
53 " outw %ax, %dx\n"
54
55 " jmp 2f\n"
56
57 "1:\n"
58 " cmp $0xf1, %al\n"
59 " jne 2f\n"
60
61 /* ACPI enable */
62 " mov $" __stringify(BUILD_PM_IO_BASE) " + 0x04, %dx\n" /* PMCNTRL */
63 " inw %dx, %ax\n"
64 " orw $1, %ax\n"
65 " outw %ax, %dx\n"
66
67 "2:\n"
68 " rsm\n"
69 "smm_code_end:\n"
70 " .code32\n"
71 );
Kevin O'Connor40967022008-07-21 22:23:05 -040072#endif
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
77void
78smm_init()
79{
80 if (!CONFIG_USE_SMM)
81 return;
82
Kevin O'Connor7b49cd92008-11-08 10:35:26 -050083 dprintf(3, "init smm\n");
84
Kevin O'Connorf7ba6d72008-07-04 05:05:54 -040085 // This code is hardcoded for PIIX4 Power Management device.
Kevin O'Connorbe19cdc2008-11-09 15:33:47 -050086 int bdf = pci_find_device(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82371AB_3
87 , 0);
88 if (bdf < 0)
Kevin O'Connorf7ba6d72008-07-04 05:05:54 -040089 // Device not found
90 return;
Kevin O'Connorbe19cdc2008-11-09 15:33:47 -050091 int i440_bdf = pci_find_device(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82441
92 , 0);
93 if (i440_bdf < 0)
Kevin O'Connorf7ba6d72008-07-04 05:05:54 -040094 return;
95
96 /* check if SMM init is already done */
Kevin O'Connorbe19cdc2008-11-09 15:33:47 -050097 u32 value = pci_config_readl(bdf, 0x58);
Kevin O'Connorf7ba6d72008-07-04 05:05:54 -040098 if (value & (1 << 25))
99 return;
100
101 /* copy the SMM relocation code */
102 memcpy((void *)0x38000, &smm_relocation_start,
103 &smm_relocation_end - &smm_relocation_start);
104
105 /* enable SMI generation when writing to the APMC register */
Kevin O'Connorbe19cdc2008-11-09 15:33:47 -0500106 pci_config_writel(bdf, 0x58, value | (1 << 25));
Kevin O'Connorf7ba6d72008-07-04 05:05:54 -0400107
108 /* init APM status port */
109 outb(0x01, 0xb3);
110
111 /* raise an SMI interrupt */
112 outb(0x00, 0xb2);
113
114 /* wait until SMM code executed */
115 while (inb(0xb3) != 0x00)
116 ;
117
118 /* enable the SMM memory window */
Kevin O'Connorbe19cdc2008-11-09 15:33:47 -0500119 pci_config_writeb(i440_bdf, 0x72, 0x02 | 0x48);
Kevin O'Connorf7ba6d72008-07-04 05:05:54 -0400120
121 /* copy the SMM code */
Kevin O'Connorbe19cdc2008-11-09 15:33:47 -0500122 memcpy((void *)0xa8000, &smm_code_start, &smm_code_end - &smm_code_start);
Kevin O'Connorf7ba6d72008-07-04 05:05:54 -0400123 wbinvd();
124
125 /* close the SMM memory window and enable normal SMM */
Kevin O'Connorbe19cdc2008-11-09 15:33:47 -0500126 pci_config_writeb(i440_bdf, 0x72, 0x02 | 0x08);
Kevin O'Connorf7ba6d72008-07-04 05:05:54 -0400127}