4 * Copyright (c) 2012 Red Hat Inc.
5 * Author: Michael S. Tsirkin <mst@redhat.com>
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.
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.
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/>.
20 #include "qemu/osdep.h"
22 #include "hw/pci/pci.h"
23 #include "qemu/event_notifier.h"
24 #include "sysemu/kvm.h"
26 typedef struct PCITestDevHdr
{
37 typedef struct IOTest
{
39 EventNotifier notifier
;
47 #define IOTEST_DATAMATCH 0xFA
48 #define IOTEST_NOMATCH 0xCE
50 #define IOTEST_IOSIZE 128
51 #define IOTEST_MEMSIZE 2048
53 static const char *iotest_test
[] = {
59 static const char *iotest_type
[] = {
64 #define IOTEST_TEST(i) (iotest_test[((i) % ARRAY_SIZE(iotest_test))])
65 #define IOTEST_TYPE(i) (iotest_type[((i) / ARRAY_SIZE(iotest_test))])
66 #define IOTEST_MAX_TEST (ARRAY_SIZE(iotest_test))
67 #define IOTEST_MAX_TYPE (ARRAY_SIZE(iotest_type))
68 #define IOTEST_MAX (IOTEST_MAX_TEST * IOTEST_MAX_TYPE)
76 #define IOTEST_ACCESS_TYPE uint8_t
77 #define IOTEST_ACCESS_WIDTH (sizeof(uint8_t))
79 typedef struct PCITestDevState
{
90 #define TYPE_PCI_TEST_DEV "pci-testdev"
92 #define PCI_TEST_DEV(obj) \
93 OBJECT_CHECK(PCITestDevState, (obj), TYPE_PCI_TEST_DEV)
95 #define IOTEST_IS_MEM(i) (strcmp(IOTEST_TYPE(i), "portio"))
96 #define IOTEST_REGION(d, i) (IOTEST_IS_MEM(i) ? &(d)->mmio : &(d)->portio)
97 #define IOTEST_SIZE(i) (IOTEST_IS_MEM(i) ? IOTEST_MEMSIZE : IOTEST_IOSIZE)
98 #define IOTEST_PCI_BAR(i) (IOTEST_IS_MEM(i) ? PCI_BASE_ADDRESS_SPACE_MEMORY : \
99 PCI_BASE_ADDRESS_SPACE_IO)
101 static int pci_testdev_start(IOTest
*test
)
103 test
->hdr
->count
= 0;
104 if (!test
->hasnotifier
) {
107 event_notifier_test_and_clear(&test
->notifier
);
108 memory_region_add_eventfd(test
->mr
,
109 le32_to_cpu(test
->hdr
->offset
),
117 static void pci_testdev_stop(IOTest
*test
)
119 if (!test
->hasnotifier
) {
122 memory_region_del_eventfd(test
->mr
,
123 le32_to_cpu(test
->hdr
->offset
),
131 pci_testdev_reset(PCITestDevState
*d
)
133 if (d
->current
== -1) {
136 pci_testdev_stop(&d
->tests
[d
->current
]);
140 static void pci_testdev_inc(IOTest
*test
, unsigned inc
)
142 uint32_t c
= le32_to_cpu(test
->hdr
->count
);
143 test
->hdr
->count
= cpu_to_le32(c
+ inc
);
147 pci_testdev_write(void *opaque
, hwaddr addr
, uint64_t val
,
148 unsigned size
, int type
)
150 PCITestDevState
*d
= opaque
;
154 if (addr
== offsetof(PCITestDevHdr
, test
)) {
155 pci_testdev_reset(d
);
156 if (val
>= IOTEST_MAX_TEST
) {
159 t
= type
* IOTEST_MAX_TEST
+ val
;
160 r
= pci_testdev_start(&d
->tests
[t
]);
167 if (d
->current
< 0) {
170 test
= &d
->tests
[d
->current
];
171 if (addr
!= le32_to_cpu(test
->hdr
->offset
)) {
174 if (test
->match_data
&& test
->size
!= size
) {
177 if (test
->match_data
&& val
!= test
->hdr
->data
) {
180 pci_testdev_inc(test
, 1);
184 pci_testdev_read(void *opaque
, hwaddr addr
, unsigned size
)
186 PCITestDevState
*d
= opaque
;
189 if (d
->current
< 0) {
192 test
= &d
->tests
[d
->current
];
193 buf
= (const char *)test
->hdr
;
194 if (addr
+ size
>= test
->bufsize
) {
197 if (test
->hasnotifier
) {
198 event_notifier_test_and_clear(&test
->notifier
);
204 pci_testdev_mmio_write(void *opaque
, hwaddr addr
, uint64_t val
,
207 pci_testdev_write(opaque
, addr
, val
, size
, 0);
211 pci_testdev_pio_write(void *opaque
, hwaddr addr
, uint64_t val
,
214 pci_testdev_write(opaque
, addr
, val
, size
, 1);
217 static const MemoryRegionOps pci_testdev_mmio_ops
= {
218 .read
= pci_testdev_read
,
219 .write
= pci_testdev_mmio_write
,
220 .endianness
= DEVICE_LITTLE_ENDIAN
,
222 .min_access_size
= 1,
223 .max_access_size
= 1,
227 static const MemoryRegionOps pci_testdev_pio_ops
= {
228 .read
= pci_testdev_read
,
229 .write
= pci_testdev_pio_write
,
230 .endianness
= DEVICE_LITTLE_ENDIAN
,
232 .min_access_size
= 1,
233 .max_access_size
= 1,
237 static void pci_testdev_realize(PCIDevice
*pci_dev
, Error
**errp
)
239 PCITestDevState
*d
= PCI_TEST_DEV(pci_dev
);
243 bool fastmmio
= kvm_ioeventfd_any_length_enabled();
245 pci_conf
= pci_dev
->config
;
247 pci_conf
[PCI_INTERRUPT_PIN
] = 0; /* no interrupt pin */
249 memory_region_init_io(&d
->mmio
, OBJECT(d
), &pci_testdev_mmio_ops
, d
,
250 "pci-testdev-mmio", IOTEST_MEMSIZE
* 2);
251 memory_region_init_io(&d
->portio
, OBJECT(d
), &pci_testdev_pio_ops
, d
,
252 "pci-testdev-portio", IOTEST_IOSIZE
* 2);
253 pci_register_bar(pci_dev
, 0, PCI_BASE_ADDRESS_SPACE_MEMORY
, &d
->mmio
);
254 pci_register_bar(pci_dev
, 1, PCI_BASE_ADDRESS_SPACE_IO
, &d
->portio
);
257 d
->tests
= g_malloc0(IOTEST_MAX
* sizeof *d
->tests
);
258 for (i
= 0; i
< IOTEST_MAX
; ++i
) {
259 IOTest
*test
= &d
->tests
[i
];
260 name
= g_strdup_printf("%s-%s", IOTEST_TYPE(i
), IOTEST_TEST(i
));
261 test
->bufsize
= sizeof(PCITestDevHdr
) + strlen(name
) + 1;
262 test
->hdr
= g_malloc0(test
->bufsize
);
263 memcpy(test
->hdr
->name
, name
, strlen(name
) + 1);
265 test
->hdr
->offset
= cpu_to_le32(IOTEST_SIZE(i
) + i
* IOTEST_ACCESS_WIDTH
);
266 test
->match_data
= strcmp(IOTEST_TEST(i
), "wildcard-eventfd");
267 if (fastmmio
&& IOTEST_IS_MEM(i
) && !test
->match_data
) {
270 test
->size
= IOTEST_ACCESS_WIDTH
;
273 test
->hdr
->data
= test
->match_data
? IOTEST_DATAMATCH
: IOTEST_NOMATCH
;
274 test
->hdr
->width
= IOTEST_ACCESS_WIDTH
;
275 test
->mr
= IOTEST_REGION(d
, i
);
276 if (!strcmp(IOTEST_TEST(i
), "no-eventfd")) {
277 test
->hasnotifier
= false;
280 r
= event_notifier_init(&test
->notifier
, 0);
282 test
->hasnotifier
= true;
287 pci_testdev_uninit(PCIDevice
*dev
)
289 PCITestDevState
*d
= PCI_TEST_DEV(dev
);
292 pci_testdev_reset(d
);
293 for (i
= 0; i
< IOTEST_MAX
; ++i
) {
294 if (d
->tests
[i
].hasnotifier
) {
295 event_notifier_cleanup(&d
->tests
[i
].notifier
);
297 g_free(d
->tests
[i
].hdr
);
302 static void qdev_pci_testdev_reset(DeviceState
*dev
)
304 PCITestDevState
*d
= PCI_TEST_DEV(dev
);
305 pci_testdev_reset(d
);
308 static void pci_testdev_class_init(ObjectClass
*klass
, void *data
)
310 DeviceClass
*dc
= DEVICE_CLASS(klass
);
311 PCIDeviceClass
*k
= PCI_DEVICE_CLASS(klass
);
313 k
->realize
= pci_testdev_realize
;
314 k
->exit
= pci_testdev_uninit
;
315 k
->vendor_id
= PCI_VENDOR_ID_REDHAT
;
316 k
->device_id
= PCI_DEVICE_ID_REDHAT_TEST
;
318 k
->class_id
= PCI_CLASS_OTHERS
;
319 dc
->desc
= "PCI Test Device";
320 set_bit(DEVICE_CATEGORY_MISC
, dc
->categories
);
321 dc
->reset
= qdev_pci_testdev_reset
;
324 static const TypeInfo pci_testdev_info
= {
325 .name
= TYPE_PCI_TEST_DEV
,
326 .parent
= TYPE_PCI_DEVICE
,
327 .instance_size
= sizeof(PCITestDevState
),
328 .class_init
= pci_testdev_class_init
,
329 .interfaces
= (InterfaceInfo
[]) {
330 { INTERFACE_CONVENTIONAL_PCI_DEVICE
},
335 static void pci_testdev_register_types(void)
337 type_register_static(&pci_testdev_info
);
340 type_init(pci_testdev_register_types
)