1 /* SPDX-License-Identifier: GPL-2.0 */
3 * head.S: The initial boot code for the Sparc port of Linux.
5 * Copyright (C) 1995 David S. Miller (davem@caip.rutgers.edu)
6 * Copyright (C) 1995,1999 Pete Zaitcev (zaitcev@yahoo.com)
7 * Copyright (C) 1996 Miguel de Icaza (miguel@nuclecu.unam.mx)
8 * Copyright (C) 1997 Jakub Jelinek (jj@sunsite.mff.cuni.cz)
9 * Copyright (C) 1997 Michael A. Griffith (grif@acm.org)
11 * CompactPCI platform by Eric Brower, 1999.
14 #include <linux/version.h>
15 #include <linux/init.h>
19 #include <asm/contregs.h>
20 #include <asm/ptrace.h>
23 #include <asm/kdebug.h>
24 #include <asm/winmacro.h>
25 #include <asm/thread_info.h> /* TI_UWINMASK */
26 #include <asm/errno.h>
27 #include <asm/pgtable.h> /* PGDIR_SHIFT */
28 #include <asm/export.h>
31 /* The following are used with the prom_vector node-ops to figure out
40 /* Tested on SS-5, SS-10 */
48 .asciz "Sparc-Linux sun4/sun4c or MMU-less not supported\n\n"
52 .asciz "Sparc-Linux sun4e support does not exist\n\n"
55 /* The trap-table - located in the __HEAD section */
56 #include "ttable_32.S"
60 /* This was the only reasonable way I could think of to properly align
61 * these page-table data structures.
63 .globl empty_zero_page
64 empty_zero_page: .skip PAGE_SIZE
65 EXPORT_SYMBOL(empty_zero_page)
70 .global sparc_ramdisk_image
71 .global sparc_ramdisk_size
73 /* This stuff has to be in sync with SILO and other potential boot loaders
74 * Fields should be kept upward compatible and whenever any change is made,
75 * HdrS version should be incremented.
78 .word LINUX_VERSION_CODE
79 .half 0x0203 /* HdrS version */
94 /* Cool, here we go. Pick up the romvec pointer in %o0 and stash it in
95 * %g7 and at prom_vector_p. And also quickly check whether we are on
96 * a v0, v2, or v3 prom.
99 /* Ok, it's nice to know, as early as possible, if we
100 * are already mapped where we expect to be in virtual
101 * memory. The Solaris /boot elf format bootloader
102 * will peek into our elf header and load us where
103 * we want to be, otherwise we have to re-map.
105 * Some boot loaders don't place the jmp'rs address
106 * in %o7, so we do a pc-relative call to a local
107 * label, then see what %o7 has.
110 mov %o7, %g4 ! Save %o7
112 /* Jump to it, and pray... */
122 mov %g4, %o7 /* Previous %o7. */
124 mov %o0, %l0 ! stash away romvec
125 mov %o0, %g7 ! put it here too
126 mov %o1, %l1 ! stash away debug_vec too
128 /* Ok, let's check out our run time program counter. */
134 /* %l6 will hold the offset we have to subtract
135 * from absolute symbols in order to access areas
136 * in our own image. If already mapped this is
137 * just plain zero, else it is KERNBASE.
146 /* Copy over the Prom's level 14 clock handler. */
150 * preserve our linked/calculated instructions
154 sub %g1, %l6, %g1 ! translate to physical
155 sub %g3, %l6, %g3 ! translate to physical
162 andn %g1, 0xfff, %g1 ! proms trap table base
163 or %g0, (0x1e<<4), %g2 ! offset to lvl14 intr
170 std %g4, [%g3 + 0x8] ! Copy proms handler
172 /* DON'T TOUCH %l0 thru %l5 in these remapping routines,
173 * we need their values afterwards!
176 /* Now check whether we are already mapped, if we
177 * are we can skip all this garbage coming up.
181 be go_to_highmem ! this will be a nop then
184 /* Validate that we are in fact running on an
202 /* It looks like this is a machine we support.
203 * Now find out what MMU we are dealing with
204 * LEON - identified by the psr.impl field
205 * Viking - identified by the psr.impl field
206 * In all other cases a sun4m srmmu.
207 * We check that the MMU is enabled in all cases.
210 /* Check if this is a LEON CPU */
212 srl %g3, PSR_IMPL_SHIFT, %g3
213 and %g3, PSR_IMPL_SHIFTED_MASK, %g3
214 cmp %g3, PSR_IMPL_LEON
215 be leon_remap /* It is a LEON - jump */
218 /* Sanity-check, is MMU enabled */
219 lda [%g0] ASI_M_MMUREGS, %g1
224 /* Check for a viking (TI) module. */
229 /* Figure out what kind of viking we are on.
230 * We need to know if we have to play with the
231 * AC bit and disable traps or not.
234 /* I've only seen MicroSparc's on SparcClassics with this
238 lda [%g0] ASI_M_MMUREGS, %g3 ! peek in the control reg
241 bnz srmmu_not_viking ! is in mbus mode
244 rd %psr, %g3 ! DO NOT TOUCH %g3
245 andn %g3, PSR_ET, %g2
249 /* Get context table pointer, then convert to
250 * a physical address, which is 36 bits.
253 lda [%g4] ASI_M_MMUREGS, %g4
254 sll %g4, 0x4, %g4 ! We use this below
257 /* Set the AC bit in the Viking's MMU control reg. */
258 lda [%g0] ASI_M_MMUREGS, %g5 ! DO NOT TOUCH %g5
259 set 0x8000, %g6 ! AC bit mask
260 or %g5, %g6, %g6 ! Or it in...
261 sta %g6, [%g0] ASI_M_MMUREGS ! Close your eyes...
263 /* Grrr, why does it seem like every other load/store
264 * on the sun4m is in some ASI space...
265 * Fine with me, let's get the pointer to the level 1
266 * page table directory and fetch its entry.
268 lda [%g4] ASI_M_BYPASS, %o1 ! This is a level 1 ptr
269 srl %o1, 0x4, %o1 ! Clear low 4 bits
270 sll %o1, 0x8, %o1 ! Make physical
272 /* Ok, pull in the PTD. */
273 lda [%o1] ASI_M_BYPASS, %o2 ! This is the 0x0 16MB pgd
275 /* Calculate to KERNBASE entry. */
276 add %o1, KERNBASE >> (PGDIR_SHIFT - 2), %o3
278 /* Poke the entry into the calculated address. */
279 sta %o2, [%o3] ASI_M_BYPASS
281 /* I don't get it Sun, if you engineered all these
282 * boot loaders and the PROM (thank you for the debugging
283 * features btw) why did you not have them load kernel
284 * images up in high address space, since this is necessary
285 * for ABI compliance anyways? Does this low-mapping provide
286 * enhanced interoperability?
288 * "The PROM is the computer."
291 /* Ok, restore the MMU control register we saved in %g5 */
292 sta %g5, [%g0] ASI_M_MMUREGS ! POW... ouch
294 /* Turn traps back on. We saved it in %g3 earlier. */
295 wr %g3, 0x0, %psr ! tick tock, tick tock
297 /* Now we burn precious CPU cycles due to bad engineering. */
300 /* Wow, all that just to move a 32-bit value from one
301 * place to another... Jump to high memory.
307 /* This works on viking's in Mbus mode and all
308 * other MBUS modules. It is virtually the same as
309 * the above madness sans turning traps off and flipping
313 lda [%g1] ASI_M_MMUREGS, %g1 ! get ctx table ptr
314 sll %g1, 0x4, %g1 ! make physical addr
315 lda [%g1] ASI_M_BYPASS, %g1 ! ptr to level 1 pg_table
317 sll %g1, 0x8, %g1 ! make phys addr for l1 tbl
319 lda [%g1] ASI_M_BYPASS, %g2 ! get level1 entry for 0x0
320 add %g1, KERNBASE >> (PGDIR_SHIFT - 2), %g3
321 sta %g2, [%g3] ASI_M_BYPASS ! place at KERNBASE entry
327 /* Sanity-check, is MMU enabled */
328 lda [%g0] ASI_LEON_MMUREGS, %g1
333 /* Same code as in the srmmu_not_viking case,
334 * with the LEON ASI for mmuregs
337 lda [%g1] ASI_LEON_MMUREGS, %g1 ! get ctx table ptr
338 sll %g1, 0x4, %g1 ! make physical addr
339 lda [%g1] ASI_M_BYPASS, %g1 ! ptr to level 1 pg_table
341 sll %g1, 0x8, %g1 ! make phys addr for l1 tbl
343 lda [%g1] ASI_M_BYPASS, %g2 ! get level1 entry for 0x0
344 add %g1, KERNBASE >> (PGDIR_SHIFT - 2), %g3
345 sta %g2, [%g3] ASI_M_BYPASS ! place at KERNBASE entry
349 /* Now do a non-relative jump so that PC is in high-memory */
351 set execute_in_high_mem, %g1
355 /* The code above should be at beginning and we have to take care about
356 * short jumps, as branching to .init.text section from .text is usually
359 /* Acquire boot time privileged register values, this will help debugging.
360 * I figure out and store nwindows and nwindowsm1 later on.
363 mov %l0, %o0 ! put back romvec
364 mov %l1, %o1 ! and debug_vec
366 sethi %hi(prom_vector_p), %g1
367 st %o0, [%g1 + %lo(prom_vector_p)]
369 sethi %hi(linux_dbvec), %g1
370 st %o1, [%g1 + %lo(linux_dbvec)]
372 /* Get the machine type via the romvec
373 * getprops node operation
379 or %g0, %g0, %o0 ! next_node(0) = first_node
382 sethi %hi(cputypvar), %o1 ! First node has cpu-arch
383 or %o1, %lo(cputypvar), %o1
384 sethi %hi(cputypval), %o2 ! information, the string
385 or %o2, %lo(cputypval), %o2
386 ld [%l1], %l0 ! 'compatible' tells
387 ld [%l0 + 0xc], %l0 ! that we want 'sun4x' where
388 call %l0 ! x is one of 'm', 'd' or 'e'.
389 nop ! %o2 holds pointer
390 ! to a buf where above string
391 ! will get stored by the prom.
394 /* Check value of "compatible" property.
400 * sun4e => "no_sun4e_here"
401 * '*' => "no_sun4u_here"
402 * Check single letters only
406 /* If cputypval[0] == 'l' (lower case letter L) this is leon */
412 /* Check cputypval[4] to find the sun model */
413 ldub [%o2 + 0x4], %l1
422 be no_sun4e_here ! Could be a sun4e.
424 b no_sun4u_here ! AIEEE, a V9 sun4u... Get our BIG BROTHER kernel :))
428 /* LEON CPU - set boot_cpu_id */
429 sethi %hi(boot_cpu_id), %g2 ! boot-cpu index
432 ldub [%g2 + %lo(boot_cpu_id)], %g1
433 cmp %g1, 0xff ! unset means first CPU
434 bne leon_smp_cpu_startup ! continue only with master
437 /* Get CPU-ID from most significant 4-bit of ASR17 */
441 /* Update boot_cpu_id only on boot cpu */
442 stub %g1, [%g2 + %lo(boot_cpu_id)]
447 /* CPUID in bootbus can be found at PA 0xff0140000 */
448 #define SUN4D_BOOTBUS_CPUID 0xf0140000
451 /* Need to patch call to handler_irq */
452 set patch_handler_irq, %g4
453 set sun4d_handler_irq, %g5
454 sethi %hi(0x40000000), %g3 ! call
461 /* Get our CPU id out of bootbus */
462 set SUN4D_BOOTBUS_CPUID, %g3
463 lduba [%g3] ASI_M_CTL, %g3
466 sta %g4, [%g0] ASI_M_VIKING_TMP1
467 sethi %hi(boot_cpu_id), %g5
468 stb %g4, [%g5 + %lo(boot_cpu_id)]
471 /* Fall through to sun4m_init */
474 /* Ok, the PROM could have done funny things and apple cider could still
475 * be sitting in the fault status/address registers. Read them all to
476 * clear them so we don't get magic faults later on.
478 /* This sucks, apparently this makes Vikings call prom panic, will fix later */
481 srl %o1, PSR_IMPL_SHIFT, %o1 ! Get a type of the CPU
483 subcc %o1, PSR_IMPL_TI, %g0 ! TI: Viking or MicroSPARC
488 lda [%o0] ASI_M_MMUREGS, %g0
490 lda [%o0] ASI_M_MMUREGS, %g0
492 /* Fujitsu MicroSPARC-II has no asynchronous flavors of FARs */
498 lda [%o0] ASI_M_MMUREGS, %g0
500 lda [%o0] ASI_M_MMUREGS, %g0
506 /* Aieee, now set PC and nPC, enable traps, give ourselves a stack and it's
509 /* Turn on Supervisor, EnableFloating, and all the PIL bits.
510 * Also puts us in register window zero with traps off.
512 set (PSR_PS | PSR_S | PSR_PIL | PSR_EF), %g2
516 /* I want a kernel stack NOW! */
517 set init_thread_union, %g1
518 set (THREAD_SIZE - STACKFRAME_SZ), %g2
520 mov 0, %fp /* And for good luck */
522 /* Zero out our BSS section. */
523 set __bss_start , %o0 ! First address of BSS
524 set _end , %o1 ! Last address of BSS
532 /* If boot_cpu_id has not been setup by machine specific
533 * init-code above we default it to zero.
535 sethi %hi(boot_cpu_id), %g2
536 ldub [%g2 + %lo(boot_cpu_id)], %g3
541 stub %g3, [%g2 + %lo(boot_cpu_id)]
545 /* Initialize the uwinmask value for init task just in case.
546 * But first make current_set[boot_cpu_id] point to something useful.
548 set init_thread_union, %g6
556 st %g0, [%g6 + TI_UWINMASK]
558 /* Compute NWINDOWS and stash it away. Now uses %wim trick explained
559 * in the V8 manual. Ok, this method seems to work, Sparc is cool...
560 * No, it doesn't work, have to play the save/readCWP/restore trick.
563 wr %g0, 0x0, %wim ! so we do not get a trap
576 wr %g1, 0x0, %wim ! make window 1 invalid
583 /* Adjust our window handling routines to
584 * do things correctly on 7 window Sparcs.
587 #define PATCH_INSN(src, dest) \
593 /* Patch for window spills... */
594 PATCH_INSN(spnwin_patch1_7win, spnwin_patch1)
595 PATCH_INSN(spnwin_patch2_7win, spnwin_patch2)
596 PATCH_INSN(spnwin_patch3_7win, spnwin_patch3)
598 /* Patch for window fills... */
599 PATCH_INSN(fnwin_patch1_7win, fnwin_patch1)
600 PATCH_INSN(fnwin_patch2_7win, fnwin_patch2)
602 /* Patch for trap entry setup... */
603 PATCH_INSN(tsetup_7win_patch1, tsetup_patch1)
604 PATCH_INSN(tsetup_7win_patch2, tsetup_patch2)
605 PATCH_INSN(tsetup_7win_patch3, tsetup_patch3)
606 PATCH_INSN(tsetup_7win_patch4, tsetup_patch4)
607 PATCH_INSN(tsetup_7win_patch5, tsetup_patch5)
608 PATCH_INSN(tsetup_7win_patch6, tsetup_patch6)
610 /* Patch for returning from traps... */
611 PATCH_INSN(rtrap_7win_patch1, rtrap_patch1)
612 PATCH_INSN(rtrap_7win_patch2, rtrap_patch2)
613 PATCH_INSN(rtrap_7win_patch3, rtrap_patch3)
614 PATCH_INSN(rtrap_7win_patch4, rtrap_patch4)
615 PATCH_INSN(rtrap_7win_patch5, rtrap_patch5)
617 /* Patch for killing user windows from the register file. */
618 PATCH_INSN(kuw_patch1_7win, kuw_patch1)
620 /* Now patch the kernel window flush sequences.
621 * This saves 2 traps on every switch and fork.
624 set flush_patch_one, %g5
627 set flush_patch_two, %g5
630 set flush_patch_three, %g5
633 set flush_patch_four, %g5
636 set flush_patch_exception, %g5
639 set flush_patch_switch, %g5
644 sethi %hi(nwindows), %g4
645 st %g3, [%g4 + %lo(nwindows)] ! store final value
647 sethi %hi(nwindowsm1), %g4
648 st %g3, [%g4 + %lo(nwindowsm1)]
650 /* Here we go, start using Linux's trap table... */
655 /* Finally, turn on traps so that we can call c-code. */
663 /* Call sparc32_start_kernel(struct linux_romvec *rp) */
664 sethi %hi(prom_vector_p), %g5
665 ld [%g5 + %lo(prom_vector_p)], %o0
666 call sparc32_start_kernel
669 /* We should not get here. */
675 set sun4e_notsup, %o0
699 .asciz "\n\rOn sun4u you have to use sparc64 kernel\n\rand not a sparc32 version\n\r\n\r"
706 .word 0, sun4u_1, 0, 1, 0, 1, 0, sun4u_2, 0
710 .word 0, sun4u_3, 0, 4, 0, 1, 0
712 .word 0, 0, sun4u_4, 0, sun4u_1, 0, 8, 0
716 .word 0, sun4u_5, 0, 3, 0, 1, 0
718 .word 0, 0, sun4u_6, 0, sun4u_6e - sun4u_6 - 1, 0
722 .word 0, sun4u_7, 0, 0, 0, 0
735 mov sun4u_r4 - sun4u_a1, %l3
751 ld [%l1 + (sun4u_r1 - sun4u_a1)], %o1
752 add %l1, (sun4u_a2 - sun4u_a1), %o0
754 st %o1, [%o0 + (sun4u_i2 - sun4u_a2)]
756 ld [%l1 + (sun4u_1 - sun4u_a1)], %o1
757 add %l1, (sun4u_a3 - sun4u_a1), %o0
759 st %o1, [%o0 + (sun4u_i3 - sun4u_a3)]
762 add %l1, (sun4u_a4 - sun4u_a1), %o0
767 call %o0 ! Get us out of here...
768 nop ! Apparently Solaris is better.
770 /* Ok, now we continue in the .data/.text sections */
776 * Fill up the prom vector, note in particular the kind first element,
777 * no joke. I don't need all of them in here as the entire prom vector
778 * gets initialized in c-code so all routines can use it.
784 /* We calculate the following at boot time, window fills/spills and trap entry
785 * code uses these to keep track of the register windows.
796 /* Boot time debugger vector value. We need this later on. */