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 #include "hw/hw.h" 21 #include "hw/pci/pci.h" 22 #include "qemu/event_notifier.h" 23 #include "qemu/osdep.h" 24 25 typedef struct PCITestDevHdr { 26 uint8_t test; 27 uint8_t width; 28 uint8_t pad0[2]; 29 uint32_t offset; 30 uint8_t data; 31 uint8_t pad1[3]; 32 uint32_t count; 33 uint8_t name[]; 34 } PCITestDevHdr; 35 36 typedef struct IOTest { 37 MemoryRegion *mr; 38 EventNotifier notifier; 39 bool hasnotifier; 40 unsigned size; 41 bool match_data; 42 PCITestDevHdr *hdr; 43 unsigned bufsize; 44 } IOTest; 45 46 #define IOTEST_DATAMATCH 0xFA 47 #define IOTEST_NOMATCH 0xCE 48 49 #define IOTEST_IOSIZE 128 50 #define IOTEST_MEMSIZE 2048 51 52 static const char *iotest_test[] = { 53 "no-eventfd", 54 "wildcard-eventfd", 55 "datamatch-eventfd" 56 }; 57 58 static const char *iotest_type[] = { 59 "mmio", 60 "portio" 61 }; 62 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) 68 69 enum { 70 IOTEST_ACCESS_NAME, 71 IOTEST_ACCESS_DATA, 72 IOTEST_ACCESS_MAX, 73 }; 74 75 #define IOTEST_ACCESS_TYPE uint8_t 76 #define IOTEST_ACCESS_WIDTH (sizeof(uint8_t)) 77 78 typedef struct PCITestDevState { 79 PCIDevice dev; 80 MemoryRegion mmio; 81 MemoryRegion portio; 82 IOTest *tests; 83 int current; 84 } PCITestDevState; 85 86 #define TYPE_PCI_TEST_DEV "pci-testdev" 87 88 #define PCI_TEST_DEV(obj) \ 89 OBJECT_CHECK(PCITestDevState, (obj), TYPE_PCI_TEST_DEV) 90 91 #define IOTEST_IS_MEM(i) (strcmp(IOTEST_TYPE(i), "portio")) 92 #define IOTEST_REGION(d, i) (IOTEST_IS_MEM(i) ? &(d)->mmio : &(d)->portio) 93 #define IOTEST_SIZE(i) (IOTEST_IS_MEM(i) ? IOTEST_MEMSIZE : IOTEST_IOSIZE) 94 #define IOTEST_PCI_BAR(i) (IOTEST_IS_MEM(i) ? PCI_BASE_ADDRESS_SPACE_MEMORY : \ 95 PCI_BASE_ADDRESS_SPACE_IO) 96 97 static int pci_testdev_start(IOTest *test) 98 { 99 test->hdr->count = 0; 100 if (!test->hasnotifier) { 101 return 0; 102 } 103 event_notifier_test_and_clear(&test->notifier); 104 memory_region_add_eventfd(test->mr, 105 le32_to_cpu(test->hdr->offset), 106 test->size, 107 test->match_data, 108 test->hdr->data, 109 &test->notifier); 110 return 0; 111 } 112 113 static void pci_testdev_stop(IOTest *test) 114 { 115 if (!test->hasnotifier) { 116 return; 117 } 118 memory_region_del_eventfd(test->mr, 119 le32_to_cpu(test->hdr->offset), 120 test->size, 121 test->match_data, 122 test->hdr->data, 123 &test->notifier); 124 } 125 126 static void 127 pci_testdev_reset(PCITestDevState *d) 128 { 129 if (d->current == -1) { 130 return; 131 } 132 pci_testdev_stop(&d->tests[d->current]); 133 d->current = -1; 134 } 135 136 static void pci_testdev_inc(IOTest *test, unsigned inc) 137 { 138 uint32_t c = le32_to_cpu(test->hdr->count); 139 test->hdr->count = cpu_to_le32(c + inc); 140 } 141 142 static void 143 pci_testdev_write(void *opaque, hwaddr addr, uint64_t val, 144 unsigned size, int type) 145 { 146 PCITestDevState *d = opaque; 147 IOTest *test; 148 int t, r; 149 150 if (addr == offsetof(PCITestDevHdr, test)) { 151 pci_testdev_reset(d); 152 if (val >= IOTEST_MAX_TEST) { 153 return; 154 } 155 t = type * IOTEST_MAX_TEST + val; 156 r = pci_testdev_start(&d->tests[t]); 157 if (r < 0) { 158 return; 159 } 160 d->current = t; 161 return; 162 } 163 if (d->current < 0) { 164 return; 165 } 166 test = &d->tests[d->current]; 167 if (addr != le32_to_cpu(test->hdr->offset)) { 168 return; 169 } 170 if (test->match_data && test->size != size) { 171 return; 172 } 173 if (test->match_data && val != test->hdr->data) { 174 return; 175 } 176 pci_testdev_inc(test, 1); 177 } 178 179 static uint64_t 180 pci_testdev_read(void *opaque, hwaddr addr, unsigned size) 181 { 182 PCITestDevState *d = opaque; 183 const char *buf; 184 IOTest *test; 185 if (d->current < 0) { 186 return 0; 187 } 188 test = &d->tests[d->current]; 189 buf = (const char *)test->hdr; 190 if (addr + size >= test->bufsize) { 191 return 0; 192 } 193 if (test->hasnotifier) { 194 event_notifier_test_and_clear(&test->notifier); 195 } 196 return buf[addr]; 197 } 198 199 static void 200 pci_testdev_mmio_write(void *opaque, hwaddr addr, uint64_t val, 201 unsigned size) 202 { 203 pci_testdev_write(opaque, addr, val, size, 0); 204 } 205 206 static void 207 pci_testdev_pio_write(void *opaque, hwaddr addr, uint64_t val, 208 unsigned size) 209 { 210 pci_testdev_write(opaque, addr, val, size, 1); 211 } 212 213 static const MemoryRegionOps pci_testdev_mmio_ops = { 214 .read = pci_testdev_read, 215 .write = pci_testdev_mmio_write, 216 .endianness = DEVICE_LITTLE_ENDIAN, 217 .impl = { 218 .min_access_size = 1, 219 .max_access_size = 1, 220 }, 221 }; 222 223 static const MemoryRegionOps pci_testdev_pio_ops = { 224 .read = pci_testdev_read, 225 .write = pci_testdev_pio_write, 226 .endianness = DEVICE_LITTLE_ENDIAN, 227 .impl = { 228 .min_access_size = 1, 229 .max_access_size = 1, 230 }, 231 }; 232 233 static int pci_testdev_init(PCIDevice *pci_dev) 234 { 235 PCITestDevState *d = PCI_TEST_DEV(pci_dev); 236 uint8_t *pci_conf; 237 char *name; 238 int r, i; 239 240 pci_conf = d->dev.config; 241 242 pci_conf[PCI_INTERRUPT_PIN] = 0; /* no interrupt pin */ 243 244 memory_region_init_io(&d->mmio, OBJECT(d), &pci_testdev_mmio_ops, d, 245 "pci-testdev-mmio", IOTEST_MEMSIZE * 2); 246 memory_region_init_io(&d->portio, OBJECT(d), &pci_testdev_pio_ops, d, 247 "pci-testdev-portio", IOTEST_IOSIZE * 2); 248 pci_register_bar(&d->dev, 0, PCI_BASE_ADDRESS_SPACE_MEMORY, &d->mmio); 249 pci_register_bar(&d->dev, 1, PCI_BASE_ADDRESS_SPACE_IO, &d->portio); 250 251 d->current = -1; 252 d->tests = g_malloc0(IOTEST_MAX * sizeof *d->tests); 253 for (i = 0; i < IOTEST_MAX; ++i) { 254 IOTest *test = &d->tests[i]; 255 name = g_strdup_printf("%s-%s", IOTEST_TYPE(i), IOTEST_TEST(i)); 256 test->bufsize = sizeof(PCITestDevHdr) + strlen(name) + 1; 257 test->hdr = g_malloc0(test->bufsize); 258 memcpy(test->hdr->name, name, strlen(name) + 1); 259 g_free(name); 260 test->hdr->offset = cpu_to_le32(IOTEST_SIZE(i) + i * IOTEST_ACCESS_WIDTH); 261 test->size = IOTEST_ACCESS_WIDTH; 262 test->match_data = strcmp(IOTEST_TEST(i), "wildcard-eventfd"); 263 test->hdr->test = i; 264 test->hdr->data = test->match_data ? IOTEST_DATAMATCH : IOTEST_NOMATCH; 265 test->hdr->width = IOTEST_ACCESS_WIDTH; 266 test->mr = IOTEST_REGION(d, i); 267 if (!strcmp(IOTEST_TEST(i), "no-eventfd")) { 268 test->hasnotifier = false; 269 continue; 270 } 271 r = event_notifier_init(&test->notifier, 0); 272 assert(r >= 0); 273 test->hasnotifier = true; 274 } 275 276 return 0; 277 } 278 279 static void 280 pci_testdev_uninit(PCIDevice *dev) 281 { 282 PCITestDevState *d = PCI_TEST_DEV(dev); 283 int i; 284 285 pci_testdev_reset(d); 286 for (i = 0; i < IOTEST_MAX; ++i) { 287 if (d->tests[i].hasnotifier) { 288 event_notifier_cleanup(&d->tests[i].notifier); 289 } 290 g_free(d->tests[i].hdr); 291 } 292 g_free(d->tests); 293 memory_region_destroy(&d->mmio); 294 memory_region_destroy(&d->portio); 295 } 296 297 static void qdev_pci_testdev_reset(DeviceState *dev) 298 { 299 PCITestDevState *d = PCI_TEST_DEV(dev); 300 pci_testdev_reset(d); 301 } 302 303 static void pci_testdev_class_init(ObjectClass *klass, void *data) 304 { 305 DeviceClass *dc = DEVICE_CLASS(klass); 306 PCIDeviceClass *k = PCI_DEVICE_CLASS(klass); 307 308 k->init = pci_testdev_init; 309 k->exit = pci_testdev_uninit; 310 k->vendor_id = PCI_VENDOR_ID_REDHAT; 311 k->device_id = PCI_DEVICE_ID_REDHAT_TEST; 312 k->revision = 0x00; 313 k->class_id = PCI_CLASS_OTHERS; 314 dc->desc = "PCI Test Device"; 315 dc->reset = qdev_pci_testdev_reset; 316 } 317 318 static const TypeInfo pci_testdev_info = { 319 .name = TYPE_PCI_TEST_DEV, 320 .parent = TYPE_PCI_DEVICE, 321 .instance_size = sizeof(PCITestDevState), 322 .class_init = pci_testdev_class_init, 323 }; 324 325 static void pci_testdev_register_types(void) 326 { 327 type_register_static(&pci_testdev_info); 328 } 329 330 type_init(pci_testdev_register_types) 331