tlbex.c 46 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740
  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. *
  13. * ... and the days got worse and worse and now you see
  14. * I've gone completly out of my mind.
  15. *
  16. * They're coming to take me a away haha
  17. * they're coming to take me a away hoho hihi haha
  18. * to the funny farm where code is beautiful all the time ...
  19. *
  20. * (Condolences to Napoleon XIV)
  21. */
  22. #include <linux/bug.h>
  23. #include <linux/kernel.h>
  24. #include <linux/types.h>
  25. #include <linux/smp.h>
  26. #include <linux/string.h>
  27. #include <linux/init.h>
  28. #include <linux/cache.h>
  29. #include <asm/cacheflush.h>
  30. #include <asm/pgtable.h>
  31. #include <asm/war.h>
  32. #include <asm/uasm.h>
  33. /*
  34. * TLB load/store/modify handlers.
  35. *
  36. * Only the fastpath gets synthesized at runtime, the slowpath for
  37. * do_page_fault remains normal asm.
  38. */
  39. extern void tlb_do_page_fault_0(void);
  40. extern void tlb_do_page_fault_1(void);
  41. static inline int r45k_bvahwbug(void)
  42. {
  43. /* XXX: We should probe for the presence of this bug, but we don't. */
  44. return 0;
  45. }
  46. static inline int r4k_250MHZhwbug(void)
  47. {
  48. /* XXX: We should probe for the presence of this bug, but we don't. */
  49. return 0;
  50. }
  51. static inline int __maybe_unused bcm1250_m3_war(void)
  52. {
  53. return BCM1250_M3_WAR;
  54. }
  55. static inline int __maybe_unused r10000_llsc_war(void)
  56. {
  57. return R10000_LLSC_WAR;
  58. }
  59. /*
  60. * Found by experiment: At least some revisions of the 4kc throw under
  61. * some circumstances a machine check exception, triggered by invalid
  62. * values in the index register. Delaying the tlbp instruction until
  63. * after the next branch, plus adding an additional nop in front of
  64. * tlbwi/tlbwr avoids the invalid index register values. Nobody knows
  65. * why; it's not an issue caused by the core RTL.
  66. *
  67. */
  68. static int __cpuinit m4kc_tlbp_war(void)
  69. {
  70. return (current_cpu_data.processor_id & 0xffff00) ==
  71. (PRID_COMP_MIPS | PRID_IMP_4KC);
  72. }
  73. /* Handle labels (which must be positive integers). */
  74. enum label_id {
  75. label_second_part = 1,
  76. label_leave,
  77. label_vmalloc,
  78. label_vmalloc_done,
  79. label_tlbw_hazard,
  80. label_split,
  81. label_tlbl_goaround1,
  82. label_tlbl_goaround2,
  83. label_nopage_tlbl,
  84. label_nopage_tlbs,
  85. label_nopage_tlbm,
  86. label_smp_pgtable_change,
  87. label_r3000_write_probe_fail,
  88. label_large_segbits_fault,
  89. #ifdef CONFIG_HUGETLB_PAGE
  90. label_tlb_huge_update,
  91. #endif
  92. };
  93. UASM_L_LA(_second_part)
  94. UASM_L_LA(_leave)
  95. UASM_L_LA(_vmalloc)
  96. UASM_L_LA(_vmalloc_done)
  97. UASM_L_LA(_tlbw_hazard)
  98. UASM_L_LA(_split)
  99. UASM_L_LA(_tlbl_goaround1)
  100. UASM_L_LA(_tlbl_goaround2)
  101. UASM_L_LA(_nopage_tlbl)
  102. UASM_L_LA(_nopage_tlbs)
  103. UASM_L_LA(_nopage_tlbm)
  104. UASM_L_LA(_smp_pgtable_change)
  105. UASM_L_LA(_r3000_write_probe_fail)
  106. UASM_L_LA(_large_segbits_fault)
  107. #ifdef CONFIG_HUGETLB_PAGE
  108. UASM_L_LA(_tlb_huge_update)
  109. #endif
  110. /*
  111. * For debug purposes.
  112. */
  113. static inline void dump_handler(const u32 *handler, int count)
  114. {
  115. int i;
  116. pr_debug("\t.set push\n");
  117. pr_debug("\t.set noreorder\n");
  118. for (i = 0; i < count; i++)
  119. pr_debug("\t%p\t.word 0x%08x\n", &handler[i], handler[i]);
  120. pr_debug("\t.set pop\n");
  121. }
  122. /* The only general purpose registers allowed in TLB handlers. */
  123. #define K0 26
  124. #define K1 27
  125. /* Some CP0 registers */
  126. #define C0_INDEX 0, 0
  127. #define C0_ENTRYLO0 2, 0
  128. #define C0_TCBIND 2, 2
  129. #define C0_ENTRYLO1 3, 0
  130. #define C0_CONTEXT 4, 0
  131. #define C0_PAGEMASK 5, 0
  132. #define C0_BADVADDR 8, 0
  133. #define C0_ENTRYHI 10, 0
  134. #define C0_EPC 14, 0
  135. #define C0_XCONTEXT 20, 0
  136. #ifdef CONFIG_64BIT
  137. # define GET_CONTEXT(buf, reg) UASM_i_MFC0(buf, reg, C0_XCONTEXT)
  138. #else
  139. # define GET_CONTEXT(buf, reg) UASM_i_MFC0(buf, reg, C0_CONTEXT)
  140. #endif
  141. /* The worst case length of the handler is around 18 instructions for
  142. * R3000-style TLBs and up to 63 instructions for R4000-style TLBs.
  143. * Maximum space available is 32 instructions for R3000 and 64
  144. * instructions for R4000.
  145. *
  146. * We deliberately chose a buffer size of 128, so we won't scribble
  147. * over anything important on overflow before we panic.
  148. */
  149. static u32 tlb_handler[128] __cpuinitdata;
  150. /* simply assume worst case size for labels and relocs */
  151. static struct uasm_label labels[128] __cpuinitdata;
  152. static struct uasm_reloc relocs[128] __cpuinitdata;
  153. #ifdef CONFIG_64BIT
  154. static int check_for_high_segbits __cpuinitdata;
  155. #endif
  156. #ifdef CONFIG_MIPS_PGD_C0_CONTEXT
  157. static unsigned int kscratch_used_mask __cpuinitdata;
  158. static int __cpuinit allocate_kscratch(void)
  159. {
  160. int r;
  161. unsigned int a = cpu_data[0].kscratch_mask & ~kscratch_used_mask;
  162. r = ffs(a);
  163. if (r == 0)
  164. return -1;
  165. r--; /* make it zero based */
  166. kscratch_used_mask |= (1 << r);
  167. return r;
  168. }
  169. static int pgd_reg __cpuinitdata;
  170. #else /* !CONFIG_MIPS_PGD_C0_CONTEXT*/
  171. /*
  172. * CONFIG_MIPS_PGD_C0_CONTEXT implies 64 bit and lack of pgd_current,
  173. * we cannot do r3000 under these circumstances.
  174. *
  175. * Declare pgd_current here instead of including mmu_context.h to avoid type
  176. * conflicts for tlbmiss_handler_setup_pgd
  177. */
  178. extern unsigned long pgd_current[];
  179. /*
  180. * The R3000 TLB handler is simple.
  181. */
  182. static void __cpuinit build_r3000_tlb_refill_handler(void)
  183. {
  184. long pgdc = (long)pgd_current;
  185. u32 *p;
  186. memset(tlb_handler, 0, sizeof(tlb_handler));
  187. p = tlb_handler;
  188. uasm_i_mfc0(&p, K0, C0_BADVADDR);
  189. uasm_i_lui(&p, K1, uasm_rel_hi(pgdc)); /* cp0 delay */
  190. uasm_i_lw(&p, K1, uasm_rel_lo(pgdc), K1);
  191. uasm_i_srl(&p, K0, K0, 22); /* load delay */
  192. uasm_i_sll(&p, K0, K0, 2);
  193. uasm_i_addu(&p, K1, K1, K0);
  194. uasm_i_mfc0(&p, K0, C0_CONTEXT);
  195. uasm_i_lw(&p, K1, 0, K1); /* cp0 delay */
  196. uasm_i_andi(&p, K0, K0, 0xffc); /* load delay */
  197. uasm_i_addu(&p, K1, K1, K0);
  198. uasm_i_lw(&p, K0, 0, K1);
  199. uasm_i_nop(&p); /* load delay */
  200. uasm_i_mtc0(&p, K0, C0_ENTRYLO0);
  201. uasm_i_mfc0(&p, K1, C0_EPC); /* cp0 delay */
  202. uasm_i_tlbwr(&p); /* cp0 delay */
  203. uasm_i_jr(&p, K1);
  204. uasm_i_rfe(&p); /* branch delay */
  205. if (p > tlb_handler + 32)
  206. panic("TLB refill handler space exceeded");
  207. pr_debug("Wrote TLB refill handler (%u instructions).\n",
  208. (unsigned int)(p - tlb_handler));
  209. memcpy((void *)ebase, tlb_handler, 0x80);
  210. dump_handler((u32 *)ebase, 32);
  211. }
  212. #endif /* CONFIG_MIPS_PGD_C0_CONTEXT */
  213. /*
  214. * The R4000 TLB handler is much more complicated. We have two
  215. * consecutive handler areas with 32 instructions space each.
  216. * Since they aren't used at the same time, we can overflow in the
  217. * other one.To keep things simple, we first assume linear space,
  218. * then we relocate it to the final handler layout as needed.
  219. */
  220. static u32 final_handler[64] __cpuinitdata;
  221. /*
  222. * Hazards
  223. *
  224. * From the IDT errata for the QED RM5230 (Nevada), processor revision 1.0:
  225. * 2. A timing hazard exists for the TLBP instruction.
  226. *
  227. * stalling_instruction
  228. * TLBP
  229. *
  230. * The JTLB is being read for the TLBP throughout the stall generated by the
  231. * previous instruction. This is not really correct as the stalling instruction
  232. * can modify the address used to access the JTLB. The failure symptom is that
  233. * the TLBP instruction will use an address created for the stalling instruction
  234. * and not the address held in C0_ENHI and thus report the wrong results.
  235. *
  236. * The software work-around is to not allow the instruction preceding the TLBP
  237. * to stall - make it an NOP or some other instruction guaranteed not to stall.
  238. *
  239. * Errata 2 will not be fixed. This errata is also on the R5000.
  240. *
  241. * As if we MIPS hackers wouldn't know how to nop pipelines happy ...
  242. */
  243. static void __cpuinit __maybe_unused build_tlb_probe_entry(u32 **p)
  244. {
  245. switch (current_cpu_type()) {
  246. /* Found by experiment: R4600 v2.0/R4700 needs this, too. */
  247. case CPU_R4600:
  248. case CPU_R4700:
  249. case CPU_R5000:
  250. case CPU_R5000A:
  251. case CPU_NEVADA:
  252. uasm_i_nop(p);
  253. uasm_i_tlbp(p);
  254. break;
  255. default:
  256. uasm_i_tlbp(p);
  257. break;
  258. }
  259. }
  260. /*
  261. * Write random or indexed TLB entry, and care about the hazards from
  262. * the preceeding mtc0 and for the following eret.
  263. */
  264. enum tlb_write_entry { tlb_random, tlb_indexed };
  265. static void __cpuinit build_tlb_write_entry(u32 **p, struct uasm_label **l,
  266. struct uasm_reloc **r,
  267. enum tlb_write_entry wmode)
  268. {
  269. void(*tlbw)(u32 **) = NULL;
  270. switch (wmode) {
  271. case tlb_random: tlbw = uasm_i_tlbwr; break;
  272. case tlb_indexed: tlbw = uasm_i_tlbwi; break;
  273. }
  274. if (cpu_has_mips_r2) {
  275. if (cpu_has_mips_r2_exec_hazard)
  276. uasm_i_ehb(p);
  277. tlbw(p);
  278. return;
  279. }
  280. switch (current_cpu_type()) {
  281. case CPU_R4000PC:
  282. case CPU_R4000SC:
  283. case CPU_R4000MC:
  284. case CPU_R4400PC:
  285. case CPU_R4400SC:
  286. case CPU_R4400MC:
  287. /*
  288. * This branch uses up a mtc0 hazard nop slot and saves
  289. * two nops after the tlbw instruction.
  290. */
  291. uasm_il_bgezl(p, r, 0, label_tlbw_hazard);
  292. tlbw(p);
  293. uasm_l_tlbw_hazard(l, *p);
  294. uasm_i_nop(p);
  295. break;
  296. case CPU_R4600:
  297. case CPU_R4700:
  298. case CPU_R5000:
  299. case CPU_R5000A:
  300. uasm_i_nop(p);
  301. tlbw(p);
  302. uasm_i_nop(p);
  303. break;
  304. case CPU_R4300:
  305. case CPU_5KC:
  306. case CPU_TX49XX:
  307. case CPU_PR4450:
  308. uasm_i_nop(p);
  309. tlbw(p);
  310. break;
  311. case CPU_R10000:
  312. case CPU_R12000:
  313. case CPU_R14000:
  314. case CPU_4KC:
  315. case CPU_4KEC:
  316. case CPU_SB1:
  317. case CPU_SB1A:
  318. case CPU_4KSC:
  319. case CPU_20KC:
  320. case CPU_25KF:
  321. case CPU_BMIPS32:
  322. case CPU_BMIPS3300:
  323. case CPU_BMIPS4350:
  324. case CPU_BMIPS4380:
  325. case CPU_BMIPS5000:
  326. case CPU_LOONGSON2:
  327. case CPU_R5500:
  328. if (m4kc_tlbp_war())
  329. uasm_i_nop(p);
  330. case CPU_ALCHEMY:
  331. tlbw(p);
  332. break;
  333. case CPU_NEVADA:
  334. uasm_i_nop(p); /* QED specifies 2 nops hazard */
  335. /*
  336. * This branch uses up a mtc0 hazard nop slot and saves
  337. * a nop after the tlbw instruction.
  338. */
  339. uasm_il_bgezl(p, r, 0, label_tlbw_hazard);
  340. tlbw(p);
  341. uasm_l_tlbw_hazard(l, *p);
  342. break;
  343. case CPU_RM7000:
  344. uasm_i_nop(p);
  345. uasm_i_nop(p);
  346. uasm_i_nop(p);
  347. uasm_i_nop(p);
  348. tlbw(p);
  349. break;
  350. case CPU_RM9000:
  351. /*
  352. * When the JTLB is updated by tlbwi or tlbwr, a subsequent
  353. * use of the JTLB for instructions should not occur for 4
  354. * cpu cycles and use for data translations should not occur
  355. * for 3 cpu cycles.
  356. */
  357. uasm_i_ssnop(p);
  358. uasm_i_ssnop(p);
  359. uasm_i_ssnop(p);
  360. uasm_i_ssnop(p);
  361. tlbw(p);
  362. uasm_i_ssnop(p);
  363. uasm_i_ssnop(p);
  364. uasm_i_ssnop(p);
  365. uasm_i_ssnop(p);
  366. break;
  367. case CPU_VR4111:
  368. case CPU_VR4121:
  369. case CPU_VR4122:
  370. case CPU_VR4181:
  371. case CPU_VR4181A:
  372. uasm_i_nop(p);
  373. uasm_i_nop(p);
  374. tlbw(p);
  375. uasm_i_nop(p);
  376. uasm_i_nop(p);
  377. break;
  378. case CPU_VR4131:
  379. case CPU_VR4133:
  380. case CPU_R5432:
  381. uasm_i_nop(p);
  382. uasm_i_nop(p);
  383. tlbw(p);
  384. break;
  385. case CPU_JZRISC:
  386. tlbw(p);
  387. uasm_i_nop(p);
  388. break;
  389. default:
  390. panic("No TLB refill handler yet (CPU type: %d)",
  391. current_cpu_data.cputype);
  392. break;
  393. }
  394. }
  395. static __cpuinit __maybe_unused void build_convert_pte_to_entrylo(u32 **p,
  396. unsigned int reg)
  397. {
  398. if (kernel_uses_smartmips_rixi) {
  399. UASM_i_SRL(p, reg, reg, ilog2(_PAGE_NO_EXEC));
  400. UASM_i_ROTR(p, reg, reg, ilog2(_PAGE_GLOBAL) - ilog2(_PAGE_NO_EXEC));
  401. } else {
  402. #ifdef CONFIG_64BIT_PHYS_ADDR
  403. uasm_i_dsrl_safe(p, reg, reg, ilog2(_PAGE_GLOBAL));
  404. #else
  405. UASM_i_SRL(p, reg, reg, ilog2(_PAGE_GLOBAL));
  406. #endif
  407. }
  408. }
  409. #ifdef CONFIG_HUGETLB_PAGE
  410. static __cpuinit void build_restore_pagemask(u32 **p,
  411. struct uasm_reloc **r,
  412. unsigned int tmp,
  413. enum label_id lid)
  414. {
  415. /* Reset default page size */
  416. if (PM_DEFAULT_MASK >> 16) {
  417. uasm_i_lui(p, tmp, PM_DEFAULT_MASK >> 16);
  418. uasm_i_ori(p, tmp, tmp, PM_DEFAULT_MASK & 0xffff);
  419. uasm_il_b(p, r, lid);
  420. uasm_i_mtc0(p, tmp, C0_PAGEMASK);
  421. } else if (PM_DEFAULT_MASK) {
  422. uasm_i_ori(p, tmp, 0, PM_DEFAULT_MASK);
  423. uasm_il_b(p, r, lid);
  424. uasm_i_mtc0(p, tmp, C0_PAGEMASK);
  425. } else {
  426. uasm_il_b(p, r, lid);
  427. uasm_i_mtc0(p, 0, C0_PAGEMASK);
  428. }
  429. }
  430. static __cpuinit void build_huge_tlb_write_entry(u32 **p,
  431. struct uasm_label **l,
  432. struct uasm_reloc **r,
  433. unsigned int tmp,
  434. enum tlb_write_entry wmode)
  435. {
  436. /* Set huge page tlb entry size */
  437. uasm_i_lui(p, tmp, PM_HUGE_MASK >> 16);
  438. uasm_i_ori(p, tmp, tmp, PM_HUGE_MASK & 0xffff);
  439. uasm_i_mtc0(p, tmp, C0_PAGEMASK);
  440. build_tlb_write_entry(p, l, r, wmode);
  441. build_restore_pagemask(p, r, tmp, label_leave);
  442. }
  443. /*
  444. * Check if Huge PTE is present, if so then jump to LABEL.
  445. */
  446. static void __cpuinit
  447. build_is_huge_pte(u32 **p, struct uasm_reloc **r, unsigned int tmp,
  448. unsigned int pmd, int lid)
  449. {
  450. UASM_i_LW(p, tmp, 0, pmd);
  451. uasm_i_andi(p, tmp, tmp, _PAGE_HUGE);
  452. uasm_il_bnez(p, r, tmp, lid);
  453. }
  454. static __cpuinit void build_huge_update_entries(u32 **p,
  455. unsigned int pte,
  456. unsigned int tmp)
  457. {
  458. int small_sequence;
  459. /*
  460. * A huge PTE describes an area the size of the
  461. * configured huge page size. This is twice the
  462. * of the large TLB entry size we intend to use.
  463. * A TLB entry half the size of the configured
  464. * huge page size is configured into entrylo0
  465. * and entrylo1 to cover the contiguous huge PTE
  466. * address space.
  467. */
  468. small_sequence = (HPAGE_SIZE >> 7) < 0x10000;
  469. /* We can clobber tmp. It isn't used after this.*/
  470. if (!small_sequence)
  471. uasm_i_lui(p, tmp, HPAGE_SIZE >> (7 + 16));
  472. build_convert_pte_to_entrylo(p, pte);
  473. UASM_i_MTC0(p, pte, C0_ENTRYLO0); /* load it */
  474. /* convert to entrylo1 */
  475. if (small_sequence)
  476. UASM_i_ADDIU(p, pte, pte, HPAGE_SIZE >> 7);
  477. else
  478. UASM_i_ADDU(p, pte, pte, tmp);
  479. UASM_i_MTC0(p, pte, C0_ENTRYLO1); /* load it */
  480. }
  481. static __cpuinit void build_huge_handler_tail(u32 **p,
  482. struct uasm_reloc **r,
  483. struct uasm_label **l,
  484. unsigned int pte,
  485. unsigned int ptr)
  486. {
  487. #ifdef CONFIG_SMP
  488. UASM_i_SC(p, pte, 0, ptr);
  489. uasm_il_beqz(p, r, pte, label_tlb_huge_update);
  490. UASM_i_LW(p, pte, 0, ptr); /* Needed because SC killed our PTE */
  491. #else
  492. UASM_i_SW(p, pte, 0, ptr);
  493. #endif
  494. build_huge_update_entries(p, pte, ptr);
  495. build_huge_tlb_write_entry(p, l, r, pte, tlb_indexed);
  496. }
  497. #endif /* CONFIG_HUGETLB_PAGE */
  498. #ifdef CONFIG_64BIT
  499. /*
  500. * TMP and PTR are scratch.
  501. * TMP will be clobbered, PTR will hold the pmd entry.
  502. */
  503. static void __cpuinit
  504. build_get_pmde64(u32 **p, struct uasm_label **l, struct uasm_reloc **r,
  505. unsigned int tmp, unsigned int ptr)
  506. {
  507. #ifndef CONFIG_MIPS_PGD_C0_CONTEXT
  508. long pgdc = (long)pgd_current;
  509. #endif
  510. /*
  511. * The vmalloc handling is not in the hotpath.
  512. */
  513. uasm_i_dmfc0(p, tmp, C0_BADVADDR);
  514. if (check_for_high_segbits) {
  515. /*
  516. * The kernel currently implicitely assumes that the
  517. * MIPS SEGBITS parameter for the processor is
  518. * (PGDIR_SHIFT+PGDIR_BITS) or less, and will never
  519. * allocate virtual addresses outside the maximum
  520. * range for SEGBITS = (PGDIR_SHIFT+PGDIR_BITS). But
  521. * that doesn't prevent user code from accessing the
  522. * higher xuseg addresses. Here, we make sure that
  523. * everything but the lower xuseg addresses goes down
  524. * the module_alloc/vmalloc path.
  525. */
  526. uasm_i_dsrl_safe(p, ptr, tmp, PGDIR_SHIFT + PGD_ORDER + PAGE_SHIFT - 3);
  527. uasm_il_bnez(p, r, ptr, label_vmalloc);
  528. } else {
  529. uasm_il_bltz(p, r, tmp, label_vmalloc);
  530. }
  531. /* No uasm_i_nop needed here, since the next insn doesn't touch TMP. */
  532. #ifdef CONFIG_MIPS_PGD_C0_CONTEXT
  533. if (pgd_reg != -1) {
  534. /* pgd is in pgd_reg */
  535. UASM_i_MFC0(p, ptr, 31, pgd_reg);
  536. } else {
  537. /*
  538. * &pgd << 11 stored in CONTEXT [23..63].
  539. */
  540. UASM_i_MFC0(p, ptr, C0_CONTEXT);
  541. /* Clear lower 23 bits of context. */
  542. uasm_i_dins(p, ptr, 0, 0, 23);
  543. /* 1 0 1 0 1 << 6 xkphys cached */
  544. uasm_i_ori(p, ptr, ptr, 0x540);
  545. uasm_i_drotr(p, ptr, ptr, 11);
  546. }
  547. #elif defined(CONFIG_SMP)
  548. # ifdef CONFIG_MIPS_MT_SMTC
  549. /*
  550. * SMTC uses TCBind value as "CPU" index
  551. */
  552. uasm_i_mfc0(p, ptr, C0_TCBIND);
  553. uasm_i_dsrl_safe(p, ptr, ptr, 19);
  554. # else
  555. /*
  556. * 64 bit SMP running in XKPHYS has smp_processor_id() << 3
  557. * stored in CONTEXT.
  558. */
  559. uasm_i_dmfc0(p, ptr, C0_CONTEXT);
  560. uasm_i_dsrl_safe(p, ptr, ptr, 23);
  561. # endif
  562. UASM_i_LA_mostly(p, tmp, pgdc);
  563. uasm_i_daddu(p, ptr, ptr, tmp);
  564. uasm_i_dmfc0(p, tmp, C0_BADVADDR);
  565. uasm_i_ld(p, ptr, uasm_rel_lo(pgdc), ptr);
  566. #else
  567. UASM_i_LA_mostly(p, ptr, pgdc);
  568. uasm_i_ld(p, ptr, uasm_rel_lo(pgdc), ptr);
  569. #endif
  570. uasm_l_vmalloc_done(l, *p);
  571. /* get pgd offset in bytes */
  572. uasm_i_dsrl_safe(p, tmp, tmp, PGDIR_SHIFT - 3);
  573. uasm_i_andi(p, tmp, tmp, (PTRS_PER_PGD - 1)<<3);
  574. uasm_i_daddu(p, ptr, ptr, tmp); /* add in pgd offset */
  575. #ifndef __PAGETABLE_PMD_FOLDED
  576. uasm_i_dmfc0(p, tmp, C0_BADVADDR); /* get faulting address */
  577. uasm_i_ld(p, ptr, 0, ptr); /* get pmd pointer */
  578. uasm_i_dsrl_safe(p, tmp, tmp, PMD_SHIFT-3); /* get pmd offset in bytes */
  579. uasm_i_andi(p, tmp, tmp, (PTRS_PER_PMD - 1)<<3);
  580. uasm_i_daddu(p, ptr, ptr, tmp); /* add in pmd offset */
  581. #endif
  582. }
  583. enum vmalloc64_mode {not_refill, refill};
  584. /*
  585. * BVADDR is the faulting address, PTR is scratch.
  586. * PTR will hold the pgd for vmalloc.
  587. */
  588. static void __cpuinit
  589. build_get_pgd_vmalloc64(u32 **p, struct uasm_label **l, struct uasm_reloc **r,
  590. unsigned int bvaddr, unsigned int ptr,
  591. enum vmalloc64_mode mode)
  592. {
  593. long swpd = (long)swapper_pg_dir;
  594. int single_insn_swpd;
  595. int did_vmalloc_branch = 0;
  596. single_insn_swpd = uasm_in_compat_space_p(swpd) && !uasm_rel_lo(swpd);
  597. uasm_l_vmalloc(l, *p);
  598. if (mode == refill && check_for_high_segbits) {
  599. if (single_insn_swpd) {
  600. uasm_il_bltz(p, r, bvaddr, label_vmalloc_done);
  601. uasm_i_lui(p, ptr, uasm_rel_hi(swpd));
  602. did_vmalloc_branch = 1;
  603. /* fall through */
  604. } else {
  605. uasm_il_bgez(p, r, bvaddr, label_large_segbits_fault);
  606. }
  607. }
  608. if (!did_vmalloc_branch) {
  609. if (uasm_in_compat_space_p(swpd) && !uasm_rel_lo(swpd)) {
  610. uasm_il_b(p, r, label_vmalloc_done);
  611. uasm_i_lui(p, ptr, uasm_rel_hi(swpd));
  612. } else {
  613. UASM_i_LA_mostly(p, ptr, swpd);
  614. uasm_il_b(p, r, label_vmalloc_done);
  615. if (uasm_in_compat_space_p(swpd))
  616. uasm_i_addiu(p, ptr, ptr, uasm_rel_lo(swpd));
  617. else
  618. uasm_i_daddiu(p, ptr, ptr, uasm_rel_lo(swpd));
  619. }
  620. }
  621. if (mode == refill && check_for_high_segbits) {
  622. uasm_l_large_segbits_fault(l, *p);
  623. /*
  624. * We get here if we are an xsseg address, or if we are
  625. * an xuseg address above (PGDIR_SHIFT+PGDIR_BITS) boundary.
  626. *
  627. * Ignoring xsseg (assume disabled so would generate
  628. * (address errors?), the only remaining possibility
  629. * is the upper xuseg addresses. On processors with
  630. * TLB_SEGBITS <= PGDIR_SHIFT+PGDIR_BITS, these
  631. * addresses would have taken an address error. We try
  632. * to mimic that here by taking a load/istream page
  633. * fault.
  634. */
  635. UASM_i_LA(p, ptr, (unsigned long)tlb_do_page_fault_0);
  636. uasm_i_jr(p, ptr);
  637. uasm_i_nop(p);
  638. }
  639. }
  640. #else /* !CONFIG_64BIT */
  641. /*
  642. * TMP and PTR are scratch.
  643. * TMP will be clobbered, PTR will hold the pgd entry.
  644. */
  645. static void __cpuinit __maybe_unused
  646. build_get_pgde32(u32 **p, unsigned int tmp, unsigned int ptr)
  647. {
  648. long pgdc = (long)pgd_current;
  649. /* 32 bit SMP has smp_processor_id() stored in CONTEXT. */
  650. #ifdef CONFIG_SMP
  651. #ifdef CONFIG_MIPS_MT_SMTC
  652. /*
  653. * SMTC uses TCBind value as "CPU" index
  654. */
  655. uasm_i_mfc0(p, ptr, C0_TCBIND);
  656. UASM_i_LA_mostly(p, tmp, pgdc);
  657. uasm_i_srl(p, ptr, ptr, 19);
  658. #else
  659. /*
  660. * smp_processor_id() << 3 is stored in CONTEXT.
  661. */
  662. uasm_i_mfc0(p, ptr, C0_CONTEXT);
  663. UASM_i_LA_mostly(p, tmp, pgdc);
  664. uasm_i_srl(p, ptr, ptr, 23);
  665. #endif
  666. uasm_i_addu(p, ptr, tmp, ptr);
  667. #else
  668. UASM_i_LA_mostly(p, ptr, pgdc);
  669. #endif
  670. uasm_i_mfc0(p, tmp, C0_BADVADDR); /* get faulting address */
  671. uasm_i_lw(p, ptr, uasm_rel_lo(pgdc), ptr);
  672. uasm_i_srl(p, tmp, tmp, PGDIR_SHIFT); /* get pgd only bits */
  673. uasm_i_sll(p, tmp, tmp, PGD_T_LOG2);
  674. uasm_i_addu(p, ptr, ptr, tmp); /* add in pgd offset */
  675. }
  676. #endif /* !CONFIG_64BIT */
  677. static void __cpuinit build_adjust_context(u32 **p, unsigned int ctx)
  678. {
  679. unsigned int shift = 4 - (PTE_T_LOG2 + 1) + PAGE_SHIFT - 12;
  680. unsigned int mask = (PTRS_PER_PTE / 2 - 1) << (PTE_T_LOG2 + 1);
  681. switch (current_cpu_type()) {
  682. case CPU_VR41XX:
  683. case CPU_VR4111:
  684. case CPU_VR4121:
  685. case CPU_VR4122:
  686. case CPU_VR4131:
  687. case CPU_VR4181:
  688. case CPU_VR4181A:
  689. case CPU_VR4133:
  690. shift += 2;
  691. break;
  692. default:
  693. break;
  694. }
  695. if (shift)
  696. UASM_i_SRL(p, ctx, ctx, shift);
  697. uasm_i_andi(p, ctx, ctx, mask);
  698. }
  699. static void __cpuinit build_get_ptep(u32 **p, unsigned int tmp, unsigned int ptr)
  700. {
  701. /*
  702. * Bug workaround for the Nevada. It seems as if under certain
  703. * circumstances the move from cp0_context might produce a
  704. * bogus result when the mfc0 instruction and its consumer are
  705. * in a different cacheline or a load instruction, probably any
  706. * memory reference, is between them.
  707. */
  708. switch (current_cpu_type()) {
  709. case CPU_NEVADA:
  710. UASM_i_LW(p, ptr, 0, ptr);
  711. GET_CONTEXT(p, tmp); /* get context reg */
  712. break;
  713. default:
  714. GET_CONTEXT(p, tmp); /* get context reg */
  715. UASM_i_LW(p, ptr, 0, ptr);
  716. break;
  717. }
  718. build_adjust_context(p, tmp);
  719. UASM_i_ADDU(p, ptr, ptr, tmp); /* add in offset */
  720. }
  721. static void __cpuinit build_update_entries(u32 **p, unsigned int tmp,
  722. unsigned int ptep)
  723. {
  724. /*
  725. * 64bit address support (36bit on a 32bit CPU) in a 32bit
  726. * Kernel is a special case. Only a few CPUs use it.
  727. */
  728. #ifdef CONFIG_64BIT_PHYS_ADDR
  729. if (cpu_has_64bits) {
  730. uasm_i_ld(p, tmp, 0, ptep); /* get even pte */
  731. uasm_i_ld(p, ptep, sizeof(pte_t), ptep); /* get odd pte */
  732. if (kernel_uses_smartmips_rixi) {
  733. UASM_i_SRL(p, tmp, tmp, ilog2(_PAGE_NO_EXEC));
  734. UASM_i_SRL(p, ptep, ptep, ilog2(_PAGE_NO_EXEC));
  735. UASM_i_ROTR(p, tmp, tmp, ilog2(_PAGE_GLOBAL) - ilog2(_PAGE_NO_EXEC));
  736. UASM_i_MTC0(p, tmp, C0_ENTRYLO0); /* load it */
  737. UASM_i_ROTR(p, ptep, ptep, ilog2(_PAGE_GLOBAL) - ilog2(_PAGE_NO_EXEC));
  738. } else {
  739. uasm_i_dsrl_safe(p, tmp, tmp, ilog2(_PAGE_GLOBAL)); /* convert to entrylo0 */
  740. UASM_i_MTC0(p, tmp, C0_ENTRYLO0); /* load it */
  741. uasm_i_dsrl_safe(p, ptep, ptep, ilog2(_PAGE_GLOBAL)); /* convert to entrylo1 */
  742. }
  743. UASM_i_MTC0(p, ptep, C0_ENTRYLO1); /* load it */
  744. } else {
  745. int pte_off_even = sizeof(pte_t) / 2;
  746. int pte_off_odd = pte_off_even + sizeof(pte_t);
  747. /* The pte entries are pre-shifted */
  748. uasm_i_lw(p, tmp, pte_off_even, ptep); /* get even pte */
  749. UASM_i_MTC0(p, tmp, C0_ENTRYLO0); /* load it */
  750. uasm_i_lw(p, ptep, pte_off_odd, ptep); /* get odd pte */
  751. UASM_i_MTC0(p, ptep, C0_ENTRYLO1); /* load it */
  752. }
  753. #else
  754. UASM_i_LW(p, tmp, 0, ptep); /* get even pte */
  755. UASM_i_LW(p, ptep, sizeof(pte_t), ptep); /* get odd pte */
  756. if (r45k_bvahwbug())
  757. build_tlb_probe_entry(p);
  758. if (kernel_uses_smartmips_rixi) {
  759. UASM_i_SRL(p, tmp, tmp, ilog2(_PAGE_NO_EXEC));
  760. UASM_i_SRL(p, ptep, ptep, ilog2(_PAGE_NO_EXEC));
  761. UASM_i_ROTR(p, tmp, tmp, ilog2(_PAGE_GLOBAL) - ilog2(_PAGE_NO_EXEC));
  762. if (r4k_250MHZhwbug())
  763. UASM_i_MTC0(p, 0, C0_ENTRYLO0);
  764. UASM_i_MTC0(p, tmp, C0_ENTRYLO0); /* load it */
  765. UASM_i_ROTR(p, ptep, ptep, ilog2(_PAGE_GLOBAL) - ilog2(_PAGE_NO_EXEC));
  766. } else {
  767. UASM_i_SRL(p, tmp, tmp, ilog2(_PAGE_GLOBAL)); /* convert to entrylo0 */
  768. if (r4k_250MHZhwbug())
  769. UASM_i_MTC0(p, 0, C0_ENTRYLO0);
  770. UASM_i_MTC0(p, tmp, C0_ENTRYLO0); /* load it */
  771. UASM_i_SRL(p, ptep, ptep, ilog2(_PAGE_GLOBAL)); /* convert to entrylo1 */
  772. if (r45k_bvahwbug())
  773. uasm_i_mfc0(p, tmp, C0_INDEX);
  774. }
  775. if (r4k_250MHZhwbug())
  776. UASM_i_MTC0(p, 0, C0_ENTRYLO1);
  777. UASM_i_MTC0(p, ptep, C0_ENTRYLO1); /* load it */
  778. #endif
  779. }
  780. /*
  781. * For a 64-bit kernel, we are using the 64-bit XTLB refill exception
  782. * because EXL == 0. If we wrap, we can also use the 32 instruction
  783. * slots before the XTLB refill exception handler which belong to the
  784. * unused TLB refill exception.
  785. */
  786. #define MIPS64_REFILL_INSNS 32
  787. static void __cpuinit build_r4000_tlb_refill_handler(void)
  788. {
  789. u32 *p = tlb_handler;
  790. struct uasm_label *l = labels;
  791. struct uasm_reloc *r = relocs;
  792. u32 *f;
  793. unsigned int final_len;
  794. memset(tlb_handler, 0, sizeof(tlb_handler));
  795. memset(labels, 0, sizeof(labels));
  796. memset(relocs, 0, sizeof(relocs));
  797. memset(final_handler, 0, sizeof(final_handler));
  798. /*
  799. * create the plain linear handler
  800. */
  801. if (bcm1250_m3_war()) {
  802. unsigned int segbits = 44;
  803. uasm_i_dmfc0(&p, K0, C0_BADVADDR);
  804. uasm_i_dmfc0(&p, K1, C0_ENTRYHI);
  805. uasm_i_xor(&p, K0, K0, K1);
  806. uasm_i_dsrl_safe(&p, K1, K0, 62);
  807. uasm_i_dsrl_safe(&p, K0, K0, 12 + 1);
  808. uasm_i_dsll_safe(&p, K0, K0, 64 + 12 + 1 - segbits);
  809. uasm_i_or(&p, K0, K0, K1);
  810. uasm_il_bnez(&p, &r, K0, label_leave);
  811. /* No need for uasm_i_nop */
  812. }
  813. #ifdef CONFIG_64BIT
  814. build_get_pmde64(&p, &l, &r, K0, K1); /* get pmd in K1 */
  815. #else
  816. build_get_pgde32(&p, K0, K1); /* get pgd in K1 */
  817. #endif
  818. #ifdef CONFIG_HUGETLB_PAGE
  819. build_is_huge_pte(&p, &r, K0, K1, label_tlb_huge_update);
  820. #endif
  821. build_get_ptep(&p, K0, K1);
  822. build_update_entries(&p, K0, K1);
  823. build_tlb_write_entry(&p, &l, &r, tlb_random);
  824. uasm_l_leave(&l, p);
  825. uasm_i_eret(&p); /* return from trap */
  826. #ifdef CONFIG_HUGETLB_PAGE
  827. uasm_l_tlb_huge_update(&l, p);
  828. UASM_i_LW(&p, K0, 0, K1);
  829. build_huge_update_entries(&p, K0, K1);
  830. build_huge_tlb_write_entry(&p, &l, &r, K0, tlb_random);
  831. #endif
  832. #ifdef CONFIG_64BIT
  833. build_get_pgd_vmalloc64(&p, &l, &r, K0, K1, refill);
  834. #endif
  835. /*
  836. * Overflow check: For the 64bit handler, we need at least one
  837. * free instruction slot for the wrap-around branch. In worst
  838. * case, if the intended insertion point is a delay slot, we
  839. * need three, with the second nop'ed and the third being
  840. * unused.
  841. */
  842. /* Loongson2 ebase is different than r4k, we have more space */
  843. #if defined(CONFIG_32BIT) || defined(CONFIG_CPU_LOONGSON2)
  844. if ((p - tlb_handler) > 64)
  845. panic("TLB refill handler space exceeded");
  846. #else
  847. if (((p - tlb_handler) > (MIPS64_REFILL_INSNS * 2) - 1)
  848. || (((p - tlb_handler) > (MIPS64_REFILL_INSNS * 2) - 3)
  849. && uasm_insn_has_bdelay(relocs,
  850. tlb_handler + MIPS64_REFILL_INSNS - 3)))
  851. panic("TLB refill handler space exceeded");
  852. #endif
  853. /*
  854. * Now fold the handler in the TLB refill handler space.
  855. */
  856. #if defined(CONFIG_32BIT) || defined(CONFIG_CPU_LOONGSON2)
  857. f = final_handler;
  858. /* Simplest case, just copy the handler. */
  859. uasm_copy_handler(relocs, labels, tlb_handler, p, f);
  860. final_len = p - tlb_handler;
  861. #else /* CONFIG_64BIT */
  862. f = final_handler + MIPS64_REFILL_INSNS;
  863. if ((p - tlb_handler) <= MIPS64_REFILL_INSNS) {
  864. /* Just copy the handler. */
  865. uasm_copy_handler(relocs, labels, tlb_handler, p, f);
  866. final_len = p - tlb_handler;
  867. } else {
  868. #if defined(CONFIG_HUGETLB_PAGE)
  869. const enum label_id ls = label_tlb_huge_update;
  870. #else
  871. const enum label_id ls = label_vmalloc;
  872. #endif
  873. u32 *split;
  874. int ov = 0;
  875. int i;
  876. for (i = 0; i < ARRAY_SIZE(labels) && labels[i].lab != ls; i++)
  877. ;
  878. BUG_ON(i == ARRAY_SIZE(labels));
  879. split = labels[i].addr;
  880. /*
  881. * See if we have overflown one way or the other.
  882. */
  883. if (split > tlb_handler + MIPS64_REFILL_INSNS ||
  884. split < p - MIPS64_REFILL_INSNS)
  885. ov = 1;
  886. if (ov) {
  887. /*
  888. * Split two instructions before the end. One
  889. * for the branch and one for the instruction
  890. * in the delay slot.
  891. */
  892. split = tlb_handler + MIPS64_REFILL_INSNS - 2;
  893. /*
  894. * If the branch would fall in a delay slot,
  895. * we must back up an additional instruction
  896. * so that it is no longer in a delay slot.
  897. */
  898. if (uasm_insn_has_bdelay(relocs, split - 1))
  899. split--;
  900. }
  901. /* Copy first part of the handler. */
  902. uasm_copy_handler(relocs, labels, tlb_handler, split, f);
  903. f += split - tlb_handler;
  904. if (ov) {
  905. /* Insert branch. */
  906. uasm_l_split(&l, final_handler);
  907. uasm_il_b(&f, &r, label_split);
  908. if (uasm_insn_has_bdelay(relocs, split))
  909. uasm_i_nop(&f);
  910. else {
  911. uasm_copy_handler(relocs, labels,
  912. split, split + 1, f);
  913. uasm_move_labels(labels, f, f + 1, -1);
  914. f++;
  915. split++;
  916. }
  917. }
  918. /* Copy the rest of the handler. */
  919. uasm_copy_handler(relocs, labels, split, p, final_handler);
  920. final_len = (f - (final_handler + MIPS64_REFILL_INSNS)) +
  921. (p - split);
  922. }
  923. #endif /* CONFIG_64BIT */
  924. uasm_resolve_relocs(relocs, labels);
  925. pr_debug("Wrote TLB refill handler (%u instructions).\n",
  926. final_len);
  927. memcpy((void *)ebase, final_handler, 0x100);
  928. dump_handler((u32 *)ebase, 64);
  929. }
  930. /*
  931. * 128 instructions for the fastpath handler is generous and should
  932. * never be exceeded.
  933. */
  934. #define FASTPATH_SIZE 128
  935. u32 handle_tlbl[FASTPATH_SIZE] __cacheline_aligned;
  936. u32 handle_tlbs[FASTPATH_SIZE] __cacheline_aligned;
  937. u32 handle_tlbm[FASTPATH_SIZE] __cacheline_aligned;
  938. #ifdef CONFIG_MIPS_PGD_C0_CONTEXT
  939. u32 tlbmiss_handler_setup_pgd[16] __cacheline_aligned;
  940. static void __cpuinit build_r4000_setup_pgd(void)
  941. {
  942. const int a0 = 4;
  943. const int a1 = 5;
  944. u32 *p = tlbmiss_handler_setup_pgd;
  945. struct uasm_label *l = labels;
  946. struct uasm_reloc *r = relocs;
  947. memset(tlbmiss_handler_setup_pgd, 0, sizeof(tlbmiss_handler_setup_pgd));
  948. memset(labels, 0, sizeof(labels));
  949. memset(relocs, 0, sizeof(relocs));
  950. pgd_reg = allocate_kscratch();
  951. if (pgd_reg == -1) {
  952. /* PGD << 11 in c0_Context */
  953. /*
  954. * If it is a ckseg0 address, convert to a physical
  955. * address. Shifting right by 29 and adding 4 will
  956. * result in zero for these addresses.
  957. *
  958. */
  959. UASM_i_SRA(&p, a1, a0, 29);
  960. UASM_i_ADDIU(&p, a1, a1, 4);
  961. uasm_il_bnez(&p, &r, a1, label_tlbl_goaround1);
  962. uasm_i_nop(&p);
  963. uasm_i_dinsm(&p, a0, 0, 29, 64 - 29);
  964. uasm_l_tlbl_goaround1(&l, p);
  965. UASM_i_SLL(&p, a0, a0, 11);
  966. uasm_i_jr(&p, 31);
  967. UASM_i_MTC0(&p, a0, C0_CONTEXT);
  968. } else {
  969. /* PGD in c0_KScratch */
  970. uasm_i_jr(&p, 31);
  971. UASM_i_MTC0(&p, a0, 31, pgd_reg);
  972. }
  973. if (p - tlbmiss_handler_setup_pgd > ARRAY_SIZE(tlbmiss_handler_setup_pgd))
  974. panic("tlbmiss_handler_setup_pgd space exceeded");
  975. uasm_resolve_relocs(relocs, labels);
  976. pr_debug("Wrote tlbmiss_handler_setup_pgd (%u instructions).\n",
  977. (unsigned int)(p - tlbmiss_handler_setup_pgd));
  978. dump_handler(tlbmiss_handler_setup_pgd,
  979. ARRAY_SIZE(tlbmiss_handler_setup_pgd));
  980. }
  981. #endif
  982. static void __cpuinit
  983. iPTE_LW(u32 **p, unsigned int pte, unsigned int ptr)
  984. {
  985. #ifdef CONFIG_SMP
  986. # ifdef CONFIG_64BIT_PHYS_ADDR
  987. if (cpu_has_64bits)
  988. uasm_i_lld(p, pte, 0, ptr);
  989. else
  990. # endif
  991. UASM_i_LL(p, pte, 0, ptr);
  992. #else
  993. # ifdef CONFIG_64BIT_PHYS_ADDR
  994. if (cpu_has_64bits)
  995. uasm_i_ld(p, pte, 0, ptr);
  996. else
  997. # endif
  998. UASM_i_LW(p, pte, 0, ptr);
  999. #endif
  1000. }
  1001. static void __cpuinit
  1002. iPTE_SW(u32 **p, struct uasm_reloc **r, unsigned int pte, unsigned int ptr,
  1003. unsigned int mode)
  1004. {
  1005. #ifdef CONFIG_64BIT_PHYS_ADDR
  1006. unsigned int hwmode = mode & (_PAGE_VALID | _PAGE_DIRTY);
  1007. #endif
  1008. uasm_i_ori(p, pte, pte, mode);
  1009. #ifdef CONFIG_SMP
  1010. # ifdef CONFIG_64BIT_PHYS_ADDR
  1011. if (cpu_has_64bits)
  1012. uasm_i_scd(p, pte, 0, ptr);
  1013. else
  1014. # endif
  1015. UASM_i_SC(p, pte, 0, ptr);
  1016. if (r10000_llsc_war())
  1017. uasm_il_beqzl(p, r, pte, label_smp_pgtable_change);
  1018. else
  1019. uasm_il_beqz(p, r, pte, label_smp_pgtable_change);
  1020. # ifdef CONFIG_64BIT_PHYS_ADDR
  1021. if (!cpu_has_64bits) {
  1022. /* no uasm_i_nop needed */
  1023. uasm_i_ll(p, pte, sizeof(pte_t) / 2, ptr);
  1024. uasm_i_ori(p, pte, pte, hwmode);
  1025. uasm_i_sc(p, pte, sizeof(pte_t) / 2, ptr);
  1026. uasm_il_beqz(p, r, pte, label_smp_pgtable_change);
  1027. /* no uasm_i_nop needed */
  1028. uasm_i_lw(p, pte, 0, ptr);
  1029. } else
  1030. uasm_i_nop(p);
  1031. # else
  1032. uasm_i_nop(p);
  1033. # endif
  1034. #else
  1035. # ifdef CONFIG_64BIT_PHYS_ADDR
  1036. if (cpu_has_64bits)
  1037. uasm_i_sd(p, pte, 0, ptr);
  1038. else
  1039. # endif
  1040. UASM_i_SW(p, pte, 0, ptr);
  1041. # ifdef CONFIG_64BIT_PHYS_ADDR
  1042. if (!cpu_has_64bits) {
  1043. uasm_i_lw(p, pte, sizeof(pte_t) / 2, ptr);
  1044. uasm_i_ori(p, pte, pte, hwmode);
  1045. uasm_i_sw(p, pte, sizeof(pte_t) / 2, ptr);
  1046. uasm_i_lw(p, pte, 0, ptr);
  1047. }
  1048. # endif
  1049. #endif
  1050. }
  1051. /*
  1052. * Check if PTE is present, if not then jump to LABEL. PTR points to
  1053. * the page table where this PTE is located, PTE will be re-loaded
  1054. * with it's original value.
  1055. */
  1056. static void __cpuinit
  1057. build_pte_present(u32 **p, struct uasm_reloc **r,
  1058. unsigned int pte, unsigned int ptr, enum label_id lid)
  1059. {
  1060. if (kernel_uses_smartmips_rixi) {
  1061. uasm_i_andi(p, pte, pte, _PAGE_PRESENT);
  1062. uasm_il_beqz(p, r, pte, lid);
  1063. } else {
  1064. uasm_i_andi(p, pte, pte, _PAGE_PRESENT | _PAGE_READ);
  1065. uasm_i_xori(p, pte, pte, _PAGE_PRESENT | _PAGE_READ);
  1066. uasm_il_bnez(p, r, pte, lid);
  1067. }
  1068. iPTE_LW(p, pte, ptr);
  1069. }
  1070. /* Make PTE valid, store result in PTR. */
  1071. static void __cpuinit
  1072. build_make_valid(u32 **p, struct uasm_reloc **r, unsigned int pte,
  1073. unsigned int ptr)
  1074. {
  1075. unsigned int mode = _PAGE_VALID | _PAGE_ACCESSED;
  1076. iPTE_SW(p, r, pte, ptr, mode);
  1077. }
  1078. /*
  1079. * Check if PTE can be written to, if not branch to LABEL. Regardless
  1080. * restore PTE with value from PTR when done.
  1081. */
  1082. static void __cpuinit
  1083. build_pte_writable(u32 **p, struct uasm_reloc **r,
  1084. unsigned int pte, unsigned int ptr, enum label_id lid)
  1085. {
  1086. uasm_i_andi(p, pte, pte, _PAGE_PRESENT | _PAGE_WRITE);
  1087. uasm_i_xori(p, pte, pte, _PAGE_PRESENT | _PAGE_WRITE);
  1088. uasm_il_bnez(p, r, pte, lid);
  1089. iPTE_LW(p, pte, ptr);
  1090. }
  1091. /* Make PTE writable, update software status bits as well, then store
  1092. * at PTR.
  1093. */
  1094. static void __cpuinit
  1095. build_make_write(u32 **p, struct uasm_reloc **r, unsigned int pte,
  1096. unsigned int ptr)
  1097. {
  1098. unsigned int mode = (_PAGE_ACCESSED | _PAGE_MODIFIED | _PAGE_VALID
  1099. | _PAGE_DIRTY);
  1100. iPTE_SW(p, r, pte, ptr, mode);
  1101. }
  1102. /*
  1103. * Check if PTE can be modified, if not branch to LABEL. Regardless
  1104. * restore PTE with value from PTR when done.
  1105. */
  1106. static void __cpuinit
  1107. build_pte_modifiable(u32 **p, struct uasm_reloc **r,
  1108. unsigned int pte, unsigned int ptr, enum label_id lid)
  1109. {
  1110. uasm_i_andi(p, pte, pte, _PAGE_WRITE);
  1111. uasm_il_beqz(p, r, pte, lid);
  1112. iPTE_LW(p, pte, ptr);
  1113. }
  1114. #ifndef CONFIG_MIPS_PGD_C0_CONTEXT
  1115. /*
  1116. * R3000 style TLB load/store/modify handlers.
  1117. */
  1118. /*
  1119. * This places the pte into ENTRYLO0 and writes it with tlbwi.
  1120. * Then it returns.
  1121. */
  1122. static void __cpuinit
  1123. build_r3000_pte_reload_tlbwi(u32 **p, unsigned int pte, unsigned int tmp)
  1124. {
  1125. uasm_i_mtc0(p, pte, C0_ENTRYLO0); /* cp0 delay */
  1126. uasm_i_mfc0(p, tmp, C0_EPC); /* cp0 delay */
  1127. uasm_i_tlbwi(p);
  1128. uasm_i_jr(p, tmp);
  1129. uasm_i_rfe(p); /* branch delay */
  1130. }
  1131. /*
  1132. * This places the pte into ENTRYLO0 and writes it with tlbwi
  1133. * or tlbwr as appropriate. This is because the index register
  1134. * may have the probe fail bit set as a result of a trap on a
  1135. * kseg2 access, i.e. without refill. Then it returns.
  1136. */
  1137. static void __cpuinit
  1138. build_r3000_tlb_reload_write(u32 **p, struct uasm_label **l,
  1139. struct uasm_reloc **r, unsigned int pte,
  1140. unsigned int tmp)
  1141. {
  1142. uasm_i_mfc0(p, tmp, C0_INDEX);
  1143. uasm_i_mtc0(p, pte, C0_ENTRYLO0); /* cp0 delay */
  1144. uasm_il_bltz(p, r, tmp, label_r3000_write_probe_fail); /* cp0 delay */
  1145. uasm_i_mfc0(p, tmp, C0_EPC); /* branch delay */
  1146. uasm_i_tlbwi(p); /* cp0 delay */
  1147. uasm_i_jr(p, tmp);
  1148. uasm_i_rfe(p); /* branch delay */
  1149. uasm_l_r3000_write_probe_fail(l, *p);
  1150. uasm_i_tlbwr(p); /* cp0 delay */
  1151. uasm_i_jr(p, tmp);
  1152. uasm_i_rfe(p); /* branch delay */
  1153. }
  1154. static void __cpuinit
  1155. build_r3000_tlbchange_handler_head(u32 **p, unsigned int pte,
  1156. unsigned int ptr)
  1157. {
  1158. long pgdc = (long)pgd_current;
  1159. uasm_i_mfc0(p, pte, C0_BADVADDR);
  1160. uasm_i_lui(p, ptr, uasm_rel_hi(pgdc)); /* cp0 delay */
  1161. uasm_i_lw(p, ptr, uasm_rel_lo(pgdc), ptr);
  1162. uasm_i_srl(p, pte, pte, 22); /* load delay */
  1163. uasm_i_sll(p, pte, pte, 2);
  1164. uasm_i_addu(p, ptr, ptr, pte);
  1165. uasm_i_mfc0(p, pte, C0_CONTEXT);
  1166. uasm_i_lw(p, ptr, 0, ptr); /* cp0 delay */
  1167. uasm_i_andi(p, pte, pte, 0xffc); /* load delay */
  1168. uasm_i_addu(p, ptr, ptr, pte);
  1169. uasm_i_lw(p, pte, 0, ptr);
  1170. uasm_i_tlbp(p); /* load delay */
  1171. }
  1172. static void __cpuinit build_r3000_tlb_load_handler(void)
  1173. {
  1174. u32 *p = handle_tlbl;
  1175. struct uasm_label *l = labels;
  1176. struct uasm_reloc *r = relocs;
  1177. memset(handle_tlbl, 0, sizeof(handle_tlbl));
  1178. memset(labels, 0, sizeof(labels));
  1179. memset(relocs, 0, sizeof(relocs));
  1180. build_r3000_tlbchange_handler_head(&p, K0, K1);
  1181. build_pte_present(&p, &r, K0, K1, label_nopage_tlbl);
  1182. uasm_i_nop(&p); /* load delay */
  1183. build_make_valid(&p, &r, K0, K1);
  1184. build_r3000_tlb_reload_write(&p, &l, &r, K0, K1);
  1185. uasm_l_nopage_tlbl(&l, p);
  1186. uasm_i_j(&p, (unsigned long)tlb_do_page_fault_0 & 0x0fffffff);
  1187. uasm_i_nop(&p);
  1188. if ((p - handle_tlbl) > FASTPATH_SIZE)
  1189. panic("TLB load handler fastpath space exceeded");
  1190. uasm_resolve_relocs(relocs, labels);
  1191. pr_debug("Wrote TLB load handler fastpath (%u instructions).\n",
  1192. (unsigned int)(p - handle_tlbl));
  1193. dump_handler(handle_tlbl, ARRAY_SIZE(handle_tlbl));
  1194. }
  1195. static void __cpuinit build_r3000_tlb_store_handler(void)
  1196. {
  1197. u32 *p = handle_tlbs;
  1198. struct uasm_label *l = labels;
  1199. struct uasm_reloc *r = relocs;
  1200. memset(handle_tlbs, 0, sizeof(handle_tlbs));
  1201. memset(labels, 0, sizeof(labels));
  1202. memset(relocs, 0, sizeof(relocs));
  1203. build_r3000_tlbchange_handler_head(&p, K0, K1);
  1204. build_pte_writable(&p, &r, K0, K1, label_nopage_tlbs);
  1205. uasm_i_nop(&p); /* load delay */
  1206. build_make_write(&p, &r, K0, K1);
  1207. build_r3000_tlb_reload_write(&p, &l, &r, K0, K1);
  1208. uasm_l_nopage_tlbs(&l, p);
  1209. uasm_i_j(&p, (unsigned long)tlb_do_page_fault_1 & 0x0fffffff);
  1210. uasm_i_nop(&p);
  1211. if ((p - handle_tlbs) > FASTPATH_SIZE)
  1212. panic("TLB store handler fastpath space exceeded");
  1213. uasm_resolve_relocs(relocs, labels);
  1214. pr_debug("Wrote TLB store handler fastpath (%u instructions).\n",
  1215. (unsigned int)(p - handle_tlbs));
  1216. dump_handler(handle_tlbs, ARRAY_SIZE(handle_tlbs));
  1217. }
  1218. static void __cpuinit build_r3000_tlb_modify_handler(void)
  1219. {
  1220. u32 *p = handle_tlbm;
  1221. struct uasm_label *l = labels;
  1222. struct uasm_reloc *r = relocs;
  1223. memset(handle_tlbm, 0, sizeof(handle_tlbm));
  1224. memset(labels, 0, sizeof(labels));
  1225. memset(relocs, 0, sizeof(relocs));
  1226. build_r3000_tlbchange_handler_head(&p, K0, K1);
  1227. build_pte_modifiable(&p, &r, K0, K1, label_nopage_tlbm);
  1228. uasm_i_nop(&p); /* load delay */
  1229. build_make_write(&p, &r, K0, K1);
  1230. build_r3000_pte_reload_tlbwi(&p, K0, K1);
  1231. uasm_l_nopage_tlbm(&l, p);
  1232. uasm_i_j(&p, (unsigned long)tlb_do_page_fault_1 & 0x0fffffff);
  1233. uasm_i_nop(&p);
  1234. if ((p - handle_tlbm) > FASTPATH_SIZE)
  1235. panic("TLB modify handler fastpath space exceeded");
  1236. uasm_resolve_relocs(relocs, labels);
  1237. pr_debug("Wrote TLB modify handler fastpath (%u instructions).\n",
  1238. (unsigned int)(p - handle_tlbm));
  1239. dump_handler(handle_tlbm, ARRAY_SIZE(handle_tlbm));
  1240. }
  1241. #endif /* CONFIG_MIPS_PGD_C0_CONTEXT */
  1242. /*
  1243. * R4000 style TLB load/store/modify handlers.
  1244. */
  1245. static void __cpuinit
  1246. build_r4000_tlbchange_handler_head(u32 **p, struct uasm_label **l,
  1247. struct uasm_reloc **r, unsigned int pte,
  1248. unsigned int ptr)
  1249. {
  1250. #ifdef CONFIG_64BIT
  1251. build_get_pmde64(p, l, r, pte, ptr); /* get pmd in ptr */
  1252. #else
  1253. build_get_pgde32(p, pte, ptr); /* get pgd in ptr */
  1254. #endif
  1255. #ifdef CONFIG_HUGETLB_PAGE
  1256. /*
  1257. * For huge tlb entries, pmd doesn't contain an address but
  1258. * instead contains the tlb pte. Check the PAGE_HUGE bit and
  1259. * see if we need to jump to huge tlb processing.
  1260. */
  1261. build_is_huge_pte(p, r, pte, ptr, label_tlb_huge_update);
  1262. #endif
  1263. UASM_i_MFC0(p, pte, C0_BADVADDR);
  1264. UASM_i_LW(p, ptr, 0, ptr);
  1265. UASM_i_SRL(p, pte, pte, PAGE_SHIFT + PTE_ORDER - PTE_T_LOG2);
  1266. uasm_i_andi(p, pte, pte, (PTRS_PER_PTE - 1) << PTE_T_LOG2);
  1267. UASM_i_ADDU(p, ptr, ptr, pte);
  1268. #ifdef CONFIG_SMP
  1269. uasm_l_smp_pgtable_change(l, *p);
  1270. #endif
  1271. iPTE_LW(p, pte, ptr); /* get even pte */
  1272. if (!m4kc_tlbp_war())
  1273. build_tlb_probe_entry(p);
  1274. }
  1275. static void __cpuinit
  1276. build_r4000_tlbchange_handler_tail(u32 **p, struct uasm_label **l,
  1277. struct uasm_reloc **r, unsigned int tmp,
  1278. unsigned int ptr)
  1279. {
  1280. uasm_i_ori(p, ptr, ptr, sizeof(pte_t));
  1281. uasm_i_xori(p, ptr, ptr, sizeof(pte_t));
  1282. build_update_entries(p, tmp, ptr);
  1283. build_tlb_write_entry(p, l, r, tlb_indexed);
  1284. uasm_l_leave(l, *p);
  1285. uasm_i_eret(p); /* return from trap */
  1286. #ifdef CONFIG_64BIT
  1287. build_get_pgd_vmalloc64(p, l, r, tmp, ptr, not_refill);
  1288. #endif
  1289. }
  1290. static void __cpuinit build_r4000_tlb_load_handler(void)
  1291. {
  1292. u32 *p = handle_tlbl;
  1293. struct uasm_label *l = labels;
  1294. struct uasm_reloc *r = relocs;
  1295. memset(handle_tlbl, 0, sizeof(handle_tlbl));
  1296. memset(labels, 0, sizeof(labels));
  1297. memset(relocs, 0, sizeof(relocs));
  1298. if (bcm1250_m3_war()) {
  1299. unsigned int segbits = 44;
  1300. uasm_i_dmfc0(&p, K0, C0_BADVADDR);
  1301. uasm_i_dmfc0(&p, K1, C0_ENTRYHI);
  1302. uasm_i_xor(&p, K0, K0, K1);
  1303. uasm_i_dsrl_safe(&p, K1, K0, 62);
  1304. uasm_i_dsrl_safe(&p, K0, K0, 12 + 1);
  1305. uasm_i_dsll_safe(&p, K0, K0, 64 + 12 + 1 - segbits);
  1306. uasm_i_or(&p, K0, K0, K1);
  1307. uasm_il_bnez(&p, &r, K0, label_leave);
  1308. /* No need for uasm_i_nop */
  1309. }
  1310. build_r4000_tlbchange_handler_head(&p, &l, &r, K0, K1);
  1311. build_pte_present(&p, &r, K0, K1, label_nopage_tlbl);
  1312. if (m4kc_tlbp_war())
  1313. build_tlb_probe_entry(&p);
  1314. if (kernel_uses_smartmips_rixi) {
  1315. /*
  1316. * If the page is not _PAGE_VALID, RI or XI could not
  1317. * have triggered it. Skip the expensive test..
  1318. */
  1319. uasm_i_andi(&p, K0, K0, _PAGE_VALID);
  1320. uasm_il_beqz(&p, &r, K0, label_tlbl_goaround1);
  1321. uasm_i_nop(&p);
  1322. uasm_i_tlbr(&p);
  1323. /* Examine entrylo 0 or 1 based on ptr. */
  1324. uasm_i_andi(&p, K0, K1, sizeof(pte_t));
  1325. uasm_i_beqz(&p, K0, 8);
  1326. UASM_i_MFC0(&p, K0, C0_ENTRYLO0); /* load it in the delay slot*/
  1327. UASM_i_MFC0(&p, K0, C0_ENTRYLO1); /* load it if ptr is odd */
  1328. /*
  1329. * If the entryLo (now in K0) is valid (bit 1), RI or
  1330. * XI must have triggered it.
  1331. */
  1332. uasm_i_andi(&p, K0, K0, 2);
  1333. uasm_il_bnez(&p, &r, K0, label_nopage_tlbl);
  1334. uasm_l_tlbl_goaround1(&l, p);
  1335. /* Reload the PTE value */
  1336. iPTE_LW(&p, K0, K1);
  1337. }
  1338. build_make_valid(&p, &r, K0, K1);
  1339. build_r4000_tlbchange_handler_tail(&p, &l, &r, K0, K1);
  1340. #ifdef CONFIG_HUGETLB_PAGE
  1341. /*
  1342. * This is the entry point when build_r4000_tlbchange_handler_head
  1343. * spots a huge page.
  1344. */
  1345. uasm_l_tlb_huge_update(&l, p);
  1346. iPTE_LW(&p, K0, K1);
  1347. build_pte_present(&p, &r, K0, K1, label_nopage_tlbl);
  1348. build_tlb_probe_entry(&p);
  1349. if (kernel_uses_smartmips_rixi) {
  1350. /*
  1351. * If the page is not _PAGE_VALID, RI or XI could not
  1352. * have triggered it. Skip the expensive test..
  1353. */
  1354. uasm_i_andi(&p, K0, K0, _PAGE_VALID);
  1355. uasm_il_beqz(&p, &r, K0, label_tlbl_goaround2);
  1356. uasm_i_nop(&p);
  1357. uasm_i_tlbr(&p);
  1358. /* Examine entrylo 0 or 1 based on ptr. */
  1359. uasm_i_andi(&p, K0, K1, sizeof(pte_t));
  1360. uasm_i_beqz(&p, K0, 8);
  1361. UASM_i_MFC0(&p, K0, C0_ENTRYLO0); /* load it in the delay slot*/
  1362. UASM_i_MFC0(&p, K0, C0_ENTRYLO1); /* load it if ptr is odd */
  1363. /*
  1364. * If the entryLo (now in K0) is valid (bit 1), RI or
  1365. * XI must have triggered it.
  1366. */
  1367. uasm_i_andi(&p, K0, K0, 2);
  1368. uasm_il_beqz(&p, &r, K0, label_tlbl_goaround2);
  1369. /* Reload the PTE value */
  1370. iPTE_LW(&p, K0, K1);
  1371. /*
  1372. * We clobbered C0_PAGEMASK, restore it. On the other branch
  1373. * it is restored in build_huge_tlb_write_entry.
  1374. */
  1375. build_restore_pagemask(&p, &r, K0, label_nopage_tlbl);
  1376. uasm_l_tlbl_goaround2(&l, p);
  1377. }
  1378. uasm_i_ori(&p, K0, K0, (_PAGE_ACCESSED | _PAGE_VALID));
  1379. build_huge_handler_tail(&p, &r, &l, K0, K1);
  1380. #endif
  1381. uasm_l_nopage_tlbl(&l, p);
  1382. uasm_i_j(&p, (unsigned long)tlb_do_page_fault_0 & 0x0fffffff);
  1383. uasm_i_nop(&p);
  1384. if ((p - handle_tlbl) > FASTPATH_SIZE)
  1385. panic("TLB load handler fastpath space exceeded");
  1386. uasm_resolve_relocs(relocs, labels);
  1387. pr_debug("Wrote TLB load handler fastpath (%u instructions).\n",
  1388. (unsigned int)(p - handle_tlbl));
  1389. dump_handler(handle_tlbl, ARRAY_SIZE(handle_tlbl));
  1390. }
  1391. static void __cpuinit build_r4000_tlb_store_handler(void)
  1392. {
  1393. u32 *p = handle_tlbs;
  1394. struct uasm_label *l = labels;
  1395. struct uasm_reloc *r = relocs;
  1396. memset(handle_tlbs, 0, sizeof(handle_tlbs));
  1397. memset(labels, 0, sizeof(labels));
  1398. memset(relocs, 0, sizeof(relocs));
  1399. build_r4000_tlbchange_handler_head(&p, &l, &r, K0, K1);
  1400. build_pte_writable(&p, &r, K0, K1, label_nopage_tlbs);
  1401. if (m4kc_tlbp_war())
  1402. build_tlb_probe_entry(&p);
  1403. build_make_write(&p, &r, K0, K1);
  1404. build_r4000_tlbchange_handler_tail(&p, &l, &r, K0, K1);
  1405. #ifdef CONFIG_HUGETLB_PAGE
  1406. /*
  1407. * This is the entry point when
  1408. * build_r4000_tlbchange_handler_head spots a huge page.
  1409. */
  1410. uasm_l_tlb_huge_update(&l, p);
  1411. iPTE_LW(&p, K0, K1);
  1412. build_pte_writable(&p, &r, K0, K1, label_nopage_tlbs);
  1413. build_tlb_probe_entry(&p);
  1414. uasm_i_ori(&p, K0, K0,
  1415. _PAGE_ACCESSED | _PAGE_MODIFIED | _PAGE_VALID | _PAGE_DIRTY);
  1416. build_huge_handler_tail(&p, &r, &l, K0, K1);
  1417. #endif
  1418. uasm_l_nopage_tlbs(&l, p);
  1419. uasm_i_j(&p, (unsigned long)tlb_do_page_fault_1 & 0x0fffffff);
  1420. uasm_i_nop(&p);
  1421. if ((p - handle_tlbs) > FASTPATH_SIZE)
  1422. panic("TLB store handler fastpath space exceeded");
  1423. uasm_resolve_relocs(relocs, labels);
  1424. pr_debug("Wrote TLB store handler fastpath (%u instructions).\n",
  1425. (unsigned int)(p - handle_tlbs));
  1426. dump_handler(handle_tlbs, ARRAY_SIZE(handle_tlbs));
  1427. }
  1428. static void __cpuinit build_r4000_tlb_modify_handler(void)
  1429. {
  1430. u32 *p = handle_tlbm;
  1431. struct uasm_label *l = labels;
  1432. struct uasm_reloc *r = relocs;
  1433. memset(handle_tlbm, 0, sizeof(handle_tlbm));
  1434. memset(labels, 0, sizeof(labels));
  1435. memset(relocs, 0, sizeof(relocs));
  1436. build_r4000_tlbchange_handler_head(&p, &l, &r, K0, K1);
  1437. build_pte_modifiable(&p, &r, K0, K1, label_nopage_tlbm);
  1438. if (m4kc_tlbp_war())
  1439. build_tlb_probe_entry(&p);
  1440. /* Present and writable bits set, set accessed and dirty bits. */
  1441. build_make_write(&p, &r, K0, K1);
  1442. build_r4000_tlbchange_handler_tail(&p, &l, &r, K0, K1);
  1443. #ifdef CONFIG_HUGETLB_PAGE
  1444. /*
  1445. * This is the entry point when
  1446. * build_r4000_tlbchange_handler_head spots a huge page.
  1447. */
  1448. uasm_l_tlb_huge_update(&l, p);
  1449. iPTE_LW(&p, K0, K1);
  1450. build_pte_modifiable(&p, &r, K0, K1, label_nopage_tlbm);
  1451. build_tlb_probe_entry(&p);
  1452. uasm_i_ori(&p, K0, K0,
  1453. _PAGE_ACCESSED | _PAGE_MODIFIED | _PAGE_VALID | _PAGE_DIRTY);
  1454. build_huge_handler_tail(&p, &r, &l, K0, K1);
  1455. #endif
  1456. uasm_l_nopage_tlbm(&l, p);
  1457. uasm_i_j(&p, (unsigned long)tlb_do_page_fault_1 & 0x0fffffff);
  1458. uasm_i_nop(&p);
  1459. if ((p - handle_tlbm) > FASTPATH_SIZE)
  1460. panic("TLB modify handler fastpath space exceeded");
  1461. uasm_resolve_relocs(relocs, labels);
  1462. pr_debug("Wrote TLB modify handler fastpath (%u instructions).\n",
  1463. (unsigned int)(p - handle_tlbm));
  1464. dump_handler(handle_tlbm, ARRAY_SIZE(handle_tlbm));
  1465. }
  1466. void __cpuinit build_tlb_refill_handler(void)
  1467. {
  1468. /*
  1469. * The refill handler is generated per-CPU, multi-node systems
  1470. * may have local storage for it. The other handlers are only
  1471. * needed once.
  1472. */
  1473. static int run_once = 0;
  1474. #ifdef CONFIG_64BIT
  1475. check_for_high_segbits = current_cpu_data.vmbits > (PGDIR_SHIFT + PGD_ORDER + PAGE_SHIFT - 3);
  1476. #endif
  1477. switch (current_cpu_type()) {
  1478. case CPU_R2000:
  1479. case CPU_R3000:
  1480. case CPU_R3000A:
  1481. case CPU_R3081E:
  1482. case CPU_TX3912:
  1483. case CPU_TX3922:
  1484. case CPU_TX3927:
  1485. #ifndef CONFIG_MIPS_PGD_C0_CONTEXT
  1486. build_r3000_tlb_refill_handler();
  1487. if (!run_once) {
  1488. build_r3000_tlb_load_handler();
  1489. build_r3000_tlb_store_handler();
  1490. build_r3000_tlb_modify_handler();
  1491. run_once++;
  1492. }
  1493. #else
  1494. panic("No R3000 TLB refill handler");
  1495. #endif
  1496. break;
  1497. case CPU_R6000:
  1498. case CPU_R6000A:
  1499. panic("No R6000 TLB refill handler yet");
  1500. break;
  1501. case CPU_R8000:
  1502. panic("No R8000 TLB refill handler yet");
  1503. break;
  1504. default:
  1505. if (!run_once) {
  1506. #ifdef CONFIG_MIPS_PGD_C0_CONTEXT
  1507. build_r4000_setup_pgd();
  1508. #endif
  1509. build_r4000_tlb_load_handler();
  1510. build_r4000_tlb_store_handler();
  1511. build_r4000_tlb_modify_handler();
  1512. run_once++;
  1513. }
  1514. build_r4000_tlb_refill_handler();
  1515. }
  1516. }
  1517. void __cpuinit flush_tlb_handlers(void)
  1518. {
  1519. local_flush_icache_range((unsigned long)handle_tlbl,
  1520. (unsigned long)handle_tlbl + sizeof(handle_tlbl));
  1521. local_flush_icache_range((unsigned long)handle_tlbs,
  1522. (unsigned long)handle_tlbs + sizeof(handle_tlbs));
  1523. local_flush_icache_range((unsigned long)handle_tlbm,
  1524. (unsigned long)handle_tlbm + sizeof(handle_tlbm));
  1525. #ifdef CONFIG_MIPS_PGD_C0_CONTEXT
  1526. local_flush_icache_range((unsigned long)tlbmiss_handler_setup_pgd,
  1527. (unsigned long)tlbmiss_handler_setup_pgd + sizeof(handle_tlbm));
  1528. #endif
  1529. }