^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) /* SPDX-License-Identifier: GPL-2.0-only */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) * Copyright (C) 2004, 2007-2010, 2011-2012 Synopsys, Inc. (www.synopsys.com)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) * vineetg: June 2011
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) * -"/proc/meminfo | grep PageTables" kept on increasing
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) * Recently added pgtable dtor was not getting called.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) * vineetg: May 2011
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) * -Variable pg-sz means that Page Tables could be variable sized themselves
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) * So calculate it based on addr traversal split [pgd-bits:pte-bits:xxx]
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) * -Page Table size capped to max 1 to save memory - hence verified.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) * -Since these deal with constants, gcc compile-time optimizes them.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) * vineetg: Nov 2010
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) * -Added pgtable ctor/dtor used for pgtable mem accounting
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) * vineetg: April 2010
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) * -Switched pgtable_t from being struct page * to unsigned long
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) * =Needed so that Page Table allocator (pte_alloc_one) is not forced to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) * deal with struct page. That way in future we can make it allocate
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) * multiple PG Tbls in one Page Frame
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) * =sweet side effect is avoiding calls to ugly page_address( ) from the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) * pg-tlb allocator sub-sys (pte_alloc_one, ptr_free, pmd_populate)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) * Amit Bhor, Sameer Dhavale: Codito Technologies 2004
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) #ifndef _ASM_ARC_PGALLOC_H
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) #define _ASM_ARC_PGALLOC_H
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) #include <linux/mm.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) #include <linux/log2.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) static inline void
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) pmd_populate_kernel(struct mm_struct *mm, pmd_t *pmd, pte_t *pte)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) pmd_set(pmd, pte);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) static inline void
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) pmd_populate(struct mm_struct *mm, pmd_t *pmd, pgtable_t ptep)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) pmd_set(pmd, (pte_t *) ptep);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) static inline int __get_order_pgd(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) return get_order(PTRS_PER_PGD * sizeof(pgd_t));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) static inline pgd_t *pgd_alloc(struct mm_struct *mm)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) int num, num2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) pgd_t *ret = (pgd_t *) __get_free_pages(GFP_KERNEL, __get_order_pgd());
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) num = USER_PTRS_PER_PGD + USER_KERNEL_GUTTER / PGDIR_SIZE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) memzero(ret, num * sizeof(pgd_t));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) num2 = VMALLOC_SIZE / PGDIR_SIZE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) memcpy(ret + num, swapper_pg_dir + num, num2 * sizeof(pgd_t));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) memzero(ret + num + num2,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) (PTRS_PER_PGD - num - num2) * sizeof(pgd_t));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) static inline void pgd_free(struct mm_struct *mm, pgd_t *pgd)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) free_pages((unsigned long)pgd, __get_order_pgd());
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) * With software-only page-tables, addr-split for traversal is tweakable and
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) * that directly governs how big tables would be at each level.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) * Further, the MMU page size is configurable.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) * Thus we need to programatically assert the size constraint
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) * All of this is const math, allowing gcc to do constant folding/propagation.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) static inline int __get_order_pte(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) return get_order(PTRS_PER_PTE * sizeof(pte_t));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) static inline pte_t *pte_alloc_one_kernel(struct mm_struct *mm)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92) pte_t *pte;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) pte = (pte_t *) __get_free_pages(GFP_KERNEL | __GFP_ZERO,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95) __get_order_pte());
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) return pte;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) static inline pgtable_t
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) pte_alloc_one(struct mm_struct *mm)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) pgtable_t pte_pg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) struct page *page;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) pte_pg = (pgtable_t)__get_free_pages(GFP_KERNEL, __get_order_pte());
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) if (!pte_pg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) memzero((void *)pte_pg, PTRS_PER_PTE * sizeof(pte_t));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) page = virt_to_page(pte_pg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) if (!pgtable_pte_page_ctor(page)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) __free_page(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) return pte_pg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) static inline void pte_free_kernel(struct mm_struct *mm, pte_t *pte)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) free_pages((unsigned long)pte, __get_order_pte()); /* takes phy addr */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) static inline void pte_free(struct mm_struct *mm, pgtable_t ptep)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) pgtable_pte_page_dtor(virt_to_page(ptep));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) free_pages((unsigned long)ptep, __get_order_pte());
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) #define __pte_free_tlb(tlb, pte, addr) pte_free((tlb)->mm, pte)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) #define pmd_pgtable(pmd) ((pgtable_t) pmd_page_vaddr(pmd))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) #endif /* _ASM_ARC_PGALLOC_H */