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