• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1/* SPDX-License-Identifier: GPL-2.0-or-later */
2/*
3 *  PowerPC version
4 *    Copyright (C) 1995-1996 Gary Thomas (gdt@linuxppc.org)
5 *
6 *  Rewritten by Cort Dougan (cort@cs.nmt.edu) for PReP
7 *    Copyright (C) 1996 Cort Dougan <cort@cs.nmt.edu>
8 *  Adapted for Power Macintosh by Paul Mackerras.
9 *  Low-level exception handlers and MMU support
10 *  rewritten by Paul Mackerras.
11 *    Copyright (C) 1996 Paul Mackerras.
12 *  MPC8xx modifications Copyright (C) 1997 Dan Malek (dmalek@jlc.net).
13 *
14 *  This file contains the low-level support and setup for the
15 *  PowerPC platform, including trap and interrupt dispatch.
16 *  (The PPC 8xx embedded CPUs use head_8xx.S instead.)
17 */
18
19#include <linux/init.h>
20#include <asm/reg.h>
21#include <asm/page.h>
22#include <asm/mmu.h>
23#include <asm/pgtable.h>
24#include <asm/cputable.h>
25#include <asm/cache.h>
26#include <asm/thread_info.h>
27#include <asm/ppc_asm.h>
28#include <asm/asm-offsets.h>
29#include <asm/ptrace.h>
30#include <asm/bug.h>
31#include <asm/kvm_book3s_asm.h>
32#include <asm/export.h>
33#include <asm/feature-fixups.h>
34
35#include "head_32.h"
36
37/* 601 only have IBAT */
38#ifdef CONFIG_PPC_BOOK3S_601
39#define LOAD_BAT(n, reg, RA, RB)	\
40	li	RA,0;			\
41	mtspr	SPRN_IBAT##n##U,RA;	\
42	lwz	RA,(n*16)+0(reg);	\
43	lwz	RB,(n*16)+4(reg);	\
44	mtspr	SPRN_IBAT##n##U,RA;	\
45	mtspr	SPRN_IBAT##n##L,RB
46#else
47#define LOAD_BAT(n, reg, RA, RB)	\
48	/* see the comment for clear_bats() -- Cort */ \
49	li	RA,0;			\
50	mtspr	SPRN_IBAT##n##U,RA;	\
51	mtspr	SPRN_DBAT##n##U,RA;	\
52	lwz	RA,(n*16)+0(reg);	\
53	lwz	RB,(n*16)+4(reg);	\
54	mtspr	SPRN_IBAT##n##U,RA;	\
55	mtspr	SPRN_IBAT##n##L,RB;	\
56	lwz	RA,(n*16)+8(reg);	\
57	lwz	RB,(n*16)+12(reg);	\
58	mtspr	SPRN_DBAT##n##U,RA;	\
59	mtspr	SPRN_DBAT##n##L,RB
60#endif
61
62	__HEAD
63	.stabs	"arch/powerpc/kernel/",N_SO,0,0,0f
64	.stabs	"head_32.S",N_SO,0,0,0f
650:
66_ENTRY(_stext);
67
68/*
69 * _start is defined this way because the XCOFF loader in the OpenFirmware
70 * on the powermac expects the entry point to be a procedure descriptor.
71 */
72_ENTRY(_start);
73	/*
74	 * These are here for legacy reasons, the kernel used to
75	 * need to look like a coff function entry for the pmac
76	 * but we're always started by some kind of bootloader now.
77	 *  -- Cort
78	 */
79	nop	/* used by __secondary_hold on prep (mtx) and chrp smp */
80	nop	/* used by __secondary_hold on prep (mtx) and chrp smp */
81	nop
82
83/* PMAC
84 * Enter here with the kernel text, data and bss loaded starting at
85 * 0, running with virtual == physical mapping.
86 * r5 points to the prom entry point (the client interface handler
87 * address).  Address translation is turned on, with the prom
88 * managing the hash table.  Interrupts are disabled.  The stack
89 * pointer (r1) points to just below the end of the half-meg region
90 * from 0x380000 - 0x400000, which is mapped in already.
91 *
92 * If we are booted from MacOS via BootX, we enter with the kernel
93 * image loaded somewhere, and the following values in registers:
94 *  r3: 'BooX' (0x426f6f58)
95 *  r4: virtual address of boot_infos_t
96 *  r5: 0
97 *
98 * PREP
99 * This is jumped to on prep systems right after the kernel is relocated
100 * to its proper place in memory by the boot loader.  The expected layout
101 * of the regs is:
102 *   r3: ptr to residual data
103 *   r4: initrd_start or if no initrd then 0
104 *   r5: initrd_end - unused if r4 is 0
105 *   r6: Start of command line string
106 *   r7: End of command line string
107 *
108 * This just gets a minimal mmu environment setup so we can call
109 * start_here() to do the real work.
110 * -- Cort
111 */
112
113	.globl	__start
114__start:
115/*
116 * We have to do any OF calls before we map ourselves to KERNELBASE,
117 * because OF may have I/O devices mapped into that area
118 * (particularly on CHRP).
119 */
120	cmpwi	0,r5,0
121	beq	1f
122
123#ifdef CONFIG_PPC_OF_BOOT_TRAMPOLINE
124	/* find out where we are now */
125	bcl	20,31,$+4
1260:	mflr	r8			/* r8 = runtime addr here */
127	addis	r8,r8,(_stext - 0b)@ha
128	addi	r8,r8,(_stext - 0b)@l	/* current runtime base addr */
129	bl	prom_init
130#endif /* CONFIG_PPC_OF_BOOT_TRAMPOLINE */
131
132	/* We never return. We also hit that trap if trying to boot
133	 * from OF while CONFIG_PPC_OF_BOOT_TRAMPOLINE isn't selected */
134	trap
135
136/*
137 * Check for BootX signature when supporting PowerMac and branch to
138 * appropriate trampoline if it's present
139 */
140#ifdef CONFIG_PPC_PMAC
1411:	lis	r31,0x426f
142	ori	r31,r31,0x6f58
143	cmpw	0,r3,r31
144	bne	1f
145	bl	bootx_init
146	trap
147#endif /* CONFIG_PPC_PMAC */
148
1491:	mr	r31,r3			/* save device tree ptr */
150	li	r24,0			/* cpu # */
151
152/*
153 * early_init() does the early machine identification and does
154 * the necessary low-level setup and clears the BSS
155 *  -- Cort <cort@fsmlabs.com>
156 */
157	bl	early_init
158
159/* Switch MMU off, clear BATs and flush TLB. At this point, r3 contains
160 * the physical address we are running at, returned by early_init()
161 */
162 	bl	mmu_off
163__after_mmu_off:
164	bl	clear_bats
165	bl	flush_tlbs
166
167	bl	initial_bats
168	bl	load_segment_registers
169#ifdef CONFIG_KASAN
170	bl	early_hash_table
171#endif
172#if defined(CONFIG_BOOTX_TEXT)
173	bl	setup_disp_bat
174#endif
175#ifdef CONFIG_PPC_EARLY_DEBUG_CPM
176	bl	setup_cpm_bat
177#endif
178#ifdef CONFIG_PPC_EARLY_DEBUG_USBGECKO
179	bl	setup_usbgecko_bat
180#endif
181
182/*
183 * Call setup_cpu for CPU 0 and initialize 6xx Idle
184 */
185	bl	reloc_offset
186	li	r24,0			/* cpu# */
187	bl	call_setup_cpu		/* Call setup_cpu for this CPU */
188#ifdef CONFIG_PPC_BOOK3S_32
189	bl	reloc_offset
190	bl	init_idle_6xx
191#endif /* CONFIG_PPC_BOOK3S_32 */
192
193
194/*
195 * We need to run with _start at physical address 0.
196 * On CHRP, we are loaded at 0x10000 since OF on CHRP uses
197 * the exception vectors at 0 (and therefore this copy
198 * overwrites OF's exception vectors with our own).
199 * The MMU is off at this point.
200 */
201	bl	reloc_offset
202	mr	r26,r3
203	addis	r4,r3,KERNELBASE@h	/* current address of _start */
204	lis	r5,PHYSICAL_START@h
205	cmplw	0,r4,r5			/* already running at PHYSICAL_START? */
206	bne	relocate_kernel
207/*
208 * we now have the 1st 16M of ram mapped with the bats.
209 * prep needs the mmu to be turned on here, but pmac already has it on.
210 * this shouldn't bother the pmac since it just gets turned on again
211 * as we jump to our code at KERNELBASE. -- Cort
212 * Actually no, pmac doesn't have it on any more. BootX enters with MMU
213 * off, and in other cases, we now turn it off before changing BATs above.
214 */
215turn_on_mmu:
216	mfmsr	r0
217	ori	r0,r0,MSR_DR|MSR_IR|MSR_RI
218	mtspr	SPRN_SRR1,r0
219	lis	r0,start_here@h
220	ori	r0,r0,start_here@l
221	mtspr	SPRN_SRR0,r0
222	SYNC
223	RFI				/* enables MMU */
224
225/*
226 * We need __secondary_hold as a place to hold the other cpus on
227 * an SMP machine, even when we are running a UP kernel.
228 */
229	. = 0xc0			/* for prep bootloader */
230	li	r3,1			/* MTX only has 1 cpu */
231	.globl	__secondary_hold
232__secondary_hold:
233	/* tell the master we're here */
234	stw	r3,__secondary_hold_acknowledge@l(0)
235#ifdef CONFIG_SMP
236100:	lwz	r4,0(0)
237	/* wait until we're told to start */
238	cmpw	0,r4,r3
239	bne	100b
240	/* our cpu # was at addr 0 - go */
241	mr	r24,r3			/* cpu # */
242	b	__secondary_start
243#else
244	b	.
245#endif /* CONFIG_SMP */
246
247	.globl	__secondary_hold_spinloop
248__secondary_hold_spinloop:
249	.long	0
250	.globl	__secondary_hold_acknowledge
251__secondary_hold_acknowledge:
252	.long	-1
253
254/* System reset */
255/* core99 pmac starts the seconary here by changing the vector, and
256   putting it back to what it was (unknown_exception) when done.  */
257	EXCEPTION(0x100, Reset, unknown_exception, EXC_XFER_STD)
258
259/* Machine check */
260/*
261 * On CHRP, this is complicated by the fact that we could get a
262 * machine check inside RTAS, and we have no guarantee that certain
263 * critical registers will have the values we expect.  The set of
264 * registers that might have bad values includes all the GPRs
265 * and all the BATs.  We indicate that we are in RTAS by putting
266 * a non-zero value, the address of the exception frame to use,
267 * in thread.rtas_sp.  The machine check handler checks thread.rtas_sp
268 * and uses its value if it is non-zero.
269 * (Other exception handlers assume that r1 is a valid kernel stack
270 * pointer when we take an exception from supervisor mode.)
271 *	-- paulus.
272 */
273	. = 0x200
274	DO_KVM  0x200
275	mtspr	SPRN_SPRG_SCRATCH0,r10
276	mtspr	SPRN_SPRG_SCRATCH1,r11
277	mfcr	r10
278#ifdef CONFIG_PPC_CHRP
279	mfspr	r11, SPRN_SPRG_THREAD
280	lwz	r11, RTAS_SP(r11)
281	cmpwi	cr1, r11, 0
282	bne	cr1, 7f
283#endif /* CONFIG_PPC_CHRP */
284	EXCEPTION_PROLOG_1
2857:	EXCEPTION_PROLOG_2
286	addi	r3,r1,STACK_FRAME_OVERHEAD
287#ifdef CONFIG_PPC_CHRP
288	bne	cr1,1f
289#endif
290	EXC_XFER_STD(0x200, machine_check_exception)
291#ifdef CONFIG_PPC_CHRP
2921:	b	machine_check_in_rtas
293#endif
294
295/* Data access exception. */
296	. = 0x300
297	DO_KVM  0x300
298DataAccess:
299	EXCEPTION_PROLOG
300	mfspr	r10,SPRN_DSISR
301	stw	r10,_DSISR(r11)
302#ifdef CONFIG_PPC_KUAP
303	andis.	r0,r10,(DSISR_BAD_FAULT_32S | DSISR_DABRMATCH | DSISR_PROTFAULT)@h
304#else
305	andis.	r0,r10,(DSISR_BAD_FAULT_32S|DSISR_DABRMATCH)@h
306#endif
307	bne	1f			/* if not, try to put a PTE */
308	mfspr	r4,SPRN_DAR		/* into the hash table */
309	rlwinm	r3,r10,32-15,21,21	/* DSISR_STORE -> _PAGE_RW */
310BEGIN_MMU_FTR_SECTION
311	bl	hash_page
312END_MMU_FTR_SECTION_IFSET(MMU_FTR_HPTE_TABLE)
3131:	lwz	r5,_DSISR(r11)		/* get DSISR value */
314	mfspr	r4,SPRN_DAR
315	EXC_XFER_LITE(0x300, handle_page_fault)
316
317
318/* Instruction access exception. */
319	. = 0x400
320	DO_KVM  0x400
321InstructionAccess:
322	EXCEPTION_PROLOG
323	andis.	r0,r9,SRR1_ISI_NOPT@h	/* no pte found? */
324	beq	1f			/* if so, try to put a PTE */
325	li	r3,0			/* into the hash table */
326	mr	r4,r12			/* SRR0 is fault address */
327BEGIN_MMU_FTR_SECTION
328	bl	hash_page
329END_MMU_FTR_SECTION_IFSET(MMU_FTR_HPTE_TABLE)
3301:	mr	r4,r12
331	andis.	r5,r9,DSISR_SRR1_MATCH_32S@h /* Filter relevant SRR1 bits */
332	EXC_XFER_LITE(0x400, handle_page_fault)
333
334/* External interrupt */
335	EXCEPTION(0x500, HardwareInterrupt, do_IRQ, EXC_XFER_LITE)
336
337/* Alignment exception */
338	. = 0x600
339	DO_KVM  0x600
340Alignment:
341	EXCEPTION_PROLOG
342	mfspr	r4,SPRN_DAR
343	stw	r4,_DAR(r11)
344	mfspr	r5,SPRN_DSISR
345	stw	r5,_DSISR(r11)
346	addi	r3,r1,STACK_FRAME_OVERHEAD
347	EXC_XFER_STD(0x600, alignment_exception)
348
349/* Program check exception */
350	EXCEPTION(0x700, ProgramCheck, program_check_exception, EXC_XFER_STD)
351
352/* Floating-point unavailable */
353	. = 0x800
354	DO_KVM  0x800
355FPUnavailable:
356BEGIN_FTR_SECTION
357/*
358 * Certain Freescale cores don't have a FPU and treat fp instructions
359 * as a FP Unavailable exception.  Redirect to illegal/emulation handling.
360 */
361	b 	ProgramCheck
362END_FTR_SECTION_IFSET(CPU_FTR_FPU_UNAVAILABLE)
363	EXCEPTION_PROLOG
364	beq	1f
365	bl	load_up_fpu		/* if from user, just load it up */
366	b	fast_exception_return
3671:	addi	r3,r1,STACK_FRAME_OVERHEAD
368	EXC_XFER_LITE(0x800, kernel_fp_unavailable_exception)
369
370/* Decrementer */
371	EXCEPTION(0x900, Decrementer, timer_interrupt, EXC_XFER_LITE)
372
373	EXCEPTION(0xa00, Trap_0a, unknown_exception, EXC_XFER_STD)
374	EXCEPTION(0xb00, Trap_0b, unknown_exception, EXC_XFER_STD)
375
376/* System call */
377	. = 0xc00
378	DO_KVM  0xc00
379SystemCall:
380	SYSCALL_ENTRY	0xc00
381
382/* Single step - not used on 601 */
383	EXCEPTION(0xd00, SingleStep, single_step_exception, EXC_XFER_STD)
384	EXCEPTION(0xe00, Trap_0e, unknown_exception, EXC_XFER_STD)
385
386/*
387 * The Altivec unavailable trap is at 0x0f20.  Foo.
388 * We effectively remap it to 0x3000.
389 * We include an altivec unavailable exception vector even if
390 * not configured for Altivec, so that you can't panic a
391 * non-altivec kernel running on a machine with altivec just
392 * by executing an altivec instruction.
393 */
394	. = 0xf00
395	DO_KVM  0xf00
396	b	PerformanceMonitor
397
398	. = 0xf20
399	DO_KVM  0xf20
400	b	AltiVecUnavailable
401
402/*
403 * Handle TLB miss for instruction on 603/603e.
404 * Note: we get an alternate set of r0 - r3 to use automatically.
405 */
406	. = 0x1000
407InstructionTLBMiss:
408/*
409 * r0:	scratch
410 * r1:	linux style pte ( later becomes ppc hardware pte )
411 * r2:	ptr to linux-style pte
412 * r3:	scratch
413 */
414	/* Get PTE (linux-style) and check access */
415	mfspr	r3,SPRN_IMISS
416#if defined(CONFIG_MODULES) || defined(CONFIG_DEBUG_PAGEALLOC)
417	lis	r1,PAGE_OFFSET@h		/* check if kernel address */
418	cmplw	0,r1,r3
419#endif
420	mfspr	r2, SPRN_SPRG_PGDIR
421	li	r1,_PAGE_PRESENT | _PAGE_ACCESSED | _PAGE_EXEC | _PAGE_USER
422#if defined(CONFIG_MODULES) || defined(CONFIG_DEBUG_PAGEALLOC)
423	bge-	112f
424	lis	r2, (swapper_pg_dir - PAGE_OFFSET)@ha	/* if kernel address, use */
425	li	r1,_PAGE_PRESENT | _PAGE_ACCESSED | _PAGE_EXEC
426	addi	r2, r2, (swapper_pg_dir - PAGE_OFFSET)@l	/* kernel page table */
427#endif
428112:	rlwimi	r2,r3,12,20,29		/* insert top 10 bits of address */
429	lwz	r2,0(r2)		/* get pmd entry */
430	rlwinm.	r2,r2,0,0,19		/* extract address of pte page */
431	beq-	InstructionAddressInvalid	/* return if no mapping */
432	rlwimi	r2,r3,22,20,29		/* insert next 10 bits of address */
433	lwz	r0,0(r2)		/* get linux-style pte */
434	andc.	r1,r1,r0		/* check access & ~permission */
435	bne-	InstructionAddressInvalid /* return if access not permitted */
436	/* Convert linux-style PTE to low word of PPC-style PTE */
437	rlwimi	r0,r0,32-2,31,31	/* _PAGE_USER -> PP lsb */
438	ori	r1, r1, 0xe06		/* clear out reserved bits */
439	andc	r1, r0, r1		/* PP = user? 1 : 0 */
440BEGIN_FTR_SECTION
441	rlwinm	r1,r1,0,~_PAGE_COHERENT	/* clear M (coherence not required) */
442END_FTR_SECTION_IFCLR(CPU_FTR_NEED_COHERENT)
443	mtspr	SPRN_RPA,r1
444	tlbli	r3
445	mfspr	r3,SPRN_SRR1		/* Need to restore CR0 */
446	mtcrf	0x80,r3
447	rfi
448InstructionAddressInvalid:
449	mfspr	r3,SPRN_SRR1
450	rlwinm	r1,r3,9,6,6	/* Get load/store bit */
451
452	addis	r1,r1,0x2000
453	mtspr	SPRN_DSISR,r1	/* (shouldn't be needed) */
454	andi.	r2,r3,0xFFFF	/* Clear upper bits of SRR1 */
455	or	r2,r2,r1
456	mtspr	SPRN_SRR1,r2
457	mfspr	r1,SPRN_IMISS	/* Get failing address */
458	rlwinm.	r2,r2,0,31,31	/* Check for little endian access */
459	rlwimi	r2,r2,1,30,30	/* change 1 -> 3 */
460	xor	r1,r1,r2
461	mtspr	SPRN_DAR,r1	/* Set fault address */
462	mfmsr	r0		/* Restore "normal" registers */
463	xoris	r0,r0,MSR_TGPR>>16
464	mtcrf	0x80,r3		/* Restore CR0 */
465	mtmsr	r0
466	b	InstructionAccess
467
468/*
469 * Handle TLB miss for DATA Load operation on 603/603e
470 */
471	. = 0x1100
472DataLoadTLBMiss:
473/*
474 * r0:	scratch
475 * r1:	linux style pte ( later becomes ppc hardware pte )
476 * r2:	ptr to linux-style pte
477 * r3:	scratch
478 */
479	/* Get PTE (linux-style) and check access */
480	mfspr	r3,SPRN_DMISS
481	lis	r1,PAGE_OFFSET@h		/* check if kernel address */
482	cmplw	0,r1,r3
483	mfspr	r2, SPRN_SPRG_PGDIR
484	li	r1, _PAGE_PRESENT | _PAGE_ACCESSED | _PAGE_USER
485	bge-	112f
486	lis	r2, (swapper_pg_dir - PAGE_OFFSET)@ha	/* if kernel address, use */
487	li	r1, _PAGE_PRESENT | _PAGE_ACCESSED
488	addi	r2, r2, (swapper_pg_dir - PAGE_OFFSET)@l	/* kernel page table */
489112:	rlwimi	r2,r3,12,20,29		/* insert top 10 bits of address */
490	lwz	r2,0(r2)		/* get pmd entry */
491	rlwinm.	r2,r2,0,0,19		/* extract address of pte page */
492	beq-	DataAddressInvalid	/* return if no mapping */
493	rlwimi	r2,r3,22,20,29		/* insert next 10 bits of address */
494	lwz	r0,0(r2)		/* get linux-style pte */
495	andc.	r1,r1,r0		/* check access & ~permission */
496	bne-	DataAddressInvalid	/* return if access not permitted */
497	/*
498	 * NOTE! We are assuming this is not an SMP system, otherwise
499	 * we would need to update the pte atomically with lwarx/stwcx.
500	 */
501	/* Convert linux-style PTE to low word of PPC-style PTE */
502	rlwinm	r1,r0,32-9,30,30	/* _PAGE_RW -> PP msb */
503	rlwimi	r0,r0,32-1,30,30	/* _PAGE_USER -> PP msb */
504	rlwimi	r0,r0,32-1,31,31	/* _PAGE_USER -> PP lsb */
505	ori	r1,r1,0xe04		/* clear out reserved bits */
506	andc	r1,r0,r1		/* PP = user? rw? 1: 3: 0 */
507BEGIN_FTR_SECTION
508	rlwinm	r1,r1,0,~_PAGE_COHERENT	/* clear M (coherence not required) */
509END_FTR_SECTION_IFCLR(CPU_FTR_NEED_COHERENT)
510	mtspr	SPRN_RPA,r1
511	mfspr	r2,SPRN_SRR1		/* Need to restore CR0 */
512	mtcrf	0x80,r2
513BEGIN_MMU_FTR_SECTION
514	li	r0,1
515	mfspr	r1,SPRN_SPRG_603_LRU
516	rlwinm	r2,r3,20,27,31		/* Get Address bits 15:19 */
517	slw	r0,r0,r2
518	xor	r1,r0,r1
519	srw	r0,r1,r2
520	mtspr   SPRN_SPRG_603_LRU,r1
521	mfspr	r2,SPRN_SRR1
522	rlwimi	r2,r0,31-14,14,14
523	mtspr   SPRN_SRR1,r2
524END_MMU_FTR_SECTION_IFSET(MMU_FTR_NEED_DTLB_SW_LRU)
525	tlbld	r3
526	rfi
527DataAddressInvalid:
528	mfspr	r3,SPRN_SRR1
529	rlwinm	r1,r3,9,6,6	/* Get load/store bit */
530	addis	r1,r1,0x2000
531	mtspr	SPRN_DSISR,r1
532	andi.	r2,r3,0xFFFF	/* Clear upper bits of SRR1 */
533	mtspr	SPRN_SRR1,r2
534	mfspr	r1,SPRN_DMISS	/* Get failing address */
535	rlwinm.	r2,r2,0,31,31	/* Check for little endian access */
536	beq	20f		/* Jump if big endian */
537	xori	r1,r1,3
53820:	mtspr	SPRN_DAR,r1	/* Set fault address */
539	mfmsr	r0		/* Restore "normal" registers */
540	xoris	r0,r0,MSR_TGPR>>16
541	mtcrf	0x80,r3		/* Restore CR0 */
542	mtmsr	r0
543	b	DataAccess
544
545/*
546 * Handle TLB miss for DATA Store on 603/603e
547 */
548	. = 0x1200
549DataStoreTLBMiss:
550/*
551 * r0:	scratch
552 * r1:	linux style pte ( later becomes ppc hardware pte )
553 * r2:	ptr to linux-style pte
554 * r3:	scratch
555 */
556	/* Get PTE (linux-style) and check access */
557	mfspr	r3,SPRN_DMISS
558	lis	r1,PAGE_OFFSET@h		/* check if kernel address */
559	cmplw	0,r1,r3
560	mfspr	r2, SPRN_SPRG_PGDIR
561	li	r1, _PAGE_RW | _PAGE_DIRTY | _PAGE_PRESENT | _PAGE_ACCESSED | _PAGE_USER
562	bge-	112f
563	lis	r2, (swapper_pg_dir - PAGE_OFFSET)@ha	/* if kernel address, use */
564	li	r1, _PAGE_RW | _PAGE_DIRTY | _PAGE_PRESENT | _PAGE_ACCESSED
565	addi	r2, r2, (swapper_pg_dir - PAGE_OFFSET)@l	/* kernel page table */
566112:	rlwimi	r2,r3,12,20,29		/* insert top 10 bits of address */
567	lwz	r2,0(r2)		/* get pmd entry */
568	rlwinm.	r2,r2,0,0,19		/* extract address of pte page */
569	beq-	DataAddressInvalid	/* return if no mapping */
570	rlwimi	r2,r3,22,20,29		/* insert next 10 bits of address */
571	lwz	r0,0(r2)		/* get linux-style pte */
572	andc.	r1,r1,r0		/* check access & ~permission */
573	bne-	DataAddressInvalid	/* return if access not permitted */
574	/*
575	 * NOTE! We are assuming this is not an SMP system, otherwise
576	 * we would need to update the pte atomically with lwarx/stwcx.
577	 */
578	/* Convert linux-style PTE to low word of PPC-style PTE */
579	rlwimi	r0,r0,32-2,31,31	/* _PAGE_USER -> PP lsb */
580	li	r1,0xe06		/* clear out reserved bits & PP msb */
581	andc	r1,r0,r1		/* PP = user? 1: 0 */
582BEGIN_FTR_SECTION
583	rlwinm	r1,r1,0,~_PAGE_COHERENT	/* clear M (coherence not required) */
584END_FTR_SECTION_IFCLR(CPU_FTR_NEED_COHERENT)
585	mtspr	SPRN_RPA,r1
586	mfspr	r2,SPRN_SRR1		/* Need to restore CR0 */
587	mtcrf	0x80,r2
588BEGIN_MMU_FTR_SECTION
589	li	r0,1
590	mfspr	r1,SPRN_SPRG_603_LRU
591	rlwinm	r2,r3,20,27,31		/* Get Address bits 15:19 */
592	slw	r0,r0,r2
593	xor	r1,r0,r1
594	srw	r0,r1,r2
595	mtspr   SPRN_SPRG_603_LRU,r1
596	mfspr	r2,SPRN_SRR1
597	rlwimi	r2,r0,31-14,14,14
598	mtspr   SPRN_SRR1,r2
599END_MMU_FTR_SECTION_IFSET(MMU_FTR_NEED_DTLB_SW_LRU)
600	tlbld	r3
601	rfi
602
603#ifndef CONFIG_ALTIVEC
604#define altivec_assist_exception	unknown_exception
605#endif
606
607	EXCEPTION(0x1300, Trap_13, instruction_breakpoint_exception, EXC_XFER_STD)
608	EXCEPTION(0x1400, SMI, SMIException, EXC_XFER_STD)
609	EXCEPTION(0x1500, Trap_15, unknown_exception, EXC_XFER_STD)
610	EXCEPTION(0x1600, Trap_16, altivec_assist_exception, EXC_XFER_STD)
611	EXCEPTION(0x1700, Trap_17, TAUException, EXC_XFER_STD)
612	EXCEPTION(0x1800, Trap_18, unknown_exception, EXC_XFER_STD)
613	EXCEPTION(0x1900, Trap_19, unknown_exception, EXC_XFER_STD)
614	EXCEPTION(0x1a00, Trap_1a, unknown_exception, EXC_XFER_STD)
615	EXCEPTION(0x1b00, Trap_1b, unknown_exception, EXC_XFER_STD)
616	EXCEPTION(0x1c00, Trap_1c, unknown_exception, EXC_XFER_STD)
617	EXCEPTION(0x1d00, Trap_1d, unknown_exception, EXC_XFER_STD)
618	EXCEPTION(0x1e00, Trap_1e, unknown_exception, EXC_XFER_STD)
619	EXCEPTION(0x1f00, Trap_1f, unknown_exception, EXC_XFER_STD)
620	EXCEPTION(0x2000, RunMode, RunModeException, EXC_XFER_STD)
621	EXCEPTION(0x2100, Trap_21, unknown_exception, EXC_XFER_STD)
622	EXCEPTION(0x2200, Trap_22, unknown_exception, EXC_XFER_STD)
623	EXCEPTION(0x2300, Trap_23, unknown_exception, EXC_XFER_STD)
624	EXCEPTION(0x2400, Trap_24, unknown_exception, EXC_XFER_STD)
625	EXCEPTION(0x2500, Trap_25, unknown_exception, EXC_XFER_STD)
626	EXCEPTION(0x2600, Trap_26, unknown_exception, EXC_XFER_STD)
627	EXCEPTION(0x2700, Trap_27, unknown_exception, EXC_XFER_STD)
628	EXCEPTION(0x2800, Trap_28, unknown_exception, EXC_XFER_STD)
629	EXCEPTION(0x2900, Trap_29, unknown_exception, EXC_XFER_STD)
630	EXCEPTION(0x2a00, Trap_2a, unknown_exception, EXC_XFER_STD)
631	EXCEPTION(0x2b00, Trap_2b, unknown_exception, EXC_XFER_STD)
632	EXCEPTION(0x2c00, Trap_2c, unknown_exception, EXC_XFER_STD)
633	EXCEPTION(0x2d00, Trap_2d, unknown_exception, EXC_XFER_STD)
634	EXCEPTION(0x2e00, Trap_2e, unknown_exception, EXC_XFER_STD)
635	EXCEPTION(0x2f00, Trap_2f, unknown_exception, EXC_XFER_STD)
636
637	. = 0x3000
638
639AltiVecUnavailable:
640	EXCEPTION_PROLOG
641#ifdef CONFIG_ALTIVEC
642	beq	1f
643	bl	load_up_altivec		/* if from user, just load it up */
644	b	fast_exception_return
645#endif /* CONFIG_ALTIVEC */
6461:	addi	r3,r1,STACK_FRAME_OVERHEAD
647	EXC_XFER_LITE(0xf20, altivec_unavailable_exception)
648
649PerformanceMonitor:
650	EXCEPTION_PROLOG
651	addi	r3,r1,STACK_FRAME_OVERHEAD
652	EXC_XFER_STD(0xf00, performance_monitor_exception)
653
654
655/*
656 * This code is jumped to from the startup code to copy
657 * the kernel image to physical address PHYSICAL_START.
658 */
659relocate_kernel:
660	addis	r9,r26,klimit@ha	/* fetch klimit */
661	lwz	r25,klimit@l(r9)
662	addis	r25,r25,-KERNELBASE@h
663	lis	r3,PHYSICAL_START@h	/* Destination base address */
664	li	r6,0			/* Destination offset */
665	li	r5,0x4000		/* # bytes of memory to copy */
666	bl	copy_and_flush		/* copy the first 0x4000 bytes */
667	addi	r0,r3,4f@l		/* jump to the address of 4f */
668	mtctr	r0			/* in copy and do the rest. */
669	bctr				/* jump to the copy */
6704:	mr	r5,r25
671	bl	copy_and_flush		/* copy the rest */
672	b	turn_on_mmu
673
674/*
675 * Copy routine used to copy the kernel to start at physical address 0
676 * and flush and invalidate the caches as needed.
677 * r3 = dest addr, r4 = source addr, r5 = copy limit, r6 = start offset
678 * on exit, r3, r4, r5 are unchanged, r6 is updated to be >= r5.
679 */
680_ENTRY(copy_and_flush)
681	addi	r5,r5,-4
682	addi	r6,r6,-4
6834:	li	r0,L1_CACHE_BYTES/4
684	mtctr	r0
6853:	addi	r6,r6,4			/* copy a cache line */
686	lwzx	r0,r6,r4
687	stwx	r0,r6,r3
688	bdnz	3b
689	dcbst	r6,r3			/* write it to memory */
690	sync
691	icbi	r6,r3			/* flush the icache line */
692	cmplw	0,r6,r5
693	blt	4b
694	sync				/* additional sync needed on g4 */
695	isync
696	addi	r5,r5,4
697	addi	r6,r6,4
698	blr
699
700#ifdef CONFIG_SMP
701	.globl __secondary_start_mpc86xx
702__secondary_start_mpc86xx:
703	mfspr	r3, SPRN_PIR
704	stw	r3, __secondary_hold_acknowledge@l(0)
705	mr	r24, r3			/* cpu # */
706	b	__secondary_start
707
708	.globl	__secondary_start_pmac_0
709__secondary_start_pmac_0:
710	/* NB the entries for cpus 0, 1, 2 must each occupy 8 bytes. */
711	li	r24,0
712	b	1f
713	li	r24,1
714	b	1f
715	li	r24,2
716	b	1f
717	li	r24,3
7181:
719	/* on powersurge, we come in here with IR=0 and DR=1, and DBAT 0
720	   set to map the 0xf0000000 - 0xffffffff region */
721	mfmsr	r0
722	rlwinm	r0,r0,0,28,26		/* clear DR (0x10) */
723	SYNC
724	mtmsr	r0
725	isync
726
727	.globl	__secondary_start
728__secondary_start:
729	/* Copy some CPU settings from CPU 0 */
730	bl	__restore_cpu_setup
731
732	lis	r3,-KERNELBASE@h
733	mr	r4,r24
734	bl	call_setup_cpu		/* Call setup_cpu for this CPU */
735#ifdef CONFIG_PPC_BOOK3S_32
736	lis	r3,-KERNELBASE@h
737	bl	init_idle_6xx
738#endif /* CONFIG_PPC_BOOK3S_32 */
739
740	/* get current's stack and current */
741	lis	r2,secondary_current@ha
742	tophys(r2,r2)
743	lwz	r2,secondary_current@l(r2)
744	tophys(r1,r2)
745	lwz	r1,TASK_STACK(r1)
746
747	/* stack */
748	addi	r1,r1,THREAD_SIZE-STACK_FRAME_OVERHEAD
749	li	r0,0
750	tophys(r3,r1)
751	stw	r0,0(r3)
752
753	/* load up the MMU */
754	bl	load_segment_registers
755	bl	load_up_mmu
756
757	/* ptr to phys current thread */
758	tophys(r4,r2)
759	addi	r4,r4,THREAD	/* phys address of our thread_struct */
760	mtspr	SPRN_SPRG_THREAD,r4
761	lis	r4, (swapper_pg_dir - PAGE_OFFSET)@h
762	ori	r4, r4, (swapper_pg_dir - PAGE_OFFSET)@l
763	mtspr	SPRN_SPRG_PGDIR, r4
764
765	/* enable MMU and jump to start_secondary */
766	li	r4,MSR_KERNEL
767	lis	r3,start_secondary@h
768	ori	r3,r3,start_secondary@l
769	mtspr	SPRN_SRR0,r3
770	mtspr	SPRN_SRR1,r4
771	SYNC
772	RFI
773#endif /* CONFIG_SMP */
774
775#ifdef CONFIG_KVM_BOOK3S_HANDLER
776#include "../kvm/book3s_rmhandlers.S"
777#endif
778
779/*
780 * Those generic dummy functions are kept for CPUs not
781 * included in CONFIG_PPC_BOOK3S_32
782 */
783#if !defined(CONFIG_PPC_BOOK3S_32)
784_ENTRY(__save_cpu_setup)
785	blr
786_ENTRY(__restore_cpu_setup)
787	blr
788#endif /* !defined(CONFIG_PPC_BOOK3S_32) */
789
790/*
791 * Load stuff into the MMU.  Intended to be called with
792 * IR=0 and DR=0.
793 */
794#ifdef CONFIG_KASAN
795early_hash_table:
796	sync			/* Force all PTE updates to finish */
797	isync
798	tlbia			/* Clear all TLB entries */
799	sync			/* wait for tlbia/tlbie to finish */
800	TLBSYNC			/* ... on all CPUs */
801	/* Load the SDR1 register (hash table base & size) */
802	lis	r6, early_hash - PAGE_OFFSET@h
803	ori	r6, r6, 3	/* 256kB table */
804	mtspr	SPRN_SDR1, r6
805	blr
806#endif
807
808load_up_mmu:
809	sync			/* Force all PTE updates to finish */
810	isync
811	tlbia			/* Clear all TLB entries */
812	sync			/* wait for tlbia/tlbie to finish */
813	TLBSYNC			/* ... on all CPUs */
814	/* Load the SDR1 register (hash table base & size) */
815	lis	r6,_SDR1@ha
816	tophys(r6,r6)
817	lwz	r6,_SDR1@l(r6)
818	mtspr	SPRN_SDR1,r6
819
820/* Load the BAT registers with the values set up by MMU_init.
821   MMU_init takes care of whether we're on a 601 or not. */
822	lis	r3,BATS@ha
823	addi	r3,r3,BATS@l
824	tophys(r3,r3)
825	LOAD_BAT(0,r3,r4,r5)
826	LOAD_BAT(1,r3,r4,r5)
827	LOAD_BAT(2,r3,r4,r5)
828	LOAD_BAT(3,r3,r4,r5)
829BEGIN_MMU_FTR_SECTION
830	LOAD_BAT(4,r3,r4,r5)
831	LOAD_BAT(5,r3,r4,r5)
832	LOAD_BAT(6,r3,r4,r5)
833	LOAD_BAT(7,r3,r4,r5)
834END_MMU_FTR_SECTION_IFSET(MMU_FTR_USE_HIGH_BATS)
835	blr
836
837_GLOBAL(load_segment_registers)
838	li	r0, NUM_USER_SEGMENTS /* load up user segment register values */
839	mtctr	r0		/* for context 0 */
840	li	r3, 0		/* Kp = 0, Ks = 0, VSID = 0 */
841#ifdef CONFIG_PPC_KUEP
842	oris	r3, r3, SR_NX@h	/* Set Nx */
843#endif
844#ifdef CONFIG_PPC_KUAP
845	oris	r3, r3, SR_KS@h	/* Set Ks */
846#endif
847	li	r4, 0
8483:	mtsrin	r3, r4
849	addi	r3, r3, 0x111	/* increment VSID */
850	addis	r4, r4, 0x1000	/* address of next segment */
851	bdnz	3b
852	li	r0, 16 - NUM_USER_SEGMENTS /* load up kernel segment registers */
853	mtctr	r0			/* for context 0 */
854	rlwinm	r3, r3, 0, ~SR_NX	/* Nx = 0 */
855	rlwinm	r3, r3, 0, ~SR_KS	/* Ks = 0 */
856	oris	r3, r3, SR_KP@h		/* Kp = 1 */
8573:	mtsrin	r3, r4
858	addi	r3, r3, 0x111	/* increment VSID */
859	addis	r4, r4, 0x1000	/* address of next segment */
860	bdnz	3b
861	blr
862
863/*
864 * This is where the main kernel code starts.
865 */
866start_here:
867	/* ptr to current */
868	lis	r2,init_task@h
869	ori	r2,r2,init_task@l
870	/* Set up for using our exception vectors */
871	/* ptr to phys current thread */
872	tophys(r4,r2)
873	addi	r4,r4,THREAD	/* init task's THREAD */
874	mtspr	SPRN_SPRG_THREAD,r4
875	lis	r4, (swapper_pg_dir - PAGE_OFFSET)@h
876	ori	r4, r4, (swapper_pg_dir - PAGE_OFFSET)@l
877	mtspr	SPRN_SPRG_PGDIR, r4
878
879	/* stack */
880	lis	r1,init_thread_union@ha
881	addi	r1,r1,init_thread_union@l
882	li	r0,0
883	stwu	r0,THREAD_SIZE-STACK_FRAME_OVERHEAD(r1)
884/*
885 * Do early platform-specific initialization,
886 * and set up the MMU.
887 */
888#ifdef CONFIG_KASAN
889	bl	kasan_early_init
890#endif
891	li	r3,0
892	mr	r4,r31
893	bl	machine_init
894	bl	__save_cpu_setup
895	bl	MMU_init
896#ifdef CONFIG_KASAN
897BEGIN_MMU_FTR_SECTION
898	bl	MMU_init_hw_patch
899END_MMU_FTR_SECTION_IFSET(MMU_FTR_HPTE_TABLE)
900#endif
901
902/*
903 * Go back to running unmapped so we can load up new values
904 * for SDR1 (hash table pointer) and the segment registers
905 * and change to using our exception vectors.
906 */
907	lis	r4,2f@h
908	ori	r4,r4,2f@l
909	tophys(r4,r4)
910	li	r3,MSR_KERNEL & ~(MSR_IR|MSR_DR)
911	mtspr	SPRN_SRR0,r4
912	mtspr	SPRN_SRR1,r3
913	SYNC
914	RFI
915/* Load up the kernel context */
9162:	bl	load_up_mmu
917
918#ifdef CONFIG_BDI_SWITCH
919	/* Add helper information for the Abatron bdiGDB debugger.
920	 * We do this here because we know the mmu is disabled, and
921	 * will be enabled for real in just a few instructions.
922	 */
923	lis	r5, abatron_pteptrs@h
924	ori	r5, r5, abatron_pteptrs@l
925	stw	r5, 0xf0(0)	/* This much match your Abatron config */
926	lis	r6, swapper_pg_dir@h
927	ori	r6, r6, swapper_pg_dir@l
928	tophys(r5, r5)
929	stw	r6, 0(r5)
930#endif /* CONFIG_BDI_SWITCH */
931
932/* Now turn on the MMU for real! */
933	li	r4,MSR_KERNEL
934	lis	r3,start_kernel@h
935	ori	r3,r3,start_kernel@l
936	mtspr	SPRN_SRR0,r3
937	mtspr	SPRN_SRR1,r4
938	SYNC
939	RFI
940
941/*
942 * void switch_mmu_context(struct mm_struct *prev, struct mm_struct *next);
943 *
944 * Set up the segment registers for a new context.
945 */
946_ENTRY(switch_mmu_context)
947	lwz	r3,MMCONTEXTID(r4)
948	cmpwi	cr0,r3,0
949	blt-	4f
950	mulli	r3,r3,897	/* multiply context by skew factor */
951	rlwinm	r3,r3,4,8,27	/* VSID = (context & 0xfffff) << 4 */
952#ifdef CONFIG_PPC_KUEP
953	oris	r3, r3, SR_NX@h	/* Set Nx */
954#endif
955#ifdef CONFIG_PPC_KUAP
956	oris	r3, r3, SR_KS@h	/* Set Ks */
957#endif
958	li	r0,NUM_USER_SEGMENTS
959	mtctr	r0
960
961	lwz	r4, MM_PGD(r4)
962#ifdef CONFIG_BDI_SWITCH
963	/* Context switch the PTE pointer for the Abatron BDI2000.
964	 * The PGDIR is passed as second argument.
965	 */
966	lis	r5, abatron_pteptrs@ha
967	stw	r4, abatron_pteptrs@l + 0x4(r5)
968#endif
969	tophys(r4, r4)
970	mtspr	SPRN_SPRG_PGDIR, r4
971	li	r4,0
972	isync
9733:
974	mtsrin	r3,r4
975	addi	r3,r3,0x111	/* next VSID */
976	rlwinm	r3,r3,0,8,3	/* clear out any overflow from VSID field */
977	addis	r4,r4,0x1000	/* address of next segment */
978	bdnz	3b
979	sync
980	isync
981	blr
9824:	trap
983	EMIT_BUG_ENTRY 4b,__FILE__,__LINE__,0
984	blr
985EXPORT_SYMBOL(switch_mmu_context)
986
987/*
988 * An undocumented "feature" of 604e requires that the v bit
989 * be cleared before changing BAT values.
990 *
991 * Also, newer IBM firmware does not clear bat3 and 4 so
992 * this makes sure it's done.
993 *  -- Cort
994 */
995clear_bats:
996	li	r10,0
997
998#ifndef CONFIG_PPC_BOOK3S_601
999	mtspr	SPRN_DBAT0U,r10
1000	mtspr	SPRN_DBAT0L,r10
1001	mtspr	SPRN_DBAT1U,r10
1002	mtspr	SPRN_DBAT1L,r10
1003	mtspr	SPRN_DBAT2U,r10
1004	mtspr	SPRN_DBAT2L,r10
1005	mtspr	SPRN_DBAT3U,r10
1006	mtspr	SPRN_DBAT3L,r10
1007#endif
1008	mtspr	SPRN_IBAT0U,r10
1009	mtspr	SPRN_IBAT0L,r10
1010	mtspr	SPRN_IBAT1U,r10
1011	mtspr	SPRN_IBAT1L,r10
1012	mtspr	SPRN_IBAT2U,r10
1013	mtspr	SPRN_IBAT2L,r10
1014	mtspr	SPRN_IBAT3U,r10
1015	mtspr	SPRN_IBAT3L,r10
1016BEGIN_MMU_FTR_SECTION
1017	/* Here's a tweak: at this point, CPU setup have
1018	 * not been called yet, so HIGH_BAT_EN may not be
1019	 * set in HID0 for the 745x processors. However, it
1020	 * seems that doesn't affect our ability to actually
1021	 * write to these SPRs.
1022	 */
1023	mtspr	SPRN_DBAT4U,r10
1024	mtspr	SPRN_DBAT4L,r10
1025	mtspr	SPRN_DBAT5U,r10
1026	mtspr	SPRN_DBAT5L,r10
1027	mtspr	SPRN_DBAT6U,r10
1028	mtspr	SPRN_DBAT6L,r10
1029	mtspr	SPRN_DBAT7U,r10
1030	mtspr	SPRN_DBAT7L,r10
1031	mtspr	SPRN_IBAT4U,r10
1032	mtspr	SPRN_IBAT4L,r10
1033	mtspr	SPRN_IBAT5U,r10
1034	mtspr	SPRN_IBAT5L,r10
1035	mtspr	SPRN_IBAT6U,r10
1036	mtspr	SPRN_IBAT6L,r10
1037	mtspr	SPRN_IBAT7U,r10
1038	mtspr	SPRN_IBAT7L,r10
1039END_MMU_FTR_SECTION_IFSET(MMU_FTR_USE_HIGH_BATS)
1040	blr
1041
1042_ENTRY(update_bats)
1043	lis	r4, 1f@h
1044	ori	r4, r4, 1f@l
1045	tophys(r4, r4)
1046	mfmsr	r6
1047	mflr	r7
1048	li	r3, MSR_KERNEL & ~(MSR_IR | MSR_DR)
1049	rlwinm	r0, r6, 0, ~MSR_RI
1050	rlwinm	r0, r0, 0, ~MSR_EE
1051	mtmsr	r0
1052	mtspr	SPRN_SRR0, r4
1053	mtspr	SPRN_SRR1, r3
1054	SYNC
1055	RFI
10561:	bl	clear_bats
1057	lis	r3, BATS@ha
1058	addi	r3, r3, BATS@l
1059	tophys(r3, r3)
1060	LOAD_BAT(0, r3, r4, r5)
1061	LOAD_BAT(1, r3, r4, r5)
1062	LOAD_BAT(2, r3, r4, r5)
1063	LOAD_BAT(3, r3, r4, r5)
1064BEGIN_MMU_FTR_SECTION
1065	LOAD_BAT(4, r3, r4, r5)
1066	LOAD_BAT(5, r3, r4, r5)
1067	LOAD_BAT(6, r3, r4, r5)
1068	LOAD_BAT(7, r3, r4, r5)
1069END_MMU_FTR_SECTION_IFSET(MMU_FTR_USE_HIGH_BATS)
1070	li	r3, MSR_KERNEL & ~(MSR_IR | MSR_DR | MSR_RI)
1071	mtmsr	r3
1072	mtspr	SPRN_SRR0, r7
1073	mtspr	SPRN_SRR1, r6
1074	SYNC
1075	RFI
1076
1077flush_tlbs:
1078	lis	r10, 0x40
10791:	addic.	r10, r10, -0x1000
1080	tlbie	r10
1081	bgt	1b
1082	sync
1083	blr
1084
1085mmu_off:
1086 	addi	r4, r3, __after_mmu_off - _start
1087	mfmsr	r3
1088	andi.	r0,r3,MSR_DR|MSR_IR		/* MMU enabled? */
1089	beqlr
1090	andc	r3,r3,r0
1091	mtspr	SPRN_SRR0,r4
1092	mtspr	SPRN_SRR1,r3
1093	sync
1094	RFI
1095
1096/*
1097 * On 601, we use 3 BATs to map up to 24M of RAM at _PAGE_OFFSET
1098 * (we keep one for debugging) and on others, we use one 256M BAT.
1099 */
1100initial_bats:
1101	lis	r11,PAGE_OFFSET@h
1102#ifdef CONFIG_PPC_BOOK3S_601
1103	ori	r11,r11,4		/* set up BAT registers for 601 */
1104	li	r8,0x7f			/* valid, block length = 8MB */
1105	mtspr	SPRN_IBAT0U,r11		/* N.B. 601 has valid bit in */
1106	mtspr	SPRN_IBAT0L,r8		/* lower BAT register */
1107	addis	r11,r11,0x800000@h
1108	addis	r8,r8,0x800000@h
1109	mtspr	SPRN_IBAT1U,r11
1110	mtspr	SPRN_IBAT1L,r8
1111	addis	r11,r11,0x800000@h
1112	addis	r8,r8,0x800000@h
1113	mtspr	SPRN_IBAT2U,r11
1114	mtspr	SPRN_IBAT2L,r8
1115#else
1116	tophys(r8,r11)
1117#ifdef CONFIG_SMP
1118	ori	r8,r8,0x12		/* R/W access, M=1 */
1119#else
1120	ori	r8,r8,2			/* R/W access */
1121#endif /* CONFIG_SMP */
1122	ori	r11,r11,BL_256M<<2|0x2	/* set up BAT registers for 604 */
1123
1124	mtspr	SPRN_DBAT0L,r8		/* N.B. 6xx (not 601) have valid */
1125	mtspr	SPRN_DBAT0U,r11		/* bit in upper BAT register */
1126	mtspr	SPRN_IBAT0L,r8
1127	mtspr	SPRN_IBAT0U,r11
1128#endif
1129	isync
1130	blr
1131
1132#ifdef CONFIG_BOOTX_TEXT
1133setup_disp_bat:
1134	/*
1135	 * setup the display bat prepared for us in prom.c
1136	 */
1137	mflr	r8
1138	bl	reloc_offset
1139	mtlr	r8
1140	addis	r8,r3,disp_BAT@ha
1141	addi	r8,r8,disp_BAT@l
1142	cmpwi	cr0,r8,0
1143	beqlr
1144	lwz	r11,0(r8)
1145	lwz	r8,4(r8)
1146#ifndef CONFIG_PPC_BOOK3S_601
1147	mtspr	SPRN_DBAT3L,r8
1148	mtspr	SPRN_DBAT3U,r11
1149#else
1150	mtspr	SPRN_IBAT3L,r8
1151	mtspr	SPRN_IBAT3U,r11
1152#endif
1153	blr
1154#endif /* CONFIG_BOOTX_TEXT */
1155
1156#ifdef CONFIG_PPC_EARLY_DEBUG_CPM
1157setup_cpm_bat:
1158	lis	r8, 0xf000
1159	ori	r8, r8,	0x002a
1160	mtspr	SPRN_DBAT1L, r8
1161
1162	lis	r11, 0xf000
1163	ori	r11, r11, (BL_1M << 2) | 2
1164	mtspr	SPRN_DBAT1U, r11
1165
1166	blr
1167#endif
1168
1169#ifdef CONFIG_PPC_EARLY_DEBUG_USBGECKO
1170setup_usbgecko_bat:
1171	/* prepare a BAT for early io */
1172#if defined(CONFIG_GAMECUBE)
1173	lis	r8, 0x0c00
1174#elif defined(CONFIG_WII)
1175	lis	r8, 0x0d00
1176#else
1177#error Invalid platform for USB Gecko based early debugging.
1178#endif
1179	/*
1180	 * The virtual address used must match the virtual address
1181	 * associated to the fixmap entry FIX_EARLY_DEBUG_BASE.
1182	 */
1183	lis	r11, 0xfffe	/* top 128K */
1184	ori	r8, r8, 0x002a	/* uncached, guarded ,rw */
1185	ori	r11, r11, 0x2	/* 128K, Vs=1, Vp=0 */
1186	mtspr	SPRN_DBAT1L, r8
1187	mtspr	SPRN_DBAT1U, r11
1188	blr
1189#endif
1190
1191#ifdef CONFIG_8260
1192/* Jump into the system reset for the rom.
1193 * We first disable the MMU, and then jump to the ROM reset address.
1194 *
1195 * r3 is the board info structure, r4 is the location for starting.
1196 * I use this for building a small kernel that can load other kernels,
1197 * rather than trying to write or rely on a rom monitor that can tftp load.
1198 */
1199       .globl  m8260_gorom
1200m8260_gorom:
1201	mfmsr	r0
1202	rlwinm	r0,r0,0,17,15	/* clear MSR_EE in r0 */
1203	sync
1204	mtmsr	r0
1205	sync
1206	mfspr	r11, SPRN_HID0
1207	lis	r10, 0
1208	ori	r10,r10,HID0_ICE|HID0_DCE
1209	andc	r11, r11, r10
1210	mtspr	SPRN_HID0, r11
1211	isync
1212	li	r5, MSR_ME|MSR_RI
1213	lis	r6,2f@h
1214	addis	r6,r6,-KERNELBASE@h
1215	ori	r6,r6,2f@l
1216	mtspr	SPRN_SRR0,r6
1217	mtspr	SPRN_SRR1,r5
1218	isync
1219	sync
1220	rfi
12212:
1222	mtlr	r4
1223	blr
1224#endif
1225
1226
1227/*
1228 * We put a few things here that have to be page-aligned.
1229 * This stuff goes at the beginning of the data segment,
1230 * which is page-aligned.
1231 */
1232	.data
1233	.globl	sdata
1234sdata:
1235	.globl	empty_zero_page
1236empty_zero_page:
1237	.space	4096
1238EXPORT_SYMBOL(empty_zero_page)
1239
1240	.globl	swapper_pg_dir
1241swapper_pg_dir:
1242	.space	PGD_TABLE_SIZE
1243
1244/* Room for two PTE pointers, usually the kernel and current user pointers
1245 * to their respective root page table.
1246 */
1247abatron_pteptrs:
1248	.space	8
1249