xref: /openbmc/qemu/hw/misc/pci-testdev.c (revision 650d103d)
1 /*
2  * QEMU PCI test device
3  *
4  * Copyright (c) 2012 Red Hat Inc.
5  * Author: Michael S. Tsirkin <mst@redhat.com>
6  *
7  * This program is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 2 of the License, or
10  * (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License along
18  * with this program; if not, see <http://www.gnu.org/licenses/>.
19  */
20 
21 #include "qemu/osdep.h"
22 #include "hw/pci/pci.h"
23 #include "qemu/event_notifier.h"
24 #include "qemu/module.h"
25 #include "sysemu/kvm.h"
26 
27 typedef struct PCITestDevHdr {
28     uint8_t test;
29     uint8_t width;
30     uint8_t pad0[2];
31     uint32_t offset;
32     uint8_t data;
33     uint8_t pad1[3];
34     uint32_t count;
35     uint8_t name[];
36 } PCITestDevHdr;
37 
38 typedef struct IOTest {
39     MemoryRegion *mr;
40     EventNotifier notifier;
41     bool hasnotifier;
42     unsigned size;
43     bool match_data;
44     PCITestDevHdr *hdr;
45     unsigned bufsize;
46 } IOTest;
47 
48 #define IOTEST_DATAMATCH 0xFA
49 #define IOTEST_NOMATCH   0xCE
50 
51 #define IOTEST_IOSIZE 128
52 #define IOTEST_MEMSIZE 2048
53 
54 static const char *iotest_test[] = {
55     "no-eventfd",
56     "wildcard-eventfd",
57     "datamatch-eventfd"
58 };
59 
60 static const char *iotest_type[] = {
61     "mmio",
62     "portio"
63 };
64 
65 #define IOTEST_TEST(i) (iotest_test[((i) % ARRAY_SIZE(iotest_test))])
66 #define IOTEST_TYPE(i) (iotest_type[((i) / ARRAY_SIZE(iotest_test))])
67 #define IOTEST_MAX_TEST (ARRAY_SIZE(iotest_test))
68 #define IOTEST_MAX_TYPE (ARRAY_SIZE(iotest_type))
69 #define IOTEST_MAX (IOTEST_MAX_TEST * IOTEST_MAX_TYPE)
70 
71 enum {
72     IOTEST_ACCESS_NAME,
73     IOTEST_ACCESS_DATA,
74     IOTEST_ACCESS_MAX,
75 };
76 
77 #define IOTEST_ACCESS_TYPE uint8_t
78 #define IOTEST_ACCESS_WIDTH (sizeof(uint8_t))
79 
80 typedef struct PCITestDevState {
81     /*< private >*/
82     PCIDevice parent_obj;
83     /*< public >*/
84 
85     MemoryRegion mmio;
86     MemoryRegion portio;
87     IOTest *tests;
88     int current;
89 
90     uint64_t membar_size;
91     MemoryRegion membar;
92 } PCITestDevState;
93 
94 #define TYPE_PCI_TEST_DEV "pci-testdev"
95 
96 #define PCI_TEST_DEV(obj) \
97     OBJECT_CHECK(PCITestDevState, (obj), TYPE_PCI_TEST_DEV)
98 
99 #define IOTEST_IS_MEM(i) (strcmp(IOTEST_TYPE(i), "portio"))
100 #define IOTEST_REGION(d, i) (IOTEST_IS_MEM(i) ?  &(d)->mmio : &(d)->portio)
101 #define IOTEST_SIZE(i) (IOTEST_IS_MEM(i) ? IOTEST_MEMSIZE : IOTEST_IOSIZE)
102 #define IOTEST_PCI_BAR(i) (IOTEST_IS_MEM(i) ? PCI_BASE_ADDRESS_SPACE_MEMORY : \
103                            PCI_BASE_ADDRESS_SPACE_IO)
104 
105 static int pci_testdev_start(IOTest *test)
106 {
107     test->hdr->count = 0;
108     if (!test->hasnotifier) {
109         return 0;
110     }
111     event_notifier_test_and_clear(&test->notifier);
112     memory_region_add_eventfd(test->mr,
113                               le32_to_cpu(test->hdr->offset),
114                               test->size,
115                               test->match_data,
116                               test->hdr->data,
117                               &test->notifier);
118     return 0;
119 }
120 
121 static void pci_testdev_stop(IOTest *test)
122 {
123     if (!test->hasnotifier) {
124         return;
125     }
126     memory_region_del_eventfd(test->mr,
127                               le32_to_cpu(test->hdr->offset),
128                               test->size,
129                               test->match_data,
130                               test->hdr->data,
131                               &test->notifier);
132 }
133 
134 static void
135 pci_testdev_reset(PCITestDevState *d)
136 {
137     if (d->current == -1) {
138         return;
139     }
140     pci_testdev_stop(&d->tests[d->current]);
141     d->current = -1;
142 }
143 
144 static void pci_testdev_inc(IOTest *test, unsigned inc)
145 {
146     uint32_t c = le32_to_cpu(test->hdr->count);
147     test->hdr->count = cpu_to_le32(c + inc);
148 }
149 
150 static void
151 pci_testdev_write(void *opaque, hwaddr addr, uint64_t val,
152                   unsigned size, int type)
153 {
154     PCITestDevState *d = opaque;
155     IOTest *test;
156     int t, r;
157 
158     if (addr == offsetof(PCITestDevHdr, test)) {
159         pci_testdev_reset(d);
160         if (val >= IOTEST_MAX_TEST) {
161             return;
162         }
163         t = type * IOTEST_MAX_TEST + val;
164         r = pci_testdev_start(&d->tests[t]);
165         if (r < 0) {
166             return;
167         }
168         d->current = t;
169         return;
170     }
171     if (d->current < 0) {
172         return;
173     }
174     test = &d->tests[d->current];
175     if (addr != le32_to_cpu(test->hdr->offset)) {
176         return;
177     }
178     if (test->match_data && test->size != size) {
179         return;
180     }
181     if (test->match_data && val != test->hdr->data) {
182         return;
183     }
184     pci_testdev_inc(test, 1);
185 }
186 
187 static uint64_t
188 pci_testdev_read(void *opaque, hwaddr addr, unsigned size)
189 {
190     PCITestDevState *d = opaque;
191     const char *buf;
192     IOTest *test;
193     if (d->current < 0) {
194         return 0;
195     }
196     test = &d->tests[d->current];
197     buf = (const char *)test->hdr;
198     if (addr + size >= test->bufsize) {
199         return 0;
200     }
201     if (test->hasnotifier) {
202         event_notifier_test_and_clear(&test->notifier);
203     }
204     return buf[addr];
205 }
206 
207 static void
208 pci_testdev_mmio_write(void *opaque, hwaddr addr, uint64_t val,
209                        unsigned size)
210 {
211     pci_testdev_write(opaque, addr, val, size, 0);
212 }
213 
214 static void
215 pci_testdev_pio_write(void *opaque, hwaddr addr, uint64_t val,
216                        unsigned size)
217 {
218     pci_testdev_write(opaque, addr, val, size, 1);
219 }
220 
221 static const MemoryRegionOps pci_testdev_mmio_ops = {
222     .read = pci_testdev_read,
223     .write = pci_testdev_mmio_write,
224     .endianness = DEVICE_LITTLE_ENDIAN,
225     .impl = {
226         .min_access_size = 1,
227         .max_access_size = 1,
228     },
229 };
230 
231 static const MemoryRegionOps pci_testdev_pio_ops = {
232     .read = pci_testdev_read,
233     .write = pci_testdev_pio_write,
234     .endianness = DEVICE_LITTLE_ENDIAN,
235     .impl = {
236         .min_access_size = 1,
237         .max_access_size = 1,
238     },
239 };
240 
241 static void pci_testdev_realize(PCIDevice *pci_dev, Error **errp)
242 {
243     PCITestDevState *d = PCI_TEST_DEV(pci_dev);
244     uint8_t *pci_conf;
245     char *name;
246     int r, i;
247     bool fastmmio = kvm_ioeventfd_any_length_enabled();
248 
249     pci_conf = pci_dev->config;
250 
251     pci_conf[PCI_INTERRUPT_PIN] = 0; /* no interrupt pin */
252 
253     memory_region_init_io(&d->mmio, OBJECT(d), &pci_testdev_mmio_ops, d,
254                           "pci-testdev-mmio", IOTEST_MEMSIZE * 2);
255     memory_region_init_io(&d->portio, OBJECT(d), &pci_testdev_pio_ops, d,
256                           "pci-testdev-portio", IOTEST_IOSIZE * 2);
257     pci_register_bar(pci_dev, 0, PCI_BASE_ADDRESS_SPACE_MEMORY, &d->mmio);
258     pci_register_bar(pci_dev, 1, PCI_BASE_ADDRESS_SPACE_IO, &d->portio);
259 
260     if (d->membar_size) {
261         memory_region_init(&d->membar, OBJECT(d), "pci-testdev-membar",
262                            d->membar_size);
263         pci_register_bar(pci_dev, 2,
264                          PCI_BASE_ADDRESS_SPACE_MEMORY |
265                          PCI_BASE_ADDRESS_MEM_PREFETCH |
266                          PCI_BASE_ADDRESS_MEM_TYPE_64,
267                          &d->membar);
268     }
269 
270     d->current = -1;
271     d->tests = g_malloc0(IOTEST_MAX * sizeof *d->tests);
272     for (i = 0; i < IOTEST_MAX; ++i) {
273         IOTest *test = &d->tests[i];
274         name = g_strdup_printf("%s-%s", IOTEST_TYPE(i), IOTEST_TEST(i));
275         test->bufsize = sizeof(PCITestDevHdr) + strlen(name) + 1;
276         test->hdr = g_malloc0(test->bufsize);
277         memcpy(test->hdr->name, name, strlen(name) + 1);
278         g_free(name);
279         test->hdr->offset = cpu_to_le32(IOTEST_SIZE(i) + i * IOTEST_ACCESS_WIDTH);
280         test->match_data = strcmp(IOTEST_TEST(i), "wildcard-eventfd");
281         if (fastmmio && IOTEST_IS_MEM(i) && !test->match_data) {
282             test->size = 0;
283         } else {
284             test->size = IOTEST_ACCESS_WIDTH;
285         }
286         test->hdr->test = i;
287         test->hdr->data = test->match_data ? IOTEST_DATAMATCH : IOTEST_NOMATCH;
288         test->hdr->width = IOTEST_ACCESS_WIDTH;
289         test->mr = IOTEST_REGION(d, i);
290         if (!strcmp(IOTEST_TEST(i), "no-eventfd")) {
291             test->hasnotifier = false;
292             continue;
293         }
294         r = event_notifier_init(&test->notifier, 0);
295         assert(r >= 0);
296         test->hasnotifier = true;
297     }
298 }
299 
300 static void
301 pci_testdev_uninit(PCIDevice *dev)
302 {
303     PCITestDevState *d = PCI_TEST_DEV(dev);
304     int i;
305 
306     pci_testdev_reset(d);
307     for (i = 0; i < IOTEST_MAX; ++i) {
308         if (d->tests[i].hasnotifier) {
309             event_notifier_cleanup(&d->tests[i].notifier);
310         }
311         g_free(d->tests[i].hdr);
312     }
313     g_free(d->tests);
314 }
315 
316 static void qdev_pci_testdev_reset(DeviceState *dev)
317 {
318     PCITestDevState *d = PCI_TEST_DEV(dev);
319     pci_testdev_reset(d);
320 }
321 
322 static Property pci_testdev_properties[] = {
323     DEFINE_PROP_SIZE("membar", PCITestDevState, membar_size, 0),
324     DEFINE_PROP_END_OF_LIST(),
325 };
326 
327 static void pci_testdev_class_init(ObjectClass *klass, void *data)
328 {
329     DeviceClass *dc = DEVICE_CLASS(klass);
330     PCIDeviceClass *k = PCI_DEVICE_CLASS(klass);
331 
332     k->realize = pci_testdev_realize;
333     k->exit = pci_testdev_uninit;
334     k->vendor_id = PCI_VENDOR_ID_REDHAT;
335     k->device_id = PCI_DEVICE_ID_REDHAT_TEST;
336     k->revision = 0x00;
337     k->class_id = PCI_CLASS_OTHERS;
338     dc->desc = "PCI Test Device";
339     set_bit(DEVICE_CATEGORY_MISC, dc->categories);
340     dc->reset = qdev_pci_testdev_reset;
341     dc->props = pci_testdev_properties;
342 }
343 
344 static const TypeInfo pci_testdev_info = {
345     .name          = TYPE_PCI_TEST_DEV,
346     .parent        = TYPE_PCI_DEVICE,
347     .instance_size = sizeof(PCITestDevState),
348     .class_init    = pci_testdev_class_init,
349     .interfaces = (InterfaceInfo[]) {
350         { INTERFACE_CONVENTIONAL_PCI_DEVICE },
351         { },
352     },
353 };
354 
355 static void pci_testdev_register_types(void)
356 {
357     type_register_static(&pci_testdev_info);
358 }
359 
360 type_init(pci_testdev_register_types)
361