/* SPDX-License-Identifier: GPL-2.0-only */1/*2* Page table support for the Hexagon architecture3*4* Copyright (c) 2010-2011, The Linux Foundation. All rights reserved.5*/67#ifndef _ASM_PGTABLE_H8#define _ASM_PGTABLE_H910/*11* Page table definitions for Qualcomm Hexagon processor.12*/13#include <asm/page.h>14#include <asm-generic/pgtable-nopmd.h>1516/* A handy thing to have if one has the RAM. Declared in head.S */17extern unsigned long empty_zero_page;1819/*20* The PTE model described here is that of the Hexagon Virtual Machine,21* which autonomously walks 2-level page tables. At a lower level, we22* also describe the RISCish software-loaded TLB entry structure of23* the underlying Hexagon processor. A kernel built to run on the24* virtual machine has no need to know about the underlying hardware.25*/26#include <asm/vm_mmu.h>2728/*29* To maximize the comfort level for the PTE manipulation macros,30* define the "well known" architecture-specific bits.31*/32#define _PAGE_READ __HVM_PTE_R33#define _PAGE_WRITE __HVM_PTE_W34#define _PAGE_EXECUTE __HVM_PTE_X35#define _PAGE_USER __HVM_PTE_U3637/*38* We have a total of 4 "soft" bits available in the abstract PTE.39* The two mandatory software bits are Dirty and Accessed.40* To make nonlinear swap work according to the more recent41* model, we want a low order "Present" bit to indicate whether42* the PTE describes MMU programming or swap space.43*/44#define _PAGE_PRESENT (1<<0)45#define _PAGE_DIRTY (1<<1)46#define _PAGE_ACCESSED (1<<2)4748/*49* For now, let's say that Valid and Present are the same thing.50* Alternatively, we could say that it's the "or" of R, W, and X51* permissions.52*/53#define _PAGE_VALID _PAGE_PRESENT5455/*56* We're not defining _PAGE_GLOBAL here, since there's no concept57* of global pages or ASIDs exposed to the Hexagon Virtual Machine,58* and we want to use the same page table structures and macros in59* the native kernel as we do in the virtual machine kernel.60* So we'll put up with a bit of inefficiency for now...61*/6263/* We borrow bit 6 to store the exclusive marker in swap PTEs. */64#define _PAGE_SWP_EXCLUSIVE (1<<6)6566/*67* Top "FOURTH" level (pgd), which for the Hexagon VM is really68* only the second from the bottom, pgd and pud both being collapsed.69* Each entry represents 4MB of virtual address space, 4K of table70* thus maps the full 4GB.71*/72#define PGDIR_SHIFT 2273#define PTRS_PER_PGD 10247475#define PGDIR_SIZE (1UL << PGDIR_SHIFT)76#define PGDIR_MASK (~(PGDIR_SIZE-1))7778#ifdef CONFIG_PAGE_SIZE_4KB79#define PTRS_PER_PTE 102480#endif8182#ifdef CONFIG_PAGE_SIZE_16KB83#define PTRS_PER_PTE 25684#endif8586#ifdef CONFIG_PAGE_SIZE_64KB87#define PTRS_PER_PTE 6488#endif8990#ifdef CONFIG_PAGE_SIZE_256KB91#define PTRS_PER_PTE 1692#endif9394#ifdef CONFIG_PAGE_SIZE_1MB95#define PTRS_PER_PTE 496#endif9798/* Any bigger and the PTE disappears. */99#define pgd_ERROR(e) \100printk(KERN_ERR "%s:%d: bad pgd %08lx.\n", __FILE__, __LINE__,\101pgd_val(e))102103/*104* Page Protection Constants. Includes (in this variant) cache attributes.105*/106extern unsigned long _dflt_cache_att;107108#define PAGE_NONE __pgprot(_PAGE_PRESENT | _PAGE_USER | \109_dflt_cache_att)110#define PAGE_READONLY __pgprot(_PAGE_PRESENT | _PAGE_USER | \111_PAGE_READ | _PAGE_EXECUTE | _dflt_cache_att)112#define PAGE_COPY PAGE_READONLY113#define PAGE_EXEC __pgprot(_PAGE_PRESENT | _PAGE_USER | \114_PAGE_READ | _PAGE_EXECUTE | _dflt_cache_att)115#define PAGE_COPY_EXEC PAGE_EXEC116#define PAGE_SHARED __pgprot(_PAGE_PRESENT | _PAGE_USER | _PAGE_READ | \117_PAGE_EXECUTE | _PAGE_WRITE | _dflt_cache_att)118#define PAGE_KERNEL __pgprot(_PAGE_PRESENT | _PAGE_READ | \119_PAGE_WRITE | _PAGE_EXECUTE | _dflt_cache_att)120121122/*123* Aliases for mapping mmap() protection bits to page protections.124* These get used for static initialization, so using the _dflt_cache_att125* variable for the default cache attribute isn't workable. If the126* default gets changed at boot time, the boot option code has to127* update data structures like the protaction_map[] array.128*/129#define CACHEDEF (CACHE_DEFAULT << 6)130131extern pgd_t swapper_pg_dir[PTRS_PER_PGD]; /* located in head.S */132133/* HUGETLB not working currently */134#ifdef CONFIG_HUGETLB_PAGE135#define pte_mkhuge(pte) __pte((pte_val(pte) & ~0x3) | HVM_HUGEPAGE_SIZE)136#endif137138/*139* For now, assume that higher-level code will do TLB/MMU invalidations140* and don't insert that overhead into this low-level function.141*/142extern void sync_icache_dcache(pte_t pte);143144#define pte_present_exec_user(pte) \145((pte_val(pte) & (_PAGE_EXECUTE | _PAGE_USER)) == \146(_PAGE_EXECUTE | _PAGE_USER))147148static inline void set_pte(pte_t *ptep, pte_t pteval)149{150/* should really be using pte_exec, if it weren't declared later. */151if (pte_present_exec_user(pteval))152sync_icache_dcache(pteval);153154*ptep = pteval;155}156157/*158* For the Hexagon Virtual Machine MMU (or its emulation), a null/invalid159* L1 PTE (PMD/PGD) has 7 in the least significant bits. For the L2 PTE160* (Linux PTE), the key is to have bits 11..9 all zero. We'd use 0x7161* as a universal null entry, but some of those least significant bits162* are interpreted by software.163*/164#define _NULL_PMD 0x7165#define _NULL_PTE 0x0166167static inline void pmd_clear(pmd_t *pmd_entry_ptr)168{169pmd_val(*pmd_entry_ptr) = _NULL_PMD;170}171172/*173* Conveniently, a null PTE value is invalid.174*/175static inline void pte_clear(struct mm_struct *mm, unsigned long addr,176pte_t *ptep)177{178pte_val(*ptep) = _NULL_PTE;179}180181/**182* pmd_none - check if pmd_entry is mapped183* @pmd_entry: pmd entry184*185* MIPS checks it against that "invalid pte table" thing.186*/187static inline int pmd_none(pmd_t pmd)188{189return pmd_val(pmd) == _NULL_PMD;190}191192/**193* pmd_present - is there a page table behind this?194* Essentially the inverse of pmd_none. We maybe195* save an inline instruction by defining it this196* way, instead of simply "!pmd_none".197*/198static inline int pmd_present(pmd_t pmd)199{200return pmd_val(pmd) != (unsigned long)_NULL_PMD;201}202203/**204* pmd_bad - check if a PMD entry is "bad". That might mean swapped out.205* As we have no known cause of badness, it's null, as it is for many206* architectures.207*/208static inline int pmd_bad(pmd_t pmd)209{210return 0;211}212213/*214* pmd_pfn - converts a PMD entry to a page frame number215*/216#define pmd_pfn(pmd) (pmd_val(pmd) >> PAGE_SHIFT)217218/*219* pmd_page - converts a PMD entry to a page pointer220*/221#define pmd_page(pmd) (pfn_to_page(pmd_val(pmd) >> PAGE_SHIFT))222223/**224* pte_none - check if pte is mapped225* @pte: pte_t entry226*/227static inline int pte_none(pte_t pte)228{229return pte_val(pte) == _NULL_PTE;230};231232/*233* pte_present - check if page is present234*/235static inline int pte_present(pte_t pte)236{237return pte_val(pte) & _PAGE_PRESENT;238}239240/* pte_page - returns a page (frame pointer/descriptor?) based on a PTE */241#define pte_page(x) pfn_to_page(pte_pfn(x))242243/* pte_mkold - mark PTE as not recently accessed */244static inline pte_t pte_mkold(pte_t pte)245{246pte_val(pte) &= ~_PAGE_ACCESSED;247return pte;248}249250/* pte_mkyoung - mark PTE as recently accessed */251static inline pte_t pte_mkyoung(pte_t pte)252{253pte_val(pte) |= _PAGE_ACCESSED;254return pte;255}256257/* pte_mkclean - mark page as in sync with backing store */258static inline pte_t pte_mkclean(pte_t pte)259{260pte_val(pte) &= ~_PAGE_DIRTY;261return pte;262}263264/* pte_mkdirty - mark page as modified */265static inline pte_t pte_mkdirty(pte_t pte)266{267pte_val(pte) |= _PAGE_DIRTY;268return pte;269}270271/* pte_young - "is PTE marked as accessed"? */272static inline int pte_young(pte_t pte)273{274return pte_val(pte) & _PAGE_ACCESSED;275}276277/* pte_dirty - "is PTE dirty?" */278static inline int pte_dirty(pte_t pte)279{280return pte_val(pte) & _PAGE_DIRTY;281}282283/* pte_modify - set protection bits on PTE */284static inline pte_t pte_modify(pte_t pte, pgprot_t prot)285{286pte_val(pte) &= PAGE_MASK;287pte_val(pte) |= pgprot_val(prot);288return pte;289}290291/* pte_wrprotect - mark page as not writable */292static inline pte_t pte_wrprotect(pte_t pte)293{294pte_val(pte) &= ~_PAGE_WRITE;295return pte;296}297298/* pte_mkwrite - mark page as writable */299static inline pte_t pte_mkwrite_novma(pte_t pte)300{301pte_val(pte) |= _PAGE_WRITE;302return pte;303}304305/* pte_mkexec - mark PTE as executable */306static inline pte_t pte_mkexec(pte_t pte)307{308pte_val(pte) |= _PAGE_EXECUTE;309return pte;310}311312/* pte_read - "is PTE marked as readable?" */313static inline int pte_read(pte_t pte)314{315return pte_val(pte) & _PAGE_READ;316}317318/* pte_write - "is PTE marked as writable?" */319static inline int pte_write(pte_t pte)320{321return pte_val(pte) & _PAGE_WRITE;322}323324325/* pte_exec - "is PTE marked as executable?" */326static inline int pte_exec(pte_t pte)327{328return pte_val(pte) & _PAGE_EXECUTE;329}330331/* __pte_to_swp_entry - extract swap entry from PTE */332#define __pte_to_swp_entry(pte) ((swp_entry_t) { pte_val(pte) })333334/* __swp_entry_to_pte - extract PTE from swap entry */335#define __swp_entry_to_pte(x) ((pte_t) { (x).val })336337#define PFN_PTE_SHIFT PAGE_SHIFT338/* pfn_pte - convert page number and protection value to page table entry */339#define pfn_pte(pfn, pgprot) __pte((pfn << PAGE_SHIFT) | pgprot_val(pgprot))340341/* pte_pfn - convert pte to page frame number */342#define pte_pfn(pte) (pte_val(pte) >> PAGE_SHIFT)343#define set_pmd(pmdptr, pmdval) (*(pmdptr) = (pmdval))344345static inline unsigned long pmd_page_vaddr(pmd_t pmd)346{347return (unsigned long)__va(pmd_val(pmd) & PAGE_MASK);348}349350/* ZERO_PAGE - returns the globally shared zero page */351#define ZERO_PAGE(vaddr) (virt_to_page(&empty_zero_page))352353/*354* Encode/decode swap entries and swap PTEs. Swap PTEs are all PTEs that355* are !pte_none() && !pte_present().356*357* Swap/file PTE definitions. If _PAGE_PRESENT is zero, the rest of the PTE is358* interpreted as swap information. The remaining free bits are interpreted as359* listed below. Rather than have the TLB fill handler test360* _PAGE_PRESENT, we're going to reserve the permissions bits and set them to361* all zeros for swap entries, which speeds up the miss handler at the cost of362* 3 bits of offset. That trade-off can be revisited if necessary, but Hexagon363* processor architecture and target applications suggest a lot of TLB misses364* and not much swap space.365*366* Format of swap PTE:367* bit 0: Present (zero)368* bits 1-5: swap type (arch independent layer uses 5 bits max)369* bit 6: exclusive marker370* bits 7-9: bits 2:0 of offset371* bits 10-12: effectively _PAGE_PROTNONE (all zero)372* bits 13-31: bits 21:3 of swap offset373*374* The split offset makes some of the following macros a little gnarly,375* but there's plenty of precedent for this sort of thing.376*/377378/* Used for swap PTEs */379#define __swp_type(swp_pte) (((swp_pte).val >> 1) & 0x1f)380381#define __swp_offset(swp_pte) \382((((swp_pte).val >> 7) & 0x7) | (((swp_pte).val >> 10) & 0x3ffff8))383384#define __swp_entry(type, offset) \385((swp_entry_t) { \386(((type & 0x1f) << 1) | \387((offset & 0x3ffff8) << 10) | ((offset & 0x7) << 7)) })388389static inline bool pte_swp_exclusive(pte_t pte)390{391return pte_val(pte) & _PAGE_SWP_EXCLUSIVE;392}393394static inline pte_t pte_swp_mkexclusive(pte_t pte)395{396pte_val(pte) |= _PAGE_SWP_EXCLUSIVE;397return pte;398}399400static inline pte_t pte_swp_clear_exclusive(pte_t pte)401{402pte_val(pte) &= ~_PAGE_SWP_EXCLUSIVE;403return pte;404}405406#endif407408409