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