tlbex.c 65 KB

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