tlb-r4k.c 11 KB

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