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