tlbex.c 46 KB

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