xref: /qemu/hw/display/vga-mmio.c (revision b83a80e8)
1 /*
2  * QEMU MMIO VGA Emulator.
3  *
4  * Copyright (c) 2003 Fabrice Bellard
5  *
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:
12  *
13  * The above copyright notice and this permission notice shall be included in
14  * all copies or substantial portions of the Software.
15  *
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.
23  */
24 
25 #include "qemu/osdep.h"
26 #include "qapi/error.h"
27 #include "hw/sysbus.h"
28 #include "hw/display/vga.h"
29 #include "hw/qdev-properties.h"
30 #include "vga_int.h"
31 
32 /*
33  * QEMU interface:
34  *  + sysbus MMIO region 0: VGA I/O registers
35  *  + sysbus MMIO region 1: VGA MMIO registers
36  *  + sysbus MMIO region 2: VGA memory
37  */
38 
39 OBJECT_DECLARE_SIMPLE_TYPE(VGAMmioState, VGA_MMIO)
40 
41 struct VGAMmioState {
42     /*< private >*/
43     SysBusDevice parent_obj;
44 
45     /*< public >*/
46     VGACommonState vga;
47     MemoryRegion iomem;
48     MemoryRegion lowmem;
49 
50     uint8_t it_shift;
51 };
52 
53 static uint64_t vga_mm_read(void *opaque, hwaddr addr, unsigned size)
54 {
55     VGAMmioState *s = opaque;
56 
57     return vga_ioport_read(&s->vga, addr >> s->it_shift) &
58         MAKE_64BIT_MASK(0, size * 8);
59 }
60 
61 static void vga_mm_write(void *opaque, hwaddr addr, uint64_t value,
62                          unsigned size)
63 {
64     VGAMmioState *s = opaque;
65 
66     vga_ioport_write(&s->vga, addr >> s->it_shift,
67                      value & MAKE_64BIT_MASK(0, size * 8));
68 }
69 
70 static const MemoryRegionOps vga_mm_ctrl_ops = {
71     .read = vga_mm_read,
72     .write = vga_mm_write,
73     .valid.min_access_size = 1,
74     .valid.max_access_size = 4,
75     .impl.min_access_size = 1,
76     .impl.max_access_size = 4,
77     .endianness = DEVICE_NATIVE_ENDIAN,
78 };
79 
80 static void vga_mmio_reset(DeviceState *dev)
81 {
82     VGAMmioState *s = VGA_MMIO(dev);
83 
84     vga_common_reset(&s->vga);
85 }
86 
87 static void vga_mmio_realizefn(DeviceState *dev, Error **errp)
88 {
89     VGAMmioState *s = VGA_MMIO(dev);
90     SysBusDevice *sbd = SYS_BUS_DEVICE(dev);
91 
92     memory_region_init_io(&s->iomem, OBJECT(dev), &vga_mm_ctrl_ops, s,
93                           "vga-mmio", 0x100000);
94     memory_region_set_flush_coalesced(&s->iomem);
95     sysbus_init_mmio(sbd, &s->iomem);
96 
97     /* XXX: endianness? */
98     memory_region_init_io(&s->lowmem, OBJECT(dev), &vga_mem_ops, &s->vga,
99                           "vga-lowmem", 0x20000);
100     memory_region_set_coalescing(&s->lowmem);
101     sysbus_init_mmio(sbd, &s->lowmem);
102 
103     s->vga.bank_offset = 0;
104     s->vga.global_vmstate = true;
105     vga_common_init(&s->vga, OBJECT(dev));
106     sysbus_init_mmio(sbd, &s->vga.vram);
107     s->vga.con = graphic_console_init(dev, 0, s->vga.hw_ops, &s->vga);
108 }
109 
110 static Property vga_mmio_properties[] = {
111     DEFINE_PROP_UINT8("it_shift", VGAMmioState, it_shift, 0),
112     DEFINE_PROP_UINT32("vgamem_mb", VGAMmioState, vga.vram_size_mb, 8),
113     DEFINE_PROP_END_OF_LIST(),
114 };
115 
116 static void vga_mmio_class_initfn(ObjectClass *klass, void *data)
117 {
118     DeviceClass *dc = DEVICE_CLASS(klass);
119 
120     dc->realize = vga_mmio_realizefn;
121     dc->reset = vga_mmio_reset;
122     dc->vmsd = &vmstate_vga_common;
123     device_class_set_props(dc, vga_mmio_properties);
124     set_bit(DEVICE_CATEGORY_DISPLAY, dc->categories);
125 }
126 
127 static const TypeInfo vga_mmio_info = {
128     .name          = TYPE_VGA_MMIO,
129     .parent        = TYPE_SYS_BUS_DEVICE,
130     .instance_size = sizeof(VGAMmioState),
131     .class_init    = vga_mmio_class_initfn,
132 };
133 
134 static void vga_mmio_register_types(void)
135 {
136     type_register_static(&vga_mmio_info);
137 }
138 
139 type_init(vga_mmio_register_types)
140