hash_utils_64.c 51 KB

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