xref: /openbmc/qemu/hw/i386/xen/xen_platform.c (revision 19f4ed36)
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/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 "exec/address-spaces.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 static void unplug_disks(PCIBus *b, PCIDevice *d, void *opaque)
139 {
140     uint32_t flags = *(uint32_t *)opaque;
141     bool aux = (flags & UNPLUG_AUX_IDE_DISKS) &&
142         !(flags & UNPLUG_IDE_SCSI_DISKS);
143 
144     /* We have to ignore passthrough devices */
145     if (!strcmp(d->name, "xen-pci-passthrough")) {
146         return;
147     }
148 
149     switch (pci_get_word(d->config + PCI_CLASS_DEVICE)) {
150     case PCI_CLASS_STORAGE_IDE:
151         pci_piix3_xen_ide_unplug(DEVICE(d), aux);
152         break;
153 
154     case PCI_CLASS_STORAGE_SCSI:
155         if (!aux) {
156             object_unparent(OBJECT(d));
157         }
158         break;
159 
160     case PCI_CLASS_STORAGE_EXPRESS:
161         if (flags & UNPLUG_NVME_DISKS) {
162             object_unparent(OBJECT(d));
163         }
164 
165     default:
166         break;
167     }
168 }
169 
170 static void pci_unplug_disks(PCIBus *bus, uint32_t flags)
171 {
172     pci_for_each_device(bus, 0, unplug_disks, &flags);
173 }
174 
175 static void platform_fixed_ioport_writew(void *opaque, uint32_t addr, uint32_t val)
176 {
177     PCIXenPlatformState *s = opaque;
178 
179     switch (addr) {
180     case 0: {
181         PCIDevice *pci_dev = PCI_DEVICE(s);
182         /* Unplug devices. See comment above flag definitions */
183         if (val & (UNPLUG_IDE_SCSI_DISKS | UNPLUG_AUX_IDE_DISKS |
184                    UNPLUG_NVME_DISKS)) {
185             DPRINTF("unplug disks\n");
186             pci_unplug_disks(pci_get_bus(pci_dev), val);
187         }
188         if (val & UNPLUG_ALL_NICS) {
189             DPRINTF("unplug nics\n");
190             pci_unplug_nics(pci_get_bus(pci_dev));
191         }
192         break;
193     }
194     case 2:
195         switch (val) {
196         case 1:
197             DPRINTF("Citrix Windows PV drivers loaded in guest\n");
198             break;
199         case 0:
200             DPRINTF("Guest claimed to be running PV product 0?\n");
201             break;
202         default:
203             DPRINTF("Unknown PV product %d loaded in guest\n", val);
204             break;
205         }
206         s->driver_product_version = val;
207         break;
208     }
209 }
210 
211 static void platform_fixed_ioport_writel(void *opaque, uint32_t addr,
212                                          uint32_t val)
213 {
214     switch (addr) {
215     case 0:
216         /* PV driver version */
217         break;
218     }
219 }
220 
221 static void platform_fixed_ioport_writeb(void *opaque, uint32_t addr, uint32_t val)
222 {
223     PCIXenPlatformState *s = opaque;
224 
225     switch (addr) {
226     case 0: /* Platform flags */ {
227         hvmmem_type_t mem_type = (val & PFFLAG_ROM_LOCK) ?
228             HVMMEM_ram_ro : HVMMEM_ram_rw;
229         if (xen_set_mem_type(xen_domid, mem_type, 0xc0, 0x40)) {
230             DPRINTF("unable to change ro/rw state of ROM memory area!\n");
231         } else {
232             s->flags = val & PFFLAG_ROM_LOCK;
233             DPRINTF("changed ro/rw state of ROM memory area. now is %s state.\n",
234                     (mem_type == HVMMEM_ram_ro ? "ro":"rw"));
235         }
236         break;
237     }
238     case 2:
239         log_writeb(s, val);
240         break;
241     }
242 }
243 
244 static uint32_t platform_fixed_ioport_readw(void *opaque, uint32_t addr)
245 {
246     switch (addr) {
247     case 0:
248         /* Magic value so that you can identify the interface. */
249         return 0x49d2;
250     default:
251         return 0xffff;
252     }
253 }
254 
255 static uint32_t platform_fixed_ioport_readb(void *opaque, uint32_t addr)
256 {
257     PCIXenPlatformState *s = opaque;
258 
259     switch (addr) {
260     case 0:
261         /* Platform flags */
262         return s->flags;
263     case 2:
264         /* Version number */
265         return 1;
266     default:
267         return 0xff;
268     }
269 }
270 
271 static void platform_fixed_ioport_reset(void *opaque)
272 {
273     PCIXenPlatformState *s = opaque;
274 
275     platform_fixed_ioport_writeb(s, 0, 0);
276 }
277 
278 static uint64_t platform_fixed_ioport_read(void *opaque,
279                                            hwaddr addr,
280                                            unsigned size)
281 {
282     switch (size) {
283     case 1:
284         return platform_fixed_ioport_readb(opaque, addr);
285     case 2:
286         return platform_fixed_ioport_readw(opaque, addr);
287     default:
288         return -1;
289     }
290 }
291 
292 static void platform_fixed_ioport_write(void *opaque, hwaddr addr,
293 
294                                         uint64_t val, unsigned size)
295 {
296     switch (size) {
297     case 1:
298         platform_fixed_ioport_writeb(opaque, addr, val);
299         break;
300     case 2:
301         platform_fixed_ioport_writew(opaque, addr, val);
302         break;
303     case 4:
304         platform_fixed_ioport_writel(opaque, addr, val);
305         break;
306     }
307 }
308 
309 
310 static const MemoryRegionOps platform_fixed_io_ops = {
311     .read = platform_fixed_ioport_read,
312     .write = platform_fixed_ioport_write,
313     .valid = {
314         .unaligned = true,
315     },
316     .impl = {
317         .min_access_size = 1,
318         .max_access_size = 4,
319         .unaligned = true,
320     },
321     .endianness = DEVICE_LITTLE_ENDIAN,
322 };
323 
324 static void platform_fixed_ioport_init(PCIXenPlatformState* s)
325 {
326     memory_region_init_io(&s->fixed_io, OBJECT(s), &platform_fixed_io_ops, s,
327                           "xen-fixed", 16);
328     memory_region_add_subregion(get_system_io(), XEN_PLATFORM_IOPORT,
329                                 &s->fixed_io);
330 }
331 
332 /* Xen Platform PCI Device */
333 
334 static uint64_t xen_platform_ioport_readb(void *opaque, hwaddr addr,
335                                           unsigned int size)
336 {
337     if (addr == 0) {
338         return platform_fixed_ioport_readb(opaque, 0);
339     } else {
340         return ~0u;
341     }
342 }
343 
344 static void xen_platform_ioport_writeb(void *opaque, hwaddr addr,
345                                        uint64_t val, unsigned int size)
346 {
347     PCIXenPlatformState *s = opaque;
348     PCIDevice *pci_dev = PCI_DEVICE(s);
349 
350     switch (addr) {
351     case 0: /* Platform flags */
352         platform_fixed_ioport_writeb(opaque, 0, (uint32_t)val);
353         break;
354     case 4:
355         if (val == 1) {
356             /*
357              * SUSE unplug for Xenlinux
358              * xen-kmp used this since xen-3.0.4, instead the official protocol
359              * from xen-3.3+ It did an unconditional "outl(1, (ioaddr + 4));"
360              * Pre VMDP 1.7 used 4 and 8 depending on how VMDP was configured.
361              * If VMDP was to control both disk and LAN it would use 4.
362              * If it controlled just disk or just LAN, it would use 8 below.
363              */
364             pci_unplug_disks(pci_get_bus(pci_dev), UNPLUG_IDE_SCSI_DISKS);
365             pci_unplug_nics(pci_get_bus(pci_dev));
366         }
367         break;
368     case 8:
369         switch (val) {
370         case 1:
371             pci_unplug_disks(pci_get_bus(pci_dev), UNPLUG_IDE_SCSI_DISKS);
372             break;
373         case 2:
374             pci_unplug_nics(pci_get_bus(pci_dev));
375             break;
376         default:
377             log_writeb(s, (uint32_t)val);
378             break;
379         }
380         break;
381     default:
382         break;
383     }
384 }
385 
386 static const MemoryRegionOps xen_pci_io_ops = {
387     .read  = xen_platform_ioport_readb,
388     .write = xen_platform_ioport_writeb,
389     .impl.min_access_size = 1,
390     .impl.max_access_size = 1,
391 };
392 
393 static void platform_ioport_bar_setup(PCIXenPlatformState *d)
394 {
395     memory_region_init_io(&d->bar, OBJECT(d), &xen_pci_io_ops, d,
396                           "xen-pci", 0x100);
397 }
398 
399 static uint64_t platform_mmio_read(void *opaque, hwaddr addr,
400                                    unsigned size)
401 {
402     DPRINTF("Warning: attempted read from physical address "
403             "0x" TARGET_FMT_plx " in xen platform mmio space\n", addr);
404 
405     return 0;
406 }
407 
408 static void platform_mmio_write(void *opaque, hwaddr addr,
409                                 uint64_t val, unsigned size)
410 {
411     DPRINTF("Warning: attempted write of 0x%"PRIx64" to physical "
412             "address 0x" TARGET_FMT_plx " in xen platform mmio space\n",
413             val, addr);
414 }
415 
416 static const MemoryRegionOps platform_mmio_handler = {
417     .read = &platform_mmio_read,
418     .write = &platform_mmio_write,
419     .endianness = DEVICE_NATIVE_ENDIAN,
420 };
421 
422 static void platform_mmio_setup(PCIXenPlatformState *d)
423 {
424     memory_region_init_io(&d->mmio_bar, OBJECT(d), &platform_mmio_handler, d,
425                           "xen-mmio", 0x1000000);
426 }
427 
428 static int xen_platform_post_load(void *opaque, int version_id)
429 {
430     PCIXenPlatformState *s = opaque;
431 
432     platform_fixed_ioport_writeb(s, 0, s->flags);
433 
434     return 0;
435 }
436 
437 static const VMStateDescription vmstate_xen_platform = {
438     .name = "platform",
439     .version_id = 4,
440     .minimum_version_id = 4,
441     .post_load = xen_platform_post_load,
442     .fields = (VMStateField[]) {
443         VMSTATE_PCI_DEVICE(parent_obj, PCIXenPlatformState),
444         VMSTATE_UINT8(flags, PCIXenPlatformState),
445         VMSTATE_END_OF_LIST()
446     }
447 };
448 
449 static void xen_platform_realize(PCIDevice *dev, Error **errp)
450 {
451     PCIXenPlatformState *d = XEN_PLATFORM(dev);
452     uint8_t *pci_conf;
453 
454     /* Device will crash on reset if xen is not initialized */
455     if (!xen_enabled()) {
456         error_setg(errp, "xen-platform device requires the Xen accelerator");
457         return;
458     }
459 
460     pci_conf = dev->config;
461 
462     pci_set_word(pci_conf + PCI_COMMAND, PCI_COMMAND_IO | PCI_COMMAND_MEMORY);
463 
464     pci_config_set_prog_interface(pci_conf, 0);
465 
466     pci_conf[PCI_INTERRUPT_PIN] = 1;
467 
468     platform_ioport_bar_setup(d);
469     pci_register_bar(dev, 0, PCI_BASE_ADDRESS_SPACE_IO, &d->bar);
470 
471     /* reserve 16MB mmio address for share memory*/
472     platform_mmio_setup(d);
473     pci_register_bar(dev, 1, PCI_BASE_ADDRESS_MEM_PREFETCH,
474                      &d->mmio_bar);
475 
476     platform_fixed_ioport_init(d);
477 }
478 
479 static void platform_reset(DeviceState *dev)
480 {
481     PCIXenPlatformState *s = XEN_PLATFORM(dev);
482 
483     platform_fixed_ioport_reset(s);
484 }
485 
486 static void xen_platform_class_init(ObjectClass *klass, void *data)
487 {
488     DeviceClass *dc = DEVICE_CLASS(klass);
489     PCIDeviceClass *k = PCI_DEVICE_CLASS(klass);
490 
491     k->realize = xen_platform_realize;
492     k->vendor_id = PCI_VENDOR_ID_XEN;
493     k->device_id = PCI_DEVICE_ID_XEN_PLATFORM;
494     k->class_id = PCI_CLASS_OTHERS << 8 | 0x80;
495     k->subsystem_vendor_id = PCI_VENDOR_ID_XEN;
496     k->subsystem_id = PCI_DEVICE_ID_XEN_PLATFORM;
497     k->revision = 1;
498     set_bit(DEVICE_CATEGORY_MISC, dc->categories);
499     dc->desc = "XEN platform pci device";
500     dc->reset = platform_reset;
501     dc->vmsd = &vmstate_xen_platform;
502 }
503 
504 static const TypeInfo xen_platform_info = {
505     .name          = TYPE_XEN_PLATFORM,
506     .parent        = TYPE_PCI_DEVICE,
507     .instance_size = sizeof(PCIXenPlatformState),
508     .class_init    = xen_platform_class_init,
509     .interfaces = (InterfaceInfo[]) {
510         { INTERFACE_CONVENTIONAL_PCI_DEVICE },
511         { },
512     },
513 };
514 
515 static void xen_platform_register_types(void)
516 {
517     type_register_static(&xen_platform_info);
518 }
519 
520 type_init(xen_platform_register_types)
521