Extract 'struct bregs' out of biosvar.h; clean up header includes.
[seabios.git] / src / smm.c
1 // 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
8 #include "pci.h" // PCIDevice
9 #include "util.h" // wbinvd
10 #include "config.h" // CONFIG_*
11 #include "ioport.h" // outb
12
13 asm(
14     ".global smm_relocation_start\n"
15     ".global smm_relocation_end\n"
16     ".global smm_code_start\n"
17     ".global smm_code_end\n"
18     "  .code16\n"
19
20     /* code to relocate SMBASE to 0xa0000 */
21     "smm_relocation_start:\n"
22     "  mov $0x38000 + 0x7efc, %ebx\n"
23     "  addr32 mov (%ebx), %al\n"  /* revision ID to see if x86_64 or x86 */
24     "  cmp $0x64, %al\n"
25     "  je 1f\n"
26     "  mov $0x38000 + 0x7ef8, %ebx\n"
27     "  jmp 2f\n"
28     "1:\n"
29     "  mov $0x38000 + 0x7f00, %ebx\n"
30     "2:\n"
31     "  movl $0xa0000, %eax\n"
32     "  addr32 movl %eax, (%ebx)\n"
33     /* indicate to the BIOS that the SMM code was executed */
34     "  mov $0x00, %al\n"
35     "  movw $0xb3, %dx\n"
36     "  outb %al, %dx\n"
37     "  rsm\n"
38     "smm_relocation_end:\n"
39
40     /* minimal SMM code to enable or disable ACPI */
41     "smm_code_start:\n"
42     "  movw $0xb2, %dx\n"
43     "  inb %dx, %al\n"
44     "  cmp $0xf0, %al\n"
45     "  jne 1f\n"
46
47     /* ACPI disable */
48     "  mov $" __stringify(BUILD_PM_IO_BASE) " + 0x04, %dx\n" /* PMCNTRL */
49     "  inw %dx, %ax\n"
50     "  andw $~1, %ax\n"
51     "  outw %ax, %dx\n"
52
53     "  jmp 2f\n"
54
55     "1:\n"
56     "  cmp $0xf1, %al\n"
57     "  jne 2f\n"
58
59     /* ACPI enable */
60     "  mov $" __stringify(BUILD_PM_IO_BASE) " + 0x04, %dx\n" /* PMCNTRL */
61     "  inw %dx, %ax\n"
62     "  orw $1, %ax\n"
63     "  outw %ax, %dx\n"
64
65     "2:\n"
66     "  rsm\n"
67     "smm_code_end:\n"
68     "  .code32\n"
69     );
70
71 extern u8 smm_relocation_start, smm_relocation_end;
72 extern u8 smm_code_start, smm_code_end;
73
74 void
75 smm_init()
76 {
77     if (!CONFIG_USE_SMM)
78         return;
79
80     // This code is hardcoded for PIIX4 Power Management device.
81     PCIDevice i440_pcidev, d;
82     int ret = pci_find_device(0x8086, 0x7113, 0, &d);
83     if (ret)
84         // Device not found
85         return;
86     ret = pci_find_device(0x8086, 0x1237, 0, &i440_pcidev);
87     if (ret)
88         return;
89
90     /* check if SMM init is already done */
91     u32 value = pci_config_readl(d, 0x58);
92     if (value & (1 << 25))
93         return;
94
95     /* copy the SMM relocation code */
96     memcpy((void *)0x38000, &smm_relocation_start,
97            &smm_relocation_end - &smm_relocation_start);
98
99     /* enable SMI generation when writing to the APMC register */
100     pci_config_writel(d, 0x58, value | (1 << 25));
101
102     /* init APM status port */
103     outb(0x01, 0xb3);
104
105     /* raise an SMI interrupt */
106     outb(0x00, 0xb2);
107
108     /* wait until SMM code executed */
109     while (inb(0xb3) != 0x00)
110         ;
111
112     /* enable the SMM memory window */
113     pci_config_writeb(i440_pcidev, 0x72, 0x02 | 0x48);
114
115     /* copy the SMM code */
116     memcpy((void *)0xa8000, &smm_code_start,
117            &smm_code_end - &smm_code_start);
118     wbinvd();
119
120     /* close the SMM memory window and enable normal SMM */
121     pci_config_writeb(i440_pcidev, 0x72, 0x02 | 0x08);
122 }