tlb-r4k.c 11 KB

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