xref: /openbmc/linux/arch/powerpc/kernel/head_44x.S (revision 4e67bfd7aa21b4b737a43df627956dba9c742983)
114cf11afSPaul Mackerras/*
214cf11afSPaul Mackerras * Kernel execution entry point code.
314cf11afSPaul Mackerras *
414cf11afSPaul Mackerras *    Copyright (c) 1995-1996 Gary Thomas <gdt@linuxppc.org>
514cf11afSPaul Mackerras *      Initial PowerPC version.
614cf11afSPaul Mackerras *    Copyright (c) 1996 Cort Dougan <cort@cs.nmt.edu>
714cf11afSPaul Mackerras *      Rewritten for PReP
814cf11afSPaul Mackerras *    Copyright (c) 1996 Paul Mackerras <paulus@cs.anu.edu.au>
914cf11afSPaul Mackerras *      Low-level exception handers, MMU support, and rewrite.
1014cf11afSPaul Mackerras *    Copyright (c) 1997 Dan Malek <dmalek@jlc.net>
1114cf11afSPaul Mackerras *      PowerPC 8xx modifications.
1214cf11afSPaul Mackerras *    Copyright (c) 1998-1999 TiVo, Inc.
1314cf11afSPaul Mackerras *      PowerPC 403GCX modifications.
1414cf11afSPaul Mackerras *    Copyright (c) 1999 Grant Erickson <grant@lcse.umn.edu>
1514cf11afSPaul Mackerras *      PowerPC 403GCX/405GP modifications.
1614cf11afSPaul Mackerras *    Copyright 2000 MontaVista Software Inc.
1714cf11afSPaul Mackerras *	PPC405 modifications
1814cf11afSPaul Mackerras *      PowerPC 403GCX/405GP modifications.
1914cf11afSPaul Mackerras * 	Author: MontaVista Software, Inc.
2014cf11afSPaul Mackerras *         	frank_rowand@mvista.com or source@mvista.com
2114cf11afSPaul Mackerras * 	   	debbie_chu@mvista.com
2214cf11afSPaul Mackerras *    Copyright 2002-2005 MontaVista Software, Inc.
2314cf11afSPaul Mackerras *      PowerPC 44x support, Matt Porter <mporter@kernel.crashing.org>
2414cf11afSPaul Mackerras *
2514cf11afSPaul Mackerras * This program is free software; you can redistribute  it and/or modify it
2614cf11afSPaul Mackerras * under  the terms of  the GNU General  Public License as published by the
2714cf11afSPaul Mackerras * Free Software Foundation;  either version 2 of the  License, or (at your
2814cf11afSPaul Mackerras * option) any later version.
2914cf11afSPaul Mackerras */
3014cf11afSPaul Mackerras
31e7039845STim Abbott#include <linux/init.h>
3214cf11afSPaul Mackerras#include <asm/processor.h>
3314cf11afSPaul Mackerras#include <asm/page.h>
3414cf11afSPaul Mackerras#include <asm/mmu.h>
3514cf11afSPaul Mackerras#include <asm/pgtable.h>
3614cf11afSPaul Mackerras#include <asm/cputable.h>
3714cf11afSPaul Mackerras#include <asm/thread_info.h>
3814cf11afSPaul Mackerras#include <asm/ppc_asm.h>
3914cf11afSPaul Mackerras#include <asm/asm-offsets.h>
4046f52210SStephen Rothwell#include <asm/ptrace.h>
41e7f75ad0SDave Kleikamp#include <asm/synch.h>
429445aa1aSAl Viro#include <asm/export.h>
436c16816bSChristophe Leroy#include <asm/code-patching-asm.h>
4414cf11afSPaul Mackerras#include "head_booke.h"
4514cf11afSPaul Mackerras
4614cf11afSPaul Mackerras
4714cf11afSPaul Mackerras/* As with the other PowerPC ports, it is expected that when code
4814cf11afSPaul Mackerras * execution begins here, the following registers contain valid, yet
4914cf11afSPaul Mackerras * optional, information:
5014cf11afSPaul Mackerras *
5114cf11afSPaul Mackerras *   r3 - Board info structure pointer (DRAM, frequency, MAC address, etc.)
5214cf11afSPaul Mackerras *   r4 - Starting address of the init RAM disk
5314cf11afSPaul Mackerras *   r5 - Ending address of the init RAM disk
5414cf11afSPaul Mackerras *   r6 - Start of kernel command line string (e.g. "mem=128")
5514cf11afSPaul Mackerras *   r7 - End of kernel command line string
5614cf11afSPaul Mackerras *
5714cf11afSPaul Mackerras */
58e7039845STim Abbott	__HEAD
59748a7683SKumar Gala_ENTRY(_stext);
60748a7683SKumar Gala_ENTRY(_start);
6114cf11afSPaul Mackerras	/*
6214cf11afSPaul Mackerras	 * Reserve a word at a fixed location to store the address
6314cf11afSPaul Mackerras	 * of abatron_pteptrs
6414cf11afSPaul Mackerras	 */
6514cf11afSPaul Mackerras	nop
666dece0ebSScott Wood	mr	r31,r3		/* save device tree ptr */
6714cf11afSPaul Mackerras	li	r24,0		/* CPU number */
6814cf11afSPaul Mackerras
6926ecb6c4SSuzuki Poulose#ifdef CONFIG_RELOCATABLE
7026ecb6c4SSuzuki Poulose/*
7126ecb6c4SSuzuki Poulose * Relocate ourselves to the current runtime address.
7226ecb6c4SSuzuki Poulose * This is called only by the Boot CPU.
7326ecb6c4SSuzuki Poulose * "relocate" is called with our current runtime virutal
7426ecb6c4SSuzuki Poulose * address.
7526ecb6c4SSuzuki Poulose * r21 will be loaded with the physical runtime address of _stext
7626ecb6c4SSuzuki Poulose */
7726ecb6c4SSuzuki Poulose	bl	0f				/* Get our runtime address */
7826ecb6c4SSuzuki Poulose0:	mflr	r21				/* Make it accessible */
7926ecb6c4SSuzuki Poulose	addis	r21,r21,(_stext - 0b)@ha
8026ecb6c4SSuzuki Poulose	addi	r21,r21,(_stext - 0b)@l 	/* Get our current runtime base */
8126ecb6c4SSuzuki Poulose
8226ecb6c4SSuzuki Poulose	/*
8326ecb6c4SSuzuki Poulose	 * We have the runtime (virutal) address of our base.
8426ecb6c4SSuzuki Poulose	 * We calculate our shift of offset from a 256M page.
8526ecb6c4SSuzuki Poulose	 * We could map the 256M page we belong to at PAGE_OFFSET and
8626ecb6c4SSuzuki Poulose	 * get going from there.
8726ecb6c4SSuzuki Poulose	 */
8826ecb6c4SSuzuki Poulose	lis	r4,KERNELBASE@h
8926ecb6c4SSuzuki Poulose	ori	r4,r4,KERNELBASE@l
9026ecb6c4SSuzuki Poulose	rlwinm	r6,r21,0,4,31			/* r6 = PHYS_START % 256M */
9126ecb6c4SSuzuki Poulose	rlwinm	r5,r4,0,4,31			/* r5 = KERNELBASE % 256M */
9226ecb6c4SSuzuki Poulose	subf	r3,r5,r6			/* r3 = r6 - r5 */
9326ecb6c4SSuzuki Poulose	add	r3,r4,r3			/* Required Virutal Address */
9426ecb6c4SSuzuki Poulose
9526ecb6c4SSuzuki Poulose	bl	relocate
9626ecb6c4SSuzuki Poulose#endif
9726ecb6c4SSuzuki Poulose
98795033c3SDave Kleikamp	bl	init_cpu_state
9914cf11afSPaul Mackerras
10014cf11afSPaul Mackerras	/*
10114cf11afSPaul Mackerras	 * This is where the main kernel code starts.
10214cf11afSPaul Mackerras	 */
10314cf11afSPaul Mackerras
10414cf11afSPaul Mackerras	/* ptr to current */
10514cf11afSPaul Mackerras	lis	r2,init_task@h
10614cf11afSPaul Mackerras	ori	r2,r2,init_task@l
10714cf11afSPaul Mackerras
10814cf11afSPaul Mackerras	/* ptr to current thread */
10914cf11afSPaul Mackerras	addi	r4,r2,THREAD	/* init task's THREAD */
110ee43eb78SBenjamin Herrenschmidt	mtspr	SPRN_SPRG_THREAD,r4
11114cf11afSPaul Mackerras
11214cf11afSPaul Mackerras	/* stack */
11314cf11afSPaul Mackerras	lis	r1,init_thread_union@h
11414cf11afSPaul Mackerras	ori	r1,r1,init_thread_union@l
11514cf11afSPaul Mackerras	li	r0,0
11614cf11afSPaul Mackerras	stwu	r0,THREAD_SIZE-STACK_FRAME_OVERHEAD(r1)
11714cf11afSPaul Mackerras
11814cf11afSPaul Mackerras	bl	early_init
11914cf11afSPaul Mackerras
12026ecb6c4SSuzuki Poulose#ifdef CONFIG_RELOCATABLE
12126ecb6c4SSuzuki Poulose	/*
12226ecb6c4SSuzuki Poulose	 * Relocatable kernel support based on processing of dynamic
12326ecb6c4SSuzuki Poulose	 * relocation entries.
12426ecb6c4SSuzuki Poulose	 *
12526ecb6c4SSuzuki Poulose	 * r25 will contain RPN/ERPN for the start address of memory
12626ecb6c4SSuzuki Poulose	 * r21 will contain the current offset of _stext
12726ecb6c4SSuzuki Poulose	 */
12826ecb6c4SSuzuki Poulose	lis	r3,kernstart_addr@ha
12926ecb6c4SSuzuki Poulose	la	r3,kernstart_addr@l(r3)
13026ecb6c4SSuzuki Poulose
13126ecb6c4SSuzuki Poulose	/*
13226ecb6c4SSuzuki Poulose	 * Compute the kernstart_addr.
13326ecb6c4SSuzuki Poulose	 * kernstart_addr => (r6,r8)
13426ecb6c4SSuzuki Poulose	 * kernstart_addr & ~0xfffffff => (r6,r7)
13526ecb6c4SSuzuki Poulose	 */
13626ecb6c4SSuzuki Poulose	rlwinm	r6,r25,0,28,31	/* ERPN. Bits 32-35 of Address */
13726ecb6c4SSuzuki Poulose	rlwinm	r7,r25,0,0,3	/* RPN - assuming 256 MB page size */
13826ecb6c4SSuzuki Poulose	rlwinm	r8,r21,0,4,31	/* r8 = (_stext & 0xfffffff) */
13926ecb6c4SSuzuki Poulose	or	r8,r7,r8	/* Compute the lower 32bit of kernstart_addr */
14026ecb6c4SSuzuki Poulose
14126ecb6c4SSuzuki Poulose	/* Store kernstart_addr */
14226ecb6c4SSuzuki Poulose	stw	r6,0(r3)	/* higher 32bit */
14326ecb6c4SSuzuki Poulose	stw	r8,4(r3)	/* lower 32bit  */
14426ecb6c4SSuzuki Poulose
14526ecb6c4SSuzuki Poulose	/*
14626ecb6c4SSuzuki Poulose	 * Compute the virt_phys_offset :
14726ecb6c4SSuzuki Poulose	 * virt_phys_offset = stext.run - kernstart_addr
14826ecb6c4SSuzuki Poulose	 *
14926ecb6c4SSuzuki Poulose	 * stext.run = (KERNELBASE & ~0xfffffff) + (kernstart_addr & 0xfffffff)
15026ecb6c4SSuzuki Poulose	 * When we relocate, we have :
15126ecb6c4SSuzuki Poulose	 *
15226ecb6c4SSuzuki Poulose	 *	(kernstart_addr & 0xfffffff) = (stext.run & 0xfffffff)
15326ecb6c4SSuzuki Poulose	 *
15426ecb6c4SSuzuki Poulose	 * hence:
15526ecb6c4SSuzuki Poulose	 *  virt_phys_offset = (KERNELBASE & ~0xfffffff) - (kernstart_addr & ~0xfffffff)
15626ecb6c4SSuzuki Poulose	 *
15726ecb6c4SSuzuki Poulose	 */
15826ecb6c4SSuzuki Poulose
15926ecb6c4SSuzuki Poulose	/* KERNELBASE&~0xfffffff => (r4,r5) */
16026ecb6c4SSuzuki Poulose	li	r4, 0		/* higer 32bit */
16126ecb6c4SSuzuki Poulose	lis	r5,KERNELBASE@h
16226ecb6c4SSuzuki Poulose	rlwinm	r5,r5,0,0,3	/* Align to 256M, lower 32bit */
16326ecb6c4SSuzuki Poulose
16426ecb6c4SSuzuki Poulose	/*
16526ecb6c4SSuzuki Poulose	 * 64bit subtraction.
16626ecb6c4SSuzuki Poulose	 */
16726ecb6c4SSuzuki Poulose	subfc	r5,r7,r5
16826ecb6c4SSuzuki Poulose	subfe	r4,r6,r4
16926ecb6c4SSuzuki Poulose
17026ecb6c4SSuzuki Poulose	/* Store virt_phys_offset */
17126ecb6c4SSuzuki Poulose	lis	r3,virt_phys_offset@ha
17226ecb6c4SSuzuki Poulose	la	r3,virt_phys_offset@l(r3)
17326ecb6c4SSuzuki Poulose
17426ecb6c4SSuzuki Poulose	stw	r4,0(r3)
17526ecb6c4SSuzuki Poulose	stw	r5,4(r3)
17626ecb6c4SSuzuki Poulose
17726ecb6c4SSuzuki Poulose#elif defined(CONFIG_DYNAMIC_MEMSTART)
1789661534dSDave Kleikamp	/*
1790f890c8dSSuzuki Poulose	 * Mapping based, page aligned dynamic kernel loading.
1800f890c8dSSuzuki Poulose	 *
1819661534dSDave Kleikamp	 * r25 will contain RPN/ERPN for the start address of memory
1829661534dSDave Kleikamp	 *
1839661534dSDave Kleikamp	 * Add the difference between KERNELBASE and PAGE_OFFSET to the
1849661534dSDave Kleikamp	 * start of physical memory to get kernstart_addr.
1859661534dSDave Kleikamp	 */
1869661534dSDave Kleikamp	lis	r3,kernstart_addr@ha
1879661534dSDave Kleikamp	la	r3,kernstart_addr@l(r3)
1889661534dSDave Kleikamp
1899661534dSDave Kleikamp	lis	r4,KERNELBASE@h
1909661534dSDave Kleikamp	ori	r4,r4,KERNELBASE@l
1919661534dSDave Kleikamp	lis	r5,PAGE_OFFSET@h
1929661534dSDave Kleikamp	ori	r5,r5,PAGE_OFFSET@l
1939661534dSDave Kleikamp	subf	r4,r5,r4
1949661534dSDave Kleikamp
1959661534dSDave Kleikamp	rlwinm	r6,r25,0,28,31	/* ERPN */
1969661534dSDave Kleikamp	rlwinm	r7,r25,0,0,3	/* RPN - assuming 256 MB page size */
1979661534dSDave Kleikamp	add	r7,r7,r4
1989661534dSDave Kleikamp
1999661534dSDave Kleikamp	stw	r6,0(r3)
2009661534dSDave Kleikamp	stw	r7,4(r3)
2019661534dSDave Kleikamp#endif
2029661534dSDave Kleikamp
20314cf11afSPaul Mackerras/*
20414cf11afSPaul Mackerras * Decide what sort of machine this is and initialize the MMU.
20514cf11afSPaul Mackerras */
2066dece0ebSScott Wood	li	r3,0
2076dece0ebSScott Wood	mr	r4,r31
20814cf11afSPaul Mackerras	bl	machine_init
20914cf11afSPaul Mackerras	bl	MMU_init
21014cf11afSPaul Mackerras
21114cf11afSPaul Mackerras	/* Setup PTE pointers for the Abatron bdiGDB */
21214cf11afSPaul Mackerras	lis	r6, swapper_pg_dir@h
21314cf11afSPaul Mackerras	ori	r6, r6, swapper_pg_dir@l
21414cf11afSPaul Mackerras	lis	r5, abatron_pteptrs@h
21514cf11afSPaul Mackerras	ori	r5, r5, abatron_pteptrs@l
21614cf11afSPaul Mackerras	lis	r4, KERNELBASE@h
21714cf11afSPaul Mackerras	ori	r4, r4, KERNELBASE@l
21814cf11afSPaul Mackerras	stw	r5, 0(r4)	/* Save abatron_pteptrs at a fixed location */
21914cf11afSPaul Mackerras	stw	r6, 0(r5)
22014cf11afSPaul Mackerras
221029b8f66SDave Kleikamp	/* Clear the Machine Check Syndrome Register */
222029b8f66SDave Kleikamp	li	r0,0
223029b8f66SDave Kleikamp	mtspr	SPRN_MCSR,r0
224029b8f66SDave Kleikamp
22514cf11afSPaul Mackerras	/* Let's move on */
22614cf11afSPaul Mackerras	lis	r4,start_kernel@h
22714cf11afSPaul Mackerras	ori	r4,r4,start_kernel@l
22814cf11afSPaul Mackerras	lis	r3,MSR_KERNEL@h
22914cf11afSPaul Mackerras	ori	r3,r3,MSR_KERNEL@l
23014cf11afSPaul Mackerras	mtspr	SPRN_SRR0,r4
23114cf11afSPaul Mackerras	mtspr	SPRN_SRR1,r3
23214cf11afSPaul Mackerras	rfi			/* change context and jump to start_kernel */
23314cf11afSPaul Mackerras
23414cf11afSPaul Mackerras/*
23514cf11afSPaul Mackerras * Interrupt vector entry code
23614cf11afSPaul Mackerras *
23714cf11afSPaul Mackerras * The Book E MMUs are always on so we don't need to handle
23814cf11afSPaul Mackerras * interrupts in real mode as with previous PPC processors. In
23914cf11afSPaul Mackerras * this case we handle interrupts in the kernel virtual address
24014cf11afSPaul Mackerras * space.
24114cf11afSPaul Mackerras *
24214cf11afSPaul Mackerras * Interrupt vectors are dynamically placed relative to the
24314cf11afSPaul Mackerras * interrupt prefix as determined by the address of interrupt_base.
24414cf11afSPaul Mackerras * The interrupt vectors offsets are programmed using the labels
24514cf11afSPaul Mackerras * for each interrupt vector entry.
24614cf11afSPaul Mackerras *
24714cf11afSPaul Mackerras * Interrupt vectors must be aligned on a 16 byte boundary.
24814cf11afSPaul Mackerras * We align on a 32 byte cache line boundary for good measure.
24914cf11afSPaul Mackerras */
25014cf11afSPaul Mackerras
25114cf11afSPaul Mackerrasinterrupt_base:
25214cf11afSPaul Mackerras	/* Critical Input Interrupt */
253cfac5784SScott Wood	CRITICAL_EXCEPTION(0x0100, CRITICAL, CriticalInput, unknown_exception)
25414cf11afSPaul Mackerras
25514cf11afSPaul Mackerras	/* Machine Check Interrupt */
256cfac5784SScott Wood	CRITICAL_EXCEPTION(0x0200, MACHINE_CHECK, MachineCheck, \
257cfac5784SScott Wood			   machine_check_exception)
25847c0bd1aSBenjamin Herrenschmidt	MCHECK_EXCEPTION(0x0210, MachineCheckA, machine_check_exception)
25914cf11afSPaul Mackerras
26014cf11afSPaul Mackerras	/* Data Storage Interrupt */
2611bc54c03SBenjamin Herrenschmidt	DATA_STORAGE_EXCEPTION
26214cf11afSPaul Mackerras
26314cf11afSPaul Mackerras		/* Instruction Storage Interrupt */
26414cf11afSPaul Mackerras	INSTRUCTION_STORAGE_EXCEPTION
26514cf11afSPaul Mackerras
26614cf11afSPaul Mackerras	/* External Input Interrupt */
267cfac5784SScott Wood	EXCEPTION(0x0500, BOOKE_INTERRUPT_EXTERNAL, ExternalInput, \
268cfac5784SScott Wood		  do_IRQ, EXC_XFER_LITE)
26914cf11afSPaul Mackerras
27014cf11afSPaul Mackerras	/* Alignment Interrupt */
27114cf11afSPaul Mackerras	ALIGNMENT_EXCEPTION
27214cf11afSPaul Mackerras
27314cf11afSPaul Mackerras	/* Program Interrupt */
27414cf11afSPaul Mackerras	PROGRAM_EXCEPTION
27514cf11afSPaul Mackerras
27614cf11afSPaul Mackerras	/* Floating Point Unavailable Interrupt */
27714cf11afSPaul Mackerras#ifdef CONFIG_PPC_FPU
27814cf11afSPaul Mackerras	FP_UNAVAILABLE_EXCEPTION
27914cf11afSPaul Mackerras#else
280cfac5784SScott Wood	EXCEPTION(0x2010, BOOKE_INTERRUPT_FP_UNAVAIL, \
281cfac5784SScott Wood		  FloatingPointUnavailable, unknown_exception, EXC_XFER_EE)
28214cf11afSPaul Mackerras#endif
28314cf11afSPaul Mackerras	/* System Call Interrupt */
28414cf11afSPaul Mackerras	START_EXCEPTION(SystemCall)
285cfac5784SScott Wood	NORMAL_EXCEPTION_PROLOG(BOOKE_INTERRUPT_SYSCALL)
28614cf11afSPaul Mackerras	EXC_XFER_EE_LITE(0x0c00, DoSyscall)
28714cf11afSPaul Mackerras
28825985edcSLucas De Marchi	/* Auxiliary Processor Unavailable Interrupt */
289cfac5784SScott Wood	EXCEPTION(0x2020, BOOKE_INTERRUPT_AP_UNAVAIL, \
290cfac5784SScott Wood		  AuxillaryProcessorUnavailable, unknown_exception, EXC_XFER_EE)
29114cf11afSPaul Mackerras
29214cf11afSPaul Mackerras	/* Decrementer Interrupt */
29314cf11afSPaul Mackerras	DECREMENTER_EXCEPTION
29414cf11afSPaul Mackerras
29514cf11afSPaul Mackerras	/* Fixed Internal Timer Interrupt */
29614cf11afSPaul Mackerras	/* TODO: Add FIT support */
297cfac5784SScott Wood	EXCEPTION(0x1010, BOOKE_INTERRUPT_FIT, FixedIntervalTimer, \
298cfac5784SScott Wood		  unknown_exception, EXC_XFER_EE)
29914cf11afSPaul Mackerras
30014cf11afSPaul Mackerras	/* Watchdog Timer Interrupt */
30114cf11afSPaul Mackerras	/* TODO: Add watchdog support */
30214cf11afSPaul Mackerras#ifdef CONFIG_BOOKE_WDT
303cfac5784SScott Wood	CRITICAL_EXCEPTION(0x1020, WATCHDOG, WatchdogTimer, WatchdogException)
30414cf11afSPaul Mackerras#else
305cfac5784SScott Wood	CRITICAL_EXCEPTION(0x1020, WATCHDOG, WatchdogTimer, unknown_exception)
30614cf11afSPaul Mackerras#endif
30714cf11afSPaul Mackerras
30814cf11afSPaul Mackerras	/* Data TLB Error Interrupt */
309e7f75ad0SDave Kleikamp	START_EXCEPTION(DataTLBError44x)
310ee43eb78SBenjamin Herrenschmidt	mtspr	SPRN_SPRG_WSCRATCH0, r10		/* Save some working registers */
311ee43eb78SBenjamin Herrenschmidt	mtspr	SPRN_SPRG_WSCRATCH1, r11
312ee43eb78SBenjamin Herrenschmidt	mtspr	SPRN_SPRG_WSCRATCH2, r12
313ee43eb78SBenjamin Herrenschmidt	mtspr	SPRN_SPRG_WSCRATCH3, r13
31414cf11afSPaul Mackerras	mfcr	r11
315ee43eb78SBenjamin Herrenschmidt	mtspr	SPRN_SPRG_WSCRATCH4, r11
31614cf11afSPaul Mackerras	mfspr	r10, SPRN_DEAR		/* Get faulting address */
31714cf11afSPaul Mackerras
31814cf11afSPaul Mackerras	/* If we are faulting a kernel address, we have to use the
31914cf11afSPaul Mackerras	 * kernel page tables.
32014cf11afSPaul Mackerras	 */
3218a13c4f9SKumar Gala	lis	r11, PAGE_OFFSET@h
32214cf11afSPaul Mackerras	cmplw	r10, r11
32314cf11afSPaul Mackerras	blt+	3f
32414cf11afSPaul Mackerras	lis	r11, swapper_pg_dir@h
32514cf11afSPaul Mackerras	ori	r11, r11, swapper_pg_dir@l
32614cf11afSPaul Mackerras
32714cf11afSPaul Mackerras	mfspr	r12,SPRN_MMUCR
32814cf11afSPaul Mackerras	rlwinm	r12,r12,0,0,23		/* Clear TID */
32914cf11afSPaul Mackerras
33014cf11afSPaul Mackerras	b	4f
33114cf11afSPaul Mackerras
33214cf11afSPaul Mackerras	/* Get the PGD for the current thread */
33314cf11afSPaul Mackerras3:
334ee43eb78SBenjamin Herrenschmidt	mfspr	r11,SPRN_SPRG_THREAD
33514cf11afSPaul Mackerras	lwz	r11,PGDIR(r11)
33614cf11afSPaul Mackerras
33714cf11afSPaul Mackerras	/* Load PID into MMUCR TID */
33814cf11afSPaul Mackerras	mfspr	r12,SPRN_MMUCR
33914cf11afSPaul Mackerras	mfspr   r13,SPRN_PID		/* Get PID */
34014cf11afSPaul Mackerras	rlwimi	r12,r13,0,24,31		/* Set TID */
34114cf11afSPaul Mackerras
34214cf11afSPaul Mackerras4:
34314cf11afSPaul Mackerras	mtspr	SPRN_MMUCR,r12
34414cf11afSPaul Mackerras
3451bc54c03SBenjamin Herrenschmidt	/* Mask of required permission bits. Note that while we
3461bc54c03SBenjamin Herrenschmidt	 * do copy ESR:ST to _PAGE_RW position as trying to write
3471bc54c03SBenjamin Herrenschmidt	 * to an RO page is pretty common, we don't do it with
3481bc54c03SBenjamin Herrenschmidt	 * _PAGE_DIRTY. We could do it, but it's a fairly rare
3491bc54c03SBenjamin Herrenschmidt	 * event so I'd rather take the overhead when it happens
3501bc54c03SBenjamin Herrenschmidt	 * rather than adding an instruction here. We should measure
3511bc54c03SBenjamin Herrenschmidt	 * whether the whole thing is worth it in the first place
3521bc54c03SBenjamin Herrenschmidt	 * as we could avoid loading SPRN_ESR completely in the first
3531bc54c03SBenjamin Herrenschmidt	 * place...
3541bc54c03SBenjamin Herrenschmidt	 *
3551bc54c03SBenjamin Herrenschmidt	 * TODO: Is it worth doing that mfspr & rlwimi in the first
3561bc54c03SBenjamin Herrenschmidt	 *       place or can we save a couple of instructions here ?
3571bc54c03SBenjamin Herrenschmidt	 */
3581bc54c03SBenjamin Herrenschmidt	mfspr	r12,SPRN_ESR
3591bc54c03SBenjamin Herrenschmidt	li	r13,_PAGE_PRESENT|_PAGE_ACCESSED
3601bc54c03SBenjamin Herrenschmidt	rlwimi	r13,r12,10,30,30
3611bc54c03SBenjamin Herrenschmidt
3621bc54c03SBenjamin Herrenschmidt	/* Load the PTE */
363ca9153a3SIlya Yanok	/* Compute pgdir/pmd offset */
364ca9153a3SIlya Yanok	rlwinm  r12, r10, PPC44x_PGD_OFF_SHIFT, PPC44x_PGD_OFF_MASK_BIT, 29
36514cf11afSPaul Mackerras	lwzx	r11, r12, r11		/* Get pgd/pmd entry */
36614cf11afSPaul Mackerras	rlwinm.	r12, r11, 0, 0, 20	/* Extract pt base address */
36714cf11afSPaul Mackerras	beq	2f			/* Bail if no table */
36814cf11afSPaul Mackerras
369ca9153a3SIlya Yanok	/* Compute pte address */
370ca9153a3SIlya Yanok	rlwimi  r12, r10, PPC44x_PTE_ADD_SHIFT, PPC44x_PTE_ADD_MASK_BIT, 28
3711bc54c03SBenjamin Herrenschmidt	lwz	r11, 0(r12)		/* Get high word of pte entry */
3721bc54c03SBenjamin Herrenschmidt	lwz	r12, 4(r12)		/* Get low word of pte entry */
37314cf11afSPaul Mackerras
3741bc54c03SBenjamin Herrenschmidt	lis	r10,tlb_44x_index@ha
3751bc54c03SBenjamin Herrenschmidt
3761bc54c03SBenjamin Herrenschmidt	andc.	r13,r13,r12		/* Check permission */
3771bc54c03SBenjamin Herrenschmidt
3781bc54c03SBenjamin Herrenschmidt	/* Load the next available TLB index */
3791bc54c03SBenjamin Herrenschmidt	lwz	r13,tlb_44x_index@l(r10)
3801bc54c03SBenjamin Herrenschmidt
3811bc54c03SBenjamin Herrenschmidt	bne	2f			/* Bail if permission mismach */
3821bc54c03SBenjamin Herrenschmidt
3831bc54c03SBenjamin Herrenschmidt	/* Increment, rollover, and store TLB index */
3841bc54c03SBenjamin Herrenschmidt	addi	r13,r13,1
3851bc54c03SBenjamin Herrenschmidt
3866c16816bSChristophe Leroy	patch_site 0f, patch__tlb_44x_hwater_D
3871bc54c03SBenjamin Herrenschmidt	/* Compare with watermark (instruction gets patched) */
3886c16816bSChristophe Leroy0:	cmpwi	0,r13,1			/* reserve entries */
3891bc54c03SBenjamin Herrenschmidt	ble	5f
3901bc54c03SBenjamin Herrenschmidt	li	r13,0
3911bc54c03SBenjamin Herrenschmidt5:
3921bc54c03SBenjamin Herrenschmidt	/* Store the next available TLB index */
3931bc54c03SBenjamin Herrenschmidt	stw	r13,tlb_44x_index@l(r10)
3941bc54c03SBenjamin Herrenschmidt
3951bc54c03SBenjamin Herrenschmidt	/* Re-load the faulting address */
3961bc54c03SBenjamin Herrenschmidt	mfspr	r10,SPRN_DEAR
39714cf11afSPaul Mackerras
39814cf11afSPaul Mackerras	 /* Jump to common tlb load */
399e7f75ad0SDave Kleikamp	b	finish_tlb_load_44x
40014cf11afSPaul Mackerras
40114cf11afSPaul Mackerras2:
40214cf11afSPaul Mackerras	/* The bailout.  Restore registers to pre-exception conditions
40314cf11afSPaul Mackerras	 * and call the heavyweights to help us out.
40414cf11afSPaul Mackerras	 */
405ee43eb78SBenjamin Herrenschmidt	mfspr	r11, SPRN_SPRG_RSCRATCH4
40614cf11afSPaul Mackerras	mtcr	r11
407ee43eb78SBenjamin Herrenschmidt	mfspr	r13, SPRN_SPRG_RSCRATCH3
408ee43eb78SBenjamin Herrenschmidt	mfspr	r12, SPRN_SPRG_RSCRATCH2
409ee43eb78SBenjamin Herrenschmidt	mfspr	r11, SPRN_SPRG_RSCRATCH1
410ee43eb78SBenjamin Herrenschmidt	mfspr	r10, SPRN_SPRG_RSCRATCH0
4111bc54c03SBenjamin Herrenschmidt	b	DataStorage
41214cf11afSPaul Mackerras
41314cf11afSPaul Mackerras	/* Instruction TLB Error Interrupt */
41414cf11afSPaul Mackerras	/*
41514cf11afSPaul Mackerras	 * Nearly the same as above, except we get our
41614cf11afSPaul Mackerras	 * information from different registers and bailout
41714cf11afSPaul Mackerras	 * to a different point.
41814cf11afSPaul Mackerras	 */
419e7f75ad0SDave Kleikamp	START_EXCEPTION(InstructionTLBError44x)
420ee43eb78SBenjamin Herrenschmidt	mtspr	SPRN_SPRG_WSCRATCH0, r10 /* Save some working registers */
421ee43eb78SBenjamin Herrenschmidt	mtspr	SPRN_SPRG_WSCRATCH1, r11
422ee43eb78SBenjamin Herrenschmidt	mtspr	SPRN_SPRG_WSCRATCH2, r12
423ee43eb78SBenjamin Herrenschmidt	mtspr	SPRN_SPRG_WSCRATCH3, r13
42414cf11afSPaul Mackerras	mfcr	r11
425ee43eb78SBenjamin Herrenschmidt	mtspr	SPRN_SPRG_WSCRATCH4, r11
42614cf11afSPaul Mackerras	mfspr	r10, SPRN_SRR0		/* Get faulting address */
42714cf11afSPaul Mackerras
42814cf11afSPaul Mackerras	/* If we are faulting a kernel address, we have to use the
42914cf11afSPaul Mackerras	 * kernel page tables.
43014cf11afSPaul Mackerras	 */
4318a13c4f9SKumar Gala	lis	r11, PAGE_OFFSET@h
43214cf11afSPaul Mackerras	cmplw	r10, r11
43314cf11afSPaul Mackerras	blt+	3f
43414cf11afSPaul Mackerras	lis	r11, swapper_pg_dir@h
43514cf11afSPaul Mackerras	ori	r11, r11, swapper_pg_dir@l
43614cf11afSPaul Mackerras
43714cf11afSPaul Mackerras	mfspr	r12,SPRN_MMUCR
43814cf11afSPaul Mackerras	rlwinm	r12,r12,0,0,23		/* Clear TID */
43914cf11afSPaul Mackerras
44014cf11afSPaul Mackerras	b	4f
44114cf11afSPaul Mackerras
44214cf11afSPaul Mackerras	/* Get the PGD for the current thread */
44314cf11afSPaul Mackerras3:
444ee43eb78SBenjamin Herrenschmidt	mfspr	r11,SPRN_SPRG_THREAD
44514cf11afSPaul Mackerras	lwz	r11,PGDIR(r11)
44614cf11afSPaul Mackerras
44714cf11afSPaul Mackerras	/* Load PID into MMUCR TID */
44814cf11afSPaul Mackerras	mfspr	r12,SPRN_MMUCR
44914cf11afSPaul Mackerras	mfspr   r13,SPRN_PID		/* Get PID */
45014cf11afSPaul Mackerras	rlwimi	r12,r13,0,24,31		/* Set TID */
45114cf11afSPaul Mackerras
45214cf11afSPaul Mackerras4:
45314cf11afSPaul Mackerras	mtspr	SPRN_MMUCR,r12
45414cf11afSPaul Mackerras
4551bc54c03SBenjamin Herrenschmidt	/* Make up the required permissions */
456ea3cc330SBenjamin Herrenschmidt	li	r13,_PAGE_PRESENT | _PAGE_ACCESSED | _PAGE_EXEC
4571bc54c03SBenjamin Herrenschmidt
458ca9153a3SIlya Yanok	/* Compute pgdir/pmd offset */
459ca9153a3SIlya Yanok	rlwinm 	r12, r10, PPC44x_PGD_OFF_SHIFT, PPC44x_PGD_OFF_MASK_BIT, 29
46014cf11afSPaul Mackerras	lwzx	r11, r12, r11		/* Get pgd/pmd entry */
46114cf11afSPaul Mackerras	rlwinm.	r12, r11, 0, 0, 20	/* Extract pt base address */
46214cf11afSPaul Mackerras	beq	2f			/* Bail if no table */
46314cf11afSPaul Mackerras
464ca9153a3SIlya Yanok	/* Compute pte address */
465ca9153a3SIlya Yanok	rlwimi	r12, r10, PPC44x_PTE_ADD_SHIFT, PPC44x_PTE_ADD_MASK_BIT, 28
4661bc54c03SBenjamin Herrenschmidt	lwz	r11, 0(r12)		/* Get high word of pte entry */
4671bc54c03SBenjamin Herrenschmidt	lwz	r12, 4(r12)		/* Get low word of pte entry */
46814cf11afSPaul Mackerras
4691bc54c03SBenjamin Herrenschmidt	lis	r10,tlb_44x_index@ha
4701bc54c03SBenjamin Herrenschmidt
4711bc54c03SBenjamin Herrenschmidt	andc.	r13,r13,r12		/* Check permission */
4721bc54c03SBenjamin Herrenschmidt
4731bc54c03SBenjamin Herrenschmidt	/* Load the next available TLB index */
4741bc54c03SBenjamin Herrenschmidt	lwz	r13,tlb_44x_index@l(r10)
4751bc54c03SBenjamin Herrenschmidt
4761bc54c03SBenjamin Herrenschmidt	bne	2f			/* Bail if permission mismach */
4771bc54c03SBenjamin Herrenschmidt
4781bc54c03SBenjamin Herrenschmidt	/* Increment, rollover, and store TLB index */
4791bc54c03SBenjamin Herrenschmidt	addi	r13,r13,1
4801bc54c03SBenjamin Herrenschmidt
4816c16816bSChristophe Leroy	patch_site 0f, patch__tlb_44x_hwater_I
4821bc54c03SBenjamin Herrenschmidt	/* Compare with watermark (instruction gets patched) */
4836c16816bSChristophe Leroy0:	cmpwi	0,r13,1			/* reserve entries */
4841bc54c03SBenjamin Herrenschmidt	ble	5f
4851bc54c03SBenjamin Herrenschmidt	li	r13,0
4861bc54c03SBenjamin Herrenschmidt5:
4871bc54c03SBenjamin Herrenschmidt	/* Store the next available TLB index */
4881bc54c03SBenjamin Herrenschmidt	stw	r13,tlb_44x_index@l(r10)
4891bc54c03SBenjamin Herrenschmidt
4901bc54c03SBenjamin Herrenschmidt	/* Re-load the faulting address */
4911bc54c03SBenjamin Herrenschmidt	mfspr	r10,SPRN_SRR0
49214cf11afSPaul Mackerras
49314cf11afSPaul Mackerras	/* Jump to common TLB load point */
494e7f75ad0SDave Kleikamp	b	finish_tlb_load_44x
49514cf11afSPaul Mackerras
49614cf11afSPaul Mackerras2:
49714cf11afSPaul Mackerras	/* The bailout.  Restore registers to pre-exception conditions
49814cf11afSPaul Mackerras	 * and call the heavyweights to help us out.
49914cf11afSPaul Mackerras	 */
500ee43eb78SBenjamin Herrenschmidt	mfspr	r11, SPRN_SPRG_RSCRATCH4
50114cf11afSPaul Mackerras	mtcr	r11
502ee43eb78SBenjamin Herrenschmidt	mfspr	r13, SPRN_SPRG_RSCRATCH3
503ee43eb78SBenjamin Herrenschmidt	mfspr	r12, SPRN_SPRG_RSCRATCH2
504ee43eb78SBenjamin Herrenschmidt	mfspr	r11, SPRN_SPRG_RSCRATCH1
505ee43eb78SBenjamin Herrenschmidt	mfspr	r10, SPRN_SPRG_RSCRATCH0
50614cf11afSPaul Mackerras	b	InstructionStorage
50714cf11afSPaul Mackerras
50814cf11afSPaul Mackerras/*
50914cf11afSPaul Mackerras * Both the instruction and data TLB miss get to this
51014cf11afSPaul Mackerras * point to load the TLB.
51114cf11afSPaul Mackerras * 	r10 - EA of fault
5121bc54c03SBenjamin Herrenschmidt * 	r11 - PTE high word value
5131bc54c03SBenjamin Herrenschmidt *	r12 - PTE low word value
5141bc54c03SBenjamin Herrenschmidt *	r13 - TLB index
51514cf11afSPaul Mackerras *	MMUCR - loaded with proper value when we get here
51614cf11afSPaul Mackerras *	Upon exit, we reload everything and RFI.
51714cf11afSPaul Mackerras */
518e7f75ad0SDave Kleikampfinish_tlb_load_44x:
5191bc54c03SBenjamin Herrenschmidt	/* Combine RPN & ERPN an write WS 0 */
520ca9153a3SIlya Yanok	rlwimi	r11,r12,0,0,31-PAGE_SHIFT
5211bc54c03SBenjamin Herrenschmidt	tlbwe	r11,r13,PPC44x_TLB_XLAT
52214cf11afSPaul Mackerras
52314cf11afSPaul Mackerras	/*
5241bc54c03SBenjamin Herrenschmidt	 * Create WS1. This is the faulting address (EPN),
52514cf11afSPaul Mackerras	 * page size, and valid flag.
52614cf11afSPaul Mackerras	 */
527ca9153a3SIlya Yanok	li	r11,PPC44x_TLB_VALID | PPC44x_TLBE_SIZE
528ca9153a3SIlya Yanok	/* Insert valid and page size */
529ca9153a3SIlya Yanok	rlwimi	r10,r11,0,PPC44x_PTE_ADD_MASK_BIT,31
53014cf11afSPaul Mackerras	tlbwe	r10,r13,PPC44x_TLB_PAGEID	/* Write PAGEID */
53114cf11afSPaul Mackerras
5321bc54c03SBenjamin Herrenschmidt	/* And WS 2 */
5331bc54c03SBenjamin Herrenschmidt	li	r10,0xf85			/* Mask to apply from PTE */
5341bc54c03SBenjamin Herrenschmidt	rlwimi	r10,r12,29,30,30		/* DIRTY -> SW position */
5351bc54c03SBenjamin Herrenschmidt	and	r11,r12,r10			/* Mask PTE bits to keep */
5361bc54c03SBenjamin Herrenschmidt	andi.	r10,r12,_PAGE_USER		/* User page ? */
5371bc54c03SBenjamin Herrenschmidt	beq	1f				/* nope, leave U bits empty */
5381bc54c03SBenjamin Herrenschmidt	rlwimi	r11,r11,3,26,28			/* yes, copy S bits to U */
5391bc54c03SBenjamin Herrenschmidt1:	tlbwe	r11,r13,PPC44x_TLB_ATTRIB	/* Write ATTRIB */
54014cf11afSPaul Mackerras
54114cf11afSPaul Mackerras	/* Done...restore registers and get out of here.
54214cf11afSPaul Mackerras	*/
543ee43eb78SBenjamin Herrenschmidt	mfspr	r11, SPRN_SPRG_RSCRATCH4
54414cf11afSPaul Mackerras	mtcr	r11
545ee43eb78SBenjamin Herrenschmidt	mfspr	r13, SPRN_SPRG_RSCRATCH3
546ee43eb78SBenjamin Herrenschmidt	mfspr	r12, SPRN_SPRG_RSCRATCH2
547ee43eb78SBenjamin Herrenschmidt	mfspr	r11, SPRN_SPRG_RSCRATCH1
548ee43eb78SBenjamin Herrenschmidt	mfspr	r10, SPRN_SPRG_RSCRATCH0
54914cf11afSPaul Mackerras	rfi					/* Force context change */
55014cf11afSPaul Mackerras
551e7f75ad0SDave Kleikamp/* TLB error interrupts for 476
552e7f75ad0SDave Kleikamp */
553e7f75ad0SDave Kleikamp#ifdef CONFIG_PPC_47x
554e7f75ad0SDave Kleikamp	START_EXCEPTION(DataTLBError47x)
555e7f75ad0SDave Kleikamp	mtspr	SPRN_SPRG_WSCRATCH0,r10	/* Save some working registers */
556e7f75ad0SDave Kleikamp	mtspr	SPRN_SPRG_WSCRATCH1,r11
557e7f75ad0SDave Kleikamp	mtspr	SPRN_SPRG_WSCRATCH2,r12
558e7f75ad0SDave Kleikamp	mtspr	SPRN_SPRG_WSCRATCH3,r13
559e7f75ad0SDave Kleikamp	mfcr	r11
560e7f75ad0SDave Kleikamp	mtspr	SPRN_SPRG_WSCRATCH4,r11
561e7f75ad0SDave Kleikamp	mfspr	r10,SPRN_DEAR		/* Get faulting address */
562e7f75ad0SDave Kleikamp
563e7f75ad0SDave Kleikamp	/* If we are faulting a kernel address, we have to use the
564e7f75ad0SDave Kleikamp	 * kernel page tables.
565e7f75ad0SDave Kleikamp	 */
566e7f75ad0SDave Kleikamp	lis	r11,PAGE_OFFSET@h
567e7f75ad0SDave Kleikamp	cmplw	cr0,r10,r11
568e7f75ad0SDave Kleikamp	blt+	3f
569e7f75ad0SDave Kleikamp	lis	r11,swapper_pg_dir@h
570e7f75ad0SDave Kleikamp	ori	r11,r11, swapper_pg_dir@l
571e7f75ad0SDave Kleikamp	li	r12,0			/* MMUCR = 0 */
572e7f75ad0SDave Kleikamp	b	4f
573e7f75ad0SDave Kleikamp
574e7f75ad0SDave Kleikamp	/* Get the PGD for the current thread and setup MMUCR */
575e7f75ad0SDave Kleikamp3:	mfspr	r11,SPRN_SPRG3
576e7f75ad0SDave Kleikamp	lwz	r11,PGDIR(r11)
577e7f75ad0SDave Kleikamp	mfspr   r12,SPRN_PID		/* Get PID */
578e7f75ad0SDave Kleikamp4:	mtspr	SPRN_MMUCR,r12		/* Set MMUCR */
579e7f75ad0SDave Kleikamp
580e7f75ad0SDave Kleikamp	/* Mask of required permission bits. Note that while we
581e7f75ad0SDave Kleikamp	 * do copy ESR:ST to _PAGE_RW position as trying to write
582e7f75ad0SDave Kleikamp	 * to an RO page is pretty common, we don't do it with
583e7f75ad0SDave Kleikamp	 * _PAGE_DIRTY. We could do it, but it's a fairly rare
584e7f75ad0SDave Kleikamp	 * event so I'd rather take the overhead when it happens
585e7f75ad0SDave Kleikamp	 * rather than adding an instruction here. We should measure
586e7f75ad0SDave Kleikamp	 * whether the whole thing is worth it in the first place
587e7f75ad0SDave Kleikamp	 * as we could avoid loading SPRN_ESR completely in the first
588e7f75ad0SDave Kleikamp	 * place...
589e7f75ad0SDave Kleikamp	 *
590e7f75ad0SDave Kleikamp	 * TODO: Is it worth doing that mfspr & rlwimi in the first
591e7f75ad0SDave Kleikamp	 *       place or can we save a couple of instructions here ?
592e7f75ad0SDave Kleikamp	 */
593e7f75ad0SDave Kleikamp	mfspr	r12,SPRN_ESR
594e7f75ad0SDave Kleikamp	li	r13,_PAGE_PRESENT|_PAGE_ACCESSED
595e7f75ad0SDave Kleikamp	rlwimi	r13,r12,10,30,30
596e7f75ad0SDave Kleikamp
597e7f75ad0SDave Kleikamp	/* Load the PTE */
598e7f75ad0SDave Kleikamp	/* Compute pgdir/pmd offset */
599e7f75ad0SDave Kleikamp	rlwinm  r12,r10,PPC44x_PGD_OFF_SHIFT,PPC44x_PGD_OFF_MASK_BIT,29
600e7f75ad0SDave Kleikamp	lwzx	r11,r12,r11		/* Get pgd/pmd entry */
601e7f75ad0SDave Kleikamp
602e7f75ad0SDave Kleikamp	/* Word 0 is EPN,V,TS,DSIZ */
603e7f75ad0SDave Kleikamp	li	r12,PPC47x_TLB0_VALID | PPC47x_TLBE_SIZE
604e7f75ad0SDave Kleikamp	rlwimi	r10,r12,0,32-PAGE_SHIFT,31	/* Insert valid and page size*/
605e7f75ad0SDave Kleikamp	li	r12,0
606e7f75ad0SDave Kleikamp	tlbwe	r10,r12,0
607e7f75ad0SDave Kleikamp
608e7f75ad0SDave Kleikamp	/* XXX can we do better ? Need to make sure tlbwe has established
609e7f75ad0SDave Kleikamp	 * latch V bit in MMUCR0 before the PTE is loaded further down */
610e7f75ad0SDave Kleikamp#ifdef CONFIG_SMP
611e7f75ad0SDave Kleikamp	isync
612e7f75ad0SDave Kleikamp#endif
613e7f75ad0SDave Kleikamp
614e7f75ad0SDave Kleikamp	rlwinm.	r12,r11,0,0,20		/* Extract pt base address */
615e7f75ad0SDave Kleikamp	/* Compute pte address */
616e7f75ad0SDave Kleikamp	rlwimi  r12,r10,PPC44x_PTE_ADD_SHIFT,PPC44x_PTE_ADD_MASK_BIT,28
617e7f75ad0SDave Kleikamp	beq	2f			/* Bail if no table */
618e7f75ad0SDave Kleikamp	lwz	r11,0(r12)		/* Get high word of pte entry */
619e7f75ad0SDave Kleikamp
620e7f75ad0SDave Kleikamp	/* XXX can we do better ? maybe insert a known 0 bit from r11 into the
621e7f75ad0SDave Kleikamp	 * bottom of r12 to create a data dependency... We can also use r10
622e7f75ad0SDave Kleikamp	 * as destination nowadays
623e7f75ad0SDave Kleikamp	 */
624e7f75ad0SDave Kleikamp#ifdef CONFIG_SMP
625e7f75ad0SDave Kleikamp	lwsync
626e7f75ad0SDave Kleikamp#endif
627e7f75ad0SDave Kleikamp	lwz	r12,4(r12)		/* Get low word of pte entry */
628e7f75ad0SDave Kleikamp
629e7f75ad0SDave Kleikamp	andc.	r13,r13,r12		/* Check permission */
630e7f75ad0SDave Kleikamp
631e7f75ad0SDave Kleikamp	 /* Jump to common tlb load */
632e7f75ad0SDave Kleikamp	beq	finish_tlb_load_47x
633e7f75ad0SDave Kleikamp
634e7f75ad0SDave Kleikamp2:	/* The bailout.  Restore registers to pre-exception conditions
635e7f75ad0SDave Kleikamp	 * and call the heavyweights to help us out.
636e7f75ad0SDave Kleikamp	 */
637e7f75ad0SDave Kleikamp	mfspr	r11,SPRN_SPRG_RSCRATCH4
638e7f75ad0SDave Kleikamp	mtcr	r11
639e7f75ad0SDave Kleikamp	mfspr	r13,SPRN_SPRG_RSCRATCH3
640e7f75ad0SDave Kleikamp	mfspr	r12,SPRN_SPRG_RSCRATCH2
641e7f75ad0SDave Kleikamp	mfspr	r11,SPRN_SPRG_RSCRATCH1
642e7f75ad0SDave Kleikamp	mfspr	r10,SPRN_SPRG_RSCRATCH0
643e7f75ad0SDave Kleikamp	b	DataStorage
644e7f75ad0SDave Kleikamp
645e7f75ad0SDave Kleikamp	/* Instruction TLB Error Interrupt */
646e7f75ad0SDave Kleikamp	/*
647e7f75ad0SDave Kleikamp	 * Nearly the same as above, except we get our
648e7f75ad0SDave Kleikamp	 * information from different registers and bailout
649e7f75ad0SDave Kleikamp	 * to a different point.
650e7f75ad0SDave Kleikamp	 */
651e7f75ad0SDave Kleikamp	START_EXCEPTION(InstructionTLBError47x)
652e7f75ad0SDave Kleikamp	mtspr	SPRN_SPRG_WSCRATCH0,r10	/* Save some working registers */
653e7f75ad0SDave Kleikamp	mtspr	SPRN_SPRG_WSCRATCH1,r11
654e7f75ad0SDave Kleikamp	mtspr	SPRN_SPRG_WSCRATCH2,r12
655e7f75ad0SDave Kleikamp	mtspr	SPRN_SPRG_WSCRATCH3,r13
656e7f75ad0SDave Kleikamp	mfcr	r11
657e7f75ad0SDave Kleikamp	mtspr	SPRN_SPRG_WSCRATCH4,r11
658e7f75ad0SDave Kleikamp	mfspr	r10,SPRN_SRR0		/* Get faulting address */
659e7f75ad0SDave Kleikamp
660e7f75ad0SDave Kleikamp	/* If we are faulting a kernel address, we have to use the
661e7f75ad0SDave Kleikamp	 * kernel page tables.
662e7f75ad0SDave Kleikamp	 */
663e7f75ad0SDave Kleikamp	lis	r11,PAGE_OFFSET@h
664e7f75ad0SDave Kleikamp	cmplw	cr0,r10,r11
665e7f75ad0SDave Kleikamp	blt+	3f
666e7f75ad0SDave Kleikamp	lis	r11,swapper_pg_dir@h
667e7f75ad0SDave Kleikamp	ori	r11,r11, swapper_pg_dir@l
668e7f75ad0SDave Kleikamp	li	r12,0			/* MMUCR = 0 */
669e7f75ad0SDave Kleikamp	b	4f
670e7f75ad0SDave Kleikamp
671e7f75ad0SDave Kleikamp	/* Get the PGD for the current thread and setup MMUCR */
672e7f75ad0SDave Kleikamp3:	mfspr	r11,SPRN_SPRG_THREAD
673e7f75ad0SDave Kleikamp	lwz	r11,PGDIR(r11)
674e7f75ad0SDave Kleikamp	mfspr   r12,SPRN_PID		/* Get PID */
675e7f75ad0SDave Kleikamp4:	mtspr	SPRN_MMUCR,r12		/* Set MMUCR */
676e7f75ad0SDave Kleikamp
677e7f75ad0SDave Kleikamp	/* Make up the required permissions */
678e7f75ad0SDave Kleikamp	li	r13,_PAGE_PRESENT | _PAGE_ACCESSED | _PAGE_EXEC
679e7f75ad0SDave Kleikamp
680e7f75ad0SDave Kleikamp	/* Load PTE */
681e7f75ad0SDave Kleikamp	/* Compute pgdir/pmd offset */
682e7f75ad0SDave Kleikamp	rlwinm  r12,r10,PPC44x_PGD_OFF_SHIFT,PPC44x_PGD_OFF_MASK_BIT,29
683e7f75ad0SDave Kleikamp	lwzx	r11,r12,r11		/* Get pgd/pmd entry */
684e7f75ad0SDave Kleikamp
685e7f75ad0SDave Kleikamp	/* Word 0 is EPN,V,TS,DSIZ */
686e7f75ad0SDave Kleikamp	li	r12,PPC47x_TLB0_VALID | PPC47x_TLBE_SIZE
687e7f75ad0SDave Kleikamp	rlwimi	r10,r12,0,32-PAGE_SHIFT,31	/* Insert valid and page size*/
688e7f75ad0SDave Kleikamp	li	r12,0
689e7f75ad0SDave Kleikamp	tlbwe	r10,r12,0
690e7f75ad0SDave Kleikamp
691e7f75ad0SDave Kleikamp	/* XXX can we do better ? Need to make sure tlbwe has established
692e7f75ad0SDave Kleikamp	 * latch V bit in MMUCR0 before the PTE is loaded further down */
693e7f75ad0SDave Kleikamp#ifdef CONFIG_SMP
694e7f75ad0SDave Kleikamp	isync
695e7f75ad0SDave Kleikamp#endif
696e7f75ad0SDave Kleikamp
697e7f75ad0SDave Kleikamp	rlwinm.	r12,r11,0,0,20		/* Extract pt base address */
698e7f75ad0SDave Kleikamp	/* Compute pte address */
699e7f75ad0SDave Kleikamp	rlwimi  r12,r10,PPC44x_PTE_ADD_SHIFT,PPC44x_PTE_ADD_MASK_BIT,28
700e7f75ad0SDave Kleikamp	beq	2f			/* Bail if no table */
701e7f75ad0SDave Kleikamp
702e7f75ad0SDave Kleikamp	lwz	r11,0(r12)		/* Get high word of pte entry */
703e7f75ad0SDave Kleikamp	/* XXX can we do better ? maybe insert a known 0 bit from r11 into the
704e7f75ad0SDave Kleikamp	 * bottom of r12 to create a data dependency... We can also use r10
705e7f75ad0SDave Kleikamp	 * as destination nowadays
706e7f75ad0SDave Kleikamp	 */
707e7f75ad0SDave Kleikamp#ifdef CONFIG_SMP
708e7f75ad0SDave Kleikamp	lwsync
709e7f75ad0SDave Kleikamp#endif
710e7f75ad0SDave Kleikamp	lwz	r12,4(r12)		/* Get low word of pte entry */
711e7f75ad0SDave Kleikamp
712e7f75ad0SDave Kleikamp	andc.	r13,r13,r12		/* Check permission */
713e7f75ad0SDave Kleikamp
714e7f75ad0SDave Kleikamp	/* Jump to common TLB load point */
715e7f75ad0SDave Kleikamp	beq	finish_tlb_load_47x
716e7f75ad0SDave Kleikamp
717e7f75ad0SDave Kleikamp2:	/* The bailout.  Restore registers to pre-exception conditions
718e7f75ad0SDave Kleikamp	 * and call the heavyweights to help us out.
719e7f75ad0SDave Kleikamp	 */
720e7f75ad0SDave Kleikamp	mfspr	r11, SPRN_SPRG_RSCRATCH4
721e7f75ad0SDave Kleikamp	mtcr	r11
722e7f75ad0SDave Kleikamp	mfspr	r13, SPRN_SPRG_RSCRATCH3
723e7f75ad0SDave Kleikamp	mfspr	r12, SPRN_SPRG_RSCRATCH2
724e7f75ad0SDave Kleikamp	mfspr	r11, SPRN_SPRG_RSCRATCH1
725e7f75ad0SDave Kleikamp	mfspr	r10, SPRN_SPRG_RSCRATCH0
726e7f75ad0SDave Kleikamp	b	InstructionStorage
727e7f75ad0SDave Kleikamp
728e7f75ad0SDave Kleikamp/*
729e7f75ad0SDave Kleikamp * Both the instruction and data TLB miss get to this
730e7f75ad0SDave Kleikamp * point to load the TLB.
731e7f75ad0SDave Kleikamp * 	r10 - free to use
732e7f75ad0SDave Kleikamp * 	r11 - PTE high word value
733e7f75ad0SDave Kleikamp *	r12 - PTE low word value
734e7f75ad0SDave Kleikamp *      r13 - free to use
735e7f75ad0SDave Kleikamp *	MMUCR - loaded with proper value when we get here
736e7f75ad0SDave Kleikamp *	Upon exit, we reload everything and RFI.
737e7f75ad0SDave Kleikamp */
738e7f75ad0SDave Kleikampfinish_tlb_load_47x:
739e7f75ad0SDave Kleikamp	/* Combine RPN & ERPN an write WS 1 */
740e7f75ad0SDave Kleikamp	rlwimi	r11,r12,0,0,31-PAGE_SHIFT
741e7f75ad0SDave Kleikamp	tlbwe	r11,r13,1
742e7f75ad0SDave Kleikamp
743e7f75ad0SDave Kleikamp	/* And make up word 2 */
744e7f75ad0SDave Kleikamp	li	r10,0xf85			/* Mask to apply from PTE */
745e7f75ad0SDave Kleikamp	rlwimi	r10,r12,29,30,30		/* DIRTY -> SW position */
746e7f75ad0SDave Kleikamp	and	r11,r12,r10			/* Mask PTE bits to keep */
747e7f75ad0SDave Kleikamp	andi.	r10,r12,_PAGE_USER		/* User page ? */
748e7f75ad0SDave Kleikamp	beq	1f				/* nope, leave U bits empty */
749e7f75ad0SDave Kleikamp	rlwimi	r11,r11,3,26,28			/* yes, copy S bits to U */
750e7f75ad0SDave Kleikamp1:	tlbwe	r11,r13,2
751e7f75ad0SDave Kleikamp
752e7f75ad0SDave Kleikamp	/* Done...restore registers and get out of here.
753e7f75ad0SDave Kleikamp	*/
754e7f75ad0SDave Kleikamp	mfspr	r11, SPRN_SPRG_RSCRATCH4
755e7f75ad0SDave Kleikamp	mtcr	r11
756e7f75ad0SDave Kleikamp	mfspr	r13, SPRN_SPRG_RSCRATCH3
757e7f75ad0SDave Kleikamp	mfspr	r12, SPRN_SPRG_RSCRATCH2
758e7f75ad0SDave Kleikamp	mfspr	r11, SPRN_SPRG_RSCRATCH1
759e7f75ad0SDave Kleikamp	mfspr	r10, SPRN_SPRG_RSCRATCH0
760e7f75ad0SDave Kleikamp	rfi
761e7f75ad0SDave Kleikamp
762e7f75ad0SDave Kleikamp#endif /* CONFIG_PPC_47x */
763e7f75ad0SDave Kleikamp
764e7f75ad0SDave Kleikamp	/* Debug Interrupt */
765e7f75ad0SDave Kleikamp	/*
766e7f75ad0SDave Kleikamp	 * This statement needs to exist at the end of the IVPR
767e7f75ad0SDave Kleikamp	 * definition just in case you end up taking a debug
768e7f75ad0SDave Kleikamp	 * exception within another exception.
769e7f75ad0SDave Kleikamp	 */
770e7f75ad0SDave Kleikamp	DEBUG_CRIT_EXCEPTION
771e7f75ad0SDave Kleikamp
772fc2a6cfeSBharat Bhushaninterrupt_end:
773fc2a6cfeSBharat Bhushan
77414cf11afSPaul Mackerras/*
77514cf11afSPaul Mackerras * Global functions
77614cf11afSPaul Mackerras */
77714cf11afSPaul Mackerras
77814cf11afSPaul Mackerras/*
77947c0bd1aSBenjamin Herrenschmidt * Adjust the machine check IVOR on 440A cores
78047c0bd1aSBenjamin Herrenschmidt */
78147c0bd1aSBenjamin Herrenschmidt_GLOBAL(__fixup_440A_mcheck)
78247c0bd1aSBenjamin Herrenschmidt	li	r3,MachineCheckA@l
78347c0bd1aSBenjamin Herrenschmidt	mtspr	SPRN_IVOR1,r3
78447c0bd1aSBenjamin Herrenschmidt	sync
78547c0bd1aSBenjamin Herrenschmidt	blr
78647c0bd1aSBenjamin Herrenschmidt
78714cf11afSPaul Mackerras_GLOBAL(set_context)
78814cf11afSPaul Mackerras
78914cf11afSPaul Mackerras#ifdef CONFIG_BDI_SWITCH
79014cf11afSPaul Mackerras	/* Context switch the PTE pointer for the Abatron BDI2000.
79114cf11afSPaul Mackerras	 * The PGDIR is the second parameter.
79214cf11afSPaul Mackerras	 */
79314cf11afSPaul Mackerras	lis	r5, abatron_pteptrs@h
79414cf11afSPaul Mackerras	ori	r5, r5, abatron_pteptrs@l
79514cf11afSPaul Mackerras	stw	r4, 0x4(r5)
79614cf11afSPaul Mackerras#endif
79714cf11afSPaul Mackerras	mtspr	SPRN_PID,r3
79814cf11afSPaul Mackerras	isync			/* Force context change */
79914cf11afSPaul Mackerras	blr
80014cf11afSPaul Mackerras
80114cf11afSPaul Mackerras/*
802795033c3SDave Kleikamp * Init CPU state. This is called at boot time or for secondary CPUs
803795033c3SDave Kleikamp * to setup initial TLB entries, setup IVORs, etc...
804e7f75ad0SDave Kleikamp *
805795033c3SDave Kleikamp */
806795033c3SDave Kleikamp_GLOBAL(init_cpu_state)
807795033c3SDave Kleikamp	mflr	r22
808e7f75ad0SDave Kleikamp#ifdef CONFIG_PPC_47x
809446957baSAdam Buchbinder	/* We use the PVR to differentiate 44x cores from 476 */
810e7f75ad0SDave Kleikamp	mfspr	r3,SPRN_PVR
811e7f75ad0SDave Kleikamp	srwi	r3,r3,16
812df777bd3STony Breeds	cmplwi	cr0,r3,PVR_476FPE@h
813df777bd3STony Breeds	beq	head_start_47x
814e7f75ad0SDave Kleikamp	cmplwi	cr0,r3,PVR_476@h
815e7f75ad0SDave Kleikamp	beq	head_start_47x
816b4e8c8ddSTorez Smith	cmplwi	cr0,r3,PVR_476_ISS@h
817b4e8c8ddSTorez Smith	beq	head_start_47x
818e7f75ad0SDave Kleikamp#endif /* CONFIG_PPC_47x */
819e7f75ad0SDave Kleikamp
820795033c3SDave Kleikamp/*
821795033c3SDave Kleikamp * In case the firmware didn't do it, we apply some workarounds
822795033c3SDave Kleikamp * that are good for all 440 core variants here
823795033c3SDave Kleikamp */
824795033c3SDave Kleikamp	mfspr	r3,SPRN_CCR0
825795033c3SDave Kleikamp	rlwinm	r3,r3,0,0,27	/* disable icache prefetch */
826795033c3SDave Kleikamp	isync
827795033c3SDave Kleikamp	mtspr	SPRN_CCR0,r3
828795033c3SDave Kleikamp	isync
829795033c3SDave Kleikamp	sync
830795033c3SDave Kleikamp
831795033c3SDave Kleikamp/*
832e7f75ad0SDave Kleikamp * Set up the initial MMU state for 44x
833795033c3SDave Kleikamp *
834795033c3SDave Kleikamp * We are still executing code at the virtual address
835795033c3SDave Kleikamp * mappings set by the firmware for the base of RAM.
836795033c3SDave Kleikamp *
837795033c3SDave Kleikamp * We first invalidate all TLB entries but the one
838795033c3SDave Kleikamp * we are running from.  We then load the KERNELBASE
839795033c3SDave Kleikamp * mappings so we can begin to use kernel addresses
840795033c3SDave Kleikamp * natively and so the interrupt vector locations are
841795033c3SDave Kleikamp * permanently pinned (necessary since Book E
842795033c3SDave Kleikamp * implementations always have translation enabled).
843795033c3SDave Kleikamp *
844795033c3SDave Kleikamp * TODO: Use the known TLB entry we are running from to
845795033c3SDave Kleikamp *	 determine which physical region we are located
846795033c3SDave Kleikamp *	 in.  This can be used to determine where in RAM
847795033c3SDave Kleikamp *	 (on a shared CPU system) or PCI memory space
848795033c3SDave Kleikamp *	 (on a DRAMless system) we are located.
849795033c3SDave Kleikamp *       For now, we assume a perfect world which means
850795033c3SDave Kleikamp *	 we are located at the base of DRAM (physical 0).
851795033c3SDave Kleikamp */
852795033c3SDave Kleikamp
853795033c3SDave Kleikamp/*
854795033c3SDave Kleikamp * Search TLB for entry that we are currently using.
855795033c3SDave Kleikamp * Invalidate all entries but the one we are using.
856795033c3SDave Kleikamp */
857795033c3SDave Kleikamp	/* Load our current PID->MMUCR TID and MSR IS->MMUCR STS */
858795033c3SDave Kleikamp	mfspr	r3,SPRN_PID			/* Get PID */
859795033c3SDave Kleikamp	mfmsr	r4				/* Get MSR */
860795033c3SDave Kleikamp	andi.	r4,r4,MSR_IS@l			/* TS=1? */
861795033c3SDave Kleikamp	beq	wmmucr				/* If not, leave STS=0 */
862795033c3SDave Kleikamp	oris	r3,r3,PPC44x_MMUCR_STS@h	/* Set STS=1 */
863795033c3SDave Kleikampwmmucr:	mtspr	SPRN_MMUCR,r3			/* Put MMUCR */
864795033c3SDave Kleikamp	sync
865795033c3SDave Kleikamp
866795033c3SDave Kleikamp	bl	invstr				/* Find our address */
867795033c3SDave Kleikampinvstr:	mflr	r5				/* Make it accessible */
868795033c3SDave Kleikamp	tlbsx	r23,0,r5			/* Find entry we are in */
869795033c3SDave Kleikamp	li	r4,0				/* Start at TLB entry 0 */
870795033c3SDave Kleikamp	li	r3,0				/* Set PAGEID inval value */
871795033c3SDave Kleikamp1:	cmpw	r23,r4				/* Is this our entry? */
872795033c3SDave Kleikamp	beq	skpinv				/* If so, skip the inval */
873795033c3SDave Kleikamp	tlbwe	r3,r4,PPC44x_TLB_PAGEID		/* If not, inval the entry */
874795033c3SDave Kleikampskpinv:	addi	r4,r4,1				/* Increment */
875795033c3SDave Kleikamp	cmpwi	r4,64				/* Are we done? */
876795033c3SDave Kleikamp	bne	1b				/* If not, repeat */
877795033c3SDave Kleikamp	isync					/* If so, context change */
878795033c3SDave Kleikamp
879795033c3SDave Kleikamp/*
880795033c3SDave Kleikamp * Configure and load pinned entry into TLB slot 63.
881795033c3SDave Kleikamp */
88226ecb6c4SSuzuki Poulose#ifdef CONFIG_NONSTATIC_KERNEL
88326ecb6c4SSuzuki Poulose	/*
88426ecb6c4SSuzuki Poulose	 * In case of a NONSTATIC_KERNEL we reuse the TLB XLAT
88526ecb6c4SSuzuki Poulose	 * entries of the initial mapping set by the boot loader.
88626ecb6c4SSuzuki Poulose	 * The XLAT entry is stored in r25
88726ecb6c4SSuzuki Poulose	 */
88823913245SSuzuki Poulose
88923913245SSuzuki Poulose	/* Read the XLAT entry for our current mapping */
89023913245SSuzuki Poulose	tlbre	r25,r23,PPC44x_TLB_XLAT
89123913245SSuzuki Poulose
89223913245SSuzuki Poulose	lis	r3,KERNELBASE@h
89323913245SSuzuki Poulose	ori	r3,r3,KERNELBASE@l
89423913245SSuzuki Poulose
89523913245SSuzuki Poulose	/* Use our current RPN entry */
89623913245SSuzuki Poulose	mr	r4,r25
89723913245SSuzuki Poulose#else
898795033c3SDave Kleikamp
899795033c3SDave Kleikamp	lis	r3,PAGE_OFFSET@h
900795033c3SDave Kleikamp	ori	r3,r3,PAGE_OFFSET@l
901795033c3SDave Kleikamp
902795033c3SDave Kleikamp	/* Kernel is at the base of RAM */
903795033c3SDave Kleikamp	li r4, 0			/* Load the kernel physical address */
90423913245SSuzuki Poulose#endif
905795033c3SDave Kleikamp
906795033c3SDave Kleikamp	/* Load the kernel PID = 0 */
907795033c3SDave Kleikamp	li	r0,0
908795033c3SDave Kleikamp	mtspr	SPRN_PID,r0
909795033c3SDave Kleikamp	sync
910795033c3SDave Kleikamp
911795033c3SDave Kleikamp	/* Initialize MMUCR */
912795033c3SDave Kleikamp	li	r5,0
913795033c3SDave Kleikamp	mtspr	SPRN_MMUCR,r5
914795033c3SDave Kleikamp	sync
915795033c3SDave Kleikamp
916795033c3SDave Kleikamp	/* pageid fields */
917795033c3SDave Kleikamp	clrrwi	r3,r3,10		/* Mask off the effective page number */
918795033c3SDave Kleikamp	ori	r3,r3,PPC44x_TLB_VALID | PPC44x_TLB_256M
919795033c3SDave Kleikamp
920795033c3SDave Kleikamp	/* xlat fields */
921795033c3SDave Kleikamp	clrrwi	r4,r4,10		/* Mask off the real page number */
922795033c3SDave Kleikamp					/* ERPN is 0 for first 4GB page */
923795033c3SDave Kleikamp
924795033c3SDave Kleikamp	/* attrib fields */
925795033c3SDave Kleikamp	/* Added guarded bit to protect against speculative loads/stores */
926795033c3SDave Kleikamp	li	r5,0
927795033c3SDave Kleikamp	ori	r5,r5,(PPC44x_TLB_SW | PPC44x_TLB_SR | PPC44x_TLB_SX | PPC44x_TLB_G)
928795033c3SDave Kleikamp
929795033c3SDave Kleikamp        li      r0,63                    /* TLB slot 63 */
930795033c3SDave Kleikamp
931795033c3SDave Kleikamp	tlbwe	r3,r0,PPC44x_TLB_PAGEID	/* Load the pageid fields */
932795033c3SDave Kleikamp	tlbwe	r4,r0,PPC44x_TLB_XLAT	/* Load the translation fields */
933795033c3SDave Kleikamp	tlbwe	r5,r0,PPC44x_TLB_ATTRIB	/* Load the attrib/access fields */
934795033c3SDave Kleikamp
935795033c3SDave Kleikamp	/* Force context change */
936795033c3SDave Kleikamp	mfmsr	r0
937795033c3SDave Kleikamp	mtspr	SPRN_SRR1, r0
938795033c3SDave Kleikamp	lis	r0,3f@h
939795033c3SDave Kleikamp	ori	r0,r0,3f@l
940795033c3SDave Kleikamp	mtspr	SPRN_SRR0,r0
941795033c3SDave Kleikamp	sync
942795033c3SDave Kleikamp	rfi
943795033c3SDave Kleikamp
944795033c3SDave Kleikamp	/* If necessary, invalidate original entry we used */
945795033c3SDave Kleikamp3:	cmpwi	r23,63
946795033c3SDave Kleikamp	beq	4f
947795033c3SDave Kleikamp	li	r6,0
948795033c3SDave Kleikamp	tlbwe   r6,r23,PPC44x_TLB_PAGEID
949795033c3SDave Kleikamp	isync
950795033c3SDave Kleikamp
951795033c3SDave Kleikamp4:
952795033c3SDave Kleikamp#ifdef CONFIG_PPC_EARLY_DEBUG_44x
953795033c3SDave Kleikamp	/* Add UART mapping for early debug. */
954795033c3SDave Kleikamp
955795033c3SDave Kleikamp	/* pageid fields */
956795033c3SDave Kleikamp	lis	r3,PPC44x_EARLY_DEBUG_VIRTADDR@h
957795033c3SDave Kleikamp	ori	r3,r3,PPC44x_TLB_VALID|PPC44x_TLB_TS|PPC44x_TLB_64K
958795033c3SDave Kleikamp
959795033c3SDave Kleikamp	/* xlat fields */
960795033c3SDave Kleikamp	lis	r4,CONFIG_PPC_EARLY_DEBUG_44x_PHYSLOW@h
961795033c3SDave Kleikamp	ori	r4,r4,CONFIG_PPC_EARLY_DEBUG_44x_PHYSHIGH
962795033c3SDave Kleikamp
963795033c3SDave Kleikamp	/* attrib fields */
964795033c3SDave Kleikamp	li	r5,(PPC44x_TLB_SW|PPC44x_TLB_SR|PPC44x_TLB_I|PPC44x_TLB_G)
965795033c3SDave Kleikamp        li      r0,62                    /* TLB slot 0 */
966795033c3SDave Kleikamp
967795033c3SDave Kleikamp	tlbwe	r3,r0,PPC44x_TLB_PAGEID
968795033c3SDave Kleikamp	tlbwe	r4,r0,PPC44x_TLB_XLAT
969795033c3SDave Kleikamp	tlbwe	r5,r0,PPC44x_TLB_ATTRIB
970795033c3SDave Kleikamp
971795033c3SDave Kleikamp	/* Force context change */
972795033c3SDave Kleikamp	isync
973795033c3SDave Kleikamp#endif /* CONFIG_PPC_EARLY_DEBUG_44x */
974795033c3SDave Kleikamp
975795033c3SDave Kleikamp	/* Establish the interrupt vector offsets */
976795033c3SDave Kleikamp	SET_IVOR(0,  CriticalInput);
977795033c3SDave Kleikamp	SET_IVOR(1,  MachineCheck);
978795033c3SDave Kleikamp	SET_IVOR(2,  DataStorage);
979795033c3SDave Kleikamp	SET_IVOR(3,  InstructionStorage);
980795033c3SDave Kleikamp	SET_IVOR(4,  ExternalInput);
981795033c3SDave Kleikamp	SET_IVOR(5,  Alignment);
982795033c3SDave Kleikamp	SET_IVOR(6,  Program);
983795033c3SDave Kleikamp	SET_IVOR(7,  FloatingPointUnavailable);
984795033c3SDave Kleikamp	SET_IVOR(8,  SystemCall);
985795033c3SDave Kleikamp	SET_IVOR(9,  AuxillaryProcessorUnavailable);
986795033c3SDave Kleikamp	SET_IVOR(10, Decrementer);
987795033c3SDave Kleikamp	SET_IVOR(11, FixedIntervalTimer);
988795033c3SDave Kleikamp	SET_IVOR(12, WatchdogTimer);
989e7f75ad0SDave Kleikamp	SET_IVOR(13, DataTLBError44x);
990e7f75ad0SDave Kleikamp	SET_IVOR(14, InstructionTLBError44x);
991795033c3SDave Kleikamp	SET_IVOR(15, DebugCrit);
992795033c3SDave Kleikamp
993e7f75ad0SDave Kleikamp	b	head_start_common
994e7f75ad0SDave Kleikamp
995e7f75ad0SDave Kleikamp
996e7f75ad0SDave Kleikamp#ifdef CONFIG_PPC_47x
997e7f75ad0SDave Kleikamp
998e7f75ad0SDave Kleikamp#ifdef CONFIG_SMP
999e7f75ad0SDave Kleikamp
1000e7f75ad0SDave Kleikamp/* Entry point for secondary 47x processors */
1001e7f75ad0SDave Kleikamp_GLOBAL(start_secondary_47x)
1002e7f75ad0SDave Kleikamp        mr      r24,r3          /* CPU number */
1003e7f75ad0SDave Kleikamp
1004e7f75ad0SDave Kleikamp	bl	init_cpu_state
1005e7f75ad0SDave Kleikamp
1006e7f75ad0SDave Kleikamp	/* Now we need to bolt the rest of kernel memory which
1007e7f75ad0SDave Kleikamp	 * is done in C code. We must be careful because our task
1008e7f75ad0SDave Kleikamp	 * struct or our stack can (and will probably) be out
1009e7f75ad0SDave Kleikamp	 * of reach of the initial 256M TLB entry, so we use a
1010e7f75ad0SDave Kleikamp	 * small temporary stack in .bss for that. This works
1011e7f75ad0SDave Kleikamp	 * because only one CPU at a time can be in this code
1012e7f75ad0SDave Kleikamp	 */
1013e7f75ad0SDave Kleikamp	lis	r1,temp_boot_stack@h
1014e7f75ad0SDave Kleikamp	ori	r1,r1,temp_boot_stack@l
1015e7f75ad0SDave Kleikamp	addi	r1,r1,1024-STACK_FRAME_OVERHEAD
1016e7f75ad0SDave Kleikamp	li	r0,0
1017e7f75ad0SDave Kleikamp	stw	r0,0(r1)
1018e7f75ad0SDave Kleikamp	bl	mmu_init_secondary
1019e7f75ad0SDave Kleikamp
1020e7f75ad0SDave Kleikamp	/* Now we can get our task struct and real stack pointer */
1021e7f75ad0SDave Kleikamp
1022*4e67bfd7SChristophe Leroy	/* Get current's stack and current */
1023e7f75ad0SDave Kleikamp	lis	r1,secondary_ti@ha
1024e7f75ad0SDave Kleikamp	lwz	r1,secondary_ti@l(r1)
1025e7f75ad0SDave Kleikamp	lwz	r2,TI_TASK(r1)
1026e7f75ad0SDave Kleikamp
1027e7f75ad0SDave Kleikamp	/* Current stack pointer */
1028e7f75ad0SDave Kleikamp	addi	r1,r1,THREAD_SIZE-STACK_FRAME_OVERHEAD
1029e7f75ad0SDave Kleikamp	li	r0,0
1030e7f75ad0SDave Kleikamp	stw	r0,0(r1)
1031e7f75ad0SDave Kleikamp
1032e7f75ad0SDave Kleikamp	/* Kernel stack for exception entry in SPRG3 */
1033e7f75ad0SDave Kleikamp	addi	r4,r2,THREAD	/* init task's THREAD */
1034e7f75ad0SDave Kleikamp	mtspr	SPRN_SPRG3,r4
1035e7f75ad0SDave Kleikamp
1036e7f75ad0SDave Kleikamp	b	start_secondary
1037e7f75ad0SDave Kleikamp
1038e7f75ad0SDave Kleikamp#endif /* CONFIG_SMP */
1039e7f75ad0SDave Kleikamp
1040e7f75ad0SDave Kleikamp/*
1041e7f75ad0SDave Kleikamp * Set up the initial MMU state for 44x
1042e7f75ad0SDave Kleikamp *
1043e7f75ad0SDave Kleikamp * We are still executing code at the virtual address
1044e7f75ad0SDave Kleikamp * mappings set by the firmware for the base of RAM.
1045e7f75ad0SDave Kleikamp */
1046e7f75ad0SDave Kleikamp
1047e7f75ad0SDave Kleikamphead_start_47x:
1048e7f75ad0SDave Kleikamp	/* Load our current PID->MMUCR TID and MSR IS->MMUCR STS */
1049e7f75ad0SDave Kleikamp	mfspr	r3,SPRN_PID			/* Get PID */
1050e7f75ad0SDave Kleikamp	mfmsr	r4				/* Get MSR */
1051e7f75ad0SDave Kleikamp	andi.	r4,r4,MSR_IS@l			/* TS=1? */
1052e7f75ad0SDave Kleikamp	beq	1f				/* If not, leave STS=0 */
1053e7f75ad0SDave Kleikamp	oris	r3,r3,PPC47x_MMUCR_STS@h	/* Set STS=1 */
1054e7f75ad0SDave Kleikamp1:	mtspr	SPRN_MMUCR,r3			/* Put MMUCR */
1055e7f75ad0SDave Kleikamp	sync
1056e7f75ad0SDave Kleikamp
1057e7f75ad0SDave Kleikamp	/* Find the entry we are running from */
1058e7f75ad0SDave Kleikamp	bl	1f
1059e7f75ad0SDave Kleikamp1:	mflr	r23
1060e7f75ad0SDave Kleikamp	tlbsx	r23,0,r23
1061e7f75ad0SDave Kleikamp	tlbre	r24,r23,0
1062e7f75ad0SDave Kleikamp	tlbre	r25,r23,1
1063e7f75ad0SDave Kleikamp	tlbre	r26,r23,2
1064e7f75ad0SDave Kleikamp
1065e7f75ad0SDave Kleikamp/*
1066e7f75ad0SDave Kleikamp * Cleanup time
1067e7f75ad0SDave Kleikamp */
1068e7f75ad0SDave Kleikamp
1069e7f75ad0SDave Kleikamp	/* Initialize MMUCR */
1070e7f75ad0SDave Kleikamp	li	r5,0
1071e7f75ad0SDave Kleikamp	mtspr	SPRN_MMUCR,r5
1072e7f75ad0SDave Kleikamp	sync
1073e7f75ad0SDave Kleikamp
1074e7f75ad0SDave Kleikampclear_all_utlb_entries:
1075e7f75ad0SDave Kleikamp
1076e7f75ad0SDave Kleikamp	#; Set initial values.
1077e7f75ad0SDave Kleikamp
1078e7f75ad0SDave Kleikamp	addis		r3,0,0x8000
1079e7f75ad0SDave Kleikamp	addi		r4,0,0
1080e7f75ad0SDave Kleikamp	addi		r5,0,0
1081e7f75ad0SDave Kleikamp	b		clear_utlb_entry
1082e7f75ad0SDave Kleikamp
1083e7f75ad0SDave Kleikamp	#; Align the loop to speed things up.
1084e7f75ad0SDave Kleikamp
1085e7f75ad0SDave Kleikamp	.align		6
1086e7f75ad0SDave Kleikamp
1087e7f75ad0SDave Kleikampclear_utlb_entry:
1088e7f75ad0SDave Kleikamp
1089e7f75ad0SDave Kleikamp	tlbwe		r4,r3,0
1090e7f75ad0SDave Kleikamp	tlbwe		r5,r3,1
1091e7f75ad0SDave Kleikamp	tlbwe		r5,r3,2
1092e7f75ad0SDave Kleikamp	addis		r3,r3,0x2000
1093e7f75ad0SDave Kleikamp	cmpwi		r3,0
1094e7f75ad0SDave Kleikamp	bne		clear_utlb_entry
1095e7f75ad0SDave Kleikamp	addis		r3,0,0x8000
1096e7f75ad0SDave Kleikamp	addis		r4,r4,0x100
1097e7f75ad0SDave Kleikamp	cmpwi		r4,0
1098e7f75ad0SDave Kleikamp	bne		clear_utlb_entry
1099e7f75ad0SDave Kleikamp
1100e7f75ad0SDave Kleikamp	#; Restore original entry.
1101e7f75ad0SDave Kleikamp
1102e7f75ad0SDave Kleikamp	oris	r23,r23,0x8000  /* specify the way */
1103e7f75ad0SDave Kleikamp	tlbwe		r24,r23,0
1104e7f75ad0SDave Kleikamp	tlbwe		r25,r23,1
1105e7f75ad0SDave Kleikamp	tlbwe		r26,r23,2
1106e7f75ad0SDave Kleikamp
1107e7f75ad0SDave Kleikamp/*
1108e7f75ad0SDave Kleikamp * Configure and load pinned entry into TLB for the kernel core
1109e7f75ad0SDave Kleikamp */
1110e7f75ad0SDave Kleikamp
1111e7f75ad0SDave Kleikamp	lis	r3,PAGE_OFFSET@h
1112e7f75ad0SDave Kleikamp	ori	r3,r3,PAGE_OFFSET@l
1113e7f75ad0SDave Kleikamp
1114e7f75ad0SDave Kleikamp	/* Load the kernel PID = 0 */
1115e7f75ad0SDave Kleikamp	li	r0,0
1116e7f75ad0SDave Kleikamp	mtspr	SPRN_PID,r0
1117e7f75ad0SDave Kleikamp	sync
1118e7f75ad0SDave Kleikamp
1119e7f75ad0SDave Kleikamp	/* Word 0 */
1120e7f75ad0SDave Kleikamp	clrrwi	r3,r3,12		/* Mask off the effective page number */
1121e7f75ad0SDave Kleikamp	ori	r3,r3,PPC47x_TLB0_VALID | PPC47x_TLB0_256M
1122e7f75ad0SDave Kleikamp
11239661534dSDave Kleikamp	/* Word 1 - use r25.  RPN is the same as the original entry */
11249661534dSDave Kleikamp
1125e7f75ad0SDave Kleikamp	/* Word 2 */
1126e7f75ad0SDave Kleikamp	li	r5,0
1127e7f75ad0SDave Kleikamp	ori	r5,r5,PPC47x_TLB2_S_RWX
1128e7f75ad0SDave Kleikamp#ifdef CONFIG_SMP
1129e7f75ad0SDave Kleikamp	ori	r5,r5,PPC47x_TLB2_M
1130e7f75ad0SDave Kleikamp#endif
1131e7f75ad0SDave Kleikamp
1132e7f75ad0SDave Kleikamp	/* We write to way 0 and bolted 0 */
1133e7f75ad0SDave Kleikamp	lis	r0,0x8800
1134e7f75ad0SDave Kleikamp	tlbwe	r3,r0,0
11359661534dSDave Kleikamp	tlbwe	r25,r0,1
1136e7f75ad0SDave Kleikamp	tlbwe	r5,r0,2
1137e7f75ad0SDave Kleikamp
1138e7f75ad0SDave Kleikamp/*
1139e7f75ad0SDave Kleikamp * Configure SSPCR, ISPCR and USPCR for now to search everything, we can fix
1140e7f75ad0SDave Kleikamp * them up later
1141e7f75ad0SDave Kleikamp */
1142e7f75ad0SDave Kleikamp	LOAD_REG_IMMEDIATE(r3, 0x9abcdef0)
1143e7f75ad0SDave Kleikamp	mtspr	SPRN_SSPCR,r3
1144e7f75ad0SDave Kleikamp	mtspr	SPRN_USPCR,r3
1145e7f75ad0SDave Kleikamp	LOAD_REG_IMMEDIATE(r3, 0x12345670)
1146e7f75ad0SDave Kleikamp	mtspr	SPRN_ISPCR,r3
1147e7f75ad0SDave Kleikamp
1148e7f75ad0SDave Kleikamp	/* Force context change */
1149e7f75ad0SDave Kleikamp	mfmsr	r0
1150e7f75ad0SDave Kleikamp	mtspr	SPRN_SRR1, r0
1151e7f75ad0SDave Kleikamp	lis	r0,3f@h
1152e7f75ad0SDave Kleikamp	ori	r0,r0,3f@l
1153e7f75ad0SDave Kleikamp	mtspr	SPRN_SRR0,r0
1154e7f75ad0SDave Kleikamp	sync
1155e7f75ad0SDave Kleikamp	rfi
1156e7f75ad0SDave Kleikamp
1157e7f75ad0SDave Kleikamp	/* Invalidate original entry we used */
1158e7f75ad0SDave Kleikamp3:
1159e7f75ad0SDave Kleikamp	rlwinm	r24,r24,0,21,19 /* clear the "valid" bit */
1160e7f75ad0SDave Kleikamp	tlbwe	r24,r23,0
1161e7f75ad0SDave Kleikamp	addi	r24,0,0
1162e7f75ad0SDave Kleikamp	tlbwe	r24,r23,1
1163e7f75ad0SDave Kleikamp	tlbwe	r24,r23,2
1164e7f75ad0SDave Kleikamp	isync                   /* Clear out the shadow TLB entries */
1165e7f75ad0SDave Kleikamp
1166e7f75ad0SDave Kleikamp#ifdef CONFIG_PPC_EARLY_DEBUG_44x
1167e7f75ad0SDave Kleikamp	/* Add UART mapping for early debug. */
1168e7f75ad0SDave Kleikamp
1169e7f75ad0SDave Kleikamp	/* Word 0 */
1170e7f75ad0SDave Kleikamp	lis	r3,PPC44x_EARLY_DEBUG_VIRTADDR@h
1171e7f75ad0SDave Kleikamp	ori	r3,r3,PPC47x_TLB0_VALID | PPC47x_TLB0_TS | PPC47x_TLB0_1M
1172e7f75ad0SDave Kleikamp
1173e7f75ad0SDave Kleikamp	/* Word 1 */
1174e7f75ad0SDave Kleikamp	lis	r4,CONFIG_PPC_EARLY_DEBUG_44x_PHYSLOW@h
1175e7f75ad0SDave Kleikamp	ori	r4,r4,CONFIG_PPC_EARLY_DEBUG_44x_PHYSHIGH
1176e7f75ad0SDave Kleikamp
1177e7f75ad0SDave Kleikamp	/* Word 2 */
1178e7f75ad0SDave Kleikamp	li	r5,(PPC47x_TLB2_S_RW | PPC47x_TLB2_IMG)
1179e7f75ad0SDave Kleikamp
1180e7f75ad0SDave Kleikamp	/* Bolted in way 0, bolt slot 5, we -hope- we don't hit the same
1181e7f75ad0SDave Kleikamp	 * congruence class as the kernel, we need to make sure of it at
1182e7f75ad0SDave Kleikamp	 * some point
1183e7f75ad0SDave Kleikamp	 */
1184e7f75ad0SDave Kleikamp        lis	r0,0x8d00
1185e7f75ad0SDave Kleikamp	tlbwe	r3,r0,0
1186e7f75ad0SDave Kleikamp	tlbwe	r4,r0,1
1187e7f75ad0SDave Kleikamp	tlbwe	r5,r0,2
1188e7f75ad0SDave Kleikamp
1189e7f75ad0SDave Kleikamp	/* Force context change */
1190e7f75ad0SDave Kleikamp	isync
1191e7f75ad0SDave Kleikamp#endif /* CONFIG_PPC_EARLY_DEBUG_44x */
1192e7f75ad0SDave Kleikamp
1193e7f75ad0SDave Kleikamp	/* Establish the interrupt vector offsets */
1194e7f75ad0SDave Kleikamp	SET_IVOR(0,  CriticalInput);
1195e7f75ad0SDave Kleikamp	SET_IVOR(1,  MachineCheckA);
1196e7f75ad0SDave Kleikamp	SET_IVOR(2,  DataStorage);
1197e7f75ad0SDave Kleikamp	SET_IVOR(3,  InstructionStorage);
1198e7f75ad0SDave Kleikamp	SET_IVOR(4,  ExternalInput);
1199e7f75ad0SDave Kleikamp	SET_IVOR(5,  Alignment);
1200e7f75ad0SDave Kleikamp	SET_IVOR(6,  Program);
1201e7f75ad0SDave Kleikamp	SET_IVOR(7,  FloatingPointUnavailable);
1202e7f75ad0SDave Kleikamp	SET_IVOR(8,  SystemCall);
1203e7f75ad0SDave Kleikamp	SET_IVOR(9,  AuxillaryProcessorUnavailable);
1204e7f75ad0SDave Kleikamp	SET_IVOR(10, Decrementer);
1205e7f75ad0SDave Kleikamp	SET_IVOR(11, FixedIntervalTimer);
1206e7f75ad0SDave Kleikamp	SET_IVOR(12, WatchdogTimer);
1207e7f75ad0SDave Kleikamp	SET_IVOR(13, DataTLBError47x);
1208e7f75ad0SDave Kleikamp	SET_IVOR(14, InstructionTLBError47x);
1209e7f75ad0SDave Kleikamp	SET_IVOR(15, DebugCrit);
1210e7f75ad0SDave Kleikamp
1211e7f75ad0SDave Kleikamp	/* We configure icbi to invalidate 128 bytes at a time since the
1212e7f75ad0SDave Kleikamp	 * current 32-bit kernel code isn't too happy with icache != dcache
121397b3be1eSAlistair Popple	 * block size. We also disable the BTAC as this can cause errors
121497b3be1eSAlistair Popple	 * in some circumstances (see IBM Erratum 47).
1215e7f75ad0SDave Kleikamp	 */
1216e7f75ad0SDave Kleikamp	mfspr	r3,SPRN_CCR0
1217e7f75ad0SDave Kleikamp	oris	r3,r3,0x0020
121897b3be1eSAlistair Popple	ori	r3,r3,0x0040
1219e7f75ad0SDave Kleikamp	mtspr	SPRN_CCR0,r3
1220e7f75ad0SDave Kleikamp	isync
1221e7f75ad0SDave Kleikamp
1222e7f75ad0SDave Kleikamp#endif /* CONFIG_PPC_47x */
1223e7f75ad0SDave Kleikamp
1224e7f75ad0SDave Kleikamp/*
1225e7f75ad0SDave Kleikamp * Here we are back to code that is common between 44x and 47x
1226e7f75ad0SDave Kleikamp *
1227e7f75ad0SDave Kleikamp * We proceed to further kernel initialization and return to the
1228e7f75ad0SDave Kleikamp * main kernel entry
1229e7f75ad0SDave Kleikamp */
1230e7f75ad0SDave Kleikamphead_start_common:
1231795033c3SDave Kleikamp	/* Establish the interrupt vector base */
1232795033c3SDave Kleikamp	lis	r4,interrupt_base@h	/* IVPR only uses the high 16-bits */
1233795033c3SDave Kleikamp	mtspr	SPRN_IVPR,r4
1234795033c3SDave Kleikamp
12359661534dSDave Kleikamp	/*
12369661534dSDave Kleikamp	 * If the kernel was loaded at a non-zero 256 MB page, we need to
12379661534dSDave Kleikamp	 * mask off the most significant 4 bits to get the relative address
12389661534dSDave Kleikamp	 * from the start of physical memory
12399661534dSDave Kleikamp	 */
12409661534dSDave Kleikamp	rlwinm	r22,r22,0,4,31
12419661534dSDave Kleikamp	addis	r22,r22,PAGE_OFFSET@h
1242795033c3SDave Kleikamp	mtlr	r22
1243e7f75ad0SDave Kleikamp	isync
1244795033c3SDave Kleikamp	blr
1245795033c3SDave Kleikamp
1246795033c3SDave Kleikamp/*
124714cf11afSPaul Mackerras * We put a few things here that have to be page-aligned. This stuff
124814cf11afSPaul Mackerras * goes at the beginning of the data segment, which is page-aligned.
124914cf11afSPaul Mackerras */
125014cf11afSPaul Mackerras	.data
1251ca9153a3SIlya Yanok	.align	PAGE_SHIFT
1252ea703ce2SKumar Gala	.globl	sdata
1253ea703ce2SKumar Galasdata:
1254ea703ce2SKumar Gala	.globl	empty_zero_page
1255ea703ce2SKumar Galaempty_zero_page:
1256ca9153a3SIlya Yanok	.space	PAGE_SIZE
12579445aa1aSAl ViroEXPORT_SYMBOL(empty_zero_page)
125814cf11afSPaul Mackerras
125914cf11afSPaul Mackerras/*
126014cf11afSPaul Mackerras * To support >32-bit physical addresses, we use an 8KB pgdir.
126114cf11afSPaul Mackerras */
1262ea703ce2SKumar Gala	.globl	swapper_pg_dir
1263ea703ce2SKumar Galaswapper_pg_dir:
1264bee86f14SKumar Gala	.space	PGD_TABLE_SIZE
126514cf11afSPaul Mackerras
126614cf11afSPaul Mackerras/*
126714cf11afSPaul Mackerras * Room for two PTE pointers, usually the kernel and current user pointers
126814cf11afSPaul Mackerras * to their respective root page table.
126914cf11afSPaul Mackerras */
127014cf11afSPaul Mackerrasabatron_pteptrs:
127114cf11afSPaul Mackerras	.space	8
1272e7f75ad0SDave Kleikamp
1273e7f75ad0SDave Kleikamp#ifdef CONFIG_SMP
1274e7f75ad0SDave Kleikamp	.align	12
1275e7f75ad0SDave Kleikamptemp_boot_stack:
1276e7f75ad0SDave Kleikamp	.space	1024
1277e7f75ad0SDave Kleikamp#endif /* CONFIG_SMP */
1278