tlbex.c 33 KB

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