Home | History | Annotate | Line # | Download | only in include
pmap.h revision 1.54.26.22
      1 /*	$NetBSD: pmap.h,v 1.54.26.22 2011/12/27 16:09:36 matt Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 1992, 1993
      5  *	The Regents of the University of California.  All rights reserved.
      6  *
      7  * This code is derived from software contributed to Berkeley by
      8  * Ralph Campbell.
      9  *
     10  * Redistribution and use in source and binary forms, with or without
     11  * modification, are permitted provided that the following conditions
     12  * are met:
     13  * 1. Redistributions of source code must retain the above copyright
     14  *    notice, this list of conditions and the following disclaimer.
     15  * 2. Redistributions in binary form must reproduce the above copyright
     16  *    notice, this list of conditions and the following disclaimer in the
     17  *    documentation and/or other materials provided with the distribution.
     18  * 3. Neither the name of the University nor the names of its contributors
     19  *    may be used to endorse or promote products derived from this software
     20  *    without specific prior written permission.
     21  *
     22  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     23  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     24  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     25  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     26  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     27  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     28  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     29  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     30  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     31  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     32  * SUCH DAMAGE.
     33  *
     34  *	@(#)pmap.h	8.1 (Berkeley) 6/10/93
     35  */
     36 
     37 /*
     38  * Copyright (c) 1987 Carnegie-Mellon University
     39  *
     40  * This code is derived from software contributed to Berkeley by
     41  * Ralph Campbell.
     42  *
     43  * Redistribution and use in source and binary forms, with or without
     44  * modification, are permitted provided that the following conditions
     45  * are met:
     46  * 1. Redistributions of source code must retain the above copyright
     47  *    notice, this list of conditions and the following disclaimer.
     48  * 2. Redistributions in binary form must reproduce the above copyright
     49  *    notice, this list of conditions and the following disclaimer in the
     50  *    documentation and/or other materials provided with the distribution.
     51  * 3. All advertising materials mentioning features or use of this software
     52  *    must display the following acknowledgement:
     53  *	This product includes software developed by the University of
     54  *	California, Berkeley and its contributors.
     55  * 4. Neither the name of the University nor the names of its contributors
     56  *    may be used to endorse or promote products derived from this software
     57  *    without specific prior written permission.
     58  *
     59  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     60  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     61  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     62  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     63  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     64  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     65  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     66  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     67  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     68  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     69  * SUCH DAMAGE.
     70  *
     71  *	@(#)pmap.h	8.1 (Berkeley) 6/10/93
     72  */
     73 
     74 #ifndef	_MIPS_PMAP_H_
     75 #define	_MIPS_PMAP_H_
     76 
     77 #ifdef _KERNEL_OPT
     78 #include "opt_multiprocessor.h"
     79 #endif
     80 
     81 #include <mips/cpuregs.h>	/* for KSEG0 below */
     82 //#include <mips/pte.h>
     83 
     84 #if !defined(_MODULE) && !defined(_LKM)
     85 /*
     86  * The user address space is 2Gb (0x0 - 0x80000000).
     87  * User programs are laid out in memory as follows:
     88  *			address
     89  *	USRTEXT		0x00001000
     90  *	USRDATA		USRTEXT + text_size
     91  *	USRSTACK	0x7FFFFFFF
     92  *
     93  * The user address space is mapped using a two level structure where
     94  * virtual address bits 30..22 are used to index into a segment table which
     95  * points to a page worth of PTEs (4096 page can hold 1024 PTEs).
     96  * Bits 21..12 are then used to index a PTE which describes a page within
     97  * a segment.
     98  *
     99  * The wired entries in the TLB will contain the following:
    100  *	0-1	(UPAGES)	for curproc user struct and kernel stack.
    101  *
    102  * Note: The kernel doesn't use the same data structures as user programs.
    103  * All the PTE entries are stored in a single array in Sysmap which is
    104  * dynamically allocated at boot time.
    105  */
    106 
    107 #define mips_trunc_seg(x)	((vaddr_t)(x) & ~SEGOFSET)
    108 #define mips_round_seg(x)	(((vaddr_t)(x) + SEGOFSET) & ~SEGOFSET)
    109 
    110 #ifdef _LP64
    111 #define PMAP_SEGTABSIZE		NSEGPG
    112 #else
    113 #define PMAP_SEGTABSIZE		(1 << (31 - SEGSHIFT))
    114 #endif
    115 
    116 union pt_entry;
    117 
    118 union segtab {
    119 #ifdef _LP64
    120 	union segtab	*seg_seg[PMAP_SEGTABSIZE];
    121 #endif
    122 	union pt_entry	*seg_tab[PMAP_SEGTABSIZE];
    123 };
    124 #else
    125 /*
    126  * Modules don't need to know this.
    127  */
    128 union segtab;
    129 #endif
    130 
    131 /*
    132  * Structure defining an tlb entry data set.
    133  */
    134 struct tlb {
    135 	vaddr_t	tlb_hi;		/* should be 64 bits */
    136 	uint32_t tlb_lo0;	/* XXX maybe 64 bits (only 32 really used) */
    137 	uint32_t tlb_lo1;	/* XXX maybe 64 bits (only 32 really used) */
    138 };
    139 
    140 struct tlbmask {
    141 	vaddr_t	tlb_hi;		/* should be 64 bits */
    142 	uint32_t tlb_lo0;	/* XXX maybe 64 bits (only 32 really used) */
    143 	uint32_t tlb_lo1;	/* XXX maybe 64 bits (only 32 really used) */
    144 	uint32_t tlb_mask;
    145 };
    146 
    147 #ifdef _KERNEL
    148 struct pmap;
    149 typedef bool (*pte_callback_t)(struct pmap *, vaddr_t, vaddr_t,
    150 	union pt_entry *, uintptr_t);
    151 union pt_entry *pmap_pte_lookup(struct pmap *, vaddr_t);
    152 union pt_entry *pmap_pte_reserve(struct pmap *, vaddr_t, int);
    153 void pmap_pte_process(struct pmap *, vaddr_t, vaddr_t, pte_callback_t,
    154 	uintptr_t);
    155 void pmap_segtab_activate(struct pmap *, struct lwp *);
    156 void pmap_segtab_init(struct pmap *);
    157 void pmap_segtab_destroy(struct pmap *);
    158 #endif /* _KERNEL */
    159 
    160 /*
    161  * Per TLB (normally same as CPU) asid info
    162  */
    163 struct pmap_asid_info {
    164 	LIST_ENTRY(pmap_asid_info) pai_link;
    165 	uint32_t	pai_asid;	/* TLB address space tag */
    166 };
    167 
    168 #define	TLBINFO_LOCK(ti)		mutex_spin_enter((ti)->ti_lock)
    169 #define	TLBINFO_UNLOCK(ti)		mutex_spin_exit((ti)->ti_lock)
    170 #define	PMAP_PAI_ASIDVALID_P(pai, ti)	((pai)->pai_asid != 0)
    171 #define	PMAP_PAI(pmap, ti)		(&(pmap)->pm_pai[tlbinfo_index(ti)])
    172 #define	PAI_PMAP(pai, ti)	\
    173 	((pmap_t)((intptr_t)(pai) \
    174 	    - offsetof(struct pmap, pm_pai[tlbinfo_index(ti)])))
    175 
    176 /*
    177  * Machine dependent pmap structure.
    178  */
    179 typedef struct pmap {
    180 #ifdef MULTIPROCESSOR
    181 	volatile uint32_t	pm_active;	/* pmap was active on ... */
    182 	volatile uint32_t	pm_onproc;	/* pmap is active on ... */
    183 	volatile u_int		pm_shootdown_pending;
    184 #endif
    185 	union segtab		*pm_segtab;	/* pointers to pages of PTEs */
    186 	u_int			pm_count;	/* pmap reference count */
    187 	u_int			pm_flags;
    188 #define	PMAP_DEFERRED_ACTIVATE	0x0001
    189 	struct pmap_statistics	pm_stats;	/* pmap statistics */
    190 	struct pmap_asid_info	pm_pai[1];
    191 } *pmap_t;
    192 
    193 enum tlb_invalidate_op {
    194 	TLBINV_NOBODY=0,
    195 	TLBINV_ONE=1,
    196 	TLBINV_ALLUSER=2,
    197 	TLBINV_ALLKERNEL=3,
    198 	TLBINV_ALL=4
    199 };
    200 
    201 struct pmap_tlb_info {
    202 	char ti_name[8];
    203 	uint32_t ti_asid_hint;		/* probable next ASID to use */
    204 	uint32_t ti_asids_free;		/* # of ASIDs free */
    205 #define	tlbinfo_noasids_p(ti)	((ti)->ti_asids_free == 0)
    206 	kmutex_t *ti_lock;
    207 	u_int ti_wired;			/* # of wired TLB entries */
    208 	uint32_t ti_asid_mask;
    209 	uint32_t ti_asid_max;
    210 	LIST_HEAD(, pmap_asid_info) ti_pais; /* list of active ASIDs */
    211 	uint32_t ti_syncicache_bitmap;	/* page indices needing a syncicache */
    212 	struct evcnt ti_evcnt_syncicache_asts;
    213 	struct evcnt ti_evcnt_syncicache_all;
    214 	struct evcnt ti_evcnt_syncicache_pages;
    215 	struct evcnt ti_evcnt_syncicache_desired;
    216 	struct evcnt ti_evcnt_syncicache_duplicate;
    217 #ifdef MULTIPROCESSOR
    218 	kmutex_t *ti_hwlock;
    219 	pmap_t ti_victim;
    220 	uint32_t ti_cpu_mask;		/* bitmask of CPUs sharing this TLB */
    221 	enum tlb_invalidate_op ti_tlbinvop;
    222 	u_int ti_index;
    223 #define tlbinfo_index(ti)	((ti)->ti_index)
    224 	struct evcnt ti_evcnt_syncipage_deferred;
    225 #else
    226 #define tlbinfo_index(ti)	(0)
    227 #endif
    228 	struct evcnt ti_evcnt_asid_reinits;
    229 	struct evcnt ti_evcnt_asid_reclaims;
    230 	u_long ti_asid_bitmap[1024 / (sizeof(u_long) * 8)];
    231 };
    232 
    233 #ifdef	_KERNEL
    234 
    235 struct pmap_kernel {
    236 	struct pmap kernel_pmap;
    237 #ifdef MULTIPROCESSOR
    238 	struct pmap_asid_info kernel_pai[MAXCPUS-1];
    239 #endif
    240 };
    241 
    242 extern struct pmap_kernel kernel_pmap_store;
    243 extern struct pmap_tlb_info pmap_tlb0_info;
    244 #ifdef MULTIPROCESSOR
    245 extern struct pmap_tlb_info *pmap_tlbs[MAXCPUS];
    246 extern u_int pmap_ntlbs;
    247 #endif
    248 extern u_int pmap_syncipage_page_mask;
    249 extern u_int pmap_syncipage_map_mask;
    250 extern paddr_t mips_avail_start;
    251 extern paddr_t mips_avail_end;
    252 extern vaddr_t mips_virtual_end;
    253 
    254 #define pmap_kernel()		(&kernel_pmap_store.kernel_pmap)
    255 #define	pmap_wired_count(pmap) 	((pmap)->pm_stats.wired_count)
    256 #define pmap_resident_count(pmap) ((pmap)->pm_stats.resident_count)
    257 
    258 #define pmap_phys_address(x)	mips_ptob(x)
    259 
    260 /*
    261  *	Bootstrap the system enough to run with virtual memory.
    262  */
    263 void	pmap_bootstrap(void);
    264 
    265 void	pmap_remove_all(pmap_t);
    266 void	pmap_set_modified(paddr_t);
    267 void	pmap_procwr(struct proc *, vaddr_t, size_t);
    268 #define	PMAP_NEED_PROCWR
    269 
    270 #ifdef MULTIPROCESSOR
    271 void	pmap_tlb_shootdown_process(void);
    272 bool	pmap_tlb_shootdown_bystanders(pmap_t pmap);
    273 void	pmap_tlb_info_attach(struct pmap_tlb_info *, struct cpu_info *);
    274 void	pmap_syncicache_wanted(struct cpu_info *);
    275 void	pmap_syncicache(uint32_t, uint32_t);
    276 #endif
    277 void	pmap_syncicache_page(struct vm_page *, uint32_t);
    278 void	pmap_syncicache_init(void);
    279 void	pmap_syncicache_ast(struct cpu_info *);
    280 void	pmap_tlb_info_init(struct pmap_tlb_info *);
    281 void	pmap_tlb_info_evcnt_attach(struct pmap_tlb_info *);
    282 void	pmap_tlb_asid_acquire(pmap_t pmap, struct lwp *l);
    283 void	pmap_tlb_asid_deactivate(pmap_t pmap);
    284 void	pmap_tlb_asid_check(void);
    285 void	pmap_tlb_asid_release_all(pmap_t pmap);
    286 int	pmap_tlb_update_addr(pmap_t pmap, vaddr_t, uint32_t, bool);
    287 void	pmap_tlb_invalidate_addr(pmap_t pmap, vaddr_t);
    288 
    289 /*
    290  * pmap_prefer() helps reduce virtual-coherency exceptions in
    291  * the virtually-indexed cache on mips3 CPUs.
    292  */
    293 #ifdef MIPS3_PLUS
    294 #define PMAP_PREFER(pa, va, sz, td)	pmap_prefer((pa), (va), (sz), (td))
    295 void	pmap_prefer(vaddr_t, vaddr_t *, vsize_t, int);
    296 #endif /* MIPS3_PLUS */
    297 
    298 #define	PMAP_STEAL_MEMORY	/* enable pmap_steal_memory() */
    299 
    300 /*
    301  * Alternate mapping hooks for pool pages.  Avoids thrashing the TLB.
    302  */
    303 vaddr_t mips_pmap_map_poolpage(paddr_t);
    304 paddr_t mips_pmap_unmap_poolpage(vaddr_t);
    305 struct vm_page *mips_pmap_alloc_poolpage(int);
    306 #define	PMAP_ALLOC_POOLPAGE(flags)	mips_pmap_alloc_poolpage(flags)
    307 #define	PMAP_MAP_POOLPAGE(pa)		mips_pmap_map_poolpage(pa)
    308 #define	PMAP_UNMAP_POOLPAGE(va)		mips_pmap_unmap_poolpage(va)
    309 
    310 /*
    311  * Other hooks for the pool allocator.
    312  */
    313 #ifdef _LP64
    314 #define	POOL_VTOPHYS(va)	(MIPS_KSEG0_P(va) \
    315 				    ? MIPS_KSEG0_TO_PHYS(va) \
    316 				    : MIPS_XKPHYS_TO_PHYS(va))
    317 #else
    318 #define	POOL_VTOPHYS(va)	MIPS_KSEG0_TO_PHYS((vaddr_t)(va))
    319 #endif
    320 
    321 /*
    322  * Select CCA to use for unmanaged pages.
    323  */
    324 #define	PMAP_CCA_FOR_PA(pa)	CCA_UNCACHED		/* uncached */
    325 
    326 #if defined(_MIPS_PADDR_T_64BIT) || defined(_LP64)
    327 #define PMAP_NOCACHE	0x4000000000000000ULL
    328 #endif
    329 
    330 uint16_t pmap_pvlist_lock(struct vm_page_md *, bool);
    331 
    332 #endif	/* _KERNEL */
    333 #endif	/* _MIPS_PMAP_H_ */
    334