pmap.h revision 298580
1/*-
2 * Copyright (c) 1991 Regents of the University of California.
3 * All rights reserved.
4 *
5 * This code is derived from software contributed to Berkeley by
6 * the Systems Programming Group of the University of Utah Computer
7 * Science Department and William Jolitz of UUNET Technologies Inc.
8 *
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
11 * are met:
12 * 1. Redistributions of source code must retain the above copyright
13 *    notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 *    notice, this list of conditions and the following disclaimer in the
16 *    documentation and/or other materials provided with the distribution.
17 * 3. Neither the name of the University nor the names of its contributors
18 *    may be used to endorse or promote products derived from this software
19 *    without specific prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * SUCH DAMAGE.
32 *
33 * $FreeBSD: head/sys/riscv/include/pmap.h 298580 2016-04-25 14:47:51Z br $
34 */
35
36#ifndef _MACHINE_PMAP_H_
37#define	_MACHINE_PMAP_H_
38
39#include <machine/pte.h>
40
41#ifndef LOCORE
42
43#include <sys/queue.h>
44#include <sys/_lock.h>
45#include <sys/_mutex.h>
46
47#ifdef _KERNEL
48
49#define	vtophys(va)	pmap_kextract((vm_offset_t)(va))
50
51#endif
52
53#define	pmap_page_get_memattr(m)	((m)->md.pv_memattr)
54#define	pmap_page_is_write_mapped(m)	(((m)->aflags & PGA_WRITEABLE) != 0)
55void pmap_page_set_memattr(vm_page_t m, vm_memattr_t ma);
56
57/*
58 * Pmap stuff
59 */
60
61struct md_page {
62	TAILQ_HEAD(,pv_entry)	pv_list;
63	int			pv_gen;
64	vm_memattr_t		pv_memattr;
65};
66
67/*
68 * This structure is used to hold a virtual<->physical address
69 * association and is used mostly by bootstrap code
70 */
71struct pv_addr {
72	SLIST_ENTRY(pv_addr) pv_list;
73	vm_offset_t	pv_va;
74	vm_paddr_t	pv_pa;
75};
76
77/* An entry in the list of all pmaps */
78struct pmap_list_entry {
79	SLIST_ENTRY(pmap_list_entry) pmap_link;
80	struct pmap *pmap;
81};
82
83struct pmap {
84	struct mtx		pm_mtx;
85	struct pmap_statistics	pm_stats;	/* pmap statictics */
86	pd_entry_t		*pm_l1;
87	TAILQ_HEAD(,pv_chunk)	pm_pvchunk;	/* list of mappings in pmap */
88	struct pmap_list_entry	*p_entry; /* Place in the list of all pmaps */
89};
90
91typedef struct pv_entry {
92	vm_offset_t		pv_va;	/* virtual address for mapping */
93	TAILQ_ENTRY(pv_entry)	pv_next;
94} *pv_entry_t;
95
96/*
97 * pv_entries are allocated in chunks per-process.  This avoids the
98 * need to track per-pmap assignments.
99 */
100#define	_NPCM	3
101#define	_NPCPV	168
102struct pv_chunk {
103	struct pmap *		pc_pmap;
104	TAILQ_ENTRY(pv_chunk)	pc_list;
105	uint64_t		pc_map[_NPCM];  /* bitmap; 1 = free */
106	TAILQ_ENTRY(pv_chunk)	pc_lru;
107	struct pv_entry		pc_pventry[_NPCPV];
108};
109
110typedef struct pmap *pmap_t;
111
112#ifdef _KERNEL
113extern struct pmap	kernel_pmap_store;
114#define	kernel_pmap	(&kernel_pmap_store)
115#define	pmap_kernel()	kernel_pmap
116
117#define	PMAP_ASSERT_LOCKED(pmap) \
118				mtx_assert(&(pmap)->pm_mtx, MA_OWNED)
119#define	PMAP_LOCK(pmap)		mtx_lock(&(pmap)->pm_mtx)
120#define	PMAP_LOCK_ASSERT(pmap, type) \
121				mtx_assert(&(pmap)->pm_mtx, (type))
122#define	PMAP_LOCK_DESTROY(pmap)	mtx_destroy(&(pmap)->pm_mtx)
123#define	PMAP_LOCK_INIT(pmap)	mtx_init(&(pmap)->pm_mtx, "pmap", \
124				    NULL, MTX_DEF | MTX_DUPOK)
125#define	PMAP_OWNED(pmap)	mtx_owned(&(pmap)->pm_mtx)
126#define	PMAP_MTX(pmap)		(&(pmap)->pm_mtx)
127#define	PMAP_TRYLOCK(pmap)	mtx_trylock(&(pmap)->pm_mtx)
128#define	PMAP_UNLOCK(pmap)	mtx_unlock(&(pmap)->pm_mtx)
129
130#define	PHYS_AVAIL_SIZE	10
131extern vm_paddr_t phys_avail[];
132extern vm_paddr_t dump_avail[];
133extern vm_offset_t virtual_avail;
134extern vm_offset_t virtual_end;
135
136/*
137 * Macros to test if a mapping is mappable with an L1 Section mapping
138 * or an L2 Large Page mapping.
139 */
140#define	L1_MAPPABLE_P(va, pa, size)					\
141	((((va) | (pa)) & L1_OFFSET) == 0 && (size) >= L1_SIZE)
142
143void	pmap_bootstrap(vm_offset_t, vm_paddr_t, vm_size_t);
144void	pmap_kenter_device(vm_offset_t, vm_size_t, vm_paddr_t);
145vm_paddr_t pmap_kextract(vm_offset_t va);
146void	pmap_kremove(vm_offset_t);
147void	pmap_kremove_device(vm_offset_t, vm_size_t);
148
149void	*pmap_mapdev(vm_offset_t, vm_size_t);
150void	*pmap_mapbios(vm_paddr_t, vm_size_t);
151void	pmap_unmapdev(vm_offset_t, vm_size_t);
152void	pmap_unmapbios(vm_offset_t, vm_size_t);
153
154boolean_t pmap_map_io_transient(vm_page_t *, vm_offset_t *, int, boolean_t);
155void	pmap_unmap_io_transient(vm_page_t *, vm_offset_t *, int, boolean_t);
156
157bool	pmap_get_tables(pmap_t, vm_offset_t, pd_entry_t **, pd_entry_t **,
158    pt_entry_t **);
159
160#define	pmap_page_is_mapped(m)	(!TAILQ_EMPTY(&(m)->md.pv_list))
161
162#endif	/* _KERNEL */
163
164#endif	/* !LOCORE */
165
166#endif	/* !_MACHINE_PMAP_H_ */
167