Kevin O'Connor | f7ba6d7 | 2008-07-04 05:05:54 -0400 | [diff] [blame] | 1 | // System Management Mode support (on emulators) |
| 2 | // |
Kevin O'Connor | f4c511c | 2014-04-07 19:49:12 -0400 | [diff] [blame] | 3 | // Copyright (C) 2008-2014 Kevin O'Connor <kevin@koconnor.net> |
Kevin O'Connor | f7ba6d7 | 2008-07-04 05:05:54 -0400 | [diff] [blame] | 4 | // Copyright (C) 2006 Fabrice Bellard |
| 5 | // |
Kevin O'Connor | b1b7c2a | 2009-01-15 20:52:58 -0500 | [diff] [blame] | 6 | // This file may be distributed under the terms of the GNU LGPLv3 license. |
Kevin O'Connor | f7ba6d7 | 2008-07-04 05:05:54 -0400 | [diff] [blame] | 7 | |
Kevin O'Connor | 9521e26 | 2008-07-04 13:04:29 -0400 | [diff] [blame] | 8 | #include "config.h" // CONFIG_* |
Isaku Yamahata | 72a590e | 2012-11-28 10:17:33 +0100 | [diff] [blame] | 9 | #include "dev-q35.h" |
Paolo Bonzini | 40d0312 | 2014-05-15 13:22:26 +0200 | [diff] [blame] | 10 | #include "dev-piix.h" |
Kevin O'Connor | 2d2fa31 | 2013-09-14 21:55:26 -0400 | [diff] [blame] | 11 | #include "hw/pci.h" // pci_config_writel |
| 12 | #include "hw/pci_ids.h" // PCI_VENDOR_ID_INTEL |
| 13 | #include "hw/pci_regs.h" // PCI_DEVICE_ID |
Kevin O'Connor | 2d2fa31 | 2013-09-14 21:55:26 -0400 | [diff] [blame] | 14 | #include "output.h" // dprintf |
Kevin O'Connor | 4ade523 | 2013-09-18 21:41:48 -0400 | [diff] [blame] | 15 | #include "paravirt.h" // PORT_SMI_STATUS |
Kevin O'Connor | fa9c66a | 2013-09-14 19:10:40 -0400 | [diff] [blame] | 16 | #include "string.h" // memcpy |
Kevin O'Connor | 2d2fa31 | 2013-09-14 21:55:26 -0400 | [diff] [blame] | 17 | #include "util.h" // smm_setup |
Kevin O'Connor | b9c6a96 | 2013-09-14 13:01:30 -0400 | [diff] [blame] | 18 | #include "x86.h" // wbinvd |
Kevin O'Connor | f7ba6d7 | 2008-07-04 05:05:54 -0400 | [diff] [blame] | 19 | |
Kevin O'Connor | 31bcda2 | 2014-05-28 13:33:50 -0400 | [diff] [blame^] | 20 | #define SMM_REV_I32 0x00020000 |
| 21 | #define SMM_REV_I64 0x00020064 |
| 22 | |
| 23 | struct smm_state { |
| 24 | union { |
| 25 | struct { |
| 26 | u8 pad_000[0xf8]; |
| 27 | u32 smm_base; |
| 28 | u32 smm_rev; |
| 29 | u8 pad_100[0xd0]; |
| 30 | u32 eax, ecx, edx, ebx, esp, ebp, esi, edi, eip, eflags; |
| 31 | u8 pad_1f8[0x08]; |
| 32 | } i32; |
| 33 | struct { |
| 34 | u8 pad_000[0xfc]; |
| 35 | u32 smm_rev; |
| 36 | u32 smm_base; |
| 37 | u8 pad_104[0x6c]; |
| 38 | u64 rflags, rip, r15, r14, r13, r12, r11, r10, r9, r8; |
| 39 | u64 rdi, rsi, rbp, rsp, rbx, rdx, rcx, rax; |
| 40 | } i64; |
| 41 | }; |
| 42 | }; |
| 43 | |
| 44 | struct smm_layout { |
| 45 | u8 stack[0x8000]; |
| 46 | u64 codeentry; |
| 47 | u8 pad_8008[0x7df8]; |
| 48 | struct smm_state cpu; |
| 49 | }; |
| 50 | |
Kevin O'Connor | f4c511c | 2014-04-07 19:49:12 -0400 | [diff] [blame] | 51 | void VISIBLE32FLAT |
| 52 | handle_smi(u16 cs) |
| 53 | { |
| 54 | u8 cmd = inb(PORT_SMI_CMD); |
Kevin O'Connor | 31bcda2 | 2014-05-28 13:33:50 -0400 | [diff] [blame^] | 55 | struct smm_layout *smm = MAKE_FLATPTR(cs, 0); |
| 56 | dprintf(DEBUG_HDL_smi, "handle_smi cmd=%x smbase=%p\n", cmd, smm); |
Paolo Bonzini | 2126994 | 2014-05-15 13:22:29 +0200 | [diff] [blame] | 57 | |
Kevin O'Connor | 31bcda2 | 2014-05-28 13:33:50 -0400 | [diff] [blame^] | 58 | if (smm == (void*)BUILD_SMM_INIT_ADDR) { |
Kevin O'Connor | f4c511c | 2014-04-07 19:49:12 -0400 | [diff] [blame] | 59 | // relocate SMBASE to 0xa0000 |
Kevin O'Connor | 31bcda2 | 2014-05-28 13:33:50 -0400 | [diff] [blame^] | 60 | if (smm->cpu.i32.smm_rev == SMM_REV_I32) { |
| 61 | smm->cpu.i32.smm_base = BUILD_SMM_ADDR; |
| 62 | } else if (smm->cpu.i64.smm_rev == SMM_REV_I64) { |
| 63 | smm->cpu.i64.smm_base = BUILD_SMM_ADDR; |
| 64 | } else { |
| 65 | warn_internalerror(); |
| 66 | return; |
| 67 | } |
Kevin O'Connor | f4c511c | 2014-04-07 19:49:12 -0400 | [diff] [blame] | 68 | // indicate to smm_relocate_and_restore() that the SMM code was executed |
| 69 | outb(0x00, PORT_SMI_STATUS); |
| 70 | return; |
| 71 | } |
| 72 | } |
Kevin O'Connor | f7ba6d7 | 2008-07-04 05:05:54 -0400 | [diff] [blame] | 73 | |
Kevin O'Connor | f4c511c | 2014-04-07 19:49:12 -0400 | [diff] [blame] | 74 | extern void entry_smi(void); |
| 75 | // movw %cs, %ax; ljmpw $SEG_BIOS, $(entry_smi - BUILD_BIOS_ADDR) |
| 76 | #define SMI_INSN (0xeac88c | ((u64)SEG_BIOS<<40) \ |
| 77 | | ((u64)((u32)entry_smi - BUILD_BIOS_ADDR) << 24)) |
Kevin O'Connor | f7ba6d7 | 2008-07-04 05:05:54 -0400 | [diff] [blame] | 78 | |
Kevin O'Connor | 6e4583c | 2011-06-19 10:09:26 -0400 | [diff] [blame] | 79 | static void |
Isaku Yamahata | aec19c9 | 2010-07-20 16:50:46 +0900 | [diff] [blame] | 80 | smm_save_and_copy(void) |
Kevin O'Connor | f7ba6d7 | 2008-07-04 05:05:54 -0400 | [diff] [blame] | 81 | { |
Kevin O'Connor | f4c511c | 2014-04-07 19:49:12 -0400 | [diff] [blame] | 82 | // save original memory content |
Kevin O'Connor | 31bcda2 | 2014-05-28 13:33:50 -0400 | [diff] [blame^] | 83 | struct smm_layout *initsmm = (void*)BUILD_SMM_INIT_ADDR; |
| 84 | struct smm_layout *smm = (void*)BUILD_SMM_ADDR; |
| 85 | memcpy(&smm->cpu, &initsmm->cpu, sizeof(smm->cpu)); |
| 86 | memcpy(&smm->codeentry, &initsmm->codeentry, sizeof(smm->codeentry)); |
Kevin O'Connor | e682cbc | 2008-12-06 23:11:56 -0500 | [diff] [blame] | 87 | |
Kevin O'Connor | f4c511c | 2014-04-07 19:49:12 -0400 | [diff] [blame] | 88 | // Setup code entry point. |
Kevin O'Connor | 31bcda2 | 2014-05-28 13:33:50 -0400 | [diff] [blame^] | 89 | initsmm->codeentry = SMI_INSN; |
Isaku Yamahata | aec19c9 | 2010-07-20 16:50:46 +0900 | [diff] [blame] | 90 | } |
Kevin O'Connor | f7ba6d7 | 2008-07-04 05:05:54 -0400 | [diff] [blame] | 91 | |
Kevin O'Connor | 6e4583c | 2011-06-19 10:09:26 -0400 | [diff] [blame] | 92 | static void |
Isaku Yamahata | aec19c9 | 2010-07-20 16:50:46 +0900 | [diff] [blame] | 93 | smm_relocate_and_restore(void) |
| 94 | { |
Kevin O'Connor | f7ba6d7 | 2008-07-04 05:05:54 -0400 | [diff] [blame] | 95 | /* init APM status port */ |
Kevin O'Connor | e682cbc | 2008-12-06 23:11:56 -0500 | [diff] [blame] | 96 | outb(0x01, PORT_SMI_STATUS); |
Kevin O'Connor | f7ba6d7 | 2008-07-04 05:05:54 -0400 | [diff] [blame] | 97 | |
| 98 | /* raise an SMI interrupt */ |
Kevin O'Connor | e682cbc | 2008-12-06 23:11:56 -0500 | [diff] [blame] | 99 | outb(0x00, PORT_SMI_CMD); |
Kevin O'Connor | f7ba6d7 | 2008-07-04 05:05:54 -0400 | [diff] [blame] | 100 | |
| 101 | /* wait until SMM code executed */ |
Kevin O'Connor | e682cbc | 2008-12-06 23:11:56 -0500 | [diff] [blame] | 102 | while (inb(PORT_SMI_STATUS) != 0x00) |
Kevin O'Connor | f7ba6d7 | 2008-07-04 05:05:54 -0400 | [diff] [blame] | 103 | ; |
| 104 | |
Kevin O'Connor | e682cbc | 2008-12-06 23:11:56 -0500 | [diff] [blame] | 105 | /* restore original memory content */ |
Kevin O'Connor | 31bcda2 | 2014-05-28 13:33:50 -0400 | [diff] [blame^] | 106 | struct smm_layout *initsmm = (void*)BUILD_SMM_INIT_ADDR; |
| 107 | struct smm_layout *smm = (void*)BUILD_SMM_ADDR; |
| 108 | memcpy(&initsmm->cpu, &smm->cpu, sizeof(initsmm->cpu)); |
| 109 | memcpy(&initsmm->codeentry, &smm->codeentry, sizeof(initsmm->codeentry)); |
Kevin O'Connor | f7ba6d7 | 2008-07-04 05:05:54 -0400 | [diff] [blame] | 110 | |
Kevin O'Connor | f4c511c | 2014-04-07 19:49:12 -0400 | [diff] [blame] | 111 | // Setup code entry point. |
Kevin O'Connor | 31bcda2 | 2014-05-28 13:33:50 -0400 | [diff] [blame^] | 112 | smm->codeentry = SMI_INSN; |
Kevin O'Connor | f7ba6d7 | 2008-07-04 05:05:54 -0400 | [diff] [blame] | 113 | wbinvd(); |
Isaku Yamahata | aec19c9 | 2010-07-20 16:50:46 +0900 | [diff] [blame] | 114 | } |
Kevin O'Connor | f7ba6d7 | 2008-07-04 05:05:54 -0400 | [diff] [blame] | 115 | |
Kevin O'Connor | 6e4583c | 2011-06-19 10:09:26 -0400 | [diff] [blame] | 116 | // This code is hardcoded for PIIX4 Power Management device. |
Kevin O'Connor | cdbac7f | 2013-03-08 19:33:39 -0500 | [diff] [blame] | 117 | static void piix4_apmc_smm_setup(int isabdf, int i440_bdf) |
Kevin O'Connor | 6e4583c | 2011-06-19 10:09:26 -0400 | [diff] [blame] | 118 | { |
Kevin O'Connor | 6e4583c | 2011-06-19 10:09:26 -0400 | [diff] [blame] | 119 | /* check if SMM init is already done */ |
Kevin O'Connor | cdbac7f | 2013-03-08 19:33:39 -0500 | [diff] [blame] | 120 | u32 value = pci_config_readl(isabdf, PIIX_DEVACTB); |
Paolo Bonzini | 40d0312 | 2014-05-15 13:22:26 +0200 | [diff] [blame] | 121 | if (value & PIIX_DEVACTB_APMC_EN) |
Kevin O'Connor | 6e4583c | 2011-06-19 10:09:26 -0400 | [diff] [blame] | 122 | return; |
| 123 | |
| 124 | /* enable the SMM memory window */ |
Kevin O'Connor | cdbac7f | 2013-03-08 19:33:39 -0500 | [diff] [blame] | 125 | pci_config_writeb(i440_bdf, I440FX_SMRAM, 0x02 | 0x48); |
Kevin O'Connor | 6e4583c | 2011-06-19 10:09:26 -0400 | [diff] [blame] | 126 | |
| 127 | smm_save_and_copy(); |
| 128 | |
| 129 | /* enable SMI generation when writing to the APMC register */ |
Paolo Bonzini | 40d0312 | 2014-05-15 13:22:26 +0200 | [diff] [blame] | 130 | pci_config_writel(isabdf, PIIX_DEVACTB, value | PIIX_DEVACTB_APMC_EN); |
Kevin O'Connor | 6e4583c | 2011-06-19 10:09:26 -0400 | [diff] [blame] | 131 | |
Paolo Bonzini | 457ba42 | 2014-05-15 13:22:28 +0200 | [diff] [blame] | 132 | /* enable SMI generation */ |
| 133 | value = inl(acpi_pm_base + PIIX_PMIO_GLBCTL); |
| 134 | outl(acpi_pm_base + PIIX_PMIO_GLBCTL, |
| 135 | value | PIIX_PMIO_GLBCTL_SMI_EN); |
| 136 | |
Kevin O'Connor | 6e4583c | 2011-06-19 10:09:26 -0400 | [diff] [blame] | 137 | smm_relocate_and_restore(); |
| 138 | |
| 139 | /* close the SMM memory window and enable normal SMM */ |
Kevin O'Connor | cdbac7f | 2013-03-08 19:33:39 -0500 | [diff] [blame] | 140 | pci_config_writeb(i440_bdf, I440FX_SMRAM, 0x02 | 0x08); |
Kevin O'Connor | 6e4583c | 2011-06-19 10:09:26 -0400 | [diff] [blame] | 141 | } |
| 142 | |
Isaku Yamahata | 72a590e | 2012-11-28 10:17:33 +0100 | [diff] [blame] | 143 | /* PCI_VENDOR_ID_INTEL && PCI_DEVICE_ID_INTEL_ICH9_LPC */ |
Kevin O'Connor | cdbac7f | 2013-03-08 19:33:39 -0500 | [diff] [blame] | 144 | void ich9_lpc_apmc_smm_setup(int isabdf, int mch_bdf) |
Isaku Yamahata | 72a590e | 2012-11-28 10:17:33 +0100 | [diff] [blame] | 145 | { |
Isaku Yamahata | 72a590e | 2012-11-28 10:17:33 +0100 | [diff] [blame] | 146 | /* check if SMM init is already done */ |
Gerd Hoffmann | a217de9 | 2014-05-13 14:01:22 +0200 | [diff] [blame] | 147 | u32 value = inl(acpi_pm_base + ICH9_PMIO_SMI_EN); |
Isaku Yamahata | 72a590e | 2012-11-28 10:17:33 +0100 | [diff] [blame] | 148 | if (value & ICH9_PMIO_SMI_EN_APMC_EN) |
| 149 | return; |
| 150 | |
| 151 | /* enable the SMM memory window */ |
| 152 | pci_config_writeb(mch_bdf, Q35_HOST_BRIDGE_SMRAM, 0x02 | 0x48); |
| 153 | |
| 154 | smm_save_and_copy(); |
| 155 | |
| 156 | /* enable SMI generation when writing to the APMC register */ |
Paolo Bonzini | 457ba42 | 2014-05-15 13:22:28 +0200 | [diff] [blame] | 157 | outl(value | ICH9_PMIO_SMI_EN_APMC_EN | ICH9_PMIO_SMI_EN_GLB_SMI_EN, |
Gerd Hoffmann | a217de9 | 2014-05-13 14:01:22 +0200 | [diff] [blame] | 158 | acpi_pm_base + ICH9_PMIO_SMI_EN); |
Isaku Yamahata | 72a590e | 2012-11-28 10:17:33 +0100 | [diff] [blame] | 159 | |
Paolo Bonzini | 457ba42 | 2014-05-15 13:22:28 +0200 | [diff] [blame] | 160 | /* lock SMI generation */ |
| 161 | value = pci_config_readw(isabdf, ICH9_LPC_GEN_PMCON_1); |
| 162 | pci_config_writel(isabdf, ICH9_LPC_GEN_PMCON_1, |
| 163 | value | ICH9_LPC_GEN_PMCON_1_SMI_LOCK); |
| 164 | |
Isaku Yamahata | 72a590e | 2012-11-28 10:17:33 +0100 | [diff] [blame] | 165 | smm_relocate_and_restore(); |
| 166 | |
| 167 | /* close the SMM memory window and enable normal SMM */ |
| 168 | pci_config_writeb(mch_bdf, Q35_HOST_BRIDGE_SMRAM, 0x02 | 0x08); |
| 169 | } |
| 170 | |
Kevin O'Connor | cdbac7f | 2013-03-08 19:33:39 -0500 | [diff] [blame] | 171 | static int SMMISADeviceBDF = -1, SMMPMDeviceBDF = -1; |
Isaku Yamahata | aec19c9 | 2010-07-20 16:50:46 +0900 | [diff] [blame] | 172 | |
| 173 | void |
Kevin O'Connor | cdbac7f | 2013-03-08 19:33:39 -0500 | [diff] [blame] | 174 | smm_device_setup(void) |
Isaku Yamahata | aec19c9 | 2010-07-20 16:50:46 +0900 | [diff] [blame] | 175 | { |
Kevin O'Connor | a2a86e2 | 2013-02-13 19:35:12 -0500 | [diff] [blame] | 176 | if (!CONFIG_USE_SMM) |
Kevin O'Connor | 028f348 | 2014-04-11 12:08:33 -0400 | [diff] [blame] | 177 | return; |
Isaku Yamahata | aec19c9 | 2010-07-20 16:50:46 +0900 | [diff] [blame] | 178 | |
Kevin O'Connor | cdbac7f | 2013-03-08 19:33:39 -0500 | [diff] [blame] | 179 | struct pci_device *isapci, *pmpci; |
| 180 | isapci = pci_find_device(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82371AB_3); |
| 181 | pmpci = pci_find_device(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82441); |
| 182 | if (isapci && pmpci) { |
| 183 | SMMISADeviceBDF = isapci->bdf; |
| 184 | SMMPMDeviceBDF = pmpci->bdf; |
| 185 | return; |
| 186 | } |
| 187 | isapci = pci_find_device(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_ICH9_LPC); |
| 188 | pmpci = pci_find_device(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_Q35_MCH); |
| 189 | if (isapci && pmpci) { |
| 190 | SMMISADeviceBDF = isapci->bdf; |
| 191 | SMMPMDeviceBDF = pmpci->bdf; |
| 192 | } |
| 193 | } |
| 194 | |
| 195 | void |
| 196 | smm_setup(void) |
| 197 | { |
| 198 | if (!CONFIG_USE_SMM || SMMISADeviceBDF < 0) |
Kevin O'Connor | 028f348 | 2014-04-11 12:08:33 -0400 | [diff] [blame] | 199 | return; |
Kevin O'Connor | cdbac7f | 2013-03-08 19:33:39 -0500 | [diff] [blame] | 200 | |
Isaku Yamahata | aec19c9 | 2010-07-20 16:50:46 +0900 | [diff] [blame] | 201 | dprintf(3, "init smm\n"); |
Kevin O'Connor | cdbac7f | 2013-03-08 19:33:39 -0500 | [diff] [blame] | 202 | u16 device = pci_config_readw(SMMISADeviceBDF, PCI_DEVICE_ID); |
| 203 | if (device == PCI_DEVICE_ID_INTEL_82371AB_3) |
| 204 | piix4_apmc_smm_setup(SMMISADeviceBDF, SMMPMDeviceBDF); |
| 205 | else |
| 206 | ich9_lpc_apmc_smm_setup(SMMISADeviceBDF, SMMPMDeviceBDF); |
Kevin O'Connor | f7ba6d7 | 2008-07-04 05:05:54 -0400 | [diff] [blame] | 207 | } |