tlbex.c 38 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462
  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. * Synthesize TLB refill handlers at runtime.
  7. *
  8. * Copyright (C) 2004, 2005, 2006, 2008 Thiemo Seufer
  9. * Copyright (C) 2005, 2007, 2008, 2009 Maciej W. Rozycki
  10. * Copyright (C) 2006 Ralf Baechle (ralf@linux-mips.org)
  11. * Copyright (C) 2008, 2009 Cavium Networks, Inc.
  12. *
  13. * ... and the days got worse and worse and now you see
  14. * I've gone completly out of my mind.
  15. *
  16. * They're coming to take me a away haha
  17. * they're coming to take me a away hoho hihi haha
  18. * to the funny farm where code is beautiful all the time ...
  19. *
  20. * (Condolences to Napoleon XIV)
  21. */
  22. #include <linux/bug.h>
  23. #include <linux/kernel.h>
  24. #include <linux/types.h>
  25. #include <linux/smp.h>
  26. #include <linux/string.h>
  27. #include <linux/init.h>
  28. #include <asm/mmu_context.h>
  29. #include <asm/war.h>
  30. #include "uasm.h"
  31. static inline int r45k_bvahwbug(void)
  32. {
  33. /* XXX: We should probe for the presence of this bug, but we don't. */
  34. return 0;
  35. }
  36. static inline int r4k_250MHZhwbug(void)
  37. {
  38. /* XXX: We should probe for the presence of this bug, but we don't. */
  39. return 0;
  40. }
  41. static inline int __maybe_unused bcm1250_m3_war(void)
  42. {
  43. return BCM1250_M3_WAR;
  44. }
  45. static inline int __maybe_unused r10000_llsc_war(void)
  46. {
  47. return R10000_LLSC_WAR;
  48. }
  49. /*
  50. * Found by experiment: At least some revisions of the 4kc throw under
  51. * some circumstances a machine check exception, triggered by invalid
  52. * values in the index register. Delaying the tlbp instruction until
  53. * after the next branch, plus adding an additional nop in front of
  54. * tlbwi/tlbwr avoids the invalid index register values. Nobody knows
  55. * why; it's not an issue caused by the core RTL.
  56. *
  57. */
  58. static int __cpuinit m4kc_tlbp_war(void)
  59. {
  60. return (current_cpu_data.processor_id & 0xffff00) ==
  61. (PRID_COMP_MIPS | PRID_IMP_4KC);
  62. }
  63. /* Handle labels (which must be positive integers). */
  64. enum label_id {
  65. label_second_part = 1,
  66. label_leave,
  67. #ifdef MODULE_START
  68. label_module_alloc,
  69. #endif
  70. label_vmalloc,
  71. label_vmalloc_done,
  72. label_tlbw_hazard,
  73. label_split,
  74. label_nopage_tlbl,
  75. label_nopage_tlbs,
  76. label_nopage_tlbm,
  77. label_smp_pgtable_change,
  78. label_r3000_write_probe_fail,
  79. #ifdef CONFIG_HUGETLB_PAGE
  80. label_tlb_huge_update,
  81. #endif
  82. };
  83. UASM_L_LA(_second_part)
  84. UASM_L_LA(_leave)
  85. #ifdef MODULE_START
  86. UASM_L_LA(_module_alloc)
  87. #endif
  88. UASM_L_LA(_vmalloc)
  89. UASM_L_LA(_vmalloc_done)
  90. UASM_L_LA(_tlbw_hazard)
  91. UASM_L_LA(_split)
  92. UASM_L_LA(_nopage_tlbl)
  93. UASM_L_LA(_nopage_tlbs)
  94. UASM_L_LA(_nopage_tlbm)
  95. UASM_L_LA(_smp_pgtable_change)
  96. UASM_L_LA(_r3000_write_probe_fail)
  97. #ifdef CONFIG_HUGETLB_PAGE
  98. UASM_L_LA(_tlb_huge_update)
  99. #endif
  100. /*
  101. * For debug purposes.
  102. */
  103. static inline void dump_handler(const u32 *handler, int count)
  104. {
  105. int i;
  106. pr_debug("\t.set push\n");
  107. pr_debug("\t.set noreorder\n");
  108. for (i = 0; i < count; i++)
  109. pr_debug("\t%p\t.word 0x%08x\n", &handler[i], handler[i]);
  110. pr_debug("\t.set pop\n");
  111. }
  112. /* The only general purpose registers allowed in TLB handlers. */
  113. #define K0 26
  114. #define K1 27
  115. /* Some CP0 registers */
  116. #define C0_INDEX 0, 0
  117. #define C0_ENTRYLO0 2, 0
  118. #define C0_TCBIND 2, 2
  119. #define C0_ENTRYLO1 3, 0
  120. #define C0_CONTEXT 4, 0
  121. #define C0_PAGEMASK 5, 0
  122. #define C0_BADVADDR 8, 0
  123. #define C0_ENTRYHI 10, 0
  124. #define C0_EPC 14, 0
  125. #define C0_XCONTEXT 20, 0
  126. #ifdef CONFIG_64BIT
  127. # define GET_CONTEXT(buf, reg) UASM_i_MFC0(buf, reg, C0_XCONTEXT)
  128. #else
  129. # define GET_CONTEXT(buf, reg) UASM_i_MFC0(buf, reg, C0_CONTEXT)
  130. #endif
  131. /* The worst case length of the handler is around 18 instructions for
  132. * R3000-style TLBs and up to 63 instructions for R4000-style TLBs.
  133. * Maximum space available is 32 instructions for R3000 and 64
  134. * instructions for R4000.
  135. *
  136. * We deliberately chose a buffer size of 128, so we won't scribble
  137. * over anything important on overflow before we panic.
  138. */
  139. static u32 tlb_handler[128] __cpuinitdata;
  140. /* simply assume worst case size for labels and relocs */
  141. static struct uasm_label labels[128] __cpuinitdata;
  142. static struct uasm_reloc relocs[128] __cpuinitdata;
  143. #ifndef CONFIG_MIPS_PGD_C0_CONTEXT
  144. /*
  145. * CONFIG_MIPS_PGD_C0_CONTEXT implies 64 bit and lack of pgd_current,
  146. * we cannot do r3000 under these circumstances.
  147. */
  148. /*
  149. * The R3000 TLB handler is simple.
  150. */
  151. static void __cpuinit build_r3000_tlb_refill_handler(void)
  152. {
  153. long pgdc = (long)pgd_current;
  154. u32 *p;
  155. memset(tlb_handler, 0, sizeof(tlb_handler));
  156. p = tlb_handler;
  157. uasm_i_mfc0(&p, K0, C0_BADVADDR);
  158. uasm_i_lui(&p, K1, uasm_rel_hi(pgdc)); /* cp0 delay */
  159. uasm_i_lw(&p, K1, uasm_rel_lo(pgdc), K1);
  160. uasm_i_srl(&p, K0, K0, 22); /* load delay */
  161. uasm_i_sll(&p, K0, K0, 2);
  162. uasm_i_addu(&p, K1, K1, K0);
  163. uasm_i_mfc0(&p, K0, C0_CONTEXT);
  164. uasm_i_lw(&p, K1, 0, K1); /* cp0 delay */
  165. uasm_i_andi(&p, K0, K0, 0xffc); /* load delay */
  166. uasm_i_addu(&p, K1, K1, K0);
  167. uasm_i_lw(&p, K0, 0, K1);
  168. uasm_i_nop(&p); /* load delay */
  169. uasm_i_mtc0(&p, K0, C0_ENTRYLO0);
  170. uasm_i_mfc0(&p, K1, C0_EPC); /* cp0 delay */
  171. uasm_i_tlbwr(&p); /* cp0 delay */
  172. uasm_i_jr(&p, K1);
  173. uasm_i_rfe(&p); /* branch delay */
  174. if (p > tlb_handler + 32)
  175. panic("TLB refill handler space exceeded");
  176. pr_debug("Wrote TLB refill handler (%u instructions).\n",
  177. (unsigned int)(p - tlb_handler));
  178. memcpy((void *)ebase, tlb_handler, 0x80);
  179. dump_handler((u32 *)ebase, 32);
  180. }
  181. #endif /* CONFIG_MIPS_PGD_C0_CONTEXT */
  182. /*
  183. * The R4000 TLB handler is much more complicated. We have two
  184. * consecutive handler areas with 32 instructions space each.
  185. * Since they aren't used at the same time, we can overflow in the
  186. * other one.To keep things simple, we first assume linear space,
  187. * then we relocate it to the final handler layout as needed.
  188. */
  189. static u32 final_handler[64] __cpuinitdata;
  190. /*
  191. * Hazards
  192. *
  193. * From the IDT errata for the QED RM5230 (Nevada), processor revision 1.0:
  194. * 2. A timing hazard exists for the TLBP instruction.
  195. *
  196. * stalling_instruction
  197. * TLBP
  198. *
  199. * The JTLB is being read for the TLBP throughout the stall generated by the
  200. * previous instruction. This is not really correct as the stalling instruction
  201. * can modify the address used to access the JTLB. The failure symptom is that
  202. * the TLBP instruction will use an address created for the stalling instruction
  203. * and not the address held in C0_ENHI and thus report the wrong results.
  204. *
  205. * The software work-around is to not allow the instruction preceding the TLBP
  206. * to stall - make it an NOP or some other instruction guaranteed not to stall.
  207. *
  208. * Errata 2 will not be fixed. This errata is also on the R5000.
  209. *
  210. * As if we MIPS hackers wouldn't know how to nop pipelines happy ...
  211. */
  212. static void __cpuinit __maybe_unused build_tlb_probe_entry(u32 **p)
  213. {
  214. switch (current_cpu_type()) {
  215. /* Found by experiment: R4600 v2.0/R4700 needs this, too. */
  216. case CPU_R4600:
  217. case CPU_R4700:
  218. case CPU_R5000:
  219. case CPU_R5000A:
  220. case CPU_NEVADA:
  221. uasm_i_nop(p);
  222. uasm_i_tlbp(p);
  223. break;
  224. default:
  225. uasm_i_tlbp(p);
  226. break;
  227. }
  228. }
  229. /*
  230. * Write random or indexed TLB entry, and care about the hazards from
  231. * the preceeding mtc0 and for the following eret.
  232. */
  233. enum tlb_write_entry { tlb_random, tlb_indexed };
  234. static void __cpuinit build_tlb_write_entry(u32 **p, struct uasm_label **l,
  235. struct uasm_reloc **r,
  236. enum tlb_write_entry wmode)
  237. {
  238. void(*tlbw)(u32 **) = NULL;
  239. switch (wmode) {
  240. case tlb_random: tlbw = uasm_i_tlbwr; break;
  241. case tlb_indexed: tlbw = uasm_i_tlbwi; break;
  242. }
  243. if (cpu_has_mips_r2) {
  244. if (cpu_has_mips_r2_exec_hazard)
  245. uasm_i_ehb(p);
  246. tlbw(p);
  247. return;
  248. }
  249. switch (current_cpu_type()) {
  250. case CPU_R4000PC:
  251. case CPU_R4000SC:
  252. case CPU_R4000MC:
  253. case CPU_R4400PC:
  254. case CPU_R4400SC:
  255. case CPU_R4400MC:
  256. /*
  257. * This branch uses up a mtc0 hazard nop slot and saves
  258. * two nops after the tlbw instruction.
  259. */
  260. uasm_il_bgezl(p, r, 0, label_tlbw_hazard);
  261. tlbw(p);
  262. uasm_l_tlbw_hazard(l, *p);
  263. uasm_i_nop(p);
  264. break;
  265. case CPU_R4600:
  266. case CPU_R4700:
  267. case CPU_R5000:
  268. case CPU_R5000A:
  269. uasm_i_nop(p);
  270. tlbw(p);
  271. uasm_i_nop(p);
  272. break;
  273. case CPU_R4300:
  274. case CPU_5KC:
  275. case CPU_TX49XX:
  276. case CPU_PR4450:
  277. uasm_i_nop(p);
  278. tlbw(p);
  279. break;
  280. case CPU_R10000:
  281. case CPU_R12000:
  282. case CPU_R14000:
  283. case CPU_4KC:
  284. case CPU_4KEC:
  285. case CPU_SB1:
  286. case CPU_SB1A:
  287. case CPU_4KSC:
  288. case CPU_20KC:
  289. case CPU_25KF:
  290. case CPU_BCM3302:
  291. case CPU_BCM4710:
  292. case CPU_LOONGSON2:
  293. case CPU_BCM6338:
  294. case CPU_BCM6345:
  295. case CPU_BCM6348:
  296. case CPU_BCM6358:
  297. case CPU_R5500:
  298. if (m4kc_tlbp_war())
  299. uasm_i_nop(p);
  300. case CPU_ALCHEMY:
  301. tlbw(p);
  302. break;
  303. case CPU_NEVADA:
  304. uasm_i_nop(p); /* QED specifies 2 nops hazard */
  305. /*
  306. * This branch uses up a mtc0 hazard nop slot and saves
  307. * a nop after the tlbw instruction.
  308. */
  309. uasm_il_bgezl(p, r, 0, label_tlbw_hazard);
  310. tlbw(p);
  311. uasm_l_tlbw_hazard(l, *p);
  312. break;
  313. case CPU_RM7000:
  314. uasm_i_nop(p);
  315. uasm_i_nop(p);
  316. uasm_i_nop(p);
  317. uasm_i_nop(p);
  318. tlbw(p);
  319. break;
  320. case CPU_RM9000:
  321. /*
  322. * When the JTLB is updated by tlbwi or tlbwr, a subsequent
  323. * use of the JTLB for instructions should not occur for 4
  324. * cpu cycles and use for data translations should not occur
  325. * for 3 cpu cycles.
  326. */
  327. uasm_i_ssnop(p);
  328. uasm_i_ssnop(p);
  329. uasm_i_ssnop(p);
  330. uasm_i_ssnop(p);
  331. tlbw(p);
  332. uasm_i_ssnop(p);
  333. uasm_i_ssnop(p);
  334. uasm_i_ssnop(p);
  335. uasm_i_ssnop(p);
  336. break;
  337. case CPU_VR4111:
  338. case CPU_VR4121:
  339. case CPU_VR4122:
  340. case CPU_VR4181:
  341. case CPU_VR4181A:
  342. uasm_i_nop(p);
  343. uasm_i_nop(p);
  344. tlbw(p);
  345. uasm_i_nop(p);
  346. uasm_i_nop(p);
  347. break;
  348. case CPU_VR4131:
  349. case CPU_VR4133:
  350. case CPU_R5432:
  351. uasm_i_nop(p);
  352. uasm_i_nop(p);
  353. tlbw(p);
  354. break;
  355. default:
  356. panic("No TLB refill handler yet (CPU type: %d)",
  357. current_cpu_data.cputype);
  358. break;
  359. }
  360. }
  361. #ifdef CONFIG_HUGETLB_PAGE
  362. static __cpuinit void build_huge_tlb_write_entry(u32 **p,
  363. struct uasm_label **l,
  364. struct uasm_reloc **r,
  365. unsigned int tmp,
  366. enum tlb_write_entry wmode)
  367. {
  368. /* Set huge page tlb entry size */
  369. uasm_i_lui(p, tmp, PM_HUGE_MASK >> 16);
  370. uasm_i_ori(p, tmp, tmp, PM_HUGE_MASK & 0xffff);
  371. uasm_i_mtc0(p, tmp, C0_PAGEMASK);
  372. build_tlb_write_entry(p, l, r, wmode);
  373. /* Reset default page size */
  374. if (PM_DEFAULT_MASK >> 16) {
  375. uasm_i_lui(p, tmp, PM_DEFAULT_MASK >> 16);
  376. uasm_i_ori(p, tmp, tmp, PM_DEFAULT_MASK & 0xffff);
  377. uasm_il_b(p, r, label_leave);
  378. uasm_i_mtc0(p, tmp, C0_PAGEMASK);
  379. } else if (PM_DEFAULT_MASK) {
  380. uasm_i_ori(p, tmp, 0, PM_DEFAULT_MASK);
  381. uasm_il_b(p, r, label_leave);
  382. uasm_i_mtc0(p, tmp, C0_PAGEMASK);
  383. } else {
  384. uasm_il_b(p, r, label_leave);
  385. uasm_i_mtc0(p, 0, C0_PAGEMASK);
  386. }
  387. }
  388. /*
  389. * Check if Huge PTE is present, if so then jump to LABEL.
  390. */
  391. static void __cpuinit
  392. build_is_huge_pte(u32 **p, struct uasm_reloc **r, unsigned int tmp,
  393. unsigned int pmd, int lid)
  394. {
  395. UASM_i_LW(p, tmp, 0, pmd);
  396. uasm_i_andi(p, tmp, tmp, _PAGE_HUGE);
  397. uasm_il_bnez(p, r, tmp, lid);
  398. }
  399. static __cpuinit void build_huge_update_entries(u32 **p,
  400. unsigned int pte,
  401. unsigned int tmp)
  402. {
  403. int small_sequence;
  404. /*
  405. * A huge PTE describes an area the size of the
  406. * configured huge page size. This is twice the
  407. * of the large TLB entry size we intend to use.
  408. * A TLB entry half the size of the configured
  409. * huge page size is configured into entrylo0
  410. * and entrylo1 to cover the contiguous huge PTE
  411. * address space.
  412. */
  413. small_sequence = (HPAGE_SIZE >> 7) < 0x10000;
  414. /* We can clobber tmp. It isn't used after this.*/
  415. if (!small_sequence)
  416. uasm_i_lui(p, tmp, HPAGE_SIZE >> (7 + 16));
  417. UASM_i_SRL(p, pte, pte, 6); /* convert to entrylo */
  418. uasm_i_mtc0(p, pte, C0_ENTRYLO0); /* load it */
  419. /* convert to entrylo1 */
  420. if (small_sequence)
  421. UASM_i_ADDIU(p, pte, pte, HPAGE_SIZE >> 7);
  422. else
  423. UASM_i_ADDU(p, pte, pte, tmp);
  424. uasm_i_mtc0(p, pte, C0_ENTRYLO1); /* load it */
  425. }
  426. static __cpuinit void build_huge_handler_tail(u32 **p,
  427. struct uasm_reloc **r,
  428. struct uasm_label **l,
  429. unsigned int pte,
  430. unsigned int ptr)
  431. {
  432. #ifdef CONFIG_SMP
  433. UASM_i_SC(p, pte, 0, ptr);
  434. uasm_il_beqz(p, r, pte, label_tlb_huge_update);
  435. UASM_i_LW(p, pte, 0, ptr); /* Needed because SC killed our PTE */
  436. #else
  437. UASM_i_SW(p, pte, 0, ptr);
  438. #endif
  439. build_huge_update_entries(p, pte, ptr);
  440. build_huge_tlb_write_entry(p, l, r, pte, tlb_indexed);
  441. }
  442. #endif /* CONFIG_HUGETLB_PAGE */
  443. #ifdef CONFIG_64BIT
  444. /*
  445. * TMP and PTR are scratch.
  446. * TMP will be clobbered, PTR will hold the pmd entry.
  447. */
  448. static void __cpuinit
  449. build_get_pmde64(u32 **p, struct uasm_label **l, struct uasm_reloc **r,
  450. unsigned int tmp, unsigned int ptr)
  451. {
  452. #ifndef CONFIG_MIPS_PGD_C0_CONTEXT
  453. long pgdc = (long)pgd_current;
  454. #endif
  455. /*
  456. * The vmalloc handling is not in the hotpath.
  457. */
  458. uasm_i_dmfc0(p, tmp, C0_BADVADDR);
  459. uasm_il_bltz(p, r, tmp, label_vmalloc);
  460. /* No uasm_i_nop needed here, since the next insn doesn't touch TMP. */
  461. #ifdef CONFIG_MIPS_PGD_C0_CONTEXT
  462. /*
  463. * &pgd << 11 stored in CONTEXT [23..63].
  464. */
  465. UASM_i_MFC0(p, ptr, C0_CONTEXT);
  466. uasm_i_dins(p, ptr, 0, 0, 23); /* Clear lower 23 bits of context. */
  467. uasm_i_ori(p, ptr, ptr, 0x540); /* 1 0 1 0 1 << 6 xkphys cached */
  468. uasm_i_drotr(p, ptr, ptr, 11);
  469. #elif defined(CONFIG_SMP)
  470. # ifdef CONFIG_MIPS_MT_SMTC
  471. /*
  472. * SMTC uses TCBind value as "CPU" index
  473. */
  474. uasm_i_mfc0(p, ptr, C0_TCBIND);
  475. uasm_i_dsrl(p, ptr, ptr, 19);
  476. # else
  477. /*
  478. * 64 bit SMP running in XKPHYS has smp_processor_id() << 3
  479. * stored in CONTEXT.
  480. */
  481. uasm_i_dmfc0(p, ptr, C0_CONTEXT);
  482. uasm_i_dsrl(p, ptr, ptr, 23);
  483. # endif
  484. UASM_i_LA_mostly(p, tmp, pgdc);
  485. uasm_i_daddu(p, ptr, ptr, tmp);
  486. uasm_i_dmfc0(p, tmp, C0_BADVADDR);
  487. uasm_i_ld(p, ptr, uasm_rel_lo(pgdc), ptr);
  488. #else
  489. UASM_i_LA_mostly(p, ptr, pgdc);
  490. uasm_i_ld(p, ptr, uasm_rel_lo(pgdc), ptr);
  491. #endif
  492. uasm_l_vmalloc_done(l, *p);
  493. if (PGDIR_SHIFT - 3 < 32) /* get pgd offset in bytes */
  494. uasm_i_dsrl(p, tmp, tmp, PGDIR_SHIFT-3);
  495. else
  496. uasm_i_dsrl32(p, tmp, tmp, PGDIR_SHIFT - 3 - 32);
  497. uasm_i_andi(p, tmp, tmp, (PTRS_PER_PGD - 1)<<3);
  498. uasm_i_daddu(p, ptr, ptr, tmp); /* add in pgd offset */
  499. uasm_i_dmfc0(p, tmp, C0_BADVADDR); /* get faulting address */
  500. uasm_i_ld(p, ptr, 0, ptr); /* get pmd pointer */
  501. uasm_i_dsrl(p, tmp, tmp, PMD_SHIFT-3); /* get pmd offset in bytes */
  502. uasm_i_andi(p, tmp, tmp, (PTRS_PER_PMD - 1)<<3);
  503. uasm_i_daddu(p, ptr, ptr, tmp); /* add in pmd offset */
  504. }
  505. /*
  506. * BVADDR is the faulting address, PTR is scratch.
  507. * PTR will hold the pgd for vmalloc.
  508. */
  509. static void __cpuinit
  510. build_get_pgd_vmalloc64(u32 **p, struct uasm_label **l, struct uasm_reloc **r,
  511. unsigned int bvaddr, unsigned int ptr)
  512. {
  513. long swpd = (long)swapper_pg_dir;
  514. uasm_l_vmalloc(l, *p);
  515. if (uasm_in_compat_space_p(swpd) && !uasm_rel_lo(swpd)) {
  516. uasm_il_b(p, r, label_vmalloc_done);
  517. uasm_i_lui(p, ptr, uasm_rel_hi(swpd));
  518. } else {
  519. UASM_i_LA_mostly(p, ptr, swpd);
  520. uasm_il_b(p, r, label_vmalloc_done);
  521. if (uasm_in_compat_space_p(swpd))
  522. uasm_i_addiu(p, ptr, ptr, uasm_rel_lo(swpd));
  523. else
  524. uasm_i_daddiu(p, ptr, ptr, uasm_rel_lo(swpd));
  525. }
  526. }
  527. #else /* !CONFIG_64BIT */
  528. /*
  529. * TMP and PTR are scratch.
  530. * TMP will be clobbered, PTR will hold the pgd entry.
  531. */
  532. static void __cpuinit __maybe_unused
  533. build_get_pgde32(u32 **p, unsigned int tmp, unsigned int ptr)
  534. {
  535. long pgdc = (long)pgd_current;
  536. /* 32 bit SMP has smp_processor_id() stored in CONTEXT. */
  537. #ifdef CONFIG_SMP
  538. #ifdef CONFIG_MIPS_MT_SMTC
  539. /*
  540. * SMTC uses TCBind value as "CPU" index
  541. */
  542. uasm_i_mfc0(p, ptr, C0_TCBIND);
  543. UASM_i_LA_mostly(p, tmp, pgdc);
  544. uasm_i_srl(p, ptr, ptr, 19);
  545. #else
  546. /*
  547. * smp_processor_id() << 3 is stored in CONTEXT.
  548. */
  549. uasm_i_mfc0(p, ptr, C0_CONTEXT);
  550. UASM_i_LA_mostly(p, tmp, pgdc);
  551. uasm_i_srl(p, ptr, ptr, 23);
  552. #endif
  553. uasm_i_addu(p, ptr, tmp, ptr);
  554. #else
  555. UASM_i_LA_mostly(p, ptr, pgdc);
  556. #endif
  557. uasm_i_mfc0(p, tmp, C0_BADVADDR); /* get faulting address */
  558. uasm_i_lw(p, ptr, uasm_rel_lo(pgdc), ptr);
  559. uasm_i_srl(p, tmp, tmp, PGDIR_SHIFT); /* get pgd only bits */
  560. uasm_i_sll(p, tmp, tmp, PGD_T_LOG2);
  561. uasm_i_addu(p, ptr, ptr, tmp); /* add in pgd offset */
  562. }
  563. #endif /* !CONFIG_64BIT */
  564. static void __cpuinit build_adjust_context(u32 **p, unsigned int ctx)
  565. {
  566. unsigned int shift = 4 - (PTE_T_LOG2 + 1) + PAGE_SHIFT - 12;
  567. unsigned int mask = (PTRS_PER_PTE / 2 - 1) << (PTE_T_LOG2 + 1);
  568. switch (current_cpu_type()) {
  569. case CPU_VR41XX:
  570. case CPU_VR4111:
  571. case CPU_VR4121:
  572. case CPU_VR4122:
  573. case CPU_VR4131:
  574. case CPU_VR4181:
  575. case CPU_VR4181A:
  576. case CPU_VR4133:
  577. shift += 2;
  578. break;
  579. default:
  580. break;
  581. }
  582. if (shift)
  583. UASM_i_SRL(p, ctx, ctx, shift);
  584. uasm_i_andi(p, ctx, ctx, mask);
  585. }
  586. static void __cpuinit build_get_ptep(u32 **p, unsigned int tmp, unsigned int ptr)
  587. {
  588. /*
  589. * Bug workaround for the Nevada. It seems as if under certain
  590. * circumstances the move from cp0_context might produce a
  591. * bogus result when the mfc0 instruction and its consumer are
  592. * in a different cacheline or a load instruction, probably any
  593. * memory reference, is between them.
  594. */
  595. switch (current_cpu_type()) {
  596. case CPU_NEVADA:
  597. UASM_i_LW(p, ptr, 0, ptr);
  598. GET_CONTEXT(p, tmp); /* get context reg */
  599. break;
  600. default:
  601. GET_CONTEXT(p, tmp); /* get context reg */
  602. UASM_i_LW(p, ptr, 0, ptr);
  603. break;
  604. }
  605. build_adjust_context(p, tmp);
  606. UASM_i_ADDU(p, ptr, ptr, tmp); /* add in offset */
  607. }
  608. static void __cpuinit build_update_entries(u32 **p, unsigned int tmp,
  609. unsigned int ptep)
  610. {
  611. /*
  612. * 64bit address support (36bit on a 32bit CPU) in a 32bit
  613. * Kernel is a special case. Only a few CPUs use it.
  614. */
  615. #ifdef CONFIG_64BIT_PHYS_ADDR
  616. if (cpu_has_64bits) {
  617. uasm_i_ld(p, tmp, 0, ptep); /* get even pte */
  618. uasm_i_ld(p, ptep, sizeof(pte_t), ptep); /* get odd pte */
  619. uasm_i_dsrl(p, tmp, tmp, 6); /* convert to entrylo0 */
  620. uasm_i_mtc0(p, tmp, C0_ENTRYLO0); /* load it */
  621. uasm_i_dsrl(p, ptep, ptep, 6); /* convert to entrylo1 */
  622. uasm_i_mtc0(p, ptep, C0_ENTRYLO1); /* load it */
  623. } else {
  624. int pte_off_even = sizeof(pte_t) / 2;
  625. int pte_off_odd = pte_off_even + sizeof(pte_t);
  626. /* The pte entries are pre-shifted */
  627. uasm_i_lw(p, tmp, pte_off_even, ptep); /* get even pte */
  628. uasm_i_mtc0(p, tmp, C0_ENTRYLO0); /* load it */
  629. uasm_i_lw(p, ptep, pte_off_odd, ptep); /* get odd pte */
  630. uasm_i_mtc0(p, ptep, C0_ENTRYLO1); /* load it */
  631. }
  632. #else
  633. UASM_i_LW(p, tmp, 0, ptep); /* get even pte */
  634. UASM_i_LW(p, ptep, sizeof(pte_t), ptep); /* get odd pte */
  635. if (r45k_bvahwbug())
  636. build_tlb_probe_entry(p);
  637. UASM_i_SRL(p, tmp, tmp, 6); /* convert to entrylo0 */
  638. if (r4k_250MHZhwbug())
  639. uasm_i_mtc0(p, 0, C0_ENTRYLO0);
  640. uasm_i_mtc0(p, tmp, C0_ENTRYLO0); /* load it */
  641. UASM_i_SRL(p, ptep, ptep, 6); /* convert to entrylo1 */
  642. if (r45k_bvahwbug())
  643. uasm_i_mfc0(p, tmp, C0_INDEX);
  644. if (r4k_250MHZhwbug())
  645. uasm_i_mtc0(p, 0, C0_ENTRYLO1);
  646. uasm_i_mtc0(p, ptep, C0_ENTRYLO1); /* load it */
  647. #endif
  648. }
  649. /*
  650. * For a 64-bit kernel, we are using the 64-bit XTLB refill exception
  651. * because EXL == 0. If we wrap, we can also use the 32 instruction
  652. * slots before the XTLB refill exception handler which belong to the
  653. * unused TLB refill exception.
  654. */
  655. #define MIPS64_REFILL_INSNS 32
  656. static void __cpuinit build_r4000_tlb_refill_handler(void)
  657. {
  658. u32 *p = tlb_handler;
  659. struct uasm_label *l = labels;
  660. struct uasm_reloc *r = relocs;
  661. u32 *f;
  662. unsigned int final_len;
  663. memset(tlb_handler, 0, sizeof(tlb_handler));
  664. memset(labels, 0, sizeof(labels));
  665. memset(relocs, 0, sizeof(relocs));
  666. memset(final_handler, 0, sizeof(final_handler));
  667. /*
  668. * create the plain linear handler
  669. */
  670. if (bcm1250_m3_war()) {
  671. UASM_i_MFC0(&p, K0, C0_BADVADDR);
  672. UASM_i_MFC0(&p, K1, C0_ENTRYHI);
  673. uasm_i_xor(&p, K0, K0, K1);
  674. UASM_i_SRL(&p, K0, K0, PAGE_SHIFT + 1);
  675. uasm_il_bnez(&p, &r, K0, label_leave);
  676. /* No need for uasm_i_nop */
  677. }
  678. #ifdef CONFIG_64BIT
  679. build_get_pmde64(&p, &l, &r, K0, K1); /* get pmd in K1 */
  680. #else
  681. build_get_pgde32(&p, K0, K1); /* get pgd in K1 */
  682. #endif
  683. #ifdef CONFIG_HUGETLB_PAGE
  684. build_is_huge_pte(&p, &r, K0, K1, label_tlb_huge_update);
  685. #endif
  686. build_get_ptep(&p, K0, K1);
  687. build_update_entries(&p, K0, K1);
  688. build_tlb_write_entry(&p, &l, &r, tlb_random);
  689. uasm_l_leave(&l, p);
  690. uasm_i_eret(&p); /* return from trap */
  691. #ifdef CONFIG_HUGETLB_PAGE
  692. uasm_l_tlb_huge_update(&l, p);
  693. UASM_i_LW(&p, K0, 0, K1);
  694. build_huge_update_entries(&p, K0, K1);
  695. build_huge_tlb_write_entry(&p, &l, &r, K0, tlb_random);
  696. #endif
  697. #ifdef CONFIG_64BIT
  698. build_get_pgd_vmalloc64(&p, &l, &r, K0, K1);
  699. #endif
  700. /*
  701. * Overflow check: For the 64bit handler, we need at least one
  702. * free instruction slot for the wrap-around branch. In worst
  703. * case, if the intended insertion point is a delay slot, we
  704. * need three, with the second nop'ed and the third being
  705. * unused.
  706. */
  707. /* Loongson2 ebase is different than r4k, we have more space */
  708. #if defined(CONFIG_32BIT) || defined(CONFIG_CPU_LOONGSON2)
  709. if ((p - tlb_handler) > 64)
  710. panic("TLB refill handler space exceeded");
  711. #else
  712. if (((p - tlb_handler) > (MIPS64_REFILL_INSNS * 2) - 1)
  713. || (((p - tlb_handler) > (MIPS64_REFILL_INSNS * 2) - 3)
  714. && uasm_insn_has_bdelay(relocs,
  715. tlb_handler + MIPS64_REFILL_INSNS - 3)))
  716. panic("TLB refill handler space exceeded");
  717. #endif
  718. /*
  719. * Now fold the handler in the TLB refill handler space.
  720. */
  721. #if defined(CONFIG_32BIT) || defined(CONFIG_CPU_LOONGSON2)
  722. f = final_handler;
  723. /* Simplest case, just copy the handler. */
  724. uasm_copy_handler(relocs, labels, tlb_handler, p, f);
  725. final_len = p - tlb_handler;
  726. #else /* CONFIG_64BIT */
  727. f = final_handler + MIPS64_REFILL_INSNS;
  728. if ((p - tlb_handler) <= MIPS64_REFILL_INSNS) {
  729. /* Just copy the handler. */
  730. uasm_copy_handler(relocs, labels, tlb_handler, p, f);
  731. final_len = p - tlb_handler;
  732. } else {
  733. #if defined(CONFIG_HUGETLB_PAGE)
  734. const enum label_id ls = label_tlb_huge_update;
  735. #elif defined(MODULE_START)
  736. const enum label_id ls = label_module_alloc;
  737. #else
  738. const enum label_id ls = label_vmalloc;
  739. #endif
  740. u32 *split;
  741. int ov = 0;
  742. int i;
  743. for (i = 0; i < ARRAY_SIZE(labels) && labels[i].lab != ls; i++)
  744. ;
  745. BUG_ON(i == ARRAY_SIZE(labels));
  746. split = labels[i].addr;
  747. /*
  748. * See if we have overflown one way or the other.
  749. */
  750. if (split > tlb_handler + MIPS64_REFILL_INSNS ||
  751. split < p - MIPS64_REFILL_INSNS)
  752. ov = 1;
  753. if (ov) {
  754. /*
  755. * Split two instructions before the end. One
  756. * for the branch and one for the instruction
  757. * in the delay slot.
  758. */
  759. split = tlb_handler + MIPS64_REFILL_INSNS - 2;
  760. /*
  761. * If the branch would fall in a delay slot,
  762. * we must back up an additional instruction
  763. * so that it is no longer in a delay slot.
  764. */
  765. if (uasm_insn_has_bdelay(relocs, split - 1))
  766. split--;
  767. }
  768. /* Copy first part of the handler. */
  769. uasm_copy_handler(relocs, labels, tlb_handler, split, f);
  770. f += split - tlb_handler;
  771. if (ov) {
  772. /* Insert branch. */
  773. uasm_l_split(&l, final_handler);
  774. uasm_il_b(&f, &r, label_split);
  775. if (uasm_insn_has_bdelay(relocs, split))
  776. uasm_i_nop(&f);
  777. else {
  778. uasm_copy_handler(relocs, labels,
  779. split, split + 1, f);
  780. uasm_move_labels(labels, f, f + 1, -1);
  781. f++;
  782. split++;
  783. }
  784. }
  785. /* Copy the rest of the handler. */
  786. uasm_copy_handler(relocs, labels, split, p, final_handler);
  787. final_len = (f - (final_handler + MIPS64_REFILL_INSNS)) +
  788. (p - split);
  789. }
  790. #endif /* CONFIG_64BIT */
  791. uasm_resolve_relocs(relocs, labels);
  792. pr_debug("Wrote TLB refill handler (%u instructions).\n",
  793. final_len);
  794. memcpy((void *)ebase, final_handler, 0x100);
  795. dump_handler((u32 *)ebase, 64);
  796. }
  797. /*
  798. * TLB load/store/modify handlers.
  799. *
  800. * Only the fastpath gets synthesized at runtime, the slowpath for
  801. * do_page_fault remains normal asm.
  802. */
  803. extern void tlb_do_page_fault_0(void);
  804. extern void tlb_do_page_fault_1(void);
  805. /*
  806. * 128 instructions for the fastpath handler is generous and should
  807. * never be exceeded.
  808. */
  809. #define FASTPATH_SIZE 128
  810. u32 handle_tlbl[FASTPATH_SIZE] __cacheline_aligned;
  811. u32 handle_tlbs[FASTPATH_SIZE] __cacheline_aligned;
  812. u32 handle_tlbm[FASTPATH_SIZE] __cacheline_aligned;
  813. static void __cpuinit
  814. iPTE_LW(u32 **p, unsigned int pte, unsigned int ptr)
  815. {
  816. #ifdef CONFIG_SMP
  817. # ifdef CONFIG_64BIT_PHYS_ADDR
  818. if (cpu_has_64bits)
  819. uasm_i_lld(p, pte, 0, ptr);
  820. else
  821. # endif
  822. UASM_i_LL(p, pte, 0, ptr);
  823. #else
  824. # ifdef CONFIG_64BIT_PHYS_ADDR
  825. if (cpu_has_64bits)
  826. uasm_i_ld(p, pte, 0, ptr);
  827. else
  828. # endif
  829. UASM_i_LW(p, pte, 0, ptr);
  830. #endif
  831. }
  832. static void __cpuinit
  833. iPTE_SW(u32 **p, struct uasm_reloc **r, unsigned int pte, unsigned int ptr,
  834. unsigned int mode)
  835. {
  836. #ifdef CONFIG_64BIT_PHYS_ADDR
  837. unsigned int hwmode = mode & (_PAGE_VALID | _PAGE_DIRTY);
  838. #endif
  839. uasm_i_ori(p, pte, pte, mode);
  840. #ifdef CONFIG_SMP
  841. # ifdef CONFIG_64BIT_PHYS_ADDR
  842. if (cpu_has_64bits)
  843. uasm_i_scd(p, pte, 0, ptr);
  844. else
  845. # endif
  846. UASM_i_SC(p, pte, 0, ptr);
  847. if (r10000_llsc_war())
  848. uasm_il_beqzl(p, r, pte, label_smp_pgtable_change);
  849. else
  850. uasm_il_beqz(p, r, pte, label_smp_pgtable_change);
  851. # ifdef CONFIG_64BIT_PHYS_ADDR
  852. if (!cpu_has_64bits) {
  853. /* no uasm_i_nop needed */
  854. uasm_i_ll(p, pte, sizeof(pte_t) / 2, ptr);
  855. uasm_i_ori(p, pte, pte, hwmode);
  856. uasm_i_sc(p, pte, sizeof(pte_t) / 2, ptr);
  857. uasm_il_beqz(p, r, pte, label_smp_pgtable_change);
  858. /* no uasm_i_nop needed */
  859. uasm_i_lw(p, pte, 0, ptr);
  860. } else
  861. uasm_i_nop(p);
  862. # else
  863. uasm_i_nop(p);
  864. # endif
  865. #else
  866. # ifdef CONFIG_64BIT_PHYS_ADDR
  867. if (cpu_has_64bits)
  868. uasm_i_sd(p, pte, 0, ptr);
  869. else
  870. # endif
  871. UASM_i_SW(p, pte, 0, ptr);
  872. # ifdef CONFIG_64BIT_PHYS_ADDR
  873. if (!cpu_has_64bits) {
  874. uasm_i_lw(p, pte, sizeof(pte_t) / 2, ptr);
  875. uasm_i_ori(p, pte, pte, hwmode);
  876. uasm_i_sw(p, pte, sizeof(pte_t) / 2, ptr);
  877. uasm_i_lw(p, pte, 0, ptr);
  878. }
  879. # endif
  880. #endif
  881. }
  882. /*
  883. * Check if PTE is present, if not then jump to LABEL. PTR points to
  884. * the page table where this PTE is located, PTE will be re-loaded
  885. * with it's original value.
  886. */
  887. static void __cpuinit
  888. build_pte_present(u32 **p, struct uasm_reloc **r,
  889. unsigned int pte, unsigned int ptr, enum label_id lid)
  890. {
  891. uasm_i_andi(p, pte, pte, _PAGE_PRESENT | _PAGE_READ);
  892. uasm_i_xori(p, pte, pte, _PAGE_PRESENT | _PAGE_READ);
  893. uasm_il_bnez(p, r, pte, lid);
  894. iPTE_LW(p, pte, ptr);
  895. }
  896. /* Make PTE valid, store result in PTR. */
  897. static void __cpuinit
  898. build_make_valid(u32 **p, struct uasm_reloc **r, unsigned int pte,
  899. unsigned int ptr)
  900. {
  901. unsigned int mode = _PAGE_VALID | _PAGE_ACCESSED;
  902. iPTE_SW(p, r, pte, ptr, mode);
  903. }
  904. /*
  905. * Check if PTE can be written to, if not branch to LABEL. Regardless
  906. * restore PTE with value from PTR when done.
  907. */
  908. static void __cpuinit
  909. build_pte_writable(u32 **p, struct uasm_reloc **r,
  910. unsigned int pte, unsigned int ptr, enum label_id lid)
  911. {
  912. uasm_i_andi(p, pte, pte, _PAGE_PRESENT | _PAGE_WRITE);
  913. uasm_i_xori(p, pte, pte, _PAGE_PRESENT | _PAGE_WRITE);
  914. uasm_il_bnez(p, r, pte, lid);
  915. iPTE_LW(p, pte, ptr);
  916. }
  917. /* Make PTE writable, update software status bits as well, then store
  918. * at PTR.
  919. */
  920. static void __cpuinit
  921. build_make_write(u32 **p, struct uasm_reloc **r, unsigned int pte,
  922. unsigned int ptr)
  923. {
  924. unsigned int mode = (_PAGE_ACCESSED | _PAGE_MODIFIED | _PAGE_VALID
  925. | _PAGE_DIRTY);
  926. iPTE_SW(p, r, pte, ptr, mode);
  927. }
  928. /*
  929. * Check if PTE can be modified, if not branch to LABEL. Regardless
  930. * restore PTE with value from PTR when done.
  931. */
  932. static void __cpuinit
  933. build_pte_modifiable(u32 **p, struct uasm_reloc **r,
  934. unsigned int pte, unsigned int ptr, enum label_id lid)
  935. {
  936. uasm_i_andi(p, pte, pte, _PAGE_WRITE);
  937. uasm_il_beqz(p, r, pte, lid);
  938. iPTE_LW(p, pte, ptr);
  939. }
  940. #ifndef CONFIG_MIPS_PGD_C0_CONTEXT
  941. /*
  942. * R3000 style TLB load/store/modify handlers.
  943. */
  944. /*
  945. * This places the pte into ENTRYLO0 and writes it with tlbwi.
  946. * Then it returns.
  947. */
  948. static void __cpuinit
  949. build_r3000_pte_reload_tlbwi(u32 **p, unsigned int pte, unsigned int tmp)
  950. {
  951. uasm_i_mtc0(p, pte, C0_ENTRYLO0); /* cp0 delay */
  952. uasm_i_mfc0(p, tmp, C0_EPC); /* cp0 delay */
  953. uasm_i_tlbwi(p);
  954. uasm_i_jr(p, tmp);
  955. uasm_i_rfe(p); /* branch delay */
  956. }
  957. /*
  958. * This places the pte into ENTRYLO0 and writes it with tlbwi
  959. * or tlbwr as appropriate. This is because the index register
  960. * may have the probe fail bit set as a result of a trap on a
  961. * kseg2 access, i.e. without refill. Then it returns.
  962. */
  963. static void __cpuinit
  964. build_r3000_tlb_reload_write(u32 **p, struct uasm_label **l,
  965. struct uasm_reloc **r, unsigned int pte,
  966. unsigned int tmp)
  967. {
  968. uasm_i_mfc0(p, tmp, C0_INDEX);
  969. uasm_i_mtc0(p, pte, C0_ENTRYLO0); /* cp0 delay */
  970. uasm_il_bltz(p, r, tmp, label_r3000_write_probe_fail); /* cp0 delay */
  971. uasm_i_mfc0(p, tmp, C0_EPC); /* branch delay */
  972. uasm_i_tlbwi(p); /* cp0 delay */
  973. uasm_i_jr(p, tmp);
  974. uasm_i_rfe(p); /* branch delay */
  975. uasm_l_r3000_write_probe_fail(l, *p);
  976. uasm_i_tlbwr(p); /* cp0 delay */
  977. uasm_i_jr(p, tmp);
  978. uasm_i_rfe(p); /* branch delay */
  979. }
  980. static void __cpuinit
  981. build_r3000_tlbchange_handler_head(u32 **p, unsigned int pte,
  982. unsigned int ptr)
  983. {
  984. long pgdc = (long)pgd_current;
  985. uasm_i_mfc0(p, pte, C0_BADVADDR);
  986. uasm_i_lui(p, ptr, uasm_rel_hi(pgdc)); /* cp0 delay */
  987. uasm_i_lw(p, ptr, uasm_rel_lo(pgdc), ptr);
  988. uasm_i_srl(p, pte, pte, 22); /* load delay */
  989. uasm_i_sll(p, pte, pte, 2);
  990. uasm_i_addu(p, ptr, ptr, pte);
  991. uasm_i_mfc0(p, pte, C0_CONTEXT);
  992. uasm_i_lw(p, ptr, 0, ptr); /* cp0 delay */
  993. uasm_i_andi(p, pte, pte, 0xffc); /* load delay */
  994. uasm_i_addu(p, ptr, ptr, pte);
  995. uasm_i_lw(p, pte, 0, ptr);
  996. uasm_i_tlbp(p); /* load delay */
  997. }
  998. static void __cpuinit build_r3000_tlb_load_handler(void)
  999. {
  1000. u32 *p = handle_tlbl;
  1001. struct uasm_label *l = labels;
  1002. struct uasm_reloc *r = relocs;
  1003. memset(handle_tlbl, 0, sizeof(handle_tlbl));
  1004. memset(labels, 0, sizeof(labels));
  1005. memset(relocs, 0, sizeof(relocs));
  1006. build_r3000_tlbchange_handler_head(&p, K0, K1);
  1007. build_pte_present(&p, &r, K0, K1, label_nopage_tlbl);
  1008. uasm_i_nop(&p); /* load delay */
  1009. build_make_valid(&p, &r, K0, K1);
  1010. build_r3000_tlb_reload_write(&p, &l, &r, K0, K1);
  1011. uasm_l_nopage_tlbl(&l, p);
  1012. uasm_i_j(&p, (unsigned long)tlb_do_page_fault_0 & 0x0fffffff);
  1013. uasm_i_nop(&p);
  1014. if ((p - handle_tlbl) > FASTPATH_SIZE)
  1015. panic("TLB load handler fastpath space exceeded");
  1016. uasm_resolve_relocs(relocs, labels);
  1017. pr_debug("Wrote TLB load handler fastpath (%u instructions).\n",
  1018. (unsigned int)(p - handle_tlbl));
  1019. dump_handler(handle_tlbl, ARRAY_SIZE(handle_tlbl));
  1020. }
  1021. static void __cpuinit build_r3000_tlb_store_handler(void)
  1022. {
  1023. u32 *p = handle_tlbs;
  1024. struct uasm_label *l = labels;
  1025. struct uasm_reloc *r = relocs;
  1026. memset(handle_tlbs, 0, sizeof(handle_tlbs));
  1027. memset(labels, 0, sizeof(labels));
  1028. memset(relocs, 0, sizeof(relocs));
  1029. build_r3000_tlbchange_handler_head(&p, K0, K1);
  1030. build_pte_writable(&p, &r, K0, K1, label_nopage_tlbs);
  1031. uasm_i_nop(&p); /* load delay */
  1032. build_make_write(&p, &r, K0, K1);
  1033. build_r3000_tlb_reload_write(&p, &l, &r, K0, K1);
  1034. uasm_l_nopage_tlbs(&l, p);
  1035. uasm_i_j(&p, (unsigned long)tlb_do_page_fault_1 & 0x0fffffff);
  1036. uasm_i_nop(&p);
  1037. if ((p - handle_tlbs) > FASTPATH_SIZE)
  1038. panic("TLB store handler fastpath space exceeded");
  1039. uasm_resolve_relocs(relocs, labels);
  1040. pr_debug("Wrote TLB store handler fastpath (%u instructions).\n",
  1041. (unsigned int)(p - handle_tlbs));
  1042. dump_handler(handle_tlbs, ARRAY_SIZE(handle_tlbs));
  1043. }
  1044. static void __cpuinit build_r3000_tlb_modify_handler(void)
  1045. {
  1046. u32 *p = handle_tlbm;
  1047. struct uasm_label *l = labels;
  1048. struct uasm_reloc *r = relocs;
  1049. memset(handle_tlbm, 0, sizeof(handle_tlbm));
  1050. memset(labels, 0, sizeof(labels));
  1051. memset(relocs, 0, sizeof(relocs));
  1052. build_r3000_tlbchange_handler_head(&p, K0, K1);
  1053. build_pte_modifiable(&p, &r, K0, K1, label_nopage_tlbm);
  1054. uasm_i_nop(&p); /* load delay */
  1055. build_make_write(&p, &r, K0, K1);
  1056. build_r3000_pte_reload_tlbwi(&p, K0, K1);
  1057. uasm_l_nopage_tlbm(&l, p);
  1058. uasm_i_j(&p, (unsigned long)tlb_do_page_fault_1 & 0x0fffffff);
  1059. uasm_i_nop(&p);
  1060. if ((p - handle_tlbm) > FASTPATH_SIZE)
  1061. panic("TLB modify handler fastpath space exceeded");
  1062. uasm_resolve_relocs(relocs, labels);
  1063. pr_debug("Wrote TLB modify handler fastpath (%u instructions).\n",
  1064. (unsigned int)(p - handle_tlbm));
  1065. dump_handler(handle_tlbm, ARRAY_SIZE(handle_tlbm));
  1066. }
  1067. #endif /* CONFIG_MIPS_PGD_C0_CONTEXT */
  1068. /*
  1069. * R4000 style TLB load/store/modify handlers.
  1070. */
  1071. static void __cpuinit
  1072. build_r4000_tlbchange_handler_head(u32 **p, struct uasm_label **l,
  1073. struct uasm_reloc **r, unsigned int pte,
  1074. unsigned int ptr)
  1075. {
  1076. #ifdef CONFIG_64BIT
  1077. build_get_pmde64(p, l, r, pte, ptr); /* get pmd in ptr */
  1078. #else
  1079. build_get_pgde32(p, pte, ptr); /* get pgd in ptr */
  1080. #endif
  1081. #ifdef CONFIG_HUGETLB_PAGE
  1082. /*
  1083. * For huge tlb entries, pmd doesn't contain an address but
  1084. * instead contains the tlb pte. Check the PAGE_HUGE bit and
  1085. * see if we need to jump to huge tlb processing.
  1086. */
  1087. build_is_huge_pte(p, r, pte, ptr, label_tlb_huge_update);
  1088. #endif
  1089. UASM_i_MFC0(p, pte, C0_BADVADDR);
  1090. UASM_i_LW(p, ptr, 0, ptr);
  1091. UASM_i_SRL(p, pte, pte, PAGE_SHIFT + PTE_ORDER - PTE_T_LOG2);
  1092. uasm_i_andi(p, pte, pte, (PTRS_PER_PTE - 1) << PTE_T_LOG2);
  1093. UASM_i_ADDU(p, ptr, ptr, pte);
  1094. #ifdef CONFIG_SMP
  1095. uasm_l_smp_pgtable_change(l, *p);
  1096. #endif
  1097. iPTE_LW(p, pte, ptr); /* get even pte */
  1098. if (!m4kc_tlbp_war())
  1099. build_tlb_probe_entry(p);
  1100. }
  1101. static void __cpuinit
  1102. build_r4000_tlbchange_handler_tail(u32 **p, struct uasm_label **l,
  1103. struct uasm_reloc **r, unsigned int tmp,
  1104. unsigned int ptr)
  1105. {
  1106. uasm_i_ori(p, ptr, ptr, sizeof(pte_t));
  1107. uasm_i_xori(p, ptr, ptr, sizeof(pte_t));
  1108. build_update_entries(p, tmp, ptr);
  1109. build_tlb_write_entry(p, l, r, tlb_indexed);
  1110. uasm_l_leave(l, *p);
  1111. uasm_i_eret(p); /* return from trap */
  1112. #ifdef CONFIG_64BIT
  1113. build_get_pgd_vmalloc64(p, l, r, tmp, ptr);
  1114. #endif
  1115. }
  1116. static void __cpuinit build_r4000_tlb_load_handler(void)
  1117. {
  1118. u32 *p = handle_tlbl;
  1119. struct uasm_label *l = labels;
  1120. struct uasm_reloc *r = relocs;
  1121. memset(handle_tlbl, 0, sizeof(handle_tlbl));
  1122. memset(labels, 0, sizeof(labels));
  1123. memset(relocs, 0, sizeof(relocs));
  1124. if (bcm1250_m3_war()) {
  1125. UASM_i_MFC0(&p, K0, C0_BADVADDR);
  1126. UASM_i_MFC0(&p, K1, C0_ENTRYHI);
  1127. uasm_i_xor(&p, K0, K0, K1);
  1128. UASM_i_SRL(&p, K0, K0, PAGE_SHIFT + 1);
  1129. uasm_il_bnez(&p, &r, K0, label_leave);
  1130. /* No need for uasm_i_nop */
  1131. }
  1132. build_r4000_tlbchange_handler_head(&p, &l, &r, K0, K1);
  1133. build_pte_present(&p, &r, K0, K1, label_nopage_tlbl);
  1134. if (m4kc_tlbp_war())
  1135. build_tlb_probe_entry(&p);
  1136. build_make_valid(&p, &r, K0, K1);
  1137. build_r4000_tlbchange_handler_tail(&p, &l, &r, K0, K1);
  1138. #ifdef CONFIG_HUGETLB_PAGE
  1139. /*
  1140. * This is the entry point when build_r4000_tlbchange_handler_head
  1141. * spots a huge page.
  1142. */
  1143. uasm_l_tlb_huge_update(&l, p);
  1144. iPTE_LW(&p, K0, K1);
  1145. build_pte_present(&p, &r, K0, K1, label_nopage_tlbl);
  1146. build_tlb_probe_entry(&p);
  1147. uasm_i_ori(&p, K0, K0, (_PAGE_ACCESSED | _PAGE_VALID));
  1148. build_huge_handler_tail(&p, &r, &l, K0, K1);
  1149. #endif
  1150. uasm_l_nopage_tlbl(&l, p);
  1151. uasm_i_j(&p, (unsigned long)tlb_do_page_fault_0 & 0x0fffffff);
  1152. uasm_i_nop(&p);
  1153. if ((p - handle_tlbl) > FASTPATH_SIZE)
  1154. panic("TLB load handler fastpath space exceeded");
  1155. uasm_resolve_relocs(relocs, labels);
  1156. pr_debug("Wrote TLB load handler fastpath (%u instructions).\n",
  1157. (unsigned int)(p - handle_tlbl));
  1158. dump_handler(handle_tlbl, ARRAY_SIZE(handle_tlbl));
  1159. }
  1160. static void __cpuinit build_r4000_tlb_store_handler(void)
  1161. {
  1162. u32 *p = handle_tlbs;
  1163. struct uasm_label *l = labels;
  1164. struct uasm_reloc *r = relocs;
  1165. memset(handle_tlbs, 0, sizeof(handle_tlbs));
  1166. memset(labels, 0, sizeof(labels));
  1167. memset(relocs, 0, sizeof(relocs));
  1168. build_r4000_tlbchange_handler_head(&p, &l, &r, K0, K1);
  1169. build_pte_writable(&p, &r, K0, K1, label_nopage_tlbs);
  1170. if (m4kc_tlbp_war())
  1171. build_tlb_probe_entry(&p);
  1172. build_make_write(&p, &r, K0, K1);
  1173. build_r4000_tlbchange_handler_tail(&p, &l, &r, K0, K1);
  1174. #ifdef CONFIG_HUGETLB_PAGE
  1175. /*
  1176. * This is the entry point when
  1177. * build_r4000_tlbchange_handler_head spots a huge page.
  1178. */
  1179. uasm_l_tlb_huge_update(&l, p);
  1180. iPTE_LW(&p, K0, K1);
  1181. build_pte_writable(&p, &r, K0, K1, label_nopage_tlbs);
  1182. build_tlb_probe_entry(&p);
  1183. uasm_i_ori(&p, K0, K0,
  1184. _PAGE_ACCESSED | _PAGE_MODIFIED | _PAGE_VALID | _PAGE_DIRTY);
  1185. build_huge_handler_tail(&p, &r, &l, K0, K1);
  1186. #endif
  1187. uasm_l_nopage_tlbs(&l, p);
  1188. uasm_i_j(&p, (unsigned long)tlb_do_page_fault_1 & 0x0fffffff);
  1189. uasm_i_nop(&p);
  1190. if ((p - handle_tlbs) > FASTPATH_SIZE)
  1191. panic("TLB store handler fastpath space exceeded");
  1192. uasm_resolve_relocs(relocs, labels);
  1193. pr_debug("Wrote TLB store handler fastpath (%u instructions).\n",
  1194. (unsigned int)(p - handle_tlbs));
  1195. dump_handler(handle_tlbs, ARRAY_SIZE(handle_tlbs));
  1196. }
  1197. static void __cpuinit build_r4000_tlb_modify_handler(void)
  1198. {
  1199. u32 *p = handle_tlbm;
  1200. struct uasm_label *l = labels;
  1201. struct uasm_reloc *r = relocs;
  1202. memset(handle_tlbm, 0, sizeof(handle_tlbm));
  1203. memset(labels, 0, sizeof(labels));
  1204. memset(relocs, 0, sizeof(relocs));
  1205. build_r4000_tlbchange_handler_head(&p, &l, &r, K0, K1);
  1206. build_pte_modifiable(&p, &r, K0, K1, label_nopage_tlbm);
  1207. if (m4kc_tlbp_war())
  1208. build_tlb_probe_entry(&p);
  1209. /* Present and writable bits set, set accessed and dirty bits. */
  1210. build_make_write(&p, &r, K0, K1);
  1211. build_r4000_tlbchange_handler_tail(&p, &l, &r, K0, K1);
  1212. #ifdef CONFIG_HUGETLB_PAGE
  1213. /*
  1214. * This is the entry point when
  1215. * build_r4000_tlbchange_handler_head spots a huge page.
  1216. */
  1217. uasm_l_tlb_huge_update(&l, p);
  1218. iPTE_LW(&p, K0, K1);
  1219. build_pte_modifiable(&p, &r, K0, K1, label_nopage_tlbm);
  1220. build_tlb_probe_entry(&p);
  1221. uasm_i_ori(&p, K0, K0,
  1222. _PAGE_ACCESSED | _PAGE_MODIFIED | _PAGE_VALID | _PAGE_DIRTY);
  1223. build_huge_handler_tail(&p, &r, &l, K0, K1);
  1224. #endif
  1225. uasm_l_nopage_tlbm(&l, p);
  1226. uasm_i_j(&p, (unsigned long)tlb_do_page_fault_1 & 0x0fffffff);
  1227. uasm_i_nop(&p);
  1228. if ((p - handle_tlbm) > FASTPATH_SIZE)
  1229. panic("TLB modify handler fastpath space exceeded");
  1230. uasm_resolve_relocs(relocs, labels);
  1231. pr_debug("Wrote TLB modify handler fastpath (%u instructions).\n",
  1232. (unsigned int)(p - handle_tlbm));
  1233. dump_handler(handle_tlbm, ARRAY_SIZE(handle_tlbm));
  1234. }
  1235. void __cpuinit build_tlb_refill_handler(void)
  1236. {
  1237. /*
  1238. * The refill handler is generated per-CPU, multi-node systems
  1239. * may have local storage for it. The other handlers are only
  1240. * needed once.
  1241. */
  1242. static int run_once = 0;
  1243. switch (current_cpu_type()) {
  1244. case CPU_R2000:
  1245. case CPU_R3000:
  1246. case CPU_R3000A:
  1247. case CPU_R3081E:
  1248. case CPU_TX3912:
  1249. case CPU_TX3922:
  1250. case CPU_TX3927:
  1251. #ifndef CONFIG_MIPS_PGD_C0_CONTEXT
  1252. build_r3000_tlb_refill_handler();
  1253. if (!run_once) {
  1254. build_r3000_tlb_load_handler();
  1255. build_r3000_tlb_store_handler();
  1256. build_r3000_tlb_modify_handler();
  1257. run_once++;
  1258. }
  1259. #else
  1260. panic("No R3000 TLB refill handler");
  1261. #endif
  1262. break;
  1263. case CPU_R6000:
  1264. case CPU_R6000A:
  1265. panic("No R6000 TLB refill handler yet");
  1266. break;
  1267. case CPU_R8000:
  1268. panic("No R8000 TLB refill handler yet");
  1269. break;
  1270. default:
  1271. build_r4000_tlb_refill_handler();
  1272. if (!run_once) {
  1273. build_r4000_tlb_load_handler();
  1274. build_r4000_tlb_store_handler();
  1275. build_r4000_tlb_modify_handler();
  1276. run_once++;
  1277. }
  1278. }
  1279. }
  1280. void __cpuinit flush_tlb_handlers(void)
  1281. {
  1282. local_flush_icache_range((unsigned long)handle_tlbl,
  1283. (unsigned long)handle_tlbl + sizeof(handle_tlbl));
  1284. local_flush_icache_range((unsigned long)handle_tlbs,
  1285. (unsigned long)handle_tlbs + sizeof(handle_tlbs));
  1286. local_flush_icache_range((unsigned long)handle_tlbm,
  1287. (unsigned long)handle_tlbm + sizeof(handle_tlbm));
  1288. }