tlbex.c 45 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815
  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_R4600:
  727. case CPU_R4700:
  728. case CPU_R5000:
  729. case CPU_R5000A:
  730. case CPU_5KC:
  731. case CPU_TX49XX:
  732. case CPU_AU1000:
  733. case CPU_AU1100:
  734. case CPU_AU1500:
  735. case CPU_AU1550:
  736. i_nop(p);
  737. tlbw(p);
  738. break;
  739. case CPU_R10000:
  740. case CPU_R12000:
  741. case CPU_4KC:
  742. case CPU_SB1:
  743. case CPU_4KSC:
  744. case CPU_20KC:
  745. case CPU_25KF:
  746. tlbw(p);
  747. break;
  748. case CPU_NEVADA:
  749. i_nop(p); /* QED specifies 2 nops hazard */
  750. /*
  751. * This branch uses up a mtc0 hazard nop slot and saves
  752. * a nop after the tlbw instruction.
  753. */
  754. il_bgezl(p, r, 0, label_tlbw_hazard);
  755. tlbw(p);
  756. l_tlbw_hazard(l, *p);
  757. break;
  758. case CPU_RM7000:
  759. i_nop(p);
  760. i_nop(p);
  761. i_nop(p);
  762. i_nop(p);
  763. tlbw(p);
  764. break;
  765. case CPU_4KEC:
  766. case CPU_24K:
  767. i_ehb(p);
  768. tlbw(p);
  769. break;
  770. case CPU_RM9000:
  771. /*
  772. * When the JTLB is updated by tlbwi or tlbwr, a subsequent
  773. * use of the JTLB for instructions should not occur for 4
  774. * cpu cycles and use for data translations should not occur
  775. * for 3 cpu cycles.
  776. */
  777. i_ssnop(p);
  778. i_ssnop(p);
  779. i_ssnop(p);
  780. i_ssnop(p);
  781. tlbw(p);
  782. i_ssnop(p);
  783. i_ssnop(p);
  784. i_ssnop(p);
  785. i_ssnop(p);
  786. break;
  787. case CPU_VR4111:
  788. case CPU_VR4121:
  789. case CPU_VR4122:
  790. case CPU_VR4181:
  791. case CPU_VR4181A:
  792. i_nop(p);
  793. i_nop(p);
  794. tlbw(p);
  795. i_nop(p);
  796. i_nop(p);
  797. break;
  798. case CPU_VR4131:
  799. case CPU_VR4133:
  800. i_nop(p);
  801. i_nop(p);
  802. tlbw(p);
  803. break;
  804. default:
  805. panic("No TLB refill handler yet (CPU type: %d)",
  806. current_cpu_data.cputype);
  807. break;
  808. }
  809. }
  810. #ifdef CONFIG_64BIT
  811. /*
  812. * TMP and PTR are scratch.
  813. * TMP will be clobbered, PTR will hold the pmd entry.
  814. */
  815. static __init void
  816. build_get_pmde64(u32 **p, struct label **l, struct reloc **r,
  817. unsigned int tmp, unsigned int ptr)
  818. {
  819. long pgdc = (long)pgd_current;
  820. /*
  821. * The vmalloc handling is not in the hotpath.
  822. */
  823. i_dmfc0(p, tmp, C0_BADVADDR);
  824. il_bltz(p, r, tmp, label_vmalloc);
  825. /* No i_nop needed here, since the next insn doesn't touch TMP. */
  826. #ifdef CONFIG_SMP
  827. /*
  828. * 64 bit SMP has the lower part of &pgd_current[smp_processor_id()]
  829. * stored in CONTEXT.
  830. */
  831. if (in_compat_space_p(pgdc)) {
  832. i_dmfc0(p, ptr, C0_CONTEXT);
  833. i_dsra(p, ptr, ptr, 23);
  834. i_ld(p, ptr, 0, ptr);
  835. } else {
  836. #ifdef CONFIG_BUILD_ELF64
  837. i_dmfc0(p, ptr, C0_CONTEXT);
  838. i_dsrl(p, ptr, ptr, 23);
  839. i_dsll(p, ptr, ptr, 3);
  840. i_LA_mostly(p, tmp, pgdc);
  841. i_daddu(p, ptr, ptr, tmp);
  842. i_dmfc0(p, tmp, C0_BADVADDR);
  843. i_ld(p, ptr, rel_lo(pgdc), ptr);
  844. #else
  845. i_dmfc0(p, ptr, C0_CONTEXT);
  846. i_lui(p, tmp, rel_highest(pgdc));
  847. i_dsll(p, ptr, ptr, 9);
  848. i_daddiu(p, tmp, tmp, rel_higher(pgdc));
  849. i_dsrl32(p, ptr, ptr, 0);
  850. i_and(p, ptr, ptr, tmp);
  851. i_dmfc0(p, tmp, C0_BADVADDR);
  852. i_ld(p, ptr, 0, ptr);
  853. #endif
  854. }
  855. #else
  856. i_LA_mostly(p, ptr, pgdc);
  857. i_ld(p, ptr, rel_lo(pgdc), ptr);
  858. #endif
  859. l_vmalloc_done(l, *p);
  860. i_dsrl(p, tmp, tmp, PGDIR_SHIFT-3); /* get pgd offset in bytes */
  861. i_andi(p, tmp, tmp, (PTRS_PER_PGD - 1)<<3);
  862. i_daddu(p, ptr, ptr, tmp); /* add in pgd offset */
  863. i_dmfc0(p, tmp, C0_BADVADDR); /* get faulting address */
  864. i_ld(p, ptr, 0, ptr); /* get pmd pointer */
  865. i_dsrl(p, tmp, tmp, PMD_SHIFT-3); /* get pmd offset in bytes */
  866. i_andi(p, tmp, tmp, (PTRS_PER_PMD - 1)<<3);
  867. i_daddu(p, ptr, ptr, tmp); /* add in pmd offset */
  868. }
  869. /*
  870. * BVADDR is the faulting address, PTR is scratch.
  871. * PTR will hold the pgd for vmalloc.
  872. */
  873. static __init void
  874. build_get_pgd_vmalloc64(u32 **p, struct label **l, struct reloc **r,
  875. unsigned int bvaddr, unsigned int ptr)
  876. {
  877. long swpd = (long)swapper_pg_dir;
  878. l_vmalloc(l, *p);
  879. i_LA(p, ptr, VMALLOC_START);
  880. i_dsubu(p, bvaddr, bvaddr, ptr);
  881. if (in_compat_space_p(swpd) && !rel_lo(swpd)) {
  882. il_b(p, r, label_vmalloc_done);
  883. i_lui(p, ptr, rel_hi(swpd));
  884. } else {
  885. i_LA_mostly(p, ptr, swpd);
  886. il_b(p, r, label_vmalloc_done);
  887. i_daddiu(p, ptr, ptr, rel_lo(swpd));
  888. }
  889. }
  890. #else /* !CONFIG_64BIT */
  891. /*
  892. * TMP and PTR are scratch.
  893. * TMP will be clobbered, PTR will hold the pgd entry.
  894. */
  895. static __init void __attribute__((unused))
  896. build_get_pgde32(u32 **p, unsigned int tmp, unsigned int ptr)
  897. {
  898. long pgdc = (long)pgd_current;
  899. /* 32 bit SMP has smp_processor_id() stored in CONTEXT. */
  900. #ifdef CONFIG_SMP
  901. i_mfc0(p, ptr, C0_CONTEXT);
  902. i_LA_mostly(p, tmp, pgdc);
  903. i_srl(p, ptr, ptr, 23);
  904. i_sll(p, ptr, ptr, 2);
  905. i_addu(p, ptr, tmp, ptr);
  906. #else
  907. i_LA_mostly(p, ptr, pgdc);
  908. #endif
  909. i_mfc0(p, tmp, C0_BADVADDR); /* get faulting address */
  910. i_lw(p, ptr, rel_lo(pgdc), ptr);
  911. i_srl(p, tmp, tmp, PGDIR_SHIFT); /* get pgd only bits */
  912. i_sll(p, tmp, tmp, PGD_T_LOG2);
  913. i_addu(p, ptr, ptr, tmp); /* add in pgd offset */
  914. }
  915. #endif /* !CONFIG_64BIT */
  916. static __init void build_adjust_context(u32 **p, unsigned int ctx)
  917. {
  918. unsigned int shift = 4 - (PTE_T_LOG2 + 1);
  919. unsigned int mask = (PTRS_PER_PTE / 2 - 1) << (PTE_T_LOG2 + 1);
  920. switch (current_cpu_data.cputype) {
  921. case CPU_VR41XX:
  922. case CPU_VR4111:
  923. case CPU_VR4121:
  924. case CPU_VR4122:
  925. case CPU_VR4131:
  926. case CPU_VR4181:
  927. case CPU_VR4181A:
  928. case CPU_VR4133:
  929. shift += 2;
  930. break;
  931. default:
  932. break;
  933. }
  934. if (shift)
  935. i_SRL(p, ctx, ctx, shift);
  936. i_andi(p, ctx, ctx, mask);
  937. }
  938. static __init void build_get_ptep(u32 **p, unsigned int tmp, unsigned int ptr)
  939. {
  940. /*
  941. * Bug workaround for the Nevada. It seems as if under certain
  942. * circumstances the move from cp0_context might produce a
  943. * bogus result when the mfc0 instruction and its consumer are
  944. * in a different cacheline or a load instruction, probably any
  945. * memory reference, is between them.
  946. */
  947. switch (current_cpu_data.cputype) {
  948. case CPU_NEVADA:
  949. i_LW(p, ptr, 0, ptr);
  950. GET_CONTEXT(p, tmp); /* get context reg */
  951. break;
  952. default:
  953. GET_CONTEXT(p, tmp); /* get context reg */
  954. i_LW(p, ptr, 0, ptr);
  955. break;
  956. }
  957. build_adjust_context(p, tmp);
  958. i_ADDU(p, ptr, ptr, tmp); /* add in offset */
  959. }
  960. static __init void build_update_entries(u32 **p, unsigned int tmp,
  961. unsigned int ptep)
  962. {
  963. /*
  964. * 64bit address support (36bit on a 32bit CPU) in a 32bit
  965. * Kernel is a special case. Only a few CPUs use it.
  966. */
  967. #ifdef CONFIG_64BIT_PHYS_ADDR
  968. if (cpu_has_64bits) {
  969. i_ld(p, tmp, 0, ptep); /* get even pte */
  970. i_ld(p, ptep, sizeof(pte_t), ptep); /* get odd pte */
  971. i_dsrl(p, tmp, tmp, 6); /* convert to entrylo0 */
  972. i_mtc0(p, tmp, C0_ENTRYLO0); /* load it */
  973. i_dsrl(p, ptep, ptep, 6); /* convert to entrylo1 */
  974. i_mtc0(p, ptep, C0_ENTRYLO1); /* load it */
  975. } else {
  976. int pte_off_even = sizeof(pte_t) / 2;
  977. int pte_off_odd = pte_off_even + sizeof(pte_t);
  978. /* The pte entries are pre-shifted */
  979. i_lw(p, tmp, pte_off_even, ptep); /* get even pte */
  980. i_mtc0(p, tmp, C0_ENTRYLO0); /* load it */
  981. i_lw(p, ptep, pte_off_odd, ptep); /* get odd pte */
  982. i_mtc0(p, ptep, C0_ENTRYLO1); /* load it */
  983. }
  984. #else
  985. i_LW(p, tmp, 0, ptep); /* get even pte */
  986. i_LW(p, ptep, sizeof(pte_t), ptep); /* get odd pte */
  987. if (r45k_bvahwbug())
  988. build_tlb_probe_entry(p);
  989. i_SRL(p, tmp, tmp, 6); /* convert to entrylo0 */
  990. if (r4k_250MHZhwbug())
  991. i_mtc0(p, 0, C0_ENTRYLO0);
  992. i_mtc0(p, tmp, C0_ENTRYLO0); /* load it */
  993. i_SRL(p, ptep, ptep, 6); /* convert to entrylo1 */
  994. if (r45k_bvahwbug())
  995. i_mfc0(p, tmp, C0_INDEX);
  996. if (r4k_250MHZhwbug())
  997. i_mtc0(p, 0, C0_ENTRYLO1);
  998. i_mtc0(p, ptep, C0_ENTRYLO1); /* load it */
  999. #endif
  1000. }
  1001. static void __init build_r4000_tlb_refill_handler(void)
  1002. {
  1003. u32 *p = tlb_handler;
  1004. struct label *l = labels;
  1005. struct reloc *r = relocs;
  1006. u32 *f;
  1007. unsigned int final_len;
  1008. memset(tlb_handler, 0, sizeof(tlb_handler));
  1009. memset(labels, 0, sizeof(labels));
  1010. memset(relocs, 0, sizeof(relocs));
  1011. memset(final_handler, 0, sizeof(final_handler));
  1012. /*
  1013. * create the plain linear handler
  1014. */
  1015. if (bcm1250_m3_war()) {
  1016. i_MFC0(&p, K0, C0_BADVADDR);
  1017. i_MFC0(&p, K1, C0_ENTRYHI);
  1018. i_xor(&p, K0, K0, K1);
  1019. i_SRL(&p, K0, K0, PAGE_SHIFT + 1);
  1020. il_bnez(&p, &r, K0, label_leave);
  1021. /* No need for i_nop */
  1022. }
  1023. #ifdef CONFIG_64BIT
  1024. build_get_pmde64(&p, &l, &r, K0, K1); /* get pmd in K1 */
  1025. #else
  1026. build_get_pgde32(&p, K0, K1); /* get pgd in K1 */
  1027. #endif
  1028. build_get_ptep(&p, K0, K1);
  1029. build_update_entries(&p, K0, K1);
  1030. build_tlb_write_entry(&p, &l, &r, tlb_random);
  1031. l_leave(&l, p);
  1032. i_eret(&p); /* return from trap */
  1033. #ifdef CONFIG_64BIT
  1034. build_get_pgd_vmalloc64(&p, &l, &r, K0, K1);
  1035. #endif
  1036. /*
  1037. * Overflow check: For the 64bit handler, we need at least one
  1038. * free instruction slot for the wrap-around branch. In worst
  1039. * case, if the intended insertion point is a delay slot, we
  1040. * need three, with the the second nop'ed and the third being
  1041. * unused.
  1042. */
  1043. #ifdef CONFIG_32BIT
  1044. if ((p - tlb_handler) > 64)
  1045. panic("TLB refill handler space exceeded");
  1046. #else
  1047. if (((p - tlb_handler) > 63)
  1048. || (((p - tlb_handler) > 61)
  1049. && insn_has_bdelay(relocs, tlb_handler + 29)))
  1050. panic("TLB refill handler space exceeded");
  1051. #endif
  1052. /*
  1053. * Now fold the handler in the TLB refill handler space.
  1054. */
  1055. #ifdef CONFIG_32BIT
  1056. f = final_handler;
  1057. /* Simplest case, just copy the handler. */
  1058. copy_handler(relocs, labels, tlb_handler, p, f);
  1059. final_len = p - tlb_handler;
  1060. #else /* CONFIG_64BIT */
  1061. f = final_handler + 32;
  1062. if ((p - tlb_handler) <= 32) {
  1063. /* Just copy the handler. */
  1064. copy_handler(relocs, labels, tlb_handler, p, f);
  1065. final_len = p - tlb_handler;
  1066. } else {
  1067. u32 *split = tlb_handler + 30;
  1068. /*
  1069. * Find the split point.
  1070. */
  1071. if (insn_has_bdelay(relocs, split - 1))
  1072. split--;
  1073. /* Copy first part of the handler. */
  1074. copy_handler(relocs, labels, tlb_handler, split, f);
  1075. f += split - tlb_handler;
  1076. /* Insert branch. */
  1077. l_split(&l, final_handler);
  1078. il_b(&f, &r, label_split);
  1079. if (insn_has_bdelay(relocs, split))
  1080. i_nop(&f);
  1081. else {
  1082. copy_handler(relocs, labels, split, split + 1, f);
  1083. move_labels(labels, f, f + 1, -1);
  1084. f++;
  1085. split++;
  1086. }
  1087. /* Copy the rest of the handler. */
  1088. copy_handler(relocs, labels, split, p, final_handler);
  1089. final_len = (f - (final_handler + 32)) + (p - split);
  1090. }
  1091. #endif /* CONFIG_64BIT */
  1092. resolve_relocs(relocs, labels);
  1093. printk("Synthesized TLB refill handler (%u instructions).\n",
  1094. final_len);
  1095. #ifdef DEBUG_TLB
  1096. {
  1097. int i;
  1098. for (i = 0; i < 64; i++)
  1099. printk("%08x\n", final_handler[i]);
  1100. }
  1101. #endif
  1102. memcpy((void *)CAC_BASE, final_handler, 0x100);
  1103. flush_icache_range(CAC_BASE, CAC_BASE + 0x100);
  1104. }
  1105. /*
  1106. * TLB load/store/modify handlers.
  1107. *
  1108. * Only the fastpath gets synthesized at runtime, the slowpath for
  1109. * do_page_fault remains normal asm.
  1110. */
  1111. extern void tlb_do_page_fault_0(void);
  1112. extern void tlb_do_page_fault_1(void);
  1113. #define __tlb_handler_align \
  1114. __attribute__((__aligned__(1 << CONFIG_MIPS_L1_CACHE_SHIFT)))
  1115. /*
  1116. * 128 instructions for the fastpath handler is generous and should
  1117. * never be exceeded.
  1118. */
  1119. #define FASTPATH_SIZE 128
  1120. u32 __tlb_handler_align handle_tlbl[FASTPATH_SIZE];
  1121. u32 __tlb_handler_align handle_tlbs[FASTPATH_SIZE];
  1122. u32 __tlb_handler_align handle_tlbm[FASTPATH_SIZE];
  1123. static void __init
  1124. iPTE_LW(u32 **p, struct label **l, unsigned int pte, int offset,
  1125. unsigned int ptr)
  1126. {
  1127. #ifdef CONFIG_SMP
  1128. # ifdef CONFIG_64BIT_PHYS_ADDR
  1129. if (cpu_has_64bits)
  1130. i_lld(p, pte, offset, ptr);
  1131. else
  1132. # endif
  1133. i_LL(p, pte, offset, ptr);
  1134. #else
  1135. # ifdef CONFIG_64BIT_PHYS_ADDR
  1136. if (cpu_has_64bits)
  1137. i_ld(p, pte, offset, ptr);
  1138. else
  1139. # endif
  1140. i_LW(p, pte, offset, ptr);
  1141. #endif
  1142. }
  1143. static void __init
  1144. iPTE_SW(u32 **p, struct reloc **r, unsigned int pte, int offset,
  1145. unsigned int ptr)
  1146. {
  1147. #ifdef CONFIG_SMP
  1148. # ifdef CONFIG_64BIT_PHYS_ADDR
  1149. if (cpu_has_64bits)
  1150. i_scd(p, pte, offset, ptr);
  1151. else
  1152. # endif
  1153. i_SC(p, pte, offset, ptr);
  1154. if (r10000_llsc_war())
  1155. il_beqzl(p, r, pte, label_smp_pgtable_change);
  1156. else
  1157. il_beqz(p, r, pte, label_smp_pgtable_change);
  1158. # ifdef CONFIG_64BIT_PHYS_ADDR
  1159. if (!cpu_has_64bits) {
  1160. /* no i_nop needed */
  1161. i_ll(p, pte, sizeof(pte_t) / 2, ptr);
  1162. i_ori(p, pte, pte, _PAGE_VALID);
  1163. i_sc(p, pte, sizeof(pte_t) / 2, ptr);
  1164. il_beqz(p, r, pte, label_smp_pgtable_change);
  1165. /* no i_nop needed */
  1166. i_lw(p, pte, 0, ptr);
  1167. } else
  1168. i_nop(p);
  1169. # else
  1170. i_nop(p);
  1171. # endif
  1172. #else
  1173. # ifdef CONFIG_64BIT_PHYS_ADDR
  1174. if (cpu_has_64bits)
  1175. i_sd(p, pte, offset, ptr);
  1176. else
  1177. # endif
  1178. i_SW(p, pte, offset, ptr);
  1179. # ifdef CONFIG_64BIT_PHYS_ADDR
  1180. if (!cpu_has_64bits) {
  1181. i_lw(p, pte, sizeof(pte_t) / 2, ptr);
  1182. i_ori(p, pte, pte, _PAGE_VALID);
  1183. i_sw(p, pte, sizeof(pte_t) / 2, ptr);
  1184. i_lw(p, pte, 0, ptr);
  1185. }
  1186. # endif
  1187. #endif
  1188. }
  1189. /*
  1190. * Check if PTE is present, if not then jump to LABEL. PTR points to
  1191. * the page table where this PTE is located, PTE will be re-loaded
  1192. * with it's original value.
  1193. */
  1194. static void __init
  1195. build_pte_present(u32 **p, struct label **l, struct reloc **r,
  1196. unsigned int pte, unsigned int ptr, enum label_id lid)
  1197. {
  1198. i_andi(p, pte, pte, _PAGE_PRESENT | _PAGE_READ);
  1199. i_xori(p, pte, pte, _PAGE_PRESENT | _PAGE_READ);
  1200. il_bnez(p, r, pte, lid);
  1201. iPTE_LW(p, l, pte, 0, ptr);
  1202. }
  1203. /* Make PTE valid, store result in PTR. */
  1204. static void __init
  1205. build_make_valid(u32 **p, struct reloc **r, unsigned int pte,
  1206. unsigned int ptr)
  1207. {
  1208. i_ori(p, pte, pte, _PAGE_VALID | _PAGE_ACCESSED);
  1209. iPTE_SW(p, r, pte, 0, ptr);
  1210. }
  1211. /*
  1212. * Check if PTE can be written to, if not branch to LABEL. Regardless
  1213. * restore PTE with value from PTR when done.
  1214. */
  1215. static void __init
  1216. build_pte_writable(u32 **p, struct label **l, struct reloc **r,
  1217. unsigned int pte, unsigned int ptr, enum label_id lid)
  1218. {
  1219. i_andi(p, pte, pte, _PAGE_PRESENT | _PAGE_WRITE);
  1220. i_xori(p, pte, pte, _PAGE_PRESENT | _PAGE_WRITE);
  1221. il_bnez(p, r, pte, lid);
  1222. iPTE_LW(p, l, pte, 0, ptr);
  1223. }
  1224. /* Make PTE writable, update software status bits as well, then store
  1225. * at PTR.
  1226. */
  1227. static void __init
  1228. build_make_write(u32 **p, struct reloc **r, unsigned int pte,
  1229. unsigned int ptr)
  1230. {
  1231. i_ori(p, pte, pte,
  1232. _PAGE_ACCESSED | _PAGE_MODIFIED | _PAGE_VALID | _PAGE_DIRTY);
  1233. iPTE_SW(p, r, pte, 0, ptr);
  1234. }
  1235. /*
  1236. * Check if PTE can be modified, if not branch to LABEL. Regardless
  1237. * restore PTE with value from PTR when done.
  1238. */
  1239. static void __init
  1240. build_pte_modifiable(u32 **p, struct label **l, struct reloc **r,
  1241. unsigned int pte, unsigned int ptr, enum label_id lid)
  1242. {
  1243. i_andi(p, pte, pte, _PAGE_WRITE);
  1244. il_beqz(p, r, pte, lid);
  1245. iPTE_LW(p, l, pte, 0, ptr);
  1246. }
  1247. /*
  1248. * R3000 style TLB load/store/modify handlers.
  1249. */
  1250. /* This places the pte in the page table at PTR into ENTRYLO0. */
  1251. static void __init
  1252. build_r3000_pte_reload(u32 **p, unsigned int ptr)
  1253. {
  1254. i_lw(p, ptr, 0, ptr);
  1255. i_nop(p); /* load delay */
  1256. i_mtc0(p, ptr, C0_ENTRYLO0);
  1257. i_nop(p); /* cp0 delay */
  1258. }
  1259. /*
  1260. * The index register may have the probe fail bit set,
  1261. * because we would trap on access kseg2, i.e. without refill.
  1262. */
  1263. static void __init
  1264. build_r3000_tlb_write(u32 **p, struct label **l, struct reloc **r,
  1265. unsigned int tmp)
  1266. {
  1267. i_mfc0(p, tmp, C0_INDEX);
  1268. i_nop(p); /* cp0 delay */
  1269. il_bltz(p, r, tmp, label_r3000_write_probe_fail);
  1270. i_nop(p); /* branch delay */
  1271. i_tlbwi(p);
  1272. il_b(p, r, label_r3000_write_probe_ok);
  1273. i_nop(p); /* branch delay */
  1274. l_r3000_write_probe_fail(l, *p);
  1275. i_tlbwr(p);
  1276. l_r3000_write_probe_ok(l, *p);
  1277. }
  1278. static void __init
  1279. build_r3000_tlbchange_handler_head(u32 **p, unsigned int pte,
  1280. unsigned int ptr)
  1281. {
  1282. long pgdc = (long)pgd_current;
  1283. i_mfc0(p, pte, C0_BADVADDR);
  1284. i_lui(p, ptr, rel_hi(pgdc)); /* cp0 delay */
  1285. i_lw(p, ptr, rel_lo(pgdc), ptr);
  1286. i_srl(p, pte, pte, 22); /* load delay */
  1287. i_sll(p, pte, pte, 2);
  1288. i_addu(p, ptr, ptr, pte);
  1289. i_mfc0(p, pte, C0_CONTEXT);
  1290. i_lw(p, ptr, 0, ptr); /* cp0 delay */
  1291. i_andi(p, pte, pte, 0xffc); /* load delay */
  1292. i_addu(p, ptr, ptr, pte);
  1293. i_lw(p, pte, 0, ptr);
  1294. i_nop(p); /* load delay */
  1295. i_tlbp(p);
  1296. }
  1297. static void __init
  1298. build_r3000_tlbchange_handler_tail(u32 **p, unsigned int tmp)
  1299. {
  1300. i_mfc0(p, tmp, C0_EPC);
  1301. i_nop(p); /* cp0 delay */
  1302. i_jr(p, tmp);
  1303. i_rfe(p); /* branch delay */
  1304. }
  1305. static void __init build_r3000_tlb_load_handler(void)
  1306. {
  1307. u32 *p = handle_tlbl;
  1308. struct label *l = labels;
  1309. struct reloc *r = relocs;
  1310. memset(handle_tlbl, 0, sizeof(handle_tlbl));
  1311. memset(labels, 0, sizeof(labels));
  1312. memset(relocs, 0, sizeof(relocs));
  1313. build_r3000_tlbchange_handler_head(&p, K0, K1);
  1314. build_pte_present(&p, &l, &r, K0, K1, label_nopage_tlbl);
  1315. build_make_valid(&p, &r, K0, K1);
  1316. build_r3000_pte_reload(&p, K1);
  1317. build_r3000_tlb_write(&p, &l, &r, K0);
  1318. build_r3000_tlbchange_handler_tail(&p, K0);
  1319. l_nopage_tlbl(&l, p);
  1320. i_j(&p, (unsigned long)tlb_do_page_fault_0 & 0x0fffffff);
  1321. i_nop(&p);
  1322. if ((p - handle_tlbl) > FASTPATH_SIZE)
  1323. panic("TLB load handler fastpath space exceeded");
  1324. resolve_relocs(relocs, labels);
  1325. printk("Synthesized TLB load handler fastpath (%u instructions).\n",
  1326. (unsigned int)(p - handle_tlbl));
  1327. #ifdef DEBUG_TLB
  1328. {
  1329. int i;
  1330. for (i = 0; i < FASTPATH_SIZE; i++)
  1331. printk("%08x\n", handle_tlbl[i]);
  1332. }
  1333. #endif
  1334. flush_icache_range((unsigned long)handle_tlbl,
  1335. (unsigned long)handle_tlbl + FASTPATH_SIZE * sizeof(u32));
  1336. }
  1337. static void __init build_r3000_tlb_store_handler(void)
  1338. {
  1339. u32 *p = handle_tlbs;
  1340. struct label *l = labels;
  1341. struct reloc *r = relocs;
  1342. memset(handle_tlbs, 0, sizeof(handle_tlbs));
  1343. memset(labels, 0, sizeof(labels));
  1344. memset(relocs, 0, sizeof(relocs));
  1345. build_r3000_tlbchange_handler_head(&p, K0, K1);
  1346. build_pte_writable(&p, &l, &r, K0, K1, label_nopage_tlbs);
  1347. build_make_write(&p, &r, K0, K1);
  1348. build_r3000_pte_reload(&p, K1);
  1349. build_r3000_tlb_write(&p, &l, &r, K0);
  1350. build_r3000_tlbchange_handler_tail(&p, K0);
  1351. l_nopage_tlbs(&l, p);
  1352. i_j(&p, (unsigned long)tlb_do_page_fault_1 & 0x0fffffff);
  1353. i_nop(&p);
  1354. if ((p - handle_tlbs) > FASTPATH_SIZE)
  1355. panic("TLB store handler fastpath space exceeded");
  1356. resolve_relocs(relocs, labels);
  1357. printk("Synthesized TLB store handler fastpath (%u instructions).\n",
  1358. (unsigned int)(p - handle_tlbs));
  1359. #ifdef DEBUG_TLB
  1360. {
  1361. int i;
  1362. for (i = 0; i < FASTPATH_SIZE; i++)
  1363. printk("%08x\n", handle_tlbs[i]);
  1364. }
  1365. #endif
  1366. flush_icache_range((unsigned long)handle_tlbs,
  1367. (unsigned long)handle_tlbs + FASTPATH_SIZE * sizeof(u32));
  1368. }
  1369. static void __init build_r3000_tlb_modify_handler(void)
  1370. {
  1371. u32 *p = handle_tlbm;
  1372. struct label *l = labels;
  1373. struct reloc *r = relocs;
  1374. memset(handle_tlbm, 0, sizeof(handle_tlbm));
  1375. memset(labels, 0, sizeof(labels));
  1376. memset(relocs, 0, sizeof(relocs));
  1377. build_r3000_tlbchange_handler_head(&p, K0, K1);
  1378. build_pte_modifiable(&p, &l, &r, K0, K1, label_nopage_tlbm);
  1379. build_make_write(&p, &r, K0, K1);
  1380. build_r3000_pte_reload(&p, K1);
  1381. i_tlbwi(&p);
  1382. build_r3000_tlbchange_handler_tail(&p, K0);
  1383. l_nopage_tlbm(&l, p);
  1384. i_j(&p, (unsigned long)tlb_do_page_fault_1 & 0x0fffffff);
  1385. i_nop(&p);
  1386. if ((p - handle_tlbm) > FASTPATH_SIZE)
  1387. panic("TLB modify handler fastpath space exceeded");
  1388. resolve_relocs(relocs, labels);
  1389. printk("Synthesized TLB modify handler fastpath (%u instructions).\n",
  1390. (unsigned int)(p - handle_tlbm));
  1391. #ifdef DEBUG_TLB
  1392. {
  1393. int i;
  1394. for (i = 0; i < FASTPATH_SIZE; i++)
  1395. printk("%08x\n", handle_tlbm[i]);
  1396. }
  1397. #endif
  1398. flush_icache_range((unsigned long)handle_tlbm,
  1399. (unsigned long)handle_tlbm + FASTPATH_SIZE * sizeof(u32));
  1400. }
  1401. /*
  1402. * R4000 style TLB load/store/modify handlers.
  1403. */
  1404. static void __init
  1405. build_r4000_tlbchange_handler_head(u32 **p, struct label **l,
  1406. struct reloc **r, unsigned int pte,
  1407. unsigned int ptr)
  1408. {
  1409. #ifdef CONFIG_64BIT
  1410. build_get_pmde64(p, l, r, pte, ptr); /* get pmd in ptr */
  1411. #else
  1412. build_get_pgde32(p, pte, ptr); /* get pgd in ptr */
  1413. #endif
  1414. i_MFC0(p, pte, C0_BADVADDR);
  1415. i_LW(p, ptr, 0, ptr);
  1416. i_SRL(p, pte, pte, PAGE_SHIFT + PTE_ORDER - PTE_T_LOG2);
  1417. i_andi(p, pte, pte, (PTRS_PER_PTE - 1) << PTE_T_LOG2);
  1418. i_ADDU(p, ptr, ptr, pte);
  1419. #ifdef CONFIG_SMP
  1420. l_smp_pgtable_change(l, *p);
  1421. # endif
  1422. iPTE_LW(p, l, pte, 0, ptr); /* get even pte */
  1423. build_tlb_probe_entry(p);
  1424. }
  1425. static void __init
  1426. build_r4000_tlbchange_handler_tail(u32 **p, struct label **l,
  1427. struct reloc **r, unsigned int tmp,
  1428. unsigned int ptr)
  1429. {
  1430. i_ori(p, ptr, ptr, sizeof(pte_t));
  1431. i_xori(p, ptr, ptr, sizeof(pte_t));
  1432. build_update_entries(p, tmp, ptr);
  1433. build_tlb_write_entry(p, l, r, tlb_indexed);
  1434. l_leave(l, *p);
  1435. i_eret(p); /* return from trap */
  1436. #ifdef CONFIG_64BIT
  1437. build_get_pgd_vmalloc64(p, l, r, tmp, ptr);
  1438. #endif
  1439. }
  1440. static void __init build_r4000_tlb_load_handler(void)
  1441. {
  1442. u32 *p = handle_tlbl;
  1443. struct label *l = labels;
  1444. struct reloc *r = relocs;
  1445. memset(handle_tlbl, 0, sizeof(handle_tlbl));
  1446. memset(labels, 0, sizeof(labels));
  1447. memset(relocs, 0, sizeof(relocs));
  1448. if (bcm1250_m3_war()) {
  1449. i_MFC0(&p, K0, C0_BADVADDR);
  1450. i_MFC0(&p, K1, C0_ENTRYHI);
  1451. i_xor(&p, K0, K0, K1);
  1452. i_SRL(&p, K0, K0, PAGE_SHIFT + 1);
  1453. il_bnez(&p, &r, K0, label_leave);
  1454. /* No need for i_nop */
  1455. }
  1456. build_r4000_tlbchange_handler_head(&p, &l, &r, K0, K1);
  1457. build_pte_present(&p, &l, &r, K0, K1, label_nopage_tlbl);
  1458. build_make_valid(&p, &r, K0, K1);
  1459. build_r4000_tlbchange_handler_tail(&p, &l, &r, K0, K1);
  1460. l_nopage_tlbl(&l, p);
  1461. i_j(&p, (unsigned long)tlb_do_page_fault_0 & 0x0fffffff);
  1462. i_nop(&p);
  1463. if ((p - handle_tlbl) > FASTPATH_SIZE)
  1464. panic("TLB load handler fastpath space exceeded");
  1465. resolve_relocs(relocs, labels);
  1466. printk("Synthesized TLB load handler fastpath (%u instructions).\n",
  1467. (unsigned int)(p - handle_tlbl));
  1468. #ifdef DEBUG_TLB
  1469. {
  1470. int i;
  1471. for (i = 0; i < FASTPATH_SIZE; i++)
  1472. printk("%08x\n", handle_tlbl[i]);
  1473. }
  1474. #endif
  1475. flush_icache_range((unsigned long)handle_tlbl,
  1476. (unsigned long)handle_tlbl + FASTPATH_SIZE * sizeof(u32));
  1477. }
  1478. static void __init build_r4000_tlb_store_handler(void)
  1479. {
  1480. u32 *p = handle_tlbs;
  1481. struct label *l = labels;
  1482. struct reloc *r = relocs;
  1483. memset(handle_tlbs, 0, sizeof(handle_tlbs));
  1484. memset(labels, 0, sizeof(labels));
  1485. memset(relocs, 0, sizeof(relocs));
  1486. build_r4000_tlbchange_handler_head(&p, &l, &r, K0, K1);
  1487. build_pte_writable(&p, &l, &r, K0, K1, label_nopage_tlbs);
  1488. build_make_write(&p, &r, K0, K1);
  1489. build_r4000_tlbchange_handler_tail(&p, &l, &r, K0, K1);
  1490. l_nopage_tlbs(&l, p);
  1491. i_j(&p, (unsigned long)tlb_do_page_fault_1 & 0x0fffffff);
  1492. i_nop(&p);
  1493. if ((p - handle_tlbs) > FASTPATH_SIZE)
  1494. panic("TLB store handler fastpath space exceeded");
  1495. resolve_relocs(relocs, labels);
  1496. printk("Synthesized TLB store handler fastpath (%u instructions).\n",
  1497. (unsigned int)(p - handle_tlbs));
  1498. #ifdef DEBUG_TLB
  1499. {
  1500. int i;
  1501. for (i = 0; i < FASTPATH_SIZE; i++)
  1502. printk("%08x\n", handle_tlbs[i]);
  1503. }
  1504. #endif
  1505. flush_icache_range((unsigned long)handle_tlbs,
  1506. (unsigned long)handle_tlbs + FASTPATH_SIZE * sizeof(u32));
  1507. }
  1508. static void __init build_r4000_tlb_modify_handler(void)
  1509. {
  1510. u32 *p = handle_tlbm;
  1511. struct label *l = labels;
  1512. struct reloc *r = relocs;
  1513. memset(handle_tlbm, 0, sizeof(handle_tlbm));
  1514. memset(labels, 0, sizeof(labels));
  1515. memset(relocs, 0, sizeof(relocs));
  1516. build_r4000_tlbchange_handler_head(&p, &l, &r, K0, K1);
  1517. build_pte_modifiable(&p, &l, &r, K0, K1, label_nopage_tlbm);
  1518. /* Present and writable bits set, set accessed and dirty bits. */
  1519. build_make_write(&p, &r, K0, K1);
  1520. build_r4000_tlbchange_handler_tail(&p, &l, &r, K0, K1);
  1521. l_nopage_tlbm(&l, p);
  1522. i_j(&p, (unsigned long)tlb_do_page_fault_1 & 0x0fffffff);
  1523. i_nop(&p);
  1524. if ((p - handle_tlbm) > FASTPATH_SIZE)
  1525. panic("TLB modify handler fastpath space exceeded");
  1526. resolve_relocs(relocs, labels);
  1527. printk("Synthesized TLB modify handler fastpath (%u instructions).\n",
  1528. (unsigned int)(p - handle_tlbm));
  1529. #ifdef DEBUG_TLB
  1530. {
  1531. int i;
  1532. for (i = 0; i < FASTPATH_SIZE; i++)
  1533. printk("%08x\n", handle_tlbm[i]);
  1534. }
  1535. #endif
  1536. flush_icache_range((unsigned long)handle_tlbm,
  1537. (unsigned long)handle_tlbm + FASTPATH_SIZE * sizeof(u32));
  1538. }
  1539. void __init build_tlb_refill_handler(void)
  1540. {
  1541. /*
  1542. * The refill handler is generated per-CPU, multi-node systems
  1543. * may have local storage for it. The other handlers are only
  1544. * needed once.
  1545. */
  1546. static int run_once = 0;
  1547. switch (current_cpu_data.cputype) {
  1548. case CPU_R2000:
  1549. case CPU_R3000:
  1550. case CPU_R3000A:
  1551. case CPU_R3081E:
  1552. case CPU_TX3912:
  1553. case CPU_TX3922:
  1554. case CPU_TX3927:
  1555. build_r3000_tlb_refill_handler();
  1556. if (!run_once) {
  1557. build_r3000_tlb_load_handler();
  1558. build_r3000_tlb_store_handler();
  1559. build_r3000_tlb_modify_handler();
  1560. run_once++;
  1561. }
  1562. break;
  1563. case CPU_R6000:
  1564. case CPU_R6000A:
  1565. panic("No R6000 TLB refill handler yet");
  1566. break;
  1567. case CPU_R8000:
  1568. panic("No R8000 TLB refill handler yet");
  1569. break;
  1570. default:
  1571. build_r4000_tlb_refill_handler();
  1572. if (!run_once) {
  1573. build_r4000_tlb_load_handler();
  1574. build_r4000_tlb_store_handler();
  1575. build_r4000_tlb_modify_handler();
  1576. run_once++;
  1577. }
  1578. }
  1579. }