Linux Audio

Check our new training course

Loading...
v4.10.11
 
   1/*
   2 * OpenRISC head.S
   3 *
   4 * Linux architectural port borrowing liberally from similar works of
   5 * others.  All original copyrights apply as per the original source
   6 * declaration.
   7 *
   8 * Modifications for the OpenRISC architecture:
   9 * Copyright (C) 2003 Matjaz Breskvar <phoenix@bsemi.com>
  10 * Copyright (C) 2010-2011 Jonas Bonn <jonas@southpole.se>
  11 *
  12 *      This program is free software; you can redistribute it and/or
  13 *      modify it under the terms of the GNU General Public License
  14 *      as published by the Free Software Foundation; either version
  15 *      2 of the License, or (at your option) any later version.
  16 */
  17
  18#include <linux/linkage.h>
  19#include <linux/threads.h>
  20#include <linux/errno.h>
  21#include <linux/init.h>
  22#include <linux/serial_reg.h>
 
  23#include <asm/processor.h>
  24#include <asm/page.h>
  25#include <asm/mmu.h>
  26#include <asm/pgtable.h>
  27#include <asm/cache.h>
  28#include <asm/spr_defs.h>
  29#include <asm/asm-offsets.h>
  30#include <linux/of_fdt.h>
  31
  32#define tophys(rd,rs)				\
  33	l.movhi	rd,hi(-KERNELBASE)		;\
  34	l.add	rd,rd,rs
  35
  36#define CLEAR_GPR(gpr)				\
  37	l.or    gpr,r0,r0
  38
  39#define LOAD_SYMBOL_2_GPR(gpr,symbol)		\
  40	l.movhi gpr,hi(symbol)			;\
  41	l.ori   gpr,gpr,lo(symbol)
  42
  43
  44#define UART_BASE_ADD      0x90000000
  45
  46#define EXCEPTION_SR  (SPR_SR_DME | SPR_SR_IME | SPR_SR_DCE | SPR_SR_ICE | SPR_SR_SM)
  47#define SYSCALL_SR  (SPR_SR_DME | SPR_SR_IME | SPR_SR_DCE | SPR_SR_ICE | SPR_SR_IEE | SPR_SR_TEE | SPR_SR_SM)
  48
  49/* ============================================[ tmp store locations ]=== */
  50
 
 
  51/*
  52 * emergency_print temporary stores
  53 */
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
  54#define EMERGENCY_PRINT_STORE_GPR4	l.sw    0x20(r0),r4
  55#define EMERGENCY_PRINT_LOAD_GPR4	l.lwz   r4,0x20(r0)
  56
  57#define EMERGENCY_PRINT_STORE_GPR5	l.sw    0x24(r0),r5
  58#define EMERGENCY_PRINT_LOAD_GPR5	l.lwz   r5,0x24(r0)
  59
  60#define EMERGENCY_PRINT_STORE_GPR6	l.sw    0x28(r0),r6
  61#define EMERGENCY_PRINT_LOAD_GPR6	l.lwz   r6,0x28(r0)
  62
  63#define EMERGENCY_PRINT_STORE_GPR7	l.sw    0x2c(r0),r7
  64#define EMERGENCY_PRINT_LOAD_GPR7	l.lwz   r7,0x2c(r0)
  65
  66#define EMERGENCY_PRINT_STORE_GPR8	l.sw    0x30(r0),r8
  67#define EMERGENCY_PRINT_LOAD_GPR8	l.lwz   r8,0x30(r0)
  68
  69#define EMERGENCY_PRINT_STORE_GPR9	l.sw    0x34(r0),r9
  70#define EMERGENCY_PRINT_LOAD_GPR9	l.lwz   r9,0x34(r0)
  71
 
  72
  73/*
  74 * TLB miss handlers temorary stores
  75 */
  76#define EXCEPTION_STORE_GPR9		l.sw    0x10(r0),r9
  77#define EXCEPTION_LOAD_GPR9		l.lwz   r9,0x10(r0)
 
 
 
 
  78
 
 
 
 
 
 
 
 
 
 
  79#define EXCEPTION_STORE_GPR2		l.sw    0x64(r0),r2
  80#define EXCEPTION_LOAD_GPR2		l.lwz   r2,0x64(r0)
  81
  82#define EXCEPTION_STORE_GPR3		l.sw    0x68(r0),r3
  83#define EXCEPTION_LOAD_GPR3		l.lwz   r3,0x68(r0)
  84
  85#define EXCEPTION_STORE_GPR4		l.sw    0x6c(r0),r4
  86#define EXCEPTION_LOAD_GPR4		l.lwz   r4,0x6c(r0)
  87
  88#define EXCEPTION_STORE_GPR5		l.sw    0x70(r0),r5
  89#define EXCEPTION_LOAD_GPR5		l.lwz   r5,0x70(r0)
  90
  91#define EXCEPTION_STORE_GPR6		l.sw    0x74(r0),r6
  92#define EXCEPTION_LOAD_GPR6		l.lwz   r6,0x74(r0)
  93
 
  94
  95/*
  96 * EXCEPTION_HANDLE temporary stores
  97 */
  98
 
 
 
 
 
 
 
 
 
 
 
  99#define EXCEPTION_T_STORE_GPR30		l.sw    0x78(r0),r30
 100#define EXCEPTION_T_LOAD_GPR30(reg)	l.lwz   reg,0x78(r0)
 101
 102#define EXCEPTION_T_STORE_GPR10		l.sw    0x7c(r0),r10
 103#define EXCEPTION_T_LOAD_GPR10(reg)	l.lwz   reg,0x7c(r0)
 104
 105#define EXCEPTION_T_STORE_SP		l.sw	0x80(r0),r1
 106#define EXCEPTION_T_LOAD_SP(reg)	l.lwz   reg,0x80(r0)
 107
 108/*
 109 * For UNHANLDED_EXCEPTION
 110 */
 111
 112#define EXCEPTION_T_STORE_GPR31		l.sw    0x84(r0),r31
 113#define EXCEPTION_T_LOAD_GPR31(reg)	l.lwz   reg,0x84(r0)
 114
 115/* =========================================================[ macros ]=== */
 116
 117
 
 
 
 
 
 
 
 
 118#define GET_CURRENT_PGD(reg,t1)					\
 119	LOAD_SYMBOL_2_GPR(reg,current_pgd)			;\
 120	tophys  (t1,reg)					;\
 121	l.lwz   reg,0(t1)
 
 122
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 123
 124/*
 125 * DSCR: this is a common hook for handling exceptions. it will save
 126 *       the needed registers, set up stack and pointer to current
 127 *	 then jump to the handler while enabling MMU
 128 *
 129 * PRMS: handler	- a function to jump to. it has to save the
 130 *			remaining registers to kernel stack, call
 131 *			appropriate arch-independant exception handler
 132 *			and finaly jump to ret_from_except
 133 *
 134 * PREQ: unchanged state from the time exception happened
 135 *
 136 * POST: SAVED the following registers original value
 137 *	       to the new created exception frame pointed to by r1
 138 *
 139 *	 r1  - ksp	pointing to the new (exception) frame
 140 *	 r4  - EEAR     exception EA
 141 *	 r10 - current	pointing to current_thread_info struct
 142 *	 r12 - syscall  0, since we didn't come from syscall
 143 *	 r13 - temp	it actually contains new SR, not needed anymore
 144 *	 r31 - handler	address of the handler we'll jump to
 145 *
 146 *	 handler has to save remaining registers to the exception
 147 *	 ksp frame *before* tainting them!
 148 *
 149 * NOTE: this function is not reentrant per se. reentrancy is guaranteed
 150 *       by processor disabling all exceptions/interrupts when exception
 151 *	 accours.
 152 *
 153 * OPTM: no need to make it so wasteful to extract ksp when in user mode
 154 */
 155
 156#define EXCEPTION_HANDLE(handler)				\
 157	EXCEPTION_T_STORE_GPR30					;\
 158	l.mfspr r30,r0,SPR_ESR_BASE				;\
 159	l.andi  r30,r30,SPR_SR_SM				;\
 160	l.sfeqi r30,0						;\
 161	EXCEPTION_T_STORE_GPR10					;\
 162	l.bnf   2f                            /* kernel_mode */	;\
 163	 EXCEPTION_T_STORE_SP                 /* delay slot */	;\
 1641: /* user_mode:   */						;\
 165	LOAD_SYMBOL_2_GPR(r1,current_thread_info_set)		;\
 166	tophys  (r30,r1)					;\
 167	/* r10: current_thread_info  */				;\
 168	l.lwz   r10,0(r30)					;\
 169	tophys  (r30,r10)					;\
 170	l.lwz   r1,(TI_KSP)(r30)				;\
 171	/* fall through */					;\
 1722: /* kernel_mode: */						;\
 173	/* create new stack frame, save only needed gprs */	;\
 174	/* r1: KSP, r10: current, r4: EEAR, r31: __pa(KSP) */	;\
 175	/* r12:	temp, syscall indicator */			;\
 176	l.addi  r1,r1,-(INT_FRAME_SIZE)				;\
 177	/* r1 is KSP, r30 is __pa(KSP) */			;\
 178	tophys  (r30,r1)					;\
 179	l.sw    PT_GPR12(r30),r12				;\
 
 180	l.mfspr r12,r0,SPR_EPCR_BASE				;\
 181	l.sw    PT_PC(r30),r12					;\
 182	l.mfspr r12,r0,SPR_ESR_BASE				;\
 183	l.sw    PT_SR(r30),r12					;\
 184	/* save r30 */						;\
 185	EXCEPTION_T_LOAD_GPR30(r12)				;\
 186	l.sw	PT_GPR30(r30),r12				;\
 187	/* save r10 as was prior to exception */		;\
 188	EXCEPTION_T_LOAD_GPR10(r12)				;\
 189	l.sw	PT_GPR10(r30),r12				;\
 190	/* save PT_SP as was prior to exception */		;\
 191	EXCEPTION_T_LOAD_SP(r12)				;\
 192	l.sw	PT_SP(r30),r12					;\
 193	/* save exception r4, set r4 = EA */			;\
 194	l.sw	PT_GPR4(r30),r4					;\
 195	l.mfspr r4,r0,SPR_EEAR_BASE				;\
 196	/* r12 == 1 if we come from syscall */			;\
 197	CLEAR_GPR(r12)						;\
 198	/* ----- turn on MMU ----- */				;\
 199	l.ori	r30,r0,(EXCEPTION_SR)				;\
 
 
 
 200	l.mtspr	r0,r30,SPR_ESR_BASE				;\
 201	/* r30:	EA address of handler */			;\
 202	LOAD_SYMBOL_2_GPR(r30,handler)				;\
 203	l.mtspr r0,r30,SPR_EPCR_BASE				;\
 204	l.rfe
 205
 206/*
 207 * this doesn't work
 208 *
 209 *
 210 * #ifdef CONFIG_JUMP_UPON_UNHANDLED_EXCEPTION
 211 * #define UNHANDLED_EXCEPTION(handler)				\
 212 *	l.ori   r3,r0,0x1					;\
 213 *	l.mtspr r0,r3,SPR_SR					;\
 214 *      l.movhi r3,hi(0xf0000100)				;\
 215 *      l.ori   r3,r3,lo(0xf0000100)				;\
 216 *	l.jr	r3						;\
 217 *	l.nop	1
 218 *
 219 * #endif
 220 */
 221
 222/* DSCR: this is the same as EXCEPTION_HANDLE(), we are just
 223 *       a bit more carefull (if we have a PT_SP or current pointer
 224 *       corruption) and set them up from 'current_set'
 225 *
 226 */
 227#define UNHANDLED_EXCEPTION(handler)				\
 228	EXCEPTION_T_STORE_GPR31					;\
 229	EXCEPTION_T_STORE_GPR10					;\
 230	EXCEPTION_T_STORE_SP					;\
 231	/* temporary store r3, r9 into r1, r10 */		;\
 232	l.addi	r1,r3,0x0					;\
 233	l.addi	r10,r9,0x0					;\
 234	/* the string referenced by r3 must be low enough */	;\
 
 235	l.jal	_emergency_print				;\
 236	l.ori	r3,r0,lo(_string_unhandled_exception)		;\
 237	l.mfspr	r3,r0,SPR_NPC					;\
 238	l.jal	_emergency_print_nr				;\
 239	l.andi	r3,r3,0x1f00					;\
 240	/* the string referenced by r3 must be low enough */	;\
 
 241	l.jal	_emergency_print				;\
 242	l.ori	r3,r0,lo(_string_epc_prefix)			;\
 243	l.jal	_emergency_print_nr				;\
 244	l.mfspr	r3,r0,SPR_EPCR_BASE				;\
 
 
 245	l.jal	_emergency_print				;\
 246	l.ori	r3,r0,lo(_string_nl)				;\
 247	/* end of printing */					;\
 248	l.addi	r3,r1,0x0					;\
 249	l.addi	r9,r10,0x0					;\
 250	/* extract current, ksp from current_set */		;\
 251	LOAD_SYMBOL_2_GPR(r1,_unhandled_stack_top)		;\
 252	LOAD_SYMBOL_2_GPR(r10,init_thread_union)		;\
 253	/* create new stack frame, save only needed gprs */	;\
 254	/* r1: KSP, r10: current, r31: __pa(KSP) */		;\
 255	/* r12:	temp, syscall indicator, r13 temp */		;\
 256	l.addi  r1,r1,-(INT_FRAME_SIZE)				;\
 257	/* r1 is KSP, r31 is __pa(KSP) */			;\
 258	tophys  (r31,r1)					;\
 259	l.sw    PT_GPR12(r31),r12					;\
 260	l.mfspr r12,r0,SPR_EPCR_BASE				;\
 261	l.sw    PT_PC(r31),r12					;\
 262	l.mfspr r12,r0,SPR_ESR_BASE				;\
 263	l.sw    PT_SR(r31),r12					;\
 264	/* save r31 */						;\
 265	EXCEPTION_T_LOAD_GPR31(r12)				;\
 266	l.sw	PT_GPR31(r31),r12					;\
 267	/* save r10 as was prior to exception */		;\
 268	EXCEPTION_T_LOAD_GPR10(r12)				;\
 269	l.sw	PT_GPR10(r31),r12					;\
 270	/* save PT_SP as was prior to exception */			;\
 271	EXCEPTION_T_LOAD_SP(r12)				;\
 272	l.sw	PT_SP(r31),r12					;\
 273	l.sw    PT_GPR13(r31),r13					;\
 274	/* --> */						;\
 275	/* save exception r4, set r4 = EA */			;\
 276	l.sw	PT_GPR4(r31),r4					;\
 277	l.mfspr r4,r0,SPR_EEAR_BASE				;\
 278	/* r12 == 1 if we come from syscall */			;\
 279	CLEAR_GPR(r12)						;\
 280	/* ----- play a MMU trick ----- */			;\
 281	l.ori	r31,r0,(EXCEPTION_SR)				;\
 282	l.mtspr	r0,r31,SPR_ESR_BASE				;\
 283	/* r31:	EA address of handler */			;\
 284	LOAD_SYMBOL_2_GPR(r31,handler)				;\
 285	l.mtspr r0,r31,SPR_EPCR_BASE				;\
 286	l.rfe
 287
 288/* =====================================================[ exceptions] === */
 289
 
 
 290/* ---[ 0x100: RESET exception ]----------------------------------------- */
 291    .org 0x100
 292	/* Jump to .init code at _start which lives in the .head section
 293	 * and will be discarded after boot.
 294	 */
 295	LOAD_SYMBOL_2_GPR(r15, _start)
 296	tophys	(r13,r15)			/* MMU disabled */
 297	l.jr	r13
 298	 l.nop
 299
 300/* ---[ 0x200: BUS exception ]------------------------------------------- */
 301    .org 0x200
 302_dispatch_bus_fault:
 303	EXCEPTION_HANDLE(_bus_fault_handler)
 304
 305/* ---[ 0x300: Data Page Fault exception ]------------------------------- */
 306    .org 0x300
 307_dispatch_do_dpage_fault:
 308//      totaly disable timer interrupt
 309// 	l.mtspr	r0,r0,SPR_TTMR
 310//	DEBUG_TLB_PROBE(0x300)
 311//	EXCEPTION_DEBUG_VALUE_ER_ENABLED(0x300)
 312	EXCEPTION_HANDLE(_data_page_fault_handler)
 313
 314/* ---[ 0x400: Insn Page Fault exception ]------------------------------- */
 315    .org 0x400
 316_dispatch_do_ipage_fault:
 317//      totaly disable timer interrupt
 318//	l.mtspr	r0,r0,SPR_TTMR
 319//	DEBUG_TLB_PROBE(0x400)
 320//	EXCEPTION_DEBUG_VALUE_ER_ENABLED(0x400)
 321	EXCEPTION_HANDLE(_insn_page_fault_handler)
 322
 323/* ---[ 0x500: Timer exception ]----------------------------------------- */
 324    .org 0x500
 325	EXCEPTION_HANDLE(_timer_handler)
 326
 327/* ---[ 0x600: Aligment exception ]-------------------------------------- */
 328    .org 0x600
 329	EXCEPTION_HANDLE(_alignment_handler)
 330
 331/* ---[ 0x700: Illegal insn exception ]---------------------------------- */
 332    .org 0x700
 333	EXCEPTION_HANDLE(_illegal_instruction_handler)
 334
 335/* ---[ 0x800: External interrupt exception ]---------------------------- */
 336    .org 0x800
 337	EXCEPTION_HANDLE(_external_irq_handler)
 338
 339/* ---[ 0x900: DTLB miss exception ]------------------------------------- */
 340    .org 0x900
 341	l.j	boot_dtlb_miss_handler
 342	l.nop
 343
 344/* ---[ 0xa00: ITLB miss exception ]------------------------------------- */
 345    .org 0xa00
 346	l.j	boot_itlb_miss_handler
 347	l.nop
 348
 349/* ---[ 0xb00: Range exception ]----------------------------------------- */
 350    .org 0xb00
 351	UNHANDLED_EXCEPTION(_vector_0xb00)
 352
 353/* ---[ 0xc00: Syscall exception ]--------------------------------------- */
 354    .org 0xc00
 355	EXCEPTION_HANDLE(_sys_call_handler)
 356
 357/* ---[ 0xd00: Trap exception ]------------------------------------------ */
 358    .org 0xd00
 359	UNHANDLED_EXCEPTION(_vector_0xd00)
 360
 361/* ---[ 0xe00: Trap exception ]------------------------------------------ */
 362    .org 0xe00
 363//	UNHANDLED_EXCEPTION(_vector_0xe00)
 364	EXCEPTION_HANDLE(_trap_handler)
 365
 366/* ---[ 0xf00: Reserved exception ]-------------------------------------- */
 367    .org 0xf00
 368	UNHANDLED_EXCEPTION(_vector_0xf00)
 369
 370/* ---[ 0x1000: Reserved exception ]------------------------------------- */
 371    .org 0x1000
 372	UNHANDLED_EXCEPTION(_vector_0x1000)
 373
 374/* ---[ 0x1100: Reserved exception ]------------------------------------- */
 375    .org 0x1100
 376	UNHANDLED_EXCEPTION(_vector_0x1100)
 377
 378/* ---[ 0x1200: Reserved exception ]------------------------------------- */
 379    .org 0x1200
 380	UNHANDLED_EXCEPTION(_vector_0x1200)
 381
 382/* ---[ 0x1300: Reserved exception ]------------------------------------- */
 383    .org 0x1300
 384	UNHANDLED_EXCEPTION(_vector_0x1300)
 385
 386/* ---[ 0x1400: Reserved exception ]------------------------------------- */
 387    .org 0x1400
 388	UNHANDLED_EXCEPTION(_vector_0x1400)
 389
 390/* ---[ 0x1500: Reserved exception ]------------------------------------- */
 391    .org 0x1500
 392	UNHANDLED_EXCEPTION(_vector_0x1500)
 393
 394/* ---[ 0x1600: Reserved exception ]------------------------------------- */
 395    .org 0x1600
 396	UNHANDLED_EXCEPTION(_vector_0x1600)
 397
 398/* ---[ 0x1700: Reserved exception ]------------------------------------- */
 399    .org 0x1700
 400	UNHANDLED_EXCEPTION(_vector_0x1700)
 401
 402/* ---[ 0x1800: Reserved exception ]------------------------------------- */
 403    .org 0x1800
 404	UNHANDLED_EXCEPTION(_vector_0x1800)
 405
 406/* ---[ 0x1900: Reserved exception ]------------------------------------- */
 407    .org 0x1900
 408	UNHANDLED_EXCEPTION(_vector_0x1900)
 409
 410/* ---[ 0x1a00: Reserved exception ]------------------------------------- */
 411    .org 0x1a00
 412	UNHANDLED_EXCEPTION(_vector_0x1a00)
 413
 414/* ---[ 0x1b00: Reserved exception ]------------------------------------- */
 415    .org 0x1b00
 416	UNHANDLED_EXCEPTION(_vector_0x1b00)
 417
 418/* ---[ 0x1c00: Reserved exception ]------------------------------------- */
 419    .org 0x1c00
 420	UNHANDLED_EXCEPTION(_vector_0x1c00)
 421
 422/* ---[ 0x1d00: Reserved exception ]------------------------------------- */
 423    .org 0x1d00
 424	UNHANDLED_EXCEPTION(_vector_0x1d00)
 425
 426/* ---[ 0x1e00: Reserved exception ]------------------------------------- */
 427    .org 0x1e00
 428	UNHANDLED_EXCEPTION(_vector_0x1e00)
 429
 430/* ---[ 0x1f00: Reserved exception ]------------------------------------- */
 431    .org 0x1f00
 432	UNHANDLED_EXCEPTION(_vector_0x1f00)
 433
 434    .org 0x2000
 435/* ===================================================[ kernel start ]=== */
 436
 437/*    .text*/
 438
 439/* This early stuff belongs in HEAD, but some of the functions below definitely
 440 * don't... */
 441
 442	__HEAD
 443	.global _start
 444_start:
 
 
 
 445	/* save kernel parameters */
 446	l.or	r25,r0,r3	/* pointer to fdt */
 447
 448	/*
 449	 * ensure a deterministic start
 450	 */
 451
 452	l.ori	r3,r0,0x1
 453	l.mtspr	r0,r3,SPR_SR
 454
 
 
 
 
 
 
 
 
 
 455	CLEAR_GPR(r1)
 456	CLEAR_GPR(r2)
 457	CLEAR_GPR(r3)
 458	CLEAR_GPR(r4)
 459	CLEAR_GPR(r5)
 460	CLEAR_GPR(r6)
 461	CLEAR_GPR(r7)
 462	CLEAR_GPR(r8)
 463	CLEAR_GPR(r9)
 464	CLEAR_GPR(r10)
 465	CLEAR_GPR(r11)
 466	CLEAR_GPR(r12)
 467	CLEAR_GPR(r13)
 468	CLEAR_GPR(r14)
 469	CLEAR_GPR(r15)
 470	CLEAR_GPR(r16)
 471	CLEAR_GPR(r17)
 472	CLEAR_GPR(r18)
 473	CLEAR_GPR(r19)
 474	CLEAR_GPR(r20)
 475	CLEAR_GPR(r21)
 476	CLEAR_GPR(r22)
 477	CLEAR_GPR(r23)
 478	CLEAR_GPR(r24)
 479	CLEAR_GPR(r26)
 480	CLEAR_GPR(r27)
 481	CLEAR_GPR(r28)
 482	CLEAR_GPR(r29)
 483	CLEAR_GPR(r30)
 484	CLEAR_GPR(r31)
 485
 
 
 
 
 
 
 486	/*
 487	 * set up initial ksp and current
 488	 */
 489	LOAD_SYMBOL_2_GPR(r1,init_thread_union+0x2000)	// setup kernel stack
 
 490	LOAD_SYMBOL_2_GPR(r10,init_thread_union)	// setup current
 491	tophys	(r31,r10)
 492	l.sw	TI_KSP(r31), r1
 493
 494	l.ori	r4,r0,0x0
 495
 496
 497	/*
 498	 * .data contains initialized data,
 499	 * .bss contains uninitialized data - clear it up
 500	 */
 501clear_bss:
 502	LOAD_SYMBOL_2_GPR(r24, __bss_start)
 503	LOAD_SYMBOL_2_GPR(r26, _end)
 504	tophys(r28,r24)
 505	tophys(r30,r26)
 506	CLEAR_GPR(r24)
 507	CLEAR_GPR(r26)
 5081:
 509	l.sw    (0)(r28),r0
 510	l.sfltu r28,r30
 511	l.bf    1b
 512	l.addi  r28,r28,4
 513
 514enable_ic:
 515	l.jal	_ic_enable
 516	 l.nop
 517
 518enable_dc:
 519	l.jal	_dc_enable
 520	 l.nop
 521
 522flush_tlb:
 523	/*
 524	 *  I N V A L I D A T E   T L B   e n t r i e s
 525	 */
 526	LOAD_SYMBOL_2_GPR(r5,SPR_DTLBMR_BASE(0))
 527	LOAD_SYMBOL_2_GPR(r6,SPR_ITLBMR_BASE(0))
 528	l.addi	r7,r0,128 /* Maximum number of sets */
 5291:
 530	l.mtspr	r5,r0,0x0
 531	l.mtspr	r6,r0,0x0
 532
 533	l.addi	r5,r5,1
 534	l.addi	r6,r6,1
 535	l.sfeq	r7,r0
 536	l.bnf	1b
 537	 l.addi	r7,r7,-1
 538
 539
 540/* The MMU needs to be enabled before or32_early_setup is called */
 541
 542enable_mmu:
 543	/*
 544	 * enable dmmu & immu
 545	 * SR[5] = 0, SR[6] = 0, 6th and 7th bit of SR set to 0
 546	 */
 547	l.mfspr	r30,r0,SPR_SR
 548	l.movhi	r28,hi(SPR_SR_DME | SPR_SR_IME)
 549	l.ori	r28,r28,lo(SPR_SR_DME | SPR_SR_IME)
 550	l.or	r30,r30,r28
 551	l.mtspr	r0,r30,SPR_SR
 552	l.nop
 553	l.nop
 554	l.nop
 555	l.nop
 556	l.nop
 557	l.nop
 558	l.nop
 559	l.nop
 560	l.nop
 561	l.nop
 562	l.nop
 563	l.nop
 564	l.nop
 565	l.nop
 566	l.nop
 567	l.nop
 568
 569	// reset the simulation counters
 570	l.nop 5
 571
 572	/* check fdt header magic word */
 573	l.lwz	r3,0(r25)	/* load magic from fdt into r3 */
 574	l.movhi	r4,hi(OF_DT_HEADER)
 575	l.ori	r4,r4,lo(OF_DT_HEADER)
 576	l.sfeq	r3,r4
 577	l.bf	_fdt_found
 578	 l.nop
 579	/* magic number mismatch, set fdt pointer to null */
 580	l.or	r25,r0,r0
 581_fdt_found:
 582	/* pass fdt pointer to or32_early_setup in r3 */
 583	l.or	r3,r0,r25
 584	LOAD_SYMBOL_2_GPR(r24, or32_early_setup)
 585	l.jalr r24
 586	 l.nop
 587
 588clear_regs:
 589	/*
 590	 * clear all GPRS to increase determinism
 591	 */
 592	CLEAR_GPR(r2)
 593	CLEAR_GPR(r3)
 594	CLEAR_GPR(r4)
 595	CLEAR_GPR(r5)
 596	CLEAR_GPR(r6)
 597	CLEAR_GPR(r7)
 598	CLEAR_GPR(r8)
 599	CLEAR_GPR(r9)
 600	CLEAR_GPR(r11)
 601	CLEAR_GPR(r12)
 602	CLEAR_GPR(r13)
 603	CLEAR_GPR(r14)
 604	CLEAR_GPR(r15)
 605	CLEAR_GPR(r16)
 606	CLEAR_GPR(r17)
 607	CLEAR_GPR(r18)
 608	CLEAR_GPR(r19)
 609	CLEAR_GPR(r20)
 610	CLEAR_GPR(r21)
 611	CLEAR_GPR(r22)
 612	CLEAR_GPR(r23)
 613	CLEAR_GPR(r24)
 614	CLEAR_GPR(r25)
 615	CLEAR_GPR(r26)
 616	CLEAR_GPR(r27)
 617	CLEAR_GPR(r28)
 618	CLEAR_GPR(r29)
 619	CLEAR_GPR(r30)
 620	CLEAR_GPR(r31)
 621
 622jump_start_kernel:
 623	/*
 624	 * jump to kernel entry (start_kernel)
 625	 */
 626	LOAD_SYMBOL_2_GPR(r30, start_kernel)
 627	l.jr    r30
 628	 l.nop
 629
 630/* ========================================[ cache ]=== */
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 631
 632	/* aligment here so we don't change memory offsets with
 633	 * memory controler defined
 634	 */
 635	.align 0x2000
 636
 637_ic_enable:
 638	/* Check if IC present and skip enabling otherwise */
 639	l.mfspr r24,r0,SPR_UPR
 640	l.andi  r26,r24,SPR_UPR_ICP
 641	l.sfeq  r26,r0
 642	l.bf	9f
 643	l.nop
 644
 645	/* Disable IC */
 646	l.mfspr r6,r0,SPR_SR
 647	l.addi  r5,r0,-1
 648	l.xori  r5,r5,SPR_SR_ICE
 649	l.and   r5,r6,r5
 650	l.mtspr r0,r5,SPR_SR
 651
 652	/* Establish cache block size
 653	   If BS=0, 16;
 654	   If BS=1, 32;
 655	   r14 contain block size
 656	*/
 657	l.mfspr r24,r0,SPR_ICCFGR
 658	l.andi	r26,r24,SPR_ICCFGR_CBS
 659	l.srli	r28,r26,7
 660	l.ori	r30,r0,16
 661	l.sll	r14,r30,r28
 662
 663	/* Establish number of cache sets
 664	   r16 contains number of cache sets
 665	   r28 contains log(# of cache sets)
 666	*/
 667	l.andi  r26,r24,SPR_ICCFGR_NCS
 668	l.srli 	r28,r26,3
 669	l.ori   r30,r0,1
 670	l.sll   r16,r30,r28
 671
 672	/* Invalidate IC */
 673	l.addi  r6,r0,0
 674	l.sll   r5,r14,r28
 675//        l.mul   r5,r14,r16
 676//	l.trap  1
 677//	l.addi  r5,r0,IC_SIZE
 6781:
 679	l.mtspr r0,r6,SPR_ICBIR
 680	l.sfne  r6,r5
 681	l.bf    1b
 682	l.add   r6,r6,r14
 683 //       l.addi   r6,r6,IC_LINE
 684
 685	/* Enable IC */
 686	l.mfspr r6,r0,SPR_SR
 687	l.ori   r6,r6,SPR_SR_ICE
 688	l.mtspr r0,r6,SPR_SR
 689	l.nop
 690	l.nop
 691	l.nop
 692	l.nop
 693	l.nop
 694	l.nop
 695	l.nop
 696	l.nop
 697	l.nop
 698	l.nop
 6999:
 700	l.jr    r9
 701	l.nop
 702
 703_dc_enable:
 704	/* Check if DC present and skip enabling otherwise */
 705	l.mfspr r24,r0,SPR_UPR
 706	l.andi  r26,r24,SPR_UPR_DCP
 707	l.sfeq  r26,r0
 708	l.bf	9f
 709	l.nop
 710
 711	/* Disable DC */
 712	l.mfspr r6,r0,SPR_SR
 713	l.addi  r5,r0,-1
 714	l.xori  r5,r5,SPR_SR_DCE
 715	l.and   r5,r6,r5
 716	l.mtspr r0,r5,SPR_SR
 717
 718	/* Establish cache block size
 719	   If BS=0, 16;
 720	   If BS=1, 32;
 721	   r14 contain block size
 722	*/
 723	l.mfspr r24,r0,SPR_DCCFGR
 724	l.andi	r26,r24,SPR_DCCFGR_CBS
 725	l.srli	r28,r26,7
 726	l.ori	r30,r0,16
 727	l.sll	r14,r30,r28
 728
 729	/* Establish number of cache sets
 730	   r16 contains number of cache sets
 731	   r28 contains log(# of cache sets)
 732	*/
 733	l.andi  r26,r24,SPR_DCCFGR_NCS
 734	l.srli 	r28,r26,3
 735	l.ori   r30,r0,1
 736	l.sll   r16,r30,r28
 737
 738	/* Invalidate DC */
 739	l.addi  r6,r0,0
 740	l.sll   r5,r14,r28
 7411:
 742	l.mtspr r0,r6,SPR_DCBIR
 743	l.sfne  r6,r5
 744	l.bf    1b
 745	l.add   r6,r6,r14
 746
 747	/* Enable DC */
 748	l.mfspr r6,r0,SPR_SR
 749	l.ori   r6,r6,SPR_SR_DCE
 750	l.mtspr r0,r6,SPR_SR
 7519:
 752	l.jr    r9
 753	l.nop
 754
 755/* ===============================================[ page table masks ]=== */
 756
 757#define DTLB_UP_CONVERT_MASK  0x3fa
 758#define ITLB_UP_CONVERT_MASK  0x3a
 759
 760/* for SMP we'd have (this is a bit subtle, CC must be always set
 761 * for SMP, but since we have _PAGE_PRESENT bit always defined
 762 * we can just modify the mask)
 763 */
 764#define DTLB_SMP_CONVERT_MASK  0x3fb
 765#define ITLB_SMP_CONVERT_MASK  0x3b
 766
 767/* ---[ boot dtlb miss handler ]----------------------------------------- */
 768
 769boot_dtlb_miss_handler:
 770
 771/* mask for DTLB_MR register: - (0) sets V (valid) bit,
 772 *                            - (31-12) sets bits belonging to VPN (31-12)
 773 */
 774#define DTLB_MR_MASK 0xfffff001
 775
 776/* mask for DTLB_TR register: - (2) sets CI (cache inhibit) bit,
 777 *			      - (4) sets A (access) bit,
 778 *                            - (5) sets D (dirty) bit,
 779 *                            - (8) sets SRE (superuser read) bit
 780 *                            - (9) sets SWE (superuser write) bit
 781 *                            - (31-12) sets bits belonging to VPN (31-12)
 782 */
 783#define DTLB_TR_MASK 0xfffff332
 784
 785/* These are for masking out the VPN/PPN value from the MR/TR registers...
 786 * it's not the same as the PFN */
 787#define VPN_MASK 0xfffff000
 788#define PPN_MASK 0xfffff000
 789
 790
 791	EXCEPTION_STORE_GPR6
 792
 793#if 0
 794	l.mfspr r6,r0,SPR_ESR_BASE	   //
 795	l.andi  r6,r6,SPR_SR_SM            // are we in kernel mode ?
 796	l.sfeqi r6,0                       // r6 == 0x1 --> SM
 797	l.bf    exit_with_no_dtranslation  //
 798	l.nop
 799#endif
 800
 801	/* this could be optimized by moving storing of
 802	 * non r6 registers here, and jumping r6 restore
 803	 * if not in supervisor mode
 804	 */
 805
 806	EXCEPTION_STORE_GPR2
 807	EXCEPTION_STORE_GPR3
 808	EXCEPTION_STORE_GPR4
 809	EXCEPTION_STORE_GPR5
 810
 811	l.mfspr r4,r0,SPR_EEAR_BASE        // get the offending EA
 812
 813immediate_translation:
 814	CLEAR_GPR(r6)
 815
 816	l.srli	r3,r4,0xd                  // r3 <- r4 / 8192 (sets are relative to page size (8Kb) NOT VPN size (4Kb)
 817
 818	l.mfspr r6, r0, SPR_DMMUCFGR
 819	l.andi	r6, r6, SPR_DMMUCFGR_NTS
 820	l.srli	r6, r6, SPR_DMMUCFGR_NTS_OFF
 821	l.ori	r5, r0, 0x1
 822	l.sll	r5, r5, r6 	// r5 = number DMMU sets
 823	l.addi	r6, r5, -1  	// r6 = nsets mask
 824	l.and	r2, r3, r6	// r2 <- r3 % NSETS_MASK
 825
 826	l.or    r6,r6,r4                   // r6 <- r4
 827	l.ori   r6,r6,~(VPN_MASK)          // r6 <- VPN :VPN .xfff - clear up lo(r6) to 0x**** *fff
 828	l.movhi r5,hi(DTLB_MR_MASK)        // r5 <- ffff:0000.x000
 829	l.ori   r5,r5,lo(DTLB_MR_MASK)     // r5 <- ffff:1111.x001 - apply DTLB_MR_MASK
 830	l.and   r5,r5,r6                   // r5 <- VPN :VPN .x001 - we have DTLBMR entry
 831	l.mtspr r2,r5,SPR_DTLBMR_BASE(0)   // set DTLBMR
 832
 833	/* set up DTLB with no translation for EA <= 0xbfffffff */
 834	LOAD_SYMBOL_2_GPR(r6,0xbfffffff)
 835	l.sfgeu  r6,r4                     // flag if r6 >= r4 (if 0xbfffffff >= EA)
 836	l.bf     1f                        // goto out
 837	l.and    r3,r4,r4                  // delay slot :: 24 <- r4 (if flag==1)
 838
 839	tophys(r3,r4)                      // r3 <- PA
 8401:
 841	l.ori   r3,r3,~(PPN_MASK)          // r3 <- PPN :PPN .xfff - clear up lo(r6) to 0x**** *fff
 842	l.movhi r5,hi(DTLB_TR_MASK)        // r5 <- ffff:0000.x000
 843	l.ori   r5,r5,lo(DTLB_TR_MASK)     // r5 <- ffff:1111.x330 - apply DTLB_MR_MASK
 844	l.and   r5,r5,r3                   // r5 <- PPN :PPN .x330 - we have DTLBTR entry
 845	l.mtspr r2,r5,SPR_DTLBTR_BASE(0)   // set DTLBTR
 846
 847	EXCEPTION_LOAD_GPR6
 848	EXCEPTION_LOAD_GPR5
 849	EXCEPTION_LOAD_GPR4
 850	EXCEPTION_LOAD_GPR3
 851	EXCEPTION_LOAD_GPR2
 852
 853	l.rfe                              // SR <- ESR, PC <- EPC
 854
 855exit_with_no_dtranslation:
 856	/* EA out of memory or not in supervisor mode */
 857	EXCEPTION_LOAD_GPR6
 858	EXCEPTION_LOAD_GPR4
 859	l.j	_dispatch_bus_fault
 860
 861/* ---[ boot itlb miss handler ]----------------------------------------- */
 862
 863boot_itlb_miss_handler:
 864
 865/* mask for ITLB_MR register: - sets V (valid) bit,
 866 *                            - sets bits belonging to VPN (15-12)
 867 */
 868#define ITLB_MR_MASK 0xfffff001
 869
 870/* mask for ITLB_TR register: - sets A (access) bit,
 871 *                            - sets SXE (superuser execute) bit
 872 *                            - sets bits belonging to VPN (15-12)
 873 */
 874#define ITLB_TR_MASK 0xfffff050
 875
 876/*
 877#define VPN_MASK 0xffffe000
 878#define PPN_MASK 0xffffe000
 879*/
 880
 881
 882
 883	EXCEPTION_STORE_GPR2
 884	EXCEPTION_STORE_GPR3
 885	EXCEPTION_STORE_GPR4
 886	EXCEPTION_STORE_GPR5
 887	EXCEPTION_STORE_GPR6
 888
 889#if 0
 890	l.mfspr r6,r0,SPR_ESR_BASE         //
 891	l.andi  r6,r6,SPR_SR_SM            // are we in kernel mode ?
 892	l.sfeqi r6,0                       // r6 == 0x1 --> SM
 893	l.bf    exit_with_no_itranslation
 894	l.nop
 895#endif
 896
 897
 898	l.mfspr r4,r0,SPR_EEAR_BASE        // get the offending EA
 899
 900earlyearly:
 901	CLEAR_GPR(r6)
 902
 903	l.srli  r3,r4,0xd                  // r3 <- r4 / 8192 (sets are relative to page size (8Kb) NOT VPN size (4Kb)
 904
 905	l.mfspr r6, r0, SPR_IMMUCFGR
 906	l.andi	r6, r6, SPR_IMMUCFGR_NTS
 907	l.srli	r6, r6, SPR_IMMUCFGR_NTS_OFF
 908	l.ori	r5, r0, 0x1
 909	l.sll	r5, r5, r6 	// r5 = number IMMU sets from IMMUCFGR
 910	l.addi	r6, r5, -1  	// r6 = nsets mask
 911	l.and	r2, r3, r6	// r2 <- r3 % NSETS_MASK
 912
 913	l.or    r6,r6,r4                   // r6 <- r4
 914	l.ori   r6,r6,~(VPN_MASK)          // r6 <- VPN :VPN .xfff - clear up lo(r6) to 0x**** *fff
 915	l.movhi r5,hi(ITLB_MR_MASK)        // r5 <- ffff:0000.x000
 916	l.ori   r5,r5,lo(ITLB_MR_MASK)     // r5 <- ffff:1111.x001 - apply ITLB_MR_MASK
 917	l.and   r5,r5,r6                   // r5 <- VPN :VPN .x001 - we have ITLBMR entry
 918	l.mtspr r2,r5,SPR_ITLBMR_BASE(0)   // set ITLBMR
 919
 920	/*
 921	 * set up ITLB with no translation for EA <= 0x0fffffff
 922	 *
 923	 * we need this for head.S mapping (EA = PA). if we move all functions
 924	 * which run with mmu enabled into entry.S, we might be able to eliminate this.
 925	 *
 926	 */
 927	LOAD_SYMBOL_2_GPR(r6,0x0fffffff)
 928	l.sfgeu  r6,r4                     // flag if r6 >= r4 (if 0xb0ffffff >= EA)
 929	l.bf     1f                        // goto out
 930	l.and    r3,r4,r4                  // delay slot :: 24 <- r4 (if flag==1)
 931
 932	tophys(r3,r4)                      // r3 <- PA
 9331:
 934	l.ori   r3,r3,~(PPN_MASK)          // r3 <- PPN :PPN .xfff - clear up lo(r6) to 0x**** *fff
 935	l.movhi r5,hi(ITLB_TR_MASK)        // r5 <- ffff:0000.x000
 936	l.ori   r5,r5,lo(ITLB_TR_MASK)     // r5 <- ffff:1111.x050 - apply ITLB_MR_MASK
 937	l.and   r5,r5,r3                   // r5 <- PPN :PPN .x050 - we have ITLBTR entry
 938	l.mtspr r2,r5,SPR_ITLBTR_BASE(0)   // set ITLBTR
 939
 940	EXCEPTION_LOAD_GPR6
 941	EXCEPTION_LOAD_GPR5
 942	EXCEPTION_LOAD_GPR4
 943	EXCEPTION_LOAD_GPR3
 944	EXCEPTION_LOAD_GPR2
 945
 946	l.rfe                              // SR <- ESR, PC <- EPC
 947
 948exit_with_no_itranslation:
 949	EXCEPTION_LOAD_GPR4
 950	EXCEPTION_LOAD_GPR6
 951	l.j    _dispatch_bus_fault
 952	l.nop
 953
 954/* ====================================================================== */
 955/*
 956 * Stuff below here shouldn't go into .head section... maybe this stuff
 957 * can be moved to entry.S ???
 958 */
 959
 960/* ==============================================[ DTLB miss handler ]=== */
 961
 962/*
 963 * Comments:
 964 *   Exception handlers are entered with MMU off so the following handler
 965 *   needs to use physical addressing
 966 *
 967 */
 968
 969	.text
 970ENTRY(dtlb_miss_handler)
 971	EXCEPTION_STORE_GPR2
 972	EXCEPTION_STORE_GPR3
 973	EXCEPTION_STORE_GPR4
 974	EXCEPTION_STORE_GPR5
 975	EXCEPTION_STORE_GPR6
 976	/*
 977	 * get EA of the miss
 978	 */
 979	l.mfspr	r2,r0,SPR_EEAR_BASE
 980	/*
 981	 * pmd = (pmd_t *)(current_pgd + pgd_index(daddr));
 982	 */
 983	GET_CURRENT_PGD(r3,r5)		// r3 is current_pgd, r5 is temp
 984	l.srli	r4,r2,0x18		// >> PAGE_SHIFT + (PAGE_SHIFT - 2)
 985	l.slli	r4,r4,0x2		// to get address << 2
 986	l.add	r5,r4,r3		// r4 is pgd_index(daddr)
 987	/*
 988	 * if (pmd_none(*pmd))
 989	 *   goto pmd_none:
 990	 */
 991	tophys	(r4,r5)
 992	l.lwz	r3,0x0(r4)		// get *pmd value
 993	l.sfne	r3,r0
 994	l.bnf	d_pmd_none
 995	 l.andi	r3,r3,~PAGE_MASK //0x1fff		// ~PAGE_MASK
 996	/*
 997	 * if (pmd_bad(*pmd))
 998	 *   pmd_clear(pmd)
 999	 *   goto pmd_bad:
1000	 */
1001//	l.sfeq	r3,r0			// check *pmd value
1002//	l.bf	d_pmd_good
1003	l.addi	r3,r0,0xffffe000	// PAGE_MASK
1004//	l.j	d_pmd_bad
1005//	l.sw	0x0(r4),r0		// clear pmd
1006d_pmd_good:
1007	/*
1008	 * pte = *pte_offset(pmd, daddr);
1009	 */
1010	l.lwz	r4,0x0(r4)		// get **pmd value
1011	l.and	r4,r4,r3		// & PAGE_MASK
1012	l.srli	r5,r2,0xd		// >> PAGE_SHIFT, r2 == EEAR
1013	l.andi	r3,r5,0x7ff		// (1UL << PAGE_SHIFT - 2) - 1
1014	l.slli	r3,r3,0x2		// to get address << 2
1015	l.add	r3,r3,r4
1016	l.lwz	r2,0x0(r3)		// this is pte at last
1017	/*
1018	 * if (!pte_present(pte))
1019	 */
1020	l.andi	r4,r2,0x1
1021	l.sfne	r4,r0			// is pte present
1022	l.bnf	d_pte_not_present
1023	l.addi	r3,r0,0xffffe3fa	// PAGE_MASK | DTLB_UP_CONVERT_MASK
1024	/*
1025	 * fill DTLB TR register
1026	 */
1027	l.and	r4,r2,r3		// apply the mask
1028	// Determine number of DMMU sets
1029	l.mfspr r6, r0, SPR_DMMUCFGR
1030	l.andi	r6, r6, SPR_DMMUCFGR_NTS
1031	l.srli	r6, r6, SPR_DMMUCFGR_NTS_OFF
1032	l.ori	r3, r0, 0x1
1033	l.sll	r3, r3, r6 	// r3 = number DMMU sets DMMUCFGR
1034	l.addi	r6, r3, -1  	// r6 = nsets mask
1035	l.and	r5, r5, r6	// calc offset:	 & (NUM_TLB_ENTRIES-1)
 
 
1036	                                                   //NUM_TLB_ENTRIES
1037	l.mtspr	r5,r4,SPR_DTLBTR_BASE(0)
1038	/*
1039	 * fill DTLB MR register
1040	 */
1041	l.mfspr	r2,r0,SPR_EEAR_BASE
1042	l.addi	r3,r0,0xffffe000	// PAGE_MASK
1043	l.and	r4,r2,r3		// apply PAGE_MASK to EA (__PHX__ do we really need this?)
1044	l.ori	r4,r4,0x1		// set hardware valid bit: DTBL_MR entry
1045	l.mtspr	r5,r4,SPR_DTLBMR_BASE(0)
1046
1047	EXCEPTION_LOAD_GPR2
1048	EXCEPTION_LOAD_GPR3
1049	EXCEPTION_LOAD_GPR4
1050	EXCEPTION_LOAD_GPR5
1051	EXCEPTION_LOAD_GPR6
1052	l.rfe
1053d_pmd_bad:
1054	l.nop	1
1055	EXCEPTION_LOAD_GPR2
1056	EXCEPTION_LOAD_GPR3
1057	EXCEPTION_LOAD_GPR4
1058	EXCEPTION_LOAD_GPR5
1059	EXCEPTION_LOAD_GPR6
1060	l.rfe
1061d_pmd_none:
1062d_pte_not_present:
1063	EXCEPTION_LOAD_GPR2
1064	EXCEPTION_LOAD_GPR3
1065	EXCEPTION_LOAD_GPR4
1066	EXCEPTION_LOAD_GPR5
1067	EXCEPTION_LOAD_GPR6
1068	EXCEPTION_HANDLE(_dtlb_miss_page_fault_handler)
1069
1070/* ==============================================[ ITLB miss handler ]=== */
1071ENTRY(itlb_miss_handler)
1072	EXCEPTION_STORE_GPR2
1073	EXCEPTION_STORE_GPR3
1074	EXCEPTION_STORE_GPR4
1075	EXCEPTION_STORE_GPR5
1076	EXCEPTION_STORE_GPR6
1077	/*
1078	 * get EA of the miss
1079	 */
1080	l.mfspr	r2,r0,SPR_EEAR_BASE
1081
1082	/*
1083	 * pmd = (pmd_t *)(current_pgd + pgd_index(daddr));
1084	 *
1085	 */
1086	GET_CURRENT_PGD(r3,r5)		// r3 is current_pgd, r5 is temp
1087	l.srli	r4,r2,0x18		// >> PAGE_SHIFT + (PAGE_SHIFT - 2)
1088	l.slli	r4,r4,0x2		// to get address << 2
1089	l.add	r5,r4,r3		// r4 is pgd_index(daddr)
1090	/*
1091	 * if (pmd_none(*pmd))
1092	 *   goto pmd_none:
1093	 */
1094	tophys	(r4,r5)
1095	l.lwz	r3,0x0(r4)		// get *pmd value
1096	l.sfne	r3,r0
1097	l.bnf	i_pmd_none
1098	l.andi	r3,r3,0x1fff		// ~PAGE_MASK
1099	/*
1100	 * if (pmd_bad(*pmd))
1101	 *   pmd_clear(pmd)
1102	 *   goto pmd_bad:
1103	 */
1104
1105//	l.sfeq	r3,r0			// check *pmd value
1106//	l.bf	i_pmd_good
1107	l.addi	r3,r0,0xffffe000	// PAGE_MASK
1108//	l.j	i_pmd_bad
1109//	l.sw	0x0(r4),r0		// clear pmd
1110
1111i_pmd_good:
1112	/*
1113	 * pte = *pte_offset(pmd, iaddr);
1114	 *
1115	 */
1116	l.lwz	r4,0x0(r4)		// get **pmd value
1117	l.and	r4,r4,r3		// & PAGE_MASK
1118	l.srli	r5,r2,0xd		// >> PAGE_SHIFT, r2 == EEAR
1119	l.andi	r3,r5,0x7ff		// (1UL << PAGE_SHIFT - 2) - 1
1120	l.slli	r3,r3,0x2		// to get address << 2
1121	l.add	r3,r3,r4
1122	l.lwz	r2,0x0(r3)		// this is pte at last
1123	/*
1124	 * if (!pte_present(pte))
1125	 *
1126	 */
1127	l.andi	r4,r2,0x1
1128	l.sfne	r4,r0			// is pte present
1129	l.bnf	i_pte_not_present
1130	l.addi	r3,r0,0xffffe03a	// PAGE_MASK | ITLB_UP_CONVERT_MASK
1131	/*
1132	 * fill ITLB TR register
1133	 */
1134	l.and	r4,r2,r3		// apply the mask
1135	l.andi	r3,r2,0x7c0		// _PAGE_EXEC | _PAGE_SRE | _PAGE_SWE |  _PAGE_URE | _PAGE_UWE
1136//	l.andi	r3,r2,0x400		// _PAGE_EXEC
1137	l.sfeq	r3,r0
1138	l.bf	itlb_tr_fill //_workaround
1139	// Determine number of IMMU sets
1140	l.mfspr r6, r0, SPR_IMMUCFGR
1141	l.andi	r6, r6, SPR_IMMUCFGR_NTS
1142	l.srli	r6, r6, SPR_IMMUCFGR_NTS_OFF
1143	l.ori	r3, r0, 0x1
1144	l.sll	r3, r3, r6 	// r3 = number IMMU sets IMMUCFGR
1145	l.addi	r6, r3, -1  	// r6 = nsets mask
1146	l.and	r5, r5, r6	// calc offset:	 & (NUM_TLB_ENTRIES-1)
 
 
1147
1148/*
1149 * __PHX__ :: fixme
1150 * we should not just blindly set executable flags,
1151 * but it does help with ping. the clean way would be to find out
1152 * (and fix it) why stack doesn't have execution permissions
1153 */
1154
1155itlb_tr_fill_workaround:
1156	l.ori	r4,r4,0xc0		// | (SPR_ITLBTR_UXE | ITLBTR_SXE)
1157itlb_tr_fill:
1158	l.mtspr	r5,r4,SPR_ITLBTR_BASE(0)
1159	/*
1160	 * fill DTLB MR register
1161	 */
1162	l.mfspr	r2,r0,SPR_EEAR_BASE
1163	l.addi	r3,r0,0xffffe000	// PAGE_MASK
1164	l.and	r4,r2,r3		// apply PAGE_MASK to EA (__PHX__ do we really need this?)
1165	l.ori	r4,r4,0x1		// set hardware valid bit: DTBL_MR entry
1166	l.mtspr	r5,r4,SPR_ITLBMR_BASE(0)
1167
1168	EXCEPTION_LOAD_GPR2
1169	EXCEPTION_LOAD_GPR3
1170	EXCEPTION_LOAD_GPR4
1171	EXCEPTION_LOAD_GPR5
1172	EXCEPTION_LOAD_GPR6
1173	l.rfe
1174
1175i_pmd_bad:
1176	l.nop	1
1177	EXCEPTION_LOAD_GPR2
1178	EXCEPTION_LOAD_GPR3
1179	EXCEPTION_LOAD_GPR4
1180	EXCEPTION_LOAD_GPR5
1181	EXCEPTION_LOAD_GPR6
1182	l.rfe
1183i_pmd_none:
1184i_pte_not_present:
1185	EXCEPTION_LOAD_GPR2
1186	EXCEPTION_LOAD_GPR3
1187	EXCEPTION_LOAD_GPR4
1188	EXCEPTION_LOAD_GPR5
1189	EXCEPTION_LOAD_GPR6
1190	EXCEPTION_HANDLE(_itlb_miss_page_fault_handler)
1191
1192/* ==============================================[ boot tlb handlers ]=== */
1193
1194
1195/* =================================================[ debugging aids ]=== */
1196
1197	.align 64
1198_immu_trampoline:
1199	.space 64
1200_immu_trampoline_top:
1201
1202#define TRAMP_SLOT_0		(0x0)
1203#define TRAMP_SLOT_1		(0x4)
1204#define TRAMP_SLOT_2		(0x8)
1205#define TRAMP_SLOT_3		(0xc)
1206#define TRAMP_SLOT_4		(0x10)
1207#define TRAMP_SLOT_5		(0x14)
1208#define TRAMP_FRAME_SIZE	(0x18)
1209
1210ENTRY(_immu_trampoline_workaround)
1211	// r2 EEA
1212	// r6 is physical EEA
1213	tophys(r6,r2)
1214
1215	LOAD_SYMBOL_2_GPR(r5,_immu_trampoline)
1216	tophys	(r3,r5)			// r3 is trampoline (physical)
1217
1218	LOAD_SYMBOL_2_GPR(r4,0x15000000)
1219	l.sw	TRAMP_SLOT_0(r3),r4
1220	l.sw	TRAMP_SLOT_1(r3),r4
1221	l.sw	TRAMP_SLOT_4(r3),r4
1222	l.sw	TRAMP_SLOT_5(r3),r4
1223
1224					// EPC = EEA - 0x4
1225	l.lwz	r4,0x0(r6)		// load op @ EEA + 0x0 (fc address)
1226	l.sw	TRAMP_SLOT_3(r3),r4	// store it to _immu_trampoline_data
1227	l.lwz	r4,-0x4(r6)		// load op @ EEA - 0x4 (f8 address)
1228	l.sw	TRAMP_SLOT_2(r3),r4	// store it to _immu_trampoline_data
1229
1230	l.srli  r5,r4,26                // check opcode for write access
1231	l.sfeqi r5,0                    // l.j
1232	l.bf    0f
1233	l.sfeqi r5,0x11                 // l.jr
1234	l.bf    1f
1235	l.sfeqi r5,1                    // l.jal
1236	l.bf    2f
1237	l.sfeqi r5,0x12                 // l.jalr
1238	l.bf    3f
1239	l.sfeqi r5,3                    // l.bnf
1240	l.bf    4f
1241	l.sfeqi r5,4                    // l.bf
1242	l.bf    5f
124399:
1244	l.nop
1245	l.j	99b			// should never happen
1246	l.nop	1
1247
1248	// r2 is EEA
1249	// r3 is trampoline address (physical)
1250	// r4 is instruction
1251	// r6 is physical(EEA)
1252	//
1253	// r5
1254
12552:	// l.jal
1256
1257	/* 19 20 aa aa	l.movhi r9,0xaaaa
1258	 * a9 29 bb bb  l.ori	r9,0xbbbb
1259	 *
1260	 * where 0xaaaabbbb is EEA + 0x4 shifted right 2
1261	 */
1262
1263	l.addi	r6,r2,0x4		// this is 0xaaaabbbb
1264
1265					// l.movhi r9,0xaaaa
1266	l.ori	r5,r0,0x1920		// 0x1920 == l.movhi r9
1267	l.sh	(TRAMP_SLOT_0+0x0)(r3),r5
1268	l.srli	r5,r6,16
1269	l.sh	(TRAMP_SLOT_0+0x2)(r3),r5
1270
1271					// l.ori   r9,0xbbbb
1272	l.ori	r5,r0,0xa929		// 0xa929 == l.ori r9
1273	l.sh	(TRAMP_SLOT_1+0x0)(r3),r5
1274	l.andi	r5,r6,0xffff
1275	l.sh	(TRAMP_SLOT_1+0x2)(r3),r5
1276
1277	/* falthrough, need to set up new jump offset */
1278
1279
12800:	// l.j
1281	l.slli	r6,r4,6			// original offset shifted left 6 - 2
1282//	l.srli	r6,r6,6			// original offset shifted right 2
1283
1284	l.slli	r4,r2,4			// old jump position: EEA shifted left 4
1285//	l.srli	r4,r4,6			// old jump position: shifted right 2
1286
1287	l.addi	r5,r3,0xc		// new jump position (physical)
1288	l.slli	r5,r5,4			// new jump position: shifted left 4
1289
1290	// calculate new jump offset
1291	// new_off = old_off + (old_jump - new_jump)
1292
1293	l.sub	r5,r4,r5		// old_jump - new_jump
1294	l.add	r5,r6,r5		// orig_off + (old_jump - new_jump)
1295	l.srli	r5,r5,6			// new offset shifted right 2
1296
1297	// r5 is new jump offset
1298					// l.j has opcode 0x0...
1299	l.sw	TRAMP_SLOT_2(r3),r5	// write it back
1300
1301	l.j	trampoline_out
1302	l.nop
1303
1304/* ----------------------------- */
1305
13063:	// l.jalr
1307
1308	/* 19 20 aa aa	l.movhi r9,0xaaaa
1309	 * a9 29 bb bb  l.ori	r9,0xbbbb
1310	 *
1311	 * where 0xaaaabbbb is EEA + 0x4 shifted right 2
1312	 */
1313
1314	l.addi	r6,r2,0x4		// this is 0xaaaabbbb
1315
1316					// l.movhi r9,0xaaaa
1317	l.ori	r5,r0,0x1920		// 0x1920 == l.movhi r9
1318	l.sh	(TRAMP_SLOT_0+0x0)(r3),r5
1319	l.srli	r5,r6,16
1320	l.sh	(TRAMP_SLOT_0+0x2)(r3),r5
1321
1322					// l.ori   r9,0xbbbb
1323	l.ori	r5,r0,0xa929		// 0xa929 == l.ori r9
1324	l.sh	(TRAMP_SLOT_1+0x0)(r3),r5
1325	l.andi	r5,r6,0xffff
1326	l.sh	(TRAMP_SLOT_1+0x2)(r3),r5
1327
1328	l.lhz	r5,(TRAMP_SLOT_2+0x0)(r3)	// load hi part of jump instruction
1329	l.andi	r5,r5,0x3ff		// clear out opcode part
1330	l.ori	r5,r5,0x4400		// opcode changed from l.jalr -> l.jr
1331	l.sh	(TRAMP_SLOT_2+0x0)(r3),r5 // write it back
1332
1333	/* falthrough */
1334
13351:	// l.jr
1336	l.j	trampoline_out
1337	l.nop
1338
1339/* ----------------------------- */
1340
13414:	// l.bnf
13425:	// l.bf
1343	l.slli	r6,r4,6			// original offset shifted left 6 - 2
1344//	l.srli	r6,r6,6			// original offset shifted right 2
1345
1346	l.slli	r4,r2,4			// old jump position: EEA shifted left 4
1347//	l.srli	r4,r4,6			// old jump position: shifted right 2
1348
1349	l.addi	r5,r3,0xc		// new jump position (physical)
1350	l.slli	r5,r5,4			// new jump position: shifted left 4
1351
1352	// calculate new jump offset
1353	// new_off = old_off + (old_jump - new_jump)
1354
1355	l.add	r6,r6,r4		// (orig_off + old_jump)
1356	l.sub	r6,r6,r5		// (orig_off + old_jump) - new_jump
1357	l.srli	r6,r6,6			// new offset shifted right 2
1358
1359	// r6 is new jump offset
1360	l.lwz	r4,(TRAMP_SLOT_2+0x0)(r3)	// load jump instruction
1361	l.srli	r4,r4,16
1362	l.andi	r4,r4,0xfc00		// get opcode part
1363	l.slli	r4,r4,16
1364	l.or	r6,r4,r6		// l.b(n)f new offset
1365	l.sw	TRAMP_SLOT_2(r3),r6	// write it back
1366
1367	/* we need to add l.j to EEA + 0x8 */
1368	tophys	(r4,r2)			// may not be needed (due to shifts down_
1369	l.addi	r4,r4,(0x8 - 0x8)	// jump target = r2 + 0x8 (compensate for 0x8)
1370					// jump position = r5 + 0x8 (0x8 compensated)
1371	l.sub	r4,r4,r5		// jump offset = target - new_position + 0x8
1372
1373	l.slli	r4,r4,4			// the amount of info in imediate of jump
1374	l.srli	r4,r4,6			// jump instruction with offset
1375	l.sw	TRAMP_SLOT_4(r3),r4	// write it to 4th slot
1376
1377	/* fallthrough */
1378
1379trampoline_out:
1380	// set up new EPC to point to our trampoline code
1381	LOAD_SYMBOL_2_GPR(r5,_immu_trampoline)
1382	l.mtspr	r0,r5,SPR_EPCR_BASE
1383
1384	// immu_trampoline is (4x) CACHE_LINE aligned
1385	// and only 6 instructions long,
1386	// so we need to invalidate only 2 lines
1387
1388	/* Establish cache block size
1389	   If BS=0, 16;
1390	   If BS=1, 32;
1391	   r14 contain block size
1392	*/
1393	l.mfspr r21,r0,SPR_ICCFGR
1394	l.andi	r21,r21,SPR_ICCFGR_CBS
1395	l.srli	r21,r21,7
1396	l.ori	r23,r0,16
1397	l.sll	r14,r23,r21
1398
1399	l.mtspr	r0,r5,SPR_ICBIR
1400	l.add	r5,r5,r14
1401	l.mtspr	r0,r5,SPR_ICBIR
1402
1403	l.jr	r9
1404	l.nop
1405
1406
1407/*
1408 * DSCR: prints a string referenced by r3.
1409 *
1410 * PRMS: r3     	- address of the first character of null
1411 *			terminated string to be printed
1412 *
1413 * PREQ: UART at UART_BASE_ADD has to be initialized
1414 *
1415 * POST: caller should be aware that r3, r9 are changed
 
 
 
1416 */
1417ENTRY(_emergency_print)
1418	EMERGENCY_PRINT_STORE_GPR4
1419	EMERGENCY_PRINT_STORE_GPR5
1420	EMERGENCY_PRINT_STORE_GPR6
1421	EMERGENCY_PRINT_STORE_GPR7
14222:
1423	l.lbz	r7,0(r3)
1424	l.sfeq	r7,r0
1425	l.bf	9f
1426	l.nop
1427
1428// putc:
1429	l.movhi r4,hi(UART_BASE_ADD)
 
1430
 
 
 
 
 
 
 
 
 
 
 
 
 
1431	l.addi  r6,r0,0x20
14321:      l.lbz   r5,5(r4)
1433	l.andi  r5,r5,0x20
1434	l.sfeq  r5,r6
1435	l.bnf   1b
1436	l.nop
1437
 
1438	l.sb    0(r4),r7
1439
 
1440	l.addi  r6,r0,0x60
14411:      l.lbz   r5,5(r4)
1442	l.andi  r5,r5,0x60
1443	l.sfeq  r5,r6
1444	l.bnf   1b
1445	l.nop
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1446
1447	/* next character */
1448	l.j	2b
1449	l.addi	r3,r3,0x1
1450
14519:
 
1452	EMERGENCY_PRINT_LOAD_GPR7
1453	EMERGENCY_PRINT_LOAD_GPR6
1454	EMERGENCY_PRINT_LOAD_GPR5
1455	EMERGENCY_PRINT_LOAD_GPR4
1456	l.jr	r9
1457	l.nop
1458
 
 
 
 
 
 
 
 
 
1459ENTRY(_emergency_print_nr)
1460	EMERGENCY_PRINT_STORE_GPR4
1461	EMERGENCY_PRINT_STORE_GPR5
1462	EMERGENCY_PRINT_STORE_GPR6
1463	EMERGENCY_PRINT_STORE_GPR7
1464	EMERGENCY_PRINT_STORE_GPR8
 
1465
1466	l.addi	r8,r0,32		// shift register
1467
14681:	/* remove leading zeros */
1469	l.addi	r8,r8,-0x4
1470	l.srl	r7,r3,r8
1471	l.andi	r7,r7,0xf
1472
1473	/* don't skip the last zero if number == 0x0 */
1474	l.sfeqi	r8,0x4
1475	l.bf	2f
1476	l.nop
1477
1478	l.sfeq	r7,r0
1479	l.bf	1b
1480	l.nop
1481
14822:
1483	l.srl	r7,r3,r8
1484
1485	l.andi	r7,r7,0xf
1486	l.sflts	r8,r0
1487	l.bf	9f
1488
 
1489	l.sfgtui r7,0x9
1490	l.bnf	8f
1491	l.nop
1492	l.addi	r7,r7,0x27
1493
14948:
1495	l.addi	r7,r7,0x30
1496// putc:
1497	l.movhi r4,hi(UART_BASE_ADD)
1498
1499	l.addi  r6,r0,0x20
15001:      l.lbz   r5,5(r4)
1501	l.andi  r5,r5,0x20
1502	l.sfeq  r5,r6
1503	l.bnf   1b
1504	l.nop
1505
1506	l.sb    0(r4),r7
1507
1508	l.addi  r6,r0,0x60
15091:      l.lbz   r5,5(r4)
1510	l.andi  r5,r5,0x60
1511	l.sfeq  r5,r6
1512	l.bnf   1b
1513	l.nop
1514
1515	/* next character */
1516	l.j	2b
1517	l.addi	r8,r8,-0x4
1518
15199:
 
1520	EMERGENCY_PRINT_LOAD_GPR8
1521	EMERGENCY_PRINT_LOAD_GPR7
1522	EMERGENCY_PRINT_LOAD_GPR6
1523	EMERGENCY_PRINT_LOAD_GPR5
1524	EMERGENCY_PRINT_LOAD_GPR4
1525	l.jr	r9
1526	l.nop
1527
1528
1529/*
1530 * This should be used for debugging only.
1531 * It messes up the Linux early serial output
1532 * somehow, so use it sparingly and essentially
1533 * only if you need to debug something that goes wrong
1534 * before Linux gets the early serial going.
1535 *
1536 * Furthermore, you'll have to make sure you set the
1537 * UART_DEVISOR correctly according to the system
1538 * clock rate.
1539 *
1540 *
1541 */
1542
1543
1544
1545#define SYS_CLK            20000000
1546//#define SYS_CLK            1843200
1547#define OR32_CONSOLE_BAUD  115200
1548#define UART_DIVISOR       SYS_CLK/(16*OR32_CONSOLE_BAUD)
1549
1550ENTRY(_early_uart_init)
1551	l.movhi	r3,hi(UART_BASE_ADD)
 
1552
 
1553	l.addi	r4,r0,0x7
1554	l.sb	0x2(r3),r4
1555
1556	l.addi	r4,r0,0x0
1557	l.sb	0x1(r3),r4
1558
1559	l.addi	r4,r0,0x3
1560	l.sb	0x3(r3),r4
1561
1562	l.lbz	r5,3(r3)
1563	l.ori	r4,r5,0x80
1564	l.sb	0x3(r3),r4
1565	l.addi	r4,r0,((UART_DIVISOR>>8) & 0x000000ff)
1566	l.sb	UART_DLM(r3),r4
1567	l.addi  r4,r0,((UART_DIVISOR) & 0x000000ff)
1568	l.sb	UART_DLL(r3),r4
1569	l.sb	0x3(r3),r5
 
1570
1571	l.jr	r9
1572	l.nop
1573
1574_string_copying_linux:
1575	.string "\n\n\n\n\n\rCopying Linux... \0"
 
 
 
 
 
 
 
1576
1577_string_ok_booting:
1578	.string "Ok, booting the kernel.\n\r\0"
1579
 
1580_string_unhandled_exception:
1581	.string "\n\rRunarunaround: Unhandled exception 0x\0"
1582
1583_string_epc_prefix:
1584	.string ": EPC=0x\0"
1585
1586_string_nl:
1587	.string "\n\r\0"
1588
1589	.global	_string_esr_irq_bug
1590_string_esr_irq_bug:
1591	.string "\n\rESR external interrupt bug, for details look into entry.S\n\r\0"
1592
1593
1594
1595/* ========================================[ page aligned structures ]=== */
1596
1597/*
1598 * .data section should be page aligned
1599 *	(look into arch/or32/kernel/vmlinux.lds)
1600 */
1601	.section .data,"aw"
1602	.align	8192
1603	.global  empty_zero_page
1604empty_zero_page:
1605	.space  8192
1606
1607	.global  swapper_pg_dir
1608swapper_pg_dir:
1609	.space  8192
1610
1611	.global	_unhandled_stack
1612_unhandled_stack:
1613	.space	8192
1614_unhandled_stack_top:
1615
1616/* ============================================================[ EOF ]=== */
v6.13.7
   1/* SPDX-License-Identifier: GPL-2.0-or-later */
   2/*
   3 * OpenRISC head.S
   4 *
   5 * Linux architectural port borrowing liberally from similar works of
   6 * others.  All original copyrights apply as per the original source
   7 * declaration.
   8 *
   9 * Modifications for the OpenRISC architecture:
  10 * Copyright (C) 2003 Matjaz Breskvar <phoenix@bsemi.com>
  11 * Copyright (C) 2010-2011 Jonas Bonn <jonas@southpole.se>
 
 
 
 
 
  12 */
  13
  14#include <linux/linkage.h>
  15#include <linux/threads.h>
  16#include <linux/errno.h>
  17#include <linux/init.h>
  18#include <linux/serial_reg.h>
  19#include <linux/pgtable.h>
  20#include <asm/processor.h>
  21#include <asm/page.h>
  22#include <asm/mmu.h>
  23#include <asm/thread_info.h>
  24#include <asm/cache.h>
  25#include <asm/spr_defs.h>
  26#include <asm/asm-offsets.h>
  27#include <linux/of_fdt.h>
  28
  29#define tophys(rd,rs)						\
  30	l.movhi	rd,hi(-KERNELBASE)				;\
  31	l.add	rd,rd,rs
  32
  33#define CLEAR_GPR(gpr)						\
  34	l.movhi	gpr,0x0
  35
  36#define LOAD_SYMBOL_2_GPR(gpr,symbol)				\
  37	l.movhi gpr,hi(symbol)					;\
  38	l.ori   gpr,gpr,lo(symbol)
  39
  40
  41#define UART_BASE_ADD      0x90000000
  42
  43#define EXCEPTION_SR  (SPR_SR_DME | SPR_SR_IME | SPR_SR_DCE | SPR_SR_ICE | SPR_SR_SM)
  44#define SYSCALL_SR  (SPR_SR_DME | SPR_SR_IME | SPR_SR_DCE | SPR_SR_ICE | SPR_SR_IEE | SPR_SR_TEE | SPR_SR_SM)
  45
  46/* ============================================[ tmp store locations ]=== */
  47
  48#define SPR_SHADOW_GPR(x)	((x) + SPR_GPR_BASE + 32)
  49
  50/*
  51 * emergency_print temporary stores
  52 */
  53#ifdef CONFIG_OPENRISC_HAVE_SHADOW_GPRS
  54#define EMERGENCY_PRINT_STORE_GPR4	l.mtspr r0,r4,SPR_SHADOW_GPR(14)
  55#define EMERGENCY_PRINT_LOAD_GPR4	l.mfspr r4,r0,SPR_SHADOW_GPR(14)
  56
  57#define EMERGENCY_PRINT_STORE_GPR5	l.mtspr r0,r5,SPR_SHADOW_GPR(15)
  58#define EMERGENCY_PRINT_LOAD_GPR5	l.mfspr r5,r0,SPR_SHADOW_GPR(15)
  59
  60#define EMERGENCY_PRINT_STORE_GPR6	l.mtspr r0,r6,SPR_SHADOW_GPR(16)
  61#define EMERGENCY_PRINT_LOAD_GPR6	l.mfspr r6,r0,SPR_SHADOW_GPR(16)
  62
  63#define EMERGENCY_PRINT_STORE_GPR7	l.mtspr r0,r7,SPR_SHADOW_GPR(7)
  64#define EMERGENCY_PRINT_LOAD_GPR7	l.mfspr r7,r0,SPR_SHADOW_GPR(7)
  65
  66#define EMERGENCY_PRINT_STORE_GPR8	l.mtspr r0,r8,SPR_SHADOW_GPR(8)
  67#define EMERGENCY_PRINT_LOAD_GPR8	l.mfspr r8,r0,SPR_SHADOW_GPR(8)
  68
  69#define EMERGENCY_PRINT_STORE_GPR9	l.mtspr r0,r9,SPR_SHADOW_GPR(9)
  70#define EMERGENCY_PRINT_LOAD_GPR9	l.mfspr r9,r0,SPR_SHADOW_GPR(9)
  71
  72#else /* !CONFIG_OPENRISC_HAVE_SHADOW_GPRS */
  73#define EMERGENCY_PRINT_STORE_GPR4	l.sw    0x20(r0),r4
  74#define EMERGENCY_PRINT_LOAD_GPR4	l.lwz   r4,0x20(r0)
  75
  76#define EMERGENCY_PRINT_STORE_GPR5	l.sw    0x24(r0),r5
  77#define EMERGENCY_PRINT_LOAD_GPR5	l.lwz   r5,0x24(r0)
  78
  79#define EMERGENCY_PRINT_STORE_GPR6	l.sw    0x28(r0),r6
  80#define EMERGENCY_PRINT_LOAD_GPR6	l.lwz   r6,0x28(r0)
  81
  82#define EMERGENCY_PRINT_STORE_GPR7	l.sw    0x2c(r0),r7
  83#define EMERGENCY_PRINT_LOAD_GPR7	l.lwz   r7,0x2c(r0)
  84
  85#define EMERGENCY_PRINT_STORE_GPR8	l.sw    0x30(r0),r8
  86#define EMERGENCY_PRINT_LOAD_GPR8	l.lwz   r8,0x30(r0)
  87
  88#define EMERGENCY_PRINT_STORE_GPR9	l.sw    0x34(r0),r9
  89#define EMERGENCY_PRINT_LOAD_GPR9	l.lwz   r9,0x34(r0)
  90
  91#endif
  92
  93/*
  94 * TLB miss handlers temorary stores
  95 */
  96#ifdef CONFIG_OPENRISC_HAVE_SHADOW_GPRS
  97#define EXCEPTION_STORE_GPR2		l.mtspr r0,r2,SPR_SHADOW_GPR(2)
  98#define EXCEPTION_LOAD_GPR2		l.mfspr r2,r0,SPR_SHADOW_GPR(2)
  99
 100#define EXCEPTION_STORE_GPR3		l.mtspr r0,r3,SPR_SHADOW_GPR(3)
 101#define EXCEPTION_LOAD_GPR3		l.mfspr r3,r0,SPR_SHADOW_GPR(3)
 102
 103#define EXCEPTION_STORE_GPR4		l.mtspr r0,r4,SPR_SHADOW_GPR(4)
 104#define EXCEPTION_LOAD_GPR4		l.mfspr r4,r0,SPR_SHADOW_GPR(4)
 105
 106#define EXCEPTION_STORE_GPR5		l.mtspr r0,r5,SPR_SHADOW_GPR(5)
 107#define EXCEPTION_LOAD_GPR5		l.mfspr r5,r0,SPR_SHADOW_GPR(5)
 108
 109#define EXCEPTION_STORE_GPR6		l.mtspr r0,r6,SPR_SHADOW_GPR(6)
 110#define EXCEPTION_LOAD_GPR6		l.mfspr r6,r0,SPR_SHADOW_GPR(6)
 111
 112#else /* !CONFIG_OPENRISC_HAVE_SHADOW_GPRS */
 113#define EXCEPTION_STORE_GPR2		l.sw    0x64(r0),r2
 114#define EXCEPTION_LOAD_GPR2		l.lwz   r2,0x64(r0)
 115
 116#define EXCEPTION_STORE_GPR3		l.sw    0x68(r0),r3
 117#define EXCEPTION_LOAD_GPR3		l.lwz   r3,0x68(r0)
 118
 119#define EXCEPTION_STORE_GPR4		l.sw    0x6c(r0),r4
 120#define EXCEPTION_LOAD_GPR4		l.lwz   r4,0x6c(r0)
 121
 122#define EXCEPTION_STORE_GPR5		l.sw    0x70(r0),r5
 123#define EXCEPTION_LOAD_GPR5		l.lwz   r5,0x70(r0)
 124
 125#define EXCEPTION_STORE_GPR6		l.sw    0x74(r0),r6
 126#define EXCEPTION_LOAD_GPR6		l.lwz   r6,0x74(r0)
 127
 128#endif
 129
 130/*
 131 * EXCEPTION_HANDLE temporary stores
 132 */
 133
 134#ifdef CONFIG_OPENRISC_HAVE_SHADOW_GPRS
 135#define EXCEPTION_T_STORE_GPR30		l.mtspr r0,r30,SPR_SHADOW_GPR(30)
 136#define EXCEPTION_T_LOAD_GPR30(reg)	l.mfspr reg,r0,SPR_SHADOW_GPR(30)
 137
 138#define EXCEPTION_T_STORE_GPR10		l.mtspr r0,r10,SPR_SHADOW_GPR(10)
 139#define EXCEPTION_T_LOAD_GPR10(reg)	l.mfspr reg,r0,SPR_SHADOW_GPR(10)
 140
 141#define EXCEPTION_T_STORE_SP		l.mtspr r0,r1,SPR_SHADOW_GPR(1)
 142#define EXCEPTION_T_LOAD_SP(reg)	l.mfspr reg,r0,SPR_SHADOW_GPR(1)
 143
 144#else /* !CONFIG_OPENRISC_HAVE_SHADOW_GPRS */
 145#define EXCEPTION_T_STORE_GPR30		l.sw    0x78(r0),r30
 146#define EXCEPTION_T_LOAD_GPR30(reg)	l.lwz   reg,0x78(r0)
 147
 148#define EXCEPTION_T_STORE_GPR10		l.sw    0x7c(r0),r10
 149#define EXCEPTION_T_LOAD_GPR10(reg)	l.lwz   reg,0x7c(r0)
 150
 151#define EXCEPTION_T_STORE_SP		l.sw    0x80(r0),r1
 152#define EXCEPTION_T_LOAD_SP(reg)	l.lwz   reg,0x80(r0)
 153#endif
 
 
 
 
 
 
 154
 155/* =========================================================[ macros ]=== */
 156
 157#ifdef CONFIG_SMP
 158#define GET_CURRENT_PGD(reg,t1)					\
 159	LOAD_SYMBOL_2_GPR(reg,current_pgd)			;\
 160	l.mfspr	t1,r0,SPR_COREID				;\
 161	l.slli	t1,t1,2						;\
 162	l.add	reg,reg,t1					;\
 163	tophys  (t1,reg)					;\
 164	l.lwz   reg,0(t1)
 165#else
 166#define GET_CURRENT_PGD(reg,t1)					\
 167	LOAD_SYMBOL_2_GPR(reg,current_pgd)			;\
 168	tophys  (t1,reg)					;\
 169	l.lwz   reg,0(t1)
 170#endif
 171
 172/* Load r10 from current_thread_info_set - clobbers r1 and r30 */
 173#ifdef CONFIG_SMP
 174#define GET_CURRENT_THREAD_INFO					\
 175	LOAD_SYMBOL_2_GPR(r1,current_thread_info_set)		;\
 176	tophys  (r30,r1)					;\
 177	l.mfspr	r10,r0,SPR_COREID				;\
 178	l.slli	r10,r10,2					;\
 179	l.add	r30,r30,r10					;\
 180	/* r10: current_thread_info  */				;\
 181	l.lwz   r10,0(r30)
 182#else
 183#define GET_CURRENT_THREAD_INFO					\
 184	LOAD_SYMBOL_2_GPR(r1,current_thread_info_set)		;\
 185	tophys  (r30,r1)					;\
 186	/* r10: current_thread_info  */				;\
 187	l.lwz   r10,0(r30)
 188#endif
 189
 190/*
 191 * DSCR: this is a common hook for handling exceptions. it will save
 192 *       the needed registers, set up stack and pointer to current
 193 *	 then jump to the handler while enabling MMU
 194 *
 195 * PRMS: handler	- a function to jump to. it has to save the
 196 *			remaining registers to kernel stack, call
 197 *			appropriate arch-independant exception handler
 198 *			and finaly jump to ret_from_except
 199 *
 200 * PREQ: unchanged state from the time exception happened
 201 *
 202 * POST: SAVED the following registers original value
 203 *	       to the new created exception frame pointed to by r1
 204 *
 205 *	 r1  - ksp	pointing to the new (exception) frame
 206 *	 r4  - EEAR     exception EA
 207 *	 r10 - current	pointing to current_thread_info struct
 208 *	 r12 - syscall  0, since we didn't come from syscall
 209 *	 r30 - handler	address of the handler we'll jump to
 
 210 *
 211 *	 handler has to save remaining registers to the exception
 212 *	 ksp frame *before* tainting them!
 213 *
 214 * NOTE: this function is not reentrant per se. reentrancy is guaranteed
 215 *       by processor disabling all exceptions/interrupts when exception
 216 *	 accours.
 217 *
 218 * OPTM: no need to make it so wasteful to extract ksp when in user mode
 219 */
 220
 221#define EXCEPTION_HANDLE(handler)				\
 222	EXCEPTION_T_STORE_GPR30					;\
 223	l.mfspr r30,r0,SPR_ESR_BASE				;\
 224	l.andi  r30,r30,SPR_SR_SM				;\
 225	l.sfeqi r30,0						;\
 226	EXCEPTION_T_STORE_GPR10					;\
 227	l.bnf   2f                            /* kernel_mode */	;\
 228	 EXCEPTION_T_STORE_SP                 /* delay slot */	;\
 2291: /* user_mode:   */						;\
 230	GET_CURRENT_THREAD_INFO	 				;\
 
 
 
 231	tophys  (r30,r10)					;\
 232	l.lwz   r1,(TI_KSP)(r30)				;\
 233	/* fall through */					;\
 2342: /* kernel_mode: */						;\
 235	/* create new stack frame, save only needed gprs */	;\
 236	/* r1: KSP, r10: current, r4: EEAR, r31: __pa(KSP) */	;\
 237	/* r12:	temp, syscall indicator */			;\
 238	l.addi  r1,r1,-(INT_FRAME_SIZE)				;\
 239	/* r1 is KSP, r30 is __pa(KSP) */			;\
 240	tophys  (r30,r1)					;\
 241	l.sw    PT_GPR12(r30),r12				;\
 242	/* r4 use for tmp before EA */				;\
 243	l.mfspr r12,r0,SPR_EPCR_BASE				;\
 244	l.sw    PT_PC(r30),r12					;\
 245	l.mfspr r12,r0,SPR_ESR_BASE				;\
 246	l.sw    PT_SR(r30),r12					;\
 247	/* save r30 */						;\
 248	EXCEPTION_T_LOAD_GPR30(r12)				;\
 249	l.sw	PT_GPR30(r30),r12				;\
 250	/* save r10 as was prior to exception */		;\
 251	EXCEPTION_T_LOAD_GPR10(r12)				;\
 252	l.sw	PT_GPR10(r30),r12				;\
 253	/* save PT_SP as was prior to exception */		;\
 254	EXCEPTION_T_LOAD_SP(r12)				;\
 255	l.sw	PT_SP(r30),r12					;\
 256	/* save exception r4, set r4 = EA */			;\
 257	l.sw	PT_GPR4(r30),r4					;\
 258	l.mfspr r4,r0,SPR_EEAR_BASE				;\
 259	/* r12 == 1 if we come from syscall */			;\
 260	CLEAR_GPR(r12)						;\
 261	/* ----- turn on MMU ----- */				;\
 262	/* Carry DSX into exception SR */			;\
 263	l.mfspr r30,r0,SPR_SR					;\
 264	l.andi	r30,r30,SPR_SR_DSX				;\
 265	l.ori	r30,r30,(EXCEPTION_SR)				;\
 266	l.mtspr	r0,r30,SPR_ESR_BASE				;\
 267	/* r30:	EA address of handler */			;\
 268	LOAD_SYMBOL_2_GPR(r30,handler)				;\
 269	l.mtspr r0,r30,SPR_EPCR_BASE				;\
 270	l.rfe
 271
 272/*
 273 * this doesn't work
 274 *
 275 *
 276 * #ifdef CONFIG_JUMP_UPON_UNHANDLED_EXCEPTION
 277 * #define UNHANDLED_EXCEPTION(handler)				\
 278 *	l.ori   r3,r0,0x1					;\
 279 *	l.mtspr r0,r3,SPR_SR					;\
 280 *      l.movhi r3,hi(0xf0000100)				;\
 281 *      l.ori   r3,r3,lo(0xf0000100)				;\
 282 *	l.jr	r3						;\
 283 *	l.nop	1
 284 *
 285 * #endif
 286 */
 287
 288/* DSCR: this is the same as EXCEPTION_HANDLE(), we are just
 289 *       a bit more carefull (if we have a PT_SP or current pointer
 290 *       corruption) and set them up from 'current_set'
 291 *
 292 */
 293#define UNHANDLED_EXCEPTION(handler)				\
 294	EXCEPTION_T_STORE_GPR30					;\
 295	EXCEPTION_T_STORE_GPR10					;\
 296	EXCEPTION_T_STORE_SP					;\
 297	/* temporary store r3, r9 into r1, r10 */		;\
 298	l.addi	r1,r3,0x0					;\
 299	l.addi	r10,r9,0x0					;\
 300	LOAD_SYMBOL_2_GPR(r9,_string_unhandled_exception)	;\
 301	tophys	(r3,r9)						;\
 302	l.jal	_emergency_print				;\
 303	 l.nop							;\
 304	l.mfspr	r3,r0,SPR_NPC					;\
 305	l.jal	_emergency_print_nr				;\
 306	 l.andi	r3,r3,0x1f00					;\
 307	LOAD_SYMBOL_2_GPR(r9,_string_epc_prefix)		;\
 308	tophys	(r3,r9)						;\
 309	l.jal	_emergency_print				;\
 310	 l.nop							;\
 311	l.jal	_emergency_print_nr				;\
 312	 l.mfspr r3,r0,SPR_EPCR_BASE				;\
 313	LOAD_SYMBOL_2_GPR(r9,_string_nl)			;\
 314	tophys	(r3,r9)						;\
 315	l.jal	_emergency_print				;\
 316	 l.nop							;\
 317	/* end of printing */					;\
 318	l.addi	r3,r1,0x0					;\
 319	l.addi	r9,r10,0x0					;\
 320	/* extract current, ksp from current_set */		;\
 321	LOAD_SYMBOL_2_GPR(r1,_unhandled_stack_top)		;\
 322	LOAD_SYMBOL_2_GPR(r10,init_thread_union)		;\
 323	/* create new stack frame, save only needed gprs */	;\
 324	/* r1: KSP, r10: current, r31: __pa(KSP) */		;\
 325	/* r12:	temp, syscall indicator, r13 temp */		;\
 326	l.addi  r1,r1,-(INT_FRAME_SIZE)				;\
 327	/* r1 is KSP, r30 is __pa(KSP) */			;\
 328	tophys  (r30,r1)					;\
 329	l.sw    PT_GPR12(r30),r12				;\
 330	l.mfspr r12,r0,SPR_EPCR_BASE				;\
 331	l.sw    PT_PC(r30),r12					;\
 332	l.mfspr r12,r0,SPR_ESR_BASE				;\
 333	l.sw    PT_SR(r30),r12					;\
 334	/* save r31 */						;\
 335	EXCEPTION_T_LOAD_GPR30(r12)				;\
 336	l.sw	PT_GPR30(r30),r12				;\
 337	/* save r10 as was prior to exception */		;\
 338	EXCEPTION_T_LOAD_GPR10(r12)				;\
 339	l.sw	PT_GPR10(r30),r12				;\
 340	/* save PT_SP as was prior to exception */		;\
 341	EXCEPTION_T_LOAD_SP(r12)				;\
 342	l.sw	PT_SP(r30),r12					;\
 343	l.sw    PT_GPR13(r30),r13				;\
 344	/* --> */						;\
 345	/* save exception r4, set r4 = EA */			;\
 346	l.sw	PT_GPR4(r30),r4					;\
 347	l.mfspr r4,r0,SPR_EEAR_BASE				;\
 348	/* r12 == 1 if we come from syscall */			;\
 349	CLEAR_GPR(r12)						;\
 350	/* ----- play a MMU trick ----- */			;\
 351	l.ori	r30,r0,(EXCEPTION_SR)				;\
 352	l.mtspr	r0,r30,SPR_ESR_BASE				;\
 353	/* r31:	EA address of handler */			;\
 354	LOAD_SYMBOL_2_GPR(r30,handler)				;\
 355	l.mtspr r0,r30,SPR_EPCR_BASE				;\
 356	l.rfe
 357
 358/* =====================================================[ exceptions] === */
 359
 360	__HEAD
 361
 362/* ---[ 0x100: RESET exception ]----------------------------------------- */
 363    .org 0x100
 364	/* Jump to .init code at _start which lives in the .head section
 365	 * and will be discarded after boot.
 366	 */
 367	LOAD_SYMBOL_2_GPR(r15, _start)
 368	tophys	(r13,r15)			/* MMU disabled */
 369	l.jr	r13
 370	 l.nop
 371
 372/* ---[ 0x200: BUS exception ]------------------------------------------- */
 373    .org 0x200
 374_dispatch_bus_fault:
 375	EXCEPTION_HANDLE(_bus_fault_handler)
 376
 377/* ---[ 0x300: Data Page Fault exception ]------------------------------- */
 378    .org 0x300
 379_dispatch_do_dpage_fault:
 380//      totaly disable timer interrupt
 381// 	l.mtspr	r0,r0,SPR_TTMR
 382//	DEBUG_TLB_PROBE(0x300)
 383//	EXCEPTION_DEBUG_VALUE_ER_ENABLED(0x300)
 384	EXCEPTION_HANDLE(_data_page_fault_handler)
 385
 386/* ---[ 0x400: Insn Page Fault exception ]------------------------------- */
 387    .org 0x400
 388_dispatch_do_ipage_fault:
 389//      totaly disable timer interrupt
 390//	l.mtspr	r0,r0,SPR_TTMR
 391//	DEBUG_TLB_PROBE(0x400)
 392//	EXCEPTION_DEBUG_VALUE_ER_ENABLED(0x400)
 393	EXCEPTION_HANDLE(_insn_page_fault_handler)
 394
 395/* ---[ 0x500: Timer exception ]----------------------------------------- */
 396    .org 0x500
 397	EXCEPTION_HANDLE(_timer_handler)
 398
 399/* ---[ 0x600: Alignment exception ]------------------------------------- */
 400    .org 0x600
 401	EXCEPTION_HANDLE(_alignment_handler)
 402
 403/* ---[ 0x700: Illegal insn exception ]---------------------------------- */
 404    .org 0x700
 405	EXCEPTION_HANDLE(_illegal_instruction_handler)
 406
 407/* ---[ 0x800: External interrupt exception ]---------------------------- */
 408    .org 0x800
 409	EXCEPTION_HANDLE(_external_irq_handler)
 410
 411/* ---[ 0x900: DTLB miss exception ]------------------------------------- */
 412    .org 0x900
 413	l.j	boot_dtlb_miss_handler
 414	l.nop
 415
 416/* ---[ 0xa00: ITLB miss exception ]------------------------------------- */
 417    .org 0xa00
 418	l.j	boot_itlb_miss_handler
 419	l.nop
 420
 421/* ---[ 0xb00: Range exception ]----------------------------------------- */
 422    .org 0xb00
 423	UNHANDLED_EXCEPTION(_vector_0xb00)
 424
 425/* ---[ 0xc00: Syscall exception ]--------------------------------------- */
 426    .org 0xc00
 427	EXCEPTION_HANDLE(_sys_call_handler)
 428
 429/* ---[ 0xd00: Floating point exception ]-------------------------------- */
 430    .org 0xd00
 431	EXCEPTION_HANDLE(_fpe_trap_handler)
 432
 433/* ---[ 0xe00: Trap exception ]------------------------------------------ */
 434    .org 0xe00
 435//	UNHANDLED_EXCEPTION(_vector_0xe00)
 436	EXCEPTION_HANDLE(_trap_handler)
 437
 438/* ---[ 0xf00: Reserved exception ]-------------------------------------- */
 439    .org 0xf00
 440	UNHANDLED_EXCEPTION(_vector_0xf00)
 441
 442/* ---[ 0x1000: Reserved exception ]------------------------------------- */
 443    .org 0x1000
 444	UNHANDLED_EXCEPTION(_vector_0x1000)
 445
 446/* ---[ 0x1100: Reserved exception ]------------------------------------- */
 447    .org 0x1100
 448	UNHANDLED_EXCEPTION(_vector_0x1100)
 449
 450/* ---[ 0x1200: Reserved exception ]------------------------------------- */
 451    .org 0x1200
 452	UNHANDLED_EXCEPTION(_vector_0x1200)
 453
 454/* ---[ 0x1300: Reserved exception ]------------------------------------- */
 455    .org 0x1300
 456	UNHANDLED_EXCEPTION(_vector_0x1300)
 457
 458/* ---[ 0x1400: Reserved exception ]------------------------------------- */
 459    .org 0x1400
 460	UNHANDLED_EXCEPTION(_vector_0x1400)
 461
 462/* ---[ 0x1500: Reserved exception ]------------------------------------- */
 463    .org 0x1500
 464	UNHANDLED_EXCEPTION(_vector_0x1500)
 465
 466/* ---[ 0x1600: Reserved exception ]------------------------------------- */
 467    .org 0x1600
 468	UNHANDLED_EXCEPTION(_vector_0x1600)
 469
 470/* ---[ 0x1700: Reserved exception ]------------------------------------- */
 471    .org 0x1700
 472	UNHANDLED_EXCEPTION(_vector_0x1700)
 473
 474/* ---[ 0x1800: Reserved exception ]------------------------------------- */
 475    .org 0x1800
 476	UNHANDLED_EXCEPTION(_vector_0x1800)
 477
 478/* ---[ 0x1900: Reserved exception ]------------------------------------- */
 479    .org 0x1900
 480	UNHANDLED_EXCEPTION(_vector_0x1900)
 481
 482/* ---[ 0x1a00: Reserved exception ]------------------------------------- */
 483    .org 0x1a00
 484	UNHANDLED_EXCEPTION(_vector_0x1a00)
 485
 486/* ---[ 0x1b00: Reserved exception ]------------------------------------- */
 487    .org 0x1b00
 488	UNHANDLED_EXCEPTION(_vector_0x1b00)
 489
 490/* ---[ 0x1c00: Reserved exception ]------------------------------------- */
 491    .org 0x1c00
 492	UNHANDLED_EXCEPTION(_vector_0x1c00)
 493
 494/* ---[ 0x1d00: Reserved exception ]------------------------------------- */
 495    .org 0x1d00
 496	UNHANDLED_EXCEPTION(_vector_0x1d00)
 497
 498/* ---[ 0x1e00: Reserved exception ]------------------------------------- */
 499    .org 0x1e00
 500	UNHANDLED_EXCEPTION(_vector_0x1e00)
 501
 502/* ---[ 0x1f00: Reserved exception ]------------------------------------- */
 503    .org 0x1f00
 504	UNHANDLED_EXCEPTION(_vector_0x1f00)
 505
 506    .org 0x2000
 507/* ===================================================[ kernel start ]=== */
 508
 509/*    .text*/
 510
 511/* This early stuff belongs in the .init.text section, but some of the functions below definitely
 512 * don't... */
 513
 514	__INIT
 515	.global _start
 516_start:
 517	/* Init r0 to zero as per spec */
 518	CLEAR_GPR(r0)
 519
 520	/* save kernel parameters */
 521	l.or	r25,r0,r3	/* pointer to fdt */
 522
 523	/*
 524	 * ensure a deterministic start
 525	 */
 526
 527	l.ori	r3,r0,0x1
 528	l.mtspr	r0,r3,SPR_SR
 529
 530	/*
 531	 * Start the TTCR as early as possible, so that the RNG can make use of
 532	 * measurements of boot time from the earliest opportunity. Especially
 533	 * important is that the TTCR does not return zero by the time we reach
 534	 * random_init().
 535	 */
 536	l.movhi r3,hi(SPR_TTMR_CR)
 537	l.mtspr r0,r3,SPR_TTMR
 538
 539	CLEAR_GPR(r1)
 540	CLEAR_GPR(r2)
 541	CLEAR_GPR(r3)
 542	CLEAR_GPR(r4)
 543	CLEAR_GPR(r5)
 544	CLEAR_GPR(r6)
 545	CLEAR_GPR(r7)
 546	CLEAR_GPR(r8)
 547	CLEAR_GPR(r9)
 548	CLEAR_GPR(r10)
 549	CLEAR_GPR(r11)
 550	CLEAR_GPR(r12)
 551	CLEAR_GPR(r13)
 552	CLEAR_GPR(r14)
 553	CLEAR_GPR(r15)
 554	CLEAR_GPR(r16)
 555	CLEAR_GPR(r17)
 556	CLEAR_GPR(r18)
 557	CLEAR_GPR(r19)
 558	CLEAR_GPR(r20)
 559	CLEAR_GPR(r21)
 560	CLEAR_GPR(r22)
 561	CLEAR_GPR(r23)
 562	CLEAR_GPR(r24)
 563	CLEAR_GPR(r26)
 564	CLEAR_GPR(r27)
 565	CLEAR_GPR(r28)
 566	CLEAR_GPR(r29)
 567	CLEAR_GPR(r30)
 568	CLEAR_GPR(r31)
 569
 570#ifdef CONFIG_SMP
 571	l.mfspr	r26,r0,SPR_COREID
 572	l.sfeq	r26,r0
 573	l.bnf	secondary_wait
 574	 l.nop
 575#endif
 576	/*
 577	 * set up initial ksp and current
 578	 */
 579	/* setup kernel stack */
 580	LOAD_SYMBOL_2_GPR(r1,init_thread_union + THREAD_SIZE)
 581	LOAD_SYMBOL_2_GPR(r10,init_thread_union)	// setup current
 582	tophys	(r31,r10)
 583	l.sw	TI_KSP(r31), r1
 584
 585	l.ori	r4,r0,0x0
 586
 587
 588	/*
 589	 * .data contains initialized data,
 590	 * .bss contains uninitialized data - clear it up
 591	 */
 592clear_bss:
 593	LOAD_SYMBOL_2_GPR(r24, __bss_start)
 594	LOAD_SYMBOL_2_GPR(r26, _end)
 595	tophys(r28,r24)
 596	tophys(r30,r26)
 597	CLEAR_GPR(r24)
 598	CLEAR_GPR(r26)
 5991:
 600	l.sw    (0)(r28),r0
 601	l.sfltu r28,r30
 602	l.bf    1b
 603	l.addi  r28,r28,4
 604
 605enable_ic:
 606	l.jal	_ic_enable
 607	 l.nop
 608
 609enable_dc:
 610	l.jal	_dc_enable
 611	 l.nop
 612
 613flush_tlb:
 614	l.jal	_flush_tlb
 615	 l.nop
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 616
 617/* The MMU needs to be enabled before or1k_early_setup is called */
 618
 619enable_mmu:
 620	/*
 621	 * enable dmmu & immu
 622	 * SR[5] = 0, SR[6] = 0, 6th and 7th bit of SR set to 0
 623	 */
 624	l.mfspr	r30,r0,SPR_SR
 625	l.movhi	r28,hi(SPR_SR_DME | SPR_SR_IME)
 626	l.ori	r28,r28,lo(SPR_SR_DME | SPR_SR_IME)
 627	l.or	r30,r30,r28
 628	l.mtspr	r0,r30,SPR_SR
 629	l.nop
 630	l.nop
 631	l.nop
 632	l.nop
 633	l.nop
 634	l.nop
 635	l.nop
 636	l.nop
 637	l.nop
 638	l.nop
 639	l.nop
 640	l.nop
 641	l.nop
 642	l.nop
 643	l.nop
 644	l.nop
 645
 646	// reset the simulation counters
 647	l.nop 5
 648
 649	/* check fdt header magic word */
 650	l.lwz	r3,0(r25)	/* load magic from fdt into r3 */
 651	l.movhi	r4,hi(OF_DT_HEADER)
 652	l.ori	r4,r4,lo(OF_DT_HEADER)
 653	l.sfeq	r3,r4
 654	l.bf	_fdt_found
 655	 l.nop
 656	/* magic number mismatch, set fdt pointer to null */
 657	l.or	r25,r0,r0
 658_fdt_found:
 659	/* pass fdt pointer to or1k_early_setup in r3 */
 660	l.or	r3,r0,r25
 661	LOAD_SYMBOL_2_GPR(r24, or1k_early_setup)
 662	l.jalr r24
 663	 l.nop
 664
 665clear_regs:
 666	/*
 667	 * clear all GPRS to increase determinism
 668	 */
 669	CLEAR_GPR(r2)
 670	CLEAR_GPR(r3)
 671	CLEAR_GPR(r4)
 672	CLEAR_GPR(r5)
 673	CLEAR_GPR(r6)
 674	CLEAR_GPR(r7)
 675	CLEAR_GPR(r8)
 676	CLEAR_GPR(r9)
 677	CLEAR_GPR(r11)
 678	CLEAR_GPR(r12)
 679	CLEAR_GPR(r13)
 680	CLEAR_GPR(r14)
 681	CLEAR_GPR(r15)
 682	CLEAR_GPR(r16)
 683	CLEAR_GPR(r17)
 684	CLEAR_GPR(r18)
 685	CLEAR_GPR(r19)
 686	CLEAR_GPR(r20)
 687	CLEAR_GPR(r21)
 688	CLEAR_GPR(r22)
 689	CLEAR_GPR(r23)
 690	CLEAR_GPR(r24)
 691	CLEAR_GPR(r25)
 692	CLEAR_GPR(r26)
 693	CLEAR_GPR(r27)
 694	CLEAR_GPR(r28)
 695	CLEAR_GPR(r29)
 696	CLEAR_GPR(r30)
 697	CLEAR_GPR(r31)
 698
 699jump_start_kernel:
 700	/*
 701	 * jump to kernel entry (start_kernel)
 702	 */
 703	LOAD_SYMBOL_2_GPR(r30, start_kernel)
 704	l.jr    r30
 705	 l.nop
 706
 707_flush_tlb:
 708	/*
 709	 *  I N V A L I D A T E   T L B   e n t r i e s
 710	 */
 711	LOAD_SYMBOL_2_GPR(r5,SPR_DTLBMR_BASE(0))
 712	LOAD_SYMBOL_2_GPR(r6,SPR_ITLBMR_BASE(0))
 713	l.addi	r7,r0,128 /* Maximum number of sets */
 7141:
 715	l.mtspr	r5,r0,0x0
 716	l.mtspr	r6,r0,0x0
 717
 718	l.addi	r5,r5,1
 719	l.addi	r6,r6,1
 720	l.sfeq	r7,r0
 721	l.bnf	1b
 722	 l.addi	r7,r7,-1
 723
 724	l.jr	r9
 725	 l.nop
 726
 727#ifdef CONFIG_SMP
 728secondary_wait:
 729	/* Doze the cpu until we are asked to run */
 730	/* If we dont have power management skip doze */
 731	l.mfspr r25,r0,SPR_UPR
 732	l.andi  r25,r25,SPR_UPR_PMP
 733	l.sfeq  r25,r0
 734	l.bf	secondary_check_release
 735	 l.nop
 736
 737	/* Setup special secondary exception handler */
 738	LOAD_SYMBOL_2_GPR(r3, _secondary_evbar)
 739	tophys(r25,r3)
 740	l.mtspr	r0,r25,SPR_EVBAR
 741
 742	/* Enable Interrupts */
 743	l.mfspr	r25,r0,SPR_SR
 744	l.ori	r25,r25,SPR_SR_IEE
 745	l.mtspr	r0,r25,SPR_SR
 746
 747	/* Unmask interrupts interrupts */
 748	l.mfspr r25,r0,SPR_PICMR
 749	l.ori   r25,r25,0xffff
 750	l.mtspr	r0,r25,SPR_PICMR
 751
 752	/* Doze */
 753	l.mfspr r25,r0,SPR_PMR
 754	LOAD_SYMBOL_2_GPR(r3, SPR_PMR_DME)
 755	l.or    r25,r25,r3
 756	l.mtspr r0,r25,SPR_PMR
 757
 758	/* Wakeup - Restore exception handler */
 759	l.mtspr	r0,r0,SPR_EVBAR
 760
 761secondary_check_release:
 762	/*
 763	 * Check if we actually got the release signal, if not go-back to
 764	 * sleep.
 765	 */
 766	l.mfspr	r25,r0,SPR_COREID
 767	LOAD_SYMBOL_2_GPR(r3, secondary_release)
 768	tophys(r4, r3)
 769	l.lwz	r3,0(r4)
 770	l.sfeq	r25,r3
 771	l.bnf	secondary_wait
 772	 l.nop
 773	/* fall through to secondary_init */
 774
 775secondary_init:
 776	/*
 777	 * set up initial ksp and current
 778	 */
 779	LOAD_SYMBOL_2_GPR(r10, secondary_thread_info)
 780	tophys	(r30,r10)
 781	l.lwz	r10,0(r30)
 782	l.addi	r1,r10,THREAD_SIZE
 783	tophys	(r30,r10)
 784	l.sw	TI_KSP(r30),r1
 785
 786	l.jal	_ic_enable
 787	 l.nop
 788
 789	l.jal	_dc_enable
 790	 l.nop
 791
 792	l.jal	_flush_tlb
 793	 l.nop
 794
 795	/*
 796	 * enable dmmu & immu
 797	 */
 798	l.mfspr	r30,r0,SPR_SR
 799	l.movhi	r28,hi(SPR_SR_DME | SPR_SR_IME)
 800	l.ori	r28,r28,lo(SPR_SR_DME | SPR_SR_IME)
 801	l.or	r30,r30,r28
 802	/*
 803	 * This is a bit tricky, we need to switch over from physical addresses
 804	 * to virtual addresses on the fly.
 805	 * To do that, we first set up ESR with the IME and DME bits set.
 806	 * Then EPCR is set to secondary_start and then a l.rfe is issued to
 807	 * "jump" to that.
 808	 */
 809	l.mtspr	r0,r30,SPR_ESR_BASE
 810	LOAD_SYMBOL_2_GPR(r30, secondary_start)
 811	l.mtspr	r0,r30,SPR_EPCR_BASE
 812	l.rfe
 813
 814secondary_start:
 815	LOAD_SYMBOL_2_GPR(r30, secondary_start_kernel)
 816	l.jr    r30
 817	 l.nop
 818
 819#endif
 820
 821/* ==========================================================[ cache ]=== */
 822
 823	/* alignment here so we don't change memory offsets with
 824	 * memory controller defined
 825	 */
 826	.align 0x2000
 827
 828_ic_enable:
 829	/* Check if IC present and skip enabling otherwise */
 830	l.mfspr r24,r0,SPR_UPR
 831	l.andi  r26,r24,SPR_UPR_ICP
 832	l.sfeq  r26,r0
 833	l.bf	9f
 834	l.nop
 835
 836	/* Disable IC */
 837	l.mfspr r6,r0,SPR_SR
 838	l.addi  r5,r0,-1
 839	l.xori  r5,r5,SPR_SR_ICE
 840	l.and   r5,r6,r5
 841	l.mtspr r0,r5,SPR_SR
 842
 843	/* Establish cache block size
 844	   If BS=0, 16;
 845	   If BS=1, 32;
 846	   r14 contain block size
 847	*/
 848	l.mfspr r24,r0,SPR_ICCFGR
 849	l.andi	r26,r24,SPR_ICCFGR_CBS
 850	l.srli	r28,r26,7
 851	l.ori	r30,r0,16
 852	l.sll	r14,r30,r28
 853
 854	/* Establish number of cache sets
 855	   r16 contains number of cache sets
 856	   r28 contains log(# of cache sets)
 857	*/
 858	l.andi  r26,r24,SPR_ICCFGR_NCS
 859	l.srli 	r28,r26,3
 860	l.ori   r30,r0,1
 861	l.sll   r16,r30,r28
 862
 863	/* Invalidate IC */
 864	l.addi  r6,r0,0
 865	l.sll   r5,r14,r28
 866//        l.mul   r5,r14,r16
 867//	l.trap  1
 868//	l.addi  r5,r0,IC_SIZE
 8691:
 870	l.mtspr r0,r6,SPR_ICBIR
 871	l.sfne  r6,r5
 872	l.bf    1b
 873	l.add   r6,r6,r14
 874 //       l.addi   r6,r6,IC_LINE
 875
 876	/* Enable IC */
 877	l.mfspr r6,r0,SPR_SR
 878	l.ori   r6,r6,SPR_SR_ICE
 879	l.mtspr r0,r6,SPR_SR
 880	l.nop
 881	l.nop
 882	l.nop
 883	l.nop
 884	l.nop
 885	l.nop
 886	l.nop
 887	l.nop
 888	l.nop
 889	l.nop
 8909:
 891	l.jr    r9
 892	l.nop
 893
 894_dc_enable:
 895	/* Check if DC present and skip enabling otherwise */
 896	l.mfspr r24,r0,SPR_UPR
 897	l.andi  r26,r24,SPR_UPR_DCP
 898	l.sfeq  r26,r0
 899	l.bf	9f
 900	l.nop
 901
 902	/* Disable DC */
 903	l.mfspr r6,r0,SPR_SR
 904	l.addi  r5,r0,-1
 905	l.xori  r5,r5,SPR_SR_DCE
 906	l.and   r5,r6,r5
 907	l.mtspr r0,r5,SPR_SR
 908
 909	/* Establish cache block size
 910	   If BS=0, 16;
 911	   If BS=1, 32;
 912	   r14 contain block size
 913	*/
 914	l.mfspr r24,r0,SPR_DCCFGR
 915	l.andi	r26,r24,SPR_DCCFGR_CBS
 916	l.srli	r28,r26,7
 917	l.ori	r30,r0,16
 918	l.sll	r14,r30,r28
 919
 920	/* Establish number of cache sets
 921	   r16 contains number of cache sets
 922	   r28 contains log(# of cache sets)
 923	*/
 924	l.andi  r26,r24,SPR_DCCFGR_NCS
 925	l.srli 	r28,r26,3
 926	l.ori   r30,r0,1
 927	l.sll   r16,r30,r28
 928
 929	/* Invalidate DC */
 930	l.addi  r6,r0,0
 931	l.sll   r5,r14,r28
 9321:
 933	l.mtspr r0,r6,SPR_DCBIR
 934	l.sfne  r6,r5
 935	l.bf    1b
 936	l.add   r6,r6,r14
 937
 938	/* Enable DC */
 939	l.mfspr r6,r0,SPR_SR
 940	l.ori   r6,r6,SPR_SR_DCE
 941	l.mtspr r0,r6,SPR_SR
 9429:
 943	l.jr    r9
 944	l.nop
 945
 946/* ===============================================[ page table masks ]=== */
 947
 948#define DTLB_UP_CONVERT_MASK  0x3fa
 949#define ITLB_UP_CONVERT_MASK  0x3a
 950
 951/* for SMP we'd have (this is a bit subtle, CC must be always set
 952 * for SMP, but since we have _PAGE_PRESENT bit always defined
 953 * we can just modify the mask)
 954 */
 955#define DTLB_SMP_CONVERT_MASK  0x3fb
 956#define ITLB_SMP_CONVERT_MASK  0x3b
 957
 958/* ---[ boot dtlb miss handler ]----------------------------------------- */
 959
 960boot_dtlb_miss_handler:
 961
 962/* mask for DTLB_MR register: - (0) sets V (valid) bit,
 963 *                            - (31-12) sets bits belonging to VPN (31-12)
 964 */
 965#define DTLB_MR_MASK 0xfffff001
 966
 967/* mask for DTLB_TR register: - (2) sets CI (cache inhibit) bit,
 968 *			      - (4) sets A (access) bit,
 969 *                            - (5) sets D (dirty) bit,
 970 *                            - (8) sets SRE (superuser read) bit
 971 *                            - (9) sets SWE (superuser write) bit
 972 *                            - (31-12) sets bits belonging to VPN (31-12)
 973 */
 974#define DTLB_TR_MASK 0xfffff332
 975
 976/* These are for masking out the VPN/PPN value from the MR/TR registers...
 977 * it's not the same as the PFN */
 978#define VPN_MASK 0xfffff000
 979#define PPN_MASK 0xfffff000
 980
 981
 982	EXCEPTION_STORE_GPR6
 983
 984#if 0
 985	l.mfspr r6,r0,SPR_ESR_BASE	   //
 986	l.andi  r6,r6,SPR_SR_SM            // are we in kernel mode ?
 987	l.sfeqi r6,0                       // r6 == 0x1 --> SM
 988	l.bf    exit_with_no_dtranslation  //
 989	l.nop
 990#endif
 991
 992	/* this could be optimized by moving storing of
 993	 * non r6 registers here, and jumping r6 restore
 994	 * if not in supervisor mode
 995	 */
 996
 997	EXCEPTION_STORE_GPR2
 998	EXCEPTION_STORE_GPR3
 999	EXCEPTION_STORE_GPR4
1000	EXCEPTION_STORE_GPR5
1001
1002	l.mfspr r4,r0,SPR_EEAR_BASE        // get the offending EA
1003
1004immediate_translation:
1005	CLEAR_GPR(r6)
1006
1007	l.srli	r3,r4,0xd                  // r3 <- r4 / 8192 (sets are relative to page size (8Kb) NOT VPN size (4Kb)
1008
1009	l.mfspr r6, r0, SPR_DMMUCFGR
1010	l.andi	r6, r6, SPR_DMMUCFGR_NTS
1011	l.srli	r6, r6, SPR_DMMUCFGR_NTS_OFF
1012	l.ori	r5, r0, 0x1
1013	l.sll	r5, r5, r6 	// r5 = number DMMU sets
1014	l.addi	r6, r5, -1  	// r6 = nsets mask
1015	l.and	r2, r3, r6	// r2 <- r3 % NSETS_MASK
1016
1017	l.or    r6,r6,r4                   // r6 <- r4
1018	l.ori   r6,r6,~(VPN_MASK)          // r6 <- VPN :VPN .xfff - clear up lo(r6) to 0x**** *fff
1019	l.movhi r5,hi(DTLB_MR_MASK)        // r5 <- ffff:0000.x000
1020	l.ori   r5,r5,lo(DTLB_MR_MASK)     // r5 <- ffff:1111.x001 - apply DTLB_MR_MASK
1021	l.and   r5,r5,r6                   // r5 <- VPN :VPN .x001 - we have DTLBMR entry
1022	l.mtspr r2,r5,SPR_DTLBMR_BASE(0)   // set DTLBMR
1023
1024	/* set up DTLB with no translation for EA <= 0xbfffffff */
1025	LOAD_SYMBOL_2_GPR(r6,0xbfffffff)
1026	l.sfgeu  r6,r4                     // flag if r6 >= r4 (if 0xbfffffff >= EA)
1027	l.bf     1f                        // goto out
1028	l.and    r3,r4,r4                  // delay slot :: 24 <- r4 (if flag==1)
1029
1030	tophys(r3,r4)                      // r3 <- PA
10311:
1032	l.ori   r3,r3,~(PPN_MASK)          // r3 <- PPN :PPN .xfff - clear up lo(r6) to 0x**** *fff
1033	l.movhi r5,hi(DTLB_TR_MASK)        // r5 <- ffff:0000.x000
1034	l.ori   r5,r5,lo(DTLB_TR_MASK)     // r5 <- ffff:1111.x330 - apply DTLB_MR_MASK
1035	l.and   r5,r5,r3                   // r5 <- PPN :PPN .x330 - we have DTLBTR entry
1036	l.mtspr r2,r5,SPR_DTLBTR_BASE(0)   // set DTLBTR
1037
1038	EXCEPTION_LOAD_GPR6
1039	EXCEPTION_LOAD_GPR5
1040	EXCEPTION_LOAD_GPR4
1041	EXCEPTION_LOAD_GPR3
1042	EXCEPTION_LOAD_GPR2
1043
1044	l.rfe                              // SR <- ESR, PC <- EPC
1045
1046exit_with_no_dtranslation:
1047	/* EA out of memory or not in supervisor mode */
1048	EXCEPTION_LOAD_GPR6
1049	EXCEPTION_LOAD_GPR4
1050	l.j	_dispatch_bus_fault
1051
1052/* ---[ boot itlb miss handler ]----------------------------------------- */
1053
1054boot_itlb_miss_handler:
1055
1056/* mask for ITLB_MR register: - sets V (valid) bit,
1057 *                            - sets bits belonging to VPN (15-12)
1058 */
1059#define ITLB_MR_MASK 0xfffff001
1060
1061/* mask for ITLB_TR register: - sets A (access) bit,
1062 *                            - sets SXE (superuser execute) bit
1063 *                            - sets bits belonging to VPN (15-12)
1064 */
1065#define ITLB_TR_MASK 0xfffff050
1066
1067/*
1068#define VPN_MASK 0xffffe000
1069#define PPN_MASK 0xffffe000
1070*/
1071
1072
1073
1074	EXCEPTION_STORE_GPR2
1075	EXCEPTION_STORE_GPR3
1076	EXCEPTION_STORE_GPR4
1077	EXCEPTION_STORE_GPR5
1078	EXCEPTION_STORE_GPR6
1079
1080#if 0
1081	l.mfspr r6,r0,SPR_ESR_BASE         //
1082	l.andi  r6,r6,SPR_SR_SM            // are we in kernel mode ?
1083	l.sfeqi r6,0                       // r6 == 0x1 --> SM
1084	l.bf    exit_with_no_itranslation
1085	l.nop
1086#endif
1087
1088
1089	l.mfspr r4,r0,SPR_EEAR_BASE        // get the offending EA
1090
1091earlyearly:
1092	CLEAR_GPR(r6)
1093
1094	l.srli  r3,r4,0xd                  // r3 <- r4 / 8192 (sets are relative to page size (8Kb) NOT VPN size (4Kb)
1095
1096	l.mfspr r6, r0, SPR_IMMUCFGR
1097	l.andi	r6, r6, SPR_IMMUCFGR_NTS
1098	l.srli	r6, r6, SPR_IMMUCFGR_NTS_OFF
1099	l.ori	r5, r0, 0x1
1100	l.sll	r5, r5, r6 	// r5 = number IMMU sets from IMMUCFGR
1101	l.addi	r6, r5, -1  	// r6 = nsets mask
1102	l.and	r2, r3, r6	// r2 <- r3 % NSETS_MASK
1103
1104	l.or    r6,r6,r4                   // r6 <- r4
1105	l.ori   r6,r6,~(VPN_MASK)          // r6 <- VPN :VPN .xfff - clear up lo(r6) to 0x**** *fff
1106	l.movhi r5,hi(ITLB_MR_MASK)        // r5 <- ffff:0000.x000
1107	l.ori   r5,r5,lo(ITLB_MR_MASK)     // r5 <- ffff:1111.x001 - apply ITLB_MR_MASK
1108	l.and   r5,r5,r6                   // r5 <- VPN :VPN .x001 - we have ITLBMR entry
1109	l.mtspr r2,r5,SPR_ITLBMR_BASE(0)   // set ITLBMR
1110
1111	/*
1112	 * set up ITLB with no translation for EA <= 0x0fffffff
1113	 *
1114	 * we need this for head.S mapping (EA = PA). if we move all functions
1115	 * which run with mmu enabled into entry.S, we might be able to eliminate this.
1116	 *
1117	 */
1118	LOAD_SYMBOL_2_GPR(r6,0x0fffffff)
1119	l.sfgeu  r6,r4                     // flag if r6 >= r4 (if 0xb0ffffff >= EA)
1120	l.bf     1f                        // goto out
1121	l.and    r3,r4,r4                  // delay slot :: 24 <- r4 (if flag==1)
1122
1123	tophys(r3,r4)                      // r3 <- PA
11241:
1125	l.ori   r3,r3,~(PPN_MASK)          // r3 <- PPN :PPN .xfff - clear up lo(r6) to 0x**** *fff
1126	l.movhi r5,hi(ITLB_TR_MASK)        // r5 <- ffff:0000.x000
1127	l.ori   r5,r5,lo(ITLB_TR_MASK)     // r5 <- ffff:1111.x050 - apply ITLB_MR_MASK
1128	l.and   r5,r5,r3                   // r5 <- PPN :PPN .x050 - we have ITLBTR entry
1129	l.mtspr r2,r5,SPR_ITLBTR_BASE(0)   // set ITLBTR
1130
1131	EXCEPTION_LOAD_GPR6
1132	EXCEPTION_LOAD_GPR5
1133	EXCEPTION_LOAD_GPR4
1134	EXCEPTION_LOAD_GPR3
1135	EXCEPTION_LOAD_GPR2
1136
1137	l.rfe                              // SR <- ESR, PC <- EPC
1138
1139exit_with_no_itranslation:
1140	EXCEPTION_LOAD_GPR4
1141	EXCEPTION_LOAD_GPR6
1142	l.j    _dispatch_bus_fault
1143	l.nop
1144
1145/* ====================================================================== */
1146/*
1147 * Stuff below here shouldn't go into .head section... maybe this stuff
1148 * can be moved to entry.S ???
1149 */
1150
1151/* ==============================================[ DTLB miss handler ]=== */
1152
1153/*
1154 * Comments:
1155 *   Exception handlers are entered with MMU off so the following handler
1156 *   needs to use physical addressing
1157 *
1158 */
1159
1160	.text
1161ENTRY(dtlb_miss_handler)
1162	EXCEPTION_STORE_GPR2
1163	EXCEPTION_STORE_GPR3
1164	EXCEPTION_STORE_GPR4
 
 
1165	/*
1166	 * get EA of the miss
1167	 */
1168	l.mfspr	r2,r0,SPR_EEAR_BASE
1169	/*
1170	 * pmd = (pmd_t *)(current_pgd + pgd_index(daddr));
1171	 */
1172	GET_CURRENT_PGD(r3,r4)		// r3 is current_pgd, r4 is temp
1173	l.srli	r4,r2,0x18		// >> PAGE_SHIFT + (PAGE_SHIFT - 2)
1174	l.slli	r4,r4,0x2		// to get address << 2
1175	l.add	r3,r4,r3		// r4 is pgd_index(daddr)
1176	/*
1177	 * if (pmd_none(*pmd))
1178	 *   goto pmd_none:
1179	 */
1180	tophys	(r4,r3)
1181	l.lwz	r3,0x0(r4)		// get *pmd value
1182	l.sfne	r3,r0
1183	l.bnf	d_pmd_none
1184	 l.addi	r3,r0,0xffffe000	// PAGE_MASK
1185
 
 
 
 
 
 
 
 
 
1186d_pmd_good:
1187	/*
1188	 * pte = *pte_offset(pmd, daddr);
1189	 */
1190	l.lwz	r4,0x0(r4)		// get **pmd value
1191	l.and	r4,r4,r3		// & PAGE_MASK
1192	l.srli	r2,r2,0xd		// >> PAGE_SHIFT, r2 == EEAR
1193	l.andi	r3,r2,0x7ff		// (1UL << PAGE_SHIFT - 2) - 1
1194	l.slli	r3,r3,0x2		// to get address << 2
1195	l.add	r3,r3,r4
1196	l.lwz	r3,0x0(r3)		// this is pte at last
1197	/*
1198	 * if (!pte_present(pte))
1199	 */
1200	l.andi	r4,r3,0x1
1201	l.sfne	r4,r0			// is pte present
1202	l.bnf	d_pte_not_present
1203	l.addi	r4,r0,0xffffe3fa	// PAGE_MASK | DTLB_UP_CONVERT_MASK
1204	/*
1205	 * fill DTLB TR register
1206	 */
1207	l.and	r4,r3,r4		// apply the mask
1208	// Determine number of DMMU sets
1209	l.mfspr r2, r0, SPR_DMMUCFGR
1210	l.andi	r2, r2, SPR_DMMUCFGR_NTS
1211	l.srli	r2, r2, SPR_DMMUCFGR_NTS_OFF
1212	l.ori	r3, r0, 0x1
1213	l.sll	r3, r3, r2 	// r3 = number DMMU sets DMMUCFGR
1214	l.addi	r2, r3, -1  	// r2 = nsets mask
1215	l.mfspr	r3, r0, SPR_EEAR_BASE
1216	l.srli	r3, r3, 0xd	// >> PAGE_SHIFT
1217	l.and	r2, r3, r2	// calc offset:	 & (NUM_TLB_ENTRIES-1)
1218	                                                   //NUM_TLB_ENTRIES
1219	l.mtspr	r2,r4,SPR_DTLBTR_BASE(0)
1220	/*
1221	 * fill DTLB MR register
1222	 */
1223	l.slli	r3, r3, 0xd		/* << PAGE_SHIFT => EA & PAGE_MASK */
1224	l.ori	r4,r3,0x1		// set hardware valid bit: DTBL_MR entry
1225	l.mtspr	r2,r4,SPR_DTLBMR_BASE(0)
 
 
1226
1227	EXCEPTION_LOAD_GPR2
1228	EXCEPTION_LOAD_GPR3
1229	EXCEPTION_LOAD_GPR4
 
 
 
 
 
 
 
 
 
 
1230	l.rfe
1231d_pmd_none:
1232d_pte_not_present:
1233	EXCEPTION_LOAD_GPR2
1234	EXCEPTION_LOAD_GPR3
1235	EXCEPTION_LOAD_GPR4
 
 
1236	EXCEPTION_HANDLE(_dtlb_miss_page_fault_handler)
1237
1238/* ==============================================[ ITLB miss handler ]=== */
1239ENTRY(itlb_miss_handler)
1240	EXCEPTION_STORE_GPR2
1241	EXCEPTION_STORE_GPR3
1242	EXCEPTION_STORE_GPR4
 
 
1243	/*
1244	 * get EA of the miss
1245	 */
1246	l.mfspr	r2,r0,SPR_EEAR_BASE
1247
1248	/*
1249	 * pmd = (pmd_t *)(current_pgd + pgd_index(daddr));
1250	 *
1251	 */
1252	GET_CURRENT_PGD(r3,r4)		// r3 is current_pgd, r5 is temp
1253	l.srli	r4,r2,0x18		// >> PAGE_SHIFT + (PAGE_SHIFT - 2)
1254	l.slli	r4,r4,0x2		// to get address << 2
1255	l.add	r3,r4,r3		// r4 is pgd_index(daddr)
1256	/*
1257	 * if (pmd_none(*pmd))
1258	 *   goto pmd_none:
1259	 */
1260	tophys	(r4,r3)
1261	l.lwz	r3,0x0(r4)		// get *pmd value
1262	l.sfne	r3,r0
1263	l.bnf	i_pmd_none
1264	 l.addi	r3,r0,0xffffe000	// PAGE_MASK
 
 
 
 
 
 
 
 
 
 
 
1265
1266i_pmd_good:
1267	/*
1268	 * pte = *pte_offset(pmd, iaddr);
1269	 *
1270	 */
1271	l.lwz	r4,0x0(r4)		// get **pmd value
1272	l.and	r4,r4,r3		// & PAGE_MASK
1273	l.srli	r2,r2,0xd		// >> PAGE_SHIFT, r2 == EEAR
1274	l.andi	r3,r2,0x7ff		// (1UL << PAGE_SHIFT - 2) - 1
1275	l.slli	r3,r3,0x2		// to get address << 2
1276	l.add	r3,r3,r4
1277	l.lwz	r3,0x0(r3)		// this is pte at last
1278	/*
1279	 * if (!pte_present(pte))
1280	 *
1281	 */
1282	l.andi	r4,r3,0x1
1283	l.sfne	r4,r0			// is pte present
1284	l.bnf	i_pte_not_present
1285	 l.addi	r4,r0,0xffffe03a	// PAGE_MASK | ITLB_UP_CONVERT_MASK
1286	/*
1287	 * fill ITLB TR register
1288	 */
1289	l.and	r4,r3,r4		// apply the mask
1290	l.andi	r3,r3,0x7c0		// _PAGE_EXEC | _PAGE_SRE | _PAGE_SWE |  _PAGE_URE | _PAGE_UWE
 
1291	l.sfeq	r3,r0
1292	l.bf	itlb_tr_fill //_workaround
1293	// Determine number of IMMU sets
1294	l.mfspr r2, r0, SPR_IMMUCFGR
1295	l.andi	r2, r2, SPR_IMMUCFGR_NTS
1296	l.srli	r2, r2, SPR_IMMUCFGR_NTS_OFF
1297	l.ori	r3, r0, 0x1
1298	l.sll	r3, r3, r2 	// r3 = number IMMU sets IMMUCFGR
1299	l.addi	r2, r3, -1  	// r2 = nsets mask
1300	l.mfspr	r3, r0, SPR_EEAR_BASE
1301	l.srli	r3, r3, 0xd	// >> PAGE_SHIFT
1302	l.and	r2, r3, r2	// calc offset:	 & (NUM_TLB_ENTRIES-1)
1303
1304/*
1305 * __PHX__ :: fixme
1306 * we should not just blindly set executable flags,
1307 * but it does help with ping. the clean way would be to find out
1308 * (and fix it) why stack doesn't have execution permissions
1309 */
1310
1311itlb_tr_fill_workaround:
1312	l.ori	r4,r4,0xc0		// | (SPR_ITLBTR_UXE | ITLBTR_SXE)
1313itlb_tr_fill:
1314	l.mtspr	r2,r4,SPR_ITLBTR_BASE(0)
1315	/*
1316	 * fill DTLB MR register
1317	 */
1318	l.slli	r3, r3, 0xd		/* << PAGE_SHIFT => EA & PAGE_MASK */
1319	l.ori	r4,r3,0x1		// set hardware valid bit: ITBL_MR entry
1320	l.mtspr	r2,r4,SPR_ITLBMR_BASE(0)
 
 
1321
1322	EXCEPTION_LOAD_GPR2
1323	EXCEPTION_LOAD_GPR3
1324	EXCEPTION_LOAD_GPR4
 
 
1325	l.rfe
1326
 
 
 
 
 
 
 
 
1327i_pmd_none:
1328i_pte_not_present:
1329	EXCEPTION_LOAD_GPR2
1330	EXCEPTION_LOAD_GPR3
1331	EXCEPTION_LOAD_GPR4
 
 
1332	EXCEPTION_HANDLE(_itlb_miss_page_fault_handler)
1333
1334/* ==============================================[ boot tlb handlers ]=== */
1335
1336
1337/* =================================================[ debugging aids ]=== */
1338
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1339/*
1340 * DESC: Prints ASCII character stored in r7
1341 *
1342 * PRMS: r7	- a 32-bit value with an ASCII character in the first byte
1343 *		position.
1344 *
1345 * PREQ: The UART at UART_BASE_ADD has to be initialized
1346 *
1347 * POST: internally used but restores:
1348 * 	 r4	- to store UART_BASE_ADD
1349 *	 r5	- for loading OFF_TXFULL / THRE,TEMT
1350 *	 r6	- for storing bitmask (SERIAL_8250)
1351 */
1352ENTRY(_emergency_putc)
1353	EMERGENCY_PRINT_STORE_GPR4
1354	EMERGENCY_PRINT_STORE_GPR5
1355	EMERGENCY_PRINT_STORE_GPR6
 
 
 
 
 
 
1356
 
1357	l.movhi r4,hi(UART_BASE_ADD)
1358	l.ori	r4,r4,lo(UART_BASE_ADD)
1359
1360#if defined(CONFIG_SERIAL_LITEUART)
1361	/* Check OFF_TXFULL status */
13621:      l.lwz	r5,4(r4)
1363	l.andi	r5,r5,0xff
1364	l.sfnei	r5,0
1365	l.bf	1b
1366	 l.nop
1367
1368	/* Write character */
1369	l.andi	r7,r7,0xff
1370	l.sw	0(r4),r7
1371#elif defined(CONFIG_SERIAL_8250)
1372	/* Check UART LSR THRE (hold) bit */
1373	l.addi  r6,r0,0x20
13741:      l.lbz   r5,5(r4)
1375	l.andi  r5,r5,0x20
1376	l.sfeq  r5,r6
1377	l.bnf   1b
1378	 l.nop
1379
1380	/* Write character */
1381	l.sb    0(r4),r7
1382
1383	/* Check UART LSR THRE|TEMT (hold, empty) bits */
1384	l.addi  r6,r0,0x60
13851:      l.lbz   r5,5(r4)
1386	l.andi  r5,r5,0x60
1387	l.sfeq  r5,r6
1388	l.bnf   1b
1389	 l.nop
1390#endif
1391	EMERGENCY_PRINT_LOAD_GPR6
1392	EMERGENCY_PRINT_LOAD_GPR5
1393	EMERGENCY_PRINT_LOAD_GPR4
1394	l.jr	r9
1395	 l.nop
1396
1397/*
1398 * DSCR: prints a string referenced by r3.
1399 *
1400 * PRMS: r3     	- address of the first character of null
1401 *			terminated string to be printed
1402 *
1403 * PREQ: UART at UART_BASE_ADD has to be initialized
1404 *
1405 * POST: caller should be aware that r3, r9 are changed
1406 */
1407ENTRY(_emergency_print)
1408	EMERGENCY_PRINT_STORE_GPR7
1409	EMERGENCY_PRINT_STORE_GPR9
1410
1411	/* Load character to r7, check for null terminator */
14122:	l.lbz	r7,0(r3)
1413	l.sfeqi	r7,0x0
1414	l.bf	9f
1415	 l.nop
1416
1417	l.jal	_emergency_putc
1418	 l.nop
1419
1420	/* next character */
1421	l.j	2b
1422	 l.addi	r3,r3,0x1
1423
14249:
1425	EMERGENCY_PRINT_LOAD_GPR9
1426	EMERGENCY_PRINT_LOAD_GPR7
 
 
 
1427	l.jr	r9
1428	 l.nop
1429
1430/*
1431 * DSCR: prints a number in r3 in hex.
1432 *
1433 * PRMS: r3     	- a 32-bit unsigned integer
1434 *
1435 * PREQ: UART at UART_BASE_ADD has to be initialized
1436 *
1437 * POST: caller should be aware that r3, r9 are changed
1438 */
1439ENTRY(_emergency_print_nr)
 
 
 
1440	EMERGENCY_PRINT_STORE_GPR7
1441	EMERGENCY_PRINT_STORE_GPR8
1442	EMERGENCY_PRINT_STORE_GPR9
1443
1444	l.addi	r8,r0,32		// shift register
1445
14461:	/* remove leading zeros */
1447	l.addi	r8,r8,-0x4
1448	l.srl	r7,r3,r8
1449	l.andi	r7,r7,0xf
1450
1451	/* don't skip the last zero if number == 0x0 */
1452	l.sfeqi	r8,0x4
1453	l.bf	2f
1454	 l.nop
1455
1456	l.sfeq	r7,r0
1457	l.bf	1b
1458	 l.nop
1459
14602:
1461	l.srl	r7,r3,r8
1462
1463	l.andi	r7,r7,0xf
1464	l.sflts	r8,r0
1465	 l.bf	9f
1466
1467	/* Numbers greater than 9 translate to a-f */
1468	l.sfgtui r7,0x9
1469	l.bnf	8f
1470	 l.nop
1471	l.addi	r7,r7,0x27
1472
1473	/* Convert to ascii and output character */
14748:	l.jal	_emergency_putc
1475	 l.addi	r7,r7,0x30
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1476
1477	/* next character */
1478	l.j	2b
1479	l.addi	r8,r8,-0x4
1480
14819:
1482	EMERGENCY_PRINT_LOAD_GPR9
1483	EMERGENCY_PRINT_LOAD_GPR8
1484	EMERGENCY_PRINT_LOAD_GPR7
 
 
 
1485	l.jr	r9
1486	 l.nop
 
1487
1488/*
1489 * This should be used for debugging only.
1490 * It messes up the Linux early serial output
1491 * somehow, so use it sparingly and essentially
1492 * only if you need to debug something that goes wrong
1493 * before Linux gets the early serial going.
1494 *
1495 * Furthermore, you'll have to make sure you set the
1496 * UART_DEVISOR correctly according to the system
1497 * clock rate.
1498 *
1499 *
1500 */
1501
1502
1503
1504#define SYS_CLK            20000000
1505//#define SYS_CLK            1843200
1506#define OR32_CONSOLE_BAUD  115200
1507#define UART_DIVISOR       SYS_CLK/(16*OR32_CONSOLE_BAUD)
1508
1509ENTRY(_early_uart_init)
1510	l.movhi	r3,hi(UART_BASE_ADD)
1511	l.ori	r3,r3,lo(UART_BASE_ADD)
1512
1513#if defined(CONFIG_SERIAL_8250)
1514	l.addi	r4,r0,0x7
1515	l.sb	0x2(r3),r4
1516
1517	l.addi	r4,r0,0x0
1518	l.sb	0x1(r3),r4
1519
1520	l.addi	r4,r0,0x3
1521	l.sb	0x3(r3),r4
1522
1523	l.lbz	r5,3(r3)
1524	l.ori	r4,r5,0x80
1525	l.sb	0x3(r3),r4
1526	l.addi	r4,r0,((UART_DIVISOR>>8) & 0x000000ff)
1527	l.sb	UART_DLM(r3),r4
1528	l.addi  r4,r0,((UART_DIVISOR) & 0x000000ff)
1529	l.sb	UART_DLL(r3),r4
1530	l.sb	0x3(r3),r5
1531#endif
1532
1533	l.jr	r9
1534	 l.nop
1535
1536	.align	0x1000
1537	.global _secondary_evbar
1538_secondary_evbar:
1539
1540	.space 0x800
1541	/* Just disable interrupts and Return */
1542	l.ori	r3,r0,SPR_SR_SM
1543	l.mtspr	r0,r3,SPR_ESR_BASE
1544	l.rfe
1545
 
 
1546
1547	.section .rodata
1548_string_unhandled_exception:
1549	.string "\r\nRunarunaround: Unhandled exception 0x\0"
1550
1551_string_epc_prefix:
1552	.string ": EPC=0x\0"
1553
1554_string_nl:
1555	.string "\r\n\0"
 
 
 
 
 
1556
1557
1558/* ========================================[ page aligned structures ]=== */
1559
1560/*
1561 * .data section should be page aligned
1562 *	(look into arch/openrisc/kernel/vmlinux.lds.S)
1563 */
1564	.section .data,"aw"
1565	.align	8192
1566	.global  empty_zero_page
1567empty_zero_page:
1568	.space  8192
1569
1570	.global  swapper_pg_dir
1571swapper_pg_dir:
1572	.space  8192
1573
1574	.global	_unhandled_stack
1575_unhandled_stack:
1576	.space	8192
1577_unhandled_stack_top:
1578
1579/* ============================================================[ EOF ]=== */