ps3mmu.c revision 217044
1/*-
2 * Copyright (C) 2010 Nathan Whitehorn
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: head/sys/boot/powerpc/ps3/ps3mmu.c 217044 2011-01-06 04:12:29Z nwhitehorn $");
28
29#include <stand.h>
30#include <stdint.h>
31
32#define _KERNEL
33#include <machine/cpufunc.h>
34#include <machine/psl.h>
35#include <machine/pte.h>
36#include <machine/slb.h>
37#include <machine/param.h>
38
39#include "bootstrap.h"
40#include "lv1call.h"
41#include "ps3.h"
42
43register_t pteg_count, pteg_mask;
44uint64_t as_id;
45uint64_t virtual_avail;
46
47int
48ps3mmu_map(uint64_t va, uint64_t pa)
49{
50	struct lpte pt;
51	int shift;
52	uint64_t vsid, ptegidx;
53
54	if (pa < 0x8000000) { /* Phys mem? */
55		pt.pte_hi = LPTE_BIG;
56		pt.pte_lo = LPTE_M;
57		shift = 24;
58		vsid = 0;
59	} else {
60		pt.pte_hi = 0;
61		pt.pte_lo = LPTE_I | LPTE_G | LPTE_M | LPTE_NOEXEC;
62		shift = ADDR_PIDX_SHFT;
63		vsid = 1;
64	}
65
66	pt.pte_hi |= (vsid << LPTE_VSID_SHIFT) |
67            (((uint64_t)(va & ADDR_PIDX) >> ADDR_API_SHFT64) & LPTE_API);
68	pt.pte_lo |= pa;
69	ptegidx = vsid ^ (((uint64_t)va & ADDR_PIDX) >> shift);
70
71	pt.pte_hi |= LPTE_LOCKED | LPTE_VALID;
72	ptegidx &= pteg_mask;
73
74	return (lv1_insert_pte(ptegidx, &pt, LPTE_LOCKED));
75}
76
77void *
78ps3mmu_mapdev(uint64_t pa, size_t length)
79{
80	uint64_t spa;
81	void *mapstart;
82	int err;
83
84	mapstart = (void *)(uintptr_t)virtual_avail;
85
86	for (spa = pa; spa < pa + length; spa += PAGE_SIZE) {
87		err = ps3mmu_map(virtual_avail, spa);
88		virtual_avail += PAGE_SIZE;
89		if (err != 0)
90			return (NULL);
91	}
92
93	return (mapstart);
94}
95
96int
97ps3mmu_init(int maxmem)
98{
99	uint64_t ptsize;
100	int i;
101
102	i = lv1_setup_address_space(&as_id, &ptsize);
103	pteg_count = ptsize / sizeof(struct lpteg);
104	pteg_mask = pteg_count - 1;
105
106	for (i = 0; i < maxmem; i += 16*1024*1024)
107		ps3mmu_map(i,i);
108
109	virtual_avail = 0x10000000;
110
111	__asm __volatile ("slbia; slbmte %0, %1; slbmte %2,%3" ::
112	    "r"((0 << SLBV_VSID_SHIFT) | SLBV_L), "r"(0 | SLBE_VALID),
113	    "r"(1 << SLBV_VSID_SHIFT),
114	    "r"((1 << SLBE_ESID_SHIFT) | SLBE_VALID | 1));
115
116	mtmsr(mfmsr() | PSL_IR | PSL_DR | PSL_RI | PSL_ME);
117
118	return (0);
119}
120
121