blob: 5037d5868cef7ef47f1baaa74dc84b7da410c7d5 [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)
Steven J. Hillc5b36782015-02-26 18:16:38 -0600336#ifdef CONFIG_XPA
337 write_c0_entrylo0(pte_to_entrylo(ptep->pte_high));
338 writex_c0_entrylo0(ptep->pte_low & _PFNX_MASK);
339 ptep++;
340 write_c0_entrylo1(pte_to_entrylo(ptep->pte_high));
341 writex_c0_entrylo1(ptep->pte_low & _PFNX_MASK);
342#else
David Daneyfd062c82009-05-27 17:47:44 -0700343 write_c0_entrylo0(ptep->pte_high);
344 ptep++;
345 write_c0_entrylo1(ptep->pte_high);
Steven J. Hillc5b36782015-02-26 18:16:38 -0600346#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347#else
David Daney6dd93442010-02-10 15:12:47 -0800348 write_c0_entrylo0(pte_to_entrylo(pte_val(*ptep++)));
349 write_c0_entrylo1(pte_to_entrylo(pte_val(*ptep)));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350#endif
David Daneyfd062c82009-05-27 17:47:44 -0700351 mtc0_tlbw_hazard();
352 if (idx < 0)
353 tlb_write_random();
354 else
355 tlb_write_indexed();
356 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357 tlbw_use_hazard();
Markos Chandras6a8dff62014-11-17 09:31:07 +0000358 htw_start();
Ralf Baechle14bd8c02013-09-25 18:21:26 +0200359 flush_itlb_vm(vma);
Ralf Baechleb633648c52014-05-23 16:29:44 +0200360 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361}
362
Manuel Lauss694b8c32011-08-02 19:51:08 +0200363void add_wired_entry(unsigned long entrylo0, unsigned long entrylo1,
364 unsigned long entryhi, unsigned long pagemask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365{
Steven J. Hillc5b36782015-02-26 18:16:38 -0600366#ifdef CONFIG_XPA
367 panic("Broken for XPA kernels");
368#else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369 unsigned long flags;
370 unsigned long wired;
371 unsigned long old_pagemask;
372 unsigned long old_ctx;
373
Ralf Baechleb633648c52014-05-23 16:29:44 +0200374 local_irq_save(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375 /* Save old context and create impossible VPN2 value */
376 old_ctx = read_c0_entryhi();
Markos Chandrasf1014d12014-07-14 12:47:09 +0100377 htw_stop();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378 old_pagemask = read_c0_pagemask();
379 wired = read_c0_wired();
380 write_c0_wired(wired + 1);
381 write_c0_index(wired);
Ralf Baechle432bef22006-09-08 04:16:21 +0200382 tlbw_use_hazard(); /* What is the hazard here? */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383 write_c0_pagemask(pagemask);
384 write_c0_entryhi(entryhi);
385 write_c0_entrylo0(entrylo0);
386 write_c0_entrylo1(entrylo1);
387 mtc0_tlbw_hazard();
388 tlb_write_indexed();
389 tlbw_use_hazard();
390
391 write_c0_entryhi(old_ctx);
Ralf Baechle432bef22006-09-08 04:16:21 +0200392 tlbw_use_hazard(); /* What is the hazard here? */
Markos Chandrasf1014d12014-07-14 12:47:09 +0100393 htw_start();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394 write_c0_pagemask(old_pagemask);
395 local_flush_tlb_all();
Ralf Baechleb633648c52014-05-23 16:29:44 +0200396 local_irq_restore(flags);
Steven J. Hillc5b36782015-02-26 18:16:38 -0600397#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398}
399
Ralf Baechle970d0322012-10-18 13:54:15 +0200400#ifdef CONFIG_TRANSPARENT_HUGEPAGE
401
402int __init has_transparent_hugepage(void)
403{
404 unsigned int mask;
405 unsigned long flags;
406
Ralf Baechleb633648c52014-05-23 16:29:44 +0200407 local_irq_save(flags);
Ralf Baechle970d0322012-10-18 13:54:15 +0200408 write_c0_pagemask(PM_HUGE_MASK);
409 back_to_back_c0_hazard();
410 mask = read_c0_pagemask();
411 write_c0_pagemask(PM_DEFAULT_MASK);
412
Ralf Baechleb633648c52014-05-23 16:29:44 +0200413 local_irq_restore(flags);
Ralf Baechle970d0322012-10-18 13:54:15 +0200414
415 return mask == PM_HUGE_MASK;
416}
417
418#endif /* CONFIG_TRANSPARENT_HUGEPAGE */
419
Rafał Miłeckid3777322014-07-17 23:26:32 +0200420/*
421 * Used for loading TLB entries before trap_init() has started, when we
422 * don't actually want to add a wired entry which remains throughout the
423 * lifetime of the system
424 */
425
Paul Gortmakerb1f7e112015-04-27 18:47:56 -0400426int temp_tlb_entry;
Rafał Miłeckid3777322014-07-17 23:26:32 +0200427
428__init int add_temporary_entry(unsigned long entrylo0, unsigned long entrylo1,
429 unsigned long entryhi, unsigned long pagemask)
430{
431 int ret = 0;
432 unsigned long flags;
433 unsigned long wired;
434 unsigned long old_pagemask;
435 unsigned long old_ctx;
436
437 local_irq_save(flags);
438 /* Save old context and create impossible VPN2 value */
Markos Chandras6a8dff62014-11-17 09:31:07 +0000439 htw_stop();
Rafał Miłeckid3777322014-07-17 23:26:32 +0200440 old_ctx = read_c0_entryhi();
441 old_pagemask = read_c0_pagemask();
442 wired = read_c0_wired();
443 if (--temp_tlb_entry < wired) {
444 printk(KERN_WARNING
445 "No TLB space left for add_temporary_entry\n");
446 ret = -ENOSPC;
447 goto out;
448 }
449
450 write_c0_index(temp_tlb_entry);
451 write_c0_pagemask(pagemask);
452 write_c0_entryhi(entryhi);
453 write_c0_entrylo0(entrylo0);
454 write_c0_entrylo1(entrylo1);
455 mtc0_tlbw_hazard();
456 tlb_write_indexed();
457 tlbw_use_hazard();
458
459 write_c0_entryhi(old_ctx);
460 write_c0_pagemask(old_pagemask);
Markos Chandras6a8dff62014-11-17 09:31:07 +0000461 htw_start();
Rafał Miłeckid3777322014-07-17 23:26:32 +0200462out:
463 local_irq_restore(flags);
464 return ret;
465}
466
Paul Gortmaker078a55f2013-06-18 13:38:59 +0000467static int ntlb;
Ralf Baechle41c594a2006-04-05 09:45:45 +0100468static int __init set_ntlb(char *str)
469{
470 get_option(&str, &ntlb);
471 return 1;
472}
473
474__setup("ntlb=", set_ntlb);
475
James Hoganeaa38d62014-02-28 17:09:20 +0000476/*
477 * Configure TLB (for init or after a CPU has been powered off).
478 */
479static void r4k_tlb_configure(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481 /*
482 * You should never change this register:
483 * - On R4600 1.7 the tlbp never hits for pages smaller than
484 * the value in the c0_pagemask register.
485 * - The entire mm handling assumes the c0_pagemask register to
Thiemo Seufera7c29962008-02-29 00:43:47 +0000486 * be set to fixed-size pages.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488 write_c0_pagemask(PM_DEFAULT_MASK);
489 write_c0_wired(0);
Ralf Baechlecde15b52009-01-06 23:07:20 +0000490 if (current_cpu_type() == CPU_R10000 ||
491 current_cpu_type() == CPU_R12000 ||
Joshua Kinard30577392015-01-21 07:59:45 -0500492 current_cpu_type() == CPU_R14000 ||
493 current_cpu_type() == CPU_R16000)
Ralf Baechlecde15b52009-01-06 23:07:20 +0000494 write_c0_framemask(0);
David Daney6dd93442010-02-10 15:12:47 -0800495
Steven J. Hill05857c62012-09-13 16:51:46 -0500496 if (cpu_has_rixi) {
David Daney6dd93442010-02-10 15:12:47 -0800497 /*
James Hogane05cb562015-05-13 11:50:55 +0100498 * Enable the no read, no exec bits, and enable large physical
David Daney6dd93442010-02-10 15:12:47 -0800499 * address.
500 */
David Daney6dd93442010-02-10 15:12:47 -0800501#ifdef CONFIG_64BIT
Steven J. Hilla5770df2015-02-19 10:18:52 -0600502 set_c0_pagegrain(PG_RIE | PG_XIE | PG_ELPA);
503#else
504 set_c0_pagegrain(PG_RIE | PG_XIE);
David Daney6dd93442010-02-10 15:12:47 -0800505#endif
David Daney6dd93442010-02-10 15:12:47 -0800506 }
507
Rafał Miłeckid3777322014-07-17 23:26:32 +0200508 temp_tlb_entry = current_cpu_data.tlbsize - 1;
509
Ralf Baechle70342282013-01-22 12:59:30 +0100510 /* From this point on the ARC firmware is dead. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511 local_flush_tlb_all();
512
Thiemo Seuferc6281ed2006-03-14 14:35:27 +0000513 /* Did I tell you that ARC SUCKS? */
James Hoganeaa38d62014-02-28 17:09:20 +0000514}
515
516void tlb_init(void)
517{
518 r4k_tlb_configure();
Thiemo Seuferc6281ed2006-03-14 14:35:27 +0000519
Ralf Baechle41c594a2006-04-05 09:45:45 +0100520 if (ntlb) {
521 if (ntlb > 1 && ntlb <= current_cpu_data.tlbsize) {
522 int wired = current_cpu_data.tlbsize - ntlb;
523 write_c0_wired(wired);
524 write_c0_index(wired-1);
Ralf Baechle49a89ef2007-10-11 23:46:15 +0100525 printk("Restricting TLB to %d entries\n", ntlb);
Ralf Baechle41c594a2006-04-05 09:45:45 +0100526 } else
527 printk("Ignoring invalid argument ntlb=%d\n", ntlb);
528 }
529
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530 build_tlb_refill_handler();
531}
James Hoganeaa38d62014-02-28 17:09:20 +0000532
533static int r4k_tlb_pm_notifier(struct notifier_block *self, unsigned long cmd,
534 void *v)
535{
536 switch (cmd) {
537 case CPU_PM_ENTER_FAILED:
538 case CPU_PM_EXIT:
539 r4k_tlb_configure();
540 break;
541 }
542
543 return NOTIFY_OK;
544}
545
546static struct notifier_block r4k_tlb_pm_notifier_block = {
547 .notifier_call = r4k_tlb_pm_notifier,
548};
549
550static int __init r4k_tlb_init_pm(void)
551{
552 return cpu_pm_register_notifier(&r4k_tlb_pm_notifier_block);
553}
554arch_initcall(r4k_tlb_init_pm);