tlbex.c 71 KB

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