tlb-sb1.c 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385
  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();
  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. int cpu = smp_processor_id();
  135. if (cpu_context(cpu, mm) != 0) {
  136. unsigned long flags;
  137. int size;
  138. size = (end - start + (PAGE_SIZE - 1)) >> PAGE_SHIFT;
  139. size = (size + 1) >> 1;
  140. local_irq_save(flags);
  141. if (size <= (current_cpu_data.tlbsize/2)) {
  142. int oldpid = read_c0_entryhi();
  143. int newpid = cpu_asid(cpu, mm);
  144. start &= (PAGE_MASK << 1);
  145. end += ((PAGE_SIZE << 1) - 1);
  146. end &= (PAGE_MASK << 1);
  147. while (start < end) {
  148. int idx;
  149. write_c0_entryhi(start | newpid);
  150. start += (PAGE_SIZE << 1);
  151. tlb_probe();
  152. idx = read_c0_index();
  153. write_c0_entrylo0(0);
  154. write_c0_entrylo1(0);
  155. if (idx < 0)
  156. continue;
  157. write_c0_entryhi(UNIQUE_ENTRYHI(idx));
  158. tlb_write_indexed();
  159. }
  160. write_c0_entryhi(oldpid);
  161. } else {
  162. drop_mmu_context(mm, cpu);
  163. }
  164. local_irq_restore(flags);
  165. }
  166. }
  167. void local_flush_tlb_kernel_range(unsigned long start, unsigned long end)
  168. {
  169. unsigned long flags;
  170. int size;
  171. size = (end - start + (PAGE_SIZE - 1)) >> PAGE_SHIFT;
  172. size = (size + 1) >> 1;
  173. local_irq_save(flags);
  174. if (size <= (current_cpu_data.tlbsize/2)) {
  175. int pid = read_c0_entryhi();
  176. start &= (PAGE_MASK << 1);
  177. end += ((PAGE_SIZE << 1) - 1);
  178. end &= (PAGE_MASK << 1);
  179. while (start < end) {
  180. int idx;
  181. write_c0_entryhi(start);
  182. start += (PAGE_SIZE << 1);
  183. tlb_probe();
  184. idx = read_c0_index();
  185. write_c0_entrylo0(0);
  186. write_c0_entrylo1(0);
  187. if (idx < 0)
  188. continue;
  189. write_c0_entryhi(UNIQUE_ENTRYHI(idx));
  190. tlb_write_indexed();
  191. }
  192. write_c0_entryhi(pid);
  193. } else {
  194. local_flush_tlb_all();
  195. }
  196. local_irq_restore(flags);
  197. }
  198. void local_flush_tlb_page(struct vm_area_struct *vma, unsigned long page)
  199. {
  200. int cpu = smp_processor_id();
  201. if (cpu_context(cpu, vma->vm_mm) != 0) {
  202. unsigned long flags;
  203. int oldpid, newpid, idx;
  204. newpid = cpu_asid(cpu, vma->vm_mm);
  205. page &= (PAGE_MASK << 1);
  206. local_irq_save(flags);
  207. oldpid = read_c0_entryhi();
  208. write_c0_entryhi(page | newpid);
  209. tlb_probe();
  210. idx = read_c0_index();
  211. write_c0_entrylo0(0);
  212. write_c0_entrylo1(0);
  213. if (idx < 0)
  214. goto finish;
  215. /* Make sure all entries differ. */
  216. write_c0_entryhi(UNIQUE_ENTRYHI(idx));
  217. tlb_write_indexed();
  218. finish:
  219. write_c0_entryhi(oldpid);
  220. local_irq_restore(flags);
  221. }
  222. }
  223. /*
  224. * Remove one kernel space TLB entry. This entry is assumed to be marked
  225. * global so we don't do the ASID thing.
  226. */
  227. void local_flush_tlb_one(unsigned long page)
  228. {
  229. unsigned long flags;
  230. int oldpid, idx;
  231. local_irq_save(flags);
  232. oldpid = read_c0_entryhi();
  233. page &= (PAGE_MASK << 1);
  234. write_c0_entryhi(page);
  235. tlb_probe();
  236. idx = read_c0_index();
  237. write_c0_entrylo0(0);
  238. write_c0_entrylo1(0);
  239. if (idx >= 0) {
  240. /* Make sure all entries differ. */
  241. write_c0_entryhi(UNIQUE_ENTRYHI(idx));
  242. tlb_write_indexed();
  243. }
  244. write_c0_entryhi(oldpid);
  245. local_irq_restore(flags);
  246. }
  247. /* All entries common to a mm share an asid. To effectively flush
  248. these entries, we just bump the asid. */
  249. void local_flush_tlb_mm(struct mm_struct *mm)
  250. {
  251. int cpu;
  252. preempt_disable();
  253. cpu = smp_processor_id();
  254. if (cpu_context(cpu, mm) != 0) {
  255. drop_mmu_context(mm, cpu);
  256. }
  257. preempt_enable();
  258. }
  259. /* Stolen from mips32 routines */
  260. void __update_tlb(struct vm_area_struct *vma, unsigned long address, pte_t pte)
  261. {
  262. unsigned long flags;
  263. pgd_t *pgdp;
  264. pud_t *pudp;
  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. pudp = pud_offset(pgdp, address);
  280. pmdp = pmd_offset(pudp, address);
  281. idx = read_c0_index();
  282. ptep = pte_offset_map(pmdp, address);
  283. #if defined(CONFIG_64BIT_PHYS_ADDR) && defined(CONFIG_CPU_MIPS32_R1)
  284. write_c0_entrylo0(ptep->pte_high);
  285. ptep++;
  286. write_c0_entrylo1(ptep->pte_high);
  287. #else
  288. write_c0_entrylo0(pte_val(*ptep++) >> 6);
  289. write_c0_entrylo1(pte_val(*ptep) >> 6);
  290. #endif
  291. if (idx < 0)
  292. tlb_write_random();
  293. else
  294. tlb_write_indexed();
  295. local_irq_restore(flags);
  296. }
  297. void __init add_wired_entry(unsigned long entrylo0, unsigned long entrylo1,
  298. unsigned long entryhi, unsigned long pagemask)
  299. {
  300. unsigned long flags;
  301. unsigned long wired;
  302. unsigned long old_pagemask;
  303. unsigned long old_ctx;
  304. local_irq_save(flags);
  305. /* Save old context and create impossible VPN2 value */
  306. old_ctx = read_c0_entryhi();
  307. old_pagemask = read_c0_pagemask();
  308. wired = read_c0_wired();
  309. write_c0_wired(wired + 1);
  310. write_c0_index(wired);
  311. write_c0_pagemask(pagemask);
  312. write_c0_entryhi(entryhi);
  313. write_c0_entrylo0(entrylo0);
  314. write_c0_entrylo1(entrylo1);
  315. tlb_write_indexed();
  316. write_c0_entryhi(old_ctx);
  317. write_c0_pagemask(old_pagemask);
  318. local_flush_tlb_all();
  319. local_irq_restore(flags);
  320. }
  321. /*
  322. * This is called from loadmmu.c. We have to set up all the
  323. * memory management function pointers, as well as initialize
  324. * the caches and tlbs
  325. */
  326. void tlb_init(void)
  327. {
  328. write_c0_pagemask(PM_DEFAULT_MASK);
  329. write_c0_wired(0);
  330. /*
  331. * We don't know what state the firmware left the TLB's in, so this is
  332. * the ultra-conservative way to flush the TLB's and avoid machine
  333. * check exceptions due to duplicate TLB entries
  334. */
  335. sb1_sanitize_tlb();
  336. build_tlb_refill_handler();
  337. }