1 /* 2 * XEN platform pci device, formerly known as the event channel device 3 * 4 * Copyright (c) 2003-2004 Intel Corp. 5 * Copyright (c) 2006 XenSource 6 * 7 * Permission is hereby granted, free of charge, to any person obtaining a copy 8 * of this software and associated documentation files (the "Software"), to deal 9 * in the Software without restriction, including without limitation the rights 10 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 11 * copies of the Software, and to permit persons to whom the Software is 12 * furnished to do so, subject to the following conditions: 13 * 14 * The above copyright notice and this permission notice shall be included in 15 * all copies or substantial portions of the Software. 16 * 17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 18 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 20 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 21 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 22 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 23 * THE SOFTWARE. 24 */ 25 26 #include "qemu/osdep.h" 27 #include "qapi/error.h" 28 #include "hw/ide.h" 29 #include "hw/pci/pci.h" 30 #include "hw/irq.h" 31 #include "hw/xen/xen_common.h" 32 #include "migration/vmstate.h" 33 #include "hw/xen/xen-legacy-backend.h" 34 #include "trace.h" 35 #include "exec/address-spaces.h" 36 #include "sysemu/xen.h" 37 #include "sysemu/block-backend.h" 38 #include "qemu/error-report.h" 39 #include "qemu/module.h" 40 #include "qom/object.h" 41 42 //#define DEBUG_PLATFORM 43 44 #ifdef DEBUG_PLATFORM 45 #define DPRINTF(fmt, ...) do { \ 46 fprintf(stderr, "xen_platform: " fmt, ## __VA_ARGS__); \ 47 } while (0) 48 #else 49 #define DPRINTF(fmt, ...) do { } while (0) 50 #endif 51 52 #define PFFLAG_ROM_LOCK 1 /* Sets whether ROM memory area is RW or RO */ 53 54 struct PCIXenPlatformState { 55 /*< private >*/ 56 PCIDevice parent_obj; 57 /*< public >*/ 58 59 MemoryRegion fixed_io; 60 MemoryRegion bar; 61 MemoryRegion mmio_bar; 62 uint8_t flags; /* used only for version_id == 2 */ 63 uint16_t driver_product_version; 64 65 /* Log from guest drivers */ 66 char log_buffer[4096]; 67 int log_buffer_off; 68 }; 69 70 #define TYPE_XEN_PLATFORM "xen-platform" 71 OBJECT_DECLARE_SIMPLE_TYPE(PCIXenPlatformState, XEN_PLATFORM) 72 73 #define XEN_PLATFORM_IOPORT 0x10 74 75 /* Send bytes to syslog */ 76 static void log_writeb(PCIXenPlatformState *s, char val) 77 { 78 if (val == '\n' || s->log_buffer_off == sizeof(s->log_buffer) - 1) { 79 /* Flush buffer */ 80 s->log_buffer[s->log_buffer_off] = 0; 81 trace_xen_platform_log(s->log_buffer); 82 s->log_buffer_off = 0; 83 } else { 84 s->log_buffer[s->log_buffer_off++] = val; 85 } 86 } 87 88 /* 89 * Unplug device flags. 90 * 91 * The logic got a little confused at some point in the past but this is 92 * what they do now. 93 * 94 * bit 0: Unplug all IDE and SCSI disks. 95 * bit 1: Unplug all NICs. 96 * bit 2: Unplug IDE disks except primary master. This is overridden if 97 * bit 0 is also present in the mask. 98 * bit 3: Unplug all NVMe disks. 99 * 100 */ 101 #define _UNPLUG_IDE_SCSI_DISKS 0 102 #define UNPLUG_IDE_SCSI_DISKS (1u << _UNPLUG_IDE_SCSI_DISKS) 103 104 #define _UNPLUG_ALL_NICS 1 105 #define UNPLUG_ALL_NICS (1u << _UNPLUG_ALL_NICS) 106 107 #define _UNPLUG_AUX_IDE_DISKS 2 108 #define UNPLUG_AUX_IDE_DISKS (1u << _UNPLUG_AUX_IDE_DISKS) 109 110 #define _UNPLUG_NVME_DISKS 3 111 #define UNPLUG_NVME_DISKS (1u << _UNPLUG_NVME_DISKS) 112 113 static void unplug_nic(PCIBus *b, PCIDevice *d, void *o) 114 { 115 /* We have to ignore passthrough devices */ 116 if (pci_get_word(d->config + PCI_CLASS_DEVICE) == 117 PCI_CLASS_NETWORK_ETHERNET 118 && strcmp(d->name, "xen-pci-passthrough") != 0) { 119 object_unparent(OBJECT(d)); 120 } 121 } 122 123 /* Remove the peer of the NIC device. Normally, this would be a tap device. */ 124 static void del_nic_peer(NICState *nic, void *opaque) 125 { 126 NetClientState *nc; 127 128 nc = qemu_get_queue(nic); 129 if (nc->peer) 130 qemu_del_net_client(nc->peer); 131 } 132 133 static void pci_unplug_nics(PCIBus *bus) 134 { 135 qemu_foreach_nic(del_nic_peer, NULL); 136 pci_for_each_device(bus, 0, unplug_nic, NULL); 137 } 138 139 static void unplug_disks(PCIBus *b, PCIDevice *d, void *opaque) 140 { 141 uint32_t flags = *(uint32_t *)opaque; 142 bool aux = (flags & UNPLUG_AUX_IDE_DISKS) && 143 !(flags & UNPLUG_IDE_SCSI_DISKS); 144 145 /* We have to ignore passthrough devices */ 146 if (!strcmp(d->name, "xen-pci-passthrough")) { 147 return; 148 } 149 150 switch (pci_get_word(d->config + PCI_CLASS_DEVICE)) { 151 case PCI_CLASS_STORAGE_IDE: 152 pci_piix3_xen_ide_unplug(DEVICE(d), aux); 153 break; 154 155 case PCI_CLASS_STORAGE_SCSI: 156 if (!aux) { 157 object_unparent(OBJECT(d)); 158 } 159 break; 160 161 case PCI_CLASS_STORAGE_EXPRESS: 162 if (flags & UNPLUG_NVME_DISKS) { 163 object_unparent(OBJECT(d)); 164 } 165 166 default: 167 break; 168 } 169 } 170 171 static void pci_unplug_disks(PCIBus *bus, uint32_t flags) 172 { 173 pci_for_each_device(bus, 0, unplug_disks, &flags); 174 } 175 176 static void platform_fixed_ioport_writew(void *opaque, uint32_t addr, uint32_t val) 177 { 178 PCIXenPlatformState *s = opaque; 179 180 switch (addr) { 181 case 0: { 182 PCIDevice *pci_dev = PCI_DEVICE(s); 183 /* Unplug devices. See comment above flag definitions */ 184 if (val & (UNPLUG_IDE_SCSI_DISKS | UNPLUG_AUX_IDE_DISKS | 185 UNPLUG_NVME_DISKS)) { 186 DPRINTF("unplug disks\n"); 187 pci_unplug_disks(pci_get_bus(pci_dev), val); 188 } 189 if (val & UNPLUG_ALL_NICS) { 190 DPRINTF("unplug nics\n"); 191 pci_unplug_nics(pci_get_bus(pci_dev)); 192 } 193 break; 194 } 195 case 2: 196 switch (val) { 197 case 1: 198 DPRINTF("Citrix Windows PV drivers loaded in guest\n"); 199 break; 200 case 0: 201 DPRINTF("Guest claimed to be running PV product 0?\n"); 202 break; 203 default: 204 DPRINTF("Unknown PV product %d loaded in guest\n", val); 205 break; 206 } 207 s->driver_product_version = val; 208 break; 209 } 210 } 211 212 static void platform_fixed_ioport_writel(void *opaque, uint32_t addr, 213 uint32_t val) 214 { 215 switch (addr) { 216 case 0: 217 /* PV driver version */ 218 break; 219 } 220 } 221 222 static void platform_fixed_ioport_writeb(void *opaque, uint32_t addr, uint32_t val) 223 { 224 PCIXenPlatformState *s = opaque; 225 226 switch (addr) { 227 case 0: /* Platform flags */ { 228 hvmmem_type_t mem_type = (val & PFFLAG_ROM_LOCK) ? 229 HVMMEM_ram_ro : HVMMEM_ram_rw; 230 if (xen_set_mem_type(xen_domid, mem_type, 0xc0, 0x40)) { 231 DPRINTF("unable to change ro/rw state of ROM memory area!\n"); 232 } else { 233 s->flags = val & PFFLAG_ROM_LOCK; 234 DPRINTF("changed ro/rw state of ROM memory area. now is %s state.\n", 235 (mem_type == HVMMEM_ram_ro ? "ro":"rw")); 236 } 237 break; 238 } 239 case 2: 240 log_writeb(s, val); 241 break; 242 } 243 } 244 245 static uint32_t platform_fixed_ioport_readw(void *opaque, uint32_t addr) 246 { 247 switch (addr) { 248 case 0: 249 /* Magic value so that you can identify the interface. */ 250 return 0x49d2; 251 default: 252 return 0xffff; 253 } 254 } 255 256 static uint32_t platform_fixed_ioport_readb(void *opaque, uint32_t addr) 257 { 258 PCIXenPlatformState *s = opaque; 259 260 switch (addr) { 261 case 0: 262 /* Platform flags */ 263 return s->flags; 264 case 2: 265 /* Version number */ 266 return 1; 267 default: 268 return 0xff; 269 } 270 } 271 272 static void platform_fixed_ioport_reset(void *opaque) 273 { 274 PCIXenPlatformState *s = opaque; 275 276 platform_fixed_ioport_writeb(s, 0, 0); 277 } 278 279 static uint64_t platform_fixed_ioport_read(void *opaque, 280 hwaddr addr, 281 unsigned size) 282 { 283 switch (size) { 284 case 1: 285 return platform_fixed_ioport_readb(opaque, addr); 286 case 2: 287 return platform_fixed_ioport_readw(opaque, addr); 288 default: 289 return -1; 290 } 291 } 292 293 static void platform_fixed_ioport_write(void *opaque, hwaddr addr, 294 295 uint64_t val, unsigned size) 296 { 297 switch (size) { 298 case 1: 299 platform_fixed_ioport_writeb(opaque, addr, val); 300 break; 301 case 2: 302 platform_fixed_ioport_writew(opaque, addr, val); 303 break; 304 case 4: 305 platform_fixed_ioport_writel(opaque, addr, val); 306 break; 307 } 308 } 309 310 311 static const MemoryRegionOps platform_fixed_io_ops = { 312 .read = platform_fixed_ioport_read, 313 .write = platform_fixed_ioport_write, 314 .valid = { 315 .unaligned = true, 316 }, 317 .impl = { 318 .min_access_size = 1, 319 .max_access_size = 4, 320 .unaligned = true, 321 }, 322 .endianness = DEVICE_LITTLE_ENDIAN, 323 }; 324 325 static void platform_fixed_ioport_init(PCIXenPlatformState* s) 326 { 327 memory_region_init_io(&s->fixed_io, OBJECT(s), &platform_fixed_io_ops, s, 328 "xen-fixed", 16); 329 memory_region_add_subregion(get_system_io(), XEN_PLATFORM_IOPORT, 330 &s->fixed_io); 331 } 332 333 /* Xen Platform PCI Device */ 334 335 static uint64_t xen_platform_ioport_readb(void *opaque, hwaddr addr, 336 unsigned int size) 337 { 338 if (addr == 0) { 339 return platform_fixed_ioport_readb(opaque, 0); 340 } else { 341 return ~0u; 342 } 343 } 344 345 static void xen_platform_ioport_writeb(void *opaque, hwaddr addr, 346 uint64_t val, unsigned int size) 347 { 348 PCIXenPlatformState *s = opaque; 349 PCIDevice *pci_dev = PCI_DEVICE(s); 350 351 switch (addr) { 352 case 0: /* Platform flags */ 353 platform_fixed_ioport_writeb(opaque, 0, (uint32_t)val); 354 break; 355 case 4: 356 if (val == 1) { 357 /* 358 * SUSE unplug for Xenlinux 359 * xen-kmp used this since xen-3.0.4, instead the official protocol 360 * from xen-3.3+ It did an unconditional "outl(1, (ioaddr + 4));" 361 * Pre VMDP 1.7 used 4 and 8 depending on how VMDP was configured. 362 * If VMDP was to control both disk and LAN it would use 4. 363 * If it controlled just disk or just LAN, it would use 8 below. 364 */ 365 pci_unplug_disks(pci_get_bus(pci_dev), UNPLUG_IDE_SCSI_DISKS); 366 pci_unplug_nics(pci_get_bus(pci_dev)); 367 } 368 break; 369 case 8: 370 switch (val) { 371 case 1: 372 pci_unplug_disks(pci_get_bus(pci_dev), UNPLUG_IDE_SCSI_DISKS); 373 break; 374 case 2: 375 pci_unplug_nics(pci_get_bus(pci_dev)); 376 break; 377 default: 378 log_writeb(s, (uint32_t)val); 379 break; 380 } 381 break; 382 default: 383 break; 384 } 385 } 386 387 static const MemoryRegionOps xen_pci_io_ops = { 388 .read = xen_platform_ioport_readb, 389 .write = xen_platform_ioport_writeb, 390 .impl.min_access_size = 1, 391 .impl.max_access_size = 1, 392 }; 393 394 static void platform_ioport_bar_setup(PCIXenPlatformState *d) 395 { 396 memory_region_init_io(&d->bar, OBJECT(d), &xen_pci_io_ops, d, 397 "xen-pci", 0x100); 398 } 399 400 static uint64_t platform_mmio_read(void *opaque, hwaddr addr, 401 unsigned size) 402 { 403 DPRINTF("Warning: attempted read from physical address " 404 "0x" TARGET_FMT_plx " in xen platform mmio space\n", addr); 405 406 return 0; 407 } 408 409 static void platform_mmio_write(void *opaque, hwaddr addr, 410 uint64_t val, unsigned size) 411 { 412 DPRINTF("Warning: attempted write of 0x%"PRIx64" to physical " 413 "address 0x" TARGET_FMT_plx " in xen platform mmio space\n", 414 val, addr); 415 } 416 417 static const MemoryRegionOps platform_mmio_handler = { 418 .read = &platform_mmio_read, 419 .write = &platform_mmio_write, 420 .endianness = DEVICE_NATIVE_ENDIAN, 421 }; 422 423 static void platform_mmio_setup(PCIXenPlatformState *d) 424 { 425 memory_region_init_io(&d->mmio_bar, OBJECT(d), &platform_mmio_handler, d, 426 "xen-mmio", 0x1000000); 427 } 428 429 static int xen_platform_post_load(void *opaque, int version_id) 430 { 431 PCIXenPlatformState *s = opaque; 432 433 platform_fixed_ioport_writeb(s, 0, s->flags); 434 435 return 0; 436 } 437 438 static const VMStateDescription vmstate_xen_platform = { 439 .name = "platform", 440 .version_id = 4, 441 .minimum_version_id = 4, 442 .post_load = xen_platform_post_load, 443 .fields = (VMStateField[]) { 444 VMSTATE_PCI_DEVICE(parent_obj, PCIXenPlatformState), 445 VMSTATE_UINT8(flags, PCIXenPlatformState), 446 VMSTATE_END_OF_LIST() 447 } 448 }; 449 450 static void xen_platform_realize(PCIDevice *dev, Error **errp) 451 { 452 PCIXenPlatformState *d = XEN_PLATFORM(dev); 453 uint8_t *pci_conf; 454 455 /* Device will crash on reset if xen is not initialized */ 456 if (!xen_enabled()) { 457 error_setg(errp, "xen-platform device requires the Xen accelerator"); 458 return; 459 } 460 461 pci_conf = dev->config; 462 463 pci_set_word(pci_conf + PCI_COMMAND, PCI_COMMAND_IO | PCI_COMMAND_MEMORY); 464 465 pci_config_set_prog_interface(pci_conf, 0); 466 467 pci_conf[PCI_INTERRUPT_PIN] = 1; 468 469 platform_ioport_bar_setup(d); 470 pci_register_bar(dev, 0, PCI_BASE_ADDRESS_SPACE_IO, &d->bar); 471 472 /* reserve 16MB mmio address for share memory*/ 473 platform_mmio_setup(d); 474 pci_register_bar(dev, 1, PCI_BASE_ADDRESS_MEM_PREFETCH, 475 &d->mmio_bar); 476 477 platform_fixed_ioport_init(d); 478 } 479 480 static void platform_reset(DeviceState *dev) 481 { 482 PCIXenPlatformState *s = XEN_PLATFORM(dev); 483 484 platform_fixed_ioport_reset(s); 485 } 486 487 static void xen_platform_class_init(ObjectClass *klass, void *data) 488 { 489 DeviceClass *dc = DEVICE_CLASS(klass); 490 PCIDeviceClass *k = PCI_DEVICE_CLASS(klass); 491 492 k->realize = xen_platform_realize; 493 k->vendor_id = PCI_VENDOR_ID_XEN; 494 k->device_id = PCI_DEVICE_ID_XEN_PLATFORM; 495 k->class_id = PCI_CLASS_OTHERS << 8 | 0x80; 496 k->subsystem_vendor_id = PCI_VENDOR_ID_XEN; 497 k->subsystem_id = PCI_DEVICE_ID_XEN_PLATFORM; 498 k->revision = 1; 499 set_bit(DEVICE_CATEGORY_MISC, dc->categories); 500 dc->desc = "XEN platform pci device"; 501 dc->reset = platform_reset; 502 dc->vmsd = &vmstate_xen_platform; 503 } 504 505 static const TypeInfo xen_platform_info = { 506 .name = TYPE_XEN_PLATFORM, 507 .parent = TYPE_PCI_DEVICE, 508 .instance_size = sizeof(PCIXenPlatformState), 509 .class_init = xen_platform_class_init, 510 .interfaces = (InterfaceInfo[]) { 511 { INTERFACE_CONVENTIONAL_PCI_DEVICE }, 512 { }, 513 }, 514 }; 515 516 static void xen_platform_register_types(void) 517 { 518 type_register_static(&xen_platform_info); 519 } 520 521 type_init(xen_platform_register_types) 522