tlb-r4k.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478
  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 (davem@davemloft.net)
  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/cpu_pm.h>
  12. #include <linux/init.h>
  13. #include <linux/sched.h>
  14. #include <linux/smp.h>
  15. #include <linux/mm.h>
  16. #include <linux/hugetlb.h>
  17. #include <linux/module.h>
  18. #include <asm/cpu.h>
  19. #include <asm/cpu-type.h>
  20. #include <asm/bootinfo.h>
  21. #include <asm/mmu_context.h>
  22. #include <asm/pgtable.h>
  23. #include <asm/tlb.h>
  24. #include <asm/tlbmisc.h>
  25. extern void build_tlb_refill_handler(void);
  26. /*
  27. * LOONGSON2/3 has a 4 entry itlb which is a subset of dtlb,
  28. * unfortunately, itlb is not totally transparent to software.
  29. */
  30. static inline void flush_itlb(void)
  31. {
  32. switch (current_cpu_type()) {
  33. case CPU_LOONGSON2:
  34. case CPU_LOONGSON3:
  35. write_c0_diag(4);
  36. break;
  37. default:
  38. break;
  39. }
  40. }
  41. static inline void flush_itlb_vm(struct vm_area_struct *vma)
  42. {
  43. if (vma->vm_flags & VM_EXEC)
  44. flush_itlb();
  45. }
  46. void local_flush_tlb_all(void)
  47. {
  48. unsigned long flags;
  49. unsigned long old_ctx;
  50. int entry, ftlbhighset;
  51. local_irq_save(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. if (cpu_has_tlbinv) {
  59. if (current_cpu_data.tlbsizevtlb) {
  60. write_c0_index(0);
  61. mtc0_tlbw_hazard();
  62. tlbinvf(); /* invalidate VTLB */
  63. }
  64. ftlbhighset = current_cpu_data.tlbsizevtlb +
  65. current_cpu_data.tlbsizeftlbsets;
  66. for (entry = current_cpu_data.tlbsizevtlb;
  67. entry < ftlbhighset;
  68. entry++) {
  69. write_c0_index(entry);
  70. mtc0_tlbw_hazard();
  71. tlbinvf(); /* invalidate one FTLB set */
  72. }
  73. } else {
  74. while (entry < current_cpu_data.tlbsize) {
  75. /* Make sure all entries differ. */
  76. write_c0_entryhi(UNIQUE_ENTRYHI(entry));
  77. write_c0_index(entry);
  78. mtc0_tlbw_hazard();
  79. tlb_write_indexed();
  80. entry++;
  81. }
  82. }
  83. tlbw_use_hazard();
  84. write_c0_entryhi(old_ctx);
  85. flush_itlb();
  86. local_irq_restore(flags);
  87. }
  88. EXPORT_SYMBOL(local_flush_tlb_all);
  89. /* All entries common to a mm share an asid. To effectively flush
  90. these entries, we just bump the asid. */
  91. void local_flush_tlb_mm(struct mm_struct *mm)
  92. {
  93. int cpu;
  94. preempt_disable();
  95. cpu = smp_processor_id();
  96. if (cpu_context(cpu, mm) != 0) {
  97. drop_mmu_context(mm, cpu);
  98. }
  99. preempt_enable();
  100. }
  101. void local_flush_tlb_range(struct vm_area_struct *vma, unsigned long start,
  102. unsigned long end)
  103. {
  104. struct mm_struct *mm = vma->vm_mm;
  105. int cpu = smp_processor_id();
  106. if (cpu_context(cpu, mm) != 0) {
  107. unsigned long size, flags;
  108. local_irq_save(flags);
  109. start = round_down(start, PAGE_SIZE << 1);
  110. end = round_up(end, PAGE_SIZE << 1);
  111. size = (end - start) >> (PAGE_SHIFT + 1);
  112. if (size <= (current_cpu_data.tlbsizeftlbsets ?
  113. current_cpu_data.tlbsize / 8 :
  114. current_cpu_data.tlbsize / 2)) {
  115. int oldpid = read_c0_entryhi();
  116. int newpid = cpu_asid(cpu, mm);
  117. while (start < end) {
  118. int idx;
  119. write_c0_entryhi(start | newpid);
  120. start += (PAGE_SIZE << 1);
  121. mtc0_tlbw_hazard();
  122. tlb_probe();
  123. tlb_probe_hazard();
  124. idx = read_c0_index();
  125. write_c0_entrylo0(0);
  126. write_c0_entrylo1(0);
  127. if (idx < 0)
  128. continue;
  129. /* Make sure all entries differ. */
  130. write_c0_entryhi(UNIQUE_ENTRYHI(idx));
  131. mtc0_tlbw_hazard();
  132. tlb_write_indexed();
  133. }
  134. tlbw_use_hazard();
  135. write_c0_entryhi(oldpid);
  136. } else {
  137. drop_mmu_context(mm, cpu);
  138. }
  139. flush_itlb();
  140. local_irq_restore(flags);
  141. }
  142. }
  143. void local_flush_tlb_kernel_range(unsigned long start, unsigned long end)
  144. {
  145. unsigned long size, flags;
  146. local_irq_save(flags);
  147. size = (end - start + (PAGE_SIZE - 1)) >> PAGE_SHIFT;
  148. size = (size + 1) >> 1;
  149. if (size <= (current_cpu_data.tlbsizeftlbsets ?
  150. current_cpu_data.tlbsize / 8 :
  151. current_cpu_data.tlbsize / 2)) {
  152. int pid = read_c0_entryhi();
  153. start &= (PAGE_MASK << 1);
  154. end += ((PAGE_SIZE << 1) - 1);
  155. end &= (PAGE_MASK << 1);
  156. while (start < end) {
  157. int idx;
  158. write_c0_entryhi(start);
  159. start += (PAGE_SIZE << 1);
  160. mtc0_tlbw_hazard();
  161. tlb_probe();
  162. tlb_probe_hazard();
  163. idx = read_c0_index();
  164. write_c0_entrylo0(0);
  165. write_c0_entrylo1(0);
  166. if (idx < 0)
  167. continue;
  168. /* Make sure all entries differ. */
  169. write_c0_entryhi(UNIQUE_ENTRYHI(idx));
  170. mtc0_tlbw_hazard();
  171. tlb_write_indexed();
  172. }
  173. tlbw_use_hazard();
  174. write_c0_entryhi(pid);
  175. } else {
  176. local_flush_tlb_all();
  177. }
  178. flush_itlb();
  179. local_irq_restore(flags);
  180. }
  181. void local_flush_tlb_page(struct vm_area_struct *vma, unsigned long page)
  182. {
  183. int cpu = smp_processor_id();
  184. if (cpu_context(cpu, vma->vm_mm) != 0) {
  185. unsigned long flags;
  186. int oldpid, newpid, idx;
  187. newpid = cpu_asid(cpu, vma->vm_mm);
  188. page &= (PAGE_MASK << 1);
  189. local_irq_save(flags);
  190. oldpid = read_c0_entryhi();
  191. write_c0_entryhi(page | newpid);
  192. mtc0_tlbw_hazard();
  193. tlb_probe();
  194. tlb_probe_hazard();
  195. idx = read_c0_index();
  196. write_c0_entrylo0(0);
  197. write_c0_entrylo1(0);
  198. if (idx < 0)
  199. goto finish;
  200. /* Make sure all entries differ. */
  201. write_c0_entryhi(UNIQUE_ENTRYHI(idx));
  202. mtc0_tlbw_hazard();
  203. tlb_write_indexed();
  204. tlbw_use_hazard();
  205. finish:
  206. write_c0_entryhi(oldpid);
  207. flush_itlb_vm(vma);
  208. local_irq_restore(flags);
  209. }
  210. }
  211. /*
  212. * This one is only used for pages with the global bit set so we don't care
  213. * much about the ASID.
  214. */
  215. void local_flush_tlb_one(unsigned long page)
  216. {
  217. unsigned long flags;
  218. int oldpid, idx;
  219. local_irq_save(flags);
  220. oldpid = read_c0_entryhi();
  221. page &= (PAGE_MASK << 1);
  222. write_c0_entryhi(page);
  223. mtc0_tlbw_hazard();
  224. tlb_probe();
  225. tlb_probe_hazard();
  226. idx = read_c0_index();
  227. write_c0_entrylo0(0);
  228. write_c0_entrylo1(0);
  229. if (idx >= 0) {
  230. /* Make sure all entries differ. */
  231. write_c0_entryhi(UNIQUE_ENTRYHI(idx));
  232. mtc0_tlbw_hazard();
  233. tlb_write_indexed();
  234. tlbw_use_hazard();
  235. }
  236. write_c0_entryhi(oldpid);
  237. flush_itlb();
  238. local_irq_restore(flags);
  239. }
  240. /*
  241. * We will need multiple versions of update_mmu_cache(), one that just
  242. * updates the TLB with the new pte(s), and another which also checks
  243. * for the R4k "end of page" hardware bug and does the needy.
  244. */
  245. void __update_tlb(struct vm_area_struct * vma, unsigned long address, pte_t pte)
  246. {
  247. unsigned long flags;
  248. pgd_t *pgdp;
  249. pud_t *pudp;
  250. pmd_t *pmdp;
  251. pte_t *ptep;
  252. int idx, pid;
  253. /*
  254. * Handle debugger faulting in for debugee.
  255. */
  256. if (current->active_mm != vma->vm_mm)
  257. return;
  258. local_irq_save(flags);
  259. pid = read_c0_entryhi() & ASID_MASK;
  260. address &= (PAGE_MASK << 1);
  261. write_c0_entryhi(address | pid);
  262. pgdp = pgd_offset(vma->vm_mm, address);
  263. mtc0_tlbw_hazard();
  264. tlb_probe();
  265. tlb_probe_hazard();
  266. pudp = pud_offset(pgdp, address);
  267. pmdp = pmd_offset(pudp, address);
  268. idx = read_c0_index();
  269. #ifdef CONFIG_MIPS_HUGE_TLB_SUPPORT
  270. /* this could be a huge page */
  271. if (pmd_huge(*pmdp)) {
  272. unsigned long lo;
  273. write_c0_pagemask(PM_HUGE_MASK);
  274. ptep = (pte_t *)pmdp;
  275. lo = pte_to_entrylo(pte_val(*ptep));
  276. write_c0_entrylo0(lo);
  277. write_c0_entrylo1(lo + (HPAGE_SIZE >> 7));
  278. mtc0_tlbw_hazard();
  279. if (idx < 0)
  280. tlb_write_random();
  281. else
  282. tlb_write_indexed();
  283. tlbw_use_hazard();
  284. write_c0_pagemask(PM_DEFAULT_MASK);
  285. } else
  286. #endif
  287. {
  288. ptep = pte_offset_map(pmdp, address);
  289. #if defined(CONFIG_64BIT_PHYS_ADDR) && defined(CONFIG_CPU_MIPS32)
  290. write_c0_entrylo0(ptep->pte_high);
  291. ptep++;
  292. write_c0_entrylo1(ptep->pte_high);
  293. #else
  294. write_c0_entrylo0(pte_to_entrylo(pte_val(*ptep++)));
  295. write_c0_entrylo1(pte_to_entrylo(pte_val(*ptep)));
  296. #endif
  297. mtc0_tlbw_hazard();
  298. if (idx < 0)
  299. tlb_write_random();
  300. else
  301. tlb_write_indexed();
  302. }
  303. tlbw_use_hazard();
  304. flush_itlb_vm(vma);
  305. local_irq_restore(flags);
  306. }
  307. void add_wired_entry(unsigned long entrylo0, unsigned long entrylo1,
  308. unsigned long entryhi, unsigned long pagemask)
  309. {
  310. unsigned long flags;
  311. unsigned long wired;
  312. unsigned long old_pagemask;
  313. unsigned long old_ctx;
  314. local_irq_save(flags);
  315. /* Save old context and create impossible VPN2 value */
  316. old_ctx = read_c0_entryhi();
  317. old_pagemask = read_c0_pagemask();
  318. wired = read_c0_wired();
  319. write_c0_wired(wired + 1);
  320. write_c0_index(wired);
  321. tlbw_use_hazard(); /* What is the hazard here? */
  322. write_c0_pagemask(pagemask);
  323. write_c0_entryhi(entryhi);
  324. write_c0_entrylo0(entrylo0);
  325. write_c0_entrylo1(entrylo1);
  326. mtc0_tlbw_hazard();
  327. tlb_write_indexed();
  328. tlbw_use_hazard();
  329. write_c0_entryhi(old_ctx);
  330. tlbw_use_hazard(); /* What is the hazard here? */
  331. write_c0_pagemask(old_pagemask);
  332. local_flush_tlb_all();
  333. local_irq_restore(flags);
  334. }
  335. #ifdef CONFIG_TRANSPARENT_HUGEPAGE
  336. int __init has_transparent_hugepage(void)
  337. {
  338. unsigned int mask;
  339. unsigned long flags;
  340. local_irq_save(flags);
  341. write_c0_pagemask(PM_HUGE_MASK);
  342. back_to_back_c0_hazard();
  343. mask = read_c0_pagemask();
  344. write_c0_pagemask(PM_DEFAULT_MASK);
  345. local_irq_restore(flags);
  346. return mask == PM_HUGE_MASK;
  347. }
  348. #endif /* CONFIG_TRANSPARENT_HUGEPAGE */
  349. static int ntlb;
  350. static int __init set_ntlb(char *str)
  351. {
  352. get_option(&str, &ntlb);
  353. return 1;
  354. }
  355. __setup("ntlb=", set_ntlb);
  356. /*
  357. * Configure TLB (for init or after a CPU has been powered off).
  358. */
  359. static void r4k_tlb_configure(void)
  360. {
  361. /*
  362. * You should never change this register:
  363. * - On R4600 1.7 the tlbp never hits for pages smaller than
  364. * the value in the c0_pagemask register.
  365. * - The entire mm handling assumes the c0_pagemask register to
  366. * be set to fixed-size pages.
  367. */
  368. write_c0_pagemask(PM_DEFAULT_MASK);
  369. write_c0_wired(0);
  370. if (current_cpu_type() == CPU_R10000 ||
  371. current_cpu_type() == CPU_R12000 ||
  372. current_cpu_type() == CPU_R14000)
  373. write_c0_framemask(0);
  374. if (cpu_has_rixi) {
  375. /*
  376. * Enable the no read, no exec bits, and enable large virtual
  377. * address.
  378. */
  379. u32 pg = PG_RIE | PG_XIE;
  380. #ifdef CONFIG_64BIT
  381. pg |= PG_ELPA;
  382. #endif
  383. write_c0_pagegrain(pg);
  384. }
  385. /* From this point on the ARC firmware is dead. */
  386. local_flush_tlb_all();
  387. /* Did I tell you that ARC SUCKS? */
  388. }
  389. void tlb_init(void)
  390. {
  391. r4k_tlb_configure();
  392. if (ntlb) {
  393. if (ntlb > 1 && ntlb <= current_cpu_data.tlbsize) {
  394. int wired = current_cpu_data.tlbsize - ntlb;
  395. write_c0_wired(wired);
  396. write_c0_index(wired-1);
  397. printk("Restricting TLB to %d entries\n", ntlb);
  398. } else
  399. printk("Ignoring invalid argument ntlb=%d\n", ntlb);
  400. }
  401. build_tlb_refill_handler();
  402. }
  403. static int r4k_tlb_pm_notifier(struct notifier_block *self, unsigned long cmd,
  404. void *v)
  405. {
  406. switch (cmd) {
  407. case CPU_PM_ENTER_FAILED:
  408. case CPU_PM_EXIT:
  409. r4k_tlb_configure();
  410. break;
  411. }
  412. return NOTIFY_OK;
  413. }
  414. static struct notifier_block r4k_tlb_pm_notifier_block = {
  415. .notifier_call = r4k_tlb_pm_notifier,
  416. };
  417. static int __init r4k_tlb_init_pm(void)
  418. {
  419. return cpu_pm_register_notifier(&r4k_tlb_pm_notifier_block);
  420. }
  421. arch_initcall(r4k_tlb_init_pm);