Merge branch 'upstream-merge'
[qemu-kvm/markmc.git] / hw / vga-pci.c
bloba0ac62975d1582b3148b4043536fd6ad7b3ce97c
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"
32 typedef struct PCIVGAState {
33 PCIDevice dev;
34 VGACommonState vga;
35 } PCIVGAState;
37 static const VMStateDescription vmstate_vga_pci = {
38 .name = "vga",
39 .version_id = 2,
40 .minimum_version_id = 2,
41 .minimum_version_id_old = 2,
42 .fields = (VMStateField []) {
43 VMSTATE_PCI_DEVICE(dev, PCIVGAState),
44 VMSTATE_STRUCT(vga, PCIVGAState, 0, vmstate_vga_common, VGACommonState),
45 VMSTATE_END_OF_LIST()
49 static void vga_map(PCIDevice *pci_dev, int region_num,
50 uint32_t addr, uint32_t size, int type)
52 PCIVGAState *d = (PCIVGAState *)pci_dev;
53 VGACommonState *s = &d->vga;
54 if (region_num == PCI_ROM_SLOT) {
55 cpu_register_physical_memory(addr, s->bios_size, s->bios_offset);
56 } else {
57 cpu_register_physical_memory(addr, s->vram_size, s->vram_offset);
58 s->map_addr = addr;
59 s->map_end = addr + s->vram_size;
60 vga_dirty_log_start(s);
64 static void pci_vga_write_config(PCIDevice *d,
65 uint32_t address, uint32_t val, int len)
67 PCIVGAState *pvs = container_of(d, PCIVGAState, dev);
68 VGACommonState *s = &pvs->vga;
70 vga_dirty_log_stop(s);
71 pci_default_write_config(d, address, val, len);
72 if (s->map_addr && pvs->dev.io_regions[0].addr == -1)
73 s->map_addr = 0;
74 vga_dirty_log_start(s);
77 static int pci_vga_initfn(PCIDevice *dev)
79 PCIVGAState *d = DO_UPCAST(PCIVGAState, dev, dev);
80 VGACommonState *s = &d->vga;
81 uint8_t *pci_conf = d->dev.config;
83 // vga + console init
84 vga_common_init(s, VGA_RAM_SIZE);
85 vga_init(s);
86 vmstate_register(0, &vmstate_vga_pci, d);
88 s->ds = graphic_console_init(s->update, s->invalidate,
89 s->screen_dump, s->text_update, s);
91 // dummy VGA (same as Bochs ID)
92 pci_config_set_vendor_id(pci_conf, PCI_VENDOR_ID_QEMU);
93 pci_config_set_device_id(pci_conf, PCI_DEVICE_ID_QEMU_VGA);
94 pci_config_set_class(pci_conf, PCI_CLASS_DISPLAY_VGA);
95 pci_conf[PCI_HEADER_TYPE] = PCI_HEADER_TYPE_NORMAL; // header_type
97 /* XXX: VGA_RAM_SIZE must be a power of two */
98 pci_register_bar(&d->dev, 0, VGA_RAM_SIZE,
99 PCI_ADDRESS_SPACE_MEM_PREFETCH, vga_map);
101 if (s->bios_size) {
102 unsigned int bios_total_size;
103 /* must be a power of two */
104 bios_total_size = 1;
105 while (bios_total_size < s->bios_size)
106 bios_total_size <<= 1;
107 pci_register_bar(&d->dev, PCI_ROM_SLOT, bios_total_size,
108 PCI_ADDRESS_SPACE_MEM_PREFETCH, vga_map);
110 return 0;
113 int pci_vga_init(PCIBus *bus,
114 unsigned long vga_bios_offset, int vga_bios_size)
116 PCIDevice *dev;
118 dev = pci_create(bus, -1, "VGA");
119 qdev_prop_set_uint32(&dev->qdev, "bios-offset", vga_bios_offset);
120 qdev_prop_set_uint32(&dev->qdev, "bios-size", vga_bios_offset);
121 qdev_init_nofail(&dev->qdev);
123 return 0;
126 static PCIDeviceInfo vga_info = {
127 .qdev.name = "VGA",
128 .qdev.size = sizeof(PCIVGAState),
129 .init = pci_vga_initfn,
130 .config_write = pci_vga_write_config,
131 .qdev.props = (Property[]) {
132 DEFINE_PROP_HEX32("bios-offset", PCIVGAState, vga.bios_offset, 0),
133 DEFINE_PROP_HEX32("bios-size", PCIVGAState, vga.bios_size, 0),
134 DEFINE_PROP_END_OF_LIST(),
138 static void vga_register(void)
140 pci_qdev_register(&vga_info);
142 device_init(vga_register);