Kevin O'Connor | f7ba6d7 | 2008-07-04 05:05:54 -0400 | [diff] [blame] | 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 | |
Kevin O'Connor | be19cdc | 2008-11-09 15:33:47 -0500 | [diff] [blame] | 8 | #include "pci.h" // pci_config_writel |
Kevin O'Connor | f7ba6d7 | 2008-07-04 05:05:54 -0400 | [diff] [blame] | 9 | #include "util.h" // wbinvd |
Kevin O'Connor | 9521e26 | 2008-07-04 13:04:29 -0400 | [diff] [blame] | 10 | #include "config.h" // CONFIG_* |
| 11 | #include "ioport.h" // outb |
Kevin O'Connor | 2ed2f58 | 2008-11-08 15:53:36 -0500 | [diff] [blame] | 12 | #include "pci_ids.h" // PCI_VENDOR_ID_INTEL |
Kevin O'Connor | f7ba6d7 | 2008-07-04 05:05:54 -0400 | [diff] [blame] | 13 | |
Kevin O'Connor | 4096702 | 2008-07-21 22:23:05 -0400 | [diff] [blame] | 14 | #if CONFIG_USE_SMM |
Kevin O'Connor | f7ba6d7 | 2008-07-04 05:05:54 -0400 | [diff] [blame] | 15 | asm( |
| 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" |
Kevin O'Connor | e682cbc | 2008-12-06 23:11:56 -0500 | [diff] [blame] | 24 | " mov $" __stringify(BUILD_SMM_INIT_ADDR) " + 0x7efc, %ebx\n" |
Kevin O'Connor | f7ba6d7 | 2008-07-04 05:05:54 -0400 | [diff] [blame] | 25 | " addr32 mov (%ebx), %al\n" /* revision ID to see if x86_64 or x86 */ |
| 26 | " cmp $0x64, %al\n" |
| 27 | " je 1f\n" |
Kevin O'Connor | e682cbc | 2008-12-06 23:11:56 -0500 | [diff] [blame] | 28 | " mov $" __stringify(BUILD_SMM_INIT_ADDR) " + 0x7ef8, %ebx\n" |
Kevin O'Connor | f7ba6d7 | 2008-07-04 05:05:54 -0400 | [diff] [blame] | 29 | " jmp 2f\n" |
| 30 | "1:\n" |
Kevin O'Connor | e682cbc | 2008-12-06 23:11:56 -0500 | [diff] [blame] | 31 | " mov $" __stringify(BUILD_SMM_INIT_ADDR) " + 0x7f00, %ebx\n" |
Kevin O'Connor | f7ba6d7 | 2008-07-04 05:05:54 -0400 | [diff] [blame] | 32 | "2:\n" |
Kevin O'Connor | e682cbc | 2008-12-06 23:11:56 -0500 | [diff] [blame] | 33 | " movl $" __stringify(BUILD_SMM_ADDR) " - 0x8000, %eax\n" |
Kevin O'Connor | f7ba6d7 | 2008-07-04 05:05:54 -0400 | [diff] [blame] | 34 | " addr32 movl %eax, (%ebx)\n" |
| 35 | /* indicate to the BIOS that the SMM code was executed */ |
| 36 | " mov $0x00, %al\n" |
Kevin O'Connor | e682cbc | 2008-12-06 23:11:56 -0500 | [diff] [blame] | 37 | " movw $" __stringify(PORT_SMI_STATUS) ", %dx\n" |
Kevin O'Connor | f7ba6d7 | 2008-07-04 05:05:54 -0400 | [diff] [blame] | 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" |
Kevin O'Connor | e682cbc | 2008-12-06 23:11:56 -0500 | [diff] [blame] | 44 | " movw $" __stringify(PORT_SMI_CMD) ", %dx\n" |
Kevin O'Connor | f7ba6d7 | 2008-07-04 05:05:54 -0400 | [diff] [blame] | 45 | " inb %dx, %al\n" |
| 46 | " cmp $0xf0, %al\n" |
| 47 | " jne 1f\n" |
| 48 | |
| 49 | /* ACPI disable */ |
Kevin O'Connor | e682cbc | 2008-12-06 23:11:56 -0500 | [diff] [blame] | 50 | " mov $" __stringify(PORT_ACPI_PM_BASE) " + 0x04, %dx\n" /* PMCNTRL */ |
Kevin O'Connor | f7ba6d7 | 2008-07-04 05:05:54 -0400 | [diff] [blame] | 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 */ |
Kevin O'Connor | e682cbc | 2008-12-06 23:11:56 -0500 | [diff] [blame] | 62 | " mov $" __stringify(PORT_ACPI_PM_BASE) " + 0x04, %dx\n" /* PMCNTRL */ |
Kevin O'Connor | f7ba6d7 | 2008-07-04 05:05:54 -0400 | [diff] [blame] | 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'Connor | 4096702 | 2008-07-21 22:23:05 -0400 | [diff] [blame] | 72 | #endif |
Kevin O'Connor | f7ba6d7 | 2008-07-04 05:05:54 -0400 | [diff] [blame] | 73 | |
| 74 | extern u8 smm_relocation_start, smm_relocation_end; |
| 75 | extern u8 smm_code_start, smm_code_end; |
| 76 | |
| 77 | void |
| 78 | smm_init() |
| 79 | { |
Kevin O'Connor | 4bbd3b3 | 2008-12-10 21:01:00 -0500 | [diff] [blame^] | 80 | if (CONFIG_COREBOOT) |
| 81 | // SMM only supported on emulators. |
| 82 | return; |
Kevin O'Connor | f7ba6d7 | 2008-07-04 05:05:54 -0400 | [diff] [blame] | 83 | if (!CONFIG_USE_SMM) |
| 84 | return; |
| 85 | |
Kevin O'Connor | 7b49cd9 | 2008-11-08 10:35:26 -0500 | [diff] [blame] | 86 | dprintf(3, "init smm\n"); |
| 87 | |
Kevin O'Connor | f7ba6d7 | 2008-07-04 05:05:54 -0400 | [diff] [blame] | 88 | // This code is hardcoded for PIIX4 Power Management device. |
Kevin O'Connor | 4132e02 | 2008-12-04 19:39:10 -0500 | [diff] [blame] | 89 | int bdf = pci_find_device(PCI_VENDOR_ID_INTEL |
| 90 | , PCI_DEVICE_ID_INTEL_82371AB_3); |
Kevin O'Connor | be19cdc | 2008-11-09 15:33:47 -0500 | [diff] [blame] | 91 | if (bdf < 0) |
Kevin O'Connor | f7ba6d7 | 2008-07-04 05:05:54 -0400 | [diff] [blame] | 92 | // Device not found |
| 93 | return; |
Kevin O'Connor | 4132e02 | 2008-12-04 19:39:10 -0500 | [diff] [blame] | 94 | int i440_bdf = pci_find_device(PCI_VENDOR_ID_INTEL |
| 95 | , PCI_DEVICE_ID_INTEL_82441); |
Kevin O'Connor | be19cdc | 2008-11-09 15:33:47 -0500 | [diff] [blame] | 96 | if (i440_bdf < 0) |
Kevin O'Connor | f7ba6d7 | 2008-07-04 05:05:54 -0400 | [diff] [blame] | 97 | return; |
| 98 | |
| 99 | /* check if SMM init is already done */ |
Kevin O'Connor | be19cdc | 2008-11-09 15:33:47 -0500 | [diff] [blame] | 100 | u32 value = pci_config_readl(bdf, 0x58); |
Kevin O'Connor | f7ba6d7 | 2008-07-04 05:05:54 -0400 | [diff] [blame] | 101 | if (value & (1 << 25)) |
| 102 | return; |
| 103 | |
Kevin O'Connor | e682cbc | 2008-12-06 23:11:56 -0500 | [diff] [blame] | 104 | /* enable the SMM memory window */ |
| 105 | pci_config_writeb(i440_bdf, 0x72, 0x02 | 0x48); |
| 106 | |
| 107 | /* save original memory content */ |
| 108 | memcpy((void *)BUILD_SMM_ADDR, (void *)BUILD_SMM_INIT_ADDR, BUILD_SMM_SIZE); |
| 109 | |
Kevin O'Connor | f7ba6d7 | 2008-07-04 05:05:54 -0400 | [diff] [blame] | 110 | /* copy the SMM relocation code */ |
Kevin O'Connor | e682cbc | 2008-12-06 23:11:56 -0500 | [diff] [blame] | 111 | memcpy((void *)BUILD_SMM_INIT_ADDR, &smm_relocation_start, |
Kevin O'Connor | f7ba6d7 | 2008-07-04 05:05:54 -0400 | [diff] [blame] | 112 | &smm_relocation_end - &smm_relocation_start); |
| 113 | |
| 114 | /* enable SMI generation when writing to the APMC register */ |
Kevin O'Connor | be19cdc | 2008-11-09 15:33:47 -0500 | [diff] [blame] | 115 | pci_config_writel(bdf, 0x58, value | (1 << 25)); |
Kevin O'Connor | f7ba6d7 | 2008-07-04 05:05:54 -0400 | [diff] [blame] | 116 | |
| 117 | /* init APM status port */ |
Kevin O'Connor | e682cbc | 2008-12-06 23:11:56 -0500 | [diff] [blame] | 118 | outb(0x01, PORT_SMI_STATUS); |
Kevin O'Connor | f7ba6d7 | 2008-07-04 05:05:54 -0400 | [diff] [blame] | 119 | |
| 120 | /* raise an SMI interrupt */ |
Kevin O'Connor | e682cbc | 2008-12-06 23:11:56 -0500 | [diff] [blame] | 121 | outb(0x00, PORT_SMI_CMD); |
Kevin O'Connor | f7ba6d7 | 2008-07-04 05:05:54 -0400 | [diff] [blame] | 122 | |
| 123 | /* wait until SMM code executed */ |
Kevin O'Connor | e682cbc | 2008-12-06 23:11:56 -0500 | [diff] [blame] | 124 | while (inb(PORT_SMI_STATUS) != 0x00) |
Kevin O'Connor | f7ba6d7 | 2008-07-04 05:05:54 -0400 | [diff] [blame] | 125 | ; |
| 126 | |
Kevin O'Connor | e682cbc | 2008-12-06 23:11:56 -0500 | [diff] [blame] | 127 | /* restore original memory content */ |
| 128 | memcpy((void *)BUILD_SMM_INIT_ADDR, (void *)BUILD_SMM_ADDR, BUILD_SMM_SIZE); |
Kevin O'Connor | f7ba6d7 | 2008-07-04 05:05:54 -0400 | [diff] [blame] | 129 | |
| 130 | /* copy the SMM code */ |
Kevin O'Connor | e682cbc | 2008-12-06 23:11:56 -0500 | [diff] [blame] | 131 | memcpy((void *)BUILD_SMM_ADDR, &smm_code_start |
| 132 | , &smm_code_end - &smm_code_start); |
Kevin O'Connor | f7ba6d7 | 2008-07-04 05:05:54 -0400 | [diff] [blame] | 133 | wbinvd(); |
| 134 | |
| 135 | /* close the SMM memory window and enable normal SMM */ |
Kevin O'Connor | be19cdc | 2008-11-09 15:33:47 -0500 | [diff] [blame] | 136 | pci_config_writeb(i440_bdf, 0x72, 0x02 | 0x08); |
Kevin O'Connor | f7ba6d7 | 2008-07-04 05:05:54 -0400 | [diff] [blame] | 137 | } |