Statistics
| Branch: | Revision:

root / target-ppc / kvm_ppc.c @ 1dd9ffb9

History | View | Annotate | Download (2.7 kB)

1 d76d1650 aurel32
/*
2 d76d1650 aurel32
 * PowerPC KVM support
3 d76d1650 aurel32
 *
4 d76d1650 aurel32
 * Copyright IBM Corp. 2008
5 d76d1650 aurel32
 *
6 d76d1650 aurel32
 * Authors:
7 d76d1650 aurel32
 *  Hollis Blanchard <hollisb@us.ibm.com>
8 d76d1650 aurel32
 *
9 d76d1650 aurel32
 * This work is licensed under the terms of the GNU GPL, version 2 or later.
10 d76d1650 aurel32
 * See the COPYING file in the top-level directory.
11 d76d1650 aurel32
 *
12 d76d1650 aurel32
 */
13 d76d1650 aurel32
14 d76d1650 aurel32
#include "qemu-common.h"
15 d76d1650 aurel32
#include "qemu-timer.h"
16 d76d1650 aurel32
#include "kvm_ppc.h"
17 d76d1650 aurel32
#include "device_tree.h"
18 d76d1650 aurel32
19 d76d1650 aurel32
#define PROC_DEVTREE_PATH "/proc/device-tree"
20 d76d1650 aurel32
21 d76d1650 aurel32
static QEMUTimer *kvmppc_timer;
22 d76d1650 aurel32
static unsigned int kvmppc_timer_rate;
23 d76d1650 aurel32
24 d76d1650 aurel32
#ifdef HAVE_FDT
25 d76d1650 aurel32
static int kvmppc_read_host_property(const char *node_path, const char *prop,
26 d76d1650 aurel32
                                     void *val, size_t len)
27 d76d1650 aurel32
{
28 d76d1650 aurel32
    char *path;
29 d76d1650 aurel32
    FILE *f;
30 d76d1650 aurel32
    int ret;
31 d76d1650 aurel32
    int pathlen;
32 d76d1650 aurel32
33 d76d1650 aurel32
    pathlen = snprintf(NULL, 0, "%s/%s/%s", PROC_DEVTREE_PATH, node_path, prop)
34 d76d1650 aurel32
              + 1;
35 d76d1650 aurel32
    path = qemu_malloc(pathlen);
36 d76d1650 aurel32
    if (path == NULL) {
37 d76d1650 aurel32
        ret = -ENOMEM;
38 d76d1650 aurel32
        goto out;
39 d76d1650 aurel32
    }
40 d76d1650 aurel32
41 d76d1650 aurel32
    snprintf(path, pathlen, "%s/%s/%s", PROC_DEVTREE_PATH, node_path, prop);
42 d76d1650 aurel32
43 d76d1650 aurel32
    f = fopen(path, "rb");
44 d76d1650 aurel32
    if (f == NULL) {
45 d76d1650 aurel32
        ret = errno;
46 d76d1650 aurel32
        goto free;
47 d76d1650 aurel32
    }
48 d76d1650 aurel32
49 d76d1650 aurel32
    len = fread(val, len, 1, f);
50 d76d1650 aurel32
    if (len != 1) {
51 d76d1650 aurel32
        ret = ferror(f);
52 d76d1650 aurel32
        goto close;
53 d76d1650 aurel32
    }
54 d76d1650 aurel32
55 d76d1650 aurel32
close:
56 d76d1650 aurel32
    fclose(f);
57 d76d1650 aurel32
free:
58 d76d1650 aurel32
    free(path);
59 d76d1650 aurel32
out:
60 d76d1650 aurel32
    return ret;
61 d76d1650 aurel32
}
62 d76d1650 aurel32
63 d76d1650 aurel32
static int kvmppc_copy_host_cell(void *fdt, const char *node, const char *prop)
64 d76d1650 aurel32
{
65 d76d1650 aurel32
    uint32_t cell;
66 d76d1650 aurel32
    int ret;
67 d76d1650 aurel32
68 d76d1650 aurel32
    ret = kvmppc_read_host_property(node, prop, &cell, sizeof(cell));
69 d76d1650 aurel32
    if (ret < 0) {
70 d76d1650 aurel32
        fprintf(stderr, "couldn't read host %s/%s\n", node, prop);
71 d76d1650 aurel32
        goto out;
72 d76d1650 aurel32
    }
73 d76d1650 aurel32
74 d76d1650 aurel32
    ret = qemu_devtree_setprop_cell(fdt, node, prop, cell);
75 d76d1650 aurel32
    if (ret < 0) {
76 d76d1650 aurel32
        fprintf(stderr, "couldn't set guest %s/%s\n", node, prop);
77 d76d1650 aurel32
        goto out;
78 d76d1650 aurel32
    }
79 d76d1650 aurel32
80 d76d1650 aurel32
out:
81 d76d1650 aurel32
    return ret;
82 d76d1650 aurel32
}
83 d76d1650 aurel32
84 d76d1650 aurel32
void kvmppc_fdt_update(void *fdt)
85 d76d1650 aurel32
{
86 d76d1650 aurel32
    /* Copy data from the host device tree into the guest. Since the guest can
87 d76d1650 aurel32
     * directly access the timebase without host involvement, we must expose
88 d76d1650 aurel32
     * the correct frequencies. */
89 d76d1650 aurel32
    kvmppc_copy_host_cell(fdt, "/cpus/cpu@0", "clock-frequency");
90 d76d1650 aurel32
    kvmppc_copy_host_cell(fdt, "/cpus/cpu@0", "timebase-frequency");
91 d76d1650 aurel32
}
92 d76d1650 aurel32
#endif
93 d76d1650 aurel32
94 d76d1650 aurel32
static void kvmppc_timer_hack(void *opaque)
95 d76d1650 aurel32
{
96 d76d1650 aurel32
    qemu_service_io();
97 d76d1650 aurel32
    qemu_mod_timer(kvmppc_timer, qemu_get_clock(vm_clock) + kvmppc_timer_rate);
98 d76d1650 aurel32
}
99 d76d1650 aurel32
100 d76d1650 aurel32
void kvmppc_init(void)
101 d76d1650 aurel32
{
102 d76d1650 aurel32
    /* XXX The only reason KVM yields control back to qemu is device IO. Since
103 d76d1650 aurel32
     * an idle guest does no IO, qemu's device model will never get a chance to
104 d76d1650 aurel32
     * run. So, until Qemu gains IO threads, we create this timer to ensure
105 d76d1650 aurel32
     * that the device model gets a chance to run. */
106 d76d1650 aurel32
    kvmppc_timer_rate = ticks_per_sec / 10;
107 d76d1650 aurel32
    kvmppc_timer = qemu_new_timer(vm_clock, &kvmppc_timer_hack, NULL);
108 d76d1650 aurel32
    qemu_mod_timer(kvmppc_timer, qemu_get_clock(vm_clock) + kvmppc_timer_rate);
109 d76d1650 aurel32
}