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"
28 #include "hw/i386/pc.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
;
89 /* Xen Platform, Fixed IOPort */
90 #define UNPLUG_ALL_IDE_DISKS 1
91 #define UNPLUG_ALL_NICS 2
92 #define UNPLUG_AUX_IDE_DISKS 4
94 static void unplug_nic(PCIBus
*b
, PCIDevice
*d
, void *o
)
96 /* We have to ignore passthrough devices */
97 if (pci_get_word(d
->config
+ PCI_CLASS_DEVICE
) ==
98 PCI_CLASS_NETWORK_ETHERNET
99 && strcmp(d
->name
, "xen-pci-passthrough") != 0) {
100 object_unparent(OBJECT(d
));
104 static void pci_unplug_nics(PCIBus
*bus
)
106 pci_for_each_device(bus
, 0, unplug_nic
, NULL
);
109 static void unplug_disks(PCIBus
*b
, PCIDevice
*d
, void *o
)
111 /* We have to ignore passthrough devices */
112 if (pci_get_word(d
->config
+ PCI_CLASS_DEVICE
) ==
113 PCI_CLASS_STORAGE_IDE
114 && strcmp(d
->name
, "xen-pci-passthrough") != 0) {
115 pci_piix3_xen_ide_unplug(DEVICE(d
));
119 static void pci_unplug_disks(PCIBus
*bus
)
121 pci_for_each_device(bus
, 0, unplug_disks
, NULL
);
124 static void platform_fixed_ioport_writew(void *opaque
, uint32_t addr
, uint32_t val
)
126 PCIXenPlatformState
*s
= opaque
;
130 PCIDevice
*pci_dev
= PCI_DEVICE(s
);
131 /* Unplug devices. Value is a bitmask of which devices to
132 unplug, with bit 0 the IDE devices, bit 1 the network
133 devices, and bit 2 the non-primary-master IDE devices. */
134 if (val
& UNPLUG_ALL_IDE_DISKS
) {
135 DPRINTF("unplug disks\n");
138 pci_unplug_disks(pci_dev
->bus
);
140 if (val
& UNPLUG_ALL_NICS
) {
141 DPRINTF("unplug nics\n");
142 pci_unplug_nics(pci_dev
->bus
);
144 if (val
& UNPLUG_AUX_IDE_DISKS
) {
145 DPRINTF("unplug auxiliary disks not supported\n");
152 DPRINTF("Citrix Windows PV drivers loaded in guest\n");
155 DPRINTF("Guest claimed to be running PV product 0?\n");
158 DPRINTF("Unknown PV product %d loaded in guest\n", val
);
161 s
->driver_product_version
= val
;
166 static void platform_fixed_ioport_writel(void *opaque
, uint32_t addr
,
171 /* PV driver version */
176 static void platform_fixed_ioport_writeb(void *opaque
, uint32_t addr
, uint32_t val
)
178 PCIXenPlatformState
*s
= opaque
;
181 case 0: /* Platform flags */ {
182 hvmmem_type_t mem_type
= (val
& PFFLAG_ROM_LOCK
) ?
183 HVMMEM_ram_ro
: HVMMEM_ram_rw
;
184 if (xc_hvm_set_mem_type(xen_xc
, xen_domid
, mem_type
, 0xc0, 0x40)) {
185 DPRINTF("unable to change ro/rw state of ROM memory area!\n");
187 s
->flags
= val
& PFFLAG_ROM_LOCK
;
188 DPRINTF("changed ro/rw state of ROM memory area. now is %s state.\n",
189 (mem_type
== HVMMEM_ram_ro
? "ro":"rw"));
199 static uint32_t platform_fixed_ioport_readw(void *opaque
, uint32_t addr
)
201 PCIXenPlatformState
*s
= opaque
;
205 if (s
->drivers_blacklisted
) {
206 /* The drivers will recognise this magic number and refuse
210 /* Magic value so that you can identify the interface. */
218 static uint32_t platform_fixed_ioport_readb(void *opaque
, uint32_t addr
)
220 PCIXenPlatformState
*s
= opaque
;
234 static void platform_fixed_ioport_reset(void *opaque
)
236 PCIXenPlatformState
*s
= opaque
;
238 platform_fixed_ioport_writeb(s
, 0, 0);
241 static uint64_t platform_fixed_ioport_read(void *opaque
,
247 return platform_fixed_ioport_readb(opaque
, addr
);
249 return platform_fixed_ioport_readw(opaque
, addr
);
255 static void platform_fixed_ioport_write(void *opaque
, hwaddr addr
,
257 uint64_t val
, unsigned size
)
261 platform_fixed_ioport_writeb(opaque
, addr
, val
);
264 platform_fixed_ioport_writew(opaque
, addr
, val
);
267 platform_fixed_ioport_writel(opaque
, addr
, val
);
273 static const MemoryRegionOps platform_fixed_io_ops
= {
274 .read
= platform_fixed_ioport_read
,
275 .write
= platform_fixed_ioport_write
,
280 .min_access_size
= 1,
281 .max_access_size
= 4,
284 .endianness
= DEVICE_LITTLE_ENDIAN
,
287 static void platform_fixed_ioport_init(PCIXenPlatformState
* s
)
289 memory_region_init_io(&s
->fixed_io
, OBJECT(s
), &platform_fixed_io_ops
, s
,
291 memory_region_add_subregion(get_system_io(), XEN_PLATFORM_IOPORT
,
295 /* Xen Platform PCI Device */
297 static uint64_t xen_platform_ioport_readb(void *opaque
, hwaddr addr
,
301 return platform_fixed_ioport_readb(opaque
, 0);
307 static void xen_platform_ioport_writeb(void *opaque
, hwaddr addr
,
308 uint64_t val
, unsigned int size
)
310 PCIXenPlatformState
*s
= opaque
;
313 case 0: /* Platform flags */
314 platform_fixed_ioport_writeb(opaque
, 0, (uint32_t)val
);
317 log_writeb(s
, (uint32_t)val
);
324 static const MemoryRegionOps xen_pci_io_ops
= {
325 .read
= xen_platform_ioport_readb
,
326 .write
= xen_platform_ioport_writeb
,
327 .impl
.min_access_size
= 1,
328 .impl
.max_access_size
= 1,
331 static void platform_ioport_bar_setup(PCIXenPlatformState
*d
)
333 memory_region_init_io(&d
->bar
, OBJECT(d
), &xen_pci_io_ops
, d
,
337 static uint64_t platform_mmio_read(void *opaque
, hwaddr addr
,
340 DPRINTF("Warning: attempted read from physical address "
341 "0x" TARGET_FMT_plx
" in xen platform mmio space\n", addr
);
346 static void platform_mmio_write(void *opaque
, hwaddr addr
,
347 uint64_t val
, unsigned size
)
349 DPRINTF("Warning: attempted write of 0x%"PRIx64
" to physical "
350 "address 0x" TARGET_FMT_plx
" in xen platform mmio space\n",
354 static const MemoryRegionOps platform_mmio_handler
= {
355 .read
= &platform_mmio_read
,
356 .write
= &platform_mmio_write
,
357 .endianness
= DEVICE_NATIVE_ENDIAN
,
360 static void platform_mmio_setup(PCIXenPlatformState
*d
)
362 memory_region_init_io(&d
->mmio_bar
, OBJECT(d
), &platform_mmio_handler
, d
,
363 "xen-mmio", 0x1000000);
366 static int xen_platform_post_load(void *opaque
, int version_id
)
368 PCIXenPlatformState
*s
= opaque
;
370 platform_fixed_ioport_writeb(s
, 0, s
->flags
);
375 static const VMStateDescription vmstate_xen_platform
= {
378 .minimum_version_id
= 4,
379 .post_load
= xen_platform_post_load
,
380 .fields
= (VMStateField
[]) {
381 VMSTATE_PCI_DEVICE(parent_obj
, PCIXenPlatformState
),
382 VMSTATE_UINT8(flags
, PCIXenPlatformState
),
383 VMSTATE_END_OF_LIST()
387 static void xen_platform_realize(PCIDevice
*dev
, Error
**errp
)
389 PCIXenPlatformState
*d
= XEN_PLATFORM(dev
);
392 /* Device will crash on reset if xen is not initialized */
393 if (!xen_enabled()) {
394 error_setg(errp
, "xen-platform device requires the Xen accelerator");
398 pci_conf
= dev
->config
;
400 pci_set_word(pci_conf
+ PCI_COMMAND
, PCI_COMMAND_IO
| PCI_COMMAND_MEMORY
);
402 pci_config_set_prog_interface(pci_conf
, 0);
404 pci_conf
[PCI_INTERRUPT_PIN
] = 1;
406 platform_ioport_bar_setup(d
);
407 pci_register_bar(dev
, 0, PCI_BASE_ADDRESS_SPACE_IO
, &d
->bar
);
409 /* reserve 16MB mmio address for share memory*/
410 platform_mmio_setup(d
);
411 pci_register_bar(dev
, 1, PCI_BASE_ADDRESS_MEM_PREFETCH
,
414 platform_fixed_ioport_init(d
);
417 static void platform_reset(DeviceState
*dev
)
419 PCIXenPlatformState
*s
= XEN_PLATFORM(dev
);
421 platform_fixed_ioport_reset(s
);
424 static void xen_platform_class_init(ObjectClass
*klass
, void *data
)
426 DeviceClass
*dc
= DEVICE_CLASS(klass
);
427 PCIDeviceClass
*k
= PCI_DEVICE_CLASS(klass
);
429 k
->realize
= xen_platform_realize
;
430 k
->vendor_id
= PCI_VENDOR_ID_XEN
;
431 k
->device_id
= PCI_DEVICE_ID_XEN_PLATFORM
;
432 k
->class_id
= PCI_CLASS_OTHERS
<< 8 | 0x80;
433 k
->subsystem_vendor_id
= PCI_VENDOR_ID_XEN
;
434 k
->subsystem_id
= PCI_DEVICE_ID_XEN_PLATFORM
;
436 set_bit(DEVICE_CATEGORY_MISC
, dc
->categories
);
437 dc
->desc
= "XEN platform pci device";
438 dc
->reset
= platform_reset
;
439 dc
->vmsd
= &vmstate_xen_platform
;
442 static const TypeInfo xen_platform_info
= {
443 .name
= TYPE_XEN_PLATFORM
,
444 .parent
= TYPE_PCI_DEVICE
,
445 .instance_size
= sizeof(PCIXenPlatformState
),
446 .class_init
= xen_platform_class_init
,
449 static void xen_platform_register_types(void)
451 type_register_static(&xen_platform_info
);
454 type_init(xen_platform_register_types
)