tlb-r4k.c 12 KB

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