2 * head.S: The initial boot code for the Sparc port of Linux.
4 * Copyright (C) 1995 David S. Miller (davem@caip.rutgers.edu)
5 * Copyright (C) 1995,1999 Pete Zaitcev (zaitcev@yahoo.com)
6 * Copyright (C) 1996 Miguel de Icaza (miguel@nuclecu.unam.mx)
7 * Copyright (C) 1997 Jakub Jelinek (jj@sunsite.mff.cuni.cz)
8 * Copyright (C) 1997 Michael A. Griffith (grif@acm.org)
10 * CompactPCI platform by Eric Brower, 1999.
13 #include <linux/version.h>
14 #include <linux/init.h>
18 #include <asm/contregs.h>
19 #include <asm/ptrace.h>
22 #include <asm/kdebug.h>
23 #include <asm/winmacro.h>
24 #include <asm/thread_info.h> /* TI_UWINMASK */
25 #include <asm/errno.h>
26 #include <asm/pgtsrmmu.h> /* SRMMU_PGDIR_SHIFT */
29 /* The following are used with the prom_vector node-ops to figure out
38 /* Tested on SS-5, SS-10 */
46 .asciz "Sparc-Linux sun4/sun4c or MMU-less not supported\n\n"
50 .asciz "Sparc-Linux sun4e support does not exist\n\n"
53 /* The trap-table - located in the __HEAD section */
54 #include "ttable_32.S"
58 /* This was the only reasonable way I could think of to properly align
59 * these page-table data structures.
61 .globl empty_zero_page
62 empty_zero_page: .skip PAGE_SIZE
67 .global sparc_ramdisk_image
68 .global sparc_ramdisk_size
70 /* This stuff has to be in sync with SILO and other potential boot loaders
71 * Fields should be kept upward compatible and whenever any change is made,
72 * HdrS version should be incremented.
75 .word LINUX_VERSION_CODE
76 .half 0x0203 /* HdrS version */
91 /* Cool, here we go. Pick up the romvec pointer in %o0 and stash it in
92 * %g7 and at prom_vector_p. And also quickly check whether we are on
93 * a v0, v2, or v3 prom.
96 /* Ok, it's nice to know, as early as possible, if we
97 * are already mapped where we expect to be in virtual
98 * memory. The Solaris /boot elf format bootloader
99 * will peek into our elf header and load us where
100 * we want to be, otherwise we have to re-map.
102 * Some boot loaders don't place the jmp'rs address
103 * in %o7, so we do a pc-relative call to a local
104 * label, then see what %o7 has.
107 mov %o7, %g4 ! Save %o7
109 /* Jump to it, and pray... */
119 mov %g4, %o7 /* Previous %o7. */
121 mov %o0, %l0 ! stash away romvec
122 mov %o0, %g7 ! put it here too
123 mov %o1, %l1 ! stash away debug_vec too
125 /* Ok, let's check out our run time program counter. */
131 /* %l6 will hold the offset we have to subtract
132 * from absolute symbols in order to access areas
133 * in our own image. If already mapped this is
134 * just plain zero, else it is KERNBASE.
143 /* Copy over the Prom's level 14 clock handler. */
147 * preserve our linked/calculated instructions
151 sub %g1, %l6, %g1 ! translate to physical
152 sub %g3, %l6, %g3 ! translate to physical
159 andn %g1, 0xfff, %g1 ! proms trap table base
160 or %g0, (0x1e<<4), %g2 ! offset to lvl14 intr
167 std %g4, [%g3 + 0x8] ! Copy proms handler
169 /* DON'T TOUCH %l0 thru %l5 in these remapping routines,
170 * we need their values afterwards!
173 /* Now check whether we are already mapped, if we
174 * are we can skip all this garbage coming up.
178 be go_to_highmem ! this will be a nop then
181 /* Validate that we are in fact running on an
199 /* It looks like this is a machine we support.
200 * Now find out what MMU we are dealing with
201 * LEON - identified by the psr.impl field
202 * Viking - identified by the psr.impl field
203 * In all other cases a sun4m srmmu.
204 * We check that the MMU is enabled in all cases.
207 /* Check if this is a LEON CPU */
209 srl %g3, PSR_IMPL_SHIFT, %g3
210 and %g3, PSR_IMPL_SHIFTED_MASK, %g3
211 cmp %g3, PSR_IMPL_LEON
212 be leon_remap /* It is a LEON - jump */
215 /* Sanity-check, is MMU enabled */
216 lda [%g0] ASI_M_MMUREGS, %g1
221 /* Check for a viking (TI) module. */
226 /* Figure out what kind of viking we are on.
227 * We need to know if we have to play with the
228 * AC bit and disable traps or not.
231 /* I've only seen MicroSparc's on SparcClassics with this
235 lda [%g0] ASI_M_MMUREGS, %g3 ! peek in the control reg
238 bnz srmmu_not_viking ! is in mbus mode
241 rd %psr, %g3 ! DO NOT TOUCH %g3
242 andn %g3, PSR_ET, %g2
246 /* Get context table pointer, then convert to
247 * a physical address, which is 36 bits.
250 lda [%g4] ASI_M_MMUREGS, %g4
251 sll %g4, 0x4, %g4 ! We use this below
254 /* Set the AC bit in the Viking's MMU control reg. */
255 lda [%g0] ASI_M_MMUREGS, %g5 ! DO NOT TOUCH %g5
256 set 0x8000, %g6 ! AC bit mask
257 or %g5, %g6, %g6 ! Or it in...
258 sta %g6, [%g0] ASI_M_MMUREGS ! Close your eyes...
260 /* Grrr, why does it seem like every other load/store
261 * on the sun4m is in some ASI space...
262 * Fine with me, let's get the pointer to the level 1
263 * page table directory and fetch its entry.
265 lda [%g4] ASI_M_BYPASS, %o1 ! This is a level 1 ptr
266 srl %o1, 0x4, %o1 ! Clear low 4 bits
267 sll %o1, 0x8, %o1 ! Make physical
269 /* Ok, pull in the PTD. */
270 lda [%o1] ASI_M_BYPASS, %o2 ! This is the 0x0 16MB pgd
272 /* Calculate to KERNBASE entry. */
273 add %o1, KERNBASE >> (SRMMU_PGDIR_SHIFT - 2), %o3
275 /* Poke the entry into the calculated address. */
276 sta %o2, [%o3] ASI_M_BYPASS
278 /* I don't get it Sun, if you engineered all these
279 * boot loaders and the PROM (thank you for the debugging
280 * features btw) why did you not have them load kernel
281 * images up in high address space, since this is necessary
282 * for ABI compliance anyways? Does this low-mapping provide
283 * enhanced interoperability?
285 * "The PROM is the computer."
288 /* Ok, restore the MMU control register we saved in %g5 */
289 sta %g5, [%g0] ASI_M_MMUREGS ! POW... ouch
291 /* Turn traps back on. We saved it in %g3 earlier. */
292 wr %g3, 0x0, %psr ! tick tock, tick tock
294 /* Now we burn precious CPU cycles due to bad engineering. */
297 /* Wow, all that just to move a 32-bit value from one
298 * place to another... Jump to high memory.
304 /* This works on viking's in Mbus mode and all
305 * other MBUS modules. It is virtually the same as
306 * the above madness sans turning traps off and flipping
310 lda [%g1] ASI_M_MMUREGS, %g1 ! get ctx table ptr
311 sll %g1, 0x4, %g1 ! make physical addr
312 lda [%g1] ASI_M_BYPASS, %g1 ! ptr to level 1 pg_table
314 sll %g1, 0x8, %g1 ! make phys addr for l1 tbl
316 lda [%g1] ASI_M_BYPASS, %g2 ! get level1 entry for 0x0
317 add %g1, KERNBASE >> (SRMMU_PGDIR_SHIFT - 2), %g3
318 sta %g2, [%g3] ASI_M_BYPASS ! place at KERNBASE entry
324 /* Sanity-check, is MMU enabled */
325 lda [%g0] ASI_LEON_MMUREGS, %g1
330 /* Same code as in the srmmu_not_viking case,
331 * with the LEON ASI for mmuregs
334 lda [%g1] ASI_LEON_MMUREGS, %g1 ! get ctx table ptr
335 sll %g1, 0x4, %g1 ! make physical addr
336 lda [%g1] ASI_M_BYPASS, %g1 ! ptr to level 1 pg_table
338 sll %g1, 0x8, %g1 ! make phys addr for l1 tbl
340 lda [%g1] ASI_M_BYPASS, %g2 ! get level1 entry for 0x0
341 add %g1, KERNBASE >> (SRMMU_PGDIR_SHIFT - 2), %g3
342 sta %g2, [%g3] ASI_M_BYPASS ! place at KERNBASE entry
346 /* Now do a non-relative jump so that PC is in high-memory */
348 set execute_in_high_mem, %g1
352 /* The code above should be at beginning and we have to take care about
353 * short jumps, as branching to .init.text section from .text is usually
356 /* Acquire boot time privileged register values, this will help debugging.
357 * I figure out and store nwindows and nwindowsm1 later on.
360 mov %l0, %o0 ! put back romvec
361 mov %l1, %o1 ! and debug_vec
363 sethi %hi(prom_vector_p), %g1
364 st %o0, [%g1 + %lo(prom_vector_p)]
366 sethi %hi(linux_dbvec), %g1
367 st %o1, [%g1 + %lo(linux_dbvec)]
369 /* Get the machine type via the romvec
370 * getprops node operation
376 or %g0, %g0, %o0 ! next_node(0) = first_node
379 sethi %hi(cputypvar), %o1 ! First node has cpu-arch
380 or %o1, %lo(cputypvar), %o1
381 sethi %hi(cputypval), %o2 ! information, the string
382 or %o2, %lo(cputypval), %o2
383 ld [%l1], %l0 ! 'compatible' tells
384 ld [%l0 + 0xc], %l0 ! that we want 'sun4x' where
385 call %l0 ! x is one of 'm', 'd' or 'e'.
386 nop ! %o2 holds pointer
387 ! to a buf where above string
388 ! will get stored by the prom.
391 /* Check value of "compatible" property.
397 * sun4e => "no_sun4e_here"
398 * '*' => "no_sun4u_here"
399 * Check single letters only
403 /* If cputypval[0] == 'l' (lower case letter L) this is leon */
409 /* Check cputypval[4] to find the sun model */
410 ldub [%o2 + 0x4], %l1
419 be no_sun4e_here ! Could be a sun4e.
421 b no_sun4u_here ! AIEEE, a V9 sun4u... Get our BIG BROTHER kernel :))
425 /* LEON CPU - set boot_cpu_id */
426 sethi %hi(boot_cpu_id), %g2 ! boot-cpu index
429 ldub [%g2 + %lo(boot_cpu_id)], %g1
430 cmp %g1, 0xff ! unset means first CPU
431 bne leon_smp_cpu_startup ! continue only with master
434 /* Get CPU-ID from most significant 4-bit of ASR17 */
438 /* Update boot_cpu_id only on boot cpu */
439 stub %g1, [%g2 + %lo(boot_cpu_id)]
444 /* CPUID in bootbus can be found at PA 0xff0140000 */
445 #define SUN4D_BOOTBUS_CPUID 0xf0140000
448 /* Need to patch call to handler_irq */
449 set patch_handler_irq, %g4
450 set sun4d_handler_irq, %g5
451 sethi %hi(0x40000000), %g3 ! call
458 /* Get our CPU id out of bootbus */
459 set SUN4D_BOOTBUS_CPUID, %g3
460 lduba [%g3] ASI_M_CTL, %g3
463 sta %g4, [%g0] ASI_M_VIKING_TMP1
464 sethi %hi(boot_cpu_id), %g5
465 stb %g4, [%g5 + %lo(boot_cpu_id)]
468 /* Fall through to sun4m_init */
471 /* Ok, the PROM could have done funny things and apple cider could still
472 * be sitting in the fault status/address registers. Read them all to
473 * clear them so we don't get magic faults later on.
475 /* This sucks, apparently this makes Vikings call prom panic, will fix later */
478 srl %o1, PSR_IMPL_SHIFT, %o1 ! Get a type of the CPU
480 subcc %o1, PSR_IMPL_TI, %g0 ! TI: Viking or MicroSPARC
485 lda [%o0] ASI_M_MMUREGS, %g0
487 lda [%o0] ASI_M_MMUREGS, %g0
489 /* Fujitsu MicroSPARC-II has no asynchronous flavors of FARs */
495 lda [%o0] ASI_M_MMUREGS, %g0
497 lda [%o0] ASI_M_MMUREGS, %g0
503 /* Aieee, now set PC and nPC, enable traps, give ourselves a stack and it's
506 /* Turn on Supervisor, EnableFloating, and all the PIL bits.
507 * Also puts us in register window zero with traps off.
509 set (PSR_PS | PSR_S | PSR_PIL | PSR_EF), %g2
513 /* I want a kernel stack NOW! */
514 set init_thread_union, %g1
515 set (THREAD_SIZE - STACKFRAME_SZ), %g2
517 mov 0, %fp /* And for good luck */
519 /* Zero out our BSS section. */
520 set __bss_start , %o0 ! First address of BSS
521 set _end , %o1 ! Last address of BSS
529 /* If boot_cpu_id has not been setup by machine specific
530 * init-code above we default it to zero.
532 sethi %hi(boot_cpu_id), %g2
533 ldub [%g2 + %lo(boot_cpu_id)], %g3
538 stub %g3, [%g2 + %lo(boot_cpu_id)]
542 /* Initialize the uwinmask value for init task just in case.
543 * But first make current_set[boot_cpu_id] point to something useful.
545 set init_thread_union, %g6
553 st %g0, [%g6 + TI_UWINMASK]
555 /* Compute NWINDOWS and stash it away. Now uses %wim trick explained
556 * in the V8 manual. Ok, this method seems to work, Sparc is cool...
557 * No, it doesn't work, have to play the save/readCWP/restore trick.
560 wr %g0, 0x0, %wim ! so we do not get a trap
573 wr %g1, 0x0, %wim ! make window 1 invalid
580 /* Adjust our window handling routines to
581 * do things correctly on 7 window Sparcs.
584 #define PATCH_INSN(src, dest) \
590 /* Patch for window spills... */
591 PATCH_INSN(spnwin_patch1_7win, spnwin_patch1)
592 PATCH_INSN(spnwin_patch2_7win, spnwin_patch2)
593 PATCH_INSN(spnwin_patch3_7win, spnwin_patch3)
595 /* Patch for window fills... */
596 PATCH_INSN(fnwin_patch1_7win, fnwin_patch1)
597 PATCH_INSN(fnwin_patch2_7win, fnwin_patch2)
599 /* Patch for trap entry setup... */
600 PATCH_INSN(tsetup_7win_patch1, tsetup_patch1)
601 PATCH_INSN(tsetup_7win_patch2, tsetup_patch2)
602 PATCH_INSN(tsetup_7win_patch3, tsetup_patch3)
603 PATCH_INSN(tsetup_7win_patch4, tsetup_patch4)
604 PATCH_INSN(tsetup_7win_patch5, tsetup_patch5)
605 PATCH_INSN(tsetup_7win_patch6, tsetup_patch6)
607 /* Patch for returning from traps... */
608 PATCH_INSN(rtrap_7win_patch1, rtrap_patch1)
609 PATCH_INSN(rtrap_7win_patch2, rtrap_patch2)
610 PATCH_INSN(rtrap_7win_patch3, rtrap_patch3)
611 PATCH_INSN(rtrap_7win_patch4, rtrap_patch4)
612 PATCH_INSN(rtrap_7win_patch5, rtrap_patch5)
614 /* Patch for killing user windows from the register file. */
615 PATCH_INSN(kuw_patch1_7win, kuw_patch1)
617 /* Now patch the kernel window flush sequences.
618 * This saves 2 traps on every switch and fork.
621 set flush_patch_one, %g5
624 set flush_patch_two, %g5
627 set flush_patch_three, %g5
630 set flush_patch_four, %g5
633 set flush_patch_exception, %g5
636 set flush_patch_switch, %g5
641 sethi %hi(nwindows), %g4
642 st %g3, [%g4 + %lo(nwindows)] ! store final value
644 sethi %hi(nwindowsm1), %g4
645 st %g3, [%g4 + %lo(nwindowsm1)]
647 /* Here we go, start using Linux's trap table... */
652 /* Finally, turn on traps so that we can call c-code. */
660 /* Call sparc32_start_kernel(struct linux_romvec *rp) */
661 sethi %hi(prom_vector_p), %g5
662 ld [%g5 + %lo(prom_vector_p)], %o0
663 call sparc32_start_kernel
666 /* We should not get here. */
672 set sun4e_notsup, %o0
696 .asciz "\n\rOn sun4u you have to use sparc64 kernel\n\rand not a sparc32 version\n\r\n\r"
703 .word 0, sun4u_1, 0, 1, 0, 1, 0, sun4u_2, 0
707 .word 0, sun4u_3, 0, 4, 0, 1, 0
709 .word 0, 0, sun4u_4, 0, sun4u_1, 0, 8, 0
713 .word 0, sun4u_5, 0, 3, 0, 1, 0
715 .word 0, 0, sun4u_6, 0, sun4u_6e - sun4u_6 - 1, 0
719 .word 0, sun4u_7, 0, 0, 0, 0
732 mov sun4u_r4 - sun4u_a1, %l3
748 ld [%l1 + (sun4u_r1 - sun4u_a1)], %o1
749 add %l1, (sun4u_a2 - sun4u_a1), %o0
751 st %o1, [%o0 + (sun4u_i2 - sun4u_a2)]
753 ld [%l1 + (sun4u_1 - sun4u_a1)], %o1
754 add %l1, (sun4u_a3 - sun4u_a1), %o0
756 st %o1, [%o0 + (sun4u_i3 - sun4u_a3)]
759 add %l1, (sun4u_a4 - sun4u_a1), %o0
764 call %o0 ! Get us out of here...
765 nop ! Apparently Solaris is better.
767 /* Ok, now we continue in the .data/.text sections */
773 * Fill up the prom vector, note in particular the kind first element,
774 * no joke. I don't need all of them in here as the entire prom vector
775 * gets initialized in c-code so all routines can use it.
781 /* We calculate the following at boot time, window fills/spills and trap entry
782 * code uses these to keep track of the register windows.
793 /* Boot time debugger vector value. We need this later on. */
811 .section ".fixup",#alloc,#execinstr
815 restore %g0, -EFAULT, %o0