tlb-r4k.c 12 KB

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