blob: 68c92307b229362791c2ad051beb0872029193f7 [file] [log] [blame]
Kevin O'Connorda4a6482008-06-08 13:48:06 -04001// Support for enabling/disabling BIOS ram shadowing.
2//
Kevin O'Connor5bd01de2010-09-13 21:19:27 -04003// Copyright (C) 2008-2010 Kevin O'Connor <kevin@koconnor.net>
Kevin O'Connorda4a6482008-06-08 13:48:06 -04004// Copyright (C) 2006 Fabrice Bellard
5//
Kevin O'Connorb1b7c2a2009-01-15 20:52:58 -05006// This file may be distributed under the terms of the GNU LGPLv3 license.
Kevin O'Connorda4a6482008-06-08 13:48:06 -04007
8#include "util.h" // memcpy
9#include "pci.h" // pci_config_writeb
Kevin O'Connor9521e262008-07-04 13:04:29 -040010#include "config.h" // CONFIG_*
Kevin O'Connor2ed2f582008-11-08 15:53:36 -050011#include "pci_ids.h" // PCI_VENDOR_ID_INTEL
Kevin O'Connorfaf6a4e2011-06-21 21:22:01 -040012#include "pci_regs.h" // PCI_VENDOR_ID
Ian Campbell74c78782011-06-01 11:00:29 +010013#include "xen.h" // usingXen
Kevin O'Connorda4a6482008-06-08 13:48:06 -040014
Kevin O'Connor35284962009-07-24 21:49:26 -040015// On the emulators, the bios at 0xf0000 is also at 0xffff0000
Kevin O'Connor5bd01de2010-09-13 21:19:27 -040016#define BIOS_SRC_OFFSET 0xfff00000
Kevin O'Connor35284962009-07-24 21:49:26 -040017
Kevin O'Connor6e4583c2011-06-19 10:09:26 -040018#define I440FX_PAM0 0x59
19
Kevin O'Connorda4a6482008-06-08 13:48:06 -040020// Enable shadowing and copy bios.
21static void
Isaku Yamahata08328e72010-07-20 16:50:45 +090022__make_bios_writable_intel(u16 bdf, u32 pam0)
Kevin O'Connorda4a6482008-06-08 13:48:06 -040023{
Kevin O'Connore7739302009-07-26 19:16:09 -040024 // Make ram from 0xc0000-0xf0000 writable
25 int clear = 0;
26 int i;
27 for (i=0; i<6; i++) {
Kevin O'Connor5bd01de2010-09-13 21:19:27 -040028 u32 pam = pam0 + 1 + i;
29 int reg = pci_config_readb(bdf, pam);
30 if (CONFIG_OPTIONROMS_DEPLOYED && (reg & 0x11) != 0x11) {
Anthony Liguori034ce4b2009-12-18 12:16:01 +010031 // Need to copy optionroms to work around qemu implementation
32 void *mem = (void*)(BUILD_ROM_START + i * 32*1024);
33 memcpy((void*)BUILD_BIOS_TMP_ADDR, mem, 32*1024);
Isaku Yamahata08328e72010-07-20 16:50:45 +090034 pci_config_writeb(bdf, pam, 0x33);
Anthony Liguori034ce4b2009-12-18 12:16:01 +010035 memcpy(mem, (void*)BUILD_BIOS_TMP_ADDR, 32*1024);
36 clear = 1;
Kevin O'Connore7739302009-07-26 19:16:09 -040037 } else {
Isaku Yamahata08328e72010-07-20 16:50:45 +090038 pci_config_writeb(bdf, pam, 0x33);
Kevin O'Connore7739302009-07-26 19:16:09 -040039 }
40 }
41 if (clear)
42 memset((void*)BUILD_BIOS_TMP_ADDR, 0, 32*1024);
43
Kevin O'Connor5b8f8092009-09-20 19:47:45 -040044 // Make ram from 0xf0000-0x100000 writable
Isaku Yamahata08328e72010-07-20 16:50:45 +090045 int reg = pci_config_readb(bdf, pam0);
46 pci_config_writeb(bdf, pam0, 0x30);
Kevin O'Connor5b8f8092009-09-20 19:47:45 -040047 if (reg & 0x10)
48 // Ram already present.
49 return;
50
51 // Copy bios.
Kevin O'Connor5bd01de2010-09-13 21:19:27 -040052 extern u8 code32flat_start[], code32flat_end[];
53 memcpy(code32flat_start, code32flat_start + BIOS_SRC_OFFSET
54 , code32flat_end - code32flat_start);
Kevin O'Connor5b8f8092009-09-20 19:47:45 -040055}
56
Kevin O'Connor6e4583c2011-06-19 10:09:26 -040057static void
Isaku Yamahata08328e72010-07-20 16:50:45 +090058make_bios_writable_intel(u16 bdf, u32 pam0)
59{
60 int reg = pci_config_readb(bdf, pam0);
61 if (!(reg & 0x10)) {
62 // QEMU doesn't fully implement the piix shadow capabilities -
63 // if ram isn't backing the bios segment when shadowing is
64 // disabled, the code itself wont be in memory. So, run the
65 // code from the high-memory flash location.
Kevin O'Connor5bd01de2010-09-13 21:19:27 -040066 u32 pos = (u32)__make_bios_writable_intel + BIOS_SRC_OFFSET;
Isaku Yamahata08328e72010-07-20 16:50:45 +090067 void (*func)(u16 bdf, u32 pam0) = (void*)pos;
68 func(bdf, pam0);
69 return;
70 }
71 // Ram already present - just enable writes
72 __make_bios_writable_intel(bdf, pam0);
73}
74
Kevin O'Connor6e4583c2011-06-19 10:09:26 -040075static void
Isaku Yamahata08328e72010-07-20 16:50:45 +090076make_bios_readonly_intel(u16 bdf, u32 pam0)
77{
78 // Flush any pending writes before locking memory.
79 wbinvd();
80
81 // Write protect roms from 0xc0000-0xf0000
82 int i;
83 for (i=0; i<6; i++) {
84 u32 mem = BUILD_ROM_START + i * 32*1024;
85 u32 pam = pam0 + 1 + i;
86 if (RomEnd <= mem + 16*1024) {
87 if (RomEnd > mem)
88 pci_config_writeb(bdf, pam, 0x31);
89 break;
90 }
91 pci_config_writeb(bdf, pam, 0x11);
92 }
93
94 // Write protect 0xf0000-0x100000
95 pci_config_writeb(bdf, pam0, 0x10);
96}
97
Kevin O'Connor6e4583c2011-06-19 10:09:26 -040098static void i440fx_bios_make_readonly(u16 bdf, void *arg)
99{
100 make_bios_readonly_intel(bdf, I440FX_PAM0);
101}
102
Kevin O'Connor6e4583c2011-06-19 10:09:26 -0400103static const struct pci_device_id dram_controller_make_readonly_tbl[] = {
104 PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82441,
105 i440fx_bios_make_readonly),
106 PCI_DEVICE_END
107};
108
Kevin O'Connor5b8f8092009-09-20 19:47:45 -0400109// Make the 0xc0000-0x100000 area read/writable.
110void
Kevin O'Connor1ca05b02010-01-03 17:43:37 -0500111make_bios_writable(void)
Kevin O'Connor5b8f8092009-09-20 19:47:45 -0400112{
Ian Campbell74c78782011-06-01 11:00:29 +0100113 if (CONFIG_COREBOOT || usingXen())
Kevin O'Connor5b8f8092009-09-20 19:47:45 -0400114 return;
115
116 dprintf(3, "enabling shadow ram\n");
117
Kevin O'Connorfaf6a4e2011-06-21 21:22:01 -0400118 // At this point, statically allocated variables can't be written,
119 // so do this search manually.
120 int bdf, max;
121 foreachbdf_in_bus(bdf, max, 0) {
122 u32 vendev = pci_config_readl(bdf, PCI_VENDOR_ID);
123 u16 vendor = vendev & 0xffff, device = vendev >> 16;
124 if (vendor == PCI_VENDOR_ID_INTEL
125 && device == PCI_DEVICE_ID_INTEL_82441) {
126 make_bios_writable_intel(bdf, I440FX_PAM0);
127 return;
128 }
Kevin O'Connor35284962009-07-24 21:49:26 -0400129 }
Kevin O'Connorfaf6a4e2011-06-21 21:22:01 -0400130 dprintf(1, "Unable to unlock ram - bridge not found\n");
Kevin O'Connorda4a6482008-06-08 13:48:06 -0400131}
132
133// Make the BIOS code segment area (0xf0000) read-only.
134void
Kevin O'Connor1ca05b02010-01-03 17:43:37 -0500135make_bios_readonly(void)
Kevin O'Connorda4a6482008-06-08 13:48:06 -0400136{
Ian Campbell74c78782011-06-01 11:00:29 +0100137 if (CONFIG_COREBOOT || usingXen())
Kevin O'Connorda4a6482008-06-08 13:48:06 -0400138 return;
139
140 dprintf(3, "locking shadow ram\n");
Isaku Yamahata08328e72010-07-20 16:50:45 +0900141 int bdf = pci_find_init_device(dram_controller_make_readonly_tbl, NULL);
Kevin O'Connorbe19cdc2008-11-09 15:33:47 -0500142 if (bdf < 0) {
Kevin O'Connorda4a6482008-06-08 13:48:06 -0400143 dprintf(1, "Unable to lock ram - bridge not found\n");
Kevin O'Connorda4a6482008-06-08 13:48:06 -0400144 }
Kevin O'Connorda4a6482008-06-08 13:48:06 -0400145}
Kevin O'Connor244caf82010-09-15 21:48:16 -0400146
147void
148qemu_prep_reset(void)
149{
150 if (CONFIG_COREBOOT)
151 return;
152 // QEMU doesn't map 0xc0000-0xfffff back to the original rom on a
153 // reset, so do that manually before invoking a hard reset.
154 make_bios_writable();
155 extern u8 code32flat_start[], code32flat_end[];
156 memcpy(code32flat_start, code32flat_start + BIOS_SRC_OFFSET
157 , code32flat_end - code32flat_start);
158}