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/>.
21 #include "hw/pci/pci.h"
22 #include "qemu/event_notifier.h"
23 #include "qemu/osdep.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
{
86 #define IOTEST_IS_MEM(i) (strcmp(IOTEST_TYPE(i), "portio"))
87 #define IOTEST_REGION(d, i) (IOTEST_IS_MEM(i) ? &(d)->mmio : &(d)->portio)
88 #define IOTEST_SIZE(i) (IOTEST_IS_MEM(i) ? IOTEST_MEMSIZE : IOTEST_IOSIZE)
89 #define IOTEST_PCI_BAR(i) (IOTEST_IS_MEM(i) ? PCI_BASE_ADDRESS_SPACE_MEMORY : \
90 PCI_BASE_ADDRESS_SPACE_IO)
92 static int pci_testdev_start(IOTest
*test
)
95 if (!test
->hasnotifier
) {
98 event_notifier_test_and_clear(&test
->notifier
);
99 memory_region_add_eventfd(test
->mr
,
100 le32_to_cpu(test
->hdr
->offset
),
108 static void pci_testdev_stop(IOTest
*test
)
110 if (!test
->hasnotifier
) {
113 memory_region_del_eventfd(test
->mr
,
114 le32_to_cpu(test
->hdr
->offset
),
122 pci_testdev_reset(PCITestDevState
*d
)
124 if (d
->current
== -1) {
127 pci_testdev_stop(&d
->tests
[d
->current
]);
131 static void pci_testdev_inc(IOTest
*test
, unsigned inc
)
133 uint32_t c
= le32_to_cpu(test
->hdr
->count
);
134 test
->hdr
->count
= cpu_to_le32(c
+ inc
);
138 pci_testdev_write(void *opaque
, hwaddr addr
, uint64_t val
,
139 unsigned size
, int type
)
141 PCITestDevState
*d
= opaque
;
145 if (addr
== offsetof(PCITestDevHdr
, test
)) {
146 pci_testdev_reset(d
);
147 if (val
>= IOTEST_MAX_TEST
) {
150 t
= type
* IOTEST_MAX_TEST
+ val
;
151 r
= pci_testdev_start(&d
->tests
[t
]);
158 if (d
->current
< 0) {
161 test
= &d
->tests
[d
->current
];
162 if (addr
!= le32_to_cpu(test
->hdr
->offset
)) {
165 if (test
->match_data
&& test
->size
!= size
) {
168 if (test
->match_data
&& val
!= test
->hdr
->data
) {
171 pci_testdev_inc(test
, 1);
175 pci_testdev_read(void *opaque
, hwaddr addr
, unsigned size
)
177 PCITestDevState
*d
= opaque
;
180 if (d
->current
< 0) {
183 test
= &d
->tests
[d
->current
];
184 buf
= (const char *)test
->hdr
;
185 if (addr
+ size
>= test
->bufsize
) {
188 if (test
->hasnotifier
) {
189 event_notifier_test_and_clear(&test
->notifier
);
195 pci_testdev_mmio_write(void *opaque
, hwaddr addr
, uint64_t val
,
198 pci_testdev_write(opaque
, addr
, val
, size
, 0);
202 pci_testdev_pio_write(void *opaque
, hwaddr addr
, uint64_t val
,
205 pci_testdev_write(opaque
, addr
, val
, size
, 1);
208 static const MemoryRegionOps pci_testdev_mmio_ops
= {
209 .read
= pci_testdev_read
,
210 .write
= pci_testdev_mmio_write
,
211 .endianness
= DEVICE_LITTLE_ENDIAN
,
213 .min_access_size
= 1,
214 .max_access_size
= 1,
218 static const MemoryRegionOps pci_testdev_pio_ops
= {
219 .read
= pci_testdev_read
,
220 .write
= pci_testdev_pio_write
,
221 .endianness
= DEVICE_LITTLE_ENDIAN
,
223 .min_access_size
= 1,
224 .max_access_size
= 1,
228 static int pci_testdev_init(PCIDevice
*pci_dev
)
230 PCITestDevState
*d
= DO_UPCAST(PCITestDevState
, dev
, pci_dev
);
235 pci_conf
= d
->dev
.config
;
237 pci_conf
[PCI_INTERRUPT_PIN
] = 0; /* no interrupt pin */
239 memory_region_init_io(&d
->mmio
, &pci_testdev_mmio_ops
, d
,
240 "pci-testdev-mmio", IOTEST_MEMSIZE
* 2);
241 memory_region_init_io(&d
->portio
, &pci_testdev_pio_ops
, d
,
242 "pci-testdev-portio", IOTEST_IOSIZE
* 2);
243 pci_register_bar(&d
->dev
, 0, PCI_BASE_ADDRESS_SPACE_MEMORY
, &d
->mmio
);
244 pci_register_bar(&d
->dev
, 1, PCI_BASE_ADDRESS_SPACE_IO
, &d
->portio
);
247 d
->tests
= g_malloc0(IOTEST_MAX
* sizeof *d
->tests
);
248 for (i
= 0; i
< IOTEST_MAX
; ++i
) {
249 IOTest
*test
= &d
->tests
[i
];
250 name
= g_strdup_printf("%s-%s", IOTEST_TYPE(i
), IOTEST_TEST(i
));
251 test
->bufsize
= sizeof(PCITestDevHdr
) + strlen(name
) + 1;
252 test
->hdr
= g_malloc0(test
->bufsize
);
253 memcpy(test
->hdr
->name
, name
, strlen(name
) + 1);
255 test
->hdr
->offset
= cpu_to_le32(IOTEST_SIZE(i
) + i
* IOTEST_ACCESS_WIDTH
);
256 test
->size
= IOTEST_ACCESS_WIDTH
;
257 test
->match_data
= strcmp(IOTEST_TEST(i
), "wildcard-eventfd");
259 test
->hdr
->data
= test
->match_data
? IOTEST_DATAMATCH
: IOTEST_NOMATCH
;
260 test
->hdr
->width
= IOTEST_ACCESS_WIDTH
;
261 test
->mr
= IOTEST_REGION(d
, i
);
262 if (!strcmp(IOTEST_TEST(i
), "no-eventfd")) {
263 test
->hasnotifier
= false;
266 r
= event_notifier_init(&test
->notifier
, 0);
268 test
->hasnotifier
= true;
275 pci_testdev_uninit(PCIDevice
*dev
)
277 PCITestDevState
*d
= DO_UPCAST(PCITestDevState
, dev
, dev
);
280 pci_testdev_reset(d
);
281 for (i
= 0; i
< IOTEST_MAX
; ++i
) {
282 if (d
->tests
[i
].hasnotifier
) {
283 event_notifier_cleanup(&d
->tests
[i
].notifier
);
285 g_free(d
->tests
[i
].hdr
);
288 memory_region_destroy(&d
->mmio
);
289 memory_region_destroy(&d
->portio
);
292 static void qdev_pci_testdev_reset(DeviceState
*dev
)
294 PCITestDevState
*d
= DO_UPCAST(PCITestDevState
, dev
.qdev
, dev
);
295 pci_testdev_reset(d
);
298 static void pci_testdev_class_init(ObjectClass
*klass
, void *data
)
300 DeviceClass
*dc
= DEVICE_CLASS(klass
);
301 PCIDeviceClass
*k
= PCI_DEVICE_CLASS(klass
);
303 k
->init
= pci_testdev_init
;
304 k
->exit
= pci_testdev_uninit
;
305 k
->vendor_id
= PCI_VENDOR_ID_REDHAT
;
306 k
->device_id
= PCI_DEVICE_ID_REDHAT_TEST
;
308 k
->class_id
= PCI_CLASS_OTHERS
;
309 dc
->desc
= "PCI Test Device";
310 dc
->reset
= qdev_pci_testdev_reset
;
313 static const TypeInfo pci_testdev_info
= {
314 .name
= "pci-testdev",
315 .parent
= TYPE_PCI_DEVICE
,
316 .instance_size
= sizeof(PCITestDevState
),
317 .class_init
= pci_testdev_class_init
,
320 static void pci_testdev_register_types(void)
322 type_register_static(&pci_testdev_info
);
325 type_init(pci_testdev_register_types
)