1 /* SPDX-License-Identifier: GPL-2.0 */
2 /*
3  * Copyright (C) 2020-2022 Loongson Technology Corporation Limited
4  *
5  * Derived from MIPS:
6  * Copyright (C) 1994, 95, 96, 97, 98, 99, 2000, 2003 Ralf Baechle
7  * Copyright (C) 1999, 2000, 2001 Silicon Graphics, Inc.
8  */
9 #ifndef _ASM_PGTABLE_H
10 #define _ASM_PGTABLE_H
11 
12 #include <linux/compiler.h>
13 #include <asm/addrspace.h>
14 #include <asm/pgtable-bits.h>
15 
16 #if CONFIG_PGTABLE_LEVELS == 2
17 #include <asm-generic/pgtable-nopmd.h>
18 #elif CONFIG_PGTABLE_LEVELS == 3
19 #include <asm-generic/pgtable-nopud.h>
20 #else
21 #include <asm-generic/pgtable-nop4d.h>
22 #endif
23 
24 #if CONFIG_PGTABLE_LEVELS == 2
25 #define PGDIR_SHIFT	(PAGE_SHIFT + (PAGE_SHIFT - 3))
26 #elif CONFIG_PGTABLE_LEVELS == 3
27 #define PMD_SHIFT	(PAGE_SHIFT + (PAGE_SHIFT - 3))
28 #define PMD_SIZE	(1UL << PMD_SHIFT)
29 #define PMD_MASK	(~(PMD_SIZE-1))
30 #define PGDIR_SHIFT	(PMD_SHIFT + (PAGE_SHIFT - 3))
31 #elif CONFIG_PGTABLE_LEVELS == 4
32 #define PMD_SHIFT	(PAGE_SHIFT + (PAGE_SHIFT - 3))
33 #define PMD_SIZE	(1UL << PMD_SHIFT)
34 #define PMD_MASK	(~(PMD_SIZE-1))
35 #define PUD_SHIFT	(PMD_SHIFT + (PAGE_SHIFT - 3))
36 #define PUD_SIZE	(1UL << PUD_SHIFT)
37 #define PUD_MASK	(~(PUD_SIZE-1))
38 #define PGDIR_SHIFT	(PUD_SHIFT + (PAGE_SHIFT - 3))
39 #endif
40 
41 #define PGDIR_SIZE	(1UL << PGDIR_SHIFT)
42 #define PGDIR_MASK	(~(PGDIR_SIZE-1))
43 
44 #define VA_BITS		(PGDIR_SHIFT + (PAGE_SHIFT - 3))
45 
46 #define PTRS_PER_PGD	(PAGE_SIZE >> 3)
47 #if CONFIG_PGTABLE_LEVELS > 3
48 #define PTRS_PER_PUD	(PAGE_SIZE >> 3)
49 #endif
50 #if CONFIG_PGTABLE_LEVELS > 2
51 #define PTRS_PER_PMD	(PAGE_SIZE >> 3)
52 #endif
53 #define PTRS_PER_PTE	(PAGE_SIZE >> 3)
54 
55 #define USER_PTRS_PER_PGD       ((TASK_SIZE64 / PGDIR_SIZE)?(TASK_SIZE64 / PGDIR_SIZE):1)
56 
57 #ifndef __ASSEMBLY__
58 
59 #include <linux/mm_types.h>
60 #include <linux/mmzone.h>
61 #include <asm/fixmap.h>
62 #include <asm/io.h>
63 
64 struct mm_struct;
65 struct vm_area_struct;
66 
67 /*
68  * ZERO_PAGE is a global shared page that is always zero; used
69  * for zero-mapped memory areas etc..
70  */
71 
72 extern unsigned long empty_zero_page;
73 extern unsigned long zero_page_mask;
74 
75 #define ZERO_PAGE(vaddr) \
76 	(virt_to_page((void *)(empty_zero_page + (((unsigned long)(vaddr)) & zero_page_mask))))
77 #define __HAVE_COLOR_ZERO_PAGE
78 
79 /*
80  * TLB refill handlers may also map the vmalloc area into xkvrange.
81  * Avoid the first couple of pages so NULL pointer dereferences will
82  * still reliably trap.
83  */
84 #define MODULES_VADDR	(vm_map_base + PCI_IOSIZE + (2 * PAGE_SIZE))
85 #define MODULES_END	(MODULES_VADDR + SZ_256M)
86 
87 #define VMALLOC_START	MODULES_END
88 #define VMALLOC_END	\
89 	(vm_map_base +	\
90 	 min(PTRS_PER_PGD * PTRS_PER_PUD * PTRS_PER_PMD * PTRS_PER_PTE * PAGE_SIZE, (1UL << cpu_vabits)) - PMD_SIZE)
91 
92 #define pte_ERROR(e) \
93 	pr_err("%s:%d: bad pte %016lx.\n", __FILE__, __LINE__, pte_val(e))
94 #ifndef __PAGETABLE_PMD_FOLDED
95 #define pmd_ERROR(e) \
96 	pr_err("%s:%d: bad pmd %016lx.\n", __FILE__, __LINE__, pmd_val(e))
97 #endif
98 #ifndef __PAGETABLE_PUD_FOLDED
99 #define pud_ERROR(e) \
100 	pr_err("%s:%d: bad pud %016lx.\n", __FILE__, __LINE__, pud_val(e))
101 #endif
102 #define pgd_ERROR(e) \
103 	pr_err("%s:%d: bad pgd %016lx.\n", __FILE__, __LINE__, pgd_val(e))
104 
105 extern pte_t invalid_pte_table[PTRS_PER_PTE];
106 
107 #ifndef __PAGETABLE_PUD_FOLDED
108 
109 typedef struct { unsigned long pud; } pud_t;
110 #define pud_val(x)	((x).pud)
111 #define __pud(x)	((pud_t) { (x) })
112 
113 extern pud_t invalid_pud_table[PTRS_PER_PUD];
114 
115 /*
116  * Empty pgd/p4d entries point to the invalid_pud_table.
117  */
118 static inline int p4d_none(p4d_t p4d)
119 {
120 	return p4d_val(p4d) == (unsigned long)invalid_pud_table;
121 }
122 
123 static inline int p4d_bad(p4d_t p4d)
124 {
125 	return p4d_val(p4d) & ~PAGE_MASK;
126 }
127 
128 static inline int p4d_present(p4d_t p4d)
129 {
130 	return p4d_val(p4d) != (unsigned long)invalid_pud_table;
131 }
132 
133 static inline void p4d_clear(p4d_t *p4dp)
134 {
135 	p4d_val(*p4dp) = (unsigned long)invalid_pud_table;
136 }
137 
138 static inline pud_t *p4d_pgtable(p4d_t p4d)
139 {
140 	return (pud_t *)p4d_val(p4d);
141 }
142 
143 static inline void set_p4d(p4d_t *p4d, p4d_t p4dval)
144 {
145 	*p4d = p4dval;
146 }
147 
148 #define p4d_phys(p4d)		virt_to_phys((void *)p4d_val(p4d))
149 #define p4d_page(p4d)		(pfn_to_page(p4d_phys(p4d) >> PAGE_SHIFT))
150 
151 #endif
152 
153 #ifndef __PAGETABLE_PMD_FOLDED
154 
155 typedef struct { unsigned long pmd; } pmd_t;
156 #define pmd_val(x)	((x).pmd)
157 #define __pmd(x)	((pmd_t) { (x) })
158 
159 extern pmd_t invalid_pmd_table[PTRS_PER_PMD];
160 
161 /*
162  * Empty pud entries point to the invalid_pmd_table.
163  */
164 static inline int pud_none(pud_t pud)
165 {
166 	return pud_val(pud) == (unsigned long)invalid_pmd_table;
167 }
168 
169 static inline int pud_bad(pud_t pud)
170 {
171 	return pud_val(pud) & ~PAGE_MASK;
172 }
173 
174 static inline int pud_present(pud_t pud)
175 {
176 	return pud_val(pud) != (unsigned long)invalid_pmd_table;
177 }
178 
179 static inline void pud_clear(pud_t *pudp)
180 {
181 	pud_val(*pudp) = ((unsigned long)invalid_pmd_table);
182 }
183 
184 static inline pmd_t *pud_pgtable(pud_t pud)
185 {
186 	return (pmd_t *)pud_val(pud);
187 }
188 
189 #define set_pud(pudptr, pudval) do { *(pudptr) = (pudval); } while (0)
190 
191 #define pud_phys(pud)		virt_to_phys((void *)pud_val(pud))
192 #define pud_page(pud)		(pfn_to_page(pud_phys(pud) >> PAGE_SHIFT))
193 
194 #endif
195 
196 /*
197  * Empty pmd entries point to the invalid_pte_table.
198  */
199 static inline int pmd_none(pmd_t pmd)
200 {
201 	return pmd_val(pmd) == (unsigned long)invalid_pte_table;
202 }
203 
204 static inline int pmd_bad(pmd_t pmd)
205 {
206 	return (pmd_val(pmd) & ~PAGE_MASK);
207 }
208 
209 static inline int pmd_present(pmd_t pmd)
210 {
211 	if (unlikely(pmd_val(pmd) & _PAGE_HUGE))
212 		return !!(pmd_val(pmd) & (_PAGE_PRESENT | _PAGE_PROTNONE));
213 
214 	return pmd_val(pmd) != (unsigned long)invalid_pte_table;
215 }
216 
217 static inline void pmd_clear(pmd_t *pmdp)
218 {
219 	pmd_val(*pmdp) = ((unsigned long)invalid_pte_table);
220 }
221 
222 #define set_pmd(pmdptr, pmdval) do { *(pmdptr) = (pmdval); } while (0)
223 
224 #define pmd_phys(pmd)		virt_to_phys((void *)pmd_val(pmd))
225 
226 #ifndef CONFIG_TRANSPARENT_HUGEPAGE
227 #define pmd_page(pmd)		(pfn_to_page(pmd_phys(pmd) >> PAGE_SHIFT))
228 #endif /* CONFIG_TRANSPARENT_HUGEPAGE  */
229 
230 #define pmd_page_vaddr(pmd)	pmd_val(pmd)
231 
232 extern pmd_t mk_pmd(struct page *page, pgprot_t prot);
233 extern void set_pmd_at(struct mm_struct *mm, unsigned long addr, pmd_t *pmdp, pmd_t pmd);
234 
235 #define pte_page(x)		pfn_to_page(pte_pfn(x))
236 #define pte_pfn(x)		((unsigned long)(((x).pte & _PFN_MASK) >> _PFN_SHIFT))
237 #define pfn_pte(pfn, prot)	__pte(((pfn) << _PFN_SHIFT) | pgprot_val(prot))
238 #define pfn_pmd(pfn, prot)	__pmd(((pfn) << _PFN_SHIFT) | pgprot_val(prot))
239 
240 /*
241  * Initialize a new pgd / pmd table with invalid pointers.
242  */
243 extern void pgd_init(unsigned long page);
244 extern void pud_init(unsigned long page, unsigned long pagetable);
245 extern void pmd_init(unsigned long page, unsigned long pagetable);
246 
247 /*
248  * Non-present pages:  high 40 bits are offset, next 8 bits type,
249  * low 16 bits zero.
250  */
251 static inline pte_t mk_swap_pte(unsigned long type, unsigned long offset)
252 { pte_t pte; pte_val(pte) = (type << 16) | (offset << 24); return pte; }
253 
254 #define __swp_type(x)		(((x).val >> 16) & 0xff)
255 #define __swp_offset(x)		((x).val >> 24)
256 #define __swp_entry(type, offset) ((swp_entry_t) { pte_val(mk_swap_pte((type), (offset))) })
257 #define __pte_to_swp_entry(pte) ((swp_entry_t) { pte_val(pte) })
258 #define __swp_entry_to_pte(x)	((pte_t) { (x).val })
259 #define __pmd_to_swp_entry(pmd) ((swp_entry_t) { pmd_val(pmd) })
260 #define __swp_entry_to_pmd(x)	((pmd_t) { (x).val | _PAGE_HUGE })
261 
262 extern void paging_init(void);
263 
264 #define pte_none(pte)		(!(pte_val(pte) & ~_PAGE_GLOBAL))
265 #define pte_present(pte)	(pte_val(pte) & (_PAGE_PRESENT | _PAGE_PROTNONE))
266 #define pte_no_exec(pte)	(pte_val(pte) & _PAGE_NO_EXEC)
267 
268 static inline void set_pte(pte_t *ptep, pte_t pteval)
269 {
270 	*ptep = pteval;
271 	if (pte_val(pteval) & _PAGE_GLOBAL) {
272 		pte_t *buddy = ptep_buddy(ptep);
273 		/*
274 		 * Make sure the buddy is global too (if it's !none,
275 		 * it better already be global)
276 		 */
277 #ifdef CONFIG_SMP
278 		/*
279 		 * For SMP, multiple CPUs can race, so we need to do
280 		 * this atomically.
281 		 */
282 		unsigned long page_global = _PAGE_GLOBAL;
283 		unsigned long tmp;
284 
285 		__asm__ __volatile__ (
286 		"1:"	__LL	"%[tmp], %[buddy]		\n"
287 		"	bnez	%[tmp], 2f			\n"
288 		"	 or	%[tmp], %[tmp], %[global]	\n"
289 			__SC	"%[tmp], %[buddy]		\n"
290 		"	beqz	%[tmp], 1b			\n"
291 		"	nop					\n"
292 		"2:						\n"
293 		__WEAK_LLSC_MB
294 		: [buddy] "+m" (buddy->pte), [tmp] "=&r" (tmp)
295 		: [global] "r" (page_global));
296 #else /* !CONFIG_SMP */
297 		if (pte_none(*buddy))
298 			pte_val(*buddy) = pte_val(*buddy) | _PAGE_GLOBAL;
299 #endif /* CONFIG_SMP */
300 	}
301 }
302 
303 static inline void set_pte_at(struct mm_struct *mm, unsigned long addr,
304 			      pte_t *ptep, pte_t pteval)
305 {
306 	set_pte(ptep, pteval);
307 }
308 
309 static inline void pte_clear(struct mm_struct *mm, unsigned long addr, pte_t *ptep)
310 {
311 	/* Preserve global status for the pair */
312 	if (pte_val(*ptep_buddy(ptep)) & _PAGE_GLOBAL)
313 		set_pte_at(mm, addr, ptep, __pte(_PAGE_GLOBAL));
314 	else
315 		set_pte_at(mm, addr, ptep, __pte(0));
316 }
317 
318 #define PGD_T_LOG2	(__builtin_ffs(sizeof(pgd_t)) - 1)
319 #define PMD_T_LOG2	(__builtin_ffs(sizeof(pmd_t)) - 1)
320 #define PTE_T_LOG2	(__builtin_ffs(sizeof(pte_t)) - 1)
321 
322 extern pgd_t swapper_pg_dir[];
323 extern pgd_t invalid_pg_dir[];
324 
325 /*
326  * The following only work if pte_present() is true.
327  * Undefined behaviour if not..
328  */
329 static inline int pte_write(pte_t pte)	{ return pte_val(pte) & _PAGE_WRITE; }
330 static inline int pte_young(pte_t pte)	{ return pte_val(pte) & _PAGE_ACCESSED; }
331 static inline int pte_dirty(pte_t pte)	{ return pte_val(pte) & _PAGE_MODIFIED; }
332 
333 static inline pte_t pte_mkold(pte_t pte)
334 {
335 	pte_val(pte) &= ~_PAGE_ACCESSED;
336 	return pte;
337 }
338 
339 static inline pte_t pte_mkyoung(pte_t pte)
340 {
341 	pte_val(pte) |= _PAGE_ACCESSED;
342 	return pte;
343 }
344 
345 static inline pte_t pte_mkclean(pte_t pte)
346 {
347 	pte_val(pte) &= ~(_PAGE_DIRTY | _PAGE_MODIFIED);
348 	return pte;
349 }
350 
351 static inline pte_t pte_mkdirty(pte_t pte)
352 {
353 	pte_val(pte) |= (_PAGE_DIRTY | _PAGE_MODIFIED);
354 	return pte;
355 }
356 
357 static inline pte_t pte_mkwrite(pte_t pte)
358 {
359 	pte_val(pte) |= (_PAGE_WRITE | _PAGE_DIRTY);
360 	return pte;
361 }
362 
363 static inline pte_t pte_wrprotect(pte_t pte)
364 {
365 	pte_val(pte) &= ~(_PAGE_WRITE | _PAGE_DIRTY);
366 	return pte;
367 }
368 
369 static inline int pte_huge(pte_t pte)	{ return pte_val(pte) & _PAGE_HUGE; }
370 
371 static inline pte_t pte_mkhuge(pte_t pte)
372 {
373 	pte_val(pte) |= _PAGE_HUGE;
374 	return pte;
375 }
376 
377 #if defined(CONFIG_ARCH_HAS_PTE_SPECIAL)
378 static inline int pte_special(pte_t pte)	{ return pte_val(pte) & _PAGE_SPECIAL; }
379 static inline pte_t pte_mkspecial(pte_t pte)	{ pte_val(pte) |= _PAGE_SPECIAL; return pte; }
380 #endif /* CONFIG_ARCH_HAS_PTE_SPECIAL */
381 
382 #define pte_accessible pte_accessible
383 static inline unsigned long pte_accessible(struct mm_struct *mm, pte_t a)
384 {
385 	if (pte_val(a) & _PAGE_PRESENT)
386 		return true;
387 
388 	if ((pte_val(a) & _PAGE_PROTNONE) &&
389 			atomic_read(&mm->tlb_flush_pending))
390 		return true;
391 
392 	return false;
393 }
394 
395 /*
396  * Conversion functions: convert a page and protection to a page entry,
397  * and a page entry and page directory to the page they refer to.
398  */
399 #define mk_pte(page, pgprot)	pfn_pte(page_to_pfn(page), (pgprot))
400 
401 static inline pte_t pte_modify(pte_t pte, pgprot_t newprot)
402 {
403 	return __pte((pte_val(pte) & _PAGE_CHG_MASK) |
404 		     (pgprot_val(newprot) & ~_PAGE_CHG_MASK));
405 }
406 
407 extern void __update_tlb(struct vm_area_struct *vma,
408 			unsigned long address, pte_t *ptep);
409 
410 static inline void update_mmu_cache(struct vm_area_struct *vma,
411 			unsigned long address, pte_t *ptep)
412 {
413 	__update_tlb(vma, address, ptep);
414 }
415 
416 static inline void update_mmu_cache_pmd(struct vm_area_struct *vma,
417 			unsigned long address, pmd_t *pmdp)
418 {
419 	__update_tlb(vma, address, (pte_t *)pmdp);
420 }
421 
422 #define kern_addr_valid(addr)	(1)
423 
424 static inline unsigned long pmd_pfn(pmd_t pmd)
425 {
426 	return (pmd_val(pmd) & _PFN_MASK) >> _PFN_SHIFT;
427 }
428 
429 #ifdef CONFIG_TRANSPARENT_HUGEPAGE
430 
431 /* We don't have hardware dirty/accessed bits, generic_pmdp_establish is fine.*/
432 #define pmdp_establish generic_pmdp_establish
433 
434 static inline int pmd_trans_huge(pmd_t pmd)
435 {
436 	return !!(pmd_val(pmd) & _PAGE_HUGE) && pmd_present(pmd);
437 }
438 
439 static inline pmd_t pmd_mkhuge(pmd_t pmd)
440 {
441 	pmd_val(pmd) = (pmd_val(pmd) & ~(_PAGE_GLOBAL)) |
442 		((pmd_val(pmd) & _PAGE_GLOBAL) << (_PAGE_HGLOBAL_SHIFT - _PAGE_GLOBAL_SHIFT));
443 	pmd_val(pmd) |= _PAGE_HUGE;
444 
445 	return pmd;
446 }
447 
448 #define pmd_write pmd_write
449 static inline int pmd_write(pmd_t pmd)
450 {
451 	return !!(pmd_val(pmd) & _PAGE_WRITE);
452 }
453 
454 static inline pmd_t pmd_mkwrite(pmd_t pmd)
455 {
456 	pmd_val(pmd) |= (_PAGE_WRITE | _PAGE_DIRTY);
457 	return pmd;
458 }
459 
460 static inline pmd_t pmd_wrprotect(pmd_t pmd)
461 {
462 	pmd_val(pmd) &= ~(_PAGE_WRITE | _PAGE_DIRTY);
463 	return pmd;
464 }
465 
466 static inline int pmd_dirty(pmd_t pmd)
467 {
468 	return !!(pmd_val(pmd) & _PAGE_MODIFIED);
469 }
470 
471 static inline pmd_t pmd_mkclean(pmd_t pmd)
472 {
473 	pmd_val(pmd) &= ~(_PAGE_DIRTY | _PAGE_MODIFIED);
474 	return pmd;
475 }
476 
477 static inline pmd_t pmd_mkdirty(pmd_t pmd)
478 {
479 	pmd_val(pmd) |= (_PAGE_DIRTY | _PAGE_MODIFIED);
480 	return pmd;
481 }
482 
483 static inline int pmd_young(pmd_t pmd)
484 {
485 	return !!(pmd_val(pmd) & _PAGE_ACCESSED);
486 }
487 
488 static inline pmd_t pmd_mkold(pmd_t pmd)
489 {
490 	pmd_val(pmd) &= ~_PAGE_ACCESSED;
491 	return pmd;
492 }
493 
494 static inline pmd_t pmd_mkyoung(pmd_t pmd)
495 {
496 	pmd_val(pmd) |= _PAGE_ACCESSED;
497 	return pmd;
498 }
499 
500 static inline struct page *pmd_page(pmd_t pmd)
501 {
502 	if (pmd_trans_huge(pmd))
503 		return pfn_to_page(pmd_pfn(pmd));
504 
505 	return pfn_to_page(pmd_phys(pmd) >> PAGE_SHIFT);
506 }
507 
508 static inline pmd_t pmd_modify(pmd_t pmd, pgprot_t newprot)
509 {
510 	pmd_val(pmd) = (pmd_val(pmd) & _HPAGE_CHG_MASK) |
511 				(pgprot_val(newprot) & ~_HPAGE_CHG_MASK);
512 	return pmd;
513 }
514 
515 static inline pmd_t pmd_mkinvalid(pmd_t pmd)
516 {
517 	pmd_val(pmd) &= ~(_PAGE_PRESENT | _PAGE_VALID | _PAGE_DIRTY | _PAGE_PROTNONE);
518 
519 	return pmd;
520 }
521 
522 /*
523  * The generic version pmdp_huge_get_and_clear uses a version of pmd_clear() with a
524  * different prototype.
525  */
526 #define __HAVE_ARCH_PMDP_HUGE_GET_AND_CLEAR
527 static inline pmd_t pmdp_huge_get_and_clear(struct mm_struct *mm,
528 					    unsigned long address, pmd_t *pmdp)
529 {
530 	pmd_t old = *pmdp;
531 
532 	pmd_clear(pmdp);
533 
534 	return old;
535 }
536 
537 #endif /* CONFIG_TRANSPARENT_HUGEPAGE */
538 
539 #ifdef CONFIG_NUMA_BALANCING
540 static inline long pte_protnone(pte_t pte)
541 {
542 	return (pte_val(pte) & _PAGE_PROTNONE);
543 }
544 
545 static inline long pmd_protnone(pmd_t pmd)
546 {
547 	return (pmd_val(pmd) & _PAGE_PROTNONE);
548 }
549 #endif /* CONFIG_NUMA_BALANCING */
550 
551 /*
552  * We provide our own get_unmapped area to cope with the virtual aliasing
553  * constraints placed on us by the cache architecture.
554  */
555 #define HAVE_ARCH_UNMAPPED_AREA
556 #define HAVE_ARCH_UNMAPPED_AREA_TOPDOWN
557 
558 #endif /* !__ASSEMBLY__ */
559 
560 #endif /* _ASM_PGTABLE_H */
561