1 /*
2  * Copyright (C) 2012 Regents of the University of California
3  *
4  *   This program is free software; you can redistribute it and/or
5  *   modify it under the terms of the GNU General Public License
6  *   as published by the Free Software Foundation, version 2.
7  *
8  *   This program is distributed in the hope that it will be useful,
9  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
10  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11  *   GNU General Public License for more details.
12  */
13 
14 #ifndef _ASM_RISCV_PGTABLE_64_H
15 #define _ASM_RISCV_PGTABLE_64_H
16 
17 #include <linux/const.h>
18 
19 #define PGDIR_SHIFT     30
20 /* Size of region mapped by a page global directory */
21 #define PGDIR_SIZE      (_AC(1, UL) << PGDIR_SHIFT)
22 #define PGDIR_MASK      (~(PGDIR_SIZE - 1))
23 
24 #define PMD_SHIFT       21
25 /* Size of region mapped by a page middle directory */
26 #define PMD_SIZE        (_AC(1, UL) << PMD_SHIFT)
27 #define PMD_MASK        (~(PMD_SIZE - 1))
28 
29 /* Page Middle Directory entry */
30 typedef struct {
31 	unsigned long pmd;
32 } pmd_t;
33 
34 #define pmd_val(x)      ((x).pmd)
35 #define __pmd(x)        ((pmd_t) { (x) })
36 
37 #define PTRS_PER_PMD    (PAGE_SIZE / sizeof(pmd_t))
38 
39 static inline int pud_present(pud_t pud)
40 {
41 	return (pud_val(pud) & _PAGE_PRESENT);
42 }
43 
44 static inline int pud_none(pud_t pud)
45 {
46 	return (pud_val(pud) == 0);
47 }
48 
49 static inline int pud_bad(pud_t pud)
50 {
51 	return !pud_present(pud);
52 }
53 
54 static inline void set_pud(pud_t *pudp, pud_t pud)
55 {
56 	*pudp = pud;
57 }
58 
59 static inline void pud_clear(pud_t *pudp)
60 {
61 	set_pud(pudp, __pud(0));
62 }
63 
64 static inline unsigned long pud_page_vaddr(pud_t pud)
65 {
66 	return (unsigned long)pfn_to_virt(pud_val(pud) >> _PAGE_PFN_SHIFT);
67 }
68 
69 #define pmd_index(addr) (((addr) >> PMD_SHIFT) & (PTRS_PER_PMD - 1))
70 
71 static inline pmd_t *pmd_offset(pud_t *pud, unsigned long addr)
72 {
73 	return (pmd_t *)pud_page_vaddr(*pud) + pmd_index(addr);
74 }
75 
76 static inline pmd_t pfn_pmd(unsigned long pfn, pgprot_t prot)
77 {
78 	return __pmd((pfn << _PAGE_PFN_SHIFT) | pgprot_val(prot));
79 }
80 
81 #define pmd_ERROR(e) \
82 	pr_err("%s:%d: bad pmd %016lx.\n", __FILE__, __LINE__, pmd_val(e))
83 
84 #endif /* _ASM_RISCV_PGTABLE_64_H */
85