tlbex.c 49 KB

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