Statistics
| Branch: | Revision:

root / target-ppc / kvm_ppc.c @ c3d420ea

History | View | Annotate | Download (2.6 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 3f0855b1 Juan Quintela
#ifdef CONFIG_FDT
25 ea23bc20 aurel32
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 57be80f9 aliguori
    int ret = 0;
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
37 d76d1650 aurel32
    snprintf(path, pathlen, "%s/%s/%s", PROC_DEVTREE_PATH, node_path, prop);
38 d76d1650 aurel32
39 d76d1650 aurel32
    f = fopen(path, "rb");
40 d76d1650 aurel32
    if (f == NULL) {
41 d76d1650 aurel32
        ret = errno;
42 d76d1650 aurel32
        goto free;
43 d76d1650 aurel32
    }
44 d76d1650 aurel32
45 d76d1650 aurel32
    len = fread(val, len, 1, f);
46 d76d1650 aurel32
    if (len != 1) {
47 d76d1650 aurel32
        ret = ferror(f);
48 d76d1650 aurel32
        goto close;
49 d76d1650 aurel32
    }
50 d76d1650 aurel32
51 d76d1650 aurel32
close:
52 d76d1650 aurel32
    fclose(f);
53 d76d1650 aurel32
free:
54 d76d1650 aurel32
    free(path);
55 d76d1650 aurel32
    return ret;
56 d76d1650 aurel32
}
57 d76d1650 aurel32
58 d76d1650 aurel32
static int kvmppc_copy_host_cell(void *fdt, const char *node, const char *prop)
59 d76d1650 aurel32
{
60 d76d1650 aurel32
    uint32_t cell;
61 d76d1650 aurel32
    int ret;
62 d76d1650 aurel32
63 d76d1650 aurel32
    ret = kvmppc_read_host_property(node, prop, &cell, sizeof(cell));
64 d76d1650 aurel32
    if (ret < 0) {
65 d76d1650 aurel32
        fprintf(stderr, "couldn't read host %s/%s\n", node, prop);
66 d76d1650 aurel32
        goto out;
67 d76d1650 aurel32
    }
68 d76d1650 aurel32
69 d76d1650 aurel32
    ret = qemu_devtree_setprop_cell(fdt, node, prop, cell);
70 d76d1650 aurel32
    if (ret < 0) {
71 d76d1650 aurel32
        fprintf(stderr, "couldn't set guest %s/%s\n", node, prop);
72 d76d1650 aurel32
        goto out;
73 d76d1650 aurel32
    }
74 d76d1650 aurel32
75 d76d1650 aurel32
out:
76 d76d1650 aurel32
    return ret;
77 d76d1650 aurel32
}
78 d76d1650 aurel32
79 d76d1650 aurel32
void kvmppc_fdt_update(void *fdt)
80 d76d1650 aurel32
{
81 d76d1650 aurel32
    /* Copy data from the host device tree into the guest. Since the guest can
82 d76d1650 aurel32
     * directly access the timebase without host involvement, we must expose
83 d76d1650 aurel32
     * the correct frequencies. */
84 d76d1650 aurel32
    kvmppc_copy_host_cell(fdt, "/cpus/cpu@0", "clock-frequency");
85 d76d1650 aurel32
    kvmppc_copy_host_cell(fdt, "/cpus/cpu@0", "timebase-frequency");
86 d76d1650 aurel32
}
87 d76d1650 aurel32
#endif
88 d76d1650 aurel32
89 d76d1650 aurel32
static void kvmppc_timer_hack(void *opaque)
90 d76d1650 aurel32
{
91 d76d1650 aurel32
    qemu_service_io();
92 d76d1650 aurel32
    qemu_mod_timer(kvmppc_timer, qemu_get_clock(vm_clock) + kvmppc_timer_rate);
93 d76d1650 aurel32
}
94 d76d1650 aurel32
95 d76d1650 aurel32
void kvmppc_init(void)
96 d76d1650 aurel32
{
97 d76d1650 aurel32
    /* XXX The only reason KVM yields control back to qemu is device IO. Since
98 d76d1650 aurel32
     * an idle guest does no IO, qemu's device model will never get a chance to
99 d76d1650 aurel32
     * run. So, until Qemu gains IO threads, we create this timer to ensure
100 d76d1650 aurel32
     * that the device model gets a chance to run. */
101 6ee093c9 Juan Quintela
    kvmppc_timer_rate = get_ticks_per_sec() / 10;
102 d76d1650 aurel32
    kvmppc_timer = qemu_new_timer(vm_clock, &kvmppc_timer_hack, NULL);
103 d76d1650 aurel32
    qemu_mod_timer(kvmppc_timer, qemu_get_clock(vm_clock) + kvmppc_timer_rate);
104 d76d1650 aurel32
}