hash_utils_64.c 39 KB

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