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