tlbex.c 48 KB

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