hash_utils_64.c 32 KB

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