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