Merge branch 'upstream-fixes' of master.kernel.org:/pub/scm/linux/kernel/git/jgarzik...
[powerpc.git] / arch / sparc64 / kernel / head.S
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.
3  *
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)
8  */
9
10 #include <linux/config.h>
11 #include <linux/version.h>
12 #include <linux/errno.h>
13 #include <asm/thread_info.h>
14 #include <asm/asi.h>
15 #include <asm/pstate.h>
16 #include <asm/ptrace.h>
17 #include <asm/spitfire.h>
18 #include <asm/page.h>
19 #include <asm/pgtable.h>
20 #include <asm/errno.h>
21 #include <asm/signal.h>
22 #include <asm/processor.h>
23 #include <asm/lsu.h>
24 #include <asm/dcr.h>
25 #include <asm/dcu.h>
26 #include <asm/head.h>
27 #include <asm/ttable.h>
28 #include <asm/mmu.h>
29         
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. 
35  */
36
37         .text
38         .globl  start, _start, stext, _stext
39 _start:
40 start:
41 _stext:
42 stext:
43 bootup_user_stack:
44 ! 0x0000000000404000
45         b       sparc64_boot
46          flushw                                 /* Flush register file.      */
47
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.
51  */
52         .global root_flags, ram_flags, root_dev
53         .global sparc_ramdisk_image, sparc_ramdisk_size
54         .global sparc_ramdisk_image64
55
56         .ascii  "HdrS"
57         .word   LINUX_VERSION_CODE
58
59         /* History:
60          *
61          * 0x0300 : Supports being located at other than 0x4000
62          * 0x0202 : Supports kernel params string
63          * 0x0201 : Supports reboot_command
64          */
65         .half   0x0301          /* HdrS version */
66
67 root_flags:
68         .half   1
69 root_dev:
70         .half   0
71 ram_flags:
72         .half   0
73 sparc_ramdisk_image:
74         .word   0
75 sparc_ramdisk_size:
76         .word   0
77         .xword  reboot_command
78         .xword  bootstr_info
79 sparc_ramdisk_image64:
80         .xword  0
81         .word   _end
82
83         /* PROM cif handler code address is in %o4.  */
84 sparc64_boot:
85 1:      rd      %pc, %g7
86         set     1b, %g1
87         cmp     %g1, %g7
88         be,pn   %xcc, sparc64_boot_after_remap
89          mov    %o4, %l7
90
91         /* We need to remap the kernel.  Use position independant
92          * code to remap us to KERNBASE.
93          *
94          * SILO can invoke us with 32-bit address masking enabled,
95          * so make sure that's clear.
96          */
97         rdpr    %pstate, %g1
98         andn    %g1, PSTATE_AM, %g1
99         wrpr    %g1, 0x0, %pstate
100         ba,a,pt %xcc, 1f
101
102         .globl  prom_finddev_name, prom_chosen_path
103         .globl  prom_getprop_name, prom_mmu_name
104         .globl  prom_callmethod_name, prom_translate_name
105         .globl  prom_map_name, prom_unmap_name, prom_mmu_ihandle_cache
106         .globl  prom_boot_mapped_pc, prom_boot_mapping_mode
107         .globl  prom_boot_mapping_phys_high, prom_boot_mapping_phys_low
108 prom_finddev_name:
109         .asciz  "finddevice"
110 prom_chosen_path:
111         .asciz  "/chosen"
112 prom_getprop_name:
113         .asciz  "getprop"
114 prom_mmu_name:
115         .asciz  "mmu"
116 prom_callmethod_name:
117         .asciz  "call-method"
118 prom_translate_name:
119         .asciz  "translate"
120 prom_map_name:
121         .asciz  "map"
122 prom_unmap_name:
123         .asciz  "unmap"
124         .align  4
125 prom_mmu_ihandle_cache:
126         .word   0
127 prom_boot_mapped_pc:
128         .word   0
129 prom_boot_mapping_mode:
130         .word   0
131         .align  8
132 prom_boot_mapping_phys_high:
133         .xword  0
134 prom_boot_mapping_phys_low:
135         .xword  0
136 1:
137         rd      %pc, %l0
138         mov     (1b - prom_finddev_name), %l1
139         mov     (1b - prom_chosen_path), %l2
140         mov     (1b - prom_boot_mapped_pc), %l3
141         sub     %l0, %l1, %l1
142         sub     %l0, %l2, %l2
143         sub     %l0, %l3, %l3
144         stw     %l0, [%l3]
145         sub     %sp, (192 + 128), %sp
146
147         /* chosen_node = prom_finddevice("/chosen") */
148         stx     %l1, [%sp + 2047 + 128 + 0x00]  ! service, "finddevice"
149         mov     1, %l3
150         stx     %l3, [%sp + 2047 + 128 + 0x08]  ! num_args, 1
151         stx     %l3, [%sp + 2047 + 128 + 0x10]  ! num_rets, 1
152         stx     %l2, [%sp + 2047 + 128 + 0x18]  ! arg1, "/chosen"
153         stx     %g0, [%sp + 2047 + 128 + 0x20]  ! ret1
154         call    %l7
155          add    %sp, (2047 + 128), %o0          ! argument array
156
157         ldx     [%sp + 2047 + 128 + 0x20], %l4  ! chosen device node
158
159         mov     (1b - prom_getprop_name), %l1
160         mov     (1b - prom_mmu_name), %l2
161         mov     (1b - prom_mmu_ihandle_cache), %l5
162         sub     %l0, %l1, %l1
163         sub     %l0, %l2, %l2
164         sub     %l0, %l5, %l5
165
166         /* prom_mmu_ihandle_cache = prom_getint(chosen_node, "mmu") */
167         stx     %l1, [%sp + 2047 + 128 + 0x00]  ! service, "getprop"
168         mov     4, %l3
169         stx     %l3, [%sp + 2047 + 128 + 0x08]  ! num_args, 4
170         mov     1, %l3
171         stx     %l3, [%sp + 2047 + 128 + 0x10]  ! num_rets, 1
172         stx     %l4, [%sp + 2047 + 128 + 0x18]  ! arg1, chosen_node
173         stx     %l2, [%sp + 2047 + 128 + 0x20]  ! arg2, "mmu"
174         stx     %l5, [%sp + 2047 + 128 + 0x28]  ! arg3, &prom_mmu_ihandle_cache
175         mov     4, %l3
176         stx     %l3, [%sp + 2047 + 128 + 0x30]  ! arg4, sizeof(arg3)
177         stx     %g0, [%sp + 2047 + 128 + 0x38]  ! ret1
178         call    %l7
179          add    %sp, (2047 + 128), %o0          ! argument array
180
181         mov     (1b - prom_callmethod_name), %l1
182         mov     (1b - prom_translate_name), %l2
183         sub     %l0, %l1, %l1
184         sub     %l0, %l2, %l2
185         lduw    [%l5], %l5                      ! prom_mmu_ihandle_cache
186
187         stx     %l1, [%sp + 2047 + 128 + 0x00]  ! service, "call-method"
188         mov     3, %l3
189         stx     %l3, [%sp + 2047 + 128 + 0x08]  ! num_args, 3
190         mov     5, %l3
191         stx     %l3, [%sp + 2047 + 128 + 0x10]  ! num_rets, 5
192         stx     %l2, [%sp + 2047 + 128 + 0x18]  ! arg1: "translate"
193         stx     %l5, [%sp + 2047 + 128 + 0x20]  ! arg2: prom_mmu_ihandle_cache
194         /* PAGE align */
195         srlx    %l0, 13, %l3
196         sllx    %l3, 13, %l3
197         stx     %l3, [%sp + 2047 + 128 + 0x28]  ! arg3: vaddr, our PC
198         stx     %g0, [%sp + 2047 + 128 + 0x30]  ! res1
199         stx     %g0, [%sp + 2047 + 128 + 0x38]  ! res2
200         stx     %g0, [%sp + 2047 + 128 + 0x40]  ! res3
201         stx     %g0, [%sp + 2047 + 128 + 0x48]  ! res4
202         stx     %g0, [%sp + 2047 + 128 + 0x50]  ! res5
203         call    %l7
204          add    %sp, (2047 + 128), %o0          ! argument array
205
206         ldx     [%sp + 2047 + 128 + 0x40], %l1  ! translation mode
207         mov     (1b - prom_boot_mapping_mode), %l4
208         sub     %l0, %l4, %l4
209         stw     %l1, [%l4]
210         mov     (1b - prom_boot_mapping_phys_high), %l4
211         sub     %l0, %l4, %l4
212         ldx     [%sp + 2047 + 128 + 0x48], %l2  ! physaddr high
213         stx     %l2, [%l4 + 0x0]
214         ldx     [%sp + 2047 + 128 + 0x50], %l3  ! physaddr low
215         /* 4MB align */
216         srlx    %l3, 22, %l3
217         sllx    %l3, 22, %l3
218         stx     %l3, [%l4 + 0x8]
219
220         /* Leave service as-is, "call-method" */
221         mov     7, %l3
222         stx     %l3, [%sp + 2047 + 128 + 0x08]  ! num_args, 7
223         mov     1, %l3
224         stx     %l3, [%sp + 2047 + 128 + 0x10]  ! num_rets, 1
225         mov     (1b - prom_map_name), %l3
226         sub     %l0, %l3, %l3
227         stx     %l3, [%sp + 2047 + 128 + 0x18]  ! arg1: "map"
228         /* Leave arg2 as-is, prom_mmu_ihandle_cache */
229         mov     -1, %l3
230         stx     %l3, [%sp + 2047 + 128 + 0x28]  ! arg3: mode (-1 default)
231         sethi   %hi(8 * 1024 * 1024), %l3
232         stx     %l3, [%sp + 2047 + 128 + 0x30]  ! arg4: size (8MB)
233         sethi   %hi(KERNBASE), %l3
234         stx     %l3, [%sp + 2047 + 128 + 0x38]  ! arg5: vaddr (KERNBASE)
235         stx     %g0, [%sp + 2047 + 128 + 0x40]  ! arg6: empty
236         mov     (1b - prom_boot_mapping_phys_low), %l3
237         sub     %l0, %l3, %l3
238         ldx     [%l3], %l3
239         stx     %l3, [%sp + 2047 + 128 + 0x48]  ! arg7: phys addr
240         call    %l7
241          add    %sp, (2047 + 128), %o0          ! argument array
242
243         add     %sp, (192 + 128), %sp
244
245 sparc64_boot_after_remap:
246         BRANCH_IF_CHEETAH_BASE(g1,g7,cheetah_boot)
247         BRANCH_IF_CHEETAH_PLUS_OR_FOLLOWON(g1,g7,cheetah_plus_boot)
248         ba,pt   %xcc, spitfire_boot
249          nop
250
251 cheetah_plus_boot:
252         /* Preserve OBP chosen DCU and DCR register settings.  */
253         ba,pt   %xcc, cheetah_generic_boot
254          nop
255
256 cheetah_boot:
257         mov     DCR_BPE | DCR_RPE | DCR_SI | DCR_IFPOE | DCR_MS, %g1
258         wr      %g1, %asr18
259
260         sethi   %uhi(DCU_ME|DCU_RE|DCU_HPE|DCU_SPE|DCU_SL|DCU_WE), %g7
261         or      %g7, %ulo(DCU_ME|DCU_RE|DCU_HPE|DCU_SPE|DCU_SL|DCU_WE), %g7
262         sllx    %g7, 32, %g7
263         or      %g7, DCU_DM | DCU_IM | DCU_DC | DCU_IC, %g7
264         stxa    %g7, [%g0] ASI_DCU_CONTROL_REG
265         membar  #Sync
266
267 cheetah_generic_boot:
268         mov     TSB_EXTENSION_P, %g3
269         stxa    %g0, [%g3] ASI_DMMU
270         stxa    %g0, [%g3] ASI_IMMU
271         membar  #Sync
272
273         mov     TSB_EXTENSION_S, %g3
274         stxa    %g0, [%g3] ASI_DMMU
275         membar  #Sync
276
277         mov     TSB_EXTENSION_N, %g3
278         stxa    %g0, [%g3] ASI_DMMU
279         stxa    %g0, [%g3] ASI_IMMU
280         membar  #Sync
281
282         ba,a,pt %xcc, jump_to_sun4u_init
283
284 spitfire_boot:
285         /* Typically PROM has already enabled both MMU's and both on-chip
286          * caches, but we do it here anyway just to be paranoid.
287          */
288         mov     (LSU_CONTROL_IC|LSU_CONTROL_DC|LSU_CONTROL_IM|LSU_CONTROL_DM), %g1
289         stxa    %g1, [%g0] ASI_LSU_CONTROL
290         membar  #Sync
291
292 jump_to_sun4u_init:
293         /*
294          * Make sure we are in privileged mode, have address masking,
295          * using the ordinary globals and have enabled floating
296          * point.
297          *
298          * Again, typically PROM has left %pil at 13 or similar, and
299          * (PSTATE_PRIV | PSTATE_PEF | PSTATE_IE) in %pstate.
300          */
301         wrpr    %g0, (PSTATE_PRIV|PSTATE_PEF|PSTATE_IE), %pstate
302         wr      %g0, 0, %fprs
303
304         set     sun4u_init, %g2
305         jmpl    %g2 + %g0, %g0
306          nop
307
308 sun4u_init:
309         /* Set ctx 0 */
310         mov     PRIMARY_CONTEXT, %g7
311         stxa    %g0, [%g7] ASI_DMMU
312         membar  #Sync
313
314         mov     SECONDARY_CONTEXT, %g7
315         stxa    %g0, [%g7] ASI_DMMU
316         membar  #Sync
317
318         BRANCH_IF_ANY_CHEETAH(g1,g7,cheetah_tlb_fixup)
319
320         ba,pt   %xcc, spitfire_tlb_fixup
321          nop
322
323 cheetah_tlb_fixup:
324         mov     2, %g2          /* Set TLB type to cheetah+. */
325         BRANCH_IF_CHEETAH_PLUS_OR_FOLLOWON(g1,g7,1f)
326
327         mov     1, %g2          /* Set TLB type to cheetah. */
328
329 1:      sethi   %hi(tlb_type), %g1
330         stw     %g2, [%g1 + %lo(tlb_type)]
331
332         /* Patch copy/page operations to cheetah optimized versions. */
333         call    cheetah_patch_copyops
334          nop
335         call    cheetah_patch_copy_page
336          nop
337         call    cheetah_patch_cachetlbops
338          nop
339
340         ba,pt   %xcc, tlb_fixup_done
341          nop
342
343 spitfire_tlb_fixup:
344         /* Set TLB type to spitfire. */
345         mov     0, %g2
346         sethi   %hi(tlb_type), %g1
347         stw     %g2, [%g1 + %lo(tlb_type)]
348
349 tlb_fixup_done:
350         sethi   %hi(init_thread_union), %g6
351         or      %g6, %lo(init_thread_union), %g6
352         ldx     [%g6 + TI_TASK], %g4
353         mov     %sp, %l6
354         mov     %o4, %l7
355
356         wr      %g0, ASI_P, %asi
357         mov     1, %g1
358         sllx    %g1, THREAD_SHIFT, %g1
359         sub     %g1, (STACKFRAME_SZ + STACK_BIAS), %g1
360         add     %g6, %g1, %sp
361         mov     0, %fp
362
363         /* Set per-cpu pointer initially to zero, this makes
364          * the boot-cpu use the in-kernel-image per-cpu areas
365          * before setup_per_cpu_area() is invoked.
366          */
367         clr     %g5
368
369         wrpr    %g0, 0, %wstate
370         wrpr    %g0, 0x0, %tl
371
372         /* Clear the bss */
373         sethi   %hi(__bss_start), %o0
374         or      %o0, %lo(__bss_start), %o0
375         sethi   %hi(_end), %o1
376         or      %o1, %lo(_end), %o1
377         call    __bzero
378          sub    %o1, %o0, %o1
379
380         mov     %l6, %o1                        ! OpenPROM stack
381         call    prom_init
382          mov    %l7, %o0                        ! OpenPROM cif handler
383
384         /* Off we go.... */
385         call    start_kernel
386          nop
387         /* Not reached... */
388
389         /* This is meant to allow the sharing of this code between
390          * boot processor invocation (via setup_tba() below) and
391          * secondary processor startup (via trampoline.S).  The
392          * former does use this code, the latter does not yet due
393          * to some complexities.  That should be fixed up at some
394          * point.
395          */
396         .globl  setup_trap_table
397 setup_trap_table:
398         save    %sp, -192, %sp
399
400         /* Force interrupts to be disabled.  Transferring over to
401          * the Linux trap table is a very delicate operation.
402          * Until we are actually on the Linux trap table, we cannot
403          * get the PAGE_OFFSET linear mappings translated.  We need
404          * that mapping to be setup in order to initialize the firmware
405          * page tables.
406          *
407          * So there is this window of time, from the return from
408          * prom_set_trap_table() until inherit_prom_mappings_post()
409          * (in arch/sparc64/mm/init.c) completes, during which no
410          * firmware address space accesses can be made.
411          */
412         rdpr    %pstate, %o1
413         andn    %o1, PSTATE_IE, %o1
414         wrpr    %o1, 0x0, %pstate
415         wrpr    %g0, 15, %pil
416
417         /* Ok, now make the final valid firmware call to jump over
418          * to the Linux trap table.
419          */
420         call    prom_set_trap_table
421          sethi  %hi(sparc64_ttable_tl0), %o0
422
423         /* Start using proper page size encodings in ctx register.  */
424         sethi   %hi(sparc64_kern_pri_context), %g3
425         ldx     [%g3 + %lo(sparc64_kern_pri_context)], %g2
426         mov     PRIMARY_CONTEXT, %g1
427         stxa    %g2, [%g1] ASI_DMMU
428         membar  #Sync
429
430         /* The Linux trap handlers expect various trap global registers
431          * to be setup with some fixed values.  So here we set these
432          * up very carefully.  These globals are:
433          *
434          * Alternate Globals (PSTATE_AG):
435          *
436          * %g6                  --> current_thread_info()
437          *
438          * MMU Globals (PSTATE_MG):
439          *
440          * %g1                  --> TLB_SFSR
441          * %g2                  --> ((_PAGE_VALID | _PAGE_SZ4MB |
442          *                            _PAGE_CP | _PAGE_CV | _PAGE_P | _PAGE_W)
443          *                           ^ 0xfffff80000000000)
444          * (this %g2 value is used for computing the PAGE_OFFSET kernel
445          *  TLB entries quickly, the virtual address of the fault XOR'd
446          *  with this %g2 value is the PTE to load into the TLB)
447          * %g3                  --> VPTE_BASE_CHEETAH or VPTE_BASE_SPITFIRE
448          *
449          * Interrupt Globals (PSTATE_IG, setup by init_irqwork_curcpu()):
450          *
451          * %g6                  --> __irq_work[smp_processor_id()]
452          */
453
454         rdpr    %pstate, %o1
455         mov     %g6, %o2
456         wrpr    %o1, PSTATE_AG, %pstate
457         mov     %o2, %g6
458
459 #define KERN_HIGHBITS           ((_PAGE_VALID|_PAGE_SZ4MB)^0xfffff80000000000)
460 #define KERN_LOWBITS            (_PAGE_CP | _PAGE_CV | _PAGE_P | _PAGE_W)
461         wrpr    %o1, PSTATE_MG, %pstate
462         mov     TSB_REG, %g1
463         stxa    %g0, [%g1] ASI_DMMU
464         membar  #Sync
465         stxa    %g0, [%g1] ASI_IMMU
466         membar  #Sync
467         mov     TLB_SFSR, %g1
468         sethi   %uhi(KERN_HIGHBITS), %g2
469         or      %g2, %ulo(KERN_HIGHBITS), %g2
470         sllx    %g2, 32, %g2
471         or      %g2, KERN_LOWBITS, %g2
472
473         BRANCH_IF_ANY_CHEETAH(g3,g7,8f)
474         ba,pt   %xcc, 9f
475          nop
476
477 8:
478         sethi           %uhi(VPTE_BASE_CHEETAH), %g3
479         or              %g3, %ulo(VPTE_BASE_CHEETAH), %g3
480         ba,pt           %xcc, 2f
481          sllx           %g3, 32, %g3
482
483 9:
484         sethi           %uhi(VPTE_BASE_SPITFIRE), %g3
485         or              %g3, %ulo(VPTE_BASE_SPITFIRE), %g3
486         sllx            %g3, 32, %g3
487
488 2:
489         clr     %g7
490 #undef KERN_HIGHBITS
491 #undef KERN_LOWBITS
492
493         /* Kill PROM timer */
494         sethi   %hi(0x80000000), %o2
495         sllx    %o2, 32, %o2
496         wr      %o2, 0, %tick_cmpr
497
498         BRANCH_IF_ANY_CHEETAH(o2,o3,1f)
499
500         ba,pt   %xcc, 2f
501          nop
502
503         /* Disable STICK_INT interrupts. */
504 1:
505         sethi   %hi(0x80000000), %o2
506         sllx    %o2, 32, %o2
507         wr      %o2, %asr25
508
509 2:
510         wrpr    %g0, %g0, %wstate
511         wrpr    %o1, 0x0, %pstate
512
513         call    init_irqwork_curcpu
514          nop
515
516         /* Now we can turn interrupts back on. */
517         rdpr    %pstate, %o1
518         or      %o1, PSTATE_IE, %o1
519         wrpr    %o1, 0, %pstate
520         wrpr    %g0, 0x0, %pil
521
522         ret
523          restore
524
525         .globl  setup_tba
526 setup_tba:      /* i0 = is_starfire */
527         save    %sp, -192, %sp
528
529         /* The boot processor is the only cpu which invokes this
530          * routine, the other cpus set things up via trampoline.S.
531          * So save the OBP trap table address here.
532          */
533         rdpr    %tba, %g7
534         sethi   %hi(prom_tba), %o1
535         or      %o1, %lo(prom_tba), %o1
536         stx     %g7, [%o1]
537
538         call    setup_trap_table
539          nop
540
541         ret
542          restore
543
544 /*
545  * The following skips make sure the trap table in ttable.S is aligned
546  * on a 32K boundary as required by the v9 specs for TBA register.
547  */
548 sparc64_boot_end:
549         .skip   0x2000 + _start - sparc64_boot_end
550 bootup_user_stack_end:
551         .skip   0x2000
552
553 #ifdef CONFIG_SBUS
554 /* This is just a hack to fool make depend config.h discovering
555    strategy: As the .S files below need config.h, but
556    make depend does not find it for them, we include config.h
557    in head.S */
558 #endif
559
560 ! 0x0000000000408000
561
562 #include "ttable.S"
563 #include "systbls.S"
564 #include "ktlb.S"
565 #include "etrap.S"
566 #include "rtrap.S"
567 #include "winfixup.S"
568 #include "entry.S"
569
570         /* This is just anal retentiveness on my part... */
571         .align  16384
572
573         .data
574         .align  8
575         .globl  prom_tba, tlb_type
576 prom_tba:       .xword  0
577 tlb_type:       .word   0       /* Must NOT end up in BSS */
578         .section        ".fixup",#alloc,#execinstr
579
580         .globl  __ret_efault, __retl_efault
581 __ret_efault:
582         ret
583          restore %g0, -EFAULT, %o0
584 __retl_efault:
585         retl
586          mov    -EFAULT, %o0