hash_utils_64.c 47 KB

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