tlb-sb1.c 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376
  1. /*
  2. * Copyright (C) 1996 David S. Miller (dm@engr.sgi.com)
  3. * Copyright (C) 1997, 2001 Ralf Baechle (ralf@gnu.org)
  4. * Copyright (C) 2000, 2001, 2002, 2003 Broadcom Corporation
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU General Public License
  8. * as published by the Free Software Foundation; either version 2
  9. * of the License, or (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program; if not, write to the Free Software
  18. * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  19. */
  20. #include <linux/init.h>
  21. #include <asm/mmu_context.h>
  22. #include <asm/bootinfo.h>
  23. #include <asm/cpu.h>
  24. extern void build_tlb_refill_handler(void);
  25. #define UNIQUE_ENTRYHI(idx) (CKSEG0 + ((idx) << (PAGE_SHIFT + 1)))
  26. /* Dump the current entry* and pagemask registers */
  27. static inline void dump_cur_tlb_regs(void)
  28. {
  29. unsigned int entryhihi, entryhilo, entrylo0hi, entrylo0lo, entrylo1hi;
  30. unsigned int entrylo1lo, pagemask;
  31. __asm__ __volatile__ (
  32. ".set push \n"
  33. ".set noreorder \n"
  34. ".set mips64 \n"
  35. ".set noat \n"
  36. " tlbr \n"
  37. " dmfc0 $1, $10 \n"
  38. " dsrl32 %0, $1, 0 \n"
  39. " sll %1, $1, 0 \n"
  40. " dmfc0 $1, $2 \n"
  41. " dsrl32 %2, $1, 0 \n"
  42. " sll %3, $1, 0 \n"
  43. " dmfc0 $1, $3 \n"
  44. " dsrl32 %4, $1, 0 \n"
  45. " sll %5, $1, 0 \n"
  46. " mfc0 %6, $5 \n"
  47. ".set pop \n"
  48. : "=r" (entryhihi), "=r" (entryhilo),
  49. "=r" (entrylo0hi), "=r" (entrylo0lo),
  50. "=r" (entrylo1hi), "=r" (entrylo1lo),
  51. "=r" (pagemask));
  52. printk("%08X%08X %08X%08X %08X%08X %08X",
  53. entryhihi, entryhilo,
  54. entrylo0hi, entrylo0lo,
  55. entrylo1hi, entrylo1lo,
  56. pagemask);
  57. }
  58. void sb1_dump_tlb(void)
  59. {
  60. unsigned long old_ctx;
  61. unsigned long flags;
  62. int entry;
  63. local_irq_save(flags);
  64. old_ctx = read_c0_entryhi();
  65. printk("Current TLB registers state:\n"
  66. " EntryHi EntryLo0 EntryLo1 PageMask Index\n"
  67. "--------------------------------------------------------------------\n");
  68. dump_cur_tlb_regs();
  69. printk(" %08X\n", read_c0_index());
  70. printk("\n\nFull TLB Dump:\n"
  71. "Idx EntryHi EntryLo0 EntryLo1 PageMask\n"
  72. "--------------------------------------------------------------\n");
  73. for (entry = 0; entry < current_cpu_data.tlbsize; entry++) {
  74. write_c0_index(entry);
  75. printk("\n%02i ", entry);
  76. dump_cur_tlb_regs();
  77. }
  78. printk("\n");
  79. write_c0_entryhi(old_ctx);
  80. local_irq_restore(flags);
  81. }
  82. void local_flush_tlb_all(void)
  83. {
  84. unsigned long flags;
  85. unsigned long old_ctx;
  86. int entry;
  87. local_irq_save(flags);
  88. /* Save old context and create impossible VPN2 value */
  89. old_ctx = read_c0_entryhi() & ASID_MASK;
  90. write_c0_entrylo0(0);
  91. write_c0_entrylo1(0);
  92. entry = read_c0_wired();
  93. while (entry < current_cpu_data.tlbsize) {
  94. write_c0_entryhi(UNIQUE_ENTRYHI(entry));
  95. write_c0_index(entry);
  96. tlb_write_indexed();
  97. entry++;
  98. }
  99. write_c0_entryhi(old_ctx);
  100. local_irq_restore(flags);
  101. }
  102. /*
  103. * Use a bogus region of memory (starting at 0) to sanitize the TLB's.
  104. * Use increments of the maximum page size (16MB), and check for duplicate
  105. * entries before doing a given write. Then, when we're safe from collisions
  106. * with the firmware, go back and give all the entries invalid addresses with
  107. * the normal flush routine. Wired entries will be killed as well!
  108. */
  109. static void __init sb1_sanitize_tlb(void)
  110. {
  111. int entry;
  112. long addr = 0;
  113. long inc = 1<<24; /* 16MB */
  114. /* Save old context and create impossible VPN2 value */
  115. write_c0_entrylo0(0);
  116. write_c0_entrylo1(0);
  117. for (entry = 0; entry < current_cpu_data.tlbsize; entry++) {
  118. do {
  119. addr += inc;
  120. write_c0_entryhi(addr);
  121. tlb_probe();
  122. } while ((int)(read_c0_index()) >= 0);
  123. write_c0_index(entry);
  124. tlb_write_indexed();
  125. }
  126. /* Now that we know we're safe from collisions, we can safely flush
  127. the TLB with the "normal" routine. */
  128. local_flush_tlb_all();
  129. }
  130. void local_flush_tlb_range(struct vm_area_struct *vma, unsigned long start,
  131. unsigned long end)
  132. {
  133. struct mm_struct *mm = vma->vm_mm;
  134. unsigned long flags;
  135. int cpu;
  136. local_irq_save(flags);
  137. cpu = smp_processor_id();
  138. if (cpu_context(cpu, mm) != 0) {
  139. int size;
  140. size = (end - start + (PAGE_SIZE - 1)) >> PAGE_SHIFT;
  141. size = (size + 1) >> 1;
  142. if (size <= (current_cpu_data.tlbsize/2)) {
  143. int oldpid = read_c0_entryhi() & ASID_MASK;
  144. int newpid = cpu_asid(cpu, mm);
  145. start &= (PAGE_MASK << 1);
  146. end += ((PAGE_SIZE << 1) - 1);
  147. end &= (PAGE_MASK << 1);
  148. while (start < end) {
  149. int idx;
  150. write_c0_entryhi(start | newpid);
  151. start += (PAGE_SIZE << 1);
  152. tlb_probe();
  153. idx = read_c0_index();
  154. write_c0_entrylo0(0);
  155. write_c0_entrylo1(0);
  156. write_c0_entryhi(UNIQUE_ENTRYHI(idx));
  157. if (idx < 0)
  158. continue;
  159. tlb_write_indexed();
  160. }
  161. write_c0_entryhi(oldpid);
  162. } else {
  163. drop_mmu_context(mm, cpu);
  164. }
  165. }
  166. local_irq_restore(flags);
  167. }
  168. void local_flush_tlb_kernel_range(unsigned long start, unsigned long end)
  169. {
  170. unsigned long flags;
  171. int size;
  172. size = (end - start + (PAGE_SIZE - 1)) >> PAGE_SHIFT;
  173. size = (size + 1) >> 1;
  174. local_irq_save(flags);
  175. if (size <= (current_cpu_data.tlbsize/2)) {
  176. int pid = read_c0_entryhi();
  177. start &= (PAGE_MASK << 1);
  178. end += ((PAGE_SIZE << 1) - 1);
  179. end &= (PAGE_MASK << 1);
  180. while (start < end) {
  181. int idx;
  182. write_c0_entryhi(start);
  183. start += (PAGE_SIZE << 1);
  184. tlb_probe();
  185. idx = read_c0_index();
  186. write_c0_entrylo0(0);
  187. write_c0_entrylo1(0);
  188. write_c0_entryhi(UNIQUE_ENTRYHI(idx));
  189. if (idx < 0)
  190. continue;
  191. tlb_write_indexed();
  192. }
  193. write_c0_entryhi(pid);
  194. } else {
  195. local_flush_tlb_all();
  196. }
  197. local_irq_restore(flags);
  198. }
  199. void local_flush_tlb_page(struct vm_area_struct *vma, unsigned long page)
  200. {
  201. unsigned long flags;
  202. int cpu = smp_processor_id();
  203. local_irq_save(flags);
  204. if (cpu_context(cpu, vma->vm_mm) != 0) {
  205. int oldpid, newpid, idx;
  206. newpid = cpu_asid(cpu, vma->vm_mm);
  207. page &= (PAGE_MASK << 1);
  208. oldpid = read_c0_entryhi() & ASID_MASK;
  209. write_c0_entryhi(page | newpid);
  210. tlb_probe();
  211. idx = read_c0_index();
  212. write_c0_entrylo0(0);
  213. write_c0_entrylo1(0);
  214. if (idx < 0)
  215. goto finish;
  216. /* Make sure all entries differ. */
  217. write_c0_entryhi(UNIQUE_ENTRYHI(idx));
  218. tlb_write_indexed();
  219. finish:
  220. write_c0_entryhi(oldpid);
  221. }
  222. local_irq_restore(flags);
  223. }
  224. /*
  225. * Remove one kernel space TLB entry. This entry is assumed to be marked
  226. * global so we don't do the ASID thing.
  227. */
  228. void local_flush_tlb_one(unsigned long page)
  229. {
  230. unsigned long flags;
  231. int oldpid, idx;
  232. page &= (PAGE_MASK << 1);
  233. oldpid = read_c0_entryhi() & ASID_MASK;
  234. local_irq_save(flags);
  235. write_c0_entryhi(page);
  236. tlb_probe();
  237. idx = read_c0_index();
  238. if (idx >= 0) {
  239. /* Make sure all entries differ. */
  240. write_c0_entryhi(UNIQUE_ENTRYHI(idx));
  241. write_c0_entrylo0(0);
  242. write_c0_entrylo1(0);
  243. tlb_write_indexed();
  244. }
  245. write_c0_entryhi(oldpid);
  246. local_irq_restore(flags);
  247. }
  248. /* All entries common to a mm share an asid. To effectively flush
  249. these entries, we just bump the asid. */
  250. void local_flush_tlb_mm(struct mm_struct *mm)
  251. {
  252. int cpu;
  253. preempt_disable();
  254. cpu = smp_processor_id();
  255. if (cpu_context(cpu, mm) != 0) {
  256. drop_mmu_context(mm, cpu);
  257. }
  258. preempt_enable();
  259. }
  260. /* Stolen from mips32 routines */
  261. void __update_tlb(struct vm_area_struct *vma, unsigned long address, pte_t pte)
  262. {
  263. unsigned long flags;
  264. pgd_t *pgdp;
  265. pmd_t *pmdp;
  266. pte_t *ptep;
  267. int idx, pid;
  268. /*
  269. * Handle debugger faulting in for debugee.
  270. */
  271. if (current->active_mm != vma->vm_mm)
  272. return;
  273. local_irq_save(flags);
  274. pid = read_c0_entryhi() & ASID_MASK;
  275. address &= (PAGE_MASK << 1);
  276. write_c0_entryhi(address | (pid));
  277. pgdp = pgd_offset(vma->vm_mm, address);
  278. tlb_probe();
  279. pmdp = pmd_offset(pgdp, address);
  280. idx = read_c0_index();
  281. ptep = pte_offset_map(pmdp, address);
  282. write_c0_entrylo0(pte_val(*ptep++) >> 6);
  283. write_c0_entrylo1(pte_val(*ptep) >> 6);
  284. if (idx < 0) {
  285. tlb_write_random();
  286. } else {
  287. tlb_write_indexed();
  288. }
  289. local_irq_restore(flags);
  290. }
  291. void __init add_wired_entry(unsigned long entrylo0, unsigned long entrylo1,
  292. unsigned long entryhi, unsigned long pagemask)
  293. {
  294. unsigned long flags;
  295. unsigned long wired;
  296. unsigned long old_pagemask;
  297. unsigned long old_ctx;
  298. local_irq_save(flags);
  299. old_ctx = read_c0_entryhi() & 0xff;
  300. old_pagemask = read_c0_pagemask();
  301. wired = read_c0_wired();
  302. write_c0_wired(wired + 1);
  303. write_c0_index(wired);
  304. write_c0_pagemask(pagemask);
  305. write_c0_entryhi(entryhi);
  306. write_c0_entrylo0(entrylo0);
  307. write_c0_entrylo1(entrylo1);
  308. tlb_write_indexed();
  309. write_c0_entryhi(old_ctx);
  310. write_c0_pagemask(old_pagemask);
  311. local_flush_tlb_all();
  312. local_irq_restore(flags);
  313. }
  314. /*
  315. * This is called from loadmmu.c. We have to set up all the
  316. * memory management function pointers, as well as initialize
  317. * the caches and tlbs
  318. */
  319. void tlb_init(void)
  320. {
  321. write_c0_pagemask(PM_DEFAULT_MASK);
  322. write_c0_wired(0);
  323. /*
  324. * We don't know what state the firmware left the TLB's in, so this is
  325. * the ultra-conservative way to flush the TLB's and avoid machine
  326. * check exceptions due to duplicate TLB entries
  327. */
  328. sb1_sanitize_tlb();
  329. build_tlb_refill_handler();
  330. }