2009-08-31 22:07:15 +08:00
|
|
|
/*
|
|
|
|
* QEMU PCI VGA Emulator.
|
|
|
|
*
|
2012-10-15 14:02:56 +08:00
|
|
|
* see docs/specs/standard-vga.txt for virtual hardware specs.
|
|
|
|
*
|
2009-08-31 22:07:15 +08:00
|
|
|
* Copyright (c) 2003 Fabrice Bellard
|
|
|
|
*
|
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
* of this software and associated documentation files (the "Software"), to deal
|
|
|
|
* in the Software without restriction, including without limitation the rights
|
|
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
* copies of the Software, and to permit persons to whom the Software is
|
|
|
|
* furnished to do so, subject to the following conditions:
|
|
|
|
*
|
|
|
|
* The above copyright notice and this permission notice shall be included in
|
|
|
|
* all copies or substantial portions of the Software.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
|
|
|
|
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
|
|
* THE SOFTWARE.
|
|
|
|
*/
|
2013-02-04 22:40:22 +08:00
|
|
|
#include "hw/hw.h"
|
2012-11-28 19:06:30 +08:00
|
|
|
#include "ui/console.h"
|
2013-02-04 22:40:22 +08:00
|
|
|
#include "hw/pci/pci.h"
|
2013-03-19 00:36:02 +08:00
|
|
|
#include "vga_int.h"
|
2012-11-28 19:06:30 +08:00
|
|
|
#include "ui/pixel_ops.h"
|
2012-12-18 01:20:00 +08:00
|
|
|
#include "qemu/timer.h"
|
2013-02-04 22:40:22 +08:00
|
|
|
#include "hw/loader.h"
|
2009-08-31 22:07:15 +08:00
|
|
|
|
2012-10-15 14:02:55 +08:00
|
|
|
#define PCI_VGA_IOPORT_OFFSET 0x400
|
|
|
|
#define PCI_VGA_IOPORT_SIZE (0x3e0 - 0x3c0)
|
|
|
|
#define PCI_VGA_BOCHS_OFFSET 0x500
|
|
|
|
#define PCI_VGA_BOCHS_SIZE (0x0b * 2)
|
2014-09-23 18:45:56 +08:00
|
|
|
#define PCI_VGA_QEXT_OFFSET 0x600
|
|
|
|
#define PCI_VGA_QEXT_SIZE (2 * 4)
|
2012-10-15 14:02:55 +08:00
|
|
|
#define PCI_VGA_MMIO_SIZE 0x1000
|
|
|
|
|
2014-09-23 18:45:56 +08:00
|
|
|
#define PCI_VGA_QEXT_REG_SIZE (0 * 4)
|
|
|
|
#define PCI_VGA_QEXT_REG_BYTEORDER (1 * 4)
|
|
|
|
#define PCI_VGA_QEXT_LITTLE_ENDIAN 0x1e1e1e1e
|
|
|
|
#define PCI_VGA_QEXT_BIG_ENDIAN 0xbebebebe
|
|
|
|
|
2012-10-15 14:02:55 +08:00
|
|
|
enum vga_pci_flags {
|
|
|
|
PCI_VGA_FLAG_ENABLE_MMIO = 1,
|
2014-09-23 18:45:56 +08:00
|
|
|
PCI_VGA_FLAG_ENABLE_QEXT = 2,
|
2012-10-15 14:02:55 +08:00
|
|
|
};
|
|
|
|
|
2009-08-31 22:07:15 +08:00
|
|
|
typedef struct PCIVGAState {
|
|
|
|
PCIDevice dev;
|
|
|
|
VGACommonState vga;
|
2012-10-15 14:02:55 +08:00
|
|
|
uint32_t flags;
|
|
|
|
MemoryRegion mmio;
|
|
|
|
MemoryRegion ioport;
|
|
|
|
MemoryRegion bochs;
|
2014-09-23 18:45:56 +08:00
|
|
|
MemoryRegion qext;
|
2009-08-31 22:07:15 +08:00
|
|
|
} PCIVGAState;
|
|
|
|
|
2009-10-14 21:42:44 +08:00
|
|
|
static const VMStateDescription vmstate_vga_pci = {
|
|
|
|
.name = "vga",
|
|
|
|
.version_id = 2,
|
|
|
|
.minimum_version_id = 2,
|
2014-04-16 21:32:32 +08:00
|
|
|
.fields = (VMStateField[]) {
|
2009-10-14 21:42:44 +08:00
|
|
|
VMSTATE_PCI_DEVICE(dev, PCIVGAState),
|
|
|
|
VMSTATE_STRUCT(vga, PCIVGAState, 0, vmstate_vga_common, VGACommonState),
|
|
|
|
VMSTATE_END_OF_LIST()
|
2009-08-31 22:07:15 +08:00
|
|
|
}
|
2009-10-14 21:42:44 +08:00
|
|
|
};
|
2009-08-31 22:07:15 +08:00
|
|
|
|
2012-10-23 18:30:10 +08:00
|
|
|
static uint64_t pci_vga_ioport_read(void *ptr, hwaddr addr,
|
2012-10-15 14:02:55 +08:00
|
|
|
unsigned size)
|
|
|
|
{
|
|
|
|
PCIVGAState *d = ptr;
|
|
|
|
uint64_t ret = 0;
|
|
|
|
|
|
|
|
switch (size) {
|
|
|
|
case 1:
|
|
|
|
ret = vga_ioport_read(&d->vga, addr);
|
|
|
|
break;
|
|
|
|
case 2:
|
|
|
|
ret = vga_ioport_read(&d->vga, addr);
|
|
|
|
ret |= vga_ioport_read(&d->vga, addr+1) << 8;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2012-10-23 18:30:10 +08:00
|
|
|
static void pci_vga_ioport_write(void *ptr, hwaddr addr,
|
2012-10-15 14:02:55 +08:00
|
|
|
uint64_t val, unsigned size)
|
|
|
|
{
|
|
|
|
PCIVGAState *d = ptr;
|
2012-11-13 05:33:21 +08:00
|
|
|
|
2012-10-15 14:02:55 +08:00
|
|
|
switch (size) {
|
|
|
|
case 1:
|
2012-11-13 05:33:21 +08:00
|
|
|
vga_ioport_write(&d->vga, addr + 0x3c0, val);
|
2012-10-15 14:02:55 +08:00
|
|
|
break;
|
|
|
|
case 2:
|
|
|
|
/*
|
|
|
|
* Update bytes in little endian order. Allows to update
|
|
|
|
* indexed registers with a single word write because the
|
|
|
|
* index byte is updated first.
|
|
|
|
*/
|
2012-11-13 05:33:21 +08:00
|
|
|
vga_ioport_write(&d->vga, addr + 0x3c0, val & 0xff);
|
|
|
|
vga_ioport_write(&d->vga, addr + 0x3c1, (val >> 8) & 0xff);
|
2012-10-15 14:02:55 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static const MemoryRegionOps pci_vga_ioport_ops = {
|
|
|
|
.read = pci_vga_ioport_read,
|
|
|
|
.write = pci_vga_ioport_write,
|
|
|
|
.valid.min_access_size = 1,
|
|
|
|
.valid.max_access_size = 4,
|
|
|
|
.impl.min_access_size = 1,
|
|
|
|
.impl.max_access_size = 2,
|
|
|
|
.endianness = DEVICE_LITTLE_ENDIAN,
|
|
|
|
};
|
|
|
|
|
2012-10-23 18:30:10 +08:00
|
|
|
static uint64_t pci_vga_bochs_read(void *ptr, hwaddr addr,
|
2012-10-15 14:02:55 +08:00
|
|
|
unsigned size)
|
|
|
|
{
|
|
|
|
PCIVGAState *d = ptr;
|
|
|
|
int index = addr >> 1;
|
|
|
|
|
|
|
|
vbe_ioport_write_index(&d->vga, 0, index);
|
|
|
|
return vbe_ioport_read_data(&d->vga, 0);
|
|
|
|
}
|
|
|
|
|
2012-10-23 18:30:10 +08:00
|
|
|
static void pci_vga_bochs_write(void *ptr, hwaddr addr,
|
2012-10-15 14:02:55 +08:00
|
|
|
uint64_t val, unsigned size)
|
|
|
|
{
|
|
|
|
PCIVGAState *d = ptr;
|
|
|
|
int index = addr >> 1;
|
|
|
|
|
|
|
|
vbe_ioport_write_index(&d->vga, 0, index);
|
|
|
|
vbe_ioport_write_data(&d->vga, 0, val);
|
|
|
|
}
|
|
|
|
|
|
|
|
static const MemoryRegionOps pci_vga_bochs_ops = {
|
|
|
|
.read = pci_vga_bochs_read,
|
|
|
|
.write = pci_vga_bochs_write,
|
|
|
|
.valid.min_access_size = 1,
|
|
|
|
.valid.max_access_size = 4,
|
|
|
|
.impl.min_access_size = 2,
|
|
|
|
.impl.max_access_size = 2,
|
|
|
|
.endianness = DEVICE_LITTLE_ENDIAN,
|
|
|
|
};
|
|
|
|
|
2014-09-23 18:45:56 +08:00
|
|
|
static uint64_t pci_vga_qext_read(void *ptr, hwaddr addr, unsigned size)
|
|
|
|
{
|
|
|
|
PCIVGAState *d = ptr;
|
|
|
|
|
|
|
|
switch (addr) {
|
|
|
|
case PCI_VGA_QEXT_REG_SIZE:
|
|
|
|
return PCI_VGA_QEXT_SIZE;
|
|
|
|
case PCI_VGA_QEXT_REG_BYTEORDER:
|
|
|
|
return d->vga.big_endian_fb ?
|
|
|
|
PCI_VGA_QEXT_BIG_ENDIAN : PCI_VGA_QEXT_LITTLE_ENDIAN;
|
|
|
|
default:
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void pci_vga_qext_write(void *ptr, hwaddr addr,
|
|
|
|
uint64_t val, unsigned size)
|
|
|
|
{
|
|
|
|
PCIVGAState *d = ptr;
|
|
|
|
|
|
|
|
switch (addr) {
|
|
|
|
case PCI_VGA_QEXT_REG_BYTEORDER:
|
|
|
|
if (val == PCI_VGA_QEXT_BIG_ENDIAN) {
|
|
|
|
d->vga.big_endian_fb = true;
|
|
|
|
}
|
|
|
|
if (val == PCI_VGA_QEXT_LITTLE_ENDIAN) {
|
|
|
|
d->vga.big_endian_fb = false;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static const MemoryRegionOps pci_vga_qext_ops = {
|
|
|
|
.read = pci_vga_qext_read,
|
|
|
|
.write = pci_vga_qext_write,
|
|
|
|
.valid.min_access_size = 4,
|
|
|
|
.valid.max_access_size = 4,
|
|
|
|
.endianness = DEVICE_LITTLE_ENDIAN,
|
|
|
|
};
|
|
|
|
|
2012-09-08 17:38:41 +08:00
|
|
|
static int pci_std_vga_initfn(PCIDevice *dev)
|
2009-08-31 22:07:15 +08:00
|
|
|
{
|
2012-10-15 14:02:54 +08:00
|
|
|
PCIVGAState *d = DO_UPCAST(PCIVGAState, dev, dev);
|
|
|
|
VGACommonState *s = &d->vga;
|
2009-08-31 22:07:15 +08:00
|
|
|
|
2012-10-15 14:02:54 +08:00
|
|
|
/* vga + console init */
|
2013-10-12 01:56:59 +08:00
|
|
|
vga_common_init(s, OBJECT(dev), true);
|
2013-06-07 09:21:13 +08:00
|
|
|
vga_init(s, OBJECT(dev), pci_address_space(dev), pci_address_space_io(dev),
|
|
|
|
true);
|
2009-08-31 22:07:15 +08:00
|
|
|
|
2014-01-24 22:35:21 +08:00
|
|
|
s->con = graphic_console_init(DEVICE(dev), 0, s->hw_ops, s);
|
2009-08-31 22:07:15 +08:00
|
|
|
|
2012-10-15 14:02:54 +08:00
|
|
|
/* XXX: VGA_RAM_SIZE must be a power of two */
|
|
|
|
pci_register_bar(&d->dev, 0, PCI_BASE_ADDRESS_MEM_PREFETCH, &s->vram);
|
2009-08-31 22:07:15 +08:00
|
|
|
|
2012-10-15 14:02:55 +08:00
|
|
|
/* mmio bar for vga register access */
|
|
|
|
if (d->flags & (1 << PCI_VGA_FLAG_ENABLE_MMIO)) {
|
2013-06-06 17:41:28 +08:00
|
|
|
memory_region_init(&d->mmio, NULL, "vga.mmio", 4096);
|
|
|
|
memory_region_init_io(&d->ioport, NULL, &pci_vga_ioport_ops, d,
|
2012-10-15 14:02:55 +08:00
|
|
|
"vga ioports remapped", PCI_VGA_IOPORT_SIZE);
|
2013-06-06 17:41:28 +08:00
|
|
|
memory_region_init_io(&d->bochs, NULL, &pci_vga_bochs_ops, d,
|
2012-10-15 14:02:55 +08:00
|
|
|
"bochs dispi interface", PCI_VGA_BOCHS_SIZE);
|
|
|
|
|
|
|
|
memory_region_add_subregion(&d->mmio, PCI_VGA_IOPORT_OFFSET,
|
|
|
|
&d->ioport);
|
|
|
|
memory_region_add_subregion(&d->mmio, PCI_VGA_BOCHS_OFFSET,
|
|
|
|
&d->bochs);
|
2014-09-23 18:45:56 +08:00
|
|
|
|
|
|
|
if (d->flags & (1 << PCI_VGA_FLAG_ENABLE_QEXT)) {
|
|
|
|
memory_region_init_io(&d->qext, NULL, &pci_vga_qext_ops, d,
|
|
|
|
"qemu extended regs", PCI_VGA_QEXT_SIZE);
|
|
|
|
memory_region_add_subregion(&d->mmio, PCI_VGA_QEXT_OFFSET,
|
|
|
|
&d->qext);
|
|
|
|
pci_set_byte(&d->dev.config[PCI_REVISION_ID], 2);
|
|
|
|
}
|
|
|
|
|
2012-10-15 14:02:55 +08:00
|
|
|
pci_register_bar(&d->dev, 2, PCI_BASE_ADDRESS_SPACE_MEMORY, &d->mmio);
|
|
|
|
}
|
|
|
|
|
2012-10-15 14:02:54 +08:00
|
|
|
if (!dev->rom_bar) {
|
|
|
|
/* compatibility with pc-0.13 and older */
|
2013-06-07 09:21:13 +08:00
|
|
|
vga_init_vbe(s, OBJECT(dev), pci_address_space(dev));
|
2012-10-15 14:02:54 +08:00
|
|
|
}
|
2010-11-17 19:06:44 +08:00
|
|
|
|
2012-10-15 14:02:54 +08:00
|
|
|
return 0;
|
2009-08-31 22:07:15 +08:00
|
|
|
}
|
|
|
|
|
2012-11-12 21:29:47 +08:00
|
|
|
static int pci_secondary_vga_initfn(PCIDevice *dev)
|
|
|
|
{
|
|
|
|
PCIVGAState *d = DO_UPCAST(PCIVGAState, dev, dev);
|
|
|
|
VGACommonState *s = &d->vga;
|
|
|
|
|
|
|
|
/* vga + console init */
|
|
|
|
vga_common_init(s, OBJECT(dev), false);
|
|
|
|
s->con = graphic_console_init(DEVICE(dev), 0, s->hw_ops, s);
|
|
|
|
|
|
|
|
/* mmio bar */
|
|
|
|
memory_region_init(&d->mmio, OBJECT(dev), "vga.mmio", 4096);
|
|
|
|
memory_region_init_io(&d->ioport, OBJECT(dev), &pci_vga_ioport_ops, d,
|
|
|
|
"vga ioports remapped", PCI_VGA_IOPORT_SIZE);
|
|
|
|
memory_region_init_io(&d->bochs, OBJECT(dev), &pci_vga_bochs_ops, d,
|
|
|
|
"bochs dispi interface", PCI_VGA_BOCHS_SIZE);
|
|
|
|
|
|
|
|
memory_region_add_subregion(&d->mmio, PCI_VGA_IOPORT_OFFSET,
|
|
|
|
&d->ioport);
|
|
|
|
memory_region_add_subregion(&d->mmio, PCI_VGA_BOCHS_OFFSET,
|
|
|
|
&d->bochs);
|
|
|
|
|
2014-09-23 18:45:56 +08:00
|
|
|
if (d->flags & (1 << PCI_VGA_FLAG_ENABLE_QEXT)) {
|
|
|
|
memory_region_init_io(&d->qext, NULL, &pci_vga_qext_ops, d,
|
|
|
|
"qemu extended regs", PCI_VGA_QEXT_SIZE);
|
|
|
|
memory_region_add_subregion(&d->mmio, PCI_VGA_QEXT_OFFSET,
|
|
|
|
&d->qext);
|
|
|
|
pci_set_byte(&d->dev.config[PCI_REVISION_ID], 2);
|
|
|
|
}
|
|
|
|
|
2012-11-12 21:29:47 +08:00
|
|
|
pci_register_bar(&d->dev, 0, PCI_BASE_ADDRESS_MEM_PREFETCH, &s->vram);
|
|
|
|
pci_register_bar(&d->dev, 2, PCI_BASE_ADDRESS_SPACE_MEMORY, &d->mmio);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void pci_secondary_vga_reset(DeviceState *dev)
|
|
|
|
{
|
|
|
|
PCIVGAState *d = DO_UPCAST(PCIVGAState, dev.qdev, dev);
|
|
|
|
|
|
|
|
vga_common_reset(&d->vga);
|
|
|
|
}
|
|
|
|
|
2012-05-24 15:59:44 +08:00
|
|
|
static Property vga_pci_properties[] = {
|
2012-06-11 16:42:53 +08:00
|
|
|
DEFINE_PROP_UINT32("vgamem_mb", PCIVGAState, vga.vram_size_mb, 16),
|
2012-10-15 14:02:55 +08:00
|
|
|
DEFINE_PROP_BIT("mmio", PCIVGAState, flags, PCI_VGA_FLAG_ENABLE_MMIO, true),
|
2014-09-23 18:45:56 +08:00
|
|
|
DEFINE_PROP_BIT("qemu-extended-regs",
|
|
|
|
PCIVGAState, flags, PCI_VGA_FLAG_ENABLE_QEXT, true),
|
2012-05-24 15:59:44 +08:00
|
|
|
DEFINE_PROP_END_OF_LIST(),
|
|
|
|
};
|
|
|
|
|
2012-11-12 21:29:47 +08:00
|
|
|
static Property secondary_pci_properties[] = {
|
|
|
|
DEFINE_PROP_UINT32("vgamem_mb", PCIVGAState, vga.vram_size_mb, 16),
|
2014-09-23 18:45:56 +08:00
|
|
|
DEFINE_PROP_BIT("qemu-extended-regs",
|
|
|
|
PCIVGAState, flags, PCI_VGA_FLAG_ENABLE_QEXT, true),
|
2012-11-12 21:29:47 +08:00
|
|
|
DEFINE_PROP_END_OF_LIST(),
|
|
|
|
};
|
|
|
|
|
2011-12-05 02:22:06 +08:00
|
|
|
static void vga_class_init(ObjectClass *klass, void *data)
|
|
|
|
{
|
2011-12-08 11:34:16 +08:00
|
|
|
DeviceClass *dc = DEVICE_CLASS(klass);
|
2011-12-05 02:22:06 +08:00
|
|
|
PCIDeviceClass *k = PCI_DEVICE_CLASS(klass);
|
|
|
|
|
2012-09-08 17:38:41 +08:00
|
|
|
k->init = pci_std_vga_initfn;
|
2011-12-05 02:22:06 +08:00
|
|
|
k->romfile = "vgabios-stdvga.bin";
|
|
|
|
k->vendor_id = PCI_VENDOR_ID_QEMU;
|
|
|
|
k->device_id = PCI_DEVICE_ID_QEMU_VGA;
|
|
|
|
k->class_id = PCI_CLASS_DISPLAY_VGA;
|
2011-12-08 11:34:16 +08:00
|
|
|
dc->vmsd = &vmstate_vga_pci;
|
2012-05-24 15:59:44 +08:00
|
|
|
dc->props = vga_pci_properties;
|
2014-02-05 23:36:48 +08:00
|
|
|
dc->hotpluggable = false;
|
2013-07-29 22:17:45 +08:00
|
|
|
set_bit(DEVICE_CATEGORY_DISPLAY, dc->categories);
|
2011-12-05 02:22:06 +08:00
|
|
|
}
|
2011-05-25 09:58:31 +08:00
|
|
|
|
2012-11-12 21:29:47 +08:00
|
|
|
static void secondary_class_init(ObjectClass *klass, void *data)
|
|
|
|
{
|
|
|
|
DeviceClass *dc = DEVICE_CLASS(klass);
|
|
|
|
PCIDeviceClass *k = PCI_DEVICE_CLASS(klass);
|
|
|
|
|
|
|
|
k->init = pci_secondary_vga_initfn;
|
|
|
|
k->vendor_id = PCI_VENDOR_ID_QEMU;
|
|
|
|
k->device_id = PCI_DEVICE_ID_QEMU_VGA;
|
|
|
|
k->class_id = PCI_CLASS_DISPLAY_OTHER;
|
|
|
|
dc->vmsd = &vmstate_vga_pci;
|
|
|
|
dc->props = secondary_pci_properties;
|
|
|
|
dc->reset = pci_secondary_vga_reset;
|
2014-12-05 11:33:37 +08:00
|
|
|
set_bit(DEVICE_CATEGORY_DISPLAY, dc->categories);
|
2012-11-12 21:29:47 +08:00
|
|
|
}
|
|
|
|
|
2013-01-10 23:19:07 +08:00
|
|
|
static const TypeInfo vga_info = {
|
2011-12-08 11:34:16 +08:00
|
|
|
.name = "VGA",
|
|
|
|
.parent = TYPE_PCI_DEVICE,
|
|
|
|
.instance_size = sizeof(PCIVGAState),
|
|
|
|
.class_init = vga_class_init,
|
2009-08-31 22:07:15 +08:00
|
|
|
};
|
|
|
|
|
2012-11-12 21:29:47 +08:00
|
|
|
static const TypeInfo secondary_info = {
|
|
|
|
.name = "secondary-vga",
|
|
|
|
.parent = TYPE_PCI_DEVICE,
|
|
|
|
.instance_size = sizeof(PCIVGAState),
|
|
|
|
.class_init = secondary_class_init,
|
|
|
|
};
|
|
|
|
|
2012-02-09 22:20:55 +08:00
|
|
|
static void vga_register_types(void)
|
2009-08-31 22:07:15 +08:00
|
|
|
{
|
2011-12-08 11:34:16 +08:00
|
|
|
type_register_static(&vga_info);
|
2012-11-12 21:29:47 +08:00
|
|
|
type_register_static(&secondary_info);
|
2009-08-31 22:07:15 +08:00
|
|
|
}
|
2012-02-09 22:20:55 +08:00
|
|
|
|
|
|
|
type_init(vga_register_types)
|