hugetlbpage.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689
  1. /*
  2. * PPC64 (POWER4) Huge TLB Page Support for Kernel.
  3. *
  4. * Copyright (C) 2003 David Gibson, IBM Corporation.
  5. *
  6. * Based on the IA-32 version:
  7. * Copyright (C) 2002, Rohit Seth <rohit.seth@intel.com>
  8. */
  9. #include <linux/init.h>
  10. #include <linux/fs.h>
  11. #include <linux/mm.h>
  12. #include <linux/hugetlb.h>
  13. #include <linux/pagemap.h>
  14. #include <linux/slab.h>
  15. #include <linux/err.h>
  16. #include <linux/sysctl.h>
  17. #include <asm/mman.h>
  18. #include <asm/pgalloc.h>
  19. #include <asm/tlb.h>
  20. #include <asm/tlbflush.h>
  21. #include <asm/mmu_context.h>
  22. #include <asm/machdep.h>
  23. #include <asm/cputable.h>
  24. #include <asm/spu.h>
  25. #define PAGE_SHIFT_64K 16
  26. #define PAGE_SHIFT_16M 24
  27. #define PAGE_SHIFT_16G 34
  28. #define NUM_LOW_AREAS (0x100000000UL >> SID_SHIFT)
  29. #define NUM_HIGH_AREAS (PGTABLE_RANGE >> HTLB_AREA_SHIFT)
  30. #define MAX_NUMBER_GPAGES 1024
  31. /* Tracks the 16G pages after the device tree is scanned and before the
  32. * huge_boot_pages list is ready. */
  33. static unsigned long gpage_freearray[MAX_NUMBER_GPAGES];
  34. static unsigned nr_gpages;
  35. unsigned int hugepte_shift;
  36. #define PTRS_PER_HUGEPTE (1 << hugepte_shift)
  37. #define HUGEPTE_TABLE_SIZE (sizeof(pte_t) << hugepte_shift)
  38. #define HUGEPD_SHIFT (HPAGE_SHIFT + hugepte_shift)
  39. #define HUGEPD_SIZE (1UL << HUGEPD_SHIFT)
  40. #define HUGEPD_MASK (~(HUGEPD_SIZE-1))
  41. #define huge_pgtable_cache (pgtable_cache[HUGEPTE_CACHE_NUM])
  42. /* Flag to mark huge PD pointers. This means pmd_bad() and pud_bad()
  43. * will choke on pointers to hugepte tables, which is handy for
  44. * catching screwups early. */
  45. #define HUGEPD_OK 0x1
  46. typedef struct { unsigned long pd; } hugepd_t;
  47. #define hugepd_none(hpd) ((hpd).pd == 0)
  48. static inline pte_t *hugepd_page(hugepd_t hpd)
  49. {
  50. BUG_ON(!(hpd.pd & HUGEPD_OK));
  51. return (pte_t *)(hpd.pd & ~HUGEPD_OK);
  52. }
  53. static inline pte_t *hugepte_offset(hugepd_t *hpdp, unsigned long addr)
  54. {
  55. unsigned long idx = ((addr >> HPAGE_SHIFT) & (PTRS_PER_HUGEPTE-1));
  56. pte_t *dir = hugepd_page(*hpdp);
  57. return dir + idx;
  58. }
  59. static int __hugepte_alloc(struct mm_struct *mm, hugepd_t *hpdp,
  60. unsigned long address)
  61. {
  62. pte_t *new = kmem_cache_alloc(huge_pgtable_cache,
  63. GFP_KERNEL|__GFP_REPEAT);
  64. if (! new)
  65. return -ENOMEM;
  66. spin_lock(&mm->page_table_lock);
  67. if (!hugepd_none(*hpdp))
  68. kmem_cache_free(huge_pgtable_cache, new);
  69. else
  70. hpdp->pd = (unsigned long)new | HUGEPD_OK;
  71. spin_unlock(&mm->page_table_lock);
  72. return 0;
  73. }
  74. /* Base page size affects how we walk hugetlb page tables */
  75. #ifdef CONFIG_PPC_64K_PAGES
  76. #define hpmd_offset(pud, addr) pmd_offset(pud, addr)
  77. #define hpmd_alloc(mm, pud, addr) pmd_alloc(mm, pud, addr)
  78. #else
  79. static inline
  80. pmd_t *hpmd_offset(pud_t *pud, unsigned long addr)
  81. {
  82. if (HPAGE_SHIFT == PAGE_SHIFT_64K)
  83. return pmd_offset(pud, addr);
  84. else
  85. return (pmd_t *) pud;
  86. }
  87. static inline
  88. pmd_t *hpmd_alloc(struct mm_struct *mm, pud_t *pud, unsigned long addr)
  89. {
  90. if (HPAGE_SHIFT == PAGE_SHIFT_64K)
  91. return pmd_alloc(mm, pud, addr);
  92. else
  93. return (pmd_t *) pud;
  94. }
  95. #endif
  96. /* Build list of addresses of gigantic pages. This function is used in early
  97. * boot before the buddy or bootmem allocator is setup.
  98. */
  99. void add_gpage(unsigned long addr, unsigned long page_size,
  100. unsigned long number_of_pages)
  101. {
  102. if (!addr)
  103. return;
  104. while (number_of_pages > 0) {
  105. gpage_freearray[nr_gpages] = addr;
  106. nr_gpages++;
  107. number_of_pages--;
  108. addr += page_size;
  109. }
  110. }
  111. /* Moves the gigantic page addresses from the temporary list to the
  112. * huge_boot_pages list. */
  113. int alloc_bootmem_huge_page(struct hstate *h)
  114. {
  115. struct huge_bootmem_page *m;
  116. if (nr_gpages == 0)
  117. return 0;
  118. m = phys_to_virt(gpage_freearray[--nr_gpages]);
  119. gpage_freearray[nr_gpages] = 0;
  120. list_add(&m->list, &huge_boot_pages);
  121. m->hstate = h;
  122. return 1;
  123. }
  124. /* Modelled after find_linux_pte() */
  125. pte_t *huge_pte_offset(struct mm_struct *mm, unsigned long addr)
  126. {
  127. pgd_t *pg;
  128. pud_t *pu;
  129. pmd_t *pm;
  130. BUG_ON(get_slice_psize(mm, addr) != mmu_huge_psize);
  131. addr &= HPAGE_MASK;
  132. pg = pgd_offset(mm, addr);
  133. if (!pgd_none(*pg)) {
  134. pu = pud_offset(pg, addr);
  135. if (!pud_none(*pu)) {
  136. pm = hpmd_offset(pu, addr);
  137. if (!pmd_none(*pm))
  138. return hugepte_offset((hugepd_t *)pm, addr);
  139. }
  140. }
  141. return NULL;
  142. }
  143. pte_t *huge_pte_alloc(struct mm_struct *mm,
  144. unsigned long addr, unsigned long sz)
  145. {
  146. pgd_t *pg;
  147. pud_t *pu;
  148. pmd_t *pm;
  149. hugepd_t *hpdp = NULL;
  150. BUG_ON(get_slice_psize(mm, addr) != mmu_huge_psize);
  151. addr &= HPAGE_MASK;
  152. pg = pgd_offset(mm, addr);
  153. pu = pud_alloc(mm, pg, addr);
  154. if (pu) {
  155. pm = hpmd_alloc(mm, pu, addr);
  156. if (pm)
  157. hpdp = (hugepd_t *)pm;
  158. }
  159. if (! hpdp)
  160. return NULL;
  161. if (hugepd_none(*hpdp) && __hugepte_alloc(mm, hpdp, addr))
  162. return NULL;
  163. return hugepte_offset(hpdp, addr);
  164. }
  165. int huge_pmd_unshare(struct mm_struct *mm, unsigned long *addr, pte_t *ptep)
  166. {
  167. return 0;
  168. }
  169. static void free_hugepte_range(struct mmu_gather *tlb, hugepd_t *hpdp)
  170. {
  171. pte_t *hugepte = hugepd_page(*hpdp);
  172. hpdp->pd = 0;
  173. tlb->need_flush = 1;
  174. pgtable_free_tlb(tlb, pgtable_free_cache(hugepte, HUGEPTE_CACHE_NUM,
  175. PGF_CACHENUM_MASK));
  176. }
  177. static void hugetlb_free_pmd_range(struct mmu_gather *tlb, pud_t *pud,
  178. unsigned long addr, unsigned long end,
  179. unsigned long floor, unsigned long ceiling)
  180. {
  181. pmd_t *pmd;
  182. unsigned long next;
  183. unsigned long start;
  184. start = addr;
  185. pmd = pmd_offset(pud, addr);
  186. do {
  187. next = pmd_addr_end(addr, end);
  188. if (pmd_none(*pmd))
  189. continue;
  190. free_hugepte_range(tlb, (hugepd_t *)pmd);
  191. } while (pmd++, addr = next, addr != end);
  192. start &= PUD_MASK;
  193. if (start < floor)
  194. return;
  195. if (ceiling) {
  196. ceiling &= PUD_MASK;
  197. if (!ceiling)
  198. return;
  199. }
  200. if (end - 1 > ceiling - 1)
  201. return;
  202. pmd = pmd_offset(pud, start);
  203. pud_clear(pud);
  204. pmd_free_tlb(tlb, pmd);
  205. }
  206. static void hugetlb_free_pud_range(struct mmu_gather *tlb, pgd_t *pgd,
  207. unsigned long addr, unsigned long end,
  208. unsigned long floor, unsigned long ceiling)
  209. {
  210. pud_t *pud;
  211. unsigned long next;
  212. unsigned long start;
  213. start = addr;
  214. pud = pud_offset(pgd, addr);
  215. do {
  216. next = pud_addr_end(addr, end);
  217. #ifdef CONFIG_PPC_64K_PAGES
  218. if (pud_none_or_clear_bad(pud))
  219. continue;
  220. hugetlb_free_pmd_range(tlb, pud, addr, next, floor, ceiling);
  221. #else
  222. if (HPAGE_SHIFT == PAGE_SHIFT_64K) {
  223. if (pud_none_or_clear_bad(pud))
  224. continue;
  225. hugetlb_free_pmd_range(tlb, pud, addr, next, floor, ceiling);
  226. } else {
  227. if (pud_none(*pud))
  228. continue;
  229. free_hugepte_range(tlb, (hugepd_t *)pud);
  230. }
  231. #endif
  232. } while (pud++, addr = next, addr != end);
  233. start &= PGDIR_MASK;
  234. if (start < floor)
  235. return;
  236. if (ceiling) {
  237. ceiling &= PGDIR_MASK;
  238. if (!ceiling)
  239. return;
  240. }
  241. if (end - 1 > ceiling - 1)
  242. return;
  243. pud = pud_offset(pgd, start);
  244. pgd_clear(pgd);
  245. pud_free_tlb(tlb, pud);
  246. }
  247. /*
  248. * This function frees user-level page tables of a process.
  249. *
  250. * Must be called with pagetable lock held.
  251. */
  252. void hugetlb_free_pgd_range(struct mmu_gather *tlb,
  253. unsigned long addr, unsigned long end,
  254. unsigned long floor, unsigned long ceiling)
  255. {
  256. pgd_t *pgd;
  257. unsigned long next;
  258. unsigned long start;
  259. /*
  260. * Comments below take from the normal free_pgd_range(). They
  261. * apply here too. The tests against HUGEPD_MASK below are
  262. * essential, because we *don't* test for this at the bottom
  263. * level. Without them we'll attempt to free a hugepte table
  264. * when we unmap just part of it, even if there are other
  265. * active mappings using it.
  266. *
  267. * The next few lines have given us lots of grief...
  268. *
  269. * Why are we testing HUGEPD* at this top level? Because
  270. * often there will be no work to do at all, and we'd prefer
  271. * not to go all the way down to the bottom just to discover
  272. * that.
  273. *
  274. * Why all these "- 1"s? Because 0 represents both the bottom
  275. * of the address space and the top of it (using -1 for the
  276. * top wouldn't help much: the masks would do the wrong thing).
  277. * The rule is that addr 0 and floor 0 refer to the bottom of
  278. * the address space, but end 0 and ceiling 0 refer to the top
  279. * Comparisons need to use "end - 1" and "ceiling - 1" (though
  280. * that end 0 case should be mythical).
  281. *
  282. * Wherever addr is brought up or ceiling brought down, we
  283. * must be careful to reject "the opposite 0" before it
  284. * confuses the subsequent tests. But what about where end is
  285. * brought down by HUGEPD_SIZE below? no, end can't go down to
  286. * 0 there.
  287. *
  288. * Whereas we round start (addr) and ceiling down, by different
  289. * masks at different levels, in order to test whether a table
  290. * now has no other vmas using it, so can be freed, we don't
  291. * bother to round floor or end up - the tests don't need that.
  292. */
  293. addr &= HUGEPD_MASK;
  294. if (addr < floor) {
  295. addr += HUGEPD_SIZE;
  296. if (!addr)
  297. return;
  298. }
  299. if (ceiling) {
  300. ceiling &= HUGEPD_MASK;
  301. if (!ceiling)
  302. return;
  303. }
  304. if (end - 1 > ceiling - 1)
  305. end -= HUGEPD_SIZE;
  306. if (addr > end - 1)
  307. return;
  308. start = addr;
  309. pgd = pgd_offset(tlb->mm, addr);
  310. do {
  311. BUG_ON(get_slice_psize(tlb->mm, addr) != mmu_huge_psize);
  312. next = pgd_addr_end(addr, end);
  313. if (pgd_none_or_clear_bad(pgd))
  314. continue;
  315. hugetlb_free_pud_range(tlb, pgd, addr, next, floor, ceiling);
  316. } while (pgd++, addr = next, addr != end);
  317. }
  318. void set_huge_pte_at(struct mm_struct *mm, unsigned long addr,
  319. pte_t *ptep, pte_t pte)
  320. {
  321. if (pte_present(*ptep)) {
  322. /* We open-code pte_clear because we need to pass the right
  323. * argument to hpte_need_flush (huge / !huge). Might not be
  324. * necessary anymore if we make hpte_need_flush() get the
  325. * page size from the slices
  326. */
  327. pte_update(mm, addr & HPAGE_MASK, ptep, ~0UL, 1);
  328. }
  329. *ptep = __pte(pte_val(pte) & ~_PAGE_HPTEFLAGS);
  330. }
  331. pte_t huge_ptep_get_and_clear(struct mm_struct *mm, unsigned long addr,
  332. pte_t *ptep)
  333. {
  334. unsigned long old = pte_update(mm, addr, ptep, ~0UL, 1);
  335. return __pte(old);
  336. }
  337. struct page *
  338. follow_huge_addr(struct mm_struct *mm, unsigned long address, int write)
  339. {
  340. pte_t *ptep;
  341. struct page *page;
  342. if (get_slice_psize(mm, address) != mmu_huge_psize)
  343. return ERR_PTR(-EINVAL);
  344. ptep = huge_pte_offset(mm, address);
  345. page = pte_page(*ptep);
  346. if (page)
  347. page += (address % HPAGE_SIZE) / PAGE_SIZE;
  348. return page;
  349. }
  350. int pmd_huge(pmd_t pmd)
  351. {
  352. return 0;
  353. }
  354. int pud_huge(pud_t pud)
  355. {
  356. return 0;
  357. }
  358. struct page *
  359. follow_huge_pmd(struct mm_struct *mm, unsigned long address,
  360. pmd_t *pmd, int write)
  361. {
  362. BUG();
  363. return NULL;
  364. }
  365. unsigned long hugetlb_get_unmapped_area(struct file *file, unsigned long addr,
  366. unsigned long len, unsigned long pgoff,
  367. unsigned long flags)
  368. {
  369. return slice_get_unmapped_area(addr, len, flags,
  370. mmu_huge_psize, 1, 0);
  371. }
  372. /*
  373. * Called by asm hashtable.S for doing lazy icache flush
  374. */
  375. static unsigned int hash_huge_page_do_lazy_icache(unsigned long rflags,
  376. pte_t pte, int trap)
  377. {
  378. struct page *page;
  379. int i;
  380. if (!pfn_valid(pte_pfn(pte)))
  381. return rflags;
  382. page = pte_page(pte);
  383. /* page is dirty */
  384. if (!test_bit(PG_arch_1, &page->flags) && !PageReserved(page)) {
  385. if (trap == 0x400) {
  386. for (i = 0; i < (HPAGE_SIZE / PAGE_SIZE); i++)
  387. __flush_dcache_icache(page_address(page+i));
  388. set_bit(PG_arch_1, &page->flags);
  389. } else {
  390. rflags |= HPTE_R_N;
  391. }
  392. }
  393. return rflags;
  394. }
  395. int hash_huge_page(struct mm_struct *mm, unsigned long access,
  396. unsigned long ea, unsigned long vsid, int local,
  397. unsigned long trap)
  398. {
  399. pte_t *ptep;
  400. unsigned long old_pte, new_pte;
  401. unsigned long va, rflags, pa;
  402. long slot;
  403. int err = 1;
  404. int ssize = user_segment_size(ea);
  405. ptep = huge_pte_offset(mm, ea);
  406. /* Search the Linux page table for a match with va */
  407. va = hpt_va(ea, vsid, ssize);
  408. /*
  409. * If no pte found or not present, send the problem up to
  410. * do_page_fault
  411. */
  412. if (unlikely(!ptep || pte_none(*ptep)))
  413. goto out;
  414. /*
  415. * Check the user's access rights to the page. If access should be
  416. * prevented then send the problem up to do_page_fault.
  417. */
  418. if (unlikely(access & ~pte_val(*ptep)))
  419. goto out;
  420. /*
  421. * At this point, we have a pte (old_pte) which can be used to build
  422. * or update an HPTE. There are 2 cases:
  423. *
  424. * 1. There is a valid (present) pte with no associated HPTE (this is
  425. * the most common case)
  426. * 2. There is a valid (present) pte with an associated HPTE. The
  427. * current values of the pp bits in the HPTE prevent access
  428. * because we are doing software DIRTY bit management and the
  429. * page is currently not DIRTY.
  430. */
  431. do {
  432. old_pte = pte_val(*ptep);
  433. if (old_pte & _PAGE_BUSY)
  434. goto out;
  435. new_pte = old_pte | _PAGE_BUSY | _PAGE_ACCESSED;
  436. } while(old_pte != __cmpxchg_u64((unsigned long *)ptep,
  437. old_pte, new_pte));
  438. rflags = 0x2 | (!(new_pte & _PAGE_RW));
  439. /* _PAGE_EXEC -> HW_NO_EXEC since it's inverted */
  440. rflags |= ((new_pte & _PAGE_EXEC) ? 0 : HPTE_R_N);
  441. if (!cpu_has_feature(CPU_FTR_COHERENT_ICACHE))
  442. /* No CPU has hugepages but lacks no execute, so we
  443. * don't need to worry about that case */
  444. rflags = hash_huge_page_do_lazy_icache(rflags, __pte(old_pte),
  445. trap);
  446. /* Check if pte already has an hpte (case 2) */
  447. if (unlikely(old_pte & _PAGE_HASHPTE)) {
  448. /* There MIGHT be an HPTE for this pte */
  449. unsigned long hash, slot;
  450. hash = hpt_hash(va, HPAGE_SHIFT, ssize);
  451. if (old_pte & _PAGE_F_SECOND)
  452. hash = ~hash;
  453. slot = (hash & htab_hash_mask) * HPTES_PER_GROUP;
  454. slot += (old_pte & _PAGE_F_GIX) >> 12;
  455. if (ppc_md.hpte_updatepp(slot, rflags, va, mmu_huge_psize,
  456. ssize, local) == -1)
  457. old_pte &= ~_PAGE_HPTEFLAGS;
  458. }
  459. if (likely(!(old_pte & _PAGE_HASHPTE))) {
  460. unsigned long hash = hpt_hash(va, HPAGE_SHIFT, ssize);
  461. unsigned long hpte_group;
  462. pa = pte_pfn(__pte(old_pte)) << PAGE_SHIFT;
  463. repeat:
  464. hpte_group = ((hash & htab_hash_mask) *
  465. HPTES_PER_GROUP) & ~0x7UL;
  466. /* clear HPTE slot informations in new PTE */
  467. #ifdef CONFIG_PPC_64K_PAGES
  468. new_pte = (new_pte & ~_PAGE_HPTEFLAGS) | _PAGE_HPTE_SUB0;
  469. #else
  470. new_pte = (new_pte & ~_PAGE_HPTEFLAGS) | _PAGE_HASHPTE;
  471. #endif
  472. /* Add in WIMG bits */
  473. rflags |= (new_pte & (_PAGE_WRITETHRU | _PAGE_NO_CACHE |
  474. _PAGE_COHERENT | _PAGE_GUARDED));
  475. /* Insert into the hash table, primary slot */
  476. slot = ppc_md.hpte_insert(hpte_group, va, pa, rflags, 0,
  477. mmu_huge_psize, ssize);
  478. /* Primary is full, try the secondary */
  479. if (unlikely(slot == -1)) {
  480. hpte_group = ((~hash & htab_hash_mask) *
  481. HPTES_PER_GROUP) & ~0x7UL;
  482. slot = ppc_md.hpte_insert(hpte_group, va, pa, rflags,
  483. HPTE_V_SECONDARY,
  484. mmu_huge_psize, ssize);
  485. if (slot == -1) {
  486. if (mftb() & 0x1)
  487. hpte_group = ((hash & htab_hash_mask) *
  488. HPTES_PER_GROUP)&~0x7UL;
  489. ppc_md.hpte_remove(hpte_group);
  490. goto repeat;
  491. }
  492. }
  493. if (unlikely(slot == -2))
  494. panic("hash_huge_page: pte_insert failed\n");
  495. new_pte |= (slot << 12) & (_PAGE_F_SECOND | _PAGE_F_GIX);
  496. }
  497. /*
  498. * No need to use ldarx/stdcx here
  499. */
  500. *ptep = __pte(new_pte & ~_PAGE_BUSY);
  501. err = 0;
  502. out:
  503. return err;
  504. }
  505. void set_huge_psize(int psize)
  506. {
  507. /* Check that it is a page size supported by the hardware and
  508. * that it fits within pagetable limits. */
  509. if (mmu_psize_defs[psize].shift &&
  510. mmu_psize_defs[psize].shift < SID_SHIFT_1T &&
  511. (mmu_psize_defs[psize].shift > MIN_HUGEPTE_SHIFT ||
  512. mmu_psize_defs[psize].shift == PAGE_SHIFT_64K ||
  513. mmu_psize_defs[psize].shift == PAGE_SHIFT_16G)) {
  514. /* Return if huge page size is the same as the
  515. * base page size. */
  516. if (mmu_psize_defs[psize].shift == PAGE_SHIFT)
  517. return;
  518. HPAGE_SHIFT = mmu_psize_defs[psize].shift;
  519. mmu_huge_psize = psize;
  520. switch (HPAGE_SHIFT) {
  521. case PAGE_SHIFT_64K:
  522. /* We only allow 64k hpages with 4k base page,
  523. * which was checked above, and always put them
  524. * at the PMD */
  525. hugepte_shift = PMD_SHIFT;
  526. break;
  527. case PAGE_SHIFT_16M:
  528. /* 16M pages can be at two different levels
  529. * of pagestables based on base page size */
  530. if (PAGE_SHIFT == PAGE_SHIFT_64K)
  531. hugepte_shift = PMD_SHIFT;
  532. else /* 4k base page */
  533. hugepte_shift = PUD_SHIFT;
  534. break;
  535. case PAGE_SHIFT_16G:
  536. /* 16G pages are always at PGD level */
  537. hugepte_shift = PGDIR_SHIFT;
  538. break;
  539. }
  540. hugepte_shift -= HPAGE_SHIFT;
  541. } else
  542. HPAGE_SHIFT = 0;
  543. }
  544. static int __init hugepage_setup_sz(char *str)
  545. {
  546. unsigned long long size;
  547. int mmu_psize = -1;
  548. int shift;
  549. size = memparse(str, &str);
  550. shift = __ffs(size);
  551. switch (shift) {
  552. #ifndef CONFIG_PPC_64K_PAGES
  553. case PAGE_SHIFT_64K:
  554. mmu_psize = MMU_PAGE_64K;
  555. break;
  556. #endif
  557. case PAGE_SHIFT_16M:
  558. mmu_psize = MMU_PAGE_16M;
  559. break;
  560. case PAGE_SHIFT_16G:
  561. mmu_psize = MMU_PAGE_16G;
  562. break;
  563. }
  564. if (mmu_psize >= 0 && mmu_psize_defs[mmu_psize].shift) {
  565. set_huge_psize(mmu_psize);
  566. hugetlb_add_hstate(shift - PAGE_SHIFT);
  567. }
  568. else
  569. printk(KERN_WARNING "Invalid huge page size specified(%llu)\n", size);
  570. return 1;
  571. }
  572. __setup("hugepagesz=", hugepage_setup_sz);
  573. static void zero_ctor(struct kmem_cache *cache, void *addr)
  574. {
  575. memset(addr, 0, kmem_cache_size(cache));
  576. }
  577. static int __init hugetlbpage_init(void)
  578. {
  579. if (!cpu_has_feature(CPU_FTR_16M_PAGE))
  580. return -ENODEV;
  581. huge_pgtable_cache = kmem_cache_create("hugepte_cache",
  582. HUGEPTE_TABLE_SIZE,
  583. HUGEPTE_TABLE_SIZE,
  584. 0,
  585. zero_ctor);
  586. if (! huge_pgtable_cache)
  587. panic("hugetlbpage_init(): could not create hugepte cache\n");
  588. return 0;
  589. }
  590. module_init(hugetlbpage_init);