]> CyberLeo.Net >> Repos - FreeBSD/stable/10.git/blob - sys/powerpc/pseries/mmu_phyp.c
Copy head (r256279) to stable/10 as part of the 10.0-RELEASE cycle.
[FreeBSD/stable/10.git] / sys / powerpc / pseries / mmu_phyp.c
1 /*
2  * Copyright (C) 2010 Andreas Tobler
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
15  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
16  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
17  * IN NO EVENT SHALL TOOLS GMBH BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
18  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
19  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
20  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
21  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
22  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
23  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24  */
25
26 #include <sys/cdefs.h>
27 __FBSDID("$FreeBSD$");
28
29 #include <sys/param.h>
30 #include <sys/kernel.h>
31 #include <sys/ktr.h>
32 #include <sys/lock.h>
33 #include <sys/msgbuf.h>
34 #include <sys/mutex.h>
35 #include <sys/proc.h>
36 #include <sys/sysctl.h>
37 #include <sys/systm.h>
38 #include <sys/vmmeter.h>
39
40 #include <dev/ofw/openfirm.h>
41 #include <machine/ofw_machdep.h>
42
43 #include <vm/vm.h>
44 #include <vm/vm_param.h>
45 #include <vm/vm_kern.h>
46 #include <vm/vm_page.h>
47 #include <vm/vm_map.h>
48 #include <vm/vm_object.h>
49 #include <vm/vm_extern.h>
50 #include <vm/vm_pageout.h>
51 #include <vm/uma.h>
52
53 #include <powerpc/aim/mmu_oea64.h>
54
55 #include "mmu_if.h"
56 #include "moea64_if.h"
57
58 #include "phyp-hvcall.h"
59
60 extern int n_slbs;
61
62 /*
63  * Kernel MMU interface
64  */
65
66 static void     mphyp_bootstrap(mmu_t mmup, vm_offset_t kernelstart,
67                     vm_offset_t kernelend);
68 static void     mphyp_cpu_bootstrap(mmu_t mmup, int ap);
69 static void     mphyp_pte_synch(mmu_t, uintptr_t pt, struct lpte *pvo_pt);
70 static void     mphyp_pte_clear(mmu_t, uintptr_t pt, struct lpte *pvo_pt,
71                     uint64_t vpn, u_int64_t ptebit);
72 static void     mphyp_pte_unset(mmu_t, uintptr_t pt, struct lpte *pvo_pt,
73                     uint64_t vpn);
74 static void     mphyp_pte_change(mmu_t, uintptr_t pt, struct lpte *pvo_pt,
75                     uint64_t vpn);
76 static int      mphyp_pte_insert(mmu_t, u_int ptegidx, struct lpte *pvo_pt);
77 static uintptr_t mphyp_pvo_to_pte(mmu_t, const struct pvo_entry *pvo);
78
79 #define VSID_HASH_MASK          0x0000007fffffffffULL
80
81
82 static mmu_method_t mphyp_methods[] = {
83         MMUMETHOD(mmu_bootstrap,        mphyp_bootstrap),
84         MMUMETHOD(mmu_cpu_bootstrap,    mphyp_cpu_bootstrap),
85
86         MMUMETHOD(moea64_pte_synch,     mphyp_pte_synch),
87         MMUMETHOD(moea64_pte_clear,     mphyp_pte_clear),
88         MMUMETHOD(moea64_pte_unset,     mphyp_pte_unset),
89         MMUMETHOD(moea64_pte_change,    mphyp_pte_change),
90         MMUMETHOD(moea64_pte_insert,    mphyp_pte_insert),
91         MMUMETHOD(moea64_pvo_to_pte,    mphyp_pvo_to_pte),
92
93         { 0, 0 }
94 };
95
96 MMU_DEF_INHERIT(pseries_mmu, "mmu_phyp", mphyp_methods, 0, oea64_mmu);
97
98 static void
99 mphyp_bootstrap(mmu_t mmup, vm_offset_t kernelstart, vm_offset_t kernelend)
100 {
101         uint64_t final_pteg_count = 0;
102         char buf[8];
103         uint32_t prop[2];
104         uint32_t nptlp, shift = 0, slb_encoding = 0;
105         phandle_t dev, node, root;
106         int idx, len, res;
107
108         moea64_early_bootstrap(mmup, kernelstart, kernelend);
109
110         root = OF_peer(0);
111
112         dev = OF_child(root);
113         while (dev != 0) {
114                 res = OF_getprop(dev, "name", buf, sizeof(buf));
115                 if (res > 0 && strcmp(buf, "cpus") == 0)
116                         break;
117                 dev = OF_peer(dev);
118         }
119
120         node = OF_child(dev);
121
122         while (node != 0) {
123                 res = OF_getprop(node, "device_type", buf, sizeof(buf));
124                 if (res > 0 && strcmp(buf, "cpu") == 0)
125                         break;
126                 node = OF_peer(node);
127         }
128
129         res = OF_getprop(node, "ibm,pft-size", prop, sizeof(prop));
130         if (res <= 0)
131                 panic("mmu_phyp: unknown PFT size");
132         final_pteg_count = 1 << prop[1];
133         res = OF_getprop(node, "ibm,slb-size", prop, sizeof(prop[0]));
134         if (res > 0)
135                 n_slbs = prop[0];
136
137         moea64_pteg_count = final_pteg_count / sizeof(struct lpteg);
138
139         /*
140          * Scan the large page size property for PAPR compatible machines.
141          * See PAPR D.5 Changes to Section 5.1.4, 'CPU Node Properties'
142          * for the encoding of the property.
143          */
144
145         len = OF_getproplen(node, "ibm,segment-page-sizes");
146         if (len > 0) {
147                 /*
148                  * We have to use a variable length array on the stack
149                  * since we have very limited stack space.
150                  */
151                 cell_t arr[len/sizeof(cell_t)];
152                 res = OF_getprop(node, "ibm,segment-page-sizes", &arr,
153                                  sizeof(arr));
154                 len /= 4;
155                 idx = 0;
156                 while (len > 0) {
157                         shift = arr[idx];
158                         slb_encoding = arr[idx + 1];
159                         nptlp = arr[idx + 2];
160                         idx += 3;
161                         len -= 3;
162                         while (len > 0 && nptlp) {
163                                 idx += 2;
164                                 len -= 2;
165                                 nptlp--;
166                         }
167                 }
168                 moea64_large_page_shift = shift;
169                 moea64_large_page_size = 1 << shift;
170         }
171
172         moea64_mid_bootstrap(mmup, kernelstart, kernelend);
173         moea64_late_bootstrap(mmup, kernelstart, kernelend);
174 }
175
176 static void
177 mphyp_cpu_bootstrap(mmu_t mmup, int ap)
178 {
179         struct slb *slb = PCPU_GET(slb);
180         register_t seg0;
181         int i;
182
183         /*
184          * Install kernel SLB entries
185          */
186
187         __asm __volatile ("slbia");
188         __asm __volatile ("slbmfee %0,%1; slbie %0;" : "=r"(seg0) : "r"(0));
189         for (i = 0; i < 64; i++) {
190                 if (!(slb[i].slbe & SLBE_VALID))
191                         continue;
192
193                 __asm __volatile ("slbmte %0, %1" ::
194                     "r"(slb[i].slbv), "r"(slb[i].slbe));
195         }
196 }
197
198 static void
199 mphyp_pte_synch(mmu_t mmu, uintptr_t slot, struct lpte *pvo_pt)
200 {
201         struct lpte pte;
202         uint64_t junk;
203
204         __asm __volatile("ptesync");
205         phyp_pft_hcall(H_READ, 0, slot, 0, 0, &pte.pte_hi, &pte.pte_lo,
206             &junk);
207
208         pvo_pt->pte_lo |= pte.pte_lo & (LPTE_CHG | LPTE_REF);
209 }
210
211 static void
212 mphyp_pte_clear(mmu_t mmu, uintptr_t slot, struct lpte *pvo_pt, uint64_t vpn,
213     u_int64_t ptebit)
214 {
215
216         if (ptebit & LPTE_CHG)
217                 phyp_hcall(H_CLEAR_MOD, 0, slot);
218         if (ptebit & LPTE_REF)
219                 phyp_hcall(H_CLEAR_REF, 0, slot);
220 }
221
222 static void
223 mphyp_pte_unset(mmu_t mmu, uintptr_t slot, struct lpte *pvo_pt, uint64_t vpn)
224 {
225         struct lpte pte;
226         uint64_t junk;
227         int err;
228
229         err = phyp_pft_hcall(H_REMOVE, 1UL << 31, slot,
230             pvo_pt->pte_hi & LPTE_AVPN_MASK, 0, &pte.pte_hi, &pte.pte_lo,
231             &junk);
232         KASSERT(err == H_SUCCESS, ("Error removing page: %d", err));
233
234         pvo_pt->pte_lo |= pte.pte_lo & (LPTE_CHG | LPTE_REF);
235 }
236
237 static void
238 mphyp_pte_change(mmu_t mmu, uintptr_t slot, struct lpte *pvo_pt, uint64_t vpn)
239 {
240         struct lpte evicted;
241         uint64_t index, junk;
242         int64_t result;
243
244         /*
245          * NB: this is protected by the global table lock, so this two-step
246          * is safe, except for the scratch-page case. No CPUs on which we run
247          * this code should be using scratch pages.
248          */
249         KASSERT(!(pvo_pt->pte_hi & LPTE_LOCKED),
250             ("Locked pages not supported on PHYP"));
251
252         /* XXX: optimization using H_PROTECT for common case? */
253         mphyp_pte_unset(mmu, slot, pvo_pt, vpn);
254         result = phyp_pft_hcall(H_ENTER, H_EXACT, slot, pvo_pt->pte_hi,
255                                 pvo_pt->pte_lo, &index, &evicted.pte_lo, &junk);
256         if (result != H_SUCCESS)
257                 panic("mphyp_pte_change() insertion failure: %ld\n", result);
258 }
259
260 static __inline int
261 mphyp_pte_spillable_ident(u_int ptegidx, struct lpte *to_evict)
262 {
263         uint64_t slot, junk, k;
264         struct lpte pt;
265         int     i, j;
266
267         /* Start at a random slot */
268         i = mftb() % 8;
269         k = -1;
270         for (j = 0; j < 8; j++) {
271                 slot = (ptegidx << 3) + (i + j) % 8;
272                 phyp_pft_hcall(H_READ, 0, slot, 0, 0, &pt.pte_hi, &pt.pte_lo,
273                     &junk);
274                 
275                 if (pt.pte_hi & LPTE_SWBITS)
276                         continue;
277
278                 /* This is a candidate, so remember it */
279                 k = slot;
280
281                 /* Try to get a page that has not been used lately */
282                 if (!(pt.pte_lo & LPTE_REF)) {
283                         memcpy(to_evict, &pt, sizeof(struct lpte));
284                         return (k);
285                 }
286         }
287
288         phyp_pft_hcall(H_READ, 0, slot, 0, 0, &to_evict->pte_hi,
289             &to_evict->pte_lo, &junk);
290         return (k);
291 }
292
293 static int
294 mphyp_pte_insert(mmu_t mmu, u_int ptegidx, struct lpte *pvo_pt)
295 {
296         int64_t result;
297         struct lpte evicted;
298         struct pvo_entry *pvo;
299         uint64_t index, junk;
300         u_int pteg_bktidx;
301
302         /* Check for locked pages, which we can't support on this system */
303         KASSERT(!(pvo_pt->pte_hi & LPTE_LOCKED),
304             ("Locked pages not supported on PHYP"));
305
306         /* Initialize PTE */
307         pvo_pt->pte_hi |= LPTE_VALID;
308         pvo_pt->pte_hi &= ~LPTE_HID;
309         evicted.pte_hi = 0;
310
311         /*
312          * First try primary hash.
313          */
314         pteg_bktidx = ptegidx;
315         result = phyp_pft_hcall(H_ENTER, 0, pteg_bktidx << 3, pvo_pt->pte_hi,
316             pvo_pt->pte_lo, &index, &evicted.pte_lo, &junk);
317         if (result == H_SUCCESS)
318                 return (index & 0x07);
319         KASSERT(result == H_PTEG_FULL, ("Page insertion error: %ld "
320             "(ptegidx: %#x/%#x, PTE %#lx/%#lx", result, ptegidx,
321             moea64_pteg_count, pvo_pt->pte_hi, pvo_pt->pte_lo));
322
323         /*
324          * Next try secondary hash.
325          */
326         pteg_bktidx ^= moea64_pteg_mask;
327         pvo_pt->pte_hi |= LPTE_HID;
328         result = phyp_pft_hcall(H_ENTER, 0, pteg_bktidx << 3,
329             pvo_pt->pte_hi, pvo_pt->pte_lo, &index, &evicted.pte_lo, &junk);
330         if (result == H_SUCCESS)
331                 return (index & 0x07);
332         KASSERT(result == H_PTEG_FULL, ("Secondary page insertion error: %ld",
333             result));
334
335         /*
336          * Out of luck. Find a PTE to sacrifice.
337          */
338         pteg_bktidx = ptegidx;
339         index = mphyp_pte_spillable_ident(pteg_bktidx, &evicted);
340         if (index == -1L) {
341                 pteg_bktidx ^= moea64_pteg_mask;
342                 index = mphyp_pte_spillable_ident(pteg_bktidx, &evicted);
343         }
344
345         if (index == -1L) {
346                 /* No freeable slots in either PTEG? We're hosed. */
347                 panic("mphyp_pte_insert: overflow");
348                 return (-1);
349         }
350
351         if (pteg_bktidx == ptegidx)
352                 pvo_pt->pte_hi &= ~LPTE_HID;
353         else
354                 pvo_pt->pte_hi |= LPTE_HID;
355
356         /*
357          * Synchronize the sacrifice PTE with its PVO, then mark both
358          * invalid. The PVO will be reused when/if the VM system comes
359          * here after a fault.
360          */
361
362         if (evicted.pte_hi & LPTE_HID)
363                 pteg_bktidx ^= moea64_pteg_mask; /* PTEs indexed by primary */
364
365         LIST_FOREACH(pvo, &moea64_pvo_table[pteg_bktidx], pvo_olink) {
366                 if (pvo->pvo_pte.lpte.pte_hi == evicted.pte_hi) {
367                         KASSERT(pvo->pvo_pte.lpte.pte_hi & LPTE_VALID,
368                             ("Invalid PVO for valid PTE!"));
369                         mphyp_pte_unset(mmu, index, &pvo->pvo_pte.lpte,
370                             pvo->pvo_vpn);
371                         PVO_PTEGIDX_CLR(pvo);
372                         moea64_pte_overflow++;
373                         break;
374                 }
375         }
376
377         KASSERT(pvo->pvo_pte.lpte.pte_hi == evicted.pte_hi,
378            ("Unable to find PVO for spilled PTE"));
379
380         /*
381          * Set the new PTE.
382          */
383         result = phyp_pft_hcall(H_ENTER, H_EXACT, index, pvo_pt->pte_hi,
384             pvo_pt->pte_lo, &index, &evicted.pte_lo, &junk);
385         if (result == H_SUCCESS)
386                 return (index & 0x07);
387
388         panic("Page replacement error: %ld", result);
389         return (-1);
390 }
391
392 static __inline u_int
393 va_to_pteg(uint64_t vsid, vm_offset_t addr, int large)
394 {
395         uint64_t hash;
396         int shift;
397
398         shift = large ? moea64_large_page_shift : ADDR_PIDX_SHFT;
399         hash = (vsid & VSID_HASH_MASK) ^ (((uint64_t)addr & ADDR_PIDX) >>
400             shift);
401         return (hash & moea64_pteg_mask);
402 }
403
404 static uintptr_t
405 mphyp_pvo_to_pte(mmu_t mmu, const struct pvo_entry *pvo)
406 {
407         uint64_t vsid;
408         u_int ptegidx;
409
410         /* If the PTEG index is not set, then there is no page table entry */
411         if (!PVO_PTEGIDX_ISSET(pvo))
412                 return (-1);
413
414         vsid = PVO_VSID(pvo);
415         ptegidx = va_to_pteg(vsid, PVO_VADDR(pvo), pvo->pvo_vaddr & PVO_LARGE);
416
417         /*
418          * We can find the actual pte entry without searching by grabbing
419          * the PTEG index from 3 unused bits in pvo_vaddr and by
420          * noticing the HID bit.
421          */
422         if (pvo->pvo_pte.lpte.pte_hi & LPTE_HID)
423                 ptegidx ^= moea64_pteg_mask;
424
425         return ((ptegidx << 3) | PVO_PTEGIDX_GET(pvo));
426 }
427