tlbex.c 60 KB

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