hash_utils.c 57 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * PowerPC64 port by Mike Corrigan and Dave Engebretsen
  4. * {mikejc|engebret}@us.ibm.com
  5. *
  6. * Copyright (c) 2000 Mike Corrigan <[email protected]>
  7. *
  8. * SMP scalability work:
  9. * Copyright (C) 2001 Anton Blanchard <[email protected]>, IBM
  10. *
  11. * Module name: htab.c
  12. *
  13. * Description:
  14. * PowerPC Hashed Page Table functions
  15. */
  16. #undef DEBUG
  17. #undef DEBUG_LOW
  18. #define pr_fmt(fmt) "hash-mmu: " fmt
  19. #include <linux/spinlock.h>
  20. #include <linux/errno.h>
  21. #include <linux/sched/mm.h>
  22. #include <linux/proc_fs.h>
  23. #include <linux/stat.h>
  24. #include <linux/sysctl.h>
  25. #include <linux/export.h>
  26. #include <linux/ctype.h>
  27. #include <linux/cache.h>
  28. #include <linux/init.h>
  29. #include <linux/signal.h>
  30. #include <linux/memblock.h>
  31. #include <linux/context_tracking.h>
  32. #include <linux/libfdt.h>
  33. #include <linux/pkeys.h>
  34. #include <linux/hugetlb.h>
  35. #include <linux/cpu.h>
  36. #include <linux/pgtable.h>
  37. #include <linux/debugfs.h>
  38. #include <linux/random.h>
  39. #include <linux/elf-randomize.h>
  40. #include <linux/of_fdt.h>
  41. #include <asm/interrupt.h>
  42. #include <asm/processor.h>
  43. #include <asm/mmu.h>
  44. #include <asm/mmu_context.h>
  45. #include <asm/page.h>
  46. #include <asm/types.h>
  47. #include <linux/uaccess.h>
  48. #include <asm/machdep.h>
  49. #include <asm/io.h>
  50. #include <asm/eeh.h>
  51. #include <asm/tlb.h>
  52. #include <asm/cacheflush.h>
  53. #include <asm/cputable.h>
  54. #include <asm/sections.h>
  55. #include <asm/copro.h>
  56. #include <asm/udbg.h>
  57. #include <asm/code-patching.h>
  58. #include <asm/fadump.h>
  59. #include <asm/firmware.h>
  60. #include <asm/tm.h>
  61. #include <asm/trace.h>
  62. #include <asm/ps3.h>
  63. #include <asm/pte-walk.h>
  64. #include <asm/asm-prototypes.h>
  65. #include <asm/ultravisor.h>
  66. #include <mm/mmu_decl.h>
  67. #include "internal.h"
  68. #ifdef DEBUG
  69. #define DBG(fmt...) udbg_printf(fmt)
  70. #else
  71. #define DBG(fmt...)
  72. #endif
  73. #ifdef DEBUG_LOW
  74. #define DBG_LOW(fmt...) udbg_printf(fmt)
  75. #else
  76. #define DBG_LOW(fmt...)
  77. #endif
  78. #define KB (1024)
  79. #define MB (1024*KB)
  80. #define GB (1024L*MB)
  81. /*
  82. * Note: pte --> Linux PTE
  83. * HPTE --> PowerPC Hashed Page Table Entry
  84. *
  85. * Execution context:
  86. * htab_initialize is called with the MMU off (of course), but
  87. * the kernel has been copied down to zero so it can directly
  88. * reference global data. At this point it is very difficult
  89. * to print debug info.
  90. *
  91. */
  92. static unsigned long _SDR1;
  93. u8 hpte_page_sizes[1 << LP_BITS];
  94. EXPORT_SYMBOL_GPL(hpte_page_sizes);
  95. struct hash_pte *htab_address;
  96. unsigned long htab_size_bytes;
  97. unsigned long htab_hash_mask;
  98. EXPORT_SYMBOL_GPL(htab_hash_mask);
  99. int mmu_linear_psize = MMU_PAGE_4K;
  100. EXPORT_SYMBOL_GPL(mmu_linear_psize);
  101. int mmu_virtual_psize = MMU_PAGE_4K;
  102. int mmu_vmalloc_psize = MMU_PAGE_4K;
  103. EXPORT_SYMBOL_GPL(mmu_vmalloc_psize);
  104. int mmu_io_psize = MMU_PAGE_4K;
  105. int mmu_kernel_ssize = MMU_SEGSIZE_256M;
  106. EXPORT_SYMBOL_GPL(mmu_kernel_ssize);
  107. int mmu_highuser_ssize = MMU_SEGSIZE_256M;
  108. u16 mmu_slb_size = 64;
  109. EXPORT_SYMBOL_GPL(mmu_slb_size);
  110. #ifdef CONFIG_PPC_64K_PAGES
  111. int mmu_ci_restrictions;
  112. #endif
  113. static u8 *linear_map_hash_slots;
  114. static unsigned long linear_map_hash_count;
  115. struct mmu_hash_ops mmu_hash_ops;
  116. EXPORT_SYMBOL(mmu_hash_ops);
  117. /*
  118. * These are definitions of page sizes arrays to be used when none
  119. * is provided by the firmware.
  120. */
  121. /*
  122. * Fallback (4k pages only)
  123. */
  124. static struct mmu_psize_def mmu_psize_defaults[] = {
  125. [MMU_PAGE_4K] = {
  126. .shift = 12,
  127. .sllp = 0,
  128. .penc = {[MMU_PAGE_4K] = 0, [1 ... MMU_PAGE_COUNT - 1] = -1},
  129. .avpnm = 0,
  130. .tlbiel = 0,
  131. },
  132. };
  133. /*
  134. * POWER4, GPUL, POWER5
  135. *
  136. * Support for 16Mb large pages
  137. */
  138. static struct mmu_psize_def mmu_psize_defaults_gp[] = {
  139. [MMU_PAGE_4K] = {
  140. .shift = 12,
  141. .sllp = 0,
  142. .penc = {[MMU_PAGE_4K] = 0, [1 ... MMU_PAGE_COUNT - 1] = -1},
  143. .avpnm = 0,
  144. .tlbiel = 1,
  145. },
  146. [MMU_PAGE_16M] = {
  147. .shift = 24,
  148. .sllp = SLB_VSID_L,
  149. .penc = {[0 ... MMU_PAGE_16M - 1] = -1, [MMU_PAGE_16M] = 0,
  150. [MMU_PAGE_16M + 1 ... MMU_PAGE_COUNT - 1] = -1 },
  151. .avpnm = 0x1UL,
  152. .tlbiel = 0,
  153. },
  154. };
  155. static inline void tlbiel_hash_set_isa206(unsigned int set, unsigned int is)
  156. {
  157. unsigned long rb;
  158. rb = (set << PPC_BITLSHIFT(51)) | (is << PPC_BITLSHIFT(53));
  159. asm volatile("tlbiel %0" : : "r" (rb));
  160. }
  161. /*
  162. * tlbiel instruction for hash, set invalidation
  163. * i.e., r=1 and is=01 or is=10 or is=11
  164. */
  165. static __always_inline void tlbiel_hash_set_isa300(unsigned int set, unsigned int is,
  166. unsigned int pid,
  167. unsigned int ric, unsigned int prs)
  168. {
  169. unsigned long rb;
  170. unsigned long rs;
  171. unsigned int r = 0; /* hash format */
  172. rb = (set << PPC_BITLSHIFT(51)) | (is << PPC_BITLSHIFT(53));
  173. rs = ((unsigned long)pid << PPC_BITLSHIFT(31));
  174. asm volatile(PPC_TLBIEL(%0, %1, %2, %3, %4)
  175. : : "r"(rb), "r"(rs), "i"(ric), "i"(prs), "i"(r)
  176. : "memory");
  177. }
  178. static void tlbiel_all_isa206(unsigned int num_sets, unsigned int is)
  179. {
  180. unsigned int set;
  181. asm volatile("ptesync": : :"memory");
  182. for (set = 0; set < num_sets; set++)
  183. tlbiel_hash_set_isa206(set, is);
  184. ppc_after_tlbiel_barrier();
  185. }
  186. static void tlbiel_all_isa300(unsigned int num_sets, unsigned int is)
  187. {
  188. unsigned int set;
  189. asm volatile("ptesync": : :"memory");
  190. /*
  191. * Flush the partition table cache if this is HV mode.
  192. */
  193. if (early_cpu_has_feature(CPU_FTR_HVMODE))
  194. tlbiel_hash_set_isa300(0, is, 0, 2, 0);
  195. /*
  196. * Now invalidate the process table cache. UPRT=0 HPT modes (what
  197. * current hardware implements) do not use the process table, but
  198. * add the flushes anyway.
  199. *
  200. * From ISA v3.0B p. 1078:
  201. * The following forms are invalid.
  202. * * PRS=1, R=0, and RIC!=2 (The only process-scoped
  203. * HPT caching is of the Process Table.)
  204. */
  205. tlbiel_hash_set_isa300(0, is, 0, 2, 1);
  206. /*
  207. * Then flush the sets of the TLB proper. Hash mode uses
  208. * partition scoped TLB translations, which may be flushed
  209. * in !HV mode.
  210. */
  211. for (set = 0; set < num_sets; set++)
  212. tlbiel_hash_set_isa300(set, is, 0, 0, 0);
  213. ppc_after_tlbiel_barrier();
  214. asm volatile(PPC_ISA_3_0_INVALIDATE_ERAT "; isync" : : :"memory");
  215. }
  216. void hash__tlbiel_all(unsigned int action)
  217. {
  218. unsigned int is;
  219. switch (action) {
  220. case TLB_INVAL_SCOPE_GLOBAL:
  221. is = 3;
  222. break;
  223. case TLB_INVAL_SCOPE_LPID:
  224. is = 2;
  225. break;
  226. default:
  227. BUG();
  228. }
  229. if (early_cpu_has_feature(CPU_FTR_ARCH_300))
  230. tlbiel_all_isa300(POWER9_TLB_SETS_HASH, is);
  231. else if (early_cpu_has_feature(CPU_FTR_ARCH_207S))
  232. tlbiel_all_isa206(POWER8_TLB_SETS, is);
  233. else if (early_cpu_has_feature(CPU_FTR_ARCH_206))
  234. tlbiel_all_isa206(POWER7_TLB_SETS, is);
  235. else
  236. WARN(1, "%s called on pre-POWER7 CPU\n", __func__);
  237. }
  238. /*
  239. * 'R' and 'C' update notes:
  240. * - Under pHyp or KVM, the updatepp path will not set C, thus it *will*
  241. * create writeable HPTEs without C set, because the hcall H_PROTECT
  242. * that we use in that case will not update C
  243. * - The above is however not a problem, because we also don't do that
  244. * fancy "no flush" variant of eviction and we use H_REMOVE which will
  245. * do the right thing and thus we don't have the race I described earlier
  246. *
  247. * - Under bare metal, we do have the race, so we need R and C set
  248. * - We make sure R is always set and never lost
  249. * - C is _PAGE_DIRTY, and *should* always be set for a writeable mapping
  250. */
  251. unsigned long htab_convert_pte_flags(unsigned long pteflags, unsigned long flags)
  252. {
  253. unsigned long rflags = 0;
  254. /* _PAGE_EXEC -> NOEXEC */
  255. if ((pteflags & _PAGE_EXEC) == 0)
  256. rflags |= HPTE_R_N;
  257. /*
  258. * PPP bits:
  259. * Linux uses slb key 0 for kernel and 1 for user.
  260. * kernel RW areas are mapped with PPP=0b000
  261. * User area is mapped with PPP=0b010 for read/write
  262. * or PPP=0b011 for read-only (including writeable but clean pages).
  263. */
  264. if (pteflags & _PAGE_PRIVILEGED) {
  265. /*
  266. * Kernel read only mapped with ppp bits 0b110
  267. */
  268. if (!(pteflags & _PAGE_WRITE)) {
  269. if (mmu_has_feature(MMU_FTR_KERNEL_RO))
  270. rflags |= (HPTE_R_PP0 | 0x2);
  271. else
  272. rflags |= 0x3;
  273. }
  274. } else {
  275. if (pteflags & _PAGE_RWX)
  276. rflags |= 0x2;
  277. if (!((pteflags & _PAGE_WRITE) && (pteflags & _PAGE_DIRTY)))
  278. rflags |= 0x1;
  279. }
  280. /*
  281. * We can't allow hardware to update hpte bits. Hence always
  282. * set 'R' bit and set 'C' if it is a write fault
  283. */
  284. rflags |= HPTE_R_R;
  285. if (pteflags & _PAGE_DIRTY)
  286. rflags |= HPTE_R_C;
  287. /*
  288. * Add in WIG bits
  289. */
  290. if ((pteflags & _PAGE_CACHE_CTL) == _PAGE_TOLERANT)
  291. rflags |= HPTE_R_I;
  292. else if ((pteflags & _PAGE_CACHE_CTL) == _PAGE_NON_IDEMPOTENT)
  293. rflags |= (HPTE_R_I | HPTE_R_G);
  294. else if ((pteflags & _PAGE_CACHE_CTL) == _PAGE_SAO)
  295. rflags |= (HPTE_R_W | HPTE_R_I | HPTE_R_M);
  296. else
  297. /*
  298. * Add memory coherence if cache inhibited is not set
  299. */
  300. rflags |= HPTE_R_M;
  301. rflags |= pte_to_hpte_pkey_bits(pteflags, flags);
  302. return rflags;
  303. }
  304. int htab_bolt_mapping(unsigned long vstart, unsigned long vend,
  305. unsigned long pstart, unsigned long prot,
  306. int psize, int ssize)
  307. {
  308. unsigned long vaddr, paddr;
  309. unsigned int step, shift;
  310. int ret = 0;
  311. shift = mmu_psize_defs[psize].shift;
  312. step = 1 << shift;
  313. prot = htab_convert_pte_flags(prot, HPTE_USE_KERNEL_KEY);
  314. DBG("htab_bolt_mapping(%lx..%lx -> %lx (%lx,%d,%d)\n",
  315. vstart, vend, pstart, prot, psize, ssize);
  316. /* Carefully map only the possible range */
  317. vaddr = ALIGN(vstart, step);
  318. paddr = ALIGN(pstart, step);
  319. vend = ALIGN_DOWN(vend, step);
  320. for (; vaddr < vend; vaddr += step, paddr += step) {
  321. unsigned long hash, hpteg;
  322. unsigned long vsid = get_kernel_vsid(vaddr, ssize);
  323. unsigned long vpn = hpt_vpn(vaddr, vsid, ssize);
  324. unsigned long tprot = prot;
  325. bool secondary_hash = false;
  326. /*
  327. * If we hit a bad address return error.
  328. */
  329. if (!vsid)
  330. return -1;
  331. /* Make kernel text executable */
  332. if (overlaps_kernel_text(vaddr, vaddr + step))
  333. tprot &= ~HPTE_R_N;
  334. /*
  335. * If relocatable, check if it overlaps interrupt vectors that
  336. * are copied down to real 0. For relocatable kernel
  337. * (e.g. kdump case) we copy interrupt vectors down to real
  338. * address 0. Mark that region as executable. This is
  339. * because on p8 system with relocation on exception feature
  340. * enabled, exceptions are raised with MMU (IR=DR=1) ON. Hence
  341. * in order to execute the interrupt handlers in virtual
  342. * mode the vector region need to be marked as executable.
  343. */
  344. if ((PHYSICAL_START > MEMORY_START) &&
  345. overlaps_interrupt_vector_text(vaddr, vaddr + step))
  346. tprot &= ~HPTE_R_N;
  347. hash = hpt_hash(vpn, shift, ssize);
  348. hpteg = ((hash & htab_hash_mask) * HPTES_PER_GROUP);
  349. BUG_ON(!mmu_hash_ops.hpte_insert);
  350. repeat:
  351. ret = mmu_hash_ops.hpte_insert(hpteg, vpn, paddr, tprot,
  352. HPTE_V_BOLTED, psize, psize,
  353. ssize);
  354. if (ret == -1) {
  355. /*
  356. * Try to keep bolted entries in primary.
  357. * Remove non bolted entries and try insert again
  358. */
  359. ret = mmu_hash_ops.hpte_remove(hpteg);
  360. if (ret != -1)
  361. ret = mmu_hash_ops.hpte_insert(hpteg, vpn, paddr, tprot,
  362. HPTE_V_BOLTED, psize, psize,
  363. ssize);
  364. if (ret == -1 && !secondary_hash) {
  365. secondary_hash = true;
  366. hpteg = ((~hash & htab_hash_mask) * HPTES_PER_GROUP);
  367. goto repeat;
  368. }
  369. }
  370. if (ret < 0)
  371. break;
  372. cond_resched();
  373. if (debug_pagealloc_enabled_or_kfence() &&
  374. (paddr >> PAGE_SHIFT) < linear_map_hash_count)
  375. linear_map_hash_slots[paddr >> PAGE_SHIFT] = ret | 0x80;
  376. }
  377. return ret < 0 ? ret : 0;
  378. }
  379. int htab_remove_mapping(unsigned long vstart, unsigned long vend,
  380. int psize, int ssize)
  381. {
  382. unsigned long vaddr, time_limit;
  383. unsigned int step, shift;
  384. int rc;
  385. int ret = 0;
  386. shift = mmu_psize_defs[psize].shift;
  387. step = 1 << shift;
  388. if (!mmu_hash_ops.hpte_removebolted)
  389. return -ENODEV;
  390. /* Unmap the full range specificied */
  391. vaddr = ALIGN_DOWN(vstart, step);
  392. time_limit = jiffies + HZ;
  393. for (;vaddr < vend; vaddr += step) {
  394. rc = mmu_hash_ops.hpte_removebolted(vaddr, psize, ssize);
  395. /*
  396. * For large number of mappings introduce a cond_resched()
  397. * to prevent softlockup warnings.
  398. */
  399. if (time_after(jiffies, time_limit)) {
  400. cond_resched();
  401. time_limit = jiffies + HZ;
  402. }
  403. if (rc == -ENOENT) {
  404. ret = -ENOENT;
  405. continue;
  406. }
  407. if (rc < 0)
  408. return rc;
  409. }
  410. return ret;
  411. }
  412. static bool disable_1tb_segments = false;
  413. static int __init parse_disable_1tb_segments(char *p)
  414. {
  415. disable_1tb_segments = true;
  416. return 0;
  417. }
  418. early_param("disable_1tb_segments", parse_disable_1tb_segments);
  419. static int __init htab_dt_scan_seg_sizes(unsigned long node,
  420. const char *uname, int depth,
  421. void *data)
  422. {
  423. const char *type = of_get_flat_dt_prop(node, "device_type", NULL);
  424. const __be32 *prop;
  425. int size = 0;
  426. /* We are scanning "cpu" nodes only */
  427. if (type == NULL || strcmp(type, "cpu") != 0)
  428. return 0;
  429. prop = of_get_flat_dt_prop(node, "ibm,processor-segment-sizes", &size);
  430. if (prop == NULL)
  431. return 0;
  432. for (; size >= 4; size -= 4, ++prop) {
  433. if (be32_to_cpu(prop[0]) == 40) {
  434. DBG("1T segment support detected\n");
  435. if (disable_1tb_segments) {
  436. DBG("1T segments disabled by command line\n");
  437. break;
  438. }
  439. cur_cpu_spec->mmu_features |= MMU_FTR_1T_SEGMENT;
  440. return 1;
  441. }
  442. }
  443. cur_cpu_spec->mmu_features &= ~MMU_FTR_NO_SLBIE_B;
  444. return 0;
  445. }
  446. static int __init get_idx_from_shift(unsigned int shift)
  447. {
  448. int idx = -1;
  449. switch (shift) {
  450. case 0xc:
  451. idx = MMU_PAGE_4K;
  452. break;
  453. case 0x10:
  454. idx = MMU_PAGE_64K;
  455. break;
  456. case 0x14:
  457. idx = MMU_PAGE_1M;
  458. break;
  459. case 0x18:
  460. idx = MMU_PAGE_16M;
  461. break;
  462. case 0x22:
  463. idx = MMU_PAGE_16G;
  464. break;
  465. }
  466. return idx;
  467. }
  468. static int __init htab_dt_scan_page_sizes(unsigned long node,
  469. const char *uname, int depth,
  470. void *data)
  471. {
  472. const char *type = of_get_flat_dt_prop(node, "device_type", NULL);
  473. const __be32 *prop;
  474. int size = 0;
  475. /* We are scanning "cpu" nodes only */
  476. if (type == NULL || strcmp(type, "cpu") != 0)
  477. return 0;
  478. prop = of_get_flat_dt_prop(node, "ibm,segment-page-sizes", &size);
  479. if (!prop)
  480. return 0;
  481. pr_info("Page sizes from device-tree:\n");
  482. size /= 4;
  483. cur_cpu_spec->mmu_features &= ~(MMU_FTR_16M_PAGE);
  484. while(size > 0) {
  485. unsigned int base_shift = be32_to_cpu(prop[0]);
  486. unsigned int slbenc = be32_to_cpu(prop[1]);
  487. unsigned int lpnum = be32_to_cpu(prop[2]);
  488. struct mmu_psize_def *def;
  489. int idx, base_idx;
  490. size -= 3; prop += 3;
  491. base_idx = get_idx_from_shift(base_shift);
  492. if (base_idx < 0) {
  493. /* skip the pte encoding also */
  494. prop += lpnum * 2; size -= lpnum * 2;
  495. continue;
  496. }
  497. def = &mmu_psize_defs[base_idx];
  498. if (base_idx == MMU_PAGE_16M)
  499. cur_cpu_spec->mmu_features |= MMU_FTR_16M_PAGE;
  500. def->shift = base_shift;
  501. if (base_shift <= 23)
  502. def->avpnm = 0;
  503. else
  504. def->avpnm = (1 << (base_shift - 23)) - 1;
  505. def->sllp = slbenc;
  506. /*
  507. * We don't know for sure what's up with tlbiel, so
  508. * for now we only set it for 4K and 64K pages
  509. */
  510. if (base_idx == MMU_PAGE_4K || base_idx == MMU_PAGE_64K)
  511. def->tlbiel = 1;
  512. else
  513. def->tlbiel = 0;
  514. while (size > 0 && lpnum) {
  515. unsigned int shift = be32_to_cpu(prop[0]);
  516. int penc = be32_to_cpu(prop[1]);
  517. prop += 2; size -= 2;
  518. lpnum--;
  519. idx = get_idx_from_shift(shift);
  520. if (idx < 0)
  521. continue;
  522. if (penc == -1)
  523. pr_err("Invalid penc for base_shift=%d "
  524. "shift=%d\n", base_shift, shift);
  525. def->penc[idx] = penc;
  526. pr_info("base_shift=%d: shift=%d, sllp=0x%04lx,"
  527. " avpnm=0x%08lx, tlbiel=%d, penc=%d\n",
  528. base_shift, shift, def->sllp,
  529. def->avpnm, def->tlbiel, def->penc[idx]);
  530. }
  531. }
  532. return 1;
  533. }
  534. #ifdef CONFIG_HUGETLB_PAGE
  535. /*
  536. * Scan for 16G memory blocks that have been set aside for huge pages
  537. * and reserve those blocks for 16G huge pages.
  538. */
  539. static int __init htab_dt_scan_hugepage_blocks(unsigned long node,
  540. const char *uname, int depth,
  541. void *data) {
  542. const char *type = of_get_flat_dt_prop(node, "device_type", NULL);
  543. const __be64 *addr_prop;
  544. const __be32 *page_count_prop;
  545. unsigned int expected_pages;
  546. long unsigned int phys_addr;
  547. long unsigned int block_size;
  548. /* We are scanning "memory" nodes only */
  549. if (type == NULL || strcmp(type, "memory") != 0)
  550. return 0;
  551. /*
  552. * This property is the log base 2 of the number of virtual pages that
  553. * will represent this memory block.
  554. */
  555. page_count_prop = of_get_flat_dt_prop(node, "ibm,expected#pages", NULL);
  556. if (page_count_prop == NULL)
  557. return 0;
  558. expected_pages = (1 << be32_to_cpu(page_count_prop[0]));
  559. addr_prop = of_get_flat_dt_prop(node, "reg", NULL);
  560. if (addr_prop == NULL)
  561. return 0;
  562. phys_addr = be64_to_cpu(addr_prop[0]);
  563. block_size = be64_to_cpu(addr_prop[1]);
  564. if (block_size != (16 * GB))
  565. return 0;
  566. printk(KERN_INFO "Huge page(16GB) memory: "
  567. "addr = 0x%lX size = 0x%lX pages = %d\n",
  568. phys_addr, block_size, expected_pages);
  569. if (phys_addr + block_size * expected_pages <= memblock_end_of_DRAM()) {
  570. memblock_reserve(phys_addr, block_size * expected_pages);
  571. pseries_add_gpage(phys_addr, block_size, expected_pages);
  572. }
  573. return 0;
  574. }
  575. #endif /* CONFIG_HUGETLB_PAGE */
  576. static void __init mmu_psize_set_default_penc(void)
  577. {
  578. int bpsize, apsize;
  579. for (bpsize = 0; bpsize < MMU_PAGE_COUNT; bpsize++)
  580. for (apsize = 0; apsize < MMU_PAGE_COUNT; apsize++)
  581. mmu_psize_defs[bpsize].penc[apsize] = -1;
  582. }
  583. #ifdef CONFIG_PPC_64K_PAGES
  584. static bool __init might_have_hea(void)
  585. {
  586. /*
  587. * The HEA ethernet adapter requires awareness of the
  588. * GX bus. Without that awareness we can easily assume
  589. * we will never see an HEA ethernet device.
  590. */
  591. #ifdef CONFIG_IBMEBUS
  592. return !cpu_has_feature(CPU_FTR_ARCH_207S) &&
  593. firmware_has_feature(FW_FEATURE_SPLPAR);
  594. #else
  595. return false;
  596. #endif
  597. }
  598. #endif /* #ifdef CONFIG_PPC_64K_PAGES */
  599. static void __init htab_scan_page_sizes(void)
  600. {
  601. int rc;
  602. /* se the invalid penc to -1 */
  603. mmu_psize_set_default_penc();
  604. /* Default to 4K pages only */
  605. memcpy(mmu_psize_defs, mmu_psize_defaults,
  606. sizeof(mmu_psize_defaults));
  607. /*
  608. * Try to find the available page sizes in the device-tree
  609. */
  610. rc = of_scan_flat_dt(htab_dt_scan_page_sizes, NULL);
  611. if (rc == 0 && early_mmu_has_feature(MMU_FTR_16M_PAGE)) {
  612. /*
  613. * Nothing in the device-tree, but the CPU supports 16M pages,
  614. * so let's fallback on a known size list for 16M capable CPUs.
  615. */
  616. memcpy(mmu_psize_defs, mmu_psize_defaults_gp,
  617. sizeof(mmu_psize_defaults_gp));
  618. }
  619. #ifdef CONFIG_HUGETLB_PAGE
  620. if (!hugetlb_disabled && !early_radix_enabled() ) {
  621. /* Reserve 16G huge page memory sections for huge pages */
  622. of_scan_flat_dt(htab_dt_scan_hugepage_blocks, NULL);
  623. }
  624. #endif /* CONFIG_HUGETLB_PAGE */
  625. }
  626. /*
  627. * Fill in the hpte_page_sizes[] array.
  628. * We go through the mmu_psize_defs[] array looking for all the
  629. * supported base/actual page size combinations. Each combination
  630. * has a unique pagesize encoding (penc) value in the low bits of
  631. * the LP field of the HPTE. For actual page sizes less than 1MB,
  632. * some of the upper LP bits are used for RPN bits, meaning that
  633. * we need to fill in several entries in hpte_page_sizes[].
  634. *
  635. * In diagrammatic form, with r = RPN bits and z = page size bits:
  636. * PTE LP actual page size
  637. * rrrr rrrz >=8KB
  638. * rrrr rrzz >=16KB
  639. * rrrr rzzz >=32KB
  640. * rrrr zzzz >=64KB
  641. * ...
  642. *
  643. * The zzzz bits are implementation-specific but are chosen so that
  644. * no encoding for a larger page size uses the same value in its
  645. * low-order N bits as the encoding for the 2^(12+N) byte page size
  646. * (if it exists).
  647. */
  648. static void __init init_hpte_page_sizes(void)
  649. {
  650. long int ap, bp;
  651. long int shift, penc;
  652. for (bp = 0; bp < MMU_PAGE_COUNT; ++bp) {
  653. if (!mmu_psize_defs[bp].shift)
  654. continue; /* not a supported page size */
  655. for (ap = bp; ap < MMU_PAGE_COUNT; ++ap) {
  656. penc = mmu_psize_defs[bp].penc[ap];
  657. if (penc == -1 || !mmu_psize_defs[ap].shift)
  658. continue;
  659. shift = mmu_psize_defs[ap].shift - LP_SHIFT;
  660. if (shift <= 0)
  661. continue; /* should never happen */
  662. /*
  663. * For page sizes less than 1MB, this loop
  664. * replicates the entry for all possible values
  665. * of the rrrr bits.
  666. */
  667. while (penc < (1 << LP_BITS)) {
  668. hpte_page_sizes[penc] = (ap << 4) | bp;
  669. penc += 1 << shift;
  670. }
  671. }
  672. }
  673. }
  674. static void __init htab_init_page_sizes(void)
  675. {
  676. bool aligned = true;
  677. init_hpte_page_sizes();
  678. if (!debug_pagealloc_enabled_or_kfence()) {
  679. /*
  680. * Pick a size for the linear mapping. Currently, we only
  681. * support 16M, 1M and 4K which is the default
  682. */
  683. if (IS_ENABLED(CONFIG_STRICT_KERNEL_RWX) &&
  684. (unsigned long)_stext % 0x1000000) {
  685. if (mmu_psize_defs[MMU_PAGE_16M].shift)
  686. pr_warn("Kernel not 16M aligned, disabling 16M linear map alignment\n");
  687. aligned = false;
  688. }
  689. if (mmu_psize_defs[MMU_PAGE_16M].shift && aligned)
  690. mmu_linear_psize = MMU_PAGE_16M;
  691. else if (mmu_psize_defs[MMU_PAGE_1M].shift)
  692. mmu_linear_psize = MMU_PAGE_1M;
  693. }
  694. #ifdef CONFIG_PPC_64K_PAGES
  695. /*
  696. * Pick a size for the ordinary pages. Default is 4K, we support
  697. * 64K for user mappings and vmalloc if supported by the processor.
  698. * We only use 64k for ioremap if the processor
  699. * (and firmware) support cache-inhibited large pages.
  700. * If not, we use 4k and set mmu_ci_restrictions so that
  701. * hash_page knows to switch processes that use cache-inhibited
  702. * mappings to 4k pages.
  703. */
  704. if (mmu_psize_defs[MMU_PAGE_64K].shift) {
  705. mmu_virtual_psize = MMU_PAGE_64K;
  706. mmu_vmalloc_psize = MMU_PAGE_64K;
  707. if (mmu_linear_psize == MMU_PAGE_4K)
  708. mmu_linear_psize = MMU_PAGE_64K;
  709. if (mmu_has_feature(MMU_FTR_CI_LARGE_PAGE)) {
  710. /*
  711. * When running on pSeries using 64k pages for ioremap
  712. * would stop us accessing the HEA ethernet. So if we
  713. * have the chance of ever seeing one, stay at 4k.
  714. */
  715. if (!might_have_hea())
  716. mmu_io_psize = MMU_PAGE_64K;
  717. } else
  718. mmu_ci_restrictions = 1;
  719. }
  720. #endif /* CONFIG_PPC_64K_PAGES */
  721. #ifdef CONFIG_SPARSEMEM_VMEMMAP
  722. /*
  723. * We try to use 16M pages for vmemmap if that is supported
  724. * and we have at least 1G of RAM at boot
  725. */
  726. if (mmu_psize_defs[MMU_PAGE_16M].shift &&
  727. memblock_phys_mem_size() >= 0x40000000)
  728. mmu_vmemmap_psize = MMU_PAGE_16M;
  729. else
  730. mmu_vmemmap_psize = mmu_virtual_psize;
  731. #endif /* CONFIG_SPARSEMEM_VMEMMAP */
  732. printk(KERN_DEBUG "Page orders: linear mapping = %d, "
  733. "virtual = %d, io = %d"
  734. #ifdef CONFIG_SPARSEMEM_VMEMMAP
  735. ", vmemmap = %d"
  736. #endif
  737. "\n",
  738. mmu_psize_defs[mmu_linear_psize].shift,
  739. mmu_psize_defs[mmu_virtual_psize].shift,
  740. mmu_psize_defs[mmu_io_psize].shift
  741. #ifdef CONFIG_SPARSEMEM_VMEMMAP
  742. ,mmu_psize_defs[mmu_vmemmap_psize].shift
  743. #endif
  744. );
  745. }
  746. static int __init htab_dt_scan_pftsize(unsigned long node,
  747. const char *uname, int depth,
  748. void *data)
  749. {
  750. const char *type = of_get_flat_dt_prop(node, "device_type", NULL);
  751. const __be32 *prop;
  752. /* We are scanning "cpu" nodes only */
  753. if (type == NULL || strcmp(type, "cpu") != 0)
  754. return 0;
  755. prop = of_get_flat_dt_prop(node, "ibm,pft-size", NULL);
  756. if (prop != NULL) {
  757. /* pft_size[0] is the NUMA CEC cookie */
  758. ppc64_pft_size = be32_to_cpu(prop[1]);
  759. return 1;
  760. }
  761. return 0;
  762. }
  763. unsigned htab_shift_for_mem_size(unsigned long mem_size)
  764. {
  765. unsigned memshift = __ilog2(mem_size);
  766. unsigned pshift = mmu_psize_defs[mmu_virtual_psize].shift;
  767. unsigned pteg_shift;
  768. /* round mem_size up to next power of 2 */
  769. if ((1UL << memshift) < mem_size)
  770. memshift += 1;
  771. /* aim for 2 pages / pteg */
  772. pteg_shift = memshift - (pshift + 1);
  773. /*
  774. * 2^11 PTEGS of 128 bytes each, ie. 2^18 bytes is the minimum htab
  775. * size permitted by the architecture.
  776. */
  777. return max(pteg_shift + 7, 18U);
  778. }
  779. static unsigned long __init htab_get_table_size(void)
  780. {
  781. /*
  782. * If hash size isn't already provided by the platform, we try to
  783. * retrieve it from the device-tree. If it's not there neither, we
  784. * calculate it now based on the total RAM size
  785. */
  786. if (ppc64_pft_size == 0)
  787. of_scan_flat_dt(htab_dt_scan_pftsize, NULL);
  788. if (ppc64_pft_size)
  789. return 1UL << ppc64_pft_size;
  790. return 1UL << htab_shift_for_mem_size(memblock_phys_mem_size());
  791. }
  792. #ifdef CONFIG_MEMORY_HOTPLUG
  793. static int resize_hpt_for_hotplug(unsigned long new_mem_size)
  794. {
  795. unsigned target_hpt_shift;
  796. if (!mmu_hash_ops.resize_hpt)
  797. return 0;
  798. target_hpt_shift = htab_shift_for_mem_size(new_mem_size);
  799. /*
  800. * To avoid lots of HPT resizes if memory size is fluctuating
  801. * across a boundary, we deliberately have some hysterisis
  802. * here: we immediately increase the HPT size if the target
  803. * shift exceeds the current shift, but we won't attempt to
  804. * reduce unless the target shift is at least 2 below the
  805. * current shift
  806. */
  807. if (target_hpt_shift > ppc64_pft_size ||
  808. target_hpt_shift < ppc64_pft_size - 1)
  809. return mmu_hash_ops.resize_hpt(target_hpt_shift);
  810. return 0;
  811. }
  812. int hash__create_section_mapping(unsigned long start, unsigned long end,
  813. int nid, pgprot_t prot)
  814. {
  815. int rc;
  816. if (end >= H_VMALLOC_START) {
  817. pr_warn("Outside the supported range\n");
  818. return -1;
  819. }
  820. resize_hpt_for_hotplug(memblock_phys_mem_size());
  821. rc = htab_bolt_mapping(start, end, __pa(start),
  822. pgprot_val(prot), mmu_linear_psize,
  823. mmu_kernel_ssize);
  824. if (rc < 0) {
  825. int rc2 = htab_remove_mapping(start, end, mmu_linear_psize,
  826. mmu_kernel_ssize);
  827. BUG_ON(rc2 && (rc2 != -ENOENT));
  828. }
  829. return rc;
  830. }
  831. int hash__remove_section_mapping(unsigned long start, unsigned long end)
  832. {
  833. int rc = htab_remove_mapping(start, end, mmu_linear_psize,
  834. mmu_kernel_ssize);
  835. if (resize_hpt_for_hotplug(memblock_phys_mem_size()) == -ENOSPC)
  836. pr_warn("Hash collision while resizing HPT\n");
  837. return rc;
  838. }
  839. #endif /* CONFIG_MEMORY_HOTPLUG */
  840. static void __init hash_init_partition_table(phys_addr_t hash_table,
  841. unsigned long htab_size)
  842. {
  843. mmu_partition_table_init();
  844. /*
  845. * PS field (VRMA page size) is not used for LPID 0, hence set to 0.
  846. * For now, UPRT is 0 and we have no segment table.
  847. */
  848. htab_size = __ilog2(htab_size) - 18;
  849. mmu_partition_table_set_entry(0, hash_table | htab_size, 0, false);
  850. pr_info("Partition table %p\n", partition_tb);
  851. }
  852. static void __init htab_initialize(void)
  853. {
  854. unsigned long table;
  855. unsigned long pteg_count;
  856. unsigned long prot;
  857. phys_addr_t base = 0, size = 0, end;
  858. u64 i;
  859. DBG(" -> htab_initialize()\n");
  860. if (mmu_has_feature(MMU_FTR_1T_SEGMENT)) {
  861. mmu_kernel_ssize = MMU_SEGSIZE_1T;
  862. mmu_highuser_ssize = MMU_SEGSIZE_1T;
  863. printk(KERN_INFO "Using 1TB segments\n");
  864. }
  865. if (stress_slb_enabled)
  866. static_branch_enable(&stress_slb_key);
  867. /*
  868. * Calculate the required size of the htab. We want the number of
  869. * PTEGs to equal one half the number of real pages.
  870. */
  871. htab_size_bytes = htab_get_table_size();
  872. pteg_count = htab_size_bytes >> 7;
  873. htab_hash_mask = pteg_count - 1;
  874. if (firmware_has_feature(FW_FEATURE_LPAR) ||
  875. firmware_has_feature(FW_FEATURE_PS3_LV1)) {
  876. /* Using a hypervisor which owns the htab */
  877. htab_address = NULL;
  878. _SDR1 = 0;
  879. #ifdef CONFIG_FA_DUMP
  880. /*
  881. * If firmware assisted dump is active firmware preserves
  882. * the contents of htab along with entire partition memory.
  883. * Clear the htab if firmware assisted dump is active so
  884. * that we dont end up using old mappings.
  885. */
  886. if (is_fadump_active() && mmu_hash_ops.hpte_clear_all)
  887. mmu_hash_ops.hpte_clear_all();
  888. #endif
  889. } else {
  890. unsigned long limit = MEMBLOCK_ALLOC_ANYWHERE;
  891. #ifdef CONFIG_PPC_CELL
  892. /*
  893. * Cell may require the hash table down low when using the
  894. * Axon IOMMU in order to fit the dynamic region over it, see
  895. * comments in cell/iommu.c
  896. */
  897. if (fdt_subnode_offset(initial_boot_params, 0, "axon") > 0) {
  898. limit = 0x80000000;
  899. pr_info("Hash table forced below 2G for Axon IOMMU\n");
  900. }
  901. #endif /* CONFIG_PPC_CELL */
  902. table = memblock_phys_alloc_range(htab_size_bytes,
  903. htab_size_bytes,
  904. 0, limit);
  905. if (!table)
  906. panic("ERROR: Failed to allocate %pa bytes below %pa\n",
  907. &htab_size_bytes, &limit);
  908. DBG("Hash table allocated at %lx, size: %lx\n", table,
  909. htab_size_bytes);
  910. htab_address = __va(table);
  911. /* htab absolute addr + encoded htabsize */
  912. _SDR1 = table + __ilog2(htab_size_bytes) - 18;
  913. /* Initialize the HPT with no entries */
  914. memset((void *)table, 0, htab_size_bytes);
  915. if (!cpu_has_feature(CPU_FTR_ARCH_300))
  916. /* Set SDR1 */
  917. mtspr(SPRN_SDR1, _SDR1);
  918. else
  919. hash_init_partition_table(table, htab_size_bytes);
  920. }
  921. prot = pgprot_val(PAGE_KERNEL);
  922. if (debug_pagealloc_enabled_or_kfence()) {
  923. linear_map_hash_count = memblock_end_of_DRAM() >> PAGE_SHIFT;
  924. linear_map_hash_slots = memblock_alloc_try_nid(
  925. linear_map_hash_count, 1, MEMBLOCK_LOW_LIMIT,
  926. ppc64_rma_size, NUMA_NO_NODE);
  927. if (!linear_map_hash_slots)
  928. panic("%s: Failed to allocate %lu bytes max_addr=%pa\n",
  929. __func__, linear_map_hash_count, &ppc64_rma_size);
  930. }
  931. /* create bolted the linear mapping in the hash table */
  932. for_each_mem_range(i, &base, &end) {
  933. size = end - base;
  934. base = (unsigned long)__va(base);
  935. DBG("creating mapping for region: %lx..%lx (prot: %lx)\n",
  936. base, size, prot);
  937. if ((base + size) >= H_VMALLOC_START) {
  938. pr_warn("Outside the supported range\n");
  939. continue;
  940. }
  941. BUG_ON(htab_bolt_mapping(base, base + size, __pa(base),
  942. prot, mmu_linear_psize, mmu_kernel_ssize));
  943. }
  944. memblock_set_current_limit(MEMBLOCK_ALLOC_ANYWHERE);
  945. /*
  946. * If we have a memory_limit and we've allocated TCEs then we need to
  947. * explicitly map the TCE area at the top of RAM. We also cope with the
  948. * case that the TCEs start below memory_limit.
  949. * tce_alloc_start/end are 16MB aligned so the mapping should work
  950. * for either 4K or 16MB pages.
  951. */
  952. if (tce_alloc_start) {
  953. tce_alloc_start = (unsigned long)__va(tce_alloc_start);
  954. tce_alloc_end = (unsigned long)__va(tce_alloc_end);
  955. if (base + size >= tce_alloc_start)
  956. tce_alloc_start = base + size + 1;
  957. BUG_ON(htab_bolt_mapping(tce_alloc_start, tce_alloc_end,
  958. __pa(tce_alloc_start), prot,
  959. mmu_linear_psize, mmu_kernel_ssize));
  960. }
  961. DBG(" <- htab_initialize()\n");
  962. }
  963. #undef KB
  964. #undef MB
  965. void __init hash__early_init_devtree(void)
  966. {
  967. /* Initialize segment sizes */
  968. of_scan_flat_dt(htab_dt_scan_seg_sizes, NULL);
  969. /* Initialize page sizes */
  970. htab_scan_page_sizes();
  971. }
  972. static struct hash_mm_context init_hash_mm_context;
  973. void __init hash__early_init_mmu(void)
  974. {
  975. #ifndef CONFIG_PPC_64K_PAGES
  976. /*
  977. * We have code in __hash_page_4K() and elsewhere, which assumes it can
  978. * do the following:
  979. * new_pte |= (slot << H_PAGE_F_GIX_SHIFT) & (H_PAGE_F_SECOND | H_PAGE_F_GIX);
  980. *
  981. * Where the slot number is between 0-15, and values of 8-15 indicate
  982. * the secondary bucket. For that code to work H_PAGE_F_SECOND and
  983. * H_PAGE_F_GIX must occupy four contiguous bits in the PTE, and
  984. * H_PAGE_F_SECOND must be placed above H_PAGE_F_GIX. Assert that here
  985. * with a BUILD_BUG_ON().
  986. */
  987. BUILD_BUG_ON(H_PAGE_F_SECOND != (1ul << (H_PAGE_F_GIX_SHIFT + 3)));
  988. #endif /* CONFIG_PPC_64K_PAGES */
  989. htab_init_page_sizes();
  990. /*
  991. * initialize page table size
  992. */
  993. __pte_frag_nr = H_PTE_FRAG_NR;
  994. __pte_frag_size_shift = H_PTE_FRAG_SIZE_SHIFT;
  995. __pmd_frag_nr = H_PMD_FRAG_NR;
  996. __pmd_frag_size_shift = H_PMD_FRAG_SIZE_SHIFT;
  997. __pte_index_size = H_PTE_INDEX_SIZE;
  998. __pmd_index_size = H_PMD_INDEX_SIZE;
  999. __pud_index_size = H_PUD_INDEX_SIZE;
  1000. __pgd_index_size = H_PGD_INDEX_SIZE;
  1001. __pud_cache_index = H_PUD_CACHE_INDEX;
  1002. __pte_table_size = H_PTE_TABLE_SIZE;
  1003. __pmd_table_size = H_PMD_TABLE_SIZE;
  1004. __pud_table_size = H_PUD_TABLE_SIZE;
  1005. __pgd_table_size = H_PGD_TABLE_SIZE;
  1006. /*
  1007. * 4k use hugepd format, so for hash set then to
  1008. * zero
  1009. */
  1010. __pmd_val_bits = HASH_PMD_VAL_BITS;
  1011. __pud_val_bits = HASH_PUD_VAL_BITS;
  1012. __pgd_val_bits = HASH_PGD_VAL_BITS;
  1013. __kernel_virt_start = H_KERN_VIRT_START;
  1014. __vmalloc_start = H_VMALLOC_START;
  1015. __vmalloc_end = H_VMALLOC_END;
  1016. __kernel_io_start = H_KERN_IO_START;
  1017. __kernel_io_end = H_KERN_IO_END;
  1018. vmemmap = (struct page *)H_VMEMMAP_START;
  1019. ioremap_bot = IOREMAP_BASE;
  1020. #ifdef CONFIG_PCI
  1021. pci_io_base = ISA_IO_BASE;
  1022. #endif
  1023. /* Select appropriate backend */
  1024. if (firmware_has_feature(FW_FEATURE_PS3_LV1))
  1025. ps3_early_mm_init();
  1026. else if (firmware_has_feature(FW_FEATURE_LPAR))
  1027. hpte_init_pseries();
  1028. else if (IS_ENABLED(CONFIG_PPC_HASH_MMU_NATIVE))
  1029. hpte_init_native();
  1030. if (!mmu_hash_ops.hpte_insert)
  1031. panic("hash__early_init_mmu: No MMU hash ops defined!\n");
  1032. /*
  1033. * Initialize the MMU Hash table and create the linear mapping
  1034. * of memory. Has to be done before SLB initialization as this is
  1035. * currently where the page size encoding is obtained.
  1036. */
  1037. htab_initialize();
  1038. init_mm.context.hash_context = &init_hash_mm_context;
  1039. mm_ctx_set_slb_addr_limit(&init_mm.context, SLB_ADDR_LIMIT_DEFAULT);
  1040. pr_info("Initializing hash mmu with SLB\n");
  1041. /* Initialize SLB management */
  1042. slb_initialize();
  1043. if (cpu_has_feature(CPU_FTR_ARCH_206)
  1044. && cpu_has_feature(CPU_FTR_HVMODE))
  1045. tlbiel_all();
  1046. }
  1047. #ifdef CONFIG_SMP
  1048. void hash__early_init_mmu_secondary(void)
  1049. {
  1050. /* Initialize hash table for that CPU */
  1051. if (!firmware_has_feature(FW_FEATURE_LPAR)) {
  1052. if (!cpu_has_feature(CPU_FTR_ARCH_300))
  1053. mtspr(SPRN_SDR1, _SDR1);
  1054. else
  1055. set_ptcr_when_no_uv(__pa(partition_tb) |
  1056. (PATB_SIZE_SHIFT - 12));
  1057. }
  1058. /* Initialize SLB */
  1059. slb_initialize();
  1060. if (cpu_has_feature(CPU_FTR_ARCH_206)
  1061. && cpu_has_feature(CPU_FTR_HVMODE))
  1062. tlbiel_all();
  1063. #ifdef CONFIG_PPC_MEM_KEYS
  1064. if (mmu_has_feature(MMU_FTR_PKEY))
  1065. mtspr(SPRN_UAMOR, default_uamor);
  1066. #endif
  1067. }
  1068. #endif /* CONFIG_SMP */
  1069. /*
  1070. * Called by asm hashtable.S for doing lazy icache flush
  1071. */
  1072. unsigned int hash_page_do_lazy_icache(unsigned int pp, pte_t pte, int trap)
  1073. {
  1074. struct page *page;
  1075. if (!pfn_valid(pte_pfn(pte)))
  1076. return pp;
  1077. page = pte_page(pte);
  1078. /* page is dirty */
  1079. if (!test_bit(PG_dcache_clean, &page->flags) && !PageReserved(page)) {
  1080. if (trap == INTERRUPT_INST_STORAGE) {
  1081. flush_dcache_icache_page(page);
  1082. set_bit(PG_dcache_clean, &page->flags);
  1083. } else
  1084. pp |= HPTE_R_N;
  1085. }
  1086. return pp;
  1087. }
  1088. static unsigned int get_paca_psize(unsigned long addr)
  1089. {
  1090. unsigned char *psizes;
  1091. unsigned long index, mask_index;
  1092. if (addr < SLICE_LOW_TOP) {
  1093. psizes = get_paca()->mm_ctx_low_slices_psize;
  1094. index = GET_LOW_SLICE_INDEX(addr);
  1095. } else {
  1096. psizes = get_paca()->mm_ctx_high_slices_psize;
  1097. index = GET_HIGH_SLICE_INDEX(addr);
  1098. }
  1099. mask_index = index & 0x1;
  1100. return (psizes[index >> 1] >> (mask_index * 4)) & 0xF;
  1101. }
  1102. /*
  1103. * Demote a segment to using 4k pages.
  1104. * For now this makes the whole process use 4k pages.
  1105. */
  1106. #ifdef CONFIG_PPC_64K_PAGES
  1107. void demote_segment_4k(struct mm_struct *mm, unsigned long addr)
  1108. {
  1109. if (get_slice_psize(mm, addr) == MMU_PAGE_4K)
  1110. return;
  1111. slice_set_range_psize(mm, addr, 1, MMU_PAGE_4K);
  1112. copro_flush_all_slbs(mm);
  1113. if ((get_paca_psize(addr) != MMU_PAGE_4K) && (current->mm == mm)) {
  1114. copy_mm_to_paca(mm);
  1115. slb_flush_and_restore_bolted();
  1116. }
  1117. }
  1118. #endif /* CONFIG_PPC_64K_PAGES */
  1119. #ifdef CONFIG_PPC_SUBPAGE_PROT
  1120. /*
  1121. * This looks up a 2-bit protection code for a 4k subpage of a 64k page.
  1122. * Userspace sets the subpage permissions using the subpage_prot system call.
  1123. *
  1124. * Result is 0: full permissions, _PAGE_RW: read-only,
  1125. * _PAGE_RWX: no access.
  1126. */
  1127. static int subpage_protection(struct mm_struct *mm, unsigned long ea)
  1128. {
  1129. struct subpage_prot_table *spt = mm_ctx_subpage_prot(&mm->context);
  1130. u32 spp = 0;
  1131. u32 **sbpm, *sbpp;
  1132. if (!spt)
  1133. return 0;
  1134. if (ea >= spt->maxaddr)
  1135. return 0;
  1136. if (ea < 0x100000000UL) {
  1137. /* addresses below 4GB use spt->low_prot */
  1138. sbpm = spt->low_prot;
  1139. } else {
  1140. sbpm = spt->protptrs[ea >> SBP_L3_SHIFT];
  1141. if (!sbpm)
  1142. return 0;
  1143. }
  1144. sbpp = sbpm[(ea >> SBP_L2_SHIFT) & (SBP_L2_COUNT - 1)];
  1145. if (!sbpp)
  1146. return 0;
  1147. spp = sbpp[(ea >> PAGE_SHIFT) & (SBP_L1_COUNT - 1)];
  1148. /* extract 2-bit bitfield for this 4k subpage */
  1149. spp >>= 30 - 2 * ((ea >> 12) & 0xf);
  1150. /*
  1151. * 0 -> full permission
  1152. * 1 -> Read only
  1153. * 2 -> no access.
  1154. * We return the flag that need to be cleared.
  1155. */
  1156. spp = ((spp & 2) ? _PAGE_RWX : 0) | ((spp & 1) ? _PAGE_WRITE : 0);
  1157. return spp;
  1158. }
  1159. #else /* CONFIG_PPC_SUBPAGE_PROT */
  1160. static inline int subpage_protection(struct mm_struct *mm, unsigned long ea)
  1161. {
  1162. return 0;
  1163. }
  1164. #endif
  1165. void hash_failure_debug(unsigned long ea, unsigned long access,
  1166. unsigned long vsid, unsigned long trap,
  1167. int ssize, int psize, int lpsize, unsigned long pte)
  1168. {
  1169. if (!printk_ratelimit())
  1170. return;
  1171. pr_info("mm: Hashing failure ! EA=0x%lx access=0x%lx current=%s\n",
  1172. ea, access, current->comm);
  1173. pr_info(" trap=0x%lx vsid=0x%lx ssize=%d base psize=%d psize %d pte=0x%lx\n",
  1174. trap, vsid, ssize, psize, lpsize, pte);
  1175. }
  1176. static void check_paca_psize(unsigned long ea, struct mm_struct *mm,
  1177. int psize, bool user_region)
  1178. {
  1179. if (user_region) {
  1180. if (psize != get_paca_psize(ea)) {
  1181. copy_mm_to_paca(mm);
  1182. slb_flush_and_restore_bolted();
  1183. }
  1184. } else if (get_paca()->vmalloc_sllp !=
  1185. mmu_psize_defs[mmu_vmalloc_psize].sllp) {
  1186. get_paca()->vmalloc_sllp =
  1187. mmu_psize_defs[mmu_vmalloc_psize].sllp;
  1188. slb_vmalloc_update();
  1189. }
  1190. }
  1191. /*
  1192. * Result code is:
  1193. * 0 - handled
  1194. * 1 - normal page fault
  1195. * -1 - critical hash insertion error
  1196. * -2 - access not permitted by subpage protection mechanism
  1197. */
  1198. int hash_page_mm(struct mm_struct *mm, unsigned long ea,
  1199. unsigned long access, unsigned long trap,
  1200. unsigned long flags)
  1201. {
  1202. bool is_thp;
  1203. pgd_t *pgdir;
  1204. unsigned long vsid;
  1205. pte_t *ptep;
  1206. unsigned hugeshift;
  1207. int rc, user_region = 0;
  1208. int psize, ssize;
  1209. DBG_LOW("hash_page(ea=%016lx, access=%lx, trap=%lx\n",
  1210. ea, access, trap);
  1211. trace_hash_fault(ea, access, trap);
  1212. /* Get region & vsid */
  1213. switch (get_region_id(ea)) {
  1214. case USER_REGION_ID:
  1215. user_region = 1;
  1216. if (! mm) {
  1217. DBG_LOW(" user region with no mm !\n");
  1218. rc = 1;
  1219. goto bail;
  1220. }
  1221. psize = get_slice_psize(mm, ea);
  1222. ssize = user_segment_size(ea);
  1223. vsid = get_user_vsid(&mm->context, ea, ssize);
  1224. break;
  1225. case VMALLOC_REGION_ID:
  1226. vsid = get_kernel_vsid(ea, mmu_kernel_ssize);
  1227. psize = mmu_vmalloc_psize;
  1228. ssize = mmu_kernel_ssize;
  1229. flags |= HPTE_USE_KERNEL_KEY;
  1230. break;
  1231. case IO_REGION_ID:
  1232. vsid = get_kernel_vsid(ea, mmu_kernel_ssize);
  1233. psize = mmu_io_psize;
  1234. ssize = mmu_kernel_ssize;
  1235. flags |= HPTE_USE_KERNEL_KEY;
  1236. break;
  1237. default:
  1238. /*
  1239. * Not a valid range
  1240. * Send the problem up to do_page_fault()
  1241. */
  1242. rc = 1;
  1243. goto bail;
  1244. }
  1245. DBG_LOW(" mm=%p, mm->pgdir=%p, vsid=%016lx\n", mm, mm->pgd, vsid);
  1246. /* Bad address. */
  1247. if (!vsid) {
  1248. DBG_LOW("Bad address!\n");
  1249. rc = 1;
  1250. goto bail;
  1251. }
  1252. /* Get pgdir */
  1253. pgdir = mm->pgd;
  1254. if (pgdir == NULL) {
  1255. rc = 1;
  1256. goto bail;
  1257. }
  1258. /* Check CPU locality */
  1259. if (user_region && mm_is_thread_local(mm))
  1260. flags |= HPTE_LOCAL_UPDATE;
  1261. #ifndef CONFIG_PPC_64K_PAGES
  1262. /*
  1263. * If we use 4K pages and our psize is not 4K, then we might
  1264. * be hitting a special driver mapping, and need to align the
  1265. * address before we fetch the PTE.
  1266. *
  1267. * It could also be a hugepage mapping, in which case this is
  1268. * not necessary, but it's not harmful, either.
  1269. */
  1270. if (psize != MMU_PAGE_4K)
  1271. ea &= ~((1ul << mmu_psize_defs[psize].shift) - 1);
  1272. #endif /* CONFIG_PPC_64K_PAGES */
  1273. /* Get PTE and page size from page tables */
  1274. ptep = find_linux_pte(pgdir, ea, &is_thp, &hugeshift);
  1275. if (ptep == NULL || !pte_present(*ptep)) {
  1276. DBG_LOW(" no PTE !\n");
  1277. rc = 1;
  1278. goto bail;
  1279. }
  1280. /*
  1281. * Add _PAGE_PRESENT to the required access perm. If there are parallel
  1282. * updates to the pte that can possibly clear _PAGE_PTE, catch that too.
  1283. *
  1284. * We can safely use the return pte address in rest of the function
  1285. * because we do set H_PAGE_BUSY which prevents further updates to pte
  1286. * from generic code.
  1287. */
  1288. access |= _PAGE_PRESENT | _PAGE_PTE;
  1289. /*
  1290. * Pre-check access permissions (will be re-checked atomically
  1291. * in __hash_page_XX but this pre-check is a fast path
  1292. */
  1293. if (!check_pte_access(access, pte_val(*ptep))) {
  1294. DBG_LOW(" no access !\n");
  1295. rc = 1;
  1296. goto bail;
  1297. }
  1298. if (hugeshift) {
  1299. if (is_thp)
  1300. rc = __hash_page_thp(ea, access, vsid, (pmd_t *)ptep,
  1301. trap, flags, ssize, psize);
  1302. #ifdef CONFIG_HUGETLB_PAGE
  1303. else
  1304. rc = __hash_page_huge(ea, access, vsid, ptep, trap,
  1305. flags, ssize, hugeshift, psize);
  1306. #else
  1307. else {
  1308. /*
  1309. * if we have hugeshift, and is not transhuge with
  1310. * hugetlb disabled, something is really wrong.
  1311. */
  1312. rc = 1;
  1313. WARN_ON(1);
  1314. }
  1315. #endif
  1316. if (current->mm == mm)
  1317. check_paca_psize(ea, mm, psize, user_region);
  1318. goto bail;
  1319. }
  1320. #ifndef CONFIG_PPC_64K_PAGES
  1321. DBG_LOW(" i-pte: %016lx\n", pte_val(*ptep));
  1322. #else
  1323. DBG_LOW(" i-pte: %016lx %016lx\n", pte_val(*ptep),
  1324. pte_val(*(ptep + PTRS_PER_PTE)));
  1325. #endif
  1326. /* Do actual hashing */
  1327. #ifdef CONFIG_PPC_64K_PAGES
  1328. /* If H_PAGE_4K_PFN is set, make sure this is a 4k segment */
  1329. if ((pte_val(*ptep) & H_PAGE_4K_PFN) && psize == MMU_PAGE_64K) {
  1330. demote_segment_4k(mm, ea);
  1331. psize = MMU_PAGE_4K;
  1332. }
  1333. /*
  1334. * If this PTE is non-cacheable and we have restrictions on
  1335. * using non cacheable large pages, then we switch to 4k
  1336. */
  1337. if (mmu_ci_restrictions && psize == MMU_PAGE_64K && pte_ci(*ptep)) {
  1338. if (user_region) {
  1339. demote_segment_4k(mm, ea);
  1340. psize = MMU_PAGE_4K;
  1341. } else if (ea < VMALLOC_END) {
  1342. /*
  1343. * some driver did a non-cacheable mapping
  1344. * in vmalloc space, so switch vmalloc
  1345. * to 4k pages
  1346. */
  1347. printk(KERN_ALERT "Reducing vmalloc segment "
  1348. "to 4kB pages because of "
  1349. "non-cacheable mapping\n");
  1350. psize = mmu_vmalloc_psize = MMU_PAGE_4K;
  1351. copro_flush_all_slbs(mm);
  1352. }
  1353. }
  1354. #endif /* CONFIG_PPC_64K_PAGES */
  1355. if (current->mm == mm)
  1356. check_paca_psize(ea, mm, psize, user_region);
  1357. #ifdef CONFIG_PPC_64K_PAGES
  1358. if (psize == MMU_PAGE_64K)
  1359. rc = __hash_page_64K(ea, access, vsid, ptep, trap,
  1360. flags, ssize);
  1361. else
  1362. #endif /* CONFIG_PPC_64K_PAGES */
  1363. {
  1364. int spp = subpage_protection(mm, ea);
  1365. if (access & spp)
  1366. rc = -2;
  1367. else
  1368. rc = __hash_page_4K(ea, access, vsid, ptep, trap,
  1369. flags, ssize, spp);
  1370. }
  1371. /*
  1372. * Dump some info in case of hash insertion failure, they should
  1373. * never happen so it is really useful to know if/when they do
  1374. */
  1375. if (rc == -1)
  1376. hash_failure_debug(ea, access, vsid, trap, ssize, psize,
  1377. psize, pte_val(*ptep));
  1378. #ifndef CONFIG_PPC_64K_PAGES
  1379. DBG_LOW(" o-pte: %016lx\n", pte_val(*ptep));
  1380. #else
  1381. DBG_LOW(" o-pte: %016lx %016lx\n", pte_val(*ptep),
  1382. pte_val(*(ptep + PTRS_PER_PTE)));
  1383. #endif
  1384. DBG_LOW(" -> rc=%d\n", rc);
  1385. bail:
  1386. return rc;
  1387. }
  1388. EXPORT_SYMBOL_GPL(hash_page_mm);
  1389. int hash_page(unsigned long ea, unsigned long access, unsigned long trap,
  1390. unsigned long dsisr)
  1391. {
  1392. unsigned long flags = 0;
  1393. struct mm_struct *mm = current->mm;
  1394. if ((get_region_id(ea) == VMALLOC_REGION_ID) ||
  1395. (get_region_id(ea) == IO_REGION_ID))
  1396. mm = &init_mm;
  1397. if (dsisr & DSISR_NOHPTE)
  1398. flags |= HPTE_NOHPTE_UPDATE;
  1399. return hash_page_mm(mm, ea, access, trap, flags);
  1400. }
  1401. EXPORT_SYMBOL_GPL(hash_page);
  1402. DEFINE_INTERRUPT_HANDLER(do_hash_fault)
  1403. {
  1404. unsigned long ea = regs->dar;
  1405. unsigned long dsisr = regs->dsisr;
  1406. unsigned long access = _PAGE_PRESENT | _PAGE_READ;
  1407. unsigned long flags = 0;
  1408. struct mm_struct *mm;
  1409. unsigned int region_id;
  1410. long err;
  1411. if (unlikely(dsisr & (DSISR_BAD_FAULT_64S | DSISR_KEYFAULT))) {
  1412. hash__do_page_fault(regs);
  1413. return;
  1414. }
  1415. region_id = get_region_id(ea);
  1416. if ((region_id == VMALLOC_REGION_ID) || (region_id == IO_REGION_ID))
  1417. mm = &init_mm;
  1418. else
  1419. mm = current->mm;
  1420. if (dsisr & DSISR_NOHPTE)
  1421. flags |= HPTE_NOHPTE_UPDATE;
  1422. if (dsisr & DSISR_ISSTORE)
  1423. access |= _PAGE_WRITE;
  1424. /*
  1425. * We set _PAGE_PRIVILEGED only when
  1426. * kernel mode access kernel space.
  1427. *
  1428. * _PAGE_PRIVILEGED is NOT set
  1429. * 1) when kernel mode access user space
  1430. * 2) user space access kernel space.
  1431. */
  1432. access |= _PAGE_PRIVILEGED;
  1433. if (user_mode(regs) || (region_id == USER_REGION_ID))
  1434. access &= ~_PAGE_PRIVILEGED;
  1435. if (TRAP(regs) == INTERRUPT_INST_STORAGE)
  1436. access |= _PAGE_EXEC;
  1437. err = hash_page_mm(mm, ea, access, TRAP(regs), flags);
  1438. if (unlikely(err < 0)) {
  1439. // failed to insert a hash PTE due to an hypervisor error
  1440. if (user_mode(regs)) {
  1441. if (IS_ENABLED(CONFIG_PPC_SUBPAGE_PROT) && err == -2)
  1442. _exception(SIGSEGV, regs, SEGV_ACCERR, ea);
  1443. else
  1444. _exception(SIGBUS, regs, BUS_ADRERR, ea);
  1445. } else {
  1446. bad_page_fault(regs, SIGBUS);
  1447. }
  1448. err = 0;
  1449. } else if (err) {
  1450. hash__do_page_fault(regs);
  1451. }
  1452. }
  1453. static bool should_hash_preload(struct mm_struct *mm, unsigned long ea)
  1454. {
  1455. int psize = get_slice_psize(mm, ea);
  1456. /* We only prefault standard pages for now */
  1457. if (unlikely(psize != mm_ctx_user_psize(&mm->context)))
  1458. return false;
  1459. /*
  1460. * Don't prefault if subpage protection is enabled for the EA.
  1461. */
  1462. if (unlikely((psize == MMU_PAGE_4K) && subpage_protection(mm, ea)))
  1463. return false;
  1464. return true;
  1465. }
  1466. static void hash_preload(struct mm_struct *mm, pte_t *ptep, unsigned long ea,
  1467. bool is_exec, unsigned long trap)
  1468. {
  1469. unsigned long vsid;
  1470. pgd_t *pgdir;
  1471. int rc, ssize, update_flags = 0;
  1472. unsigned long access = _PAGE_PRESENT | _PAGE_READ | (is_exec ? _PAGE_EXEC : 0);
  1473. unsigned long flags;
  1474. BUG_ON(get_region_id(ea) != USER_REGION_ID);
  1475. if (!should_hash_preload(mm, ea))
  1476. return;
  1477. DBG_LOW("hash_preload(mm=%p, mm->pgdir=%p, ea=%016lx, access=%lx,"
  1478. " trap=%lx\n", mm, mm->pgd, ea, access, trap);
  1479. /* Get Linux PTE if available */
  1480. pgdir = mm->pgd;
  1481. if (pgdir == NULL)
  1482. return;
  1483. /* Get VSID */
  1484. ssize = user_segment_size(ea);
  1485. vsid = get_user_vsid(&mm->context, ea, ssize);
  1486. if (!vsid)
  1487. return;
  1488. #ifdef CONFIG_PPC_64K_PAGES
  1489. /* If either H_PAGE_4K_PFN or cache inhibited is set (and we are on
  1490. * a 64K kernel), then we don't preload, hash_page() will take
  1491. * care of it once we actually try to access the page.
  1492. * That way we don't have to duplicate all of the logic for segment
  1493. * page size demotion here
  1494. * Called with PTL held, hence can be sure the value won't change in
  1495. * between.
  1496. */
  1497. if ((pte_val(*ptep) & H_PAGE_4K_PFN) || pte_ci(*ptep))
  1498. return;
  1499. #endif /* CONFIG_PPC_64K_PAGES */
  1500. /*
  1501. * __hash_page_* must run with interrupts off, including PMI interrupts
  1502. * off, as it sets the H_PAGE_BUSY bit.
  1503. *
  1504. * It's otherwise possible for perf interrupts to hit at any time and
  1505. * may take a hash fault reading the user stack, which could take a
  1506. * hash miss and deadlock on the same H_PAGE_BUSY bit.
  1507. *
  1508. * Interrupts must also be off for the duration of the
  1509. * mm_is_thread_local test and update, to prevent preempt running the
  1510. * mm on another CPU (XXX: this may be racy vs kthread_use_mm).
  1511. */
  1512. powerpc_local_irq_pmu_save(flags);
  1513. /* Is that local to this CPU ? */
  1514. if (mm_is_thread_local(mm))
  1515. update_flags |= HPTE_LOCAL_UPDATE;
  1516. /* Hash it in */
  1517. #ifdef CONFIG_PPC_64K_PAGES
  1518. if (mm_ctx_user_psize(&mm->context) == MMU_PAGE_64K)
  1519. rc = __hash_page_64K(ea, access, vsid, ptep, trap,
  1520. update_flags, ssize);
  1521. else
  1522. #endif /* CONFIG_PPC_64K_PAGES */
  1523. rc = __hash_page_4K(ea, access, vsid, ptep, trap, update_flags,
  1524. ssize, subpage_protection(mm, ea));
  1525. /* Dump some info in case of hash insertion failure, they should
  1526. * never happen so it is really useful to know if/when they do
  1527. */
  1528. if (rc == -1)
  1529. hash_failure_debug(ea, access, vsid, trap, ssize,
  1530. mm_ctx_user_psize(&mm->context),
  1531. mm_ctx_user_psize(&mm->context),
  1532. pte_val(*ptep));
  1533. powerpc_local_irq_pmu_restore(flags);
  1534. }
  1535. /*
  1536. * This is called at the end of handling a user page fault, when the
  1537. * fault has been handled by updating a PTE in the linux page tables.
  1538. * We use it to preload an HPTE into the hash table corresponding to
  1539. * the updated linux PTE.
  1540. *
  1541. * This must always be called with the pte lock held.
  1542. */
  1543. void __update_mmu_cache(struct vm_area_struct *vma, unsigned long address,
  1544. pte_t *ptep)
  1545. {
  1546. /*
  1547. * We don't need to worry about _PAGE_PRESENT here because we are
  1548. * called with either mm->page_table_lock held or ptl lock held
  1549. */
  1550. unsigned long trap;
  1551. bool is_exec;
  1552. /* We only want HPTEs for linux PTEs that have _PAGE_ACCESSED set */
  1553. if (!pte_young(*ptep) || address >= TASK_SIZE)
  1554. return;
  1555. /*
  1556. * We try to figure out if we are coming from an instruction
  1557. * access fault and pass that down to __hash_page so we avoid
  1558. * double-faulting on execution of fresh text. We have to test
  1559. * for regs NULL since init will get here first thing at boot.
  1560. *
  1561. * We also avoid filling the hash if not coming from a fault.
  1562. */
  1563. trap = current->thread.regs ? TRAP(current->thread.regs) : 0UL;
  1564. switch (trap) {
  1565. case 0x300:
  1566. is_exec = false;
  1567. break;
  1568. case 0x400:
  1569. is_exec = true;
  1570. break;
  1571. default:
  1572. return;
  1573. }
  1574. hash_preload(vma->vm_mm, ptep, address, is_exec, trap);
  1575. }
  1576. #ifdef CONFIG_PPC_TRANSACTIONAL_MEM
  1577. static inline void tm_flush_hash_page(int local)
  1578. {
  1579. /*
  1580. * Transactions are not aborted by tlbiel, only tlbie. Without, syncing a
  1581. * page back to a block device w/PIO could pick up transactional data
  1582. * (bad!) so we force an abort here. Before the sync the page will be
  1583. * made read-only, which will flush_hash_page. BIG ISSUE here: if the
  1584. * kernel uses a page from userspace without unmapping it first, it may
  1585. * see the speculated version.
  1586. */
  1587. if (local && cpu_has_feature(CPU_FTR_TM) && current->thread.regs &&
  1588. MSR_TM_ACTIVE(current->thread.regs->msr)) {
  1589. tm_enable();
  1590. tm_abort(TM_CAUSE_TLBI);
  1591. }
  1592. }
  1593. #else
  1594. static inline void tm_flush_hash_page(int local)
  1595. {
  1596. }
  1597. #endif
  1598. /*
  1599. * Return the global hash slot, corresponding to the given PTE, which contains
  1600. * the HPTE.
  1601. */
  1602. unsigned long pte_get_hash_gslot(unsigned long vpn, unsigned long shift,
  1603. int ssize, real_pte_t rpte, unsigned int subpg_index)
  1604. {
  1605. unsigned long hash, gslot, hidx;
  1606. hash = hpt_hash(vpn, shift, ssize);
  1607. hidx = __rpte_to_hidx(rpte, subpg_index);
  1608. if (hidx & _PTEIDX_SECONDARY)
  1609. hash = ~hash;
  1610. gslot = (hash & htab_hash_mask) * HPTES_PER_GROUP;
  1611. gslot += hidx & _PTEIDX_GROUP_IX;
  1612. return gslot;
  1613. }
  1614. void flush_hash_page(unsigned long vpn, real_pte_t pte, int psize, int ssize,
  1615. unsigned long flags)
  1616. {
  1617. unsigned long index, shift, gslot;
  1618. int local = flags & HPTE_LOCAL_UPDATE;
  1619. DBG_LOW("flush_hash_page(vpn=%016lx)\n", vpn);
  1620. pte_iterate_hashed_subpages(pte, psize, vpn, index, shift) {
  1621. gslot = pte_get_hash_gslot(vpn, shift, ssize, pte, index);
  1622. DBG_LOW(" sub %ld: gslot=%lx\n", index, gslot);
  1623. /*
  1624. * We use same base page size and actual psize, because we don't
  1625. * use these functions for hugepage
  1626. */
  1627. mmu_hash_ops.hpte_invalidate(gslot, vpn, psize, psize,
  1628. ssize, local);
  1629. } pte_iterate_hashed_end();
  1630. tm_flush_hash_page(local);
  1631. }
  1632. #ifdef CONFIG_TRANSPARENT_HUGEPAGE
  1633. void flush_hash_hugepage(unsigned long vsid, unsigned long addr,
  1634. pmd_t *pmdp, unsigned int psize, int ssize,
  1635. unsigned long flags)
  1636. {
  1637. int i, max_hpte_count, valid;
  1638. unsigned long s_addr;
  1639. unsigned char *hpte_slot_array;
  1640. unsigned long hidx, shift, vpn, hash, slot;
  1641. int local = flags & HPTE_LOCAL_UPDATE;
  1642. s_addr = addr & HPAGE_PMD_MASK;
  1643. hpte_slot_array = get_hpte_slot_array(pmdp);
  1644. /*
  1645. * IF we try to do a HUGE PTE update after a withdraw is done.
  1646. * we will find the below NULL. This happens when we do
  1647. * split_huge_pmd
  1648. */
  1649. if (!hpte_slot_array)
  1650. return;
  1651. if (mmu_hash_ops.hugepage_invalidate) {
  1652. mmu_hash_ops.hugepage_invalidate(vsid, s_addr, hpte_slot_array,
  1653. psize, ssize, local);
  1654. goto tm_abort;
  1655. }
  1656. /*
  1657. * No bluk hpte removal support, invalidate each entry
  1658. */
  1659. shift = mmu_psize_defs[psize].shift;
  1660. max_hpte_count = HPAGE_PMD_SIZE >> shift;
  1661. for (i = 0; i < max_hpte_count; i++) {
  1662. /*
  1663. * 8 bits per each hpte entries
  1664. * 000| [ secondary group (one bit) | hidx (3 bits) | valid bit]
  1665. */
  1666. valid = hpte_valid(hpte_slot_array, i);
  1667. if (!valid)
  1668. continue;
  1669. hidx = hpte_hash_index(hpte_slot_array, i);
  1670. /* get the vpn */
  1671. addr = s_addr + (i * (1ul << shift));
  1672. vpn = hpt_vpn(addr, vsid, ssize);
  1673. hash = hpt_hash(vpn, shift, ssize);
  1674. if (hidx & _PTEIDX_SECONDARY)
  1675. hash = ~hash;
  1676. slot = (hash & htab_hash_mask) * HPTES_PER_GROUP;
  1677. slot += hidx & _PTEIDX_GROUP_IX;
  1678. mmu_hash_ops.hpte_invalidate(slot, vpn, psize,
  1679. MMU_PAGE_16M, ssize, local);
  1680. }
  1681. tm_abort:
  1682. tm_flush_hash_page(local);
  1683. }
  1684. #endif /* CONFIG_TRANSPARENT_HUGEPAGE */
  1685. void flush_hash_range(unsigned long number, int local)
  1686. {
  1687. if (mmu_hash_ops.flush_hash_range)
  1688. mmu_hash_ops.flush_hash_range(number, local);
  1689. else {
  1690. int i;
  1691. struct ppc64_tlb_batch *batch =
  1692. this_cpu_ptr(&ppc64_tlb_batch);
  1693. for (i = 0; i < number; i++)
  1694. flush_hash_page(batch->vpn[i], batch->pte[i],
  1695. batch->psize, batch->ssize, local);
  1696. }
  1697. }
  1698. long hpte_insert_repeating(unsigned long hash, unsigned long vpn,
  1699. unsigned long pa, unsigned long rflags,
  1700. unsigned long vflags, int psize, int ssize)
  1701. {
  1702. unsigned long hpte_group;
  1703. long slot;
  1704. repeat:
  1705. hpte_group = (hash & htab_hash_mask) * HPTES_PER_GROUP;
  1706. /* Insert into the hash table, primary slot */
  1707. slot = mmu_hash_ops.hpte_insert(hpte_group, vpn, pa, rflags, vflags,
  1708. psize, psize, ssize);
  1709. /* Primary is full, try the secondary */
  1710. if (unlikely(slot == -1)) {
  1711. hpte_group = (~hash & htab_hash_mask) * HPTES_PER_GROUP;
  1712. slot = mmu_hash_ops.hpte_insert(hpte_group, vpn, pa, rflags,
  1713. vflags | HPTE_V_SECONDARY,
  1714. psize, psize, ssize);
  1715. if (slot == -1) {
  1716. if (mftb() & 0x1)
  1717. hpte_group = (hash & htab_hash_mask) *
  1718. HPTES_PER_GROUP;
  1719. mmu_hash_ops.hpte_remove(hpte_group);
  1720. goto repeat;
  1721. }
  1722. }
  1723. return slot;
  1724. }
  1725. #if defined(CONFIG_DEBUG_PAGEALLOC) || defined(CONFIG_KFENCE)
  1726. static DEFINE_RAW_SPINLOCK(linear_map_hash_lock);
  1727. static void kernel_map_linear_page(unsigned long vaddr, unsigned long lmi)
  1728. {
  1729. unsigned long hash;
  1730. unsigned long vsid = get_kernel_vsid(vaddr, mmu_kernel_ssize);
  1731. unsigned long vpn = hpt_vpn(vaddr, vsid, mmu_kernel_ssize);
  1732. unsigned long mode = htab_convert_pte_flags(pgprot_val(PAGE_KERNEL), HPTE_USE_KERNEL_KEY);
  1733. long ret;
  1734. hash = hpt_hash(vpn, PAGE_SHIFT, mmu_kernel_ssize);
  1735. /* Don't create HPTE entries for bad address */
  1736. if (!vsid)
  1737. return;
  1738. if (linear_map_hash_slots[lmi] & 0x80)
  1739. return;
  1740. ret = hpte_insert_repeating(hash, vpn, __pa(vaddr), mode,
  1741. HPTE_V_BOLTED,
  1742. mmu_linear_psize, mmu_kernel_ssize);
  1743. BUG_ON (ret < 0);
  1744. raw_spin_lock(&linear_map_hash_lock);
  1745. BUG_ON(linear_map_hash_slots[lmi] & 0x80);
  1746. linear_map_hash_slots[lmi] = ret | 0x80;
  1747. raw_spin_unlock(&linear_map_hash_lock);
  1748. }
  1749. static void kernel_unmap_linear_page(unsigned long vaddr, unsigned long lmi)
  1750. {
  1751. unsigned long hash, hidx, slot;
  1752. unsigned long vsid = get_kernel_vsid(vaddr, mmu_kernel_ssize);
  1753. unsigned long vpn = hpt_vpn(vaddr, vsid, mmu_kernel_ssize);
  1754. hash = hpt_hash(vpn, PAGE_SHIFT, mmu_kernel_ssize);
  1755. raw_spin_lock(&linear_map_hash_lock);
  1756. if (!(linear_map_hash_slots[lmi] & 0x80)) {
  1757. raw_spin_unlock(&linear_map_hash_lock);
  1758. return;
  1759. }
  1760. hidx = linear_map_hash_slots[lmi] & 0x7f;
  1761. linear_map_hash_slots[lmi] = 0;
  1762. raw_spin_unlock(&linear_map_hash_lock);
  1763. if (hidx & _PTEIDX_SECONDARY)
  1764. hash = ~hash;
  1765. slot = (hash & htab_hash_mask) * HPTES_PER_GROUP;
  1766. slot += hidx & _PTEIDX_GROUP_IX;
  1767. mmu_hash_ops.hpte_invalidate(slot, vpn, mmu_linear_psize,
  1768. mmu_linear_psize,
  1769. mmu_kernel_ssize, 0);
  1770. }
  1771. void hash__kernel_map_pages(struct page *page, int numpages, int enable)
  1772. {
  1773. unsigned long flags, vaddr, lmi;
  1774. int i;
  1775. local_irq_save(flags);
  1776. for (i = 0; i < numpages; i++, page++) {
  1777. vaddr = (unsigned long)page_address(page);
  1778. lmi = __pa(vaddr) >> PAGE_SHIFT;
  1779. if (lmi >= linear_map_hash_count)
  1780. continue;
  1781. if (enable)
  1782. kernel_map_linear_page(vaddr, lmi);
  1783. else
  1784. kernel_unmap_linear_page(vaddr, lmi);
  1785. }
  1786. local_irq_restore(flags);
  1787. }
  1788. #endif /* CONFIG_DEBUG_PAGEALLOC || CONFIG_KFENCE */
  1789. void hash__setup_initial_memory_limit(phys_addr_t first_memblock_base,
  1790. phys_addr_t first_memblock_size)
  1791. {
  1792. /*
  1793. * We don't currently support the first MEMBLOCK not mapping 0
  1794. * physical on those processors
  1795. */
  1796. BUG_ON(first_memblock_base != 0);
  1797. /*
  1798. * On virtualized systems the first entry is our RMA region aka VRMA,
  1799. * non-virtualized 64-bit hash MMU systems don't have a limitation
  1800. * on real mode access.
  1801. *
  1802. * For guests on platforms before POWER9, we clamp the it limit to 1G
  1803. * to avoid some funky things such as RTAS bugs etc...
  1804. *
  1805. * On POWER9 we limit to 1TB in case the host erroneously told us that
  1806. * the RMA was >1TB. Effective address bits 0:23 are treated as zero
  1807. * (meaning the access is aliased to zero i.e. addr = addr % 1TB)
  1808. * for virtual real mode addressing and so it doesn't make sense to
  1809. * have an area larger than 1TB as it can't be addressed.
  1810. */
  1811. if (!early_cpu_has_feature(CPU_FTR_HVMODE)) {
  1812. ppc64_rma_size = first_memblock_size;
  1813. if (!early_cpu_has_feature(CPU_FTR_ARCH_300))
  1814. ppc64_rma_size = min_t(u64, ppc64_rma_size, 0x40000000);
  1815. else
  1816. ppc64_rma_size = min_t(u64, ppc64_rma_size,
  1817. 1UL << SID_SHIFT_1T);
  1818. /* Finally limit subsequent allocations */
  1819. memblock_set_current_limit(ppc64_rma_size);
  1820. } else {
  1821. ppc64_rma_size = ULONG_MAX;
  1822. }
  1823. }
  1824. #ifdef CONFIG_DEBUG_FS
  1825. static int hpt_order_get(void *data, u64 *val)
  1826. {
  1827. *val = ppc64_pft_size;
  1828. return 0;
  1829. }
  1830. static int hpt_order_set(void *data, u64 val)
  1831. {
  1832. int ret;
  1833. if (!mmu_hash_ops.resize_hpt)
  1834. return -ENODEV;
  1835. cpus_read_lock();
  1836. ret = mmu_hash_ops.resize_hpt(val);
  1837. cpus_read_unlock();
  1838. return ret;
  1839. }
  1840. DEFINE_DEBUGFS_ATTRIBUTE(fops_hpt_order, hpt_order_get, hpt_order_set, "%llu\n");
  1841. static int __init hash64_debugfs(void)
  1842. {
  1843. debugfs_create_file("hpt_order", 0600, arch_debugfs_dir, NULL,
  1844. &fops_hpt_order);
  1845. return 0;
  1846. }
  1847. machine_device_initcall(pseries, hash64_debugfs);
  1848. #endif /* CONFIG_DEBUG_FS */
  1849. void __init print_system_hash_info(void)
  1850. {
  1851. pr_info("ppc64_pft_size = 0x%llx\n", ppc64_pft_size);
  1852. if (htab_hash_mask)
  1853. pr_info("htab_hash_mask = 0x%lx\n", htab_hash_mask);
  1854. }
  1855. unsigned long arch_randomize_brk(struct mm_struct *mm)
  1856. {
  1857. /*
  1858. * If we are using 1TB segments and we are allowed to randomise
  1859. * the heap, we can put it above 1TB so it is backed by a 1TB
  1860. * segment. Otherwise the heap will be in the bottom 1TB
  1861. * which always uses 256MB segments and this may result in a
  1862. * performance penalty.
  1863. */
  1864. if (is_32bit_task())
  1865. return randomize_page(mm->brk, SZ_32M);
  1866. else if (!radix_enabled() && mmu_highuser_ssize == MMU_SEGSIZE_1T)
  1867. return randomize_page(max_t(unsigned long, mm->brk, SZ_1T), SZ_1G);
  1868. else
  1869. return randomize_page(mm->brk, SZ_1G);
  1870. }