• Home
  • History
  • Annotate
  • Line#
  • Navigate
  • Raw
  • Download
  • only in /asuswrt-rt-n18u-9.0.0.4.380.2695/release/src-rt-6.x.4708/linux/linux-2.6/arch/ia64/kvm/
1/*
2 * PAL/SAL call delegation
3 *
4 * Copyright (c) 2004 Li Susie <susie.li@intel.com>
5 * Copyright (c) 2005 Yu Ke <ke.yu@intel.com>
6 * Copyright (c) 2007 Xiantao Zhang <xiantao.zhang@intel.com>
7 *
8 * This program is free software; you can redistribute it and/or modify it
9 * under the terms and conditions of the GNU General Public License,
10 * version 2, as published by the Free Software Foundation.
11 *
12 * This program is distributed in the hope it will be useful, but WITHOUT
13 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
15 * more details.
16 *
17 * You should have received a copy of the GNU General Public License along with
18 * this program; if not, write to the Free Software Foundation, Inc., 59 Temple
19 * Place - Suite 330, Boston, MA 02111-1307 USA.
20 */
21
22#include <linux/kvm_host.h>
23#include <linux/smp.h>
24#include <asm/sn/addrs.h>
25#include <asm/sn/clksupport.h>
26#include <asm/sn/shub_mmr.h>
27
28#include "vti.h"
29#include "misc.h"
30
31#include <asm/pal.h>
32#include <asm/sal.h>
33#include <asm/tlb.h>
34
35/*
36 * Handy macros to make sure that the PAL return values start out
37 * as something meaningful.
38 */
39#define INIT_PAL_STATUS_UNIMPLEMENTED(x)		\
40	{						\
41		x.status = PAL_STATUS_UNIMPLEMENTED;	\
42		x.v0 = 0;				\
43		x.v1 = 0;				\
44		x.v2 = 0;				\
45	}
46
47#define INIT_PAL_STATUS_SUCCESS(x)			\
48	{						\
49		x.status = PAL_STATUS_SUCCESS;		\
50		x.v0 = 0;				\
51		x.v1 = 0;				\
52		x.v2 = 0;				\
53    }
54
55static void kvm_get_pal_call_data(struct kvm_vcpu *vcpu,
56		u64 *gr28, u64 *gr29, u64 *gr30, u64 *gr31) {
57	struct exit_ctl_data *p;
58
59	if (vcpu) {
60		p = &vcpu->arch.exit_data;
61		if (p->exit_reason == EXIT_REASON_PAL_CALL) {
62			*gr28 = p->u.pal_data.gr28;
63			*gr29 = p->u.pal_data.gr29;
64			*gr30 = p->u.pal_data.gr30;
65			*gr31 = p->u.pal_data.gr31;
66			return ;
67		}
68	}
69	printk(KERN_DEBUG"Failed to get vcpu pal data!!!\n");
70}
71
72static void set_pal_result(struct kvm_vcpu *vcpu,
73		struct ia64_pal_retval result) {
74
75	struct exit_ctl_data *p;
76
77	p = kvm_get_exit_data(vcpu);
78	if (p->exit_reason == EXIT_REASON_PAL_CALL) {
79		p->u.pal_data.ret = result;
80		return ;
81	}
82	INIT_PAL_STATUS_UNIMPLEMENTED(p->u.pal_data.ret);
83}
84
85static void set_sal_result(struct kvm_vcpu *vcpu,
86		struct sal_ret_values result) {
87	struct exit_ctl_data *p;
88
89	p = kvm_get_exit_data(vcpu);
90	if (p->exit_reason == EXIT_REASON_SAL_CALL) {
91		p->u.sal_data.ret = result;
92		return ;
93	}
94	printk(KERN_WARNING"Failed to set sal result!!\n");
95}
96
97struct cache_flush_args {
98	u64 cache_type;
99	u64 operation;
100	u64 progress;
101	long status;
102};
103
104cpumask_t cpu_cache_coherent_map;
105
106static void remote_pal_cache_flush(void *data)
107{
108	struct cache_flush_args *args = data;
109	long status;
110	u64 progress = args->progress;
111
112	status = ia64_pal_cache_flush(args->cache_type, args->operation,
113					&progress, NULL);
114	if (status != 0)
115	args->status = status;
116}
117
118static struct ia64_pal_retval pal_cache_flush(struct kvm_vcpu *vcpu)
119{
120	u64 gr28, gr29, gr30, gr31;
121	struct ia64_pal_retval result = {0, 0, 0, 0};
122	struct cache_flush_args args = {0, 0, 0, 0};
123	long psr;
124
125	gr28 = gr29 = gr30 = gr31 = 0;
126	kvm_get_pal_call_data(vcpu, &gr28, &gr29, &gr30, &gr31);
127
128	if (gr31 != 0)
129		printk(KERN_ERR"vcpu:%p called cache_flush error!\n", vcpu);
130
131	/* Always call Host Pal in int=1 */
132	gr30 &= ~PAL_CACHE_FLUSH_CHK_INTRS;
133	args.cache_type = gr29;
134	args.operation = gr30;
135	smp_call_function(remote_pal_cache_flush,
136				(void *)&args, 1);
137	if (args.status != 0)
138		printk(KERN_ERR"pal_cache_flush error!,"
139				"status:0x%lx\n", args.status);
140	/*
141	 * Call Host PAL cache flush
142	 * Clear psr.ic when call PAL_CACHE_FLUSH
143	 */
144	local_irq_save(psr);
145	result.status = ia64_pal_cache_flush(gr29, gr30, &result.v1,
146						&result.v0);
147	local_irq_restore(psr);
148	if (result.status != 0)
149		printk(KERN_ERR"vcpu:%p crashed due to cache_flush err:%ld"
150				"in1:%lx,in2:%lx\n",
151				vcpu, result.status, gr29, gr30);
152
153	return result;
154}
155
156struct ia64_pal_retval pal_cache_summary(struct kvm_vcpu *vcpu)
157{
158
159	struct ia64_pal_retval result;
160
161	PAL_CALL(result, PAL_CACHE_SUMMARY, 0, 0, 0);
162	return result;
163}
164
165static struct ia64_pal_retval pal_freq_base(struct kvm_vcpu *vcpu)
166{
167
168	struct ia64_pal_retval result;
169
170	PAL_CALL(result, PAL_FREQ_BASE, 0, 0, 0);
171
172	/*
173	 * PAL_FREQ_BASE may not be implemented in some platforms,
174	 * call SAL instead.
175	 */
176	if (result.v0 == 0) {
177		result.status = ia64_sal_freq_base(SAL_FREQ_BASE_PLATFORM,
178							&result.v0,
179							&result.v1);
180		result.v2 = 0;
181	}
182
183	return result;
184}
185
186/*
187 * On the SGI SN2, the ITC isn't stable. Emulation backed by the SN2
188 * RTC is used instead. This function patches the ratios from SAL
189 * to match the RTC before providing them to the guest.
190 */
191static void sn2_patch_itc_freq_ratios(struct ia64_pal_retval *result)
192{
193	struct pal_freq_ratio *ratio;
194	unsigned long sal_freq, sal_drift, factor;
195
196	result->status = ia64_sal_freq_base(SAL_FREQ_BASE_PLATFORM,
197					    &sal_freq, &sal_drift);
198	ratio = (struct pal_freq_ratio *)&result->v2;
199	factor = ((sal_freq * 3) + (sn_rtc_cycles_per_second / 2)) /
200		sn_rtc_cycles_per_second;
201
202	ratio->num = 3;
203	ratio->den = factor;
204}
205
206static struct ia64_pal_retval pal_freq_ratios(struct kvm_vcpu *vcpu)
207{
208	struct ia64_pal_retval result;
209
210	PAL_CALL(result, PAL_FREQ_RATIOS, 0, 0, 0);
211
212	if (vcpu->kvm->arch.is_sn2)
213		sn2_patch_itc_freq_ratios(&result);
214
215	return result;
216}
217
218static struct ia64_pal_retval pal_logical_to_physica(struct kvm_vcpu *vcpu)
219{
220	struct ia64_pal_retval result;
221
222	INIT_PAL_STATUS_UNIMPLEMENTED(result);
223	return result;
224}
225
226static struct ia64_pal_retval pal_platform_addr(struct kvm_vcpu *vcpu)
227{
228
229	struct ia64_pal_retval result;
230
231	INIT_PAL_STATUS_SUCCESS(result);
232	return result;
233}
234
235static struct ia64_pal_retval pal_proc_get_features(struct kvm_vcpu *vcpu)
236{
237
238	struct ia64_pal_retval result = {0, 0, 0, 0};
239	long in0, in1, in2, in3;
240
241	kvm_get_pal_call_data(vcpu, &in0, &in1, &in2, &in3);
242	result.status = ia64_pal_proc_get_features(&result.v0, &result.v1,
243			&result.v2, in2);
244
245	return result;
246}
247
248static struct ia64_pal_retval pal_register_info(struct kvm_vcpu *vcpu)
249{
250
251	struct ia64_pal_retval result = {0, 0, 0, 0};
252	long in0, in1, in2, in3;
253
254	kvm_get_pal_call_data(vcpu, &in0, &in1, &in2, &in3);
255	result.status = ia64_pal_register_info(in1, &result.v1, &result.v2);
256
257	return result;
258}
259
260static struct ia64_pal_retval pal_cache_info(struct kvm_vcpu *vcpu)
261{
262
263	pal_cache_config_info_t ci;
264	long status;
265	unsigned long in0, in1, in2, in3, r9, r10;
266
267	kvm_get_pal_call_data(vcpu, &in0, &in1, &in2, &in3);
268	status = ia64_pal_cache_config_info(in1, in2, &ci);
269	r9 = ci.pcci_info_1.pcci1_data;
270	r10 = ci.pcci_info_2.pcci2_data;
271	return ((struct ia64_pal_retval){status, r9, r10, 0});
272}
273
274#define GUEST_IMPL_VA_MSB	59
275#define GUEST_RID_BITS		18
276
277static struct ia64_pal_retval pal_vm_summary(struct kvm_vcpu *vcpu)
278{
279
280	pal_vm_info_1_u_t vminfo1;
281	pal_vm_info_2_u_t vminfo2;
282	struct ia64_pal_retval result;
283
284	PAL_CALL(result, PAL_VM_SUMMARY, 0, 0, 0);
285	if (!result.status) {
286		vminfo1.pvi1_val = result.v0;
287		vminfo1.pal_vm_info_1_s.max_itr_entry = 8;
288		vminfo1.pal_vm_info_1_s.max_dtr_entry = 8;
289		result.v0 = vminfo1.pvi1_val;
290		vminfo2.pal_vm_info_2_s.impl_va_msb = GUEST_IMPL_VA_MSB;
291		vminfo2.pal_vm_info_2_s.rid_size = GUEST_RID_BITS;
292		result.v1 = vminfo2.pvi2_val;
293	}
294
295	return result;
296}
297
298static struct ia64_pal_retval pal_vm_info(struct kvm_vcpu *vcpu)
299{
300	struct ia64_pal_retval result;
301	unsigned long in0, in1, in2, in3;
302
303	kvm_get_pal_call_data(vcpu, &in0, &in1, &in2, &in3);
304
305	result.status = ia64_pal_vm_info(in1, in2,
306			(pal_tc_info_u_t *)&result.v1, &result.v2);
307
308	return result;
309}
310
311static  u64 kvm_get_pal_call_index(struct kvm_vcpu *vcpu)
312{
313	u64 index = 0;
314	struct exit_ctl_data *p;
315
316	p = kvm_get_exit_data(vcpu);
317	if (p->exit_reason == EXIT_REASON_PAL_CALL)
318		index = p->u.pal_data.gr28;
319
320	return index;
321}
322
323static void prepare_for_halt(struct kvm_vcpu *vcpu)
324{
325	vcpu->arch.timer_pending = 1;
326	vcpu->arch.timer_fired = 0;
327}
328
329static struct ia64_pal_retval pal_perf_mon_info(struct kvm_vcpu *vcpu)
330{
331	long status;
332	unsigned long in0, in1, in2, in3, r9;
333	unsigned long pm_buffer[16];
334
335	kvm_get_pal_call_data(vcpu, &in0, &in1, &in2, &in3);
336	status = ia64_pal_perf_mon_info(pm_buffer,
337				(pal_perf_mon_info_u_t *) &r9);
338	if (status != 0) {
339		printk(KERN_DEBUG"PAL_PERF_MON_INFO fails ret=%ld\n", status);
340	} else {
341		if (in1)
342			memcpy((void *)in1, pm_buffer, sizeof(pm_buffer));
343		else {
344			status = PAL_STATUS_EINVAL;
345			printk(KERN_WARNING"Invalid parameters "
346						"for PAL call:0x%lx!\n", in0);
347		}
348	}
349	return (struct ia64_pal_retval){status, r9, 0, 0};
350}
351
352static struct ia64_pal_retval pal_halt_info(struct kvm_vcpu *vcpu)
353{
354	unsigned long in0, in1, in2, in3;
355	long status;
356	unsigned long res = 1000UL | (1000UL << 16) | (10UL << 32)
357					| (1UL << 61) | (1UL << 60);
358
359	kvm_get_pal_call_data(vcpu, &in0, &in1, &in2, &in3);
360	if (in1) {
361		memcpy((void *)in1, &res, sizeof(res));
362		status = 0;
363	} else{
364		status = PAL_STATUS_EINVAL;
365		printk(KERN_WARNING"Invalid parameters "
366					"for PAL call:0x%lx!\n", in0);
367	}
368
369	return (struct ia64_pal_retval){status, 0, 0, 0};
370}
371
372static struct ia64_pal_retval pal_mem_attrib(struct kvm_vcpu *vcpu)
373{
374	unsigned long r9;
375	long status;
376
377	status = ia64_pal_mem_attrib(&r9);
378
379	return (struct ia64_pal_retval){status, r9, 0, 0};
380}
381
382static void remote_pal_prefetch_visibility(void *v)
383{
384	s64 trans_type = (s64)v;
385	ia64_pal_prefetch_visibility(trans_type);
386}
387
388static struct ia64_pal_retval pal_prefetch_visibility(struct kvm_vcpu *vcpu)
389{
390	struct ia64_pal_retval result = {0, 0, 0, 0};
391	unsigned long in0, in1, in2, in3;
392	kvm_get_pal_call_data(vcpu, &in0, &in1, &in2, &in3);
393	result.status = ia64_pal_prefetch_visibility(in1);
394	if (result.status == 0) {
395		/* Must be performed on all remote processors
396		in the coherence domain. */
397		smp_call_function(remote_pal_prefetch_visibility,
398					(void *)in1, 1);
399		/* Unnecessary on remote processor for other vcpus!*/
400		result.status = 1;
401	}
402	return result;
403}
404
405static void remote_pal_mc_drain(void *v)
406{
407	ia64_pal_mc_drain();
408}
409
410static struct ia64_pal_retval pal_get_brand_info(struct kvm_vcpu *vcpu)
411{
412	struct ia64_pal_retval result = {0, 0, 0, 0};
413	unsigned long in0, in1, in2, in3;
414
415	kvm_get_pal_call_data(vcpu, &in0, &in1, &in2, &in3);
416
417	if (in1 == 0 && in2) {
418		char brand_info[128];
419		result.status = ia64_pal_get_brand_info(brand_info);
420		if (result.status == PAL_STATUS_SUCCESS)
421			memcpy((void *)in2, brand_info, 128);
422	} else {
423		result.status = PAL_STATUS_REQUIRES_MEMORY;
424		printk(KERN_WARNING"Invalid parameters for "
425					"PAL call:0x%lx!\n", in0);
426	}
427
428	return result;
429}
430
431int kvm_pal_emul(struct kvm_vcpu *vcpu, struct kvm_run *run)
432{
433
434	u64 gr28;
435	struct ia64_pal_retval result;
436	int ret = 1;
437
438	gr28 = kvm_get_pal_call_index(vcpu);
439	switch (gr28) {
440	case PAL_CACHE_FLUSH:
441		result = pal_cache_flush(vcpu);
442		break;
443	case PAL_MEM_ATTRIB:
444		result = pal_mem_attrib(vcpu);
445		break;
446	case PAL_CACHE_SUMMARY:
447		result = pal_cache_summary(vcpu);
448		break;
449	case PAL_PERF_MON_INFO:
450		result = pal_perf_mon_info(vcpu);
451		break;
452	case PAL_HALT_INFO:
453		result = pal_halt_info(vcpu);
454		break;
455	case PAL_HALT_LIGHT:
456	{
457		INIT_PAL_STATUS_SUCCESS(result);
458		prepare_for_halt(vcpu);
459		if (kvm_highest_pending_irq(vcpu) == -1)
460			ret = kvm_emulate_halt(vcpu);
461	}
462		break;
463
464	case PAL_PREFETCH_VISIBILITY:
465		result = pal_prefetch_visibility(vcpu);
466		break;
467	case PAL_MC_DRAIN:
468		result.status = ia64_pal_mc_drain();
469		smp_call_function(remote_pal_mc_drain, NULL, 1);
470		break;
471
472	case PAL_FREQ_RATIOS:
473		result = pal_freq_ratios(vcpu);
474		break;
475
476	case PAL_FREQ_BASE:
477		result = pal_freq_base(vcpu);
478		break;
479
480	case PAL_LOGICAL_TO_PHYSICAL :
481		result = pal_logical_to_physica(vcpu);
482		break;
483
484	case PAL_VM_SUMMARY :
485		result = pal_vm_summary(vcpu);
486		break;
487
488	case PAL_VM_INFO :
489		result = pal_vm_info(vcpu);
490		break;
491	case PAL_PLATFORM_ADDR :
492		result = pal_platform_addr(vcpu);
493		break;
494	case PAL_CACHE_INFO:
495		result = pal_cache_info(vcpu);
496		break;
497	case PAL_PTCE_INFO:
498		INIT_PAL_STATUS_SUCCESS(result);
499		result.v1 = (1L << 32) | 1L;
500		break;
501	case PAL_REGISTER_INFO:
502		result = pal_register_info(vcpu);
503		break;
504	case PAL_VM_PAGE_SIZE:
505		result.status = ia64_pal_vm_page_size(&result.v0,
506							&result.v1);
507		break;
508	case PAL_RSE_INFO:
509		result.status = ia64_pal_rse_info(&result.v0,
510					(pal_hints_u_t *)&result.v1);
511		break;
512	case PAL_PROC_GET_FEATURES:
513		result = pal_proc_get_features(vcpu);
514		break;
515	case PAL_DEBUG_INFO:
516		result.status = ia64_pal_debug_info(&result.v0,
517							&result.v1);
518		break;
519	case PAL_VERSION:
520		result.status = ia64_pal_version(
521				(pal_version_u_t *)&result.v0,
522				(pal_version_u_t *)&result.v1);
523		break;
524	case PAL_FIXED_ADDR:
525		result.status = PAL_STATUS_SUCCESS;
526		result.v0 = vcpu->vcpu_id;
527		break;
528	case PAL_BRAND_INFO:
529		result = pal_get_brand_info(vcpu);
530		break;
531	case PAL_GET_PSTATE:
532	case PAL_CACHE_SHARED_INFO:
533		INIT_PAL_STATUS_UNIMPLEMENTED(result);
534		break;
535	default:
536		INIT_PAL_STATUS_UNIMPLEMENTED(result);
537		printk(KERN_WARNING"kvm: Unsupported pal call,"
538					" index:0x%lx\n", gr28);
539	}
540	set_pal_result(vcpu, result);
541	return ret;
542}
543
544static struct sal_ret_values sal_emulator(struct kvm *kvm,
545				long index, unsigned long in1,
546				unsigned long in2, unsigned long in3,
547				unsigned long in4, unsigned long in5,
548				unsigned long in6, unsigned long in7)
549{
550	unsigned long r9  = 0;
551	unsigned long r10 = 0;
552	long r11 = 0;
553	long status;
554
555	status = 0;
556	switch (index) {
557	case SAL_FREQ_BASE:
558		status = ia64_sal_freq_base(in1, &r9, &r10);
559		break;
560	case SAL_PCI_CONFIG_READ:
561		printk(KERN_WARNING"kvm: Not allowed to call here!"
562			" SAL_PCI_CONFIG_READ\n");
563		break;
564	case SAL_PCI_CONFIG_WRITE:
565		printk(KERN_WARNING"kvm: Not allowed to call here!"
566			" SAL_PCI_CONFIG_WRITE\n");
567		break;
568	case SAL_SET_VECTORS:
569		if (in1 == SAL_VECTOR_OS_BOOT_RENDEZ) {
570			if (in4 != 0 || in5 != 0 || in6 != 0 || in7 != 0) {
571				status = -2;
572			} else {
573				kvm->arch.rdv_sal_data.boot_ip = in2;
574				kvm->arch.rdv_sal_data.boot_gp = in3;
575			}
576			printk("Rendvous called! iip:%lx\n\n", in2);
577		} else
578			printk(KERN_WARNING"kvm: CALLED SAL_SET_VECTORS %lu."
579							"ignored...\n", in1);
580		break;
581	case SAL_GET_STATE_INFO:
582		/* No more info.  */
583		status = -5;
584		r9 = 0;
585		break;
586	case SAL_GET_STATE_INFO_SIZE:
587		/* Return a dummy size.  */
588		status = 0;
589		r9 = 128;
590		break;
591	case SAL_CLEAR_STATE_INFO:
592		/* Noop.  */
593		break;
594	case SAL_MC_RENDEZ:
595		printk(KERN_WARNING
596			"kvm: called SAL_MC_RENDEZ. ignored...\n");
597		break;
598	case SAL_MC_SET_PARAMS:
599		printk(KERN_WARNING
600			"kvm: called  SAL_MC_SET_PARAMS.ignored!\n");
601		break;
602	case SAL_CACHE_FLUSH:
603		if (1) {
604			/*Flush using SAL.
605			This method is faster but has a side
606			effect on other vcpu running on
607			this cpu.  */
608			status = ia64_sal_cache_flush(in1);
609		} else {
610			/*Maybe need to implement the method
611			without side effect!*/
612			status = 0;
613		}
614		break;
615	case SAL_CACHE_INIT:
616		printk(KERN_WARNING
617			"kvm: called SAL_CACHE_INIT.  ignored...\n");
618		break;
619	case SAL_UPDATE_PAL:
620		printk(KERN_WARNING
621			"kvm: CALLED SAL_UPDATE_PAL.  ignored...\n");
622		break;
623	default:
624		printk(KERN_WARNING"kvm: called SAL_CALL with unknown index."
625						" index:%ld\n", index);
626		status = -1;
627		break;
628	}
629	return ((struct sal_ret_values) {status, r9, r10, r11});
630}
631
632static void kvm_get_sal_call_data(struct kvm_vcpu *vcpu, u64 *in0, u64 *in1,
633		u64 *in2, u64 *in3, u64 *in4, u64 *in5, u64 *in6, u64 *in7){
634
635	struct exit_ctl_data *p;
636
637	p = kvm_get_exit_data(vcpu);
638
639	if (p->exit_reason == EXIT_REASON_SAL_CALL) {
640		*in0 = p->u.sal_data.in0;
641		*in1 = p->u.sal_data.in1;
642		*in2 = p->u.sal_data.in2;
643		*in3 = p->u.sal_data.in3;
644		*in4 = p->u.sal_data.in4;
645		*in5 = p->u.sal_data.in5;
646		*in6 = p->u.sal_data.in6;
647		*in7 = p->u.sal_data.in7;
648		return ;
649	}
650	*in0 = 0;
651}
652
653void kvm_sal_emul(struct kvm_vcpu *vcpu)
654{
655
656	struct sal_ret_values result;
657	u64 index, in1, in2, in3, in4, in5, in6, in7;
658
659	kvm_get_sal_call_data(vcpu, &index, &in1, &in2,
660			&in3, &in4, &in5, &in6, &in7);
661	result = sal_emulator(vcpu->kvm, index, in1, in2, in3,
662					in4, in5, in6, in7);
663	set_sal_result(vcpu, result);
664}
665