tlbex.c 44 KB

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