Merge branch 'kvm-ppc-infrastructure' into kvm-ppc-next
[cascardo/linux.git] / arch / powerpc / include / asm / kvm_book3s_64.h
1 /*
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
23 #include <asm/book3s/64/mmu-hash.h>
24
25 #ifdef CONFIG_KVM_BOOK3S_PR_POSSIBLE
26 static inline struct kvmppc_book3s_shadow_vcpu *svcpu_get(struct kvm_vcpu *vcpu)
27 {
28         preempt_disable();
29         return &get_paca()->shadow_vcpu;
30 }
31
32 static inline void svcpu_put(struct kvmppc_book3s_shadow_vcpu *svcpu)
33 {
34         preempt_enable();
35 }
36 #endif
37
38 #ifdef CONFIG_KVM_BOOK3S_HV_POSSIBLE
39 #define KVM_DEFAULT_HPT_ORDER   24      /* 16MB HPT by default */
40 #endif
41
42 #define VRMA_VSID       0x1ffffffUL     /* 1TB VSID reserved for VRMA */
43
44 /*
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
50 #define HPTE_V_ABSENT   0x20UL
51
52 /*
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
61 static inline long try_lock_hpte(__be64 *hpte, unsigned long bits)
62 {
63         unsigned long tmp, old;
64         __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);
73
74         asm volatile("  ldarx   %0,0,%2\n"
75                      "  and.    %1,%0,%3\n"
76                      "  bne     2f\n"
77                      "  or      %0,%0,%4\n"
78                      "  stdcx.  %0,0,%2\n"
79                      "  beq+    2f\n"
80                      "  mr      %1,%3\n"
81                      "2:        isync"
82                      : "=&r" (tmp), "=&r" (old)
83                      : "r" (hpte), "r" (be_bits), "r" (be_lockbit)
84                      : "cc", "memory");
85         return old == 0;
86 }
87
88 static 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 */
96 static 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
102 static inline unsigned long compute_tlbie_rb(unsigned long v, unsigned long r,
103                                              unsigned long pte_index)
104 {
105         int i, b_psize = MMU_PAGE_4K, a_psize = MMU_PAGE_4K;
106         unsigned int penc;
107         unsigned long rb = 0, va_low, sllp;
108         unsigned int lp = (r >> LP_SHIFT) & ((1 << LP_BITS) - 1);
109
110         if (v & HPTE_V_LARGE) {
111                 i = hpte_page_sizes[lp];
112                 b_psize = i & 0xf;
113                 a_psize = i >> 4;
114         }
115
116         /*
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*/
126         rb = (v & ~0x7fUL) << 16;               /* AVA field */
127
128         rb |= (v >> HPTE_V_SSIZE_SHIFT) << 8;   /*  B field */
129         /*
130          * AVA in v had cleared lower 23 bits. We need to derive
131          * that from pteg index
132          */
133         va_low = pte_index >> 3;
134         if (v & HPTE_V_SECONDARY)
135                 va_low = ~va_low;
136         /*
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          */
142         if (!(v & HPTE_V_1TB_SEG))
143                 va_low ^= v >> (SID_SHIFT - 16);
144         else
145                 va_low ^= v >> (SID_SHIFT_1T - 16);
146         va_low &= 0x7ff;
147
148         switch (b_psize) {
149         case MMU_PAGE_4K:
150                 sllp = get_sllp_encoding(a_psize);
151                 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                 /*
158                  * remaining bits of AVA/LP fields
159                  * Also contain the rr bits of LP
160                  */
161                 rb |= (va_low << mmu_psize_defs[b_psize].shift) & 0x7ff000;
162                 /*
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         }
179         }
180         rb |= (v >> 54) & 0x300;                /* B field */
181         return rb;
182 }
183
184 static inline unsigned long hpte_rpn(unsigned long ptel, unsigned long psize)
185 {
186         return ((ptel & HPTE_R_RPN) & ~(psize - 1)) >> PAGE_SHIFT;
187 }
188
189 static 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
196 static 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
205 static inline bool hpte_cache_flags_ok(unsigned long hptel, bool is_ci)
206 {
207         unsigned int wimg = hptel & HPTE_R_WIMG;
208
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
214         if (!is_ci)
215                 return wimg == HPTE_R_M;
216         /*
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);
223 }
224
225 /*
226  * If it's present and writable, atomically set dirty and referenced bits and
227  * return the PTE, otherwise return 0.
228  */
229 static inline pte_t kvmppc_read_update_linux_pte(pte_t *ptep, int writing)
230 {
231         pte_t old_pte, new_pte = __pte(0);
232
233         while (1) {
234                 /*
235                  * Make sure we don't reload from ptep
236                  */
237                 old_pte = READ_ONCE(*ptep);
238                 /*
239                  * wait until H_PAGE_BUSY is clear then set it atomically
240                  */
241                 if (unlikely(pte_val(old_pte) & H_PAGE_BUSY)) {
242                         cpu_relax();
243                         continue;
244                 }
245                 /* If pte is not present return None */
246                 if (unlikely(!(pte_val(old_pte) & _PAGE_PRESENT)))
247                         return __pte(0);
248
249                 new_pte = pte_mkyoung(old_pte);
250                 if (writing && pte_write(old_pte))
251                         new_pte = pte_mkdirty(new_pte);
252
253                 if (pte_xchg(ptep, old_pte, new_pte))
254                         break;
255         }
256         return new_pte;
257 }
258
259 static inline bool hpte_read_permission(unsigned long pp, unsigned long key)
260 {
261         if (key)
262                 return PP_RWRX <= pp && pp <= PP_RXRX;
263         return true;
264 }
265
266 static 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
273 static 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
282 static 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
290 static inline void unlock_rmap(unsigned long *rmap)
291 {
292         __clear_bit_unlock(KVMPPC_RMAP_LOCK_BIT, rmap);
293 }
294
295 static 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)
301                 return true;
302         return !(memslot->base_gfn & mask) && !(memslot->npages & mask);
303 }
304
305 /*
306  * This works for 4k, 64k and 16M pages on POWER7,
307  * and 4k and 16M pages on PPC970.
308  */
309 static 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
321 static 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
327 #ifdef CONFIG_KVM_BOOK3S_HV_POSSIBLE
328 /*
329  * Note modification of an HPTE; set the HPTE modified bit
330  * if anyone is interested.
331  */
332 static 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 }
338
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  */
345 static inline struct kvm_memslots *kvm_memslots_raw(struct kvm *kvm)
346 {
347         return rcu_dereference_raw_notrace(kvm->memslots[0]);
348 }
349
350 extern void kvmppc_mmu_debugfs_init(struct kvm *kvm);
351
352 extern void kvmhv_rm_send_ipi(int cpu);
353
354 #endif /* CONFIG_KVM_BOOK3S_HV_POSSIBLE */
355
356 #endif /* __ASM_KVM_BOOK3S_64_H__ */