xref: /openbmc/qemu/hw/i386/vmport.c (revision dda2441b)
1 /*
2  * QEMU VMPort emulation
3  *
4  * Copyright (C) 2007 Hervé Poussineau
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 #include "qemu/osdep.h"
25 #include "hw/hw.h"
26 #include "hw/isa/isa.h"
27 #include "hw/i386/pc.h"
28 #include "hw/input/i8042.h"
29 #include "sysemu/hw_accel.h"
30 #include "hw/qdev.h"
31 #include "qemu/log.h"
32 #include "trace.h"
33 
34 #define VMPORT_CMD_GETVERSION 0x0a
35 #define VMPORT_CMD_GETRAMSIZE 0x14
36 
37 #define VMPORT_ENTRIES 0x2c
38 #define VMPORT_MAGIC   0x564D5868
39 
40 #define VMPORT(obj) OBJECT_CHECK(VMPortState, (obj), TYPE_VMPORT)
41 
42 typedef struct VMPortState {
43     ISADevice parent_obj;
44 
45     MemoryRegion io;
46     VMPortReadFunc *func[VMPORT_ENTRIES];
47     void *opaque[VMPORT_ENTRIES];
48 } VMPortState;
49 
50 static VMPortState *port_state;
51 
52 void vmport_register(unsigned char command, VMPortReadFunc *func, void *opaque)
53 {
54     if (command >= VMPORT_ENTRIES) {
55         return;
56     }
57 
58     trace_vmport_register(command, func, opaque);
59     port_state->func[command] = func;
60     port_state->opaque[command] = opaque;
61 }
62 
63 static uint64_t vmport_ioport_read(void *opaque, hwaddr addr,
64                                    unsigned size)
65 {
66     VMPortState *s = opaque;
67     CPUState *cs = current_cpu;
68     X86CPU *cpu = X86_CPU(cs);
69     CPUX86State *env = &cpu->env;
70     unsigned char command;
71     uint32_t eax;
72 
73     cpu_synchronize_state(cs);
74 
75     eax = env->regs[R_EAX];
76     if (eax != VMPORT_MAGIC) {
77         return eax;
78     }
79 
80     command = env->regs[R_ECX];
81     trace_vmport_command(command);
82     if (command >= VMPORT_ENTRIES || !s->func[command]) {
83         qemu_log_mask(LOG_UNIMP, "vmport: unknown command %x\n", command);
84         return eax;
85     }
86 
87     return s->func[command](s->opaque[command], addr);
88 }
89 
90 static void vmport_ioport_write(void *opaque, hwaddr addr,
91                                 uint64_t val, unsigned size)
92 {
93     X86CPU *cpu = X86_CPU(current_cpu);
94 
95     cpu->env.regs[R_EAX] = vmport_ioport_read(opaque, addr, 4);
96 }
97 
98 static uint32_t vmport_cmd_get_version(void *opaque, uint32_t addr)
99 {
100     X86CPU *cpu = X86_CPU(current_cpu);
101 
102     cpu->env.regs[R_EBX] = VMPORT_MAGIC;
103     return 6;
104 }
105 
106 static uint32_t vmport_cmd_ram_size(void *opaque, uint32_t addr)
107 {
108     X86CPU *cpu = X86_CPU(current_cpu);
109 
110     cpu->env.regs[R_EBX] = 0x1177;
111     return ram_size;
112 }
113 
114 /* vmmouse helpers */
115 void vmmouse_get_data(uint32_t *data)
116 {
117     X86CPU *cpu = X86_CPU(current_cpu);
118     CPUX86State *env = &cpu->env;
119 
120     data[0] = env->regs[R_EAX]; data[1] = env->regs[R_EBX];
121     data[2] = env->regs[R_ECX]; data[3] = env->regs[R_EDX];
122     data[4] = env->regs[R_ESI]; data[5] = env->regs[R_EDI];
123 }
124 
125 void vmmouse_set_data(const uint32_t *data)
126 {
127     X86CPU *cpu = X86_CPU(current_cpu);
128     CPUX86State *env = &cpu->env;
129 
130     env->regs[R_EAX] = data[0]; env->regs[R_EBX] = data[1];
131     env->regs[R_ECX] = data[2]; env->regs[R_EDX] = data[3];
132     env->regs[R_ESI] = data[4]; env->regs[R_EDI] = data[5];
133 }
134 
135 static const MemoryRegionOps vmport_ops = {
136     .read = vmport_ioport_read,
137     .write = vmport_ioport_write,
138     .impl = {
139         .min_access_size = 4,
140         .max_access_size = 4,
141     },
142     .endianness = DEVICE_LITTLE_ENDIAN,
143 };
144 
145 static void vmport_realizefn(DeviceState *dev, Error **errp)
146 {
147     ISADevice *isadev = ISA_DEVICE(dev);
148     VMPortState *s = VMPORT(dev);
149 
150     memory_region_init_io(&s->io, OBJECT(s), &vmport_ops, s, "vmport", 1);
151     isa_register_ioport(isadev, &s->io, 0x5658);
152 
153     port_state = s;
154     /* Register some generic port commands */
155     vmport_register(VMPORT_CMD_GETVERSION, vmport_cmd_get_version, NULL);
156     vmport_register(VMPORT_CMD_GETRAMSIZE, vmport_cmd_ram_size, NULL);
157 }
158 
159 static void vmport_class_initfn(ObjectClass *klass, void *data)
160 {
161     DeviceClass *dc = DEVICE_CLASS(klass);
162 
163     dc->realize = vmport_realizefn;
164     /* Reason: realize sets global port_state */
165     dc->user_creatable = false;
166 }
167 
168 static const TypeInfo vmport_info = {
169     .name          = TYPE_VMPORT,
170     .parent        = TYPE_ISA_DEVICE,
171     .instance_size = sizeof(VMPortState),
172     .class_init    = vmport_class_initfn,
173 };
174 
175 static void vmport_register_types(void)
176 {
177     type_register_static(&vmport_info);
178 }
179 
180 type_init(vmport_register_types)
181