tlbex.c 70 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585
  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. * Copyright (C) 2011 MIPS Technologies, Inc.
  13. *
  14. * ... and the days got worse and worse and now you see
  15. * I've gone completely out of my mind.
  16. *
  17. * They're coming to take me a away haha
  18. * they're coming to take me a away hoho hihi haha
  19. * to the funny farm where code is beautiful all the time ...
  20. *
  21. * (Condolences to Napoleon XIV)
  22. */
  23. #include <linux/bug.h>
  24. #include <linux/export.h>
  25. #include <linux/kernel.h>
  26. #include <linux/types.h>
  27. #include <linux/smp.h>
  28. #include <linux/string.h>
  29. #include <linux/cache.h>
  30. #include <linux/pgtable.h>
  31. #include <asm/cacheflush.h>
  32. #include <asm/cpu-type.h>
  33. #include <asm/mipsregs.h>
  34. #include <asm/mmu_context.h>
  35. #include <asm/regdef.h>
  36. #include <asm/uasm.h>
  37. #include <asm/setup.h>
  38. #include <asm/tlbex.h>
  39. static int mips_xpa_disabled;
  40. static int __init xpa_disable(char *s)
  41. {
  42. mips_xpa_disabled = 1;
  43. return 1;
  44. }
  45. __setup("noxpa", xpa_disable);
  46. /*
  47. * TLB load/store/modify handlers.
  48. *
  49. * Only the fastpath gets synthesized at runtime, the slowpath for
  50. * do_page_fault remains normal asm.
  51. */
  52. extern void tlb_do_page_fault_0(void);
  53. extern void tlb_do_page_fault_1(void);
  54. struct work_registers {
  55. int r1;
  56. int r2;
  57. int r3;
  58. };
  59. struct tlb_reg_save {
  60. unsigned long a;
  61. unsigned long b;
  62. } ____cacheline_aligned_in_smp;
  63. static struct tlb_reg_save handler_reg_save[NR_CPUS];
  64. static inline int r45k_bvahwbug(void)
  65. {
  66. /* XXX: We should probe for the presence of this bug, but we don't. */
  67. return 0;
  68. }
  69. static inline int r4k_250MHZhwbug(void)
  70. {
  71. /* XXX: We should probe for the presence of this bug, but we don't. */
  72. return 0;
  73. }
  74. extern int sb1250_m3_workaround_needed(void);
  75. static inline int __maybe_unused bcm1250_m3_war(void)
  76. {
  77. if (IS_ENABLED(CONFIG_SB1_PASS_2_WORKAROUNDS))
  78. return sb1250_m3_workaround_needed();
  79. return 0;
  80. }
  81. static inline int __maybe_unused r10000_llsc_war(void)
  82. {
  83. return IS_ENABLED(CONFIG_WAR_R10000_LLSC);
  84. }
  85. static int use_bbit_insns(void)
  86. {
  87. switch (current_cpu_type()) {
  88. case CPU_CAVIUM_OCTEON:
  89. case CPU_CAVIUM_OCTEON_PLUS:
  90. case CPU_CAVIUM_OCTEON2:
  91. case CPU_CAVIUM_OCTEON3:
  92. return 1;
  93. default:
  94. return 0;
  95. }
  96. }
  97. static int use_lwx_insns(void)
  98. {
  99. switch (current_cpu_type()) {
  100. case CPU_CAVIUM_OCTEON2:
  101. case CPU_CAVIUM_OCTEON3:
  102. return 1;
  103. default:
  104. return 0;
  105. }
  106. }
  107. #if defined(CONFIG_CAVIUM_OCTEON_CVMSEG_SIZE) && \
  108. CONFIG_CAVIUM_OCTEON_CVMSEG_SIZE > 0
  109. static bool scratchpad_available(void)
  110. {
  111. return true;
  112. }
  113. static int scratchpad_offset(int i)
  114. {
  115. /*
  116. * CVMSEG starts at address -32768 and extends for
  117. * CAVIUM_OCTEON_CVMSEG_SIZE 128 byte cache lines.
  118. */
  119. i += 1; /* Kernel use starts at the top and works down. */
  120. return CONFIG_CAVIUM_OCTEON_CVMSEG_SIZE * 128 - (8 * i) - 32768;
  121. }
  122. #else
  123. static bool scratchpad_available(void)
  124. {
  125. return false;
  126. }
  127. static int scratchpad_offset(int i)
  128. {
  129. BUG();
  130. /* Really unreachable, but evidently some GCC want this. */
  131. return 0;
  132. }
  133. #endif
  134. /*
  135. * Found by experiment: At least some revisions of the 4kc throw under
  136. * some circumstances a machine check exception, triggered by invalid
  137. * values in the index register. Delaying the tlbp instruction until
  138. * after the next branch, plus adding an additional nop in front of
  139. * tlbwi/tlbwr avoids the invalid index register values. Nobody knows
  140. * why; it's not an issue caused by the core RTL.
  141. *
  142. */
  143. static int m4kc_tlbp_war(void)
  144. {
  145. return current_cpu_type() == CPU_4KC;
  146. }
  147. /* Handle labels (which must be positive integers). */
  148. enum label_id {
  149. label_second_part = 1,
  150. label_leave,
  151. label_vmalloc,
  152. label_vmalloc_done,
  153. label_tlbw_hazard_0,
  154. label_split = label_tlbw_hazard_0 + 8,
  155. label_tlbl_goaround1,
  156. label_tlbl_goaround2,
  157. label_nopage_tlbl,
  158. label_nopage_tlbs,
  159. label_nopage_tlbm,
  160. label_smp_pgtable_change,
  161. label_r3000_write_probe_fail,
  162. label_large_segbits_fault,
  163. #ifdef CONFIG_MIPS_HUGE_TLB_SUPPORT
  164. label_tlb_huge_update,
  165. #endif
  166. };
  167. UASM_L_LA(_second_part)
  168. UASM_L_LA(_leave)
  169. UASM_L_LA(_vmalloc)
  170. UASM_L_LA(_vmalloc_done)
  171. /* _tlbw_hazard_x is handled differently. */
  172. UASM_L_LA(_split)
  173. UASM_L_LA(_tlbl_goaround1)
  174. UASM_L_LA(_tlbl_goaround2)
  175. UASM_L_LA(_nopage_tlbl)
  176. UASM_L_LA(_nopage_tlbs)
  177. UASM_L_LA(_nopage_tlbm)
  178. UASM_L_LA(_smp_pgtable_change)
  179. UASM_L_LA(_r3000_write_probe_fail)
  180. UASM_L_LA(_large_segbits_fault)
  181. #ifdef CONFIG_MIPS_HUGE_TLB_SUPPORT
  182. UASM_L_LA(_tlb_huge_update)
  183. #endif
  184. static int hazard_instance;
  185. static void uasm_bgezl_hazard(u32 **p, struct uasm_reloc **r, int instance)
  186. {
  187. switch (instance) {
  188. case 0 ... 7:
  189. uasm_il_bgezl(p, r, 0, label_tlbw_hazard_0 + instance);
  190. return;
  191. default:
  192. BUG();
  193. }
  194. }
  195. static void uasm_bgezl_label(struct uasm_label **l, u32 **p, int instance)
  196. {
  197. switch (instance) {
  198. case 0 ... 7:
  199. uasm_build_label(l, *p, label_tlbw_hazard_0 + instance);
  200. break;
  201. default:
  202. BUG();
  203. }
  204. }
  205. /*
  206. * pgtable bits are assigned dynamically depending on processor feature
  207. * and statically based on kernel configuration. This spits out the actual
  208. * values the kernel is using. Required to make sense from disassembled
  209. * TLB exception handlers.
  210. */
  211. static void output_pgtable_bits_defines(void)
  212. {
  213. #define pr_define(fmt, ...) \
  214. pr_debug("#define " fmt, ##__VA_ARGS__)
  215. pr_debug("#include <asm/asm.h>\n");
  216. pr_debug("#include <asm/regdef.h>\n");
  217. pr_debug("\n");
  218. pr_define("_PAGE_PRESENT_SHIFT %d\n", _PAGE_PRESENT_SHIFT);
  219. pr_define("_PAGE_NO_READ_SHIFT %d\n", _PAGE_NO_READ_SHIFT);
  220. pr_define("_PAGE_WRITE_SHIFT %d\n", _PAGE_WRITE_SHIFT);
  221. pr_define("_PAGE_ACCESSED_SHIFT %d\n", _PAGE_ACCESSED_SHIFT);
  222. pr_define("_PAGE_MODIFIED_SHIFT %d\n", _PAGE_MODIFIED_SHIFT);
  223. #ifdef CONFIG_MIPS_HUGE_TLB_SUPPORT
  224. pr_define("_PAGE_HUGE_SHIFT %d\n", _PAGE_HUGE_SHIFT);
  225. #endif
  226. #ifdef _PAGE_NO_EXEC_SHIFT
  227. if (cpu_has_rixi)
  228. pr_define("_PAGE_NO_EXEC_SHIFT %d\n", _PAGE_NO_EXEC_SHIFT);
  229. #endif
  230. pr_define("_PAGE_GLOBAL_SHIFT %d\n", _PAGE_GLOBAL_SHIFT);
  231. pr_define("_PAGE_VALID_SHIFT %d\n", _PAGE_VALID_SHIFT);
  232. pr_define("_PAGE_DIRTY_SHIFT %d\n", _PAGE_DIRTY_SHIFT);
  233. pr_define("PFN_PTE_SHIFT %d\n", PFN_PTE_SHIFT);
  234. pr_debug("\n");
  235. }
  236. static inline void dump_handler(const char *symbol, const void *start, const void *end)
  237. {
  238. unsigned int count = (end - start) / sizeof(u32);
  239. const u32 *handler = start;
  240. int i;
  241. pr_debug("LEAF(%s)\n", symbol);
  242. pr_debug("\t.set push\n");
  243. pr_debug("\t.set noreorder\n");
  244. for (i = 0; i < count; i++)
  245. pr_debug("\t.word\t0x%08x\t\t# %p\n", handler[i], &handler[i]);
  246. pr_debug("\t.set\tpop\n");
  247. pr_debug("\tEND(%s)\n", symbol);
  248. }
  249. #ifdef CONFIG_64BIT
  250. # define GET_CONTEXT(buf, reg) UASM_i_MFC0(buf, reg, C0_XCONTEXT)
  251. #else
  252. # define GET_CONTEXT(buf, reg) UASM_i_MFC0(buf, reg, C0_CONTEXT)
  253. #endif
  254. /* The worst case length of the handler is around 18 instructions for
  255. * R3000-style TLBs and up to 63 instructions for R4000-style TLBs.
  256. * Maximum space available is 32 instructions for R3000 and 64
  257. * instructions for R4000.
  258. *
  259. * We deliberately chose a buffer size of 128, so we won't scribble
  260. * over anything important on overflow before we panic.
  261. */
  262. static u32 tlb_handler[128];
  263. /* simply assume worst case size for labels and relocs */
  264. static struct uasm_label labels[128];
  265. static struct uasm_reloc relocs[128];
  266. static int check_for_high_segbits;
  267. static bool fill_includes_sw_bits;
  268. static unsigned int kscratch_used_mask;
  269. static inline int __maybe_unused c0_kscratch(void)
  270. {
  271. return 31;
  272. }
  273. static int allocate_kscratch(void)
  274. {
  275. int r;
  276. unsigned int a = cpu_data[0].kscratch_mask & ~kscratch_used_mask;
  277. r = ffs(a);
  278. if (r == 0)
  279. return -1;
  280. r--; /* make it zero based */
  281. kscratch_used_mask |= (1 << r);
  282. return r;
  283. }
  284. static int scratch_reg;
  285. int pgd_reg;
  286. EXPORT_SYMBOL_GPL(pgd_reg);
  287. enum vmalloc64_mode {not_refill, refill_scratch, refill_noscratch};
  288. static struct work_registers build_get_work_registers(u32 **p)
  289. {
  290. struct work_registers r;
  291. if (scratch_reg >= 0) {
  292. /* Save in CPU local C0_KScratch? */
  293. UASM_i_MTC0(p, 1, c0_kscratch(), scratch_reg);
  294. r.r1 = GPR_K0;
  295. r.r2 = GPR_K1;
  296. r.r3 = GPR_AT;
  297. return r;
  298. }
  299. if (num_possible_cpus() > 1) {
  300. /* Get smp_processor_id */
  301. UASM_i_CPUID_MFC0(p, GPR_K0, SMP_CPUID_REG);
  302. UASM_i_SRL_SAFE(p, GPR_K0, GPR_K0, SMP_CPUID_REGSHIFT);
  303. /* handler_reg_save index in GPR_K0 */
  304. UASM_i_SLL(p, GPR_K0, GPR_K0, ilog2(sizeof(struct tlb_reg_save)));
  305. UASM_i_LA(p, GPR_K1, (long)&handler_reg_save);
  306. UASM_i_ADDU(p, GPR_K0, GPR_K0, GPR_K1);
  307. } else {
  308. UASM_i_LA(p, GPR_K0, (long)&handler_reg_save);
  309. }
  310. /* GPR_K0 now points to save area, save $1 and $2 */
  311. UASM_i_SW(p, 1, offsetof(struct tlb_reg_save, a), GPR_K0);
  312. UASM_i_SW(p, 2, offsetof(struct tlb_reg_save, b), GPR_K0);
  313. r.r1 = GPR_K1;
  314. r.r2 = 1;
  315. r.r3 = 2;
  316. return r;
  317. }
  318. static void build_restore_work_registers(u32 **p)
  319. {
  320. if (scratch_reg >= 0) {
  321. uasm_i_ehb(p);
  322. UASM_i_MFC0(p, 1, c0_kscratch(), scratch_reg);
  323. return;
  324. }
  325. /* GPR_K0 already points to save area, restore $1 and $2 */
  326. UASM_i_LW(p, 1, offsetof(struct tlb_reg_save, a), GPR_K0);
  327. UASM_i_LW(p, 2, offsetof(struct tlb_reg_save, b), GPR_K0);
  328. }
  329. #ifndef CONFIG_MIPS_PGD_C0_CONTEXT
  330. /*
  331. * CONFIG_MIPS_PGD_C0_CONTEXT implies 64 bit and lack of pgd_current,
  332. * we cannot do r3000 under these circumstances.
  333. *
  334. * The R3000 TLB handler is simple.
  335. */
  336. static void build_r3000_tlb_refill_handler(void)
  337. {
  338. long pgdc = (long)pgd_current;
  339. u32 *p;
  340. memset(tlb_handler, 0, sizeof(tlb_handler));
  341. p = tlb_handler;
  342. uasm_i_mfc0(&p, GPR_K0, C0_BADVADDR);
  343. uasm_i_lui(&p, GPR_K1, uasm_rel_hi(pgdc)); /* cp0 delay */
  344. uasm_i_lw(&p, GPR_K1, uasm_rel_lo(pgdc), GPR_K1);
  345. uasm_i_srl(&p, GPR_K0, GPR_K0, 22); /* load delay */
  346. uasm_i_sll(&p, GPR_K0, GPR_K0, 2);
  347. uasm_i_addu(&p, GPR_K1, GPR_K1, GPR_K0);
  348. uasm_i_mfc0(&p, GPR_K0, C0_CONTEXT);
  349. uasm_i_lw(&p, GPR_K1, 0, GPR_K1); /* cp0 delay */
  350. uasm_i_andi(&p, GPR_K0, GPR_K0, 0xffc); /* load delay */
  351. uasm_i_addu(&p, GPR_K1, GPR_K1, GPR_K0);
  352. uasm_i_lw(&p, GPR_K0, 0, GPR_K1);
  353. uasm_i_nop(&p); /* load delay */
  354. uasm_i_mtc0(&p, GPR_K0, C0_ENTRYLO0);
  355. uasm_i_mfc0(&p, GPR_K1, C0_EPC); /* cp0 delay */
  356. uasm_i_tlbwr(&p); /* cp0 delay */
  357. uasm_i_jr(&p, GPR_K1);
  358. uasm_i_rfe(&p); /* branch delay */
  359. if (p > tlb_handler + 32)
  360. panic("TLB refill handler space exceeded");
  361. pr_debug("Wrote TLB refill handler (%u instructions).\n",
  362. (unsigned int)(p - tlb_handler));
  363. memcpy((void *)ebase, tlb_handler, 0x80);
  364. local_flush_icache_range(ebase, ebase + 0x80);
  365. dump_handler("r3000_tlb_refill", (u32 *)ebase, (u32 *)(ebase + 0x80));
  366. }
  367. #endif /* CONFIG_MIPS_PGD_C0_CONTEXT */
  368. /*
  369. * The R4000 TLB handler is much more complicated. We have two
  370. * consecutive handler areas with 32 instructions space each.
  371. * Since they aren't used at the same time, we can overflow in the
  372. * other one.To keep things simple, we first assume linear space,
  373. * then we relocate it to the final handler layout as needed.
  374. */
  375. static u32 final_handler[64];
  376. /*
  377. * Hazards
  378. *
  379. * From the IDT errata for the QED RM5230 (Nevada), processor revision 1.0:
  380. * 2. A timing hazard exists for the TLBP instruction.
  381. *
  382. * stalling_instruction
  383. * TLBP
  384. *
  385. * The JTLB is being read for the TLBP throughout the stall generated by the
  386. * previous instruction. This is not really correct as the stalling instruction
  387. * can modify the address used to access the JTLB. The failure symptom is that
  388. * the TLBP instruction will use an address created for the stalling instruction
  389. * and not the address held in C0_ENHI and thus report the wrong results.
  390. *
  391. * The software work-around is to not allow the instruction preceding the TLBP
  392. * to stall - make it an NOP or some other instruction guaranteed not to stall.
  393. *
  394. * Errata 2 will not be fixed. This errata is also on the R5000.
  395. *
  396. * As if we MIPS hackers wouldn't know how to nop pipelines happy ...
  397. */
  398. static void __maybe_unused build_tlb_probe_entry(u32 **p)
  399. {
  400. switch (current_cpu_type()) {
  401. /* Found by experiment: R4600 v2.0/R4700 needs this, too. */
  402. case CPU_R4600:
  403. case CPU_R4700:
  404. case CPU_R5000:
  405. case CPU_NEVADA:
  406. uasm_i_nop(p);
  407. uasm_i_tlbp(p);
  408. break;
  409. default:
  410. uasm_i_tlbp(p);
  411. break;
  412. }
  413. }
  414. void build_tlb_write_entry(u32 **p, struct uasm_label **l,
  415. struct uasm_reloc **r,
  416. enum tlb_write_entry wmode)
  417. {
  418. void(*tlbw)(u32 **) = NULL;
  419. switch (wmode) {
  420. case tlb_random: tlbw = uasm_i_tlbwr; break;
  421. case tlb_indexed: tlbw = uasm_i_tlbwi; break;
  422. }
  423. if (cpu_has_mips_r2_r6) {
  424. if (cpu_has_mips_r2_exec_hazard)
  425. uasm_i_ehb(p);
  426. tlbw(p);
  427. return;
  428. }
  429. switch (current_cpu_type()) {
  430. case CPU_R4000PC:
  431. case CPU_R4000SC:
  432. case CPU_R4000MC:
  433. case CPU_R4400PC:
  434. case CPU_R4400SC:
  435. case CPU_R4400MC:
  436. /*
  437. * This branch uses up a mtc0 hazard nop slot and saves
  438. * two nops after the tlbw instruction.
  439. */
  440. uasm_bgezl_hazard(p, r, hazard_instance);
  441. tlbw(p);
  442. uasm_bgezl_label(l, p, hazard_instance);
  443. hazard_instance++;
  444. uasm_i_nop(p);
  445. break;
  446. case CPU_R4600:
  447. case CPU_R4700:
  448. uasm_i_nop(p);
  449. tlbw(p);
  450. uasm_i_nop(p);
  451. break;
  452. case CPU_R5000:
  453. case CPU_NEVADA:
  454. uasm_i_nop(p); /* QED specifies 2 nops hazard */
  455. uasm_i_nop(p); /* QED specifies 2 nops hazard */
  456. tlbw(p);
  457. break;
  458. case CPU_R4300:
  459. case CPU_5KC:
  460. case CPU_TX49XX:
  461. case CPU_PR4450:
  462. uasm_i_nop(p);
  463. tlbw(p);
  464. break;
  465. case CPU_R10000:
  466. case CPU_R12000:
  467. case CPU_R14000:
  468. case CPU_R16000:
  469. case CPU_4KC:
  470. case CPU_4KEC:
  471. case CPU_M14KC:
  472. case CPU_M14KEC:
  473. case CPU_SB1:
  474. case CPU_SB1A:
  475. case CPU_4KSC:
  476. case CPU_20KC:
  477. case CPU_25KF:
  478. case CPU_BMIPS32:
  479. case CPU_BMIPS3300:
  480. case CPU_BMIPS4350:
  481. case CPU_BMIPS4380:
  482. case CPU_BMIPS5000:
  483. case CPU_LOONGSON2EF:
  484. case CPU_LOONGSON64:
  485. case CPU_R5500:
  486. if (m4kc_tlbp_war())
  487. uasm_i_nop(p);
  488. fallthrough;
  489. case CPU_ALCHEMY:
  490. tlbw(p);
  491. break;
  492. case CPU_RM7000:
  493. uasm_i_nop(p);
  494. uasm_i_nop(p);
  495. uasm_i_nop(p);
  496. uasm_i_nop(p);
  497. tlbw(p);
  498. break;
  499. case CPU_XBURST:
  500. tlbw(p);
  501. uasm_i_nop(p);
  502. break;
  503. default:
  504. panic("No TLB refill handler yet (CPU type: %d)",
  505. current_cpu_type());
  506. break;
  507. }
  508. }
  509. EXPORT_SYMBOL_GPL(build_tlb_write_entry);
  510. static __maybe_unused void build_convert_pte_to_entrylo(u32 **p,
  511. unsigned int reg)
  512. {
  513. if (_PAGE_GLOBAL_SHIFT == 0) {
  514. /* pte_t is already in EntryLo format */
  515. return;
  516. }
  517. if (cpu_has_rixi && _PAGE_NO_EXEC != 0) {
  518. if (fill_includes_sw_bits) {
  519. UASM_i_ROTR(p, reg, reg, ilog2(_PAGE_GLOBAL));
  520. } else {
  521. UASM_i_SRL(p, reg, reg, ilog2(_PAGE_NO_EXEC));
  522. UASM_i_ROTR(p, reg, reg,
  523. ilog2(_PAGE_GLOBAL) - ilog2(_PAGE_NO_EXEC));
  524. }
  525. } else {
  526. #ifdef CONFIG_PHYS_ADDR_T_64BIT
  527. uasm_i_dsrl_safe(p, reg, reg, ilog2(_PAGE_GLOBAL));
  528. #else
  529. UASM_i_SRL(p, reg, reg, ilog2(_PAGE_GLOBAL));
  530. #endif
  531. }
  532. }
  533. #ifdef CONFIG_MIPS_HUGE_TLB_SUPPORT
  534. static void build_restore_pagemask(u32 **p, struct uasm_reloc **r,
  535. unsigned int tmp, enum label_id lid,
  536. int restore_scratch)
  537. {
  538. if (restore_scratch) {
  539. /*
  540. * Ensure the MFC0 below observes the value written to the
  541. * KScratch register by the prior MTC0.
  542. */
  543. if (scratch_reg >= 0)
  544. uasm_i_ehb(p);
  545. /* Reset default page size */
  546. if (PM_DEFAULT_MASK >> 16) {
  547. uasm_i_lui(p, tmp, PM_DEFAULT_MASK >> 16);
  548. uasm_i_ori(p, tmp, tmp, PM_DEFAULT_MASK & 0xffff);
  549. uasm_i_mtc0(p, tmp, C0_PAGEMASK);
  550. uasm_il_b(p, r, lid);
  551. } else if (PM_DEFAULT_MASK) {
  552. uasm_i_ori(p, tmp, 0, PM_DEFAULT_MASK);
  553. uasm_i_mtc0(p, tmp, C0_PAGEMASK);
  554. uasm_il_b(p, r, lid);
  555. } else {
  556. uasm_i_mtc0(p, 0, C0_PAGEMASK);
  557. uasm_il_b(p, r, lid);
  558. }
  559. if (scratch_reg >= 0)
  560. UASM_i_MFC0(p, 1, c0_kscratch(), scratch_reg);
  561. else
  562. UASM_i_LW(p, 1, scratchpad_offset(0), 0);
  563. } else {
  564. /* Reset default page size */
  565. if (PM_DEFAULT_MASK >> 16) {
  566. uasm_i_lui(p, tmp, PM_DEFAULT_MASK >> 16);
  567. uasm_i_ori(p, tmp, tmp, PM_DEFAULT_MASK & 0xffff);
  568. uasm_il_b(p, r, lid);
  569. uasm_i_mtc0(p, tmp, C0_PAGEMASK);
  570. } else if (PM_DEFAULT_MASK) {
  571. uasm_i_ori(p, tmp, 0, PM_DEFAULT_MASK);
  572. uasm_il_b(p, r, lid);
  573. uasm_i_mtc0(p, tmp, C0_PAGEMASK);
  574. } else {
  575. uasm_il_b(p, r, lid);
  576. uasm_i_mtc0(p, 0, C0_PAGEMASK);
  577. }
  578. }
  579. }
  580. static void build_huge_tlb_write_entry(u32 **p, struct uasm_label **l,
  581. struct uasm_reloc **r,
  582. unsigned int tmp,
  583. enum tlb_write_entry wmode,
  584. int restore_scratch)
  585. {
  586. /* Set huge page tlb entry size */
  587. uasm_i_lui(p, tmp, PM_HUGE_MASK >> 16);
  588. uasm_i_ori(p, tmp, tmp, PM_HUGE_MASK & 0xffff);
  589. uasm_i_mtc0(p, tmp, C0_PAGEMASK);
  590. build_tlb_write_entry(p, l, r, wmode);
  591. build_restore_pagemask(p, r, tmp, label_leave, restore_scratch);
  592. }
  593. /*
  594. * Check if Huge PTE is present, if so then jump to LABEL.
  595. */
  596. static void
  597. build_is_huge_pte(u32 **p, struct uasm_reloc **r, unsigned int tmp,
  598. unsigned int pmd, int lid)
  599. {
  600. UASM_i_LW(p, tmp, 0, pmd);
  601. if (use_bbit_insns()) {
  602. uasm_il_bbit1(p, r, tmp, ilog2(_PAGE_HUGE), lid);
  603. } else {
  604. uasm_i_andi(p, tmp, tmp, _PAGE_HUGE);
  605. uasm_il_bnez(p, r, tmp, lid);
  606. }
  607. }
  608. static void build_huge_update_entries(u32 **p, unsigned int pte,
  609. unsigned int tmp)
  610. {
  611. int small_sequence;
  612. /*
  613. * A huge PTE describes an area the size of the
  614. * configured huge page size. This is twice the
  615. * of the large TLB entry size we intend to use.
  616. * A TLB entry half the size of the configured
  617. * huge page size is configured into entrylo0
  618. * and entrylo1 to cover the contiguous huge PTE
  619. * address space.
  620. */
  621. small_sequence = (HPAGE_SIZE >> 7) < 0x10000;
  622. /* We can clobber tmp. It isn't used after this.*/
  623. if (!small_sequence)
  624. uasm_i_lui(p, tmp, HPAGE_SIZE >> (7 + 16));
  625. build_convert_pte_to_entrylo(p, pte);
  626. UASM_i_MTC0(p, pte, C0_ENTRYLO0); /* load it */
  627. /* convert to entrylo1 */
  628. if (small_sequence)
  629. UASM_i_ADDIU(p, pte, pte, HPAGE_SIZE >> 7);
  630. else
  631. UASM_i_ADDU(p, pte, pte, tmp);
  632. UASM_i_MTC0(p, pte, C0_ENTRYLO1); /* load it */
  633. }
  634. static void build_huge_handler_tail(u32 **p, struct uasm_reloc **r,
  635. struct uasm_label **l,
  636. unsigned int pte,
  637. unsigned int ptr,
  638. unsigned int flush)
  639. {
  640. #ifdef CONFIG_SMP
  641. UASM_i_SC(p, pte, 0, ptr);
  642. uasm_il_beqz(p, r, pte, label_tlb_huge_update);
  643. UASM_i_LW(p, pte, 0, ptr); /* Needed because SC killed our PTE */
  644. #else
  645. UASM_i_SW(p, pte, 0, ptr);
  646. #endif
  647. if (cpu_has_ftlb && flush) {
  648. BUG_ON(!cpu_has_tlbinv);
  649. UASM_i_MFC0(p, ptr, C0_ENTRYHI);
  650. uasm_i_ori(p, ptr, ptr, MIPS_ENTRYHI_EHINV);
  651. UASM_i_MTC0(p, ptr, C0_ENTRYHI);
  652. build_tlb_write_entry(p, l, r, tlb_indexed);
  653. uasm_i_xori(p, ptr, ptr, MIPS_ENTRYHI_EHINV);
  654. UASM_i_MTC0(p, ptr, C0_ENTRYHI);
  655. build_huge_update_entries(p, pte, ptr);
  656. build_huge_tlb_write_entry(p, l, r, pte, tlb_random, 0);
  657. return;
  658. }
  659. build_huge_update_entries(p, pte, ptr);
  660. build_huge_tlb_write_entry(p, l, r, pte, tlb_indexed, 0);
  661. }
  662. #endif /* CONFIG_MIPS_HUGE_TLB_SUPPORT */
  663. #ifdef CONFIG_64BIT
  664. /*
  665. * TMP and PTR are scratch.
  666. * TMP will be clobbered, PTR will hold the pmd entry.
  667. */
  668. void build_get_pmde64(u32 **p, struct uasm_label **l, struct uasm_reloc **r,
  669. unsigned int tmp, unsigned int ptr)
  670. {
  671. #ifndef CONFIG_MIPS_PGD_C0_CONTEXT
  672. long pgdc = (long)pgd_current;
  673. #endif
  674. /*
  675. * The vmalloc handling is not in the hotpath.
  676. */
  677. uasm_i_dmfc0(p, tmp, C0_BADVADDR);
  678. if (check_for_high_segbits) {
  679. /*
  680. * The kernel currently implicitly assumes that the
  681. * MIPS SEGBITS parameter for the processor is
  682. * (PGDIR_SHIFT+PGDIR_BITS) or less, and will never
  683. * allocate virtual addresses outside the maximum
  684. * range for SEGBITS = (PGDIR_SHIFT+PGDIR_BITS). But
  685. * that doesn't prevent user code from accessing the
  686. * higher xuseg addresses. Here, we make sure that
  687. * everything but the lower xuseg addresses goes down
  688. * the module_alloc/vmalloc path.
  689. */
  690. uasm_i_dsrl_safe(p, ptr, tmp, PGDIR_SHIFT + PGD_TABLE_ORDER + PAGE_SHIFT - 3);
  691. uasm_il_bnez(p, r, ptr, label_vmalloc);
  692. } else {
  693. uasm_il_bltz(p, r, tmp, label_vmalloc);
  694. }
  695. /* No uasm_i_nop needed here, since the next insn doesn't touch TMP. */
  696. if (pgd_reg != -1) {
  697. /* pgd is in pgd_reg */
  698. if (cpu_has_ldpte)
  699. UASM_i_MFC0(p, ptr, C0_PWBASE);
  700. else
  701. UASM_i_MFC0(p, ptr, c0_kscratch(), pgd_reg);
  702. } else {
  703. #if defined(CONFIG_MIPS_PGD_C0_CONTEXT)
  704. /*
  705. * &pgd << 11 stored in CONTEXT [23..63].
  706. */
  707. UASM_i_MFC0(p, ptr, C0_CONTEXT);
  708. /* Clear lower 23 bits of context. */
  709. uasm_i_dins(p, ptr, 0, 0, 23);
  710. /* insert bit[63:59] of CAC_BASE into bit[11:6] of ptr */
  711. uasm_i_ori(p, ptr, ptr, ((u64)(CAC_BASE) >> 53));
  712. uasm_i_drotr(p, ptr, ptr, 11);
  713. #elif defined(CONFIG_SMP)
  714. UASM_i_CPUID_MFC0(p, ptr, SMP_CPUID_REG);
  715. uasm_i_dsrl_safe(p, ptr, ptr, SMP_CPUID_PTRSHIFT);
  716. UASM_i_LA_mostly(p, tmp, pgdc);
  717. uasm_i_daddu(p, ptr, ptr, tmp);
  718. uasm_i_dmfc0(p, tmp, C0_BADVADDR);
  719. uasm_i_ld(p, ptr, uasm_rel_lo(pgdc), ptr);
  720. #else
  721. UASM_i_LA_mostly(p, ptr, pgdc);
  722. uasm_i_ld(p, ptr, uasm_rel_lo(pgdc), ptr);
  723. #endif
  724. }
  725. uasm_l_vmalloc_done(l, *p);
  726. /* get pgd offset in bytes */
  727. uasm_i_dsrl_safe(p, tmp, tmp, PGDIR_SHIFT - 3);
  728. uasm_i_andi(p, tmp, tmp, (PTRS_PER_PGD - 1)<<3);
  729. uasm_i_daddu(p, ptr, ptr, tmp); /* add in pgd offset */
  730. #ifndef __PAGETABLE_PUD_FOLDED
  731. uasm_i_dmfc0(p, tmp, C0_BADVADDR); /* get faulting address */
  732. uasm_i_ld(p, ptr, 0, ptr); /* get pud pointer */
  733. uasm_i_dsrl_safe(p, tmp, tmp, PUD_SHIFT - 3); /* get pud offset in bytes */
  734. uasm_i_andi(p, tmp, tmp, (PTRS_PER_PUD - 1) << 3);
  735. uasm_i_daddu(p, ptr, ptr, tmp); /* add in pud offset */
  736. #endif
  737. #ifndef __PAGETABLE_PMD_FOLDED
  738. uasm_i_dmfc0(p, tmp, C0_BADVADDR); /* get faulting address */
  739. uasm_i_ld(p, ptr, 0, ptr); /* get pmd pointer */
  740. uasm_i_dsrl_safe(p, tmp, tmp, PMD_SHIFT-3); /* get pmd offset in bytes */
  741. uasm_i_andi(p, tmp, tmp, (PTRS_PER_PMD - 1)<<3);
  742. uasm_i_daddu(p, ptr, ptr, tmp); /* add in pmd offset */
  743. #endif
  744. }
  745. EXPORT_SYMBOL_GPL(build_get_pmde64);
  746. /*
  747. * BVADDR is the faulting address, PTR is scratch.
  748. * PTR will hold the pgd for vmalloc.
  749. */
  750. static void
  751. build_get_pgd_vmalloc64(u32 **p, struct uasm_label **l, struct uasm_reloc **r,
  752. unsigned int bvaddr, unsigned int ptr,
  753. enum vmalloc64_mode mode)
  754. {
  755. long swpd = (long)swapper_pg_dir;
  756. int single_insn_swpd;
  757. int did_vmalloc_branch = 0;
  758. single_insn_swpd = uasm_in_compat_space_p(swpd) && !uasm_rel_lo(swpd);
  759. uasm_l_vmalloc(l, *p);
  760. if (mode != not_refill && check_for_high_segbits) {
  761. if (single_insn_swpd) {
  762. uasm_il_bltz(p, r, bvaddr, label_vmalloc_done);
  763. uasm_i_lui(p, ptr, uasm_rel_hi(swpd));
  764. did_vmalloc_branch = 1;
  765. /* fall through */
  766. } else {
  767. uasm_il_bgez(p, r, bvaddr, label_large_segbits_fault);
  768. }
  769. }
  770. if (!did_vmalloc_branch) {
  771. if (single_insn_swpd) {
  772. uasm_il_b(p, r, label_vmalloc_done);
  773. uasm_i_lui(p, ptr, uasm_rel_hi(swpd));
  774. } else {
  775. UASM_i_LA_mostly(p, ptr, swpd);
  776. uasm_il_b(p, r, label_vmalloc_done);
  777. if (uasm_in_compat_space_p(swpd))
  778. uasm_i_addiu(p, ptr, ptr, uasm_rel_lo(swpd));
  779. else
  780. uasm_i_daddiu(p, ptr, ptr, uasm_rel_lo(swpd));
  781. }
  782. }
  783. if (mode != not_refill && check_for_high_segbits) {
  784. uasm_l_large_segbits_fault(l, *p);
  785. if (mode == refill_scratch && scratch_reg >= 0)
  786. uasm_i_ehb(p);
  787. /*
  788. * We get here if we are an xsseg address, or if we are
  789. * an xuseg address above (PGDIR_SHIFT+PGDIR_BITS) boundary.
  790. *
  791. * Ignoring xsseg (assume disabled so would generate
  792. * (address errors?), the only remaining possibility
  793. * is the upper xuseg addresses. On processors with
  794. * TLB_SEGBITS <= PGDIR_SHIFT+PGDIR_BITS, these
  795. * addresses would have taken an address error. We try
  796. * to mimic that here by taking a load/istream page
  797. * fault.
  798. */
  799. if (IS_ENABLED(CONFIG_CPU_LOONGSON3_WORKAROUNDS))
  800. uasm_i_sync(p, 0);
  801. UASM_i_LA(p, ptr, (unsigned long)tlb_do_page_fault_0);
  802. uasm_i_jr(p, ptr);
  803. if (mode == refill_scratch) {
  804. if (scratch_reg >= 0)
  805. UASM_i_MFC0(p, 1, c0_kscratch(), scratch_reg);
  806. else
  807. UASM_i_LW(p, 1, scratchpad_offset(0), 0);
  808. } else {
  809. uasm_i_nop(p);
  810. }
  811. }
  812. }
  813. #else /* !CONFIG_64BIT */
  814. /*
  815. * TMP and PTR are scratch.
  816. * TMP will be clobbered, PTR will hold the pgd entry.
  817. */
  818. void build_get_pgde32(u32 **p, unsigned int tmp, unsigned int ptr)
  819. {
  820. if (pgd_reg != -1) {
  821. /* pgd is in pgd_reg */
  822. uasm_i_mfc0(p, ptr, c0_kscratch(), pgd_reg);
  823. uasm_i_mfc0(p, tmp, C0_BADVADDR); /* get faulting address */
  824. } else {
  825. long pgdc = (long)pgd_current;
  826. /* 32 bit SMP has smp_processor_id() stored in CONTEXT. */
  827. #ifdef CONFIG_SMP
  828. uasm_i_mfc0(p, ptr, SMP_CPUID_REG);
  829. UASM_i_LA_mostly(p, tmp, pgdc);
  830. uasm_i_srl(p, ptr, ptr, SMP_CPUID_PTRSHIFT);
  831. uasm_i_addu(p, ptr, tmp, ptr);
  832. #else
  833. UASM_i_LA_mostly(p, ptr, pgdc);
  834. #endif
  835. uasm_i_mfc0(p, tmp, C0_BADVADDR); /* get faulting address */
  836. uasm_i_lw(p, ptr, uasm_rel_lo(pgdc), ptr);
  837. }
  838. uasm_i_srl(p, tmp, tmp, PGDIR_SHIFT); /* get pgd only bits */
  839. uasm_i_sll(p, tmp, tmp, PGD_T_LOG2);
  840. uasm_i_addu(p, ptr, ptr, tmp); /* add in pgd offset */
  841. }
  842. EXPORT_SYMBOL_GPL(build_get_pgde32);
  843. #endif /* !CONFIG_64BIT */
  844. static void build_adjust_context(u32 **p, unsigned int ctx)
  845. {
  846. unsigned int shift = 4 - (PTE_T_LOG2 + 1) + PAGE_SHIFT - 12;
  847. unsigned int mask = (PTRS_PER_PTE / 2 - 1) << (PTE_T_LOG2 + 1);
  848. if (shift)
  849. UASM_i_SRL(p, ctx, ctx, shift);
  850. uasm_i_andi(p, ctx, ctx, mask);
  851. }
  852. void build_get_ptep(u32 **p, unsigned int tmp, unsigned int ptr)
  853. {
  854. /*
  855. * Bug workaround for the Nevada. It seems as if under certain
  856. * circumstances the move from cp0_context might produce a
  857. * bogus result when the mfc0 instruction and its consumer are
  858. * in a different cacheline or a load instruction, probably any
  859. * memory reference, is between them.
  860. */
  861. switch (current_cpu_type()) {
  862. case CPU_NEVADA:
  863. UASM_i_LW(p, ptr, 0, ptr);
  864. GET_CONTEXT(p, tmp); /* get context reg */
  865. break;
  866. default:
  867. GET_CONTEXT(p, tmp); /* get context reg */
  868. UASM_i_LW(p, ptr, 0, ptr);
  869. break;
  870. }
  871. build_adjust_context(p, tmp);
  872. UASM_i_ADDU(p, ptr, ptr, tmp); /* add in offset */
  873. }
  874. EXPORT_SYMBOL_GPL(build_get_ptep);
  875. void build_update_entries(u32 **p, unsigned int tmp, unsigned int ptep)
  876. {
  877. int pte_off_even = 0;
  878. int pte_off_odd = sizeof(pte_t);
  879. #if defined(CONFIG_CPU_MIPS32) && defined(CONFIG_PHYS_ADDR_T_64BIT)
  880. /* The low 32 bits of EntryLo is stored in pte_high */
  881. pte_off_even += offsetof(pte_t, pte_high);
  882. pte_off_odd += offsetof(pte_t, pte_high);
  883. #endif
  884. if (IS_ENABLED(CONFIG_XPA)) {
  885. uasm_i_lw(p, tmp, pte_off_even, ptep); /* even pte */
  886. UASM_i_ROTR(p, tmp, tmp, ilog2(_PAGE_GLOBAL));
  887. UASM_i_MTC0(p, tmp, C0_ENTRYLO0);
  888. if (cpu_has_xpa && !mips_xpa_disabled) {
  889. uasm_i_lw(p, tmp, 0, ptep);
  890. uasm_i_ext(p, tmp, tmp, 0, 24);
  891. uasm_i_mthc0(p, tmp, C0_ENTRYLO0);
  892. }
  893. uasm_i_lw(p, tmp, pte_off_odd, ptep); /* odd pte */
  894. UASM_i_ROTR(p, tmp, tmp, ilog2(_PAGE_GLOBAL));
  895. UASM_i_MTC0(p, tmp, C0_ENTRYLO1);
  896. if (cpu_has_xpa && !mips_xpa_disabled) {
  897. uasm_i_lw(p, tmp, sizeof(pte_t), ptep);
  898. uasm_i_ext(p, tmp, tmp, 0, 24);
  899. uasm_i_mthc0(p, tmp, C0_ENTRYLO1);
  900. }
  901. return;
  902. }
  903. UASM_i_LW(p, tmp, pte_off_even, ptep); /* get even pte */
  904. UASM_i_LW(p, ptep, pte_off_odd, ptep); /* get odd pte */
  905. if (r45k_bvahwbug())
  906. build_tlb_probe_entry(p);
  907. build_convert_pte_to_entrylo(p, tmp);
  908. if (r4k_250MHZhwbug())
  909. UASM_i_MTC0(p, 0, C0_ENTRYLO0);
  910. UASM_i_MTC0(p, tmp, C0_ENTRYLO0); /* load it */
  911. build_convert_pte_to_entrylo(p, ptep);
  912. if (r45k_bvahwbug())
  913. uasm_i_mfc0(p, tmp, C0_INDEX);
  914. if (r4k_250MHZhwbug())
  915. UASM_i_MTC0(p, 0, C0_ENTRYLO1);
  916. UASM_i_MTC0(p, ptep, C0_ENTRYLO1); /* load it */
  917. }
  918. EXPORT_SYMBOL_GPL(build_update_entries);
  919. struct mips_huge_tlb_info {
  920. int huge_pte;
  921. int restore_scratch;
  922. bool need_reload_pte;
  923. };
  924. static struct mips_huge_tlb_info
  925. build_fast_tlb_refill_handler (u32 **p, struct uasm_label **l,
  926. struct uasm_reloc **r, unsigned int tmp,
  927. unsigned int ptr, int c0_scratch_reg)
  928. {
  929. struct mips_huge_tlb_info rv;
  930. unsigned int even, odd;
  931. int vmalloc_branch_delay_filled = 0;
  932. const int scratch = 1; /* Our extra working register */
  933. rv.huge_pte = scratch;
  934. rv.restore_scratch = 0;
  935. rv.need_reload_pte = false;
  936. if (check_for_high_segbits) {
  937. UASM_i_MFC0(p, tmp, C0_BADVADDR);
  938. if (pgd_reg != -1)
  939. UASM_i_MFC0(p, ptr, c0_kscratch(), pgd_reg);
  940. else
  941. UASM_i_MFC0(p, ptr, C0_CONTEXT);
  942. if (c0_scratch_reg >= 0)
  943. UASM_i_MTC0(p, scratch, c0_kscratch(), c0_scratch_reg);
  944. else
  945. UASM_i_SW(p, scratch, scratchpad_offset(0), 0);
  946. uasm_i_dsrl_safe(p, scratch, tmp,
  947. PGDIR_SHIFT + PGD_TABLE_ORDER + PAGE_SHIFT - 3);
  948. uasm_il_bnez(p, r, scratch, label_vmalloc);
  949. if (pgd_reg == -1) {
  950. vmalloc_branch_delay_filled = 1;
  951. /* Clear lower 23 bits of context. */
  952. uasm_i_dins(p, ptr, 0, 0, 23);
  953. }
  954. } else {
  955. if (pgd_reg != -1)
  956. UASM_i_MFC0(p, ptr, c0_kscratch(), pgd_reg);
  957. else
  958. UASM_i_MFC0(p, ptr, C0_CONTEXT);
  959. UASM_i_MFC0(p, tmp, C0_BADVADDR);
  960. if (c0_scratch_reg >= 0)
  961. UASM_i_MTC0(p, scratch, c0_kscratch(), c0_scratch_reg);
  962. else
  963. UASM_i_SW(p, scratch, scratchpad_offset(0), 0);
  964. if (pgd_reg == -1)
  965. /* Clear lower 23 bits of context. */
  966. uasm_i_dins(p, ptr, 0, 0, 23);
  967. uasm_il_bltz(p, r, tmp, label_vmalloc);
  968. }
  969. if (pgd_reg == -1) {
  970. vmalloc_branch_delay_filled = 1;
  971. /* insert bit[63:59] of CAC_BASE into bit[11:6] of ptr */
  972. uasm_i_ori(p, ptr, ptr, ((u64)(CAC_BASE) >> 53));
  973. uasm_i_drotr(p, ptr, ptr, 11);
  974. }
  975. #ifdef __PAGETABLE_PMD_FOLDED
  976. #define LOC_PTEP scratch
  977. #else
  978. #define LOC_PTEP ptr
  979. #endif
  980. if (!vmalloc_branch_delay_filled)
  981. /* get pgd offset in bytes */
  982. uasm_i_dsrl_safe(p, scratch, tmp, PGDIR_SHIFT - 3);
  983. uasm_l_vmalloc_done(l, *p);
  984. /*
  985. * tmp ptr
  986. * fall-through case = badvaddr *pgd_current
  987. * vmalloc case = badvaddr swapper_pg_dir
  988. */
  989. if (vmalloc_branch_delay_filled)
  990. /* get pgd offset in bytes */
  991. uasm_i_dsrl_safe(p, scratch, tmp, PGDIR_SHIFT - 3);
  992. #ifdef __PAGETABLE_PMD_FOLDED
  993. GET_CONTEXT(p, tmp); /* get context reg */
  994. #endif
  995. uasm_i_andi(p, scratch, scratch, (PTRS_PER_PGD - 1) << 3);
  996. if (use_lwx_insns()) {
  997. UASM_i_LWX(p, LOC_PTEP, scratch, ptr);
  998. } else {
  999. uasm_i_daddu(p, ptr, ptr, scratch); /* add in pgd offset */
  1000. uasm_i_ld(p, LOC_PTEP, 0, ptr); /* get pmd pointer */
  1001. }
  1002. #ifndef __PAGETABLE_PUD_FOLDED
  1003. /* get pud offset in bytes */
  1004. uasm_i_dsrl_safe(p, scratch, tmp, PUD_SHIFT - 3);
  1005. uasm_i_andi(p, scratch, scratch, (PTRS_PER_PUD - 1) << 3);
  1006. if (use_lwx_insns()) {
  1007. UASM_i_LWX(p, ptr, scratch, ptr);
  1008. } else {
  1009. uasm_i_daddu(p, ptr, ptr, scratch); /* add in pmd offset */
  1010. UASM_i_LW(p, ptr, 0, ptr);
  1011. }
  1012. /* ptr contains a pointer to PMD entry */
  1013. /* tmp contains the address */
  1014. #endif
  1015. #ifndef __PAGETABLE_PMD_FOLDED
  1016. /* get pmd offset in bytes */
  1017. uasm_i_dsrl_safe(p, scratch, tmp, PMD_SHIFT - 3);
  1018. uasm_i_andi(p, scratch, scratch, (PTRS_PER_PMD - 1) << 3);
  1019. GET_CONTEXT(p, tmp); /* get context reg */
  1020. if (use_lwx_insns()) {
  1021. UASM_i_LWX(p, scratch, scratch, ptr);
  1022. } else {
  1023. uasm_i_daddu(p, ptr, ptr, scratch); /* add in pmd offset */
  1024. UASM_i_LW(p, scratch, 0, ptr);
  1025. }
  1026. #endif
  1027. /* Adjust the context during the load latency. */
  1028. build_adjust_context(p, tmp);
  1029. #ifdef CONFIG_MIPS_HUGE_TLB_SUPPORT
  1030. uasm_il_bbit1(p, r, scratch, ilog2(_PAGE_HUGE), label_tlb_huge_update);
  1031. /*
  1032. * The in the LWX case we don't want to do the load in the
  1033. * delay slot. It cannot issue in the same cycle and may be
  1034. * speculative and unneeded.
  1035. */
  1036. if (use_lwx_insns())
  1037. uasm_i_nop(p);
  1038. #endif /* CONFIG_MIPS_HUGE_TLB_SUPPORT */
  1039. /* build_update_entries */
  1040. if (use_lwx_insns()) {
  1041. even = ptr;
  1042. odd = tmp;
  1043. UASM_i_LWX(p, even, scratch, tmp);
  1044. UASM_i_ADDIU(p, tmp, tmp, sizeof(pte_t));
  1045. UASM_i_LWX(p, odd, scratch, tmp);
  1046. } else {
  1047. UASM_i_ADDU(p, ptr, scratch, tmp); /* add in offset */
  1048. even = tmp;
  1049. odd = ptr;
  1050. UASM_i_LW(p, even, 0, ptr); /* get even pte */
  1051. UASM_i_LW(p, odd, sizeof(pte_t), ptr); /* get odd pte */
  1052. }
  1053. if (cpu_has_rixi) {
  1054. uasm_i_drotr(p, even, even, ilog2(_PAGE_GLOBAL));
  1055. UASM_i_MTC0(p, even, C0_ENTRYLO0); /* load it */
  1056. uasm_i_drotr(p, odd, odd, ilog2(_PAGE_GLOBAL));
  1057. } else {
  1058. uasm_i_dsrl_safe(p, even, even, ilog2(_PAGE_GLOBAL));
  1059. UASM_i_MTC0(p, even, C0_ENTRYLO0); /* load it */
  1060. uasm_i_dsrl_safe(p, odd, odd, ilog2(_PAGE_GLOBAL));
  1061. }
  1062. UASM_i_MTC0(p, odd, C0_ENTRYLO1); /* load it */
  1063. if (c0_scratch_reg >= 0) {
  1064. uasm_i_ehb(p);
  1065. UASM_i_MFC0(p, scratch, c0_kscratch(), c0_scratch_reg);
  1066. build_tlb_write_entry(p, l, r, tlb_random);
  1067. uasm_l_leave(l, *p);
  1068. rv.restore_scratch = 1;
  1069. } else if (PAGE_SHIFT == 14 || PAGE_SHIFT == 13) {
  1070. build_tlb_write_entry(p, l, r, tlb_random);
  1071. uasm_l_leave(l, *p);
  1072. UASM_i_LW(p, scratch, scratchpad_offset(0), 0);
  1073. } else {
  1074. UASM_i_LW(p, scratch, scratchpad_offset(0), 0);
  1075. build_tlb_write_entry(p, l, r, tlb_random);
  1076. uasm_l_leave(l, *p);
  1077. rv.restore_scratch = 1;
  1078. }
  1079. uasm_i_eret(p); /* return from trap */
  1080. return rv;
  1081. }
  1082. /*
  1083. * For a 64-bit kernel, we are using the 64-bit XTLB refill exception
  1084. * because EXL == 0. If we wrap, we can also use the 32 instruction
  1085. * slots before the XTLB refill exception handler which belong to the
  1086. * unused TLB refill exception.
  1087. */
  1088. #define MIPS64_REFILL_INSNS 32
  1089. static void build_r4000_tlb_refill_handler(void)
  1090. {
  1091. u32 *p = tlb_handler;
  1092. struct uasm_label *l = labels;
  1093. struct uasm_reloc *r = relocs;
  1094. u32 *f;
  1095. unsigned int final_len;
  1096. struct mips_huge_tlb_info htlb_info __maybe_unused;
  1097. enum vmalloc64_mode vmalloc_mode __maybe_unused;
  1098. memset(tlb_handler, 0, sizeof(tlb_handler));
  1099. memset(labels, 0, sizeof(labels));
  1100. memset(relocs, 0, sizeof(relocs));
  1101. memset(final_handler, 0, sizeof(final_handler));
  1102. if (IS_ENABLED(CONFIG_64BIT) && (scratch_reg >= 0 || scratchpad_available()) && use_bbit_insns()) {
  1103. htlb_info = build_fast_tlb_refill_handler(&p, &l, &r, GPR_K0, GPR_K1,
  1104. scratch_reg);
  1105. vmalloc_mode = refill_scratch;
  1106. } else {
  1107. htlb_info.huge_pte = GPR_K0;
  1108. htlb_info.restore_scratch = 0;
  1109. htlb_info.need_reload_pte = true;
  1110. vmalloc_mode = refill_noscratch;
  1111. /*
  1112. * create the plain linear handler
  1113. */
  1114. if (bcm1250_m3_war()) {
  1115. unsigned int segbits = 44;
  1116. uasm_i_dmfc0(&p, GPR_K0, C0_BADVADDR);
  1117. uasm_i_dmfc0(&p, GPR_K1, C0_ENTRYHI);
  1118. uasm_i_xor(&p, GPR_K0, GPR_K0, GPR_K1);
  1119. uasm_i_dsrl_safe(&p, GPR_K1, GPR_K0, 62);
  1120. uasm_i_dsrl_safe(&p, GPR_K0, GPR_K0, 12 + 1);
  1121. uasm_i_dsll_safe(&p, GPR_K0, GPR_K0, 64 + 12 + 1 - segbits);
  1122. uasm_i_or(&p, GPR_K0, GPR_K0, GPR_K1);
  1123. uasm_il_bnez(&p, &r, GPR_K0, label_leave);
  1124. /* No need for uasm_i_nop */
  1125. }
  1126. #ifdef CONFIG_64BIT
  1127. build_get_pmde64(&p, &l, &r, GPR_K0, GPR_K1); /* get pmd in GPR_K1 */
  1128. #else
  1129. build_get_pgde32(&p, GPR_K0, GPR_K1); /* get pgd in GPR_K1 */
  1130. #endif
  1131. #ifdef CONFIG_MIPS_HUGE_TLB_SUPPORT
  1132. build_is_huge_pte(&p, &r, GPR_K0, GPR_K1, label_tlb_huge_update);
  1133. #endif
  1134. build_get_ptep(&p, GPR_K0, GPR_K1);
  1135. build_update_entries(&p, GPR_K0, GPR_K1);
  1136. build_tlb_write_entry(&p, &l, &r, tlb_random);
  1137. uasm_l_leave(&l, p);
  1138. uasm_i_eret(&p); /* return from trap */
  1139. }
  1140. #ifdef CONFIG_MIPS_HUGE_TLB_SUPPORT
  1141. uasm_l_tlb_huge_update(&l, p);
  1142. if (htlb_info.need_reload_pte)
  1143. UASM_i_LW(&p, htlb_info.huge_pte, 0, GPR_K1);
  1144. build_huge_update_entries(&p, htlb_info.huge_pte, GPR_K1);
  1145. build_huge_tlb_write_entry(&p, &l, &r, GPR_K0, tlb_random,
  1146. htlb_info.restore_scratch);
  1147. #endif
  1148. #ifdef CONFIG_64BIT
  1149. build_get_pgd_vmalloc64(&p, &l, &r, GPR_K0, GPR_K1, vmalloc_mode);
  1150. #endif
  1151. /*
  1152. * Overflow check: For the 64bit handler, we need at least one
  1153. * free instruction slot for the wrap-around branch. In worst
  1154. * case, if the intended insertion point is a delay slot, we
  1155. * need three, with the second nop'ed and the third being
  1156. * unused.
  1157. */
  1158. switch (boot_cpu_type()) {
  1159. default:
  1160. if (sizeof(long) == 4) {
  1161. fallthrough;
  1162. case CPU_LOONGSON2EF:
  1163. /* Loongson2 ebase is different than r4k, we have more space */
  1164. if ((p - tlb_handler) > 64)
  1165. panic("TLB refill handler space exceeded");
  1166. /*
  1167. * Now fold the handler in the TLB refill handler space.
  1168. */
  1169. f = final_handler;
  1170. /* Simplest case, just copy the handler. */
  1171. uasm_copy_handler(relocs, labels, tlb_handler, p, f);
  1172. final_len = p - tlb_handler;
  1173. break;
  1174. } else {
  1175. if (((p - tlb_handler) > (MIPS64_REFILL_INSNS * 2) - 1)
  1176. || (((p - tlb_handler) > (MIPS64_REFILL_INSNS * 2) - 3)
  1177. && uasm_insn_has_bdelay(relocs,
  1178. tlb_handler + MIPS64_REFILL_INSNS - 3)))
  1179. panic("TLB refill handler space exceeded");
  1180. /*
  1181. * Now fold the handler in the TLB refill handler space.
  1182. */
  1183. f = final_handler + MIPS64_REFILL_INSNS;
  1184. if ((p - tlb_handler) <= MIPS64_REFILL_INSNS) {
  1185. /* Just copy the handler. */
  1186. uasm_copy_handler(relocs, labels, tlb_handler, p, f);
  1187. final_len = p - tlb_handler;
  1188. } else {
  1189. #ifdef CONFIG_MIPS_HUGE_TLB_SUPPORT
  1190. const enum label_id ls = label_tlb_huge_update;
  1191. #else
  1192. const enum label_id ls = label_vmalloc;
  1193. #endif
  1194. u32 *split;
  1195. int ov = 0;
  1196. int i;
  1197. for (i = 0; i < ARRAY_SIZE(labels) && labels[i].lab != ls; i++)
  1198. ;
  1199. BUG_ON(i == ARRAY_SIZE(labels));
  1200. split = labels[i].addr;
  1201. /*
  1202. * See if we have overflown one way or the other.
  1203. */
  1204. if (split > tlb_handler + MIPS64_REFILL_INSNS ||
  1205. split < p - MIPS64_REFILL_INSNS)
  1206. ov = 1;
  1207. if (ov) {
  1208. /*
  1209. * Split two instructions before the end. One
  1210. * for the branch and one for the instruction
  1211. * in the delay slot.
  1212. */
  1213. split = tlb_handler + MIPS64_REFILL_INSNS - 2;
  1214. /*
  1215. * If the branch would fall in a delay slot,
  1216. * we must back up an additional instruction
  1217. * so that it is no longer in a delay slot.
  1218. */
  1219. if (uasm_insn_has_bdelay(relocs, split - 1))
  1220. split--;
  1221. }
  1222. /* Copy first part of the handler. */
  1223. uasm_copy_handler(relocs, labels, tlb_handler, split, f);
  1224. f += split - tlb_handler;
  1225. if (ov) {
  1226. /* Insert branch. */
  1227. uasm_l_split(&l, final_handler);
  1228. uasm_il_b(&f, &r, label_split);
  1229. if (uasm_insn_has_bdelay(relocs, split))
  1230. uasm_i_nop(&f);
  1231. else {
  1232. uasm_copy_handler(relocs, labels,
  1233. split, split + 1, f);
  1234. uasm_move_labels(labels, f, f + 1, -1);
  1235. f++;
  1236. split++;
  1237. }
  1238. }
  1239. /* Copy the rest of the handler. */
  1240. uasm_copy_handler(relocs, labels, split, p, final_handler);
  1241. final_len = (f - (final_handler + MIPS64_REFILL_INSNS)) +
  1242. (p - split);
  1243. }
  1244. }
  1245. break;
  1246. }
  1247. uasm_resolve_relocs(relocs, labels);
  1248. pr_debug("Wrote TLB refill handler (%u instructions).\n",
  1249. final_len);
  1250. memcpy((void *)ebase, final_handler, 0x100);
  1251. local_flush_icache_range(ebase, ebase + 0x100);
  1252. dump_handler("r4000_tlb_refill", (u32 *)ebase, (u32 *)(ebase + 0x100));
  1253. }
  1254. static void setup_pw(void)
  1255. {
  1256. unsigned int pwctl;
  1257. unsigned long pgd_i, pgd_w;
  1258. #ifndef __PAGETABLE_PMD_FOLDED
  1259. unsigned long pmd_i, pmd_w;
  1260. #endif
  1261. unsigned long pt_i, pt_w;
  1262. unsigned long pte_i, pte_w;
  1263. #ifdef CONFIG_MIPS_HUGE_TLB_SUPPORT
  1264. unsigned long psn;
  1265. psn = ilog2(_PAGE_HUGE); /* bit used to indicate huge page */
  1266. #endif
  1267. pgd_i = PGDIR_SHIFT; /* 1st level PGD */
  1268. #ifndef __PAGETABLE_PMD_FOLDED
  1269. pgd_w = PGDIR_SHIFT - PMD_SHIFT + PGD_TABLE_ORDER;
  1270. pmd_i = PMD_SHIFT; /* 2nd level PMD */
  1271. pmd_w = PMD_SHIFT - PAGE_SHIFT;
  1272. #else
  1273. pgd_w = PGDIR_SHIFT - PAGE_SHIFT + PGD_TABLE_ORDER;
  1274. #endif
  1275. pt_i = PAGE_SHIFT; /* 3rd level PTE */
  1276. pt_w = PAGE_SHIFT - 3;
  1277. pte_i = ilog2(_PAGE_GLOBAL);
  1278. pte_w = 0;
  1279. pwctl = 1 << 30; /* Set PWDirExt */
  1280. #ifndef __PAGETABLE_PMD_FOLDED
  1281. write_c0_pwfield(pgd_i << 24 | pmd_i << 12 | pt_i << 6 | pte_i);
  1282. write_c0_pwsize(1 << 30 | pgd_w << 24 | pmd_w << 12 | pt_w << 6 | pte_w);
  1283. #else
  1284. write_c0_pwfield(pgd_i << 24 | pt_i << 6 | pte_i);
  1285. write_c0_pwsize(1 << 30 | pgd_w << 24 | pt_w << 6 | pte_w);
  1286. #endif
  1287. #ifdef CONFIG_MIPS_HUGE_TLB_SUPPORT
  1288. pwctl |= (1 << 6 | psn);
  1289. #endif
  1290. write_c0_pwctl(pwctl);
  1291. write_c0_kpgd((long)swapper_pg_dir);
  1292. kscratch_used_mask |= (1 << 7); /* KScratch6 is used for KPGD */
  1293. }
  1294. static void build_loongson3_tlb_refill_handler(void)
  1295. {
  1296. u32 *p = tlb_handler;
  1297. struct uasm_label *l = labels;
  1298. struct uasm_reloc *r = relocs;
  1299. memset(labels, 0, sizeof(labels));
  1300. memset(relocs, 0, sizeof(relocs));
  1301. memset(tlb_handler, 0, sizeof(tlb_handler));
  1302. if (check_for_high_segbits) {
  1303. uasm_i_dmfc0(&p, GPR_K0, C0_BADVADDR);
  1304. uasm_i_dsrl_safe(&p, GPR_K1, GPR_K0,
  1305. PGDIR_SHIFT + PGD_TABLE_ORDER + PAGE_SHIFT - 3);
  1306. uasm_il_beqz(&p, &r, GPR_K1, label_vmalloc);
  1307. uasm_i_nop(&p);
  1308. uasm_il_bgez(&p, &r, GPR_K0, label_large_segbits_fault);
  1309. uasm_i_nop(&p);
  1310. uasm_l_vmalloc(&l, p);
  1311. }
  1312. uasm_i_dmfc0(&p, GPR_K1, C0_PGD);
  1313. uasm_i_lddir(&p, GPR_K0, GPR_K1, 3); /* global page dir */
  1314. #ifndef __PAGETABLE_PMD_FOLDED
  1315. uasm_i_lddir(&p, GPR_K1, GPR_K0, 1); /* middle page dir */
  1316. #endif
  1317. uasm_i_ldpte(&p, GPR_K1, 0); /* even */
  1318. uasm_i_ldpte(&p, GPR_K1, 1); /* odd */
  1319. uasm_i_tlbwr(&p);
  1320. /* restore page mask */
  1321. if (PM_DEFAULT_MASK >> 16) {
  1322. uasm_i_lui(&p, GPR_K0, PM_DEFAULT_MASK >> 16);
  1323. uasm_i_ori(&p, GPR_K0, GPR_K0, PM_DEFAULT_MASK & 0xffff);
  1324. uasm_i_mtc0(&p, GPR_K0, C0_PAGEMASK);
  1325. } else if (PM_DEFAULT_MASK) {
  1326. uasm_i_ori(&p, GPR_K0, 0, PM_DEFAULT_MASK);
  1327. uasm_i_mtc0(&p, GPR_K0, C0_PAGEMASK);
  1328. } else {
  1329. uasm_i_mtc0(&p, 0, C0_PAGEMASK);
  1330. }
  1331. uasm_i_eret(&p);
  1332. if (check_for_high_segbits) {
  1333. uasm_l_large_segbits_fault(&l, p);
  1334. UASM_i_LA(&p, GPR_K1, (unsigned long)tlb_do_page_fault_0);
  1335. uasm_i_jr(&p, GPR_K1);
  1336. uasm_i_nop(&p);
  1337. }
  1338. uasm_resolve_relocs(relocs, labels);
  1339. memcpy((void *)(ebase + 0x80), tlb_handler, 0x80);
  1340. local_flush_icache_range(ebase + 0x80, ebase + 0x100);
  1341. dump_handler("loongson3_tlb_refill",
  1342. (u32 *)(ebase + 0x80), (u32 *)(ebase + 0x100));
  1343. }
  1344. static void build_setup_pgd(void)
  1345. {
  1346. const int a0 = 4;
  1347. const int __maybe_unused a1 = 5;
  1348. const int __maybe_unused a2 = 6;
  1349. u32 *p = (u32 *)msk_isa16_mode((ulong)tlbmiss_handler_setup_pgd);
  1350. #ifndef CONFIG_MIPS_PGD_C0_CONTEXT
  1351. long pgdc = (long)pgd_current;
  1352. #endif
  1353. memset(p, 0, tlbmiss_handler_setup_pgd_end - (char *)p);
  1354. memset(labels, 0, sizeof(labels));
  1355. memset(relocs, 0, sizeof(relocs));
  1356. pgd_reg = allocate_kscratch();
  1357. #ifdef CONFIG_MIPS_PGD_C0_CONTEXT
  1358. if (pgd_reg == -1) {
  1359. struct uasm_label *l = labels;
  1360. struct uasm_reloc *r = relocs;
  1361. /* PGD << 11 in c0_Context */
  1362. /*
  1363. * If it is a ckseg0 address, convert to a physical
  1364. * address. Shifting right by 29 and adding 4 will
  1365. * result in zero for these addresses.
  1366. *
  1367. */
  1368. UASM_i_SRA(&p, a1, a0, 29);
  1369. UASM_i_ADDIU(&p, a1, a1, 4);
  1370. uasm_il_bnez(&p, &r, a1, label_tlbl_goaround1);
  1371. uasm_i_nop(&p);
  1372. uasm_i_dinsm(&p, a0, 0, 29, 64 - 29);
  1373. uasm_l_tlbl_goaround1(&l, p);
  1374. UASM_i_SLL(&p, a0, a0, 11);
  1375. UASM_i_MTC0(&p, a0, C0_CONTEXT);
  1376. uasm_i_jr(&p, 31);
  1377. uasm_i_ehb(&p);
  1378. } else {
  1379. /* PGD in c0_KScratch */
  1380. if (cpu_has_ldpte)
  1381. UASM_i_MTC0(&p, a0, C0_PWBASE);
  1382. else
  1383. UASM_i_MTC0(&p, a0, c0_kscratch(), pgd_reg);
  1384. uasm_i_jr(&p, 31);
  1385. uasm_i_ehb(&p);
  1386. }
  1387. #else
  1388. #ifdef CONFIG_SMP
  1389. /* Save PGD to pgd_current[smp_processor_id()] */
  1390. UASM_i_CPUID_MFC0(&p, a1, SMP_CPUID_REG);
  1391. UASM_i_SRL_SAFE(&p, a1, a1, SMP_CPUID_PTRSHIFT);
  1392. UASM_i_LA_mostly(&p, a2, pgdc);
  1393. UASM_i_ADDU(&p, a2, a2, a1);
  1394. UASM_i_SW(&p, a0, uasm_rel_lo(pgdc), a2);
  1395. #else
  1396. UASM_i_LA_mostly(&p, a2, pgdc);
  1397. UASM_i_SW(&p, a0, uasm_rel_lo(pgdc), a2);
  1398. #endif /* SMP */
  1399. /* if pgd_reg is allocated, save PGD also to scratch register */
  1400. if (pgd_reg != -1) {
  1401. UASM_i_MTC0(&p, a0, c0_kscratch(), pgd_reg);
  1402. uasm_i_jr(&p, 31);
  1403. uasm_i_ehb(&p);
  1404. } else {
  1405. uasm_i_jr(&p, 31);
  1406. uasm_i_nop(&p);
  1407. }
  1408. #endif
  1409. if (p >= (u32 *)tlbmiss_handler_setup_pgd_end)
  1410. panic("tlbmiss_handler_setup_pgd space exceeded");
  1411. uasm_resolve_relocs(relocs, labels);
  1412. pr_debug("Wrote tlbmiss_handler_setup_pgd (%u instructions).\n",
  1413. (unsigned int)(p - (u32 *)tlbmiss_handler_setup_pgd));
  1414. dump_handler("tlbmiss_handler", tlbmiss_handler_setup_pgd,
  1415. tlbmiss_handler_setup_pgd_end);
  1416. }
  1417. static void
  1418. iPTE_LW(u32 **p, unsigned int pte, unsigned int ptr)
  1419. {
  1420. #ifdef CONFIG_SMP
  1421. if (IS_ENABLED(CONFIG_CPU_LOONGSON3_WORKAROUNDS))
  1422. uasm_i_sync(p, 0);
  1423. # ifdef CONFIG_PHYS_ADDR_T_64BIT
  1424. if (cpu_has_64bits)
  1425. uasm_i_lld(p, pte, 0, ptr);
  1426. else
  1427. # endif
  1428. UASM_i_LL(p, pte, 0, ptr);
  1429. #else
  1430. # ifdef CONFIG_PHYS_ADDR_T_64BIT
  1431. if (cpu_has_64bits)
  1432. uasm_i_ld(p, pte, 0, ptr);
  1433. else
  1434. # endif
  1435. UASM_i_LW(p, pte, 0, ptr);
  1436. #endif
  1437. }
  1438. static void
  1439. iPTE_SW(u32 **p, struct uasm_reloc **r, unsigned int pte, unsigned int ptr,
  1440. unsigned int mode, unsigned int scratch)
  1441. {
  1442. unsigned int hwmode = mode & (_PAGE_VALID | _PAGE_DIRTY);
  1443. unsigned int swmode = mode & ~hwmode;
  1444. if (IS_ENABLED(CONFIG_XPA) && !cpu_has_64bits) {
  1445. uasm_i_lui(p, scratch, swmode >> 16);
  1446. uasm_i_or(p, pte, pte, scratch);
  1447. BUG_ON(swmode & 0xffff);
  1448. } else {
  1449. uasm_i_ori(p, pte, pte, mode);
  1450. }
  1451. #ifdef CONFIG_SMP
  1452. # ifdef CONFIG_PHYS_ADDR_T_64BIT
  1453. if (cpu_has_64bits)
  1454. uasm_i_scd(p, pte, 0, ptr);
  1455. else
  1456. # endif
  1457. UASM_i_SC(p, pte, 0, ptr);
  1458. if (r10000_llsc_war())
  1459. uasm_il_beqzl(p, r, pte, label_smp_pgtable_change);
  1460. else
  1461. uasm_il_beqz(p, r, pte, label_smp_pgtable_change);
  1462. # ifdef CONFIG_PHYS_ADDR_T_64BIT
  1463. if (!cpu_has_64bits) {
  1464. /* no uasm_i_nop needed */
  1465. uasm_i_ll(p, pte, sizeof(pte_t) / 2, ptr);
  1466. uasm_i_ori(p, pte, pte, hwmode);
  1467. BUG_ON(hwmode & ~0xffff);
  1468. uasm_i_sc(p, pte, sizeof(pte_t) / 2, ptr);
  1469. uasm_il_beqz(p, r, pte, label_smp_pgtable_change);
  1470. /* no uasm_i_nop needed */
  1471. uasm_i_lw(p, pte, 0, ptr);
  1472. } else
  1473. uasm_i_nop(p);
  1474. # else
  1475. uasm_i_nop(p);
  1476. # endif
  1477. #else
  1478. # ifdef CONFIG_PHYS_ADDR_T_64BIT
  1479. if (cpu_has_64bits)
  1480. uasm_i_sd(p, pte, 0, ptr);
  1481. else
  1482. # endif
  1483. UASM_i_SW(p, pte, 0, ptr);
  1484. # ifdef CONFIG_PHYS_ADDR_T_64BIT
  1485. if (!cpu_has_64bits) {
  1486. uasm_i_lw(p, pte, sizeof(pte_t) / 2, ptr);
  1487. uasm_i_ori(p, pte, pte, hwmode);
  1488. BUG_ON(hwmode & ~0xffff);
  1489. uasm_i_sw(p, pte, sizeof(pte_t) / 2, ptr);
  1490. uasm_i_lw(p, pte, 0, ptr);
  1491. }
  1492. # endif
  1493. #endif
  1494. }
  1495. /*
  1496. * Check if PTE is present, if not then jump to LABEL. PTR points to
  1497. * the page table where this PTE is located, PTE will be re-loaded
  1498. * with its original value.
  1499. */
  1500. static void
  1501. build_pte_present(u32 **p, struct uasm_reloc **r,
  1502. int pte, int ptr, int scratch, enum label_id lid)
  1503. {
  1504. int t = scratch >= 0 ? scratch : pte;
  1505. int cur = pte;
  1506. if (cpu_has_rixi) {
  1507. if (use_bbit_insns()) {
  1508. uasm_il_bbit0(p, r, pte, ilog2(_PAGE_PRESENT), lid);
  1509. uasm_i_nop(p);
  1510. } else {
  1511. if (_PAGE_PRESENT_SHIFT) {
  1512. uasm_i_srl(p, t, cur, _PAGE_PRESENT_SHIFT);
  1513. cur = t;
  1514. }
  1515. uasm_i_andi(p, t, cur, 1);
  1516. uasm_il_beqz(p, r, t, lid);
  1517. if (pte == t)
  1518. /* You lose the SMP race :-(*/
  1519. iPTE_LW(p, pte, ptr);
  1520. }
  1521. } else {
  1522. if (_PAGE_PRESENT_SHIFT) {
  1523. uasm_i_srl(p, t, cur, _PAGE_PRESENT_SHIFT);
  1524. cur = t;
  1525. }
  1526. uasm_i_andi(p, t, cur,
  1527. (_PAGE_PRESENT | _PAGE_NO_READ) >> _PAGE_PRESENT_SHIFT);
  1528. uasm_i_xori(p, t, t, _PAGE_PRESENT >> _PAGE_PRESENT_SHIFT);
  1529. uasm_il_bnez(p, r, t, lid);
  1530. if (pte == t)
  1531. /* You lose the SMP race :-(*/
  1532. iPTE_LW(p, pte, ptr);
  1533. }
  1534. }
  1535. /* Make PTE valid, store result in PTR. */
  1536. static void
  1537. build_make_valid(u32 **p, struct uasm_reloc **r, unsigned int pte,
  1538. unsigned int ptr, unsigned int scratch)
  1539. {
  1540. unsigned int mode = _PAGE_VALID | _PAGE_ACCESSED;
  1541. iPTE_SW(p, r, pte, ptr, mode, scratch);
  1542. }
  1543. /*
  1544. * Check if PTE can be written to, if not branch to LABEL. Regardless
  1545. * restore PTE with value from PTR when done.
  1546. */
  1547. static void
  1548. build_pte_writable(u32 **p, struct uasm_reloc **r,
  1549. unsigned int pte, unsigned int ptr, int scratch,
  1550. enum label_id lid)
  1551. {
  1552. int t = scratch >= 0 ? scratch : pte;
  1553. int cur = pte;
  1554. if (_PAGE_PRESENT_SHIFT) {
  1555. uasm_i_srl(p, t, cur, _PAGE_PRESENT_SHIFT);
  1556. cur = t;
  1557. }
  1558. uasm_i_andi(p, t, cur,
  1559. (_PAGE_PRESENT | _PAGE_WRITE) >> _PAGE_PRESENT_SHIFT);
  1560. uasm_i_xori(p, t, t,
  1561. (_PAGE_PRESENT | _PAGE_WRITE) >> _PAGE_PRESENT_SHIFT);
  1562. uasm_il_bnez(p, r, t, lid);
  1563. if (pte == t)
  1564. /* You lose the SMP race :-(*/
  1565. iPTE_LW(p, pte, ptr);
  1566. else
  1567. uasm_i_nop(p);
  1568. }
  1569. /* Make PTE writable, update software status bits as well, then store
  1570. * at PTR.
  1571. */
  1572. static void
  1573. build_make_write(u32 **p, struct uasm_reloc **r, unsigned int pte,
  1574. unsigned int ptr, unsigned int scratch)
  1575. {
  1576. unsigned int mode = (_PAGE_ACCESSED | _PAGE_MODIFIED | _PAGE_VALID
  1577. | _PAGE_DIRTY);
  1578. iPTE_SW(p, r, pte, ptr, mode, scratch);
  1579. }
  1580. /*
  1581. * Check if PTE can be modified, if not branch to LABEL. Regardless
  1582. * restore PTE with value from PTR when done.
  1583. */
  1584. static void
  1585. build_pte_modifiable(u32 **p, struct uasm_reloc **r,
  1586. unsigned int pte, unsigned int ptr, int scratch,
  1587. enum label_id lid)
  1588. {
  1589. if (use_bbit_insns()) {
  1590. uasm_il_bbit0(p, r, pte, ilog2(_PAGE_WRITE), lid);
  1591. uasm_i_nop(p);
  1592. } else {
  1593. int t = scratch >= 0 ? scratch : pte;
  1594. uasm_i_srl(p, t, pte, _PAGE_WRITE_SHIFT);
  1595. uasm_i_andi(p, t, t, 1);
  1596. uasm_il_beqz(p, r, t, lid);
  1597. if (pte == t)
  1598. /* You lose the SMP race :-(*/
  1599. iPTE_LW(p, pte, ptr);
  1600. }
  1601. }
  1602. #ifndef CONFIG_MIPS_PGD_C0_CONTEXT
  1603. /*
  1604. * R3000 style TLB load/store/modify handlers.
  1605. */
  1606. /*
  1607. * This places the pte into ENTRYLO0 and writes it with tlbwi.
  1608. * Then it returns.
  1609. */
  1610. static void
  1611. build_r3000_pte_reload_tlbwi(u32 **p, unsigned int pte, unsigned int tmp)
  1612. {
  1613. uasm_i_mtc0(p, pte, C0_ENTRYLO0); /* cp0 delay */
  1614. uasm_i_mfc0(p, tmp, C0_EPC); /* cp0 delay */
  1615. uasm_i_tlbwi(p);
  1616. uasm_i_jr(p, tmp);
  1617. uasm_i_rfe(p); /* branch delay */
  1618. }
  1619. /*
  1620. * This places the pte into ENTRYLO0 and writes it with tlbwi
  1621. * or tlbwr as appropriate. This is because the index register
  1622. * may have the probe fail bit set as a result of a trap on a
  1623. * kseg2 access, i.e. without refill. Then it returns.
  1624. */
  1625. static void
  1626. build_r3000_tlb_reload_write(u32 **p, struct uasm_label **l,
  1627. struct uasm_reloc **r, unsigned int pte,
  1628. unsigned int tmp)
  1629. {
  1630. uasm_i_mfc0(p, tmp, C0_INDEX);
  1631. uasm_i_mtc0(p, pte, C0_ENTRYLO0); /* cp0 delay */
  1632. uasm_il_bltz(p, r, tmp, label_r3000_write_probe_fail); /* cp0 delay */
  1633. uasm_i_mfc0(p, tmp, C0_EPC); /* branch delay */
  1634. uasm_i_tlbwi(p); /* cp0 delay */
  1635. uasm_i_jr(p, tmp);
  1636. uasm_i_rfe(p); /* branch delay */
  1637. uasm_l_r3000_write_probe_fail(l, *p);
  1638. uasm_i_tlbwr(p); /* cp0 delay */
  1639. uasm_i_jr(p, tmp);
  1640. uasm_i_rfe(p); /* branch delay */
  1641. }
  1642. static void
  1643. build_r3000_tlbchange_handler_head(u32 **p, unsigned int pte,
  1644. unsigned int ptr)
  1645. {
  1646. long pgdc = (long)pgd_current;
  1647. uasm_i_mfc0(p, pte, C0_BADVADDR);
  1648. uasm_i_lui(p, ptr, uasm_rel_hi(pgdc)); /* cp0 delay */
  1649. uasm_i_lw(p, ptr, uasm_rel_lo(pgdc), ptr);
  1650. uasm_i_srl(p, pte, pte, 22); /* load delay */
  1651. uasm_i_sll(p, pte, pte, 2);
  1652. uasm_i_addu(p, ptr, ptr, pte);
  1653. uasm_i_mfc0(p, pte, C0_CONTEXT);
  1654. uasm_i_lw(p, ptr, 0, ptr); /* cp0 delay */
  1655. uasm_i_andi(p, pte, pte, 0xffc); /* load delay */
  1656. uasm_i_addu(p, ptr, ptr, pte);
  1657. uasm_i_lw(p, pte, 0, ptr);
  1658. uasm_i_tlbp(p); /* load delay */
  1659. }
  1660. static void build_r3000_tlb_load_handler(void)
  1661. {
  1662. u32 *p = (u32 *)handle_tlbl;
  1663. struct uasm_label *l = labels;
  1664. struct uasm_reloc *r = relocs;
  1665. memset(p, 0, handle_tlbl_end - (char *)p);
  1666. memset(labels, 0, sizeof(labels));
  1667. memset(relocs, 0, sizeof(relocs));
  1668. build_r3000_tlbchange_handler_head(&p, GPR_K0, GPR_K1);
  1669. build_pte_present(&p, &r, GPR_K0, GPR_K1, -1, label_nopage_tlbl);
  1670. uasm_i_nop(&p); /* load delay */
  1671. build_make_valid(&p, &r, GPR_K0, GPR_K1, -1);
  1672. build_r3000_tlb_reload_write(&p, &l, &r, GPR_K0, GPR_K1);
  1673. uasm_l_nopage_tlbl(&l, p);
  1674. uasm_i_j(&p, (unsigned long)tlb_do_page_fault_0 & 0x0fffffff);
  1675. uasm_i_nop(&p);
  1676. if (p >= (u32 *)handle_tlbl_end)
  1677. panic("TLB load handler fastpath space exceeded");
  1678. uasm_resolve_relocs(relocs, labels);
  1679. pr_debug("Wrote TLB load handler fastpath (%u instructions).\n",
  1680. (unsigned int)(p - (u32 *)handle_tlbl));
  1681. dump_handler("r3000_tlb_load", handle_tlbl, handle_tlbl_end);
  1682. }
  1683. static void build_r3000_tlb_store_handler(void)
  1684. {
  1685. u32 *p = (u32 *)handle_tlbs;
  1686. struct uasm_label *l = labels;
  1687. struct uasm_reloc *r = relocs;
  1688. memset(p, 0, handle_tlbs_end - (char *)p);
  1689. memset(labels, 0, sizeof(labels));
  1690. memset(relocs, 0, sizeof(relocs));
  1691. build_r3000_tlbchange_handler_head(&p, GPR_K0, GPR_K1);
  1692. build_pte_writable(&p, &r, GPR_K0, GPR_K1, -1, label_nopage_tlbs);
  1693. uasm_i_nop(&p); /* load delay */
  1694. build_make_write(&p, &r, GPR_K0, GPR_K1, -1);
  1695. build_r3000_tlb_reload_write(&p, &l, &r, GPR_K0, GPR_K1);
  1696. uasm_l_nopage_tlbs(&l, p);
  1697. uasm_i_j(&p, (unsigned long)tlb_do_page_fault_1 & 0x0fffffff);
  1698. uasm_i_nop(&p);
  1699. if (p >= (u32 *)handle_tlbs_end)
  1700. panic("TLB store handler fastpath space exceeded");
  1701. uasm_resolve_relocs(relocs, labels);
  1702. pr_debug("Wrote TLB store handler fastpath (%u instructions).\n",
  1703. (unsigned int)(p - (u32 *)handle_tlbs));
  1704. dump_handler("r3000_tlb_store", handle_tlbs, handle_tlbs_end);
  1705. }
  1706. static void build_r3000_tlb_modify_handler(void)
  1707. {
  1708. u32 *p = (u32 *)handle_tlbm;
  1709. struct uasm_label *l = labels;
  1710. struct uasm_reloc *r = relocs;
  1711. memset(p, 0, handle_tlbm_end - (char *)p);
  1712. memset(labels, 0, sizeof(labels));
  1713. memset(relocs, 0, sizeof(relocs));
  1714. build_r3000_tlbchange_handler_head(&p, GPR_K0, GPR_K1);
  1715. build_pte_modifiable(&p, &r, GPR_K0, GPR_K1, -1, label_nopage_tlbm);
  1716. uasm_i_nop(&p); /* load delay */
  1717. build_make_write(&p, &r, GPR_K0, GPR_K1, -1);
  1718. build_r3000_pte_reload_tlbwi(&p, GPR_K0, GPR_K1);
  1719. uasm_l_nopage_tlbm(&l, p);
  1720. uasm_i_j(&p, (unsigned long)tlb_do_page_fault_1 & 0x0fffffff);
  1721. uasm_i_nop(&p);
  1722. if (p >= (u32 *)handle_tlbm_end)
  1723. panic("TLB modify handler fastpath space exceeded");
  1724. uasm_resolve_relocs(relocs, labels);
  1725. pr_debug("Wrote TLB modify handler fastpath (%u instructions).\n",
  1726. (unsigned int)(p - (u32 *)handle_tlbm));
  1727. dump_handler("r3000_tlb_modify", handle_tlbm, handle_tlbm_end);
  1728. }
  1729. #endif /* CONFIG_MIPS_PGD_C0_CONTEXT */
  1730. static bool cpu_has_tlbex_tlbp_race(void)
  1731. {
  1732. /*
  1733. * When a Hardware Table Walker is running it can replace TLB entries
  1734. * at any time, leading to a race between it & the CPU.
  1735. */
  1736. if (cpu_has_htw)
  1737. return true;
  1738. /*
  1739. * If the CPU shares FTLB RAM with its siblings then our entry may be
  1740. * replaced at any time by a sibling performing a write to the FTLB.
  1741. */
  1742. if (cpu_has_shared_ftlb_ram)
  1743. return true;
  1744. /* In all other cases there ought to be no race condition to handle */
  1745. return false;
  1746. }
  1747. /*
  1748. * R4000 style TLB load/store/modify handlers.
  1749. */
  1750. static struct work_registers
  1751. build_r4000_tlbchange_handler_head(u32 **p, struct uasm_label **l,
  1752. struct uasm_reloc **r)
  1753. {
  1754. struct work_registers wr = build_get_work_registers(p);
  1755. #ifdef CONFIG_64BIT
  1756. build_get_pmde64(p, l, r, wr.r1, wr.r2); /* get pmd in ptr */
  1757. #else
  1758. build_get_pgde32(p, wr.r1, wr.r2); /* get pgd in ptr */
  1759. #endif
  1760. #ifdef CONFIG_MIPS_HUGE_TLB_SUPPORT
  1761. /*
  1762. * For huge tlb entries, pmd doesn't contain an address but
  1763. * instead contains the tlb pte. Check the PAGE_HUGE bit and
  1764. * see if we need to jump to huge tlb processing.
  1765. */
  1766. build_is_huge_pte(p, r, wr.r1, wr.r2, label_tlb_huge_update);
  1767. #endif
  1768. UASM_i_MFC0(p, wr.r1, C0_BADVADDR);
  1769. UASM_i_LW(p, wr.r2, 0, wr.r2);
  1770. UASM_i_SRL(p, wr.r1, wr.r1, PAGE_SHIFT - PTE_T_LOG2);
  1771. uasm_i_andi(p, wr.r1, wr.r1, (PTRS_PER_PTE - 1) << PTE_T_LOG2);
  1772. UASM_i_ADDU(p, wr.r2, wr.r2, wr.r1);
  1773. #ifdef CONFIG_SMP
  1774. uasm_l_smp_pgtable_change(l, *p);
  1775. #endif
  1776. iPTE_LW(p, wr.r1, wr.r2); /* get even pte */
  1777. if (!m4kc_tlbp_war()) {
  1778. build_tlb_probe_entry(p);
  1779. if (cpu_has_tlbex_tlbp_race()) {
  1780. /* race condition happens, leaving */
  1781. uasm_i_ehb(p);
  1782. uasm_i_mfc0(p, wr.r3, C0_INDEX);
  1783. uasm_il_bltz(p, r, wr.r3, label_leave);
  1784. uasm_i_nop(p);
  1785. }
  1786. }
  1787. return wr;
  1788. }
  1789. static void
  1790. build_r4000_tlbchange_handler_tail(u32 **p, struct uasm_label **l,
  1791. struct uasm_reloc **r, unsigned int tmp,
  1792. unsigned int ptr)
  1793. {
  1794. uasm_i_ori(p, ptr, ptr, sizeof(pte_t));
  1795. uasm_i_xori(p, ptr, ptr, sizeof(pte_t));
  1796. build_update_entries(p, tmp, ptr);
  1797. build_tlb_write_entry(p, l, r, tlb_indexed);
  1798. uasm_l_leave(l, *p);
  1799. build_restore_work_registers(p);
  1800. uasm_i_eret(p); /* return from trap */
  1801. #ifdef CONFIG_64BIT
  1802. build_get_pgd_vmalloc64(p, l, r, tmp, ptr, not_refill);
  1803. #endif
  1804. }
  1805. static void build_r4000_tlb_load_handler(void)
  1806. {
  1807. u32 *p = (u32 *)msk_isa16_mode((ulong)handle_tlbl);
  1808. struct uasm_label *l = labels;
  1809. struct uasm_reloc *r = relocs;
  1810. struct work_registers wr;
  1811. memset(p, 0, handle_tlbl_end - (char *)p);
  1812. memset(labels, 0, sizeof(labels));
  1813. memset(relocs, 0, sizeof(relocs));
  1814. if (bcm1250_m3_war()) {
  1815. unsigned int segbits = 44;
  1816. uasm_i_dmfc0(&p, GPR_K0, C0_BADVADDR);
  1817. uasm_i_dmfc0(&p, GPR_K1, C0_ENTRYHI);
  1818. uasm_i_xor(&p, GPR_K0, GPR_K0, GPR_K1);
  1819. uasm_i_dsrl_safe(&p, GPR_K1, GPR_K0, 62);
  1820. uasm_i_dsrl_safe(&p, GPR_K0, GPR_K0, 12 + 1);
  1821. uasm_i_dsll_safe(&p, GPR_K0, GPR_K0, 64 + 12 + 1 - segbits);
  1822. uasm_i_or(&p, GPR_K0, GPR_K0, GPR_K1);
  1823. uasm_il_bnez(&p, &r, GPR_K0, label_leave);
  1824. /* No need for uasm_i_nop */
  1825. }
  1826. wr = build_r4000_tlbchange_handler_head(&p, &l, &r);
  1827. build_pte_present(&p, &r, wr.r1, wr.r2, wr.r3, label_nopage_tlbl);
  1828. if (m4kc_tlbp_war())
  1829. build_tlb_probe_entry(&p);
  1830. if (cpu_has_rixi && !cpu_has_rixiex) {
  1831. /*
  1832. * If the page is not _PAGE_VALID, RI or XI could not
  1833. * have triggered it. Skip the expensive test..
  1834. */
  1835. if (use_bbit_insns()) {
  1836. uasm_il_bbit0(&p, &r, wr.r1, ilog2(_PAGE_VALID),
  1837. label_tlbl_goaround1);
  1838. } else {
  1839. uasm_i_andi(&p, wr.r3, wr.r1, _PAGE_VALID);
  1840. uasm_il_beqz(&p, &r, wr.r3, label_tlbl_goaround1);
  1841. }
  1842. uasm_i_nop(&p);
  1843. /*
  1844. * Warn if something may race with us & replace the TLB entry
  1845. * before we read it here. Everything with such races should
  1846. * also have dedicated RiXi exception handlers, so this
  1847. * shouldn't be hit.
  1848. */
  1849. WARN(cpu_has_tlbex_tlbp_race(), "Unhandled race in RiXi path");
  1850. uasm_i_tlbr(&p);
  1851. if (cpu_has_mips_r2_exec_hazard)
  1852. uasm_i_ehb(&p);
  1853. /* Examine entrylo 0 or 1 based on ptr. */
  1854. if (use_bbit_insns()) {
  1855. uasm_i_bbit0(&p, wr.r2, ilog2(sizeof(pte_t)), 8);
  1856. } else {
  1857. uasm_i_andi(&p, wr.r3, wr.r2, sizeof(pte_t));
  1858. uasm_i_beqz(&p, wr.r3, 8);
  1859. }
  1860. /* load it in the delay slot*/
  1861. UASM_i_MFC0(&p, wr.r3, C0_ENTRYLO0);
  1862. /* load it if ptr is odd */
  1863. UASM_i_MFC0(&p, wr.r3, C0_ENTRYLO1);
  1864. /*
  1865. * If the entryLo (now in wr.r3) is valid (bit 1), RI or
  1866. * XI must have triggered it.
  1867. */
  1868. if (use_bbit_insns()) {
  1869. uasm_il_bbit1(&p, &r, wr.r3, 1, label_nopage_tlbl);
  1870. uasm_i_nop(&p);
  1871. uasm_l_tlbl_goaround1(&l, p);
  1872. } else {
  1873. uasm_i_andi(&p, wr.r3, wr.r3, 2);
  1874. uasm_il_bnez(&p, &r, wr.r3, label_nopage_tlbl);
  1875. uasm_i_nop(&p);
  1876. }
  1877. uasm_l_tlbl_goaround1(&l, p);
  1878. }
  1879. build_make_valid(&p, &r, wr.r1, wr.r2, wr.r3);
  1880. build_r4000_tlbchange_handler_tail(&p, &l, &r, wr.r1, wr.r2);
  1881. #ifdef CONFIG_MIPS_HUGE_TLB_SUPPORT
  1882. /*
  1883. * This is the entry point when build_r4000_tlbchange_handler_head
  1884. * spots a huge page.
  1885. */
  1886. uasm_l_tlb_huge_update(&l, p);
  1887. iPTE_LW(&p, wr.r1, wr.r2);
  1888. build_pte_present(&p, &r, wr.r1, wr.r2, wr.r3, label_nopage_tlbl);
  1889. build_tlb_probe_entry(&p);
  1890. if (cpu_has_rixi && !cpu_has_rixiex) {
  1891. /*
  1892. * If the page is not _PAGE_VALID, RI or XI could not
  1893. * have triggered it. Skip the expensive test..
  1894. */
  1895. if (use_bbit_insns()) {
  1896. uasm_il_bbit0(&p, &r, wr.r1, ilog2(_PAGE_VALID),
  1897. label_tlbl_goaround2);
  1898. } else {
  1899. uasm_i_andi(&p, wr.r3, wr.r1, _PAGE_VALID);
  1900. uasm_il_beqz(&p, &r, wr.r3, label_tlbl_goaround2);
  1901. }
  1902. uasm_i_nop(&p);
  1903. /*
  1904. * Warn if something may race with us & replace the TLB entry
  1905. * before we read it here. Everything with such races should
  1906. * also have dedicated RiXi exception handlers, so this
  1907. * shouldn't be hit.
  1908. */
  1909. WARN(cpu_has_tlbex_tlbp_race(), "Unhandled race in RiXi path");
  1910. uasm_i_tlbr(&p);
  1911. if (cpu_has_mips_r2_exec_hazard)
  1912. uasm_i_ehb(&p);
  1913. /* Examine entrylo 0 or 1 based on ptr. */
  1914. if (use_bbit_insns()) {
  1915. uasm_i_bbit0(&p, wr.r2, ilog2(sizeof(pte_t)), 8);
  1916. } else {
  1917. uasm_i_andi(&p, wr.r3, wr.r2, sizeof(pte_t));
  1918. uasm_i_beqz(&p, wr.r3, 8);
  1919. }
  1920. /* load it in the delay slot*/
  1921. UASM_i_MFC0(&p, wr.r3, C0_ENTRYLO0);
  1922. /* load it if ptr is odd */
  1923. UASM_i_MFC0(&p, wr.r3, C0_ENTRYLO1);
  1924. /*
  1925. * If the entryLo (now in wr.r3) is valid (bit 1), RI or
  1926. * XI must have triggered it.
  1927. */
  1928. if (use_bbit_insns()) {
  1929. uasm_il_bbit0(&p, &r, wr.r3, 1, label_tlbl_goaround2);
  1930. } else {
  1931. uasm_i_andi(&p, wr.r3, wr.r3, 2);
  1932. uasm_il_beqz(&p, &r, wr.r3, label_tlbl_goaround2);
  1933. }
  1934. if (PM_DEFAULT_MASK == 0)
  1935. uasm_i_nop(&p);
  1936. /*
  1937. * We clobbered C0_PAGEMASK, restore it. On the other branch
  1938. * it is restored in build_huge_tlb_write_entry.
  1939. */
  1940. build_restore_pagemask(&p, &r, wr.r3, label_nopage_tlbl, 0);
  1941. uasm_l_tlbl_goaround2(&l, p);
  1942. }
  1943. uasm_i_ori(&p, wr.r1, wr.r1, (_PAGE_ACCESSED | _PAGE_VALID));
  1944. build_huge_handler_tail(&p, &r, &l, wr.r1, wr.r2, 1);
  1945. #endif
  1946. uasm_l_nopage_tlbl(&l, p);
  1947. if (IS_ENABLED(CONFIG_CPU_LOONGSON3_WORKAROUNDS))
  1948. uasm_i_sync(&p, 0);
  1949. build_restore_work_registers(&p);
  1950. #ifdef CONFIG_CPU_MICROMIPS
  1951. if ((unsigned long)tlb_do_page_fault_0 & 1) {
  1952. uasm_i_lui(&p, GPR_K0, uasm_rel_hi((long)tlb_do_page_fault_0));
  1953. uasm_i_addiu(&p, GPR_K0, GPR_K0, uasm_rel_lo((long)tlb_do_page_fault_0));
  1954. uasm_i_jr(&p, GPR_K0);
  1955. } else
  1956. #endif
  1957. uasm_i_j(&p, (unsigned long)tlb_do_page_fault_0 & 0x0fffffff);
  1958. uasm_i_nop(&p);
  1959. if (p >= (u32 *)handle_tlbl_end)
  1960. panic("TLB load handler fastpath space exceeded");
  1961. uasm_resolve_relocs(relocs, labels);
  1962. pr_debug("Wrote TLB load handler fastpath (%u instructions).\n",
  1963. (unsigned int)(p - (u32 *)handle_tlbl));
  1964. dump_handler("r4000_tlb_load", handle_tlbl, handle_tlbl_end);
  1965. }
  1966. static void build_r4000_tlb_store_handler(void)
  1967. {
  1968. u32 *p = (u32 *)msk_isa16_mode((ulong)handle_tlbs);
  1969. struct uasm_label *l = labels;
  1970. struct uasm_reloc *r = relocs;
  1971. struct work_registers wr;
  1972. memset(p, 0, handle_tlbs_end - (char *)p);
  1973. memset(labels, 0, sizeof(labels));
  1974. memset(relocs, 0, sizeof(relocs));
  1975. wr = build_r4000_tlbchange_handler_head(&p, &l, &r);
  1976. build_pte_writable(&p, &r, wr.r1, wr.r2, wr.r3, label_nopage_tlbs);
  1977. if (m4kc_tlbp_war())
  1978. build_tlb_probe_entry(&p);
  1979. build_make_write(&p, &r, wr.r1, wr.r2, wr.r3);
  1980. build_r4000_tlbchange_handler_tail(&p, &l, &r, wr.r1, wr.r2);
  1981. #ifdef CONFIG_MIPS_HUGE_TLB_SUPPORT
  1982. /*
  1983. * This is the entry point when
  1984. * build_r4000_tlbchange_handler_head spots a huge page.
  1985. */
  1986. uasm_l_tlb_huge_update(&l, p);
  1987. iPTE_LW(&p, wr.r1, wr.r2);
  1988. build_pte_writable(&p, &r, wr.r1, wr.r2, wr.r3, label_nopage_tlbs);
  1989. build_tlb_probe_entry(&p);
  1990. uasm_i_ori(&p, wr.r1, wr.r1,
  1991. _PAGE_ACCESSED | _PAGE_MODIFIED | _PAGE_VALID | _PAGE_DIRTY);
  1992. build_huge_handler_tail(&p, &r, &l, wr.r1, wr.r2, 1);
  1993. #endif
  1994. uasm_l_nopage_tlbs(&l, p);
  1995. if (IS_ENABLED(CONFIG_CPU_LOONGSON3_WORKAROUNDS))
  1996. uasm_i_sync(&p, 0);
  1997. build_restore_work_registers(&p);
  1998. #ifdef CONFIG_CPU_MICROMIPS
  1999. if ((unsigned long)tlb_do_page_fault_1 & 1) {
  2000. uasm_i_lui(&p, GPR_K0, uasm_rel_hi((long)tlb_do_page_fault_1));
  2001. uasm_i_addiu(&p, GPR_K0, GPR_K0, uasm_rel_lo((long)tlb_do_page_fault_1));
  2002. uasm_i_jr(&p, GPR_K0);
  2003. } else
  2004. #endif
  2005. uasm_i_j(&p, (unsigned long)tlb_do_page_fault_1 & 0x0fffffff);
  2006. uasm_i_nop(&p);
  2007. if (p >= (u32 *)handle_tlbs_end)
  2008. panic("TLB store handler fastpath space exceeded");
  2009. uasm_resolve_relocs(relocs, labels);
  2010. pr_debug("Wrote TLB store handler fastpath (%u instructions).\n",
  2011. (unsigned int)(p - (u32 *)handle_tlbs));
  2012. dump_handler("r4000_tlb_store", handle_tlbs, handle_tlbs_end);
  2013. }
  2014. static void build_r4000_tlb_modify_handler(void)
  2015. {
  2016. u32 *p = (u32 *)msk_isa16_mode((ulong)handle_tlbm);
  2017. struct uasm_label *l = labels;
  2018. struct uasm_reloc *r = relocs;
  2019. struct work_registers wr;
  2020. memset(p, 0, handle_tlbm_end - (char *)p);
  2021. memset(labels, 0, sizeof(labels));
  2022. memset(relocs, 0, sizeof(relocs));
  2023. wr = build_r4000_tlbchange_handler_head(&p, &l, &r);
  2024. build_pte_modifiable(&p, &r, wr.r1, wr.r2, wr.r3, label_nopage_tlbm);
  2025. if (m4kc_tlbp_war())
  2026. build_tlb_probe_entry(&p);
  2027. /* Present and writable bits set, set accessed and dirty bits. */
  2028. build_make_write(&p, &r, wr.r1, wr.r2, wr.r3);
  2029. build_r4000_tlbchange_handler_tail(&p, &l, &r, wr.r1, wr.r2);
  2030. #ifdef CONFIG_MIPS_HUGE_TLB_SUPPORT
  2031. /*
  2032. * This is the entry point when
  2033. * build_r4000_tlbchange_handler_head spots a huge page.
  2034. */
  2035. uasm_l_tlb_huge_update(&l, p);
  2036. iPTE_LW(&p, wr.r1, wr.r2);
  2037. build_pte_modifiable(&p, &r, wr.r1, wr.r2, wr.r3, label_nopage_tlbm);
  2038. build_tlb_probe_entry(&p);
  2039. uasm_i_ori(&p, wr.r1, wr.r1,
  2040. _PAGE_ACCESSED | _PAGE_MODIFIED | _PAGE_VALID | _PAGE_DIRTY);
  2041. build_huge_handler_tail(&p, &r, &l, wr.r1, wr.r2, 0);
  2042. #endif
  2043. uasm_l_nopage_tlbm(&l, p);
  2044. if (IS_ENABLED(CONFIG_CPU_LOONGSON3_WORKAROUNDS))
  2045. uasm_i_sync(&p, 0);
  2046. build_restore_work_registers(&p);
  2047. #ifdef CONFIG_CPU_MICROMIPS
  2048. if ((unsigned long)tlb_do_page_fault_1 & 1) {
  2049. uasm_i_lui(&p, GPR_K0, uasm_rel_hi((long)tlb_do_page_fault_1));
  2050. uasm_i_addiu(&p, GPR_K0, GPR_K0, uasm_rel_lo((long)tlb_do_page_fault_1));
  2051. uasm_i_jr(&p, GPR_K0);
  2052. } else
  2053. #endif
  2054. uasm_i_j(&p, (unsigned long)tlb_do_page_fault_1 & 0x0fffffff);
  2055. uasm_i_nop(&p);
  2056. if (p >= (u32 *)handle_tlbm_end)
  2057. panic("TLB modify handler fastpath space exceeded");
  2058. uasm_resolve_relocs(relocs, labels);
  2059. pr_debug("Wrote TLB modify handler fastpath (%u instructions).\n",
  2060. (unsigned int)(p - (u32 *)handle_tlbm));
  2061. dump_handler("r4000_tlb_modify", handle_tlbm, handle_tlbm_end);
  2062. }
  2063. static void flush_tlb_handlers(void)
  2064. {
  2065. local_flush_icache_range((unsigned long)handle_tlbl,
  2066. (unsigned long)handle_tlbl_end);
  2067. local_flush_icache_range((unsigned long)handle_tlbs,
  2068. (unsigned long)handle_tlbs_end);
  2069. local_flush_icache_range((unsigned long)handle_tlbm,
  2070. (unsigned long)handle_tlbm_end);
  2071. local_flush_icache_range((unsigned long)tlbmiss_handler_setup_pgd,
  2072. (unsigned long)tlbmiss_handler_setup_pgd_end);
  2073. }
  2074. static void print_htw_config(void)
  2075. {
  2076. unsigned long config;
  2077. unsigned int pwctl;
  2078. const int field = 2 * sizeof(unsigned long);
  2079. config = read_c0_pwfield();
  2080. pr_debug("PWField (0x%0*lx): GDI: 0x%02lx UDI: 0x%02lx MDI: 0x%02lx PTI: 0x%02lx PTEI: 0x%02lx\n",
  2081. field, config,
  2082. (config & MIPS_PWFIELD_GDI_MASK) >> MIPS_PWFIELD_GDI_SHIFT,
  2083. (config & MIPS_PWFIELD_UDI_MASK) >> MIPS_PWFIELD_UDI_SHIFT,
  2084. (config & MIPS_PWFIELD_MDI_MASK) >> MIPS_PWFIELD_MDI_SHIFT,
  2085. (config & MIPS_PWFIELD_PTI_MASK) >> MIPS_PWFIELD_PTI_SHIFT,
  2086. (config & MIPS_PWFIELD_PTEI_MASK) >> MIPS_PWFIELD_PTEI_SHIFT);
  2087. config = read_c0_pwsize();
  2088. pr_debug("PWSize (0x%0*lx): PS: 0x%lx GDW: 0x%02lx UDW: 0x%02lx MDW: 0x%02lx PTW: 0x%02lx PTEW: 0x%02lx\n",
  2089. field, config,
  2090. (config & MIPS_PWSIZE_PS_MASK) >> MIPS_PWSIZE_PS_SHIFT,
  2091. (config & MIPS_PWSIZE_GDW_MASK) >> MIPS_PWSIZE_GDW_SHIFT,
  2092. (config & MIPS_PWSIZE_UDW_MASK) >> MIPS_PWSIZE_UDW_SHIFT,
  2093. (config & MIPS_PWSIZE_MDW_MASK) >> MIPS_PWSIZE_MDW_SHIFT,
  2094. (config & MIPS_PWSIZE_PTW_MASK) >> MIPS_PWSIZE_PTW_SHIFT,
  2095. (config & MIPS_PWSIZE_PTEW_MASK) >> MIPS_PWSIZE_PTEW_SHIFT);
  2096. pwctl = read_c0_pwctl();
  2097. pr_debug("PWCtl (0x%x): PWEn: 0x%x XK: 0x%x XS: 0x%x XU: 0x%x DPH: 0x%x HugePg: 0x%x Psn: 0x%x\n",
  2098. pwctl,
  2099. (pwctl & MIPS_PWCTL_PWEN_MASK) >> MIPS_PWCTL_PWEN_SHIFT,
  2100. (pwctl & MIPS_PWCTL_XK_MASK) >> MIPS_PWCTL_XK_SHIFT,
  2101. (pwctl & MIPS_PWCTL_XS_MASK) >> MIPS_PWCTL_XS_SHIFT,
  2102. (pwctl & MIPS_PWCTL_XU_MASK) >> MIPS_PWCTL_XU_SHIFT,
  2103. (pwctl & MIPS_PWCTL_DPH_MASK) >> MIPS_PWCTL_DPH_SHIFT,
  2104. (pwctl & MIPS_PWCTL_HUGEPG_MASK) >> MIPS_PWCTL_HUGEPG_SHIFT,
  2105. (pwctl & MIPS_PWCTL_PSN_MASK) >> MIPS_PWCTL_PSN_SHIFT);
  2106. }
  2107. static void config_htw_params(void)
  2108. {
  2109. unsigned long pwfield, pwsize, ptei;
  2110. unsigned int config;
  2111. /*
  2112. * We are using 2-level page tables, so we only need to
  2113. * setup GDW and PTW appropriately. UDW and MDW will remain 0.
  2114. * The default value of GDI/UDI/MDI/PTI is 0xc. It is illegal to
  2115. * write values less than 0xc in these fields because the entire
  2116. * write will be dropped. As a result of which, we must preserve
  2117. * the original reset values and overwrite only what we really want.
  2118. */
  2119. pwfield = read_c0_pwfield();
  2120. /* re-initialize the GDI field */
  2121. pwfield &= ~MIPS_PWFIELD_GDI_MASK;
  2122. pwfield |= PGDIR_SHIFT << MIPS_PWFIELD_GDI_SHIFT;
  2123. /* re-initialize the PTI field including the even/odd bit */
  2124. pwfield &= ~MIPS_PWFIELD_PTI_MASK;
  2125. pwfield |= PAGE_SHIFT << MIPS_PWFIELD_PTI_SHIFT;
  2126. if (CONFIG_PGTABLE_LEVELS >= 3) {
  2127. pwfield &= ~MIPS_PWFIELD_MDI_MASK;
  2128. pwfield |= PMD_SHIFT << MIPS_PWFIELD_MDI_SHIFT;
  2129. }
  2130. /* Set the PTEI right shift */
  2131. ptei = _PAGE_GLOBAL_SHIFT << MIPS_PWFIELD_PTEI_SHIFT;
  2132. pwfield |= ptei;
  2133. write_c0_pwfield(pwfield);
  2134. /* Check whether the PTEI value is supported */
  2135. back_to_back_c0_hazard();
  2136. pwfield = read_c0_pwfield();
  2137. if (((pwfield & MIPS_PWFIELD_PTEI_MASK) << MIPS_PWFIELD_PTEI_SHIFT)
  2138. != ptei) {
  2139. pr_warn("Unsupported PTEI field value: 0x%lx. HTW will not be enabled",
  2140. ptei);
  2141. /*
  2142. * Drop option to avoid HTW being enabled via another path
  2143. * (eg htw_reset())
  2144. */
  2145. current_cpu_data.options &= ~MIPS_CPU_HTW;
  2146. return;
  2147. }
  2148. pwsize = ilog2(PTRS_PER_PGD) << MIPS_PWSIZE_GDW_SHIFT;
  2149. pwsize |= ilog2(PTRS_PER_PTE) << MIPS_PWSIZE_PTW_SHIFT;
  2150. if (CONFIG_PGTABLE_LEVELS >= 3)
  2151. pwsize |= ilog2(PTRS_PER_PMD) << MIPS_PWSIZE_MDW_SHIFT;
  2152. /* Set pointer size to size of directory pointers */
  2153. if (IS_ENABLED(CONFIG_64BIT))
  2154. pwsize |= MIPS_PWSIZE_PS_MASK;
  2155. /* PTEs may be multiple pointers long (e.g. with XPA) */
  2156. pwsize |= ((PTE_T_LOG2 - PGD_T_LOG2) << MIPS_PWSIZE_PTEW_SHIFT)
  2157. & MIPS_PWSIZE_PTEW_MASK;
  2158. write_c0_pwsize(pwsize);
  2159. /* Make sure everything is set before we enable the HTW */
  2160. back_to_back_c0_hazard();
  2161. /*
  2162. * Enable HTW (and only for XUSeg on 64-bit), and disable the rest of
  2163. * the pwctl fields.
  2164. */
  2165. config = 1 << MIPS_PWCTL_PWEN_SHIFT;
  2166. if (IS_ENABLED(CONFIG_64BIT))
  2167. config |= MIPS_PWCTL_XU_MASK;
  2168. write_c0_pwctl(config);
  2169. pr_info("Hardware Page Table Walker enabled\n");
  2170. print_htw_config();
  2171. }
  2172. static void config_xpa_params(void)
  2173. {
  2174. #ifdef CONFIG_XPA
  2175. unsigned int pagegrain;
  2176. if (mips_xpa_disabled) {
  2177. pr_info("Extended Physical Addressing (XPA) disabled\n");
  2178. return;
  2179. }
  2180. pagegrain = read_c0_pagegrain();
  2181. write_c0_pagegrain(pagegrain | PG_ELPA);
  2182. back_to_back_c0_hazard();
  2183. pagegrain = read_c0_pagegrain();
  2184. if (pagegrain & PG_ELPA)
  2185. pr_info("Extended Physical Addressing (XPA) enabled\n");
  2186. else
  2187. panic("Extended Physical Addressing (XPA) disabled");
  2188. #endif
  2189. }
  2190. static void check_pabits(void)
  2191. {
  2192. unsigned long entry;
  2193. unsigned pabits, fillbits;
  2194. if (!cpu_has_rixi || _PAGE_NO_EXEC == 0) {
  2195. /*
  2196. * We'll only be making use of the fact that we can rotate bits
  2197. * into the fill if the CPU supports RIXI, so don't bother
  2198. * probing this for CPUs which don't.
  2199. */
  2200. return;
  2201. }
  2202. write_c0_entrylo0(~0ul);
  2203. back_to_back_c0_hazard();
  2204. entry = read_c0_entrylo0();
  2205. /* clear all non-PFN bits */
  2206. entry &= ~((1 << MIPS_ENTRYLO_PFN_SHIFT) - 1);
  2207. entry &= ~(MIPS_ENTRYLO_RI | MIPS_ENTRYLO_XI);
  2208. /* find a lower bound on PABITS, and upper bound on fill bits */
  2209. pabits = fls_long(entry) + 6;
  2210. fillbits = max_t(int, (int)BITS_PER_LONG - pabits, 0);
  2211. /* minus the RI & XI bits */
  2212. fillbits -= min_t(unsigned, fillbits, 2);
  2213. if (fillbits >= ilog2(_PAGE_NO_EXEC))
  2214. fill_includes_sw_bits = true;
  2215. pr_debug("Entry* registers contain %u fill bits\n", fillbits);
  2216. }
  2217. void build_tlb_refill_handler(void)
  2218. {
  2219. /*
  2220. * The refill handler is generated per-CPU, multi-node systems
  2221. * may have local storage for it. The other handlers are only
  2222. * needed once.
  2223. */
  2224. static int run_once = 0;
  2225. if (IS_ENABLED(CONFIG_XPA) && !cpu_has_rixi)
  2226. panic("Kernels supporting XPA currently require CPUs with RIXI");
  2227. output_pgtable_bits_defines();
  2228. check_pabits();
  2229. #ifdef CONFIG_64BIT
  2230. check_for_high_segbits = current_cpu_data.vmbits > (PGDIR_SHIFT + PGD_TABLE_ORDER + PAGE_SHIFT - 3);
  2231. #endif
  2232. if (cpu_has_3kex) {
  2233. #ifndef CONFIG_MIPS_PGD_C0_CONTEXT
  2234. if (!run_once) {
  2235. build_setup_pgd();
  2236. build_r3000_tlb_refill_handler();
  2237. build_r3000_tlb_load_handler();
  2238. build_r3000_tlb_store_handler();
  2239. build_r3000_tlb_modify_handler();
  2240. flush_tlb_handlers();
  2241. run_once++;
  2242. }
  2243. #else
  2244. panic("No R3000 TLB refill handler");
  2245. #endif
  2246. return;
  2247. }
  2248. if (cpu_has_ldpte)
  2249. setup_pw();
  2250. if (!run_once) {
  2251. scratch_reg = allocate_kscratch();
  2252. build_setup_pgd();
  2253. build_r4000_tlb_load_handler();
  2254. build_r4000_tlb_store_handler();
  2255. build_r4000_tlb_modify_handler();
  2256. if (cpu_has_ldpte)
  2257. build_loongson3_tlb_refill_handler();
  2258. else
  2259. build_r4000_tlb_refill_handler();
  2260. flush_tlb_handlers();
  2261. run_once++;
  2262. }
  2263. if (cpu_has_xpa)
  2264. config_xpa_params();
  2265. if (cpu_has_htw)
  2266. config_htw_params();
  2267. }