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