xref: /linux/arch/s390/mm/hugetlbpage.c (revision ca55b2fef3a9373fcfc30f82fd26bc7fccbda732)
1 /*
2  *  IBM System z Huge TLB Page Support for Kernel.
3  *
4  *    Copyright IBM Corp. 2007
5  *    Author(s): Gerald Schaefer <gerald.schaefer@de.ibm.com>
6  */
7 
8 #include <linux/mm.h>
9 #include <linux/hugetlb.h>
10 
11 static inline pmd_t __pte_to_pmd(pte_t pte)
12 {
13 	pmd_t pmd;
14 
15 	/*
16 	 * Convert encoding		  pte bits	   pmd bits
17 	 *				lIR.uswrdy.p	dy..R...I...wr
18 	 * empty			010.000000.0 -> 00..0...1...00
19 	 * prot-none, clean, old	111.000000.1 -> 00..1...1...00
20 	 * prot-none, clean, young	111.000001.1 -> 01..1...1...00
21 	 * prot-none, dirty, old	111.000010.1 -> 10..1...1...00
22 	 * prot-none, dirty, young	111.000011.1 -> 11..1...1...00
23 	 * read-only, clean, old	111.000100.1 -> 00..1...1...01
24 	 * read-only, clean, young	101.000101.1 -> 01..1...0...01
25 	 * read-only, dirty, old	111.000110.1 -> 10..1...1...01
26 	 * read-only, dirty, young	101.000111.1 -> 11..1...0...01
27 	 * read-write, clean, old	111.001100.1 -> 00..1...1...11
28 	 * read-write, clean, young	101.001101.1 -> 01..1...0...11
29 	 * read-write, dirty, old	110.001110.1 -> 10..0...1...11
30 	 * read-write, dirty, young	100.001111.1 -> 11..0...0...11
31 	 * HW-bits: R read-only, I invalid
32 	 * SW-bits: p present, y young, d dirty, r read, w write, s special,
33 	 *	    u unused, l large
34 	 */
35 	if (pte_present(pte)) {
36 		pmd_val(pmd) = pte_val(pte) & PAGE_MASK;
37 		pmd_val(pmd) |= (pte_val(pte) & _PAGE_READ) >> 4;
38 		pmd_val(pmd) |= (pte_val(pte) & _PAGE_WRITE) >> 4;
39 		pmd_val(pmd) |=	(pte_val(pte) & _PAGE_INVALID) >> 5;
40 		pmd_val(pmd) |= (pte_val(pte) & _PAGE_PROTECT);
41 		pmd_val(pmd) |= (pte_val(pte) & _PAGE_DIRTY) << 10;
42 		pmd_val(pmd) |= (pte_val(pte) & _PAGE_YOUNG) << 10;
43 	} else
44 		pmd_val(pmd) = _SEGMENT_ENTRY_INVALID;
45 	return pmd;
46 }
47 
48 static inline pte_t __pmd_to_pte(pmd_t pmd)
49 {
50 	pte_t pte;
51 
52 	/*
53 	 * Convert encoding		   pmd bits	    pte bits
54 	 *				dy..R...I...wr	  lIR.uswrdy.p
55 	 * empty			00..0...1...00 -> 010.000000.0
56 	 * prot-none, clean, old	00..1...1...00 -> 111.000000.1
57 	 * prot-none, clean, young	01..1...1...00 -> 111.000001.1
58 	 * prot-none, dirty, old	10..1...1...00 -> 111.000010.1
59 	 * prot-none, dirty, young	11..1...1...00 -> 111.000011.1
60 	 * read-only, clean, old	00..1...1...01 -> 111.000100.1
61 	 * read-only, clean, young	01..1...0...01 -> 101.000101.1
62 	 * read-only, dirty, old	10..1...1...01 -> 111.000110.1
63 	 * read-only, dirty, young	11..1...0...01 -> 101.000111.1
64 	 * read-write, clean, old	00..1...1...11 -> 111.001100.1
65 	 * read-write, clean, young	01..1...0...11 -> 101.001101.1
66 	 * read-write, dirty, old	10..0...1...11 -> 110.001110.1
67 	 * read-write, dirty, young	11..0...0...11 -> 100.001111.1
68 	 * HW-bits: R read-only, I invalid
69 	 * SW-bits: p present, y young, d dirty, r read, w write, s special,
70 	 *	    u unused, l large
71 	 */
72 	if (pmd_present(pmd)) {
73 		pte_val(pte) = pmd_val(pmd) & _SEGMENT_ENTRY_ORIGIN_LARGE;
74 		pte_val(pte) |= _PAGE_LARGE | _PAGE_PRESENT;
75 		pte_val(pte) |= (pmd_val(pmd) & _SEGMENT_ENTRY_READ) << 4;
76 		pte_val(pte) |= (pmd_val(pmd) & _SEGMENT_ENTRY_WRITE) << 4;
77 		pte_val(pte) |= (pmd_val(pmd) & _SEGMENT_ENTRY_INVALID) << 5;
78 		pte_val(pte) |= (pmd_val(pmd) & _SEGMENT_ENTRY_PROTECT);
79 		pte_val(pte) |= (pmd_val(pmd) & _SEGMENT_ENTRY_DIRTY) >> 10;
80 		pte_val(pte) |= (pmd_val(pmd) & _SEGMENT_ENTRY_YOUNG) >> 10;
81 	} else
82 		pte_val(pte) = _PAGE_INVALID;
83 	return pte;
84 }
85 
86 void set_huge_pte_at(struct mm_struct *mm, unsigned long addr,
87 		     pte_t *ptep, pte_t pte)
88 {
89 	pmd_t pmd = __pte_to_pmd(pte);
90 
91 	pmd_val(pmd) |= _SEGMENT_ENTRY_LARGE;
92 	*(pmd_t *) ptep = pmd;
93 }
94 
95 pte_t huge_ptep_get(pte_t *ptep)
96 {
97 	pmd_t pmd = *(pmd_t *) ptep;
98 
99 	return __pmd_to_pte(pmd);
100 }
101 
102 pte_t huge_ptep_get_and_clear(struct mm_struct *mm,
103 			      unsigned long addr, pte_t *ptep)
104 {
105 	pmd_t *pmdp = (pmd_t *) ptep;
106 	pte_t pte = huge_ptep_get(ptep);
107 
108 	pmdp_flush_direct(mm, addr, pmdp);
109 	pmd_val(*pmdp) = _SEGMENT_ENTRY_EMPTY;
110 	return pte;
111 }
112 
113 pte_t *huge_pte_alloc(struct mm_struct *mm,
114 			unsigned long addr, unsigned long sz)
115 {
116 	pgd_t *pgdp;
117 	pud_t *pudp;
118 	pmd_t *pmdp = NULL;
119 
120 	pgdp = pgd_offset(mm, addr);
121 	pudp = pud_alloc(mm, pgdp, addr);
122 	if (pudp)
123 		pmdp = pmd_alloc(mm, pudp, addr);
124 	return (pte_t *) pmdp;
125 }
126 
127 pte_t *huge_pte_offset(struct mm_struct *mm, unsigned long addr)
128 {
129 	pgd_t *pgdp;
130 	pud_t *pudp;
131 	pmd_t *pmdp = NULL;
132 
133 	pgdp = pgd_offset(mm, addr);
134 	if (pgd_present(*pgdp)) {
135 		pudp = pud_offset(pgdp, addr);
136 		if (pud_present(*pudp))
137 			pmdp = pmd_offset(pudp, addr);
138 	}
139 	return (pte_t *) pmdp;
140 }
141 
142 int pmd_huge(pmd_t pmd)
143 {
144 	return pmd_large(pmd);
145 }
146 
147 int pud_huge(pud_t pud)
148 {
149 	return 0;
150 }
151