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