xref: /freebsd/sys/powerpc/include/pmap.h (revision 4026b447908ec41deae2e5c9cdd62eef3072daa4)
1f9bac91bSBenno Rice /*-
2ffb56695SRafal Jaworowski  * Copyright (C) 2006 Semihalf, Marian Balakowicz <m8@semihalf.com>
3ffb56695SRafal Jaworowski  * All rights reserved.
4ffb56695SRafal Jaworowski  *
5ffb56695SRafal Jaworowski  * Adapted for Freescale's e500 core CPUs.
6ffb56695SRafal Jaworowski  *
7ffb56695SRafal Jaworowski  * Redistribution and use in source and binary forms, with or without
8ffb56695SRafal Jaworowski  * modification, are permitted provided that the following conditions
9ffb56695SRafal Jaworowski  * are met:
10ffb56695SRafal Jaworowski  * 1. Redistributions of source code must retain the above copyright
11ffb56695SRafal Jaworowski  *    notice, this list of conditions and the following disclaimer.
12ffb56695SRafal Jaworowski  * 2. Redistributions in binary form must reproduce the above copyright
13ffb56695SRafal Jaworowski  *    notice, this list of conditions and the following disclaimer in the
14ffb56695SRafal Jaworowski  *    documentation and/or other materials provided with the distribution.
15ffb56695SRafal Jaworowski  * 3. The name of the author may not be used to endorse or promote products
16ffb56695SRafal Jaworowski  *    derived from this software without specific prior written permission.
17ffb56695SRafal Jaworowski  *
18ffb56695SRafal Jaworowski  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19ffb56695SRafal Jaworowski  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20ffb56695SRafal Jaworowski  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN
21ffb56695SRafal Jaworowski  * NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
22ffb56695SRafal Jaworowski  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
23ffb56695SRafal Jaworowski  * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
24ffb56695SRafal Jaworowski  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
25ffb56695SRafal Jaworowski  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
26ffb56695SRafal Jaworowski  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
27ffb56695SRafal Jaworowski  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28ffb56695SRafal Jaworowski  *
29ffb56695SRafal Jaworowski  * $FreeBSD$
30ffb56695SRafal Jaworowski  */
31ffb56695SRafal Jaworowski /*-
32f9bac91bSBenno Rice  * Copyright (C) 1995, 1996 Wolfgang Solfrank.
33f9bac91bSBenno Rice  * Copyright (C) 1995, 1996 TooLs GmbH.
34f9bac91bSBenno Rice  * All rights reserved.
35f9bac91bSBenno Rice  *
36f9bac91bSBenno Rice  * Redistribution and use in source and binary forms, with or without
37f9bac91bSBenno Rice  * modification, are permitted provided that the following conditions
38f9bac91bSBenno Rice  * are met:
39f9bac91bSBenno Rice  * 1. Redistributions of source code must retain the above copyright
40f9bac91bSBenno Rice  *    notice, this list of conditions and the following disclaimer.
41f9bac91bSBenno Rice  * 2. Redistributions in binary form must reproduce the above copyright
42f9bac91bSBenno Rice  *    notice, this list of conditions and the following disclaimer in the
43f9bac91bSBenno Rice  *    documentation and/or other materials provided with the distribution.
44f9bac91bSBenno Rice  * 3. All advertising materials mentioning features or use of this software
45f9bac91bSBenno Rice  *    must display the following acknowledgement:
46f9bac91bSBenno Rice  *	This product includes software developed by TooLs GmbH.
47f9bac91bSBenno Rice  * 4. The name of TooLs GmbH may not be used to endorse or promote products
48f9bac91bSBenno Rice  *    derived from this software without specific prior written permission.
49f9bac91bSBenno Rice  *
50f9bac91bSBenno Rice  * THIS SOFTWARE IS PROVIDED BY TOOLS GMBH ``AS IS'' AND ANY EXPRESS OR
51f9bac91bSBenno Rice  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
52f9bac91bSBenno Rice  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
53f9bac91bSBenno Rice  * IN NO EVENT SHALL TOOLS GMBH BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
54f9bac91bSBenno Rice  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
55f9bac91bSBenno Rice  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
56f9bac91bSBenno Rice  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
57f9bac91bSBenno Rice  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
58f9bac91bSBenno Rice  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
59f9bac91bSBenno Rice  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
60f9bac91bSBenno Rice  *
61ffb56695SRafal Jaworowski  *	from: $NetBSD: pmap.h,v 1.17 2000/03/30 16:18:24 jdolecek Exp $
62f9bac91bSBenno Rice  */
63f9bac91bSBenno Rice 
64f9bac91bSBenno Rice #ifndef	_MACHINE_PMAP_H_
65f9bac91bSBenno Rice #define	_MACHINE_PMAP_H_
66f9bac91bSBenno Rice 
6748d0b1a0SAlan Cox #include <sys/queue.h>
68c3e289e1SNathan Whitehorn #include <sys/tree.h>
69c47dd3dbSAttilio Rao #include <sys/_cpuset.h>
7048d0b1a0SAlan Cox #include <sys/_lock.h>
7148d0b1a0SAlan Cox #include <sys/_mutex.h>
725244eac9SBenno Rice #include <machine/sr.h>
737f89270bSPeter Grehan #include <machine/pte.h>
74c3e289e1SNathan Whitehorn #include <machine/slb.h>
75ffb56695SRafal Jaworowski #include <machine/tlb.h>
76ffb56695SRafal Jaworowski 
77*4026b447SJustin Hibbits struct pmap;
78*4026b447SJustin Hibbits typedef struct pmap *pmap_t;
79*4026b447SJustin Hibbits 
80ffb56695SRafal Jaworowski #if defined(AIM)
81f9bac91bSBenno Rice 
827c277971SPeter Grehan #if !defined(NPMAPS)
837c277971SPeter Grehan #define	NPMAPS		32768
847c277971SPeter Grehan #endif /* !defined(NPMAPS) */
857c277971SPeter Grehan 
8695fa3335SNathan Whitehorn struct	slbtnode;
87f9bac91bSBenno Rice 
885244eac9SBenno Rice struct pvo_entry {
895244eac9SBenno Rice 	LIST_ENTRY(pvo_entry) pvo_vlink;	/* Link to common virt page */
90827cc9b9SNathan Whitehorn #ifndef __powerpc64__
915244eac9SBenno Rice 	LIST_ENTRY(pvo_entry) pvo_olink;	/* Link to overflow entry */
92827cc9b9SNathan Whitehorn #endif
93ccc4a5c7SNathan Whitehorn 	RB_ENTRY(pvo_entry) pvo_plink;	/* Link to pmap entries */
94827cc9b9SNathan Whitehorn 	struct {
95827cc9b9SNathan Whitehorn #ifndef __powerpc64__
96827cc9b9SNathan Whitehorn 		/* 32-bit fields */
97827cc9b9SNathan Whitehorn 		struct	pte pte;
98827cc9b9SNathan Whitehorn #endif
99827cc9b9SNathan Whitehorn 		/* 64-bit fields */
100827cc9b9SNathan Whitehorn 		uintptr_t   slot;
101827cc9b9SNathan Whitehorn 		vm_paddr_t  pa;
102827cc9b9SNathan Whitehorn 		vm_prot_t   prot;
10352a7870dSNathan Whitehorn 	} pvo_pte;
1045244eac9SBenno Rice 	pmap_t		pvo_pmap;		/* Owning pmap */
1055244eac9SBenno Rice 	vm_offset_t	pvo_vaddr;		/* VA of entry */
106c3e289e1SNathan Whitehorn 	uint64_t	pvo_vpn;		/* Virtual page number */
1075244eac9SBenno Rice };
1085244eac9SBenno Rice LIST_HEAD(pvo_head, pvo_entry);
109ccc4a5c7SNathan Whitehorn RB_HEAD(pvo_tree, pvo_entry);
110ccc4a5c7SNathan Whitehorn int pvo_vaddr_compare(struct pvo_entry *, struct pvo_entry *);
111ccc4a5c7SNathan Whitehorn RB_PROTOTYPE(pvo_tree, pvo_entry, pvo_plink, pvo_vaddr_compare);
1125244eac9SBenno Rice 
113827cc9b9SNathan Whitehorn /* Used by 32-bit PMAP */
114bef5da7fSNathan Whitehorn #define	PVO_PTEGIDX_MASK	0x007UL		/* which PTEG slot */
115bef5da7fSNathan Whitehorn #define	PVO_PTEGIDX_VALID	0x008UL		/* slot is valid */
116827cc9b9SNathan Whitehorn /* Used by 64-bit PMAP */
117827cc9b9SNathan Whitehorn #define	PVO_HID			0x008UL		/* PVO entry in alternate hash*/
118827cc9b9SNathan Whitehorn /* Used by both */
119bef5da7fSNathan Whitehorn #define	PVO_WIRED		0x010UL		/* PVO entry is wired */
120bef5da7fSNathan Whitehorn #define	PVO_MANAGED		0x020UL		/* PVO entry is managed */
121bef5da7fSNathan Whitehorn #define	PVO_BOOTSTRAP		0x080UL		/* PVO entry allocated during
122bef5da7fSNathan Whitehorn 						   bootstrap */
123827cc9b9SNathan Whitehorn #define PVO_DEAD		0x100UL		/* waiting to be deleted */
124bef5da7fSNathan Whitehorn #define PVO_LARGE		0x200UL		/* large page */
125bef5da7fSNathan Whitehorn #define	PVO_VADDR(pvo)		((pvo)->pvo_vaddr & ~ADDR_POFF)
126bef5da7fSNathan Whitehorn #define	PVO_PTEGIDX_GET(pvo)	((pvo)->pvo_vaddr & PVO_PTEGIDX_MASK)
127bef5da7fSNathan Whitehorn #define	PVO_PTEGIDX_ISSET(pvo)	((pvo)->pvo_vaddr & PVO_PTEGIDX_VALID)
128bef5da7fSNathan Whitehorn #define	PVO_PTEGIDX_CLR(pvo)	\
129bef5da7fSNathan Whitehorn 	((void)((pvo)->pvo_vaddr &= ~(PVO_PTEGIDX_VALID|PVO_PTEGIDX_MASK)))
130bef5da7fSNathan Whitehorn #define	PVO_PTEGIDX_SET(pvo, i)	\
131bef5da7fSNathan Whitehorn 	((void)((pvo)->pvo_vaddr |= (i)|PVO_PTEGIDX_VALID))
132bef5da7fSNathan Whitehorn #define	PVO_VSID(pvo)		((pvo)->pvo_vpn >> 16)
133bef5da7fSNathan Whitehorn 
134598d99ddSNathan Whitehorn struct	pmap {
135*4026b447SJustin Hibbits 	struct		pmap_statistics	pm_stats;
136598d99ddSNathan Whitehorn 	struct	mtx	pm_mtx;
137598d99ddSNathan Whitehorn 
138598d99ddSNathan Whitehorn     #ifdef __powerpc64__
139598d99ddSNathan Whitehorn 	struct slbtnode	*pm_slb_tree_root;
140598d99ddSNathan Whitehorn 	struct slb	**pm_slb;
141598d99ddSNathan Whitehorn 	int		pm_slb_len;
142598d99ddSNathan Whitehorn     #else
143598d99ddSNathan Whitehorn 	register_t	pm_sr[16];
144598d99ddSNathan Whitehorn     #endif
145598d99ddSNathan Whitehorn 	cpuset_t	pm_active;
146598d99ddSNathan Whitehorn 
147598d99ddSNathan Whitehorn 	struct pmap	*pmap_phys;
148ccc4a5c7SNathan Whitehorn 	struct pvo_tree pmap_pvo;
149598d99ddSNathan Whitehorn };
150598d99ddSNathan Whitehorn 
1515244eac9SBenno Rice struct	md_page {
152827cc9b9SNathan Whitehorn 	volatile int32_t mdpg_attrs;
153c1f4123bSNathan Whitehorn 	vm_memattr_t	 mdpg_cache_attrs;
1545244eac9SBenno Rice 	struct	pvo_head mdpg_pvoh;
1555244eac9SBenno Rice };
1565244eac9SBenno Rice 
157c1f4123bSNathan Whitehorn #define	pmap_page_get_memattr(m)	((m)->md.mdpg_cache_attrs)
158ffb56695SRafal Jaworowski #define	pmap_page_is_mapped(m)	(!LIST_EMPTY(&(m)->md.mdpg_pvoh))
159ffb56695SRafal Jaworowski 
160c3e289e1SNathan Whitehorn /*
161c3e289e1SNathan Whitehorn  * Return the VSID corresponding to a given virtual address.
162c3e289e1SNathan Whitehorn  * If no VSID is currently defined, it will allocate one, and add
163c3e289e1SNathan Whitehorn  * it to a free slot if available.
164c3e289e1SNathan Whitehorn  *
165c3e289e1SNathan Whitehorn  * NB: The PMAP MUST be locked already.
166c3e289e1SNathan Whitehorn  */
167c3e289e1SNathan Whitehorn uint64_t va_to_vsid(pmap_t pm, vm_offset_t va);
16895fa3335SNathan Whitehorn 
16995fa3335SNathan Whitehorn /* Lock-free, non-allocating lookup routines */
17095fa3335SNathan Whitehorn uint64_t kernel_va_to_slbv(vm_offset_t va);
17195fa3335SNathan Whitehorn struct slb *user_va_to_slb_entry(pmap_t pm, vm_offset_t va);
172c3e289e1SNathan Whitehorn 
1736416b9a8SNathan Whitehorn uint64_t allocate_user_vsid(pmap_t pm, uint64_t esid, int large);
17495fa3335SNathan Whitehorn void	free_vsid(pmap_t pm, uint64_t esid, int large);
1756416b9a8SNathan Whitehorn void	slb_insert_user(pmap_t pm, struct slb *slb);
1766416b9a8SNathan Whitehorn void	slb_insert_kernel(uint64_t slbe, uint64_t slbv);
17795fa3335SNathan Whitehorn 
17895fa3335SNathan Whitehorn struct slbtnode *slb_alloc_tree(void);
17995fa3335SNathan Whitehorn void     slb_free_tree(pmap_t pm);
1806416b9a8SNathan Whitehorn struct slb **slb_alloc_user_cache(void);
1816416b9a8SNathan Whitehorn void	slb_free_user_cache(struct slb **);
182c3e289e1SNathan Whitehorn 
183fcef00c3SJustin Hibbits #elif defined(BOOKE)
184ffb56695SRafal Jaworowski 
185ffb56695SRafal Jaworowski struct pmap {
186*4026b447SJustin Hibbits 	struct pmap_statistics	pm_stats;	/* pmap statistics */
187ffb56695SRafal Jaworowski 	struct mtx		pm_mtx;		/* pmap mutex */
188b2b734e7SRafal Jaworowski 	tlbtid_t		pm_tid[MAXCPU];	/* TID to identify this pmap entries in TLB */
189c47dd3dbSAttilio Rao 	cpuset_t		pm_active;	/* active on cpus */
190ffb56695SRafal Jaworowski 
191ffb56695SRafal Jaworowski 	/* Page table directory, array of pointers to page tables. */
192ffb56695SRafal Jaworowski 	pte_t			*pm_pdir[PDIR_NENTRIES];
193ffb56695SRafal Jaworowski 
194ffb56695SRafal Jaworowski 	/* List of allocated ptbl bufs (ptbl kva regions). */
195b2b734e7SRafal Jaworowski 	TAILQ_HEAD(, ptbl_buf)	pm_ptbl_list;
196ffb56695SRafal Jaworowski };
197ffb56695SRafal Jaworowski 
198ffb56695SRafal Jaworowski struct pv_entry {
199ffb56695SRafal Jaworowski 	pmap_t pv_pmap;
200ffb56695SRafal Jaworowski 	vm_offset_t pv_va;
201ffb56695SRafal Jaworowski 	TAILQ_ENTRY(pv_entry) pv_link;
202ffb56695SRafal Jaworowski };
203ffb56695SRafal Jaworowski typedef struct pv_entry *pv_entry_t;
204ffb56695SRafal Jaworowski 
205ffb56695SRafal Jaworowski struct md_page {
206ffb56695SRafal Jaworowski 	TAILQ_HEAD(, pv_entry) pv_list;
207fcef00c3SJustin Hibbits 	int	pv_tracked;
208ffb56695SRafal Jaworowski };
209ffb56695SRafal Jaworowski 
2103153e878SAlan Cox #define	pmap_page_get_memattr(m)	VM_MEMATTR_DEFAULT
211ffb56695SRafal Jaworowski #define	pmap_page_is_mapped(m)	(!TAILQ_EMPTY(&(m)->md.pv_list))
212ffb56695SRafal Jaworowski 
213*4026b447SJustin Hibbits #else
214*4026b447SJustin Hibbits /*
215*4026b447SJustin Hibbits  * Common pmap members between AIM and BOOKE.
216*4026b447SJustin Hibbits  * libkvm needs pm_stats at the same location between both, as it doesn't define
217*4026b447SJustin Hibbits  * AIM nor BOOKE, and is expected to work across all.
218*4026b447SJustin Hibbits  */
219*4026b447SJustin Hibbits struct pmap {
220*4026b447SJustin Hibbits 	struct pmap_statistics	pm_stats;	/* pmap statistics */
221*4026b447SJustin Hibbits 	struct mtx		pm_mtx;		/* pmap mutex */
222*4026b447SJustin Hibbits };
223ffb56695SRafal Jaworowski #endif /* AIM */
224ffb56695SRafal Jaworowski 
2255244eac9SBenno Rice extern	struct pmap kernel_pmap_store;
2265244eac9SBenno Rice #define	kernel_pmap	(&kernel_pmap_store)
2275244eac9SBenno Rice 
228f9bac91bSBenno Rice #ifdef _KERNEL
229f9bac91bSBenno Rice 
23048d0b1a0SAlan Cox #define	PMAP_LOCK(pmap)		mtx_lock(&(pmap)->pm_mtx)
23148d0b1a0SAlan Cox #define	PMAP_LOCK_ASSERT(pmap, type) \
23248d0b1a0SAlan Cox 				mtx_assert(&(pmap)->pm_mtx, (type))
23348d0b1a0SAlan Cox #define	PMAP_LOCK_DESTROY(pmap)	mtx_destroy(&(pmap)->pm_mtx)
234629e40e4SNathan Whitehorn #define	PMAP_LOCK_INIT(pmap)	mtx_init(&(pmap)->pm_mtx, \
235629e40e4SNathan Whitehorn 				    (pmap == kernel_pmap) ? "kernelpmap" : \
236629e40e4SNathan Whitehorn 				    "pmap", NULL, MTX_DEF)
23748d0b1a0SAlan Cox #define	PMAP_LOCKED(pmap)	mtx_owned(&(pmap)->pm_mtx)
23848d0b1a0SAlan Cox #define	PMAP_MTX(pmap)		(&(pmap)->pm_mtx)
23948d0b1a0SAlan Cox #define	PMAP_TRYLOCK(pmap)	mtx_trylock(&(pmap)->pm_mtx)
24048d0b1a0SAlan Cox #define	PMAP_UNLOCK(pmap)	mtx_unlock(&(pmap)->pm_mtx)
24148d0b1a0SAlan Cox 
2426031c68dSAlan Cox #define	pmap_page_is_write_mapped(m)	(((m)->aflags & PGA_WRITEABLE) != 0)
2436031c68dSAlan Cox 
2445244eac9SBenno Rice void		pmap_bootstrap(vm_offset_t, vm_offset_t);
24520b79612SRafal Jaworowski void		pmap_kenter(vm_offset_t va, vm_paddr_t pa);
246c1f4123bSNathan Whitehorn void		pmap_kenter_attr(vm_offset_t va, vm_offset_t pa, vm_memattr_t);
2475501d40bSJake Burkholder void		pmap_kremove(vm_offset_t);
24820b79612SRafal Jaworowski void		*pmap_mapdev(vm_paddr_t, vm_size_t);
2492109efd1SJustin Hibbits void		*pmap_mapdev_attr(vm_paddr_t, vm_size_t, vm_memattr_t);
2508bbfa33aSBenno Rice void		pmap_unmapdev(vm_offset_t, vm_size_t);
251c1f4123bSNathan Whitehorn void		pmap_page_set_memattr(vm_page_t, vm_memattr_t);
2520f7aeab0SJustin Hibbits int		pmap_change_attr(vm_offset_t, vm_size_t, vm_memattr_t);
253ac6ba8bdSBenno Rice void		pmap_deactivate(struct thread *);
25420b79612SRafal Jaworowski vm_paddr_t	pmap_kextract(vm_offset_t);
25520b79612SRafal Jaworowski int		pmap_dev_direct_mapped(vm_paddr_t, vm_size_t);
256f9c702dbSPeter Grehan boolean_t	pmap_mmu_install(char *name, int prio);
257f9c702dbSPeter Grehan 
258696effb6SJohn Baldwin #define	vtophys(va)	pmap_kextract((vm_offset_t)(va))
259a0889814SBenno Rice 
26005e1c598SAndreas Tobler #define PHYS_AVAIL_SZ	256	/* Allows up to 16GB Ram on pSeries with
26105e1c598SAndreas Tobler 				 * logical memory block size of 64MB.
26205e1c598SAndreas Tobler 				 * For more Ram increase the lmb or this value.
26305e1c598SAndreas Tobler 				 */
26405e1c598SAndreas Tobler 
265b3936ebeSJustin Hibbits extern	vm_paddr_t phys_avail[PHYS_AVAIL_SZ];
266f9bac91bSBenno Rice extern	vm_offset_t virtual_avail;
267f9bac91bSBenno Rice extern	vm_offset_t virtual_end;
268f9bac91bSBenno Rice 
2695244eac9SBenno Rice extern	vm_offset_t msgbuf_phys;
270f9bac91bSBenno Rice 
271f9c702dbSPeter Grehan extern	int pmap_bootstrapped;
272f9c702dbSPeter Grehan 
27333724f17SNathan Whitehorn vm_offset_t pmap_early_io_map(vm_paddr_t pa, vm_size_t size);
27460152a40SJustin Hibbits void pmap_early_io_unmap(vm_offset_t va, vm_size_t size);
275b2f831c0SJustin Hibbits void pmap_track_page(pmap_t pmap, vm_offset_t va);
27633724f17SNathan Whitehorn 
277f9bac91bSBenno Rice #endif
278f9bac91bSBenno Rice 
2795244eac9SBenno Rice #endif /* !_MACHINE_PMAP_H_ */
280