blob: deb7a0c374d4c1b2d52d86e1e3878b41afa32057 [file] [log] [blame]
Steven Smith01195b72011-06-16 17:05:17 +01001/*
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
Peter Maydellb6a0aa02016-01-26 18:17:03 +000026#include "qemu/osdep.h"
Markus Armbrusterda34e652016-03-14 09:01:28 +010027#include "qapi/error.h"
Paolo Bonzini83c9f4c2013-02-04 15:40:22 +010028#include "hw/hw.h"
Stefano Stabellini15e81592014-02-20 17:28:08 +000029#include "hw/ide.h"
Paolo Bonzini83c9f4c2013-02-04 15:40:22 +010030#include "hw/pci/pci.h"
31#include "hw/irq.h"
Paolo Bonzini0d09e412013-02-05 17:06:20 +010032#include "hw/xen/xen_common.h"
33#include "hw/xen/xen_backend.h"
Steven Smith01195b72011-06-16 17:05:17 +010034#include "trace.h"
Paolo Bonzini022c62c2012-12-17 18:19:49 +010035#include "exec/address-spaces.h"
Markus Armbruster4be74632014-10-07 13:59:18 +020036#include "sysemu/block-backend.h"
Eduardo Habkostb1ecd512015-10-21 13:46:50 -020037#include "qemu/error-report.h"
Steven Smith01195b72011-06-16 17:05:17 +010038
39#include <xenguest.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
53typedef struct PCIXenPlatformState {
Andreas Färberdc4aa512013-06-30 15:20:05 +020054 /*< private >*/
55 PCIDevice parent_obj;
56 /*< public >*/
57
Avi Kivityde009822011-08-08 16:09:25 +030058 MemoryRegion fixed_io;
59 MemoryRegion bar;
60 MemoryRegion mmio_bar;
Steven Smith01195b72011-06-16 17:05:17 +010061 uint8_t flags; /* used only for version_id == 2 */
62 int drivers_blacklisted;
63 uint16_t driver_product_version;
64
65 /* Log from guest drivers */
66 char log_buffer[4096];
67 int log_buffer_off;
68} PCIXenPlatformState;
69
Peter Crosthwaite51a3fe92013-06-24 17:00:14 +100070#define TYPE_XEN_PLATFORM "xen-platform"
71#define XEN_PLATFORM(obj) \
72 OBJECT_CHECK(PCIXenPlatformState, (obj), TYPE_XEN_PLATFORM)
73
Steven Smith01195b72011-06-16 17:05:17 +010074#define XEN_PLATFORM_IOPORT 0x10
75
76/* Send bytes to syslog */
77static void log_writeb(PCIXenPlatformState *s, char val)
78{
79 if (val == '\n' || s->log_buffer_off == sizeof(s->log_buffer) - 1) {
80 /* Flush buffer */
81 s->log_buffer[s->log_buffer_off] = 0;
82 trace_xen_platform_log(s->log_buffer);
83 s->log_buffer_off = 0;
84 } else {
85 s->log_buffer[s->log_buffer_off++] = val;
86 }
87}
88
Stefano Stabellini04d6da42017-07-18 13:28:12 -070089/*
90 * Unplug device flags.
91 *
92 * The logic got a little confused at some point in the past but this is
93 * what they do now.
94 *
95 * bit 0: Unplug all IDE and SCSI disks.
96 * bit 1: Unplug all NICs.
97 * bit 2: Unplug IDE disks except primary master. This is overridden if
98 * bit 0 is also present in the mask.
99 * bit 3: Unplug all NVMe disks.
100 *
101 */
102#define _UNPLUG_IDE_SCSI_DISKS 0
103#define UNPLUG_IDE_SCSI_DISKS (1u << _UNPLUG_IDE_SCSI_DISKS)
104
105#define _UNPLUG_ALL_NICS 1
106#define UNPLUG_ALL_NICS (1u << _UNPLUG_ALL_NICS)
107
108#define _UNPLUG_AUX_IDE_DISKS 2
109#define UNPLUG_AUX_IDE_DISKS (1u << _UNPLUG_AUX_IDE_DISKS)
110
111#define _UNPLUG_NVME_DISKS 3
112#define UNPLUG_NVME_DISKS (1u << _UNPLUG_NVME_DISKS)
Stefano Stabellini679f4f82011-07-18 06:07:02 +0000113
Anthony PERARD7aa8cbb2012-06-21 15:35:28 +0000114static void unplug_nic(PCIBus *b, PCIDevice *d, void *o)
Stefano Stabellini679f4f82011-07-18 06:07:02 +0000115{
Anthony PERARDbd4982a2012-10-03 13:45:24 +0000116 /* We have to ignore passthrough devices */
Stefano Stabellini679f4f82011-07-18 06:07:02 +0000117 if (pci_get_word(d->config + PCI_CLASS_DEVICE) ==
Anthony PERARDbd4982a2012-10-03 13:45:24 +0000118 PCI_CLASS_NETWORK_ETHERNET
119 && strcmp(d->name, "xen-pci-passthrough") != 0) {
Stefan Hajnoczi02a5c4c2013-09-11 14:54:09 +0200120 object_unparent(OBJECT(d));
Stefano Stabellini679f4f82011-07-18 06:07:02 +0000121 }
122}
123
Ross Lagerwall6c808652017-06-30 13:50:28 +0100124/* Remove the peer of the NIC device. Normally, this would be a tap device. */
125static void del_nic_peer(NICState *nic, void *opaque)
126{
127 NetClientState *nc;
128
129 nc = qemu_get_queue(nic);
130 if (nc->peer)
131 qemu_del_net_client(nc->peer);
132}
133
Stefano Stabellini679f4f82011-07-18 06:07:02 +0000134static void pci_unplug_nics(PCIBus *bus)
135{
Ross Lagerwall6c808652017-06-30 13:50:28 +0100136 qemu_foreach_nic(del_nic_peer, NULL);
Anthony PERARD7aa8cbb2012-06-21 15:35:28 +0000137 pci_for_each_device(bus, 0, unplug_nic, NULL);
Stefano Stabellini679f4f82011-07-18 06:07:02 +0000138}
139
Paul Durrantae4d2eb2017-01-24 14:05:47 +0000140static void unplug_disks(PCIBus *b, PCIDevice *d, void *opaque)
Stefano Stabellini679f4f82011-07-18 06:07:02 +0000141{
Paul Durrantae4d2eb2017-01-24 14:05:47 +0000142 uint32_t flags = *(uint32_t *)opaque;
143 bool aux = (flags & UNPLUG_AUX_IDE_DISKS) &&
Stefano Stabellini04d6da42017-07-18 13:28:12 -0700144 !(flags & UNPLUG_IDE_SCSI_DISKS);
Paul Durrantae4d2eb2017-01-24 14:05:47 +0000145
Anthony PERARDbd4982a2012-10-03 13:45:24 +0000146 /* We have to ignore passthrough devices */
Paul Durrant3d89e3f2017-01-24 14:05:45 +0000147 if (!strcmp(d->name, "xen-pci-passthrough")) {
148 return;
149 }
150
151 switch (pci_get_word(d->config + PCI_CLASS_DEVICE)) {
152 case PCI_CLASS_STORAGE_IDE:
Paul Durrantae4d2eb2017-01-24 14:05:47 +0000153 pci_piix3_xen_ide_unplug(DEVICE(d), aux);
Paul Durrant3d89e3f2017-01-24 14:05:45 +0000154 break;
155
156 case PCI_CLASS_STORAGE_SCSI:
Paul Durrantae4d2eb2017-01-24 14:05:47 +0000157 if (!aux) {
158 object_unparent(OBJECT(d));
159 }
Paul Durrant3d89e3f2017-01-24 14:05:45 +0000160 break;
161
Stefano Stabellini04d6da42017-07-18 13:28:12 -0700162 case PCI_CLASS_STORAGE_EXPRESS:
163 if (flags & UNPLUG_NVME_DISKS) {
164 object_unparent(OBJECT(d));
165 }
166
Paul Durrant3d89e3f2017-01-24 14:05:45 +0000167 default:
168 break;
Stefano Stabellini679f4f82011-07-18 06:07:02 +0000169 }
170}
171
Paul Durrantae4d2eb2017-01-24 14:05:47 +0000172static void pci_unplug_disks(PCIBus *bus, uint32_t flags)
Stefano Stabellini679f4f82011-07-18 06:07:02 +0000173{
Paul Durrantae4d2eb2017-01-24 14:05:47 +0000174 pci_for_each_device(bus, 0, unplug_disks, &flags);
Stefano Stabellini679f4f82011-07-18 06:07:02 +0000175}
Steven Smith01195b72011-06-16 17:05:17 +0100176
177static void platform_fixed_ioport_writew(void *opaque, uint32_t addr, uint32_t val)
178{
179 PCIXenPlatformState *s = opaque;
180
Anthony PERARDe7b48c92011-11-04 15:35:11 +0000181 switch (addr) {
Andreas Färberdc4aa512013-06-30 15:20:05 +0200182 case 0: {
183 PCIDevice *pci_dev = PCI_DEVICE(s);
Stefano Stabellini04d6da42017-07-18 13:28:12 -0700184 /* Unplug devices. See comment above flag definitions */
185 if (val & (UNPLUG_IDE_SCSI_DISKS | UNPLUG_AUX_IDE_DISKS |
186 UNPLUG_NVME_DISKS)) {
Stefano Stabellini679f4f82011-07-18 06:07:02 +0000187 DPRINTF("unplug disks\n");
David Gibsonfd56e062017-11-29 19:46:27 +1100188 pci_unplug_disks(pci_get_bus(pci_dev), val);
Stefano Stabellini679f4f82011-07-18 06:07:02 +0000189 }
190 if (val & UNPLUG_ALL_NICS) {
191 DPRINTF("unplug nics\n");
David Gibsonfd56e062017-11-29 19:46:27 +1100192 pci_unplug_nics(pci_get_bus(pci_dev));
Stefano Stabellini679f4f82011-07-18 06:07:02 +0000193 }
Steven Smith01195b72011-06-16 17:05:17 +0100194 break;
Andreas Färberdc4aa512013-06-30 15:20:05 +0200195 }
Steven Smith01195b72011-06-16 17:05:17 +0100196 case 2:
197 switch (val) {
198 case 1:
199 DPRINTF("Citrix Windows PV drivers loaded in guest\n");
200 break;
201 case 0:
202 DPRINTF("Guest claimed to be running PV product 0?\n");
203 break;
204 default:
205 DPRINTF("Unknown PV product %d loaded in guest\n", val);
206 break;
207 }
208 s->driver_product_version = val;
209 break;
210 }
211}
212
213static void platform_fixed_ioport_writel(void *opaque, uint32_t addr,
214 uint32_t val)
215{
Anthony PERARDe7b48c92011-11-04 15:35:11 +0000216 switch (addr) {
Steven Smith01195b72011-06-16 17:05:17 +0100217 case 0:
218 /* PV driver version */
219 break;
220 }
221}
222
223static void platform_fixed_ioport_writeb(void *opaque, uint32_t addr, uint32_t val)
224{
225 PCIXenPlatformState *s = opaque;
226
Anthony PERARDe7b48c92011-11-04 15:35:11 +0000227 switch (addr) {
Steven Smith01195b72011-06-16 17:05:17 +0100228 case 0: /* Platform flags */ {
229 hvmmem_type_t mem_type = (val & PFFLAG_ROM_LOCK) ?
230 HVMMEM_ram_ro : HVMMEM_ram_rw;
Paul Durrant8f25e752017-03-07 10:55:32 +0000231 if (xen_set_mem_type(xen_domid, mem_type, 0xc0, 0x40)) {
Steven Smith01195b72011-06-16 17:05:17 +0100232 DPRINTF("unable to change ro/rw state of ROM memory area!\n");
233 } else {
234 s->flags = val & PFFLAG_ROM_LOCK;
235 DPRINTF("changed ro/rw state of ROM memory area. now is %s state.\n",
236 (mem_type == HVMMEM_ram_ro ? "ro":"rw"));
237 }
238 break;
239 }
240 case 2:
241 log_writeb(s, val);
242 break;
243 }
244}
245
246static uint32_t platform_fixed_ioport_readw(void *opaque, uint32_t addr)
247{
248 PCIXenPlatformState *s = opaque;
249
Anthony PERARDe7b48c92011-11-04 15:35:11 +0000250 switch (addr) {
Steven Smith01195b72011-06-16 17:05:17 +0100251 case 0:
252 if (s->drivers_blacklisted) {
253 /* The drivers will recognise this magic number and refuse
254 * to do anything. */
255 return 0xd249;
256 } else {
257 /* Magic value so that you can identify the interface. */
258 return 0x49d2;
259 }
260 default:
261 return 0xffff;
262 }
263}
264
265static uint32_t platform_fixed_ioport_readb(void *opaque, uint32_t addr)
266{
267 PCIXenPlatformState *s = opaque;
268
Anthony PERARDe7b48c92011-11-04 15:35:11 +0000269 switch (addr) {
Steven Smith01195b72011-06-16 17:05:17 +0100270 case 0:
271 /* Platform flags */
272 return s->flags;
273 case 2:
274 /* Version number */
275 return 1;
276 default:
277 return 0xff;
278 }
279}
280
281static void platform_fixed_ioport_reset(void *opaque)
282{
283 PCIXenPlatformState *s = opaque;
284
Anthony PERARDe7b48c92011-11-04 15:35:11 +0000285 platform_fixed_ioport_writeb(s, 0, 0);
Steven Smith01195b72011-06-16 17:05:17 +0100286}
287
Alexander Graf626c7a12012-10-08 13:47:30 +0200288static uint64_t platform_fixed_ioport_read(void *opaque,
289 hwaddr addr,
290 unsigned size)
291{
292 switch (size) {
293 case 1:
294 return platform_fixed_ioport_readb(opaque, addr);
295 case 2:
296 return platform_fixed_ioport_readw(opaque, addr);
297 default:
298 return -1;
299 }
300}
301
302static void platform_fixed_ioport_write(void *opaque, hwaddr addr,
303
304 uint64_t val, unsigned size)
305{
306 switch (size) {
307 case 1:
308 platform_fixed_ioport_writeb(opaque, addr, val);
309 break;
310 case 2:
311 platform_fixed_ioport_writew(opaque, addr, val);
312 break;
313 case 4:
314 platform_fixed_ioport_writel(opaque, addr, val);
315 break;
316 }
317}
318
Avi Kivityde009822011-08-08 16:09:25 +0300319
320static const MemoryRegionOps platform_fixed_io_ops = {
Alexander Graf626c7a12012-10-08 13:47:30 +0200321 .read = platform_fixed_ioport_read,
322 .write = platform_fixed_ioport_write,
Jan Kiszka962b03f2013-06-22 08:07:03 +0200323 .valid = {
324 .unaligned = true,
325 },
Alexander Graf626c7a12012-10-08 13:47:30 +0200326 .impl = {
327 .min_access_size = 1,
328 .max_access_size = 4,
Jan Kiszka962b03f2013-06-22 08:07:03 +0200329 .unaligned = true,
Alexander Graf626c7a12012-10-08 13:47:30 +0200330 },
331 .endianness = DEVICE_LITTLE_ENDIAN,
Avi Kivityde009822011-08-08 16:09:25 +0300332};
333
Steven Smith01195b72011-06-16 17:05:17 +0100334static void platform_fixed_ioport_init(PCIXenPlatformState* s)
335{
Paolo Bonzini22fc8602013-06-06 21:25:08 -0400336 memory_region_init_io(&s->fixed_io, OBJECT(s), &platform_fixed_io_ops, s,
Avi Kivityde009822011-08-08 16:09:25 +0300337 "xen-fixed", 16);
338 memory_region_add_subregion(get_system_io(), XEN_PLATFORM_IOPORT,
339 &s->fixed_io);
Steven Smith01195b72011-06-16 17:05:17 +0100340}
341
342/* Xen Platform PCI Device */
343
Hervé Poussineau7a652ef2013-01-04 22:29:40 +0100344static uint64_t xen_platform_ioport_readb(void *opaque, hwaddr addr,
345 unsigned int size)
Steven Smith01195b72011-06-16 17:05:17 +0100346{
Steven Smith01195b72011-06-16 17:05:17 +0100347 if (addr == 0) {
Anthony PERARDe7b48c92011-11-04 15:35:11 +0000348 return platform_fixed_ioport_readb(opaque, 0);
Steven Smith01195b72011-06-16 17:05:17 +0100349 } else {
350 return ~0u;
351 }
352}
353
Hervé Poussineau7a652ef2013-01-04 22:29:40 +0100354static void xen_platform_ioport_writeb(void *opaque, hwaddr addr,
355 uint64_t val, unsigned int size)
Steven Smith01195b72011-06-16 17:05:17 +0100356{
357 PCIXenPlatformState *s = opaque;
Olaf Hering35132012016-10-21 14:37:07 +0200358 PCIDevice *pci_dev = PCI_DEVICE(s);
Steven Smith01195b72011-06-16 17:05:17 +0100359
Steven Smith01195b72011-06-16 17:05:17 +0100360 switch (addr) {
361 case 0: /* Platform flags */
Hervé Poussineau7a652ef2013-01-04 22:29:40 +0100362 platform_fixed_ioport_writeb(opaque, 0, (uint32_t)val);
Steven Smith01195b72011-06-16 17:05:17 +0100363 break;
Olaf Hering35132012016-10-21 14:37:07 +0200364 case 4:
365 if (val == 1) {
366 /*
367 * SUSE unplug for Xenlinux
368 * xen-kmp used this since xen-3.0.4, instead the official protocol
369 * from xen-3.3+ It did an unconditional "outl(1, (ioaddr + 4));"
370 * Pre VMDP 1.7 used 4 and 8 depending on how VMDP was configured.
371 * If VMDP was to control both disk and LAN it would use 4.
372 * If it controlled just disk or just LAN, it would use 8 below.
373 */
David Gibsonfd56e062017-11-29 19:46:27 +1100374 pci_unplug_disks(pci_get_bus(pci_dev), UNPLUG_IDE_SCSI_DISKS);
375 pci_unplug_nics(pci_get_bus(pci_dev));
Olaf Hering35132012016-10-21 14:37:07 +0200376 }
377 break;
Steven Smith01195b72011-06-16 17:05:17 +0100378 case 8:
Olaf Hering35132012016-10-21 14:37:07 +0200379 switch (val) {
380 case 1:
David Gibsonfd56e062017-11-29 19:46:27 +1100381 pci_unplug_disks(pci_get_bus(pci_dev), UNPLUG_IDE_SCSI_DISKS);
Olaf Hering35132012016-10-21 14:37:07 +0200382 break;
383 case 2:
David Gibsonfd56e062017-11-29 19:46:27 +1100384 pci_unplug_nics(pci_get_bus(pci_dev));
Olaf Hering35132012016-10-21 14:37:07 +0200385 break;
386 default:
387 log_writeb(s, (uint32_t)val);
388 break;
389 }
Steven Smith01195b72011-06-16 17:05:17 +0100390 break;
391 default:
392 break;
393 }
394}
395
Avi Kivityde009822011-08-08 16:09:25 +0300396static const MemoryRegionOps xen_pci_io_ops = {
Hervé Poussineau7a652ef2013-01-04 22:29:40 +0100397 .read = xen_platform_ioport_readb,
398 .write = xen_platform_ioport_writeb,
399 .impl.min_access_size = 1,
400 .impl.max_access_size = 1,
Avi Kivityde009822011-08-08 16:09:25 +0300401};
402
403static void platform_ioport_bar_setup(PCIXenPlatformState *d)
404{
Paolo Bonzini22fc8602013-06-06 21:25:08 -0400405 memory_region_init_io(&d->bar, OBJECT(d), &xen_pci_io_ops, d,
406 "xen-pci", 0x100);
Steven Smith01195b72011-06-16 17:05:17 +0100407}
408
Avi Kivitya8170e52012-10-23 12:30:10 +0200409static uint64_t platform_mmio_read(void *opaque, hwaddr addr,
Avi Kivityde009822011-08-08 16:09:25 +0300410 unsigned size)
Steven Smith01195b72011-06-16 17:05:17 +0100411{
412 DPRINTF("Warning: attempted read from physical address "
413 "0x" TARGET_FMT_plx " in xen platform mmio space\n", addr);
414
415 return 0;
416}
417
Avi Kivitya8170e52012-10-23 12:30:10 +0200418static void platform_mmio_write(void *opaque, hwaddr addr,
Avi Kivityde009822011-08-08 16:09:25 +0300419 uint64_t val, unsigned size)
Steven Smith01195b72011-06-16 17:05:17 +0100420{
Avi Kivityde009822011-08-08 16:09:25 +0300421 DPRINTF("Warning: attempted write of 0x%"PRIx64" to physical "
Steven Smith01195b72011-06-16 17:05:17 +0100422 "address 0x" TARGET_FMT_plx " in xen platform mmio space\n",
423 val, addr);
424}
425
Avi Kivityde009822011-08-08 16:09:25 +0300426static const MemoryRegionOps platform_mmio_handler = {
Steven Smith01195b72011-06-16 17:05:17 +0100427 .read = &platform_mmio_read,
428 .write = &platform_mmio_write,
Avi Kivityde009822011-08-08 16:09:25 +0300429 .endianness = DEVICE_NATIVE_ENDIAN,
Steven Smith01195b72011-06-16 17:05:17 +0100430};
431
Avi Kivityde009822011-08-08 16:09:25 +0300432static void platform_mmio_setup(PCIXenPlatformState *d)
Steven Smith01195b72011-06-16 17:05:17 +0100433{
Paolo Bonzini22fc8602013-06-06 21:25:08 -0400434 memory_region_init_io(&d->mmio_bar, OBJECT(d), &platform_mmio_handler, d,
Avi Kivityde009822011-08-08 16:09:25 +0300435 "xen-mmio", 0x1000000);
Steven Smith01195b72011-06-16 17:05:17 +0100436}
437
438static int xen_platform_post_load(void *opaque, int version_id)
439{
440 PCIXenPlatformState *s = opaque;
441
Anthony PERARDe7b48c92011-11-04 15:35:11 +0000442 platform_fixed_ioport_writeb(s, 0, s->flags);
Steven Smith01195b72011-06-16 17:05:17 +0100443
444 return 0;
445}
446
447static const VMStateDescription vmstate_xen_platform = {
448 .name = "platform",
449 .version_id = 4,
450 .minimum_version_id = 4,
Steven Smith01195b72011-06-16 17:05:17 +0100451 .post_load = xen_platform_post_load,
Juan Quintelad49805a2014-04-16 15:32:32 +0200452 .fields = (VMStateField[]) {
Andreas Färberdc4aa512013-06-30 15:20:05 +0200453 VMSTATE_PCI_DEVICE(parent_obj, PCIXenPlatformState),
Steven Smith01195b72011-06-16 17:05:17 +0100454 VMSTATE_UINT8(flags, PCIXenPlatformState),
455 VMSTATE_END_OF_LIST()
456 }
457};
458
Stefano Stabellini4098d492015-10-21 13:46:49 -0200459static void xen_platform_realize(PCIDevice *dev, Error **errp)
Steven Smith01195b72011-06-16 17:05:17 +0100460{
Peter Crosthwaite51a3fe92013-06-24 17:00:14 +1000461 PCIXenPlatformState *d = XEN_PLATFORM(dev);
Steven Smith01195b72011-06-16 17:05:17 +0100462 uint8_t *pci_conf;
463
Eduardo Habkostdbb74052015-09-28 17:01:24 -0300464 /* Device will crash on reset if xen is not initialized */
Eduardo Habkostb1ecd512015-10-21 13:46:50 -0200465 if (!xen_enabled()) {
466 error_setg(errp, "xen-platform device requires the Xen accelerator");
467 return;
468 }
Eduardo Habkostdbb74052015-09-28 17:01:24 -0300469
Andreas Färberdc4aa512013-06-30 15:20:05 +0200470 pci_conf = dev->config;
Steven Smith01195b72011-06-16 17:05:17 +0100471
Steven Smith01195b72011-06-16 17:05:17 +0100472 pci_set_word(pci_conf + PCI_COMMAND, PCI_COMMAND_IO | PCI_COMMAND_MEMORY);
473
Steven Smith01195b72011-06-16 17:05:17 +0100474 pci_config_set_prog_interface(pci_conf, 0);
475
Steven Smith01195b72011-06-16 17:05:17 +0100476 pci_conf[PCI_INTERRUPT_PIN] = 1;
477
Avi Kivityde009822011-08-08 16:09:25 +0300478 platform_ioport_bar_setup(d);
Andreas Färberdc4aa512013-06-30 15:20:05 +0200479 pci_register_bar(dev, 0, PCI_BASE_ADDRESS_SPACE_IO, &d->bar);
Steven Smith01195b72011-06-16 17:05:17 +0100480
481 /* reserve 16MB mmio address for share memory*/
Avi Kivityde009822011-08-08 16:09:25 +0300482 platform_mmio_setup(d);
Andreas Färberdc4aa512013-06-30 15:20:05 +0200483 pci_register_bar(dev, 1, PCI_BASE_ADDRESS_MEM_PREFETCH,
Avi Kivitye824b2c2011-08-08 16:09:31 +0300484 &d->mmio_bar);
Steven Smith01195b72011-06-16 17:05:17 +0100485
486 platform_fixed_ioport_init(d);
Steven Smith01195b72011-06-16 17:05:17 +0100487}
488
489static void platform_reset(DeviceState *dev)
490{
Peter Crosthwaite51a3fe92013-06-24 17:00:14 +1000491 PCIXenPlatformState *s = XEN_PLATFORM(dev);
Steven Smith01195b72011-06-16 17:05:17 +0100492
493 platform_fixed_ioport_reset(s);
494}
495
Anthony Liguori40021f02011-12-04 12:22:06 -0600496static void xen_platform_class_init(ObjectClass *klass, void *data)
497{
Anthony Liguori39bffca2011-12-07 21:34:16 -0600498 DeviceClass *dc = DEVICE_CLASS(klass);
Anthony Liguori40021f02011-12-04 12:22:06 -0600499 PCIDeviceClass *k = PCI_DEVICE_CLASS(klass);
Michael S. Tsirkin0d2b9622011-06-26 16:30:45 +0300500
Stefano Stabellini4098d492015-10-21 13:46:49 -0200501 k->realize = xen_platform_realize;
Anthony Liguori40021f02011-12-04 12:22:06 -0600502 k->vendor_id = PCI_VENDOR_ID_XEN;
503 k->device_id = PCI_DEVICE_ID_XEN_PLATFORM;
504 k->class_id = PCI_CLASS_OTHERS << 8 | 0x80;
505 k->subsystem_vendor_id = PCI_VENDOR_ID_XEN;
506 k->subsystem_id = PCI_DEVICE_ID_XEN_PLATFORM;
507 k->revision = 1;
Marcel Apfelbaum125ee0e2013-07-29 17:17:45 +0300508 set_bit(DEVICE_CATEGORY_MISC, dc->categories);
Anthony Liguori39bffca2011-12-07 21:34:16 -0600509 dc->desc = "XEN platform pci device";
510 dc->reset = platform_reset;
511 dc->vmsd = &vmstate_xen_platform;
Anthony Liguori40021f02011-12-04 12:22:06 -0600512}
513
Andreas Färber8c43a6f2013-01-10 16:19:07 +0100514static const TypeInfo xen_platform_info = {
Peter Crosthwaite51a3fe92013-06-24 17:00:14 +1000515 .name = TYPE_XEN_PLATFORM,
Anthony Liguori39bffca2011-12-07 21:34:16 -0600516 .parent = TYPE_PCI_DEVICE,
517 .instance_size = sizeof(PCIXenPlatformState),
518 .class_init = xen_platform_class_init,
Eduardo Habkostfd3b02c2017-09-27 16:56:34 -0300519 .interfaces = (InterfaceInfo[]) {
520 { INTERFACE_CONVENTIONAL_PCI_DEVICE },
521 { },
522 },
Steven Smith01195b72011-06-16 17:05:17 +0100523};
524
Andreas Färber83f7d432012-02-09 15:20:55 +0100525static void xen_platform_register_types(void)
Steven Smith01195b72011-06-16 17:05:17 +0100526{
Anthony Liguori39bffca2011-12-07 21:34:16 -0600527 type_register_static(&xen_platform_info);
Steven Smith01195b72011-06-16 17:05:17 +0100528}
529
Andreas Färber83f7d432012-02-09 15:20:55 +0100530type_init(xen_platform_register_types)