hash_utils_64.c 42 KB

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