xref: /freebsd-12.1/stand/sparc64/loader/main.c (revision cefee548)
1 /*-
2  * Initial implementation:
3  * Copyright (c) 2001 Robert Drehmel
4  * All rights reserved.
5  *
6  * As long as the above copyright statement and this notice remain
7  * unchanged, you can do what ever you want with this file.
8  */
9 /*-
10  * Copyright (c) 2008 - 2012 Marius Strobl <[email protected]>
11  * All rights reserved.
12  *
13  * Redistribution and use in source and binary forms, with or without
14  * modification, are permitted provided that the following conditions
15  * are met:
16  * 1. Redistributions of source code must retain the above copyright
17  *    notice, this list of conditions and the following disclaimer.
18  * 2. Redistributions in binary form must reproduce the above copyright
19  *    notice, this list of conditions and the following disclaimer in the
20  *    documentation and/or other materials provided with the distribution.
21  *
22  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
23  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
26  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32  * SUCH DAMAGE.
33  */
34 
35 #include <sys/cdefs.h>
36 __FBSDID("$FreeBSD$");
37 
38 /*
39  * FreeBSD/sparc64 kernel loader - machine dependent part
40  *
41  *  - implements copyin and readin functions that map kernel
42  *    pages on demand.  The machine independent code does not
43  *    know the size of the kernel early enough to pre-enter
44  *    TTEs and install just one 4MB mapping seemed to limiting
45  *    to me.
46  */
47 
48 #include <stand.h>
49 #include <sys/param.h>
50 #include <sys/exec.h>
51 #include <sys/linker.h>
52 #include <sys/queue.h>
53 #include <sys/types.h>
54 #ifdef LOADER_ZFS_SUPPORT
55 #include <sys/vtoc.h>
56 #include "libzfs.h"
57 #endif
58 
59 #include <vm/vm.h>
60 #include <machine/asi.h>
61 #include <machine/cmt.h>
62 #include <machine/cpufunc.h>
63 #include <machine/elf.h>
64 #include <machine/fireplane.h>
65 #include <machine/jbus.h>
66 #include <machine/lsu.h>
67 #include <machine/metadata.h>
68 #include <machine/tte.h>
69 #include <machine/tlb.h>
70 #include <machine/upa.h>
71 #include <machine/ver.h>
72 #include <machine/vmparam.h>
73 
74 #include "bootstrap.h"
75 #include "libofw.h"
76 #include "dev_net.h"
77 
78 enum {
79 	HEAPVA		= 0x800000,
80 	HEAPSZ		= 0x3000000,
81 	LOADSZ		= 0x1000000	/* for kernel and modules */
82 };
83 
84 /* At least Sun Fire V1280 require page sized allocations to be claimed. */
85 CTASSERT(HEAPSZ % PAGE_SIZE == 0);
86 
87 static struct mmu_ops {
88 	void (*tlb_init)(void);
89 	int (*mmu_mapin)(vm_offset_t va, vm_size_t len);
90 } *mmu_ops;
91 
92 typedef void kernel_entry_t(vm_offset_t mdp, u_long o1, u_long o2, u_long o3,
93     void *openfirmware);
94 
95 static inline u_long dtlb_get_data_sun4u(u_int, u_int);
96 static int dtlb_enter_sun4u(u_int, u_long data, vm_offset_t);
97 static vm_offset_t dtlb_va_to_pa_sun4u(vm_offset_t);
98 static inline u_long itlb_get_data_sun4u(u_int, u_int);
99 static int itlb_enter_sun4u(u_int, u_long data, vm_offset_t);
100 static vm_offset_t itlb_va_to_pa_sun4u(vm_offset_t);
101 static void itlb_relocate_locked0_sun4u(void);
102 static int sparc64_autoload(void);
103 static ssize_t sparc64_readin(const int, vm_offset_t, const size_t);
104 static ssize_t sparc64_copyin(const void *, vm_offset_t, size_t);
105 static vm_offset_t claim_virt(vm_offset_t, size_t, int);
106 static vm_offset_t alloc_phys(size_t, int);
107 static int map_phys(int, size_t, vm_offset_t, vm_offset_t);
108 static void release_phys(vm_offset_t, u_int);
109 static int __elfN(exec)(struct preloaded_file *);
110 static int mmu_mapin_sun4u(vm_offset_t, vm_size_t);
111 static vm_offset_t init_heap(void);
112 static phandle_t find_bsp_sun4u(phandle_t, uint32_t);
113 const char *cpu_cpuid_prop_sun4u(void);
114 uint32_t cpu_get_mid_sun4u(void);
115 static void tlb_init_sun4u(void);
116 
117 #ifdef LOADER_DEBUG
118 typedef uint64_t tte_t;
119 
120 static void pmap_print_tlb_sun4u(void);
121 static void pmap_print_tte_sun4u(tte_t, tte_t);
122 #endif
123 
124 static struct mmu_ops mmu_ops_sun4u = { tlb_init_sun4u, mmu_mapin_sun4u };
125 
126 /* sun4u */
127 struct tlb_entry *dtlb_store;
128 struct tlb_entry *itlb_store;
129 u_int dtlb_slot;
130 u_int itlb_slot;
131 static int cpu_impl;
132 static u_int dtlb_slot_max;
133 static u_int itlb_slot_max;
134 static u_int tlb_locked;
135 
136 static vm_offset_t curkva = 0;
137 static vm_offset_t heapva;
138 
139 static char bootpath[64];
140 static phandle_t root;
141 
142 #ifdef LOADER_ZFS_SUPPORT
143 static struct zfs_devdesc zfs_currdev;
144 #endif
145 
146 /*
147  * Machine dependent structures that the machine independent
148  * loader part uses.
149  */
150 struct devsw *devsw[] = {
151 #ifdef LOADER_DISK_SUPPORT
152 	&ofwdisk,
153 #endif
154 #ifdef LOADER_NET_SUPPORT
155 	&netdev,
156 #endif
157 #ifdef LOADER_ZFS_SUPPORT
158 	&zfs_dev,
159 #endif
160 	NULL
161 };
162 
163 struct arch_switch archsw;
164 
165 static struct file_format sparc64_elf = {
166 	__elfN(loadfile),
167 	__elfN(exec)
168 };
169 
170 struct file_format *file_formats[] = {
171 	&sparc64_elf,
172 	NULL
173 };
174 
175 struct fs_ops *file_system[] = {
176 #ifdef LOADER_ZFS_SUPPORT
177 	&zfs_fsops,
178 #endif
179 #ifdef LOADER_UFS_SUPPORT
180 	&ufs_fsops,
181 #endif
182 #ifdef LOADER_CD9660_SUPPORT
183 	&cd9660_fsops,
184 #endif
185 #ifdef LOADER_ZIP_SUPPORT
186 	&zipfs_fsops,
187 #endif
188 #ifdef LOADER_GZIP_SUPPORT
189 	&gzipfs_fsops,
190 #endif
191 #ifdef LOADER_BZIP2_SUPPORT
192 	&bzipfs_fsops,
193 #endif
194 #ifdef LOADER_NFS_SUPPORT
195 	&nfs_fsops,
196 #endif
197 #ifdef LOADER_TFTP_SUPPORT
198 	&tftp_fsops,
199 #endif
200 	NULL
201 };
202 
203 struct netif_driver *netif_drivers[] = {
204 #ifdef LOADER_NET_SUPPORT
205 	&ofwnet,
206 #endif
207 	NULL
208 };
209 
210 extern struct console ofwconsole;
211 struct console *consoles[] = {
212 	&ofwconsole,
213 	NULL
214 };
215 
216 #ifdef LOADER_DEBUG
217 static int
watch_phys_set_mask(vm_offset_t pa,u_long mask)218 watch_phys_set_mask(vm_offset_t pa, u_long mask)
219 {
220 	u_long lsucr;
221 
222 	stxa(AA_DMMU_PWPR, ASI_DMMU, pa & (((2UL << 38) - 1) << 3));
223 	lsucr = ldxa(0, ASI_LSU_CTL_REG);
224 	lsucr = ((lsucr | LSU_PW) & ~LSU_PM_MASK) |
225 	    (mask << LSU_PM_SHIFT);
226 	stxa(0, ASI_LSU_CTL_REG, lsucr);
227 	return (0);
228 }
229 
230 static int
watch_phys_set(vm_offset_t pa,int sz)231 watch_phys_set(vm_offset_t pa, int sz)
232 {
233 	u_long off;
234 
235 	off = (u_long)pa & 7;
236 	/* Test for misaligned watch points. */
237 	if (off + sz > 8)
238 		return (-1);
239 	return (watch_phys_set_mask(pa, ((1 << sz) - 1) << off));
240 }
241 
242 
243 static int
watch_virt_set_mask(vm_offset_t va,u_long mask)244 watch_virt_set_mask(vm_offset_t va, u_long mask)
245 {
246 	u_long lsucr;
247 
248 	stxa(AA_DMMU_VWPR, ASI_DMMU, va & (((2UL << 41) - 1) << 3));
249 	lsucr = ldxa(0, ASI_LSU_CTL_REG);
250 	lsucr = ((lsucr | LSU_VW) & ~LSU_VM_MASK) |
251 	    (mask << LSU_VM_SHIFT);
252 	stxa(0, ASI_LSU_CTL_REG, lsucr);
253 	return (0);
254 }
255 
256 static int
watch_virt_set(vm_offset_t va,int sz)257 watch_virt_set(vm_offset_t va, int sz)
258 {
259 	u_long off;
260 
261 	off = (u_long)va & 7;
262 	/* Test for misaligned watch points. */
263 	if (off + sz > 8)
264 		return (-1);
265 	return (watch_virt_set_mask(va, ((1 << sz) - 1) << off));
266 }
267 #endif
268 
269 /*
270  * archsw functions
271  */
272 static int
sparc64_autoload(void)273 sparc64_autoload(void)
274 {
275 
276 	return (0);
277 }
278 
279 static ssize_t
sparc64_readin(const int fd,vm_offset_t va,const size_t len)280 sparc64_readin(const int fd, vm_offset_t va, const size_t len)
281 {
282 
283 	mmu_ops->mmu_mapin(va, len);
284 	return (read(fd, (void *)va, len));
285 }
286 
287 static ssize_t
sparc64_copyin(const void * src,vm_offset_t dest,size_t len)288 sparc64_copyin(const void *src, vm_offset_t dest, size_t len)
289 {
290 
291 	mmu_ops->mmu_mapin(dest, len);
292 	memcpy((void *)dest, src, len);
293 	return (len);
294 }
295 
296 /*
297  * other MD functions
298  */
299 static vm_offset_t
claim_virt(vm_offset_t virt,size_t size,int align)300 claim_virt(vm_offset_t virt, size_t size, int align)
301 {
302 	vm_offset_t mva;
303 
304 	if (OF_call_method("claim", mmu, 3, 1, virt, size, align, &mva) == -1)
305 		return ((vm_offset_t)-1);
306 	return (mva);
307 }
308 
309 static vm_offset_t
alloc_phys(size_t size,int align)310 alloc_phys(size_t size, int align)
311 {
312 	cell_t phys_hi, phys_low;
313 
314 	if (OF_call_method("claim", memory, 2, 2, size, align, &phys_low,
315 	    &phys_hi) == -1)
316 		return ((vm_offset_t)-1);
317 	return ((vm_offset_t)phys_hi << 32 | phys_low);
318 }
319 
320 static int
map_phys(int mode,size_t size,vm_offset_t virt,vm_offset_t phys)321 map_phys(int mode, size_t size, vm_offset_t virt, vm_offset_t phys)
322 {
323 
324 	return (OF_call_method("map", mmu, 5, 0, (uint32_t)phys,
325 	    (uint32_t)(phys >> 32), virt, size, mode));
326 }
327 
328 static void
release_phys(vm_offset_t phys,u_int size)329 release_phys(vm_offset_t phys, u_int size)
330 {
331 
332 	(void)OF_call_method("release", memory, 3, 0, (uint32_t)phys,
333 	    (uint32_t)(phys >> 32), size);
334 }
335 
336 static int
__elfN(exec)337 __elfN(exec)(struct preloaded_file *fp)
338 {
339 	struct file_metadata *fmp;
340 	vm_offset_t mdp;
341 	Elf_Addr entry;
342 	Elf_Ehdr *e;
343 	int error;
344 
345 	if ((fmp = file_findmetadata(fp, MODINFOMD_ELFHDR)) == 0)
346 		return (EFTYPE);
347 	e = (Elf_Ehdr *)&fmp->md_data;
348 
349 	if ((error = md_load64(fp->f_args, &mdp, NULL)) != 0)
350 		return (error);
351 
352 	printf("jumping to kernel entry at %#lx.\n", e->e_entry);
353 #ifdef LOADER_DEBUG
354 	pmap_print_tlb_sun4u();
355 #endif
356 
357 	dev_cleanup();
358 
359 	entry = e->e_entry;
360 
361 	OF_release((void *)heapva, HEAPSZ);
362 
363 	((kernel_entry_t *)entry)(mdp, 0, 0, 0, openfirmware);
364 
365 	panic("%s: exec returned", __func__);
366 }
367 
368 static inline u_long
dtlb_get_data_sun4u(u_int tlb,u_int slot)369 dtlb_get_data_sun4u(u_int tlb, u_int slot)
370 {
371 	u_long data, pstate;
372 
373 	slot = TLB_DAR_SLOT(tlb, slot);
374 	/*
375 	 * We read ASI_DTLB_DATA_ACCESS_REG twice back-to-back in order to
376 	 * work around errata of USIII and beyond.
377 	 */
378 	pstate = rdpr(pstate);
379 	wrpr(pstate, pstate & ~PSTATE_IE, 0);
380 	(void)ldxa(slot, ASI_DTLB_DATA_ACCESS_REG);
381 	data = ldxa(slot, ASI_DTLB_DATA_ACCESS_REG);
382 	wrpr(pstate, pstate, 0);
383 	return (data);
384 }
385 
386 static inline u_long
itlb_get_data_sun4u(u_int tlb,u_int slot)387 itlb_get_data_sun4u(u_int tlb, u_int slot)
388 {
389 	u_long data, pstate;
390 
391 	slot = TLB_DAR_SLOT(tlb, slot);
392 	/*
393 	 * We read ASI_DTLB_DATA_ACCESS_REG twice back-to-back in order to
394 	 * work around errata of USIII and beyond.
395 	 */
396 	pstate = rdpr(pstate);
397 	wrpr(pstate, pstate & ~PSTATE_IE, 0);
398 	(void)ldxa(slot, ASI_ITLB_DATA_ACCESS_REG);
399 	data = ldxa(slot, ASI_ITLB_DATA_ACCESS_REG);
400 	wrpr(pstate, pstate, 0);
401 	return (data);
402 }
403 
404 static vm_offset_t
dtlb_va_to_pa_sun4u(vm_offset_t va)405 dtlb_va_to_pa_sun4u(vm_offset_t va)
406 {
407 	u_long pstate, reg;
408 	u_int i, tlb;
409 
410 	pstate = rdpr(pstate);
411 	wrpr(pstate, pstate & ~PSTATE_IE, 0);
412 	for (i = 0; i < dtlb_slot_max; i++) {
413 		reg = ldxa(TLB_DAR_SLOT(tlb_locked, i),
414 		    ASI_DTLB_TAG_READ_REG);
415 		if (TLB_TAR_VA(reg) != va)
416 			continue;
417 		reg = dtlb_get_data_sun4u(tlb_locked, i);
418 		wrpr(pstate, pstate, 0);
419 		reg >>= TD_PA_SHIFT;
420 		if (cpu_impl == CPU_IMPL_SPARC64V ||
421 		    cpu_impl >= CPU_IMPL_ULTRASPARCIII)
422 			return (reg & TD_PA_CH_MASK);
423 		return (reg & TD_PA_SF_MASK);
424 	}
425 	wrpr(pstate, pstate, 0);
426 	return (-1);
427 }
428 
429 static vm_offset_t
itlb_va_to_pa_sun4u(vm_offset_t va)430 itlb_va_to_pa_sun4u(vm_offset_t va)
431 {
432 	u_long pstate, reg;
433 	int i;
434 
435 	pstate = rdpr(pstate);
436 	wrpr(pstate, pstate & ~PSTATE_IE, 0);
437 	for (i = 0; i < itlb_slot_max; i++) {
438 		reg = ldxa(TLB_DAR_SLOT(tlb_locked, i),
439 		    ASI_ITLB_TAG_READ_REG);
440 		if (TLB_TAR_VA(reg) != va)
441 			continue;
442 		reg = itlb_get_data_sun4u(tlb_locked, i);
443 		wrpr(pstate, pstate, 0);
444 		reg >>= TD_PA_SHIFT;
445 		if (cpu_impl == CPU_IMPL_SPARC64V ||
446 		    cpu_impl >= CPU_IMPL_ULTRASPARCIII)
447 			return (reg & TD_PA_CH_MASK);
448 		return (reg & TD_PA_SF_MASK);
449 	}
450 	wrpr(pstate, pstate, 0);
451 	return (-1);
452 }
453 
454 static int
dtlb_enter_sun4u(u_int index,u_long data,vm_offset_t virt)455 dtlb_enter_sun4u(u_int index, u_long data, vm_offset_t virt)
456 {
457 
458 	return (OF_call_method("SUNW,dtlb-load", mmu, 3, 0, index, data,
459 	    virt));
460 }
461 
462 static int
itlb_enter_sun4u(u_int index,u_long data,vm_offset_t virt)463 itlb_enter_sun4u(u_int index, u_long data, vm_offset_t virt)
464 {
465 
466 	if (cpu_impl == CPU_IMPL_ULTRASPARCIIIp && index == 0 &&
467 	    (data & TD_L) != 0)
468 		panic("%s: won't enter locked TLB entry at index 0 on USIII+",
469 		    __func__);
470 	return (OF_call_method("SUNW,itlb-load", mmu, 3, 0, index, data,
471 	    virt));
472 }
473 
474 static void
itlb_relocate_locked0_sun4u(void)475 itlb_relocate_locked0_sun4u(void)
476 {
477 	u_long data, pstate, tag;
478 	int i;
479 
480 	if (cpu_impl != CPU_IMPL_ULTRASPARCIIIp)
481 		return;
482 
483 	pstate = rdpr(pstate);
484 	wrpr(pstate, pstate & ~PSTATE_IE, 0);
485 
486 	data = itlb_get_data_sun4u(tlb_locked, 0);
487 	if ((data & (TD_V | TD_L)) != (TD_V | TD_L)) {
488 		wrpr(pstate, pstate, 0);
489 		return;
490 	}
491 
492 	/* Flush the mapping of slot 0. */
493 	tag = ldxa(TLB_DAR_SLOT(tlb_locked, 0), ASI_ITLB_TAG_READ_REG);
494 	stxa(TLB_DEMAP_VA(TLB_TAR_VA(tag)) | TLB_DEMAP_PRIMARY |
495 	    TLB_DEMAP_PAGE, ASI_IMMU_DEMAP, 0);
496 	flush(0);	/* The USIII-family ignores the address. */
497 
498 	/*
499 	 * Search a replacement slot != 0 and enter the data and tag
500 	 * that formerly were in slot 0.
501 	 */
502 	for (i = 1; i < itlb_slot_max; i++) {
503 		if ((itlb_get_data_sun4u(tlb_locked, i) & TD_V) != 0)
504 			continue;
505 
506 		stxa(AA_IMMU_TAR, ASI_IMMU, tag);
507 		stxa(TLB_DAR_SLOT(tlb_locked, i), ASI_ITLB_DATA_ACCESS_REG,
508 		    data);
509 		flush(0);	/* The USIII-family ignores the address. */
510 		break;
511 	}
512 	wrpr(pstate, pstate, 0);
513 	if (i == itlb_slot_max)
514 		panic("%s: could not find a replacement slot", __func__);
515 }
516 
517 static int
mmu_mapin_sun4u(vm_offset_t va,vm_size_t len)518 mmu_mapin_sun4u(vm_offset_t va, vm_size_t len)
519 {
520 	vm_offset_t pa, mva;
521 	u_long data;
522 	u_int index;
523 
524 	if (va + len > curkva)
525 		curkva = va + len;
526 
527 	pa = (vm_offset_t)-1;
528 	len += va & PAGE_MASK_4M;
529 	va &= ~PAGE_MASK_4M;
530 	while (len) {
531 		if (dtlb_va_to_pa_sun4u(va) == (vm_offset_t)-1 ||
532 		    itlb_va_to_pa_sun4u(va) == (vm_offset_t)-1) {
533 			/* Allocate a physical page, claim the virtual area. */
534 			if (pa == (vm_offset_t)-1) {
535 				pa = alloc_phys(PAGE_SIZE_4M, PAGE_SIZE_4M);
536 				if (pa == (vm_offset_t)-1)
537 					panic("%s: out of memory", __func__);
538 				mva = claim_virt(va, PAGE_SIZE_4M, 0);
539 				if (mva != va)
540 					panic("%s: can't claim virtual page "
541 					    "(wanted %#lx, got %#lx)",
542 					    __func__, va, mva);
543 				/*
544 				 * The mappings may have changed, be paranoid.
545 				 */
546 				continue;
547 			}
548 			/*
549 			 * Actually, we can only allocate two pages less at
550 			 * most (depending on the kernel TSB size).
551 			 */
552 			if (dtlb_slot >= dtlb_slot_max)
553 				panic("%s: out of dtlb_slots", __func__);
554 			if (itlb_slot >= itlb_slot_max)
555 				panic("%s: out of itlb_slots", __func__);
556 			data = TD_V | TD_4M | TD_PA(pa) | TD_L | TD_CP |
557 			    TD_CV | TD_P | TD_W;
558 			dtlb_store[dtlb_slot].te_pa = pa;
559 			dtlb_store[dtlb_slot].te_va = va;
560 			index = dtlb_slot_max - dtlb_slot - 1;
561 			if (dtlb_enter_sun4u(index, data, va) < 0)
562 				panic("%s: can't enter dTLB slot %d data "
563 				    "%#lx va %#lx", __func__, index, data,
564 				    va);
565 			dtlb_slot++;
566 			itlb_store[itlb_slot].te_pa = pa;
567 			itlb_store[itlb_slot].te_va = va;
568 			index = itlb_slot_max - itlb_slot - 1;
569 			if (itlb_enter_sun4u(index, data, va) < 0)
570 				panic("%s: can't enter iTLB slot %d data "
571 				    "%#lx va %#lxd", __func__, index, data,
572 				    va);
573 			itlb_slot++;
574 			pa = (vm_offset_t)-1;
575 		}
576 		len -= len > PAGE_SIZE_4M ? PAGE_SIZE_4M : len;
577 		va += PAGE_SIZE_4M;
578 	}
579 	if (pa != (vm_offset_t)-1)
580 		release_phys(pa, PAGE_SIZE_4M);
581 	return (0);
582 }
583 
584 static vm_offset_t
init_heap(void)585 init_heap(void)
586 {
587 
588 	/* There is no need for continuous physical heap memory. */
589 	heapva = (vm_offset_t)OF_claim((void *)HEAPVA, HEAPSZ, 32);
590 	return (heapva);
591 }
592 
593 static phandle_t
find_bsp_sun4u(phandle_t node,uint32_t bspid)594 find_bsp_sun4u(phandle_t node, uint32_t bspid)
595 {
596 	char type[sizeof("cpu")];
597 	phandle_t child;
598 	uint32_t cpuid;
599 
600 	for (; node > 0; node = OF_peer(node)) {
601 		child = OF_child(node);
602 		if (child > 0) {
603 			child = find_bsp_sun4u(child, bspid);
604 			if (child > 0)
605 				return (child);
606 		} else {
607 			if (OF_getprop(node, "device_type", type,
608 			    sizeof(type)) <= 0)
609 				continue;
610 			if (strcmp(type, "cpu") != 0)
611 				continue;
612 			if (OF_getprop(node, cpu_cpuid_prop_sun4u(), &cpuid,
613 			    sizeof(cpuid)) <= 0)
614 				continue;
615 			if (cpuid == bspid)
616 				return (node);
617 		}
618 	}
619 	return (0);
620 }
621 
622 const char *
cpu_cpuid_prop_sun4u(void)623 cpu_cpuid_prop_sun4u(void)
624 {
625 
626 	switch (cpu_impl) {
627 	case CPU_IMPL_SPARC64:
628 	case CPU_IMPL_SPARC64V:
629 	case CPU_IMPL_ULTRASPARCI:
630 	case CPU_IMPL_ULTRASPARCII:
631 	case CPU_IMPL_ULTRASPARCIIi:
632 	case CPU_IMPL_ULTRASPARCIIe:
633 		return ("upa-portid");
634 	case CPU_IMPL_ULTRASPARCIII:
635 	case CPU_IMPL_ULTRASPARCIIIp:
636 	case CPU_IMPL_ULTRASPARCIIIi:
637 	case CPU_IMPL_ULTRASPARCIIIip:
638 		return ("portid");
639 	case CPU_IMPL_ULTRASPARCIV:
640 	case CPU_IMPL_ULTRASPARCIVp:
641 		return ("cpuid");
642 	default:
643 		return ("");
644 	}
645 }
646 
647 uint32_t
cpu_get_mid_sun4u(void)648 cpu_get_mid_sun4u(void)
649 {
650 
651 	switch (cpu_impl) {
652 	case CPU_IMPL_SPARC64:
653 	case CPU_IMPL_SPARC64V:
654 	case CPU_IMPL_ULTRASPARCI:
655 	case CPU_IMPL_ULTRASPARCII:
656 	case CPU_IMPL_ULTRASPARCIIi:
657 	case CPU_IMPL_ULTRASPARCIIe:
658 		return (UPA_CR_GET_MID(ldxa(0, ASI_UPA_CONFIG_REG)));
659 	case CPU_IMPL_ULTRASPARCIII:
660 	case CPU_IMPL_ULTRASPARCIIIp:
661 		return (FIREPLANE_CR_GET_AID(ldxa(AA_FIREPLANE_CONFIG,
662 		    ASI_FIREPLANE_CONFIG_REG)));
663 	case CPU_IMPL_ULTRASPARCIIIi:
664 	case CPU_IMPL_ULTRASPARCIIIip:
665 		return (JBUS_CR_GET_JID(ldxa(0, ASI_JBUS_CONFIG_REG)));
666 	case CPU_IMPL_ULTRASPARCIV:
667 	case CPU_IMPL_ULTRASPARCIVp:
668 		return (INTR_ID_GET_ID(ldxa(AA_INTR_ID, ASI_INTR_ID)));
669 	default:
670 		return (0);
671 	}
672 }
673 
674 static void
tlb_init_sun4u(void)675 tlb_init_sun4u(void)
676 {
677 	phandle_t bsp;
678 
679 	cpu_impl = VER_IMPL(rdpr(ver));
680 	switch (cpu_impl) {
681 	case CPU_IMPL_SPARC64:
682 	case CPU_IMPL_ULTRASPARCI:
683 	case CPU_IMPL_ULTRASPARCII:
684 	case CPU_IMPL_ULTRASPARCIIi:
685 	case CPU_IMPL_ULTRASPARCIIe:
686 		tlb_locked = TLB_DAR_T32;
687 		break;
688 	case CPU_IMPL_ULTRASPARCIII:
689 	case CPU_IMPL_ULTRASPARCIIIp:
690 	case CPU_IMPL_ULTRASPARCIIIi:
691 	case CPU_IMPL_ULTRASPARCIIIip:
692 	case CPU_IMPL_ULTRASPARCIV:
693 	case CPU_IMPL_ULTRASPARCIVp:
694 		tlb_locked = TLB_DAR_T16;
695 		break;
696 	case CPU_IMPL_SPARC64V:
697 		tlb_locked = TLB_DAR_FTLB;
698 		break;
699 	}
700 	bsp = find_bsp_sun4u(OF_child(root), cpu_get_mid_sun4u());
701 	if (bsp == 0)
702 		panic("%s: no node for bootcpu?!?!", __func__);
703 
704 	if (OF_getprop(bsp, "#dtlb-entries", &dtlb_slot_max,
705 	    sizeof(dtlb_slot_max)) == -1 ||
706 	    OF_getprop(bsp, "#itlb-entries", &itlb_slot_max,
707 	    sizeof(itlb_slot_max)) == -1)
708 		panic("%s: can't get TLB slot max.", __func__);
709 
710 	if (cpu_impl == CPU_IMPL_ULTRASPARCIIIp) {
711 #ifdef LOADER_DEBUG
712 		printf("pre fixup:\n");
713 		pmap_print_tlb_sun4u();
714 #endif
715 
716 		/*
717 		 * Relocate the locked entry in it16 slot 0 (if existent)
718 		 * as part of working around Cheetah+ erratum 34.
719 		 */
720 		itlb_relocate_locked0_sun4u();
721 
722 #ifdef LOADER_DEBUG
723 		printf("post fixup:\n");
724 		pmap_print_tlb_sun4u();
725 #endif
726 	}
727 
728 	dtlb_store = malloc(dtlb_slot_max * sizeof(*dtlb_store));
729 	itlb_store = malloc(itlb_slot_max * sizeof(*itlb_store));
730 	if (dtlb_store == NULL || itlb_store == NULL)
731 		panic("%s: can't allocate TLB store", __func__);
732 }
733 
734 #ifdef LOADER_ZFS_SUPPORT
735 
736 static void
sparc64_zfs_probe(void)737 sparc64_zfs_probe(void)
738 {
739 	struct vtoc8 vtoc;
740 	char alias[64], devname[sizeof(alias) + sizeof(":x") - 1];
741 	char type[sizeof("device_type")];
742 	char *bdev, *dev, *odev;
743 	uint64_t guid, *guidp;
744 	int fd, len, part;
745 	phandle_t aliases, options;
746 
747 	guid = 0;
748 
749 	/*
750 	 * Get the GUIDs of the ZFS pools on any additional disks listed in
751 	 * the boot-device environment variable.
752 	 */
753 	if ((aliases = OF_finddevice("/aliases")) == -1)
754 		goto out;
755 	options = OF_finddevice("/options");
756 	len = OF_getproplen(options, "boot-device");
757 	if (len <= 0)
758 		goto out;
759 	bdev = odev = malloc(len + 1);
760 	if (bdev == NULL)
761 		goto out;
762 	if (OF_getprop(options, "boot-device", bdev, len) <= 0)
763 		goto out;
764 	bdev[len] = '\0';
765 	while ((dev = strsep(&bdev, " ")) != NULL) {
766 		if (*dev == '\0')
767 			continue;
768 		strcpy(alias, dev);
769 		(void)OF_getprop(aliases, dev, alias, sizeof(alias));
770 		if (OF_getprop(OF_finddevice(alias), "device_type", type,
771 		    sizeof(type)) == -1)
772 			continue;
773 		if (strcmp(type, "block") != 0)
774 			continue;
775 
776 		/* Find freebsd-zfs slices in the VTOC. */
777 		fd = open(alias, O_RDONLY);
778 		if (fd == -1)
779 			continue;
780 		lseek(fd, 0, SEEK_SET);
781 		if (read(fd, &vtoc, sizeof(vtoc)) != sizeof(vtoc)) {
782 			close(fd);
783 			continue;
784 		}
785 		close(fd);
786 
787 		for (part = 0; part < 8; part++) {
788 			if (part == 2 || vtoc.part[part].tag !=
789 			    VTOC_TAG_FREEBSD_ZFS)
790 				continue;
791 			(void)sprintf(devname, "%s:%c", alias, part + 'a');
792 			/* Get the GUID of the ZFS pool on the boot device. */
793 			if (strcmp(devname, bootpath) == 0)
794 				guidp = &guid;
795 			else
796 				guidp = NULL;
797 			if (zfs_probe_dev(devname, guidp) == ENXIO)
798 				break;
799 		}
800 	}
801 	free(odev);
802 
803  out:
804 	if (guid != 0) {
805 		zfs_currdev.pool_guid = guid;
806 		zfs_currdev.root_guid = 0;
807 		zfs_currdev.dd.d_dev = &zfs_dev;
808 	}
809 }
810 #endif /* LOADER_ZFS_SUPPORT */
811 
812 int
main(int (* openfirm)(void *))813 main(int (*openfirm)(void *))
814 {
815 	char compatible[32];
816 	struct devsw **dp;
817 
818 	/*
819 	 * Tell the Open Firmware functions where they find the OFW gate.
820 	 */
821 	OF_init(openfirm);
822 
823 	archsw.arch_getdev = ofw_getdev;
824 	archsw.arch_copyin = sparc64_copyin;
825 	archsw.arch_copyout = ofw_copyout;
826 	archsw.arch_readin = sparc64_readin;
827 	archsw.arch_autoload = sparc64_autoload;
828 #ifdef LOADER_ZFS_SUPPORT
829 	archsw.arch_zfs_probe = sparc64_zfs_probe;
830 #endif
831 
832 	if (init_heap() == (vm_offset_t)-1)
833 		OF_exit();
834 	setheap((void *)heapva, (void *)(heapva + HEAPSZ));
835 
836 	/*
837 	 * Probe for a console.
838 	 */
839 	cons_probe();
840 
841 	if ((root = OF_peer(0)) == -1)
842 		panic("%s: can't get root phandle", __func__);
843 	OF_getprop(root, "compatible", compatible, sizeof(compatible));
844 	mmu_ops = &mmu_ops_sun4u;
845 
846 	mmu_ops->tlb_init();
847 
848 	/*
849 	 * Set up the current device.
850 	 */
851 	OF_getprop(chosen, "bootpath", bootpath, sizeof(bootpath));
852 
853 	/*
854 	 * Initialize devices.
855 	 */
856 	for (dp = devsw; *dp != NULL; dp++)
857 		if ((*dp)->dv_init != 0)
858 			(*dp)->dv_init();
859 
860 #ifdef LOADER_ZFS_SUPPORT
861 	if (zfs_currdev.pool_guid != 0) {
862 		(void)strncpy(bootpath, zfs_fmtdev(&zfs_currdev),
863 		    sizeof(bootpath) - 1);
864 		bootpath[sizeof(bootpath) - 1] = '\0';
865 	} else
866 #endif
867 
868 	/*
869 	 * Sun compatible bootable CD-ROMs have a disk label placed before
870 	 * the ISO 9660 data, with the actual file system being in the first
871 	 * partition, while the other partitions contain pseudo disk labels
872 	 * with embedded boot blocks for different architectures, which may
873 	 * be followed by UFS file systems.
874 	 * The firmware will set the boot path to the partition it boots from
875 	 * ('f' in the sun4u/sun4v case), but we want the kernel to be loaded
876 	 * from the ISO 9660 file system ('a'), so the boot path needs to be
877 	 * altered.
878 	 */
879 	if (bootpath[strlen(bootpath) - 2] == ':' &&
880 	    bootpath[strlen(bootpath) - 1] == 'f')
881 		bootpath[strlen(bootpath) - 1] = 'a';
882 
883 	env_setenv("currdev", EV_VOLATILE, bootpath,
884 	    ofw_setcurrdev, env_nounset);
885 	env_setenv("loaddev", EV_VOLATILE, bootpath,
886 	    env_noset, env_nounset);
887 
888 	printf("\n%s", bootprog_info);
889 	printf("bootpath=\"%s\"\n", bootpath);
890 
891 	/* Give control to the machine independent loader code. */
892 	interact();
893 	return (1);
894 }
895 
896 COMMAND_SET(heap, "heap", "show heap usage", command_heap);
897 
898 static int
command_heap(int argc,char * argv[])899 command_heap(int argc, char *argv[])
900 {
901 
902 	mallocstats();
903 	printf("heap base at %p, top at %p, upper limit at %p\n", heapva,
904 	    sbrk(0), heapva + HEAPSZ);
905 	return(CMD_OK);
906 }
907 
908 COMMAND_SET(reboot, "reboot", "reboot the system", command_reboot);
909 
910 static int
command_reboot(int argc,char * argv[])911 command_reboot(int argc, char *argv[])
912 {
913 	int i;
914 
915 	for (i = 0; devsw[i] != NULL; ++i)
916 		if (devsw[i]->dv_cleanup != NULL)
917 			(devsw[i]->dv_cleanup)();
918 
919 	printf("Rebooting...\n");
920 	OF_exit();
921 }
922 
923 /* provide this for panic, as it's not in the startup code */
924 void
exit(int code)925 exit(int code)
926 {
927 
928 	OF_exit();
929 }
930 
931 #ifdef LOADER_DEBUG
932 static const char *const page_sizes[] = {
933 	"  8k", " 64k", "512k", "  4m"
934 };
935 
936 static void
pmap_print_tte_sun4u(tte_t tag,tte_t tte)937 pmap_print_tte_sun4u(tte_t tag, tte_t tte)
938 {
939 
940 	printf("%s %s ",
941 	    page_sizes[(tte >> TD_SIZE_SHIFT) & TD_SIZE_MASK],
942 	    tag & TD_G ? "G" : " ");
943 	printf(tte & TD_W ? "W " : "  ");
944 	printf(tte & TD_P ? "\e[33mP\e[0m " : "  ");
945 	printf(tte & TD_E ? "E " : "  ");
946 	printf(tte & TD_CV ? "CV " : "   ");
947 	printf(tte & TD_CP ? "CP " : "   ");
948 	printf(tte & TD_L ? "\e[32mL\e[0m " : "  ");
949 	printf(tte & TD_IE ? "IE " : "   ");
950 	printf(tte & TD_NFO ? "NFO " : "    ");
951 	printf("pa=0x%lx va=0x%lx ctx=%ld\n",
952 	    TD_PA(tte), TLB_TAR_VA(tag), TLB_TAR_CTX(tag));
953 }
954 
955 static void
pmap_print_tlb_sun4u(void)956 pmap_print_tlb_sun4u(void)
957 {
958 	tte_t tag, tte;
959 	u_long pstate;
960 	int i;
961 
962 	pstate = rdpr(pstate);
963 	for (i = 0; i < itlb_slot_max; i++) {
964 		wrpr(pstate, pstate & ~PSTATE_IE, 0);
965 		tte = itlb_get_data_sun4u(tlb_locked, i);
966 		wrpr(pstate, pstate, 0);
967 		if (!(tte & TD_V))
968 			continue;
969 		tag = ldxa(TLB_DAR_SLOT(tlb_locked, i),
970 		    ASI_ITLB_TAG_READ_REG);
971 		printf("iTLB-%2u: ", i);
972 		pmap_print_tte_sun4u(tag, tte);
973 	}
974 	for (i = 0; i < dtlb_slot_max; i++) {
975 		wrpr(pstate, pstate & ~PSTATE_IE, 0);
976 		tte = dtlb_get_data_sun4u(tlb_locked, i);
977 		wrpr(pstate, pstate, 0);
978 		if (!(tte & TD_V))
979 			continue;
980 		tag = ldxa(TLB_DAR_SLOT(tlb_locked, i),
981 		    ASI_DTLB_TAG_READ_REG);
982 		printf("dTLB-%2u: ", i);
983 		pmap_print_tte_sun4u(tag, tte);
984 	}
985 }
986 #endif
987