tlbex.c 59 KB

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