hash_utils_64.c 50 KB

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