~ubuntu-branches/ubuntu/trusty/seabios/trusty-proposed

« back to all changes in this revision

Viewing changes to src/smm.c

  • Committer: Package Import Robot
  • Author(s): Michael Tokarev
  • Date: 2014-01-16 15:50:54 UTC
  • mfrom: (1.1.8)
  • Revision ID: package-import@ubuntu.com-20140116155054-cp15pjpnirgyn1y1
Tags: 1.7.4-1
* new upstream release (1.7.4)
* remove all patches (upstreamed)
* acpi tables are now in $out/src/fw/ not in $out/

Show diffs side-by-side

added added

removed removed

Lines of Context:
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 LGPLv3 license.
7
 
 
8
 
#include "pci.h" // pci_config_writel
9
 
#include "pci_regs.h" // PCI_DEVICE_ID
10
 
#include "util.h" // wbinvd
11
 
#include "config.h" // CONFIG_*
12
 
#include "ioport.h" // outb
13
 
#include "pci_ids.h" // PCI_VENDOR_ID_INTEL
14
 
#include "dev-q35.h"
15
 
 
16
 
ASM32FLAT(
17
 
    ".global smm_relocation_start\n"
18
 
    ".global smm_relocation_end\n"
19
 
    ".global smm_code_start\n"
20
 
    ".global smm_code_end\n"
21
 
    "  .code16\n"
22
 
 
23
 
    /* code to relocate SMBASE to 0xa0000 */
24
 
    "smm_relocation_start:\n"
25
 
    "  mov $" __stringify(BUILD_SMM_INIT_ADDR) " + 0x7efc, %ebx\n"
26
 
    "  addr32 mov (%ebx), %al\n"  /* revision ID to see if x86_64 or x86 */
27
 
    "  cmp $0x64, %al\n"
28
 
    "  je 1f\n"
29
 
    "  mov $" __stringify(BUILD_SMM_INIT_ADDR) " + 0x7ef8, %ebx\n"
30
 
    "  jmp 2f\n"
31
 
    "1:\n"
32
 
    "  mov $" __stringify(BUILD_SMM_INIT_ADDR) " + 0x7f00, %ebx\n"
33
 
    "2:\n"
34
 
    "  movl $" __stringify(BUILD_SMM_ADDR) " - 0x8000, %eax\n"
35
 
    "  addr32 movl %eax, (%ebx)\n"
36
 
    /* indicate to the BIOS that the SMM code was executed */
37
 
    "  mov $0x00, %al\n"
38
 
    "  movw $" __stringify(PORT_SMI_STATUS) ", %dx\n"
39
 
    "  outb %al, %dx\n"
40
 
    "  rsm\n"
41
 
    "smm_relocation_end:\n"
42
 
 
43
 
    /* minimal SMM code to enable or disable ACPI */
44
 
    "smm_code_start:\n"
45
 
    "  movw $" __stringify(PORT_SMI_CMD) ", %dx\n"
46
 
    "  inb %dx, %al\n"
47
 
    "  cmp $0xf0, %al\n"
48
 
    "  jne 1f\n"
49
 
 
50
 
    /* ACPI disable */
51
 
    "  mov $" __stringify(PORT_ACPI_PM_BASE) " + 0x04, %dx\n" /* PMCNTRL */
52
 
    "  inw %dx, %ax\n"
53
 
    "  andw $~1, %ax\n"
54
 
    "  outw %ax, %dx\n"
55
 
 
56
 
    "  jmp 2f\n"
57
 
 
58
 
    "1:\n"
59
 
    "  cmp $0xf1, %al\n"
60
 
    "  jne 2f\n"
61
 
 
62
 
    /* ACPI enable */
63
 
    "  mov $" __stringify(PORT_ACPI_PM_BASE) " + 0x04, %dx\n" /* PMCNTRL */
64
 
    "  inw %dx, %ax\n"
65
 
    "  orw $1, %ax\n"
66
 
    "  outw %ax, %dx\n"
67
 
 
68
 
    "2:\n"
69
 
    "  rsm\n"
70
 
    "smm_code_end:\n"
71
 
    "  .code32\n"
72
 
    );
73
 
 
74
 
extern u8 smm_relocation_start, smm_relocation_end;
75
 
extern u8 smm_code_start, smm_code_end;
76
 
 
77
 
static void
78
 
smm_save_and_copy(void)
79
 
{
80
 
    /* save original memory content */
81
 
    memcpy((void *)BUILD_SMM_ADDR, (void *)BUILD_SMM_INIT_ADDR, BUILD_SMM_SIZE);
82
 
 
83
 
    /* copy the SMM relocation code */
84
 
    memcpy((void *)BUILD_SMM_INIT_ADDR, &smm_relocation_start,
85
 
           &smm_relocation_end - &smm_relocation_start);
86
 
}
87
 
 
88
 
static void
89
 
smm_relocate_and_restore(void)
90
 
{
91
 
    /* init APM status port */
92
 
    outb(0x01, PORT_SMI_STATUS);
93
 
 
94
 
    /* raise an SMI interrupt */
95
 
    outb(0x00, PORT_SMI_CMD);
96
 
 
97
 
    /* wait until SMM code executed */
98
 
    while (inb(PORT_SMI_STATUS) != 0x00)
99
 
        ;
100
 
 
101
 
    /* restore original memory content */
102
 
    memcpy((void *)BUILD_SMM_INIT_ADDR, (void *)BUILD_SMM_ADDR, BUILD_SMM_SIZE);
103
 
 
104
 
    /* copy the SMM code */
105
 
    memcpy((void *)BUILD_SMM_ADDR, &smm_code_start
106
 
           , &smm_code_end - &smm_code_start);
107
 
    wbinvd();
108
 
}
109
 
 
110
 
#define I440FX_SMRAM    0x72
111
 
#define PIIX_DEVACTB    0x58
112
 
#define PIIX_APMC_EN    (1 << 25)
113
 
 
114
 
// This code is hardcoded for PIIX4 Power Management device.
115
 
static void piix4_apmc_smm_setup(int isabdf, int i440_bdf)
116
 
{
117
 
    /* check if SMM init is already done */
118
 
    u32 value = pci_config_readl(isabdf, PIIX_DEVACTB);
119
 
    if (value & PIIX_APMC_EN)
120
 
        return;
121
 
 
122
 
    /* enable the SMM memory window */
123
 
    pci_config_writeb(i440_bdf, I440FX_SMRAM, 0x02 | 0x48);
124
 
 
125
 
    smm_save_and_copy();
126
 
 
127
 
    /* enable SMI generation when writing to the APMC register */
128
 
    pci_config_writel(isabdf, PIIX_DEVACTB, value | PIIX_APMC_EN);
129
 
 
130
 
    smm_relocate_and_restore();
131
 
 
132
 
    /* close the SMM memory window and enable normal SMM */
133
 
    pci_config_writeb(i440_bdf, I440FX_SMRAM, 0x02 | 0x08);
134
 
}
135
 
 
136
 
/* PCI_VENDOR_ID_INTEL && PCI_DEVICE_ID_INTEL_ICH9_LPC */
137
 
void ich9_lpc_apmc_smm_setup(int isabdf, int mch_bdf)
138
 
{
139
 
    /* check if SMM init is already done */
140
 
    u32 value = inl(PORT_ACPI_PM_BASE + ICH9_PMIO_SMI_EN);
141
 
    if (value & ICH9_PMIO_SMI_EN_APMC_EN)
142
 
        return;
143
 
 
144
 
    /* enable the SMM memory window */
145
 
    pci_config_writeb(mch_bdf, Q35_HOST_BRIDGE_SMRAM, 0x02 | 0x48);
146
 
 
147
 
    smm_save_and_copy();
148
 
 
149
 
    /* enable SMI generation when writing to the APMC register */
150
 
    outl(value | ICH9_PMIO_SMI_EN_APMC_EN,
151
 
         PORT_ACPI_PM_BASE + ICH9_PMIO_SMI_EN);
152
 
 
153
 
    smm_relocate_and_restore();
154
 
 
155
 
    /* close the SMM memory window and enable normal SMM */
156
 
    pci_config_writeb(mch_bdf, Q35_HOST_BRIDGE_SMRAM, 0x02 | 0x08);
157
 
}
158
 
 
159
 
static int SMMISADeviceBDF = -1, SMMPMDeviceBDF = -1;
160
 
 
161
 
void
162
 
smm_device_setup(void)
163
 
{
164
 
    if (!CONFIG_USE_SMM)
165
 
        return;
166
 
 
167
 
    struct pci_device *isapci, *pmpci;
168
 
    isapci = pci_find_device(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82371AB_3);
169
 
    pmpci = pci_find_device(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82441);
170
 
    if (isapci && pmpci) {
171
 
        SMMISADeviceBDF = isapci->bdf;
172
 
        SMMPMDeviceBDF = pmpci->bdf;
173
 
        return;
174
 
    }
175
 
    isapci = pci_find_device(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_ICH9_LPC);
176
 
    pmpci = pci_find_device(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_Q35_MCH);
177
 
    if (isapci && pmpci) {
178
 
        SMMISADeviceBDF = isapci->bdf;
179
 
        SMMPMDeviceBDF = pmpci->bdf;
180
 
    }
181
 
}
182
 
 
183
 
void
184
 
smm_setup(void)
185
 
{
186
 
    if (!CONFIG_USE_SMM || SMMISADeviceBDF < 0)
187
 
        return;
188
 
 
189
 
    dprintf(3, "init smm\n");
190
 
    u16 device = pci_config_readw(SMMISADeviceBDF, PCI_DEVICE_ID);
191
 
    if (device == PCI_DEVICE_ID_INTEL_82371AB_3)
192
 
        piix4_apmc_smm_setup(SMMISADeviceBDF, SMMPMDeviceBDF);
193
 
    else
194
 
        ich9_lpc_apmc_smm_setup(SMMISADeviceBDF, SMMPMDeviceBDF);
195
 
}