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