4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License, Version 1.0 only
6 * (the "License"). You may not use this file except in compliance
9 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10 * or http://www.opensolaris.org/os/licensing.
11 * See the License for the specific language governing permissions
12 * and limitations under the License.
14 * When distributing Covered Code, include this CDDL HEADER in each
15 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16 * If applicable, add the following below this CDDL HEADER, with the
17 * fields enclosed by brackets "[]" replaced with your own identifying
18 * information: Portions Copyright [yyyy] [name of copyright owner]
23 * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
24 * Use is subject to license terms.
28 * Kernel Physical Mapping (kpm) segment driver (segkpm).
30 * This driver delivers along with the hat_kpm* interfaces an alternative
31 * mechanism for kernel mappings within the 64-bit Solaris operating system,
32 * which allows the mapping of all physical memory into the kernel address
33 * space at once. This is feasible in 64 bit kernels, e.g. for Ultrasparc II
34 * and beyond processors, since the available VA range is much larger than
35 * possible physical memory. Momentarily all physical memory is supported,
36 * that is represented by the list of memory segments (memsegs).
38 * Segkpm mappings have also very low overhead and large pages are used
39 * (when possible) to minimize the TLB and TSB footprint. It is also
40 * extentable for other than Sparc architectures (e.g. AMD64). Main
41 * advantage is the avoidance of the TLB-shootdown X-calls, which are
42 * normally needed when a kernel (global) mapping has to be removed.
44 * First example of a kernel facility that uses the segkpm mapping scheme
45 * is seg_map, where it is used as an alternative to hat_memload().
46 * See also hat layer for more information about the hat_kpm* routines.
47 * The kpm facilty can be turned off at boot time (e.g. /etc/system).
50 #include <sys/types.h>
51 #include <sys/param.h>
52 #include <sys/sysmacros.h>
53 #include <sys/systm.h>
54 #include <sys/vnode.h>
55 #include <sys/cmn_err.h>
56 #include <sys/debug.h>
57 #include <sys/thread.h>
58 #include <sys/cpuvar.h>
59 #include <sys/bitmap.h>
60 #include <sys/atomic.h>
63 #include <vm/seg_kmem.h>
64 #include <vm/seg_kpm.h>
71 * Global kpm controls.
72 * See also platform and mmu specific controls.
74 * kpm_enable -- global on/off switch for segkpm.
75 * . Set by default on 64bit platforms that have kpm support.
76 * . Will be disabled from platform layer if not supported.
77 * . Can be disabled via /etc/system.
79 * kpm_smallpages -- use only regular/system pagesize for kpm mappings.
80 * . Can be useful for critical debugging of kpm clients.
81 * . Set to zero by default for platforms that support kpm large pages.
82 * The use of kpm large pages reduces the footprint of kpm meta data
83 * and has all the other advantages of using large pages (e.g TLB
85 * . Set by default for platforms that don't support kpm large pages or
86 * where large pages cannot be used for other reasons (e.g. there are
87 * only few full associative TLB entries available for large pages).
89 * segmap_kpm -- separate on/off switch for segmap using segkpm:
91 * . Will be disabled when kpm_enable is zero.
92 * . Will be disabled when MAXBSIZE != PAGESIZE.
93 * . Can be disabled via /etc/system.
97 int kpm_smallpages
= 0;
101 * Private seg op routines.
103 faultcode_t
segkpm_fault(struct hat
*hat
, struct seg
*seg
, caddr_t addr
,
104 size_t len
, enum fault_type type
, enum seg_rw rw
);
105 static void segkpm_badop(void);
107 #define SEGKPM_BADOP(t) (t(*)())segkpm_badop
109 static const struct seg_ops segkpm_ops
= {
110 .dup
= SEGKPM_BADOP(int),
111 .unmap
= SEGKPM_BADOP(int),
112 .free
= SEGKPM_BADOP(void),
113 .fault
= segkpm_fault
,
114 .faulta
= SEGKPM_BADOP(int),
115 .setprot
= SEGKPM_BADOP(int),
116 .checkprot
= SEGKPM_BADOP(int),
117 .kluster
= SEGKPM_BADOP(int),
118 .sync
= SEGKPM_BADOP(int),
119 .incore
= SEGKPM_BADOP(size_t),
120 .lockop
= SEGKPM_BADOP(int),
121 .getprot
= SEGKPM_BADOP(int),
122 .getoffset
= SEGKPM_BADOP(uoff_t
),
123 .gettype
= SEGKPM_BADOP(int),
124 .getvp
= SEGKPM_BADOP(int),
125 .advise
= SEGKPM_BADOP(int),
126 .setpagesize
= SEGKPM_BADOP(int),
127 .getmemid
= SEGKPM_BADOP(int),
128 .getpolicy
= SEGKPM_BADOP(lgrp_mem_policy_info_t
*),
132 * kpm_pgsz and kpm_pgshft are set by platform layer.
134 size_t kpm_pgsz
; /* kpm page size */
135 uint_t kpm_pgshft
; /* kpm page shift */
136 uoff_t kpm_pgoff
; /* kpm page offset mask */
137 uint_t kpmp2pshft
; /* kpm page to page shift */
138 pgcnt_t kpmpnpgs
; /* how many pages per kpm page */
141 #ifdef SEGKPM_SUPPORT
144 segkpm_create(struct seg
*seg
, void *argsp
)
146 struct segkpm_data
*skd
;
147 struct segkpm_crargs
*b
= (struct segkpm_crargs
*)argsp
;
151 ASSERT(seg
->s_as
&& RW_WRITE_HELD(&seg
->s_as
->a_lock
));
152 ASSERT(btokpmp(seg
->s_size
) >= 1 &&
153 kpmpageoff((uintptr_t)seg
->s_base
) == 0 &&
154 kpmpageoff((uintptr_t)seg
->s_base
+ seg
->s_size
) == 0);
156 skd
= kmem_zalloc(sizeof (struct segkpm_data
), KM_SLEEP
);
158 seg
->s_data
= (void *)skd
;
159 seg
->s_ops
= &segkpm_ops
;
160 skd
->skd_prot
= b
->prot
;
163 * (1) Segkpm virtual addresses are based on physical adresses.
164 * From this and in opposite to other segment drivers it is
165 * often required to allocate a page first to be able to
166 * calculate the final segkpm virtual address.
167 * (2) Page allocation is done by calling page_create_va(),
168 * one important input argument is a virtual address (also
169 * expressed by the "va" in the function name). This function
170 * is highly optimized to select the right page for an optimal
171 * processor and platform support (e.g. virtual addressed
172 * caches (VAC), physical addressed caches, NUMA).
174 * Because of (1) the approach is to generate a faked virtual
175 * address for calling page_create_va(). In order to exploit
176 * the abilities of (2), especially to utilize the cache
177 * hierarchy (3) and to avoid VAC alias conflicts (4) the
178 * selection has to be done carefully. For each virtual color
179 * a separate counter is provided (4). The count values are
180 * used for the utilization of all cache lines (3) and are
181 * corresponding to the cache bins.
183 skd
->skd_nvcolors
= b
->nvcolors
;
185 p
= skd
->skd_va_select
=
186 kmem_zalloc(NCPU
* b
->nvcolors
* sizeof (ushort_t
), KM_SLEEP
);
188 for (i
= 0; i
< NCPU
; i
++)
189 for (j
= 0; j
< b
->nvcolors
; j
++, p
++)
196 * This routine is called via a machine specific fault handling
201 segkpm_fault(struct hat
*hat
, struct seg
*seg
, caddr_t addr
, size_t len
,
202 enum fault_type type
, enum seg_rw rw
)
204 ASSERT(seg
->s_as
&& AS_LOCK_HELD(seg
->s_as
));
208 return (hat_kpm_fault(hat
, addr
));
213 return (FC_NOSUPPORT
);
218 #define addr_to_vcolor(addr, vcolors) \
219 ((int)(((uintptr_t)(addr) & ((vcolors << PAGESHIFT) - 1)) >> PAGESHIFT))
222 * Create a virtual address that can be used for invocations of
223 * page_create_va. Goal is to utilize the cache hierarchy (round
224 * robin bins) and to select the right color for virtual indexed
225 * caches. It isn't exact since we also increment the bin counter
226 * when the caller uses fop_getpage and gets a hit in the page
227 * cache, but we keep the bins turning for cache distribution
228 * (see also segkpm_create block comment).
231 segkpm_create_va(uoff_t off
)
235 struct segkpm_data
*skd
= (struct segkpm_data
*)segkpm
->s_data
;
236 int nvcolors
= skd
->skd_nvcolors
;
239 vcolor
= (nvcolors
> 1) ? addr_to_vcolor(off
, nvcolors
) : 0;
240 p
= &skd
->skd_va_select
[(CPU
->cpu_id
* nvcolors
) + vcolor
];
241 va
= (caddr_t
)ptob(*p
);
243 atomic_add_16(p
, nvcolors
);
249 * Unload mapping if the instance has an active kpm mapping.
252 segkpm_mapout_validkpme(struct kpme
*kpme
)
258 if ((pp
= kpme
->kpe_page
) == NULL
) {
262 if (page_lock(pp
, SE_SHARED
, NULL
, P_RECLAIM
) == 0)
266 * Check if segkpm mapping is not unloaded in the meantime
268 if (kpme
->kpe_page
== NULL
) {
273 vaddr
= hat_kpm_page2va(pp
, 1);
274 hat_kpm_mapout(pp
, kpme
, vaddr
);
281 panic("segkpm_badop");
284 #else /* SEGKPM_SUPPORT */
289 int segkpm_create(struct seg
*seg
, void *argsp
) { return (0); }
293 segkpm_fault(struct hat
*hat
, struct seg
*seg
, caddr_t addr
, size_t len
,
294 enum fault_type type
, enum seg_rw rw
)
296 return ((faultcode_t
)0);
300 caddr_t
segkpm_create_va(uoff_t off
) { return (NULL
); }
303 void segkpm_mapout_validkpme(struct kpme
*kpme
) {}
308 #endif /* SEGKPM_SUPPORT */