tlbex.c 48 KB

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