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