blob: 0789a0f5096941d4411eaf6ef333b517efcd759e [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
Aneesh Kumar K.V7aa79932013-10-07 22:17:51 +053023#ifdef CONFIG_KVM_BOOK3S_PR_POSSIBLE
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
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
Aneesh Kumar K.V1f365bb2014-05-06 23:31:36 +0530102static inline int __hpte_actual_psize(unsigned int lp, int psize)
103{
104 int i, shift;
105 unsigned int mask;
106
107 /* start from 1 ignoring MMU_PAGE_4K */
108 for (i = 1; i < MMU_PAGE_COUNT; i++) {
109
110 /* invalid penc */
111 if (mmu_psize_defs[psize].penc[i] == -1)
112 continue;
113 /*
114 * encoding bits per actual page size
115 * PTE LP actual page size
116 * rrrr rrrz >=8KB
117 * rrrr rrzz >=16KB
118 * rrrr rzzz >=32KB
119 * rrrr zzzz >=64KB
120 * .......
121 */
122 shift = mmu_psize_defs[i].shift - LP_SHIFT;
123 if (shift > LP_BITS)
124 shift = LP_BITS;
125 mask = (1 << shift) - 1;
126 if ((lp & mask) == mmu_psize_defs[psize].penc[i])
127 return i;
128 }
129 return -1;
130}
131
Andreas Schwab36cc66d2011-11-08 07:08:52 +0000132static inline unsigned long compute_tlbie_rb(unsigned long v, unsigned long r,
133 unsigned long pte_index)
134{
Alexander Graff6bf3a62014-06-11 17:13:55 +0200135 int b_psize = MMU_PAGE_4K, a_psize = MMU_PAGE_4K;
Aneesh Kumar K.V1f365bb2014-05-06 23:31:36 +0530136 unsigned int penc;
137 unsigned long rb = 0, va_low, sllp;
138 unsigned int lp = (r >> LP_SHIFT) & ((1 << LP_BITS) - 1);
Andreas Schwab36cc66d2011-11-08 07:08:52 +0000139
Alexander Graff6bf3a62014-06-11 17:13:55 +0200140 if (v & HPTE_V_LARGE) {
Aneesh Kumar K.V1f365bb2014-05-06 23:31:36 +0530141 for (b_psize = 0; b_psize < MMU_PAGE_COUNT; b_psize++) {
142
143 /* valid entries have a shift value */
144 if (!mmu_psize_defs[b_psize].shift)
145 continue;
146
147 a_psize = __hpte_actual_psize(lp, b_psize);
148 if (a_psize != -1)
149 break;
150 }
151 }
152 /*
153 * Ignore the top 14 bits of va
154 * v have top two bits covering segment size, hence move
155 * by 16 bits, Also clear the lower HPTE_V_AVPN_SHIFT (7) bits.
156 * AVA field in v also have the lower 23 bits ignored.
157 * For base page size 4K we need 14 .. 65 bits (so need to
158 * collect extra 11 bits)
159 * For others we need 14..14+i
160 */
161 /* This covers 14..54 bits of va*/
Andreas Schwab36cc66d2011-11-08 07:08:52 +0000162 rb = (v & ~0x7fUL) << 16; /* AVA field */
Aneesh Kumar K.V63fff5c2014-06-29 16:47:30 +0530163
Paul Mackerrasd5067352014-11-03 15:51:56 +1100164 rb |= (v >> HPTE_V_SSIZE_SHIFT) << 8; /* B field */
Aneesh Kumar K.V1f365bb2014-05-06 23:31:36 +0530165 /*
166 * AVA in v had cleared lower 23 bits. We need to derive
167 * that from pteg index
168 */
Andreas Schwab36cc66d2011-11-08 07:08:52 +0000169 va_low = pte_index >> 3;
170 if (v & HPTE_V_SECONDARY)
171 va_low = ~va_low;
Aneesh Kumar K.V1f365bb2014-05-06 23:31:36 +0530172 /*
173 * get the vpn bits from va_low using reverse of hashing.
174 * In v we have va with 23 bits dropped and then left shifted
175 * HPTE_V_AVPN_SHIFT (7) bits. Now to find vsid we need
176 * right shift it with (SID_SHIFT - (23 - 7))
177 */
Andreas Schwab36cc66d2011-11-08 07:08:52 +0000178 if (!(v & HPTE_V_1TB_SEG))
Aneesh Kumar K.V1f365bb2014-05-06 23:31:36 +0530179 va_low ^= v >> (SID_SHIFT - 16);
Andreas Schwab36cc66d2011-11-08 07:08:52 +0000180 else
Aneesh Kumar K.V1f365bb2014-05-06 23:31:36 +0530181 va_low ^= v >> (SID_SHIFT_1T - 16);
Andreas Schwab36cc66d2011-11-08 07:08:52 +0000182 va_low &= 0x7ff;
Aneesh Kumar K.V1f365bb2014-05-06 23:31:36 +0530183
184 switch (b_psize) {
185 case MMU_PAGE_4K:
186 sllp = ((mmu_psize_defs[a_psize].sllp & SLB_VSID_L) >> 6) |
187 ((mmu_psize_defs[a_psize].sllp & SLB_VSID_LP) >> 4);
188 rb |= sllp << 5; /* AP field */
189 rb |= (va_low & 0x7ff) << 12; /* remaining 11 bits of AVA */
190 break;
191 default:
192 {
193 int aval_shift;
194 /*
Aneesh Kumar K.V63fff5c2014-06-29 16:47:30 +0530195 * remaining bits of AVA/LP fields
Aneesh Kumar K.V1f365bb2014-05-06 23:31:36 +0530196 * Also contain the rr bits of LP
197 */
Aneesh Kumar K.V63fff5c2014-06-29 16:47:30 +0530198 rb |= (va_low << mmu_psize_defs[b_psize].shift) & 0x7ff000;
Aneesh Kumar K.V1f365bb2014-05-06 23:31:36 +0530199 /*
200 * Now clear not needed LP bits based on actual psize
201 */
202 rb &= ~((1ul << mmu_psize_defs[a_psize].shift) - 1);
203 /*
204 * AVAL field 58..77 - base_page_shift bits of va
205 * we have space for 58..64 bits, Missing bits should
206 * be zero filled. +1 is to take care of L bit shift
207 */
208 aval_shift = 64 - (77 - mmu_psize_defs[b_psize].shift) + 1;
209 rb |= ((va_low << aval_shift) & 0xfe);
210
211 rb |= 1; /* L field */
212 penc = mmu_psize_defs[b_psize].penc[a_psize];
213 rb |= penc << 12; /* LP field */
214 break;
215 }
Andreas Schwab36cc66d2011-11-08 07:08:52 +0000216 }
217 rb |= (v >> 54) & 0x300; /* B field */
218 return rb;
219}
220
Aneesh Kumar K.V341acbb32014-06-16 00:17:07 +0530221static inline unsigned long __hpte_page_size(unsigned long h, unsigned long l,
222 bool is_base_size)
Paul Mackerrasc77162d2011-12-12 12:31:00 +0000223{
Aneesh Kumar K.V341acbb32014-06-16 00:17:07 +0530224
Aneesh Kumar K.V1f365bb2014-05-06 23:31:36 +0530225 int size, a_psize;
226 /* Look at the 8 bit LP value */
227 unsigned int lp = (l >> LP_SHIFT) & ((1 << LP_BITS) - 1);
228
Paul Mackerrasc77162d2011-12-12 12:31:00 +0000229 /* only handle 4k, 64k and 16M pages for now */
230 if (!(h & HPTE_V_LARGE))
Aneesh Kumar K.V1f365bb2014-05-06 23:31:36 +0530231 return 1ul << 12;
232 else {
233 for (size = 0; size < MMU_PAGE_COUNT; size++) {
234 /* valid entries have a shift value */
235 if (!mmu_psize_defs[size].shift)
236 continue;
237
238 a_psize = __hpte_actual_psize(lp, size);
Aneesh Kumar K.V341acbb32014-06-16 00:17:07 +0530239 if (a_psize != -1) {
240 if (is_base_size)
241 return 1ul << mmu_psize_defs[size].shift;
Aneesh Kumar K.V1f365bb2014-05-06 23:31:36 +0530242 return 1ul << mmu_psize_defs[a_psize].shift;
Aneesh Kumar K.V341acbb32014-06-16 00:17:07 +0530243 }
Aneesh Kumar K.V1f365bb2014-05-06 23:31:36 +0530244 }
245
246 }
247 return 0;
Paul Mackerrasc77162d2011-12-12 12:31:00 +0000248}
249
Aneesh Kumar K.V341acbb32014-06-16 00:17:07 +0530250static inline unsigned long hpte_page_size(unsigned long h, unsigned long l)
251{
252 return __hpte_page_size(h, l, 0);
253}
254
255static inline unsigned long hpte_base_page_size(unsigned long h, unsigned long l)
256{
257 return __hpte_page_size(h, l, 1);
258}
259
Paul Mackerras06ce2c62011-12-12 12:33:07 +0000260static inline unsigned long hpte_rpn(unsigned long ptel, unsigned long psize)
261{
262 return ((ptel & HPTE_R_RPN) & ~(psize - 1)) >> PAGE_SHIFT;
263}
264
Paul Mackerras4cf302b2011-12-12 12:38:51 +0000265static inline int hpte_is_writable(unsigned long ptel)
266{
267 unsigned long pp = ptel & (HPTE_R_PP0 | HPTE_R_PP);
268
269 return pp != PP_RXRX && pp != PP_RXXX;
270}
271
272static inline unsigned long hpte_make_readonly(unsigned long ptel)
273{
274 if ((ptel & HPTE_R_PP0) || (ptel & HPTE_R_PP) == PP_RWXX)
275 ptel = (ptel & ~HPTE_R_PP) | PP_RXXX;
276 else
277 ptel |= PP_RXRX;
278 return ptel;
279}
280
Paul Mackerras9d0ef5ea2011-12-12 12:32:27 +0000281static inline int hpte_cache_flags_ok(unsigned long ptel, unsigned long io_type)
282{
283 unsigned int wimg = ptel & HPTE_R_WIMG;
284
285 /* Handle SAO */
286 if (wimg == (HPTE_R_W | HPTE_R_I | HPTE_R_M) &&
287 cpu_has_feature(CPU_FTR_ARCH_206))
288 wimg = HPTE_R_M;
289
290 if (!io_type)
291 return wimg == HPTE_R_M;
292
293 return (wimg & (HPTE_R_W | HPTE_R_I)) == io_type;
294}
295
Paul Mackerras342d3db2011-12-12 12:38:05 +0000296/*
Aneesh Kumar K.Vdb7cb5b2013-06-20 14:30:19 +0530297 * If it's present and writable, atomically set dirty and referenced bits and
298 * return the PTE, otherwise return 0. If we find a transparent hugepage
299 * and if it is marked splitting we return 0;
Paul Mackerras342d3db2011-12-12 12:38:05 +0000300 */
Aneesh Kumar K.Vdb7cb5b2013-06-20 14:30:19 +0530301static inline pte_t kvmppc_read_update_linux_pte(pte_t *ptep, int writing,
302 unsigned int hugepage)
Paul Mackerras342d3db2011-12-12 12:38:05 +0000303{
Aneesh Kumar K.Vdb7cb5b2013-06-20 14:30:19 +0530304 pte_t old_pte, new_pte = __pte(0);
Paul Mackerras342d3db2011-12-12 12:38:05 +0000305
Aneesh Kumar K.Vdb7cb5b2013-06-20 14:30:19 +0530306 while (1) {
307 old_pte = pte_val(*ptep);
308 /*
309 * wait until _PAGE_BUSY is clear then set it atomically
310 */
311 if (unlikely(old_pte & _PAGE_BUSY)) {
312 cpu_relax();
313 continue;
314 }
315#ifdef CONFIG_TRANSPARENT_HUGEPAGE
316 /* If hugepage and is trans splitting return None */
317 if (unlikely(hugepage &&
318 pmd_trans_splitting(pte_pmd(old_pte))))
319 return __pte(0);
320#endif
321 /* If pte is not present return None */
322 if (unlikely(!(old_pte & _PAGE_PRESENT)))
323 return __pte(0);
Paul Mackerras342d3db2011-12-12 12:38:05 +0000324
Aneesh Kumar K.Vdb7cb5b2013-06-20 14:30:19 +0530325 new_pte = pte_mkyoung(old_pte);
326 if (writing && pte_write(old_pte))
327 new_pte = pte_mkdirty(new_pte);
328
329 if (old_pte == __cmpxchg_u64((unsigned long *)ptep, old_pte,
330 new_pte))
331 break;
Paul Mackerras342d3db2011-12-12 12:38:05 +0000332 }
Aneesh Kumar K.Vdb7cb5b2013-06-20 14:30:19 +0530333 return new_pte;
Paul Mackerras342d3db2011-12-12 12:38:05 +0000334}
335
Aneesh Kumar K.Vdb7cb5b2013-06-20 14:30:19 +0530336
Paul Mackerras9d0ef5ea2011-12-12 12:32:27 +0000337/* Return HPTE cache control bits corresponding to Linux pte bits */
338static inline unsigned long hpte_cache_bits(unsigned long pte_val)
339{
340#if _PAGE_NO_CACHE == HPTE_R_I && _PAGE_WRITETHRU == HPTE_R_W
341 return pte_val & (HPTE_R_W | HPTE_R_I);
342#else
343 return ((pte_val & _PAGE_NO_CACHE) ? HPTE_R_I : 0) +
344 ((pte_val & _PAGE_WRITETHRU) ? HPTE_R_W : 0);
345#endif
346}
347
Paul Mackerras697d3892011-12-12 12:36:37 +0000348static inline bool hpte_read_permission(unsigned long pp, unsigned long key)
349{
350 if (key)
351 return PP_RWRX <= pp && pp <= PP_RXRX;
352 return 1;
353}
354
355static inline bool hpte_write_permission(unsigned long pp, unsigned long key)
356{
357 if (key)
358 return pp == PP_RWRW;
359 return pp <= PP_RWRW;
360}
361
362static inline int hpte_get_skey_perm(unsigned long hpte_r, unsigned long amr)
363{
364 unsigned long skey;
365
366 skey = ((hpte_r & HPTE_R_KEY_HI) >> 57) |
367 ((hpte_r & HPTE_R_KEY_LO) >> 9);
368 return (amr >> (62 - 2 * skey)) & 3;
369}
370
Paul Mackerras06ce2c62011-12-12 12:33:07 +0000371static inline void lock_rmap(unsigned long *rmap)
372{
373 do {
374 while (test_bit(KVMPPC_RMAP_LOCK_BIT, rmap))
375 cpu_relax();
376 } while (test_and_set_bit_lock(KVMPPC_RMAP_LOCK_BIT, rmap));
377}
378
379static inline void unlock_rmap(unsigned long *rmap)
380{
381 __clear_bit_unlock(KVMPPC_RMAP_LOCK_BIT, rmap);
382}
383
Paul Mackerrasda9d1d72011-12-12 12:31:41 +0000384static inline bool slot_is_aligned(struct kvm_memory_slot *memslot,
385 unsigned long pagesize)
386{
387 unsigned long mask = (pagesize >> PAGE_SHIFT) - 1;
388
389 if (pagesize <= PAGE_SIZE)
390 return 1;
391 return !(memslot->base_gfn & mask) && !(memslot->npages & mask);
392}
393
Paul Mackerrasa2932922012-11-19 22:57:20 +0000394/*
395 * This works for 4k, 64k and 16M pages on POWER7,
396 * and 4k and 16M pages on PPC970.
397 */
398static inline unsigned long slb_pgsize_encoding(unsigned long psize)
399{
400 unsigned long senc = 0;
401
402 if (psize > 0x1000) {
403 senc = SLB_VSID_L;
404 if (psize == 0x10000)
405 senc |= SLB_VSID_LP_01;
406 }
407 return senc;
408}
409
410static inline int is_vrma_hpte(unsigned long hpte_v)
411{
412 return (hpte_v & ~0xffffffUL) ==
413 (HPTE_V_1TB_SEG | (VRMA_VSID << (40 - 16)));
414}
415
Aneesh Kumar K.V9975f5e2013-10-07 22:17:52 +0530416#ifdef CONFIG_KVM_BOOK3S_HV_POSSIBLE
Paul Mackerrasa1b4a0f2013-04-18 19:50:24 +0000417/*
418 * Note modification of an HPTE; set the HPTE modified bit
419 * if anyone is interested.
420 */
421static inline void note_hpte_modification(struct kvm *kvm,
422 struct revmap_entry *rev)
423{
424 if (atomic_read(&kvm->arch.hpte_mod_interest))
425 rev->guest_rpte |= HPTE_GR_MODIFIED;
426}
Paul Mackerras797f9c02014-03-25 10:47:06 +1100427
428/*
429 * Like kvm_memslots(), but for use in real mode when we can't do
430 * any RCU stuff (since the secondary threads are offline from the
431 * kernel's point of view), and we can't print anything.
432 * Thus we use rcu_dereference_raw() rather than rcu_dereference_check().
433 */
434static inline struct kvm_memslots *kvm_memslots_raw(struct kvm *kvm)
435{
436 return rcu_dereference_raw_notrace(kvm->memslots);
437}
438
Aneesh Kumar K.V9975f5e2013-10-07 22:17:52 +0530439#endif /* CONFIG_KVM_BOOK3S_HV_POSSIBLE */
Paul Mackerrasa1b4a0f2013-04-18 19:50:24 +0000440
Alexander Graf3ae07892010-04-16 00:11:37 +0200441#endif /* __ASM_KVM_BOOK3S_64_H__ */