hash_utils_64.c 34 KB

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