1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38#include "config-host.h"
39#if defined(CONFIG_POSIX)
40#include <sys/mman.h>
41#endif
42#include "hw/hw.h"
43#include "hw/qdev.h"
44#include "hw/isa/isa.h"
45
46#define IOMEM_LEN 0x10000
47
48typedef struct PCTestdev {
49 ISADevice parent_obj;
50
51 MemoryRegion ioport;
52 MemoryRegion flush;
53 MemoryRegion irq;
54 MemoryRegion iomem;
55 uint32_t ioport_data;
56 char iomem_buf[IOMEM_LEN];
57} PCTestdev;
58
59#define TYPE_TESTDEV "pc-testdev"
60#define TESTDEV(obj) \
61 OBJECT_CHECK(PCTestdev, (obj), TYPE_TESTDEV)
62
63static void test_irq_line(void *opaque, hwaddr addr, uint64_t data,
64 unsigned len)
65{
66 PCTestdev *dev = opaque;
67 ISADevice *isa = ISA_DEVICE(dev);
68
69 qemu_set_irq(isa_get_irq(isa, addr), !!data);
70}
71
72static const MemoryRegionOps test_irq_ops = {
73 .write = test_irq_line,
74 .valid.min_access_size = 1,
75 .valid.max_access_size = 1,
76 .endianness = DEVICE_LITTLE_ENDIAN,
77};
78
79static void test_ioport_write(void *opaque, hwaddr addr, uint64_t data,
80 unsigned len)
81{
82 PCTestdev *dev = opaque;
83 dev->ioport_data = data;
84}
85
86static uint64_t test_ioport_read(void *opaque, hwaddr addr, unsigned len)
87{
88 PCTestdev *dev = opaque;
89 return dev->ioport_data;
90}
91
92static const MemoryRegionOps test_ioport_ops = {
93 .read = test_ioport_read,
94 .write = test_ioport_write,
95 .endianness = DEVICE_LITTLE_ENDIAN,
96};
97
98static void test_flush_page(void *opaque, hwaddr addr, uint64_t data,
99 unsigned len)
100{
101 hwaddr page = 4096;
102 void *a = cpu_physical_memory_map(data & ~0xffful, &page, 0);
103
104
105
106#if defined(CONFIG_POSIX)
107 mprotect(a, page, PROT_NONE);
108 mprotect(a, page, PROT_READ|PROT_WRITE);
109#endif
110 cpu_physical_memory_unmap(a, page, 0, 0);
111}
112
113static const MemoryRegionOps test_flush_ops = {
114 .write = test_flush_page,
115 .valid.min_access_size = 4,
116 .valid.max_access_size = 4,
117 .endianness = DEVICE_LITTLE_ENDIAN,
118};
119
120static uint64_t test_iomem_read(void *opaque, hwaddr addr, unsigned len)
121{
122 PCTestdev *dev = opaque;
123 uint64_t ret = 0;
124 memcpy(&ret, &dev->iomem_buf[addr], len);
125 ret = le64_to_cpu(ret);
126
127 return ret;
128}
129
130static void test_iomem_write(void *opaque, hwaddr addr, uint64_t val,
131 unsigned len)
132{
133 PCTestdev *dev = opaque;
134 val = cpu_to_le64(val);
135 memcpy(&dev->iomem_buf[addr], &val, len);
136 dev->iomem_buf[addr] = val;
137}
138
139static const MemoryRegionOps test_iomem_ops = {
140 .read = test_iomem_read,
141 .write = test_iomem_write,
142 .endianness = DEVICE_LITTLE_ENDIAN,
143};
144
145static int init_test_device(ISADevice *isa)
146{
147 PCTestdev *dev = TESTDEV(isa);
148 MemoryRegion *mem = isa_address_space(isa);
149 MemoryRegion *io = isa_address_space_io(isa);
150
151 memory_region_init_io(&dev->ioport, &test_ioport_ops, dev,
152 "pc-testdev-ioport", 4);
153 memory_region_init_io(&dev->flush, &test_flush_ops, dev,
154 "pc-testdev-flush-page", 4);
155 memory_region_init_io(&dev->irq, &test_irq_ops, dev,
156 "pc-testdev-irq-line", 24);
157 memory_region_init_io(&dev->iomem, &test_iomem_ops, dev,
158 "pc-testdev-iomem", IOMEM_LEN);
159
160 memory_region_add_subregion(io, 0xe0, &dev->ioport);
161 memory_region_add_subregion(io, 0xe4, &dev->flush);
162 memory_region_add_subregion(io, 0x2000, &dev->irq);
163 memory_region_add_subregion(mem, 0xff000000, &dev->iomem);
164
165 return 0;
166}
167
168static void testdev_class_init(ObjectClass *klass, void *data)
169{
170 ISADeviceClass *k = ISA_DEVICE_CLASS(klass);
171
172 k->init = init_test_device;
173}
174
175static const TypeInfo testdev_info = {
176 .name = TYPE_TESTDEV,
177 .parent = TYPE_ISA_DEVICE,
178 .instance_size = sizeof(PCTestdev),
179 .class_init = testdev_class_init,
180};
181
182static void testdev_register_types(void)
183{
184 type_register_static(&testdev_info);
185}
186
187type_init(testdev_register_types)
188