tlb-r4k.c 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431
  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. *
  6. * Copyright (C) 1996 David S. Miller (dm@engr.sgi.com)
  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. */
  11. #include <linux/config.h>
  12. #include <linux/init.h>
  13. #include <linux/sched.h>
  14. #include <linux/mm.h>
  15. #include <asm/cpu.h>
  16. #include <asm/bootinfo.h>
  17. #include <asm/mmu_context.h>
  18. #include <asm/pgtable.h>
  19. #include <asm/system.h>
  20. extern void build_tlb_refill_handler(void);
  21. /*
  22. * Make sure all entries differ. If they're not different
  23. * MIPS32 will take revenge ...
  24. */
  25. #define UNIQUE_ENTRYHI(idx) (CKSEG0 + ((idx) << (PAGE_SHIFT + 1)))
  26. /* CP0 hazard avoidance. */
  27. #define BARRIER __asm__ __volatile__(".set noreorder\n\t" \
  28. "nop; nop; nop; nop; nop; nop;\n\t" \
  29. ".set reorder\n\t")
  30. void local_flush_tlb_all(void)
  31. {
  32. unsigned long flags;
  33. unsigned long old_ctx;
  34. int entry;
  35. local_irq_save(flags);
  36. /* Save old context and create impossible VPN2 value */
  37. old_ctx = read_c0_entryhi();
  38. write_c0_entrylo0(0);
  39. write_c0_entrylo1(0);
  40. entry = read_c0_wired();
  41. /* Blast 'em all away. */
  42. while (entry < current_cpu_data.tlbsize) {
  43. /* Make sure all entries differ. */
  44. write_c0_entryhi(UNIQUE_ENTRYHI(entry));
  45. write_c0_index(entry);
  46. mtc0_tlbw_hazard();
  47. tlb_write_indexed();
  48. entry++;
  49. }
  50. tlbw_use_hazard();
  51. write_c0_entryhi(old_ctx);
  52. local_irq_restore(flags);
  53. }
  54. /* All entries common to a mm share an asid. To effectively flush
  55. these entries, we just bump the asid. */
  56. void local_flush_tlb_mm(struct mm_struct *mm)
  57. {
  58. int cpu;
  59. preempt_disable();
  60. cpu = smp_processor_id();
  61. if (cpu_context(cpu, mm) != 0) {
  62. drop_mmu_context(mm, cpu);
  63. }
  64. preempt_enable();
  65. }
  66. void local_flush_tlb_range(struct vm_area_struct *vma, unsigned long start,
  67. unsigned long end)
  68. {
  69. struct mm_struct *mm = vma->vm_mm;
  70. int cpu = smp_processor_id();
  71. if (cpu_context(cpu, mm) != 0) {
  72. unsigned long flags;
  73. int size;
  74. size = (end - start + (PAGE_SIZE - 1)) >> PAGE_SHIFT;
  75. size = (size + 1) >> 1;
  76. local_irq_save(flags);
  77. if (size <= current_cpu_data.tlbsize/2) {
  78. int oldpid = read_c0_entryhi();
  79. int newpid = cpu_asid(cpu, mm);
  80. start &= (PAGE_MASK << 1);
  81. end += ((PAGE_SIZE << 1) - 1);
  82. end &= (PAGE_MASK << 1);
  83. while (start < end) {
  84. int idx;
  85. write_c0_entryhi(start | newpid);
  86. start += (PAGE_SIZE << 1);
  87. mtc0_tlbw_hazard();
  88. tlb_probe();
  89. BARRIER;
  90. idx = read_c0_index();
  91. write_c0_entrylo0(0);
  92. write_c0_entrylo1(0);
  93. if (idx < 0)
  94. continue;
  95. /* Make sure all entries differ. */
  96. write_c0_entryhi(UNIQUE_ENTRYHI(idx));
  97. mtc0_tlbw_hazard();
  98. tlb_write_indexed();
  99. }
  100. tlbw_use_hazard();
  101. write_c0_entryhi(oldpid);
  102. } else {
  103. drop_mmu_context(mm, cpu);
  104. }
  105. local_irq_restore(flags);
  106. }
  107. }
  108. void local_flush_tlb_kernel_range(unsigned long start, unsigned long end)
  109. {
  110. unsigned long flags;
  111. int size;
  112. size = (end - start + (PAGE_SIZE - 1)) >> PAGE_SHIFT;
  113. size = (size + 1) >> 1;
  114. local_irq_save(flags);
  115. if (size <= current_cpu_data.tlbsize / 2) {
  116. int pid = read_c0_entryhi();
  117. start &= (PAGE_MASK << 1);
  118. end += ((PAGE_SIZE << 1) - 1);
  119. end &= (PAGE_MASK << 1);
  120. while (start < end) {
  121. int idx;
  122. write_c0_entryhi(start);
  123. start += (PAGE_SIZE << 1);
  124. mtc0_tlbw_hazard();
  125. tlb_probe();
  126. BARRIER;
  127. idx = read_c0_index();
  128. write_c0_entrylo0(0);
  129. write_c0_entrylo1(0);
  130. if (idx < 0)
  131. continue;
  132. /* Make sure all entries differ. */
  133. write_c0_entryhi(UNIQUE_ENTRYHI(idx));
  134. mtc0_tlbw_hazard();
  135. tlb_write_indexed();
  136. }
  137. tlbw_use_hazard();
  138. write_c0_entryhi(pid);
  139. } else {
  140. local_flush_tlb_all();
  141. }
  142. local_irq_restore(flags);
  143. }
  144. void local_flush_tlb_page(struct vm_area_struct *vma, unsigned long page)
  145. {
  146. int cpu = smp_processor_id();
  147. if (cpu_context(cpu, vma->vm_mm) != 0) {
  148. unsigned long flags;
  149. int oldpid, newpid, idx;
  150. newpid = cpu_asid(cpu, vma->vm_mm);
  151. page &= (PAGE_MASK << 1);
  152. local_irq_save(flags);
  153. oldpid = read_c0_entryhi();
  154. write_c0_entryhi(page | newpid);
  155. mtc0_tlbw_hazard();
  156. tlb_probe();
  157. BARRIER;
  158. idx = read_c0_index();
  159. write_c0_entrylo0(0);
  160. write_c0_entrylo1(0);
  161. if (idx < 0)
  162. goto finish;
  163. /* Make sure all entries differ. */
  164. write_c0_entryhi(UNIQUE_ENTRYHI(idx));
  165. mtc0_tlbw_hazard();
  166. tlb_write_indexed();
  167. tlbw_use_hazard();
  168. finish:
  169. write_c0_entryhi(oldpid);
  170. local_irq_restore(flags);
  171. }
  172. }
  173. /*
  174. * This one is only used for pages with the global bit set so we don't care
  175. * much about the ASID.
  176. */
  177. void local_flush_tlb_one(unsigned long page)
  178. {
  179. unsigned long flags;
  180. int oldpid, idx;
  181. local_irq_save(flags);
  182. oldpid = read_c0_entryhi();
  183. page &= (PAGE_MASK << 1);
  184. write_c0_entryhi(page);
  185. mtc0_tlbw_hazard();
  186. tlb_probe();
  187. BARRIER;
  188. idx = read_c0_index();
  189. write_c0_entrylo0(0);
  190. write_c0_entrylo1(0);
  191. if (idx >= 0) {
  192. /* Make sure all entries differ. */
  193. write_c0_entryhi(UNIQUE_ENTRYHI(idx));
  194. mtc0_tlbw_hazard();
  195. tlb_write_indexed();
  196. tlbw_use_hazard();
  197. }
  198. write_c0_entryhi(oldpid);
  199. local_irq_restore(flags);
  200. }
  201. /*
  202. * We will need multiple versions of update_mmu_cache(), one that just
  203. * updates the TLB with the new pte(s), and another which also checks
  204. * for the R4k "end of page" hardware bug and does the needy.
  205. */
  206. void __update_tlb(struct vm_area_struct * vma, unsigned long address, pte_t pte)
  207. {
  208. unsigned long flags;
  209. pgd_t *pgdp;
  210. pud_t *pudp;
  211. pmd_t *pmdp;
  212. pte_t *ptep;
  213. int idx, pid;
  214. /*
  215. * Handle debugger faulting in for debugee.
  216. */
  217. if (current->active_mm != vma->vm_mm)
  218. return;
  219. local_irq_save(flags);
  220. pid = read_c0_entryhi() & ASID_MASK;
  221. address &= (PAGE_MASK << 1);
  222. write_c0_entryhi(address | pid);
  223. pgdp = pgd_offset(vma->vm_mm, address);
  224. mtc0_tlbw_hazard();
  225. tlb_probe();
  226. BARRIER;
  227. pudp = pud_offset(pgdp, address);
  228. pmdp = pmd_offset(pudp, address);
  229. idx = read_c0_index();
  230. ptep = pte_offset_map(pmdp, address);
  231. #if defined(CONFIG_64BIT_PHYS_ADDR) && defined(CONFIG_CPU_MIPS32_R1)
  232. write_c0_entrylo0(ptep->pte_high);
  233. ptep++;
  234. write_c0_entrylo1(ptep->pte_high);
  235. #else
  236. write_c0_entrylo0(pte_val(*ptep++) >> 6);
  237. write_c0_entrylo1(pte_val(*ptep) >> 6);
  238. #endif
  239. mtc0_tlbw_hazard();
  240. if (idx < 0)
  241. tlb_write_random();
  242. else
  243. tlb_write_indexed();
  244. tlbw_use_hazard();
  245. local_irq_restore(flags);
  246. }
  247. #if 0
  248. static void r4k_update_mmu_cache_hwbug(struct vm_area_struct * vma,
  249. unsigned long address, pte_t pte)
  250. {
  251. unsigned long flags;
  252. unsigned int asid;
  253. pgd_t *pgdp;
  254. pmd_t *pmdp;
  255. pte_t *ptep;
  256. int idx;
  257. local_irq_save(flags);
  258. address &= (PAGE_MASK << 1);
  259. asid = read_c0_entryhi() & ASID_MASK;
  260. write_c0_entryhi(address | asid);
  261. pgdp = pgd_offset(vma->vm_mm, address);
  262. mtc0_tlbw_hazard();
  263. tlb_probe();
  264. BARRIER;
  265. pmdp = pmd_offset(pgdp, address);
  266. idx = read_c0_index();
  267. ptep = pte_offset_map(pmdp, address);
  268. write_c0_entrylo0(pte_val(*ptep++) >> 6);
  269. write_c0_entrylo1(pte_val(*ptep) >> 6);
  270. mtc0_tlbw_hazard();
  271. if (idx < 0)
  272. tlb_write_random();
  273. else
  274. tlb_write_indexed();
  275. tlbw_use_hazard();
  276. local_irq_restore(flags);
  277. }
  278. #endif
  279. void __init add_wired_entry(unsigned long entrylo0, unsigned long entrylo1,
  280. unsigned long entryhi, unsigned long pagemask)
  281. {
  282. unsigned long flags;
  283. unsigned long wired;
  284. unsigned long old_pagemask;
  285. unsigned long old_ctx;
  286. local_irq_save(flags);
  287. /* Save old context and create impossible VPN2 value */
  288. old_ctx = read_c0_entryhi();
  289. old_pagemask = read_c0_pagemask();
  290. wired = read_c0_wired();
  291. write_c0_wired(wired + 1);
  292. write_c0_index(wired);
  293. BARRIER;
  294. write_c0_pagemask(pagemask);
  295. write_c0_entryhi(entryhi);
  296. write_c0_entrylo0(entrylo0);
  297. write_c0_entrylo1(entrylo1);
  298. mtc0_tlbw_hazard();
  299. tlb_write_indexed();
  300. tlbw_use_hazard();
  301. write_c0_entryhi(old_ctx);
  302. BARRIER;
  303. write_c0_pagemask(old_pagemask);
  304. local_flush_tlb_all();
  305. local_irq_restore(flags);
  306. }
  307. /*
  308. * Used for loading TLB entries before trap_init() has started, when we
  309. * don't actually want to add a wired entry which remains throughout the
  310. * lifetime of the system
  311. */
  312. static int temp_tlb_entry __initdata;
  313. __init int add_temporary_entry(unsigned long entrylo0, unsigned long entrylo1,
  314. unsigned long entryhi, unsigned long pagemask)
  315. {
  316. int ret = 0;
  317. unsigned long flags;
  318. unsigned long wired;
  319. unsigned long old_pagemask;
  320. unsigned long old_ctx;
  321. local_irq_save(flags);
  322. /* Save old context and create impossible VPN2 value */
  323. old_ctx = read_c0_entryhi();
  324. old_pagemask = read_c0_pagemask();
  325. wired = read_c0_wired();
  326. if (--temp_tlb_entry < wired) {
  327. printk(KERN_WARNING
  328. "No TLB space left for add_temporary_entry\n");
  329. ret = -ENOSPC;
  330. goto out;
  331. }
  332. write_c0_index(temp_tlb_entry);
  333. write_c0_pagemask(pagemask);
  334. write_c0_entryhi(entryhi);
  335. write_c0_entrylo0(entrylo0);
  336. write_c0_entrylo1(entrylo1);
  337. mtc0_tlbw_hazard();
  338. tlb_write_indexed();
  339. tlbw_use_hazard();
  340. write_c0_entryhi(old_ctx);
  341. write_c0_pagemask(old_pagemask);
  342. out:
  343. local_irq_restore(flags);
  344. return ret;
  345. }
  346. static void __init probe_tlb(unsigned long config)
  347. {
  348. struct cpuinfo_mips *c = &current_cpu_data;
  349. unsigned int reg;
  350. /*
  351. * If this isn't a MIPS32 / MIPS64 compliant CPU. Config 1 register
  352. * is not supported, we assume R4k style. Cpu probing already figured
  353. * out the number of tlb entries.
  354. */
  355. if ((c->processor_id & 0xff0000) == PRID_COMP_LEGACY)
  356. return;
  357. reg = read_c0_config1();
  358. if (!((config >> 7) & 3))
  359. panic("No TLB present");
  360. c->tlbsize = ((reg >> 25) & 0x3f) + 1;
  361. }
  362. void __init tlb_init(void)
  363. {
  364. unsigned int config = read_c0_config();
  365. /*
  366. * You should never change this register:
  367. * - On R4600 1.7 the tlbp never hits for pages smaller than
  368. * the value in the c0_pagemask register.
  369. * - The entire mm handling assumes the c0_pagemask register to
  370. * be set for 4kb pages.
  371. */
  372. probe_tlb(config);
  373. write_c0_pagemask(PM_DEFAULT_MASK);
  374. write_c0_wired(0);
  375. temp_tlb_entry = current_cpu_data.tlbsize - 1;
  376. local_flush_tlb_all();
  377. build_tlb_refill_handler();
  378. }