blob: 8443e23f36052e30fde8873a527323b0f14e9f4f [file] [log] [blame]
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -05001/*
2 * This program is free software; you can redistribute it and/or modify
3 * it under the terms of the GNU General Public License, version 2, as
4 * published by the Free Software Foundation.
5 *
6 * This program is distributed in the hope that it will be useful,
7 * but WITHOUT ANY WARRANTY; without even the implied warranty of
8 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
9 * GNU General Public License for more details.
10 *
11 * You should have received a copy of the GNU General Public License
12 * along with this program; if not, write to the Free Software
13 * Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
14 *
15 * Copyright IBM Corp. 2007
16 *
17 * Authors: Hollis Blanchard <hollisb@us.ibm.com>
18 * Christian Ehrhardt <ehrhardt@linux.vnet.ibm.com>
19 */
20
21#include <linux/errno.h>
22#include <linux/err.h>
23#include <linux/kvm_host.h>
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -050024#include <linux/vmalloc.h>
Alexander Graf544c6762009-11-02 12:02:31 +000025#include <linux/hrtimer.h>
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -050026#include <linux/fs.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090027#include <linux/slab.h>
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -050028#include <asm/cputable.h>
29#include <asm/uaccess.h>
30#include <asm/kvm_ppc.h>
Hollis Blanchard83aae4a2008-07-25 13:54:52 -050031#include <asm/tlbflush.h>
Paul Mackerras371fefd2011-06-29 00:23:08 +000032#include <asm/cputhreads.h>
Alexander Grafbd2be682012-08-13 01:04:19 +020033#include <asm/irqflags.h>
Hollis Blanchard73e75b42008-12-02 15:51:57 -060034#include "timing.h"
Paul Mackerrasfad7b9b2008-12-23 14:57:26 +110035#include "../mm/mmu_decl.h"
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -050036
Marcelo Tosatti46f43c62009-06-18 11:47:27 -030037#define CREATE_TRACE_POINTS
38#include "trace.h"
39
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -050040int kvm_arch_vcpu_runnable(struct kvm_vcpu *v)
41{
Liu Yu-B132019202e072012-07-03 05:48:52 +000042 return !!(v->arch.pending_exceptions) ||
Scott Wooddfd4d472011-11-17 12:39:59 +000043 v->requests;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -050044}
45
Christoffer Dallb6d33832012-03-08 16:44:24 -050046int kvm_arch_vcpu_should_kick(struct kvm_vcpu *vcpu)
47{
48 return 1;
49}
50
Alexander Graf03d25c52012-08-10 12:28:50 +020051#ifndef CONFIG_KVM_BOOK3S_64_HV
52/*
53 * Common checks before entering the guest world. Call with interrupts
54 * disabled.
55 *
Alexander Graf7ee78852012-08-13 12:44:41 +020056 * returns:
57 *
58 * == 1 if we're ready to go into guest state
59 * <= 0 if we need to go back to the host with return value
Alexander Graf03d25c52012-08-10 12:28:50 +020060 */
61int kvmppc_prepare_to_enter(struct kvm_vcpu *vcpu)
62{
Alexander Graf7ee78852012-08-13 12:44:41 +020063 int r = 1;
Alexander Graf03d25c52012-08-10 12:28:50 +020064
65 WARN_ON_ONCE(!irqs_disabled());
66 while (true) {
67 if (need_resched()) {
68 local_irq_enable();
69 cond_resched();
70 local_irq_disable();
71 continue;
72 }
73
74 if (signal_pending(current)) {
Alexander Graf7ee78852012-08-13 12:44:41 +020075 kvmppc_account_exit(vcpu, SIGNAL_EXITS);
76 vcpu->run->exit_reason = KVM_EXIT_INTR;
77 r = -EINTR;
Alexander Graf03d25c52012-08-10 12:28:50 +020078 break;
79 }
80
81 smp_mb();
82 if (vcpu->requests) {
83 /* Make sure we process requests preemptable */
84 local_irq_enable();
85 trace_kvm_check_requests(vcpu);
Alexander Graf7c973a22012-08-13 12:50:35 +020086 r = kvmppc_core_check_requests(vcpu);
Alexander Graf03d25c52012-08-10 12:28:50 +020087 local_irq_disable();
Alexander Graf7c973a22012-08-13 12:50:35 +020088 if (r > 0)
89 continue;
90 break;
Alexander Graf03d25c52012-08-10 12:28:50 +020091 }
92
93 if (kvmppc_core_prepare_to_enter(vcpu)) {
94 /* interrupts got enabled in between, so we
95 are back at square 1 */
96 continue;
97 }
98
Alexander Grafbd2be682012-08-13 01:04:19 +020099#ifdef CONFIG_PPC64
100 /* lazy EE magic */
101 hard_irq_disable();
102 if (lazy_irq_pending()) {
103 /* Got an interrupt in between, try again */
104 local_irq_enable();
105 local_irq_disable();
Alexander Graf3766a4c2012-08-13 01:24:01 +0200106 kvm_guest_exit();
Alexander Grafbd2be682012-08-13 01:04:19 +0200107 continue;
108 }
109
110 trace_hardirqs_on();
111#endif
112
Alexander Graf3766a4c2012-08-13 01:24:01 +0200113 kvm_guest_enter();
114
Alexander Graf03d25c52012-08-10 12:28:50 +0200115 /* Going into guest context! Yay! */
116 vcpu->mode = IN_GUEST_MODE;
117 smp_wmb();
118
119 break;
120 }
121
122 return r;
123}
124#endif /* CONFIG_KVM_BOOK3S_64_HV */
125
Alexander Graf2a342ed2010-07-29 14:47:48 +0200126int kvmppc_kvm_pv(struct kvm_vcpu *vcpu)
127{
128 int nr = kvmppc_get_gpr(vcpu, 11);
129 int r;
130 unsigned long __maybe_unused param1 = kvmppc_get_gpr(vcpu, 3);
131 unsigned long __maybe_unused param2 = kvmppc_get_gpr(vcpu, 4);
132 unsigned long __maybe_unused param3 = kvmppc_get_gpr(vcpu, 5);
133 unsigned long __maybe_unused param4 = kvmppc_get_gpr(vcpu, 6);
134 unsigned long r2 = 0;
135
136 if (!(vcpu->arch.shared->msr & MSR_SF)) {
137 /* 32 bit mode */
138 param1 &= 0xffffffff;
139 param2 &= 0xffffffff;
140 param3 &= 0xffffffff;
141 param4 &= 0xffffffff;
142 }
143
144 switch (nr) {
Stuart Yoderfdcf8bd2012-07-03 05:48:50 +0000145 case KVM_HCALL_TOKEN(KVM_HC_PPC_MAP_MAGIC_PAGE):
Alexander Graf5fc87402010-07-29 14:47:55 +0200146 {
147 vcpu->arch.magic_page_pa = param1;
148 vcpu->arch.magic_page_ea = param2;
149
Scott Woodb5904972011-11-08 18:23:30 -0600150 r2 = KVM_MAGIC_FEAT_SR | KVM_MAGIC_FEAT_MAS0_TO_SPRG7;
Alexander Graf7508e162010-08-03 11:32:56 +0200151
Stuart Yoderfdcf8bd2012-07-03 05:48:50 +0000152 r = EV_SUCCESS;
Alexander Graf5fc87402010-07-29 14:47:55 +0200153 break;
154 }
Stuart Yoderfdcf8bd2012-07-03 05:48:50 +0000155 case KVM_HCALL_TOKEN(KVM_HC_FEATURES):
156 r = EV_SUCCESS;
Alexander Grafbf7ca4b2012-02-15 23:40:00 +0000157#if defined(CONFIG_PPC_BOOK3S) || defined(CONFIG_KVM_E500V2)
Scott Wooda4cd8b22011-06-14 18:34:41 -0500158 /* XXX Missing magic page on 44x */
Alexander Graf5fc87402010-07-29 14:47:55 +0200159 r2 |= (1 << KVM_FEATURE_MAGIC_PAGE);
160#endif
Alexander Graf2a342ed2010-07-29 14:47:48 +0200161
162 /* Second return value is in r4 */
Alexander Graf2a342ed2010-07-29 14:47:48 +0200163 break;
Liu Yu-B132019202e072012-07-03 05:48:52 +0000164 case EV_HCALL_TOKEN(EV_IDLE):
165 r = EV_SUCCESS;
166 kvm_vcpu_block(vcpu);
167 clear_bit(KVM_REQ_UNHALT, &vcpu->requests);
168 break;
Alexander Graf2a342ed2010-07-29 14:47:48 +0200169 default:
Stuart Yoderfdcf8bd2012-07-03 05:48:50 +0000170 r = EV_UNIMPLEMENTED;
Alexander Graf2a342ed2010-07-29 14:47:48 +0200171 break;
172 }
173
Alexander Graf7508e162010-08-03 11:32:56 +0200174 kvmppc_set_gpr(vcpu, 4, r2);
175
Alexander Graf2a342ed2010-07-29 14:47:48 +0200176 return r;
177}
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500178
Alexander Grafaf8f38b2011-08-10 13:57:08 +0200179int kvmppc_sanity_check(struct kvm_vcpu *vcpu)
180{
181 int r = false;
182
183 /* We have to know what CPU to virtualize */
184 if (!vcpu->arch.pvr)
185 goto out;
186
187 /* PAPR only works with book3s_64 */
188 if ((vcpu->arch.cpu_type != KVM_CPU_3S_64) && vcpu->arch.papr_enabled)
189 goto out;
190
191#ifdef CONFIG_KVM_BOOK3S_64_HV
192 /* HV KVM can only do PAPR mode for now */
193 if (!vcpu->arch.papr_enabled)
194 goto out;
195#endif
196
Scott Woodd30f6e42011-12-20 15:34:43 +0000197#ifdef CONFIG_KVM_BOOKE_HV
198 if (!cpu_has_feature(CPU_FTR_EMB_HV))
199 goto out;
200#endif
201
Alexander Grafaf8f38b2011-08-10 13:57:08 +0200202 r = true;
203
204out:
205 vcpu->arch.sane = r;
206 return r ? 0 : -EINVAL;
207}
208
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500209int kvmppc_emulate_mmio(struct kvm_run *run, struct kvm_vcpu *vcpu)
210{
211 enum emulation_result er;
212 int r;
213
214 er = kvmppc_emulate_instruction(run, vcpu);
215 switch (er) {
216 case EMULATE_DONE:
217 /* Future optimization: only reload non-volatiles if they were
218 * actually modified. */
219 r = RESUME_GUEST_NV;
220 break;
221 case EMULATE_DO_MMIO:
222 run->exit_reason = KVM_EXIT_MMIO;
223 /* We must reload nonvolatiles because "update" load/store
224 * instructions modify register state. */
225 /* Future optimization: only reload non-volatiles if they were
226 * actually modified. */
227 r = RESUME_HOST_NV;
228 break;
229 case EMULATE_FAIL:
230 /* XXX Deliver Program interrupt to guest. */
231 printk(KERN_EMERG "%s: emulation failed (%08x)\n", __func__,
Alexander Grafc7f38f42010-04-16 00:11:40 +0200232 kvmppc_get_last_inst(vcpu));
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500233 r = RESUME_HOST;
234 break;
235 default:
236 BUG();
237 }
238
239 return r;
240}
241
Alexander Graf10474ae2009-09-15 11:37:46 +0200242int kvm_arch_hardware_enable(void *garbage)
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500243{
Alexander Graf10474ae2009-09-15 11:37:46 +0200244 return 0;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500245}
246
247void kvm_arch_hardware_disable(void *garbage)
248{
249}
250
251int kvm_arch_hardware_setup(void)
252{
253 return 0;
254}
255
256void kvm_arch_hardware_unsetup(void)
257{
258}
259
260void kvm_arch_check_processor_compat(void *rtn)
261{
Hollis Blanchard9dd921c2008-11-05 09:36:14 -0600262 *(int *)rtn = kvmppc_core_check_processor_compat();
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500263}
264
Carsten Ottee08b9632012-01-04 10:25:20 +0100265int kvm_arch_init_vm(struct kvm *kvm, unsigned long type)
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500266{
Carsten Ottee08b9632012-01-04 10:25:20 +0100267 if (type)
268 return -EINVAL;
269
Paul Mackerrasf9e05542011-06-29 00:19:22 +0000270 return kvmppc_core_init_vm(kvm);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500271}
272
Jan Kiszkad89f5ef2010-11-09 17:02:49 +0100273void kvm_arch_destroy_vm(struct kvm *kvm)
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500274{
275 unsigned int i;
Gleb Natapov988a2ca2009-06-09 15:56:29 +0300276 struct kvm_vcpu *vcpu;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500277
Gleb Natapov988a2ca2009-06-09 15:56:29 +0300278 kvm_for_each_vcpu(i, vcpu, kvm)
279 kvm_arch_vcpu_free(vcpu);
280
281 mutex_lock(&kvm->lock);
282 for (i = 0; i < atomic_read(&kvm->online_vcpus); i++)
283 kvm->vcpus[i] = NULL;
284
285 atomic_set(&kvm->online_vcpus, 0);
Paul Mackerrasf9e05542011-06-29 00:19:22 +0000286
287 kvmppc_core_destroy_vm(kvm);
288
Gleb Natapov988a2ca2009-06-09 15:56:29 +0300289 mutex_unlock(&kvm->lock);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500290}
291
Sheng Yangad8ba2c2009-01-06 10:03:02 +0800292void kvm_arch_sync_events(struct kvm *kvm)
293{
294}
295
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500296int kvm_dev_ioctl_check_extension(long ext)
297{
298 int r;
299
300 switch (ext) {
Scott Wood5ce941e2011-04-27 17:24:21 -0500301#ifdef CONFIG_BOOKE
302 case KVM_CAP_PPC_BOOKE_SREGS:
Bharat Bhushanf61c94b2012-08-08 20:38:19 +0000303 case KVM_CAP_PPC_BOOKE_WATCHDOG:
Scott Wood5ce941e2011-04-27 17:24:21 -0500304#else
Alexander Grafe15a1132009-11-30 03:02:02 +0000305 case KVM_CAP_PPC_SEGSTATE:
Alexander Graf1022fc32011-09-14 21:45:23 +0200306 case KVM_CAP_PPC_HIOR:
Alexander Graf930b4122011-08-08 17:29:42 +0200307 case KVM_CAP_PPC_PAPR:
Scott Wood5ce941e2011-04-27 17:24:21 -0500308#endif
Alexander Graf18978762010-03-24 21:48:18 +0100309 case KVM_CAP_PPC_UNSET_IRQ:
Alexander Graf7b4203e2010-08-30 13:50:45 +0200310 case KVM_CAP_PPC_IRQ_LEVEL:
Alexander Graf71fbfd52010-03-24 21:48:29 +0100311 case KVM_CAP_ENABLE_CAP:
Alexander Grafe24ed812011-09-14 10:02:41 +0200312 case KVM_CAP_ONE_REG:
Paul Mackerrasde56a942011-06-29 00:21:34 +0000313 r = 1;
314 break;
315#ifndef CONFIG_KVM_BOOK3S_64_HV
316 case KVM_CAP_PPC_PAIRED_SINGLES:
Alexander Grafad0a0482010-03-24 21:48:30 +0100317 case KVM_CAP_PPC_OSI:
Alexander Graf15711e92010-07-29 14:48:08 +0200318 case KVM_CAP_PPC_GET_PVINFO:
Alexander Grafbf7ca4b2012-02-15 23:40:00 +0000319#if defined(CONFIG_KVM_E500V2) || defined(CONFIG_KVM_E500MC)
Scott Wooddc83b8b2011-08-18 15:25:21 -0500320 case KVM_CAP_SW_TLB:
321#endif
Alexander Grafe15a1132009-11-30 03:02:02 +0000322 r = 1;
323 break;
Laurent Vivier588968b2008-05-30 16:05:56 +0200324 case KVM_CAP_COALESCED_MMIO:
325 r = KVM_COALESCED_MMIO_PAGE_OFFSET;
326 break;
Paul Mackerrasde56a942011-06-29 00:21:34 +0000327#endif
Benjamin Herrenschmidtf31e65e2012-03-15 21:58:34 +0000328#ifdef CONFIG_PPC_BOOK3S_64
David Gibson54738c02011-06-29 00:22:41 +0000329 case KVM_CAP_SPAPR_TCE:
Paul Mackerras32fad282012-05-04 02:32:53 +0000330 case KVM_CAP_PPC_ALLOC_HTAB:
David Gibson54738c02011-06-29 00:22:41 +0000331 r = 1;
332 break;
Benjamin Herrenschmidtf31e65e2012-03-15 21:58:34 +0000333#endif /* CONFIG_PPC_BOOK3S_64 */
334#ifdef CONFIG_KVM_BOOK3S_64_HV
Paul Mackerras371fefd2011-06-29 00:23:08 +0000335 case KVM_CAP_PPC_SMT:
336 r = threads_per_core;
337 break;
Paul Mackerrasaa04b4c2011-06-29 00:25:44 +0000338 case KVM_CAP_PPC_RMA:
339 r = 1;
Paul Mackerras9e368f22011-06-29 00:40:08 +0000340 /* PPC970 requires an RMA */
341 if (cpu_has_feature(CPU_FTR_ARCH_201))
342 r = 2;
Paul Mackerrasaa04b4c2011-06-29 00:25:44 +0000343 break;
David Gibson54738c02011-06-29 00:22:41 +0000344#endif
Alexander Graff4800b12012-08-07 10:24:14 +0200345 case KVM_CAP_SYNC_MMU:
346#ifdef CONFIG_KVM_BOOK3S_64_HV
347 r = cpu_has_feature(CPU_FTR_ARCH_206) ? 1 : 0;
348#elif defined(KVM_ARCH_WANT_MMU_NOTIFIER)
349 r = 1;
350#else
351 r = 0;
352#endif
353 break;
Matt Evansb5434032011-12-07 16:55:57 +0000354 case KVM_CAP_NR_VCPUS:
355 /*
356 * Recommending a number of CPUs is somewhat arbitrary; we
357 * return the number of present CPUs for -HV (since a host
358 * will have secondary threads "offline"), and for other KVM
359 * implementations just count online CPUs.
360 */
361#ifdef CONFIG_KVM_BOOK3S_64_HV
362 r = num_present_cpus();
363#else
364 r = num_online_cpus();
365#endif
366 break;
367 case KVM_CAP_MAX_VCPUS:
368 r = KVM_MAX_VCPUS;
369 break;
Benjamin Herrenschmidt5b747162012-04-26 19:43:42 +0000370#ifdef CONFIG_PPC_BOOK3S_64
371 case KVM_CAP_PPC_GET_SMMU_INFO:
372 r = 1;
373 break;
374#endif
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500375 default:
376 r = 0;
377 break;
378 }
379 return r;
380
381}
382
383long kvm_arch_dev_ioctl(struct file *filp,
384 unsigned int ioctl, unsigned long arg)
385{
386 return -EINVAL;
387}
388
Takuya Yoshikawadb3fe4e2012-02-08 13:02:18 +0900389void kvm_arch_free_memslot(struct kvm_memory_slot *free,
390 struct kvm_memory_slot *dont)
391{
Paul Mackerrasa66b48c2012-09-11 13:27:46 +0000392 kvmppc_core_free_memslot(free, dont);
Takuya Yoshikawadb3fe4e2012-02-08 13:02:18 +0900393}
394
395int kvm_arch_create_memslot(struct kvm_memory_slot *slot, unsigned long npages)
396{
Paul Mackerrasa66b48c2012-09-11 13:27:46 +0000397 return kvmppc_core_create_memslot(slot, npages);
Takuya Yoshikawadb3fe4e2012-02-08 13:02:18 +0900398}
399
Marcelo Tosattif7784b82009-12-23 14:35:18 -0200400int kvm_arch_prepare_memory_region(struct kvm *kvm,
401 struct kvm_memory_slot *memslot,
402 struct kvm_memory_slot old,
403 struct kvm_userspace_memory_region *mem,
404 int user_alloc)
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500405{
Paul Mackerrasa66b48c2012-09-11 13:27:46 +0000406 return kvmppc_core_prepare_memory_region(kvm, memslot, mem);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500407}
408
Marcelo Tosattif7784b82009-12-23 14:35:18 -0200409void kvm_arch_commit_memory_region(struct kvm *kvm,
410 struct kvm_userspace_memory_region *mem,
411 struct kvm_memory_slot old,
412 int user_alloc)
413{
Paul Mackerrasdfe49db2012-09-11 13:28:18 +0000414 kvmppc_core_commit_memory_region(kvm, mem, old);
Marcelo Tosattif7784b82009-12-23 14:35:18 -0200415}
416
Marcelo Tosatti2df72e92012-08-24 15:54:57 -0300417void kvm_arch_flush_shadow_all(struct kvm *kvm)
418{
419}
Marcelo Tosattif7784b82009-12-23 14:35:18 -0200420
Marcelo Tosatti2df72e92012-08-24 15:54:57 -0300421void kvm_arch_flush_shadow_memslot(struct kvm *kvm,
422 struct kvm_memory_slot *slot)
Marcelo Tosatti34d4cb82008-07-10 20:49:31 -0300423{
Paul Mackerrasdfe49db2012-09-11 13:28:18 +0000424 kvmppc_core_flush_memslot(kvm, slot);
Marcelo Tosatti34d4cb82008-07-10 20:49:31 -0300425}
426
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500427struct kvm_vcpu *kvm_arch_vcpu_create(struct kvm *kvm, unsigned int id)
428{
Hollis Blanchard73e75b42008-12-02 15:51:57 -0600429 struct kvm_vcpu *vcpu;
430 vcpu = kvmppc_core_vcpu_create(kvm, id);
Matt Evans03cdab52011-12-06 21:19:42 +0000431 if (!IS_ERR(vcpu)) {
432 vcpu->arch.wqp = &vcpu->wq;
Wei Yongjun06056bf2010-03-09 14:13:43 +0800433 kvmppc_create_vcpu_debugfs(vcpu, id);
Matt Evans03cdab52011-12-06 21:19:42 +0000434 }
Hollis Blanchard73e75b42008-12-02 15:51:57 -0600435 return vcpu;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500436}
437
438void kvm_arch_vcpu_free(struct kvm_vcpu *vcpu)
439{
Alexander Grafa5954052010-02-22 16:52:14 +0100440 /* Make sure we're not using the vcpu anymore */
441 hrtimer_cancel(&vcpu->arch.dec_timer);
442 tasklet_kill(&vcpu->arch.tasklet);
443
Hollis Blanchard73e75b42008-12-02 15:51:57 -0600444 kvmppc_remove_vcpu_debugfs(vcpu);
Hollis Blancharddb93f572008-11-05 09:36:18 -0600445 kvmppc_core_vcpu_free(vcpu);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500446}
447
448void kvm_arch_vcpu_destroy(struct kvm_vcpu *vcpu)
449{
450 kvm_arch_vcpu_free(vcpu);
451}
452
453int kvm_cpu_has_pending_timer(struct kvm_vcpu *vcpu)
454{
Hollis Blanchard9dd921c2008-11-05 09:36:14 -0600455 return kvmppc_core_pending_dec(vcpu);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500456}
457
Alexander Graf544c6762009-11-02 12:02:31 +0000458/*
459 * low level hrtimer wake routine. Because this runs in hardirq context
460 * we schedule a tasklet to do the real work.
461 */
462enum hrtimer_restart kvmppc_decrementer_wakeup(struct hrtimer *timer)
463{
464 struct kvm_vcpu *vcpu;
465
466 vcpu = container_of(timer, struct kvm_vcpu, arch.dec_timer);
467 tasklet_schedule(&vcpu->arch.tasklet);
468
469 return HRTIMER_NORESTART;
470}
471
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500472int kvm_arch_vcpu_init(struct kvm_vcpu *vcpu)
473{
Bharat Bhushanf61c94b2012-08-08 20:38:19 +0000474 int ret;
475
Alexander Graf544c6762009-11-02 12:02:31 +0000476 hrtimer_init(&vcpu->arch.dec_timer, CLOCK_REALTIME, HRTIMER_MODE_ABS);
477 tasklet_init(&vcpu->arch.tasklet, kvmppc_decrementer_func, (ulong)vcpu);
478 vcpu->arch.dec_timer.function = kvmppc_decrementer_wakeup;
Paul Mackerrasde56a942011-06-29 00:21:34 +0000479 vcpu->arch.dec_expires = ~(u64)0;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500480
Bharat Bhushan09000ad2011-03-25 10:32:13 +0530481#ifdef CONFIG_KVM_EXIT_TIMING
482 mutex_init(&vcpu->arch.exit_timing_lock);
483#endif
Bharat Bhushanf61c94b2012-08-08 20:38:19 +0000484 ret = kvmppc_subarch_vcpu_init(vcpu);
485 return ret;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500486}
487
488void kvm_arch_vcpu_uninit(struct kvm_vcpu *vcpu)
489{
Hollis Blanchardecc09812009-01-03 16:22:59 -0600490 kvmppc_mmu_destroy(vcpu);
Bharat Bhushanf61c94b2012-08-08 20:38:19 +0000491 kvmppc_subarch_vcpu_uninit(vcpu);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500492}
493
494void kvm_arch_vcpu_load(struct kvm_vcpu *vcpu, int cpu)
495{
Scott Woodeab17672011-04-27 17:24:10 -0500496#ifdef CONFIG_BOOKE
497 /*
498 * vrsave (formerly usprg0) isn't used by Linux, but may
499 * be used by the guest.
500 *
501 * On non-booke this is associated with Altivec and
502 * is handled by code in book3s.c.
503 */
504 mtspr(SPRN_VRSAVE, vcpu->arch.vrsave);
505#endif
Hollis Blanchard9dd921c2008-11-05 09:36:14 -0600506 kvmppc_core_vcpu_load(vcpu, cpu);
Paul Mackerrasde56a942011-06-29 00:21:34 +0000507 vcpu->cpu = smp_processor_id();
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500508}
509
510void kvm_arch_vcpu_put(struct kvm_vcpu *vcpu)
511{
Hollis Blanchard9dd921c2008-11-05 09:36:14 -0600512 kvmppc_core_vcpu_put(vcpu);
Scott Woodeab17672011-04-27 17:24:10 -0500513#ifdef CONFIG_BOOKE
514 vcpu->arch.vrsave = mfspr(SPRN_VRSAVE);
515#endif
Paul Mackerrasde56a942011-06-29 00:21:34 +0000516 vcpu->cpu = -1;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500517}
518
Jan Kiszkad0bfb942008-12-15 13:52:10 +0100519int kvm_arch_vcpu_ioctl_set_guest_debug(struct kvm_vcpu *vcpu,
Hollis Blanchardf5d09062009-01-04 13:51:09 -0600520 struct kvm_guest_debug *dbg)
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500521{
Hollis Blanchardf5d09062009-01-04 13:51:09 -0600522 return -EINVAL;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500523}
524
525static void kvmppc_complete_dcr_load(struct kvm_vcpu *vcpu,
526 struct kvm_run *run)
527{
Alexander Graf8e5b26b2010-01-08 02:58:01 +0100528 kvmppc_set_gpr(vcpu, vcpu->arch.io_gpr, run->dcr.data);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500529}
530
531static void kvmppc_complete_mmio_load(struct kvm_vcpu *vcpu,
532 struct kvm_run *run)
533{
Denis Kirjanov69b61832010-06-11 11:23:26 +0000534 u64 uninitialized_var(gpr);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500535
Alexander Graf8e5b26b2010-01-08 02:58:01 +0100536 if (run->mmio.len > sizeof(gpr)) {
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500537 printk(KERN_ERR "bad MMIO length: %d\n", run->mmio.len);
538 return;
539 }
540
541 if (vcpu->arch.mmio_is_bigendian) {
542 switch (run->mmio.len) {
Alexander Grafb104d062010-02-19 11:00:29 +0100543 case 8: gpr = *(u64 *)run->mmio.data; break;
Alexander Graf8e5b26b2010-01-08 02:58:01 +0100544 case 4: gpr = *(u32 *)run->mmio.data; break;
545 case 2: gpr = *(u16 *)run->mmio.data; break;
546 case 1: gpr = *(u8 *)run->mmio.data; break;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500547 }
548 } else {
549 /* Convert BE data from userland back to LE. */
550 switch (run->mmio.len) {
Alexander Graf8e5b26b2010-01-08 02:58:01 +0100551 case 4: gpr = ld_le32((u32 *)run->mmio.data); break;
552 case 2: gpr = ld_le16((u16 *)run->mmio.data); break;
553 case 1: gpr = *(u8 *)run->mmio.data; break;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500554 }
555 }
Alexander Graf8e5b26b2010-01-08 02:58:01 +0100556
Alexander Graf3587d532010-02-19 11:00:30 +0100557 if (vcpu->arch.mmio_sign_extend) {
558 switch (run->mmio.len) {
559#ifdef CONFIG_PPC64
560 case 4:
561 gpr = (s64)(s32)gpr;
562 break;
563#endif
564 case 2:
565 gpr = (s64)(s16)gpr;
566 break;
567 case 1:
568 gpr = (s64)(s8)gpr;
569 break;
570 }
571 }
572
Alexander Graf8e5b26b2010-01-08 02:58:01 +0100573 kvmppc_set_gpr(vcpu, vcpu->arch.io_gpr, gpr);
Alexander Grafb104d062010-02-19 11:00:29 +0100574
Alexander Grafb3c5d3c2012-01-07 02:07:38 +0100575 switch (vcpu->arch.io_gpr & KVM_MMIO_REG_EXT_MASK) {
576 case KVM_MMIO_REG_GPR:
Alexander Grafb104d062010-02-19 11:00:29 +0100577 kvmppc_set_gpr(vcpu, vcpu->arch.io_gpr, gpr);
578 break;
Alexander Grafb3c5d3c2012-01-07 02:07:38 +0100579 case KVM_MMIO_REG_FPR:
580 vcpu->arch.fpr[vcpu->arch.io_gpr & KVM_MMIO_REG_MASK] = gpr;
Alexander Grafb104d062010-02-19 11:00:29 +0100581 break;
Alexander Graf287d5612010-04-01 15:33:21 +0200582#ifdef CONFIG_PPC_BOOK3S
Alexander Grafb3c5d3c2012-01-07 02:07:38 +0100583 case KVM_MMIO_REG_QPR:
584 vcpu->arch.qpr[vcpu->arch.io_gpr & KVM_MMIO_REG_MASK] = gpr;
Alexander Grafb104d062010-02-19 11:00:29 +0100585 break;
Alexander Grafb3c5d3c2012-01-07 02:07:38 +0100586 case KVM_MMIO_REG_FQPR:
587 vcpu->arch.fpr[vcpu->arch.io_gpr & KVM_MMIO_REG_MASK] = gpr;
588 vcpu->arch.qpr[vcpu->arch.io_gpr & KVM_MMIO_REG_MASK] = gpr;
Alexander Grafb104d062010-02-19 11:00:29 +0100589 break;
Alexander Graf287d5612010-04-01 15:33:21 +0200590#endif
Alexander Grafb104d062010-02-19 11:00:29 +0100591 default:
592 BUG();
593 }
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500594}
595
596int kvmppc_handle_load(struct kvm_run *run, struct kvm_vcpu *vcpu,
597 unsigned int rt, unsigned int bytes, int is_bigendian)
598{
599 if (bytes > sizeof(run->mmio.data)) {
600 printk(KERN_ERR "%s: bad MMIO length: %d\n", __func__,
601 run->mmio.len);
602 }
603
604 run->mmio.phys_addr = vcpu->arch.paddr_accessed;
605 run->mmio.len = bytes;
606 run->mmio.is_write = 0;
607
608 vcpu->arch.io_gpr = rt;
609 vcpu->arch.mmio_is_bigendian = is_bigendian;
610 vcpu->mmio_needed = 1;
611 vcpu->mmio_is_write = 0;
Alexander Graf3587d532010-02-19 11:00:30 +0100612 vcpu->arch.mmio_sign_extend = 0;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500613
614 return EMULATE_DO_MMIO;
615}
616
Alexander Graf3587d532010-02-19 11:00:30 +0100617/* Same as above, but sign extends */
618int kvmppc_handle_loads(struct kvm_run *run, struct kvm_vcpu *vcpu,
619 unsigned int rt, unsigned int bytes, int is_bigendian)
620{
621 int r;
622
623 r = kvmppc_handle_load(run, vcpu, rt, bytes, is_bigendian);
624 vcpu->arch.mmio_sign_extend = 1;
625
626 return r;
627}
628
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500629int kvmppc_handle_store(struct kvm_run *run, struct kvm_vcpu *vcpu,
Alexander Grafb104d062010-02-19 11:00:29 +0100630 u64 val, unsigned int bytes, int is_bigendian)
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500631{
632 void *data = run->mmio.data;
633
634 if (bytes > sizeof(run->mmio.data)) {
635 printk(KERN_ERR "%s: bad MMIO length: %d\n", __func__,
636 run->mmio.len);
637 }
638
639 run->mmio.phys_addr = vcpu->arch.paddr_accessed;
640 run->mmio.len = bytes;
641 run->mmio.is_write = 1;
642 vcpu->mmio_needed = 1;
643 vcpu->mmio_is_write = 1;
644
645 /* Store the value at the lowest bytes in 'data'. */
646 if (is_bigendian) {
647 switch (bytes) {
Alexander Grafb104d062010-02-19 11:00:29 +0100648 case 8: *(u64 *)data = val; break;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500649 case 4: *(u32 *)data = val; break;
650 case 2: *(u16 *)data = val; break;
651 case 1: *(u8 *)data = val; break;
652 }
653 } else {
654 /* Store LE value into 'data'. */
655 switch (bytes) {
656 case 4: st_le32(data, val); break;
657 case 2: st_le16(data, val); break;
658 case 1: *(u8 *)data = val; break;
659 }
660 }
661
662 return EMULATE_DO_MMIO;
663}
664
665int kvm_arch_vcpu_ioctl_run(struct kvm_vcpu *vcpu, struct kvm_run *run)
666{
667 int r;
668 sigset_t sigsaved;
669
670 if (vcpu->sigset_active)
671 sigprocmask(SIG_SETMASK, &vcpu->sigset, &sigsaved);
672
673 if (vcpu->mmio_needed) {
674 if (!vcpu->mmio_is_write)
675 kvmppc_complete_mmio_load(vcpu, run);
676 vcpu->mmio_needed = 0;
677 } else if (vcpu->arch.dcr_needed) {
678 if (!vcpu->arch.dcr_is_write)
679 kvmppc_complete_dcr_load(vcpu, run);
680 vcpu->arch.dcr_needed = 0;
Alexander Grafad0a0482010-03-24 21:48:30 +0100681 } else if (vcpu->arch.osi_needed) {
682 u64 *gprs = run->osi.gprs;
683 int i;
684
685 for (i = 0; i < 32; i++)
686 kvmppc_set_gpr(vcpu, i, gprs[i]);
687 vcpu->arch.osi_needed = 0;
Paul Mackerrasde56a942011-06-29 00:21:34 +0000688 } else if (vcpu->arch.hcall_needed) {
689 int i;
690
691 kvmppc_set_gpr(vcpu, 3, run->papr_hcall.ret);
692 for (i = 0; i < 9; ++i)
693 kvmppc_set_gpr(vcpu, 4 + i, run->papr_hcall.args[i]);
694 vcpu->arch.hcall_needed = 0;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500695 }
696
Paul Mackerrasdf6909e52011-06-29 00:19:50 +0000697 r = kvmppc_vcpu_run(run, vcpu);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500698
699 if (vcpu->sigset_active)
700 sigprocmask(SIG_SETMASK, &sigsaved, NULL);
701
702 return r;
703}
704
705int kvm_vcpu_ioctl_interrupt(struct kvm_vcpu *vcpu, struct kvm_interrupt *irq)
706{
Paul Mackerras19ccb762011-07-23 17:42:46 +1000707 if (irq->irq == KVM_INTERRUPT_UNSET) {
Alexander Graf18978762010-03-24 21:48:18 +0100708 kvmppc_core_dequeue_external(vcpu, irq);
Paul Mackerras19ccb762011-07-23 17:42:46 +1000709 return 0;
710 }
Hollis Blanchard45c5eb62008-04-25 17:55:49 -0500711
Paul Mackerras19ccb762011-07-23 17:42:46 +1000712 kvmppc_core_queue_external(vcpu, irq);
Christoffer Dallb6d33832012-03-08 16:44:24 -0500713
Scott Wooddfd4d472011-11-17 12:39:59 +0000714 kvm_vcpu_kick(vcpu);
Hollis Blanchard45c5eb62008-04-25 17:55:49 -0500715
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500716 return 0;
717}
718
Alexander Graf71fbfd52010-03-24 21:48:29 +0100719static int kvm_vcpu_ioctl_enable_cap(struct kvm_vcpu *vcpu,
720 struct kvm_enable_cap *cap)
721{
722 int r;
723
724 if (cap->flags)
725 return -EINVAL;
726
727 switch (cap->cap) {
Alexander Grafad0a0482010-03-24 21:48:30 +0100728 case KVM_CAP_PPC_OSI:
729 r = 0;
730 vcpu->arch.osi_enabled = true;
731 break;
Alexander Graf930b4122011-08-08 17:29:42 +0200732 case KVM_CAP_PPC_PAPR:
733 r = 0;
734 vcpu->arch.papr_enabled = true;
735 break;
Bharat Bhushanf61c94b2012-08-08 20:38:19 +0000736#ifdef CONFIG_BOOKE
737 case KVM_CAP_PPC_BOOKE_WATCHDOG:
738 r = 0;
739 vcpu->arch.watchdog_enabled = true;
740 break;
741#endif
Alexander Grafbf7ca4b2012-02-15 23:40:00 +0000742#if defined(CONFIG_KVM_E500V2) || defined(CONFIG_KVM_E500MC)
Scott Wooddc83b8b2011-08-18 15:25:21 -0500743 case KVM_CAP_SW_TLB: {
744 struct kvm_config_tlb cfg;
745 void __user *user_ptr = (void __user *)(uintptr_t)cap->args[0];
746
747 r = -EFAULT;
748 if (copy_from_user(&cfg, user_ptr, sizeof(cfg)))
749 break;
750
751 r = kvm_vcpu_ioctl_config_tlb(vcpu, &cfg);
752 break;
753 }
754#endif
Alexander Graf71fbfd52010-03-24 21:48:29 +0100755 default:
756 r = -EINVAL;
757 break;
758 }
759
Alexander Grafaf8f38b2011-08-10 13:57:08 +0200760 if (!r)
761 r = kvmppc_sanity_check(vcpu);
762
Alexander Graf71fbfd52010-03-24 21:48:29 +0100763 return r;
764}
765
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500766int kvm_arch_vcpu_ioctl_get_mpstate(struct kvm_vcpu *vcpu,
767 struct kvm_mp_state *mp_state)
768{
769 return -EINVAL;
770}
771
772int kvm_arch_vcpu_ioctl_set_mpstate(struct kvm_vcpu *vcpu,
773 struct kvm_mp_state *mp_state)
774{
775 return -EINVAL;
776}
777
778long kvm_arch_vcpu_ioctl(struct file *filp,
779 unsigned int ioctl, unsigned long arg)
780{
781 struct kvm_vcpu *vcpu = filp->private_data;
782 void __user *argp = (void __user *)arg;
783 long r;
784
Avi Kivity93736622010-05-13 12:35:17 +0300785 switch (ioctl) {
786 case KVM_INTERRUPT: {
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500787 struct kvm_interrupt irq;
788 r = -EFAULT;
789 if (copy_from_user(&irq, argp, sizeof(irq)))
Avi Kivity93736622010-05-13 12:35:17 +0300790 goto out;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500791 r = kvm_vcpu_ioctl_interrupt(vcpu, &irq);
Avi Kivity93736622010-05-13 12:35:17 +0300792 goto out;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500793 }
Avi Kivity19483d12010-05-13 12:30:43 +0300794
Alexander Graf71fbfd52010-03-24 21:48:29 +0100795 case KVM_ENABLE_CAP:
796 {
797 struct kvm_enable_cap cap;
798 r = -EFAULT;
799 if (copy_from_user(&cap, argp, sizeof(cap)))
800 goto out;
801 r = kvm_vcpu_ioctl_enable_cap(vcpu, &cap);
802 break;
803 }
Scott Wooddc83b8b2011-08-18 15:25:21 -0500804
Alexander Grafe24ed812011-09-14 10:02:41 +0200805 case KVM_SET_ONE_REG:
806 case KVM_GET_ONE_REG:
807 {
808 struct kvm_one_reg reg;
809 r = -EFAULT;
810 if (copy_from_user(&reg, argp, sizeof(reg)))
811 goto out;
812 if (ioctl == KVM_SET_ONE_REG)
813 r = kvm_vcpu_ioctl_set_one_reg(vcpu, &reg);
814 else
815 r = kvm_vcpu_ioctl_get_one_reg(vcpu, &reg);
816 break;
817 }
818
Alexander Grafbf7ca4b2012-02-15 23:40:00 +0000819#if defined(CONFIG_KVM_E500V2) || defined(CONFIG_KVM_E500MC)
Scott Wooddc83b8b2011-08-18 15:25:21 -0500820 case KVM_DIRTY_TLB: {
821 struct kvm_dirty_tlb dirty;
822 r = -EFAULT;
823 if (copy_from_user(&dirty, argp, sizeof(dirty)))
824 goto out;
825 r = kvm_vcpu_ioctl_dirty_tlb(vcpu, &dirty);
826 break;
827 }
828#endif
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500829 default:
830 r = -EINVAL;
831 }
832
833out:
834 return r;
835}
836
Carsten Otte5b1c1492012-01-04 10:25:23 +0100837int kvm_arch_vcpu_fault(struct kvm_vcpu *vcpu, struct vm_fault *vmf)
838{
839 return VM_FAULT_SIGBUS;
840}
841
Alexander Graf15711e92010-07-29 14:48:08 +0200842static int kvm_vm_ioctl_get_pvinfo(struct kvm_ppc_pvinfo *pvinfo)
843{
Stuart Yoder784bafa2012-07-03 05:48:51 +0000844 u32 inst_nop = 0x60000000;
845#ifdef CONFIG_KVM_BOOKE_HV
846 u32 inst_sc1 = 0x44000022;
847 pvinfo->hcall[0] = inst_sc1;
848 pvinfo->hcall[1] = inst_nop;
849 pvinfo->hcall[2] = inst_nop;
850 pvinfo->hcall[3] = inst_nop;
851#else
Alexander Graf15711e92010-07-29 14:48:08 +0200852 u32 inst_lis = 0x3c000000;
853 u32 inst_ori = 0x60000000;
Alexander Graf15711e92010-07-29 14:48:08 +0200854 u32 inst_sc = 0x44000002;
855 u32 inst_imm_mask = 0xffff;
856
857 /*
858 * The hypercall to get into KVM from within guest context is as
859 * follows:
860 *
861 * lis r0, r0, KVM_SC_MAGIC_R0@h
862 * ori r0, KVM_SC_MAGIC_R0@l
863 * sc
864 * nop
865 */
866 pvinfo->hcall[0] = inst_lis | ((KVM_SC_MAGIC_R0 >> 16) & inst_imm_mask);
867 pvinfo->hcall[1] = inst_ori | (KVM_SC_MAGIC_R0 & inst_imm_mask);
868 pvinfo->hcall[2] = inst_sc;
869 pvinfo->hcall[3] = inst_nop;
Stuart Yoder784bafa2012-07-03 05:48:51 +0000870#endif
Alexander Graf15711e92010-07-29 14:48:08 +0200871
Liu Yu-B132019202e072012-07-03 05:48:52 +0000872 pvinfo->flags = KVM_PPC_PVINFO_FLAGS_EV_IDLE;
873
Alexander Graf15711e92010-07-29 14:48:08 +0200874 return 0;
875}
876
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500877long kvm_arch_vm_ioctl(struct file *filp,
878 unsigned int ioctl, unsigned long arg)
879{
Alexander Graf15711e92010-07-29 14:48:08 +0200880 void __user *argp = (void __user *)arg;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500881 long r;
882
883 switch (ioctl) {
Alexander Graf15711e92010-07-29 14:48:08 +0200884 case KVM_PPC_GET_PVINFO: {
885 struct kvm_ppc_pvinfo pvinfo;
Vasiliy Kulikovd8cdddc2010-10-30 13:04:24 +0400886 memset(&pvinfo, 0, sizeof(pvinfo));
Alexander Graf15711e92010-07-29 14:48:08 +0200887 r = kvm_vm_ioctl_get_pvinfo(&pvinfo);
888 if (copy_to_user(argp, &pvinfo, sizeof(pvinfo))) {
889 r = -EFAULT;
890 goto out;
891 }
892
893 break;
894 }
Benjamin Herrenschmidtf31e65e2012-03-15 21:58:34 +0000895#ifdef CONFIG_PPC_BOOK3S_64
David Gibson54738c02011-06-29 00:22:41 +0000896 case KVM_CREATE_SPAPR_TCE: {
897 struct kvm_create_spapr_tce create_tce;
898 struct kvm *kvm = filp->private_data;
899
900 r = -EFAULT;
901 if (copy_from_user(&create_tce, argp, sizeof(create_tce)))
902 goto out;
903 r = kvm_vm_ioctl_create_spapr_tce(kvm, &create_tce);
904 goto out;
905 }
Benjamin Herrenschmidtf31e65e2012-03-15 21:58:34 +0000906#endif /* CONFIG_PPC_BOOK3S_64 */
Paul Mackerrasaa04b4c2011-06-29 00:25:44 +0000907
Benjamin Herrenschmidtf31e65e2012-03-15 21:58:34 +0000908#ifdef CONFIG_KVM_BOOK3S_64_HV
Paul Mackerrasaa04b4c2011-06-29 00:25:44 +0000909 case KVM_ALLOCATE_RMA: {
910 struct kvm *kvm = filp->private_data;
911 struct kvm_allocate_rma rma;
912
913 r = kvm_vm_ioctl_allocate_rma(kvm, &rma);
914 if (r >= 0 && copy_to_user(argp, &rma, sizeof(rma)))
915 r = -EFAULT;
916 break;
917 }
Paul Mackerras32fad282012-05-04 02:32:53 +0000918
919 case KVM_PPC_ALLOCATE_HTAB: {
920 struct kvm *kvm = filp->private_data;
921 u32 htab_order;
922
923 r = -EFAULT;
924 if (get_user(htab_order, (u32 __user *)argp))
925 break;
926 r = kvmppc_alloc_reset_hpt(kvm, &htab_order);
927 if (r)
928 break;
929 r = -EFAULT;
930 if (put_user(htab_order, (u32 __user *)argp))
931 break;
932 r = 0;
933 break;
934 }
David Gibson54738c02011-06-29 00:22:41 +0000935#endif /* CONFIG_KVM_BOOK3S_64_HV */
936
Benjamin Herrenschmidt5b747162012-04-26 19:43:42 +0000937#ifdef CONFIG_PPC_BOOK3S_64
938 case KVM_PPC_GET_SMMU_INFO: {
939 struct kvm *kvm = filp->private_data;
940 struct kvm_ppc_smmu_info info;
941
942 memset(&info, 0, sizeof(info));
943 r = kvm_vm_ioctl_get_smmu_info(kvm, &info);
944 if (r >= 0 && copy_to_user(argp, &info, sizeof(info)))
945 r = -EFAULT;
946 break;
947 }
948#endif /* CONFIG_PPC_BOOK3S_64 */
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500949 default:
Avi Kivity367e1312009-08-26 14:57:07 +0300950 r = -ENOTTY;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500951 }
952
Alexander Graf15711e92010-07-29 14:48:08 +0200953out:
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500954 return r;
955}
956
Scott Wood043cc4d2011-12-20 15:34:20 +0000957static unsigned long lpid_inuse[BITS_TO_LONGS(KVMPPC_NR_LPIDS)];
958static unsigned long nr_lpids;
959
960long kvmppc_alloc_lpid(void)
961{
962 long lpid;
963
964 do {
965 lpid = find_first_zero_bit(lpid_inuse, KVMPPC_NR_LPIDS);
966 if (lpid >= nr_lpids) {
967 pr_err("%s: No LPIDs free\n", __func__);
968 return -ENOMEM;
969 }
970 } while (test_and_set_bit(lpid, lpid_inuse));
971
972 return lpid;
973}
974
975void kvmppc_claim_lpid(long lpid)
976{
977 set_bit(lpid, lpid_inuse);
978}
979
980void kvmppc_free_lpid(long lpid)
981{
982 clear_bit(lpid, lpid_inuse);
983}
984
985void kvmppc_init_lpid(unsigned long nr_lpids_param)
986{
987 nr_lpids = min_t(unsigned long, KVMPPC_NR_LPIDS, nr_lpids_param);
988 memset(lpid_inuse, 0, sizeof(lpid_inuse));
989}
990
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500991int kvm_arch_init(void *opaque)
992{
993 return 0;
994}
995
996void kvm_arch_exit(void)
997{
998}