tlbex.c 71 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619
  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, 2008 Thiemo Seufer
  9. * Copyright (C) 2005, 2007, 2008, 2009 Maciej W. Rozycki
  10. * Copyright (C) 2006 Ralf Baechle ([email protected])
  11. * Copyright (C) 2008, 2009 Cavium Networks, Inc.
  12. * Copyright (C) 2011 MIPS Technologies, Inc.
  13. *
  14. * ... and the days got worse and worse and now you see
  15. * I've gone completely out of my mind.
  16. *
  17. * They're coming to take me a away haha
  18. * they're coming to take me a away hoho hihi haha
  19. * to the funny farm where code is beautiful all the time ...
  20. *
  21. * (Condolences to Napoleon XIV)
  22. */
  23. #include <linux/bug.h>
  24. #include <linux/export.h>
  25. #include <linux/kernel.h>
  26. #include <linux/types.h>
  27. #include <linux/smp.h>
  28. #include <linux/string.h>
  29. #include <linux/cache.h>
  30. #include <linux/pgtable.h>
  31. #include <asm/cacheflush.h>
  32. #include <asm/cpu-type.h>
  33. #include <asm/mmu_context.h>
  34. #include <asm/uasm.h>
  35. #include <asm/setup.h>
  36. #include <asm/tlbex.h>
  37. static int mips_xpa_disabled;
  38. static int __init xpa_disable(char *s)
  39. {
  40. mips_xpa_disabled = 1;
  41. return 1;
  42. }
  43. __setup("noxpa", xpa_disable);
  44. /*
  45. * TLB load/store/modify handlers.
  46. *
  47. * Only the fastpath gets synthesized at runtime, the slowpath for
  48. * do_page_fault remains normal asm.
  49. */
  50. extern void tlb_do_page_fault_0(void);
  51. extern void tlb_do_page_fault_1(void);
  52. struct work_registers {
  53. int r1;
  54. int r2;
  55. int r3;
  56. };
  57. struct tlb_reg_save {
  58. unsigned long a;
  59. unsigned long b;
  60. } ____cacheline_aligned_in_smp;
  61. static struct tlb_reg_save handler_reg_save[NR_CPUS];
  62. static inline int r45k_bvahwbug(void)
  63. {
  64. /* XXX: We should probe for the presence of this bug, but we don't. */
  65. return 0;
  66. }
  67. static inline int r4k_250MHZhwbug(void)
  68. {
  69. /* XXX: We should probe for the presence of this bug, but we don't. */
  70. return 0;
  71. }
  72. extern int sb1250_m3_workaround_needed(void);
  73. static inline int __maybe_unused bcm1250_m3_war(void)
  74. {
  75. if (IS_ENABLED(CONFIG_SB1_PASS_2_WORKAROUNDS))
  76. return sb1250_m3_workaround_needed();
  77. return 0;
  78. }
  79. static inline int __maybe_unused r10000_llsc_war(void)
  80. {
  81. return IS_ENABLED(CONFIG_WAR_R10000_LLSC);
  82. }
  83. static int use_bbit_insns(void)
  84. {
  85. switch (current_cpu_type()) {
  86. case CPU_CAVIUM_OCTEON:
  87. case CPU_CAVIUM_OCTEON_PLUS:
  88. case CPU_CAVIUM_OCTEON2:
  89. case CPU_CAVIUM_OCTEON3:
  90. return 1;
  91. default:
  92. return 0;
  93. }
  94. }
  95. static int use_lwx_insns(void)
  96. {
  97. switch (current_cpu_type()) {
  98. case CPU_CAVIUM_OCTEON2:
  99. case CPU_CAVIUM_OCTEON3:
  100. return 1;
  101. default:
  102. return 0;
  103. }
  104. }
  105. #if defined(CONFIG_CAVIUM_OCTEON_CVMSEG_SIZE) && \
  106. CONFIG_CAVIUM_OCTEON_CVMSEG_SIZE > 0
  107. static bool scratchpad_available(void)
  108. {
  109. return true;
  110. }
  111. static int scratchpad_offset(int i)
  112. {
  113. /*
  114. * CVMSEG starts at address -32768 and extends for
  115. * CAVIUM_OCTEON_CVMSEG_SIZE 128 byte cache lines.
  116. */
  117. i += 1; /* Kernel use starts at the top and works down. */
  118. return CONFIG_CAVIUM_OCTEON_CVMSEG_SIZE * 128 - (8 * i) - 32768;
  119. }
  120. #else
  121. static bool scratchpad_available(void)
  122. {
  123. return false;
  124. }
  125. static int scratchpad_offset(int i)
  126. {
  127. BUG();
  128. /* Really unreachable, but evidently some GCC want this. */
  129. return 0;
  130. }
  131. #endif
  132. /*
  133. * Found by experiment: At least some revisions of the 4kc throw under
  134. * some circumstances a machine check exception, triggered by invalid
  135. * values in the index register. Delaying the tlbp instruction until
  136. * after the next branch, plus adding an additional nop in front of
  137. * tlbwi/tlbwr avoids the invalid index register values. Nobody knows
  138. * why; it's not an issue caused by the core RTL.
  139. *
  140. */
  141. static int m4kc_tlbp_war(void)
  142. {
  143. return current_cpu_type() == CPU_4KC;
  144. }
  145. /* Handle labels (which must be positive integers). */
  146. enum label_id {
  147. label_second_part = 1,
  148. label_leave,
  149. label_vmalloc,
  150. label_vmalloc_done,
  151. label_tlbw_hazard_0,
  152. label_split = label_tlbw_hazard_0 + 8,
  153. label_tlbl_goaround1,
  154. label_tlbl_goaround2,
  155. label_nopage_tlbl,
  156. label_nopage_tlbs,
  157. label_nopage_tlbm,
  158. label_smp_pgtable_change,
  159. label_r3000_write_probe_fail,
  160. label_large_segbits_fault,
  161. #ifdef CONFIG_MIPS_HUGE_TLB_SUPPORT
  162. label_tlb_huge_update,
  163. #endif
  164. };
  165. UASM_L_LA(_second_part)
  166. UASM_L_LA(_leave)
  167. UASM_L_LA(_vmalloc)
  168. UASM_L_LA(_vmalloc_done)
  169. /* _tlbw_hazard_x is handled differently. */
  170. UASM_L_LA(_split)
  171. UASM_L_LA(_tlbl_goaround1)
  172. UASM_L_LA(_tlbl_goaround2)
  173. UASM_L_LA(_nopage_tlbl)
  174. UASM_L_LA(_nopage_tlbs)
  175. UASM_L_LA(_nopage_tlbm)
  176. UASM_L_LA(_smp_pgtable_change)
  177. UASM_L_LA(_r3000_write_probe_fail)
  178. UASM_L_LA(_large_segbits_fault)
  179. #ifdef CONFIG_MIPS_HUGE_TLB_SUPPORT
  180. UASM_L_LA(_tlb_huge_update)
  181. #endif
  182. static int hazard_instance;
  183. static void uasm_bgezl_hazard(u32 **p, struct uasm_reloc **r, int instance)
  184. {
  185. switch (instance) {
  186. case 0 ... 7:
  187. uasm_il_bgezl(p, r, 0, label_tlbw_hazard_0 + instance);
  188. return;
  189. default:
  190. BUG();
  191. }
  192. }
  193. static void uasm_bgezl_label(struct uasm_label **l, u32 **p, int instance)
  194. {
  195. switch (instance) {
  196. case 0 ... 7:
  197. uasm_build_label(l, *p, label_tlbw_hazard_0 + instance);
  198. break;
  199. default:
  200. BUG();
  201. }
  202. }
  203. /*
  204. * pgtable bits are assigned dynamically depending on processor feature
  205. * and statically based on kernel configuration. This spits out the actual
  206. * values the kernel is using. Required to make sense from disassembled
  207. * TLB exception handlers.
  208. */
  209. static void output_pgtable_bits_defines(void)
  210. {
  211. #define pr_define(fmt, ...) \
  212. pr_debug("#define " fmt, ##__VA_ARGS__)
  213. pr_debug("#include <asm/asm.h>\n");
  214. pr_debug("#include <asm/regdef.h>\n");
  215. pr_debug("\n");
  216. pr_define("_PAGE_PRESENT_SHIFT %d\n", _PAGE_PRESENT_SHIFT);
  217. pr_define("_PAGE_NO_READ_SHIFT %d\n", _PAGE_NO_READ_SHIFT);
  218. pr_define("_PAGE_WRITE_SHIFT %d\n", _PAGE_WRITE_SHIFT);
  219. pr_define("_PAGE_ACCESSED_SHIFT %d\n", _PAGE_ACCESSED_SHIFT);
  220. pr_define("_PAGE_MODIFIED_SHIFT %d\n", _PAGE_MODIFIED_SHIFT);
  221. #ifdef CONFIG_MIPS_HUGE_TLB_SUPPORT
  222. pr_define("_PAGE_HUGE_SHIFT %d\n", _PAGE_HUGE_SHIFT);
  223. #endif
  224. #ifdef _PAGE_NO_EXEC_SHIFT
  225. if (cpu_has_rixi)
  226. pr_define("_PAGE_NO_EXEC_SHIFT %d\n", _PAGE_NO_EXEC_SHIFT);
  227. #endif
  228. pr_define("_PAGE_GLOBAL_SHIFT %d\n", _PAGE_GLOBAL_SHIFT);
  229. pr_define("_PAGE_VALID_SHIFT %d\n", _PAGE_VALID_SHIFT);
  230. pr_define("_PAGE_DIRTY_SHIFT %d\n", _PAGE_DIRTY_SHIFT);
  231. pr_define("_PFN_SHIFT %d\n", _PFN_SHIFT);
  232. pr_debug("\n");
  233. }
  234. static inline void dump_handler(const char *symbol, const void *start, const void *end)
  235. {
  236. unsigned int count = (end - start) / sizeof(u32);
  237. const u32 *handler = start;
  238. int i;
  239. pr_debug("LEAF(%s)\n", symbol);
  240. pr_debug("\t.set push\n");
  241. pr_debug("\t.set noreorder\n");
  242. for (i = 0; i < count; i++)
  243. pr_debug("\t.word\t0x%08x\t\t# %p\n", handler[i], &handler[i]);
  244. pr_debug("\t.set\tpop\n");
  245. pr_debug("\tEND(%s)\n", symbol);
  246. }
  247. /* The only general purpose registers allowed in TLB handlers. */
  248. #define K0 26
  249. #define K1 27
  250. /* Some CP0 registers */
  251. #define C0_INDEX 0, 0
  252. #define C0_ENTRYLO0 2, 0
  253. #define C0_TCBIND 2, 2
  254. #define C0_ENTRYLO1 3, 0
  255. #define C0_CONTEXT 4, 0
  256. #define C0_PAGEMASK 5, 0
  257. #define C0_PWBASE 5, 5
  258. #define C0_PWFIELD 5, 6
  259. #define C0_PWSIZE 5, 7
  260. #define C0_PWCTL 6, 6
  261. #define C0_BADVADDR 8, 0
  262. #define C0_PGD 9, 7
  263. #define C0_ENTRYHI 10, 0
  264. #define C0_EPC 14, 0
  265. #define C0_XCONTEXT 20, 0
  266. #ifdef CONFIG_64BIT
  267. # define GET_CONTEXT(buf, reg) UASM_i_MFC0(buf, reg, C0_XCONTEXT)
  268. #else
  269. # define GET_CONTEXT(buf, reg) UASM_i_MFC0(buf, reg, C0_CONTEXT)
  270. #endif
  271. /* The worst case length of the handler is around 18 instructions for
  272. * R3000-style TLBs and up to 63 instructions for R4000-style TLBs.
  273. * Maximum space available is 32 instructions for R3000 and 64
  274. * instructions for R4000.
  275. *
  276. * We deliberately chose a buffer size of 128, so we won't scribble
  277. * over anything important on overflow before we panic.
  278. */
  279. static u32 tlb_handler[128];
  280. /* simply assume worst case size for labels and relocs */
  281. static struct uasm_label labels[128];
  282. static struct uasm_reloc relocs[128];
  283. static int check_for_high_segbits;
  284. static bool fill_includes_sw_bits;
  285. static unsigned int kscratch_used_mask;
  286. static inline int __maybe_unused c0_kscratch(void)
  287. {
  288. return 31;
  289. }
  290. static int allocate_kscratch(void)
  291. {
  292. int r;
  293. unsigned int a = cpu_data[0].kscratch_mask & ~kscratch_used_mask;
  294. r = ffs(a);
  295. if (r == 0)
  296. return -1;
  297. r--; /* make it zero based */
  298. kscratch_used_mask |= (1 << r);
  299. return r;
  300. }
  301. static int scratch_reg;
  302. int pgd_reg;
  303. EXPORT_SYMBOL_GPL(pgd_reg);
  304. enum vmalloc64_mode {not_refill, refill_scratch, refill_noscratch};
  305. static struct work_registers build_get_work_registers(u32 **p)
  306. {
  307. struct work_registers r;
  308. if (scratch_reg >= 0) {
  309. /* Save in CPU local C0_KScratch? */
  310. UASM_i_MTC0(p, 1, c0_kscratch(), scratch_reg);
  311. r.r1 = K0;
  312. r.r2 = K1;
  313. r.r3 = 1;
  314. return r;
  315. }
  316. if (num_possible_cpus() > 1) {
  317. /* Get smp_processor_id */
  318. UASM_i_CPUID_MFC0(p, K0, SMP_CPUID_REG);
  319. UASM_i_SRL_SAFE(p, K0, K0, SMP_CPUID_REGSHIFT);
  320. /* handler_reg_save index in K0 */
  321. UASM_i_SLL(p, K0, K0, ilog2(sizeof(struct tlb_reg_save)));
  322. UASM_i_LA(p, K1, (long)&handler_reg_save);
  323. UASM_i_ADDU(p, K0, K0, K1);
  324. } else {
  325. UASM_i_LA(p, K0, (long)&handler_reg_save);
  326. }
  327. /* K0 now points to save area, save $1 and $2 */
  328. UASM_i_SW(p, 1, offsetof(struct tlb_reg_save, a), K0);
  329. UASM_i_SW(p, 2, offsetof(struct tlb_reg_save, b), K0);
  330. r.r1 = K1;
  331. r.r2 = 1;
  332. r.r3 = 2;
  333. return r;
  334. }
  335. static void build_restore_work_registers(u32 **p)
  336. {
  337. if (scratch_reg >= 0) {
  338. uasm_i_ehb(p);
  339. UASM_i_MFC0(p, 1, c0_kscratch(), scratch_reg);
  340. return;
  341. }
  342. /* K0 already points to save area, restore $1 and $2 */
  343. UASM_i_LW(p, 1, offsetof(struct tlb_reg_save, a), K0);
  344. UASM_i_LW(p, 2, offsetof(struct tlb_reg_save, b), K0);
  345. }
  346. #ifndef CONFIG_MIPS_PGD_C0_CONTEXT
  347. /*
  348. * CONFIG_MIPS_PGD_C0_CONTEXT implies 64 bit and lack of pgd_current,
  349. * we cannot do r3000 under these circumstances.
  350. *
  351. * The R3000 TLB handler is simple.
  352. */
  353. static void build_r3000_tlb_refill_handler(void)
  354. {
  355. long pgdc = (long)pgd_current;
  356. u32 *p;
  357. memset(tlb_handler, 0, sizeof(tlb_handler));
  358. p = tlb_handler;
  359. uasm_i_mfc0(&p, K0, C0_BADVADDR);
  360. uasm_i_lui(&p, K1, uasm_rel_hi(pgdc)); /* cp0 delay */
  361. uasm_i_lw(&p, K1, uasm_rel_lo(pgdc), K1);
  362. uasm_i_srl(&p, K0, K0, 22); /* load delay */
  363. uasm_i_sll(&p, K0, K0, 2);
  364. uasm_i_addu(&p, K1, K1, K0);
  365. uasm_i_mfc0(&p, K0, C0_CONTEXT);
  366. uasm_i_lw(&p, K1, 0, K1); /* cp0 delay */
  367. uasm_i_andi(&p, K0, K0, 0xffc); /* load delay */
  368. uasm_i_addu(&p, K1, K1, K0);
  369. uasm_i_lw(&p, K0, 0, K1);
  370. uasm_i_nop(&p); /* load delay */
  371. uasm_i_mtc0(&p, K0, C0_ENTRYLO0);
  372. uasm_i_mfc0(&p, K1, C0_EPC); /* cp0 delay */
  373. uasm_i_tlbwr(&p); /* cp0 delay */
  374. uasm_i_jr(&p, K1);
  375. uasm_i_rfe(&p); /* branch delay */
  376. if (p > tlb_handler + 32)
  377. panic("TLB refill handler space exceeded");
  378. pr_debug("Wrote TLB refill handler (%u instructions).\n",
  379. (unsigned int)(p - tlb_handler));
  380. memcpy((void *)ebase, tlb_handler, 0x80);
  381. local_flush_icache_range(ebase, ebase + 0x80);
  382. dump_handler("r3000_tlb_refill", (u32 *)ebase, (u32 *)(ebase + 0x80));
  383. }
  384. #endif /* CONFIG_MIPS_PGD_C0_CONTEXT */
  385. /*
  386. * The R4000 TLB handler is much more complicated. We have two
  387. * consecutive handler areas with 32 instructions space each.
  388. * Since they aren't used at the same time, we can overflow in the
  389. * other one.To keep things simple, we first assume linear space,
  390. * then we relocate it to the final handler layout as needed.
  391. */
  392. static u32 final_handler[64];
  393. /*
  394. * Hazards
  395. *
  396. * From the IDT errata for the QED RM5230 (Nevada), processor revision 1.0:
  397. * 2. A timing hazard exists for the TLBP instruction.
  398. *
  399. * stalling_instruction
  400. * TLBP
  401. *
  402. * The JTLB is being read for the TLBP throughout the stall generated by the
  403. * previous instruction. This is not really correct as the stalling instruction
  404. * can modify the address used to access the JTLB. The failure symptom is that
  405. * the TLBP instruction will use an address created for the stalling instruction
  406. * and not the address held in C0_ENHI and thus report the wrong results.
  407. *
  408. * The software work-around is to not allow the instruction preceding the TLBP
  409. * to stall - make it an NOP or some other instruction guaranteed not to stall.
  410. *
  411. * Errata 2 will not be fixed. This errata is also on the R5000.
  412. *
  413. * As if we MIPS hackers wouldn't know how to nop pipelines happy ...
  414. */
  415. static void __maybe_unused build_tlb_probe_entry(u32 **p)
  416. {
  417. switch (current_cpu_type()) {
  418. /* Found by experiment: R4600 v2.0/R4700 needs this, too. */
  419. case CPU_R4600:
  420. case CPU_R4700:
  421. case CPU_R5000:
  422. case CPU_NEVADA:
  423. uasm_i_nop(p);
  424. uasm_i_tlbp(p);
  425. break;
  426. default:
  427. uasm_i_tlbp(p);
  428. break;
  429. }
  430. }
  431. void build_tlb_write_entry(u32 **p, struct uasm_label **l,
  432. struct uasm_reloc **r,
  433. enum tlb_write_entry wmode)
  434. {
  435. void(*tlbw)(u32 **) = NULL;
  436. switch (wmode) {
  437. case tlb_random: tlbw = uasm_i_tlbwr; break;
  438. case tlb_indexed: tlbw = uasm_i_tlbwi; break;
  439. }
  440. if (cpu_has_mips_r2_r6) {
  441. if (cpu_has_mips_r2_exec_hazard)
  442. uasm_i_ehb(p);
  443. tlbw(p);
  444. return;
  445. }
  446. switch (current_cpu_type()) {
  447. case CPU_R4000PC:
  448. case CPU_R4000SC:
  449. case CPU_R4000MC:
  450. case CPU_R4400PC:
  451. case CPU_R4400SC:
  452. case CPU_R4400MC:
  453. /*
  454. * This branch uses up a mtc0 hazard nop slot and saves
  455. * two nops after the tlbw instruction.
  456. */
  457. uasm_bgezl_hazard(p, r, hazard_instance);
  458. tlbw(p);
  459. uasm_bgezl_label(l, p, hazard_instance);
  460. hazard_instance++;
  461. uasm_i_nop(p);
  462. break;
  463. case CPU_R4600:
  464. case CPU_R4700:
  465. uasm_i_nop(p);
  466. tlbw(p);
  467. uasm_i_nop(p);
  468. break;
  469. case CPU_R5000:
  470. case CPU_NEVADA:
  471. uasm_i_nop(p); /* QED specifies 2 nops hazard */
  472. uasm_i_nop(p); /* QED specifies 2 nops hazard */
  473. tlbw(p);
  474. break;
  475. case CPU_R4300:
  476. case CPU_5KC:
  477. case CPU_TX49XX:
  478. case CPU_PR4450:
  479. uasm_i_nop(p);
  480. tlbw(p);
  481. break;
  482. case CPU_R10000:
  483. case CPU_R12000:
  484. case CPU_R14000:
  485. case CPU_R16000:
  486. case CPU_4KC:
  487. case CPU_4KEC:
  488. case CPU_M14KC:
  489. case CPU_M14KEC:
  490. case CPU_SB1:
  491. case CPU_SB1A:
  492. case CPU_4KSC:
  493. case CPU_20KC:
  494. case CPU_25KF:
  495. case CPU_BMIPS32:
  496. case CPU_BMIPS3300:
  497. case CPU_BMIPS4350:
  498. case CPU_BMIPS4380:
  499. case CPU_BMIPS5000:
  500. case CPU_LOONGSON2EF:
  501. case CPU_LOONGSON64:
  502. case CPU_R5500:
  503. if (m4kc_tlbp_war())
  504. uasm_i_nop(p);
  505. fallthrough;
  506. case CPU_ALCHEMY:
  507. tlbw(p);
  508. break;
  509. case CPU_RM7000:
  510. uasm_i_nop(p);
  511. uasm_i_nop(p);
  512. uasm_i_nop(p);
  513. uasm_i_nop(p);
  514. tlbw(p);
  515. break;
  516. case CPU_XBURST:
  517. tlbw(p);
  518. uasm_i_nop(p);
  519. break;
  520. default:
  521. panic("No TLB refill handler yet (CPU type: %d)",
  522. current_cpu_type());
  523. break;
  524. }
  525. }
  526. EXPORT_SYMBOL_GPL(build_tlb_write_entry);
  527. static __maybe_unused void build_convert_pte_to_entrylo(u32 **p,
  528. unsigned int reg)
  529. {
  530. if (_PAGE_GLOBAL_SHIFT == 0) {
  531. /* pte_t is already in EntryLo format */
  532. return;
  533. }
  534. if (cpu_has_rixi && _PAGE_NO_EXEC != 0) {
  535. if (fill_includes_sw_bits) {
  536. UASM_i_ROTR(p, reg, reg, ilog2(_PAGE_GLOBAL));
  537. } else {
  538. UASM_i_SRL(p, reg, reg, ilog2(_PAGE_NO_EXEC));
  539. UASM_i_ROTR(p, reg, reg,
  540. ilog2(_PAGE_GLOBAL) - ilog2(_PAGE_NO_EXEC));
  541. }
  542. } else {
  543. #ifdef CONFIG_PHYS_ADDR_T_64BIT
  544. uasm_i_dsrl_safe(p, reg, reg, ilog2(_PAGE_GLOBAL));
  545. #else
  546. UASM_i_SRL(p, reg, reg, ilog2(_PAGE_GLOBAL));
  547. #endif
  548. }
  549. }
  550. #ifdef CONFIG_MIPS_HUGE_TLB_SUPPORT
  551. static void build_restore_pagemask(u32 **p, struct uasm_reloc **r,
  552. unsigned int tmp, enum label_id lid,
  553. int restore_scratch)
  554. {
  555. if (restore_scratch) {
  556. /*
  557. * Ensure the MFC0 below observes the value written to the
  558. * KScratch register by the prior MTC0.
  559. */
  560. if (scratch_reg >= 0)
  561. uasm_i_ehb(p);
  562. /* Reset default page size */
  563. if (PM_DEFAULT_MASK >> 16) {
  564. uasm_i_lui(p, tmp, PM_DEFAULT_MASK >> 16);
  565. uasm_i_ori(p, tmp, tmp, PM_DEFAULT_MASK & 0xffff);
  566. uasm_i_mtc0(p, tmp, C0_PAGEMASK);
  567. uasm_il_b(p, r, lid);
  568. } else if (PM_DEFAULT_MASK) {
  569. uasm_i_ori(p, tmp, 0, PM_DEFAULT_MASK);
  570. uasm_i_mtc0(p, tmp, C0_PAGEMASK);
  571. uasm_il_b(p, r, lid);
  572. } else {
  573. uasm_i_mtc0(p, 0, C0_PAGEMASK);
  574. uasm_il_b(p, r, lid);
  575. }
  576. if (scratch_reg >= 0)
  577. UASM_i_MFC0(p, 1, c0_kscratch(), scratch_reg);
  578. else
  579. UASM_i_LW(p, 1, scratchpad_offset(0), 0);
  580. } else {
  581. /* Reset default page size */
  582. if (PM_DEFAULT_MASK >> 16) {
  583. uasm_i_lui(p, tmp, PM_DEFAULT_MASK >> 16);
  584. uasm_i_ori(p, tmp, tmp, PM_DEFAULT_MASK & 0xffff);
  585. uasm_il_b(p, r, lid);
  586. uasm_i_mtc0(p, tmp, C0_PAGEMASK);
  587. } else if (PM_DEFAULT_MASK) {
  588. uasm_i_ori(p, tmp, 0, PM_DEFAULT_MASK);
  589. uasm_il_b(p, r, lid);
  590. uasm_i_mtc0(p, tmp, C0_PAGEMASK);
  591. } else {
  592. uasm_il_b(p, r, lid);
  593. uasm_i_mtc0(p, 0, C0_PAGEMASK);
  594. }
  595. }
  596. }
  597. static void build_huge_tlb_write_entry(u32 **p, struct uasm_label **l,
  598. struct uasm_reloc **r,
  599. unsigned int tmp,
  600. enum tlb_write_entry wmode,
  601. int restore_scratch)
  602. {
  603. /* Set huge page tlb entry size */
  604. uasm_i_lui(p, tmp, PM_HUGE_MASK >> 16);
  605. uasm_i_ori(p, tmp, tmp, PM_HUGE_MASK & 0xffff);
  606. uasm_i_mtc0(p, tmp, C0_PAGEMASK);
  607. build_tlb_write_entry(p, l, r, wmode);
  608. build_restore_pagemask(p, r, tmp, label_leave, restore_scratch);
  609. }
  610. /*
  611. * Check if Huge PTE is present, if so then jump to LABEL.
  612. */
  613. static void
  614. build_is_huge_pte(u32 **p, struct uasm_reloc **r, unsigned int tmp,
  615. unsigned int pmd, int lid)
  616. {
  617. UASM_i_LW(p, tmp, 0, pmd);
  618. if (use_bbit_insns()) {
  619. uasm_il_bbit1(p, r, tmp, ilog2(_PAGE_HUGE), lid);
  620. } else {
  621. uasm_i_andi(p, tmp, tmp, _PAGE_HUGE);
  622. uasm_il_bnez(p, r, tmp, lid);
  623. }
  624. }
  625. static void build_huge_update_entries(u32 **p, unsigned int pte,
  626. unsigned int tmp)
  627. {
  628. int small_sequence;
  629. /*
  630. * A huge PTE describes an area the size of the
  631. * configured huge page size. This is twice the
  632. * of the large TLB entry size we intend to use.
  633. * A TLB entry half the size of the configured
  634. * huge page size is configured into entrylo0
  635. * and entrylo1 to cover the contiguous huge PTE
  636. * address space.
  637. */
  638. small_sequence = (HPAGE_SIZE >> 7) < 0x10000;
  639. /* We can clobber tmp. It isn't used after this.*/
  640. if (!small_sequence)
  641. uasm_i_lui(p, tmp, HPAGE_SIZE >> (7 + 16));
  642. build_convert_pte_to_entrylo(p, pte);
  643. UASM_i_MTC0(p, pte, C0_ENTRYLO0); /* load it */
  644. /* convert to entrylo1 */
  645. if (small_sequence)
  646. UASM_i_ADDIU(p, pte, pte, HPAGE_SIZE >> 7);
  647. else
  648. UASM_i_ADDU(p, pte, pte, tmp);
  649. UASM_i_MTC0(p, pte, C0_ENTRYLO1); /* load it */
  650. }
  651. static void build_huge_handler_tail(u32 **p, struct uasm_reloc **r,
  652. struct uasm_label **l,
  653. unsigned int pte,
  654. unsigned int ptr,
  655. unsigned int flush)
  656. {
  657. #ifdef CONFIG_SMP
  658. UASM_i_SC(p, pte, 0, ptr);
  659. uasm_il_beqz(p, r, pte, label_tlb_huge_update);
  660. UASM_i_LW(p, pte, 0, ptr); /* Needed because SC killed our PTE */
  661. #else
  662. UASM_i_SW(p, pte, 0, ptr);
  663. #endif
  664. if (cpu_has_ftlb && flush) {
  665. BUG_ON(!cpu_has_tlbinv);
  666. UASM_i_MFC0(p, ptr, C0_ENTRYHI);
  667. uasm_i_ori(p, ptr, ptr, MIPS_ENTRYHI_EHINV);
  668. UASM_i_MTC0(p, ptr, C0_ENTRYHI);
  669. build_tlb_write_entry(p, l, r, tlb_indexed);
  670. uasm_i_xori(p, ptr, ptr, MIPS_ENTRYHI_EHINV);
  671. UASM_i_MTC0(p, ptr, C0_ENTRYHI);
  672. build_huge_update_entries(p, pte, ptr);
  673. build_huge_tlb_write_entry(p, l, r, pte, tlb_random, 0);
  674. return;
  675. }
  676. build_huge_update_entries(p, pte, ptr);
  677. build_huge_tlb_write_entry(p, l, r, pte, tlb_indexed, 0);
  678. }
  679. #endif /* CONFIG_MIPS_HUGE_TLB_SUPPORT */
  680. #ifdef CONFIG_64BIT
  681. /*
  682. * TMP and PTR are scratch.
  683. * TMP will be clobbered, PTR will hold the pmd entry.
  684. */
  685. void build_get_pmde64(u32 **p, struct uasm_label **l, struct uasm_reloc **r,
  686. unsigned int tmp, unsigned int ptr)
  687. {
  688. #ifndef CONFIG_MIPS_PGD_C0_CONTEXT
  689. long pgdc = (long)pgd_current;
  690. #endif
  691. /*
  692. * The vmalloc handling is not in the hotpath.
  693. */
  694. uasm_i_dmfc0(p, tmp, C0_BADVADDR);
  695. if (check_for_high_segbits) {
  696. /*
  697. * The kernel currently implicitely assumes that the
  698. * MIPS SEGBITS parameter for the processor is
  699. * (PGDIR_SHIFT+PGDIR_BITS) or less, and will never
  700. * allocate virtual addresses outside the maximum
  701. * range for SEGBITS = (PGDIR_SHIFT+PGDIR_BITS). But
  702. * that doesn't prevent user code from accessing the
  703. * higher xuseg addresses. Here, we make sure that
  704. * everything but the lower xuseg addresses goes down
  705. * the module_alloc/vmalloc path.
  706. */
  707. uasm_i_dsrl_safe(p, ptr, tmp, PGDIR_SHIFT + PGD_TABLE_ORDER + PAGE_SHIFT - 3);
  708. uasm_il_bnez(p, r, ptr, label_vmalloc);
  709. } else {
  710. uasm_il_bltz(p, r, tmp, label_vmalloc);
  711. }
  712. /* No uasm_i_nop needed here, since the next insn doesn't touch TMP. */
  713. if (pgd_reg != -1) {
  714. /* pgd is in pgd_reg */
  715. if (cpu_has_ldpte)
  716. UASM_i_MFC0(p, ptr, C0_PWBASE);
  717. else
  718. UASM_i_MFC0(p, ptr, c0_kscratch(), pgd_reg);
  719. } else {
  720. #if defined(CONFIG_MIPS_PGD_C0_CONTEXT)
  721. /*
  722. * &pgd << 11 stored in CONTEXT [23..63].
  723. */
  724. UASM_i_MFC0(p, ptr, C0_CONTEXT);
  725. /* Clear lower 23 bits of context. */
  726. uasm_i_dins(p, ptr, 0, 0, 23);
  727. /* insert bit[63:59] of CAC_BASE into bit[11:6] of ptr */
  728. uasm_i_ori(p, ptr, ptr, ((u64)(CAC_BASE) >> 53));
  729. uasm_i_drotr(p, ptr, ptr, 11);
  730. #elif defined(CONFIG_SMP)
  731. UASM_i_CPUID_MFC0(p, ptr, SMP_CPUID_REG);
  732. uasm_i_dsrl_safe(p, ptr, ptr, SMP_CPUID_PTRSHIFT);
  733. UASM_i_LA_mostly(p, tmp, pgdc);
  734. uasm_i_daddu(p, ptr, ptr, tmp);
  735. uasm_i_dmfc0(p, tmp, C0_BADVADDR);
  736. uasm_i_ld(p, ptr, uasm_rel_lo(pgdc), ptr);
  737. #else
  738. UASM_i_LA_mostly(p, ptr, pgdc);
  739. uasm_i_ld(p, ptr, uasm_rel_lo(pgdc), ptr);
  740. #endif
  741. }
  742. uasm_l_vmalloc_done(l, *p);
  743. /* get pgd offset in bytes */
  744. uasm_i_dsrl_safe(p, tmp, tmp, PGDIR_SHIFT - 3);
  745. uasm_i_andi(p, tmp, tmp, (PTRS_PER_PGD - 1)<<3);
  746. uasm_i_daddu(p, ptr, ptr, tmp); /* add in pgd offset */
  747. #ifndef __PAGETABLE_PUD_FOLDED
  748. uasm_i_dmfc0(p, tmp, C0_BADVADDR); /* get faulting address */
  749. uasm_i_ld(p, ptr, 0, ptr); /* get pud pointer */
  750. uasm_i_dsrl_safe(p, tmp, tmp, PUD_SHIFT - 3); /* get pud offset in bytes */
  751. uasm_i_andi(p, tmp, tmp, (PTRS_PER_PUD - 1) << 3);
  752. uasm_i_daddu(p, ptr, ptr, tmp); /* add in pud offset */
  753. #endif
  754. #ifndef __PAGETABLE_PMD_FOLDED
  755. uasm_i_dmfc0(p, tmp, C0_BADVADDR); /* get faulting address */
  756. uasm_i_ld(p, ptr, 0, ptr); /* get pmd pointer */
  757. uasm_i_dsrl_safe(p, tmp, tmp, PMD_SHIFT-3); /* get pmd offset in bytes */
  758. uasm_i_andi(p, tmp, tmp, (PTRS_PER_PMD - 1)<<3);
  759. uasm_i_daddu(p, ptr, ptr, tmp); /* add in pmd offset */
  760. #endif
  761. }
  762. EXPORT_SYMBOL_GPL(build_get_pmde64);
  763. /*
  764. * BVADDR is the faulting address, PTR is scratch.
  765. * PTR will hold the pgd for vmalloc.
  766. */
  767. static void
  768. build_get_pgd_vmalloc64(u32 **p, struct uasm_label **l, struct uasm_reloc **r,
  769. unsigned int bvaddr, unsigned int ptr,
  770. enum vmalloc64_mode mode)
  771. {
  772. long swpd = (long)swapper_pg_dir;
  773. int single_insn_swpd;
  774. int did_vmalloc_branch = 0;
  775. single_insn_swpd = uasm_in_compat_space_p(swpd) && !uasm_rel_lo(swpd);
  776. uasm_l_vmalloc(l, *p);
  777. if (mode != not_refill && check_for_high_segbits) {
  778. if (single_insn_swpd) {
  779. uasm_il_bltz(p, r, bvaddr, label_vmalloc_done);
  780. uasm_i_lui(p, ptr, uasm_rel_hi(swpd));
  781. did_vmalloc_branch = 1;
  782. /* fall through */
  783. } else {
  784. uasm_il_bgez(p, r, bvaddr, label_large_segbits_fault);
  785. }
  786. }
  787. if (!did_vmalloc_branch) {
  788. if (single_insn_swpd) {
  789. uasm_il_b(p, r, label_vmalloc_done);
  790. uasm_i_lui(p, ptr, uasm_rel_hi(swpd));
  791. } else {
  792. UASM_i_LA_mostly(p, ptr, swpd);
  793. uasm_il_b(p, r, label_vmalloc_done);
  794. if (uasm_in_compat_space_p(swpd))
  795. uasm_i_addiu(p, ptr, ptr, uasm_rel_lo(swpd));
  796. else
  797. uasm_i_daddiu(p, ptr, ptr, uasm_rel_lo(swpd));
  798. }
  799. }
  800. if (mode != not_refill && check_for_high_segbits) {
  801. uasm_l_large_segbits_fault(l, *p);
  802. if (mode == refill_scratch && scratch_reg >= 0)
  803. uasm_i_ehb(p);
  804. /*
  805. * We get here if we are an xsseg address, or if we are
  806. * an xuseg address above (PGDIR_SHIFT+PGDIR_BITS) boundary.
  807. *
  808. * Ignoring xsseg (assume disabled so would generate
  809. * (address errors?), the only remaining possibility
  810. * is the upper xuseg addresses. On processors with
  811. * TLB_SEGBITS <= PGDIR_SHIFT+PGDIR_BITS, these
  812. * addresses would have taken an address error. We try
  813. * to mimic that here by taking a load/istream page
  814. * fault.
  815. */
  816. if (IS_ENABLED(CONFIG_CPU_LOONGSON3_WORKAROUNDS))
  817. uasm_i_sync(p, 0);
  818. UASM_i_LA(p, ptr, (unsigned long)tlb_do_page_fault_0);
  819. uasm_i_jr(p, ptr);
  820. if (mode == refill_scratch) {
  821. if (scratch_reg >= 0)
  822. UASM_i_MFC0(p, 1, c0_kscratch(), scratch_reg);
  823. else
  824. UASM_i_LW(p, 1, scratchpad_offset(0), 0);
  825. } else {
  826. uasm_i_nop(p);
  827. }
  828. }
  829. }
  830. #else /* !CONFIG_64BIT */
  831. /*
  832. * TMP and PTR are scratch.
  833. * TMP will be clobbered, PTR will hold the pgd entry.
  834. */
  835. void build_get_pgde32(u32 **p, unsigned int tmp, unsigned int ptr)
  836. {
  837. if (pgd_reg != -1) {
  838. /* pgd is in pgd_reg */
  839. uasm_i_mfc0(p, ptr, c0_kscratch(), pgd_reg);
  840. uasm_i_mfc0(p, tmp, C0_BADVADDR); /* get faulting address */
  841. } else {
  842. long pgdc = (long)pgd_current;
  843. /* 32 bit SMP has smp_processor_id() stored in CONTEXT. */
  844. #ifdef CONFIG_SMP
  845. uasm_i_mfc0(p, ptr, SMP_CPUID_REG);
  846. UASM_i_LA_mostly(p, tmp, pgdc);
  847. uasm_i_srl(p, ptr, ptr, SMP_CPUID_PTRSHIFT);
  848. uasm_i_addu(p, ptr, tmp, ptr);
  849. #else
  850. UASM_i_LA_mostly(p, ptr, pgdc);
  851. #endif
  852. uasm_i_mfc0(p, tmp, C0_BADVADDR); /* get faulting address */
  853. uasm_i_lw(p, ptr, uasm_rel_lo(pgdc), ptr);
  854. }
  855. uasm_i_srl(p, tmp, tmp, PGDIR_SHIFT); /* get pgd only bits */
  856. uasm_i_sll(p, tmp, tmp, PGD_T_LOG2);
  857. uasm_i_addu(p, ptr, ptr, tmp); /* add in pgd offset */
  858. }
  859. EXPORT_SYMBOL_GPL(build_get_pgde32);
  860. #endif /* !CONFIG_64BIT */
  861. static void build_adjust_context(u32 **p, unsigned int ctx)
  862. {
  863. unsigned int shift = 4 - (PTE_T_LOG2 + 1) + PAGE_SHIFT - 12;
  864. unsigned int mask = (PTRS_PER_PTE / 2 - 1) << (PTE_T_LOG2 + 1);
  865. if (shift)
  866. UASM_i_SRL(p, ctx, ctx, shift);
  867. uasm_i_andi(p, ctx, ctx, mask);
  868. }
  869. void build_get_ptep(u32 **p, unsigned int tmp, unsigned int ptr)
  870. {
  871. /*
  872. * Bug workaround for the Nevada. It seems as if under certain
  873. * circumstances the move from cp0_context might produce a
  874. * bogus result when the mfc0 instruction and its consumer are
  875. * in a different cacheline or a load instruction, probably any
  876. * memory reference, is between them.
  877. */
  878. switch (current_cpu_type()) {
  879. case CPU_NEVADA:
  880. UASM_i_LW(p, ptr, 0, ptr);
  881. GET_CONTEXT(p, tmp); /* get context reg */
  882. break;
  883. default:
  884. GET_CONTEXT(p, tmp); /* get context reg */
  885. UASM_i_LW(p, ptr, 0, ptr);
  886. break;
  887. }
  888. build_adjust_context(p, tmp);
  889. UASM_i_ADDU(p, ptr, ptr, tmp); /* add in offset */
  890. }
  891. EXPORT_SYMBOL_GPL(build_get_ptep);
  892. void build_update_entries(u32 **p, unsigned int tmp, unsigned int ptep)
  893. {
  894. int pte_off_even = 0;
  895. int pte_off_odd = sizeof(pte_t);
  896. #if defined(CONFIG_CPU_MIPS32) && defined(CONFIG_PHYS_ADDR_T_64BIT)
  897. /* The low 32 bits of EntryLo is stored in pte_high */
  898. pte_off_even += offsetof(pte_t, pte_high);
  899. pte_off_odd += offsetof(pte_t, pte_high);
  900. #endif
  901. if (IS_ENABLED(CONFIG_XPA)) {
  902. uasm_i_lw(p, tmp, pte_off_even, ptep); /* even pte */
  903. UASM_i_ROTR(p, tmp, tmp, ilog2(_PAGE_GLOBAL));
  904. UASM_i_MTC0(p, tmp, C0_ENTRYLO0);
  905. if (cpu_has_xpa && !mips_xpa_disabled) {
  906. uasm_i_lw(p, tmp, 0, ptep);
  907. uasm_i_ext(p, tmp, tmp, 0, 24);
  908. uasm_i_mthc0(p, tmp, C0_ENTRYLO0);
  909. }
  910. uasm_i_lw(p, tmp, pte_off_odd, ptep); /* odd pte */
  911. UASM_i_ROTR(p, tmp, tmp, ilog2(_PAGE_GLOBAL));
  912. UASM_i_MTC0(p, tmp, C0_ENTRYLO1);
  913. if (cpu_has_xpa && !mips_xpa_disabled) {
  914. uasm_i_lw(p, tmp, sizeof(pte_t), ptep);
  915. uasm_i_ext(p, tmp, tmp, 0, 24);
  916. uasm_i_mthc0(p, tmp, C0_ENTRYLO1);
  917. }
  918. return;
  919. }
  920. UASM_i_LW(p, tmp, pte_off_even, ptep); /* get even pte */
  921. UASM_i_LW(p, ptep, pte_off_odd, ptep); /* get odd pte */
  922. if (r45k_bvahwbug())
  923. build_tlb_probe_entry(p);
  924. build_convert_pte_to_entrylo(p, tmp);
  925. if (r4k_250MHZhwbug())
  926. UASM_i_MTC0(p, 0, C0_ENTRYLO0);
  927. UASM_i_MTC0(p, tmp, C0_ENTRYLO0); /* load it */
  928. build_convert_pte_to_entrylo(p, ptep);
  929. if (r45k_bvahwbug())
  930. uasm_i_mfc0(p, tmp, C0_INDEX);
  931. if (r4k_250MHZhwbug())
  932. UASM_i_MTC0(p, 0, C0_ENTRYLO1);
  933. UASM_i_MTC0(p, ptep, C0_ENTRYLO1); /* load it */
  934. }
  935. EXPORT_SYMBOL_GPL(build_update_entries);
  936. struct mips_huge_tlb_info {
  937. int huge_pte;
  938. int restore_scratch;
  939. bool need_reload_pte;
  940. };
  941. static struct mips_huge_tlb_info
  942. build_fast_tlb_refill_handler (u32 **p, struct uasm_label **l,
  943. struct uasm_reloc **r, unsigned int tmp,
  944. unsigned int ptr, int c0_scratch_reg)
  945. {
  946. struct mips_huge_tlb_info rv;
  947. unsigned int even, odd;
  948. int vmalloc_branch_delay_filled = 0;
  949. const int scratch = 1; /* Our extra working register */
  950. rv.huge_pte = scratch;
  951. rv.restore_scratch = 0;
  952. rv.need_reload_pte = false;
  953. if (check_for_high_segbits) {
  954. UASM_i_MFC0(p, tmp, C0_BADVADDR);
  955. if (pgd_reg != -1)
  956. UASM_i_MFC0(p, ptr, c0_kscratch(), pgd_reg);
  957. else
  958. UASM_i_MFC0(p, ptr, C0_CONTEXT);
  959. if (c0_scratch_reg >= 0)
  960. UASM_i_MTC0(p, scratch, c0_kscratch(), c0_scratch_reg);
  961. else
  962. UASM_i_SW(p, scratch, scratchpad_offset(0), 0);
  963. uasm_i_dsrl_safe(p, scratch, tmp,
  964. PGDIR_SHIFT + PGD_TABLE_ORDER + PAGE_SHIFT - 3);
  965. uasm_il_bnez(p, r, scratch, label_vmalloc);
  966. if (pgd_reg == -1) {
  967. vmalloc_branch_delay_filled = 1;
  968. /* Clear lower 23 bits of context. */
  969. uasm_i_dins(p, ptr, 0, 0, 23);
  970. }
  971. } else {
  972. if (pgd_reg != -1)
  973. UASM_i_MFC0(p, ptr, c0_kscratch(), pgd_reg);
  974. else
  975. UASM_i_MFC0(p, ptr, C0_CONTEXT);
  976. UASM_i_MFC0(p, tmp, C0_BADVADDR);
  977. if (c0_scratch_reg >= 0)
  978. UASM_i_MTC0(p, scratch, c0_kscratch(), c0_scratch_reg);
  979. else
  980. UASM_i_SW(p, scratch, scratchpad_offset(0), 0);
  981. if (pgd_reg == -1)
  982. /* Clear lower 23 bits of context. */
  983. uasm_i_dins(p, ptr, 0, 0, 23);
  984. uasm_il_bltz(p, r, tmp, label_vmalloc);
  985. }
  986. if (pgd_reg == -1) {
  987. vmalloc_branch_delay_filled = 1;
  988. /* insert bit[63:59] of CAC_BASE into bit[11:6] of ptr */
  989. uasm_i_ori(p, ptr, ptr, ((u64)(CAC_BASE) >> 53));
  990. uasm_i_drotr(p, ptr, ptr, 11);
  991. }
  992. #ifdef __PAGETABLE_PMD_FOLDED
  993. #define LOC_PTEP scratch
  994. #else
  995. #define LOC_PTEP ptr
  996. #endif
  997. if (!vmalloc_branch_delay_filled)
  998. /* get pgd offset in bytes */
  999. uasm_i_dsrl_safe(p, scratch, tmp, PGDIR_SHIFT - 3);
  1000. uasm_l_vmalloc_done(l, *p);
  1001. /*
  1002. * tmp ptr
  1003. * fall-through case = badvaddr *pgd_current
  1004. * vmalloc case = badvaddr swapper_pg_dir
  1005. */
  1006. if (vmalloc_branch_delay_filled)
  1007. /* get pgd offset in bytes */
  1008. uasm_i_dsrl_safe(p, scratch, tmp, PGDIR_SHIFT - 3);
  1009. #ifdef __PAGETABLE_PMD_FOLDED
  1010. GET_CONTEXT(p, tmp); /* get context reg */
  1011. #endif
  1012. uasm_i_andi(p, scratch, scratch, (PTRS_PER_PGD - 1) << 3);
  1013. if (use_lwx_insns()) {
  1014. UASM_i_LWX(p, LOC_PTEP, scratch, ptr);
  1015. } else {
  1016. uasm_i_daddu(p, ptr, ptr, scratch); /* add in pgd offset */
  1017. uasm_i_ld(p, LOC_PTEP, 0, ptr); /* get pmd pointer */
  1018. }
  1019. #ifndef __PAGETABLE_PUD_FOLDED
  1020. /* get pud offset in bytes */
  1021. uasm_i_dsrl_safe(p, scratch, tmp, PUD_SHIFT - 3);
  1022. uasm_i_andi(p, scratch, scratch, (PTRS_PER_PUD - 1) << 3);
  1023. if (use_lwx_insns()) {
  1024. UASM_i_LWX(p, ptr, scratch, ptr);
  1025. } else {
  1026. uasm_i_daddu(p, ptr, ptr, scratch); /* add in pmd offset */
  1027. UASM_i_LW(p, ptr, 0, ptr);
  1028. }
  1029. /* ptr contains a pointer to PMD entry */
  1030. /* tmp contains the address */
  1031. #endif
  1032. #ifndef __PAGETABLE_PMD_FOLDED
  1033. /* get pmd offset in bytes */
  1034. uasm_i_dsrl_safe(p, scratch, tmp, PMD_SHIFT - 3);
  1035. uasm_i_andi(p, scratch, scratch, (PTRS_PER_PMD - 1) << 3);
  1036. GET_CONTEXT(p, tmp); /* get context reg */
  1037. if (use_lwx_insns()) {
  1038. UASM_i_LWX(p, scratch, scratch, ptr);
  1039. } else {
  1040. uasm_i_daddu(p, ptr, ptr, scratch); /* add in pmd offset */
  1041. UASM_i_LW(p, scratch, 0, ptr);
  1042. }
  1043. #endif
  1044. /* Adjust the context during the load latency. */
  1045. build_adjust_context(p, tmp);
  1046. #ifdef CONFIG_MIPS_HUGE_TLB_SUPPORT
  1047. uasm_il_bbit1(p, r, scratch, ilog2(_PAGE_HUGE), label_tlb_huge_update);
  1048. /*
  1049. * The in the LWX case we don't want to do the load in the
  1050. * delay slot. It cannot issue in the same cycle and may be
  1051. * speculative and unneeded.
  1052. */
  1053. if (use_lwx_insns())
  1054. uasm_i_nop(p);
  1055. #endif /* CONFIG_MIPS_HUGE_TLB_SUPPORT */
  1056. /* build_update_entries */
  1057. if (use_lwx_insns()) {
  1058. even = ptr;
  1059. odd = tmp;
  1060. UASM_i_LWX(p, even, scratch, tmp);
  1061. UASM_i_ADDIU(p, tmp, tmp, sizeof(pte_t));
  1062. UASM_i_LWX(p, odd, scratch, tmp);
  1063. } else {
  1064. UASM_i_ADDU(p, ptr, scratch, tmp); /* add in offset */
  1065. even = tmp;
  1066. odd = ptr;
  1067. UASM_i_LW(p, even, 0, ptr); /* get even pte */
  1068. UASM_i_LW(p, odd, sizeof(pte_t), ptr); /* get odd pte */
  1069. }
  1070. if (cpu_has_rixi) {
  1071. uasm_i_drotr(p, even, even, ilog2(_PAGE_GLOBAL));
  1072. UASM_i_MTC0(p, even, C0_ENTRYLO0); /* load it */
  1073. uasm_i_drotr(p, odd, odd, ilog2(_PAGE_GLOBAL));
  1074. } else {
  1075. uasm_i_dsrl_safe(p, even, even, ilog2(_PAGE_GLOBAL));
  1076. UASM_i_MTC0(p, even, C0_ENTRYLO0); /* load it */
  1077. uasm_i_dsrl_safe(p, odd, odd, ilog2(_PAGE_GLOBAL));
  1078. }
  1079. UASM_i_MTC0(p, odd, C0_ENTRYLO1); /* load it */
  1080. if (c0_scratch_reg >= 0) {
  1081. uasm_i_ehb(p);
  1082. UASM_i_MFC0(p, scratch, c0_kscratch(), c0_scratch_reg);
  1083. build_tlb_write_entry(p, l, r, tlb_random);
  1084. uasm_l_leave(l, *p);
  1085. rv.restore_scratch = 1;
  1086. } else if (PAGE_SHIFT == 14 || PAGE_SHIFT == 13) {
  1087. build_tlb_write_entry(p, l, r, tlb_random);
  1088. uasm_l_leave(l, *p);
  1089. UASM_i_LW(p, scratch, scratchpad_offset(0), 0);
  1090. } else {
  1091. UASM_i_LW(p, scratch, scratchpad_offset(0), 0);
  1092. build_tlb_write_entry(p, l, r, tlb_random);
  1093. uasm_l_leave(l, *p);
  1094. rv.restore_scratch = 1;
  1095. }
  1096. uasm_i_eret(p); /* return from trap */
  1097. return rv;
  1098. }
  1099. /*
  1100. * For a 64-bit kernel, we are using the 64-bit XTLB refill exception
  1101. * because EXL == 0. If we wrap, we can also use the 32 instruction
  1102. * slots before the XTLB refill exception handler which belong to the
  1103. * unused TLB refill exception.
  1104. */
  1105. #define MIPS64_REFILL_INSNS 32
  1106. static void build_r4000_tlb_refill_handler(void)
  1107. {
  1108. u32 *p = tlb_handler;
  1109. struct uasm_label *l = labels;
  1110. struct uasm_reloc *r = relocs;
  1111. u32 *f;
  1112. unsigned int final_len;
  1113. struct mips_huge_tlb_info htlb_info __maybe_unused;
  1114. enum vmalloc64_mode vmalloc_mode __maybe_unused;
  1115. memset(tlb_handler, 0, sizeof(tlb_handler));
  1116. memset(labels, 0, sizeof(labels));
  1117. memset(relocs, 0, sizeof(relocs));
  1118. memset(final_handler, 0, sizeof(final_handler));
  1119. if (IS_ENABLED(CONFIG_64BIT) && (scratch_reg >= 0 || scratchpad_available()) && use_bbit_insns()) {
  1120. htlb_info = build_fast_tlb_refill_handler(&p, &l, &r, K0, K1,
  1121. scratch_reg);
  1122. vmalloc_mode = refill_scratch;
  1123. } else {
  1124. htlb_info.huge_pte = K0;
  1125. htlb_info.restore_scratch = 0;
  1126. htlb_info.need_reload_pte = true;
  1127. vmalloc_mode = refill_noscratch;
  1128. /*
  1129. * create the plain linear handler
  1130. */
  1131. if (bcm1250_m3_war()) {
  1132. unsigned int segbits = 44;
  1133. uasm_i_dmfc0(&p, K0, C0_BADVADDR);
  1134. uasm_i_dmfc0(&p, K1, C0_ENTRYHI);
  1135. uasm_i_xor(&p, K0, K0, K1);
  1136. uasm_i_dsrl_safe(&p, K1, K0, 62);
  1137. uasm_i_dsrl_safe(&p, K0, K0, 12 + 1);
  1138. uasm_i_dsll_safe(&p, K0, K0, 64 + 12 + 1 - segbits);
  1139. uasm_i_or(&p, K0, K0, K1);
  1140. uasm_il_bnez(&p, &r, K0, label_leave);
  1141. /* No need for uasm_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. #ifdef CONFIG_MIPS_HUGE_TLB_SUPPORT
  1149. build_is_huge_pte(&p, &r, K0, K1, label_tlb_huge_update);
  1150. #endif
  1151. build_get_ptep(&p, K0, K1);
  1152. build_update_entries(&p, K0, K1);
  1153. build_tlb_write_entry(&p, &l, &r, tlb_random);
  1154. uasm_l_leave(&l, p);
  1155. uasm_i_eret(&p); /* return from trap */
  1156. }
  1157. #ifdef CONFIG_MIPS_HUGE_TLB_SUPPORT
  1158. uasm_l_tlb_huge_update(&l, p);
  1159. if (htlb_info.need_reload_pte)
  1160. UASM_i_LW(&p, htlb_info.huge_pte, 0, K1);
  1161. build_huge_update_entries(&p, htlb_info.huge_pte, K1);
  1162. build_huge_tlb_write_entry(&p, &l, &r, K0, tlb_random,
  1163. htlb_info.restore_scratch);
  1164. #endif
  1165. #ifdef CONFIG_64BIT
  1166. build_get_pgd_vmalloc64(&p, &l, &r, K0, K1, vmalloc_mode);
  1167. #endif
  1168. /*
  1169. * Overflow check: For the 64bit handler, we need at least one
  1170. * free instruction slot for the wrap-around branch. In worst
  1171. * case, if the intended insertion point is a delay slot, we
  1172. * need three, with the second nop'ed and the third being
  1173. * unused.
  1174. */
  1175. switch (boot_cpu_type()) {
  1176. default:
  1177. if (sizeof(long) == 4) {
  1178. fallthrough;
  1179. case CPU_LOONGSON2EF:
  1180. /* Loongson2 ebase is different than r4k, we have more space */
  1181. if ((p - tlb_handler) > 64)
  1182. panic("TLB refill handler space exceeded");
  1183. /*
  1184. * Now fold the handler in the TLB refill handler space.
  1185. */
  1186. f = final_handler;
  1187. /* Simplest case, just copy the handler. */
  1188. uasm_copy_handler(relocs, labels, tlb_handler, p, f);
  1189. final_len = p - tlb_handler;
  1190. break;
  1191. } else {
  1192. if (((p - tlb_handler) > (MIPS64_REFILL_INSNS * 2) - 1)
  1193. || (((p - tlb_handler) > (MIPS64_REFILL_INSNS * 2) - 3)
  1194. && uasm_insn_has_bdelay(relocs,
  1195. tlb_handler + MIPS64_REFILL_INSNS - 3)))
  1196. panic("TLB refill handler space exceeded");
  1197. /*
  1198. * Now fold the handler in the TLB refill handler space.
  1199. */
  1200. f = final_handler + MIPS64_REFILL_INSNS;
  1201. if ((p - tlb_handler) <= MIPS64_REFILL_INSNS) {
  1202. /* Just copy the handler. */
  1203. uasm_copy_handler(relocs, labels, tlb_handler, p, f);
  1204. final_len = p - tlb_handler;
  1205. } else {
  1206. #ifdef CONFIG_MIPS_HUGE_TLB_SUPPORT
  1207. const enum label_id ls = label_tlb_huge_update;
  1208. #else
  1209. const enum label_id ls = label_vmalloc;
  1210. #endif
  1211. u32 *split;
  1212. int ov = 0;
  1213. int i;
  1214. for (i = 0; i < ARRAY_SIZE(labels) && labels[i].lab != ls; i++)
  1215. ;
  1216. BUG_ON(i == ARRAY_SIZE(labels));
  1217. split = labels[i].addr;
  1218. /*
  1219. * See if we have overflown one way or the other.
  1220. */
  1221. if (split > tlb_handler + MIPS64_REFILL_INSNS ||
  1222. split < p - MIPS64_REFILL_INSNS)
  1223. ov = 1;
  1224. if (ov) {
  1225. /*
  1226. * Split two instructions before the end. One
  1227. * for the branch and one for the instruction
  1228. * in the delay slot.
  1229. */
  1230. split = tlb_handler + MIPS64_REFILL_INSNS - 2;
  1231. /*
  1232. * If the branch would fall in a delay slot,
  1233. * we must back up an additional instruction
  1234. * so that it is no longer in a delay slot.
  1235. */
  1236. if (uasm_insn_has_bdelay(relocs, split - 1))
  1237. split--;
  1238. }
  1239. /* Copy first part of the handler. */
  1240. uasm_copy_handler(relocs, labels, tlb_handler, split, f);
  1241. f += split - tlb_handler;
  1242. if (ov) {
  1243. /* Insert branch. */
  1244. uasm_l_split(&l, final_handler);
  1245. uasm_il_b(&f, &r, label_split);
  1246. if (uasm_insn_has_bdelay(relocs, split))
  1247. uasm_i_nop(&f);
  1248. else {
  1249. uasm_copy_handler(relocs, labels,
  1250. split, split + 1, f);
  1251. uasm_move_labels(labels, f, f + 1, -1);
  1252. f++;
  1253. split++;
  1254. }
  1255. }
  1256. /* Copy the rest of the handler. */
  1257. uasm_copy_handler(relocs, labels, split, p, final_handler);
  1258. final_len = (f - (final_handler + MIPS64_REFILL_INSNS)) +
  1259. (p - split);
  1260. }
  1261. }
  1262. break;
  1263. }
  1264. uasm_resolve_relocs(relocs, labels);
  1265. pr_debug("Wrote TLB refill handler (%u instructions).\n",
  1266. final_len);
  1267. memcpy((void *)ebase, final_handler, 0x100);
  1268. local_flush_icache_range(ebase, ebase + 0x100);
  1269. dump_handler("r4000_tlb_refill", (u32 *)ebase, (u32 *)(ebase + 0x100));
  1270. }
  1271. static void setup_pw(void)
  1272. {
  1273. unsigned int pwctl;
  1274. unsigned long pgd_i, pgd_w;
  1275. #ifndef __PAGETABLE_PMD_FOLDED
  1276. unsigned long pmd_i, pmd_w;
  1277. #endif
  1278. unsigned long pt_i, pt_w;
  1279. unsigned long pte_i, pte_w;
  1280. #ifdef CONFIG_MIPS_HUGE_TLB_SUPPORT
  1281. unsigned long psn;
  1282. psn = ilog2(_PAGE_HUGE); /* bit used to indicate huge page */
  1283. #endif
  1284. pgd_i = PGDIR_SHIFT; /* 1st level PGD */
  1285. #ifndef __PAGETABLE_PMD_FOLDED
  1286. pgd_w = PGDIR_SHIFT - PMD_SHIFT + PGD_TABLE_ORDER;
  1287. pmd_i = PMD_SHIFT; /* 2nd level PMD */
  1288. pmd_w = PMD_SHIFT - PAGE_SHIFT;
  1289. #else
  1290. pgd_w = PGDIR_SHIFT - PAGE_SHIFT + PGD_TABLE_ORDER;
  1291. #endif
  1292. pt_i = PAGE_SHIFT; /* 3rd level PTE */
  1293. pt_w = PAGE_SHIFT - 3;
  1294. pte_i = ilog2(_PAGE_GLOBAL);
  1295. pte_w = 0;
  1296. pwctl = 1 << 30; /* Set PWDirExt */
  1297. #ifndef __PAGETABLE_PMD_FOLDED
  1298. write_c0_pwfield(pgd_i << 24 | pmd_i << 12 | pt_i << 6 | pte_i);
  1299. write_c0_pwsize(1 << 30 | pgd_w << 24 | pmd_w << 12 | pt_w << 6 | pte_w);
  1300. #else
  1301. write_c0_pwfield(pgd_i << 24 | pt_i << 6 | pte_i);
  1302. write_c0_pwsize(1 << 30 | pgd_w << 24 | pt_w << 6 | pte_w);
  1303. #endif
  1304. #ifdef CONFIG_MIPS_HUGE_TLB_SUPPORT
  1305. pwctl |= (1 << 6 | psn);
  1306. #endif
  1307. write_c0_pwctl(pwctl);
  1308. write_c0_kpgd((long)swapper_pg_dir);
  1309. kscratch_used_mask |= (1 << 7); /* KScratch6 is used for KPGD */
  1310. }
  1311. static void build_loongson3_tlb_refill_handler(void)
  1312. {
  1313. u32 *p = tlb_handler;
  1314. struct uasm_label *l = labels;
  1315. struct uasm_reloc *r = relocs;
  1316. memset(labels, 0, sizeof(labels));
  1317. memset(relocs, 0, sizeof(relocs));
  1318. memset(tlb_handler, 0, sizeof(tlb_handler));
  1319. if (check_for_high_segbits) {
  1320. uasm_i_dmfc0(&p, K0, C0_BADVADDR);
  1321. uasm_i_dsrl_safe(&p, K1, K0, PGDIR_SHIFT + PGD_TABLE_ORDER + PAGE_SHIFT - 3);
  1322. uasm_il_beqz(&p, &r, K1, label_vmalloc);
  1323. uasm_i_nop(&p);
  1324. uasm_il_bgez(&p, &r, K0, label_large_segbits_fault);
  1325. uasm_i_nop(&p);
  1326. uasm_l_vmalloc(&l, p);
  1327. }
  1328. uasm_i_dmfc0(&p, K1, C0_PGD);
  1329. uasm_i_lddir(&p, K0, K1, 3); /* global page dir */
  1330. #ifndef __PAGETABLE_PMD_FOLDED
  1331. uasm_i_lddir(&p, K1, K0, 1); /* middle page dir */
  1332. #endif
  1333. uasm_i_ldpte(&p, K1, 0); /* even */
  1334. uasm_i_ldpte(&p, K1, 1); /* odd */
  1335. uasm_i_tlbwr(&p);
  1336. /* restore page mask */
  1337. if (PM_DEFAULT_MASK >> 16) {
  1338. uasm_i_lui(&p, K0, PM_DEFAULT_MASK >> 16);
  1339. uasm_i_ori(&p, K0, K0, PM_DEFAULT_MASK & 0xffff);
  1340. uasm_i_mtc0(&p, K0, C0_PAGEMASK);
  1341. } else if (PM_DEFAULT_MASK) {
  1342. uasm_i_ori(&p, K0, 0, PM_DEFAULT_MASK);
  1343. uasm_i_mtc0(&p, K0, C0_PAGEMASK);
  1344. } else {
  1345. uasm_i_mtc0(&p, 0, C0_PAGEMASK);
  1346. }
  1347. uasm_i_eret(&p);
  1348. if (check_for_high_segbits) {
  1349. uasm_l_large_segbits_fault(&l, p);
  1350. UASM_i_LA(&p, K1, (unsigned long)tlb_do_page_fault_0);
  1351. uasm_i_jr(&p, K1);
  1352. uasm_i_nop(&p);
  1353. }
  1354. uasm_resolve_relocs(relocs, labels);
  1355. memcpy((void *)(ebase + 0x80), tlb_handler, 0x80);
  1356. local_flush_icache_range(ebase + 0x80, ebase + 0x100);
  1357. dump_handler("loongson3_tlb_refill",
  1358. (u32 *)(ebase + 0x80), (u32 *)(ebase + 0x100));
  1359. }
  1360. static void build_setup_pgd(void)
  1361. {
  1362. const int a0 = 4;
  1363. const int __maybe_unused a1 = 5;
  1364. const int __maybe_unused a2 = 6;
  1365. u32 *p = (u32 *)msk_isa16_mode((ulong)tlbmiss_handler_setup_pgd);
  1366. #ifndef CONFIG_MIPS_PGD_C0_CONTEXT
  1367. long pgdc = (long)pgd_current;
  1368. #endif
  1369. memset(p, 0, tlbmiss_handler_setup_pgd_end - (char *)p);
  1370. memset(labels, 0, sizeof(labels));
  1371. memset(relocs, 0, sizeof(relocs));
  1372. pgd_reg = allocate_kscratch();
  1373. #ifdef CONFIG_MIPS_PGD_C0_CONTEXT
  1374. if (pgd_reg == -1) {
  1375. struct uasm_label *l = labels;
  1376. struct uasm_reloc *r = relocs;
  1377. /* PGD << 11 in c0_Context */
  1378. /*
  1379. * If it is a ckseg0 address, convert to a physical
  1380. * address. Shifting right by 29 and adding 4 will
  1381. * result in zero for these addresses.
  1382. *
  1383. */
  1384. UASM_i_SRA(&p, a1, a0, 29);
  1385. UASM_i_ADDIU(&p, a1, a1, 4);
  1386. uasm_il_bnez(&p, &r, a1, label_tlbl_goaround1);
  1387. uasm_i_nop(&p);
  1388. uasm_i_dinsm(&p, a0, 0, 29, 64 - 29);
  1389. uasm_l_tlbl_goaround1(&l, p);
  1390. UASM_i_SLL(&p, a0, a0, 11);
  1391. UASM_i_MTC0(&p, a0, C0_CONTEXT);
  1392. uasm_i_jr(&p, 31);
  1393. uasm_i_ehb(&p);
  1394. } else {
  1395. /* PGD in c0_KScratch */
  1396. if (cpu_has_ldpte)
  1397. UASM_i_MTC0(&p, a0, C0_PWBASE);
  1398. else
  1399. UASM_i_MTC0(&p, a0, c0_kscratch(), pgd_reg);
  1400. uasm_i_jr(&p, 31);
  1401. uasm_i_ehb(&p);
  1402. }
  1403. #else
  1404. #ifdef CONFIG_SMP
  1405. /* Save PGD to pgd_current[smp_processor_id()] */
  1406. UASM_i_CPUID_MFC0(&p, a1, SMP_CPUID_REG);
  1407. UASM_i_SRL_SAFE(&p, a1, a1, SMP_CPUID_PTRSHIFT);
  1408. UASM_i_LA_mostly(&p, a2, pgdc);
  1409. UASM_i_ADDU(&p, a2, a2, a1);
  1410. UASM_i_SW(&p, a0, uasm_rel_lo(pgdc), a2);
  1411. #else
  1412. UASM_i_LA_mostly(&p, a2, pgdc);
  1413. UASM_i_SW(&p, a0, uasm_rel_lo(pgdc), a2);
  1414. #endif /* SMP */
  1415. /* if pgd_reg is allocated, save PGD also to scratch register */
  1416. if (pgd_reg != -1) {
  1417. UASM_i_MTC0(&p, a0, c0_kscratch(), pgd_reg);
  1418. uasm_i_jr(&p, 31);
  1419. uasm_i_ehb(&p);
  1420. } else {
  1421. uasm_i_jr(&p, 31);
  1422. uasm_i_nop(&p);
  1423. }
  1424. #endif
  1425. if (p >= (u32 *)tlbmiss_handler_setup_pgd_end)
  1426. panic("tlbmiss_handler_setup_pgd space exceeded");
  1427. uasm_resolve_relocs(relocs, labels);
  1428. pr_debug("Wrote tlbmiss_handler_setup_pgd (%u instructions).\n",
  1429. (unsigned int)(p - (u32 *)tlbmiss_handler_setup_pgd));
  1430. dump_handler("tlbmiss_handler", tlbmiss_handler_setup_pgd,
  1431. tlbmiss_handler_setup_pgd_end);
  1432. }
  1433. static void
  1434. iPTE_LW(u32 **p, unsigned int pte, unsigned int ptr)
  1435. {
  1436. #ifdef CONFIG_SMP
  1437. if (IS_ENABLED(CONFIG_CPU_LOONGSON3_WORKAROUNDS))
  1438. uasm_i_sync(p, 0);
  1439. # ifdef CONFIG_PHYS_ADDR_T_64BIT
  1440. if (cpu_has_64bits)
  1441. uasm_i_lld(p, pte, 0, ptr);
  1442. else
  1443. # endif
  1444. UASM_i_LL(p, pte, 0, ptr);
  1445. #else
  1446. # ifdef CONFIG_PHYS_ADDR_T_64BIT
  1447. if (cpu_has_64bits)
  1448. uasm_i_ld(p, pte, 0, ptr);
  1449. else
  1450. # endif
  1451. UASM_i_LW(p, pte, 0, ptr);
  1452. #endif
  1453. }
  1454. static void
  1455. iPTE_SW(u32 **p, struct uasm_reloc **r, unsigned int pte, unsigned int ptr,
  1456. unsigned int mode, unsigned int scratch)
  1457. {
  1458. unsigned int hwmode = mode & (_PAGE_VALID | _PAGE_DIRTY);
  1459. unsigned int swmode = mode & ~hwmode;
  1460. if (IS_ENABLED(CONFIG_XPA) && !cpu_has_64bits) {
  1461. uasm_i_lui(p, scratch, swmode >> 16);
  1462. uasm_i_or(p, pte, pte, scratch);
  1463. BUG_ON(swmode & 0xffff);
  1464. } else {
  1465. uasm_i_ori(p, pte, pte, mode);
  1466. }
  1467. #ifdef CONFIG_SMP
  1468. # ifdef CONFIG_PHYS_ADDR_T_64BIT
  1469. if (cpu_has_64bits)
  1470. uasm_i_scd(p, pte, 0, ptr);
  1471. else
  1472. # endif
  1473. UASM_i_SC(p, pte, 0, ptr);
  1474. if (r10000_llsc_war())
  1475. uasm_il_beqzl(p, r, pte, label_smp_pgtable_change);
  1476. else
  1477. uasm_il_beqz(p, r, pte, label_smp_pgtable_change);
  1478. # ifdef CONFIG_PHYS_ADDR_T_64BIT
  1479. if (!cpu_has_64bits) {
  1480. /* no uasm_i_nop needed */
  1481. uasm_i_ll(p, pte, sizeof(pte_t) / 2, ptr);
  1482. uasm_i_ori(p, pte, pte, hwmode);
  1483. BUG_ON(hwmode & ~0xffff);
  1484. uasm_i_sc(p, pte, sizeof(pte_t) / 2, ptr);
  1485. uasm_il_beqz(p, r, pte, label_smp_pgtable_change);
  1486. /* no uasm_i_nop needed */
  1487. uasm_i_lw(p, pte, 0, ptr);
  1488. } else
  1489. uasm_i_nop(p);
  1490. # else
  1491. uasm_i_nop(p);
  1492. # endif
  1493. #else
  1494. # ifdef CONFIG_PHYS_ADDR_T_64BIT
  1495. if (cpu_has_64bits)
  1496. uasm_i_sd(p, pte, 0, ptr);
  1497. else
  1498. # endif
  1499. UASM_i_SW(p, pte, 0, ptr);
  1500. # ifdef CONFIG_PHYS_ADDR_T_64BIT
  1501. if (!cpu_has_64bits) {
  1502. uasm_i_lw(p, pte, sizeof(pte_t) / 2, ptr);
  1503. uasm_i_ori(p, pte, pte, hwmode);
  1504. BUG_ON(hwmode & ~0xffff);
  1505. uasm_i_sw(p, pte, sizeof(pte_t) / 2, ptr);
  1506. uasm_i_lw(p, pte, 0, ptr);
  1507. }
  1508. # endif
  1509. #endif
  1510. }
  1511. /*
  1512. * Check if PTE is present, if not then jump to LABEL. PTR points to
  1513. * the page table where this PTE is located, PTE will be re-loaded
  1514. * with it's original value.
  1515. */
  1516. static void
  1517. build_pte_present(u32 **p, struct uasm_reloc **r,
  1518. int pte, int ptr, int scratch, enum label_id lid)
  1519. {
  1520. int t = scratch >= 0 ? scratch : pte;
  1521. int cur = pte;
  1522. if (cpu_has_rixi) {
  1523. if (use_bbit_insns()) {
  1524. uasm_il_bbit0(p, r, pte, ilog2(_PAGE_PRESENT), lid);
  1525. uasm_i_nop(p);
  1526. } else {
  1527. if (_PAGE_PRESENT_SHIFT) {
  1528. uasm_i_srl(p, t, cur, _PAGE_PRESENT_SHIFT);
  1529. cur = t;
  1530. }
  1531. uasm_i_andi(p, t, cur, 1);
  1532. uasm_il_beqz(p, r, t, lid);
  1533. if (pte == t)
  1534. /* You lose the SMP race :-(*/
  1535. iPTE_LW(p, pte, ptr);
  1536. }
  1537. } else {
  1538. if (_PAGE_PRESENT_SHIFT) {
  1539. uasm_i_srl(p, t, cur, _PAGE_PRESENT_SHIFT);
  1540. cur = t;
  1541. }
  1542. uasm_i_andi(p, t, cur,
  1543. (_PAGE_PRESENT | _PAGE_NO_READ) >> _PAGE_PRESENT_SHIFT);
  1544. uasm_i_xori(p, t, t, _PAGE_PRESENT >> _PAGE_PRESENT_SHIFT);
  1545. uasm_il_bnez(p, r, t, lid);
  1546. if (pte == t)
  1547. /* You lose the SMP race :-(*/
  1548. iPTE_LW(p, pte, ptr);
  1549. }
  1550. }
  1551. /* Make PTE valid, store result in PTR. */
  1552. static void
  1553. build_make_valid(u32 **p, struct uasm_reloc **r, unsigned int pte,
  1554. unsigned int ptr, unsigned int scratch)
  1555. {
  1556. unsigned int mode = _PAGE_VALID | _PAGE_ACCESSED;
  1557. iPTE_SW(p, r, pte, ptr, mode, scratch);
  1558. }
  1559. /*
  1560. * Check if PTE can be written to, if not branch to LABEL. Regardless
  1561. * restore PTE with value from PTR when done.
  1562. */
  1563. static void
  1564. build_pte_writable(u32 **p, struct uasm_reloc **r,
  1565. unsigned int pte, unsigned int ptr, int scratch,
  1566. enum label_id lid)
  1567. {
  1568. int t = scratch >= 0 ? scratch : pte;
  1569. int cur = pte;
  1570. if (_PAGE_PRESENT_SHIFT) {
  1571. uasm_i_srl(p, t, cur, _PAGE_PRESENT_SHIFT);
  1572. cur = t;
  1573. }
  1574. uasm_i_andi(p, t, cur,
  1575. (_PAGE_PRESENT | _PAGE_WRITE) >> _PAGE_PRESENT_SHIFT);
  1576. uasm_i_xori(p, t, t,
  1577. (_PAGE_PRESENT | _PAGE_WRITE) >> _PAGE_PRESENT_SHIFT);
  1578. uasm_il_bnez(p, r, t, lid);
  1579. if (pte == t)
  1580. /* You lose the SMP race :-(*/
  1581. iPTE_LW(p, pte, ptr);
  1582. else
  1583. uasm_i_nop(p);
  1584. }
  1585. /* Make PTE writable, update software status bits as well, then store
  1586. * at PTR.
  1587. */
  1588. static void
  1589. build_make_write(u32 **p, struct uasm_reloc **r, unsigned int pte,
  1590. unsigned int ptr, unsigned int scratch)
  1591. {
  1592. unsigned int mode = (_PAGE_ACCESSED | _PAGE_MODIFIED | _PAGE_VALID
  1593. | _PAGE_DIRTY);
  1594. iPTE_SW(p, r, pte, ptr, mode, scratch);
  1595. }
  1596. /*
  1597. * Check if PTE can be modified, if not branch to LABEL. Regardless
  1598. * restore PTE with value from PTR when done.
  1599. */
  1600. static void
  1601. build_pte_modifiable(u32 **p, struct uasm_reloc **r,
  1602. unsigned int pte, unsigned int ptr, int scratch,
  1603. enum label_id lid)
  1604. {
  1605. if (use_bbit_insns()) {
  1606. uasm_il_bbit0(p, r, pte, ilog2(_PAGE_WRITE), lid);
  1607. uasm_i_nop(p);
  1608. } else {
  1609. int t = scratch >= 0 ? scratch : pte;
  1610. uasm_i_srl(p, t, pte, _PAGE_WRITE_SHIFT);
  1611. uasm_i_andi(p, t, t, 1);
  1612. uasm_il_beqz(p, r, t, lid);
  1613. if (pte == t)
  1614. /* You lose the SMP race :-(*/
  1615. iPTE_LW(p, pte, ptr);
  1616. }
  1617. }
  1618. #ifndef CONFIG_MIPS_PGD_C0_CONTEXT
  1619. /*
  1620. * R3000 style TLB load/store/modify handlers.
  1621. */
  1622. /*
  1623. * This places the pte into ENTRYLO0 and writes it with tlbwi.
  1624. * Then it returns.
  1625. */
  1626. static void
  1627. build_r3000_pte_reload_tlbwi(u32 **p, unsigned int pte, unsigned int tmp)
  1628. {
  1629. uasm_i_mtc0(p, pte, C0_ENTRYLO0); /* cp0 delay */
  1630. uasm_i_mfc0(p, tmp, C0_EPC); /* cp0 delay */
  1631. uasm_i_tlbwi(p);
  1632. uasm_i_jr(p, tmp);
  1633. uasm_i_rfe(p); /* branch delay */
  1634. }
  1635. /*
  1636. * This places the pte into ENTRYLO0 and writes it with tlbwi
  1637. * or tlbwr as appropriate. This is because the index register
  1638. * may have the probe fail bit set as a result of a trap on a
  1639. * kseg2 access, i.e. without refill. Then it returns.
  1640. */
  1641. static void
  1642. build_r3000_tlb_reload_write(u32 **p, struct uasm_label **l,
  1643. struct uasm_reloc **r, unsigned int pte,
  1644. unsigned int tmp)
  1645. {
  1646. uasm_i_mfc0(p, tmp, C0_INDEX);
  1647. uasm_i_mtc0(p, pte, C0_ENTRYLO0); /* cp0 delay */
  1648. uasm_il_bltz(p, r, tmp, label_r3000_write_probe_fail); /* cp0 delay */
  1649. uasm_i_mfc0(p, tmp, C0_EPC); /* branch delay */
  1650. uasm_i_tlbwi(p); /* cp0 delay */
  1651. uasm_i_jr(p, tmp);
  1652. uasm_i_rfe(p); /* branch delay */
  1653. uasm_l_r3000_write_probe_fail(l, *p);
  1654. uasm_i_tlbwr(p); /* cp0 delay */
  1655. uasm_i_jr(p, tmp);
  1656. uasm_i_rfe(p); /* branch delay */
  1657. }
  1658. static void
  1659. build_r3000_tlbchange_handler_head(u32 **p, unsigned int pte,
  1660. unsigned int ptr)
  1661. {
  1662. long pgdc = (long)pgd_current;
  1663. uasm_i_mfc0(p, pte, C0_BADVADDR);
  1664. uasm_i_lui(p, ptr, uasm_rel_hi(pgdc)); /* cp0 delay */
  1665. uasm_i_lw(p, ptr, uasm_rel_lo(pgdc), ptr);
  1666. uasm_i_srl(p, pte, pte, 22); /* load delay */
  1667. uasm_i_sll(p, pte, pte, 2);
  1668. uasm_i_addu(p, ptr, ptr, pte);
  1669. uasm_i_mfc0(p, pte, C0_CONTEXT);
  1670. uasm_i_lw(p, ptr, 0, ptr); /* cp0 delay */
  1671. uasm_i_andi(p, pte, pte, 0xffc); /* load delay */
  1672. uasm_i_addu(p, ptr, ptr, pte);
  1673. uasm_i_lw(p, pte, 0, ptr);
  1674. uasm_i_tlbp(p); /* load delay */
  1675. }
  1676. static void build_r3000_tlb_load_handler(void)
  1677. {
  1678. u32 *p = (u32 *)handle_tlbl;
  1679. struct uasm_label *l = labels;
  1680. struct uasm_reloc *r = relocs;
  1681. memset(p, 0, handle_tlbl_end - (char *)p);
  1682. memset(labels, 0, sizeof(labels));
  1683. memset(relocs, 0, sizeof(relocs));
  1684. build_r3000_tlbchange_handler_head(&p, K0, K1);
  1685. build_pte_present(&p, &r, K0, K1, -1, label_nopage_tlbl);
  1686. uasm_i_nop(&p); /* load delay */
  1687. build_make_valid(&p, &r, K0, K1, -1);
  1688. build_r3000_tlb_reload_write(&p, &l, &r, K0, K1);
  1689. uasm_l_nopage_tlbl(&l, p);
  1690. uasm_i_j(&p, (unsigned long)tlb_do_page_fault_0 & 0x0fffffff);
  1691. uasm_i_nop(&p);
  1692. if (p >= (u32 *)handle_tlbl_end)
  1693. panic("TLB load handler fastpath space exceeded");
  1694. uasm_resolve_relocs(relocs, labels);
  1695. pr_debug("Wrote TLB load handler fastpath (%u instructions).\n",
  1696. (unsigned int)(p - (u32 *)handle_tlbl));
  1697. dump_handler("r3000_tlb_load", handle_tlbl, handle_tlbl_end);
  1698. }
  1699. static void build_r3000_tlb_store_handler(void)
  1700. {
  1701. u32 *p = (u32 *)handle_tlbs;
  1702. struct uasm_label *l = labels;
  1703. struct uasm_reloc *r = relocs;
  1704. memset(p, 0, handle_tlbs_end - (char *)p);
  1705. memset(labels, 0, sizeof(labels));
  1706. memset(relocs, 0, sizeof(relocs));
  1707. build_r3000_tlbchange_handler_head(&p, K0, K1);
  1708. build_pte_writable(&p, &r, K0, K1, -1, label_nopage_tlbs);
  1709. uasm_i_nop(&p); /* load delay */
  1710. build_make_write(&p, &r, K0, K1, -1);
  1711. build_r3000_tlb_reload_write(&p, &l, &r, K0, K1);
  1712. uasm_l_nopage_tlbs(&l, p);
  1713. uasm_i_j(&p, (unsigned long)tlb_do_page_fault_1 & 0x0fffffff);
  1714. uasm_i_nop(&p);
  1715. if (p >= (u32 *)handle_tlbs_end)
  1716. panic("TLB store handler fastpath space exceeded");
  1717. uasm_resolve_relocs(relocs, labels);
  1718. pr_debug("Wrote TLB store handler fastpath (%u instructions).\n",
  1719. (unsigned int)(p - (u32 *)handle_tlbs));
  1720. dump_handler("r3000_tlb_store", handle_tlbs, handle_tlbs_end);
  1721. }
  1722. static void build_r3000_tlb_modify_handler(void)
  1723. {
  1724. u32 *p = (u32 *)handle_tlbm;
  1725. struct uasm_label *l = labels;
  1726. struct uasm_reloc *r = relocs;
  1727. memset(p, 0, handle_tlbm_end - (char *)p);
  1728. memset(labels, 0, sizeof(labels));
  1729. memset(relocs, 0, sizeof(relocs));
  1730. build_r3000_tlbchange_handler_head(&p, K0, K1);
  1731. build_pte_modifiable(&p, &r, K0, K1, -1, label_nopage_tlbm);
  1732. uasm_i_nop(&p); /* load delay */
  1733. build_make_write(&p, &r, K0, K1, -1);
  1734. build_r3000_pte_reload_tlbwi(&p, K0, K1);
  1735. uasm_l_nopage_tlbm(&l, p);
  1736. uasm_i_j(&p, (unsigned long)tlb_do_page_fault_1 & 0x0fffffff);
  1737. uasm_i_nop(&p);
  1738. if (p >= (u32 *)handle_tlbm_end)
  1739. panic("TLB modify handler fastpath space exceeded");
  1740. uasm_resolve_relocs(relocs, labels);
  1741. pr_debug("Wrote TLB modify handler fastpath (%u instructions).\n",
  1742. (unsigned int)(p - (u32 *)handle_tlbm));
  1743. dump_handler("r3000_tlb_modify", handle_tlbm, handle_tlbm_end);
  1744. }
  1745. #endif /* CONFIG_MIPS_PGD_C0_CONTEXT */
  1746. static bool cpu_has_tlbex_tlbp_race(void)
  1747. {
  1748. /*
  1749. * When a Hardware Table Walker is running it can replace TLB entries
  1750. * at any time, leading to a race between it & the CPU.
  1751. */
  1752. if (cpu_has_htw)
  1753. return true;
  1754. /*
  1755. * If the CPU shares FTLB RAM with its siblings then our entry may be
  1756. * replaced at any time by a sibling performing a write to the FTLB.
  1757. */
  1758. if (cpu_has_shared_ftlb_ram)
  1759. return true;
  1760. /* In all other cases there ought to be no race condition to handle */
  1761. return false;
  1762. }
  1763. /*
  1764. * R4000 style TLB load/store/modify handlers.
  1765. */
  1766. static struct work_registers
  1767. build_r4000_tlbchange_handler_head(u32 **p, struct uasm_label **l,
  1768. struct uasm_reloc **r)
  1769. {
  1770. struct work_registers wr = build_get_work_registers(p);
  1771. #ifdef CONFIG_64BIT
  1772. build_get_pmde64(p, l, r, wr.r1, wr.r2); /* get pmd in ptr */
  1773. #else
  1774. build_get_pgde32(p, wr.r1, wr.r2); /* get pgd in ptr */
  1775. #endif
  1776. #ifdef CONFIG_MIPS_HUGE_TLB_SUPPORT
  1777. /*
  1778. * For huge tlb entries, pmd doesn't contain an address but
  1779. * instead contains the tlb pte. Check the PAGE_HUGE bit and
  1780. * see if we need to jump to huge tlb processing.
  1781. */
  1782. build_is_huge_pte(p, r, wr.r1, wr.r2, label_tlb_huge_update);
  1783. #endif
  1784. UASM_i_MFC0(p, wr.r1, C0_BADVADDR);
  1785. UASM_i_LW(p, wr.r2, 0, wr.r2);
  1786. UASM_i_SRL(p, wr.r1, wr.r1, PAGE_SHIFT - PTE_T_LOG2);
  1787. uasm_i_andi(p, wr.r1, wr.r1, (PTRS_PER_PTE - 1) << PTE_T_LOG2);
  1788. UASM_i_ADDU(p, wr.r2, wr.r2, wr.r1);
  1789. #ifdef CONFIG_SMP
  1790. uasm_l_smp_pgtable_change(l, *p);
  1791. #endif
  1792. iPTE_LW(p, wr.r1, wr.r2); /* get even pte */
  1793. if (!m4kc_tlbp_war()) {
  1794. build_tlb_probe_entry(p);
  1795. if (cpu_has_tlbex_tlbp_race()) {
  1796. /* race condition happens, leaving */
  1797. uasm_i_ehb(p);
  1798. uasm_i_mfc0(p, wr.r3, C0_INDEX);
  1799. uasm_il_bltz(p, r, wr.r3, label_leave);
  1800. uasm_i_nop(p);
  1801. }
  1802. }
  1803. return wr;
  1804. }
  1805. static void
  1806. build_r4000_tlbchange_handler_tail(u32 **p, struct uasm_label **l,
  1807. struct uasm_reloc **r, unsigned int tmp,
  1808. unsigned int ptr)
  1809. {
  1810. uasm_i_ori(p, ptr, ptr, sizeof(pte_t));
  1811. uasm_i_xori(p, ptr, ptr, sizeof(pte_t));
  1812. build_update_entries(p, tmp, ptr);
  1813. build_tlb_write_entry(p, l, r, tlb_indexed);
  1814. uasm_l_leave(l, *p);
  1815. build_restore_work_registers(p);
  1816. uasm_i_eret(p); /* return from trap */
  1817. #ifdef CONFIG_64BIT
  1818. build_get_pgd_vmalloc64(p, l, r, tmp, ptr, not_refill);
  1819. #endif
  1820. }
  1821. static void build_r4000_tlb_load_handler(void)
  1822. {
  1823. u32 *p = (u32 *)msk_isa16_mode((ulong)handle_tlbl);
  1824. struct uasm_label *l = labels;
  1825. struct uasm_reloc *r = relocs;
  1826. struct work_registers wr;
  1827. memset(p, 0, handle_tlbl_end - (char *)p);
  1828. memset(labels, 0, sizeof(labels));
  1829. memset(relocs, 0, sizeof(relocs));
  1830. if (bcm1250_m3_war()) {
  1831. unsigned int segbits = 44;
  1832. uasm_i_dmfc0(&p, K0, C0_BADVADDR);
  1833. uasm_i_dmfc0(&p, K1, C0_ENTRYHI);
  1834. uasm_i_xor(&p, K0, K0, K1);
  1835. uasm_i_dsrl_safe(&p, K1, K0, 62);
  1836. uasm_i_dsrl_safe(&p, K0, K0, 12 + 1);
  1837. uasm_i_dsll_safe(&p, K0, K0, 64 + 12 + 1 - segbits);
  1838. uasm_i_or(&p, K0, K0, K1);
  1839. uasm_il_bnez(&p, &r, K0, label_leave);
  1840. /* No need for uasm_i_nop */
  1841. }
  1842. wr = build_r4000_tlbchange_handler_head(&p, &l, &r);
  1843. build_pte_present(&p, &r, wr.r1, wr.r2, wr.r3, label_nopage_tlbl);
  1844. if (m4kc_tlbp_war())
  1845. build_tlb_probe_entry(&p);
  1846. if (cpu_has_rixi && !cpu_has_rixiex) {
  1847. /*
  1848. * If the page is not _PAGE_VALID, RI or XI could not
  1849. * have triggered it. Skip the expensive test..
  1850. */
  1851. if (use_bbit_insns()) {
  1852. uasm_il_bbit0(&p, &r, wr.r1, ilog2(_PAGE_VALID),
  1853. label_tlbl_goaround1);
  1854. } else {
  1855. uasm_i_andi(&p, wr.r3, wr.r1, _PAGE_VALID);
  1856. uasm_il_beqz(&p, &r, wr.r3, label_tlbl_goaround1);
  1857. }
  1858. uasm_i_nop(&p);
  1859. /*
  1860. * Warn if something may race with us & replace the TLB entry
  1861. * before we read it here. Everything with such races should
  1862. * also have dedicated RiXi exception handlers, so this
  1863. * shouldn't be hit.
  1864. */
  1865. WARN(cpu_has_tlbex_tlbp_race(), "Unhandled race in RiXi path");
  1866. uasm_i_tlbr(&p);
  1867. switch (current_cpu_type()) {
  1868. case CPU_CAVIUM_OCTEON:
  1869. case CPU_CAVIUM_OCTEON_PLUS:
  1870. case CPU_CAVIUM_OCTEON2:
  1871. break;
  1872. default:
  1873. if (cpu_has_mips_r2_exec_hazard)
  1874. uasm_i_ehb(&p);
  1875. break;
  1876. }
  1877. /* Examine entrylo 0 or 1 based on ptr. */
  1878. if (use_bbit_insns()) {
  1879. uasm_i_bbit0(&p, wr.r2, ilog2(sizeof(pte_t)), 8);
  1880. } else {
  1881. uasm_i_andi(&p, wr.r3, wr.r2, sizeof(pte_t));
  1882. uasm_i_beqz(&p, wr.r3, 8);
  1883. }
  1884. /* load it in the delay slot*/
  1885. UASM_i_MFC0(&p, wr.r3, C0_ENTRYLO0);
  1886. /* load it if ptr is odd */
  1887. UASM_i_MFC0(&p, wr.r3, C0_ENTRYLO1);
  1888. /*
  1889. * If the entryLo (now in wr.r3) is valid (bit 1), RI or
  1890. * XI must have triggered it.
  1891. */
  1892. if (use_bbit_insns()) {
  1893. uasm_il_bbit1(&p, &r, wr.r3, 1, label_nopage_tlbl);
  1894. uasm_i_nop(&p);
  1895. uasm_l_tlbl_goaround1(&l, p);
  1896. } else {
  1897. uasm_i_andi(&p, wr.r3, wr.r3, 2);
  1898. uasm_il_bnez(&p, &r, wr.r3, label_nopage_tlbl);
  1899. uasm_i_nop(&p);
  1900. }
  1901. uasm_l_tlbl_goaround1(&l, p);
  1902. }
  1903. build_make_valid(&p, &r, wr.r1, wr.r2, wr.r3);
  1904. build_r4000_tlbchange_handler_tail(&p, &l, &r, wr.r1, wr.r2);
  1905. #ifdef CONFIG_MIPS_HUGE_TLB_SUPPORT
  1906. /*
  1907. * This is the entry point when build_r4000_tlbchange_handler_head
  1908. * spots a huge page.
  1909. */
  1910. uasm_l_tlb_huge_update(&l, p);
  1911. iPTE_LW(&p, wr.r1, wr.r2);
  1912. build_pte_present(&p, &r, wr.r1, wr.r2, wr.r3, label_nopage_tlbl);
  1913. build_tlb_probe_entry(&p);
  1914. if (cpu_has_rixi && !cpu_has_rixiex) {
  1915. /*
  1916. * If the page is not _PAGE_VALID, RI or XI could not
  1917. * have triggered it. Skip the expensive test..
  1918. */
  1919. if (use_bbit_insns()) {
  1920. uasm_il_bbit0(&p, &r, wr.r1, ilog2(_PAGE_VALID),
  1921. label_tlbl_goaround2);
  1922. } else {
  1923. uasm_i_andi(&p, wr.r3, wr.r1, _PAGE_VALID);
  1924. uasm_il_beqz(&p, &r, wr.r3, label_tlbl_goaround2);
  1925. }
  1926. uasm_i_nop(&p);
  1927. /*
  1928. * Warn if something may race with us & replace the TLB entry
  1929. * before we read it here. Everything with such races should
  1930. * also have dedicated RiXi exception handlers, so this
  1931. * shouldn't be hit.
  1932. */
  1933. WARN(cpu_has_tlbex_tlbp_race(), "Unhandled race in RiXi path");
  1934. uasm_i_tlbr(&p);
  1935. switch (current_cpu_type()) {
  1936. case CPU_CAVIUM_OCTEON:
  1937. case CPU_CAVIUM_OCTEON_PLUS:
  1938. case CPU_CAVIUM_OCTEON2:
  1939. break;
  1940. default:
  1941. if (cpu_has_mips_r2_exec_hazard)
  1942. uasm_i_ehb(&p);
  1943. break;
  1944. }
  1945. /* Examine entrylo 0 or 1 based on ptr. */
  1946. if (use_bbit_insns()) {
  1947. uasm_i_bbit0(&p, wr.r2, ilog2(sizeof(pte_t)), 8);
  1948. } else {
  1949. uasm_i_andi(&p, wr.r3, wr.r2, sizeof(pte_t));
  1950. uasm_i_beqz(&p, wr.r3, 8);
  1951. }
  1952. /* load it in the delay slot*/
  1953. UASM_i_MFC0(&p, wr.r3, C0_ENTRYLO0);
  1954. /* load it if ptr is odd */
  1955. UASM_i_MFC0(&p, wr.r3, C0_ENTRYLO1);
  1956. /*
  1957. * If the entryLo (now in wr.r3) is valid (bit 1), RI or
  1958. * XI must have triggered it.
  1959. */
  1960. if (use_bbit_insns()) {
  1961. uasm_il_bbit0(&p, &r, wr.r3, 1, label_tlbl_goaround2);
  1962. } else {
  1963. uasm_i_andi(&p, wr.r3, wr.r3, 2);
  1964. uasm_il_beqz(&p, &r, wr.r3, label_tlbl_goaround2);
  1965. }
  1966. if (PM_DEFAULT_MASK == 0)
  1967. uasm_i_nop(&p);
  1968. /*
  1969. * We clobbered C0_PAGEMASK, restore it. On the other branch
  1970. * it is restored in build_huge_tlb_write_entry.
  1971. */
  1972. build_restore_pagemask(&p, &r, wr.r3, label_nopage_tlbl, 0);
  1973. uasm_l_tlbl_goaround2(&l, p);
  1974. }
  1975. uasm_i_ori(&p, wr.r1, wr.r1, (_PAGE_ACCESSED | _PAGE_VALID));
  1976. build_huge_handler_tail(&p, &r, &l, wr.r1, wr.r2, 1);
  1977. #endif
  1978. uasm_l_nopage_tlbl(&l, p);
  1979. if (IS_ENABLED(CONFIG_CPU_LOONGSON3_WORKAROUNDS))
  1980. uasm_i_sync(&p, 0);
  1981. build_restore_work_registers(&p);
  1982. #ifdef CONFIG_CPU_MICROMIPS
  1983. if ((unsigned long)tlb_do_page_fault_0 & 1) {
  1984. uasm_i_lui(&p, K0, uasm_rel_hi((long)tlb_do_page_fault_0));
  1985. uasm_i_addiu(&p, K0, K0, uasm_rel_lo((long)tlb_do_page_fault_0));
  1986. uasm_i_jr(&p, K0);
  1987. } else
  1988. #endif
  1989. uasm_i_j(&p, (unsigned long)tlb_do_page_fault_0 & 0x0fffffff);
  1990. uasm_i_nop(&p);
  1991. if (p >= (u32 *)handle_tlbl_end)
  1992. panic("TLB load handler fastpath space exceeded");
  1993. uasm_resolve_relocs(relocs, labels);
  1994. pr_debug("Wrote TLB load handler fastpath (%u instructions).\n",
  1995. (unsigned int)(p - (u32 *)handle_tlbl));
  1996. dump_handler("r4000_tlb_load", handle_tlbl, handle_tlbl_end);
  1997. }
  1998. static void build_r4000_tlb_store_handler(void)
  1999. {
  2000. u32 *p = (u32 *)msk_isa16_mode((ulong)handle_tlbs);
  2001. struct uasm_label *l = labels;
  2002. struct uasm_reloc *r = relocs;
  2003. struct work_registers wr;
  2004. memset(p, 0, handle_tlbs_end - (char *)p);
  2005. memset(labels, 0, sizeof(labels));
  2006. memset(relocs, 0, sizeof(relocs));
  2007. wr = build_r4000_tlbchange_handler_head(&p, &l, &r);
  2008. build_pte_writable(&p, &r, wr.r1, wr.r2, wr.r3, label_nopage_tlbs);
  2009. if (m4kc_tlbp_war())
  2010. build_tlb_probe_entry(&p);
  2011. build_make_write(&p, &r, wr.r1, wr.r2, wr.r3);
  2012. build_r4000_tlbchange_handler_tail(&p, &l, &r, wr.r1, wr.r2);
  2013. #ifdef CONFIG_MIPS_HUGE_TLB_SUPPORT
  2014. /*
  2015. * This is the entry point when
  2016. * build_r4000_tlbchange_handler_head spots a huge page.
  2017. */
  2018. uasm_l_tlb_huge_update(&l, p);
  2019. iPTE_LW(&p, wr.r1, wr.r2);
  2020. build_pte_writable(&p, &r, wr.r1, wr.r2, wr.r3, label_nopage_tlbs);
  2021. build_tlb_probe_entry(&p);
  2022. uasm_i_ori(&p, wr.r1, wr.r1,
  2023. _PAGE_ACCESSED | _PAGE_MODIFIED | _PAGE_VALID | _PAGE_DIRTY);
  2024. build_huge_handler_tail(&p, &r, &l, wr.r1, wr.r2, 1);
  2025. #endif
  2026. uasm_l_nopage_tlbs(&l, p);
  2027. if (IS_ENABLED(CONFIG_CPU_LOONGSON3_WORKAROUNDS))
  2028. uasm_i_sync(&p, 0);
  2029. build_restore_work_registers(&p);
  2030. #ifdef CONFIG_CPU_MICROMIPS
  2031. if ((unsigned long)tlb_do_page_fault_1 & 1) {
  2032. uasm_i_lui(&p, K0, uasm_rel_hi((long)tlb_do_page_fault_1));
  2033. uasm_i_addiu(&p, K0, K0, uasm_rel_lo((long)tlb_do_page_fault_1));
  2034. uasm_i_jr(&p, K0);
  2035. } else
  2036. #endif
  2037. uasm_i_j(&p, (unsigned long)tlb_do_page_fault_1 & 0x0fffffff);
  2038. uasm_i_nop(&p);
  2039. if (p >= (u32 *)handle_tlbs_end)
  2040. panic("TLB store handler fastpath space exceeded");
  2041. uasm_resolve_relocs(relocs, labels);
  2042. pr_debug("Wrote TLB store handler fastpath (%u instructions).\n",
  2043. (unsigned int)(p - (u32 *)handle_tlbs));
  2044. dump_handler("r4000_tlb_store", handle_tlbs, handle_tlbs_end);
  2045. }
  2046. static void build_r4000_tlb_modify_handler(void)
  2047. {
  2048. u32 *p = (u32 *)msk_isa16_mode((ulong)handle_tlbm);
  2049. struct uasm_label *l = labels;
  2050. struct uasm_reloc *r = relocs;
  2051. struct work_registers wr;
  2052. memset(p, 0, handle_tlbm_end - (char *)p);
  2053. memset(labels, 0, sizeof(labels));
  2054. memset(relocs, 0, sizeof(relocs));
  2055. wr = build_r4000_tlbchange_handler_head(&p, &l, &r);
  2056. build_pte_modifiable(&p, &r, wr.r1, wr.r2, wr.r3, label_nopage_tlbm);
  2057. if (m4kc_tlbp_war())
  2058. build_tlb_probe_entry(&p);
  2059. /* Present and writable bits set, set accessed and dirty bits. */
  2060. build_make_write(&p, &r, wr.r1, wr.r2, wr.r3);
  2061. build_r4000_tlbchange_handler_tail(&p, &l, &r, wr.r1, wr.r2);
  2062. #ifdef CONFIG_MIPS_HUGE_TLB_SUPPORT
  2063. /*
  2064. * This is the entry point when
  2065. * build_r4000_tlbchange_handler_head spots a huge page.
  2066. */
  2067. uasm_l_tlb_huge_update(&l, p);
  2068. iPTE_LW(&p, wr.r1, wr.r2);
  2069. build_pte_modifiable(&p, &r, wr.r1, wr.r2, wr.r3, label_nopage_tlbm);
  2070. build_tlb_probe_entry(&p);
  2071. uasm_i_ori(&p, wr.r1, wr.r1,
  2072. _PAGE_ACCESSED | _PAGE_MODIFIED | _PAGE_VALID | _PAGE_DIRTY);
  2073. build_huge_handler_tail(&p, &r, &l, wr.r1, wr.r2, 0);
  2074. #endif
  2075. uasm_l_nopage_tlbm(&l, p);
  2076. if (IS_ENABLED(CONFIG_CPU_LOONGSON3_WORKAROUNDS))
  2077. uasm_i_sync(&p, 0);
  2078. build_restore_work_registers(&p);
  2079. #ifdef CONFIG_CPU_MICROMIPS
  2080. if ((unsigned long)tlb_do_page_fault_1 & 1) {
  2081. uasm_i_lui(&p, K0, uasm_rel_hi((long)tlb_do_page_fault_1));
  2082. uasm_i_addiu(&p, K0, K0, uasm_rel_lo((long)tlb_do_page_fault_1));
  2083. uasm_i_jr(&p, K0);
  2084. } else
  2085. #endif
  2086. uasm_i_j(&p, (unsigned long)tlb_do_page_fault_1 & 0x0fffffff);
  2087. uasm_i_nop(&p);
  2088. if (p >= (u32 *)handle_tlbm_end)
  2089. panic("TLB modify handler fastpath space exceeded");
  2090. uasm_resolve_relocs(relocs, labels);
  2091. pr_debug("Wrote TLB modify handler fastpath (%u instructions).\n",
  2092. (unsigned int)(p - (u32 *)handle_tlbm));
  2093. dump_handler("r4000_tlb_modify", handle_tlbm, handle_tlbm_end);
  2094. }
  2095. static void flush_tlb_handlers(void)
  2096. {
  2097. local_flush_icache_range((unsigned long)handle_tlbl,
  2098. (unsigned long)handle_tlbl_end);
  2099. local_flush_icache_range((unsigned long)handle_tlbs,
  2100. (unsigned long)handle_tlbs_end);
  2101. local_flush_icache_range((unsigned long)handle_tlbm,
  2102. (unsigned long)handle_tlbm_end);
  2103. local_flush_icache_range((unsigned long)tlbmiss_handler_setup_pgd,
  2104. (unsigned long)tlbmiss_handler_setup_pgd_end);
  2105. }
  2106. static void print_htw_config(void)
  2107. {
  2108. unsigned long config;
  2109. unsigned int pwctl;
  2110. const int field = 2 * sizeof(unsigned long);
  2111. config = read_c0_pwfield();
  2112. pr_debug("PWField (0x%0*lx): GDI: 0x%02lx UDI: 0x%02lx MDI: 0x%02lx PTI: 0x%02lx PTEI: 0x%02lx\n",
  2113. field, config,
  2114. (config & MIPS_PWFIELD_GDI_MASK) >> MIPS_PWFIELD_GDI_SHIFT,
  2115. (config & MIPS_PWFIELD_UDI_MASK) >> MIPS_PWFIELD_UDI_SHIFT,
  2116. (config & MIPS_PWFIELD_MDI_MASK) >> MIPS_PWFIELD_MDI_SHIFT,
  2117. (config & MIPS_PWFIELD_PTI_MASK) >> MIPS_PWFIELD_PTI_SHIFT,
  2118. (config & MIPS_PWFIELD_PTEI_MASK) >> MIPS_PWFIELD_PTEI_SHIFT);
  2119. config = read_c0_pwsize();
  2120. pr_debug("PWSize (0x%0*lx): PS: 0x%lx GDW: 0x%02lx UDW: 0x%02lx MDW: 0x%02lx PTW: 0x%02lx PTEW: 0x%02lx\n",
  2121. field, config,
  2122. (config & MIPS_PWSIZE_PS_MASK) >> MIPS_PWSIZE_PS_SHIFT,
  2123. (config & MIPS_PWSIZE_GDW_MASK) >> MIPS_PWSIZE_GDW_SHIFT,
  2124. (config & MIPS_PWSIZE_UDW_MASK) >> MIPS_PWSIZE_UDW_SHIFT,
  2125. (config & MIPS_PWSIZE_MDW_MASK) >> MIPS_PWSIZE_MDW_SHIFT,
  2126. (config & MIPS_PWSIZE_PTW_MASK) >> MIPS_PWSIZE_PTW_SHIFT,
  2127. (config & MIPS_PWSIZE_PTEW_MASK) >> MIPS_PWSIZE_PTEW_SHIFT);
  2128. pwctl = read_c0_pwctl();
  2129. pr_debug("PWCtl (0x%x): PWEn: 0x%x XK: 0x%x XS: 0x%x XU: 0x%x DPH: 0x%x HugePg: 0x%x Psn: 0x%x\n",
  2130. pwctl,
  2131. (pwctl & MIPS_PWCTL_PWEN_MASK) >> MIPS_PWCTL_PWEN_SHIFT,
  2132. (pwctl & MIPS_PWCTL_XK_MASK) >> MIPS_PWCTL_XK_SHIFT,
  2133. (pwctl & MIPS_PWCTL_XS_MASK) >> MIPS_PWCTL_XS_SHIFT,
  2134. (pwctl & MIPS_PWCTL_XU_MASK) >> MIPS_PWCTL_XU_SHIFT,
  2135. (pwctl & MIPS_PWCTL_DPH_MASK) >> MIPS_PWCTL_DPH_SHIFT,
  2136. (pwctl & MIPS_PWCTL_HUGEPG_MASK) >> MIPS_PWCTL_HUGEPG_SHIFT,
  2137. (pwctl & MIPS_PWCTL_PSN_MASK) >> MIPS_PWCTL_PSN_SHIFT);
  2138. }
  2139. static void config_htw_params(void)
  2140. {
  2141. unsigned long pwfield, pwsize, ptei;
  2142. unsigned int config;
  2143. /*
  2144. * We are using 2-level page tables, so we only need to
  2145. * setup GDW and PTW appropriately. UDW and MDW will remain 0.
  2146. * The default value of GDI/UDI/MDI/PTI is 0xc. It is illegal to
  2147. * write values less than 0xc in these fields because the entire
  2148. * write will be dropped. As a result of which, we must preserve
  2149. * the original reset values and overwrite only what we really want.
  2150. */
  2151. pwfield = read_c0_pwfield();
  2152. /* re-initialize the GDI field */
  2153. pwfield &= ~MIPS_PWFIELD_GDI_MASK;
  2154. pwfield |= PGDIR_SHIFT << MIPS_PWFIELD_GDI_SHIFT;
  2155. /* re-initialize the PTI field including the even/odd bit */
  2156. pwfield &= ~MIPS_PWFIELD_PTI_MASK;
  2157. pwfield |= PAGE_SHIFT << MIPS_PWFIELD_PTI_SHIFT;
  2158. if (CONFIG_PGTABLE_LEVELS >= 3) {
  2159. pwfield &= ~MIPS_PWFIELD_MDI_MASK;
  2160. pwfield |= PMD_SHIFT << MIPS_PWFIELD_MDI_SHIFT;
  2161. }
  2162. /* Set the PTEI right shift */
  2163. ptei = _PAGE_GLOBAL_SHIFT << MIPS_PWFIELD_PTEI_SHIFT;
  2164. pwfield |= ptei;
  2165. write_c0_pwfield(pwfield);
  2166. /* Check whether the PTEI value is supported */
  2167. back_to_back_c0_hazard();
  2168. pwfield = read_c0_pwfield();
  2169. if (((pwfield & MIPS_PWFIELD_PTEI_MASK) << MIPS_PWFIELD_PTEI_SHIFT)
  2170. != ptei) {
  2171. pr_warn("Unsupported PTEI field value: 0x%lx. HTW will not be enabled",
  2172. ptei);
  2173. /*
  2174. * Drop option to avoid HTW being enabled via another path
  2175. * (eg htw_reset())
  2176. */
  2177. current_cpu_data.options &= ~MIPS_CPU_HTW;
  2178. return;
  2179. }
  2180. pwsize = ilog2(PTRS_PER_PGD) << MIPS_PWSIZE_GDW_SHIFT;
  2181. pwsize |= ilog2(PTRS_PER_PTE) << MIPS_PWSIZE_PTW_SHIFT;
  2182. if (CONFIG_PGTABLE_LEVELS >= 3)
  2183. pwsize |= ilog2(PTRS_PER_PMD) << MIPS_PWSIZE_MDW_SHIFT;
  2184. /* Set pointer size to size of directory pointers */
  2185. if (IS_ENABLED(CONFIG_64BIT))
  2186. pwsize |= MIPS_PWSIZE_PS_MASK;
  2187. /* PTEs may be multiple pointers long (e.g. with XPA) */
  2188. pwsize |= ((PTE_T_LOG2 - PGD_T_LOG2) << MIPS_PWSIZE_PTEW_SHIFT)
  2189. & MIPS_PWSIZE_PTEW_MASK;
  2190. write_c0_pwsize(pwsize);
  2191. /* Make sure everything is set before we enable the HTW */
  2192. back_to_back_c0_hazard();
  2193. /*
  2194. * Enable HTW (and only for XUSeg on 64-bit), and disable the rest of
  2195. * the pwctl fields.
  2196. */
  2197. config = 1 << MIPS_PWCTL_PWEN_SHIFT;
  2198. if (IS_ENABLED(CONFIG_64BIT))
  2199. config |= MIPS_PWCTL_XU_MASK;
  2200. write_c0_pwctl(config);
  2201. pr_info("Hardware Page Table Walker enabled\n");
  2202. print_htw_config();
  2203. }
  2204. static void config_xpa_params(void)
  2205. {
  2206. #ifdef CONFIG_XPA
  2207. unsigned int pagegrain;
  2208. if (mips_xpa_disabled) {
  2209. pr_info("Extended Physical Addressing (XPA) disabled\n");
  2210. return;
  2211. }
  2212. pagegrain = read_c0_pagegrain();
  2213. write_c0_pagegrain(pagegrain | PG_ELPA);
  2214. back_to_back_c0_hazard();
  2215. pagegrain = read_c0_pagegrain();
  2216. if (pagegrain & PG_ELPA)
  2217. pr_info("Extended Physical Addressing (XPA) enabled\n");
  2218. else
  2219. panic("Extended Physical Addressing (XPA) disabled");
  2220. #endif
  2221. }
  2222. static void check_pabits(void)
  2223. {
  2224. unsigned long entry;
  2225. unsigned pabits, fillbits;
  2226. if (!cpu_has_rixi || _PAGE_NO_EXEC == 0) {
  2227. /*
  2228. * We'll only be making use of the fact that we can rotate bits
  2229. * into the fill if the CPU supports RIXI, so don't bother
  2230. * probing this for CPUs which don't.
  2231. */
  2232. return;
  2233. }
  2234. write_c0_entrylo0(~0ul);
  2235. back_to_back_c0_hazard();
  2236. entry = read_c0_entrylo0();
  2237. /* clear all non-PFN bits */
  2238. entry &= ~((1 << MIPS_ENTRYLO_PFN_SHIFT) - 1);
  2239. entry &= ~(MIPS_ENTRYLO_RI | MIPS_ENTRYLO_XI);
  2240. /* find a lower bound on PABITS, and upper bound on fill bits */
  2241. pabits = fls_long(entry) + 6;
  2242. fillbits = max_t(int, (int)BITS_PER_LONG - pabits, 0);
  2243. /* minus the RI & XI bits */
  2244. fillbits -= min_t(unsigned, fillbits, 2);
  2245. if (fillbits >= ilog2(_PAGE_NO_EXEC))
  2246. fill_includes_sw_bits = true;
  2247. pr_debug("Entry* registers contain %u fill bits\n", fillbits);
  2248. }
  2249. void build_tlb_refill_handler(void)
  2250. {
  2251. /*
  2252. * The refill handler is generated per-CPU, multi-node systems
  2253. * may have local storage for it. The other handlers are only
  2254. * needed once.
  2255. */
  2256. static int run_once = 0;
  2257. if (IS_ENABLED(CONFIG_XPA) && !cpu_has_rixi)
  2258. panic("Kernels supporting XPA currently require CPUs with RIXI");
  2259. output_pgtable_bits_defines();
  2260. check_pabits();
  2261. #ifdef CONFIG_64BIT
  2262. check_for_high_segbits = current_cpu_data.vmbits > (PGDIR_SHIFT + PGD_TABLE_ORDER + PAGE_SHIFT - 3);
  2263. #endif
  2264. if (cpu_has_3kex) {
  2265. #ifndef CONFIG_MIPS_PGD_C0_CONTEXT
  2266. if (!run_once) {
  2267. build_setup_pgd();
  2268. build_r3000_tlb_refill_handler();
  2269. build_r3000_tlb_load_handler();
  2270. build_r3000_tlb_store_handler();
  2271. build_r3000_tlb_modify_handler();
  2272. flush_tlb_handlers();
  2273. run_once++;
  2274. }
  2275. #else
  2276. panic("No R3000 TLB refill handler");
  2277. #endif
  2278. return;
  2279. }
  2280. if (cpu_has_ldpte)
  2281. setup_pw();
  2282. if (!run_once) {
  2283. scratch_reg = allocate_kscratch();
  2284. build_setup_pgd();
  2285. build_r4000_tlb_load_handler();
  2286. build_r4000_tlb_store_handler();
  2287. build_r4000_tlb_modify_handler();
  2288. if (cpu_has_ldpte)
  2289. build_loongson3_tlb_refill_handler();
  2290. else
  2291. build_r4000_tlb_refill_handler();
  2292. flush_tlb_handlers();
  2293. run_once++;
  2294. }
  2295. if (cpu_has_xpa)
  2296. config_xpa_params();
  2297. if (cpu_has_htw)
  2298. config_htw_params();
  2299. }