c2a9146ee016a6e446212bd2ff1c44df1df523ce
[sfrench/cifs-2.6.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 <linux/string.h>
24 #include <asm/bitops.h>
25 #include <asm/book3s/64/mmu-hash.h>
26 #include <asm/cpu_has_feature.h>
27
28 #ifdef CONFIG_PPC_PSERIES
29 static inline bool kvmhv_on_pseries(void)
30 {
31         return !cpu_has_feature(CPU_FTR_HVMODE);
32 }
33 #else
34 static inline bool kvmhv_on_pseries(void)
35 {
36         return false;
37 }
38 #endif
39
40 /*
41  * Structure for a nested guest, that is, for a guest that is managed by
42  * one of our guests.
43  */
44 struct kvm_nested_guest {
45         struct kvm *l1_host;            /* L1 VM that owns this nested guest */
46         int l1_lpid;                    /* lpid L1 guest thinks this guest is */
47         int shadow_lpid;                /* real lpid of this nested guest */
48         pgd_t *shadow_pgtable;          /* our page table for this guest */
49         u64 l1_gr_to_hr;                /* L1's addr of part'n-scoped table */
50         u64 process_table;              /* process table entry for this guest */
51         long refcnt;                    /* number of pointers to this struct */
52         struct mutex tlb_lock;          /* serialize page faults and tlbies */
53         struct kvm_nested_guest *next;
54 };
55
56 /*
57  * We define a nested rmap entry as a single 64-bit quantity
58  * 0xFFF0000000000000   12-bit lpid field
59  * 0x000FFFFFFFFFF000   40-bit guest 4k page frame number
60  * 0x0000000000000001   1-bit  single entry flag
61  */
62 #define RMAP_NESTED_LPID_MASK           0xFFF0000000000000UL
63 #define RMAP_NESTED_LPID_SHIFT          (52)
64 #define RMAP_NESTED_GPA_MASK            0x000FFFFFFFFFF000UL
65 #define RMAP_NESTED_IS_SINGLE_ENTRY     0x0000000000000001UL
66
67 /* Structure for a nested guest rmap entry */
68 struct rmap_nested {
69         struct llist_node list;
70         u64 rmap;
71 };
72
73 /*
74  * for_each_nest_rmap_safe - iterate over the list of nested rmap entries
75  *                           safe against removal of the list entry or NULL list
76  * @pos:        a (struct rmap_nested *) to use as a loop cursor
77  * @node:       pointer to the first entry
78  *              NOTE: this can be NULL
79  * @rmapp:      an (unsigned long *) in which to return the rmap entries on each
80  *              iteration
81  *              NOTE: this must point to already allocated memory
82  *
83  * The nested_rmap is a llist of (struct rmap_nested) entries pointed to by the
84  * rmap entry in the memslot. The list is always terminated by a "single entry"
85  * stored in the list element of the final entry of the llist. If there is ONLY
86  * a single entry then this is itself in the rmap entry of the memslot, not a
87  * llist head pointer.
88  *
89  * Note that the iterator below assumes that a nested rmap entry is always
90  * non-zero.  This is true for our usage because the LPID field is always
91  * non-zero (zero is reserved for the host).
92  *
93  * This should be used to iterate over the list of rmap_nested entries with
94  * processing done on the u64 rmap value given by each iteration. This is safe
95  * against removal of list entries and it is always safe to call free on (pos).
96  *
97  * e.g.
98  * struct rmap_nested *cursor;
99  * struct llist_node *first;
100  * unsigned long rmap;
101  * for_each_nest_rmap_safe(cursor, first, &rmap) {
102  *      do_something(rmap);
103  *      free(cursor);
104  * }
105  */
106 #define for_each_nest_rmap_safe(pos, node, rmapp)                              \
107         for ((pos) = llist_entry((node), typeof(*(pos)), list);                \
108              (node) &&                                                         \
109              (*(rmapp) = ((RMAP_NESTED_IS_SINGLE_ENTRY & ((u64) (node))) ?     \
110                           ((u64) (node)) : ((pos)->rmap))) &&                  \
111              (((node) = ((RMAP_NESTED_IS_SINGLE_ENTRY & ((u64) (node))) ?      \
112                          ((struct llist_node *) ((pos) = NULL)) :              \
113                          (pos)->list.next)), true);                            \
114              (pos) = llist_entry((node), typeof(*(pos)), list))
115
116 struct kvm_nested_guest *kvmhv_get_nested(struct kvm *kvm, int l1_lpid,
117                                           bool create);
118 void kvmhv_put_nested(struct kvm_nested_guest *gp);
119
120 /* Power architecture requires HPT is at least 256kiB, at most 64TiB */
121 #define PPC_MIN_HPT_ORDER       18
122 #define PPC_MAX_HPT_ORDER       46
123
124 #ifdef CONFIG_KVM_BOOK3S_PR_POSSIBLE
125 static inline struct kvmppc_book3s_shadow_vcpu *svcpu_get(struct kvm_vcpu *vcpu)
126 {
127         preempt_disable();
128         return &get_paca()->shadow_vcpu;
129 }
130
131 static inline void svcpu_put(struct kvmppc_book3s_shadow_vcpu *svcpu)
132 {
133         preempt_enable();
134 }
135 #endif
136
137 #ifdef CONFIG_KVM_BOOK3S_HV_POSSIBLE
138
139 static inline bool kvm_is_radix(struct kvm *kvm)
140 {
141         return kvm->arch.radix;
142 }
143
144 #define KVM_DEFAULT_HPT_ORDER   24      /* 16MB HPT by default */
145 #endif
146
147 /*
148  * We use a lock bit in HPTE dword 0 to synchronize updates and
149  * accesses to each HPTE, and another bit to indicate non-present
150  * HPTEs.
151  */
152 #define HPTE_V_HVLOCK   0x40UL
153 #define HPTE_V_ABSENT   0x20UL
154
155 /*
156  * We use this bit in the guest_rpte field of the revmap entry
157  * to indicate a modified HPTE.
158  */
159 #define HPTE_GR_MODIFIED        (1ul << 62)
160
161 /* These bits are reserved in the guest view of the HPTE */
162 #define HPTE_GR_RESERVED        HPTE_GR_MODIFIED
163
164 static inline long try_lock_hpte(__be64 *hpte, unsigned long bits)
165 {
166         unsigned long tmp, old;
167         __be64 be_lockbit, be_bits;
168
169         /*
170          * We load/store in native endian, but the HTAB is in big endian. If
171          * we byte swap all data we apply on the PTE we're implicitly correct
172          * again.
173          */
174         be_lockbit = cpu_to_be64(HPTE_V_HVLOCK);
175         be_bits = cpu_to_be64(bits);
176
177         asm volatile("  ldarx   %0,0,%2\n"
178                      "  and.    %1,%0,%3\n"
179                      "  bne     2f\n"
180                      "  or      %0,%0,%4\n"
181                      "  stdcx.  %0,0,%2\n"
182                      "  beq+    2f\n"
183                      "  mr      %1,%3\n"
184                      "2:        isync"
185                      : "=&r" (tmp), "=&r" (old)
186                      : "r" (hpte), "r" (be_bits), "r" (be_lockbit)
187                      : "cc", "memory");
188         return old == 0;
189 }
190
191 static inline void unlock_hpte(__be64 *hpte, unsigned long hpte_v)
192 {
193         hpte_v &= ~HPTE_V_HVLOCK;
194         asm volatile(PPC_RELEASE_BARRIER "" : : : "memory");
195         hpte[0] = cpu_to_be64(hpte_v);
196 }
197
198 /* Without barrier */
199 static inline void __unlock_hpte(__be64 *hpte, unsigned long hpte_v)
200 {
201         hpte_v &= ~HPTE_V_HVLOCK;
202         hpte[0] = cpu_to_be64(hpte_v);
203 }
204
205 /*
206  * These functions encode knowledge of the POWER7/8/9 hardware
207  * interpretations of the HPTE LP (large page size) field.
208  */
209 static inline int kvmppc_hpte_page_shifts(unsigned long h, unsigned long l)
210 {
211         unsigned int lphi;
212
213         if (!(h & HPTE_V_LARGE))
214                 return 12;      /* 4kB */
215         lphi = (l >> 16) & 0xf;
216         switch ((l >> 12) & 0xf) {
217         case 0:
218                 return !lphi ? 24 : 0;          /* 16MB */
219                 break;
220         case 1:
221                 return 16;                      /* 64kB */
222                 break;
223         case 3:
224                 return !lphi ? 34 : 0;          /* 16GB */
225                 break;
226         case 7:
227                 return (16 << 8) + 12;          /* 64kB in 4kB */
228                 break;
229         case 8:
230                 if (!lphi)
231                         return (24 << 8) + 16;  /* 16MB in 64kkB */
232                 if (lphi == 3)
233                         return (24 << 8) + 12;  /* 16MB in 4kB */
234                 break;
235         }
236         return 0;
237 }
238
239 static inline int kvmppc_hpte_base_page_shift(unsigned long h, unsigned long l)
240 {
241         return kvmppc_hpte_page_shifts(h, l) & 0xff;
242 }
243
244 static inline int kvmppc_hpte_actual_page_shift(unsigned long h, unsigned long l)
245 {
246         int tmp = kvmppc_hpte_page_shifts(h, l);
247
248         if (tmp >= 0x100)
249                 tmp >>= 8;
250         return tmp;
251 }
252
253 static inline unsigned long kvmppc_actual_pgsz(unsigned long v, unsigned long r)
254 {
255         int shift = kvmppc_hpte_actual_page_shift(v, r);
256
257         if (shift)
258                 return 1ul << shift;
259         return 0;
260 }
261
262 static inline int kvmppc_pgsize_lp_encoding(int base_shift, int actual_shift)
263 {
264         switch (base_shift) {
265         case 12:
266                 switch (actual_shift) {
267                 case 12:
268                         return 0;
269                 case 16:
270                         return 7;
271                 case 24:
272                         return 0x38;
273                 }
274                 break;
275         case 16:
276                 switch (actual_shift) {
277                 case 16:
278                         return 1;
279                 case 24:
280                         return 8;
281                 }
282                 break;
283         case 24:
284                 return 0;
285         }
286         return -1;
287 }
288
289 static inline unsigned long compute_tlbie_rb(unsigned long v, unsigned long r,
290                                              unsigned long pte_index)
291 {
292         int a_pgshift, b_pgshift;
293         unsigned long rb = 0, va_low, sllp;
294
295         b_pgshift = a_pgshift = kvmppc_hpte_page_shifts(v, r);
296         if (a_pgshift >= 0x100) {
297                 b_pgshift &= 0xff;
298                 a_pgshift >>= 8;
299         }
300
301         /*
302          * Ignore the top 14 bits of va
303          * v have top two bits covering segment size, hence move
304          * by 16 bits, Also clear the lower HPTE_V_AVPN_SHIFT (7) bits.
305          * AVA field in v also have the lower 23 bits ignored.
306          * For base page size 4K we need 14 .. 65 bits (so need to
307          * collect extra 11 bits)
308          * For others we need 14..14+i
309          */
310         /* This covers 14..54 bits of va*/
311         rb = (v & ~0x7fUL) << 16;               /* AVA field */
312
313         /*
314          * AVA in v had cleared lower 23 bits. We need to derive
315          * that from pteg index
316          */
317         va_low = pte_index >> 3;
318         if (v & HPTE_V_SECONDARY)
319                 va_low = ~va_low;
320         /*
321          * get the vpn bits from va_low using reverse of hashing.
322          * In v we have va with 23 bits dropped and then left shifted
323          * HPTE_V_AVPN_SHIFT (7) bits. Now to find vsid we need
324          * right shift it with (SID_SHIFT - (23 - 7))
325          */
326         if (!(v & HPTE_V_1TB_SEG))
327                 va_low ^= v >> (SID_SHIFT - 16);
328         else
329                 va_low ^= v >> (SID_SHIFT_1T - 16);
330         va_low &= 0x7ff;
331
332         if (b_pgshift <= 12) {
333                 if (a_pgshift > 12) {
334                         sllp = (a_pgshift == 16) ? 5 : 4;
335                         rb |= sllp << 5;        /*  AP field */
336                 }
337                 rb |= (va_low & 0x7ff) << 12;   /* remaining 11 bits of AVA */
338         } else {
339                 int aval_shift;
340                 /*
341                  * remaining bits of AVA/LP fields
342                  * Also contain the rr bits of LP
343                  */
344                 rb |= (va_low << b_pgshift) & 0x7ff000;
345                 /*
346                  * Now clear not needed LP bits based on actual psize
347                  */
348                 rb &= ~((1ul << a_pgshift) - 1);
349                 /*
350                  * AVAL field 58..77 - base_page_shift bits of va
351                  * we have space for 58..64 bits, Missing bits should
352                  * be zero filled. +1 is to take care of L bit shift
353                  */
354                 aval_shift = 64 - (77 - b_pgshift) + 1;
355                 rb |= ((va_low << aval_shift) & 0xfe);
356
357                 rb |= 1;                /* L field */
358                 rb |= r & 0xff000 & ((1ul << a_pgshift) - 1); /* LP field */
359         }
360         rb |= (v >> HPTE_V_SSIZE_SHIFT) << 8;   /* B field */
361         return rb;
362 }
363
364 static inline unsigned long hpte_rpn(unsigned long ptel, unsigned long psize)
365 {
366         return ((ptel & HPTE_R_RPN) & ~(psize - 1)) >> PAGE_SHIFT;
367 }
368
369 static inline int hpte_is_writable(unsigned long ptel)
370 {
371         unsigned long pp = ptel & (HPTE_R_PP0 | HPTE_R_PP);
372
373         return pp != PP_RXRX && pp != PP_RXXX;
374 }
375
376 static inline unsigned long hpte_make_readonly(unsigned long ptel)
377 {
378         if ((ptel & HPTE_R_PP0) || (ptel & HPTE_R_PP) == PP_RWXX)
379                 ptel = (ptel & ~HPTE_R_PP) | PP_RXXX;
380         else
381                 ptel |= PP_RXRX;
382         return ptel;
383 }
384
385 static inline bool hpte_cache_flags_ok(unsigned long hptel, bool is_ci)
386 {
387         unsigned int wimg = hptel & HPTE_R_WIMG;
388
389         /* Handle SAO */
390         if (wimg == (HPTE_R_W | HPTE_R_I | HPTE_R_M) &&
391             cpu_has_feature(CPU_FTR_ARCH_206))
392                 wimg = HPTE_R_M;
393
394         if (!is_ci)
395                 return wimg == HPTE_R_M;
396         /*
397          * if host is mapped cache inhibited, make sure hptel also have
398          * cache inhibited.
399          */
400         if (wimg & HPTE_R_W) /* FIXME!! is this ok for all guest. ? */
401                 return false;
402         return !!(wimg & HPTE_R_I);
403 }
404
405 /*
406  * If it's present and writable, atomically set dirty and referenced bits and
407  * return the PTE, otherwise return 0.
408  */
409 static inline pte_t kvmppc_read_update_linux_pte(pte_t *ptep, int writing)
410 {
411         pte_t old_pte, new_pte = __pte(0);
412
413         while (1) {
414                 /*
415                  * Make sure we don't reload from ptep
416                  */
417                 old_pte = READ_ONCE(*ptep);
418                 /*
419                  * wait until H_PAGE_BUSY is clear then set it atomically
420                  */
421                 if (unlikely(pte_val(old_pte) & H_PAGE_BUSY)) {
422                         cpu_relax();
423                         continue;
424                 }
425                 /* If pte is not present return None */
426                 if (unlikely(!(pte_val(old_pte) & _PAGE_PRESENT)))
427                         return __pte(0);
428
429                 new_pte = pte_mkyoung(old_pte);
430                 if (writing && pte_write(old_pte))
431                         new_pte = pte_mkdirty(new_pte);
432
433                 if (pte_xchg(ptep, old_pte, new_pte))
434                         break;
435         }
436         return new_pte;
437 }
438
439 static inline bool hpte_read_permission(unsigned long pp, unsigned long key)
440 {
441         if (key)
442                 return PP_RWRX <= pp && pp <= PP_RXRX;
443         return true;
444 }
445
446 static inline bool hpte_write_permission(unsigned long pp, unsigned long key)
447 {
448         if (key)
449                 return pp == PP_RWRW;
450         return pp <= PP_RWRW;
451 }
452
453 static inline int hpte_get_skey_perm(unsigned long hpte_r, unsigned long amr)
454 {
455         unsigned long skey;
456
457         skey = ((hpte_r & HPTE_R_KEY_HI) >> 57) |
458                 ((hpte_r & HPTE_R_KEY_LO) >> 9);
459         return (amr >> (62 - 2 * skey)) & 3;
460 }
461
462 static inline void lock_rmap(unsigned long *rmap)
463 {
464         do {
465                 while (test_bit(KVMPPC_RMAP_LOCK_BIT, rmap))
466                         cpu_relax();
467         } while (test_and_set_bit_lock(KVMPPC_RMAP_LOCK_BIT, rmap));
468 }
469
470 static inline void unlock_rmap(unsigned long *rmap)
471 {
472         __clear_bit_unlock(KVMPPC_RMAP_LOCK_BIT, rmap);
473 }
474
475 static inline bool slot_is_aligned(struct kvm_memory_slot *memslot,
476                                    unsigned long pagesize)
477 {
478         unsigned long mask = (pagesize >> PAGE_SHIFT) - 1;
479
480         if (pagesize <= PAGE_SIZE)
481                 return true;
482         return !(memslot->base_gfn & mask) && !(memslot->npages & mask);
483 }
484
485 /*
486  * This works for 4k, 64k and 16M pages on POWER7,
487  * and 4k and 16M pages on PPC970.
488  */
489 static inline unsigned long slb_pgsize_encoding(unsigned long psize)
490 {
491         unsigned long senc = 0;
492
493         if (psize > 0x1000) {
494                 senc = SLB_VSID_L;
495                 if (psize == 0x10000)
496                         senc |= SLB_VSID_LP_01;
497         }
498         return senc;
499 }
500
501 static inline int is_vrma_hpte(unsigned long hpte_v)
502 {
503         return (hpte_v & ~0xffffffUL) ==
504                 (HPTE_V_1TB_SEG | (VRMA_VSID << (40 - 16)));
505 }
506
507 #ifdef CONFIG_KVM_BOOK3S_HV_POSSIBLE
508 /*
509  * Note modification of an HPTE; set the HPTE modified bit
510  * if anyone is interested.
511  */
512 static inline void note_hpte_modification(struct kvm *kvm,
513                                           struct revmap_entry *rev)
514 {
515         if (atomic_read(&kvm->arch.hpte_mod_interest))
516                 rev->guest_rpte |= HPTE_GR_MODIFIED;
517 }
518
519 /*
520  * Like kvm_memslots(), but for use in real mode when we can't do
521  * any RCU stuff (since the secondary threads are offline from the
522  * kernel's point of view), and we can't print anything.
523  * Thus we use rcu_dereference_raw() rather than rcu_dereference_check().
524  */
525 static inline struct kvm_memslots *kvm_memslots_raw(struct kvm *kvm)
526 {
527         return rcu_dereference_raw_notrace(kvm->memslots[0]);
528 }
529
530 extern void kvmppc_mmu_debugfs_init(struct kvm *kvm);
531 extern void kvmhv_radix_debugfs_init(struct kvm *kvm);
532
533 extern void kvmhv_rm_send_ipi(int cpu);
534
535 static inline unsigned long kvmppc_hpt_npte(struct kvm_hpt_info *hpt)
536 {
537         /* HPTEs are 2**4 bytes long */
538         return 1UL << (hpt->order - 4);
539 }
540
541 static inline unsigned long kvmppc_hpt_mask(struct kvm_hpt_info *hpt)
542 {
543         /* 128 (2**7) bytes in each HPTEG */
544         return (1UL << (hpt->order - 7)) - 1;
545 }
546
547 /* Set bits in a dirty bitmap, which is in LE format */
548 static inline void set_dirty_bits(unsigned long *map, unsigned long i,
549                                   unsigned long npages)
550 {
551
552         if (npages >= 8)
553                 memset((char *)map + i / 8, 0xff, npages / 8);
554         else
555                 for (; npages; ++i, --npages)
556                         __set_bit_le(i, map);
557 }
558
559 static inline void set_dirty_bits_atomic(unsigned long *map, unsigned long i,
560                                          unsigned long npages)
561 {
562         if (npages >= 8)
563                 memset((char *)map + i / 8, 0xff, npages / 8);
564         else
565                 for (; npages; ++i, --npages)
566                         set_bit_le(i, map);
567 }
568
569 static inline u64 sanitize_msr(u64 msr)
570 {
571         msr &= ~MSR_HV;
572         msr |= MSR_ME;
573         return msr;
574 }
575
576 #ifdef CONFIG_PPC_TRANSACTIONAL_MEM
577 static inline void copy_from_checkpoint(struct kvm_vcpu *vcpu)
578 {
579         vcpu->arch.regs.ccr  = vcpu->arch.cr_tm;
580         vcpu->arch.regs.xer = vcpu->arch.xer_tm;
581         vcpu->arch.regs.link  = vcpu->arch.lr_tm;
582         vcpu->arch.regs.ctr = vcpu->arch.ctr_tm;
583         vcpu->arch.amr = vcpu->arch.amr_tm;
584         vcpu->arch.ppr = vcpu->arch.ppr_tm;
585         vcpu->arch.dscr = vcpu->arch.dscr_tm;
586         vcpu->arch.tar = vcpu->arch.tar_tm;
587         memcpy(vcpu->arch.regs.gpr, vcpu->arch.gpr_tm,
588                sizeof(vcpu->arch.regs.gpr));
589         vcpu->arch.fp  = vcpu->arch.fp_tm;
590         vcpu->arch.vr  = vcpu->arch.vr_tm;
591         vcpu->arch.vrsave = vcpu->arch.vrsave_tm;
592 }
593
594 static inline void copy_to_checkpoint(struct kvm_vcpu *vcpu)
595 {
596         vcpu->arch.cr_tm  = vcpu->arch.regs.ccr;
597         vcpu->arch.xer_tm = vcpu->arch.regs.xer;
598         vcpu->arch.lr_tm  = vcpu->arch.regs.link;
599         vcpu->arch.ctr_tm = vcpu->arch.regs.ctr;
600         vcpu->arch.amr_tm = vcpu->arch.amr;
601         vcpu->arch.ppr_tm = vcpu->arch.ppr;
602         vcpu->arch.dscr_tm = vcpu->arch.dscr;
603         vcpu->arch.tar_tm = vcpu->arch.tar;
604         memcpy(vcpu->arch.gpr_tm, vcpu->arch.regs.gpr,
605                sizeof(vcpu->arch.regs.gpr));
606         vcpu->arch.fp_tm  = vcpu->arch.fp;
607         vcpu->arch.vr_tm  = vcpu->arch.vr;
608         vcpu->arch.vrsave_tm = vcpu->arch.vrsave;
609 }
610 #endif /* CONFIG_PPC_TRANSACTIONAL_MEM */
611
612 extern int kvmppc_create_pte(struct kvm *kvm, pgd_t *pgtable, pte_t pte,
613                              unsigned long gpa, unsigned int level,
614                              unsigned long mmu_seq, unsigned int lpid,
615                              unsigned long *rmapp, struct rmap_nested **n_rmap);
616 extern void kvmhv_insert_nest_rmap(struct kvm *kvm, unsigned long *rmapp,
617                                    struct rmap_nested **n_rmap);
618 extern void kvmhv_remove_nest_rmap_range(struct kvm *kvm,
619                                 struct kvm_memory_slot *memslot,
620                                 unsigned long gpa, unsigned long hpa,
621                                 unsigned long nbytes);
622
623 #endif /* CONFIG_KVM_BOOK3S_HV_POSSIBLE */
624
625 #endif /* __ASM_KVM_BOOK3S_64_H__ */