- Update to 3.3-final.
[linux-flexiantxendom0-3.2.10.git] / arch / powerpc / kvm / book3s_64_mmu_hv.c
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 2010 Paul Mackerras, IBM Corp. <paulus@au1.ibm.com>
16  */
17
18 #include <linux/types.h>
19 #include <linux/string.h>
20 #include <linux/kvm.h>
21 #include <linux/kvm_host.h>
22 #include <linux/highmem.h>
23 #include <linux/gfp.h>
24 #include <linux/slab.h>
25 #include <linux/hugetlb.h>
26
27 #include <asm/tlbflush.h>
28 #include <asm/kvm_ppc.h>
29 #include <asm/kvm_book3s.h>
30 #include <asm/mmu-hash64.h>
31 #include <asm/hvcall.h>
32 #include <asm/synch.h>
33 #include <asm/ppc-opcode.h>
34 #include <asm/cputable.h>
35
36 /* For now use fixed-size 16MB page table */
37 #define HPT_ORDER       24
38 #define HPT_NPTEG       (1ul << (HPT_ORDER - 7))        /* 128B per pteg */
39 #define HPT_HASH_MASK   (HPT_NPTEG - 1)
40
41 /* Pages in the VRMA are 16MB pages */
42 #define VRMA_PAGE_ORDER 24
43 #define VRMA_VSID       0x1ffffffUL     /* 1TB VSID reserved for VRMA */
44
45 /* POWER7 has 10-bit LPIDs, PPC970 has 6-bit LPIDs */
46 #define MAX_LPID_970    63
47 #define NR_LPIDS        (LPID_RSVD + 1)
48 unsigned long lpid_inuse[BITS_TO_LONGS(NR_LPIDS)];
49
50 long kvmppc_alloc_hpt(struct kvm *kvm)
51 {
52         unsigned long hpt;
53         unsigned long lpid;
54         struct kvmppc_linear_info *li;
55
56         /* using preallocated memory */
57         li = kvm_alloc_hpt();
58         if (li) {
59                 hpt = (ulong)li->base_virt;
60                 kvm->arch.hpt_li = li;
61                 memset(li->base_virt, 0, li->npages << PAGE_SHIFT);
62                 goto has_hpt;
63         }
64
65         /* using dynamic memory */
66         hpt = __get_free_pages(GFP_KERNEL|__GFP_ZERO|__GFP_REPEAT|__GFP_NOWARN,
67                                HPT_ORDER - PAGE_SHIFT);
68 has_hpt:
69         if (!hpt) {
70                 pr_err("kvm_alloc_hpt: Couldn't alloc HPT\n");
71                 return -ENOMEM;
72         }
73         kvm->arch.hpt_virt = hpt;
74
75         do {
76                 lpid = find_first_zero_bit(lpid_inuse, NR_LPIDS);
77                 if (lpid >= NR_LPIDS) {
78                         pr_err("kvm_alloc_hpt: No LPIDs free\n");
79                         free_pages(hpt, HPT_ORDER - PAGE_SHIFT);
80                         return -ENOMEM;
81                 }
82         } while (test_and_set_bit(lpid, lpid_inuse));
83
84         kvm->arch.sdr1 = __pa(hpt) | (HPT_ORDER - 18);
85         kvm->arch.lpid = lpid;
86
87         pr_info("KVM guest htab at %lx, LPID %lx\n", hpt, lpid);
88         return 0;
89 }
90
91 void kvmppc_free_hpt(struct kvm *kvm)
92 {
93         clear_bit(kvm->arch.lpid, lpid_inuse);
94         free_pages(kvm->arch.hpt_virt, HPT_ORDER - PAGE_SHIFT);
95         if (kvm->arch.hpt_li)
96                 kvm_release_hpt(kvm->arch.hpt_li);
97         else
98                 free_pages(kvm->arch.hpt_virt, HPT_ORDER - PAGE_SHIFT);
99 }
100
101 void kvmppc_map_vrma(struct kvm *kvm, struct kvm_userspace_memory_region *mem)
102 {
103         unsigned long i;
104         unsigned long npages = kvm->arch.ram_npages;
105         unsigned long pfn;
106         unsigned long *hpte;
107         unsigned long hash;
108         struct kvmppc_pginfo *pginfo = kvm->arch.ram_pginfo;
109
110         if (!pginfo)
111                 return;
112
113         /* VRMA can't be > 1TB */
114         if (npages > 1ul << (40 - kvm->arch.ram_porder))
115                 npages = 1ul << (40 - kvm->arch.ram_porder);
116         /* Can't use more than 1 HPTE per HPTEG */
117         if (npages > HPT_NPTEG)
118                 npages = HPT_NPTEG;
119
120         for (i = 0; i < npages; ++i) {
121                 pfn = pginfo[i].pfn;
122                 if (!pfn)
123                         break;
124                 /* can't use hpt_hash since va > 64 bits */
125                 hash = (i ^ (VRMA_VSID ^ (VRMA_VSID << 25))) & HPT_HASH_MASK;
126                 /*
127                  * We assume that the hash table is empty and no
128                  * vcpus are using it at this stage.  Since we create
129                  * at most one HPTE per HPTEG, we just assume entry 7
130                  * is available and use it.
131                  */
132                 hpte = (unsigned long *) (kvm->arch.hpt_virt + (hash << 7));
133                 hpte += 7 * 2;
134                 /* HPTE low word - RPN, protection, etc. */
135                 hpte[1] = (pfn << PAGE_SHIFT) | HPTE_R_R | HPTE_R_C |
136                         HPTE_R_M | PP_RWXX;
137                 wmb();
138                 hpte[0] = HPTE_V_1TB_SEG | (VRMA_VSID << (40 - 16)) |
139                         (i << (VRMA_PAGE_ORDER - 16)) | HPTE_V_BOLTED |
140                         HPTE_V_LARGE | HPTE_V_VALID;
141         }
142 }
143
144 int kvmppc_mmu_hv_init(void)
145 {
146         unsigned long host_lpid, rsvd_lpid;
147
148         if (!cpu_has_feature(CPU_FTR_HVMODE))
149                 return -EINVAL;
150
151         memset(lpid_inuse, 0, sizeof(lpid_inuse));
152
153         if (cpu_has_feature(CPU_FTR_ARCH_206)) {
154                 host_lpid = mfspr(SPRN_LPID);   /* POWER7 */
155                 rsvd_lpid = LPID_RSVD;
156         } else {
157                 host_lpid = 0;                  /* PPC970 */
158                 rsvd_lpid = MAX_LPID_970;
159         }
160
161         set_bit(host_lpid, lpid_inuse);
162         /* rsvd_lpid is reserved for use in partition switching */
163         set_bit(rsvd_lpid, lpid_inuse);
164
165         return 0;
166 }
167
168 void kvmppc_mmu_destroy(struct kvm_vcpu *vcpu)
169 {
170 }
171
172 static void kvmppc_mmu_book3s_64_hv_reset_msr(struct kvm_vcpu *vcpu)
173 {
174         kvmppc_set_msr(vcpu, MSR_SF | MSR_ME);
175 }
176
177 static int kvmppc_mmu_book3s_64_hv_xlate(struct kvm_vcpu *vcpu, gva_t eaddr,
178                                 struct kvmppc_pte *gpte, bool data)
179 {
180         return -ENOENT;
181 }
182
183 void kvmppc_mmu_book3s_hv_init(struct kvm_vcpu *vcpu)
184 {
185         struct kvmppc_mmu *mmu = &vcpu->arch.mmu;
186
187         if (cpu_has_feature(CPU_FTR_ARCH_206))
188                 vcpu->arch.slb_nr = 32;         /* POWER7 */
189         else
190                 vcpu->arch.slb_nr = 64;
191
192         mmu->xlate = kvmppc_mmu_book3s_64_hv_xlate;
193         mmu->reset_msr = kvmppc_mmu_book3s_64_hv_reset_msr;
194
195         vcpu->arch.hflags |= BOOK3S_HFLAG_SLB;
196 }