2008-04-30 18:38:46 +07:00
|
|
|
/*
|
|
|
|
* IBM System z Huge TLB Page Support for Kernel.
|
|
|
|
*
|
2012-07-20 16:15:04 +07:00
|
|
|
* Copyright IBM Corp. 2007
|
2008-04-30 18:38:46 +07:00
|
|
|
* Author(s): Gerald Schaefer <gerald.schaefer@de.ibm.com>
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <linux/mm.h>
|
|
|
|
#include <linux/hugetlb.h>
|
|
|
|
|
2013-07-24 01:57:57 +07:00
|
|
|
static inline pmd_t __pte_to_pmd(pte_t pte)
|
|
|
|
{
|
|
|
|
pmd_t pmd;
|
|
|
|
|
|
|
|
/*
|
2014-07-24 16:03:41 +07:00
|
|
|
* Convert encoding pte bits pmd bits
|
|
|
|
* .IR...wrdytp dy..R...I...wr
|
|
|
|
* empty .10...000000 -> 00..0...1...00
|
|
|
|
* prot-none, clean, old .11...000001 -> 00..1...1...00
|
|
|
|
* prot-none, clean, young .11...000101 -> 01..1...1...00
|
|
|
|
* prot-none, dirty, old .10...001001 -> 10..1...1...00
|
|
|
|
* prot-none, dirty, young .10...001101 -> 11..1...1...00
|
|
|
|
* read-only, clean, old .11...010001 -> 00..1...1...01
|
|
|
|
* read-only, clean, young .01...010101 -> 01..1...0...01
|
|
|
|
* read-only, dirty, old .11...011001 -> 10..1...1...01
|
|
|
|
* read-only, dirty, young .01...011101 -> 11..1...0...01
|
|
|
|
* read-write, clean, old .11...110001 -> 00..0...1...11
|
|
|
|
* read-write, clean, young .01...110101 -> 01..0...0...11
|
|
|
|
* read-write, dirty, old .10...111001 -> 10..0...1...11
|
|
|
|
* read-write, dirty, young .00...111101 -> 11..0...0...11
|
2013-07-24 01:57:57 +07:00
|
|
|
*/
|
|
|
|
if (pte_present(pte)) {
|
|
|
|
pmd_val(pmd) = pte_val(pte) & PAGE_MASK;
|
2014-07-24 16:03:41 +07:00
|
|
|
pmd_val(pmd) |= (pte_val(pte) & _PAGE_READ) >> 4;
|
|
|
|
pmd_val(pmd) |= (pte_val(pte) & _PAGE_WRITE) >> 4;
|
|
|
|
pmd_val(pmd) |= (pte_val(pte) & _PAGE_INVALID) >> 5;
|
|
|
|
pmd_val(pmd) |= (pte_val(pte) & _PAGE_PROTECT);
|
|
|
|
pmd_val(pmd) |= (pte_val(pte) & _PAGE_DIRTY) << 10;
|
|
|
|
pmd_val(pmd) |= (pte_val(pte) & _PAGE_YOUNG) << 10;
|
2013-07-24 01:57:57 +07:00
|
|
|
} else
|
|
|
|
pmd_val(pmd) = _SEGMENT_ENTRY_INVALID;
|
|
|
|
return pmd;
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline pte_t __pmd_to_pte(pmd_t pmd)
|
|
|
|
{
|
|
|
|
pte_t pte;
|
|
|
|
|
|
|
|
/*
|
2014-07-24 16:03:41 +07:00
|
|
|
* Convert encoding pmd bits pte bits
|
|
|
|
* dy..R...I...wr .IR...wrdytp
|
|
|
|
* empty 00..0...1...00 -> .10...001100
|
|
|
|
* prot-none, clean, old 00..0...1...00 -> .10...000001
|
|
|
|
* prot-none, clean, young 01..0...1...00 -> .10...000101
|
|
|
|
* prot-none, dirty, old 10..0...1...00 -> .10...001001
|
|
|
|
* prot-none, dirty, young 11..0...1...00 -> .10...001101
|
|
|
|
* read-only, clean, old 00..1...1...01 -> .11...010001
|
|
|
|
* read-only, clean, young 01..1...1...01 -> .11...010101
|
|
|
|
* read-only, dirty, old 10..1...1...01 -> .11...011001
|
|
|
|
* read-only, dirty, young 11..1...1...01 -> .11...011101
|
|
|
|
* read-write, clean, old 00..0...1...11 -> .10...110001
|
|
|
|
* read-write, clean, young 01..0...1...11 -> .10...110101
|
|
|
|
* read-write, dirty, old 10..0...1...11 -> .10...111001
|
|
|
|
* read-write, dirty, young 11..0...1...11 -> .10...111101
|
2013-07-24 01:57:57 +07:00
|
|
|
*/
|
|
|
|
if (pmd_present(pmd)) {
|
2014-07-24 16:03:41 +07:00
|
|
|
pte_val(pte) = pmd_val(pmd) & _SEGMENT_ENTRY_ORIGIN_LARGE;
|
|
|
|
pte_val(pte) |= _PAGE_LARGE | _PAGE_PRESENT;
|
|
|
|
pte_val(pte) |= (pmd_val(pmd) & _SEGMENT_ENTRY_READ) << 4;
|
|
|
|
pte_val(pte) |= (pmd_val(pmd) & _SEGMENT_ENTRY_WRITE) << 4;
|
|
|
|
pte_val(pte) |= (pmd_val(pmd) & _SEGMENT_ENTRY_INVALID) << 5;
|
|
|
|
pte_val(pte) |= (pmd_val(pmd) & _SEGMENT_ENTRY_PROTECT);
|
|
|
|
pmd_val(pmd) |= (pte_val(pte) & _PAGE_DIRTY) << 10;
|
|
|
|
pmd_val(pmd) |= (pte_val(pte) & _PAGE_YOUNG) << 10;
|
2013-07-24 01:57:57 +07:00
|
|
|
} else
|
|
|
|
pte_val(pte) = _PAGE_INVALID;
|
|
|
|
return pte;
|
|
|
|
}
|
2008-04-30 18:38:46 +07:00
|
|
|
|
|
|
|
void set_huge_pte_at(struct mm_struct *mm, unsigned long addr,
|
2013-07-24 01:57:57 +07:00
|
|
|
pte_t *ptep, pte_t pte)
|
2008-04-30 18:38:46 +07:00
|
|
|
{
|
2013-07-24 01:57:57 +07:00
|
|
|
pmd_t pmd;
|
2008-04-30 18:38:46 +07:00
|
|
|
|
2013-07-24 01:57:57 +07:00
|
|
|
pmd = __pte_to_pmd(pte);
|
2008-04-30 18:38:46 +07:00
|
|
|
if (!MACHINE_HAS_HPAGE) {
|
2014-07-24 16:03:41 +07:00
|
|
|
/* Emulated huge ptes loose the dirty and young bit */
|
2013-07-24 01:57:57 +07:00
|
|
|
pmd_val(pmd) &= ~_SEGMENT_ENTRY_ORIGIN;
|
|
|
|
pmd_val(pmd) |= pte_page(pte)[1].index;
|
|
|
|
} else
|
2014-09-22 13:50:51 +07:00
|
|
|
pmd_val(pmd) |= _SEGMENT_ENTRY_LARGE;
|
2013-07-24 01:57:57 +07:00
|
|
|
*(pmd_t *) ptep = pmd;
|
|
|
|
}
|
|
|
|
|
|
|
|
pte_t huge_ptep_get(pte_t *ptep)
|
|
|
|
{
|
|
|
|
unsigned long origin;
|
|
|
|
pmd_t pmd;
|
|
|
|
|
|
|
|
pmd = *(pmd_t *) ptep;
|
|
|
|
if (!MACHINE_HAS_HPAGE && pmd_present(pmd)) {
|
|
|
|
origin = pmd_val(pmd) & _SEGMENT_ENTRY_ORIGIN;
|
|
|
|
pmd_val(pmd) &= ~_SEGMENT_ENTRY_ORIGIN;
|
|
|
|
pmd_val(pmd) |= *(unsigned long *) origin;
|
2014-07-24 16:03:41 +07:00
|
|
|
/* Emulated huge ptes are young and dirty by definition */
|
|
|
|
pmd_val(pmd) |= _SEGMENT_ENTRY_YOUNG | _SEGMENT_ENTRY_DIRTY;
|
2008-04-30 18:38:46 +07:00
|
|
|
}
|
2013-07-24 01:57:57 +07:00
|
|
|
return __pmd_to_pte(pmd);
|
|
|
|
}
|
|
|
|
|
|
|
|
pte_t huge_ptep_get_and_clear(struct mm_struct *mm,
|
|
|
|
unsigned long addr, pte_t *ptep)
|
|
|
|
{
|
|
|
|
pmd_t *pmdp = (pmd_t *) ptep;
|
|
|
|
pte_t pte = huge_ptep_get(ptep);
|
2008-04-30 18:38:46 +07:00
|
|
|
|
2014-04-03 18:55:01 +07:00
|
|
|
pmdp_flush_direct(mm, addr, pmdp);
|
2013-07-24 01:57:57 +07:00
|
|
|
pmd_val(*pmdp) = _SEGMENT_ENTRY_EMPTY;
|
|
|
|
return pte;
|
2008-04-30 18:38:46 +07:00
|
|
|
}
|
|
|
|
|
|
|
|
int arch_prepare_hugepage(struct page *page)
|
|
|
|
{
|
|
|
|
unsigned long addr = page_to_phys(page);
|
|
|
|
pte_t pte;
|
|
|
|
pte_t *ptep;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
if (MACHINE_HAS_HPAGE)
|
|
|
|
return 0;
|
|
|
|
|
2011-07-24 15:48:20 +07:00
|
|
|
ptep = (pte_t *) pte_alloc_one(&init_mm, addr);
|
2008-04-30 18:38:46 +07:00
|
|
|
if (!ptep)
|
|
|
|
return -ENOMEM;
|
|
|
|
|
2013-04-30 05:07:23 +07:00
|
|
|
pte_val(pte) = addr;
|
2008-04-30 18:38:46 +07:00
|
|
|
for (i = 0; i < PTRS_PER_PTE; i++) {
|
|
|
|
set_pte_at(&init_mm, addr + i * PAGE_SIZE, ptep + i, pte);
|
|
|
|
pte_val(pte) += PAGE_SIZE;
|
|
|
|
}
|
|
|
|
page[1].index = (unsigned long) ptep;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void arch_release_hugepage(struct page *page)
|
|
|
|
{
|
|
|
|
pte_t *ptep;
|
|
|
|
|
|
|
|
if (MACHINE_HAS_HPAGE)
|
|
|
|
return;
|
|
|
|
|
|
|
|
ptep = (pte_t *) page[1].index;
|
|
|
|
if (!ptep)
|
|
|
|
return;
|
2013-07-24 01:57:57 +07:00
|
|
|
clear_table((unsigned long *) ptep, _PAGE_INVALID,
|
2012-05-09 21:27:37 +07:00
|
|
|
PTRS_PER_PTE * sizeof(pte_t));
|
2010-10-25 21:10:11 +07:00
|
|
|
page_table_free(&init_mm, (unsigned long *) ptep);
|
2008-04-30 18:38:46 +07:00
|
|
|
page[1].index = 0;
|
|
|
|
}
|
|
|
|
|
2008-07-24 11:27:41 +07:00
|
|
|
pte_t *huge_pte_alloc(struct mm_struct *mm,
|
|
|
|
unsigned long addr, unsigned long sz)
|
2008-04-30 18:38:46 +07:00
|
|
|
{
|
|
|
|
pgd_t *pgdp;
|
|
|
|
pud_t *pudp;
|
|
|
|
pmd_t *pmdp = NULL;
|
|
|
|
|
|
|
|
pgdp = pgd_offset(mm, addr);
|
|
|
|
pudp = pud_alloc(mm, pgdp, addr);
|
|
|
|
if (pudp)
|
|
|
|
pmdp = pmd_alloc(mm, pudp, addr);
|
|
|
|
return (pte_t *) pmdp;
|
|
|
|
}
|
|
|
|
|
|
|
|
pte_t *huge_pte_offset(struct mm_struct *mm, unsigned long addr)
|
|
|
|
{
|
|
|
|
pgd_t *pgdp;
|
|
|
|
pud_t *pudp;
|
|
|
|
pmd_t *pmdp = NULL;
|
|
|
|
|
|
|
|
pgdp = pgd_offset(mm, addr);
|
|
|
|
if (pgd_present(*pgdp)) {
|
|
|
|
pudp = pud_offset(pgdp, addr);
|
|
|
|
if (pud_present(*pudp))
|
|
|
|
pmdp = pmd_offset(pudp, addr);
|
|
|
|
}
|
|
|
|
return (pte_t *) pmdp;
|
|
|
|
}
|
|
|
|
|
|
|
|
int huge_pmd_unshare(struct mm_struct *mm, unsigned long *addr, pte_t *ptep)
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
struct page *follow_huge_addr(struct mm_struct *mm, unsigned long address,
|
|
|
|
int write)
|
|
|
|
{
|
|
|
|
return ERR_PTR(-EINVAL);
|
|
|
|
}
|
|
|
|
|
|
|
|
int pmd_huge(pmd_t pmd)
|
|
|
|
{
|
|
|
|
if (!MACHINE_HAS_HPAGE)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
return !!(pmd_val(pmd) & _SEGMENT_ENTRY_LARGE);
|
|
|
|
}
|
|
|
|
|
2008-07-24 11:27:50 +07:00
|
|
|
int pud_huge(pud_t pud)
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2008-04-30 18:38:46 +07:00
|
|
|
struct page *follow_huge_pmd(struct mm_struct *mm, unsigned long address,
|
|
|
|
pmd_t *pmdp, int write)
|
|
|
|
{
|
|
|
|
struct page *page;
|
|
|
|
|
|
|
|
if (!MACHINE_HAS_HPAGE)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
page = pmd_page(*pmdp);
|
|
|
|
if (page)
|
|
|
|
page += ((address & ~HPAGE_MASK) >> PAGE_SHIFT);
|
|
|
|
return page;
|
|
|
|
}
|