When enabling write access to 0xf0000, just copy from 0xffff0000.
[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 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 copy_bios(u16 bdf, int reg)
27 {
28     pci_config_writeb(bdf, 0x59, reg | 0x30);
29     memcpy((void*)BUILD_BIOS_ADDR, (void*)BIOS_SRC_ADDR, 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     int bdf = pci_find_device(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82441);
43     if (bdf < 0) {
44         dprintf(1, "Unable to unlock ram - bridge not found\n");
45         return;
46     }
47
48     int reg = pci_config_readb(bdf, 0x59);
49     if (reg & 0x30) {
50         // Ram already present - just enable writes
51         pci_config_writeb(bdf, 0x59, reg | 0x30);
52         return;
53     }
54
55     // Enable shadowing and copy bios.
56     if (IN_RANGE((u32)copy_bios, BUILD_BIOS_ADDR, BUILD_BIOS_SIZE)) {
57         // Jump to shadow enable function - use the copy in the
58         // temporary storage area so that memory does not change under
59         // the executing code.
60         u32 pos = (u32)copy_bios - BUILD_BIOS_ADDR + BIOS_SRC_ADDR;
61         void (*func)(u16 bdf, int reg) = (void*)pos;
62         func(bdf, reg);
63     } else {
64         copy_bios(bdf, reg);
65     }
66 }
67
68 // Make the BIOS code segment area (0xf0000) read-only.
69 void
70 make_bios_readonly()
71 {
72     if (CONFIG_COREBOOT)
73         return;
74
75     dprintf(3, "locking shadow ram\n");
76
77     int bdf = pci_find_device(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82441);
78     if (bdf < 0) {
79         dprintf(1, "Unable to lock ram - bridge not found\n");
80         return;
81     }
82
83     wbinvd();
84     int v = pci_config_readb(bdf, 0x59);
85     v = (v & 0x0f) | (0x10);
86     pci_config_writeb(bdf, 0x59, v);
87 }