xref: /openbmc/qemu/hw/i386/xen/xen_platform.c (revision d447a624)
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 "net/net.h"
32 #include "trace.h"
33 #include "sysemu/xen.h"
34 #include "sysemu/block-backend.h"
35 #include "qemu/error-report.h"
36 #include "qemu/module.h"
37 #include "qom/object.h"
38 
39 #ifdef CONFIG_XEN
40 #include "hw/xen/xen_native.h"
41 #endif
42 
43 /* The rule is that xen_native.h must come first */
44 #include "hw/xen/xen.h"
45 
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 
58 struct PCIXenPlatformState {
59     /*< private >*/
60     PCIDevice parent_obj;
61     /*< public >*/
62 
63     MemoryRegion fixed_io;
64     MemoryRegion bar;
65     MemoryRegion mmio_bar;
66     uint8_t flags; /* used only for version_id == 2 */
67     uint16_t driver_product_version;
68 
69     /* Log from guest drivers */
70     char log_buffer[4096];
71     int log_buffer_off;
72 };
73 
74 #define TYPE_XEN_PLATFORM "xen-platform"
75 OBJECT_DECLARE_SIMPLE_TYPE(PCIXenPlatformState, XEN_PLATFORM)
76 
77 #define XEN_PLATFORM_IOPORT 0x10
78 
79 /* Send bytes to syslog */
80 static 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 
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)
116 
117 static 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 
130 static void unplug_nic(PCIBus *b, PCIDevice *d, void *o)
131 {
132     /* We have to ignore passthrough devices */
133     if (pci_get_word(d->config + PCI_CLASS_DEVICE) ==
134             PCI_CLASS_NETWORK_ETHERNET
135             && !pci_device_is_passthrough(d)) {
136         object_unparent(OBJECT(d));
137     }
138 }
139 
140 /* Remove the peer of the NIC device. Normally, this would be a tap device. */
141 static 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 
150 static void pci_unplug_nics(PCIBus *bus)
151 {
152     qemu_foreach_nic(del_nic_peer, NULL);
153     pci_for_each_device(bus, 0, unplug_nic, NULL);
154 }
155 
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  */
167 static void pci_xen_ide_unplug(PCIDevice *d, bool aux)
168 {
169     DeviceState *dev = DEVICE(d);
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     }
199     pci_device_reset(d);
200 }
201 
202 static void unplug_disks(PCIBus *b, PCIDevice *d, void *opaque)
203 {
204     uint32_t flags = *(uint32_t *)opaque;
205     bool aux = (flags & UNPLUG_AUX_IDE_DISKS) &&
206         !(flags & UNPLUG_IDE_SCSI_DISKS);
207 
208     /* We have to ignore passthrough devices */
209     if (pci_device_is_passthrough(d))
210         return;
211 
212     switch (pci_get_word(d->config + PCI_CLASS_DEVICE)) {
213     case PCI_CLASS_STORAGE_IDE:
214         pci_xen_ide_unplug(d, aux);
215         break;
216 
217     case PCI_CLASS_STORAGE_SCSI:
218         if (!aux) {
219             object_unparent(OBJECT(d));
220         }
221         break;
222 
223     case PCI_CLASS_STORAGE_EXPRESS:
224         if (flags & UNPLUG_NVME_DISKS) {
225             object_unparent(OBJECT(d));
226         }
227 
228     default:
229         break;
230     }
231 }
232 
233 static void pci_unplug_disks(PCIBus *bus, uint32_t flags)
234 {
235     pci_for_each_device(bus, 0, unplug_disks, &flags);
236 }
237 
238 static void platform_fixed_ioport_writew(void *opaque, uint32_t addr, uint32_t val)
239 {
240     PCIXenPlatformState *s = opaque;
241 
242     switch (addr) {
243     case 0: {
244         PCIDevice *pci_dev = PCI_DEVICE(s);
245         /* Unplug devices. See comment above flag definitions */
246         if (val & (UNPLUG_IDE_SCSI_DISKS | UNPLUG_AUX_IDE_DISKS |
247                    UNPLUG_NVME_DISKS)) {
248             DPRINTF("unplug disks\n");
249             pci_unplug_disks(pci_get_bus(pci_dev), val);
250         }
251         if (val & UNPLUG_ALL_NICS) {
252             DPRINTF("unplug nics\n");
253             pci_unplug_nics(pci_get_bus(pci_dev));
254         }
255         break;
256     }
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 
274 static void platform_fixed_ioport_writel(void *opaque, uint32_t addr,
275                                          uint32_t val)
276 {
277     switch (addr) {
278     case 0:
279         /* PV driver version */
280         break;
281     }
282 }
283 
284 static void platform_fixed_ioport_writeb(void *opaque, uint32_t addr, uint32_t val)
285 {
286     PCIXenPlatformState *s = opaque;
287 
288     switch (addr) {
289     case 0: /* Platform flags */
290         if (xen_mode == XEN_EMULATE) {
291             /* XX: Use i440gx/q35 PAM setup to do this? */
292             s->flags = val & PFFLAG_ROM_LOCK;
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
306         }
307         break;
308 
309     case 2:
310         log_writeb(s, val);
311         break;
312     }
313 }
314 
315 static uint32_t platform_fixed_ioport_readw(void *opaque, uint32_t addr)
316 {
317     switch (addr) {
318     case 0:
319         /* Magic value so that you can identify the interface. */
320         return 0x49d2;
321     default:
322         return 0xffff;
323     }
324 }
325 
326 static uint32_t platform_fixed_ioport_readb(void *opaque, uint32_t addr)
327 {
328     PCIXenPlatformState *s = opaque;
329 
330     switch (addr) {
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 
342 static void platform_fixed_ioport_reset(void *opaque)
343 {
344     PCIXenPlatformState *s = opaque;
345 
346     platform_fixed_ioport_writeb(s, 0, 0);
347 }
348 
349 static 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 
363 static 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 
380 
381 static const MemoryRegionOps platform_fixed_io_ops = {
382     .read = platform_fixed_ioport_read,
383     .write = platform_fixed_ioport_write,
384     .valid = {
385         .unaligned = true,
386     },
387     .impl = {
388         .min_access_size = 1,
389         .max_access_size = 4,
390         .unaligned = true,
391     },
392     .endianness = DEVICE_LITTLE_ENDIAN,
393 };
394 
395 static void platform_fixed_ioport_init(PCIXenPlatformState* s)
396 {
397     memory_region_init_io(&s->fixed_io, OBJECT(s), &platform_fixed_io_ops, s,
398                           "xen-fixed", 16);
399     memory_region_add_subregion(get_system_io(), XEN_PLATFORM_IOPORT,
400                                 &s->fixed_io);
401 }
402 
403 /* Xen Platform PCI Device */
404 
405 static uint64_t xen_platform_ioport_readb(void *opaque, hwaddr addr,
406                                           unsigned int size)
407 {
408     if (addr == 0) {
409         return platform_fixed_ioport_readb(opaque, 0);
410     } else {
411         return ~0u;
412     }
413 }
414 
415 static void xen_platform_ioport_writeb(void *opaque, hwaddr addr,
416                                        uint64_t val, unsigned int size)
417 {
418     PCIXenPlatformState *s = opaque;
419     PCIDevice *pci_dev = PCI_DEVICE(s);
420 
421     switch (addr) {
422     case 0: /* Platform flags */
423         platform_fixed_ioport_writeb(opaque, 0, (uint32_t)val);
424         break;
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              */
435             pci_unplug_disks(pci_get_bus(pci_dev), UNPLUG_IDE_SCSI_DISKS);
436             pci_unplug_nics(pci_get_bus(pci_dev));
437         }
438         break;
439     case 8:
440         switch (val) {
441         case 1:
442             pci_unplug_disks(pci_get_bus(pci_dev), UNPLUG_IDE_SCSI_DISKS);
443             break;
444         case 2:
445             pci_unplug_nics(pci_get_bus(pci_dev));
446             break;
447         default:
448             log_writeb(s, (uint32_t)val);
449             break;
450         }
451         break;
452     default:
453         break;
454     }
455 }
456 
457 static const MemoryRegionOps xen_pci_io_ops = {
458     .read  = xen_platform_ioport_readb,
459     .write = xen_platform_ioport_writeb,
460     .impl.min_access_size = 1,
461     .impl.max_access_size = 1,
462 };
463 
464 static void platform_ioport_bar_setup(PCIXenPlatformState *d)
465 {
466     memory_region_init_io(&d->bar, OBJECT(d), &xen_pci_io_ops, d,
467                           "xen-pci", 0x100);
468 }
469 
470 static uint64_t platform_mmio_read(void *opaque, hwaddr addr,
471                                    unsigned size)
472 {
473     DPRINTF("Warning: attempted read from physical address "
474             "0x" HWADDR_FMT_plx " in xen platform mmio space\n", addr);
475 
476     return 0;
477 }
478 
479 static void platform_mmio_write(void *opaque, hwaddr addr,
480                                 uint64_t val, unsigned size)
481 {
482     DPRINTF("Warning: attempted write of 0x%"PRIx64" to physical "
483             "address 0x" HWADDR_FMT_plx " in xen platform mmio space\n",
484             val, addr);
485 }
486 
487 static const MemoryRegionOps platform_mmio_handler = {
488     .read = &platform_mmio_read,
489     .write = &platform_mmio_write,
490     .endianness = DEVICE_NATIVE_ENDIAN,
491 };
492 
493 static void platform_mmio_setup(PCIXenPlatformState *d)
494 {
495     memory_region_init_io(&d->mmio_bar, OBJECT(d), &platform_mmio_handler, d,
496                           "xen-mmio", 0x1000000);
497 }
498 
499 static int xen_platform_post_load(void *opaque, int version_id)
500 {
501     PCIXenPlatformState *s = opaque;
502 
503     platform_fixed_ioport_writeb(s, 0, s->flags);
504 
505     return 0;
506 }
507 
508 static const VMStateDescription vmstate_xen_platform = {
509     .name = "platform",
510     .version_id = 4,
511     .minimum_version_id = 4,
512     .post_load = xen_platform_post_load,
513     .fields = (VMStateField[]) {
514         VMSTATE_PCI_DEVICE(parent_obj, PCIXenPlatformState),
515         VMSTATE_UINT8(flags, PCIXenPlatformState),
516         VMSTATE_END_OF_LIST()
517     }
518 };
519 
520 static void xen_platform_realize(PCIDevice *dev, Error **errp)
521 {
522     PCIXenPlatformState *d = XEN_PLATFORM(dev);
523     uint8_t *pci_conf;
524 
525     /* Device will crash on reset if xen is not initialized */
526     if (xen_mode == XEN_DISABLED) {
527         error_setg(errp, "xen-platform device requires a Xen guest");
528         return;
529     }
530 
531     pci_conf = dev->config;
532 
533     pci_set_word(pci_conf + PCI_COMMAND, PCI_COMMAND_IO | PCI_COMMAND_MEMORY);
534 
535     pci_config_set_prog_interface(pci_conf, 0);
536 
537     pci_conf[PCI_INTERRUPT_PIN] = 1;
538 
539     platform_ioport_bar_setup(d);
540     pci_register_bar(dev, 0, PCI_BASE_ADDRESS_SPACE_IO, &d->bar);
541 
542     /* reserve 16MB mmio address for share memory*/
543     platform_mmio_setup(d);
544     pci_register_bar(dev, 1, PCI_BASE_ADDRESS_MEM_PREFETCH,
545                      &d->mmio_bar);
546 
547     platform_fixed_ioport_init(d);
548 }
549 
550 static void platform_reset(DeviceState *dev)
551 {
552     PCIXenPlatformState *s = XEN_PLATFORM(dev);
553 
554     platform_fixed_ioport_reset(s);
555 }
556 
557 static void xen_platform_class_init(ObjectClass *klass, void *data)
558 {
559     DeviceClass *dc = DEVICE_CLASS(klass);
560     PCIDeviceClass *k = PCI_DEVICE_CLASS(klass);
561 
562     k->realize = xen_platform_realize;
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;
569     set_bit(DEVICE_CATEGORY_MISC, dc->categories);
570     dc->desc = "XEN platform pci device";
571     dc->reset = platform_reset;
572     dc->vmsd = &vmstate_xen_platform;
573 }
574 
575 static const TypeInfo xen_platform_info = {
576     .name          = TYPE_XEN_PLATFORM,
577     .parent        = TYPE_PCI_DEVICE,
578     .instance_size = sizeof(PCIXenPlatformState),
579     .class_init    = xen_platform_class_init,
580     .interfaces = (InterfaceInfo[]) {
581         { INTERFACE_CONVENTIONAL_PCI_DEVICE },
582         { },
583     },
584 };
585 
586 static void xen_platform_register_types(void)
587 {
588     type_register_static(&xen_platform_info);
589 }
590 
591 type_init(xen_platform_register_types)
592