18c2ecf20Sopenharmony_ci/* SPDX-License-Identifier: GPL-2.0-only */
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci * TLB Exception Handling for ARC
48c2ecf20Sopenharmony_ci *
58c2ecf20Sopenharmony_ci * Copyright (C) 2004, 2007-2010, 2011-2012 Synopsys, Inc. (www.synopsys.com)
68c2ecf20Sopenharmony_ci *
78c2ecf20Sopenharmony_ci * Vineetg: April 2011 :
88c2ecf20Sopenharmony_ci *  -MMU v1: moved out legacy code into a seperate file
98c2ecf20Sopenharmony_ci *  -MMU v3: PD{0,1} bits layout changed: They don't overlap anymore,
108c2ecf20Sopenharmony_ci *      helps avoid a shift when preparing PD0 from PTE
118c2ecf20Sopenharmony_ci *
128c2ecf20Sopenharmony_ci * Vineetg: July 2009
138c2ecf20Sopenharmony_ci *  -For MMU V2, we need not do heuristics at the time of commiting a D-TLB
148c2ecf20Sopenharmony_ci *   entry, so that it doesn't knock out it's I-TLB entry
158c2ecf20Sopenharmony_ci *  -Some more fine tuning:
168c2ecf20Sopenharmony_ci *   bmsk instead of add, asl.cc instead of branch, delay slot utilise etc
178c2ecf20Sopenharmony_ci *
188c2ecf20Sopenharmony_ci * Vineetg: July 2009
198c2ecf20Sopenharmony_ci *  -Practically rewrote the I/D TLB Miss handlers
208c2ecf20Sopenharmony_ci *   Now 40 and 135 instructions a peice as compared to 131 and 449 resp.
218c2ecf20Sopenharmony_ci *   Hence Leaner by 1.5 K
228c2ecf20Sopenharmony_ci *   Used Conditional arithmetic to replace excessive branching
238c2ecf20Sopenharmony_ci *   Also used short instructions wherever possible
248c2ecf20Sopenharmony_ci *
258c2ecf20Sopenharmony_ci * Vineetg: Aug 13th 2008
268c2ecf20Sopenharmony_ci *  -Passing ECR (Exception Cause REG) to do_page_fault( ) for printing
278c2ecf20Sopenharmony_ci *   more information in case of a Fatality
288c2ecf20Sopenharmony_ci *
298c2ecf20Sopenharmony_ci * Vineetg: March 25th Bug #92690
308c2ecf20Sopenharmony_ci *  -Added Debug Code to check if sw-ASID == hw-ASID
318c2ecf20Sopenharmony_ci
328c2ecf20Sopenharmony_ci * Rahul Trivedi, Amit Bhor: Codito Technologies 2004
338c2ecf20Sopenharmony_ci */
348c2ecf20Sopenharmony_ci
358c2ecf20Sopenharmony_ci#include <linux/linkage.h>
368c2ecf20Sopenharmony_ci#include <linux/pgtable.h>
378c2ecf20Sopenharmony_ci#include <asm/entry.h>
388c2ecf20Sopenharmony_ci#include <asm/mmu.h>
398c2ecf20Sopenharmony_ci#include <asm/arcregs.h>
408c2ecf20Sopenharmony_ci#include <asm/cache.h>
418c2ecf20Sopenharmony_ci#include <asm/processor.h>
428c2ecf20Sopenharmony_ci#include <asm/tlb-mmu1.h>
438c2ecf20Sopenharmony_ci
448c2ecf20Sopenharmony_ci#ifdef CONFIG_ISA_ARCOMPACT
458c2ecf20Sopenharmony_ci;-----------------------------------------------------------------
468c2ecf20Sopenharmony_ci; ARC700 Exception Handling doesn't auto-switch stack and it only provides
478c2ecf20Sopenharmony_ci; ONE scratch AUX reg "ARC_REG_SCRATCH_DATA0"
488c2ecf20Sopenharmony_ci;
498c2ecf20Sopenharmony_ci; For Non-SMP, the scratch AUX reg is repurposed to cache task PGD, so a
508c2ecf20Sopenharmony_ci; "global" is used to free-up FIRST core reg to be able to code the rest of
518c2ecf20Sopenharmony_ci; exception prologue (IRQ auto-disabled on Exceptions, so it's IRQ-safe).
528c2ecf20Sopenharmony_ci; Since the Fast Path TLB Miss handler is coded with 4 regs, the remaining 3
538c2ecf20Sopenharmony_ci; need to be saved as well by extending the "global" to be 4 words. Hence
548c2ecf20Sopenharmony_ci;	".size   ex_saved_reg1, 16"
558c2ecf20Sopenharmony_ci; [All of this dance is to avoid stack switching for each TLB Miss, since we
568c2ecf20Sopenharmony_ci; only need to save only a handful of regs, as opposed to complete reg file]
578c2ecf20Sopenharmony_ci;
588c2ecf20Sopenharmony_ci; For ARC700 SMP, the "global" obviously can't be used for free up the FIRST
598c2ecf20Sopenharmony_ci; core reg as it will not be SMP safe.
608c2ecf20Sopenharmony_ci; Thus scratch AUX reg is used (and no longer used to cache task PGD).
618c2ecf20Sopenharmony_ci; To save the rest of 3 regs - per cpu, the global is made "per-cpu".
628c2ecf20Sopenharmony_ci; Epilogue thus has to locate the "per-cpu" storage for regs.
638c2ecf20Sopenharmony_ci; To avoid cache line bouncing the per-cpu global is aligned/sized per
648c2ecf20Sopenharmony_ci; L1_CACHE_SHIFT, despite fundamentally needing to be 12 bytes only. Hence
658c2ecf20Sopenharmony_ci;	".size   ex_saved_reg1, (CONFIG_NR_CPUS << L1_CACHE_SHIFT)"
668c2ecf20Sopenharmony_ci
678c2ecf20Sopenharmony_ci; As simple as that....
688c2ecf20Sopenharmony_ci;--------------------------------------------------------------------------
698c2ecf20Sopenharmony_ci
708c2ecf20Sopenharmony_ci; scratch memory to save [r0-r3] used to code TLB refill Handler
718c2ecf20Sopenharmony_ciARCFP_DATA ex_saved_reg1
728c2ecf20Sopenharmony_ci	.align 1 << L1_CACHE_SHIFT
738c2ecf20Sopenharmony_ci	.type   ex_saved_reg1, @object
748c2ecf20Sopenharmony_ci#ifdef CONFIG_SMP
758c2ecf20Sopenharmony_ci	.size   ex_saved_reg1, (CONFIG_NR_CPUS << L1_CACHE_SHIFT)
768c2ecf20Sopenharmony_ciex_saved_reg1:
778c2ecf20Sopenharmony_ci	.zero (CONFIG_NR_CPUS << L1_CACHE_SHIFT)
788c2ecf20Sopenharmony_ci#else
798c2ecf20Sopenharmony_ci	.size   ex_saved_reg1, 16
808c2ecf20Sopenharmony_ciex_saved_reg1:
818c2ecf20Sopenharmony_ci	.zero 16
828c2ecf20Sopenharmony_ci#endif
838c2ecf20Sopenharmony_ci
848c2ecf20Sopenharmony_ci.macro TLBMISS_FREEUP_REGS
858c2ecf20Sopenharmony_ci#ifdef CONFIG_SMP
868c2ecf20Sopenharmony_ci	sr  r0, [ARC_REG_SCRATCH_DATA0]	; freeup r0 to code with
878c2ecf20Sopenharmony_ci	GET_CPU_ID  r0			; get to per cpu scratch mem,
888c2ecf20Sopenharmony_ci	asl r0, r0, L1_CACHE_SHIFT	; cache line wide per cpu
898c2ecf20Sopenharmony_ci	add r0, @ex_saved_reg1, r0
908c2ecf20Sopenharmony_ci#else
918c2ecf20Sopenharmony_ci	st    r0, [@ex_saved_reg1]
928c2ecf20Sopenharmony_ci	mov_s r0, @ex_saved_reg1
938c2ecf20Sopenharmony_ci#endif
948c2ecf20Sopenharmony_ci	st_s  r1, [r0, 4]
958c2ecf20Sopenharmony_ci	st_s  r2, [r0, 8]
968c2ecf20Sopenharmony_ci	st_s  r3, [r0, 12]
978c2ecf20Sopenharmony_ci
988c2ecf20Sopenharmony_ci	; VERIFY if the ASID in MMU-PID Reg is same as
998c2ecf20Sopenharmony_ci	; one in Linux data structures
1008c2ecf20Sopenharmony_ci
1018c2ecf20Sopenharmony_ci	tlb_paranoid_check_asm
1028c2ecf20Sopenharmony_ci.endm
1038c2ecf20Sopenharmony_ci
1048c2ecf20Sopenharmony_ci.macro TLBMISS_RESTORE_REGS
1058c2ecf20Sopenharmony_ci#ifdef CONFIG_SMP
1068c2ecf20Sopenharmony_ci	GET_CPU_ID  r0			; get to per cpu scratch mem
1078c2ecf20Sopenharmony_ci	asl r0, r0, L1_CACHE_SHIFT	; each is cache line wide
1088c2ecf20Sopenharmony_ci	add r0, @ex_saved_reg1, r0
1098c2ecf20Sopenharmony_ci	ld_s  r3, [r0,12]
1108c2ecf20Sopenharmony_ci	ld_s  r2, [r0, 8]
1118c2ecf20Sopenharmony_ci	ld_s  r1, [r0, 4]
1128c2ecf20Sopenharmony_ci	lr    r0, [ARC_REG_SCRATCH_DATA0]
1138c2ecf20Sopenharmony_ci#else
1148c2ecf20Sopenharmony_ci	mov_s r0, @ex_saved_reg1
1158c2ecf20Sopenharmony_ci	ld_s  r3, [r0,12]
1168c2ecf20Sopenharmony_ci	ld_s  r2, [r0, 8]
1178c2ecf20Sopenharmony_ci	ld_s  r1, [r0, 4]
1188c2ecf20Sopenharmony_ci	ld_s  r0, [r0]
1198c2ecf20Sopenharmony_ci#endif
1208c2ecf20Sopenharmony_ci.endm
1218c2ecf20Sopenharmony_ci
1228c2ecf20Sopenharmony_ci#else	/* ARCv2 */
1238c2ecf20Sopenharmony_ci
1248c2ecf20Sopenharmony_ci.macro TLBMISS_FREEUP_REGS
1258c2ecf20Sopenharmony_ci#ifdef CONFIG_ARC_HAS_LL64
1268c2ecf20Sopenharmony_ci	std   r0, [sp, -16]
1278c2ecf20Sopenharmony_ci	std   r2, [sp, -8]
1288c2ecf20Sopenharmony_ci#else
1298c2ecf20Sopenharmony_ci	PUSH  r0
1308c2ecf20Sopenharmony_ci	PUSH  r1
1318c2ecf20Sopenharmony_ci	PUSH  r2
1328c2ecf20Sopenharmony_ci	PUSH  r3
1338c2ecf20Sopenharmony_ci#endif
1348c2ecf20Sopenharmony_ci.endm
1358c2ecf20Sopenharmony_ci
1368c2ecf20Sopenharmony_ci.macro TLBMISS_RESTORE_REGS
1378c2ecf20Sopenharmony_ci#ifdef CONFIG_ARC_HAS_LL64
1388c2ecf20Sopenharmony_ci	ldd   r0, [sp, -16]
1398c2ecf20Sopenharmony_ci	ldd   r2, [sp, -8]
1408c2ecf20Sopenharmony_ci#else
1418c2ecf20Sopenharmony_ci	POP   r3
1428c2ecf20Sopenharmony_ci	POP   r2
1438c2ecf20Sopenharmony_ci	POP   r1
1448c2ecf20Sopenharmony_ci	POP   r0
1458c2ecf20Sopenharmony_ci#endif
1468c2ecf20Sopenharmony_ci.endm
1478c2ecf20Sopenharmony_ci
1488c2ecf20Sopenharmony_ci#endif
1498c2ecf20Sopenharmony_ci
1508c2ecf20Sopenharmony_ci;============================================================================
1518c2ecf20Sopenharmony_ci;  Troubleshooting Stuff
1528c2ecf20Sopenharmony_ci;============================================================================
1538c2ecf20Sopenharmony_ci
1548c2ecf20Sopenharmony_ci; Linux keeps ASID (Address Space ID) in task->active_mm->context.asid
1558c2ecf20Sopenharmony_ci; When Creating TLB Entries, instead of doing 3 dependent loads from memory,
1568c2ecf20Sopenharmony_ci; we use the MMU PID Reg to get current ASID.
1578c2ecf20Sopenharmony_ci; In bizzare scenrios SW and HW ASID can get out-of-sync which is trouble.
1588c2ecf20Sopenharmony_ci; So we try to detect this in TLB Mis shandler
1598c2ecf20Sopenharmony_ci
1608c2ecf20Sopenharmony_ci.macro tlb_paranoid_check_asm
1618c2ecf20Sopenharmony_ci
1628c2ecf20Sopenharmony_ci#ifdef CONFIG_ARC_DBG_TLB_PARANOIA
1638c2ecf20Sopenharmony_ci
1648c2ecf20Sopenharmony_ci	GET_CURR_TASK_ON_CPU  r3
1658c2ecf20Sopenharmony_ci	ld r0, [r3, TASK_ACT_MM]
1668c2ecf20Sopenharmony_ci	ld r0, [r0, MM_CTXT+MM_CTXT_ASID]
1678c2ecf20Sopenharmony_ci	breq r0, 0, 55f	; Error if no ASID allocated
1688c2ecf20Sopenharmony_ci
1698c2ecf20Sopenharmony_ci	lr r1, [ARC_REG_PID]
1708c2ecf20Sopenharmony_ci	and r1, r1, 0xFF
1718c2ecf20Sopenharmony_ci
1728c2ecf20Sopenharmony_ci	and r2, r0, 0xFF	; MMU PID bits only for comparison
1738c2ecf20Sopenharmony_ci	breq r1, r2, 5f
1748c2ecf20Sopenharmony_ci
1758c2ecf20Sopenharmony_ci55:
1768c2ecf20Sopenharmony_ci	; Error if H/w and S/w ASID don't match, but NOT if in kernel mode
1778c2ecf20Sopenharmony_ci	lr  r2, [erstatus]
1788c2ecf20Sopenharmony_ci	bbit0 r2, STATUS_U_BIT, 5f
1798c2ecf20Sopenharmony_ci
1808c2ecf20Sopenharmony_ci	; We sure are in troubled waters, Flag the error, but to do so
1818c2ecf20Sopenharmony_ci	; need to switch to kernel mode stack to call error routine
1828c2ecf20Sopenharmony_ci	GET_TSK_STACK_BASE   r3, sp
1838c2ecf20Sopenharmony_ci
1848c2ecf20Sopenharmony_ci	; Call printk to shoutout aloud
1858c2ecf20Sopenharmony_ci	mov r2, 1
1868c2ecf20Sopenharmony_ci	j print_asid_mismatch
1878c2ecf20Sopenharmony_ci
1888c2ecf20Sopenharmony_ci5:	; ASIDs match so proceed normally
1898c2ecf20Sopenharmony_ci	nop
1908c2ecf20Sopenharmony_ci
1918c2ecf20Sopenharmony_ci#endif
1928c2ecf20Sopenharmony_ci
1938c2ecf20Sopenharmony_ci.endm
1948c2ecf20Sopenharmony_ci
1958c2ecf20Sopenharmony_ci;============================================================================
1968c2ecf20Sopenharmony_ci;TLB Miss handling Code
1978c2ecf20Sopenharmony_ci;============================================================================
1988c2ecf20Sopenharmony_ci
1998c2ecf20Sopenharmony_ci;-----------------------------------------------------------------------------
2008c2ecf20Sopenharmony_ci; This macro does the page-table lookup for the faulting address.
2018c2ecf20Sopenharmony_ci; OUT: r0 = PTE faulted on, r1 = ptr to PTE, r2 = Faulting V-address
2028c2ecf20Sopenharmony_ci.macro LOAD_FAULT_PTE
2038c2ecf20Sopenharmony_ci
2048c2ecf20Sopenharmony_ci	lr  r2, [efa]
2058c2ecf20Sopenharmony_ci
2068c2ecf20Sopenharmony_ci#ifdef ARC_USE_SCRATCH_REG
2078c2ecf20Sopenharmony_ci	lr  r1, [ARC_REG_SCRATCH_DATA0] ; current pgd
2088c2ecf20Sopenharmony_ci#else
2098c2ecf20Sopenharmony_ci	GET_CURR_TASK_ON_CPU  r1
2108c2ecf20Sopenharmony_ci	ld  r1, [r1, TASK_ACT_MM]
2118c2ecf20Sopenharmony_ci	ld  r1, [r1, MM_PGD]
2128c2ecf20Sopenharmony_ci#endif
2138c2ecf20Sopenharmony_ci
2148c2ecf20Sopenharmony_ci	lsr     r0, r2, PGDIR_SHIFT     ; Bits for indexing into PGD
2158c2ecf20Sopenharmony_ci	ld.as   r3, [r1, r0]            ; PGD entry corresp to faulting addr
2168c2ecf20Sopenharmony_ci	tst	r3, r3
2178c2ecf20Sopenharmony_ci	bz	do_slow_path_pf         ; if no Page Table, do page fault
2188c2ecf20Sopenharmony_ci
2198c2ecf20Sopenharmony_ci#ifdef CONFIG_TRANSPARENT_HUGEPAGE
2208c2ecf20Sopenharmony_ci	and.f	0, r3, _PAGE_HW_SZ	; Is this Huge PMD (thp)
2218c2ecf20Sopenharmony_ci	add2.nz	r1, r1, r0
2228c2ecf20Sopenharmony_ci	bnz.d	2f		; YES: PGD == PMD has THP PTE: stop pgd walk
2238c2ecf20Sopenharmony_ci	mov.nz	r0, r3
2248c2ecf20Sopenharmony_ci
2258c2ecf20Sopenharmony_ci#endif
2268c2ecf20Sopenharmony_ci	and	r1, r3, PAGE_MASK
2278c2ecf20Sopenharmony_ci
2288c2ecf20Sopenharmony_ci	; Get the PTE entry: The idea is
2298c2ecf20Sopenharmony_ci	; (1) x = addr >> PAGE_SHIFT 	-> masks page-off bits from @fault-addr
2308c2ecf20Sopenharmony_ci	; (2) y = x & (PTRS_PER_PTE - 1) -> to get index
2318c2ecf20Sopenharmony_ci	; (3) z = (pgtbl + y * 4)
2328c2ecf20Sopenharmony_ci
2338c2ecf20Sopenharmony_ci#ifdef CONFIG_ARC_HAS_PAE40
2348c2ecf20Sopenharmony_ci#define PTE_SIZE_LOG	3	/* 8 == 2 ^ 3 */
2358c2ecf20Sopenharmony_ci#else
2368c2ecf20Sopenharmony_ci#define PTE_SIZE_LOG	2	/* 4 == 2 ^ 2 */
2378c2ecf20Sopenharmony_ci#endif
2388c2ecf20Sopenharmony_ci
2398c2ecf20Sopenharmony_ci	; multiply in step (3) above avoided by shifting lesser in step (1)
2408c2ecf20Sopenharmony_ci	lsr     r0, r2, ( PAGE_SHIFT - PTE_SIZE_LOG )
2418c2ecf20Sopenharmony_ci	and     r0, r0, ( (PTRS_PER_PTE - 1) << PTE_SIZE_LOG )
2428c2ecf20Sopenharmony_ci	ld.aw   r0, [r1, r0]            ; r0: PTE (lower word only for PAE40)
2438c2ecf20Sopenharmony_ci					; r1: PTE ptr
2448c2ecf20Sopenharmony_ci
2458c2ecf20Sopenharmony_ci2:
2468c2ecf20Sopenharmony_ci
2478c2ecf20Sopenharmony_ci.endm
2488c2ecf20Sopenharmony_ci
2498c2ecf20Sopenharmony_ci;-----------------------------------------------------------------
2508c2ecf20Sopenharmony_ci; Convert Linux PTE entry into TLB entry
2518c2ecf20Sopenharmony_ci; A one-word PTE entry is programmed as two-word TLB Entry [PD0:PD1] in mmu
2528c2ecf20Sopenharmony_ci;    (for PAE40, two-words PTE, while three-word TLB Entry [PD0:PD1:PD1HI])
2538c2ecf20Sopenharmony_ci; IN: r0 = PTE, r1 = ptr to PTE
2548c2ecf20Sopenharmony_ci
2558c2ecf20Sopenharmony_ci.macro CONV_PTE_TO_TLB
2568c2ecf20Sopenharmony_ci	and    r3, r0, PTE_BITS_RWX	;          r  w  x
2578c2ecf20Sopenharmony_ci	asl    r2, r3, 3		; Kr Kw Kx 0  0  0 (GLOBAL, kernel only)
2588c2ecf20Sopenharmony_ci	and.f  0,  r0, _PAGE_GLOBAL
2598c2ecf20Sopenharmony_ci	or.z   r2, r2, r3		; Kr Kw Kx Ur Uw Ux (!GLOBAL, user page)
2608c2ecf20Sopenharmony_ci
2618c2ecf20Sopenharmony_ci	and r3, r0, PTE_BITS_NON_RWX_IN_PD1 ; Extract PFN+cache bits from PTE
2628c2ecf20Sopenharmony_ci	or  r3, r3, r2
2638c2ecf20Sopenharmony_ci
2648c2ecf20Sopenharmony_ci	sr  r3, [ARC_REG_TLBPD1]    	; paddr[31..13] | Kr Kw Kx Ur Uw Ux | C
2658c2ecf20Sopenharmony_ci#ifdef	CONFIG_ARC_HAS_PAE40
2668c2ecf20Sopenharmony_ci	ld	r3, [r1, 4]		; paddr[39..32]
2678c2ecf20Sopenharmony_ci	sr	r3, [ARC_REG_TLBPD1HI]
2688c2ecf20Sopenharmony_ci#endif
2698c2ecf20Sopenharmony_ci
2708c2ecf20Sopenharmony_ci	and r2, r0, PTE_BITS_IN_PD0 ; Extract other PTE flags: (V)alid, (G)lb
2718c2ecf20Sopenharmony_ci
2728c2ecf20Sopenharmony_ci	lr  r3,[ARC_REG_TLBPD0]     ; MMU prepares PD0 with vaddr and asid
2738c2ecf20Sopenharmony_ci
2748c2ecf20Sopenharmony_ci	or  r3, r3, r2              ; S | vaddr | {sasid|asid}
2758c2ecf20Sopenharmony_ci	sr  r3,[ARC_REG_TLBPD0]     ; rewrite PD0
2768c2ecf20Sopenharmony_ci.endm
2778c2ecf20Sopenharmony_ci
2788c2ecf20Sopenharmony_ci;-----------------------------------------------------------------
2798c2ecf20Sopenharmony_ci; Commit the TLB entry into MMU
2808c2ecf20Sopenharmony_ci
2818c2ecf20Sopenharmony_ci.macro COMMIT_ENTRY_TO_MMU
2828c2ecf20Sopenharmony_ci#if (CONFIG_ARC_MMU_VER < 4)
2838c2ecf20Sopenharmony_ci
2848c2ecf20Sopenharmony_ci	/* Get free TLB slot: Set = computed from vaddr, way = random */
2858c2ecf20Sopenharmony_ci	sr  TLBGetIndex, [ARC_REG_TLBCOMMAND]
2868c2ecf20Sopenharmony_ci
2878c2ecf20Sopenharmony_ci	/* Commit the Write */
2888c2ecf20Sopenharmony_ci	sr TLBWriteNI, [ARC_REG_TLBCOMMAND]
2898c2ecf20Sopenharmony_ci
2908c2ecf20Sopenharmony_ci#else
2918c2ecf20Sopenharmony_ci	sr TLBInsertEntry, [ARC_REG_TLBCOMMAND]
2928c2ecf20Sopenharmony_ci#endif
2938c2ecf20Sopenharmony_ci
2948c2ecf20Sopenharmony_ci88:
2958c2ecf20Sopenharmony_ci.endm
2968c2ecf20Sopenharmony_ci
2978c2ecf20Sopenharmony_ci
2988c2ecf20Sopenharmony_ciARCFP_CODE	;Fast Path Code, candidate for ICCM
2998c2ecf20Sopenharmony_ci
3008c2ecf20Sopenharmony_ci;-----------------------------------------------------------------------------
3018c2ecf20Sopenharmony_ci; I-TLB Miss Exception Handler
3028c2ecf20Sopenharmony_ci;-----------------------------------------------------------------------------
3038c2ecf20Sopenharmony_ci
3048c2ecf20Sopenharmony_ciENTRY(EV_TLBMissI)
3058c2ecf20Sopenharmony_ci
3068c2ecf20Sopenharmony_ci	TLBMISS_FREEUP_REGS
3078c2ecf20Sopenharmony_ci
3088c2ecf20Sopenharmony_ci	;----------------------------------------------------------------
3098c2ecf20Sopenharmony_ci	; Get the PTE corresponding to V-addr accessed, r2 is setup with EFA
3108c2ecf20Sopenharmony_ci	LOAD_FAULT_PTE
3118c2ecf20Sopenharmony_ci
3128c2ecf20Sopenharmony_ci	;----------------------------------------------------------------
3138c2ecf20Sopenharmony_ci	; VERIFY_PTE: Check if PTE permissions approp for executing code
3148c2ecf20Sopenharmony_ci	cmp_s   r2, VMALLOC_START
3158c2ecf20Sopenharmony_ci	mov_s   r2, (_PAGE_PRESENT | _PAGE_EXECUTE)
3168c2ecf20Sopenharmony_ci	or.hs   r2, r2, _PAGE_GLOBAL
3178c2ecf20Sopenharmony_ci
3188c2ecf20Sopenharmony_ci	and     r3, r0, r2  ; Mask out NON Flag bits from PTE
3198c2ecf20Sopenharmony_ci	xor.f   r3, r3, r2  ; check ( ( pte & flags_test ) == flags_test )
3208c2ecf20Sopenharmony_ci	bnz     do_slow_path_pf
3218c2ecf20Sopenharmony_ci
3228c2ecf20Sopenharmony_ci	; Let Linux VM know that the page was accessed
3238c2ecf20Sopenharmony_ci	or      r0, r0, _PAGE_ACCESSED  ; set Accessed Bit
3248c2ecf20Sopenharmony_ci	st_s    r0, [r1]                ; Write back PTE
3258c2ecf20Sopenharmony_ci
3268c2ecf20Sopenharmony_ci	CONV_PTE_TO_TLB
3278c2ecf20Sopenharmony_ci	COMMIT_ENTRY_TO_MMU
3288c2ecf20Sopenharmony_ci	TLBMISS_RESTORE_REGS
3298c2ecf20Sopenharmony_ciEV_TLBMissI_fast_ret:	; additional label for VDK OS-kit instrumentation
3308c2ecf20Sopenharmony_ci	rtie
3318c2ecf20Sopenharmony_ci
3328c2ecf20Sopenharmony_ciEND(EV_TLBMissI)
3338c2ecf20Sopenharmony_ci
3348c2ecf20Sopenharmony_ci;-----------------------------------------------------------------------------
3358c2ecf20Sopenharmony_ci; D-TLB Miss Exception Handler
3368c2ecf20Sopenharmony_ci;-----------------------------------------------------------------------------
3378c2ecf20Sopenharmony_ci
3388c2ecf20Sopenharmony_ciENTRY(EV_TLBMissD)
3398c2ecf20Sopenharmony_ci
3408c2ecf20Sopenharmony_ci	TLBMISS_FREEUP_REGS
3418c2ecf20Sopenharmony_ci
3428c2ecf20Sopenharmony_ci	;----------------------------------------------------------------
3438c2ecf20Sopenharmony_ci	; Get the PTE corresponding to V-addr accessed
3448c2ecf20Sopenharmony_ci	; If PTE exists, it will setup, r0 = PTE, r1 = Ptr to PTE, r2 = EFA
3458c2ecf20Sopenharmony_ci	LOAD_FAULT_PTE
3468c2ecf20Sopenharmony_ci
3478c2ecf20Sopenharmony_ci	;----------------------------------------------------------------
3488c2ecf20Sopenharmony_ci	; VERIFY_PTE: Chk if PTE permissions approp for data access (R/W/R+W)
3498c2ecf20Sopenharmony_ci
3508c2ecf20Sopenharmony_ci	cmp_s	r2, VMALLOC_START
3518c2ecf20Sopenharmony_ci	mov_s   r2, _PAGE_PRESENT	; common bit for K/U PTE
3528c2ecf20Sopenharmony_ci	or.hs	r2, r2, _PAGE_GLOBAL	; kernel PTE only
3538c2ecf20Sopenharmony_ci
3548c2ecf20Sopenharmony_ci	; Linux PTE [RWX] bits are semantically overloaded:
3558c2ecf20Sopenharmony_ci	; -If PAGE_GLOBAL set, they refer to kernel-only flags (vmalloc)
3568c2ecf20Sopenharmony_ci	; -Otherwise they are user-mode permissions, and those are exactly
3578c2ecf20Sopenharmony_ci	;  same for kernel mode as well (e.g. copy_(to|from)_user)
3588c2ecf20Sopenharmony_ci
3598c2ecf20Sopenharmony_ci	lr      r3, [ecr]
3608c2ecf20Sopenharmony_ci	btst_s  r3, ECR_C_BIT_DTLB_LD_MISS	; Read Access
3618c2ecf20Sopenharmony_ci	or.nz   r2, r2, _PAGE_READ      	; chk for Read flag in PTE
3628c2ecf20Sopenharmony_ci	btst_s  r3, ECR_C_BIT_DTLB_ST_MISS	; Write Access
3638c2ecf20Sopenharmony_ci	or.nz   r2, r2, _PAGE_WRITE     	; chk for Write flag in PTE
3648c2ecf20Sopenharmony_ci	; Above laddering takes care of XCHG access (both R and W)
3658c2ecf20Sopenharmony_ci
3668c2ecf20Sopenharmony_ci	; By now, r2 setup with all the Flags we need to check in PTE
3678c2ecf20Sopenharmony_ci	and     r3, r0, r2              ; Mask out NON Flag bits from PTE
3688c2ecf20Sopenharmony_ci	brne.d  r3, r2, do_slow_path_pf ; is ((pte & flags_test) == flags_test)
3698c2ecf20Sopenharmony_ci
3708c2ecf20Sopenharmony_ci	;----------------------------------------------------------------
3718c2ecf20Sopenharmony_ci	; UPDATE_PTE: Let Linux VM know that page was accessed/dirty
3728c2ecf20Sopenharmony_ci	or      r0, r0, _PAGE_ACCESSED        ; Accessed bit always
3738c2ecf20Sopenharmony_ci	or.nz   r0, r0, _PAGE_DIRTY           ; if Write, set Dirty bit as well
3748c2ecf20Sopenharmony_ci	st_s    r0, [r1]                      ; Write back PTE
3758c2ecf20Sopenharmony_ci
3768c2ecf20Sopenharmony_ci	CONV_PTE_TO_TLB
3778c2ecf20Sopenharmony_ci
3788c2ecf20Sopenharmony_ci#if (CONFIG_ARC_MMU_VER == 1)
3798c2ecf20Sopenharmony_ci	; MMU with 2 way set assoc J-TLB, needs some help in pathetic case of
3808c2ecf20Sopenharmony_ci	; memcpy where 3 parties contend for 2 ways, ensuing a livelock.
3818c2ecf20Sopenharmony_ci	; But only for old MMU or one with Metal Fix
3828c2ecf20Sopenharmony_ci	TLB_WRITE_HEURISTICS
3838c2ecf20Sopenharmony_ci#endif
3848c2ecf20Sopenharmony_ci
3858c2ecf20Sopenharmony_ci	COMMIT_ENTRY_TO_MMU
3868c2ecf20Sopenharmony_ci	TLBMISS_RESTORE_REGS
3878c2ecf20Sopenharmony_ciEV_TLBMissD_fast_ret:	; additional label for VDK OS-kit instrumentation
3888c2ecf20Sopenharmony_ci	rtie
3898c2ecf20Sopenharmony_ci
3908c2ecf20Sopenharmony_ci;-------- Common routine to call Linux Page Fault Handler -----------
3918c2ecf20Sopenharmony_cido_slow_path_pf:
3928c2ecf20Sopenharmony_ci
3938c2ecf20Sopenharmony_ci#ifdef CONFIG_ISA_ARCV2
3948c2ecf20Sopenharmony_ci	; Set Z flag if exception in U mode. Hardware micro-ops do this on any
3958c2ecf20Sopenharmony_ci	; taken interrupt/exception, and thus is already the case at the entry
3968c2ecf20Sopenharmony_ci	; above, but ensuing code would have already clobbered.
3978c2ecf20Sopenharmony_ci	; EXCEPTION_PROLOGUE called in slow path, relies on correct Z flag set
3988c2ecf20Sopenharmony_ci
3998c2ecf20Sopenharmony_ci	lr	r2, [erstatus]
4008c2ecf20Sopenharmony_ci	and	r2, r2, STATUS_U_MASK
4018c2ecf20Sopenharmony_ci	bxor.f	0, r2, STATUS_U_BIT
4028c2ecf20Sopenharmony_ci#endif
4038c2ecf20Sopenharmony_ci
4048c2ecf20Sopenharmony_ci	; Restore the 4-scratch regs saved by fast path miss handler
4058c2ecf20Sopenharmony_ci	TLBMISS_RESTORE_REGS
4068c2ecf20Sopenharmony_ci
4078c2ecf20Sopenharmony_ci	; Slow path TLB Miss handled as a regular ARC Exception
4088c2ecf20Sopenharmony_ci	; (stack switching / save the complete reg-file).
4098c2ecf20Sopenharmony_ci	b  call_do_page_fault
4108c2ecf20Sopenharmony_ciEND(EV_TLBMissD)
411