tlbex.c 45 KB

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