tlbex.c 48 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918
  1. /*
  2. * This file is subject to the terms and conditions of the GNU General Public
  3. * License. See the file "COPYING" in the main directory of this archive
  4. * for more details.
  5. *
  6. * Synthesize TLB refill handlers at runtime.
  7. *
  8. * Copyright (C) 2004,2005,2006 by Thiemo Seufer
  9. * Copyright (C) 2005 Maciej W. Rozycki
  10. * Copyright (C) 2006 Ralf Baechle (ralf@linux-mips.org)
  11. *
  12. * ... and the days got worse and worse and now you see
  13. * I've gone completly out of my mind.
  14. *
  15. * They're coming to take me a away haha
  16. * they're coming to take me a away hoho hihi haha
  17. * to the funny farm where code is beautiful all the time ...
  18. *
  19. * (Condolences to Napoleon XIV)
  20. */
  21. #include <stdarg.h>
  22. #include <linux/mm.h>
  23. #include <linux/kernel.h>
  24. #include <linux/types.h>
  25. #include <linux/string.h>
  26. #include <linux/init.h>
  27. #include <asm/pgtable.h>
  28. #include <asm/cacheflush.h>
  29. #include <asm/mmu_context.h>
  30. #include <asm/inst.h>
  31. #include <asm/elf.h>
  32. #include <asm/smp.h>
  33. #include <asm/war.h>
  34. static __init int __maybe_unused r45k_bvahwbug(void)
  35. {
  36. /* XXX: We should probe for the presence of this bug, but we don't. */
  37. return 0;
  38. }
  39. static __init int __maybe_unused r4k_250MHZhwbug(void)
  40. {
  41. /* XXX: We should probe for the presence of this bug, but we don't. */
  42. return 0;
  43. }
  44. static __init int __maybe_unused bcm1250_m3_war(void)
  45. {
  46. return BCM1250_M3_WAR;
  47. }
  48. static __init 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_data.cputype) {
  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_data.cputype) {
  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_LOONGSON2:
  800. if (m4kc_tlbp_war())
  801. i_nop(p);
  802. tlbw(p);
  803. break;
  804. case CPU_NEVADA:
  805. i_nop(p); /* QED specifies 2 nops hazard */
  806. /*
  807. * This branch uses up a mtc0 hazard nop slot and saves
  808. * a nop after the tlbw instruction.
  809. */
  810. il_bgezl(p, r, 0, label_tlbw_hazard);
  811. tlbw(p);
  812. l_tlbw_hazard(l, *p);
  813. break;
  814. case CPU_RM7000:
  815. i_nop(p);
  816. i_nop(p);
  817. i_nop(p);
  818. i_nop(p);
  819. tlbw(p);
  820. break;
  821. case CPU_4KEC:
  822. case CPU_24K:
  823. case CPU_34K:
  824. case CPU_74K:
  825. i_ehb(p);
  826. tlbw(p);
  827. break;
  828. case CPU_RM9000:
  829. /*
  830. * When the JTLB is updated by tlbwi or tlbwr, a subsequent
  831. * use of the JTLB for instructions should not occur for 4
  832. * cpu cycles and use for data translations should not occur
  833. * for 3 cpu cycles.
  834. */
  835. i_ssnop(p);
  836. i_ssnop(p);
  837. i_ssnop(p);
  838. i_ssnop(p);
  839. tlbw(p);
  840. i_ssnop(p);
  841. i_ssnop(p);
  842. i_ssnop(p);
  843. i_ssnop(p);
  844. break;
  845. case CPU_VR4111:
  846. case CPU_VR4121:
  847. case CPU_VR4122:
  848. case CPU_VR4181:
  849. case CPU_VR4181A:
  850. i_nop(p);
  851. i_nop(p);
  852. tlbw(p);
  853. i_nop(p);
  854. i_nop(p);
  855. break;
  856. case CPU_VR4131:
  857. case CPU_VR4133:
  858. case CPU_R5432:
  859. i_nop(p);
  860. i_nop(p);
  861. tlbw(p);
  862. break;
  863. default:
  864. panic("No TLB refill handler yet (CPU type: %d)",
  865. current_cpu_data.cputype);
  866. break;
  867. }
  868. }
  869. #ifdef CONFIG_64BIT
  870. /*
  871. * TMP and PTR are scratch.
  872. * TMP will be clobbered, PTR will hold the pmd entry.
  873. */
  874. static __init void
  875. build_get_pmde64(u32 **p, struct label **l, struct reloc **r,
  876. unsigned int tmp, unsigned int ptr)
  877. {
  878. long pgdc = (long)pgd_current;
  879. /*
  880. * The vmalloc handling is not in the hotpath.
  881. */
  882. i_dmfc0(p, tmp, C0_BADVADDR);
  883. #ifdef MODULE_START
  884. il_bltz(p, r, tmp, label_module_alloc);
  885. #else
  886. il_bltz(p, r, tmp, label_vmalloc);
  887. #endif
  888. /* No i_nop needed here, since the next insn doesn't touch TMP. */
  889. #ifdef CONFIG_SMP
  890. # ifdef CONFIG_MIPS_MT_SMTC
  891. /*
  892. * SMTC uses TCBind value as "CPU" index
  893. */
  894. i_mfc0(p, ptr, C0_TCBIND);
  895. i_dsrl(p, ptr, ptr, 19);
  896. # else
  897. /*
  898. * 64 bit SMP running in XKPHYS has smp_processor_id() << 3
  899. * stored in CONTEXT.
  900. */
  901. i_dmfc0(p, ptr, C0_CONTEXT);
  902. i_dsrl(p, ptr, ptr, 23);
  903. #endif
  904. i_LA_mostly(p, tmp, pgdc);
  905. i_daddu(p, ptr, ptr, tmp);
  906. i_dmfc0(p, tmp, C0_BADVADDR);
  907. i_ld(p, ptr, rel_lo(pgdc), ptr);
  908. #else
  909. i_LA_mostly(p, ptr, pgdc);
  910. i_ld(p, ptr, rel_lo(pgdc), ptr);
  911. #endif
  912. l_vmalloc_done(l, *p);
  913. if (PGDIR_SHIFT - 3 < 32) /* get pgd offset in bytes */
  914. i_dsrl(p, tmp, tmp, PGDIR_SHIFT-3);
  915. else
  916. i_dsrl32(p, tmp, tmp, PGDIR_SHIFT - 3 - 32);
  917. i_andi(p, tmp, tmp, (PTRS_PER_PGD - 1)<<3);
  918. i_daddu(p, ptr, ptr, tmp); /* add in pgd offset */
  919. i_dmfc0(p, tmp, C0_BADVADDR); /* get faulting address */
  920. i_ld(p, ptr, 0, ptr); /* get pmd pointer */
  921. i_dsrl(p, tmp, tmp, PMD_SHIFT-3); /* get pmd offset in bytes */
  922. i_andi(p, tmp, tmp, (PTRS_PER_PMD - 1)<<3);
  923. i_daddu(p, ptr, ptr, tmp); /* add in pmd offset */
  924. }
  925. /*
  926. * BVADDR is the faulting address, PTR is scratch.
  927. * PTR will hold the pgd for vmalloc.
  928. */
  929. static __init void
  930. build_get_pgd_vmalloc64(u32 **p, struct label **l, struct reloc **r,
  931. unsigned int bvaddr, unsigned int ptr)
  932. {
  933. long swpd = (long)swapper_pg_dir;
  934. #ifdef MODULE_START
  935. long modd = (long)module_pg_dir;
  936. l_module_alloc(l, *p);
  937. /*
  938. * Assumption:
  939. * VMALLOC_START >= 0xc000000000000000UL
  940. * MODULE_START >= 0xe000000000000000UL
  941. */
  942. i_SLL(p, ptr, bvaddr, 2);
  943. il_bgez(p, r, ptr, label_vmalloc);
  944. if (in_compat_space_p(MODULE_START) && !rel_lo(MODULE_START)) {
  945. i_lui(p, ptr, rel_hi(MODULE_START)); /* delay slot */
  946. } else {
  947. /* unlikely configuration */
  948. i_nop(p); /* delay slot */
  949. i_LA(p, ptr, MODULE_START);
  950. }
  951. i_dsubu(p, bvaddr, bvaddr, ptr);
  952. if (in_compat_space_p(modd) && !rel_lo(modd)) {
  953. il_b(p, r, label_vmalloc_done);
  954. i_lui(p, ptr, rel_hi(modd));
  955. } else {
  956. i_LA_mostly(p, ptr, modd);
  957. il_b(p, r, label_vmalloc_done);
  958. i_daddiu(p, ptr, ptr, rel_lo(modd));
  959. }
  960. l_vmalloc(l, *p);
  961. if (in_compat_space_p(MODULE_START) && !rel_lo(MODULE_START) &&
  962. MODULE_START << 32 == VMALLOC_START)
  963. i_dsll32(p, ptr, ptr, 0); /* typical case */
  964. else
  965. i_LA(p, ptr, VMALLOC_START);
  966. #else
  967. l_vmalloc(l, *p);
  968. i_LA(p, ptr, VMALLOC_START);
  969. #endif
  970. i_dsubu(p, bvaddr, bvaddr, ptr);
  971. if (in_compat_space_p(swpd) && !rel_lo(swpd)) {
  972. il_b(p, r, label_vmalloc_done);
  973. i_lui(p, ptr, rel_hi(swpd));
  974. } else {
  975. i_LA_mostly(p, ptr, swpd);
  976. il_b(p, r, label_vmalloc_done);
  977. i_daddiu(p, ptr, ptr, rel_lo(swpd));
  978. }
  979. }
  980. #else /* !CONFIG_64BIT */
  981. /*
  982. * TMP and PTR are scratch.
  983. * TMP will be clobbered, PTR will hold the pgd entry.
  984. */
  985. static __init void __maybe_unused
  986. build_get_pgde32(u32 **p, unsigned int tmp, unsigned int ptr)
  987. {
  988. long pgdc = (long)pgd_current;
  989. /* 32 bit SMP has smp_processor_id() stored in CONTEXT. */
  990. #ifdef CONFIG_SMP
  991. #ifdef CONFIG_MIPS_MT_SMTC
  992. /*
  993. * SMTC uses TCBind value as "CPU" index
  994. */
  995. i_mfc0(p, ptr, C0_TCBIND);
  996. i_LA_mostly(p, tmp, pgdc);
  997. i_srl(p, ptr, ptr, 19);
  998. #else
  999. /*
  1000. * smp_processor_id() << 3 is stored in CONTEXT.
  1001. */
  1002. i_mfc0(p, ptr, C0_CONTEXT);
  1003. i_LA_mostly(p, tmp, pgdc);
  1004. i_srl(p, ptr, ptr, 23);
  1005. #endif
  1006. i_addu(p, ptr, tmp, ptr);
  1007. #else
  1008. i_LA_mostly(p, ptr, pgdc);
  1009. #endif
  1010. i_mfc0(p, tmp, C0_BADVADDR); /* get faulting address */
  1011. i_lw(p, ptr, rel_lo(pgdc), ptr);
  1012. i_srl(p, tmp, tmp, PGDIR_SHIFT); /* get pgd only bits */
  1013. i_sll(p, tmp, tmp, PGD_T_LOG2);
  1014. i_addu(p, ptr, ptr, tmp); /* add in pgd offset */
  1015. }
  1016. #endif /* !CONFIG_64BIT */
  1017. static __init void build_adjust_context(u32 **p, unsigned int ctx)
  1018. {
  1019. unsigned int shift = 4 - (PTE_T_LOG2 + 1) + PAGE_SHIFT - 12;
  1020. unsigned int mask = (PTRS_PER_PTE / 2 - 1) << (PTE_T_LOG2 + 1);
  1021. switch (current_cpu_data.cputype) {
  1022. case CPU_VR41XX:
  1023. case CPU_VR4111:
  1024. case CPU_VR4121:
  1025. case CPU_VR4122:
  1026. case CPU_VR4131:
  1027. case CPU_VR4181:
  1028. case CPU_VR4181A:
  1029. case CPU_VR4133:
  1030. shift += 2;
  1031. break;
  1032. default:
  1033. break;
  1034. }
  1035. if (shift)
  1036. i_SRL(p, ctx, ctx, shift);
  1037. i_andi(p, ctx, ctx, mask);
  1038. }
  1039. static __init void build_get_ptep(u32 **p, unsigned int tmp, unsigned int ptr)
  1040. {
  1041. /*
  1042. * Bug workaround for the Nevada. It seems as if under certain
  1043. * circumstances the move from cp0_context might produce a
  1044. * bogus result when the mfc0 instruction and its consumer are
  1045. * in a different cacheline or a load instruction, probably any
  1046. * memory reference, is between them.
  1047. */
  1048. switch (current_cpu_data.cputype) {
  1049. case CPU_NEVADA:
  1050. i_LW(p, ptr, 0, ptr);
  1051. GET_CONTEXT(p, tmp); /* get context reg */
  1052. break;
  1053. default:
  1054. GET_CONTEXT(p, tmp); /* get context reg */
  1055. i_LW(p, ptr, 0, ptr);
  1056. break;
  1057. }
  1058. build_adjust_context(p, tmp);
  1059. i_ADDU(p, ptr, ptr, tmp); /* add in offset */
  1060. }
  1061. static __init void build_update_entries(u32 **p, unsigned int tmp,
  1062. unsigned int ptep)
  1063. {
  1064. /*
  1065. * 64bit address support (36bit on a 32bit CPU) in a 32bit
  1066. * Kernel is a special case. Only a few CPUs use it.
  1067. */
  1068. #ifdef CONFIG_64BIT_PHYS_ADDR
  1069. if (cpu_has_64bits) {
  1070. i_ld(p, tmp, 0, ptep); /* get even pte */
  1071. i_ld(p, ptep, sizeof(pte_t), ptep); /* get odd pte */
  1072. i_dsrl(p, tmp, tmp, 6); /* convert to entrylo0 */
  1073. i_mtc0(p, tmp, C0_ENTRYLO0); /* load it */
  1074. i_dsrl(p, ptep, ptep, 6); /* convert to entrylo1 */
  1075. i_mtc0(p, ptep, C0_ENTRYLO1); /* load it */
  1076. } else {
  1077. int pte_off_even = sizeof(pte_t) / 2;
  1078. int pte_off_odd = pte_off_even + sizeof(pte_t);
  1079. /* The pte entries are pre-shifted */
  1080. i_lw(p, tmp, pte_off_even, ptep); /* get even pte */
  1081. i_mtc0(p, tmp, C0_ENTRYLO0); /* load it */
  1082. i_lw(p, ptep, pte_off_odd, ptep); /* get odd pte */
  1083. i_mtc0(p, ptep, C0_ENTRYLO1); /* load it */
  1084. }
  1085. #else
  1086. i_LW(p, tmp, 0, ptep); /* get even pte */
  1087. i_LW(p, ptep, sizeof(pte_t), ptep); /* get odd pte */
  1088. if (r45k_bvahwbug())
  1089. build_tlb_probe_entry(p);
  1090. i_SRL(p, tmp, tmp, 6); /* convert to entrylo0 */
  1091. if (r4k_250MHZhwbug())
  1092. i_mtc0(p, 0, C0_ENTRYLO0);
  1093. i_mtc0(p, tmp, C0_ENTRYLO0); /* load it */
  1094. i_SRL(p, ptep, ptep, 6); /* convert to entrylo1 */
  1095. if (r45k_bvahwbug())
  1096. i_mfc0(p, tmp, C0_INDEX);
  1097. if (r4k_250MHZhwbug())
  1098. i_mtc0(p, 0, C0_ENTRYLO1);
  1099. i_mtc0(p, ptep, C0_ENTRYLO1); /* load it */
  1100. #endif
  1101. }
  1102. static void __init build_r4000_tlb_refill_handler(void)
  1103. {
  1104. u32 *p = tlb_handler;
  1105. struct label *l = labels;
  1106. struct reloc *r = relocs;
  1107. u32 *f;
  1108. unsigned int final_len;
  1109. int i;
  1110. memset(tlb_handler, 0, sizeof(tlb_handler));
  1111. memset(labels, 0, sizeof(labels));
  1112. memset(relocs, 0, sizeof(relocs));
  1113. memset(final_handler, 0, sizeof(final_handler));
  1114. /*
  1115. * create the plain linear handler
  1116. */
  1117. if (bcm1250_m3_war()) {
  1118. i_MFC0(&p, K0, C0_BADVADDR);
  1119. i_MFC0(&p, K1, C0_ENTRYHI);
  1120. i_xor(&p, K0, K0, K1);
  1121. i_SRL(&p, K0, K0, PAGE_SHIFT + 1);
  1122. il_bnez(&p, &r, K0, label_leave);
  1123. /* No need for i_nop */
  1124. }
  1125. #ifdef CONFIG_64BIT
  1126. build_get_pmde64(&p, &l, &r, K0, K1); /* get pmd in K1 */
  1127. #else
  1128. build_get_pgde32(&p, K0, K1); /* get pgd in K1 */
  1129. #endif
  1130. build_get_ptep(&p, K0, K1);
  1131. build_update_entries(&p, K0, K1);
  1132. build_tlb_write_entry(&p, &l, &r, tlb_random);
  1133. l_leave(&l, p);
  1134. i_eret(&p); /* return from trap */
  1135. #ifdef CONFIG_64BIT
  1136. build_get_pgd_vmalloc64(&p, &l, &r, K0, K1);
  1137. #endif
  1138. /*
  1139. * Overflow check: For the 64bit handler, we need at least one
  1140. * free instruction slot for the wrap-around branch. In worst
  1141. * case, if the intended insertion point is a delay slot, we
  1142. * need three, with the second nop'ed and the third being
  1143. * unused.
  1144. */
  1145. /* Loongson2 ebase is different than r4k, we have more space */
  1146. #if defined(CONFIG_32BIT) || defined(CONFIG_CPU_LOONGSON2)
  1147. if ((p - tlb_handler) > 64)
  1148. panic("TLB refill handler space exceeded");
  1149. #else
  1150. if (((p - tlb_handler) > 63)
  1151. || (((p - tlb_handler) > 61)
  1152. && insn_has_bdelay(relocs, tlb_handler + 29)))
  1153. panic("TLB refill handler space exceeded");
  1154. #endif
  1155. /*
  1156. * Now fold the handler in the TLB refill handler space.
  1157. */
  1158. #if defined(CONFIG_32BIT) || defined(CONFIG_CPU_LOONGSON2)
  1159. f = final_handler;
  1160. /* Simplest case, just copy the handler. */
  1161. copy_handler(relocs, labels, tlb_handler, p, f);
  1162. final_len = p - tlb_handler;
  1163. #else /* CONFIG_64BIT */
  1164. f = final_handler + 32;
  1165. if ((p - tlb_handler) <= 32) {
  1166. /* Just copy the handler. */
  1167. copy_handler(relocs, labels, tlb_handler, p, f);
  1168. final_len = p - tlb_handler;
  1169. } else {
  1170. u32 *split = tlb_handler + 30;
  1171. /*
  1172. * Find the split point.
  1173. */
  1174. if (insn_has_bdelay(relocs, split - 1))
  1175. split--;
  1176. /* Copy first part of the handler. */
  1177. copy_handler(relocs, labels, tlb_handler, split, f);
  1178. f += split - tlb_handler;
  1179. /* Insert branch. */
  1180. l_split(&l, final_handler);
  1181. il_b(&f, &r, label_split);
  1182. if (insn_has_bdelay(relocs, split))
  1183. i_nop(&f);
  1184. else {
  1185. copy_handler(relocs, labels, split, split + 1, f);
  1186. move_labels(labels, f, f + 1, -1);
  1187. f++;
  1188. split++;
  1189. }
  1190. /* Copy the rest of the handler. */
  1191. copy_handler(relocs, labels, split, p, final_handler);
  1192. final_len = (f - (final_handler + 32)) + (p - split);
  1193. }
  1194. #endif /* CONFIG_64BIT */
  1195. resolve_relocs(relocs, labels);
  1196. pr_info("Synthesized TLB refill handler (%u instructions).\n",
  1197. final_len);
  1198. f = final_handler;
  1199. #if defined(CONFIG_64BIT) && !defined(CONFIG_CPU_LOONGSON2)
  1200. if (final_len > 32)
  1201. final_len = 64;
  1202. else
  1203. f = final_handler + 32;
  1204. #endif /* CONFIG_64BIT */
  1205. pr_debug("\t.set push\n");
  1206. pr_debug("\t.set noreorder\n");
  1207. for (i = 0; i < final_len; i++)
  1208. pr_debug("\t.word 0x%08x\n", f[i]);
  1209. pr_debug("\t.set pop\n");
  1210. memcpy((void *)ebase, final_handler, 0x100);
  1211. }
  1212. /*
  1213. * TLB load/store/modify handlers.
  1214. *
  1215. * Only the fastpath gets synthesized at runtime, the slowpath for
  1216. * do_page_fault remains normal asm.
  1217. */
  1218. extern void tlb_do_page_fault_0(void);
  1219. extern void tlb_do_page_fault_1(void);
  1220. #define __tlb_handler_align \
  1221. __attribute__((__aligned__(1 << CONFIG_MIPS_L1_CACHE_SHIFT)))
  1222. /*
  1223. * 128 instructions for the fastpath handler is generous and should
  1224. * never be exceeded.
  1225. */
  1226. #define FASTPATH_SIZE 128
  1227. u32 __tlb_handler_align handle_tlbl[FASTPATH_SIZE];
  1228. u32 __tlb_handler_align handle_tlbs[FASTPATH_SIZE];
  1229. u32 __tlb_handler_align handle_tlbm[FASTPATH_SIZE];
  1230. static void __init
  1231. iPTE_LW(u32 **p, struct label **l, unsigned int pte, unsigned int ptr)
  1232. {
  1233. #ifdef CONFIG_SMP
  1234. # ifdef CONFIG_64BIT_PHYS_ADDR
  1235. if (cpu_has_64bits)
  1236. i_lld(p, pte, 0, ptr);
  1237. else
  1238. # endif
  1239. i_LL(p, pte, 0, ptr);
  1240. #else
  1241. # ifdef CONFIG_64BIT_PHYS_ADDR
  1242. if (cpu_has_64bits)
  1243. i_ld(p, pte, 0, ptr);
  1244. else
  1245. # endif
  1246. i_LW(p, pte, 0, ptr);
  1247. #endif
  1248. }
  1249. static void __init
  1250. iPTE_SW(u32 **p, struct reloc **r, unsigned int pte, unsigned int ptr,
  1251. unsigned int mode)
  1252. {
  1253. #ifdef CONFIG_64BIT_PHYS_ADDR
  1254. unsigned int hwmode = mode & (_PAGE_VALID | _PAGE_DIRTY);
  1255. #endif
  1256. i_ori(p, pte, pte, mode);
  1257. #ifdef CONFIG_SMP
  1258. # ifdef CONFIG_64BIT_PHYS_ADDR
  1259. if (cpu_has_64bits)
  1260. i_scd(p, pte, 0, ptr);
  1261. else
  1262. # endif
  1263. i_SC(p, pte, 0, ptr);
  1264. if (r10000_llsc_war())
  1265. il_beqzl(p, r, pte, label_smp_pgtable_change);
  1266. else
  1267. il_beqz(p, r, pte, label_smp_pgtable_change);
  1268. # ifdef CONFIG_64BIT_PHYS_ADDR
  1269. if (!cpu_has_64bits) {
  1270. /* no i_nop needed */
  1271. i_ll(p, pte, sizeof(pte_t) / 2, ptr);
  1272. i_ori(p, pte, pte, hwmode);
  1273. i_sc(p, pte, sizeof(pte_t) / 2, ptr);
  1274. il_beqz(p, r, pte, label_smp_pgtable_change);
  1275. /* no i_nop needed */
  1276. i_lw(p, pte, 0, ptr);
  1277. } else
  1278. i_nop(p);
  1279. # else
  1280. i_nop(p);
  1281. # endif
  1282. #else
  1283. # ifdef CONFIG_64BIT_PHYS_ADDR
  1284. if (cpu_has_64bits)
  1285. i_sd(p, pte, 0, ptr);
  1286. else
  1287. # endif
  1288. i_SW(p, pte, 0, ptr);
  1289. # ifdef CONFIG_64BIT_PHYS_ADDR
  1290. if (!cpu_has_64bits) {
  1291. i_lw(p, pte, sizeof(pte_t) / 2, ptr);
  1292. i_ori(p, pte, pte, hwmode);
  1293. i_sw(p, pte, sizeof(pte_t) / 2, ptr);
  1294. i_lw(p, pte, 0, ptr);
  1295. }
  1296. # endif
  1297. #endif
  1298. }
  1299. /*
  1300. * Check if PTE is present, if not then jump to LABEL. PTR points to
  1301. * the page table where this PTE is located, PTE will be re-loaded
  1302. * with it's original value.
  1303. */
  1304. static void __init
  1305. build_pte_present(u32 **p, struct label **l, struct reloc **r,
  1306. unsigned int pte, unsigned int ptr, enum label_id lid)
  1307. {
  1308. i_andi(p, pte, pte, _PAGE_PRESENT | _PAGE_READ);
  1309. i_xori(p, pte, pte, _PAGE_PRESENT | _PAGE_READ);
  1310. il_bnez(p, r, pte, lid);
  1311. iPTE_LW(p, l, pte, ptr);
  1312. }
  1313. /* Make PTE valid, store result in PTR. */
  1314. static void __init
  1315. build_make_valid(u32 **p, struct reloc **r, unsigned int pte,
  1316. unsigned int ptr)
  1317. {
  1318. unsigned int mode = _PAGE_VALID | _PAGE_ACCESSED;
  1319. iPTE_SW(p, r, pte, ptr, mode);
  1320. }
  1321. /*
  1322. * Check if PTE can be written to, if not branch to LABEL. Regardless
  1323. * restore PTE with value from PTR when done.
  1324. */
  1325. static void __init
  1326. build_pte_writable(u32 **p, struct label **l, struct reloc **r,
  1327. unsigned int pte, unsigned int ptr, enum label_id lid)
  1328. {
  1329. i_andi(p, pte, pte, _PAGE_PRESENT | _PAGE_WRITE);
  1330. i_xori(p, pte, pte, _PAGE_PRESENT | _PAGE_WRITE);
  1331. il_bnez(p, r, pte, lid);
  1332. iPTE_LW(p, l, pte, ptr);
  1333. }
  1334. /* Make PTE writable, update software status bits as well, then store
  1335. * at PTR.
  1336. */
  1337. static void __init
  1338. build_make_write(u32 **p, struct reloc **r, unsigned int pte,
  1339. unsigned int ptr)
  1340. {
  1341. unsigned int mode = (_PAGE_ACCESSED | _PAGE_MODIFIED | _PAGE_VALID
  1342. | _PAGE_DIRTY);
  1343. iPTE_SW(p, r, pte, ptr, mode);
  1344. }
  1345. /*
  1346. * Check if PTE can be modified, if not branch to LABEL. Regardless
  1347. * restore PTE with value from PTR when done.
  1348. */
  1349. static void __init
  1350. build_pte_modifiable(u32 **p, struct label **l, struct reloc **r,
  1351. unsigned int pte, unsigned int ptr, enum label_id lid)
  1352. {
  1353. i_andi(p, pte, pte, _PAGE_WRITE);
  1354. il_beqz(p, r, pte, lid);
  1355. iPTE_LW(p, l, pte, ptr);
  1356. }
  1357. /*
  1358. * R3000 style TLB load/store/modify handlers.
  1359. */
  1360. /*
  1361. * This places the pte into ENTRYLO0 and writes it with tlbwi.
  1362. * Then it returns.
  1363. */
  1364. static void __init
  1365. build_r3000_pte_reload_tlbwi(u32 **p, unsigned int pte, unsigned int tmp)
  1366. {
  1367. i_mtc0(p, pte, C0_ENTRYLO0); /* cp0 delay */
  1368. i_mfc0(p, tmp, C0_EPC); /* cp0 delay */
  1369. i_tlbwi(p);
  1370. i_jr(p, tmp);
  1371. i_rfe(p); /* branch delay */
  1372. }
  1373. /*
  1374. * This places the pte into ENTRYLO0 and writes it with tlbwi
  1375. * or tlbwr as appropriate. This is because the index register
  1376. * may have the probe fail bit set as a result of a trap on a
  1377. * kseg2 access, i.e. without refill. Then it returns.
  1378. */
  1379. static void __init
  1380. build_r3000_tlb_reload_write(u32 **p, struct label **l, struct reloc **r,
  1381. unsigned int pte, unsigned int tmp)
  1382. {
  1383. i_mfc0(p, tmp, C0_INDEX);
  1384. i_mtc0(p, pte, C0_ENTRYLO0); /* cp0 delay */
  1385. il_bltz(p, r, tmp, label_r3000_write_probe_fail); /* cp0 delay */
  1386. i_mfc0(p, tmp, C0_EPC); /* branch delay */
  1387. i_tlbwi(p); /* cp0 delay */
  1388. i_jr(p, tmp);
  1389. i_rfe(p); /* branch delay */
  1390. l_r3000_write_probe_fail(l, *p);
  1391. i_tlbwr(p); /* cp0 delay */
  1392. i_jr(p, tmp);
  1393. i_rfe(p); /* branch delay */
  1394. }
  1395. static void __init
  1396. build_r3000_tlbchange_handler_head(u32 **p, unsigned int pte,
  1397. unsigned int ptr)
  1398. {
  1399. long pgdc = (long)pgd_current;
  1400. i_mfc0(p, pte, C0_BADVADDR);
  1401. i_lui(p, ptr, rel_hi(pgdc)); /* cp0 delay */
  1402. i_lw(p, ptr, rel_lo(pgdc), ptr);
  1403. i_srl(p, pte, pte, 22); /* load delay */
  1404. i_sll(p, pte, pte, 2);
  1405. i_addu(p, ptr, ptr, pte);
  1406. i_mfc0(p, pte, C0_CONTEXT);
  1407. i_lw(p, ptr, 0, ptr); /* cp0 delay */
  1408. i_andi(p, pte, pte, 0xffc); /* load delay */
  1409. i_addu(p, ptr, ptr, pte);
  1410. i_lw(p, pte, 0, ptr);
  1411. i_tlbp(p); /* load delay */
  1412. }
  1413. static void __init build_r3000_tlb_load_handler(void)
  1414. {
  1415. u32 *p = handle_tlbl;
  1416. struct label *l = labels;
  1417. struct reloc *r = relocs;
  1418. int i;
  1419. memset(handle_tlbl, 0, sizeof(handle_tlbl));
  1420. memset(labels, 0, sizeof(labels));
  1421. memset(relocs, 0, sizeof(relocs));
  1422. build_r3000_tlbchange_handler_head(&p, K0, K1);
  1423. build_pte_present(&p, &l, &r, K0, K1, label_nopage_tlbl);
  1424. i_nop(&p); /* load delay */
  1425. build_make_valid(&p, &r, K0, K1);
  1426. build_r3000_tlb_reload_write(&p, &l, &r, K0, K1);
  1427. l_nopage_tlbl(&l, p);
  1428. i_j(&p, (unsigned long)tlb_do_page_fault_0 & 0x0fffffff);
  1429. i_nop(&p);
  1430. if ((p - handle_tlbl) > FASTPATH_SIZE)
  1431. panic("TLB load handler fastpath space exceeded");
  1432. resolve_relocs(relocs, labels);
  1433. pr_info("Synthesized TLB load handler fastpath (%u instructions).\n",
  1434. (unsigned int)(p - handle_tlbl));
  1435. pr_debug("\t.set push\n");
  1436. pr_debug("\t.set noreorder\n");
  1437. for (i = 0; i < (p - handle_tlbl); i++)
  1438. pr_debug("\t.word 0x%08x\n", handle_tlbl[i]);
  1439. pr_debug("\t.set pop\n");
  1440. }
  1441. static void __init build_r3000_tlb_store_handler(void)
  1442. {
  1443. u32 *p = handle_tlbs;
  1444. struct label *l = labels;
  1445. struct reloc *r = relocs;
  1446. int i;
  1447. memset(handle_tlbs, 0, sizeof(handle_tlbs));
  1448. memset(labels, 0, sizeof(labels));
  1449. memset(relocs, 0, sizeof(relocs));
  1450. build_r3000_tlbchange_handler_head(&p, K0, K1);
  1451. build_pte_writable(&p, &l, &r, K0, K1, label_nopage_tlbs);
  1452. i_nop(&p); /* load delay */
  1453. build_make_write(&p, &r, K0, K1);
  1454. build_r3000_tlb_reload_write(&p, &l, &r, K0, K1);
  1455. l_nopage_tlbs(&l, p);
  1456. i_j(&p, (unsigned long)tlb_do_page_fault_1 & 0x0fffffff);
  1457. i_nop(&p);
  1458. if ((p - handle_tlbs) > FASTPATH_SIZE)
  1459. panic("TLB store handler fastpath space exceeded");
  1460. resolve_relocs(relocs, labels);
  1461. pr_info("Synthesized TLB store handler fastpath (%u instructions).\n",
  1462. (unsigned int)(p - handle_tlbs));
  1463. pr_debug("\t.set push\n");
  1464. pr_debug("\t.set noreorder\n");
  1465. for (i = 0; i < (p - handle_tlbs); i++)
  1466. pr_debug("\t.word 0x%08x\n", handle_tlbs[i]);
  1467. pr_debug("\t.set pop\n");
  1468. }
  1469. static void __init build_r3000_tlb_modify_handler(void)
  1470. {
  1471. u32 *p = handle_tlbm;
  1472. struct label *l = labels;
  1473. struct reloc *r = relocs;
  1474. int i;
  1475. memset(handle_tlbm, 0, sizeof(handle_tlbm));
  1476. memset(labels, 0, sizeof(labels));
  1477. memset(relocs, 0, sizeof(relocs));
  1478. build_r3000_tlbchange_handler_head(&p, K0, K1);
  1479. build_pte_modifiable(&p, &l, &r, K0, K1, label_nopage_tlbm);
  1480. i_nop(&p); /* load delay */
  1481. build_make_write(&p, &r, K0, K1);
  1482. build_r3000_pte_reload_tlbwi(&p, K0, K1);
  1483. l_nopage_tlbm(&l, p);
  1484. i_j(&p, (unsigned long)tlb_do_page_fault_1 & 0x0fffffff);
  1485. i_nop(&p);
  1486. if ((p - handle_tlbm) > FASTPATH_SIZE)
  1487. panic("TLB modify handler fastpath space exceeded");
  1488. resolve_relocs(relocs, labels);
  1489. pr_info("Synthesized TLB modify handler fastpath (%u instructions).\n",
  1490. (unsigned int)(p - handle_tlbm));
  1491. pr_debug("\t.set push\n");
  1492. pr_debug("\t.set noreorder\n");
  1493. for (i = 0; i < (p - handle_tlbm); i++)
  1494. pr_debug("\t.word 0x%08x\n", handle_tlbm[i]);
  1495. pr_debug("\t.set pop\n");
  1496. }
  1497. /*
  1498. * R4000 style TLB load/store/modify handlers.
  1499. */
  1500. static void __init
  1501. build_r4000_tlbchange_handler_head(u32 **p, struct label **l,
  1502. struct reloc **r, unsigned int pte,
  1503. unsigned int ptr)
  1504. {
  1505. #ifdef CONFIG_64BIT
  1506. build_get_pmde64(p, l, r, pte, ptr); /* get pmd in ptr */
  1507. #else
  1508. build_get_pgde32(p, pte, ptr); /* get pgd in ptr */
  1509. #endif
  1510. i_MFC0(p, pte, C0_BADVADDR);
  1511. i_LW(p, ptr, 0, ptr);
  1512. i_SRL(p, pte, pte, PAGE_SHIFT + PTE_ORDER - PTE_T_LOG2);
  1513. i_andi(p, pte, pte, (PTRS_PER_PTE - 1) << PTE_T_LOG2);
  1514. i_ADDU(p, ptr, ptr, pte);
  1515. #ifdef CONFIG_SMP
  1516. l_smp_pgtable_change(l, *p);
  1517. # endif
  1518. iPTE_LW(p, l, pte, ptr); /* get even pte */
  1519. if (!m4kc_tlbp_war())
  1520. build_tlb_probe_entry(p);
  1521. }
  1522. static void __init
  1523. build_r4000_tlbchange_handler_tail(u32 **p, struct label **l,
  1524. struct reloc **r, unsigned int tmp,
  1525. unsigned int ptr)
  1526. {
  1527. i_ori(p, ptr, ptr, sizeof(pte_t));
  1528. i_xori(p, ptr, ptr, sizeof(pte_t));
  1529. build_update_entries(p, tmp, ptr);
  1530. build_tlb_write_entry(p, l, r, tlb_indexed);
  1531. l_leave(l, *p);
  1532. i_eret(p); /* return from trap */
  1533. #ifdef CONFIG_64BIT
  1534. build_get_pgd_vmalloc64(p, l, r, tmp, ptr);
  1535. #endif
  1536. }
  1537. static void __init build_r4000_tlb_load_handler(void)
  1538. {
  1539. u32 *p = handle_tlbl;
  1540. struct label *l = labels;
  1541. struct reloc *r = relocs;
  1542. int i;
  1543. memset(handle_tlbl, 0, sizeof(handle_tlbl));
  1544. memset(labels, 0, sizeof(labels));
  1545. memset(relocs, 0, sizeof(relocs));
  1546. if (bcm1250_m3_war()) {
  1547. i_MFC0(&p, K0, C0_BADVADDR);
  1548. i_MFC0(&p, K1, C0_ENTRYHI);
  1549. i_xor(&p, K0, K0, K1);
  1550. i_SRL(&p, K0, K0, PAGE_SHIFT + 1);
  1551. il_bnez(&p, &r, K0, label_leave);
  1552. /* No need for i_nop */
  1553. }
  1554. build_r4000_tlbchange_handler_head(&p, &l, &r, K0, K1);
  1555. build_pte_present(&p, &l, &r, K0, K1, label_nopage_tlbl);
  1556. if (m4kc_tlbp_war())
  1557. build_tlb_probe_entry(&p);
  1558. build_make_valid(&p, &r, K0, K1);
  1559. build_r4000_tlbchange_handler_tail(&p, &l, &r, K0, K1);
  1560. l_nopage_tlbl(&l, p);
  1561. i_j(&p, (unsigned long)tlb_do_page_fault_0 & 0x0fffffff);
  1562. i_nop(&p);
  1563. if ((p - handle_tlbl) > FASTPATH_SIZE)
  1564. panic("TLB load handler fastpath space exceeded");
  1565. resolve_relocs(relocs, labels);
  1566. pr_info("Synthesized TLB load handler fastpath (%u instructions).\n",
  1567. (unsigned int)(p - handle_tlbl));
  1568. pr_debug("\t.set push\n");
  1569. pr_debug("\t.set noreorder\n");
  1570. for (i = 0; i < (p - handle_tlbl); i++)
  1571. pr_debug("\t.word 0x%08x\n", handle_tlbl[i]);
  1572. pr_debug("\t.set pop\n");
  1573. }
  1574. static void __init build_r4000_tlb_store_handler(void)
  1575. {
  1576. u32 *p = handle_tlbs;
  1577. struct label *l = labels;
  1578. struct reloc *r = relocs;
  1579. int i;
  1580. memset(handle_tlbs, 0, sizeof(handle_tlbs));
  1581. memset(labels, 0, sizeof(labels));
  1582. memset(relocs, 0, sizeof(relocs));
  1583. build_r4000_tlbchange_handler_head(&p, &l, &r, K0, K1);
  1584. build_pte_writable(&p, &l, &r, K0, K1, label_nopage_tlbs);
  1585. if (m4kc_tlbp_war())
  1586. build_tlb_probe_entry(&p);
  1587. build_make_write(&p, &r, K0, K1);
  1588. build_r4000_tlbchange_handler_tail(&p, &l, &r, K0, K1);
  1589. l_nopage_tlbs(&l, p);
  1590. i_j(&p, (unsigned long)tlb_do_page_fault_1 & 0x0fffffff);
  1591. i_nop(&p);
  1592. if ((p - handle_tlbs) > FASTPATH_SIZE)
  1593. panic("TLB store handler fastpath space exceeded");
  1594. resolve_relocs(relocs, labels);
  1595. pr_info("Synthesized TLB store handler fastpath (%u instructions).\n",
  1596. (unsigned int)(p - handle_tlbs));
  1597. pr_debug("\t.set push\n");
  1598. pr_debug("\t.set noreorder\n");
  1599. for (i = 0; i < (p - handle_tlbs); i++)
  1600. pr_debug("\t.word 0x%08x\n", handle_tlbs[i]);
  1601. pr_debug("\t.set pop\n");
  1602. }
  1603. static void __init build_r4000_tlb_modify_handler(void)
  1604. {
  1605. u32 *p = handle_tlbm;
  1606. struct label *l = labels;
  1607. struct reloc *r = relocs;
  1608. int i;
  1609. memset(handle_tlbm, 0, sizeof(handle_tlbm));
  1610. memset(labels, 0, sizeof(labels));
  1611. memset(relocs, 0, sizeof(relocs));
  1612. build_r4000_tlbchange_handler_head(&p, &l, &r, K0, K1);
  1613. build_pte_modifiable(&p, &l, &r, K0, K1, label_nopage_tlbm);
  1614. if (m4kc_tlbp_war())
  1615. build_tlb_probe_entry(&p);
  1616. /* Present and writable bits set, set accessed and dirty bits. */
  1617. build_make_write(&p, &r, K0, K1);
  1618. build_r4000_tlbchange_handler_tail(&p, &l, &r, K0, K1);
  1619. l_nopage_tlbm(&l, p);
  1620. i_j(&p, (unsigned long)tlb_do_page_fault_1 & 0x0fffffff);
  1621. i_nop(&p);
  1622. if ((p - handle_tlbm) > FASTPATH_SIZE)
  1623. panic("TLB modify handler fastpath space exceeded");
  1624. resolve_relocs(relocs, labels);
  1625. pr_info("Synthesized TLB modify handler fastpath (%u instructions).\n",
  1626. (unsigned int)(p - handle_tlbm));
  1627. pr_debug("\t.set push\n");
  1628. pr_debug("\t.set noreorder\n");
  1629. for (i = 0; i < (p - handle_tlbm); i++)
  1630. pr_debug("\t.word 0x%08x\n", handle_tlbm[i]);
  1631. pr_debug("\t.set pop\n");
  1632. }
  1633. void __init build_tlb_refill_handler(void)
  1634. {
  1635. /*
  1636. * The refill handler is generated per-CPU, multi-node systems
  1637. * may have local storage for it. The other handlers are only
  1638. * needed once.
  1639. */
  1640. static int run_once = 0;
  1641. switch (current_cpu_data.cputype) {
  1642. case CPU_R2000:
  1643. case CPU_R3000:
  1644. case CPU_R3000A:
  1645. case CPU_R3081E:
  1646. case CPU_TX3912:
  1647. case CPU_TX3922:
  1648. case CPU_TX3927:
  1649. build_r3000_tlb_refill_handler();
  1650. if (!run_once) {
  1651. build_r3000_tlb_load_handler();
  1652. build_r3000_tlb_store_handler();
  1653. build_r3000_tlb_modify_handler();
  1654. run_once++;
  1655. }
  1656. break;
  1657. case CPU_R6000:
  1658. case CPU_R6000A:
  1659. panic("No R6000 TLB refill handler yet");
  1660. break;
  1661. case CPU_R8000:
  1662. panic("No R8000 TLB refill handler yet");
  1663. break;
  1664. default:
  1665. build_r4000_tlb_refill_handler();
  1666. if (!run_once) {
  1667. build_r4000_tlb_load_handler();
  1668. build_r4000_tlb_store_handler();
  1669. build_r4000_tlb_modify_handler();
  1670. run_once++;
  1671. }
  1672. }
  1673. }
  1674. void __init flush_tlb_handlers(void)
  1675. {
  1676. flush_icache_range((unsigned long)handle_tlbl,
  1677. (unsigned long)handle_tlbl + sizeof(handle_tlbl));
  1678. flush_icache_range((unsigned long)handle_tlbs,
  1679. (unsigned long)handle_tlbs + sizeof(handle_tlbs));
  1680. flush_icache_range((unsigned long)handle_tlbm,
  1681. (unsigned long)handle_tlbm + sizeof(handle_tlbm));
  1682. }