tlb-r4k.c 11 KB

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