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