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 | |
| 8 | #include "pci.h" // PCIDevice |
| 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 | f7ba6d7 | 2008-07-04 05:05:54 -0400 | [diff] [blame] | 12 | |
Kevin O'Connor | 4096702 | 2008-07-21 22:23:05 -0400 | [diff] [blame] | 13 | #if CONFIG_USE_SMM |
Kevin O'Connor | f7ba6d7 | 2008-07-04 05:05:54 -0400 | [diff] [blame] | 14 | asm( |
| 15 | ".global smm_relocation_start\n" |
| 16 | ".global smm_relocation_end\n" |
| 17 | ".global smm_code_start\n" |
| 18 | ".global smm_code_end\n" |
| 19 | " .code16\n" |
| 20 | |
| 21 | /* code to relocate SMBASE to 0xa0000 */ |
| 22 | "smm_relocation_start:\n" |
| 23 | " mov $0x38000 + 0x7efc, %ebx\n" |
| 24 | " addr32 mov (%ebx), %al\n" /* revision ID to see if x86_64 or x86 */ |
| 25 | " cmp $0x64, %al\n" |
| 26 | " je 1f\n" |
| 27 | " mov $0x38000 + 0x7ef8, %ebx\n" |
| 28 | " jmp 2f\n" |
| 29 | "1:\n" |
| 30 | " mov $0x38000 + 0x7f00, %ebx\n" |
| 31 | "2:\n" |
| 32 | " movl $0xa0000, %eax\n" |
| 33 | " addr32 movl %eax, (%ebx)\n" |
| 34 | /* indicate to the BIOS that the SMM code was executed */ |
| 35 | " mov $0x00, %al\n" |
| 36 | " movw $0xb3, %dx\n" |
| 37 | " outb %al, %dx\n" |
| 38 | " rsm\n" |
| 39 | "smm_relocation_end:\n" |
| 40 | |
| 41 | /* minimal SMM code to enable or disable ACPI */ |
| 42 | "smm_code_start:\n" |
| 43 | " movw $0xb2, %dx\n" |
| 44 | " inb %dx, %al\n" |
| 45 | " cmp $0xf0, %al\n" |
| 46 | " jne 1f\n" |
| 47 | |
| 48 | /* ACPI disable */ |
| 49 | " mov $" __stringify(BUILD_PM_IO_BASE) " + 0x04, %dx\n" /* PMCNTRL */ |
| 50 | " inw %dx, %ax\n" |
| 51 | " andw $~1, %ax\n" |
| 52 | " outw %ax, %dx\n" |
| 53 | |
| 54 | " jmp 2f\n" |
| 55 | |
| 56 | "1:\n" |
| 57 | " cmp $0xf1, %al\n" |
| 58 | " jne 2f\n" |
| 59 | |
| 60 | /* ACPI enable */ |
| 61 | " mov $" __stringify(BUILD_PM_IO_BASE) " + 0x04, %dx\n" /* PMCNTRL */ |
| 62 | " inw %dx, %ax\n" |
| 63 | " orw $1, %ax\n" |
| 64 | " outw %ax, %dx\n" |
| 65 | |
| 66 | "2:\n" |
| 67 | " rsm\n" |
| 68 | "smm_code_end:\n" |
| 69 | " .code32\n" |
| 70 | ); |
Kevin O'Connor | 4096702 | 2008-07-21 22:23:05 -0400 | [diff] [blame] | 71 | #endif |
Kevin O'Connor | f7ba6d7 | 2008-07-04 05:05:54 -0400 | [diff] [blame] | 72 | |
| 73 | extern u8 smm_relocation_start, smm_relocation_end; |
| 74 | extern u8 smm_code_start, smm_code_end; |
| 75 | |
| 76 | void |
| 77 | smm_init() |
| 78 | { |
| 79 | if (!CONFIG_USE_SMM) |
| 80 | return; |
| 81 | |
Kevin O'Connor | 7b49cd9 | 2008-11-08 10:35:26 -0500 | [diff] [blame^] | 82 | dprintf(3, "init smm\n"); |
| 83 | |
Kevin O'Connor | f7ba6d7 | 2008-07-04 05:05:54 -0400 | [diff] [blame] | 84 | // This code is hardcoded for PIIX4 Power Management device. |
| 85 | PCIDevice i440_pcidev, d; |
Kevin O'Connor | 415c2dc | 2008-10-25 14:35:59 -0400 | [diff] [blame] | 86 | int ret = pci_find_device(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82371AB_3 |
| 87 | , 0, &d); |
Kevin O'Connor | f7ba6d7 | 2008-07-04 05:05:54 -0400 | [diff] [blame] | 88 | if (ret) |
| 89 | // Device not found |
| 90 | return; |
Kevin O'Connor | 415c2dc | 2008-10-25 14:35:59 -0400 | [diff] [blame] | 91 | ret = pci_find_device(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82441 |
| 92 | , 0, &i440_pcidev); |
Kevin O'Connor | f7ba6d7 | 2008-07-04 05:05:54 -0400 | [diff] [blame] | 93 | if (ret) |
| 94 | return; |
| 95 | |
| 96 | /* check if SMM init is already done */ |
| 97 | u32 value = pci_config_readl(d, 0x58); |
| 98 | 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 */ |
| 106 | pci_config_writel(d, 0x58, value | (1 << 25)); |
| 107 | |
| 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 */ |
| 119 | pci_config_writeb(i440_pcidev, 0x72, 0x02 | 0x48); |
| 120 | |
| 121 | /* copy the SMM code */ |
| 122 | memcpy((void *)0xa8000, &smm_code_start, |
| 123 | &smm_code_end - &smm_code_start); |
| 124 | wbinvd(); |
| 125 | |
| 126 | /* close the SMM memory window and enable normal SMM */ |
| 127 | pci_config_writeb(i440_pcidev, 0x72, 0x02 | 0x08); |
| 128 | } |