xref: /onnv-gate/usr/src/uts/common/vm/seg_dev.c (revision 1900)
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
5*1900Seota  * Common Development and Distribution License (the "License").
6*1900Seota  * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate  *
80Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate  * See the License for the specific language governing permissions
110Sstevel@tonic-gate  * and limitations under the License.
120Sstevel@tonic-gate  *
130Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate  *
190Sstevel@tonic-gate  * CDDL HEADER END
200Sstevel@tonic-gate  */
21*1900Seota 
220Sstevel@tonic-gate /*
23*1900Seota  * 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 /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
280Sstevel@tonic-gate /*	  All Rights Reserved  	*/
290Sstevel@tonic-gate 
300Sstevel@tonic-gate /*
310Sstevel@tonic-gate  * University Copyright- Copyright (c) 1982, 1986, 1988
320Sstevel@tonic-gate  * The Regents of the University of California
330Sstevel@tonic-gate  * All Rights Reserved
340Sstevel@tonic-gate  *
350Sstevel@tonic-gate  * University Acknowledgment- Portions of this document are derived from
360Sstevel@tonic-gate  * software developed by the University of California, Berkeley, and its
370Sstevel@tonic-gate  * contributors.
380Sstevel@tonic-gate  */
390Sstevel@tonic-gate 
400Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
410Sstevel@tonic-gate 
420Sstevel@tonic-gate /*
430Sstevel@tonic-gate  * VM - segment of a mapped device.
440Sstevel@tonic-gate  *
450Sstevel@tonic-gate  * This segment driver is used when mapping character special devices.
460Sstevel@tonic-gate  */
470Sstevel@tonic-gate 
480Sstevel@tonic-gate #include <sys/types.h>
490Sstevel@tonic-gate #include <sys/t_lock.h>
500Sstevel@tonic-gate #include <sys/sysmacros.h>
510Sstevel@tonic-gate #include <sys/vtrace.h>
520Sstevel@tonic-gate #include <sys/systm.h>
530Sstevel@tonic-gate #include <sys/vmsystm.h>
540Sstevel@tonic-gate #include <sys/mman.h>
550Sstevel@tonic-gate #include <sys/errno.h>
560Sstevel@tonic-gate #include <sys/kmem.h>
570Sstevel@tonic-gate #include <sys/cmn_err.h>
580Sstevel@tonic-gate #include <sys/vnode.h>
590Sstevel@tonic-gate #include <sys/proc.h>
600Sstevel@tonic-gate #include <sys/conf.h>
610Sstevel@tonic-gate #include <sys/debug.h>
620Sstevel@tonic-gate #include <sys/ddidevmap.h>
63*1900Seota #include <sys/ddi_implfuncs.h>
640Sstevel@tonic-gate #include <sys/lgrp.h>
650Sstevel@tonic-gate 
660Sstevel@tonic-gate #include <vm/page.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/seg_dev.h>
710Sstevel@tonic-gate #include <vm/seg_kp.h>
720Sstevel@tonic-gate #include <vm/seg_kmem.h>
730Sstevel@tonic-gate #include <vm/vpage.h>
740Sstevel@tonic-gate 
750Sstevel@tonic-gate #include <sys/sunddi.h>
760Sstevel@tonic-gate #include <sys/esunddi.h>
770Sstevel@tonic-gate #include <sys/fs/snode.h>
780Sstevel@tonic-gate 
79*1900Seota 
800Sstevel@tonic-gate #if DEBUG
810Sstevel@tonic-gate int segdev_debug;
820Sstevel@tonic-gate #define	DEBUGF(level, args) { if (segdev_debug >= (level)) cmn_err args; }
830Sstevel@tonic-gate #else
840Sstevel@tonic-gate #define	DEBUGF(level, args)
850Sstevel@tonic-gate #endif
860Sstevel@tonic-gate 
870Sstevel@tonic-gate /* Default timeout for devmap context management */
880Sstevel@tonic-gate #define	CTX_TIMEOUT_VALUE 0
890Sstevel@tonic-gate 
900Sstevel@tonic-gate #define	HOLD_DHP_LOCK(dhp)  if (dhp->dh_flags & DEVMAP_ALLOW_REMAP) \
910Sstevel@tonic-gate 			{ mutex_enter(&dhp->dh_lock); }
920Sstevel@tonic-gate 
930Sstevel@tonic-gate #define	RELE_DHP_LOCK(dhp) if (dhp->dh_flags & DEVMAP_ALLOW_REMAP) \
940Sstevel@tonic-gate 			{ mutex_exit(&dhp->dh_lock); }
950Sstevel@tonic-gate 
960Sstevel@tonic-gate #define	round_down_p2(a, s)	((a) & ~((s) - 1))
970Sstevel@tonic-gate #define	round_up_p2(a, s)	(((a) + (s) - 1) & ~((s) - 1))
980Sstevel@tonic-gate 
990Sstevel@tonic-gate /*
1000Sstevel@tonic-gate  * VA_PA_ALIGNED checks to see if both VA and PA are on pgsize boundary
1010Sstevel@tonic-gate  * VA_PA_PGSIZE_ALIGNED check to see if VA is aligned with PA w.r.t. pgsize
1020Sstevel@tonic-gate  */
1030Sstevel@tonic-gate #define	VA_PA_ALIGNED(uvaddr, paddr, pgsize)		\
1040Sstevel@tonic-gate 	(((uvaddr | paddr) & (pgsize - 1)) == 0)
1050Sstevel@tonic-gate #define	VA_PA_PGSIZE_ALIGNED(uvaddr, paddr, pgsize)	\
1060Sstevel@tonic-gate 	(((uvaddr ^ paddr) & (pgsize - 1)) == 0)
1070Sstevel@tonic-gate 
1080Sstevel@tonic-gate #define	vpgtob(n)	((n) * sizeof (struct vpage))	/* For brevity */
1090Sstevel@tonic-gate 
1100Sstevel@tonic-gate #define	VTOCVP(vp)	(VTOS(vp)->s_commonvp)	/* we "know" it's an snode */
1110Sstevel@tonic-gate 
1120Sstevel@tonic-gate static struct devmap_ctx *devmapctx_list = NULL;
1130Sstevel@tonic-gate static struct devmap_softlock *devmap_slist = NULL;
1140Sstevel@tonic-gate 
1150Sstevel@tonic-gate /*
1160Sstevel@tonic-gate  * mutex, vnode and page for the page of zeros we use for the trash mappings.
1170Sstevel@tonic-gate  * One trash page is allocated on the first ddi_umem_setup call that uses it
1180Sstevel@tonic-gate  * XXX Eventually, we may want to combine this with what segnf does when all
1190Sstevel@tonic-gate  * hat layers implement HAT_NOFAULT.
1200Sstevel@tonic-gate  *
1210Sstevel@tonic-gate  * The trash page is used when the backing store for a userland mapping is
1220Sstevel@tonic-gate  * removed but the application semantics do not take kindly to a SIGBUS.
1230Sstevel@tonic-gate  * In that scenario, the applications pages are mapped to some dummy page
1240Sstevel@tonic-gate  * which returns garbage on read and writes go into a common place.
1250Sstevel@tonic-gate  * (Perfect for NO_FAULT semantics)
1260Sstevel@tonic-gate  * The device driver is responsible to communicating to the app with some
1270Sstevel@tonic-gate  * other mechanism that such remapping has happened and the app should take
1280Sstevel@tonic-gate  * corrective action.
1290Sstevel@tonic-gate  * We can also use an anonymous memory page as there is no requirement to
1300Sstevel@tonic-gate  * keep the page locked, however this complicates the fault code. RFE.
1310Sstevel@tonic-gate  */
1320Sstevel@tonic-gate static struct vnode trashvp;
1330Sstevel@tonic-gate static struct page *trashpp;
1340Sstevel@tonic-gate 
1350Sstevel@tonic-gate /* Non-pageable kernel memory is allocated from the umem_np_arena. */
1360Sstevel@tonic-gate static vmem_t *umem_np_arena;
1370Sstevel@tonic-gate 
1380Sstevel@tonic-gate /* Set the cookie to a value we know will never be a valid umem_cookie */
1390Sstevel@tonic-gate #define	DEVMAP_DEVMEM_COOKIE	((ddi_umem_cookie_t)0x1)
1400Sstevel@tonic-gate 
1410Sstevel@tonic-gate /*
1420Sstevel@tonic-gate  * Macros to check if type of devmap handle
1430Sstevel@tonic-gate  */
1440Sstevel@tonic-gate #define	cookie_is_devmem(c)	\
1450Sstevel@tonic-gate 	((c) == (struct ddi_umem_cookie *)DEVMAP_DEVMEM_COOKIE)
1460Sstevel@tonic-gate 
1470Sstevel@tonic-gate #define	cookie_is_pmem(c)	\
1480Sstevel@tonic-gate 	((c) == (struct ddi_umem_cookie *)DEVMAP_PMEM_COOKIE)
1490Sstevel@tonic-gate 
1500Sstevel@tonic-gate #define	cookie_is_kpmem(c)	(!cookie_is_devmem(c) && !cookie_is_pmem(c) &&\
1510Sstevel@tonic-gate 	((c)->type == KMEM_PAGEABLE))
1520Sstevel@tonic-gate 
1530Sstevel@tonic-gate #define	dhp_is_devmem(dhp)	\
1540Sstevel@tonic-gate 	(cookie_is_devmem((struct ddi_umem_cookie *)((dhp)->dh_cookie)))
1550Sstevel@tonic-gate 
1560Sstevel@tonic-gate #define	dhp_is_pmem(dhp)	\
1570Sstevel@tonic-gate 	(cookie_is_pmem((struct ddi_umem_cookie *)((dhp)->dh_cookie)))
1580Sstevel@tonic-gate 
1590Sstevel@tonic-gate #define	dhp_is_kpmem(dhp)	\
1600Sstevel@tonic-gate 	(cookie_is_kpmem((struct ddi_umem_cookie *)((dhp)->dh_cookie)))
1610Sstevel@tonic-gate 
1620Sstevel@tonic-gate /*
1630Sstevel@tonic-gate  * Private seg op routines.
1640Sstevel@tonic-gate  */
1650Sstevel@tonic-gate static int	segdev_dup(struct seg *, struct seg *);
1660Sstevel@tonic-gate static int	segdev_unmap(struct seg *, caddr_t, size_t);
1670Sstevel@tonic-gate static void	segdev_free(struct seg *);
1680Sstevel@tonic-gate static faultcode_t segdev_fault(struct hat *, struct seg *, caddr_t, size_t,
1690Sstevel@tonic-gate 		    enum fault_type, enum seg_rw);
1700Sstevel@tonic-gate static faultcode_t segdev_faulta(struct seg *, caddr_t);
1710Sstevel@tonic-gate static int	segdev_setprot(struct seg *, caddr_t, size_t, uint_t);
1720Sstevel@tonic-gate static int	segdev_checkprot(struct seg *, caddr_t, size_t, uint_t);
1730Sstevel@tonic-gate static void	segdev_badop(void);
1740Sstevel@tonic-gate static int	segdev_sync(struct seg *, caddr_t, size_t, int, uint_t);
1750Sstevel@tonic-gate static size_t	segdev_incore(struct seg *, caddr_t, size_t, char *);
1760Sstevel@tonic-gate static int	segdev_lockop(struct seg *, caddr_t, size_t, int, int,
1770Sstevel@tonic-gate 		    ulong_t *, size_t);
1780Sstevel@tonic-gate static int	segdev_getprot(struct seg *, caddr_t, size_t, uint_t *);
1790Sstevel@tonic-gate static u_offset_t	segdev_getoffset(struct seg *, caddr_t);
1800Sstevel@tonic-gate static int	segdev_gettype(struct seg *, caddr_t);
1810Sstevel@tonic-gate static int	segdev_getvp(struct seg *, caddr_t, struct vnode **);
1820Sstevel@tonic-gate static int	segdev_advise(struct seg *, caddr_t, size_t, uint_t);
1830Sstevel@tonic-gate static void	segdev_dump(struct seg *);
1840Sstevel@tonic-gate static int	segdev_pagelock(struct seg *, caddr_t, size_t,
1850Sstevel@tonic-gate 		    struct page ***, enum lock_type, enum seg_rw);
1860Sstevel@tonic-gate static int	segdev_setpagesize(struct seg *, caddr_t, size_t, uint_t);
1870Sstevel@tonic-gate static int	segdev_getmemid(struct seg *, caddr_t, memid_t *);
1880Sstevel@tonic-gate static lgrp_mem_policy_info_t	*segdev_getpolicy(struct seg *, caddr_t);
189670Selowe static int	segdev_capable(struct seg *, segcapability_t);
1900Sstevel@tonic-gate 
1910Sstevel@tonic-gate /*
1920Sstevel@tonic-gate  * XXX	this struct is used by rootnex_map_fault to identify
1930Sstevel@tonic-gate  *	the segment it has been passed. So if you make it
1940Sstevel@tonic-gate  *	"static" you'll need to fix rootnex_map_fault.
1950Sstevel@tonic-gate  */
1960Sstevel@tonic-gate struct seg_ops segdev_ops = {
1970Sstevel@tonic-gate 	segdev_dup,
1980Sstevel@tonic-gate 	segdev_unmap,
1990Sstevel@tonic-gate 	segdev_free,
2000Sstevel@tonic-gate 	segdev_fault,
2010Sstevel@tonic-gate 	segdev_faulta,
2020Sstevel@tonic-gate 	segdev_setprot,
2030Sstevel@tonic-gate 	segdev_checkprot,
2040Sstevel@tonic-gate 	(int (*)())segdev_badop,	/* kluster */
2050Sstevel@tonic-gate 	(size_t (*)(struct seg *))NULL,	/* swapout */
2060Sstevel@tonic-gate 	segdev_sync,			/* sync */
2070Sstevel@tonic-gate 	segdev_incore,
2080Sstevel@tonic-gate 	segdev_lockop,			/* lockop */
2090Sstevel@tonic-gate 	segdev_getprot,
2100Sstevel@tonic-gate 	segdev_getoffset,
2110Sstevel@tonic-gate 	segdev_gettype,
2120Sstevel@tonic-gate 	segdev_getvp,
2130Sstevel@tonic-gate 	segdev_advise,
2140Sstevel@tonic-gate 	segdev_dump,
2150Sstevel@tonic-gate 	segdev_pagelock,
2160Sstevel@tonic-gate 	segdev_setpagesize,
2170Sstevel@tonic-gate 	segdev_getmemid,
2180Sstevel@tonic-gate 	segdev_getpolicy,
219670Selowe 	segdev_capable,
2200Sstevel@tonic-gate };
2210Sstevel@tonic-gate 
2220Sstevel@tonic-gate /*
2230Sstevel@tonic-gate  * Private segdev support routines
2240Sstevel@tonic-gate  */
2250Sstevel@tonic-gate static struct segdev_data *sdp_alloc(void);
2260Sstevel@tonic-gate 
2270Sstevel@tonic-gate static void segdev_softunlock(struct hat *, struct seg *, caddr_t,
2280Sstevel@tonic-gate     size_t, enum seg_rw);
2290Sstevel@tonic-gate 
2300Sstevel@tonic-gate static faultcode_t segdev_faultpage(struct hat *, struct seg *, caddr_t,
2310Sstevel@tonic-gate     struct vpage *, enum fault_type, enum seg_rw, devmap_handle_t *);
2320Sstevel@tonic-gate 
2330Sstevel@tonic-gate static faultcode_t segdev_faultpages(struct hat *, struct seg *, caddr_t,
2340Sstevel@tonic-gate     size_t, enum fault_type, enum seg_rw, devmap_handle_t *);
2350Sstevel@tonic-gate 
2360Sstevel@tonic-gate static struct devmap_ctx *devmap_ctxinit(dev_t, ulong_t);
2370Sstevel@tonic-gate static struct devmap_softlock *devmap_softlock_init(dev_t, ulong_t);
2380Sstevel@tonic-gate static void devmap_softlock_rele(devmap_handle_t *);
2390Sstevel@tonic-gate static void devmap_ctx_rele(devmap_handle_t *);
2400Sstevel@tonic-gate 
2410Sstevel@tonic-gate static void devmap_ctxto(void *);
2420Sstevel@tonic-gate 
2430Sstevel@tonic-gate static devmap_handle_t *devmap_find_handle(devmap_handle_t *dhp_head,
2440Sstevel@tonic-gate     caddr_t addr);
2450Sstevel@tonic-gate 
2460Sstevel@tonic-gate static ulong_t devmap_roundup(devmap_handle_t *dhp, ulong_t offset, size_t len,
2470Sstevel@tonic-gate     ulong_t *opfn, ulong_t *pagesize);
2480Sstevel@tonic-gate 
2490Sstevel@tonic-gate static void free_devmap_handle(devmap_handle_t *dhp);
2500Sstevel@tonic-gate 
2510Sstevel@tonic-gate static int devmap_handle_dup(devmap_handle_t *dhp, devmap_handle_t **new_dhp,
2520Sstevel@tonic-gate     struct seg *newseg);
2530Sstevel@tonic-gate 
2540Sstevel@tonic-gate static devmap_handle_t *devmap_handle_unmap(devmap_handle_t *dhp);
2550Sstevel@tonic-gate 
2560Sstevel@tonic-gate static void devmap_handle_unmap_head(devmap_handle_t *dhp, size_t len);
2570Sstevel@tonic-gate 
2580Sstevel@tonic-gate static void devmap_handle_unmap_tail(devmap_handle_t *dhp, caddr_t addr);
2590Sstevel@tonic-gate 
2600Sstevel@tonic-gate static int devmap_device(devmap_handle_t *dhp, struct as *as, caddr_t *addr,
2610Sstevel@tonic-gate     offset_t off, size_t len, uint_t flags);
2620Sstevel@tonic-gate 
2630Sstevel@tonic-gate static void devmap_get_large_pgsize(devmap_handle_t *dhp, size_t len,
2640Sstevel@tonic-gate     caddr_t addr, size_t *llen, caddr_t *laddr);
2650Sstevel@tonic-gate 
2660Sstevel@tonic-gate static void devmap_handle_reduce_len(devmap_handle_t *dhp, size_t len);
2670Sstevel@tonic-gate 
2680Sstevel@tonic-gate static void *devmap_alloc_pages(vmem_t *vmp, size_t size, int vmflag);
2690Sstevel@tonic-gate static void devmap_free_pages(vmem_t *vmp, void *inaddr, size_t size);
2700Sstevel@tonic-gate 
2710Sstevel@tonic-gate static void *devmap_umem_alloc_np(size_t size, size_t flags);
2720Sstevel@tonic-gate static void devmap_umem_free_np(void *addr, size_t size);
2730Sstevel@tonic-gate 
2740Sstevel@tonic-gate /*
2750Sstevel@tonic-gate  * routines to lock and unlock underlying segkp segment for
2760Sstevel@tonic-gate  * KMEM_PAGEABLE type cookies.
2770Sstevel@tonic-gate  */
2780Sstevel@tonic-gate static faultcode_t  acquire_kpmem_lock(struct ddi_umem_cookie *, size_t);
2790Sstevel@tonic-gate static void release_kpmem_lock(struct ddi_umem_cookie *, size_t);
2800Sstevel@tonic-gate 
2810Sstevel@tonic-gate /*
2820Sstevel@tonic-gate  * Routines to synchronize F_SOFTLOCK and F_INVAL faults for
2830Sstevel@tonic-gate  * drivers with devmap_access callbacks
2840Sstevel@tonic-gate  */
2850Sstevel@tonic-gate static int devmap_softlock_enter(struct devmap_softlock *, size_t,
2860Sstevel@tonic-gate 	enum fault_type);
2870Sstevel@tonic-gate static void devmap_softlock_exit(struct devmap_softlock *, size_t,
2880Sstevel@tonic-gate 	enum fault_type);
2890Sstevel@tonic-gate 
2900Sstevel@tonic-gate static kmutex_t devmapctx_lock;
2910Sstevel@tonic-gate 
2920Sstevel@tonic-gate static kmutex_t devmap_slock;
2930Sstevel@tonic-gate 
2940Sstevel@tonic-gate /*
2950Sstevel@tonic-gate  * Initialize the thread callbacks and thread private data.
2960Sstevel@tonic-gate  */
2970Sstevel@tonic-gate static struct devmap_ctx *
2980Sstevel@tonic-gate devmap_ctxinit(dev_t dev, ulong_t id)
2990Sstevel@tonic-gate {
3000Sstevel@tonic-gate 	struct devmap_ctx	*devctx;
3010Sstevel@tonic-gate 	struct devmap_ctx	*tmp;
3020Sstevel@tonic-gate 	dev_info_t		*dip;
3030Sstevel@tonic-gate 
3040Sstevel@tonic-gate 	tmp =  kmem_zalloc(sizeof (struct devmap_ctx), KM_SLEEP);
3050Sstevel@tonic-gate 
3060Sstevel@tonic-gate 	mutex_enter(&devmapctx_lock);
3070Sstevel@tonic-gate 
3080Sstevel@tonic-gate 	dip = e_ddi_hold_devi_by_dev(dev, 0);
3090Sstevel@tonic-gate 	ASSERT(dip != NULL);
3100Sstevel@tonic-gate 	ddi_release_devi(dip);
3110Sstevel@tonic-gate 
3120Sstevel@tonic-gate 	for (devctx = devmapctx_list; devctx != NULL; devctx = devctx->next)
3130Sstevel@tonic-gate 		if ((devctx->dip == dip) && (devctx->id == id))
3140Sstevel@tonic-gate 			break;
3150Sstevel@tonic-gate 
3160Sstevel@tonic-gate 	if (devctx == NULL) {
3170Sstevel@tonic-gate 		devctx = tmp;
3180Sstevel@tonic-gate 		devctx->dip = dip;
3190Sstevel@tonic-gate 		devctx->id = id;
3200Sstevel@tonic-gate 		mutex_init(&devctx->lock, NULL, MUTEX_DEFAULT, NULL);
3210Sstevel@tonic-gate 		cv_init(&devctx->cv, NULL, CV_DEFAULT, NULL);
3220Sstevel@tonic-gate 		devctx->next = devmapctx_list;
3230Sstevel@tonic-gate 		devmapctx_list = devctx;
3240Sstevel@tonic-gate 	} else
3250Sstevel@tonic-gate 		kmem_free(tmp, sizeof (struct devmap_ctx));
3260Sstevel@tonic-gate 
3270Sstevel@tonic-gate 	mutex_enter(&devctx->lock);
3280Sstevel@tonic-gate 	devctx->refcnt++;
3290Sstevel@tonic-gate 	mutex_exit(&devctx->lock);
3300Sstevel@tonic-gate 	mutex_exit(&devmapctx_lock);
3310Sstevel@tonic-gate 
3320Sstevel@tonic-gate 	return (devctx);
3330Sstevel@tonic-gate }
3340Sstevel@tonic-gate 
3350Sstevel@tonic-gate /*
3360Sstevel@tonic-gate  * Timeout callback called if a CPU has not given up the device context
3370Sstevel@tonic-gate  * within dhp->dh_timeout_length ticks
3380Sstevel@tonic-gate  */
3390Sstevel@tonic-gate static void
3400Sstevel@tonic-gate devmap_ctxto(void *data)
3410Sstevel@tonic-gate {
3420Sstevel@tonic-gate 	struct devmap_ctx *devctx = data;
3430Sstevel@tonic-gate 
3440Sstevel@tonic-gate 	TRACE_1(TR_FAC_DEVMAP, TR_DEVMAP_CTXTO,
3450Sstevel@tonic-gate 	    "devmap_ctxto:timeout expired, devctx=%p", (void *)devctx);
3460Sstevel@tonic-gate 	mutex_enter(&devctx->lock);
3470Sstevel@tonic-gate 	/*
3480Sstevel@tonic-gate 	 * Set oncpu = 0 so the next mapping trying to get the device context
3490Sstevel@tonic-gate 	 * can.
3500Sstevel@tonic-gate 	 */
3510Sstevel@tonic-gate 	devctx->oncpu = 0;
3520Sstevel@tonic-gate 	devctx->timeout = 0;
3530Sstevel@tonic-gate 	cv_signal(&devctx->cv);
3540Sstevel@tonic-gate 	mutex_exit(&devctx->lock);
3550Sstevel@tonic-gate }
3560Sstevel@tonic-gate 
3570Sstevel@tonic-gate /*
3580Sstevel@tonic-gate  * Create a device segment.
3590Sstevel@tonic-gate  */
3600Sstevel@tonic-gate int
3610Sstevel@tonic-gate segdev_create(struct seg *seg, void *argsp)
3620Sstevel@tonic-gate {
3630Sstevel@tonic-gate 	struct segdev_data *sdp;
3640Sstevel@tonic-gate 	struct segdev_crargs *a = (struct segdev_crargs *)argsp;
3650Sstevel@tonic-gate 	devmap_handle_t *dhp = (devmap_handle_t *)a->devmap_data;
3660Sstevel@tonic-gate 	int error;
3670Sstevel@tonic-gate 
3680Sstevel@tonic-gate 	/*
3690Sstevel@tonic-gate 	 * Since the address space is "write" locked, we
3700Sstevel@tonic-gate 	 * don't need the segment lock to protect "segdev" data.
3710Sstevel@tonic-gate 	 */
3720Sstevel@tonic-gate 	ASSERT(seg->s_as && AS_WRITE_HELD(seg->s_as, &seg->s_as->a_lock));
3730Sstevel@tonic-gate 
3740Sstevel@tonic-gate 	hat_map(seg->s_as->a_hat, seg->s_base, seg->s_size, HAT_MAP);
3750Sstevel@tonic-gate 
3760Sstevel@tonic-gate 	sdp = sdp_alloc();
3770Sstevel@tonic-gate 
3780Sstevel@tonic-gate 	sdp->mapfunc = a->mapfunc;
3790Sstevel@tonic-gate 	sdp->offset = a->offset;
3800Sstevel@tonic-gate 	sdp->prot = a->prot;
3810Sstevel@tonic-gate 	sdp->maxprot = a->maxprot;
3820Sstevel@tonic-gate 	sdp->type = a->type;
3830Sstevel@tonic-gate 	sdp->pageprot = 0;
3840Sstevel@tonic-gate 	sdp->softlockcnt = 0;
3850Sstevel@tonic-gate 	sdp->vpage = NULL;
3860Sstevel@tonic-gate 
3870Sstevel@tonic-gate 	if (sdp->mapfunc == NULL)
3880Sstevel@tonic-gate 		sdp->devmap_data = dhp;
3890Sstevel@tonic-gate 	else
3900Sstevel@tonic-gate 		sdp->devmap_data = dhp = NULL;
3910Sstevel@tonic-gate 
3920Sstevel@tonic-gate 	sdp->hat_flags = a->hat_flags;
3930Sstevel@tonic-gate 	sdp->hat_attr = a->hat_attr;
3940Sstevel@tonic-gate 
3950Sstevel@tonic-gate 	/*
3960Sstevel@tonic-gate 	 * Currently, hat_flags supports only HAT_LOAD_NOCONSIST
3970Sstevel@tonic-gate 	 */
3980Sstevel@tonic-gate 	ASSERT(!(sdp->hat_flags & ~HAT_LOAD_NOCONSIST));
3990Sstevel@tonic-gate 
4000Sstevel@tonic-gate 	/*
4010Sstevel@tonic-gate 	 * Hold shadow vnode -- segdev only deals with
4020Sstevel@tonic-gate 	 * character (VCHR) devices. We use the common
4030Sstevel@tonic-gate 	 * vp to hang pages on.
4040Sstevel@tonic-gate 	 */
4050Sstevel@tonic-gate 	sdp->vp = specfind(a->dev, VCHR);
4060Sstevel@tonic-gate 	ASSERT(sdp->vp != NULL);
4070Sstevel@tonic-gate 
4080Sstevel@tonic-gate 	seg->s_ops = &segdev_ops;
4090Sstevel@tonic-gate 	seg->s_data = sdp;
4100Sstevel@tonic-gate 
4110Sstevel@tonic-gate 	while (dhp != NULL) {
4120Sstevel@tonic-gate 		dhp->dh_seg = seg;
4130Sstevel@tonic-gate 		dhp = dhp->dh_next;
4140Sstevel@tonic-gate 	}
4150Sstevel@tonic-gate 
4160Sstevel@tonic-gate 	/*
4170Sstevel@tonic-gate 	 * Inform the vnode of the new mapping.
4180Sstevel@tonic-gate 	 */
4190Sstevel@tonic-gate 	/*
4200Sstevel@tonic-gate 	 * It is ok to use pass sdp->maxprot to ADDMAP rather than to use
4210Sstevel@tonic-gate 	 * dhp specific maxprot because spec_addmap does not use maxprot.
4220Sstevel@tonic-gate 	 */
4230Sstevel@tonic-gate 	error = VOP_ADDMAP(VTOCVP(sdp->vp), sdp->offset,
4240Sstevel@tonic-gate 	    seg->s_as, seg->s_base, seg->s_size,
4250Sstevel@tonic-gate 	    sdp->prot, sdp->maxprot, sdp->type, CRED());
4260Sstevel@tonic-gate 
4270Sstevel@tonic-gate 	if (error != 0) {
4280Sstevel@tonic-gate 		sdp->devmap_data = NULL;
4290Sstevel@tonic-gate 		hat_unload(seg->s_as->a_hat, seg->s_base, seg->s_size,
4300Sstevel@tonic-gate 		    HAT_UNLOAD_UNMAP);
4310Sstevel@tonic-gate 	}
4320Sstevel@tonic-gate 
4330Sstevel@tonic-gate 	return (error);
4340Sstevel@tonic-gate }
4350Sstevel@tonic-gate 
4360Sstevel@tonic-gate static struct segdev_data *
4370Sstevel@tonic-gate sdp_alloc(void)
4380Sstevel@tonic-gate {
4390Sstevel@tonic-gate 	struct segdev_data *sdp;
4400Sstevel@tonic-gate 
4410Sstevel@tonic-gate 	sdp = kmem_zalloc(sizeof (struct segdev_data), KM_SLEEP);
4420Sstevel@tonic-gate 	mutex_init(&sdp->lock, NULL, MUTEX_DEFAULT, NULL);
4430Sstevel@tonic-gate 
4440Sstevel@tonic-gate 	return (sdp);
4450Sstevel@tonic-gate }
4460Sstevel@tonic-gate 
4470Sstevel@tonic-gate /*
4480Sstevel@tonic-gate  * Duplicate seg and return new segment in newseg.
4490Sstevel@tonic-gate  */
4500Sstevel@tonic-gate static int
4510Sstevel@tonic-gate segdev_dup(struct seg *seg, struct seg *newseg)
4520Sstevel@tonic-gate {
4530Sstevel@tonic-gate 	struct segdev_data *sdp = (struct segdev_data *)seg->s_data;
4540Sstevel@tonic-gate 	struct segdev_data *newsdp;
4550Sstevel@tonic-gate 	devmap_handle_t *dhp = (devmap_handle_t *)sdp->devmap_data;
4560Sstevel@tonic-gate 	size_t npages;
4570Sstevel@tonic-gate 	int ret;
4580Sstevel@tonic-gate 
4590Sstevel@tonic-gate 	TRACE_2(TR_FAC_DEVMAP, TR_DEVMAP_DUP,
4600Sstevel@tonic-gate 	    "segdev_dup:start dhp=%p, seg=%p", (void *)dhp, (void *)seg);
4610Sstevel@tonic-gate 
4620Sstevel@tonic-gate 	DEBUGF(3, (CE_CONT, "segdev_dup: dhp %p seg %p\n",
4630Sstevel@tonic-gate 	    (void *)dhp, (void *)seg));
4640Sstevel@tonic-gate 
4650Sstevel@tonic-gate 	/*
4660Sstevel@tonic-gate 	 * Since the address space is "write" locked, we
4670Sstevel@tonic-gate 	 * don't need the segment lock to protect "segdev" data.
4680Sstevel@tonic-gate 	 */
4690Sstevel@tonic-gate 	ASSERT(seg->s_as && AS_WRITE_HELD(seg->s_as, &seg->s_as->a_lock));
4700Sstevel@tonic-gate 
4710Sstevel@tonic-gate 	newsdp = sdp_alloc();
4720Sstevel@tonic-gate 
4730Sstevel@tonic-gate 	newseg->s_ops = seg->s_ops;
4740Sstevel@tonic-gate 	newseg->s_data = (void *)newsdp;
4750Sstevel@tonic-gate 
4760Sstevel@tonic-gate 	VN_HOLD(sdp->vp);
4770Sstevel@tonic-gate 	newsdp->vp 	= sdp->vp;
4780Sstevel@tonic-gate 	newsdp->mapfunc = sdp->mapfunc;
4790Sstevel@tonic-gate 	newsdp->offset	= sdp->offset;
4800Sstevel@tonic-gate 	newsdp->pageprot = sdp->pageprot;
4810Sstevel@tonic-gate 	newsdp->prot	= sdp->prot;
4820Sstevel@tonic-gate 	newsdp->maxprot = sdp->maxprot;
4830Sstevel@tonic-gate 	newsdp->type = sdp->type;
4840Sstevel@tonic-gate 	newsdp->hat_attr = sdp->hat_attr;
4850Sstevel@tonic-gate 	newsdp->hat_flags = sdp->hat_flags;
4860Sstevel@tonic-gate 	newsdp->softlockcnt = 0;
4870Sstevel@tonic-gate 
4880Sstevel@tonic-gate 	/*
4890Sstevel@tonic-gate 	 * Initialize per page data if the segment we are
4900Sstevel@tonic-gate 	 * dup'ing has per page information.
4910Sstevel@tonic-gate 	 */
4920Sstevel@tonic-gate 	npages = seg_pages(newseg);
4930Sstevel@tonic-gate 
4940Sstevel@tonic-gate 	if (sdp->vpage != NULL) {
4950Sstevel@tonic-gate 		size_t nbytes = vpgtob(npages);
4960Sstevel@tonic-gate 
4970Sstevel@tonic-gate 		newsdp->vpage = kmem_zalloc(nbytes, KM_SLEEP);
4980Sstevel@tonic-gate 		bcopy(sdp->vpage, newsdp->vpage, nbytes);
4990Sstevel@tonic-gate 	} else
5000Sstevel@tonic-gate 		newsdp->vpage = NULL;
5010Sstevel@tonic-gate 
5020Sstevel@tonic-gate 	/*
5030Sstevel@tonic-gate 	 * duplicate devmap handles
5040Sstevel@tonic-gate 	 */
5050Sstevel@tonic-gate 	if (dhp != NULL) {
5060Sstevel@tonic-gate 		ret = devmap_handle_dup(dhp,
5070Sstevel@tonic-gate 			(devmap_handle_t **)&newsdp->devmap_data, newseg);
5080Sstevel@tonic-gate 		if (ret != 0) {
5090Sstevel@tonic-gate 			TRACE_3(TR_FAC_DEVMAP, TR_DEVMAP_DUP_CK1,
5100Sstevel@tonic-gate 			    "segdev_dup:ret1 ret=%x, dhp=%p seg=%p",
5110Sstevel@tonic-gate 			    ret, (void *)dhp, (void *)seg);
5120Sstevel@tonic-gate 			DEBUGF(1, (CE_CONT,
5130Sstevel@tonic-gate 			    "segdev_dup: ret %x dhp %p seg %p\n",
5140Sstevel@tonic-gate 			    ret, (void *)dhp, (void *)seg));
5150Sstevel@tonic-gate 			return (ret);
5160Sstevel@tonic-gate 		}
5170Sstevel@tonic-gate 	}
5180Sstevel@tonic-gate 
5190Sstevel@tonic-gate 	/*
5200Sstevel@tonic-gate 	 * Inform the common vnode of the new mapping.
5210Sstevel@tonic-gate 	 */
5220Sstevel@tonic-gate 	return (VOP_ADDMAP(VTOCVP(newsdp->vp),
5230Sstevel@tonic-gate 		newsdp->offset, newseg->s_as,
5240Sstevel@tonic-gate 		newseg->s_base, newseg->s_size, newsdp->prot,
5250Sstevel@tonic-gate 		newsdp->maxprot, sdp->type, CRED()));
5260Sstevel@tonic-gate }
5270Sstevel@tonic-gate 
5280Sstevel@tonic-gate /*
5290Sstevel@tonic-gate  * duplicate devmap handles
5300Sstevel@tonic-gate  */
5310Sstevel@tonic-gate static int
5320Sstevel@tonic-gate devmap_handle_dup(devmap_handle_t *dhp, devmap_handle_t **new_dhp,
5330Sstevel@tonic-gate     struct seg *newseg)
5340Sstevel@tonic-gate {
5350Sstevel@tonic-gate 	devmap_handle_t *newdhp_save = NULL;
5360Sstevel@tonic-gate 	devmap_handle_t *newdhp = NULL;
5370Sstevel@tonic-gate 	struct devmap_callback_ctl *callbackops;
5380Sstevel@tonic-gate 
5390Sstevel@tonic-gate 	while (dhp != NULL) {
5400Sstevel@tonic-gate 		newdhp = kmem_alloc(sizeof (devmap_handle_t), KM_SLEEP);
5410Sstevel@tonic-gate 
5420Sstevel@tonic-gate 		/* Need to lock the original dhp while copying if REMAP */
5430Sstevel@tonic-gate 		HOLD_DHP_LOCK(dhp);
5440Sstevel@tonic-gate 		bcopy(dhp, newdhp, sizeof (devmap_handle_t));
5450Sstevel@tonic-gate 		RELE_DHP_LOCK(dhp);
5460Sstevel@tonic-gate 		newdhp->dh_seg = newseg;
5470Sstevel@tonic-gate 		newdhp->dh_next = NULL;
5480Sstevel@tonic-gate 		if (newdhp_save != NULL)
5490Sstevel@tonic-gate 			newdhp_save->dh_next = newdhp;
5500Sstevel@tonic-gate 		else
5510Sstevel@tonic-gate 			*new_dhp = newdhp;
5520Sstevel@tonic-gate 		newdhp_save = newdhp;
5530Sstevel@tonic-gate 
5540Sstevel@tonic-gate 		callbackops = &newdhp->dh_callbackops;
5550Sstevel@tonic-gate 
5560Sstevel@tonic-gate 		if (dhp->dh_softlock != NULL)
5570Sstevel@tonic-gate 			newdhp->dh_softlock = devmap_softlock_init(
5580Sstevel@tonic-gate 			    newdhp->dh_dev,
5590Sstevel@tonic-gate 			    (ulong_t)callbackops->devmap_access);
5600Sstevel@tonic-gate 		if (dhp->dh_ctx != NULL)
5610Sstevel@tonic-gate 			newdhp->dh_ctx = devmap_ctxinit(newdhp->dh_dev,
5620Sstevel@tonic-gate 			    (ulong_t)callbackops->devmap_access);
5630Sstevel@tonic-gate 
5640Sstevel@tonic-gate 		/*
5650Sstevel@tonic-gate 		 * Initialize dh_lock if we want to do remap.
5660Sstevel@tonic-gate 		 */
5670Sstevel@tonic-gate 		if (newdhp->dh_flags & DEVMAP_ALLOW_REMAP) {
5680Sstevel@tonic-gate 			mutex_init(&newdhp->dh_lock, NULL, MUTEX_DEFAULT, NULL);
5690Sstevel@tonic-gate 			newdhp->dh_flags |= DEVMAP_LOCK_INITED;
5700Sstevel@tonic-gate 		}
5710Sstevel@tonic-gate 
5720Sstevel@tonic-gate 		if (callbackops->devmap_dup != NULL) {
5730Sstevel@tonic-gate 			int ret;
5740Sstevel@tonic-gate 
5750Sstevel@tonic-gate 			/*
5760Sstevel@tonic-gate 			 * Call the dup callback so that the driver can
5770Sstevel@tonic-gate 			 * duplicate its private data.
5780Sstevel@tonic-gate 			 */
5790Sstevel@tonic-gate 			ret = (*callbackops->devmap_dup)(dhp, dhp->dh_pvtp,
5800Sstevel@tonic-gate 				(devmap_cookie_t *)newdhp, &newdhp->dh_pvtp);
5810Sstevel@tonic-gate 
5820Sstevel@tonic-gate 			if (ret != 0) {
5830Sstevel@tonic-gate 				/*
5840Sstevel@tonic-gate 				 * We want to free up this segment as the driver
5850Sstevel@tonic-gate 				 * has indicated that we can't dup it.  But we
5860Sstevel@tonic-gate 				 * don't want to call the drivers, devmap_unmap,
5870Sstevel@tonic-gate 				 * callback function as the driver does not
5880Sstevel@tonic-gate 				 * think this segment exists. The caller of
5890Sstevel@tonic-gate 				 * devmap_dup will call seg_free on newseg
5900Sstevel@tonic-gate 				 * as it was the caller that allocated the
5910Sstevel@tonic-gate 				 * segment.
5920Sstevel@tonic-gate 				 */
5930Sstevel@tonic-gate 				DEBUGF(1, (CE_CONT, "devmap_handle_dup ERROR: "
5940Sstevel@tonic-gate 				    "newdhp %p dhp %p\n", (void *)newdhp,
5950Sstevel@tonic-gate 				    (void *)dhp));
5960Sstevel@tonic-gate 				callbackops->devmap_unmap = NULL;
5970Sstevel@tonic-gate 				return (ret);
5980Sstevel@tonic-gate 			}
5990Sstevel@tonic-gate 		}
6000Sstevel@tonic-gate 
6010Sstevel@tonic-gate 		dhp = dhp->dh_next;
6020Sstevel@tonic-gate 	}
6030Sstevel@tonic-gate 
6040Sstevel@tonic-gate 	return (0);
6050Sstevel@tonic-gate }
6060Sstevel@tonic-gate 
6070Sstevel@tonic-gate /*
6080Sstevel@tonic-gate  * Split a segment at addr for length len.
6090Sstevel@tonic-gate  */
6100Sstevel@tonic-gate /*ARGSUSED*/
6110Sstevel@tonic-gate static int
6120Sstevel@tonic-gate segdev_unmap(struct seg *seg, caddr_t addr, size_t len)
6130Sstevel@tonic-gate {
6140Sstevel@tonic-gate 	register struct segdev_data *sdp = (struct segdev_data *)seg->s_data;
6150Sstevel@tonic-gate 	register struct segdev_data *nsdp;
6160Sstevel@tonic-gate 	register struct seg *nseg;
6170Sstevel@tonic-gate 	register size_t	opages;		/* old segment size in pages */
6180Sstevel@tonic-gate 	register size_t	npages;		/* new segment size in pages */
6190Sstevel@tonic-gate 	register size_t	dpages;		/* pages being deleted (unmapped) */
6200Sstevel@tonic-gate 	register size_t	nbytes;
6210Sstevel@tonic-gate 	devmap_handle_t *dhp = (devmap_handle_t *)sdp->devmap_data;
6220Sstevel@tonic-gate 	devmap_handle_t *dhpp;
6230Sstevel@tonic-gate 	devmap_handle_t *newdhp;
6240Sstevel@tonic-gate 	struct devmap_callback_ctl *callbackops;
6250Sstevel@tonic-gate 	caddr_t nbase;
6260Sstevel@tonic-gate 	offset_t off;
6270Sstevel@tonic-gate 	ulong_t nsize;
6280Sstevel@tonic-gate 	size_t mlen, sz;
6290Sstevel@tonic-gate 
6300Sstevel@tonic-gate 	TRACE_4(TR_FAC_DEVMAP, TR_DEVMAP_UNMAP,
6310Sstevel@tonic-gate 	    "segdev_unmap:start dhp=%p, seg=%p addr=%p len=%lx",
6320Sstevel@tonic-gate 	    (void *)dhp, (void *)seg, (void *)addr, len);
6330Sstevel@tonic-gate 
6340Sstevel@tonic-gate 	DEBUGF(3, (CE_CONT, "segdev_unmap: dhp %p seg %p addr %p len %lx\n",
6350Sstevel@tonic-gate 	    (void *)dhp, (void *)seg, (void *)addr, len));
6360Sstevel@tonic-gate 
6370Sstevel@tonic-gate 	/*
6380Sstevel@tonic-gate 	 * Since the address space is "write" locked, we
6390Sstevel@tonic-gate 	 * don't need the segment lock to protect "segdev" data.
6400Sstevel@tonic-gate 	 */
6410Sstevel@tonic-gate 	ASSERT(seg->s_as && AS_WRITE_HELD(seg->s_as, &seg->s_as->a_lock));
6420Sstevel@tonic-gate 
6430Sstevel@tonic-gate 	if ((sz = sdp->softlockcnt) > 0) {
6440Sstevel@tonic-gate 		/*
6450Sstevel@tonic-gate 		 * Fail the unmap if pages are SOFTLOCKed through this mapping.
6460Sstevel@tonic-gate 		 * softlockcnt is protected from change by the as write lock.
6470Sstevel@tonic-gate 		 */
6480Sstevel@tonic-gate 		TRACE_1(TR_FAC_DEVMAP, TR_DEVMAP_UNMAP_CK1,
6490Sstevel@tonic-gate 		    "segdev_unmap:error softlockcnt = %ld", sz);
6500Sstevel@tonic-gate 		DEBUGF(1, (CE_CONT, "segdev_unmap: softlockcnt %ld\n", sz));
6510Sstevel@tonic-gate 		return (EAGAIN);
6520Sstevel@tonic-gate 	}
6530Sstevel@tonic-gate 
6540Sstevel@tonic-gate 	/*
6550Sstevel@tonic-gate 	 * Check for bad sizes
6560Sstevel@tonic-gate 	 */
6570Sstevel@tonic-gate 	if (addr < seg->s_base || addr + len > seg->s_base + seg->s_size ||
6580Sstevel@tonic-gate 	    (len & PAGEOFFSET) || ((uintptr_t)addr & PAGEOFFSET))
6590Sstevel@tonic-gate 		panic("segdev_unmap");
6600Sstevel@tonic-gate 
6610Sstevel@tonic-gate 	if (dhp != NULL) {
6620Sstevel@tonic-gate 		devmap_handle_t *tdhp;
6630Sstevel@tonic-gate 		/*
6640Sstevel@tonic-gate 		 * If large page size was used in hat_devload(),
6650Sstevel@tonic-gate 		 * the same page size must be used in hat_unload().
6660Sstevel@tonic-gate 		 */
6670Sstevel@tonic-gate 		dhpp = tdhp = devmap_find_handle(dhp, addr);
6680Sstevel@tonic-gate 		while (tdhp != NULL) {
6690Sstevel@tonic-gate 			if (tdhp->dh_flags & DEVMAP_FLAG_LARGE) {
6700Sstevel@tonic-gate 				break;
6710Sstevel@tonic-gate 			}
6720Sstevel@tonic-gate 			tdhp = tdhp->dh_next;
6730Sstevel@tonic-gate 		}
6740Sstevel@tonic-gate 		if (tdhp != NULL) {	/* found a dhp using large pages */
6750Sstevel@tonic-gate 			size_t slen = len;
6760Sstevel@tonic-gate 			size_t mlen;
6770Sstevel@tonic-gate 			size_t soff;
6780Sstevel@tonic-gate 
6790Sstevel@tonic-gate 			soff = (ulong_t)(addr - dhpp->dh_uvaddr);
6800Sstevel@tonic-gate 			while (slen != 0) {
6810Sstevel@tonic-gate 				mlen = MIN(slen, (dhpp->dh_len - soff));
6820Sstevel@tonic-gate 				hat_unload(seg->s_as->a_hat, dhpp->dh_uvaddr,
6830Sstevel@tonic-gate 					dhpp->dh_len, HAT_UNLOAD_UNMAP);
6840Sstevel@tonic-gate 				dhpp = dhpp->dh_next;
6850Sstevel@tonic-gate 				ASSERT(slen >= mlen);
6860Sstevel@tonic-gate 				slen -= mlen;
6870Sstevel@tonic-gate 				soff = 0;
6880Sstevel@tonic-gate 			}
6890Sstevel@tonic-gate 		} else
6900Sstevel@tonic-gate 			hat_unload(seg->s_as->a_hat, addr, len,
6910Sstevel@tonic-gate 				HAT_UNLOAD_UNMAP);
6920Sstevel@tonic-gate 	} else {
6930Sstevel@tonic-gate 		/*
6940Sstevel@tonic-gate 		 * Unload any hardware translations in the range
6950Sstevel@tonic-gate 		 * to be taken out.
6960Sstevel@tonic-gate 		 */
6970Sstevel@tonic-gate 		hat_unload(seg->s_as->a_hat, addr, len, HAT_UNLOAD_UNMAP);
6980Sstevel@tonic-gate 	}
6990Sstevel@tonic-gate 
7000Sstevel@tonic-gate 	/*
7010Sstevel@tonic-gate 	 * get the user offset which will used in the driver callbacks
7020Sstevel@tonic-gate 	 */
7030Sstevel@tonic-gate 	off = sdp->offset + (offset_t)(addr - seg->s_base);
7040Sstevel@tonic-gate 
7050Sstevel@tonic-gate 	/*
7060Sstevel@tonic-gate 	 * Inform the vnode of the unmapping.
7070Sstevel@tonic-gate 	 */
7080Sstevel@tonic-gate 	ASSERT(sdp->vp != NULL);
7090Sstevel@tonic-gate 	(void) VOP_DELMAP(VTOCVP(sdp->vp), off, seg->s_as, addr, len,
7100Sstevel@tonic-gate 		sdp->prot, sdp->maxprot, sdp->type, CRED());
7110Sstevel@tonic-gate 
7120Sstevel@tonic-gate 	/*
7130Sstevel@tonic-gate 	 * Check for entire segment
7140Sstevel@tonic-gate 	 */
7150Sstevel@tonic-gate 	if (addr == seg->s_base && len == seg->s_size) {
7160Sstevel@tonic-gate 		seg_free(seg);
7170Sstevel@tonic-gate 		return (0);
7180Sstevel@tonic-gate 	}
7190Sstevel@tonic-gate 
7200Sstevel@tonic-gate 	opages = seg_pages(seg);
7210Sstevel@tonic-gate 	dpages = btop(len);
7220Sstevel@tonic-gate 	npages = opages - dpages;
7230Sstevel@tonic-gate 
7240Sstevel@tonic-gate 	/*
7250Sstevel@tonic-gate 	 * Check for beginning of segment
7260Sstevel@tonic-gate 	 */
7270Sstevel@tonic-gate 	if (addr == seg->s_base) {
7280Sstevel@tonic-gate 		if (sdp->vpage != NULL) {
7290Sstevel@tonic-gate 			register struct vpage *ovpage;
7300Sstevel@tonic-gate 
7310Sstevel@tonic-gate 			ovpage = sdp->vpage;	/* keep pointer to vpage */
7320Sstevel@tonic-gate 
7330Sstevel@tonic-gate 			nbytes = vpgtob(npages);
7340Sstevel@tonic-gate 			sdp->vpage = kmem_alloc(nbytes, KM_SLEEP);
7350Sstevel@tonic-gate 			bcopy(&ovpage[dpages], sdp->vpage, nbytes);
7360Sstevel@tonic-gate 
7370Sstevel@tonic-gate 			/* free up old vpage */
7380Sstevel@tonic-gate 			kmem_free(ovpage, vpgtob(opages));
7390Sstevel@tonic-gate 		}
7400Sstevel@tonic-gate 
7410Sstevel@tonic-gate 		/*
7420Sstevel@tonic-gate 		 * free devmap handles from the beginning of the mapping.
7430Sstevel@tonic-gate 		 */
7440Sstevel@tonic-gate 		if (dhp != NULL)
7450Sstevel@tonic-gate 			devmap_handle_unmap_head(dhp, len);
7460Sstevel@tonic-gate 
7470Sstevel@tonic-gate 		sdp->offset += (offset_t)len;
7480Sstevel@tonic-gate 
7490Sstevel@tonic-gate 		seg->s_base += len;
7500Sstevel@tonic-gate 		seg->s_size -= len;
7510Sstevel@tonic-gate 
7520Sstevel@tonic-gate 		return (0);
7530Sstevel@tonic-gate 	}
7540Sstevel@tonic-gate 
7550Sstevel@tonic-gate 	/*
7560Sstevel@tonic-gate 	 * Check for end of segment
7570Sstevel@tonic-gate 	 */
7580Sstevel@tonic-gate 	if (addr + len == seg->s_base + seg->s_size) {
7590Sstevel@tonic-gate 		if (sdp->vpage != NULL) {
7600Sstevel@tonic-gate 			register struct vpage *ovpage;
7610Sstevel@tonic-gate 
7620Sstevel@tonic-gate 			ovpage = sdp->vpage;	/* keep pointer to vpage */
7630Sstevel@tonic-gate 
7640Sstevel@tonic-gate 			nbytes = vpgtob(npages);
7650Sstevel@tonic-gate 			sdp->vpage = kmem_alloc(nbytes, KM_SLEEP);
7660Sstevel@tonic-gate 			bcopy(ovpage, sdp->vpage, nbytes);
7670Sstevel@tonic-gate 
7680Sstevel@tonic-gate 			/* free up old vpage */
7690Sstevel@tonic-gate 			kmem_free(ovpage, vpgtob(opages));
7700Sstevel@tonic-gate 		}
7710Sstevel@tonic-gate 		seg->s_size -= len;
7720Sstevel@tonic-gate 
7730Sstevel@tonic-gate 		/*
7740Sstevel@tonic-gate 		 * free devmap handles from addr to the end of the mapping.
7750Sstevel@tonic-gate 		 */
7760Sstevel@tonic-gate 		if (dhp != NULL)
7770Sstevel@tonic-gate 			devmap_handle_unmap_tail(dhp, addr);
7780Sstevel@tonic-gate 
7790Sstevel@tonic-gate 		return (0);
7800Sstevel@tonic-gate 	}
7810Sstevel@tonic-gate 
7820Sstevel@tonic-gate 	/*
7830Sstevel@tonic-gate 	 * The section to go is in the middle of the segment,
7840Sstevel@tonic-gate 	 * have to make it into two segments.  nseg is made for
7850Sstevel@tonic-gate 	 * the high end while seg is cut down at the low end.
7860Sstevel@tonic-gate 	 */
7870Sstevel@tonic-gate 	nbase = addr + len;				/* new seg base */
7880Sstevel@tonic-gate 	nsize = (seg->s_base + seg->s_size) - nbase;	/* new seg size */
7890Sstevel@tonic-gate 	seg->s_size = addr - seg->s_base;		/* shrink old seg */
7900Sstevel@tonic-gate 	nseg = seg_alloc(seg->s_as, nbase, nsize);
7910Sstevel@tonic-gate 	if (nseg == NULL)
7920Sstevel@tonic-gate 		panic("segdev_unmap seg_alloc");
7930Sstevel@tonic-gate 
7940Sstevel@tonic-gate 	TRACE_2(TR_FAC_DEVMAP, TR_DEVMAP_UNMAP_CK2,
7950Sstevel@tonic-gate 	    "segdev_unmap: seg=%p nseg=%p", (void *)seg, (void *)nseg);
7960Sstevel@tonic-gate 	DEBUGF(3, (CE_CONT, "segdev_unmap: segdev_dup seg %p nseg %p\n",
7970Sstevel@tonic-gate 	    (void *)seg, (void *)nseg));
7980Sstevel@tonic-gate 	nsdp = sdp_alloc();
7990Sstevel@tonic-gate 
8000Sstevel@tonic-gate 	nseg->s_ops = seg->s_ops;
8010Sstevel@tonic-gate 	nseg->s_data = (void *)nsdp;
8020Sstevel@tonic-gate 
8030Sstevel@tonic-gate 	VN_HOLD(sdp->vp);
8040Sstevel@tonic-gate 	nsdp->mapfunc = sdp->mapfunc;
8050Sstevel@tonic-gate 	nsdp->offset = sdp->offset + (offset_t)(nseg->s_base - seg->s_base);
8060Sstevel@tonic-gate 	nsdp->vp 	= sdp->vp;
8070Sstevel@tonic-gate 	nsdp->pageprot = sdp->pageprot;
8080Sstevel@tonic-gate 	nsdp->prot	= sdp->prot;
8090Sstevel@tonic-gate 	nsdp->maxprot = sdp->maxprot;
8100Sstevel@tonic-gate 	nsdp->type = sdp->type;
8110Sstevel@tonic-gate 	nsdp->hat_attr = sdp->hat_attr;
8120Sstevel@tonic-gate 	nsdp->hat_flags = sdp->hat_flags;
8130Sstevel@tonic-gate 	nsdp->softlockcnt = 0;
8140Sstevel@tonic-gate 
8150Sstevel@tonic-gate 	/*
8160Sstevel@tonic-gate 	 * Initialize per page data if the segment we are
8170Sstevel@tonic-gate 	 * dup'ing has per page information.
8180Sstevel@tonic-gate 	 */
8190Sstevel@tonic-gate 	if (sdp->vpage != NULL) {
8200Sstevel@tonic-gate 		/* need to split vpage into two arrays */
8210Sstevel@tonic-gate 		register size_t nnbytes;
8220Sstevel@tonic-gate 		register size_t nnpages;
8230Sstevel@tonic-gate 		register struct vpage *ovpage;
8240Sstevel@tonic-gate 
8250Sstevel@tonic-gate 		ovpage = sdp->vpage;		/* keep pointer to vpage */
8260Sstevel@tonic-gate 
8270Sstevel@tonic-gate 		npages = seg_pages(seg);	/* seg has shrunk */
8280Sstevel@tonic-gate 		nbytes = vpgtob(npages);
8290Sstevel@tonic-gate 		nnpages = seg_pages(nseg);
8300Sstevel@tonic-gate 		nnbytes = vpgtob(nnpages);
8310Sstevel@tonic-gate 
8320Sstevel@tonic-gate 		sdp->vpage = kmem_alloc(nbytes, KM_SLEEP);
8330Sstevel@tonic-gate 		bcopy(ovpage, sdp->vpage, nbytes);
8340Sstevel@tonic-gate 
8350Sstevel@tonic-gate 		nsdp->vpage = kmem_alloc(nnbytes, KM_SLEEP);
8360Sstevel@tonic-gate 		bcopy(&ovpage[npages + dpages], nsdp->vpage, nnbytes);
8370Sstevel@tonic-gate 
8380Sstevel@tonic-gate 		/* free up old vpage */
8390Sstevel@tonic-gate 		kmem_free(ovpage, vpgtob(opages));
8400Sstevel@tonic-gate 	} else
8410Sstevel@tonic-gate 		nsdp->vpage = NULL;
8420Sstevel@tonic-gate 
8430Sstevel@tonic-gate 	/*
8440Sstevel@tonic-gate 	 * unmap dhps.
8450Sstevel@tonic-gate 	 */
8460Sstevel@tonic-gate 	if (dhp == NULL) {
8470Sstevel@tonic-gate 		nsdp->devmap_data = NULL;
8480Sstevel@tonic-gate 		return (0);
8490Sstevel@tonic-gate 	}
8500Sstevel@tonic-gate 	while (dhp != NULL) {
8510Sstevel@tonic-gate 		callbackops = &dhp->dh_callbackops;
8520Sstevel@tonic-gate 		TRACE_2(TR_FAC_DEVMAP, TR_DEVMAP_UNMAP_CK3,
8530Sstevel@tonic-gate 		    "segdev_unmap: dhp=%p addr=%p", dhp, addr);
8540Sstevel@tonic-gate 		DEBUGF(3, (CE_CONT, "unmap: dhp %p addr %p uvaddr %p len %lx\n",
8550Sstevel@tonic-gate 		    (void *)dhp, (void *)addr,
8560Sstevel@tonic-gate 		    (void *)dhp->dh_uvaddr, dhp->dh_len));
8570Sstevel@tonic-gate 
8580Sstevel@tonic-gate 		if (addr == (dhp->dh_uvaddr + dhp->dh_len)) {
8590Sstevel@tonic-gate 			dhpp = dhp->dh_next;
8600Sstevel@tonic-gate 			dhp->dh_next = NULL;
8610Sstevel@tonic-gate 			dhp = dhpp;
8620Sstevel@tonic-gate 		} else if (addr > (dhp->dh_uvaddr + dhp->dh_len)) {
8630Sstevel@tonic-gate 			dhp = dhp->dh_next;
8640Sstevel@tonic-gate 		} else if (addr > dhp->dh_uvaddr &&
8650Sstevel@tonic-gate 			(addr + len) < (dhp->dh_uvaddr + dhp->dh_len)) {
8660Sstevel@tonic-gate 			/*
8670Sstevel@tonic-gate 			 * <addr, addr+len> is enclosed by dhp.
8680Sstevel@tonic-gate 			 * create a newdhp that begins at addr+len and
8690Sstevel@tonic-gate 			 * ends at dhp->dh_uvaddr+dhp->dh_len.
8700Sstevel@tonic-gate 			 */
8710Sstevel@tonic-gate 			newdhp = kmem_alloc(sizeof (devmap_handle_t), KM_SLEEP);
8720Sstevel@tonic-gate 			HOLD_DHP_LOCK(dhp);
8730Sstevel@tonic-gate 			bcopy(dhp, newdhp, sizeof (devmap_handle_t));
8740Sstevel@tonic-gate 			RELE_DHP_LOCK(dhp);
8750Sstevel@tonic-gate 			newdhp->dh_seg = nseg;
8760Sstevel@tonic-gate 			newdhp->dh_next = dhp->dh_next;
8770Sstevel@tonic-gate 			if (dhp->dh_softlock != NULL)
8780Sstevel@tonic-gate 				newdhp->dh_softlock = devmap_softlock_init(
8790Sstevel@tonic-gate 					newdhp->dh_dev,
8800Sstevel@tonic-gate 					(ulong_t)callbackops->devmap_access);
8810Sstevel@tonic-gate 			if (dhp->dh_ctx != NULL)
8820Sstevel@tonic-gate 				newdhp->dh_ctx = devmap_ctxinit(newdhp->dh_dev,
8830Sstevel@tonic-gate 					(ulong_t)callbackops->devmap_access);
8840Sstevel@tonic-gate 			if (newdhp->dh_flags & DEVMAP_LOCK_INITED) {
8850Sstevel@tonic-gate 				mutex_init(&newdhp->dh_lock,
8860Sstevel@tonic-gate 				    NULL, MUTEX_DEFAULT, NULL);
8870Sstevel@tonic-gate 			}
8880Sstevel@tonic-gate 			if (callbackops->devmap_unmap != NULL)
8890Sstevel@tonic-gate 				(*callbackops->devmap_unmap)(dhp, dhp->dh_pvtp,
8900Sstevel@tonic-gate 					off, len, dhp, &dhp->dh_pvtp,
8910Sstevel@tonic-gate 					newdhp, &newdhp->dh_pvtp);
8920Sstevel@tonic-gate 			mlen = len + (addr - dhp->dh_uvaddr);
8930Sstevel@tonic-gate 			devmap_handle_reduce_len(newdhp, mlen);
8940Sstevel@tonic-gate 			nsdp->devmap_data = newdhp;
8950Sstevel@tonic-gate 			/* XX Changing len should recalculate LARGE flag */
8960Sstevel@tonic-gate 			dhp->dh_len = addr - dhp->dh_uvaddr;
8970Sstevel@tonic-gate 			dhpp = dhp->dh_next;
8980Sstevel@tonic-gate 			dhp->dh_next = NULL;
8990Sstevel@tonic-gate 			dhp = dhpp;
9000Sstevel@tonic-gate 		} else if ((addr > dhp->dh_uvaddr) &&
9010Sstevel@tonic-gate 			    ((addr + len) >= (dhp->dh_uvaddr + dhp->dh_len))) {
9020Sstevel@tonic-gate 			mlen = dhp->dh_len + dhp->dh_uvaddr - addr;
9030Sstevel@tonic-gate 			/*
9040Sstevel@tonic-gate 			 * <addr, addr+len> spans over dhps.
9050Sstevel@tonic-gate 			 */
9060Sstevel@tonic-gate 			if (callbackops->devmap_unmap != NULL)
9070Sstevel@tonic-gate 				(*callbackops->devmap_unmap)(dhp, dhp->dh_pvtp,
9080Sstevel@tonic-gate 					off, mlen, (devmap_cookie_t *)dhp,
9090Sstevel@tonic-gate 					&dhp->dh_pvtp, NULL, NULL);
9100Sstevel@tonic-gate 			/* XX Changing len should recalculate LARGE flag */
9110Sstevel@tonic-gate 			dhp->dh_len = addr - dhp->dh_uvaddr;
9120Sstevel@tonic-gate 			dhpp = dhp->dh_next;
9130Sstevel@tonic-gate 			dhp->dh_next = NULL;
9140Sstevel@tonic-gate 			dhp = dhpp;
9150Sstevel@tonic-gate 			nsdp->devmap_data = dhp;
9160Sstevel@tonic-gate 		} else if ((addr + len) >= (dhp->dh_uvaddr + dhp->dh_len)) {
9170Sstevel@tonic-gate 			/*
9180Sstevel@tonic-gate 			 * dhp is enclosed by <addr, addr+len>.
9190Sstevel@tonic-gate 			 */
9200Sstevel@tonic-gate 			dhp->dh_seg = nseg;
9210Sstevel@tonic-gate 			nsdp->devmap_data = dhp;
9220Sstevel@tonic-gate 			dhp = devmap_handle_unmap(dhp);
9230Sstevel@tonic-gate 			nsdp->devmap_data = dhp; /* XX redundant? */
9240Sstevel@tonic-gate 		} else if (((addr + len) > dhp->dh_uvaddr) &&
9250Sstevel@tonic-gate 			    ((addr + len) < (dhp->dh_uvaddr + dhp->dh_len))) {
9260Sstevel@tonic-gate 			mlen = addr + len - dhp->dh_uvaddr;
9270Sstevel@tonic-gate 			if (callbackops->devmap_unmap != NULL)
9280Sstevel@tonic-gate 				(*callbackops->devmap_unmap)(dhp, dhp->dh_pvtp,
9290Sstevel@tonic-gate 					dhp->dh_uoff, mlen, NULL,
9300Sstevel@tonic-gate 					NULL, dhp, &dhp->dh_pvtp);
9310Sstevel@tonic-gate 			devmap_handle_reduce_len(dhp, mlen);
9320Sstevel@tonic-gate 			nsdp->devmap_data = dhp;
9330Sstevel@tonic-gate 			dhp->dh_seg = nseg;
9340Sstevel@tonic-gate 			dhp = dhp->dh_next;
9350Sstevel@tonic-gate 		} else {
9360Sstevel@tonic-gate 			dhp->dh_seg = nseg;
9370Sstevel@tonic-gate 			dhp = dhp->dh_next;
9380Sstevel@tonic-gate 		}
9390Sstevel@tonic-gate 	}
9400Sstevel@tonic-gate 	return (0);
9410Sstevel@tonic-gate }
9420Sstevel@tonic-gate 
9430Sstevel@tonic-gate /*
9440Sstevel@tonic-gate  * Utility function handles reducing the length of a devmap handle during unmap
9450Sstevel@tonic-gate  * Note that is only used for unmapping the front portion of the handler,
9460Sstevel@tonic-gate  * i.e., we are bumping up the offset/pfn etc up by len
9470Sstevel@tonic-gate  * Do not use if reducing length at the tail.
9480Sstevel@tonic-gate  */
9490Sstevel@tonic-gate static void
9500Sstevel@tonic-gate devmap_handle_reduce_len(devmap_handle_t *dhp, size_t len)
9510Sstevel@tonic-gate {
9520Sstevel@tonic-gate 	struct ddi_umem_cookie *cp;
9530Sstevel@tonic-gate 	struct devmap_pmem_cookie *pcp;
9540Sstevel@tonic-gate 	/*
9550Sstevel@tonic-gate 	 * adjust devmap handle fields
9560Sstevel@tonic-gate 	 */
9570Sstevel@tonic-gate 	ASSERT(len < dhp->dh_len);
9580Sstevel@tonic-gate 
9590Sstevel@tonic-gate 	/* Make sure only page-aligned changes are done */
9600Sstevel@tonic-gate 	ASSERT((len & PAGEOFFSET) == 0);
9610Sstevel@tonic-gate 
9620Sstevel@tonic-gate 	dhp->dh_len -= len;
9630Sstevel@tonic-gate 	dhp->dh_uoff += (offset_t)len;
9640Sstevel@tonic-gate 	dhp->dh_roff += (offset_t)len;
9650Sstevel@tonic-gate 	dhp->dh_uvaddr += len;
9660Sstevel@tonic-gate 	/* Need to grab dhp lock if REMAP */
9670Sstevel@tonic-gate 	HOLD_DHP_LOCK(dhp);
9680Sstevel@tonic-gate 	cp = dhp->dh_cookie;
9690Sstevel@tonic-gate 	if (!(dhp->dh_flags & DEVMAP_MAPPING_INVALID)) {
9700Sstevel@tonic-gate 		if (cookie_is_devmem(cp)) {
9710Sstevel@tonic-gate 			dhp->dh_pfn += btop(len);
9720Sstevel@tonic-gate 		} else if (cookie_is_pmem(cp)) {
9730Sstevel@tonic-gate 			pcp = (struct devmap_pmem_cookie *)dhp->dh_pcookie;
9740Sstevel@tonic-gate 			ASSERT((dhp->dh_roff & PAGEOFFSET) == 0 &&
9750Sstevel@tonic-gate 				dhp->dh_roff < ptob(pcp->dp_npages));
9760Sstevel@tonic-gate 		} else {
9770Sstevel@tonic-gate 			ASSERT(dhp->dh_roff < cp->size);
9780Sstevel@tonic-gate 			ASSERT(dhp->dh_cvaddr >= cp->cvaddr &&
9790Sstevel@tonic-gate 				dhp->dh_cvaddr < (cp->cvaddr + cp->size));
9800Sstevel@tonic-gate 			ASSERT((dhp->dh_cvaddr + len) <=
9810Sstevel@tonic-gate 				(cp->cvaddr + cp->size));
9820Sstevel@tonic-gate 
9830Sstevel@tonic-gate 			dhp->dh_cvaddr += len;
9840Sstevel@tonic-gate 		}
9850Sstevel@tonic-gate 	}
9860Sstevel@tonic-gate 	/* XXX - Should recalculate the DEVMAP_FLAG_LARGE after changes */
9870Sstevel@tonic-gate 	RELE_DHP_LOCK(dhp);
9880Sstevel@tonic-gate }
9890Sstevel@tonic-gate 
9900Sstevel@tonic-gate /*
9910Sstevel@tonic-gate  * Free devmap handle, dhp.
9920Sstevel@tonic-gate  * Return the next devmap handle on the linked list.
9930Sstevel@tonic-gate  */
9940Sstevel@tonic-gate static devmap_handle_t *
9950Sstevel@tonic-gate devmap_handle_unmap(devmap_handle_t *dhp)
9960Sstevel@tonic-gate {
9970Sstevel@tonic-gate 	struct devmap_callback_ctl *callbackops = &dhp->dh_callbackops;
9980Sstevel@tonic-gate 	struct segdev_data *sdp = (struct segdev_data *)dhp->dh_seg->s_data;
9990Sstevel@tonic-gate 	devmap_handle_t *dhpp = (devmap_handle_t *)sdp->devmap_data;
10000Sstevel@tonic-gate 
10010Sstevel@tonic-gate 	ASSERT(dhp != NULL);
10020Sstevel@tonic-gate 
10030Sstevel@tonic-gate 	/*
10040Sstevel@tonic-gate 	 * before we free up dhp, call the driver's devmap_unmap entry point
10050Sstevel@tonic-gate 	 * to free resources allocated for this dhp.
10060Sstevel@tonic-gate 	 */
10070Sstevel@tonic-gate 	if (callbackops->devmap_unmap != NULL) {
10080Sstevel@tonic-gate 		(*callbackops->devmap_unmap)(dhp, dhp->dh_pvtp, dhp->dh_uoff,
10090Sstevel@tonic-gate 			dhp->dh_len, NULL, NULL, NULL, NULL);
10100Sstevel@tonic-gate 	}
10110Sstevel@tonic-gate 
10120Sstevel@tonic-gate 	if (dhpp == dhp) {	/* releasing first dhp, change sdp data */
10130Sstevel@tonic-gate 		sdp->devmap_data = dhp->dh_next;
10140Sstevel@tonic-gate 	} else {
10150Sstevel@tonic-gate 		while (dhpp->dh_next != dhp) {
10160Sstevel@tonic-gate 			dhpp = dhpp->dh_next;
10170Sstevel@tonic-gate 		}
10180Sstevel@tonic-gate 		dhpp->dh_next = dhp->dh_next;
10190Sstevel@tonic-gate 	}
10200Sstevel@tonic-gate 	dhpp = dhp->dh_next;	/* return value is next dhp in chain */
10210Sstevel@tonic-gate 
10220Sstevel@tonic-gate 	if (dhp->dh_softlock != NULL)
10230Sstevel@tonic-gate 		devmap_softlock_rele(dhp);
10240Sstevel@tonic-gate 
10250Sstevel@tonic-gate 	if (dhp->dh_ctx != NULL)
10260Sstevel@tonic-gate 		devmap_ctx_rele(dhp);
10270Sstevel@tonic-gate 
10280Sstevel@tonic-gate 	if (dhp->dh_flags & DEVMAP_LOCK_INITED) {
10290Sstevel@tonic-gate 		mutex_destroy(&dhp->dh_lock);
10300Sstevel@tonic-gate 	}
10310Sstevel@tonic-gate 	kmem_free(dhp, sizeof (devmap_handle_t));
10320Sstevel@tonic-gate 
10330Sstevel@tonic-gate 	return (dhpp);
10340Sstevel@tonic-gate }
10350Sstevel@tonic-gate 
10360Sstevel@tonic-gate /*
10370Sstevel@tonic-gate  * Free complete devmap handles from dhp for len bytes
10380Sstevel@tonic-gate  * dhp can be either the first handle or a subsequent handle
10390Sstevel@tonic-gate  */
10400Sstevel@tonic-gate static void
10410Sstevel@tonic-gate devmap_handle_unmap_head(devmap_handle_t *dhp, size_t len)
10420Sstevel@tonic-gate {
10430Sstevel@tonic-gate 	struct devmap_callback_ctl *callbackops;
10440Sstevel@tonic-gate 
10450Sstevel@tonic-gate 	/*
10460Sstevel@tonic-gate 	 * free the devmap handles covered by len.
10470Sstevel@tonic-gate 	 */
10480Sstevel@tonic-gate 	while (len >= dhp->dh_len) {
10490Sstevel@tonic-gate 		len -= dhp->dh_len;
10500Sstevel@tonic-gate 		dhp = devmap_handle_unmap(dhp);
10510Sstevel@tonic-gate 	}
10520Sstevel@tonic-gate 	if (len != 0) {	/* partial unmap at head of first remaining dhp */
10530Sstevel@tonic-gate 		callbackops = &dhp->dh_callbackops;
10540Sstevel@tonic-gate 
10550Sstevel@tonic-gate 		/*
10560Sstevel@tonic-gate 		 * Call the unmap callback so the drivers can make
10570Sstevel@tonic-gate 		 * adjustment on its private data.
10580Sstevel@tonic-gate 		 */
10590Sstevel@tonic-gate 		if (callbackops->devmap_unmap != NULL)
10600Sstevel@tonic-gate 			(*callbackops->devmap_unmap)(dhp, dhp->dh_pvtp,
10610Sstevel@tonic-gate 			    dhp->dh_uoff, len, NULL, NULL, dhp, &dhp->dh_pvtp);
10620Sstevel@tonic-gate 		devmap_handle_reduce_len(dhp, len);
10630Sstevel@tonic-gate 	}
10640Sstevel@tonic-gate }
10650Sstevel@tonic-gate 
10660Sstevel@tonic-gate /*
10670Sstevel@tonic-gate  * Free devmap handles to truncate  the mapping after addr
10680Sstevel@tonic-gate  * RFE: Simpler to pass in dhp pointing at correct dhp (avoid find again)
10690Sstevel@tonic-gate  *	Also could then use the routine in middle unmap case too
10700Sstevel@tonic-gate  */
10710Sstevel@tonic-gate static void
10720Sstevel@tonic-gate devmap_handle_unmap_tail(devmap_handle_t *dhp, caddr_t addr)
10730Sstevel@tonic-gate {
10740Sstevel@tonic-gate 	register struct seg *seg = dhp->dh_seg;
10750Sstevel@tonic-gate 	register struct segdev_data *sdp = (struct segdev_data *)seg->s_data;
10760Sstevel@tonic-gate 	register devmap_handle_t *dhph = (devmap_handle_t *)sdp->devmap_data;
10770Sstevel@tonic-gate 	struct devmap_callback_ctl *callbackops;
10780Sstevel@tonic-gate 	register devmap_handle_t *dhpp;
10790Sstevel@tonic-gate 	size_t maplen;
10800Sstevel@tonic-gate 	ulong_t off;
10810Sstevel@tonic-gate 	size_t len;
10820Sstevel@tonic-gate 
10830Sstevel@tonic-gate 	maplen = (size_t)(addr - dhp->dh_uvaddr);
10840Sstevel@tonic-gate 	dhph = devmap_find_handle(dhph, addr);
10850Sstevel@tonic-gate 
10860Sstevel@tonic-gate 	while (dhph != NULL) {
10870Sstevel@tonic-gate 		if (maplen == 0) {
10880Sstevel@tonic-gate 			dhph =  devmap_handle_unmap(dhph);
10890Sstevel@tonic-gate 		} else {
10900Sstevel@tonic-gate 			callbackops = &dhph->dh_callbackops;
10910Sstevel@tonic-gate 			len = dhph->dh_len - maplen;
10920Sstevel@tonic-gate 			off = (ulong_t)sdp->offset + (addr - seg->s_base);
10930Sstevel@tonic-gate 			/*
10940Sstevel@tonic-gate 			 * Call the unmap callback so the driver
10950Sstevel@tonic-gate 			 * can make adjustments on its private data.
10960Sstevel@tonic-gate 			 */
10970Sstevel@tonic-gate 			if (callbackops->devmap_unmap != NULL)
10980Sstevel@tonic-gate 				(*callbackops->devmap_unmap)(dhph,
10990Sstevel@tonic-gate 					dhph->dh_pvtp, off, len,
11000Sstevel@tonic-gate 					(devmap_cookie_t *)dhph,
11010Sstevel@tonic-gate 					&dhph->dh_pvtp, NULL, NULL);
11020Sstevel@tonic-gate 			/* XXX Reducing len needs to recalculate LARGE flag */
11030Sstevel@tonic-gate 			dhph->dh_len = maplen;
11040Sstevel@tonic-gate 			maplen = 0;
11050Sstevel@tonic-gate 			dhpp = dhph->dh_next;
11060Sstevel@tonic-gate 			dhph->dh_next = NULL;
11070Sstevel@tonic-gate 			dhph = dhpp;
11080Sstevel@tonic-gate 		}
11090Sstevel@tonic-gate 	} /* end while */
11100Sstevel@tonic-gate }
11110Sstevel@tonic-gate 
11120Sstevel@tonic-gate /*
11130Sstevel@tonic-gate  * Free a segment.
11140Sstevel@tonic-gate  */
11150Sstevel@tonic-gate static void
11160Sstevel@tonic-gate segdev_free(struct seg *seg)
11170Sstevel@tonic-gate {
11180Sstevel@tonic-gate 	register struct segdev_data *sdp = (struct segdev_data *)seg->s_data;
11190Sstevel@tonic-gate 	devmap_handle_t *dhp = (devmap_handle_t *)sdp->devmap_data;
11200Sstevel@tonic-gate 
11210Sstevel@tonic-gate 	TRACE_2(TR_FAC_DEVMAP, TR_DEVMAP_FREE,
11220Sstevel@tonic-gate 	    "segdev_free: dhp=%p seg=%p", (void *)dhp, (void *)seg);
11230Sstevel@tonic-gate 	DEBUGF(3, (CE_CONT, "segdev_free: dhp %p seg %p\n",
11240Sstevel@tonic-gate 	    (void *)dhp, (void *)seg));
11250Sstevel@tonic-gate 
11260Sstevel@tonic-gate 	/*
11270Sstevel@tonic-gate 	 * Since the address space is "write" locked, we
11280Sstevel@tonic-gate 	 * don't need the segment lock to protect "segdev" data.
11290Sstevel@tonic-gate 	 */
11300Sstevel@tonic-gate 	ASSERT(seg->s_as && AS_WRITE_HELD(seg->s_as, &seg->s_as->a_lock));
11310Sstevel@tonic-gate 
11320Sstevel@tonic-gate 	while (dhp != NULL)
11330Sstevel@tonic-gate 		dhp = devmap_handle_unmap(dhp);
11340Sstevel@tonic-gate 
11350Sstevel@tonic-gate 	VN_RELE(sdp->vp);
11360Sstevel@tonic-gate 	if (sdp->vpage != NULL)
11370Sstevel@tonic-gate 		kmem_free(sdp->vpage, vpgtob(seg_pages(seg)));
11380Sstevel@tonic-gate 
11390Sstevel@tonic-gate 	mutex_destroy(&sdp->lock);
11400Sstevel@tonic-gate 	kmem_free(sdp, sizeof (*sdp));
11410Sstevel@tonic-gate }
11420Sstevel@tonic-gate 
11430Sstevel@tonic-gate static void
11440Sstevel@tonic-gate free_devmap_handle(devmap_handle_t *dhp)
11450Sstevel@tonic-gate {
11460Sstevel@tonic-gate 	register devmap_handle_t *dhpp;
11470Sstevel@tonic-gate 
11480Sstevel@tonic-gate 	/*
11490Sstevel@tonic-gate 	 * free up devmap handle
11500Sstevel@tonic-gate 	 */
11510Sstevel@tonic-gate 	while (dhp != NULL) {
11520Sstevel@tonic-gate 		dhpp = dhp->dh_next;
11530Sstevel@tonic-gate 		if (dhp->dh_flags & DEVMAP_LOCK_INITED) {
11540Sstevel@tonic-gate 			mutex_destroy(&dhp->dh_lock);
11550Sstevel@tonic-gate 		}
11560Sstevel@tonic-gate 
11570Sstevel@tonic-gate 		if (dhp->dh_softlock != NULL)
11580Sstevel@tonic-gate 			devmap_softlock_rele(dhp);
11590Sstevel@tonic-gate 
11600Sstevel@tonic-gate 		if (dhp->dh_ctx != NULL)
11610Sstevel@tonic-gate 			devmap_ctx_rele(dhp);
11620Sstevel@tonic-gate 
11630Sstevel@tonic-gate 		kmem_free(dhp, sizeof (devmap_handle_t));
11640Sstevel@tonic-gate 		dhp = dhpp;
11650Sstevel@tonic-gate 	}
11660Sstevel@tonic-gate }
11670Sstevel@tonic-gate 
11680Sstevel@tonic-gate /*
11690Sstevel@tonic-gate  * routines to lock and unlock underlying segkp segment for
11700Sstevel@tonic-gate  * KMEM_PAGEABLE type cookies.
11710Sstevel@tonic-gate  * segkp only allows a single pending F_SOFTLOCK
11720Sstevel@tonic-gate  * we keep track of number of locks in the cookie so we can
11730Sstevel@tonic-gate  * have multiple pending faults and manage the calls to segkp.
11740Sstevel@tonic-gate  * RFE: if segkp supports either pagelock or can support multiple
11750Sstevel@tonic-gate  * calls to F_SOFTLOCK, then these routines can go away.
11760Sstevel@tonic-gate  *	If pagelock, segdev_faultpage can fault on a page by page basis
11770Sstevel@tonic-gate  *		and simplifies the code quite a bit.
11780Sstevel@tonic-gate  *	if multiple calls allowed but not partial ranges, then need for
11790Sstevel@tonic-gate  *	cookie->lock and locked count goes away, code can call as_fault directly
11800Sstevel@tonic-gate  */
11810Sstevel@tonic-gate static faultcode_t
11820Sstevel@tonic-gate acquire_kpmem_lock(struct ddi_umem_cookie *cookie, size_t npages)
11830Sstevel@tonic-gate {
11840Sstevel@tonic-gate 	int err = 0;
11850Sstevel@tonic-gate 	ASSERT(cookie_is_kpmem(cookie));
11860Sstevel@tonic-gate 	/*
11870Sstevel@tonic-gate 	 * Fault in pages in segkp with F_SOFTLOCK.
11880Sstevel@tonic-gate 	 * We want to hold the lock until all pages have been loaded.
11890Sstevel@tonic-gate 	 * segkp only allows single caller to hold SOFTLOCK, so cookie
11900Sstevel@tonic-gate 	 * holds a count so we dont call into segkp multiple times
11910Sstevel@tonic-gate 	 */
11920Sstevel@tonic-gate 	mutex_enter(&cookie->lock);
11930Sstevel@tonic-gate 
11940Sstevel@tonic-gate 	/*
11950Sstevel@tonic-gate 	 * Check for overflow in locked field
11960Sstevel@tonic-gate 	 */
11970Sstevel@tonic-gate 	if ((UINT32_MAX - cookie->locked) < npages) {
11980Sstevel@tonic-gate 		err = FC_MAKE_ERR(ENOMEM);
11990Sstevel@tonic-gate 	} else if (cookie->locked == 0) {
12000Sstevel@tonic-gate 		/* First time locking */
12010Sstevel@tonic-gate 		err = as_fault(kas.a_hat, &kas, cookie->cvaddr,
12020Sstevel@tonic-gate 		    cookie->size, F_SOFTLOCK, PROT_READ|PROT_WRITE);
12030Sstevel@tonic-gate 	}
12040Sstevel@tonic-gate 	if (!err) {
12050Sstevel@tonic-gate 		cookie->locked += npages;
12060Sstevel@tonic-gate 	}
12070Sstevel@tonic-gate 	mutex_exit(&cookie->lock);
12080Sstevel@tonic-gate 	return (err);
12090Sstevel@tonic-gate }
12100Sstevel@tonic-gate 
12110Sstevel@tonic-gate static void
12120Sstevel@tonic-gate release_kpmem_lock(struct ddi_umem_cookie *cookie, size_t npages)
12130Sstevel@tonic-gate {
12140Sstevel@tonic-gate 	mutex_enter(&cookie->lock);
12150Sstevel@tonic-gate 	ASSERT(cookie_is_kpmem(cookie));
12160Sstevel@tonic-gate 	ASSERT(cookie->locked >= npages);
12170Sstevel@tonic-gate 	cookie->locked -= (uint_t)npages;
12180Sstevel@tonic-gate 	if (cookie->locked == 0) {
12190Sstevel@tonic-gate 		/* Last unlock */
12200Sstevel@tonic-gate 		if (as_fault(kas.a_hat, &kas, cookie->cvaddr,
12210Sstevel@tonic-gate 		    cookie->size, F_SOFTUNLOCK, PROT_READ|PROT_WRITE))
12220Sstevel@tonic-gate 			panic("segdev releasing kpmem lock %p", (void *)cookie);
12230Sstevel@tonic-gate 	}
12240Sstevel@tonic-gate 	mutex_exit(&cookie->lock);
12250Sstevel@tonic-gate }
12260Sstevel@tonic-gate 
12270Sstevel@tonic-gate /*
12280Sstevel@tonic-gate  * Routines to synchronize F_SOFTLOCK and F_INVAL faults for
12290Sstevel@tonic-gate  * drivers with devmap_access callbacks
12300Sstevel@tonic-gate  * slock->softlocked basically works like a rw lock
12310Sstevel@tonic-gate  *	-ve counts => F_SOFTLOCK in progress
12320Sstevel@tonic-gate  *	+ve counts => F_INVAL/F_PROT in progress
12330Sstevel@tonic-gate  * We allow only one F_SOFTLOCK at a time
12340Sstevel@tonic-gate  * but can have multiple pending F_INVAL/F_PROT calls
12350Sstevel@tonic-gate  *
12360Sstevel@tonic-gate  * This routine waits using cv_wait_sig so killing processes is more graceful
12370Sstevel@tonic-gate  * Returns EINTR if coming out of this routine due to a signal, 0 otherwise
12380Sstevel@tonic-gate  */
12390Sstevel@tonic-gate static int devmap_softlock_enter(
12400Sstevel@tonic-gate 	struct devmap_softlock *slock,
12410Sstevel@tonic-gate 	size_t npages,
12420Sstevel@tonic-gate 	enum fault_type type)
12430Sstevel@tonic-gate {
12440Sstevel@tonic-gate 	if (npages == 0)
12450Sstevel@tonic-gate 		return (0);
12460Sstevel@tonic-gate 	mutex_enter(&(slock->lock));
12470Sstevel@tonic-gate 	switch (type) {
12480Sstevel@tonic-gate 	case F_SOFTLOCK :
12490Sstevel@tonic-gate 		while (slock->softlocked) {
12500Sstevel@tonic-gate 			if (cv_wait_sig(&(slock)->cv, &(slock)->lock) == 0) {
12510Sstevel@tonic-gate 				/* signalled */
12520Sstevel@tonic-gate 				mutex_exit(&(slock->lock));
12530Sstevel@tonic-gate 				return (EINTR);
12540Sstevel@tonic-gate 			}
12550Sstevel@tonic-gate 		}
12560Sstevel@tonic-gate 		slock->softlocked -= npages; /* -ve count => locked */
12570Sstevel@tonic-gate 		break;
12580Sstevel@tonic-gate 	case F_INVAL :
12590Sstevel@tonic-gate 	case F_PROT :
12600Sstevel@tonic-gate 		while (slock->softlocked < 0)
12610Sstevel@tonic-gate 			if (cv_wait_sig(&(slock)->cv, &(slock)->lock) == 0) {
12620Sstevel@tonic-gate 				/* signalled */
12630Sstevel@tonic-gate 				mutex_exit(&(slock->lock));
12640Sstevel@tonic-gate 				return (EINTR);
12650Sstevel@tonic-gate 			}
12660Sstevel@tonic-gate 		slock->softlocked += npages; /* +ve count => f_invals */
12670Sstevel@tonic-gate 		break;
12680Sstevel@tonic-gate 	default:
12690Sstevel@tonic-gate 		ASSERT(0);
12700Sstevel@tonic-gate 	}
12710Sstevel@tonic-gate 	mutex_exit(&(slock->lock));
12720Sstevel@tonic-gate 	return (0);
12730Sstevel@tonic-gate }
12740Sstevel@tonic-gate 
12750Sstevel@tonic-gate static void devmap_softlock_exit(
12760Sstevel@tonic-gate 	struct devmap_softlock *slock,
12770Sstevel@tonic-gate 	size_t npages,
12780Sstevel@tonic-gate 	enum fault_type type)
12790Sstevel@tonic-gate {
12800Sstevel@tonic-gate 	if (slock == NULL)
12810Sstevel@tonic-gate 		return;
12820Sstevel@tonic-gate 	mutex_enter(&(slock->lock));
12830Sstevel@tonic-gate 	switch (type) {
12840Sstevel@tonic-gate 	case F_SOFTLOCK :
12850Sstevel@tonic-gate 		ASSERT(-slock->softlocked >= npages);
12860Sstevel@tonic-gate 		slock->softlocked += npages;	/* -ve count is softlocked */
12870Sstevel@tonic-gate 		if (slock->softlocked == 0)
12880Sstevel@tonic-gate 			cv_signal(&slock->cv);
12890Sstevel@tonic-gate 		break;
12900Sstevel@tonic-gate 	case F_INVAL :
12910Sstevel@tonic-gate 	case F_PROT:
12920Sstevel@tonic-gate 		ASSERT(slock->softlocked >= npages);
12930Sstevel@tonic-gate 		slock->softlocked -= npages;
12940Sstevel@tonic-gate 		if (slock->softlocked == 0)
12950Sstevel@tonic-gate 			cv_signal(&slock->cv);
12960Sstevel@tonic-gate 		break;
12970Sstevel@tonic-gate 	default:
12980Sstevel@tonic-gate 		ASSERT(0);
12990Sstevel@tonic-gate 	}
13000Sstevel@tonic-gate 	mutex_exit(&(slock->lock));
13010Sstevel@tonic-gate }
13020Sstevel@tonic-gate 
13030Sstevel@tonic-gate /*
13040Sstevel@tonic-gate  * Do a F_SOFTUNLOCK call over the range requested.
13050Sstevel@tonic-gate  * The range must have already been F_SOFTLOCK'ed.
13060Sstevel@tonic-gate  * The segment lock should be held, (but not the segment private lock?)
13070Sstevel@tonic-gate  *  The softunlock code below does not adjust for large page sizes
13080Sstevel@tonic-gate  *	assumes the caller already did any addr/len adjustments for
13090Sstevel@tonic-gate  *	pagesize mappings before calling.
13100Sstevel@tonic-gate  */
13110Sstevel@tonic-gate /*ARGSUSED*/
13120Sstevel@tonic-gate static void
13130Sstevel@tonic-gate segdev_softunlock(
13140Sstevel@tonic-gate 	struct hat *hat,		/* the hat */
13150Sstevel@tonic-gate 	struct seg *seg,		/* seg_dev of interest */
13160Sstevel@tonic-gate 	caddr_t addr,			/* base address of range */
13170Sstevel@tonic-gate 	size_t len,			/* number of bytes */
13180Sstevel@tonic-gate 	enum seg_rw rw)			/* type of access at fault */
13190Sstevel@tonic-gate {
13200Sstevel@tonic-gate 	struct segdev_data *sdp = (struct segdev_data *)seg->s_data;
13210Sstevel@tonic-gate 	devmap_handle_t *dhp_head = (devmap_handle_t *)sdp->devmap_data;
13220Sstevel@tonic-gate 
13230Sstevel@tonic-gate 	TRACE_4(TR_FAC_DEVMAP, TR_DEVMAP_SOFTUNLOCK,
13240Sstevel@tonic-gate 	    "segdev_softunlock:dhp_head=%p sdp=%p addr=%p len=%lx",
13250Sstevel@tonic-gate 	    dhp_head, sdp, addr, len);
13260Sstevel@tonic-gate 	DEBUGF(3, (CE_CONT, "segdev_softunlock: dhp %p lockcnt %lx "
13270Sstevel@tonic-gate 	    "addr %p len %lx\n",
13280Sstevel@tonic-gate 	    (void *)dhp_head, sdp->softlockcnt, (void *)addr, len));
13290Sstevel@tonic-gate 
13300Sstevel@tonic-gate 	hat_unlock(hat, addr, len);
13310Sstevel@tonic-gate 
13320Sstevel@tonic-gate 	if (dhp_head != NULL) {
13330Sstevel@tonic-gate 		devmap_handle_t *dhp;
13340Sstevel@tonic-gate 		size_t mlen;
1335882Smec 		size_t tlen = len;
13360Sstevel@tonic-gate 		ulong_t off;
13370Sstevel@tonic-gate 
13380Sstevel@tonic-gate 		dhp = devmap_find_handle(dhp_head, addr);
13390Sstevel@tonic-gate 		ASSERT(dhp != NULL);
13400Sstevel@tonic-gate 
13410Sstevel@tonic-gate 		off = (ulong_t)(addr - dhp->dh_uvaddr);
1342882Smec 		while (tlen != 0) {
1343882Smec 			mlen = MIN(tlen, (dhp->dh_len - off));
13440Sstevel@tonic-gate 
13450Sstevel@tonic-gate 			/*
13460Sstevel@tonic-gate 			 * unlock segkp memory, locked during F_SOFTLOCK
13470Sstevel@tonic-gate 			 */
13480Sstevel@tonic-gate 			if (dhp_is_kpmem(dhp)) {
13490Sstevel@tonic-gate 				release_kpmem_lock(
13500Sstevel@tonic-gate 				    (struct ddi_umem_cookie *)dhp->dh_cookie,
13510Sstevel@tonic-gate 				    btopr(mlen));
13520Sstevel@tonic-gate 			}
13530Sstevel@tonic-gate 
13540Sstevel@tonic-gate 			/*
13550Sstevel@tonic-gate 			 * Do the softlock accounting for devmap_access
13560Sstevel@tonic-gate 			 */
13570Sstevel@tonic-gate 			if (dhp->dh_callbackops.devmap_access != NULL) {
13580Sstevel@tonic-gate 				devmap_softlock_exit(dhp->dh_softlock,
13590Sstevel@tonic-gate 					btopr(mlen), F_SOFTLOCK);
13600Sstevel@tonic-gate 			}
13610Sstevel@tonic-gate 
1362882Smec 			tlen -= mlen;
13630Sstevel@tonic-gate 			dhp = dhp->dh_next;
13640Sstevel@tonic-gate 			off = 0;
13650Sstevel@tonic-gate 		}
13660Sstevel@tonic-gate 	}
13670Sstevel@tonic-gate 
13680Sstevel@tonic-gate 	mutex_enter(&freemem_lock);
13690Sstevel@tonic-gate 	ASSERT(sdp->softlockcnt >= btopr(len));
13700Sstevel@tonic-gate 	sdp->softlockcnt -= btopr(len);
13710Sstevel@tonic-gate 	mutex_exit(&freemem_lock);
13720Sstevel@tonic-gate 	if (sdp->softlockcnt == 0) {
13730Sstevel@tonic-gate 		/*
13740Sstevel@tonic-gate 		 * All SOFTLOCKS are gone. Wakeup any waiting
13750Sstevel@tonic-gate 		 * unmappers so they can try again to unmap.
13760Sstevel@tonic-gate 		 * Check for waiters first without the mutex
13770Sstevel@tonic-gate 		 * held so we don't always grab the mutex on
13780Sstevel@tonic-gate 		 * softunlocks.
13790Sstevel@tonic-gate 		 */
13800Sstevel@tonic-gate 		if (AS_ISUNMAPWAIT(seg->s_as)) {
13810Sstevel@tonic-gate 			mutex_enter(&seg->s_as->a_contents);
13820Sstevel@tonic-gate 			if (AS_ISUNMAPWAIT(seg->s_as)) {
13830Sstevel@tonic-gate 				AS_CLRUNMAPWAIT(seg->s_as);
13840Sstevel@tonic-gate 				cv_broadcast(&seg->s_as->a_cv);
13850Sstevel@tonic-gate 			}
13860Sstevel@tonic-gate 			mutex_exit(&seg->s_as->a_contents);
13870Sstevel@tonic-gate 		}
13880Sstevel@tonic-gate 	}
13890Sstevel@tonic-gate 
13900Sstevel@tonic-gate }
13910Sstevel@tonic-gate 
13920Sstevel@tonic-gate /*
13930Sstevel@tonic-gate  * Handle fault for a single page.
13940Sstevel@tonic-gate  * Done in a separate routine so we can handle errors more easily.
13950Sstevel@tonic-gate  * This routine is called only from segdev_faultpages()
13960Sstevel@tonic-gate  * when looping over the range of addresses requested. The segment lock is held.
13970Sstevel@tonic-gate  */
13980Sstevel@tonic-gate static faultcode_t
13990Sstevel@tonic-gate segdev_faultpage(
14000Sstevel@tonic-gate 	struct hat *hat,		/* the hat */
14010Sstevel@tonic-gate 	struct seg *seg,		/* seg_dev of interest */
14020Sstevel@tonic-gate 	caddr_t addr,			/* address in as */
14030Sstevel@tonic-gate 	struct vpage *vpage,		/* pointer to vpage for seg, addr */
14040Sstevel@tonic-gate 	enum fault_type type,		/* type of fault */
14050Sstevel@tonic-gate 	enum seg_rw rw,			/* type of access at fault */
14060Sstevel@tonic-gate 	devmap_handle_t *dhp)		/* devmap handle if any for this page */
14070Sstevel@tonic-gate {
14080Sstevel@tonic-gate 	struct segdev_data *sdp = (struct segdev_data *)seg->s_data;
14090Sstevel@tonic-gate 	uint_t prot;
14100Sstevel@tonic-gate 	pfn_t pfnum = PFN_INVALID;
14110Sstevel@tonic-gate 	u_offset_t offset;
14120Sstevel@tonic-gate 	uint_t hat_flags;
14130Sstevel@tonic-gate 	dev_info_t *dip;
14140Sstevel@tonic-gate 
14150Sstevel@tonic-gate 	TRACE_3(TR_FAC_DEVMAP, TR_DEVMAP_FAULTPAGE,
14160Sstevel@tonic-gate 	    "segdev_faultpage: dhp=%p seg=%p addr=%p", dhp, seg, addr);
14170Sstevel@tonic-gate 	DEBUGF(8, (CE_CONT, "segdev_faultpage: dhp %p seg %p addr %p \n",
14180Sstevel@tonic-gate 	    (void *)dhp, (void *)seg, (void *)addr));
14190Sstevel@tonic-gate 
14200Sstevel@tonic-gate 	/*
14210Sstevel@tonic-gate 	 * Initialize protection value for this page.
14220Sstevel@tonic-gate 	 * If we have per page protection values check it now.
14230Sstevel@tonic-gate 	 */
14240Sstevel@tonic-gate 	if (sdp->pageprot) {
14250Sstevel@tonic-gate 		uint_t protchk;
14260Sstevel@tonic-gate 
14270Sstevel@tonic-gate 		switch (rw) {
14280Sstevel@tonic-gate 		case S_READ:
14290Sstevel@tonic-gate 			protchk = PROT_READ;
14300Sstevel@tonic-gate 			break;
14310Sstevel@tonic-gate 		case S_WRITE:
14320Sstevel@tonic-gate 			protchk = PROT_WRITE;
14330Sstevel@tonic-gate 			break;
14340Sstevel@tonic-gate 		case S_EXEC:
14350Sstevel@tonic-gate 			protchk = PROT_EXEC;
14360Sstevel@tonic-gate 			break;
14370Sstevel@tonic-gate 		case S_OTHER:
14380Sstevel@tonic-gate 		default:
14390Sstevel@tonic-gate 			protchk = PROT_READ | PROT_WRITE | PROT_EXEC;
14400Sstevel@tonic-gate 			break;
14410Sstevel@tonic-gate 		}
14420Sstevel@tonic-gate 
14430Sstevel@tonic-gate 		prot = VPP_PROT(vpage);
14440Sstevel@tonic-gate 		if ((prot & protchk) == 0)
14450Sstevel@tonic-gate 			return (FC_PROT);	/* illegal access type */
14460Sstevel@tonic-gate 	} else {
14470Sstevel@tonic-gate 		prot = sdp->prot;
14480Sstevel@tonic-gate 		/* caller has already done segment level protection check */
14490Sstevel@tonic-gate 	}
14500Sstevel@tonic-gate 
14510Sstevel@tonic-gate 	if (type == F_SOFTLOCK) {
14520Sstevel@tonic-gate 		mutex_enter(&freemem_lock);
14530Sstevel@tonic-gate 		sdp->softlockcnt++;
14540Sstevel@tonic-gate 		mutex_exit(&freemem_lock);
14550Sstevel@tonic-gate 	}
14560Sstevel@tonic-gate 
14570Sstevel@tonic-gate 	hat_flags = ((type == F_SOFTLOCK) ? HAT_LOAD_LOCK : HAT_LOAD);
14580Sstevel@tonic-gate 	offset = sdp->offset + (u_offset_t)(addr - seg->s_base);
14590Sstevel@tonic-gate 	/*
14600Sstevel@tonic-gate 	 * In the devmap framework, sdp->mapfunc is set to NULL.  we can get
14610Sstevel@tonic-gate 	 * pfnum from dhp->dh_pfn (at beginning of segment) and offset from
14620Sstevel@tonic-gate 	 * seg->s_base.
14630Sstevel@tonic-gate 	 */
14640Sstevel@tonic-gate 	if (dhp == NULL) {
14650Sstevel@tonic-gate 		/* If segment has devmap_data, then dhp should be non-NULL */
14660Sstevel@tonic-gate 		ASSERT(sdp->devmap_data == NULL);
14670Sstevel@tonic-gate 		pfnum = (pfn_t)cdev_mmap(sdp->mapfunc, sdp->vp->v_rdev,
14680Sstevel@tonic-gate 			(off_t)offset, prot);
14690Sstevel@tonic-gate 		prot |= sdp->hat_attr;
14700Sstevel@tonic-gate 	} else {
14710Sstevel@tonic-gate 		ulong_t off;
14720Sstevel@tonic-gate 		struct ddi_umem_cookie *cp;
14730Sstevel@tonic-gate 		struct devmap_pmem_cookie *pcp;
14740Sstevel@tonic-gate 
14750Sstevel@tonic-gate 		/* ensure the dhp passed in contains addr. */
14760Sstevel@tonic-gate 		ASSERT(dhp == devmap_find_handle(
14770Sstevel@tonic-gate 			(devmap_handle_t *)sdp->devmap_data, addr));
14780Sstevel@tonic-gate 
14790Sstevel@tonic-gate 		off = addr - dhp->dh_uvaddr;
14800Sstevel@tonic-gate 
14810Sstevel@tonic-gate 		/*
14820Sstevel@tonic-gate 		 * This routine assumes that the caller makes sure that the
14830Sstevel@tonic-gate 		 * fields in dhp used below are unchanged due to remap during
14840Sstevel@tonic-gate 		 * this call. Caller does HOLD_DHP_LOCK if neeed
14850Sstevel@tonic-gate 		 */
14860Sstevel@tonic-gate 		cp = dhp->dh_cookie;
14870Sstevel@tonic-gate 		if (dhp->dh_flags & DEVMAP_MAPPING_INVALID) {
14880Sstevel@tonic-gate 			pfnum = PFN_INVALID;
14890Sstevel@tonic-gate 		} else if (cookie_is_devmem(cp)) {
14900Sstevel@tonic-gate 			pfnum = dhp->dh_pfn + btop(off);
14910Sstevel@tonic-gate 		} else if (cookie_is_pmem(cp)) {
14920Sstevel@tonic-gate 			pcp = (struct devmap_pmem_cookie *)dhp->dh_pcookie;
14930Sstevel@tonic-gate 			ASSERT((dhp->dh_roff & PAGEOFFSET) == 0 &&
14940Sstevel@tonic-gate 				dhp->dh_roff < ptob(pcp->dp_npages));
14950Sstevel@tonic-gate 			pfnum = page_pptonum(
14960Sstevel@tonic-gate 			    pcp->dp_pparray[btop(off + dhp->dh_roff)]);
14970Sstevel@tonic-gate 		} else {
14980Sstevel@tonic-gate 			ASSERT(dhp->dh_roff < cp->size);
14990Sstevel@tonic-gate 			ASSERT(dhp->dh_cvaddr >= cp->cvaddr &&
15000Sstevel@tonic-gate 				dhp->dh_cvaddr < (cp->cvaddr + cp->size));
15010Sstevel@tonic-gate 			ASSERT((dhp->dh_cvaddr + off) <=
15020Sstevel@tonic-gate 				(cp->cvaddr + cp->size));
15030Sstevel@tonic-gate 			ASSERT((dhp->dh_cvaddr + off + PAGESIZE) <=
15040Sstevel@tonic-gate 				(cp->cvaddr + cp->size));
15050Sstevel@tonic-gate 
15060Sstevel@tonic-gate 			switch (cp->type) {
15070Sstevel@tonic-gate 			case UMEM_LOCKED :
15080Sstevel@tonic-gate 			    if (cp->pparray != NULL) {
15090Sstevel@tonic-gate 				ASSERT((dhp->dh_roff & PAGEOFFSET) == 0);
15100Sstevel@tonic-gate 				pfnum = page_pptonum(
15110Sstevel@tonic-gate 				    cp->pparray[btop(off + dhp->dh_roff)]);
15120Sstevel@tonic-gate 			    } else {
15130Sstevel@tonic-gate 				pfnum = hat_getpfnum(
15140Sstevel@tonic-gate 				    ((proc_t *)cp->procp)->p_as->a_hat,
15150Sstevel@tonic-gate 				    cp->cvaddr + off);
15160Sstevel@tonic-gate 			    }
15170Sstevel@tonic-gate 			    break;
15180Sstevel@tonic-gate 			case UMEM_TRASH :
15190Sstevel@tonic-gate 			    pfnum = page_pptonum(trashpp);
15200Sstevel@tonic-gate 			    /* We should set hat_flags to HAT_NOFAULT also */
15210Sstevel@tonic-gate 			    /* However, not all hat layers implement this */
15220Sstevel@tonic-gate 			    break;
15230Sstevel@tonic-gate 			case KMEM_PAGEABLE:
15240Sstevel@tonic-gate 			case KMEM_NON_PAGEABLE:
15250Sstevel@tonic-gate 			    pfnum = hat_getpfnum(kas.a_hat,
15260Sstevel@tonic-gate 				dhp->dh_cvaddr + off);
15270Sstevel@tonic-gate 			    break;
15280Sstevel@tonic-gate 			default :
15290Sstevel@tonic-gate 			    pfnum = PFN_INVALID;
15300Sstevel@tonic-gate 			    break;
15310Sstevel@tonic-gate 			}
15320Sstevel@tonic-gate 		}
15330Sstevel@tonic-gate 		prot |= dhp->dh_hat_attr;
15340Sstevel@tonic-gate 	}
15350Sstevel@tonic-gate 	if (pfnum == PFN_INVALID) {
15360Sstevel@tonic-gate 		return (FC_MAKE_ERR(EFAULT));
15370Sstevel@tonic-gate 	}
15380Sstevel@tonic-gate 	/* prot should already be OR'ed in with hat_attributes if needed */
15390Sstevel@tonic-gate 
15400Sstevel@tonic-gate 	TRACE_4(TR_FAC_DEVMAP, TR_DEVMAP_FAULTPAGE_CK1,
15410Sstevel@tonic-gate 	    "segdev_faultpage: pfnum=%lx memory=%x prot=%x flags=%x",
15420Sstevel@tonic-gate 	    pfnum, pf_is_memory(pfnum), prot, hat_flags);
15430Sstevel@tonic-gate 	DEBUGF(9, (CE_CONT, "segdev_faultpage: pfnum %lx memory %x "
15440Sstevel@tonic-gate 	    "prot %x flags %x\n", pfnum, pf_is_memory(pfnum), prot, hat_flags));
15450Sstevel@tonic-gate 
15460Sstevel@tonic-gate 	if (pf_is_memory(pfnum) || (dhp != NULL)) {
15470Sstevel@tonic-gate 		/*
15480Sstevel@tonic-gate 		 * It's not _really_ required here to pass sdp->hat_flags
15490Sstevel@tonic-gate 		 * to hat_devload even though we do it.
15500Sstevel@tonic-gate 		 * This is because hat figures it out DEVMEM mappings
15510Sstevel@tonic-gate 		 * are non-consistent, anyway.
15520Sstevel@tonic-gate 		 */
15530Sstevel@tonic-gate 		hat_devload(hat, addr, PAGESIZE, pfnum,
15540Sstevel@tonic-gate 				prot, hat_flags | sdp->hat_flags);
15550Sstevel@tonic-gate 		return (0);
15560Sstevel@tonic-gate 	}
15570Sstevel@tonic-gate 
15580Sstevel@tonic-gate 	/*
15590Sstevel@tonic-gate 	 * Fall through to the case where devmap is not used and need to call
15600Sstevel@tonic-gate 	 * up the device tree to set up the mapping
15610Sstevel@tonic-gate 	 */
15620Sstevel@tonic-gate 
15630Sstevel@tonic-gate 	dip = VTOS(VTOCVP(sdp->vp))->s_dip;
15640Sstevel@tonic-gate 	ASSERT(dip);
15650Sstevel@tonic-gate 
15660Sstevel@tonic-gate 	/*
15670Sstevel@tonic-gate 	 * When calling ddi_map_fault, we do not OR in sdp->hat_attr
15680Sstevel@tonic-gate 	 * This is because this calls drivers which may not expect
15690Sstevel@tonic-gate 	 * prot to have any other values than PROT_ALL
15700Sstevel@tonic-gate 	 * The root nexus driver has a hack to peek into the segment
15710Sstevel@tonic-gate 	 * structure and then OR in sdp->hat_attr.
15720Sstevel@tonic-gate 	 * XX In case the bus_ops interfaces are ever revisited
15730Sstevel@tonic-gate 	 * we need to fix this. prot should include other hat attributes
15740Sstevel@tonic-gate 	 */
15750Sstevel@tonic-gate 	if (ddi_map_fault(dip, hat, seg, addr, NULL, pfnum, prot & PROT_ALL,
15760Sstevel@tonic-gate 	    (uint_t)(type == F_SOFTLOCK)) != DDI_SUCCESS) {
15770Sstevel@tonic-gate 		return (FC_MAKE_ERR(EFAULT));
15780Sstevel@tonic-gate 	}
15790Sstevel@tonic-gate 	return (0);
15800Sstevel@tonic-gate }
15810Sstevel@tonic-gate 
15820Sstevel@tonic-gate static faultcode_t
15830Sstevel@tonic-gate segdev_fault(
15840Sstevel@tonic-gate 	struct hat *hat,		/* the hat */
15850Sstevel@tonic-gate 	struct seg *seg,		/* the seg_dev of interest */
15860Sstevel@tonic-gate 	caddr_t addr,			/* the address of the fault */
15870Sstevel@tonic-gate 	size_t len,			/* the length of the range */
15880Sstevel@tonic-gate 	enum fault_type type,		/* type of fault */
15890Sstevel@tonic-gate 	enum seg_rw rw)			/* type of access at fault */
15900Sstevel@tonic-gate {
15910Sstevel@tonic-gate 	struct segdev_data *sdp = (struct segdev_data *)seg->s_data;
15920Sstevel@tonic-gate 	devmap_handle_t *dhp_head = (devmap_handle_t *)sdp->devmap_data;
15930Sstevel@tonic-gate 	devmap_handle_t *dhp;
15940Sstevel@tonic-gate 	struct devmap_softlock *slock = NULL;
15950Sstevel@tonic-gate 	ulong_t slpage = 0;
15960Sstevel@tonic-gate 	ulong_t off;
15970Sstevel@tonic-gate 	caddr_t maddr = addr;
15980Sstevel@tonic-gate 	int err;
15990Sstevel@tonic-gate 	int err_is_faultcode = 0;
16000Sstevel@tonic-gate 
16010Sstevel@tonic-gate 	TRACE_5(TR_FAC_DEVMAP, TR_DEVMAP_FAULT,
16020Sstevel@tonic-gate 	    "segdev_fault: dhp_head=%p seg=%p addr=%p len=%lx type=%x",
16030Sstevel@tonic-gate 	    (void *)dhp_head, (void *)seg, (void *)addr, len, type);
16040Sstevel@tonic-gate 	DEBUGF(7, (CE_CONT, "segdev_fault: dhp_head %p seg %p "
16050Sstevel@tonic-gate 	    "addr %p len %lx type %x\n",
16060Sstevel@tonic-gate 	    (void *)dhp_head, (void *)seg, (void *)addr, len, type));
16070Sstevel@tonic-gate 
16080Sstevel@tonic-gate 	ASSERT(seg->s_as && AS_LOCK_HELD(seg->s_as, &seg->s_as->a_lock));
16090Sstevel@tonic-gate 
16100Sstevel@tonic-gate 	/* Handle non-devmap case */
16110Sstevel@tonic-gate 	if (dhp_head == NULL)
16120Sstevel@tonic-gate 		return (segdev_faultpages(hat, seg, addr, len, type, rw, NULL));
16130Sstevel@tonic-gate 
16140Sstevel@tonic-gate 	/* Find devmap handle */
16150Sstevel@tonic-gate 	if ((dhp = devmap_find_handle(dhp_head, addr)) == NULL)
16160Sstevel@tonic-gate 		return (FC_NOMAP);
16170Sstevel@tonic-gate 
16180Sstevel@tonic-gate 	/*
16190Sstevel@tonic-gate 	 * The seg_dev driver does not implement copy-on-write,
16200Sstevel@tonic-gate 	 * and always loads translations with maximal allowed permissions
16210Sstevel@tonic-gate 	 * but we got an fault trying to access the device.
16220Sstevel@tonic-gate 	 * Servicing the fault is not going to result in any better result
16230Sstevel@tonic-gate 	 * RFE: If we want devmap_access callbacks to be involved in F_PROT
16240Sstevel@tonic-gate 	 *	faults, then the code below is written for that
16250Sstevel@tonic-gate 	 *	Pending resolution of the following:
16260Sstevel@tonic-gate 	 *	- determine if the F_INVAL/F_SOFTLOCK syncing
16270Sstevel@tonic-gate 	 *	is needed for F_PROT also or not. The code below assumes it does
16280Sstevel@tonic-gate 	 *	- If driver sees F_PROT and calls devmap_load with same type,
16290Sstevel@tonic-gate 	 *	then segdev_faultpages will fail with FC_PROT anyway, need to
16300Sstevel@tonic-gate 	 *	change that so calls from devmap_load to segdev_faultpages for
16310Sstevel@tonic-gate 	 *	F_PROT type are retagged to F_INVAL.
16320Sstevel@tonic-gate 	 * RFE: Today we dont have drivers that use devmap and want to handle
16330Sstevel@tonic-gate 	 *	F_PROT calls. The code in segdev_fault* is written to allow
16340Sstevel@tonic-gate 	 *	this case but is not tested. A driver that needs this capability
16350Sstevel@tonic-gate 	 *	should be able to remove the short-circuit case; resolve the
16360Sstevel@tonic-gate 	 *	above issues and "should" work.
16370Sstevel@tonic-gate 	 */
16380Sstevel@tonic-gate 	if (type == F_PROT) {
16390Sstevel@tonic-gate 		return (FC_PROT);
16400Sstevel@tonic-gate 	}
16410Sstevel@tonic-gate 
16420Sstevel@tonic-gate 	/*
16430Sstevel@tonic-gate 	 * Loop through dhp list calling devmap_access or segdev_faultpages for
16440Sstevel@tonic-gate 	 * each devmap handle.
16450Sstevel@tonic-gate 	 * drivers which implement devmap_access can interpose on faults and do
16460Sstevel@tonic-gate 	 * device-appropriate special actions before calling devmap_load.
16470Sstevel@tonic-gate 	 */
16480Sstevel@tonic-gate 
16490Sstevel@tonic-gate 	/*
16500Sstevel@tonic-gate 	 * Unfortunately, this simple loop has turned out to expose a variety
16510Sstevel@tonic-gate 	 * of complex problems which results in the following convoluted code.
16520Sstevel@tonic-gate 	 *
16530Sstevel@tonic-gate 	 * First, a desire to handle a serialization of F_SOFTLOCK calls
16540Sstevel@tonic-gate 	 * to the driver within the framework.
16550Sstevel@tonic-gate 	 *	This results in a dh_softlock structure that is on a per device
16560Sstevel@tonic-gate 	 *	(or device instance) basis and serializes devmap_access calls.
16570Sstevel@tonic-gate 	 *	Ideally we would need to do this for underlying
16580Sstevel@tonic-gate 	 *	memory/device regions that are being faulted on
16590Sstevel@tonic-gate 	 *	but that is hard to identify and with REMAP, harder
16600Sstevel@tonic-gate 	 * Second, a desire to serialize F_INVAL(and F_PROT) calls w.r.t.
16610Sstevel@tonic-gate 	 * 	to F_SOFTLOCK calls to the driver.
16620Sstevel@tonic-gate 	 * These serializations are to simplify the driver programmer model.
16630Sstevel@tonic-gate 	 * To support these two features, the code first goes through the
16640Sstevel@tonic-gate 	 *	devmap handles and counts the pages (slpage) that are covered
16650Sstevel@tonic-gate 	 *	by devmap_access callbacks.
16660Sstevel@tonic-gate 	 * This part ends with a devmap_softlock_enter call
16670Sstevel@tonic-gate 	 *	which allows only one F_SOFTLOCK active on a device instance,
16680Sstevel@tonic-gate 	 *	but multiple F_INVAL/F_PROTs can be active except when a
16690Sstevel@tonic-gate 	 *	F_SOFTLOCK is active
16700Sstevel@tonic-gate 	 *
16710Sstevel@tonic-gate 	 * Next, we dont short-circuit the fault code upfront to call
16720Sstevel@tonic-gate 	 *	segdev_softunlock for F_SOFTUNLOCK, because we must use
16730Sstevel@tonic-gate 	 *	the same length when we softlock and softunlock.
16740Sstevel@tonic-gate 	 *
16750Sstevel@tonic-gate 	 *	-Hat layers may not support softunlocking lengths less than the
16760Sstevel@tonic-gate 	 *	original length when there is large page support.
16770Sstevel@tonic-gate 	 *	-kpmem locking is dependent on keeping the lengths same.
16780Sstevel@tonic-gate 	 *	-if drivers handled F_SOFTLOCK, they probably also expect to
16790Sstevel@tonic-gate 	 *		see an F_SOFTUNLOCK of the same length
16800Sstevel@tonic-gate 	 *	Hence, if extending lengths during softlock,
16810Sstevel@tonic-gate 	 *	softunlock has to make the same adjustments and goes through
16820Sstevel@tonic-gate 	 *	the same loop calling segdev_faultpages/segdev_softunlock
16830Sstevel@tonic-gate 	 *	But some of the synchronization and error handling is different
16840Sstevel@tonic-gate 	 */
16850Sstevel@tonic-gate 
16860Sstevel@tonic-gate 	if (type != F_SOFTUNLOCK) {
16870Sstevel@tonic-gate 		devmap_handle_t *dhpp = dhp;
16880Sstevel@tonic-gate 		size_t slen = len;
16890Sstevel@tonic-gate 
16900Sstevel@tonic-gate 		/*
16910Sstevel@tonic-gate 		 * Calculate count of pages that are :
16920Sstevel@tonic-gate 		 * a) within the (potentially extended) fault region
16930Sstevel@tonic-gate 		 * b) AND covered by devmap handle with devmap_access
16940Sstevel@tonic-gate 		 */
16950Sstevel@tonic-gate 		off = (ulong_t)(addr - dhpp->dh_uvaddr);
16960Sstevel@tonic-gate 		while (slen != 0) {
16970Sstevel@tonic-gate 			size_t mlen;
16980Sstevel@tonic-gate 
16990Sstevel@tonic-gate 			/*
17000Sstevel@tonic-gate 			 * Softlocking on a region that allows remap is
17010Sstevel@tonic-gate 			 * unsupported due to unresolved locking issues
17020Sstevel@tonic-gate 			 * XXX: unclear what these are?
17030Sstevel@tonic-gate 			 *	One potential is that if there is a pending
17040Sstevel@tonic-gate 			 *	softlock, then a remap should not be allowed
17050Sstevel@tonic-gate 			 *	until the unlock is done. This is easily
17060Sstevel@tonic-gate 			 *	fixed by returning error in devmap*remap on
17070Sstevel@tonic-gate 			 *	checking the dh->dh_softlock->softlocked value
17080Sstevel@tonic-gate 			 */
17090Sstevel@tonic-gate 			if ((type == F_SOFTLOCK) &&
17100Sstevel@tonic-gate 			    (dhpp->dh_flags & DEVMAP_ALLOW_REMAP)) {
17110Sstevel@tonic-gate 				return (FC_NOSUPPORT);
17120Sstevel@tonic-gate 			}
17130Sstevel@tonic-gate 
17140Sstevel@tonic-gate 			mlen = MIN(slen, (dhpp->dh_len - off));
17150Sstevel@tonic-gate 			if (dhpp->dh_callbackops.devmap_access) {
17160Sstevel@tonic-gate 				size_t llen;
17170Sstevel@tonic-gate 				caddr_t laddr;
17180Sstevel@tonic-gate 				/*
17190Sstevel@tonic-gate 				 * use extended length for large page mappings
17200Sstevel@tonic-gate 				 */
17210Sstevel@tonic-gate 				HOLD_DHP_LOCK(dhpp);
17220Sstevel@tonic-gate 				if ((sdp->pageprot == 0) &&
17230Sstevel@tonic-gate 				    (dhpp->dh_flags & DEVMAP_FLAG_LARGE)) {
17240Sstevel@tonic-gate 					devmap_get_large_pgsize(dhpp,
17250Sstevel@tonic-gate 					    mlen, maddr, &llen, &laddr);
17260Sstevel@tonic-gate 				} else {
17270Sstevel@tonic-gate 					llen = mlen;
17280Sstevel@tonic-gate 				}
17290Sstevel@tonic-gate 				RELE_DHP_LOCK(dhpp);
17300Sstevel@tonic-gate 
17310Sstevel@tonic-gate 				slpage += btopr(llen);
17320Sstevel@tonic-gate 				slock = dhpp->dh_softlock;
17330Sstevel@tonic-gate 			}
17340Sstevel@tonic-gate 			maddr += mlen;
17350Sstevel@tonic-gate 			ASSERT(slen >= mlen);
17360Sstevel@tonic-gate 			slen -= mlen;
17370Sstevel@tonic-gate 			dhpp = dhpp->dh_next;
17380Sstevel@tonic-gate 			off = 0;
17390Sstevel@tonic-gate 		}
17400Sstevel@tonic-gate 		/*
17410Sstevel@tonic-gate 		 * synchonize with other faulting threads and wait till safe
17420Sstevel@tonic-gate 		 * devmap_softlock_enter might return due to signal in cv_wait
17430Sstevel@tonic-gate 		 *
17440Sstevel@tonic-gate 		 * devmap_softlock_enter has to be called outside of while loop
17450Sstevel@tonic-gate 		 * to prevent a deadlock if len spans over multiple dhps.
17460Sstevel@tonic-gate 		 * dh_softlock is based on device instance and if multiple dhps
17470Sstevel@tonic-gate 		 * use the same device instance, the second dhp's LOCK call
17480Sstevel@tonic-gate 		 * will hang waiting on the first to complete.
17490Sstevel@tonic-gate 		 * devmap_setup verifies that slocks in a dhp_chain are same.
17500Sstevel@tonic-gate 		 * RFE: this deadlock only hold true for F_SOFTLOCK. For
17510Sstevel@tonic-gate 		 * 	F_INVAL/F_PROT, since we now allow multiple in parallel,
17520Sstevel@tonic-gate 		 *	we could have done the softlock_enter inside the loop
17530Sstevel@tonic-gate 		 *	and supported multi-dhp mappings with dissimilar devices
17540Sstevel@tonic-gate 		 */
17550Sstevel@tonic-gate 		if (err = devmap_softlock_enter(slock, slpage, type))
17560Sstevel@tonic-gate 			return (FC_MAKE_ERR(err));
17570Sstevel@tonic-gate 	}
17580Sstevel@tonic-gate 
17590Sstevel@tonic-gate 	/* reset 'maddr' to the start addr of the range of fault. */
17600Sstevel@tonic-gate 	maddr = addr;
17610Sstevel@tonic-gate 
17620Sstevel@tonic-gate 	/* calculate the offset corresponds to 'addr' in the first dhp. */
17630Sstevel@tonic-gate 	off = (ulong_t)(addr - dhp->dh_uvaddr);
17640Sstevel@tonic-gate 
17650Sstevel@tonic-gate 	/*
17660Sstevel@tonic-gate 	 * The fault length may span over multiple dhps.
17670Sstevel@tonic-gate 	 * Loop until the total length is satisfied.
17680Sstevel@tonic-gate 	 */
17690Sstevel@tonic-gate 	while (len != 0) {
17700Sstevel@tonic-gate 		size_t llen;
17710Sstevel@tonic-gate 		size_t mlen;
17720Sstevel@tonic-gate 		caddr_t laddr;
17730Sstevel@tonic-gate 
17740Sstevel@tonic-gate 		/*
17750Sstevel@tonic-gate 		 * mlen is the smaller of 'len' and the length
17760Sstevel@tonic-gate 		 * from addr to the end of mapping defined by dhp.
17770Sstevel@tonic-gate 		 */
17780Sstevel@tonic-gate 		mlen = MIN(len, (dhp->dh_len - off));
17790Sstevel@tonic-gate 
17800Sstevel@tonic-gate 		HOLD_DHP_LOCK(dhp);
17810Sstevel@tonic-gate 		/*
17820Sstevel@tonic-gate 		 * Pass the extended length and address to devmap_access
17830Sstevel@tonic-gate 		 * if large pagesize is used for loading address translations.
17840Sstevel@tonic-gate 		 */
17850Sstevel@tonic-gate 		if ((sdp->pageprot == 0) &&
17860Sstevel@tonic-gate 		    (dhp->dh_flags & DEVMAP_FLAG_LARGE)) {
17870Sstevel@tonic-gate 			devmap_get_large_pgsize(dhp, mlen, maddr,
17880Sstevel@tonic-gate 				&llen, &laddr);
17890Sstevel@tonic-gate 			ASSERT(maddr == addr || laddr == maddr);
17900Sstevel@tonic-gate 		} else {
17910Sstevel@tonic-gate 			llen = mlen;
17920Sstevel@tonic-gate 			laddr = maddr;
17930Sstevel@tonic-gate 		}
17940Sstevel@tonic-gate 
17950Sstevel@tonic-gate 		if (dhp->dh_callbackops.devmap_access != NULL) {
17960Sstevel@tonic-gate 			offset_t aoff;
17970Sstevel@tonic-gate 
17980Sstevel@tonic-gate 			aoff = sdp->offset + (offset_t)(laddr - seg->s_base);
17990Sstevel@tonic-gate 
18000Sstevel@tonic-gate 			/*
18010Sstevel@tonic-gate 			 * call driver's devmap_access entry point which will
18020Sstevel@tonic-gate 			 * call devmap_load/contextmgmt to load the translations
18030Sstevel@tonic-gate 			 *
18040Sstevel@tonic-gate 			 * We drop the dhp_lock before calling access so
18050Sstevel@tonic-gate 			 * drivers can call devmap_*_remap within access
18060Sstevel@tonic-gate 			 */
18070Sstevel@tonic-gate 			RELE_DHP_LOCK(dhp);
18080Sstevel@tonic-gate 
18090Sstevel@tonic-gate 			err = (*dhp->dh_callbackops.devmap_access)(
18100Sstevel@tonic-gate 			    dhp, (void *)dhp->dh_pvtp, aoff, llen, type, rw);
18110Sstevel@tonic-gate 		} else {
18120Sstevel@tonic-gate 			/*
18130Sstevel@tonic-gate 			 * If no devmap_access entry point, then load mappings
18140Sstevel@tonic-gate 			 * hold dhp_lock across faultpages if REMAP
18150Sstevel@tonic-gate 			 */
18160Sstevel@tonic-gate 			err = segdev_faultpages(hat, seg, laddr, llen,
18170Sstevel@tonic-gate 			    type, rw, dhp);
18180Sstevel@tonic-gate 			err_is_faultcode = 1;
18190Sstevel@tonic-gate 			RELE_DHP_LOCK(dhp);
18200Sstevel@tonic-gate 		}
18210Sstevel@tonic-gate 
18220Sstevel@tonic-gate 		if (err) {
18230Sstevel@tonic-gate 			if ((type == F_SOFTLOCK) && (maddr > addr)) {
18240Sstevel@tonic-gate 				/*
18250Sstevel@tonic-gate 				 * If not first dhp, use
18260Sstevel@tonic-gate 				 * segdev_fault(F_SOFTUNLOCK) for prior dhps
18270Sstevel@tonic-gate 				 * While this is recursion, it is incorrect to
18280Sstevel@tonic-gate 				 * call just segdev_softunlock
18290Sstevel@tonic-gate 				 * if we are using either large pages
18300Sstevel@tonic-gate 				 * or devmap_access. It will be more right
18310Sstevel@tonic-gate 				 * to go through the same loop as above
18320Sstevel@tonic-gate 				 * rather than call segdev_softunlock directly
18330Sstevel@tonic-gate 				 * It will use the right lenghths as well as
18340Sstevel@tonic-gate 				 * call into the driver devmap_access routines.
18350Sstevel@tonic-gate 				 */
18360Sstevel@tonic-gate 				size_t done = (size_t)(maddr - addr);
18370Sstevel@tonic-gate 				(void) segdev_fault(hat, seg, addr, done,
18380Sstevel@tonic-gate 					F_SOFTUNLOCK, S_OTHER);
18390Sstevel@tonic-gate 				/*
18400Sstevel@tonic-gate 				 * reduce slpage by number of pages
18410Sstevel@tonic-gate 				 * released by segdev_softunlock
18420Sstevel@tonic-gate 				 */
18430Sstevel@tonic-gate 				ASSERT(slpage >= btopr(done));
18440Sstevel@tonic-gate 				devmap_softlock_exit(slock,
18450Sstevel@tonic-gate 					slpage - btopr(done), type);
18460Sstevel@tonic-gate 			} else {
18470Sstevel@tonic-gate 				devmap_softlock_exit(slock, slpage, type);
18480Sstevel@tonic-gate 			}
18490Sstevel@tonic-gate 
18500Sstevel@tonic-gate 
18510Sstevel@tonic-gate 			/*
18520Sstevel@tonic-gate 			 * Segdev_faultpages() already returns a faultcode,
18530Sstevel@tonic-gate 			 * hence, result from segdev_faultpages() should be
18540Sstevel@tonic-gate 			 * returned directly.
18550Sstevel@tonic-gate 			 */
18560Sstevel@tonic-gate 			if (err_is_faultcode)
18570Sstevel@tonic-gate 				return (err);
18580Sstevel@tonic-gate 			return (FC_MAKE_ERR(err));
18590Sstevel@tonic-gate 		}
18600Sstevel@tonic-gate 
18610Sstevel@tonic-gate 		maddr += mlen;
18620Sstevel@tonic-gate 		ASSERT(len >= mlen);
18630Sstevel@tonic-gate 		len -= mlen;
18640Sstevel@tonic-gate 		dhp = dhp->dh_next;
18650Sstevel@tonic-gate 		off = 0;
18660Sstevel@tonic-gate 
18670Sstevel@tonic-gate 		ASSERT(!dhp || len == 0 || maddr == dhp->dh_uvaddr);
18680Sstevel@tonic-gate 	}
18690Sstevel@tonic-gate 	/*
18700Sstevel@tonic-gate 	 * release the softlock count at end of fault
18710Sstevel@tonic-gate 	 * For F_SOFTLOCk this is done in the later F_SOFTUNLOCK
18720Sstevel@tonic-gate 	 */
18730Sstevel@tonic-gate 	if ((type == F_INVAL) || (type == F_PROT))
18740Sstevel@tonic-gate 		devmap_softlock_exit(slock, slpage, type);
18750Sstevel@tonic-gate 	return (0);
18760Sstevel@tonic-gate }
18770Sstevel@tonic-gate 
18780Sstevel@tonic-gate /*
18790Sstevel@tonic-gate  * segdev_faultpages
18800Sstevel@tonic-gate  *
18810Sstevel@tonic-gate  * Used to fault in seg_dev segment pages. Called by segdev_fault or devmap_load
18820Sstevel@tonic-gate  * This routine assumes that the callers makes sure that the fields
18830Sstevel@tonic-gate  * in dhp used below are not changed due to remap during this call.
18840Sstevel@tonic-gate  * Caller does HOLD_DHP_LOCK if neeed
18850Sstevel@tonic-gate  * This routine returns a faultcode_t as a return value for segdev_fault.
18860Sstevel@tonic-gate  */
18870Sstevel@tonic-gate static faultcode_t
18880Sstevel@tonic-gate segdev_faultpages(
18890Sstevel@tonic-gate 	struct hat *hat,		/* the hat */
18900Sstevel@tonic-gate 	struct seg *seg,		/* the seg_dev of interest */
18910Sstevel@tonic-gate 	caddr_t addr,			/* the address of the fault */
18920Sstevel@tonic-gate 	size_t len,			/* the length of the range */
18930Sstevel@tonic-gate 	enum fault_type type,		/* type of fault */
18940Sstevel@tonic-gate 	enum seg_rw rw,			/* type of access at fault */
18950Sstevel@tonic-gate 	devmap_handle_t *dhp)		/* devmap handle */
18960Sstevel@tonic-gate {
18970Sstevel@tonic-gate 	register struct segdev_data *sdp = (struct segdev_data *)seg->s_data;
18980Sstevel@tonic-gate 	register caddr_t a;
18990Sstevel@tonic-gate 	struct vpage *vpage;
19000Sstevel@tonic-gate 	struct ddi_umem_cookie *kpmem_cookie = NULL;
19010Sstevel@tonic-gate 	int err;
19020Sstevel@tonic-gate 
19030Sstevel@tonic-gate 	TRACE_4(TR_FAC_DEVMAP, TR_DEVMAP_FAULTPAGES,
19040Sstevel@tonic-gate 	    "segdev_faultpages: dhp=%p seg=%p addr=%p len=%lx",
19050Sstevel@tonic-gate 	    (void *)dhp, (void *)seg, (void *)addr, len);
19060Sstevel@tonic-gate 	DEBUGF(5, (CE_CONT, "segdev_faultpages: "
19070Sstevel@tonic-gate 	    "dhp %p seg %p addr %p len %lx\n",
19080Sstevel@tonic-gate 	    (void *)dhp, (void *)seg, (void *)addr, len));
19090Sstevel@tonic-gate 
19100Sstevel@tonic-gate 	/*
19110Sstevel@tonic-gate 	 * The seg_dev driver does not implement copy-on-write,
19120Sstevel@tonic-gate 	 * and always loads translations with maximal allowed permissions
19130Sstevel@tonic-gate 	 * but we got an fault trying to access the device.
19140Sstevel@tonic-gate 	 * Servicing the fault is not going to result in any better result
19150Sstevel@tonic-gate 	 * XXX: If we want to allow devmap_access to handle F_PROT calls,
19160Sstevel@tonic-gate 	 * This code should be removed and let the normal fault handling
19170Sstevel@tonic-gate 	 * take care of finding the error
19180Sstevel@tonic-gate 	 */
19190Sstevel@tonic-gate 	if (type == F_PROT) {
19200Sstevel@tonic-gate 		return (FC_PROT);
19210Sstevel@tonic-gate 	}
19220Sstevel@tonic-gate 
19230Sstevel@tonic-gate 	if (type == F_SOFTUNLOCK) {
19240Sstevel@tonic-gate 		segdev_softunlock(hat, seg, addr, len, rw);
19250Sstevel@tonic-gate 		return (0);
19260Sstevel@tonic-gate 	}
19270Sstevel@tonic-gate 
19280Sstevel@tonic-gate 	/*
19290Sstevel@tonic-gate 	 * For kernel pageable memory, fault/lock segkp pages
19300Sstevel@tonic-gate 	 * We hold this until the completion of this
19310Sstevel@tonic-gate 	 * fault (INVAL/PROT) or till unlock (SOFTLOCK).
19320Sstevel@tonic-gate 	 */
19330Sstevel@tonic-gate 	if ((dhp != NULL) && dhp_is_kpmem(dhp)) {
19340Sstevel@tonic-gate 		kpmem_cookie = (struct ddi_umem_cookie *)dhp->dh_cookie;
19350Sstevel@tonic-gate 		if (err = acquire_kpmem_lock(kpmem_cookie, btopr(len)))
19360Sstevel@tonic-gate 			return (err);
19370Sstevel@tonic-gate 	}
19380Sstevel@tonic-gate 
19390Sstevel@tonic-gate 	/*
19400Sstevel@tonic-gate 	 * If we have the same protections for the entire segment,
19410Sstevel@tonic-gate 	 * insure that the access being attempted is legitimate.
19420Sstevel@tonic-gate 	 */
19430Sstevel@tonic-gate 	mutex_enter(&sdp->lock);
19440Sstevel@tonic-gate 	if (sdp->pageprot == 0) {
19450Sstevel@tonic-gate 		uint_t protchk;
19460Sstevel@tonic-gate 
19470Sstevel@tonic-gate 		switch (rw) {
19480Sstevel@tonic-gate 		case S_READ:
19490Sstevel@tonic-gate 			protchk = PROT_READ;
19500Sstevel@tonic-gate 			break;
19510Sstevel@tonic-gate 		case S_WRITE:
19520Sstevel@tonic-gate 			protchk = PROT_WRITE;
19530Sstevel@tonic-gate 			break;
19540Sstevel@tonic-gate 		case S_EXEC:
19550Sstevel@tonic-gate 			protchk = PROT_EXEC;
19560Sstevel@tonic-gate 			break;
19570Sstevel@tonic-gate 		case S_OTHER:
19580Sstevel@tonic-gate 		default:
19590Sstevel@tonic-gate 			protchk = PROT_READ | PROT_WRITE | PROT_EXEC;
19600Sstevel@tonic-gate 			break;
19610Sstevel@tonic-gate 		}
19620Sstevel@tonic-gate 
19630Sstevel@tonic-gate 		if ((sdp->prot & protchk) == 0) {
19640Sstevel@tonic-gate 			mutex_exit(&sdp->lock);
19650Sstevel@tonic-gate 			/* undo kpmem locking */
19660Sstevel@tonic-gate 			if (kpmem_cookie != NULL) {
19670Sstevel@tonic-gate 				release_kpmem_lock(kpmem_cookie, btopr(len));
19680Sstevel@tonic-gate 			}
19690Sstevel@tonic-gate 			return (FC_PROT);	/* illegal access type */
19700Sstevel@tonic-gate 		}
19710Sstevel@tonic-gate 	}
19720Sstevel@tonic-gate 
19730Sstevel@tonic-gate 	/*
19740Sstevel@tonic-gate 	 * we do a single hat_devload for the range if
19750Sstevel@tonic-gate 	 *   - devmap framework (dhp is not NULL),
19760Sstevel@tonic-gate 	 *   - pageprot == 0, i.e., no per-page protection set and
19770Sstevel@tonic-gate 	 *   - is device pages, irrespective of whether we are using large pages
19780Sstevel@tonic-gate 	 */
19790Sstevel@tonic-gate 	if ((sdp->pageprot == 0) && (dhp != NULL) && dhp_is_devmem(dhp)) {
19800Sstevel@tonic-gate 		pfn_t pfnum;
19810Sstevel@tonic-gate 		uint_t hat_flags;
19820Sstevel@tonic-gate 
19830Sstevel@tonic-gate 		if (dhp->dh_flags & DEVMAP_MAPPING_INVALID) {
19840Sstevel@tonic-gate 			mutex_exit(&sdp->lock);
19850Sstevel@tonic-gate 			return (FC_NOMAP);
19860Sstevel@tonic-gate 		}
19870Sstevel@tonic-gate 
19880Sstevel@tonic-gate 		if (type == F_SOFTLOCK) {
19890Sstevel@tonic-gate 			mutex_enter(&freemem_lock);
19900Sstevel@tonic-gate 			sdp->softlockcnt += btopr(len);
19910Sstevel@tonic-gate 			mutex_exit(&freemem_lock);
19920Sstevel@tonic-gate 		}
19930Sstevel@tonic-gate 
19940Sstevel@tonic-gate 		hat_flags = ((type == F_SOFTLOCK) ? HAT_LOAD_LOCK : HAT_LOAD);
19950Sstevel@tonic-gate 		pfnum = dhp->dh_pfn + btop((uintptr_t)(addr - dhp->dh_uvaddr));
19960Sstevel@tonic-gate 		ASSERT(!pf_is_memory(pfnum));
19970Sstevel@tonic-gate 
19980Sstevel@tonic-gate 		hat_devload(hat, addr, len, pfnum, sdp->prot | dhp->dh_hat_attr,
19990Sstevel@tonic-gate 			hat_flags | sdp->hat_flags);
20000Sstevel@tonic-gate 		mutex_exit(&sdp->lock);
20010Sstevel@tonic-gate 		return (0);
20020Sstevel@tonic-gate 	}
20030Sstevel@tonic-gate 
20040Sstevel@tonic-gate 	/* Handle cases where we have to loop through fault handling per-page */
20050Sstevel@tonic-gate 
20060Sstevel@tonic-gate 	if (sdp->vpage == NULL)
20070Sstevel@tonic-gate 		vpage = NULL;
20080Sstevel@tonic-gate 	else
20090Sstevel@tonic-gate 		vpage = &sdp->vpage[seg_page(seg, addr)];
20100Sstevel@tonic-gate 
20110Sstevel@tonic-gate 	/* loop over the address range handling each fault */
20120Sstevel@tonic-gate 	for (a = addr; a < addr + len; a += PAGESIZE) {
20130Sstevel@tonic-gate 		if (err = segdev_faultpage(hat, seg, a, vpage, type, rw, dhp)) {
20140Sstevel@tonic-gate 			break;
20150Sstevel@tonic-gate 		}
20160Sstevel@tonic-gate 		if (vpage != NULL)
20170Sstevel@tonic-gate 			vpage++;
20180Sstevel@tonic-gate 	}
20190Sstevel@tonic-gate 	mutex_exit(&sdp->lock);
20200Sstevel@tonic-gate 	if (err && (type == F_SOFTLOCK)) { /* error handling for F_SOFTLOCK */
20210Sstevel@tonic-gate 		size_t done = (size_t)(a - addr); /* pages fault successfully */
20220Sstevel@tonic-gate 		if (done > 0) {
20230Sstevel@tonic-gate 			/* use softunlock for those pages */
20240Sstevel@tonic-gate 			segdev_softunlock(hat, seg, addr, done, S_OTHER);
20250Sstevel@tonic-gate 		}
20260Sstevel@tonic-gate 		if (kpmem_cookie != NULL) {
20270Sstevel@tonic-gate 			/* release kpmem lock for rest of pages */
20280Sstevel@tonic-gate 			ASSERT(len >= done);
20290Sstevel@tonic-gate 			release_kpmem_lock(kpmem_cookie, btopr(len - done));
20300Sstevel@tonic-gate 		}
20310Sstevel@tonic-gate 	} else if ((kpmem_cookie != NULL) && (type != F_SOFTLOCK)) {
20320Sstevel@tonic-gate 		/* for non-SOFTLOCK cases, release kpmem */
20330Sstevel@tonic-gate 		release_kpmem_lock(kpmem_cookie, btopr(len));
20340Sstevel@tonic-gate 	}
20350Sstevel@tonic-gate 	return (err);
20360Sstevel@tonic-gate }
20370Sstevel@tonic-gate 
20380Sstevel@tonic-gate /*
20390Sstevel@tonic-gate  * Asynchronous page fault.  We simply do nothing since this
20400Sstevel@tonic-gate  * entry point is not supposed to load up the translation.
20410Sstevel@tonic-gate  */
20420Sstevel@tonic-gate /*ARGSUSED*/
20430Sstevel@tonic-gate static faultcode_t
20440Sstevel@tonic-gate segdev_faulta(struct seg *seg, caddr_t addr)
20450Sstevel@tonic-gate {
20460Sstevel@tonic-gate 	TRACE_2(TR_FAC_DEVMAP, TR_DEVMAP_FAULTA,
20470Sstevel@tonic-gate 	    "segdev_faulta: seg=%p addr=%p", (void *)seg, (void *)addr);
20480Sstevel@tonic-gate 	ASSERT(seg->s_as && AS_LOCK_HELD(seg->s_as, &seg->s_as->a_lock));
20490Sstevel@tonic-gate 
20500Sstevel@tonic-gate 	return (0);
20510Sstevel@tonic-gate }
20520Sstevel@tonic-gate 
20530Sstevel@tonic-gate static int
20540Sstevel@tonic-gate segdev_setprot(struct seg *seg, caddr_t addr, size_t len, uint_t prot)
20550Sstevel@tonic-gate {
20560Sstevel@tonic-gate 	register struct segdev_data *sdp = (struct segdev_data *)seg->s_data;
20570Sstevel@tonic-gate 	register devmap_handle_t *dhp;
20580Sstevel@tonic-gate 	register struct vpage *vp, *evp;
20590Sstevel@tonic-gate 	devmap_handle_t *dhp_head = (devmap_handle_t *)sdp->devmap_data;
20600Sstevel@tonic-gate 	ulong_t off;
20610Sstevel@tonic-gate 	size_t mlen, sz;
20620Sstevel@tonic-gate 
20630Sstevel@tonic-gate 	TRACE_4(TR_FAC_DEVMAP, TR_DEVMAP_SETPROT,
20640Sstevel@tonic-gate 	    "segdev_setprot:start seg=%p addr=%p len=%lx prot=%x",
20650Sstevel@tonic-gate 	    (void *)seg, (void *)addr, len, prot);
20660Sstevel@tonic-gate 	ASSERT(seg->s_as && AS_LOCK_HELD(seg->s_as, &seg->s_as->a_lock));
20670Sstevel@tonic-gate 
20680Sstevel@tonic-gate 	if ((sz = sdp->softlockcnt) > 0 && dhp_head != NULL) {
20690Sstevel@tonic-gate 		/*
20700Sstevel@tonic-gate 		 * Fail the setprot if pages are SOFTLOCKed through this
20710Sstevel@tonic-gate 		 * mapping.
20720Sstevel@tonic-gate 		 * Softlockcnt is protected from change by the as read lock.
20730Sstevel@tonic-gate 		 */
20740Sstevel@tonic-gate 		TRACE_1(TR_FAC_DEVMAP, TR_DEVMAP_SETPROT_CK1,
20750Sstevel@tonic-gate 		    "segdev_setprot:error softlockcnt=%lx", sz);
20760Sstevel@tonic-gate 		DEBUGF(1, (CE_CONT, "segdev_setprot: softlockcnt %ld\n", sz));
20770Sstevel@tonic-gate 		return (EAGAIN);
20780Sstevel@tonic-gate 	}
20790Sstevel@tonic-gate 
20800Sstevel@tonic-gate 	if (dhp_head != NULL) {
20810Sstevel@tonic-gate 		if ((dhp = devmap_find_handle(dhp_head, addr)) == NULL)
20820Sstevel@tonic-gate 			return (EINVAL);
20830Sstevel@tonic-gate 
20840Sstevel@tonic-gate 		/*
20850Sstevel@tonic-gate 		 * check if violate maxprot.
20860Sstevel@tonic-gate 		 */
20870Sstevel@tonic-gate 		off = (ulong_t)(addr - dhp->dh_uvaddr);
20880Sstevel@tonic-gate 		mlen  = len;
20890Sstevel@tonic-gate 		while (dhp) {
20900Sstevel@tonic-gate 			if ((dhp->dh_maxprot & prot) != prot)
20910Sstevel@tonic-gate 				return (EACCES);	/* violated maxprot */
20920Sstevel@tonic-gate 
20930Sstevel@tonic-gate 			if (mlen > (dhp->dh_len - off)) {
20940Sstevel@tonic-gate 				mlen -= dhp->dh_len - off;
20950Sstevel@tonic-gate 				dhp = dhp->dh_next;
20960Sstevel@tonic-gate 				off = 0;
20970Sstevel@tonic-gate 			} else
20980Sstevel@tonic-gate 				break;
20990Sstevel@tonic-gate 		}
21000Sstevel@tonic-gate 	} else {
21010Sstevel@tonic-gate 		if ((sdp->maxprot & prot) != prot)
21020Sstevel@tonic-gate 			return (EACCES);
21030Sstevel@tonic-gate 	}
21040Sstevel@tonic-gate 
21050Sstevel@tonic-gate 	mutex_enter(&sdp->lock);
21060Sstevel@tonic-gate 	if (addr == seg->s_base && len == seg->s_size && sdp->pageprot == 0) {
21070Sstevel@tonic-gate 		if (sdp->prot == prot) {
21080Sstevel@tonic-gate 			mutex_exit(&sdp->lock);
21090Sstevel@tonic-gate 			return (0);			/* all done */
21100Sstevel@tonic-gate 		}
21110Sstevel@tonic-gate 		sdp->prot = (uchar_t)prot;
21120Sstevel@tonic-gate 	} else {
21130Sstevel@tonic-gate 		sdp->pageprot = 1;
21140Sstevel@tonic-gate 		if (sdp->vpage == NULL) {
21150Sstevel@tonic-gate 			/*
21160Sstevel@tonic-gate 			 * First time through setting per page permissions,
21170Sstevel@tonic-gate 			 * initialize all the vpage structures to prot
21180Sstevel@tonic-gate 			 */
21190Sstevel@tonic-gate 			sdp->vpage = kmem_zalloc(vpgtob(seg_pages(seg)),
21200Sstevel@tonic-gate 			    KM_SLEEP);
21210Sstevel@tonic-gate 			evp = &sdp->vpage[seg_pages(seg)];
21220Sstevel@tonic-gate 			for (vp = sdp->vpage; vp < evp; vp++)
21230Sstevel@tonic-gate 				VPP_SETPROT(vp, sdp->prot);
21240Sstevel@tonic-gate 		}
21250Sstevel@tonic-gate 		/*
21260Sstevel@tonic-gate 		 * Now go change the needed vpages protections.
21270Sstevel@tonic-gate 		 */
21280Sstevel@tonic-gate 		evp = &sdp->vpage[seg_page(seg, addr + len)];
21290Sstevel@tonic-gate 		for (vp = &sdp->vpage[seg_page(seg, addr)]; vp < evp; vp++)
21300Sstevel@tonic-gate 			VPP_SETPROT(vp, prot);
21310Sstevel@tonic-gate 	}
21320Sstevel@tonic-gate 	mutex_exit(&sdp->lock);
21330Sstevel@tonic-gate 
21340Sstevel@tonic-gate 	if (dhp_head != NULL) {
21350Sstevel@tonic-gate 		devmap_handle_t *tdhp;
21360Sstevel@tonic-gate 		/*
21370Sstevel@tonic-gate 		 * If large page size was used in hat_devload(),
21380Sstevel@tonic-gate 		 * the same page size must be used in hat_unload().
21390Sstevel@tonic-gate 		 */
21400Sstevel@tonic-gate 		dhp = tdhp = devmap_find_handle(dhp_head, addr);
21410Sstevel@tonic-gate 		while (tdhp != NULL) {
21420Sstevel@tonic-gate 			if (tdhp->dh_flags & DEVMAP_FLAG_LARGE) {
21430Sstevel@tonic-gate 				break;
21440Sstevel@tonic-gate 			}
21450Sstevel@tonic-gate 			tdhp = tdhp->dh_next;
21460Sstevel@tonic-gate 		}
21470Sstevel@tonic-gate 		if (tdhp) {
21480Sstevel@tonic-gate 			size_t slen = len;
21490Sstevel@tonic-gate 			size_t mlen;
21500Sstevel@tonic-gate 			size_t soff;
21510Sstevel@tonic-gate 
21520Sstevel@tonic-gate 			soff = (ulong_t)(addr - dhp->dh_uvaddr);
21530Sstevel@tonic-gate 			while (slen != 0) {
21540Sstevel@tonic-gate 				mlen = MIN(slen, (dhp->dh_len - soff));
21550Sstevel@tonic-gate 				hat_unload(seg->s_as->a_hat, dhp->dh_uvaddr,
21560Sstevel@tonic-gate 					dhp->dh_len, HAT_UNLOAD);
21570Sstevel@tonic-gate 				dhp = dhp->dh_next;
21580Sstevel@tonic-gate 				ASSERT(slen >= mlen);
21590Sstevel@tonic-gate 				slen -= mlen;
21600Sstevel@tonic-gate 				soff = 0;
21610Sstevel@tonic-gate 			}
21620Sstevel@tonic-gate 			return (0);
21630Sstevel@tonic-gate 		}
21640Sstevel@tonic-gate 	}
21650Sstevel@tonic-gate 
21660Sstevel@tonic-gate 	if ((prot & ~PROT_USER) == PROT_NONE) {
21670Sstevel@tonic-gate 		hat_unload(seg->s_as->a_hat, addr, len, HAT_UNLOAD);
21680Sstevel@tonic-gate 	} else {
21690Sstevel@tonic-gate 		/*
21700Sstevel@tonic-gate 		 * RFE: the segment should keep track of all attributes
21710Sstevel@tonic-gate 		 * allowing us to remove the deprecated hat_chgprot
21720Sstevel@tonic-gate 		 * and use hat_chgattr.
21730Sstevel@tonic-gate 		 */
21740Sstevel@tonic-gate 		hat_chgprot(seg->s_as->a_hat, addr, len, prot);
21750Sstevel@tonic-gate 	}
21760Sstevel@tonic-gate 
21770Sstevel@tonic-gate 	return (0);
21780Sstevel@tonic-gate }
21790Sstevel@tonic-gate 
21800Sstevel@tonic-gate static int
21810Sstevel@tonic-gate segdev_checkprot(struct seg *seg, caddr_t addr, size_t len, uint_t prot)
21820Sstevel@tonic-gate {
21830Sstevel@tonic-gate 	struct segdev_data *sdp = (struct segdev_data *)seg->s_data;
21840Sstevel@tonic-gate 	struct vpage *vp, *evp;
21850Sstevel@tonic-gate 
21860Sstevel@tonic-gate 	TRACE_4(TR_FAC_DEVMAP, TR_DEVMAP_CHECKPROT,
21870Sstevel@tonic-gate 	    "segdev_checkprot:start seg=%p addr=%p len=%lx prot=%x",
21880Sstevel@tonic-gate 	    (void *)seg, (void *)addr, len, prot);
21890Sstevel@tonic-gate 	ASSERT(seg->s_as && AS_LOCK_HELD(seg->s_as, &seg->s_as->a_lock));
21900Sstevel@tonic-gate 
21910Sstevel@tonic-gate 	/*
21920Sstevel@tonic-gate 	 * If segment protection can be used, simply check against them
21930Sstevel@tonic-gate 	 */
21940Sstevel@tonic-gate 	mutex_enter(&sdp->lock);
21950Sstevel@tonic-gate 	if (sdp->pageprot == 0) {
21960Sstevel@tonic-gate 		register int err;
21970Sstevel@tonic-gate 
21980Sstevel@tonic-gate 		err = ((sdp->prot & prot) != prot) ? EACCES : 0;
21990Sstevel@tonic-gate 		mutex_exit(&sdp->lock);
22000Sstevel@tonic-gate 		return (err);
22010Sstevel@tonic-gate 	}
22020Sstevel@tonic-gate 
22030Sstevel@tonic-gate 	/*
22040Sstevel@tonic-gate 	 * Have to check down to the vpage level
22050Sstevel@tonic-gate 	 */
22060Sstevel@tonic-gate 	evp = &sdp->vpage[seg_page(seg, addr + len)];
22070Sstevel@tonic-gate 	for (vp = &sdp->vpage[seg_page(seg, addr)]; vp < evp; vp++) {
22080Sstevel@tonic-gate 		if ((VPP_PROT(vp) & prot) != prot) {
22090Sstevel@tonic-gate 			mutex_exit(&sdp->lock);
22100Sstevel@tonic-gate 			return (EACCES);
22110Sstevel@tonic-gate 		}
22120Sstevel@tonic-gate 	}
22130Sstevel@tonic-gate 	mutex_exit(&sdp->lock);
22140Sstevel@tonic-gate 	return (0);
22150Sstevel@tonic-gate }
22160Sstevel@tonic-gate 
22170Sstevel@tonic-gate static int
22180Sstevel@tonic-gate segdev_getprot(struct seg *seg, caddr_t addr, size_t len, uint_t *protv)
22190Sstevel@tonic-gate {
22200Sstevel@tonic-gate 	struct segdev_data *sdp = (struct segdev_data *)seg->s_data;
22210Sstevel@tonic-gate 	size_t pgno;
22220Sstevel@tonic-gate 
22230Sstevel@tonic-gate 	TRACE_4(TR_FAC_DEVMAP, TR_DEVMAP_GETPROT,
22240Sstevel@tonic-gate 	    "segdev_getprot:start seg=%p addr=%p len=%lx protv=%p",
22250Sstevel@tonic-gate 	    (void *)seg, (void *)addr, len, (void *)protv);
22260Sstevel@tonic-gate 	ASSERT(seg->s_as && AS_LOCK_HELD(seg->s_as, &seg->s_as->a_lock));
22270Sstevel@tonic-gate 
22280Sstevel@tonic-gate 	pgno = seg_page(seg, addr + len) - seg_page(seg, addr) + 1;
22290Sstevel@tonic-gate 	if (pgno != 0) {
22300Sstevel@tonic-gate 		mutex_enter(&sdp->lock);
22310Sstevel@tonic-gate 		if (sdp->pageprot == 0) {
22320Sstevel@tonic-gate 			do
22330Sstevel@tonic-gate 				protv[--pgno] = sdp->prot;
22340Sstevel@tonic-gate 			while (pgno != 0);
22350Sstevel@tonic-gate 		} else {
22360Sstevel@tonic-gate 			size_t pgoff = seg_page(seg, addr);
22370Sstevel@tonic-gate 
22380Sstevel@tonic-gate 			do {
22390Sstevel@tonic-gate 				pgno--;
22400Sstevel@tonic-gate 				protv[pgno] =
22410Sstevel@tonic-gate 					VPP_PROT(&sdp->vpage[pgno + pgoff]);
22420Sstevel@tonic-gate 			} while (pgno != 0);
22430Sstevel@tonic-gate 		}
22440Sstevel@tonic-gate 		mutex_exit(&sdp->lock);
22450Sstevel@tonic-gate 	}
22460Sstevel@tonic-gate 	return (0);
22470Sstevel@tonic-gate }
22480Sstevel@tonic-gate 
22490Sstevel@tonic-gate static u_offset_t
22500Sstevel@tonic-gate segdev_getoffset(register struct seg *seg, caddr_t addr)
22510Sstevel@tonic-gate {
22520Sstevel@tonic-gate 	register struct segdev_data *sdp = (struct segdev_data *)seg->s_data;
22530Sstevel@tonic-gate 
22540Sstevel@tonic-gate 	TRACE_2(TR_FAC_DEVMAP, TR_DEVMAP_GETOFFSET,
22550Sstevel@tonic-gate 	    "segdev_getoffset:start seg=%p addr=%p", (void *)seg, (void *)addr);
22560Sstevel@tonic-gate 
22570Sstevel@tonic-gate 	ASSERT(seg->s_as && AS_LOCK_HELD(seg->s_as, &seg->s_as->a_lock));
22580Sstevel@tonic-gate 
22590Sstevel@tonic-gate 	return ((u_offset_t)sdp->offset + (addr - seg->s_base));
22600Sstevel@tonic-gate }
22610Sstevel@tonic-gate 
22620Sstevel@tonic-gate /*ARGSUSED*/
22630Sstevel@tonic-gate static int
22640Sstevel@tonic-gate segdev_gettype(register struct seg *seg, caddr_t addr)
22650Sstevel@tonic-gate {
22660Sstevel@tonic-gate 	register struct segdev_data *sdp = (struct segdev_data *)seg->s_data;
22670Sstevel@tonic-gate 
22680Sstevel@tonic-gate 	TRACE_2(TR_FAC_DEVMAP, TR_DEVMAP_GETTYPE,
22690Sstevel@tonic-gate 	    "segdev_gettype:start seg=%p addr=%p", (void *)seg, (void *)addr);
22700Sstevel@tonic-gate 
22710Sstevel@tonic-gate 	ASSERT(seg->s_as && AS_LOCK_HELD(seg->s_as, &seg->s_as->a_lock));
22720Sstevel@tonic-gate 
22730Sstevel@tonic-gate 	return (sdp->type);
22740Sstevel@tonic-gate }
22750Sstevel@tonic-gate 
22760Sstevel@tonic-gate 
22770Sstevel@tonic-gate /*ARGSUSED*/
22780Sstevel@tonic-gate static int
22790Sstevel@tonic-gate segdev_getvp(register struct seg *seg, caddr_t addr, struct vnode **vpp)
22800Sstevel@tonic-gate {
22810Sstevel@tonic-gate 	register struct segdev_data *sdp = (struct segdev_data *)seg->s_data;
22820Sstevel@tonic-gate 
22830Sstevel@tonic-gate 	TRACE_2(TR_FAC_DEVMAP, TR_DEVMAP_GETVP,
22840Sstevel@tonic-gate 	    "segdev_getvp:start seg=%p addr=%p", (void *)seg, (void *)addr);
22850Sstevel@tonic-gate 
22860Sstevel@tonic-gate 	ASSERT(seg->s_as && AS_LOCK_HELD(seg->s_as, &seg->s_as->a_lock));
22870Sstevel@tonic-gate 
22880Sstevel@tonic-gate 	/*
22890Sstevel@tonic-gate 	 * Note that this vp is the common_vp of the device, where the
22900Sstevel@tonic-gate 	 * pages are hung ..
22910Sstevel@tonic-gate 	 */
22920Sstevel@tonic-gate 	*vpp = VTOCVP(sdp->vp);
22930Sstevel@tonic-gate 
22940Sstevel@tonic-gate 	return (0);
22950Sstevel@tonic-gate }
22960Sstevel@tonic-gate 
22970Sstevel@tonic-gate static void
22980Sstevel@tonic-gate segdev_badop(void)
22990Sstevel@tonic-gate {
23000Sstevel@tonic-gate 	TRACE_0(TR_FAC_DEVMAP, TR_DEVMAP_SEGDEV_BADOP,
23010Sstevel@tonic-gate 		"segdev_badop:start");
23020Sstevel@tonic-gate 	panic("segdev_badop");
23030Sstevel@tonic-gate 	/*NOTREACHED*/
23040Sstevel@tonic-gate }
23050Sstevel@tonic-gate 
23060Sstevel@tonic-gate /*
23070Sstevel@tonic-gate  * segdev pages are not in the cache, and thus can't really be controlled.
23080Sstevel@tonic-gate  * Hence, syncs are simply always successful.
23090Sstevel@tonic-gate  */
23100Sstevel@tonic-gate /*ARGSUSED*/
23110Sstevel@tonic-gate static int
23120Sstevel@tonic-gate segdev_sync(struct seg *seg, caddr_t addr, size_t len, int attr, uint_t flags)
23130Sstevel@tonic-gate {
23140Sstevel@tonic-gate 	TRACE_0(TR_FAC_DEVMAP, TR_DEVMAP_SYNC, "segdev_sync:start");
23150Sstevel@tonic-gate 
23160Sstevel@tonic-gate 	ASSERT(seg->s_as && AS_LOCK_HELD(seg->s_as, &seg->s_as->a_lock));
23170Sstevel@tonic-gate 
23180Sstevel@tonic-gate 	return (0);
23190Sstevel@tonic-gate }
23200Sstevel@tonic-gate 
23210Sstevel@tonic-gate /*
23220Sstevel@tonic-gate  * segdev pages are always "in core".
23230Sstevel@tonic-gate  */
23240Sstevel@tonic-gate /*ARGSUSED*/
23250Sstevel@tonic-gate static size_t
23260Sstevel@tonic-gate segdev_incore(struct seg *seg, caddr_t addr, size_t len, char *vec)
23270Sstevel@tonic-gate {
23280Sstevel@tonic-gate 	size_t v = 0;
23290Sstevel@tonic-gate 
23300Sstevel@tonic-gate 	TRACE_0(TR_FAC_DEVMAP, TR_DEVMAP_INCORE, "segdev_incore:start");
23310Sstevel@tonic-gate 
23320Sstevel@tonic-gate 	ASSERT(seg->s_as && AS_LOCK_HELD(seg->s_as, &seg->s_as->a_lock));
23330Sstevel@tonic-gate 
23340Sstevel@tonic-gate 	for (len = (len + PAGEOFFSET) & PAGEMASK; len; len -= PAGESIZE,
23350Sstevel@tonic-gate 	    v += PAGESIZE)
23360Sstevel@tonic-gate 		*vec++ = 1;
23370Sstevel@tonic-gate 	return (v);
23380Sstevel@tonic-gate }
23390Sstevel@tonic-gate 
23400Sstevel@tonic-gate /*
23410Sstevel@tonic-gate  * segdev pages are not in the cache, and thus can't really be controlled.
23420Sstevel@tonic-gate  * Hence, locks are simply always successful.
23430Sstevel@tonic-gate  */
23440Sstevel@tonic-gate /*ARGSUSED*/
23450Sstevel@tonic-gate static int
23460Sstevel@tonic-gate segdev_lockop(struct seg *seg, caddr_t addr,
23470Sstevel@tonic-gate     size_t len, int attr, int op, ulong_t *lockmap, size_t pos)
23480Sstevel@tonic-gate {
23490Sstevel@tonic-gate 	TRACE_0(TR_FAC_DEVMAP, TR_DEVMAP_LOCKOP, "segdev_lockop:start");
23500Sstevel@tonic-gate 
23510Sstevel@tonic-gate 	ASSERT(seg->s_as && AS_LOCK_HELD(seg->s_as, &seg->s_as->a_lock));
23520Sstevel@tonic-gate 
23530Sstevel@tonic-gate 	return (0);
23540Sstevel@tonic-gate }
23550Sstevel@tonic-gate 
23560Sstevel@tonic-gate /*
23570Sstevel@tonic-gate  * segdev pages are not in the cache, and thus can't really be controlled.
23580Sstevel@tonic-gate  * Hence, advise is simply always successful.
23590Sstevel@tonic-gate  */
23600Sstevel@tonic-gate /*ARGSUSED*/
23610Sstevel@tonic-gate static int
23620Sstevel@tonic-gate segdev_advise(struct seg *seg, caddr_t addr, size_t len, uint_t behav)
23630Sstevel@tonic-gate {
23640Sstevel@tonic-gate 	TRACE_0(TR_FAC_DEVMAP, TR_DEVMAP_ADVISE, "segdev_advise:start");
23650Sstevel@tonic-gate 
23660Sstevel@tonic-gate 	ASSERT(seg->s_as && AS_LOCK_HELD(seg->s_as, &seg->s_as->a_lock));
23670Sstevel@tonic-gate 
23680Sstevel@tonic-gate 	return (0);
23690Sstevel@tonic-gate }
23700Sstevel@tonic-gate 
23710Sstevel@tonic-gate /*
23720Sstevel@tonic-gate  * segdev pages are not dumped, so we just return
23730Sstevel@tonic-gate  */
23740Sstevel@tonic-gate /*ARGSUSED*/
23750Sstevel@tonic-gate static void
23760Sstevel@tonic-gate segdev_dump(struct seg *seg)
23770Sstevel@tonic-gate {}
23780Sstevel@tonic-gate 
23790Sstevel@tonic-gate /*
23800Sstevel@tonic-gate  * ddi_segmap_setup:	Used by drivers who wish specify mapping attributes
23810Sstevel@tonic-gate  *			for a segment.	Called from a drivers segmap(9E)
23820Sstevel@tonic-gate  *			routine.
23830Sstevel@tonic-gate  */
23840Sstevel@tonic-gate /*ARGSUSED*/
23850Sstevel@tonic-gate int
23860Sstevel@tonic-gate ddi_segmap_setup(dev_t dev, off_t offset, struct as *as, caddr_t *addrp,
23870Sstevel@tonic-gate     off_t len, uint_t prot, uint_t maxprot, uint_t flags, cred_t *cred,
23880Sstevel@tonic-gate     ddi_device_acc_attr_t *accattrp, uint_t rnumber)
23890Sstevel@tonic-gate {
23900Sstevel@tonic-gate 	struct segdev_crargs dev_a;
23910Sstevel@tonic-gate 	int (*mapfunc)(dev_t dev, off_t off, int prot);
23920Sstevel@tonic-gate 	uint_t hat_attr;
23930Sstevel@tonic-gate 	pfn_t pfn;
23940Sstevel@tonic-gate 	int	error, i;
23950Sstevel@tonic-gate 
23960Sstevel@tonic-gate 	TRACE_0(TR_FAC_DEVMAP, TR_DEVMAP_SEGMAP_SETUP,
23970Sstevel@tonic-gate 	    "ddi_segmap_setup:start");
23980Sstevel@tonic-gate 
23990Sstevel@tonic-gate 	if ((mapfunc = devopsp[getmajor(dev)]->devo_cb_ops->cb_mmap) == nodev)
24000Sstevel@tonic-gate 		return (ENODEV);
24010Sstevel@tonic-gate 
24020Sstevel@tonic-gate 	/*
24030Sstevel@tonic-gate 	 * Character devices that support the d_mmap
24040Sstevel@tonic-gate 	 * interface can only be mmap'ed shared.
24050Sstevel@tonic-gate 	 */
24060Sstevel@tonic-gate 	if ((flags & MAP_TYPE) != MAP_SHARED)
24070Sstevel@tonic-gate 		return (EINVAL);
24080Sstevel@tonic-gate 
24090Sstevel@tonic-gate 	/*
24100Sstevel@tonic-gate 	 * Check that this region is indeed mappable on this platform.
24110Sstevel@tonic-gate 	 * Use the mapping function.
24120Sstevel@tonic-gate 	 */
24130Sstevel@tonic-gate 	if (ddi_device_mapping_check(dev, accattrp, rnumber, &hat_attr) == -1)
24140Sstevel@tonic-gate 		return (ENXIO);
24150Sstevel@tonic-gate 
24160Sstevel@tonic-gate 	/*
24170Sstevel@tonic-gate 	 * Check to ensure that the entire range is
24180Sstevel@tonic-gate 	 * legal and we are not trying to map in
24190Sstevel@tonic-gate 	 * more than the device will let us.
24200Sstevel@tonic-gate 	 */
24210Sstevel@tonic-gate 	for (i = 0; i < len; i += PAGESIZE) {
24220Sstevel@tonic-gate 		if (i == 0) {
24230Sstevel@tonic-gate 			/*
24240Sstevel@tonic-gate 			 * Save the pfn at offset here. This pfn will be
24250Sstevel@tonic-gate 			 * used later to get user address.
24260Sstevel@tonic-gate 			 */
24270Sstevel@tonic-gate 			if ((pfn = (pfn_t)cdev_mmap(mapfunc, dev, offset,
24280Sstevel@tonic-gate 				maxprot)) == PFN_INVALID)
24290Sstevel@tonic-gate 				return (ENXIO);
24300Sstevel@tonic-gate 		} else {
24310Sstevel@tonic-gate 			if (cdev_mmap(mapfunc, dev, offset + i, maxprot) ==
24320Sstevel@tonic-gate 				PFN_INVALID)
24330Sstevel@tonic-gate 				return (ENXIO);
24340Sstevel@tonic-gate 		}
24350Sstevel@tonic-gate 	}
24360Sstevel@tonic-gate 
24370Sstevel@tonic-gate 	as_rangelock(as);
24380Sstevel@tonic-gate 	if ((flags & MAP_FIXED) == 0) {
24390Sstevel@tonic-gate 		/*
24400Sstevel@tonic-gate 		 * Pick an address w/o worrying about
24410Sstevel@tonic-gate 		 * any vac alignment constraints.
24420Sstevel@tonic-gate 		 */
24430Sstevel@tonic-gate 		map_addr(addrp, len, ptob(pfn), 0, flags);
24440Sstevel@tonic-gate 		if (*addrp == NULL) {
24450Sstevel@tonic-gate 			as_rangeunlock(as);
24460Sstevel@tonic-gate 			return (ENOMEM);
24470Sstevel@tonic-gate 		}
24480Sstevel@tonic-gate 	} else {
24490Sstevel@tonic-gate 		/*
24500Sstevel@tonic-gate 		 * User-specified address; blow away any previous mappings.
24510Sstevel@tonic-gate 		 */
24520Sstevel@tonic-gate 		(void) as_unmap(as, *addrp, len);
24530Sstevel@tonic-gate 	}
24540Sstevel@tonic-gate 
24550Sstevel@tonic-gate 	dev_a.mapfunc = mapfunc;
24560Sstevel@tonic-gate 	dev_a.dev = dev;
24570Sstevel@tonic-gate 	dev_a.offset = (offset_t)offset;
24580Sstevel@tonic-gate 	dev_a.type = flags & MAP_TYPE;
24590Sstevel@tonic-gate 	dev_a.prot = (uchar_t)prot;
24600Sstevel@tonic-gate 	dev_a.maxprot = (uchar_t)maxprot;
24610Sstevel@tonic-gate 	dev_a.hat_attr = hat_attr;
24620Sstevel@tonic-gate 	dev_a.hat_flags = 0;
24630Sstevel@tonic-gate 	dev_a.devmap_data = NULL;
24640Sstevel@tonic-gate 
24650Sstevel@tonic-gate 	error = as_map(as, *addrp, len, segdev_create, &dev_a);
24660Sstevel@tonic-gate 	as_rangeunlock(as);
24670Sstevel@tonic-gate 	return (error);
24680Sstevel@tonic-gate 
24690Sstevel@tonic-gate }
24700Sstevel@tonic-gate 
24710Sstevel@tonic-gate /*ARGSUSED*/
24720Sstevel@tonic-gate static int
24730Sstevel@tonic-gate segdev_pagelock(struct seg *seg, caddr_t addr, size_t len,
24740Sstevel@tonic-gate     struct page ***ppp, enum lock_type type, enum seg_rw rw)
24750Sstevel@tonic-gate {
24760Sstevel@tonic-gate 	TRACE_0(TR_FAC_DEVMAP, TR_DEVMAP_PAGELOCK,
24770Sstevel@tonic-gate 	    "segdev_pagelock:start");
24780Sstevel@tonic-gate 	return (ENOTSUP);
24790Sstevel@tonic-gate }
24800Sstevel@tonic-gate 
24810Sstevel@tonic-gate /*ARGSUSED*/
24820Sstevel@tonic-gate static int
24830Sstevel@tonic-gate segdev_setpagesize(struct seg *seg, caddr_t addr, size_t len,
24840Sstevel@tonic-gate     uint_t szc)
24850Sstevel@tonic-gate {
24860Sstevel@tonic-gate 	return (ENOTSUP);
24870Sstevel@tonic-gate }
24880Sstevel@tonic-gate 
24890Sstevel@tonic-gate /*
24900Sstevel@tonic-gate  * devmap_device: Used by devmap framework to establish mapping
24910Sstevel@tonic-gate  *                called by devmap_seup(9F) during map setup time.
24920Sstevel@tonic-gate  */
24930Sstevel@tonic-gate /*ARGSUSED*/
24940Sstevel@tonic-gate static int
24950Sstevel@tonic-gate devmap_device(devmap_handle_t *dhp, struct as *as, caddr_t *addr,
24960Sstevel@tonic-gate     offset_t off, size_t len, uint_t flags)
24970Sstevel@tonic-gate {
24980Sstevel@tonic-gate 	devmap_handle_t *rdhp, *maxdhp;
24990Sstevel@tonic-gate 	struct segdev_crargs dev_a;
25000Sstevel@tonic-gate 	int	err;
25010Sstevel@tonic-gate 	uint_t maxprot = PROT_ALL;
25020Sstevel@tonic-gate 	offset_t offset = 0;
25030Sstevel@tonic-gate 	pfn_t pfn;
25040Sstevel@tonic-gate 	struct devmap_pmem_cookie *pcp;
25050Sstevel@tonic-gate 
25060Sstevel@tonic-gate 	TRACE_4(TR_FAC_DEVMAP, TR_DEVMAP_DEVICE,
25070Sstevel@tonic-gate 	    "devmap_device:start dhp=%p addr=%p off=%llx, len=%lx",
25080Sstevel@tonic-gate 	    (void *)dhp, (void *)addr, off, len);
25090Sstevel@tonic-gate 
25100Sstevel@tonic-gate 	DEBUGF(2, (CE_CONT, "devmap_device: dhp %p addr %p off %llx len %lx\n",
25110Sstevel@tonic-gate 	    (void *)dhp, (void *)addr, off, len));
25120Sstevel@tonic-gate 
25130Sstevel@tonic-gate 	as_rangelock(as);
25140Sstevel@tonic-gate 	if ((flags & MAP_FIXED) == 0) {
25150Sstevel@tonic-gate 		offset_t aligned_off;
25160Sstevel@tonic-gate 
25170Sstevel@tonic-gate 		rdhp = maxdhp = dhp;
25180Sstevel@tonic-gate 		while (rdhp != NULL) {
25190Sstevel@tonic-gate 			maxdhp = (maxdhp->dh_len > rdhp->dh_len) ?
25200Sstevel@tonic-gate 				maxdhp : rdhp;
25210Sstevel@tonic-gate 			rdhp = rdhp->dh_next;
25220Sstevel@tonic-gate 			maxprot |= dhp->dh_maxprot;
25230Sstevel@tonic-gate 		}
25240Sstevel@tonic-gate 		offset = maxdhp->dh_uoff - dhp->dh_uoff;
25250Sstevel@tonic-gate 
25260Sstevel@tonic-gate 		/*
25270Sstevel@tonic-gate 		 * Use the dhp that has the
25280Sstevel@tonic-gate 		 * largest len to get user address.
25290Sstevel@tonic-gate 		 */
25300Sstevel@tonic-gate 		/*
25310Sstevel@tonic-gate 		 * If MAPPING_INVALID, cannot use dh_pfn/dh_cvaddr,
25320Sstevel@tonic-gate 		 * use 0 which is as good as any other.
25330Sstevel@tonic-gate 		 */
25340Sstevel@tonic-gate 		if (maxdhp->dh_flags & DEVMAP_MAPPING_INVALID) {
25350Sstevel@tonic-gate 			aligned_off = (offset_t)0;
25360Sstevel@tonic-gate 		} else if (dhp_is_devmem(maxdhp)) {
25370Sstevel@tonic-gate 			aligned_off = (offset_t)ptob(maxdhp->dh_pfn) - offset;
25380Sstevel@tonic-gate 		} else if (dhp_is_pmem(maxdhp)) {
25390Sstevel@tonic-gate 			pcp = (struct devmap_pmem_cookie *)maxdhp->dh_pcookie;
25400Sstevel@tonic-gate 			pfn = page_pptonum(
25410Sstevel@tonic-gate 			    pcp->dp_pparray[btop(maxdhp->dh_roff)]);
25420Sstevel@tonic-gate 			aligned_off = (offset_t)ptob(pfn) - offset;
25430Sstevel@tonic-gate 		} else {
25440Sstevel@tonic-gate 			aligned_off = (offset_t)(uintptr_t)maxdhp->dh_cvaddr -
25450Sstevel@tonic-gate 			    offset;
25460Sstevel@tonic-gate 		}
25470Sstevel@tonic-gate 
25480Sstevel@tonic-gate 		/*
25490Sstevel@tonic-gate 		 * Pick an address aligned to dh_cookie.
25500Sstevel@tonic-gate 		 * for kernel memory/user memory, cookie is cvaddr.
25510Sstevel@tonic-gate 		 * for device memory, cookie is physical address.
25520Sstevel@tonic-gate 		 */
25530Sstevel@tonic-gate 		map_addr(addr, len, aligned_off, 1, flags);
25540Sstevel@tonic-gate 		if (*addr == NULL) {
25550Sstevel@tonic-gate 			as_rangeunlock(as);
25560Sstevel@tonic-gate 			return (ENOMEM);
25570Sstevel@tonic-gate 		}
25580Sstevel@tonic-gate 	} else {
25590Sstevel@tonic-gate 		/*
25600Sstevel@tonic-gate 		 * User-specified address; blow away any previous mappings.
25610Sstevel@tonic-gate 		 */
25620Sstevel@tonic-gate 		(void) as_unmap(as, *addr, len);
25630Sstevel@tonic-gate 	}
25640Sstevel@tonic-gate 
25650Sstevel@tonic-gate 	dev_a.mapfunc = NULL;
25660Sstevel@tonic-gate 	dev_a.dev = dhp->dh_dev;
25670Sstevel@tonic-gate 	dev_a.type = flags & MAP_TYPE;
25680Sstevel@tonic-gate 	dev_a.offset = off;
25690Sstevel@tonic-gate 	/*
25700Sstevel@tonic-gate 	 * sdp->maxprot has the least restrict protection of all dhps.
25710Sstevel@tonic-gate 	 */
25720Sstevel@tonic-gate 	dev_a.maxprot = maxprot;
25730Sstevel@tonic-gate 	dev_a.prot = dhp->dh_prot;
25740Sstevel@tonic-gate 	/*
25750Sstevel@tonic-gate 	 * devmap uses dhp->dh_hat_attr for hat.
25760Sstevel@tonic-gate 	 */
25770Sstevel@tonic-gate 	dev_a.hat_flags = 0;
25780Sstevel@tonic-gate 	dev_a.hat_attr = 0;
25790Sstevel@tonic-gate 	dev_a.devmap_data = (void *)dhp;
25800Sstevel@tonic-gate 
25810Sstevel@tonic-gate 	err = as_map(as, *addr, len, segdev_create, &dev_a);
25820Sstevel@tonic-gate 	as_rangeunlock(as);
25830Sstevel@tonic-gate 	return (err);
25840Sstevel@tonic-gate }
25850Sstevel@tonic-gate 
25860Sstevel@tonic-gate int
25870Sstevel@tonic-gate devmap_do_ctxmgt(devmap_cookie_t dhc, void *pvtp, offset_t off, size_t len,
25880Sstevel@tonic-gate     uint_t type, uint_t rw, int (*ctxmgt)(devmap_cookie_t, void *, offset_t,
25890Sstevel@tonic-gate     size_t, uint_t, uint_t))
25900Sstevel@tonic-gate {
25910Sstevel@tonic-gate 	register devmap_handle_t *dhp = (devmap_handle_t *)dhc;
25920Sstevel@tonic-gate 	struct devmap_ctx *devctx;
25930Sstevel@tonic-gate 	int do_timeout = 0;
25940Sstevel@tonic-gate 	int ret;
25950Sstevel@tonic-gate 
25960Sstevel@tonic-gate #ifdef lint
25970Sstevel@tonic-gate 	pvtp = pvtp;
25980Sstevel@tonic-gate #endif
25990Sstevel@tonic-gate 
26000Sstevel@tonic-gate 	TRACE_3(TR_FAC_DEVMAP, TR_DEVMAP_DO_CTXMGT,
26010Sstevel@tonic-gate 	    "devmap_do_ctxmgt:start dhp=%p off=%llx, len=%lx",
26020Sstevel@tonic-gate 	    (void *)dhp, off, len);
26030Sstevel@tonic-gate 	DEBUGF(7, (CE_CONT, "devmap_do_ctxmgt: dhp %p off %llx len %lx\n",
26040Sstevel@tonic-gate 	    (void *)dhp, off, len));
26050Sstevel@tonic-gate 
26060Sstevel@tonic-gate 	if (ctxmgt == NULL)
26070Sstevel@tonic-gate 		return (FC_HWERR);
26080Sstevel@tonic-gate 
26090Sstevel@tonic-gate 	devctx = dhp->dh_ctx;
26100Sstevel@tonic-gate 
26110Sstevel@tonic-gate 	/*
26120Sstevel@tonic-gate 	 * If we are on an MP system with more than one cpu running
26130Sstevel@tonic-gate 	 * and if a thread on some CPU already has the context, wait
26140Sstevel@tonic-gate 	 * for it to finish if there is a hysteresis timeout.
26150Sstevel@tonic-gate 	 *
26160Sstevel@tonic-gate 	 * We call cv_wait() instead of cv_wait_sig() because
26170Sstevel@tonic-gate 	 * it does not matter much if it returned due to a signal
26180Sstevel@tonic-gate 	 * or due to a cv_signal() or cv_broadcast().  In either event
26190Sstevel@tonic-gate 	 * we need to complete the mapping otherwise the processes
26200Sstevel@tonic-gate 	 * will die with a SEGV.
26210Sstevel@tonic-gate 	 */
26220Sstevel@tonic-gate 	if ((dhp->dh_timeout_length > 0) && (ncpus > 1)) {
26230Sstevel@tonic-gate 		TRACE_2(TR_FAC_DEVMAP, TR_DEVMAP_DO_CTXMGT_CK1,
26240Sstevel@tonic-gate 		    "devmap_do_ctxmgt:doing hysteresis, devctl %p dhp %p",
26250Sstevel@tonic-gate 		    devctx, dhp);
26260Sstevel@tonic-gate 		do_timeout = 1;
26270Sstevel@tonic-gate 		mutex_enter(&devctx->lock);
26280Sstevel@tonic-gate 		while (devctx->oncpu)
26290Sstevel@tonic-gate 			cv_wait(&devctx->cv, &devctx->lock);
26300Sstevel@tonic-gate 		devctx->oncpu = 1;
26310Sstevel@tonic-gate 		mutex_exit(&devctx->lock);
26320Sstevel@tonic-gate 	}
26330Sstevel@tonic-gate 
26340Sstevel@tonic-gate 	/*
26350Sstevel@tonic-gate 	 * Call the contextmgt callback so that the driver can handle
26360Sstevel@tonic-gate 	 * the fault.
26370Sstevel@tonic-gate 	 */
26380Sstevel@tonic-gate 	ret = (*ctxmgt)(dhp, dhp->dh_pvtp, off, len, type, rw);
26390Sstevel@tonic-gate 
26400Sstevel@tonic-gate 	/*
26410Sstevel@tonic-gate 	 * If devmap_access() returned -1, then there was a hardware
26420Sstevel@tonic-gate 	 * error so we need to convert the return value to something
26430Sstevel@tonic-gate 	 * that trap() will understand.  Otherwise, the return value
26440Sstevel@tonic-gate 	 * is already a fault code generated by devmap_unload()
26450Sstevel@tonic-gate 	 * or devmap_load().
26460Sstevel@tonic-gate 	 */
26470Sstevel@tonic-gate 	if (ret) {
26480Sstevel@tonic-gate 		TRACE_3(TR_FAC_DEVMAP, TR_DEVMAP_DO_CTXMGT_CK2,
26490Sstevel@tonic-gate 		    "devmap_do_ctxmgt: ret=%x dhp=%p devctx=%p",
26500Sstevel@tonic-gate 		    ret, dhp, devctx);
26510Sstevel@tonic-gate 		DEBUGF(1, (CE_CONT, "devmap_do_ctxmgt: ret %x dhp %p\n",
26520Sstevel@tonic-gate 		    ret, (void *)dhp));
26530Sstevel@tonic-gate 		if (devctx->oncpu) {
26540Sstevel@tonic-gate 			mutex_enter(&devctx->lock);
26550Sstevel@tonic-gate 			devctx->oncpu = 0;
26560Sstevel@tonic-gate 			cv_signal(&devctx->cv);
26570Sstevel@tonic-gate 			mutex_exit(&devctx->lock);
26580Sstevel@tonic-gate 		}
26590Sstevel@tonic-gate 		return (FC_HWERR);
26600Sstevel@tonic-gate 	}
26610Sstevel@tonic-gate 
26620Sstevel@tonic-gate 	/*
26630Sstevel@tonic-gate 	 * Setup the timeout if we need to
26640Sstevel@tonic-gate 	 */
26650Sstevel@tonic-gate 	if (do_timeout) {
26660Sstevel@tonic-gate 		mutex_enter(&devctx->lock);
26670Sstevel@tonic-gate 		if (dhp->dh_timeout_length > 0) {
26680Sstevel@tonic-gate 			TRACE_0(TR_FAC_DEVMAP, TR_DEVMAP_DO_CTXMGT_CK3,
26690Sstevel@tonic-gate 			    "devmap_do_ctxmgt:timeout set");
26700Sstevel@tonic-gate 			devctx->timeout = timeout(devmap_ctxto,
26710Sstevel@tonic-gate 			    devctx, dhp->dh_timeout_length);
26720Sstevel@tonic-gate 		} else {
26730Sstevel@tonic-gate 			/*
26740Sstevel@tonic-gate 			 * We don't want to wait so set oncpu to
26750Sstevel@tonic-gate 			 * 0 and wake up anyone waiting.
26760Sstevel@tonic-gate 			 */
26770Sstevel@tonic-gate 			TRACE_0(TR_FAC_DEVMAP, TR_DEVMAP_DO_CTXMGT_CK4,
26780Sstevel@tonic-gate 			    "devmap_do_ctxmgt:timeout not set");
26790Sstevel@tonic-gate 			devctx->oncpu = 0;
26800Sstevel@tonic-gate 			cv_signal(&devctx->cv);
26810Sstevel@tonic-gate 		}
26820Sstevel@tonic-gate 		mutex_exit(&devctx->lock);
26830Sstevel@tonic-gate 	}
26840Sstevel@tonic-gate 
26850Sstevel@tonic-gate 	return (DDI_SUCCESS);
26860Sstevel@tonic-gate }
26870Sstevel@tonic-gate 
26880Sstevel@tonic-gate /*
26890Sstevel@tonic-gate  *                                       end of mapping
26900Sstevel@tonic-gate  *                    poff   fault_offset         |
26910Sstevel@tonic-gate  *            base     |        |                 |
26920Sstevel@tonic-gate  *              |      |        |                 |
26930Sstevel@tonic-gate  *              V      V        V                 V
26940Sstevel@tonic-gate  *  +-----------+---------------+-------+---------+-------+
26950Sstevel@tonic-gate  *              ^               ^       ^         ^
26960Sstevel@tonic-gate  *              |<--- offset--->|<-len->|         |
26970Sstevel@tonic-gate  *              |<--- dh_len(size of mapping) --->|
26980Sstevel@tonic-gate  *                     |<--  pg -->|
26990Sstevel@tonic-gate  *                              -->|rlen|<--
27000Sstevel@tonic-gate  */
27010Sstevel@tonic-gate static ulong_t
27020Sstevel@tonic-gate devmap_roundup(devmap_handle_t *dhp, ulong_t offset, size_t len,
27030Sstevel@tonic-gate     ulong_t *opfn, ulong_t *pagesize)
27040Sstevel@tonic-gate {
27050Sstevel@tonic-gate 	register int level;
27060Sstevel@tonic-gate 	ulong_t pg;
27070Sstevel@tonic-gate 	ulong_t poff;
27080Sstevel@tonic-gate 	ulong_t base;
27090Sstevel@tonic-gate 	caddr_t uvaddr;
27100Sstevel@tonic-gate 	long rlen;
27110Sstevel@tonic-gate 
27120Sstevel@tonic-gate 	TRACE_3(TR_FAC_DEVMAP, TR_DEVMAP_ROUNDUP,
27130Sstevel@tonic-gate 	    "devmap_roundup:start dhp=%p off=%lx len=%lx",
27140Sstevel@tonic-gate 	    (void *)dhp, offset, len);
27150Sstevel@tonic-gate 	DEBUGF(2, (CE_CONT, "devmap_roundup: dhp %p off %lx len %lx\n",
27160Sstevel@tonic-gate 	    (void *)dhp, offset, len));
27170Sstevel@tonic-gate 
27180Sstevel@tonic-gate 	/*
27190Sstevel@tonic-gate 	 * get the max. pagesize that is aligned within the range
27200Sstevel@tonic-gate 	 * <dh_pfn, dh_pfn+offset>.
27210Sstevel@tonic-gate 	 *
27220Sstevel@tonic-gate 	 * The calculations below use physical address to ddetermine
27230Sstevel@tonic-gate 	 * the page size to use. The same calculations can use the
27240Sstevel@tonic-gate 	 * virtual address to determine the page size.
27250Sstevel@tonic-gate 	 */
27260Sstevel@tonic-gate 	base = (ulong_t)ptob(dhp->dh_pfn);
27270Sstevel@tonic-gate 	for (level = dhp->dh_mmulevel; level >= 0; level--) {
27280Sstevel@tonic-gate 		pg = page_get_pagesize(level);
27290Sstevel@tonic-gate 		poff = ((base + offset) & ~(pg - 1));
27300Sstevel@tonic-gate 		uvaddr = dhp->dh_uvaddr + (poff - base);
27310Sstevel@tonic-gate 		if ((poff >= base) &&
27320Sstevel@tonic-gate 		    ((poff + pg) <= (base + dhp->dh_len)) &&
27330Sstevel@tonic-gate 		    VA_PA_ALIGNED((uintptr_t)uvaddr, poff, pg))
27340Sstevel@tonic-gate 			break;
27350Sstevel@tonic-gate 	}
27360Sstevel@tonic-gate 
27370Sstevel@tonic-gate 	TRACE_3(TR_FAC_DEVMAP, TR_DEVMAP_ROUNDUP_CK1,
27380Sstevel@tonic-gate 	    "devmap_roundup: base=%lx poff=%lx dhp=%p",
27390Sstevel@tonic-gate 	    base, poff, dhp);
27400Sstevel@tonic-gate 	DEBUGF(2, (CE_CONT, "devmap_roundup: base %lx poff %lx pfn %lx\n",
27410Sstevel@tonic-gate 	    base, poff, dhp->dh_pfn));
27420Sstevel@tonic-gate 
27430Sstevel@tonic-gate 	ASSERT(VA_PA_ALIGNED((uintptr_t)uvaddr, poff, pg));
27440Sstevel@tonic-gate 	ASSERT(level >= 0);
27450Sstevel@tonic-gate 
27460Sstevel@tonic-gate 	*pagesize = pg;
27470Sstevel@tonic-gate 	*opfn = dhp->dh_pfn + btop(poff - base);
27480Sstevel@tonic-gate 
27490Sstevel@tonic-gate 	rlen = len + offset - (poff - base + pg);
27500Sstevel@tonic-gate 
27510Sstevel@tonic-gate 	ASSERT(rlen < (long)len);
27520Sstevel@tonic-gate 
27530Sstevel@tonic-gate 	TRACE_5(TR_FAC_DEVMAP, TR_DEVMAP_ROUNDUP_CK2,
27540Sstevel@tonic-gate 	    "devmap_roundup:ret dhp=%p level=%x rlen=%lx psiz=%p opfn=%p",
27550Sstevel@tonic-gate 	    (void *)dhp, level, rlen, pagesize, opfn);
27560Sstevel@tonic-gate 	DEBUGF(1, (CE_CONT, "devmap_roundup: dhp %p "
27570Sstevel@tonic-gate 	    "level %x rlen %lx psize %lx opfn %lx\n",
27580Sstevel@tonic-gate 	    (void *)dhp, level, rlen, *pagesize, *opfn));
27590Sstevel@tonic-gate 
27600Sstevel@tonic-gate 	return ((ulong_t)((rlen > 0) ? rlen : 0));
27610Sstevel@tonic-gate }
27620Sstevel@tonic-gate 
27630Sstevel@tonic-gate /*
27640Sstevel@tonic-gate  * find the dhp that contains addr.
27650Sstevel@tonic-gate  */
27660Sstevel@tonic-gate static devmap_handle_t *
27670Sstevel@tonic-gate devmap_find_handle(devmap_handle_t *dhp_head, caddr_t addr)
27680Sstevel@tonic-gate {
27690Sstevel@tonic-gate 	devmap_handle_t *dhp;
27700Sstevel@tonic-gate 
27710Sstevel@tonic-gate 	TRACE_0(TR_FAC_DEVMAP, TR_DEVMAP_FIND_HANDLE,
27720Sstevel@tonic-gate 	    "devmap_find_handle:start");
27730Sstevel@tonic-gate 
27740Sstevel@tonic-gate 	dhp = dhp_head;
27750Sstevel@tonic-gate 	while (dhp) {
27760Sstevel@tonic-gate 		if (addr >= dhp->dh_uvaddr &&
27770Sstevel@tonic-gate 		    addr < (dhp->dh_uvaddr + dhp->dh_len))
27780Sstevel@tonic-gate 			return (dhp);
27790Sstevel@tonic-gate 		dhp = dhp->dh_next;
27800Sstevel@tonic-gate 	}
27810Sstevel@tonic-gate 
27820Sstevel@tonic-gate 	return ((devmap_handle_t *)NULL);
27830Sstevel@tonic-gate }
27840Sstevel@tonic-gate 
27850Sstevel@tonic-gate /*
27860Sstevel@tonic-gate  * devmap_unload:
27870Sstevel@tonic-gate  *			Marks a segdev segment or pages if offset->offset+len
27880Sstevel@tonic-gate  *			is not the entire segment as intercept and unloads the
27890Sstevel@tonic-gate  *			pages in the range offset -> offset+len.
27900Sstevel@tonic-gate  */
27910Sstevel@tonic-gate int
27920Sstevel@tonic-gate devmap_unload(devmap_cookie_t dhc, offset_t offset, size_t len)
27930Sstevel@tonic-gate {
27940Sstevel@tonic-gate 	register devmap_handle_t *dhp = (devmap_handle_t *)dhc;
27950Sstevel@tonic-gate 	caddr_t	addr;
27960Sstevel@tonic-gate 	ulong_t	size;
27970Sstevel@tonic-gate 	ssize_t	soff;
27980Sstevel@tonic-gate 
27990Sstevel@tonic-gate 	TRACE_3(TR_FAC_DEVMAP, TR_DEVMAP_UNLOAD,
28000Sstevel@tonic-gate 	    "devmap_unload:start dhp=%p offset=%llx len=%lx",
28010Sstevel@tonic-gate 	    (void *)dhp, offset, len);
28020Sstevel@tonic-gate 	DEBUGF(7, (CE_CONT, "devmap_unload: dhp %p offset %llx len %lx\n",
28030Sstevel@tonic-gate 	    (void *)dhp, offset, len));
28040Sstevel@tonic-gate 
28050Sstevel@tonic-gate 	soff = (ssize_t)(offset - dhp->dh_uoff);
28060Sstevel@tonic-gate 	soff = round_down_p2(soff, PAGESIZE);
28070Sstevel@tonic-gate 	if (soff < 0 || soff >= dhp->dh_len)
28080Sstevel@tonic-gate 		return (FC_MAKE_ERR(EINVAL));
28090Sstevel@tonic-gate 
28100Sstevel@tonic-gate 	/*
28110Sstevel@tonic-gate 	 * Address and size must be page aligned.  Len is set to the
28120Sstevel@tonic-gate 	 * number of bytes in the number of pages that are required to
28130Sstevel@tonic-gate 	 * support len.  Offset is set to the byte offset of the first byte
28140Sstevel@tonic-gate 	 * of the page that contains offset.
28150Sstevel@tonic-gate 	 */
28160Sstevel@tonic-gate 	len = round_up_p2(len, PAGESIZE);
28170Sstevel@tonic-gate 
28180Sstevel@tonic-gate 	/*
28190Sstevel@tonic-gate 	 * If len is == 0, then calculate the size by getting
28200Sstevel@tonic-gate 	 * the number of bytes from offset to the end of the segment.
28210Sstevel@tonic-gate 	 */
28220Sstevel@tonic-gate 	if (len == 0)
28230Sstevel@tonic-gate 		size = dhp->dh_len - soff;
28240Sstevel@tonic-gate 	else {
28250Sstevel@tonic-gate 		size = len;
28260Sstevel@tonic-gate 		if ((soff + size) > dhp->dh_len)
28270Sstevel@tonic-gate 			return (FC_MAKE_ERR(EINVAL));
28280Sstevel@tonic-gate 	}
28290Sstevel@tonic-gate 
28300Sstevel@tonic-gate 	/*
28310Sstevel@tonic-gate 	 * The address is offset bytes from the base address of
28320Sstevel@tonic-gate 	 * the dhp.
28330Sstevel@tonic-gate 	 */
28340Sstevel@tonic-gate 	addr = (caddr_t)(soff + dhp->dh_uvaddr);
28350Sstevel@tonic-gate 
28360Sstevel@tonic-gate 	/*
28370Sstevel@tonic-gate 	 * If large page size was used in hat_devload(),
28380Sstevel@tonic-gate 	 * the same page size must be used in hat_unload().
28390Sstevel@tonic-gate 	 */
28400Sstevel@tonic-gate 	if (dhp->dh_flags & DEVMAP_FLAG_LARGE) {
28410Sstevel@tonic-gate 		hat_unload(dhp->dh_seg->s_as->a_hat, dhp->dh_uvaddr,
28420Sstevel@tonic-gate 			dhp->dh_len, HAT_UNLOAD|HAT_UNLOAD_OTHER);
28430Sstevel@tonic-gate 	} else {
28440Sstevel@tonic-gate 		hat_unload(dhp->dh_seg->s_as->a_hat,  addr, size,
28450Sstevel@tonic-gate 			HAT_UNLOAD|HAT_UNLOAD_OTHER);
28460Sstevel@tonic-gate 	}
28470Sstevel@tonic-gate 
28480Sstevel@tonic-gate 	return (0);
28490Sstevel@tonic-gate }
28500Sstevel@tonic-gate 
28510Sstevel@tonic-gate /*
28520Sstevel@tonic-gate  * calculates the optimal page size that will be used for hat_devload().
28530Sstevel@tonic-gate  */
28540Sstevel@tonic-gate static void
28550Sstevel@tonic-gate devmap_get_large_pgsize(devmap_handle_t *dhp, size_t len, caddr_t addr,
28560Sstevel@tonic-gate     size_t *llen, caddr_t *laddr)
28570Sstevel@tonic-gate {
28580Sstevel@tonic-gate 	ulong_t off;
28590Sstevel@tonic-gate 	ulong_t pfn;
28600Sstevel@tonic-gate 	ulong_t pgsize;
28610Sstevel@tonic-gate 	uint_t first = 1;
28620Sstevel@tonic-gate 
28630Sstevel@tonic-gate 	TRACE_0(TR_FAC_DEVMAP, TR_DEVMAP_GET_LARGE_PGSIZE,
28640Sstevel@tonic-gate 	    "devmap_get_large_pgsize:start");
28650Sstevel@tonic-gate 
28660Sstevel@tonic-gate 	/*
28670Sstevel@tonic-gate 	 * RFE - Code only supports large page mappings for devmem
28680Sstevel@tonic-gate 	 * This code could be changed in future if we want to support
28690Sstevel@tonic-gate 	 * large page mappings for kernel exported memory.
28700Sstevel@tonic-gate 	 */
28710Sstevel@tonic-gate 	ASSERT(dhp_is_devmem(dhp));
28720Sstevel@tonic-gate 	ASSERT(!(dhp->dh_flags & DEVMAP_MAPPING_INVALID));
28730Sstevel@tonic-gate 
28740Sstevel@tonic-gate 	*llen = 0;
28750Sstevel@tonic-gate 	off = (ulong_t)(addr - dhp->dh_uvaddr);
28760Sstevel@tonic-gate 	while ((long)len > 0) {
28770Sstevel@tonic-gate 		/*
28780Sstevel@tonic-gate 		 * get the optimal pfn to minimize address translations.
28790Sstevel@tonic-gate 		 * devmap_roundup() returns residue bytes for next round
28800Sstevel@tonic-gate 		 * calculations.
28810Sstevel@tonic-gate 		 */
28820Sstevel@tonic-gate 		len = devmap_roundup(dhp, off, len, &pfn, &pgsize);
28830Sstevel@tonic-gate 
28840Sstevel@tonic-gate 		if (first) {
28850Sstevel@tonic-gate 			*laddr = dhp->dh_uvaddr + ptob(pfn - dhp->dh_pfn);
28860Sstevel@tonic-gate 			first = 0;
28870Sstevel@tonic-gate 		}
28880Sstevel@tonic-gate 
28890Sstevel@tonic-gate 		*llen += pgsize;
28900Sstevel@tonic-gate 		off = ptob(pfn - dhp->dh_pfn) + pgsize;
28910Sstevel@tonic-gate 	}
28920Sstevel@tonic-gate 	/* Large page mapping len/addr cover more range than orginal fault */
28930Sstevel@tonic-gate 	ASSERT(*llen >= len && *laddr <= addr);
28940Sstevel@tonic-gate 	ASSERT((*laddr + *llen) >= (addr + len));
28950Sstevel@tonic-gate }
28960Sstevel@tonic-gate 
28970Sstevel@tonic-gate /*
28980Sstevel@tonic-gate  * Initialize the devmap_softlock structure.
28990Sstevel@tonic-gate  */
29000Sstevel@tonic-gate static struct devmap_softlock *
29010Sstevel@tonic-gate devmap_softlock_init(dev_t dev, ulong_t id)
29020Sstevel@tonic-gate {
29030Sstevel@tonic-gate 	struct devmap_softlock *slock;
29040Sstevel@tonic-gate 	struct devmap_softlock *tmp;
29050Sstevel@tonic-gate 
29060Sstevel@tonic-gate 	TRACE_0(TR_FAC_DEVMAP, TR_DEVMAP_SOFTLOCK_INIT,
29070Sstevel@tonic-gate 	    "devmap_softlock_init:start");
29080Sstevel@tonic-gate 
29090Sstevel@tonic-gate 	tmp = kmem_zalloc(sizeof (struct devmap_softlock), KM_SLEEP);
29100Sstevel@tonic-gate 	mutex_enter(&devmap_slock);
29110Sstevel@tonic-gate 
29120Sstevel@tonic-gate 	for (slock = devmap_slist; slock != NULL; slock = slock->next)
29130Sstevel@tonic-gate 		if ((slock->dev == dev) && (slock->id == id))
29140Sstevel@tonic-gate 			break;
29150Sstevel@tonic-gate 
29160Sstevel@tonic-gate 	if (slock == NULL) {
29170Sstevel@tonic-gate 		slock = tmp;
29180Sstevel@tonic-gate 		slock->dev = dev;
29190Sstevel@tonic-gate 		slock->id = id;
29200Sstevel@tonic-gate 		mutex_init(&slock->lock, NULL, MUTEX_DEFAULT, NULL);
29210Sstevel@tonic-gate 		cv_init(&slock->cv, NULL, CV_DEFAULT, NULL);
29220Sstevel@tonic-gate 		slock->next = devmap_slist;
29230Sstevel@tonic-gate 		devmap_slist = slock;
29240Sstevel@tonic-gate 	} else
29250Sstevel@tonic-gate 		kmem_free(tmp, sizeof (struct devmap_softlock));
29260Sstevel@tonic-gate 
29270Sstevel@tonic-gate 	mutex_enter(&slock->lock);
29280Sstevel@tonic-gate 	slock->refcnt++;
29290Sstevel@tonic-gate 	mutex_exit(&slock->lock);
29300Sstevel@tonic-gate 	mutex_exit(&devmap_slock);
29310Sstevel@tonic-gate 
29320Sstevel@tonic-gate 	return (slock);
29330Sstevel@tonic-gate }
29340Sstevel@tonic-gate 
29350Sstevel@tonic-gate /*
29360Sstevel@tonic-gate  * Wake up processes that sleep on softlocked.
29370Sstevel@tonic-gate  * Free dh_softlock if refcnt is 0.
29380Sstevel@tonic-gate  */
29390Sstevel@tonic-gate static void
29400Sstevel@tonic-gate devmap_softlock_rele(devmap_handle_t *dhp)
29410Sstevel@tonic-gate {
29420Sstevel@tonic-gate 	struct devmap_softlock *slock = dhp->dh_softlock;
29430Sstevel@tonic-gate 	struct devmap_softlock *tmp;
29440Sstevel@tonic-gate 	struct devmap_softlock *parent;
29450Sstevel@tonic-gate 
29460Sstevel@tonic-gate 	TRACE_0(TR_FAC_DEVMAP, TR_DEVMAP_SOFTLOCK_RELE,
29470Sstevel@tonic-gate 	    "devmap_softlock_rele:start");
29480Sstevel@tonic-gate 
29490Sstevel@tonic-gate 	mutex_enter(&devmap_slock);
29500Sstevel@tonic-gate 	mutex_enter(&slock->lock);
29510Sstevel@tonic-gate 
29520Sstevel@tonic-gate 	ASSERT(slock->refcnt > 0);
29530Sstevel@tonic-gate 
29540Sstevel@tonic-gate 	slock->refcnt--;
29550Sstevel@tonic-gate 
29560Sstevel@tonic-gate 	/*
29570Sstevel@tonic-gate 	 * If no one is using the device, free up the slock data.
29580Sstevel@tonic-gate 	 */
29590Sstevel@tonic-gate 	if (slock->refcnt == 0) {
29600Sstevel@tonic-gate 		slock->softlocked = 0;
29610Sstevel@tonic-gate 		cv_signal(&slock->cv);
29620Sstevel@tonic-gate 
29630Sstevel@tonic-gate 		if (devmap_slist == slock)
29640Sstevel@tonic-gate 			devmap_slist = slock->next;
29650Sstevel@tonic-gate 		else {
29660Sstevel@tonic-gate 			parent = devmap_slist;
29670Sstevel@tonic-gate 			for (tmp = devmap_slist->next; tmp != NULL;
29680Sstevel@tonic-gate 				tmp = tmp->next) {
29690Sstevel@tonic-gate 				if (tmp == slock) {
29700Sstevel@tonic-gate 					parent->next = tmp->next;
29710Sstevel@tonic-gate 					break;
29720Sstevel@tonic-gate 				}
29730Sstevel@tonic-gate 				parent = tmp;
29740Sstevel@tonic-gate 			}
29750Sstevel@tonic-gate 		}
29760Sstevel@tonic-gate 		mutex_exit(&slock->lock);
29770Sstevel@tonic-gate 		mutex_destroy(&slock->lock);
29780Sstevel@tonic-gate 		cv_destroy(&slock->cv);
29790Sstevel@tonic-gate 		kmem_free(slock, sizeof (struct devmap_softlock));
29800Sstevel@tonic-gate 	} else
29810Sstevel@tonic-gate 		mutex_exit(&slock->lock);
29820Sstevel@tonic-gate 
29830Sstevel@tonic-gate 	mutex_exit(&devmap_slock);
29840Sstevel@tonic-gate }
29850Sstevel@tonic-gate 
29860Sstevel@tonic-gate /*
29870Sstevel@tonic-gate  * Wake up processes that sleep on dh_ctx->locked.
29880Sstevel@tonic-gate  * Free dh_ctx if refcnt is 0.
29890Sstevel@tonic-gate  */
29900Sstevel@tonic-gate static void
29910Sstevel@tonic-gate devmap_ctx_rele(devmap_handle_t *dhp)
29920Sstevel@tonic-gate {
29930Sstevel@tonic-gate 	struct devmap_ctx *devctx = dhp->dh_ctx;
29940Sstevel@tonic-gate 	struct devmap_ctx *tmp;
29950Sstevel@tonic-gate 	struct devmap_ctx *parent;
29960Sstevel@tonic-gate 	timeout_id_t tid;
29970Sstevel@tonic-gate 
29980Sstevel@tonic-gate 	TRACE_0(TR_FAC_DEVMAP, TR_DEVMAP_CTX_RELE,
29990Sstevel@tonic-gate 	    "devmap_ctx_rele:start");
30000Sstevel@tonic-gate 
30010Sstevel@tonic-gate 	mutex_enter(&devmapctx_lock);
30020Sstevel@tonic-gate 	mutex_enter(&devctx->lock);
30030Sstevel@tonic-gate 
30040Sstevel@tonic-gate 	ASSERT(devctx->refcnt > 0);
30050Sstevel@tonic-gate 
30060Sstevel@tonic-gate 	devctx->refcnt--;
30070Sstevel@tonic-gate 
30080Sstevel@tonic-gate 	/*
30090Sstevel@tonic-gate 	 * If no one is using the device, free up the devctx data.
30100Sstevel@tonic-gate 	 */
30110Sstevel@tonic-gate 	if (devctx->refcnt == 0) {
30120Sstevel@tonic-gate 		/*
30130Sstevel@tonic-gate 		 * Untimeout any threads using this mapping as they are about
30140Sstevel@tonic-gate 		 * to go away.
30150Sstevel@tonic-gate 		 */
30160Sstevel@tonic-gate 		if (devctx->timeout != 0) {
30170Sstevel@tonic-gate 			TRACE_0(TR_FAC_DEVMAP, TR_DEVMAP_CTX_RELE_CK1,
30180Sstevel@tonic-gate 			    "devmap_ctx_rele:untimeout ctx->timeout");
30190Sstevel@tonic-gate 
30200Sstevel@tonic-gate 			tid = devctx->timeout;
30210Sstevel@tonic-gate 			mutex_exit(&devctx->lock);
30220Sstevel@tonic-gate 			(void) untimeout(tid);
30230Sstevel@tonic-gate 			mutex_enter(&devctx->lock);
30240Sstevel@tonic-gate 		}
30250Sstevel@tonic-gate 
30260Sstevel@tonic-gate 		devctx->oncpu = 0;
30270Sstevel@tonic-gate 		cv_signal(&devctx->cv);
30280Sstevel@tonic-gate 
30290Sstevel@tonic-gate 		if (devmapctx_list == devctx)
30300Sstevel@tonic-gate 			devmapctx_list = devctx->next;
30310Sstevel@tonic-gate 		else {
30320Sstevel@tonic-gate 			parent = devmapctx_list;
30330Sstevel@tonic-gate 			for (tmp = devmapctx_list->next; tmp != NULL;
30340Sstevel@tonic-gate 				tmp = tmp->next) {
30350Sstevel@tonic-gate 				if (tmp == devctx) {
30360Sstevel@tonic-gate 					parent->next = tmp->next;
30370Sstevel@tonic-gate 					break;
30380Sstevel@tonic-gate 				}
30390Sstevel@tonic-gate 				parent = tmp;
30400Sstevel@tonic-gate 			}
30410Sstevel@tonic-gate 		}
30420Sstevel@tonic-gate 		mutex_exit(&devctx->lock);
30430Sstevel@tonic-gate 		mutex_destroy(&devctx->lock);
30440Sstevel@tonic-gate 		cv_destroy(&devctx->cv);
30450Sstevel@tonic-gate 		kmem_free(devctx, sizeof (struct devmap_ctx));
30460Sstevel@tonic-gate 	} else
30470Sstevel@tonic-gate 		mutex_exit(&devctx->lock);
30480Sstevel@tonic-gate 
30490Sstevel@tonic-gate 	mutex_exit(&devmapctx_lock);
30500Sstevel@tonic-gate }
30510Sstevel@tonic-gate 
30520Sstevel@tonic-gate /*
30530Sstevel@tonic-gate  * devmap_load:
30540Sstevel@tonic-gate  *			Marks a segdev segment or pages if offset->offset+len
30550Sstevel@tonic-gate  *			is not the entire segment as nointercept and faults in
30560Sstevel@tonic-gate  *			the pages in the range offset -> offset+len.
30570Sstevel@tonic-gate  */
30580Sstevel@tonic-gate int
30590Sstevel@tonic-gate devmap_load(devmap_cookie_t dhc, offset_t offset, size_t len, uint_t type,
30600Sstevel@tonic-gate     uint_t rw)
30610Sstevel@tonic-gate {
30620Sstevel@tonic-gate 	devmap_handle_t *dhp = (devmap_handle_t *)dhc;
30630Sstevel@tonic-gate 	struct as *asp = dhp->dh_seg->s_as;
30640Sstevel@tonic-gate 	caddr_t	addr;
30650Sstevel@tonic-gate 	ulong_t	size;
30660Sstevel@tonic-gate 	ssize_t	soff;	/* offset from the beginning of the segment */
30670Sstevel@tonic-gate 	int rc;
30680Sstevel@tonic-gate 
30690Sstevel@tonic-gate 	TRACE_3(TR_FAC_DEVMAP, TR_DEVMAP_LOAD,
30700Sstevel@tonic-gate 	    "devmap_load:start dhp=%p offset=%llx len=%lx",
30710Sstevel@tonic-gate 		(void *)dhp, offset, len);
30720Sstevel@tonic-gate 
30730Sstevel@tonic-gate 	DEBUGF(7, (CE_CONT, "devmap_load: dhp %p offset %llx len %lx\n",
30740Sstevel@tonic-gate 	    (void *)dhp, offset, len));
30750Sstevel@tonic-gate 
30760Sstevel@tonic-gate 	/*
30770Sstevel@tonic-gate 	 *	Hat layer only supports devload to process' context for which
30780Sstevel@tonic-gate 	 *	the as lock is held. Verify here and return error if drivers
30790Sstevel@tonic-gate 	 *	inadvertently call devmap_load on a wrong devmap handle.
30800Sstevel@tonic-gate 	 */
30810Sstevel@tonic-gate 	if ((asp != &kas) && !AS_LOCK_HELD(asp, &asp->a_lock))
30820Sstevel@tonic-gate 		return (FC_MAKE_ERR(EINVAL));
30830Sstevel@tonic-gate 
30840Sstevel@tonic-gate 	soff = (ssize_t)(offset - dhp->dh_uoff);
30850Sstevel@tonic-gate 	soff = round_down_p2(soff, PAGESIZE);
30860Sstevel@tonic-gate 	if (soff < 0 || soff >= dhp->dh_len)
30870Sstevel@tonic-gate 		return (FC_MAKE_ERR(EINVAL));
30880Sstevel@tonic-gate 
30890Sstevel@tonic-gate 	/*
30900Sstevel@tonic-gate 	 * Address and size must be page aligned.  Len is set to the
30910Sstevel@tonic-gate 	 * number of bytes in the number of pages that are required to
30920Sstevel@tonic-gate 	 * support len.  Offset is set to the byte offset of the first byte
30930Sstevel@tonic-gate 	 * of the page that contains offset.
30940Sstevel@tonic-gate 	 */
30950Sstevel@tonic-gate 	len = round_up_p2(len, PAGESIZE);
30960Sstevel@tonic-gate 
30970Sstevel@tonic-gate 	/*
30980Sstevel@tonic-gate 	 * If len == 0, then calculate the size by getting
30990Sstevel@tonic-gate 	 * the number of bytes from offset to the end of the segment.
31000Sstevel@tonic-gate 	 */
31010Sstevel@tonic-gate 	if (len == 0)
31020Sstevel@tonic-gate 		size = dhp->dh_len - soff;
31030Sstevel@tonic-gate 	else {
31040Sstevel@tonic-gate 		size = len;
31050Sstevel@tonic-gate 		if ((soff + size) > dhp->dh_len)
31060Sstevel@tonic-gate 			return (FC_MAKE_ERR(EINVAL));
31070Sstevel@tonic-gate 	}
31080Sstevel@tonic-gate 
31090Sstevel@tonic-gate 	/*
31100Sstevel@tonic-gate 	 * The address is offset bytes from the base address of
31110Sstevel@tonic-gate 	 * the segment.
31120Sstevel@tonic-gate 	 */
31130Sstevel@tonic-gate 	addr = (caddr_t)(soff + dhp->dh_uvaddr);
31140Sstevel@tonic-gate 
31150Sstevel@tonic-gate 	HOLD_DHP_LOCK(dhp);
31160Sstevel@tonic-gate 	rc = segdev_faultpages(asp->a_hat,
31170Sstevel@tonic-gate 			dhp->dh_seg, addr, size, type, rw, dhp);
31180Sstevel@tonic-gate 	RELE_DHP_LOCK(dhp);
31190Sstevel@tonic-gate 	return (rc);
31200Sstevel@tonic-gate }
31210Sstevel@tonic-gate 
31220Sstevel@tonic-gate int
31230Sstevel@tonic-gate devmap_setup(dev_t dev, offset_t off, struct as *as, caddr_t *addrp,
31240Sstevel@tonic-gate     size_t len, uint_t prot, uint_t maxprot, uint_t flags, struct cred *cred)
31250Sstevel@tonic-gate {
31260Sstevel@tonic-gate 	register devmap_handle_t *dhp;
31270Sstevel@tonic-gate 	int (*devmap)(dev_t, devmap_cookie_t, offset_t, size_t,
31280Sstevel@tonic-gate 		size_t *, uint_t);
31290Sstevel@tonic-gate 	int (*mmap)(dev_t, off_t, int);
31300Sstevel@tonic-gate 	struct devmap_callback_ctl *callbackops;
31310Sstevel@tonic-gate 	devmap_handle_t *dhp_head = NULL;
31320Sstevel@tonic-gate 	devmap_handle_t *dhp_prev = NULL;
31330Sstevel@tonic-gate 	devmap_handle_t *dhp_curr;
31340Sstevel@tonic-gate 	caddr_t addr;
31350Sstevel@tonic-gate 	int map_flag;
31360Sstevel@tonic-gate 	int ret;
31370Sstevel@tonic-gate 	ulong_t total_len;
31380Sstevel@tonic-gate 	size_t map_len;
31390Sstevel@tonic-gate 	size_t resid_len = len;
31400Sstevel@tonic-gate 	offset_t map_off = off;
31410Sstevel@tonic-gate 	struct devmap_softlock *slock = NULL;
31420Sstevel@tonic-gate 
31430Sstevel@tonic-gate #ifdef lint
31440Sstevel@tonic-gate 	cred = cred;
31450Sstevel@tonic-gate #endif
31460Sstevel@tonic-gate 
31470Sstevel@tonic-gate 	TRACE_2(TR_FAC_DEVMAP, TR_DEVMAP_SETUP,
31480Sstevel@tonic-gate 	    "devmap_setup:start off=%llx len=%lx", off, len);
31490Sstevel@tonic-gate 	DEBUGF(3, (CE_CONT, "devmap_setup: off %llx len %lx\n",
31500Sstevel@tonic-gate 	    off, len));
31510Sstevel@tonic-gate 
31520Sstevel@tonic-gate 	devmap = devopsp[getmajor(dev)]->devo_cb_ops->cb_devmap;
31530Sstevel@tonic-gate 	mmap = devopsp[getmajor(dev)]->devo_cb_ops->cb_mmap;
31540Sstevel@tonic-gate 
31550Sstevel@tonic-gate 	/*
31560Sstevel@tonic-gate 	 * driver must provide devmap(9E) entry point in cb_ops to use the
31570Sstevel@tonic-gate 	 * devmap framework.
31580Sstevel@tonic-gate 	 */
31590Sstevel@tonic-gate 	if (devmap == NULL || devmap == nulldev || devmap == nodev)
31600Sstevel@tonic-gate 		return (EINVAL);
31610Sstevel@tonic-gate 
31620Sstevel@tonic-gate 	/*
31630Sstevel@tonic-gate 	 * To protect from an inadvertent entry because the devmap entry point
31640Sstevel@tonic-gate 	 * is not NULL, return error if D_DEVMAP bit is not set in cb_flag and
31650Sstevel@tonic-gate 	 * mmap is NULL.
31660Sstevel@tonic-gate 	 */
31670Sstevel@tonic-gate 	map_flag = devopsp[getmajor(dev)]->devo_cb_ops->cb_flag;
31680Sstevel@tonic-gate 	if ((map_flag & D_DEVMAP) == 0 && (mmap == NULL || mmap == nulldev))
31690Sstevel@tonic-gate 		return (EINVAL);
31700Sstevel@tonic-gate 
31710Sstevel@tonic-gate 	/*
31720Sstevel@tonic-gate 	 * devmap allows mmap(2) to map multiple registers.
31730Sstevel@tonic-gate 	 * one devmap_handle is created for each register mapped.
31740Sstevel@tonic-gate 	 */
31750Sstevel@tonic-gate 	for (total_len = 0; total_len < len; total_len += map_len) {
31760Sstevel@tonic-gate 		dhp = kmem_zalloc(sizeof (devmap_handle_t), KM_SLEEP);
31770Sstevel@tonic-gate 
31780Sstevel@tonic-gate 		if (dhp_prev != NULL)
31790Sstevel@tonic-gate 			dhp_prev->dh_next = dhp;
31800Sstevel@tonic-gate 		else
31810Sstevel@tonic-gate 			dhp_head = dhp;
31820Sstevel@tonic-gate 		dhp_prev = dhp;
31830Sstevel@tonic-gate 
31840Sstevel@tonic-gate 		dhp->dh_prot = prot;
31850Sstevel@tonic-gate 		dhp->dh_orig_maxprot = dhp->dh_maxprot = maxprot;
31860Sstevel@tonic-gate 		dhp->dh_dev = dev;
31870Sstevel@tonic-gate 		dhp->dh_timeout_length = CTX_TIMEOUT_VALUE;
31880Sstevel@tonic-gate 		dhp->dh_uoff = map_off;
31890Sstevel@tonic-gate 
31900Sstevel@tonic-gate 		/*
31910Sstevel@tonic-gate 		 * Get mapping specific info from
31920Sstevel@tonic-gate 		 * the driver, such as rnumber, roff, len, callbackops,
31930Sstevel@tonic-gate 		 * accattrp and, if the mapping is for kernel memory,
31940Sstevel@tonic-gate 		 * ddi_umem_cookie.
31950Sstevel@tonic-gate 		 */
31960Sstevel@tonic-gate 		if ((ret = cdev_devmap(dev, dhp, map_off,
31970Sstevel@tonic-gate 		    resid_len, &map_len, get_udatamodel())) != 0) {
31980Sstevel@tonic-gate 			free_devmap_handle(dhp_head);
31990Sstevel@tonic-gate 			return (ENXIO);
32000Sstevel@tonic-gate 		}
32010Sstevel@tonic-gate 
32020Sstevel@tonic-gate 		if (map_len & PAGEOFFSET) {
32030Sstevel@tonic-gate 			free_devmap_handle(dhp_head);
32040Sstevel@tonic-gate 			return (EINVAL);
32050Sstevel@tonic-gate 		}
32060Sstevel@tonic-gate 
32070Sstevel@tonic-gate 		callbackops = &dhp->dh_callbackops;
32080Sstevel@tonic-gate 
32090Sstevel@tonic-gate 		if ((callbackops->devmap_access == NULL) ||
32100Sstevel@tonic-gate 			(callbackops->devmap_access == nulldev) ||
32110Sstevel@tonic-gate 			(callbackops->devmap_access == nodev)) {
32120Sstevel@tonic-gate 			/*
32130Sstevel@tonic-gate 			 * Normally devmap does not support MAP_PRIVATE unless
32140Sstevel@tonic-gate 			 * the drivers provide a valid devmap_access routine.
32150Sstevel@tonic-gate 			 */
32160Sstevel@tonic-gate 			if ((flags & MAP_PRIVATE) != 0) {
32170Sstevel@tonic-gate 				free_devmap_handle(dhp_head);
32180Sstevel@tonic-gate 				return (EINVAL);
32190Sstevel@tonic-gate 			}
32200Sstevel@tonic-gate 		} else {
32210Sstevel@tonic-gate 			/*
32220Sstevel@tonic-gate 			 * Initialize dhp_softlock and dh_ctx if the drivers
32230Sstevel@tonic-gate 			 * provide devmap_access.
32240Sstevel@tonic-gate 			 */
32250Sstevel@tonic-gate 			dhp->dh_softlock = devmap_softlock_init(dev,
32260Sstevel@tonic-gate 				(ulong_t)callbackops->devmap_access);
32270Sstevel@tonic-gate 			dhp->dh_ctx = devmap_ctxinit(dev,
32280Sstevel@tonic-gate 				(ulong_t)callbackops->devmap_access);
32290Sstevel@tonic-gate 
32300Sstevel@tonic-gate 			/*
32310Sstevel@tonic-gate 			 * segdev_fault can only work when all
32320Sstevel@tonic-gate 			 * dh_softlock in a multi-dhp mapping
32330Sstevel@tonic-gate 			 * are same. see comments in segdev_fault
32340Sstevel@tonic-gate 			 * This code keeps track of the first
32350Sstevel@tonic-gate 			 * dh_softlock allocated in slock and
32360Sstevel@tonic-gate 			 * compares all later allocations and if
32370Sstevel@tonic-gate 			 * not similar, returns an error.
32380Sstevel@tonic-gate 			 */
32390Sstevel@tonic-gate 			if (slock == NULL)
32400Sstevel@tonic-gate 				slock = dhp->dh_softlock;
32410Sstevel@tonic-gate 			if (slock != dhp->dh_softlock) {
32420Sstevel@tonic-gate 				free_devmap_handle(dhp_head);
32430Sstevel@tonic-gate 				return (ENOTSUP);
32440Sstevel@tonic-gate 			}
32450Sstevel@tonic-gate 		}
32460Sstevel@tonic-gate 
32470Sstevel@tonic-gate 		map_off += map_len;
32480Sstevel@tonic-gate 		resid_len -= map_len;
32490Sstevel@tonic-gate 	}
32500Sstevel@tonic-gate 
32510Sstevel@tonic-gate 	/*
32520Sstevel@tonic-gate 	 * get the user virtual address and establish the mapping between
32530Sstevel@tonic-gate 	 * uvaddr and device physical address.
32540Sstevel@tonic-gate 	 */
32550Sstevel@tonic-gate 	if ((ret = devmap_device(dhp_head, as, addrp, off, len, flags))
32560Sstevel@tonic-gate 			!= 0) {
32570Sstevel@tonic-gate 		/*
32580Sstevel@tonic-gate 		 * free devmap handles if error during the mapping.
32590Sstevel@tonic-gate 		 */
32600Sstevel@tonic-gate 		free_devmap_handle(dhp_head);
32610Sstevel@tonic-gate 
32620Sstevel@tonic-gate 		return (ret);
32630Sstevel@tonic-gate 	}
32640Sstevel@tonic-gate 
32650Sstevel@tonic-gate 	/*
32660Sstevel@tonic-gate 	 * call the driver's devmap_map callback to do more after the mapping,
32670Sstevel@tonic-gate 	 * such as to allocate driver private data for context management.
32680Sstevel@tonic-gate 	 */
32690Sstevel@tonic-gate 	dhp = dhp_head;
32700Sstevel@tonic-gate 	map_off = off;
32710Sstevel@tonic-gate 	addr = *addrp;
32720Sstevel@tonic-gate 	while (dhp != NULL) {
32730Sstevel@tonic-gate 		callbackops = &dhp->dh_callbackops;
32740Sstevel@tonic-gate 		dhp->dh_uvaddr = addr;
32750Sstevel@tonic-gate 		dhp_curr = dhp;
32760Sstevel@tonic-gate 		if (callbackops->devmap_map != NULL) {
32770Sstevel@tonic-gate 			ret = (*callbackops->devmap_map)((devmap_cookie_t)dhp,
32780Sstevel@tonic-gate 					dev, flags, map_off,
32790Sstevel@tonic-gate 					dhp->dh_len, &dhp->dh_pvtp);
32800Sstevel@tonic-gate 			if (ret != 0) {
32810Sstevel@tonic-gate 				struct segdev_data *sdp;
32820Sstevel@tonic-gate 
32830Sstevel@tonic-gate 				/*
32840Sstevel@tonic-gate 				 * call driver's devmap_unmap entry point
32850Sstevel@tonic-gate 				 * to free driver resources.
32860Sstevel@tonic-gate 				 */
32870Sstevel@tonic-gate 				dhp = dhp_head;
32880Sstevel@tonic-gate 				map_off = off;
32890Sstevel@tonic-gate 				while (dhp != dhp_curr) {
32900Sstevel@tonic-gate 					callbackops = &dhp->dh_callbackops;
32910Sstevel@tonic-gate 					if (callbackops->devmap_unmap != NULL) {
32920Sstevel@tonic-gate 						(*callbackops->devmap_unmap)(
32930Sstevel@tonic-gate 							dhp, dhp->dh_pvtp,
32940Sstevel@tonic-gate 							map_off, dhp->dh_len,
32950Sstevel@tonic-gate 							NULL, NULL, NULL, NULL);
32960Sstevel@tonic-gate 					}
32970Sstevel@tonic-gate 					map_off += dhp->dh_len;
32980Sstevel@tonic-gate 					dhp = dhp->dh_next;
32990Sstevel@tonic-gate 				}
33000Sstevel@tonic-gate 				sdp = dhp_head->dh_seg->s_data;
33010Sstevel@tonic-gate 				sdp->devmap_data = NULL;
33020Sstevel@tonic-gate 				free_devmap_handle(dhp_head);
33030Sstevel@tonic-gate 				return (ENXIO);
33040Sstevel@tonic-gate 			}
33050Sstevel@tonic-gate 		}
33060Sstevel@tonic-gate 		map_off += dhp->dh_len;
33070Sstevel@tonic-gate 		addr += dhp->dh_len;
33080Sstevel@tonic-gate 		dhp = dhp->dh_next;
33090Sstevel@tonic-gate 	}
33100Sstevel@tonic-gate 
33110Sstevel@tonic-gate 	return (0);
33120Sstevel@tonic-gate }
33130Sstevel@tonic-gate 
33140Sstevel@tonic-gate int
33150Sstevel@tonic-gate ddi_devmap_segmap(dev_t dev, off_t off, ddi_as_handle_t as, caddr_t *addrp,
33160Sstevel@tonic-gate     off_t len, uint_t prot, uint_t maxprot, uint_t flags, struct cred *cred)
33170Sstevel@tonic-gate {
33180Sstevel@tonic-gate 	TRACE_0(TR_FAC_DEVMAP, TR_DEVMAP_SEGMAP,
33190Sstevel@tonic-gate 	    "devmap_segmap:start");
33200Sstevel@tonic-gate 	return (devmap_setup(dev, (offset_t)off, (struct as *)as, addrp,
33210Sstevel@tonic-gate 	    (size_t)len, prot, maxprot, flags, cred));
33220Sstevel@tonic-gate }
33230Sstevel@tonic-gate 
33240Sstevel@tonic-gate /*
33250Sstevel@tonic-gate  * Called from devmap_devmem_setup/remap to see if can use large pages for
33260Sstevel@tonic-gate  * this device mapping.
33270Sstevel@tonic-gate  * Also calculate the max. page size for this mapping.
33280Sstevel@tonic-gate  * this page size will be used in fault routine for
33290Sstevel@tonic-gate  * optimal page size calculations.
33300Sstevel@tonic-gate  */
33310Sstevel@tonic-gate static void
33320Sstevel@tonic-gate devmap_devmem_large_page_setup(devmap_handle_t *dhp)
33330Sstevel@tonic-gate {
33340Sstevel@tonic-gate 	ASSERT(dhp_is_devmem(dhp));
33350Sstevel@tonic-gate 	dhp->dh_mmulevel = 0;
33360Sstevel@tonic-gate 
33370Sstevel@tonic-gate 	/*
33380Sstevel@tonic-gate 	 * use large page size only if:
33390Sstevel@tonic-gate 	 *  1. device memory.
33400Sstevel@tonic-gate 	 *  2. mmu supports multiple page sizes,
33410Sstevel@tonic-gate 	 *  3. Driver did not disallow it
33420Sstevel@tonic-gate 	 *  4. dhp length is at least as big as the large pagesize
33430Sstevel@tonic-gate 	 *  5. the uvaddr and pfn are large pagesize aligned
33440Sstevel@tonic-gate 	 */
33450Sstevel@tonic-gate 	if (page_num_pagesizes() > 1 &&
33460Sstevel@tonic-gate 	    !(dhp->dh_flags & (DEVMAP_USE_PAGESIZE | DEVMAP_MAPPING_INVALID))) {
33470Sstevel@tonic-gate 		ulong_t base;
33480Sstevel@tonic-gate 		int level;
33490Sstevel@tonic-gate 
33500Sstevel@tonic-gate 		base = (ulong_t)ptob(dhp->dh_pfn);
33510Sstevel@tonic-gate 		for (level = 1; level < page_num_pagesizes(); level++) {
33520Sstevel@tonic-gate 			size_t pgsize = page_get_pagesize(level);
33530Sstevel@tonic-gate 			if ((dhp->dh_len < pgsize) ||
33540Sstevel@tonic-gate 			    (!VA_PA_PGSIZE_ALIGNED((uintptr_t)dhp->dh_uvaddr,
33550Sstevel@tonic-gate 					base, pgsize))) {
33560Sstevel@tonic-gate 				break;
33570Sstevel@tonic-gate 			}
33580Sstevel@tonic-gate 		}
33590Sstevel@tonic-gate 		dhp->dh_mmulevel = level - 1;
33600Sstevel@tonic-gate 	}
33610Sstevel@tonic-gate 	if (dhp->dh_mmulevel > 0) {
33620Sstevel@tonic-gate 		dhp->dh_flags |= DEVMAP_FLAG_LARGE;
33630Sstevel@tonic-gate 	} else {
33640Sstevel@tonic-gate 		dhp->dh_flags &= ~DEVMAP_FLAG_LARGE;
33650Sstevel@tonic-gate 	}
33660Sstevel@tonic-gate }
33670Sstevel@tonic-gate 
33680Sstevel@tonic-gate /*
33690Sstevel@tonic-gate  * Called by driver devmap routine to pass device specific info to
33700Sstevel@tonic-gate  * the framework.    used for device memory mapping only.
33710Sstevel@tonic-gate  */
33720Sstevel@tonic-gate int
33730Sstevel@tonic-gate devmap_devmem_setup(devmap_cookie_t dhc, dev_info_t *dip,
33740Sstevel@tonic-gate     struct devmap_callback_ctl *callbackops, uint_t rnumber, offset_t roff,
33750Sstevel@tonic-gate     size_t len, uint_t maxprot, uint_t flags, ddi_device_acc_attr_t *accattrp)
33760Sstevel@tonic-gate {
33770Sstevel@tonic-gate 	devmap_handle_t *dhp = (devmap_handle_t *)dhc;
33780Sstevel@tonic-gate 	ddi_acc_handle_t handle;
33790Sstevel@tonic-gate 	ddi_map_req_t mr;
33800Sstevel@tonic-gate 	ddi_acc_hdl_t *hp;
33810Sstevel@tonic-gate 	int err;
33820Sstevel@tonic-gate 
33830Sstevel@tonic-gate 	TRACE_4(TR_FAC_DEVMAP, TR_DEVMAP_DEVMEM_SETUP,
33840Sstevel@tonic-gate 	    "devmap_devmem_setup:start dhp=%p offset=%llx rnum=%d len=%lx",
33850Sstevel@tonic-gate 	    (void *)dhp, roff, rnumber, (uint_t)len);
33860Sstevel@tonic-gate 	DEBUGF(2, (CE_CONT, "devmap_devmem_setup: dhp %p offset %llx "
33870Sstevel@tonic-gate 	    "rnum %d len %lx\n", (void *)dhp, roff, rnumber, len));
33880Sstevel@tonic-gate 
33890Sstevel@tonic-gate 	/*
33900Sstevel@tonic-gate 	 * First to check if this function has been called for this dhp.
33910Sstevel@tonic-gate 	 */
33920Sstevel@tonic-gate 	if (dhp->dh_flags & DEVMAP_SETUP_DONE)
33930Sstevel@tonic-gate 		return (DDI_FAILURE);
33940Sstevel@tonic-gate 
33950Sstevel@tonic-gate 	if ((dhp->dh_prot & dhp->dh_orig_maxprot & maxprot) != dhp->dh_prot)
33960Sstevel@tonic-gate 		return (DDI_FAILURE);
33970Sstevel@tonic-gate 
33980Sstevel@tonic-gate 	if (flags & DEVMAP_MAPPING_INVALID) {
33990Sstevel@tonic-gate 		/*
34000Sstevel@tonic-gate 		 * Don't go up the tree to get pfn if the driver specifies
34010Sstevel@tonic-gate 		 * DEVMAP_MAPPING_INVALID in flags.
34020Sstevel@tonic-gate 		 *
34030Sstevel@tonic-gate 		 * If DEVMAP_MAPPING_INVALID is specified, we have to grant
34040Sstevel@tonic-gate 		 * remap permission.
34050Sstevel@tonic-gate 		 */
34060Sstevel@tonic-gate 		if (!(flags & DEVMAP_ALLOW_REMAP)) {
34070Sstevel@tonic-gate 			return (DDI_FAILURE);
34080Sstevel@tonic-gate 		}
34090Sstevel@tonic-gate 		dhp->dh_pfn = PFN_INVALID;
34100Sstevel@tonic-gate 	} else {
34110Sstevel@tonic-gate 		handle = impl_acc_hdl_alloc(KM_SLEEP, NULL);
34120Sstevel@tonic-gate 		if (handle == NULL)
34130Sstevel@tonic-gate 			return (DDI_FAILURE);
34140Sstevel@tonic-gate 
34150Sstevel@tonic-gate 		hp = impl_acc_hdl_get(handle);
34160Sstevel@tonic-gate 		hp->ah_vers = VERS_ACCHDL;
34170Sstevel@tonic-gate 		hp->ah_dip = dip;
34180Sstevel@tonic-gate 		hp->ah_rnumber = rnumber;
34190Sstevel@tonic-gate 		hp->ah_offset = roff;
34200Sstevel@tonic-gate 		hp->ah_len = len;
34210Sstevel@tonic-gate 		if (accattrp != NULL)
34220Sstevel@tonic-gate 			hp->ah_acc = *accattrp;
34230Sstevel@tonic-gate 
34240Sstevel@tonic-gate 		mr.map_op = DDI_MO_MAP_LOCKED;
34250Sstevel@tonic-gate 		mr.map_type = DDI_MT_RNUMBER;
34260Sstevel@tonic-gate 		mr.map_obj.rnumber = rnumber;
34270Sstevel@tonic-gate 		mr.map_prot = maxprot & dhp->dh_orig_maxprot;
34280Sstevel@tonic-gate 		mr.map_flags = DDI_MF_DEVICE_MAPPING;
34290Sstevel@tonic-gate 		mr.map_handlep = hp;
34300Sstevel@tonic-gate 		mr.map_vers = DDI_MAP_VERSION;
34310Sstevel@tonic-gate 
34320Sstevel@tonic-gate 		/*
34330Sstevel@tonic-gate 		 * up the device tree to get pfn.
34340Sstevel@tonic-gate 		 * The rootnex_map_regspec() routine in nexus drivers has been
34350Sstevel@tonic-gate 		 * modified to return pfn if map_flags is DDI_MF_DEVICE_MAPPING.
34360Sstevel@tonic-gate 		 */
34370Sstevel@tonic-gate 		err = ddi_map(dip, &mr, roff, len, (caddr_t *)&dhp->dh_pfn);
34380Sstevel@tonic-gate 		dhp->dh_hat_attr = hp->ah_hat_flags;
34390Sstevel@tonic-gate 		impl_acc_hdl_free(handle);
34400Sstevel@tonic-gate 
34410Sstevel@tonic-gate 		if (err)
34420Sstevel@tonic-gate 			return (DDI_FAILURE);
34430Sstevel@tonic-gate 	}
34440Sstevel@tonic-gate 	/* Should not be using devmem setup for memory pages */
34450Sstevel@tonic-gate 	ASSERT(!pf_is_memory(dhp->dh_pfn));
34460Sstevel@tonic-gate 
34470Sstevel@tonic-gate 	/* Only some of the flags bits are settable by the driver */
34480Sstevel@tonic-gate 	dhp->dh_flags |= (flags & DEVMAP_SETUP_FLAGS);
34490Sstevel@tonic-gate 	dhp->dh_len = ptob(btopr(len));
34500Sstevel@tonic-gate 
34510Sstevel@tonic-gate 	dhp->dh_cookie = DEVMAP_DEVMEM_COOKIE;
34520Sstevel@tonic-gate 	dhp->dh_roff = ptob(btop(roff));
34530Sstevel@tonic-gate 
34540Sstevel@tonic-gate 	/* setup the dh_mmulevel and DEVMAP_FLAG_LARGE */
34550Sstevel@tonic-gate 	devmap_devmem_large_page_setup(dhp);
34560Sstevel@tonic-gate 	dhp->dh_maxprot = maxprot & dhp->dh_orig_maxprot;
34570Sstevel@tonic-gate 	ASSERT((dhp->dh_prot & dhp->dh_orig_maxprot & maxprot) == dhp->dh_prot);
34580Sstevel@tonic-gate 
34590Sstevel@tonic-gate 
34600Sstevel@tonic-gate 	if (callbackops != NULL) {
34610Sstevel@tonic-gate 		bcopy(callbackops, &dhp->dh_callbackops,
34620Sstevel@tonic-gate 		    sizeof (struct devmap_callback_ctl));
34630Sstevel@tonic-gate 	}
34640Sstevel@tonic-gate 
34650Sstevel@tonic-gate 	/*
34660Sstevel@tonic-gate 	 * Initialize dh_lock if we want to do remap.
34670Sstevel@tonic-gate 	 */
34680Sstevel@tonic-gate 	if (dhp->dh_flags & DEVMAP_ALLOW_REMAP) {
34690Sstevel@tonic-gate 		mutex_init(&dhp->dh_lock, NULL, MUTEX_DEFAULT, NULL);
34700Sstevel@tonic-gate 		dhp->dh_flags |= DEVMAP_LOCK_INITED;
34710Sstevel@tonic-gate 	}
34720Sstevel@tonic-gate 
34730Sstevel@tonic-gate 	dhp->dh_flags |= DEVMAP_SETUP_DONE;
34740Sstevel@tonic-gate 
34750Sstevel@tonic-gate 	return (DDI_SUCCESS);
34760Sstevel@tonic-gate }
34770Sstevel@tonic-gate 
34780Sstevel@tonic-gate int
34790Sstevel@tonic-gate devmap_devmem_remap(devmap_cookie_t dhc, dev_info_t *dip,
34800Sstevel@tonic-gate     uint_t rnumber, offset_t roff, size_t len, uint_t maxprot,
34810Sstevel@tonic-gate     uint_t flags, ddi_device_acc_attr_t *accattrp)
34820Sstevel@tonic-gate {
34830Sstevel@tonic-gate 	devmap_handle_t *dhp = (devmap_handle_t *)dhc;
34840Sstevel@tonic-gate 	ddi_acc_handle_t handle;
34850Sstevel@tonic-gate 	ddi_map_req_t mr;
34860Sstevel@tonic-gate 	ddi_acc_hdl_t *hp;
34870Sstevel@tonic-gate 	pfn_t	pfn;
34880Sstevel@tonic-gate 	uint_t	hat_flags;
34890Sstevel@tonic-gate 	int	err;
34900Sstevel@tonic-gate 
34910Sstevel@tonic-gate 	TRACE_4(TR_FAC_DEVMAP, TR_DEVMAP_DEVMEM_REMAP,
34920Sstevel@tonic-gate 	    "devmap_devmem_setup:start dhp=%p offset=%llx rnum=%d len=%lx",
34930Sstevel@tonic-gate 	    (void *)dhp, roff, rnumber, (uint_t)len);
34940Sstevel@tonic-gate 	DEBUGF(2, (CE_CONT, "devmap_devmem_remap: dhp %p offset %llx "
34950Sstevel@tonic-gate 	    "rnum %d len %lx\n", (void *)dhp, roff, rnumber, len));
34960Sstevel@tonic-gate 
34970Sstevel@tonic-gate 	/*
34980Sstevel@tonic-gate 	 * Return failure if setup has not been done or no remap permission
34990Sstevel@tonic-gate 	 * has been granted during the setup.
35000Sstevel@tonic-gate 	 */
35010Sstevel@tonic-gate 	if ((dhp->dh_flags & DEVMAP_SETUP_DONE) == 0 ||
35020Sstevel@tonic-gate 	    (dhp->dh_flags & DEVMAP_ALLOW_REMAP) == 0)
35030Sstevel@tonic-gate 		return (DDI_FAILURE);
35040Sstevel@tonic-gate 
35050Sstevel@tonic-gate 	/* Only DEVMAP_MAPPING_INVALID flag supported for remap */
35060Sstevel@tonic-gate 	if ((flags != 0) && (flags != DEVMAP_MAPPING_INVALID))
35070Sstevel@tonic-gate 		return (DDI_FAILURE);
35080Sstevel@tonic-gate 
35090Sstevel@tonic-gate 	if ((dhp->dh_prot & dhp->dh_orig_maxprot & maxprot) != dhp->dh_prot)
35100Sstevel@tonic-gate 		return (DDI_FAILURE);
35110Sstevel@tonic-gate 
35120Sstevel@tonic-gate 	if (!(flags & DEVMAP_MAPPING_INVALID)) {
35130Sstevel@tonic-gate 		handle = impl_acc_hdl_alloc(KM_SLEEP, NULL);
35140Sstevel@tonic-gate 		if (handle == NULL)
35150Sstevel@tonic-gate 			return (DDI_FAILURE);
35160Sstevel@tonic-gate 	}
35170Sstevel@tonic-gate 
35180Sstevel@tonic-gate 	HOLD_DHP_LOCK(dhp);
35190Sstevel@tonic-gate 
35200Sstevel@tonic-gate 	/*
35210Sstevel@tonic-gate 	 * Unload the old mapping, so next fault will setup the new mappings
35220Sstevel@tonic-gate 	 * Do this while holding the dhp lock so other faults dont reestablish
35230Sstevel@tonic-gate 	 * the mappings
35240Sstevel@tonic-gate 	 */
35250Sstevel@tonic-gate 	hat_unload(dhp->dh_seg->s_as->a_hat, dhp->dh_uvaddr,
35260Sstevel@tonic-gate 		dhp->dh_len, HAT_UNLOAD|HAT_UNLOAD_OTHER);
35270Sstevel@tonic-gate 
35280Sstevel@tonic-gate 	if (flags & DEVMAP_MAPPING_INVALID) {
35290Sstevel@tonic-gate 		dhp->dh_flags |= DEVMAP_MAPPING_INVALID;
35300Sstevel@tonic-gate 		dhp->dh_pfn = PFN_INVALID;
35310Sstevel@tonic-gate 	} else {
35320Sstevel@tonic-gate 		/* clear any prior DEVMAP_MAPPING_INVALID flag */
35330Sstevel@tonic-gate 		dhp->dh_flags &= ~DEVMAP_MAPPING_INVALID;
35340Sstevel@tonic-gate 		hp = impl_acc_hdl_get(handle);
35350Sstevel@tonic-gate 		hp->ah_vers = VERS_ACCHDL;
35360Sstevel@tonic-gate 		hp->ah_dip = dip;
35370Sstevel@tonic-gate 		hp->ah_rnumber = rnumber;
35380Sstevel@tonic-gate 		hp->ah_offset = roff;
35390Sstevel@tonic-gate 		hp->ah_len = len;
35400Sstevel@tonic-gate 		if (accattrp != NULL)
35410Sstevel@tonic-gate 			hp->ah_acc = *accattrp;
35420Sstevel@tonic-gate 
35430Sstevel@tonic-gate 		mr.map_op = DDI_MO_MAP_LOCKED;
35440Sstevel@tonic-gate 		mr.map_type = DDI_MT_RNUMBER;
35450Sstevel@tonic-gate 		mr.map_obj.rnumber = rnumber;
35460Sstevel@tonic-gate 		mr.map_prot = maxprot & dhp->dh_orig_maxprot;
35470Sstevel@tonic-gate 		mr.map_flags = DDI_MF_DEVICE_MAPPING;
35480Sstevel@tonic-gate 		mr.map_handlep = hp;
35490Sstevel@tonic-gate 		mr.map_vers = DDI_MAP_VERSION;
35500Sstevel@tonic-gate 
35510Sstevel@tonic-gate 		/*
35520Sstevel@tonic-gate 		 * up the device tree to get pfn.
35530Sstevel@tonic-gate 		 * The rootnex_map_regspec() routine in nexus drivers has been
35540Sstevel@tonic-gate 		 * modified to return pfn if map_flags is DDI_MF_DEVICE_MAPPING.
35550Sstevel@tonic-gate 		 */
35560Sstevel@tonic-gate 		err = ddi_map(dip, &mr, roff, len, (caddr_t *)&pfn);
35570Sstevel@tonic-gate 		hat_flags = hp->ah_hat_flags;
35580Sstevel@tonic-gate 		impl_acc_hdl_free(handle);
35590Sstevel@tonic-gate 		if (err) {
35600Sstevel@tonic-gate 			RELE_DHP_LOCK(dhp);
35610Sstevel@tonic-gate 			return (DDI_FAILURE);
35620Sstevel@tonic-gate 		}
35630Sstevel@tonic-gate 		/*
35640Sstevel@tonic-gate 		 * Store result of ddi_map first in local variables, as we do
35650Sstevel@tonic-gate 		 * not want to overwrite the existing dhp with wrong data.
35660Sstevel@tonic-gate 		 */
35670Sstevel@tonic-gate 		dhp->dh_pfn = pfn;
35680Sstevel@tonic-gate 		dhp->dh_hat_attr = hat_flags;
35690Sstevel@tonic-gate 	}
35700Sstevel@tonic-gate 
35710Sstevel@tonic-gate 	/* clear the large page size flag */
35720Sstevel@tonic-gate 	dhp->dh_flags &= ~DEVMAP_FLAG_LARGE;
35730Sstevel@tonic-gate 
35740Sstevel@tonic-gate 	dhp->dh_cookie = DEVMAP_DEVMEM_COOKIE;
35750Sstevel@tonic-gate 	dhp->dh_roff = ptob(btop(roff));
35760Sstevel@tonic-gate 
35770Sstevel@tonic-gate 	/* setup the dh_mmulevel and DEVMAP_FLAG_LARGE */
35780Sstevel@tonic-gate 	devmap_devmem_large_page_setup(dhp);
35790Sstevel@tonic-gate 	dhp->dh_maxprot = maxprot & dhp->dh_orig_maxprot;
35800Sstevel@tonic-gate 	ASSERT((dhp->dh_prot & dhp->dh_orig_maxprot & maxprot) == dhp->dh_prot);
35810Sstevel@tonic-gate 
35820Sstevel@tonic-gate 	RELE_DHP_LOCK(dhp);
35830Sstevel@tonic-gate 	return (DDI_SUCCESS);
35840Sstevel@tonic-gate }
35850Sstevel@tonic-gate 
35860Sstevel@tonic-gate /*
35870Sstevel@tonic-gate  * called by driver devmap routine to pass kernel virtual address  mapping
35880Sstevel@tonic-gate  * info to the framework.    used only for kernel memory
35890Sstevel@tonic-gate  * allocated from ddi_umem_alloc().
35900Sstevel@tonic-gate  */
35910Sstevel@tonic-gate int
35920Sstevel@tonic-gate devmap_umem_setup(devmap_cookie_t dhc, dev_info_t *dip,
35930Sstevel@tonic-gate     struct devmap_callback_ctl *callbackops, ddi_umem_cookie_t cookie,
35940Sstevel@tonic-gate     offset_t off, size_t len, uint_t maxprot, uint_t flags,
35950Sstevel@tonic-gate     ddi_device_acc_attr_t *accattrp)
35960Sstevel@tonic-gate {
35970Sstevel@tonic-gate 	devmap_handle_t *dhp = (devmap_handle_t *)dhc;
35980Sstevel@tonic-gate 	struct ddi_umem_cookie *cp = (struct ddi_umem_cookie *)cookie;
35990Sstevel@tonic-gate 
36000Sstevel@tonic-gate #ifdef lint
36010Sstevel@tonic-gate 	dip = dip;
36020Sstevel@tonic-gate #endif
36030Sstevel@tonic-gate 
36040Sstevel@tonic-gate 	TRACE_4(TR_FAC_DEVMAP, TR_DEVMAP_UMEM_SETUP,
36050Sstevel@tonic-gate 	    "devmap_umem_setup:start dhp=%p offset=%llx cookie=%p len=%lx",
36060Sstevel@tonic-gate 	    (void *)dhp, off, cookie, len);
36070Sstevel@tonic-gate 	DEBUGF(2, (CE_CONT, "devmap_umem_setup: dhp %p offset %llx "
36080Sstevel@tonic-gate 	    "cookie %p len %lx\n", (void *)dhp, off, (void *)cookie, len));
36090Sstevel@tonic-gate 
36100Sstevel@tonic-gate 	if (cookie == NULL)
36110Sstevel@tonic-gate 		return (DDI_FAILURE);
36120Sstevel@tonic-gate 
36130Sstevel@tonic-gate 	/* For UMEM_TRASH, this restriction is not needed */
36140Sstevel@tonic-gate 	if ((off + len) > cp->size)
36150Sstevel@tonic-gate 		return (DDI_FAILURE);
36160Sstevel@tonic-gate 
3617*1900Seota 	/* check if the cache and endian attributes are supported */
3618*1900Seota 	if (i_ddi_check_cache_attr(flags) == B_FALSE ||
3619*1900Seota 	    i_ddi_check_endian_attr(accattrp) == B_FALSE)
3620*1900Seota 		return (DDI_FAILURE);
3621*1900Seota 
36220Sstevel@tonic-gate 	/*
36230Sstevel@tonic-gate 	 * First to check if this function has been called for this dhp.
36240Sstevel@tonic-gate 	 */
36250Sstevel@tonic-gate 	if (dhp->dh_flags & DEVMAP_SETUP_DONE)
36260Sstevel@tonic-gate 		return (DDI_FAILURE);
36270Sstevel@tonic-gate 
36280Sstevel@tonic-gate 	if ((dhp->dh_prot & dhp->dh_orig_maxprot & maxprot) != dhp->dh_prot)
36290Sstevel@tonic-gate 		return (DDI_FAILURE);
36300Sstevel@tonic-gate 
36310Sstevel@tonic-gate 	if (flags & DEVMAP_MAPPING_INVALID) {
36320Sstevel@tonic-gate 		/*
36330Sstevel@tonic-gate 		 * If DEVMAP_MAPPING_INVALID is specified, we have to grant
36340Sstevel@tonic-gate 		 * remap permission.
36350Sstevel@tonic-gate 		 */
36360Sstevel@tonic-gate 		if (!(flags & DEVMAP_ALLOW_REMAP)) {
36370Sstevel@tonic-gate 			return (DDI_FAILURE);
36380Sstevel@tonic-gate 		}
36390Sstevel@tonic-gate 	} else {
36400Sstevel@tonic-gate 		dhp->dh_cookie = cookie;
36410Sstevel@tonic-gate 		dhp->dh_roff = ptob(btop(off));
36420Sstevel@tonic-gate 		dhp->dh_cvaddr = cp->cvaddr + dhp->dh_roff;
3643*1900Seota 		/* set HAT cache attributes */
3644*1900Seota 		i_ddi_cacheattr_to_hatacc(flags, &dhp->dh_hat_attr);
3645*1900Seota 		/* set HAT endianess attributes */
3646*1900Seota 		i_ddi_devacc_to_hatacc(accattrp, &dhp->dh_hat_attr);
36470Sstevel@tonic-gate 	}
36480Sstevel@tonic-gate 
36490Sstevel@tonic-gate 	/*
36500Sstevel@tonic-gate 	 * The default is _not_ to pass HAT_LOAD_NOCONSIST to hat_devload();
36510Sstevel@tonic-gate 	 * we pass HAT_LOAD_NOCONSIST _only_ in cases where hat tries to
36520Sstevel@tonic-gate 	 * create consistent mappings but our intention was to create
36530Sstevel@tonic-gate 	 * non-consistent mappings.
36540Sstevel@tonic-gate 	 *
36550Sstevel@tonic-gate 	 * DEVMEM: hat figures it out it's DEVMEM and creates non-consistent
36560Sstevel@tonic-gate 	 * mappings.
36570Sstevel@tonic-gate 	 *
36580Sstevel@tonic-gate 	 * kernel exported memory: hat figures it out it's memory and always
36590Sstevel@tonic-gate 	 * creates consistent mappings.
36600Sstevel@tonic-gate 	 *
36610Sstevel@tonic-gate 	 * /dev/mem: non-consistent mappings. See comments in common/io/mem.c
36620Sstevel@tonic-gate 	 *
36630Sstevel@tonic-gate 	 * /dev/kmem: consistent mappings are created unless they are
36640Sstevel@tonic-gate 	 * MAP_FIXED. We _explicitly_ tell hat to create non-consistent
36650Sstevel@tonic-gate 	 * mappings by passing HAT_LOAD_NOCONSIST in case of MAP_FIXED
36660Sstevel@tonic-gate 	 * mappings of /dev/kmem. See common/io/mem.c
36670Sstevel@tonic-gate 	 */
36680Sstevel@tonic-gate 
36690Sstevel@tonic-gate 	/* Only some of the flags bits are settable by the driver */
36700Sstevel@tonic-gate 	dhp->dh_flags |= (flags & DEVMAP_SETUP_FLAGS);
36710Sstevel@tonic-gate 
36720Sstevel@tonic-gate 	dhp->dh_len = ptob(btopr(len));
36730Sstevel@tonic-gate 	dhp->dh_maxprot = maxprot & dhp->dh_orig_maxprot;
36740Sstevel@tonic-gate 	ASSERT((dhp->dh_prot & dhp->dh_orig_maxprot & maxprot) == dhp->dh_prot);
36750Sstevel@tonic-gate 
36760Sstevel@tonic-gate 	if (callbackops != NULL) {
36770Sstevel@tonic-gate 		bcopy(callbackops, &dhp->dh_callbackops,
36780Sstevel@tonic-gate 		    sizeof (struct devmap_callback_ctl));
36790Sstevel@tonic-gate 	}
36800Sstevel@tonic-gate 	/*
36810Sstevel@tonic-gate 	 * Initialize dh_lock if we want to do remap.
36820Sstevel@tonic-gate 	 */
36830Sstevel@tonic-gate 	if (dhp->dh_flags & DEVMAP_ALLOW_REMAP) {
36840Sstevel@tonic-gate 		mutex_init(&dhp->dh_lock, NULL, MUTEX_DEFAULT, NULL);
36850Sstevel@tonic-gate 		dhp->dh_flags |= DEVMAP_LOCK_INITED;
36860Sstevel@tonic-gate 	}
36870Sstevel@tonic-gate 
36880Sstevel@tonic-gate 	dhp->dh_flags |= DEVMAP_SETUP_DONE;
36890Sstevel@tonic-gate 
36900Sstevel@tonic-gate 	return (DDI_SUCCESS);
36910Sstevel@tonic-gate }
36920Sstevel@tonic-gate 
36930Sstevel@tonic-gate int
36940Sstevel@tonic-gate devmap_umem_remap(devmap_cookie_t dhc, dev_info_t *dip,
36950Sstevel@tonic-gate     ddi_umem_cookie_t cookie, offset_t off, size_t len, uint_t maxprot,
36960Sstevel@tonic-gate     uint_t flags, ddi_device_acc_attr_t *accattrp)
36970Sstevel@tonic-gate {
36980Sstevel@tonic-gate 	devmap_handle_t *dhp = (devmap_handle_t *)dhc;
36990Sstevel@tonic-gate 	struct ddi_umem_cookie *cp = (struct ddi_umem_cookie *)cookie;
37000Sstevel@tonic-gate 
37010Sstevel@tonic-gate 	TRACE_4(TR_FAC_DEVMAP, TR_DEVMAP_UMEM_REMAP,
37020Sstevel@tonic-gate 	    "devmap_umem_remap:start dhp=%p offset=%llx cookie=%p len=%lx",
37030Sstevel@tonic-gate 	    (void *)dhp, off, cookie, len);
37040Sstevel@tonic-gate 	DEBUGF(2, (CE_CONT, "devmap_umem_remap: dhp %p offset %llx "
37050Sstevel@tonic-gate 	    "cookie %p len %lx\n", (void *)dhp, off, (void *)cookie, len));
37060Sstevel@tonic-gate 
37070Sstevel@tonic-gate #ifdef lint
37080Sstevel@tonic-gate 	dip = dip;
37090Sstevel@tonic-gate 	accattrp = accattrp;
37100Sstevel@tonic-gate #endif
37110Sstevel@tonic-gate 	/*
37120Sstevel@tonic-gate 	 * Reture failure if setup has not been done or no remap permission
37130Sstevel@tonic-gate 	 * has been granted during the setup.
37140Sstevel@tonic-gate 	 */
37150Sstevel@tonic-gate 	if ((dhp->dh_flags & DEVMAP_SETUP_DONE) == 0 ||
37160Sstevel@tonic-gate 		(dhp->dh_flags & DEVMAP_ALLOW_REMAP) == 0)
37170Sstevel@tonic-gate 		return (DDI_FAILURE);
37180Sstevel@tonic-gate 
37190Sstevel@tonic-gate 	/* No flags supported for remap yet */
37200Sstevel@tonic-gate 	if (flags != 0)
37210Sstevel@tonic-gate 		return (DDI_FAILURE);
37220Sstevel@tonic-gate 
3723*1900Seota 	/* check if the cache and endian attributes are supported */
3724*1900Seota 	if (i_ddi_check_cache_attr(flags) == B_FALSE ||
3725*1900Seota 	    i_ddi_check_endian_attr(accattrp) == B_FALSE)
3726*1900Seota 		return (DDI_FAILURE);
3727*1900Seota 
37280Sstevel@tonic-gate 	if ((dhp->dh_prot & dhp->dh_orig_maxprot & maxprot) != dhp->dh_prot)
37290Sstevel@tonic-gate 		return (DDI_FAILURE);
37300Sstevel@tonic-gate 
37310Sstevel@tonic-gate 	/* For UMEM_TRASH, this restriction is not needed */
37320Sstevel@tonic-gate 	if ((off + len) > cp->size)
37330Sstevel@tonic-gate 		return (DDI_FAILURE);
37340Sstevel@tonic-gate 
37350Sstevel@tonic-gate 	HOLD_DHP_LOCK(dhp);
37360Sstevel@tonic-gate 	/*
37370Sstevel@tonic-gate 	 * Unload the old mapping, so next fault will setup the new mappings
37380Sstevel@tonic-gate 	 * Do this while holding the dhp lock so other faults dont reestablish
37390Sstevel@tonic-gate 	 * the mappings
37400Sstevel@tonic-gate 	 */
37410Sstevel@tonic-gate 	hat_unload(dhp->dh_seg->s_as->a_hat, dhp->dh_uvaddr,
37420Sstevel@tonic-gate 		dhp->dh_len, HAT_UNLOAD|HAT_UNLOAD_OTHER);
37430Sstevel@tonic-gate 
37440Sstevel@tonic-gate 	dhp->dh_cookie = cookie;
37450Sstevel@tonic-gate 	dhp->dh_roff = ptob(btop(off));
37460Sstevel@tonic-gate 	dhp->dh_cvaddr = cp->cvaddr + dhp->dh_roff;
3747*1900Seota 	/* set HAT cache attributes */
3748*1900Seota 	i_ddi_cacheattr_to_hatacc(flags, &dhp->dh_hat_attr);
3749*1900Seota 	/* set HAT endianess attributes */
3750*1900Seota 	i_ddi_devacc_to_hatacc(accattrp, &dhp->dh_hat_attr);
37510Sstevel@tonic-gate 
37520Sstevel@tonic-gate 	/* clear the large page size flag */
37530Sstevel@tonic-gate 	dhp->dh_flags &= ~DEVMAP_FLAG_LARGE;
37540Sstevel@tonic-gate 
37550Sstevel@tonic-gate 	dhp->dh_maxprot = maxprot & dhp->dh_orig_maxprot;
37560Sstevel@tonic-gate 	ASSERT((dhp->dh_prot & dhp->dh_orig_maxprot & maxprot) == dhp->dh_prot);
37570Sstevel@tonic-gate 	RELE_DHP_LOCK(dhp);
37580Sstevel@tonic-gate 	return (DDI_SUCCESS);
37590Sstevel@tonic-gate }
37600Sstevel@tonic-gate 
37610Sstevel@tonic-gate /*
37620Sstevel@tonic-gate  * to set timeout value for the driver's context management callback, e.g.
37630Sstevel@tonic-gate  * devmap_access().
37640Sstevel@tonic-gate  */
37650Sstevel@tonic-gate void
37660Sstevel@tonic-gate devmap_set_ctx_timeout(devmap_cookie_t dhc, clock_t ticks)
37670Sstevel@tonic-gate {
37680Sstevel@tonic-gate 	devmap_handle_t *dhp = (devmap_handle_t *)dhc;
37690Sstevel@tonic-gate 
37700Sstevel@tonic-gate 	TRACE_2(TR_FAC_DEVMAP, TR_DEVMAP_SET_CTX_TIMEOUT,
37710Sstevel@tonic-gate 	    "devmap_set_ctx_timeout:start dhp=%p ticks=%x",
37720Sstevel@tonic-gate 	    (void *)dhp, ticks);
37730Sstevel@tonic-gate 	dhp->dh_timeout_length = ticks;
37740Sstevel@tonic-gate }
37750Sstevel@tonic-gate 
37760Sstevel@tonic-gate int
37770Sstevel@tonic-gate devmap_default_access(devmap_cookie_t dhp, void *pvtp, offset_t off,
37780Sstevel@tonic-gate     size_t len, uint_t type, uint_t rw)
37790Sstevel@tonic-gate {
37800Sstevel@tonic-gate #ifdef lint
37810Sstevel@tonic-gate 	pvtp = pvtp;
37820Sstevel@tonic-gate #endif
37830Sstevel@tonic-gate 
37840Sstevel@tonic-gate 	TRACE_0(TR_FAC_DEVMAP, TR_DEVMAP_DEFAULT_ACCESS,
37850Sstevel@tonic-gate 	    "devmap_default_access:start");
37860Sstevel@tonic-gate 	return (devmap_load(dhp, off, len, type, rw));
37870Sstevel@tonic-gate }
37880Sstevel@tonic-gate 
37890Sstevel@tonic-gate /*
37900Sstevel@tonic-gate  * segkmem_alloc() wrapper to allocate memory which is both
37910Sstevel@tonic-gate  * non-relocatable (for DR) and sharelocked, since the rest
37920Sstevel@tonic-gate  * of this segment driver requires it.
37930Sstevel@tonic-gate  */
37940Sstevel@tonic-gate static void *
37950Sstevel@tonic-gate devmap_alloc_pages(vmem_t *vmp, size_t size, int vmflag)
37960Sstevel@tonic-gate {
37970Sstevel@tonic-gate 	ASSERT(vmp != NULL);
37980Sstevel@tonic-gate 	ASSERT(kvseg.s_base != NULL);
37990Sstevel@tonic-gate 	vmflag |= (VM_NORELOC | SEGKMEM_SHARELOCKED);
38000Sstevel@tonic-gate 	return (segkmem_alloc(vmp, size, vmflag));
38010Sstevel@tonic-gate }
38020Sstevel@tonic-gate 
38030Sstevel@tonic-gate /*
38040Sstevel@tonic-gate  * This is where things are a bit incestrous with seg_kmem: unlike
38050Sstevel@tonic-gate  * seg_kp, seg_kmem does not keep its pages long-term sharelocked, so
38060Sstevel@tonic-gate  * we need to do a bit of a dance around that to prevent duplication of
38070Sstevel@tonic-gate  * code until we decide to bite the bullet and implement a new kernel
38080Sstevel@tonic-gate  * segment for driver-allocated memory that is exported to user space.
38090Sstevel@tonic-gate  */
38100Sstevel@tonic-gate static void
38110Sstevel@tonic-gate devmap_free_pages(vmem_t *vmp, void *inaddr, size_t size)
38120Sstevel@tonic-gate {
38130Sstevel@tonic-gate 	page_t *pp;
38140Sstevel@tonic-gate 	caddr_t addr = inaddr;
38150Sstevel@tonic-gate 	caddr_t eaddr;
38160Sstevel@tonic-gate 	pgcnt_t npages = btopr(size);
38170Sstevel@tonic-gate 
38180Sstevel@tonic-gate 	ASSERT(vmp != NULL);
38190Sstevel@tonic-gate 	ASSERT(kvseg.s_base != NULL);
38200Sstevel@tonic-gate 	ASSERT(((uintptr_t)addr & PAGEOFFSET) == 0);
38210Sstevel@tonic-gate 
38220Sstevel@tonic-gate 	hat_unload(kas.a_hat, addr, size, HAT_UNLOAD_UNLOCK);
38230Sstevel@tonic-gate 
38240Sstevel@tonic-gate 	for (eaddr = addr + size; addr < eaddr; addr += PAGESIZE) {
38250Sstevel@tonic-gate 		/*
38260Sstevel@tonic-gate 		 * Use page_find() instead of page_lookup() to find the page
38270Sstevel@tonic-gate 		 * since we know that it is hashed and has a shared lock.
38280Sstevel@tonic-gate 		 */
38290Sstevel@tonic-gate 		pp = page_find(&kvp, (u_offset_t)(uintptr_t)addr);
38300Sstevel@tonic-gate 
38310Sstevel@tonic-gate 		if (pp == NULL)
38320Sstevel@tonic-gate 			panic("devmap_free_pages: page not found");
38330Sstevel@tonic-gate 		if (!page_tryupgrade(pp)) {
38340Sstevel@tonic-gate 			page_unlock(pp);
38350Sstevel@tonic-gate 			pp = page_lookup(&kvp, (u_offset_t)(uintptr_t)addr,
38360Sstevel@tonic-gate 			    SE_EXCL);
38370Sstevel@tonic-gate 			if (pp == NULL)
38380Sstevel@tonic-gate 				panic("devmap_free_pages: page already freed");
38390Sstevel@tonic-gate 		}
38400Sstevel@tonic-gate 		/* Clear p_lckcnt so page_destroy() doesn't update availrmem */
38410Sstevel@tonic-gate 		pp->p_lckcnt = 0;
38420Sstevel@tonic-gate 		page_destroy(pp, 0);
38430Sstevel@tonic-gate 	}
38440Sstevel@tonic-gate 	page_unresv(npages);
38450Sstevel@tonic-gate 
38460Sstevel@tonic-gate 	if (vmp != NULL)
38470Sstevel@tonic-gate 		vmem_free(vmp, inaddr, size);
38480Sstevel@tonic-gate }
38490Sstevel@tonic-gate 
38500Sstevel@tonic-gate /*
38510Sstevel@tonic-gate  * devmap_umem_alloc_np() replaces kmem_zalloc() as the method for
38520Sstevel@tonic-gate  * allocating non-pageable kmem in response to a ddi_umem_alloc()
38530Sstevel@tonic-gate  * default request. For now we allocate our own pages and we keep
38540Sstevel@tonic-gate  * them long-term sharelocked, since: A) the fault routines expect the
38550Sstevel@tonic-gate  * memory to already be locked; B) pageable umem is already long-term
38560Sstevel@tonic-gate  * locked; C) it's a lot of work to make it otherwise, particuarly
38570Sstevel@tonic-gate  * since the nexus layer expects the pages to never fault. An RFE is to
38580Sstevel@tonic-gate  * not keep the pages long-term locked, but instead to be able to
38590Sstevel@tonic-gate  * take faults on them and simply look them up in kvp in case we
38600Sstevel@tonic-gate  * fault on them. Even then, we must take care not to let pageout
38610Sstevel@tonic-gate  * steal them from us since the data must remain resident; if we
38620Sstevel@tonic-gate  * do this we must come up with some way to pin the pages to prevent
38630Sstevel@tonic-gate  * faults while a driver is doing DMA to/from them.
38640Sstevel@tonic-gate  */
38650Sstevel@tonic-gate static void *
38660Sstevel@tonic-gate devmap_umem_alloc_np(size_t size, size_t flags)
38670Sstevel@tonic-gate {
38680Sstevel@tonic-gate 	void *buf;
38690Sstevel@tonic-gate 	int vmflags = (flags & DDI_UMEM_NOSLEEP)? VM_NOSLEEP : VM_SLEEP;
38700Sstevel@tonic-gate 
38710Sstevel@tonic-gate 	buf = vmem_alloc(umem_np_arena, size, vmflags);
38720Sstevel@tonic-gate 	if (buf != NULL)
38730Sstevel@tonic-gate 		bzero(buf, size);
38740Sstevel@tonic-gate 	return (buf);
38750Sstevel@tonic-gate }
38760Sstevel@tonic-gate 
38770Sstevel@tonic-gate static void
38780Sstevel@tonic-gate devmap_umem_free_np(void *addr, size_t size)
38790Sstevel@tonic-gate {
38800Sstevel@tonic-gate 	vmem_free(umem_np_arena, addr, size);
38810Sstevel@tonic-gate }
38820Sstevel@tonic-gate 
38830Sstevel@tonic-gate /*
38840Sstevel@tonic-gate  * allocate page aligned kernel memory for exporting to user land.
38850Sstevel@tonic-gate  * The devmap framework will use the cookie allocated by ddi_umem_alloc()
38860Sstevel@tonic-gate  * to find a user virtual address that is in same color as the address
38870Sstevel@tonic-gate  * allocated here.
38880Sstevel@tonic-gate  */
38890Sstevel@tonic-gate void *
38900Sstevel@tonic-gate ddi_umem_alloc(size_t size, int flags, ddi_umem_cookie_t *cookie)
38910Sstevel@tonic-gate {
38920Sstevel@tonic-gate 	register size_t len = ptob(btopr(size));
38930Sstevel@tonic-gate 	void *buf = NULL;
38940Sstevel@tonic-gate 	struct ddi_umem_cookie *cp;
38950Sstevel@tonic-gate 	int iflags = 0;
38960Sstevel@tonic-gate 
38970Sstevel@tonic-gate 	*cookie = NULL;
38980Sstevel@tonic-gate 
38990Sstevel@tonic-gate 	TRACE_0(TR_FAC_DEVMAP, TR_DEVMAP_UMEM_ALLOC,
39000Sstevel@tonic-gate 	    "devmap_umem_alloc:start");
39010Sstevel@tonic-gate 	if (len == 0)
39020Sstevel@tonic-gate 		return ((void *)NULL);
39030Sstevel@tonic-gate 
39040Sstevel@tonic-gate 	/*
39050Sstevel@tonic-gate 	 * allocate cookie
39060Sstevel@tonic-gate 	 */
39070Sstevel@tonic-gate 	if ((cp = kmem_zalloc(sizeof (struct ddi_umem_cookie),
39080Sstevel@tonic-gate 		flags & DDI_UMEM_NOSLEEP ? KM_NOSLEEP : KM_SLEEP)) == NULL) {
39090Sstevel@tonic-gate 		ASSERT(flags & DDI_UMEM_NOSLEEP);
39100Sstevel@tonic-gate 		return ((void *)NULL);
39110Sstevel@tonic-gate 	}
39120Sstevel@tonic-gate 
39130Sstevel@tonic-gate 	if (flags & DDI_UMEM_PAGEABLE) {
39140Sstevel@tonic-gate 		/* Only one of the flags is allowed */
39150Sstevel@tonic-gate 		ASSERT(!(flags & DDI_UMEM_TRASH));
39160Sstevel@tonic-gate 		/* initialize resource with 0 */
39170Sstevel@tonic-gate 		iflags = KPD_ZERO;
39180Sstevel@tonic-gate 
39190Sstevel@tonic-gate 		/*
39200Sstevel@tonic-gate 		 * to allocate unlocked pageable memory, use segkp_get() to
39210Sstevel@tonic-gate 		 * create a segkp segment.  Since segkp can only service kas,
39220Sstevel@tonic-gate 		 * other segment drivers such as segdev have to do
39230Sstevel@tonic-gate 		 * as_fault(segkp, SOFTLOCK) in its fault routine,
39240Sstevel@tonic-gate 		 */
39250Sstevel@tonic-gate 		if (flags & DDI_UMEM_NOSLEEP)
39260Sstevel@tonic-gate 			iflags |= KPD_NOWAIT;
39270Sstevel@tonic-gate 
39280Sstevel@tonic-gate 		if ((buf = segkp_get(segkp, len, iflags)) == NULL) {
39290Sstevel@tonic-gate 			kmem_free(cp, sizeof (struct ddi_umem_cookie));
39300Sstevel@tonic-gate 			return ((void *)NULL);
39310Sstevel@tonic-gate 		}
39320Sstevel@tonic-gate 		cp->type = KMEM_PAGEABLE;
39330Sstevel@tonic-gate 		mutex_init(&cp->lock, NULL, MUTEX_DEFAULT, NULL);
39340Sstevel@tonic-gate 		cp->locked = 0;
39350Sstevel@tonic-gate 	} else if (flags & DDI_UMEM_TRASH) {
39360Sstevel@tonic-gate 		/* Only one of the flags is allowed */
39370Sstevel@tonic-gate 		ASSERT(!(flags & DDI_UMEM_PAGEABLE));
39380Sstevel@tonic-gate 		cp->type = UMEM_TRASH;
39390Sstevel@tonic-gate 		buf = NULL;
39400Sstevel@tonic-gate 	} else {
39410Sstevel@tonic-gate 		if ((buf = devmap_umem_alloc_np(len, flags)) == NULL) {
39420Sstevel@tonic-gate 			kmem_free(cp, sizeof (struct ddi_umem_cookie));
39430Sstevel@tonic-gate 			return ((void *)NULL);
39440Sstevel@tonic-gate 		}
39450Sstevel@tonic-gate 
39460Sstevel@tonic-gate 		cp->type = KMEM_NON_PAGEABLE;
39470Sstevel@tonic-gate 	}
39480Sstevel@tonic-gate 
39490Sstevel@tonic-gate 	/*
39500Sstevel@tonic-gate 	 * need to save size here.  size will be used when
39510Sstevel@tonic-gate 	 * we do kmem_free.
39520Sstevel@tonic-gate 	 */
39530Sstevel@tonic-gate 	cp->size = len;
39540Sstevel@tonic-gate 	cp->cvaddr = (caddr_t)buf;
39550Sstevel@tonic-gate 
39560Sstevel@tonic-gate 	*cookie =  (void *)cp;
39570Sstevel@tonic-gate 	return (buf);
39580Sstevel@tonic-gate }
39590Sstevel@tonic-gate 
39600Sstevel@tonic-gate void
39610Sstevel@tonic-gate ddi_umem_free(ddi_umem_cookie_t cookie)
39620Sstevel@tonic-gate {
39630Sstevel@tonic-gate 	struct ddi_umem_cookie *cp;
39640Sstevel@tonic-gate 
39650Sstevel@tonic-gate 	TRACE_0(TR_FAC_DEVMAP, TR_DEVMAP_UMEM_FREE,
39660Sstevel@tonic-gate 	    "devmap_umem_free:start");
39670Sstevel@tonic-gate 
39680Sstevel@tonic-gate 	/*
39690Sstevel@tonic-gate 	 * if cookie is NULL, no effects on the system
39700Sstevel@tonic-gate 	 */
39710Sstevel@tonic-gate 	if (cookie == NULL)
39720Sstevel@tonic-gate 		return;
39730Sstevel@tonic-gate 
39740Sstevel@tonic-gate 	cp = (struct ddi_umem_cookie *)cookie;
39750Sstevel@tonic-gate 
39760Sstevel@tonic-gate 	switch (cp->type) {
39770Sstevel@tonic-gate 	case KMEM_PAGEABLE :
39780Sstevel@tonic-gate 		ASSERT(cp->cvaddr != NULL && cp->size != 0);
39790Sstevel@tonic-gate 		/*
39800Sstevel@tonic-gate 		 * Check if there are still any pending faults on the cookie
39810Sstevel@tonic-gate 		 * while the driver is deleting it,
39820Sstevel@tonic-gate 		 * XXX - could change to an ASSERT but wont catch errant drivers
39830Sstevel@tonic-gate 		 */
39840Sstevel@tonic-gate 		mutex_enter(&cp->lock);
39850Sstevel@tonic-gate 		if (cp->locked) {
39860Sstevel@tonic-gate 			mutex_exit(&cp->lock);
39870Sstevel@tonic-gate 			panic("ddi_umem_free for cookie with pending faults %p",
39880Sstevel@tonic-gate 			    (void *)cp);
39890Sstevel@tonic-gate 			return;
39900Sstevel@tonic-gate 		}
39910Sstevel@tonic-gate 
39920Sstevel@tonic-gate 		segkp_release(segkp, cp->cvaddr);
39930Sstevel@tonic-gate 
39940Sstevel@tonic-gate 		/*
39950Sstevel@tonic-gate 		 * release mutex associated with this cookie.
39960Sstevel@tonic-gate 		 */
39970Sstevel@tonic-gate 		mutex_destroy(&cp->lock);
39980Sstevel@tonic-gate 		break;
39990Sstevel@tonic-gate 	case KMEM_NON_PAGEABLE :
40000Sstevel@tonic-gate 		ASSERT(cp->cvaddr != NULL && cp->size != 0);
40010Sstevel@tonic-gate 		devmap_umem_free_np(cp->cvaddr, cp->size);
40020Sstevel@tonic-gate 		break;
40030Sstevel@tonic-gate 	case UMEM_TRASH :
40040Sstevel@tonic-gate 		break;
40050Sstevel@tonic-gate 	case UMEM_LOCKED :
40060Sstevel@tonic-gate 		/* Callers should use ddi_umem_unlock for this type */
40070Sstevel@tonic-gate 		ddi_umem_unlock(cookie);
40080Sstevel@tonic-gate 		/* Frees the cookie too */
40090Sstevel@tonic-gate 		return;
40100Sstevel@tonic-gate 	default:
40110Sstevel@tonic-gate 		/* panic so we can diagnose the underlying cause */
40120Sstevel@tonic-gate 		panic("ddi_umem_free: illegal cookie type 0x%x\n",
40130Sstevel@tonic-gate 		    cp->type);
40140Sstevel@tonic-gate 	}
40150Sstevel@tonic-gate 
40160Sstevel@tonic-gate 	kmem_free(cookie, sizeof (struct ddi_umem_cookie));
40170Sstevel@tonic-gate }
40180Sstevel@tonic-gate 
40190Sstevel@tonic-gate 
40200Sstevel@tonic-gate static int
40210Sstevel@tonic-gate segdev_getmemid(struct seg *seg, caddr_t addr, memid_t *memidp)
40220Sstevel@tonic-gate {
40230Sstevel@tonic-gate 	struct segdev_data *sdp = (struct segdev_data *)seg->s_data;
40240Sstevel@tonic-gate 
40250Sstevel@tonic-gate 	/*
40260Sstevel@tonic-gate 	 * It looks as if it is always mapped shared
40270Sstevel@tonic-gate 	 */
40280Sstevel@tonic-gate 	TRACE_0(TR_FAC_DEVMAP, TR_DEVMAP_GETMEMID,
40290Sstevel@tonic-gate 	    "segdev_getmemid:start");
40300Sstevel@tonic-gate 	memidp->val[0] = (uintptr_t)VTOCVP(sdp->vp);
40310Sstevel@tonic-gate 	memidp->val[1] = sdp->offset + (uintptr_t)(addr - seg->s_base);
40320Sstevel@tonic-gate 	return (0);
40330Sstevel@tonic-gate }
40340Sstevel@tonic-gate 
40350Sstevel@tonic-gate /*ARGSUSED*/
40360Sstevel@tonic-gate static lgrp_mem_policy_info_t *
40370Sstevel@tonic-gate segdev_getpolicy(struct seg *seg, caddr_t addr)
40380Sstevel@tonic-gate {
40390Sstevel@tonic-gate 	return (NULL);
40400Sstevel@tonic-gate }
40410Sstevel@tonic-gate 
4042670Selowe /*ARGSUSED*/
4043670Selowe static int
4044670Selowe segdev_capable(struct seg *seg, segcapability_t capability)
4045670Selowe {
4046670Selowe 	return (0);
4047670Selowe }
4048670Selowe 
40490Sstevel@tonic-gate /*
40500Sstevel@tonic-gate  * ddi_umem_alloc() non-pageable quantum cache max size.
40510Sstevel@tonic-gate  * This is just a SWAG.
40520Sstevel@tonic-gate  */
40530Sstevel@tonic-gate #define	DEVMAP_UMEM_QUANTUM	(8*PAGESIZE)
40540Sstevel@tonic-gate 
40550Sstevel@tonic-gate /*
40560Sstevel@tonic-gate  * Initialize seg_dev from boot. This routine sets up the trash page
40570Sstevel@tonic-gate  * and creates the umem_np_arena used to back non-pageable memory
40580Sstevel@tonic-gate  * requests.
40590Sstevel@tonic-gate  */
40600Sstevel@tonic-gate void
40610Sstevel@tonic-gate segdev_init(void)
40620Sstevel@tonic-gate {
40630Sstevel@tonic-gate 	struct seg kseg;
40640Sstevel@tonic-gate 
40650Sstevel@tonic-gate 	umem_np_arena = vmem_create("umem_np", NULL, 0, PAGESIZE,
40660Sstevel@tonic-gate 	    devmap_alloc_pages, devmap_free_pages, heap_arena,
40670Sstevel@tonic-gate 	    DEVMAP_UMEM_QUANTUM, VM_SLEEP);
40680Sstevel@tonic-gate 
40690Sstevel@tonic-gate 	kseg.s_as = &kas;
40700Sstevel@tonic-gate 	trashpp = page_create_va(&trashvp, 0, PAGESIZE,
40710Sstevel@tonic-gate 	    PG_NORELOC | PG_EXCL | PG_WAIT, &kseg, NULL);
40720Sstevel@tonic-gate 	if (trashpp == NULL)
40730Sstevel@tonic-gate 		panic("segdev_init: failed to create trash page");
40740Sstevel@tonic-gate 	pagezero(trashpp, 0, PAGESIZE);
40750Sstevel@tonic-gate 	page_downgrade(trashpp);
40760Sstevel@tonic-gate }
40770Sstevel@tonic-gate 
40780Sstevel@tonic-gate /*
40790Sstevel@tonic-gate  * Invoke platform-dependent support routines so that /proc can have
40800Sstevel@tonic-gate  * the platform code deal with curious hardware.
40810Sstevel@tonic-gate  */
40820Sstevel@tonic-gate int
40830Sstevel@tonic-gate segdev_copyfrom(struct seg *seg,
40840Sstevel@tonic-gate     caddr_t uaddr, const void *devaddr, void *kaddr, size_t len)
40850Sstevel@tonic-gate {
40860Sstevel@tonic-gate 	struct segdev_data *sdp = (struct segdev_data *)seg->s_data;
40870Sstevel@tonic-gate 	struct snode *sp = VTOS(VTOCVP(sdp->vp));
40880Sstevel@tonic-gate 
40890Sstevel@tonic-gate 	return (e_ddi_copyfromdev(sp->s_dip,
40900Sstevel@tonic-gate 	    (off_t)(uaddr - seg->s_base), devaddr, kaddr, len));
40910Sstevel@tonic-gate }
40920Sstevel@tonic-gate 
40930Sstevel@tonic-gate int
40940Sstevel@tonic-gate segdev_copyto(struct seg *seg,
40950Sstevel@tonic-gate     caddr_t uaddr, const void *kaddr, void *devaddr, size_t len)
40960Sstevel@tonic-gate {
40970Sstevel@tonic-gate 	struct segdev_data *sdp = (struct segdev_data *)seg->s_data;
40980Sstevel@tonic-gate 	struct snode *sp = VTOS(VTOCVP(sdp->vp));
40990Sstevel@tonic-gate 
41000Sstevel@tonic-gate 	return (e_ddi_copytodev(sp->s_dip,
41010Sstevel@tonic-gate 	    (off_t)(uaddr - seg->s_base), kaddr, devaddr, len));
41020Sstevel@tonic-gate }
4103