tlb-r4k.c 11 KB

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