tlb-r4k.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523
  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_val(*ptep) >> 6;
  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_val(*ptep++) >> 6);
  283. write_c0_entrylo1(pte_val(*ptep) >> 6);
  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. #if 0
  296. static void r4k_update_mmu_cache_hwbug(struct vm_area_struct * vma,
  297. unsigned long address, pte_t pte)
  298. {
  299. unsigned long flags;
  300. unsigned int asid;
  301. pgd_t *pgdp;
  302. pmd_t *pmdp;
  303. pte_t *ptep;
  304. int idx;
  305. ENTER_CRITICAL(flags);
  306. address &= (PAGE_MASK << 1);
  307. asid = read_c0_entryhi() & ASID_MASK;
  308. write_c0_entryhi(address | asid);
  309. pgdp = pgd_offset(vma->vm_mm, address);
  310. mtc0_tlbw_hazard();
  311. tlb_probe();
  312. tlb_probe_hazard();
  313. pmdp = pmd_offset(pgdp, address);
  314. idx = read_c0_index();
  315. ptep = pte_offset_map(pmdp, address);
  316. write_c0_entrylo0(pte_val(*ptep++) >> 6);
  317. write_c0_entrylo1(pte_val(*ptep) >> 6);
  318. mtc0_tlbw_hazard();
  319. if (idx < 0)
  320. tlb_write_random();
  321. else
  322. tlb_write_indexed();
  323. tlbw_use_hazard();
  324. EXIT_CRITICAL(flags);
  325. }
  326. #endif
  327. void __init add_wired_entry(unsigned long entrylo0, unsigned long entrylo1,
  328. unsigned long entryhi, unsigned long pagemask)
  329. {
  330. unsigned long flags;
  331. unsigned long wired;
  332. unsigned long old_pagemask;
  333. unsigned long old_ctx;
  334. ENTER_CRITICAL(flags);
  335. /* Save old context and create impossible VPN2 value */
  336. old_ctx = read_c0_entryhi();
  337. old_pagemask = read_c0_pagemask();
  338. wired = read_c0_wired();
  339. write_c0_wired(wired + 1);
  340. write_c0_index(wired);
  341. tlbw_use_hazard(); /* What is the hazard here? */
  342. write_c0_pagemask(pagemask);
  343. write_c0_entryhi(entryhi);
  344. write_c0_entrylo0(entrylo0);
  345. write_c0_entrylo1(entrylo1);
  346. mtc0_tlbw_hazard();
  347. tlb_write_indexed();
  348. tlbw_use_hazard();
  349. write_c0_entryhi(old_ctx);
  350. tlbw_use_hazard(); /* What is the hazard here? */
  351. write_c0_pagemask(old_pagemask);
  352. local_flush_tlb_all();
  353. EXIT_CRITICAL(flags);
  354. }
  355. /*
  356. * Used for loading TLB entries before trap_init() has started, when we
  357. * don't actually want to add a wired entry which remains throughout the
  358. * lifetime of the system
  359. */
  360. static int temp_tlb_entry __cpuinitdata;
  361. __init int add_temporary_entry(unsigned long entrylo0, unsigned long entrylo1,
  362. unsigned long entryhi, unsigned long pagemask)
  363. {
  364. int ret = 0;
  365. unsigned long flags;
  366. unsigned long wired;
  367. unsigned long old_pagemask;
  368. unsigned long old_ctx;
  369. ENTER_CRITICAL(flags);
  370. /* Save old context and create impossible VPN2 value */
  371. old_ctx = read_c0_entryhi();
  372. old_pagemask = read_c0_pagemask();
  373. wired = read_c0_wired();
  374. if (--temp_tlb_entry < wired) {
  375. printk(KERN_WARNING
  376. "No TLB space left for add_temporary_entry\n");
  377. ret = -ENOSPC;
  378. goto out;
  379. }
  380. write_c0_index(temp_tlb_entry);
  381. write_c0_pagemask(pagemask);
  382. write_c0_entryhi(entryhi);
  383. write_c0_entrylo0(entrylo0);
  384. write_c0_entrylo1(entrylo1);
  385. mtc0_tlbw_hazard();
  386. tlb_write_indexed();
  387. tlbw_use_hazard();
  388. write_c0_entryhi(old_ctx);
  389. write_c0_pagemask(old_pagemask);
  390. out:
  391. EXIT_CRITICAL(flags);
  392. return ret;
  393. }
  394. static void __cpuinit probe_tlb(unsigned long config)
  395. {
  396. struct cpuinfo_mips *c = &current_cpu_data;
  397. unsigned int reg;
  398. /*
  399. * If this isn't a MIPS32 / MIPS64 compliant CPU. Config 1 register
  400. * is not supported, we assume R4k style. Cpu probing already figured
  401. * out the number of tlb entries.
  402. */
  403. if ((c->processor_id & 0xff0000) == PRID_COMP_LEGACY)
  404. return;
  405. #ifdef CONFIG_MIPS_MT_SMTC
  406. /*
  407. * If TLB is shared in SMTC system, total size already
  408. * has been calculated and written into cpu_data tlbsize
  409. */
  410. if((smtc_status & SMTC_TLB_SHARED) == SMTC_TLB_SHARED)
  411. return;
  412. #endif /* CONFIG_MIPS_MT_SMTC */
  413. reg = read_c0_config1();
  414. if (!((config >> 7) & 3))
  415. panic("No TLB present");
  416. c->tlbsize = ((reg >> 25) & 0x3f) + 1;
  417. }
  418. static int __cpuinitdata ntlb;
  419. static int __init set_ntlb(char *str)
  420. {
  421. get_option(&str, &ntlb);
  422. return 1;
  423. }
  424. __setup("ntlb=", set_ntlb);
  425. void __cpuinit tlb_init(void)
  426. {
  427. unsigned int config = read_c0_config();
  428. /*
  429. * You should never change this register:
  430. * - On R4600 1.7 the tlbp never hits for pages smaller than
  431. * the value in the c0_pagemask register.
  432. * - The entire mm handling assumes the c0_pagemask register to
  433. * be set to fixed-size pages.
  434. */
  435. probe_tlb(config);
  436. write_c0_pagemask(PM_DEFAULT_MASK);
  437. write_c0_wired(0);
  438. if (current_cpu_type() == CPU_R10000 ||
  439. current_cpu_type() == CPU_R12000 ||
  440. current_cpu_type() == CPU_R14000)
  441. write_c0_framemask(0);
  442. temp_tlb_entry = current_cpu_data.tlbsize - 1;
  443. /* From this point on the ARC firmware is dead. */
  444. local_flush_tlb_all();
  445. /* Did I tell you that ARC SUCKS? */
  446. if (ntlb) {
  447. if (ntlb > 1 && ntlb <= current_cpu_data.tlbsize) {
  448. int wired = current_cpu_data.tlbsize - ntlb;
  449. write_c0_wired(wired);
  450. write_c0_index(wired-1);
  451. printk("Restricting TLB to %d entries\n", ntlb);
  452. } else
  453. printk("Ignoring invalid argument ntlb=%d\n", ntlb);
  454. }
  455. build_tlb_refill_handler();
  456. }