]> git.proxmox.com Git - mirror_qemu.git/blame - hw/i386/xen/xen_platform.c
xen-platform: do full PCI reset during unplug of IDE devices
[mirror_qemu.git] / hw / i386 / xen / xen_platform.c
CommitLineData
01195b73
SS
1/*
2 * XEN platform pci device, formerly known as the event channel device
3 *
4 * Copyright (c) 2003-2004 Intel Corp.
5 * Copyright (c) 2006 XenSource
6 *
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:
13 *
14 * The above copyright notice and this permission notice shall be included in
15 * all copies or substantial portions of the Software.
16 *
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.
24 */
25
b6a0aa05 26#include "qemu/osdep.h"
da34e65c 27#include "qapi/error.h"
6a8a8b62 28#include "hw/ide/pci.h"
83c9f4ca 29#include "hw/pci/pci.h"
d6454270 30#include "migration/vmstate.h"
bb346fae 31#include "net/net.h"
01195b73 32#include "trace.h"
da278d58 33#include "sysemu/xen.h"
4be74634 34#include "sysemu/block-backend.h"
b1ecd51b 35#include "qemu/error-report.h"
0b8fa32f 36#include "qemu/module.h"
db1015e9 37#include "qom/object.h"
01195b73 38
bb346fae 39#ifdef CONFIG_XEN
e2abfe5e 40#include "hw/xen/xen_native.h"
bb346fae
JM
41#endif
42
e2abfe5e
DW
43/* The rule is that xen_native.h must come first */
44#include "hw/xen/xen.h"
45
01195b73
SS
46//#define DEBUG_PLATFORM
47
48#ifdef DEBUG_PLATFORM
49#define DPRINTF(fmt, ...) do { \
50 fprintf(stderr, "xen_platform: " fmt, ## __VA_ARGS__); \
51} while (0)
52#else
53#define DPRINTF(fmt, ...) do { } while (0)
54#endif
55
56#define PFFLAG_ROM_LOCK 1 /* Sets whether ROM memory area is RW or RO */
57
db1015e9 58struct PCIXenPlatformState {
dc4aa51b
AF
59 /*< private >*/
60 PCIDevice parent_obj;
61 /*< public >*/
62
de00982e
AK
63 MemoryRegion fixed_io;
64 MemoryRegion bar;
65 MemoryRegion mmio_bar;
01195b73 66 uint8_t flags; /* used only for version_id == 2 */
01195b73
SS
67 uint16_t driver_product_version;
68
69 /* Log from guest drivers */
70 char log_buffer[4096];
71 int log_buffer_off;
db1015e9 72};
01195b73 73
51a3fe99 74#define TYPE_XEN_PLATFORM "xen-platform"
8063396b 75OBJECT_DECLARE_SIMPLE_TYPE(PCIXenPlatformState, XEN_PLATFORM)
51a3fe99 76
01195b73
SS
77#define XEN_PLATFORM_IOPORT 0x10
78
79/* Send bytes to syslog */
80static void log_writeb(PCIXenPlatformState *s, char val)
81{
82 if (val == '\n' || s->log_buffer_off == sizeof(s->log_buffer) - 1) {
83 /* Flush buffer */
84 s->log_buffer[s->log_buffer_off] = 0;
85 trace_xen_platform_log(s->log_buffer);
86 s->log_buffer_off = 0;
87 } else {
88 s->log_buffer[s->log_buffer_off++] = val;
89 }
90}
91
04d6da4f
SS
92/*
93 * Unplug device flags.
94 *
95 * The logic got a little confused at some point in the past but this is
96 * what they do now.
97 *
98 * bit 0: Unplug all IDE and SCSI disks.
99 * bit 1: Unplug all NICs.
100 * bit 2: Unplug IDE disks except primary master. This is overridden if
101 * bit 0 is also present in the mask.
102 * bit 3: Unplug all NVMe disks.
103 *
104 */
105#define _UNPLUG_IDE_SCSI_DISKS 0
106#define UNPLUG_IDE_SCSI_DISKS (1u << _UNPLUG_IDE_SCSI_DISKS)
107
108#define _UNPLUG_ALL_NICS 1
109#define UNPLUG_ALL_NICS (1u << _UNPLUG_ALL_NICS)
110
111#define _UNPLUG_AUX_IDE_DISKS 2
112#define UNPLUG_AUX_IDE_DISKS (1u << _UNPLUG_AUX_IDE_DISKS)
113
114#define _UNPLUG_NVME_DISKS 3
115#define UNPLUG_NVME_DISKS (1u << _UNPLUG_NVME_DISKS)
679f4f8b 116
3bb1ebac
JM
117static bool pci_device_is_passthrough(PCIDevice *d)
118{
119 if (!strcmp(d->name, "xen-pci-passthrough")) {
120 return true;
121 }
122
123 if (xen_mode == XEN_EMULATE && !strcmp(d->name, "vfio-pci")) {
124 return true;
125 }
126
127 return false;
128}
129
7aa8cbb9 130static void unplug_nic(PCIBus *b, PCIDevice *d, void *o)
679f4f8b 131{
bd4982a6 132 /* We have to ignore passthrough devices */
679f4f8b 133 if (pci_get_word(d->config + PCI_CLASS_DEVICE) ==
bd4982a6 134 PCI_CLASS_NETWORK_ETHERNET
3bb1ebac 135 && !pci_device_is_passthrough(d)) {
02a5c4c9 136 object_unparent(OBJECT(d));
679f4f8b
SS
137 }
138}
139
6c808651
RL
140/* Remove the peer of the NIC device. Normally, this would be a tap device. */
141static void del_nic_peer(NICState *nic, void *opaque)
142{
143 NetClientState *nc;
144
145 nc = qemu_get_queue(nic);
146 if (nc->peer)
147 qemu_del_net_client(nc->peer);
148}
149
679f4f8b
SS
150static void pci_unplug_nics(PCIBus *bus)
151{
6c808651 152 qemu_foreach_nic(del_nic_peer, NULL);
7aa8cbb9 153 pci_for_each_device(bus, 0, unplug_nic, NULL);
679f4f8b
SS
154}
155
6a8a8b62
BB
156/*
157 * The Xen HVM unplug protocol [1] specifies a mechanism to allow guests to
158 * request unplug of 'aux' disks (which is stated to mean all IDE disks,
159 * except the primary master).
160 *
161 * NOTE: The semantics of what happens if unplug of all disks and 'aux' disks
162 * is simultaneously requested is not clear. The implementation assumes
163 * that an 'all' request overrides an 'aux' request.
164 *
165 * [1] https://xenbits.xen.org/gitweb/?p=xen.git;a=blob;f=docs/misc/hvm-emulated-unplug.pandoc
166 */
856ca10f 167static void pci_xen_ide_unplug(PCIDevice *d, bool aux)
6a8a8b62 168{
856ca10f 169 DeviceState *dev = DEVICE(d);
6a8a8b62
BB
170 PCIIDEState *pci_ide;
171 int i;
172 IDEDevice *idedev;
173 IDEBus *idebus;
174 BlockBackend *blk;
175
176 pci_ide = PCI_IDE(dev);
177
178 for (i = aux ? 1 : 0; i < 4; i++) {
179 idebus = &pci_ide->bus[i / 2];
180 blk = idebus->ifs[i % 2].blk;
181
182 if (blk && idebus->ifs[i % 2].drive_kind != IDE_CD) {
183 if (!(i % 2)) {
184 idedev = idebus->master;
185 } else {
186 idedev = idebus->slave;
187 }
188
189 blk_drain(blk);
190 blk_flush(blk);
191
192 blk_detach_dev(blk, DEVICE(idedev));
193 idebus->ifs[i % 2].blk = NULL;
194 idedev->conf.blk = NULL;
195 monitor_remove_blk(blk);
196 blk_unref(blk);
197 }
198 }
856ca10f 199 pci_device_reset(d);
6a8a8b62
BB
200}
201
ae4d2eb2 202static void unplug_disks(PCIBus *b, PCIDevice *d, void *opaque)
679f4f8b 203{
ae4d2eb2
PD
204 uint32_t flags = *(uint32_t *)opaque;
205 bool aux = (flags & UNPLUG_AUX_IDE_DISKS) &&
04d6da4f 206 !(flags & UNPLUG_IDE_SCSI_DISKS);
ae4d2eb2 207
bd4982a6 208 /* We have to ignore passthrough devices */
3bb1ebac 209 if (pci_device_is_passthrough(d))
3d89e3f7 210 return;
3d89e3f7
PD
211
212 switch (pci_get_word(d->config + PCI_CLASS_DEVICE)) {
213 case PCI_CLASS_STORAGE_IDE:
856ca10f 214 pci_xen_ide_unplug(d, aux);
3d89e3f7
PD
215 break;
216
217 case PCI_CLASS_STORAGE_SCSI:
ae4d2eb2
PD
218 if (!aux) {
219 object_unparent(OBJECT(d));
220 }
3d89e3f7
PD
221 break;
222
04d6da4f
SS
223 case PCI_CLASS_STORAGE_EXPRESS:
224 if (flags & UNPLUG_NVME_DISKS) {
225 object_unparent(OBJECT(d));
226 }
227
3d89e3f7
PD
228 default:
229 break;
679f4f8b
SS
230 }
231}
232
ae4d2eb2 233static void pci_unplug_disks(PCIBus *bus, uint32_t flags)
679f4f8b 234{
ae4d2eb2 235 pci_for_each_device(bus, 0, unplug_disks, &flags);
679f4f8b 236}
01195b73
SS
237
238static void platform_fixed_ioport_writew(void *opaque, uint32_t addr, uint32_t val)
239{
240 PCIXenPlatformState *s = opaque;
241
e7b48c97 242 switch (addr) {
dc4aa51b
AF
243 case 0: {
244 PCIDevice *pci_dev = PCI_DEVICE(s);
04d6da4f
SS
245 /* Unplug devices. See comment above flag definitions */
246 if (val & (UNPLUG_IDE_SCSI_DISKS | UNPLUG_AUX_IDE_DISKS |
247 UNPLUG_NVME_DISKS)) {
679f4f8b 248 DPRINTF("unplug disks\n");
fd56e061 249 pci_unplug_disks(pci_get_bus(pci_dev), val);
679f4f8b
SS
250 }
251 if (val & UNPLUG_ALL_NICS) {
252 DPRINTF("unplug nics\n");
fd56e061 253 pci_unplug_nics(pci_get_bus(pci_dev));
679f4f8b 254 }
01195b73 255 break;
dc4aa51b 256 }
01195b73
SS
257 case 2:
258 switch (val) {
259 case 1:
260 DPRINTF("Citrix Windows PV drivers loaded in guest\n");
261 break;
262 case 0:
263 DPRINTF("Guest claimed to be running PV product 0?\n");
264 break;
265 default:
266 DPRINTF("Unknown PV product %d loaded in guest\n", val);
267 break;
268 }
269 s->driver_product_version = val;
270 break;
271 }
272}
273
274static void platform_fixed_ioport_writel(void *opaque, uint32_t addr,
275 uint32_t val)
276{
e7b48c97 277 switch (addr) {
01195b73
SS
278 case 0:
279 /* PV driver version */
280 break;
281 }
282}
283
284static void platform_fixed_ioport_writeb(void *opaque, uint32_t addr, uint32_t val)
285{
286 PCIXenPlatformState *s = opaque;
287
e7b48c97 288 switch (addr) {
bb346fae
JM
289 case 0: /* Platform flags */
290 if (xen_mode == XEN_EMULATE) {
291 /* XX: Use i440gx/q35 PAM setup to do this? */
01195b73 292 s->flags = val & PFFLAG_ROM_LOCK;
bb346fae
JM
293#ifdef CONFIG_XEN
294 } else {
295 hvmmem_type_t mem_type = (val & PFFLAG_ROM_LOCK) ?
296 HVMMEM_ram_ro : HVMMEM_ram_rw;
297
298 if (xen_set_mem_type(xen_domid, mem_type, 0xc0, 0x40)) {
299 DPRINTF("unable to change ro/rw state of ROM memory area!\n");
300 } else {
301 s->flags = val & PFFLAG_ROM_LOCK;
302 DPRINTF("changed ro/rw state of ROM memory area. now is %s state.\n",
303 (mem_type == HVMMEM_ram_ro ? "ro" : "rw"));
304 }
305#endif
01195b73
SS
306 }
307 break;
bb346fae 308
01195b73
SS
309 case 2:
310 log_writeb(s, val);
311 break;
312 }
313}
314
315static uint32_t platform_fixed_ioport_readw(void *opaque, uint32_t addr)
316{
e7b48c97 317 switch (addr) {
01195b73 318 case 0:
6661d9a5
PMD
319 /* Magic value so that you can identify the interface. */
320 return 0x49d2;
01195b73
SS
321 default:
322 return 0xffff;
323 }
324}
325
326static uint32_t platform_fixed_ioport_readb(void *opaque, uint32_t addr)
327{
328 PCIXenPlatformState *s = opaque;
329
e7b48c97 330 switch (addr) {
01195b73
SS
331 case 0:
332 /* Platform flags */
333 return s->flags;
334 case 2:
335 /* Version number */
336 return 1;
337 default:
338 return 0xff;
339 }
340}
341
342static void platform_fixed_ioport_reset(void *opaque)
343{
344 PCIXenPlatformState *s = opaque;
345
e7b48c97 346 platform_fixed_ioport_writeb(s, 0, 0);
01195b73
SS
347}
348
626c7a17
AG
349static uint64_t platform_fixed_ioport_read(void *opaque,
350 hwaddr addr,
351 unsigned size)
352{
353 switch (size) {
354 case 1:
355 return platform_fixed_ioport_readb(opaque, addr);
356 case 2:
357 return platform_fixed_ioport_readw(opaque, addr);
358 default:
359 return -1;
360 }
361}
362
363static void platform_fixed_ioport_write(void *opaque, hwaddr addr,
364
365 uint64_t val, unsigned size)
366{
367 switch (size) {
368 case 1:
369 platform_fixed_ioport_writeb(opaque, addr, val);
370 break;
371 case 2:
372 platform_fixed_ioport_writew(opaque, addr, val);
373 break;
374 case 4:
375 platform_fixed_ioport_writel(opaque, addr, val);
376 break;
377 }
378}
379
de00982e
AK
380
381static const MemoryRegionOps platform_fixed_io_ops = {
626c7a17
AG
382 .read = platform_fixed_ioport_read,
383 .write = platform_fixed_ioport_write,
962b03fc
JK
384 .valid = {
385 .unaligned = true,
386 },
626c7a17
AG
387 .impl = {
388 .min_access_size = 1,
389 .max_access_size = 4,
962b03fc 390 .unaligned = true,
626c7a17
AG
391 },
392 .endianness = DEVICE_LITTLE_ENDIAN,
de00982e
AK
393};
394
01195b73
SS
395static void platform_fixed_ioport_init(PCIXenPlatformState* s)
396{
22fc860b 397 memory_region_init_io(&s->fixed_io, OBJECT(s), &platform_fixed_io_ops, s,
de00982e
AK
398 "xen-fixed", 16);
399 memory_region_add_subregion(get_system_io(), XEN_PLATFORM_IOPORT,
400 &s->fixed_io);
01195b73
SS
401}
402
403/* Xen Platform PCI Device */
404
7a652efa
HP
405static uint64_t xen_platform_ioport_readb(void *opaque, hwaddr addr,
406 unsigned int size)
01195b73 407{
01195b73 408 if (addr == 0) {
e7b48c97 409 return platform_fixed_ioport_readb(opaque, 0);
01195b73
SS
410 } else {
411 return ~0u;
412 }
413}
414
7a652efa
HP
415static void xen_platform_ioport_writeb(void *opaque, hwaddr addr,
416 uint64_t val, unsigned int size)
01195b73
SS
417{
418 PCIXenPlatformState *s = opaque;
35132016 419 PCIDevice *pci_dev = PCI_DEVICE(s);
01195b73 420
01195b73
SS
421 switch (addr) {
422 case 0: /* Platform flags */
7a652efa 423 platform_fixed_ioport_writeb(opaque, 0, (uint32_t)val);
01195b73 424 break;
35132016
OH
425 case 4:
426 if (val == 1) {
427 /*
428 * SUSE unplug for Xenlinux
429 * xen-kmp used this since xen-3.0.4, instead the official protocol
430 * from xen-3.3+ It did an unconditional "outl(1, (ioaddr + 4));"
431 * Pre VMDP 1.7 used 4 and 8 depending on how VMDP was configured.
432 * If VMDP was to control both disk and LAN it would use 4.
433 * If it controlled just disk or just LAN, it would use 8 below.
434 */
fd56e061
DG
435 pci_unplug_disks(pci_get_bus(pci_dev), UNPLUG_IDE_SCSI_DISKS);
436 pci_unplug_nics(pci_get_bus(pci_dev));
35132016
OH
437 }
438 break;
01195b73 439 case 8:
35132016
OH
440 switch (val) {
441 case 1:
fd56e061 442 pci_unplug_disks(pci_get_bus(pci_dev), UNPLUG_IDE_SCSI_DISKS);
35132016
OH
443 break;
444 case 2:
fd56e061 445 pci_unplug_nics(pci_get_bus(pci_dev));
35132016
OH
446 break;
447 default:
448 log_writeb(s, (uint32_t)val);
449 break;
450 }
01195b73
SS
451 break;
452 default:
453 break;
454 }
455}
456
de00982e 457static const MemoryRegionOps xen_pci_io_ops = {
7a652efa
HP
458 .read = xen_platform_ioport_readb,
459 .write = xen_platform_ioport_writeb,
460 .impl.min_access_size = 1,
461 .impl.max_access_size = 1,
de00982e 462};
01195b73 463
de00982e
AK
464static void platform_ioport_bar_setup(PCIXenPlatformState *d)
465{
22fc860b
PB
466 memory_region_init_io(&d->bar, OBJECT(d), &xen_pci_io_ops, d,
467 "xen-pci", 0x100);
01195b73
SS
468}
469
a8170e5e 470static uint64_t platform_mmio_read(void *opaque, hwaddr addr,
de00982e 471 unsigned size)
01195b73
SS
472{
473 DPRINTF("Warning: attempted read from physical address "
883f2c59 474 "0x" HWADDR_FMT_plx " in xen platform mmio space\n", addr);
01195b73
SS
475
476 return 0;
477}
478
a8170e5e 479static void platform_mmio_write(void *opaque, hwaddr addr,
de00982e 480 uint64_t val, unsigned size)
01195b73 481{
de00982e 482 DPRINTF("Warning: attempted write of 0x%"PRIx64" to physical "
883f2c59 483 "address 0x" HWADDR_FMT_plx " in xen platform mmio space\n",
01195b73
SS
484 val, addr);
485}
486
de00982e 487static const MemoryRegionOps platform_mmio_handler = {
01195b73
SS
488 .read = &platform_mmio_read,
489 .write = &platform_mmio_write,
de00982e 490 .endianness = DEVICE_NATIVE_ENDIAN,
01195b73
SS
491};
492
de00982e 493static void platform_mmio_setup(PCIXenPlatformState *d)
01195b73 494{
22fc860b 495 memory_region_init_io(&d->mmio_bar, OBJECT(d), &platform_mmio_handler, d,
de00982e 496 "xen-mmio", 0x1000000);
01195b73
SS
497}
498
499static int xen_platform_post_load(void *opaque, int version_id)
500{
501 PCIXenPlatformState *s = opaque;
502
e7b48c97 503 platform_fixed_ioport_writeb(s, 0, s->flags);
01195b73
SS
504
505 return 0;
506}
507
508static const VMStateDescription vmstate_xen_platform = {
509 .name = "platform",
510 .version_id = 4,
511 .minimum_version_id = 4,
01195b73 512 .post_load = xen_platform_post_load,
d49805ae 513 .fields = (VMStateField[]) {
dc4aa51b 514 VMSTATE_PCI_DEVICE(parent_obj, PCIXenPlatformState),
01195b73
SS
515 VMSTATE_UINT8(flags, PCIXenPlatformState),
516 VMSTATE_END_OF_LIST()
517 }
518};
519
4098d49d 520static void xen_platform_realize(PCIDevice *dev, Error **errp)
01195b73 521{
51a3fe99 522 PCIXenPlatformState *d = XEN_PLATFORM(dev);
01195b73
SS
523 uint8_t *pci_conf;
524
dbb7405d 525 /* Device will crash on reset if xen is not initialized */
bb346fae
JM
526 if (xen_mode == XEN_DISABLED) {
527 error_setg(errp, "xen-platform device requires a Xen guest");
b1ecd51b
EH
528 return;
529 }
dbb7405d 530
dc4aa51b 531 pci_conf = dev->config;
01195b73 532
01195b73
SS
533 pci_set_word(pci_conf + PCI_COMMAND, PCI_COMMAND_IO | PCI_COMMAND_MEMORY);
534
01195b73
SS
535 pci_config_set_prog_interface(pci_conf, 0);
536
01195b73
SS
537 pci_conf[PCI_INTERRUPT_PIN] = 1;
538
de00982e 539 platform_ioport_bar_setup(d);
dc4aa51b 540 pci_register_bar(dev, 0, PCI_BASE_ADDRESS_SPACE_IO, &d->bar);
01195b73
SS
541
542 /* reserve 16MB mmio address for share memory*/
de00982e 543 platform_mmio_setup(d);
dc4aa51b 544 pci_register_bar(dev, 1, PCI_BASE_ADDRESS_MEM_PREFETCH,
e824b2cc 545 &d->mmio_bar);
01195b73
SS
546
547 platform_fixed_ioport_init(d);
01195b73
SS
548}
549
550static void platform_reset(DeviceState *dev)
551{
51a3fe99 552 PCIXenPlatformState *s = XEN_PLATFORM(dev);
01195b73
SS
553
554 platform_fixed_ioport_reset(s);
555}
556
40021f08
AL
557static void xen_platform_class_init(ObjectClass *klass, void *data)
558{
39bffca2 559 DeviceClass *dc = DEVICE_CLASS(klass);
40021f08
AL
560 PCIDeviceClass *k = PCI_DEVICE_CLASS(klass);
561
4098d49d 562 k->realize = xen_platform_realize;
40021f08
AL
563 k->vendor_id = PCI_VENDOR_ID_XEN;
564 k->device_id = PCI_DEVICE_ID_XEN_PLATFORM;
565 k->class_id = PCI_CLASS_OTHERS << 8 | 0x80;
566 k->subsystem_vendor_id = PCI_VENDOR_ID_XEN;
567 k->subsystem_id = PCI_DEVICE_ID_XEN_PLATFORM;
568 k->revision = 1;
125ee0ed 569 set_bit(DEVICE_CATEGORY_MISC, dc->categories);
39bffca2
AL
570 dc->desc = "XEN platform pci device";
571 dc->reset = platform_reset;
572 dc->vmsd = &vmstate_xen_platform;
40021f08
AL
573}
574
8c43a6f0 575static const TypeInfo xen_platform_info = {
51a3fe99 576 .name = TYPE_XEN_PLATFORM,
39bffca2
AL
577 .parent = TYPE_PCI_DEVICE,
578 .instance_size = sizeof(PCIXenPlatformState),
579 .class_init = xen_platform_class_init,
fd3b02c8
EH
580 .interfaces = (InterfaceInfo[]) {
581 { INTERFACE_CONVENTIONAL_PCI_DEVICE },
582 { },
583 },
01195b73
SS
584};
585
83f7d43a 586static void xen_platform_register_types(void)
01195b73 587{
39bffca2 588 type_register_static(&xen_platform_info);
01195b73
SS
589}
590
83f7d43a 591type_init(xen_platform_register_types)