1 /* $Id: head.S,v 1.87 2002/02/09 19:49:31 davem Exp $
2 * head.S: Initial boot code for the Sparc64 port of Linux.
4 * Copyright (C) 1996,1997 David S. Miller (davem@caip.rutgers.edu)
5 * Copyright (C) 1996 David Sitsky (David.Sitsky@anu.edu.au)
6 * Copyright (C) 1997,1998 Jakub Jelinek (jj@sunsite.mff.cuni.cz)
7 * Copyright (C) 1997 Miguel de Icaza (miguel@nuclecu.unam.mx)
10 #include <linux/config.h>
11 #include <linux/version.h>
12 #include <linux/errno.h>
13 #include <asm/thread_info.h>
15 #include <asm/pstate.h>
16 #include <asm/ptrace.h>
17 #include <asm/spitfire.h>
19 #include <asm/pgtable.h>
20 #include <asm/errno.h>
21 #include <asm/signal.h>
22 #include <asm/processor.h>
27 #include <asm/ttable.h>
30 /* This section from from _start to sparc64_boot_end should fit into
31 * 0x0000.0000.0040.4000 to 0x0000.0000.0040.8000 and will be sharing space
32 * with bootup_user_stack, which is from 0x0000.0000.0040.4000 to
33 * 0x0000.0000.0040.6000 and empty_bad_page, which is from
34 * 0x0000.0000.0040.6000 to 0x0000.0000.0040.8000.
38 .globl start, _start, stext, _stext
46 flushw /* Flush register file. */
48 /* This stuff has to be in sync with SILO and other potential boot loaders
49 * Fields should be kept upward compatible and whenever any change is made,
50 * HdrS version should be incremented.
52 .global root_flags, ram_flags, root_dev
53 .global sparc_ramdisk_image, sparc_ramdisk_size
54 .global sparc_ramdisk_image64
57 .word LINUX_VERSION_CODE
61 * 0x0300 : Supports being located at other than 0x4000
62 * 0x0202 : Supports kernel params string
63 * 0x0201 : Supports reboot_command
65 .half 0x0301 /* HdrS version */
79 sparc_ramdisk_image64:
83 /* We must be careful, 32-bit OpenBOOT will get confused if it
84 * tries to save away a register window to a 64-bit kernel
85 * stack address. Flush all windows, disable interrupts,
86 * remap if necessary, jump onto kernel trap table, then kernel
87 * stack, or else we die.
89 * PROM entry point is on %o4
92 BRANCH_IF_CHEETAH_BASE(g1,g7,cheetah_boot)
93 BRANCH_IF_CHEETAH_PLUS_OR_FOLLOWON(g1,g7,cheetah_plus_boot)
94 ba,pt %xcc, spitfire_boot
98 /* Preserve OBP chosen DCU and DCR register settings. */
99 ba,pt %xcc, cheetah_generic_boot
103 mov DCR_BPE | DCR_RPE | DCR_SI | DCR_IFPOE | DCR_MS, %g1
106 sethi %uhi(DCU_ME|DCU_RE|DCU_HPE|DCU_SPE|DCU_SL|DCU_WE), %g7
107 or %g7, %ulo(DCU_ME|DCU_RE|DCU_HPE|DCU_SPE|DCU_SL|DCU_WE), %g7
109 or %g7, DCU_DM | DCU_IM | DCU_DC | DCU_IC, %g7
110 stxa %g7, [%g0] ASI_DCU_CONTROL_REG
113 cheetah_generic_boot:
114 mov TSB_EXTENSION_P, %g3
115 stxa %g0, [%g3] ASI_DMMU
116 stxa %g0, [%g3] ASI_IMMU
119 mov TSB_EXTENSION_S, %g3
120 stxa %g0, [%g3] ASI_DMMU
123 mov TSB_EXTENSION_N, %g3
124 stxa %g0, [%g3] ASI_DMMU
125 stxa %g0, [%g3] ASI_IMMU
128 wrpr %g0, (PSTATE_PRIV|PSTATE_PEF|PSTATE_IE), %pstate
131 /* Just like for Spitfire, we probe itlb-2 for a mapping which
132 * matches our current %pc. We take the physical address in
133 * that mapping and use it to make our own.
136 /* %g5 holds the tlb data */
137 sethi %uhi(_PAGE_VALID | _PAGE_SZ4MB), %g5
139 or %g5, (_PAGE_CP | _PAGE_CV | _PAGE_P | _PAGE_L | _PAGE_W | _PAGE_G), %g5
141 /* Put PADDR tlb data mask into %g3. */
142 sethi %uhi(_PAGE_PADDR), %g3
143 or %g3, %ulo(_PAGE_PADDR), %g3
145 sethi %hi(_PAGE_PADDR), %g7
146 or %g7, %lo(_PAGE_PADDR), %g7
149 set 2 << 16, %l0 /* TLB entry walker. */
150 set 0x1fff, %l2 /* Page mask. */
152 andn %l3, %l2, %g2 /* vaddr comparator */
154 1: ldxa [%l0] ASI_ITLB_TAG_READ, %g1
158 be,pn %xcc, cheetah_got_tlbentry
160 and %l0, (127 << 3), %g1
163 add %l0, (1 << 3), %l0
165 /* Search the small TLB. OBP never maps us like that but
170 1: ldxa [%l0] ASI_ITLB_TAG_READ, %g1
174 be,pn %xcc, cheetah_got_tlbentry
178 add %l0, (1 << 3), %l0
180 /* BUG() if we get here... */
183 cheetah_got_tlbentry:
184 ldxa [%l0] ASI_ITLB_DATA_ACCESS, %g0
185 ldxa [%l0] ASI_ITLB_DATA_ACCESS, %g1
192 /* Clear out any KERNBASE area entries. */
194 sethi %hi(KERNBASE), %g3
195 sethi %hi(KERNBASE<<1), %g7
196 mov TLB_TAG_ACCESS, %l7
198 /* First, check ITLB */
199 1: ldxa [%l0] ASI_ITLB_TAG_READ, %g1
207 stxa %g0, [%l7] ASI_IMMU
209 stxa %g0, [%l0] ASI_ITLB_DATA_ACCESS
212 2: and %l0, (127 << 3), %g1
215 add %l0, (1 << 3), %l0
217 /* Next, check DTLB */
219 1: ldxa [%l0] ASI_DTLB_TAG_READ, %g1
227 stxa %g0, [%l7] ASI_DMMU
229 stxa %g0, [%l0] ASI_DTLB_DATA_ACCESS
232 2: and %l0, (511 << 3), %g1
235 add %l0, (1 << 3), %l0
237 /* On Cheetah+, have to check second DTLB. */
238 BRANCH_IF_CHEETAH_PLUS_OR_FOLLOWON(g1,l0,2f)
243 1: ldxa [%l0] ASI_DTLB_TAG_READ, %g1
251 stxa %g0, [%l7] ASI_DMMU
253 stxa %g0, [%l0] ASI_DTLB_DATA_ACCESS
256 2: and %l0, (511 << 3), %g1
259 add %l0, (1 << 3), %l0
263 /* Now lock the TTE we created into ITLB-0 and DTLB-0,
264 * entry 15 (and maybe 14 too).
266 sethi %hi(KERNBASE), %g3
267 set (0 << 16) | (15 << 3), %g7
268 stxa %g3, [%l7] ASI_DMMU
270 stxa %g5, [%g7] ASI_DTLB_DATA_ACCESS
272 stxa %g3, [%l7] ASI_IMMU
274 stxa %g5, [%g7] ASI_ITLB_DATA_ACCESS
278 sethi %hi(_end), %g3 /* Check for bigkernel case */
279 or %g3, %lo(_end), %g3
280 srl %g3, 23, %g3 /* Check if _end > 8M */
282 sethi %hi(KERNBASE), %g3 /* Restore for fixup code below */
283 sethi %hi(0x400000), %g3
284 or %g3, %lo(0x400000), %g3
285 add %g5, %g3, %g5 /* New tte data */
286 andn %g5, (_PAGE_G), %g5
287 sethi %hi(KERNBASE+0x400000), %g3
288 or %g3, %lo(KERNBASE+0x400000), %g3
289 set (0 << 16) | (14 << 3), %g7
290 stxa %g3, [%l7] ASI_DMMU
292 stxa %g5, [%g7] ASI_DTLB_DATA_ACCESS
294 stxa %g3, [%l7] ASI_IMMU
296 stxa %g5, [%g7] ASI_ITLB_DATA_ACCESS
300 sethi %hi(KERNBASE), %g3 /* Restore for fixup code below */
304 1: set sun4u_init, %g2
309 /* Typically PROM has already enabled both MMU's and both on-chip
310 * caches, but we do it here anyway just to be paranoid.
312 mov (LSU_CONTROL_IC|LSU_CONTROL_DC|LSU_CONTROL_IM|LSU_CONTROL_DM), %g1
313 stxa %g1, [%g0] ASI_LSU_CONTROL
317 * Make sure we are in privileged mode, have address masking,
318 * using the ordinary globals and have enabled floating
321 * Again, typically PROM has left %pil at 13 or similar, and
322 * (PSTATE_PRIV | PSTATE_PEF | PSTATE_IE) in %pstate.
324 wrpr %g0, (PSTATE_PRIV|PSTATE_PEF|PSTATE_IE), %pstate
327 spitfire_create_mappings:
328 /* %g5 holds the tlb data */
329 sethi %uhi(_PAGE_VALID | _PAGE_SZ4MB), %g5
331 or %g5, (_PAGE_CP | _PAGE_CV | _PAGE_P | _PAGE_L | _PAGE_W | _PAGE_G), %g5
333 /* Base of physical memory cannot reliably be assumed to be
334 * at 0x0! Figure out where it happens to be. -DaveM
337 /* Put PADDR tlb data mask into %g3. */
338 sethi %uhi(_PAGE_PADDR_SF), %g3
339 or %g3, %ulo(_PAGE_PADDR_SF), %g3
341 sethi %hi(_PAGE_PADDR_SF), %g7
342 or %g7, %lo(_PAGE_PADDR_SF), %g7
345 /* Walk through entire ITLB, looking for entry which maps
346 * our %pc currently, stick PADDR from there into %g5 tlb data.
348 clr %l0 /* TLB entry walker. */
349 set 0x1fff, %l2 /* Page mask. */
351 andn %l3, %l2, %g2 /* vaddr comparator */
353 /* Yes, the nops seem to be necessary for now, don't ask me why. -DaveM */
354 ldxa [%l0] ASI_ITLB_TAG_READ, %g1
358 andn %g1, %l2, %g1 /* Get vaddr */
360 be,a,pn %xcc, spitfire_got_tlbentry
361 ldxa [%l0] ASI_ITLB_DATA_ACCESS, %g1
364 add %l0, (1 << 3), %l0
366 /* BUG() if we get here... */
369 spitfire_got_tlbentry:
370 /* Nops here again, perhaps Cheetah/Blackbird are better behaved... */
374 and %g1, %g3, %g1 /* Mask to just get paddr bits. */
375 set 0x5fff, %l3 /* Mask offset to get phys base. */
378 /* NOTE: We hold on to %g1 paddr base as we need it below to lock
379 * NOTE: the PROM cif code into the TLB.
382 or %g5, %g1, %g5 /* Or it into TAG being built. */
384 clr %l0 /* TLB entry walker. */
385 sethi %hi(KERNBASE), %g3 /* 4M lower limit */
386 sethi %hi(KERNBASE<<1), %g7 /* 8M upper limit */
387 mov TLB_TAG_ACCESS, %l7
389 /* Yes, the nops seem to be necessary for now, don't ask me why. -DaveM */
390 ldxa [%l0] ASI_ITLB_TAG_READ, %g1
394 andn %g1, %l2, %g1 /* Get vaddr */
400 stxa %g0, [%l7] ASI_IMMU
401 stxa %g0, [%l0] ASI_ITLB_DATA_ACCESS
406 add %l0, (1 << 3), %l0
410 clr %l0 /* TLB entry walker. */
412 /* Yes, the nops seem to be necessary for now, don't ask me why. -DaveM */
413 ldxa [%l0] ASI_DTLB_TAG_READ, %g1
417 andn %g1, %l2, %g1 /* Get vaddr */
423 stxa %g0, [%l7] ASI_DMMU
424 stxa %g0, [%l0] ASI_DTLB_DATA_ACCESS
429 add %l0, (1 << 3), %l0
434 /* PROM never puts any TLB entries into the MMU with the lock bit
435 * set. So we gladly use tlb entry 63 for KERNBASE. And maybe 62 too.
438 sethi %hi(KERNBASE), %g3
440 stxa %g3, [%l7] ASI_DMMU /* KERNBASE into TLB TAG */
441 stxa %g5, [%g7] ASI_DTLB_DATA_ACCESS /* TTE into TLB DATA */
443 stxa %g3, [%l7] ASI_IMMU /* KERNBASE into TLB TAG */
444 stxa %g5, [%g7] ASI_ITLB_DATA_ACCESS /* TTE into TLB DATA */
448 sethi %hi(_end), %g3 /* Check for bigkernel case */
449 or %g3, %lo(_end), %g3
450 srl %g3, 23, %g3 /* Check if _end > 8M */
452 sethi %hi(KERNBASE), %g3 /* Restore for fixup code below */
453 sethi %hi(0x400000), %g3
454 or %g3, %lo(0x400000), %g3
455 add %g5, %g3, %g5 /* New tte data */
456 andn %g5, (_PAGE_G), %g5
457 sethi %hi(KERNBASE+0x400000), %g3
458 or %g3, %lo(KERNBASE+0x400000), %g3
460 stxa %g3, [%l7] ASI_DMMU
461 stxa %g5, [%g7] ASI_DTLB_DATA_ACCESS
463 stxa %g3, [%l7] ASI_IMMU
464 stxa %g5, [%g7] ASI_ITLB_DATA_ACCESS
468 sethi %hi(KERNBASE), %g3 /* Restore for fixup code below */
478 mov PRIMARY_CONTEXT, %g7
479 stxa %g0, [%g7] ASI_DMMU
482 mov SECONDARY_CONTEXT, %g7
483 stxa %g0, [%g7] ASI_DMMU
486 /* We are now safely (we hope) in Nucleus context (0), rewrite
487 * the KERNBASE TTE's so they no longer have the global bit set.
488 * Don't forget to setup TAG_ACCESS first 8-)
490 mov TLB_TAG_ACCESS, %g2
491 stxa %g3, [%g2] ASI_IMMU
492 stxa %g3, [%g2] ASI_DMMU
495 BRANCH_IF_ANY_CHEETAH(g1,g7,cheetah_tlb_fixup)
497 ba,pt %xcc, spitfire_tlb_fixup
501 set (0 << 16) | (15 << 3), %g7
502 ldxa [%g7] ASI_ITLB_DATA_ACCESS, %g0
503 ldxa [%g7] ASI_ITLB_DATA_ACCESS, %g1
504 andn %g1, (_PAGE_G), %g1
505 stxa %g1, [%g7] ASI_ITLB_DATA_ACCESS
508 ldxa [%g7] ASI_DTLB_DATA_ACCESS, %g0
509 ldxa [%g7] ASI_DTLB_DATA_ACCESS, %g1
510 andn %g1, (_PAGE_G), %g1
511 stxa %g1, [%g7] ASI_DTLB_DATA_ACCESS
514 /* Kill instruction prefetch queues. */
518 mov 2, %g2 /* Set TLB type to cheetah+. */
519 BRANCH_IF_CHEETAH_PLUS_OR_FOLLOWON(g1,g7,1f)
521 mov 1, %g2 /* Set TLB type to cheetah. */
523 1: sethi %hi(tlb_type), %g1
524 stw %g2, [%g1 + %lo(tlb_type)]
526 BRANCH_IF_CHEETAH_PLUS_OR_FOLLOWON(g1,g7,1f)
530 1: /* Patch context register writes to support nucleus page
533 call cheetah_plus_patch_etrap
535 call cheetah_plus_patch_rtrap
537 call cheetah_plus_patch_fpdis
539 call cheetah_plus_patch_winfixup
542 2: /* Patch copy/page operations to cheetah optimized versions. */
543 call cheetah_patch_copyops
545 call cheetah_patch_copy_page
547 call cheetah_patch_cachetlbops
550 ba,pt %xcc, tlb_fixup_done
555 ldxa [%g7] ASI_ITLB_DATA_ACCESS, %g1
556 andn %g1, (_PAGE_G), %g1
557 stxa %g1, [%g7] ASI_ITLB_DATA_ACCESS
560 ldxa [%g7] ASI_DTLB_DATA_ACCESS, %g1
561 andn %g1, (_PAGE_G), %g1
562 stxa %g1, [%g7] ASI_DTLB_DATA_ACCESS
565 /* Kill instruction prefetch queues. */
569 /* Set TLB type to spitfire. */
571 sethi %hi(tlb_type), %g1
572 stw %g2, [%g1 + %lo(tlb_type)]
575 sethi %hi(init_thread_union), %g6
576 or %g6, %lo(init_thread_union), %g6
577 ldx [%g6 + TI_TASK], %g4
581 #if 0 /* We don't do it like this anymore, but for historical hack value
582 * I leave this snippet here to show how crazy we can be sometimes. 8-)
585 /* Setup "Linux Current Register", thanks Sun 8-) */
588 /* Blackbird errata workaround. See commentary in
589 * smp.c:smp_percpu_timer_interrupt() for more
595 99: wr %g6, %g0, %pic
601 sllx %g1, THREAD_SHIFT, %g1
602 sub %g1, (STACKFRAME_SZ + STACK_BIAS), %g1
606 /* Set per-cpu pointer initially to zero, this makes
607 * the boot-cpu use the in-kernel-image per-cpu areas
608 * before setup_per_cpu_area() is invoked.
616 sethi %hi(__bss_start), %o0
617 or %o0, %lo(__bss_start), %o0
619 or %o1, %lo(_end), %o1
623 mov %l6, %o1 ! OpenPROM stack
625 mov %l7, %o0 ! OpenPROM cif handler
632 /* IMPORTANT NOTE: Whenever making changes here, check
633 * trampoline.S as well. -jj */
635 setup_tba: /* i0 = is_starfire */
639 sethi %hi(prom_tba), %o1
640 or %o1, %lo(prom_tba), %o1
643 /* Setup "Linux" globals 8-) */
646 wrpr %o1, (PSTATE_AG|PSTATE_IE), %pstate
647 sethi %hi(sparc64_ttable_tl0), %g1
651 /* Set up MMU globals */
652 wrpr %o1, (PSTATE_MG|PSTATE_IE), %pstate
654 /* Set fixed globals used by dTLB miss handler. */
655 #define KERN_HIGHBITS ((_PAGE_VALID|_PAGE_SZ4MB)^0xfffff80000000000)
656 #define KERN_LOWBITS (_PAGE_CP | _PAGE_CV | _PAGE_P | _PAGE_W)
659 stxa %g0, [%g1] ASI_DMMU
661 stxa %g0, [%g1] ASI_IMMU
664 sethi %uhi(KERN_HIGHBITS), %g2
665 or %g2, %ulo(KERN_HIGHBITS), %g2
667 or %g2, KERN_LOWBITS, %g2
669 BRANCH_IF_ANY_CHEETAH(g3,g7,cheetah_vpte_base)
670 ba,pt %xcc, spitfire_vpte_base
674 sethi %uhi(VPTE_BASE_CHEETAH), %g3
675 or %g3, %ulo(VPTE_BASE_CHEETAH), %g3
680 sethi %uhi(VPTE_BASE_SPITFIRE), %g3
681 or %g3, %ulo(VPTE_BASE_SPITFIRE), %g3
689 /* Kill PROM timer */
690 sethi %hi(0x80000000), %o2
692 wr %o2, 0, %tick_cmpr
694 BRANCH_IF_ANY_CHEETAH(o2,o3,1f)
699 /* Disable STICK_INT interrupts. */
701 sethi %hi(0x80000000), %o2
705 /* Ok, we're done setting up all the state our trap mechanims needs,
706 * now get back into normal globals and let the PROM know what is up.
709 wrpr %g0, %g0, %wstate
710 wrpr %o1, PSTATE_IE, %pstate
712 call init_irqwork_curcpu
715 call prom_set_trap_table
716 sethi %hi(sparc64_ttable_tl0), %o0
718 BRANCH_IF_CHEETAH_PLUS_OR_FOLLOWON(g2,g3,1f)
722 1: /* Start using proper page size encodings in ctx register. */
723 sethi %uhi(CTX_CHEETAH_PLUS_NUC), %g3
724 mov PRIMARY_CONTEXT, %g1
726 sethi %hi(CTX_CHEETAH_PLUS_CTX0), %g2
728 stxa %g3, [%g1] ASI_DMMU
733 or %o1, PSTATE_IE, %o1
740 * The following skips make sure the trap table in ttable.S is aligned
741 * on a 32K boundary as required by the v9 specs for TBA register.
744 .skip 0x2000 + _start - sparc64_boot_end
745 bootup_user_stack_end:
749 /* This is just a hack to fool make depend config.h discovering
750 strategy: As the .S files below need config.h, but
751 make depend does not find it for them, we include config.h
761 .globl swapper_pg_dir
767 #include "winfixup.S"
770 /* This is just anal retentiveness on my part... */
775 .globl prom_tba, tlb_type
777 tlb_type: .word 0 /* Must NOT end up in BSS */
778 .section ".fixup",#alloc,#execinstr
782 restore %g0, -EFAULT, %o0