1187301Sgonzo/*-
2178172Simp * Copyright (c) 1991 Regents of the University of California.
3178172Simp * All rights reserved.
4178172Simp *
5178172Simp * This code is derived from software contributed to Berkeley by
6178172Simp * the Systems Programming Group of the University of Utah Computer
7178172Simp * Science Department and William Jolitz of UUNET Technologies Inc.
8178172Simp *
9178172Simp * Redistribution and use in source and binary forms, with or without
10178172Simp * modification, are permitted provided that the following conditions
11178172Simp * are met:
12178172Simp * 1. Redistributions of source code must retain the above copyright
13178172Simp *    notice, this list of conditions and the following disclaimer.
14178172Simp * 2. Redistributions in binary form must reproduce the above copyright
15178172Simp *    notice, this list of conditions and the following disclaimer in the
16178172Simp *    documentation and/or other materials provided with the distribution.
17178172Simp * 4. Neither the name of the University nor the names of its contributors
18178172Simp *    may be used to endorse or promote products derived from this software
19178172Simp *    without specific prior written permission.
20178172Simp *
21178172Simp * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22178172Simp * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23178172Simp * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24178172Simp * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25178172Simp * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26178172Simp * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27178172Simp * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28178172Simp * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29178172Simp * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30178172Simp * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31178172Simp * SUCH DAMAGE.
32178172Simp *
33178172Simp * Derived from hp300 version by Mike Hibler, this version by William
34178172Simp * Jolitz uses a recursive map [a pde points to the page directory] to
35178172Simp * map the page tables using the pagetables themselves. This is done to
36178172Simp * reduce the impact on kernel virtual memory for lots of sparse address
37178172Simp * space, and to reduce the cost of memory to each process.
38178172Simp *
39178172Simp *	from: hp300: @(#)pmap.h 7.2 (Berkeley) 12/16/90
40178172Simp *	from: @(#)pmap.h	7.4 (Berkeley) 5/12/91
41178172Simp *	from: src/sys/i386/include/pmap.h,v 1.65.2.2 2000/11/30 01:54:42 peter
42178172Simp *	JNPR: pmap.h,v 1.7.2.1 2007/09/10 07:44:12 girish
43178172Simp *      $FreeBSD$
44178172Simp */
45178172Simp
46178172Simp#ifndef _MACHINE_PMAP_H_
47178172Simp#define	_MACHINE_PMAP_H_
48178172Simp
49178172Simp#include <machine/vmparam.h>
50187301Sgonzo#include <machine/pte.h>
51178172Simp
52219122Sjchandra#if defined(__mips_n32) || defined(__mips_n64) /* PHYSADDR_64BIT */
53219122Sjchandra#define	NKPT		256	/* mem > 4G, vm_page_startup needs more KPTs */
54219122Sjchandra#else
55178172Simp#define	NKPT		120	/* actual number of kernel page tables */
56219122Sjchandra#endif
57178172Simp
58178172Simp#ifndef LOCORE
59178172Simp
60178172Simp#include <sys/queue.h>
61222813Sattilio#include <sys/_cpuset.h>
62178172Simp#include <sys/_lock.h>
63178172Simp#include <sys/_mutex.h>
64178172Simp
65178172Simp/*
66178172Simp * Pmap stuff
67178172Simp */
68178172Simpstruct pv_entry;
69239236Salcstruct pv_chunk;
70178172Simp
71178172Simpstruct md_page {
72178172Simp	int pv_flags;
73191735Salc	TAILQ_HEAD(, pv_entry) pv_list;
74178172Simp};
75178172Simp
76178172Simp#define	PV_TABLE_REF		0x02	/* referenced */
77178172Simp
78178172Simp#define	ASID_BITS		8
79178172Simp#define	ASIDGEN_BITS		(32 - ASID_BITS)
80178172Simp#define	ASIDGEN_MASK		((1 << ASIDGEN_BITS) - 1)
81178172Simp
82178172Simpstruct pmap {
83178172Simp	pd_entry_t *pm_segtab;	/* KVA of segment table */
84239236Salc	TAILQ_HEAD(, pv_chunk)	pm_pvchunk;	/* list of mappings in pmap */
85222813Sattilio	cpuset_t	pm_active;		/* active on cpus */
86178172Simp	struct {
87178172Simp		u_int32_t asid:ASID_BITS;	/* TLB address space tag */
88178172Simp		u_int32_t gen:ASIDGEN_BITS;	/* its generation number */
89178172Simp	}      pm_asid[MAXSMPCPU];
90178172Simp	struct pmap_statistics pm_stats;	/* pmap statistics */
91178172Simp	struct mtx pm_mtx;
92178172Simp};
93178172Simp
94178172Simptypedef struct pmap *pmap_t;
95178172Simp
96187301Sgonzo#ifdef	_KERNEL
97178172Simp
98178172Simppt_entry_t *pmap_pte(pmap_t, vm_offset_t);
99233381Sgonzovm_paddr_t pmap_kextract(vm_offset_t va);
100178172Simp
101178172Simp#define	vtophys(va)	pmap_kextract(((vm_offset_t) (va)))
102209243Sjchandra#define	pmap_asid(pmap)	(pmap)->pm_asid[PCPU_GET(cpuid)].asid
103178172Simp
104191735Salcextern struct pmap	kernel_pmap_store;
105191735Salc#define kernel_pmap	(&kernel_pmap_store)
106191735Salc
107178172Simp#define	PMAP_LOCK(pmap)		mtx_lock(&(pmap)->pm_mtx)
108178172Simp#define	PMAP_LOCK_ASSERT(pmap, type)	mtx_assert(&(pmap)->pm_mtx, (type))
109178172Simp#define	PMAP_LOCK_DESTROY(pmap) mtx_destroy(&(pmap)->pm_mtx)
110178172Simp#define	PMAP_LOCK_INIT(pmap)	mtx_init(&(pmap)->pm_mtx, "pmap", \
111178172Simp				    NULL, MTX_DEF)
112178172Simp#define	PMAP_LOCKED(pmap)	mtx_owned(&(pmap)->pm_mtx)
113178172Simp#define	PMAP_MTX(pmap)		(&(pmap)->pm_mtx)
114178172Simp#define	PMAP_TRYLOCK(pmap)	mtx_trylock(&(pmap)->pm_mtx)
115178172Simp#define	PMAP_UNLOCK(pmap)	mtx_unlock(&(pmap)->pm_mtx)
116178172Simp
117178172Simp/*
118178172Simp * For each vm_page_t, there is a list of all currently valid virtual
119178172Simp * mappings of that page.  An entry is a pv_entry_t, the list is pv_table.
120178172Simp */
121178172Simptypedef struct pv_entry {
122178172Simp	vm_offset_t pv_va;	/* virtual address for mapping */
123191735Salc	TAILQ_ENTRY(pv_entry) pv_list;
124178172Simp}       *pv_entry_t;
125178172Simp
126202031Simp/*
127239236Salc * pv_entries are allocated in chunks per-process.  This avoids the
128239236Salc * need to track per-pmap assignments.
129239236Salc */
130239236Salc#ifdef __mips_n64
131239236Salc#define	_NPCM	3
132239236Salc#define	_NPCPV	168
133239236Salc#else
134239236Salc#define	_NPCM	11
135239236Salc#define	_NPCPV	336
136239236Salc#endif
137239236Salcstruct pv_chunk {
138239236Salc	pmap_t			pc_pmap;
139239236Salc	TAILQ_ENTRY(pv_chunk)	pc_list;
140239236Salc	u_long			pc_map[_NPCM];	/* bitmap; 1 = free */
141239236Salc	TAILQ_ENTRY(pv_chunk)	pc_lru;
142239236Salc	struct pv_entry		pc_pventry[_NPCPV];
143239236Salc};
144239236Salc
145239236Salc/*
146202031Simp * physmem_desc[] is a superset of phys_avail[] and describes all the
147202031Simp * memory present in the system.
148202031Simp *
149202031Simp * phys_avail[] is similar but does not include the memory stolen by
150202031Simp * pmap_steal_memory().
151202031Simp *
152202031Simp * Each memory region is described by a pair of elements in the array
153202031Simp * so we can describe up to (PHYS_AVAIL_ENTRIES / 2) distinct memory
154202031Simp * regions.
155202031Simp */
156202031Simp#define	PHYS_AVAIL_ENTRIES	10
157217345Sjchandraextern vm_paddr_t phys_avail[PHYS_AVAIL_ENTRIES + 2];
158217345Sjchandraextern vm_paddr_t physmem_desc[PHYS_AVAIL_ENTRIES + 2];
159202031Simp
160178172Simpextern vm_offset_t virtual_avail;
161178172Simpextern vm_offset_t virtual_end;
162178172Simp
163214903Sgonzoextern vm_paddr_t dump_avail[PHYS_AVAIL_ENTRIES + 2];
164214903Sgonzo
165195649Salc#define	pmap_page_get_memattr(m)	VM_MEMATTR_DEFAULT
166178172Simp#define	pmap_page_is_mapped(m)	(!TAILQ_EMPTY(&(m)->md.pv_list))
167237168Salc#define	pmap_page_is_write_mapped(m)	(((m)->aflags & PGA_WRITEABLE) != 0)
168195649Salc#define	pmap_page_set_memattr(m, ma)	(void)0
169178172Simp
170178172Simpvoid pmap_bootstrap(void);
171217345Sjchandravoid *pmap_mapdev(vm_paddr_t, vm_size_t);
172178172Simpvoid pmap_unmapdev(vm_offset_t, vm_size_t);
173178172Simpvm_offset_t pmap_steal_memory(vm_size_t size);
174187327Simpvoid pmap_kenter(vm_offset_t va, vm_paddr_t pa);
175212989Sneelvoid pmap_kenter_attr(vm_offset_t va, vm_paddr_t pa, int attr);
176187327Simpvoid pmap_kremove(vm_offset_t va);
177178172Simpvoid *pmap_kenter_temporary(vm_paddr_t pa, int i);
178178172Simpvoid pmap_kenter_temporary_free(vm_paddr_t pa);
179202031Simpvoid pmap_flush_pvcache(vm_page_t m);
180211217Sjchandraint pmap_emulate_modified(pmap_t pmap, vm_offset_t va);
181216315Sjchandravoid pmap_grow_direct_page_cache(void);
182216315Sjchandra
183178172Simp#endif				/* _KERNEL */
184178172Simp
185178172Simp#endif				/* !LOCORE */
186178172Simp
187178172Simp#endif				/* !_MACHINE_PMAP_H_ */
188