tlb-r4k.c 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433
  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 (davem@davemloft.net)
  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/init.h>
  12. #include <linux/sched.h>
  13. #include <linux/smp.h>
  14. #include <linux/mm.h>
  15. #include <linux/hugetlb.h>
  16. #include <asm/cpu.h>
  17. #include <asm/bootinfo.h>
  18. #include <asm/mmu_context.h>
  19. #include <asm/pgtable.h>
  20. #include <asm/tlbmisc.h>
  21. extern void build_tlb_refill_handler(void);
  22. /*
  23. * Make sure all entries differ. If they're not different
  24. * MIPS32 will take revenge ...
  25. */
  26. #define UNIQUE_ENTRYHI(idx) (CKSEG0 + ((idx) << (PAGE_SHIFT + 1)))
  27. /* Atomicity and interruptability */
  28. #ifdef CONFIG_MIPS_MT_SMTC
  29. #include <asm/smtc.h>
  30. #include <asm/mipsmtregs.h>
  31. #define ENTER_CRITICAL(flags) \
  32. { \
  33. unsigned int mvpflags; \
  34. local_irq_save(flags);\
  35. mvpflags = dvpe()
  36. #define EXIT_CRITICAL(flags) \
  37. evpe(mvpflags); \
  38. local_irq_restore(flags); \
  39. }
  40. #else
  41. #define ENTER_CRITICAL(flags) local_irq_save(flags)
  42. #define EXIT_CRITICAL(flags) local_irq_restore(flags)
  43. #endif /* CONFIG_MIPS_MT_SMTC */
  44. #if defined(CONFIG_CPU_LOONGSON2)
  45. /*
  46. * LOONGSON2 has a 4 entry itlb which is a subset of dtlb,
  47. * unfortrunately, itlb is not totally transparent to software.
  48. */
  49. #define FLUSH_ITLB write_c0_diag(4);
  50. #define FLUSH_ITLB_VM(vma) { if ((vma)->vm_flags & VM_EXEC) write_c0_diag(4); }
  51. #else
  52. #define FLUSH_ITLB
  53. #define FLUSH_ITLB_VM(vma)
  54. #endif
  55. void local_flush_tlb_all(void)
  56. {
  57. unsigned long flags;
  58. unsigned long old_ctx;
  59. int entry;
  60. ENTER_CRITICAL(flags);
  61. /* Save old context and create impossible VPN2 value */
  62. old_ctx = read_c0_entryhi();
  63. write_c0_entrylo0(0);
  64. write_c0_entrylo1(0);
  65. entry = read_c0_wired();
  66. /* Blast 'em all away. */
  67. while (entry < current_cpu_data.tlbsize) {
  68. /* Make sure all entries differ. */
  69. write_c0_entryhi(UNIQUE_ENTRYHI(entry));
  70. write_c0_index(entry);
  71. mtc0_tlbw_hazard();
  72. tlb_write_indexed();
  73. entry++;
  74. }
  75. tlbw_use_hazard();
  76. write_c0_entryhi(old_ctx);
  77. FLUSH_ITLB;
  78. EXIT_CRITICAL(flags);
  79. }
  80. /* All entries common to a mm share an asid. To effectively flush
  81. these entries, we just bump the asid. */
  82. void local_flush_tlb_mm(struct mm_struct *mm)
  83. {
  84. int cpu;
  85. preempt_disable();
  86. cpu = smp_processor_id();
  87. if (cpu_context(cpu, mm) != 0) {
  88. drop_mmu_context(mm, cpu);
  89. }
  90. preempt_enable();
  91. }
  92. void local_flush_tlb_range(struct vm_area_struct *vma, unsigned long start,
  93. unsigned long end)
  94. {
  95. struct mm_struct *mm = vma->vm_mm;
  96. int cpu = smp_processor_id();
  97. if (cpu_context(cpu, mm) != 0) {
  98. unsigned long size, flags;
  99. int huge = is_vm_hugetlb_page(vma);
  100. ENTER_CRITICAL(flags);
  101. if (huge) {
  102. start = round_down(start, HPAGE_SIZE);
  103. end = round_up(end, HPAGE_SIZE);
  104. size = (end - start) >> HPAGE_SHIFT;
  105. } else {
  106. start = round_down(start, PAGE_SIZE << 1);
  107. end = round_up(end, PAGE_SIZE << 1);
  108. size = (end - start) >> (PAGE_SHIFT + 1);
  109. }
  110. if (size <= current_cpu_data.tlbsize/2) {
  111. int oldpid = read_c0_entryhi();
  112. int newpid = cpu_asid(cpu, mm);
  113. while (start < end) {
  114. int idx;
  115. write_c0_entryhi(start | newpid);
  116. if (huge)
  117. start += HPAGE_SIZE;
  118. else
  119. start += (PAGE_SIZE << 1);
  120. mtc0_tlbw_hazard();
  121. tlb_probe();
  122. tlb_probe_hazard();
  123. idx = read_c0_index();
  124. write_c0_entrylo0(0);
  125. write_c0_entrylo1(0);
  126. if (idx < 0)
  127. continue;
  128. /* Make sure all entries differ. */
  129. write_c0_entryhi(UNIQUE_ENTRYHI(idx));
  130. mtc0_tlbw_hazard();
  131. tlb_write_indexed();
  132. }
  133. tlbw_use_hazard();
  134. write_c0_entryhi(oldpid);
  135. } else {
  136. drop_mmu_context(mm, cpu);
  137. }
  138. FLUSH_ITLB;
  139. EXIT_CRITICAL(flags);
  140. }
  141. }
  142. void local_flush_tlb_kernel_range(unsigned long start, unsigned long end)
  143. {
  144. unsigned long size, flags;
  145. ENTER_CRITICAL(flags);
  146. size = (end - start + (PAGE_SIZE - 1)) >> PAGE_SHIFT;
  147. size = (size + 1) >> 1;
  148. if (size <= current_cpu_data.tlbsize / 2) {
  149. int pid = read_c0_entryhi();
  150. start &= (PAGE_MASK << 1);
  151. end += ((PAGE_SIZE << 1) - 1);
  152. end &= (PAGE_MASK << 1);
  153. while (start < end) {
  154. int idx;
  155. write_c0_entryhi(start);
  156. start += (PAGE_SIZE << 1);
  157. mtc0_tlbw_hazard();
  158. tlb_probe();
  159. tlb_probe_hazard();
  160. idx = read_c0_index();
  161. write_c0_entrylo0(0);
  162. write_c0_entrylo1(0);
  163. if (idx < 0)
  164. continue;
  165. /* Make sure all entries differ. */
  166. write_c0_entryhi(UNIQUE_ENTRYHI(idx));
  167. mtc0_tlbw_hazard();
  168. tlb_write_indexed();
  169. }
  170. tlbw_use_hazard();
  171. write_c0_entryhi(pid);
  172. } else {
  173. local_flush_tlb_all();
  174. }
  175. FLUSH_ITLB;
  176. EXIT_CRITICAL(flags);
  177. }
  178. void local_flush_tlb_page(struct vm_area_struct *vma, unsigned long page)
  179. {
  180. int cpu = smp_processor_id();
  181. if (cpu_context(cpu, vma->vm_mm) != 0) {
  182. unsigned long flags;
  183. int oldpid, newpid, idx;
  184. newpid = cpu_asid(cpu, vma->vm_mm);
  185. page &= (PAGE_MASK << 1);
  186. ENTER_CRITICAL(flags);
  187. oldpid = read_c0_entryhi();
  188. write_c0_entryhi(page | newpid);
  189. mtc0_tlbw_hazard();
  190. tlb_probe();
  191. tlb_probe_hazard();
  192. idx = read_c0_index();
  193. write_c0_entrylo0(0);
  194. write_c0_entrylo1(0);
  195. if (idx < 0)
  196. goto finish;
  197. /* Make sure all entries differ. */
  198. write_c0_entryhi(UNIQUE_ENTRYHI(idx));
  199. mtc0_tlbw_hazard();
  200. tlb_write_indexed();
  201. tlbw_use_hazard();
  202. finish:
  203. write_c0_entryhi(oldpid);
  204. FLUSH_ITLB_VM(vma);
  205. EXIT_CRITICAL(flags);
  206. }
  207. }
  208. /*
  209. * This one is only used for pages with the global bit set so we don't care
  210. * much about the ASID.
  211. */
  212. void local_flush_tlb_one(unsigned long page)
  213. {
  214. unsigned long flags;
  215. int oldpid, idx;
  216. ENTER_CRITICAL(flags);
  217. oldpid = read_c0_entryhi();
  218. page &= (PAGE_MASK << 1);
  219. write_c0_entryhi(page);
  220. mtc0_tlbw_hazard();
  221. tlb_probe();
  222. tlb_probe_hazard();
  223. idx = read_c0_index();
  224. write_c0_entrylo0(0);
  225. write_c0_entrylo1(0);
  226. if (idx >= 0) {
  227. /* Make sure all entries differ. */
  228. write_c0_entryhi(UNIQUE_ENTRYHI(idx));
  229. mtc0_tlbw_hazard();
  230. tlb_write_indexed();
  231. tlbw_use_hazard();
  232. }
  233. write_c0_entryhi(oldpid);
  234. FLUSH_ITLB;
  235. EXIT_CRITICAL(flags);
  236. }
  237. /*
  238. * We will need multiple versions of update_mmu_cache(), one that just
  239. * updates the TLB with the new pte(s), and another which also checks
  240. * for the R4k "end of page" hardware bug and does the needy.
  241. */
  242. void __update_tlb(struct vm_area_struct * vma, unsigned long address, pte_t pte)
  243. {
  244. unsigned long flags;
  245. pgd_t *pgdp;
  246. pud_t *pudp;
  247. pmd_t *pmdp;
  248. pte_t *ptep;
  249. int idx, pid;
  250. /*
  251. * Handle debugger faulting in for debugee.
  252. */
  253. if (current->active_mm != vma->vm_mm)
  254. return;
  255. ENTER_CRITICAL(flags);
  256. pid = read_c0_entryhi() & ASID_MASK;
  257. address &= (PAGE_MASK << 1);
  258. write_c0_entryhi(address | pid);
  259. pgdp = pgd_offset(vma->vm_mm, address);
  260. mtc0_tlbw_hazard();
  261. tlb_probe();
  262. tlb_probe_hazard();
  263. pudp = pud_offset(pgdp, address);
  264. pmdp = pmd_offset(pudp, address);
  265. idx = read_c0_index();
  266. #ifdef CONFIG_HUGETLB_PAGE
  267. /* this could be a huge page */
  268. if (pmd_huge(*pmdp)) {
  269. unsigned long lo;
  270. write_c0_pagemask(PM_HUGE_MASK);
  271. ptep = (pte_t *)pmdp;
  272. lo = pte_to_entrylo(pte_val(*ptep));
  273. write_c0_entrylo0(lo);
  274. write_c0_entrylo1(lo + (HPAGE_SIZE >> 7));
  275. mtc0_tlbw_hazard();
  276. if (idx < 0)
  277. tlb_write_random();
  278. else
  279. tlb_write_indexed();
  280. write_c0_pagemask(PM_DEFAULT_MASK);
  281. } else
  282. #endif
  283. {
  284. ptep = pte_offset_map(pmdp, address);
  285. #if defined(CONFIG_64BIT_PHYS_ADDR) && defined(CONFIG_CPU_MIPS32)
  286. write_c0_entrylo0(ptep->pte_high);
  287. ptep++;
  288. write_c0_entrylo1(ptep->pte_high);
  289. #else
  290. write_c0_entrylo0(pte_to_entrylo(pte_val(*ptep++)));
  291. write_c0_entrylo1(pte_to_entrylo(pte_val(*ptep)));
  292. #endif
  293. mtc0_tlbw_hazard();
  294. if (idx < 0)
  295. tlb_write_random();
  296. else
  297. tlb_write_indexed();
  298. }
  299. tlbw_use_hazard();
  300. FLUSH_ITLB_VM(vma);
  301. EXIT_CRITICAL(flags);
  302. }
  303. void add_wired_entry(unsigned long entrylo0, unsigned long entrylo1,
  304. unsigned long entryhi, unsigned long pagemask)
  305. {
  306. unsigned long flags;
  307. unsigned long wired;
  308. unsigned long old_pagemask;
  309. unsigned long old_ctx;
  310. ENTER_CRITICAL(flags);
  311. /* Save old context and create impossible VPN2 value */
  312. old_ctx = read_c0_entryhi();
  313. old_pagemask = read_c0_pagemask();
  314. wired = read_c0_wired();
  315. write_c0_wired(wired + 1);
  316. write_c0_index(wired);
  317. tlbw_use_hazard(); /* What is the hazard here? */
  318. write_c0_pagemask(pagemask);
  319. write_c0_entryhi(entryhi);
  320. write_c0_entrylo0(entrylo0);
  321. write_c0_entrylo1(entrylo1);
  322. mtc0_tlbw_hazard();
  323. tlb_write_indexed();
  324. tlbw_use_hazard();
  325. write_c0_entryhi(old_ctx);
  326. tlbw_use_hazard(); /* What is the hazard here? */
  327. write_c0_pagemask(old_pagemask);
  328. local_flush_tlb_all();
  329. EXIT_CRITICAL(flags);
  330. }
  331. static int __cpuinitdata ntlb;
  332. static int __init set_ntlb(char *str)
  333. {
  334. get_option(&str, &ntlb);
  335. return 1;
  336. }
  337. __setup("ntlb=", set_ntlb);
  338. void __cpuinit tlb_init(void)
  339. {
  340. /*
  341. * You should never change this register:
  342. * - On R4600 1.7 the tlbp never hits for pages smaller than
  343. * the value in the c0_pagemask register.
  344. * - The entire mm handling assumes the c0_pagemask register to
  345. * be set to fixed-size pages.
  346. */
  347. write_c0_pagemask(PM_DEFAULT_MASK);
  348. write_c0_wired(0);
  349. if (current_cpu_type() == CPU_R10000 ||
  350. current_cpu_type() == CPU_R12000 ||
  351. current_cpu_type() == CPU_R14000)
  352. write_c0_framemask(0);
  353. if (kernel_uses_smartmips_rixi) {
  354. /*
  355. * Enable the no read, no exec bits, and enable large virtual
  356. * address.
  357. */
  358. u32 pg = PG_RIE | PG_XIE;
  359. #ifdef CONFIG_64BIT
  360. pg |= PG_ELPA;
  361. #endif
  362. write_c0_pagegrain(pg);
  363. }
  364. /* From this point on the ARC firmware is dead. */
  365. local_flush_tlb_all();
  366. /* Did I tell you that ARC SUCKS? */
  367. if (ntlb) {
  368. if (ntlb > 1 && ntlb <= current_cpu_data.tlbsize) {
  369. int wired = current_cpu_data.tlbsize - ntlb;
  370. write_c0_wired(wired);
  371. write_c0_index(wired-1);
  372. printk("Restricting TLB to %d entries\n", ntlb);
  373. } else
  374. printk("Ignoring invalid argument ntlb=%d\n", ntlb);
  375. }
  376. build_tlb_refill_handler();
  377. }