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