tlbex.c 69 KB

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