blob: 0c1af1267843c9953ffdf4b052bb60610c446561 [file] [log] [blame]
Hollis Blanchardbc8080c2009-01-03 16:23:10 -06001/*
2 * Copyright (C) 2008 Freescale Semiconductor, Inc. All rights reserved.
3 *
4 * Author: Yu Liu, <yu.liu@freescale.com>
5 *
6 * Description:
7 * This file is derived from arch/powerpc/kvm/44x.c,
8 * by Hollis Blanchard <hollisb@us.ibm.com>.
9 *
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License, version 2, as
12 * published by the Free Software Foundation.
13 */
14
15#include <linux/kvm_host.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090016#include <linux/slab.h>
Hollis Blanchardbc8080c2009-01-03 16:23:10 -060017#include <linux/err.h>
18
19#include <asm/reg.h>
20#include <asm/cputable.h>
21#include <asm/tlbflush.h>
22#include <asm/kvm_e500.h>
23#include <asm/kvm_ppc.h>
24
Hollis Blanchardbb3a8a12009-01-03 16:23:13 -060025#include "booke.h"
Hollis Blanchardbc8080c2009-01-03 16:23:10 -060026#include "e500_tlb.h"
27
28void kvmppc_core_load_host_debugstate(struct kvm_vcpu *vcpu)
29{
30}
31
32void kvmppc_core_load_guest_debugstate(struct kvm_vcpu *vcpu)
33{
34}
35
36void kvmppc_core_vcpu_load(struct kvm_vcpu *vcpu, int cpu)
37{
38 kvmppc_e500_tlb_load(vcpu, cpu);
39}
40
41void kvmppc_core_vcpu_put(struct kvm_vcpu *vcpu)
42{
43 kvmppc_e500_tlb_put(vcpu);
44}
45
46int kvmppc_core_check_processor_compat(void)
47{
48 int r;
49
50 if (strcmp(cur_cpu_spec->cpu_name, "e500v2") == 0)
51 r = 0;
52 else
53 r = -ENOTSUPP;
54
55 return r;
56}
57
58int kvmppc_core_vcpu_setup(struct kvm_vcpu *vcpu)
59{
60 struct kvmppc_vcpu_e500 *vcpu_e500 = to_e500(vcpu);
61
62 kvmppc_e500_tlb_setup(vcpu_e500);
63
Liu Yua9040f22010-01-22 18:50:30 +080064 /* Registers init */
65 vcpu->arch.pvr = mfspr(SPRN_PVR);
Scott Wood90d34b02011-03-29 16:49:10 -050066 vcpu_e500->svr = mfspr(SPRN_SVR);
Liu Yua9040f22010-01-22 18:50:30 +080067
68 /* Since booke kvm only support one core, update all vcpus' PIR to 0 */
69 vcpu->vcpu_id = 0;
70
Hollis Blanchardbc8080c2009-01-03 16:23:10 -060071 return 0;
72}
73
74/* 'linear_address' is actually an encoding of AS|PID|EADDR . */
75int kvmppc_core_vcpu_translate(struct kvm_vcpu *vcpu,
76 struct kvm_translation *tr)
77{
78 int index;
79 gva_t eaddr;
80 u8 pid;
81 u8 as;
82
83 eaddr = tr->linear_address;
84 pid = (tr->linear_address >> 32) & 0xff;
85 as = (tr->linear_address >> 40) & 0x1;
86
87 index = kvmppc_e500_tlb_search(vcpu, eaddr, pid, as);
88 if (index < 0) {
89 tr->valid = 0;
90 return 0;
91 }
92
93 tr->physical_address = kvmppc_mmu_xlate(vcpu, index, eaddr);
94 /* XXX what does "writeable" and "usermode" even mean? */
95 tr->valid = 1;
96
97 return 0;
98}
99
100struct kvm_vcpu *kvmppc_core_vcpu_create(struct kvm *kvm, unsigned int id)
101{
102 struct kvmppc_vcpu_e500 *vcpu_e500;
103 struct kvm_vcpu *vcpu;
104 int err;
105
106 vcpu_e500 = kmem_cache_zalloc(kvm_vcpu_cache, GFP_KERNEL);
107 if (!vcpu_e500) {
108 err = -ENOMEM;
109 goto out;
110 }
111
112 vcpu = &vcpu_e500->vcpu;
113 err = kvm_vcpu_init(vcpu, kvm, id);
114 if (err)
115 goto free_vcpu;
116
117 err = kvmppc_e500_tlb_init(vcpu_e500);
118 if (err)
119 goto uninit_vcpu;
120
Alexander Graf96bc4512010-07-29 14:47:42 +0200121 vcpu->arch.shared = (void*)__get_free_page(GFP_KERNEL|__GFP_ZERO);
122 if (!vcpu->arch.shared)
123 goto uninit_tlb;
124
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600125 return vcpu;
126
Alexander Graf96bc4512010-07-29 14:47:42 +0200127uninit_tlb:
128 kvmppc_e500_tlb_uninit(vcpu_e500);
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600129uninit_vcpu:
130 kvm_vcpu_uninit(vcpu);
131free_vcpu:
132 kmem_cache_free(kvm_vcpu_cache, vcpu_e500);
133out:
134 return ERR_PTR(err);
135}
136
137void kvmppc_core_vcpu_free(struct kvm_vcpu *vcpu)
138{
139 struct kvmppc_vcpu_e500 *vcpu_e500 = to_e500(vcpu);
140
Alexander Graf96bc4512010-07-29 14:47:42 +0200141 free_page((unsigned long)vcpu->arch.shared);
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600142 kvm_vcpu_uninit(vcpu);
Scott Woodf22e2f02010-10-05 14:22:41 -0500143 kvmppc_e500_tlb_uninit(vcpu_e500);
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600144 kmem_cache_free(kvm_vcpu_cache, vcpu_e500);
145}
146
Stephen Rothwell2986b8c2009-06-02 11:46:14 +1000147static int __init kvmppc_e500_init(void)
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600148{
Hollis Blanchardbb3a8a12009-01-03 16:23:13 -0600149 int r, i;
150 unsigned long ivor[3];
151 unsigned long max_ivor = 0;
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600152
153 r = kvmppc_booke_init();
154 if (r)
155 return r;
156
Hollis Blanchardbb3a8a12009-01-03 16:23:13 -0600157 /* copy extra E500 exception handlers */
158 ivor[0] = mfspr(SPRN_IVOR32);
159 ivor[1] = mfspr(SPRN_IVOR33);
160 ivor[2] = mfspr(SPRN_IVOR34);
161 for (i = 0; i < 3; i++) {
162 if (ivor[i] > max_ivor)
163 max_ivor = ivor[i];
164
165 memcpy((void *)kvmppc_booke_handlers + ivor[i],
166 kvmppc_handlers_start + (i + 16) * kvmppc_handler_len,
167 kvmppc_handler_len);
168 }
169 flush_icache_range(kvmppc_booke_handlers,
170 kvmppc_booke_handlers + max_ivor + kvmppc_handler_len);
171
Avi Kivity0ee75be2010-04-28 15:39:01 +0300172 return kvm_init(NULL, sizeof(struct kvmppc_vcpu_e500), 0, THIS_MODULE);
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600173}
174
Jean Delvarea06cdb52010-05-18 09:34:12 +0200175static void __exit kvmppc_e500_exit(void)
Hollis Blanchardbc8080c2009-01-03 16:23:10 -0600176{
177 kvmppc_booke_exit();
178}
179
180module_init(kvmppc_e500_init);
181module_exit(kvmppc_e500_exit);