hash_utils_64.c 39 KB

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