tlb-r4k.c 12 KB

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