blob: 4ffd5a1e788da2480b216c1dbf8bd4754e5f3dcd [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 Mackerras0eeede02016-09-02 17:20:43 +100023#include <asm/book3s/64/mmu-hash.h>
24
Aneesh Kumar K.V7aa79932013-10-07 22:17:51 +053025#ifdef CONFIG_KVM_BOOK3S_PR_POSSIBLE
Alexander Graf468a12c2011-12-09 14:44:13 +010026static inline struct kvmppc_book3s_shadow_vcpu *svcpu_get(struct kvm_vcpu *vcpu)
Alexander Graf3ae07892010-04-16 00:11:37 +020027{
Alexander Graf468a12c2011-12-09 14:44:13 +010028 preempt_disable();
Alexander Graf3ae07892010-04-16 00:11:37 +020029 return &get_paca()->shadow_vcpu;
30}
Alexander Graf468a12c2011-12-09 14:44:13 +010031
32static inline void svcpu_put(struct kvmppc_book3s_shadow_vcpu *svcpu)
33{
34 preempt_enable();
35}
Paul Mackerrasde56a942011-06-29 00:21:34 +000036#endif
Alexander Graf3ae07892010-04-16 00:11:37 +020037
Aneesh Kumar K.V9975f5e2013-10-07 22:17:52 +053038#ifdef CONFIG_KVM_BOOK3S_HV_POSSIBLE
Paul Mackerras32fad282012-05-04 02:32:53 +000039#define KVM_DEFAULT_HPT_ORDER 24 /* 16MB HPT by default */
Paul Mackerras8936dda2011-12-12 12:27:39 +000040#endif
41
Paul Mackerras697d3892011-12-12 12:36:37 +000042#define VRMA_VSID 0x1ffffffUL /* 1TB VSID reserved for VRMA */
43
Paul Mackerras075295d2011-12-12 12:30:16 +000044/*
45 * We use a lock bit in HPTE dword 0 to synchronize updates and
46 * accesses to each HPTE, and another bit to indicate non-present
47 * HPTEs.
48 */
49#define HPTE_V_HVLOCK 0x40UL
Paul Mackerras697d3892011-12-12 12:36:37 +000050#define HPTE_V_ABSENT 0x20UL
Paul Mackerras075295d2011-12-12 12:30:16 +000051
Paul Mackerras44e5f6b2012-11-19 22:52:49 +000052/*
53 * We use this bit in the guest_rpte field of the revmap entry
54 * to indicate a modified HPTE.
55 */
56#define HPTE_GR_MODIFIED (1ul << 62)
57
58/* These bits are reserved in the guest view of the HPTE */
59#define HPTE_GR_RESERVED HPTE_GR_MODIFIED
60
Alexander Graf6f22bd32014-06-11 10:16:06 +020061static inline long try_lock_hpte(__be64 *hpte, unsigned long bits)
Paul Mackerras075295d2011-12-12 12:30:16 +000062{
63 unsigned long tmp, old;
Alexander Graf6f22bd32014-06-11 10:16:06 +020064 __be64 be_lockbit, be_bits;
65
66 /*
67 * We load/store in native endian, but the HTAB is in big endian. If
68 * we byte swap all data we apply on the PTE we're implicitly correct
69 * again.
70 */
71 be_lockbit = cpu_to_be64(HPTE_V_HVLOCK);
72 be_bits = cpu_to_be64(bits);
Paul Mackerras075295d2011-12-12 12:30:16 +000073
74 asm volatile(" ldarx %0,0,%2\n"
75 " and. %1,%0,%3\n"
76 " bne 2f\n"
Alexander Graf6f22bd32014-06-11 10:16:06 +020077 " or %0,%0,%4\n"
Paul Mackerras075295d2011-12-12 12:30:16 +000078 " stdcx. %0,0,%2\n"
79 " beq+ 2f\n"
Paul Mackerras8b5869a2012-10-15 01:20:50 +000080 " mr %1,%3\n"
Paul Mackerras075295d2011-12-12 12:30:16 +000081 "2: isync"
82 : "=&r" (tmp), "=&r" (old)
Alexander Graf6f22bd32014-06-11 10:16:06 +020083 : "r" (hpte), "r" (be_bits), "r" (be_lockbit)
Paul Mackerras075295d2011-12-12 12:30:16 +000084 : "cc", "memory");
85 return old == 0;
86}
87
Aneesh Kumar K.Va4bd6eb2015-03-20 20:39:43 +110088static inline void unlock_hpte(__be64 *hpte, unsigned long hpte_v)
89{
90 hpte_v &= ~HPTE_V_HVLOCK;
91 asm volatile(PPC_RELEASE_BARRIER "" : : : "memory");
92 hpte[0] = cpu_to_be64(hpte_v);
93}
94
95/* Without barrier */
96static inline void __unlock_hpte(__be64 *hpte, unsigned long hpte_v)
97{
98 hpte_v &= ~HPTE_V_HVLOCK;
99 hpte[0] = cpu_to_be64(hpte_v);
100}
101
Andreas Schwab36cc66d2011-11-08 07:08:52 +0000102static inline unsigned long compute_tlbie_rb(unsigned long v, unsigned long r,
103 unsigned long pte_index)
104{
Paul Mackerras0eeede02016-09-02 17:20:43 +1000105 int i, b_psize = MMU_PAGE_4K, a_psize = MMU_PAGE_4K;
Aneesh Kumar K.V1f365bb2014-05-06 23:31:36 +0530106 unsigned int penc;
107 unsigned long rb = 0, va_low, sllp;
108 unsigned int lp = (r >> LP_SHIFT) & ((1 << LP_BITS) - 1);
Andreas Schwab36cc66d2011-11-08 07:08:52 +0000109
Alexander Graff6bf3a62014-06-11 17:13:55 +0200110 if (v & HPTE_V_LARGE) {
Paul Mackerras0eeede02016-09-02 17:20:43 +1000111 i = hpte_page_sizes[lp];
112 b_psize = i & 0xf;
113 a_psize = i >> 4;
Aneesh Kumar K.V1f365bb2014-05-06 23:31:36 +0530114 }
Paul Mackerras0eeede02016-09-02 17:20:43 +1000115
Aneesh Kumar K.V1f365bb2014-05-06 23:31:36 +0530116 /*
117 * Ignore the top 14 bits of va
118 * v have top two bits covering segment size, hence move
119 * by 16 bits, Also clear the lower HPTE_V_AVPN_SHIFT (7) bits.
120 * AVA field in v also have the lower 23 bits ignored.
121 * For base page size 4K we need 14 .. 65 bits (so need to
122 * collect extra 11 bits)
123 * For others we need 14..14+i
124 */
125 /* This covers 14..54 bits of va*/
Andreas Schwab36cc66d2011-11-08 07:08:52 +0000126 rb = (v & ~0x7fUL) << 16; /* AVA field */
Aneesh Kumar K.V63fff5c2014-06-29 16:47:30 +0530127
Paul Mackerrasd5067352014-11-03 15:51:56 +1100128 rb |= (v >> HPTE_V_SSIZE_SHIFT) << 8; /* B field */
Aneesh Kumar K.V1f365bb2014-05-06 23:31:36 +0530129 /*
130 * AVA in v had cleared lower 23 bits. We need to derive
131 * that from pteg index
132 */
Andreas Schwab36cc66d2011-11-08 07:08:52 +0000133 va_low = pte_index >> 3;
134 if (v & HPTE_V_SECONDARY)
135 va_low = ~va_low;
Aneesh Kumar K.V1f365bb2014-05-06 23:31:36 +0530136 /*
137 * get the vpn bits from va_low using reverse of hashing.
138 * In v we have va with 23 bits dropped and then left shifted
139 * HPTE_V_AVPN_SHIFT (7) bits. Now to find vsid we need
140 * right shift it with (SID_SHIFT - (23 - 7))
141 */
Andreas Schwab36cc66d2011-11-08 07:08:52 +0000142 if (!(v & HPTE_V_1TB_SEG))
Aneesh Kumar K.V1f365bb2014-05-06 23:31:36 +0530143 va_low ^= v >> (SID_SHIFT - 16);
Andreas Schwab36cc66d2011-11-08 07:08:52 +0000144 else
Aneesh Kumar K.V1f365bb2014-05-06 23:31:36 +0530145 va_low ^= v >> (SID_SHIFT_1T - 16);
Andreas Schwab36cc66d2011-11-08 07:08:52 +0000146 va_low &= 0x7ff;
Aneesh Kumar K.V1f365bb2014-05-06 23:31:36 +0530147
148 switch (b_psize) {
149 case MMU_PAGE_4K:
Aneesh Kumar K.V138ee7e2016-07-13 15:06:37 +0530150 sllp = get_sllp_encoding(a_psize);
Aneesh Kumar K.V1f365bb2014-05-06 23:31:36 +0530151 rb |= sllp << 5; /* AP field */
152 rb |= (va_low & 0x7ff) << 12; /* remaining 11 bits of AVA */
153 break;
154 default:
155 {
156 int aval_shift;
157 /*
Aneesh Kumar K.V63fff5c2014-06-29 16:47:30 +0530158 * remaining bits of AVA/LP fields
Aneesh Kumar K.V1f365bb2014-05-06 23:31:36 +0530159 * Also contain the rr bits of LP
160 */
Aneesh Kumar K.V63fff5c2014-06-29 16:47:30 +0530161 rb |= (va_low << mmu_psize_defs[b_psize].shift) & 0x7ff000;
Aneesh Kumar K.V1f365bb2014-05-06 23:31:36 +0530162 /*
163 * Now clear not needed LP bits based on actual psize
164 */
165 rb &= ~((1ul << mmu_psize_defs[a_psize].shift) - 1);
166 /*
167 * AVAL field 58..77 - base_page_shift bits of va
168 * we have space for 58..64 bits, Missing bits should
169 * be zero filled. +1 is to take care of L bit shift
170 */
171 aval_shift = 64 - (77 - mmu_psize_defs[b_psize].shift) + 1;
172 rb |= ((va_low << aval_shift) & 0xfe);
173
174 rb |= 1; /* L field */
175 penc = mmu_psize_defs[b_psize].penc[a_psize];
176 rb |= penc << 12; /* LP field */
177 break;
178 }
Andreas Schwab36cc66d2011-11-08 07:08:52 +0000179 }
180 rb |= (v >> 54) & 0x300; /* B field */
181 return rb;
182}
183
Paul Mackerras06ce2c62011-12-12 12:33:07 +0000184static inline unsigned long hpte_rpn(unsigned long ptel, unsigned long psize)
185{
186 return ((ptel & HPTE_R_RPN) & ~(psize - 1)) >> PAGE_SHIFT;
187}
188
Paul Mackerras4cf302b2011-12-12 12:38:51 +0000189static inline int hpte_is_writable(unsigned long ptel)
190{
191 unsigned long pp = ptel & (HPTE_R_PP0 | HPTE_R_PP);
192
193 return pp != PP_RXRX && pp != PP_RXXX;
194}
195
196static inline unsigned long hpte_make_readonly(unsigned long ptel)
197{
198 if ((ptel & HPTE_R_PP0) || (ptel & HPTE_R_PP) == PP_RWXX)
199 ptel = (ptel & ~HPTE_R_PP) | PP_RXXX;
200 else
201 ptel |= PP_RXRX;
202 return ptel;
203}
204
Aneesh Kumar K.V30bda412016-04-29 23:25:38 +1000205static inline bool hpte_cache_flags_ok(unsigned long hptel, bool is_ci)
Paul Mackerras9d0ef5ea2011-12-12 12:32:27 +0000206{
Aneesh Kumar K.V30bda412016-04-29 23:25:38 +1000207 unsigned int wimg = hptel & HPTE_R_WIMG;
Paul Mackerras9d0ef5ea2011-12-12 12:32:27 +0000208
209 /* Handle SAO */
210 if (wimg == (HPTE_R_W | HPTE_R_I | HPTE_R_M) &&
211 cpu_has_feature(CPU_FTR_ARCH_206))
212 wimg = HPTE_R_M;
213
Aneesh Kumar K.V30bda412016-04-29 23:25:38 +1000214 if (!is_ci)
Paul Mackerras9d0ef5ea2011-12-12 12:32:27 +0000215 return wimg == HPTE_R_M;
Aneesh Kumar K.V30bda412016-04-29 23:25:38 +1000216 /*
217 * if host is mapped cache inhibited, make sure hptel also have
218 * cache inhibited.
219 */
220 if (wimg & HPTE_R_W) /* FIXME!! is this ok for all guest. ? */
221 return false;
222 return !!(wimg & HPTE_R_I);
Paul Mackerras9d0ef5ea2011-12-12 12:32:27 +0000223}
224
Paul Mackerras342d3db2011-12-12 12:38:05 +0000225/*
Aneesh Kumar K.Vdb7cb5b2013-06-20 14:30:19 +0530226 * If it's present and writable, atomically set dirty and referenced bits and
Aneesh Kumar K.V7d6e7f72015-03-30 10:41:04 +0530227 * return the PTE, otherwise return 0.
Paul Mackerras342d3db2011-12-12 12:38:05 +0000228 */
Aneesh Kumar K.V7d6e7f72015-03-30 10:41:04 +0530229static inline pte_t kvmppc_read_update_linux_pte(pte_t *ptep, int writing)
Paul Mackerras342d3db2011-12-12 12:38:05 +0000230{
Aneesh Kumar K.Vdb7cb5b2013-06-20 14:30:19 +0530231 pte_t old_pte, new_pte = __pte(0);
Paul Mackerras342d3db2011-12-12 12:38:05 +0000232
Aneesh Kumar K.Vdb7cb5b2013-06-20 14:30:19 +0530233 while (1) {
Aneesh Kumar K.V5e1d44a2015-03-30 10:39:12 +0530234 /*
235 * Make sure we don't reload from ptep
236 */
237 old_pte = READ_ONCE(*ptep);
Aneesh Kumar K.Vdb7cb5b2013-06-20 14:30:19 +0530238 /*
Aneesh Kumar K.V945537d2016-04-29 23:25:45 +1000239 * wait until H_PAGE_BUSY is clear then set it atomically
Aneesh Kumar K.Vdb7cb5b2013-06-20 14:30:19 +0530240 */
Aneesh Kumar K.V945537d2016-04-29 23:25:45 +1000241 if (unlikely(pte_val(old_pte) & H_PAGE_BUSY)) {
Aneesh Kumar K.Vdb7cb5b2013-06-20 14:30:19 +0530242 cpu_relax();
243 continue;
244 }
Aneesh Kumar K.Vdb7cb5b2013-06-20 14:30:19 +0530245 /* If pte is not present return None */
Michael Ellerman4f9c53c2015-03-25 20:11:57 +1100246 if (unlikely(!(pte_val(old_pte) & _PAGE_PRESENT)))
Aneesh Kumar K.Vdb7cb5b2013-06-20 14:30:19 +0530247 return __pte(0);
Paul Mackerras342d3db2011-12-12 12:38:05 +0000248
Aneesh Kumar K.Vdb7cb5b2013-06-20 14:30:19 +0530249 new_pte = pte_mkyoung(old_pte);
250 if (writing && pte_write(old_pte))
251 new_pte = pte_mkdirty(new_pte);
252
Michael Ellerman3910a7f2016-04-29 23:25:27 +1000253 if (pte_xchg(ptep, old_pte, new_pte))
Aneesh Kumar K.Vdb7cb5b2013-06-20 14:30:19 +0530254 break;
Paul Mackerras342d3db2011-12-12 12:38:05 +0000255 }
Aneesh Kumar K.Vdb7cb5b2013-06-20 14:30:19 +0530256 return new_pte;
Paul Mackerras342d3db2011-12-12 12:38:05 +0000257}
258
Paul Mackerras697d3892011-12-12 12:36:37 +0000259static inline bool hpte_read_permission(unsigned long pp, unsigned long key)
260{
261 if (key)
262 return PP_RWRX <= pp && pp <= PP_RXRX;
Joe Perchesacdb6682015-03-30 16:46:04 -0700263 return true;
Paul Mackerras697d3892011-12-12 12:36:37 +0000264}
265
266static inline bool hpte_write_permission(unsigned long pp, unsigned long key)
267{
268 if (key)
269 return pp == PP_RWRW;
270 return pp <= PP_RWRW;
271}
272
273static inline int hpte_get_skey_perm(unsigned long hpte_r, unsigned long amr)
274{
275 unsigned long skey;
276
277 skey = ((hpte_r & HPTE_R_KEY_HI) >> 57) |
278 ((hpte_r & HPTE_R_KEY_LO) >> 9);
279 return (amr >> (62 - 2 * skey)) & 3;
280}
281
Paul Mackerras06ce2c62011-12-12 12:33:07 +0000282static inline void lock_rmap(unsigned long *rmap)
283{
284 do {
285 while (test_bit(KVMPPC_RMAP_LOCK_BIT, rmap))
286 cpu_relax();
287 } while (test_and_set_bit_lock(KVMPPC_RMAP_LOCK_BIT, rmap));
288}
289
290static inline void unlock_rmap(unsigned long *rmap)
291{
292 __clear_bit_unlock(KVMPPC_RMAP_LOCK_BIT, rmap);
293}
294
Paul Mackerrasda9d1d72011-12-12 12:31:41 +0000295static inline bool slot_is_aligned(struct kvm_memory_slot *memslot,
296 unsigned long pagesize)
297{
298 unsigned long mask = (pagesize >> PAGE_SHIFT) - 1;
299
300 if (pagesize <= PAGE_SIZE)
Joe Perchesacdb6682015-03-30 16:46:04 -0700301 return true;
Paul Mackerrasda9d1d72011-12-12 12:31:41 +0000302 return !(memslot->base_gfn & mask) && !(memslot->npages & mask);
303}
304
Paul Mackerrasa2932922012-11-19 22:57:20 +0000305/*
306 * This works for 4k, 64k and 16M pages on POWER7,
307 * and 4k and 16M pages on PPC970.
308 */
309static inline unsigned long slb_pgsize_encoding(unsigned long psize)
310{
311 unsigned long senc = 0;
312
313 if (psize > 0x1000) {
314 senc = SLB_VSID_L;
315 if (psize == 0x10000)
316 senc |= SLB_VSID_LP_01;
317 }
318 return senc;
319}
320
321static inline int is_vrma_hpte(unsigned long hpte_v)
322{
323 return (hpte_v & ~0xffffffUL) ==
324 (HPTE_V_1TB_SEG | (VRMA_VSID << (40 - 16)));
325}
326
Aneesh Kumar K.V9975f5e2013-10-07 22:17:52 +0530327#ifdef CONFIG_KVM_BOOK3S_HV_POSSIBLE
Paul Mackerrasa1b4a0f2013-04-18 19:50:24 +0000328/*
329 * Note modification of an HPTE; set the HPTE modified bit
330 * if anyone is interested.
331 */
332static inline void note_hpte_modification(struct kvm *kvm,
333 struct revmap_entry *rev)
334{
335 if (atomic_read(&kvm->arch.hpte_mod_interest))
336 rev->guest_rpte |= HPTE_GR_MODIFIED;
337}
Paul Mackerras797f9c02014-03-25 10:47:06 +1100338
339/*
340 * Like kvm_memslots(), but for use in real mode when we can't do
341 * any RCU stuff (since the secondary threads are offline from the
342 * kernel's point of view), and we can't print anything.
343 * Thus we use rcu_dereference_raw() rather than rcu_dereference_check().
344 */
345static inline struct kvm_memslots *kvm_memslots_raw(struct kvm *kvm)
346{
Paolo Bonzinif481b062015-05-17 17:30:37 +0200347 return rcu_dereference_raw_notrace(kvm->memslots[0]);
Paul Mackerras797f9c02014-03-25 10:47:06 +1100348}
349
Paul Mackerrase23a8082015-03-28 14:21:01 +1100350extern void kvmppc_mmu_debugfs_init(struct kvm *kvm);
351
Paul Mackerraseddb60f2015-03-28 14:21:11 +1100352extern void kvmhv_rm_send_ipi(int cpu);
353
Aneesh Kumar K.V9975f5e2013-10-07 22:17:52 +0530354#endif /* CONFIG_KVM_BOOK3S_HV_POSSIBLE */
Paul Mackerrasa1b4a0f2013-04-18 19:50:24 +0000355
Alexander Graf3ae07892010-04-16 00:11:37 +0200356#endif /* __ASM_KVM_BOOK3S_64_H__ */