tlb-r4k.c 13 KB

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