hash_utils_64.c 49 KB

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