vga-pci.c 12.5 KB
Newer Older
1 2 3
/*
 * QEMU PCI VGA Emulator.
 *
G
Gerd Hoffmann 已提交
4 5
 * see docs/specs/standard-vga.txt for virtual hardware specs.
 *
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
 * 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.
 */
P
Peter Maydell 已提交
26
#include "qemu/osdep.h"
27 28
#include "hw/hw.h"
#include "hw/pci/pci.h"
29
#include "vga_int.h"
30
#include "ui/pixel_ops.h"
31
#include "qemu/timer.h"
32
#include "hw/loader.h"
33
#include "hw/display/edid.h"
34

G
Gerd Hoffmann 已提交
35 36
enum vga_pci_flags {
    PCI_VGA_FLAG_ENABLE_MMIO = 1,
G
Gerd Hoffmann 已提交
37
    PCI_VGA_FLAG_ENABLE_QEXT = 2,
38
    PCI_VGA_FLAG_ENABLE_EDID = 3,
G
Gerd Hoffmann 已提交
39 40
};

41 42 43
typedef struct PCIVGAState {
    PCIDevice dev;
    VGACommonState vga;
G
Gerd Hoffmann 已提交
44
    uint32_t flags;
45
    qemu_edid_info edid_info;
G
Gerd Hoffmann 已提交
46
    MemoryRegion mmio;
47 48
    MemoryRegion mrs[4];
    uint8_t edid[256];
49 50
} PCIVGAState;

G
Gonglei 已提交
51 52 53
#define TYPE_PCI_VGA "pci-vga"
#define PCI_VGA(obj) OBJECT_CHECK(PCIVGAState, (obj), TYPE_PCI_VGA)

J
Juan Quintela 已提交
54 55 56 57
static const VMStateDescription vmstate_vga_pci = {
    .name = "vga",
    .version_id = 2,
    .minimum_version_id = 2,
58
    .fields = (VMStateField[]) {
J
Juan Quintela 已提交
59 60 61
        VMSTATE_PCI_DEVICE(dev, PCIVGAState),
        VMSTATE_STRUCT(vga, PCIVGAState, 0, vmstate_vga_common, VGACommonState),
        VMSTATE_END_OF_LIST()
62
    }
J
Juan Quintela 已提交
63
};
64

A
Avi Kivity 已提交
65
static uint64_t pci_vga_ioport_read(void *ptr, hwaddr addr,
G
Gerd Hoffmann 已提交
66 67
                                    unsigned size)
{
68
    VGACommonState *s = ptr;
G
Gerd Hoffmann 已提交
69 70 71 72
    uint64_t ret = 0;

    switch (size) {
    case 1:
73
        ret = vga_ioport_read(s, addr + 0x3c0);
G
Gerd Hoffmann 已提交
74 75
        break;
    case 2:
76 77
        ret  = vga_ioport_read(s, addr + 0x3c0);
        ret |= vga_ioport_read(s, addr + 0x3c1) << 8;
G
Gerd Hoffmann 已提交
78 79 80 81 82
        break;
    }
    return ret;
}

A
Avi Kivity 已提交
83
static void pci_vga_ioport_write(void *ptr, hwaddr addr,
G
Gerd Hoffmann 已提交
84 85
                                 uint64_t val, unsigned size)
{
86
    VGACommonState *s = ptr;
G
Gerd Hoffmann 已提交
87

G
Gerd Hoffmann 已提交
88 89
    switch (size) {
    case 1:
90
        vga_ioport_write(s, addr + 0x3c0, val);
G
Gerd Hoffmann 已提交
91 92 93 94 95 96 97
        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.
         */
98 99
        vga_ioport_write(s, addr + 0x3c0, val & 0xff);
        vga_ioport_write(s, addr + 0x3c1, (val >> 8) & 0xff);
G
Gerd Hoffmann 已提交
100 101 102 103 104 105 106 107 108 109 110 111 112 113
        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,
};

A
Avi Kivity 已提交
114
static uint64_t pci_vga_bochs_read(void *ptr, hwaddr addr,
G
Gerd Hoffmann 已提交
115 116
                                   unsigned size)
{
117
    VGACommonState *s = ptr;
G
Gerd Hoffmann 已提交
118 119
    int index = addr >> 1;

120 121
    vbe_ioport_write_index(s, 0, index);
    return vbe_ioport_read_data(s, 0);
G
Gerd Hoffmann 已提交
122 123
}

A
Avi Kivity 已提交
124
static void pci_vga_bochs_write(void *ptr, hwaddr addr,
G
Gerd Hoffmann 已提交
125 126
                                uint64_t val, unsigned size)
{
127
    VGACommonState *s = ptr;
G
Gerd Hoffmann 已提交
128 129
    int index = addr >> 1;

130 131
    vbe_ioport_write_index(s, 0, index);
    vbe_ioport_write_data(s, 0, val);
G
Gerd Hoffmann 已提交
132 133 134 135 136 137 138 139 140 141 142 143
}

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,
};

G
Gerd Hoffmann 已提交
144 145
static uint64_t pci_vga_qext_read(void *ptr, hwaddr addr, unsigned size)
{
146
    VGACommonState *s = ptr;
G
Gerd Hoffmann 已提交
147 148 149 150 151

    switch (addr) {
    case PCI_VGA_QEXT_REG_SIZE:
        return PCI_VGA_QEXT_SIZE;
    case PCI_VGA_QEXT_REG_BYTEORDER:
152
        return s->big_endian_fb ?
G
Gerd Hoffmann 已提交
153 154 155 156 157 158 159 160 161
            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)
{
162
    VGACommonState *s = ptr;
G
Gerd Hoffmann 已提交
163 164 165 166

    switch (addr) {
    case PCI_VGA_QEXT_REG_BYTEORDER:
        if (val == PCI_VGA_QEXT_BIG_ENDIAN) {
167
            s->big_endian_fb = true;
G
Gerd Hoffmann 已提交
168 169
        }
        if (val == PCI_VGA_QEXT_LITTLE_ENDIAN) {
170
            s->big_endian_fb = false;
G
Gerd Hoffmann 已提交
171 172 173 174 175
        }
        break;
    }
}

176 177
static bool vga_get_big_endian_fb(Object *obj, Error **errp)
{
G
Gonglei 已提交
178
    PCIVGAState *d = PCI_VGA(PCI_DEVICE(obj));
179 180 181 182 183 184

    return d->vga.big_endian_fb;
}

static void vga_set_big_endian_fb(Object *obj, bool value, Error **errp)
{
G
Gonglei 已提交
185
    PCIVGAState *d = PCI_VGA(PCI_DEVICE(obj));
186 187 188 189

    d->vga.big_endian_fb = value;
}

G
Gerd Hoffmann 已提交
190 191 192 193 194 195 196 197
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,
};

198
void pci_std_vga_mmio_region_init(VGACommonState *s,
G
Gerd Hoffmann 已提交
199
                                  Object *owner,
200 201
                                  MemoryRegion *parent,
                                  MemoryRegion *subs,
202
                                  bool qext, bool edid)
203
{
204 205
    PCIVGAState *d = container_of(s, PCIVGAState, vga);

G
Gerd Hoffmann 已提交
206
    memory_region_init_io(&subs[0], owner, &pci_vga_ioport_ops, s,
207 208 209 210
                          "vga ioports remapped", PCI_VGA_IOPORT_SIZE);
    memory_region_add_subregion(parent, PCI_VGA_IOPORT_OFFSET,
                                &subs[0]);

G
Gerd Hoffmann 已提交
211
    memory_region_init_io(&subs[1], owner, &pci_vga_bochs_ops, s,
212 213 214 215 216
                          "bochs dispi interface", PCI_VGA_BOCHS_SIZE);
    memory_region_add_subregion(parent, PCI_VGA_BOCHS_OFFSET,
                                &subs[1]);

    if (qext) {
G
Gerd Hoffmann 已提交
217
        memory_region_init_io(&subs[2], owner, &pci_vga_qext_ops, s,
218 219 220 221
                              "qemu extended regs", PCI_VGA_QEXT_SIZE);
        memory_region_add_subregion(parent, PCI_VGA_QEXT_OFFSET,
                                    &subs[2]);
    }
222 223 224 225 226 227

    if (edid) {
        qemu_edid_generate(d->edid, sizeof(d->edid), &d->edid_info);
        qemu_edid_region_io(&subs[3], owner, d->edid, sizeof(d->edid));
        memory_region_add_subregion(parent, 0, &subs[3]);
    }
228 229
}

230
static void pci_std_vga_realize(PCIDevice *dev, Error **errp)
231
{
G
Gonglei 已提交
232
    PCIVGAState *d = PCI_VGA(dev);
G
Gerd Hoffmann 已提交
233
    VGACommonState *s = &d->vga;
234
    bool qext = false;
235
    bool edid = false;
236

G
Gerd Hoffmann 已提交
237
    /* vga + console init */
238
    vga_common_init(s, OBJECT(dev));
P
Paolo Bonzini 已提交
239 240
    vga_init(s, OBJECT(dev), pci_address_space(dev), pci_address_space_io(dev),
             true);
241

242
    s->con = graphic_console_init(DEVICE(dev), 0, s->hw_ops, s);
243

G
Gerd Hoffmann 已提交
244 245
    /* XXX: VGA_RAM_SIZE must be a power of two */
    pci_register_bar(&d->dev, 0, PCI_BASE_ADDRESS_MEM_PREFETCH, &s->vram);
246

G
Gerd Hoffmann 已提交
247 248
    /* mmio bar for vga register access */
    if (d->flags & (1 << PCI_VGA_FLAG_ENABLE_MMIO)) {
G
Gerd Hoffmann 已提交
249 250
        memory_region_init(&d->mmio, NULL, "vga.mmio",
                           PCI_VGA_MMIO_SIZE);
G
Gerd Hoffmann 已提交
251 252

        if (d->flags & (1 << PCI_VGA_FLAG_ENABLE_QEXT)) {
253
            qext = true;
G
Gerd Hoffmann 已提交
254 255
            pci_set_byte(&d->dev.config[PCI_REVISION_ID], 2);
        }
256 257 258 259 260
        if (d->flags & (1 << PCI_VGA_FLAG_ENABLE_EDID)) {
            edid = true;
        }
        pci_std_vga_mmio_region_init(s, OBJECT(dev), &d->mmio, d->mrs,
                                     qext, edid);
G
Gerd Hoffmann 已提交
261

G
Gerd Hoffmann 已提交
262 263 264
        pci_register_bar(&d->dev, 2, PCI_BASE_ADDRESS_SPACE_MEMORY, &d->mmio);
    }

G
Gerd Hoffmann 已提交
265 266
    if (!dev->rom_bar) {
        /* compatibility with pc-0.13 and older */
P
Paolo Bonzini 已提交
267
        vga_init_vbe(s, OBJECT(dev), pci_address_space(dev));
G
Gerd Hoffmann 已提交
268
    }
269 270
}

271 272 273 274 275 276 277
static void pci_std_vga_init(Object *obj)
{
    /* Expose framebuffer byteorder via QOM */
    object_property_add_bool(obj, "big-endian-framebuffer",
                             vga_get_big_endian_fb, vga_set_big_endian_fb, NULL);
}

278
static void pci_secondary_vga_realize(PCIDevice *dev, Error **errp)
279
{
G
Gonglei 已提交
280
    PCIVGAState *d = PCI_VGA(dev);
281
    VGACommonState *s = &d->vga;
282
    bool qext = false;
283
    bool edid = false;
284 285

    /* vga + console init */
286
    vga_common_init(s, OBJECT(dev));
287 288 289
    s->con = graphic_console_init(DEVICE(dev), 0, s->hw_ops, s);

    /* mmio bar */
G
Gerd Hoffmann 已提交
290 291
    memory_region_init(&d->mmio, OBJECT(dev), "vga.mmio",
                       PCI_VGA_MMIO_SIZE);
292

G
Gerd Hoffmann 已提交
293
    if (d->flags & (1 << PCI_VGA_FLAG_ENABLE_QEXT)) {
294
        qext = true;
G
Gerd Hoffmann 已提交
295 296
        pci_set_byte(&d->dev.config[PCI_REVISION_ID], 2);
    }
297 298 299 300
    if (d->flags & (1 << PCI_VGA_FLAG_ENABLE_EDID)) {
        edid = true;
    }
    pci_std_vga_mmio_region_init(s, OBJECT(dev), &d->mmio, d->mrs, qext, edid);
G
Gerd Hoffmann 已提交
301

302 303
    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);
304
}
305

306 307 308 309 310 311 312 313
static void pci_secondary_vga_exit(PCIDevice *dev)
{
    PCIVGAState *d = PCI_VGA(dev);
    VGACommonState *s = &d->vga;

    graphic_console_close(s->con);
}

314 315 316 317 318
static void pci_secondary_vga_init(Object *obj)
{
    /* Expose framebuffer byteorder via QOM */
    object_property_add_bool(obj, "big-endian-framebuffer",
                             vga_get_big_endian_fb, vga_set_big_endian_fb, NULL);
319 320 321 322
}

static void pci_secondary_vga_reset(DeviceState *dev)
{
G
Gonglei 已提交
323
    PCIVGAState *d = PCI_VGA(PCI_DEVICE(dev));
324 325 326
    vga_common_reset(&d->vga);
}

G
Gerd Hoffmann 已提交
327
static Property vga_pci_properties[] = {
G
Gerd Hoffmann 已提交
328
    DEFINE_PROP_UINT32("vgamem_mb", PCIVGAState, vga.vram_size_mb, 16),
G
Gerd Hoffmann 已提交
329
    DEFINE_PROP_BIT("mmio", PCIVGAState, flags, PCI_VGA_FLAG_ENABLE_MMIO, true),
G
Gerd Hoffmann 已提交
330 331
    DEFINE_PROP_BIT("qemu-extended-regs",
                    PCIVGAState, flags, PCI_VGA_FLAG_ENABLE_QEXT, true),
332 333 334
    DEFINE_PROP_BIT("edid",
                    PCIVGAState, flags, PCI_VGA_FLAG_ENABLE_EDID, false),
    DEFINE_EDID_PROPERTIES(PCIVGAState, edid_info),
335
    DEFINE_PROP_BOOL("global-vmstate", PCIVGAState, vga.global_vmstate, false),
G
Gerd Hoffmann 已提交
336 337 338
    DEFINE_PROP_END_OF_LIST(),
};

339 340
static Property secondary_pci_properties[] = {
    DEFINE_PROP_UINT32("vgamem_mb", PCIVGAState, vga.vram_size_mb, 16),
G
Gerd Hoffmann 已提交
341 342
    DEFINE_PROP_BIT("qemu-extended-regs",
                    PCIVGAState, flags, PCI_VGA_FLAG_ENABLE_QEXT, true),
343 344 345
    DEFINE_PROP_BIT("edid",
                    PCIVGAState, flags, PCI_VGA_FLAG_ENABLE_EDID, false),
    DEFINE_EDID_PROPERTIES(PCIVGAState, edid_info),
346 347 348
    DEFINE_PROP_END_OF_LIST(),
};

G
Gonglei 已提交
349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365
static void vga_pci_class_init(ObjectClass *klass, void *data)
{
    DeviceClass *dc = DEVICE_CLASS(klass);
    PCIDeviceClass *k = PCI_DEVICE_CLASS(klass);

    k->vendor_id = PCI_VENDOR_ID_QEMU;
    k->device_id = PCI_DEVICE_ID_QEMU_VGA;
    dc->vmsd = &vmstate_vga_pci;
    set_bit(DEVICE_CATEGORY_DISPLAY, dc->categories);
}

static const TypeInfo vga_pci_type_info = {
    .name = TYPE_PCI_VGA,
    .parent = TYPE_PCI_DEVICE,
    .instance_size = sizeof(PCIVGAState),
    .abstract = true,
    .class_init = vga_pci_class_init,
366 367 368 369
    .interfaces = (InterfaceInfo[]) {
        { INTERFACE_CONVENTIONAL_PCI_DEVICE },
        { },
    },
G
Gonglei 已提交
370 371
};

372 373
static void vga_class_init(ObjectClass *klass, void *data)
{
374
    DeviceClass *dc = DEVICE_CLASS(klass);
375 376
    PCIDeviceClass *k = PCI_DEVICE_CLASS(klass);

377
    k->realize = pci_std_vga_realize;
378 379
    k->romfile = "vgabios-stdvga.bin";
    k->class_id = PCI_CLASS_DISPLAY_VGA;
G
Gerd Hoffmann 已提交
380
    dc->props = vga_pci_properties;
381
    dc->hotpluggable = false;
382
}
383

384 385 386 387 388
static void secondary_class_init(ObjectClass *klass, void *data)
{
    DeviceClass *dc = DEVICE_CLASS(klass);
    PCIDeviceClass *k = PCI_DEVICE_CLASS(klass);

389
    k->realize = pci_secondary_vga_realize;
390
    k->exit = pci_secondary_vga_exit;
391 392 393 394 395
    k->class_id = PCI_CLASS_DISPLAY_OTHER;
    dc->props = secondary_pci_properties;
    dc->reset = pci_secondary_vga_reset;
}

396
static const TypeInfo vga_info = {
397
    .name          = "VGA",
G
Gonglei 已提交
398
    .parent        = TYPE_PCI_VGA,
399
    .instance_init = pci_std_vga_init,
400
    .class_init    = vga_class_init,
401 402
};

403 404
static const TypeInfo secondary_info = {
    .name          = "secondary-vga",
G
Gonglei 已提交
405
    .parent        = TYPE_PCI_VGA,
406
    .instance_init = pci_secondary_vga_init,
407 408 409
    .class_init    = secondary_class_init,
};

A
Andreas Färber 已提交
410
static void vga_register_types(void)
411
{
G
Gonglei 已提交
412
    type_register_static(&vga_pci_type_info);
413
    type_register_static(&vga_info);
414
    type_register_static(&secondary_info);
415
}
A
Andreas Färber 已提交
416 417

type_init(vga_register_types)