Don't redefine NULL, please
[qemu/aliguori-queue.git] / hw / vga-pci.c
blob6038cec00db79292469e92f9d2653133a7c48a87
1 /*
2 * QEMU PCI VGA Emulator.
4 * Copyright (c) 2003 Fabrice Bellard
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 * THE SOFTWARE.
24 #include "hw.h"
25 #include "console.h"
26 #include "pc.h"
27 #include "pci.h"
28 #include "vga_int.h"
29 #include "pixel_ops.h"
30 #include "qemu-timer.h"
31 #include "kvm.h"
33 typedef struct PCIVGAState {
34 PCIDevice dev;
35 VGACommonState vga;
36 } PCIVGAState;
38 static void pci_vga_save(QEMUFile *f, void *opaque)
40 PCIVGAState *s = opaque;
42 pci_device_save(&s->dev, f);
43 vga_common_save(f, &s->vga);
46 static int pci_vga_load(QEMUFile *f, void *opaque, int version_id)
48 PCIVGAState *s = opaque;
49 int ret;
51 if (version_id > 2)
52 return -EINVAL;
54 if (version_id >= 2) {
55 ret = pci_device_load(&s->dev, f);
56 if (ret < 0)
57 return ret;
59 return vga_common_load(f, &s->vga, version_id);
62 void vga_dirty_log_start(VGACommonState *s)
64 if (kvm_enabled() && s->map_addr)
65 kvm_log_start(s->map_addr, s->map_end - s->map_addr);
67 if (kvm_enabled() && s->lfb_vram_mapped) {
68 kvm_log_start(isa_mem_base + 0xa0000, 0x8000);
69 kvm_log_start(isa_mem_base + 0xa8000, 0x8000);
73 static void vga_map(PCIDevice *pci_dev, int region_num,
74 uint32_t addr, uint32_t size, int type)
76 PCIVGAState *d = (PCIVGAState *)pci_dev;
77 VGACommonState *s = &d->vga;
78 if (region_num == PCI_ROM_SLOT) {
79 cpu_register_physical_memory(addr, s->bios_size, s->bios_offset);
80 } else {
81 cpu_register_physical_memory(addr, s->vram_size, s->vram_offset);
82 s->map_addr = addr;
83 s->map_end = addr + s->vram_size;
84 vga_dirty_log_start(s);
88 static void pci_vga_write_config(PCIDevice *d,
89 uint32_t address, uint32_t val, int len)
91 PCIVGAState *pvs = container_of(d, PCIVGAState, dev);
92 VGACommonState *s = &pvs->vga;
94 pci_default_write_config(d, address, val, len);
95 if (s->map_addr && pvs->dev.io_regions[0].addr == -1)
96 s->map_addr = 0;
99 static int pci_vga_initfn(PCIDevice *dev)
101 PCIVGAState *d = DO_UPCAST(PCIVGAState, dev, dev);
102 VGACommonState *s = &d->vga;
103 uint8_t *pci_conf = d->dev.config;
105 // vga + console init
106 vga_common_init(s, VGA_RAM_SIZE);
107 vga_init(s);
108 register_savevm("vga", 0, 2, pci_vga_save, pci_vga_load, d);
110 s->ds = graphic_console_init(s->update, s->invalidate,
111 s->screen_dump, s->text_update, s);
113 // dummy VGA (same as Bochs ID)
114 pci_config_set_vendor_id(pci_conf, PCI_VENDOR_ID_QEMU);
115 pci_config_set_device_id(pci_conf, PCI_DEVICE_ID_QEMU_VGA);
116 pci_config_set_class(pci_conf, PCI_CLASS_DISPLAY_VGA);
117 pci_conf[PCI_HEADER_TYPE] = PCI_HEADER_TYPE_NORMAL; // header_type
119 /* XXX: VGA_RAM_SIZE must be a power of two */
120 pci_register_bar(&d->dev, 0, VGA_RAM_SIZE,
121 PCI_ADDRESS_SPACE_MEM_PREFETCH, vga_map);
123 if (s->bios_size) {
124 unsigned int bios_total_size;
125 /* must be a power of two */
126 bios_total_size = 1;
127 while (bios_total_size < s->bios_size)
128 bios_total_size <<= 1;
129 pci_register_bar(&d->dev, PCI_ROM_SLOT, bios_total_size,
130 PCI_ADDRESS_SPACE_MEM_PREFETCH, vga_map);
132 return 0;
135 int pci_vga_init(PCIBus *bus,
136 unsigned long vga_bios_offset, int vga_bios_size)
138 PCIDevice *dev;
140 dev = pci_create("VGA", NULL);
141 qdev_prop_set_uint32(&dev->qdev, "bios-offset", vga_bios_offset);
142 qdev_prop_set_uint32(&dev->qdev, "bios-size", vga_bios_offset);
143 qdev_init(&dev->qdev);
145 return 0;
148 static PCIDeviceInfo vga_info = {
149 .qdev.name = "VGA",
150 .qdev.size = sizeof(PCIVGAState),
151 .init = pci_vga_initfn,
152 .config_write = pci_vga_write_config,
153 .qdev.props = (Property[]) {
154 DEFINE_PROP_HEX32("bios-offset", PCIVGAState, vga.bios_offset, 0),
155 DEFINE_PROP_HEX32("bios-size", PCIVGAState, vga.bios_size, 0),
156 DEFINE_PROP_END_OF_LIST(),
160 static void vga_register(void)
162 pci_qdev_register(&vga_info);
164 device_init(vga_register);