hash_utils_64.c 50 KB

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