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"
29 #include "hw/ide/pci.h"
30 #include "hw/pci/pci.h"
31 #include "hw/xen/xen_common.h"
32 #include "migration/vmstate.h"
33 #include "hw/xen/xen-legacy-backend.h"
35 #include "sysemu/xen.h"
36 #include "sysemu/block-backend.h"
37 #include "qemu/error-report.h"
38 #include "qemu/module.h"
39 #include "qom/object.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 struct PCIXenPlatformState
{
58 MemoryRegion fixed_io
;
60 MemoryRegion mmio_bar
;
61 uint8_t flags
; /* used only for version_id == 2 */
62 uint16_t driver_product_version
;
64 /* Log from guest drivers */
65 char log_buffer
[4096];
69 #define TYPE_XEN_PLATFORM "xen-platform"
70 OBJECT_DECLARE_SIMPLE_TYPE(PCIXenPlatformState
, XEN_PLATFORM
)
72 #define XEN_PLATFORM_IOPORT 0x10
74 /* Send bytes to syslog */
75 static void log_writeb(PCIXenPlatformState
*s
, char val
)
77 if (val
== '\n' || s
->log_buffer_off
== sizeof(s
->log_buffer
) - 1) {
79 s
->log_buffer
[s
->log_buffer_off
] = 0;
80 trace_xen_platform_log(s
->log_buffer
);
81 s
->log_buffer_off
= 0;
83 s
->log_buffer
[s
->log_buffer_off
++] = val
;
88 * Unplug device flags.
90 * The logic got a little confused at some point in the past but this is
93 * bit 0: Unplug all IDE and SCSI disks.
94 * bit 1: Unplug all NICs.
95 * bit 2: Unplug IDE disks except primary master. This is overridden if
96 * bit 0 is also present in the mask.
97 * bit 3: Unplug all NVMe disks.
100 #define _UNPLUG_IDE_SCSI_DISKS 0
101 #define UNPLUG_IDE_SCSI_DISKS (1u << _UNPLUG_IDE_SCSI_DISKS)
103 #define _UNPLUG_ALL_NICS 1
104 #define UNPLUG_ALL_NICS (1u << _UNPLUG_ALL_NICS)
106 #define _UNPLUG_AUX_IDE_DISKS 2
107 #define UNPLUG_AUX_IDE_DISKS (1u << _UNPLUG_AUX_IDE_DISKS)
109 #define _UNPLUG_NVME_DISKS 3
110 #define UNPLUG_NVME_DISKS (1u << _UNPLUG_NVME_DISKS)
112 static void unplug_nic(PCIBus
*b
, PCIDevice
*d
, void *o
)
114 /* We have to ignore passthrough devices */
115 if (pci_get_word(d
->config
+ PCI_CLASS_DEVICE
) ==
116 PCI_CLASS_NETWORK_ETHERNET
117 && strcmp(d
->name
, "xen-pci-passthrough") != 0) {
118 object_unparent(OBJECT(d
));
122 /* Remove the peer of the NIC device. Normally, this would be a tap device. */
123 static void del_nic_peer(NICState
*nic
, void *opaque
)
127 nc
= qemu_get_queue(nic
);
129 qemu_del_net_client(nc
->peer
);
132 static void pci_unplug_nics(PCIBus
*bus
)
134 qemu_foreach_nic(del_nic_peer
, NULL
);
135 pci_for_each_device(bus
, 0, unplug_nic
, NULL
);
139 * The Xen HVM unplug protocol [1] specifies a mechanism to allow guests to
140 * request unplug of 'aux' disks (which is stated to mean all IDE disks,
141 * except the primary master).
143 * NOTE: The semantics of what happens if unplug of all disks and 'aux' disks
144 * is simultaneously requested is not clear. The implementation assumes
145 * that an 'all' request overrides an 'aux' request.
147 * [1] https://xenbits.xen.org/gitweb/?p=xen.git;a=blob;f=docs/misc/hvm-emulated-unplug.pandoc
149 static void pci_xen_ide_unplug(DeviceState
*dev
, bool aux
)
151 PCIIDEState
*pci_ide
;
157 pci_ide
= PCI_IDE(dev
);
159 for (i
= aux
? 1 : 0; i
< 4; i
++) {
160 idebus
= &pci_ide
->bus
[i
/ 2];
161 blk
= idebus
->ifs
[i
% 2].blk
;
163 if (blk
&& idebus
->ifs
[i
% 2].drive_kind
!= IDE_CD
) {
165 idedev
= idebus
->master
;
167 idedev
= idebus
->slave
;
173 blk_detach_dev(blk
, DEVICE(idedev
));
174 idebus
->ifs
[i
% 2].blk
= NULL
;
175 idedev
->conf
.blk
= NULL
;
176 monitor_remove_blk(blk
);
180 device_cold_reset(dev
);
183 static void unplug_disks(PCIBus
*b
, PCIDevice
*d
, void *opaque
)
185 uint32_t flags
= *(uint32_t *)opaque
;
186 bool aux
= (flags
& UNPLUG_AUX_IDE_DISKS
) &&
187 !(flags
& UNPLUG_IDE_SCSI_DISKS
);
189 /* We have to ignore passthrough devices */
190 if (!strcmp(d
->name
, "xen-pci-passthrough")) {
194 switch (pci_get_word(d
->config
+ PCI_CLASS_DEVICE
)) {
195 case PCI_CLASS_STORAGE_IDE
:
196 pci_xen_ide_unplug(DEVICE(d
), aux
);
199 case PCI_CLASS_STORAGE_SCSI
:
201 object_unparent(OBJECT(d
));
205 case PCI_CLASS_STORAGE_EXPRESS
:
206 if (flags
& UNPLUG_NVME_DISKS
) {
207 object_unparent(OBJECT(d
));
215 static void pci_unplug_disks(PCIBus
*bus
, uint32_t flags
)
217 pci_for_each_device(bus
, 0, unplug_disks
, &flags
);
220 static void platform_fixed_ioport_writew(void *opaque
, uint32_t addr
, uint32_t val
)
222 PCIXenPlatformState
*s
= opaque
;
226 PCIDevice
*pci_dev
= PCI_DEVICE(s
);
227 /* Unplug devices. See comment above flag definitions */
228 if (val
& (UNPLUG_IDE_SCSI_DISKS
| UNPLUG_AUX_IDE_DISKS
|
229 UNPLUG_NVME_DISKS
)) {
230 DPRINTF("unplug disks\n");
231 pci_unplug_disks(pci_get_bus(pci_dev
), val
);
233 if (val
& UNPLUG_ALL_NICS
) {
234 DPRINTF("unplug nics\n");
235 pci_unplug_nics(pci_get_bus(pci_dev
));
242 DPRINTF("Citrix Windows PV drivers loaded in guest\n");
245 DPRINTF("Guest claimed to be running PV product 0?\n");
248 DPRINTF("Unknown PV product %d loaded in guest\n", val
);
251 s
->driver_product_version
= val
;
256 static void platform_fixed_ioport_writel(void *opaque
, uint32_t addr
,
261 /* PV driver version */
266 static void platform_fixed_ioport_writeb(void *opaque
, uint32_t addr
, uint32_t val
)
268 PCIXenPlatformState
*s
= opaque
;
271 case 0: /* Platform flags */ {
272 hvmmem_type_t mem_type
= (val
& PFFLAG_ROM_LOCK
) ?
273 HVMMEM_ram_ro
: HVMMEM_ram_rw
;
274 if (xen_set_mem_type(xen_domid
, mem_type
, 0xc0, 0x40)) {
275 DPRINTF("unable to change ro/rw state of ROM memory area!\n");
277 s
->flags
= val
& PFFLAG_ROM_LOCK
;
278 DPRINTF("changed ro/rw state of ROM memory area. now is %s state.\n",
279 (mem_type
== HVMMEM_ram_ro
? "ro":"rw"));
289 static uint32_t platform_fixed_ioport_readw(void *opaque
, uint32_t addr
)
293 /* Magic value so that you can identify the interface. */
300 static uint32_t platform_fixed_ioport_readb(void *opaque
, uint32_t addr
)
302 PCIXenPlatformState
*s
= opaque
;
316 static void platform_fixed_ioport_reset(void *opaque
)
318 PCIXenPlatformState
*s
= opaque
;
320 platform_fixed_ioport_writeb(s
, 0, 0);
323 static uint64_t platform_fixed_ioport_read(void *opaque
,
329 return platform_fixed_ioport_readb(opaque
, addr
);
331 return platform_fixed_ioport_readw(opaque
, addr
);
337 static void platform_fixed_ioport_write(void *opaque
, hwaddr addr
,
339 uint64_t val
, unsigned size
)
343 platform_fixed_ioport_writeb(opaque
, addr
, val
);
346 platform_fixed_ioport_writew(opaque
, addr
, val
);
349 platform_fixed_ioport_writel(opaque
, addr
, val
);
355 static const MemoryRegionOps platform_fixed_io_ops
= {
356 .read
= platform_fixed_ioport_read
,
357 .write
= platform_fixed_ioport_write
,
362 .min_access_size
= 1,
363 .max_access_size
= 4,
366 .endianness
= DEVICE_LITTLE_ENDIAN
,
369 static void platform_fixed_ioport_init(PCIXenPlatformState
* s
)
371 memory_region_init_io(&s
->fixed_io
, OBJECT(s
), &platform_fixed_io_ops
, s
,
373 memory_region_add_subregion(get_system_io(), XEN_PLATFORM_IOPORT
,
377 /* Xen Platform PCI Device */
379 static uint64_t xen_platform_ioport_readb(void *opaque
, hwaddr addr
,
383 return platform_fixed_ioport_readb(opaque
, 0);
389 static void xen_platform_ioport_writeb(void *opaque
, hwaddr addr
,
390 uint64_t val
, unsigned int size
)
392 PCIXenPlatformState
*s
= opaque
;
393 PCIDevice
*pci_dev
= PCI_DEVICE(s
);
396 case 0: /* Platform flags */
397 platform_fixed_ioport_writeb(opaque
, 0, (uint32_t)val
);
402 * SUSE unplug for Xenlinux
403 * xen-kmp used this since xen-3.0.4, instead the official protocol
404 * from xen-3.3+ It did an unconditional "outl(1, (ioaddr + 4));"
405 * Pre VMDP 1.7 used 4 and 8 depending on how VMDP was configured.
406 * If VMDP was to control both disk and LAN it would use 4.
407 * If it controlled just disk or just LAN, it would use 8 below.
409 pci_unplug_disks(pci_get_bus(pci_dev
), UNPLUG_IDE_SCSI_DISKS
);
410 pci_unplug_nics(pci_get_bus(pci_dev
));
416 pci_unplug_disks(pci_get_bus(pci_dev
), UNPLUG_IDE_SCSI_DISKS
);
419 pci_unplug_nics(pci_get_bus(pci_dev
));
422 log_writeb(s
, (uint32_t)val
);
431 static const MemoryRegionOps xen_pci_io_ops
= {
432 .read
= xen_platform_ioport_readb
,
433 .write
= xen_platform_ioport_writeb
,
434 .impl
.min_access_size
= 1,
435 .impl
.max_access_size
= 1,
438 static void platform_ioport_bar_setup(PCIXenPlatformState
*d
)
440 memory_region_init_io(&d
->bar
, OBJECT(d
), &xen_pci_io_ops
, d
,
444 static uint64_t platform_mmio_read(void *opaque
, hwaddr addr
,
447 DPRINTF("Warning: attempted read from physical address "
448 "0x" HWADDR_FMT_plx
" in xen platform mmio space\n", addr
);
453 static void platform_mmio_write(void *opaque
, hwaddr addr
,
454 uint64_t val
, unsigned size
)
456 DPRINTF("Warning: attempted write of 0x%"PRIx64
" to physical "
457 "address 0x" HWADDR_FMT_plx
" in xen platform mmio space\n",
461 static const MemoryRegionOps platform_mmio_handler
= {
462 .read
= &platform_mmio_read
,
463 .write
= &platform_mmio_write
,
464 .endianness
= DEVICE_NATIVE_ENDIAN
,
467 static void platform_mmio_setup(PCIXenPlatformState
*d
)
469 memory_region_init_io(&d
->mmio_bar
, OBJECT(d
), &platform_mmio_handler
, d
,
470 "xen-mmio", 0x1000000);
473 static int xen_platform_post_load(void *opaque
, int version_id
)
475 PCIXenPlatformState
*s
= opaque
;
477 platform_fixed_ioport_writeb(s
, 0, s
->flags
);
482 static const VMStateDescription vmstate_xen_platform
= {
485 .minimum_version_id
= 4,
486 .post_load
= xen_platform_post_load
,
487 .fields
= (VMStateField
[]) {
488 VMSTATE_PCI_DEVICE(parent_obj
, PCIXenPlatformState
),
489 VMSTATE_UINT8(flags
, PCIXenPlatformState
),
490 VMSTATE_END_OF_LIST()
494 static void xen_platform_realize(PCIDevice
*dev
, Error
**errp
)
496 PCIXenPlatformState
*d
= XEN_PLATFORM(dev
);
499 /* Device will crash on reset if xen is not initialized */
500 if (!xen_enabled()) {
501 error_setg(errp
, "xen-platform device requires the Xen accelerator");
505 pci_conf
= dev
->config
;
507 pci_set_word(pci_conf
+ PCI_COMMAND
, PCI_COMMAND_IO
| PCI_COMMAND_MEMORY
);
509 pci_config_set_prog_interface(pci_conf
, 0);
511 pci_conf
[PCI_INTERRUPT_PIN
] = 1;
513 platform_ioport_bar_setup(d
);
514 pci_register_bar(dev
, 0, PCI_BASE_ADDRESS_SPACE_IO
, &d
->bar
);
516 /* reserve 16MB mmio address for share memory*/
517 platform_mmio_setup(d
);
518 pci_register_bar(dev
, 1, PCI_BASE_ADDRESS_MEM_PREFETCH
,
521 platform_fixed_ioport_init(d
);
524 static void platform_reset(DeviceState
*dev
)
526 PCIXenPlatformState
*s
= XEN_PLATFORM(dev
);
528 platform_fixed_ioport_reset(s
);
531 static void xen_platform_class_init(ObjectClass
*klass
, void *data
)
533 DeviceClass
*dc
= DEVICE_CLASS(klass
);
534 PCIDeviceClass
*k
= PCI_DEVICE_CLASS(klass
);
536 k
->realize
= xen_platform_realize
;
537 k
->vendor_id
= PCI_VENDOR_ID_XEN
;
538 k
->device_id
= PCI_DEVICE_ID_XEN_PLATFORM
;
539 k
->class_id
= PCI_CLASS_OTHERS
<< 8 | 0x80;
540 k
->subsystem_vendor_id
= PCI_VENDOR_ID_XEN
;
541 k
->subsystem_id
= PCI_DEVICE_ID_XEN_PLATFORM
;
543 set_bit(DEVICE_CATEGORY_MISC
, dc
->categories
);
544 dc
->desc
= "XEN platform pci device";
545 dc
->reset
= platform_reset
;
546 dc
->vmsd
= &vmstate_xen_platform
;
549 static const TypeInfo xen_platform_info
= {
550 .name
= TYPE_XEN_PLATFORM
,
551 .parent
= TYPE_PCI_DEVICE
,
552 .instance_size
= sizeof(PCIXenPlatformState
),
553 .class_init
= xen_platform_class_init
,
554 .interfaces
= (InterfaceInfo
[]) {
555 { INTERFACE_CONVENTIONAL_PCI_DEVICE
},
560 static void xen_platform_register_types(void)
562 type_register_static(&xen_platform_info
);
565 type_init(xen_platform_register_types
)