103479763SNathan Whitehorn /*-
2*4d846d26SWarner Losh * SPDX-License-Identifier: BSD-2-Clause
371e3c308SPedro F. Giffuni *
403479763SNathan Whitehorn * Copyright (C) 2010 Nathan Whitehorn
503479763SNathan Whitehorn * All rights reserved.
603479763SNathan Whitehorn *
703479763SNathan Whitehorn * Redistribution and use in source and binary forms, with or without
803479763SNathan Whitehorn * modification, are permitted provided that the following conditions
903479763SNathan Whitehorn * are met:
1003479763SNathan Whitehorn * 1. Redistributions of source code must retain the above copyright
1103479763SNathan Whitehorn * notice, this list of conditions and the following disclaimer.
1203479763SNathan Whitehorn * 2. Redistributions in binary form must reproduce the above copyright
1303479763SNathan Whitehorn * notice, this list of conditions and the following disclaimer in the
1403479763SNathan Whitehorn * documentation and/or other materials provided with the distribution.
1503479763SNathan Whitehorn *
1603479763SNathan Whitehorn * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
1703479763SNathan Whitehorn * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
1803479763SNathan Whitehorn * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
1903479763SNathan Whitehorn * IN NO EVENT SHALL TOOLS GMBH BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
2003479763SNathan Whitehorn * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
2103479763SNathan Whitehorn * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
2203479763SNathan Whitehorn * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
2303479763SNathan Whitehorn * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
2403479763SNathan Whitehorn * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
2503479763SNathan Whitehorn * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
2603479763SNathan Whitehorn */
2703479763SNathan Whitehorn
2803479763SNathan Whitehorn #include <sys/param.h>
2903479763SNathan Whitehorn #include <sys/kernel.h>
3003479763SNathan Whitehorn #include <sys/ktr.h>
3103479763SNathan Whitehorn #include <sys/lock.h>
3203479763SNathan Whitehorn #include <sys/msgbuf.h>
3303479763SNathan Whitehorn #include <sys/mutex.h>
3403479763SNathan Whitehorn #include <sys/proc.h>
3503479763SNathan Whitehorn #include <sys/sysctl.h>
3603479763SNathan Whitehorn #include <sys/systm.h>
3703479763SNathan Whitehorn #include <sys/vmmeter.h>
3803479763SNathan Whitehorn
3903479763SNathan Whitehorn #include <vm/vm.h>
4003479763SNathan Whitehorn #include <vm/vm_param.h>
4103479763SNathan Whitehorn #include <vm/vm_kern.h>
4203479763SNathan Whitehorn #include <vm/vm_page.h>
4303479763SNathan Whitehorn #include <vm/vm_map.h>
4403479763SNathan Whitehorn #include <vm/vm_object.h>
4503479763SNathan Whitehorn #include <vm/vm_extern.h>
4603479763SNathan Whitehorn #include <vm/vm_pageout.h>
4703479763SNathan Whitehorn #include <vm/uma.h>
4803479763SNathan Whitehorn
4903479763SNathan Whitehorn #include <powerpc/aim/mmu_oea64.h>
5003479763SNathan Whitehorn
5103479763SNathan Whitehorn #include "ps3-hvcall.h"
5203479763SNathan Whitehorn
5303479763SNathan Whitehorn #define VSID_HASH_MASK 0x0000007fffffffffUL
5403479763SNathan Whitehorn #define PTESYNC() __asm __volatile("ptesync")
5503479763SNathan Whitehorn
5603479763SNathan Whitehorn extern int ps3fb_remap(void);
5703479763SNathan Whitehorn
5803479763SNathan Whitehorn static uint64_t mps3_vas_id;
5903479763SNathan Whitehorn
6003479763SNathan Whitehorn /*
6103479763SNathan Whitehorn * Kernel MMU interface
6203479763SNathan Whitehorn */
6303479763SNathan Whitehorn
6445b69dd6SJustin Hibbits static void mps3_install(void);
6545b69dd6SJustin Hibbits static void mps3_bootstrap(vm_offset_t kernelstart,
6603479763SNathan Whitehorn vm_offset_t kernelend);
6745b69dd6SJustin Hibbits static void mps3_cpu_bootstrap(int ap);
6845b69dd6SJustin Hibbits static int64_t mps3_pte_synch(struct pvo_entry *);
6945b69dd6SJustin Hibbits static int64_t mps3_pte_clear(struct pvo_entry *, uint64_t ptebit);
7045b69dd6SJustin Hibbits static int64_t mps3_pte_unset(struct pvo_entry *);
7145b69dd6SJustin Hibbits static int64_t mps3_pte_insert(struct pvo_entry *);
7203479763SNathan Whitehorn
7345b69dd6SJustin Hibbits static struct pmap_funcs mps3_methods = {
7445b69dd6SJustin Hibbits .install = mps3_install,
7545b69dd6SJustin Hibbits .bootstrap = mps3_bootstrap,
7645b69dd6SJustin Hibbits .cpu_bootstrap = mps3_cpu_bootstrap,
7703479763SNathan Whitehorn };
7803479763SNathan Whitehorn
7945b69dd6SJustin Hibbits static struct moea64_funcs mps3_funcs = {
8045b69dd6SJustin Hibbits .pte_synch = mps3_pte_synch,
8145b69dd6SJustin Hibbits .pte_clear = mps3_pte_clear,
8245b69dd6SJustin Hibbits .pte_unset = mps3_pte_unset,
8345b69dd6SJustin Hibbits .pte_insert = mps3_pte_insert,
8445b69dd6SJustin Hibbits };
8545b69dd6SJustin Hibbits
8645b69dd6SJustin Hibbits MMU_DEF_INHERIT(ps3_mmu, "mmu_ps3", mps3_methods, oea64_mmu);
8703479763SNathan Whitehorn
88827cc9b9SNathan Whitehorn static struct mtx mps3_table_lock;
89827cc9b9SNathan Whitehorn
9003479763SNathan Whitehorn static void
mps3_install(void)91429ba161SPiotr Kubaj mps3_install(void)
9245b69dd6SJustin Hibbits {
9345b69dd6SJustin Hibbits moea64_ops = &mps3_funcs;
9449c894ddSJustin Hibbits moea64_install();
9545b69dd6SJustin Hibbits }
9645b69dd6SJustin Hibbits
9745b69dd6SJustin Hibbits static void
mps3_bootstrap(vm_offset_t kernelstart,vm_offset_t kernelend)9845b69dd6SJustin Hibbits mps3_bootstrap(vm_offset_t kernelstart, vm_offset_t kernelend)
9903479763SNathan Whitehorn {
10003479763SNathan Whitehorn uint64_t final_pteg_count;
10103479763SNathan Whitehorn
102827cc9b9SNathan Whitehorn mtx_init(&mps3_table_lock, "page table", NULL, MTX_DEF);
103827cc9b9SNathan Whitehorn
10445b69dd6SJustin Hibbits moea64_early_bootstrap(kernelstart, kernelend);
10503479763SNathan Whitehorn
1063fca7880SNathan Whitehorn /* In case we had a page table already */
1073fca7880SNathan Whitehorn lv1_destruct_virtual_address_space(0);
1083fca7880SNathan Whitehorn
1093fca7880SNathan Whitehorn /* Allocate new hardware page table */
11003479763SNathan Whitehorn lv1_construct_virtual_address_space(
11103479763SNathan Whitehorn 20 /* log_2(moea64_pteg_count) */, 2 /* n page sizes */,
11203479763SNathan Whitehorn (24UL << 56) | (16UL << 48) /* page sizes 16 MB + 64 KB */,
11303479763SNathan Whitehorn &mps3_vas_id, &final_pteg_count
11403479763SNathan Whitehorn );
11503479763SNathan Whitehorn
1163fca7880SNathan Whitehorn lv1_select_virtual_address_space(mps3_vas_id);
1173fca7880SNathan Whitehorn
11803479763SNathan Whitehorn moea64_pteg_count = final_pteg_count / sizeof(struct lpteg);
11903479763SNathan Whitehorn
12045b69dd6SJustin Hibbits moea64_mid_bootstrap(kernelstart, kernelend);
12145b69dd6SJustin Hibbits moea64_late_bootstrap(kernelstart, kernelend);
12203479763SNathan Whitehorn }
12303479763SNathan Whitehorn
12403479763SNathan Whitehorn static void
mps3_cpu_bootstrap(int ap)12545b69dd6SJustin Hibbits mps3_cpu_bootstrap(int ap)
12603479763SNathan Whitehorn {
127bce6d88bSJustin Hibbits struct slb *slb = PCPU_GET(aim.slb);
12803479763SNathan Whitehorn register_t seg0;
12903479763SNathan Whitehorn int i;
13003479763SNathan Whitehorn
13103479763SNathan Whitehorn mtmsr(mfmsr() & ~PSL_DR & ~PSL_IR);
13203479763SNathan Whitehorn
13303479763SNathan Whitehorn /*
1343fca7880SNathan Whitehorn * Select the page table we configured above and set up the FB mapping
1353fca7880SNathan Whitehorn * so we can have a console.
13603479763SNathan Whitehorn */
13703479763SNathan Whitehorn lv1_select_virtual_address_space(mps3_vas_id);
13803479763SNathan Whitehorn
13903479763SNathan Whitehorn if (!ap)
14003479763SNathan Whitehorn ps3fb_remap();
14103479763SNathan Whitehorn
14203479763SNathan Whitehorn /*
14303479763SNathan Whitehorn * Install kernel SLB entries
14403479763SNathan Whitehorn */
14503479763SNathan Whitehorn
14603479763SNathan Whitehorn __asm __volatile ("slbia");
14703479763SNathan Whitehorn __asm __volatile ("slbmfee %0,%1; slbie %0;" : "=r"(seg0) : "r"(0));
14803479763SNathan Whitehorn for (i = 0; i < 64; i++) {
14903479763SNathan Whitehorn if (!(slb[i].slbe & SLBE_VALID))
15003479763SNathan Whitehorn continue;
15103479763SNathan Whitehorn
15203479763SNathan Whitehorn __asm __volatile ("slbmte %0, %1" ::
15303479763SNathan Whitehorn "r"(slb[i].slbv), "r"(slb[i].slbe));
15403479763SNathan Whitehorn }
15503479763SNathan Whitehorn }
15603479763SNathan Whitehorn
157827cc9b9SNathan Whitehorn static int64_t
mps3_pte_synch_locked(struct pvo_entry * pvo)158827cc9b9SNathan Whitehorn mps3_pte_synch_locked(struct pvo_entry *pvo)
15903479763SNathan Whitehorn {
16003479763SNathan Whitehorn uint64_t halfbucket[4], rcbits;
16103479763SNathan Whitehorn
16203479763SNathan Whitehorn PTESYNC();
163827cc9b9SNathan Whitehorn lv1_read_htab_entries(mps3_vas_id, pvo->pvo_pte.slot & ~0x3UL,
164827cc9b9SNathan Whitehorn &halfbucket[0], &halfbucket[1], &halfbucket[2], &halfbucket[3],
165827cc9b9SNathan Whitehorn &rcbits);
166827cc9b9SNathan Whitehorn
167827cc9b9SNathan Whitehorn /* Check if present in page table */
168827cc9b9SNathan Whitehorn if ((halfbucket[pvo->pvo_pte.slot & 0x3] & LPTE_AVPN_MASK) !=
169827cc9b9SNathan Whitehorn ((pvo->pvo_vpn >> (ADDR_API_SHFT64 - ADDR_PIDX_SHFT)) &
170827cc9b9SNathan Whitehorn LPTE_AVPN_MASK))
171827cc9b9SNathan Whitehorn return (-1);
172827cc9b9SNathan Whitehorn if (!(halfbucket[pvo->pvo_pte.slot & 0x3] & LPTE_VALID))
173827cc9b9SNathan Whitehorn return (-1);
17403479763SNathan Whitehorn
17503479763SNathan Whitehorn /*
176827cc9b9SNathan Whitehorn * rcbits contains the low 12 bits of each PTE's 2nd part,
17703479763SNathan Whitehorn * spaced at 16-bit intervals
17803479763SNathan Whitehorn */
17903479763SNathan Whitehorn
180827cc9b9SNathan Whitehorn return ((rcbits >> ((3 - (pvo->pvo_pte.slot & 0x3))*16)) &
181827cc9b9SNathan Whitehorn (LPTE_CHG | LPTE_REF));
18203479763SNathan Whitehorn }
18303479763SNathan Whitehorn
184827cc9b9SNathan Whitehorn static int64_t
mps3_pte_synch(struct pvo_entry * pvo)18545b69dd6SJustin Hibbits mps3_pte_synch(struct pvo_entry *pvo)
18603479763SNathan Whitehorn {
187827cc9b9SNathan Whitehorn int64_t retval;
18803479763SNathan Whitehorn
189827cc9b9SNathan Whitehorn mtx_lock(&mps3_table_lock);
190827cc9b9SNathan Whitehorn retval = mps3_pte_synch_locked(pvo);
191827cc9b9SNathan Whitehorn mtx_unlock(&mps3_table_lock);
192827cc9b9SNathan Whitehorn
193827cc9b9SNathan Whitehorn return (retval);
19403479763SNathan Whitehorn }
19503479763SNathan Whitehorn
196827cc9b9SNathan Whitehorn static int64_t
mps3_pte_clear(struct pvo_entry * pvo,uint64_t ptebit)19745b69dd6SJustin Hibbits mps3_pte_clear(struct pvo_entry *pvo, uint64_t ptebit)
19803479763SNathan Whitehorn {
199827cc9b9SNathan Whitehorn int64_t refchg;
200827cc9b9SNathan Whitehorn struct lpte pte;
20103479763SNathan Whitehorn
202827cc9b9SNathan Whitehorn mtx_lock(&mps3_table_lock);
203827cc9b9SNathan Whitehorn
204827cc9b9SNathan Whitehorn refchg = mps3_pte_synch_locked(pvo);
205827cc9b9SNathan Whitehorn if (refchg < 0) {
206827cc9b9SNathan Whitehorn mtx_unlock(&mps3_table_lock);
207827cc9b9SNathan Whitehorn return (refchg);
208827cc9b9SNathan Whitehorn }
209827cc9b9SNathan Whitehorn
210827cc9b9SNathan Whitehorn moea64_pte_from_pvo(pvo, &pte);
211827cc9b9SNathan Whitehorn
212827cc9b9SNathan Whitehorn pte.pte_lo |= refchg;
213827cc9b9SNathan Whitehorn pte.pte_lo &= ~ptebit;
214827cc9b9SNathan Whitehorn /* XXX: race on RC bits between write and sync. Anything to do? */
215827cc9b9SNathan Whitehorn lv1_write_htab_entry(mps3_vas_id, pvo->pvo_pte.slot, pte.pte_hi,
216827cc9b9SNathan Whitehorn pte.pte_lo);
217827cc9b9SNathan Whitehorn mtx_unlock(&mps3_table_lock);
218827cc9b9SNathan Whitehorn
219827cc9b9SNathan Whitehorn return (refchg);
220827cc9b9SNathan Whitehorn }
221827cc9b9SNathan Whitehorn
222827cc9b9SNathan Whitehorn static int64_t
mps3_pte_unset(struct pvo_entry * pvo)22345b69dd6SJustin Hibbits mps3_pte_unset(struct pvo_entry *pvo)
224827cc9b9SNathan Whitehorn {
225827cc9b9SNathan Whitehorn int64_t refchg;
226827cc9b9SNathan Whitehorn
227827cc9b9SNathan Whitehorn mtx_lock(&mps3_table_lock);
228827cc9b9SNathan Whitehorn refchg = mps3_pte_synch_locked(pvo);
229827cc9b9SNathan Whitehorn if (refchg < 0) {
2307c382eeaSJustin Hibbits STAT_MOEA64(moea64_pte_overflow--);
231827cc9b9SNathan Whitehorn mtx_unlock(&mps3_table_lock);
232827cc9b9SNathan Whitehorn return (-1);
233827cc9b9SNathan Whitehorn }
234827cc9b9SNathan Whitehorn /* XXX: race on RC bits between unset and sync. Anything to do? */
235827cc9b9SNathan Whitehorn lv1_write_htab_entry(mps3_vas_id, pvo->pvo_pte.slot, 0, 0);
236827cc9b9SNathan Whitehorn mtx_unlock(&mps3_table_lock);
2377c382eeaSJustin Hibbits STAT_MOEA64(moea64_pte_valid--);
23803479763SNathan Whitehorn
239827cc9b9SNathan Whitehorn return (refchg & (LPTE_REF | LPTE_CHG));
24003479763SNathan Whitehorn }
24103479763SNathan Whitehorn
24245b69dd6SJustin Hibbits static int64_t
mps3_pte_insert(struct pvo_entry * pvo)24345b69dd6SJustin Hibbits mps3_pte_insert(struct pvo_entry *pvo)
24403479763SNathan Whitehorn {
24503479763SNathan Whitehorn int result;
246827cc9b9SNathan Whitehorn struct lpte pte, evicted;
24703479763SNathan Whitehorn uint64_t index;
24803479763SNathan Whitehorn
249827cc9b9SNathan Whitehorn if (pvo->pvo_vaddr & PVO_HID) {
250827cc9b9SNathan Whitehorn /* Hypercall needs primary PTEG */
251827cc9b9SNathan Whitehorn pvo->pvo_vaddr &= ~PVO_HID;
252827cc9b9SNathan Whitehorn pvo->pvo_pte.slot ^= (moea64_pteg_mask << 3);
253827cc9b9SNathan Whitehorn }
254827cc9b9SNathan Whitehorn
255827cc9b9SNathan Whitehorn pvo->pvo_pte.slot &= ~7UL;
256827cc9b9SNathan Whitehorn moea64_pte_from_pvo(pvo, &pte);
25703479763SNathan Whitehorn evicted.pte_hi = 0;
25803479763SNathan Whitehorn PTESYNC();
259827cc9b9SNathan Whitehorn mtx_lock(&mps3_table_lock);
260827cc9b9SNathan Whitehorn result = lv1_insert_htab_entry(mps3_vas_id, pvo->pvo_pte.slot,
261827cc9b9SNathan Whitehorn pte.pte_hi, pte.pte_lo, LPTE_LOCKED | LPTE_WIRED, 0,
26203479763SNathan Whitehorn &index, &evicted.pte_hi, &evicted.pte_lo);
263827cc9b9SNathan Whitehorn mtx_unlock(&mps3_table_lock);
26403479763SNathan Whitehorn
26503479763SNathan Whitehorn if (result != 0) {
26603479763SNathan Whitehorn /* No freeable slots in either PTEG? We're hosed. */
26703479763SNathan Whitehorn panic("mps3_pte_insert: overflow (%d)", result);
26803479763SNathan Whitehorn return (-1);
26903479763SNathan Whitehorn }
27003479763SNathan Whitehorn
27103479763SNathan Whitehorn /*
27203479763SNathan Whitehorn * See where we ended up.
27303479763SNathan Whitehorn */
274827cc9b9SNathan Whitehorn if ((index & ~7UL) != pvo->pvo_pte.slot)
275827cc9b9SNathan Whitehorn pvo->pvo_vaddr |= PVO_HID;
276827cc9b9SNathan Whitehorn pvo->pvo_pte.slot = index;
27703479763SNathan Whitehorn
2787c382eeaSJustin Hibbits STAT_MOEA64(moea64_pte_valid++);
27903479763SNathan Whitehorn
280827cc9b9SNathan Whitehorn if (evicted.pte_hi) {
28103479763SNathan Whitehorn KASSERT((evicted.pte_hi & (LPTE_WIRED | LPTE_LOCKED)) == 0,
28203479763SNathan Whitehorn ("Evicted a wired PTE"));
2837c382eeaSJustin Hibbits STAT_MOEA64(moea64_pte_valid--);
2847c382eeaSJustin Hibbits STAT_MOEA64(moea64_pte_overflow++);
28503479763SNathan Whitehorn }
28603479763SNathan Whitehorn
287827cc9b9SNathan Whitehorn return (0);
28803479763SNathan Whitehorn }
289