tlbex.c 47 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898
  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 by 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/bugs.h>
  26. #include <asm/mmu_context.h>
  27. #include <asm/inst.h>
  28. #include <asm/elf.h>
  29. #include <asm/war.h>
  30. static inline int r45k_bvahwbug(void)
  31. {
  32. /* XXX: We should probe for the presence of this bug, but we don't. */
  33. return 0;
  34. }
  35. static inline int r4k_250MHZhwbug(void)
  36. {
  37. /* XXX: We should probe for the presence of this bug, but we don't. */
  38. return 0;
  39. }
  40. static inline int __maybe_unused bcm1250_m3_war(void)
  41. {
  42. return BCM1250_M3_WAR;
  43. }
  44. static inline int __maybe_unused r10000_llsc_war(void)
  45. {
  46. return R10000_LLSC_WAR;
  47. }
  48. /*
  49. * Found by experiment: At least some revisions of the 4kc throw under
  50. * some circumstances a machine check exception, triggered by invalid
  51. * values in the index register. Delaying the tlbp instruction until
  52. * after the next branch, plus adding an additional nop in front of
  53. * tlbwi/tlbwr avoids the invalid index register values. Nobody knows
  54. * why; it's not an issue caused by the core RTL.
  55. *
  56. */
  57. static int __init m4kc_tlbp_war(void)
  58. {
  59. return (current_cpu_data.processor_id & 0xffff00) ==
  60. (PRID_COMP_MIPS | PRID_IMP_4KC);
  61. }
  62. /*
  63. * A little micro-assembler, intended for TLB refill handler
  64. * synthesizing. It is intentionally kept simple, does only support
  65. * a subset of instructions, and does not try to hide pipeline effects
  66. * like branch delay slots.
  67. */
  68. enum fields
  69. {
  70. RS = 0x001,
  71. RT = 0x002,
  72. RD = 0x004,
  73. RE = 0x008,
  74. SIMM = 0x010,
  75. UIMM = 0x020,
  76. BIMM = 0x040,
  77. JIMM = 0x080,
  78. FUNC = 0x100,
  79. SET = 0x200
  80. };
  81. #define OP_MASK 0x3f
  82. #define OP_SH 26
  83. #define RS_MASK 0x1f
  84. #define RS_SH 21
  85. #define RT_MASK 0x1f
  86. #define RT_SH 16
  87. #define RD_MASK 0x1f
  88. #define RD_SH 11
  89. #define RE_MASK 0x1f
  90. #define RE_SH 6
  91. #define IMM_MASK 0xffff
  92. #define IMM_SH 0
  93. #define JIMM_MASK 0x3ffffff
  94. #define JIMM_SH 0
  95. #define FUNC_MASK 0x3f
  96. #define FUNC_SH 0
  97. #define SET_MASK 0x7
  98. #define SET_SH 0
  99. enum opcode {
  100. insn_invalid,
  101. insn_addu, insn_addiu, insn_and, insn_andi, insn_beq,
  102. insn_beql, insn_bgez, insn_bgezl, insn_bltz, insn_bltzl,
  103. insn_bne, insn_daddu, insn_daddiu, insn_dmfc0, insn_dmtc0,
  104. insn_dsll, insn_dsll32, insn_dsra, insn_dsrl, insn_dsrl32,
  105. insn_dsubu, insn_eret, insn_j, insn_jal, insn_jr, insn_ld,
  106. insn_ll, insn_lld, insn_lui, insn_lw, insn_mfc0, insn_mtc0,
  107. insn_ori, insn_rfe, insn_sc, insn_scd, insn_sd, insn_sll,
  108. insn_sra, insn_srl, insn_subu, insn_sw, insn_tlbp, insn_tlbwi,
  109. insn_tlbwr, insn_xor, insn_xori
  110. };
  111. struct insn {
  112. enum opcode opcode;
  113. u32 match;
  114. enum fields fields;
  115. };
  116. /* This macro sets the non-variable bits of an instruction. */
  117. #define M(a, b, c, d, e, f) \
  118. ((a) << OP_SH \
  119. | (b) << RS_SH \
  120. | (c) << RT_SH \
  121. | (d) << RD_SH \
  122. | (e) << RE_SH \
  123. | (f) << FUNC_SH)
  124. static struct insn insn_table[] __initdata = {
  125. { insn_addiu, M(addiu_op, 0, 0, 0, 0, 0), RS | RT | SIMM },
  126. { insn_addu, M(spec_op, 0, 0, 0, 0, addu_op), RS | RT | RD },
  127. { insn_and, M(spec_op, 0, 0, 0, 0, and_op), RS | RT | RD },
  128. { insn_andi, M(andi_op, 0, 0, 0, 0, 0), RS | RT | UIMM },
  129. { insn_beq, M(beq_op, 0, 0, 0, 0, 0), RS | RT | BIMM },
  130. { insn_beql, M(beql_op, 0, 0, 0, 0, 0), RS | RT | BIMM },
  131. { insn_bgez, M(bcond_op, 0, bgez_op, 0, 0, 0), RS | BIMM },
  132. { insn_bgezl, M(bcond_op, 0, bgezl_op, 0, 0, 0), RS | BIMM },
  133. { insn_bltz, M(bcond_op, 0, bltz_op, 0, 0, 0), RS | BIMM },
  134. { insn_bltzl, M(bcond_op, 0, bltzl_op, 0, 0, 0), RS | BIMM },
  135. { insn_bne, M(bne_op, 0, 0, 0, 0, 0), RS | RT | BIMM },
  136. { insn_daddiu, M(daddiu_op, 0, 0, 0, 0, 0), RS | RT | SIMM },
  137. { insn_daddu, M(spec_op, 0, 0, 0, 0, daddu_op), RS | RT | RD },
  138. { insn_dmfc0, M(cop0_op, dmfc_op, 0, 0, 0, 0), RT | RD | SET},
  139. { insn_dmtc0, M(cop0_op, dmtc_op, 0, 0, 0, 0), RT | RD | SET},
  140. { insn_dsll, M(spec_op, 0, 0, 0, 0, dsll_op), RT | RD | RE },
  141. { insn_dsll32, M(spec_op, 0, 0, 0, 0, dsll32_op), RT | RD | RE },
  142. { insn_dsra, M(spec_op, 0, 0, 0, 0, dsra_op), RT | RD | RE },
  143. { insn_dsrl, M(spec_op, 0, 0, 0, 0, dsrl_op), RT | RD | RE },
  144. { insn_dsrl32, M(spec_op, 0, 0, 0, 0, dsrl32_op), RT | RD | RE },
  145. { insn_dsubu, M(spec_op, 0, 0, 0, 0, dsubu_op), RS | RT | RD },
  146. { insn_eret, M(cop0_op, cop_op, 0, 0, 0, eret_op), 0 },
  147. { insn_j, M(j_op, 0, 0, 0, 0, 0), JIMM },
  148. { insn_jal, M(jal_op, 0, 0, 0, 0, 0), JIMM },
  149. { insn_jr, M(spec_op, 0, 0, 0, 0, jr_op), RS },
  150. { insn_ld, M(ld_op, 0, 0, 0, 0, 0), RS | RT | SIMM },
  151. { insn_ll, M(ll_op, 0, 0, 0, 0, 0), RS | RT | SIMM },
  152. { insn_lld, M(lld_op, 0, 0, 0, 0, 0), RS | RT | SIMM },
  153. { insn_lui, M(lui_op, 0, 0, 0, 0, 0), RT | SIMM },
  154. { insn_lw, M(lw_op, 0, 0, 0, 0, 0), RS | RT | SIMM },
  155. { insn_mfc0, M(cop0_op, mfc_op, 0, 0, 0, 0), RT | RD | SET},
  156. { insn_mtc0, M(cop0_op, mtc_op, 0, 0, 0, 0), RT | RD | SET},
  157. { insn_ori, M(ori_op, 0, 0, 0, 0, 0), RS | RT | UIMM },
  158. { insn_rfe, M(cop0_op, cop_op, 0, 0, 0, rfe_op), 0 },
  159. { insn_sc, M(sc_op, 0, 0, 0, 0, 0), RS | RT | SIMM },
  160. { insn_scd, M(scd_op, 0, 0, 0, 0, 0), RS | RT | SIMM },
  161. { insn_sd, M(sd_op, 0, 0, 0, 0, 0), RS | RT | SIMM },
  162. { insn_sll, M(spec_op, 0, 0, 0, 0, sll_op), RT | RD | RE },
  163. { insn_sra, M(spec_op, 0, 0, 0, 0, sra_op), RT | RD | RE },
  164. { insn_srl, M(spec_op, 0, 0, 0, 0, srl_op), RT | RD | RE },
  165. { insn_subu, M(spec_op, 0, 0, 0, 0, subu_op), RS | RT | RD },
  166. { insn_sw, M(sw_op, 0, 0, 0, 0, 0), RS | RT | SIMM },
  167. { insn_tlbp, M(cop0_op, cop_op, 0, 0, 0, tlbp_op), 0 },
  168. { insn_tlbwi, M(cop0_op, cop_op, 0, 0, 0, tlbwi_op), 0 },
  169. { insn_tlbwr, M(cop0_op, cop_op, 0, 0, 0, tlbwr_op), 0 },
  170. { insn_xor, M(spec_op, 0, 0, 0, 0, xor_op), RS | RT | RD },
  171. { insn_xori, M(xori_op, 0, 0, 0, 0, 0), RS | RT | UIMM },
  172. { insn_invalid, 0, 0 }
  173. };
  174. #undef M
  175. static u32 __init build_rs(u32 arg)
  176. {
  177. if (arg & ~RS_MASK)
  178. printk(KERN_WARNING "TLB synthesizer field overflow\n");
  179. return (arg & RS_MASK) << RS_SH;
  180. }
  181. static u32 __init build_rt(u32 arg)
  182. {
  183. if (arg & ~RT_MASK)
  184. printk(KERN_WARNING "TLB synthesizer field overflow\n");
  185. return (arg & RT_MASK) << RT_SH;
  186. }
  187. static u32 __init build_rd(u32 arg)
  188. {
  189. if (arg & ~RD_MASK)
  190. printk(KERN_WARNING "TLB synthesizer field overflow\n");
  191. return (arg & RD_MASK) << RD_SH;
  192. }
  193. static u32 __init build_re(u32 arg)
  194. {
  195. if (arg & ~RE_MASK)
  196. printk(KERN_WARNING "TLB synthesizer field overflow\n");
  197. return (arg & RE_MASK) << RE_SH;
  198. }
  199. static u32 __init build_simm(s32 arg)
  200. {
  201. if (arg > 0x7fff || arg < -0x8000)
  202. printk(KERN_WARNING "TLB synthesizer field overflow\n");
  203. return arg & 0xffff;
  204. }
  205. static u32 __init build_uimm(u32 arg)
  206. {
  207. if (arg & ~IMM_MASK)
  208. printk(KERN_WARNING "TLB synthesizer field overflow\n");
  209. return arg & IMM_MASK;
  210. }
  211. static u32 __init build_bimm(s32 arg)
  212. {
  213. if (arg > 0x1ffff || arg < -0x20000)
  214. printk(KERN_WARNING "TLB synthesizer field overflow\n");
  215. if (arg & 0x3)
  216. printk(KERN_WARNING "Invalid TLB synthesizer branch target\n");
  217. return ((arg < 0) ? (1 << 15) : 0) | ((arg >> 2) & 0x7fff);
  218. }
  219. static u32 __init build_jimm(u32 arg)
  220. {
  221. if (arg & ~((JIMM_MASK) << 2))
  222. printk(KERN_WARNING "TLB synthesizer field overflow\n");
  223. return (arg >> 2) & JIMM_MASK;
  224. }
  225. static u32 __init build_func(u32 arg)
  226. {
  227. if (arg & ~FUNC_MASK)
  228. printk(KERN_WARNING "TLB synthesizer field overflow\n");
  229. return arg & FUNC_MASK;
  230. }
  231. static u32 __init build_set(u32 arg)
  232. {
  233. if (arg & ~SET_MASK)
  234. printk(KERN_WARNING "TLB synthesizer field overflow\n");
  235. return arg & SET_MASK;
  236. }
  237. /*
  238. * The order of opcode arguments is implicitly left to right,
  239. * starting with RS and ending with FUNC or IMM.
  240. */
  241. static void __init build_insn(u32 **buf, enum opcode opc, ...)
  242. {
  243. struct insn *ip = NULL;
  244. unsigned int i;
  245. va_list ap;
  246. u32 op;
  247. for (i = 0; insn_table[i].opcode != insn_invalid; i++)
  248. if (insn_table[i].opcode == opc) {
  249. ip = &insn_table[i];
  250. break;
  251. }
  252. if (!ip || (opc == insn_daddiu && r4k_daddiu_bug()))
  253. panic("Unsupported TLB synthesizer instruction %d", opc);
  254. op = ip->match;
  255. va_start(ap, opc);
  256. if (ip->fields & RS) op |= build_rs(va_arg(ap, u32));
  257. if (ip->fields & RT) op |= build_rt(va_arg(ap, u32));
  258. if (ip->fields & RD) op |= build_rd(va_arg(ap, u32));
  259. if (ip->fields & RE) op |= build_re(va_arg(ap, u32));
  260. if (ip->fields & SIMM) op |= build_simm(va_arg(ap, s32));
  261. if (ip->fields & UIMM) op |= build_uimm(va_arg(ap, u32));
  262. if (ip->fields & BIMM) op |= build_bimm(va_arg(ap, s32));
  263. if (ip->fields & JIMM) op |= build_jimm(va_arg(ap, u32));
  264. if (ip->fields & FUNC) op |= build_func(va_arg(ap, u32));
  265. if (ip->fields & SET) op |= build_set(va_arg(ap, u32));
  266. va_end(ap);
  267. **buf = op;
  268. (*buf)++;
  269. }
  270. #define I_u1u2u3(op) \
  271. static void __init __maybe_unused i##op(u32 **buf, unsigned int a, \
  272. unsigned int b, unsigned int c) \
  273. { \
  274. build_insn(buf, insn##op, a, b, c); \
  275. }
  276. #define I_u2u1u3(op) \
  277. static void __init __maybe_unused i##op(u32 **buf, unsigned int a, \
  278. unsigned int b, unsigned int c) \
  279. { \
  280. build_insn(buf, insn##op, b, a, c); \
  281. }
  282. #define I_u3u1u2(op) \
  283. static void __init __maybe_unused i##op(u32 **buf, unsigned int a, \
  284. unsigned int b, unsigned int c) \
  285. { \
  286. build_insn(buf, insn##op, b, c, a); \
  287. }
  288. #define I_u1u2s3(op) \
  289. static void __init __maybe_unused i##op(u32 **buf, unsigned int a, \
  290. unsigned int b, signed int c) \
  291. { \
  292. build_insn(buf, insn##op, a, b, c); \
  293. }
  294. #define I_u2s3u1(op) \
  295. static void __init __maybe_unused i##op(u32 **buf, unsigned int a, \
  296. signed int b, unsigned int c) \
  297. { \
  298. build_insn(buf, insn##op, c, a, b); \
  299. }
  300. #define I_u2u1s3(op) \
  301. static void __init __maybe_unused i##op(u32 **buf, unsigned int a, \
  302. unsigned int b, signed int c) \
  303. { \
  304. build_insn(buf, insn##op, b, a, c); \
  305. }
  306. #define I_u1u2(op) \
  307. static void __init __maybe_unused i##op(u32 **buf, unsigned int a, \
  308. unsigned int b) \
  309. { \
  310. build_insn(buf, insn##op, a, b); \
  311. }
  312. #define I_u1s2(op) \
  313. static void __init __maybe_unused i##op(u32 **buf, unsigned int a, \
  314. signed int b) \
  315. { \
  316. build_insn(buf, insn##op, a, b); \
  317. }
  318. #define I_u1(op) \
  319. static void __init __maybe_unused i##op(u32 **buf, unsigned int a) \
  320. { \
  321. build_insn(buf, insn##op, a); \
  322. }
  323. #define I_0(op) \
  324. static void __init __maybe_unused i##op(u32 **buf) \
  325. { \
  326. build_insn(buf, insn##op); \
  327. }
  328. I_u2u1s3(_addiu);
  329. I_u3u1u2(_addu);
  330. I_u2u1u3(_andi);
  331. I_u3u1u2(_and);
  332. I_u1u2s3(_beq);
  333. I_u1u2s3(_beql);
  334. I_u1s2(_bgez);
  335. I_u1s2(_bgezl);
  336. I_u1s2(_bltz);
  337. I_u1s2(_bltzl);
  338. I_u1u2s3(_bne);
  339. I_u1u2u3(_dmfc0);
  340. I_u1u2u3(_dmtc0);
  341. I_u2u1s3(_daddiu);
  342. I_u3u1u2(_daddu);
  343. I_u2u1u3(_dsll);
  344. I_u2u1u3(_dsll32);
  345. I_u2u1u3(_dsra);
  346. I_u2u1u3(_dsrl);
  347. I_u2u1u3(_dsrl32);
  348. I_u3u1u2(_dsubu);
  349. I_0(_eret);
  350. I_u1(_j);
  351. I_u1(_jal);
  352. I_u1(_jr);
  353. I_u2s3u1(_ld);
  354. I_u2s3u1(_ll);
  355. I_u2s3u1(_lld);
  356. I_u1s2(_lui);
  357. I_u2s3u1(_lw);
  358. I_u1u2u3(_mfc0);
  359. I_u1u2u3(_mtc0);
  360. I_u2u1u3(_ori);
  361. I_0(_rfe);
  362. I_u2s3u1(_sc);
  363. I_u2s3u1(_scd);
  364. I_u2s3u1(_sd);
  365. I_u2u1u3(_sll);
  366. I_u2u1u3(_sra);
  367. I_u2u1u3(_srl);
  368. I_u3u1u2(_subu);
  369. I_u2s3u1(_sw);
  370. I_0(_tlbp);
  371. I_0(_tlbwi);
  372. I_0(_tlbwr);
  373. I_u3u1u2(_xor)
  374. I_u2u1u3(_xori);
  375. /*
  376. * handling labels
  377. */
  378. enum label_id {
  379. label_invalid,
  380. label_second_part,
  381. label_leave,
  382. #ifdef MODULE_START
  383. label_module_alloc,
  384. #endif
  385. label_vmalloc,
  386. label_vmalloc_done,
  387. label_tlbw_hazard,
  388. label_split,
  389. label_nopage_tlbl,
  390. label_nopage_tlbs,
  391. label_nopage_tlbm,
  392. label_smp_pgtable_change,
  393. label_r3000_write_probe_fail,
  394. };
  395. struct label {
  396. u32 *addr;
  397. enum label_id lab;
  398. };
  399. static void __init build_label(struct label **lab, u32 *addr,
  400. enum label_id l)
  401. {
  402. (*lab)->addr = addr;
  403. (*lab)->lab = l;
  404. (*lab)++;
  405. }
  406. #define L_LA(lb) \
  407. static inline void __init l##lb(struct label **lab, u32 *addr) \
  408. { \
  409. build_label(lab, addr, label##lb); \
  410. }
  411. L_LA(_second_part)
  412. L_LA(_leave)
  413. #ifdef MODULE_START
  414. L_LA(_module_alloc)
  415. #endif
  416. L_LA(_vmalloc)
  417. L_LA(_vmalloc_done)
  418. L_LA(_tlbw_hazard)
  419. L_LA(_split)
  420. L_LA(_nopage_tlbl)
  421. L_LA(_nopage_tlbs)
  422. L_LA(_nopage_tlbm)
  423. L_LA(_smp_pgtable_change)
  424. L_LA(_r3000_write_probe_fail)
  425. /* convenience macros for instructions */
  426. #ifdef CONFIG_64BIT
  427. # define i_LW(buf, rs, rt, off) i_ld(buf, rs, rt, off)
  428. # define i_SW(buf, rs, rt, off) i_sd(buf, rs, rt, off)
  429. # define i_SLL(buf, rs, rt, sh) i_dsll(buf, rs, rt, sh)
  430. # define i_SRA(buf, rs, rt, sh) i_dsra(buf, rs, rt, sh)
  431. # define i_SRL(buf, rs, rt, sh) i_dsrl(buf, rs, rt, sh)
  432. # define i_MFC0(buf, rt, rd...) i_dmfc0(buf, rt, rd)
  433. # define i_MTC0(buf, rt, rd...) i_dmtc0(buf, rt, rd)
  434. # define i_ADDIU(buf, rs, rt, val) i_daddiu(buf, rs, rt, val)
  435. # define i_ADDU(buf, rs, rt, rd) i_daddu(buf, rs, rt, rd)
  436. # define i_SUBU(buf, rs, rt, rd) i_dsubu(buf, rs, rt, rd)
  437. # define i_LL(buf, rs, rt, off) i_lld(buf, rs, rt, off)
  438. # define i_SC(buf, rs, rt, off) i_scd(buf, rs, rt, off)
  439. #else
  440. # define i_LW(buf, rs, rt, off) i_lw(buf, rs, rt, off)
  441. # define i_SW(buf, rs, rt, off) i_sw(buf, rs, rt, off)
  442. # define i_SLL(buf, rs, rt, sh) i_sll(buf, rs, rt, sh)
  443. # define i_SRA(buf, rs, rt, sh) i_sra(buf, rs, rt, sh)
  444. # define i_SRL(buf, rs, rt, sh) i_srl(buf, rs, rt, sh)
  445. # define i_MFC0(buf, rt, rd...) i_mfc0(buf, rt, rd)
  446. # define i_MTC0(buf, rt, rd...) i_mtc0(buf, rt, rd)
  447. # define i_ADDIU(buf, rs, rt, val) i_addiu(buf, rs, rt, val)
  448. # define i_ADDU(buf, rs, rt, rd) i_addu(buf, rs, rt, rd)
  449. # define i_SUBU(buf, rs, rt, rd) i_subu(buf, rs, rt, rd)
  450. # define i_LL(buf, rs, rt, off) i_ll(buf, rs, rt, off)
  451. # define i_SC(buf, rs, rt, off) i_sc(buf, rs, rt, off)
  452. #endif
  453. #define i_b(buf, off) i_beq(buf, 0, 0, off)
  454. #define i_beqz(buf, rs, off) i_beq(buf, rs, 0, off)
  455. #define i_beqzl(buf, rs, off) i_beql(buf, rs, 0, off)
  456. #define i_bnez(buf, rs, off) i_bne(buf, rs, 0, off)
  457. #define i_bnezl(buf, rs, off) i_bnel(buf, rs, 0, off)
  458. #define i_move(buf, a, b) i_ADDU(buf, a, 0, b)
  459. #define i_nop(buf) i_sll(buf, 0, 0, 0)
  460. #define i_ssnop(buf) i_sll(buf, 0, 0, 1)
  461. #define i_ehb(buf) i_sll(buf, 0, 0, 3)
  462. static int __init __maybe_unused in_compat_space_p(long addr)
  463. {
  464. /* Is this address in 32bit compat space? */
  465. #ifdef CONFIG_64BIT
  466. return (((addr) & 0xffffffff00000000L) == 0xffffffff00000000L);
  467. #else
  468. return 1;
  469. #endif
  470. }
  471. static int __init __maybe_unused rel_highest(long val)
  472. {
  473. #ifdef CONFIG_64BIT
  474. return ((((val + 0x800080008000L) >> 48) & 0xffff) ^ 0x8000) - 0x8000;
  475. #else
  476. return 0;
  477. #endif
  478. }
  479. static int __init __maybe_unused rel_higher(long val)
  480. {
  481. #ifdef CONFIG_64BIT
  482. return ((((val + 0x80008000L) >> 32) & 0xffff) ^ 0x8000) - 0x8000;
  483. #else
  484. return 0;
  485. #endif
  486. }
  487. static int __init rel_hi(long val)
  488. {
  489. return ((((val + 0x8000L) >> 16) & 0xffff) ^ 0x8000) - 0x8000;
  490. }
  491. static int __init rel_lo(long val)
  492. {
  493. return ((val & 0xffff) ^ 0x8000) - 0x8000;
  494. }
  495. static void __init i_LA_mostly(u32 **buf, unsigned int rs, long addr)
  496. {
  497. if (!in_compat_space_p(addr)) {
  498. i_lui(buf, rs, rel_highest(addr));
  499. if (rel_higher(addr))
  500. i_daddiu(buf, rs, rs, rel_higher(addr));
  501. if (rel_hi(addr)) {
  502. i_dsll(buf, rs, rs, 16);
  503. i_daddiu(buf, rs, rs, rel_hi(addr));
  504. i_dsll(buf, rs, rs, 16);
  505. } else
  506. i_dsll32(buf, rs, rs, 0);
  507. } else
  508. i_lui(buf, rs, rel_hi(addr));
  509. }
  510. static void __init __maybe_unused i_LA(u32 **buf, unsigned int rs, long addr)
  511. {
  512. i_LA_mostly(buf, rs, addr);
  513. if (rel_lo(addr)) {
  514. if (!in_compat_space_p(addr))
  515. i_daddiu(buf, rs, rs, rel_lo(addr));
  516. else
  517. i_addiu(buf, rs, rs, rel_lo(addr));
  518. }
  519. }
  520. /*
  521. * handle relocations
  522. */
  523. struct reloc {
  524. u32 *addr;
  525. unsigned int type;
  526. enum label_id lab;
  527. };
  528. static void __init r_mips_pc16(struct reloc **rel, u32 *addr,
  529. enum label_id l)
  530. {
  531. (*rel)->addr = addr;
  532. (*rel)->type = R_MIPS_PC16;
  533. (*rel)->lab = l;
  534. (*rel)++;
  535. }
  536. static inline void __resolve_relocs(struct reloc *rel, struct label *lab)
  537. {
  538. long laddr = (long)lab->addr;
  539. long raddr = (long)rel->addr;
  540. switch (rel->type) {
  541. case R_MIPS_PC16:
  542. *rel->addr |= build_bimm(laddr - (raddr + 4));
  543. break;
  544. default:
  545. panic("Unsupported TLB synthesizer relocation %d",
  546. rel->type);
  547. }
  548. }
  549. static void __init resolve_relocs(struct reloc *rel, struct label *lab)
  550. {
  551. struct label *l;
  552. for (; rel->lab != label_invalid; rel++)
  553. for (l = lab; l->lab != label_invalid; l++)
  554. if (rel->lab == l->lab)
  555. __resolve_relocs(rel, l);
  556. }
  557. static void __init move_relocs(struct reloc *rel, u32 *first, u32 *end,
  558. long off)
  559. {
  560. for (; rel->lab != label_invalid; rel++)
  561. if (rel->addr >= first && rel->addr < end)
  562. rel->addr += off;
  563. }
  564. static void __init move_labels(struct label *lab, u32 *first, u32 *end,
  565. long off)
  566. {
  567. for (; lab->lab != label_invalid; lab++)
  568. if (lab->addr >= first && lab->addr < end)
  569. lab->addr += off;
  570. }
  571. static void __init copy_handler(struct reloc *rel, struct label *lab,
  572. u32 *first, u32 *end, u32 *target)
  573. {
  574. long off = (long)(target - first);
  575. memcpy(target, first, (end - first) * sizeof(u32));
  576. move_relocs(rel, first, end, off);
  577. move_labels(lab, first, end, off);
  578. }
  579. static int __init __maybe_unused insn_has_bdelay(struct reloc *rel,
  580. u32 *addr)
  581. {
  582. for (; rel->lab != label_invalid; rel++) {
  583. if (rel->addr == addr
  584. && (rel->type == R_MIPS_PC16
  585. || rel->type == R_MIPS_26))
  586. return 1;
  587. }
  588. return 0;
  589. }
  590. /* convenience functions for labeled branches */
  591. static void __init __maybe_unused
  592. il_bltz(u32 **p, struct reloc **r, unsigned int reg, enum label_id l)
  593. {
  594. r_mips_pc16(r, *p, l);
  595. i_bltz(p, reg, 0);
  596. }
  597. static void __init __maybe_unused il_b(u32 **p, struct reloc **r,
  598. enum label_id l)
  599. {
  600. r_mips_pc16(r, *p, l);
  601. i_b(p, 0);
  602. }
  603. static void __init il_beqz(u32 **p, struct reloc **r, unsigned int reg,
  604. enum label_id l)
  605. {
  606. r_mips_pc16(r, *p, l);
  607. i_beqz(p, reg, 0);
  608. }
  609. static void __init __maybe_unused
  610. il_beqzl(u32 **p, struct reloc **r, unsigned int reg, enum label_id l)
  611. {
  612. r_mips_pc16(r, *p, l);
  613. i_beqzl(p, reg, 0);
  614. }
  615. static void __init il_bnez(u32 **p, struct reloc **r, unsigned int reg,
  616. enum label_id l)
  617. {
  618. r_mips_pc16(r, *p, l);
  619. i_bnez(p, reg, 0);
  620. }
  621. static void __init il_bgezl(u32 **p, struct reloc **r, unsigned int reg,
  622. enum label_id l)
  623. {
  624. r_mips_pc16(r, *p, l);
  625. i_bgezl(p, reg, 0);
  626. }
  627. static void __init __maybe_unused
  628. il_bgez(u32 **p, struct reloc **r, unsigned int reg, enum label_id l)
  629. {
  630. r_mips_pc16(r, *p, l);
  631. i_bgez(p, reg, 0);
  632. }
  633. /*
  634. * For debug purposes.
  635. */
  636. static inline void dump_handler(const u32 *handler, int count)
  637. {
  638. int i;
  639. pr_debug("\t.set push\n");
  640. pr_debug("\t.set noreorder\n");
  641. for (i = 0; i < count; i++)
  642. pr_debug("\t%p\t.word 0x%08x\n", &handler[i], handler[i]);
  643. pr_debug("\t.set pop\n");
  644. }
  645. /* The only general purpose registers allowed in TLB handlers. */
  646. #define K0 26
  647. #define K1 27
  648. /* Some CP0 registers */
  649. #define C0_INDEX 0, 0
  650. #define C0_ENTRYLO0 2, 0
  651. #define C0_TCBIND 2, 2
  652. #define C0_ENTRYLO1 3, 0
  653. #define C0_CONTEXT 4, 0
  654. #define C0_BADVADDR 8, 0
  655. #define C0_ENTRYHI 10, 0
  656. #define C0_EPC 14, 0
  657. #define C0_XCONTEXT 20, 0
  658. #ifdef CONFIG_64BIT
  659. # define GET_CONTEXT(buf, reg) i_MFC0(buf, reg, C0_XCONTEXT)
  660. #else
  661. # define GET_CONTEXT(buf, reg) i_MFC0(buf, reg, C0_CONTEXT)
  662. #endif
  663. /* The worst case length of the handler is around 18 instructions for
  664. * R3000-style TLBs and up to 63 instructions for R4000-style TLBs.
  665. * Maximum space available is 32 instructions for R3000 and 64
  666. * instructions for R4000.
  667. *
  668. * We deliberately chose a buffer size of 128, so we won't scribble
  669. * over anything important on overflow before we panic.
  670. */
  671. static u32 tlb_handler[128] __initdata;
  672. /* simply assume worst case size for labels and relocs */
  673. static struct label labels[128] __initdata;
  674. static struct reloc relocs[128] __initdata;
  675. /*
  676. * The R3000 TLB handler is simple.
  677. */
  678. static void __init build_r3000_tlb_refill_handler(void)
  679. {
  680. long pgdc = (long)pgd_current;
  681. u32 *p;
  682. memset(tlb_handler, 0, sizeof(tlb_handler));
  683. p = tlb_handler;
  684. i_mfc0(&p, K0, C0_BADVADDR);
  685. i_lui(&p, K1, rel_hi(pgdc)); /* cp0 delay */
  686. i_lw(&p, K1, rel_lo(pgdc), K1);
  687. i_srl(&p, K0, K0, 22); /* load delay */
  688. i_sll(&p, K0, K0, 2);
  689. i_addu(&p, K1, K1, K0);
  690. i_mfc0(&p, K0, C0_CONTEXT);
  691. i_lw(&p, K1, 0, K1); /* cp0 delay */
  692. i_andi(&p, K0, K0, 0xffc); /* load delay */
  693. i_addu(&p, K1, K1, K0);
  694. i_lw(&p, K0, 0, K1);
  695. i_nop(&p); /* load delay */
  696. i_mtc0(&p, K0, C0_ENTRYLO0);
  697. i_mfc0(&p, K1, C0_EPC); /* cp0 delay */
  698. i_tlbwr(&p); /* cp0 delay */
  699. i_jr(&p, K1);
  700. i_rfe(&p); /* branch delay */
  701. if (p > tlb_handler + 32)
  702. panic("TLB refill handler space exceeded");
  703. pr_info("Synthesized TLB refill handler (%u instructions).\n",
  704. (unsigned int)(p - tlb_handler));
  705. memcpy((void *)ebase, tlb_handler, 0x80);
  706. dump_handler((u32 *)ebase, 32);
  707. }
  708. /*
  709. * The R4000 TLB handler is much more complicated. We have two
  710. * consecutive handler areas with 32 instructions space each.
  711. * Since they aren't used at the same time, we can overflow in the
  712. * other one.To keep things simple, we first assume linear space,
  713. * then we relocate it to the final handler layout as needed.
  714. */
  715. static u32 final_handler[64] __initdata;
  716. /*
  717. * Hazards
  718. *
  719. * From the IDT errata for the QED RM5230 (Nevada), processor revision 1.0:
  720. * 2. A timing hazard exists for the TLBP instruction.
  721. *
  722. * stalling_instruction
  723. * TLBP
  724. *
  725. * The JTLB is being read for the TLBP throughout the stall generated by the
  726. * previous instruction. This is not really correct as the stalling instruction
  727. * can modify the address used to access the JTLB. The failure symptom is that
  728. * the TLBP instruction will use an address created for the stalling instruction
  729. * and not the address held in C0_ENHI and thus report the wrong results.
  730. *
  731. * The software work-around is to not allow the instruction preceding the TLBP
  732. * to stall - make it an NOP or some other instruction guaranteed not to stall.
  733. *
  734. * Errata 2 will not be fixed. This errata is also on the R5000.
  735. *
  736. * As if we MIPS hackers wouldn't know how to nop pipelines happy ...
  737. */
  738. static void __init __maybe_unused build_tlb_probe_entry(u32 **p)
  739. {
  740. switch (current_cpu_type()) {
  741. /* Found by experiment: R4600 v2.0 needs this, too. */
  742. case CPU_R4600:
  743. case CPU_R5000:
  744. case CPU_R5000A:
  745. case CPU_NEVADA:
  746. i_nop(p);
  747. i_tlbp(p);
  748. break;
  749. default:
  750. i_tlbp(p);
  751. break;
  752. }
  753. }
  754. /*
  755. * Write random or indexed TLB entry, and care about the hazards from
  756. * the preceeding mtc0 and for the following eret.
  757. */
  758. enum tlb_write_entry { tlb_random, tlb_indexed };
  759. static void __init build_tlb_write_entry(u32 **p, struct label **l,
  760. struct reloc **r,
  761. enum tlb_write_entry wmode)
  762. {
  763. void(*tlbw)(u32 **) = NULL;
  764. switch (wmode) {
  765. case tlb_random: tlbw = i_tlbwr; break;
  766. case tlb_indexed: tlbw = i_tlbwi; break;
  767. }
  768. if (cpu_has_mips_r2) {
  769. i_ehb(p);
  770. tlbw(p);
  771. return;
  772. }
  773. switch (current_cpu_type()) {
  774. case CPU_R4000PC:
  775. case CPU_R4000SC:
  776. case CPU_R4000MC:
  777. case CPU_R4400PC:
  778. case CPU_R4400SC:
  779. case CPU_R4400MC:
  780. /*
  781. * This branch uses up a mtc0 hazard nop slot and saves
  782. * two nops after the tlbw instruction.
  783. */
  784. il_bgezl(p, r, 0, label_tlbw_hazard);
  785. tlbw(p);
  786. l_tlbw_hazard(l, *p);
  787. i_nop(p);
  788. break;
  789. case CPU_R4600:
  790. case CPU_R4700:
  791. case CPU_R5000:
  792. case CPU_R5000A:
  793. i_nop(p);
  794. tlbw(p);
  795. i_nop(p);
  796. break;
  797. case CPU_R4300:
  798. case CPU_5KC:
  799. case CPU_TX49XX:
  800. case CPU_AU1000:
  801. case CPU_AU1100:
  802. case CPU_AU1500:
  803. case CPU_AU1550:
  804. case CPU_AU1200:
  805. case CPU_AU1210:
  806. case CPU_AU1250:
  807. case CPU_PR4450:
  808. i_nop(p);
  809. tlbw(p);
  810. break;
  811. case CPU_R10000:
  812. case CPU_R12000:
  813. case CPU_R14000:
  814. case CPU_4KC:
  815. case CPU_SB1:
  816. case CPU_SB1A:
  817. case CPU_4KSC:
  818. case CPU_20KC:
  819. case CPU_25KF:
  820. case CPU_BCM3302:
  821. case CPU_BCM4710:
  822. case CPU_LOONGSON2:
  823. if (m4kc_tlbp_war())
  824. i_nop(p);
  825. tlbw(p);
  826. break;
  827. case CPU_NEVADA:
  828. i_nop(p); /* QED specifies 2 nops hazard */
  829. /*
  830. * This branch uses up a mtc0 hazard nop slot and saves
  831. * a nop after the tlbw instruction.
  832. */
  833. il_bgezl(p, r, 0, label_tlbw_hazard);
  834. tlbw(p);
  835. l_tlbw_hazard(l, *p);
  836. break;
  837. case CPU_RM7000:
  838. i_nop(p);
  839. i_nop(p);
  840. i_nop(p);
  841. i_nop(p);
  842. tlbw(p);
  843. break;
  844. case CPU_RM9000:
  845. /*
  846. * When the JTLB is updated by tlbwi or tlbwr, a subsequent
  847. * use of the JTLB for instructions should not occur for 4
  848. * cpu cycles and use for data translations should not occur
  849. * for 3 cpu cycles.
  850. */
  851. i_ssnop(p);
  852. i_ssnop(p);
  853. i_ssnop(p);
  854. i_ssnop(p);
  855. tlbw(p);
  856. i_ssnop(p);
  857. i_ssnop(p);
  858. i_ssnop(p);
  859. i_ssnop(p);
  860. break;
  861. case CPU_VR4111:
  862. case CPU_VR4121:
  863. case CPU_VR4122:
  864. case CPU_VR4181:
  865. case CPU_VR4181A:
  866. i_nop(p);
  867. i_nop(p);
  868. tlbw(p);
  869. i_nop(p);
  870. i_nop(p);
  871. break;
  872. case CPU_VR4131:
  873. case CPU_VR4133:
  874. case CPU_R5432:
  875. i_nop(p);
  876. i_nop(p);
  877. tlbw(p);
  878. break;
  879. default:
  880. panic("No TLB refill handler yet (CPU type: %d)",
  881. current_cpu_data.cputype);
  882. break;
  883. }
  884. }
  885. #ifdef CONFIG_64BIT
  886. /*
  887. * TMP and PTR are scratch.
  888. * TMP will be clobbered, PTR will hold the pmd entry.
  889. */
  890. static void __init
  891. build_get_pmde64(u32 **p, struct label **l, struct reloc **r,
  892. unsigned int tmp, unsigned int ptr)
  893. {
  894. long pgdc = (long)pgd_current;
  895. /*
  896. * The vmalloc handling is not in the hotpath.
  897. */
  898. i_dmfc0(p, tmp, C0_BADVADDR);
  899. #ifdef MODULE_START
  900. il_bltz(p, r, tmp, label_module_alloc);
  901. #else
  902. il_bltz(p, r, tmp, label_vmalloc);
  903. #endif
  904. /* No i_nop needed here, since the next insn doesn't touch TMP. */
  905. #ifdef CONFIG_SMP
  906. # ifdef CONFIG_MIPS_MT_SMTC
  907. /*
  908. * SMTC uses TCBind value as "CPU" index
  909. */
  910. i_mfc0(p, ptr, C0_TCBIND);
  911. i_dsrl(p, ptr, ptr, 19);
  912. # else
  913. /*
  914. * 64 bit SMP running in XKPHYS has smp_processor_id() << 3
  915. * stored in CONTEXT.
  916. */
  917. i_dmfc0(p, ptr, C0_CONTEXT);
  918. i_dsrl(p, ptr, ptr, 23);
  919. #endif
  920. i_LA_mostly(p, tmp, pgdc);
  921. i_daddu(p, ptr, ptr, tmp);
  922. i_dmfc0(p, tmp, C0_BADVADDR);
  923. i_ld(p, ptr, rel_lo(pgdc), ptr);
  924. #else
  925. i_LA_mostly(p, ptr, pgdc);
  926. i_ld(p, ptr, rel_lo(pgdc), ptr);
  927. #endif
  928. l_vmalloc_done(l, *p);
  929. if (PGDIR_SHIFT - 3 < 32) /* get pgd offset in bytes */
  930. i_dsrl(p, tmp, tmp, PGDIR_SHIFT-3);
  931. else
  932. i_dsrl32(p, tmp, tmp, PGDIR_SHIFT - 3 - 32);
  933. i_andi(p, tmp, tmp, (PTRS_PER_PGD - 1)<<3);
  934. i_daddu(p, ptr, ptr, tmp); /* add in pgd offset */
  935. i_dmfc0(p, tmp, C0_BADVADDR); /* get faulting address */
  936. i_ld(p, ptr, 0, ptr); /* get pmd pointer */
  937. i_dsrl(p, tmp, tmp, PMD_SHIFT-3); /* get pmd offset in bytes */
  938. i_andi(p, tmp, tmp, (PTRS_PER_PMD - 1)<<3);
  939. i_daddu(p, ptr, ptr, tmp); /* add in pmd offset */
  940. }
  941. /*
  942. * BVADDR is the faulting address, PTR is scratch.
  943. * PTR will hold the pgd for vmalloc.
  944. */
  945. static void __init
  946. build_get_pgd_vmalloc64(u32 **p, struct label **l, struct reloc **r,
  947. unsigned int bvaddr, unsigned int ptr)
  948. {
  949. long swpd = (long)swapper_pg_dir;
  950. #ifdef MODULE_START
  951. long modd = (long)module_pg_dir;
  952. l_module_alloc(l, *p);
  953. /*
  954. * Assumption:
  955. * VMALLOC_START >= 0xc000000000000000UL
  956. * MODULE_START >= 0xe000000000000000UL
  957. */
  958. i_SLL(p, ptr, bvaddr, 2);
  959. il_bgez(p, r, ptr, label_vmalloc);
  960. if (in_compat_space_p(MODULE_START) && !rel_lo(MODULE_START)) {
  961. i_lui(p, ptr, rel_hi(MODULE_START)); /* delay slot */
  962. } else {
  963. /* unlikely configuration */
  964. i_nop(p); /* delay slot */
  965. i_LA(p, ptr, MODULE_START);
  966. }
  967. i_dsubu(p, bvaddr, bvaddr, ptr);
  968. if (in_compat_space_p(modd) && !rel_lo(modd)) {
  969. il_b(p, r, label_vmalloc_done);
  970. i_lui(p, ptr, rel_hi(modd));
  971. } else {
  972. i_LA_mostly(p, ptr, modd);
  973. il_b(p, r, label_vmalloc_done);
  974. if (in_compat_space_p(modd))
  975. i_addiu(p, ptr, ptr, rel_lo(modd));
  976. else
  977. i_daddiu(p, ptr, ptr, rel_lo(modd));
  978. }
  979. l_vmalloc(l, *p);
  980. if (in_compat_space_p(MODULE_START) && !rel_lo(MODULE_START) &&
  981. MODULE_START << 32 == VMALLOC_START)
  982. i_dsll32(p, ptr, ptr, 0); /* typical case */
  983. else
  984. i_LA(p, ptr, VMALLOC_START);
  985. #else
  986. l_vmalloc(l, *p);
  987. i_LA(p, ptr, VMALLOC_START);
  988. #endif
  989. i_dsubu(p, bvaddr, bvaddr, ptr);
  990. if (in_compat_space_p(swpd) && !rel_lo(swpd)) {
  991. il_b(p, r, label_vmalloc_done);
  992. i_lui(p, ptr, rel_hi(swpd));
  993. } else {
  994. i_LA_mostly(p, ptr, swpd);
  995. il_b(p, r, label_vmalloc_done);
  996. if (in_compat_space_p(swpd))
  997. i_addiu(p, ptr, ptr, rel_lo(swpd));
  998. else
  999. i_daddiu(p, ptr, ptr, rel_lo(swpd));
  1000. }
  1001. }
  1002. #else /* !CONFIG_64BIT */
  1003. /*
  1004. * TMP and PTR are scratch.
  1005. * TMP will be clobbered, PTR will hold the pgd entry.
  1006. */
  1007. static void __init __maybe_unused
  1008. build_get_pgde32(u32 **p, unsigned int tmp, unsigned int ptr)
  1009. {
  1010. long pgdc = (long)pgd_current;
  1011. /* 32 bit SMP has smp_processor_id() stored in CONTEXT. */
  1012. #ifdef CONFIG_SMP
  1013. #ifdef CONFIG_MIPS_MT_SMTC
  1014. /*
  1015. * SMTC uses TCBind value as "CPU" index
  1016. */
  1017. i_mfc0(p, ptr, C0_TCBIND);
  1018. i_LA_mostly(p, tmp, pgdc);
  1019. i_srl(p, ptr, ptr, 19);
  1020. #else
  1021. /*
  1022. * smp_processor_id() << 3 is stored in CONTEXT.
  1023. */
  1024. i_mfc0(p, ptr, C0_CONTEXT);
  1025. i_LA_mostly(p, tmp, pgdc);
  1026. i_srl(p, ptr, ptr, 23);
  1027. #endif
  1028. i_addu(p, ptr, tmp, ptr);
  1029. #else
  1030. i_LA_mostly(p, ptr, pgdc);
  1031. #endif
  1032. i_mfc0(p, tmp, C0_BADVADDR); /* get faulting address */
  1033. i_lw(p, ptr, rel_lo(pgdc), ptr);
  1034. i_srl(p, tmp, tmp, PGDIR_SHIFT); /* get pgd only bits */
  1035. i_sll(p, tmp, tmp, PGD_T_LOG2);
  1036. i_addu(p, ptr, ptr, tmp); /* add in pgd offset */
  1037. }
  1038. #endif /* !CONFIG_64BIT */
  1039. static void __init build_adjust_context(u32 **p, unsigned int ctx)
  1040. {
  1041. unsigned int shift = 4 - (PTE_T_LOG2 + 1) + PAGE_SHIFT - 12;
  1042. unsigned int mask = (PTRS_PER_PTE / 2 - 1) << (PTE_T_LOG2 + 1);
  1043. switch (current_cpu_type()) {
  1044. case CPU_VR41XX:
  1045. case CPU_VR4111:
  1046. case CPU_VR4121:
  1047. case CPU_VR4122:
  1048. case CPU_VR4131:
  1049. case CPU_VR4181:
  1050. case CPU_VR4181A:
  1051. case CPU_VR4133:
  1052. shift += 2;
  1053. break;
  1054. default:
  1055. break;
  1056. }
  1057. if (shift)
  1058. i_SRL(p, ctx, ctx, shift);
  1059. i_andi(p, ctx, ctx, mask);
  1060. }
  1061. static void __init build_get_ptep(u32 **p, unsigned int tmp, unsigned int ptr)
  1062. {
  1063. /*
  1064. * Bug workaround for the Nevada. It seems as if under certain
  1065. * circumstances the move from cp0_context might produce a
  1066. * bogus result when the mfc0 instruction and its consumer are
  1067. * in a different cacheline or a load instruction, probably any
  1068. * memory reference, is between them.
  1069. */
  1070. switch (current_cpu_type()) {
  1071. case CPU_NEVADA:
  1072. i_LW(p, ptr, 0, ptr);
  1073. GET_CONTEXT(p, tmp); /* get context reg */
  1074. break;
  1075. default:
  1076. GET_CONTEXT(p, tmp); /* get context reg */
  1077. i_LW(p, ptr, 0, ptr);
  1078. break;
  1079. }
  1080. build_adjust_context(p, tmp);
  1081. i_ADDU(p, ptr, ptr, tmp); /* add in offset */
  1082. }
  1083. static void __init build_update_entries(u32 **p, unsigned int tmp,
  1084. unsigned int ptep)
  1085. {
  1086. /*
  1087. * 64bit address support (36bit on a 32bit CPU) in a 32bit
  1088. * Kernel is a special case. Only a few CPUs use it.
  1089. */
  1090. #ifdef CONFIG_64BIT_PHYS_ADDR
  1091. if (cpu_has_64bits) {
  1092. i_ld(p, tmp, 0, ptep); /* get even pte */
  1093. i_ld(p, ptep, sizeof(pte_t), ptep); /* get odd pte */
  1094. i_dsrl(p, tmp, tmp, 6); /* convert to entrylo0 */
  1095. i_mtc0(p, tmp, C0_ENTRYLO0); /* load it */
  1096. i_dsrl(p, ptep, ptep, 6); /* convert to entrylo1 */
  1097. i_mtc0(p, ptep, C0_ENTRYLO1); /* load it */
  1098. } else {
  1099. int pte_off_even = sizeof(pte_t) / 2;
  1100. int pte_off_odd = pte_off_even + sizeof(pte_t);
  1101. /* The pte entries are pre-shifted */
  1102. i_lw(p, tmp, pte_off_even, ptep); /* get even pte */
  1103. i_mtc0(p, tmp, C0_ENTRYLO0); /* load it */
  1104. i_lw(p, ptep, pte_off_odd, ptep); /* get odd pte */
  1105. i_mtc0(p, ptep, C0_ENTRYLO1); /* load it */
  1106. }
  1107. #else
  1108. i_LW(p, tmp, 0, ptep); /* get even pte */
  1109. i_LW(p, ptep, sizeof(pte_t), ptep); /* get odd pte */
  1110. if (r45k_bvahwbug())
  1111. build_tlb_probe_entry(p);
  1112. i_SRL(p, tmp, tmp, 6); /* convert to entrylo0 */
  1113. if (r4k_250MHZhwbug())
  1114. i_mtc0(p, 0, C0_ENTRYLO0);
  1115. i_mtc0(p, tmp, C0_ENTRYLO0); /* load it */
  1116. i_SRL(p, ptep, ptep, 6); /* convert to entrylo1 */
  1117. if (r45k_bvahwbug())
  1118. i_mfc0(p, tmp, C0_INDEX);
  1119. if (r4k_250MHZhwbug())
  1120. i_mtc0(p, 0, C0_ENTRYLO1);
  1121. i_mtc0(p, ptep, C0_ENTRYLO1); /* load it */
  1122. #endif
  1123. }
  1124. static void __init build_r4000_tlb_refill_handler(void)
  1125. {
  1126. u32 *p = tlb_handler;
  1127. struct label *l = labels;
  1128. struct reloc *r = relocs;
  1129. u32 *f;
  1130. unsigned int final_len;
  1131. memset(tlb_handler, 0, sizeof(tlb_handler));
  1132. memset(labels, 0, sizeof(labels));
  1133. memset(relocs, 0, sizeof(relocs));
  1134. memset(final_handler, 0, sizeof(final_handler));
  1135. /*
  1136. * create the plain linear handler
  1137. */
  1138. if (bcm1250_m3_war()) {
  1139. i_MFC0(&p, K0, C0_BADVADDR);
  1140. i_MFC0(&p, K1, C0_ENTRYHI);
  1141. i_xor(&p, K0, K0, K1);
  1142. i_SRL(&p, K0, K0, PAGE_SHIFT + 1);
  1143. il_bnez(&p, &r, K0, label_leave);
  1144. /* No need for i_nop */
  1145. }
  1146. #ifdef CONFIG_64BIT
  1147. build_get_pmde64(&p, &l, &r, K0, K1); /* get pmd in K1 */
  1148. #else
  1149. build_get_pgde32(&p, K0, K1); /* get pgd in K1 */
  1150. #endif
  1151. build_get_ptep(&p, K0, K1);
  1152. build_update_entries(&p, K0, K1);
  1153. build_tlb_write_entry(&p, &l, &r, tlb_random);
  1154. l_leave(&l, p);
  1155. i_eret(&p); /* return from trap */
  1156. #ifdef CONFIG_64BIT
  1157. build_get_pgd_vmalloc64(&p, &l, &r, K0, K1);
  1158. #endif
  1159. /*
  1160. * Overflow check: For the 64bit handler, we need at least one
  1161. * free instruction slot for the wrap-around branch. In worst
  1162. * case, if the intended insertion point is a delay slot, we
  1163. * need three, with the second nop'ed and the third being
  1164. * unused.
  1165. */
  1166. /* Loongson2 ebase is different than r4k, we have more space */
  1167. #if defined(CONFIG_32BIT) || defined(CONFIG_CPU_LOONGSON2)
  1168. if ((p - tlb_handler) > 64)
  1169. panic("TLB refill handler space exceeded");
  1170. #else
  1171. if (((p - tlb_handler) > 63)
  1172. || (((p - tlb_handler) > 61)
  1173. && insn_has_bdelay(relocs, tlb_handler + 29)))
  1174. panic("TLB refill handler space exceeded");
  1175. #endif
  1176. /*
  1177. * Now fold the handler in the TLB refill handler space.
  1178. */
  1179. #if defined(CONFIG_32BIT) || defined(CONFIG_CPU_LOONGSON2)
  1180. f = final_handler;
  1181. /* Simplest case, just copy the handler. */
  1182. copy_handler(relocs, labels, tlb_handler, p, f);
  1183. final_len = p - tlb_handler;
  1184. #else /* CONFIG_64BIT */
  1185. f = final_handler + 32;
  1186. if ((p - tlb_handler) <= 32) {
  1187. /* Just copy the handler. */
  1188. copy_handler(relocs, labels, tlb_handler, p, f);
  1189. final_len = p - tlb_handler;
  1190. } else {
  1191. u32 *split = tlb_handler + 30;
  1192. /*
  1193. * Find the split point.
  1194. */
  1195. if (insn_has_bdelay(relocs, split - 1))
  1196. split--;
  1197. /* Copy first part of the handler. */
  1198. copy_handler(relocs, labels, tlb_handler, split, f);
  1199. f += split - tlb_handler;
  1200. /* Insert branch. */
  1201. l_split(&l, final_handler);
  1202. il_b(&f, &r, label_split);
  1203. if (insn_has_bdelay(relocs, split))
  1204. i_nop(&f);
  1205. else {
  1206. copy_handler(relocs, labels, split, split + 1, f);
  1207. move_labels(labels, f, f + 1, -1);
  1208. f++;
  1209. split++;
  1210. }
  1211. /* Copy the rest of the handler. */
  1212. copy_handler(relocs, labels, split, p, final_handler);
  1213. final_len = (f - (final_handler + 32)) + (p - split);
  1214. }
  1215. #endif /* CONFIG_64BIT */
  1216. resolve_relocs(relocs, labels);
  1217. pr_info("Synthesized TLB refill handler (%u instructions).\n",
  1218. final_len);
  1219. memcpy((void *)ebase, final_handler, 0x100);
  1220. dump_handler((u32 *)ebase, 64);
  1221. }
  1222. /*
  1223. * TLB load/store/modify handlers.
  1224. *
  1225. * Only the fastpath gets synthesized at runtime, the slowpath for
  1226. * do_page_fault remains normal asm.
  1227. */
  1228. extern void tlb_do_page_fault_0(void);
  1229. extern void tlb_do_page_fault_1(void);
  1230. /*
  1231. * 128 instructions for the fastpath handler is generous and should
  1232. * never be exceeded.
  1233. */
  1234. #define FASTPATH_SIZE 128
  1235. u32 handle_tlbl[FASTPATH_SIZE] __cacheline_aligned;
  1236. u32 handle_tlbs[FASTPATH_SIZE] __cacheline_aligned;
  1237. u32 handle_tlbm[FASTPATH_SIZE] __cacheline_aligned;
  1238. static void __init
  1239. iPTE_LW(u32 **p, struct label **l, unsigned int pte, unsigned int ptr)
  1240. {
  1241. #ifdef CONFIG_SMP
  1242. # ifdef CONFIG_64BIT_PHYS_ADDR
  1243. if (cpu_has_64bits)
  1244. i_lld(p, pte, 0, ptr);
  1245. else
  1246. # endif
  1247. i_LL(p, pte, 0, ptr);
  1248. #else
  1249. # ifdef CONFIG_64BIT_PHYS_ADDR
  1250. if (cpu_has_64bits)
  1251. i_ld(p, pte, 0, ptr);
  1252. else
  1253. # endif
  1254. i_LW(p, pte, 0, ptr);
  1255. #endif
  1256. }
  1257. static void __init
  1258. iPTE_SW(u32 **p, struct reloc **r, unsigned int pte, unsigned int ptr,
  1259. unsigned int mode)
  1260. {
  1261. #ifdef CONFIG_64BIT_PHYS_ADDR
  1262. unsigned int hwmode = mode & (_PAGE_VALID | _PAGE_DIRTY);
  1263. #endif
  1264. i_ori(p, pte, pte, mode);
  1265. #ifdef CONFIG_SMP
  1266. # ifdef CONFIG_64BIT_PHYS_ADDR
  1267. if (cpu_has_64bits)
  1268. i_scd(p, pte, 0, ptr);
  1269. else
  1270. # endif
  1271. i_SC(p, pte, 0, ptr);
  1272. if (r10000_llsc_war())
  1273. il_beqzl(p, r, pte, label_smp_pgtable_change);
  1274. else
  1275. il_beqz(p, r, pte, label_smp_pgtable_change);
  1276. # ifdef CONFIG_64BIT_PHYS_ADDR
  1277. if (!cpu_has_64bits) {
  1278. /* no i_nop needed */
  1279. i_ll(p, pte, sizeof(pte_t) / 2, ptr);
  1280. i_ori(p, pte, pte, hwmode);
  1281. i_sc(p, pte, sizeof(pte_t) / 2, ptr);
  1282. il_beqz(p, r, pte, label_smp_pgtable_change);
  1283. /* no i_nop needed */
  1284. i_lw(p, pte, 0, ptr);
  1285. } else
  1286. i_nop(p);
  1287. # else
  1288. i_nop(p);
  1289. # endif
  1290. #else
  1291. # ifdef CONFIG_64BIT_PHYS_ADDR
  1292. if (cpu_has_64bits)
  1293. i_sd(p, pte, 0, ptr);
  1294. else
  1295. # endif
  1296. i_SW(p, pte, 0, ptr);
  1297. # ifdef CONFIG_64BIT_PHYS_ADDR
  1298. if (!cpu_has_64bits) {
  1299. i_lw(p, pte, sizeof(pte_t) / 2, ptr);
  1300. i_ori(p, pte, pte, hwmode);
  1301. i_sw(p, pte, sizeof(pte_t) / 2, ptr);
  1302. i_lw(p, pte, 0, ptr);
  1303. }
  1304. # endif
  1305. #endif
  1306. }
  1307. /*
  1308. * Check if PTE is present, if not then jump to LABEL. PTR points to
  1309. * the page table where this PTE is located, PTE will be re-loaded
  1310. * with it's original value.
  1311. */
  1312. static void __init
  1313. build_pte_present(u32 **p, struct label **l, struct reloc **r,
  1314. unsigned int pte, unsigned int ptr, enum label_id lid)
  1315. {
  1316. i_andi(p, pte, pte, _PAGE_PRESENT | _PAGE_READ);
  1317. i_xori(p, pte, pte, _PAGE_PRESENT | _PAGE_READ);
  1318. il_bnez(p, r, pte, lid);
  1319. iPTE_LW(p, l, pte, ptr);
  1320. }
  1321. /* Make PTE valid, store result in PTR. */
  1322. static void __init
  1323. build_make_valid(u32 **p, struct reloc **r, unsigned int pte,
  1324. unsigned int ptr)
  1325. {
  1326. unsigned int mode = _PAGE_VALID | _PAGE_ACCESSED;
  1327. iPTE_SW(p, r, pte, ptr, mode);
  1328. }
  1329. /*
  1330. * Check if PTE can be written to, if not branch to LABEL. Regardless
  1331. * restore PTE with value from PTR when done.
  1332. */
  1333. static void __init
  1334. build_pte_writable(u32 **p, struct label **l, struct reloc **r,
  1335. unsigned int pte, unsigned int ptr, enum label_id lid)
  1336. {
  1337. i_andi(p, pte, pte, _PAGE_PRESENT | _PAGE_WRITE);
  1338. i_xori(p, pte, pte, _PAGE_PRESENT | _PAGE_WRITE);
  1339. il_bnez(p, r, pte, lid);
  1340. iPTE_LW(p, l, pte, ptr);
  1341. }
  1342. /* Make PTE writable, update software status bits as well, then store
  1343. * at PTR.
  1344. */
  1345. static void __init
  1346. build_make_write(u32 **p, struct reloc **r, unsigned int pte,
  1347. unsigned int ptr)
  1348. {
  1349. unsigned int mode = (_PAGE_ACCESSED | _PAGE_MODIFIED | _PAGE_VALID
  1350. | _PAGE_DIRTY);
  1351. iPTE_SW(p, r, pte, ptr, mode);
  1352. }
  1353. /*
  1354. * Check if PTE can be modified, if not branch to LABEL. Regardless
  1355. * restore PTE with value from PTR when done.
  1356. */
  1357. static void __init
  1358. build_pte_modifiable(u32 **p, struct label **l, struct reloc **r,
  1359. unsigned int pte, unsigned int ptr, enum label_id lid)
  1360. {
  1361. i_andi(p, pte, pte, _PAGE_WRITE);
  1362. il_beqz(p, r, pte, lid);
  1363. iPTE_LW(p, l, pte, ptr);
  1364. }
  1365. /*
  1366. * R3000 style TLB load/store/modify handlers.
  1367. */
  1368. /*
  1369. * This places the pte into ENTRYLO0 and writes it with tlbwi.
  1370. * Then it returns.
  1371. */
  1372. static void __init
  1373. build_r3000_pte_reload_tlbwi(u32 **p, unsigned int pte, unsigned int tmp)
  1374. {
  1375. i_mtc0(p, pte, C0_ENTRYLO0); /* cp0 delay */
  1376. i_mfc0(p, tmp, C0_EPC); /* cp0 delay */
  1377. i_tlbwi(p);
  1378. i_jr(p, tmp);
  1379. i_rfe(p); /* branch delay */
  1380. }
  1381. /*
  1382. * This places the pte into ENTRYLO0 and writes it with tlbwi
  1383. * or tlbwr as appropriate. This is because the index register
  1384. * may have the probe fail bit set as a result of a trap on a
  1385. * kseg2 access, i.e. without refill. Then it returns.
  1386. */
  1387. static void __init
  1388. build_r3000_tlb_reload_write(u32 **p, struct label **l, struct reloc **r,
  1389. unsigned int pte, unsigned int tmp)
  1390. {
  1391. i_mfc0(p, tmp, C0_INDEX);
  1392. i_mtc0(p, pte, C0_ENTRYLO0); /* cp0 delay */
  1393. il_bltz(p, r, tmp, label_r3000_write_probe_fail); /* cp0 delay */
  1394. i_mfc0(p, tmp, C0_EPC); /* branch delay */
  1395. i_tlbwi(p); /* cp0 delay */
  1396. i_jr(p, tmp);
  1397. i_rfe(p); /* branch delay */
  1398. l_r3000_write_probe_fail(l, *p);
  1399. i_tlbwr(p); /* cp0 delay */
  1400. i_jr(p, tmp);
  1401. i_rfe(p); /* branch delay */
  1402. }
  1403. static void __init
  1404. build_r3000_tlbchange_handler_head(u32 **p, unsigned int pte,
  1405. unsigned int ptr)
  1406. {
  1407. long pgdc = (long)pgd_current;
  1408. i_mfc0(p, pte, C0_BADVADDR);
  1409. i_lui(p, ptr, rel_hi(pgdc)); /* cp0 delay */
  1410. i_lw(p, ptr, rel_lo(pgdc), ptr);
  1411. i_srl(p, pte, pte, 22); /* load delay */
  1412. i_sll(p, pte, pte, 2);
  1413. i_addu(p, ptr, ptr, pte);
  1414. i_mfc0(p, pte, C0_CONTEXT);
  1415. i_lw(p, ptr, 0, ptr); /* cp0 delay */
  1416. i_andi(p, pte, pte, 0xffc); /* load delay */
  1417. i_addu(p, ptr, ptr, pte);
  1418. i_lw(p, pte, 0, ptr);
  1419. i_tlbp(p); /* load delay */
  1420. }
  1421. static void __init build_r3000_tlb_load_handler(void)
  1422. {
  1423. u32 *p = handle_tlbl;
  1424. struct label *l = labels;
  1425. struct reloc *r = relocs;
  1426. memset(handle_tlbl, 0, sizeof(handle_tlbl));
  1427. memset(labels, 0, sizeof(labels));
  1428. memset(relocs, 0, sizeof(relocs));
  1429. build_r3000_tlbchange_handler_head(&p, K0, K1);
  1430. build_pte_present(&p, &l, &r, K0, K1, label_nopage_tlbl);
  1431. i_nop(&p); /* load delay */
  1432. build_make_valid(&p, &r, K0, K1);
  1433. build_r3000_tlb_reload_write(&p, &l, &r, K0, K1);
  1434. l_nopage_tlbl(&l, p);
  1435. i_j(&p, (unsigned long)tlb_do_page_fault_0 & 0x0fffffff);
  1436. i_nop(&p);
  1437. if ((p - handle_tlbl) > FASTPATH_SIZE)
  1438. panic("TLB load handler fastpath space exceeded");
  1439. resolve_relocs(relocs, labels);
  1440. pr_info("Synthesized TLB load handler fastpath (%u instructions).\n",
  1441. (unsigned int)(p - handle_tlbl));
  1442. dump_handler(handle_tlbl, ARRAY_SIZE(handle_tlbl));
  1443. }
  1444. static void __init build_r3000_tlb_store_handler(void)
  1445. {
  1446. u32 *p = handle_tlbs;
  1447. struct label *l = labels;
  1448. struct reloc *r = relocs;
  1449. memset(handle_tlbs, 0, sizeof(handle_tlbs));
  1450. memset(labels, 0, sizeof(labels));
  1451. memset(relocs, 0, sizeof(relocs));
  1452. build_r3000_tlbchange_handler_head(&p, K0, K1);
  1453. build_pte_writable(&p, &l, &r, K0, K1, label_nopage_tlbs);
  1454. i_nop(&p); /* load delay */
  1455. build_make_write(&p, &r, K0, K1);
  1456. build_r3000_tlb_reload_write(&p, &l, &r, K0, K1);
  1457. l_nopage_tlbs(&l, p);
  1458. i_j(&p, (unsigned long)tlb_do_page_fault_1 & 0x0fffffff);
  1459. i_nop(&p);
  1460. if ((p - handle_tlbs) > FASTPATH_SIZE)
  1461. panic("TLB store handler fastpath space exceeded");
  1462. resolve_relocs(relocs, labels);
  1463. pr_info("Synthesized TLB store handler fastpath (%u instructions).\n",
  1464. (unsigned int)(p - handle_tlbs));
  1465. dump_handler(handle_tlbs, ARRAY_SIZE(handle_tlbs));
  1466. }
  1467. static void __init build_r3000_tlb_modify_handler(void)
  1468. {
  1469. u32 *p = handle_tlbm;
  1470. struct label *l = labels;
  1471. struct reloc *r = relocs;
  1472. memset(handle_tlbm, 0, sizeof(handle_tlbm));
  1473. memset(labels, 0, sizeof(labels));
  1474. memset(relocs, 0, sizeof(relocs));
  1475. build_r3000_tlbchange_handler_head(&p, K0, K1);
  1476. build_pte_modifiable(&p, &l, &r, K0, K1, label_nopage_tlbm);
  1477. i_nop(&p); /* load delay */
  1478. build_make_write(&p, &r, K0, K1);
  1479. build_r3000_pte_reload_tlbwi(&p, K0, K1);
  1480. l_nopage_tlbm(&l, p);
  1481. i_j(&p, (unsigned long)tlb_do_page_fault_1 & 0x0fffffff);
  1482. i_nop(&p);
  1483. if ((p - handle_tlbm) > FASTPATH_SIZE)
  1484. panic("TLB modify handler fastpath space exceeded");
  1485. resolve_relocs(relocs, labels);
  1486. pr_info("Synthesized TLB modify handler fastpath (%u instructions).\n",
  1487. (unsigned int)(p - handle_tlbm));
  1488. dump_handler(handle_tlbm, ARRAY_SIZE(handle_tlbm));
  1489. }
  1490. /*
  1491. * R4000 style TLB load/store/modify handlers.
  1492. */
  1493. static void __init
  1494. build_r4000_tlbchange_handler_head(u32 **p, struct label **l,
  1495. struct reloc **r, unsigned int pte,
  1496. unsigned int ptr)
  1497. {
  1498. #ifdef CONFIG_64BIT
  1499. build_get_pmde64(p, l, r, pte, ptr); /* get pmd in ptr */
  1500. #else
  1501. build_get_pgde32(p, pte, ptr); /* get pgd in ptr */
  1502. #endif
  1503. i_MFC0(p, pte, C0_BADVADDR);
  1504. i_LW(p, ptr, 0, ptr);
  1505. i_SRL(p, pte, pte, PAGE_SHIFT + PTE_ORDER - PTE_T_LOG2);
  1506. i_andi(p, pte, pte, (PTRS_PER_PTE - 1) << PTE_T_LOG2);
  1507. i_ADDU(p, ptr, ptr, pte);
  1508. #ifdef CONFIG_SMP
  1509. l_smp_pgtable_change(l, *p);
  1510. # endif
  1511. iPTE_LW(p, l, pte, ptr); /* get even pte */
  1512. if (!m4kc_tlbp_war())
  1513. build_tlb_probe_entry(p);
  1514. }
  1515. static void __init
  1516. build_r4000_tlbchange_handler_tail(u32 **p, struct label **l,
  1517. struct reloc **r, unsigned int tmp,
  1518. unsigned int ptr)
  1519. {
  1520. i_ori(p, ptr, ptr, sizeof(pte_t));
  1521. i_xori(p, ptr, ptr, sizeof(pte_t));
  1522. build_update_entries(p, tmp, ptr);
  1523. build_tlb_write_entry(p, l, r, tlb_indexed);
  1524. l_leave(l, *p);
  1525. i_eret(p); /* return from trap */
  1526. #ifdef CONFIG_64BIT
  1527. build_get_pgd_vmalloc64(p, l, r, tmp, ptr);
  1528. #endif
  1529. }
  1530. static void __init build_r4000_tlb_load_handler(void)
  1531. {
  1532. u32 *p = handle_tlbl;
  1533. struct label *l = labels;
  1534. struct reloc *r = relocs;
  1535. memset(handle_tlbl, 0, sizeof(handle_tlbl));
  1536. memset(labels, 0, sizeof(labels));
  1537. memset(relocs, 0, sizeof(relocs));
  1538. if (bcm1250_m3_war()) {
  1539. i_MFC0(&p, K0, C0_BADVADDR);
  1540. i_MFC0(&p, K1, C0_ENTRYHI);
  1541. i_xor(&p, K0, K0, K1);
  1542. i_SRL(&p, K0, K0, PAGE_SHIFT + 1);
  1543. il_bnez(&p, &r, K0, label_leave);
  1544. /* No need for i_nop */
  1545. }
  1546. build_r4000_tlbchange_handler_head(&p, &l, &r, K0, K1);
  1547. build_pte_present(&p, &l, &r, K0, K1, label_nopage_tlbl);
  1548. if (m4kc_tlbp_war())
  1549. build_tlb_probe_entry(&p);
  1550. build_make_valid(&p, &r, K0, K1);
  1551. build_r4000_tlbchange_handler_tail(&p, &l, &r, K0, K1);
  1552. l_nopage_tlbl(&l, p);
  1553. i_j(&p, (unsigned long)tlb_do_page_fault_0 & 0x0fffffff);
  1554. i_nop(&p);
  1555. if ((p - handle_tlbl) > FASTPATH_SIZE)
  1556. panic("TLB load handler fastpath space exceeded");
  1557. resolve_relocs(relocs, labels);
  1558. pr_info("Synthesized TLB load handler fastpath (%u instructions).\n",
  1559. (unsigned int)(p - handle_tlbl));
  1560. dump_handler(handle_tlbl, ARRAY_SIZE(handle_tlbl));
  1561. }
  1562. static void __init build_r4000_tlb_store_handler(void)
  1563. {
  1564. u32 *p = handle_tlbs;
  1565. struct label *l = labels;
  1566. struct reloc *r = relocs;
  1567. memset(handle_tlbs, 0, sizeof(handle_tlbs));
  1568. memset(labels, 0, sizeof(labels));
  1569. memset(relocs, 0, sizeof(relocs));
  1570. build_r4000_tlbchange_handler_head(&p, &l, &r, K0, K1);
  1571. build_pte_writable(&p, &l, &r, K0, K1, label_nopage_tlbs);
  1572. if (m4kc_tlbp_war())
  1573. build_tlb_probe_entry(&p);
  1574. build_make_write(&p, &r, K0, K1);
  1575. build_r4000_tlbchange_handler_tail(&p, &l, &r, K0, K1);
  1576. l_nopage_tlbs(&l, p);
  1577. i_j(&p, (unsigned long)tlb_do_page_fault_1 & 0x0fffffff);
  1578. i_nop(&p);
  1579. if ((p - handle_tlbs) > FASTPATH_SIZE)
  1580. panic("TLB store handler fastpath space exceeded");
  1581. resolve_relocs(relocs, labels);
  1582. pr_info("Synthesized TLB store handler fastpath (%u instructions).\n",
  1583. (unsigned int)(p - handle_tlbs));
  1584. dump_handler(handle_tlbs, ARRAY_SIZE(handle_tlbs));
  1585. }
  1586. static void __init build_r4000_tlb_modify_handler(void)
  1587. {
  1588. u32 *p = handle_tlbm;
  1589. struct label *l = labels;
  1590. struct reloc *r = relocs;
  1591. memset(handle_tlbm, 0, sizeof(handle_tlbm));
  1592. memset(labels, 0, sizeof(labels));
  1593. memset(relocs, 0, sizeof(relocs));
  1594. build_r4000_tlbchange_handler_head(&p, &l, &r, K0, K1);
  1595. build_pte_modifiable(&p, &l, &r, K0, K1, label_nopage_tlbm);
  1596. if (m4kc_tlbp_war())
  1597. build_tlb_probe_entry(&p);
  1598. /* Present and writable bits set, set accessed and dirty bits. */
  1599. build_make_write(&p, &r, K0, K1);
  1600. build_r4000_tlbchange_handler_tail(&p, &l, &r, K0, K1);
  1601. l_nopage_tlbm(&l, p);
  1602. i_j(&p, (unsigned long)tlb_do_page_fault_1 & 0x0fffffff);
  1603. i_nop(&p);
  1604. if ((p - handle_tlbm) > FASTPATH_SIZE)
  1605. panic("TLB modify handler fastpath space exceeded");
  1606. resolve_relocs(relocs, labels);
  1607. pr_info("Synthesized TLB modify handler fastpath (%u instructions).\n",
  1608. (unsigned int)(p - handle_tlbm));
  1609. dump_handler(handle_tlbm, ARRAY_SIZE(handle_tlbm));
  1610. }
  1611. void __init build_tlb_refill_handler(void)
  1612. {
  1613. /*
  1614. * The refill handler is generated per-CPU, multi-node systems
  1615. * may have local storage for it. The other handlers are only
  1616. * needed once.
  1617. */
  1618. static int run_once = 0;
  1619. switch (current_cpu_type()) {
  1620. case CPU_R2000:
  1621. case CPU_R3000:
  1622. case CPU_R3000A:
  1623. case CPU_R3081E:
  1624. case CPU_TX3912:
  1625. case CPU_TX3922:
  1626. case CPU_TX3927:
  1627. build_r3000_tlb_refill_handler();
  1628. if (!run_once) {
  1629. build_r3000_tlb_load_handler();
  1630. build_r3000_tlb_store_handler();
  1631. build_r3000_tlb_modify_handler();
  1632. run_once++;
  1633. }
  1634. break;
  1635. case CPU_R6000:
  1636. case CPU_R6000A:
  1637. panic("No R6000 TLB refill handler yet");
  1638. break;
  1639. case CPU_R8000:
  1640. panic("No R8000 TLB refill handler yet");
  1641. break;
  1642. default:
  1643. build_r4000_tlb_refill_handler();
  1644. if (!run_once) {
  1645. build_r4000_tlb_load_handler();
  1646. build_r4000_tlb_store_handler();
  1647. build_r4000_tlb_modify_handler();
  1648. run_once++;
  1649. }
  1650. }
  1651. }
  1652. void __init flush_tlb_handlers(void)
  1653. {
  1654. flush_icache_range((unsigned long)handle_tlbl,
  1655. (unsigned long)handle_tlbl + sizeof(handle_tlbl));
  1656. flush_icache_range((unsigned long)handle_tlbs,
  1657. (unsigned long)handle_tlbs + sizeof(handle_tlbs));
  1658. flush_icache_range((unsigned long)handle_tlbm,
  1659. (unsigned long)handle_tlbm + sizeof(handle_tlbm));
  1660. }