cpu: flush TB cache when loading VMState
[qemu/ar7.git] / hw / i386 / xen / xen_platform.c
blobfc8623c90bf6262153bc57e16ddfb75c770f7594
1 /*
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
23 * THE SOFTWARE.
26 #include "qemu/osdep.h"
27 #include "qapi/error.h"
28 #include "hw/hw.h"
29 #include "hw/ide.h"
30 #include "hw/pci/pci.h"
31 #include "hw/irq.h"
32 #include "hw/xen/xen_common.h"
33 #include "hw/xen/xen_backend.h"
34 #include "trace.h"
35 #include "exec/address-spaces.h"
36 #include "sysemu/block-backend.h"
37 #include "qemu/error-report.h"
39 #include <xenguest.h>
41 //#define DEBUG_PLATFORM
43 #ifdef DEBUG_PLATFORM
44 #define DPRINTF(fmt, ...) do { \
45 fprintf(stderr, "xen_platform: " fmt, ## __VA_ARGS__); \
46 } while (0)
47 #else
48 #define DPRINTF(fmt, ...) do { } while (0)
49 #endif
51 #define PFFLAG_ROM_LOCK 1 /* Sets whether ROM memory area is RW or RO */
53 typedef struct PCIXenPlatformState {
54 /*< private >*/
55 PCIDevice parent_obj;
56 /*< public >*/
58 MemoryRegion fixed_io;
59 MemoryRegion bar;
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];
67 int log_buffer_off;
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) {
80 /* Flush buffer */
81 s->log_buffer[s->log_buffer_off] = 0;
82 trace_xen_platform_log(s->log_buffer);
83 s->log_buffer_off = 0;
84 } else {
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
93 * what they do now.
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)
127 NetClientState *nc;
129 nc = qemu_get_queue(nic);
130 if (nc->peer)
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")) {
148 return;
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);
154 break;
156 case PCI_CLASS_STORAGE_SCSI:
157 if (!aux) {
158 object_unparent(OBJECT(d));
160 break;
162 case PCI_CLASS_STORAGE_EXPRESS:
163 if (flags & UNPLUG_NVME_DISKS) {
164 object_unparent(OBJECT(d));
167 default:
168 break;
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;
181 switch (addr) {
182 case 0: {
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);
194 break;
196 case 2:
197 switch (val) {
198 case 1:
199 DPRINTF("Citrix Windows PV drivers loaded in guest\n");
200 break;
201 case 0:
202 DPRINTF("Guest claimed to be running PV product 0?\n");
203 break;
204 default:
205 DPRINTF("Unknown PV product %d loaded in guest\n", val);
206 break;
208 s->driver_product_version = val;
209 break;
213 static void platform_fixed_ioport_writel(void *opaque, uint32_t addr,
214 uint32_t val)
216 switch (addr) {
217 case 0:
218 /* PV driver version */
219 break;
223 static void platform_fixed_ioport_writeb(void *opaque, uint32_t addr, uint32_t val)
225 PCIXenPlatformState *s = opaque;
227 switch (addr) {
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");
233 } else {
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"));
238 break;
240 case 2:
241 log_writeb(s, val);
242 break;
246 static uint32_t platform_fixed_ioport_readw(void *opaque, uint32_t addr)
248 PCIXenPlatformState *s = opaque;
250 switch (addr) {
251 case 0:
252 if (s->drivers_blacklisted) {
253 /* The drivers will recognise this magic number and refuse
254 * to do anything. */
255 return 0xd249;
256 } else {
257 /* Magic value so that you can identify the interface. */
258 return 0x49d2;
260 default:
261 return 0xffff;
265 static uint32_t platform_fixed_ioport_readb(void *opaque, uint32_t addr)
267 PCIXenPlatformState *s = opaque;
269 switch (addr) {
270 case 0:
271 /* Platform flags */
272 return s->flags;
273 case 2:
274 /* Version number */
275 return 1;
276 default:
277 return 0xff;
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,
289 hwaddr addr,
290 unsigned size)
292 switch (size) {
293 case 1:
294 return platform_fixed_ioport_readb(opaque, addr);
295 case 2:
296 return platform_fixed_ioport_readw(opaque, addr);
297 default:
298 return -1;
302 static void platform_fixed_ioport_write(void *opaque, hwaddr addr,
304 uint64_t val, unsigned size)
306 switch (size) {
307 case 1:
308 platform_fixed_ioport_writeb(opaque, addr, val);
309 break;
310 case 2:
311 platform_fixed_ioport_writew(opaque, addr, val);
312 break;
313 case 4:
314 platform_fixed_ioport_writel(opaque, addr, val);
315 break;
320 static const MemoryRegionOps platform_fixed_io_ops = {
321 .read = platform_fixed_ioport_read,
322 .write = platform_fixed_ioport_write,
323 .valid = {
324 .unaligned = true,
326 .impl = {
327 .min_access_size = 1,
328 .max_access_size = 4,
329 .unaligned = true,
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,
337 "xen-fixed", 16);
338 memory_region_add_subregion(get_system_io(), XEN_PLATFORM_IOPORT,
339 &s->fixed_io);
342 /* Xen Platform PCI Device */
344 static uint64_t xen_platform_ioport_readb(void *opaque, hwaddr addr,
345 unsigned int size)
347 if (addr == 0) {
348 return platform_fixed_ioport_readb(opaque, 0);
349 } else {
350 return ~0u;
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);
360 switch (addr) {
361 case 0: /* Platform flags */
362 platform_fixed_ioport_writeb(opaque, 0, (uint32_t)val);
363 break;
364 case 4:
365 if (val == 1) {
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);
377 break;
378 case 8:
379 switch (val) {
380 case 1:
381 pci_unplug_disks(pci_dev->bus, UNPLUG_IDE_SCSI_DISKS);
382 break;
383 case 2:
384 pci_unplug_nics(pci_dev->bus);
385 break;
386 default:
387 log_writeb(s, (uint32_t)val);
388 break;
390 break;
391 default:
392 break;
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,
406 "xen-pci", 0x100);
409 static uint64_t platform_mmio_read(void *opaque, hwaddr addr,
410 unsigned size)
412 DPRINTF("Warning: attempted read from physical address "
413 "0x" TARGET_FMT_plx " in xen platform mmio space\n", addr);
415 return 0;
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",
423 val, addr);
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);
444 return 0;
447 static const VMStateDescription vmstate_xen_platform = {
448 .name = "platform",
449 .version_id = 4,
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);
462 uint8_t *pci_conf;
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");
467 return;
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,
484 &d->mmio_bar);
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;
507 k->revision = 1;
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 },
521 { },
525 static void xen_platform_register_types(void)
527 type_register_static(&xen_platform_info);
530 type_init(xen_platform_register_types)