Statistics
| Branch: | Revision:

root / hw / xen_platform.c @ a0a3167a

History | View | Annotate | Download (9.7 kB)

1 01195b73 Steven Smith
/*
2 01195b73 Steven Smith
 * XEN platform pci device, formerly known as the event channel device
3 01195b73 Steven Smith
 *
4 01195b73 Steven Smith
 * Copyright (c) 2003-2004 Intel Corp.
5 01195b73 Steven Smith
 * Copyright (c) 2006 XenSource
6 01195b73 Steven Smith
 *
7 01195b73 Steven Smith
 * Permission is hereby granted, free of charge, to any person obtaining a copy
8 01195b73 Steven Smith
 * of this software and associated documentation files (the "Software"), to deal
9 01195b73 Steven Smith
 * in the Software without restriction, including without limitation the rights
10 01195b73 Steven Smith
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11 01195b73 Steven Smith
 * copies of the Software, and to permit persons to whom the Software is
12 01195b73 Steven Smith
 * furnished to do so, subject to the following conditions:
13 01195b73 Steven Smith
 *
14 01195b73 Steven Smith
 * The above copyright notice and this permission notice shall be included in
15 01195b73 Steven Smith
 * all copies or substantial portions of the Software.
16 01195b73 Steven Smith
 *
17 01195b73 Steven Smith
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 01195b73 Steven Smith
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 01195b73 Steven Smith
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
20 01195b73 Steven Smith
 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21 01195b73 Steven Smith
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22 01195b73 Steven Smith
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
23 01195b73 Steven Smith
 * THE SOFTWARE.
24 01195b73 Steven Smith
 */
25 01195b73 Steven Smith
26 01195b73 Steven Smith
#include <assert.h>
27 01195b73 Steven Smith
28 01195b73 Steven Smith
#include "hw.h"
29 01195b73 Steven Smith
#include "pc.h"
30 01195b73 Steven Smith
#include "pci.h"
31 01195b73 Steven Smith
#include "irq.h"
32 01195b73 Steven Smith
#include "xen_common.h"
33 01195b73 Steven Smith
#include "net.h"
34 01195b73 Steven Smith
#include "xen_backend.h"
35 01195b73 Steven Smith
#include "rwhandler.h"
36 01195b73 Steven Smith
#include "trace.h"
37 01195b73 Steven Smith
38 01195b73 Steven Smith
#include <xenguest.h>
39 01195b73 Steven Smith
40 01195b73 Steven Smith
//#define DEBUG_PLATFORM
41 01195b73 Steven Smith
42 01195b73 Steven Smith
#ifdef DEBUG_PLATFORM
43 01195b73 Steven Smith
#define DPRINTF(fmt, ...) do { \
44 01195b73 Steven Smith
    fprintf(stderr, "xen_platform: " fmt, ## __VA_ARGS__); \
45 01195b73 Steven Smith
} while (0)
46 01195b73 Steven Smith
#else
47 01195b73 Steven Smith
#define DPRINTF(fmt, ...) do { } while (0)
48 01195b73 Steven Smith
#endif
49 01195b73 Steven Smith
50 01195b73 Steven Smith
#define PFFLAG_ROM_LOCK 1 /* Sets whether ROM memory area is RW or RO */
51 01195b73 Steven Smith
52 01195b73 Steven Smith
typedef struct PCIXenPlatformState {
53 01195b73 Steven Smith
    PCIDevice  pci_dev;
54 01195b73 Steven Smith
    uint8_t flags; /* used only for version_id == 2 */
55 01195b73 Steven Smith
    int drivers_blacklisted;
56 01195b73 Steven Smith
    uint16_t driver_product_version;
57 01195b73 Steven Smith
58 01195b73 Steven Smith
    /* Log from guest drivers */
59 01195b73 Steven Smith
    char log_buffer[4096];
60 01195b73 Steven Smith
    int log_buffer_off;
61 01195b73 Steven Smith
} PCIXenPlatformState;
62 01195b73 Steven Smith
63 01195b73 Steven Smith
#define XEN_PLATFORM_IOPORT 0x10
64 01195b73 Steven Smith
65 01195b73 Steven Smith
/* Send bytes to syslog */
66 01195b73 Steven Smith
static void log_writeb(PCIXenPlatformState *s, char val)
67 01195b73 Steven Smith
{
68 01195b73 Steven Smith
    if (val == '\n' || s->log_buffer_off == sizeof(s->log_buffer) - 1) {
69 01195b73 Steven Smith
        /* Flush buffer */
70 01195b73 Steven Smith
        s->log_buffer[s->log_buffer_off] = 0;
71 01195b73 Steven Smith
        trace_xen_platform_log(s->log_buffer);
72 01195b73 Steven Smith
        s->log_buffer_off = 0;
73 01195b73 Steven Smith
    } else {
74 01195b73 Steven Smith
        s->log_buffer[s->log_buffer_off++] = val;
75 01195b73 Steven Smith
    }
76 01195b73 Steven Smith
}
77 01195b73 Steven Smith
78 01195b73 Steven Smith
/* Xen Platform, Fixed IOPort */
79 01195b73 Steven Smith
80 01195b73 Steven Smith
static void platform_fixed_ioport_writew(void *opaque, uint32_t addr, uint32_t val)
81 01195b73 Steven Smith
{
82 01195b73 Steven Smith
    PCIXenPlatformState *s = opaque;
83 01195b73 Steven Smith
84 01195b73 Steven Smith
    switch (addr - XEN_PLATFORM_IOPORT) {
85 01195b73 Steven Smith
    case 0:
86 01195b73 Steven Smith
        /* TODO: */
87 01195b73 Steven Smith
        /* Unplug devices.  Value is a bitmask of which devices to
88 01195b73 Steven Smith
           unplug, with bit 0 the IDE devices, bit 1 the network
89 01195b73 Steven Smith
           devices, and bit 2 the non-primary-master IDE devices. */
90 01195b73 Steven Smith
        break;
91 01195b73 Steven Smith
    case 2:
92 01195b73 Steven Smith
        switch (val) {
93 01195b73 Steven Smith
        case 1:
94 01195b73 Steven Smith
            DPRINTF("Citrix Windows PV drivers loaded in guest\n");
95 01195b73 Steven Smith
            break;
96 01195b73 Steven Smith
        case 0:
97 01195b73 Steven Smith
            DPRINTF("Guest claimed to be running PV product 0?\n");
98 01195b73 Steven Smith
            break;
99 01195b73 Steven Smith
        default:
100 01195b73 Steven Smith
            DPRINTF("Unknown PV product %d loaded in guest\n", val);
101 01195b73 Steven Smith
            break;
102 01195b73 Steven Smith
        }
103 01195b73 Steven Smith
        s->driver_product_version = val;
104 01195b73 Steven Smith
        break;
105 01195b73 Steven Smith
    }
106 01195b73 Steven Smith
}
107 01195b73 Steven Smith
108 01195b73 Steven Smith
static void platform_fixed_ioport_writel(void *opaque, uint32_t addr,
109 01195b73 Steven Smith
                                         uint32_t val)
110 01195b73 Steven Smith
{
111 01195b73 Steven Smith
    switch (addr - XEN_PLATFORM_IOPORT) {
112 01195b73 Steven Smith
    case 0:
113 01195b73 Steven Smith
        /* PV driver version */
114 01195b73 Steven Smith
        break;
115 01195b73 Steven Smith
    }
116 01195b73 Steven Smith
}
117 01195b73 Steven Smith
118 01195b73 Steven Smith
static void platform_fixed_ioport_writeb(void *opaque, uint32_t addr, uint32_t val)
119 01195b73 Steven Smith
{
120 01195b73 Steven Smith
    PCIXenPlatformState *s = opaque;
121 01195b73 Steven Smith
122 01195b73 Steven Smith
    switch (addr - XEN_PLATFORM_IOPORT) {
123 01195b73 Steven Smith
    case 0: /* Platform flags */ {
124 01195b73 Steven Smith
        hvmmem_type_t mem_type = (val & PFFLAG_ROM_LOCK) ?
125 01195b73 Steven Smith
            HVMMEM_ram_ro : HVMMEM_ram_rw;
126 01195b73 Steven Smith
        if (xc_hvm_set_mem_type(xen_xc, xen_domid, mem_type, 0xc0, 0x40)) {
127 01195b73 Steven Smith
            DPRINTF("unable to change ro/rw state of ROM memory area!\n");
128 01195b73 Steven Smith
        } else {
129 01195b73 Steven Smith
            s->flags = val & PFFLAG_ROM_LOCK;
130 01195b73 Steven Smith
            DPRINTF("changed ro/rw state of ROM memory area. now is %s state.\n",
131 01195b73 Steven Smith
                    (mem_type == HVMMEM_ram_ro ? "ro":"rw"));
132 01195b73 Steven Smith
        }
133 01195b73 Steven Smith
        break;
134 01195b73 Steven Smith
    }
135 01195b73 Steven Smith
    case 2:
136 01195b73 Steven Smith
        log_writeb(s, val);
137 01195b73 Steven Smith
        break;
138 01195b73 Steven Smith
    }
139 01195b73 Steven Smith
}
140 01195b73 Steven Smith
141 01195b73 Steven Smith
static uint32_t platform_fixed_ioport_readw(void *opaque, uint32_t addr)
142 01195b73 Steven Smith
{
143 01195b73 Steven Smith
    PCIXenPlatformState *s = opaque;
144 01195b73 Steven Smith
145 01195b73 Steven Smith
    switch (addr - XEN_PLATFORM_IOPORT) {
146 01195b73 Steven Smith
    case 0:
147 01195b73 Steven Smith
        if (s->drivers_blacklisted) {
148 01195b73 Steven Smith
            /* The drivers will recognise this magic number and refuse
149 01195b73 Steven Smith
             * to do anything. */
150 01195b73 Steven Smith
            return 0xd249;
151 01195b73 Steven Smith
        } else {
152 01195b73 Steven Smith
            /* Magic value so that you can identify the interface. */
153 01195b73 Steven Smith
            return 0x49d2;
154 01195b73 Steven Smith
        }
155 01195b73 Steven Smith
    default:
156 01195b73 Steven Smith
        return 0xffff;
157 01195b73 Steven Smith
    }
158 01195b73 Steven Smith
}
159 01195b73 Steven Smith
160 01195b73 Steven Smith
static uint32_t platform_fixed_ioport_readb(void *opaque, uint32_t addr)
161 01195b73 Steven Smith
{
162 01195b73 Steven Smith
    PCIXenPlatformState *s = opaque;
163 01195b73 Steven Smith
164 01195b73 Steven Smith
    switch (addr - XEN_PLATFORM_IOPORT) {
165 01195b73 Steven Smith
    case 0:
166 01195b73 Steven Smith
        /* Platform flags */
167 01195b73 Steven Smith
        return s->flags;
168 01195b73 Steven Smith
    case 2:
169 01195b73 Steven Smith
        /* Version number */
170 01195b73 Steven Smith
        return 1;
171 01195b73 Steven Smith
    default:
172 01195b73 Steven Smith
        return 0xff;
173 01195b73 Steven Smith
    }
174 01195b73 Steven Smith
}
175 01195b73 Steven Smith
176 01195b73 Steven Smith
static void platform_fixed_ioport_reset(void *opaque)
177 01195b73 Steven Smith
{
178 01195b73 Steven Smith
    PCIXenPlatformState *s = opaque;
179 01195b73 Steven Smith
180 01195b73 Steven Smith
    platform_fixed_ioport_writeb(s, XEN_PLATFORM_IOPORT, 0);
181 01195b73 Steven Smith
}
182 01195b73 Steven Smith
183 01195b73 Steven Smith
static void platform_fixed_ioport_init(PCIXenPlatformState* s)
184 01195b73 Steven Smith
{
185 01195b73 Steven Smith
    register_ioport_write(XEN_PLATFORM_IOPORT, 16, 4, platform_fixed_ioport_writel, s);
186 01195b73 Steven Smith
    register_ioport_write(XEN_PLATFORM_IOPORT, 16, 2, platform_fixed_ioport_writew, s);
187 01195b73 Steven Smith
    register_ioport_write(XEN_PLATFORM_IOPORT, 16, 1, platform_fixed_ioport_writeb, s);
188 01195b73 Steven Smith
    register_ioport_read(XEN_PLATFORM_IOPORT, 16, 2, platform_fixed_ioport_readw, s);
189 01195b73 Steven Smith
    register_ioport_read(XEN_PLATFORM_IOPORT, 16, 1, platform_fixed_ioport_readb, s);
190 01195b73 Steven Smith
}
191 01195b73 Steven Smith
192 01195b73 Steven Smith
/* Xen Platform PCI Device */
193 01195b73 Steven Smith
194 01195b73 Steven Smith
static uint32_t xen_platform_ioport_readb(void *opaque, uint32_t addr)
195 01195b73 Steven Smith
{
196 01195b73 Steven Smith
    addr &= 0xff;
197 01195b73 Steven Smith
198 01195b73 Steven Smith
    if (addr == 0) {
199 01195b73 Steven Smith
        return platform_fixed_ioport_readb(opaque, XEN_PLATFORM_IOPORT);
200 01195b73 Steven Smith
    } else {
201 01195b73 Steven Smith
        return ~0u;
202 01195b73 Steven Smith
    }
203 01195b73 Steven Smith
}
204 01195b73 Steven Smith
205 01195b73 Steven Smith
static void xen_platform_ioport_writeb(void *opaque, uint32_t addr, uint32_t val)
206 01195b73 Steven Smith
{
207 01195b73 Steven Smith
    PCIXenPlatformState *s = opaque;
208 01195b73 Steven Smith
209 01195b73 Steven Smith
    addr &= 0xff;
210 01195b73 Steven Smith
    val  &= 0xff;
211 01195b73 Steven Smith
212 01195b73 Steven Smith
    switch (addr) {
213 01195b73 Steven Smith
    case 0: /* Platform flags */
214 01195b73 Steven Smith
        platform_fixed_ioport_writeb(opaque, XEN_PLATFORM_IOPORT, val);
215 01195b73 Steven Smith
        break;
216 01195b73 Steven Smith
    case 8:
217 01195b73 Steven Smith
        log_writeb(s, val);
218 01195b73 Steven Smith
        break;
219 01195b73 Steven Smith
    default:
220 01195b73 Steven Smith
        break;
221 01195b73 Steven Smith
    }
222 01195b73 Steven Smith
}
223 01195b73 Steven Smith
224 01195b73 Steven Smith
static void platform_ioport_map(PCIDevice *pci_dev, int region_num, pcibus_t addr, pcibus_t size, int type)
225 01195b73 Steven Smith
{
226 01195b73 Steven Smith
    PCIXenPlatformState *d = DO_UPCAST(PCIXenPlatformState, pci_dev, pci_dev);
227 01195b73 Steven Smith
228 01195b73 Steven Smith
    register_ioport_write(addr, size, 1, xen_platform_ioport_writeb, d);
229 01195b73 Steven Smith
    register_ioport_read(addr, size, 1, xen_platform_ioport_readb, d);
230 01195b73 Steven Smith
}
231 01195b73 Steven Smith
232 01195b73 Steven Smith
static uint32_t platform_mmio_read(ReadWriteHandler *handler, pcibus_t addr, int len)
233 01195b73 Steven Smith
{
234 01195b73 Steven Smith
    DPRINTF("Warning: attempted read from physical address "
235 01195b73 Steven Smith
            "0x" TARGET_FMT_plx " in xen platform mmio space\n", addr);
236 01195b73 Steven Smith
237 01195b73 Steven Smith
    return 0;
238 01195b73 Steven Smith
}
239 01195b73 Steven Smith
240 01195b73 Steven Smith
static void platform_mmio_write(ReadWriteHandler *handler, pcibus_t addr,
241 01195b73 Steven Smith
                                uint32_t val, int len)
242 01195b73 Steven Smith
{
243 01195b73 Steven Smith
    DPRINTF("Warning: attempted write of 0x%x to physical "
244 01195b73 Steven Smith
            "address 0x" TARGET_FMT_plx " in xen platform mmio space\n",
245 01195b73 Steven Smith
            val, addr);
246 01195b73 Steven Smith
}
247 01195b73 Steven Smith
248 01195b73 Steven Smith
static ReadWriteHandler platform_mmio_handler = {
249 01195b73 Steven Smith
    .read = &platform_mmio_read,
250 01195b73 Steven Smith
    .write = &platform_mmio_write,
251 01195b73 Steven Smith
};
252 01195b73 Steven Smith
253 01195b73 Steven Smith
static void platform_mmio_map(PCIDevice *d, int region_num,
254 01195b73 Steven Smith
                              pcibus_t addr, pcibus_t size, int type)
255 01195b73 Steven Smith
{
256 01195b73 Steven Smith
    int mmio_io_addr;
257 01195b73 Steven Smith
258 01195b73 Steven Smith
    mmio_io_addr = cpu_register_io_memory_simple(&platform_mmio_handler,
259 01195b73 Steven Smith
                                                 DEVICE_NATIVE_ENDIAN);
260 01195b73 Steven Smith
261 01195b73 Steven Smith
    cpu_register_physical_memory(addr, size, mmio_io_addr);
262 01195b73 Steven Smith
}
263 01195b73 Steven Smith
264 01195b73 Steven Smith
static int xen_platform_post_load(void *opaque, int version_id)
265 01195b73 Steven Smith
{
266 01195b73 Steven Smith
    PCIXenPlatformState *s = opaque;
267 01195b73 Steven Smith
268 01195b73 Steven Smith
    platform_fixed_ioport_writeb(s, XEN_PLATFORM_IOPORT, s->flags);
269 01195b73 Steven Smith
270 01195b73 Steven Smith
    return 0;
271 01195b73 Steven Smith
}
272 01195b73 Steven Smith
273 01195b73 Steven Smith
static const VMStateDescription vmstate_xen_platform = {
274 01195b73 Steven Smith
    .name = "platform",
275 01195b73 Steven Smith
    .version_id = 4,
276 01195b73 Steven Smith
    .minimum_version_id = 4,
277 01195b73 Steven Smith
    .minimum_version_id_old = 4,
278 01195b73 Steven Smith
    .post_load = xen_platform_post_load,
279 01195b73 Steven Smith
    .fields = (VMStateField []) {
280 01195b73 Steven Smith
        VMSTATE_PCI_DEVICE(pci_dev, PCIXenPlatformState),
281 01195b73 Steven Smith
        VMSTATE_UINT8(flags, PCIXenPlatformState),
282 01195b73 Steven Smith
        VMSTATE_END_OF_LIST()
283 01195b73 Steven Smith
    }
284 01195b73 Steven Smith
};
285 01195b73 Steven Smith
286 01195b73 Steven Smith
static int xen_platform_initfn(PCIDevice *dev)
287 01195b73 Steven Smith
{
288 01195b73 Steven Smith
    PCIXenPlatformState *d = DO_UPCAST(PCIXenPlatformState, pci_dev, dev);
289 01195b73 Steven Smith
    uint8_t *pci_conf;
290 01195b73 Steven Smith
291 01195b73 Steven Smith
    pci_conf = d->pci_dev.config;
292 01195b73 Steven Smith
293 01195b73 Steven Smith
    pci_config_set_vendor_id(pci_conf, PCI_VENDOR_ID_XENSOURCE);
294 01195b73 Steven Smith
    pci_config_set_device_id(pci_conf, 0x0001);
295 01195b73 Steven Smith
    pci_set_word(pci_conf + PCI_SUBSYSTEM_VENDOR_ID, PCI_VENDOR_ID_XENSOURCE);
296 01195b73 Steven Smith
    pci_set_word(pci_conf + PCI_SUBSYSTEM_ID, 0x0001);
297 01195b73 Steven Smith
298 01195b73 Steven Smith
    pci_set_word(pci_conf + PCI_COMMAND, PCI_COMMAND_IO | PCI_COMMAND_MEMORY);
299 01195b73 Steven Smith
300 01195b73 Steven Smith
    pci_config_set_revision(pci_conf, 1);
301 01195b73 Steven Smith
    pci_config_set_prog_interface(pci_conf, 0);
302 01195b73 Steven Smith
303 01195b73 Steven Smith
    pci_config_set_class(pci_conf, PCI_CLASS_OTHERS << 8 | 0x80);
304 01195b73 Steven Smith
305 01195b73 Steven Smith
    pci_conf[PCI_INTERRUPT_PIN] = 1;
306 01195b73 Steven Smith
307 01195b73 Steven Smith
    pci_register_bar(&d->pci_dev, 0, 0x100,
308 01195b73 Steven Smith
            PCI_BASE_ADDRESS_SPACE_IO, platform_ioport_map);
309 01195b73 Steven Smith
310 01195b73 Steven Smith
    /* reserve 16MB mmio address for share memory*/
311 01195b73 Steven Smith
    pci_register_bar(&d->pci_dev, 1, 0x1000000,
312 01195b73 Steven Smith
            PCI_BASE_ADDRESS_MEM_PREFETCH, platform_mmio_map);
313 01195b73 Steven Smith
314 01195b73 Steven Smith
    platform_fixed_ioport_init(d);
315 01195b73 Steven Smith
316 01195b73 Steven Smith
    return 0;
317 01195b73 Steven Smith
}
318 01195b73 Steven Smith
319 01195b73 Steven Smith
static void platform_reset(DeviceState *dev)
320 01195b73 Steven Smith
{
321 01195b73 Steven Smith
    PCIXenPlatformState *s = DO_UPCAST(PCIXenPlatformState, pci_dev.qdev, dev);
322 01195b73 Steven Smith
323 01195b73 Steven Smith
    platform_fixed_ioport_reset(s);
324 01195b73 Steven Smith
}
325 01195b73 Steven Smith
326 01195b73 Steven Smith
static PCIDeviceInfo xen_platform_info = {
327 01195b73 Steven Smith
    .init = xen_platform_initfn,
328 01195b73 Steven Smith
    .qdev.name = "xen-platform",
329 01195b73 Steven Smith
    .qdev.desc = "XEN platform pci device",
330 01195b73 Steven Smith
    .qdev.size = sizeof(PCIXenPlatformState),
331 01195b73 Steven Smith
    .qdev.vmsd = &vmstate_xen_platform,
332 01195b73 Steven Smith
    .qdev.reset = platform_reset,
333 01195b73 Steven Smith
};
334 01195b73 Steven Smith
335 01195b73 Steven Smith
static void xen_platform_register(void)
336 01195b73 Steven Smith
{
337 01195b73 Steven Smith
    pci_qdev_register(&xen_platform_info);
338 01195b73 Steven Smith
}
339 01195b73 Steven Smith
340 01195b73 Steven Smith
device_init(xen_platform_register);