tlbex.c 59 KB

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