hash_utils_64.c 39 KB

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