tlb-r4k.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862
  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/memblock.h>
  16. #include <linux/minmax.h>
  17. #include <linux/mm.h>
  18. #include <linux/hugetlb.h>
  19. #include <linux/export.h>
  20. #include <linux/sort.h>
  21. #include <asm/cpu.h>
  22. #include <asm/cpu-type.h>
  23. #include <asm/bootinfo.h>
  24. #include <asm/hazards.h>
  25. #include <asm/mmu_context.h>
  26. #include <asm/tlb.h>
  27. #include <asm/tlbdebug.h>
  28. #include <asm/tlbex.h>
  29. #include <asm/tlbmisc.h>
  30. #include <asm/setup.h>
  31. /*
  32. * LOONGSON-2 has a 4 entry itlb which is a subset of jtlb, LOONGSON-3 has
  33. * a 4 entry itlb and a 4 entry dtlb which are subsets of jtlb. Unfortunately,
  34. * itlb/dtlb are not totally transparent to software.
  35. */
  36. static inline void flush_micro_tlb(void)
  37. {
  38. switch (current_cpu_type()) {
  39. case CPU_LOONGSON2EF:
  40. write_c0_diag(LOONGSON_DIAG_ITLB);
  41. break;
  42. case CPU_LOONGSON64:
  43. write_c0_diag(LOONGSON_DIAG_ITLB | LOONGSON_DIAG_DTLB);
  44. break;
  45. default:
  46. break;
  47. }
  48. }
  49. static inline void flush_micro_tlb_vm(struct vm_area_struct *vma)
  50. {
  51. if (vma->vm_flags & VM_EXEC)
  52. flush_micro_tlb();
  53. }
  54. void local_flush_tlb_all(void)
  55. {
  56. unsigned long flags;
  57. unsigned long old_ctx;
  58. int entry, ftlbhighset;
  59. local_irq_save(flags);
  60. /* Save old context and create impossible VPN2 value */
  61. old_ctx = read_c0_entryhi();
  62. htw_stop();
  63. write_c0_entrylo0(0);
  64. write_c0_entrylo1(0);
  65. entry = num_wired_entries();
  66. /*
  67. * Blast 'em all away.
  68. * If there are any wired entries, fall back to iterating
  69. */
  70. if (cpu_has_tlbinv && !entry) {
  71. if (current_cpu_data.tlbsizevtlb) {
  72. write_c0_index(0);
  73. mtc0_tlbw_hazard();
  74. tlbinvf(); /* invalidate VTLB */
  75. }
  76. ftlbhighset = current_cpu_data.tlbsizevtlb +
  77. current_cpu_data.tlbsizeftlbsets;
  78. for (entry = current_cpu_data.tlbsizevtlb;
  79. entry < ftlbhighset;
  80. entry++) {
  81. write_c0_index(entry);
  82. mtc0_tlbw_hazard();
  83. tlbinvf(); /* invalidate one FTLB set */
  84. }
  85. } else {
  86. while (entry < current_cpu_data.tlbsize) {
  87. /* Make sure all entries differ. */
  88. write_c0_entryhi(UNIQUE_ENTRYHI(entry));
  89. write_c0_index(entry);
  90. mtc0_tlbw_hazard();
  91. tlb_write_indexed();
  92. entry++;
  93. }
  94. }
  95. tlbw_use_hazard();
  96. write_c0_entryhi(old_ctx);
  97. htw_start();
  98. flush_micro_tlb();
  99. local_irq_restore(flags);
  100. }
  101. EXPORT_SYMBOL(local_flush_tlb_all);
  102. void local_flush_tlb_range(struct vm_area_struct *vma, unsigned long start,
  103. unsigned long end)
  104. {
  105. struct mm_struct *mm = vma->vm_mm;
  106. int cpu = smp_processor_id();
  107. if (cpu_context(cpu, mm) != 0) {
  108. unsigned long size, flags;
  109. local_irq_save(flags);
  110. start = round_down(start, PAGE_SIZE << 1);
  111. end = round_up(end, PAGE_SIZE << 1);
  112. size = (end - start) >> (PAGE_SHIFT + 1);
  113. if (size <= (current_cpu_data.tlbsizeftlbsets ?
  114. current_cpu_data.tlbsize / 8 :
  115. current_cpu_data.tlbsize / 2)) {
  116. unsigned long old_entryhi, old_mmid;
  117. int newpid = cpu_asid(cpu, mm);
  118. old_entryhi = read_c0_entryhi();
  119. if (cpu_has_mmid) {
  120. old_mmid = read_c0_memorymapid();
  121. write_c0_memorymapid(newpid);
  122. }
  123. htw_stop();
  124. while (start < end) {
  125. int idx;
  126. if (cpu_has_mmid)
  127. write_c0_entryhi(start);
  128. else
  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(old_entryhi);
  146. if (cpu_has_mmid)
  147. write_c0_memorymapid(old_mmid);
  148. htw_start();
  149. } else {
  150. drop_mmu_context(mm);
  151. }
  152. flush_micro_tlb();
  153. local_irq_restore(flags);
  154. }
  155. }
  156. void local_flush_tlb_kernel_range(unsigned long start, unsigned long end)
  157. {
  158. unsigned long size, flags;
  159. local_irq_save(flags);
  160. size = (end - start + (PAGE_SIZE - 1)) >> PAGE_SHIFT;
  161. size = (size + 1) >> 1;
  162. if (size <= (current_cpu_data.tlbsizeftlbsets ?
  163. current_cpu_data.tlbsize / 8 :
  164. current_cpu_data.tlbsize / 2)) {
  165. int pid = read_c0_entryhi();
  166. start &= (PAGE_MASK << 1);
  167. end += ((PAGE_SIZE << 1) - 1);
  168. end &= (PAGE_MASK << 1);
  169. htw_stop();
  170. while (start < end) {
  171. int idx;
  172. write_c0_entryhi(start);
  173. start += (PAGE_SIZE << 1);
  174. mtc0_tlbw_hazard();
  175. tlb_probe();
  176. tlb_probe_hazard();
  177. idx = read_c0_index();
  178. write_c0_entrylo0(0);
  179. write_c0_entrylo1(0);
  180. if (idx < 0)
  181. continue;
  182. /* Make sure all entries differ. */
  183. write_c0_entryhi(UNIQUE_ENTRYHI(idx));
  184. mtc0_tlbw_hazard();
  185. tlb_write_indexed();
  186. }
  187. tlbw_use_hazard();
  188. write_c0_entryhi(pid);
  189. htw_start();
  190. } else {
  191. local_flush_tlb_all();
  192. }
  193. flush_micro_tlb();
  194. local_irq_restore(flags);
  195. }
  196. void local_flush_tlb_page(struct vm_area_struct *vma, unsigned long page)
  197. {
  198. int cpu = smp_processor_id();
  199. if (cpu_context(cpu, vma->vm_mm) != 0) {
  200. unsigned long old_mmid;
  201. unsigned long flags, old_entryhi;
  202. int idx;
  203. page &= (PAGE_MASK << 1);
  204. local_irq_save(flags);
  205. old_entryhi = read_c0_entryhi();
  206. htw_stop();
  207. if (cpu_has_mmid) {
  208. old_mmid = read_c0_memorymapid();
  209. write_c0_entryhi(page);
  210. write_c0_memorymapid(cpu_asid(cpu, vma->vm_mm));
  211. } else {
  212. write_c0_entryhi(page | cpu_asid(cpu, vma->vm_mm));
  213. }
  214. mtc0_tlbw_hazard();
  215. tlb_probe();
  216. tlb_probe_hazard();
  217. idx = read_c0_index();
  218. write_c0_entrylo0(0);
  219. write_c0_entrylo1(0);
  220. if (idx < 0)
  221. goto finish;
  222. /* Make sure all entries differ. */
  223. write_c0_entryhi(UNIQUE_ENTRYHI(idx));
  224. mtc0_tlbw_hazard();
  225. tlb_write_indexed();
  226. tlbw_use_hazard();
  227. finish:
  228. write_c0_entryhi(old_entryhi);
  229. if (cpu_has_mmid)
  230. write_c0_memorymapid(old_mmid);
  231. htw_start();
  232. flush_micro_tlb_vm(vma);
  233. local_irq_restore(flags);
  234. }
  235. }
  236. /*
  237. * This one is only used for pages with the global bit set so we don't care
  238. * much about the ASID.
  239. */
  240. void local_flush_tlb_one(unsigned long page)
  241. {
  242. unsigned long flags;
  243. int oldpid, idx;
  244. local_irq_save(flags);
  245. oldpid = read_c0_entryhi();
  246. htw_stop();
  247. page &= (PAGE_MASK << 1);
  248. write_c0_entryhi(page);
  249. mtc0_tlbw_hazard();
  250. tlb_probe();
  251. tlb_probe_hazard();
  252. idx = read_c0_index();
  253. write_c0_entrylo0(0);
  254. write_c0_entrylo1(0);
  255. if (idx >= 0) {
  256. /* Make sure all entries differ. */
  257. write_c0_entryhi(UNIQUE_ENTRYHI(idx));
  258. mtc0_tlbw_hazard();
  259. tlb_write_indexed();
  260. tlbw_use_hazard();
  261. }
  262. write_c0_entryhi(oldpid);
  263. htw_start();
  264. flush_micro_tlb();
  265. local_irq_restore(flags);
  266. }
  267. /*
  268. * We will need multiple versions of update_mmu_cache(), one that just
  269. * updates the TLB with the new pte(s), and another which also checks
  270. * for the R4k "end of page" hardware bug and does the needy.
  271. */
  272. void __update_tlb(struct vm_area_struct * vma, unsigned long address, pte_t pte)
  273. {
  274. unsigned long flags;
  275. pgd_t *pgdp;
  276. p4d_t *p4dp;
  277. pud_t *pudp;
  278. pmd_t *pmdp;
  279. pte_t *ptep, *ptemap = NULL;
  280. int idx, pid;
  281. /*
  282. * Handle debugger faulting in for debuggee.
  283. */
  284. if (current->active_mm != vma->vm_mm)
  285. return;
  286. local_irq_save(flags);
  287. htw_stop();
  288. address &= (PAGE_MASK << 1);
  289. if (cpu_has_mmid) {
  290. write_c0_entryhi(address);
  291. } else {
  292. pid = read_c0_entryhi() & cpu_asid_mask(&current_cpu_data);
  293. write_c0_entryhi(address | pid);
  294. }
  295. pgdp = pgd_offset(vma->vm_mm, address);
  296. mtc0_tlbw_hazard();
  297. tlb_probe();
  298. tlb_probe_hazard();
  299. p4dp = p4d_offset(pgdp, address);
  300. pudp = pud_offset(p4dp, address);
  301. pmdp = pmd_offset(pudp, address);
  302. idx = read_c0_index();
  303. #ifdef CONFIG_MIPS_HUGE_TLB_SUPPORT
  304. /* this could be a huge page */
  305. if (pmd_leaf(*pmdp)) {
  306. unsigned long lo;
  307. write_c0_pagemask(PM_HUGE_MASK);
  308. ptep = (pte_t *)pmdp;
  309. lo = pte_to_entrylo(pte_val(*ptep));
  310. write_c0_entrylo0(lo);
  311. write_c0_entrylo1(lo + (HPAGE_SIZE >> 7));
  312. mtc0_tlbw_hazard();
  313. if (idx < 0)
  314. tlb_write_random();
  315. else
  316. tlb_write_indexed();
  317. tlbw_use_hazard();
  318. write_c0_pagemask(PM_DEFAULT_MASK);
  319. } else
  320. #endif
  321. {
  322. ptemap = ptep = pte_offset_map(pmdp, address);
  323. /*
  324. * update_mmu_cache() is called between pte_offset_map_lock()
  325. * and pte_unmap_unlock(), so we can assume that ptep is not
  326. * NULL here: and what should be done below if it were NULL?
  327. */
  328. #if defined(CONFIG_PHYS_ADDR_T_64BIT) && defined(CONFIG_CPU_MIPS32)
  329. #ifdef CONFIG_XPA
  330. write_c0_entrylo0(pte_to_entrylo(ptep->pte_high));
  331. if (cpu_has_xpa)
  332. writex_c0_entrylo0(ptep->pte_low & _PFNX_MASK);
  333. ptep++;
  334. write_c0_entrylo1(pte_to_entrylo(ptep->pte_high));
  335. if (cpu_has_xpa)
  336. writex_c0_entrylo1(ptep->pte_low & _PFNX_MASK);
  337. #else
  338. write_c0_entrylo0(ptep->pte_high);
  339. ptep++;
  340. write_c0_entrylo1(ptep->pte_high);
  341. #endif
  342. #else
  343. write_c0_entrylo0(pte_to_entrylo(pte_val(*ptep++)));
  344. write_c0_entrylo1(pte_to_entrylo(pte_val(*ptep)));
  345. #endif
  346. mtc0_tlbw_hazard();
  347. if (idx < 0)
  348. tlb_write_random();
  349. else
  350. tlb_write_indexed();
  351. }
  352. tlbw_use_hazard();
  353. htw_start();
  354. flush_micro_tlb_vm(vma);
  355. if (ptemap)
  356. pte_unmap(ptemap);
  357. local_irq_restore(flags);
  358. }
  359. void add_wired_entry(unsigned long entrylo0, unsigned long entrylo1,
  360. unsigned long entryhi, unsigned long pagemask)
  361. {
  362. #ifdef CONFIG_XPA
  363. panic("Broken for XPA kernels");
  364. #else
  365. unsigned int old_mmid;
  366. unsigned long flags;
  367. unsigned long wired;
  368. unsigned long old_pagemask;
  369. unsigned long old_ctx;
  370. local_irq_save(flags);
  371. if (cpu_has_mmid) {
  372. old_mmid = read_c0_memorymapid();
  373. write_c0_memorymapid(MMID_KERNEL_WIRED);
  374. }
  375. /* Save old context and create impossible VPN2 value */
  376. old_ctx = read_c0_entryhi();
  377. htw_stop();
  378. old_pagemask = read_c0_pagemask();
  379. wired = num_wired_entries();
  380. write_c0_wired(wired + 1);
  381. write_c0_index(wired);
  382. tlbw_use_hazard(); /* What is the hazard here? */
  383. write_c0_pagemask(pagemask);
  384. write_c0_entryhi(entryhi);
  385. write_c0_entrylo0(entrylo0);
  386. write_c0_entrylo1(entrylo1);
  387. mtc0_tlbw_hazard();
  388. tlb_write_indexed();
  389. tlbw_use_hazard();
  390. write_c0_entryhi(old_ctx);
  391. if (cpu_has_mmid)
  392. write_c0_memorymapid(old_mmid);
  393. tlbw_use_hazard(); /* What is the hazard here? */
  394. htw_start();
  395. write_c0_pagemask(old_pagemask);
  396. local_flush_tlb_all();
  397. local_irq_restore(flags);
  398. #endif
  399. }
  400. #ifdef CONFIG_TRANSPARENT_HUGEPAGE
  401. int has_transparent_hugepage(void)
  402. {
  403. static unsigned int mask = -1;
  404. if (mask == -1) { /* first call comes during __init */
  405. unsigned long flags;
  406. local_irq_save(flags);
  407. write_c0_pagemask(PM_HUGE_MASK);
  408. back_to_back_c0_hazard();
  409. mask = read_c0_pagemask();
  410. write_c0_pagemask(PM_DEFAULT_MASK);
  411. local_irq_restore(flags);
  412. }
  413. return mask == PM_HUGE_MASK;
  414. }
  415. EXPORT_SYMBOL(has_transparent_hugepage);
  416. #endif /* CONFIG_TRANSPARENT_HUGEPAGE */
  417. /*
  418. * Used for loading TLB entries before trap_init() has started, when we
  419. * don't actually want to add a wired entry which remains throughout the
  420. * lifetime of the system
  421. */
  422. int temp_tlb_entry;
  423. #ifndef CONFIG_64BIT
  424. __init int add_temporary_entry(unsigned long entrylo0, unsigned long entrylo1,
  425. unsigned long entryhi, unsigned long pagemask)
  426. {
  427. int ret = 0;
  428. unsigned long flags;
  429. unsigned long wired;
  430. unsigned long old_pagemask;
  431. unsigned long old_ctx;
  432. local_irq_save(flags);
  433. /* Save old context and create impossible VPN2 value */
  434. htw_stop();
  435. old_ctx = read_c0_entryhi();
  436. old_pagemask = read_c0_pagemask();
  437. wired = num_wired_entries();
  438. if (--temp_tlb_entry < wired) {
  439. printk(KERN_WARNING
  440. "No TLB space left for add_temporary_entry\n");
  441. ret = -ENOSPC;
  442. goto out;
  443. }
  444. write_c0_index(temp_tlb_entry);
  445. write_c0_pagemask(pagemask);
  446. write_c0_entryhi(entryhi);
  447. write_c0_entrylo0(entrylo0);
  448. write_c0_entrylo1(entrylo1);
  449. mtc0_tlbw_hazard();
  450. tlb_write_indexed();
  451. tlbw_use_hazard();
  452. write_c0_entryhi(old_ctx);
  453. write_c0_pagemask(old_pagemask);
  454. htw_start();
  455. out:
  456. local_irq_restore(flags);
  457. return ret;
  458. }
  459. #endif
  460. static int ntlb;
  461. static int __init set_ntlb(char *str)
  462. {
  463. get_option(&str, &ntlb);
  464. return 1;
  465. }
  466. __setup("ntlb=", set_ntlb);
  467. /* The start bit position of VPN2 and Mask in EntryHi/PageMask registers. */
  468. #define VPN2_SHIFT 13
  469. /* Read full EntryHi even with CONFIG_32BIT. */
  470. static inline unsigned long long read_c0_entryhi_native(void)
  471. {
  472. return cpu_has_64bits ? read_c0_entryhi_64() : read_c0_entryhi();
  473. }
  474. /* Write full EntryHi even with CONFIG_32BIT. */
  475. static inline void write_c0_entryhi_native(unsigned long long v)
  476. {
  477. if (cpu_has_64bits)
  478. write_c0_entryhi_64(v);
  479. else
  480. write_c0_entryhi(v);
  481. }
  482. /* TLB entry state for uniquification. */
  483. struct tlbent {
  484. unsigned long long wired:1;
  485. unsigned long long global:1;
  486. unsigned long long asid:10;
  487. unsigned long long vpn:51;
  488. unsigned long long pagesz:5;
  489. unsigned long long index:14;
  490. };
  491. /*
  492. * Comparison function for TLB entry sorting. Place wired entries first,
  493. * then global entries, then order by the increasing VPN/ASID and the
  494. * decreasing page size. This lets us avoid clashes with wired entries
  495. * easily and get entries for larger pages out of the way first.
  496. *
  497. * We could group bits so as to reduce the number of comparisons, but this
  498. * is seldom executed and not performance-critical, so prefer legibility.
  499. */
  500. static int r4k_entry_cmp(const void *a, const void *b)
  501. {
  502. struct tlbent ea = *(struct tlbent *)a, eb = *(struct tlbent *)b;
  503. if (ea.wired > eb.wired)
  504. return -1;
  505. else if (ea.wired < eb.wired)
  506. return 1;
  507. else if (ea.global > eb.global)
  508. return -1;
  509. else if (ea.global < eb.global)
  510. return 1;
  511. else if (ea.vpn < eb.vpn)
  512. return -1;
  513. else if (ea.vpn > eb.vpn)
  514. return 1;
  515. else if (ea.asid < eb.asid)
  516. return -1;
  517. else if (ea.asid > eb.asid)
  518. return 1;
  519. else if (ea.pagesz > eb.pagesz)
  520. return -1;
  521. else if (ea.pagesz < eb.pagesz)
  522. return 1;
  523. else
  524. return 0;
  525. }
  526. /*
  527. * Fetch all the TLB entries. Mask individual VPN values retrieved with
  528. * the corresponding page mask and ignoring any 1KiB extension as we'll
  529. * be using 4KiB pages for uniquification.
  530. */
  531. static void __ref r4k_tlb_uniquify_read(struct tlbent *tlb_vpns, int tlbsize)
  532. {
  533. int start = num_wired_entries();
  534. unsigned long long vpn_mask;
  535. bool global;
  536. int i;
  537. vpn_mask = GENMASK(current_cpu_data.vmbits - 1, VPN2_SHIFT);
  538. vpn_mask |= cpu_has_64bits ? 3ULL << 62 : 1 << 31;
  539. for (i = 0; i < tlbsize; i++) {
  540. unsigned long long entryhi, vpn, mask, asid;
  541. unsigned int pagesz;
  542. write_c0_index(i);
  543. mtc0_tlbr_hazard();
  544. tlb_read();
  545. tlb_read_hazard();
  546. global = !!(read_c0_entrylo0() & ENTRYLO_G);
  547. entryhi = read_c0_entryhi_native();
  548. mask = read_c0_pagemask();
  549. asid = entryhi & cpu_asid_mask(&current_cpu_data);
  550. vpn = (entryhi & vpn_mask & ~mask) >> VPN2_SHIFT;
  551. pagesz = ilog2((mask >> VPN2_SHIFT) + 1);
  552. tlb_vpns[i].global = global;
  553. tlb_vpns[i].asid = global ? 0 : asid;
  554. tlb_vpns[i].vpn = vpn;
  555. tlb_vpns[i].pagesz = pagesz;
  556. tlb_vpns[i].wired = i < start;
  557. tlb_vpns[i].index = i;
  558. }
  559. }
  560. /*
  561. * Write unique values to all but the wired TLB entries each, using
  562. * the 4KiB page size. This size might not be supported with R6, but
  563. * EHINV is mandatory for R6, so we won't ever be called in that case.
  564. *
  565. * A sorted table is supplied with any wired entries at the beginning,
  566. * followed by any global entries, and then finally regular entries.
  567. * We start at the VPN and ASID values of zero and only assign user
  568. * addresses, therefore guaranteeing no clash with addresses produced
  569. * by UNIQUE_ENTRYHI. We avoid any VPN values used by wired or global
  570. * entries, by increasing the VPN value beyond the span of such entry.
  571. *
  572. * When a VPN/ASID clash is found with a regular entry we increment the
  573. * ASID instead until no VPN/ASID clash has been found or the ASID space
  574. * has been exhausted, in which case we increase the VPN value beyond
  575. * the span of the largest clashing entry.
  576. *
  577. * We do not need to be concerned about FTLB or MMID configurations as
  578. * those are required to implement the EHINV feature.
  579. */
  580. static void __ref r4k_tlb_uniquify_write(struct tlbent *tlb_vpns, int tlbsize)
  581. {
  582. unsigned long long asid, vpn, vpn_size, pagesz;
  583. int widx, gidx, idx, sidx, lidx, i;
  584. vpn_size = 1ULL << (current_cpu_data.vmbits - VPN2_SHIFT);
  585. pagesz = ilog2((PM_4K >> VPN2_SHIFT) + 1);
  586. write_c0_pagemask(PM_4K);
  587. write_c0_entrylo0(0);
  588. write_c0_entrylo1(0);
  589. asid = 0;
  590. vpn = 0;
  591. widx = 0;
  592. gidx = 0;
  593. for (sidx = 0; sidx < tlbsize && tlb_vpns[sidx].wired; sidx++)
  594. ;
  595. for (lidx = sidx; lidx < tlbsize && tlb_vpns[lidx].global; lidx++)
  596. ;
  597. idx = gidx = sidx + 1;
  598. for (i = sidx; i < tlbsize; i++) {
  599. unsigned long long entryhi, vpn_pagesz = 0;
  600. while (1) {
  601. if (WARN_ON(vpn >= vpn_size)) {
  602. dump_tlb_all();
  603. /* Pray local_flush_tlb_all() will cope. */
  604. return;
  605. }
  606. /* VPN must be below the next wired entry. */
  607. if (widx < sidx && vpn >= tlb_vpns[widx].vpn) {
  608. vpn = max(vpn,
  609. (tlb_vpns[widx].vpn +
  610. (1ULL << tlb_vpns[widx].pagesz)));
  611. asid = 0;
  612. widx++;
  613. continue;
  614. }
  615. /* VPN must be below the next global entry. */
  616. if (gidx < lidx && vpn >= tlb_vpns[gidx].vpn) {
  617. vpn = max(vpn,
  618. (tlb_vpns[gidx].vpn +
  619. (1ULL << tlb_vpns[gidx].pagesz)));
  620. asid = 0;
  621. gidx++;
  622. continue;
  623. }
  624. /* Try to find a free ASID so as to conserve VPNs. */
  625. if (idx < tlbsize && vpn == tlb_vpns[idx].vpn &&
  626. asid == tlb_vpns[idx].asid) {
  627. unsigned long long idx_pagesz;
  628. idx_pagesz = tlb_vpns[idx].pagesz;
  629. vpn_pagesz = max(vpn_pagesz, idx_pagesz);
  630. do
  631. idx++;
  632. while (idx < tlbsize &&
  633. vpn == tlb_vpns[idx].vpn &&
  634. asid == tlb_vpns[idx].asid);
  635. asid++;
  636. if (asid > cpu_asid_mask(&current_cpu_data)) {
  637. vpn += vpn_pagesz;
  638. asid = 0;
  639. vpn_pagesz = 0;
  640. }
  641. continue;
  642. }
  643. /* VPN mustn't be above the next regular entry. */
  644. if (idx < tlbsize && vpn > tlb_vpns[idx].vpn) {
  645. vpn = max(vpn,
  646. (tlb_vpns[idx].vpn +
  647. (1ULL << tlb_vpns[idx].pagesz)));
  648. asid = 0;
  649. idx++;
  650. continue;
  651. }
  652. break;
  653. }
  654. entryhi = (vpn << VPN2_SHIFT) | asid;
  655. write_c0_entryhi_native(entryhi);
  656. write_c0_index(tlb_vpns[i].index);
  657. mtc0_tlbw_hazard();
  658. tlb_write_indexed();
  659. tlb_vpns[i].asid = asid;
  660. tlb_vpns[i].vpn = vpn;
  661. tlb_vpns[i].pagesz = pagesz;
  662. asid++;
  663. if (asid > cpu_asid_mask(&current_cpu_data)) {
  664. vpn += 1ULL << pagesz;
  665. asid = 0;
  666. }
  667. }
  668. }
  669. /*
  670. * Initialise all TLB entries with unique values that do not clash with
  671. * what we have been handed over and what we'll be using ourselves.
  672. */
  673. static void __ref r4k_tlb_uniquify(void)
  674. {
  675. int tlbsize = current_cpu_data.tlbsize;
  676. bool use_slab = slab_is_available();
  677. phys_addr_t tlb_vpn_size;
  678. struct tlbent *tlb_vpns;
  679. tlb_vpn_size = tlbsize * sizeof(*tlb_vpns);
  680. tlb_vpns = (use_slab ?
  681. kmalloc(tlb_vpn_size, GFP_ATOMIC) :
  682. memblock_alloc_raw(tlb_vpn_size, sizeof(*tlb_vpns)));
  683. if (WARN_ON(!tlb_vpns))
  684. return; /* Pray local_flush_tlb_all() is good enough. */
  685. htw_stop();
  686. r4k_tlb_uniquify_read(tlb_vpns, tlbsize);
  687. sort(tlb_vpns, tlbsize, sizeof(*tlb_vpns), r4k_entry_cmp, NULL);
  688. r4k_tlb_uniquify_write(tlb_vpns, tlbsize);
  689. write_c0_pagemask(PM_DEFAULT_MASK);
  690. tlbw_use_hazard();
  691. htw_start();
  692. flush_micro_tlb();
  693. if (use_slab)
  694. kfree(tlb_vpns);
  695. else
  696. memblock_free(tlb_vpns, tlb_vpn_size);
  697. }
  698. /*
  699. * Configure TLB (for init or after a CPU has been powered off).
  700. */
  701. static void r4k_tlb_configure(void)
  702. {
  703. /*
  704. * You should never change this register:
  705. * - On R4600 1.7 the tlbp never hits for pages smaller than
  706. * the value in the c0_pagemask register.
  707. * - The entire mm handling assumes the c0_pagemask register to
  708. * be set to fixed-size pages.
  709. */
  710. write_c0_pagemask(PM_DEFAULT_MASK);
  711. back_to_back_c0_hazard();
  712. if (read_c0_pagemask() != PM_DEFAULT_MASK)
  713. panic("MMU doesn't support PAGE_SIZE=0x%lx", PAGE_SIZE);
  714. write_c0_wired(0);
  715. if (current_cpu_type() == CPU_R10000 ||
  716. current_cpu_type() == CPU_R12000 ||
  717. current_cpu_type() == CPU_R14000 ||
  718. current_cpu_type() == CPU_R16000)
  719. write_c0_framemask(0);
  720. if (cpu_has_rixi) {
  721. /*
  722. * Enable the no read, no exec bits, and enable large physical
  723. * address.
  724. */
  725. #ifdef CONFIG_64BIT
  726. set_c0_pagegrain(PG_RIE | PG_XIE | PG_ELPA);
  727. #else
  728. set_c0_pagegrain(PG_RIE | PG_XIE);
  729. #endif
  730. }
  731. temp_tlb_entry = current_cpu_data.tlbsize - 1;
  732. /* From this point on the ARC firmware is dead. */
  733. if (!cpu_has_tlbinv)
  734. r4k_tlb_uniquify();
  735. local_flush_tlb_all();
  736. /* Did I tell you that ARC SUCKS? */
  737. }
  738. void tlb_init(void)
  739. {
  740. r4k_tlb_configure();
  741. if (ntlb) {
  742. if (ntlb > 1 && ntlb <= current_cpu_data.tlbsize) {
  743. int wired = current_cpu_data.tlbsize - ntlb;
  744. write_c0_wired(wired);
  745. write_c0_index(wired-1);
  746. printk("Restricting TLB to %d entries\n", ntlb);
  747. } else
  748. printk("Ignoring invalid argument ntlb=%d\n", ntlb);
  749. }
  750. build_tlb_refill_handler();
  751. }
  752. static int r4k_tlb_pm_notifier(struct notifier_block *self, unsigned long cmd,
  753. void *v)
  754. {
  755. switch (cmd) {
  756. case CPU_PM_ENTER_FAILED:
  757. case CPU_PM_EXIT:
  758. r4k_tlb_configure();
  759. break;
  760. }
  761. return NOTIFY_OK;
  762. }
  763. static struct notifier_block r4k_tlb_pm_notifier_block = {
  764. .notifier_call = r4k_tlb_pm_notifier,
  765. };
  766. static int __init r4k_tlb_init_pm(void)
  767. {
  768. return cpu_pm_register_notifier(&r4k_tlb_pm_notifier_block);
  769. }
  770. arch_initcall(r4k_tlb_init_pm);