tlbex.c 38 KB

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