1b2441318SGreg Kroah-Hartman /* SPDX-License-Identifier: GPL-2.0 */
2c6557e7fSMartin Schwidefsky /*
3c6557e7fSMartin Schwidefsky * S390 version
4a53c8fabSHeiko Carstens * Copyright IBM Corp. 1999, 2000
5c6557e7fSMartin Schwidefsky * Author(s): Hartmut Penner (hp@de.ibm.com)
6c6557e7fSMartin Schwidefsky */
7c6557e7fSMartin Schwidefsky
8c6557e7fSMartin Schwidefsky #ifndef _S390_PAGE_H
9c6557e7fSMartin Schwidefsky #define _S390_PAGE_H
10c6557e7fSMartin Schwidefsky
11c6557e7fSMartin Schwidefsky #include <linux/const.h>
12c6557e7fSMartin Schwidefsky #include <asm/types.h>
13c6557e7fSMartin Schwidefsky
145394f1e9SArnd Bergmann #define _PAGE_SHIFT CONFIG_PAGE_SHIFT
15c67da7c7SHeiko Carstens #define _PAGE_SIZE (_AC(1, UL) << _PAGE_SHIFT)
16c67da7c7SHeiko Carstens #define _PAGE_MASK (~(_PAGE_SIZE - 1))
17c67da7c7SHeiko Carstens
18c6557e7fSMartin Schwidefsky /* PAGE_SHIFT determines the page size */
19c67da7c7SHeiko Carstens #define PAGE_SHIFT _PAGE_SHIFT
20c67da7c7SHeiko Carstens #define PAGE_SIZE _PAGE_SIZE
21c67da7c7SHeiko Carstens #define PAGE_MASK _PAGE_MASK
2263764028SHeiko Carstens #define PAGE_DEFAULT_ACC _AC(0, UL)
23e613d834SJanis Schoetterl-Glausch /* storage-protection override */
24e613d834SJanis Schoetterl-Glausch #define PAGE_SPO_ACC 9
25c6557e7fSMartin Schwidefsky #define PAGE_DEFAULT_KEY (PAGE_DEFAULT_ACC << 4)
26c6557e7fSMartin Schwidefsky
2741318bfeSDominik Dingel #define HPAGE_SHIFT 20
28c6557e7fSMartin Schwidefsky #define HPAGE_SIZE (1UL << HPAGE_SHIFT)
29c6557e7fSMartin Schwidefsky #define HPAGE_MASK (~(HPAGE_SIZE - 1))
30c6557e7fSMartin Schwidefsky #define HUGETLB_PAGE_ORDER (HPAGE_SHIFT - PAGE_SHIFT)
31d08de8e2SGerald Schaefer #define HUGE_MAX_HSTATE 2
32c6557e7fSMartin Schwidefsky
33c6557e7fSMartin Schwidefsky #define ARCH_HAS_SETCLEAR_HUGE_PTE
34c6557e7fSMartin Schwidefsky #define ARCH_HAS_HUGE_PTE_TYPE
35c6557e7fSMartin Schwidefsky #define ARCH_HAS_PREPARE_HUGEPAGE
36c6557e7fSMartin Schwidefsky #define ARCH_HAS_HUGEPAGE_CLEAR_FLUSH
37c6557e7fSMartin Schwidefsky
385f490a52SGerald Schaefer #define HAVE_ARCH_HUGETLB_UNMAPPED_AREA
395f490a52SGerald Schaefer
4041318bfeSDominik Dingel #include <asm/setup.h>
4141318bfeSDominik Dingel #ifndef __ASSEMBLY__
4241318bfeSDominik Dingel
43de3fa841SHeiko Carstens void __storage_key_init_range(unsigned long start, unsigned long end);
44de3fa841SHeiko Carstens
storage_key_init_range(unsigned long start,unsigned long end)45127c1fefSMartin Schwidefsky static inline void storage_key_init_range(unsigned long start, unsigned long end)
46127c1fefSMartin Schwidefsky {
4738032473SNathan Chancellor if (PAGE_DEFAULT_KEY != 0)
48127c1fefSMartin Schwidefsky __storage_key_init_range(start, end);
49127c1fefSMartin Schwidefsky }
506b70a920SHeiko Carstens
51fb3d1c08SChristian Borntraeger #define clear_page(page) memset((page), 0, PAGE_SIZE)
52c6557e7fSMartin Schwidefsky
53dba6bb60SHeiko Carstens /*
54dba6bb60SHeiko Carstens * copy_page uses the mvcl instruction with 0xb0 padding byte in order to
55dba6bb60SHeiko Carstens * bypass caches when copying a page. Especially when copying huge pages
56dba6bb60SHeiko Carstens * this keeps L1 and L2 data caches alive.
57dba6bb60SHeiko Carstens */
copy_page(void * to,void * from)58c6557e7fSMartin Schwidefsky static inline void copy_page(void *to, void *from)
59c6557e7fSMartin Schwidefsky {
6075c89a2bSHeiko Carstens union register_pair dst, src;
6175c89a2bSHeiko Carstens
6275c89a2bSHeiko Carstens dst.even = (unsigned long) to;
6375c89a2bSHeiko Carstens dst.odd = 0x1000;
6475c89a2bSHeiko Carstens src.even = (unsigned long) from;
6575c89a2bSHeiko Carstens src.odd = 0xb0001000;
6675c89a2bSHeiko Carstens
67c6557e7fSMartin Schwidefsky asm volatile(
6875c89a2bSHeiko Carstens " mvcl %[dst],%[src]"
6975c89a2bSHeiko Carstens : [dst] "+&d" (dst.pair), [src] "+&d" (src.pair)
70dba6bb60SHeiko Carstens : : "memory", "cc");
71c6557e7fSMartin Schwidefsky }
72c6557e7fSMartin Schwidefsky
73c6557e7fSMartin Schwidefsky #define clear_user_page(page, vaddr, pg) clear_page(page)
74c6557e7fSMartin Schwidefsky #define copy_user_page(to, from, vaddr, pg) copy_page(to, from)
75c6557e7fSMartin Schwidefsky
766bc56a4dSMatthew Wilcox (Oracle) #define vma_alloc_zeroed_movable_folio(vma, vaddr) \
776bc56a4dSMatthew Wilcox (Oracle) vma_alloc_folio(GFP_HIGHUSER_MOVABLE | __GFP_ZERO, 0, vma, vaddr, false)
78c6557e7fSMartin Schwidefsky
79c6557e7fSMartin Schwidefsky /*
80c6557e7fSMartin Schwidefsky * These are used to make use of C type-checking..
81c6557e7fSMartin Schwidefsky */
82c6557e7fSMartin Schwidefsky
83c6557e7fSMartin Schwidefsky typedef struct { unsigned long pgprot; } pgprot_t;
84b2fa47e6SMartin Schwidefsky typedef struct { unsigned long pgste; } pgste_t;
85c6557e7fSMartin Schwidefsky typedef struct { unsigned long pte; } pte_t;
86c6557e7fSMartin Schwidefsky typedef struct { unsigned long pmd; } pmd_t;
87c6557e7fSMartin Schwidefsky typedef struct { unsigned long pud; } pud_t;
881aea9b3fSMartin Schwidefsky typedef struct { unsigned long p4d; } p4d_t;
89c6557e7fSMartin Schwidefsky typedef struct { unsigned long pgd; } pgd_t;
90c6557e7fSMartin Schwidefsky typedef pte_t *pgtable_t;
91c6557e7fSMartin Schwidefsky
92c6557e7fSMartin Schwidefsky #define pgprot_val(x) ((x).pgprot)
93b2fa47e6SMartin Schwidefsky #define pgste_val(x) ((x).pgste)
94966ffbd8SHeiko Carstens
pte_val(pte_t pte)95966ffbd8SHeiko Carstens static inline unsigned long pte_val(pte_t pte)
96966ffbd8SHeiko Carstens {
97966ffbd8SHeiko Carstens return pte.pte;
98966ffbd8SHeiko Carstens }
99966ffbd8SHeiko Carstens
pmd_val(pmd_t pmd)100966ffbd8SHeiko Carstens static inline unsigned long pmd_val(pmd_t pmd)
101966ffbd8SHeiko Carstens {
102966ffbd8SHeiko Carstens return pmd.pmd;
103966ffbd8SHeiko Carstens }
104966ffbd8SHeiko Carstens
pud_val(pud_t pud)105966ffbd8SHeiko Carstens static inline unsigned long pud_val(pud_t pud)
106966ffbd8SHeiko Carstens {
107966ffbd8SHeiko Carstens return pud.pud;
108966ffbd8SHeiko Carstens }
109966ffbd8SHeiko Carstens
p4d_val(p4d_t p4d)110966ffbd8SHeiko Carstens static inline unsigned long p4d_val(p4d_t p4d)
111966ffbd8SHeiko Carstens {
112966ffbd8SHeiko Carstens return p4d.p4d;
113966ffbd8SHeiko Carstens }
114966ffbd8SHeiko Carstens
pgd_val(pgd_t pgd)115966ffbd8SHeiko Carstens static inline unsigned long pgd_val(pgd_t pgd)
116966ffbd8SHeiko Carstens {
117966ffbd8SHeiko Carstens return pgd.pgd;
118966ffbd8SHeiko Carstens }
119c6557e7fSMartin Schwidefsky
120b2fa47e6SMartin Schwidefsky #define __pgste(x) ((pgste_t) { (x) } )
121c6557e7fSMartin Schwidefsky #define __pte(x) ((pte_t) { (x) } )
122c6557e7fSMartin Schwidefsky #define __pmd(x) ((pmd_t) { (x) } )
123b2fa47e6SMartin Schwidefsky #define __pud(x) ((pud_t) { (x) } )
1241aea9b3fSMartin Schwidefsky #define __p4d(x) ((p4d_t) { (x) } )
125c6557e7fSMartin Schwidefsky #define __pgd(x) ((pgd_t) { (x) } )
126c6557e7fSMartin Schwidefsky #define __pgprot(x) ((pgprot_t) { (x) } )
127c6557e7fSMartin Schwidefsky
page_set_storage_key(unsigned long addr,unsigned char skey,int mapped)1282d42552dSMartin Schwidefsky static inline void page_set_storage_key(unsigned long addr,
1292d42552dSMartin Schwidefsky unsigned char skey, int mapped)
130c6557e7fSMartin Schwidefsky {
131e2b8d7afSMartin Schwidefsky if (!mapped)
132e2b8d7afSMartin Schwidefsky asm volatile(".insn rrf,0xb22b0000,%0,%1,8,0"
133e2b8d7afSMartin Schwidefsky : : "d" (skey), "a" (addr));
134e2b8d7afSMartin Schwidefsky else
135c6557e7fSMartin Schwidefsky asm volatile("sske %0,%1" : : "d" (skey), "a" (addr));
136c6557e7fSMartin Schwidefsky }
137c6557e7fSMartin Schwidefsky
page_get_storage_key(unsigned long addr)1382d42552dSMartin Schwidefsky static inline unsigned char page_get_storage_key(unsigned long addr)
139c6557e7fSMartin Schwidefsky {
1402d42552dSMartin Schwidefsky unsigned char skey;
141c6557e7fSMartin Schwidefsky
1422d42552dSMartin Schwidefsky asm volatile("iske %0,%1" : "=d" (skey) : "a" (addr));
143c6557e7fSMartin Schwidefsky return skey;
144c6557e7fSMartin Schwidefsky }
145c6557e7fSMartin Schwidefsky
page_reset_referenced(unsigned long addr)1462d42552dSMartin Schwidefsky static inline int page_reset_referenced(unsigned long addr)
1472d42552dSMartin Schwidefsky {
148a7e19ab5SDavid Hildenbrand int cc;
1492d42552dSMartin Schwidefsky
1502d42552dSMartin Schwidefsky asm volatile(
1512d42552dSMartin Schwidefsky " rrbe 0,%1\n"
1522d42552dSMartin Schwidefsky " ipm %0\n"
153a7e19ab5SDavid Hildenbrand " srl %0,28\n"
154a7e19ab5SDavid Hildenbrand : "=d" (cc) : "a" (addr) : "cc");
155a7e19ab5SDavid Hildenbrand return cc;
1562d42552dSMartin Schwidefsky }
1572d42552dSMartin Schwidefsky
1582d42552dSMartin Schwidefsky /* Bits int the storage key */
1592d42552dSMartin Schwidefsky #define _PAGE_CHANGED 0x02 /* HW changed bit */
1602d42552dSMartin Schwidefsky #define _PAGE_REFERENCED 0x04 /* HW referenced bit */
1612d42552dSMartin Schwidefsky #define _PAGE_FP_BIT 0x08 /* HW fetch protection bit */
1622d42552dSMartin Schwidefsky #define _PAGE_ACC_BITS 0xf0 /* HW access control bits */
1632d42552dSMartin Schwidefsky
164c6557e7fSMartin Schwidefsky struct page;
16599b3f8f7SDavid Hildenbrand struct folio;
166c6557e7fSMartin Schwidefsky void arch_free_page(struct page *page, int order);
167c6557e7fSMartin Schwidefsky void arch_alloc_page(struct page *page, int order);
168c6557e7fSMartin Schwidefsky
devmem_is_allowed(unsigned long pfn)169ec6743bbSHendrik Brueckner static inline int devmem_is_allowed(unsigned long pfn)
170ec6743bbSHendrik Brueckner {
171ec6743bbSHendrik Brueckner return 0;
172ec6743bbSHendrik Brueckner }
173ec6743bbSHendrik Brueckner
174c6557e7fSMartin Schwidefsky #define HAVE_ARCH_FREE_PAGE
175c6557e7fSMartin Schwidefsky #define HAVE_ARCH_ALLOC_PAGE
176c6557e7fSMartin Schwidefsky
17799b3f8f7SDavid Hildenbrand int arch_make_folio_accessible(struct folio *folio);
17899b3f8f7SDavid Hildenbrand #define HAVE_ARCH_MAKE_FOLIO_ACCESSIBLE
179214d9bbcSClaudio Imbrenda
180214d9bbcSClaudio Imbrenda struct vm_layout {
181214d9bbcSClaudio Imbrenda unsigned long kaslr_offset;
182236f324bSAlexander Gordeev unsigned long kaslr_offset_phys;
183236f324bSAlexander Gordeev unsigned long identity_base;
1843bb11234SAlexander Gordeev unsigned long identity_size;
1857de0446fSAlexander Gordeev };
186236f324bSAlexander Gordeev
187236f324bSAlexander Gordeev extern struct vm_layout vm_layout;
188236f324bSAlexander Gordeev
189236f324bSAlexander Gordeev #define __kaslr_offset vm_layout.kaslr_offset
190236f324bSAlexander Gordeev #define __kaslr_offset_phys vm_layout.kaslr_offset_phys
191236f324bSAlexander Gordeev #define __identity_base vm_layout.identity_base
1923bb11234SAlexander Gordeev #define ident_map_size vm_layout.identity_size
1937de0446fSAlexander Gordeev
kaslr_offset(void)194236f324bSAlexander Gordeev static inline unsigned long kaslr_offset(void)
195236f324bSAlexander Gordeev {
196bbe72f39SAlexander Gordeev return __kaslr_offset;
197bbe72f39SAlexander Gordeev }
198bbe72f39SAlexander Gordeev
199bbe72f39SAlexander Gordeev extern int __kaslr_enabled;
kaslr_enabled(void)200bbe72f39SAlexander Gordeev static inline int kaslr_enabled(void)
201bbe72f39SAlexander Gordeev {
202bbe72f39SAlexander Gordeev if (IS_ENABLED(CONFIG_RANDOMIZE_BASE))
203bbe72f39SAlexander Gordeev return __kaslr_enabled;
204bbe72f39SAlexander Gordeev return 0;
205bbe72f39SAlexander Gordeev }
206bbe72f39SAlexander Gordeev
207bbe72f39SAlexander Gordeev #define __PAGE_OFFSET __identity_base
208bbe72f39SAlexander Gordeev #define PAGE_OFFSET __PAGE_OFFSET
2097de0446fSAlexander Gordeev
2107de0446fSAlexander Gordeev #ifdef __DECOMPRESSOR
21116f48641SHeiko Carstens
2125f58bde7SHeiko Carstens #define __pa_nodebug(x) ((unsigned long)(x))
2135f58bde7SHeiko Carstens #define __pa(x) __pa_nodebug(x)
214c98d2ecaSAlexander Gordeev #define __pa32(x) __pa(x)
2155f58bde7SHeiko Carstens #define __va(x) ((void *)(unsigned long)(x))
2165f58bde7SHeiko Carstens
21716f48641SHeiko Carstens #else /* __DECOMPRESSOR */
21816f48641SHeiko Carstens
__pa_nodebug(unsigned long x)2195f58bde7SHeiko Carstens static inline unsigned long __pa_nodebug(unsigned long x)
2205f58bde7SHeiko Carstens {
221c98d2ecaSAlexander Gordeev if (x < __kaslr_offset)
222c98d2ecaSAlexander Gordeev return x - __identity_base;
223c98d2ecaSAlexander Gordeev return x - __kaslr_offset + __kaslr_offset_phys;
224c98d2ecaSAlexander Gordeev }
225c98d2ecaSAlexander Gordeev
226c98d2ecaSAlexander Gordeev #ifdef CONFIG_DEBUG_VIRTUAL
227c98d2ecaSAlexander Gordeev
2285f58bde7SHeiko Carstens unsigned long __phys_addr(unsigned long x, bool is_31bit);
2295f58bde7SHeiko Carstens
2305f58bde7SHeiko Carstens #else /* CONFIG_DEBUG_VIRTUAL */
2315f58bde7SHeiko Carstens
__phys_addr(unsigned long x,bool is_31bit)2325f58bde7SHeiko Carstens static inline unsigned long __phys_addr(unsigned long x, bool is_31bit)
2335f58bde7SHeiko Carstens {
2345f58bde7SHeiko Carstens return __pa_nodebug(x);
2355f58bde7SHeiko Carstens }
2365f58bde7SHeiko Carstens
2375f58bde7SHeiko Carstens #endif /* CONFIG_DEBUG_VIRTUAL */
2385f58bde7SHeiko Carstens
2395f58bde7SHeiko Carstens #define __pa(x) __phys_addr((unsigned long)(x), false)
2405f58bde7SHeiko Carstens #define __pa32(x) __phys_addr((unsigned long)(x), true)
2415f58bde7SHeiko Carstens #define __va(x) ((void *)((unsigned long)(x) + __identity_base))
2425f58bde7SHeiko Carstens
243c98d2ecaSAlexander Gordeev #endif /* __DECOMPRESSOR */
2445f58bde7SHeiko Carstens
2455f58bde7SHeiko Carstens #define phys_to_pfn(phys) ((phys) >> PAGE_SHIFT)
2465f58bde7SHeiko Carstens #define pfn_to_phys(pfn) ((pfn) << PAGE_SHIFT)
247014b0204SAlexander Gordeev
248014b0204SAlexander Gordeev #define phys_to_page(phys) pfn_to_page(phys_to_pfn(phys))
249014b0204SAlexander Gordeev #define phys_to_folio(phys) page_folio(phys_to_page(phys))
250014b0204SAlexander Gordeev #define page_to_phys(page) pfn_to_phys(page_to_pfn(page))
251036c0e10SDavid Hildenbrand #define folio_to_phys(page) pfn_to_phys(folio_pfn(folio))
252014b0204SAlexander Gordeev
pfn_to_virt(unsigned long pfn)253036c0e10SDavid Hildenbrand static inline void *pfn_to_virt(unsigned long pfn)
254014b0204SAlexander Gordeev {
2552d1494fbSLinus Walleij return __va(pfn_to_phys(pfn));
2562d1494fbSLinus Walleij }
2572d1494fbSLinus Walleij
virt_to_pfn(const void * kaddr)2582d1494fbSLinus Walleij static inline unsigned long virt_to_pfn(const void *kaddr)
2592d1494fbSLinus Walleij {
2602d1494fbSLinus Walleij return phys_to_pfn(__pa(kaddr));
2612d1494fbSLinus Walleij }
2622d1494fbSLinus Walleij
2632d1494fbSLinus Walleij #define pfn_to_kaddr(pfn) pfn_to_virt(pfn)
2642d1494fbSLinus Walleij
26575f19542SVasily Gorbik #define virt_to_page(kaddr) pfn_to_page(virt_to_pfn(kaddr))
26616f48641SHeiko Carstens #define page_to_virt(page) pfn_to_virt(page_to_pfn(page))
26716f48641SHeiko Carstens
2683d84683bSDavid Hildenbrand #define virt_addr_valid(kaddr) pfn_valid(phys_to_pfn(__pa_nodebug((unsigned long)(kaddr))))
269c6557e7fSMartin Schwidefsky
270c98d2ecaSAlexander Gordeev #define VM_DATA_DEFAULT_FLAGS VM_DATA_FLAGS_NON_EXEC
27116f48641SHeiko Carstens
272c62da0c3SAnshuman Khandual #endif /* !__ASSEMBLY__ */
273c6557e7fSMartin Schwidefsky
274b492425cSAlexander Gordeev #include <asm-generic/memory_model.h>
275b492425cSAlexander Gordeev #include <asm-generic/getorder.h>
276c6557e7fSMartin Schwidefsky
2775b17e1cdSArnd Bergmann #define AMODE31_SIZE (3 * PAGE_SIZE)
278c6557e7fSMartin Schwidefsky
27956b1069cSAlexander Gordeev #define KERNEL_IMAGE_SIZE (512 * 1024 * 1024)
28056b1069cSAlexander Gordeev #define __NO_KASLR_START_KERNEL CONFIG_KERNEL_IMAGE_BASE
28154f2ecc3SAlexander Gordeev #define __NO_KASLR_END_KERNEL (__NO_KASLR_START_KERNEL + KERNEL_IMAGE_SIZE)
2821812dc9cSAlexander Gordeev
28354f2ecc3SAlexander Gordeev #define TEXT_OFFSET 0x100000
28454f2ecc3SAlexander Gordeev
285*1642285eSAlexander Gordeev #endif /* _S390_PAGE_H */
286*1642285eSAlexander Gordeev