blob: 848292176908da1916f1dd3aeedb5b457e734008 [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
Aneesh Kumar K.V1f365bb2014-05-06 23:31:36 +0530128 /*
129 * AVA in v had cleared lower 23 bits. We need to derive
130 * that from pteg index
131 */
Andreas Schwab36cc66d2011-11-08 07:08:52 +0000132 va_low = pte_index >> 3;
133 if (v & HPTE_V_SECONDARY)
134 va_low = ~va_low;
Aneesh Kumar K.V1f365bb2014-05-06 23:31:36 +0530135 /*
136 * get the vpn bits from va_low using reverse of hashing.
137 * In v we have va with 23 bits dropped and then left shifted
138 * HPTE_V_AVPN_SHIFT (7) bits. Now to find vsid we need
139 * right shift it with (SID_SHIFT - (23 - 7))
140 */
Andreas Schwab36cc66d2011-11-08 07:08:52 +0000141 if (!(v & HPTE_V_1TB_SEG))
Aneesh Kumar K.V1f365bb2014-05-06 23:31:36 +0530142 va_low ^= v >> (SID_SHIFT - 16);
Andreas Schwab36cc66d2011-11-08 07:08:52 +0000143 else
Aneesh Kumar K.V1f365bb2014-05-06 23:31:36 +0530144 va_low ^= v >> (SID_SHIFT_1T - 16);
Andreas Schwab36cc66d2011-11-08 07:08:52 +0000145 va_low &= 0x7ff;
Aneesh Kumar K.V1f365bb2014-05-06 23:31:36 +0530146
147 switch (b_psize) {
148 case MMU_PAGE_4K:
Aneesh Kumar K.V138ee7e2016-07-13 15:06:37 +0530149 sllp = get_sllp_encoding(a_psize);
Aneesh Kumar K.V1f365bb2014-05-06 23:31:36 +0530150 rb |= sllp << 5; /* AP field */
151 rb |= (va_low & 0x7ff) << 12; /* remaining 11 bits of AVA */
152 break;
153 default:
154 {
155 int aval_shift;
156 /*
Aneesh Kumar K.V63fff5c2014-06-29 16:47:30 +0530157 * remaining bits of AVA/LP fields
Aneesh Kumar K.V1f365bb2014-05-06 23:31:36 +0530158 * Also contain the rr bits of LP
159 */
Aneesh Kumar K.V63fff5c2014-06-29 16:47:30 +0530160 rb |= (va_low << mmu_psize_defs[b_psize].shift) & 0x7ff000;
Aneesh Kumar K.V1f365bb2014-05-06 23:31:36 +0530161 /*
162 * Now clear not needed LP bits based on actual psize
163 */
164 rb &= ~((1ul << mmu_psize_defs[a_psize].shift) - 1);
165 /*
166 * AVAL field 58..77 - base_page_shift bits of va
167 * we have space for 58..64 bits, Missing bits should
168 * be zero filled. +1 is to take care of L bit shift
169 */
170 aval_shift = 64 - (77 - mmu_psize_defs[b_psize].shift) + 1;
171 rb |= ((va_low << aval_shift) & 0xfe);
172
173 rb |= 1; /* L field */
174 penc = mmu_psize_defs[b_psize].penc[a_psize];
175 rb |= penc << 12; /* LP field */
176 break;
177 }
Andreas Schwab36cc66d2011-11-08 07:08:52 +0000178 }
Balbir Singh4f053d02016-09-16 17:25:50 +1000179 rb |= (v >> HPTE_V_SSIZE_SHIFT) << 8; /* B field */
Andreas Schwab36cc66d2011-11-08 07:08:52 +0000180 return rb;
181}
182
Paul Mackerras06ce2c62011-12-12 12:33:07 +0000183static inline unsigned long hpte_rpn(unsigned long ptel, unsigned long psize)
184{
185 return ((ptel & HPTE_R_RPN) & ~(psize - 1)) >> PAGE_SHIFT;
186}
187
Paul Mackerras4cf302b2011-12-12 12:38:51 +0000188static inline int hpte_is_writable(unsigned long ptel)
189{
190 unsigned long pp = ptel & (HPTE_R_PP0 | HPTE_R_PP);
191
192 return pp != PP_RXRX && pp != PP_RXXX;
193}
194
195static inline unsigned long hpte_make_readonly(unsigned long ptel)
196{
197 if ((ptel & HPTE_R_PP0) || (ptel & HPTE_R_PP) == PP_RWXX)
198 ptel = (ptel & ~HPTE_R_PP) | PP_RXXX;
199 else
200 ptel |= PP_RXRX;
201 return ptel;
202}
203
Aneesh Kumar K.V30bda412016-04-29 23:25:38 +1000204static inline bool hpte_cache_flags_ok(unsigned long hptel, bool is_ci)
Paul Mackerras9d0ef5ea2011-12-12 12:32:27 +0000205{
Aneesh Kumar K.V30bda412016-04-29 23:25:38 +1000206 unsigned int wimg = hptel & HPTE_R_WIMG;
Paul Mackerras9d0ef5ea2011-12-12 12:32:27 +0000207
208 /* Handle SAO */
209 if (wimg == (HPTE_R_W | HPTE_R_I | HPTE_R_M) &&
210 cpu_has_feature(CPU_FTR_ARCH_206))
211 wimg = HPTE_R_M;
212
Aneesh Kumar K.V30bda412016-04-29 23:25:38 +1000213 if (!is_ci)
Paul Mackerras9d0ef5ea2011-12-12 12:32:27 +0000214 return wimg == HPTE_R_M;
Aneesh Kumar K.V30bda412016-04-29 23:25:38 +1000215 /*
216 * if host is mapped cache inhibited, make sure hptel also have
217 * cache inhibited.
218 */
219 if (wimg & HPTE_R_W) /* FIXME!! is this ok for all guest. ? */
220 return false;
221 return !!(wimg & HPTE_R_I);
Paul Mackerras9d0ef5ea2011-12-12 12:32:27 +0000222}
223
Paul Mackerras342d3db2011-12-12 12:38:05 +0000224/*
Aneesh Kumar K.Vdb7cb5b2013-06-20 14:30:19 +0530225 * If it's present and writable, atomically set dirty and referenced bits and
Aneesh Kumar K.V7d6e7f72015-03-30 10:41:04 +0530226 * return the PTE, otherwise return 0.
Paul Mackerras342d3db2011-12-12 12:38:05 +0000227 */
Aneesh Kumar K.V7d6e7f72015-03-30 10:41:04 +0530228static inline pte_t kvmppc_read_update_linux_pte(pte_t *ptep, int writing)
Paul Mackerras342d3db2011-12-12 12:38:05 +0000229{
Aneesh Kumar K.Vdb7cb5b2013-06-20 14:30:19 +0530230 pte_t old_pte, new_pte = __pte(0);
Paul Mackerras342d3db2011-12-12 12:38:05 +0000231
Aneesh Kumar K.Vdb7cb5b2013-06-20 14:30:19 +0530232 while (1) {
Aneesh Kumar K.V5e1d44a2015-03-30 10:39:12 +0530233 /*
234 * Make sure we don't reload from ptep
235 */
236 old_pte = READ_ONCE(*ptep);
Aneesh Kumar K.Vdb7cb5b2013-06-20 14:30:19 +0530237 /*
Aneesh Kumar K.V945537d2016-04-29 23:25:45 +1000238 * wait until H_PAGE_BUSY is clear then set it atomically
Aneesh Kumar K.Vdb7cb5b2013-06-20 14:30:19 +0530239 */
Aneesh Kumar K.V945537d2016-04-29 23:25:45 +1000240 if (unlikely(pte_val(old_pte) & H_PAGE_BUSY)) {
Aneesh Kumar K.Vdb7cb5b2013-06-20 14:30:19 +0530241 cpu_relax();
242 continue;
243 }
Aneesh Kumar K.Vdb7cb5b2013-06-20 14:30:19 +0530244 /* If pte is not present return None */
Michael Ellerman4f9c53c2015-03-25 20:11:57 +1100245 if (unlikely(!(pte_val(old_pte) & _PAGE_PRESENT)))
Aneesh Kumar K.Vdb7cb5b2013-06-20 14:30:19 +0530246 return __pte(0);
Paul Mackerras342d3db2011-12-12 12:38:05 +0000247
Aneesh Kumar K.Vdb7cb5b2013-06-20 14:30:19 +0530248 new_pte = pte_mkyoung(old_pte);
249 if (writing && pte_write(old_pte))
250 new_pte = pte_mkdirty(new_pte);
251
Michael Ellerman3910a7f2016-04-29 23:25:27 +1000252 if (pte_xchg(ptep, old_pte, new_pte))
Aneesh Kumar K.Vdb7cb5b2013-06-20 14:30:19 +0530253 break;
Paul Mackerras342d3db2011-12-12 12:38:05 +0000254 }
Aneesh Kumar K.Vdb7cb5b2013-06-20 14:30:19 +0530255 return new_pte;
Paul Mackerras342d3db2011-12-12 12:38:05 +0000256}
257
Paul Mackerras697d3892011-12-12 12:36:37 +0000258static inline bool hpte_read_permission(unsigned long pp, unsigned long key)
259{
260 if (key)
261 return PP_RWRX <= pp && pp <= PP_RXRX;
Joe Perchesacdb6682015-03-30 16:46:04 -0700262 return true;
Paul Mackerras697d3892011-12-12 12:36:37 +0000263}
264
265static inline bool hpte_write_permission(unsigned long pp, unsigned long key)
266{
267 if (key)
268 return pp == PP_RWRW;
269 return pp <= PP_RWRW;
270}
271
272static inline int hpte_get_skey_perm(unsigned long hpte_r, unsigned long amr)
273{
274 unsigned long skey;
275
276 skey = ((hpte_r & HPTE_R_KEY_HI) >> 57) |
277 ((hpte_r & HPTE_R_KEY_LO) >> 9);
278 return (amr >> (62 - 2 * skey)) & 3;
279}
280
Paul Mackerras06ce2c62011-12-12 12:33:07 +0000281static inline void lock_rmap(unsigned long *rmap)
282{
283 do {
284 while (test_bit(KVMPPC_RMAP_LOCK_BIT, rmap))
285 cpu_relax();
286 } while (test_and_set_bit_lock(KVMPPC_RMAP_LOCK_BIT, rmap));
287}
288
289static inline void unlock_rmap(unsigned long *rmap)
290{
291 __clear_bit_unlock(KVMPPC_RMAP_LOCK_BIT, rmap);
292}
293
Paul Mackerrasda9d1d72011-12-12 12:31:41 +0000294static inline bool slot_is_aligned(struct kvm_memory_slot *memslot,
295 unsigned long pagesize)
296{
297 unsigned long mask = (pagesize >> PAGE_SHIFT) - 1;
298
299 if (pagesize <= PAGE_SIZE)
Joe Perchesacdb6682015-03-30 16:46:04 -0700300 return true;
Paul Mackerrasda9d1d72011-12-12 12:31:41 +0000301 return !(memslot->base_gfn & mask) && !(memslot->npages & mask);
302}
303
Paul Mackerrasa2932922012-11-19 22:57:20 +0000304/*
305 * This works for 4k, 64k and 16M pages on POWER7,
306 * and 4k and 16M pages on PPC970.
307 */
308static inline unsigned long slb_pgsize_encoding(unsigned long psize)
309{
310 unsigned long senc = 0;
311
312 if (psize > 0x1000) {
313 senc = SLB_VSID_L;
314 if (psize == 0x10000)
315 senc |= SLB_VSID_LP_01;
316 }
317 return senc;
318}
319
320static inline int is_vrma_hpte(unsigned long hpte_v)
321{
322 return (hpte_v & ~0xffffffUL) ==
323 (HPTE_V_1TB_SEG | (VRMA_VSID << (40 - 16)));
324}
325
Aneesh Kumar K.V9975f5e2013-10-07 22:17:52 +0530326#ifdef CONFIG_KVM_BOOK3S_HV_POSSIBLE
Paul Mackerrasa1b4a0f2013-04-18 19:50:24 +0000327/*
328 * Note modification of an HPTE; set the HPTE modified bit
329 * if anyone is interested.
330 */
331static inline void note_hpte_modification(struct kvm *kvm,
332 struct revmap_entry *rev)
333{
334 if (atomic_read(&kvm->arch.hpte_mod_interest))
335 rev->guest_rpte |= HPTE_GR_MODIFIED;
336}
Paul Mackerras797f9c02014-03-25 10:47:06 +1100337
338/*
339 * Like kvm_memslots(), but for use in real mode when we can't do
340 * any RCU stuff (since the secondary threads are offline from the
341 * kernel's point of view), and we can't print anything.
342 * Thus we use rcu_dereference_raw() rather than rcu_dereference_check().
343 */
344static inline struct kvm_memslots *kvm_memslots_raw(struct kvm *kvm)
345{
Paolo Bonzinif481b062015-05-17 17:30:37 +0200346 return rcu_dereference_raw_notrace(kvm->memslots[0]);
Paul Mackerras797f9c02014-03-25 10:47:06 +1100347}
348
Paul Mackerrase23a8082015-03-28 14:21:01 +1100349extern void kvmppc_mmu_debugfs_init(struct kvm *kvm);
350
Paul Mackerraseddb60f2015-03-28 14:21:11 +1100351extern void kvmhv_rm_send_ipi(int cpu);
352
Aneesh Kumar K.V9975f5e2013-10-07 22:17:52 +0530353#endif /* CONFIG_KVM_BOOK3S_HV_POSSIBLE */
Paul Mackerrasa1b4a0f2013-04-18 19:50:24 +0000354
Alexander Graf3ae07892010-04-16 00:11:37 +0200355#endif /* __ASM_KVM_BOOK3S_64_H__ */