Home | History | Annotate | Line # | Download | only in include
pmap.h revision 1.9
      1  1.9    skrll /* $NetBSD: pmap.h,v 1.9 2021/05/01 07:41:24 skrll Exp $ */
      2  1.1     matt 
      3  1.3     maxv /*
      4  1.9    skrll  * Copyright (c) 2014, 2019, 2021 The NetBSD Foundation, Inc.
      5  1.1     matt  * All rights reserved.
      6  1.1     matt  *
      7  1.1     matt  * This code is derived from software contributed to The NetBSD Foundation
      8  1.9    skrll  * by Matt Thomas (of 3am Software Foundry), Maxime Villard, and
      9  1.9    skrll  * Nick Hudson.
     10  1.1     matt  *
     11  1.1     matt  * Redistribution and use in source and binary forms, with or without
     12  1.1     matt  * modification, are permitted provided that the following conditions
     13  1.1     matt  * are met:
     14  1.1     matt  * 1. Redistributions of source code must retain the above copyright
     15  1.1     matt  *    notice, this list of conditions and the following disclaimer.
     16  1.1     matt  * 2. Redistributions in binary form must reproduce the above copyright
     17  1.1     matt  *    notice, this list of conditions and the following disclaimer in the
     18  1.1     matt  *    documentation and/or other materials provided with the distribution.
     19  1.1     matt  *
     20  1.1     matt  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     21  1.1     matt  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     22  1.1     matt  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     23  1.1     matt  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     24  1.1     matt  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     25  1.1     matt  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     26  1.1     matt  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     27  1.1     matt  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     28  1.1     matt  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     29  1.1     matt  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     30  1.1     matt  * POSSIBILITY OF SUCH DAMAGE.
     31  1.1     matt  */
     32  1.1     matt 
     33  1.1     matt #ifndef _RISCV_PMAP_H_
     34  1.1     matt #define _RISCV_PMAP_H_
     35  1.1     matt 
     36  1.1     matt #ifdef _KERNEL_OPT
     37  1.1     matt #include "opt_modular.h"
     38  1.1     matt #endif
     39  1.1     matt 
     40  1.1     matt #if !defined(_MODULE)
     41  1.1     matt 
     42  1.9    skrll #include <sys/cdefs.h>
     43  1.1     matt #include <sys/types.h>
     44  1.1     matt #include <sys/pool.h>
     45  1.1     matt #include <sys/evcnt.h>
     46  1.1     matt 
     47  1.2     maxv #include <uvm/uvm_physseg.h>
     48  1.1     matt #include <uvm/pmap/vmpagemd.h>
     49  1.1     matt 
     50  1.1     matt #include <riscv/pte.h>
     51  1.9    skrll #include <riscv/sysreg.h>
     52  1.1     matt 
     53  1.7    skrll #define PMAP_SEGTABSIZE	NPTEPG
     54  1.9    skrll #define PMAP_PDETABSIZE	NPTEPG
     55  1.1     matt 
     56  1.1     matt #ifdef _LP64
     57  1.9    skrll #define PTPSHIFT	3
     58  1.9    skrll /* This is SV48. */
     59  1.9    skrll //#define SEGLENGTH + SEGSHIFT + SEGSHIFT */
     60  1.9    skrll 
     61  1.9    skrll /* This is SV39. */
     62  1.9    skrll #define XSEGSHIFT	(SEGSHIFT + SEGLENGTH)
     63  1.9    skrll #define NBXSEG		(1ULL << XSEGSHIFT)
     64  1.9    skrll #define XSEGOFSET	(NBXSEG - 1)		/* byte offset into xsegment */
     65  1.9    skrll #define XSEGLENGTH	(PGSHIFT - 3)
     66  1.9    skrll #define NXSEGPG		(1 << XSEGLENGTH)
     67  1.1     matt #else
     68  1.9    skrll #define PTPSHIFT	2
     69  1.9    skrll #define XSEGSHIFT	SEGLENGTH
     70  1.1     matt #endif
     71  1.3     maxv 
     72  1.9    skrll #define SEGLENGTH	(PGSHIFT - PTPSHIFT)
     73  1.9    skrll #define SEGSHIFT	(SEGLENGTH + PGSHIFT)
     74  1.9    skrll #define NBSEG		(1 << SEGSHIFT)		/* bytes/segment */
     75  1.9    skrll #define SEGOFSET	(NBSEG - 1)		/* byte offset into segment */
     76  1.1     matt 
     77  1.1     matt #define KERNEL_PID	0
     78  1.1     matt 
     79  1.1     matt #define PMAP_HWPAGEWALKER		1
     80  1.1     matt #define PMAP_TLB_MAX			1
     81  1.1     matt #ifdef _LP64
     82  1.1     matt #define PMAP_INVALID_PDETAB_ADDRESS	((pmap_pdetab_t *)(VM_MIN_KERNEL_ADDRESS - PAGE_SIZE))
     83  1.1     matt #define PMAP_INVALID_SEGTAB_ADDRESS	((pmap_segtab_t *)(VM_MIN_KERNEL_ADDRESS - PAGE_SIZE))
     84  1.1     matt #else
     85  1.1     matt #define PMAP_INVALID_PDETAB_ADDRESS	((pmap_pdetab_t *)0xdeadbeef)
     86  1.1     matt #define PMAP_INVALID_SEGTAB_ADDRESS	((pmap_segtab_t *)0xdeadbeef)
     87  1.1     matt #endif
     88  1.9    skrll #define PMAP_TLB_NUM_PIDS		(__SHIFTOUT_MASK(SATP_ASID) + 1)
     89  1.9    skrll #define PMAP_TLB_BITMAP_LENGTH          PMAP_TLB_NUM_PIDS
     90  1.1     matt #define PMAP_TLB_FLUSH_ASID_ON_RESET	false
     91  1.1     matt 
     92  1.1     matt #define pmap_phys_address(x)		(x)
     93  1.1     matt 
     94  1.2     maxv #ifndef __BSD_PTENTRY_T__
     95  1.2     maxv #define __BSD_PTENTRY_T__
     96  1.2     maxv #ifdef _LP64
     97  1.2     maxv #define PRIxPTE         PRIx64
     98  1.2     maxv #else
     99  1.2     maxv #define PRIxPTE         PRIx32
    100  1.2     maxv #endif
    101  1.2     maxv #endif /* __BSD_PTENTRY_T__ */
    102  1.2     maxv 
    103  1.1     matt #define PMAP_NEED_PROCWR
    104  1.1     matt static inline void
    105  1.1     matt pmap_procwr(struct proc *p, vaddr_t va, vsize_t len)
    106  1.1     matt {
    107  1.1     matt 	__asm __volatile("fence\trw,rw; fence.i");
    108  1.1     matt }
    109  1.1     matt 
    110  1.1     matt #include <uvm/pmap/tlb.h>
    111  1.1     matt #include <uvm/pmap/pmap_tlb.h>
    112  1.1     matt 
    113  1.1     matt #define PMAP_GROWKERNEL
    114  1.1     matt #define PMAP_STEAL_MEMORY
    115  1.1     matt 
    116  1.1     matt #ifdef _KERNEL
    117  1.1     matt 
    118  1.1     matt #define __HAVE_PMAP_MD
    119  1.1     matt struct pmap_md {
    120  1.1     matt 	paddr_t md_ptbr;
    121  1.2     maxv 	pd_entry_t *md_pdetab;
    122  1.1     matt };
    123  1.1     matt 
    124  1.4  thorpej void	pmap_bootstrap(void);
    125  1.4  thorpej 
    126  1.1     matt struct vm_page *
    127  1.6    skrll 	pmap_md_alloc_poolpage(int flags);
    128  1.6    skrll vaddr_t	pmap_md_map_poolpage(paddr_t, vsize_t);
    129  1.6    skrll void	pmap_md_unmap_poolpage(vaddr_t, vsize_t);
    130  1.6    skrll bool	pmap_md_direct_mapped_vaddr_p(vaddr_t);
    131  1.6    skrll bool	pmap_md_io_vaddr_p(vaddr_t);
    132  1.6    skrll paddr_t	pmap_md_direct_mapped_vaddr_to_paddr(vaddr_t);
    133  1.6    skrll vaddr_t	pmap_md_direct_map_paddr(paddr_t);
    134  1.6    skrll void	pmap_md_init(void);
    135  1.6    skrll bool	pmap_md_tlb_check_entry(void *, vaddr_t, tlb_asid_t, pt_entry_t);
    136  1.1     matt 
    137  1.9    skrll void	pmap_md_xtab_activate(struct pmap *, struct lwp *);
    138  1.9    skrll void	pmap_md_xtab_deactivate(struct pmap *);
    139  1.1     matt void	pmap_md_pdetab_init(struct pmap *);
    140  1.2     maxv bool	pmap_md_ok_to_steal_p(const uvm_physseg_t, size_t);
    141  1.1     matt 
    142  1.3     maxv extern vaddr_t pmap_direct_base;
    143  1.3     maxv extern vaddr_t pmap_direct_end;
    144  1.3     maxv #define PMAP_DIRECT_MAP(pa)	(pmap_direct_base + (pa))
    145  1.3     maxv #define PMAP_DIRECT_UNMAP(va)	((paddr_t)(va) - pmap_direct_base)
    146  1.3     maxv 
    147  1.9    skrll #define MEGAPAGE_TRUNC(x)	((x) & ~SEGOFSET)
    148  1.9    skrll #define MEGAPAGE_ROUND(x)	MEGAPAGE_TRUNC((x) + SEGOFSET)
    149  1.9    skrll 
    150  1.1     matt #ifdef __PMAP_PRIVATE
    151  1.1     matt static inline void
    152  1.8    skrll pmap_md_page_syncicache(struct vm_page_md *mdpg, const kcpuset_t *kc)
    153  1.1     matt {
    154  1.1     matt 	__asm __volatile("fence\trw,rw; fence.i");
    155  1.1     matt }
    156  1.1     matt 
    157  1.1     matt /*
    158  1.1     matt  * Virtual Cache Alias helper routines.  Not a problem for RISCV CPUs.
    159  1.1     matt  */
    160  1.1     matt static inline bool
    161  1.8    skrll pmap_md_vca_add(struct vm_page_md *mdpg, vaddr_t va, pt_entry_t *nptep)
    162  1.1     matt {
    163  1.1     matt 	return false;
    164  1.1     matt }
    165  1.1     matt 
    166  1.1     matt static inline void
    167  1.8    skrll pmap_md_vca_remove(struct vm_page_md *mdpg, vaddr_t va)
    168  1.1     matt {
    169  1.1     matt }
    170  1.1     matt 
    171  1.1     matt static inline void
    172  1.8    skrll pmap_md_vca_clean(struct vm_page_md *mdpg, vaddr_t va, int op)
    173  1.1     matt {
    174  1.1     matt }
    175  1.1     matt 
    176  1.1     matt static inline size_t
    177  1.1     matt pmap_md_tlb_asid_max(void)
    178  1.1     matt {
    179  1.1     matt 	return PMAP_TLB_NUM_PIDS - 1;
    180  1.1     matt }
    181  1.5    skrll 
    182  1.1     matt #endif /* __PMAP_PRIVATE */
    183  1.1     matt #endif /* _KERNEL */
    184  1.1     matt 
    185  1.1     matt #include <uvm/pmap/pmap.h>
    186  1.1     matt 
    187  1.1     matt #endif /* !_MODULE */
    188  1.1     matt 
    189  1.1     matt #if defined(MODULAR) || defined(_MODULE)
    190  1.1     matt /*
    191  1.1     matt  * Define a compatible vm_page_md so that struct vm_page is the same size
    192  1.1     matt  * whether we are using modules or not.
    193  1.1     matt  */
    194  1.1     matt #ifndef __HAVE_VM_PAGE_MD
    195  1.1     matt #define __HAVE_VM_PAGE_MD
    196  1.1     matt 
    197  1.1     matt struct vm_page_md {
    198  1.1     matt 	uintptr_t mdpg_dummy[3];
    199  1.1     matt };
    200  1.2     maxv __CTASSERT(sizeof(struct vm_page_md) == sizeof(uintptr_t)*3);
    201  1.1     matt 
    202  1.2     maxv #endif /* !__HAVE_VM_PAGE_MD */
    203  1.1     matt 
    204  1.1     matt #endif /* MODULAR || _MODULE */
    205  1.1     matt 
    206  1.1     matt #endif /* !_RISCV_PMAP_H_ */
    207