powerpc: Move altivec_unavailable
[safe/jmp/linux-2.6] / arch / powerpc / kernel / head_64.S
1 /*
2  *  PowerPC version
3  *    Copyright (C) 1995-1996 Gary Thomas (gdt@linuxppc.org)
4  *
5  *  Rewritten by Cort Dougan (cort@cs.nmt.edu) for PReP
6  *    Copyright (C) 1996 Cort Dougan <cort@cs.nmt.edu>
7  *  Adapted for Power Macintosh by Paul Mackerras.
8  *  Low-level exception handlers and MMU support
9  *  rewritten by Paul Mackerras.
10  *    Copyright (C) 1996 Paul Mackerras.
11  *
12  *  Adapted for 64bit PowerPC by Dave Engebretsen, Peter Bergner, and
13  *    Mike Corrigan {engebret|bergner|mikejc}@us.ibm.com
14  *
15  *  This file contains the low-level support and setup for the
16  *  PowerPC-64 platform, including trap and interrupt dispatch.
17  *
18  *  This program is free software; you can redistribute it and/or
19  *  modify it under the terms of the GNU General Public License
20  *  as published by the Free Software Foundation; either version
21  *  2 of the License, or (at your option) any later version.
22  */
23
24 #include <linux/threads.h>
25 #include <asm/reg.h>
26 #include <asm/page.h>
27 #include <asm/mmu.h>
28 #include <asm/ppc_asm.h>
29 #include <asm/asm-offsets.h>
30 #include <asm/bug.h>
31 #include <asm/cputable.h>
32 #include <asm/setup.h>
33 #include <asm/hvcall.h>
34 #include <asm/iseries/lpar_map.h>
35 #include <asm/thread_info.h>
36 #include <asm/firmware.h>
37 #include <asm/page_64.h>
38 #include <asm/exception.h>
39 #include <asm/irqflags.h>
40
41 /*
42  * We layout physical memory as follows:
43  * 0x0000 - 0x00ff : Secondary processor spin code
44  * 0x0100 - 0x2fff : pSeries Interrupt prologs
45  * 0x3000 - 0x5fff : interrupt support, iSeries and common interrupt prologs
46  * 0x6000 - 0x6fff : Initial (CPU0) segment table
47  * 0x7000 - 0x7fff : FWNMI data area
48  * 0x8000 -        : Early init and support code
49  */
50
51 /*
52  *   SPRG Usage
53  *
54  *   Register   Definition
55  *
56  *   SPRG0      reserved for hypervisor
57  *   SPRG1      temp - used to save gpr
58  *   SPRG2      temp - used to save gpr
59  *   SPRG3      virt addr of paca
60  */
61
62 /*
63  * Entering into this code we make the following assumptions:
64  *  For pSeries:
65  *   1. The MMU is off & open firmware is running in real mode.
66  *   2. The kernel is entered at __start
67  *
68  *  For iSeries:
69  *   1. The MMU is on (as it always is for iSeries)
70  *   2. The kernel is entered at system_reset_iSeries
71  */
72
73         .text
74         .globl  _stext
75 _stext:
76 _GLOBAL(__start)
77         /* NOP this out unconditionally */
78 BEGIN_FTR_SECTION
79         b       .__start_initialization_multiplatform
80 END_FTR_SECTION(0, 1)
81
82         /* Catch branch to 0 in real mode */
83         trap
84
85         /* Secondary processors spin on this value until it goes to 1. */
86         .globl  __secondary_hold_spinloop
87 __secondary_hold_spinloop:
88         .llong  0x0
89
90         /* Secondary processors write this value with their cpu # */
91         /* after they enter the spin loop immediately below.      */
92         .globl  __secondary_hold_acknowledge
93 __secondary_hold_acknowledge:
94         .llong  0x0
95
96 #ifdef CONFIG_PPC_ISERIES
97         /*
98          * At offset 0x20, there is a pointer to iSeries LPAR data.
99          * This is required by the hypervisor
100          */
101         . = 0x20
102         .llong hvReleaseData-KERNELBASE
103 #endif /* CONFIG_PPC_ISERIES */
104
105         . = 0x60
106 /*
107  * The following code is used to hold secondary processors
108  * in a spin loop after they have entered the kernel, but
109  * before the bulk of the kernel has been relocated.  This code
110  * is relocated to physical address 0x60 before prom_init is run.
111  * All of it must fit below the first exception vector at 0x100.
112  */
113 _GLOBAL(__secondary_hold)
114         mfmsr   r24
115         ori     r24,r24,MSR_RI
116         mtmsrd  r24                     /* RI on */
117
118         /* Grab our physical cpu number */
119         mr      r24,r3
120
121         /* Tell the master cpu we're here */
122         /* Relocation is off & we are located at an address less */
123         /* than 0x100, so only need to grab low order offset.    */
124         std     r24,__secondary_hold_acknowledge@l(0)
125         sync
126
127         /* All secondary cpus wait here until told to start. */
128 100:    ld      r4,__secondary_hold_spinloop@l(0)
129         cmpdi   0,r4,1
130         bne     100b
131
132 #if defined(CONFIG_SMP) || defined(CONFIG_KEXEC)
133         LOAD_REG_IMMEDIATE(r4, .generic_secondary_smp_init)
134         mtctr   r4
135         mr      r3,r24
136         bctr
137 #else
138         BUG_OPCODE
139 #endif
140
141 /* This value is used to mark exception frames on the stack. */
142         .section ".toc","aw"
143 exception_marker:
144         .tc     ID_72656773_68657265[TC],0x7265677368657265
145         .text
146
147 /*
148  * This is the start of the interrupt handlers for pSeries
149  * This code runs with relocation off.
150  */
151         . = 0x100
152         .globl __start_interrupts
153 __start_interrupts:
154
155         STD_EXCEPTION_PSERIES(0x100, system_reset)
156
157         . = 0x200
158 _machine_check_pSeries:
159         HMT_MEDIUM
160         mtspr   SPRN_SPRG1,r13          /* save r13 */
161         EXCEPTION_PROLOG_PSERIES(PACA_EXMC, machine_check_common)
162
163         . = 0x300
164         .globl data_access_pSeries
165 data_access_pSeries:
166         HMT_MEDIUM
167         mtspr   SPRN_SPRG1,r13
168 BEGIN_FTR_SECTION
169         mtspr   SPRN_SPRG2,r12
170         mfspr   r13,SPRN_DAR
171         mfspr   r12,SPRN_DSISR
172         srdi    r13,r13,60
173         rlwimi  r13,r12,16,0x20
174         mfcr    r12
175         cmpwi   r13,0x2c
176         beq     do_stab_bolted_pSeries
177         mtcrf   0x80,r12
178         mfspr   r12,SPRN_SPRG2
179 END_FTR_SECTION_IFCLR(CPU_FTR_SLB)
180         EXCEPTION_PROLOG_PSERIES(PACA_EXGEN, data_access_common)
181
182         . = 0x380
183         .globl data_access_slb_pSeries
184 data_access_slb_pSeries:
185         HMT_MEDIUM
186         mtspr   SPRN_SPRG1,r13
187         mfspr   r13,SPRN_SPRG3          /* get paca address into r13 */
188         std     r3,PACA_EXSLB+EX_R3(r13)
189         mfspr   r3,SPRN_DAR
190         std     r9,PACA_EXSLB+EX_R9(r13)        /* save r9 - r12 */
191         mfcr    r9
192 #ifdef __DISABLED__
193         /* Keep that around for when we re-implement dynamic VSIDs */
194         cmpdi   r3,0
195         bge     slb_miss_user_pseries
196 #endif /* __DISABLED__ */
197         std     r10,PACA_EXSLB+EX_R10(r13)
198         std     r11,PACA_EXSLB+EX_R11(r13)
199         std     r12,PACA_EXSLB+EX_R12(r13)
200         mfspr   r10,SPRN_SPRG1
201         std     r10,PACA_EXSLB+EX_R13(r13)
202         mfspr   r12,SPRN_SRR1           /* and SRR1 */
203         b       .slb_miss_realmode      /* Rel. branch works in real mode */
204
205         STD_EXCEPTION_PSERIES(0x400, instruction_access)
206
207         . = 0x480
208         .globl instruction_access_slb_pSeries
209 instruction_access_slb_pSeries:
210         HMT_MEDIUM
211         mtspr   SPRN_SPRG1,r13
212         mfspr   r13,SPRN_SPRG3          /* get paca address into r13 */
213         std     r3,PACA_EXSLB+EX_R3(r13)
214         mfspr   r3,SPRN_SRR0            /* SRR0 is faulting address */
215         std     r9,PACA_EXSLB+EX_R9(r13)        /* save r9 - r12 */
216         mfcr    r9
217 #ifdef __DISABLED__
218         /* Keep that around for when we re-implement dynamic VSIDs */
219         cmpdi   r3,0
220         bge     slb_miss_user_pseries
221 #endif /* __DISABLED__ */
222         std     r10,PACA_EXSLB+EX_R10(r13)
223         std     r11,PACA_EXSLB+EX_R11(r13)
224         std     r12,PACA_EXSLB+EX_R12(r13)
225         mfspr   r10,SPRN_SPRG1
226         std     r10,PACA_EXSLB+EX_R13(r13)
227         mfspr   r12,SPRN_SRR1           /* and SRR1 */
228         b       .slb_miss_realmode      /* Rel. branch works in real mode */
229
230         MASKABLE_EXCEPTION_PSERIES(0x500, hardware_interrupt)
231         STD_EXCEPTION_PSERIES(0x600, alignment)
232         STD_EXCEPTION_PSERIES(0x700, program_check)
233         STD_EXCEPTION_PSERIES(0x800, fp_unavailable)
234         MASKABLE_EXCEPTION_PSERIES(0x900, decrementer)
235         STD_EXCEPTION_PSERIES(0xa00, trap_0a)
236         STD_EXCEPTION_PSERIES(0xb00, trap_0b)
237
238         . = 0xc00
239         .globl  system_call_pSeries
240 system_call_pSeries:
241         HMT_MEDIUM
242 BEGIN_FTR_SECTION
243         cmpdi   r0,0x1ebe
244         beq-    1f
245 END_FTR_SECTION_IFSET(CPU_FTR_REAL_LE)
246         mr      r9,r13
247         mfmsr   r10
248         mfspr   r13,SPRN_SPRG3
249         mfspr   r11,SPRN_SRR0
250         clrrdi  r12,r13,32
251         oris    r12,r12,system_call_common@h
252         ori     r12,r12,system_call_common@l
253         mtspr   SPRN_SRR0,r12
254         ori     r10,r10,MSR_IR|MSR_DR|MSR_RI
255         mfspr   r12,SPRN_SRR1
256         mtspr   SPRN_SRR1,r10
257         rfid
258         b       .       /* prevent speculative execution */
259
260 /* Fast LE/BE switch system call */
261 1:      mfspr   r12,SPRN_SRR1
262         xori    r12,r12,MSR_LE
263         mtspr   SPRN_SRR1,r12
264         rfid            /* return to userspace */
265         b       .
266
267         STD_EXCEPTION_PSERIES(0xd00, single_step)
268         STD_EXCEPTION_PSERIES(0xe00, trap_0e)
269
270         /* We need to deal with the Altivec unavailable exception
271          * here which is at 0xf20, thus in the middle of the
272          * prolog code of the PerformanceMonitor one. A little
273          * trickery is thus necessary
274          */
275         . = 0xf00
276         b       performance_monitor_pSeries
277
278         . = 0xf20
279         b       altivec_unavailable_pSeries
280
281 #ifdef CONFIG_CBE_RAS
282         HSTD_EXCEPTION_PSERIES(0x1200, cbe_system_error)
283 #endif /* CONFIG_CBE_RAS */
284         STD_EXCEPTION_PSERIES(0x1300, instruction_breakpoint)
285 #ifdef CONFIG_CBE_RAS
286         HSTD_EXCEPTION_PSERIES(0x1600, cbe_maintenance)
287 #endif /* CONFIG_CBE_RAS */
288         STD_EXCEPTION_PSERIES(0x1700, altivec_assist)
289 #ifdef CONFIG_CBE_RAS
290         HSTD_EXCEPTION_PSERIES(0x1800, cbe_thermal)
291 #endif /* CONFIG_CBE_RAS */
292
293         . = 0x3000
294
295 /*** pSeries interrupt support ***/
296
297         /* moved from 0xf00 */
298         STD_EXCEPTION_PSERIES(., performance_monitor)
299         STD_EXCEPTION_PSERIES(., altivec_unavailable)
300
301 /*
302  * An interrupt came in while soft-disabled; clear EE in SRR1,
303  * clear paca->hard_enabled and return.
304  */
305 masked_interrupt:
306         stb     r10,PACAHARDIRQEN(r13)
307         mtcrf   0x80,r9
308         ld      r9,PACA_EXGEN+EX_R9(r13)
309         mfspr   r10,SPRN_SRR1
310         rldicl  r10,r10,48,1            /* clear MSR_EE */
311         rotldi  r10,r10,16
312         mtspr   SPRN_SRR1,r10
313         ld      r10,PACA_EXGEN+EX_R10(r13)
314         mfspr   r13,SPRN_SPRG1
315         rfid
316         b       .
317
318         .align  7
319 do_stab_bolted_pSeries:
320         mtcrf   0x80,r12
321         mfspr   r12,SPRN_SPRG2
322         EXCEPTION_PROLOG_PSERIES(PACA_EXSLB, .do_stab_bolted)
323
324 /*
325  * We have some room here  we use that to put
326  * the peries slb miss user trampoline code so it's reasonably
327  * away from slb_miss_user_common to avoid problems with rfid
328  *
329  * This is used for when the SLB miss handler has to go virtual,
330  * which doesn't happen for now anymore but will once we re-implement
331  * dynamic VSIDs for shared page tables
332  */
333 #ifdef __DISABLED__
334 slb_miss_user_pseries:
335         std     r10,PACA_EXGEN+EX_R10(r13)
336         std     r11,PACA_EXGEN+EX_R11(r13)
337         std     r12,PACA_EXGEN+EX_R12(r13)
338         mfspr   r10,SPRG1
339         ld      r11,PACA_EXSLB+EX_R9(r13)
340         ld      r12,PACA_EXSLB+EX_R3(r13)
341         std     r10,PACA_EXGEN+EX_R13(r13)
342         std     r11,PACA_EXGEN+EX_R9(r13)
343         std     r12,PACA_EXGEN+EX_R3(r13)
344         clrrdi  r12,r13,32
345         mfmsr   r10
346         mfspr   r11,SRR0                        /* save SRR0 */
347         ori     r12,r12,slb_miss_user_common@l  /* virt addr of handler */
348         ori     r10,r10,MSR_IR|MSR_DR|MSR_RI
349         mtspr   SRR0,r12
350         mfspr   r12,SRR1                        /* and SRR1 */
351         mtspr   SRR1,r10
352         rfid
353         b       .                               /* prevent spec. execution */
354 #endif /* __DISABLED__ */
355
356 #ifdef CONFIG_PPC_PSERIES
357 /*
358  * Vectors for the FWNMI option.  Share common code.
359  */
360         .globl system_reset_fwnmi
361       .align 7
362 system_reset_fwnmi:
363         HMT_MEDIUM
364         mtspr   SPRN_SPRG1,r13          /* save r13 */
365         EXCEPTION_PROLOG_PSERIES_FORCE_64BIT(PACA_EXGEN, system_reset_common)
366
367         .globl machine_check_fwnmi
368       .align 7
369 machine_check_fwnmi:
370         HMT_MEDIUM
371         mtspr   SPRN_SPRG1,r13          /* save r13 */
372         EXCEPTION_PROLOG_PSERIES_FORCE_64BIT(PACA_EXMC, machine_check_common)
373
374 #endif /* CONFIG_PPC_PSERIES */
375
376 /*** Common interrupt handlers ***/
377
378         STD_EXCEPTION_COMMON(0x100, system_reset, .system_reset_exception)
379
380         /*
381          * Machine check is different because we use a different
382          * save area: PACA_EXMC instead of PACA_EXGEN.
383          */
384         .align  7
385         .globl machine_check_common
386 machine_check_common:
387         EXCEPTION_PROLOG_COMMON(0x200, PACA_EXMC)
388         FINISH_NAP
389         DISABLE_INTS
390         bl      .save_nvgprs
391         addi    r3,r1,STACK_FRAME_OVERHEAD
392         bl      .machine_check_exception
393         b       .ret_from_except
394
395         STD_EXCEPTION_COMMON_LITE(0x900, decrementer, .timer_interrupt)
396         STD_EXCEPTION_COMMON(0xa00, trap_0a, .unknown_exception)
397         STD_EXCEPTION_COMMON(0xb00, trap_0b, .unknown_exception)
398         STD_EXCEPTION_COMMON(0xd00, single_step, .single_step_exception)
399         STD_EXCEPTION_COMMON(0xe00, trap_0e, .unknown_exception)
400         STD_EXCEPTION_COMMON_IDLE(0xf00, performance_monitor, .performance_monitor_exception)
401         STD_EXCEPTION_COMMON(0x1300, instruction_breakpoint, .instruction_breakpoint_exception)
402 #ifdef CONFIG_ALTIVEC
403         STD_EXCEPTION_COMMON(0x1700, altivec_assist, .altivec_assist_exception)
404 #else
405         STD_EXCEPTION_COMMON(0x1700, altivec_assist, .unknown_exception)
406 #endif
407 #ifdef CONFIG_CBE_RAS
408         STD_EXCEPTION_COMMON(0x1200, cbe_system_error, .cbe_system_error_exception)
409         STD_EXCEPTION_COMMON(0x1600, cbe_maintenance, .cbe_maintenance_exception)
410         STD_EXCEPTION_COMMON(0x1800, cbe_thermal, .cbe_thermal_exception)
411 #endif /* CONFIG_CBE_RAS */
412
413 /*
414  * Here we have detected that the kernel stack pointer is bad.
415  * R9 contains the saved CR, r13 points to the paca,
416  * r10 contains the (bad) kernel stack pointer,
417  * r11 and r12 contain the saved SRR0 and SRR1.
418  * We switch to using an emergency stack, save the registers there,
419  * and call kernel_bad_stack(), which panics.
420  */
421 bad_stack:
422         ld      r1,PACAEMERGSP(r13)
423         subi    r1,r1,64+INT_FRAME_SIZE
424         std     r9,_CCR(r1)
425         std     r10,GPR1(r1)
426         std     r11,_NIP(r1)
427         std     r12,_MSR(r1)
428         mfspr   r11,SPRN_DAR
429         mfspr   r12,SPRN_DSISR
430         std     r11,_DAR(r1)
431         std     r12,_DSISR(r1)
432         mflr    r10
433         mfctr   r11
434         mfxer   r12
435         std     r10,_LINK(r1)
436         std     r11,_CTR(r1)
437         std     r12,_XER(r1)
438         SAVE_GPR(0,r1)
439         SAVE_GPR(2,r1)
440         SAVE_4GPRS(3,r1)
441         SAVE_2GPRS(7,r1)
442         SAVE_10GPRS(12,r1)
443         SAVE_10GPRS(22,r1)
444         lhz     r12,PACA_TRAP_SAVE(r13)
445         std     r12,_TRAP(r1)
446         addi    r11,r1,INT_FRAME_SIZE
447         std     r11,0(r1)
448         li      r12,0
449         std     r12,0(r11)
450         ld      r2,PACATOC(r13)
451 1:      addi    r3,r1,STACK_FRAME_OVERHEAD
452         bl      .kernel_bad_stack
453         b       1b
454
455 /*
456  * Return from an exception with minimal checks.
457  * The caller is assumed to have done EXCEPTION_PROLOG_COMMON.
458  * If interrupts have been enabled, or anything has been
459  * done that might have changed the scheduling status of
460  * any task or sent any task a signal, you should use
461  * ret_from_except or ret_from_except_lite instead of this.
462  */
463 fast_exc_return_irq:                    /* restores irq state too */
464         ld      r3,SOFTE(r1)
465         TRACE_AND_RESTORE_IRQ(r3);
466         ld      r12,_MSR(r1)
467         rldicl  r4,r12,49,63            /* get MSR_EE to LSB */
468         stb     r4,PACAHARDIRQEN(r13)   /* restore paca->hard_enabled */
469         b       1f
470
471         .globl  fast_exception_return
472 fast_exception_return:
473         ld      r12,_MSR(r1)
474 1:      ld      r11,_NIP(r1)
475         andi.   r3,r12,MSR_RI           /* check if RI is set */
476         beq-    unrecov_fer
477
478 #ifdef CONFIG_VIRT_CPU_ACCOUNTING
479         andi.   r3,r12,MSR_PR
480         beq     2f
481         ACCOUNT_CPU_USER_EXIT(r3, r4)
482 2:
483 #endif
484
485         ld      r3,_CCR(r1)
486         ld      r4,_LINK(r1)
487         ld      r5,_CTR(r1)
488         ld      r6,_XER(r1)
489         mtcr    r3
490         mtlr    r4
491         mtctr   r5
492         mtxer   r6
493         REST_GPR(0, r1)
494         REST_8GPRS(2, r1)
495
496         mfmsr   r10
497         rldicl  r10,r10,48,1            /* clear EE */
498         rldicr  r10,r10,16,61           /* clear RI (LE is 0 already) */
499         mtmsrd  r10,1
500
501         mtspr   SPRN_SRR1,r12
502         mtspr   SPRN_SRR0,r11
503         REST_4GPRS(10, r1)
504         ld      r1,GPR1(r1)
505         rfid
506         b       .       /* prevent speculative execution */
507
508 unrecov_fer:
509         bl      .save_nvgprs
510 1:      addi    r3,r1,STACK_FRAME_OVERHEAD
511         bl      .unrecoverable_exception
512         b       1b
513
514 /*
515  * Here r13 points to the paca, r9 contains the saved CR,
516  * SRR0 and SRR1 are saved in r11 and r12,
517  * r9 - r13 are saved in paca->exgen.
518  */
519         .align  7
520         .globl data_access_common
521 data_access_common:
522         mfspr   r10,SPRN_DAR
523         std     r10,PACA_EXGEN+EX_DAR(r13)
524         mfspr   r10,SPRN_DSISR
525         stw     r10,PACA_EXGEN+EX_DSISR(r13)
526         EXCEPTION_PROLOG_COMMON(0x300, PACA_EXGEN)
527         ld      r3,PACA_EXGEN+EX_DAR(r13)
528         lwz     r4,PACA_EXGEN+EX_DSISR(r13)
529         li      r5,0x300
530         b       .do_hash_page           /* Try to handle as hpte fault */
531
532         .align  7
533         .globl instruction_access_common
534 instruction_access_common:
535         EXCEPTION_PROLOG_COMMON(0x400, PACA_EXGEN)
536         ld      r3,_NIP(r1)
537         andis.  r4,r12,0x5820
538         li      r5,0x400
539         b       .do_hash_page           /* Try to handle as hpte fault */
540
541 /*
542  * Here is the common SLB miss user that is used when going to virtual
543  * mode for SLB misses, that is currently not used
544  */
545 #ifdef __DISABLED__
546         .align  7
547         .globl  slb_miss_user_common
548 slb_miss_user_common:
549         mflr    r10
550         std     r3,PACA_EXGEN+EX_DAR(r13)
551         stw     r9,PACA_EXGEN+EX_CCR(r13)
552         std     r10,PACA_EXGEN+EX_LR(r13)
553         std     r11,PACA_EXGEN+EX_SRR0(r13)
554         bl      .slb_allocate_user
555
556         ld      r10,PACA_EXGEN+EX_LR(r13)
557         ld      r3,PACA_EXGEN+EX_R3(r13)
558         lwz     r9,PACA_EXGEN+EX_CCR(r13)
559         ld      r11,PACA_EXGEN+EX_SRR0(r13)
560         mtlr    r10
561         beq-    slb_miss_fault
562
563         andi.   r10,r12,MSR_RI          /* check for unrecoverable exception */
564         beq-    unrecov_user_slb
565         mfmsr   r10
566
567 .machine push
568 .machine "power4"
569         mtcrf   0x80,r9
570 .machine pop
571
572         clrrdi  r10,r10,2               /* clear RI before setting SRR0/1 */
573         mtmsrd  r10,1
574
575         mtspr   SRR0,r11
576         mtspr   SRR1,r12
577
578         ld      r9,PACA_EXGEN+EX_R9(r13)
579         ld      r10,PACA_EXGEN+EX_R10(r13)
580         ld      r11,PACA_EXGEN+EX_R11(r13)
581         ld      r12,PACA_EXGEN+EX_R12(r13)
582         ld      r13,PACA_EXGEN+EX_R13(r13)
583         rfid
584         b       .
585
586 slb_miss_fault:
587         EXCEPTION_PROLOG_COMMON(0x380, PACA_EXGEN)
588         ld      r4,PACA_EXGEN+EX_DAR(r13)
589         li      r5,0
590         std     r4,_DAR(r1)
591         std     r5,_DSISR(r1)
592         b       handle_page_fault
593
594 unrecov_user_slb:
595         EXCEPTION_PROLOG_COMMON(0x4200, PACA_EXGEN)
596         DISABLE_INTS
597         bl      .save_nvgprs
598 1:      addi    r3,r1,STACK_FRAME_OVERHEAD
599         bl      .unrecoverable_exception
600         b       1b
601
602 #endif /* __DISABLED__ */
603
604
605 /*
606  * r13 points to the PACA, r9 contains the saved CR,
607  * r12 contain the saved SRR1, SRR0 is still ready for return
608  * r3 has the faulting address
609  * r9 - r13 are saved in paca->exslb.
610  * r3 is saved in paca->slb_r3
611  * We assume we aren't going to take any exceptions during this procedure.
612  */
613 _GLOBAL(slb_miss_realmode)
614         mflr    r10
615
616         stw     r9,PACA_EXSLB+EX_CCR(r13)       /* save CR in exc. frame */
617         std     r10,PACA_EXSLB+EX_LR(r13)       /* save LR */
618
619         bl      .slb_allocate_realmode
620
621         /* All done -- return from exception. */
622
623         ld      r10,PACA_EXSLB+EX_LR(r13)
624         ld      r3,PACA_EXSLB+EX_R3(r13)
625         lwz     r9,PACA_EXSLB+EX_CCR(r13)       /* get saved CR */
626 #ifdef CONFIG_PPC_ISERIES
627 BEGIN_FW_FTR_SECTION
628         ld      r11,PACALPPACAPTR(r13)
629         ld      r11,LPPACASRR0(r11)             /* get SRR0 value */
630 END_FW_FTR_SECTION_IFSET(FW_FEATURE_ISERIES)
631 #endif /* CONFIG_PPC_ISERIES */
632
633         mtlr    r10
634
635         andi.   r10,r12,MSR_RI  /* check for unrecoverable exception */
636         beq-    2f
637
638 .machine        push
639 .machine        "power4"
640         mtcrf   0x80,r9
641         mtcrf   0x01,r9         /* slb_allocate uses cr0 and cr7 */
642 .machine        pop
643
644 #ifdef CONFIG_PPC_ISERIES
645 BEGIN_FW_FTR_SECTION
646         mtspr   SPRN_SRR0,r11
647         mtspr   SPRN_SRR1,r12
648 END_FW_FTR_SECTION_IFSET(FW_FEATURE_ISERIES)
649 #endif /* CONFIG_PPC_ISERIES */
650         ld      r9,PACA_EXSLB+EX_R9(r13)
651         ld      r10,PACA_EXSLB+EX_R10(r13)
652         ld      r11,PACA_EXSLB+EX_R11(r13)
653         ld      r12,PACA_EXSLB+EX_R12(r13)
654         ld      r13,PACA_EXSLB+EX_R13(r13)
655         rfid
656         b       .       /* prevent speculative execution */
657
658 2:
659 #ifdef CONFIG_PPC_ISERIES
660 BEGIN_FW_FTR_SECTION
661         b       unrecov_slb
662 END_FW_FTR_SECTION_IFSET(FW_FEATURE_ISERIES)
663 #endif /* CONFIG_PPC_ISERIES */
664         mfspr   r11,SPRN_SRR0
665         clrrdi  r10,r13,32
666         LOAD_HANDLER(r10,unrecov_slb)
667         mtspr   SPRN_SRR0,r10
668         mfmsr   r10
669         ori     r10,r10,MSR_IR|MSR_DR|MSR_RI
670         mtspr   SPRN_SRR1,r10
671         rfid
672         b       .
673
674 unrecov_slb:
675         EXCEPTION_PROLOG_COMMON(0x4100, PACA_EXSLB)
676         DISABLE_INTS
677         bl      .save_nvgprs
678 1:      addi    r3,r1,STACK_FRAME_OVERHEAD
679         bl      .unrecoverable_exception
680         b       1b
681
682         .align  7
683         .globl hardware_interrupt_common
684         .globl hardware_interrupt_entry
685 hardware_interrupt_common:
686         EXCEPTION_PROLOG_COMMON(0x500, PACA_EXGEN)
687         FINISH_NAP
688 hardware_interrupt_entry:
689         DISABLE_INTS
690 BEGIN_FTR_SECTION
691         bl      .ppc64_runlatch_on
692 END_FTR_SECTION_IFSET(CPU_FTR_CTRL)
693         addi    r3,r1,STACK_FRAME_OVERHEAD
694         bl      .do_IRQ
695         b       .ret_from_except_lite
696
697 #ifdef CONFIG_PPC_970_NAP
698 power4_fixup_nap:
699         andc    r9,r9,r10
700         std     r9,TI_LOCAL_FLAGS(r11)
701         ld      r10,_LINK(r1)           /* make idle task do the */
702         std     r10,_NIP(r1)            /* equivalent of a blr */
703         blr
704 #endif
705
706         .align  7
707         .globl alignment_common
708 alignment_common:
709         mfspr   r10,SPRN_DAR
710         std     r10,PACA_EXGEN+EX_DAR(r13)
711         mfspr   r10,SPRN_DSISR
712         stw     r10,PACA_EXGEN+EX_DSISR(r13)
713         EXCEPTION_PROLOG_COMMON(0x600, PACA_EXGEN)
714         ld      r3,PACA_EXGEN+EX_DAR(r13)
715         lwz     r4,PACA_EXGEN+EX_DSISR(r13)
716         std     r3,_DAR(r1)
717         std     r4,_DSISR(r1)
718         bl      .save_nvgprs
719         addi    r3,r1,STACK_FRAME_OVERHEAD
720         ENABLE_INTS
721         bl      .alignment_exception
722         b       .ret_from_except
723
724         .align  7
725         .globl program_check_common
726 program_check_common:
727         EXCEPTION_PROLOG_COMMON(0x700, PACA_EXGEN)
728         bl      .save_nvgprs
729         addi    r3,r1,STACK_FRAME_OVERHEAD
730         ENABLE_INTS
731         bl      .program_check_exception
732         b       .ret_from_except
733
734         .align  7
735         .globl fp_unavailable_common
736 fp_unavailable_common:
737         EXCEPTION_PROLOG_COMMON(0x800, PACA_EXGEN)
738         bne     1f                      /* if from user, just load it up */
739         bl      .save_nvgprs
740         addi    r3,r1,STACK_FRAME_OVERHEAD
741         ENABLE_INTS
742         bl      .kernel_fp_unavailable_exception
743         BUG_OPCODE
744 1:      b       .load_up_fpu
745
746         .align  7
747         .globl altivec_unavailable_common
748 altivec_unavailable_common:
749         EXCEPTION_PROLOG_COMMON(0xf20, PACA_EXGEN)
750 #ifdef CONFIG_ALTIVEC
751 BEGIN_FTR_SECTION
752         bne     .load_up_altivec        /* if from user, just load it up */
753 END_FTR_SECTION_IFSET(CPU_FTR_ALTIVEC)
754 #endif
755         bl      .save_nvgprs
756         addi    r3,r1,STACK_FRAME_OVERHEAD
757         ENABLE_INTS
758         bl      .altivec_unavailable_exception
759         b       .ret_from_except
760
761 #ifdef CONFIG_ALTIVEC
762 /*
763  * load_up_altivec(unused, unused, tsk)
764  * Disable VMX for the task which had it previously,
765  * and save its vector registers in its thread_struct.
766  * Enables the VMX for use in the kernel on return.
767  * On SMP we know the VMX is free, since we give it up every
768  * switch (ie, no lazy save of the vector registers).
769  * On entry: r13 == 'current' && last_task_used_altivec != 'current'
770  */
771 _STATIC(load_up_altivec)
772         mfmsr   r5                      /* grab the current MSR */
773         oris    r5,r5,MSR_VEC@h
774         mtmsrd  r5                      /* enable use of VMX now */
775         isync
776
777 /*
778  * For SMP, we don't do lazy VMX switching because it just gets too
779  * horrendously complex, especially when a task switches from one CPU
780  * to another.  Instead we call giveup_altvec in switch_to.
781  * VRSAVE isn't dealt with here, that is done in the normal context
782  * switch code. Note that we could rely on vrsave value to eventually
783  * avoid saving all of the VREGs here...
784  */
785 #ifndef CONFIG_SMP
786         ld      r3,last_task_used_altivec@got(r2)
787         ld      r4,0(r3)
788         cmpdi   0,r4,0
789         beq     1f
790         /* Save VMX state to last_task_used_altivec's THREAD struct */
791         addi    r4,r4,THREAD
792         SAVE_32VRS(0,r5,r4)
793         mfvscr  vr0
794         li      r10,THREAD_VSCR
795         stvx    vr0,r10,r4
796         /* Disable VMX for last_task_used_altivec */
797         ld      r5,PT_REGS(r4)
798         ld      r4,_MSR-STACK_FRAME_OVERHEAD(r5)
799         lis     r6,MSR_VEC@h
800         andc    r4,r4,r6
801         std     r4,_MSR-STACK_FRAME_OVERHEAD(r5)
802 1:
803 #endif /* CONFIG_SMP */
804         /* Hack: if we get an altivec unavailable trap with VRSAVE
805          * set to all zeros, we assume this is a broken application
806          * that fails to set it properly, and thus we switch it to
807          * all 1's
808          */
809         mfspr   r4,SPRN_VRSAVE
810         cmpdi   0,r4,0
811         bne+    1f
812         li      r4,-1
813         mtspr   SPRN_VRSAVE,r4
814 1:
815         /* enable use of VMX after return */
816         ld      r4,PACACURRENT(r13)
817         addi    r5,r4,THREAD            /* Get THREAD */
818         oris    r12,r12,MSR_VEC@h
819         std     r12,_MSR(r1)
820         li      r4,1
821         li      r10,THREAD_VSCR
822         stw     r4,THREAD_USED_VR(r5)
823         lvx     vr0,r10,r5
824         mtvscr  vr0
825         REST_32VRS(0,r4,r5)
826 #ifndef CONFIG_SMP
827         /* Update last_task_used_math to 'current' */
828         subi    r4,r5,THREAD            /* Back to 'current' */
829         std     r4,0(r3)
830 #endif /* CONFIG_SMP */
831         /* restore registers and return */
832         b       fast_exception_return
833 #endif /* CONFIG_ALTIVEC */
834
835 /*
836  * Hash table stuff
837  */
838         .align  7
839 _STATIC(do_hash_page)
840         std     r3,_DAR(r1)
841         std     r4,_DSISR(r1)
842
843         andis.  r0,r4,0xa450            /* weird error? */
844         bne-    handle_page_fault       /* if not, try to insert a HPTE */
845 BEGIN_FTR_SECTION
846         andis.  r0,r4,0x0020            /* Is it a segment table fault? */
847         bne-    do_ste_alloc            /* If so handle it */
848 END_FTR_SECTION_IFCLR(CPU_FTR_SLB)
849
850         /*
851          * On iSeries, we soft-disable interrupts here, then
852          * hard-enable interrupts so that the hash_page code can spin on
853          * the hash_table_lock without problems on a shared processor.
854          */
855         DISABLE_INTS
856
857         /*
858          * Currently, trace_hardirqs_off() will be called by DISABLE_INTS
859          * and will clobber volatile registers when irq tracing is enabled
860          * so we need to reload them. It may be possible to be smarter here
861          * and move the irq tracing elsewhere but let's keep it simple for
862          * now
863          */
864 #ifdef CONFIG_TRACE_IRQFLAGS
865         ld      r3,_DAR(r1)
866         ld      r4,_DSISR(r1)
867         ld      r5,_TRAP(r1)
868         ld      r12,_MSR(r1)
869         clrrdi  r5,r5,4
870 #endif /* CONFIG_TRACE_IRQFLAGS */
871         /*
872          * We need to set the _PAGE_USER bit if MSR_PR is set or if we are
873          * accessing a userspace segment (even from the kernel). We assume
874          * kernel addresses always have the high bit set.
875          */
876         rlwinm  r4,r4,32-25+9,31-9,31-9 /* DSISR_STORE -> _PAGE_RW */
877         rotldi  r0,r3,15                /* Move high bit into MSR_PR posn */
878         orc     r0,r12,r0               /* MSR_PR | ~high_bit */
879         rlwimi  r4,r0,32-13,30,30       /* becomes _PAGE_USER access bit */
880         ori     r4,r4,1                 /* add _PAGE_PRESENT */
881         rlwimi  r4,r5,22+2,31-2,31-2    /* Set _PAGE_EXEC if trap is 0x400 */
882
883         /*
884          * r3 contains the faulting address
885          * r4 contains the required access permissions
886          * r5 contains the trap number
887          *
888          * at return r3 = 0 for success
889          */
890         bl      .hash_page              /* build HPTE if possible */
891         cmpdi   r3,0                    /* see if hash_page succeeded */
892
893 BEGIN_FW_FTR_SECTION
894         /*
895          * If we had interrupts soft-enabled at the point where the
896          * DSI/ISI occurred, and an interrupt came in during hash_page,
897          * handle it now.
898          * We jump to ret_from_except_lite rather than fast_exception_return
899          * because ret_from_except_lite will check for and handle pending
900          * interrupts if necessary.
901          */
902         beq     13f
903 END_FW_FTR_SECTION_IFSET(FW_FEATURE_ISERIES)
904
905 BEGIN_FW_FTR_SECTION
906         /*
907          * Here we have interrupts hard-disabled, so it is sufficient
908          * to restore paca->{soft,hard}_enable and get out.
909          */
910         beq     fast_exc_return_irq     /* Return from exception on success */
911 END_FW_FTR_SECTION_IFCLR(FW_FEATURE_ISERIES)
912
913         /* For a hash failure, we don't bother re-enabling interrupts */
914         ble-    12f
915
916         /*
917          * hash_page couldn't handle it, set soft interrupt enable back
918          * to what it was before the trap.  Note that .raw_local_irq_restore
919          * handles any interrupts pending at this point.
920          */
921         ld      r3,SOFTE(r1)
922         TRACE_AND_RESTORE_IRQ_PARTIAL(r3, 11f)
923         bl      .raw_local_irq_restore
924         b       11f
925
926 /* Here we have a page fault that hash_page can't handle. */
927 handle_page_fault:
928         ENABLE_INTS
929 11:     ld      r4,_DAR(r1)
930         ld      r5,_DSISR(r1)
931         addi    r3,r1,STACK_FRAME_OVERHEAD
932         bl      .do_page_fault
933         cmpdi   r3,0
934         beq+    13f
935         bl      .save_nvgprs
936         mr      r5,r3
937         addi    r3,r1,STACK_FRAME_OVERHEAD
938         lwz     r4,_DAR(r1)
939         bl      .bad_page_fault
940         b       .ret_from_except
941
942 13:     b       .ret_from_except_lite
943
944 /* We have a page fault that hash_page could handle but HV refused
945  * the PTE insertion
946  */
947 12:     bl      .save_nvgprs
948         mr      r5,r3
949         addi    r3,r1,STACK_FRAME_OVERHEAD
950         ld      r4,_DAR(r1)
951         bl      .low_hash_fault
952         b       .ret_from_except
953
954         /* here we have a segment miss */
955 do_ste_alloc:
956         bl      .ste_allocate           /* try to insert stab entry */
957         cmpdi   r3,0
958         bne-    handle_page_fault
959         b       fast_exception_return
960
961 /*
962  * r13 points to the PACA, r9 contains the saved CR,
963  * r11 and r12 contain the saved SRR0 and SRR1.
964  * r9 - r13 are saved in paca->exslb.
965  * We assume we aren't going to take any exceptions during this procedure.
966  * We assume (DAR >> 60) == 0xc.
967  */
968         .align  7
969 _GLOBAL(do_stab_bolted)
970         stw     r9,PACA_EXSLB+EX_CCR(r13)       /* save CR in exc. frame */
971         std     r11,PACA_EXSLB+EX_SRR0(r13)     /* save SRR0 in exc. frame */
972
973         /* Hash to the primary group */
974         ld      r10,PACASTABVIRT(r13)
975         mfspr   r11,SPRN_DAR
976         srdi    r11,r11,28
977         rldimi  r10,r11,7,52    /* r10 = first ste of the group */
978
979         /* Calculate VSID */
980         /* This is a kernel address, so protovsid = ESID */
981         ASM_VSID_SCRAMBLE(r11, r9, 256M)
982         rldic   r9,r11,12,16    /* r9 = vsid << 12 */
983
984         /* Search the primary group for a free entry */
985 1:      ld      r11,0(r10)      /* Test valid bit of the current ste    */
986         andi.   r11,r11,0x80
987         beq     2f
988         addi    r10,r10,16
989         andi.   r11,r10,0x70
990         bne     1b
991
992         /* Stick for only searching the primary group for now.          */
993         /* At least for now, we use a very simple random castout scheme */
994         /* Use the TB as a random number ;  OR in 1 to avoid entry 0    */
995         mftb    r11
996         rldic   r11,r11,4,57    /* r11 = (r11 << 4) & 0x70 */
997         ori     r11,r11,0x10
998
999         /* r10 currently points to an ste one past the group of interest */
1000         /* make it point to the randomly selected entry                 */
1001         subi    r10,r10,128
1002         or      r10,r10,r11     /* r10 is the entry to invalidate       */
1003
1004         isync                   /* mark the entry invalid               */
1005         ld      r11,0(r10)
1006         rldicl  r11,r11,56,1    /* clear the valid bit */
1007         rotldi  r11,r11,8
1008         std     r11,0(r10)
1009         sync
1010
1011         clrrdi  r11,r11,28      /* Get the esid part of the ste         */
1012         slbie   r11
1013
1014 2:      std     r9,8(r10)       /* Store the vsid part of the ste       */
1015         eieio
1016
1017         mfspr   r11,SPRN_DAR            /* Get the new esid                     */
1018         clrrdi  r11,r11,28      /* Permits a full 32b of ESID           */
1019         ori     r11,r11,0x90    /* Turn on valid and kp                 */
1020         std     r11,0(r10)      /* Put new entry back into the stab     */
1021
1022         sync
1023
1024         /* All done -- return from exception. */
1025         lwz     r9,PACA_EXSLB+EX_CCR(r13)       /* get saved CR */
1026         ld      r11,PACA_EXSLB+EX_SRR0(r13)     /* get saved SRR0 */
1027
1028         andi.   r10,r12,MSR_RI
1029         beq-    unrecov_slb
1030
1031         mtcrf   0x80,r9                 /* restore CR */
1032
1033         mfmsr   r10
1034         clrrdi  r10,r10,2
1035         mtmsrd  r10,1
1036
1037         mtspr   SPRN_SRR0,r11
1038         mtspr   SPRN_SRR1,r12
1039         ld      r9,PACA_EXSLB+EX_R9(r13)
1040         ld      r10,PACA_EXSLB+EX_R10(r13)
1041         ld      r11,PACA_EXSLB+EX_R11(r13)
1042         ld      r12,PACA_EXSLB+EX_R12(r13)
1043         ld      r13,PACA_EXSLB+EX_R13(r13)
1044         rfid
1045         b       .       /* prevent speculative execution */
1046
1047 /*
1048  * Space for CPU0's segment table.
1049  *
1050  * On iSeries, the hypervisor must fill in at least one entry before
1051  * we get control (with relocate on).  The address is given to the hv
1052  * as a page number (see xLparMap below), so this must be at a
1053  * fixed address (the linker can't compute (u64)&initial_stab >>
1054  * PAGE_SHIFT).
1055  */
1056         . = STAB0_OFFSET        /* 0x6000 */
1057         .globl initial_stab
1058 initial_stab:
1059         .space  4096
1060
1061 #ifdef CONFIG_PPC_PSERIES
1062 /*
1063  * Data area reserved for FWNMI option.
1064  * This address (0x7000) is fixed by the RPA.
1065  */
1066         .= 0x7000
1067         .globl fwnmi_data_area
1068 fwnmi_data_area:
1069 #endif /* CONFIG_PPC_PSERIES */
1070
1071         /* iSeries does not use the FWNMI stuff, so it is safe to put
1072          * this here, even if we later allow kernels that will boot on
1073          * both pSeries and iSeries */
1074 #ifdef CONFIG_PPC_ISERIES
1075         . = LPARMAP_PHYS
1076         .globl xLparMap
1077 xLparMap:
1078         .quad   HvEsidsToMap            /* xNumberEsids */
1079         .quad   HvRangesToMap           /* xNumberRanges */
1080         .quad   STAB0_PAGE              /* xSegmentTableOffs */
1081         .zero   40                      /* xRsvd */
1082         /* xEsids (HvEsidsToMap entries of 2 quads) */
1083         .quad   PAGE_OFFSET_ESID        /* xKernelEsid */
1084         .quad   PAGE_OFFSET_VSID        /* xKernelVsid */
1085         .quad   VMALLOC_START_ESID      /* xKernelEsid */
1086         .quad   VMALLOC_START_VSID      /* xKernelVsid */
1087         /* xRanges (HvRangesToMap entries of 3 quads) */
1088         .quad   HvPagesToMap            /* xPages */
1089         .quad   0                       /* xOffset */
1090         .quad   PAGE_OFFSET_VSID << (SID_SHIFT - HW_PAGE_SHIFT) /* xVPN */
1091
1092 #endif /* CONFIG_PPC_ISERIES */
1093
1094 #ifdef CONFIG_PPC_PSERIES
1095         . = 0x8000
1096 #endif /* CONFIG_PPC_PSERIES */
1097
1098 /*
1099  * On pSeries and most other platforms, secondary processors spin
1100  * in the following code.
1101  * At entry, r3 = this processor's number (physical cpu id)
1102  */
1103 _GLOBAL(generic_secondary_smp_init)
1104         mr      r24,r3
1105         
1106         /* turn on 64-bit mode */
1107         bl      .enable_64b_mode
1108
1109         /* Set up a paca value for this processor. Since we have the
1110          * physical cpu id in r24, we need to search the pacas to find
1111          * which logical id maps to our physical one.
1112          */
1113         LOAD_REG_IMMEDIATE(r13, paca)   /* Get base vaddr of paca array  */
1114         li      r5,0                    /* logical cpu id                */
1115 1:      lhz     r6,PACAHWCPUID(r13)     /* Load HW procid from paca      */
1116         cmpw    r6,r24                  /* Compare to our id             */
1117         beq     2f
1118         addi    r13,r13,PACA_SIZE       /* Loop to next PACA on miss     */
1119         addi    r5,r5,1
1120         cmpwi   r5,NR_CPUS
1121         blt     1b
1122
1123         mr      r3,r24                  /* not found, copy phys to r3    */
1124         b       .kexec_wait             /* next kernel might do better   */
1125
1126 2:      mtspr   SPRN_SPRG3,r13          /* Save vaddr of paca in SPRG3   */
1127         /* From now on, r24 is expected to be logical cpuid */
1128         mr      r24,r5
1129 3:      HMT_LOW
1130         lbz     r23,PACAPROCSTART(r13)  /* Test if this processor should */
1131                                         /* start.                        */
1132         sync
1133
1134 #ifndef CONFIG_SMP
1135         b       3b                      /* Never go on non-SMP           */
1136 #else
1137         cmpwi   0,r23,0
1138         beq     3b                      /* Loop until told to go         */
1139
1140         /* See if we need to call a cpu state restore handler */
1141         LOAD_REG_IMMEDIATE(r23, cur_cpu_spec)
1142         ld      r23,0(r23)
1143         ld      r23,CPU_SPEC_RESTORE(r23)
1144         cmpdi   0,r23,0
1145         beq     4f
1146         ld      r23,0(r23)
1147         mtctr   r23
1148         bctrl
1149
1150 4:      /* Create a temp kernel stack for use before relocation is on.  */
1151         ld      r1,PACAEMERGSP(r13)
1152         subi    r1,r1,STACK_FRAME_OVERHEAD
1153
1154         b       __secondary_start
1155 #endif
1156
1157 _STATIC(__mmu_off)
1158         mfmsr   r3
1159         andi.   r0,r3,MSR_IR|MSR_DR
1160         beqlr
1161         andc    r3,r3,r0
1162         mtspr   SPRN_SRR0,r4
1163         mtspr   SPRN_SRR1,r3
1164         sync
1165         rfid
1166         b       .       /* prevent speculative execution */
1167
1168
1169 /*
1170  * Here is our main kernel entry point. We support currently 2 kind of entries
1171  * depending on the value of r5.
1172  *
1173  *   r5 != NULL -> OF entry, we go to prom_init, "legacy" parameter content
1174  *                 in r3...r7
1175  *   
1176  *   r5 == NULL -> kexec style entry. r3 is a physical pointer to the
1177  *                 DT block, r4 is a physical pointer to the kernel itself
1178  *
1179  */
1180 _GLOBAL(__start_initialization_multiplatform)
1181         /*
1182          * Are we booted from a PROM Of-type client-interface ?
1183          */
1184         cmpldi  cr0,r5,0
1185         beq     1f
1186         b       .__boot_from_prom               /* yes -> prom */
1187 1:
1188         /* Save parameters */
1189         mr      r31,r3
1190         mr      r30,r4
1191
1192         /* Make sure we are running in 64 bits mode */
1193         bl      .enable_64b_mode
1194
1195         /* Setup some critical 970 SPRs before switching MMU off */
1196         mfspr   r0,SPRN_PVR
1197         srwi    r0,r0,16
1198         cmpwi   r0,0x39         /* 970 */
1199         beq     1f
1200         cmpwi   r0,0x3c         /* 970FX */
1201         beq     1f
1202         cmpwi   r0,0x44         /* 970MP */
1203         beq     1f
1204         cmpwi   r0,0x45         /* 970GX */
1205         bne     2f
1206 1:      bl      .__cpu_preinit_ppc970
1207 2:
1208
1209         /* Switch off MMU if not already */
1210         LOAD_REG_IMMEDIATE(r4, .__after_prom_start - KERNELBASE)
1211         add     r4,r4,r30
1212         bl      .__mmu_off
1213         b       .__after_prom_start
1214
1215 _INIT_STATIC(__boot_from_prom)
1216         /* Save parameters */
1217         mr      r31,r3
1218         mr      r30,r4
1219         mr      r29,r5
1220         mr      r28,r6
1221         mr      r27,r7
1222
1223         /*
1224          * Align the stack to 16-byte boundary
1225          * Depending on the size and layout of the ELF sections in the initial
1226          * boot binary, the stack pointer will be unalignet on PowerMac
1227          */
1228         rldicr  r1,r1,0,59
1229
1230         /* Make sure we are running in 64 bits mode */
1231         bl      .enable_64b_mode
1232
1233         /* put a relocation offset into r3 */
1234         bl      .reloc_offset
1235
1236         LOAD_REG_IMMEDIATE(r2,__toc_start)
1237         addi    r2,r2,0x4000
1238         addi    r2,r2,0x4000
1239
1240         /* Relocate the TOC from a virt addr to a real addr */
1241         add     r2,r2,r3
1242
1243         /* Restore parameters */
1244         mr      r3,r31
1245         mr      r4,r30
1246         mr      r5,r29
1247         mr      r6,r28
1248         mr      r7,r27
1249
1250         /* Do all of the interaction with OF client interface */
1251         bl      .prom_init
1252         /* We never return */
1253         trap
1254
1255 _STATIC(__after_prom_start)
1256
1257 /*
1258  * We need to run with __start at physical address PHYSICAL_START.
1259  * This will leave some code in the first 256B of
1260  * real memory, which are reserved for software use.
1261  * The remainder of the first page is loaded with the fixed
1262  * interrupt vectors.  The next two pages are filled with
1263  * unknown exception placeholders.
1264  *
1265  * Note: This process overwrites the OF exception vectors.
1266  *      r26 == relocation offset
1267  *      r27 == KERNELBASE
1268  */
1269         bl      .reloc_offset
1270         mr      r26,r3
1271         LOAD_REG_IMMEDIATE(r27, KERNELBASE)
1272
1273         LOAD_REG_IMMEDIATE(r3, PHYSICAL_START)  /* target addr */
1274
1275         // XXX FIXME: Use phys returned by OF (r30)
1276         add     r4,r27,r26              /* source addr                   */
1277                                         /* current address of _start     */
1278                                         /*   i.e. where we are running   */
1279                                         /*      the source addr          */
1280
1281         cmpdi   r4,0                    /* In some cases the loader may  */
1282         bne     1f
1283         b       .start_here_multiplatform /* have already put us at zero */
1284                                         /* so we can skip the copy.      */
1285 1:      LOAD_REG_IMMEDIATE(r5,copy_to_here) /* # bytes of memory to copy */
1286         sub     r5,r5,r27
1287
1288         li      r6,0x100                /* Start offset, the first 0x100 */
1289                                         /* bytes were copied earlier.    */
1290
1291         bl      .copy_and_flush         /* copy the first n bytes        */
1292                                         /* this includes the code being  */
1293                                         /* executed here.                */
1294
1295         LOAD_REG_IMMEDIATE(r0, 4f)      /* Jump to the copy of this code */
1296         mtctr   r0                      /* that we just made/relocated   */
1297         bctr
1298
1299 4:      LOAD_REG_IMMEDIATE(r5,klimit)
1300         add     r5,r5,r26
1301         ld      r5,0(r5)                /* get the value of klimit */
1302         sub     r5,r5,r27
1303         bl      .copy_and_flush         /* copy the rest */
1304         b       .start_here_multiplatform
1305
1306 /*
1307  * Copy routine used to copy the kernel to start at physical address 0
1308  * and flush and invalidate the caches as needed.
1309  * r3 = dest addr, r4 = source addr, r5 = copy limit, r6 = start offset
1310  * on exit, r3, r4, r5 are unchanged, r6 is updated to be >= r5.
1311  *
1312  * Note: this routine *only* clobbers r0, r6 and lr
1313  */
1314 _GLOBAL(copy_and_flush)
1315         addi    r5,r5,-8
1316         addi    r6,r6,-8
1317 4:      li      r0,8                    /* Use the smallest common      */
1318                                         /* denominator cache line       */
1319                                         /* size.  This results in       */
1320                                         /* extra cache line flushes     */
1321                                         /* but operation is correct.    */
1322                                         /* Can't get cache line size    */
1323                                         /* from NACA as it is being     */
1324                                         /* moved too.                   */
1325
1326         mtctr   r0                      /* put # words/line in ctr      */
1327 3:      addi    r6,r6,8                 /* copy a cache line            */
1328         ldx     r0,r6,r4
1329         stdx    r0,r6,r3
1330         bdnz    3b
1331         dcbst   r6,r3                   /* write it to memory           */
1332         sync
1333         icbi    r6,r3                   /* flush the icache line        */
1334         cmpld   0,r6,r5
1335         blt     4b
1336         sync
1337         addi    r5,r5,8
1338         addi    r6,r6,8
1339         blr
1340
1341 .align 8
1342 copy_to_here:
1343
1344 #ifdef CONFIG_SMP
1345 #ifdef CONFIG_PPC_PMAC
1346 /*
1347  * On PowerMac, secondary processors starts from the reset vector, which
1348  * is temporarily turned into a call to one of the functions below.
1349  */
1350         .section ".text";
1351         .align 2 ;
1352
1353         .globl  __secondary_start_pmac_0
1354 __secondary_start_pmac_0:
1355         /* NB the entries for cpus 0, 1, 2 must each occupy 8 bytes. */
1356         li      r24,0
1357         b       1f
1358         li      r24,1
1359         b       1f
1360         li      r24,2
1361         b       1f
1362         li      r24,3
1363 1:
1364         
1365 _GLOBAL(pmac_secondary_start)
1366         /* turn on 64-bit mode */
1367         bl      .enable_64b_mode
1368
1369         /* Copy some CPU settings from CPU 0 */
1370         bl      .__restore_cpu_ppc970
1371
1372         /* pSeries do that early though I don't think we really need it */
1373         mfmsr   r3
1374         ori     r3,r3,MSR_RI
1375         mtmsrd  r3                      /* RI on */
1376
1377         /* Set up a paca value for this processor. */
1378         LOAD_REG_IMMEDIATE(r4, paca)    /* Get base vaddr of paca array */
1379         mulli   r13,r24,PACA_SIZE        /* Calculate vaddr of right paca */
1380         add     r13,r13,r4              /* for this processor.          */
1381         mtspr   SPRN_SPRG3,r13           /* Save vaddr of paca in SPRG3 */
1382
1383         /* Create a temp kernel stack for use before relocation is on.  */
1384         ld      r1,PACAEMERGSP(r13)
1385         subi    r1,r1,STACK_FRAME_OVERHEAD
1386
1387         b       __secondary_start
1388
1389 #endif /* CONFIG_PPC_PMAC */
1390
1391 /*
1392  * This function is called after the master CPU has released the
1393  * secondary processors.  The execution environment is relocation off.
1394  * The paca for this processor has the following fields initialized at
1395  * this point:
1396  *   1. Processor number
1397  *   2. Segment table pointer (virtual address)
1398  * On entry the following are set:
1399  *   r1 = stack pointer.  vaddr for iSeries, raddr (temp stack) for pSeries
1400  *   r24   = cpu# (in Linux terms)
1401  *   r13   = paca virtual address
1402  *   SPRG3 = paca virtual address
1403  */
1404         .globl  __secondary_start
1405 __secondary_start:
1406         /* Set thread priority to MEDIUM */
1407         HMT_MEDIUM
1408
1409         /* Load TOC */
1410         ld      r2,PACATOC(r13)
1411
1412         /* Do early setup for that CPU (stab, slb, hash table pointer) */
1413         bl      .early_setup_secondary
1414
1415         /* Initialize the kernel stack.  Just a repeat for iSeries.      */
1416         LOAD_REG_ADDR(r3, current_set)
1417         sldi    r28,r24,3               /* get current_set[cpu#]         */
1418         ldx     r1,r3,r28
1419         addi    r1,r1,THREAD_SIZE-STACK_FRAME_OVERHEAD
1420         std     r1,PACAKSAVE(r13)
1421
1422         /* Clear backchain so we get nice backtraces */
1423         li      r7,0
1424         mtlr    r7
1425
1426         /* enable MMU and jump to start_secondary */
1427         LOAD_REG_ADDR(r3, .start_secondary_prolog)
1428         LOAD_REG_IMMEDIATE(r4, MSR_KERNEL)
1429 #ifdef CONFIG_PPC_ISERIES
1430 BEGIN_FW_FTR_SECTION
1431         ori     r4,r4,MSR_EE
1432         li      r8,1
1433         stb     r8,PACAHARDIRQEN(r13)
1434 END_FW_FTR_SECTION_IFSET(FW_FEATURE_ISERIES)
1435 #endif
1436 BEGIN_FW_FTR_SECTION
1437         stb     r7,PACAHARDIRQEN(r13)
1438 END_FW_FTR_SECTION_IFCLR(FW_FEATURE_ISERIES)
1439         stb     r7,PACASOFTIRQEN(r13)
1440
1441         mtspr   SPRN_SRR0,r3
1442         mtspr   SPRN_SRR1,r4
1443         rfid
1444         b       .       /* prevent speculative execution */
1445
1446 /* 
1447  * Running with relocation on at this point.  All we want to do is
1448  * zero the stack back-chain pointer before going into C code.
1449  */
1450 _GLOBAL(start_secondary_prolog)
1451         li      r3,0
1452         std     r3,0(r1)                /* Zero the stack frame pointer */
1453         bl      .start_secondary
1454         b       .
1455 #endif
1456
1457 /*
1458  * This subroutine clobbers r11 and r12
1459  */
1460 _GLOBAL(enable_64b_mode)
1461         mfmsr   r11                     /* grab the current MSR */
1462         li      r12,1
1463         rldicr  r12,r12,MSR_SF_LG,(63-MSR_SF_LG)
1464         or      r11,r11,r12
1465         li      r12,1
1466         rldicr  r12,r12,MSR_ISF_LG,(63-MSR_ISF_LG)
1467         or      r11,r11,r12
1468         mtmsrd  r11
1469         isync
1470         blr
1471
1472 /*
1473  * This is where the main kernel code starts.
1474  */
1475 _INIT_STATIC(start_here_multiplatform)
1476         /* get a new offset, now that the kernel has moved. */
1477         bl      .reloc_offset
1478         mr      r26,r3
1479
1480         /* Clear out the BSS. It may have been done in prom_init,
1481          * already but that's irrelevant since prom_init will soon
1482          * be detached from the kernel completely. Besides, we need
1483          * to clear it now for kexec-style entry.
1484          */
1485         LOAD_REG_IMMEDIATE(r11,__bss_stop)
1486         LOAD_REG_IMMEDIATE(r8,__bss_start)
1487         sub     r11,r11,r8              /* bss size                     */
1488         addi    r11,r11,7               /* round up to an even double word */
1489         rldicl. r11,r11,61,3            /* shift right by 3             */
1490         beq     4f
1491         addi    r8,r8,-8
1492         li      r0,0
1493         mtctr   r11                     /* zero this many doublewords   */
1494 3:      stdu    r0,8(r8)
1495         bdnz    3b
1496 4:
1497
1498         mfmsr   r6
1499         ori     r6,r6,MSR_RI
1500         mtmsrd  r6                      /* RI on */
1501
1502         /* The following gets the stack and TOC set up with the regs */
1503         /* pointing to the real addr of the kernel stack.  This is   */
1504         /* all done to support the C function call below which sets  */
1505         /* up the htab.  This is done because we have relocated the  */
1506         /* kernel but are still running in real mode. */
1507
1508         LOAD_REG_IMMEDIATE(r3,init_thread_union)
1509         add     r3,r3,r26
1510
1511         /* set up a stack pointer (physical address) */
1512         addi    r1,r3,THREAD_SIZE
1513         li      r0,0
1514         stdu    r0,-STACK_FRAME_OVERHEAD(r1)
1515
1516         /* set up the TOC (physical address) */
1517         LOAD_REG_IMMEDIATE(r2,__toc_start)
1518         addi    r2,r2,0x4000
1519         addi    r2,r2,0x4000
1520         add     r2,r2,r26
1521
1522         /* Do very early kernel initializations, including initial hash table,
1523          * stab and slb setup before we turn on relocation.     */
1524
1525         /* Restore parameters passed from prom_init/kexec */
1526         mr      r3,r31
1527         bl      .early_setup
1528
1529         LOAD_REG_IMMEDIATE(r3, .start_here_common)
1530         LOAD_REG_IMMEDIATE(r4, MSR_KERNEL)
1531         mtspr   SPRN_SRR0,r3
1532         mtspr   SPRN_SRR1,r4
1533         rfid
1534         b       .       /* prevent speculative execution */
1535         
1536         /* This is where all platforms converge execution */
1537 _INIT_GLOBAL(start_here_common)
1538         /* relocation is on at this point */
1539
1540         /* The following code sets up the SP and TOC now that we are */
1541         /* running with translation enabled. */
1542
1543         LOAD_REG_IMMEDIATE(r3,init_thread_union)
1544
1545         /* set up the stack */
1546         addi    r1,r3,THREAD_SIZE
1547         li      r0,0
1548         stdu    r0,-STACK_FRAME_OVERHEAD(r1)
1549
1550         /* Load the TOC */
1551         ld      r2,PACATOC(r13)
1552         std     r1,PACAKSAVE(r13)
1553
1554         bl      .setup_system
1555
1556         /* Load up the kernel context */
1557 5:
1558         li      r5,0
1559         stb     r5,PACASOFTIRQEN(r13)   /* Soft Disabled */
1560 #ifdef CONFIG_PPC_ISERIES
1561 BEGIN_FW_FTR_SECTION
1562         mfmsr   r5
1563         ori     r5,r5,MSR_EE            /* Hard Enabled on iSeries*/
1564         mtmsrd  r5
1565         li      r5,1
1566 END_FW_FTR_SECTION_IFSET(FW_FEATURE_ISERIES)
1567 #endif
1568         stb     r5,PACAHARDIRQEN(r13)   /* Hard Disabled on others */
1569
1570         bl      .start_kernel
1571
1572         /* Not reached */
1573         BUG_OPCODE
1574
1575 /*
1576  * We put a few things here that have to be page-aligned.
1577  * This stuff goes at the beginning of the bss, which is page-aligned.
1578  */
1579         .section ".bss"
1580
1581         .align  PAGE_SHIFT
1582
1583         .globl  empty_zero_page
1584 empty_zero_page:
1585         .space  PAGE_SIZE
1586
1587         .globl  swapper_pg_dir
1588 swapper_pg_dir:
1589         .space  PGD_TABLE_SIZE