blob: d7a9d5f211f0f1a19bf55db114f28af6d67ade91 [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>
Paul Gortmakerd9ba5772016-08-21 15:58:14 -040017#include <linux/export.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>
Paul Burton091bc3a2015-07-13 17:12:44 +010022#include <asm/hazards.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070023#include <asm/mmu_context.h>
24#include <asm/pgtable.h>
Markos Chandrasc01905e2013-11-14 16:12:22 +000025#include <asm/tlb.h>
Ralf Baechle3d18c982011-11-28 16:11:28 +000026#include <asm/tlbmisc.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070027
28extern void build_tlb_refill_handler(void);
29
Fuxin Zhang2a21c732007-06-06 14:52:43 +080030/*
Huacai Chen06e48142016-03-03 09:45:11 +080031 * LOONGSON-2 has a 4 entry itlb which is a subset of jtlb, LOONGSON-3 has
32 * a 4 entry itlb and a 4 entry dtlb which are subsets of jtlb. Unfortunately,
33 * itlb/dtlb are not totally transparent to software.
Fuxin Zhang2a21c732007-06-06 14:52:43 +080034 */
Huacai Chen06e48142016-03-03 09:45:11 +080035static inline void flush_micro_tlb(void)
Ralf Baechle14bd8c02013-09-25 18:21:26 +020036{
37 switch (current_cpu_type()) {
Jiaxun Yang268a2d62019-10-20 22:43:13 +080038 case CPU_LOONGSON2EF:
Huacai Chen06e48142016-03-03 09:45:11 +080039 write_c0_diag(LOONGSON_DIAG_ITLB);
40 break;
Jiaxun Yang268a2d62019-10-20 22:43:13 +080041 case CPU_LOONGSON64:
Huacai Chen06e48142016-03-03 09:45:11 +080042 write_c0_diag(LOONGSON_DIAG_ITLB | LOONGSON_DIAG_DTLB);
Ralf Baechle14bd8c02013-09-25 18:21:26 +020043 break;
44 default:
45 break;
46 }
47}
Fuxin Zhang2a21c732007-06-06 14:52:43 +080048
Huacai Chen06e48142016-03-03 09:45:11 +080049static inline void flush_micro_tlb_vm(struct vm_area_struct *vma)
Ralf Baechle14bd8c02013-09-25 18:21:26 +020050{
51 if (vma->vm_flags & VM_EXEC)
Huacai Chen06e48142016-03-03 09:45:11 +080052 flush_micro_tlb();
Ralf Baechle14bd8c02013-09-25 18:21:26 +020053}
Fuxin Zhang2a21c732007-06-06 14:52:43 +080054
Linus Torvalds1da177e2005-04-16 15:20:36 -070055void local_flush_tlb_all(void)
56{
57 unsigned long flags;
58 unsigned long old_ctx;
Leonid Yegoshin75b5b5e2013-11-14 16:12:31 +000059 int entry, ftlbhighset;
Linus Torvalds1da177e2005-04-16 15:20:36 -070060
Ralf Baechleb6336482014-05-23 16:29:44 +020061 local_irq_save(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -070062 /* Save old context and create impossible VPN2 value */
63 old_ctx = read_c0_entryhi();
Markos Chandrasf1014d12014-07-14 12:47:09 +010064 htw_stop();
Linus Torvalds1da177e2005-04-16 15:20:36 -070065 write_c0_entrylo0(0);
66 write_c0_entrylo1(0);
67
Paul Burton10313982016-11-12 01:26:07 +000068 entry = num_wired_entries();
Linus Torvalds1da177e2005-04-16 15:20:36 -070069
Matt Redfearne710d662016-09-20 09:47:25 +010070 /*
71 * Blast 'em all away.
72 * If there are any wired entries, fall back to iterating
73 */
74 if (cpu_has_tlbinv && !entry) {
Leonid Yegoshin75b5b5e2013-11-14 16:12:31 +000075 if (current_cpu_data.tlbsizevtlb) {
76 write_c0_index(0);
77 mtc0_tlbw_hazard();
78 tlbinvf(); /* invalidate VTLB */
79 }
80 ftlbhighset = current_cpu_data.tlbsizevtlb +
81 current_cpu_data.tlbsizeftlbsets;
82 for (entry = current_cpu_data.tlbsizevtlb;
83 entry < ftlbhighset;
84 entry++) {
85 write_c0_index(entry);
86 mtc0_tlbw_hazard();
87 tlbinvf(); /* invalidate one FTLB set */
88 }
Leonid Yegoshin601cfa72013-11-14 16:12:30 +000089 } else {
90 while (entry < current_cpu_data.tlbsize) {
91 /* Make sure all entries differ. */
92 write_c0_entryhi(UNIQUE_ENTRYHI(entry));
93 write_c0_index(entry);
94 mtc0_tlbw_hazard();
95 tlb_write_indexed();
96 entry++;
97 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070098 }
99 tlbw_use_hazard();
100 write_c0_entryhi(old_ctx);
Markos Chandrasf1014d12014-07-14 12:47:09 +0100101 htw_start();
Huacai Chen06e48142016-03-03 09:45:11 +0800102 flush_micro_tlb();
Ralf Baechleb6336482014-05-23 16:29:44 +0200103 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104}
Sanjay Lalf2e36562012-11-21 18:34:10 -0800105EXPORT_SYMBOL(local_flush_tlb_all);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107void local_flush_tlb_range(struct vm_area_struct *vma, unsigned long start,
108 unsigned long end)
109{
110 struct mm_struct *mm = vma->vm_mm;
111 int cpu = smp_processor_id();
112
113 if (cpu_context(cpu, mm) != 0) {
Greg Ungerera5e696e2009-05-20 16:12:32 +1000114 unsigned long size, flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115
Ralf Baechleb6336482014-05-23 16:29:44 +0200116 local_irq_save(flags);
David Daneyac53c4f2012-12-03 12:44:26 -0800117 start = round_down(start, PAGE_SIZE << 1);
118 end = round_up(end, PAGE_SIZE << 1);
119 size = (end - start) >> (PAGE_SHIFT + 1);
Leonid Yegoshin75b5b5e2013-11-14 16:12:31 +0000120 if (size <= (current_cpu_data.tlbsizeftlbsets ?
121 current_cpu_data.tlbsize / 8 :
122 current_cpu_data.tlbsize / 2)) {
Paul Burtonc8790d62019-02-02 01:43:28 +0000123 unsigned long old_entryhi, uninitialized_var(old_mmid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124 int newpid = cpu_asid(cpu, mm);
125
Paul Burtonc8790d62019-02-02 01:43:28 +0000126 old_entryhi = read_c0_entryhi();
127 if (cpu_has_mmid) {
128 old_mmid = read_c0_memorymapid();
129 write_c0_memorymapid(newpid);
130 }
131
Markos Chandrasf1014d12014-07-14 12:47:09 +0100132 htw_stop();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133 while (start < end) {
134 int idx;
135
Paul Burtonc8790d62019-02-02 01:43:28 +0000136 if (cpu_has_mmid)
137 write_c0_entryhi(start);
138 else
139 write_c0_entryhi(start | newpid);
David Daneyac53c4f2012-12-03 12:44:26 -0800140 start += (PAGE_SIZE << 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141 mtc0_tlbw_hazard();
142 tlb_probe();
Ralf Baechle432bef22006-09-08 04:16:21 +0200143 tlb_probe_hazard();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144 idx = read_c0_index();
145 write_c0_entrylo0(0);
146 write_c0_entrylo1(0);
147 if (idx < 0)
148 continue;
149 /* Make sure all entries differ. */
Thiemo Seufer172546b2005-04-02 10:21:56 +0000150 write_c0_entryhi(UNIQUE_ENTRYHI(idx));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151 mtc0_tlbw_hazard();
152 tlb_write_indexed();
153 }
154 tlbw_use_hazard();
Paul Burtonc8790d62019-02-02 01:43:28 +0000155 write_c0_entryhi(old_entryhi);
156 if (cpu_has_mmid)
157 write_c0_memorymapid(old_mmid);
Markos Chandrasf1014d12014-07-14 12:47:09 +0100158 htw_start();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159 } else {
Paul Burton9a273242019-02-02 01:43:16 +0000160 drop_mmu_context(mm);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161 }
Huacai Chen06e48142016-03-03 09:45:11 +0800162 flush_micro_tlb();
Ralf Baechleb6336482014-05-23 16:29:44 +0200163 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164 }
165}
166
167void local_flush_tlb_kernel_range(unsigned long start, unsigned long end)
168{
Greg Ungerera5e696e2009-05-20 16:12:32 +1000169 unsigned long size, flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170
Ralf Baechleb6336482014-05-23 16:29:44 +0200171 local_irq_save(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172 size = (end - start + (PAGE_SIZE - 1)) >> PAGE_SHIFT;
173 size = (size + 1) >> 1;
Leonid Yegoshin75b5b5e2013-11-14 16:12:31 +0000174 if (size <= (current_cpu_data.tlbsizeftlbsets ?
175 current_cpu_data.tlbsize / 8 :
176 current_cpu_data.tlbsize / 2)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177 int pid = read_c0_entryhi();
178
179 start &= (PAGE_MASK << 1);
180 end += ((PAGE_SIZE << 1) - 1);
181 end &= (PAGE_MASK << 1);
Markos Chandrasf1014d12014-07-14 12:47:09 +0100182 htw_stop();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183
184 while (start < end) {
185 int idx;
186
187 write_c0_entryhi(start);
188 start += (PAGE_SIZE << 1);
189 mtc0_tlbw_hazard();
190 tlb_probe();
Ralf Baechle432bef22006-09-08 04:16:21 +0200191 tlb_probe_hazard();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192 idx = read_c0_index();
193 write_c0_entrylo0(0);
194 write_c0_entrylo1(0);
195 if (idx < 0)
196 continue;
197 /* Make sure all entries differ. */
Thiemo Seufer172546b2005-04-02 10:21:56 +0000198 write_c0_entryhi(UNIQUE_ENTRYHI(idx));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199 mtc0_tlbw_hazard();
200 tlb_write_indexed();
201 }
202 tlbw_use_hazard();
203 write_c0_entryhi(pid);
Markos Chandrasf1014d12014-07-14 12:47:09 +0100204 htw_start();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205 } else {
206 local_flush_tlb_all();
207 }
Huacai Chen06e48142016-03-03 09:45:11 +0800208 flush_micro_tlb();
Ralf Baechleb6336482014-05-23 16:29:44 +0200209 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210}
211
212void local_flush_tlb_page(struct vm_area_struct *vma, unsigned long page)
213{
214 int cpu = smp_processor_id();
215
216 if (cpu_context(cpu, vma->vm_mm) != 0) {
Paul Burtonc8790d62019-02-02 01:43:28 +0000217 unsigned long uninitialized_var(old_mmid);
218 unsigned long flags, old_entryhi;
219 int idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221 page &= (PAGE_MASK << 1);
Ralf Baechleb6336482014-05-23 16:29:44 +0200222 local_irq_save(flags);
Paul Burtonc8790d62019-02-02 01:43:28 +0000223 old_entryhi = read_c0_entryhi();
Markos Chandrasf1014d12014-07-14 12:47:09 +0100224 htw_stop();
Paul Burtonc8790d62019-02-02 01:43:28 +0000225 if (cpu_has_mmid) {
226 old_mmid = read_c0_memorymapid();
227 write_c0_entryhi(page);
228 write_c0_memorymapid(cpu_asid(cpu, vma->vm_mm));
229 } else {
230 write_c0_entryhi(page | cpu_asid(cpu, vma->vm_mm));
231 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232 mtc0_tlbw_hazard();
233 tlb_probe();
Ralf Baechle432bef22006-09-08 04:16:21 +0200234 tlb_probe_hazard();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235 idx = read_c0_index();
236 write_c0_entrylo0(0);
237 write_c0_entrylo1(0);
238 if (idx < 0)
239 goto finish;
240 /* Make sure all entries differ. */
Thiemo Seufer172546b2005-04-02 10:21:56 +0000241 write_c0_entryhi(UNIQUE_ENTRYHI(idx));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242 mtc0_tlbw_hazard();
243 tlb_write_indexed();
244 tlbw_use_hazard();
245
246 finish:
Paul Burtonc8790d62019-02-02 01:43:28 +0000247 write_c0_entryhi(old_entryhi);
248 if (cpu_has_mmid)
249 write_c0_memorymapid(old_mmid);
Markos Chandrasf1014d12014-07-14 12:47:09 +0100250 htw_start();
Huacai Chen06e48142016-03-03 09:45:11 +0800251 flush_micro_tlb_vm(vma);
Ralf Baechleb6336482014-05-23 16:29:44 +0200252 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253 }
254}
255
256/*
257 * This one is only used for pages with the global bit set so we don't care
258 * much about the ASID.
259 */
260void local_flush_tlb_one(unsigned long page)
261{
262 unsigned long flags;
263 int oldpid, idx;
264
Ralf Baechleb6336482014-05-23 16:29:44 +0200265 local_irq_save(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266 oldpid = read_c0_entryhi();
Markos Chandrasf1014d12014-07-14 12:47:09 +0100267 htw_stop();
Thiemo Seufer172546b2005-04-02 10:21:56 +0000268 page &= (PAGE_MASK << 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269 write_c0_entryhi(page);
270 mtc0_tlbw_hazard();
271 tlb_probe();
Ralf Baechle432bef22006-09-08 04:16:21 +0200272 tlb_probe_hazard();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273 idx = read_c0_index();
274 write_c0_entrylo0(0);
275 write_c0_entrylo1(0);
276 if (idx >= 0) {
277 /* Make sure all entries differ. */
Thiemo Seufer172546b2005-04-02 10:21:56 +0000278 write_c0_entryhi(UNIQUE_ENTRYHI(idx));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279 mtc0_tlbw_hazard();
280 tlb_write_indexed();
281 tlbw_use_hazard();
282 }
283 write_c0_entryhi(oldpid);
Markos Chandrasf1014d12014-07-14 12:47:09 +0100284 htw_start();
Huacai Chen06e48142016-03-03 09:45:11 +0800285 flush_micro_tlb();
Ralf Baechleb6336482014-05-23 16:29:44 +0200286 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287}
288
289/*
290 * We will need multiple versions of update_mmu_cache(), one that just
291 * updates the TLB with the new pte(s), and another which also checks
292 * for the R4k "end of page" hardware bug and does the needy.
293 */
294void __update_tlb(struct vm_area_struct * vma, unsigned long address, pte_t pte)
295{
296 unsigned long flags;
297 pgd_t *pgdp;
Mike Rapoport2bee1b52019-11-21 18:21:33 +0200298 p4d_t *p4dp;
Ralf Baechlec6e8b582005-02-10 12:19:59 +0000299 pud_t *pudp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300 pmd_t *pmdp;
301 pte_t *ptep;
302 int idx, pid;
303
304 /*
305 * Handle debugger faulting in for debugee.
306 */
307 if (current->active_mm != vma->vm_mm)
308 return;
309
Ralf Baechleb6336482014-05-23 16:29:44 +0200310 local_irq_save(flags);
Thiemo Seufer172546b2005-04-02 10:21:56 +0000311
Markos Chandras6a8dff62014-11-17 09:31:07 +0000312 htw_stop();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313 address &= (PAGE_MASK << 1);
Paul Burtonc8790d62019-02-02 01:43:28 +0000314 if (cpu_has_mmid) {
315 write_c0_entryhi(address);
316 } else {
317 pid = read_c0_entryhi() & cpu_asid_mask(&current_cpu_data);
318 write_c0_entryhi(address | pid);
319 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320 pgdp = pgd_offset(vma->vm_mm, address);
321 mtc0_tlbw_hazard();
322 tlb_probe();
Ralf Baechle432bef22006-09-08 04:16:21 +0200323 tlb_probe_hazard();
Mike Rapoport2bee1b52019-11-21 18:21:33 +0200324 p4dp = p4d_offset(pgdp, address);
325 pudp = pud_offset(p4dp, address);
Ralf Baechlec6e8b582005-02-10 12:19:59 +0000326 pmdp = pmd_offset(pudp, address);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327 idx = read_c0_index();
David Daneyaa1762f2012-10-17 00:48:10 +0200328#ifdef CONFIG_MIPS_HUGE_TLB_SUPPORT
David Daneyfd062c82009-05-27 17:47:44 -0700329 /* this could be a huge page */
330 if (pmd_huge(*pmdp)) {
331 unsigned long lo;
332 write_c0_pagemask(PM_HUGE_MASK);
333 ptep = (pte_t *)pmdp;
David Daney6dd93442010-02-10 15:12:47 -0800334 lo = pte_to_entrylo(pte_val(*ptep));
David Daneyfd062c82009-05-27 17:47:44 -0700335 write_c0_entrylo0(lo);
336 write_c0_entrylo1(lo + (HPAGE_SIZE >> 7));
337
338 mtc0_tlbw_hazard();
339 if (idx < 0)
340 tlb_write_random();
341 else
342 tlb_write_indexed();
Ralf Baechlefb944c92012-10-17 01:01:21 +0200343 tlbw_use_hazard();
David Daneyfd062c82009-05-27 17:47:44 -0700344 write_c0_pagemask(PM_DEFAULT_MASK);
345 } else
346#endif
347 {
348 ptep = pte_offset_map(pmdp, address);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349
Ralf Baechle34adb282014-11-22 00:16:48 +0100350#if defined(CONFIG_PHYS_ADDR_T_64BIT) && defined(CONFIG_CPU_MIPS32)
Steven J. Hillc5b36782015-02-26 18:16:38 -0600351#ifdef CONFIG_XPA
352 write_c0_entrylo0(pte_to_entrylo(ptep->pte_high));
James Hogan4b6f99d2016-04-19 09:25:10 +0100353 if (cpu_has_xpa)
354 writex_c0_entrylo0(ptep->pte_low & _PFNX_MASK);
Steven J. Hillc5b36782015-02-26 18:16:38 -0600355 ptep++;
356 write_c0_entrylo1(pte_to_entrylo(ptep->pte_high));
James Hogan4b6f99d2016-04-19 09:25:10 +0100357 if (cpu_has_xpa)
358 writex_c0_entrylo1(ptep->pte_low & _PFNX_MASK);
Steven J. Hillc5b36782015-02-26 18:16:38 -0600359#else
David Daneyfd062c82009-05-27 17:47:44 -0700360 write_c0_entrylo0(ptep->pte_high);
361 ptep++;
362 write_c0_entrylo1(ptep->pte_high);
Steven J. Hillc5b36782015-02-26 18:16:38 -0600363#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364#else
David Daney6dd93442010-02-10 15:12:47 -0800365 write_c0_entrylo0(pte_to_entrylo(pte_val(*ptep++)));
366 write_c0_entrylo1(pte_to_entrylo(pte_val(*ptep)));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367#endif
David Daneyfd062c82009-05-27 17:47:44 -0700368 mtc0_tlbw_hazard();
369 if (idx < 0)
370 tlb_write_random();
371 else
372 tlb_write_indexed();
373 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374 tlbw_use_hazard();
Markos Chandras6a8dff62014-11-17 09:31:07 +0000375 htw_start();
Huacai Chen06e48142016-03-03 09:45:11 +0800376 flush_micro_tlb_vm(vma);
Ralf Baechleb6336482014-05-23 16:29:44 +0200377 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378}
379
Manuel Lauss694b8c32011-08-02 19:51:08 +0200380void add_wired_entry(unsigned long entrylo0, unsigned long entrylo1,
381 unsigned long entryhi, unsigned long pagemask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382{
Steven J. Hillc5b36782015-02-26 18:16:38 -0600383#ifdef CONFIG_XPA
384 panic("Broken for XPA kernels");
385#else
Paul Burtonc8790d62019-02-02 01:43:28 +0000386 unsigned int uninitialized_var(old_mmid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387 unsigned long flags;
388 unsigned long wired;
389 unsigned long old_pagemask;
390 unsigned long old_ctx;
391
Ralf Baechleb6336482014-05-23 16:29:44 +0200392 local_irq_save(flags);
Paul Burtonc8790d62019-02-02 01:43:28 +0000393 if (cpu_has_mmid) {
394 old_mmid = read_c0_memorymapid();
395 write_c0_memorymapid(MMID_KERNEL_WIRED);
396 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397 /* Save old context and create impossible VPN2 value */
398 old_ctx = read_c0_entryhi();
Markos Chandrasf1014d12014-07-14 12:47:09 +0100399 htw_stop();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400 old_pagemask = read_c0_pagemask();
Paul Burton10313982016-11-12 01:26:07 +0000401 wired = num_wired_entries();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402 write_c0_wired(wired + 1);
403 write_c0_index(wired);
Ralf Baechle432bef22006-09-08 04:16:21 +0200404 tlbw_use_hazard(); /* What is the hazard here? */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405 write_c0_pagemask(pagemask);
406 write_c0_entryhi(entryhi);
407 write_c0_entrylo0(entrylo0);
408 write_c0_entrylo1(entrylo1);
409 mtc0_tlbw_hazard();
410 tlb_write_indexed();
411 tlbw_use_hazard();
412
413 write_c0_entryhi(old_ctx);
Paul Burtonc8790d62019-02-02 01:43:28 +0000414 if (cpu_has_mmid)
415 write_c0_memorymapid(old_mmid);
Ralf Baechle432bef22006-09-08 04:16:21 +0200416 tlbw_use_hazard(); /* What is the hazard here? */
Markos Chandrasf1014d12014-07-14 12:47:09 +0100417 htw_start();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418 write_c0_pagemask(old_pagemask);
419 local_flush_tlb_all();
Ralf Baechleb6336482014-05-23 16:29:44 +0200420 local_irq_restore(flags);
Steven J. Hillc5b36782015-02-26 18:16:38 -0600421#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422}
423
Ralf Baechle970d0322012-10-18 13:54:15 +0200424#ifdef CONFIG_TRANSPARENT_HUGEPAGE
425
Hugh Dickinsfd8cfd32016-05-19 17:13:00 -0700426int has_transparent_hugepage(void)
Ralf Baechle970d0322012-10-18 13:54:15 +0200427{
Hugh Dickinsfd8cfd32016-05-19 17:13:00 -0700428 static unsigned int mask = -1;
Ralf Baechle970d0322012-10-18 13:54:15 +0200429
Hugh Dickinsfd8cfd32016-05-19 17:13:00 -0700430 if (mask == -1) { /* first call comes during __init */
431 unsigned long flags;
Ralf Baechle970d0322012-10-18 13:54:15 +0200432
Hugh Dickinsfd8cfd32016-05-19 17:13:00 -0700433 local_irq_save(flags);
434 write_c0_pagemask(PM_HUGE_MASK);
435 back_to_back_c0_hazard();
436 mask = read_c0_pagemask();
437 write_c0_pagemask(PM_DEFAULT_MASK);
438 local_irq_restore(flags);
439 }
Ralf Baechle970d0322012-10-18 13:54:15 +0200440 return mask == PM_HUGE_MASK;
441}
442
443#endif /* CONFIG_TRANSPARENT_HUGEPAGE */
444
Rafał Miłeckid3777322014-07-17 23:26:32 +0200445/*
446 * Used for loading TLB entries before trap_init() has started, when we
447 * don't actually want to add a wired entry which remains throughout the
448 * lifetime of the system
449 */
450
Paul Gortmakerb1f7e112015-04-27 18:47:56 -0400451int temp_tlb_entry;
Rafał Miłeckid3777322014-07-17 23:26:32 +0200452
453__init int add_temporary_entry(unsigned long entrylo0, unsigned long entrylo1,
454 unsigned long entryhi, unsigned long pagemask)
455{
456 int ret = 0;
457 unsigned long flags;
458 unsigned long wired;
459 unsigned long old_pagemask;
460 unsigned long old_ctx;
461
462 local_irq_save(flags);
463 /* Save old context and create impossible VPN2 value */
Markos Chandras6a8dff62014-11-17 09:31:07 +0000464 htw_stop();
Rafał Miłeckid3777322014-07-17 23:26:32 +0200465 old_ctx = read_c0_entryhi();
466 old_pagemask = read_c0_pagemask();
Paul Burton10313982016-11-12 01:26:07 +0000467 wired = num_wired_entries();
Rafał Miłeckid3777322014-07-17 23:26:32 +0200468 if (--temp_tlb_entry < wired) {
469 printk(KERN_WARNING
470 "No TLB space left for add_temporary_entry\n");
471 ret = -ENOSPC;
472 goto out;
473 }
474
475 write_c0_index(temp_tlb_entry);
476 write_c0_pagemask(pagemask);
477 write_c0_entryhi(entryhi);
478 write_c0_entrylo0(entrylo0);
479 write_c0_entrylo1(entrylo1);
480 mtc0_tlbw_hazard();
481 tlb_write_indexed();
482 tlbw_use_hazard();
483
484 write_c0_entryhi(old_ctx);
485 write_c0_pagemask(old_pagemask);
Markos Chandras6a8dff62014-11-17 09:31:07 +0000486 htw_start();
Rafał Miłeckid3777322014-07-17 23:26:32 +0200487out:
488 local_irq_restore(flags);
489 return ret;
490}
491
Paul Gortmaker078a55f2013-06-18 13:38:59 +0000492static int ntlb;
Ralf Baechle41c594a2006-04-05 09:45:45 +0100493static int __init set_ntlb(char *str)
494{
495 get_option(&str, &ntlb);
496 return 1;
497}
498
499__setup("ntlb=", set_ntlb);
500
James Hoganeaa38d62014-02-28 17:09:20 +0000501/*
502 * Configure TLB (for init or after a CPU has been powered off).
503 */
504static void r4k_tlb_configure(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506 /*
507 * You should never change this register:
508 * - On R4600 1.7 the tlbp never hits for pages smaller than
509 * the value in the c0_pagemask register.
510 * - The entire mm handling assumes the c0_pagemask register to
Thiemo Seufera7c29962008-02-29 00:43:47 +0000511 * be set to fixed-size pages.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513 write_c0_pagemask(PM_DEFAULT_MASK);
Paul Burton091bc3a2015-07-13 17:12:44 +0100514 back_to_back_c0_hazard();
515 if (read_c0_pagemask() != PM_DEFAULT_MASK)
516 panic("MMU doesn't support PAGE_SIZE=0x%lx", PAGE_SIZE);
517
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518 write_c0_wired(0);
Ralf Baechlecde15b52009-01-06 23:07:20 +0000519 if (current_cpu_type() == CPU_R10000 ||
520 current_cpu_type() == CPU_R12000 ||
Joshua Kinard30577392015-01-21 07:59:45 -0500521 current_cpu_type() == CPU_R14000 ||
522 current_cpu_type() == CPU_R16000)
Ralf Baechlecde15b52009-01-06 23:07:20 +0000523 write_c0_framemask(0);
David Daney6dd93442010-02-10 15:12:47 -0800524
Steven J. Hill05857c62012-09-13 16:51:46 -0500525 if (cpu_has_rixi) {
David Daney6dd93442010-02-10 15:12:47 -0800526 /*
James Hogane05cb562015-05-13 11:50:55 +0100527 * Enable the no read, no exec bits, and enable large physical
David Daney6dd93442010-02-10 15:12:47 -0800528 * address.
529 */
David Daney6dd93442010-02-10 15:12:47 -0800530#ifdef CONFIG_64BIT
Steven J. Hilla5770df2015-02-19 10:18:52 -0600531 set_c0_pagegrain(PG_RIE | PG_XIE | PG_ELPA);
532#else
533 set_c0_pagegrain(PG_RIE | PG_XIE);
David Daney6dd93442010-02-10 15:12:47 -0800534#endif
David Daney6dd93442010-02-10 15:12:47 -0800535 }
536
Rafał Miłeckid3777322014-07-17 23:26:32 +0200537 temp_tlb_entry = current_cpu_data.tlbsize - 1;
538
Ralf Baechle70342282013-01-22 12:59:30 +0100539 /* From this point on the ARC firmware is dead. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540 local_flush_tlb_all();
541
Thiemo Seuferc6281ed2006-03-14 14:35:27 +0000542 /* Did I tell you that ARC SUCKS? */
James Hoganeaa38d62014-02-28 17:09:20 +0000543}
544
545void tlb_init(void)
546{
547 r4k_tlb_configure();
Thiemo Seuferc6281ed2006-03-14 14:35:27 +0000548
Ralf Baechle41c594a2006-04-05 09:45:45 +0100549 if (ntlb) {
550 if (ntlb > 1 && ntlb <= current_cpu_data.tlbsize) {
551 int wired = current_cpu_data.tlbsize - ntlb;
552 write_c0_wired(wired);
553 write_c0_index(wired-1);
Ralf Baechle49a89ef2007-10-11 23:46:15 +0100554 printk("Restricting TLB to %d entries\n", ntlb);
Ralf Baechle41c594a2006-04-05 09:45:45 +0100555 } else
556 printk("Ignoring invalid argument ntlb=%d\n", ntlb);
557 }
558
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559 build_tlb_refill_handler();
560}
James Hoganeaa38d62014-02-28 17:09:20 +0000561
562static int r4k_tlb_pm_notifier(struct notifier_block *self, unsigned long cmd,
563 void *v)
564{
565 switch (cmd) {
566 case CPU_PM_ENTER_FAILED:
567 case CPU_PM_EXIT:
568 r4k_tlb_configure();
569 break;
570 }
571
572 return NOTIFY_OK;
573}
574
575static struct notifier_block r4k_tlb_pm_notifier_block = {
576 .notifier_call = r4k_tlb_pm_notifier,
577};
578
579static int __init r4k_tlb_init_pm(void)
580{
581 return cpu_pm_register_notifier(&r4k_tlb_pm_notifier_block);
582}
583arch_initcall(r4k_tlb_init_pm);