blob: 79dc37fb86b540295ba2e016292f78706f97c900 [file] [log] [blame]
Alexander Graf3ae07892010-04-16 00:11:37 +02001/*
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 SUSE Linux Products GmbH 2010
16 *
17 * Authors: Alexander Graf <agraf@suse.de>
18 */
19
20#ifndef __ASM_KVM_BOOK3S_64_H__
21#define __ASM_KVM_BOOK3S_64_H__
22
Paul Mackerrasde56a942011-06-29 00:21:34 +000023#ifdef CONFIG_KVM_BOOK3S_PR
Alexander Graf468a12c2011-12-09 14:44:13 +010024static inline struct kvmppc_book3s_shadow_vcpu *svcpu_get(struct kvm_vcpu *vcpu)
Alexander Graf3ae07892010-04-16 00:11:37 +020025{
Alexander Graf468a12c2011-12-09 14:44:13 +010026 preempt_disable();
Alexander Graf3ae07892010-04-16 00:11:37 +020027 return &get_paca()->shadow_vcpu;
28}
Alexander Graf468a12c2011-12-09 14:44:13 +010029
30static inline void svcpu_put(struct kvmppc_book3s_shadow_vcpu *svcpu)
31{
32 preempt_enable();
33}
Paul Mackerrasde56a942011-06-29 00:21:34 +000034#endif
Alexander Graf3ae07892010-04-16 00:11:37 +020035
David Gibson54738c02011-06-29 00:22:41 +000036#define SPAPR_TCE_SHIFT 12
37
Paul Mackerras8936dda2011-12-12 12:27:39 +000038#ifdef CONFIG_KVM_BOOK3S_64_HV
39/* For now use fixed-size 16MB page table */
40#define HPT_ORDER 24
41#define HPT_NPTEG (1ul << (HPT_ORDER - 7)) /* 128B per pteg */
42#define HPT_NPTE (HPT_NPTEG << 3) /* 8 PTEs per PTEG */
43#define HPT_HASH_MASK (HPT_NPTEG - 1)
44#endif
45
Paul Mackerras697d3892011-12-12 12:36:37 +000046#define VRMA_VSID 0x1ffffffUL /* 1TB VSID reserved for VRMA */
47
Paul Mackerras075295d2011-12-12 12:30:16 +000048/*
49 * We use a lock bit in HPTE dword 0 to synchronize updates and
50 * accesses to each HPTE, and another bit to indicate non-present
51 * HPTEs.
52 */
53#define HPTE_V_HVLOCK 0x40UL
Paul Mackerras697d3892011-12-12 12:36:37 +000054#define HPTE_V_ABSENT 0x20UL
Paul Mackerras075295d2011-12-12 12:30:16 +000055
56static inline long try_lock_hpte(unsigned long *hpte, unsigned long bits)
57{
58 unsigned long tmp, old;
59
60 asm volatile(" ldarx %0,0,%2\n"
61 " and. %1,%0,%3\n"
62 " bne 2f\n"
63 " ori %0,%0,%4\n"
64 " stdcx. %0,0,%2\n"
65 " beq+ 2f\n"
66 " li %1,%3\n"
67 "2: isync"
68 : "=&r" (tmp), "=&r" (old)
69 : "r" (hpte), "r" (bits), "i" (HPTE_V_HVLOCK)
70 : "cc", "memory");
71 return old == 0;
72}
73
Andreas Schwab36cc66d2011-11-08 07:08:52 +000074static inline unsigned long compute_tlbie_rb(unsigned long v, unsigned long r,
75 unsigned long pte_index)
76{
77 unsigned long rb, va_low;
78
79 rb = (v & ~0x7fUL) << 16; /* AVA field */
80 va_low = pte_index >> 3;
81 if (v & HPTE_V_SECONDARY)
82 va_low = ~va_low;
83 /* xor vsid from AVA */
84 if (!(v & HPTE_V_1TB_SEG))
85 va_low ^= v >> 12;
86 else
87 va_low ^= v >> 24;
88 va_low &= 0x7ff;
89 if (v & HPTE_V_LARGE) {
90 rb |= 1; /* L field */
91 if (cpu_has_feature(CPU_FTR_ARCH_206) &&
92 (r & 0xff000)) {
93 /* non-16MB large page, must be 64k */
94 /* (masks depend on page size) */
95 rb |= 0x1000; /* page encoding in LP field */
96 rb |= (va_low & 0x7f) << 16; /* 7b of VA in AVA/LP field */
97 rb |= (va_low & 0xfe); /* AVAL field (P7 doesn't seem to care) */
98 }
99 } else {
100 /* 4kB page */
101 rb |= (va_low & 0x7ff) << 12; /* remaining 11b of VA */
102 }
103 rb |= (v >> 54) & 0x300; /* B field */
104 return rb;
105}
106
Paul Mackerrasc77162d2011-12-12 12:31:00 +0000107static inline unsigned long hpte_page_size(unsigned long h, unsigned long l)
108{
109 /* only handle 4k, 64k and 16M pages for now */
110 if (!(h & HPTE_V_LARGE))
111 return 1ul << 12; /* 4k page */
112 if ((l & 0xf000) == 0x1000 && cpu_has_feature(CPU_FTR_ARCH_206))
113 return 1ul << 16; /* 64k page */
114 if ((l & 0xff000) == 0)
115 return 1ul << 24; /* 16M page */
116 return 0; /* error */
117}
118
Paul Mackerras06ce2c62011-12-12 12:33:07 +0000119static inline unsigned long hpte_rpn(unsigned long ptel, unsigned long psize)
120{
121 return ((ptel & HPTE_R_RPN) & ~(psize - 1)) >> PAGE_SHIFT;
122}
123
Paul Mackerras9d0ef5ea2011-12-12 12:32:27 +0000124static inline int hpte_cache_flags_ok(unsigned long ptel, unsigned long io_type)
125{
126 unsigned int wimg = ptel & HPTE_R_WIMG;
127
128 /* Handle SAO */
129 if (wimg == (HPTE_R_W | HPTE_R_I | HPTE_R_M) &&
130 cpu_has_feature(CPU_FTR_ARCH_206))
131 wimg = HPTE_R_M;
132
133 if (!io_type)
134 return wimg == HPTE_R_M;
135
136 return (wimg & (HPTE_R_W | HPTE_R_I)) == io_type;
137}
138
139/* Return HPTE cache control bits corresponding to Linux pte bits */
140static inline unsigned long hpte_cache_bits(unsigned long pte_val)
141{
142#if _PAGE_NO_CACHE == HPTE_R_I && _PAGE_WRITETHRU == HPTE_R_W
143 return pte_val & (HPTE_R_W | HPTE_R_I);
144#else
145 return ((pte_val & _PAGE_NO_CACHE) ? HPTE_R_I : 0) +
146 ((pte_val & _PAGE_WRITETHRU) ? HPTE_R_W : 0);
147#endif
148}
149
Paul Mackerras697d3892011-12-12 12:36:37 +0000150static inline bool hpte_read_permission(unsigned long pp, unsigned long key)
151{
152 if (key)
153 return PP_RWRX <= pp && pp <= PP_RXRX;
154 return 1;
155}
156
157static inline bool hpte_write_permission(unsigned long pp, unsigned long key)
158{
159 if (key)
160 return pp == PP_RWRW;
161 return pp <= PP_RWRW;
162}
163
164static inline int hpte_get_skey_perm(unsigned long hpte_r, unsigned long amr)
165{
166 unsigned long skey;
167
168 skey = ((hpte_r & HPTE_R_KEY_HI) >> 57) |
169 ((hpte_r & HPTE_R_KEY_LO) >> 9);
170 return (amr >> (62 - 2 * skey)) & 3;
171}
172
Paul Mackerras06ce2c62011-12-12 12:33:07 +0000173static inline void lock_rmap(unsigned long *rmap)
174{
175 do {
176 while (test_bit(KVMPPC_RMAP_LOCK_BIT, rmap))
177 cpu_relax();
178 } while (test_and_set_bit_lock(KVMPPC_RMAP_LOCK_BIT, rmap));
179}
180
181static inline void unlock_rmap(unsigned long *rmap)
182{
183 __clear_bit_unlock(KVMPPC_RMAP_LOCK_BIT, rmap);
184}
185
Paul Mackerrasda9d1d72011-12-12 12:31:41 +0000186static inline bool slot_is_aligned(struct kvm_memory_slot *memslot,
187 unsigned long pagesize)
188{
189 unsigned long mask = (pagesize >> PAGE_SHIFT) - 1;
190
191 if (pagesize <= PAGE_SIZE)
192 return 1;
193 return !(memslot->base_gfn & mask) && !(memslot->npages & mask);
194}
195
Alexander Graf3ae07892010-04-16 00:11:37 +0200196#endif /* __ASM_KVM_BOOK3S_64_H__ */