tlbex.c 60 KB

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