tlb-r4k.c 11 KB

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