18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci * IA-64 Huge TLB Page Support for Kernel.
48c2ecf20Sopenharmony_ci *
58c2ecf20Sopenharmony_ci * Copyright (C) 2002-2004 Rohit Seth <rohit.seth@intel.com>
68c2ecf20Sopenharmony_ci * Copyright (C) 2003-2004 Ken Chen <kenneth.w.chen@intel.com>
78c2ecf20Sopenharmony_ci *
88c2ecf20Sopenharmony_ci * Sep, 2003: add numa support
98c2ecf20Sopenharmony_ci * Feb, 2004: dynamic hugetlb page size via boot parameter
108c2ecf20Sopenharmony_ci */
118c2ecf20Sopenharmony_ci
128c2ecf20Sopenharmony_ci#include <linux/init.h>
138c2ecf20Sopenharmony_ci#include <linux/fs.h>
148c2ecf20Sopenharmony_ci#include <linux/mm.h>
158c2ecf20Sopenharmony_ci#include <linux/hugetlb.h>
168c2ecf20Sopenharmony_ci#include <linux/pagemap.h>
178c2ecf20Sopenharmony_ci#include <linux/module.h>
188c2ecf20Sopenharmony_ci#include <linux/sysctl.h>
198c2ecf20Sopenharmony_ci#include <linux/log2.h>
208c2ecf20Sopenharmony_ci#include <asm/mman.h>
218c2ecf20Sopenharmony_ci#include <asm/tlb.h>
228c2ecf20Sopenharmony_ci#include <asm/tlbflush.h>
238c2ecf20Sopenharmony_ci
248c2ecf20Sopenharmony_ciunsigned int hpage_shift = HPAGE_SHIFT_DEFAULT;
258c2ecf20Sopenharmony_ciEXPORT_SYMBOL(hpage_shift);
268c2ecf20Sopenharmony_ci
278c2ecf20Sopenharmony_cipte_t *
288c2ecf20Sopenharmony_cihuge_pte_alloc(struct mm_struct *mm, unsigned long addr, unsigned long sz)
298c2ecf20Sopenharmony_ci{
308c2ecf20Sopenharmony_ci	unsigned long taddr = htlbpage_to_page(addr);
318c2ecf20Sopenharmony_ci	pgd_t *pgd;
328c2ecf20Sopenharmony_ci	p4d_t *p4d;
338c2ecf20Sopenharmony_ci	pud_t *pud;
348c2ecf20Sopenharmony_ci	pmd_t *pmd;
358c2ecf20Sopenharmony_ci	pte_t *pte = NULL;
368c2ecf20Sopenharmony_ci
378c2ecf20Sopenharmony_ci	pgd = pgd_offset(mm, taddr);
388c2ecf20Sopenharmony_ci	p4d = p4d_offset(pgd, taddr);
398c2ecf20Sopenharmony_ci	pud = pud_alloc(mm, p4d, taddr);
408c2ecf20Sopenharmony_ci	if (pud) {
418c2ecf20Sopenharmony_ci		pmd = pmd_alloc(mm, pud, taddr);
428c2ecf20Sopenharmony_ci		if (pmd)
438c2ecf20Sopenharmony_ci			pte = pte_alloc_map(mm, pmd, taddr);
448c2ecf20Sopenharmony_ci	}
458c2ecf20Sopenharmony_ci	return pte;
468c2ecf20Sopenharmony_ci}
478c2ecf20Sopenharmony_ci
488c2ecf20Sopenharmony_cipte_t *
498c2ecf20Sopenharmony_cihuge_pte_offset (struct mm_struct *mm, unsigned long addr, unsigned long sz)
508c2ecf20Sopenharmony_ci{
518c2ecf20Sopenharmony_ci	unsigned long taddr = htlbpage_to_page(addr);
528c2ecf20Sopenharmony_ci	pgd_t *pgd;
538c2ecf20Sopenharmony_ci	p4d_t *p4d;
548c2ecf20Sopenharmony_ci	pud_t *pud;
558c2ecf20Sopenharmony_ci	pmd_t *pmd;
568c2ecf20Sopenharmony_ci	pte_t *pte = NULL;
578c2ecf20Sopenharmony_ci
588c2ecf20Sopenharmony_ci	pgd = pgd_offset(mm, taddr);
598c2ecf20Sopenharmony_ci	if (pgd_present(*pgd)) {
608c2ecf20Sopenharmony_ci		p4d = p4d_offset(pgd, taddr);
618c2ecf20Sopenharmony_ci		if (p4d_present(*p4d)) {
628c2ecf20Sopenharmony_ci			pud = pud_offset(p4d, taddr);
638c2ecf20Sopenharmony_ci			if (pud_present(*pud)) {
648c2ecf20Sopenharmony_ci				pmd = pmd_offset(pud, taddr);
658c2ecf20Sopenharmony_ci				if (pmd_present(*pmd))
668c2ecf20Sopenharmony_ci					pte = pte_offset_map(pmd, taddr);
678c2ecf20Sopenharmony_ci			}
688c2ecf20Sopenharmony_ci		}
698c2ecf20Sopenharmony_ci	}
708c2ecf20Sopenharmony_ci
718c2ecf20Sopenharmony_ci	return pte;
728c2ecf20Sopenharmony_ci}
738c2ecf20Sopenharmony_ci
748c2ecf20Sopenharmony_ci#define mk_pte_huge(entry) { pte_val(entry) |= _PAGE_P; }
758c2ecf20Sopenharmony_ci
768c2ecf20Sopenharmony_ci/*
778c2ecf20Sopenharmony_ci * Don't actually need to do any preparation, but need to make sure
788c2ecf20Sopenharmony_ci * the address is in the right region.
798c2ecf20Sopenharmony_ci */
808c2ecf20Sopenharmony_ciint prepare_hugepage_range(struct file *file,
818c2ecf20Sopenharmony_ci			unsigned long addr, unsigned long len)
828c2ecf20Sopenharmony_ci{
838c2ecf20Sopenharmony_ci	if (len & ~HPAGE_MASK)
848c2ecf20Sopenharmony_ci		return -EINVAL;
858c2ecf20Sopenharmony_ci	if (addr & ~HPAGE_MASK)
868c2ecf20Sopenharmony_ci		return -EINVAL;
878c2ecf20Sopenharmony_ci	if (REGION_NUMBER(addr) != RGN_HPAGE)
888c2ecf20Sopenharmony_ci		return -EINVAL;
898c2ecf20Sopenharmony_ci
908c2ecf20Sopenharmony_ci	return 0;
918c2ecf20Sopenharmony_ci}
928c2ecf20Sopenharmony_ci
938c2ecf20Sopenharmony_cistruct page *follow_huge_addr(struct mm_struct *mm, unsigned long addr, int write)
948c2ecf20Sopenharmony_ci{
958c2ecf20Sopenharmony_ci	struct page *page;
968c2ecf20Sopenharmony_ci	pte_t *ptep;
978c2ecf20Sopenharmony_ci
988c2ecf20Sopenharmony_ci	if (REGION_NUMBER(addr) != RGN_HPAGE)
998c2ecf20Sopenharmony_ci		return ERR_PTR(-EINVAL);
1008c2ecf20Sopenharmony_ci
1018c2ecf20Sopenharmony_ci	ptep = huge_pte_offset(mm, addr, HPAGE_SIZE);
1028c2ecf20Sopenharmony_ci	if (!ptep || pte_none(*ptep))
1038c2ecf20Sopenharmony_ci		return NULL;
1048c2ecf20Sopenharmony_ci	page = pte_page(*ptep);
1058c2ecf20Sopenharmony_ci	page += ((addr & ~HPAGE_MASK) >> PAGE_SHIFT);
1068c2ecf20Sopenharmony_ci	return page;
1078c2ecf20Sopenharmony_ci}
1088c2ecf20Sopenharmony_ciint pmd_huge(pmd_t pmd)
1098c2ecf20Sopenharmony_ci{
1108c2ecf20Sopenharmony_ci	return 0;
1118c2ecf20Sopenharmony_ci}
1128c2ecf20Sopenharmony_ci
1138c2ecf20Sopenharmony_ciint pud_huge(pud_t pud)
1148c2ecf20Sopenharmony_ci{
1158c2ecf20Sopenharmony_ci	return 0;
1168c2ecf20Sopenharmony_ci}
1178c2ecf20Sopenharmony_ci
1188c2ecf20Sopenharmony_civoid hugetlb_free_pgd_range(struct mmu_gather *tlb,
1198c2ecf20Sopenharmony_ci			unsigned long addr, unsigned long end,
1208c2ecf20Sopenharmony_ci			unsigned long floor, unsigned long ceiling)
1218c2ecf20Sopenharmony_ci{
1228c2ecf20Sopenharmony_ci	/*
1238c2ecf20Sopenharmony_ci	 * This is called to free hugetlb page tables.
1248c2ecf20Sopenharmony_ci	 *
1258c2ecf20Sopenharmony_ci	 * The offset of these addresses from the base of the hugetlb
1268c2ecf20Sopenharmony_ci	 * region must be scaled down by HPAGE_SIZE/PAGE_SIZE so that
1278c2ecf20Sopenharmony_ci	 * the standard free_pgd_range will free the right page tables.
1288c2ecf20Sopenharmony_ci	 *
1298c2ecf20Sopenharmony_ci	 * If floor and ceiling are also in the hugetlb region, they
1308c2ecf20Sopenharmony_ci	 * must likewise be scaled down; but if outside, left unchanged.
1318c2ecf20Sopenharmony_ci	 */
1328c2ecf20Sopenharmony_ci
1338c2ecf20Sopenharmony_ci	addr = htlbpage_to_page(addr);
1348c2ecf20Sopenharmony_ci	end  = htlbpage_to_page(end);
1358c2ecf20Sopenharmony_ci	if (REGION_NUMBER(floor) == RGN_HPAGE)
1368c2ecf20Sopenharmony_ci		floor = htlbpage_to_page(floor);
1378c2ecf20Sopenharmony_ci	if (REGION_NUMBER(ceiling) == RGN_HPAGE)
1388c2ecf20Sopenharmony_ci		ceiling = htlbpage_to_page(ceiling);
1398c2ecf20Sopenharmony_ci
1408c2ecf20Sopenharmony_ci	free_pgd_range(tlb, addr, end, floor, ceiling);
1418c2ecf20Sopenharmony_ci}
1428c2ecf20Sopenharmony_ci
1438c2ecf20Sopenharmony_ciunsigned long hugetlb_get_unmapped_area(struct file *file, unsigned long addr, unsigned long len,
1448c2ecf20Sopenharmony_ci		unsigned long pgoff, unsigned long flags)
1458c2ecf20Sopenharmony_ci{
1468c2ecf20Sopenharmony_ci	struct vm_unmapped_area_info info;
1478c2ecf20Sopenharmony_ci
1488c2ecf20Sopenharmony_ci	if (len > RGN_MAP_LIMIT)
1498c2ecf20Sopenharmony_ci		return -ENOMEM;
1508c2ecf20Sopenharmony_ci	if (len & ~HPAGE_MASK)
1518c2ecf20Sopenharmony_ci		return -EINVAL;
1528c2ecf20Sopenharmony_ci
1538c2ecf20Sopenharmony_ci	/* Handle MAP_FIXED */
1548c2ecf20Sopenharmony_ci	if (flags & MAP_FIXED) {
1558c2ecf20Sopenharmony_ci		if (prepare_hugepage_range(file, addr, len))
1568c2ecf20Sopenharmony_ci			return -EINVAL;
1578c2ecf20Sopenharmony_ci		return addr;
1588c2ecf20Sopenharmony_ci	}
1598c2ecf20Sopenharmony_ci
1608c2ecf20Sopenharmony_ci	/* This code assumes that RGN_HPAGE != 0. */
1618c2ecf20Sopenharmony_ci	if ((REGION_NUMBER(addr) != RGN_HPAGE) || (addr & (HPAGE_SIZE - 1)))
1628c2ecf20Sopenharmony_ci		addr = HPAGE_REGION_BASE;
1638c2ecf20Sopenharmony_ci
1648c2ecf20Sopenharmony_ci	info.flags = 0;
1658c2ecf20Sopenharmony_ci	info.length = len;
1668c2ecf20Sopenharmony_ci	info.low_limit = addr;
1678c2ecf20Sopenharmony_ci	info.high_limit = HPAGE_REGION_BASE + RGN_MAP_LIMIT;
1688c2ecf20Sopenharmony_ci	info.align_mask = PAGE_MASK & (HPAGE_SIZE - 1);
1698c2ecf20Sopenharmony_ci	info.align_offset = 0;
1708c2ecf20Sopenharmony_ci	return vm_unmapped_area(&info);
1718c2ecf20Sopenharmony_ci}
1728c2ecf20Sopenharmony_ci
1738c2ecf20Sopenharmony_cistatic int __init hugetlb_setup_sz(char *str)
1748c2ecf20Sopenharmony_ci{
1758c2ecf20Sopenharmony_ci	u64 tr_pages;
1768c2ecf20Sopenharmony_ci	unsigned long long size;
1778c2ecf20Sopenharmony_ci
1788c2ecf20Sopenharmony_ci	if (ia64_pal_vm_page_size(&tr_pages, NULL) != 0)
1798c2ecf20Sopenharmony_ci		/*
1808c2ecf20Sopenharmony_ci		 * shouldn't happen, but just in case.
1818c2ecf20Sopenharmony_ci		 */
1828c2ecf20Sopenharmony_ci		tr_pages = 0x15557000UL;
1838c2ecf20Sopenharmony_ci
1848c2ecf20Sopenharmony_ci	size = memparse(str, &str);
1858c2ecf20Sopenharmony_ci	if (*str || !is_power_of_2(size) || !(tr_pages & size) ||
1868c2ecf20Sopenharmony_ci		size <= PAGE_SIZE ||
1878c2ecf20Sopenharmony_ci		size >= (1UL << PAGE_SHIFT << MAX_ORDER)) {
1888c2ecf20Sopenharmony_ci		printk(KERN_WARNING "Invalid huge page size specified\n");
1898c2ecf20Sopenharmony_ci		return 1;
1908c2ecf20Sopenharmony_ci	}
1918c2ecf20Sopenharmony_ci
1928c2ecf20Sopenharmony_ci	hpage_shift = __ffs(size);
1938c2ecf20Sopenharmony_ci	/*
1948c2ecf20Sopenharmony_ci	 * boot cpu already executed ia64_mmu_init, and has HPAGE_SHIFT_DEFAULT
1958c2ecf20Sopenharmony_ci	 * override here with new page shift.
1968c2ecf20Sopenharmony_ci	 */
1978c2ecf20Sopenharmony_ci	ia64_set_rr(HPAGE_REGION_BASE, hpage_shift << 2);
1988c2ecf20Sopenharmony_ci	return 0;
1998c2ecf20Sopenharmony_ci}
2008c2ecf20Sopenharmony_ciearly_param("hugepagesz", hugetlb_setup_sz);
201