tlbex.c 33 KB

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