1b8b572e1SStephen Rothwell /*
2b8b572e1SStephen Rothwell  * This program is free software; you can redistribute it and/or modify
3b8b572e1SStephen Rothwell  * it under the terms of the GNU General Public License, version 2, as
4b8b572e1SStephen Rothwell  * published by the Free Software Foundation.
5b8b572e1SStephen Rothwell  *
6b8b572e1SStephen Rothwell  * This program is distributed in the hope that it will be useful,
7b8b572e1SStephen Rothwell  * but WITHOUT ANY WARRANTY; without even the implied warranty of
8b8b572e1SStephen Rothwell  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
9b8b572e1SStephen Rothwell  * GNU General Public License for more details.
10b8b572e1SStephen Rothwell  *
11b8b572e1SStephen Rothwell  * You should have received a copy of the GNU General Public License
12b8b572e1SStephen Rothwell  * along with this program; if not, write to the Free Software
13b8b572e1SStephen Rothwell  * Foundation, 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
14b8b572e1SStephen Rothwell  *
15b8b572e1SStephen Rothwell  * Copyright IBM Corp. 2008
16b8b572e1SStephen Rothwell  *
17b8b572e1SStephen Rothwell  * Authors: Hollis Blanchard <hollisb@us.ibm.com>
18b8b572e1SStephen Rothwell  */
19b8b572e1SStephen Rothwell 
20b8b572e1SStephen Rothwell #ifndef __POWERPC_KVM_PARA_H__
21b8b572e1SStephen Rothwell #define __POWERPC_KVM_PARA_H__
22b8b572e1SStephen Rothwell 
2396bc451aSAlexander Graf #include <linux/types.h>
2496bc451aSAlexander Graf 
2596bc451aSAlexander Graf struct kvm_vcpu_arch_shared {
26fad93fe1SAlexander Graf 	__u64 scratch1;
27fad93fe1SAlexander Graf 	__u64 scratch2;
28fad93fe1SAlexander Graf 	__u64 scratch3;
295c6cedf4SAlexander Graf 	__u64 critical;		/* Guest may not get interrupts if == r1 */
30a73a9599SAlexander Graf 	__u64 sprg0;
31a73a9599SAlexander Graf 	__u64 sprg1;
32a73a9599SAlexander Graf 	__u64 sprg2;
33a73a9599SAlexander Graf 	__u64 sprg3;
34de7906c3SAlexander Graf 	__u64 srr0;
35de7906c3SAlexander Graf 	__u64 srr1;
36b5904972SScott Wood 	__u64 dar;		/* dear on BookE */
37666e7252SAlexander Graf 	__u64 msr;
38d562de48SAlexander Graf 	__u32 dsisr;
3990bba358SAlexander Graf 	__u32 int_pending;	/* Tells the guest if we have an interrupt */
40df1bfa25SAlexander Graf 	__u32 sr[16];
41b5904972SScott Wood 	__u32 mas0;
42b5904972SScott Wood 	__u32 mas1;
43b5904972SScott Wood 	__u64 mas7_3;
44b5904972SScott Wood 	__u64 mas2;
45b5904972SScott Wood 	__u32 mas4;
46b5904972SScott Wood 	__u32 mas6;
47b5904972SScott Wood 	__u32 esr;
48b5904972SScott Wood 	__u32 pir;
49b5904972SScott Wood 
50b5904972SScott Wood 	/*
51b5904972SScott Wood 	 * SPRG4-7 are user-readable, so we can only keep these consistent
52b5904972SScott Wood 	 * between the shared area and the real registers when there's an
53b5904972SScott Wood 	 * intervening exit to KVM.  This also applies to SPRG3 on some
54b5904972SScott Wood 	 * chips.
55b5904972SScott Wood 	 *
56b5904972SScott Wood 	 * This suffices for access by guest userspace, since in PR-mode
57b5904972SScott Wood 	 * KVM, an exit must occur when changing the guest's MSR[PR].
58b5904972SScott Wood 	 * If the guest kernel writes to SPRG3-7 via the shared area, it
59b5904972SScott Wood 	 * must also use the shared area for reading while in kernel space.
60b5904972SScott Wood 	 */
61b5904972SScott Wood 	__u64 sprg4;
62b5904972SScott Wood 	__u64 sprg5;
63b5904972SScott Wood 	__u64 sprg6;
64b5904972SScott Wood 	__u64 sprg7;
6596bc451aSAlexander Graf };
6696bc451aSAlexander Graf 
672a342ed5SAlexander Graf #define KVM_SC_MAGIC_R0		0x4b564d21 /* "KVM!" */
682a342ed5SAlexander Graf #define HC_VENDOR_KVM		(42 << 16)
692a342ed5SAlexander Graf #define HC_EV_SUCCESS		0
702a342ed5SAlexander Graf #define HC_EV_UNIMPLEMENTED	12
712a342ed5SAlexander Graf 
725fc87407SAlexander Graf #define KVM_FEATURE_MAGIC_PAGE	1
735fc87407SAlexander Graf 
747508e16cSAlexander Graf #define KVM_MAGIC_FEAT_SR		(1 << 0)
757508e16cSAlexander Graf 
76b5904972SScott Wood /* MASn, ESR, PIR, and high SPRGs */
77b5904972SScott Wood #define KVM_MAGIC_FEAT_MAS0_TO_SPRG7	(1 << 1)
78b5904972SScott Wood 
79b8b572e1SStephen Rothwell #ifdef __KERNEL__
80b8b572e1SStephen Rothwell 
812a342ed5SAlexander Graf #ifdef CONFIG_KVM_GUEST
822a342ed5SAlexander Graf 
8326e673c3SAlexander Graf #include <linux/of.h>
8426e673c3SAlexander Graf 
852a342ed5SAlexander Graf static inline int kvm_para_available(void)
862a342ed5SAlexander Graf {
872a342ed5SAlexander Graf 	struct device_node *hyper_node;
882a342ed5SAlexander Graf 
892a342ed5SAlexander Graf 	hyper_node = of_find_node_by_path("/hypervisor");
902a342ed5SAlexander Graf 	if (!hyper_node)
912a342ed5SAlexander Graf 		return 0;
922a342ed5SAlexander Graf 
932a342ed5SAlexander Graf 	if (!of_device_is_compatible(hyper_node, "linux,kvm"))
942a342ed5SAlexander Graf 		return 0;
952a342ed5SAlexander Graf 
962a342ed5SAlexander Graf 	return 1;
972a342ed5SAlexander Graf }
982a342ed5SAlexander Graf 
992a342ed5SAlexander Graf extern unsigned long kvm_hypercall(unsigned long *in,
1002a342ed5SAlexander Graf 				   unsigned long *out,
1012a342ed5SAlexander Graf 				   unsigned long nr);
1022a342ed5SAlexander Graf 
1032a342ed5SAlexander Graf #else
1042a342ed5SAlexander Graf 
105b8b572e1SStephen Rothwell static inline int kvm_para_available(void)
106b8b572e1SStephen Rothwell {
107b8b572e1SStephen Rothwell 	return 0;
108b8b572e1SStephen Rothwell }
109b8b572e1SStephen Rothwell 
1102a342ed5SAlexander Graf static unsigned long kvm_hypercall(unsigned long *in,
1112a342ed5SAlexander Graf 				   unsigned long *out,
1122a342ed5SAlexander Graf 				   unsigned long nr)
1132a342ed5SAlexander Graf {
1142a342ed5SAlexander Graf 	return HC_EV_UNIMPLEMENTED;
1152a342ed5SAlexander Graf }
1162a342ed5SAlexander Graf 
1172a342ed5SAlexander Graf #endif
1182a342ed5SAlexander Graf 
1192a342ed5SAlexander Graf static inline long kvm_hypercall0_1(unsigned int nr, unsigned long *r2)
1202a342ed5SAlexander Graf {
1212a342ed5SAlexander Graf 	unsigned long in[8];
1222a342ed5SAlexander Graf 	unsigned long out[8];
1232a342ed5SAlexander Graf 	unsigned long r;
1242a342ed5SAlexander Graf 
1252a342ed5SAlexander Graf 	r = kvm_hypercall(in, out, nr | HC_VENDOR_KVM);
1262a342ed5SAlexander Graf 	*r2 = out[0];
1272a342ed5SAlexander Graf 
1282a342ed5SAlexander Graf 	return r;
1292a342ed5SAlexander Graf }
1302a342ed5SAlexander Graf 
1312a342ed5SAlexander Graf static inline long kvm_hypercall0(unsigned int nr)
1322a342ed5SAlexander Graf {
1332a342ed5SAlexander Graf 	unsigned long in[8];
1342a342ed5SAlexander Graf 	unsigned long out[8];
1352a342ed5SAlexander Graf 
1362a342ed5SAlexander Graf 	return kvm_hypercall(in, out, nr | HC_VENDOR_KVM);
1372a342ed5SAlexander Graf }
1382a342ed5SAlexander Graf 
1392a342ed5SAlexander Graf static inline long kvm_hypercall1(unsigned int nr, unsigned long p1)
1402a342ed5SAlexander Graf {
1412a342ed5SAlexander Graf 	unsigned long in[8];
1422a342ed5SAlexander Graf 	unsigned long out[8];
1432a342ed5SAlexander Graf 
1442a342ed5SAlexander Graf 	in[0] = p1;
1452a342ed5SAlexander Graf 	return kvm_hypercall(in, out, nr | HC_VENDOR_KVM);
1462a342ed5SAlexander Graf }
1472a342ed5SAlexander Graf 
1482a342ed5SAlexander Graf static inline long kvm_hypercall2(unsigned int nr, unsigned long p1,
1492a342ed5SAlexander Graf 				  unsigned long p2)
1502a342ed5SAlexander Graf {
1512a342ed5SAlexander Graf 	unsigned long in[8];
1522a342ed5SAlexander Graf 	unsigned long out[8];
1532a342ed5SAlexander Graf 
1542a342ed5SAlexander Graf 	in[0] = p1;
1552a342ed5SAlexander Graf 	in[1] = p2;
1562a342ed5SAlexander Graf 	return kvm_hypercall(in, out, nr | HC_VENDOR_KVM);
1572a342ed5SAlexander Graf }
1582a342ed5SAlexander Graf 
1592a342ed5SAlexander Graf static inline long kvm_hypercall3(unsigned int nr, unsigned long p1,
1602a342ed5SAlexander Graf 				  unsigned long p2, unsigned long p3)
1612a342ed5SAlexander Graf {
1622a342ed5SAlexander Graf 	unsigned long in[8];
1632a342ed5SAlexander Graf 	unsigned long out[8];
1642a342ed5SAlexander Graf 
1652a342ed5SAlexander Graf 	in[0] = p1;
1662a342ed5SAlexander Graf 	in[1] = p2;
1672a342ed5SAlexander Graf 	in[2] = p3;
1682a342ed5SAlexander Graf 	return kvm_hypercall(in, out, nr | HC_VENDOR_KVM);
1692a342ed5SAlexander Graf }
1702a342ed5SAlexander Graf 
1712a342ed5SAlexander Graf static inline long kvm_hypercall4(unsigned int nr, unsigned long p1,
1722a342ed5SAlexander Graf 				  unsigned long p2, unsigned long p3,
1732a342ed5SAlexander Graf 				  unsigned long p4)
1742a342ed5SAlexander Graf {
1752a342ed5SAlexander Graf 	unsigned long in[8];
1762a342ed5SAlexander Graf 	unsigned long out[8];
1772a342ed5SAlexander Graf 
1782a342ed5SAlexander Graf 	in[0] = p1;
1792a342ed5SAlexander Graf 	in[1] = p2;
1802a342ed5SAlexander Graf 	in[2] = p3;
1812a342ed5SAlexander Graf 	in[3] = p4;
1822a342ed5SAlexander Graf 	return kvm_hypercall(in, out, nr | HC_VENDOR_KVM);
1832a342ed5SAlexander Graf }
1842a342ed5SAlexander Graf 
1852a342ed5SAlexander Graf 
186b8b572e1SStephen Rothwell static inline unsigned int kvm_arch_para_features(void)
187b8b572e1SStephen Rothwell {
1882a342ed5SAlexander Graf 	unsigned long r;
1892a342ed5SAlexander Graf 
1902a342ed5SAlexander Graf 	if (!kvm_para_available())
191b8b572e1SStephen Rothwell 		return 0;
1922a342ed5SAlexander Graf 
1932a342ed5SAlexander Graf 	if(kvm_hypercall0_1(KVM_HC_FEATURES, &r))
1942a342ed5SAlexander Graf 		return 0;
1952a342ed5SAlexander Graf 
1962a342ed5SAlexander Graf 	return r;
197b8b572e1SStephen Rothwell }
198b8b572e1SStephen Rothwell 
199b8b572e1SStephen Rothwell #endif /* __KERNEL__ */
200b8b572e1SStephen Rothwell 
201b8b572e1SStephen Rothwell #endif /* __POWERPC_KVM_PARA_H__ */
202