10Sstevel@tonic-gate /*
20Sstevel@tonic-gate * CDDL HEADER START
30Sstevel@tonic-gate *
40Sstevel@tonic-gate * The contents of this file are subject to the terms of the
50Sstevel@tonic-gate * Common Development and Distribution License, Version 1.0 only
60Sstevel@tonic-gate * (the "License"). You may not use this file except in compliance
70Sstevel@tonic-gate * with the License.
80Sstevel@tonic-gate *
90Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
100Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing.
110Sstevel@tonic-gate * See the License for the specific language governing permissions
120Sstevel@tonic-gate * and limitations under the License.
130Sstevel@tonic-gate *
140Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each
150Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
160Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the
170Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying
180Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner]
190Sstevel@tonic-gate *
200Sstevel@tonic-gate * CDDL HEADER END
210Sstevel@tonic-gate */
220Sstevel@tonic-gate /*
23*1338Selowe * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
240Sstevel@tonic-gate * Use is subject to license terms.
250Sstevel@tonic-gate */
260Sstevel@tonic-gate
270Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI"
280Sstevel@tonic-gate
290Sstevel@tonic-gate /*
300Sstevel@tonic-gate * Kernel Physical Mapping (kpm) segment driver (segkpm).
310Sstevel@tonic-gate *
320Sstevel@tonic-gate * This driver delivers along with the hat_kpm* interfaces an alternative
330Sstevel@tonic-gate * mechanism for kernel mappings within the 64-bit Solaris operating system,
340Sstevel@tonic-gate * which allows the mapping of all physical memory into the kernel address
350Sstevel@tonic-gate * space at once. This is feasible in 64 bit kernels, e.g. for Ultrasparc II
360Sstevel@tonic-gate * and beyond processors, since the available VA range is much larger than
370Sstevel@tonic-gate * possible physical memory. Momentarily all physical memory is supported,
380Sstevel@tonic-gate * that is represented by the list of memory segments (memsegs).
390Sstevel@tonic-gate *
400Sstevel@tonic-gate * Segkpm mappings have also very low overhead and large pages are used
410Sstevel@tonic-gate * (when possible) to minimize the TLB and TSB footprint. It is also
420Sstevel@tonic-gate * extentable for other than Sparc architectures (e.g. AMD64). Main
430Sstevel@tonic-gate * advantage is the avoidance of the TLB-shootdown X-calls, which are
440Sstevel@tonic-gate * normally needed when a kernel (global) mapping has to be removed.
450Sstevel@tonic-gate *
460Sstevel@tonic-gate * First example of a kernel facility that uses the segkpm mapping scheme
470Sstevel@tonic-gate * is seg_map, where it is used as an alternative to hat_memload().
480Sstevel@tonic-gate * See also hat layer for more information about the hat_kpm* routines.
490Sstevel@tonic-gate * The kpm facilty can be turned off at boot time (e.g. /etc/system).
500Sstevel@tonic-gate */
510Sstevel@tonic-gate
520Sstevel@tonic-gate #include <sys/types.h>
530Sstevel@tonic-gate #include <sys/param.h>
540Sstevel@tonic-gate #include <sys/sysmacros.h>
550Sstevel@tonic-gate #include <sys/systm.h>
560Sstevel@tonic-gate #include <sys/vnode.h>
570Sstevel@tonic-gate #include <sys/cmn_err.h>
580Sstevel@tonic-gate #include <sys/debug.h>
590Sstevel@tonic-gate #include <sys/thread.h>
600Sstevel@tonic-gate #include <sys/cpuvar.h>
610Sstevel@tonic-gate #include <sys/bitmap.h>
620Sstevel@tonic-gate #include <sys/atomic.h>
63670Selowe #include <sys/lgrp.h>
640Sstevel@tonic-gate
650Sstevel@tonic-gate #include <vm/seg_kmem.h>
660Sstevel@tonic-gate #include <vm/seg_kpm.h>
670Sstevel@tonic-gate #include <vm/hat.h>
680Sstevel@tonic-gate #include <vm/as.h>
690Sstevel@tonic-gate #include <vm/seg.h>
700Sstevel@tonic-gate #include <vm/page.h>
710Sstevel@tonic-gate
720Sstevel@tonic-gate /*
730Sstevel@tonic-gate * Global kpm controls.
740Sstevel@tonic-gate * See also platform and mmu specific controls.
750Sstevel@tonic-gate *
760Sstevel@tonic-gate * kpm_enable -- global on/off switch for segkpm.
770Sstevel@tonic-gate * . Set by default on 64bit platforms that have kpm support.
780Sstevel@tonic-gate * . Will be disabled from platform layer if not supported.
790Sstevel@tonic-gate * . Can be disabled via /etc/system.
800Sstevel@tonic-gate *
810Sstevel@tonic-gate * kpm_smallpages -- use only regular/system pagesize for kpm mappings.
820Sstevel@tonic-gate * . Can be useful for critical debugging of kpm clients.
830Sstevel@tonic-gate * . Set to zero by default for platforms that support kpm large pages.
840Sstevel@tonic-gate * The use of kpm large pages reduces the footprint of kpm meta data
850Sstevel@tonic-gate * and has all the other advantages of using large pages (e.g TLB
860Sstevel@tonic-gate * miss reduction).
870Sstevel@tonic-gate * . Set by default for platforms that don't support kpm large pages or
880Sstevel@tonic-gate * where large pages cannot be used for other reasons (e.g. there are
890Sstevel@tonic-gate * only few full associative TLB entries available for large pages).
900Sstevel@tonic-gate *
910Sstevel@tonic-gate * segmap_kpm -- separate on/off switch for segmap using segkpm:
920Sstevel@tonic-gate * . Set by default.
930Sstevel@tonic-gate * . Will be disabled when kpm_enable is zero.
940Sstevel@tonic-gate * . Will be disabled when MAXBSIZE != PAGESIZE.
950Sstevel@tonic-gate * . Can be disabled via /etc/system.
960Sstevel@tonic-gate *
970Sstevel@tonic-gate */
980Sstevel@tonic-gate int kpm_enable = 1;
990Sstevel@tonic-gate int kpm_smallpages = 0;
1000Sstevel@tonic-gate int segmap_kpm = 1;
1010Sstevel@tonic-gate
1020Sstevel@tonic-gate /*
1030Sstevel@tonic-gate * Private seg op routines.
1040Sstevel@tonic-gate */
1050Sstevel@tonic-gate faultcode_t segkpm_fault(struct hat *hat, struct seg *seg, caddr_t addr,
1060Sstevel@tonic-gate size_t len, enum fault_type type, enum seg_rw rw);
1070Sstevel@tonic-gate static void segkpm_dump(struct seg *);
1080Sstevel@tonic-gate static void segkpm_badop(void);
1090Sstevel@tonic-gate static int segkpm_notsup(void);
110670Selowe static int segkpm_capable(struct seg *, segcapability_t);
1110Sstevel@tonic-gate
1120Sstevel@tonic-gate #define SEGKPM_BADOP(t) (t(*)())segkpm_badop
1130Sstevel@tonic-gate #define SEGKPM_NOTSUP (int(*)())segkpm_notsup
1140Sstevel@tonic-gate
1150Sstevel@tonic-gate static struct seg_ops segkpm_ops = {
1160Sstevel@tonic-gate SEGKPM_BADOP(int), /* dup */
1170Sstevel@tonic-gate SEGKPM_BADOP(int), /* unmap */
1180Sstevel@tonic-gate SEGKPM_BADOP(void), /* free */
1190Sstevel@tonic-gate segkpm_fault,
1200Sstevel@tonic-gate SEGKPM_BADOP(int), /* faulta */
1210Sstevel@tonic-gate SEGKPM_BADOP(int), /* setprot */
1220Sstevel@tonic-gate SEGKPM_BADOP(int), /* checkprot */
1230Sstevel@tonic-gate SEGKPM_BADOP(int), /* kluster */
1240Sstevel@tonic-gate SEGKPM_BADOP(size_t), /* swapout */
1250Sstevel@tonic-gate SEGKPM_BADOP(int), /* sync */
1260Sstevel@tonic-gate SEGKPM_BADOP(size_t), /* incore */
1270Sstevel@tonic-gate SEGKPM_BADOP(int), /* lockop */
1280Sstevel@tonic-gate SEGKPM_BADOP(int), /* getprot */
1290Sstevel@tonic-gate SEGKPM_BADOP(u_offset_t), /* getoffset */
1300Sstevel@tonic-gate SEGKPM_BADOP(int), /* gettype */
1310Sstevel@tonic-gate SEGKPM_BADOP(int), /* getvp */
1320Sstevel@tonic-gate SEGKPM_BADOP(int), /* advise */
1330Sstevel@tonic-gate segkpm_dump, /* dump */
1340Sstevel@tonic-gate SEGKPM_NOTSUP, /* pagelock */
1350Sstevel@tonic-gate SEGKPM_BADOP(int), /* setpgsz */
1360Sstevel@tonic-gate SEGKPM_BADOP(int), /* getmemid */
137670Selowe SEGKPM_BADOP(lgrp_mem_policy_info_t *), /* getpolicy */
138670Selowe segkpm_capable, /* capable */
1390Sstevel@tonic-gate };
1400Sstevel@tonic-gate
1410Sstevel@tonic-gate /*
1420Sstevel@tonic-gate * kpm_pgsz and kpm_pgshft are set by platform layer.
1430Sstevel@tonic-gate */
1440Sstevel@tonic-gate size_t kpm_pgsz; /* kpm page size */
1450Sstevel@tonic-gate uint_t kpm_pgshft; /* kpm page shift */
1460Sstevel@tonic-gate u_offset_t kpm_pgoff; /* kpm page offset mask */
1470Sstevel@tonic-gate uint_t kpmp2pshft; /* kpm page to page shift */
1480Sstevel@tonic-gate pgcnt_t kpmpnpgs; /* how many pages per kpm page */
1490Sstevel@tonic-gate
1500Sstevel@tonic-gate
1510Sstevel@tonic-gate #ifdef SEGKPM_SUPPORT
1520Sstevel@tonic-gate
1530Sstevel@tonic-gate int
segkpm_create(struct seg * seg,void * argsp)1540Sstevel@tonic-gate segkpm_create(struct seg *seg, void *argsp)
1550Sstevel@tonic-gate {
1560Sstevel@tonic-gate struct segkpm_data *skd;
1570Sstevel@tonic-gate struct segkpm_crargs *b = (struct segkpm_crargs *)argsp;
1580Sstevel@tonic-gate ushort_t *p;
1590Sstevel@tonic-gate int i, j;
1600Sstevel@tonic-gate
1610Sstevel@tonic-gate ASSERT(seg->s_as && RW_WRITE_HELD(&seg->s_as->a_lock));
1620Sstevel@tonic-gate ASSERT(btokpmp(seg->s_size) >= 1 &&
1630Sstevel@tonic-gate kpmpageoff((uintptr_t)seg->s_base) == 0 &&
1640Sstevel@tonic-gate kpmpageoff((uintptr_t)seg->s_base + seg->s_size) == 0);
1650Sstevel@tonic-gate
1660Sstevel@tonic-gate skd = kmem_zalloc(sizeof (struct segkpm_data), KM_SLEEP);
1670Sstevel@tonic-gate
1680Sstevel@tonic-gate seg->s_data = (void *)skd;
1690Sstevel@tonic-gate seg->s_ops = &segkpm_ops;
1700Sstevel@tonic-gate skd->skd_prot = b->prot;
1710Sstevel@tonic-gate
1720Sstevel@tonic-gate /*
1730Sstevel@tonic-gate * (1) Segkpm virtual addresses are based on physical adresses.
1740Sstevel@tonic-gate * From this and in opposite to other segment drivers it is
1750Sstevel@tonic-gate * often required to allocate a page first to be able to
1760Sstevel@tonic-gate * calculate the final segkpm virtual address.
1770Sstevel@tonic-gate * (2) Page allocation is done by calling page_create_va(),
1780Sstevel@tonic-gate * one important input argument is a virtual address (also
1790Sstevel@tonic-gate * expressed by the "va" in the function name). This function
1800Sstevel@tonic-gate * is highly optimized to select the right page for an optimal
1810Sstevel@tonic-gate * processor and platform support (e.g. virtual addressed
1820Sstevel@tonic-gate * caches (VAC), physical addressed caches, NUMA).
1830Sstevel@tonic-gate *
1840Sstevel@tonic-gate * Because of (1) the approach is to generate a faked virtual
1850Sstevel@tonic-gate * address for calling page_create_va(). In order to exploit
1860Sstevel@tonic-gate * the abilities of (2), especially to utilize the cache
1870Sstevel@tonic-gate * hierarchy (3) and to avoid VAC alias conflicts (4) the
1880Sstevel@tonic-gate * selection has to be done carefully. For each virtual color
1890Sstevel@tonic-gate * a separate counter is provided (4). The count values are
1900Sstevel@tonic-gate * used for the utilization of all cache lines (3) and are
1910Sstevel@tonic-gate * corresponding to the cache bins.
1920Sstevel@tonic-gate */
1930Sstevel@tonic-gate skd->skd_nvcolors = b->nvcolors;
1940Sstevel@tonic-gate
1950Sstevel@tonic-gate p = skd->skd_va_select =
1960Sstevel@tonic-gate kmem_zalloc(NCPU * b->nvcolors * sizeof (ushort_t), KM_SLEEP);
1970Sstevel@tonic-gate
1980Sstevel@tonic-gate for (i = 0; i < NCPU; i++)
1990Sstevel@tonic-gate for (j = 0; j < b->nvcolors; j++, p++)
2000Sstevel@tonic-gate *p = j;
2010Sstevel@tonic-gate
2020Sstevel@tonic-gate return (0);
2030Sstevel@tonic-gate }
2040Sstevel@tonic-gate
2050Sstevel@tonic-gate /*
2060Sstevel@tonic-gate * This routine is called via a machine specific fault handling
2070Sstevel@tonic-gate * routine.
2080Sstevel@tonic-gate */
2090Sstevel@tonic-gate /* ARGSUSED */
2100Sstevel@tonic-gate faultcode_t
segkpm_fault(struct hat * hat,struct seg * seg,caddr_t addr,size_t len,enum fault_type type,enum seg_rw rw)2110Sstevel@tonic-gate segkpm_fault(struct hat *hat, struct seg *seg, caddr_t addr, size_t len,
2120Sstevel@tonic-gate enum fault_type type, enum seg_rw rw)
2130Sstevel@tonic-gate {
2140Sstevel@tonic-gate ASSERT(seg->s_as && AS_LOCK_HELD(seg->s_as, &seg->s_as->a_lock));
2150Sstevel@tonic-gate
216*1338Selowe switch (type) {
217*1338Selowe case F_INVAL:
218*1338Selowe return (hat_kpm_fault(hat, addr));
219*1338Selowe case F_SOFTLOCK:
220*1338Selowe case F_SOFTUNLOCK:
221*1338Selowe return (0);
222*1338Selowe default:
223*1338Selowe return (FC_NOSUPPORT);
224*1338Selowe }
225*1338Selowe /*NOTREACHED*/
2260Sstevel@tonic-gate }
2270Sstevel@tonic-gate
2280Sstevel@tonic-gate #define addr_to_vcolor(addr, vcolors) \
2290Sstevel@tonic-gate ((int)(((uintptr_t)(addr) & ((vcolors << PAGESHIFT) - 1)) >> PAGESHIFT))
2300Sstevel@tonic-gate
2310Sstevel@tonic-gate /*
2320Sstevel@tonic-gate * Create a virtual address that can be used for invocations of
2330Sstevel@tonic-gate * page_create_va. Goal is to utilize the cache hierarchy (round
2340Sstevel@tonic-gate * robin bins) and to select the right color for virtual indexed
2350Sstevel@tonic-gate * caches. It isn't exact since we also increment the bin counter
2360Sstevel@tonic-gate * when the caller uses VOP_GETPAGE and gets a hit in the page
2370Sstevel@tonic-gate * cache, but we keep the bins turning for cache distribution
2380Sstevel@tonic-gate * (see also segkpm_create block comment).
2390Sstevel@tonic-gate */
2400Sstevel@tonic-gate caddr_t
segkpm_create_va(u_offset_t off)2410Sstevel@tonic-gate segkpm_create_va(u_offset_t off)
2420Sstevel@tonic-gate {
2430Sstevel@tonic-gate int vcolor;
2440Sstevel@tonic-gate ushort_t *p;
2450Sstevel@tonic-gate struct segkpm_data *skd = (struct segkpm_data *)segkpm->s_data;
2460Sstevel@tonic-gate int nvcolors = skd->skd_nvcolors;
2470Sstevel@tonic-gate caddr_t va;
2480Sstevel@tonic-gate
2490Sstevel@tonic-gate vcolor = (nvcolors > 1) ? addr_to_vcolor(off, nvcolors) : 0;
2500Sstevel@tonic-gate p = &skd->skd_va_select[(CPU->cpu_id * nvcolors) + vcolor];
2510Sstevel@tonic-gate va = (caddr_t)ptob(*p);
2520Sstevel@tonic-gate
2530Sstevel@tonic-gate atomic_add_16(p, nvcolors);
2540Sstevel@tonic-gate
2550Sstevel@tonic-gate return (va);
2560Sstevel@tonic-gate }
2570Sstevel@tonic-gate
2580Sstevel@tonic-gate /*
2590Sstevel@tonic-gate * Unload mapping if the instance has an active kpm mapping.
2600Sstevel@tonic-gate */
2610Sstevel@tonic-gate void
segkpm_mapout_validkpme(struct kpme * kpme)2620Sstevel@tonic-gate segkpm_mapout_validkpme(struct kpme *kpme)
2630Sstevel@tonic-gate {
2640Sstevel@tonic-gate caddr_t vaddr;
2650Sstevel@tonic-gate page_t *pp;
2660Sstevel@tonic-gate
2670Sstevel@tonic-gate retry:
2680Sstevel@tonic-gate if ((pp = kpme->kpe_page) == NULL) {
2690Sstevel@tonic-gate return;
2700Sstevel@tonic-gate }
2710Sstevel@tonic-gate
2720Sstevel@tonic-gate if (page_lock(pp, SE_SHARED, (kmutex_t *)NULL, P_RECLAIM) == 0)
2730Sstevel@tonic-gate goto retry;
2740Sstevel@tonic-gate
2750Sstevel@tonic-gate /*
2760Sstevel@tonic-gate * Check if segkpm mapping is not unloaded in the meantime
2770Sstevel@tonic-gate */
2780Sstevel@tonic-gate if (kpme->kpe_page == NULL) {
2790Sstevel@tonic-gate page_unlock(pp);
2800Sstevel@tonic-gate return;
2810Sstevel@tonic-gate }
2820Sstevel@tonic-gate
2830Sstevel@tonic-gate vaddr = hat_kpm_page2va(pp, 1);
2840Sstevel@tonic-gate hat_kpm_mapout(pp, kpme, vaddr);
2850Sstevel@tonic-gate page_unlock(pp);
2860Sstevel@tonic-gate }
2870Sstevel@tonic-gate
2880Sstevel@tonic-gate static void
segkpm_badop()2890Sstevel@tonic-gate segkpm_badop()
2900Sstevel@tonic-gate {
2910Sstevel@tonic-gate panic("segkpm_badop");
2920Sstevel@tonic-gate }
2930Sstevel@tonic-gate
2940Sstevel@tonic-gate #else /* SEGKPM_SUPPORT */
2950Sstevel@tonic-gate
2960Sstevel@tonic-gate /* segkpm stubs */
2970Sstevel@tonic-gate
2980Sstevel@tonic-gate /*ARGSUSED*/
segkpm_create(struct seg * seg,void * argsp)2990Sstevel@tonic-gate int segkpm_create(struct seg *seg, void *argsp) { return (0); }
3000Sstevel@tonic-gate
3010Sstevel@tonic-gate /* ARGSUSED */
3020Sstevel@tonic-gate faultcode_t
segkpm_fault(struct hat * hat,struct seg * seg,caddr_t addr,size_t len,enum fault_type type,enum seg_rw rw)3030Sstevel@tonic-gate segkpm_fault(struct hat *hat, struct seg *seg, caddr_t addr, size_t len,
3040Sstevel@tonic-gate enum fault_type type, enum seg_rw rw)
3050Sstevel@tonic-gate {
3060Sstevel@tonic-gate return ((faultcode_t)0);
3070Sstevel@tonic-gate }
3080Sstevel@tonic-gate
3090Sstevel@tonic-gate /* ARGSUSED */
segkpm_create_va(u_offset_t off)3100Sstevel@tonic-gate caddr_t segkpm_create_va(u_offset_t off) { return (NULL); }
3110Sstevel@tonic-gate
3120Sstevel@tonic-gate /* ARGSUSED */
segkpm_mapout_validkpme(struct kpme * kpme)3130Sstevel@tonic-gate void segkpm_mapout_validkpme(struct kpme *kpme) {}
3140Sstevel@tonic-gate
3150Sstevel@tonic-gate static void
segkpm_badop()3160Sstevel@tonic-gate segkpm_badop() {}
3170Sstevel@tonic-gate
3180Sstevel@tonic-gate #endif /* SEGKPM_SUPPORT */
3190Sstevel@tonic-gate
3200Sstevel@tonic-gate static int
segkpm_notsup()3210Sstevel@tonic-gate segkpm_notsup()
3220Sstevel@tonic-gate {
3230Sstevel@tonic-gate return (ENOTSUP);
3240Sstevel@tonic-gate }
3250Sstevel@tonic-gate
3260Sstevel@tonic-gate /*
3270Sstevel@tonic-gate * segkpm pages are not dumped, so we just return
3280Sstevel@tonic-gate */
3290Sstevel@tonic-gate /*ARGSUSED*/
3300Sstevel@tonic-gate static void
segkpm_dump(struct seg * seg)3310Sstevel@tonic-gate segkpm_dump(struct seg *seg)
3320Sstevel@tonic-gate {}
333670Selowe
334670Selowe /*
335670Selowe * We claim to have no special capabilities.
336670Selowe */
337670Selowe /*ARGSUSED*/
338670Selowe static int
segkpm_capable(struct seg * seg,segcapability_t capability)339670Selowe segkpm_capable(struct seg *seg, segcapability_t capability)
340670Selowe {
341670Selowe return (0);
342670Selowe }
343