Allow rom to grow beyond 64K.
[seabios.git] / src / shadow.c
1 // Support for enabling/disabling BIOS ram shadowing.
2 //
3 // Copyright (C) 2008,2009  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 "util.h" // memcpy
9 #include "pci.h" // pci_config_writeb
10 #include "config.h" // CONFIG_*
11 #include "pci_ids.h" // PCI_VENDOR_ID_INTEL
12
13 // Test if 'addr' is in the range from 'start'..'start+size'
14 #define IN_RANGE(addr, start, size) ({   \
15             u32 __addr = (addr);         \
16             u32 __start = (start);       \
17             u32 __size = (size);         \
18             (__addr - __start < __size); \
19         })
20
21 // On the emulators, the bios at 0xf0000 is also at 0xffff0000
22 #define BIOS_SRC_ADDR 0xffff0000
23
24 // Enable shadowing and copy bios.
25 static void
26 __make_bios_writable(u16 bdf)
27 {
28     // Make ram from 0xc0000-0xf0000 writable
29     int clear = 0;
30     int i;
31     for (i=0; i<6; i++) {
32         if (CONFIG_OPTIONROMS_DEPLOYED) {
33             int reg = pci_config_readb(bdf, 0x5a + i);
34             if ((reg & 0x11) != 0x11) {
35                 // Need to copy optionroms to work around qemu implementation
36                 void *mem = (void*)(BUILD_ROM_START + i * 32*1024);
37                 memcpy((void*)BUILD_BIOS_TMP_ADDR, mem, 32*1024);
38                 pci_config_writeb(bdf, 0x5a + i, 0x33);
39                 memcpy(mem, (void*)BUILD_BIOS_TMP_ADDR, 32*1024);
40                 clear = 1;
41             } else {
42                 pci_config_writeb(bdf, 0x5a + i, 0x33);
43             }
44         } else {
45             pci_config_writeb(bdf, 0x5a + i, 0x33);
46         }
47     }
48     if (clear)
49         memset((void*)BUILD_BIOS_TMP_ADDR, 0, 32*1024);
50
51     // Make ram from 0xf0000-0x100000 writable
52     int reg = pci_config_readb(bdf, 0x59);
53     pci_config_writeb(bdf, 0x59, 0x30);
54     if (reg & 0x10)
55         // Ram already present.
56         return;
57
58     // Copy bios.
59     memcpy((void*)BUILD_BIOS_ADDR, (void*)BIOS_SRC_ADDR, BUILD_BIOS_SIZE);
60 }
61
62 // Make the 0xc0000-0x100000 area read/writable.
63 void
64 make_bios_writable()
65 {
66     if (CONFIG_COREBOOT)
67         return;
68
69     dprintf(3, "enabling shadow ram\n");
70
71     // Locate chip controlling ram shadowing.
72     int bdf = pci_find_device(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82441);
73     if (bdf < 0) {
74         dprintf(1, "Unable to unlock ram - bridge not found\n");
75         return;
76     }
77
78     int reg = pci_config_readb(bdf, 0x59);
79     if (!(reg & 0x10)) {
80         // QEMU doesn't fully implement the piix shadow capabilities -
81         // if ram isn't backing the bios segment when shadowing is
82         // disabled, the code itself wont be in memory.  So, run the
83         // code from the high-memory flash location.
84         u32 pos = (u32)__make_bios_writable - BUILD_BIOS_ADDR + BIOS_SRC_ADDR;
85         void (*func)(u16 bdf) = (void*)pos;
86         func(bdf);
87         return;
88     }
89     // Ram already present - just enable writes
90     __make_bios_writable(bdf);
91 }
92
93 // Make the BIOS code segment area (0xf0000) read-only.
94 void
95 make_bios_readonly()
96 {
97     if (CONFIG_COREBOOT)
98         return;
99
100     dprintf(3, "locking shadow ram\n");
101
102     int bdf = pci_find_device(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82441);
103     if (bdf < 0) {
104         dprintf(1, "Unable to lock ram - bridge not found\n");
105         return;
106     }
107
108     // Flush any pending writes before locking memory.
109     wbinvd();
110
111     // Write protect roms from 0xc0000-0xf0000
112     int i;
113     for (i=0; i<6; i++) {
114         u32 mem = BUILD_ROM_START + i * 32*1024;
115         if (RomEnd <= mem + 16*1024) {
116             if (RomEnd > mem)
117                 pci_config_writeb(bdf, 0x5a + i, 0x31);
118             break;
119         }
120         pci_config_writeb(bdf, 0x5a + i, 0x11);
121     }
122
123     // Write protect 0xf0000-0x100000
124     pci_config_writeb(bdf, 0x59, 0x10);
125 }