2 * XEN platform pci device, formerly known as the event channel device
4 * Copyright (c) 2003-2004 Intel Corp.
5 * Copyright (c) 2006 XenSource
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:
14 * The above copyright notice and this permission notice shall be included in
15 * all copies or substantial portions of the Software.
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
26 #include "qemu/osdep.h"
27 #include "qapi/error.h"
30 #include "hw/pci/pci.h"
32 #include "hw/xen/xen_common.h"
33 #include "hw/xen/xen_backend.h"
35 #include "exec/address-spaces.h"
36 #include "sysemu/block-backend.h"
37 #include "qemu/error-report.h"
41 //#define DEBUG_PLATFORM
44 #define DPRINTF(fmt, ...) do { \
45 fprintf(stderr, "xen_platform: " fmt, ## __VA_ARGS__); \
48 #define DPRINTF(fmt, ...) do { } while (0)
51 #define PFFLAG_ROM_LOCK 1 /* Sets whether ROM memory area is RW or RO */
53 typedef struct PCIXenPlatformState {
58 MemoryRegion fixed_io;
60 MemoryRegion mmio_bar;
61 uint8_t flags; /* used only for version_id == 2 */
62 int drivers_blacklisted;
63 uint16_t driver_product_version;
65 /* Log from guest drivers */
66 char log_buffer[4096];
68 } PCIXenPlatformState;
70 #define TYPE_XEN_PLATFORM "xen-platform"
71 #define XEN_PLATFORM(obj) \
72 OBJECT_CHECK(PCIXenPlatformState, (obj), TYPE_XEN_PLATFORM)
74 #define XEN_PLATFORM_IOPORT 0x10
76 /* Send bytes to syslog */
77 static void log_writeb(PCIXenPlatformState *s, char val)
79 if (val == '\n' || s->log_buffer_off == sizeof(s->log_buffer) - 1) {
81 s->log_buffer[s->log_buffer_off] = 0;
82 trace_xen_platform_log(s->log_buffer);
83 s->log_buffer_off = 0;
85 s->log_buffer[s->log_buffer_off++] = val;
90 * Unplug device flags.
92 * The logic got a little confused at some point in the past but this is
95 * bit 0: Unplug all IDE and SCSI disks.
96 * bit 1: Unplug all NICs.
97 * bit 2: Unplug IDE disks except primary master. This is overridden if
98 * bit 0 is also present in the mask.
99 * bit 3: Unplug all NVMe disks.
102 #define _UNPLUG_IDE_SCSI_DISKS 0
103 #define UNPLUG_IDE_SCSI_DISKS (1u << _UNPLUG_IDE_SCSI_DISKS)
105 #define _UNPLUG_ALL_NICS 1
106 #define UNPLUG_ALL_NICS (1u << _UNPLUG_ALL_NICS)
108 #define _UNPLUG_AUX_IDE_DISKS 2
109 #define UNPLUG_AUX_IDE_DISKS (1u << _UNPLUG_AUX_IDE_DISKS)
111 #define _UNPLUG_NVME_DISKS 3
112 #define UNPLUG_NVME_DISKS (1u << _UNPLUG_NVME_DISKS)
114 static void unplug_nic(PCIBus *b, PCIDevice *d, void *o)
116 /* We have to ignore passthrough devices */
117 if (pci_get_word(d->config + PCI_CLASS_DEVICE) ==
118 PCI_CLASS_NETWORK_ETHERNET
119 && strcmp(d->name, "xen-pci-passthrough") != 0) {
120 object_unparent(OBJECT(d));
124 /* Remove the peer of the NIC device. Normally, this would be a tap device. */
125 static void del_nic_peer(NICState *nic, void *opaque)
129 nc = qemu_get_queue(nic);
131 qemu_del_net_client(nc->peer);
134 static void pci_unplug_nics(PCIBus *bus)
136 qemu_foreach_nic(del_nic_peer, NULL);
137 pci_for_each_device(bus, 0, unplug_nic, NULL);
140 static void unplug_disks(PCIBus *b, PCIDevice *d, void *opaque)
142 uint32_t flags = *(uint32_t *)opaque;
143 bool aux = (flags & UNPLUG_AUX_IDE_DISKS) &&
144 !(flags & UNPLUG_IDE_SCSI_DISKS);
146 /* We have to ignore passthrough devices */
147 if (!strcmp(d->name, "xen-pci-passthrough")) {
151 switch (pci_get_word(d->config + PCI_CLASS_DEVICE)) {
152 case PCI_CLASS_STORAGE_IDE:
153 pci_piix3_xen_ide_unplug(DEVICE(d), aux);
156 case PCI_CLASS_STORAGE_SCSI:
158 object_unparent(OBJECT(d));
162 case PCI_CLASS_STORAGE_EXPRESS:
163 if (flags & UNPLUG_NVME_DISKS) {
164 object_unparent(OBJECT(d));
172 static void pci_unplug_disks(PCIBus *bus, uint32_t flags)
174 pci_for_each_device(bus, 0, unplug_disks, &flags);
177 static void platform_fixed_ioport_writew(void *opaque, uint32_t addr, uint32_t val)
179 PCIXenPlatformState *s = opaque;
183 PCIDevice *pci_dev = PCI_DEVICE(s);
184 /* Unplug devices. See comment above flag definitions */
185 if (val & (UNPLUG_IDE_SCSI_DISKS | UNPLUG_AUX_IDE_DISKS |
186 UNPLUG_NVME_DISKS)) {
187 DPRINTF("unplug disks\n");
188 pci_unplug_disks(pci_dev->bus, val);
190 if (val & UNPLUG_ALL_NICS) {
191 DPRINTF("unplug nics\n");
192 pci_unplug_nics(pci_dev->bus);
199 DPRINTF("Citrix Windows PV drivers loaded in guest\n");
202 DPRINTF("Guest claimed to be running PV product 0?\n");
205 DPRINTF("Unknown PV product %d loaded in guest\n", val);
208 s->driver_product_version = val;
213 static void platform_fixed_ioport_writel(void *opaque, uint32_t addr,
218 /* PV driver version */
223 static void platform_fixed_ioport_writeb(void *opaque, uint32_t addr, uint32_t val)
225 PCIXenPlatformState *s = opaque;
228 case 0: /* Platform flags */ {
229 hvmmem_type_t mem_type = (val & PFFLAG_ROM_LOCK) ?
230 HVMMEM_ram_ro : HVMMEM_ram_rw;
231 if (xen_set_mem_type(xen_domid, mem_type, 0xc0, 0x40)) {
232 DPRINTF("unable to change ro/rw state of ROM memory area!\n");
234 s->flags = val & PFFLAG_ROM_LOCK;
235 DPRINTF("changed ro/rw state of ROM memory area. now is %s state.\n",
236 (mem_type == HVMMEM_ram_ro ? "ro":"rw"));
246 static uint32_t platform_fixed_ioport_readw(void *opaque, uint32_t addr)
248 PCIXenPlatformState *s = opaque;
252 if (s->drivers_blacklisted) {
253 /* The drivers will recognise this magic number and refuse
257 /* Magic value so that you can identify the interface. */
265 static uint32_t platform_fixed_ioport_readb(void *opaque, uint32_t addr)
267 PCIXenPlatformState *s = opaque;
281 static void platform_fixed_ioport_reset(void *opaque)
283 PCIXenPlatformState *s = opaque;
285 platform_fixed_ioport_writeb(s, 0, 0);
288 static uint64_t platform_fixed_ioport_read(void *opaque,
294 return platform_fixed_ioport_readb(opaque, addr);
296 return platform_fixed_ioport_readw(opaque, addr);
302 static void platform_fixed_ioport_write(void *opaque, hwaddr addr,
304 uint64_t val, unsigned size)
308 platform_fixed_ioport_writeb(opaque, addr, val);
311 platform_fixed_ioport_writew(opaque, addr, val);
314 platform_fixed_ioport_writel(opaque, addr, val);
320 static const MemoryRegionOps platform_fixed_io_ops = {
321 .read = platform_fixed_ioport_read,
322 .write = platform_fixed_ioport_write,
327 .min_access_size = 1,
328 .max_access_size = 4,
331 .endianness = DEVICE_LITTLE_ENDIAN,
334 static void platform_fixed_ioport_init(PCIXenPlatformState* s)
336 memory_region_init_io(&s->fixed_io, OBJECT(s), &platform_fixed_io_ops, s,
338 memory_region_add_subregion(get_system_io(), XEN_PLATFORM_IOPORT,
342 /* Xen Platform PCI Device */
344 static uint64_t xen_platform_ioport_readb(void *opaque, hwaddr addr,
348 return platform_fixed_ioport_readb(opaque, 0);
354 static void xen_platform_ioport_writeb(void *opaque, hwaddr addr,
355 uint64_t val, unsigned int size)
357 PCIXenPlatformState *s = opaque;
358 PCIDevice *pci_dev = PCI_DEVICE(s);
361 case 0: /* Platform flags */
362 platform_fixed_ioport_writeb(opaque, 0, (uint32_t)val);
367 * SUSE unplug for Xenlinux
368 * xen-kmp used this since xen-3.0.4, instead the official protocol
369 * from xen-3.3+ It did an unconditional "outl(1, (ioaddr + 4));"
370 * Pre VMDP 1.7 used 4 and 8 depending on how VMDP was configured.
371 * If VMDP was to control both disk and LAN it would use 4.
372 * If it controlled just disk or just LAN, it would use 8 below.
374 pci_unplug_disks(pci_dev->bus, UNPLUG_IDE_SCSI_DISKS);
375 pci_unplug_nics(pci_dev->bus);
381 pci_unplug_disks(pci_dev->bus, UNPLUG_IDE_SCSI_DISKS);
384 pci_unplug_nics(pci_dev->bus);
387 log_writeb(s, (uint32_t)val);
396 static const MemoryRegionOps xen_pci_io_ops = {
397 .read = xen_platform_ioport_readb,
398 .write = xen_platform_ioport_writeb,
399 .impl.min_access_size = 1,
400 .impl.max_access_size = 1,
403 static void platform_ioport_bar_setup(PCIXenPlatformState *d)
405 memory_region_init_io(&d->bar, OBJECT(d), &xen_pci_io_ops, d,
409 static uint64_t platform_mmio_read(void *opaque, hwaddr addr,
412 DPRINTF("Warning: attempted read from physical address "
413 "0x" TARGET_FMT_plx " in xen platform mmio space\n", addr);
418 static void platform_mmio_write(void *opaque, hwaddr addr,
419 uint64_t val, unsigned size)
421 DPRINTF("Warning: attempted write of 0x%"PRIx64" to physical "
422 "address 0x" TARGET_FMT_plx " in xen platform mmio space\n",
426 static const MemoryRegionOps platform_mmio_handler = {
427 .read = &platform_mmio_read,
428 .write = &platform_mmio_write,
429 .endianness = DEVICE_NATIVE_ENDIAN,
432 static void platform_mmio_setup(PCIXenPlatformState *d)
434 memory_region_init_io(&d->mmio_bar, OBJECT(d), &platform_mmio_handler, d,
435 "xen-mmio", 0x1000000);
438 static int xen_platform_post_load(void *opaque, int version_id)
440 PCIXenPlatformState *s = opaque;
442 platform_fixed_ioport_writeb(s, 0, s->flags);
447 static const VMStateDescription vmstate_xen_platform = {
450 .minimum_version_id = 4,
451 .post_load = xen_platform_post_load,
452 .fields = (VMStateField[]) {
453 VMSTATE_PCI_DEVICE(parent_obj, PCIXenPlatformState),
454 VMSTATE_UINT8(flags, PCIXenPlatformState),
455 VMSTATE_END_OF_LIST()
459 static void xen_platform_realize(PCIDevice *dev, Error **errp)
461 PCIXenPlatformState *d = XEN_PLATFORM(dev);
464 /* Device will crash on reset if xen is not initialized */
465 if (!xen_enabled()) {
466 error_setg(errp, "xen-platform device requires the Xen accelerator");
470 pci_conf = dev->config;
472 pci_set_word(pci_conf + PCI_COMMAND, PCI_COMMAND_IO | PCI_COMMAND_MEMORY);
474 pci_config_set_prog_interface(pci_conf, 0);
476 pci_conf[PCI_INTERRUPT_PIN] = 1;
478 platform_ioport_bar_setup(d);
479 pci_register_bar(dev, 0, PCI_BASE_ADDRESS_SPACE_IO, &d->bar);
481 /* reserve 16MB mmio address for share memory*/
482 platform_mmio_setup(d);
483 pci_register_bar(dev, 1, PCI_BASE_ADDRESS_MEM_PREFETCH,
486 platform_fixed_ioport_init(d);
489 static void platform_reset(DeviceState *dev)
491 PCIXenPlatformState *s = XEN_PLATFORM(dev);
493 platform_fixed_ioport_reset(s);
496 static void xen_platform_class_init(ObjectClass *klass, void *data)
498 DeviceClass *dc = DEVICE_CLASS(klass);
499 PCIDeviceClass *k = PCI_DEVICE_CLASS(klass);
501 k->realize = xen_platform_realize;
502 k->vendor_id = PCI_VENDOR_ID_XEN;
503 k->device_id = PCI_DEVICE_ID_XEN_PLATFORM;
504 k->class_id = PCI_CLASS_OTHERS << 8 | 0x80;
505 k->subsystem_vendor_id = PCI_VENDOR_ID_XEN;
506 k->subsystem_id = PCI_DEVICE_ID_XEN_PLATFORM;
508 set_bit(DEVICE_CATEGORY_MISC, dc->categories);
509 dc->desc = "XEN platform pci device";
510 dc->reset = platform_reset;
511 dc->vmsd = &vmstate_xen_platform;
514 static const TypeInfo xen_platform_info = {
515 .name = TYPE_XEN_PLATFORM,
516 .parent = TYPE_PCI_DEVICE,
517 .instance_size = sizeof(PCIXenPlatformState),
518 .class_init = xen_platform_class_init,
519 .interfaces = (InterfaceInfo[]) {
520 { INTERFACE_CONVENTIONAL_PCI_DEVICE },
525 static void xen_platform_register_types(void)
527 type_register_static(&xen_platform_info);
530 type_init(xen_platform_register_types)