tlbex.c 42 KB

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