tlbex.c 60 KB

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