Synch pci ids and registers with Linux kernel source.
[seabios.git] / src / shadow.c
1 // Support for enabling/disabling BIOS ram shadowing.
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 "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 // Enable shadowing and copy bios.
22 static void
23 copy_bios(PCIDevice d)
24 {
25     int v = pci_config_readb(d, 0x59);
26     v |= 0x30;
27     pci_config_writeb(d, 0x59, v);
28     memcpy((void *)BUILD_BIOS_ADDR, (void *)BUILD_BIOS_TMP_ADDR
29            , BUILD_BIOS_SIZE);
30 }
31
32 // Make the BIOS code segment area (0xf0000) writable.
33 void
34 make_bios_writable()
35 {
36     if (CONFIG_COREBOOT)
37         return;
38
39     dprintf(3, "enabling shadow ram\n");
40
41     // Locate chip controlling ram shadowing.
42     PCIDevice d;
43     int ret = pci_find_device(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82441
44                               , 0, &d);
45     if (ret) {
46         dprintf(1, "Unable to unlock ram - bridge not found\n");
47         return;
48     }
49
50     // Copy the bios to a temporary area.
51     memcpy((void *)BUILD_BIOS_TMP_ADDR, (void *)BUILD_BIOS_ADDR
52            , BUILD_BIOS_SIZE);
53
54     // Enable shadowing and copy bios.
55     if (IN_RANGE((u32)copy_bios, BUILD_BIOS_ADDR, BUILD_BIOS_SIZE)) {
56         // Jump to shadow enable function - use the copy in the
57         // temporary storage area so that memory does not change under
58         // the executing code.
59         u32 pos = (u32)copy_bios - BUILD_BIOS_ADDR + BUILD_BIOS_TMP_ADDR;
60         void (*func)(PCIDevice) = (void*)pos;
61         func(d);
62     } else {
63         copy_bios(d);
64     }
65
66     // Clear the temporary area.
67     memset((void *)BUILD_BIOS_TMP_ADDR, 0, BUILD_BIOS_SIZE);
68 }
69
70 // Make the BIOS code segment area (0xf0000) read-only.
71 void
72 make_bios_readonly()
73 {
74     if (CONFIG_COREBOOT)
75         return;
76
77     dprintf(3, "locking shadow ram\n");
78
79     PCIDevice d;
80     int ret = pci_find_device(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82441
81                               , 0, &d);
82     if (ret) {
83         dprintf(1, "Unable to lock ram - bridge not found\n");
84         return;
85     }
86
87     wbinvd();
88     int v = pci_config_readb(d, 0x59);
89     v = (v & 0x0f) | (0x10);
90     pci_config_writeb(d, 0x59, v);
91 }