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"
25 typedef struct PCITestDevHdr
{
36 typedef struct IOTest
{
38 EventNotifier notifier
;
46 #define IOTEST_DATAMATCH 0xFA
47 #define IOTEST_NOMATCH 0xCE
49 #define IOTEST_IOSIZE 128
50 #define IOTEST_MEMSIZE 2048
52 static const char *iotest_test
[] = {
58 static const char *iotest_type
[] = {
63 #define IOTEST_TEST(i) (iotest_test[((i) % ARRAY_SIZE(iotest_test))])
64 #define IOTEST_TYPE(i) (iotest_type[((i) / ARRAY_SIZE(iotest_test))])
65 #define IOTEST_MAX_TEST (ARRAY_SIZE(iotest_test))
66 #define IOTEST_MAX_TYPE (ARRAY_SIZE(iotest_type))
67 #define IOTEST_MAX (IOTEST_MAX_TEST * IOTEST_MAX_TYPE)
75 #define IOTEST_ACCESS_TYPE uint8_t
76 #define IOTEST_ACCESS_WIDTH (sizeof(uint8_t))
78 typedef struct PCITestDevState
{
89 #define TYPE_PCI_TEST_DEV "pci-testdev"
91 #define PCI_TEST_DEV(obj) \
92 OBJECT_CHECK(PCITestDevState, (obj), TYPE_PCI_TEST_DEV)
94 #define IOTEST_IS_MEM(i) (strcmp(IOTEST_TYPE(i), "portio"))
95 #define IOTEST_REGION(d, i) (IOTEST_IS_MEM(i) ? &(d)->mmio : &(d)->portio)
96 #define IOTEST_SIZE(i) (IOTEST_IS_MEM(i) ? IOTEST_MEMSIZE : IOTEST_IOSIZE)
97 #define IOTEST_PCI_BAR(i) (IOTEST_IS_MEM(i) ? PCI_BASE_ADDRESS_SPACE_MEMORY : \
98 PCI_BASE_ADDRESS_SPACE_IO)
100 static int pci_testdev_start(IOTest
*test
)
102 test
->hdr
->count
= 0;
103 if (!test
->hasnotifier
) {
106 event_notifier_test_and_clear(&test
->notifier
);
107 memory_region_add_eventfd(test
->mr
,
108 le32_to_cpu(test
->hdr
->offset
),
116 static void pci_testdev_stop(IOTest
*test
)
118 if (!test
->hasnotifier
) {
121 memory_region_del_eventfd(test
->mr
,
122 le32_to_cpu(test
->hdr
->offset
),
130 pci_testdev_reset(PCITestDevState
*d
)
132 if (d
->current
== -1) {
135 pci_testdev_stop(&d
->tests
[d
->current
]);
139 static void pci_testdev_inc(IOTest
*test
, unsigned inc
)
141 uint32_t c
= le32_to_cpu(test
->hdr
->count
);
142 test
->hdr
->count
= cpu_to_le32(c
+ inc
);
146 pci_testdev_write(void *opaque
, hwaddr addr
, uint64_t val
,
147 unsigned size
, int type
)
149 PCITestDevState
*d
= opaque
;
153 if (addr
== offsetof(PCITestDevHdr
, test
)) {
154 pci_testdev_reset(d
);
155 if (val
>= IOTEST_MAX_TEST
) {
158 t
= type
* IOTEST_MAX_TEST
+ val
;
159 r
= pci_testdev_start(&d
->tests
[t
]);
166 if (d
->current
< 0) {
169 test
= &d
->tests
[d
->current
];
170 if (addr
!= le32_to_cpu(test
->hdr
->offset
)) {
173 if (test
->match_data
&& test
->size
!= size
) {
176 if (test
->match_data
&& val
!= test
->hdr
->data
) {
179 pci_testdev_inc(test
, 1);
183 pci_testdev_read(void *opaque
, hwaddr addr
, unsigned size
)
185 PCITestDevState
*d
= opaque
;
188 if (d
->current
< 0) {
191 test
= &d
->tests
[d
->current
];
192 buf
= (const char *)test
->hdr
;
193 if (addr
+ size
>= test
->bufsize
) {
196 if (test
->hasnotifier
) {
197 event_notifier_test_and_clear(&test
->notifier
);
203 pci_testdev_mmio_write(void *opaque
, hwaddr addr
, uint64_t val
,
206 pci_testdev_write(opaque
, addr
, val
, size
, 0);
210 pci_testdev_pio_write(void *opaque
, hwaddr addr
, uint64_t val
,
213 pci_testdev_write(opaque
, addr
, val
, size
, 1);
216 static const MemoryRegionOps pci_testdev_mmio_ops
= {
217 .read
= pci_testdev_read
,
218 .write
= pci_testdev_mmio_write
,
219 .endianness
= DEVICE_LITTLE_ENDIAN
,
221 .min_access_size
= 1,
222 .max_access_size
= 1,
226 static const MemoryRegionOps pci_testdev_pio_ops
= {
227 .read
= pci_testdev_read
,
228 .write
= pci_testdev_pio_write
,
229 .endianness
= DEVICE_LITTLE_ENDIAN
,
231 .min_access_size
= 1,
232 .max_access_size
= 1,
236 static void pci_testdev_realize(PCIDevice
*pci_dev
, Error
**errp
)
238 PCITestDevState
*d
= PCI_TEST_DEV(pci_dev
);
242 bool fastmmio
= kvm_ioeventfd_any_length_enabled();
244 pci_conf
= pci_dev
->config
;
246 pci_conf
[PCI_INTERRUPT_PIN
] = 0; /* no interrupt pin */
248 memory_region_init_io(&d
->mmio
, OBJECT(d
), &pci_testdev_mmio_ops
, d
,
249 "pci-testdev-mmio", IOTEST_MEMSIZE
* 2);
250 memory_region_init_io(&d
->portio
, OBJECT(d
), &pci_testdev_pio_ops
, d
,
251 "pci-testdev-portio", IOTEST_IOSIZE
* 2);
252 pci_register_bar(pci_dev
, 0, PCI_BASE_ADDRESS_SPACE_MEMORY
, &d
->mmio
);
253 pci_register_bar(pci_dev
, 1, PCI_BASE_ADDRESS_SPACE_IO
, &d
->portio
);
256 d
->tests
= g_malloc0(IOTEST_MAX
* sizeof *d
->tests
);
257 for (i
= 0; i
< IOTEST_MAX
; ++i
) {
258 IOTest
*test
= &d
->tests
[i
];
259 name
= g_strdup_printf("%s-%s", IOTEST_TYPE(i
), IOTEST_TEST(i
));
260 test
->bufsize
= sizeof(PCITestDevHdr
) + strlen(name
) + 1;
261 test
->hdr
= g_malloc0(test
->bufsize
);
262 memcpy(test
->hdr
->name
, name
, strlen(name
) + 1);
264 test
->hdr
->offset
= cpu_to_le32(IOTEST_SIZE(i
) + i
* IOTEST_ACCESS_WIDTH
);
265 test
->match_data
= strcmp(IOTEST_TEST(i
), "wildcard-eventfd");
266 if (fastmmio
&& IOTEST_IS_MEM(i
) && !test
->match_data
) {
269 test
->size
= IOTEST_ACCESS_WIDTH
;
272 test
->hdr
->data
= test
->match_data
? IOTEST_DATAMATCH
: IOTEST_NOMATCH
;
273 test
->hdr
->width
= IOTEST_ACCESS_WIDTH
;
274 test
->mr
= IOTEST_REGION(d
, i
);
275 if (!strcmp(IOTEST_TEST(i
), "no-eventfd")) {
276 test
->hasnotifier
= false;
279 r
= event_notifier_init(&test
->notifier
, 0);
281 test
->hasnotifier
= true;
286 pci_testdev_uninit(PCIDevice
*dev
)
288 PCITestDevState
*d
= PCI_TEST_DEV(dev
);
291 pci_testdev_reset(d
);
292 for (i
= 0; i
< IOTEST_MAX
; ++i
) {
293 if (d
->tests
[i
].hasnotifier
) {
294 event_notifier_cleanup(&d
->tests
[i
].notifier
);
296 g_free(d
->tests
[i
].hdr
);
301 static void qdev_pci_testdev_reset(DeviceState
*dev
)
303 PCITestDevState
*d
= PCI_TEST_DEV(dev
);
304 pci_testdev_reset(d
);
307 static void pci_testdev_class_init(ObjectClass
*klass
, void *data
)
309 DeviceClass
*dc
= DEVICE_CLASS(klass
);
310 PCIDeviceClass
*k
= PCI_DEVICE_CLASS(klass
);
312 k
->realize
= pci_testdev_realize
;
313 k
->exit
= pci_testdev_uninit
;
314 k
->vendor_id
= PCI_VENDOR_ID_REDHAT
;
315 k
->device_id
= PCI_DEVICE_ID_REDHAT_TEST
;
317 k
->class_id
= PCI_CLASS_OTHERS
;
318 dc
->desc
= "PCI Test Device";
319 set_bit(DEVICE_CATEGORY_MISC
, dc
->categories
);
320 dc
->reset
= qdev_pci_testdev_reset
;
323 static const TypeInfo pci_testdev_info
= {
324 .name
= TYPE_PCI_TEST_DEV
,
325 .parent
= TYPE_PCI_DEVICE
,
326 .instance_size
= sizeof(PCITestDevState
),
327 .class_init
= pci_testdev_class_init
,
330 static void pci_testdev_register_types(void)
332 type_register_static(&pci_testdev_info
);
335 type_init(pci_testdev_register_types
)