1/* $NetBSD: pmap.h,v 1.39.4.1 2026/07/03 17:51:59 martin Exp $ */
2
3/*-
4 * Copyright (C) 1995, 1996 Wolfgang Solfrank.
5 * Copyright (C) 1995, 1996 TooLs GmbH.
6 * All rights reserved.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. All advertising materials mentioning features or use of this software
17 * must display the following acknowledgement:
18 * This product includes software developed by TooLs GmbH.
19 * 4. The name of TooLs GmbH may not be used to endorse or promote products
20 * derived from this software without specific prior written permission.
21 *
22 * THIS SOFTWARE IS PROVIDED BY TOOLS GMBH ``AS IS'' AND ANY EXPRESS OR
23 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
24 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
25 * IN NO EVENT SHALL TOOLS GMBH BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
27 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
28 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
29 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
30 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
31 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 */
33
34#ifndef _POWERPC_OEA_PMAP_H_
35#define _POWERPC_OEA_PMAP_H_
36
37#ifdef _LOCORE
38#error use assym.h instead
39#endif
40
41#ifdef _MODULE
42#error this file should not be included by loadable kernel modules
43#endif
44
45#ifdef _KERNEL_OPT
46#include "opt_ppcarch.h"
47#include "opt_modular.h"
48#endif
49#include <powerpc/oea/pte.h>
50
51#define __HAVE_PMAP_PV_TRACK
52#include <uvm/pmap/pmap_pvt.h>
53
54/*
55 * Pmap stuff
56 */
57struct pmap {
58#ifdef PPC_OEA64
59 struct steg *pm_steg_table; /* segment table pointer */
60 /* XXX need way to track exec pages */
61#endif
62
63#if defined(PPC_OEA) || defined (PPC_OEA64_BRIDGE)
64 register_t pm_sr[16]; /* segments used in this pmap */
65 int pm_exec[16]; /* counts of exec mappings */
66#endif
67 register_t pm_vsid; /* VSID bits */
68 int pm_refs; /* ref count */
69 struct pmap_statistics pm_stats; /* pmap statistics */
70 unsigned int pm_evictions; /* pvo's not in page table */
71
72#ifdef PPC_OEA64
73 unsigned int pm_ste_evictions;
74#endif
75};
76
77struct pmap_ops {
78 int (*pmapop_pte_spill)(struct pmap *, vaddr_t, bool);
79 void (*pmapop_real_memory)(paddr_t *, psize_t *);
80 void (*pmapop_init)(void);
81 void (*pmapop_virtual_space)(vaddr_t *, vaddr_t *);
82 pmap_t (*pmapop_create)(void);
83 void (*pmapop_reference)(pmap_t);
84 void (*pmapop_destroy)(pmap_t);
85 void (*pmapop_copy)(pmap_t, pmap_t, vaddr_t, vsize_t, vaddr_t);
86 void (*pmapop_update)(pmap_t);
87 int (*pmapop_enter)(pmap_t, vaddr_t, paddr_t, vm_prot_t, u_int);
88 void (*pmapop_remove)(pmap_t, vaddr_t, vaddr_t);
89 void (*pmapop_kenter_pa)(vaddr_t, paddr_t, vm_prot_t, u_int);
90 void (*pmapop_kremove)(vaddr_t, vsize_t);
91 bool (*pmapop_extract)(pmap_t, vaddr_t, paddr_t *);
92
93 void (*pmapop_protect)(pmap_t, vaddr_t, vaddr_t, vm_prot_t);
94 void (*pmapop_unwire)(pmap_t, vaddr_t);
95 void (*pmapop_page_protect)(struct vm_page *, vm_prot_t);
96 void (*pmapop_pv_protect)(paddr_t, vm_prot_t);
97 bool (*pmapop_query_bit)(struct vm_page *, int);
98 bool (*pmapop_clear_bit)(struct vm_page *, int);
99
100 void (*pmapop_activate)(struct lwp *);
101 void (*pmapop_deactivate)(struct lwp *);
102
103 void (*pmapop_pinit)(pmap_t);
104 void (*pmapop_procwr)(struct proc *, vaddr_t, size_t);
105
106 void (*pmapop_pte_print)(volatile struct pte *);
107 void (*pmapop_pteg_check)(void);
108 void (*pmapop_print_mmuregs)(void);
109 void (*pmapop_print_pte)(pmap_t, vaddr_t);
110 void (*pmapop_pteg_dist)(void);
111 void (*pmapop_pvo_verify)(void);
112 vaddr_t (*pmapop_steal_memory)(vsize_t, vaddr_t *, vaddr_t *);
113 void (*pmapop_bootstrap)(paddr_t, paddr_t);
114 void (*pmapop_bootstrap1)(paddr_t, paddr_t);
115 void (*pmapop_bootstrap2)(void);
116};
117
118#ifdef _KERNEL
119#include <sys/cdefs.h>
120__BEGIN_DECLS
121#include <sys/param.h>
122#include <sys/systm.h>
123
124/*
125 * Physical memory below PMAP_DIRECT_MAPPED_LEN is direct-mapped
126 * (pa == va). Direct region covers the segments below BOTH
127 * the user copyin window (USER_SR) and the kernel HTAB window
128 * (KERNEL_SR), so it can never overlap.
129 */
130#if defined(PPC_OEA) || defined(PPC_OEA64_BRIDGE)
131#define PMAP_DIRECT_MAPPED_SR (MIN(USER_SR, KERNEL_SR) - 1)
132#define PMAP_DIRECT_MAPPED_LEN \
133 ((vaddr_t)SEGMENT_LENGTH * (PMAP_DIRECT_MAPPED_SR + 1))
134#endif
135
136#if defined (PPC_OEA) || defined (PPC_OEA64_BRIDGE)
137extern register_t iosrtable[];
138#endif
139extern int pmap_use_altivec;
140
141#define pmap_clear_modify(pg) (pmap_clear_bit((pg), PTE_CHG))
142#define pmap_clear_reference(pg) (pmap_clear_bit((pg), PTE_REF))
143#define pmap_is_modified(pg) (pmap_query_bit((pg), PTE_CHG))
144#define pmap_is_referenced(pg) (pmap_query_bit((pg), PTE_REF))
145
146#define pmap_resident_count(pmap) ((pmap)->pm_stats.resident_count)
147#define pmap_wired_count(pmap) ((pmap)->pm_stats.wired_count)
148
149/* ARGSUSED */
150static __inline bool
151pmap_remove_all(struct pmap *pmap)
152{
153 /* Nothing. */
154 return false;
155}
156
157#if (defined(PPC_OEA) + defined(PPC_OEA64) + defined(PPC_OEA64_BRIDGE)) != 1
158#define PMAP_NEEDS_FIXUP
159#endif
160
161extern volatile struct pteg *pmap_pteg_table;
162extern unsigned int pmap_pteg_cnt;
163extern unsigned int pmap_pteg_mask;
164
165void pmap_bootstrap(vaddr_t, vaddr_t);
166void pmap_bootstrap1(vaddr_t, vaddr_t);
167void pmap_bootstrap2(void);
168bool pmap_extract(pmap_t, vaddr_t, paddr_t *);
169bool pmap_query_bit(struct vm_page *, int);
170bool pmap_clear_bit(struct vm_page *, int);
171void pmap_real_memory(paddr_t *, psize_t *);
172void pmap_procwr(struct proc *, vaddr_t, size_t);
173int pmap_pte_spill(pmap_t, vaddr_t, bool);
174int pmap_ste_spill(pmap_t, vaddr_t, bool);
175void pmap_pinit(pmap_t);
176
177#ifdef PPC_OEA601
178bool pmap_extract_ioseg601(vaddr_t, paddr_t *);
179#endif /* PPC_OEA601 */
180#ifdef PPC_OEA
181bool pmap_extract_battable(vaddr_t, paddr_t *);
182#endif /* PPC_OEA */
183
184u_int powerpc_mmap_flags(paddr_t);
185#define POWERPC_MMAP_FLAG_MASK 0xf
186#define POWERPC_MMAP_FLAG_PREFETCHABLE 0x1
187#define POWERPC_MMAP_FLAG_CACHEABLE 0x2
188
189#define pmap_phys_address(ppn) (ppn & ~POWERPC_MMAP_FLAG_MASK)
190#define pmap_mmap_flags(ppn) powerpc_mmap_flags(ppn)
191
192static __inline paddr_t vtophys (vaddr_t);
193
194/*
195 * Alternate mapping hooks for pool pages. Avoids thrashing the TLB.
196 *
197 * Note: This won't work if we have more memory than can be direct-mapped
198 * VA==PA all at once. But pmap_copy_page() and pmap_zero_page() will have
199 * this problem, too.
200 */
201#if !defined(PPC_OEA64)
202#define PMAP_MAP_POOLPAGE(pa) (pa)
203#define PMAP_UNMAP_POOLPAGE(pa) (pa)
204#define POOL_VTOPHYS(va) vtophys((vaddr_t) va)
205
206#define PMAP_ALLOC_POOLPAGE(flags) pmap_alloc_poolpage(flags)
207struct vm_page *pmap_alloc_poolpage(int);
208#endif
209
210static __inline paddr_t
211vtophys(vaddr_t va)
212{
213 paddr_t pa;
214
215 if (pmap_extract(pmap_kernel(), va, &pa))
216 return pa;
217 KASSERTMSG(0, "vtophys: pmap_extract of %#"PRIxVADDR" failed", va);
218 return (paddr_t) -1;
219}
220
221
222#ifdef PMAP_NEEDS_FIXUP
223extern const struct pmap_ops *pmapops;
224extern const struct pmap_ops pmap32_ops;
225extern const struct pmap_ops pmap64_ops;
226extern const struct pmap_ops pmap64bridge_ops;
227
228static __inline void
229pmap_setup32(void)
230{
231 pmapops = &pmap32_ops;
232}
233
234static __inline void
235pmap_setup64(void)
236{
237 pmapops = &pmap64_ops;
238}
239
240static __inline void
241pmap_setup64bridge(void)
242{
243 pmapops = &pmap64bridge_ops;
244}
245#endif
246
247bool pmap_pageidlezero (paddr_t);
248void pmap_syncicache (paddr_t, psize_t);
249#ifdef PPC_OEA64
250vaddr_t pmap_setusr (vaddr_t);
251vaddr_t pmap_unsetusr (void);
252#endif
253
254#ifdef PPC_OEA64_BRIDGE
255int pmap_setup_segment0_map(int use_large_pages, ...);
256#endif
257
258#define PMAP_MD_PREFETCHABLE 0x2000000
259#define PMAP_STEAL_MEMORY
260#define PMAP_NEED_PROCWR
261
262void pmap_zero_page(paddr_t);
263void pmap_copy_page(paddr_t, paddr_t);
264
265LIST_HEAD(pvo_head, pvo_entry);
266
267#define __HAVE_VM_PAGE_MD
268
269struct pmap_page {
270 unsigned int pp_attrs;
271 struct pvo_head pp_pvoh;
272#ifdef MODULAR
273 uintptr_t pp_dummy[3];
274#endif
275};
276
277struct vm_page_md {
278 struct pmap_page mdpg_pp;
279#define mdpg_attrs mdpg_pp.pp_attrs
280#define mdpg_pvoh mdpg_pp.pp_pvoh
281#ifdef MODULAR
282#define mdpg_dummy mdpg_pp.pp_dummy
283#endif
284};
285
286#define VM_MDPAGE_INIT(pg) do { \
287 (pg)->mdpage.mdpg_attrs = 0; \
288 LIST_INIT(&(pg)->mdpage.mdpg_pvoh); \
289} while (/*CONSTCOND*/0)
290
291__END_DECLS
292#endif /* _KERNEL */
293
294#endif /* _POWERPC_OEA_PMAP_H_ */