hash_utils_64.c 35 KB

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