44x.c 5.4 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
/*
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License, version 2, as
 * published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 *
 * Copyright IBM Corp. 2008
 *
 * Authors: Hollis Blanchard <hollisb@us.ibm.com>
 */

#include <linux/kvm_host.h>
21
#include <linux/slab.h>
22
#include <linux/err.h>
23
#include <linux/export.h>
24

25 26 27
#include <asm/reg.h>
#include <asm/cputable.h>
#include <asm/tlbflush.h>
28 29
#include <asm/kvm_44x.h>
#include <asm/kvm_ppc.h>
30 31

#include "44x_tlb.h"
32
#include "booke.h"
33

34
static void kvmppc_core_vcpu_load_44x(struct kvm_vcpu *vcpu, int cpu)
35
{
36
	kvmppc_booke_vcpu_load(vcpu, cpu);
37
	kvmppc_44x_tlb_load(vcpu);
38 39
}

40
static void kvmppc_core_vcpu_put_44x(struct kvm_vcpu *vcpu)
41
{
42
	kvmppc_44x_tlb_put(vcpu);
43
	kvmppc_booke_vcpu_put(vcpu);
44 45 46 47 48 49
}

int kvmppc_core_check_processor_compat(void)
{
	int r;

50
	if (strncmp(cur_cpu_spec->platform, "ppc440", 6) == 0)
51 52 53 54 55 56
		r = 0;
	else
		r = -ENOTSUPP;

	return r;
}
57 58 59

int kvmppc_core_vcpu_setup(struct kvm_vcpu *vcpu)
{
60 61
	struct kvmppc_vcpu_44x *vcpu_44x = to_44x(vcpu);
	struct kvmppc_44x_tlbe *tlbe = &vcpu_44x->guest_tlb[0];
62
	int i;
63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78

	tlbe->tid = 0;
	tlbe->word0 = PPC44x_TLB_16M | PPC44x_TLB_VALID;
	tlbe->word1 = 0;
	tlbe->word2 = PPC44x_TLB_SX | PPC44x_TLB_SW | PPC44x_TLB_SR;

	tlbe++;
	tlbe->tid = 0;
	tlbe->word0 = 0xef600000 | PPC44x_TLB_4K | PPC44x_TLB_VALID;
	tlbe->word1 = 0xef600000;
	tlbe->word2 = PPC44x_TLB_SX | PPC44x_TLB_SW | PPC44x_TLB_SR
	              | PPC44x_TLB_I | PPC44x_TLB_G;

	/* Since the guest can directly access the timebase, it must know the
	 * real timebase frequency. Accordingly, it must see the state of
	 * CCR1[TCS]. */
79
	/* XXX CCR1 doesn't exist on all 440 SoCs. */
80 81
	vcpu->arch.ccr1 = mfspr(SPRN_CCR1);

82 83 84
	for (i = 0; i < ARRAY_SIZE(vcpu_44x->shadow_refs); i++)
		vcpu_44x->shadow_refs[i].gtlb_index = -1;

85
	vcpu->arch.cpu_type = KVM_CPU_440;
A
Alexander Graf 已提交
86
	vcpu->arch.pvr = mfspr(SPRN_PVR);
87

88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109
	return 0;
}

/* 'linear_address' is actually an encoding of AS|PID|EADDR . */
int kvmppc_core_vcpu_translate(struct kvm_vcpu *vcpu,
                               struct kvm_translation *tr)
{
	int index;
	gva_t eaddr;
	u8 pid;
	u8 as;

	eaddr = tr->linear_address;
	pid = (tr->linear_address >> 32) & 0xff;
	as = (tr->linear_address >> 40) & 0x1;

	index = kvmppc_44x_tlb_index(vcpu, eaddr, pid, as);
	if (index == -1) {
		tr->valid = 0;
		return 0;
	}

110
	tr->physical_address = kvmppc_mmu_xlate(vcpu, index, eaddr);
111 112 113 114 115
	/* XXX what does "writeable" and "usermode" even mean? */
	tr->valid = 1;

	return 0;
}
116

117 118
static int kvmppc_core_get_sregs_44x(struct kvm_vcpu *vcpu,
				      struct kvm_sregs *sregs)
S
Scott Wood 已提交
119
{
120
	return kvmppc_get_sregs_ivor(vcpu, sregs);
S
Scott Wood 已提交
121 122
}

123 124
static int kvmppc_core_set_sregs_44x(struct kvm_vcpu *vcpu,
				     struct kvm_sregs *sregs)
S
Scott Wood 已提交
125 126 127 128
{
	return kvmppc_set_sregs_ivor(vcpu, sregs);
}

129 130
static int kvmppc_get_one_reg_44x(struct kvm_vcpu *vcpu, u64 id,
				  union kvmppc_one_reg *val)
131 132 133 134
{
	return -EINVAL;
}

135 136
static int kvmppc_set_one_reg_44x(struct kvm_vcpu *vcpu, u64 id,
				  union kvmppc_one_reg *val)
137 138 139 140
{
	return -EINVAL;
}

141 142
static struct kvm_vcpu *kvmppc_core_vcpu_create_44x(struct kvm *kvm,
						    unsigned int id)
143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158
{
	struct kvmppc_vcpu_44x *vcpu_44x;
	struct kvm_vcpu *vcpu;
	int err;

	vcpu_44x = kmem_cache_zalloc(kvm_vcpu_cache, GFP_KERNEL);
	if (!vcpu_44x) {
		err = -ENOMEM;
		goto out;
	}

	vcpu = &vcpu_44x->vcpu;
	err = kvm_vcpu_init(vcpu, kvm, id);
	if (err)
		goto free_vcpu;

A
Alexander Graf 已提交
159 160 161 162
	vcpu->arch.shared = (void*)__get_free_page(GFP_KERNEL|__GFP_ZERO);
	if (!vcpu->arch.shared)
		goto uninit_vcpu;

163 164
	return vcpu;

A
Alexander Graf 已提交
165 166
uninit_vcpu:
	kvm_vcpu_uninit(vcpu);
167 168 169 170 171 172
free_vcpu:
	kmem_cache_free(kvm_vcpu_cache, vcpu_44x);
out:
	return ERR_PTR(err);
}

173
static void kvmppc_core_vcpu_free_44x(struct kvm_vcpu *vcpu)
174 175 176
{
	struct kvmppc_vcpu_44x *vcpu_44x = to_44x(vcpu);

A
Alexander Graf 已提交
177
	free_page((unsigned long)vcpu->arch.shared);
178 179 180 181
	kvm_vcpu_uninit(vcpu);
	kmem_cache_free(kvm_vcpu_cache, vcpu_44x);
}

182
static int kvmppc_core_init_vm_44x(struct kvm *kvm)
183 184 185 186
{
	return 0;
}

187
static void kvmppc_core_destroy_vm_44x(struct kvm *kvm)
188 189 190
{
}

191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207
static struct kvmppc_ops kvm_ops_44x = {
	.get_sregs = kvmppc_core_get_sregs_44x,
	.set_sregs = kvmppc_core_set_sregs_44x,
	.get_one_reg = kvmppc_get_one_reg_44x,
	.set_one_reg = kvmppc_set_one_reg_44x,
	.vcpu_load   = kvmppc_core_vcpu_load_44x,
	.vcpu_put    = kvmppc_core_vcpu_put_44x,
	.vcpu_create = kvmppc_core_vcpu_create_44x,
	.vcpu_free   = kvmppc_core_vcpu_free_44x,
	.mmu_destroy  = kvmppc_mmu_destroy_44x,
	.init_vm = kvmppc_core_init_vm_44x,
	.destroy_vm = kvmppc_core_destroy_vm_44x,
	.emulate_op = kvmppc_core_emulate_op_44x,
	.emulate_mtspr = kvmppc_core_emulate_mtspr_44x,
	.emulate_mfspr = kvmppc_core_emulate_mfspr_44x,
};

208
static int __init kvmppc_44x_init(void)
209 210 211 212 213
{
	int r;

	r = kvmppc_booke_init();
	if (r)
214
		goto err_out;
215

216 217 218 219 220 221
	r = kvm_init(&kvm_ops_44x, sizeof(struct kvmppc_vcpu_44x),
		     0, THIS_MODULE);
	if (r)
		goto err_out;
err_out:
	return r;
222 223
}

224
static void __exit kvmppc_44x_exit(void)
225 226 227 228 229 230
{
	kvmppc_booke_exit();
}

module_init(kvmppc_44x_init);
module_exit(kvmppc_44x_exit);