tlb-r4k.c 11 KB

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