tlbex.c 45 KB

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