blob: e90b2e899291620ee8d1b89f8674bf93b5450274 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * This file is subject to the terms and conditions of the GNU General Public
3 * License. See the file "COPYING" in the main directory of this archive
4 * for more details.
5 *
Justin P. Mattock79add622011-04-04 14:15:29 -07006 * Copyright (C) 1996 David S. Miller (davem@davemloft.net)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007 * Copyright (C) 1997, 1998, 1999, 2000 Ralf Baechle ralf@gnu.org
8 * Carsten Langgaard, carstenl@mips.com
9 * Copyright (C) 2002 MIPS Technologies, Inc. All rights reserved.
10 */
James Hoganeaa38d62014-02-28 17:09:20 +000011#include <linux/cpu_pm.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070012#include <linux/init.h>
13#include <linux/sched.h>
Ralf Baechle631330f2009-06-19 14:05:26 +010014#include <linux/smp.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070015#include <linux/mm.h>
David Daneyfd062c82009-05-27 17:47:44 -070016#include <linux/hugetlb.h>
Sanjay Lalf2e36562012-11-21 18:34:10 -080017#include <linux/module.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070018
19#include <asm/cpu.h>
Ralf Baechle69f24d12013-09-17 10:25:47 +020020#include <asm/cpu-type.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070021#include <asm/bootinfo.h>
22#include <asm/mmu_context.h>
23#include <asm/pgtable.h>
Markos Chandrasc01905e2013-11-14 16:12:22 +000024#include <asm/tlb.h>
Ralf Baechle3d18c982011-11-28 16:11:28 +000025#include <asm/tlbmisc.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070026
27extern void build_tlb_refill_handler(void);
28
Fuxin Zhang2a21c732007-06-06 14:52:43 +080029/*
Huacai Chenc579d312014-03-21 18:44:00 +080030 * LOONGSON2/3 has a 4 entry itlb which is a subset of dtlb,
31 * unfortunately, itlb is not totally transparent to software.
Fuxin Zhang2a21c732007-06-06 14:52:43 +080032 */
Ralf Baechle14bd8c02013-09-25 18:21:26 +020033static inline void flush_itlb(void)
34{
35 switch (current_cpu_type()) {
36 case CPU_LOONGSON2:
Huacai Chenc579d312014-03-21 18:44:00 +080037 case CPU_LOONGSON3:
Ralf Baechle14bd8c02013-09-25 18:21:26 +020038 write_c0_diag(4);
39 break;
40 default:
41 break;
42 }
43}
Fuxin Zhang2a21c732007-06-06 14:52:43 +080044
Ralf Baechle14bd8c02013-09-25 18:21:26 +020045static inline void flush_itlb_vm(struct vm_area_struct *vma)
46{
47 if (vma->vm_flags & VM_EXEC)
48 flush_itlb();
49}
Fuxin Zhang2a21c732007-06-06 14:52:43 +080050
Linus Torvalds1da177e2005-04-16 15:20:36 -070051void local_flush_tlb_all(void)
52{
53 unsigned long flags;
54 unsigned long old_ctx;
Leonid Yegoshin75b5b5e2013-11-14 16:12:31 +000055 int entry, ftlbhighset;
Linus Torvalds1da177e2005-04-16 15:20:36 -070056
Ralf Baechleb633648c52014-05-23 16:29:44 +020057 local_irq_save(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -070058 /* Save old context and create impossible VPN2 value */
59 old_ctx = read_c0_entryhi();
Markos Chandrasf1014d12014-07-14 12:47:09 +010060 htw_stop();
Linus Torvalds1da177e2005-04-16 15:20:36 -070061 write_c0_entrylo0(0);
62 write_c0_entrylo1(0);
63
64 entry = read_c0_wired();
65
66 /* Blast 'em all away. */
Leonid Yegoshin75b5b5e2013-11-14 16:12:31 +000067 if (cpu_has_tlbinv) {
68 if (current_cpu_data.tlbsizevtlb) {
69 write_c0_index(0);
70 mtc0_tlbw_hazard();
71 tlbinvf(); /* invalidate VTLB */
72 }
73 ftlbhighset = current_cpu_data.tlbsizevtlb +
74 current_cpu_data.tlbsizeftlbsets;
75 for (entry = current_cpu_data.tlbsizevtlb;
76 entry < ftlbhighset;
77 entry++) {
78 write_c0_index(entry);
79 mtc0_tlbw_hazard();
80 tlbinvf(); /* invalidate one FTLB set */
81 }
Leonid Yegoshin601cfa72013-11-14 16:12:30 +000082 } else {
83 while (entry < current_cpu_data.tlbsize) {
84 /* Make sure all entries differ. */
85 write_c0_entryhi(UNIQUE_ENTRYHI(entry));
86 write_c0_index(entry);
87 mtc0_tlbw_hazard();
88 tlb_write_indexed();
89 entry++;
90 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070091 }
92 tlbw_use_hazard();
93 write_c0_entryhi(old_ctx);
Markos Chandrasf1014d12014-07-14 12:47:09 +010094 htw_start();
Ralf Baechle14bd8c02013-09-25 18:21:26 +020095 flush_itlb();
Ralf Baechleb633648c52014-05-23 16:29:44 +020096 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -070097}
Sanjay Lalf2e36562012-11-21 18:34:10 -080098EXPORT_SYMBOL(local_flush_tlb_all);
Linus Torvalds1da177e2005-04-16 15:20:36 -070099
Thiemo Seufer172546b2005-04-02 10:21:56 +0000100/* All entries common to a mm share an asid. To effectively flush
101 these entries, we just bump the asid. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102void local_flush_tlb_mm(struct mm_struct *mm)
103{
Thiemo Seufer172546b2005-04-02 10:21:56 +0000104 int cpu;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105
Thiemo Seufer172546b2005-04-02 10:21:56 +0000106 preempt_disable();
107
108 cpu = smp_processor_id();
109
110 if (cpu_context(cpu, mm) != 0) {
111 drop_mmu_context(mm, cpu);
112 }
113
114 preempt_enable();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115}
116
117void local_flush_tlb_range(struct vm_area_struct *vma, unsigned long start,
118 unsigned long end)
119{
120 struct mm_struct *mm = vma->vm_mm;
121 int cpu = smp_processor_id();
122
123 if (cpu_context(cpu, mm) != 0) {
Greg Ungerera5e696e2009-05-20 16:12:32 +1000124 unsigned long size, flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125
Ralf Baechleb633648c52014-05-23 16:29:44 +0200126 local_irq_save(flags);
David Daneyac53c4f2012-12-03 12:44:26 -0800127 start = round_down(start, PAGE_SIZE << 1);
128 end = round_up(end, PAGE_SIZE << 1);
129 size = (end - start) >> (PAGE_SHIFT + 1);
Leonid Yegoshin75b5b5e2013-11-14 16:12:31 +0000130 if (size <= (current_cpu_data.tlbsizeftlbsets ?
131 current_cpu_data.tlbsize / 8 :
132 current_cpu_data.tlbsize / 2)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133 int oldpid = read_c0_entryhi();
134 int newpid = cpu_asid(cpu, mm);
135
Markos Chandrasf1014d12014-07-14 12:47:09 +0100136 htw_stop();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137 while (start < end) {
138 int idx;
139
140 write_c0_entryhi(start | newpid);
David Daneyac53c4f2012-12-03 12:44:26 -0800141 start += (PAGE_SIZE << 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142 mtc0_tlbw_hazard();
143 tlb_probe();
Ralf Baechle432bef22006-09-08 04:16:21 +0200144 tlb_probe_hazard();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145 idx = read_c0_index();
146 write_c0_entrylo0(0);
147 write_c0_entrylo1(0);
148 if (idx < 0)
149 continue;
150 /* Make sure all entries differ. */
Thiemo Seufer172546b2005-04-02 10:21:56 +0000151 write_c0_entryhi(UNIQUE_ENTRYHI(idx));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152 mtc0_tlbw_hazard();
153 tlb_write_indexed();
154 }
155 tlbw_use_hazard();
156 write_c0_entryhi(oldpid);
Markos Chandrasf1014d12014-07-14 12:47:09 +0100157 htw_start();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158 } else {
159 drop_mmu_context(mm, cpu);
160 }
Ralf Baechle14bd8c02013-09-25 18:21:26 +0200161 flush_itlb();
Ralf Baechleb633648c52014-05-23 16:29:44 +0200162 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163 }
164}
165
166void local_flush_tlb_kernel_range(unsigned long start, unsigned long end)
167{
Greg Ungerera5e696e2009-05-20 16:12:32 +1000168 unsigned long size, flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169
Ralf Baechleb633648c52014-05-23 16:29:44 +0200170 local_irq_save(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171 size = (end - start + (PAGE_SIZE - 1)) >> PAGE_SHIFT;
172 size = (size + 1) >> 1;
Leonid Yegoshin75b5b5e2013-11-14 16:12:31 +0000173 if (size <= (current_cpu_data.tlbsizeftlbsets ?
174 current_cpu_data.tlbsize / 8 :
175 current_cpu_data.tlbsize / 2)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176 int pid = read_c0_entryhi();
177
178 start &= (PAGE_MASK << 1);
179 end += ((PAGE_SIZE << 1) - 1);
180 end &= (PAGE_MASK << 1);
Markos Chandrasf1014d12014-07-14 12:47:09 +0100181 htw_stop();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182
183 while (start < end) {
184 int idx;
185
186 write_c0_entryhi(start);
187 start += (PAGE_SIZE << 1);
188 mtc0_tlbw_hazard();
189 tlb_probe();
Ralf Baechle432bef22006-09-08 04:16:21 +0200190 tlb_probe_hazard();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191 idx = read_c0_index();
192 write_c0_entrylo0(0);
193 write_c0_entrylo1(0);
194 if (idx < 0)
195 continue;
196 /* Make sure all entries differ. */
Thiemo Seufer172546b2005-04-02 10:21:56 +0000197 write_c0_entryhi(UNIQUE_ENTRYHI(idx));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198 mtc0_tlbw_hazard();
199 tlb_write_indexed();
200 }
201 tlbw_use_hazard();
202 write_c0_entryhi(pid);
Markos Chandrasf1014d12014-07-14 12:47:09 +0100203 htw_start();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204 } else {
205 local_flush_tlb_all();
206 }
Ralf Baechle14bd8c02013-09-25 18:21:26 +0200207 flush_itlb();
Ralf Baechleb633648c52014-05-23 16:29:44 +0200208 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209}
210
211void local_flush_tlb_page(struct vm_area_struct *vma, unsigned long page)
212{
213 int cpu = smp_processor_id();
214
215 if (cpu_context(cpu, vma->vm_mm) != 0) {
216 unsigned long flags;
217 int oldpid, newpid, idx;
218
219 newpid = cpu_asid(cpu, vma->vm_mm);
220 page &= (PAGE_MASK << 1);
Ralf Baechleb633648c52014-05-23 16:29:44 +0200221 local_irq_save(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222 oldpid = read_c0_entryhi();
Markos Chandrasf1014d12014-07-14 12:47:09 +0100223 htw_stop();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224 write_c0_entryhi(page | newpid);
225 mtc0_tlbw_hazard();
226 tlb_probe();
Ralf Baechle432bef22006-09-08 04:16:21 +0200227 tlb_probe_hazard();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228 idx = read_c0_index();
229 write_c0_entrylo0(0);
230 write_c0_entrylo1(0);
231 if (idx < 0)
232 goto finish;
233 /* Make sure all entries differ. */
Thiemo Seufer172546b2005-04-02 10:21:56 +0000234 write_c0_entryhi(UNIQUE_ENTRYHI(idx));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235 mtc0_tlbw_hazard();
236 tlb_write_indexed();
237 tlbw_use_hazard();
238
239 finish:
240 write_c0_entryhi(oldpid);
Markos Chandrasf1014d12014-07-14 12:47:09 +0100241 htw_start();
Ralf Baechle14bd8c02013-09-25 18:21:26 +0200242 flush_itlb_vm(vma);
Ralf Baechleb633648c52014-05-23 16:29:44 +0200243 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244 }
245}
246
247/*
248 * This one is only used for pages with the global bit set so we don't care
249 * much about the ASID.
250 */
251void local_flush_tlb_one(unsigned long page)
252{
253 unsigned long flags;
254 int oldpid, idx;
255
Ralf Baechleb633648c52014-05-23 16:29:44 +0200256 local_irq_save(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257 oldpid = read_c0_entryhi();
Markos Chandrasf1014d12014-07-14 12:47:09 +0100258 htw_stop();
Thiemo Seufer172546b2005-04-02 10:21:56 +0000259 page &= (PAGE_MASK << 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260 write_c0_entryhi(page);
261 mtc0_tlbw_hazard();
262 tlb_probe();
Ralf Baechle432bef22006-09-08 04:16:21 +0200263 tlb_probe_hazard();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264 idx = read_c0_index();
265 write_c0_entrylo0(0);
266 write_c0_entrylo1(0);
267 if (idx >= 0) {
268 /* Make sure all entries differ. */
Thiemo Seufer172546b2005-04-02 10:21:56 +0000269 write_c0_entryhi(UNIQUE_ENTRYHI(idx));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270 mtc0_tlbw_hazard();
271 tlb_write_indexed();
272 tlbw_use_hazard();
273 }
274 write_c0_entryhi(oldpid);
Markos Chandrasf1014d12014-07-14 12:47:09 +0100275 htw_start();
Ralf Baechle14bd8c02013-09-25 18:21:26 +0200276 flush_itlb();
Ralf Baechleb633648c52014-05-23 16:29:44 +0200277 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278}
279
280/*
281 * We will need multiple versions of update_mmu_cache(), one that just
282 * updates the TLB with the new pte(s), and another which also checks
283 * for the R4k "end of page" hardware bug and does the needy.
284 */
285void __update_tlb(struct vm_area_struct * vma, unsigned long address, pte_t pte)
286{
287 unsigned long flags;
288 pgd_t *pgdp;
Ralf Baechlec6e8b582005-02-10 12:19:59 +0000289 pud_t *pudp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290 pmd_t *pmdp;
291 pte_t *ptep;
292 int idx, pid;
293
294 /*
295 * Handle debugger faulting in for debugee.
296 */
297 if (current->active_mm != vma->vm_mm)
298 return;
299
Ralf Baechleb633648c52014-05-23 16:29:44 +0200300 local_irq_save(flags);
Thiemo Seufer172546b2005-04-02 10:21:56 +0000301
Markos Chandras6a8dff62014-11-17 09:31:07 +0000302 htw_stop();
David Daney48c4ac92013-05-13 13:56:44 -0700303 pid = read_c0_entryhi() & ASID_MASK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304 address &= (PAGE_MASK << 1);
305 write_c0_entryhi(address | pid);
306 pgdp = pgd_offset(vma->vm_mm, address);
307 mtc0_tlbw_hazard();
308 tlb_probe();
Ralf Baechle432bef22006-09-08 04:16:21 +0200309 tlb_probe_hazard();
Ralf Baechlec6e8b582005-02-10 12:19:59 +0000310 pudp = pud_offset(pgdp, address);
311 pmdp = pmd_offset(pudp, address);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312 idx = read_c0_index();
David Daneyaa1762f2012-10-17 00:48:10 +0200313#ifdef CONFIG_MIPS_HUGE_TLB_SUPPORT
David Daneyfd062c82009-05-27 17:47:44 -0700314 /* this could be a huge page */
315 if (pmd_huge(*pmdp)) {
316 unsigned long lo;
317 write_c0_pagemask(PM_HUGE_MASK);
318 ptep = (pte_t *)pmdp;
David Daney6dd93442010-02-10 15:12:47 -0800319 lo = pte_to_entrylo(pte_val(*ptep));
David Daneyfd062c82009-05-27 17:47:44 -0700320 write_c0_entrylo0(lo);
321 write_c0_entrylo1(lo + (HPAGE_SIZE >> 7));
322
323 mtc0_tlbw_hazard();
324 if (idx < 0)
325 tlb_write_random();
326 else
327 tlb_write_indexed();
Ralf Baechlefb944c92012-10-17 01:01:21 +0200328 tlbw_use_hazard();
David Daneyfd062c82009-05-27 17:47:44 -0700329 write_c0_pagemask(PM_DEFAULT_MASK);
330 } else
331#endif
332 {
333 ptep = pte_offset_map(pmdp, address);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334
Ralf Baechle34adb282014-11-22 00:16:48 +0100335#if defined(CONFIG_PHYS_ADDR_T_64BIT) && defined(CONFIG_CPU_MIPS32)
David Daneyfd062c82009-05-27 17:47:44 -0700336 write_c0_entrylo0(ptep->pte_high);
337 ptep++;
338 write_c0_entrylo1(ptep->pte_high);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339#else
David Daney6dd93442010-02-10 15:12:47 -0800340 write_c0_entrylo0(pte_to_entrylo(pte_val(*ptep++)));
341 write_c0_entrylo1(pte_to_entrylo(pte_val(*ptep)));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342#endif
David Daneyfd062c82009-05-27 17:47:44 -0700343 mtc0_tlbw_hazard();
344 if (idx < 0)
345 tlb_write_random();
346 else
347 tlb_write_indexed();
348 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349 tlbw_use_hazard();
Markos Chandras6a8dff62014-11-17 09:31:07 +0000350 htw_start();
Ralf Baechle14bd8c02013-09-25 18:21:26 +0200351 flush_itlb_vm(vma);
Ralf Baechleb633648c52014-05-23 16:29:44 +0200352 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353}
354
Manuel Lauss694b8c32011-08-02 19:51:08 +0200355void add_wired_entry(unsigned long entrylo0, unsigned long entrylo1,
356 unsigned long entryhi, unsigned long pagemask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357{
358 unsigned long flags;
359 unsigned long wired;
360 unsigned long old_pagemask;
361 unsigned long old_ctx;
362
Ralf Baechleb633648c52014-05-23 16:29:44 +0200363 local_irq_save(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364 /* Save old context and create impossible VPN2 value */
365 old_ctx = read_c0_entryhi();
Markos Chandrasf1014d12014-07-14 12:47:09 +0100366 htw_stop();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367 old_pagemask = read_c0_pagemask();
368 wired = read_c0_wired();
369 write_c0_wired(wired + 1);
370 write_c0_index(wired);
Ralf Baechle432bef22006-09-08 04:16:21 +0200371 tlbw_use_hazard(); /* What is the hazard here? */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372 write_c0_pagemask(pagemask);
373 write_c0_entryhi(entryhi);
374 write_c0_entrylo0(entrylo0);
375 write_c0_entrylo1(entrylo1);
376 mtc0_tlbw_hazard();
377 tlb_write_indexed();
378 tlbw_use_hazard();
379
380 write_c0_entryhi(old_ctx);
Ralf Baechle432bef22006-09-08 04:16:21 +0200381 tlbw_use_hazard(); /* What is the hazard here? */
Markos Chandrasf1014d12014-07-14 12:47:09 +0100382 htw_start();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383 write_c0_pagemask(old_pagemask);
384 local_flush_tlb_all();
Ralf Baechleb633648c52014-05-23 16:29:44 +0200385 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386}
387
Ralf Baechle970d0322012-10-18 13:54:15 +0200388#ifdef CONFIG_TRANSPARENT_HUGEPAGE
389
390int __init has_transparent_hugepage(void)
391{
392 unsigned int mask;
393 unsigned long flags;
394
Ralf Baechleb633648c52014-05-23 16:29:44 +0200395 local_irq_save(flags);
Ralf Baechle970d0322012-10-18 13:54:15 +0200396 write_c0_pagemask(PM_HUGE_MASK);
397 back_to_back_c0_hazard();
398 mask = read_c0_pagemask();
399 write_c0_pagemask(PM_DEFAULT_MASK);
400
Ralf Baechleb633648c52014-05-23 16:29:44 +0200401 local_irq_restore(flags);
Ralf Baechle970d0322012-10-18 13:54:15 +0200402
403 return mask == PM_HUGE_MASK;
404}
405
406#endif /* CONFIG_TRANSPARENT_HUGEPAGE */
407
Rafał Miłeckid3777322014-07-17 23:26:32 +0200408/*
409 * Used for loading TLB entries before trap_init() has started, when we
410 * don't actually want to add a wired entry which remains throughout the
411 * lifetime of the system
412 */
413
Rafał Miłecki6ee1d932014-07-17 23:26:33 +0200414int temp_tlb_entry __cpuinitdata;
Rafał Miłeckid3777322014-07-17 23:26:32 +0200415
416__init int add_temporary_entry(unsigned long entrylo0, unsigned long entrylo1,
417 unsigned long entryhi, unsigned long pagemask)
418{
419 int ret = 0;
420 unsigned long flags;
421 unsigned long wired;
422 unsigned long old_pagemask;
423 unsigned long old_ctx;
424
425 local_irq_save(flags);
426 /* Save old context and create impossible VPN2 value */
Markos Chandras6a8dff62014-11-17 09:31:07 +0000427 htw_stop();
Rafał Miłeckid3777322014-07-17 23:26:32 +0200428 old_ctx = read_c0_entryhi();
429 old_pagemask = read_c0_pagemask();
430 wired = read_c0_wired();
431 if (--temp_tlb_entry < wired) {
432 printk(KERN_WARNING
433 "No TLB space left for add_temporary_entry\n");
434 ret = -ENOSPC;
435 goto out;
436 }
437
438 write_c0_index(temp_tlb_entry);
439 write_c0_pagemask(pagemask);
440 write_c0_entryhi(entryhi);
441 write_c0_entrylo0(entrylo0);
442 write_c0_entrylo1(entrylo1);
443 mtc0_tlbw_hazard();
444 tlb_write_indexed();
445 tlbw_use_hazard();
446
447 write_c0_entryhi(old_ctx);
448 write_c0_pagemask(old_pagemask);
Markos Chandras6a8dff62014-11-17 09:31:07 +0000449 htw_start();
Rafał Miłeckid3777322014-07-17 23:26:32 +0200450out:
451 local_irq_restore(flags);
452 return ret;
453}
454
Paul Gortmaker078a55f2013-06-18 13:38:59 +0000455static int ntlb;
Ralf Baechle41c594a2006-04-05 09:45:45 +0100456static int __init set_ntlb(char *str)
457{
458 get_option(&str, &ntlb);
459 return 1;
460}
461
462__setup("ntlb=", set_ntlb);
463
James Hoganeaa38d62014-02-28 17:09:20 +0000464/*
465 * Configure TLB (for init or after a CPU has been powered off).
466 */
467static void r4k_tlb_configure(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469 /*
470 * You should never change this register:
471 * - On R4600 1.7 the tlbp never hits for pages smaller than
472 * the value in the c0_pagemask register.
473 * - The entire mm handling assumes the c0_pagemask register to
Thiemo Seufera7c29962008-02-29 00:43:47 +0000474 * be set to fixed-size pages.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476 write_c0_pagemask(PM_DEFAULT_MASK);
477 write_c0_wired(0);
Ralf Baechlecde15b52009-01-06 23:07:20 +0000478 if (current_cpu_type() == CPU_R10000 ||
479 current_cpu_type() == CPU_R12000 ||
480 current_cpu_type() == CPU_R14000)
481 write_c0_framemask(0);
David Daney6dd93442010-02-10 15:12:47 -0800482
Steven J. Hill05857c62012-09-13 16:51:46 -0500483 if (cpu_has_rixi) {
David Daney6dd93442010-02-10 15:12:47 -0800484 /*
485 * Enable the no read, no exec bits, and enable large virtual
486 * address.
487 */
488 u32 pg = PG_RIE | PG_XIE;
489#ifdef CONFIG_64BIT
490 pg |= PG_ELPA;
491#endif
492 write_c0_pagegrain(pg);
493 }
494
Rafał Miłeckid3777322014-07-17 23:26:32 +0200495 temp_tlb_entry = current_cpu_data.tlbsize - 1;
496
Ralf Baechle70342282013-01-22 12:59:30 +0100497 /* From this point on the ARC firmware is dead. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498 local_flush_tlb_all();
499
Thiemo Seuferc6281ed2006-03-14 14:35:27 +0000500 /* Did I tell you that ARC SUCKS? */
James Hoganeaa38d62014-02-28 17:09:20 +0000501}
502
503void tlb_init(void)
504{
505 r4k_tlb_configure();
Thiemo Seuferc6281ed2006-03-14 14:35:27 +0000506
Ralf Baechle41c594a2006-04-05 09:45:45 +0100507 if (ntlb) {
508 if (ntlb > 1 && ntlb <= current_cpu_data.tlbsize) {
509 int wired = current_cpu_data.tlbsize - ntlb;
510 write_c0_wired(wired);
511 write_c0_index(wired-1);
Ralf Baechle49a89ef2007-10-11 23:46:15 +0100512 printk("Restricting TLB to %d entries\n", ntlb);
Ralf Baechle41c594a2006-04-05 09:45:45 +0100513 } else
514 printk("Ignoring invalid argument ntlb=%d\n", ntlb);
515 }
516
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517 build_tlb_refill_handler();
518}
James Hoganeaa38d62014-02-28 17:09:20 +0000519
520static int r4k_tlb_pm_notifier(struct notifier_block *self, unsigned long cmd,
521 void *v)
522{
523 switch (cmd) {
524 case CPU_PM_ENTER_FAILED:
525 case CPU_PM_EXIT:
526 r4k_tlb_configure();
527 break;
528 }
529
530 return NOTIFY_OK;
531}
532
533static struct notifier_block r4k_tlb_pm_notifier_block = {
534 .notifier_call = r4k_tlb_pm_notifier,
535};
536
537static int __init r4k_tlb_init_pm(void)
538{
539 return cpu_pm_register_notifier(&r4k_tlb_pm_notifier_block);
540}
541arch_initcall(r4k_tlb_init_pm);