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 
2554f65795SScott Wood /*
2654f65795SScott Wood  * Additions to this struct must only occur at the end, and should be
2754f65795SScott Wood  * accompanied by a KVM_MAGIC_FEAT flag to advertise that they are present
2854f65795SScott Wood  * (albeit not necessarily relevant to the current target hardware platform).
2954f65795SScott Wood  *
3054f65795SScott Wood  * Struct fields are always 32 or 64 bit aligned, depending on them being 32
3154f65795SScott Wood  * or 64 bit wide respectively.
3254f65795SScott Wood  *
3354f65795SScott Wood  * See Documentation/virtual/kvm/ppc-pv.txt
3454f65795SScott Wood  */
3596bc451aSAlexander Graf struct kvm_vcpu_arch_shared {
36fad93fe1SAlexander Graf 	__u64 scratch1;
37fad93fe1SAlexander Graf 	__u64 scratch2;
38fad93fe1SAlexander Graf 	__u64 scratch3;
395c6cedf4SAlexander Graf 	__u64 critical;		/* Guest may not get interrupts if == r1 */
40a73a9599SAlexander Graf 	__u64 sprg0;
41a73a9599SAlexander Graf 	__u64 sprg1;
42a73a9599SAlexander Graf 	__u64 sprg2;
43a73a9599SAlexander Graf 	__u64 sprg3;
44de7906c3SAlexander Graf 	__u64 srr0;
45de7906c3SAlexander Graf 	__u64 srr1;
46b5904972SScott Wood 	__u64 dar;		/* dear on BookE */
47666e7252SAlexander Graf 	__u64 msr;
48d562de48SAlexander Graf 	__u32 dsisr;
4990bba358SAlexander Graf 	__u32 int_pending;	/* Tells the guest if we have an interrupt */
50df1bfa25SAlexander Graf 	__u32 sr[16];
51b5904972SScott Wood 	__u32 mas0;
52b5904972SScott Wood 	__u32 mas1;
53b5904972SScott Wood 	__u64 mas7_3;
54b5904972SScott Wood 	__u64 mas2;
55b5904972SScott Wood 	__u32 mas4;
56b5904972SScott Wood 	__u32 mas6;
57b5904972SScott Wood 	__u32 esr;
58b5904972SScott Wood 	__u32 pir;
59b5904972SScott Wood 
60b5904972SScott Wood 	/*
61b5904972SScott Wood 	 * SPRG4-7 are user-readable, so we can only keep these consistent
62b5904972SScott Wood 	 * between the shared area and the real registers when there's an
63b5904972SScott Wood 	 * intervening exit to KVM.  This also applies to SPRG3 on some
64b5904972SScott Wood 	 * chips.
65b5904972SScott Wood 	 *
66b5904972SScott Wood 	 * This suffices for access by guest userspace, since in PR-mode
67b5904972SScott Wood 	 * KVM, an exit must occur when changing the guest's MSR[PR].
68b5904972SScott Wood 	 * If the guest kernel writes to SPRG3-7 via the shared area, it
69b5904972SScott Wood 	 * must also use the shared area for reading while in kernel space.
70b5904972SScott Wood 	 */
71b5904972SScott Wood 	__u64 sprg4;
72b5904972SScott Wood 	__u64 sprg5;
73b5904972SScott Wood 	__u64 sprg6;
74b5904972SScott Wood 	__u64 sprg7;
7596bc451aSAlexander Graf };
7696bc451aSAlexander Graf 
772a342ed5SAlexander Graf #define KVM_SC_MAGIC_R0		0x4b564d21 /* "KVM!" */
782a342ed5SAlexander Graf #define HC_VENDOR_KVM		(42 << 16)
792a342ed5SAlexander Graf #define HC_EV_SUCCESS		0
802a342ed5SAlexander Graf #define HC_EV_UNIMPLEMENTED	12
812a342ed5SAlexander Graf 
825fc87407SAlexander Graf #define KVM_FEATURE_MAGIC_PAGE	1
835fc87407SAlexander Graf 
847508e16cSAlexander Graf #define KVM_MAGIC_FEAT_SR		(1 << 0)
857508e16cSAlexander Graf 
86b5904972SScott Wood /* MASn, ESR, PIR, and high SPRGs */
87b5904972SScott Wood #define KVM_MAGIC_FEAT_MAS0_TO_SPRG7	(1 << 1)
88b5904972SScott Wood 
89b8b572e1SStephen Rothwell #ifdef __KERNEL__
90b8b572e1SStephen Rothwell 
912a342ed5SAlexander Graf #ifdef CONFIG_KVM_GUEST
922a342ed5SAlexander Graf 
9326e673c3SAlexander Graf #include <linux/of.h>
9426e673c3SAlexander Graf 
952a342ed5SAlexander Graf static inline int kvm_para_available(void)
962a342ed5SAlexander Graf {
972a342ed5SAlexander Graf 	struct device_node *hyper_node;
982a342ed5SAlexander Graf 
992a342ed5SAlexander Graf 	hyper_node = of_find_node_by_path("/hypervisor");
1002a342ed5SAlexander Graf 	if (!hyper_node)
1012a342ed5SAlexander Graf 		return 0;
1022a342ed5SAlexander Graf 
1032a342ed5SAlexander Graf 	if (!of_device_is_compatible(hyper_node, "linux,kvm"))
1042a342ed5SAlexander Graf 		return 0;
1052a342ed5SAlexander Graf 
1062a342ed5SAlexander Graf 	return 1;
1072a342ed5SAlexander Graf }
1082a342ed5SAlexander Graf 
1092a342ed5SAlexander Graf extern unsigned long kvm_hypercall(unsigned long *in,
1102a342ed5SAlexander Graf 				   unsigned long *out,
1112a342ed5SAlexander Graf 				   unsigned long nr);
1122a342ed5SAlexander Graf 
1132a342ed5SAlexander Graf #else
1142a342ed5SAlexander Graf 
115b8b572e1SStephen Rothwell static inline int kvm_para_available(void)
116b8b572e1SStephen Rothwell {
117b8b572e1SStephen Rothwell 	return 0;
118b8b572e1SStephen Rothwell }
119b8b572e1SStephen Rothwell 
1202a342ed5SAlexander Graf static unsigned long kvm_hypercall(unsigned long *in,
1212a342ed5SAlexander Graf 				   unsigned long *out,
1222a342ed5SAlexander Graf 				   unsigned long nr)
1232a342ed5SAlexander Graf {
1242a342ed5SAlexander Graf 	return HC_EV_UNIMPLEMENTED;
1252a342ed5SAlexander Graf }
1262a342ed5SAlexander Graf 
1272a342ed5SAlexander Graf #endif
1282a342ed5SAlexander Graf 
1292a342ed5SAlexander Graf static inline long kvm_hypercall0_1(unsigned int nr, unsigned long *r2)
1302a342ed5SAlexander Graf {
1312a342ed5SAlexander Graf 	unsigned long in[8];
1322a342ed5SAlexander Graf 	unsigned long out[8];
1332a342ed5SAlexander Graf 	unsigned long r;
1342a342ed5SAlexander Graf 
1352a342ed5SAlexander Graf 	r = kvm_hypercall(in, out, nr | HC_VENDOR_KVM);
1362a342ed5SAlexander Graf 	*r2 = out[0];
1372a342ed5SAlexander Graf 
1382a342ed5SAlexander Graf 	return r;
1392a342ed5SAlexander Graf }
1402a342ed5SAlexander Graf 
1412a342ed5SAlexander Graf static inline long kvm_hypercall0(unsigned int nr)
1422a342ed5SAlexander Graf {
1432a342ed5SAlexander Graf 	unsigned long in[8];
1442a342ed5SAlexander Graf 	unsigned long out[8];
1452a342ed5SAlexander Graf 
1462a342ed5SAlexander Graf 	return kvm_hypercall(in, out, nr | HC_VENDOR_KVM);
1472a342ed5SAlexander Graf }
1482a342ed5SAlexander Graf 
1492a342ed5SAlexander Graf static inline long kvm_hypercall1(unsigned int nr, unsigned long p1)
1502a342ed5SAlexander Graf {
1512a342ed5SAlexander Graf 	unsigned long in[8];
1522a342ed5SAlexander Graf 	unsigned long out[8];
1532a342ed5SAlexander Graf 
1542a342ed5SAlexander Graf 	in[0] = p1;
1552a342ed5SAlexander Graf 	return kvm_hypercall(in, out, nr | HC_VENDOR_KVM);
1562a342ed5SAlexander Graf }
1572a342ed5SAlexander Graf 
1582a342ed5SAlexander Graf static inline long kvm_hypercall2(unsigned int nr, unsigned long p1,
1592a342ed5SAlexander Graf 				  unsigned long p2)
1602a342ed5SAlexander Graf {
1612a342ed5SAlexander Graf 	unsigned long in[8];
1622a342ed5SAlexander Graf 	unsigned long out[8];
1632a342ed5SAlexander Graf 
1642a342ed5SAlexander Graf 	in[0] = p1;
1652a342ed5SAlexander Graf 	in[1] = p2;
1662a342ed5SAlexander Graf 	return kvm_hypercall(in, out, nr | HC_VENDOR_KVM);
1672a342ed5SAlexander Graf }
1682a342ed5SAlexander Graf 
1692a342ed5SAlexander Graf static inline long kvm_hypercall3(unsigned int nr, unsigned long p1,
1702a342ed5SAlexander Graf 				  unsigned long p2, unsigned long p3)
1712a342ed5SAlexander Graf {
1722a342ed5SAlexander Graf 	unsigned long in[8];
1732a342ed5SAlexander Graf 	unsigned long out[8];
1742a342ed5SAlexander Graf 
1752a342ed5SAlexander Graf 	in[0] = p1;
1762a342ed5SAlexander Graf 	in[1] = p2;
1772a342ed5SAlexander Graf 	in[2] = p3;
1782a342ed5SAlexander Graf 	return kvm_hypercall(in, out, nr | HC_VENDOR_KVM);
1792a342ed5SAlexander Graf }
1802a342ed5SAlexander Graf 
1812a342ed5SAlexander Graf static inline long kvm_hypercall4(unsigned int nr, unsigned long p1,
1822a342ed5SAlexander Graf 				  unsigned long p2, unsigned long p3,
1832a342ed5SAlexander Graf 				  unsigned long p4)
1842a342ed5SAlexander Graf {
1852a342ed5SAlexander Graf 	unsigned long in[8];
1862a342ed5SAlexander Graf 	unsigned long out[8];
1872a342ed5SAlexander Graf 
1882a342ed5SAlexander Graf 	in[0] = p1;
1892a342ed5SAlexander Graf 	in[1] = p2;
1902a342ed5SAlexander Graf 	in[2] = p3;
1912a342ed5SAlexander Graf 	in[3] = p4;
1922a342ed5SAlexander Graf 	return kvm_hypercall(in, out, nr | HC_VENDOR_KVM);
1932a342ed5SAlexander Graf }
1942a342ed5SAlexander Graf 
1952a342ed5SAlexander Graf 
196b8b572e1SStephen Rothwell static inline unsigned int kvm_arch_para_features(void)
197b8b572e1SStephen Rothwell {
1982a342ed5SAlexander Graf 	unsigned long r;
1992a342ed5SAlexander Graf 
2002a342ed5SAlexander Graf 	if (!kvm_para_available())
201b8b572e1SStephen Rothwell 		return 0;
2022a342ed5SAlexander Graf 
2032a342ed5SAlexander Graf 	if(kvm_hypercall0_1(KVM_HC_FEATURES, &r))
2042a342ed5SAlexander Graf 		return 0;
2052a342ed5SAlexander Graf 
2062a342ed5SAlexander Graf 	return r;
207b8b572e1SStephen Rothwell }
208b8b572e1SStephen Rothwell 
209b8b572e1SStephen Rothwell #endif /* __KERNEL__ */
210b8b572e1SStephen Rothwell 
211b8b572e1SStephen Rothwell #endif /* __POWERPC_KVM_PARA_H__ */
212