booke_pmap.c revision 1.1.2.1 1 /* $NetBSD: booke_pmap.c,v 1.1.2.1 2011/01/07 01:26:19 matt Exp $ */
2 /*-
3 * Copyright (c) 2010, 2011 The NetBSD Foundation, Inc.
4 * All rights reserved.
5 *
6 * This code is derived from software contributed to The NetBSD Foundation
7 * by Raytheon BBN Technologies Corp and Defense Advanced Research Projects
8 * Agency and which was developed by Matt Thomas of 3am Software Foundry.
9 *
10 * This material is based upon work supported by the Defense Advanced Research
11 * Projects Agency and Space and Naval Warfare Systems Center, Pacific, under
12 * Contract No. N66001-09-C-2073.
13 * Approved for Public Release, Distribution Unlimited
14 *
15 * Redistribution and use in source and binary forms, with or without
16 * modification, are permitted provided that the following conditions
17 * are met:
18 * 1. Redistributions of source code must retain the above copyright
19 * notice, this list of conditions and the following disclaimer.
20 * 2. Redistributions in binary form must reproduce the above copyright
21 * notice, this list of conditions and the following disclaimer in the
22 * documentation and/or other materials provided with the distribution.
23 *
24 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
25 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
26 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
27 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
28 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
29 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
30 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
31 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
32 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
33 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
34 * POSSIBILITY OF SUCH DAMAGE.
35 */
36
37 #include <sys/cdefs.h>
38
39 __KERNEL_RCSID(0, "$NetBSD: booke_pmap.c,v 1.1.2.1 2011/01/07 01:26:19 matt Exp $");
40
41 #include <sys/param.h>
42 #include <sys/kcore.h>
43 #include <sys/buf.h>
44
45 #include <uvm/uvm_extern.h>
46
47 #include <machine/pmap.h>
48
49 /*
50 * Initialize the kernel pmap.
51 */
52 #ifdef MULTIPROCESSOR
53 #define PMAP_SIZE offsetof(struct pmap, pm_pai[MAXCPUS])
54 #else
55 #define PMAP_SIZE sizeof(struct pmap)
56 #endif
57
58 CTASSERT(sizeof(struct pmap_segtab) == NBPG);
59
60 void
61 pmap_procwr(struct proc *p, vaddr_t va, size_t len)
62 {
63 struct pmap * const pmap = p->p_vmspace->vm_map.pmap;
64 vsize_t off = va & PAGE_SIZE;
65
66 kpreempt_disable();
67 for (const vaddr_t eva = va + len; va < eva; off = 0) {
68 const vaddr_t segeva = min(va + len, va - off + PAGE_SIZE);
69 pt_entry_t * const ptep = pmap_pte_lookup(pmap, va);
70 if (ptep == NULL) {
71 va = segeva;
72 continue;
73 }
74 pt_entry_t pt_entry = *ptep;
75 if (!pte_valid_p(pt_entry) || !pte_exec_p(pt_entry)) {
76 va = segeva;
77 continue;
78 }
79 kpreempt_enable();
80 dcache_wb(pte_to_paddr(pt_entry), segeva - va);
81 icache_inv(pte_to_paddr(pt_entry), segeva - va);
82 kpreempt_disable();
83 va = segeva;
84 }
85 kpreempt_enable();
86 }
87
88 void
89 pmap_md_page_syncicache(struct vm_page *pg)
90 {
91 paddr_t pa = VM_PAGE_TO_PHYS(pg);
92 dcache_wb_page(pa);
93 icache_inv_page(pa);
94 }
95
96 vaddr_t
97 pmap_md_direct_map_paddr(paddr_t pa)
98 {
99 return (vaddr_t) pa;
100 }
101
102 bool
103 pmap_md_direct_mapped_vaddr_p(vaddr_t va)
104 {
105 return va < VM_MIN_KERNEL_ADDRESS || VM_MAX_KERNEL_ADDRESS <= va;
106 }
107
108 paddr_t
109 pmap_md_direct_mapped_vaddr_to_paddr(vaddr_t va)
110 {
111 return (paddr_t) va;
112 }
113
114 /*
115 * Bootstrap the system enough to run with virtual memory.
116 * firstaddr is the first unused kseg0 address (not page aligned).
117 */
118 void
119 pmap_bootstrap(vaddr_t startkernel, vaddr_t endkernel,
120 const phys_ram_seg_t *avail, size_t cnt)
121 {
122 for (size_t i = 0; i < cnt; i++) {
123 printf(" uvm_page_physload(%#lx,%#lx,%#lx,%#lx,%d)",
124 atop(avail[i].start),
125 atop(avail[i].start + avail[i].size) - 1,
126 atop(avail[i].start),
127 atop(avail[i].start + avail[i].size) - 1,
128 VM_FREELIST_DEFAULT);
129 uvm_page_physload(
130 atop(avail[i].start),
131 atop(avail[i].start + avail[i].size) - 1,
132 atop(avail[i].start),
133 atop(avail[i].start + avail[i].size) - 1,
134 VM_FREELIST_DEFAULT);
135 }
136
137 pmap_tlb_info_init(&pmap_tlb0_info); /* init the lock */
138
139 /*
140 * Compute the number of pages kmem_map will have.
141 */
142 kmeminit_nkmempages();
143
144 /*
145 * Figure out how many PTE's are necessary to map the kernel.
146 * We also reserve space for kmem_alloc_pageable() for vm_fork().
147 */
148
149 /* Get size of buffer cache and set an upper limit */
150 buf_setvalimit((VM_MAX_KERNEL_ADDRESS - VM_MIN_KERNEL_ADDRESS) / 8);
151 vsize_t bufsz = buf_memcalc();
152 buf_setvalimit(bufsz);
153
154 vsize_t nsegtabs = pmap_round_seg(VM_PHYS_SIZE
155 + (ubc_nwins << ubc_winshift)
156 + bufsz
157 + 16 * NCARGS
158 + pager_map_size
159 + maxproc * USPACE
160 #ifdef SYSVSHM
161 + NBPG * shminfo.shmall
162 #endif
163 + NBPG * nkmempages);
164
165 /*
166 * Initialize `FYI' variables. Note we're relying on
167 * the fact that BSEARCH sorts the vm_physmem[] array
168 * for us. Must do this before uvm_pageboot_alloc()
169 * can be called.
170 */
171 pmap_limits.avail_start = vm_physmem[0].start << PGSHIFT;
172 pmap_limits.avail_end = vm_physmem[vm_nphysseg - 1].end << PGSHIFT;
173 const vsize_t max_nsegtabs =
174 (pmap_round_seg(VM_MAX_KERNEL_ADDRESS)
175 - pmap_trunc_seg(VM_MIN_KERNEL_ADDRESS)) / NBSEG;
176 if (nsegtabs >= max_nsegtabs) {
177 pmap_limits.virtual_end = VM_MAX_KERNEL_ADDRESS;
178 nsegtabs = max_nsegtabs;
179 } else {
180 pmap_limits.virtual_end = VM_MIN_KERNEL_ADDRESS
181 + nsegtabs * NBSEG;
182 }
183
184 pmap_pvlist_lock_init(curcpu()->ci_ci.dcache_line_size);
185
186 /*
187 * Now actually allocate the kernel PTE array (must be done
188 * after virtual_end is initialized).
189 */
190 vaddr_t segtabs =
191 uvm_pageboot_alloc(NBPG * nsegtabs + sizeof(struct pmap_segtab));
192
193 /*
194 * Initialize the kernel's two-level page level. This only wastes
195 * an extra page for the segment table and allows the user/kernel
196 * access to be common.
197 */
198 struct pmap_segtab * const stp = (void *)segtabs;
199 segtabs += round_page(sizeof(struct pmap_segtab));
200 pt_entry_t **ptp = &stp->seg_tab[VM_MIN_KERNEL_ADDRESS >> SEGSHIFT];
201 for (u_int i = 0; i < nsegtabs; i++, segtabs += NBPG) {
202 *ptp++ = (void *)segtabs;
203 }
204 pmap_kernel()->pm_segtab = stp;
205 curcpu()->ci_pmap_kern_segtab = stp;
206 printf(" kern_segtab=%p", stp);
207
208 #if 0
209 nsegtabs = (physmem + NPTEPG - 1) / NPTEPG;
210 segtabs = uvm_pageboot_alloc(NBPG * nsegtabs);
211 ptp = stp->seg_tab;
212 pt_entry_t pt_entry = PTE_M|PTE_xX|PTE_xR;
213 pt_entry_t *ptep = (void *)segtabs;
214 printf("%s: allocated %lu page table pages for mapping %u pages\n",
215 __func__, nsegtabs, physmem);
216 for (u_int i = 0; i < nsegtabs; i++, segtabs += NBPG, ptp++) {
217 *ptp = ptep;
218 for (u_int j = 0; j < NPTEPG; j++, ptep++) {
219 *ptep = pt_entry;
220 pt_entry += NBPG;
221 }
222 printf(" [%u]=%p (%#x)", i, *ptp, **ptp);
223 pt_entry |= PTE_xW;
224 pt_entry &= ~PTE_xX;
225 }
226
227 /*
228 * Now make everything before the kernel inaccessible.
229 */
230 for (u_int i = 0; i < startkernel / NBPG; i += NBPG) {
231 stp->seg_tab[i >> SEGSHIFT][(i & SEGOFSET) >> PAGE_SHIFT] = 0;
232 }
233 #endif
234
235 /*
236 * Initialize the pools.
237 */
238 pool_init(&pmap_pmap_pool, PMAP_SIZE, 0, 0, 0, "pmappl",
239 &pool_allocator_nointr, IPL_NONE);
240 pool_init(&pmap_pv_pool, sizeof(struct pv_entry), 0, 0, 0, "pvpl",
241 &pmap_pv_page_allocator, IPL_NONE);
242
243 tlb_set_asid(0);
244 }
245
246 struct vm_page *
247 pmap_md_alloc_poolpage(int flags)
248 {
249 /*
250 * Any managed page works for us.
251 */
252 return uvm_pagealloc(NULL, 0, NULL, flags);
253 }
254
255 void
256 pmap_zero_page(paddr_t pa)
257 {
258 // printf("%s(%#lx): calling dcache_zero_page(%#lx)\n", __func__, pa, pa);
259 dcache_zero_page(pa);
260 }
261
262 void
263 pmap_copy_page(paddr_t src, paddr_t dst)
264 {
265 const size_t line_size = curcpu()->ci_ci.dcache_line_size;
266 const paddr_t end = src + PAGE_SIZE;
267
268 while (src < end) {
269 __asm(
270 "dcbt %2,%1" "\n\t" /* touch next src cachline */
271 "dcba 0,%1" "\n\t" /* don't fetch dst cacheline */
272 :: "b"(src), "b"(dst), "b"(line_size));
273 for (u_int i = 0;
274 i < line_size;
275 src += 32, dst += 32, i += 32) {
276 __asm(
277 "lmw 24,0(%0)" "\n\t"
278 "stmw 24,0(%1)"
279 :: "b"(src), "b"(dst)
280 : "r24", "r25", "r26", "r27",
281 "r28", "r29", "r30", "r31");
282 }
283 }
284 }
285
286 void
287 pmap_md_init(void)
288 {
289
290 /* nothing for now */
291 }
292
293 bool
294 pmap_md_io_vaddr_p(vaddr_t va)
295 {
296 return va >= pmap_limits.avail_end
297 && !(VM_MIN_KERNEL_ADDRESS <= va && va < VM_MAX_KERNEL_ADDRESS);
298 }
299
300