blob: 74630989006d51beac9f9011eb3edaa27ad79053 [file] [log] [blame]
David Brazdil0f672f62019-12-10 10:32:29 +00001/* SPDX-License-Identifier: GPL-2.0-only */
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002/*
3 * Copyright (C) 2012 Regents of the University of California
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00004 */
5
6#ifndef _ASM_RISCV_PGTABLE_64_H
7#define _ASM_RISCV_PGTABLE_64_H
8
9#include <linux/const.h>
10
11#define PGDIR_SHIFT 30
12/* Size of region mapped by a page global directory */
13#define PGDIR_SIZE (_AC(1, UL) << PGDIR_SHIFT)
14#define PGDIR_MASK (~(PGDIR_SIZE - 1))
15
16#define PMD_SHIFT 21
17/* Size of region mapped by a page middle directory */
18#define PMD_SIZE (_AC(1, UL) << PMD_SHIFT)
19#define PMD_MASK (~(PMD_SIZE - 1))
20
21/* Page Middle Directory entry */
22typedef struct {
23 unsigned long pmd;
24} pmd_t;
25
26#define pmd_val(x) ((x).pmd)
27#define __pmd(x) ((pmd_t) { (x) })
28
29#define PTRS_PER_PMD (PAGE_SIZE / sizeof(pmd_t))
30
31static inline int pud_present(pud_t pud)
32{
33 return (pud_val(pud) & _PAGE_PRESENT);
34}
35
36static inline int pud_none(pud_t pud)
37{
38 return (pud_val(pud) == 0);
39}
40
41static inline int pud_bad(pud_t pud)
42{
43 return !pud_present(pud);
44}
45
46static inline void set_pud(pud_t *pudp, pud_t pud)
47{
48 *pudp = pud;
49}
50
51static inline void pud_clear(pud_t *pudp)
52{
53 set_pud(pudp, __pud(0));
54}
55
56static inline unsigned long pud_page_vaddr(pud_t pud)
57{
58 return (unsigned long)pfn_to_virt(pud_val(pud) >> _PAGE_PFN_SHIFT);
59}
60
61#define pmd_index(addr) (((addr) >> PMD_SHIFT) & (PTRS_PER_PMD - 1))
62
63static inline pmd_t *pmd_offset(pud_t *pud, unsigned long addr)
64{
65 return (pmd_t *)pud_page_vaddr(*pud) + pmd_index(addr);
66}
67
68static inline pmd_t pfn_pmd(unsigned long pfn, pgprot_t prot)
69{
70 return __pmd((pfn << _PAGE_PFN_SHIFT) | pgprot_val(prot));
71}
72
David Brazdil0f672f62019-12-10 10:32:29 +000073static inline unsigned long _pmd_pfn(pmd_t pmd)
74{
75 return pmd_val(pmd) >> _PAGE_PFN_SHIFT;
76}
77
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000078#define pmd_ERROR(e) \
79 pr_err("%s:%d: bad pmd %016lx.\n", __FILE__, __LINE__, pmd_val(e))
80
81#endif /* _ASM_RISCV_PGTABLE_64_H */