tlbex.c 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284
  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. case CPU_CAVIUM_OCTEON:
  284. case CPU_R5500:
  285. if (m4kc_tlbp_war())
  286. uasm_i_nop(p);
  287. tlbw(p);
  288. break;
  289. case CPU_NEVADA:
  290. uasm_i_nop(p); /* QED specifies 2 nops hazard */
  291. /*
  292. * This branch uses up a mtc0 hazard nop slot and saves
  293. * a nop after the tlbw instruction.
  294. */
  295. uasm_il_bgezl(p, r, 0, label_tlbw_hazard);
  296. tlbw(p);
  297. uasm_l_tlbw_hazard(l, *p);
  298. break;
  299. case CPU_RM7000:
  300. uasm_i_nop(p);
  301. uasm_i_nop(p);
  302. uasm_i_nop(p);
  303. uasm_i_nop(p);
  304. tlbw(p);
  305. break;
  306. case CPU_RM9000:
  307. /*
  308. * When the JTLB is updated by tlbwi or tlbwr, a subsequent
  309. * use of the JTLB for instructions should not occur for 4
  310. * cpu cycles and use for data translations should not occur
  311. * for 3 cpu cycles.
  312. */
  313. uasm_i_ssnop(p);
  314. uasm_i_ssnop(p);
  315. uasm_i_ssnop(p);
  316. uasm_i_ssnop(p);
  317. tlbw(p);
  318. uasm_i_ssnop(p);
  319. uasm_i_ssnop(p);
  320. uasm_i_ssnop(p);
  321. uasm_i_ssnop(p);
  322. break;
  323. case CPU_VR4111:
  324. case CPU_VR4121:
  325. case CPU_VR4122:
  326. case CPU_VR4181:
  327. case CPU_VR4181A:
  328. uasm_i_nop(p);
  329. uasm_i_nop(p);
  330. tlbw(p);
  331. uasm_i_nop(p);
  332. uasm_i_nop(p);
  333. break;
  334. case CPU_VR4131:
  335. case CPU_VR4133:
  336. case CPU_R5432:
  337. uasm_i_nop(p);
  338. uasm_i_nop(p);
  339. tlbw(p);
  340. break;
  341. default:
  342. panic("No TLB refill handler yet (CPU type: %d)",
  343. current_cpu_data.cputype);
  344. break;
  345. }
  346. }
  347. #ifdef CONFIG_64BIT
  348. /*
  349. * TMP and PTR are scratch.
  350. * TMP will be clobbered, PTR will hold the pmd entry.
  351. */
  352. static void __cpuinit
  353. build_get_pmde64(u32 **p, struct uasm_label **l, struct uasm_reloc **r,
  354. unsigned int tmp, unsigned int ptr)
  355. {
  356. long pgdc = (long)pgd_current;
  357. /*
  358. * The vmalloc handling is not in the hotpath.
  359. */
  360. uasm_i_dmfc0(p, tmp, C0_BADVADDR);
  361. #ifdef MODULE_START
  362. uasm_il_bltz(p, r, tmp, label_module_alloc);
  363. #else
  364. uasm_il_bltz(p, r, tmp, label_vmalloc);
  365. #endif
  366. /* No uasm_i_nop needed here, since the next insn doesn't touch TMP. */
  367. #ifdef CONFIG_SMP
  368. # ifdef CONFIG_MIPS_MT_SMTC
  369. /*
  370. * SMTC uses TCBind value as "CPU" index
  371. */
  372. uasm_i_mfc0(p, ptr, C0_TCBIND);
  373. uasm_i_dsrl(p, ptr, ptr, 19);
  374. # else
  375. /*
  376. * 64 bit SMP running in XKPHYS has smp_processor_id() << 3
  377. * stored in CONTEXT.
  378. */
  379. uasm_i_dmfc0(p, ptr, C0_CONTEXT);
  380. uasm_i_dsrl(p, ptr, ptr, 23);
  381. #endif
  382. UASM_i_LA_mostly(p, tmp, pgdc);
  383. uasm_i_daddu(p, ptr, ptr, tmp);
  384. uasm_i_dmfc0(p, tmp, C0_BADVADDR);
  385. uasm_i_ld(p, ptr, uasm_rel_lo(pgdc), ptr);
  386. #else
  387. UASM_i_LA_mostly(p, ptr, pgdc);
  388. uasm_i_ld(p, ptr, uasm_rel_lo(pgdc), ptr);
  389. #endif
  390. uasm_l_vmalloc_done(l, *p);
  391. if (PGDIR_SHIFT - 3 < 32) /* get pgd offset in bytes */
  392. uasm_i_dsrl(p, tmp, tmp, PGDIR_SHIFT-3);
  393. else
  394. uasm_i_dsrl32(p, tmp, tmp, PGDIR_SHIFT - 3 - 32);
  395. uasm_i_andi(p, tmp, tmp, (PTRS_PER_PGD - 1)<<3);
  396. uasm_i_daddu(p, ptr, ptr, tmp); /* add in pgd offset */
  397. uasm_i_dmfc0(p, tmp, C0_BADVADDR); /* get faulting address */
  398. uasm_i_ld(p, ptr, 0, ptr); /* get pmd pointer */
  399. uasm_i_dsrl(p, tmp, tmp, PMD_SHIFT-3); /* get pmd offset in bytes */
  400. uasm_i_andi(p, tmp, tmp, (PTRS_PER_PMD - 1)<<3);
  401. uasm_i_daddu(p, ptr, ptr, tmp); /* add in pmd offset */
  402. }
  403. /*
  404. * BVADDR is the faulting address, PTR is scratch.
  405. * PTR will hold the pgd for vmalloc.
  406. */
  407. static void __cpuinit
  408. build_get_pgd_vmalloc64(u32 **p, struct uasm_label **l, struct uasm_reloc **r,
  409. unsigned int bvaddr, unsigned int ptr)
  410. {
  411. long swpd = (long)swapper_pg_dir;
  412. #ifdef MODULE_START
  413. long modd = (long)module_pg_dir;
  414. uasm_l_module_alloc(l, *p);
  415. /*
  416. * Assumption:
  417. * VMALLOC_START >= 0xc000000000000000UL
  418. * MODULE_START >= 0xe000000000000000UL
  419. */
  420. UASM_i_SLL(p, ptr, bvaddr, 2);
  421. uasm_il_bgez(p, r, ptr, label_vmalloc);
  422. if (uasm_in_compat_space_p(MODULE_START) &&
  423. !uasm_rel_lo(MODULE_START)) {
  424. uasm_i_lui(p, ptr, uasm_rel_hi(MODULE_START)); /* delay slot */
  425. } else {
  426. /* unlikely configuration */
  427. uasm_i_nop(p); /* delay slot */
  428. UASM_i_LA(p, ptr, MODULE_START);
  429. }
  430. uasm_i_dsubu(p, bvaddr, bvaddr, ptr);
  431. if (uasm_in_compat_space_p(modd) && !uasm_rel_lo(modd)) {
  432. uasm_il_b(p, r, label_vmalloc_done);
  433. uasm_i_lui(p, ptr, uasm_rel_hi(modd));
  434. } else {
  435. UASM_i_LA_mostly(p, ptr, modd);
  436. uasm_il_b(p, r, label_vmalloc_done);
  437. if (uasm_in_compat_space_p(modd))
  438. uasm_i_addiu(p, ptr, ptr, uasm_rel_lo(modd));
  439. else
  440. uasm_i_daddiu(p, ptr, ptr, uasm_rel_lo(modd));
  441. }
  442. uasm_l_vmalloc(l, *p);
  443. if (uasm_in_compat_space_p(MODULE_START) &&
  444. !uasm_rel_lo(MODULE_START) &&
  445. MODULE_START << 32 == VMALLOC_START)
  446. uasm_i_dsll32(p, ptr, ptr, 0); /* typical case */
  447. else
  448. UASM_i_LA(p, ptr, VMALLOC_START);
  449. #else
  450. uasm_l_vmalloc(l, *p);
  451. UASM_i_LA(p, ptr, VMALLOC_START);
  452. #endif
  453. uasm_i_dsubu(p, bvaddr, bvaddr, ptr);
  454. if (uasm_in_compat_space_p(swpd) && !uasm_rel_lo(swpd)) {
  455. uasm_il_b(p, r, label_vmalloc_done);
  456. uasm_i_lui(p, ptr, uasm_rel_hi(swpd));
  457. } else {
  458. UASM_i_LA_mostly(p, ptr, swpd);
  459. uasm_il_b(p, r, label_vmalloc_done);
  460. if (uasm_in_compat_space_p(swpd))
  461. uasm_i_addiu(p, ptr, ptr, uasm_rel_lo(swpd));
  462. else
  463. uasm_i_daddiu(p, ptr, ptr, uasm_rel_lo(swpd));
  464. }
  465. }
  466. #else /* !CONFIG_64BIT */
  467. /*
  468. * TMP and PTR are scratch.
  469. * TMP will be clobbered, PTR will hold the pgd entry.
  470. */
  471. static void __cpuinit __maybe_unused
  472. build_get_pgde32(u32 **p, unsigned int tmp, unsigned int ptr)
  473. {
  474. long pgdc = (long)pgd_current;
  475. /* 32 bit SMP has smp_processor_id() stored in CONTEXT. */
  476. #ifdef CONFIG_SMP
  477. #ifdef CONFIG_MIPS_MT_SMTC
  478. /*
  479. * SMTC uses TCBind value as "CPU" index
  480. */
  481. uasm_i_mfc0(p, ptr, C0_TCBIND);
  482. UASM_i_LA_mostly(p, tmp, pgdc);
  483. uasm_i_srl(p, ptr, ptr, 19);
  484. #else
  485. /*
  486. * smp_processor_id() << 3 is stored in CONTEXT.
  487. */
  488. uasm_i_mfc0(p, ptr, C0_CONTEXT);
  489. UASM_i_LA_mostly(p, tmp, pgdc);
  490. uasm_i_srl(p, ptr, ptr, 23);
  491. #endif
  492. uasm_i_addu(p, ptr, tmp, ptr);
  493. #else
  494. UASM_i_LA_mostly(p, ptr, pgdc);
  495. #endif
  496. uasm_i_mfc0(p, tmp, C0_BADVADDR); /* get faulting address */
  497. uasm_i_lw(p, ptr, uasm_rel_lo(pgdc), ptr);
  498. uasm_i_srl(p, tmp, tmp, PGDIR_SHIFT); /* get pgd only bits */
  499. uasm_i_sll(p, tmp, tmp, PGD_T_LOG2);
  500. uasm_i_addu(p, ptr, ptr, tmp); /* add in pgd offset */
  501. }
  502. #endif /* !CONFIG_64BIT */
  503. static void __cpuinit build_adjust_context(u32 **p, unsigned int ctx)
  504. {
  505. unsigned int shift = 4 - (PTE_T_LOG2 + 1) + PAGE_SHIFT - 12;
  506. unsigned int mask = (PTRS_PER_PTE / 2 - 1) << (PTE_T_LOG2 + 1);
  507. switch (current_cpu_type()) {
  508. case CPU_VR41XX:
  509. case CPU_VR4111:
  510. case CPU_VR4121:
  511. case CPU_VR4122:
  512. case CPU_VR4131:
  513. case CPU_VR4181:
  514. case CPU_VR4181A:
  515. case CPU_VR4133:
  516. shift += 2;
  517. break;
  518. default:
  519. break;
  520. }
  521. if (shift)
  522. UASM_i_SRL(p, ctx, ctx, shift);
  523. uasm_i_andi(p, ctx, ctx, mask);
  524. }
  525. static void __cpuinit build_get_ptep(u32 **p, unsigned int tmp, unsigned int ptr)
  526. {
  527. /*
  528. * Bug workaround for the Nevada. It seems as if under certain
  529. * circumstances the move from cp0_context might produce a
  530. * bogus result when the mfc0 instruction and its consumer are
  531. * in a different cacheline or a load instruction, probably any
  532. * memory reference, is between them.
  533. */
  534. switch (current_cpu_type()) {
  535. case CPU_NEVADA:
  536. UASM_i_LW(p, ptr, 0, ptr);
  537. GET_CONTEXT(p, tmp); /* get context reg */
  538. break;
  539. default:
  540. GET_CONTEXT(p, tmp); /* get context reg */
  541. UASM_i_LW(p, ptr, 0, ptr);
  542. break;
  543. }
  544. build_adjust_context(p, tmp);
  545. UASM_i_ADDU(p, ptr, ptr, tmp); /* add in offset */
  546. }
  547. static void __cpuinit build_update_entries(u32 **p, unsigned int tmp,
  548. unsigned int ptep)
  549. {
  550. /*
  551. * 64bit address support (36bit on a 32bit CPU) in a 32bit
  552. * Kernel is a special case. Only a few CPUs use it.
  553. */
  554. #ifdef CONFIG_64BIT_PHYS_ADDR
  555. if (cpu_has_64bits) {
  556. uasm_i_ld(p, tmp, 0, ptep); /* get even pte */
  557. uasm_i_ld(p, ptep, sizeof(pte_t), ptep); /* get odd pte */
  558. uasm_i_dsrl(p, tmp, tmp, 6); /* convert to entrylo0 */
  559. uasm_i_mtc0(p, tmp, C0_ENTRYLO0); /* load it */
  560. uasm_i_dsrl(p, ptep, ptep, 6); /* convert to entrylo1 */
  561. uasm_i_mtc0(p, ptep, C0_ENTRYLO1); /* load it */
  562. } else {
  563. int pte_off_even = sizeof(pte_t) / 2;
  564. int pte_off_odd = pte_off_even + sizeof(pte_t);
  565. /* The pte entries are pre-shifted */
  566. uasm_i_lw(p, tmp, pte_off_even, ptep); /* get even pte */
  567. uasm_i_mtc0(p, tmp, C0_ENTRYLO0); /* load it */
  568. uasm_i_lw(p, ptep, pte_off_odd, ptep); /* get odd pte */
  569. uasm_i_mtc0(p, ptep, C0_ENTRYLO1); /* load it */
  570. }
  571. #else
  572. UASM_i_LW(p, tmp, 0, ptep); /* get even pte */
  573. UASM_i_LW(p, ptep, sizeof(pte_t), ptep); /* get odd pte */
  574. if (r45k_bvahwbug())
  575. build_tlb_probe_entry(p);
  576. UASM_i_SRL(p, tmp, tmp, 6); /* convert to entrylo0 */
  577. if (r4k_250MHZhwbug())
  578. uasm_i_mtc0(p, 0, C0_ENTRYLO0);
  579. uasm_i_mtc0(p, tmp, C0_ENTRYLO0); /* load it */
  580. UASM_i_SRL(p, ptep, ptep, 6); /* convert to entrylo1 */
  581. if (r45k_bvahwbug())
  582. uasm_i_mfc0(p, tmp, C0_INDEX);
  583. if (r4k_250MHZhwbug())
  584. uasm_i_mtc0(p, 0, C0_ENTRYLO1);
  585. uasm_i_mtc0(p, ptep, C0_ENTRYLO1); /* load it */
  586. #endif
  587. }
  588. static void __cpuinit build_r4000_tlb_refill_handler(void)
  589. {
  590. u32 *p = tlb_handler;
  591. struct uasm_label *l = labels;
  592. struct uasm_reloc *r = relocs;
  593. u32 *f;
  594. unsigned int final_len;
  595. memset(tlb_handler, 0, sizeof(tlb_handler));
  596. memset(labels, 0, sizeof(labels));
  597. memset(relocs, 0, sizeof(relocs));
  598. memset(final_handler, 0, sizeof(final_handler));
  599. /*
  600. * create the plain linear handler
  601. */
  602. if (bcm1250_m3_war()) {
  603. UASM_i_MFC0(&p, K0, C0_BADVADDR);
  604. UASM_i_MFC0(&p, K1, C0_ENTRYHI);
  605. uasm_i_xor(&p, K0, K0, K1);
  606. UASM_i_SRL(&p, K0, K0, PAGE_SHIFT + 1);
  607. uasm_il_bnez(&p, &r, K0, label_leave);
  608. /* No need for uasm_i_nop */
  609. }
  610. #ifdef CONFIG_64BIT
  611. build_get_pmde64(&p, &l, &r, K0, K1); /* get pmd in K1 */
  612. #else
  613. build_get_pgde32(&p, K0, K1); /* get pgd in K1 */
  614. #endif
  615. build_get_ptep(&p, K0, K1);
  616. build_update_entries(&p, K0, K1);
  617. build_tlb_write_entry(&p, &l, &r, tlb_random);
  618. uasm_l_leave(&l, p);
  619. uasm_i_eret(&p); /* return from trap */
  620. #ifdef CONFIG_64BIT
  621. build_get_pgd_vmalloc64(&p, &l, &r, K0, K1);
  622. #endif
  623. /*
  624. * Overflow check: For the 64bit handler, we need at least one
  625. * free instruction slot for the wrap-around branch. In worst
  626. * case, if the intended insertion point is a delay slot, we
  627. * need three, with the second nop'ed and the third being
  628. * unused.
  629. */
  630. /* Loongson2 ebase is different than r4k, we have more space */
  631. #if defined(CONFIG_32BIT) || defined(CONFIG_CPU_LOONGSON2)
  632. if ((p - tlb_handler) > 64)
  633. panic("TLB refill handler space exceeded");
  634. #else
  635. if (((p - tlb_handler) > 63)
  636. || (((p - tlb_handler) > 61)
  637. && uasm_insn_has_bdelay(relocs, tlb_handler + 29)))
  638. panic("TLB refill handler space exceeded");
  639. #endif
  640. /*
  641. * Now fold the handler in the TLB refill handler space.
  642. */
  643. #if defined(CONFIG_32BIT) || defined(CONFIG_CPU_LOONGSON2)
  644. f = final_handler;
  645. /* Simplest case, just copy the handler. */
  646. uasm_copy_handler(relocs, labels, tlb_handler, p, f);
  647. final_len = p - tlb_handler;
  648. #else /* CONFIG_64BIT */
  649. f = final_handler + 32;
  650. if ((p - tlb_handler) <= 32) {
  651. /* Just copy the handler. */
  652. uasm_copy_handler(relocs, labels, tlb_handler, p, f);
  653. final_len = p - tlb_handler;
  654. } else {
  655. u32 *split = tlb_handler + 30;
  656. /*
  657. * Find the split point.
  658. */
  659. if (uasm_insn_has_bdelay(relocs, split - 1))
  660. split--;
  661. /* Copy first part of the handler. */
  662. uasm_copy_handler(relocs, labels, tlb_handler, split, f);
  663. f += split - tlb_handler;
  664. /* Insert branch. */
  665. uasm_l_split(&l, final_handler);
  666. uasm_il_b(&f, &r, label_split);
  667. if (uasm_insn_has_bdelay(relocs, split))
  668. uasm_i_nop(&f);
  669. else {
  670. uasm_copy_handler(relocs, labels, split, split + 1, f);
  671. uasm_move_labels(labels, f, f + 1, -1);
  672. f++;
  673. split++;
  674. }
  675. /* Copy the rest of the handler. */
  676. uasm_copy_handler(relocs, labels, split, p, final_handler);
  677. final_len = (f - (final_handler + 32)) + (p - split);
  678. }
  679. #endif /* CONFIG_64BIT */
  680. uasm_resolve_relocs(relocs, labels);
  681. pr_debug("Wrote TLB refill handler (%u instructions).\n",
  682. final_len);
  683. memcpy((void *)ebase, final_handler, 0x100);
  684. dump_handler((u32 *)ebase, 64);
  685. }
  686. /*
  687. * TLB load/store/modify handlers.
  688. *
  689. * Only the fastpath gets synthesized at runtime, the slowpath for
  690. * do_page_fault remains normal asm.
  691. */
  692. extern void tlb_do_page_fault_0(void);
  693. extern void tlb_do_page_fault_1(void);
  694. /*
  695. * 128 instructions for the fastpath handler is generous and should
  696. * never be exceeded.
  697. */
  698. #define FASTPATH_SIZE 128
  699. u32 handle_tlbl[FASTPATH_SIZE] __cacheline_aligned;
  700. u32 handle_tlbs[FASTPATH_SIZE] __cacheline_aligned;
  701. u32 handle_tlbm[FASTPATH_SIZE] __cacheline_aligned;
  702. static void __cpuinit
  703. iPTE_LW(u32 **p, struct uasm_label **l, unsigned int pte, unsigned int ptr)
  704. {
  705. #ifdef CONFIG_SMP
  706. # ifdef CONFIG_64BIT_PHYS_ADDR
  707. if (cpu_has_64bits)
  708. uasm_i_lld(p, pte, 0, ptr);
  709. else
  710. # endif
  711. UASM_i_LL(p, pte, 0, ptr);
  712. #else
  713. # ifdef CONFIG_64BIT_PHYS_ADDR
  714. if (cpu_has_64bits)
  715. uasm_i_ld(p, pte, 0, ptr);
  716. else
  717. # endif
  718. UASM_i_LW(p, pte, 0, ptr);
  719. #endif
  720. }
  721. static void __cpuinit
  722. iPTE_SW(u32 **p, struct uasm_reloc **r, unsigned int pte, unsigned int ptr,
  723. unsigned int mode)
  724. {
  725. #ifdef CONFIG_64BIT_PHYS_ADDR
  726. unsigned int hwmode = mode & (_PAGE_VALID | _PAGE_DIRTY);
  727. #endif
  728. uasm_i_ori(p, pte, pte, mode);
  729. #ifdef CONFIG_SMP
  730. # ifdef CONFIG_64BIT_PHYS_ADDR
  731. if (cpu_has_64bits)
  732. uasm_i_scd(p, pte, 0, ptr);
  733. else
  734. # endif
  735. UASM_i_SC(p, pte, 0, ptr);
  736. if (r10000_llsc_war())
  737. uasm_il_beqzl(p, r, pte, label_smp_pgtable_change);
  738. else
  739. uasm_il_beqz(p, r, pte, label_smp_pgtable_change);
  740. # ifdef CONFIG_64BIT_PHYS_ADDR
  741. if (!cpu_has_64bits) {
  742. /* no uasm_i_nop needed */
  743. uasm_i_ll(p, pte, sizeof(pte_t) / 2, ptr);
  744. uasm_i_ori(p, pte, pte, hwmode);
  745. uasm_i_sc(p, pte, sizeof(pte_t) / 2, ptr);
  746. uasm_il_beqz(p, r, pte, label_smp_pgtable_change);
  747. /* no uasm_i_nop needed */
  748. uasm_i_lw(p, pte, 0, ptr);
  749. } else
  750. uasm_i_nop(p);
  751. # else
  752. uasm_i_nop(p);
  753. # endif
  754. #else
  755. # ifdef CONFIG_64BIT_PHYS_ADDR
  756. if (cpu_has_64bits)
  757. uasm_i_sd(p, pte, 0, ptr);
  758. else
  759. # endif
  760. UASM_i_SW(p, pte, 0, ptr);
  761. # ifdef CONFIG_64BIT_PHYS_ADDR
  762. if (!cpu_has_64bits) {
  763. uasm_i_lw(p, pte, sizeof(pte_t) / 2, ptr);
  764. uasm_i_ori(p, pte, pte, hwmode);
  765. uasm_i_sw(p, pte, sizeof(pte_t) / 2, ptr);
  766. uasm_i_lw(p, pte, 0, ptr);
  767. }
  768. # endif
  769. #endif
  770. }
  771. /*
  772. * Check if PTE is present, if not then jump to LABEL. PTR points to
  773. * the page table where this PTE is located, PTE will be re-loaded
  774. * with it's original value.
  775. */
  776. static void __cpuinit
  777. build_pte_present(u32 **p, struct uasm_label **l, struct uasm_reloc **r,
  778. unsigned int pte, unsigned int ptr, enum label_id lid)
  779. {
  780. uasm_i_andi(p, pte, pte, _PAGE_PRESENT | _PAGE_READ);
  781. uasm_i_xori(p, pte, pte, _PAGE_PRESENT | _PAGE_READ);
  782. uasm_il_bnez(p, r, pte, lid);
  783. iPTE_LW(p, l, pte, ptr);
  784. }
  785. /* Make PTE valid, store result in PTR. */
  786. static void __cpuinit
  787. build_make_valid(u32 **p, struct uasm_reloc **r, unsigned int pte,
  788. unsigned int ptr)
  789. {
  790. unsigned int mode = _PAGE_VALID | _PAGE_ACCESSED;
  791. iPTE_SW(p, r, pte, ptr, mode);
  792. }
  793. /*
  794. * Check if PTE can be written to, if not branch to LABEL. Regardless
  795. * restore PTE with value from PTR when done.
  796. */
  797. static void __cpuinit
  798. build_pte_writable(u32 **p, struct uasm_label **l, struct uasm_reloc **r,
  799. unsigned int pte, unsigned int ptr, enum label_id lid)
  800. {
  801. uasm_i_andi(p, pte, pte, _PAGE_PRESENT | _PAGE_WRITE);
  802. uasm_i_xori(p, pte, pte, _PAGE_PRESENT | _PAGE_WRITE);
  803. uasm_il_bnez(p, r, pte, lid);
  804. iPTE_LW(p, l, pte, ptr);
  805. }
  806. /* Make PTE writable, update software status bits as well, then store
  807. * at PTR.
  808. */
  809. static void __cpuinit
  810. build_make_write(u32 **p, struct uasm_reloc **r, unsigned int pte,
  811. unsigned int ptr)
  812. {
  813. unsigned int mode = (_PAGE_ACCESSED | _PAGE_MODIFIED | _PAGE_VALID
  814. | _PAGE_DIRTY);
  815. iPTE_SW(p, r, pte, ptr, mode);
  816. }
  817. /*
  818. * Check if PTE can be modified, if not branch to LABEL. Regardless
  819. * restore PTE with value from PTR when done.
  820. */
  821. static void __cpuinit
  822. build_pte_modifiable(u32 **p, struct uasm_label **l, struct uasm_reloc **r,
  823. unsigned int pte, unsigned int ptr, enum label_id lid)
  824. {
  825. uasm_i_andi(p, pte, pte, _PAGE_WRITE);
  826. uasm_il_beqz(p, r, pte, lid);
  827. iPTE_LW(p, l, pte, ptr);
  828. }
  829. /*
  830. * R3000 style TLB load/store/modify handlers.
  831. */
  832. /*
  833. * This places the pte into ENTRYLO0 and writes it with tlbwi.
  834. * Then it returns.
  835. */
  836. static void __cpuinit
  837. build_r3000_pte_reload_tlbwi(u32 **p, unsigned int pte, unsigned int tmp)
  838. {
  839. uasm_i_mtc0(p, pte, C0_ENTRYLO0); /* cp0 delay */
  840. uasm_i_mfc0(p, tmp, C0_EPC); /* cp0 delay */
  841. uasm_i_tlbwi(p);
  842. uasm_i_jr(p, tmp);
  843. uasm_i_rfe(p); /* branch delay */
  844. }
  845. /*
  846. * This places the pte into ENTRYLO0 and writes it with tlbwi
  847. * or tlbwr as appropriate. This is because the index register
  848. * may have the probe fail bit set as a result of a trap on a
  849. * kseg2 access, i.e. without refill. Then it returns.
  850. */
  851. static void __cpuinit
  852. build_r3000_tlb_reload_write(u32 **p, struct uasm_label **l,
  853. struct uasm_reloc **r, unsigned int pte,
  854. unsigned int tmp)
  855. {
  856. uasm_i_mfc0(p, tmp, C0_INDEX);
  857. uasm_i_mtc0(p, pte, C0_ENTRYLO0); /* cp0 delay */
  858. uasm_il_bltz(p, r, tmp, label_r3000_write_probe_fail); /* cp0 delay */
  859. uasm_i_mfc0(p, tmp, C0_EPC); /* branch delay */
  860. uasm_i_tlbwi(p); /* cp0 delay */
  861. uasm_i_jr(p, tmp);
  862. uasm_i_rfe(p); /* branch delay */
  863. uasm_l_r3000_write_probe_fail(l, *p);
  864. uasm_i_tlbwr(p); /* cp0 delay */
  865. uasm_i_jr(p, tmp);
  866. uasm_i_rfe(p); /* branch delay */
  867. }
  868. static void __cpuinit
  869. build_r3000_tlbchange_handler_head(u32 **p, unsigned int pte,
  870. unsigned int ptr)
  871. {
  872. long pgdc = (long)pgd_current;
  873. uasm_i_mfc0(p, pte, C0_BADVADDR);
  874. uasm_i_lui(p, ptr, uasm_rel_hi(pgdc)); /* cp0 delay */
  875. uasm_i_lw(p, ptr, uasm_rel_lo(pgdc), ptr);
  876. uasm_i_srl(p, pte, pte, 22); /* load delay */
  877. uasm_i_sll(p, pte, pte, 2);
  878. uasm_i_addu(p, ptr, ptr, pte);
  879. uasm_i_mfc0(p, pte, C0_CONTEXT);
  880. uasm_i_lw(p, ptr, 0, ptr); /* cp0 delay */
  881. uasm_i_andi(p, pte, pte, 0xffc); /* load delay */
  882. uasm_i_addu(p, ptr, ptr, pte);
  883. uasm_i_lw(p, pte, 0, ptr);
  884. uasm_i_tlbp(p); /* load delay */
  885. }
  886. static void __cpuinit build_r3000_tlb_load_handler(void)
  887. {
  888. u32 *p = handle_tlbl;
  889. struct uasm_label *l = labels;
  890. struct uasm_reloc *r = relocs;
  891. memset(handle_tlbl, 0, sizeof(handle_tlbl));
  892. memset(labels, 0, sizeof(labels));
  893. memset(relocs, 0, sizeof(relocs));
  894. build_r3000_tlbchange_handler_head(&p, K0, K1);
  895. build_pte_present(&p, &l, &r, K0, K1, label_nopage_tlbl);
  896. uasm_i_nop(&p); /* load delay */
  897. build_make_valid(&p, &r, K0, K1);
  898. build_r3000_tlb_reload_write(&p, &l, &r, K0, K1);
  899. uasm_l_nopage_tlbl(&l, p);
  900. uasm_i_j(&p, (unsigned long)tlb_do_page_fault_0 & 0x0fffffff);
  901. uasm_i_nop(&p);
  902. if ((p - handle_tlbl) > FASTPATH_SIZE)
  903. panic("TLB load handler fastpath space exceeded");
  904. uasm_resolve_relocs(relocs, labels);
  905. pr_debug("Wrote TLB load handler fastpath (%u instructions).\n",
  906. (unsigned int)(p - handle_tlbl));
  907. dump_handler(handle_tlbl, ARRAY_SIZE(handle_tlbl));
  908. }
  909. static void __cpuinit build_r3000_tlb_store_handler(void)
  910. {
  911. u32 *p = handle_tlbs;
  912. struct uasm_label *l = labels;
  913. struct uasm_reloc *r = relocs;
  914. memset(handle_tlbs, 0, sizeof(handle_tlbs));
  915. memset(labels, 0, sizeof(labels));
  916. memset(relocs, 0, sizeof(relocs));
  917. build_r3000_tlbchange_handler_head(&p, K0, K1);
  918. build_pte_writable(&p, &l, &r, K0, K1, label_nopage_tlbs);
  919. uasm_i_nop(&p); /* load delay */
  920. build_make_write(&p, &r, K0, K1);
  921. build_r3000_tlb_reload_write(&p, &l, &r, K0, K1);
  922. uasm_l_nopage_tlbs(&l, p);
  923. uasm_i_j(&p, (unsigned long)tlb_do_page_fault_1 & 0x0fffffff);
  924. uasm_i_nop(&p);
  925. if ((p - handle_tlbs) > FASTPATH_SIZE)
  926. panic("TLB store handler fastpath space exceeded");
  927. uasm_resolve_relocs(relocs, labels);
  928. pr_debug("Wrote TLB store handler fastpath (%u instructions).\n",
  929. (unsigned int)(p - handle_tlbs));
  930. dump_handler(handle_tlbs, ARRAY_SIZE(handle_tlbs));
  931. }
  932. static void __cpuinit build_r3000_tlb_modify_handler(void)
  933. {
  934. u32 *p = handle_tlbm;
  935. struct uasm_label *l = labels;
  936. struct uasm_reloc *r = relocs;
  937. memset(handle_tlbm, 0, sizeof(handle_tlbm));
  938. memset(labels, 0, sizeof(labels));
  939. memset(relocs, 0, sizeof(relocs));
  940. build_r3000_tlbchange_handler_head(&p, K0, K1);
  941. build_pte_modifiable(&p, &l, &r, K0, K1, label_nopage_tlbm);
  942. uasm_i_nop(&p); /* load delay */
  943. build_make_write(&p, &r, K0, K1);
  944. build_r3000_pte_reload_tlbwi(&p, K0, K1);
  945. uasm_l_nopage_tlbm(&l, p);
  946. uasm_i_j(&p, (unsigned long)tlb_do_page_fault_1 & 0x0fffffff);
  947. uasm_i_nop(&p);
  948. if ((p - handle_tlbm) > FASTPATH_SIZE)
  949. panic("TLB modify handler fastpath space exceeded");
  950. uasm_resolve_relocs(relocs, labels);
  951. pr_debug("Wrote TLB modify handler fastpath (%u instructions).\n",
  952. (unsigned int)(p - handle_tlbm));
  953. dump_handler(handle_tlbm, ARRAY_SIZE(handle_tlbm));
  954. }
  955. /*
  956. * R4000 style TLB load/store/modify handlers.
  957. */
  958. static void __cpuinit
  959. build_r4000_tlbchange_handler_head(u32 **p, struct uasm_label **l,
  960. struct uasm_reloc **r, unsigned int pte,
  961. unsigned int ptr)
  962. {
  963. #ifdef CONFIG_64BIT
  964. build_get_pmde64(p, l, r, pte, ptr); /* get pmd in ptr */
  965. #else
  966. build_get_pgde32(p, pte, ptr); /* get pgd in ptr */
  967. #endif
  968. UASM_i_MFC0(p, pte, C0_BADVADDR);
  969. UASM_i_LW(p, ptr, 0, ptr);
  970. UASM_i_SRL(p, pte, pte, PAGE_SHIFT + PTE_ORDER - PTE_T_LOG2);
  971. uasm_i_andi(p, pte, pte, (PTRS_PER_PTE - 1) << PTE_T_LOG2);
  972. UASM_i_ADDU(p, ptr, ptr, pte);
  973. #ifdef CONFIG_SMP
  974. uasm_l_smp_pgtable_change(l, *p);
  975. #endif
  976. iPTE_LW(p, l, pte, ptr); /* get even pte */
  977. if (!m4kc_tlbp_war())
  978. build_tlb_probe_entry(p);
  979. }
  980. static void __cpuinit
  981. build_r4000_tlbchange_handler_tail(u32 **p, struct uasm_label **l,
  982. struct uasm_reloc **r, unsigned int tmp,
  983. unsigned int ptr)
  984. {
  985. uasm_i_ori(p, ptr, ptr, sizeof(pte_t));
  986. uasm_i_xori(p, ptr, ptr, sizeof(pte_t));
  987. build_update_entries(p, tmp, ptr);
  988. build_tlb_write_entry(p, l, r, tlb_indexed);
  989. uasm_l_leave(l, *p);
  990. uasm_i_eret(p); /* return from trap */
  991. #ifdef CONFIG_64BIT
  992. build_get_pgd_vmalloc64(p, l, r, tmp, ptr);
  993. #endif
  994. }
  995. static void __cpuinit build_r4000_tlb_load_handler(void)
  996. {
  997. u32 *p = handle_tlbl;
  998. struct uasm_label *l = labels;
  999. struct uasm_reloc *r = relocs;
  1000. memset(handle_tlbl, 0, sizeof(handle_tlbl));
  1001. memset(labels, 0, sizeof(labels));
  1002. memset(relocs, 0, sizeof(relocs));
  1003. if (bcm1250_m3_war()) {
  1004. UASM_i_MFC0(&p, K0, C0_BADVADDR);
  1005. UASM_i_MFC0(&p, K1, C0_ENTRYHI);
  1006. uasm_i_xor(&p, K0, K0, K1);
  1007. UASM_i_SRL(&p, K0, K0, PAGE_SHIFT + 1);
  1008. uasm_il_bnez(&p, &r, K0, label_leave);
  1009. /* No need for uasm_i_nop */
  1010. }
  1011. build_r4000_tlbchange_handler_head(&p, &l, &r, K0, K1);
  1012. build_pte_present(&p, &l, &r, K0, K1, label_nopage_tlbl);
  1013. if (m4kc_tlbp_war())
  1014. build_tlb_probe_entry(&p);
  1015. build_make_valid(&p, &r, K0, K1);
  1016. build_r4000_tlbchange_handler_tail(&p, &l, &r, K0, K1);
  1017. uasm_l_nopage_tlbl(&l, p);
  1018. uasm_i_j(&p, (unsigned long)tlb_do_page_fault_0 & 0x0fffffff);
  1019. uasm_i_nop(&p);
  1020. if ((p - handle_tlbl) > FASTPATH_SIZE)
  1021. panic("TLB load handler fastpath space exceeded");
  1022. uasm_resolve_relocs(relocs, labels);
  1023. pr_debug("Wrote TLB load handler fastpath (%u instructions).\n",
  1024. (unsigned int)(p - handle_tlbl));
  1025. dump_handler(handle_tlbl, ARRAY_SIZE(handle_tlbl));
  1026. }
  1027. static void __cpuinit build_r4000_tlb_store_handler(void)
  1028. {
  1029. u32 *p = handle_tlbs;
  1030. struct uasm_label *l = labels;
  1031. struct uasm_reloc *r = relocs;
  1032. memset(handle_tlbs, 0, sizeof(handle_tlbs));
  1033. memset(labels, 0, sizeof(labels));
  1034. memset(relocs, 0, sizeof(relocs));
  1035. build_r4000_tlbchange_handler_head(&p, &l, &r, K0, K1);
  1036. build_pte_writable(&p, &l, &r, K0, K1, label_nopage_tlbs);
  1037. if (m4kc_tlbp_war())
  1038. build_tlb_probe_entry(&p);
  1039. build_make_write(&p, &r, K0, K1);
  1040. build_r4000_tlbchange_handler_tail(&p, &l, &r, K0, K1);
  1041. uasm_l_nopage_tlbs(&l, p);
  1042. uasm_i_j(&p, (unsigned long)tlb_do_page_fault_1 & 0x0fffffff);
  1043. uasm_i_nop(&p);
  1044. if ((p - handle_tlbs) > FASTPATH_SIZE)
  1045. panic("TLB store handler fastpath space exceeded");
  1046. uasm_resolve_relocs(relocs, labels);
  1047. pr_debug("Wrote TLB store handler fastpath (%u instructions).\n",
  1048. (unsigned int)(p - handle_tlbs));
  1049. dump_handler(handle_tlbs, ARRAY_SIZE(handle_tlbs));
  1050. }
  1051. static void __cpuinit build_r4000_tlb_modify_handler(void)
  1052. {
  1053. u32 *p = handle_tlbm;
  1054. struct uasm_label *l = labels;
  1055. struct uasm_reloc *r = relocs;
  1056. memset(handle_tlbm, 0, sizeof(handle_tlbm));
  1057. memset(labels, 0, sizeof(labels));
  1058. memset(relocs, 0, sizeof(relocs));
  1059. build_r4000_tlbchange_handler_head(&p, &l, &r, K0, K1);
  1060. build_pte_modifiable(&p, &l, &r, K0, K1, label_nopage_tlbm);
  1061. if (m4kc_tlbp_war())
  1062. build_tlb_probe_entry(&p);
  1063. /* Present and writable bits set, set accessed and dirty bits. */
  1064. build_make_write(&p, &r, K0, K1);
  1065. build_r4000_tlbchange_handler_tail(&p, &l, &r, K0, K1);
  1066. uasm_l_nopage_tlbm(&l, p);
  1067. uasm_i_j(&p, (unsigned long)tlb_do_page_fault_1 & 0x0fffffff);
  1068. uasm_i_nop(&p);
  1069. if ((p - handle_tlbm) > FASTPATH_SIZE)
  1070. panic("TLB modify handler fastpath space exceeded");
  1071. uasm_resolve_relocs(relocs, labels);
  1072. pr_debug("Wrote TLB modify handler fastpath (%u instructions).\n",
  1073. (unsigned int)(p - handle_tlbm));
  1074. dump_handler(handle_tlbm, ARRAY_SIZE(handle_tlbm));
  1075. }
  1076. void __cpuinit build_tlb_refill_handler(void)
  1077. {
  1078. /*
  1079. * The refill handler is generated per-CPU, multi-node systems
  1080. * may have local storage for it. The other handlers are only
  1081. * needed once.
  1082. */
  1083. static int run_once = 0;
  1084. switch (current_cpu_type()) {
  1085. case CPU_R2000:
  1086. case CPU_R3000:
  1087. case CPU_R3000A:
  1088. case CPU_R3081E:
  1089. case CPU_TX3912:
  1090. case CPU_TX3922:
  1091. case CPU_TX3927:
  1092. build_r3000_tlb_refill_handler();
  1093. if (!run_once) {
  1094. build_r3000_tlb_load_handler();
  1095. build_r3000_tlb_store_handler();
  1096. build_r3000_tlb_modify_handler();
  1097. run_once++;
  1098. }
  1099. break;
  1100. case CPU_R6000:
  1101. case CPU_R6000A:
  1102. panic("No R6000 TLB refill handler yet");
  1103. break;
  1104. case CPU_R8000:
  1105. panic("No R8000 TLB refill handler yet");
  1106. break;
  1107. default:
  1108. build_r4000_tlb_refill_handler();
  1109. if (!run_once) {
  1110. build_r4000_tlb_load_handler();
  1111. build_r4000_tlb_store_handler();
  1112. build_r4000_tlb_modify_handler();
  1113. run_once++;
  1114. }
  1115. }
  1116. }
  1117. void __cpuinit flush_tlb_handlers(void)
  1118. {
  1119. local_flush_icache_range((unsigned long)handle_tlbl,
  1120. (unsigned long)handle_tlbl + sizeof(handle_tlbl));
  1121. local_flush_icache_range((unsigned long)handle_tlbs,
  1122. (unsigned long)handle_tlbs + sizeof(handle_tlbs));
  1123. local_flush_icache_range((unsigned long)handle_tlbm,
  1124. (unsigned long)handle_tlbm + sizeof(handle_tlbm));
  1125. }