tlb-r4k.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451
  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. /*
  47. * LOONGSON2 has a 4 entry itlb which is a subset of dtlb,
  48. * unfortrunately, itlb is not totally transparent to software.
  49. */
  50. static inline void flush_itlb(void)
  51. {
  52. switch (current_cpu_type()) {
  53. case CPU_LOONGSON2:
  54. write_c0_diag(4);
  55. break;
  56. default:
  57. break;
  58. }
  59. }
  60. static inline void flush_itlb_vm(struct vm_area_struct *vma)
  61. {
  62. if (vma->vm_flags & VM_EXEC)
  63. flush_itlb();
  64. }
  65. void local_flush_tlb_all(void)
  66. {
  67. unsigned long flags;
  68. unsigned long old_ctx;
  69. int entry;
  70. ENTER_CRITICAL(flags);
  71. /* Save old context and create impossible VPN2 value */
  72. old_ctx = read_c0_entryhi();
  73. write_c0_entrylo0(0);
  74. write_c0_entrylo1(0);
  75. entry = read_c0_wired();
  76. /* Blast 'em all away. */
  77. while (entry < current_cpu_data.tlbsize) {
  78. /* Make sure all entries differ. */
  79. write_c0_entryhi(UNIQUE_ENTRYHI(entry));
  80. write_c0_index(entry);
  81. mtc0_tlbw_hazard();
  82. tlb_write_indexed();
  83. entry++;
  84. }
  85. tlbw_use_hazard();
  86. write_c0_entryhi(old_ctx);
  87. flush_itlb();
  88. EXIT_CRITICAL(flags);
  89. }
  90. EXPORT_SYMBOL(local_flush_tlb_all);
  91. /* All entries common to a mm share an asid. To effectively flush
  92. these entries, we just bump the asid. */
  93. void local_flush_tlb_mm(struct mm_struct *mm)
  94. {
  95. int cpu;
  96. preempt_disable();
  97. cpu = smp_processor_id();
  98. if (cpu_context(cpu, mm) != 0) {
  99. drop_mmu_context(mm, cpu);
  100. }
  101. preempt_enable();
  102. }
  103. void local_flush_tlb_range(struct vm_area_struct *vma, unsigned long start,
  104. unsigned long end)
  105. {
  106. struct mm_struct *mm = vma->vm_mm;
  107. int cpu = smp_processor_id();
  108. if (cpu_context(cpu, mm) != 0) {
  109. unsigned long size, flags;
  110. ENTER_CRITICAL(flags);
  111. start = round_down(start, PAGE_SIZE << 1);
  112. end = round_up(end, PAGE_SIZE << 1);
  113. size = (end - start) >> (PAGE_SHIFT + 1);
  114. if (size <= current_cpu_data.tlbsize/2) {
  115. int oldpid = read_c0_entryhi();
  116. int newpid = cpu_asid(cpu, mm);
  117. while (start < end) {
  118. int idx;
  119. write_c0_entryhi(start | newpid);
  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_MIPS_HUGE_TLB_SUPPORT
  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. tlbw_use_hazard();
  282. write_c0_pagemask(PM_DEFAULT_MASK);
  283. } else
  284. #endif
  285. {
  286. ptep = pte_offset_map(pmdp, address);
  287. #if defined(CONFIG_64BIT_PHYS_ADDR) && defined(CONFIG_CPU_MIPS32)
  288. write_c0_entrylo0(ptep->pte_high);
  289. ptep++;
  290. write_c0_entrylo1(ptep->pte_high);
  291. #else
  292. write_c0_entrylo0(pte_to_entrylo(pte_val(*ptep++)));
  293. write_c0_entrylo1(pte_to_entrylo(pte_val(*ptep)));
  294. #endif
  295. mtc0_tlbw_hazard();
  296. if (idx < 0)
  297. tlb_write_random();
  298. else
  299. tlb_write_indexed();
  300. }
  301. tlbw_use_hazard();
  302. flush_itlb_vm(vma);
  303. EXIT_CRITICAL(flags);
  304. }
  305. void add_wired_entry(unsigned long entrylo0, unsigned long entrylo1,
  306. unsigned long entryhi, unsigned long pagemask)
  307. {
  308. unsigned long flags;
  309. unsigned long wired;
  310. unsigned long old_pagemask;
  311. unsigned long old_ctx;
  312. ENTER_CRITICAL(flags);
  313. /* Save old context and create impossible VPN2 value */
  314. old_ctx = read_c0_entryhi();
  315. old_pagemask = read_c0_pagemask();
  316. wired = read_c0_wired();
  317. write_c0_wired(wired + 1);
  318. write_c0_index(wired);
  319. tlbw_use_hazard(); /* What is the hazard here? */
  320. write_c0_pagemask(pagemask);
  321. write_c0_entryhi(entryhi);
  322. write_c0_entrylo0(entrylo0);
  323. write_c0_entrylo1(entrylo1);
  324. mtc0_tlbw_hazard();
  325. tlb_write_indexed();
  326. tlbw_use_hazard();
  327. write_c0_entryhi(old_ctx);
  328. tlbw_use_hazard(); /* What is the hazard here? */
  329. write_c0_pagemask(old_pagemask);
  330. local_flush_tlb_all();
  331. EXIT_CRITICAL(flags);
  332. }
  333. #ifdef CONFIG_TRANSPARENT_HUGEPAGE
  334. int __init has_transparent_hugepage(void)
  335. {
  336. unsigned int mask;
  337. unsigned long flags;
  338. ENTER_CRITICAL(flags);
  339. write_c0_pagemask(PM_HUGE_MASK);
  340. back_to_back_c0_hazard();
  341. mask = read_c0_pagemask();
  342. write_c0_pagemask(PM_DEFAULT_MASK);
  343. EXIT_CRITICAL(flags);
  344. return mask == PM_HUGE_MASK;
  345. }
  346. #endif /* CONFIG_TRANSPARENT_HUGEPAGE */
  347. static int ntlb;
  348. static int __init set_ntlb(char *str)
  349. {
  350. get_option(&str, &ntlb);
  351. return 1;
  352. }
  353. __setup("ntlb=", set_ntlb);
  354. void tlb_init(void)
  355. {
  356. /*
  357. * You should never change this register:
  358. * - On R4600 1.7 the tlbp never hits for pages smaller than
  359. * the value in the c0_pagemask register.
  360. * - The entire mm handling assumes the c0_pagemask register to
  361. * be set to fixed-size pages.
  362. */
  363. write_c0_pagemask(PM_DEFAULT_MASK);
  364. write_c0_wired(0);
  365. if (current_cpu_type() == CPU_R10000 ||
  366. current_cpu_type() == CPU_R12000 ||
  367. current_cpu_type() == CPU_R14000)
  368. write_c0_framemask(0);
  369. if (cpu_has_rixi) {
  370. /*
  371. * Enable the no read, no exec bits, and enable large virtual
  372. * address.
  373. */
  374. u32 pg = PG_RIE | PG_XIE;
  375. #ifdef CONFIG_64BIT
  376. pg |= PG_ELPA;
  377. #endif
  378. write_c0_pagegrain(pg);
  379. }
  380. /* From this point on the ARC firmware is dead. */
  381. local_flush_tlb_all();
  382. /* Did I tell you that ARC SUCKS? */
  383. if (ntlb) {
  384. if (ntlb > 1 && ntlb <= current_cpu_data.tlbsize) {
  385. int wired = current_cpu_data.tlbsize - ntlb;
  386. write_c0_wired(wired);
  387. write_c0_index(wired-1);
  388. printk("Restricting TLB to %d entries\n", ntlb);
  389. } else
  390. printk("Ignoring invalid argument ntlb=%d\n", ntlb);
  391. }
  392. build_tlb_refill_handler();
  393. }