xref: /titanic_41/usr/src/uts/sun4v/vm/mach_sfmmu.c (revision 70025d765b044c6d8594bb965a2247a61e991a99)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License, Version 1.0 only
6  * (the "License").  You may not use this file except in compliance
7  * with the License.
8  *
9  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10  * or http://www.opensolaris.org/os/licensing.
11  * See the License for the specific language governing permissions
12  * and limitations under the License.
13  *
14  * When distributing Covered Code, include this CDDL HEADER in each
15  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16  * If applicable, add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your own identifying
18  * information: Portions Copyright [yyyy] [name of copyright owner]
19  *
20  * CDDL HEADER END
21  */
22 /*
23  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 #pragma ident	"%Z%%M%	%I%	%E% SMI"
28 
29 #include <sys/types.h>
30 #include <vm/hat.h>
31 #include <vm/hat_sfmmu.h>
32 #include <vm/page.h>
33 #include <sys/pte.h>
34 #include <sys/systm.h>
35 #include <sys/mman.h>
36 #include <sys/sysmacros.h>
37 #include <sys/machparam.h>
38 #include <sys/vtrace.h>
39 #include <sys/kmem.h>
40 #include <sys/mmu.h>
41 #include <sys/cmn_err.h>
42 #include <sys/cpu.h>
43 #include <sys/cpuvar.h>
44 #include <sys/debug.h>
45 #include <sys/lgrp.h>
46 #include <sys/archsystm.h>
47 #include <sys/machsystm.h>
48 #include <sys/vmsystm.h>
49 #include <sys/bitmap.h>
50 #include <vm/rm.h>
51 #include <sys/t_lock.h>
52 #include <sys/vm_machparam.h>
53 #include <sys/promif.h>
54 #include <sys/prom_isa.h>
55 #include <sys/prom_plat.h>
56 #include <sys/prom_debug.h>
57 #include <sys/privregs.h>
58 #include <sys/bootconf.h>
59 #include <sys/memlist.h>
60 #include <sys/memlist_plat.h>
61 #include <sys/cpu_module.h>
62 #include <sys/reboot.h>
63 #include <sys/kdi.h>
64 #include <sys/hypervisor_api.h>
65 
66 /*
67  * External routines and data structures
68  */
69 extern void	sfmmu_cache_flushcolor(int, pfn_t);
70 
71 /*
72  * Static routines
73  */
74 static void	sfmmu_set_tlb(void);
75 
76 /*
77  * Global Data:
78  */
79 caddr_t	textva, datava;
80 tte_t	ktext_tte, kdata_tte;		/* ttes for kernel text and data */
81 
82 int	enable_bigktsb = 1;
83 
84 tte_t bigktsb_ttes[MAX_BIGKTSB_TTES];
85 int bigktsb_nttes = 0;
86 
87 
88 /*
89  * Controls the logic which enables the use of the
90  * QUAD_LDD_PHYS ASI for TSB accesses.
91  */
92 int	ktsb_phys = 1;
93 
94 #ifdef SET_MMU_STATS
95 struct mmu_stat	mmu_stat_area[NCPU];
96 #endif /* SET_MMU_STATS */
97 
98 #ifdef DEBUG
99 /*
100  * The following two variables control if the hypervisor/hardware will
101  * be used to do the TSB table walk for kernel and user contexts.
102  */
103 int hv_use_0_tsb = 1;
104 int hv_use_non0_tsb = 1;
105 #endif /* DEBUG */
106 
107 static void
108 sfmmu_set_fault_status_area(void)
109 {
110 	caddr_t mmfsa_va;
111 	extern	caddr_t mmu_fault_status_area;
112 
113 	mmfsa_va =
114 	    mmu_fault_status_area + (MMFSA_SIZE  * getprocessorid());
115 	set_mmfsa_scratchpad(mmfsa_va);
116 	prom_set_mmfsa_traptable(&trap_table, va_to_pa(mmfsa_va));
117 }
118 
119 void
120 sfmmu_set_tsbs()
121 {
122 	uint64_t rv;
123 	struct hv_tsb_block *hvbp = &ksfmmup->sfmmu_hvblock;
124 
125 #ifdef DEBUG
126 	if (hv_use_0_tsb == 0)
127 		return;
128 #endif /* DEBUG */
129 
130 	rv = hv_set_ctx0(hvbp->hv_tsb_info_cnt,
131 	    hvbp->hv_tsb_info_pa);
132 	if (rv != H_EOK)
133 		prom_printf("cpu%d: hv_set_ctx0() returned %lx\n",
134 		    getprocessorid(), rv);
135 
136 #ifdef SET_MMU_STATS
137 	ASSERT(getprocessorid() < NCPU);
138 	rv = hv_mmu_set_stat_area(va_to_pa(&mmu_stat_area[getprocessorid()]),
139 	    sizeof (mmu_stat_area[0]));
140 	if (rv != H_EOK)
141 		prom_printf("cpu%d: hv_mmu_set_stat_area() returned %lx\n",
142 		    getprocessorid(), rv);
143 #endif /* SET_MMU_STATS */
144 }
145 
146 /*
147  * This routine remaps the kernel using large ttes
148  * All entries except locked ones will be removed from the tlb.
149  * It assumes that both the text and data segments reside in a separate
150  * 4mb virtual and physical contigous memory chunk.  This routine
151  * is only executed by the first cpu.  The remaining cpus execute
152  * sfmmu_mp_startup() instead.
153  * XXX It assumes that the start of the text segment is KERNELBASE.  It should
154  * actually be based on start.
155  */
156 void
157 sfmmu_remap_kernel(void)
158 {
159 	pfn_t	pfn;
160 	uint_t	attr;
161 	int	flags;
162 
163 	extern char end[];
164 	extern struct as kas;
165 
166 	textva = (caddr_t)(KERNELBASE & MMU_PAGEMASK4M);
167 	pfn = va_to_pfn(textva);
168 	if (pfn == PFN_INVALID)
169 		prom_panic("can't find kernel text pfn");
170 	pfn &= TTE_PFNMASK(TTE4M);
171 
172 	attr = PROC_TEXT | HAT_NOSYNC;
173 	flags = HAT_LOAD_LOCK | SFMMU_NO_TSBLOAD;
174 	sfmmu_memtte(&ktext_tte, pfn, attr, TTE4M);
175 	/*
176 	 * We set the lock bit in the tte to lock the translation in
177 	 * the tlb.
178 	 */
179 	TTE_SET_LOCKED(&ktext_tte);
180 	sfmmu_tteload(kas.a_hat, &ktext_tte, textva, NULL, flags);
181 
182 	datava = (caddr_t)((uintptr_t)end & MMU_PAGEMASK4M);
183 	pfn = va_to_pfn(datava);
184 	if (pfn == PFN_INVALID)
185 		prom_panic("can't find kernel data pfn");
186 	pfn &= TTE_PFNMASK(TTE4M);
187 
188 	attr = PROC_DATA | HAT_NOSYNC;
189 	sfmmu_memtte(&kdata_tte, pfn, attr, TTE4M);
190 	/*
191 	 * We set the lock bit in the tte to lock the translation in
192 	 * the tlb.  We also set the mod bit to avoid taking dirty bit
193 	 * traps on kernel data.
194 	 */
195 	TTE_SET_LOCKED(&kdata_tte);
196 	TTE_SET_LOFLAGS(&kdata_tte, 0, TTE_HWWR_INT);
197 	sfmmu_tteload(kas.a_hat, &kdata_tte, datava,
198 	    (struct page *)NULL, flags);
199 
200 	/*
201 	 * create bigktsb ttes if necessary.
202 	 */
203 	if (enable_bigktsb) {
204 		int i = 0;
205 		caddr_t va = ktsb_base;
206 		size_t tsbsz = ktsb_sz;
207 		tte_t tte;
208 
209 		ASSERT(va >= datava + MMU_PAGESIZE4M);
210 		ASSERT(tsbsz >= MMU_PAGESIZE4M);
211 		ASSERT(IS_P2ALIGNED(tsbsz, tsbsz));
212 		ASSERT(IS_P2ALIGNED(va, tsbsz));
213 		attr = PROC_DATA | HAT_NOSYNC;
214 		while (tsbsz != 0) {
215 			ASSERT(i < MAX_BIGKTSB_TTES);
216 			pfn = va_to_pfn(va);
217 			ASSERT(pfn != PFN_INVALID);
218 			ASSERT((pfn & ~TTE_PFNMASK(TTE4M)) == 0);
219 			sfmmu_memtte(&tte, pfn, attr, TTE4M);
220 			ASSERT(TTE_IS_MOD(&tte));
221 			/*
222 			 * No need to lock if we use physical addresses.
223 			 * Since we invalidate the kernel TSB using virtual
224 			 * addresses, it's an optimization to load them now
225 			 * so that we won't have to load them later.
226 			 */
227 			if (!ktsb_phys) {
228 				TTE_SET_LOCKED(&tte);
229 			}
230 			sfmmu_tteload(kas.a_hat, &tte, va, NULL, flags);
231 			bigktsb_ttes[i] = tte;
232 			va += MMU_PAGESIZE4M;
233 			tsbsz -= MMU_PAGESIZE4M;
234 			i++;
235 		}
236 		bigktsb_nttes = i;
237 	}
238 
239 	sfmmu_set_tlb();
240 }
241 
242 /*
243  * Setup the kernel's locked tte's
244  */
245 void
246 sfmmu_set_tlb(void)
247 {
248 	(void) hv_mmu_map_perm_addr(textva, KCONTEXT, *(uint64_t *)&ktext_tte,
249 	    MAP_ITLB | MAP_DTLB);
250 	(void) hv_mmu_map_perm_addr(datava, KCONTEXT, *(uint64_t *)&kdata_tte,
251 	    MAP_DTLB);
252 
253 	if (!ktsb_phys && enable_bigktsb) {
254 		int i;
255 		caddr_t va = ktsb_base;
256 		uint64_t tte;
257 
258 		ASSERT(bigktsb_nttes <= MAX_BIGKTSB_TTES);
259 		for (i = 0; i < bigktsb_nttes; i++) {
260 			tte = *(uint64_t *)&bigktsb_ttes[i];
261 			(void) hv_mmu_map_perm_addr(va, KCONTEXT, tte,
262 			    MAP_DTLB);
263 			va += MMU_PAGESIZE4M;
264 		}
265 	}
266 }
267 
268 /*
269  * This routine is executed by all other cpus except the first one
270  * at initialization time.  It is responsible for taking over the
271  * mmu from the prom.  We follow these steps.
272  * Lock the kernel's ttes in the TLB
273  * Initialize the tsb hardware registers
274  * Take over the trap table
275  * Flush the prom's locked entries from the TLB
276  */
277 void
278 sfmmu_mp_startup(void)
279 {
280 	sfmmu_set_tlb();
281 	setwstate(WSTATE_KERN);
282 	/*
283 	 * sfmmu_set_fault_status_area() takes over trap_table
284 	 */
285 	sfmmu_set_fault_status_area();
286 	sfmmu_set_tsbs();
287 	install_va_to_tte();
288 }
289 
290 void
291 kdi_tlb_page_lock(caddr_t va, int do_dtlb)
292 {
293 	tte_t tte;
294 	pfn_t pfn = va_to_pfn(va);
295 	uint64_t ret;
296 
297 	sfmmu_memtte(&tte, pfn, (PROC_TEXT | HAT_NOSYNC), TTE8K);
298 	ret = hv_mmu_map_perm_addr(va, KCONTEXT, *(uint64_t *)&tte,
299 	    MAP_ITLB | (do_dtlb ? MAP_DTLB : 0));
300 
301 	if (ret != H_EOK) {
302 		cmn_err(CE_PANIC, "cpu%d: cannot set permanent mapping for "
303 		    "va=0x%p, hv error code 0x%lux",
304 		    getprocessorid(), (void *)va, ret);
305 	}
306 }
307 
308 void
309 kdi_tlb_page_unlock(caddr_t va, int do_dtlb)
310 {
311 	(void) hv_mmu_unmap_perm_addr(va, KCONTEXT,
312 	    MAP_ITLB | (do_dtlb ? MAP_DTLB : 0));
313 }
314 
315 /*
316  * Clear machine specific TSB information for a user process
317  */
318 void
319 sfmmu_clear_utsbinfo()
320 {
321 	(void) hv_set_ctxnon0(0, NULL);
322 }
323 
324 /*
325  * Set machine specific TSB information
326  */
327 void
328 sfmmu_setup_tsbinfo(sfmmu_t *sfmmup)
329 {
330 	struct tsb_info *tsbinfop;
331 	hv_tsb_info_t *tdp;
332 
333 	tsbinfop = sfmmup->sfmmu_tsb;
334 	if (tsbinfop == NULL) {
335 		sfmmup->sfmmu_hvblock.hv_tsb_info_pa = (uint64_t)-1;
336 		sfmmup->sfmmu_hvblock.hv_tsb_info_cnt = 0;
337 		return;
338 	}
339 	tdp = &sfmmup->sfmmu_hvblock.hv_tsb_info[0];
340 	sfmmup->sfmmu_hvblock.hv_tsb_info_pa = va_to_pa(tdp);
341 	sfmmup->sfmmu_hvblock.hv_tsb_info_cnt = 1;
342 	tdp->hvtsb_idxpgsz = TTE8K;
343 	tdp->hvtsb_assoc = 1;
344 	tdp->hvtsb_ntte = TSB_ENTRIES(tsbinfop->tsb_szc);
345 	tdp->hvtsb_ctx_index = 0;
346 	tdp->hvtsb_pgszs = tsbinfop->tsb_ttesz_mask;
347 	tdp->hvtsb_rsvd = 0;
348 	tdp->hvtsb_pa = tsbinfop->tsb_pa;
349 	if ((tsbinfop = tsbinfop->tsb_next) == NULL)
350 		return;
351 	sfmmup->sfmmu_hvblock.hv_tsb_info_cnt++;
352 	tdp++;
353 	tdp->hvtsb_idxpgsz = TTE4M;
354 	tdp->hvtsb_assoc = 1;
355 	tdp->hvtsb_ntte = TSB_ENTRIES(tsbinfop->tsb_szc);
356 	tdp->hvtsb_ctx_index = 0;
357 	tdp->hvtsb_pgszs = tsbinfop->tsb_ttesz_mask;
358 	tdp->hvtsb_rsvd = 0;
359 	tdp->hvtsb_pa = tsbinfop->tsb_pa;
360 	/* Only allow for 2 TSBs */
361 	ASSERT(tsbinfop->tsb_next == NULL);
362 }
363 
364 /*
365  * Invalidate a TSB via processor specific TSB invalidation routine
366  */
367 void
368 sfmmu_inv_tsb(caddr_t tsb_base, uint_t tsb_bytes)
369 {
370 	extern void cpu_inv_tsb(caddr_t, uint_t);
371 
372 	cpu_inv_tsb(tsb_base, tsb_bytes);
373 }
374 
375 /*
376  * Completely flush the D-cache on all cpus.
377  * Not applicable to sun4v.
378  */
379 void
380 sfmmu_cache_flushall()
381 {
382 }
383