tlbex.c 38 KB

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