tlbex.c 71 KB

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