tlb-r4k.c 10 KB

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