tlbex.c 33 KB

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