xref: /illumos-gate/usr/src/uts/sun4v/cpu/generic.c (revision 459190a5c46206e7885f6a649a055ceb46be49a7)
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 (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 
22 /*
23  * Copyright 2007 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 <sys/systm.h>
31 #include <sys/archsystm.h>
32 #include <sys/machparam.h>
33 #include <sys/machsystm.h>
34 #include <sys/cpu.h>
35 #include <sys/elf_SPARC.h>
36 #include <vm/hat_sfmmu.h>
37 #include <vm/page.h>
38 #include <sys/cpuvar.h>
39 #include <sys/async.h>
40 #include <sys/cmn_err.h>
41 #include <sys/debug.h>
42 #include <sys/dditypes.h>
43 #include <sys/sunddi.h>
44 #include <sys/cpu_module.h>
45 #include <sys/prom_debug.h>
46 #include <sys/vmsystm.h>
47 #include <sys/prom_plat.h>
48 #include <sys/sysmacros.h>
49 #include <sys/intreg.h>
50 #include <sys/machtrap.h>
51 #include <sys/ontrap.h>
52 #include <sys/ivintr.h>
53 #include <sys/atomic.h>
54 #include <sys/panic.h>
55 #include <sys/dtrace.h>
56 #include <vm/seg_spt.h>
57 #include <sys/simulate.h>
58 #include <sys/fault.h>
59 
60 
61 uint_t root_phys_addr_lo_mask = 0xffffffffU;
62 
63 void
64 cpu_setup(void)
65 {
66 	extern int mmu_exported_pagesize_mask;
67 	char *generic_isa_set[] = {
68 	    "sparcv9+vis",
69 	    "sparcv8plus+vis",
70 	    NULL
71 	};
72 
73 	/*
74 	 * The setup common to all CPU modules is done in cpu_setup_common
75 	 * routine.
76 	 */
77 	cpu_setup_common(generic_isa_set);
78 
79 	cache |= (CACHE_PTAG | CACHE_IOCOHERENT);
80 
81 	if (broken_md_flag) {
82 		/*
83 		 * Turn on the missing bits supported by sun4v architecture in
84 		 * MMU pagesize mask returned by MD.
85 		 */
86 		mmu_exported_pagesize_mask |= DEFAULT_SUN4V_MMU_PAGESIZE_MASK;
87 	} else {
88 		/*
89 		 * According to sun4v architecture each processor must
90 		 * support 8K, 64K and 4M page sizes. If any of the page
91 		 * size is missing from page size mask, then panic.
92 		 */
93 		if ((mmu_exported_pagesize_mask &
94 		    DEFAULT_SUN4V_MMU_PAGESIZE_MASK) !=
95 		    DEFAULT_SUN4V_MMU_PAGESIZE_MASK)
96 			cmn_err(CE_PANIC, "machine description"
97 			    " does not have required sun4v page sizes"
98 			    " 8K, 64K and 4M: MD mask is 0x%x",
99 			    mmu_exported_pagesize_mask);
100 	}
101 
102 	/*
103 	 * If processor supports the subset of full 64-bit virtual
104 	 * address space, then set VA hole accordingly.
105 	 */
106 	if (va_bits < VA_ADDRESS_SPACE_BITS) {
107 		hole_start = (caddr_t)(1ull << (va_bits - 1));
108 		hole_end = (caddr_t)(0ull - (1ull << (va_bits - 1)));
109 	} else {
110 		hole_start = hole_end = 0;
111 	}
112 }
113 
114 void
115 cpu_fiximp(struct cpu_node *cpunode)
116 {
117 	/*
118 	 * The Cache node is optional in MD. Therefore in case "Cache"
119 	 * does not exists in MD, set the default L2 cache associativity,
120 	 * size, linesize for generic CPU module.
121 	 */
122 	if (cpunode->ecache_size == 0)
123 		cpunode->ecache_size = 0x100000;
124 	if (cpunode->ecache_linesize == 0)
125 		cpunode->ecache_linesize = 64;
126 	if (cpunode->ecache_associativity == 0)
127 		cpunode->ecache_associativity = 1;
128 }
129 
130 void
131 dtrace_flush_sec(uintptr_t addr)
132 {
133 	pfn_t pfn;
134 	proc_t *procp = ttoproc(curthread);
135 	page_t *pp;
136 	caddr_t va;
137 
138 	pfn = hat_getpfnum(procp->p_as->a_hat, (void *)addr);
139 	if (pfn != -1) {
140 		ASSERT(pf_is_memory(pfn));
141 		pp = page_numtopp_noreclaim(pfn, SE_SHARED);
142 		if (pp != NULL) {
143 			va = ppmapin(pp, PROT_READ | PROT_WRITE, (void *)addr);
144 			/* sparc needs 8-byte align */
145 			doflush((caddr_t)((uintptr_t)va & -8l));
146 			ppmapout(va);
147 			page_unlock(pp);
148 		}
149 	}
150 }
151 
152 void
153 cpu_map_exec_units(struct cpu *cp)
154 {
155 	ASSERT(MUTEX_HELD(&cpu_lock));
156 
157 	/*
158 	 * The cpu_ipipe and cpu_fpu fields are initialized based on
159 	 * the execution unit sharing information from the MD. They
160 	 * default to the CPU id in the absence of such information.
161 	 */
162 	cp->cpu_m.cpu_ipipe = cpunodes[cp->cpu_id].exec_unit_mapping;
163 	if (cp->cpu_m.cpu_ipipe == NO_EU_MAPPING_FOUND)
164 		cp->cpu_m.cpu_ipipe = (id_t)(cp->cpu_id);
165 
166 	cp->cpu_m.cpu_fpu = cpunodes[cp->cpu_id].fpu_mapping;
167 	if (cp->cpu_m.cpu_fpu == NO_EU_MAPPING_FOUND)
168 		cp->cpu_m.cpu_fpu = (id_t)(cp->cpu_id);
169 
170 	cp->cpu_m.cpu_core = (id_t)(cp->cpu_id);
171 }
172 
173 void
174 cpu_init_private(struct cpu *cp)
175 {
176 	cpu_map_exec_units(cp);
177 }
178 
179 /*ARGSUSED*/
180 void
181 cpu_uninit_private(struct cpu *cp)
182 {}
183 
184 /*
185  * Invalidate a TSB. Since this needs to work on all sun4v
186  * architecture compliant processors, we use the old method of
187  * walking the TSB, setting each tag to TSBTAG_INVALID.
188  */
189 void
190 cpu_inv_tsb(caddr_t tsb_base, uint_t tsb_bytes)
191 {
192 	struct tsbe *tsbaddr;
193 
194 	for (tsbaddr = (struct tsbe *)tsb_base;
195 	    (uintptr_t)tsbaddr < (uintptr_t)(tsb_base + tsb_bytes);
196 	    tsbaddr++) {
197 		tsbaddr->tte_tag.tag_inthi = TSBTAG_INVALID;
198 	}
199 }
200 
201 /*
202  * Sun4v kernel must emulate code a generic sun4v processor may not support
203  * i.e. VIS1 and VIS2.
204  */
205 #define	IS_FLOAT(i) (((i) & 0x1000000) != 0)
206 #define	IS_IBIT_SET(x)	(x & 0x2000)
207 #define	IS_VIS1(op, op3)(op == 2 && op3 == 0x36)
208 #define	IS_PARTIAL_OR_SHORT_FLOAT_LD_ST(op, op3, asi)		\
209 		(op == 3 && (op3 == IOP_V8_LDDFA ||		\
210 		op3 == IOP_V8_STDFA) &&	asi > ASI_SNFL)
211 int
212 vis1_partial_support(struct regs *rp, k_siginfo_t *siginfo, uint_t *fault)
213 {
214 	char *badaddr;
215 	int instr;
216 	uint_t	optype, op3, asi;
217 	uint_t	rd, ignor;
218 
219 	if (!USERMODE(rp->r_tstate))
220 		return (-1);
221 
222 	instr = fetch_user_instr((caddr_t)rp->r_pc);
223 
224 	rd = (instr >> 25) & 0x1f;
225 	optype = (instr >> 30) & 0x3;
226 	op3 = (instr >> 19) & 0x3f;
227 	ignor = (instr >> 5) & 0xff;
228 	if (IS_IBIT_SET(instr)) {
229 		asi = (uint32_t)((rp->r_tstate >> TSTATE_ASI_SHIFT) &
230 		    TSTATE_ASI_MASK);
231 	} else {
232 		asi = ignor;
233 	}
234 
235 	if (!IS_VIS1(optype, op3) &&
236 	    !IS_PARTIAL_OR_SHORT_FLOAT_LD_ST(optype, op3, asi)) {
237 		return (-1);
238 	}
239 	switch (simulate_unimp(rp, &badaddr)) {
240 	case SIMU_RETRY:
241 		break;	/* regs are already set up */
242 		/*NOTREACHED*/
243 
244 	case SIMU_SUCCESS:
245 		/*
246 		 * skip the successfully
247 		 * simulated instruction
248 		 */
249 		rp->r_pc = rp->r_npc;
250 		rp->r_npc += 4;
251 		break;
252 		/*NOTREACHED*/
253 
254 	case SIMU_FAULT:
255 		siginfo->si_signo = SIGSEGV;
256 		siginfo->si_code = SEGV_MAPERR;
257 		siginfo->si_addr = badaddr;
258 		*fault = FLTBOUNDS;
259 		break;
260 
261 	case SIMU_DZERO:
262 		siginfo->si_signo = SIGFPE;
263 		siginfo->si_code = FPE_INTDIV;
264 		siginfo->si_addr = (caddr_t)rp->r_pc;
265 		*fault = FLTIZDIV;
266 		break;
267 
268 	case SIMU_UNALIGN:
269 		siginfo->si_signo = SIGBUS;
270 		siginfo->si_code = BUS_ADRALN;
271 		siginfo->si_addr = badaddr;
272 		*fault = FLTACCESS;
273 		break;
274 
275 	case SIMU_ILLEGAL:
276 	default:
277 		siginfo->si_signo = SIGILL;
278 		op3 = (instr >> 19) & 0x3F;
279 		if ((IS_FLOAT(instr) && (op3 == IOP_V8_STQFA) ||
280 		    (op3 == IOP_V8_STDFA)))
281 			siginfo->si_code = ILL_ILLADR;
282 		else
283 			siginfo->si_code = ILL_ILLOPC;
284 		siginfo->si_addr = (caddr_t)rp->r_pc;
285 		*fault = FLTILL;
286 		break;
287 	}
288 	return (0);
289 }
290 
291 /*
292  * Trapstat support for generic sun4v processor
293  */
294 int
295 cpu_trapstat_conf(int cmd)
296 {
297 	int status;
298 
299 	switch (cmd) {
300 	case CPU_TSTATCONF_INIT:
301 	case CPU_TSTATCONF_FINI:
302 	case CPU_TSTATCONF_ENABLE:
303 	case CPU_TSTATCONF_DISABLE:
304 		status = ENOTSUP;
305 		break;
306 
307 	default:
308 		status = EINVAL;
309 		break;
310 	}
311 	return (status);
312 }
313 
314 /*ARGSUSED*/
315 void
316 cpu_trapstat_data(void *buf, uint_t tstat_pgszs)
317 {
318 }
319