hash_utils_64.c 42 KB

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