tlbex.c 65 KB

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