xref: /linux/arch/s390/include/asm/page.h (revision 5394f1e9b687bcf26595cabf83483e568676128d)
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 
14*5394f1e9SArnd 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 
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  */
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 
95966ffbd8SHeiko Carstens static inline unsigned long pte_val(pte_t pte)
96966ffbd8SHeiko Carstens {
97966ffbd8SHeiko Carstens 	return pte.pte;
98966ffbd8SHeiko Carstens }
99966ffbd8SHeiko Carstens 
100966ffbd8SHeiko Carstens static inline unsigned long pmd_val(pmd_t pmd)
101966ffbd8SHeiko Carstens {
102966ffbd8SHeiko Carstens 	return pmd.pmd;
103966ffbd8SHeiko Carstens }
104966ffbd8SHeiko Carstens 
105966ffbd8SHeiko Carstens static inline unsigned long pud_val(pud_t pud)
106966ffbd8SHeiko Carstens {
107966ffbd8SHeiko Carstens 	return pud.pud;
108966ffbd8SHeiko Carstens }
109966ffbd8SHeiko Carstens 
110966ffbd8SHeiko Carstens static inline unsigned long p4d_val(p4d_t p4d)
111966ffbd8SHeiko Carstens {
112966ffbd8SHeiko Carstens 	return p4d.p4d;
113966ffbd8SHeiko Carstens }
114966ffbd8SHeiko Carstens 
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 
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 
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 
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;
165c6557e7fSMartin Schwidefsky void arch_free_page(struct page *page, int order);
166c6557e7fSMartin Schwidefsky void arch_alloc_page(struct page *page, int order);
167c6557e7fSMartin Schwidefsky 
168ec6743bbSHendrik Brueckner static inline int devmem_is_allowed(unsigned long pfn)
169ec6743bbSHendrik Brueckner {
170ec6743bbSHendrik Brueckner 	return 0;
171ec6743bbSHendrik Brueckner }
172ec6743bbSHendrik Brueckner 
173c6557e7fSMartin Schwidefsky #define HAVE_ARCH_FREE_PAGE
174c6557e7fSMartin Schwidefsky #define HAVE_ARCH_ALLOC_PAGE
175c6557e7fSMartin Schwidefsky 
176214d9bbcSClaudio Imbrenda #if IS_ENABLED(CONFIG_PGSTE)
177214d9bbcSClaudio Imbrenda int arch_make_page_accessible(struct page *page);
178214d9bbcSClaudio Imbrenda #define HAVE_ARCH_MAKE_PAGE_ACCESSIBLE
179214d9bbcSClaudio Imbrenda #endif
180214d9bbcSClaudio Imbrenda 
181c6557e7fSMartin Schwidefsky #define __PAGE_OFFSET		0x0UL
182c6557e7fSMartin Schwidefsky #define PAGE_OFFSET		0x0UL
18316f48641SHeiko Carstens 
18416f48641SHeiko Carstens #define __pa(x)			((unsigned long)(x))
18516f48641SHeiko Carstens #define __va(x)			((void *)(unsigned long)(x))
18616f48641SHeiko Carstens 
187014b0204SAlexander Gordeev #define phys_to_pfn(phys)	((phys) >> PAGE_SHIFT)
188014b0204SAlexander Gordeev #define pfn_to_phys(pfn)	((pfn) << PAGE_SHIFT)
189014b0204SAlexander Gordeev 
190014b0204SAlexander Gordeev #define phys_to_page(phys)	pfn_to_page(phys_to_pfn(phys))
191014b0204SAlexander Gordeev #define page_to_phys(page)	pfn_to_phys(page_to_pfn(page))
192014b0204SAlexander Gordeev 
1932d1494fbSLinus Walleij static inline void *pfn_to_virt(unsigned long pfn)
1942d1494fbSLinus Walleij {
1952d1494fbSLinus Walleij 	return __va(pfn_to_phys(pfn));
1962d1494fbSLinus Walleij }
1972d1494fbSLinus Walleij 
1982d1494fbSLinus Walleij static inline unsigned long virt_to_pfn(const void *kaddr)
1992d1494fbSLinus Walleij {
2002d1494fbSLinus Walleij 	return phys_to_pfn(__pa(kaddr));
2012d1494fbSLinus Walleij }
2022d1494fbSLinus Walleij 
20375f19542SVasily Gorbik #define pfn_to_kaddr(pfn)	pfn_to_virt(pfn)
20416f48641SHeiko Carstens 
20516f48641SHeiko Carstens #define virt_to_page(kaddr)	pfn_to_page(virt_to_pfn(kaddr))
2063d84683bSDavid Hildenbrand #define page_to_virt(page)	pfn_to_virt(page_to_pfn(page))
207c6557e7fSMartin Schwidefsky 
208014b0204SAlexander Gordeev #define virt_addr_valid(kaddr)	pfn_valid(virt_to_pfn(kaddr))
20916f48641SHeiko Carstens 
210c62da0c3SAnshuman Khandual #define VM_DATA_DEFAULT_FLAGS	VM_DATA_FLAGS_NON_EXEC
211c6557e7fSMartin Schwidefsky 
212b492425cSAlexander Gordeev #endif /* !__ASSEMBLY__ */
213b492425cSAlexander Gordeev 
214c6557e7fSMartin Schwidefsky #include <asm-generic/memory_model.h>
2155b17e1cdSArnd Bergmann #include <asm-generic/getorder.h>
216c6557e7fSMartin Schwidefsky 
217c6557e7fSMartin Schwidefsky #endif /* _S390_PAGE_H */
218