tlbex.c 54 KB

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