PageRenderTime 436ms CodeModel.GetById 27ms RepoModel.GetById 2ms app.codeStats 0ms

/arch/mips/mm/tlb-r4k.c

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