tlb-r4k.c 12 KB

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