hash_utils_64.c 39 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473
  1. /*
  2. * PowerPC64 port by Mike Corrigan and Dave Engebretsen
  3. * {mikejc|engebret}@us.ibm.com
  4. *
  5. * Copyright (c) 2000 Mike Corrigan <mikejc@us.ibm.com>
  6. *
  7. * SMP scalability work:
  8. * Copyright (C) 2001 Anton Blanchard <anton@au.ibm.com>, IBM
  9. *
  10. * Module name: htab.c
  11. *
  12. * Description:
  13. * PowerPC Hashed Page Table functions
  14. *
  15. * This program is free software; you can redistribute it and/or
  16. * modify it under the terms of the GNU General Public License
  17. * as published by the Free Software Foundation; either version
  18. * 2 of the License, or (at your option) any later version.
  19. */
  20. #undef DEBUG
  21. #undef DEBUG_LOW
  22. #include <linux/spinlock.h>
  23. #include <linux/errno.h>
  24. #include <linux/sched.h>
  25. #include <linux/proc_fs.h>
  26. #include <linux/stat.h>
  27. #include <linux/sysctl.h>
  28. #include <linux/export.h>
  29. #include <linux/ctype.h>
  30. #include <linux/cache.h>
  31. #include <linux/init.h>
  32. #include <linux/signal.h>
  33. #include <linux/memblock.h>
  34. #include <linux/context_tracking.h>
  35. #include <asm/processor.h>
  36. #include <asm/pgtable.h>
  37. #include <asm/mmu.h>
  38. #include <asm/mmu_context.h>
  39. #include <asm/page.h>
  40. #include <asm/types.h>
  41. #include <asm/uaccess.h>
  42. #include <asm/machdep.h>
  43. #include <asm/prom.h>
  44. #include <asm/tlbflush.h>
  45. #include <asm/io.h>
  46. #include <asm/eeh.h>
  47. #include <asm/tlb.h>
  48. #include <asm/cacheflush.h>
  49. #include <asm/cputable.h>
  50. #include <asm/sections.h>
  51. #include <asm/copro.h>
  52. #include <asm/udbg.h>
  53. #include <asm/code-patching.h>
  54. #include <asm/fadump.h>
  55. #include <asm/firmware.h>
  56. #include <asm/tm.h>
  57. #ifdef DEBUG
  58. #define DBG(fmt...) udbg_printf(fmt)
  59. #else
  60. #define DBG(fmt...)
  61. #endif
  62. #ifdef DEBUG_LOW
  63. #define DBG_LOW(fmt...) udbg_printf(fmt)
  64. #else
  65. #define DBG_LOW(fmt...)
  66. #endif
  67. #define KB (1024)
  68. #define MB (1024*KB)
  69. #define GB (1024L*MB)
  70. /*
  71. * Note: pte --> Linux PTE
  72. * HPTE --> PowerPC Hashed Page Table Entry
  73. *
  74. * Execution context:
  75. * htab_initialize is called with the MMU off (of course), but
  76. * the kernel has been copied down to zero so it can directly
  77. * reference global data. At this point it is very difficult
  78. * to print debug info.
  79. *
  80. */
  81. #ifdef CONFIG_U3_DART
  82. extern unsigned long dart_tablebase;
  83. #endif /* CONFIG_U3_DART */
  84. static unsigned long _SDR1;
  85. struct mmu_psize_def mmu_psize_defs[MMU_PAGE_COUNT];
  86. EXPORT_SYMBOL_GPL(mmu_psize_defs);
  87. struct hash_pte *htab_address;
  88. unsigned long htab_size_bytes;
  89. unsigned long htab_hash_mask;
  90. EXPORT_SYMBOL_GPL(htab_hash_mask);
  91. int mmu_linear_psize = MMU_PAGE_4K;
  92. EXPORT_SYMBOL_GPL(mmu_linear_psize);
  93. int mmu_virtual_psize = MMU_PAGE_4K;
  94. int mmu_vmalloc_psize = MMU_PAGE_4K;
  95. #ifdef CONFIG_SPARSEMEM_VMEMMAP
  96. int mmu_vmemmap_psize = MMU_PAGE_4K;
  97. #endif
  98. int mmu_io_psize = MMU_PAGE_4K;
  99. int mmu_kernel_ssize = MMU_SEGSIZE_256M;
  100. EXPORT_SYMBOL_GPL(mmu_kernel_ssize);
  101. int mmu_highuser_ssize = MMU_SEGSIZE_256M;
  102. u16 mmu_slb_size = 64;
  103. EXPORT_SYMBOL_GPL(mmu_slb_size);
  104. #ifdef CONFIG_PPC_64K_PAGES
  105. int mmu_ci_restrictions;
  106. #endif
  107. #ifdef CONFIG_DEBUG_PAGEALLOC
  108. static u8 *linear_map_hash_slots;
  109. static unsigned long linear_map_hash_count;
  110. static DEFINE_SPINLOCK(linear_map_hash_lock);
  111. #endif /* CONFIG_DEBUG_PAGEALLOC */
  112. /* There are definitions of page sizes arrays to be used when none
  113. * is provided by the firmware.
  114. */
  115. /* Pre-POWER4 CPUs (4k pages only)
  116. */
  117. static struct mmu_psize_def mmu_psize_defaults_old[] = {
  118. [MMU_PAGE_4K] = {
  119. .shift = 12,
  120. .sllp = 0,
  121. .penc = {[MMU_PAGE_4K] = 0, [1 ... MMU_PAGE_COUNT - 1] = -1},
  122. .avpnm = 0,
  123. .tlbiel = 0,
  124. },
  125. };
  126. /* POWER4, GPUL, POWER5
  127. *
  128. * Support for 16Mb large pages
  129. */
  130. static struct mmu_psize_def mmu_psize_defaults_gp[] = {
  131. [MMU_PAGE_4K] = {
  132. .shift = 12,
  133. .sllp = 0,
  134. .penc = {[MMU_PAGE_4K] = 0, [1 ... MMU_PAGE_COUNT - 1] = -1},
  135. .avpnm = 0,
  136. .tlbiel = 1,
  137. },
  138. [MMU_PAGE_16M] = {
  139. .shift = 24,
  140. .sllp = SLB_VSID_L,
  141. .penc = {[0 ... MMU_PAGE_16M - 1] = -1, [MMU_PAGE_16M] = 0,
  142. [MMU_PAGE_16M + 1 ... MMU_PAGE_COUNT - 1] = -1 },
  143. .avpnm = 0x1UL,
  144. .tlbiel = 0,
  145. },
  146. };
  147. static unsigned long htab_convert_pte_flags(unsigned long pteflags)
  148. {
  149. unsigned long rflags = pteflags & 0x1fa;
  150. /* _PAGE_EXEC -> NOEXEC */
  151. if ((pteflags & _PAGE_EXEC) == 0)
  152. rflags |= HPTE_R_N;
  153. /* PP bits. PAGE_USER is already PP bit 0x2, so we only
  154. * need to add in 0x1 if it's a read-only user page
  155. */
  156. if ((pteflags & _PAGE_USER) && !((pteflags & _PAGE_RW) &&
  157. (pteflags & _PAGE_DIRTY)))
  158. rflags |= 1;
  159. /*
  160. * Always add "C" bit for perf. Memory coherence is always enabled
  161. */
  162. return rflags | HPTE_R_C | HPTE_R_M;
  163. }
  164. int htab_bolt_mapping(unsigned long vstart, unsigned long vend,
  165. unsigned long pstart, unsigned long prot,
  166. int psize, int ssize)
  167. {
  168. unsigned long vaddr, paddr;
  169. unsigned int step, shift;
  170. int ret = 0;
  171. shift = mmu_psize_defs[psize].shift;
  172. step = 1 << shift;
  173. prot = htab_convert_pte_flags(prot);
  174. DBG("htab_bolt_mapping(%lx..%lx -> %lx (%lx,%d,%d)\n",
  175. vstart, vend, pstart, prot, psize, ssize);
  176. for (vaddr = vstart, paddr = pstart; vaddr < vend;
  177. vaddr += step, paddr += step) {
  178. unsigned long hash, hpteg;
  179. unsigned long vsid = get_kernel_vsid(vaddr, ssize);
  180. unsigned long vpn = hpt_vpn(vaddr, vsid, ssize);
  181. unsigned long tprot = prot;
  182. /*
  183. * If we hit a bad address return error.
  184. */
  185. if (!vsid)
  186. return -1;
  187. /* Make kernel text executable */
  188. if (overlaps_kernel_text(vaddr, vaddr + step))
  189. tprot &= ~HPTE_R_N;
  190. /* Make kvm guest trampolines executable */
  191. if (overlaps_kvm_tmp(vaddr, vaddr + step))
  192. tprot &= ~HPTE_R_N;
  193. /*
  194. * If relocatable, check if it overlaps interrupt vectors that
  195. * are copied down to real 0. For relocatable kernel
  196. * (e.g. kdump case) we copy interrupt vectors down to real
  197. * address 0. Mark that region as executable. This is
  198. * because on p8 system with relocation on exception feature
  199. * enabled, exceptions are raised with MMU (IR=DR=1) ON. Hence
  200. * in order to execute the interrupt handlers in virtual
  201. * mode the vector region need to be marked as executable.
  202. */
  203. if ((PHYSICAL_START > MEMORY_START) &&
  204. overlaps_interrupt_vector_text(vaddr, vaddr + step))
  205. tprot &= ~HPTE_R_N;
  206. hash = hpt_hash(vpn, shift, ssize);
  207. hpteg = ((hash & htab_hash_mask) * HPTES_PER_GROUP);
  208. BUG_ON(!ppc_md.hpte_insert);
  209. ret = ppc_md.hpte_insert(hpteg, vpn, paddr, tprot,
  210. HPTE_V_BOLTED, psize, psize, ssize);
  211. if (ret < 0)
  212. break;
  213. #ifdef CONFIG_DEBUG_PAGEALLOC
  214. if ((paddr >> PAGE_SHIFT) < linear_map_hash_count)
  215. linear_map_hash_slots[paddr >> PAGE_SHIFT] = ret | 0x80;
  216. #endif /* CONFIG_DEBUG_PAGEALLOC */
  217. }
  218. return ret < 0 ? ret : 0;
  219. }
  220. #ifdef CONFIG_MEMORY_HOTPLUG
  221. int htab_remove_mapping(unsigned long vstart, unsigned long vend,
  222. int psize, int ssize)
  223. {
  224. unsigned long vaddr;
  225. unsigned int step, shift;
  226. shift = mmu_psize_defs[psize].shift;
  227. step = 1 << shift;
  228. if (!ppc_md.hpte_removebolted) {
  229. printk(KERN_WARNING "Platform doesn't implement "
  230. "hpte_removebolted\n");
  231. return -EINVAL;
  232. }
  233. for (vaddr = vstart; vaddr < vend; vaddr += step)
  234. ppc_md.hpte_removebolted(vaddr, psize, ssize);
  235. return 0;
  236. }
  237. #endif /* CONFIG_MEMORY_HOTPLUG */
  238. static int __init htab_dt_scan_seg_sizes(unsigned long node,
  239. const char *uname, int depth,
  240. void *data)
  241. {
  242. const char *type = of_get_flat_dt_prop(node, "device_type", NULL);
  243. const __be32 *prop;
  244. int size = 0;
  245. /* We are scanning "cpu" nodes only */
  246. if (type == NULL || strcmp(type, "cpu") != 0)
  247. return 0;
  248. prop = of_get_flat_dt_prop(node, "ibm,processor-segment-sizes", &size);
  249. if (prop == NULL)
  250. return 0;
  251. for (; size >= 4; size -= 4, ++prop) {
  252. if (be32_to_cpu(prop[0]) == 40) {
  253. DBG("1T segment support detected\n");
  254. cur_cpu_spec->mmu_features |= MMU_FTR_1T_SEGMENT;
  255. return 1;
  256. }
  257. }
  258. cur_cpu_spec->mmu_features &= ~MMU_FTR_NO_SLBIE_B;
  259. return 0;
  260. }
  261. static void __init htab_init_seg_sizes(void)
  262. {
  263. of_scan_flat_dt(htab_dt_scan_seg_sizes, NULL);
  264. }
  265. static int __init get_idx_from_shift(unsigned int shift)
  266. {
  267. int idx = -1;
  268. switch (shift) {
  269. case 0xc:
  270. idx = MMU_PAGE_4K;
  271. break;
  272. case 0x10:
  273. idx = MMU_PAGE_64K;
  274. break;
  275. case 0x14:
  276. idx = MMU_PAGE_1M;
  277. break;
  278. case 0x18:
  279. idx = MMU_PAGE_16M;
  280. break;
  281. case 0x22:
  282. idx = MMU_PAGE_16G;
  283. break;
  284. }
  285. return idx;
  286. }
  287. static int __init htab_dt_scan_page_sizes(unsigned long node,
  288. const char *uname, int depth,
  289. void *data)
  290. {
  291. const char *type = of_get_flat_dt_prop(node, "device_type", NULL);
  292. const __be32 *prop;
  293. int size = 0;
  294. /* We are scanning "cpu" nodes only */
  295. if (type == NULL || strcmp(type, "cpu") != 0)
  296. return 0;
  297. prop = of_get_flat_dt_prop(node, "ibm,segment-page-sizes", &size);
  298. if (!prop)
  299. return 0;
  300. pr_info("Page sizes from device-tree:\n");
  301. size /= 4;
  302. cur_cpu_spec->mmu_features &= ~(MMU_FTR_16M_PAGE);
  303. while(size > 0) {
  304. unsigned int base_shift = be32_to_cpu(prop[0]);
  305. unsigned int slbenc = be32_to_cpu(prop[1]);
  306. unsigned int lpnum = be32_to_cpu(prop[2]);
  307. struct mmu_psize_def *def;
  308. int idx, base_idx;
  309. size -= 3; prop += 3;
  310. base_idx = get_idx_from_shift(base_shift);
  311. if (base_idx < 0) {
  312. /* skip the pte encoding also */
  313. prop += lpnum * 2; size -= lpnum * 2;
  314. continue;
  315. }
  316. def = &mmu_psize_defs[base_idx];
  317. if (base_idx == MMU_PAGE_16M)
  318. cur_cpu_spec->mmu_features |= MMU_FTR_16M_PAGE;
  319. def->shift = base_shift;
  320. if (base_shift <= 23)
  321. def->avpnm = 0;
  322. else
  323. def->avpnm = (1 << (base_shift - 23)) - 1;
  324. def->sllp = slbenc;
  325. /*
  326. * We don't know for sure what's up with tlbiel, so
  327. * for now we only set it for 4K and 64K pages
  328. */
  329. if (base_idx == MMU_PAGE_4K || base_idx == MMU_PAGE_64K)
  330. def->tlbiel = 1;
  331. else
  332. def->tlbiel = 0;
  333. while (size > 0 && lpnum) {
  334. unsigned int shift = be32_to_cpu(prop[0]);
  335. int penc = be32_to_cpu(prop[1]);
  336. prop += 2; size -= 2;
  337. lpnum--;
  338. idx = get_idx_from_shift(shift);
  339. if (idx < 0)
  340. continue;
  341. if (penc == -1)
  342. pr_err("Invalid penc for base_shift=%d "
  343. "shift=%d\n", base_shift, shift);
  344. def->penc[idx] = penc;
  345. pr_info("base_shift=%d: shift=%d, sllp=0x%04lx,"
  346. " avpnm=0x%08lx, tlbiel=%d, penc=%d\n",
  347. base_shift, shift, def->sllp,
  348. def->avpnm, def->tlbiel, def->penc[idx]);
  349. }
  350. }
  351. return 1;
  352. }
  353. #ifdef CONFIG_HUGETLB_PAGE
  354. /* Scan for 16G memory blocks that have been set aside for huge pages
  355. * and reserve those blocks for 16G huge pages.
  356. */
  357. static int __init htab_dt_scan_hugepage_blocks(unsigned long node,
  358. const char *uname, int depth,
  359. void *data) {
  360. const char *type = of_get_flat_dt_prop(node, "device_type", NULL);
  361. const __be64 *addr_prop;
  362. const __be32 *page_count_prop;
  363. unsigned int expected_pages;
  364. long unsigned int phys_addr;
  365. long unsigned int block_size;
  366. /* We are scanning "memory" nodes only */
  367. if (type == NULL || strcmp(type, "memory") != 0)
  368. return 0;
  369. /* This property is the log base 2 of the number of virtual pages that
  370. * will represent this memory block. */
  371. page_count_prop = of_get_flat_dt_prop(node, "ibm,expected#pages", NULL);
  372. if (page_count_prop == NULL)
  373. return 0;
  374. expected_pages = (1 << be32_to_cpu(page_count_prop[0]));
  375. addr_prop = of_get_flat_dt_prop(node, "reg", NULL);
  376. if (addr_prop == NULL)
  377. return 0;
  378. phys_addr = be64_to_cpu(addr_prop[0]);
  379. block_size = be64_to_cpu(addr_prop[1]);
  380. if (block_size != (16 * GB))
  381. return 0;
  382. printk(KERN_INFO "Huge page(16GB) memory: "
  383. "addr = 0x%lX size = 0x%lX pages = %d\n",
  384. phys_addr, block_size, expected_pages);
  385. if (phys_addr + (16 * GB) <= memblock_end_of_DRAM()) {
  386. memblock_reserve(phys_addr, block_size * expected_pages);
  387. add_gpage(phys_addr, block_size, expected_pages);
  388. }
  389. return 0;
  390. }
  391. #endif /* CONFIG_HUGETLB_PAGE */
  392. static void mmu_psize_set_default_penc(void)
  393. {
  394. int bpsize, apsize;
  395. for (bpsize = 0; bpsize < MMU_PAGE_COUNT; bpsize++)
  396. for (apsize = 0; apsize < MMU_PAGE_COUNT; apsize++)
  397. mmu_psize_defs[bpsize].penc[apsize] = -1;
  398. }
  399. #ifdef CONFIG_PPC_64K_PAGES
  400. static bool might_have_hea(void)
  401. {
  402. /*
  403. * The HEA ethernet adapter requires awareness of the
  404. * GX bus. Without that awareness we can easily assume
  405. * we will never see an HEA ethernet device.
  406. */
  407. #ifdef CONFIG_IBMEBUS
  408. return !cpu_has_feature(CPU_FTR_ARCH_207S);
  409. #else
  410. return false;
  411. #endif
  412. }
  413. #endif /* #ifdef CONFIG_PPC_64K_PAGES */
  414. static void __init htab_init_page_sizes(void)
  415. {
  416. int rc;
  417. /* se the invalid penc to -1 */
  418. mmu_psize_set_default_penc();
  419. /* Default to 4K pages only */
  420. memcpy(mmu_psize_defs, mmu_psize_defaults_old,
  421. sizeof(mmu_psize_defaults_old));
  422. /*
  423. * Try to find the available page sizes in the device-tree
  424. */
  425. rc = of_scan_flat_dt(htab_dt_scan_page_sizes, NULL);
  426. if (rc != 0) /* Found */
  427. goto found;
  428. /*
  429. * Not in the device-tree, let's fallback on known size
  430. * list for 16M capable GP & GR
  431. */
  432. if (mmu_has_feature(MMU_FTR_16M_PAGE))
  433. memcpy(mmu_psize_defs, mmu_psize_defaults_gp,
  434. sizeof(mmu_psize_defaults_gp));
  435. found:
  436. #ifndef CONFIG_DEBUG_PAGEALLOC
  437. /*
  438. * Pick a size for the linear mapping. Currently, we only support
  439. * 16M, 1M and 4K which is the default
  440. */
  441. if (mmu_psize_defs[MMU_PAGE_16M].shift)
  442. mmu_linear_psize = MMU_PAGE_16M;
  443. else if (mmu_psize_defs[MMU_PAGE_1M].shift)
  444. mmu_linear_psize = MMU_PAGE_1M;
  445. #endif /* CONFIG_DEBUG_PAGEALLOC */
  446. #ifdef CONFIG_PPC_64K_PAGES
  447. /*
  448. * Pick a size for the ordinary pages. Default is 4K, we support
  449. * 64K for user mappings and vmalloc if supported by the processor.
  450. * We only use 64k for ioremap if the processor
  451. * (and firmware) support cache-inhibited large pages.
  452. * If not, we use 4k and set mmu_ci_restrictions so that
  453. * hash_page knows to switch processes that use cache-inhibited
  454. * mappings to 4k pages.
  455. */
  456. if (mmu_psize_defs[MMU_PAGE_64K].shift) {
  457. mmu_virtual_psize = MMU_PAGE_64K;
  458. mmu_vmalloc_psize = MMU_PAGE_64K;
  459. if (mmu_linear_psize == MMU_PAGE_4K)
  460. mmu_linear_psize = MMU_PAGE_64K;
  461. if (mmu_has_feature(MMU_FTR_CI_LARGE_PAGE)) {
  462. /*
  463. * When running on pSeries using 64k pages for ioremap
  464. * would stop us accessing the HEA ethernet. So if we
  465. * have the chance of ever seeing one, stay at 4k.
  466. */
  467. if (!might_have_hea() || !machine_is(pseries))
  468. mmu_io_psize = MMU_PAGE_64K;
  469. } else
  470. mmu_ci_restrictions = 1;
  471. }
  472. #endif /* CONFIG_PPC_64K_PAGES */
  473. #ifdef CONFIG_SPARSEMEM_VMEMMAP
  474. /* We try to use 16M pages for vmemmap if that is supported
  475. * and we have at least 1G of RAM at boot
  476. */
  477. if (mmu_psize_defs[MMU_PAGE_16M].shift &&
  478. memblock_phys_mem_size() >= 0x40000000)
  479. mmu_vmemmap_psize = MMU_PAGE_16M;
  480. else if (mmu_psize_defs[MMU_PAGE_64K].shift)
  481. mmu_vmemmap_psize = MMU_PAGE_64K;
  482. else
  483. mmu_vmemmap_psize = MMU_PAGE_4K;
  484. #endif /* CONFIG_SPARSEMEM_VMEMMAP */
  485. printk(KERN_DEBUG "Page orders: linear mapping = %d, "
  486. "virtual = %d, io = %d"
  487. #ifdef CONFIG_SPARSEMEM_VMEMMAP
  488. ", vmemmap = %d"
  489. #endif
  490. "\n",
  491. mmu_psize_defs[mmu_linear_psize].shift,
  492. mmu_psize_defs[mmu_virtual_psize].shift,
  493. mmu_psize_defs[mmu_io_psize].shift
  494. #ifdef CONFIG_SPARSEMEM_VMEMMAP
  495. ,mmu_psize_defs[mmu_vmemmap_psize].shift
  496. #endif
  497. );
  498. #ifdef CONFIG_HUGETLB_PAGE
  499. /* Reserve 16G huge page memory sections for huge pages */
  500. of_scan_flat_dt(htab_dt_scan_hugepage_blocks, NULL);
  501. #endif /* CONFIG_HUGETLB_PAGE */
  502. }
  503. static int __init htab_dt_scan_pftsize(unsigned long node,
  504. const char *uname, int depth,
  505. void *data)
  506. {
  507. const char *type = of_get_flat_dt_prop(node, "device_type", NULL);
  508. const __be32 *prop;
  509. /* We are scanning "cpu" nodes only */
  510. if (type == NULL || strcmp(type, "cpu") != 0)
  511. return 0;
  512. prop = of_get_flat_dt_prop(node, "ibm,pft-size", NULL);
  513. if (prop != NULL) {
  514. /* pft_size[0] is the NUMA CEC cookie */
  515. ppc64_pft_size = be32_to_cpu(prop[1]);
  516. return 1;
  517. }
  518. return 0;
  519. }
  520. static unsigned long __init htab_get_table_size(void)
  521. {
  522. unsigned long mem_size, rnd_mem_size, pteg_count, psize;
  523. /* If hash size isn't already provided by the platform, we try to
  524. * retrieve it from the device-tree. If it's not there neither, we
  525. * calculate it now based on the total RAM size
  526. */
  527. if (ppc64_pft_size == 0)
  528. of_scan_flat_dt(htab_dt_scan_pftsize, NULL);
  529. if (ppc64_pft_size)
  530. return 1UL << ppc64_pft_size;
  531. /* round mem_size up to next power of 2 */
  532. mem_size = memblock_phys_mem_size();
  533. rnd_mem_size = 1UL << __ilog2(mem_size);
  534. if (rnd_mem_size < mem_size)
  535. rnd_mem_size <<= 1;
  536. /* # pages / 2 */
  537. psize = mmu_psize_defs[mmu_virtual_psize].shift;
  538. pteg_count = max(rnd_mem_size >> (psize + 1), 1UL << 11);
  539. return pteg_count << 7;
  540. }
  541. #ifdef CONFIG_MEMORY_HOTPLUG
  542. int create_section_mapping(unsigned long start, unsigned long end)
  543. {
  544. return htab_bolt_mapping(start, end, __pa(start),
  545. pgprot_val(PAGE_KERNEL), mmu_linear_psize,
  546. mmu_kernel_ssize);
  547. }
  548. int remove_section_mapping(unsigned long start, unsigned long end)
  549. {
  550. return htab_remove_mapping(start, end, mmu_linear_psize,
  551. mmu_kernel_ssize);
  552. }
  553. #endif /* CONFIG_MEMORY_HOTPLUG */
  554. extern u32 htab_call_hpte_insert1[];
  555. extern u32 htab_call_hpte_insert2[];
  556. extern u32 htab_call_hpte_remove[];
  557. extern u32 htab_call_hpte_updatepp[];
  558. extern u32 ht64_call_hpte_insert1[];
  559. extern u32 ht64_call_hpte_insert2[];
  560. extern u32 ht64_call_hpte_remove[];
  561. extern u32 ht64_call_hpte_updatepp[];
  562. static void __init htab_finish_init(void)
  563. {
  564. #ifdef CONFIG_PPC_HAS_HASH_64K
  565. patch_branch(ht64_call_hpte_insert1,
  566. ppc_function_entry(ppc_md.hpte_insert),
  567. BRANCH_SET_LINK);
  568. patch_branch(ht64_call_hpte_insert2,
  569. ppc_function_entry(ppc_md.hpte_insert),
  570. BRANCH_SET_LINK);
  571. patch_branch(ht64_call_hpte_remove,
  572. ppc_function_entry(ppc_md.hpte_remove),
  573. BRANCH_SET_LINK);
  574. patch_branch(ht64_call_hpte_updatepp,
  575. ppc_function_entry(ppc_md.hpte_updatepp),
  576. BRANCH_SET_LINK);
  577. #endif /* CONFIG_PPC_HAS_HASH_64K */
  578. patch_branch(htab_call_hpte_insert1,
  579. ppc_function_entry(ppc_md.hpte_insert),
  580. BRANCH_SET_LINK);
  581. patch_branch(htab_call_hpte_insert2,
  582. ppc_function_entry(ppc_md.hpte_insert),
  583. BRANCH_SET_LINK);
  584. patch_branch(htab_call_hpte_remove,
  585. ppc_function_entry(ppc_md.hpte_remove),
  586. BRANCH_SET_LINK);
  587. patch_branch(htab_call_hpte_updatepp,
  588. ppc_function_entry(ppc_md.hpte_updatepp),
  589. BRANCH_SET_LINK);
  590. }
  591. static void __init htab_initialize(void)
  592. {
  593. unsigned long table;
  594. unsigned long pteg_count;
  595. unsigned long prot;
  596. unsigned long base = 0, size = 0, limit;
  597. struct memblock_region *reg;
  598. DBG(" -> htab_initialize()\n");
  599. /* Initialize segment sizes */
  600. htab_init_seg_sizes();
  601. /* Initialize page sizes */
  602. htab_init_page_sizes();
  603. if (mmu_has_feature(MMU_FTR_1T_SEGMENT)) {
  604. mmu_kernel_ssize = MMU_SEGSIZE_1T;
  605. mmu_highuser_ssize = MMU_SEGSIZE_1T;
  606. printk(KERN_INFO "Using 1TB segments\n");
  607. }
  608. /*
  609. * Calculate the required size of the htab. We want the number of
  610. * PTEGs to equal one half the number of real pages.
  611. */
  612. htab_size_bytes = htab_get_table_size();
  613. pteg_count = htab_size_bytes >> 7;
  614. htab_hash_mask = pteg_count - 1;
  615. if (firmware_has_feature(FW_FEATURE_LPAR)) {
  616. /* Using a hypervisor which owns the htab */
  617. htab_address = NULL;
  618. _SDR1 = 0;
  619. #ifdef CONFIG_FA_DUMP
  620. /*
  621. * If firmware assisted dump is active firmware preserves
  622. * the contents of htab along with entire partition memory.
  623. * Clear the htab if firmware assisted dump is active so
  624. * that we dont end up using old mappings.
  625. */
  626. if (is_fadump_active() && ppc_md.hpte_clear_all)
  627. ppc_md.hpte_clear_all();
  628. #endif
  629. } else {
  630. /* Find storage for the HPT. Must be contiguous in
  631. * the absolute address space. On cell we want it to be
  632. * in the first 2 Gig so we can use it for IOMMU hacks.
  633. */
  634. if (machine_is(cell))
  635. limit = 0x80000000;
  636. else
  637. limit = MEMBLOCK_ALLOC_ANYWHERE;
  638. table = memblock_alloc_base(htab_size_bytes, htab_size_bytes, limit);
  639. DBG("Hash table allocated at %lx, size: %lx\n", table,
  640. htab_size_bytes);
  641. htab_address = __va(table);
  642. /* htab absolute addr + encoded htabsize */
  643. _SDR1 = table + __ilog2(pteg_count) - 11;
  644. /* Initialize the HPT with no entries */
  645. memset((void *)table, 0, htab_size_bytes);
  646. /* Set SDR1 */
  647. mtspr(SPRN_SDR1, _SDR1);
  648. }
  649. prot = pgprot_val(PAGE_KERNEL);
  650. #ifdef CONFIG_DEBUG_PAGEALLOC
  651. linear_map_hash_count = memblock_end_of_DRAM() >> PAGE_SHIFT;
  652. linear_map_hash_slots = __va(memblock_alloc_base(linear_map_hash_count,
  653. 1, ppc64_rma_size));
  654. memset(linear_map_hash_slots, 0, linear_map_hash_count);
  655. #endif /* CONFIG_DEBUG_PAGEALLOC */
  656. /* On U3 based machines, we need to reserve the DART area and
  657. * _NOT_ map it to avoid cache paradoxes as it's remapped non
  658. * cacheable later on
  659. */
  660. /* create bolted the linear mapping in the hash table */
  661. for_each_memblock(memory, reg) {
  662. base = (unsigned long)__va(reg->base);
  663. size = reg->size;
  664. DBG("creating mapping for region: %lx..%lx (prot: %lx)\n",
  665. base, size, prot);
  666. #ifdef CONFIG_U3_DART
  667. /* Do not map the DART space. Fortunately, it will be aligned
  668. * in such a way that it will not cross two memblock regions and
  669. * will fit within a single 16Mb page.
  670. * The DART space is assumed to be a full 16Mb region even if
  671. * we only use 2Mb of that space. We will use more of it later
  672. * for AGP GART. We have to use a full 16Mb large page.
  673. */
  674. DBG("DART base: %lx\n", dart_tablebase);
  675. if (dart_tablebase != 0 && dart_tablebase >= base
  676. && dart_tablebase < (base + size)) {
  677. unsigned long dart_table_end = dart_tablebase + 16 * MB;
  678. if (base != dart_tablebase)
  679. BUG_ON(htab_bolt_mapping(base, dart_tablebase,
  680. __pa(base), prot,
  681. mmu_linear_psize,
  682. mmu_kernel_ssize));
  683. if ((base + size) > dart_table_end)
  684. BUG_ON(htab_bolt_mapping(dart_tablebase+16*MB,
  685. base + size,
  686. __pa(dart_table_end),
  687. prot,
  688. mmu_linear_psize,
  689. mmu_kernel_ssize));
  690. continue;
  691. }
  692. #endif /* CONFIG_U3_DART */
  693. BUG_ON(htab_bolt_mapping(base, base + size, __pa(base),
  694. prot, mmu_linear_psize, mmu_kernel_ssize));
  695. }
  696. memblock_set_current_limit(MEMBLOCK_ALLOC_ANYWHERE);
  697. /*
  698. * If we have a memory_limit and we've allocated TCEs then we need to
  699. * explicitly map the TCE area at the top of RAM. We also cope with the
  700. * case that the TCEs start below memory_limit.
  701. * tce_alloc_start/end are 16MB aligned so the mapping should work
  702. * for either 4K or 16MB pages.
  703. */
  704. if (tce_alloc_start) {
  705. tce_alloc_start = (unsigned long)__va(tce_alloc_start);
  706. tce_alloc_end = (unsigned long)__va(tce_alloc_end);
  707. if (base + size >= tce_alloc_start)
  708. tce_alloc_start = base + size + 1;
  709. BUG_ON(htab_bolt_mapping(tce_alloc_start, tce_alloc_end,
  710. __pa(tce_alloc_start), prot,
  711. mmu_linear_psize, mmu_kernel_ssize));
  712. }
  713. htab_finish_init();
  714. DBG(" <- htab_initialize()\n");
  715. }
  716. #undef KB
  717. #undef MB
  718. void __init early_init_mmu(void)
  719. {
  720. /* Initialize the MMU Hash table and create the linear mapping
  721. * of memory. Has to be done before SLB initialization as this is
  722. * currently where the page size encoding is obtained.
  723. */
  724. htab_initialize();
  725. /* Initialize SLB management */
  726. slb_initialize();
  727. }
  728. #ifdef CONFIG_SMP
  729. void early_init_mmu_secondary(void)
  730. {
  731. /* Initialize hash table for that CPU */
  732. if (!firmware_has_feature(FW_FEATURE_LPAR))
  733. mtspr(SPRN_SDR1, _SDR1);
  734. /* Initialize SLB */
  735. slb_initialize();
  736. }
  737. #endif /* CONFIG_SMP */
  738. /*
  739. * Called by asm hashtable.S for doing lazy icache flush
  740. */
  741. unsigned int hash_page_do_lazy_icache(unsigned int pp, pte_t pte, int trap)
  742. {
  743. struct page *page;
  744. if (!pfn_valid(pte_pfn(pte)))
  745. return pp;
  746. page = pte_page(pte);
  747. /* page is dirty */
  748. if (!test_bit(PG_arch_1, &page->flags) && !PageReserved(page)) {
  749. if (trap == 0x400) {
  750. flush_dcache_icache_page(page);
  751. set_bit(PG_arch_1, &page->flags);
  752. } else
  753. pp |= HPTE_R_N;
  754. }
  755. return pp;
  756. }
  757. #ifdef CONFIG_PPC_MM_SLICES
  758. static unsigned int get_paca_psize(unsigned long addr)
  759. {
  760. u64 lpsizes;
  761. unsigned char *hpsizes;
  762. unsigned long index, mask_index;
  763. if (addr < SLICE_LOW_TOP) {
  764. lpsizes = get_paca()->context.low_slices_psize;
  765. index = GET_LOW_SLICE_INDEX(addr);
  766. return (lpsizes >> (index * 4)) & 0xF;
  767. }
  768. hpsizes = get_paca()->context.high_slices_psize;
  769. index = GET_HIGH_SLICE_INDEX(addr);
  770. mask_index = index & 0x1;
  771. return (hpsizes[index >> 1] >> (mask_index * 4)) & 0xF;
  772. }
  773. #else
  774. unsigned int get_paca_psize(unsigned long addr)
  775. {
  776. return get_paca()->context.user_psize;
  777. }
  778. #endif
  779. /*
  780. * Demote a segment to using 4k pages.
  781. * For now this makes the whole process use 4k pages.
  782. */
  783. #ifdef CONFIG_PPC_64K_PAGES
  784. void demote_segment_4k(struct mm_struct *mm, unsigned long addr)
  785. {
  786. if (get_slice_psize(mm, addr) == MMU_PAGE_4K)
  787. return;
  788. slice_set_range_psize(mm, addr, 1, MMU_PAGE_4K);
  789. copro_flush_all_slbs(mm);
  790. if ((get_paca_psize(addr) != MMU_PAGE_4K) && (current->mm == mm)) {
  791. get_paca()->context = mm->context;
  792. slb_flush_and_rebolt();
  793. }
  794. }
  795. #endif /* CONFIG_PPC_64K_PAGES */
  796. #ifdef CONFIG_PPC_SUBPAGE_PROT
  797. /*
  798. * This looks up a 2-bit protection code for a 4k subpage of a 64k page.
  799. * Userspace sets the subpage permissions using the subpage_prot system call.
  800. *
  801. * Result is 0: full permissions, _PAGE_RW: read-only,
  802. * _PAGE_USER or _PAGE_USER|_PAGE_RW: no access.
  803. */
  804. static int subpage_protection(struct mm_struct *mm, unsigned long ea)
  805. {
  806. struct subpage_prot_table *spt = &mm->context.spt;
  807. u32 spp = 0;
  808. u32 **sbpm, *sbpp;
  809. if (ea >= spt->maxaddr)
  810. return 0;
  811. if (ea < 0x100000000UL) {
  812. /* addresses below 4GB use spt->low_prot */
  813. sbpm = spt->low_prot;
  814. } else {
  815. sbpm = spt->protptrs[ea >> SBP_L3_SHIFT];
  816. if (!sbpm)
  817. return 0;
  818. }
  819. sbpp = sbpm[(ea >> SBP_L2_SHIFT) & (SBP_L2_COUNT - 1)];
  820. if (!sbpp)
  821. return 0;
  822. spp = sbpp[(ea >> PAGE_SHIFT) & (SBP_L1_COUNT - 1)];
  823. /* extract 2-bit bitfield for this 4k subpage */
  824. spp >>= 30 - 2 * ((ea >> 12) & 0xf);
  825. /* turn 0,1,2,3 into combination of _PAGE_USER and _PAGE_RW */
  826. spp = ((spp & 2) ? _PAGE_USER : 0) | ((spp & 1) ? _PAGE_RW : 0);
  827. return spp;
  828. }
  829. #else /* CONFIG_PPC_SUBPAGE_PROT */
  830. static inline int subpage_protection(struct mm_struct *mm, unsigned long ea)
  831. {
  832. return 0;
  833. }
  834. #endif
  835. void hash_failure_debug(unsigned long ea, unsigned long access,
  836. unsigned long vsid, unsigned long trap,
  837. int ssize, int psize, int lpsize, unsigned long pte)
  838. {
  839. if (!printk_ratelimit())
  840. return;
  841. pr_info("mm: Hashing failure ! EA=0x%lx access=0x%lx current=%s\n",
  842. ea, access, current->comm);
  843. pr_info(" trap=0x%lx vsid=0x%lx ssize=%d base psize=%d psize %d pte=0x%lx\n",
  844. trap, vsid, ssize, psize, lpsize, pte);
  845. }
  846. static void check_paca_psize(unsigned long ea, struct mm_struct *mm,
  847. int psize, bool user_region)
  848. {
  849. if (user_region) {
  850. if (psize != get_paca_psize(ea)) {
  851. get_paca()->context = mm->context;
  852. slb_flush_and_rebolt();
  853. }
  854. } else if (get_paca()->vmalloc_sllp !=
  855. mmu_psize_defs[mmu_vmalloc_psize].sllp) {
  856. get_paca()->vmalloc_sllp =
  857. mmu_psize_defs[mmu_vmalloc_psize].sllp;
  858. slb_vmalloc_update();
  859. }
  860. }
  861. /* Result code is:
  862. * 0 - handled
  863. * 1 - normal page fault
  864. * -1 - critical hash insertion error
  865. * -2 - access not permitted by subpage protection mechanism
  866. */
  867. int hash_page_mm(struct mm_struct *mm, unsigned long ea, unsigned long access, unsigned long trap)
  868. {
  869. enum ctx_state prev_state = exception_enter();
  870. pgd_t *pgdir;
  871. unsigned long vsid;
  872. pte_t *ptep;
  873. unsigned hugeshift;
  874. const struct cpumask *tmp;
  875. int rc, user_region = 0, local = 0;
  876. int psize, ssize;
  877. DBG_LOW("hash_page(ea=%016lx, access=%lx, trap=%lx\n",
  878. ea, access, trap);
  879. /* Get region & vsid */
  880. switch (REGION_ID(ea)) {
  881. case USER_REGION_ID:
  882. user_region = 1;
  883. if (! mm) {
  884. DBG_LOW(" user region with no mm !\n");
  885. rc = 1;
  886. goto bail;
  887. }
  888. psize = get_slice_psize(mm, ea);
  889. ssize = user_segment_size(ea);
  890. vsid = get_vsid(mm->context.id, ea, ssize);
  891. break;
  892. case VMALLOC_REGION_ID:
  893. vsid = get_kernel_vsid(ea, mmu_kernel_ssize);
  894. if (ea < VMALLOC_END)
  895. psize = mmu_vmalloc_psize;
  896. else
  897. psize = mmu_io_psize;
  898. ssize = mmu_kernel_ssize;
  899. break;
  900. default:
  901. /* Not a valid range
  902. * Send the problem up to do_page_fault
  903. */
  904. rc = 1;
  905. goto bail;
  906. }
  907. DBG_LOW(" mm=%p, mm->pgdir=%p, vsid=%016lx\n", mm, mm->pgd, vsid);
  908. /* Bad address. */
  909. if (!vsid) {
  910. DBG_LOW("Bad address!\n");
  911. rc = 1;
  912. goto bail;
  913. }
  914. /* Get pgdir */
  915. pgdir = mm->pgd;
  916. if (pgdir == NULL) {
  917. rc = 1;
  918. goto bail;
  919. }
  920. /* Check CPU locality */
  921. tmp = cpumask_of(smp_processor_id());
  922. if (user_region && cpumask_equal(mm_cpumask(mm), tmp))
  923. local = 1;
  924. #ifndef CONFIG_PPC_64K_PAGES
  925. /* If we use 4K pages and our psize is not 4K, then we might
  926. * be hitting a special driver mapping, and need to align the
  927. * address before we fetch the PTE.
  928. *
  929. * It could also be a hugepage mapping, in which case this is
  930. * not necessary, but it's not harmful, either.
  931. */
  932. if (psize != MMU_PAGE_4K)
  933. ea &= ~((1ul << mmu_psize_defs[psize].shift) - 1);
  934. #endif /* CONFIG_PPC_64K_PAGES */
  935. /* Get PTE and page size from page tables */
  936. ptep = find_linux_pte_or_hugepte(pgdir, ea, &hugeshift);
  937. if (ptep == NULL || !pte_present(*ptep)) {
  938. DBG_LOW(" no PTE !\n");
  939. rc = 1;
  940. goto bail;
  941. }
  942. /* Add _PAGE_PRESENT to the required access perm */
  943. access |= _PAGE_PRESENT;
  944. /* Pre-check access permissions (will be re-checked atomically
  945. * in __hash_page_XX but this pre-check is a fast path
  946. */
  947. if (access & ~pte_val(*ptep)) {
  948. DBG_LOW(" no access !\n");
  949. rc = 1;
  950. goto bail;
  951. }
  952. if (hugeshift) {
  953. if (pmd_trans_huge(*(pmd_t *)ptep))
  954. rc = __hash_page_thp(ea, access, vsid, (pmd_t *)ptep,
  955. trap, local, ssize, psize);
  956. #ifdef CONFIG_HUGETLB_PAGE
  957. else
  958. rc = __hash_page_huge(ea, access, vsid, ptep, trap,
  959. local, ssize, hugeshift, psize);
  960. #else
  961. else {
  962. /*
  963. * if we have hugeshift, and is not transhuge with
  964. * hugetlb disabled, something is really wrong.
  965. */
  966. rc = 1;
  967. WARN_ON(1);
  968. }
  969. #endif
  970. if (current->mm == mm)
  971. check_paca_psize(ea, mm, psize, user_region);
  972. goto bail;
  973. }
  974. #ifndef CONFIG_PPC_64K_PAGES
  975. DBG_LOW(" i-pte: %016lx\n", pte_val(*ptep));
  976. #else
  977. DBG_LOW(" i-pte: %016lx %016lx\n", pte_val(*ptep),
  978. pte_val(*(ptep + PTRS_PER_PTE)));
  979. #endif
  980. /* Do actual hashing */
  981. #ifdef CONFIG_PPC_64K_PAGES
  982. /* If _PAGE_4K_PFN is set, make sure this is a 4k segment */
  983. if ((pte_val(*ptep) & _PAGE_4K_PFN) && psize == MMU_PAGE_64K) {
  984. demote_segment_4k(mm, ea);
  985. psize = MMU_PAGE_4K;
  986. }
  987. /* If this PTE is non-cacheable and we have restrictions on
  988. * using non cacheable large pages, then we switch to 4k
  989. */
  990. if (mmu_ci_restrictions && psize == MMU_PAGE_64K &&
  991. (pte_val(*ptep) & _PAGE_NO_CACHE)) {
  992. if (user_region) {
  993. demote_segment_4k(mm, ea);
  994. psize = MMU_PAGE_4K;
  995. } else if (ea < VMALLOC_END) {
  996. /*
  997. * some driver did a non-cacheable mapping
  998. * in vmalloc space, so switch vmalloc
  999. * to 4k pages
  1000. */
  1001. printk(KERN_ALERT "Reducing vmalloc segment "
  1002. "to 4kB pages because of "
  1003. "non-cacheable mapping\n");
  1004. psize = mmu_vmalloc_psize = MMU_PAGE_4K;
  1005. copro_flush_all_slbs(mm);
  1006. }
  1007. }
  1008. if (current->mm == mm)
  1009. check_paca_psize(ea, mm, psize, user_region);
  1010. #endif /* CONFIG_PPC_64K_PAGES */
  1011. #ifdef CONFIG_PPC_HAS_HASH_64K
  1012. if (psize == MMU_PAGE_64K)
  1013. rc = __hash_page_64K(ea, access, vsid, ptep, trap, local, ssize);
  1014. else
  1015. #endif /* CONFIG_PPC_HAS_HASH_64K */
  1016. {
  1017. int spp = subpage_protection(mm, ea);
  1018. if (access & spp)
  1019. rc = -2;
  1020. else
  1021. rc = __hash_page_4K(ea, access, vsid, ptep, trap,
  1022. local, ssize, spp);
  1023. }
  1024. /* Dump some info in case of hash insertion failure, they should
  1025. * never happen so it is really useful to know if/when they do
  1026. */
  1027. if (rc == -1)
  1028. hash_failure_debug(ea, access, vsid, trap, ssize, psize,
  1029. psize, pte_val(*ptep));
  1030. #ifndef CONFIG_PPC_64K_PAGES
  1031. DBG_LOW(" o-pte: %016lx\n", pte_val(*ptep));
  1032. #else
  1033. DBG_LOW(" o-pte: %016lx %016lx\n", pte_val(*ptep),
  1034. pte_val(*(ptep + PTRS_PER_PTE)));
  1035. #endif
  1036. DBG_LOW(" -> rc=%d\n", rc);
  1037. bail:
  1038. exception_exit(prev_state);
  1039. return rc;
  1040. }
  1041. EXPORT_SYMBOL_GPL(hash_page_mm);
  1042. int hash_page(unsigned long ea, unsigned long access, unsigned long trap)
  1043. {
  1044. struct mm_struct *mm = current->mm;
  1045. if (REGION_ID(ea) == VMALLOC_REGION_ID)
  1046. mm = &init_mm;
  1047. return hash_page_mm(mm, ea, access, trap);
  1048. }
  1049. EXPORT_SYMBOL_GPL(hash_page);
  1050. void hash_preload(struct mm_struct *mm, unsigned long ea,
  1051. unsigned long access, unsigned long trap)
  1052. {
  1053. int hugepage_shift;
  1054. unsigned long vsid;
  1055. pgd_t *pgdir;
  1056. pte_t *ptep;
  1057. unsigned long flags;
  1058. int rc, ssize, local = 0;
  1059. BUG_ON(REGION_ID(ea) != USER_REGION_ID);
  1060. #ifdef CONFIG_PPC_MM_SLICES
  1061. /* We only prefault standard pages for now */
  1062. if (unlikely(get_slice_psize(mm, ea) != mm->context.user_psize))
  1063. return;
  1064. #endif
  1065. DBG_LOW("hash_preload(mm=%p, mm->pgdir=%p, ea=%016lx, access=%lx,"
  1066. " trap=%lx\n", mm, mm->pgd, ea, access, trap);
  1067. /* Get Linux PTE if available */
  1068. pgdir = mm->pgd;
  1069. if (pgdir == NULL)
  1070. return;
  1071. /* Get VSID */
  1072. ssize = user_segment_size(ea);
  1073. vsid = get_vsid(mm->context.id, ea, ssize);
  1074. if (!vsid)
  1075. return;
  1076. /*
  1077. * Hash doesn't like irqs. Walking linux page table with irq disabled
  1078. * saves us from holding multiple locks.
  1079. */
  1080. local_irq_save(flags);
  1081. /*
  1082. * THP pages use update_mmu_cache_pmd. We don't do
  1083. * hash preload there. Hence can ignore THP here
  1084. */
  1085. ptep = find_linux_pte_or_hugepte(pgdir, ea, &hugepage_shift);
  1086. if (!ptep)
  1087. goto out_exit;
  1088. WARN_ON(hugepage_shift);
  1089. #ifdef CONFIG_PPC_64K_PAGES
  1090. /* If either _PAGE_4K_PFN or _PAGE_NO_CACHE is set (and we are on
  1091. * a 64K kernel), then we don't preload, hash_page() will take
  1092. * care of it once we actually try to access the page.
  1093. * That way we don't have to duplicate all of the logic for segment
  1094. * page size demotion here
  1095. */
  1096. if (pte_val(*ptep) & (_PAGE_4K_PFN | _PAGE_NO_CACHE))
  1097. goto out_exit;
  1098. #endif /* CONFIG_PPC_64K_PAGES */
  1099. /* Is that local to this CPU ? */
  1100. if (cpumask_equal(mm_cpumask(mm), cpumask_of(smp_processor_id())))
  1101. local = 1;
  1102. /* Hash it in */
  1103. #ifdef CONFIG_PPC_HAS_HASH_64K
  1104. if (mm->context.user_psize == MMU_PAGE_64K)
  1105. rc = __hash_page_64K(ea, access, vsid, ptep, trap, local, ssize);
  1106. else
  1107. #endif /* CONFIG_PPC_HAS_HASH_64K */
  1108. rc = __hash_page_4K(ea, access, vsid, ptep, trap, local, ssize,
  1109. subpage_protection(mm, ea));
  1110. /* Dump some info in case of hash insertion failure, they should
  1111. * never happen so it is really useful to know if/when they do
  1112. */
  1113. if (rc == -1)
  1114. hash_failure_debug(ea, access, vsid, trap, ssize,
  1115. mm->context.user_psize,
  1116. mm->context.user_psize,
  1117. pte_val(*ptep));
  1118. out_exit:
  1119. local_irq_restore(flags);
  1120. }
  1121. /* WARNING: This is called from hash_low_64.S, if you change this prototype,
  1122. * do not forget to update the assembly call site !
  1123. */
  1124. void flush_hash_page(unsigned long vpn, real_pte_t pte, int psize, int ssize,
  1125. int local)
  1126. {
  1127. unsigned long hash, index, shift, hidx, slot;
  1128. DBG_LOW("flush_hash_page(vpn=%016lx)\n", vpn);
  1129. pte_iterate_hashed_subpages(pte, psize, vpn, index, shift) {
  1130. hash = hpt_hash(vpn, shift, ssize);
  1131. hidx = __rpte_to_hidx(pte, index);
  1132. if (hidx & _PTEIDX_SECONDARY)
  1133. hash = ~hash;
  1134. slot = (hash & htab_hash_mask) * HPTES_PER_GROUP;
  1135. slot += hidx & _PTEIDX_GROUP_IX;
  1136. DBG_LOW(" sub %ld: hash=%lx, hidx=%lx\n", index, slot, hidx);
  1137. /*
  1138. * We use same base page size and actual psize, because we don't
  1139. * use these functions for hugepage
  1140. */
  1141. ppc_md.hpte_invalidate(slot, vpn, psize, psize, ssize, local);
  1142. } pte_iterate_hashed_end();
  1143. #ifdef CONFIG_PPC_TRANSACTIONAL_MEM
  1144. /* Transactions are not aborted by tlbiel, only tlbie.
  1145. * Without, syncing a page back to a block device w/ PIO could pick up
  1146. * transactional data (bad!) so we force an abort here. Before the
  1147. * sync the page will be made read-only, which will flush_hash_page.
  1148. * BIG ISSUE here: if the kernel uses a page from userspace without
  1149. * unmapping it first, it may see the speculated version.
  1150. */
  1151. if (local && cpu_has_feature(CPU_FTR_TM) &&
  1152. current->thread.regs &&
  1153. MSR_TM_ACTIVE(current->thread.regs->msr)) {
  1154. tm_enable();
  1155. tm_abort(TM_CAUSE_TLBI);
  1156. }
  1157. #endif
  1158. }
  1159. void flush_hash_range(unsigned long number, int local)
  1160. {
  1161. if (ppc_md.flush_hash_range)
  1162. ppc_md.flush_hash_range(number, local);
  1163. else {
  1164. int i;
  1165. struct ppc64_tlb_batch *batch =
  1166. &__get_cpu_var(ppc64_tlb_batch);
  1167. for (i = 0; i < number; i++)
  1168. flush_hash_page(batch->vpn[i], batch->pte[i],
  1169. batch->psize, batch->ssize, local);
  1170. }
  1171. }
  1172. /*
  1173. * low_hash_fault is called when we the low level hash code failed
  1174. * to instert a PTE due to an hypervisor error
  1175. */
  1176. void low_hash_fault(struct pt_regs *regs, unsigned long address, int rc)
  1177. {
  1178. enum ctx_state prev_state = exception_enter();
  1179. if (user_mode(regs)) {
  1180. #ifdef CONFIG_PPC_SUBPAGE_PROT
  1181. if (rc == -2)
  1182. _exception(SIGSEGV, regs, SEGV_ACCERR, address);
  1183. else
  1184. #endif
  1185. _exception(SIGBUS, regs, BUS_ADRERR, address);
  1186. } else
  1187. bad_page_fault(regs, address, SIGBUS);
  1188. exception_exit(prev_state);
  1189. }
  1190. long hpte_insert_repeating(unsigned long hash, unsigned long vpn,
  1191. unsigned long pa, unsigned long rflags,
  1192. unsigned long vflags, int psize, int ssize)
  1193. {
  1194. unsigned long hpte_group;
  1195. long slot;
  1196. repeat:
  1197. hpte_group = ((hash & htab_hash_mask) *
  1198. HPTES_PER_GROUP) & ~0x7UL;
  1199. /* Insert into the hash table, primary slot */
  1200. slot = ppc_md.hpte_insert(hpte_group, vpn, pa, rflags, vflags,
  1201. psize, psize, ssize);
  1202. /* Primary is full, try the secondary */
  1203. if (unlikely(slot == -1)) {
  1204. hpte_group = ((~hash & htab_hash_mask) *
  1205. HPTES_PER_GROUP) & ~0x7UL;
  1206. slot = ppc_md.hpte_insert(hpte_group, vpn, pa, rflags,
  1207. vflags | HPTE_V_SECONDARY,
  1208. psize, psize, ssize);
  1209. if (slot == -1) {
  1210. if (mftb() & 0x1)
  1211. hpte_group = ((hash & htab_hash_mask) *
  1212. HPTES_PER_GROUP)&~0x7UL;
  1213. ppc_md.hpte_remove(hpte_group);
  1214. goto repeat;
  1215. }
  1216. }
  1217. return slot;
  1218. }
  1219. #ifdef CONFIG_DEBUG_PAGEALLOC
  1220. static void kernel_map_linear_page(unsigned long vaddr, unsigned long lmi)
  1221. {
  1222. unsigned long hash;
  1223. unsigned long vsid = get_kernel_vsid(vaddr, mmu_kernel_ssize);
  1224. unsigned long vpn = hpt_vpn(vaddr, vsid, mmu_kernel_ssize);
  1225. unsigned long mode = htab_convert_pte_flags(PAGE_KERNEL);
  1226. long ret;
  1227. hash = hpt_hash(vpn, PAGE_SHIFT, mmu_kernel_ssize);
  1228. /* Don't create HPTE entries for bad address */
  1229. if (!vsid)
  1230. return;
  1231. ret = hpte_insert_repeating(hash, vpn, __pa(vaddr), mode,
  1232. HPTE_V_BOLTED,
  1233. mmu_linear_psize, mmu_kernel_ssize);
  1234. BUG_ON (ret < 0);
  1235. spin_lock(&linear_map_hash_lock);
  1236. BUG_ON(linear_map_hash_slots[lmi] & 0x80);
  1237. linear_map_hash_slots[lmi] = ret | 0x80;
  1238. spin_unlock(&linear_map_hash_lock);
  1239. }
  1240. static void kernel_unmap_linear_page(unsigned long vaddr, unsigned long lmi)
  1241. {
  1242. unsigned long hash, hidx, slot;
  1243. unsigned long vsid = get_kernel_vsid(vaddr, mmu_kernel_ssize);
  1244. unsigned long vpn = hpt_vpn(vaddr, vsid, mmu_kernel_ssize);
  1245. hash = hpt_hash(vpn, PAGE_SHIFT, mmu_kernel_ssize);
  1246. spin_lock(&linear_map_hash_lock);
  1247. BUG_ON(!(linear_map_hash_slots[lmi] & 0x80));
  1248. hidx = linear_map_hash_slots[lmi] & 0x7f;
  1249. linear_map_hash_slots[lmi] = 0;
  1250. spin_unlock(&linear_map_hash_lock);
  1251. if (hidx & _PTEIDX_SECONDARY)
  1252. hash = ~hash;
  1253. slot = (hash & htab_hash_mask) * HPTES_PER_GROUP;
  1254. slot += hidx & _PTEIDX_GROUP_IX;
  1255. ppc_md.hpte_invalidate(slot, vpn, mmu_linear_psize, mmu_linear_psize,
  1256. mmu_kernel_ssize, 0);
  1257. }
  1258. void kernel_map_pages(struct page *page, int numpages, int enable)
  1259. {
  1260. unsigned long flags, vaddr, lmi;
  1261. int i;
  1262. local_irq_save(flags);
  1263. for (i = 0; i < numpages; i++, page++) {
  1264. vaddr = (unsigned long)page_address(page);
  1265. lmi = __pa(vaddr) >> PAGE_SHIFT;
  1266. if (lmi >= linear_map_hash_count)
  1267. continue;
  1268. if (enable)
  1269. kernel_map_linear_page(vaddr, lmi);
  1270. else
  1271. kernel_unmap_linear_page(vaddr, lmi);
  1272. }
  1273. local_irq_restore(flags);
  1274. }
  1275. #endif /* CONFIG_DEBUG_PAGEALLOC */
  1276. void setup_initial_memory_limit(phys_addr_t first_memblock_base,
  1277. phys_addr_t first_memblock_size)
  1278. {
  1279. /* We don't currently support the first MEMBLOCK not mapping 0
  1280. * physical on those processors
  1281. */
  1282. BUG_ON(first_memblock_base != 0);
  1283. /* On LPAR systems, the first entry is our RMA region,
  1284. * non-LPAR 64-bit hash MMU systems don't have a limitation
  1285. * on real mode access, but using the first entry works well
  1286. * enough. We also clamp it to 1G to avoid some funky things
  1287. * such as RTAS bugs etc...
  1288. */
  1289. ppc64_rma_size = min_t(u64, first_memblock_size, 0x40000000);
  1290. /* Finally limit subsequent allocations */
  1291. memblock_set_current_limit(ppc64_rma_size);
  1292. }