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
32 #include "xen_common.h"
34 #include "xen_backend.h"
35 #include "rwhandler.h"
40 //#define DEBUG_PLATFORM
43 #define DPRINTF(fmt, ...) do { \
44 fprintf(stderr, "xen_platform: " fmt, ## __VA_ARGS__); \
47 #define DPRINTF(fmt, ...) do { } while (0)
50 #define PFFLAG_ROM_LOCK 1 /* Sets whether ROM memory area is RW or RO */
52 typedef struct PCIXenPlatformState
{
54 uint8_t flags
; /* used only for version_id == 2 */
55 int drivers_blacklisted
;
56 uint16_t driver_product_version
;
58 /* Log from guest drivers */
59 char log_buffer
[4096];
61 } PCIXenPlatformState
;
63 #define XEN_PLATFORM_IOPORT 0x10
65 /* Send bytes to syslog */
66 static void log_writeb(PCIXenPlatformState
*s
, char val
)
68 if (val
== '\n' || s
->log_buffer_off
== sizeof(s
->log_buffer
) - 1) {
70 s
->log_buffer
[s
->log_buffer_off
] = 0;
71 trace_xen_platform_log(s
->log_buffer
);
72 s
->log_buffer_off
= 0;
74 s
->log_buffer
[s
->log_buffer_off
++] = val
;
78 /* Xen Platform, Fixed IOPort */
80 static void platform_fixed_ioport_writew(void *opaque
, uint32_t addr
, uint32_t val
)
82 PCIXenPlatformState
*s
= opaque
;
84 switch (addr
- XEN_PLATFORM_IOPORT
) {
87 /* Unplug devices. Value is a bitmask of which devices to
88 unplug, with bit 0 the IDE devices, bit 1 the network
89 devices, and bit 2 the non-primary-master IDE devices. */
94 DPRINTF("Citrix Windows PV drivers loaded in guest\n");
97 DPRINTF("Guest claimed to be running PV product 0?\n");
100 DPRINTF("Unknown PV product %d loaded in guest\n", val
);
103 s
->driver_product_version
= val
;
108 static void platform_fixed_ioport_writel(void *opaque
, uint32_t addr
,
111 switch (addr
- XEN_PLATFORM_IOPORT
) {
113 /* PV driver version */
118 static void platform_fixed_ioport_writeb(void *opaque
, uint32_t addr
, uint32_t val
)
120 PCIXenPlatformState
*s
= opaque
;
122 switch (addr
- XEN_PLATFORM_IOPORT
) {
123 case 0: /* Platform flags */ {
124 hvmmem_type_t mem_type
= (val
& PFFLAG_ROM_LOCK
) ?
125 HVMMEM_ram_ro
: HVMMEM_ram_rw
;
126 if (xc_hvm_set_mem_type(xen_xc
, xen_domid
, mem_type
, 0xc0, 0x40)) {
127 DPRINTF("unable to change ro/rw state of ROM memory area!\n");
129 s
->flags
= val
& PFFLAG_ROM_LOCK
;
130 DPRINTF("changed ro/rw state of ROM memory area. now is %s state.\n",
131 (mem_type
== HVMMEM_ram_ro
? "ro":"rw"));
141 static uint32_t platform_fixed_ioport_readw(void *opaque
, uint32_t addr
)
143 PCIXenPlatformState
*s
= opaque
;
145 switch (addr
- XEN_PLATFORM_IOPORT
) {
147 if (s
->drivers_blacklisted
) {
148 /* The drivers will recognise this magic number and refuse
152 /* Magic value so that you can identify the interface. */
160 static uint32_t platform_fixed_ioport_readb(void *opaque
, uint32_t addr
)
162 PCIXenPlatformState
*s
= opaque
;
164 switch (addr
- XEN_PLATFORM_IOPORT
) {
176 static void platform_fixed_ioport_reset(void *opaque
)
178 PCIXenPlatformState
*s
= opaque
;
180 platform_fixed_ioport_writeb(s
, XEN_PLATFORM_IOPORT
, 0);
183 static void platform_fixed_ioport_init(PCIXenPlatformState
* s
)
185 register_ioport_write(XEN_PLATFORM_IOPORT
, 16, 4, platform_fixed_ioport_writel
, s
);
186 register_ioport_write(XEN_PLATFORM_IOPORT
, 16, 2, platform_fixed_ioport_writew
, s
);
187 register_ioport_write(XEN_PLATFORM_IOPORT
, 16, 1, platform_fixed_ioport_writeb
, s
);
188 register_ioport_read(XEN_PLATFORM_IOPORT
, 16, 2, platform_fixed_ioport_readw
, s
);
189 register_ioport_read(XEN_PLATFORM_IOPORT
, 16, 1, platform_fixed_ioport_readb
, s
);
192 /* Xen Platform PCI Device */
194 static uint32_t xen_platform_ioport_readb(void *opaque
, uint32_t addr
)
199 return platform_fixed_ioport_readb(opaque
, XEN_PLATFORM_IOPORT
);
205 static void xen_platform_ioport_writeb(void *opaque
, uint32_t addr
, uint32_t val
)
207 PCIXenPlatformState
*s
= opaque
;
213 case 0: /* Platform flags */
214 platform_fixed_ioport_writeb(opaque
, XEN_PLATFORM_IOPORT
, val
);
224 static void platform_ioport_map(PCIDevice
*pci_dev
, int region_num
, pcibus_t addr
, pcibus_t size
, int type
)
226 PCIXenPlatformState
*d
= DO_UPCAST(PCIXenPlatformState
, pci_dev
, pci_dev
);
228 register_ioport_write(addr
, size
, 1, xen_platform_ioport_writeb
, d
);
229 register_ioport_read(addr
, size
, 1, xen_platform_ioport_readb
, d
);
232 static uint32_t platform_mmio_read(ReadWriteHandler
*handler
, pcibus_t addr
, int len
)
234 DPRINTF("Warning: attempted read from physical address "
235 "0x" TARGET_FMT_plx
" in xen platform mmio space\n", addr
);
240 static void platform_mmio_write(ReadWriteHandler
*handler
, pcibus_t addr
,
241 uint32_t val
, int len
)
243 DPRINTF("Warning: attempted write of 0x%x to physical "
244 "address 0x" TARGET_FMT_plx
" in xen platform mmio space\n",
248 static ReadWriteHandler platform_mmio_handler
= {
249 .read
= &platform_mmio_read
,
250 .write
= &platform_mmio_write
,
253 static void platform_mmio_map(PCIDevice
*d
, int region_num
,
254 pcibus_t addr
, pcibus_t size
, int type
)
258 mmio_io_addr
= cpu_register_io_memory_simple(&platform_mmio_handler
,
259 DEVICE_NATIVE_ENDIAN
);
261 cpu_register_physical_memory(addr
, size
, mmio_io_addr
);
264 static int xen_platform_post_load(void *opaque
, int version_id
)
266 PCIXenPlatformState
*s
= opaque
;
268 platform_fixed_ioport_writeb(s
, XEN_PLATFORM_IOPORT
, s
->flags
);
273 static const VMStateDescription vmstate_xen_platform
= {
276 .minimum_version_id
= 4,
277 .minimum_version_id_old
= 4,
278 .post_load
= xen_platform_post_load
,
279 .fields
= (VMStateField
[]) {
280 VMSTATE_PCI_DEVICE(pci_dev
, PCIXenPlatformState
),
281 VMSTATE_UINT8(flags
, PCIXenPlatformState
),
282 VMSTATE_END_OF_LIST()
286 static int xen_platform_initfn(PCIDevice
*dev
)
288 PCIXenPlatformState
*d
= DO_UPCAST(PCIXenPlatformState
, pci_dev
, dev
);
291 pci_conf
= d
->pci_dev
.config
;
293 pci_set_word(pci_conf
+ PCI_COMMAND
, PCI_COMMAND_IO
| PCI_COMMAND_MEMORY
);
295 pci_config_set_prog_interface(pci_conf
, 0);
297 pci_conf
[PCI_INTERRUPT_PIN
] = 1;
299 pci_register_bar(&d
->pci_dev
, 0, 0x100,
300 PCI_BASE_ADDRESS_SPACE_IO
, platform_ioport_map
);
302 /* reserve 16MB mmio address for share memory*/
303 pci_register_bar(&d
->pci_dev
, 1, 0x1000000,
304 PCI_BASE_ADDRESS_MEM_PREFETCH
, platform_mmio_map
);
306 platform_fixed_ioport_init(d
);
311 static void platform_reset(DeviceState
*dev
)
313 PCIXenPlatformState
*s
= DO_UPCAST(PCIXenPlatformState
, pci_dev
.qdev
, dev
);
315 platform_fixed_ioport_reset(s
);
318 static PCIDeviceInfo xen_platform_info
= {
319 .init
= xen_platform_initfn
,
320 .qdev
.name
= "xen-platform",
321 .qdev
.desc
= "XEN platform pci device",
322 .qdev
.size
= sizeof(PCIXenPlatformState
),
323 .qdev
.vmsd
= &vmstate_xen_platform
,
324 .qdev
.reset
= platform_reset
,
326 .vendor_id
= PCI_VENDOR_ID_XEN
,
327 .device_id
= PCI_DEVICE_ID_XEN_PLATFORM
,
328 .class_id
= PCI_CLASS_OTHERS
<< 8 | 0x80,
329 .subsystem_vendor_id
= PCI_VENDOR_ID_XEN
,
330 .subsystem_id
= PCI_DEVICE_ID_XEN_PLATFORM
,
334 static void xen_platform_register(void)
336 pci_qdev_register(&xen_platform_info
);
339 device_init(xen_platform_register
);