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