tlb-r4k.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555
  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. htw_stop();
  270. pid = read_c0_entryhi() & ASID_MASK;
  271. address &= (PAGE_MASK << 1);
  272. write_c0_entryhi(address | pid);
  273. pgdp = pgd_offset(vma->vm_mm, address);
  274. mtc0_tlbw_hazard();
  275. tlb_probe();
  276. tlb_probe_hazard();
  277. pudp = pud_offset(pgdp, address);
  278. pmdp = pmd_offset(pudp, address);
  279. idx = read_c0_index();
  280. #ifdef CONFIG_MIPS_HUGE_TLB_SUPPORT
  281. /* this could be a huge page */
  282. if (pmd_huge(*pmdp)) {
  283. unsigned long lo;
  284. write_c0_pagemask(PM_HUGE_MASK);
  285. ptep = (pte_t *)pmdp;
  286. lo = pte_to_entrylo(pte_val(*ptep));
  287. write_c0_entrylo0(lo);
  288. write_c0_entrylo1(lo + (HPAGE_SIZE >> 7));
  289. mtc0_tlbw_hazard();
  290. if (idx < 0)
  291. tlb_write_random();
  292. else
  293. tlb_write_indexed();
  294. tlbw_use_hazard();
  295. write_c0_pagemask(PM_DEFAULT_MASK);
  296. } else
  297. #endif
  298. {
  299. ptep = pte_offset_map(pmdp, address);
  300. #if defined(CONFIG_PHYS_ADDR_T_64BIT) && defined(CONFIG_CPU_MIPS32)
  301. #ifdef CONFIG_XPA
  302. write_c0_entrylo0(pte_to_entrylo(ptep->pte_high));
  303. writex_c0_entrylo0(ptep->pte_low & _PFNX_MASK);
  304. ptep++;
  305. write_c0_entrylo1(pte_to_entrylo(ptep->pte_high));
  306. writex_c0_entrylo1(ptep->pte_low & _PFNX_MASK);
  307. #else
  308. write_c0_entrylo0(ptep->pte_high);
  309. ptep++;
  310. write_c0_entrylo1(ptep->pte_high);
  311. #endif
  312. #else
  313. write_c0_entrylo0(pte_to_entrylo(pte_val(*ptep++)));
  314. write_c0_entrylo1(pte_to_entrylo(pte_val(*ptep)));
  315. #endif
  316. mtc0_tlbw_hazard();
  317. if (idx < 0)
  318. tlb_write_random();
  319. else
  320. tlb_write_indexed();
  321. }
  322. tlbw_use_hazard();
  323. htw_start();
  324. flush_itlb_vm(vma);
  325. local_irq_restore(flags);
  326. }
  327. void add_wired_entry(unsigned long entrylo0, unsigned long entrylo1,
  328. unsigned long entryhi, unsigned long pagemask)
  329. {
  330. #ifdef CONFIG_XPA
  331. panic("Broken for XPA kernels");
  332. #else
  333. unsigned long flags;
  334. unsigned long wired;
  335. unsigned long old_pagemask;
  336. unsigned long old_ctx;
  337. local_irq_save(flags);
  338. /* Save old context and create impossible VPN2 value */
  339. old_ctx = read_c0_entryhi();
  340. htw_stop();
  341. old_pagemask = read_c0_pagemask();
  342. wired = read_c0_wired();
  343. write_c0_wired(wired + 1);
  344. write_c0_index(wired);
  345. tlbw_use_hazard(); /* What is the hazard here? */
  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. tlbw_use_hazard(); /* What is the hazard here? */
  355. htw_start();
  356. write_c0_pagemask(old_pagemask);
  357. local_flush_tlb_all();
  358. local_irq_restore(flags);
  359. #endif
  360. }
  361. #ifdef CONFIG_TRANSPARENT_HUGEPAGE
  362. int __init has_transparent_hugepage(void)
  363. {
  364. unsigned int mask;
  365. unsigned long flags;
  366. local_irq_save(flags);
  367. write_c0_pagemask(PM_HUGE_MASK);
  368. back_to_back_c0_hazard();
  369. mask = read_c0_pagemask();
  370. write_c0_pagemask(PM_DEFAULT_MASK);
  371. local_irq_restore(flags);
  372. return mask == PM_HUGE_MASK;
  373. }
  374. #endif /* CONFIG_TRANSPARENT_HUGEPAGE */
  375. /*
  376. * Used for loading TLB entries before trap_init() has started, when we
  377. * don't actually want to add a wired entry which remains throughout the
  378. * lifetime of the system
  379. */
  380. int temp_tlb_entry;
  381. __init int add_temporary_entry(unsigned long entrylo0, unsigned long entrylo1,
  382. unsigned long entryhi, unsigned long pagemask)
  383. {
  384. int ret = 0;
  385. unsigned long flags;
  386. unsigned long wired;
  387. unsigned long old_pagemask;
  388. unsigned long old_ctx;
  389. local_irq_save(flags);
  390. /* Save old context and create impossible VPN2 value */
  391. htw_stop();
  392. old_ctx = read_c0_entryhi();
  393. old_pagemask = read_c0_pagemask();
  394. wired = read_c0_wired();
  395. if (--temp_tlb_entry < wired) {
  396. printk(KERN_WARNING
  397. "No TLB space left for add_temporary_entry\n");
  398. ret = -ENOSPC;
  399. goto out;
  400. }
  401. write_c0_index(temp_tlb_entry);
  402. write_c0_pagemask(pagemask);
  403. write_c0_entryhi(entryhi);
  404. write_c0_entrylo0(entrylo0);
  405. write_c0_entrylo1(entrylo1);
  406. mtc0_tlbw_hazard();
  407. tlb_write_indexed();
  408. tlbw_use_hazard();
  409. write_c0_entryhi(old_ctx);
  410. write_c0_pagemask(old_pagemask);
  411. htw_start();
  412. out:
  413. local_irq_restore(flags);
  414. return ret;
  415. }
  416. static int ntlb;
  417. static int __init set_ntlb(char *str)
  418. {
  419. get_option(&str, &ntlb);
  420. return 1;
  421. }
  422. __setup("ntlb=", set_ntlb);
  423. /*
  424. * Configure TLB (for init or after a CPU has been powered off).
  425. */
  426. static void r4k_tlb_configure(void)
  427. {
  428. /*
  429. * You should never change this register:
  430. * - On R4600 1.7 the tlbp never hits for pages smaller than
  431. * the value in the c0_pagemask register.
  432. * - The entire mm handling assumes the c0_pagemask register to
  433. * be set to fixed-size pages.
  434. */
  435. write_c0_pagemask(PM_DEFAULT_MASK);
  436. write_c0_wired(0);
  437. if (current_cpu_type() == CPU_R10000 ||
  438. current_cpu_type() == CPU_R12000 ||
  439. current_cpu_type() == CPU_R14000 ||
  440. current_cpu_type() == CPU_R16000)
  441. write_c0_framemask(0);
  442. if (cpu_has_rixi) {
  443. /*
  444. * Enable the no read, no exec bits, and enable large physical
  445. * address.
  446. */
  447. #ifdef CONFIG_64BIT
  448. set_c0_pagegrain(PG_RIE | PG_XIE | PG_ELPA);
  449. #else
  450. set_c0_pagegrain(PG_RIE | PG_XIE);
  451. #endif
  452. }
  453. temp_tlb_entry = current_cpu_data.tlbsize - 1;
  454. /* From this point on the ARC firmware is dead. */
  455. local_flush_tlb_all();
  456. /* Did I tell you that ARC SUCKS? */
  457. }
  458. void tlb_init(void)
  459. {
  460. r4k_tlb_configure();
  461. if (ntlb) {
  462. if (ntlb > 1 && ntlb <= current_cpu_data.tlbsize) {
  463. int wired = current_cpu_data.tlbsize - ntlb;
  464. write_c0_wired(wired);
  465. write_c0_index(wired-1);
  466. printk("Restricting TLB to %d entries\n", ntlb);
  467. } else
  468. printk("Ignoring invalid argument ntlb=%d\n", ntlb);
  469. }
  470. build_tlb_refill_handler();
  471. }
  472. static int r4k_tlb_pm_notifier(struct notifier_block *self, unsigned long cmd,
  473. void *v)
  474. {
  475. switch (cmd) {
  476. case CPU_PM_ENTER_FAILED:
  477. case CPU_PM_EXIT:
  478. r4k_tlb_configure();
  479. break;
  480. }
  481. return NOTIFY_OK;
  482. }
  483. static struct notifier_block r4k_tlb_pm_notifier_block = {
  484. .notifier_call = r4k_tlb_pm_notifier,
  485. };
  486. static int __init r4k_tlb_init_pm(void)
  487. {
  488. return cpu_pm_register_notifier(&r4k_tlb_pm_notifier_block);
  489. }
  490. arch_initcall(r4k_tlb_init_pm);