blob: 404e5be4f704dd39bdc845d63f0508e1d4b7e595 [file] [log] [blame]
Vineet Guptaf1f33472013-01-18 15:12:19 +05301/*
2 * TLB Management (flush/create/diagnostics) for ARC700
3 *
4 * Copyright (C) 2004, 2007-2010, 2011-2012 Synopsys, Inc. (www.synopsys.com)
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 as
8 * published by the Free Software Foundation.
9 */
10
11#include <linux/module.h>
12#include <asm/arcregs.h>
13#include <asm/mmu_context.h>
14#include <asm/tlb.h>
15
16/* A copy of the ASID from the PID reg is kept in asid_cache */
17int asid_cache = FIRST_ASID;
18
19/* ASID to mm struct mapping. We have one extra entry corresponding to
20 * NO_ASID to save us a compare when clearing the mm entry for old asid
21 * see get_new_mmu_context (asm-arc/mmu_context.h)
22 */
23struct mm_struct *asid_mm_map[NUM_ASID + 1];
Vineet Guptacc562d22013-01-18 15:12:19 +053024
25
26/*
27 * Routine to create a TLB entry
28 */
29void create_tlb(struct vm_area_struct *vma, unsigned long address, pte_t *ptep)
30{
31 unsigned long flags;
32 unsigned int idx, asid_or_sasid;
33 unsigned long pd0_flags;
34
35 /*
36 * create_tlb() assumes that current->mm == vma->mm, since
37 * -it ASID for TLB entry is fetched from MMU ASID reg (valid for curr)
38 * -completes the lazy write to SASID reg (again valid for curr tsk)
39 *
40 * Removing the assumption involves
41 * -Using vma->mm->context{ASID,SASID}, as opposed to MMU reg.
42 * -Fix the TLB paranoid debug code to not trigger false negatives.
43 * -More importantly it makes this handler inconsistent with fast-path
44 * TLB Refill handler which always deals with "current"
45 *
46 * Lets see the use cases when current->mm != vma->mm and we land here
47 * 1. execve->copy_strings()->__get_user_pages->handle_mm_fault
48 * Here VM wants to pre-install a TLB entry for user stack while
49 * current->mm still points to pre-execve mm (hence the condition).
50 * However the stack vaddr is soon relocated (randomization) and
51 * move_page_tables() tries to undo that TLB entry.
52 * Thus not creating TLB entry is not any worse.
53 *
54 * 2. ptrace(POKETEXT) causes a CoW - debugger(current) inserting a
55 * breakpoint in debugged task. Not creating a TLB now is not
56 * performance critical.
57 *
58 * Both the cases above are not good enough for code churn.
59 */
60 if (current->active_mm != vma->vm_mm)
61 return;
62
63 local_irq_save(flags);
64
65 tlb_paranoid_check(vma->vm_mm->context.asid, address);
66
67 address &= PAGE_MASK;
68
69 /* update this PTE credentials */
70 pte_val(*ptep) |= (_PAGE_PRESENT | _PAGE_ACCESSED);
71
72 /* Create HW TLB entry Flags (in PD0) from PTE Flags */
73#if (CONFIG_ARC_MMU_VER <= 2)
74 pd0_flags = ((pte_val(*ptep) & PTE_BITS_IN_PD0) >> 1);
75#else
76 pd0_flags = ((pte_val(*ptep) & PTE_BITS_IN_PD0));
77#endif
78
79 /* ASID for this task */
80 asid_or_sasid = read_aux_reg(ARC_REG_PID) & 0xff;
81
82 write_aux_reg(ARC_REG_TLBPD0, address | pd0_flags | asid_or_sasid);
83
84 /* Load remaining info in PD1 (Page Frame Addr and Kx/Kw/Kr Flags) */
85 write_aux_reg(ARC_REG_TLBPD1, (pte_val(*ptep) & PTE_BITS_IN_PD1));
86
87 /* First verify if entry for this vaddr+ASID already exists */
88 write_aux_reg(ARC_REG_TLBCOMMAND, TLBProbe);
89 idx = read_aux_reg(ARC_REG_TLBINDEX);
90
91 /*
92 * If Not already present get a free slot from MMU.
93 * Otherwise, Probe would have located the entry and set INDEX Reg
94 * with existing location. This will cause Write CMD to over-write
95 * existing entry with new PD0 and PD1
96 */
97 if (likely(idx & TLB_LKUP_ERR))
98 write_aux_reg(ARC_REG_TLBCOMMAND, TLBGetIndex);
99
100 /*
101 * Commit the Entry to MMU
102 * It doesnt sound safe to use the TLBWriteNI cmd here
103 * which doesn't flush uTLBs. I'd rather be safe than sorry.
104 */
105 write_aux_reg(ARC_REG_TLBCOMMAND, TLBWrite);
106
107 local_irq_restore(flags);
108}
109
110/* arch hook called by core VM at the end of handle_mm_fault( ),
111 * when a new PTE is entered in Page Tables or an existing one
112 * is modified. We aggresively pre-install a TLB entry
113 */
114
115void update_mmu_cache(struct vm_area_struct *vma, unsigned long vaddress,
116 pte_t *ptep)
117{
118
119 create_tlb(vma, vaddress, ptep);
120}
121
122/* Read the Cache Build Confuration Registers, Decode them and save into
123 * the cpuinfo structure for later use.
124 * No Validation is done here, simply read/convert the BCRs
125 */
126void __init read_decode_mmu_bcr(void)
127{
128 unsigned int tmp;
129 struct bcr_mmu_1_2 *mmu2; /* encoded MMU2 attr */
130 struct bcr_mmu_3 *mmu3; /* encoded MMU3 attr */
131 struct cpuinfo_arc_mmu *mmu = &cpuinfo_arc700[smp_processor_id()].mmu;
132
133 tmp = read_aux_reg(ARC_REG_MMU_BCR);
134 mmu->ver = (tmp >> 24);
135
136 if (mmu->ver <= 2) {
137 mmu2 = (struct bcr_mmu_1_2 *)&tmp;
138 mmu->pg_sz = PAGE_SIZE;
139 mmu->sets = 1 << mmu2->sets;
140 mmu->ways = 1 << mmu2->ways;
141 mmu->u_dtlb = mmu2->u_dtlb;
142 mmu->u_itlb = mmu2->u_itlb;
143 } else {
144 mmu3 = (struct bcr_mmu_3 *)&tmp;
145 mmu->pg_sz = 512 << mmu3->pg_sz;
146 mmu->sets = 1 << mmu3->sets;
147 mmu->ways = 1 << mmu3->ways;
148 mmu->u_dtlb = mmu3->u_dtlb;
149 mmu->u_itlb = mmu3->u_itlb;
150 }
151
152 mmu->num_tlb = mmu->sets * mmu->ways;
153}
154
155void __init arc_mmu_init(void)
156{
157 /*
158 * ASID mgmt data structures are compile time init
159 * asid_cache = FIRST_ASID and asid_mm_map[] all zeroes
160 */
161
162 local_flush_tlb_all();
163
164 /* Enable the MMU */
165 write_aux_reg(ARC_REG_PID, MMU_ENABLE);
166}
167
168/*
169 * TLB Programmer's Model uses Linear Indexes: 0 to {255, 511} for 128 x {2,4}
170 * The mapping is Column-first.
171 * --------------------- -----------
172 * |way0|way1|way2|way3| |way0|way1|
173 * --------------------- -----------
174 * [set0] | 0 | 1 | 2 | 3 | | 0 | 1 |
175 * [set1] | 4 | 5 | 6 | 7 | | 2 | 3 |
176 * ~ ~ ~ ~
177 * [set127] | 508| 509| 510| 511| | 254| 255|
178 * --------------------- -----------
179 * For normal operations we don't(must not) care how above works since
180 * MMU cmd getIndex(vaddr) abstracts that out.
181 * However for walking WAYS of a SET, we need to know this
182 */
183#define SET_WAY_TO_IDX(mmu, set, way) ((set) * mmu->ways + (way))
184
185/* Handling of Duplicate PD (TLB entry) in MMU.
186 * -Could be due to buggy customer tapeouts or obscure kernel bugs
187 * -MMU complaints not at the time of duplicate PD installation, but at the
188 * time of lookup matching multiple ways.
189 * -Ideally these should never happen - but if they do - workaround by deleting
190 * the duplicate one.
191 * -Knob to be verbose abt it.(TODO: hook them up to debugfs)
192 */
193volatile int dup_pd_verbose = 1;/* Be slient abt it or complain (default) */
194
195void do_tlb_overlap_fault(unsigned long cause, unsigned long address,
196 struct pt_regs *regs)
197{
198 int set, way, n;
199 unsigned int pd0[4], pd1[4]; /* assume max 4 ways */
200 unsigned long flags, is_valid;
201 struct cpuinfo_arc_mmu *mmu = &cpuinfo_arc700[smp_processor_id()].mmu;
202
203 local_irq_save(flags);
204
205 /* re-enable the MMU */
206 write_aux_reg(ARC_REG_PID, MMU_ENABLE | read_aux_reg(ARC_REG_PID));
207
208 /* loop thru all sets of TLB */
209 for (set = 0; set < mmu->sets; set++) {
210
211 /* read out all the ways of current set */
212 for (way = 0, is_valid = 0; way < mmu->ways; way++) {
213 write_aux_reg(ARC_REG_TLBINDEX,
214 SET_WAY_TO_IDX(mmu, set, way));
215 write_aux_reg(ARC_REG_TLBCOMMAND, TLBRead);
216 pd0[way] = read_aux_reg(ARC_REG_TLBPD0);
217 pd1[way] = read_aux_reg(ARC_REG_TLBPD1);
218 is_valid |= pd0[way] & _PAGE_PRESENT;
219 }
220
221 /* If all the WAYS in SET are empty, skip to next SET */
222 if (!is_valid)
223 continue;
224
225 /* Scan the set for duplicate ways: needs a nested loop */
226 for (way = 0; way < mmu->ways; way++) {
227 if (!pd0[way])
228 continue;
229
230 for (n = way + 1; n < mmu->ways; n++) {
231 if ((pd0[way] & PAGE_MASK) ==
232 (pd0[n] & PAGE_MASK)) {
233
234 if (dup_pd_verbose) {
235 pr_info("Duplicate PD's @"
236 "[%d:%d]/[%d:%d]\n",
237 set, way, set, n);
238 pr_info("TLBPD0[%u]: %08x\n",
239 way, pd0[way]);
240 }
241
242 /*
243 * clear entry @way and not @n. This is
244 * critical to our optimised loop
245 */
246 pd0[way] = pd1[way] = 0;
247 write_aux_reg(ARC_REG_TLBINDEX,
248 SET_WAY_TO_IDX(mmu, set, way));
249 __tlb_entry_erase();
250 }
251 }
252 }
253 }
254
255 local_irq_restore(flags);
256}
257
258/***********************************************************************
259 * Diagnostic Routines
260 * -Called from Low Level TLB Hanlders if things don;t look good
261 **********************************************************************/
262
263#ifdef CONFIG_ARC_DBG_TLB_PARANOIA
264
265/*
266 * Low Level ASM TLB handler calls this if it finds that HW and SW ASIDS
267 * don't match
268 */
269void print_asid_mismatch(int is_fast_path)
270{
271 int pid_sw, pid_hw;
272 pid_sw = current->active_mm->context.asid;
273 pid_hw = read_aux_reg(ARC_REG_PID) & 0xff;
274
275 pr_emerg("ASID Mismatch in %s Path Handler: sw-pid=0x%x hw-pid=0x%x\n",
276 is_fast_path ? "Fast" : "Slow", pid_sw, pid_hw);
277
278 __asm__ __volatile__("flag 1");
279}
280
281void tlb_paranoid_check(unsigned int pid_sw, unsigned long addr)
282{
283 unsigned int pid_hw;
284
285 pid_hw = read_aux_reg(ARC_REG_PID) & 0xff;
286
287 if (addr < 0x70000000 && ((pid_hw != pid_sw) || (pid_sw == NO_ASID)))
288 print_asid_mismatch(0);
289}
290#endif