Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 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. Mattock | 79add62 | 2011-04-04 14:15:29 -0700 | [diff] [blame] | 6 | * Copyright (C) 1996 David S. Miller (davem@davemloft.net) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7 | * 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 Hogan | eaa38d6 | 2014-02-28 17:09:20 +0000 | [diff] [blame] | 11 | #include <linux/cpu_pm.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 12 | #include <linux/init.h> |
| 13 | #include <linux/sched.h> |
Ralf Baechle | 631330f | 2009-06-19 14:05:26 +0100 | [diff] [blame] | 14 | #include <linux/smp.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 15 | #include <linux/mm.h> |
David Daney | fd062c8 | 2009-05-27 17:47:44 -0700 | [diff] [blame] | 16 | #include <linux/hugetlb.h> |
Sanjay Lal | f2e3656 | 2012-11-21 18:34:10 -0800 | [diff] [blame] | 17 | #include <linux/module.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 18 | |
| 19 | #include <asm/cpu.h> |
Ralf Baechle | 69f24d1 | 2013-09-17 10:25:47 +0200 | [diff] [blame] | 20 | #include <asm/cpu-type.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 21 | #include <asm/bootinfo.h> |
| 22 | #include <asm/mmu_context.h> |
| 23 | #include <asm/pgtable.h> |
Markos Chandras | c01905e | 2013-11-14 16:12:22 +0000 | [diff] [blame] | 24 | #include <asm/tlb.h> |
Ralf Baechle | 3d18c98 | 2011-11-28 16:11:28 +0000 | [diff] [blame] | 25 | #include <asm/tlbmisc.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 26 | |
| 27 | extern void build_tlb_refill_handler(void); |
| 28 | |
Fuxin Zhang | 2a21c73 | 2007-06-06 14:52:43 +0800 | [diff] [blame] | 29 | /* |
Huacai Chen | c579d31 | 2014-03-21 18:44:00 +0800 | [diff] [blame] | 30 | * LOONGSON2/3 has a 4 entry itlb which is a subset of dtlb, |
| 31 | * unfortunately, itlb is not totally transparent to software. |
Fuxin Zhang | 2a21c73 | 2007-06-06 14:52:43 +0800 | [diff] [blame] | 32 | */ |
Ralf Baechle | 14bd8c0 | 2013-09-25 18:21:26 +0200 | [diff] [blame] | 33 | static inline void flush_itlb(void) |
| 34 | { |
| 35 | switch (current_cpu_type()) { |
| 36 | case CPU_LOONGSON2: |
Huacai Chen | c579d31 | 2014-03-21 18:44:00 +0800 | [diff] [blame] | 37 | case CPU_LOONGSON3: |
Ralf Baechle | 14bd8c0 | 2013-09-25 18:21:26 +0200 | [diff] [blame] | 38 | write_c0_diag(4); |
| 39 | break; |
| 40 | default: |
| 41 | break; |
| 42 | } |
| 43 | } |
Fuxin Zhang | 2a21c73 | 2007-06-06 14:52:43 +0800 | [diff] [blame] | 44 | |
Ralf Baechle | 14bd8c0 | 2013-09-25 18:21:26 +0200 | [diff] [blame] | 45 | static inline void flush_itlb_vm(struct vm_area_struct *vma) |
| 46 | { |
| 47 | if (vma->vm_flags & VM_EXEC) |
| 48 | flush_itlb(); |
| 49 | } |
Fuxin Zhang | 2a21c73 | 2007-06-06 14:52:43 +0800 | [diff] [blame] | 50 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 51 | void local_flush_tlb_all(void) |
| 52 | { |
| 53 | unsigned long flags; |
| 54 | unsigned long old_ctx; |
Leonid Yegoshin | 75b5b5e | 2013-11-14 16:12:31 +0000 | [diff] [blame] | 55 | int entry, ftlbhighset; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 56 | |
Ralf Baechle | b633648c5 | 2014-05-23 16:29:44 +0200 | [diff] [blame] | 57 | local_irq_save(flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 58 | /* Save old context and create impossible VPN2 value */ |
| 59 | old_ctx = read_c0_entryhi(); |
Markos Chandras | f1014d1 | 2014-07-14 12:47:09 +0100 | [diff] [blame] | 60 | htw_stop(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 61 | write_c0_entrylo0(0); |
| 62 | write_c0_entrylo1(0); |
| 63 | |
| 64 | entry = read_c0_wired(); |
| 65 | |
| 66 | /* Blast 'em all away. */ |
Leonid Yegoshin | 75b5b5e | 2013-11-14 16:12:31 +0000 | [diff] [blame] | 67 | 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 Yegoshin | 601cfa7 | 2013-11-14 16:12:30 +0000 | [diff] [blame] | 82 | } 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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 91 | } |
| 92 | tlbw_use_hazard(); |
| 93 | write_c0_entryhi(old_ctx); |
Markos Chandras | f1014d1 | 2014-07-14 12:47:09 +0100 | [diff] [blame] | 94 | htw_start(); |
Ralf Baechle | 14bd8c0 | 2013-09-25 18:21:26 +0200 | [diff] [blame] | 95 | flush_itlb(); |
Ralf Baechle | b633648c5 | 2014-05-23 16:29:44 +0200 | [diff] [blame] | 96 | local_irq_restore(flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 97 | } |
Sanjay Lal | f2e3656 | 2012-11-21 18:34:10 -0800 | [diff] [blame] | 98 | EXPORT_SYMBOL(local_flush_tlb_all); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 99 | |
Thiemo Seufer | 172546b | 2005-04-02 10:21:56 +0000 | [diff] [blame] | 100 | /* All entries common to a mm share an asid. To effectively flush |
| 101 | these entries, we just bump the asid. */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 102 | void local_flush_tlb_mm(struct mm_struct *mm) |
| 103 | { |
Thiemo Seufer | 172546b | 2005-04-02 10:21:56 +0000 | [diff] [blame] | 104 | int cpu; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 105 | |
Thiemo Seufer | 172546b | 2005-04-02 10:21:56 +0000 | [diff] [blame] | 106 | 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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 115 | } |
| 116 | |
| 117 | void 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 Ungerer | a5e696e | 2009-05-20 16:12:32 +1000 | [diff] [blame] | 124 | unsigned long size, flags; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 125 | |
Ralf Baechle | b633648c5 | 2014-05-23 16:29:44 +0200 | [diff] [blame] | 126 | local_irq_save(flags); |
David Daney | ac53c4f | 2012-12-03 12:44:26 -0800 | [diff] [blame] | 127 | start = round_down(start, PAGE_SIZE << 1); |
| 128 | end = round_up(end, PAGE_SIZE << 1); |
| 129 | size = (end - start) >> (PAGE_SHIFT + 1); |
Leonid Yegoshin | 75b5b5e | 2013-11-14 16:12:31 +0000 | [diff] [blame] | 130 | if (size <= (current_cpu_data.tlbsizeftlbsets ? |
| 131 | current_cpu_data.tlbsize / 8 : |
| 132 | current_cpu_data.tlbsize / 2)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 133 | int oldpid = read_c0_entryhi(); |
| 134 | int newpid = cpu_asid(cpu, mm); |
| 135 | |
Markos Chandras | f1014d1 | 2014-07-14 12:47:09 +0100 | [diff] [blame] | 136 | htw_stop(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 137 | while (start < end) { |
| 138 | int idx; |
| 139 | |
| 140 | write_c0_entryhi(start | newpid); |
David Daney | ac53c4f | 2012-12-03 12:44:26 -0800 | [diff] [blame] | 141 | start += (PAGE_SIZE << 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 142 | mtc0_tlbw_hazard(); |
| 143 | tlb_probe(); |
Ralf Baechle | 432bef2 | 2006-09-08 04:16:21 +0200 | [diff] [blame] | 144 | tlb_probe_hazard(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 145 | 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 Seufer | 172546b | 2005-04-02 10:21:56 +0000 | [diff] [blame] | 151 | write_c0_entryhi(UNIQUE_ENTRYHI(idx)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 152 | mtc0_tlbw_hazard(); |
| 153 | tlb_write_indexed(); |
| 154 | } |
| 155 | tlbw_use_hazard(); |
| 156 | write_c0_entryhi(oldpid); |
Markos Chandras | f1014d1 | 2014-07-14 12:47:09 +0100 | [diff] [blame] | 157 | htw_start(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 158 | } else { |
| 159 | drop_mmu_context(mm, cpu); |
| 160 | } |
Ralf Baechle | 14bd8c0 | 2013-09-25 18:21:26 +0200 | [diff] [blame] | 161 | flush_itlb(); |
Ralf Baechle | b633648c5 | 2014-05-23 16:29:44 +0200 | [diff] [blame] | 162 | local_irq_restore(flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 163 | } |
| 164 | } |
| 165 | |
| 166 | void local_flush_tlb_kernel_range(unsigned long start, unsigned long end) |
| 167 | { |
Greg Ungerer | a5e696e | 2009-05-20 16:12:32 +1000 | [diff] [blame] | 168 | unsigned long size, flags; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 169 | |
Ralf Baechle | b633648c5 | 2014-05-23 16:29:44 +0200 | [diff] [blame] | 170 | local_irq_save(flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 171 | size = (end - start + (PAGE_SIZE - 1)) >> PAGE_SHIFT; |
| 172 | size = (size + 1) >> 1; |
Leonid Yegoshin | 75b5b5e | 2013-11-14 16:12:31 +0000 | [diff] [blame] | 173 | if (size <= (current_cpu_data.tlbsizeftlbsets ? |
| 174 | current_cpu_data.tlbsize / 8 : |
| 175 | current_cpu_data.tlbsize / 2)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 176 | int pid = read_c0_entryhi(); |
| 177 | |
| 178 | start &= (PAGE_MASK << 1); |
| 179 | end += ((PAGE_SIZE << 1) - 1); |
| 180 | end &= (PAGE_MASK << 1); |
Markos Chandras | f1014d1 | 2014-07-14 12:47:09 +0100 | [diff] [blame] | 181 | htw_stop(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 182 | |
| 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 Baechle | 432bef2 | 2006-09-08 04:16:21 +0200 | [diff] [blame] | 190 | tlb_probe_hazard(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 191 | 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 Seufer | 172546b | 2005-04-02 10:21:56 +0000 | [diff] [blame] | 197 | write_c0_entryhi(UNIQUE_ENTRYHI(idx)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 198 | mtc0_tlbw_hazard(); |
| 199 | tlb_write_indexed(); |
| 200 | } |
| 201 | tlbw_use_hazard(); |
| 202 | write_c0_entryhi(pid); |
Markos Chandras | f1014d1 | 2014-07-14 12:47:09 +0100 | [diff] [blame] | 203 | htw_start(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 204 | } else { |
| 205 | local_flush_tlb_all(); |
| 206 | } |
Ralf Baechle | 14bd8c0 | 2013-09-25 18:21:26 +0200 | [diff] [blame] | 207 | flush_itlb(); |
Ralf Baechle | b633648c5 | 2014-05-23 16:29:44 +0200 | [diff] [blame] | 208 | local_irq_restore(flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 209 | } |
| 210 | |
| 211 | void 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 Baechle | b633648c5 | 2014-05-23 16:29:44 +0200 | [diff] [blame] | 221 | local_irq_save(flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 222 | oldpid = read_c0_entryhi(); |
Markos Chandras | f1014d1 | 2014-07-14 12:47:09 +0100 | [diff] [blame] | 223 | htw_stop(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 224 | write_c0_entryhi(page | newpid); |
| 225 | mtc0_tlbw_hazard(); |
| 226 | tlb_probe(); |
Ralf Baechle | 432bef2 | 2006-09-08 04:16:21 +0200 | [diff] [blame] | 227 | tlb_probe_hazard(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 228 | 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 Seufer | 172546b | 2005-04-02 10:21:56 +0000 | [diff] [blame] | 234 | write_c0_entryhi(UNIQUE_ENTRYHI(idx)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 235 | mtc0_tlbw_hazard(); |
| 236 | tlb_write_indexed(); |
| 237 | tlbw_use_hazard(); |
| 238 | |
| 239 | finish: |
| 240 | write_c0_entryhi(oldpid); |
Markos Chandras | f1014d1 | 2014-07-14 12:47:09 +0100 | [diff] [blame] | 241 | htw_start(); |
Ralf Baechle | 14bd8c0 | 2013-09-25 18:21:26 +0200 | [diff] [blame] | 242 | flush_itlb_vm(vma); |
Ralf Baechle | b633648c5 | 2014-05-23 16:29:44 +0200 | [diff] [blame] | 243 | local_irq_restore(flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 244 | } |
| 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 | */ |
| 251 | void local_flush_tlb_one(unsigned long page) |
| 252 | { |
| 253 | unsigned long flags; |
| 254 | int oldpid, idx; |
| 255 | |
Ralf Baechle | b633648c5 | 2014-05-23 16:29:44 +0200 | [diff] [blame] | 256 | local_irq_save(flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 257 | oldpid = read_c0_entryhi(); |
Markos Chandras | f1014d1 | 2014-07-14 12:47:09 +0100 | [diff] [blame] | 258 | htw_stop(); |
Thiemo Seufer | 172546b | 2005-04-02 10:21:56 +0000 | [diff] [blame] | 259 | page &= (PAGE_MASK << 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 260 | write_c0_entryhi(page); |
| 261 | mtc0_tlbw_hazard(); |
| 262 | tlb_probe(); |
Ralf Baechle | 432bef2 | 2006-09-08 04:16:21 +0200 | [diff] [blame] | 263 | tlb_probe_hazard(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 264 | 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 Seufer | 172546b | 2005-04-02 10:21:56 +0000 | [diff] [blame] | 269 | write_c0_entryhi(UNIQUE_ENTRYHI(idx)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 270 | mtc0_tlbw_hazard(); |
| 271 | tlb_write_indexed(); |
| 272 | tlbw_use_hazard(); |
| 273 | } |
| 274 | write_c0_entryhi(oldpid); |
Markos Chandras | f1014d1 | 2014-07-14 12:47:09 +0100 | [diff] [blame] | 275 | htw_start(); |
Ralf Baechle | 14bd8c0 | 2013-09-25 18:21:26 +0200 | [diff] [blame] | 276 | flush_itlb(); |
Ralf Baechle | b633648c5 | 2014-05-23 16:29:44 +0200 | [diff] [blame] | 277 | local_irq_restore(flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 278 | } |
| 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 | */ |
| 285 | void __update_tlb(struct vm_area_struct * vma, unsigned long address, pte_t pte) |
| 286 | { |
| 287 | unsigned long flags; |
| 288 | pgd_t *pgdp; |
Ralf Baechle | c6e8b58 | 2005-02-10 12:19:59 +0000 | [diff] [blame] | 289 | pud_t *pudp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 290 | 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 Baechle | b633648c5 | 2014-05-23 16:29:44 +0200 | [diff] [blame] | 300 | local_irq_save(flags); |
Thiemo Seufer | 172546b | 2005-04-02 10:21:56 +0000 | [diff] [blame] | 301 | |
Markos Chandras | 6a8dff6 | 2014-11-17 09:31:07 +0000 | [diff] [blame] | 302 | htw_stop(); |
David Daney | 48c4ac9 | 2013-05-13 13:56:44 -0700 | [diff] [blame] | 303 | pid = read_c0_entryhi() & ASID_MASK; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 304 | 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 Baechle | 432bef2 | 2006-09-08 04:16:21 +0200 | [diff] [blame] | 309 | tlb_probe_hazard(); |
Ralf Baechle | c6e8b58 | 2005-02-10 12:19:59 +0000 | [diff] [blame] | 310 | pudp = pud_offset(pgdp, address); |
| 311 | pmdp = pmd_offset(pudp, address); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 312 | idx = read_c0_index(); |
David Daney | aa1762f | 2012-10-17 00:48:10 +0200 | [diff] [blame] | 313 | #ifdef CONFIG_MIPS_HUGE_TLB_SUPPORT |
David Daney | fd062c8 | 2009-05-27 17:47:44 -0700 | [diff] [blame] | 314 | /* 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 Daney | 6dd9344 | 2010-02-10 15:12:47 -0800 | [diff] [blame] | 319 | lo = pte_to_entrylo(pte_val(*ptep)); |
David Daney | fd062c8 | 2009-05-27 17:47:44 -0700 | [diff] [blame] | 320 | 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 Baechle | fb944c9 | 2012-10-17 01:01:21 +0200 | [diff] [blame] | 328 | tlbw_use_hazard(); |
David Daney | fd062c8 | 2009-05-27 17:47:44 -0700 | [diff] [blame] | 329 | write_c0_pagemask(PM_DEFAULT_MASK); |
| 330 | } else |
| 331 | #endif |
| 332 | { |
| 333 | ptep = pte_offset_map(pmdp, address); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 334 | |
Ralf Baechle | 34adb28 | 2014-11-22 00:16:48 +0100 | [diff] [blame] | 335 | #if defined(CONFIG_PHYS_ADDR_T_64BIT) && defined(CONFIG_CPU_MIPS32) |
David Daney | fd062c8 | 2009-05-27 17:47:44 -0700 | [diff] [blame] | 336 | write_c0_entrylo0(ptep->pte_high); |
| 337 | ptep++; |
| 338 | write_c0_entrylo1(ptep->pte_high); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 339 | #else |
David Daney | 6dd9344 | 2010-02-10 15:12:47 -0800 | [diff] [blame] | 340 | write_c0_entrylo0(pte_to_entrylo(pte_val(*ptep++))); |
| 341 | write_c0_entrylo1(pte_to_entrylo(pte_val(*ptep))); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 342 | #endif |
David Daney | fd062c8 | 2009-05-27 17:47:44 -0700 | [diff] [blame] | 343 | mtc0_tlbw_hazard(); |
| 344 | if (idx < 0) |
| 345 | tlb_write_random(); |
| 346 | else |
| 347 | tlb_write_indexed(); |
| 348 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 349 | tlbw_use_hazard(); |
Markos Chandras | 6a8dff6 | 2014-11-17 09:31:07 +0000 | [diff] [blame] | 350 | htw_start(); |
Ralf Baechle | 14bd8c0 | 2013-09-25 18:21:26 +0200 | [diff] [blame] | 351 | flush_itlb_vm(vma); |
Ralf Baechle | b633648c5 | 2014-05-23 16:29:44 +0200 | [diff] [blame] | 352 | local_irq_restore(flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 353 | } |
| 354 | |
Manuel Lauss | 694b8c3 | 2011-08-02 19:51:08 +0200 | [diff] [blame] | 355 | void add_wired_entry(unsigned long entrylo0, unsigned long entrylo1, |
| 356 | unsigned long entryhi, unsigned long pagemask) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 357 | { |
| 358 | unsigned long flags; |
| 359 | unsigned long wired; |
| 360 | unsigned long old_pagemask; |
| 361 | unsigned long old_ctx; |
| 362 | |
Ralf Baechle | b633648c5 | 2014-05-23 16:29:44 +0200 | [diff] [blame] | 363 | local_irq_save(flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 364 | /* Save old context and create impossible VPN2 value */ |
| 365 | old_ctx = read_c0_entryhi(); |
Markos Chandras | f1014d1 | 2014-07-14 12:47:09 +0100 | [diff] [blame] | 366 | htw_stop(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 367 | old_pagemask = read_c0_pagemask(); |
| 368 | wired = read_c0_wired(); |
| 369 | write_c0_wired(wired + 1); |
| 370 | write_c0_index(wired); |
Ralf Baechle | 432bef2 | 2006-09-08 04:16:21 +0200 | [diff] [blame] | 371 | tlbw_use_hazard(); /* What is the hazard here? */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 372 | 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 Baechle | 432bef2 | 2006-09-08 04:16:21 +0200 | [diff] [blame] | 381 | tlbw_use_hazard(); /* What is the hazard here? */ |
Markos Chandras | f1014d1 | 2014-07-14 12:47:09 +0100 | [diff] [blame] | 382 | htw_start(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 383 | write_c0_pagemask(old_pagemask); |
| 384 | local_flush_tlb_all(); |
Ralf Baechle | b633648c5 | 2014-05-23 16:29:44 +0200 | [diff] [blame] | 385 | local_irq_restore(flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 386 | } |
| 387 | |
Ralf Baechle | 970d032 | 2012-10-18 13:54:15 +0200 | [diff] [blame] | 388 | #ifdef CONFIG_TRANSPARENT_HUGEPAGE |
| 389 | |
| 390 | int __init has_transparent_hugepage(void) |
| 391 | { |
| 392 | unsigned int mask; |
| 393 | unsigned long flags; |
| 394 | |
Ralf Baechle | b633648c5 | 2014-05-23 16:29:44 +0200 | [diff] [blame] | 395 | local_irq_save(flags); |
Ralf Baechle | 970d032 | 2012-10-18 13:54:15 +0200 | [diff] [blame] | 396 | 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 Baechle | b633648c5 | 2014-05-23 16:29:44 +0200 | [diff] [blame] | 401 | local_irq_restore(flags); |
Ralf Baechle | 970d032 | 2012-10-18 13:54:15 +0200 | [diff] [blame] | 402 | |
| 403 | return mask == PM_HUGE_MASK; |
| 404 | } |
| 405 | |
| 406 | #endif /* CONFIG_TRANSPARENT_HUGEPAGE */ |
| 407 | |
Rafał Miłecki | d377732 | 2014-07-17 23:26:32 +0200 | [diff] [blame] | 408 | /* |
| 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łecki | 6ee1d93 | 2014-07-17 23:26:33 +0200 | [diff] [blame] | 414 | int temp_tlb_entry __cpuinitdata; |
Rafał Miłecki | d377732 | 2014-07-17 23:26:32 +0200 | [diff] [blame] | 415 | |
| 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 Chandras | 6a8dff6 | 2014-11-17 09:31:07 +0000 | [diff] [blame] | 427 | htw_stop(); |
Rafał Miłecki | d377732 | 2014-07-17 23:26:32 +0200 | [diff] [blame] | 428 | 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 Chandras | 6a8dff6 | 2014-11-17 09:31:07 +0000 | [diff] [blame] | 449 | htw_start(); |
Rafał Miłecki | d377732 | 2014-07-17 23:26:32 +0200 | [diff] [blame] | 450 | out: |
| 451 | local_irq_restore(flags); |
| 452 | return ret; |
| 453 | } |
| 454 | |
Paul Gortmaker | 078a55f | 2013-06-18 13:38:59 +0000 | [diff] [blame] | 455 | static int ntlb; |
Ralf Baechle | 41c594a | 2006-04-05 09:45:45 +0100 | [diff] [blame] | 456 | static int __init set_ntlb(char *str) |
| 457 | { |
| 458 | get_option(&str, &ntlb); |
| 459 | return 1; |
| 460 | } |
| 461 | |
| 462 | __setup("ntlb=", set_ntlb); |
| 463 | |
James Hogan | eaa38d6 | 2014-02-28 17:09:20 +0000 | [diff] [blame] | 464 | /* |
| 465 | * Configure TLB (for init or after a CPU has been powered off). |
| 466 | */ |
| 467 | static void r4k_tlb_configure(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 468 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 469 | /* |
| 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 Seufer | a7c2996 | 2008-02-29 00:43:47 +0000 | [diff] [blame] | 474 | * be set to fixed-size pages. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 475 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 476 | write_c0_pagemask(PM_DEFAULT_MASK); |
| 477 | write_c0_wired(0); |
Ralf Baechle | cde15b5 | 2009-01-06 23:07:20 +0000 | [diff] [blame] | 478 | if (current_cpu_type() == CPU_R10000 || |
| 479 | current_cpu_type() == CPU_R12000 || |
| 480 | current_cpu_type() == CPU_R14000) |
| 481 | write_c0_framemask(0); |
David Daney | 6dd9344 | 2010-02-10 15:12:47 -0800 | [diff] [blame] | 482 | |
Steven J. Hill | 05857c6 | 2012-09-13 16:51:46 -0500 | [diff] [blame] | 483 | if (cpu_has_rixi) { |
David Daney | 6dd9344 | 2010-02-10 15:12:47 -0800 | [diff] [blame] | 484 | /* |
| 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 |
David Daney | 9ead863 | 2015-01-06 10:42:23 -0800 | [diff] [blame] | 492 | if (cpu_has_rixiex) |
| 493 | pg |= PG_IEC; |
David Daney | 6dd9344 | 2010-02-10 15:12:47 -0800 | [diff] [blame] | 494 | write_c0_pagegrain(pg); |
| 495 | } |
| 496 | |
Rafał Miłecki | d377732 | 2014-07-17 23:26:32 +0200 | [diff] [blame] | 497 | temp_tlb_entry = current_cpu_data.tlbsize - 1; |
| 498 | |
Ralf Baechle | 7034228 | 2013-01-22 12:59:30 +0100 | [diff] [blame] | 499 | /* From this point on the ARC firmware is dead. */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 500 | local_flush_tlb_all(); |
| 501 | |
Thiemo Seufer | c6281ed | 2006-03-14 14:35:27 +0000 | [diff] [blame] | 502 | /* Did I tell you that ARC SUCKS? */ |
James Hogan | eaa38d6 | 2014-02-28 17:09:20 +0000 | [diff] [blame] | 503 | } |
| 504 | |
| 505 | void tlb_init(void) |
| 506 | { |
| 507 | r4k_tlb_configure(); |
Thiemo Seufer | c6281ed | 2006-03-14 14:35:27 +0000 | [diff] [blame] | 508 | |
Ralf Baechle | 41c594a | 2006-04-05 09:45:45 +0100 | [diff] [blame] | 509 | if (ntlb) { |
| 510 | if (ntlb > 1 && ntlb <= current_cpu_data.tlbsize) { |
| 511 | int wired = current_cpu_data.tlbsize - ntlb; |
| 512 | write_c0_wired(wired); |
| 513 | write_c0_index(wired-1); |
Ralf Baechle | 49a89ef | 2007-10-11 23:46:15 +0100 | [diff] [blame] | 514 | printk("Restricting TLB to %d entries\n", ntlb); |
Ralf Baechle | 41c594a | 2006-04-05 09:45:45 +0100 | [diff] [blame] | 515 | } else |
| 516 | printk("Ignoring invalid argument ntlb=%d\n", ntlb); |
| 517 | } |
| 518 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 519 | build_tlb_refill_handler(); |
| 520 | } |
James Hogan | eaa38d6 | 2014-02-28 17:09:20 +0000 | [diff] [blame] | 521 | |
| 522 | static int r4k_tlb_pm_notifier(struct notifier_block *self, unsigned long cmd, |
| 523 | void *v) |
| 524 | { |
| 525 | switch (cmd) { |
| 526 | case CPU_PM_ENTER_FAILED: |
| 527 | case CPU_PM_EXIT: |
| 528 | r4k_tlb_configure(); |
| 529 | break; |
| 530 | } |
| 531 | |
| 532 | return NOTIFY_OK; |
| 533 | } |
| 534 | |
| 535 | static struct notifier_block r4k_tlb_pm_notifier_block = { |
| 536 | .notifier_call = r4k_tlb_pm_notifier, |
| 537 | }; |
| 538 | |
| 539 | static int __init r4k_tlb_init_pm(void) |
| 540 | { |
| 541 | return cpu_pm_register_notifier(&r4k_tlb_pm_notifier_block); |
| 542 | } |
| 543 | arch_initcall(r4k_tlb_init_pm); |