xref: /titanic_50/usr/src/uts/sun4/os/mlsetup.c (revision df14233e629298598736976c5bfcf4a31873745f)
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  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
23  * Use is subject to license terms.
24  */
25 
26 #pragma ident	"%Z%%M%	%I%	%E% SMI"
27 
28 #include <sys/types.h>
29 #include <sys/systm.h>
30 #include <sys/archsystm.h>
31 #include <sys/machsystm.h>
32 #include <sys/disp.h>
33 #include <sys/autoconf.h>
34 #include <sys/promif.h>
35 #include <sys/prom_plat.h>
36 #include <sys/promimpl.h>
37 #include <sys/platform_module.h>
38 #include <sys/clock.h>
39 #include <sys/pte.h>
40 #include <sys/scb.h>
41 #include <sys/cpu.h>
42 #include <sys/stack.h>
43 #include <sys/intreg.h>
44 #include <sys/ivintr.h>
45 #include <vm/as.h>
46 #include <vm/hat_sfmmu.h>
47 #include <sys/reboot.h>
48 #include <sys/sysmacros.h>
49 #include <sys/vtrace.h>
50 #include <sys/trap.h>
51 #include <sys/machtrap.h>
52 #include <sys/privregs.h>
53 #include <sys/machpcb.h>
54 #include <sys/proc.h>
55 #include <sys/cpupart.h>
56 #include <sys/pset.h>
57 #include <sys/cpu_module.h>
58 #include <sys/copyops.h>
59 #include <sys/panic.h>
60 #include <sys/bootconf.h>	/* for bootops */
61 #include <sys/pg.h>
62 #include <sys/kdi.h>
63 #include <sys/fpras.h>
64 
65 #include <sys/prom_debug.h>
66 #include <sys/debug.h>
67 
68 #include <sys/sunddi.h>
69 #include <sys/lgrp.h>
70 #include <sys/traptrace.h>
71 
72 #include <sys/kobj_impl.h>
73 #include <sys/kdi_machimpl.h>
74 
75 /*
76  * External Routines:
77  */
78 extern void map_wellknown_devices(void);
79 extern void hsvc_setup(void);
80 extern void mach_descrip_startup_init(void);
81 extern void mach_soft_state_init(void);
82 
83 int	dcache_size;
84 int	dcache_linesize;
85 int	icache_size;
86 int	icache_linesize;
87 int	ecache_size;
88 int	ecache_alignsize;
89 int	ecache_associativity;
90 int	ecache_setsize;			/* max possible e$ setsize */
91 int	cpu_setsize;			/* max e$ setsize of configured cpus */
92 int	dcache_line_mask;		/* spitfire only */
93 int	vac_size;			/* cache size in bytes */
94 uint_t	vac_mask;			/* VAC alignment consistency mask */
95 int	vac_shift;			/* log2(vac_size) for ppmapout() */
96 int	vac = 0;	/* virtual address cache type (none == 0) */
97 
98 /*
99  * fpRAS.  An individual sun4* machine class (or perhaps subclass,
100  * eg sun4u/cheetah) must set fpras_implemented to indicate that it implements
101  * the fpRAS feature.  The feature can be suppressed by setting fpras_disable
102  * or the mechanism can be disabled for individual copy operations with
103  * fpras_disableids.  All these are checked in post_startup() code so
104  * fpras_disable and fpras_disableids can be set in /etc/system.
105  * If/when fpRAS is implemented on non-sun4 architectures these
106  * definitions will need to move up to the common level.
107  */
108 int	fpras_implemented;
109 int	fpras_disable;
110 int	fpras_disableids;
111 
112 /*
113  * Static Routines:
114  */
115 static void kern_splr_preprom(void);
116 static void kern_splx_postprom(void);
117 
118 /*
119  * Setup routine called right before main(). Interposing this function
120  * before main() allows us to call it in a machine-independent fashion.
121  */
122 
123 void
124 mlsetup(struct regs *rp, kfpu_t *fp)
125 {
126 	struct machpcb *mpcb;
127 
128 	extern char t0stack[];
129 	extern struct classfuncs sys_classfuncs;
130 	extern disp_t cpu0_disp;
131 	unsigned long long pa;
132 
133 #ifdef TRAPTRACE
134 	TRAP_TRACE_CTL *ctlp;
135 #endif /* TRAPTRACE */
136 
137 	/* drop into kmdb on boot -d */
138 	if (boothowto & RB_DEBUGENTER)
139 		kmdb_enter();
140 
141 	/*
142 	 * initialize cpu_self
143 	 */
144 	cpu0.cpu_self = &cpu0;
145 
146 	/*
147 	 * initialize t0
148 	 */
149 	t0.t_stk = (caddr_t)rp - REGOFF;
150 	/* Can't use va_to_pa here - wait until prom_ initialized */
151 	t0.t_stkbase = t0stack;
152 	t0.t_pri = maxclsyspri - 3;
153 	t0.t_schedflag = TS_LOAD | TS_DONT_SWAP;
154 	t0.t_procp = &p0;
155 	t0.t_plockp = &p0lock.pl_lock;
156 	t0.t_lwp = &lwp0;
157 	t0.t_forw = &t0;
158 	t0.t_back = &t0;
159 	t0.t_next = &t0;
160 	t0.t_prev = &t0;
161 	t0.t_cpu = &cpu0;			/* loaded by _start */
162 	t0.t_disp_queue = &cpu0_disp;
163 	t0.t_bind_cpu = PBIND_NONE;
164 	t0.t_bind_pset = PS_NONE;
165 	t0.t_cpupart = &cp_default;
166 	t0.t_clfuncs = &sys_classfuncs.thread;
167 	t0.t_copyops = NULL;
168 	THREAD_ONPROC(&t0, CPU);
169 
170 	lwp0.lwp_thread = &t0;
171 	lwp0.lwp_procp = &p0;
172 	lwp0.lwp_regs = (void *)rp;
173 	t0.t_tid = p0.p_lwpcnt = p0.p_lwprcnt = p0.p_lwpid = 1;
174 
175 	mpcb = lwptompcb(&lwp0);
176 	mpcb->mpcb_fpu = fp;
177 	mpcb->mpcb_fpu->fpu_q = mpcb->mpcb_fpu_q;
178 	mpcb->mpcb_thread = &t0;
179 	lwp0.lwp_fpu = (void *)mpcb->mpcb_fpu;
180 
181 	p0.p_exec = NULL;
182 	p0.p_stat = SRUN;
183 	p0.p_flag = SSYS;
184 	p0.p_tlist = &t0;
185 	p0.p_stksize = 2*PAGESIZE;
186 	p0.p_stkpageszc = 0;
187 	p0.p_as = &kas;
188 	p0.p_lockp = &p0lock;
189 	p0.p_utraps = NULL;
190 	p0.p_brkpageszc = 0;
191 	p0.p_t1_lgrpid = LGRP_NONE;
192 	p0.p_tr_lgrpid = LGRP_NONE;
193 	sigorset(&p0.p_ignore, &ignoredefault);
194 
195 	CPU->cpu_thread = &t0;
196 	CPU->cpu_dispthread = &t0;
197 	bzero(&cpu0_disp, sizeof (disp_t));
198 	CPU->cpu_disp = &cpu0_disp;
199 	CPU->cpu_disp->disp_cpu = CPU;
200 	CPU->cpu_idle_thread = &t0;
201 	CPU->cpu_flags = CPU_RUNNING;
202 	CPU->cpu_id = getprocessorid();
203 	CPU->cpu_dispatch_pri = t0.t_pri;
204 
205 	/*
206 	 * Initialize thread/cpu microstate accounting
207 	 */
208 	init_mstate(&t0, LMS_SYSTEM);
209 	init_cpu_mstate(CPU, CMS_SYSTEM);
210 
211 	/*
212 	 * Initialize lists of available and active CPUs.
213 	 */
214 	cpu_list_init(CPU);
215 
216 	cpu_vm_data_init(CPU);
217 
218 	(void) prom_set_preprom(kern_splr_preprom);
219 	(void) prom_set_postprom(kern_splx_postprom);
220 	PRM_INFO("mlsetup: now ok to call prom_printf");
221 
222 	mpcb->mpcb_pa = va_to_pa(t0.t_stk);
223 
224 	/*
225 	 * Claim the physical and virtual resources used by panicbuf,
226 	 * then map panicbuf.  This operation removes the phys and
227 	 * virtual addresses from the free lists.
228 	 */
229 	if (prom_claim_virt(PANICBUFSIZE, panicbuf) != panicbuf)
230 		prom_panic("Can't claim panicbuf virtual address");
231 
232 	if (prom_retain("panicbuf", PANICBUFSIZE, MMU_PAGESIZE, &pa) != 0)
233 		prom_panic("Can't allocate retained panicbuf physical address");
234 
235 	if (prom_map_phys(-1, PANICBUFSIZE, panicbuf, pa) != 0)
236 		prom_panic("Can't map panicbuf");
237 
238 	PRM_DEBUG(panicbuf);
239 	PRM_DEBUG(pa);
240 
241 	/*
242 	 * Negotiate hypervisor services, if any
243 	 */
244 	hsvc_setup();
245 	mach_soft_state_init();
246 
247 #ifdef TRAPTRACE
248 	/*
249 	 * initialize the trap trace buffer for the boot cpu
250 	 * XXX todo, dynamically allocate this buffer too
251 	 */
252 	ctlp = &trap_trace_ctl[CPU->cpu_id];
253 	ctlp->d.vaddr_base = trap_tr0;
254 	ctlp->d.offset = ctlp->d.last_offset = 0;
255 	ctlp->d.limit = TRAP_TSIZE;		/* XXX dynamic someday */
256 	ctlp->d.paddr_base = va_to_pa(trap_tr0);
257 #endif /* TRAPTRACE */
258 
259 	/*
260 	 * Initialize the Machine Description kernel framework
261 	 */
262 
263 	mach_descrip_startup_init();
264 
265 	/*
266 	 * initialize HV trap trace buffer for the boot cpu
267 	 */
268 	mach_htraptrace_setup(CPU->cpu_id);
269 	mach_htraptrace_configure(CPU->cpu_id);
270 
271 	/*
272 	 * lgroup framework initialization. This must be done prior
273 	 * to devices being mapped.
274 	 */
275 	lgrp_init();
276 
277 	cpu_setup();
278 
279 	if (boothowto & RB_HALT) {
280 		prom_printf("unix: kernel halted by -h flag\n");
281 		prom_enter_mon();
282 	}
283 
284 	setcputype();
285 	map_wellknown_devices();
286 	setcpudelay();
287 }
288 
289 /*
290  * These routines are called immediately before and
291  * immediately after calling into the firmware.  The
292  * firmware is significantly confused by preemption -
293  * particularly on MP machines - but also on UP's too.
294  */
295 
296 static int saved_spl;
297 
298 static void
299 kern_splr_preprom(void)
300 {
301 	saved_spl = spl7();
302 }
303 
304 static void
305 kern_splx_postprom(void)
306 {
307 	splx(saved_spl);
308 }
309 
310 
311 /*
312  * WARNING
313  * The code fom here to the end of mlsetup.c runs before krtld has
314  * knitted unix and genunix together.  It can call routines in unix,
315  * but calls into genunix will fail spectacularly.  More specifically,
316  * calls to prom_*, bop_* and str* will work, everything else is
317  * caveat emptor.
318  *
319  * Also note that while #ifdef sun4u is generally a bad idea, they
320  * exist here to concentrate the dangerous code into a single file.
321  */
322 
323 static char *
324 getcpulist(void)
325 {
326 	pnode_t node;
327 	/* big enough for OBP_NAME and for a reasonably sized OBP_COMPATIBLE. */
328 	static char cpubuf[5 * OBP_MAXDRVNAME];
329 	int nlen, clen, i;
330 #ifdef	sun4u
331 	char dname[OBP_MAXDRVNAME];
332 #endif
333 
334 	node = prom_findnode_bydevtype(prom_rootnode(), OBP_CPU);
335 	if (node != OBP_NONODE && node != OBP_BADNODE) {
336 		if ((nlen = prom_getproplen(node, OBP_NAME)) <= 0 ||
337 		    nlen > sizeof (cpubuf) ||
338 		    prom_getprop(node, OBP_NAME, cpubuf) <= 0)
339 			prom_panic("no name in cpu node");
340 
341 		/* nlen includes the terminating null character */
342 #ifdef	sun4v
343 		if ((clen = prom_getproplen(node, OBP_COMPATIBLE)) > 0) {
344 #else	/* sun4u */
345 		/*
346 		 * For the CMT case, need check the parent "core"
347 		 * node for the compatible property.
348 		 */
349 		if ((clen = prom_getproplen(node, OBP_COMPATIBLE)) > 0 ||
350 		    ((node = prom_parentnode(node)) != OBP_NONODE &&
351 		    node != OBP_BADNODE &&
352 		    (clen = prom_getproplen(node, OBP_COMPATIBLE)) > 0 &&
353 		    prom_getprop(node, OBP_DEVICETYPE, dname) > 0 &&
354 		    strcmp(dname, "core") == 0)) {
355 #endif
356 			if ((clen + nlen) > sizeof (cpubuf))
357 				prom_panic("cpu node \"compatible\" too long");
358 			/* read in compatible, leaving space for ':' */
359 			if (prom_getprop(node, OBP_COMPATIBLE,
360 			    &cpubuf[nlen]) != clen)
361 				prom_panic("cpu node \"compatible\" error");
362 			clen += nlen;	/* total length */
363 			/* convert all null characters to ':' */
364 			clen--;	/* except the final one... */
365 			for (i = 0; i < clen; i++)
366 				if (cpubuf[i] == '\0')
367 					cpubuf[i] = ':';
368 		}
369 #ifdef	sun4u
370 		/*
371 		 * Some PROMs return SUNW,UltraSPARC when they actually have
372 		 * SUNW,UltraSPARC-II cpus. SInce we're now filtering out all
373 		 * SUNW,UltraSPARC systems during the boot phase, we can safely
374 		 * point the auxv CPU value at SUNW,UltraSPARC-II.
375 		 */
376 		if (strcmp("SUNW,UltraSPARC", cpubuf) == 0)
377 			(void) strcpy(cpubuf, "SUNW,UltraSPARC-II");
378 #endif
379 		return (cpubuf);
380 	} else
381 		return (NULL);
382 }
383 
384 /*
385  * called immediately from _start to stich the
386  * primary modules together
387  */
388 void
389 kobj_start(void *cif)
390 {
391 	Ehdr *ehdr;
392 	Phdr *phdr;
393 	uint32_t eadr, padr;
394 	val_t bootaux[BA_NUM];
395 	int i;
396 
397 	prom_init("kernel", cif);
398 	bop_init();
399 #ifdef	DEBUG
400 	if (bop_getproplen("stop-me") != -1)
401 		prom_enter_mon();
402 #endif
403 
404 	if (bop_getprop("elfheader-address", (caddr_t)&eadr) == -1)
405 		prom_panic("no ELF image");
406 	ehdr = (Ehdr *)(uintptr_t)eadr;
407 	for (i = 0; i < BA_NUM; i++)
408 		bootaux[i].ba_val = NULL;
409 	bootaux[BA_PHNUM].ba_val = ehdr->e_phnum;
410 	bootaux[BA_PHENT].ba_val = ehdr->e_phentsize;
411 	bootaux[BA_LDNAME].ba_ptr = NULL;
412 
413 	padr = eadr + ehdr->e_phoff;
414 	bootaux[BA_PHDR].ba_ptr = (void *)(uintptr_t)padr;
415 	for (i = 0; i < ehdr->e_phnum; i++) {
416 		phdr = (Phdr *)((uintptr_t)padr + i * ehdr->e_phentsize);
417 		if (phdr->p_type == PT_DYNAMIC) {
418 			bootaux[BA_DYNAMIC].ba_ptr = (void *)phdr->p_vaddr;
419 			break;
420 		}
421 	}
422 
423 	bootaux[BA_LPAGESZ].ba_val = MMU_PAGESIZE4M;
424 	bootaux[BA_PAGESZ].ba_val = MMU_PAGESIZE;
425 	bootaux[BA_IFLUSH].ba_val = 1;
426 	bootaux[BA_CPU].ba_ptr = getcpulist();
427 	bootaux[BA_MMU].ba_ptr = NULL;
428 
429 	kobj_init(cif, NULL, bootops, bootaux);
430 
431 	/* kernel stitched together; we can now test #pragma's */
432 	if (&plat_setprop_enter != NULL) {
433 		prom_setprop_enter = &plat_setprop_enter;
434 		prom_setprop_exit = &plat_setprop_exit;
435 		ASSERT(prom_setprop_exit != NULL);
436 	}
437 
438 }
439 
440 /*
441  * Create modpath from kernel name.
442  * If we booted:
443  *  /platform/`uname -i`/kernel/sparcv9/unix
444  *   or
445  *  /platform/`uname -m`/kernel/sparcv9/unix
446  *
447  * then make the modpath:
448  *  /platform/`uname -i`/kernel /platform/`uname -m`/kernel
449  *
450  * otherwise, make the modpath the dir the kernel was
451  * loaded from, minus any sparcv9 extension
452  *
453  * note the sparcv9 dir is optional since a unix -> sparcv9/unix
454  * symlink is available as a shortcut.
455  */
456 void
457 mach_modpath(char *path, const char *fname)
458 {
459 	char *p;
460 	int len, compat;
461 	const char prefix[] = "/platform/";
462 	char platname[MAXPATHLEN];
463 #ifdef	sun4u
464 	char defname[] = "sun4u";
465 #else
466 	char defname[] = "sun4v";
467 #endif
468 	const char suffix[] = "/kernel";
469 	const char isastr[] = "/sparcv9";
470 
471 	/*
472 	 * check for /platform
473 	 */
474 	p = (char *)fname;
475 	if (strncmp(p, prefix, sizeof (prefix) - 1) != 0)
476 		goto nopath;
477 	p += sizeof (prefix) - 1;
478 
479 	/*
480 	 * check for the default name or the platform name.
481 	 * also see if we used the 'compatible' name
482 	 * (platname == default)
483 	 */
484 	(void) bop_getprop("impl-arch-name", platname);
485 	compat = strcmp(platname, defname) == 0;
486 	len = strlen(platname);
487 	if (strncmp(p, platname, len) == 0)
488 		p += len;
489 	else if (strncmp(p, defname, sizeof (defname) - 1) == 0)
490 		p += sizeof (defname) - 1;
491 	else
492 		goto nopath;
493 
494 	/*
495 	 * check for /kernel/sparcv9 or just /kernel
496 	 */
497 	if (strncmp(p, suffix, sizeof (suffix) - 1) != 0)
498 		goto nopath;
499 	p += sizeof (suffix) - 1;
500 	if (strncmp(p, isastr, sizeof (isastr) - 1) == 0)
501 		p += sizeof (isastr) - 1;
502 
503 	/*
504 	 * check we're at the last component
505 	 */
506 	if (p != strrchr(fname, '/'))
507 		goto nopath;
508 
509 	/*
510 	 * everything is kosher; setup modpath
511 	 */
512 	(void) strcpy(path, "/platform/");
513 	(void) strcat(path, platname);
514 	(void) strcat(path, "/kernel ");
515 	if (!compat) {
516 		(void) strcat(path, "/platform/");
517 		(void) strcat(path, defname);
518 		(void) strcat(path, "/kernel ");
519 	}
520 	return;
521 
522 nopath:
523 	/*
524 	 * Construct the directory path from the filename.
525 	 */
526 	if ((p = strrchr(fname, '/')) == NULL)
527 		return;
528 
529 	while (p > fname && *(p - 1) == '/')
530 		p--;	/* remove trailing '/' characters */
531 	if (p == fname)
532 		p++;	/* so "/" -is- the modpath in this case */
533 
534 	/*
535 	 * Remove optional isa-dependent directory name - the module
536 	 * subsystem will put this back again (!)
537 	 */
538 	len = p - fname;
539 	if (len > sizeof (isastr) - 1 &&
540 	    strncmp(&fname[len - (sizeof (isastr) - 1)], isastr,
541 	    sizeof (isastr) - 1) == 0)
542 		p -= sizeof (isastr) - 1;
543 	(void) strncpy(path, fname, p - fname);
544 }
545