2 * QEMU PCI VGA Emulator.
4 * see docs/specs/standard-vga.txt for virtual hardware specs.
6 * Copyright (c) 2003 Fabrice Bellard
8 * Permission is hereby granted, free of charge, to any person obtaining a copy
9 * of this software and associated documentation files (the "Software"), to deal
10 * in the Software without restriction, including without limitation the rights
11 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12 * copies of the Software, and to permit persons to whom the Software is
13 * furnished to do so, subject to the following conditions:
15 * The above copyright notice and this permission notice shall be included in
16 * all copies or substantial portions of the Software.
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
21 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
27 #include "ui/console.h"
28 #include "hw/pci/pci.h"
30 #include "ui/pixel_ops.h"
31 #include "qemu/timer.h"
32 #include "hw/loader.h"
34 #define PCI_VGA_IOPORT_OFFSET 0x400
35 #define PCI_VGA_IOPORT_SIZE (0x3e0 - 0x3c0)
36 #define PCI_VGA_BOCHS_OFFSET 0x500
37 #define PCI_VGA_BOCHS_SIZE (0x0b * 2)
38 #define PCI_VGA_QEXT_OFFSET 0x600
39 #define PCI_VGA_QEXT_SIZE (2 * 4)
40 #define PCI_VGA_MMIO_SIZE 0x1000
42 #define PCI_VGA_QEXT_REG_SIZE (0 * 4)
43 #define PCI_VGA_QEXT_REG_BYTEORDER (1 * 4)
44 #define PCI_VGA_QEXT_LITTLE_ENDIAN 0x1e1e1e1e
45 #define PCI_VGA_QEXT_BIG_ENDIAN 0xbebebebe
48 PCI_VGA_FLAG_ENABLE_MMIO
= 1,
49 PCI_VGA_FLAG_ENABLE_QEXT
= 2,
52 typedef struct PCIVGAState
{
62 #define TYPE_PCI_VGA "pci-vga"
63 #define PCI_VGA(obj) OBJECT_CHECK(PCIVGAState, (obj), TYPE_PCI_VGA)
65 static const VMStateDescription vmstate_vga_pci
= {
68 .minimum_version_id
= 2,
69 .fields
= (VMStateField
[]) {
70 VMSTATE_PCI_DEVICE(dev
, PCIVGAState
),
71 VMSTATE_STRUCT(vga
, PCIVGAState
, 0, vmstate_vga_common
, VGACommonState
),
76 static uint64_t pci_vga_ioport_read(void *ptr
, hwaddr addr
,
84 ret
= vga_ioport_read(&d
->vga
, addr
);
87 ret
= vga_ioport_read(&d
->vga
, addr
);
88 ret
|= vga_ioport_read(&d
->vga
, addr
+1) << 8;
94 static void pci_vga_ioport_write(void *ptr
, hwaddr addr
,
95 uint64_t val
, unsigned size
)
101 vga_ioport_write(&d
->vga
, addr
+ 0x3c0, val
);
105 * Update bytes in little endian order. Allows to update
106 * indexed registers with a single word write because the
107 * index byte is updated first.
109 vga_ioport_write(&d
->vga
, addr
+ 0x3c0, val
& 0xff);
110 vga_ioport_write(&d
->vga
, addr
+ 0x3c1, (val
>> 8) & 0xff);
115 static const MemoryRegionOps pci_vga_ioport_ops
= {
116 .read
= pci_vga_ioport_read
,
117 .write
= pci_vga_ioport_write
,
118 .valid
.min_access_size
= 1,
119 .valid
.max_access_size
= 4,
120 .impl
.min_access_size
= 1,
121 .impl
.max_access_size
= 2,
122 .endianness
= DEVICE_LITTLE_ENDIAN
,
125 static uint64_t pci_vga_bochs_read(void *ptr
, hwaddr addr
,
128 PCIVGAState
*d
= ptr
;
129 int index
= addr
>> 1;
131 vbe_ioport_write_index(&d
->vga
, 0, index
);
132 return vbe_ioport_read_data(&d
->vga
, 0);
135 static void pci_vga_bochs_write(void *ptr
, hwaddr addr
,
136 uint64_t val
, unsigned size
)
138 PCIVGAState
*d
= ptr
;
139 int index
= addr
>> 1;
141 vbe_ioport_write_index(&d
->vga
, 0, index
);
142 vbe_ioport_write_data(&d
->vga
, 0, val
);
145 static const MemoryRegionOps pci_vga_bochs_ops
= {
146 .read
= pci_vga_bochs_read
,
147 .write
= pci_vga_bochs_write
,
148 .valid
.min_access_size
= 1,
149 .valid
.max_access_size
= 4,
150 .impl
.min_access_size
= 2,
151 .impl
.max_access_size
= 2,
152 .endianness
= DEVICE_LITTLE_ENDIAN
,
155 static uint64_t pci_vga_qext_read(void *ptr
, hwaddr addr
, unsigned size
)
157 PCIVGAState
*d
= ptr
;
160 case PCI_VGA_QEXT_REG_SIZE
:
161 return PCI_VGA_QEXT_SIZE
;
162 case PCI_VGA_QEXT_REG_BYTEORDER
:
163 return d
->vga
.big_endian_fb
?
164 PCI_VGA_QEXT_BIG_ENDIAN
: PCI_VGA_QEXT_LITTLE_ENDIAN
;
170 static void pci_vga_qext_write(void *ptr
, hwaddr addr
,
171 uint64_t val
, unsigned size
)
173 PCIVGAState
*d
= ptr
;
176 case PCI_VGA_QEXT_REG_BYTEORDER
:
177 if (val
== PCI_VGA_QEXT_BIG_ENDIAN
) {
178 d
->vga
.big_endian_fb
= true;
180 if (val
== PCI_VGA_QEXT_LITTLE_ENDIAN
) {
181 d
->vga
.big_endian_fb
= false;
187 static bool vga_get_big_endian_fb(Object
*obj
, Error
**errp
)
189 PCIVGAState
*d
= PCI_VGA(PCI_DEVICE(obj
));
191 return d
->vga
.big_endian_fb
;
194 static void vga_set_big_endian_fb(Object
*obj
, bool value
, Error
**errp
)
196 PCIVGAState
*d
= PCI_VGA(PCI_DEVICE(obj
));
198 d
->vga
.big_endian_fb
= value
;
201 static const MemoryRegionOps pci_vga_qext_ops
= {
202 .read
= pci_vga_qext_read
,
203 .write
= pci_vga_qext_write
,
204 .valid
.min_access_size
= 4,
205 .valid
.max_access_size
= 4,
206 .endianness
= DEVICE_LITTLE_ENDIAN
,
209 static void pci_std_vga_realize(PCIDevice
*dev
, Error
**errp
)
211 PCIVGAState
*d
= PCI_VGA(dev
);
212 VGACommonState
*s
= &d
->vga
;
214 /* vga + console init */
215 vga_common_init(s
, OBJECT(dev
), true);
216 vga_init(s
, OBJECT(dev
), pci_address_space(dev
), pci_address_space_io(dev
),
219 s
->con
= graphic_console_init(DEVICE(dev
), 0, s
->hw_ops
, s
);
221 /* XXX: VGA_RAM_SIZE must be a power of two */
222 pci_register_bar(&d
->dev
, 0, PCI_BASE_ADDRESS_MEM_PREFETCH
, &s
->vram
);
224 /* mmio bar for vga register access */
225 if (d
->flags
& (1 << PCI_VGA_FLAG_ENABLE_MMIO
)) {
226 memory_region_init(&d
->mmio
, NULL
, "vga.mmio", 4096);
227 memory_region_init_io(&d
->ioport
, NULL
, &pci_vga_ioport_ops
, d
,
228 "vga ioports remapped", PCI_VGA_IOPORT_SIZE
);
229 memory_region_init_io(&d
->bochs
, NULL
, &pci_vga_bochs_ops
, d
,
230 "bochs dispi interface", PCI_VGA_BOCHS_SIZE
);
232 memory_region_add_subregion(&d
->mmio
, PCI_VGA_IOPORT_OFFSET
,
234 memory_region_add_subregion(&d
->mmio
, PCI_VGA_BOCHS_OFFSET
,
237 if (d
->flags
& (1 << PCI_VGA_FLAG_ENABLE_QEXT
)) {
238 memory_region_init_io(&d
->qext
, NULL
, &pci_vga_qext_ops
, d
,
239 "qemu extended regs", PCI_VGA_QEXT_SIZE
);
240 memory_region_add_subregion(&d
->mmio
, PCI_VGA_QEXT_OFFSET
,
242 pci_set_byte(&d
->dev
.config
[PCI_REVISION_ID
], 2);
245 pci_register_bar(&d
->dev
, 2, PCI_BASE_ADDRESS_SPACE_MEMORY
, &d
->mmio
);
249 /* compatibility with pc-0.13 and older */
250 vga_init_vbe(s
, OBJECT(dev
), pci_address_space(dev
));
254 static void pci_std_vga_init(Object
*obj
)
256 /* Expose framebuffer byteorder via QOM */
257 object_property_add_bool(obj
, "big-endian-framebuffer",
258 vga_get_big_endian_fb
, vga_set_big_endian_fb
, NULL
);
261 static void pci_secondary_vga_realize(PCIDevice
*dev
, Error
**errp
)
263 PCIVGAState
*d
= PCI_VGA(dev
);
264 VGACommonState
*s
= &d
->vga
;
266 /* vga + console init */
267 vga_common_init(s
, OBJECT(dev
), false);
268 s
->con
= graphic_console_init(DEVICE(dev
), 0, s
->hw_ops
, s
);
271 memory_region_init(&d
->mmio
, OBJECT(dev
), "vga.mmio", 4096);
272 memory_region_init_io(&d
->ioport
, OBJECT(dev
), &pci_vga_ioport_ops
, d
,
273 "vga ioports remapped", PCI_VGA_IOPORT_SIZE
);
274 memory_region_init_io(&d
->bochs
, OBJECT(dev
), &pci_vga_bochs_ops
, d
,
275 "bochs dispi interface", PCI_VGA_BOCHS_SIZE
);
277 memory_region_add_subregion(&d
->mmio
, PCI_VGA_IOPORT_OFFSET
,
279 memory_region_add_subregion(&d
->mmio
, PCI_VGA_BOCHS_OFFSET
,
282 if (d
->flags
& (1 << PCI_VGA_FLAG_ENABLE_QEXT
)) {
283 memory_region_init_io(&d
->qext
, NULL
, &pci_vga_qext_ops
, d
,
284 "qemu extended regs", PCI_VGA_QEXT_SIZE
);
285 memory_region_add_subregion(&d
->mmio
, PCI_VGA_QEXT_OFFSET
,
287 pci_set_byte(&d
->dev
.config
[PCI_REVISION_ID
], 2);
290 pci_register_bar(&d
->dev
, 0, PCI_BASE_ADDRESS_MEM_PREFETCH
, &s
->vram
);
291 pci_register_bar(&d
->dev
, 2, PCI_BASE_ADDRESS_SPACE_MEMORY
, &d
->mmio
);
294 static void pci_secondary_vga_init(Object
*obj
)
296 /* Expose framebuffer byteorder via QOM */
297 object_property_add_bool(obj
, "big-endian-framebuffer",
298 vga_get_big_endian_fb
, vga_set_big_endian_fb
, NULL
);
301 static void pci_secondary_vga_reset(DeviceState
*dev
)
303 PCIVGAState
*d
= PCI_VGA(PCI_DEVICE(dev
));
304 vga_common_reset(&d
->vga
);
307 static Property vga_pci_properties
[] = {
308 DEFINE_PROP_UINT32("vgamem_mb", PCIVGAState
, vga
.vram_size_mb
, 16),
309 DEFINE_PROP_BIT("mmio", PCIVGAState
, flags
, PCI_VGA_FLAG_ENABLE_MMIO
, true),
310 DEFINE_PROP_BIT("qemu-extended-regs",
311 PCIVGAState
, flags
, PCI_VGA_FLAG_ENABLE_QEXT
, true),
312 DEFINE_PROP_END_OF_LIST(),
315 static Property secondary_pci_properties
[] = {
316 DEFINE_PROP_UINT32("vgamem_mb", PCIVGAState
, vga
.vram_size_mb
, 16),
317 DEFINE_PROP_BIT("qemu-extended-regs",
318 PCIVGAState
, flags
, PCI_VGA_FLAG_ENABLE_QEXT
, true),
319 DEFINE_PROP_END_OF_LIST(),
322 static void vga_pci_class_init(ObjectClass
*klass
, void *data
)
324 DeviceClass
*dc
= DEVICE_CLASS(klass
);
325 PCIDeviceClass
*k
= PCI_DEVICE_CLASS(klass
);
327 k
->vendor_id
= PCI_VENDOR_ID_QEMU
;
328 k
->device_id
= PCI_DEVICE_ID_QEMU_VGA
;
329 dc
->vmsd
= &vmstate_vga_pci
;
330 set_bit(DEVICE_CATEGORY_DISPLAY
, dc
->categories
);
333 static const TypeInfo vga_pci_type_info
= {
334 .name
= TYPE_PCI_VGA
,
335 .parent
= TYPE_PCI_DEVICE
,
336 .instance_size
= sizeof(PCIVGAState
),
338 .class_init
= vga_pci_class_init
,
341 static void vga_class_init(ObjectClass
*klass
, void *data
)
343 DeviceClass
*dc
= DEVICE_CLASS(klass
);
344 PCIDeviceClass
*k
= PCI_DEVICE_CLASS(klass
);
346 k
->realize
= pci_std_vga_realize
;
347 k
->romfile
= "vgabios-stdvga.bin";
348 k
->class_id
= PCI_CLASS_DISPLAY_VGA
;
349 dc
->props
= vga_pci_properties
;
350 dc
->hotpluggable
= false;
353 static void secondary_class_init(ObjectClass
*klass
, void *data
)
355 DeviceClass
*dc
= DEVICE_CLASS(klass
);
356 PCIDeviceClass
*k
= PCI_DEVICE_CLASS(klass
);
358 k
->realize
= pci_secondary_vga_realize
;
359 k
->class_id
= PCI_CLASS_DISPLAY_OTHER
;
360 dc
->props
= secondary_pci_properties
;
361 dc
->reset
= pci_secondary_vga_reset
;
364 static const TypeInfo vga_info
= {
366 .parent
= TYPE_PCI_VGA
,
367 .instance_init
= pci_std_vga_init
,
368 .class_init
= vga_class_init
,
371 static const TypeInfo secondary_info
= {
372 .name
= "secondary-vga",
373 .parent
= TYPE_PCI_VGA
,
374 .instance_init
= pci_secondary_vga_init
,
375 .class_init
= secondary_class_init
,
378 static void vga_register_types(void)
380 type_register_static(&vga_pci_type_info
);
381 type_register_static(&vga_info
);
382 type_register_static(&secondary_info
);
385 type_init(vga_register_types
)