tlbex.c 61 KB

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