hash_utils_64.c 38 KB

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