xref: /illumos-gate/usr/src/uts/common/vm/seg_dev.c (revision 1bd5c35f)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
57c478bd9Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
67c478bd9Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
77c478bd9Sstevel@tonic-gate  * with the License.
87c478bd9Sstevel@tonic-gate  *
97c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
107c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
117c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
127c478bd9Sstevel@tonic-gate  * and limitations under the License.
137c478bd9Sstevel@tonic-gate  *
147c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
157c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
167c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
177c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
187c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
197c478bd9Sstevel@tonic-gate  *
207c478bd9Sstevel@tonic-gate  * CDDL HEADER END
217c478bd9Sstevel@tonic-gate  */
227c478bd9Sstevel@tonic-gate /*
237c478bd9Sstevel@tonic-gate  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
247c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
257c478bd9Sstevel@tonic-gate  */
267c478bd9Sstevel@tonic-gate 
277c478bd9Sstevel@tonic-gate /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
287c478bd9Sstevel@tonic-gate /*	  All Rights Reserved  	*/
297c478bd9Sstevel@tonic-gate 
307c478bd9Sstevel@tonic-gate /*
317c478bd9Sstevel@tonic-gate  * University Copyright- Copyright (c) 1982, 1986, 1988
327c478bd9Sstevel@tonic-gate  * The Regents of the University of California
337c478bd9Sstevel@tonic-gate  * All Rights Reserved
347c478bd9Sstevel@tonic-gate  *
357c478bd9Sstevel@tonic-gate  * University Acknowledgment- Portions of this document are derived from
367c478bd9Sstevel@tonic-gate  * software developed by the University of California, Berkeley, and its
377c478bd9Sstevel@tonic-gate  * contributors.
387c478bd9Sstevel@tonic-gate  */
397c478bd9Sstevel@tonic-gate 
407c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
417c478bd9Sstevel@tonic-gate 
427c478bd9Sstevel@tonic-gate /*
437c478bd9Sstevel@tonic-gate  * VM - segment of a mapped device.
447c478bd9Sstevel@tonic-gate  *
457c478bd9Sstevel@tonic-gate  * This segment driver is used when mapping character special devices.
467c478bd9Sstevel@tonic-gate  */
477c478bd9Sstevel@tonic-gate 
487c478bd9Sstevel@tonic-gate #include <sys/types.h>
497c478bd9Sstevel@tonic-gate #include <sys/t_lock.h>
507c478bd9Sstevel@tonic-gate #include <sys/sysmacros.h>
517c478bd9Sstevel@tonic-gate #include <sys/vtrace.h>
527c478bd9Sstevel@tonic-gate #include <sys/systm.h>
537c478bd9Sstevel@tonic-gate #include <sys/vmsystm.h>
547c478bd9Sstevel@tonic-gate #include <sys/mman.h>
557c478bd9Sstevel@tonic-gate #include <sys/errno.h>
567c478bd9Sstevel@tonic-gate #include <sys/kmem.h>
577c478bd9Sstevel@tonic-gate #include <sys/cmn_err.h>
587c478bd9Sstevel@tonic-gate #include <sys/vnode.h>
597c478bd9Sstevel@tonic-gate #include <sys/proc.h>
607c478bd9Sstevel@tonic-gate #include <sys/conf.h>
617c478bd9Sstevel@tonic-gate #include <sys/debug.h>
627c478bd9Sstevel@tonic-gate #include <sys/ddidevmap.h>
637c478bd9Sstevel@tonic-gate #include <sys/lgrp.h>
647c478bd9Sstevel@tonic-gate 
657c478bd9Sstevel@tonic-gate #include <vm/page.h>
667c478bd9Sstevel@tonic-gate #include <vm/hat.h>
677c478bd9Sstevel@tonic-gate #include <vm/as.h>
687c478bd9Sstevel@tonic-gate #include <vm/seg.h>
697c478bd9Sstevel@tonic-gate #include <vm/seg_dev.h>
707c478bd9Sstevel@tonic-gate #include <vm/seg_kp.h>
717c478bd9Sstevel@tonic-gate #include <vm/seg_kmem.h>
727c478bd9Sstevel@tonic-gate #include <vm/vpage.h>
737c478bd9Sstevel@tonic-gate 
747c478bd9Sstevel@tonic-gate #include <sys/sunddi.h>
757c478bd9Sstevel@tonic-gate #include <sys/esunddi.h>
767c478bd9Sstevel@tonic-gate #include <sys/fs/snode.h>
777c478bd9Sstevel@tonic-gate 
787c478bd9Sstevel@tonic-gate #if DEBUG
797c478bd9Sstevel@tonic-gate int segdev_debug;
807c478bd9Sstevel@tonic-gate #define	DEBUGF(level, args) { if (segdev_debug >= (level)) cmn_err args; }
817c478bd9Sstevel@tonic-gate #else
827c478bd9Sstevel@tonic-gate #define	DEBUGF(level, args)
837c478bd9Sstevel@tonic-gate #endif
847c478bd9Sstevel@tonic-gate 
857c478bd9Sstevel@tonic-gate /* Default timeout for devmap context management */
867c478bd9Sstevel@tonic-gate #define	CTX_TIMEOUT_VALUE 0
877c478bd9Sstevel@tonic-gate 
887c478bd9Sstevel@tonic-gate #define	HOLD_DHP_LOCK(dhp)  if (dhp->dh_flags & DEVMAP_ALLOW_REMAP) \
897c478bd9Sstevel@tonic-gate 			{ mutex_enter(&dhp->dh_lock); }
907c478bd9Sstevel@tonic-gate 
917c478bd9Sstevel@tonic-gate #define	RELE_DHP_LOCK(dhp) if (dhp->dh_flags & DEVMAP_ALLOW_REMAP) \
927c478bd9Sstevel@tonic-gate 			{ mutex_exit(&dhp->dh_lock); }
937c478bd9Sstevel@tonic-gate 
947c478bd9Sstevel@tonic-gate #define	round_down_p2(a, s)	((a) & ~((s) - 1))
957c478bd9Sstevel@tonic-gate #define	round_up_p2(a, s)	(((a) + (s) - 1) & ~((s) - 1))
967c478bd9Sstevel@tonic-gate 
977c478bd9Sstevel@tonic-gate /*
987c478bd9Sstevel@tonic-gate  * VA_PA_ALIGNED checks to see if both VA and PA are on pgsize boundary
997c478bd9Sstevel@tonic-gate  * VA_PA_PGSIZE_ALIGNED check to see if VA is aligned with PA w.r.t. pgsize
1007c478bd9Sstevel@tonic-gate  */
1017c478bd9Sstevel@tonic-gate #define	VA_PA_ALIGNED(uvaddr, paddr, pgsize)		\
1027c478bd9Sstevel@tonic-gate 	(((uvaddr | paddr) & (pgsize - 1)) == 0)
1037c478bd9Sstevel@tonic-gate #define	VA_PA_PGSIZE_ALIGNED(uvaddr, paddr, pgsize)	\
1047c478bd9Sstevel@tonic-gate 	(((uvaddr ^ paddr) & (pgsize - 1)) == 0)
1057c478bd9Sstevel@tonic-gate 
1067c478bd9Sstevel@tonic-gate #define	vpgtob(n)	((n) * sizeof (struct vpage))	/* For brevity */
1077c478bd9Sstevel@tonic-gate 
1087c478bd9Sstevel@tonic-gate #define	VTOCVP(vp)	(VTOS(vp)->s_commonvp)	/* we "know" it's an snode */
1097c478bd9Sstevel@tonic-gate 
1107c478bd9Sstevel@tonic-gate static struct devmap_ctx *devmapctx_list = NULL;
1117c478bd9Sstevel@tonic-gate static struct devmap_softlock *devmap_slist = NULL;
1127c478bd9Sstevel@tonic-gate 
1137c478bd9Sstevel@tonic-gate /*
1147c478bd9Sstevel@tonic-gate  * mutex, vnode and page for the page of zeros we use for the trash mappings.
1157c478bd9Sstevel@tonic-gate  * One trash page is allocated on the first ddi_umem_setup call that uses it
1167c478bd9Sstevel@tonic-gate  * XXX Eventually, we may want to combine this with what segnf does when all
1177c478bd9Sstevel@tonic-gate  * hat layers implement HAT_NOFAULT.
1187c478bd9Sstevel@tonic-gate  *
1197c478bd9Sstevel@tonic-gate  * The trash page is used when the backing store for a userland mapping is
1207c478bd9Sstevel@tonic-gate  * removed but the application semantics do not take kindly to a SIGBUS.
1217c478bd9Sstevel@tonic-gate  * In that scenario, the applications pages are mapped to some dummy page
1227c478bd9Sstevel@tonic-gate  * which returns garbage on read and writes go into a common place.
1237c478bd9Sstevel@tonic-gate  * (Perfect for NO_FAULT semantics)
1247c478bd9Sstevel@tonic-gate  * The device driver is responsible to communicating to the app with some
1257c478bd9Sstevel@tonic-gate  * other mechanism that such remapping has happened and the app should take
1267c478bd9Sstevel@tonic-gate  * corrective action.
1277c478bd9Sstevel@tonic-gate  * We can also use an anonymous memory page as there is no requirement to
1287c478bd9Sstevel@tonic-gate  * keep the page locked, however this complicates the fault code. RFE.
1297c478bd9Sstevel@tonic-gate  */
1307c478bd9Sstevel@tonic-gate static struct vnode trashvp;
1317c478bd9Sstevel@tonic-gate static struct page *trashpp;
1327c478bd9Sstevel@tonic-gate 
1337c478bd9Sstevel@tonic-gate /* Non-pageable kernel memory is allocated from the umem_np_arena. */
1347c478bd9Sstevel@tonic-gate static vmem_t *umem_np_arena;
1357c478bd9Sstevel@tonic-gate 
1367c478bd9Sstevel@tonic-gate /* Set the cookie to a value we know will never be a valid umem_cookie */
1377c478bd9Sstevel@tonic-gate #define	DEVMAP_DEVMEM_COOKIE	((ddi_umem_cookie_t)0x1)
1387c478bd9Sstevel@tonic-gate 
1397c478bd9Sstevel@tonic-gate /*
1407c478bd9Sstevel@tonic-gate  * Macros to check if type of devmap handle
1417c478bd9Sstevel@tonic-gate  */
1427c478bd9Sstevel@tonic-gate #define	cookie_is_devmem(c)	\
1437c478bd9Sstevel@tonic-gate 	((c) == (struct ddi_umem_cookie *)DEVMAP_DEVMEM_COOKIE)
1447c478bd9Sstevel@tonic-gate 
1457c478bd9Sstevel@tonic-gate #define	cookie_is_pmem(c)	\
1467c478bd9Sstevel@tonic-gate 	((c) == (struct ddi_umem_cookie *)DEVMAP_PMEM_COOKIE)
1477c478bd9Sstevel@tonic-gate 
1487c478bd9Sstevel@tonic-gate #define	cookie_is_kpmem(c)	(!cookie_is_devmem(c) && !cookie_is_pmem(c) &&\
1497c478bd9Sstevel@tonic-gate 	((c)->type == KMEM_PAGEABLE))
1507c478bd9Sstevel@tonic-gate 
1517c478bd9Sstevel@tonic-gate #define	dhp_is_devmem(dhp)	\
1527c478bd9Sstevel@tonic-gate 	(cookie_is_devmem((struct ddi_umem_cookie *)((dhp)->dh_cookie)))
1537c478bd9Sstevel@tonic-gate 
1547c478bd9Sstevel@tonic-gate #define	dhp_is_pmem(dhp)	\
1557c478bd9Sstevel@tonic-gate 	(cookie_is_pmem((struct ddi_umem_cookie *)((dhp)->dh_cookie)))
1567c478bd9Sstevel@tonic-gate 
1577c478bd9Sstevel@tonic-gate #define	dhp_is_kpmem(dhp)	\
1587c478bd9Sstevel@tonic-gate 	(cookie_is_kpmem((struct ddi_umem_cookie *)((dhp)->dh_cookie)))
1597c478bd9Sstevel@tonic-gate 
1607c478bd9Sstevel@tonic-gate /*
1617c478bd9Sstevel@tonic-gate  * Private seg op routines.
1627c478bd9Sstevel@tonic-gate  */
1637c478bd9Sstevel@tonic-gate static int	segdev_dup(struct seg *, struct seg *);
1647c478bd9Sstevel@tonic-gate static int	segdev_unmap(struct seg *, caddr_t, size_t);
1657c478bd9Sstevel@tonic-gate static void	segdev_free(struct seg *);
1667c478bd9Sstevel@tonic-gate static faultcode_t segdev_fault(struct hat *, struct seg *, caddr_t, size_t,
1677c478bd9Sstevel@tonic-gate 		    enum fault_type, enum seg_rw);
1687c478bd9Sstevel@tonic-gate static faultcode_t segdev_faulta(struct seg *, caddr_t);
1697c478bd9Sstevel@tonic-gate static int	segdev_setprot(struct seg *, caddr_t, size_t, uint_t);
1707c478bd9Sstevel@tonic-gate static int	segdev_checkprot(struct seg *, caddr_t, size_t, uint_t);
1717c478bd9Sstevel@tonic-gate static void	segdev_badop(void);
1727c478bd9Sstevel@tonic-gate static int	segdev_sync(struct seg *, caddr_t, size_t, int, uint_t);
1737c478bd9Sstevel@tonic-gate static size_t	segdev_incore(struct seg *, caddr_t, size_t, char *);
1747c478bd9Sstevel@tonic-gate static int	segdev_lockop(struct seg *, caddr_t, size_t, int, int,
1757c478bd9Sstevel@tonic-gate 		    ulong_t *, size_t);
1767c478bd9Sstevel@tonic-gate static int	segdev_getprot(struct seg *, caddr_t, size_t, uint_t *);
1777c478bd9Sstevel@tonic-gate static u_offset_t	segdev_getoffset(struct seg *, caddr_t);
1787c478bd9Sstevel@tonic-gate static int	segdev_gettype(struct seg *, caddr_t);
1797c478bd9Sstevel@tonic-gate static int	segdev_getvp(struct seg *, caddr_t, struct vnode **);
1807c478bd9Sstevel@tonic-gate static int	segdev_advise(struct seg *, caddr_t, size_t, uint_t);
1817c478bd9Sstevel@tonic-gate static void	segdev_dump(struct seg *);
1827c478bd9Sstevel@tonic-gate static int	segdev_pagelock(struct seg *, caddr_t, size_t,
1837c478bd9Sstevel@tonic-gate 		    struct page ***, enum lock_type, enum seg_rw);
1847c478bd9Sstevel@tonic-gate static int	segdev_setpagesize(struct seg *, caddr_t, size_t, uint_t);
1857c478bd9Sstevel@tonic-gate static int	segdev_getmemid(struct seg *, caddr_t, memid_t *);
1867c478bd9Sstevel@tonic-gate static lgrp_mem_policy_info_t	*segdev_getpolicy(struct seg *, caddr_t);
187*1bd5c35fSelowe static int	segdev_capable(struct seg *, segcapability_t);
1887c478bd9Sstevel@tonic-gate 
1897c478bd9Sstevel@tonic-gate /*
1907c478bd9Sstevel@tonic-gate  * XXX	this struct is used by rootnex_map_fault to identify
1917c478bd9Sstevel@tonic-gate  *	the segment it has been passed. So if you make it
1927c478bd9Sstevel@tonic-gate  *	"static" you'll need to fix rootnex_map_fault.
1937c478bd9Sstevel@tonic-gate  */
1947c478bd9Sstevel@tonic-gate struct seg_ops segdev_ops = {
1957c478bd9Sstevel@tonic-gate 	segdev_dup,
1967c478bd9Sstevel@tonic-gate 	segdev_unmap,
1977c478bd9Sstevel@tonic-gate 	segdev_free,
1987c478bd9Sstevel@tonic-gate 	segdev_fault,
1997c478bd9Sstevel@tonic-gate 	segdev_faulta,
2007c478bd9Sstevel@tonic-gate 	segdev_setprot,
2017c478bd9Sstevel@tonic-gate 	segdev_checkprot,
2027c478bd9Sstevel@tonic-gate 	(int (*)())segdev_badop,	/* kluster */
2037c478bd9Sstevel@tonic-gate 	(size_t (*)(struct seg *))NULL,	/* swapout */
2047c478bd9Sstevel@tonic-gate 	segdev_sync,			/* sync */
2057c478bd9Sstevel@tonic-gate 	segdev_incore,
2067c478bd9Sstevel@tonic-gate 	segdev_lockop,			/* lockop */
2077c478bd9Sstevel@tonic-gate 	segdev_getprot,
2087c478bd9Sstevel@tonic-gate 	segdev_getoffset,
2097c478bd9Sstevel@tonic-gate 	segdev_gettype,
2107c478bd9Sstevel@tonic-gate 	segdev_getvp,
2117c478bd9Sstevel@tonic-gate 	segdev_advise,
2127c478bd9Sstevel@tonic-gate 	segdev_dump,
2137c478bd9Sstevel@tonic-gate 	segdev_pagelock,
2147c478bd9Sstevel@tonic-gate 	segdev_setpagesize,
2157c478bd9Sstevel@tonic-gate 	segdev_getmemid,
2167c478bd9Sstevel@tonic-gate 	segdev_getpolicy,
217*1bd5c35fSelowe 	segdev_capable,
2187c478bd9Sstevel@tonic-gate };
2197c478bd9Sstevel@tonic-gate 
2207c478bd9Sstevel@tonic-gate /*
2217c478bd9Sstevel@tonic-gate  * Private segdev support routines
2227c478bd9Sstevel@tonic-gate  */
2237c478bd9Sstevel@tonic-gate static struct segdev_data *sdp_alloc(void);
2247c478bd9Sstevel@tonic-gate 
2257c478bd9Sstevel@tonic-gate static void segdev_softunlock(struct hat *, struct seg *, caddr_t,
2267c478bd9Sstevel@tonic-gate     size_t, enum seg_rw);
2277c478bd9Sstevel@tonic-gate 
2287c478bd9Sstevel@tonic-gate static faultcode_t segdev_faultpage(struct hat *, struct seg *, caddr_t,
2297c478bd9Sstevel@tonic-gate     struct vpage *, enum fault_type, enum seg_rw, devmap_handle_t *);
2307c478bd9Sstevel@tonic-gate 
2317c478bd9Sstevel@tonic-gate static faultcode_t segdev_faultpages(struct hat *, struct seg *, caddr_t,
2327c478bd9Sstevel@tonic-gate     size_t, enum fault_type, enum seg_rw, devmap_handle_t *);
2337c478bd9Sstevel@tonic-gate 
2347c478bd9Sstevel@tonic-gate static struct devmap_ctx *devmap_ctxinit(dev_t, ulong_t);
2357c478bd9Sstevel@tonic-gate static struct devmap_softlock *devmap_softlock_init(dev_t, ulong_t);
2367c478bd9Sstevel@tonic-gate static void devmap_softlock_rele(devmap_handle_t *);
2377c478bd9Sstevel@tonic-gate static void devmap_ctx_rele(devmap_handle_t *);
2387c478bd9Sstevel@tonic-gate 
2397c478bd9Sstevel@tonic-gate static void devmap_ctxto(void *);
2407c478bd9Sstevel@tonic-gate 
2417c478bd9Sstevel@tonic-gate static devmap_handle_t *devmap_find_handle(devmap_handle_t *dhp_head,
2427c478bd9Sstevel@tonic-gate     caddr_t addr);
2437c478bd9Sstevel@tonic-gate 
2447c478bd9Sstevel@tonic-gate static ulong_t devmap_roundup(devmap_handle_t *dhp, ulong_t offset, size_t len,
2457c478bd9Sstevel@tonic-gate     ulong_t *opfn, ulong_t *pagesize);
2467c478bd9Sstevel@tonic-gate 
2477c478bd9Sstevel@tonic-gate static void free_devmap_handle(devmap_handle_t *dhp);
2487c478bd9Sstevel@tonic-gate 
2497c478bd9Sstevel@tonic-gate static int devmap_handle_dup(devmap_handle_t *dhp, devmap_handle_t **new_dhp,
2507c478bd9Sstevel@tonic-gate     struct seg *newseg);
2517c478bd9Sstevel@tonic-gate 
2527c478bd9Sstevel@tonic-gate static devmap_handle_t *devmap_handle_unmap(devmap_handle_t *dhp);
2537c478bd9Sstevel@tonic-gate 
2547c478bd9Sstevel@tonic-gate static void devmap_handle_unmap_head(devmap_handle_t *dhp, size_t len);
2557c478bd9Sstevel@tonic-gate 
2567c478bd9Sstevel@tonic-gate static void devmap_handle_unmap_tail(devmap_handle_t *dhp, caddr_t addr);
2577c478bd9Sstevel@tonic-gate 
2587c478bd9Sstevel@tonic-gate static int devmap_device(devmap_handle_t *dhp, struct as *as, caddr_t *addr,
2597c478bd9Sstevel@tonic-gate     offset_t off, size_t len, uint_t flags);
2607c478bd9Sstevel@tonic-gate 
2617c478bd9Sstevel@tonic-gate static void devmap_get_large_pgsize(devmap_handle_t *dhp, size_t len,
2627c478bd9Sstevel@tonic-gate     caddr_t addr, size_t *llen, caddr_t *laddr);
2637c478bd9Sstevel@tonic-gate 
2647c478bd9Sstevel@tonic-gate static void devmap_handle_reduce_len(devmap_handle_t *dhp, size_t len);
2657c478bd9Sstevel@tonic-gate 
2667c478bd9Sstevel@tonic-gate static void *devmap_alloc_pages(vmem_t *vmp, size_t size, int vmflag);
2677c478bd9Sstevel@tonic-gate static void devmap_free_pages(vmem_t *vmp, void *inaddr, size_t size);
2687c478bd9Sstevel@tonic-gate 
2697c478bd9Sstevel@tonic-gate static void *devmap_umem_alloc_np(size_t size, size_t flags);
2707c478bd9Sstevel@tonic-gate static void devmap_umem_free_np(void *addr, size_t size);
2717c478bd9Sstevel@tonic-gate 
2727c478bd9Sstevel@tonic-gate /*
2737c478bd9Sstevel@tonic-gate  * routines to lock and unlock underlying segkp segment for
2747c478bd9Sstevel@tonic-gate  * KMEM_PAGEABLE type cookies.
2757c478bd9Sstevel@tonic-gate  */
2767c478bd9Sstevel@tonic-gate static faultcode_t  acquire_kpmem_lock(struct ddi_umem_cookie *, size_t);
2777c478bd9Sstevel@tonic-gate static void release_kpmem_lock(struct ddi_umem_cookie *, size_t);
2787c478bd9Sstevel@tonic-gate 
2797c478bd9Sstevel@tonic-gate /*
2807c478bd9Sstevel@tonic-gate  * Routines to synchronize F_SOFTLOCK and F_INVAL faults for
2817c478bd9Sstevel@tonic-gate  * drivers with devmap_access callbacks
2827c478bd9Sstevel@tonic-gate  */
2837c478bd9Sstevel@tonic-gate static int devmap_softlock_enter(struct devmap_softlock *, size_t,
2847c478bd9Sstevel@tonic-gate 	enum fault_type);
2857c478bd9Sstevel@tonic-gate static void devmap_softlock_exit(struct devmap_softlock *, size_t,
2867c478bd9Sstevel@tonic-gate 	enum fault_type);
2877c478bd9Sstevel@tonic-gate 
2887c478bd9Sstevel@tonic-gate static kmutex_t devmapctx_lock;
2897c478bd9Sstevel@tonic-gate 
2907c478bd9Sstevel@tonic-gate static kmutex_t devmap_slock;
2917c478bd9Sstevel@tonic-gate 
2927c478bd9Sstevel@tonic-gate /*
2937c478bd9Sstevel@tonic-gate  * Initialize the thread callbacks and thread private data.
2947c478bd9Sstevel@tonic-gate  */
2957c478bd9Sstevel@tonic-gate static struct devmap_ctx *
2967c478bd9Sstevel@tonic-gate devmap_ctxinit(dev_t dev, ulong_t id)
2977c478bd9Sstevel@tonic-gate {
2987c478bd9Sstevel@tonic-gate 	struct devmap_ctx	*devctx;
2997c478bd9Sstevel@tonic-gate 	struct devmap_ctx	*tmp;
3007c478bd9Sstevel@tonic-gate 	dev_info_t		*dip;
3017c478bd9Sstevel@tonic-gate 
3027c478bd9Sstevel@tonic-gate 	tmp =  kmem_zalloc(sizeof (struct devmap_ctx), KM_SLEEP);
3037c478bd9Sstevel@tonic-gate 
3047c478bd9Sstevel@tonic-gate 	mutex_enter(&devmapctx_lock);
3057c478bd9Sstevel@tonic-gate 
3067c478bd9Sstevel@tonic-gate 	dip = e_ddi_hold_devi_by_dev(dev, 0);
3077c478bd9Sstevel@tonic-gate 	ASSERT(dip != NULL);
3087c478bd9Sstevel@tonic-gate 	ddi_release_devi(dip);
3097c478bd9Sstevel@tonic-gate 
3107c478bd9Sstevel@tonic-gate 	for (devctx = devmapctx_list; devctx != NULL; devctx = devctx->next)
3117c478bd9Sstevel@tonic-gate 		if ((devctx->dip == dip) && (devctx->id == id))
3127c478bd9Sstevel@tonic-gate 			break;
3137c478bd9Sstevel@tonic-gate 
3147c478bd9Sstevel@tonic-gate 	if (devctx == NULL) {
3157c478bd9Sstevel@tonic-gate 		devctx = tmp;
3167c478bd9Sstevel@tonic-gate 		devctx->dip = dip;
3177c478bd9Sstevel@tonic-gate 		devctx->id = id;
3187c478bd9Sstevel@tonic-gate 		mutex_init(&devctx->lock, NULL, MUTEX_DEFAULT, NULL);
3197c478bd9Sstevel@tonic-gate 		cv_init(&devctx->cv, NULL, CV_DEFAULT, NULL);
3207c478bd9Sstevel@tonic-gate 		devctx->next = devmapctx_list;
3217c478bd9Sstevel@tonic-gate 		devmapctx_list = devctx;
3227c478bd9Sstevel@tonic-gate 	} else
3237c478bd9Sstevel@tonic-gate 		kmem_free(tmp, sizeof (struct devmap_ctx));
3247c478bd9Sstevel@tonic-gate 
3257c478bd9Sstevel@tonic-gate 	mutex_enter(&devctx->lock);
3267c478bd9Sstevel@tonic-gate 	devctx->refcnt++;
3277c478bd9Sstevel@tonic-gate 	mutex_exit(&devctx->lock);
3287c478bd9Sstevel@tonic-gate 	mutex_exit(&devmapctx_lock);
3297c478bd9Sstevel@tonic-gate 
3307c478bd9Sstevel@tonic-gate 	return (devctx);
3317c478bd9Sstevel@tonic-gate }
3327c478bd9Sstevel@tonic-gate 
3337c478bd9Sstevel@tonic-gate /*
3347c478bd9Sstevel@tonic-gate  * Timeout callback called if a CPU has not given up the device context
3357c478bd9Sstevel@tonic-gate  * within dhp->dh_timeout_length ticks
3367c478bd9Sstevel@tonic-gate  */
3377c478bd9Sstevel@tonic-gate static void
3387c478bd9Sstevel@tonic-gate devmap_ctxto(void *data)
3397c478bd9Sstevel@tonic-gate {
3407c478bd9Sstevel@tonic-gate 	struct devmap_ctx *devctx = data;
3417c478bd9Sstevel@tonic-gate 
3427c478bd9Sstevel@tonic-gate 	TRACE_1(TR_FAC_DEVMAP, TR_DEVMAP_CTXTO,
3437c478bd9Sstevel@tonic-gate 	    "devmap_ctxto:timeout expired, devctx=%p", (void *)devctx);
3447c478bd9Sstevel@tonic-gate 	mutex_enter(&devctx->lock);
3457c478bd9Sstevel@tonic-gate 	/*
3467c478bd9Sstevel@tonic-gate 	 * Set oncpu = 0 so the next mapping trying to get the device context
3477c478bd9Sstevel@tonic-gate 	 * can.
3487c478bd9Sstevel@tonic-gate 	 */
3497c478bd9Sstevel@tonic-gate 	devctx->oncpu = 0;
3507c478bd9Sstevel@tonic-gate 	devctx->timeout = 0;
3517c478bd9Sstevel@tonic-gate 	cv_signal(&devctx->cv);
3527c478bd9Sstevel@tonic-gate 	mutex_exit(&devctx->lock);
3537c478bd9Sstevel@tonic-gate }
3547c478bd9Sstevel@tonic-gate 
3557c478bd9Sstevel@tonic-gate /*
3567c478bd9Sstevel@tonic-gate  * Create a device segment.
3577c478bd9Sstevel@tonic-gate  */
3587c478bd9Sstevel@tonic-gate int
3597c478bd9Sstevel@tonic-gate segdev_create(struct seg *seg, void *argsp)
3607c478bd9Sstevel@tonic-gate {
3617c478bd9Sstevel@tonic-gate 	struct segdev_data *sdp;
3627c478bd9Sstevel@tonic-gate 	struct segdev_crargs *a = (struct segdev_crargs *)argsp;
3637c478bd9Sstevel@tonic-gate 	devmap_handle_t *dhp = (devmap_handle_t *)a->devmap_data;
3647c478bd9Sstevel@tonic-gate 	int error;
3657c478bd9Sstevel@tonic-gate 
3667c478bd9Sstevel@tonic-gate 	/*
3677c478bd9Sstevel@tonic-gate 	 * Since the address space is "write" locked, we
3687c478bd9Sstevel@tonic-gate 	 * don't need the segment lock to protect "segdev" data.
3697c478bd9Sstevel@tonic-gate 	 */
3707c478bd9Sstevel@tonic-gate 	ASSERT(seg->s_as && AS_WRITE_HELD(seg->s_as, &seg->s_as->a_lock));
3717c478bd9Sstevel@tonic-gate 
3727c478bd9Sstevel@tonic-gate 	hat_map(seg->s_as->a_hat, seg->s_base, seg->s_size, HAT_MAP);
3737c478bd9Sstevel@tonic-gate 
3747c478bd9Sstevel@tonic-gate 	sdp = sdp_alloc();
3757c478bd9Sstevel@tonic-gate 
3767c478bd9Sstevel@tonic-gate 	sdp->mapfunc = a->mapfunc;
3777c478bd9Sstevel@tonic-gate 	sdp->offset = a->offset;
3787c478bd9Sstevel@tonic-gate 	sdp->prot = a->prot;
3797c478bd9Sstevel@tonic-gate 	sdp->maxprot = a->maxprot;
3807c478bd9Sstevel@tonic-gate 	sdp->type = a->type;
3817c478bd9Sstevel@tonic-gate 	sdp->pageprot = 0;
3827c478bd9Sstevel@tonic-gate 	sdp->softlockcnt = 0;
3837c478bd9Sstevel@tonic-gate 	sdp->vpage = NULL;
3847c478bd9Sstevel@tonic-gate 
3857c478bd9Sstevel@tonic-gate 	if (sdp->mapfunc == NULL)
3867c478bd9Sstevel@tonic-gate 		sdp->devmap_data = dhp;
3877c478bd9Sstevel@tonic-gate 	else
3887c478bd9Sstevel@tonic-gate 		sdp->devmap_data = dhp = NULL;
3897c478bd9Sstevel@tonic-gate 
3907c478bd9Sstevel@tonic-gate 	sdp->hat_flags = a->hat_flags;
3917c478bd9Sstevel@tonic-gate 	sdp->hat_attr = a->hat_attr;
3927c478bd9Sstevel@tonic-gate 
3937c478bd9Sstevel@tonic-gate 	/*
3947c478bd9Sstevel@tonic-gate 	 * Currently, hat_flags supports only HAT_LOAD_NOCONSIST
3957c478bd9Sstevel@tonic-gate 	 */
3967c478bd9Sstevel@tonic-gate 	ASSERT(!(sdp->hat_flags & ~HAT_LOAD_NOCONSIST));
3977c478bd9Sstevel@tonic-gate 
3987c478bd9Sstevel@tonic-gate 	/*
3997c478bd9Sstevel@tonic-gate 	 * Hold shadow vnode -- segdev only deals with
4007c478bd9Sstevel@tonic-gate 	 * character (VCHR) devices. We use the common
4017c478bd9Sstevel@tonic-gate 	 * vp to hang pages on.
4027c478bd9Sstevel@tonic-gate 	 */
4037c478bd9Sstevel@tonic-gate 	sdp->vp = specfind(a->dev, VCHR);
4047c478bd9Sstevel@tonic-gate 	ASSERT(sdp->vp != NULL);
4057c478bd9Sstevel@tonic-gate 
4067c478bd9Sstevel@tonic-gate 	seg->s_ops = &segdev_ops;
4077c478bd9Sstevel@tonic-gate 	seg->s_data = sdp;
4087c478bd9Sstevel@tonic-gate 
4097c478bd9Sstevel@tonic-gate 	while (dhp != NULL) {
4107c478bd9Sstevel@tonic-gate 		dhp->dh_seg = seg;
4117c478bd9Sstevel@tonic-gate 		dhp = dhp->dh_next;
4127c478bd9Sstevel@tonic-gate 	}
4137c478bd9Sstevel@tonic-gate 
4147c478bd9Sstevel@tonic-gate 	/*
4157c478bd9Sstevel@tonic-gate 	 * Inform the vnode of the new mapping.
4167c478bd9Sstevel@tonic-gate 	 */
4177c478bd9Sstevel@tonic-gate 	/*
4187c478bd9Sstevel@tonic-gate 	 * It is ok to use pass sdp->maxprot to ADDMAP rather than to use
4197c478bd9Sstevel@tonic-gate 	 * dhp specific maxprot because spec_addmap does not use maxprot.
4207c478bd9Sstevel@tonic-gate 	 */
4217c478bd9Sstevel@tonic-gate 	error = VOP_ADDMAP(VTOCVP(sdp->vp), sdp->offset,
4227c478bd9Sstevel@tonic-gate 	    seg->s_as, seg->s_base, seg->s_size,
4237c478bd9Sstevel@tonic-gate 	    sdp->prot, sdp->maxprot, sdp->type, CRED());
4247c478bd9Sstevel@tonic-gate 
4257c478bd9Sstevel@tonic-gate 	if (error != 0) {
4267c478bd9Sstevel@tonic-gate 		sdp->devmap_data = NULL;
4277c478bd9Sstevel@tonic-gate 		hat_unload(seg->s_as->a_hat, seg->s_base, seg->s_size,
4287c478bd9Sstevel@tonic-gate 		    HAT_UNLOAD_UNMAP);
4297c478bd9Sstevel@tonic-gate 	}
4307c478bd9Sstevel@tonic-gate 
4317c478bd9Sstevel@tonic-gate 	return (error);
4327c478bd9Sstevel@tonic-gate }
4337c478bd9Sstevel@tonic-gate 
4347c478bd9Sstevel@tonic-gate static struct segdev_data *
4357c478bd9Sstevel@tonic-gate sdp_alloc(void)
4367c478bd9Sstevel@tonic-gate {
4377c478bd9Sstevel@tonic-gate 	struct segdev_data *sdp;
4387c478bd9Sstevel@tonic-gate 
4397c478bd9Sstevel@tonic-gate 	sdp = kmem_zalloc(sizeof (struct segdev_data), KM_SLEEP);
4407c478bd9Sstevel@tonic-gate 	mutex_init(&sdp->lock, NULL, MUTEX_DEFAULT, NULL);
4417c478bd9Sstevel@tonic-gate 
4427c478bd9Sstevel@tonic-gate 	return (sdp);
4437c478bd9Sstevel@tonic-gate }
4447c478bd9Sstevel@tonic-gate 
4457c478bd9Sstevel@tonic-gate /*
4467c478bd9Sstevel@tonic-gate  * Duplicate seg and return new segment in newseg.
4477c478bd9Sstevel@tonic-gate  */
4487c478bd9Sstevel@tonic-gate static int
4497c478bd9Sstevel@tonic-gate segdev_dup(struct seg *seg, struct seg *newseg)
4507c478bd9Sstevel@tonic-gate {
4517c478bd9Sstevel@tonic-gate 	struct segdev_data *sdp = (struct segdev_data *)seg->s_data;
4527c478bd9Sstevel@tonic-gate 	struct segdev_data *newsdp;
4537c478bd9Sstevel@tonic-gate 	devmap_handle_t *dhp = (devmap_handle_t *)sdp->devmap_data;
4547c478bd9Sstevel@tonic-gate 	size_t npages;
4557c478bd9Sstevel@tonic-gate 	int ret;
4567c478bd9Sstevel@tonic-gate 
4577c478bd9Sstevel@tonic-gate 	TRACE_2(TR_FAC_DEVMAP, TR_DEVMAP_DUP,
4587c478bd9Sstevel@tonic-gate 	    "segdev_dup:start dhp=%p, seg=%p", (void *)dhp, (void *)seg);
4597c478bd9Sstevel@tonic-gate 
4607c478bd9Sstevel@tonic-gate 	DEBUGF(3, (CE_CONT, "segdev_dup: dhp %p seg %p\n",
4617c478bd9Sstevel@tonic-gate 	    (void *)dhp, (void *)seg));
4627c478bd9Sstevel@tonic-gate 
4637c478bd9Sstevel@tonic-gate 	/*
4647c478bd9Sstevel@tonic-gate 	 * Since the address space is "write" locked, we
4657c478bd9Sstevel@tonic-gate 	 * don't need the segment lock to protect "segdev" data.
4667c478bd9Sstevel@tonic-gate 	 */
4677c478bd9Sstevel@tonic-gate 	ASSERT(seg->s_as && AS_WRITE_HELD(seg->s_as, &seg->s_as->a_lock));
4687c478bd9Sstevel@tonic-gate 
4697c478bd9Sstevel@tonic-gate 	newsdp = sdp_alloc();
4707c478bd9Sstevel@tonic-gate 
4717c478bd9Sstevel@tonic-gate 	newseg->s_ops = seg->s_ops;
4727c478bd9Sstevel@tonic-gate 	newseg->s_data = (void *)newsdp;
4737c478bd9Sstevel@tonic-gate 
4747c478bd9Sstevel@tonic-gate 	VN_HOLD(sdp->vp);
4757c478bd9Sstevel@tonic-gate 	newsdp->vp 	= sdp->vp;
4767c478bd9Sstevel@tonic-gate 	newsdp->mapfunc = sdp->mapfunc;
4777c478bd9Sstevel@tonic-gate 	newsdp->offset	= sdp->offset;
4787c478bd9Sstevel@tonic-gate 	newsdp->pageprot = sdp->pageprot;
4797c478bd9Sstevel@tonic-gate 	newsdp->prot	= sdp->prot;
4807c478bd9Sstevel@tonic-gate 	newsdp->maxprot = sdp->maxprot;
4817c478bd9Sstevel@tonic-gate 	newsdp->type = sdp->type;
4827c478bd9Sstevel@tonic-gate 	newsdp->hat_attr = sdp->hat_attr;
4837c478bd9Sstevel@tonic-gate 	newsdp->hat_flags = sdp->hat_flags;
4847c478bd9Sstevel@tonic-gate 	newsdp->softlockcnt = 0;
4857c478bd9Sstevel@tonic-gate 
4867c478bd9Sstevel@tonic-gate 	/*
4877c478bd9Sstevel@tonic-gate 	 * Initialize per page data if the segment we are
4887c478bd9Sstevel@tonic-gate 	 * dup'ing has per page information.
4897c478bd9Sstevel@tonic-gate 	 */
4907c478bd9Sstevel@tonic-gate 	npages = seg_pages(newseg);
4917c478bd9Sstevel@tonic-gate 
4927c478bd9Sstevel@tonic-gate 	if (sdp->vpage != NULL) {
4937c478bd9Sstevel@tonic-gate 		size_t nbytes = vpgtob(npages);
4947c478bd9Sstevel@tonic-gate 
4957c478bd9Sstevel@tonic-gate 		newsdp->vpage = kmem_zalloc(nbytes, KM_SLEEP);
4967c478bd9Sstevel@tonic-gate 		bcopy(sdp->vpage, newsdp->vpage, nbytes);
4977c478bd9Sstevel@tonic-gate 	} else
4987c478bd9Sstevel@tonic-gate 		newsdp->vpage = NULL;
4997c478bd9Sstevel@tonic-gate 
5007c478bd9Sstevel@tonic-gate 	/*
5017c478bd9Sstevel@tonic-gate 	 * duplicate devmap handles
5027c478bd9Sstevel@tonic-gate 	 */
5037c478bd9Sstevel@tonic-gate 	if (dhp != NULL) {
5047c478bd9Sstevel@tonic-gate 		ret = devmap_handle_dup(dhp,
5057c478bd9Sstevel@tonic-gate 			(devmap_handle_t **)&newsdp->devmap_data, newseg);
5067c478bd9Sstevel@tonic-gate 		if (ret != 0) {
5077c478bd9Sstevel@tonic-gate 			TRACE_3(TR_FAC_DEVMAP, TR_DEVMAP_DUP_CK1,
5087c478bd9Sstevel@tonic-gate 			    "segdev_dup:ret1 ret=%x, dhp=%p seg=%p",
5097c478bd9Sstevel@tonic-gate 			    ret, (void *)dhp, (void *)seg);
5107c478bd9Sstevel@tonic-gate 			DEBUGF(1, (CE_CONT,
5117c478bd9Sstevel@tonic-gate 			    "segdev_dup: ret %x dhp %p seg %p\n",
5127c478bd9Sstevel@tonic-gate 			    ret, (void *)dhp, (void *)seg));
5137c478bd9Sstevel@tonic-gate 			return (ret);
5147c478bd9Sstevel@tonic-gate 		}
5157c478bd9Sstevel@tonic-gate 	}
5167c478bd9Sstevel@tonic-gate 
5177c478bd9Sstevel@tonic-gate 	/*
5187c478bd9Sstevel@tonic-gate 	 * Inform the common vnode of the new mapping.
5197c478bd9Sstevel@tonic-gate 	 */
5207c478bd9Sstevel@tonic-gate 	return (VOP_ADDMAP(VTOCVP(newsdp->vp),
5217c478bd9Sstevel@tonic-gate 		newsdp->offset, newseg->s_as,
5227c478bd9Sstevel@tonic-gate 		newseg->s_base, newseg->s_size, newsdp->prot,
5237c478bd9Sstevel@tonic-gate 		newsdp->maxprot, sdp->type, CRED()));
5247c478bd9Sstevel@tonic-gate }
5257c478bd9Sstevel@tonic-gate 
5267c478bd9Sstevel@tonic-gate /*
5277c478bd9Sstevel@tonic-gate  * duplicate devmap handles
5287c478bd9Sstevel@tonic-gate  */
5297c478bd9Sstevel@tonic-gate static int
5307c478bd9Sstevel@tonic-gate devmap_handle_dup(devmap_handle_t *dhp, devmap_handle_t **new_dhp,
5317c478bd9Sstevel@tonic-gate     struct seg *newseg)
5327c478bd9Sstevel@tonic-gate {
5337c478bd9Sstevel@tonic-gate 	devmap_handle_t *newdhp_save = NULL;
5347c478bd9Sstevel@tonic-gate 	devmap_handle_t *newdhp = NULL;
5357c478bd9Sstevel@tonic-gate 	struct devmap_callback_ctl *callbackops;
5367c478bd9Sstevel@tonic-gate 
5377c478bd9Sstevel@tonic-gate 	while (dhp != NULL) {
5387c478bd9Sstevel@tonic-gate 		newdhp = kmem_alloc(sizeof (devmap_handle_t), KM_SLEEP);
5397c478bd9Sstevel@tonic-gate 
5407c478bd9Sstevel@tonic-gate 		/* Need to lock the original dhp while copying if REMAP */
5417c478bd9Sstevel@tonic-gate 		HOLD_DHP_LOCK(dhp);
5427c478bd9Sstevel@tonic-gate 		bcopy(dhp, newdhp, sizeof (devmap_handle_t));
5437c478bd9Sstevel@tonic-gate 		RELE_DHP_LOCK(dhp);
5447c478bd9Sstevel@tonic-gate 		newdhp->dh_seg = newseg;
5457c478bd9Sstevel@tonic-gate 		newdhp->dh_next = NULL;
5467c478bd9Sstevel@tonic-gate 		if (newdhp_save != NULL)
5477c478bd9Sstevel@tonic-gate 			newdhp_save->dh_next = newdhp;
5487c478bd9Sstevel@tonic-gate 		else
5497c478bd9Sstevel@tonic-gate 			*new_dhp = newdhp;
5507c478bd9Sstevel@tonic-gate 		newdhp_save = newdhp;
5517c478bd9Sstevel@tonic-gate 
5527c478bd9Sstevel@tonic-gate 		callbackops = &newdhp->dh_callbackops;
5537c478bd9Sstevel@tonic-gate 
5547c478bd9Sstevel@tonic-gate 		if (dhp->dh_softlock != NULL)
5557c478bd9Sstevel@tonic-gate 			newdhp->dh_softlock = devmap_softlock_init(
5567c478bd9Sstevel@tonic-gate 			    newdhp->dh_dev,
5577c478bd9Sstevel@tonic-gate 			    (ulong_t)callbackops->devmap_access);
5587c478bd9Sstevel@tonic-gate 		if (dhp->dh_ctx != NULL)
5597c478bd9Sstevel@tonic-gate 			newdhp->dh_ctx = devmap_ctxinit(newdhp->dh_dev,
5607c478bd9Sstevel@tonic-gate 			    (ulong_t)callbackops->devmap_access);
5617c478bd9Sstevel@tonic-gate 
5627c478bd9Sstevel@tonic-gate 		/*
5637c478bd9Sstevel@tonic-gate 		 * Initialize dh_lock if we want to do remap.
5647c478bd9Sstevel@tonic-gate 		 */
5657c478bd9Sstevel@tonic-gate 		if (newdhp->dh_flags & DEVMAP_ALLOW_REMAP) {
5667c478bd9Sstevel@tonic-gate 			mutex_init(&newdhp->dh_lock, NULL, MUTEX_DEFAULT, NULL);
5677c478bd9Sstevel@tonic-gate 			newdhp->dh_flags |= DEVMAP_LOCK_INITED;
5687c478bd9Sstevel@tonic-gate 		}
5697c478bd9Sstevel@tonic-gate 
5707c478bd9Sstevel@tonic-gate 		if (callbackops->devmap_dup != NULL) {
5717c478bd9Sstevel@tonic-gate 			int ret;
5727c478bd9Sstevel@tonic-gate 
5737c478bd9Sstevel@tonic-gate 			/*
5747c478bd9Sstevel@tonic-gate 			 * Call the dup callback so that the driver can
5757c478bd9Sstevel@tonic-gate 			 * duplicate its private data.
5767c478bd9Sstevel@tonic-gate 			 */
5777c478bd9Sstevel@tonic-gate 			ret = (*callbackops->devmap_dup)(dhp, dhp->dh_pvtp,
5787c478bd9Sstevel@tonic-gate 				(devmap_cookie_t *)newdhp, &newdhp->dh_pvtp);
5797c478bd9Sstevel@tonic-gate 
5807c478bd9Sstevel@tonic-gate 			if (ret != 0) {
5817c478bd9Sstevel@tonic-gate 				/*
5827c478bd9Sstevel@tonic-gate 				 * We want to free up this segment as the driver
5837c478bd9Sstevel@tonic-gate 				 * has indicated that we can't dup it.  But we
5847c478bd9Sstevel@tonic-gate 				 * don't want to call the drivers, devmap_unmap,
5857c478bd9Sstevel@tonic-gate 				 * callback function as the driver does not
5867c478bd9Sstevel@tonic-gate 				 * think this segment exists. The caller of
5877c478bd9Sstevel@tonic-gate 				 * devmap_dup will call seg_free on newseg
5887c478bd9Sstevel@tonic-gate 				 * as it was the caller that allocated the
5897c478bd9Sstevel@tonic-gate 				 * segment.
5907c478bd9Sstevel@tonic-gate 				 */
5917c478bd9Sstevel@tonic-gate 				DEBUGF(1, (CE_CONT, "devmap_handle_dup ERROR: "
5927c478bd9Sstevel@tonic-gate 				    "newdhp %p dhp %p\n", (void *)newdhp,
5937c478bd9Sstevel@tonic-gate 				    (void *)dhp));
5947c478bd9Sstevel@tonic-gate 				callbackops->devmap_unmap = NULL;
5957c478bd9Sstevel@tonic-gate 				return (ret);
5967c478bd9Sstevel@tonic-gate 			}
5977c478bd9Sstevel@tonic-gate 		}
5987c478bd9Sstevel@tonic-gate 
5997c478bd9Sstevel@tonic-gate 		dhp = dhp->dh_next;
6007c478bd9Sstevel@tonic-gate 	}
6017c478bd9Sstevel@tonic-gate 
6027c478bd9Sstevel@tonic-gate 	return (0);
6037c478bd9Sstevel@tonic-gate }
6047c478bd9Sstevel@tonic-gate 
6057c478bd9Sstevel@tonic-gate /*
6067c478bd9Sstevel@tonic-gate  * Split a segment at addr for length len.
6077c478bd9Sstevel@tonic-gate  */
6087c478bd9Sstevel@tonic-gate /*ARGSUSED*/
6097c478bd9Sstevel@tonic-gate static int
6107c478bd9Sstevel@tonic-gate segdev_unmap(struct seg *seg, caddr_t addr, size_t len)
6117c478bd9Sstevel@tonic-gate {
6127c478bd9Sstevel@tonic-gate 	register struct segdev_data *sdp = (struct segdev_data *)seg->s_data;
6137c478bd9Sstevel@tonic-gate 	register struct segdev_data *nsdp;
6147c478bd9Sstevel@tonic-gate 	register struct seg *nseg;
6157c478bd9Sstevel@tonic-gate 	register size_t	opages;		/* old segment size in pages */
6167c478bd9Sstevel@tonic-gate 	register size_t	npages;		/* new segment size in pages */
6177c478bd9Sstevel@tonic-gate 	register size_t	dpages;		/* pages being deleted (unmapped) */
6187c478bd9Sstevel@tonic-gate 	register size_t	nbytes;
6197c478bd9Sstevel@tonic-gate 	devmap_handle_t *dhp = (devmap_handle_t *)sdp->devmap_data;
6207c478bd9Sstevel@tonic-gate 	devmap_handle_t *dhpp;
6217c478bd9Sstevel@tonic-gate 	devmap_handle_t *newdhp;
6227c478bd9Sstevel@tonic-gate 	struct devmap_callback_ctl *callbackops;
6237c478bd9Sstevel@tonic-gate 	caddr_t nbase;
6247c478bd9Sstevel@tonic-gate 	offset_t off;
6257c478bd9Sstevel@tonic-gate 	ulong_t nsize;
6267c478bd9Sstevel@tonic-gate 	size_t mlen, sz;
6277c478bd9Sstevel@tonic-gate 
6287c478bd9Sstevel@tonic-gate 	TRACE_4(TR_FAC_DEVMAP, TR_DEVMAP_UNMAP,
6297c478bd9Sstevel@tonic-gate 	    "segdev_unmap:start dhp=%p, seg=%p addr=%p len=%lx",
6307c478bd9Sstevel@tonic-gate 	    (void *)dhp, (void *)seg, (void *)addr, len);
6317c478bd9Sstevel@tonic-gate 
6327c478bd9Sstevel@tonic-gate 	DEBUGF(3, (CE_CONT, "segdev_unmap: dhp %p seg %p addr %p len %lx\n",
6337c478bd9Sstevel@tonic-gate 	    (void *)dhp, (void *)seg, (void *)addr, len));
6347c478bd9Sstevel@tonic-gate 
6357c478bd9Sstevel@tonic-gate 	/*
6367c478bd9Sstevel@tonic-gate 	 * Since the address space is "write" locked, we
6377c478bd9Sstevel@tonic-gate 	 * don't need the segment lock to protect "segdev" data.
6387c478bd9Sstevel@tonic-gate 	 */
6397c478bd9Sstevel@tonic-gate 	ASSERT(seg->s_as && AS_WRITE_HELD(seg->s_as, &seg->s_as->a_lock));
6407c478bd9Sstevel@tonic-gate 
6417c478bd9Sstevel@tonic-gate 	if ((sz = sdp->softlockcnt) > 0) {
6427c478bd9Sstevel@tonic-gate 		/*
6437c478bd9Sstevel@tonic-gate 		 * Fail the unmap if pages are SOFTLOCKed through this mapping.
6447c478bd9Sstevel@tonic-gate 		 * softlockcnt is protected from change by the as write lock.
6457c478bd9Sstevel@tonic-gate 		 */
6467c478bd9Sstevel@tonic-gate 		TRACE_1(TR_FAC_DEVMAP, TR_DEVMAP_UNMAP_CK1,
6477c478bd9Sstevel@tonic-gate 		    "segdev_unmap:error softlockcnt = %ld", sz);
6487c478bd9Sstevel@tonic-gate 		DEBUGF(1, (CE_CONT, "segdev_unmap: softlockcnt %ld\n", sz));
6497c478bd9Sstevel@tonic-gate 		return (EAGAIN);
6507c478bd9Sstevel@tonic-gate 	}
6517c478bd9Sstevel@tonic-gate 
6527c478bd9Sstevel@tonic-gate 	/*
6537c478bd9Sstevel@tonic-gate 	 * Check for bad sizes
6547c478bd9Sstevel@tonic-gate 	 */
6557c478bd9Sstevel@tonic-gate 	if (addr < seg->s_base || addr + len > seg->s_base + seg->s_size ||
6567c478bd9Sstevel@tonic-gate 	    (len & PAGEOFFSET) || ((uintptr_t)addr & PAGEOFFSET))
6577c478bd9Sstevel@tonic-gate 		panic("segdev_unmap");
6587c478bd9Sstevel@tonic-gate 
6597c478bd9Sstevel@tonic-gate 	if (dhp != NULL) {
6607c478bd9Sstevel@tonic-gate 		devmap_handle_t *tdhp;
6617c478bd9Sstevel@tonic-gate 		/*
6627c478bd9Sstevel@tonic-gate 		 * If large page size was used in hat_devload(),
6637c478bd9Sstevel@tonic-gate 		 * the same page size must be used in hat_unload().
6647c478bd9Sstevel@tonic-gate 		 */
6657c478bd9Sstevel@tonic-gate 		dhpp = tdhp = devmap_find_handle(dhp, addr);
6667c478bd9Sstevel@tonic-gate 		while (tdhp != NULL) {
6677c478bd9Sstevel@tonic-gate 			if (tdhp->dh_flags & DEVMAP_FLAG_LARGE) {
6687c478bd9Sstevel@tonic-gate 				break;
6697c478bd9Sstevel@tonic-gate 			}
6707c478bd9Sstevel@tonic-gate 			tdhp = tdhp->dh_next;
6717c478bd9Sstevel@tonic-gate 		}
6727c478bd9Sstevel@tonic-gate 		if (tdhp != NULL) {	/* found a dhp using large pages */
6737c478bd9Sstevel@tonic-gate 			size_t slen = len;
6747c478bd9Sstevel@tonic-gate 			size_t mlen;
6757c478bd9Sstevel@tonic-gate 			size_t soff;
6767c478bd9Sstevel@tonic-gate 
6777c478bd9Sstevel@tonic-gate 			soff = (ulong_t)(addr - dhpp->dh_uvaddr);
6787c478bd9Sstevel@tonic-gate 			while (slen != 0) {
6797c478bd9Sstevel@tonic-gate 				mlen = MIN(slen, (dhpp->dh_len - soff));
6807c478bd9Sstevel@tonic-gate 				hat_unload(seg->s_as->a_hat, dhpp->dh_uvaddr,
6817c478bd9Sstevel@tonic-gate 					dhpp->dh_len, HAT_UNLOAD_UNMAP);
6827c478bd9Sstevel@tonic-gate 				dhpp = dhpp->dh_next;
6837c478bd9Sstevel@tonic-gate 				ASSERT(slen >= mlen);
6847c478bd9Sstevel@tonic-gate 				slen -= mlen;
6857c478bd9Sstevel@tonic-gate 				soff = 0;
6867c478bd9Sstevel@tonic-gate 			}
6877c478bd9Sstevel@tonic-gate 		} else
6887c478bd9Sstevel@tonic-gate 			hat_unload(seg->s_as->a_hat, addr, len,
6897c478bd9Sstevel@tonic-gate 				HAT_UNLOAD_UNMAP);
6907c478bd9Sstevel@tonic-gate 	} else {
6917c478bd9Sstevel@tonic-gate 		/*
6927c478bd9Sstevel@tonic-gate 		 * Unload any hardware translations in the range
6937c478bd9Sstevel@tonic-gate 		 * to be taken out.
6947c478bd9Sstevel@tonic-gate 		 */
6957c478bd9Sstevel@tonic-gate 		hat_unload(seg->s_as->a_hat, addr, len, HAT_UNLOAD_UNMAP);
6967c478bd9Sstevel@tonic-gate 	}
6977c478bd9Sstevel@tonic-gate 
6987c478bd9Sstevel@tonic-gate 	/*
6997c478bd9Sstevel@tonic-gate 	 * get the user offset which will used in the driver callbacks
7007c478bd9Sstevel@tonic-gate 	 */
7017c478bd9Sstevel@tonic-gate 	off = sdp->offset + (offset_t)(addr - seg->s_base);
7027c478bd9Sstevel@tonic-gate 
7037c478bd9Sstevel@tonic-gate 	/*
7047c478bd9Sstevel@tonic-gate 	 * Inform the vnode of the unmapping.
7057c478bd9Sstevel@tonic-gate 	 */
7067c478bd9Sstevel@tonic-gate 	ASSERT(sdp->vp != NULL);
7077c478bd9Sstevel@tonic-gate 	(void) VOP_DELMAP(VTOCVP(sdp->vp), off, seg->s_as, addr, len,
7087c478bd9Sstevel@tonic-gate 		sdp->prot, sdp->maxprot, sdp->type, CRED());
7097c478bd9Sstevel@tonic-gate 
7107c478bd9Sstevel@tonic-gate 	/*
7117c478bd9Sstevel@tonic-gate 	 * Check for entire segment
7127c478bd9Sstevel@tonic-gate 	 */
7137c478bd9Sstevel@tonic-gate 	if (addr == seg->s_base && len == seg->s_size) {
7147c478bd9Sstevel@tonic-gate 		seg_free(seg);
7157c478bd9Sstevel@tonic-gate 		return (0);
7167c478bd9Sstevel@tonic-gate 	}
7177c478bd9Sstevel@tonic-gate 
7187c478bd9Sstevel@tonic-gate 	opages = seg_pages(seg);
7197c478bd9Sstevel@tonic-gate 	dpages = btop(len);
7207c478bd9Sstevel@tonic-gate 	npages = opages - dpages;
7217c478bd9Sstevel@tonic-gate 
7227c478bd9Sstevel@tonic-gate 	/*
7237c478bd9Sstevel@tonic-gate 	 * Check for beginning of segment
7247c478bd9Sstevel@tonic-gate 	 */
7257c478bd9Sstevel@tonic-gate 	if (addr == seg->s_base) {
7267c478bd9Sstevel@tonic-gate 		if (sdp->vpage != NULL) {
7277c478bd9Sstevel@tonic-gate 			register struct vpage *ovpage;
7287c478bd9Sstevel@tonic-gate 
7297c478bd9Sstevel@tonic-gate 			ovpage = sdp->vpage;	/* keep pointer to vpage */
7307c478bd9Sstevel@tonic-gate 
7317c478bd9Sstevel@tonic-gate 			nbytes = vpgtob(npages);
7327c478bd9Sstevel@tonic-gate 			sdp->vpage = kmem_alloc(nbytes, KM_SLEEP);
7337c478bd9Sstevel@tonic-gate 			bcopy(&ovpage[dpages], sdp->vpage, nbytes);
7347c478bd9Sstevel@tonic-gate 
7357c478bd9Sstevel@tonic-gate 			/* free up old vpage */
7367c478bd9Sstevel@tonic-gate 			kmem_free(ovpage, vpgtob(opages));
7377c478bd9Sstevel@tonic-gate 		}
7387c478bd9Sstevel@tonic-gate 
7397c478bd9Sstevel@tonic-gate 		/*
7407c478bd9Sstevel@tonic-gate 		 * free devmap handles from the beginning of the mapping.
7417c478bd9Sstevel@tonic-gate 		 */
7427c478bd9Sstevel@tonic-gate 		if (dhp != NULL)
7437c478bd9Sstevel@tonic-gate 			devmap_handle_unmap_head(dhp, len);
7447c478bd9Sstevel@tonic-gate 
7457c478bd9Sstevel@tonic-gate 		sdp->offset += (offset_t)len;
7467c478bd9Sstevel@tonic-gate 
7477c478bd9Sstevel@tonic-gate 		seg->s_base += len;
7487c478bd9Sstevel@tonic-gate 		seg->s_size -= len;
7497c478bd9Sstevel@tonic-gate 
7507c478bd9Sstevel@tonic-gate 		return (0);
7517c478bd9Sstevel@tonic-gate 	}
7527c478bd9Sstevel@tonic-gate 
7537c478bd9Sstevel@tonic-gate 	/*
7547c478bd9Sstevel@tonic-gate 	 * Check for end of segment
7557c478bd9Sstevel@tonic-gate 	 */
7567c478bd9Sstevel@tonic-gate 	if (addr + len == seg->s_base + seg->s_size) {
7577c478bd9Sstevel@tonic-gate 		if (sdp->vpage != NULL) {
7587c478bd9Sstevel@tonic-gate 			register struct vpage *ovpage;
7597c478bd9Sstevel@tonic-gate 
7607c478bd9Sstevel@tonic-gate 			ovpage = sdp->vpage;	/* keep pointer to vpage */
7617c478bd9Sstevel@tonic-gate 
7627c478bd9Sstevel@tonic-gate 			nbytes = vpgtob(npages);
7637c478bd9Sstevel@tonic-gate 			sdp->vpage = kmem_alloc(nbytes, KM_SLEEP);
7647c478bd9Sstevel@tonic-gate 			bcopy(ovpage, sdp->vpage, nbytes);
7657c478bd9Sstevel@tonic-gate 
7667c478bd9Sstevel@tonic-gate 			/* free up old vpage */
7677c478bd9Sstevel@tonic-gate 			kmem_free(ovpage, vpgtob(opages));
7687c478bd9Sstevel@tonic-gate 		}
7697c478bd9Sstevel@tonic-gate 		seg->s_size -= len;
7707c478bd9Sstevel@tonic-gate 
7717c478bd9Sstevel@tonic-gate 		/*
7727c478bd9Sstevel@tonic-gate 		 * free devmap handles from addr to the end of the mapping.
7737c478bd9Sstevel@tonic-gate 		 */
7747c478bd9Sstevel@tonic-gate 		if (dhp != NULL)
7757c478bd9Sstevel@tonic-gate 			devmap_handle_unmap_tail(dhp, addr);
7767c478bd9Sstevel@tonic-gate 
7777c478bd9Sstevel@tonic-gate 		return (0);
7787c478bd9Sstevel@tonic-gate 	}
7797c478bd9Sstevel@tonic-gate 
7807c478bd9Sstevel@tonic-gate 	/*
7817c478bd9Sstevel@tonic-gate 	 * The section to go is in the middle of the segment,
7827c478bd9Sstevel@tonic-gate 	 * have to make it into two segments.  nseg is made for
7837c478bd9Sstevel@tonic-gate 	 * the high end while seg is cut down at the low end.
7847c478bd9Sstevel@tonic-gate 	 */
7857c478bd9Sstevel@tonic-gate 	nbase = addr + len;				/* new seg base */
7867c478bd9Sstevel@tonic-gate 	nsize = (seg->s_base + seg->s_size) - nbase;	/* new seg size */
7877c478bd9Sstevel@tonic-gate 	seg->s_size = addr - seg->s_base;		/* shrink old seg */
7887c478bd9Sstevel@tonic-gate 	nseg = seg_alloc(seg->s_as, nbase, nsize);
7897c478bd9Sstevel@tonic-gate 	if (nseg == NULL)
7907c478bd9Sstevel@tonic-gate 		panic("segdev_unmap seg_alloc");
7917c478bd9Sstevel@tonic-gate 
7927c478bd9Sstevel@tonic-gate 	TRACE_2(TR_FAC_DEVMAP, TR_DEVMAP_UNMAP_CK2,
7937c478bd9Sstevel@tonic-gate 	    "segdev_unmap: seg=%p nseg=%p", (void *)seg, (void *)nseg);
7947c478bd9Sstevel@tonic-gate 	DEBUGF(3, (CE_CONT, "segdev_unmap: segdev_dup seg %p nseg %p\n",
7957c478bd9Sstevel@tonic-gate 	    (void *)seg, (void *)nseg));
7967c478bd9Sstevel@tonic-gate 	nsdp = sdp_alloc();
7977c478bd9Sstevel@tonic-gate 
7987c478bd9Sstevel@tonic-gate 	nseg->s_ops = seg->s_ops;
7997c478bd9Sstevel@tonic-gate 	nseg->s_data = (void *)nsdp;
8007c478bd9Sstevel@tonic-gate 
8017c478bd9Sstevel@tonic-gate 	VN_HOLD(sdp->vp);
8027c478bd9Sstevel@tonic-gate 	nsdp->mapfunc = sdp->mapfunc;
8037c478bd9Sstevel@tonic-gate 	nsdp->offset = sdp->offset + (offset_t)(nseg->s_base - seg->s_base);
8047c478bd9Sstevel@tonic-gate 	nsdp->vp 	= sdp->vp;
8057c478bd9Sstevel@tonic-gate 	nsdp->pageprot = sdp->pageprot;
8067c478bd9Sstevel@tonic-gate 	nsdp->prot	= sdp->prot;
8077c478bd9Sstevel@tonic-gate 	nsdp->maxprot = sdp->maxprot;
8087c478bd9Sstevel@tonic-gate 	nsdp->type = sdp->type;
8097c478bd9Sstevel@tonic-gate 	nsdp->hat_attr = sdp->hat_attr;
8107c478bd9Sstevel@tonic-gate 	nsdp->hat_flags = sdp->hat_flags;
8117c478bd9Sstevel@tonic-gate 	nsdp->softlockcnt = 0;
8127c478bd9Sstevel@tonic-gate 
8137c478bd9Sstevel@tonic-gate 	/*
8147c478bd9Sstevel@tonic-gate 	 * Initialize per page data if the segment we are
8157c478bd9Sstevel@tonic-gate 	 * dup'ing has per page information.
8167c478bd9Sstevel@tonic-gate 	 */
8177c478bd9Sstevel@tonic-gate 	if (sdp->vpage != NULL) {
8187c478bd9Sstevel@tonic-gate 		/* need to split vpage into two arrays */
8197c478bd9Sstevel@tonic-gate 		register size_t nnbytes;
8207c478bd9Sstevel@tonic-gate 		register size_t nnpages;
8217c478bd9Sstevel@tonic-gate 		register struct vpage *ovpage;
8227c478bd9Sstevel@tonic-gate 
8237c478bd9Sstevel@tonic-gate 		ovpage = sdp->vpage;		/* keep pointer to vpage */
8247c478bd9Sstevel@tonic-gate 
8257c478bd9Sstevel@tonic-gate 		npages = seg_pages(seg);	/* seg has shrunk */
8267c478bd9Sstevel@tonic-gate 		nbytes = vpgtob(npages);
8277c478bd9Sstevel@tonic-gate 		nnpages = seg_pages(nseg);
8287c478bd9Sstevel@tonic-gate 		nnbytes = vpgtob(nnpages);
8297c478bd9Sstevel@tonic-gate 
8307c478bd9Sstevel@tonic-gate 		sdp->vpage = kmem_alloc(nbytes, KM_SLEEP);
8317c478bd9Sstevel@tonic-gate 		bcopy(ovpage, sdp->vpage, nbytes);
8327c478bd9Sstevel@tonic-gate 
8337c478bd9Sstevel@tonic-gate 		nsdp->vpage = kmem_alloc(nnbytes, KM_SLEEP);
8347c478bd9Sstevel@tonic-gate 		bcopy(&ovpage[npages + dpages], nsdp->vpage, nnbytes);
8357c478bd9Sstevel@tonic-gate 
8367c478bd9Sstevel@tonic-gate 		/* free up old vpage */
8377c478bd9Sstevel@tonic-gate 		kmem_free(ovpage, vpgtob(opages));
8387c478bd9Sstevel@tonic-gate 	} else
8397c478bd9Sstevel@tonic-gate 		nsdp->vpage = NULL;
8407c478bd9Sstevel@tonic-gate 
8417c478bd9Sstevel@tonic-gate 	/*
8427c478bd9Sstevel@tonic-gate 	 * unmap dhps.
8437c478bd9Sstevel@tonic-gate 	 */
8447c478bd9Sstevel@tonic-gate 	if (dhp == NULL) {
8457c478bd9Sstevel@tonic-gate 		nsdp->devmap_data = NULL;
8467c478bd9Sstevel@tonic-gate 		return (0);
8477c478bd9Sstevel@tonic-gate 	}
8487c478bd9Sstevel@tonic-gate 	while (dhp != NULL) {
8497c478bd9Sstevel@tonic-gate 		callbackops = &dhp->dh_callbackops;
8507c478bd9Sstevel@tonic-gate 		TRACE_2(TR_FAC_DEVMAP, TR_DEVMAP_UNMAP_CK3,
8517c478bd9Sstevel@tonic-gate 		    "segdev_unmap: dhp=%p addr=%p", dhp, addr);
8527c478bd9Sstevel@tonic-gate 		DEBUGF(3, (CE_CONT, "unmap: dhp %p addr %p uvaddr %p len %lx\n",
8537c478bd9Sstevel@tonic-gate 		    (void *)dhp, (void *)addr,
8547c478bd9Sstevel@tonic-gate 		    (void *)dhp->dh_uvaddr, dhp->dh_len));
8557c478bd9Sstevel@tonic-gate 
8567c478bd9Sstevel@tonic-gate 		if (addr == (dhp->dh_uvaddr + dhp->dh_len)) {
8577c478bd9Sstevel@tonic-gate 			dhpp = dhp->dh_next;
8587c478bd9Sstevel@tonic-gate 			dhp->dh_next = NULL;
8597c478bd9Sstevel@tonic-gate 			dhp = dhpp;
8607c478bd9Sstevel@tonic-gate 		} else if (addr > (dhp->dh_uvaddr + dhp->dh_len)) {
8617c478bd9Sstevel@tonic-gate 			dhp = dhp->dh_next;
8627c478bd9Sstevel@tonic-gate 		} else if (addr > dhp->dh_uvaddr &&
8637c478bd9Sstevel@tonic-gate 			(addr + len) < (dhp->dh_uvaddr + dhp->dh_len)) {
8647c478bd9Sstevel@tonic-gate 			/*
8657c478bd9Sstevel@tonic-gate 			 * <addr, addr+len> is enclosed by dhp.
8667c478bd9Sstevel@tonic-gate 			 * create a newdhp that begins at addr+len and
8677c478bd9Sstevel@tonic-gate 			 * ends at dhp->dh_uvaddr+dhp->dh_len.
8687c478bd9Sstevel@tonic-gate 			 */
8697c478bd9Sstevel@tonic-gate 			newdhp = kmem_alloc(sizeof (devmap_handle_t), KM_SLEEP);
8707c478bd9Sstevel@tonic-gate 			HOLD_DHP_LOCK(dhp);
8717c478bd9Sstevel@tonic-gate 			bcopy(dhp, newdhp, sizeof (devmap_handle_t));
8727c478bd9Sstevel@tonic-gate 			RELE_DHP_LOCK(dhp);
8737c478bd9Sstevel@tonic-gate 			newdhp->dh_seg = nseg;
8747c478bd9Sstevel@tonic-gate 			newdhp->dh_next = dhp->dh_next;
8757c478bd9Sstevel@tonic-gate 			if (dhp->dh_softlock != NULL)
8767c478bd9Sstevel@tonic-gate 				newdhp->dh_softlock = devmap_softlock_init(
8777c478bd9Sstevel@tonic-gate 					newdhp->dh_dev,
8787c478bd9Sstevel@tonic-gate 					(ulong_t)callbackops->devmap_access);
8797c478bd9Sstevel@tonic-gate 			if (dhp->dh_ctx != NULL)
8807c478bd9Sstevel@tonic-gate 				newdhp->dh_ctx = devmap_ctxinit(newdhp->dh_dev,
8817c478bd9Sstevel@tonic-gate 					(ulong_t)callbackops->devmap_access);
8827c478bd9Sstevel@tonic-gate 			if (newdhp->dh_flags & DEVMAP_LOCK_INITED) {
8837c478bd9Sstevel@tonic-gate 				mutex_init(&newdhp->dh_lock,
8847c478bd9Sstevel@tonic-gate 				    NULL, MUTEX_DEFAULT, NULL);
8857c478bd9Sstevel@tonic-gate 			}
8867c478bd9Sstevel@tonic-gate 			if (callbackops->devmap_unmap != NULL)
8877c478bd9Sstevel@tonic-gate 				(*callbackops->devmap_unmap)(dhp, dhp->dh_pvtp,
8887c478bd9Sstevel@tonic-gate 					off, len, dhp, &dhp->dh_pvtp,
8897c478bd9Sstevel@tonic-gate 					newdhp, &newdhp->dh_pvtp);
8907c478bd9Sstevel@tonic-gate 			mlen = len + (addr - dhp->dh_uvaddr);
8917c478bd9Sstevel@tonic-gate 			devmap_handle_reduce_len(newdhp, mlen);
8927c478bd9Sstevel@tonic-gate 			nsdp->devmap_data = newdhp;
8937c478bd9Sstevel@tonic-gate 			/* XX Changing len should recalculate LARGE flag */
8947c478bd9Sstevel@tonic-gate 			dhp->dh_len = addr - dhp->dh_uvaddr;
8957c478bd9Sstevel@tonic-gate 			dhpp = dhp->dh_next;
8967c478bd9Sstevel@tonic-gate 			dhp->dh_next = NULL;
8977c478bd9Sstevel@tonic-gate 			dhp = dhpp;
8987c478bd9Sstevel@tonic-gate 		} else if ((addr > dhp->dh_uvaddr) &&
8997c478bd9Sstevel@tonic-gate 			    ((addr + len) >= (dhp->dh_uvaddr + dhp->dh_len))) {
9007c478bd9Sstevel@tonic-gate 			mlen = dhp->dh_len + dhp->dh_uvaddr - addr;
9017c478bd9Sstevel@tonic-gate 			/*
9027c478bd9Sstevel@tonic-gate 			 * <addr, addr+len> spans over dhps.
9037c478bd9Sstevel@tonic-gate 			 */
9047c478bd9Sstevel@tonic-gate 			if (callbackops->devmap_unmap != NULL)
9057c478bd9Sstevel@tonic-gate 				(*callbackops->devmap_unmap)(dhp, dhp->dh_pvtp,
9067c478bd9Sstevel@tonic-gate 					off, mlen, (devmap_cookie_t *)dhp,
9077c478bd9Sstevel@tonic-gate 					&dhp->dh_pvtp, NULL, NULL);
9087c478bd9Sstevel@tonic-gate 			/* XX Changing len should recalculate LARGE flag */
9097c478bd9Sstevel@tonic-gate 			dhp->dh_len = addr - dhp->dh_uvaddr;
9107c478bd9Sstevel@tonic-gate 			dhpp = dhp->dh_next;
9117c478bd9Sstevel@tonic-gate 			dhp->dh_next = NULL;
9127c478bd9Sstevel@tonic-gate 			dhp = dhpp;
9137c478bd9Sstevel@tonic-gate 			nsdp->devmap_data = dhp;
9147c478bd9Sstevel@tonic-gate 		} else if ((addr + len) >= (dhp->dh_uvaddr + dhp->dh_len)) {
9157c478bd9Sstevel@tonic-gate 			/*
9167c478bd9Sstevel@tonic-gate 			 * dhp is enclosed by <addr, addr+len>.
9177c478bd9Sstevel@tonic-gate 			 */
9187c478bd9Sstevel@tonic-gate 			dhp->dh_seg = nseg;
9197c478bd9Sstevel@tonic-gate 			nsdp->devmap_data = dhp;
9207c478bd9Sstevel@tonic-gate 			dhp = devmap_handle_unmap(dhp);
9217c478bd9Sstevel@tonic-gate 			nsdp->devmap_data = dhp; /* XX redundant? */
9227c478bd9Sstevel@tonic-gate 		} else if (((addr + len) > dhp->dh_uvaddr) &&
9237c478bd9Sstevel@tonic-gate 			    ((addr + len) < (dhp->dh_uvaddr + dhp->dh_len))) {
9247c478bd9Sstevel@tonic-gate 			mlen = addr + len - dhp->dh_uvaddr;
9257c478bd9Sstevel@tonic-gate 			if (callbackops->devmap_unmap != NULL)
9267c478bd9Sstevel@tonic-gate 				(*callbackops->devmap_unmap)(dhp, dhp->dh_pvtp,
9277c478bd9Sstevel@tonic-gate 					dhp->dh_uoff, mlen, NULL,
9287c478bd9Sstevel@tonic-gate 					NULL, dhp, &dhp->dh_pvtp);
9297c478bd9Sstevel@tonic-gate 			devmap_handle_reduce_len(dhp, mlen);
9307c478bd9Sstevel@tonic-gate 			nsdp->devmap_data = dhp;
9317c478bd9Sstevel@tonic-gate 			dhp->dh_seg = nseg;
9327c478bd9Sstevel@tonic-gate 			dhp = dhp->dh_next;
9337c478bd9Sstevel@tonic-gate 		} else {
9347c478bd9Sstevel@tonic-gate 			dhp->dh_seg = nseg;
9357c478bd9Sstevel@tonic-gate 			dhp = dhp->dh_next;
9367c478bd9Sstevel@tonic-gate 		}
9377c478bd9Sstevel@tonic-gate 	}
9387c478bd9Sstevel@tonic-gate 	return (0);
9397c478bd9Sstevel@tonic-gate }
9407c478bd9Sstevel@tonic-gate 
9417c478bd9Sstevel@tonic-gate /*
9427c478bd9Sstevel@tonic-gate  * Utility function handles reducing the length of a devmap handle during unmap
9437c478bd9Sstevel@tonic-gate  * Note that is only used for unmapping the front portion of the handler,
9447c478bd9Sstevel@tonic-gate  * i.e., we are bumping up the offset/pfn etc up by len
9457c478bd9Sstevel@tonic-gate  * Do not use if reducing length at the tail.
9467c478bd9Sstevel@tonic-gate  */
9477c478bd9Sstevel@tonic-gate static void
9487c478bd9Sstevel@tonic-gate devmap_handle_reduce_len(devmap_handle_t *dhp, size_t len)
9497c478bd9Sstevel@tonic-gate {
9507c478bd9Sstevel@tonic-gate 	struct ddi_umem_cookie *cp;
9517c478bd9Sstevel@tonic-gate 	struct devmap_pmem_cookie *pcp;
9527c478bd9Sstevel@tonic-gate 	/*
9537c478bd9Sstevel@tonic-gate 	 * adjust devmap handle fields
9547c478bd9Sstevel@tonic-gate 	 */
9557c478bd9Sstevel@tonic-gate 	ASSERT(len < dhp->dh_len);
9567c478bd9Sstevel@tonic-gate 
9577c478bd9Sstevel@tonic-gate 	/* Make sure only page-aligned changes are done */
9587c478bd9Sstevel@tonic-gate 	ASSERT((len & PAGEOFFSET) == 0);
9597c478bd9Sstevel@tonic-gate 
9607c478bd9Sstevel@tonic-gate 	dhp->dh_len -= len;
9617c478bd9Sstevel@tonic-gate 	dhp->dh_uoff += (offset_t)len;
9627c478bd9Sstevel@tonic-gate 	dhp->dh_roff += (offset_t)len;
9637c478bd9Sstevel@tonic-gate 	dhp->dh_uvaddr += len;
9647c478bd9Sstevel@tonic-gate 	/* Need to grab dhp lock if REMAP */
9657c478bd9Sstevel@tonic-gate 	HOLD_DHP_LOCK(dhp);
9667c478bd9Sstevel@tonic-gate 	cp = dhp->dh_cookie;
9677c478bd9Sstevel@tonic-gate 	if (!(dhp->dh_flags & DEVMAP_MAPPING_INVALID)) {
9687c478bd9Sstevel@tonic-gate 		if (cookie_is_devmem(cp)) {
9697c478bd9Sstevel@tonic-gate 			dhp->dh_pfn += btop(len);
9707c478bd9Sstevel@tonic-gate 		} else if (cookie_is_pmem(cp)) {
9717c478bd9Sstevel@tonic-gate 			pcp = (struct devmap_pmem_cookie *)dhp->dh_pcookie;
9727c478bd9Sstevel@tonic-gate 			ASSERT((dhp->dh_roff & PAGEOFFSET) == 0 &&
9737c478bd9Sstevel@tonic-gate 				dhp->dh_roff < ptob(pcp->dp_npages));
9747c478bd9Sstevel@tonic-gate 		} else {
9757c478bd9Sstevel@tonic-gate 			ASSERT(dhp->dh_roff < cp->size);
9767c478bd9Sstevel@tonic-gate 			ASSERT(dhp->dh_cvaddr >= cp->cvaddr &&
9777c478bd9Sstevel@tonic-gate 				dhp->dh_cvaddr < (cp->cvaddr + cp->size));
9787c478bd9Sstevel@tonic-gate 			ASSERT((dhp->dh_cvaddr + len) <=
9797c478bd9Sstevel@tonic-gate 				(cp->cvaddr + cp->size));
9807c478bd9Sstevel@tonic-gate 
9817c478bd9Sstevel@tonic-gate 			dhp->dh_cvaddr += len;
9827c478bd9Sstevel@tonic-gate 		}
9837c478bd9Sstevel@tonic-gate 	}
9847c478bd9Sstevel@tonic-gate 	/* XXX - Should recalculate the DEVMAP_FLAG_LARGE after changes */
9857c478bd9Sstevel@tonic-gate 	RELE_DHP_LOCK(dhp);
9867c478bd9Sstevel@tonic-gate }
9877c478bd9Sstevel@tonic-gate 
9887c478bd9Sstevel@tonic-gate /*
9897c478bd9Sstevel@tonic-gate  * Free devmap handle, dhp.
9907c478bd9Sstevel@tonic-gate  * Return the next devmap handle on the linked list.
9917c478bd9Sstevel@tonic-gate  */
9927c478bd9Sstevel@tonic-gate static devmap_handle_t *
9937c478bd9Sstevel@tonic-gate devmap_handle_unmap(devmap_handle_t *dhp)
9947c478bd9Sstevel@tonic-gate {
9957c478bd9Sstevel@tonic-gate 	struct devmap_callback_ctl *callbackops = &dhp->dh_callbackops;
9967c478bd9Sstevel@tonic-gate 	struct segdev_data *sdp = (struct segdev_data *)dhp->dh_seg->s_data;
9977c478bd9Sstevel@tonic-gate 	devmap_handle_t *dhpp = (devmap_handle_t *)sdp->devmap_data;
9987c478bd9Sstevel@tonic-gate 
9997c478bd9Sstevel@tonic-gate 	ASSERT(dhp != NULL);
10007c478bd9Sstevel@tonic-gate 
10017c478bd9Sstevel@tonic-gate 	/*
10027c478bd9Sstevel@tonic-gate 	 * before we free up dhp, call the driver's devmap_unmap entry point
10037c478bd9Sstevel@tonic-gate 	 * to free resources allocated for this dhp.
10047c478bd9Sstevel@tonic-gate 	 */
10057c478bd9Sstevel@tonic-gate 	if (callbackops->devmap_unmap != NULL) {
10067c478bd9Sstevel@tonic-gate 		(*callbackops->devmap_unmap)(dhp, dhp->dh_pvtp, dhp->dh_uoff,
10077c478bd9Sstevel@tonic-gate 			dhp->dh_len, NULL, NULL, NULL, NULL);
10087c478bd9Sstevel@tonic-gate 	}
10097c478bd9Sstevel@tonic-gate 
10107c478bd9Sstevel@tonic-gate 	if (dhpp == dhp) {	/* releasing first dhp, change sdp data */
10117c478bd9Sstevel@tonic-gate 		sdp->devmap_data = dhp->dh_next;
10127c478bd9Sstevel@tonic-gate 	} else {
10137c478bd9Sstevel@tonic-gate 		while (dhpp->dh_next != dhp) {
10147c478bd9Sstevel@tonic-gate 			dhpp = dhpp->dh_next;
10157c478bd9Sstevel@tonic-gate 		}
10167c478bd9Sstevel@tonic-gate 		dhpp->dh_next = dhp->dh_next;
10177c478bd9Sstevel@tonic-gate 	}
10187c478bd9Sstevel@tonic-gate 	dhpp = dhp->dh_next;	/* return value is next dhp in chain */
10197c478bd9Sstevel@tonic-gate 
10207c478bd9Sstevel@tonic-gate 	if (dhp->dh_softlock != NULL)
10217c478bd9Sstevel@tonic-gate 		devmap_softlock_rele(dhp);
10227c478bd9Sstevel@tonic-gate 
10237c478bd9Sstevel@tonic-gate 	if (dhp->dh_ctx != NULL)
10247c478bd9Sstevel@tonic-gate 		devmap_ctx_rele(dhp);
10257c478bd9Sstevel@tonic-gate 
10267c478bd9Sstevel@tonic-gate 	if (dhp->dh_flags & DEVMAP_LOCK_INITED) {
10277c478bd9Sstevel@tonic-gate 		mutex_destroy(&dhp->dh_lock);
10287c478bd9Sstevel@tonic-gate 	}
10297c478bd9Sstevel@tonic-gate 	kmem_free(dhp, sizeof (devmap_handle_t));
10307c478bd9Sstevel@tonic-gate 
10317c478bd9Sstevel@tonic-gate 	return (dhpp);
10327c478bd9Sstevel@tonic-gate }
10337c478bd9Sstevel@tonic-gate 
10347c478bd9Sstevel@tonic-gate /*
10357c478bd9Sstevel@tonic-gate  * Free complete devmap handles from dhp for len bytes
10367c478bd9Sstevel@tonic-gate  * dhp can be either the first handle or a subsequent handle
10377c478bd9Sstevel@tonic-gate  */
10387c478bd9Sstevel@tonic-gate static void
10397c478bd9Sstevel@tonic-gate devmap_handle_unmap_head(devmap_handle_t *dhp, size_t len)
10407c478bd9Sstevel@tonic-gate {
10417c478bd9Sstevel@tonic-gate 	struct devmap_callback_ctl *callbackops;
10427c478bd9Sstevel@tonic-gate 
10437c478bd9Sstevel@tonic-gate 	/*
10447c478bd9Sstevel@tonic-gate 	 * free the devmap handles covered by len.
10457c478bd9Sstevel@tonic-gate 	 */
10467c478bd9Sstevel@tonic-gate 	while (len >= dhp->dh_len) {
10477c478bd9Sstevel@tonic-gate 		len -= dhp->dh_len;
10487c478bd9Sstevel@tonic-gate 		dhp = devmap_handle_unmap(dhp);
10497c478bd9Sstevel@tonic-gate 	}
10507c478bd9Sstevel@tonic-gate 	if (len != 0) {	/* partial unmap at head of first remaining dhp */
10517c478bd9Sstevel@tonic-gate 		callbackops = &dhp->dh_callbackops;
10527c478bd9Sstevel@tonic-gate 
10537c478bd9Sstevel@tonic-gate 		/*
10547c478bd9Sstevel@tonic-gate 		 * Call the unmap callback so the drivers can make
10557c478bd9Sstevel@tonic-gate 		 * adjustment on its private data.
10567c478bd9Sstevel@tonic-gate 		 */
10577c478bd9Sstevel@tonic-gate 		if (callbackops->devmap_unmap != NULL)
10587c478bd9Sstevel@tonic-gate 			(*callbackops->devmap_unmap)(dhp, dhp->dh_pvtp,
10597c478bd9Sstevel@tonic-gate 			    dhp->dh_uoff, len, NULL, NULL, dhp, &dhp->dh_pvtp);
10607c478bd9Sstevel@tonic-gate 		devmap_handle_reduce_len(dhp, len);
10617c478bd9Sstevel@tonic-gate 	}
10627c478bd9Sstevel@tonic-gate }
10637c478bd9Sstevel@tonic-gate 
10647c478bd9Sstevel@tonic-gate /*
10657c478bd9Sstevel@tonic-gate  * Free devmap handles to truncate  the mapping after addr
10667c478bd9Sstevel@tonic-gate  * RFE: Simpler to pass in dhp pointing at correct dhp (avoid find again)
10677c478bd9Sstevel@tonic-gate  *	Also could then use the routine in middle unmap case too
10687c478bd9Sstevel@tonic-gate  */
10697c478bd9Sstevel@tonic-gate static void
10707c478bd9Sstevel@tonic-gate devmap_handle_unmap_tail(devmap_handle_t *dhp, caddr_t addr)
10717c478bd9Sstevel@tonic-gate {
10727c478bd9Sstevel@tonic-gate 	register struct seg *seg = dhp->dh_seg;
10737c478bd9Sstevel@tonic-gate 	register struct segdev_data *sdp = (struct segdev_data *)seg->s_data;
10747c478bd9Sstevel@tonic-gate 	register devmap_handle_t *dhph = (devmap_handle_t *)sdp->devmap_data;
10757c478bd9Sstevel@tonic-gate 	struct devmap_callback_ctl *callbackops;
10767c478bd9Sstevel@tonic-gate 	register devmap_handle_t *dhpp;
10777c478bd9Sstevel@tonic-gate 	size_t maplen;
10787c478bd9Sstevel@tonic-gate 	ulong_t off;
10797c478bd9Sstevel@tonic-gate 	size_t len;
10807c478bd9Sstevel@tonic-gate 
10817c478bd9Sstevel@tonic-gate 	maplen = (size_t)(addr - dhp->dh_uvaddr);
10827c478bd9Sstevel@tonic-gate 	dhph = devmap_find_handle(dhph, addr);
10837c478bd9Sstevel@tonic-gate 
10847c478bd9Sstevel@tonic-gate 	while (dhph != NULL) {
10857c478bd9Sstevel@tonic-gate 		if (maplen == 0) {
10867c478bd9Sstevel@tonic-gate 			dhph =  devmap_handle_unmap(dhph);
10877c478bd9Sstevel@tonic-gate 		} else {
10887c478bd9Sstevel@tonic-gate 			callbackops = &dhph->dh_callbackops;
10897c478bd9Sstevel@tonic-gate 			len = dhph->dh_len - maplen;
10907c478bd9Sstevel@tonic-gate 			off = (ulong_t)sdp->offset + (addr - seg->s_base);
10917c478bd9Sstevel@tonic-gate 			/*
10927c478bd9Sstevel@tonic-gate 			 * Call the unmap callback so the driver
10937c478bd9Sstevel@tonic-gate 			 * can make adjustments on its private data.
10947c478bd9Sstevel@tonic-gate 			 */
10957c478bd9Sstevel@tonic-gate 			if (callbackops->devmap_unmap != NULL)
10967c478bd9Sstevel@tonic-gate 				(*callbackops->devmap_unmap)(dhph,
10977c478bd9Sstevel@tonic-gate 					dhph->dh_pvtp, off, len,
10987c478bd9Sstevel@tonic-gate 					(devmap_cookie_t *)dhph,
10997c478bd9Sstevel@tonic-gate 					&dhph->dh_pvtp, NULL, NULL);
11007c478bd9Sstevel@tonic-gate 			/* XXX Reducing len needs to recalculate LARGE flag */
11017c478bd9Sstevel@tonic-gate 			dhph->dh_len = maplen;
11027c478bd9Sstevel@tonic-gate 			maplen = 0;
11037c478bd9Sstevel@tonic-gate 			dhpp = dhph->dh_next;
11047c478bd9Sstevel@tonic-gate 			dhph->dh_next = NULL;
11057c478bd9Sstevel@tonic-gate 			dhph = dhpp;
11067c478bd9Sstevel@tonic-gate 		}
11077c478bd9Sstevel@tonic-gate 	} /* end while */
11087c478bd9Sstevel@tonic-gate }
11097c478bd9Sstevel@tonic-gate 
11107c478bd9Sstevel@tonic-gate /*
11117c478bd9Sstevel@tonic-gate  * Free a segment.
11127c478bd9Sstevel@tonic-gate  */
11137c478bd9Sstevel@tonic-gate static void
11147c478bd9Sstevel@tonic-gate segdev_free(struct seg *seg)
11157c478bd9Sstevel@tonic-gate {
11167c478bd9Sstevel@tonic-gate 	register struct segdev_data *sdp = (struct segdev_data *)seg->s_data;
11177c478bd9Sstevel@tonic-gate 	devmap_handle_t *dhp = (devmap_handle_t *)sdp->devmap_data;
11187c478bd9Sstevel@tonic-gate 
11197c478bd9Sstevel@tonic-gate 	TRACE_2(TR_FAC_DEVMAP, TR_DEVMAP_FREE,
11207c478bd9Sstevel@tonic-gate 	    "segdev_free: dhp=%p seg=%p", (void *)dhp, (void *)seg);
11217c478bd9Sstevel@tonic-gate 	DEBUGF(3, (CE_CONT, "segdev_free: dhp %p seg %p\n",
11227c478bd9Sstevel@tonic-gate 	    (void *)dhp, (void *)seg));
11237c478bd9Sstevel@tonic-gate 
11247c478bd9Sstevel@tonic-gate 	/*
11257c478bd9Sstevel@tonic-gate 	 * Since the address space is "write" locked, we
11267c478bd9Sstevel@tonic-gate 	 * don't need the segment lock to protect "segdev" data.
11277c478bd9Sstevel@tonic-gate 	 */
11287c478bd9Sstevel@tonic-gate 	ASSERT(seg->s_as && AS_WRITE_HELD(seg->s_as, &seg->s_as->a_lock));
11297c478bd9Sstevel@tonic-gate 
11307c478bd9Sstevel@tonic-gate 	while (dhp != NULL)
11317c478bd9Sstevel@tonic-gate 		dhp = devmap_handle_unmap(dhp);
11327c478bd9Sstevel@tonic-gate 
11337c478bd9Sstevel@tonic-gate 	VN_RELE(sdp->vp);
11347c478bd9Sstevel@tonic-gate 	if (sdp->vpage != NULL)
11357c478bd9Sstevel@tonic-gate 		kmem_free(sdp->vpage, vpgtob(seg_pages(seg)));
11367c478bd9Sstevel@tonic-gate 
11377c478bd9Sstevel@tonic-gate 	mutex_destroy(&sdp->lock);
11387c478bd9Sstevel@tonic-gate 	kmem_free(sdp, sizeof (*sdp));
11397c478bd9Sstevel@tonic-gate }
11407c478bd9Sstevel@tonic-gate 
11417c478bd9Sstevel@tonic-gate static void
11427c478bd9Sstevel@tonic-gate free_devmap_handle(devmap_handle_t *dhp)
11437c478bd9Sstevel@tonic-gate {
11447c478bd9Sstevel@tonic-gate 	register devmap_handle_t *dhpp;
11457c478bd9Sstevel@tonic-gate 
11467c478bd9Sstevel@tonic-gate 	/*
11477c478bd9Sstevel@tonic-gate 	 * free up devmap handle
11487c478bd9Sstevel@tonic-gate 	 */
11497c478bd9Sstevel@tonic-gate 	while (dhp != NULL) {
11507c478bd9Sstevel@tonic-gate 		dhpp = dhp->dh_next;
11517c478bd9Sstevel@tonic-gate 		if (dhp->dh_flags & DEVMAP_LOCK_INITED) {
11527c478bd9Sstevel@tonic-gate 			mutex_destroy(&dhp->dh_lock);
11537c478bd9Sstevel@tonic-gate 		}
11547c478bd9Sstevel@tonic-gate 
11557c478bd9Sstevel@tonic-gate 		if (dhp->dh_softlock != NULL)
11567c478bd9Sstevel@tonic-gate 			devmap_softlock_rele(dhp);
11577c478bd9Sstevel@tonic-gate 
11587c478bd9Sstevel@tonic-gate 		if (dhp->dh_ctx != NULL)
11597c478bd9Sstevel@tonic-gate 			devmap_ctx_rele(dhp);
11607c478bd9Sstevel@tonic-gate 
11617c478bd9Sstevel@tonic-gate 		kmem_free(dhp, sizeof (devmap_handle_t));
11627c478bd9Sstevel@tonic-gate 		dhp = dhpp;
11637c478bd9Sstevel@tonic-gate 	}
11647c478bd9Sstevel@tonic-gate }
11657c478bd9Sstevel@tonic-gate 
11667c478bd9Sstevel@tonic-gate /*
11677c478bd9Sstevel@tonic-gate  * routines to lock and unlock underlying segkp segment for
11687c478bd9Sstevel@tonic-gate  * KMEM_PAGEABLE type cookies.
11697c478bd9Sstevel@tonic-gate  * segkp only allows a single pending F_SOFTLOCK
11707c478bd9Sstevel@tonic-gate  * we keep track of number of locks in the cookie so we can
11717c478bd9Sstevel@tonic-gate  * have multiple pending faults and manage the calls to segkp.
11727c478bd9Sstevel@tonic-gate  * RFE: if segkp supports either pagelock or can support multiple
11737c478bd9Sstevel@tonic-gate  * calls to F_SOFTLOCK, then these routines can go away.
11747c478bd9Sstevel@tonic-gate  *	If pagelock, segdev_faultpage can fault on a page by page basis
11757c478bd9Sstevel@tonic-gate  *		and simplifies the code quite a bit.
11767c478bd9Sstevel@tonic-gate  *	if multiple calls allowed but not partial ranges, then need for
11777c478bd9Sstevel@tonic-gate  *	cookie->lock and locked count goes away, code can call as_fault directly
11787c478bd9Sstevel@tonic-gate  */
11797c478bd9Sstevel@tonic-gate static faultcode_t
11807c478bd9Sstevel@tonic-gate acquire_kpmem_lock(struct ddi_umem_cookie *cookie, size_t npages)
11817c478bd9Sstevel@tonic-gate {
11827c478bd9Sstevel@tonic-gate 	int err = 0;
11837c478bd9Sstevel@tonic-gate 	ASSERT(cookie_is_kpmem(cookie));
11847c478bd9Sstevel@tonic-gate 	/*
11857c478bd9Sstevel@tonic-gate 	 * Fault in pages in segkp with F_SOFTLOCK.
11867c478bd9Sstevel@tonic-gate 	 * We want to hold the lock until all pages have been loaded.
11877c478bd9Sstevel@tonic-gate 	 * segkp only allows single caller to hold SOFTLOCK, so cookie
11887c478bd9Sstevel@tonic-gate 	 * holds a count so we dont call into segkp multiple times
11897c478bd9Sstevel@tonic-gate 	 */
11907c478bd9Sstevel@tonic-gate 	mutex_enter(&cookie->lock);
11917c478bd9Sstevel@tonic-gate 
11927c478bd9Sstevel@tonic-gate 	/*
11937c478bd9Sstevel@tonic-gate 	 * Check for overflow in locked field
11947c478bd9Sstevel@tonic-gate 	 */
11957c478bd9Sstevel@tonic-gate 	if ((UINT32_MAX - cookie->locked) < npages) {
11967c478bd9Sstevel@tonic-gate 		err = FC_MAKE_ERR(ENOMEM);
11977c478bd9Sstevel@tonic-gate 	} else if (cookie->locked == 0) {
11987c478bd9Sstevel@tonic-gate 		/* First time locking */
11997c478bd9Sstevel@tonic-gate 		err = as_fault(kas.a_hat, &kas, cookie->cvaddr,
12007c478bd9Sstevel@tonic-gate 		    cookie->size, F_SOFTLOCK, PROT_READ|PROT_WRITE);
12017c478bd9Sstevel@tonic-gate 	}
12027c478bd9Sstevel@tonic-gate 	if (!err) {
12037c478bd9Sstevel@tonic-gate 		cookie->locked += npages;
12047c478bd9Sstevel@tonic-gate 	}
12057c478bd9Sstevel@tonic-gate 	mutex_exit(&cookie->lock);
12067c478bd9Sstevel@tonic-gate 	return (err);
12077c478bd9Sstevel@tonic-gate }
12087c478bd9Sstevel@tonic-gate 
12097c478bd9Sstevel@tonic-gate static void
12107c478bd9Sstevel@tonic-gate release_kpmem_lock(struct ddi_umem_cookie *cookie, size_t npages)
12117c478bd9Sstevel@tonic-gate {
12127c478bd9Sstevel@tonic-gate 	mutex_enter(&cookie->lock);
12137c478bd9Sstevel@tonic-gate 	ASSERT(cookie_is_kpmem(cookie));
12147c478bd9Sstevel@tonic-gate 	ASSERT(cookie->locked >= npages);
12157c478bd9Sstevel@tonic-gate 	cookie->locked -= (uint_t)npages;
12167c478bd9Sstevel@tonic-gate 	if (cookie->locked == 0) {
12177c478bd9Sstevel@tonic-gate 		/* Last unlock */
12187c478bd9Sstevel@tonic-gate 		if (as_fault(kas.a_hat, &kas, cookie->cvaddr,
12197c478bd9Sstevel@tonic-gate 		    cookie->size, F_SOFTUNLOCK, PROT_READ|PROT_WRITE))
12207c478bd9Sstevel@tonic-gate 			panic("segdev releasing kpmem lock %p", (void *)cookie);
12217c478bd9Sstevel@tonic-gate 	}
12227c478bd9Sstevel@tonic-gate 	mutex_exit(&cookie->lock);
12237c478bd9Sstevel@tonic-gate }
12247c478bd9Sstevel@tonic-gate 
12257c478bd9Sstevel@tonic-gate /*
12267c478bd9Sstevel@tonic-gate  * Routines to synchronize F_SOFTLOCK and F_INVAL faults for
12277c478bd9Sstevel@tonic-gate  * drivers with devmap_access callbacks
12287c478bd9Sstevel@tonic-gate  * slock->softlocked basically works like a rw lock
12297c478bd9Sstevel@tonic-gate  *	-ve counts => F_SOFTLOCK in progress
12307c478bd9Sstevel@tonic-gate  *	+ve counts => F_INVAL/F_PROT in progress
12317c478bd9Sstevel@tonic-gate  * We allow only one F_SOFTLOCK at a time
12327c478bd9Sstevel@tonic-gate  * but can have multiple pending F_INVAL/F_PROT calls
12337c478bd9Sstevel@tonic-gate  *
12347c478bd9Sstevel@tonic-gate  * This routine waits using cv_wait_sig so killing processes is more graceful
12357c478bd9Sstevel@tonic-gate  * Returns EINTR if coming out of this routine due to a signal, 0 otherwise
12367c478bd9Sstevel@tonic-gate  */
12377c478bd9Sstevel@tonic-gate static int devmap_softlock_enter(
12387c478bd9Sstevel@tonic-gate 	struct devmap_softlock *slock,
12397c478bd9Sstevel@tonic-gate 	size_t npages,
12407c478bd9Sstevel@tonic-gate 	enum fault_type type)
12417c478bd9Sstevel@tonic-gate {
12427c478bd9Sstevel@tonic-gate 	if (npages == 0)
12437c478bd9Sstevel@tonic-gate 		return (0);
12447c478bd9Sstevel@tonic-gate 	mutex_enter(&(slock->lock));
12457c478bd9Sstevel@tonic-gate 	switch (type) {
12467c478bd9Sstevel@tonic-gate 	case F_SOFTLOCK :
12477c478bd9Sstevel@tonic-gate 		while (slock->softlocked) {
12487c478bd9Sstevel@tonic-gate 			if (cv_wait_sig(&(slock)->cv, &(slock)->lock) == 0) {
12497c478bd9Sstevel@tonic-gate 				/* signalled */
12507c478bd9Sstevel@tonic-gate 				mutex_exit(&(slock->lock));
12517c478bd9Sstevel@tonic-gate 				return (EINTR);
12527c478bd9Sstevel@tonic-gate 			}
12537c478bd9Sstevel@tonic-gate 		}
12547c478bd9Sstevel@tonic-gate 		slock->softlocked -= npages; /* -ve count => locked */
12557c478bd9Sstevel@tonic-gate 		break;
12567c478bd9Sstevel@tonic-gate 	case F_INVAL :
12577c478bd9Sstevel@tonic-gate 	case F_PROT :
12587c478bd9Sstevel@tonic-gate 		while (slock->softlocked < 0)
12597c478bd9Sstevel@tonic-gate 			if (cv_wait_sig(&(slock)->cv, &(slock)->lock) == 0) {
12607c478bd9Sstevel@tonic-gate 				/* signalled */
12617c478bd9Sstevel@tonic-gate 				mutex_exit(&(slock->lock));
12627c478bd9Sstevel@tonic-gate 				return (EINTR);
12637c478bd9Sstevel@tonic-gate 			}
12647c478bd9Sstevel@tonic-gate 		slock->softlocked += npages; /* +ve count => f_invals */
12657c478bd9Sstevel@tonic-gate 		break;
12667c478bd9Sstevel@tonic-gate 	default:
12677c478bd9Sstevel@tonic-gate 		ASSERT(0);
12687c478bd9Sstevel@tonic-gate 	}
12697c478bd9Sstevel@tonic-gate 	mutex_exit(&(slock->lock));
12707c478bd9Sstevel@tonic-gate 	return (0);
12717c478bd9Sstevel@tonic-gate }
12727c478bd9Sstevel@tonic-gate 
12737c478bd9Sstevel@tonic-gate static void devmap_softlock_exit(
12747c478bd9Sstevel@tonic-gate 	struct devmap_softlock *slock,
12757c478bd9Sstevel@tonic-gate 	size_t npages,
12767c478bd9Sstevel@tonic-gate 	enum fault_type type)
12777c478bd9Sstevel@tonic-gate {
12787c478bd9Sstevel@tonic-gate 	if (slock == NULL)
12797c478bd9Sstevel@tonic-gate 		return;
12807c478bd9Sstevel@tonic-gate 	mutex_enter(&(slock->lock));
12817c478bd9Sstevel@tonic-gate 	switch (type) {
12827c478bd9Sstevel@tonic-gate 	case F_SOFTLOCK :
12837c478bd9Sstevel@tonic-gate 		ASSERT(-slock->softlocked >= npages);
12847c478bd9Sstevel@tonic-gate 		slock->softlocked += npages;	/* -ve count is softlocked */
12857c478bd9Sstevel@tonic-gate 		if (slock->softlocked == 0)
12867c478bd9Sstevel@tonic-gate 			cv_signal(&slock->cv);
12877c478bd9Sstevel@tonic-gate 		break;
12887c478bd9Sstevel@tonic-gate 	case F_INVAL :
12897c478bd9Sstevel@tonic-gate 	case F_PROT:
12907c478bd9Sstevel@tonic-gate 		ASSERT(slock->softlocked >= npages);
12917c478bd9Sstevel@tonic-gate 		slock->softlocked -= npages;
12927c478bd9Sstevel@tonic-gate 		if (slock->softlocked == 0)
12937c478bd9Sstevel@tonic-gate 			cv_signal(&slock->cv);
12947c478bd9Sstevel@tonic-gate 		break;
12957c478bd9Sstevel@tonic-gate 	default:
12967c478bd9Sstevel@tonic-gate 		ASSERT(0);
12977c478bd9Sstevel@tonic-gate 	}
12987c478bd9Sstevel@tonic-gate 	mutex_exit(&(slock->lock));
12997c478bd9Sstevel@tonic-gate }
13007c478bd9Sstevel@tonic-gate 
13017c478bd9Sstevel@tonic-gate /*
13027c478bd9Sstevel@tonic-gate  * Do a F_SOFTUNLOCK call over the range requested.
13037c478bd9Sstevel@tonic-gate  * The range must have already been F_SOFTLOCK'ed.
13047c478bd9Sstevel@tonic-gate  * The segment lock should be held, (but not the segment private lock?)
13057c478bd9Sstevel@tonic-gate  *  The softunlock code below does not adjust for large page sizes
13067c478bd9Sstevel@tonic-gate  *	assumes the caller already did any addr/len adjustments for
13077c478bd9Sstevel@tonic-gate  *	pagesize mappings before calling.
13087c478bd9Sstevel@tonic-gate  */
13097c478bd9Sstevel@tonic-gate /*ARGSUSED*/
13107c478bd9Sstevel@tonic-gate static void
13117c478bd9Sstevel@tonic-gate segdev_softunlock(
13127c478bd9Sstevel@tonic-gate 	struct hat *hat,		/* the hat */
13137c478bd9Sstevel@tonic-gate 	struct seg *seg,		/* seg_dev of interest */
13147c478bd9Sstevel@tonic-gate 	caddr_t addr,			/* base address of range */
13157c478bd9Sstevel@tonic-gate 	size_t len,			/* number of bytes */
13167c478bd9Sstevel@tonic-gate 	enum seg_rw rw)			/* type of access at fault */
13177c478bd9Sstevel@tonic-gate {
13187c478bd9Sstevel@tonic-gate 	struct segdev_data *sdp = (struct segdev_data *)seg->s_data;
13197c478bd9Sstevel@tonic-gate 	devmap_handle_t *dhp_head = (devmap_handle_t *)sdp->devmap_data;
13207c478bd9Sstevel@tonic-gate 
13217c478bd9Sstevel@tonic-gate 	TRACE_4(TR_FAC_DEVMAP, TR_DEVMAP_SOFTUNLOCK,
13227c478bd9Sstevel@tonic-gate 	    "segdev_softunlock:dhp_head=%p sdp=%p addr=%p len=%lx",
13237c478bd9Sstevel@tonic-gate 	    dhp_head, sdp, addr, len);
13247c478bd9Sstevel@tonic-gate 	DEBUGF(3, (CE_CONT, "segdev_softunlock: dhp %p lockcnt %lx "
13257c478bd9Sstevel@tonic-gate 	    "addr %p len %lx\n",
13267c478bd9Sstevel@tonic-gate 	    (void *)dhp_head, sdp->softlockcnt, (void *)addr, len));
13277c478bd9Sstevel@tonic-gate 
13287c478bd9Sstevel@tonic-gate 	hat_unlock(hat, addr, len);
13297c478bd9Sstevel@tonic-gate 
13307c478bd9Sstevel@tonic-gate 	if (dhp_head != NULL) {
13317c478bd9Sstevel@tonic-gate 		devmap_handle_t *dhp;
13327c478bd9Sstevel@tonic-gate 		size_t mlen;
13337c478bd9Sstevel@tonic-gate 		ulong_t off;
13347c478bd9Sstevel@tonic-gate 
13357c478bd9Sstevel@tonic-gate 		dhp = devmap_find_handle(dhp_head, addr);
13367c478bd9Sstevel@tonic-gate 		ASSERT(dhp != NULL);
13377c478bd9Sstevel@tonic-gate 
13387c478bd9Sstevel@tonic-gate 		off = (ulong_t)(addr - dhp->dh_uvaddr);
13397c478bd9Sstevel@tonic-gate 		while (len != 0) {
13407c478bd9Sstevel@tonic-gate 			mlen = MIN(len, (dhp->dh_len - off));
13417c478bd9Sstevel@tonic-gate 
13427c478bd9Sstevel@tonic-gate 			/*
13437c478bd9Sstevel@tonic-gate 			 * unlock segkp memory, locked during F_SOFTLOCK
13447c478bd9Sstevel@tonic-gate 			 */
13457c478bd9Sstevel@tonic-gate 			if (dhp_is_kpmem(dhp)) {
13467c478bd9Sstevel@tonic-gate 				release_kpmem_lock(
13477c478bd9Sstevel@tonic-gate 				    (struct ddi_umem_cookie *)dhp->dh_cookie,
13487c478bd9Sstevel@tonic-gate 				    btopr(mlen));
13497c478bd9Sstevel@tonic-gate 			}
13507c478bd9Sstevel@tonic-gate 
13517c478bd9Sstevel@tonic-gate 			/*
13527c478bd9Sstevel@tonic-gate 			 * Do the softlock accounting for devmap_access
13537c478bd9Sstevel@tonic-gate 			 */
13547c478bd9Sstevel@tonic-gate 			if (dhp->dh_callbackops.devmap_access != NULL) {
13557c478bd9Sstevel@tonic-gate 				devmap_softlock_exit(dhp->dh_softlock,
13567c478bd9Sstevel@tonic-gate 					btopr(mlen), F_SOFTLOCK);
13577c478bd9Sstevel@tonic-gate 			}
13587c478bd9Sstevel@tonic-gate 
13597c478bd9Sstevel@tonic-gate 			len -= mlen;
13607c478bd9Sstevel@tonic-gate 			dhp = dhp->dh_next;
13617c478bd9Sstevel@tonic-gate 			off = 0;
13627c478bd9Sstevel@tonic-gate 		}
13637c478bd9Sstevel@tonic-gate 	}
13647c478bd9Sstevel@tonic-gate 
13657c478bd9Sstevel@tonic-gate 	mutex_enter(&freemem_lock);
13667c478bd9Sstevel@tonic-gate 	ASSERT(sdp->softlockcnt >= btopr(len));
13677c478bd9Sstevel@tonic-gate 	sdp->softlockcnt -= btopr(len);
13687c478bd9Sstevel@tonic-gate 	mutex_exit(&freemem_lock);
13697c478bd9Sstevel@tonic-gate 	if (sdp->softlockcnt == 0) {
13707c478bd9Sstevel@tonic-gate 		/*
13717c478bd9Sstevel@tonic-gate 		 * All SOFTLOCKS are gone. Wakeup any waiting
13727c478bd9Sstevel@tonic-gate 		 * unmappers so they can try again to unmap.
13737c478bd9Sstevel@tonic-gate 		 * Check for waiters first without the mutex
13747c478bd9Sstevel@tonic-gate 		 * held so we don't always grab the mutex on
13757c478bd9Sstevel@tonic-gate 		 * softunlocks.
13767c478bd9Sstevel@tonic-gate 		 */
13777c478bd9Sstevel@tonic-gate 		if (AS_ISUNMAPWAIT(seg->s_as)) {
13787c478bd9Sstevel@tonic-gate 			mutex_enter(&seg->s_as->a_contents);
13797c478bd9Sstevel@tonic-gate 			if (AS_ISUNMAPWAIT(seg->s_as)) {
13807c478bd9Sstevel@tonic-gate 				AS_CLRUNMAPWAIT(seg->s_as);
13817c478bd9Sstevel@tonic-gate 				cv_broadcast(&seg->s_as->a_cv);
13827c478bd9Sstevel@tonic-gate 			}
13837c478bd9Sstevel@tonic-gate 			mutex_exit(&seg->s_as->a_contents);
13847c478bd9Sstevel@tonic-gate 		}
13857c478bd9Sstevel@tonic-gate 	}
13867c478bd9Sstevel@tonic-gate 
13877c478bd9Sstevel@tonic-gate }
13887c478bd9Sstevel@tonic-gate 
13897c478bd9Sstevel@tonic-gate /*
13907c478bd9Sstevel@tonic-gate  * Handle fault for a single page.
13917c478bd9Sstevel@tonic-gate  * Done in a separate routine so we can handle errors more easily.
13927c478bd9Sstevel@tonic-gate  * This routine is called only from segdev_faultpages()
13937c478bd9Sstevel@tonic-gate  * when looping over the range of addresses requested. The segment lock is held.
13947c478bd9Sstevel@tonic-gate  */
13957c478bd9Sstevel@tonic-gate static faultcode_t
13967c478bd9Sstevel@tonic-gate segdev_faultpage(
13977c478bd9Sstevel@tonic-gate 	struct hat *hat,		/* the hat */
13987c478bd9Sstevel@tonic-gate 	struct seg *seg,		/* seg_dev of interest */
13997c478bd9Sstevel@tonic-gate 	caddr_t addr,			/* address in as */
14007c478bd9Sstevel@tonic-gate 	struct vpage *vpage,		/* pointer to vpage for seg, addr */
14017c478bd9Sstevel@tonic-gate 	enum fault_type type,		/* type of fault */
14027c478bd9Sstevel@tonic-gate 	enum seg_rw rw,			/* type of access at fault */
14037c478bd9Sstevel@tonic-gate 	devmap_handle_t *dhp)		/* devmap handle if any for this page */
14047c478bd9Sstevel@tonic-gate {
14057c478bd9Sstevel@tonic-gate 	struct segdev_data *sdp = (struct segdev_data *)seg->s_data;
14067c478bd9Sstevel@tonic-gate 	uint_t prot;
14077c478bd9Sstevel@tonic-gate 	pfn_t pfnum = PFN_INVALID;
14087c478bd9Sstevel@tonic-gate 	u_offset_t offset;
14097c478bd9Sstevel@tonic-gate 	uint_t hat_flags;
14107c478bd9Sstevel@tonic-gate 	dev_info_t *dip;
14117c478bd9Sstevel@tonic-gate 
14127c478bd9Sstevel@tonic-gate 	TRACE_3(TR_FAC_DEVMAP, TR_DEVMAP_FAULTPAGE,
14137c478bd9Sstevel@tonic-gate 	    "segdev_faultpage: dhp=%p seg=%p addr=%p", dhp, seg, addr);
14147c478bd9Sstevel@tonic-gate 	DEBUGF(8, (CE_CONT, "segdev_faultpage: dhp %p seg %p addr %p \n",
14157c478bd9Sstevel@tonic-gate 	    (void *)dhp, (void *)seg, (void *)addr));
14167c478bd9Sstevel@tonic-gate 
14177c478bd9Sstevel@tonic-gate 	/*
14187c478bd9Sstevel@tonic-gate 	 * Initialize protection value for this page.
14197c478bd9Sstevel@tonic-gate 	 * If we have per page protection values check it now.
14207c478bd9Sstevel@tonic-gate 	 */
14217c478bd9Sstevel@tonic-gate 	if (sdp->pageprot) {
14227c478bd9Sstevel@tonic-gate 		uint_t protchk;
14237c478bd9Sstevel@tonic-gate 
14247c478bd9Sstevel@tonic-gate 		switch (rw) {
14257c478bd9Sstevel@tonic-gate 		case S_READ:
14267c478bd9Sstevel@tonic-gate 			protchk = PROT_READ;
14277c478bd9Sstevel@tonic-gate 			break;
14287c478bd9Sstevel@tonic-gate 		case S_WRITE:
14297c478bd9Sstevel@tonic-gate 			protchk = PROT_WRITE;
14307c478bd9Sstevel@tonic-gate 			break;
14317c478bd9Sstevel@tonic-gate 		case S_EXEC:
14327c478bd9Sstevel@tonic-gate 			protchk = PROT_EXEC;
14337c478bd9Sstevel@tonic-gate 			break;
14347c478bd9Sstevel@tonic-gate 		case S_OTHER:
14357c478bd9Sstevel@tonic-gate 		default:
14367c478bd9Sstevel@tonic-gate 			protchk = PROT_READ | PROT_WRITE | PROT_EXEC;
14377c478bd9Sstevel@tonic-gate 			break;
14387c478bd9Sstevel@tonic-gate 		}
14397c478bd9Sstevel@tonic-gate 
14407c478bd9Sstevel@tonic-gate 		prot = VPP_PROT(vpage);
14417c478bd9Sstevel@tonic-gate 		if ((prot & protchk) == 0)
14427c478bd9Sstevel@tonic-gate 			return (FC_PROT);	/* illegal access type */
14437c478bd9Sstevel@tonic-gate 	} else {
14447c478bd9Sstevel@tonic-gate 		prot = sdp->prot;
14457c478bd9Sstevel@tonic-gate 		/* caller has already done segment level protection check */
14467c478bd9Sstevel@tonic-gate 	}
14477c478bd9Sstevel@tonic-gate 
14487c478bd9Sstevel@tonic-gate 	if (type == F_SOFTLOCK) {
14497c478bd9Sstevel@tonic-gate 		mutex_enter(&freemem_lock);
14507c478bd9Sstevel@tonic-gate 		sdp->softlockcnt++;
14517c478bd9Sstevel@tonic-gate 		mutex_exit(&freemem_lock);
14527c478bd9Sstevel@tonic-gate 	}
14537c478bd9Sstevel@tonic-gate 
14547c478bd9Sstevel@tonic-gate 	hat_flags = ((type == F_SOFTLOCK) ? HAT_LOAD_LOCK : HAT_LOAD);
14557c478bd9Sstevel@tonic-gate 	offset = sdp->offset + (u_offset_t)(addr - seg->s_base);
14567c478bd9Sstevel@tonic-gate 	/*
14577c478bd9Sstevel@tonic-gate 	 * In the devmap framework, sdp->mapfunc is set to NULL.  we can get
14587c478bd9Sstevel@tonic-gate 	 * pfnum from dhp->dh_pfn (at beginning of segment) and offset from
14597c478bd9Sstevel@tonic-gate 	 * seg->s_base.
14607c478bd9Sstevel@tonic-gate 	 */
14617c478bd9Sstevel@tonic-gate 	if (dhp == NULL) {
14627c478bd9Sstevel@tonic-gate 		/* If segment has devmap_data, then dhp should be non-NULL */
14637c478bd9Sstevel@tonic-gate 		ASSERT(sdp->devmap_data == NULL);
14647c478bd9Sstevel@tonic-gate 		pfnum = (pfn_t)cdev_mmap(sdp->mapfunc, sdp->vp->v_rdev,
14657c478bd9Sstevel@tonic-gate 			(off_t)offset, prot);
14667c478bd9Sstevel@tonic-gate 		prot |= sdp->hat_attr;
14677c478bd9Sstevel@tonic-gate 	} else {
14687c478bd9Sstevel@tonic-gate 		ulong_t off;
14697c478bd9Sstevel@tonic-gate 		struct ddi_umem_cookie *cp;
14707c478bd9Sstevel@tonic-gate 		struct devmap_pmem_cookie *pcp;
14717c478bd9Sstevel@tonic-gate 
14727c478bd9Sstevel@tonic-gate 		/* ensure the dhp passed in contains addr. */
14737c478bd9Sstevel@tonic-gate 		ASSERT(dhp == devmap_find_handle(
14747c478bd9Sstevel@tonic-gate 			(devmap_handle_t *)sdp->devmap_data, addr));
14757c478bd9Sstevel@tonic-gate 
14767c478bd9Sstevel@tonic-gate 		off = addr - dhp->dh_uvaddr;
14777c478bd9Sstevel@tonic-gate 
14787c478bd9Sstevel@tonic-gate 		/*
14797c478bd9Sstevel@tonic-gate 		 * This routine assumes that the caller makes sure that the
14807c478bd9Sstevel@tonic-gate 		 * fields in dhp used below are unchanged due to remap during
14817c478bd9Sstevel@tonic-gate 		 * this call. Caller does HOLD_DHP_LOCK if neeed
14827c478bd9Sstevel@tonic-gate 		 */
14837c478bd9Sstevel@tonic-gate 		cp = dhp->dh_cookie;
14847c478bd9Sstevel@tonic-gate 		if (dhp->dh_flags & DEVMAP_MAPPING_INVALID) {
14857c478bd9Sstevel@tonic-gate 			pfnum = PFN_INVALID;
14867c478bd9Sstevel@tonic-gate 		} else if (cookie_is_devmem(cp)) {
14877c478bd9Sstevel@tonic-gate 			pfnum = dhp->dh_pfn + btop(off);
14887c478bd9Sstevel@tonic-gate 		} else if (cookie_is_pmem(cp)) {
14897c478bd9Sstevel@tonic-gate 			pcp = (struct devmap_pmem_cookie *)dhp->dh_pcookie;
14907c478bd9Sstevel@tonic-gate 			ASSERT((dhp->dh_roff & PAGEOFFSET) == 0 &&
14917c478bd9Sstevel@tonic-gate 				dhp->dh_roff < ptob(pcp->dp_npages));
14927c478bd9Sstevel@tonic-gate 			pfnum = page_pptonum(
14937c478bd9Sstevel@tonic-gate 			    pcp->dp_pparray[btop(off + dhp->dh_roff)]);
14947c478bd9Sstevel@tonic-gate 		} else {
14957c478bd9Sstevel@tonic-gate 			ASSERT(dhp->dh_roff < cp->size);
14967c478bd9Sstevel@tonic-gate 			ASSERT(dhp->dh_cvaddr >= cp->cvaddr &&
14977c478bd9Sstevel@tonic-gate 				dhp->dh_cvaddr < (cp->cvaddr + cp->size));
14987c478bd9Sstevel@tonic-gate 			ASSERT((dhp->dh_cvaddr + off) <=
14997c478bd9Sstevel@tonic-gate 				(cp->cvaddr + cp->size));
15007c478bd9Sstevel@tonic-gate 			ASSERT((dhp->dh_cvaddr + off + PAGESIZE) <=
15017c478bd9Sstevel@tonic-gate 				(cp->cvaddr + cp->size));
15027c478bd9Sstevel@tonic-gate 
15037c478bd9Sstevel@tonic-gate 			switch (cp->type) {
15047c478bd9Sstevel@tonic-gate 			case UMEM_LOCKED :
15057c478bd9Sstevel@tonic-gate 			    if (cp->pparray != NULL) {
15067c478bd9Sstevel@tonic-gate 				ASSERT((dhp->dh_roff & PAGEOFFSET) == 0);
15077c478bd9Sstevel@tonic-gate 				pfnum = page_pptonum(
15087c478bd9Sstevel@tonic-gate 				    cp->pparray[btop(off + dhp->dh_roff)]);
15097c478bd9Sstevel@tonic-gate 			    } else {
15107c478bd9Sstevel@tonic-gate 				pfnum = hat_getpfnum(
15117c478bd9Sstevel@tonic-gate 				    ((proc_t *)cp->procp)->p_as->a_hat,
15127c478bd9Sstevel@tonic-gate 				    cp->cvaddr + off);
15137c478bd9Sstevel@tonic-gate 			    }
15147c478bd9Sstevel@tonic-gate 			    break;
15157c478bd9Sstevel@tonic-gate 			case UMEM_TRASH :
15167c478bd9Sstevel@tonic-gate 			    pfnum = page_pptonum(trashpp);
15177c478bd9Sstevel@tonic-gate 			    /* We should set hat_flags to HAT_NOFAULT also */
15187c478bd9Sstevel@tonic-gate 			    /* However, not all hat layers implement this */
15197c478bd9Sstevel@tonic-gate 			    break;
15207c478bd9Sstevel@tonic-gate 			case KMEM_PAGEABLE:
15217c478bd9Sstevel@tonic-gate 			case KMEM_NON_PAGEABLE:
15227c478bd9Sstevel@tonic-gate 			    pfnum = hat_getpfnum(kas.a_hat,
15237c478bd9Sstevel@tonic-gate 				dhp->dh_cvaddr + off);
15247c478bd9Sstevel@tonic-gate 			    break;
15257c478bd9Sstevel@tonic-gate 			default :
15267c478bd9Sstevel@tonic-gate 			    pfnum = PFN_INVALID;
15277c478bd9Sstevel@tonic-gate 			    break;
15287c478bd9Sstevel@tonic-gate 			}
15297c478bd9Sstevel@tonic-gate 		}
15307c478bd9Sstevel@tonic-gate 		prot |= dhp->dh_hat_attr;
15317c478bd9Sstevel@tonic-gate 	}
15327c478bd9Sstevel@tonic-gate 	if (pfnum == PFN_INVALID) {
15337c478bd9Sstevel@tonic-gate 		return (FC_MAKE_ERR(EFAULT));
15347c478bd9Sstevel@tonic-gate 	}
15357c478bd9Sstevel@tonic-gate 	/* prot should already be OR'ed in with hat_attributes if needed */
15367c478bd9Sstevel@tonic-gate 
15377c478bd9Sstevel@tonic-gate 	TRACE_4(TR_FAC_DEVMAP, TR_DEVMAP_FAULTPAGE_CK1,
15387c478bd9Sstevel@tonic-gate 	    "segdev_faultpage: pfnum=%lx memory=%x prot=%x flags=%x",
15397c478bd9Sstevel@tonic-gate 	    pfnum, pf_is_memory(pfnum), prot, hat_flags);
15407c478bd9Sstevel@tonic-gate 	DEBUGF(9, (CE_CONT, "segdev_faultpage: pfnum %lx memory %x "
15417c478bd9Sstevel@tonic-gate 	    "prot %x flags %x\n", pfnum, pf_is_memory(pfnum), prot, hat_flags));
15427c478bd9Sstevel@tonic-gate 
15437c478bd9Sstevel@tonic-gate 	if (pf_is_memory(pfnum) || (dhp != NULL)) {
15447c478bd9Sstevel@tonic-gate 		/*
15457c478bd9Sstevel@tonic-gate 		 * It's not _really_ required here to pass sdp->hat_flags
15467c478bd9Sstevel@tonic-gate 		 * to hat_devload even though we do it.
15477c478bd9Sstevel@tonic-gate 		 * This is because hat figures it out DEVMEM mappings
15487c478bd9Sstevel@tonic-gate 		 * are non-consistent, anyway.
15497c478bd9Sstevel@tonic-gate 		 */
15507c478bd9Sstevel@tonic-gate 		hat_devload(hat, addr, PAGESIZE, pfnum,
15517c478bd9Sstevel@tonic-gate 				prot, hat_flags | sdp->hat_flags);
15527c478bd9Sstevel@tonic-gate 		return (0);
15537c478bd9Sstevel@tonic-gate 	}
15547c478bd9Sstevel@tonic-gate 
15557c478bd9Sstevel@tonic-gate 	/*
15567c478bd9Sstevel@tonic-gate 	 * Fall through to the case where devmap is not used and need to call
15577c478bd9Sstevel@tonic-gate 	 * up the device tree to set up the mapping
15587c478bd9Sstevel@tonic-gate 	 */
15597c478bd9Sstevel@tonic-gate 
15607c478bd9Sstevel@tonic-gate 	dip = VTOS(VTOCVP(sdp->vp))->s_dip;
15617c478bd9Sstevel@tonic-gate 	ASSERT(dip);
15627c478bd9Sstevel@tonic-gate 
15637c478bd9Sstevel@tonic-gate 	/*
15647c478bd9Sstevel@tonic-gate 	 * When calling ddi_map_fault, we do not OR in sdp->hat_attr
15657c478bd9Sstevel@tonic-gate 	 * This is because this calls drivers which may not expect
15667c478bd9Sstevel@tonic-gate 	 * prot to have any other values than PROT_ALL
15677c478bd9Sstevel@tonic-gate 	 * The root nexus driver has a hack to peek into the segment
15687c478bd9Sstevel@tonic-gate 	 * structure and then OR in sdp->hat_attr.
15697c478bd9Sstevel@tonic-gate 	 * XX In case the bus_ops interfaces are ever revisited
15707c478bd9Sstevel@tonic-gate 	 * we need to fix this. prot should include other hat attributes
15717c478bd9Sstevel@tonic-gate 	 */
15727c478bd9Sstevel@tonic-gate 	if (ddi_map_fault(dip, hat, seg, addr, NULL, pfnum, prot & PROT_ALL,
15737c478bd9Sstevel@tonic-gate 	    (uint_t)(type == F_SOFTLOCK)) != DDI_SUCCESS) {
15747c478bd9Sstevel@tonic-gate 		return (FC_MAKE_ERR(EFAULT));
15757c478bd9Sstevel@tonic-gate 	}
15767c478bd9Sstevel@tonic-gate 	return (0);
15777c478bd9Sstevel@tonic-gate }
15787c478bd9Sstevel@tonic-gate 
15797c478bd9Sstevel@tonic-gate static faultcode_t
15807c478bd9Sstevel@tonic-gate segdev_fault(
15817c478bd9Sstevel@tonic-gate 	struct hat *hat,		/* the hat */
15827c478bd9Sstevel@tonic-gate 	struct seg *seg,		/* the seg_dev of interest */
15837c478bd9Sstevel@tonic-gate 	caddr_t addr,			/* the address of the fault */
15847c478bd9Sstevel@tonic-gate 	size_t len,			/* the length of the range */
15857c478bd9Sstevel@tonic-gate 	enum fault_type type,		/* type of fault */
15867c478bd9Sstevel@tonic-gate 	enum seg_rw rw)			/* type of access at fault */
15877c478bd9Sstevel@tonic-gate {
15887c478bd9Sstevel@tonic-gate 	struct segdev_data *sdp = (struct segdev_data *)seg->s_data;
15897c478bd9Sstevel@tonic-gate 	devmap_handle_t *dhp_head = (devmap_handle_t *)sdp->devmap_data;
15907c478bd9Sstevel@tonic-gate 	devmap_handle_t *dhp;
15917c478bd9Sstevel@tonic-gate 	struct devmap_softlock *slock = NULL;
15927c478bd9Sstevel@tonic-gate 	ulong_t slpage = 0;
15937c478bd9Sstevel@tonic-gate 	ulong_t off;
15947c478bd9Sstevel@tonic-gate 	caddr_t maddr = addr;
15957c478bd9Sstevel@tonic-gate 	int err;
15967c478bd9Sstevel@tonic-gate 	int err_is_faultcode = 0;
15977c478bd9Sstevel@tonic-gate 
15987c478bd9Sstevel@tonic-gate 	TRACE_5(TR_FAC_DEVMAP, TR_DEVMAP_FAULT,
15997c478bd9Sstevel@tonic-gate 	    "segdev_fault: dhp_head=%p seg=%p addr=%p len=%lx type=%x",
16007c478bd9Sstevel@tonic-gate 	    (void *)dhp_head, (void *)seg, (void *)addr, len, type);
16017c478bd9Sstevel@tonic-gate 	DEBUGF(7, (CE_CONT, "segdev_fault: dhp_head %p seg %p "
16027c478bd9Sstevel@tonic-gate 	    "addr %p len %lx type %x\n",
16037c478bd9Sstevel@tonic-gate 	    (void *)dhp_head, (void *)seg, (void *)addr, len, type));
16047c478bd9Sstevel@tonic-gate 
16057c478bd9Sstevel@tonic-gate 	ASSERT(seg->s_as && AS_LOCK_HELD(seg->s_as, &seg->s_as->a_lock));
16067c478bd9Sstevel@tonic-gate 
16077c478bd9Sstevel@tonic-gate 	/* Handle non-devmap case */
16087c478bd9Sstevel@tonic-gate 	if (dhp_head == NULL)
16097c478bd9Sstevel@tonic-gate 		return (segdev_faultpages(hat, seg, addr, len, type, rw, NULL));
16107c478bd9Sstevel@tonic-gate 
16117c478bd9Sstevel@tonic-gate 	/* Find devmap handle */
16127c478bd9Sstevel@tonic-gate 	if ((dhp = devmap_find_handle(dhp_head, addr)) == NULL)
16137c478bd9Sstevel@tonic-gate 		return (FC_NOMAP);
16147c478bd9Sstevel@tonic-gate 
16157c478bd9Sstevel@tonic-gate 	/*
16167c478bd9Sstevel@tonic-gate 	 * The seg_dev driver does not implement copy-on-write,
16177c478bd9Sstevel@tonic-gate 	 * and always loads translations with maximal allowed permissions
16187c478bd9Sstevel@tonic-gate 	 * but we got an fault trying to access the device.
16197c478bd9Sstevel@tonic-gate 	 * Servicing the fault is not going to result in any better result
16207c478bd9Sstevel@tonic-gate 	 * RFE: If we want devmap_access callbacks to be involved in F_PROT
16217c478bd9Sstevel@tonic-gate 	 *	faults, then the code below is written for that
16227c478bd9Sstevel@tonic-gate 	 *	Pending resolution of the following:
16237c478bd9Sstevel@tonic-gate 	 *	- determine if the F_INVAL/F_SOFTLOCK syncing
16247c478bd9Sstevel@tonic-gate 	 *	is needed for F_PROT also or not. The code below assumes it does
16257c478bd9Sstevel@tonic-gate 	 *	- If driver sees F_PROT and calls devmap_load with same type,
16267c478bd9Sstevel@tonic-gate 	 *	then segdev_faultpages will fail with FC_PROT anyway, need to
16277c478bd9Sstevel@tonic-gate 	 *	change that so calls from devmap_load to segdev_faultpages for
16287c478bd9Sstevel@tonic-gate 	 *	F_PROT type are retagged to F_INVAL.
16297c478bd9Sstevel@tonic-gate 	 * RFE: Today we dont have drivers that use devmap and want to handle
16307c478bd9Sstevel@tonic-gate 	 *	F_PROT calls. The code in segdev_fault* is written to allow
16317c478bd9Sstevel@tonic-gate 	 *	this case but is not tested. A driver that needs this capability
16327c478bd9Sstevel@tonic-gate 	 *	should be able to remove the short-circuit case; resolve the
16337c478bd9Sstevel@tonic-gate 	 *	above issues and "should" work.
16347c478bd9Sstevel@tonic-gate 	 */
16357c478bd9Sstevel@tonic-gate 	if (type == F_PROT) {
16367c478bd9Sstevel@tonic-gate 		return (FC_PROT);
16377c478bd9Sstevel@tonic-gate 	}
16387c478bd9Sstevel@tonic-gate 
16397c478bd9Sstevel@tonic-gate 	/*
16407c478bd9Sstevel@tonic-gate 	 * Loop through dhp list calling devmap_access or segdev_faultpages for
16417c478bd9Sstevel@tonic-gate 	 * each devmap handle.
16427c478bd9Sstevel@tonic-gate 	 * drivers which implement devmap_access can interpose on faults and do
16437c478bd9Sstevel@tonic-gate 	 * device-appropriate special actions before calling devmap_load.
16447c478bd9Sstevel@tonic-gate 	 */
16457c478bd9Sstevel@tonic-gate 
16467c478bd9Sstevel@tonic-gate 	/*
16477c478bd9Sstevel@tonic-gate 	 * Unfortunately, this simple loop has turned out to expose a variety
16487c478bd9Sstevel@tonic-gate 	 * of complex problems which results in the following convoluted code.
16497c478bd9Sstevel@tonic-gate 	 *
16507c478bd9Sstevel@tonic-gate 	 * First, a desire to handle a serialization of F_SOFTLOCK calls
16517c478bd9Sstevel@tonic-gate 	 * to the driver within the framework.
16527c478bd9Sstevel@tonic-gate 	 *	This results in a dh_softlock structure that is on a per device
16537c478bd9Sstevel@tonic-gate 	 *	(or device instance) basis and serializes devmap_access calls.
16547c478bd9Sstevel@tonic-gate 	 *	Ideally we would need to do this for underlying
16557c478bd9Sstevel@tonic-gate 	 *	memory/device regions that are being faulted on
16567c478bd9Sstevel@tonic-gate 	 *	but that is hard to identify and with REMAP, harder
16577c478bd9Sstevel@tonic-gate 	 * Second, a desire to serialize F_INVAL(and F_PROT) calls w.r.t.
16587c478bd9Sstevel@tonic-gate 	 * 	to F_SOFTLOCK calls to the driver.
16597c478bd9Sstevel@tonic-gate 	 * These serializations are to simplify the driver programmer model.
16607c478bd9Sstevel@tonic-gate 	 * To support these two features, the code first goes through the
16617c478bd9Sstevel@tonic-gate 	 *	devmap handles and counts the pages (slpage) that are covered
16627c478bd9Sstevel@tonic-gate 	 *	by devmap_access callbacks.
16637c478bd9Sstevel@tonic-gate 	 * This part ends with a devmap_softlock_enter call
16647c478bd9Sstevel@tonic-gate 	 *	which allows only one F_SOFTLOCK active on a device instance,
16657c478bd9Sstevel@tonic-gate 	 *	but multiple F_INVAL/F_PROTs can be active except when a
16667c478bd9Sstevel@tonic-gate 	 *	F_SOFTLOCK is active
16677c478bd9Sstevel@tonic-gate 	 *
16687c478bd9Sstevel@tonic-gate 	 * Next, we dont short-circuit the fault code upfront to call
16697c478bd9Sstevel@tonic-gate 	 *	segdev_softunlock for F_SOFTUNLOCK, because we must use
16707c478bd9Sstevel@tonic-gate 	 *	the same length when we softlock and softunlock.
16717c478bd9Sstevel@tonic-gate 	 *
16727c478bd9Sstevel@tonic-gate 	 *	-Hat layers may not support softunlocking lengths less than the
16737c478bd9Sstevel@tonic-gate 	 *	original length when there is large page support.
16747c478bd9Sstevel@tonic-gate 	 *	-kpmem locking is dependent on keeping the lengths same.
16757c478bd9Sstevel@tonic-gate 	 *	-if drivers handled F_SOFTLOCK, they probably also expect to
16767c478bd9Sstevel@tonic-gate 	 *		see an F_SOFTUNLOCK of the same length
16777c478bd9Sstevel@tonic-gate 	 *	Hence, if extending lengths during softlock,
16787c478bd9Sstevel@tonic-gate 	 *	softunlock has to make the same adjustments and goes through
16797c478bd9Sstevel@tonic-gate 	 *	the same loop calling segdev_faultpages/segdev_softunlock
16807c478bd9Sstevel@tonic-gate 	 *	But some of the synchronization and error handling is different
16817c478bd9Sstevel@tonic-gate 	 */
16827c478bd9Sstevel@tonic-gate 
16837c478bd9Sstevel@tonic-gate 	if (type != F_SOFTUNLOCK) {
16847c478bd9Sstevel@tonic-gate 		devmap_handle_t *dhpp = dhp;
16857c478bd9Sstevel@tonic-gate 		size_t slen = len;
16867c478bd9Sstevel@tonic-gate 
16877c478bd9Sstevel@tonic-gate 		/*
16887c478bd9Sstevel@tonic-gate 		 * Calculate count of pages that are :
16897c478bd9Sstevel@tonic-gate 		 * a) within the (potentially extended) fault region
16907c478bd9Sstevel@tonic-gate 		 * b) AND covered by devmap handle with devmap_access
16917c478bd9Sstevel@tonic-gate 		 */
16927c478bd9Sstevel@tonic-gate 		off = (ulong_t)(addr - dhpp->dh_uvaddr);
16937c478bd9Sstevel@tonic-gate 		while (slen != 0) {
16947c478bd9Sstevel@tonic-gate 			size_t mlen;
16957c478bd9Sstevel@tonic-gate 
16967c478bd9Sstevel@tonic-gate 			/*
16977c478bd9Sstevel@tonic-gate 			 * Softlocking on a region that allows remap is
16987c478bd9Sstevel@tonic-gate 			 * unsupported due to unresolved locking issues
16997c478bd9Sstevel@tonic-gate 			 * XXX: unclear what these are?
17007c478bd9Sstevel@tonic-gate 			 *	One potential is that if there is a pending
17017c478bd9Sstevel@tonic-gate 			 *	softlock, then a remap should not be allowed
17027c478bd9Sstevel@tonic-gate 			 *	until the unlock is done. This is easily
17037c478bd9Sstevel@tonic-gate 			 *	fixed by returning error in devmap*remap on
17047c478bd9Sstevel@tonic-gate 			 *	checking the dh->dh_softlock->softlocked value
17057c478bd9Sstevel@tonic-gate 			 */
17067c478bd9Sstevel@tonic-gate 			if ((type == F_SOFTLOCK) &&
17077c478bd9Sstevel@tonic-gate 			    (dhpp->dh_flags & DEVMAP_ALLOW_REMAP)) {
17087c478bd9Sstevel@tonic-gate 				return (FC_NOSUPPORT);
17097c478bd9Sstevel@tonic-gate 			}
17107c478bd9Sstevel@tonic-gate 
17117c478bd9Sstevel@tonic-gate 			mlen = MIN(slen, (dhpp->dh_len - off));
17127c478bd9Sstevel@tonic-gate 			if (dhpp->dh_callbackops.devmap_access) {
17137c478bd9Sstevel@tonic-gate 				size_t llen;
17147c478bd9Sstevel@tonic-gate 				caddr_t laddr;
17157c478bd9Sstevel@tonic-gate 				/*
17167c478bd9Sstevel@tonic-gate 				 * use extended length for large page mappings
17177c478bd9Sstevel@tonic-gate 				 */
17187c478bd9Sstevel@tonic-gate 				HOLD_DHP_LOCK(dhpp);
17197c478bd9Sstevel@tonic-gate 				if ((sdp->pageprot == 0) &&
17207c478bd9Sstevel@tonic-gate 				    (dhpp->dh_flags & DEVMAP_FLAG_LARGE)) {
17217c478bd9Sstevel@tonic-gate 					devmap_get_large_pgsize(dhpp,
17227c478bd9Sstevel@tonic-gate 					    mlen, maddr, &llen, &laddr);
17237c478bd9Sstevel@tonic-gate 				} else {
17247c478bd9Sstevel@tonic-gate 					llen = mlen;
17257c478bd9Sstevel@tonic-gate 				}
17267c478bd9Sstevel@tonic-gate 				RELE_DHP_LOCK(dhpp);
17277c478bd9Sstevel@tonic-gate 
17287c478bd9Sstevel@tonic-gate 				slpage += btopr(llen);
17297c478bd9Sstevel@tonic-gate 				slock = dhpp->dh_softlock;
17307c478bd9Sstevel@tonic-gate 			}
17317c478bd9Sstevel@tonic-gate 			maddr += mlen;
17327c478bd9Sstevel@tonic-gate 			ASSERT(slen >= mlen);
17337c478bd9Sstevel@tonic-gate 			slen -= mlen;
17347c478bd9Sstevel@tonic-gate 			dhpp = dhpp->dh_next;
17357c478bd9Sstevel@tonic-gate 			off = 0;
17367c478bd9Sstevel@tonic-gate 		}
17377c478bd9Sstevel@tonic-gate 		/*
17387c478bd9Sstevel@tonic-gate 		 * synchonize with other faulting threads and wait till safe
17397c478bd9Sstevel@tonic-gate 		 * devmap_softlock_enter might return due to signal in cv_wait
17407c478bd9Sstevel@tonic-gate 		 *
17417c478bd9Sstevel@tonic-gate 		 * devmap_softlock_enter has to be called outside of while loop
17427c478bd9Sstevel@tonic-gate 		 * to prevent a deadlock if len spans over multiple dhps.
17437c478bd9Sstevel@tonic-gate 		 * dh_softlock is based on device instance and if multiple dhps
17447c478bd9Sstevel@tonic-gate 		 * use the same device instance, the second dhp's LOCK call
17457c478bd9Sstevel@tonic-gate 		 * will hang waiting on the first to complete.
17467c478bd9Sstevel@tonic-gate 		 * devmap_setup verifies that slocks in a dhp_chain are same.
17477c478bd9Sstevel@tonic-gate 		 * RFE: this deadlock only hold true for F_SOFTLOCK. For
17487c478bd9Sstevel@tonic-gate 		 * 	F_INVAL/F_PROT, since we now allow multiple in parallel,
17497c478bd9Sstevel@tonic-gate 		 *	we could have done the softlock_enter inside the loop
17507c478bd9Sstevel@tonic-gate 		 *	and supported multi-dhp mappings with dissimilar devices
17517c478bd9Sstevel@tonic-gate 		 */
17527c478bd9Sstevel@tonic-gate 		if (err = devmap_softlock_enter(slock, slpage, type))
17537c478bd9Sstevel@tonic-gate 			return (FC_MAKE_ERR(err));
17547c478bd9Sstevel@tonic-gate 	}
17557c478bd9Sstevel@tonic-gate 
17567c478bd9Sstevel@tonic-gate 	/* reset 'maddr' to the start addr of the range of fault. */
17577c478bd9Sstevel@tonic-gate 	maddr = addr;
17587c478bd9Sstevel@tonic-gate 
17597c478bd9Sstevel@tonic-gate 	/* calculate the offset corresponds to 'addr' in the first dhp. */
17607c478bd9Sstevel@tonic-gate 	off = (ulong_t)(addr - dhp->dh_uvaddr);
17617c478bd9Sstevel@tonic-gate 
17627c478bd9Sstevel@tonic-gate 	/*
17637c478bd9Sstevel@tonic-gate 	 * The fault length may span over multiple dhps.
17647c478bd9Sstevel@tonic-gate 	 * Loop until the total length is satisfied.
17657c478bd9Sstevel@tonic-gate 	 */
17667c478bd9Sstevel@tonic-gate 	while (len != 0) {
17677c478bd9Sstevel@tonic-gate 		size_t llen;
17687c478bd9Sstevel@tonic-gate 		size_t mlen;
17697c478bd9Sstevel@tonic-gate 		caddr_t laddr;
17707c478bd9Sstevel@tonic-gate 
17717c478bd9Sstevel@tonic-gate 		/*
17727c478bd9Sstevel@tonic-gate 		 * mlen is the smaller of 'len' and the length
17737c478bd9Sstevel@tonic-gate 		 * from addr to the end of mapping defined by dhp.
17747c478bd9Sstevel@tonic-gate 		 */
17757c478bd9Sstevel@tonic-gate 		mlen = MIN(len, (dhp->dh_len - off));
17767c478bd9Sstevel@tonic-gate 
17777c478bd9Sstevel@tonic-gate 		HOLD_DHP_LOCK(dhp);
17787c478bd9Sstevel@tonic-gate 		/*
17797c478bd9Sstevel@tonic-gate 		 * Pass the extended length and address to devmap_access
17807c478bd9Sstevel@tonic-gate 		 * if large pagesize is used for loading address translations.
17817c478bd9Sstevel@tonic-gate 		 */
17827c478bd9Sstevel@tonic-gate 		if ((sdp->pageprot == 0) &&
17837c478bd9Sstevel@tonic-gate 		    (dhp->dh_flags & DEVMAP_FLAG_LARGE)) {
17847c478bd9Sstevel@tonic-gate 			devmap_get_large_pgsize(dhp, mlen, maddr,
17857c478bd9Sstevel@tonic-gate 				&llen, &laddr);
17867c478bd9Sstevel@tonic-gate 			ASSERT(maddr == addr || laddr == maddr);
17877c478bd9Sstevel@tonic-gate 		} else {
17887c478bd9Sstevel@tonic-gate 			llen = mlen;
17897c478bd9Sstevel@tonic-gate 			laddr = maddr;
17907c478bd9Sstevel@tonic-gate 		}
17917c478bd9Sstevel@tonic-gate 
17927c478bd9Sstevel@tonic-gate 		if (dhp->dh_callbackops.devmap_access != NULL) {
17937c478bd9Sstevel@tonic-gate 			offset_t aoff;
17947c478bd9Sstevel@tonic-gate 
17957c478bd9Sstevel@tonic-gate 			aoff = sdp->offset + (offset_t)(laddr - seg->s_base);
17967c478bd9Sstevel@tonic-gate 
17977c478bd9Sstevel@tonic-gate 			/*
17987c478bd9Sstevel@tonic-gate 			 * call driver's devmap_access entry point which will
17997c478bd9Sstevel@tonic-gate 			 * call devmap_load/contextmgmt to load the translations
18007c478bd9Sstevel@tonic-gate 			 *
18017c478bd9Sstevel@tonic-gate 			 * We drop the dhp_lock before calling access so
18027c478bd9Sstevel@tonic-gate 			 * drivers can call devmap_*_remap within access
18037c478bd9Sstevel@tonic-gate 			 */
18047c478bd9Sstevel@tonic-gate 			RELE_DHP_LOCK(dhp);
18057c478bd9Sstevel@tonic-gate 
18067c478bd9Sstevel@tonic-gate 			err = (*dhp->dh_callbackops.devmap_access)(
18077c478bd9Sstevel@tonic-gate 			    dhp, (void *)dhp->dh_pvtp, aoff, llen, type, rw);
18087c478bd9Sstevel@tonic-gate 		} else {
18097c478bd9Sstevel@tonic-gate 			/*
18107c478bd9Sstevel@tonic-gate 			 * If no devmap_access entry point, then load mappings
18117c478bd9Sstevel@tonic-gate 			 * hold dhp_lock across faultpages if REMAP
18127c478bd9Sstevel@tonic-gate 			 */
18137c478bd9Sstevel@tonic-gate 			err = segdev_faultpages(hat, seg, laddr, llen,
18147c478bd9Sstevel@tonic-gate 			    type, rw, dhp);
18157c478bd9Sstevel@tonic-gate 			err_is_faultcode = 1;
18167c478bd9Sstevel@tonic-gate 			RELE_DHP_LOCK(dhp);
18177c478bd9Sstevel@tonic-gate 		}
18187c478bd9Sstevel@tonic-gate 
18197c478bd9Sstevel@tonic-gate 		if (err) {
18207c478bd9Sstevel@tonic-gate 			if ((type == F_SOFTLOCK) && (maddr > addr)) {
18217c478bd9Sstevel@tonic-gate 				/*
18227c478bd9Sstevel@tonic-gate 				 * If not first dhp, use
18237c478bd9Sstevel@tonic-gate 				 * segdev_fault(F_SOFTUNLOCK) for prior dhps
18247c478bd9Sstevel@tonic-gate 				 * While this is recursion, it is incorrect to
18257c478bd9Sstevel@tonic-gate 				 * call just segdev_softunlock
18267c478bd9Sstevel@tonic-gate 				 * if we are using either large pages
18277c478bd9Sstevel@tonic-gate 				 * or devmap_access. It will be more right
18287c478bd9Sstevel@tonic-gate 				 * to go through the same loop as above
18297c478bd9Sstevel@tonic-gate 				 * rather than call segdev_softunlock directly
18307c478bd9Sstevel@tonic-gate 				 * It will use the right lenghths as well as
18317c478bd9Sstevel@tonic-gate 				 * call into the driver devmap_access routines.
18327c478bd9Sstevel@tonic-gate 				 */
18337c478bd9Sstevel@tonic-gate 				size_t done = (size_t)(maddr - addr);
18347c478bd9Sstevel@tonic-gate 				(void) segdev_fault(hat, seg, addr, done,
18357c478bd9Sstevel@tonic-gate 					F_SOFTUNLOCK, S_OTHER);
18367c478bd9Sstevel@tonic-gate 				/*
18377c478bd9Sstevel@tonic-gate 				 * reduce slpage by number of pages
18387c478bd9Sstevel@tonic-gate 				 * released by segdev_softunlock
18397c478bd9Sstevel@tonic-gate 				 */
18407c478bd9Sstevel@tonic-gate 				ASSERT(slpage >= btopr(done));
18417c478bd9Sstevel@tonic-gate 				devmap_softlock_exit(slock,
18427c478bd9Sstevel@tonic-gate 					slpage - btopr(done), type);
18437c478bd9Sstevel@tonic-gate 			} else {
18447c478bd9Sstevel@tonic-gate 				devmap_softlock_exit(slock, slpage, type);
18457c478bd9Sstevel@tonic-gate 			}
18467c478bd9Sstevel@tonic-gate 
18477c478bd9Sstevel@tonic-gate 
18487c478bd9Sstevel@tonic-gate 			/*
18497c478bd9Sstevel@tonic-gate 			 * Segdev_faultpages() already returns a faultcode,
18507c478bd9Sstevel@tonic-gate 			 * hence, result from segdev_faultpages() should be
18517c478bd9Sstevel@tonic-gate 			 * returned directly.
18527c478bd9Sstevel@tonic-gate 			 */
18537c478bd9Sstevel@tonic-gate 			if (err_is_faultcode)
18547c478bd9Sstevel@tonic-gate 				return (err);
18557c478bd9Sstevel@tonic-gate 			return (FC_MAKE_ERR(err));
18567c478bd9Sstevel@tonic-gate 		}
18577c478bd9Sstevel@tonic-gate 
18587c478bd9Sstevel@tonic-gate 		maddr += mlen;
18597c478bd9Sstevel@tonic-gate 		ASSERT(len >= mlen);
18607c478bd9Sstevel@tonic-gate 		len -= mlen;
18617c478bd9Sstevel@tonic-gate 		dhp = dhp->dh_next;
18627c478bd9Sstevel@tonic-gate 		off = 0;
18637c478bd9Sstevel@tonic-gate 
18647c478bd9Sstevel@tonic-gate 		ASSERT(!dhp || len == 0 || maddr == dhp->dh_uvaddr);
18657c478bd9Sstevel@tonic-gate 	}
18667c478bd9Sstevel@tonic-gate 	/*
18677c478bd9Sstevel@tonic-gate 	 * release the softlock count at end of fault
18687c478bd9Sstevel@tonic-gate 	 * For F_SOFTLOCk this is done in the later F_SOFTUNLOCK
18697c478bd9Sstevel@tonic-gate 	 */
18707c478bd9Sstevel@tonic-gate 	if ((type == F_INVAL) || (type == F_PROT))
18717c478bd9Sstevel@tonic-gate 		devmap_softlock_exit(slock, slpage, type);
18727c478bd9Sstevel@tonic-gate 	return (0);
18737c478bd9Sstevel@tonic-gate }
18747c478bd9Sstevel@tonic-gate 
18757c478bd9Sstevel@tonic-gate /*
18767c478bd9Sstevel@tonic-gate  * segdev_faultpages
18777c478bd9Sstevel@tonic-gate  *
18787c478bd9Sstevel@tonic-gate  * Used to fault in seg_dev segment pages. Called by segdev_fault or devmap_load
18797c478bd9Sstevel@tonic-gate  * This routine assumes that the callers makes sure that the fields
18807c478bd9Sstevel@tonic-gate  * in dhp used below are not changed due to remap during this call.
18817c478bd9Sstevel@tonic-gate  * Caller does HOLD_DHP_LOCK if neeed
18827c478bd9Sstevel@tonic-gate  * This routine returns a faultcode_t as a return value for segdev_fault.
18837c478bd9Sstevel@tonic-gate  */
18847c478bd9Sstevel@tonic-gate static faultcode_t
18857c478bd9Sstevel@tonic-gate segdev_faultpages(
18867c478bd9Sstevel@tonic-gate 	struct hat *hat,		/* the hat */
18877c478bd9Sstevel@tonic-gate 	struct seg *seg,		/* the seg_dev of interest */
18887c478bd9Sstevel@tonic-gate 	caddr_t addr,			/* the address of the fault */
18897c478bd9Sstevel@tonic-gate 	size_t len,			/* the length of the range */
18907c478bd9Sstevel@tonic-gate 	enum fault_type type,		/* type of fault */
18917c478bd9Sstevel@tonic-gate 	enum seg_rw rw,			/* type of access at fault */
18927c478bd9Sstevel@tonic-gate 	devmap_handle_t *dhp)		/* devmap handle */
18937c478bd9Sstevel@tonic-gate {
18947c478bd9Sstevel@tonic-gate 	register struct segdev_data *sdp = (struct segdev_data *)seg->s_data;
18957c478bd9Sstevel@tonic-gate 	register caddr_t a;
18967c478bd9Sstevel@tonic-gate 	struct vpage *vpage;
18977c478bd9Sstevel@tonic-gate 	struct ddi_umem_cookie *kpmem_cookie = NULL;
18987c478bd9Sstevel@tonic-gate 	int err;
18997c478bd9Sstevel@tonic-gate 
19007c478bd9Sstevel@tonic-gate 	TRACE_4(TR_FAC_DEVMAP, TR_DEVMAP_FAULTPAGES,
19017c478bd9Sstevel@tonic-gate 	    "segdev_faultpages: dhp=%p seg=%p addr=%p len=%lx",
19027c478bd9Sstevel@tonic-gate 	    (void *)dhp, (void *)seg, (void *)addr, len);
19037c478bd9Sstevel@tonic-gate 	DEBUGF(5, (CE_CONT, "segdev_faultpages: "
19047c478bd9Sstevel@tonic-gate 	    "dhp %p seg %p addr %p len %lx\n",
19057c478bd9Sstevel@tonic-gate 	    (void *)dhp, (void *)seg, (void *)addr, len));
19067c478bd9Sstevel@tonic-gate 
19077c478bd9Sstevel@tonic-gate 	/*
19087c478bd9Sstevel@tonic-gate 	 * The seg_dev driver does not implement copy-on-write,
19097c478bd9Sstevel@tonic-gate 	 * and always loads translations with maximal allowed permissions
19107c478bd9Sstevel@tonic-gate 	 * but we got an fault trying to access the device.
19117c478bd9Sstevel@tonic-gate 	 * Servicing the fault is not going to result in any better result
19127c478bd9Sstevel@tonic-gate 	 * XXX: If we want to allow devmap_access to handle F_PROT calls,
19137c478bd9Sstevel@tonic-gate 	 * This code should be removed and let the normal fault handling
19147c478bd9Sstevel@tonic-gate 	 * take care of finding the error
19157c478bd9Sstevel@tonic-gate 	 */
19167c478bd9Sstevel@tonic-gate 	if (type == F_PROT) {
19177c478bd9Sstevel@tonic-gate 		return (FC_PROT);
19187c478bd9Sstevel@tonic-gate 	}
19197c478bd9Sstevel@tonic-gate 
19207c478bd9Sstevel@tonic-gate 	if (type == F_SOFTUNLOCK) {
19217c478bd9Sstevel@tonic-gate 		segdev_softunlock(hat, seg, addr, len, rw);
19227c478bd9Sstevel@tonic-gate 		return (0);
19237c478bd9Sstevel@tonic-gate 	}
19247c478bd9Sstevel@tonic-gate 
19257c478bd9Sstevel@tonic-gate 	/*
19267c478bd9Sstevel@tonic-gate 	 * For kernel pageable memory, fault/lock segkp pages
19277c478bd9Sstevel@tonic-gate 	 * We hold this until the completion of this
19287c478bd9Sstevel@tonic-gate 	 * fault (INVAL/PROT) or till unlock (SOFTLOCK).
19297c478bd9Sstevel@tonic-gate 	 */
19307c478bd9Sstevel@tonic-gate 	if ((dhp != NULL) && dhp_is_kpmem(dhp)) {
19317c478bd9Sstevel@tonic-gate 		kpmem_cookie = (struct ddi_umem_cookie *)dhp->dh_cookie;
19327c478bd9Sstevel@tonic-gate 		if (err = acquire_kpmem_lock(kpmem_cookie, btopr(len)))
19337c478bd9Sstevel@tonic-gate 			return (err);
19347c478bd9Sstevel@tonic-gate 	}
19357c478bd9Sstevel@tonic-gate 
19367c478bd9Sstevel@tonic-gate 	/*
19377c478bd9Sstevel@tonic-gate 	 * If we have the same protections for the entire segment,
19387c478bd9Sstevel@tonic-gate 	 * insure that the access being attempted is legitimate.
19397c478bd9Sstevel@tonic-gate 	 */
19407c478bd9Sstevel@tonic-gate 	mutex_enter(&sdp->lock);
19417c478bd9Sstevel@tonic-gate 	if (sdp->pageprot == 0) {
19427c478bd9Sstevel@tonic-gate 		uint_t protchk;
19437c478bd9Sstevel@tonic-gate 
19447c478bd9Sstevel@tonic-gate 		switch (rw) {
19457c478bd9Sstevel@tonic-gate 		case S_READ:
19467c478bd9Sstevel@tonic-gate 			protchk = PROT_READ;
19477c478bd9Sstevel@tonic-gate 			break;
19487c478bd9Sstevel@tonic-gate 		case S_WRITE:
19497c478bd9Sstevel@tonic-gate 			protchk = PROT_WRITE;
19507c478bd9Sstevel@tonic-gate 			break;
19517c478bd9Sstevel@tonic-gate 		case S_EXEC:
19527c478bd9Sstevel@tonic-gate 			protchk = PROT_EXEC;
19537c478bd9Sstevel@tonic-gate 			break;
19547c478bd9Sstevel@tonic-gate 		case S_OTHER:
19557c478bd9Sstevel@tonic-gate 		default:
19567c478bd9Sstevel@tonic-gate 			protchk = PROT_READ | PROT_WRITE | PROT_EXEC;
19577c478bd9Sstevel@tonic-gate 			break;
19587c478bd9Sstevel@tonic-gate 		}
19597c478bd9Sstevel@tonic-gate 
19607c478bd9Sstevel@tonic-gate 		if ((sdp->prot & protchk) == 0) {
19617c478bd9Sstevel@tonic-gate 			mutex_exit(&sdp->lock);
19627c478bd9Sstevel@tonic-gate 			/* undo kpmem locking */
19637c478bd9Sstevel@tonic-gate 			if (kpmem_cookie != NULL) {
19647c478bd9Sstevel@tonic-gate 				release_kpmem_lock(kpmem_cookie, btopr(len));
19657c478bd9Sstevel@tonic-gate 			}
19667c478bd9Sstevel@tonic-gate 			return (FC_PROT);	/* illegal access type */
19677c478bd9Sstevel@tonic-gate 		}
19687c478bd9Sstevel@tonic-gate 	}
19697c478bd9Sstevel@tonic-gate 
19707c478bd9Sstevel@tonic-gate 	/*
19717c478bd9Sstevel@tonic-gate 	 * we do a single hat_devload for the range if
19727c478bd9Sstevel@tonic-gate 	 *   - devmap framework (dhp is not NULL),
19737c478bd9Sstevel@tonic-gate 	 *   - pageprot == 0, i.e., no per-page protection set and
19747c478bd9Sstevel@tonic-gate 	 *   - is device pages, irrespective of whether we are using large pages
19757c478bd9Sstevel@tonic-gate 	 */
19767c478bd9Sstevel@tonic-gate 	if ((sdp->pageprot == 0) && (dhp != NULL) && dhp_is_devmem(dhp)) {
19777c478bd9Sstevel@tonic-gate 		pfn_t pfnum;
19787c478bd9Sstevel@tonic-gate 		uint_t hat_flags;
19797c478bd9Sstevel@tonic-gate 
19807c478bd9Sstevel@tonic-gate 		if (dhp->dh_flags & DEVMAP_MAPPING_INVALID) {
19817c478bd9Sstevel@tonic-gate 			mutex_exit(&sdp->lock);
19827c478bd9Sstevel@tonic-gate 			return (FC_NOMAP);
19837c478bd9Sstevel@tonic-gate 		}
19847c478bd9Sstevel@tonic-gate 
19857c478bd9Sstevel@tonic-gate 		if (type == F_SOFTLOCK) {
19867c478bd9Sstevel@tonic-gate 			mutex_enter(&freemem_lock);
19877c478bd9Sstevel@tonic-gate 			sdp->softlockcnt += btopr(len);
19887c478bd9Sstevel@tonic-gate 			mutex_exit(&freemem_lock);
19897c478bd9Sstevel@tonic-gate 		}
19907c478bd9Sstevel@tonic-gate 
19917c478bd9Sstevel@tonic-gate 		hat_flags = ((type == F_SOFTLOCK) ? HAT_LOAD_LOCK : HAT_LOAD);
19927c478bd9Sstevel@tonic-gate 		pfnum = dhp->dh_pfn + btop((uintptr_t)(addr - dhp->dh_uvaddr));
19937c478bd9Sstevel@tonic-gate 		ASSERT(!pf_is_memory(pfnum));
19947c478bd9Sstevel@tonic-gate 
19957c478bd9Sstevel@tonic-gate 		hat_devload(hat, addr, len, pfnum, sdp->prot | dhp->dh_hat_attr,
19967c478bd9Sstevel@tonic-gate 			hat_flags | sdp->hat_flags);
19977c478bd9Sstevel@tonic-gate 		mutex_exit(&sdp->lock);
19987c478bd9Sstevel@tonic-gate 		return (0);
19997c478bd9Sstevel@tonic-gate 	}
20007c478bd9Sstevel@tonic-gate 
20017c478bd9Sstevel@tonic-gate 	/* Handle cases where we have to loop through fault handling per-page */
20027c478bd9Sstevel@tonic-gate 
20037c478bd9Sstevel@tonic-gate 	if (sdp->vpage == NULL)
20047c478bd9Sstevel@tonic-gate 		vpage = NULL;
20057c478bd9Sstevel@tonic-gate 	else
20067c478bd9Sstevel@tonic-gate 		vpage = &sdp->vpage[seg_page(seg, addr)];
20077c478bd9Sstevel@tonic-gate 
20087c478bd9Sstevel@tonic-gate 	/* loop over the address range handling each fault */
20097c478bd9Sstevel@tonic-gate 	for (a = addr; a < addr + len; a += PAGESIZE) {
20107c478bd9Sstevel@tonic-gate 		if (err = segdev_faultpage(hat, seg, a, vpage, type, rw, dhp)) {
20117c478bd9Sstevel@tonic-gate 			break;
20127c478bd9Sstevel@tonic-gate 		}
20137c478bd9Sstevel@tonic-gate 		if (vpage != NULL)
20147c478bd9Sstevel@tonic-gate 			vpage++;
20157c478bd9Sstevel@tonic-gate 	}
20167c478bd9Sstevel@tonic-gate 	mutex_exit(&sdp->lock);
20177c478bd9Sstevel@tonic-gate 	if (err && (type == F_SOFTLOCK)) { /* error handling for F_SOFTLOCK */
20187c478bd9Sstevel@tonic-gate 		size_t done = (size_t)(a - addr); /* pages fault successfully */
20197c478bd9Sstevel@tonic-gate 		if (done > 0) {
20207c478bd9Sstevel@tonic-gate 			/* use softunlock for those pages */
20217c478bd9Sstevel@tonic-gate 			segdev_softunlock(hat, seg, addr, done, S_OTHER);
20227c478bd9Sstevel@tonic-gate 		}
20237c478bd9Sstevel@tonic-gate 		if (kpmem_cookie != NULL) {
20247c478bd9Sstevel@tonic-gate 			/* release kpmem lock for rest of pages */
20257c478bd9Sstevel@tonic-gate 			ASSERT(len >= done);
20267c478bd9Sstevel@tonic-gate 			release_kpmem_lock(kpmem_cookie, btopr(len - done));
20277c478bd9Sstevel@tonic-gate 		}
20287c478bd9Sstevel@tonic-gate 	} else if ((kpmem_cookie != NULL) && (type != F_SOFTLOCK)) {
20297c478bd9Sstevel@tonic-gate 		/* for non-SOFTLOCK cases, release kpmem */
20307c478bd9Sstevel@tonic-gate 		release_kpmem_lock(kpmem_cookie, btopr(len));
20317c478bd9Sstevel@tonic-gate 	}
20327c478bd9Sstevel@tonic-gate 	return (err);
20337c478bd9Sstevel@tonic-gate }
20347c478bd9Sstevel@tonic-gate 
20357c478bd9Sstevel@tonic-gate /*
20367c478bd9Sstevel@tonic-gate  * Asynchronous page fault.  We simply do nothing since this
20377c478bd9Sstevel@tonic-gate  * entry point is not supposed to load up the translation.
20387c478bd9Sstevel@tonic-gate  */
20397c478bd9Sstevel@tonic-gate /*ARGSUSED*/
20407c478bd9Sstevel@tonic-gate static faultcode_t
20417c478bd9Sstevel@tonic-gate segdev_faulta(struct seg *seg, caddr_t addr)
20427c478bd9Sstevel@tonic-gate {
20437c478bd9Sstevel@tonic-gate 	TRACE_2(TR_FAC_DEVMAP, TR_DEVMAP_FAULTA,
20447c478bd9Sstevel@tonic-gate 	    "segdev_faulta: seg=%p addr=%p", (void *)seg, (void *)addr);
20457c478bd9Sstevel@tonic-gate 	ASSERT(seg->s_as && AS_LOCK_HELD(seg->s_as, &seg->s_as->a_lock));
20467c478bd9Sstevel@tonic-gate 
20477c478bd9Sstevel@tonic-gate 	return (0);
20487c478bd9Sstevel@tonic-gate }
20497c478bd9Sstevel@tonic-gate 
20507c478bd9Sstevel@tonic-gate static int
20517c478bd9Sstevel@tonic-gate segdev_setprot(struct seg *seg, caddr_t addr, size_t len, uint_t prot)
20527c478bd9Sstevel@tonic-gate {
20537c478bd9Sstevel@tonic-gate 	register struct segdev_data *sdp = (struct segdev_data *)seg->s_data;
20547c478bd9Sstevel@tonic-gate 	register devmap_handle_t *dhp;
20557c478bd9Sstevel@tonic-gate 	register struct vpage *vp, *evp;
20567c478bd9Sstevel@tonic-gate 	devmap_handle_t *dhp_head = (devmap_handle_t *)sdp->devmap_data;
20577c478bd9Sstevel@tonic-gate 	ulong_t off;
20587c478bd9Sstevel@tonic-gate 	size_t mlen, sz;
20597c478bd9Sstevel@tonic-gate 
20607c478bd9Sstevel@tonic-gate 	TRACE_4(TR_FAC_DEVMAP, TR_DEVMAP_SETPROT,
20617c478bd9Sstevel@tonic-gate 	    "segdev_setprot:start seg=%p addr=%p len=%lx prot=%x",
20627c478bd9Sstevel@tonic-gate 	    (void *)seg, (void *)addr, len, prot);
20637c478bd9Sstevel@tonic-gate 	ASSERT(seg->s_as && AS_LOCK_HELD(seg->s_as, &seg->s_as->a_lock));
20647c478bd9Sstevel@tonic-gate 
20657c478bd9Sstevel@tonic-gate 	if ((sz = sdp->softlockcnt) > 0 && dhp_head != NULL) {
20667c478bd9Sstevel@tonic-gate 		/*
20677c478bd9Sstevel@tonic-gate 		 * Fail the setprot if pages are SOFTLOCKed through this
20687c478bd9Sstevel@tonic-gate 		 * mapping.
20697c478bd9Sstevel@tonic-gate 		 * Softlockcnt is protected from change by the as read lock.
20707c478bd9Sstevel@tonic-gate 		 */
20717c478bd9Sstevel@tonic-gate 		TRACE_1(TR_FAC_DEVMAP, TR_DEVMAP_SETPROT_CK1,
20727c478bd9Sstevel@tonic-gate 		    "segdev_setprot:error softlockcnt=%lx", sz);
20737c478bd9Sstevel@tonic-gate 		DEBUGF(1, (CE_CONT, "segdev_setprot: softlockcnt %ld\n", sz));
20747c478bd9Sstevel@tonic-gate 		return (EAGAIN);
20757c478bd9Sstevel@tonic-gate 	}
20767c478bd9Sstevel@tonic-gate 
20777c478bd9Sstevel@tonic-gate 	if (dhp_head != NULL) {
20787c478bd9Sstevel@tonic-gate 		if ((dhp = devmap_find_handle(dhp_head, addr)) == NULL)
20797c478bd9Sstevel@tonic-gate 			return (EINVAL);
20807c478bd9Sstevel@tonic-gate 
20817c478bd9Sstevel@tonic-gate 		/*
20827c478bd9Sstevel@tonic-gate 		 * check if violate maxprot.
20837c478bd9Sstevel@tonic-gate 		 */
20847c478bd9Sstevel@tonic-gate 		off = (ulong_t)(addr - dhp->dh_uvaddr);
20857c478bd9Sstevel@tonic-gate 		mlen  = len;
20867c478bd9Sstevel@tonic-gate 		while (dhp) {
20877c478bd9Sstevel@tonic-gate 			if ((dhp->dh_maxprot & prot) != prot)
20887c478bd9Sstevel@tonic-gate 				return (EACCES);	/* violated maxprot */
20897c478bd9Sstevel@tonic-gate 
20907c478bd9Sstevel@tonic-gate 			if (mlen > (dhp->dh_len - off)) {
20917c478bd9Sstevel@tonic-gate 				mlen -= dhp->dh_len - off;
20927c478bd9Sstevel@tonic-gate 				dhp = dhp->dh_next;
20937c478bd9Sstevel@tonic-gate 				off = 0;
20947c478bd9Sstevel@tonic-gate 			} else
20957c478bd9Sstevel@tonic-gate 				break;
20967c478bd9Sstevel@tonic-gate 		}
20977c478bd9Sstevel@tonic-gate 	} else {
20987c478bd9Sstevel@tonic-gate 		if ((sdp->maxprot & prot) != prot)
20997c478bd9Sstevel@tonic-gate 			return (EACCES);
21007c478bd9Sstevel@tonic-gate 	}
21017c478bd9Sstevel@tonic-gate 
21027c478bd9Sstevel@tonic-gate 	mutex_enter(&sdp->lock);
21037c478bd9Sstevel@tonic-gate 	if (addr == seg->s_base && len == seg->s_size && sdp->pageprot == 0) {
21047c478bd9Sstevel@tonic-gate 		if (sdp->prot == prot) {
21057c478bd9Sstevel@tonic-gate 			mutex_exit(&sdp->lock);
21067c478bd9Sstevel@tonic-gate 			return (0);			/* all done */
21077c478bd9Sstevel@tonic-gate 		}
21087c478bd9Sstevel@tonic-gate 		sdp->prot = (uchar_t)prot;
21097c478bd9Sstevel@tonic-gate 	} else {
21107c478bd9Sstevel@tonic-gate 		sdp->pageprot = 1;
21117c478bd9Sstevel@tonic-gate 		if (sdp->vpage == NULL) {
21127c478bd9Sstevel@tonic-gate 			/*
21137c478bd9Sstevel@tonic-gate 			 * First time through setting per page permissions,
21147c478bd9Sstevel@tonic-gate 			 * initialize all the vpage structures to prot
21157c478bd9Sstevel@tonic-gate 			 */
21167c478bd9Sstevel@tonic-gate 			sdp->vpage = kmem_zalloc(vpgtob(seg_pages(seg)),
21177c478bd9Sstevel@tonic-gate 			    KM_SLEEP);
21187c478bd9Sstevel@tonic-gate 			evp = &sdp->vpage[seg_pages(seg)];
21197c478bd9Sstevel@tonic-gate 			for (vp = sdp->vpage; vp < evp; vp++)
21207c478bd9Sstevel@tonic-gate 				VPP_SETPROT(vp, sdp->prot);
21217c478bd9Sstevel@tonic-gate 		}
21227c478bd9Sstevel@tonic-gate 		/*
21237c478bd9Sstevel@tonic-gate 		 * Now go change the needed vpages protections.
21247c478bd9Sstevel@tonic-gate 		 */
21257c478bd9Sstevel@tonic-gate 		evp = &sdp->vpage[seg_page(seg, addr + len)];
21267c478bd9Sstevel@tonic-gate 		for (vp = &sdp->vpage[seg_page(seg, addr)]; vp < evp; vp++)
21277c478bd9Sstevel@tonic-gate 			VPP_SETPROT(vp, prot);
21287c478bd9Sstevel@tonic-gate 	}
21297c478bd9Sstevel@tonic-gate 	mutex_exit(&sdp->lock);
21307c478bd9Sstevel@tonic-gate 
21317c478bd9Sstevel@tonic-gate 	if (dhp_head != NULL) {
21327c478bd9Sstevel@tonic-gate 		devmap_handle_t *tdhp;
21337c478bd9Sstevel@tonic-gate 		/*
21347c478bd9Sstevel@tonic-gate 		 * If large page size was used in hat_devload(),
21357c478bd9Sstevel@tonic-gate 		 * the same page size must be used in hat_unload().
21367c478bd9Sstevel@tonic-gate 		 */
21377c478bd9Sstevel@tonic-gate 		dhp = tdhp = devmap_find_handle(dhp_head, addr);
21387c478bd9Sstevel@tonic-gate 		while (tdhp != NULL) {
21397c478bd9Sstevel@tonic-gate 			if (tdhp->dh_flags & DEVMAP_FLAG_LARGE) {
21407c478bd9Sstevel@tonic-gate 				break;
21417c478bd9Sstevel@tonic-gate 			}
21427c478bd9Sstevel@tonic-gate 			tdhp = tdhp->dh_next;
21437c478bd9Sstevel@tonic-gate 		}
21447c478bd9Sstevel@tonic-gate 		if (tdhp) {
21457c478bd9Sstevel@tonic-gate 			size_t slen = len;
21467c478bd9Sstevel@tonic-gate 			size_t mlen;
21477c478bd9Sstevel@tonic-gate 			size_t soff;
21487c478bd9Sstevel@tonic-gate 
21497c478bd9Sstevel@tonic-gate 			soff = (ulong_t)(addr - dhp->dh_uvaddr);
21507c478bd9Sstevel@tonic-gate 			while (slen != 0) {
21517c478bd9Sstevel@tonic-gate 				mlen = MIN(slen, (dhp->dh_len - soff));
21527c478bd9Sstevel@tonic-gate 				hat_unload(seg->s_as->a_hat, dhp->dh_uvaddr,
21537c478bd9Sstevel@tonic-gate 					dhp->dh_len, HAT_UNLOAD);
21547c478bd9Sstevel@tonic-gate 				dhp = dhp->dh_next;
21557c478bd9Sstevel@tonic-gate 				ASSERT(slen >= mlen);
21567c478bd9Sstevel@tonic-gate 				slen -= mlen;
21577c478bd9Sstevel@tonic-gate 				soff = 0;
21587c478bd9Sstevel@tonic-gate 			}
21597c478bd9Sstevel@tonic-gate 			return (0);
21607c478bd9Sstevel@tonic-gate 		}
21617c478bd9Sstevel@tonic-gate 	}
21627c478bd9Sstevel@tonic-gate 
21637c478bd9Sstevel@tonic-gate 	if ((prot & ~PROT_USER) == PROT_NONE) {
21647c478bd9Sstevel@tonic-gate 		hat_unload(seg->s_as->a_hat, addr, len, HAT_UNLOAD);
21657c478bd9Sstevel@tonic-gate 	} else {
21667c478bd9Sstevel@tonic-gate 		/*
21677c478bd9Sstevel@tonic-gate 		 * RFE: the segment should keep track of all attributes
21687c478bd9Sstevel@tonic-gate 		 * allowing us to remove the deprecated hat_chgprot
21697c478bd9Sstevel@tonic-gate 		 * and use hat_chgattr.
21707c478bd9Sstevel@tonic-gate 		 */
21717c478bd9Sstevel@tonic-gate 		hat_chgprot(seg->s_as->a_hat, addr, len, prot);
21727c478bd9Sstevel@tonic-gate 	}
21737c478bd9Sstevel@tonic-gate 
21747c478bd9Sstevel@tonic-gate 	return (0);
21757c478bd9Sstevel@tonic-gate }
21767c478bd9Sstevel@tonic-gate 
21777c478bd9Sstevel@tonic-gate static int
21787c478bd9Sstevel@tonic-gate segdev_checkprot(struct seg *seg, caddr_t addr, size_t len, uint_t prot)
21797c478bd9Sstevel@tonic-gate {
21807c478bd9Sstevel@tonic-gate 	struct segdev_data *sdp = (struct segdev_data *)seg->s_data;
21817c478bd9Sstevel@tonic-gate 	struct vpage *vp, *evp;
21827c478bd9Sstevel@tonic-gate 
21837c478bd9Sstevel@tonic-gate 	TRACE_4(TR_FAC_DEVMAP, TR_DEVMAP_CHECKPROT,
21847c478bd9Sstevel@tonic-gate 	    "segdev_checkprot:start seg=%p addr=%p len=%lx prot=%x",
21857c478bd9Sstevel@tonic-gate 	    (void *)seg, (void *)addr, len, prot);
21867c478bd9Sstevel@tonic-gate 	ASSERT(seg->s_as && AS_LOCK_HELD(seg->s_as, &seg->s_as->a_lock));
21877c478bd9Sstevel@tonic-gate 
21887c478bd9Sstevel@tonic-gate 	/*
21897c478bd9Sstevel@tonic-gate 	 * If segment protection can be used, simply check against them
21907c478bd9Sstevel@tonic-gate 	 */
21917c478bd9Sstevel@tonic-gate 	mutex_enter(&sdp->lock);
21927c478bd9Sstevel@tonic-gate 	if (sdp->pageprot == 0) {
21937c478bd9Sstevel@tonic-gate 		register int err;
21947c478bd9Sstevel@tonic-gate 
21957c478bd9Sstevel@tonic-gate 		err = ((sdp->prot & prot) != prot) ? EACCES : 0;
21967c478bd9Sstevel@tonic-gate 		mutex_exit(&sdp->lock);
21977c478bd9Sstevel@tonic-gate 		return (err);
21987c478bd9Sstevel@tonic-gate 	}
21997c478bd9Sstevel@tonic-gate 
22007c478bd9Sstevel@tonic-gate 	/*
22017c478bd9Sstevel@tonic-gate 	 * Have to check down to the vpage level
22027c478bd9Sstevel@tonic-gate 	 */
22037c478bd9Sstevel@tonic-gate 	evp = &sdp->vpage[seg_page(seg, addr + len)];
22047c478bd9Sstevel@tonic-gate 	for (vp = &sdp->vpage[seg_page(seg, addr)]; vp < evp; vp++) {
22057c478bd9Sstevel@tonic-gate 		if ((VPP_PROT(vp) & prot) != prot) {
22067c478bd9Sstevel@tonic-gate 			mutex_exit(&sdp->lock);
22077c478bd9Sstevel@tonic-gate 			return (EACCES);
22087c478bd9Sstevel@tonic-gate 		}
22097c478bd9Sstevel@tonic-gate 	}
22107c478bd9Sstevel@tonic-gate 	mutex_exit(&sdp->lock);
22117c478bd9Sstevel@tonic-gate 	return (0);
22127c478bd9Sstevel@tonic-gate }
22137c478bd9Sstevel@tonic-gate 
22147c478bd9Sstevel@tonic-gate static int
22157c478bd9Sstevel@tonic-gate segdev_getprot(struct seg *seg, caddr_t addr, size_t len, uint_t *protv)
22167c478bd9Sstevel@tonic-gate {
22177c478bd9Sstevel@tonic-gate 	struct segdev_data *sdp = (struct segdev_data *)seg->s_data;
22187c478bd9Sstevel@tonic-gate 	size_t pgno;
22197c478bd9Sstevel@tonic-gate 
22207c478bd9Sstevel@tonic-gate 	TRACE_4(TR_FAC_DEVMAP, TR_DEVMAP_GETPROT,
22217c478bd9Sstevel@tonic-gate 	    "segdev_getprot:start seg=%p addr=%p len=%lx protv=%p",
22227c478bd9Sstevel@tonic-gate 	    (void *)seg, (void *)addr, len, (void *)protv);
22237c478bd9Sstevel@tonic-gate 	ASSERT(seg->s_as && AS_LOCK_HELD(seg->s_as, &seg->s_as->a_lock));
22247c478bd9Sstevel@tonic-gate 
22257c478bd9Sstevel@tonic-gate 	pgno = seg_page(seg, addr + len) - seg_page(seg, addr) + 1;
22267c478bd9Sstevel@tonic-gate 	if (pgno != 0) {
22277c478bd9Sstevel@tonic-gate 		mutex_enter(&sdp->lock);
22287c478bd9Sstevel@tonic-gate 		if (sdp->pageprot == 0) {
22297c478bd9Sstevel@tonic-gate 			do
22307c478bd9Sstevel@tonic-gate 				protv[--pgno] = sdp->prot;
22317c478bd9Sstevel@tonic-gate 			while (pgno != 0);
22327c478bd9Sstevel@tonic-gate 		} else {
22337c478bd9Sstevel@tonic-gate 			size_t pgoff = seg_page(seg, addr);
22347c478bd9Sstevel@tonic-gate 
22357c478bd9Sstevel@tonic-gate 			do {
22367c478bd9Sstevel@tonic-gate 				pgno--;
22377c478bd9Sstevel@tonic-gate 				protv[pgno] =
22387c478bd9Sstevel@tonic-gate 					VPP_PROT(&sdp->vpage[pgno + pgoff]);
22397c478bd9Sstevel@tonic-gate 			} while (pgno != 0);
22407c478bd9Sstevel@tonic-gate 		}
22417c478bd9Sstevel@tonic-gate 		mutex_exit(&sdp->lock);
22427c478bd9Sstevel@tonic-gate 	}
22437c478bd9Sstevel@tonic-gate 	return (0);
22447c478bd9Sstevel@tonic-gate }
22457c478bd9Sstevel@tonic-gate 
22467c478bd9Sstevel@tonic-gate static u_offset_t
22477c478bd9Sstevel@tonic-gate segdev_getoffset(register struct seg *seg, caddr_t addr)
22487c478bd9Sstevel@tonic-gate {
22497c478bd9Sstevel@tonic-gate 	register struct segdev_data *sdp = (struct segdev_data *)seg->s_data;
22507c478bd9Sstevel@tonic-gate 
22517c478bd9Sstevel@tonic-gate 	TRACE_2(TR_FAC_DEVMAP, TR_DEVMAP_GETOFFSET,
22527c478bd9Sstevel@tonic-gate 	    "segdev_getoffset:start seg=%p addr=%p", (void *)seg, (void *)addr);
22537c478bd9Sstevel@tonic-gate 
22547c478bd9Sstevel@tonic-gate 	ASSERT(seg->s_as && AS_LOCK_HELD(seg->s_as, &seg->s_as->a_lock));
22557c478bd9Sstevel@tonic-gate 
22567c478bd9Sstevel@tonic-gate 	return ((u_offset_t)sdp->offset + (addr - seg->s_base));
22577c478bd9Sstevel@tonic-gate }
22587c478bd9Sstevel@tonic-gate 
22597c478bd9Sstevel@tonic-gate /*ARGSUSED*/
22607c478bd9Sstevel@tonic-gate static int
22617c478bd9Sstevel@tonic-gate segdev_gettype(register struct seg *seg, caddr_t addr)
22627c478bd9Sstevel@tonic-gate {
22637c478bd9Sstevel@tonic-gate 	register struct segdev_data *sdp = (struct segdev_data *)seg->s_data;
22647c478bd9Sstevel@tonic-gate 
22657c478bd9Sstevel@tonic-gate 	TRACE_2(TR_FAC_DEVMAP, TR_DEVMAP_GETTYPE,
22667c478bd9Sstevel@tonic-gate 	    "segdev_gettype:start seg=%p addr=%p", (void *)seg, (void *)addr);
22677c478bd9Sstevel@tonic-gate 
22687c478bd9Sstevel@tonic-gate 	ASSERT(seg->s_as && AS_LOCK_HELD(seg->s_as, &seg->s_as->a_lock));
22697c478bd9Sstevel@tonic-gate 
22707c478bd9Sstevel@tonic-gate 	return (sdp->type);
22717c478bd9Sstevel@tonic-gate }
22727c478bd9Sstevel@tonic-gate 
22737c478bd9Sstevel@tonic-gate 
22747c478bd9Sstevel@tonic-gate /*ARGSUSED*/
22757c478bd9Sstevel@tonic-gate static int
22767c478bd9Sstevel@tonic-gate segdev_getvp(register struct seg *seg, caddr_t addr, struct vnode **vpp)
22777c478bd9Sstevel@tonic-gate {
22787c478bd9Sstevel@tonic-gate 	register struct segdev_data *sdp = (struct segdev_data *)seg->s_data;
22797c478bd9Sstevel@tonic-gate 
22807c478bd9Sstevel@tonic-gate 	TRACE_2(TR_FAC_DEVMAP, TR_DEVMAP_GETVP,
22817c478bd9Sstevel@tonic-gate 	    "segdev_getvp:start seg=%p addr=%p", (void *)seg, (void *)addr);
22827c478bd9Sstevel@tonic-gate 
22837c478bd9Sstevel@tonic-gate 	ASSERT(seg->s_as && AS_LOCK_HELD(seg->s_as, &seg->s_as->a_lock));
22847c478bd9Sstevel@tonic-gate 
22857c478bd9Sstevel@tonic-gate 	/*
22867c478bd9Sstevel@tonic-gate 	 * Note that this vp is the common_vp of the device, where the
22877c478bd9Sstevel@tonic-gate 	 * pages are hung ..
22887c478bd9Sstevel@tonic-gate 	 */
22897c478bd9Sstevel@tonic-gate 	*vpp = VTOCVP(sdp->vp);
22907c478bd9Sstevel@tonic-gate 
22917c478bd9Sstevel@tonic-gate 	return (0);
22927c478bd9Sstevel@tonic-gate }
22937c478bd9Sstevel@tonic-gate 
22947c478bd9Sstevel@tonic-gate static void
22957c478bd9Sstevel@tonic-gate segdev_badop(void)
22967c478bd9Sstevel@tonic-gate {
22977c478bd9Sstevel@tonic-gate 	TRACE_0(TR_FAC_DEVMAP, TR_DEVMAP_SEGDEV_BADOP,
22987c478bd9Sstevel@tonic-gate 		"segdev_badop:start");
22997c478bd9Sstevel@tonic-gate 	panic("segdev_badop");
23007c478bd9Sstevel@tonic-gate 	/*NOTREACHED*/
23017c478bd9Sstevel@tonic-gate }
23027c478bd9Sstevel@tonic-gate 
23037c478bd9Sstevel@tonic-gate /*
23047c478bd9Sstevel@tonic-gate  * segdev pages are not in the cache, and thus can't really be controlled.
23057c478bd9Sstevel@tonic-gate  * Hence, syncs are simply always successful.
23067c478bd9Sstevel@tonic-gate  */
23077c478bd9Sstevel@tonic-gate /*ARGSUSED*/
23087c478bd9Sstevel@tonic-gate static int
23097c478bd9Sstevel@tonic-gate segdev_sync(struct seg *seg, caddr_t addr, size_t len, int attr, uint_t flags)
23107c478bd9Sstevel@tonic-gate {
23117c478bd9Sstevel@tonic-gate 	TRACE_0(TR_FAC_DEVMAP, TR_DEVMAP_SYNC, "segdev_sync:start");
23127c478bd9Sstevel@tonic-gate 
23137c478bd9Sstevel@tonic-gate 	ASSERT(seg->s_as && AS_LOCK_HELD(seg->s_as, &seg->s_as->a_lock));
23147c478bd9Sstevel@tonic-gate 
23157c478bd9Sstevel@tonic-gate 	return (0);
23167c478bd9Sstevel@tonic-gate }
23177c478bd9Sstevel@tonic-gate 
23187c478bd9Sstevel@tonic-gate /*
23197c478bd9Sstevel@tonic-gate  * segdev pages are always "in core".
23207c478bd9Sstevel@tonic-gate  */
23217c478bd9Sstevel@tonic-gate /*ARGSUSED*/
23227c478bd9Sstevel@tonic-gate static size_t
23237c478bd9Sstevel@tonic-gate segdev_incore(struct seg *seg, caddr_t addr, size_t len, char *vec)
23247c478bd9Sstevel@tonic-gate {
23257c478bd9Sstevel@tonic-gate 	size_t v = 0;
23267c478bd9Sstevel@tonic-gate 
23277c478bd9Sstevel@tonic-gate 	TRACE_0(TR_FAC_DEVMAP, TR_DEVMAP_INCORE, "segdev_incore:start");
23287c478bd9Sstevel@tonic-gate 
23297c478bd9Sstevel@tonic-gate 	ASSERT(seg->s_as && AS_LOCK_HELD(seg->s_as, &seg->s_as->a_lock));
23307c478bd9Sstevel@tonic-gate 
23317c478bd9Sstevel@tonic-gate 	for (len = (len + PAGEOFFSET) & PAGEMASK; len; len -= PAGESIZE,
23327c478bd9Sstevel@tonic-gate 	    v += PAGESIZE)
23337c478bd9Sstevel@tonic-gate 		*vec++ = 1;
23347c478bd9Sstevel@tonic-gate 	return (v);
23357c478bd9Sstevel@tonic-gate }
23367c478bd9Sstevel@tonic-gate 
23377c478bd9Sstevel@tonic-gate /*
23387c478bd9Sstevel@tonic-gate  * segdev pages are not in the cache, and thus can't really be controlled.
23397c478bd9Sstevel@tonic-gate  * Hence, locks are simply always successful.
23407c478bd9Sstevel@tonic-gate  */
23417c478bd9Sstevel@tonic-gate /*ARGSUSED*/
23427c478bd9Sstevel@tonic-gate static int
23437c478bd9Sstevel@tonic-gate segdev_lockop(struct seg *seg, caddr_t addr,
23447c478bd9Sstevel@tonic-gate     size_t len, int attr, int op, ulong_t *lockmap, size_t pos)
23457c478bd9Sstevel@tonic-gate {
23467c478bd9Sstevel@tonic-gate 	TRACE_0(TR_FAC_DEVMAP, TR_DEVMAP_LOCKOP, "segdev_lockop:start");
23477c478bd9Sstevel@tonic-gate 
23487c478bd9Sstevel@tonic-gate 	ASSERT(seg->s_as && AS_LOCK_HELD(seg->s_as, &seg->s_as->a_lock));
23497c478bd9Sstevel@tonic-gate 
23507c478bd9Sstevel@tonic-gate 	return (0);
23517c478bd9Sstevel@tonic-gate }
23527c478bd9Sstevel@tonic-gate 
23537c478bd9Sstevel@tonic-gate /*
23547c478bd9Sstevel@tonic-gate  * segdev pages are not in the cache, and thus can't really be controlled.
23557c478bd9Sstevel@tonic-gate  * Hence, advise is simply always successful.
23567c478bd9Sstevel@tonic-gate  */
23577c478bd9Sstevel@tonic-gate /*ARGSUSED*/
23587c478bd9Sstevel@tonic-gate static int
23597c478bd9Sstevel@tonic-gate segdev_advise(struct seg *seg, caddr_t addr, size_t len, uint_t behav)
23607c478bd9Sstevel@tonic-gate {
23617c478bd9Sstevel@tonic-gate 	TRACE_0(TR_FAC_DEVMAP, TR_DEVMAP_ADVISE, "segdev_advise:start");
23627c478bd9Sstevel@tonic-gate 
23637c478bd9Sstevel@tonic-gate 	ASSERT(seg->s_as && AS_LOCK_HELD(seg->s_as, &seg->s_as->a_lock));
23647c478bd9Sstevel@tonic-gate 
23657c478bd9Sstevel@tonic-gate 	return (0);
23667c478bd9Sstevel@tonic-gate }
23677c478bd9Sstevel@tonic-gate 
23687c478bd9Sstevel@tonic-gate /*
23697c478bd9Sstevel@tonic-gate  * segdev pages are not dumped, so we just return
23707c478bd9Sstevel@tonic-gate  */
23717c478bd9Sstevel@tonic-gate /*ARGSUSED*/
23727c478bd9Sstevel@tonic-gate static void
23737c478bd9Sstevel@tonic-gate segdev_dump(struct seg *seg)
23747c478bd9Sstevel@tonic-gate {}
23757c478bd9Sstevel@tonic-gate 
23767c478bd9Sstevel@tonic-gate /*
23777c478bd9Sstevel@tonic-gate  * ddi_segmap_setup:	Used by drivers who wish specify mapping attributes
23787c478bd9Sstevel@tonic-gate  *			for a segment.	Called from a drivers segmap(9E)
23797c478bd9Sstevel@tonic-gate  *			routine.
23807c478bd9Sstevel@tonic-gate  */
23817c478bd9Sstevel@tonic-gate /*ARGSUSED*/
23827c478bd9Sstevel@tonic-gate int
23837c478bd9Sstevel@tonic-gate ddi_segmap_setup(dev_t dev, off_t offset, struct as *as, caddr_t *addrp,
23847c478bd9Sstevel@tonic-gate     off_t len, uint_t prot, uint_t maxprot, uint_t flags, cred_t *cred,
23857c478bd9Sstevel@tonic-gate     ddi_device_acc_attr_t *accattrp, uint_t rnumber)
23867c478bd9Sstevel@tonic-gate {
23877c478bd9Sstevel@tonic-gate 	struct segdev_crargs dev_a;
23887c478bd9Sstevel@tonic-gate 	int (*mapfunc)(dev_t dev, off_t off, int prot);
23897c478bd9Sstevel@tonic-gate 	uint_t hat_attr;
23907c478bd9Sstevel@tonic-gate 	pfn_t pfn;
23917c478bd9Sstevel@tonic-gate 	int	error, i;
23927c478bd9Sstevel@tonic-gate 
23937c478bd9Sstevel@tonic-gate 	TRACE_0(TR_FAC_DEVMAP, TR_DEVMAP_SEGMAP_SETUP,
23947c478bd9Sstevel@tonic-gate 	    "ddi_segmap_setup:start");
23957c478bd9Sstevel@tonic-gate 
23967c478bd9Sstevel@tonic-gate 	if ((mapfunc = devopsp[getmajor(dev)]->devo_cb_ops->cb_mmap) == nodev)
23977c478bd9Sstevel@tonic-gate 		return (ENODEV);
23987c478bd9Sstevel@tonic-gate 
23997c478bd9Sstevel@tonic-gate 	/*
24007c478bd9Sstevel@tonic-gate 	 * Character devices that support the d_mmap
24017c478bd9Sstevel@tonic-gate 	 * interface can only be mmap'ed shared.
24027c478bd9Sstevel@tonic-gate 	 */
24037c478bd9Sstevel@tonic-gate 	if ((flags & MAP_TYPE) != MAP_SHARED)
24047c478bd9Sstevel@tonic-gate 		return (EINVAL);
24057c478bd9Sstevel@tonic-gate 
24067c478bd9Sstevel@tonic-gate 	/*
24077c478bd9Sstevel@tonic-gate 	 * Check that this region is indeed mappable on this platform.
24087c478bd9Sstevel@tonic-gate 	 * Use the mapping function.
24097c478bd9Sstevel@tonic-gate 	 */
24107c478bd9Sstevel@tonic-gate 	if (ddi_device_mapping_check(dev, accattrp, rnumber, &hat_attr) == -1)
24117c478bd9Sstevel@tonic-gate 		return (ENXIO);
24127c478bd9Sstevel@tonic-gate 
24137c478bd9Sstevel@tonic-gate 	/*
24147c478bd9Sstevel@tonic-gate 	 * Check to ensure that the entire range is
24157c478bd9Sstevel@tonic-gate 	 * legal and we are not trying to map in
24167c478bd9Sstevel@tonic-gate 	 * more than the device will let us.
24177c478bd9Sstevel@tonic-gate 	 */
24187c478bd9Sstevel@tonic-gate 	for (i = 0; i < len; i += PAGESIZE) {
24197c478bd9Sstevel@tonic-gate 		if (i == 0) {
24207c478bd9Sstevel@tonic-gate 			/*
24217c478bd9Sstevel@tonic-gate 			 * Save the pfn at offset here. This pfn will be
24227c478bd9Sstevel@tonic-gate 			 * used later to get user address.
24237c478bd9Sstevel@tonic-gate 			 */
24247c478bd9Sstevel@tonic-gate 			if ((pfn = (pfn_t)cdev_mmap(mapfunc, dev, offset,
24257c478bd9Sstevel@tonic-gate 				maxprot)) == PFN_INVALID)
24267c478bd9Sstevel@tonic-gate 				return (ENXIO);
24277c478bd9Sstevel@tonic-gate 		} else {
24287c478bd9Sstevel@tonic-gate 			if (cdev_mmap(mapfunc, dev, offset + i, maxprot) ==
24297c478bd9Sstevel@tonic-gate 				PFN_INVALID)
24307c478bd9Sstevel@tonic-gate 				return (ENXIO);
24317c478bd9Sstevel@tonic-gate 		}
24327c478bd9Sstevel@tonic-gate 	}
24337c478bd9Sstevel@tonic-gate 
24347c478bd9Sstevel@tonic-gate 	as_rangelock(as);
24357c478bd9Sstevel@tonic-gate 	if ((flags & MAP_FIXED) == 0) {
24367c478bd9Sstevel@tonic-gate 		/*
24377c478bd9Sstevel@tonic-gate 		 * Pick an address w/o worrying about
24387c478bd9Sstevel@tonic-gate 		 * any vac alignment constraints.
24397c478bd9Sstevel@tonic-gate 		 */
24407c478bd9Sstevel@tonic-gate 		map_addr(addrp, len, ptob(pfn), 0, flags);
24417c478bd9Sstevel@tonic-gate 		if (*addrp == NULL) {
24427c478bd9Sstevel@tonic-gate 			as_rangeunlock(as);
24437c478bd9Sstevel@tonic-gate 			return (ENOMEM);
24447c478bd9Sstevel@tonic-gate 		}
24457c478bd9Sstevel@tonic-gate 	} else {
24467c478bd9Sstevel@tonic-gate 		/*
24477c478bd9Sstevel@tonic-gate 		 * User-specified address; blow away any previous mappings.
24487c478bd9Sstevel@tonic-gate 		 */
24497c478bd9Sstevel@tonic-gate 		(void) as_unmap(as, *addrp, len);
24507c478bd9Sstevel@tonic-gate 	}
24517c478bd9Sstevel@tonic-gate 
24527c478bd9Sstevel@tonic-gate 	dev_a.mapfunc = mapfunc;
24537c478bd9Sstevel@tonic-gate 	dev_a.dev = dev;
24547c478bd9Sstevel@tonic-gate 	dev_a.offset = (offset_t)offset;
24557c478bd9Sstevel@tonic-gate 	dev_a.type = flags & MAP_TYPE;
24567c478bd9Sstevel@tonic-gate 	dev_a.prot = (uchar_t)prot;
24577c478bd9Sstevel@tonic-gate 	dev_a.maxprot = (uchar_t)maxprot;
24587c478bd9Sstevel@tonic-gate 	dev_a.hat_attr = hat_attr;
24597c478bd9Sstevel@tonic-gate 	dev_a.hat_flags = 0;
24607c478bd9Sstevel@tonic-gate 	dev_a.devmap_data = NULL;
24617c478bd9Sstevel@tonic-gate 
24627c478bd9Sstevel@tonic-gate 	error = as_map(as, *addrp, len, segdev_create, &dev_a);
24637c478bd9Sstevel@tonic-gate 	as_rangeunlock(as);
24647c478bd9Sstevel@tonic-gate 	return (error);
24657c478bd9Sstevel@tonic-gate 
24667c478bd9Sstevel@tonic-gate }
24677c478bd9Sstevel@tonic-gate 
24687c478bd9Sstevel@tonic-gate /*ARGSUSED*/
24697c478bd9Sstevel@tonic-gate static int
24707c478bd9Sstevel@tonic-gate segdev_pagelock(struct seg *seg, caddr_t addr, size_t len,
24717c478bd9Sstevel@tonic-gate     struct page ***ppp, enum lock_type type, enum seg_rw rw)
24727c478bd9Sstevel@tonic-gate {
24737c478bd9Sstevel@tonic-gate 	TRACE_0(TR_FAC_DEVMAP, TR_DEVMAP_PAGELOCK,
24747c478bd9Sstevel@tonic-gate 	    "segdev_pagelock:start");
24757c478bd9Sstevel@tonic-gate 	return (ENOTSUP);
24767c478bd9Sstevel@tonic-gate }
24777c478bd9Sstevel@tonic-gate 
24787c478bd9Sstevel@tonic-gate /*ARGSUSED*/
24797c478bd9Sstevel@tonic-gate static int
24807c478bd9Sstevel@tonic-gate segdev_setpagesize(struct seg *seg, caddr_t addr, size_t len,
24817c478bd9Sstevel@tonic-gate     uint_t szc)
24827c478bd9Sstevel@tonic-gate {
24837c478bd9Sstevel@tonic-gate 	return (ENOTSUP);
24847c478bd9Sstevel@tonic-gate }
24857c478bd9Sstevel@tonic-gate 
24867c478bd9Sstevel@tonic-gate /*
24877c478bd9Sstevel@tonic-gate  * devmap_device: Used by devmap framework to establish mapping
24887c478bd9Sstevel@tonic-gate  *                called by devmap_seup(9F) during map setup time.
24897c478bd9Sstevel@tonic-gate  */
24907c478bd9Sstevel@tonic-gate /*ARGSUSED*/
24917c478bd9Sstevel@tonic-gate static int
24927c478bd9Sstevel@tonic-gate devmap_device(devmap_handle_t *dhp, struct as *as, caddr_t *addr,
24937c478bd9Sstevel@tonic-gate     offset_t off, size_t len, uint_t flags)
24947c478bd9Sstevel@tonic-gate {
24957c478bd9Sstevel@tonic-gate 	devmap_handle_t *rdhp, *maxdhp;
24967c478bd9Sstevel@tonic-gate 	struct segdev_crargs dev_a;
24977c478bd9Sstevel@tonic-gate 	int	err;
24987c478bd9Sstevel@tonic-gate 	uint_t maxprot = PROT_ALL;
24997c478bd9Sstevel@tonic-gate 	offset_t offset = 0;
25007c478bd9Sstevel@tonic-gate 	pfn_t pfn;
25017c478bd9Sstevel@tonic-gate 	struct devmap_pmem_cookie *pcp;
25027c478bd9Sstevel@tonic-gate 
25037c478bd9Sstevel@tonic-gate 	TRACE_4(TR_FAC_DEVMAP, TR_DEVMAP_DEVICE,
25047c478bd9Sstevel@tonic-gate 	    "devmap_device:start dhp=%p addr=%p off=%llx, len=%lx",
25057c478bd9Sstevel@tonic-gate 	    (void *)dhp, (void *)addr, off, len);
25067c478bd9Sstevel@tonic-gate 
25077c478bd9Sstevel@tonic-gate 	DEBUGF(2, (CE_CONT, "devmap_device: dhp %p addr %p off %llx len %lx\n",
25087c478bd9Sstevel@tonic-gate 	    (void *)dhp, (void *)addr, off, len));
25097c478bd9Sstevel@tonic-gate 
25107c478bd9Sstevel@tonic-gate 	as_rangelock(as);
25117c478bd9Sstevel@tonic-gate 	if ((flags & MAP_FIXED) == 0) {
25127c478bd9Sstevel@tonic-gate 		offset_t aligned_off;
25137c478bd9Sstevel@tonic-gate 
25147c478bd9Sstevel@tonic-gate 		rdhp = maxdhp = dhp;
25157c478bd9Sstevel@tonic-gate 		while (rdhp != NULL) {
25167c478bd9Sstevel@tonic-gate 			maxdhp = (maxdhp->dh_len > rdhp->dh_len) ?
25177c478bd9Sstevel@tonic-gate 				maxdhp : rdhp;
25187c478bd9Sstevel@tonic-gate 			rdhp = rdhp->dh_next;
25197c478bd9Sstevel@tonic-gate 			maxprot |= dhp->dh_maxprot;
25207c478bd9Sstevel@tonic-gate 		}
25217c478bd9Sstevel@tonic-gate 		offset = maxdhp->dh_uoff - dhp->dh_uoff;
25227c478bd9Sstevel@tonic-gate 
25237c478bd9Sstevel@tonic-gate 		/*
25247c478bd9Sstevel@tonic-gate 		 * Use the dhp that has the
25257c478bd9Sstevel@tonic-gate 		 * largest len to get user address.
25267c478bd9Sstevel@tonic-gate 		 */
25277c478bd9Sstevel@tonic-gate 		/*
25287c478bd9Sstevel@tonic-gate 		 * If MAPPING_INVALID, cannot use dh_pfn/dh_cvaddr,
25297c478bd9Sstevel@tonic-gate 		 * use 0 which is as good as any other.
25307c478bd9Sstevel@tonic-gate 		 */
25317c478bd9Sstevel@tonic-gate 		if (maxdhp->dh_flags & DEVMAP_MAPPING_INVALID) {
25327c478bd9Sstevel@tonic-gate 			aligned_off = (offset_t)0;
25337c478bd9Sstevel@tonic-gate 		} else if (dhp_is_devmem(maxdhp)) {
25347c478bd9Sstevel@tonic-gate 			aligned_off = (offset_t)ptob(maxdhp->dh_pfn) - offset;
25357c478bd9Sstevel@tonic-gate 		} else if (dhp_is_pmem(maxdhp)) {
25367c478bd9Sstevel@tonic-gate 			pcp = (struct devmap_pmem_cookie *)maxdhp->dh_pcookie;
25377c478bd9Sstevel@tonic-gate 			pfn = page_pptonum(
25387c478bd9Sstevel@tonic-gate 			    pcp->dp_pparray[btop(maxdhp->dh_roff)]);
25397c478bd9Sstevel@tonic-gate 			aligned_off = (offset_t)ptob(pfn) - offset;
25407c478bd9Sstevel@tonic-gate 		} else {
25417c478bd9Sstevel@tonic-gate 			aligned_off = (offset_t)(uintptr_t)maxdhp->dh_cvaddr -
25427c478bd9Sstevel@tonic-gate 			    offset;
25437c478bd9Sstevel@tonic-gate 		}
25447c478bd9Sstevel@tonic-gate 
25457c478bd9Sstevel@tonic-gate 		/*
25467c478bd9Sstevel@tonic-gate 		 * Pick an address aligned to dh_cookie.
25477c478bd9Sstevel@tonic-gate 		 * for kernel memory/user memory, cookie is cvaddr.
25487c478bd9Sstevel@tonic-gate 		 * for device memory, cookie is physical address.
25497c478bd9Sstevel@tonic-gate 		 */
25507c478bd9Sstevel@tonic-gate 		map_addr(addr, len, aligned_off, 1, flags);
25517c478bd9Sstevel@tonic-gate 		if (*addr == NULL) {
25527c478bd9Sstevel@tonic-gate 			as_rangeunlock(as);
25537c478bd9Sstevel@tonic-gate 			return (ENOMEM);
25547c478bd9Sstevel@tonic-gate 		}
25557c478bd9Sstevel@tonic-gate 	} else {
25567c478bd9Sstevel@tonic-gate 		/*
25577c478bd9Sstevel@tonic-gate 		 * User-specified address; blow away any previous mappings.
25587c478bd9Sstevel@tonic-gate 		 */
25597c478bd9Sstevel@tonic-gate 		(void) as_unmap(as, *addr, len);
25607c478bd9Sstevel@tonic-gate 	}
25617c478bd9Sstevel@tonic-gate 
25627c478bd9Sstevel@tonic-gate 	dev_a.mapfunc = NULL;
25637c478bd9Sstevel@tonic-gate 	dev_a.dev = dhp->dh_dev;
25647c478bd9Sstevel@tonic-gate 	dev_a.type = flags & MAP_TYPE;
25657c478bd9Sstevel@tonic-gate 	dev_a.offset = off;
25667c478bd9Sstevel@tonic-gate 	/*
25677c478bd9Sstevel@tonic-gate 	 * sdp->maxprot has the least restrict protection of all dhps.
25687c478bd9Sstevel@tonic-gate 	 */
25697c478bd9Sstevel@tonic-gate 	dev_a.maxprot = maxprot;
25707c478bd9Sstevel@tonic-gate 	dev_a.prot = dhp->dh_prot;
25717c478bd9Sstevel@tonic-gate 	/*
25727c478bd9Sstevel@tonic-gate 	 * devmap uses dhp->dh_hat_attr for hat.
25737c478bd9Sstevel@tonic-gate 	 */
25747c478bd9Sstevel@tonic-gate 	dev_a.hat_flags = 0;
25757c478bd9Sstevel@tonic-gate 	dev_a.hat_attr = 0;
25767c478bd9Sstevel@tonic-gate 	dev_a.devmap_data = (void *)dhp;
25777c478bd9Sstevel@tonic-gate 
25787c478bd9Sstevel@tonic-gate 	err = as_map(as, *addr, len, segdev_create, &dev_a);
25797c478bd9Sstevel@tonic-gate 	as_rangeunlock(as);
25807c478bd9Sstevel@tonic-gate 	return (err);
25817c478bd9Sstevel@tonic-gate }
25827c478bd9Sstevel@tonic-gate 
25837c478bd9Sstevel@tonic-gate int
25847c478bd9Sstevel@tonic-gate devmap_do_ctxmgt(devmap_cookie_t dhc, void *pvtp, offset_t off, size_t len,
25857c478bd9Sstevel@tonic-gate     uint_t type, uint_t rw, int (*ctxmgt)(devmap_cookie_t, void *, offset_t,
25867c478bd9Sstevel@tonic-gate     size_t, uint_t, uint_t))
25877c478bd9Sstevel@tonic-gate {
25887c478bd9Sstevel@tonic-gate 	register devmap_handle_t *dhp = (devmap_handle_t *)dhc;
25897c478bd9Sstevel@tonic-gate 	struct devmap_ctx *devctx;
25907c478bd9Sstevel@tonic-gate 	int do_timeout = 0;
25917c478bd9Sstevel@tonic-gate 	int ret;
25927c478bd9Sstevel@tonic-gate 
25937c478bd9Sstevel@tonic-gate #ifdef lint
25947c478bd9Sstevel@tonic-gate 	pvtp = pvtp;
25957c478bd9Sstevel@tonic-gate #endif
25967c478bd9Sstevel@tonic-gate 
25977c478bd9Sstevel@tonic-gate 	TRACE_3(TR_FAC_DEVMAP, TR_DEVMAP_DO_CTXMGT,
25987c478bd9Sstevel@tonic-gate 	    "devmap_do_ctxmgt:start dhp=%p off=%llx, len=%lx",
25997c478bd9Sstevel@tonic-gate 	    (void *)dhp, off, len);
26007c478bd9Sstevel@tonic-gate 	DEBUGF(7, (CE_CONT, "devmap_do_ctxmgt: dhp %p off %llx len %lx\n",
26017c478bd9Sstevel@tonic-gate 	    (void *)dhp, off, len));
26027c478bd9Sstevel@tonic-gate 
26037c478bd9Sstevel@tonic-gate 	if (ctxmgt == NULL)
26047c478bd9Sstevel@tonic-gate 		return (FC_HWERR);
26057c478bd9Sstevel@tonic-gate 
26067c478bd9Sstevel@tonic-gate 	devctx = dhp->dh_ctx;
26077c478bd9Sstevel@tonic-gate 
26087c478bd9Sstevel@tonic-gate 	/*
26097c478bd9Sstevel@tonic-gate 	 * If we are on an MP system with more than one cpu running
26107c478bd9Sstevel@tonic-gate 	 * and if a thread on some CPU already has the context, wait
26117c478bd9Sstevel@tonic-gate 	 * for it to finish if there is a hysteresis timeout.
26127c478bd9Sstevel@tonic-gate 	 *
26137c478bd9Sstevel@tonic-gate 	 * We call cv_wait() instead of cv_wait_sig() because
26147c478bd9Sstevel@tonic-gate 	 * it does not matter much if it returned due to a signal
26157c478bd9Sstevel@tonic-gate 	 * or due to a cv_signal() or cv_broadcast().  In either event
26167c478bd9Sstevel@tonic-gate 	 * we need to complete the mapping otherwise the processes
26177c478bd9Sstevel@tonic-gate 	 * will die with a SEGV.
26187c478bd9Sstevel@tonic-gate 	 */
26197c478bd9Sstevel@tonic-gate 	if ((dhp->dh_timeout_length > 0) && (ncpus > 1)) {
26207c478bd9Sstevel@tonic-gate 		TRACE_2(TR_FAC_DEVMAP, TR_DEVMAP_DO_CTXMGT_CK1,
26217c478bd9Sstevel@tonic-gate 		    "devmap_do_ctxmgt:doing hysteresis, devctl %p dhp %p",
26227c478bd9Sstevel@tonic-gate 		    devctx, dhp);
26237c478bd9Sstevel@tonic-gate 		do_timeout = 1;
26247c478bd9Sstevel@tonic-gate 		mutex_enter(&devctx->lock);
26257c478bd9Sstevel@tonic-gate 		while (devctx->oncpu)
26267c478bd9Sstevel@tonic-gate 			cv_wait(&devctx->cv, &devctx->lock);
26277c478bd9Sstevel@tonic-gate 		devctx->oncpu = 1;
26287c478bd9Sstevel@tonic-gate 		mutex_exit(&devctx->lock);
26297c478bd9Sstevel@tonic-gate 	}
26307c478bd9Sstevel@tonic-gate 
26317c478bd9Sstevel@tonic-gate 	/*
26327c478bd9Sstevel@tonic-gate 	 * Call the contextmgt callback so that the driver can handle
26337c478bd9Sstevel@tonic-gate 	 * the fault.
26347c478bd9Sstevel@tonic-gate 	 */
26357c478bd9Sstevel@tonic-gate 	ret = (*ctxmgt)(dhp, dhp->dh_pvtp, off, len, type, rw);
26367c478bd9Sstevel@tonic-gate 
26377c478bd9Sstevel@tonic-gate 	/*
26387c478bd9Sstevel@tonic-gate 	 * If devmap_access() returned -1, then there was a hardware
26397c478bd9Sstevel@tonic-gate 	 * error so we need to convert the return value to something
26407c478bd9Sstevel@tonic-gate 	 * that trap() will understand.  Otherwise, the return value
26417c478bd9Sstevel@tonic-gate 	 * is already a fault code generated by devmap_unload()
26427c478bd9Sstevel@tonic-gate 	 * or devmap_load().
26437c478bd9Sstevel@tonic-gate 	 */
26447c478bd9Sstevel@tonic-gate 	if (ret) {
26457c478bd9Sstevel@tonic-gate 		TRACE_3(TR_FAC_DEVMAP, TR_DEVMAP_DO_CTXMGT_CK2,
26467c478bd9Sstevel@tonic-gate 		    "devmap_do_ctxmgt: ret=%x dhp=%p devctx=%p",
26477c478bd9Sstevel@tonic-gate 		    ret, dhp, devctx);
26487c478bd9Sstevel@tonic-gate 		DEBUGF(1, (CE_CONT, "devmap_do_ctxmgt: ret %x dhp %p\n",
26497c478bd9Sstevel@tonic-gate 		    ret, (void *)dhp));
26507c478bd9Sstevel@tonic-gate 		if (devctx->oncpu) {
26517c478bd9Sstevel@tonic-gate 			mutex_enter(&devctx->lock);
26527c478bd9Sstevel@tonic-gate 			devctx->oncpu = 0;
26537c478bd9Sstevel@tonic-gate 			cv_signal(&devctx->cv);
26547c478bd9Sstevel@tonic-gate 			mutex_exit(&devctx->lock);
26557c478bd9Sstevel@tonic-gate 		}
26567c478bd9Sstevel@tonic-gate 		return (FC_HWERR);
26577c478bd9Sstevel@tonic-gate 	}
26587c478bd9Sstevel@tonic-gate 
26597c478bd9Sstevel@tonic-gate 	/*
26607c478bd9Sstevel@tonic-gate 	 * Setup the timeout if we need to
26617c478bd9Sstevel@tonic-gate 	 */
26627c478bd9Sstevel@tonic-gate 	if (do_timeout) {
26637c478bd9Sstevel@tonic-gate 		mutex_enter(&devctx->lock);
26647c478bd9Sstevel@tonic-gate 		if (dhp->dh_timeout_length > 0) {
26657c478bd9Sstevel@tonic-gate 			TRACE_0(TR_FAC_DEVMAP, TR_DEVMAP_DO_CTXMGT_CK3,
26667c478bd9Sstevel@tonic-gate 			    "devmap_do_ctxmgt:timeout set");
26677c478bd9Sstevel@tonic-gate 			devctx->timeout = timeout(devmap_ctxto,
26687c478bd9Sstevel@tonic-gate 			    devctx, dhp->dh_timeout_length);
26697c478bd9Sstevel@tonic-gate 		} else {
26707c478bd9Sstevel@tonic-gate 			/*
26717c478bd9Sstevel@tonic-gate 			 * We don't want to wait so set oncpu to
26727c478bd9Sstevel@tonic-gate 			 * 0 and wake up anyone waiting.
26737c478bd9Sstevel@tonic-gate 			 */
26747c478bd9Sstevel@tonic-gate 			TRACE_0(TR_FAC_DEVMAP, TR_DEVMAP_DO_CTXMGT_CK4,
26757c478bd9Sstevel@tonic-gate 			    "devmap_do_ctxmgt:timeout not set");
26767c478bd9Sstevel@tonic-gate 			devctx->oncpu = 0;
26777c478bd9Sstevel@tonic-gate 			cv_signal(&devctx->cv);
26787c478bd9Sstevel@tonic-gate 		}
26797c478bd9Sstevel@tonic-gate 		mutex_exit(&devctx->lock);
26807c478bd9Sstevel@tonic-gate 	}
26817c478bd9Sstevel@tonic-gate 
26827c478bd9Sstevel@tonic-gate 	return (DDI_SUCCESS);
26837c478bd9Sstevel@tonic-gate }
26847c478bd9Sstevel@tonic-gate 
26857c478bd9Sstevel@tonic-gate /*
26867c478bd9Sstevel@tonic-gate  *                                       end of mapping
26877c478bd9Sstevel@tonic-gate  *                    poff   fault_offset         |
26887c478bd9Sstevel@tonic-gate  *            base     |        |                 |
26897c478bd9Sstevel@tonic-gate  *              |      |        |                 |
26907c478bd9Sstevel@tonic-gate  *              V      V        V                 V
26917c478bd9Sstevel@tonic-gate  *  +-----------+---------------+-------+---------+-------+
26927c478bd9Sstevel@tonic-gate  *              ^               ^       ^         ^
26937c478bd9Sstevel@tonic-gate  *              |<--- offset--->|<-len->|         |
26947c478bd9Sstevel@tonic-gate  *              |<--- dh_len(size of mapping) --->|
26957c478bd9Sstevel@tonic-gate  *                     |<--  pg -->|
26967c478bd9Sstevel@tonic-gate  *                              -->|rlen|<--
26977c478bd9Sstevel@tonic-gate  */
26987c478bd9Sstevel@tonic-gate static ulong_t
26997c478bd9Sstevel@tonic-gate devmap_roundup(devmap_handle_t *dhp, ulong_t offset, size_t len,
27007c478bd9Sstevel@tonic-gate     ulong_t *opfn, ulong_t *pagesize)
27017c478bd9Sstevel@tonic-gate {
27027c478bd9Sstevel@tonic-gate 	register int level;
27037c478bd9Sstevel@tonic-gate 	ulong_t pg;
27047c478bd9Sstevel@tonic-gate 	ulong_t poff;
27057c478bd9Sstevel@tonic-gate 	ulong_t base;
27067c478bd9Sstevel@tonic-gate 	caddr_t uvaddr;
27077c478bd9Sstevel@tonic-gate 	long rlen;
27087c478bd9Sstevel@tonic-gate 
27097c478bd9Sstevel@tonic-gate 	TRACE_3(TR_FAC_DEVMAP, TR_DEVMAP_ROUNDUP,
27107c478bd9Sstevel@tonic-gate 	    "devmap_roundup:start dhp=%p off=%lx len=%lx",
27117c478bd9Sstevel@tonic-gate 	    (void *)dhp, offset, len);
27127c478bd9Sstevel@tonic-gate 	DEBUGF(2, (CE_CONT, "devmap_roundup: dhp %p off %lx len %lx\n",
27137c478bd9Sstevel@tonic-gate 	    (void *)dhp, offset, len));
27147c478bd9Sstevel@tonic-gate 
27157c478bd9Sstevel@tonic-gate 	/*
27167c478bd9Sstevel@tonic-gate 	 * get the max. pagesize that is aligned within the range
27177c478bd9Sstevel@tonic-gate 	 * <dh_pfn, dh_pfn+offset>.
27187c478bd9Sstevel@tonic-gate 	 *
27197c478bd9Sstevel@tonic-gate 	 * The calculations below use physical address to ddetermine
27207c478bd9Sstevel@tonic-gate 	 * the page size to use. The same calculations can use the
27217c478bd9Sstevel@tonic-gate 	 * virtual address to determine the page size.
27227c478bd9Sstevel@tonic-gate 	 */
27237c478bd9Sstevel@tonic-gate 	base = (ulong_t)ptob(dhp->dh_pfn);
27247c478bd9Sstevel@tonic-gate 	for (level = dhp->dh_mmulevel; level >= 0; level--) {
27257c478bd9Sstevel@tonic-gate 		pg = page_get_pagesize(level);
27267c478bd9Sstevel@tonic-gate 		poff = ((base + offset) & ~(pg - 1));
27277c478bd9Sstevel@tonic-gate 		uvaddr = dhp->dh_uvaddr + (poff - base);
27287c478bd9Sstevel@tonic-gate 		if ((poff >= base) &&
27297c478bd9Sstevel@tonic-gate 		    ((poff + pg) <= (base + dhp->dh_len)) &&
27307c478bd9Sstevel@tonic-gate 		    VA_PA_ALIGNED((uintptr_t)uvaddr, poff, pg))
27317c478bd9Sstevel@tonic-gate 			break;
27327c478bd9Sstevel@tonic-gate 	}
27337c478bd9Sstevel@tonic-gate 
27347c478bd9Sstevel@tonic-gate 	TRACE_3(TR_FAC_DEVMAP, TR_DEVMAP_ROUNDUP_CK1,
27357c478bd9Sstevel@tonic-gate 	    "devmap_roundup: base=%lx poff=%lx dhp=%p",
27367c478bd9Sstevel@tonic-gate 	    base, poff, dhp);
27377c478bd9Sstevel@tonic-gate 	DEBUGF(2, (CE_CONT, "devmap_roundup: base %lx poff %lx pfn %lx\n",
27387c478bd9Sstevel@tonic-gate 	    base, poff, dhp->dh_pfn));
27397c478bd9Sstevel@tonic-gate 
27407c478bd9Sstevel@tonic-gate 	ASSERT(VA_PA_ALIGNED((uintptr_t)uvaddr, poff, pg));
27417c478bd9Sstevel@tonic-gate 	ASSERT(level >= 0);
27427c478bd9Sstevel@tonic-gate 
27437c478bd9Sstevel@tonic-gate 	*pagesize = pg;
27447c478bd9Sstevel@tonic-gate 	*opfn = dhp->dh_pfn + btop(poff - base);
27457c478bd9Sstevel@tonic-gate 
27467c478bd9Sstevel@tonic-gate 	rlen = len + offset - (poff - base + pg);
27477c478bd9Sstevel@tonic-gate 
27487c478bd9Sstevel@tonic-gate 	ASSERT(rlen < (long)len);
27497c478bd9Sstevel@tonic-gate 
27507c478bd9Sstevel@tonic-gate 	TRACE_5(TR_FAC_DEVMAP, TR_DEVMAP_ROUNDUP_CK2,
27517c478bd9Sstevel@tonic-gate 	    "devmap_roundup:ret dhp=%p level=%x rlen=%lx psiz=%p opfn=%p",
27527c478bd9Sstevel@tonic-gate 	    (void *)dhp, level, rlen, pagesize, opfn);
27537c478bd9Sstevel@tonic-gate 	DEBUGF(1, (CE_CONT, "devmap_roundup: dhp %p "
27547c478bd9Sstevel@tonic-gate 	    "level %x rlen %lx psize %lx opfn %lx\n",
27557c478bd9Sstevel@tonic-gate 	    (void *)dhp, level, rlen, *pagesize, *opfn));
27567c478bd9Sstevel@tonic-gate 
27577c478bd9Sstevel@tonic-gate 	return ((ulong_t)((rlen > 0) ? rlen : 0));
27587c478bd9Sstevel@tonic-gate }
27597c478bd9Sstevel@tonic-gate 
27607c478bd9Sstevel@tonic-gate /*
27617c478bd9Sstevel@tonic-gate  * find the dhp that contains addr.
27627c478bd9Sstevel@tonic-gate  */
27637c478bd9Sstevel@tonic-gate static devmap_handle_t *
27647c478bd9Sstevel@tonic-gate devmap_find_handle(devmap_handle_t *dhp_head, caddr_t addr)
27657c478bd9Sstevel@tonic-gate {
27667c478bd9Sstevel@tonic-gate 	devmap_handle_t *dhp;
27677c478bd9Sstevel@tonic-gate 
27687c478bd9Sstevel@tonic-gate 	TRACE_0(TR_FAC_DEVMAP, TR_DEVMAP_FIND_HANDLE,
27697c478bd9Sstevel@tonic-gate 	    "devmap_find_handle:start");
27707c478bd9Sstevel@tonic-gate 
27717c478bd9Sstevel@tonic-gate 	dhp = dhp_head;
27727c478bd9Sstevel@tonic-gate 	while (dhp) {
27737c478bd9Sstevel@tonic-gate 		if (addr >= dhp->dh_uvaddr &&
27747c478bd9Sstevel@tonic-gate 		    addr < (dhp->dh_uvaddr + dhp->dh_len))
27757c478bd9Sstevel@tonic-gate 			return (dhp);
27767c478bd9Sstevel@tonic-gate 		dhp = dhp->dh_next;
27777c478bd9Sstevel@tonic-gate 	}
27787c478bd9Sstevel@tonic-gate 
27797c478bd9Sstevel@tonic-gate 	return ((devmap_handle_t *)NULL);
27807c478bd9Sstevel@tonic-gate }
27817c478bd9Sstevel@tonic-gate 
27827c478bd9Sstevel@tonic-gate /*
27837c478bd9Sstevel@tonic-gate  * devmap_unload:
27847c478bd9Sstevel@tonic-gate  *			Marks a segdev segment or pages if offset->offset+len
27857c478bd9Sstevel@tonic-gate  *			is not the entire segment as intercept and unloads the
27867c478bd9Sstevel@tonic-gate  *			pages in the range offset -> offset+len.
27877c478bd9Sstevel@tonic-gate  */
27887c478bd9Sstevel@tonic-gate int
27897c478bd9Sstevel@tonic-gate devmap_unload(devmap_cookie_t dhc, offset_t offset, size_t len)
27907c478bd9Sstevel@tonic-gate {
27917c478bd9Sstevel@tonic-gate 	register devmap_handle_t *dhp = (devmap_handle_t *)dhc;
27927c478bd9Sstevel@tonic-gate 	caddr_t	addr;
27937c478bd9Sstevel@tonic-gate 	ulong_t	size;
27947c478bd9Sstevel@tonic-gate 	ssize_t	soff;
27957c478bd9Sstevel@tonic-gate 
27967c478bd9Sstevel@tonic-gate 	TRACE_3(TR_FAC_DEVMAP, TR_DEVMAP_UNLOAD,
27977c478bd9Sstevel@tonic-gate 	    "devmap_unload:start dhp=%p offset=%llx len=%lx",
27987c478bd9Sstevel@tonic-gate 	    (void *)dhp, offset, len);
27997c478bd9Sstevel@tonic-gate 	DEBUGF(7, (CE_CONT, "devmap_unload: dhp %p offset %llx len %lx\n",
28007c478bd9Sstevel@tonic-gate 	    (void *)dhp, offset, len));
28017c478bd9Sstevel@tonic-gate 
28027c478bd9Sstevel@tonic-gate 	soff = (ssize_t)(offset - dhp->dh_uoff);
28037c478bd9Sstevel@tonic-gate 	soff = round_down_p2(soff, PAGESIZE);
28047c478bd9Sstevel@tonic-gate 	if (soff < 0 || soff >= dhp->dh_len)
28057c478bd9Sstevel@tonic-gate 		return (FC_MAKE_ERR(EINVAL));
28067c478bd9Sstevel@tonic-gate 
28077c478bd9Sstevel@tonic-gate 	/*
28087c478bd9Sstevel@tonic-gate 	 * Address and size must be page aligned.  Len is set to the
28097c478bd9Sstevel@tonic-gate 	 * number of bytes in the number of pages that are required to
28107c478bd9Sstevel@tonic-gate 	 * support len.  Offset is set to the byte offset of the first byte
28117c478bd9Sstevel@tonic-gate 	 * of the page that contains offset.
28127c478bd9Sstevel@tonic-gate 	 */
28137c478bd9Sstevel@tonic-gate 	len = round_up_p2(len, PAGESIZE);
28147c478bd9Sstevel@tonic-gate 
28157c478bd9Sstevel@tonic-gate 	/*
28167c478bd9Sstevel@tonic-gate 	 * If len is == 0, then calculate the size by getting
28177c478bd9Sstevel@tonic-gate 	 * the number of bytes from offset to the end of the segment.
28187c478bd9Sstevel@tonic-gate 	 */
28197c478bd9Sstevel@tonic-gate 	if (len == 0)
28207c478bd9Sstevel@tonic-gate 		size = dhp->dh_len - soff;
28217c478bd9Sstevel@tonic-gate 	else {
28227c478bd9Sstevel@tonic-gate 		size = len;
28237c478bd9Sstevel@tonic-gate 		if ((soff + size) > dhp->dh_len)
28247c478bd9Sstevel@tonic-gate 			return (FC_MAKE_ERR(EINVAL));
28257c478bd9Sstevel@tonic-gate 	}
28267c478bd9Sstevel@tonic-gate 
28277c478bd9Sstevel@tonic-gate 	/*
28287c478bd9Sstevel@tonic-gate 	 * The address is offset bytes from the base address of
28297c478bd9Sstevel@tonic-gate 	 * the dhp.
28307c478bd9Sstevel@tonic-gate 	 */
28317c478bd9Sstevel@tonic-gate 	addr = (caddr_t)(soff + dhp->dh_uvaddr);
28327c478bd9Sstevel@tonic-gate 
28337c478bd9Sstevel@tonic-gate 	/*
28347c478bd9Sstevel@tonic-gate 	 * If large page size was used in hat_devload(),
28357c478bd9Sstevel@tonic-gate 	 * the same page size must be used in hat_unload().
28367c478bd9Sstevel@tonic-gate 	 */
28377c478bd9Sstevel@tonic-gate 	if (dhp->dh_flags & DEVMAP_FLAG_LARGE) {
28387c478bd9Sstevel@tonic-gate 		hat_unload(dhp->dh_seg->s_as->a_hat, dhp->dh_uvaddr,
28397c478bd9Sstevel@tonic-gate 			dhp->dh_len, HAT_UNLOAD|HAT_UNLOAD_OTHER);
28407c478bd9Sstevel@tonic-gate 	} else {
28417c478bd9Sstevel@tonic-gate 		hat_unload(dhp->dh_seg->s_as->a_hat,  addr, size,
28427c478bd9Sstevel@tonic-gate 			HAT_UNLOAD|HAT_UNLOAD_OTHER);
28437c478bd9Sstevel@tonic-gate 	}
28447c478bd9Sstevel@tonic-gate 
28457c478bd9Sstevel@tonic-gate 	return (0);
28467c478bd9Sstevel@tonic-gate }
28477c478bd9Sstevel@tonic-gate 
28487c478bd9Sstevel@tonic-gate /*
28497c478bd9Sstevel@tonic-gate  * calculates the optimal page size that will be used for hat_devload().
28507c478bd9Sstevel@tonic-gate  */
28517c478bd9Sstevel@tonic-gate static void
28527c478bd9Sstevel@tonic-gate devmap_get_large_pgsize(devmap_handle_t *dhp, size_t len, caddr_t addr,
28537c478bd9Sstevel@tonic-gate     size_t *llen, caddr_t *laddr)
28547c478bd9Sstevel@tonic-gate {
28557c478bd9Sstevel@tonic-gate 	ulong_t off;
28567c478bd9Sstevel@tonic-gate 	ulong_t pfn;
28577c478bd9Sstevel@tonic-gate 	ulong_t pgsize;
28587c478bd9Sstevel@tonic-gate 	uint_t first = 1;
28597c478bd9Sstevel@tonic-gate 
28607c478bd9Sstevel@tonic-gate 	TRACE_0(TR_FAC_DEVMAP, TR_DEVMAP_GET_LARGE_PGSIZE,
28617c478bd9Sstevel@tonic-gate 	    "devmap_get_large_pgsize:start");
28627c478bd9Sstevel@tonic-gate 
28637c478bd9Sstevel@tonic-gate 	/*
28647c478bd9Sstevel@tonic-gate 	 * RFE - Code only supports large page mappings for devmem
28657c478bd9Sstevel@tonic-gate 	 * This code could be changed in future if we want to support
28667c478bd9Sstevel@tonic-gate 	 * large page mappings for kernel exported memory.
28677c478bd9Sstevel@tonic-gate 	 */
28687c478bd9Sstevel@tonic-gate 	ASSERT(dhp_is_devmem(dhp));
28697c478bd9Sstevel@tonic-gate 	ASSERT(!(dhp->dh_flags & DEVMAP_MAPPING_INVALID));
28707c478bd9Sstevel@tonic-gate 
28717c478bd9Sstevel@tonic-gate 	*llen = 0;
28727c478bd9Sstevel@tonic-gate 	off = (ulong_t)(addr - dhp->dh_uvaddr);
28737c478bd9Sstevel@tonic-gate 	while ((long)len > 0) {
28747c478bd9Sstevel@tonic-gate 		/*
28757c478bd9Sstevel@tonic-gate 		 * get the optimal pfn to minimize address translations.
28767c478bd9Sstevel@tonic-gate 		 * devmap_roundup() returns residue bytes for next round
28777c478bd9Sstevel@tonic-gate 		 * calculations.
28787c478bd9Sstevel@tonic-gate 		 */
28797c478bd9Sstevel@tonic-gate 		len = devmap_roundup(dhp, off, len, &pfn, &pgsize);
28807c478bd9Sstevel@tonic-gate 
28817c478bd9Sstevel@tonic-gate 		if (first) {
28827c478bd9Sstevel@tonic-gate 			*laddr = dhp->dh_uvaddr + ptob(pfn - dhp->dh_pfn);
28837c478bd9Sstevel@tonic-gate 			first = 0;
28847c478bd9Sstevel@tonic-gate 		}
28857c478bd9Sstevel@tonic-gate 
28867c478bd9Sstevel@tonic-gate 		*llen += pgsize;
28877c478bd9Sstevel@tonic-gate 		off = ptob(pfn - dhp->dh_pfn) + pgsize;
28887c478bd9Sstevel@tonic-gate 	}
28897c478bd9Sstevel@tonic-gate 	/* Large page mapping len/addr cover more range than orginal fault */
28907c478bd9Sstevel@tonic-gate 	ASSERT(*llen >= len && *laddr <= addr);
28917c478bd9Sstevel@tonic-gate 	ASSERT((*laddr + *llen) >= (addr + len));
28927c478bd9Sstevel@tonic-gate }
28937c478bd9Sstevel@tonic-gate 
28947c478bd9Sstevel@tonic-gate /*
28957c478bd9Sstevel@tonic-gate  * Initialize the devmap_softlock structure.
28967c478bd9Sstevel@tonic-gate  */
28977c478bd9Sstevel@tonic-gate static struct devmap_softlock *
28987c478bd9Sstevel@tonic-gate devmap_softlock_init(dev_t dev, ulong_t id)
28997c478bd9Sstevel@tonic-gate {
29007c478bd9Sstevel@tonic-gate 	struct devmap_softlock *slock;
29017c478bd9Sstevel@tonic-gate 	struct devmap_softlock *tmp;
29027c478bd9Sstevel@tonic-gate 
29037c478bd9Sstevel@tonic-gate 	TRACE_0(TR_FAC_DEVMAP, TR_DEVMAP_SOFTLOCK_INIT,
29047c478bd9Sstevel@tonic-gate 	    "devmap_softlock_init:start");
29057c478bd9Sstevel@tonic-gate 
29067c478bd9Sstevel@tonic-gate 	tmp = kmem_zalloc(sizeof (struct devmap_softlock), KM_SLEEP);
29077c478bd9Sstevel@tonic-gate 	mutex_enter(&devmap_slock);
29087c478bd9Sstevel@tonic-gate 
29097c478bd9Sstevel@tonic-gate 	for (slock = devmap_slist; slock != NULL; slock = slock->next)
29107c478bd9Sstevel@tonic-gate 		if ((slock->dev == dev) && (slock->id == id))
29117c478bd9Sstevel@tonic-gate 			break;
29127c478bd9Sstevel@tonic-gate 
29137c478bd9Sstevel@tonic-gate 	if (slock == NULL) {
29147c478bd9Sstevel@tonic-gate 		slock = tmp;
29157c478bd9Sstevel@tonic-gate 		slock->dev = dev;
29167c478bd9Sstevel@tonic-gate 		slock->id = id;
29177c478bd9Sstevel@tonic-gate 		mutex_init(&slock->lock, NULL, MUTEX_DEFAULT, NULL);
29187c478bd9Sstevel@tonic-gate 		cv_init(&slock->cv, NULL, CV_DEFAULT, NULL);
29197c478bd9Sstevel@tonic-gate 		slock->next = devmap_slist;
29207c478bd9Sstevel@tonic-gate 		devmap_slist = slock;
29217c478bd9Sstevel@tonic-gate 	} else
29227c478bd9Sstevel@tonic-gate 		kmem_free(tmp, sizeof (struct devmap_softlock));
29237c478bd9Sstevel@tonic-gate 
29247c478bd9Sstevel@tonic-gate 	mutex_enter(&slock->lock);
29257c478bd9Sstevel@tonic-gate 	slock->refcnt++;
29267c478bd9Sstevel@tonic-gate 	mutex_exit(&slock->lock);
29277c478bd9Sstevel@tonic-gate 	mutex_exit(&devmap_slock);
29287c478bd9Sstevel@tonic-gate 
29297c478bd9Sstevel@tonic-gate 	return (slock);
29307c478bd9Sstevel@tonic-gate }
29317c478bd9Sstevel@tonic-gate 
29327c478bd9Sstevel@tonic-gate /*
29337c478bd9Sstevel@tonic-gate  * Wake up processes that sleep on softlocked.
29347c478bd9Sstevel@tonic-gate  * Free dh_softlock if refcnt is 0.
29357c478bd9Sstevel@tonic-gate  */
29367c478bd9Sstevel@tonic-gate static void
29377c478bd9Sstevel@tonic-gate devmap_softlock_rele(devmap_handle_t *dhp)
29387c478bd9Sstevel@tonic-gate {
29397c478bd9Sstevel@tonic-gate 	struct devmap_softlock *slock = dhp->dh_softlock;
29407c478bd9Sstevel@tonic-gate 	struct devmap_softlock *tmp;
29417c478bd9Sstevel@tonic-gate 	struct devmap_softlock *parent;
29427c478bd9Sstevel@tonic-gate 
29437c478bd9Sstevel@tonic-gate 	TRACE_0(TR_FAC_DEVMAP, TR_DEVMAP_SOFTLOCK_RELE,
29447c478bd9Sstevel@tonic-gate 	    "devmap_softlock_rele:start");
29457c478bd9Sstevel@tonic-gate 
29467c478bd9Sstevel@tonic-gate 	mutex_enter(&devmap_slock);
29477c478bd9Sstevel@tonic-gate 	mutex_enter(&slock->lock);
29487c478bd9Sstevel@tonic-gate 
29497c478bd9Sstevel@tonic-gate 	ASSERT(slock->refcnt > 0);
29507c478bd9Sstevel@tonic-gate 
29517c478bd9Sstevel@tonic-gate 	slock->refcnt--;
29527c478bd9Sstevel@tonic-gate 
29537c478bd9Sstevel@tonic-gate 	/*
29547c478bd9Sstevel@tonic-gate 	 * If no one is using the device, free up the slock data.
29557c478bd9Sstevel@tonic-gate 	 */
29567c478bd9Sstevel@tonic-gate 	if (slock->refcnt == 0) {
29577c478bd9Sstevel@tonic-gate 		slock->softlocked = 0;
29587c478bd9Sstevel@tonic-gate 		cv_signal(&slock->cv);
29597c478bd9Sstevel@tonic-gate 
29607c478bd9Sstevel@tonic-gate 		if (devmap_slist == slock)
29617c478bd9Sstevel@tonic-gate 			devmap_slist = slock->next;
29627c478bd9Sstevel@tonic-gate 		else {
29637c478bd9Sstevel@tonic-gate 			parent = devmap_slist;
29647c478bd9Sstevel@tonic-gate 			for (tmp = devmap_slist->next; tmp != NULL;
29657c478bd9Sstevel@tonic-gate 				tmp = tmp->next) {
29667c478bd9Sstevel@tonic-gate 				if (tmp == slock) {
29677c478bd9Sstevel@tonic-gate 					parent->next = tmp->next;
29687c478bd9Sstevel@tonic-gate 					break;
29697c478bd9Sstevel@tonic-gate 				}
29707c478bd9Sstevel@tonic-gate 				parent = tmp;
29717c478bd9Sstevel@tonic-gate 			}
29727c478bd9Sstevel@tonic-gate 		}
29737c478bd9Sstevel@tonic-gate 		mutex_exit(&slock->lock);
29747c478bd9Sstevel@tonic-gate 		mutex_destroy(&slock->lock);
29757c478bd9Sstevel@tonic-gate 		cv_destroy(&slock->cv);
29767c478bd9Sstevel@tonic-gate 		kmem_free(slock, sizeof (struct devmap_softlock));
29777c478bd9Sstevel@tonic-gate 	} else
29787c478bd9Sstevel@tonic-gate 		mutex_exit(&slock->lock);
29797c478bd9Sstevel@tonic-gate 
29807c478bd9Sstevel@tonic-gate 	mutex_exit(&devmap_slock);
29817c478bd9Sstevel@tonic-gate }
29827c478bd9Sstevel@tonic-gate 
29837c478bd9Sstevel@tonic-gate /*
29847c478bd9Sstevel@tonic-gate  * Wake up processes that sleep on dh_ctx->locked.
29857c478bd9Sstevel@tonic-gate  * Free dh_ctx if refcnt is 0.
29867c478bd9Sstevel@tonic-gate  */
29877c478bd9Sstevel@tonic-gate static void
29887c478bd9Sstevel@tonic-gate devmap_ctx_rele(devmap_handle_t *dhp)
29897c478bd9Sstevel@tonic-gate {
29907c478bd9Sstevel@tonic-gate 	struct devmap_ctx *devctx = dhp->dh_ctx;
29917c478bd9Sstevel@tonic-gate 	struct devmap_ctx *tmp;
29927c478bd9Sstevel@tonic-gate 	struct devmap_ctx *parent;
29937c478bd9Sstevel@tonic-gate 	timeout_id_t tid;
29947c478bd9Sstevel@tonic-gate 
29957c478bd9Sstevel@tonic-gate 	TRACE_0(TR_FAC_DEVMAP, TR_DEVMAP_CTX_RELE,
29967c478bd9Sstevel@tonic-gate 	    "devmap_ctx_rele:start");
29977c478bd9Sstevel@tonic-gate 
29987c478bd9Sstevel@tonic-gate 	mutex_enter(&devmapctx_lock);
29997c478bd9Sstevel@tonic-gate 	mutex_enter(&devctx->lock);
30007c478bd9Sstevel@tonic-gate 
30017c478bd9Sstevel@tonic-gate 	ASSERT(devctx->refcnt > 0);
30027c478bd9Sstevel@tonic-gate 
30037c478bd9Sstevel@tonic-gate 	devctx->refcnt--;
30047c478bd9Sstevel@tonic-gate 
30057c478bd9Sstevel@tonic-gate 	/*
30067c478bd9Sstevel@tonic-gate 	 * If no one is using the device, free up the devctx data.
30077c478bd9Sstevel@tonic-gate 	 */
30087c478bd9Sstevel@tonic-gate 	if (devctx->refcnt == 0) {
30097c478bd9Sstevel@tonic-gate 		/*
30107c478bd9Sstevel@tonic-gate 		 * Untimeout any threads using this mapping as they are about
30117c478bd9Sstevel@tonic-gate 		 * to go away.
30127c478bd9Sstevel@tonic-gate 		 */
30137c478bd9Sstevel@tonic-gate 		if (devctx->timeout != 0) {
30147c478bd9Sstevel@tonic-gate 			TRACE_0(TR_FAC_DEVMAP, TR_DEVMAP_CTX_RELE_CK1,
30157c478bd9Sstevel@tonic-gate 			    "devmap_ctx_rele:untimeout ctx->timeout");
30167c478bd9Sstevel@tonic-gate 
30177c478bd9Sstevel@tonic-gate 			tid = devctx->timeout;
30187c478bd9Sstevel@tonic-gate 			mutex_exit(&devctx->lock);
30197c478bd9Sstevel@tonic-gate 			(void) untimeout(tid);
30207c478bd9Sstevel@tonic-gate 			mutex_enter(&devctx->lock);
30217c478bd9Sstevel@tonic-gate 		}
30227c478bd9Sstevel@tonic-gate 
30237c478bd9Sstevel@tonic-gate 		devctx->oncpu = 0;
30247c478bd9Sstevel@tonic-gate 		cv_signal(&devctx->cv);
30257c478bd9Sstevel@tonic-gate 
30267c478bd9Sstevel@tonic-gate 		if (devmapctx_list == devctx)
30277c478bd9Sstevel@tonic-gate 			devmapctx_list = devctx->next;
30287c478bd9Sstevel@tonic-gate 		else {
30297c478bd9Sstevel@tonic-gate 			parent = devmapctx_list;
30307c478bd9Sstevel@tonic-gate 			for (tmp = devmapctx_list->next; tmp != NULL;
30317c478bd9Sstevel@tonic-gate 				tmp = tmp->next) {
30327c478bd9Sstevel@tonic-gate 				if (tmp == devctx) {
30337c478bd9Sstevel@tonic-gate 					parent->next = tmp->next;
30347c478bd9Sstevel@tonic-gate 					break;
30357c478bd9Sstevel@tonic-gate 				}
30367c478bd9Sstevel@tonic-gate 				parent = tmp;
30377c478bd9Sstevel@tonic-gate 			}
30387c478bd9Sstevel@tonic-gate 		}
30397c478bd9Sstevel@tonic-gate 		mutex_exit(&devctx->lock);
30407c478bd9Sstevel@tonic-gate 		mutex_destroy(&devctx->lock);
30417c478bd9Sstevel@tonic-gate 		cv_destroy(&devctx->cv);
30427c478bd9Sstevel@tonic-gate 		kmem_free(devctx, sizeof (struct devmap_ctx));
30437c478bd9Sstevel@tonic-gate 	} else
30447c478bd9Sstevel@tonic-gate 		mutex_exit(&devctx->lock);
30457c478bd9Sstevel@tonic-gate 
30467c478bd9Sstevel@tonic-gate 	mutex_exit(&devmapctx_lock);
30477c478bd9Sstevel@tonic-gate }
30487c478bd9Sstevel@tonic-gate 
30497c478bd9Sstevel@tonic-gate /*
30507c478bd9Sstevel@tonic-gate  * devmap_load:
30517c478bd9Sstevel@tonic-gate  *			Marks a segdev segment or pages if offset->offset+len
30527c478bd9Sstevel@tonic-gate  *			is not the entire segment as nointercept and faults in
30537c478bd9Sstevel@tonic-gate  *			the pages in the range offset -> offset+len.
30547c478bd9Sstevel@tonic-gate  */
30557c478bd9Sstevel@tonic-gate int
30567c478bd9Sstevel@tonic-gate devmap_load(devmap_cookie_t dhc, offset_t offset, size_t len, uint_t type,
30577c478bd9Sstevel@tonic-gate     uint_t rw)
30587c478bd9Sstevel@tonic-gate {
30597c478bd9Sstevel@tonic-gate 	devmap_handle_t *dhp = (devmap_handle_t *)dhc;
30607c478bd9Sstevel@tonic-gate 	struct as *asp = dhp->dh_seg->s_as;
30617c478bd9Sstevel@tonic-gate 	caddr_t	addr;
30627c478bd9Sstevel@tonic-gate 	ulong_t	size;
30637c478bd9Sstevel@tonic-gate 	ssize_t	soff;	/* offset from the beginning of the segment */
30647c478bd9Sstevel@tonic-gate 	int rc;
30657c478bd9Sstevel@tonic-gate 
30667c478bd9Sstevel@tonic-gate 	TRACE_3(TR_FAC_DEVMAP, TR_DEVMAP_LOAD,
30677c478bd9Sstevel@tonic-gate 	    "devmap_load:start dhp=%p offset=%llx len=%lx",
30687c478bd9Sstevel@tonic-gate 		(void *)dhp, offset, len);
30697c478bd9Sstevel@tonic-gate 
30707c478bd9Sstevel@tonic-gate 	DEBUGF(7, (CE_CONT, "devmap_load: dhp %p offset %llx len %lx\n",
30717c478bd9Sstevel@tonic-gate 	    (void *)dhp, offset, len));
30727c478bd9Sstevel@tonic-gate 
30737c478bd9Sstevel@tonic-gate 	/*
30747c478bd9Sstevel@tonic-gate 	 *	Hat layer only supports devload to process' context for which
30757c478bd9Sstevel@tonic-gate 	 *	the as lock is held. Verify here and return error if drivers
30767c478bd9Sstevel@tonic-gate 	 *	inadvertently call devmap_load on a wrong devmap handle.
30777c478bd9Sstevel@tonic-gate 	 */
30787c478bd9Sstevel@tonic-gate 	if ((asp != &kas) && !AS_LOCK_HELD(asp, &asp->a_lock))
30797c478bd9Sstevel@tonic-gate 		return (FC_MAKE_ERR(EINVAL));
30807c478bd9Sstevel@tonic-gate 
30817c478bd9Sstevel@tonic-gate 	soff = (ssize_t)(offset - dhp->dh_uoff);
30827c478bd9Sstevel@tonic-gate 	soff = round_down_p2(soff, PAGESIZE);
30837c478bd9Sstevel@tonic-gate 	if (soff < 0 || soff >= dhp->dh_len)
30847c478bd9Sstevel@tonic-gate 		return (FC_MAKE_ERR(EINVAL));
30857c478bd9Sstevel@tonic-gate 
30867c478bd9Sstevel@tonic-gate 	/*
30877c478bd9Sstevel@tonic-gate 	 * Address and size must be page aligned.  Len is set to the
30887c478bd9Sstevel@tonic-gate 	 * number of bytes in the number of pages that are required to
30897c478bd9Sstevel@tonic-gate 	 * support len.  Offset is set to the byte offset of the first byte
30907c478bd9Sstevel@tonic-gate 	 * of the page that contains offset.
30917c478bd9Sstevel@tonic-gate 	 */
30927c478bd9Sstevel@tonic-gate 	len = round_up_p2(len, PAGESIZE);
30937c478bd9Sstevel@tonic-gate 
30947c478bd9Sstevel@tonic-gate 	/*
30957c478bd9Sstevel@tonic-gate 	 * If len == 0, then calculate the size by getting
30967c478bd9Sstevel@tonic-gate 	 * the number of bytes from offset to the end of the segment.
30977c478bd9Sstevel@tonic-gate 	 */
30987c478bd9Sstevel@tonic-gate 	if (len == 0)
30997c478bd9Sstevel@tonic-gate 		size = dhp->dh_len - soff;
31007c478bd9Sstevel@tonic-gate 	else {
31017c478bd9Sstevel@tonic-gate 		size = len;
31027c478bd9Sstevel@tonic-gate 		if ((soff + size) > dhp->dh_len)
31037c478bd9Sstevel@tonic-gate 			return (FC_MAKE_ERR(EINVAL));
31047c478bd9Sstevel@tonic-gate 	}
31057c478bd9Sstevel@tonic-gate 
31067c478bd9Sstevel@tonic-gate 	/*
31077c478bd9Sstevel@tonic-gate 	 * The address is offset bytes from the base address of
31087c478bd9Sstevel@tonic-gate 	 * the segment.
31097c478bd9Sstevel@tonic-gate 	 */
31107c478bd9Sstevel@tonic-gate 	addr = (caddr_t)(soff + dhp->dh_uvaddr);
31117c478bd9Sstevel@tonic-gate 
31127c478bd9Sstevel@tonic-gate 	HOLD_DHP_LOCK(dhp);
31137c478bd9Sstevel@tonic-gate 	rc = segdev_faultpages(asp->a_hat,
31147c478bd9Sstevel@tonic-gate 			dhp->dh_seg, addr, size, type, rw, dhp);
31157c478bd9Sstevel@tonic-gate 	RELE_DHP_LOCK(dhp);
31167c478bd9Sstevel@tonic-gate 	return (rc);
31177c478bd9Sstevel@tonic-gate }
31187c478bd9Sstevel@tonic-gate 
31197c478bd9Sstevel@tonic-gate int
31207c478bd9Sstevel@tonic-gate devmap_setup(dev_t dev, offset_t off, struct as *as, caddr_t *addrp,
31217c478bd9Sstevel@tonic-gate     size_t len, uint_t prot, uint_t maxprot, uint_t flags, struct cred *cred)
31227c478bd9Sstevel@tonic-gate {
31237c478bd9Sstevel@tonic-gate 	register devmap_handle_t *dhp;
31247c478bd9Sstevel@tonic-gate 	int (*devmap)(dev_t, devmap_cookie_t, offset_t, size_t,
31257c478bd9Sstevel@tonic-gate 		size_t *, uint_t);
31267c478bd9Sstevel@tonic-gate 	int (*mmap)(dev_t, off_t, int);
31277c478bd9Sstevel@tonic-gate 	struct devmap_callback_ctl *callbackops;
31287c478bd9Sstevel@tonic-gate 	devmap_handle_t *dhp_head = NULL;
31297c478bd9Sstevel@tonic-gate 	devmap_handle_t *dhp_prev = NULL;
31307c478bd9Sstevel@tonic-gate 	devmap_handle_t *dhp_curr;
31317c478bd9Sstevel@tonic-gate 	caddr_t addr;
31327c478bd9Sstevel@tonic-gate 	int map_flag;
31337c478bd9Sstevel@tonic-gate 	int ret;
31347c478bd9Sstevel@tonic-gate 	ulong_t total_len;
31357c478bd9Sstevel@tonic-gate 	size_t map_len;
31367c478bd9Sstevel@tonic-gate 	size_t resid_len = len;
31377c478bd9Sstevel@tonic-gate 	offset_t map_off = off;
31387c478bd9Sstevel@tonic-gate 	struct devmap_softlock *slock = NULL;
31397c478bd9Sstevel@tonic-gate 
31407c478bd9Sstevel@tonic-gate #ifdef lint
31417c478bd9Sstevel@tonic-gate 	cred = cred;
31427c478bd9Sstevel@tonic-gate #endif
31437c478bd9Sstevel@tonic-gate 
31447c478bd9Sstevel@tonic-gate 	TRACE_2(TR_FAC_DEVMAP, TR_DEVMAP_SETUP,
31457c478bd9Sstevel@tonic-gate 	    "devmap_setup:start off=%llx len=%lx", off, len);
31467c478bd9Sstevel@tonic-gate 	DEBUGF(3, (CE_CONT, "devmap_setup: off %llx len %lx\n",
31477c478bd9Sstevel@tonic-gate 	    off, len));
31487c478bd9Sstevel@tonic-gate 
31497c478bd9Sstevel@tonic-gate 	devmap = devopsp[getmajor(dev)]->devo_cb_ops->cb_devmap;
31507c478bd9Sstevel@tonic-gate 	mmap = devopsp[getmajor(dev)]->devo_cb_ops->cb_mmap;
31517c478bd9Sstevel@tonic-gate 
31527c478bd9Sstevel@tonic-gate 	/*
31537c478bd9Sstevel@tonic-gate 	 * driver must provide devmap(9E) entry point in cb_ops to use the
31547c478bd9Sstevel@tonic-gate 	 * devmap framework.
31557c478bd9Sstevel@tonic-gate 	 */
31567c478bd9Sstevel@tonic-gate 	if (devmap == NULL || devmap == nulldev || devmap == nodev)
31577c478bd9Sstevel@tonic-gate 		return (EINVAL);
31587c478bd9Sstevel@tonic-gate 
31597c478bd9Sstevel@tonic-gate 	/*
31607c478bd9Sstevel@tonic-gate 	 * To protect from an inadvertent entry because the devmap entry point
31617c478bd9Sstevel@tonic-gate 	 * is not NULL, return error if D_DEVMAP bit is not set in cb_flag and
31627c478bd9Sstevel@tonic-gate 	 * mmap is NULL.
31637c478bd9Sstevel@tonic-gate 	 */
31647c478bd9Sstevel@tonic-gate 	map_flag = devopsp[getmajor(dev)]->devo_cb_ops->cb_flag;
31657c478bd9Sstevel@tonic-gate 	if ((map_flag & D_DEVMAP) == 0 && (mmap == NULL || mmap == nulldev))
31667c478bd9Sstevel@tonic-gate 		return (EINVAL);
31677c478bd9Sstevel@tonic-gate 
31687c478bd9Sstevel@tonic-gate 	/*
31697c478bd9Sstevel@tonic-gate 	 * devmap allows mmap(2) to map multiple registers.
31707c478bd9Sstevel@tonic-gate 	 * one devmap_handle is created for each register mapped.
31717c478bd9Sstevel@tonic-gate 	 */
31727c478bd9Sstevel@tonic-gate 	for (total_len = 0; total_len < len; total_len += map_len) {
31737c478bd9Sstevel@tonic-gate 		dhp = kmem_zalloc(sizeof (devmap_handle_t), KM_SLEEP);
31747c478bd9Sstevel@tonic-gate 
31757c478bd9Sstevel@tonic-gate 		if (dhp_prev != NULL)
31767c478bd9Sstevel@tonic-gate 			dhp_prev->dh_next = dhp;
31777c478bd9Sstevel@tonic-gate 		else
31787c478bd9Sstevel@tonic-gate 			dhp_head = dhp;
31797c478bd9Sstevel@tonic-gate 		dhp_prev = dhp;
31807c478bd9Sstevel@tonic-gate 
31817c478bd9Sstevel@tonic-gate 		dhp->dh_prot = prot;
31827c478bd9Sstevel@tonic-gate 		dhp->dh_orig_maxprot = dhp->dh_maxprot = maxprot;
31837c478bd9Sstevel@tonic-gate 		dhp->dh_dev = dev;
31847c478bd9Sstevel@tonic-gate 		dhp->dh_timeout_length = CTX_TIMEOUT_VALUE;
31857c478bd9Sstevel@tonic-gate 		dhp->dh_uoff = map_off;
31867c478bd9Sstevel@tonic-gate 
31877c478bd9Sstevel@tonic-gate 		/*
31887c478bd9Sstevel@tonic-gate 		 * Get mapping specific info from
31897c478bd9Sstevel@tonic-gate 		 * the driver, such as rnumber, roff, len, callbackops,
31907c478bd9Sstevel@tonic-gate 		 * accattrp and, if the mapping is for kernel memory,
31917c478bd9Sstevel@tonic-gate 		 * ddi_umem_cookie.
31927c478bd9Sstevel@tonic-gate 		 */
31937c478bd9Sstevel@tonic-gate 		if ((ret = cdev_devmap(dev, dhp, map_off,
31947c478bd9Sstevel@tonic-gate 		    resid_len, &map_len, get_udatamodel())) != 0) {
31957c478bd9Sstevel@tonic-gate 			free_devmap_handle(dhp_head);
31967c478bd9Sstevel@tonic-gate 			return (ENXIO);
31977c478bd9Sstevel@tonic-gate 		}
31987c478bd9Sstevel@tonic-gate 
31997c478bd9Sstevel@tonic-gate 		if (map_len & PAGEOFFSET) {
32007c478bd9Sstevel@tonic-gate 			free_devmap_handle(dhp_head);
32017c478bd9Sstevel@tonic-gate 			return (EINVAL);
32027c478bd9Sstevel@tonic-gate 		}
32037c478bd9Sstevel@tonic-gate 
32047c478bd9Sstevel@tonic-gate 		callbackops = &dhp->dh_callbackops;
32057c478bd9Sstevel@tonic-gate 
32067c478bd9Sstevel@tonic-gate 		if ((callbackops->devmap_access == NULL) ||
32077c478bd9Sstevel@tonic-gate 			(callbackops->devmap_access == nulldev) ||
32087c478bd9Sstevel@tonic-gate 			(callbackops->devmap_access == nodev)) {
32097c478bd9Sstevel@tonic-gate 			/*
32107c478bd9Sstevel@tonic-gate 			 * Normally devmap does not support MAP_PRIVATE unless
32117c478bd9Sstevel@tonic-gate 			 * the drivers provide a valid devmap_access routine.
32127c478bd9Sstevel@tonic-gate 			 */
32137c478bd9Sstevel@tonic-gate 			if ((flags & MAP_PRIVATE) != 0) {
32147c478bd9Sstevel@tonic-gate 				free_devmap_handle(dhp_head);
32157c478bd9Sstevel@tonic-gate 				return (EINVAL);
32167c478bd9Sstevel@tonic-gate 			}
32177c478bd9Sstevel@tonic-gate 		} else {
32187c478bd9Sstevel@tonic-gate 			/*
32197c478bd9Sstevel@tonic-gate 			 * Initialize dhp_softlock and dh_ctx if the drivers
32207c478bd9Sstevel@tonic-gate 			 * provide devmap_access.
32217c478bd9Sstevel@tonic-gate 			 */
32227c478bd9Sstevel@tonic-gate 			dhp->dh_softlock = devmap_softlock_init(dev,
32237c478bd9Sstevel@tonic-gate 				(ulong_t)callbackops->devmap_access);
32247c478bd9Sstevel@tonic-gate 			dhp->dh_ctx = devmap_ctxinit(dev,
32257c478bd9Sstevel@tonic-gate 				(ulong_t)callbackops->devmap_access);
32267c478bd9Sstevel@tonic-gate 
32277c478bd9Sstevel@tonic-gate 			/*
32287c478bd9Sstevel@tonic-gate 			 * segdev_fault can only work when all
32297c478bd9Sstevel@tonic-gate 			 * dh_softlock in a multi-dhp mapping
32307c478bd9Sstevel@tonic-gate 			 * are same. see comments in segdev_fault
32317c478bd9Sstevel@tonic-gate 			 * This code keeps track of the first
32327c478bd9Sstevel@tonic-gate 			 * dh_softlock allocated in slock and
32337c478bd9Sstevel@tonic-gate 			 * compares all later allocations and if
32347c478bd9Sstevel@tonic-gate 			 * not similar, returns an error.
32357c478bd9Sstevel@tonic-gate 			 */
32367c478bd9Sstevel@tonic-gate 			if (slock == NULL)
32377c478bd9Sstevel@tonic-gate 				slock = dhp->dh_softlock;
32387c478bd9Sstevel@tonic-gate 			if (slock != dhp->dh_softlock) {
32397c478bd9Sstevel@tonic-gate 				free_devmap_handle(dhp_head);
32407c478bd9Sstevel@tonic-gate 				return (ENOTSUP);
32417c478bd9Sstevel@tonic-gate 			}
32427c478bd9Sstevel@tonic-gate 		}
32437c478bd9Sstevel@tonic-gate 
32447c478bd9Sstevel@tonic-gate 		map_off += map_len;
32457c478bd9Sstevel@tonic-gate 		resid_len -= map_len;
32467c478bd9Sstevel@tonic-gate 	}
32477c478bd9Sstevel@tonic-gate 
32487c478bd9Sstevel@tonic-gate 	/*
32497c478bd9Sstevel@tonic-gate 	 * get the user virtual address and establish the mapping between
32507c478bd9Sstevel@tonic-gate 	 * uvaddr and device physical address.
32517c478bd9Sstevel@tonic-gate 	 */
32527c478bd9Sstevel@tonic-gate 	if ((ret = devmap_device(dhp_head, as, addrp, off, len, flags))
32537c478bd9Sstevel@tonic-gate 			!= 0) {
32547c478bd9Sstevel@tonic-gate 		/*
32557c478bd9Sstevel@tonic-gate 		 * free devmap handles if error during the mapping.
32567c478bd9Sstevel@tonic-gate 		 */
32577c478bd9Sstevel@tonic-gate 		free_devmap_handle(dhp_head);
32587c478bd9Sstevel@tonic-gate 
32597c478bd9Sstevel@tonic-gate 		return (ret);
32607c478bd9Sstevel@tonic-gate 	}
32617c478bd9Sstevel@tonic-gate 
32627c478bd9Sstevel@tonic-gate 	/*
32637c478bd9Sstevel@tonic-gate 	 * call the driver's devmap_map callback to do more after the mapping,
32647c478bd9Sstevel@tonic-gate 	 * such as to allocate driver private data for context management.
32657c478bd9Sstevel@tonic-gate 	 */
32667c478bd9Sstevel@tonic-gate 	dhp = dhp_head;
32677c478bd9Sstevel@tonic-gate 	map_off = off;
32687c478bd9Sstevel@tonic-gate 	addr = *addrp;
32697c478bd9Sstevel@tonic-gate 	while (dhp != NULL) {
32707c478bd9Sstevel@tonic-gate 		callbackops = &dhp->dh_callbackops;
32717c478bd9Sstevel@tonic-gate 		dhp->dh_uvaddr = addr;
32727c478bd9Sstevel@tonic-gate 		dhp_curr = dhp;
32737c478bd9Sstevel@tonic-gate 		if (callbackops->devmap_map != NULL) {
32747c478bd9Sstevel@tonic-gate 			ret = (*callbackops->devmap_map)((devmap_cookie_t)dhp,
32757c478bd9Sstevel@tonic-gate 					dev, flags, map_off,
32767c478bd9Sstevel@tonic-gate 					dhp->dh_len, &dhp->dh_pvtp);
32777c478bd9Sstevel@tonic-gate 			if (ret != 0) {
32787c478bd9Sstevel@tonic-gate 				struct segdev_data *sdp;
32797c478bd9Sstevel@tonic-gate 
32807c478bd9Sstevel@tonic-gate 				/*
32817c478bd9Sstevel@tonic-gate 				 * call driver's devmap_unmap entry point
32827c478bd9Sstevel@tonic-gate 				 * to free driver resources.
32837c478bd9Sstevel@tonic-gate 				 */
32847c478bd9Sstevel@tonic-gate 				dhp = dhp_head;
32857c478bd9Sstevel@tonic-gate 				map_off = off;
32867c478bd9Sstevel@tonic-gate 				while (dhp != dhp_curr) {
32877c478bd9Sstevel@tonic-gate 					callbackops = &dhp->dh_callbackops;
32887c478bd9Sstevel@tonic-gate 					if (callbackops->devmap_unmap != NULL) {
32897c478bd9Sstevel@tonic-gate 						(*callbackops->devmap_unmap)(
32907c478bd9Sstevel@tonic-gate 							dhp, dhp->dh_pvtp,
32917c478bd9Sstevel@tonic-gate 							map_off, dhp->dh_len,
32927c478bd9Sstevel@tonic-gate 							NULL, NULL, NULL, NULL);
32937c478bd9Sstevel@tonic-gate 					}
32947c478bd9Sstevel@tonic-gate 					map_off += dhp->dh_len;
32957c478bd9Sstevel@tonic-gate 					dhp = dhp->dh_next;
32967c478bd9Sstevel@tonic-gate 				}
32977c478bd9Sstevel@tonic-gate 				sdp = dhp_head->dh_seg->s_data;
32987c478bd9Sstevel@tonic-gate 				sdp->devmap_data = NULL;
32997c478bd9Sstevel@tonic-gate 				free_devmap_handle(dhp_head);
33007c478bd9Sstevel@tonic-gate 				return (ENXIO);
33017c478bd9Sstevel@tonic-gate 			}
33027c478bd9Sstevel@tonic-gate 		}
33037c478bd9Sstevel@tonic-gate 		map_off += dhp->dh_len;
33047c478bd9Sstevel@tonic-gate 		addr += dhp->dh_len;
33057c478bd9Sstevel@tonic-gate 		dhp = dhp->dh_next;
33067c478bd9Sstevel@tonic-gate 	}
33077c478bd9Sstevel@tonic-gate 
33087c478bd9Sstevel@tonic-gate 	return (0);
33097c478bd9Sstevel@tonic-gate }
33107c478bd9Sstevel@tonic-gate 
33117c478bd9Sstevel@tonic-gate int
33127c478bd9Sstevel@tonic-gate ddi_devmap_segmap(dev_t dev, off_t off, ddi_as_handle_t as, caddr_t *addrp,
33137c478bd9Sstevel@tonic-gate     off_t len, uint_t prot, uint_t maxprot, uint_t flags, struct cred *cred)
33147c478bd9Sstevel@tonic-gate {
33157c478bd9Sstevel@tonic-gate 	TRACE_0(TR_FAC_DEVMAP, TR_DEVMAP_SEGMAP,
33167c478bd9Sstevel@tonic-gate 	    "devmap_segmap:start");
33177c478bd9Sstevel@tonic-gate 	return (devmap_setup(dev, (offset_t)off, (struct as *)as, addrp,
33187c478bd9Sstevel@tonic-gate 	    (size_t)len, prot, maxprot, flags, cred));
33197c478bd9Sstevel@tonic-gate }
33207c478bd9Sstevel@tonic-gate 
33217c478bd9Sstevel@tonic-gate /*
33227c478bd9Sstevel@tonic-gate  * Called from devmap_devmem_setup/remap to see if can use large pages for
33237c478bd9Sstevel@tonic-gate  * this device mapping.
33247c478bd9Sstevel@tonic-gate  * Also calculate the max. page size for this mapping.
33257c478bd9Sstevel@tonic-gate  * this page size will be used in fault routine for
33267c478bd9Sstevel@tonic-gate  * optimal page size calculations.
33277c478bd9Sstevel@tonic-gate  */
33287c478bd9Sstevel@tonic-gate static void
33297c478bd9Sstevel@tonic-gate devmap_devmem_large_page_setup(devmap_handle_t *dhp)
33307c478bd9Sstevel@tonic-gate {
33317c478bd9Sstevel@tonic-gate 	ASSERT(dhp_is_devmem(dhp));
33327c478bd9Sstevel@tonic-gate 	dhp->dh_mmulevel = 0;
33337c478bd9Sstevel@tonic-gate 
33347c478bd9Sstevel@tonic-gate 	/*
33357c478bd9Sstevel@tonic-gate 	 * use large page size only if:
33367c478bd9Sstevel@tonic-gate 	 *  1. device memory.
33377c478bd9Sstevel@tonic-gate 	 *  2. mmu supports multiple page sizes,
33387c478bd9Sstevel@tonic-gate 	 *  3. Driver did not disallow it
33397c478bd9Sstevel@tonic-gate 	 *  4. dhp length is at least as big as the large pagesize
33407c478bd9Sstevel@tonic-gate 	 *  5. the uvaddr and pfn are large pagesize aligned
33417c478bd9Sstevel@tonic-gate 	 */
33427c478bd9Sstevel@tonic-gate 	if (page_num_pagesizes() > 1 &&
33437c478bd9Sstevel@tonic-gate 	    !(dhp->dh_flags & (DEVMAP_USE_PAGESIZE | DEVMAP_MAPPING_INVALID))) {
33447c478bd9Sstevel@tonic-gate 		ulong_t base;
33457c478bd9Sstevel@tonic-gate 		int level;
33467c478bd9Sstevel@tonic-gate 
33477c478bd9Sstevel@tonic-gate 		base = (ulong_t)ptob(dhp->dh_pfn);
33487c478bd9Sstevel@tonic-gate 		for (level = 1; level < page_num_pagesizes(); level++) {
33497c478bd9Sstevel@tonic-gate 			size_t pgsize = page_get_pagesize(level);
33507c478bd9Sstevel@tonic-gate 			if ((dhp->dh_len < pgsize) ||
33517c478bd9Sstevel@tonic-gate 			    (!VA_PA_PGSIZE_ALIGNED((uintptr_t)dhp->dh_uvaddr,
33527c478bd9Sstevel@tonic-gate 					base, pgsize))) {
33537c478bd9Sstevel@tonic-gate 				break;
33547c478bd9Sstevel@tonic-gate 			}
33557c478bd9Sstevel@tonic-gate 		}
33567c478bd9Sstevel@tonic-gate 		dhp->dh_mmulevel = level - 1;
33577c478bd9Sstevel@tonic-gate 	}
33587c478bd9Sstevel@tonic-gate 	if (dhp->dh_mmulevel > 0) {
33597c478bd9Sstevel@tonic-gate 		dhp->dh_flags |= DEVMAP_FLAG_LARGE;
33607c478bd9Sstevel@tonic-gate 	} else {
33617c478bd9Sstevel@tonic-gate 		dhp->dh_flags &= ~DEVMAP_FLAG_LARGE;
33627c478bd9Sstevel@tonic-gate 	}
33637c478bd9Sstevel@tonic-gate }
33647c478bd9Sstevel@tonic-gate 
33657c478bd9Sstevel@tonic-gate /*
33667c478bd9Sstevel@tonic-gate  * Called by driver devmap routine to pass device specific info to
33677c478bd9Sstevel@tonic-gate  * the framework.    used for device memory mapping only.
33687c478bd9Sstevel@tonic-gate  */
33697c478bd9Sstevel@tonic-gate int
33707c478bd9Sstevel@tonic-gate devmap_devmem_setup(devmap_cookie_t dhc, dev_info_t *dip,
33717c478bd9Sstevel@tonic-gate     struct devmap_callback_ctl *callbackops, uint_t rnumber, offset_t roff,
33727c478bd9Sstevel@tonic-gate     size_t len, uint_t maxprot, uint_t flags, ddi_device_acc_attr_t *accattrp)
33737c478bd9Sstevel@tonic-gate {
33747c478bd9Sstevel@tonic-gate 	devmap_handle_t *dhp = (devmap_handle_t *)dhc;
33757c478bd9Sstevel@tonic-gate 	ddi_acc_handle_t handle;
33767c478bd9Sstevel@tonic-gate 	ddi_map_req_t mr;
33777c478bd9Sstevel@tonic-gate 	ddi_acc_hdl_t *hp;
33787c478bd9Sstevel@tonic-gate 	int err;
33797c478bd9Sstevel@tonic-gate 
33807c478bd9Sstevel@tonic-gate 	TRACE_4(TR_FAC_DEVMAP, TR_DEVMAP_DEVMEM_SETUP,
33817c478bd9Sstevel@tonic-gate 	    "devmap_devmem_setup:start dhp=%p offset=%llx rnum=%d len=%lx",
33827c478bd9Sstevel@tonic-gate 	    (void *)dhp, roff, rnumber, (uint_t)len);
33837c478bd9Sstevel@tonic-gate 	DEBUGF(2, (CE_CONT, "devmap_devmem_setup: dhp %p offset %llx "
33847c478bd9Sstevel@tonic-gate 	    "rnum %d len %lx\n", (void *)dhp, roff, rnumber, len));
33857c478bd9Sstevel@tonic-gate 
33867c478bd9Sstevel@tonic-gate 	/*
33877c478bd9Sstevel@tonic-gate 	 * First to check if this function has been called for this dhp.
33887c478bd9Sstevel@tonic-gate 	 */
33897c478bd9Sstevel@tonic-gate 	if (dhp->dh_flags & DEVMAP_SETUP_DONE)
33907c478bd9Sstevel@tonic-gate 		return (DDI_FAILURE);
33917c478bd9Sstevel@tonic-gate 
33927c478bd9Sstevel@tonic-gate 	if ((dhp->dh_prot & dhp->dh_orig_maxprot & maxprot) != dhp->dh_prot)
33937c478bd9Sstevel@tonic-gate 		return (DDI_FAILURE);
33947c478bd9Sstevel@tonic-gate 
33957c478bd9Sstevel@tonic-gate 	if (flags & DEVMAP_MAPPING_INVALID) {
33967c478bd9Sstevel@tonic-gate 		/*
33977c478bd9Sstevel@tonic-gate 		 * Don't go up the tree to get pfn if the driver specifies
33987c478bd9Sstevel@tonic-gate 		 * DEVMAP_MAPPING_INVALID in flags.
33997c478bd9Sstevel@tonic-gate 		 *
34007c478bd9Sstevel@tonic-gate 		 * If DEVMAP_MAPPING_INVALID is specified, we have to grant
34017c478bd9Sstevel@tonic-gate 		 * remap permission.
34027c478bd9Sstevel@tonic-gate 		 */
34037c478bd9Sstevel@tonic-gate 		if (!(flags & DEVMAP_ALLOW_REMAP)) {
34047c478bd9Sstevel@tonic-gate 			return (DDI_FAILURE);
34057c478bd9Sstevel@tonic-gate 		}
34067c478bd9Sstevel@tonic-gate 		dhp->dh_pfn = PFN_INVALID;
34077c478bd9Sstevel@tonic-gate 	} else {
34087c478bd9Sstevel@tonic-gate 		handle = impl_acc_hdl_alloc(KM_SLEEP, NULL);
34097c478bd9Sstevel@tonic-gate 		if (handle == NULL)
34107c478bd9Sstevel@tonic-gate 			return (DDI_FAILURE);
34117c478bd9Sstevel@tonic-gate 
34127c478bd9Sstevel@tonic-gate 		hp = impl_acc_hdl_get(handle);
34137c478bd9Sstevel@tonic-gate 		hp->ah_vers = VERS_ACCHDL;
34147c478bd9Sstevel@tonic-gate 		hp->ah_dip = dip;
34157c478bd9Sstevel@tonic-gate 		hp->ah_rnumber = rnumber;
34167c478bd9Sstevel@tonic-gate 		hp->ah_offset = roff;
34177c478bd9Sstevel@tonic-gate 		hp->ah_len = len;
34187c478bd9Sstevel@tonic-gate 		if (accattrp != NULL)
34197c478bd9Sstevel@tonic-gate 			hp->ah_acc = *accattrp;
34207c478bd9Sstevel@tonic-gate 
34217c478bd9Sstevel@tonic-gate 		mr.map_op = DDI_MO_MAP_LOCKED;
34227c478bd9Sstevel@tonic-gate 		mr.map_type = DDI_MT_RNUMBER;
34237c478bd9Sstevel@tonic-gate 		mr.map_obj.rnumber = rnumber;
34247c478bd9Sstevel@tonic-gate 		mr.map_prot = maxprot & dhp->dh_orig_maxprot;
34257c478bd9Sstevel@tonic-gate 		mr.map_flags = DDI_MF_DEVICE_MAPPING;
34267c478bd9Sstevel@tonic-gate 		mr.map_handlep = hp;
34277c478bd9Sstevel@tonic-gate 		mr.map_vers = DDI_MAP_VERSION;
34287c478bd9Sstevel@tonic-gate 
34297c478bd9Sstevel@tonic-gate 		/*
34307c478bd9Sstevel@tonic-gate 		 * up the device tree to get pfn.
34317c478bd9Sstevel@tonic-gate 		 * The rootnex_map_regspec() routine in nexus drivers has been
34327c478bd9Sstevel@tonic-gate 		 * modified to return pfn if map_flags is DDI_MF_DEVICE_MAPPING.
34337c478bd9Sstevel@tonic-gate 		 */
34347c478bd9Sstevel@tonic-gate 		err = ddi_map(dip, &mr, roff, len, (caddr_t *)&dhp->dh_pfn);
34357c478bd9Sstevel@tonic-gate 		dhp->dh_hat_attr = hp->ah_hat_flags;
34367c478bd9Sstevel@tonic-gate 		impl_acc_hdl_free(handle);
34377c478bd9Sstevel@tonic-gate 
34387c478bd9Sstevel@tonic-gate 		if (err)
34397c478bd9Sstevel@tonic-gate 			return (DDI_FAILURE);
34407c478bd9Sstevel@tonic-gate 	}
34417c478bd9Sstevel@tonic-gate 	/* Should not be using devmem setup for memory pages */
34427c478bd9Sstevel@tonic-gate 	ASSERT(!pf_is_memory(dhp->dh_pfn));
34437c478bd9Sstevel@tonic-gate 
34447c478bd9Sstevel@tonic-gate 	/* Only some of the flags bits are settable by the driver */
34457c478bd9Sstevel@tonic-gate 	dhp->dh_flags |= (flags & DEVMAP_SETUP_FLAGS);
34467c478bd9Sstevel@tonic-gate 	dhp->dh_len = ptob(btopr(len));
34477c478bd9Sstevel@tonic-gate 
34487c478bd9Sstevel@tonic-gate 	dhp->dh_cookie = DEVMAP_DEVMEM_COOKIE;
34497c478bd9Sstevel@tonic-gate 	dhp->dh_roff = ptob(btop(roff));
34507c478bd9Sstevel@tonic-gate 
34517c478bd9Sstevel@tonic-gate 	/* setup the dh_mmulevel and DEVMAP_FLAG_LARGE */
34527c478bd9Sstevel@tonic-gate 	devmap_devmem_large_page_setup(dhp);
34537c478bd9Sstevel@tonic-gate 	dhp->dh_maxprot = maxprot & dhp->dh_orig_maxprot;
34547c478bd9Sstevel@tonic-gate 	ASSERT((dhp->dh_prot & dhp->dh_orig_maxprot & maxprot) == dhp->dh_prot);
34557c478bd9Sstevel@tonic-gate 
34567c478bd9Sstevel@tonic-gate 
34577c478bd9Sstevel@tonic-gate 	if (callbackops != NULL) {
34587c478bd9Sstevel@tonic-gate 		bcopy(callbackops, &dhp->dh_callbackops,
34597c478bd9Sstevel@tonic-gate 		    sizeof (struct devmap_callback_ctl));
34607c478bd9Sstevel@tonic-gate 	}
34617c478bd9Sstevel@tonic-gate 
34627c478bd9Sstevel@tonic-gate 	/*
34637c478bd9Sstevel@tonic-gate 	 * Initialize dh_lock if we want to do remap.
34647c478bd9Sstevel@tonic-gate 	 */
34657c478bd9Sstevel@tonic-gate 	if (dhp->dh_flags & DEVMAP_ALLOW_REMAP) {
34667c478bd9Sstevel@tonic-gate 		mutex_init(&dhp->dh_lock, NULL, MUTEX_DEFAULT, NULL);
34677c478bd9Sstevel@tonic-gate 		dhp->dh_flags |= DEVMAP_LOCK_INITED;
34687c478bd9Sstevel@tonic-gate 	}
34697c478bd9Sstevel@tonic-gate 
34707c478bd9Sstevel@tonic-gate 	dhp->dh_flags |= DEVMAP_SETUP_DONE;
34717c478bd9Sstevel@tonic-gate 
34727c478bd9Sstevel@tonic-gate 	return (DDI_SUCCESS);
34737c478bd9Sstevel@tonic-gate }
34747c478bd9Sstevel@tonic-gate 
34757c478bd9Sstevel@tonic-gate int
34767c478bd9Sstevel@tonic-gate devmap_devmem_remap(devmap_cookie_t dhc, dev_info_t *dip,
34777c478bd9Sstevel@tonic-gate     uint_t rnumber, offset_t roff, size_t len, uint_t maxprot,
34787c478bd9Sstevel@tonic-gate     uint_t flags, ddi_device_acc_attr_t *accattrp)
34797c478bd9Sstevel@tonic-gate {
34807c478bd9Sstevel@tonic-gate 	devmap_handle_t *dhp = (devmap_handle_t *)dhc;
34817c478bd9Sstevel@tonic-gate 	ddi_acc_handle_t handle;
34827c478bd9Sstevel@tonic-gate 	ddi_map_req_t mr;
34837c478bd9Sstevel@tonic-gate 	ddi_acc_hdl_t *hp;
34847c478bd9Sstevel@tonic-gate 	pfn_t	pfn;
34857c478bd9Sstevel@tonic-gate 	uint_t	hat_flags;
34867c478bd9Sstevel@tonic-gate 	int	err;
34877c478bd9Sstevel@tonic-gate 
34887c478bd9Sstevel@tonic-gate 	TRACE_4(TR_FAC_DEVMAP, TR_DEVMAP_DEVMEM_REMAP,
34897c478bd9Sstevel@tonic-gate 	    "devmap_devmem_setup:start dhp=%p offset=%llx rnum=%d len=%lx",
34907c478bd9Sstevel@tonic-gate 	    (void *)dhp, roff, rnumber, (uint_t)len);
34917c478bd9Sstevel@tonic-gate 	DEBUGF(2, (CE_CONT, "devmap_devmem_remap: dhp %p offset %llx "
34927c478bd9Sstevel@tonic-gate 	    "rnum %d len %lx\n", (void *)dhp, roff, rnumber, len));
34937c478bd9Sstevel@tonic-gate 
34947c478bd9Sstevel@tonic-gate 	/*
34957c478bd9Sstevel@tonic-gate 	 * Return failure if setup has not been done or no remap permission
34967c478bd9Sstevel@tonic-gate 	 * has been granted during the setup.
34977c478bd9Sstevel@tonic-gate 	 */
34987c478bd9Sstevel@tonic-gate 	if ((dhp->dh_flags & DEVMAP_SETUP_DONE) == 0 ||
34997c478bd9Sstevel@tonic-gate 	    (dhp->dh_flags & DEVMAP_ALLOW_REMAP) == 0)
35007c478bd9Sstevel@tonic-gate 		return (DDI_FAILURE);
35017c478bd9Sstevel@tonic-gate 
35027c478bd9Sstevel@tonic-gate 	/* Only DEVMAP_MAPPING_INVALID flag supported for remap */
35037c478bd9Sstevel@tonic-gate 	if ((flags != 0) && (flags != DEVMAP_MAPPING_INVALID))
35047c478bd9Sstevel@tonic-gate 		return (DDI_FAILURE);
35057c478bd9Sstevel@tonic-gate 
35067c478bd9Sstevel@tonic-gate 	if ((dhp->dh_prot & dhp->dh_orig_maxprot & maxprot) != dhp->dh_prot)
35077c478bd9Sstevel@tonic-gate 		return (DDI_FAILURE);
35087c478bd9Sstevel@tonic-gate 
35097c478bd9Sstevel@tonic-gate 	if (!(flags & DEVMAP_MAPPING_INVALID)) {
35107c478bd9Sstevel@tonic-gate 		handle = impl_acc_hdl_alloc(KM_SLEEP, NULL);
35117c478bd9Sstevel@tonic-gate 		if (handle == NULL)
35127c478bd9Sstevel@tonic-gate 			return (DDI_FAILURE);
35137c478bd9Sstevel@tonic-gate 	}
35147c478bd9Sstevel@tonic-gate 
35157c478bd9Sstevel@tonic-gate 	HOLD_DHP_LOCK(dhp);
35167c478bd9Sstevel@tonic-gate 
35177c478bd9Sstevel@tonic-gate 	/*
35187c478bd9Sstevel@tonic-gate 	 * Unload the old mapping, so next fault will setup the new mappings
35197c478bd9Sstevel@tonic-gate 	 * Do this while holding the dhp lock so other faults dont reestablish
35207c478bd9Sstevel@tonic-gate 	 * the mappings
35217c478bd9Sstevel@tonic-gate 	 */
35227c478bd9Sstevel@tonic-gate 	hat_unload(dhp->dh_seg->s_as->a_hat, dhp->dh_uvaddr,
35237c478bd9Sstevel@tonic-gate 		dhp->dh_len, HAT_UNLOAD|HAT_UNLOAD_OTHER);
35247c478bd9Sstevel@tonic-gate 
35257c478bd9Sstevel@tonic-gate 	if (flags & DEVMAP_MAPPING_INVALID) {
35267c478bd9Sstevel@tonic-gate 		dhp->dh_flags |= DEVMAP_MAPPING_INVALID;
35277c478bd9Sstevel@tonic-gate 		dhp->dh_pfn = PFN_INVALID;
35287c478bd9Sstevel@tonic-gate 	} else {
35297c478bd9Sstevel@tonic-gate 		/* clear any prior DEVMAP_MAPPING_INVALID flag */
35307c478bd9Sstevel@tonic-gate 		dhp->dh_flags &= ~DEVMAP_MAPPING_INVALID;
35317c478bd9Sstevel@tonic-gate 		hp = impl_acc_hdl_get(handle);
35327c478bd9Sstevel@tonic-gate 		hp->ah_vers = VERS_ACCHDL;
35337c478bd9Sstevel@tonic-gate 		hp->ah_dip = dip;
35347c478bd9Sstevel@tonic-gate 		hp->ah_rnumber = rnumber;
35357c478bd9Sstevel@tonic-gate 		hp->ah_offset = roff;
35367c478bd9Sstevel@tonic-gate 		hp->ah_len = len;
35377c478bd9Sstevel@tonic-gate 		if (accattrp != NULL)
35387c478bd9Sstevel@tonic-gate 			hp->ah_acc = *accattrp;
35397c478bd9Sstevel@tonic-gate 
35407c478bd9Sstevel@tonic-gate 		mr.map_op = DDI_MO_MAP_LOCKED;
35417c478bd9Sstevel@tonic-gate 		mr.map_type = DDI_MT_RNUMBER;
35427c478bd9Sstevel@tonic-gate 		mr.map_obj.rnumber = rnumber;
35437c478bd9Sstevel@tonic-gate 		mr.map_prot = maxprot & dhp->dh_orig_maxprot;
35447c478bd9Sstevel@tonic-gate 		mr.map_flags = DDI_MF_DEVICE_MAPPING;
35457c478bd9Sstevel@tonic-gate 		mr.map_handlep = hp;
35467c478bd9Sstevel@tonic-gate 		mr.map_vers = DDI_MAP_VERSION;
35477c478bd9Sstevel@tonic-gate 
35487c478bd9Sstevel@tonic-gate 		/*
35497c478bd9Sstevel@tonic-gate 		 * up the device tree to get pfn.
35507c478bd9Sstevel@tonic-gate 		 * The rootnex_map_regspec() routine in nexus drivers has been
35517c478bd9Sstevel@tonic-gate 		 * modified to return pfn if map_flags is DDI_MF_DEVICE_MAPPING.
35527c478bd9Sstevel@tonic-gate 		 */
35537c478bd9Sstevel@tonic-gate 		err = ddi_map(dip, &mr, roff, len, (caddr_t *)&pfn);
35547c478bd9Sstevel@tonic-gate 		hat_flags = hp->ah_hat_flags;
35557c478bd9Sstevel@tonic-gate 		impl_acc_hdl_free(handle);
35567c478bd9Sstevel@tonic-gate 		if (err) {
35577c478bd9Sstevel@tonic-gate 			RELE_DHP_LOCK(dhp);
35587c478bd9Sstevel@tonic-gate 			return (DDI_FAILURE);
35597c478bd9Sstevel@tonic-gate 		}
35607c478bd9Sstevel@tonic-gate 		/*
35617c478bd9Sstevel@tonic-gate 		 * Store result of ddi_map first in local variables, as we do
35627c478bd9Sstevel@tonic-gate 		 * not want to overwrite the existing dhp with wrong data.
35637c478bd9Sstevel@tonic-gate 		 */
35647c478bd9Sstevel@tonic-gate 		dhp->dh_pfn = pfn;
35657c478bd9Sstevel@tonic-gate 		dhp->dh_hat_attr = hat_flags;
35667c478bd9Sstevel@tonic-gate 	}
35677c478bd9Sstevel@tonic-gate 
35687c478bd9Sstevel@tonic-gate 	/* clear the large page size flag */
35697c478bd9Sstevel@tonic-gate 	dhp->dh_flags &= ~DEVMAP_FLAG_LARGE;
35707c478bd9Sstevel@tonic-gate 
35717c478bd9Sstevel@tonic-gate 	dhp->dh_cookie = DEVMAP_DEVMEM_COOKIE;
35727c478bd9Sstevel@tonic-gate 	dhp->dh_roff = ptob(btop(roff));
35737c478bd9Sstevel@tonic-gate 
35747c478bd9Sstevel@tonic-gate 	/* setup the dh_mmulevel and DEVMAP_FLAG_LARGE */
35757c478bd9Sstevel@tonic-gate 	devmap_devmem_large_page_setup(dhp);
35767c478bd9Sstevel@tonic-gate 	dhp->dh_maxprot = maxprot & dhp->dh_orig_maxprot;
35777c478bd9Sstevel@tonic-gate 	ASSERT((dhp->dh_prot & dhp->dh_orig_maxprot & maxprot) == dhp->dh_prot);
35787c478bd9Sstevel@tonic-gate 
35797c478bd9Sstevel@tonic-gate 	RELE_DHP_LOCK(dhp);
35807c478bd9Sstevel@tonic-gate 	return (DDI_SUCCESS);
35817c478bd9Sstevel@tonic-gate }
35827c478bd9Sstevel@tonic-gate 
35837c478bd9Sstevel@tonic-gate /*
35847c478bd9Sstevel@tonic-gate  * called by driver devmap routine to pass kernel virtual address  mapping
35857c478bd9Sstevel@tonic-gate  * info to the framework.    used only for kernel memory
35867c478bd9Sstevel@tonic-gate  * allocated from ddi_umem_alloc().
35877c478bd9Sstevel@tonic-gate  */
35887c478bd9Sstevel@tonic-gate int
35897c478bd9Sstevel@tonic-gate devmap_umem_setup(devmap_cookie_t dhc, dev_info_t *dip,
35907c478bd9Sstevel@tonic-gate     struct devmap_callback_ctl *callbackops, ddi_umem_cookie_t cookie,
35917c478bd9Sstevel@tonic-gate     offset_t off, size_t len, uint_t maxprot, uint_t flags,
35927c478bd9Sstevel@tonic-gate     ddi_device_acc_attr_t *accattrp)
35937c478bd9Sstevel@tonic-gate {
35947c478bd9Sstevel@tonic-gate 	devmap_handle_t *dhp = (devmap_handle_t *)dhc;
35957c478bd9Sstevel@tonic-gate 	struct ddi_umem_cookie *cp = (struct ddi_umem_cookie *)cookie;
35967c478bd9Sstevel@tonic-gate 
35977c478bd9Sstevel@tonic-gate #ifdef lint
35987c478bd9Sstevel@tonic-gate 	dip = dip;
35997c478bd9Sstevel@tonic-gate 	accattrp = accattrp;
36007c478bd9Sstevel@tonic-gate #endif
36017c478bd9Sstevel@tonic-gate 
36027c478bd9Sstevel@tonic-gate 	TRACE_4(TR_FAC_DEVMAP, TR_DEVMAP_UMEM_SETUP,
36037c478bd9Sstevel@tonic-gate 	    "devmap_umem_setup:start dhp=%p offset=%llx cookie=%p len=%lx",
36047c478bd9Sstevel@tonic-gate 	    (void *)dhp, off, cookie, len);
36057c478bd9Sstevel@tonic-gate 	DEBUGF(2, (CE_CONT, "devmap_umem_setup: dhp %p offset %llx "
36067c478bd9Sstevel@tonic-gate 	    "cookie %p len %lx\n", (void *)dhp, off, (void *)cookie, len));
36077c478bd9Sstevel@tonic-gate 
36087c478bd9Sstevel@tonic-gate 	if (cookie == NULL)
36097c478bd9Sstevel@tonic-gate 		return (DDI_FAILURE);
36107c478bd9Sstevel@tonic-gate 
36117c478bd9Sstevel@tonic-gate 	/* For UMEM_TRASH, this restriction is not needed */
36127c478bd9Sstevel@tonic-gate 	if ((off + len) > cp->size)
36137c478bd9Sstevel@tonic-gate 		return (DDI_FAILURE);
36147c478bd9Sstevel@tonic-gate 
36157c478bd9Sstevel@tonic-gate 	/*
36167c478bd9Sstevel@tonic-gate 	 * First to check if this function has been called for this dhp.
36177c478bd9Sstevel@tonic-gate 	 */
36187c478bd9Sstevel@tonic-gate 	if (dhp->dh_flags & DEVMAP_SETUP_DONE)
36197c478bd9Sstevel@tonic-gate 		return (DDI_FAILURE);
36207c478bd9Sstevel@tonic-gate 
36217c478bd9Sstevel@tonic-gate 	if ((dhp->dh_prot & dhp->dh_orig_maxprot & maxprot) != dhp->dh_prot)
36227c478bd9Sstevel@tonic-gate 		return (DDI_FAILURE);
36237c478bd9Sstevel@tonic-gate 
36247c478bd9Sstevel@tonic-gate 	if (flags & DEVMAP_MAPPING_INVALID) {
36257c478bd9Sstevel@tonic-gate 		/*
36267c478bd9Sstevel@tonic-gate 		 * If DEVMAP_MAPPING_INVALID is specified, we have to grant
36277c478bd9Sstevel@tonic-gate 		 * remap permission.
36287c478bd9Sstevel@tonic-gate 		 */
36297c478bd9Sstevel@tonic-gate 		if (!(flags & DEVMAP_ALLOW_REMAP)) {
36307c478bd9Sstevel@tonic-gate 			return (DDI_FAILURE);
36317c478bd9Sstevel@tonic-gate 		}
36327c478bd9Sstevel@tonic-gate 	} else {
36337c478bd9Sstevel@tonic-gate 		dhp->dh_cookie = cookie;
36347c478bd9Sstevel@tonic-gate 		dhp->dh_roff = ptob(btop(off));
36357c478bd9Sstevel@tonic-gate 		dhp->dh_cvaddr = cp->cvaddr + dhp->dh_roff;
36367c478bd9Sstevel@tonic-gate 	}
36377c478bd9Sstevel@tonic-gate 
36387c478bd9Sstevel@tonic-gate 	/*
36397c478bd9Sstevel@tonic-gate 	 * The default is _not_ to pass HAT_LOAD_NOCONSIST to hat_devload();
36407c478bd9Sstevel@tonic-gate 	 * we pass HAT_LOAD_NOCONSIST _only_ in cases where hat tries to
36417c478bd9Sstevel@tonic-gate 	 * create consistent mappings but our intention was to create
36427c478bd9Sstevel@tonic-gate 	 * non-consistent mappings.
36437c478bd9Sstevel@tonic-gate 	 *
36447c478bd9Sstevel@tonic-gate 	 * DEVMEM: hat figures it out it's DEVMEM and creates non-consistent
36457c478bd9Sstevel@tonic-gate 	 * mappings.
36467c478bd9Sstevel@tonic-gate 	 *
36477c478bd9Sstevel@tonic-gate 	 * kernel exported memory: hat figures it out it's memory and always
36487c478bd9Sstevel@tonic-gate 	 * creates consistent mappings.
36497c478bd9Sstevel@tonic-gate 	 *
36507c478bd9Sstevel@tonic-gate 	 * /dev/mem: non-consistent mappings. See comments in common/io/mem.c
36517c478bd9Sstevel@tonic-gate 	 *
36527c478bd9Sstevel@tonic-gate 	 * /dev/kmem: consistent mappings are created unless they are
36537c478bd9Sstevel@tonic-gate 	 * MAP_FIXED. We _explicitly_ tell hat to create non-consistent
36547c478bd9Sstevel@tonic-gate 	 * mappings by passing HAT_LOAD_NOCONSIST in case of MAP_FIXED
36557c478bd9Sstevel@tonic-gate 	 * mappings of /dev/kmem. See common/io/mem.c
36567c478bd9Sstevel@tonic-gate 	 */
36577c478bd9Sstevel@tonic-gate 
36587c478bd9Sstevel@tonic-gate 	/* Only some of the flags bits are settable by the driver */
36597c478bd9Sstevel@tonic-gate 	dhp->dh_flags |= (flags & DEVMAP_SETUP_FLAGS);
36607c478bd9Sstevel@tonic-gate 
36617c478bd9Sstevel@tonic-gate 	dhp->dh_len = ptob(btopr(len));
36627c478bd9Sstevel@tonic-gate 	dhp->dh_maxprot = maxprot & dhp->dh_orig_maxprot;
36637c478bd9Sstevel@tonic-gate 	ASSERT((dhp->dh_prot & dhp->dh_orig_maxprot & maxprot) == dhp->dh_prot);
36647c478bd9Sstevel@tonic-gate 
36657c478bd9Sstevel@tonic-gate 	if (callbackops != NULL) {
36667c478bd9Sstevel@tonic-gate 		bcopy(callbackops, &dhp->dh_callbackops,
36677c478bd9Sstevel@tonic-gate 		    sizeof (struct devmap_callback_ctl));
36687c478bd9Sstevel@tonic-gate 	}
36697c478bd9Sstevel@tonic-gate 	/*
36707c478bd9Sstevel@tonic-gate 	 * Initialize dh_lock if we want to do remap.
36717c478bd9Sstevel@tonic-gate 	 */
36727c478bd9Sstevel@tonic-gate 	if (dhp->dh_flags & DEVMAP_ALLOW_REMAP) {
36737c478bd9Sstevel@tonic-gate 		mutex_init(&dhp->dh_lock, NULL, MUTEX_DEFAULT, NULL);
36747c478bd9Sstevel@tonic-gate 		dhp->dh_flags |= DEVMAP_LOCK_INITED;
36757c478bd9Sstevel@tonic-gate 	}
36767c478bd9Sstevel@tonic-gate 
36777c478bd9Sstevel@tonic-gate 	dhp->dh_flags |= DEVMAP_SETUP_DONE;
36787c478bd9Sstevel@tonic-gate 
36797c478bd9Sstevel@tonic-gate 	return (DDI_SUCCESS);
36807c478bd9Sstevel@tonic-gate }
36817c478bd9Sstevel@tonic-gate 
36827c478bd9Sstevel@tonic-gate int
36837c478bd9Sstevel@tonic-gate devmap_umem_remap(devmap_cookie_t dhc, dev_info_t *dip,
36847c478bd9Sstevel@tonic-gate     ddi_umem_cookie_t cookie, offset_t off, size_t len, uint_t maxprot,
36857c478bd9Sstevel@tonic-gate     uint_t flags, ddi_device_acc_attr_t *accattrp)
36867c478bd9Sstevel@tonic-gate {
36877c478bd9Sstevel@tonic-gate 	devmap_handle_t *dhp = (devmap_handle_t *)dhc;
36887c478bd9Sstevel@tonic-gate 	struct ddi_umem_cookie *cp = (struct ddi_umem_cookie *)cookie;
36897c478bd9Sstevel@tonic-gate 
36907c478bd9Sstevel@tonic-gate 	TRACE_4(TR_FAC_DEVMAP, TR_DEVMAP_UMEM_REMAP,
36917c478bd9Sstevel@tonic-gate 	    "devmap_umem_remap:start dhp=%p offset=%llx cookie=%p len=%lx",
36927c478bd9Sstevel@tonic-gate 	    (void *)dhp, off, cookie, len);
36937c478bd9Sstevel@tonic-gate 	DEBUGF(2, (CE_CONT, "devmap_umem_remap: dhp %p offset %llx "
36947c478bd9Sstevel@tonic-gate 	    "cookie %p len %lx\n", (void *)dhp, off, (void *)cookie, len));
36957c478bd9Sstevel@tonic-gate 
36967c478bd9Sstevel@tonic-gate #ifdef lint
36977c478bd9Sstevel@tonic-gate 	dip = dip;
36987c478bd9Sstevel@tonic-gate 	accattrp = accattrp;
36997c478bd9Sstevel@tonic-gate #endif
37007c478bd9Sstevel@tonic-gate 	/*
37017c478bd9Sstevel@tonic-gate 	 * Reture failure if setup has not been done or no remap permission
37027c478bd9Sstevel@tonic-gate 	 * has been granted during the setup.
37037c478bd9Sstevel@tonic-gate 	 */
37047c478bd9Sstevel@tonic-gate 	if ((dhp->dh_flags & DEVMAP_SETUP_DONE) == 0 ||
37057c478bd9Sstevel@tonic-gate 		(dhp->dh_flags & DEVMAP_ALLOW_REMAP) == 0)
37067c478bd9Sstevel@tonic-gate 		return (DDI_FAILURE);
37077c478bd9Sstevel@tonic-gate 
37087c478bd9Sstevel@tonic-gate 	/* No flags supported for remap yet */
37097c478bd9Sstevel@tonic-gate 	if (flags != 0)
37107c478bd9Sstevel@tonic-gate 		return (DDI_FAILURE);
37117c478bd9Sstevel@tonic-gate 
37127c478bd9Sstevel@tonic-gate 	if ((dhp->dh_prot & dhp->dh_orig_maxprot & maxprot) != dhp->dh_prot)
37137c478bd9Sstevel@tonic-gate 		return (DDI_FAILURE);
37147c478bd9Sstevel@tonic-gate 
37157c478bd9Sstevel@tonic-gate 	/* For UMEM_TRASH, this restriction is not needed */
37167c478bd9Sstevel@tonic-gate 	if ((off + len) > cp->size)
37177c478bd9Sstevel@tonic-gate 		return (DDI_FAILURE);
37187c478bd9Sstevel@tonic-gate 
37197c478bd9Sstevel@tonic-gate 	HOLD_DHP_LOCK(dhp);
37207c478bd9Sstevel@tonic-gate 	/*
37217c478bd9Sstevel@tonic-gate 	 * Unload the old mapping, so next fault will setup the new mappings
37227c478bd9Sstevel@tonic-gate 	 * Do this while holding the dhp lock so other faults dont reestablish
37237c478bd9Sstevel@tonic-gate 	 * the mappings
37247c478bd9Sstevel@tonic-gate 	 */
37257c478bd9Sstevel@tonic-gate 	hat_unload(dhp->dh_seg->s_as->a_hat, dhp->dh_uvaddr,
37267c478bd9Sstevel@tonic-gate 		dhp->dh_len, HAT_UNLOAD|HAT_UNLOAD_OTHER);
37277c478bd9Sstevel@tonic-gate 
37287c478bd9Sstevel@tonic-gate 	dhp->dh_cookie = cookie;
37297c478bd9Sstevel@tonic-gate 	dhp->dh_roff = ptob(btop(off));
37307c478bd9Sstevel@tonic-gate 	dhp->dh_cvaddr = cp->cvaddr + dhp->dh_roff;
37317c478bd9Sstevel@tonic-gate 
37327c478bd9Sstevel@tonic-gate 	/* clear the large page size flag */
37337c478bd9Sstevel@tonic-gate 	dhp->dh_flags &= ~DEVMAP_FLAG_LARGE;
37347c478bd9Sstevel@tonic-gate 
37357c478bd9Sstevel@tonic-gate 	dhp->dh_maxprot = maxprot & dhp->dh_orig_maxprot;
37367c478bd9Sstevel@tonic-gate 	ASSERT((dhp->dh_prot & dhp->dh_orig_maxprot & maxprot) == dhp->dh_prot);
37377c478bd9Sstevel@tonic-gate 	RELE_DHP_LOCK(dhp);
37387c478bd9Sstevel@tonic-gate 	return (DDI_SUCCESS);
37397c478bd9Sstevel@tonic-gate }
37407c478bd9Sstevel@tonic-gate 
37417c478bd9Sstevel@tonic-gate /*
37427c478bd9Sstevel@tonic-gate  * to set timeout value for the driver's context management callback, e.g.
37437c478bd9Sstevel@tonic-gate  * devmap_access().
37447c478bd9Sstevel@tonic-gate  */
37457c478bd9Sstevel@tonic-gate void
37467c478bd9Sstevel@tonic-gate devmap_set_ctx_timeout(devmap_cookie_t dhc, clock_t ticks)
37477c478bd9Sstevel@tonic-gate {
37487c478bd9Sstevel@tonic-gate 	devmap_handle_t *dhp = (devmap_handle_t *)dhc;
37497c478bd9Sstevel@tonic-gate 
37507c478bd9Sstevel@tonic-gate 	TRACE_2(TR_FAC_DEVMAP, TR_DEVMAP_SET_CTX_TIMEOUT,
37517c478bd9Sstevel@tonic-gate 	    "devmap_set_ctx_timeout:start dhp=%p ticks=%x",
37527c478bd9Sstevel@tonic-gate 	    (void *)dhp, ticks);
37537c478bd9Sstevel@tonic-gate 	dhp->dh_timeout_length = ticks;
37547c478bd9Sstevel@tonic-gate }
37557c478bd9Sstevel@tonic-gate 
37567c478bd9Sstevel@tonic-gate int
37577c478bd9Sstevel@tonic-gate devmap_default_access(devmap_cookie_t dhp, void *pvtp, offset_t off,
37587c478bd9Sstevel@tonic-gate     size_t len, uint_t type, uint_t rw)
37597c478bd9Sstevel@tonic-gate {
37607c478bd9Sstevel@tonic-gate #ifdef lint
37617c478bd9Sstevel@tonic-gate 	pvtp = pvtp;
37627c478bd9Sstevel@tonic-gate #endif
37637c478bd9Sstevel@tonic-gate 
37647c478bd9Sstevel@tonic-gate 	TRACE_0(TR_FAC_DEVMAP, TR_DEVMAP_DEFAULT_ACCESS,
37657c478bd9Sstevel@tonic-gate 	    "devmap_default_access:start");
37667c478bd9Sstevel@tonic-gate 	return (devmap_load(dhp, off, len, type, rw));
37677c478bd9Sstevel@tonic-gate }
37687c478bd9Sstevel@tonic-gate 
37697c478bd9Sstevel@tonic-gate /*
37707c478bd9Sstevel@tonic-gate  * segkmem_alloc() wrapper to allocate memory which is both
37717c478bd9Sstevel@tonic-gate  * non-relocatable (for DR) and sharelocked, since the rest
37727c478bd9Sstevel@tonic-gate  * of this segment driver requires it.
37737c478bd9Sstevel@tonic-gate  */
37747c478bd9Sstevel@tonic-gate static void *
37757c478bd9Sstevel@tonic-gate devmap_alloc_pages(vmem_t *vmp, size_t size, int vmflag)
37767c478bd9Sstevel@tonic-gate {
37777c478bd9Sstevel@tonic-gate 	ASSERT(vmp != NULL);
37787c478bd9Sstevel@tonic-gate 	ASSERT(kvseg.s_base != NULL);
37797c478bd9Sstevel@tonic-gate 	vmflag |= (VM_NORELOC | SEGKMEM_SHARELOCKED);
37807c478bd9Sstevel@tonic-gate 	return (segkmem_alloc(vmp, size, vmflag));
37817c478bd9Sstevel@tonic-gate }
37827c478bd9Sstevel@tonic-gate 
37837c478bd9Sstevel@tonic-gate /*
37847c478bd9Sstevel@tonic-gate  * This is where things are a bit incestrous with seg_kmem: unlike
37857c478bd9Sstevel@tonic-gate  * seg_kp, seg_kmem does not keep its pages long-term sharelocked, so
37867c478bd9Sstevel@tonic-gate  * we need to do a bit of a dance around that to prevent duplication of
37877c478bd9Sstevel@tonic-gate  * code until we decide to bite the bullet and implement a new kernel
37887c478bd9Sstevel@tonic-gate  * segment for driver-allocated memory that is exported to user space.
37897c478bd9Sstevel@tonic-gate  */
37907c478bd9Sstevel@tonic-gate static void
37917c478bd9Sstevel@tonic-gate devmap_free_pages(vmem_t *vmp, void *inaddr, size_t size)
37927c478bd9Sstevel@tonic-gate {
37937c478bd9Sstevel@tonic-gate 	page_t *pp;
37947c478bd9Sstevel@tonic-gate 	caddr_t addr = inaddr;
37957c478bd9Sstevel@tonic-gate 	caddr_t eaddr;
37967c478bd9Sstevel@tonic-gate 	pgcnt_t npages = btopr(size);
37977c478bd9Sstevel@tonic-gate 
37987c478bd9Sstevel@tonic-gate 	ASSERT(vmp != NULL);
37997c478bd9Sstevel@tonic-gate 	ASSERT(kvseg.s_base != NULL);
38007c478bd9Sstevel@tonic-gate 	ASSERT(((uintptr_t)addr & PAGEOFFSET) == 0);
38017c478bd9Sstevel@tonic-gate 
38027c478bd9Sstevel@tonic-gate 	hat_unload(kas.a_hat, addr, size, HAT_UNLOAD_UNLOCK);
38037c478bd9Sstevel@tonic-gate 
38047c478bd9Sstevel@tonic-gate 	for (eaddr = addr + size; addr < eaddr; addr += PAGESIZE) {
38057c478bd9Sstevel@tonic-gate 		/*
38067c478bd9Sstevel@tonic-gate 		 * Use page_find() instead of page_lookup() to find the page
38077c478bd9Sstevel@tonic-gate 		 * since we know that it is hashed and has a shared lock.
38087c478bd9Sstevel@tonic-gate 		 */
38097c478bd9Sstevel@tonic-gate 		pp = page_find(&kvp, (u_offset_t)(uintptr_t)addr);
38107c478bd9Sstevel@tonic-gate 
38117c478bd9Sstevel@tonic-gate 		if (pp == NULL)
38127c478bd9Sstevel@tonic-gate 			panic("devmap_free_pages: page not found");
38137c478bd9Sstevel@tonic-gate 		if (!page_tryupgrade(pp)) {
38147c478bd9Sstevel@tonic-gate 			page_unlock(pp);
38157c478bd9Sstevel@tonic-gate 			pp = page_lookup(&kvp, (u_offset_t)(uintptr_t)addr,
38167c478bd9Sstevel@tonic-gate 			    SE_EXCL);
38177c478bd9Sstevel@tonic-gate 			if (pp == NULL)
38187c478bd9Sstevel@tonic-gate 				panic("devmap_free_pages: page already freed");
38197c478bd9Sstevel@tonic-gate 		}
38207c478bd9Sstevel@tonic-gate 		/* Clear p_lckcnt so page_destroy() doesn't update availrmem */
38217c478bd9Sstevel@tonic-gate 		pp->p_lckcnt = 0;
38227c478bd9Sstevel@tonic-gate 		page_destroy(pp, 0);
38237c478bd9Sstevel@tonic-gate 	}
38247c478bd9Sstevel@tonic-gate 	page_unresv(npages);
38257c478bd9Sstevel@tonic-gate 
38267c478bd9Sstevel@tonic-gate 	if (vmp != NULL)
38277c478bd9Sstevel@tonic-gate 		vmem_free(vmp, inaddr, size);
38287c478bd9Sstevel@tonic-gate }
38297c478bd9Sstevel@tonic-gate 
38307c478bd9Sstevel@tonic-gate /*
38317c478bd9Sstevel@tonic-gate  * devmap_umem_alloc_np() replaces kmem_zalloc() as the method for
38327c478bd9Sstevel@tonic-gate  * allocating non-pageable kmem in response to a ddi_umem_alloc()
38337c478bd9Sstevel@tonic-gate  * default request. For now we allocate our own pages and we keep
38347c478bd9Sstevel@tonic-gate  * them long-term sharelocked, since: A) the fault routines expect the
38357c478bd9Sstevel@tonic-gate  * memory to already be locked; B) pageable umem is already long-term
38367c478bd9Sstevel@tonic-gate  * locked; C) it's a lot of work to make it otherwise, particuarly
38377c478bd9Sstevel@tonic-gate  * since the nexus layer expects the pages to never fault. An RFE is to
38387c478bd9Sstevel@tonic-gate  * not keep the pages long-term locked, but instead to be able to
38397c478bd9Sstevel@tonic-gate  * take faults on them and simply look them up in kvp in case we
38407c478bd9Sstevel@tonic-gate  * fault on them. Even then, we must take care not to let pageout
38417c478bd9Sstevel@tonic-gate  * steal them from us since the data must remain resident; if we
38427c478bd9Sstevel@tonic-gate  * do this we must come up with some way to pin the pages to prevent
38437c478bd9Sstevel@tonic-gate  * faults while a driver is doing DMA to/from them.
38447c478bd9Sstevel@tonic-gate  */
38457c478bd9Sstevel@tonic-gate static void *
38467c478bd9Sstevel@tonic-gate devmap_umem_alloc_np(size_t size, size_t flags)
38477c478bd9Sstevel@tonic-gate {
38487c478bd9Sstevel@tonic-gate 	void *buf;
38497c478bd9Sstevel@tonic-gate 	int vmflags = (flags & DDI_UMEM_NOSLEEP)? VM_NOSLEEP : VM_SLEEP;
38507c478bd9Sstevel@tonic-gate 
38517c478bd9Sstevel@tonic-gate 	buf = vmem_alloc(umem_np_arena, size, vmflags);
38527c478bd9Sstevel@tonic-gate 	if (buf != NULL)
38537c478bd9Sstevel@tonic-gate 		bzero(buf, size);
38547c478bd9Sstevel@tonic-gate 	return (buf);
38557c478bd9Sstevel@tonic-gate }
38567c478bd9Sstevel@tonic-gate 
38577c478bd9Sstevel@tonic-gate static void
38587c478bd9Sstevel@tonic-gate devmap_umem_free_np(void *addr, size_t size)
38597c478bd9Sstevel@tonic-gate {
38607c478bd9Sstevel@tonic-gate 	vmem_free(umem_np_arena, addr, size);
38617c478bd9Sstevel@tonic-gate }
38627c478bd9Sstevel@tonic-gate 
38637c478bd9Sstevel@tonic-gate /*
38647c478bd9Sstevel@tonic-gate  * allocate page aligned kernel memory for exporting to user land.
38657c478bd9Sstevel@tonic-gate  * The devmap framework will use the cookie allocated by ddi_umem_alloc()
38667c478bd9Sstevel@tonic-gate  * to find a user virtual address that is in same color as the address
38677c478bd9Sstevel@tonic-gate  * allocated here.
38687c478bd9Sstevel@tonic-gate  */
38697c478bd9Sstevel@tonic-gate void *
38707c478bd9Sstevel@tonic-gate ddi_umem_alloc(size_t size, int flags, ddi_umem_cookie_t *cookie)
38717c478bd9Sstevel@tonic-gate {
38727c478bd9Sstevel@tonic-gate 	register size_t len = ptob(btopr(size));
38737c478bd9Sstevel@tonic-gate 	void *buf = NULL;
38747c478bd9Sstevel@tonic-gate 	struct ddi_umem_cookie *cp;
38757c478bd9Sstevel@tonic-gate 	int iflags = 0;
38767c478bd9Sstevel@tonic-gate 
38777c478bd9Sstevel@tonic-gate 	*cookie = NULL;
38787c478bd9Sstevel@tonic-gate 
38797c478bd9Sstevel@tonic-gate 	TRACE_0(TR_FAC_DEVMAP, TR_DEVMAP_UMEM_ALLOC,
38807c478bd9Sstevel@tonic-gate 	    "devmap_umem_alloc:start");
38817c478bd9Sstevel@tonic-gate 	if (len == 0)
38827c478bd9Sstevel@tonic-gate 		return ((void *)NULL);
38837c478bd9Sstevel@tonic-gate 
38847c478bd9Sstevel@tonic-gate 	/*
38857c478bd9Sstevel@tonic-gate 	 * allocate cookie
38867c478bd9Sstevel@tonic-gate 	 */
38877c478bd9Sstevel@tonic-gate 	if ((cp = kmem_zalloc(sizeof (struct ddi_umem_cookie),
38887c478bd9Sstevel@tonic-gate 		flags & DDI_UMEM_NOSLEEP ? KM_NOSLEEP : KM_SLEEP)) == NULL) {
38897c478bd9Sstevel@tonic-gate 		ASSERT(flags & DDI_UMEM_NOSLEEP);
38907c478bd9Sstevel@tonic-gate 		return ((void *)NULL);
38917c478bd9Sstevel@tonic-gate 	}
38927c478bd9Sstevel@tonic-gate 
38937c478bd9Sstevel@tonic-gate 	if (flags & DDI_UMEM_PAGEABLE) {
38947c478bd9Sstevel@tonic-gate 		/* Only one of the flags is allowed */
38957c478bd9Sstevel@tonic-gate 		ASSERT(!(flags & DDI_UMEM_TRASH));
38967c478bd9Sstevel@tonic-gate 		/* initialize resource with 0 */
38977c478bd9Sstevel@tonic-gate 		iflags = KPD_ZERO;
38987c478bd9Sstevel@tonic-gate 
38997c478bd9Sstevel@tonic-gate 		/*
39007c478bd9Sstevel@tonic-gate 		 * to allocate unlocked pageable memory, use segkp_get() to
39017c478bd9Sstevel@tonic-gate 		 * create a segkp segment.  Since segkp can only service kas,
39027c478bd9Sstevel@tonic-gate 		 * other segment drivers such as segdev have to do
39037c478bd9Sstevel@tonic-gate 		 * as_fault(segkp, SOFTLOCK) in its fault routine,
39047c478bd9Sstevel@tonic-gate 		 */
39057c478bd9Sstevel@tonic-gate 		if (flags & DDI_UMEM_NOSLEEP)
39067c478bd9Sstevel@tonic-gate 			iflags |= KPD_NOWAIT;
39077c478bd9Sstevel@tonic-gate 
39087c478bd9Sstevel@tonic-gate 		if ((buf = segkp_get(segkp, len, iflags)) == NULL) {
39097c478bd9Sstevel@tonic-gate 			kmem_free(cp, sizeof (struct ddi_umem_cookie));
39107c478bd9Sstevel@tonic-gate 			return ((void *)NULL);
39117c478bd9Sstevel@tonic-gate 		}
39127c478bd9Sstevel@tonic-gate 		cp->type = KMEM_PAGEABLE;
39137c478bd9Sstevel@tonic-gate 		mutex_init(&cp->lock, NULL, MUTEX_DEFAULT, NULL);
39147c478bd9Sstevel@tonic-gate 		cp->locked = 0;
39157c478bd9Sstevel@tonic-gate 	} else if (flags & DDI_UMEM_TRASH) {
39167c478bd9Sstevel@tonic-gate 		/* Only one of the flags is allowed */
39177c478bd9Sstevel@tonic-gate 		ASSERT(!(flags & DDI_UMEM_PAGEABLE));
39187c478bd9Sstevel@tonic-gate 		cp->type = UMEM_TRASH;
39197c478bd9Sstevel@tonic-gate 		buf = NULL;
39207c478bd9Sstevel@tonic-gate 	} else {
39217c478bd9Sstevel@tonic-gate 		if ((buf = devmap_umem_alloc_np(len, flags)) == NULL) {
39227c478bd9Sstevel@tonic-gate 			kmem_free(cp, sizeof (struct ddi_umem_cookie));
39237c478bd9Sstevel@tonic-gate 			return ((void *)NULL);
39247c478bd9Sstevel@tonic-gate 		}
39257c478bd9Sstevel@tonic-gate 
39267c478bd9Sstevel@tonic-gate 		cp->type = KMEM_NON_PAGEABLE;
39277c478bd9Sstevel@tonic-gate 	}
39287c478bd9Sstevel@tonic-gate 
39297c478bd9Sstevel@tonic-gate 	/*
39307c478bd9Sstevel@tonic-gate 	 * need to save size here.  size will be used when
39317c478bd9Sstevel@tonic-gate 	 * we do kmem_free.
39327c478bd9Sstevel@tonic-gate 	 */
39337c478bd9Sstevel@tonic-gate 	cp->size = len;
39347c478bd9Sstevel@tonic-gate 	cp->cvaddr = (caddr_t)buf;
39357c478bd9Sstevel@tonic-gate 
39367c478bd9Sstevel@tonic-gate 	*cookie =  (void *)cp;
39377c478bd9Sstevel@tonic-gate 	return (buf);
39387c478bd9Sstevel@tonic-gate }
39397c478bd9Sstevel@tonic-gate 
39407c478bd9Sstevel@tonic-gate void
39417c478bd9Sstevel@tonic-gate ddi_umem_free(ddi_umem_cookie_t cookie)
39427c478bd9Sstevel@tonic-gate {
39437c478bd9Sstevel@tonic-gate 	struct ddi_umem_cookie *cp;
39447c478bd9Sstevel@tonic-gate 
39457c478bd9Sstevel@tonic-gate 	TRACE_0(TR_FAC_DEVMAP, TR_DEVMAP_UMEM_FREE,
39467c478bd9Sstevel@tonic-gate 	    "devmap_umem_free:start");
39477c478bd9Sstevel@tonic-gate 
39487c478bd9Sstevel@tonic-gate 	/*
39497c478bd9Sstevel@tonic-gate 	 * if cookie is NULL, no effects on the system
39507c478bd9Sstevel@tonic-gate 	 */
39517c478bd9Sstevel@tonic-gate 	if (cookie == NULL)
39527c478bd9Sstevel@tonic-gate 		return;
39537c478bd9Sstevel@tonic-gate 
39547c478bd9Sstevel@tonic-gate 	cp = (struct ddi_umem_cookie *)cookie;
39557c478bd9Sstevel@tonic-gate 
39567c478bd9Sstevel@tonic-gate 	switch (cp->type) {
39577c478bd9Sstevel@tonic-gate 	case KMEM_PAGEABLE :
39587c478bd9Sstevel@tonic-gate 		ASSERT(cp->cvaddr != NULL && cp->size != 0);
39597c478bd9Sstevel@tonic-gate 		/*
39607c478bd9Sstevel@tonic-gate 		 * Check if there are still any pending faults on the cookie
39617c478bd9Sstevel@tonic-gate 		 * while the driver is deleting it,
39627c478bd9Sstevel@tonic-gate 		 * XXX - could change to an ASSERT but wont catch errant drivers
39637c478bd9Sstevel@tonic-gate 		 */
39647c478bd9Sstevel@tonic-gate 		mutex_enter(&cp->lock);
39657c478bd9Sstevel@tonic-gate 		if (cp->locked) {
39667c478bd9Sstevel@tonic-gate 			mutex_exit(&cp->lock);
39677c478bd9Sstevel@tonic-gate 			panic("ddi_umem_free for cookie with pending faults %p",
39687c478bd9Sstevel@tonic-gate 			    (void *)cp);
39697c478bd9Sstevel@tonic-gate 			return;
39707c478bd9Sstevel@tonic-gate 		}
39717c478bd9Sstevel@tonic-gate 
39727c478bd9Sstevel@tonic-gate 		segkp_release(segkp, cp->cvaddr);
39737c478bd9Sstevel@tonic-gate 
39747c478bd9Sstevel@tonic-gate 		/*
39757c478bd9Sstevel@tonic-gate 		 * release mutex associated with this cookie.
39767c478bd9Sstevel@tonic-gate 		 */
39777c478bd9Sstevel@tonic-gate 		mutex_destroy(&cp->lock);
39787c478bd9Sstevel@tonic-gate 		break;
39797c478bd9Sstevel@tonic-gate 	case KMEM_NON_PAGEABLE :
39807c478bd9Sstevel@tonic-gate 		ASSERT(cp->cvaddr != NULL && cp->size != 0);
39817c478bd9Sstevel@tonic-gate 		devmap_umem_free_np(cp->cvaddr, cp->size);
39827c478bd9Sstevel@tonic-gate 		break;
39837c478bd9Sstevel@tonic-gate 	case UMEM_TRASH :
39847c478bd9Sstevel@tonic-gate 		break;
39857c478bd9Sstevel@tonic-gate 	case UMEM_LOCKED :
39867c478bd9Sstevel@tonic-gate 		/* Callers should use ddi_umem_unlock for this type */
39877c478bd9Sstevel@tonic-gate 		ddi_umem_unlock(cookie);
39887c478bd9Sstevel@tonic-gate 		/* Frees the cookie too */
39897c478bd9Sstevel@tonic-gate 		return;
39907c478bd9Sstevel@tonic-gate 	default:
39917c478bd9Sstevel@tonic-gate 		/* panic so we can diagnose the underlying cause */
39927c478bd9Sstevel@tonic-gate 		panic("ddi_umem_free: illegal cookie type 0x%x\n",
39937c478bd9Sstevel@tonic-gate 		    cp->type);
39947c478bd9Sstevel@tonic-gate 	}
39957c478bd9Sstevel@tonic-gate 
39967c478bd9Sstevel@tonic-gate 	kmem_free(cookie, sizeof (struct ddi_umem_cookie));
39977c478bd9Sstevel@tonic-gate }
39987c478bd9Sstevel@tonic-gate 
39997c478bd9Sstevel@tonic-gate 
40007c478bd9Sstevel@tonic-gate static int
40017c478bd9Sstevel@tonic-gate segdev_getmemid(struct seg *seg, caddr_t addr, memid_t *memidp)
40027c478bd9Sstevel@tonic-gate {
40037c478bd9Sstevel@tonic-gate 	struct segdev_data *sdp = (struct segdev_data *)seg->s_data;
40047c478bd9Sstevel@tonic-gate 
40057c478bd9Sstevel@tonic-gate 	/*
40067c478bd9Sstevel@tonic-gate 	 * It looks as if it is always mapped shared
40077c478bd9Sstevel@tonic-gate 	 */
40087c478bd9Sstevel@tonic-gate 	TRACE_0(TR_FAC_DEVMAP, TR_DEVMAP_GETMEMID,
40097c478bd9Sstevel@tonic-gate 	    "segdev_getmemid:start");
40107c478bd9Sstevel@tonic-gate 	memidp->val[0] = (uintptr_t)VTOCVP(sdp->vp);
40117c478bd9Sstevel@tonic-gate 	memidp->val[1] = sdp->offset + (uintptr_t)(addr - seg->s_base);
40127c478bd9Sstevel@tonic-gate 	return (0);
40137c478bd9Sstevel@tonic-gate }
40147c478bd9Sstevel@tonic-gate 
40157c478bd9Sstevel@tonic-gate /*ARGSUSED*/
40167c478bd9Sstevel@tonic-gate static lgrp_mem_policy_info_t *
40177c478bd9Sstevel@tonic-gate segdev_getpolicy(struct seg *seg, caddr_t addr)
40187c478bd9Sstevel@tonic-gate {
40197c478bd9Sstevel@tonic-gate 	return (NULL);
40207c478bd9Sstevel@tonic-gate }
40217c478bd9Sstevel@tonic-gate 
4022*1bd5c35fSelowe /*ARGSUSED*/
4023*1bd5c35fSelowe static int
4024*1bd5c35fSelowe segdev_capable(struct seg *seg, segcapability_t capability)
4025*1bd5c35fSelowe {
4026*1bd5c35fSelowe 	return (0);
4027*1bd5c35fSelowe }
4028*1bd5c35fSelowe 
40297c478bd9Sstevel@tonic-gate /*
40307c478bd9Sstevel@tonic-gate  * ddi_umem_alloc() non-pageable quantum cache max size.
40317c478bd9Sstevel@tonic-gate  * This is just a SWAG.
40327c478bd9Sstevel@tonic-gate  */
40337c478bd9Sstevel@tonic-gate #define	DEVMAP_UMEM_QUANTUM	(8*PAGESIZE)
40347c478bd9Sstevel@tonic-gate 
40357c478bd9Sstevel@tonic-gate /*
40367c478bd9Sstevel@tonic-gate  * Initialize seg_dev from boot. This routine sets up the trash page
40377c478bd9Sstevel@tonic-gate  * and creates the umem_np_arena used to back non-pageable memory
40387c478bd9Sstevel@tonic-gate  * requests.
40397c478bd9Sstevel@tonic-gate  */
40407c478bd9Sstevel@tonic-gate void
40417c478bd9Sstevel@tonic-gate segdev_init(void)
40427c478bd9Sstevel@tonic-gate {
40437c478bd9Sstevel@tonic-gate 	struct seg kseg;
40447c478bd9Sstevel@tonic-gate 
40457c478bd9Sstevel@tonic-gate 	umem_np_arena = vmem_create("umem_np", NULL, 0, PAGESIZE,
40467c478bd9Sstevel@tonic-gate 	    devmap_alloc_pages, devmap_free_pages, heap_arena,
40477c478bd9Sstevel@tonic-gate 	    DEVMAP_UMEM_QUANTUM, VM_SLEEP);
40487c478bd9Sstevel@tonic-gate 
40497c478bd9Sstevel@tonic-gate 	kseg.s_as = &kas;
40507c478bd9Sstevel@tonic-gate 	trashpp = page_create_va(&trashvp, 0, PAGESIZE,
40517c478bd9Sstevel@tonic-gate 	    PG_NORELOC | PG_EXCL | PG_WAIT, &kseg, NULL);
40527c478bd9Sstevel@tonic-gate 	if (trashpp == NULL)
40537c478bd9Sstevel@tonic-gate 		panic("segdev_init: failed to create trash page");
40547c478bd9Sstevel@tonic-gate 	pagezero(trashpp, 0, PAGESIZE);
40557c478bd9Sstevel@tonic-gate 	page_downgrade(trashpp);
40567c478bd9Sstevel@tonic-gate }
40577c478bd9Sstevel@tonic-gate 
40587c478bd9Sstevel@tonic-gate /*
40597c478bd9Sstevel@tonic-gate  * Invoke platform-dependent support routines so that /proc can have
40607c478bd9Sstevel@tonic-gate  * the platform code deal with curious hardware.
40617c478bd9Sstevel@tonic-gate  */
40627c478bd9Sstevel@tonic-gate int
40637c478bd9Sstevel@tonic-gate segdev_copyfrom(struct seg *seg,
40647c478bd9Sstevel@tonic-gate     caddr_t uaddr, const void *devaddr, void *kaddr, size_t len)
40657c478bd9Sstevel@tonic-gate {
40667c478bd9Sstevel@tonic-gate 	struct segdev_data *sdp = (struct segdev_data *)seg->s_data;
40677c478bd9Sstevel@tonic-gate 	struct snode *sp = VTOS(VTOCVP(sdp->vp));
40687c478bd9Sstevel@tonic-gate 
40697c478bd9Sstevel@tonic-gate 	return (e_ddi_copyfromdev(sp->s_dip,
40707c478bd9Sstevel@tonic-gate 	    (off_t)(uaddr - seg->s_base), devaddr, kaddr, len));
40717c478bd9Sstevel@tonic-gate }
40727c478bd9Sstevel@tonic-gate 
40737c478bd9Sstevel@tonic-gate int
40747c478bd9Sstevel@tonic-gate segdev_copyto(struct seg *seg,
40757c478bd9Sstevel@tonic-gate     caddr_t uaddr, const void *kaddr, void *devaddr, size_t len)
40767c478bd9Sstevel@tonic-gate {
40777c478bd9Sstevel@tonic-gate 	struct segdev_data *sdp = (struct segdev_data *)seg->s_data;
40787c478bd9Sstevel@tonic-gate 	struct snode *sp = VTOS(VTOCVP(sdp->vp));
40797c478bd9Sstevel@tonic-gate 
40807c478bd9Sstevel@tonic-gate 	return (e_ddi_copytodev(sp->s_dip,
40817c478bd9Sstevel@tonic-gate 	    (off_t)(uaddr - seg->s_base), kaddr, devaddr, len));
40827c478bd9Sstevel@tonic-gate }
4083