tlbex.c 70 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635
  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. unsigned int flush)
  674. {
  675. #ifdef CONFIG_SMP
  676. UASM_i_SC(p, pte, 0, ptr);
  677. uasm_il_beqz(p, r, pte, label_tlb_huge_update);
  678. UASM_i_LW(p, pte, 0, ptr); /* Needed because SC killed our PTE */
  679. #else
  680. UASM_i_SW(p, pte, 0, ptr);
  681. #endif
  682. if (cpu_has_ftlb && flush) {
  683. BUG_ON(!cpu_has_tlbinv);
  684. UASM_i_MFC0(p, ptr, C0_ENTRYHI);
  685. uasm_i_ori(p, ptr, ptr, MIPS_ENTRYHI_EHINV);
  686. UASM_i_MTC0(p, ptr, C0_ENTRYHI);
  687. build_tlb_write_entry(p, l, r, tlb_indexed);
  688. uasm_i_xori(p, ptr, ptr, MIPS_ENTRYHI_EHINV);
  689. UASM_i_MTC0(p, ptr, C0_ENTRYHI);
  690. build_huge_update_entries(p, pte, ptr);
  691. build_huge_tlb_write_entry(p, l, r, pte, tlb_random, 0);
  692. return;
  693. }
  694. build_huge_update_entries(p, pte, ptr);
  695. build_huge_tlb_write_entry(p, l, r, pte, tlb_indexed, 0);
  696. }
  697. #endif /* CONFIG_MIPS_HUGE_TLB_SUPPORT */
  698. #ifdef CONFIG_64BIT
  699. /*
  700. * TMP and PTR are scratch.
  701. * TMP will be clobbered, PTR will hold the pmd entry.
  702. */
  703. void build_get_pmde64(u32 **p, struct uasm_label **l, struct uasm_reloc **r,
  704. unsigned int tmp, unsigned int ptr)
  705. {
  706. #ifndef CONFIG_MIPS_PGD_C0_CONTEXT
  707. long pgdc = (long)pgd_current;
  708. #endif
  709. /*
  710. * The vmalloc handling is not in the hotpath.
  711. */
  712. uasm_i_dmfc0(p, tmp, C0_BADVADDR);
  713. if (check_for_high_segbits) {
  714. /*
  715. * The kernel currently implicitely assumes that the
  716. * MIPS SEGBITS parameter for the processor is
  717. * (PGDIR_SHIFT+PGDIR_BITS) or less, and will never
  718. * allocate virtual addresses outside the maximum
  719. * range for SEGBITS = (PGDIR_SHIFT+PGDIR_BITS). But
  720. * that doesn't prevent user code from accessing the
  721. * higher xuseg addresses. Here, we make sure that
  722. * everything but the lower xuseg addresses goes down
  723. * the module_alloc/vmalloc path.
  724. */
  725. uasm_i_dsrl_safe(p, ptr, tmp, PGDIR_SHIFT + PGD_ORDER + PAGE_SHIFT - 3);
  726. uasm_il_bnez(p, r, ptr, label_vmalloc);
  727. } else {
  728. uasm_il_bltz(p, r, tmp, label_vmalloc);
  729. }
  730. /* No uasm_i_nop needed here, since the next insn doesn't touch TMP. */
  731. if (pgd_reg != -1) {
  732. /* pgd is in pgd_reg */
  733. if (cpu_has_ldpte)
  734. UASM_i_MFC0(p, ptr, C0_PWBASE);
  735. else
  736. UASM_i_MFC0(p, ptr, c0_kscratch(), pgd_reg);
  737. } else {
  738. #if defined(CONFIG_MIPS_PGD_C0_CONTEXT)
  739. /*
  740. * &pgd << 11 stored in CONTEXT [23..63].
  741. */
  742. UASM_i_MFC0(p, ptr, C0_CONTEXT);
  743. /* Clear lower 23 bits of context. */
  744. uasm_i_dins(p, ptr, 0, 0, 23);
  745. /* 1 0 1 0 1 << 6 xkphys cached */
  746. uasm_i_ori(p, ptr, ptr, 0x540);
  747. uasm_i_drotr(p, ptr, ptr, 11);
  748. #elif defined(CONFIG_SMP)
  749. UASM_i_CPUID_MFC0(p, ptr, SMP_CPUID_REG);
  750. uasm_i_dsrl_safe(p, ptr, ptr, SMP_CPUID_PTRSHIFT);
  751. UASM_i_LA_mostly(p, tmp, pgdc);
  752. uasm_i_daddu(p, ptr, ptr, tmp);
  753. uasm_i_dmfc0(p, tmp, C0_BADVADDR);
  754. uasm_i_ld(p, ptr, uasm_rel_lo(pgdc), ptr);
  755. #else
  756. UASM_i_LA_mostly(p, ptr, pgdc);
  757. uasm_i_ld(p, ptr, uasm_rel_lo(pgdc), ptr);
  758. #endif
  759. }
  760. uasm_l_vmalloc_done(l, *p);
  761. /* get pgd offset in bytes */
  762. uasm_i_dsrl_safe(p, tmp, tmp, PGDIR_SHIFT - 3);
  763. uasm_i_andi(p, tmp, tmp, (PTRS_PER_PGD - 1)<<3);
  764. uasm_i_daddu(p, ptr, ptr, tmp); /* add in pgd offset */
  765. #ifndef __PAGETABLE_PUD_FOLDED
  766. uasm_i_dmfc0(p, tmp, C0_BADVADDR); /* get faulting address */
  767. uasm_i_ld(p, ptr, 0, ptr); /* get pud pointer */
  768. uasm_i_dsrl_safe(p, tmp, tmp, PUD_SHIFT - 3); /* get pud offset in bytes */
  769. uasm_i_andi(p, tmp, tmp, (PTRS_PER_PUD - 1) << 3);
  770. uasm_i_daddu(p, ptr, ptr, tmp); /* add in pud offset */
  771. #endif
  772. #ifndef __PAGETABLE_PMD_FOLDED
  773. uasm_i_dmfc0(p, tmp, C0_BADVADDR); /* get faulting address */
  774. uasm_i_ld(p, ptr, 0, ptr); /* get pmd pointer */
  775. uasm_i_dsrl_safe(p, tmp, tmp, PMD_SHIFT-3); /* get pmd offset in bytes */
  776. uasm_i_andi(p, tmp, tmp, (PTRS_PER_PMD - 1)<<3);
  777. uasm_i_daddu(p, ptr, ptr, tmp); /* add in pmd offset */
  778. #endif
  779. }
  780. EXPORT_SYMBOL_GPL(build_get_pmde64);
  781. /*
  782. * BVADDR is the faulting address, PTR is scratch.
  783. * PTR will hold the pgd for vmalloc.
  784. */
  785. static void
  786. build_get_pgd_vmalloc64(u32 **p, struct uasm_label **l, struct uasm_reloc **r,
  787. unsigned int bvaddr, unsigned int ptr,
  788. enum vmalloc64_mode mode)
  789. {
  790. long swpd = (long)swapper_pg_dir;
  791. int single_insn_swpd;
  792. int did_vmalloc_branch = 0;
  793. single_insn_swpd = uasm_in_compat_space_p(swpd) && !uasm_rel_lo(swpd);
  794. uasm_l_vmalloc(l, *p);
  795. if (mode != not_refill && check_for_high_segbits) {
  796. if (single_insn_swpd) {
  797. uasm_il_bltz(p, r, bvaddr, label_vmalloc_done);
  798. uasm_i_lui(p, ptr, uasm_rel_hi(swpd));
  799. did_vmalloc_branch = 1;
  800. /* fall through */
  801. } else {
  802. uasm_il_bgez(p, r, bvaddr, label_large_segbits_fault);
  803. }
  804. }
  805. if (!did_vmalloc_branch) {
  806. if (single_insn_swpd) {
  807. uasm_il_b(p, r, label_vmalloc_done);
  808. uasm_i_lui(p, ptr, uasm_rel_hi(swpd));
  809. } else {
  810. UASM_i_LA_mostly(p, ptr, swpd);
  811. uasm_il_b(p, r, label_vmalloc_done);
  812. if (uasm_in_compat_space_p(swpd))
  813. uasm_i_addiu(p, ptr, ptr, uasm_rel_lo(swpd));
  814. else
  815. uasm_i_daddiu(p, ptr, ptr, uasm_rel_lo(swpd));
  816. }
  817. }
  818. if (mode != not_refill && check_for_high_segbits) {
  819. uasm_l_large_segbits_fault(l, *p);
  820. /*
  821. * We get here if we are an xsseg address, or if we are
  822. * an xuseg address above (PGDIR_SHIFT+PGDIR_BITS) boundary.
  823. *
  824. * Ignoring xsseg (assume disabled so would generate
  825. * (address errors?), the only remaining possibility
  826. * is the upper xuseg addresses. On processors with
  827. * TLB_SEGBITS <= PGDIR_SHIFT+PGDIR_BITS, these
  828. * addresses would have taken an address error. We try
  829. * to mimic that here by taking a load/istream page
  830. * fault.
  831. */
  832. UASM_i_LA(p, ptr, (unsigned long)tlb_do_page_fault_0);
  833. uasm_i_jr(p, ptr);
  834. if (mode == refill_scratch) {
  835. if (scratch_reg >= 0)
  836. UASM_i_MFC0(p, 1, c0_kscratch(), scratch_reg);
  837. else
  838. UASM_i_LW(p, 1, scratchpad_offset(0), 0);
  839. } else {
  840. uasm_i_nop(p);
  841. }
  842. }
  843. }
  844. #else /* !CONFIG_64BIT */
  845. /*
  846. * TMP and PTR are scratch.
  847. * TMP will be clobbered, PTR will hold the pgd entry.
  848. */
  849. void build_get_pgde32(u32 **p, unsigned int tmp, unsigned int ptr)
  850. {
  851. if (pgd_reg != -1) {
  852. /* pgd is in pgd_reg */
  853. uasm_i_mfc0(p, ptr, c0_kscratch(), pgd_reg);
  854. uasm_i_mfc0(p, tmp, C0_BADVADDR); /* get faulting address */
  855. } else {
  856. long pgdc = (long)pgd_current;
  857. /* 32 bit SMP has smp_processor_id() stored in CONTEXT. */
  858. #ifdef CONFIG_SMP
  859. uasm_i_mfc0(p, ptr, SMP_CPUID_REG);
  860. UASM_i_LA_mostly(p, tmp, pgdc);
  861. uasm_i_srl(p, ptr, ptr, SMP_CPUID_PTRSHIFT);
  862. uasm_i_addu(p, ptr, tmp, ptr);
  863. #else
  864. UASM_i_LA_mostly(p, ptr, pgdc);
  865. #endif
  866. uasm_i_mfc0(p, tmp, C0_BADVADDR); /* get faulting address */
  867. uasm_i_lw(p, ptr, uasm_rel_lo(pgdc), ptr);
  868. }
  869. uasm_i_srl(p, tmp, tmp, PGDIR_SHIFT); /* get pgd only bits */
  870. uasm_i_sll(p, tmp, tmp, PGD_T_LOG2);
  871. uasm_i_addu(p, ptr, ptr, tmp); /* add in pgd offset */
  872. }
  873. EXPORT_SYMBOL_GPL(build_get_pgde32);
  874. #endif /* !CONFIG_64BIT */
  875. static void build_adjust_context(u32 **p, unsigned int ctx)
  876. {
  877. unsigned int shift = 4 - (PTE_T_LOG2 + 1) + PAGE_SHIFT - 12;
  878. unsigned int mask = (PTRS_PER_PTE / 2 - 1) << (PTE_T_LOG2 + 1);
  879. switch (current_cpu_type()) {
  880. case CPU_VR41XX:
  881. case CPU_VR4111:
  882. case CPU_VR4121:
  883. case CPU_VR4122:
  884. case CPU_VR4131:
  885. case CPU_VR4181:
  886. case CPU_VR4181A:
  887. case CPU_VR4133:
  888. shift += 2;
  889. break;
  890. default:
  891. break;
  892. }
  893. if (shift)
  894. UASM_i_SRL(p, ctx, ctx, shift);
  895. uasm_i_andi(p, ctx, ctx, mask);
  896. }
  897. void build_get_ptep(u32 **p, unsigned int tmp, unsigned int ptr)
  898. {
  899. /*
  900. * Bug workaround for the Nevada. It seems as if under certain
  901. * circumstances the move from cp0_context might produce a
  902. * bogus result when the mfc0 instruction and its consumer are
  903. * in a different cacheline or a load instruction, probably any
  904. * memory reference, is between them.
  905. */
  906. switch (current_cpu_type()) {
  907. case CPU_NEVADA:
  908. UASM_i_LW(p, ptr, 0, ptr);
  909. GET_CONTEXT(p, tmp); /* get context reg */
  910. break;
  911. default:
  912. GET_CONTEXT(p, tmp); /* get context reg */
  913. UASM_i_LW(p, ptr, 0, ptr);
  914. break;
  915. }
  916. build_adjust_context(p, tmp);
  917. UASM_i_ADDU(p, ptr, ptr, tmp); /* add in offset */
  918. }
  919. EXPORT_SYMBOL_GPL(build_get_ptep);
  920. void build_update_entries(u32 **p, unsigned int tmp, unsigned int ptep)
  921. {
  922. int pte_off_even = 0;
  923. int pte_off_odd = sizeof(pte_t);
  924. #if defined(CONFIG_CPU_MIPS32) && defined(CONFIG_PHYS_ADDR_T_64BIT)
  925. /* The low 32 bits of EntryLo is stored in pte_high */
  926. pte_off_even += offsetof(pte_t, pte_high);
  927. pte_off_odd += offsetof(pte_t, pte_high);
  928. #endif
  929. if (IS_ENABLED(CONFIG_XPA)) {
  930. uasm_i_lw(p, tmp, pte_off_even, ptep); /* even pte */
  931. UASM_i_ROTR(p, tmp, tmp, ilog2(_PAGE_GLOBAL));
  932. UASM_i_MTC0(p, tmp, C0_ENTRYLO0);
  933. if (cpu_has_xpa && !mips_xpa_disabled) {
  934. uasm_i_lw(p, tmp, 0, ptep);
  935. uasm_i_ext(p, tmp, tmp, 0, 24);
  936. uasm_i_mthc0(p, tmp, C0_ENTRYLO0);
  937. }
  938. uasm_i_lw(p, tmp, pte_off_odd, ptep); /* odd pte */
  939. UASM_i_ROTR(p, tmp, tmp, ilog2(_PAGE_GLOBAL));
  940. UASM_i_MTC0(p, tmp, C0_ENTRYLO1);
  941. if (cpu_has_xpa && !mips_xpa_disabled) {
  942. uasm_i_lw(p, tmp, sizeof(pte_t), ptep);
  943. uasm_i_ext(p, tmp, tmp, 0, 24);
  944. uasm_i_mthc0(p, tmp, C0_ENTRYLO1);
  945. }
  946. return;
  947. }
  948. UASM_i_LW(p, tmp, pte_off_even, ptep); /* get even pte */
  949. UASM_i_LW(p, ptep, pte_off_odd, ptep); /* get odd pte */
  950. if (r45k_bvahwbug())
  951. build_tlb_probe_entry(p);
  952. build_convert_pte_to_entrylo(p, tmp);
  953. if (r4k_250MHZhwbug())
  954. UASM_i_MTC0(p, 0, C0_ENTRYLO0);
  955. UASM_i_MTC0(p, tmp, C0_ENTRYLO0); /* load it */
  956. build_convert_pte_to_entrylo(p, ptep);
  957. if (r45k_bvahwbug())
  958. uasm_i_mfc0(p, tmp, C0_INDEX);
  959. if (r4k_250MHZhwbug())
  960. UASM_i_MTC0(p, 0, C0_ENTRYLO1);
  961. UASM_i_MTC0(p, ptep, C0_ENTRYLO1); /* load it */
  962. }
  963. EXPORT_SYMBOL_GPL(build_update_entries);
  964. struct mips_huge_tlb_info {
  965. int huge_pte;
  966. int restore_scratch;
  967. bool need_reload_pte;
  968. };
  969. static struct mips_huge_tlb_info
  970. build_fast_tlb_refill_handler (u32 **p, struct uasm_label **l,
  971. struct uasm_reloc **r, unsigned int tmp,
  972. unsigned int ptr, int c0_scratch_reg)
  973. {
  974. struct mips_huge_tlb_info rv;
  975. unsigned int even, odd;
  976. int vmalloc_branch_delay_filled = 0;
  977. const int scratch = 1; /* Our extra working register */
  978. rv.huge_pte = scratch;
  979. rv.restore_scratch = 0;
  980. rv.need_reload_pte = false;
  981. if (check_for_high_segbits) {
  982. UASM_i_MFC0(p, tmp, C0_BADVADDR);
  983. if (pgd_reg != -1)
  984. UASM_i_MFC0(p, ptr, c0_kscratch(), pgd_reg);
  985. else
  986. UASM_i_MFC0(p, ptr, C0_CONTEXT);
  987. if (c0_scratch_reg >= 0)
  988. UASM_i_MTC0(p, scratch, c0_kscratch(), c0_scratch_reg);
  989. else
  990. UASM_i_SW(p, scratch, scratchpad_offset(0), 0);
  991. uasm_i_dsrl_safe(p, scratch, tmp,
  992. PGDIR_SHIFT + PGD_ORDER + PAGE_SHIFT - 3);
  993. uasm_il_bnez(p, r, scratch, label_vmalloc);
  994. if (pgd_reg == -1) {
  995. vmalloc_branch_delay_filled = 1;
  996. /* Clear lower 23 bits of context. */
  997. uasm_i_dins(p, ptr, 0, 0, 23);
  998. }
  999. } else {
  1000. if (pgd_reg != -1)
  1001. UASM_i_MFC0(p, ptr, c0_kscratch(), pgd_reg);
  1002. else
  1003. UASM_i_MFC0(p, ptr, C0_CONTEXT);
  1004. UASM_i_MFC0(p, tmp, C0_BADVADDR);
  1005. if (c0_scratch_reg >= 0)
  1006. UASM_i_MTC0(p, scratch, c0_kscratch(), c0_scratch_reg);
  1007. else
  1008. UASM_i_SW(p, scratch, scratchpad_offset(0), 0);
  1009. if (pgd_reg == -1)
  1010. /* Clear lower 23 bits of context. */
  1011. uasm_i_dins(p, ptr, 0, 0, 23);
  1012. uasm_il_bltz(p, r, tmp, label_vmalloc);
  1013. }
  1014. if (pgd_reg == -1) {
  1015. vmalloc_branch_delay_filled = 1;
  1016. /* 1 0 1 0 1 << 6 xkphys cached */
  1017. uasm_i_ori(p, ptr, ptr, 0x540);
  1018. uasm_i_drotr(p, ptr, ptr, 11);
  1019. }
  1020. #ifdef __PAGETABLE_PMD_FOLDED
  1021. #define LOC_PTEP scratch
  1022. #else
  1023. #define LOC_PTEP ptr
  1024. #endif
  1025. if (!vmalloc_branch_delay_filled)
  1026. /* get pgd offset in bytes */
  1027. uasm_i_dsrl_safe(p, scratch, tmp, PGDIR_SHIFT - 3);
  1028. uasm_l_vmalloc_done(l, *p);
  1029. /*
  1030. * tmp ptr
  1031. * fall-through case = badvaddr *pgd_current
  1032. * vmalloc case = badvaddr swapper_pg_dir
  1033. */
  1034. if (vmalloc_branch_delay_filled)
  1035. /* get pgd offset in bytes */
  1036. uasm_i_dsrl_safe(p, scratch, tmp, PGDIR_SHIFT - 3);
  1037. #ifdef __PAGETABLE_PMD_FOLDED
  1038. GET_CONTEXT(p, tmp); /* get context reg */
  1039. #endif
  1040. uasm_i_andi(p, scratch, scratch, (PTRS_PER_PGD - 1) << 3);
  1041. if (use_lwx_insns()) {
  1042. UASM_i_LWX(p, LOC_PTEP, scratch, ptr);
  1043. } else {
  1044. uasm_i_daddu(p, ptr, ptr, scratch); /* add in pgd offset */
  1045. uasm_i_ld(p, LOC_PTEP, 0, ptr); /* get pmd pointer */
  1046. }
  1047. #ifndef __PAGETABLE_PUD_FOLDED
  1048. /* get pud offset in bytes */
  1049. uasm_i_dsrl_safe(p, scratch, tmp, PUD_SHIFT - 3);
  1050. uasm_i_andi(p, scratch, scratch, (PTRS_PER_PUD - 1) << 3);
  1051. if (use_lwx_insns()) {
  1052. UASM_i_LWX(p, ptr, scratch, ptr);
  1053. } else {
  1054. uasm_i_daddu(p, ptr, ptr, scratch); /* add in pmd offset */
  1055. UASM_i_LW(p, ptr, 0, ptr);
  1056. }
  1057. /* ptr contains a pointer to PMD entry */
  1058. /* tmp contains the address */
  1059. #endif
  1060. #ifndef __PAGETABLE_PMD_FOLDED
  1061. /* get pmd offset in bytes */
  1062. uasm_i_dsrl_safe(p, scratch, tmp, PMD_SHIFT - 3);
  1063. uasm_i_andi(p, scratch, scratch, (PTRS_PER_PMD - 1) << 3);
  1064. GET_CONTEXT(p, tmp); /* get context reg */
  1065. if (use_lwx_insns()) {
  1066. UASM_i_LWX(p, scratch, scratch, ptr);
  1067. } else {
  1068. uasm_i_daddu(p, ptr, ptr, scratch); /* add in pmd offset */
  1069. UASM_i_LW(p, scratch, 0, ptr);
  1070. }
  1071. #endif
  1072. /* Adjust the context during the load latency. */
  1073. build_adjust_context(p, tmp);
  1074. #ifdef CONFIG_MIPS_HUGE_TLB_SUPPORT
  1075. uasm_il_bbit1(p, r, scratch, ilog2(_PAGE_HUGE), label_tlb_huge_update);
  1076. /*
  1077. * The in the LWX case we don't want to do the load in the
  1078. * delay slot. It cannot issue in the same cycle and may be
  1079. * speculative and unneeded.
  1080. */
  1081. if (use_lwx_insns())
  1082. uasm_i_nop(p);
  1083. #endif /* CONFIG_MIPS_HUGE_TLB_SUPPORT */
  1084. /* build_update_entries */
  1085. if (use_lwx_insns()) {
  1086. even = ptr;
  1087. odd = tmp;
  1088. UASM_i_LWX(p, even, scratch, tmp);
  1089. UASM_i_ADDIU(p, tmp, tmp, sizeof(pte_t));
  1090. UASM_i_LWX(p, odd, scratch, tmp);
  1091. } else {
  1092. UASM_i_ADDU(p, ptr, scratch, tmp); /* add in offset */
  1093. even = tmp;
  1094. odd = ptr;
  1095. UASM_i_LW(p, even, 0, ptr); /* get even pte */
  1096. UASM_i_LW(p, odd, sizeof(pte_t), ptr); /* get odd pte */
  1097. }
  1098. if (cpu_has_rixi) {
  1099. uasm_i_drotr(p, even, even, ilog2(_PAGE_GLOBAL));
  1100. UASM_i_MTC0(p, even, C0_ENTRYLO0); /* load it */
  1101. uasm_i_drotr(p, odd, odd, ilog2(_PAGE_GLOBAL));
  1102. } else {
  1103. uasm_i_dsrl_safe(p, even, even, ilog2(_PAGE_GLOBAL));
  1104. UASM_i_MTC0(p, even, C0_ENTRYLO0); /* load it */
  1105. uasm_i_dsrl_safe(p, odd, odd, ilog2(_PAGE_GLOBAL));
  1106. }
  1107. UASM_i_MTC0(p, odd, C0_ENTRYLO1); /* load it */
  1108. if (c0_scratch_reg >= 0) {
  1109. UASM_i_MFC0(p, scratch, c0_kscratch(), c0_scratch_reg);
  1110. build_tlb_write_entry(p, l, r, tlb_random);
  1111. uasm_l_leave(l, *p);
  1112. rv.restore_scratch = 1;
  1113. } else if (PAGE_SHIFT == 14 || PAGE_SHIFT == 13) {
  1114. build_tlb_write_entry(p, l, r, tlb_random);
  1115. uasm_l_leave(l, *p);
  1116. UASM_i_LW(p, scratch, scratchpad_offset(0), 0);
  1117. } else {
  1118. UASM_i_LW(p, scratch, scratchpad_offset(0), 0);
  1119. build_tlb_write_entry(p, l, r, tlb_random);
  1120. uasm_l_leave(l, *p);
  1121. rv.restore_scratch = 1;
  1122. }
  1123. uasm_i_eret(p); /* return from trap */
  1124. return rv;
  1125. }
  1126. /*
  1127. * For a 64-bit kernel, we are using the 64-bit XTLB refill exception
  1128. * because EXL == 0. If we wrap, we can also use the 32 instruction
  1129. * slots before the XTLB refill exception handler which belong to the
  1130. * unused TLB refill exception.
  1131. */
  1132. #define MIPS64_REFILL_INSNS 32
  1133. static void build_r4000_tlb_refill_handler(void)
  1134. {
  1135. u32 *p = tlb_handler;
  1136. struct uasm_label *l = labels;
  1137. struct uasm_reloc *r = relocs;
  1138. u32 *f;
  1139. unsigned int final_len;
  1140. struct mips_huge_tlb_info htlb_info __maybe_unused;
  1141. enum vmalloc64_mode vmalloc_mode __maybe_unused;
  1142. memset(tlb_handler, 0, sizeof(tlb_handler));
  1143. memset(labels, 0, sizeof(labels));
  1144. memset(relocs, 0, sizeof(relocs));
  1145. memset(final_handler, 0, sizeof(final_handler));
  1146. if (IS_ENABLED(CONFIG_64BIT) && (scratch_reg >= 0 || scratchpad_available()) && use_bbit_insns()) {
  1147. htlb_info = build_fast_tlb_refill_handler(&p, &l, &r, K0, K1,
  1148. scratch_reg);
  1149. vmalloc_mode = refill_scratch;
  1150. } else {
  1151. htlb_info.huge_pte = K0;
  1152. htlb_info.restore_scratch = 0;
  1153. htlb_info.need_reload_pte = true;
  1154. vmalloc_mode = refill_noscratch;
  1155. /*
  1156. * create the plain linear handler
  1157. */
  1158. if (bcm1250_m3_war()) {
  1159. unsigned int segbits = 44;
  1160. uasm_i_dmfc0(&p, K0, C0_BADVADDR);
  1161. uasm_i_dmfc0(&p, K1, C0_ENTRYHI);
  1162. uasm_i_xor(&p, K0, K0, K1);
  1163. uasm_i_dsrl_safe(&p, K1, K0, 62);
  1164. uasm_i_dsrl_safe(&p, K0, K0, 12 + 1);
  1165. uasm_i_dsll_safe(&p, K0, K0, 64 + 12 + 1 - segbits);
  1166. uasm_i_or(&p, K0, K0, K1);
  1167. uasm_il_bnez(&p, &r, K0, label_leave);
  1168. /* No need for uasm_i_nop */
  1169. }
  1170. #ifdef CONFIG_64BIT
  1171. build_get_pmde64(&p, &l, &r, K0, K1); /* get pmd in K1 */
  1172. #else
  1173. build_get_pgde32(&p, K0, K1); /* get pgd in K1 */
  1174. #endif
  1175. #ifdef CONFIG_MIPS_HUGE_TLB_SUPPORT
  1176. build_is_huge_pte(&p, &r, K0, K1, label_tlb_huge_update);
  1177. #endif
  1178. build_get_ptep(&p, K0, K1);
  1179. build_update_entries(&p, K0, K1);
  1180. build_tlb_write_entry(&p, &l, &r, tlb_random);
  1181. uasm_l_leave(&l, p);
  1182. uasm_i_eret(&p); /* return from trap */
  1183. }
  1184. #ifdef CONFIG_MIPS_HUGE_TLB_SUPPORT
  1185. uasm_l_tlb_huge_update(&l, p);
  1186. if (htlb_info.need_reload_pte)
  1187. UASM_i_LW(&p, htlb_info.huge_pte, 0, K1);
  1188. build_huge_update_entries(&p, htlb_info.huge_pte, K1);
  1189. build_huge_tlb_write_entry(&p, &l, &r, K0, tlb_random,
  1190. htlb_info.restore_scratch);
  1191. #endif
  1192. #ifdef CONFIG_64BIT
  1193. build_get_pgd_vmalloc64(&p, &l, &r, K0, K1, vmalloc_mode);
  1194. #endif
  1195. /*
  1196. * Overflow check: For the 64bit handler, we need at least one
  1197. * free instruction slot for the wrap-around branch. In worst
  1198. * case, if the intended insertion point is a delay slot, we
  1199. * need three, with the second nop'ed and the third being
  1200. * unused.
  1201. */
  1202. switch (boot_cpu_type()) {
  1203. default:
  1204. if (sizeof(long) == 4) {
  1205. case CPU_LOONGSON2:
  1206. /* Loongson2 ebase is different than r4k, we have more space */
  1207. if ((p - tlb_handler) > 64)
  1208. panic("TLB refill handler space exceeded");
  1209. /*
  1210. * Now fold the handler in the TLB refill handler space.
  1211. */
  1212. f = final_handler;
  1213. /* Simplest case, just copy the handler. */
  1214. uasm_copy_handler(relocs, labels, tlb_handler, p, f);
  1215. final_len = p - tlb_handler;
  1216. break;
  1217. } else {
  1218. if (((p - tlb_handler) > (MIPS64_REFILL_INSNS * 2) - 1)
  1219. || (((p - tlb_handler) > (MIPS64_REFILL_INSNS * 2) - 3)
  1220. && uasm_insn_has_bdelay(relocs,
  1221. tlb_handler + MIPS64_REFILL_INSNS - 3)))
  1222. panic("TLB refill handler space exceeded");
  1223. /*
  1224. * Now fold the handler in the TLB refill handler space.
  1225. */
  1226. f = final_handler + MIPS64_REFILL_INSNS;
  1227. if ((p - tlb_handler) <= MIPS64_REFILL_INSNS) {
  1228. /* Just copy the handler. */
  1229. uasm_copy_handler(relocs, labels, tlb_handler, p, f);
  1230. final_len = p - tlb_handler;
  1231. } else {
  1232. #ifdef CONFIG_MIPS_HUGE_TLB_SUPPORT
  1233. const enum label_id ls = label_tlb_huge_update;
  1234. #else
  1235. const enum label_id ls = label_vmalloc;
  1236. #endif
  1237. u32 *split;
  1238. int ov = 0;
  1239. int i;
  1240. for (i = 0; i < ARRAY_SIZE(labels) && labels[i].lab != ls; i++)
  1241. ;
  1242. BUG_ON(i == ARRAY_SIZE(labels));
  1243. split = labels[i].addr;
  1244. /*
  1245. * See if we have overflown one way or the other.
  1246. */
  1247. if (split > tlb_handler + MIPS64_REFILL_INSNS ||
  1248. split < p - MIPS64_REFILL_INSNS)
  1249. ov = 1;
  1250. if (ov) {
  1251. /*
  1252. * Split two instructions before the end. One
  1253. * for the branch and one for the instruction
  1254. * in the delay slot.
  1255. */
  1256. split = tlb_handler + MIPS64_REFILL_INSNS - 2;
  1257. /*
  1258. * If the branch would fall in a delay slot,
  1259. * we must back up an additional instruction
  1260. * so that it is no longer in a delay slot.
  1261. */
  1262. if (uasm_insn_has_bdelay(relocs, split - 1))
  1263. split--;
  1264. }
  1265. /* Copy first part of the handler. */
  1266. uasm_copy_handler(relocs, labels, tlb_handler, split, f);
  1267. f += split - tlb_handler;
  1268. if (ov) {
  1269. /* Insert branch. */
  1270. uasm_l_split(&l, final_handler);
  1271. uasm_il_b(&f, &r, label_split);
  1272. if (uasm_insn_has_bdelay(relocs, split))
  1273. uasm_i_nop(&f);
  1274. else {
  1275. uasm_copy_handler(relocs, labels,
  1276. split, split + 1, f);
  1277. uasm_move_labels(labels, f, f + 1, -1);
  1278. f++;
  1279. split++;
  1280. }
  1281. }
  1282. /* Copy the rest of the handler. */
  1283. uasm_copy_handler(relocs, labels, split, p, final_handler);
  1284. final_len = (f - (final_handler + MIPS64_REFILL_INSNS)) +
  1285. (p - split);
  1286. }
  1287. }
  1288. break;
  1289. }
  1290. uasm_resolve_relocs(relocs, labels);
  1291. pr_debug("Wrote TLB refill handler (%u instructions).\n",
  1292. final_len);
  1293. memcpy((void *)ebase, final_handler, 0x100);
  1294. local_flush_icache_range(ebase, ebase + 0x100);
  1295. dump_handler("r4000_tlb_refill", (u32 *)ebase, 64);
  1296. }
  1297. static void setup_pw(void)
  1298. {
  1299. unsigned long pgd_i, pgd_w;
  1300. #ifndef __PAGETABLE_PMD_FOLDED
  1301. unsigned long pmd_i, pmd_w;
  1302. #endif
  1303. unsigned long pt_i, pt_w;
  1304. unsigned long pte_i, pte_w;
  1305. #ifdef CONFIG_MIPS_HUGE_TLB_SUPPORT
  1306. unsigned long psn;
  1307. psn = ilog2(_PAGE_HUGE); /* bit used to indicate huge page */
  1308. #endif
  1309. pgd_i = PGDIR_SHIFT; /* 1st level PGD */
  1310. #ifndef __PAGETABLE_PMD_FOLDED
  1311. pgd_w = PGDIR_SHIFT - PMD_SHIFT + PGD_ORDER;
  1312. pmd_i = PMD_SHIFT; /* 2nd level PMD */
  1313. pmd_w = PMD_SHIFT - PAGE_SHIFT;
  1314. #else
  1315. pgd_w = PGDIR_SHIFT - PAGE_SHIFT + PGD_ORDER;
  1316. #endif
  1317. pt_i = PAGE_SHIFT; /* 3rd level PTE */
  1318. pt_w = PAGE_SHIFT - 3;
  1319. pte_i = ilog2(_PAGE_GLOBAL);
  1320. pte_w = 0;
  1321. #ifndef __PAGETABLE_PMD_FOLDED
  1322. write_c0_pwfield(pgd_i << 24 | pmd_i << 12 | pt_i << 6 | pte_i);
  1323. write_c0_pwsize(1 << 30 | pgd_w << 24 | pmd_w << 12 | pt_w << 6 | pte_w);
  1324. #else
  1325. write_c0_pwfield(pgd_i << 24 | pt_i << 6 | pte_i);
  1326. write_c0_pwsize(1 << 30 | pgd_w << 24 | pt_w << 6 | pte_w);
  1327. #endif
  1328. #ifdef CONFIG_MIPS_HUGE_TLB_SUPPORT
  1329. write_c0_pwctl(1 << 6 | psn);
  1330. #endif
  1331. write_c0_kpgd(swapper_pg_dir);
  1332. kscratch_used_mask |= (1 << 7); /* KScratch6 is used for KPGD */
  1333. }
  1334. static void build_loongson3_tlb_refill_handler(void)
  1335. {
  1336. u32 *p = tlb_handler;
  1337. struct uasm_label *l = labels;
  1338. struct uasm_reloc *r = relocs;
  1339. memset(labels, 0, sizeof(labels));
  1340. memset(relocs, 0, sizeof(relocs));
  1341. memset(tlb_handler, 0, sizeof(tlb_handler));
  1342. if (check_for_high_segbits) {
  1343. uasm_i_dmfc0(&p, K0, C0_BADVADDR);
  1344. uasm_i_dsrl_safe(&p, K1, K0, PGDIR_SHIFT + PGD_ORDER + PAGE_SHIFT - 3);
  1345. uasm_il_beqz(&p, &r, K1, label_vmalloc);
  1346. uasm_i_nop(&p);
  1347. uasm_il_bgez(&p, &r, K0, label_large_segbits_fault);
  1348. uasm_i_nop(&p);
  1349. uasm_l_vmalloc(&l, p);
  1350. }
  1351. uasm_i_dmfc0(&p, K1, C0_PGD);
  1352. uasm_i_lddir(&p, K0, K1, 3); /* global page dir */
  1353. #ifndef __PAGETABLE_PMD_FOLDED
  1354. uasm_i_lddir(&p, K1, K0, 1); /* middle page dir */
  1355. #endif
  1356. uasm_i_ldpte(&p, K1, 0); /* even */
  1357. uasm_i_ldpte(&p, K1, 1); /* odd */
  1358. uasm_i_tlbwr(&p);
  1359. /* restore page mask */
  1360. if (PM_DEFAULT_MASK >> 16) {
  1361. uasm_i_lui(&p, K0, PM_DEFAULT_MASK >> 16);
  1362. uasm_i_ori(&p, K0, K0, PM_DEFAULT_MASK & 0xffff);
  1363. uasm_i_mtc0(&p, K0, C0_PAGEMASK);
  1364. } else if (PM_DEFAULT_MASK) {
  1365. uasm_i_ori(&p, K0, 0, PM_DEFAULT_MASK);
  1366. uasm_i_mtc0(&p, K0, C0_PAGEMASK);
  1367. } else {
  1368. uasm_i_mtc0(&p, 0, C0_PAGEMASK);
  1369. }
  1370. uasm_i_eret(&p);
  1371. if (check_for_high_segbits) {
  1372. uasm_l_large_segbits_fault(&l, p);
  1373. UASM_i_LA(&p, K1, (unsigned long)tlb_do_page_fault_0);
  1374. uasm_i_jr(&p, K1);
  1375. uasm_i_nop(&p);
  1376. }
  1377. uasm_resolve_relocs(relocs, labels);
  1378. memcpy((void *)(ebase + 0x80), tlb_handler, 0x80);
  1379. local_flush_icache_range(ebase + 0x80, ebase + 0x100);
  1380. dump_handler("loongson3_tlb_refill", (u32 *)(ebase + 0x80), 32);
  1381. }
  1382. extern u32 handle_tlbl[], handle_tlbl_end[];
  1383. extern u32 handle_tlbs[], handle_tlbs_end[];
  1384. extern u32 handle_tlbm[], handle_tlbm_end[];
  1385. extern u32 tlbmiss_handler_setup_pgd_start[];
  1386. extern u32 tlbmiss_handler_setup_pgd[];
  1387. EXPORT_SYMBOL_GPL(tlbmiss_handler_setup_pgd);
  1388. extern u32 tlbmiss_handler_setup_pgd_end[];
  1389. static void build_setup_pgd(void)
  1390. {
  1391. const int a0 = 4;
  1392. const int __maybe_unused a1 = 5;
  1393. const int __maybe_unused a2 = 6;
  1394. u32 *p = tlbmiss_handler_setup_pgd_start;
  1395. const int tlbmiss_handler_setup_pgd_size =
  1396. tlbmiss_handler_setup_pgd_end - tlbmiss_handler_setup_pgd_start;
  1397. #ifndef CONFIG_MIPS_PGD_C0_CONTEXT
  1398. long pgdc = (long)pgd_current;
  1399. #endif
  1400. memset(tlbmiss_handler_setup_pgd, 0, tlbmiss_handler_setup_pgd_size *
  1401. sizeof(tlbmiss_handler_setup_pgd[0]));
  1402. memset(labels, 0, sizeof(labels));
  1403. memset(relocs, 0, sizeof(relocs));
  1404. pgd_reg = allocate_kscratch();
  1405. #ifdef CONFIG_MIPS_PGD_C0_CONTEXT
  1406. if (pgd_reg == -1) {
  1407. struct uasm_label *l = labels;
  1408. struct uasm_reloc *r = relocs;
  1409. /* PGD << 11 in c0_Context */
  1410. /*
  1411. * If it is a ckseg0 address, convert to a physical
  1412. * address. Shifting right by 29 and adding 4 will
  1413. * result in zero for these addresses.
  1414. *
  1415. */
  1416. UASM_i_SRA(&p, a1, a0, 29);
  1417. UASM_i_ADDIU(&p, a1, a1, 4);
  1418. uasm_il_bnez(&p, &r, a1, label_tlbl_goaround1);
  1419. uasm_i_nop(&p);
  1420. uasm_i_dinsm(&p, a0, 0, 29, 64 - 29);
  1421. uasm_l_tlbl_goaround1(&l, p);
  1422. UASM_i_SLL(&p, a0, a0, 11);
  1423. uasm_i_jr(&p, 31);
  1424. UASM_i_MTC0(&p, a0, C0_CONTEXT);
  1425. } else {
  1426. /* PGD in c0_KScratch */
  1427. uasm_i_jr(&p, 31);
  1428. if (cpu_has_ldpte)
  1429. UASM_i_MTC0(&p, a0, C0_PWBASE);
  1430. else
  1431. UASM_i_MTC0(&p, a0, c0_kscratch(), pgd_reg);
  1432. }
  1433. #else
  1434. #ifdef CONFIG_SMP
  1435. /* Save PGD to pgd_current[smp_processor_id()] */
  1436. UASM_i_CPUID_MFC0(&p, a1, SMP_CPUID_REG);
  1437. UASM_i_SRL_SAFE(&p, a1, a1, SMP_CPUID_PTRSHIFT);
  1438. UASM_i_LA_mostly(&p, a2, pgdc);
  1439. UASM_i_ADDU(&p, a2, a2, a1);
  1440. UASM_i_SW(&p, a0, uasm_rel_lo(pgdc), a2);
  1441. #else
  1442. UASM_i_LA_mostly(&p, a2, pgdc);
  1443. UASM_i_SW(&p, a0, uasm_rel_lo(pgdc), a2);
  1444. #endif /* SMP */
  1445. uasm_i_jr(&p, 31);
  1446. /* if pgd_reg is allocated, save PGD also to scratch register */
  1447. if (pgd_reg != -1)
  1448. UASM_i_MTC0(&p, a0, c0_kscratch(), pgd_reg);
  1449. else
  1450. uasm_i_nop(&p);
  1451. #endif
  1452. if (p >= tlbmiss_handler_setup_pgd_end)
  1453. panic("tlbmiss_handler_setup_pgd space exceeded");
  1454. uasm_resolve_relocs(relocs, labels);
  1455. pr_debug("Wrote tlbmiss_handler_setup_pgd (%u instructions).\n",
  1456. (unsigned int)(p - tlbmiss_handler_setup_pgd));
  1457. dump_handler("tlbmiss_handler", tlbmiss_handler_setup_pgd,
  1458. tlbmiss_handler_setup_pgd_size);
  1459. }
  1460. static void
  1461. iPTE_LW(u32 **p, unsigned int pte, unsigned int ptr)
  1462. {
  1463. #ifdef CONFIG_SMP
  1464. # ifdef CONFIG_PHYS_ADDR_T_64BIT
  1465. if (cpu_has_64bits)
  1466. uasm_i_lld(p, pte, 0, ptr);
  1467. else
  1468. # endif
  1469. UASM_i_LL(p, pte, 0, ptr);
  1470. #else
  1471. # ifdef CONFIG_PHYS_ADDR_T_64BIT
  1472. if (cpu_has_64bits)
  1473. uasm_i_ld(p, pte, 0, ptr);
  1474. else
  1475. # endif
  1476. UASM_i_LW(p, pte, 0, ptr);
  1477. #endif
  1478. }
  1479. static void
  1480. iPTE_SW(u32 **p, struct uasm_reloc **r, unsigned int pte, unsigned int ptr,
  1481. unsigned int mode, unsigned int scratch)
  1482. {
  1483. unsigned int hwmode = mode & (_PAGE_VALID | _PAGE_DIRTY);
  1484. unsigned int swmode = mode & ~hwmode;
  1485. if (IS_ENABLED(CONFIG_XPA) && !cpu_has_64bits) {
  1486. uasm_i_lui(p, scratch, swmode >> 16);
  1487. uasm_i_or(p, pte, pte, scratch);
  1488. BUG_ON(swmode & 0xffff);
  1489. } else {
  1490. uasm_i_ori(p, pte, pte, mode);
  1491. }
  1492. #ifdef CONFIG_SMP
  1493. # ifdef CONFIG_PHYS_ADDR_T_64BIT
  1494. if (cpu_has_64bits)
  1495. uasm_i_scd(p, pte, 0, ptr);
  1496. else
  1497. # endif
  1498. UASM_i_SC(p, pte, 0, ptr);
  1499. if (r10000_llsc_war())
  1500. uasm_il_beqzl(p, r, pte, label_smp_pgtable_change);
  1501. else
  1502. uasm_il_beqz(p, r, pte, label_smp_pgtable_change);
  1503. # ifdef CONFIG_PHYS_ADDR_T_64BIT
  1504. if (!cpu_has_64bits) {
  1505. /* no uasm_i_nop needed */
  1506. uasm_i_ll(p, pte, sizeof(pte_t) / 2, ptr);
  1507. uasm_i_ori(p, pte, pte, hwmode);
  1508. BUG_ON(hwmode & ~0xffff);
  1509. uasm_i_sc(p, pte, sizeof(pte_t) / 2, ptr);
  1510. uasm_il_beqz(p, r, pte, label_smp_pgtable_change);
  1511. /* no uasm_i_nop needed */
  1512. uasm_i_lw(p, pte, 0, ptr);
  1513. } else
  1514. uasm_i_nop(p);
  1515. # else
  1516. uasm_i_nop(p);
  1517. # endif
  1518. #else
  1519. # ifdef CONFIG_PHYS_ADDR_T_64BIT
  1520. if (cpu_has_64bits)
  1521. uasm_i_sd(p, pte, 0, ptr);
  1522. else
  1523. # endif
  1524. UASM_i_SW(p, pte, 0, ptr);
  1525. # ifdef CONFIG_PHYS_ADDR_T_64BIT
  1526. if (!cpu_has_64bits) {
  1527. uasm_i_lw(p, pte, sizeof(pte_t) / 2, ptr);
  1528. uasm_i_ori(p, pte, pte, hwmode);
  1529. BUG_ON(hwmode & ~0xffff);
  1530. uasm_i_sw(p, pte, sizeof(pte_t) / 2, ptr);
  1531. uasm_i_lw(p, pte, 0, ptr);
  1532. }
  1533. # endif
  1534. #endif
  1535. }
  1536. /*
  1537. * Check if PTE is present, if not then jump to LABEL. PTR points to
  1538. * the page table where this PTE is located, PTE will be re-loaded
  1539. * with it's original value.
  1540. */
  1541. static void
  1542. build_pte_present(u32 **p, struct uasm_reloc **r,
  1543. int pte, int ptr, int scratch, enum label_id lid)
  1544. {
  1545. int t = scratch >= 0 ? scratch : pte;
  1546. int cur = pte;
  1547. if (cpu_has_rixi) {
  1548. if (use_bbit_insns()) {
  1549. uasm_il_bbit0(p, r, pte, ilog2(_PAGE_PRESENT), lid);
  1550. uasm_i_nop(p);
  1551. } else {
  1552. if (_PAGE_PRESENT_SHIFT) {
  1553. uasm_i_srl(p, t, cur, _PAGE_PRESENT_SHIFT);
  1554. cur = t;
  1555. }
  1556. uasm_i_andi(p, t, cur, 1);
  1557. uasm_il_beqz(p, r, t, lid);
  1558. if (pte == t)
  1559. /* You lose the SMP race :-(*/
  1560. iPTE_LW(p, pte, ptr);
  1561. }
  1562. } else {
  1563. if (_PAGE_PRESENT_SHIFT) {
  1564. uasm_i_srl(p, t, cur, _PAGE_PRESENT_SHIFT);
  1565. cur = t;
  1566. }
  1567. uasm_i_andi(p, t, cur,
  1568. (_PAGE_PRESENT | _PAGE_NO_READ) >> _PAGE_PRESENT_SHIFT);
  1569. uasm_i_xori(p, t, t, _PAGE_PRESENT >> _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. }
  1575. }
  1576. /* Make PTE valid, store result in PTR. */
  1577. static void
  1578. build_make_valid(u32 **p, struct uasm_reloc **r, unsigned int pte,
  1579. unsigned int ptr, unsigned int scratch)
  1580. {
  1581. unsigned int mode = _PAGE_VALID | _PAGE_ACCESSED;
  1582. iPTE_SW(p, r, pte, ptr, mode, scratch);
  1583. }
  1584. /*
  1585. * Check if PTE can be written to, if not branch to LABEL. Regardless
  1586. * restore PTE with value from PTR when done.
  1587. */
  1588. static void
  1589. build_pte_writable(u32 **p, struct uasm_reloc **r,
  1590. unsigned int pte, unsigned int ptr, int scratch,
  1591. enum label_id lid)
  1592. {
  1593. int t = scratch >= 0 ? scratch : pte;
  1594. int cur = pte;
  1595. if (_PAGE_PRESENT_SHIFT) {
  1596. uasm_i_srl(p, t, cur, _PAGE_PRESENT_SHIFT);
  1597. cur = t;
  1598. }
  1599. uasm_i_andi(p, t, cur,
  1600. (_PAGE_PRESENT | _PAGE_WRITE) >> _PAGE_PRESENT_SHIFT);
  1601. uasm_i_xori(p, t, t,
  1602. (_PAGE_PRESENT | _PAGE_WRITE) >> _PAGE_PRESENT_SHIFT);
  1603. uasm_il_bnez(p, r, t, lid);
  1604. if (pte == t)
  1605. /* You lose the SMP race :-(*/
  1606. iPTE_LW(p, pte, ptr);
  1607. else
  1608. uasm_i_nop(p);
  1609. }
  1610. /* Make PTE writable, update software status bits as well, then store
  1611. * at PTR.
  1612. */
  1613. static void
  1614. build_make_write(u32 **p, struct uasm_reloc **r, unsigned int pte,
  1615. unsigned int ptr, unsigned int scratch)
  1616. {
  1617. unsigned int mode = (_PAGE_ACCESSED | _PAGE_MODIFIED | _PAGE_VALID
  1618. | _PAGE_DIRTY);
  1619. iPTE_SW(p, r, pte, ptr, mode, scratch);
  1620. }
  1621. /*
  1622. * Check if PTE can be modified, if not branch to LABEL. Regardless
  1623. * restore PTE with value from PTR when done.
  1624. */
  1625. static void
  1626. build_pte_modifiable(u32 **p, struct uasm_reloc **r,
  1627. unsigned int pte, unsigned int ptr, int scratch,
  1628. enum label_id lid)
  1629. {
  1630. if (use_bbit_insns()) {
  1631. uasm_il_bbit0(p, r, pte, ilog2(_PAGE_WRITE), lid);
  1632. uasm_i_nop(p);
  1633. } else {
  1634. int t = scratch >= 0 ? scratch : pte;
  1635. uasm_i_srl(p, t, pte, _PAGE_WRITE_SHIFT);
  1636. uasm_i_andi(p, t, t, 1);
  1637. uasm_il_beqz(p, r, t, lid);
  1638. if (pte == t)
  1639. /* You lose the SMP race :-(*/
  1640. iPTE_LW(p, pte, ptr);
  1641. }
  1642. }
  1643. #ifndef CONFIG_MIPS_PGD_C0_CONTEXT
  1644. /*
  1645. * R3000 style TLB load/store/modify handlers.
  1646. */
  1647. /*
  1648. * This places the pte into ENTRYLO0 and writes it with tlbwi.
  1649. * Then it returns.
  1650. */
  1651. static void
  1652. build_r3000_pte_reload_tlbwi(u32 **p, unsigned int pte, unsigned int tmp)
  1653. {
  1654. uasm_i_mtc0(p, pte, C0_ENTRYLO0); /* cp0 delay */
  1655. uasm_i_mfc0(p, tmp, C0_EPC); /* cp0 delay */
  1656. uasm_i_tlbwi(p);
  1657. uasm_i_jr(p, tmp);
  1658. uasm_i_rfe(p); /* branch delay */
  1659. }
  1660. /*
  1661. * This places the pte into ENTRYLO0 and writes it with tlbwi
  1662. * or tlbwr as appropriate. This is because the index register
  1663. * may have the probe fail bit set as a result of a trap on a
  1664. * kseg2 access, i.e. without refill. Then it returns.
  1665. */
  1666. static void
  1667. build_r3000_tlb_reload_write(u32 **p, struct uasm_label **l,
  1668. struct uasm_reloc **r, unsigned int pte,
  1669. unsigned int tmp)
  1670. {
  1671. uasm_i_mfc0(p, tmp, C0_INDEX);
  1672. uasm_i_mtc0(p, pte, C0_ENTRYLO0); /* cp0 delay */
  1673. uasm_il_bltz(p, r, tmp, label_r3000_write_probe_fail); /* cp0 delay */
  1674. uasm_i_mfc0(p, tmp, C0_EPC); /* branch delay */
  1675. uasm_i_tlbwi(p); /* cp0 delay */
  1676. uasm_i_jr(p, tmp);
  1677. uasm_i_rfe(p); /* branch delay */
  1678. uasm_l_r3000_write_probe_fail(l, *p);
  1679. uasm_i_tlbwr(p); /* cp0 delay */
  1680. uasm_i_jr(p, tmp);
  1681. uasm_i_rfe(p); /* branch delay */
  1682. }
  1683. static void
  1684. build_r3000_tlbchange_handler_head(u32 **p, unsigned int pte,
  1685. unsigned int ptr)
  1686. {
  1687. long pgdc = (long)pgd_current;
  1688. uasm_i_mfc0(p, pte, C0_BADVADDR);
  1689. uasm_i_lui(p, ptr, uasm_rel_hi(pgdc)); /* cp0 delay */
  1690. uasm_i_lw(p, ptr, uasm_rel_lo(pgdc), ptr);
  1691. uasm_i_srl(p, pte, pte, 22); /* load delay */
  1692. uasm_i_sll(p, pte, pte, 2);
  1693. uasm_i_addu(p, ptr, ptr, pte);
  1694. uasm_i_mfc0(p, pte, C0_CONTEXT);
  1695. uasm_i_lw(p, ptr, 0, ptr); /* cp0 delay */
  1696. uasm_i_andi(p, pte, pte, 0xffc); /* load delay */
  1697. uasm_i_addu(p, ptr, ptr, pte);
  1698. uasm_i_lw(p, pte, 0, ptr);
  1699. uasm_i_tlbp(p); /* load delay */
  1700. }
  1701. static void build_r3000_tlb_load_handler(void)
  1702. {
  1703. u32 *p = handle_tlbl;
  1704. const int handle_tlbl_size = handle_tlbl_end - handle_tlbl;
  1705. struct uasm_label *l = labels;
  1706. struct uasm_reloc *r = relocs;
  1707. memset(handle_tlbl, 0, handle_tlbl_size * sizeof(handle_tlbl[0]));
  1708. memset(labels, 0, sizeof(labels));
  1709. memset(relocs, 0, sizeof(relocs));
  1710. build_r3000_tlbchange_handler_head(&p, K0, K1);
  1711. build_pte_present(&p, &r, K0, K1, -1, label_nopage_tlbl);
  1712. uasm_i_nop(&p); /* load delay */
  1713. build_make_valid(&p, &r, K0, K1, -1);
  1714. build_r3000_tlb_reload_write(&p, &l, &r, K0, K1);
  1715. uasm_l_nopage_tlbl(&l, p);
  1716. uasm_i_j(&p, (unsigned long)tlb_do_page_fault_0 & 0x0fffffff);
  1717. uasm_i_nop(&p);
  1718. if (p >= handle_tlbl_end)
  1719. panic("TLB load handler fastpath space exceeded");
  1720. uasm_resolve_relocs(relocs, labels);
  1721. pr_debug("Wrote TLB load handler fastpath (%u instructions).\n",
  1722. (unsigned int)(p - handle_tlbl));
  1723. dump_handler("r3000_tlb_load", handle_tlbl, handle_tlbl_size);
  1724. }
  1725. static void build_r3000_tlb_store_handler(void)
  1726. {
  1727. u32 *p = handle_tlbs;
  1728. const int handle_tlbs_size = handle_tlbs_end - handle_tlbs;
  1729. struct uasm_label *l = labels;
  1730. struct uasm_reloc *r = relocs;
  1731. memset(handle_tlbs, 0, handle_tlbs_size * sizeof(handle_tlbs[0]));
  1732. memset(labels, 0, sizeof(labels));
  1733. memset(relocs, 0, sizeof(relocs));
  1734. build_r3000_tlbchange_handler_head(&p, K0, K1);
  1735. build_pte_writable(&p, &r, K0, K1, -1, label_nopage_tlbs);
  1736. uasm_i_nop(&p); /* load delay */
  1737. build_make_write(&p, &r, K0, K1, -1);
  1738. build_r3000_tlb_reload_write(&p, &l, &r, K0, K1);
  1739. uasm_l_nopage_tlbs(&l, p);
  1740. uasm_i_j(&p, (unsigned long)tlb_do_page_fault_1 & 0x0fffffff);
  1741. uasm_i_nop(&p);
  1742. if (p >= handle_tlbs_end)
  1743. panic("TLB store handler fastpath space exceeded");
  1744. uasm_resolve_relocs(relocs, labels);
  1745. pr_debug("Wrote TLB store handler fastpath (%u instructions).\n",
  1746. (unsigned int)(p - handle_tlbs));
  1747. dump_handler("r3000_tlb_store", handle_tlbs, handle_tlbs_size);
  1748. }
  1749. static void build_r3000_tlb_modify_handler(void)
  1750. {
  1751. u32 *p = handle_tlbm;
  1752. const int handle_tlbm_size = handle_tlbm_end - handle_tlbm;
  1753. struct uasm_label *l = labels;
  1754. struct uasm_reloc *r = relocs;
  1755. memset(handle_tlbm, 0, handle_tlbm_size * sizeof(handle_tlbm[0]));
  1756. memset(labels, 0, sizeof(labels));
  1757. memset(relocs, 0, sizeof(relocs));
  1758. build_r3000_tlbchange_handler_head(&p, K0, K1);
  1759. build_pte_modifiable(&p, &r, K0, K1, -1, label_nopage_tlbm);
  1760. uasm_i_nop(&p); /* load delay */
  1761. build_make_write(&p, &r, K0, K1, -1);
  1762. build_r3000_pte_reload_tlbwi(&p, K0, K1);
  1763. uasm_l_nopage_tlbm(&l, p);
  1764. uasm_i_j(&p, (unsigned long)tlb_do_page_fault_1 & 0x0fffffff);
  1765. uasm_i_nop(&p);
  1766. if (p >= handle_tlbm_end)
  1767. panic("TLB modify handler fastpath space exceeded");
  1768. uasm_resolve_relocs(relocs, labels);
  1769. pr_debug("Wrote TLB modify handler fastpath (%u instructions).\n",
  1770. (unsigned int)(p - handle_tlbm));
  1771. dump_handler("r3000_tlb_modify", handle_tlbm, handle_tlbm_size);
  1772. }
  1773. #endif /* CONFIG_MIPS_PGD_C0_CONTEXT */
  1774. /*
  1775. * R4000 style TLB load/store/modify handlers.
  1776. */
  1777. static struct work_registers
  1778. build_r4000_tlbchange_handler_head(u32 **p, struct uasm_label **l,
  1779. struct uasm_reloc **r)
  1780. {
  1781. struct work_registers wr = build_get_work_registers(p);
  1782. #ifdef CONFIG_64BIT
  1783. build_get_pmde64(p, l, r, wr.r1, wr.r2); /* get pmd in ptr */
  1784. #else
  1785. build_get_pgde32(p, wr.r1, wr.r2); /* get pgd in ptr */
  1786. #endif
  1787. #ifdef CONFIG_MIPS_HUGE_TLB_SUPPORT
  1788. /*
  1789. * For huge tlb entries, pmd doesn't contain an address but
  1790. * instead contains the tlb pte. Check the PAGE_HUGE bit and
  1791. * see if we need to jump to huge tlb processing.
  1792. */
  1793. build_is_huge_pte(p, r, wr.r1, wr.r2, label_tlb_huge_update);
  1794. #endif
  1795. UASM_i_MFC0(p, wr.r1, C0_BADVADDR);
  1796. UASM_i_LW(p, wr.r2, 0, wr.r2);
  1797. UASM_i_SRL(p, wr.r1, wr.r1, PAGE_SHIFT + PTE_ORDER - PTE_T_LOG2);
  1798. uasm_i_andi(p, wr.r1, wr.r1, (PTRS_PER_PTE - 1) << PTE_T_LOG2);
  1799. UASM_i_ADDU(p, wr.r2, wr.r2, wr.r1);
  1800. #ifdef CONFIG_SMP
  1801. uasm_l_smp_pgtable_change(l, *p);
  1802. #endif
  1803. iPTE_LW(p, wr.r1, wr.r2); /* get even pte */
  1804. if (!m4kc_tlbp_war()) {
  1805. build_tlb_probe_entry(p);
  1806. if (cpu_has_htw) {
  1807. /* race condition happens, leaving */
  1808. uasm_i_ehb(p);
  1809. uasm_i_mfc0(p, wr.r3, C0_INDEX);
  1810. uasm_il_bltz(p, r, wr.r3, label_leave);
  1811. uasm_i_nop(p);
  1812. }
  1813. }
  1814. return wr;
  1815. }
  1816. static void
  1817. build_r4000_tlbchange_handler_tail(u32 **p, struct uasm_label **l,
  1818. struct uasm_reloc **r, unsigned int tmp,
  1819. unsigned int ptr)
  1820. {
  1821. uasm_i_ori(p, ptr, ptr, sizeof(pte_t));
  1822. uasm_i_xori(p, ptr, ptr, sizeof(pte_t));
  1823. build_update_entries(p, tmp, ptr);
  1824. build_tlb_write_entry(p, l, r, tlb_indexed);
  1825. uasm_l_leave(l, *p);
  1826. build_restore_work_registers(p);
  1827. uasm_i_eret(p); /* return from trap */
  1828. #ifdef CONFIG_64BIT
  1829. build_get_pgd_vmalloc64(p, l, r, tmp, ptr, not_refill);
  1830. #endif
  1831. }
  1832. static void build_r4000_tlb_load_handler(void)
  1833. {
  1834. u32 *p = (u32 *)msk_isa16_mode((ulong)handle_tlbl);
  1835. const int handle_tlbl_size = handle_tlbl_end - handle_tlbl;
  1836. struct uasm_label *l = labels;
  1837. struct uasm_reloc *r = relocs;
  1838. struct work_registers wr;
  1839. memset(handle_tlbl, 0, handle_tlbl_size * sizeof(handle_tlbl[0]));
  1840. memset(labels, 0, sizeof(labels));
  1841. memset(relocs, 0, sizeof(relocs));
  1842. if (bcm1250_m3_war()) {
  1843. unsigned int segbits = 44;
  1844. uasm_i_dmfc0(&p, K0, C0_BADVADDR);
  1845. uasm_i_dmfc0(&p, K1, C0_ENTRYHI);
  1846. uasm_i_xor(&p, K0, K0, K1);
  1847. uasm_i_dsrl_safe(&p, K1, K0, 62);
  1848. uasm_i_dsrl_safe(&p, K0, K0, 12 + 1);
  1849. uasm_i_dsll_safe(&p, K0, K0, 64 + 12 + 1 - segbits);
  1850. uasm_i_or(&p, K0, K0, K1);
  1851. uasm_il_bnez(&p, &r, K0, label_leave);
  1852. /* No need for uasm_i_nop */
  1853. }
  1854. wr = build_r4000_tlbchange_handler_head(&p, &l, &r);
  1855. build_pte_present(&p, &r, wr.r1, wr.r2, wr.r3, label_nopage_tlbl);
  1856. if (m4kc_tlbp_war())
  1857. build_tlb_probe_entry(&p);
  1858. if (cpu_has_rixi && !cpu_has_rixiex) {
  1859. /*
  1860. * If the page is not _PAGE_VALID, RI or XI could not
  1861. * have triggered it. Skip the expensive test..
  1862. */
  1863. if (use_bbit_insns()) {
  1864. uasm_il_bbit0(&p, &r, wr.r1, ilog2(_PAGE_VALID),
  1865. label_tlbl_goaround1);
  1866. } else {
  1867. uasm_i_andi(&p, wr.r3, wr.r1, _PAGE_VALID);
  1868. uasm_il_beqz(&p, &r, wr.r3, label_tlbl_goaround1);
  1869. }
  1870. uasm_i_nop(&p);
  1871. uasm_i_tlbr(&p);
  1872. switch (current_cpu_type()) {
  1873. default:
  1874. if (cpu_has_mips_r2_exec_hazard) {
  1875. uasm_i_ehb(&p);
  1876. case CPU_CAVIUM_OCTEON:
  1877. case CPU_CAVIUM_OCTEON_PLUS:
  1878. case CPU_CAVIUM_OCTEON2:
  1879. break;
  1880. }
  1881. }
  1882. /* Examine entrylo 0 or 1 based on ptr. */
  1883. if (use_bbit_insns()) {
  1884. uasm_i_bbit0(&p, wr.r2, ilog2(sizeof(pte_t)), 8);
  1885. } else {
  1886. uasm_i_andi(&p, wr.r3, wr.r2, sizeof(pte_t));
  1887. uasm_i_beqz(&p, wr.r3, 8);
  1888. }
  1889. /* load it in the delay slot*/
  1890. UASM_i_MFC0(&p, wr.r3, C0_ENTRYLO0);
  1891. /* load it if ptr is odd */
  1892. UASM_i_MFC0(&p, wr.r3, C0_ENTRYLO1);
  1893. /*
  1894. * If the entryLo (now in wr.r3) is valid (bit 1), RI or
  1895. * XI must have triggered it.
  1896. */
  1897. if (use_bbit_insns()) {
  1898. uasm_il_bbit1(&p, &r, wr.r3, 1, label_nopage_tlbl);
  1899. uasm_i_nop(&p);
  1900. uasm_l_tlbl_goaround1(&l, p);
  1901. } else {
  1902. uasm_i_andi(&p, wr.r3, wr.r3, 2);
  1903. uasm_il_bnez(&p, &r, wr.r3, label_nopage_tlbl);
  1904. uasm_i_nop(&p);
  1905. }
  1906. uasm_l_tlbl_goaround1(&l, p);
  1907. }
  1908. build_make_valid(&p, &r, wr.r1, wr.r2, wr.r3);
  1909. build_r4000_tlbchange_handler_tail(&p, &l, &r, wr.r1, wr.r2);
  1910. #ifdef CONFIG_MIPS_HUGE_TLB_SUPPORT
  1911. /*
  1912. * This is the entry point when build_r4000_tlbchange_handler_head
  1913. * spots a huge page.
  1914. */
  1915. uasm_l_tlb_huge_update(&l, p);
  1916. iPTE_LW(&p, wr.r1, wr.r2);
  1917. build_pte_present(&p, &r, wr.r1, wr.r2, wr.r3, label_nopage_tlbl);
  1918. build_tlb_probe_entry(&p);
  1919. if (cpu_has_rixi && !cpu_has_rixiex) {
  1920. /*
  1921. * If the page is not _PAGE_VALID, RI or XI could not
  1922. * have triggered it. Skip the expensive test..
  1923. */
  1924. if (use_bbit_insns()) {
  1925. uasm_il_bbit0(&p, &r, wr.r1, ilog2(_PAGE_VALID),
  1926. label_tlbl_goaround2);
  1927. } else {
  1928. uasm_i_andi(&p, wr.r3, wr.r1, _PAGE_VALID);
  1929. uasm_il_beqz(&p, &r, wr.r3, label_tlbl_goaround2);
  1930. }
  1931. uasm_i_nop(&p);
  1932. uasm_i_tlbr(&p);
  1933. switch (current_cpu_type()) {
  1934. default:
  1935. if (cpu_has_mips_r2_exec_hazard) {
  1936. uasm_i_ehb(&p);
  1937. case CPU_CAVIUM_OCTEON:
  1938. case CPU_CAVIUM_OCTEON_PLUS:
  1939. case CPU_CAVIUM_OCTEON2:
  1940. break;
  1941. }
  1942. }
  1943. /* Examine entrylo 0 or 1 based on ptr. */
  1944. if (use_bbit_insns()) {
  1945. uasm_i_bbit0(&p, wr.r2, ilog2(sizeof(pte_t)), 8);
  1946. } else {
  1947. uasm_i_andi(&p, wr.r3, wr.r2, sizeof(pte_t));
  1948. uasm_i_beqz(&p, wr.r3, 8);
  1949. }
  1950. /* load it in the delay slot*/
  1951. UASM_i_MFC0(&p, wr.r3, C0_ENTRYLO0);
  1952. /* load it if ptr is odd */
  1953. UASM_i_MFC0(&p, wr.r3, C0_ENTRYLO1);
  1954. /*
  1955. * If the entryLo (now in wr.r3) is valid (bit 1), RI or
  1956. * XI must have triggered it.
  1957. */
  1958. if (use_bbit_insns()) {
  1959. uasm_il_bbit0(&p, &r, wr.r3, 1, label_tlbl_goaround2);
  1960. } else {
  1961. uasm_i_andi(&p, wr.r3, wr.r3, 2);
  1962. uasm_il_beqz(&p, &r, wr.r3, label_tlbl_goaround2);
  1963. }
  1964. if (PM_DEFAULT_MASK == 0)
  1965. uasm_i_nop(&p);
  1966. /*
  1967. * We clobbered C0_PAGEMASK, restore it. On the other branch
  1968. * it is restored in build_huge_tlb_write_entry.
  1969. */
  1970. build_restore_pagemask(&p, &r, wr.r3, label_nopage_tlbl, 0);
  1971. uasm_l_tlbl_goaround2(&l, p);
  1972. }
  1973. uasm_i_ori(&p, wr.r1, wr.r1, (_PAGE_ACCESSED | _PAGE_VALID));
  1974. build_huge_handler_tail(&p, &r, &l, wr.r1, wr.r2, 1);
  1975. #endif
  1976. uasm_l_nopage_tlbl(&l, p);
  1977. build_restore_work_registers(&p);
  1978. #ifdef CONFIG_CPU_MICROMIPS
  1979. if ((unsigned long)tlb_do_page_fault_0 & 1) {
  1980. uasm_i_lui(&p, K0, uasm_rel_hi((long)tlb_do_page_fault_0));
  1981. uasm_i_addiu(&p, K0, K0, uasm_rel_lo((long)tlb_do_page_fault_0));
  1982. uasm_i_jr(&p, K0);
  1983. } else
  1984. #endif
  1985. uasm_i_j(&p, (unsigned long)tlb_do_page_fault_0 & 0x0fffffff);
  1986. uasm_i_nop(&p);
  1987. if (p >= handle_tlbl_end)
  1988. panic("TLB load handler fastpath space exceeded");
  1989. uasm_resolve_relocs(relocs, labels);
  1990. pr_debug("Wrote TLB load handler fastpath (%u instructions).\n",
  1991. (unsigned int)(p - handle_tlbl));
  1992. dump_handler("r4000_tlb_load", handle_tlbl, handle_tlbl_size);
  1993. }
  1994. static void build_r4000_tlb_store_handler(void)
  1995. {
  1996. u32 *p = (u32 *)msk_isa16_mode((ulong)handle_tlbs);
  1997. const int handle_tlbs_size = handle_tlbs_end - handle_tlbs;
  1998. struct uasm_label *l = labels;
  1999. struct uasm_reloc *r = relocs;
  2000. struct work_registers wr;
  2001. memset(handle_tlbs, 0, handle_tlbs_size * sizeof(handle_tlbs[0]));
  2002. memset(labels, 0, sizeof(labels));
  2003. memset(relocs, 0, sizeof(relocs));
  2004. wr = build_r4000_tlbchange_handler_head(&p, &l, &r);
  2005. build_pte_writable(&p, &r, wr.r1, wr.r2, wr.r3, label_nopage_tlbs);
  2006. if (m4kc_tlbp_war())
  2007. build_tlb_probe_entry(&p);
  2008. build_make_write(&p, &r, wr.r1, wr.r2, wr.r3);
  2009. build_r4000_tlbchange_handler_tail(&p, &l, &r, wr.r1, wr.r2);
  2010. #ifdef CONFIG_MIPS_HUGE_TLB_SUPPORT
  2011. /*
  2012. * This is the entry point when
  2013. * build_r4000_tlbchange_handler_head spots a huge page.
  2014. */
  2015. uasm_l_tlb_huge_update(&l, p);
  2016. iPTE_LW(&p, wr.r1, wr.r2);
  2017. build_pte_writable(&p, &r, wr.r1, wr.r2, wr.r3, label_nopage_tlbs);
  2018. build_tlb_probe_entry(&p);
  2019. uasm_i_ori(&p, wr.r1, wr.r1,
  2020. _PAGE_ACCESSED | _PAGE_MODIFIED | _PAGE_VALID | _PAGE_DIRTY);
  2021. build_huge_handler_tail(&p, &r, &l, wr.r1, wr.r2, 1);
  2022. #endif
  2023. uasm_l_nopage_tlbs(&l, p);
  2024. build_restore_work_registers(&p);
  2025. #ifdef CONFIG_CPU_MICROMIPS
  2026. if ((unsigned long)tlb_do_page_fault_1 & 1) {
  2027. uasm_i_lui(&p, K0, uasm_rel_hi((long)tlb_do_page_fault_1));
  2028. uasm_i_addiu(&p, K0, K0, uasm_rel_lo((long)tlb_do_page_fault_1));
  2029. uasm_i_jr(&p, K0);
  2030. } else
  2031. #endif
  2032. uasm_i_j(&p, (unsigned long)tlb_do_page_fault_1 & 0x0fffffff);
  2033. uasm_i_nop(&p);
  2034. if (p >= handle_tlbs_end)
  2035. panic("TLB store handler fastpath space exceeded");
  2036. uasm_resolve_relocs(relocs, labels);
  2037. pr_debug("Wrote TLB store handler fastpath (%u instructions).\n",
  2038. (unsigned int)(p - handle_tlbs));
  2039. dump_handler("r4000_tlb_store", handle_tlbs, handle_tlbs_size);
  2040. }
  2041. static void build_r4000_tlb_modify_handler(void)
  2042. {
  2043. u32 *p = (u32 *)msk_isa16_mode((ulong)handle_tlbm);
  2044. const int handle_tlbm_size = handle_tlbm_end - handle_tlbm;
  2045. struct uasm_label *l = labels;
  2046. struct uasm_reloc *r = relocs;
  2047. struct work_registers wr;
  2048. memset(handle_tlbm, 0, handle_tlbm_size * sizeof(handle_tlbm[0]));
  2049. memset(labels, 0, sizeof(labels));
  2050. memset(relocs, 0, sizeof(relocs));
  2051. wr = build_r4000_tlbchange_handler_head(&p, &l, &r);
  2052. build_pte_modifiable(&p, &r, wr.r1, wr.r2, wr.r3, label_nopage_tlbm);
  2053. if (m4kc_tlbp_war())
  2054. build_tlb_probe_entry(&p);
  2055. /* Present and writable bits set, set accessed and dirty bits. */
  2056. build_make_write(&p, &r, wr.r1, wr.r2, wr.r3);
  2057. build_r4000_tlbchange_handler_tail(&p, &l, &r, wr.r1, wr.r2);
  2058. #ifdef CONFIG_MIPS_HUGE_TLB_SUPPORT
  2059. /*
  2060. * This is the entry point when
  2061. * build_r4000_tlbchange_handler_head spots a huge page.
  2062. */
  2063. uasm_l_tlb_huge_update(&l, p);
  2064. iPTE_LW(&p, wr.r1, wr.r2);
  2065. build_pte_modifiable(&p, &r, wr.r1, wr.r2, wr.r3, label_nopage_tlbm);
  2066. build_tlb_probe_entry(&p);
  2067. uasm_i_ori(&p, wr.r1, wr.r1,
  2068. _PAGE_ACCESSED | _PAGE_MODIFIED | _PAGE_VALID | _PAGE_DIRTY);
  2069. build_huge_handler_tail(&p, &r, &l, wr.r1, wr.r2, 0);
  2070. #endif
  2071. uasm_l_nopage_tlbm(&l, p);
  2072. build_restore_work_registers(&p);
  2073. #ifdef CONFIG_CPU_MICROMIPS
  2074. if ((unsigned long)tlb_do_page_fault_1 & 1) {
  2075. uasm_i_lui(&p, K0, uasm_rel_hi((long)tlb_do_page_fault_1));
  2076. uasm_i_addiu(&p, K0, K0, uasm_rel_lo((long)tlb_do_page_fault_1));
  2077. uasm_i_jr(&p, K0);
  2078. } else
  2079. #endif
  2080. uasm_i_j(&p, (unsigned long)tlb_do_page_fault_1 & 0x0fffffff);
  2081. uasm_i_nop(&p);
  2082. if (p >= handle_tlbm_end)
  2083. panic("TLB modify handler fastpath space exceeded");
  2084. uasm_resolve_relocs(relocs, labels);
  2085. pr_debug("Wrote TLB modify handler fastpath (%u instructions).\n",
  2086. (unsigned int)(p - handle_tlbm));
  2087. dump_handler("r4000_tlb_modify", handle_tlbm, handle_tlbm_size);
  2088. }
  2089. static void flush_tlb_handlers(void)
  2090. {
  2091. local_flush_icache_range((unsigned long)handle_tlbl,
  2092. (unsigned long)handle_tlbl_end);
  2093. local_flush_icache_range((unsigned long)handle_tlbs,
  2094. (unsigned long)handle_tlbs_end);
  2095. local_flush_icache_range((unsigned long)handle_tlbm,
  2096. (unsigned long)handle_tlbm_end);
  2097. local_flush_icache_range((unsigned long)tlbmiss_handler_setup_pgd,
  2098. (unsigned long)tlbmiss_handler_setup_pgd_end);
  2099. }
  2100. static void print_htw_config(void)
  2101. {
  2102. unsigned long config;
  2103. unsigned int pwctl;
  2104. const int field = 2 * sizeof(unsigned long);
  2105. config = read_c0_pwfield();
  2106. pr_debug("PWField (0x%0*lx): GDI: 0x%02lx UDI: 0x%02lx MDI: 0x%02lx PTI: 0x%02lx PTEI: 0x%02lx\n",
  2107. field, config,
  2108. (config & MIPS_PWFIELD_GDI_MASK) >> MIPS_PWFIELD_GDI_SHIFT,
  2109. (config & MIPS_PWFIELD_UDI_MASK) >> MIPS_PWFIELD_UDI_SHIFT,
  2110. (config & MIPS_PWFIELD_MDI_MASK) >> MIPS_PWFIELD_MDI_SHIFT,
  2111. (config & MIPS_PWFIELD_PTI_MASK) >> MIPS_PWFIELD_PTI_SHIFT,
  2112. (config & MIPS_PWFIELD_PTEI_MASK) >> MIPS_PWFIELD_PTEI_SHIFT);
  2113. config = read_c0_pwsize();
  2114. 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",
  2115. field, config,
  2116. (config & MIPS_PWSIZE_PS_MASK) >> MIPS_PWSIZE_PS_SHIFT,
  2117. (config & MIPS_PWSIZE_GDW_MASK) >> MIPS_PWSIZE_GDW_SHIFT,
  2118. (config & MIPS_PWSIZE_UDW_MASK) >> MIPS_PWSIZE_UDW_SHIFT,
  2119. (config & MIPS_PWSIZE_MDW_MASK) >> MIPS_PWSIZE_MDW_SHIFT,
  2120. (config & MIPS_PWSIZE_PTW_MASK) >> MIPS_PWSIZE_PTW_SHIFT,
  2121. (config & MIPS_PWSIZE_PTEW_MASK) >> MIPS_PWSIZE_PTEW_SHIFT);
  2122. pwctl = read_c0_pwctl();
  2123. 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",
  2124. pwctl,
  2125. (pwctl & MIPS_PWCTL_PWEN_MASK) >> MIPS_PWCTL_PWEN_SHIFT,
  2126. (pwctl & MIPS_PWCTL_XK_MASK) >> MIPS_PWCTL_XK_SHIFT,
  2127. (pwctl & MIPS_PWCTL_XS_MASK) >> MIPS_PWCTL_XS_SHIFT,
  2128. (pwctl & MIPS_PWCTL_XU_MASK) >> MIPS_PWCTL_XU_SHIFT,
  2129. (pwctl & MIPS_PWCTL_DPH_MASK) >> MIPS_PWCTL_DPH_SHIFT,
  2130. (pwctl & MIPS_PWCTL_HUGEPG_MASK) >> MIPS_PWCTL_HUGEPG_SHIFT,
  2131. (pwctl & MIPS_PWCTL_PSN_MASK) >> MIPS_PWCTL_PSN_SHIFT);
  2132. }
  2133. static void config_htw_params(void)
  2134. {
  2135. unsigned long pwfield, pwsize, ptei;
  2136. unsigned int config;
  2137. /*
  2138. * We are using 2-level page tables, so we only need to
  2139. * setup GDW and PTW appropriately. UDW and MDW will remain 0.
  2140. * The default value of GDI/UDI/MDI/PTI is 0xc. It is illegal to
  2141. * write values less than 0xc in these fields because the entire
  2142. * write will be dropped. As a result of which, we must preserve
  2143. * the original reset values and overwrite only what we really want.
  2144. */
  2145. pwfield = read_c0_pwfield();
  2146. /* re-initialize the GDI field */
  2147. pwfield &= ~MIPS_PWFIELD_GDI_MASK;
  2148. pwfield |= PGDIR_SHIFT << MIPS_PWFIELD_GDI_SHIFT;
  2149. /* re-initialize the PTI field including the even/odd bit */
  2150. pwfield &= ~MIPS_PWFIELD_PTI_MASK;
  2151. pwfield |= PAGE_SHIFT << MIPS_PWFIELD_PTI_SHIFT;
  2152. if (CONFIG_PGTABLE_LEVELS >= 3) {
  2153. pwfield &= ~MIPS_PWFIELD_MDI_MASK;
  2154. pwfield |= PMD_SHIFT << MIPS_PWFIELD_MDI_SHIFT;
  2155. }
  2156. /* Set the PTEI right shift */
  2157. ptei = _PAGE_GLOBAL_SHIFT << MIPS_PWFIELD_PTEI_SHIFT;
  2158. pwfield |= ptei;
  2159. write_c0_pwfield(pwfield);
  2160. /* Check whether the PTEI value is supported */
  2161. back_to_back_c0_hazard();
  2162. pwfield = read_c0_pwfield();
  2163. if (((pwfield & MIPS_PWFIELD_PTEI_MASK) << MIPS_PWFIELD_PTEI_SHIFT)
  2164. != ptei) {
  2165. pr_warn("Unsupported PTEI field value: 0x%lx. HTW will not be enabled",
  2166. ptei);
  2167. /*
  2168. * Drop option to avoid HTW being enabled via another path
  2169. * (eg htw_reset())
  2170. */
  2171. current_cpu_data.options &= ~MIPS_CPU_HTW;
  2172. return;
  2173. }
  2174. pwsize = ilog2(PTRS_PER_PGD) << MIPS_PWSIZE_GDW_SHIFT;
  2175. pwsize |= ilog2(PTRS_PER_PTE) << MIPS_PWSIZE_PTW_SHIFT;
  2176. if (CONFIG_PGTABLE_LEVELS >= 3)
  2177. pwsize |= ilog2(PTRS_PER_PMD) << MIPS_PWSIZE_MDW_SHIFT;
  2178. /* Set pointer size to size of directory pointers */
  2179. if (IS_ENABLED(CONFIG_64BIT))
  2180. pwsize |= MIPS_PWSIZE_PS_MASK;
  2181. /* PTEs may be multiple pointers long (e.g. with XPA) */
  2182. pwsize |= ((PTE_T_LOG2 - PGD_T_LOG2) << MIPS_PWSIZE_PTEW_SHIFT)
  2183. & MIPS_PWSIZE_PTEW_MASK;
  2184. write_c0_pwsize(pwsize);
  2185. /* Make sure everything is set before we enable the HTW */
  2186. back_to_back_c0_hazard();
  2187. /*
  2188. * Enable HTW (and only for XUSeg on 64-bit), and disable the rest of
  2189. * the pwctl fields.
  2190. */
  2191. config = 1 << MIPS_PWCTL_PWEN_SHIFT;
  2192. if (IS_ENABLED(CONFIG_64BIT))
  2193. config |= MIPS_PWCTL_XU_MASK;
  2194. write_c0_pwctl(config);
  2195. pr_info("Hardware Page Table Walker enabled\n");
  2196. print_htw_config();
  2197. }
  2198. static void config_xpa_params(void)
  2199. {
  2200. #ifdef CONFIG_XPA
  2201. unsigned int pagegrain;
  2202. if (mips_xpa_disabled) {
  2203. pr_info("Extended Physical Addressing (XPA) disabled\n");
  2204. return;
  2205. }
  2206. pagegrain = read_c0_pagegrain();
  2207. write_c0_pagegrain(pagegrain | PG_ELPA);
  2208. back_to_back_c0_hazard();
  2209. pagegrain = read_c0_pagegrain();
  2210. if (pagegrain & PG_ELPA)
  2211. pr_info("Extended Physical Addressing (XPA) enabled\n");
  2212. else
  2213. panic("Extended Physical Addressing (XPA) disabled");
  2214. #endif
  2215. }
  2216. static void check_pabits(void)
  2217. {
  2218. unsigned long entry;
  2219. unsigned pabits, fillbits;
  2220. if (!cpu_has_rixi || !_PAGE_NO_EXEC) {
  2221. /*
  2222. * We'll only be making use of the fact that we can rotate bits
  2223. * into the fill if the CPU supports RIXI, so don't bother
  2224. * probing this for CPUs which don't.
  2225. */
  2226. return;
  2227. }
  2228. write_c0_entrylo0(~0ul);
  2229. back_to_back_c0_hazard();
  2230. entry = read_c0_entrylo0();
  2231. /* clear all non-PFN bits */
  2232. entry &= ~((1 << MIPS_ENTRYLO_PFN_SHIFT) - 1);
  2233. entry &= ~(MIPS_ENTRYLO_RI | MIPS_ENTRYLO_XI);
  2234. /* find a lower bound on PABITS, and upper bound on fill bits */
  2235. pabits = fls_long(entry) + 6;
  2236. fillbits = max_t(int, (int)BITS_PER_LONG - pabits, 0);
  2237. /* minus the RI & XI bits */
  2238. fillbits -= min_t(unsigned, fillbits, 2);
  2239. if (fillbits >= ilog2(_PAGE_NO_EXEC))
  2240. fill_includes_sw_bits = true;
  2241. pr_debug("Entry* registers contain %u fill bits\n", fillbits);
  2242. }
  2243. void build_tlb_refill_handler(void)
  2244. {
  2245. /*
  2246. * The refill handler is generated per-CPU, multi-node systems
  2247. * may have local storage for it. The other handlers are only
  2248. * needed once.
  2249. */
  2250. static int run_once = 0;
  2251. if (IS_ENABLED(CONFIG_XPA) && !cpu_has_rixi)
  2252. panic("Kernels supporting XPA currently require CPUs with RIXI");
  2253. output_pgtable_bits_defines();
  2254. check_pabits();
  2255. #ifdef CONFIG_64BIT
  2256. check_for_high_segbits = current_cpu_data.vmbits > (PGDIR_SHIFT + PGD_ORDER + PAGE_SHIFT - 3);
  2257. #endif
  2258. switch (current_cpu_type()) {
  2259. case CPU_R2000:
  2260. case CPU_R3000:
  2261. case CPU_R3000A:
  2262. case CPU_R3081E:
  2263. case CPU_TX3912:
  2264. case CPU_TX3922:
  2265. case CPU_TX3927:
  2266. #ifndef CONFIG_MIPS_PGD_C0_CONTEXT
  2267. if (cpu_has_local_ebase)
  2268. build_r3000_tlb_refill_handler();
  2269. if (!run_once) {
  2270. if (!cpu_has_local_ebase)
  2271. build_r3000_tlb_refill_handler();
  2272. build_setup_pgd();
  2273. build_r3000_tlb_load_handler();
  2274. build_r3000_tlb_store_handler();
  2275. build_r3000_tlb_modify_handler();
  2276. flush_tlb_handlers();
  2277. run_once++;
  2278. }
  2279. #else
  2280. panic("No R3000 TLB refill handler");
  2281. #endif
  2282. break;
  2283. case CPU_R6000:
  2284. case CPU_R6000A:
  2285. panic("No R6000 TLB refill handler yet");
  2286. break;
  2287. case CPU_R8000:
  2288. panic("No R8000 TLB refill handler yet");
  2289. break;
  2290. default:
  2291. if (cpu_has_ldpte)
  2292. setup_pw();
  2293. if (!run_once) {
  2294. scratch_reg = allocate_kscratch();
  2295. build_setup_pgd();
  2296. build_r4000_tlb_load_handler();
  2297. build_r4000_tlb_store_handler();
  2298. build_r4000_tlb_modify_handler();
  2299. if (cpu_has_ldpte)
  2300. build_loongson3_tlb_refill_handler();
  2301. else if (!cpu_has_local_ebase)
  2302. build_r4000_tlb_refill_handler();
  2303. flush_tlb_handlers();
  2304. run_once++;
  2305. }
  2306. if (cpu_has_local_ebase)
  2307. build_r4000_tlb_refill_handler();
  2308. if (cpu_has_xpa)
  2309. config_xpa_params();
  2310. if (cpu_has_htw)
  2311. config_htw_params();
  2312. }
  2313. }