hash_utils_64.c 51 KB

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