tlb-r4k.c 13 KB

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