hash_utils_64.c 51 KB

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