tlbex.c 39 KB

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