xref: /titanic_51/usr/src/uts/common/vm/seg_dev.c (revision 24da5b34f49324ed742a340010ed5bd3d4e06625)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 
22 /*
23  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
28 /*	  All Rights Reserved  	*/
29 
30 /*
31  * University Copyright- Copyright (c) 1982, 1986, 1988
32  * The Regents of the University of California
33  * All Rights Reserved
34  *
35  * University Acknowledgment- Portions of this document are derived from
36  * software developed by the University of California, Berkeley, and its
37  * contributors.
38  */
39 
40 #pragma ident	"%Z%%M%	%I%	%E% SMI"
41 
42 /*
43  * VM - segment of a mapped device.
44  *
45  * This segment driver is used when mapping character special devices.
46  */
47 
48 #include <sys/types.h>
49 #include <sys/t_lock.h>
50 #include <sys/sysmacros.h>
51 #include <sys/vtrace.h>
52 #include <sys/systm.h>
53 #include <sys/vmsystm.h>
54 #include <sys/mman.h>
55 #include <sys/errno.h>
56 #include <sys/kmem.h>
57 #include <sys/cmn_err.h>
58 #include <sys/vnode.h>
59 #include <sys/proc.h>
60 #include <sys/conf.h>
61 #include <sys/debug.h>
62 #include <sys/ddidevmap.h>
63 #include <sys/ddi_implfuncs.h>
64 #include <sys/lgrp.h>
65 
66 #include <vm/page.h>
67 #include <vm/hat.h>
68 #include <vm/as.h>
69 #include <vm/seg.h>
70 #include <vm/seg_dev.h>
71 #include <vm/seg_kp.h>
72 #include <vm/seg_kmem.h>
73 #include <vm/vpage.h>
74 
75 #include <sys/sunddi.h>
76 #include <sys/esunddi.h>
77 #include <sys/fs/snode.h>
78 
79 
80 #if DEBUG
81 int segdev_debug;
82 #define	DEBUGF(level, args) { if (segdev_debug >= (level)) cmn_err args; }
83 #else
84 #define	DEBUGF(level, args)
85 #endif
86 
87 /* Default timeout for devmap context management */
88 #define	CTX_TIMEOUT_VALUE 0
89 
90 #define	HOLD_DHP_LOCK(dhp)  if (dhp->dh_flags & DEVMAP_ALLOW_REMAP) \
91 			{ mutex_enter(&dhp->dh_lock); }
92 
93 #define	RELE_DHP_LOCK(dhp) if (dhp->dh_flags & DEVMAP_ALLOW_REMAP) \
94 			{ mutex_exit(&dhp->dh_lock); }
95 
96 #define	round_down_p2(a, s)	((a) & ~((s) - 1))
97 #define	round_up_p2(a, s)	(((a) + (s) - 1) & ~((s) - 1))
98 
99 /*
100  * VA_PA_ALIGNED checks to see if both VA and PA are on pgsize boundary
101  * VA_PA_PGSIZE_ALIGNED check to see if VA is aligned with PA w.r.t. pgsize
102  */
103 #define	VA_PA_ALIGNED(uvaddr, paddr, pgsize)		\
104 	(((uvaddr | paddr) & (pgsize - 1)) == 0)
105 #define	VA_PA_PGSIZE_ALIGNED(uvaddr, paddr, pgsize)	\
106 	(((uvaddr ^ paddr) & (pgsize - 1)) == 0)
107 
108 #define	vpgtob(n)	((n) * sizeof (struct vpage))	/* For brevity */
109 
110 #define	VTOCVP(vp)	(VTOS(vp)->s_commonvp)	/* we "know" it's an snode */
111 
112 static struct devmap_ctx *devmapctx_list = NULL;
113 static struct devmap_softlock *devmap_slist = NULL;
114 
115 /*
116  * mutex, vnode and page for the page of zeros we use for the trash mappings.
117  * One trash page is allocated on the first ddi_umem_setup call that uses it
118  * XXX Eventually, we may want to combine this with what segnf does when all
119  * hat layers implement HAT_NOFAULT.
120  *
121  * The trash page is used when the backing store for a userland mapping is
122  * removed but the application semantics do not take kindly to a SIGBUS.
123  * In that scenario, the applications pages are mapped to some dummy page
124  * which returns garbage on read and writes go into a common place.
125  * (Perfect for NO_FAULT semantics)
126  * The device driver is responsible to communicating to the app with some
127  * other mechanism that such remapping has happened and the app should take
128  * corrective action.
129  * We can also use an anonymous memory page as there is no requirement to
130  * keep the page locked, however this complicates the fault code. RFE.
131  */
132 static struct vnode trashvp;
133 static struct page *trashpp;
134 
135 /* Non-pageable kernel memory is allocated from the umem_np_arena. */
136 static vmem_t *umem_np_arena;
137 
138 /* Set the cookie to a value we know will never be a valid umem_cookie */
139 #define	DEVMAP_DEVMEM_COOKIE	((ddi_umem_cookie_t)0x1)
140 
141 /*
142  * Macros to check if type of devmap handle
143  */
144 #define	cookie_is_devmem(c)	\
145 	((c) == (struct ddi_umem_cookie *)DEVMAP_DEVMEM_COOKIE)
146 
147 #define	cookie_is_pmem(c)	\
148 	((c) == (struct ddi_umem_cookie *)DEVMAP_PMEM_COOKIE)
149 
150 #define	cookie_is_kpmem(c)	(!cookie_is_devmem(c) && !cookie_is_pmem(c) &&\
151 	((c)->type == KMEM_PAGEABLE))
152 
153 #define	dhp_is_devmem(dhp)	\
154 	(cookie_is_devmem((struct ddi_umem_cookie *)((dhp)->dh_cookie)))
155 
156 #define	dhp_is_pmem(dhp)	\
157 	(cookie_is_pmem((struct ddi_umem_cookie *)((dhp)->dh_cookie)))
158 
159 #define	dhp_is_kpmem(dhp)	\
160 	(cookie_is_kpmem((struct ddi_umem_cookie *)((dhp)->dh_cookie)))
161 
162 /*
163  * Private seg op routines.
164  */
165 static int	segdev_dup(struct seg *, struct seg *);
166 static int	segdev_unmap(struct seg *, caddr_t, size_t);
167 static void	segdev_free(struct seg *);
168 static faultcode_t segdev_fault(struct hat *, struct seg *, caddr_t, size_t,
169 		    enum fault_type, enum seg_rw);
170 static faultcode_t segdev_faulta(struct seg *, caddr_t);
171 static int	segdev_setprot(struct seg *, caddr_t, size_t, uint_t);
172 static int	segdev_checkprot(struct seg *, caddr_t, size_t, uint_t);
173 static void	segdev_badop(void);
174 static int	segdev_sync(struct seg *, caddr_t, size_t, int, uint_t);
175 static size_t	segdev_incore(struct seg *, caddr_t, size_t, char *);
176 static int	segdev_lockop(struct seg *, caddr_t, size_t, int, int,
177 		    ulong_t *, size_t);
178 static int	segdev_getprot(struct seg *, caddr_t, size_t, uint_t *);
179 static u_offset_t	segdev_getoffset(struct seg *, caddr_t);
180 static int	segdev_gettype(struct seg *, caddr_t);
181 static int	segdev_getvp(struct seg *, caddr_t, struct vnode **);
182 static int	segdev_advise(struct seg *, caddr_t, size_t, uint_t);
183 static void	segdev_dump(struct seg *);
184 static int	segdev_pagelock(struct seg *, caddr_t, size_t,
185 		    struct page ***, enum lock_type, enum seg_rw);
186 static int	segdev_setpagesize(struct seg *, caddr_t, size_t, uint_t);
187 static int	segdev_getmemid(struct seg *, caddr_t, memid_t *);
188 static lgrp_mem_policy_info_t	*segdev_getpolicy(struct seg *, caddr_t);
189 static int	segdev_capable(struct seg *, segcapability_t);
190 
191 /*
192  * XXX	this struct is used by rootnex_map_fault to identify
193  *	the segment it has been passed. So if you make it
194  *	"static" you'll need to fix rootnex_map_fault.
195  */
196 struct seg_ops segdev_ops = {
197 	segdev_dup,
198 	segdev_unmap,
199 	segdev_free,
200 	segdev_fault,
201 	segdev_faulta,
202 	segdev_setprot,
203 	segdev_checkprot,
204 	(int (*)())segdev_badop,	/* kluster */
205 	(size_t (*)(struct seg *))NULL,	/* swapout */
206 	segdev_sync,			/* sync */
207 	segdev_incore,
208 	segdev_lockop,			/* lockop */
209 	segdev_getprot,
210 	segdev_getoffset,
211 	segdev_gettype,
212 	segdev_getvp,
213 	segdev_advise,
214 	segdev_dump,
215 	segdev_pagelock,
216 	segdev_setpagesize,
217 	segdev_getmemid,
218 	segdev_getpolicy,
219 	segdev_capable,
220 };
221 
222 /*
223  * Private segdev support routines
224  */
225 static struct segdev_data *sdp_alloc(void);
226 
227 static void segdev_softunlock(struct hat *, struct seg *, caddr_t,
228     size_t, enum seg_rw);
229 
230 static faultcode_t segdev_faultpage(struct hat *, struct seg *, caddr_t,
231     struct vpage *, enum fault_type, enum seg_rw, devmap_handle_t *);
232 
233 static faultcode_t segdev_faultpages(struct hat *, struct seg *, caddr_t,
234     size_t, enum fault_type, enum seg_rw, devmap_handle_t *);
235 
236 static struct devmap_ctx *devmap_ctxinit(dev_t, ulong_t);
237 static struct devmap_softlock *devmap_softlock_init(dev_t, ulong_t);
238 static void devmap_softlock_rele(devmap_handle_t *);
239 static void devmap_ctx_rele(devmap_handle_t *);
240 
241 static void devmap_ctxto(void *);
242 
243 static devmap_handle_t *devmap_find_handle(devmap_handle_t *dhp_head,
244     caddr_t addr);
245 
246 static ulong_t devmap_roundup(devmap_handle_t *dhp, ulong_t offset, size_t len,
247     ulong_t *opfn, ulong_t *pagesize);
248 
249 static void free_devmap_handle(devmap_handle_t *dhp);
250 
251 static int devmap_handle_dup(devmap_handle_t *dhp, devmap_handle_t **new_dhp,
252     struct seg *newseg);
253 
254 static devmap_handle_t *devmap_handle_unmap(devmap_handle_t *dhp);
255 
256 static void devmap_handle_unmap_head(devmap_handle_t *dhp, size_t len);
257 
258 static void devmap_handle_unmap_tail(devmap_handle_t *dhp, caddr_t addr);
259 
260 static int devmap_device(devmap_handle_t *dhp, struct as *as, caddr_t *addr,
261     offset_t off, size_t len, uint_t flags);
262 
263 static void devmap_get_large_pgsize(devmap_handle_t *dhp, size_t len,
264     caddr_t addr, size_t *llen, caddr_t *laddr);
265 
266 static void devmap_handle_reduce_len(devmap_handle_t *dhp, size_t len);
267 
268 static void *devmap_alloc_pages(vmem_t *vmp, size_t size, int vmflag);
269 static void devmap_free_pages(vmem_t *vmp, void *inaddr, size_t size);
270 
271 static void *devmap_umem_alloc_np(size_t size, size_t flags);
272 static void devmap_umem_free_np(void *addr, size_t size);
273 
274 /*
275  * routines to lock and unlock underlying segkp segment for
276  * KMEM_PAGEABLE type cookies.
277  */
278 static faultcode_t  acquire_kpmem_lock(struct ddi_umem_cookie *, size_t);
279 static void release_kpmem_lock(struct ddi_umem_cookie *, size_t);
280 
281 /*
282  * Routines to synchronize F_SOFTLOCK and F_INVAL faults for
283  * drivers with devmap_access callbacks
284  */
285 static int devmap_softlock_enter(struct devmap_softlock *, size_t,
286 	enum fault_type);
287 static void devmap_softlock_exit(struct devmap_softlock *, size_t,
288 	enum fault_type);
289 
290 static kmutex_t devmapctx_lock;
291 
292 static kmutex_t devmap_slock;
293 
294 /*
295  * Initialize the thread callbacks and thread private data.
296  */
297 static struct devmap_ctx *
298 devmap_ctxinit(dev_t dev, ulong_t id)
299 {
300 	struct devmap_ctx	*devctx;
301 	struct devmap_ctx	*tmp;
302 	dev_info_t		*dip;
303 
304 	tmp =  kmem_zalloc(sizeof (struct devmap_ctx), KM_SLEEP);
305 
306 	mutex_enter(&devmapctx_lock);
307 
308 	dip = e_ddi_hold_devi_by_dev(dev, 0);
309 	ASSERT(dip != NULL);
310 	ddi_release_devi(dip);
311 
312 	for (devctx = devmapctx_list; devctx != NULL; devctx = devctx->next)
313 		if ((devctx->dip == dip) && (devctx->id == id))
314 			break;
315 
316 	if (devctx == NULL) {
317 		devctx = tmp;
318 		devctx->dip = dip;
319 		devctx->id = id;
320 		mutex_init(&devctx->lock, NULL, MUTEX_DEFAULT, NULL);
321 		cv_init(&devctx->cv, NULL, CV_DEFAULT, NULL);
322 		devctx->next = devmapctx_list;
323 		devmapctx_list = devctx;
324 	} else
325 		kmem_free(tmp, sizeof (struct devmap_ctx));
326 
327 	mutex_enter(&devctx->lock);
328 	devctx->refcnt++;
329 	mutex_exit(&devctx->lock);
330 	mutex_exit(&devmapctx_lock);
331 
332 	return (devctx);
333 }
334 
335 /*
336  * Timeout callback called if a CPU has not given up the device context
337  * within dhp->dh_timeout_length ticks
338  */
339 static void
340 devmap_ctxto(void *data)
341 {
342 	struct devmap_ctx *devctx = data;
343 
344 	TRACE_1(TR_FAC_DEVMAP, TR_DEVMAP_CTXTO,
345 	    "devmap_ctxto:timeout expired, devctx=%p", (void *)devctx);
346 	mutex_enter(&devctx->lock);
347 	/*
348 	 * Set oncpu = 0 so the next mapping trying to get the device context
349 	 * can.
350 	 */
351 	devctx->oncpu = 0;
352 	devctx->timeout = 0;
353 	cv_signal(&devctx->cv);
354 	mutex_exit(&devctx->lock);
355 }
356 
357 /*
358  * Create a device segment.
359  */
360 int
361 segdev_create(struct seg *seg, void *argsp)
362 {
363 	struct segdev_data *sdp;
364 	struct segdev_crargs *a = (struct segdev_crargs *)argsp;
365 	devmap_handle_t *dhp = (devmap_handle_t *)a->devmap_data;
366 	int error;
367 
368 	/*
369 	 * Since the address space is "write" locked, we
370 	 * don't need the segment lock to protect "segdev" data.
371 	 */
372 	ASSERT(seg->s_as && AS_WRITE_HELD(seg->s_as, &seg->s_as->a_lock));
373 
374 	hat_map(seg->s_as->a_hat, seg->s_base, seg->s_size, HAT_MAP);
375 
376 	sdp = sdp_alloc();
377 
378 	sdp->mapfunc = a->mapfunc;
379 	sdp->offset = a->offset;
380 	sdp->prot = a->prot;
381 	sdp->maxprot = a->maxprot;
382 	sdp->type = a->type;
383 	sdp->pageprot = 0;
384 	sdp->softlockcnt = 0;
385 	sdp->vpage = NULL;
386 
387 	if (sdp->mapfunc == NULL)
388 		sdp->devmap_data = dhp;
389 	else
390 		sdp->devmap_data = dhp = NULL;
391 
392 	sdp->hat_flags = a->hat_flags;
393 	sdp->hat_attr = a->hat_attr;
394 
395 	/*
396 	 * Currently, hat_flags supports only HAT_LOAD_NOCONSIST
397 	 */
398 	ASSERT(!(sdp->hat_flags & ~HAT_LOAD_NOCONSIST));
399 
400 	/*
401 	 * Hold shadow vnode -- segdev only deals with
402 	 * character (VCHR) devices. We use the common
403 	 * vp to hang pages on.
404 	 */
405 	sdp->vp = specfind(a->dev, VCHR);
406 	ASSERT(sdp->vp != NULL);
407 
408 	seg->s_ops = &segdev_ops;
409 	seg->s_data = sdp;
410 
411 	while (dhp != NULL) {
412 		dhp->dh_seg = seg;
413 		dhp = dhp->dh_next;
414 	}
415 
416 	/*
417 	 * Inform the vnode of the new mapping.
418 	 */
419 	/*
420 	 * It is ok to use pass sdp->maxprot to ADDMAP rather than to use
421 	 * dhp specific maxprot because spec_addmap does not use maxprot.
422 	 */
423 	error = VOP_ADDMAP(VTOCVP(sdp->vp), sdp->offset,
424 	    seg->s_as, seg->s_base, seg->s_size,
425 	    sdp->prot, sdp->maxprot, sdp->type, CRED());
426 
427 	if (error != 0) {
428 		sdp->devmap_data = NULL;
429 		hat_unload(seg->s_as->a_hat, seg->s_base, seg->s_size,
430 		    HAT_UNLOAD_UNMAP);
431 	}
432 
433 	return (error);
434 }
435 
436 static struct segdev_data *
437 sdp_alloc(void)
438 {
439 	struct segdev_data *sdp;
440 
441 	sdp = kmem_zalloc(sizeof (struct segdev_data), KM_SLEEP);
442 	mutex_init(&sdp->lock, NULL, MUTEX_DEFAULT, NULL);
443 
444 	return (sdp);
445 }
446 
447 /*
448  * Duplicate seg and return new segment in newseg.
449  */
450 static int
451 segdev_dup(struct seg *seg, struct seg *newseg)
452 {
453 	struct segdev_data *sdp = (struct segdev_data *)seg->s_data;
454 	struct segdev_data *newsdp;
455 	devmap_handle_t *dhp = (devmap_handle_t *)sdp->devmap_data;
456 	size_t npages;
457 	int ret;
458 
459 	TRACE_2(TR_FAC_DEVMAP, TR_DEVMAP_DUP,
460 	    "segdev_dup:start dhp=%p, seg=%p", (void *)dhp, (void *)seg);
461 
462 	DEBUGF(3, (CE_CONT, "segdev_dup: dhp %p seg %p\n",
463 	    (void *)dhp, (void *)seg));
464 
465 	/*
466 	 * Since the address space is "write" locked, we
467 	 * don't need the segment lock to protect "segdev" data.
468 	 */
469 	ASSERT(seg->s_as && AS_WRITE_HELD(seg->s_as, &seg->s_as->a_lock));
470 
471 	newsdp = sdp_alloc();
472 
473 	newseg->s_ops = seg->s_ops;
474 	newseg->s_data = (void *)newsdp;
475 
476 	VN_HOLD(sdp->vp);
477 	newsdp->vp 	= sdp->vp;
478 	newsdp->mapfunc = sdp->mapfunc;
479 	newsdp->offset	= sdp->offset;
480 	newsdp->pageprot = sdp->pageprot;
481 	newsdp->prot	= sdp->prot;
482 	newsdp->maxprot = sdp->maxprot;
483 	newsdp->type = sdp->type;
484 	newsdp->hat_attr = sdp->hat_attr;
485 	newsdp->hat_flags = sdp->hat_flags;
486 	newsdp->softlockcnt = 0;
487 
488 	/*
489 	 * Initialize per page data if the segment we are
490 	 * dup'ing has per page information.
491 	 */
492 	npages = seg_pages(newseg);
493 
494 	if (sdp->vpage != NULL) {
495 		size_t nbytes = vpgtob(npages);
496 
497 		newsdp->vpage = kmem_zalloc(nbytes, KM_SLEEP);
498 		bcopy(sdp->vpage, newsdp->vpage, nbytes);
499 	} else
500 		newsdp->vpage = NULL;
501 
502 	/*
503 	 * duplicate devmap handles
504 	 */
505 	if (dhp != NULL) {
506 		ret = devmap_handle_dup(dhp,
507 			(devmap_handle_t **)&newsdp->devmap_data, newseg);
508 		if (ret != 0) {
509 			TRACE_3(TR_FAC_DEVMAP, TR_DEVMAP_DUP_CK1,
510 			    "segdev_dup:ret1 ret=%x, dhp=%p seg=%p",
511 			    ret, (void *)dhp, (void *)seg);
512 			DEBUGF(1, (CE_CONT,
513 			    "segdev_dup: ret %x dhp %p seg %p\n",
514 			    ret, (void *)dhp, (void *)seg));
515 			return (ret);
516 		}
517 	}
518 
519 	/*
520 	 * Inform the common vnode of the new mapping.
521 	 */
522 	return (VOP_ADDMAP(VTOCVP(newsdp->vp),
523 		newsdp->offset, newseg->s_as,
524 		newseg->s_base, newseg->s_size, newsdp->prot,
525 		newsdp->maxprot, sdp->type, CRED()));
526 }
527 
528 /*
529  * duplicate devmap handles
530  */
531 static int
532 devmap_handle_dup(devmap_handle_t *dhp, devmap_handle_t **new_dhp,
533     struct seg *newseg)
534 {
535 	devmap_handle_t *newdhp_save = NULL;
536 	devmap_handle_t *newdhp = NULL;
537 	struct devmap_callback_ctl *callbackops;
538 
539 	while (dhp != NULL) {
540 		newdhp = kmem_alloc(sizeof (devmap_handle_t), KM_SLEEP);
541 
542 		/* Need to lock the original dhp while copying if REMAP */
543 		HOLD_DHP_LOCK(dhp);
544 		bcopy(dhp, newdhp, sizeof (devmap_handle_t));
545 		RELE_DHP_LOCK(dhp);
546 		newdhp->dh_seg = newseg;
547 		newdhp->dh_next = NULL;
548 		if (newdhp_save != NULL)
549 			newdhp_save->dh_next = newdhp;
550 		else
551 			*new_dhp = newdhp;
552 		newdhp_save = newdhp;
553 
554 		callbackops = &newdhp->dh_callbackops;
555 
556 		if (dhp->dh_softlock != NULL)
557 			newdhp->dh_softlock = devmap_softlock_init(
558 			    newdhp->dh_dev,
559 			    (ulong_t)callbackops->devmap_access);
560 		if (dhp->dh_ctx != NULL)
561 			newdhp->dh_ctx = devmap_ctxinit(newdhp->dh_dev,
562 			    (ulong_t)callbackops->devmap_access);
563 
564 		/*
565 		 * Initialize dh_lock if we want to do remap.
566 		 */
567 		if (newdhp->dh_flags & DEVMAP_ALLOW_REMAP) {
568 			mutex_init(&newdhp->dh_lock, NULL, MUTEX_DEFAULT, NULL);
569 			newdhp->dh_flags |= DEVMAP_LOCK_INITED;
570 		}
571 
572 		if (callbackops->devmap_dup != NULL) {
573 			int ret;
574 
575 			/*
576 			 * Call the dup callback so that the driver can
577 			 * duplicate its private data.
578 			 */
579 			ret = (*callbackops->devmap_dup)(dhp, dhp->dh_pvtp,
580 				(devmap_cookie_t *)newdhp, &newdhp->dh_pvtp);
581 
582 			if (ret != 0) {
583 				/*
584 				 * We want to free up this segment as the driver
585 				 * has indicated that we can't dup it.  But we
586 				 * don't want to call the drivers, devmap_unmap,
587 				 * callback function as the driver does not
588 				 * think this segment exists. The caller of
589 				 * devmap_dup will call seg_free on newseg
590 				 * as it was the caller that allocated the
591 				 * segment.
592 				 */
593 				DEBUGF(1, (CE_CONT, "devmap_handle_dup ERROR: "
594 				    "newdhp %p dhp %p\n", (void *)newdhp,
595 				    (void *)dhp));
596 				callbackops->devmap_unmap = NULL;
597 				return (ret);
598 			}
599 		}
600 
601 		dhp = dhp->dh_next;
602 	}
603 
604 	return (0);
605 }
606 
607 /*
608  * Split a segment at addr for length len.
609  */
610 /*ARGSUSED*/
611 static int
612 segdev_unmap(struct seg *seg, caddr_t addr, size_t len)
613 {
614 	register struct segdev_data *sdp = (struct segdev_data *)seg->s_data;
615 	register struct segdev_data *nsdp;
616 	register struct seg *nseg;
617 	register size_t	opages;		/* old segment size in pages */
618 	register size_t	npages;		/* new segment size in pages */
619 	register size_t	dpages;		/* pages being deleted (unmapped) */
620 	register size_t	nbytes;
621 	devmap_handle_t *dhp = (devmap_handle_t *)sdp->devmap_data;
622 	devmap_handle_t *dhpp;
623 	devmap_handle_t *newdhp;
624 	struct devmap_callback_ctl *callbackops;
625 	caddr_t nbase;
626 	offset_t off;
627 	ulong_t nsize;
628 	size_t mlen, sz;
629 
630 	TRACE_4(TR_FAC_DEVMAP, TR_DEVMAP_UNMAP,
631 	    "segdev_unmap:start dhp=%p, seg=%p addr=%p len=%lx",
632 	    (void *)dhp, (void *)seg, (void *)addr, len);
633 
634 	DEBUGF(3, (CE_CONT, "segdev_unmap: dhp %p seg %p addr %p len %lx\n",
635 	    (void *)dhp, (void *)seg, (void *)addr, len));
636 
637 	/*
638 	 * Since the address space is "write" locked, we
639 	 * don't need the segment lock to protect "segdev" data.
640 	 */
641 	ASSERT(seg->s_as && AS_WRITE_HELD(seg->s_as, &seg->s_as->a_lock));
642 
643 	if ((sz = sdp->softlockcnt) > 0) {
644 		/*
645 		 * Fail the unmap if pages are SOFTLOCKed through this mapping.
646 		 * softlockcnt is protected from change by the as write lock.
647 		 */
648 		TRACE_1(TR_FAC_DEVMAP, TR_DEVMAP_UNMAP_CK1,
649 		    "segdev_unmap:error softlockcnt = %ld", sz);
650 		DEBUGF(1, (CE_CONT, "segdev_unmap: softlockcnt %ld\n", sz));
651 		return (EAGAIN);
652 	}
653 
654 	/*
655 	 * Check for bad sizes
656 	 */
657 	if (addr < seg->s_base || addr + len > seg->s_base + seg->s_size ||
658 	    (len & PAGEOFFSET) || ((uintptr_t)addr & PAGEOFFSET))
659 		panic("segdev_unmap");
660 
661 	if (dhp != NULL) {
662 		devmap_handle_t *tdhp;
663 		/*
664 		 * If large page size was used in hat_devload(),
665 		 * the same page size must be used in hat_unload().
666 		 */
667 		dhpp = tdhp = devmap_find_handle(dhp, addr);
668 		while (tdhp != NULL) {
669 			if (tdhp->dh_flags & DEVMAP_FLAG_LARGE) {
670 				break;
671 			}
672 			tdhp = tdhp->dh_next;
673 		}
674 		if (tdhp != NULL) {	/* found a dhp using large pages */
675 			size_t slen = len;
676 			size_t mlen;
677 			size_t soff;
678 
679 			soff = (ulong_t)(addr - dhpp->dh_uvaddr);
680 			while (slen != 0) {
681 				mlen = MIN(slen, (dhpp->dh_len - soff));
682 				hat_unload(seg->s_as->a_hat, dhpp->dh_uvaddr,
683 					dhpp->dh_len, HAT_UNLOAD_UNMAP);
684 				dhpp = dhpp->dh_next;
685 				ASSERT(slen >= mlen);
686 				slen -= mlen;
687 				soff = 0;
688 			}
689 		} else
690 			hat_unload(seg->s_as->a_hat, addr, len,
691 				HAT_UNLOAD_UNMAP);
692 	} else {
693 		/*
694 		 * Unload any hardware translations in the range
695 		 * to be taken out.
696 		 */
697 		hat_unload(seg->s_as->a_hat, addr, len, HAT_UNLOAD_UNMAP);
698 	}
699 
700 	/*
701 	 * get the user offset which will used in the driver callbacks
702 	 */
703 	off = sdp->offset + (offset_t)(addr - seg->s_base);
704 
705 	/*
706 	 * Inform the vnode of the unmapping.
707 	 */
708 	ASSERT(sdp->vp != NULL);
709 	(void) VOP_DELMAP(VTOCVP(sdp->vp), off, seg->s_as, addr, len,
710 		sdp->prot, sdp->maxprot, sdp->type, CRED());
711 
712 	/*
713 	 * Check for entire segment
714 	 */
715 	if (addr == seg->s_base && len == seg->s_size) {
716 		seg_free(seg);
717 		return (0);
718 	}
719 
720 	opages = seg_pages(seg);
721 	dpages = btop(len);
722 	npages = opages - dpages;
723 
724 	/*
725 	 * Check for beginning of segment
726 	 */
727 	if (addr == seg->s_base) {
728 		if (sdp->vpage != NULL) {
729 			register struct vpage *ovpage;
730 
731 			ovpage = sdp->vpage;	/* keep pointer to vpage */
732 
733 			nbytes = vpgtob(npages);
734 			sdp->vpage = kmem_alloc(nbytes, KM_SLEEP);
735 			bcopy(&ovpage[dpages], sdp->vpage, nbytes);
736 
737 			/* free up old vpage */
738 			kmem_free(ovpage, vpgtob(opages));
739 		}
740 
741 		/*
742 		 * free devmap handles from the beginning of the mapping.
743 		 */
744 		if (dhp != NULL)
745 			devmap_handle_unmap_head(dhp, len);
746 
747 		sdp->offset += (offset_t)len;
748 
749 		seg->s_base += len;
750 		seg->s_size -= len;
751 
752 		return (0);
753 	}
754 
755 	/*
756 	 * Check for end of segment
757 	 */
758 	if (addr + len == seg->s_base + seg->s_size) {
759 		if (sdp->vpage != NULL) {
760 			register struct vpage *ovpage;
761 
762 			ovpage = sdp->vpage;	/* keep pointer to vpage */
763 
764 			nbytes = vpgtob(npages);
765 			sdp->vpage = kmem_alloc(nbytes, KM_SLEEP);
766 			bcopy(ovpage, sdp->vpage, nbytes);
767 
768 			/* free up old vpage */
769 			kmem_free(ovpage, vpgtob(opages));
770 		}
771 		seg->s_size -= len;
772 
773 		/*
774 		 * free devmap handles from addr to the end of the mapping.
775 		 */
776 		if (dhp != NULL)
777 			devmap_handle_unmap_tail(dhp, addr);
778 
779 		return (0);
780 	}
781 
782 	/*
783 	 * The section to go is in the middle of the segment,
784 	 * have to make it into two segments.  nseg is made for
785 	 * the high end while seg is cut down at the low end.
786 	 */
787 	nbase = addr + len;				/* new seg base */
788 	nsize = (seg->s_base + seg->s_size) - nbase;	/* new seg size */
789 	seg->s_size = addr - seg->s_base;		/* shrink old seg */
790 	nseg = seg_alloc(seg->s_as, nbase, nsize);
791 	if (nseg == NULL)
792 		panic("segdev_unmap seg_alloc");
793 
794 	TRACE_2(TR_FAC_DEVMAP, TR_DEVMAP_UNMAP_CK2,
795 	    "segdev_unmap: seg=%p nseg=%p", (void *)seg, (void *)nseg);
796 	DEBUGF(3, (CE_CONT, "segdev_unmap: segdev_dup seg %p nseg %p\n",
797 	    (void *)seg, (void *)nseg));
798 	nsdp = sdp_alloc();
799 
800 	nseg->s_ops = seg->s_ops;
801 	nseg->s_data = (void *)nsdp;
802 
803 	VN_HOLD(sdp->vp);
804 	nsdp->mapfunc = sdp->mapfunc;
805 	nsdp->offset = sdp->offset + (offset_t)(nseg->s_base - seg->s_base);
806 	nsdp->vp 	= sdp->vp;
807 	nsdp->pageprot = sdp->pageprot;
808 	nsdp->prot	= sdp->prot;
809 	nsdp->maxprot = sdp->maxprot;
810 	nsdp->type = sdp->type;
811 	nsdp->hat_attr = sdp->hat_attr;
812 	nsdp->hat_flags = sdp->hat_flags;
813 	nsdp->softlockcnt = 0;
814 
815 	/*
816 	 * Initialize per page data if the segment we are
817 	 * dup'ing has per page information.
818 	 */
819 	if (sdp->vpage != NULL) {
820 		/* need to split vpage into two arrays */
821 		register size_t nnbytes;
822 		register size_t nnpages;
823 		register struct vpage *ovpage;
824 
825 		ovpage = sdp->vpage;		/* keep pointer to vpage */
826 
827 		npages = seg_pages(seg);	/* seg has shrunk */
828 		nbytes = vpgtob(npages);
829 		nnpages = seg_pages(nseg);
830 		nnbytes = vpgtob(nnpages);
831 
832 		sdp->vpage = kmem_alloc(nbytes, KM_SLEEP);
833 		bcopy(ovpage, sdp->vpage, nbytes);
834 
835 		nsdp->vpage = kmem_alloc(nnbytes, KM_SLEEP);
836 		bcopy(&ovpage[npages + dpages], nsdp->vpage, nnbytes);
837 
838 		/* free up old vpage */
839 		kmem_free(ovpage, vpgtob(opages));
840 	} else
841 		nsdp->vpage = NULL;
842 
843 	/*
844 	 * unmap dhps.
845 	 */
846 	if (dhp == NULL) {
847 		nsdp->devmap_data = NULL;
848 		return (0);
849 	}
850 	while (dhp != NULL) {
851 		callbackops = &dhp->dh_callbackops;
852 		TRACE_2(TR_FAC_DEVMAP, TR_DEVMAP_UNMAP_CK3,
853 		    "segdev_unmap: dhp=%p addr=%p", dhp, addr);
854 		DEBUGF(3, (CE_CONT, "unmap: dhp %p addr %p uvaddr %p len %lx\n",
855 		    (void *)dhp, (void *)addr,
856 		    (void *)dhp->dh_uvaddr, dhp->dh_len));
857 
858 		if (addr == (dhp->dh_uvaddr + dhp->dh_len)) {
859 			dhpp = dhp->dh_next;
860 			dhp->dh_next = NULL;
861 			dhp = dhpp;
862 		} else if (addr > (dhp->dh_uvaddr + dhp->dh_len)) {
863 			dhp = dhp->dh_next;
864 		} else if (addr > dhp->dh_uvaddr &&
865 			(addr + len) < (dhp->dh_uvaddr + dhp->dh_len)) {
866 			/*
867 			 * <addr, addr+len> is enclosed by dhp.
868 			 * create a newdhp that begins at addr+len and
869 			 * ends at dhp->dh_uvaddr+dhp->dh_len.
870 			 */
871 			newdhp = kmem_alloc(sizeof (devmap_handle_t), KM_SLEEP);
872 			HOLD_DHP_LOCK(dhp);
873 			bcopy(dhp, newdhp, sizeof (devmap_handle_t));
874 			RELE_DHP_LOCK(dhp);
875 			newdhp->dh_seg = nseg;
876 			newdhp->dh_next = dhp->dh_next;
877 			if (dhp->dh_softlock != NULL)
878 				newdhp->dh_softlock = devmap_softlock_init(
879 					newdhp->dh_dev,
880 					(ulong_t)callbackops->devmap_access);
881 			if (dhp->dh_ctx != NULL)
882 				newdhp->dh_ctx = devmap_ctxinit(newdhp->dh_dev,
883 					(ulong_t)callbackops->devmap_access);
884 			if (newdhp->dh_flags & DEVMAP_LOCK_INITED) {
885 				mutex_init(&newdhp->dh_lock,
886 				    NULL, MUTEX_DEFAULT, NULL);
887 			}
888 			if (callbackops->devmap_unmap != NULL)
889 				(*callbackops->devmap_unmap)(dhp, dhp->dh_pvtp,
890 					off, len, dhp, &dhp->dh_pvtp,
891 					newdhp, &newdhp->dh_pvtp);
892 			mlen = len + (addr - dhp->dh_uvaddr);
893 			devmap_handle_reduce_len(newdhp, mlen);
894 			nsdp->devmap_data = newdhp;
895 			/* XX Changing len should recalculate LARGE flag */
896 			dhp->dh_len = addr - dhp->dh_uvaddr;
897 			dhpp = dhp->dh_next;
898 			dhp->dh_next = NULL;
899 			dhp = dhpp;
900 		} else if ((addr > dhp->dh_uvaddr) &&
901 			    ((addr + len) >= (dhp->dh_uvaddr + dhp->dh_len))) {
902 			mlen = dhp->dh_len + dhp->dh_uvaddr - addr;
903 			/*
904 			 * <addr, addr+len> spans over dhps.
905 			 */
906 			if (callbackops->devmap_unmap != NULL)
907 				(*callbackops->devmap_unmap)(dhp, dhp->dh_pvtp,
908 					off, mlen, (devmap_cookie_t *)dhp,
909 					&dhp->dh_pvtp, NULL, NULL);
910 			/* XX Changing len should recalculate LARGE flag */
911 			dhp->dh_len = addr - dhp->dh_uvaddr;
912 			dhpp = dhp->dh_next;
913 			dhp->dh_next = NULL;
914 			dhp = dhpp;
915 			nsdp->devmap_data = dhp;
916 		} else if ((addr + len) >= (dhp->dh_uvaddr + dhp->dh_len)) {
917 			/*
918 			 * dhp is enclosed by <addr, addr+len>.
919 			 */
920 			dhp->dh_seg = nseg;
921 			nsdp->devmap_data = dhp;
922 			dhp = devmap_handle_unmap(dhp);
923 			nsdp->devmap_data = dhp; /* XX redundant? */
924 		} else if (((addr + len) > dhp->dh_uvaddr) &&
925 			    ((addr + len) < (dhp->dh_uvaddr + dhp->dh_len))) {
926 			mlen = addr + len - dhp->dh_uvaddr;
927 			if (callbackops->devmap_unmap != NULL)
928 				(*callbackops->devmap_unmap)(dhp, dhp->dh_pvtp,
929 					dhp->dh_uoff, mlen, NULL,
930 					NULL, dhp, &dhp->dh_pvtp);
931 			devmap_handle_reduce_len(dhp, mlen);
932 			nsdp->devmap_data = dhp;
933 			dhp->dh_seg = nseg;
934 			dhp = dhp->dh_next;
935 		} else {
936 			dhp->dh_seg = nseg;
937 			dhp = dhp->dh_next;
938 		}
939 	}
940 	return (0);
941 }
942 
943 /*
944  * Utility function handles reducing the length of a devmap handle during unmap
945  * Note that is only used for unmapping the front portion of the handler,
946  * i.e., we are bumping up the offset/pfn etc up by len
947  * Do not use if reducing length at the tail.
948  */
949 static void
950 devmap_handle_reduce_len(devmap_handle_t *dhp, size_t len)
951 {
952 	struct ddi_umem_cookie *cp;
953 	struct devmap_pmem_cookie *pcp;
954 	/*
955 	 * adjust devmap handle fields
956 	 */
957 	ASSERT(len < dhp->dh_len);
958 
959 	/* Make sure only page-aligned changes are done */
960 	ASSERT((len & PAGEOFFSET) == 0);
961 
962 	dhp->dh_len -= len;
963 	dhp->dh_uoff += (offset_t)len;
964 	dhp->dh_roff += (offset_t)len;
965 	dhp->dh_uvaddr += len;
966 	/* Need to grab dhp lock if REMAP */
967 	HOLD_DHP_LOCK(dhp);
968 	cp = dhp->dh_cookie;
969 	if (!(dhp->dh_flags & DEVMAP_MAPPING_INVALID)) {
970 		if (cookie_is_devmem(cp)) {
971 			dhp->dh_pfn += btop(len);
972 		} else if (cookie_is_pmem(cp)) {
973 			pcp = (struct devmap_pmem_cookie *)dhp->dh_pcookie;
974 			ASSERT((dhp->dh_roff & PAGEOFFSET) == 0 &&
975 				dhp->dh_roff < ptob(pcp->dp_npages));
976 		} else {
977 			ASSERT(dhp->dh_roff < cp->size);
978 			ASSERT(dhp->dh_cvaddr >= cp->cvaddr &&
979 				dhp->dh_cvaddr < (cp->cvaddr + cp->size));
980 			ASSERT((dhp->dh_cvaddr + len) <=
981 				(cp->cvaddr + cp->size));
982 
983 			dhp->dh_cvaddr += len;
984 		}
985 	}
986 	/* XXX - Should recalculate the DEVMAP_FLAG_LARGE after changes */
987 	RELE_DHP_LOCK(dhp);
988 }
989 
990 /*
991  * Free devmap handle, dhp.
992  * Return the next devmap handle on the linked list.
993  */
994 static devmap_handle_t *
995 devmap_handle_unmap(devmap_handle_t *dhp)
996 {
997 	struct devmap_callback_ctl *callbackops = &dhp->dh_callbackops;
998 	struct segdev_data *sdp = (struct segdev_data *)dhp->dh_seg->s_data;
999 	devmap_handle_t *dhpp = (devmap_handle_t *)sdp->devmap_data;
1000 
1001 	ASSERT(dhp != NULL);
1002 
1003 	/*
1004 	 * before we free up dhp, call the driver's devmap_unmap entry point
1005 	 * to free resources allocated for this dhp.
1006 	 */
1007 	if (callbackops->devmap_unmap != NULL) {
1008 		(*callbackops->devmap_unmap)(dhp, dhp->dh_pvtp, dhp->dh_uoff,
1009 			dhp->dh_len, NULL, NULL, NULL, NULL);
1010 	}
1011 
1012 	if (dhpp == dhp) {	/* releasing first dhp, change sdp data */
1013 		sdp->devmap_data = dhp->dh_next;
1014 	} else {
1015 		while (dhpp->dh_next != dhp) {
1016 			dhpp = dhpp->dh_next;
1017 		}
1018 		dhpp->dh_next = dhp->dh_next;
1019 	}
1020 	dhpp = dhp->dh_next;	/* return value is next dhp in chain */
1021 
1022 	if (dhp->dh_softlock != NULL)
1023 		devmap_softlock_rele(dhp);
1024 
1025 	if (dhp->dh_ctx != NULL)
1026 		devmap_ctx_rele(dhp);
1027 
1028 	if (dhp->dh_flags & DEVMAP_LOCK_INITED) {
1029 		mutex_destroy(&dhp->dh_lock);
1030 	}
1031 	kmem_free(dhp, sizeof (devmap_handle_t));
1032 
1033 	return (dhpp);
1034 }
1035 
1036 /*
1037  * Free complete devmap handles from dhp for len bytes
1038  * dhp can be either the first handle or a subsequent handle
1039  */
1040 static void
1041 devmap_handle_unmap_head(devmap_handle_t *dhp, size_t len)
1042 {
1043 	struct devmap_callback_ctl *callbackops;
1044 
1045 	/*
1046 	 * free the devmap handles covered by len.
1047 	 */
1048 	while (len >= dhp->dh_len) {
1049 		len -= dhp->dh_len;
1050 		dhp = devmap_handle_unmap(dhp);
1051 	}
1052 	if (len != 0) {	/* partial unmap at head of first remaining dhp */
1053 		callbackops = &dhp->dh_callbackops;
1054 
1055 		/*
1056 		 * Call the unmap callback so the drivers can make
1057 		 * adjustment on its private data.
1058 		 */
1059 		if (callbackops->devmap_unmap != NULL)
1060 			(*callbackops->devmap_unmap)(dhp, dhp->dh_pvtp,
1061 			    dhp->dh_uoff, len, NULL, NULL, dhp, &dhp->dh_pvtp);
1062 		devmap_handle_reduce_len(dhp, len);
1063 	}
1064 }
1065 
1066 /*
1067  * Free devmap handles to truncate  the mapping after addr
1068  * RFE: Simpler to pass in dhp pointing at correct dhp (avoid find again)
1069  *	Also could then use the routine in middle unmap case too
1070  */
1071 static void
1072 devmap_handle_unmap_tail(devmap_handle_t *dhp, caddr_t addr)
1073 {
1074 	register struct seg *seg = dhp->dh_seg;
1075 	register struct segdev_data *sdp = (struct segdev_data *)seg->s_data;
1076 	register devmap_handle_t *dhph = (devmap_handle_t *)sdp->devmap_data;
1077 	struct devmap_callback_ctl *callbackops;
1078 	register devmap_handle_t *dhpp;
1079 	size_t maplen;
1080 	ulong_t off;
1081 	size_t len;
1082 
1083 	maplen = (size_t)(addr - dhp->dh_uvaddr);
1084 	dhph = devmap_find_handle(dhph, addr);
1085 
1086 	while (dhph != NULL) {
1087 		if (maplen == 0) {
1088 			dhph =  devmap_handle_unmap(dhph);
1089 		} else {
1090 			callbackops = &dhph->dh_callbackops;
1091 			len = dhph->dh_len - maplen;
1092 			off = (ulong_t)sdp->offset + (addr - seg->s_base);
1093 			/*
1094 			 * Call the unmap callback so the driver
1095 			 * can make adjustments on its private data.
1096 			 */
1097 			if (callbackops->devmap_unmap != NULL)
1098 				(*callbackops->devmap_unmap)(dhph,
1099 					dhph->dh_pvtp, off, len,
1100 					(devmap_cookie_t *)dhph,
1101 					&dhph->dh_pvtp, NULL, NULL);
1102 			/* XXX Reducing len needs to recalculate LARGE flag */
1103 			dhph->dh_len = maplen;
1104 			maplen = 0;
1105 			dhpp = dhph->dh_next;
1106 			dhph->dh_next = NULL;
1107 			dhph = dhpp;
1108 		}
1109 	} /* end while */
1110 }
1111 
1112 /*
1113  * Free a segment.
1114  */
1115 static void
1116 segdev_free(struct seg *seg)
1117 {
1118 	register struct segdev_data *sdp = (struct segdev_data *)seg->s_data;
1119 	devmap_handle_t *dhp = (devmap_handle_t *)sdp->devmap_data;
1120 
1121 	TRACE_2(TR_FAC_DEVMAP, TR_DEVMAP_FREE,
1122 	    "segdev_free: dhp=%p seg=%p", (void *)dhp, (void *)seg);
1123 	DEBUGF(3, (CE_CONT, "segdev_free: dhp %p seg %p\n",
1124 	    (void *)dhp, (void *)seg));
1125 
1126 	/*
1127 	 * Since the address space is "write" locked, we
1128 	 * don't need the segment lock to protect "segdev" data.
1129 	 */
1130 	ASSERT(seg->s_as && AS_WRITE_HELD(seg->s_as, &seg->s_as->a_lock));
1131 
1132 	while (dhp != NULL)
1133 		dhp = devmap_handle_unmap(dhp);
1134 
1135 	VN_RELE(sdp->vp);
1136 	if (sdp->vpage != NULL)
1137 		kmem_free(sdp->vpage, vpgtob(seg_pages(seg)));
1138 
1139 	mutex_destroy(&sdp->lock);
1140 	kmem_free(sdp, sizeof (*sdp));
1141 }
1142 
1143 static void
1144 free_devmap_handle(devmap_handle_t *dhp)
1145 {
1146 	register devmap_handle_t *dhpp;
1147 
1148 	/*
1149 	 * free up devmap handle
1150 	 */
1151 	while (dhp != NULL) {
1152 		dhpp = dhp->dh_next;
1153 		if (dhp->dh_flags & DEVMAP_LOCK_INITED) {
1154 			mutex_destroy(&dhp->dh_lock);
1155 		}
1156 
1157 		if (dhp->dh_softlock != NULL)
1158 			devmap_softlock_rele(dhp);
1159 
1160 		if (dhp->dh_ctx != NULL)
1161 			devmap_ctx_rele(dhp);
1162 
1163 		kmem_free(dhp, sizeof (devmap_handle_t));
1164 		dhp = dhpp;
1165 	}
1166 }
1167 
1168 /*
1169  * routines to lock and unlock underlying segkp segment for
1170  * KMEM_PAGEABLE type cookies.
1171  * segkp only allows a single pending F_SOFTLOCK
1172  * we keep track of number of locks in the cookie so we can
1173  * have multiple pending faults and manage the calls to segkp.
1174  * RFE: if segkp supports either pagelock or can support multiple
1175  * calls to F_SOFTLOCK, then these routines can go away.
1176  *	If pagelock, segdev_faultpage can fault on a page by page basis
1177  *		and simplifies the code quite a bit.
1178  *	if multiple calls allowed but not partial ranges, then need for
1179  *	cookie->lock and locked count goes away, code can call as_fault directly
1180  */
1181 static faultcode_t
1182 acquire_kpmem_lock(struct ddi_umem_cookie *cookie, size_t npages)
1183 {
1184 	int err = 0;
1185 	ASSERT(cookie_is_kpmem(cookie));
1186 	/*
1187 	 * Fault in pages in segkp with F_SOFTLOCK.
1188 	 * We want to hold the lock until all pages have been loaded.
1189 	 * segkp only allows single caller to hold SOFTLOCK, so cookie
1190 	 * holds a count so we dont call into segkp multiple times
1191 	 */
1192 	mutex_enter(&cookie->lock);
1193 
1194 	/*
1195 	 * Check for overflow in locked field
1196 	 */
1197 	if ((UINT32_MAX - cookie->locked) < npages) {
1198 		err = FC_MAKE_ERR(ENOMEM);
1199 	} else if (cookie->locked == 0) {
1200 		/* First time locking */
1201 		err = as_fault(kas.a_hat, &kas, cookie->cvaddr,
1202 		    cookie->size, F_SOFTLOCK, PROT_READ|PROT_WRITE);
1203 	}
1204 	if (!err) {
1205 		cookie->locked += npages;
1206 	}
1207 	mutex_exit(&cookie->lock);
1208 	return (err);
1209 }
1210 
1211 static void
1212 release_kpmem_lock(struct ddi_umem_cookie *cookie, size_t npages)
1213 {
1214 	mutex_enter(&cookie->lock);
1215 	ASSERT(cookie_is_kpmem(cookie));
1216 	ASSERT(cookie->locked >= npages);
1217 	cookie->locked -= (uint_t)npages;
1218 	if (cookie->locked == 0) {
1219 		/* Last unlock */
1220 		if (as_fault(kas.a_hat, &kas, cookie->cvaddr,
1221 		    cookie->size, F_SOFTUNLOCK, PROT_READ|PROT_WRITE))
1222 			panic("segdev releasing kpmem lock %p", (void *)cookie);
1223 	}
1224 	mutex_exit(&cookie->lock);
1225 }
1226 
1227 /*
1228  * Routines to synchronize F_SOFTLOCK and F_INVAL faults for
1229  * drivers with devmap_access callbacks
1230  * slock->softlocked basically works like a rw lock
1231  *	-ve counts => F_SOFTLOCK in progress
1232  *	+ve counts => F_INVAL/F_PROT in progress
1233  * We allow only one F_SOFTLOCK at a time
1234  * but can have multiple pending F_INVAL/F_PROT calls
1235  *
1236  * This routine waits using cv_wait_sig so killing processes is more graceful
1237  * Returns EINTR if coming out of this routine due to a signal, 0 otherwise
1238  */
1239 static int devmap_softlock_enter(
1240 	struct devmap_softlock *slock,
1241 	size_t npages,
1242 	enum fault_type type)
1243 {
1244 	if (npages == 0)
1245 		return (0);
1246 	mutex_enter(&(slock->lock));
1247 	switch (type) {
1248 	case F_SOFTLOCK :
1249 		while (slock->softlocked) {
1250 			if (cv_wait_sig(&(slock)->cv, &(slock)->lock) == 0) {
1251 				/* signalled */
1252 				mutex_exit(&(slock->lock));
1253 				return (EINTR);
1254 			}
1255 		}
1256 		slock->softlocked -= npages; /* -ve count => locked */
1257 		break;
1258 	case F_INVAL :
1259 	case F_PROT :
1260 		while (slock->softlocked < 0)
1261 			if (cv_wait_sig(&(slock)->cv, &(slock)->lock) == 0) {
1262 				/* signalled */
1263 				mutex_exit(&(slock->lock));
1264 				return (EINTR);
1265 			}
1266 		slock->softlocked += npages; /* +ve count => f_invals */
1267 		break;
1268 	default:
1269 		ASSERT(0);
1270 	}
1271 	mutex_exit(&(slock->lock));
1272 	return (0);
1273 }
1274 
1275 static void devmap_softlock_exit(
1276 	struct devmap_softlock *slock,
1277 	size_t npages,
1278 	enum fault_type type)
1279 {
1280 	if (slock == NULL)
1281 		return;
1282 	mutex_enter(&(slock->lock));
1283 	switch (type) {
1284 	case F_SOFTLOCK :
1285 		ASSERT(-slock->softlocked >= npages);
1286 		slock->softlocked += npages;	/* -ve count is softlocked */
1287 		if (slock->softlocked == 0)
1288 			cv_signal(&slock->cv);
1289 		break;
1290 	case F_INVAL :
1291 	case F_PROT:
1292 		ASSERT(slock->softlocked >= npages);
1293 		slock->softlocked -= npages;
1294 		if (slock->softlocked == 0)
1295 			cv_signal(&slock->cv);
1296 		break;
1297 	default:
1298 		ASSERT(0);
1299 	}
1300 	mutex_exit(&(slock->lock));
1301 }
1302 
1303 /*
1304  * Do a F_SOFTUNLOCK call over the range requested.
1305  * The range must have already been F_SOFTLOCK'ed.
1306  * The segment lock should be held, (but not the segment private lock?)
1307  *  The softunlock code below does not adjust for large page sizes
1308  *	assumes the caller already did any addr/len adjustments for
1309  *	pagesize mappings before calling.
1310  */
1311 /*ARGSUSED*/
1312 static void
1313 segdev_softunlock(
1314 	struct hat *hat,		/* the hat */
1315 	struct seg *seg,		/* seg_dev of interest */
1316 	caddr_t addr,			/* base address of range */
1317 	size_t len,			/* number of bytes */
1318 	enum seg_rw rw)			/* type of access at fault */
1319 {
1320 	struct segdev_data *sdp = (struct segdev_data *)seg->s_data;
1321 	devmap_handle_t *dhp_head = (devmap_handle_t *)sdp->devmap_data;
1322 
1323 	TRACE_4(TR_FAC_DEVMAP, TR_DEVMAP_SOFTUNLOCK,
1324 	    "segdev_softunlock:dhp_head=%p sdp=%p addr=%p len=%lx",
1325 	    dhp_head, sdp, addr, len);
1326 	DEBUGF(3, (CE_CONT, "segdev_softunlock: dhp %p lockcnt %lx "
1327 	    "addr %p len %lx\n",
1328 	    (void *)dhp_head, sdp->softlockcnt, (void *)addr, len));
1329 
1330 	hat_unlock(hat, addr, len);
1331 
1332 	if (dhp_head != NULL) {
1333 		devmap_handle_t *dhp;
1334 		size_t mlen;
1335 		size_t tlen = len;
1336 		ulong_t off;
1337 
1338 		dhp = devmap_find_handle(dhp_head, addr);
1339 		ASSERT(dhp != NULL);
1340 
1341 		off = (ulong_t)(addr - dhp->dh_uvaddr);
1342 		while (tlen != 0) {
1343 			mlen = MIN(tlen, (dhp->dh_len - off));
1344 
1345 			/*
1346 			 * unlock segkp memory, locked during F_SOFTLOCK
1347 			 */
1348 			if (dhp_is_kpmem(dhp)) {
1349 				release_kpmem_lock(
1350 				    (struct ddi_umem_cookie *)dhp->dh_cookie,
1351 				    btopr(mlen));
1352 			}
1353 
1354 			/*
1355 			 * Do the softlock accounting for devmap_access
1356 			 */
1357 			if (dhp->dh_callbackops.devmap_access != NULL) {
1358 				devmap_softlock_exit(dhp->dh_softlock,
1359 					btopr(mlen), F_SOFTLOCK);
1360 			}
1361 
1362 			tlen -= mlen;
1363 			dhp = dhp->dh_next;
1364 			off = 0;
1365 		}
1366 	}
1367 
1368 	mutex_enter(&freemem_lock);
1369 	ASSERT(sdp->softlockcnt >= btopr(len));
1370 	sdp->softlockcnt -= btopr(len);
1371 	mutex_exit(&freemem_lock);
1372 	if (sdp->softlockcnt == 0) {
1373 		/*
1374 		 * All SOFTLOCKS are gone. Wakeup any waiting
1375 		 * unmappers so they can try again to unmap.
1376 		 * Check for waiters first without the mutex
1377 		 * held so we don't always grab the mutex on
1378 		 * softunlocks.
1379 		 */
1380 		if (AS_ISUNMAPWAIT(seg->s_as)) {
1381 			mutex_enter(&seg->s_as->a_contents);
1382 			if (AS_ISUNMAPWAIT(seg->s_as)) {
1383 				AS_CLRUNMAPWAIT(seg->s_as);
1384 				cv_broadcast(&seg->s_as->a_cv);
1385 			}
1386 			mutex_exit(&seg->s_as->a_contents);
1387 		}
1388 	}
1389 
1390 }
1391 
1392 /*
1393  * Handle fault for a single page.
1394  * Done in a separate routine so we can handle errors more easily.
1395  * This routine is called only from segdev_faultpages()
1396  * when looping over the range of addresses requested. The segment lock is held.
1397  */
1398 static faultcode_t
1399 segdev_faultpage(
1400 	struct hat *hat,		/* the hat */
1401 	struct seg *seg,		/* seg_dev of interest */
1402 	caddr_t addr,			/* address in as */
1403 	struct vpage *vpage,		/* pointer to vpage for seg, addr */
1404 	enum fault_type type,		/* type of fault */
1405 	enum seg_rw rw,			/* type of access at fault */
1406 	devmap_handle_t *dhp)		/* devmap handle if any for this page */
1407 {
1408 	struct segdev_data *sdp = (struct segdev_data *)seg->s_data;
1409 	uint_t prot;
1410 	pfn_t pfnum = PFN_INVALID;
1411 	u_offset_t offset;
1412 	uint_t hat_flags;
1413 	dev_info_t *dip;
1414 
1415 	TRACE_3(TR_FAC_DEVMAP, TR_DEVMAP_FAULTPAGE,
1416 	    "segdev_faultpage: dhp=%p seg=%p addr=%p", dhp, seg, addr);
1417 	DEBUGF(8, (CE_CONT, "segdev_faultpage: dhp %p seg %p addr %p \n",
1418 	    (void *)dhp, (void *)seg, (void *)addr));
1419 
1420 	/*
1421 	 * Initialize protection value for this page.
1422 	 * If we have per page protection values check it now.
1423 	 */
1424 	if (sdp->pageprot) {
1425 		uint_t protchk;
1426 
1427 		switch (rw) {
1428 		case S_READ:
1429 			protchk = PROT_READ;
1430 			break;
1431 		case S_WRITE:
1432 			protchk = PROT_WRITE;
1433 			break;
1434 		case S_EXEC:
1435 			protchk = PROT_EXEC;
1436 			break;
1437 		case S_OTHER:
1438 		default:
1439 			protchk = PROT_READ | PROT_WRITE | PROT_EXEC;
1440 			break;
1441 		}
1442 
1443 		prot = VPP_PROT(vpage);
1444 		if ((prot & protchk) == 0)
1445 			return (FC_PROT);	/* illegal access type */
1446 	} else {
1447 		prot = sdp->prot;
1448 		/* caller has already done segment level protection check */
1449 	}
1450 
1451 	if (type == F_SOFTLOCK) {
1452 		mutex_enter(&freemem_lock);
1453 		sdp->softlockcnt++;
1454 		mutex_exit(&freemem_lock);
1455 	}
1456 
1457 	hat_flags = ((type == F_SOFTLOCK) ? HAT_LOAD_LOCK : HAT_LOAD);
1458 	offset = sdp->offset + (u_offset_t)(addr - seg->s_base);
1459 	/*
1460 	 * In the devmap framework, sdp->mapfunc is set to NULL.  we can get
1461 	 * pfnum from dhp->dh_pfn (at beginning of segment) and offset from
1462 	 * seg->s_base.
1463 	 */
1464 	if (dhp == NULL) {
1465 		/* If segment has devmap_data, then dhp should be non-NULL */
1466 		ASSERT(sdp->devmap_data == NULL);
1467 		pfnum = (pfn_t)cdev_mmap(sdp->mapfunc, sdp->vp->v_rdev,
1468 			(off_t)offset, prot);
1469 		prot |= sdp->hat_attr;
1470 	} else {
1471 		ulong_t off;
1472 		struct ddi_umem_cookie *cp;
1473 		struct devmap_pmem_cookie *pcp;
1474 
1475 		/* ensure the dhp passed in contains addr. */
1476 		ASSERT(dhp == devmap_find_handle(
1477 			(devmap_handle_t *)sdp->devmap_data, addr));
1478 
1479 		off = addr - dhp->dh_uvaddr;
1480 
1481 		/*
1482 		 * This routine assumes that the caller makes sure that the
1483 		 * fields in dhp used below are unchanged due to remap during
1484 		 * this call. Caller does HOLD_DHP_LOCK if neeed
1485 		 */
1486 		cp = dhp->dh_cookie;
1487 		if (dhp->dh_flags & DEVMAP_MAPPING_INVALID) {
1488 			pfnum = PFN_INVALID;
1489 		} else if (cookie_is_devmem(cp)) {
1490 			pfnum = dhp->dh_pfn + btop(off);
1491 		} else if (cookie_is_pmem(cp)) {
1492 			pcp = (struct devmap_pmem_cookie *)dhp->dh_pcookie;
1493 			ASSERT((dhp->dh_roff & PAGEOFFSET) == 0 &&
1494 				dhp->dh_roff < ptob(pcp->dp_npages));
1495 			pfnum = page_pptonum(
1496 			    pcp->dp_pparray[btop(off + dhp->dh_roff)]);
1497 		} else {
1498 			ASSERT(dhp->dh_roff < cp->size);
1499 			ASSERT(dhp->dh_cvaddr >= cp->cvaddr &&
1500 				dhp->dh_cvaddr < (cp->cvaddr + cp->size));
1501 			ASSERT((dhp->dh_cvaddr + off) <=
1502 				(cp->cvaddr + cp->size));
1503 			ASSERT((dhp->dh_cvaddr + off + PAGESIZE) <=
1504 				(cp->cvaddr + cp->size));
1505 
1506 			switch (cp->type) {
1507 			case UMEM_LOCKED :
1508 			    if (cp->pparray != NULL) {
1509 				ASSERT((dhp->dh_roff & PAGEOFFSET) == 0);
1510 				pfnum = page_pptonum(
1511 				    cp->pparray[btop(off + dhp->dh_roff)]);
1512 			    } else {
1513 				pfnum = hat_getpfnum(
1514 				    ((proc_t *)cp->procp)->p_as->a_hat,
1515 				    cp->cvaddr + off);
1516 			    }
1517 			    break;
1518 			case UMEM_TRASH :
1519 			    pfnum = page_pptonum(trashpp);
1520 			    /* We should set hat_flags to HAT_NOFAULT also */
1521 			    /* However, not all hat layers implement this */
1522 			    break;
1523 			case KMEM_PAGEABLE:
1524 			case KMEM_NON_PAGEABLE:
1525 			    pfnum = hat_getpfnum(kas.a_hat,
1526 				dhp->dh_cvaddr + off);
1527 			    break;
1528 			default :
1529 			    pfnum = PFN_INVALID;
1530 			    break;
1531 			}
1532 		}
1533 		prot |= dhp->dh_hat_attr;
1534 	}
1535 	if (pfnum == PFN_INVALID) {
1536 		return (FC_MAKE_ERR(EFAULT));
1537 	}
1538 	/* prot should already be OR'ed in with hat_attributes if needed */
1539 
1540 	TRACE_4(TR_FAC_DEVMAP, TR_DEVMAP_FAULTPAGE_CK1,
1541 	    "segdev_faultpage: pfnum=%lx memory=%x prot=%x flags=%x",
1542 	    pfnum, pf_is_memory(pfnum), prot, hat_flags);
1543 	DEBUGF(9, (CE_CONT, "segdev_faultpage: pfnum %lx memory %x "
1544 	    "prot %x flags %x\n", pfnum, pf_is_memory(pfnum), prot, hat_flags));
1545 
1546 	if (pf_is_memory(pfnum) || (dhp != NULL)) {
1547 		/*
1548 		 * It's not _really_ required here to pass sdp->hat_flags
1549 		 * to hat_devload even though we do it.
1550 		 * This is because hat figures it out DEVMEM mappings
1551 		 * are non-consistent, anyway.
1552 		 */
1553 		hat_devload(hat, addr, PAGESIZE, pfnum,
1554 				prot, hat_flags | sdp->hat_flags);
1555 		return (0);
1556 	}
1557 
1558 	/*
1559 	 * Fall through to the case where devmap is not used and need to call
1560 	 * up the device tree to set up the mapping
1561 	 */
1562 
1563 	dip = VTOS(VTOCVP(sdp->vp))->s_dip;
1564 	ASSERT(dip);
1565 
1566 	/*
1567 	 * When calling ddi_map_fault, we do not OR in sdp->hat_attr
1568 	 * This is because this calls drivers which may not expect
1569 	 * prot to have any other values than PROT_ALL
1570 	 * The root nexus driver has a hack to peek into the segment
1571 	 * structure and then OR in sdp->hat_attr.
1572 	 * XX In case the bus_ops interfaces are ever revisited
1573 	 * we need to fix this. prot should include other hat attributes
1574 	 */
1575 	if (ddi_map_fault(dip, hat, seg, addr, NULL, pfnum, prot & PROT_ALL,
1576 	    (uint_t)(type == F_SOFTLOCK)) != DDI_SUCCESS) {
1577 		return (FC_MAKE_ERR(EFAULT));
1578 	}
1579 	return (0);
1580 }
1581 
1582 static faultcode_t
1583 segdev_fault(
1584 	struct hat *hat,		/* the hat */
1585 	struct seg *seg,		/* the seg_dev of interest */
1586 	caddr_t addr,			/* the address of the fault */
1587 	size_t len,			/* the length of the range */
1588 	enum fault_type type,		/* type of fault */
1589 	enum seg_rw rw)			/* type of access at fault */
1590 {
1591 	struct segdev_data *sdp = (struct segdev_data *)seg->s_data;
1592 	devmap_handle_t *dhp_head = (devmap_handle_t *)sdp->devmap_data;
1593 	devmap_handle_t *dhp;
1594 	struct devmap_softlock *slock = NULL;
1595 	ulong_t slpage = 0;
1596 	ulong_t off;
1597 	caddr_t maddr = addr;
1598 	int err;
1599 	int err_is_faultcode = 0;
1600 
1601 	TRACE_5(TR_FAC_DEVMAP, TR_DEVMAP_FAULT,
1602 	    "segdev_fault: dhp_head=%p seg=%p addr=%p len=%lx type=%x",
1603 	    (void *)dhp_head, (void *)seg, (void *)addr, len, type);
1604 	DEBUGF(7, (CE_CONT, "segdev_fault: dhp_head %p seg %p "
1605 	    "addr %p len %lx type %x\n",
1606 	    (void *)dhp_head, (void *)seg, (void *)addr, len, type));
1607 
1608 	ASSERT(seg->s_as && AS_LOCK_HELD(seg->s_as, &seg->s_as->a_lock));
1609 
1610 	/* Handle non-devmap case */
1611 	if (dhp_head == NULL)
1612 		return (segdev_faultpages(hat, seg, addr, len, type, rw, NULL));
1613 
1614 	/* Find devmap handle */
1615 	if ((dhp = devmap_find_handle(dhp_head, addr)) == NULL)
1616 		return (FC_NOMAP);
1617 
1618 	/*
1619 	 * The seg_dev driver does not implement copy-on-write,
1620 	 * and always loads translations with maximal allowed permissions
1621 	 * but we got an fault trying to access the device.
1622 	 * Servicing the fault is not going to result in any better result
1623 	 * RFE: If we want devmap_access callbacks to be involved in F_PROT
1624 	 *	faults, then the code below is written for that
1625 	 *	Pending resolution of the following:
1626 	 *	- determine if the F_INVAL/F_SOFTLOCK syncing
1627 	 *	is needed for F_PROT also or not. The code below assumes it does
1628 	 *	- If driver sees F_PROT and calls devmap_load with same type,
1629 	 *	then segdev_faultpages will fail with FC_PROT anyway, need to
1630 	 *	change that so calls from devmap_load to segdev_faultpages for
1631 	 *	F_PROT type are retagged to F_INVAL.
1632 	 * RFE: Today we dont have drivers that use devmap and want to handle
1633 	 *	F_PROT calls. The code in segdev_fault* is written to allow
1634 	 *	this case but is not tested. A driver that needs this capability
1635 	 *	should be able to remove the short-circuit case; resolve the
1636 	 *	above issues and "should" work.
1637 	 */
1638 	if (type == F_PROT) {
1639 		return (FC_PROT);
1640 	}
1641 
1642 	/*
1643 	 * Loop through dhp list calling devmap_access or segdev_faultpages for
1644 	 * each devmap handle.
1645 	 * drivers which implement devmap_access can interpose on faults and do
1646 	 * device-appropriate special actions before calling devmap_load.
1647 	 */
1648 
1649 	/*
1650 	 * Unfortunately, this simple loop has turned out to expose a variety
1651 	 * of complex problems which results in the following convoluted code.
1652 	 *
1653 	 * First, a desire to handle a serialization of F_SOFTLOCK calls
1654 	 * to the driver within the framework.
1655 	 *	This results in a dh_softlock structure that is on a per device
1656 	 *	(or device instance) basis and serializes devmap_access calls.
1657 	 *	Ideally we would need to do this for underlying
1658 	 *	memory/device regions that are being faulted on
1659 	 *	but that is hard to identify and with REMAP, harder
1660 	 * Second, a desire to serialize F_INVAL(and F_PROT) calls w.r.t.
1661 	 * 	to F_SOFTLOCK calls to the driver.
1662 	 * These serializations are to simplify the driver programmer model.
1663 	 * To support these two features, the code first goes through the
1664 	 *	devmap handles and counts the pages (slpage) that are covered
1665 	 *	by devmap_access callbacks.
1666 	 * This part ends with a devmap_softlock_enter call
1667 	 *	which allows only one F_SOFTLOCK active on a device instance,
1668 	 *	but multiple F_INVAL/F_PROTs can be active except when a
1669 	 *	F_SOFTLOCK is active
1670 	 *
1671 	 * Next, we dont short-circuit the fault code upfront to call
1672 	 *	segdev_softunlock for F_SOFTUNLOCK, because we must use
1673 	 *	the same length when we softlock and softunlock.
1674 	 *
1675 	 *	-Hat layers may not support softunlocking lengths less than the
1676 	 *	original length when there is large page support.
1677 	 *	-kpmem locking is dependent on keeping the lengths same.
1678 	 *	-if drivers handled F_SOFTLOCK, they probably also expect to
1679 	 *		see an F_SOFTUNLOCK of the same length
1680 	 *	Hence, if extending lengths during softlock,
1681 	 *	softunlock has to make the same adjustments and goes through
1682 	 *	the same loop calling segdev_faultpages/segdev_softunlock
1683 	 *	But some of the synchronization and error handling is different
1684 	 */
1685 
1686 	if (type != F_SOFTUNLOCK) {
1687 		devmap_handle_t *dhpp = dhp;
1688 		size_t slen = len;
1689 
1690 		/*
1691 		 * Calculate count of pages that are :
1692 		 * a) within the (potentially extended) fault region
1693 		 * b) AND covered by devmap handle with devmap_access
1694 		 */
1695 		off = (ulong_t)(addr - dhpp->dh_uvaddr);
1696 		while (slen != 0) {
1697 			size_t mlen;
1698 
1699 			/*
1700 			 * Softlocking on a region that allows remap is
1701 			 * unsupported due to unresolved locking issues
1702 			 * XXX: unclear what these are?
1703 			 *	One potential is that if there is a pending
1704 			 *	softlock, then a remap should not be allowed
1705 			 *	until the unlock is done. This is easily
1706 			 *	fixed by returning error in devmap*remap on
1707 			 *	checking the dh->dh_softlock->softlocked value
1708 			 */
1709 			if ((type == F_SOFTLOCK) &&
1710 			    (dhpp->dh_flags & DEVMAP_ALLOW_REMAP)) {
1711 				return (FC_NOSUPPORT);
1712 			}
1713 
1714 			mlen = MIN(slen, (dhpp->dh_len - off));
1715 			if (dhpp->dh_callbackops.devmap_access) {
1716 				size_t llen;
1717 				caddr_t laddr;
1718 				/*
1719 				 * use extended length for large page mappings
1720 				 */
1721 				HOLD_DHP_LOCK(dhpp);
1722 				if ((sdp->pageprot == 0) &&
1723 				    (dhpp->dh_flags & DEVMAP_FLAG_LARGE)) {
1724 					devmap_get_large_pgsize(dhpp,
1725 					    mlen, maddr, &llen, &laddr);
1726 				} else {
1727 					llen = mlen;
1728 				}
1729 				RELE_DHP_LOCK(dhpp);
1730 
1731 				slpage += btopr(llen);
1732 				slock = dhpp->dh_softlock;
1733 			}
1734 			maddr += mlen;
1735 			ASSERT(slen >= mlen);
1736 			slen -= mlen;
1737 			dhpp = dhpp->dh_next;
1738 			off = 0;
1739 		}
1740 		/*
1741 		 * synchonize with other faulting threads and wait till safe
1742 		 * devmap_softlock_enter might return due to signal in cv_wait
1743 		 *
1744 		 * devmap_softlock_enter has to be called outside of while loop
1745 		 * to prevent a deadlock if len spans over multiple dhps.
1746 		 * dh_softlock is based on device instance and if multiple dhps
1747 		 * use the same device instance, the second dhp's LOCK call
1748 		 * will hang waiting on the first to complete.
1749 		 * devmap_setup verifies that slocks in a dhp_chain are same.
1750 		 * RFE: this deadlock only hold true for F_SOFTLOCK. For
1751 		 * 	F_INVAL/F_PROT, since we now allow multiple in parallel,
1752 		 *	we could have done the softlock_enter inside the loop
1753 		 *	and supported multi-dhp mappings with dissimilar devices
1754 		 */
1755 		if (err = devmap_softlock_enter(slock, slpage, type))
1756 			return (FC_MAKE_ERR(err));
1757 	}
1758 
1759 	/* reset 'maddr' to the start addr of the range of fault. */
1760 	maddr = addr;
1761 
1762 	/* calculate the offset corresponds to 'addr' in the first dhp. */
1763 	off = (ulong_t)(addr - dhp->dh_uvaddr);
1764 
1765 	/*
1766 	 * The fault length may span over multiple dhps.
1767 	 * Loop until the total length is satisfied.
1768 	 */
1769 	while (len != 0) {
1770 		size_t llen;
1771 		size_t mlen;
1772 		caddr_t laddr;
1773 
1774 		/*
1775 		 * mlen is the smaller of 'len' and the length
1776 		 * from addr to the end of mapping defined by dhp.
1777 		 */
1778 		mlen = MIN(len, (dhp->dh_len - off));
1779 
1780 		HOLD_DHP_LOCK(dhp);
1781 		/*
1782 		 * Pass the extended length and address to devmap_access
1783 		 * if large pagesize is used for loading address translations.
1784 		 */
1785 		if ((sdp->pageprot == 0) &&
1786 		    (dhp->dh_flags & DEVMAP_FLAG_LARGE)) {
1787 			devmap_get_large_pgsize(dhp, mlen, maddr,
1788 				&llen, &laddr);
1789 			ASSERT(maddr == addr || laddr == maddr);
1790 		} else {
1791 			llen = mlen;
1792 			laddr = maddr;
1793 		}
1794 
1795 		if (dhp->dh_callbackops.devmap_access != NULL) {
1796 			offset_t aoff;
1797 
1798 			aoff = sdp->offset + (offset_t)(laddr - seg->s_base);
1799 
1800 			/*
1801 			 * call driver's devmap_access entry point which will
1802 			 * call devmap_load/contextmgmt to load the translations
1803 			 *
1804 			 * We drop the dhp_lock before calling access so
1805 			 * drivers can call devmap_*_remap within access
1806 			 */
1807 			RELE_DHP_LOCK(dhp);
1808 
1809 			err = (*dhp->dh_callbackops.devmap_access)(
1810 			    dhp, (void *)dhp->dh_pvtp, aoff, llen, type, rw);
1811 		} else {
1812 			/*
1813 			 * If no devmap_access entry point, then load mappings
1814 			 * hold dhp_lock across faultpages if REMAP
1815 			 */
1816 			err = segdev_faultpages(hat, seg, laddr, llen,
1817 			    type, rw, dhp);
1818 			err_is_faultcode = 1;
1819 			RELE_DHP_LOCK(dhp);
1820 		}
1821 
1822 		if (err) {
1823 			if ((type == F_SOFTLOCK) && (maddr > addr)) {
1824 				/*
1825 				 * If not first dhp, use
1826 				 * segdev_fault(F_SOFTUNLOCK) for prior dhps
1827 				 * While this is recursion, it is incorrect to
1828 				 * call just segdev_softunlock
1829 				 * if we are using either large pages
1830 				 * or devmap_access. It will be more right
1831 				 * to go through the same loop as above
1832 				 * rather than call segdev_softunlock directly
1833 				 * It will use the right lenghths as well as
1834 				 * call into the driver devmap_access routines.
1835 				 */
1836 				size_t done = (size_t)(maddr - addr);
1837 				(void) segdev_fault(hat, seg, addr, done,
1838 					F_SOFTUNLOCK, S_OTHER);
1839 				/*
1840 				 * reduce slpage by number of pages
1841 				 * released by segdev_softunlock
1842 				 */
1843 				ASSERT(slpage >= btopr(done));
1844 				devmap_softlock_exit(slock,
1845 					slpage - btopr(done), type);
1846 			} else {
1847 				devmap_softlock_exit(slock, slpage, type);
1848 			}
1849 
1850 
1851 			/*
1852 			 * Segdev_faultpages() already returns a faultcode,
1853 			 * hence, result from segdev_faultpages() should be
1854 			 * returned directly.
1855 			 */
1856 			if (err_is_faultcode)
1857 				return (err);
1858 			return (FC_MAKE_ERR(err));
1859 		}
1860 
1861 		maddr += mlen;
1862 		ASSERT(len >= mlen);
1863 		len -= mlen;
1864 		dhp = dhp->dh_next;
1865 		off = 0;
1866 
1867 		ASSERT(!dhp || len == 0 || maddr == dhp->dh_uvaddr);
1868 	}
1869 	/*
1870 	 * release the softlock count at end of fault
1871 	 * For F_SOFTLOCk this is done in the later F_SOFTUNLOCK
1872 	 */
1873 	if ((type == F_INVAL) || (type == F_PROT))
1874 		devmap_softlock_exit(slock, slpage, type);
1875 	return (0);
1876 }
1877 
1878 /*
1879  * segdev_faultpages
1880  *
1881  * Used to fault in seg_dev segment pages. Called by segdev_fault or devmap_load
1882  * This routine assumes that the callers makes sure that the fields
1883  * in dhp used below are not changed due to remap during this call.
1884  * Caller does HOLD_DHP_LOCK if neeed
1885  * This routine returns a faultcode_t as a return value for segdev_fault.
1886  */
1887 static faultcode_t
1888 segdev_faultpages(
1889 	struct hat *hat,		/* the hat */
1890 	struct seg *seg,		/* the seg_dev of interest */
1891 	caddr_t addr,			/* the address of the fault */
1892 	size_t len,			/* the length of the range */
1893 	enum fault_type type,		/* type of fault */
1894 	enum seg_rw rw,			/* type of access at fault */
1895 	devmap_handle_t *dhp)		/* devmap handle */
1896 {
1897 	register struct segdev_data *sdp = (struct segdev_data *)seg->s_data;
1898 	register caddr_t a;
1899 	struct vpage *vpage;
1900 	struct ddi_umem_cookie *kpmem_cookie = NULL;
1901 	int err;
1902 
1903 	TRACE_4(TR_FAC_DEVMAP, TR_DEVMAP_FAULTPAGES,
1904 	    "segdev_faultpages: dhp=%p seg=%p addr=%p len=%lx",
1905 	    (void *)dhp, (void *)seg, (void *)addr, len);
1906 	DEBUGF(5, (CE_CONT, "segdev_faultpages: "
1907 	    "dhp %p seg %p addr %p len %lx\n",
1908 	    (void *)dhp, (void *)seg, (void *)addr, len));
1909 
1910 	/*
1911 	 * The seg_dev driver does not implement copy-on-write,
1912 	 * and always loads translations with maximal allowed permissions
1913 	 * but we got an fault trying to access the device.
1914 	 * Servicing the fault is not going to result in any better result
1915 	 * XXX: If we want to allow devmap_access to handle F_PROT calls,
1916 	 * This code should be removed and let the normal fault handling
1917 	 * take care of finding the error
1918 	 */
1919 	if (type == F_PROT) {
1920 		return (FC_PROT);
1921 	}
1922 
1923 	if (type == F_SOFTUNLOCK) {
1924 		segdev_softunlock(hat, seg, addr, len, rw);
1925 		return (0);
1926 	}
1927 
1928 	/*
1929 	 * For kernel pageable memory, fault/lock segkp pages
1930 	 * We hold this until the completion of this
1931 	 * fault (INVAL/PROT) or till unlock (SOFTLOCK).
1932 	 */
1933 	if ((dhp != NULL) && dhp_is_kpmem(dhp)) {
1934 		kpmem_cookie = (struct ddi_umem_cookie *)dhp->dh_cookie;
1935 		if (err = acquire_kpmem_lock(kpmem_cookie, btopr(len)))
1936 			return (err);
1937 	}
1938 
1939 	/*
1940 	 * If we have the same protections for the entire segment,
1941 	 * insure that the access being attempted is legitimate.
1942 	 */
1943 	mutex_enter(&sdp->lock);
1944 	if (sdp->pageprot == 0) {
1945 		uint_t protchk;
1946 
1947 		switch (rw) {
1948 		case S_READ:
1949 			protchk = PROT_READ;
1950 			break;
1951 		case S_WRITE:
1952 			protchk = PROT_WRITE;
1953 			break;
1954 		case S_EXEC:
1955 			protchk = PROT_EXEC;
1956 			break;
1957 		case S_OTHER:
1958 		default:
1959 			protchk = PROT_READ | PROT_WRITE | PROT_EXEC;
1960 			break;
1961 		}
1962 
1963 		if ((sdp->prot & protchk) == 0) {
1964 			mutex_exit(&sdp->lock);
1965 			/* undo kpmem locking */
1966 			if (kpmem_cookie != NULL) {
1967 				release_kpmem_lock(kpmem_cookie, btopr(len));
1968 			}
1969 			return (FC_PROT);	/* illegal access type */
1970 		}
1971 	}
1972 
1973 	/*
1974 	 * we do a single hat_devload for the range if
1975 	 *   - devmap framework (dhp is not NULL),
1976 	 *   - pageprot == 0, i.e., no per-page protection set and
1977 	 *   - is device pages, irrespective of whether we are using large pages
1978 	 */
1979 	if ((sdp->pageprot == 0) && (dhp != NULL) && dhp_is_devmem(dhp)) {
1980 		pfn_t pfnum;
1981 		uint_t hat_flags;
1982 
1983 		if (dhp->dh_flags & DEVMAP_MAPPING_INVALID) {
1984 			mutex_exit(&sdp->lock);
1985 			return (FC_NOMAP);
1986 		}
1987 
1988 		if (type == F_SOFTLOCK) {
1989 			mutex_enter(&freemem_lock);
1990 			sdp->softlockcnt += btopr(len);
1991 			mutex_exit(&freemem_lock);
1992 		}
1993 
1994 		hat_flags = ((type == F_SOFTLOCK) ? HAT_LOAD_LOCK : HAT_LOAD);
1995 		pfnum = dhp->dh_pfn + btop((uintptr_t)(addr - dhp->dh_uvaddr));
1996 		ASSERT(!pf_is_memory(pfnum));
1997 
1998 		hat_devload(hat, addr, len, pfnum, sdp->prot | dhp->dh_hat_attr,
1999 			hat_flags | sdp->hat_flags);
2000 		mutex_exit(&sdp->lock);
2001 		return (0);
2002 	}
2003 
2004 	/* Handle cases where we have to loop through fault handling per-page */
2005 
2006 	if (sdp->vpage == NULL)
2007 		vpage = NULL;
2008 	else
2009 		vpage = &sdp->vpage[seg_page(seg, addr)];
2010 
2011 	/* loop over the address range handling each fault */
2012 	for (a = addr; a < addr + len; a += PAGESIZE) {
2013 		if (err = segdev_faultpage(hat, seg, a, vpage, type, rw, dhp)) {
2014 			break;
2015 		}
2016 		if (vpage != NULL)
2017 			vpage++;
2018 	}
2019 	mutex_exit(&sdp->lock);
2020 	if (err && (type == F_SOFTLOCK)) { /* error handling for F_SOFTLOCK */
2021 		size_t done = (size_t)(a - addr); /* pages fault successfully */
2022 		if (done > 0) {
2023 			/* use softunlock for those pages */
2024 			segdev_softunlock(hat, seg, addr, done, S_OTHER);
2025 		}
2026 		if (kpmem_cookie != NULL) {
2027 			/* release kpmem lock for rest of pages */
2028 			ASSERT(len >= done);
2029 			release_kpmem_lock(kpmem_cookie, btopr(len - done));
2030 		}
2031 	} else if ((kpmem_cookie != NULL) && (type != F_SOFTLOCK)) {
2032 		/* for non-SOFTLOCK cases, release kpmem */
2033 		release_kpmem_lock(kpmem_cookie, btopr(len));
2034 	}
2035 	return (err);
2036 }
2037 
2038 /*
2039  * Asynchronous page fault.  We simply do nothing since this
2040  * entry point is not supposed to load up the translation.
2041  */
2042 /*ARGSUSED*/
2043 static faultcode_t
2044 segdev_faulta(struct seg *seg, caddr_t addr)
2045 {
2046 	TRACE_2(TR_FAC_DEVMAP, TR_DEVMAP_FAULTA,
2047 	    "segdev_faulta: seg=%p addr=%p", (void *)seg, (void *)addr);
2048 	ASSERT(seg->s_as && AS_LOCK_HELD(seg->s_as, &seg->s_as->a_lock));
2049 
2050 	return (0);
2051 }
2052 
2053 static int
2054 segdev_setprot(struct seg *seg, caddr_t addr, size_t len, uint_t prot)
2055 {
2056 	register struct segdev_data *sdp = (struct segdev_data *)seg->s_data;
2057 	register devmap_handle_t *dhp;
2058 	register struct vpage *vp, *evp;
2059 	devmap_handle_t *dhp_head = (devmap_handle_t *)sdp->devmap_data;
2060 	ulong_t off;
2061 	size_t mlen, sz;
2062 
2063 	TRACE_4(TR_FAC_DEVMAP, TR_DEVMAP_SETPROT,
2064 	    "segdev_setprot:start seg=%p addr=%p len=%lx prot=%x",
2065 	    (void *)seg, (void *)addr, len, prot);
2066 	ASSERT(seg->s_as && AS_LOCK_HELD(seg->s_as, &seg->s_as->a_lock));
2067 
2068 	if ((sz = sdp->softlockcnt) > 0 && dhp_head != NULL) {
2069 		/*
2070 		 * Fail the setprot if pages are SOFTLOCKed through this
2071 		 * mapping.
2072 		 * Softlockcnt is protected from change by the as read lock.
2073 		 */
2074 		TRACE_1(TR_FAC_DEVMAP, TR_DEVMAP_SETPROT_CK1,
2075 		    "segdev_setprot:error softlockcnt=%lx", sz);
2076 		DEBUGF(1, (CE_CONT, "segdev_setprot: softlockcnt %ld\n", sz));
2077 		return (EAGAIN);
2078 	}
2079 
2080 	if (dhp_head != NULL) {
2081 		if ((dhp = devmap_find_handle(dhp_head, addr)) == NULL)
2082 			return (EINVAL);
2083 
2084 		/*
2085 		 * check if violate maxprot.
2086 		 */
2087 		off = (ulong_t)(addr - dhp->dh_uvaddr);
2088 		mlen  = len;
2089 		while (dhp) {
2090 			if ((dhp->dh_maxprot & prot) != prot)
2091 				return (EACCES);	/* violated maxprot */
2092 
2093 			if (mlen > (dhp->dh_len - off)) {
2094 				mlen -= dhp->dh_len - off;
2095 				dhp = dhp->dh_next;
2096 				off = 0;
2097 			} else
2098 				break;
2099 		}
2100 	} else {
2101 		if ((sdp->maxprot & prot) != prot)
2102 			return (EACCES);
2103 	}
2104 
2105 	mutex_enter(&sdp->lock);
2106 	if (addr == seg->s_base && len == seg->s_size && sdp->pageprot == 0) {
2107 		if (sdp->prot == prot) {
2108 			mutex_exit(&sdp->lock);
2109 			return (0);			/* all done */
2110 		}
2111 		sdp->prot = (uchar_t)prot;
2112 	} else {
2113 		sdp->pageprot = 1;
2114 		if (sdp->vpage == NULL) {
2115 			/*
2116 			 * First time through setting per page permissions,
2117 			 * initialize all the vpage structures to prot
2118 			 */
2119 			sdp->vpage = kmem_zalloc(vpgtob(seg_pages(seg)),
2120 			    KM_SLEEP);
2121 			evp = &sdp->vpage[seg_pages(seg)];
2122 			for (vp = sdp->vpage; vp < evp; vp++)
2123 				VPP_SETPROT(vp, sdp->prot);
2124 		}
2125 		/*
2126 		 * Now go change the needed vpages protections.
2127 		 */
2128 		evp = &sdp->vpage[seg_page(seg, addr + len)];
2129 		for (vp = &sdp->vpage[seg_page(seg, addr)]; vp < evp; vp++)
2130 			VPP_SETPROT(vp, prot);
2131 	}
2132 	mutex_exit(&sdp->lock);
2133 
2134 	if (dhp_head != NULL) {
2135 		devmap_handle_t *tdhp;
2136 		/*
2137 		 * If large page size was used in hat_devload(),
2138 		 * the same page size must be used in hat_unload().
2139 		 */
2140 		dhp = tdhp = devmap_find_handle(dhp_head, addr);
2141 		while (tdhp != NULL) {
2142 			if (tdhp->dh_flags & DEVMAP_FLAG_LARGE) {
2143 				break;
2144 			}
2145 			tdhp = tdhp->dh_next;
2146 		}
2147 		if (tdhp) {
2148 			size_t slen = len;
2149 			size_t mlen;
2150 			size_t soff;
2151 
2152 			soff = (ulong_t)(addr - dhp->dh_uvaddr);
2153 			while (slen != 0) {
2154 				mlen = MIN(slen, (dhp->dh_len - soff));
2155 				hat_unload(seg->s_as->a_hat, dhp->dh_uvaddr,
2156 					dhp->dh_len, HAT_UNLOAD);
2157 				dhp = dhp->dh_next;
2158 				ASSERT(slen >= mlen);
2159 				slen -= mlen;
2160 				soff = 0;
2161 			}
2162 			return (0);
2163 		}
2164 	}
2165 
2166 	if ((prot & ~PROT_USER) == PROT_NONE) {
2167 		hat_unload(seg->s_as->a_hat, addr, len, HAT_UNLOAD);
2168 	} else {
2169 		/*
2170 		 * RFE: the segment should keep track of all attributes
2171 		 * allowing us to remove the deprecated hat_chgprot
2172 		 * and use hat_chgattr.
2173 		 */
2174 		hat_chgprot(seg->s_as->a_hat, addr, len, prot);
2175 	}
2176 
2177 	return (0);
2178 }
2179 
2180 static int
2181 segdev_checkprot(struct seg *seg, caddr_t addr, size_t len, uint_t prot)
2182 {
2183 	struct segdev_data *sdp = (struct segdev_data *)seg->s_data;
2184 	struct vpage *vp, *evp;
2185 
2186 	TRACE_4(TR_FAC_DEVMAP, TR_DEVMAP_CHECKPROT,
2187 	    "segdev_checkprot:start seg=%p addr=%p len=%lx prot=%x",
2188 	    (void *)seg, (void *)addr, len, prot);
2189 	ASSERT(seg->s_as && AS_LOCK_HELD(seg->s_as, &seg->s_as->a_lock));
2190 
2191 	/*
2192 	 * If segment protection can be used, simply check against them
2193 	 */
2194 	mutex_enter(&sdp->lock);
2195 	if (sdp->pageprot == 0) {
2196 		register int err;
2197 
2198 		err = ((sdp->prot & prot) != prot) ? EACCES : 0;
2199 		mutex_exit(&sdp->lock);
2200 		return (err);
2201 	}
2202 
2203 	/*
2204 	 * Have to check down to the vpage level
2205 	 */
2206 	evp = &sdp->vpage[seg_page(seg, addr + len)];
2207 	for (vp = &sdp->vpage[seg_page(seg, addr)]; vp < evp; vp++) {
2208 		if ((VPP_PROT(vp) & prot) != prot) {
2209 			mutex_exit(&sdp->lock);
2210 			return (EACCES);
2211 		}
2212 	}
2213 	mutex_exit(&sdp->lock);
2214 	return (0);
2215 }
2216 
2217 static int
2218 segdev_getprot(struct seg *seg, caddr_t addr, size_t len, uint_t *protv)
2219 {
2220 	struct segdev_data *sdp = (struct segdev_data *)seg->s_data;
2221 	size_t pgno;
2222 
2223 	TRACE_4(TR_FAC_DEVMAP, TR_DEVMAP_GETPROT,
2224 	    "segdev_getprot:start seg=%p addr=%p len=%lx protv=%p",
2225 	    (void *)seg, (void *)addr, len, (void *)protv);
2226 	ASSERT(seg->s_as && AS_LOCK_HELD(seg->s_as, &seg->s_as->a_lock));
2227 
2228 	pgno = seg_page(seg, addr + len) - seg_page(seg, addr) + 1;
2229 	if (pgno != 0) {
2230 		mutex_enter(&sdp->lock);
2231 		if (sdp->pageprot == 0) {
2232 			do
2233 				protv[--pgno] = sdp->prot;
2234 			while (pgno != 0);
2235 		} else {
2236 			size_t pgoff = seg_page(seg, addr);
2237 
2238 			do {
2239 				pgno--;
2240 				protv[pgno] =
2241 					VPP_PROT(&sdp->vpage[pgno + pgoff]);
2242 			} while (pgno != 0);
2243 		}
2244 		mutex_exit(&sdp->lock);
2245 	}
2246 	return (0);
2247 }
2248 
2249 static u_offset_t
2250 segdev_getoffset(register struct seg *seg, caddr_t addr)
2251 {
2252 	register struct segdev_data *sdp = (struct segdev_data *)seg->s_data;
2253 
2254 	TRACE_2(TR_FAC_DEVMAP, TR_DEVMAP_GETOFFSET,
2255 	    "segdev_getoffset:start seg=%p addr=%p", (void *)seg, (void *)addr);
2256 
2257 	ASSERT(seg->s_as && AS_LOCK_HELD(seg->s_as, &seg->s_as->a_lock));
2258 
2259 	return ((u_offset_t)sdp->offset + (addr - seg->s_base));
2260 }
2261 
2262 /*ARGSUSED*/
2263 static int
2264 segdev_gettype(register struct seg *seg, caddr_t addr)
2265 {
2266 	register struct segdev_data *sdp = (struct segdev_data *)seg->s_data;
2267 
2268 	TRACE_2(TR_FAC_DEVMAP, TR_DEVMAP_GETTYPE,
2269 	    "segdev_gettype:start seg=%p addr=%p", (void *)seg, (void *)addr);
2270 
2271 	ASSERT(seg->s_as && AS_LOCK_HELD(seg->s_as, &seg->s_as->a_lock));
2272 
2273 	return (sdp->type);
2274 }
2275 
2276 
2277 /*ARGSUSED*/
2278 static int
2279 segdev_getvp(register struct seg *seg, caddr_t addr, struct vnode **vpp)
2280 {
2281 	register struct segdev_data *sdp = (struct segdev_data *)seg->s_data;
2282 
2283 	TRACE_2(TR_FAC_DEVMAP, TR_DEVMAP_GETVP,
2284 	    "segdev_getvp:start seg=%p addr=%p", (void *)seg, (void *)addr);
2285 
2286 	ASSERT(seg->s_as && AS_LOCK_HELD(seg->s_as, &seg->s_as->a_lock));
2287 
2288 	/*
2289 	 * Note that this vp is the common_vp of the device, where the
2290 	 * pages are hung ..
2291 	 */
2292 	*vpp = VTOCVP(sdp->vp);
2293 
2294 	return (0);
2295 }
2296 
2297 static void
2298 segdev_badop(void)
2299 {
2300 	TRACE_0(TR_FAC_DEVMAP, TR_DEVMAP_SEGDEV_BADOP,
2301 		"segdev_badop:start");
2302 	panic("segdev_badop");
2303 	/*NOTREACHED*/
2304 }
2305 
2306 /*
2307  * segdev pages are not in the cache, and thus can't really be controlled.
2308  * Hence, syncs are simply always successful.
2309  */
2310 /*ARGSUSED*/
2311 static int
2312 segdev_sync(struct seg *seg, caddr_t addr, size_t len, int attr, uint_t flags)
2313 {
2314 	TRACE_0(TR_FAC_DEVMAP, TR_DEVMAP_SYNC, "segdev_sync:start");
2315 
2316 	ASSERT(seg->s_as && AS_LOCK_HELD(seg->s_as, &seg->s_as->a_lock));
2317 
2318 	return (0);
2319 }
2320 
2321 /*
2322  * segdev pages are always "in core".
2323  */
2324 /*ARGSUSED*/
2325 static size_t
2326 segdev_incore(struct seg *seg, caddr_t addr, size_t len, char *vec)
2327 {
2328 	size_t v = 0;
2329 
2330 	TRACE_0(TR_FAC_DEVMAP, TR_DEVMAP_INCORE, "segdev_incore:start");
2331 
2332 	ASSERT(seg->s_as && AS_LOCK_HELD(seg->s_as, &seg->s_as->a_lock));
2333 
2334 	for (len = (len + PAGEOFFSET) & PAGEMASK; len; len -= PAGESIZE,
2335 	    v += PAGESIZE)
2336 		*vec++ = 1;
2337 	return (v);
2338 }
2339 
2340 /*
2341  * segdev pages are not in the cache, and thus can't really be controlled.
2342  * Hence, locks are simply always successful.
2343  */
2344 /*ARGSUSED*/
2345 static int
2346 segdev_lockop(struct seg *seg, caddr_t addr,
2347     size_t len, int attr, int op, ulong_t *lockmap, size_t pos)
2348 {
2349 	TRACE_0(TR_FAC_DEVMAP, TR_DEVMAP_LOCKOP, "segdev_lockop:start");
2350 
2351 	ASSERT(seg->s_as && AS_LOCK_HELD(seg->s_as, &seg->s_as->a_lock));
2352 
2353 	return (0);
2354 }
2355 
2356 /*
2357  * segdev pages are not in the cache, and thus can't really be controlled.
2358  * Hence, advise is simply always successful.
2359  */
2360 /*ARGSUSED*/
2361 static int
2362 segdev_advise(struct seg *seg, caddr_t addr, size_t len, uint_t behav)
2363 {
2364 	TRACE_0(TR_FAC_DEVMAP, TR_DEVMAP_ADVISE, "segdev_advise:start");
2365 
2366 	ASSERT(seg->s_as && AS_LOCK_HELD(seg->s_as, &seg->s_as->a_lock));
2367 
2368 	return (0);
2369 }
2370 
2371 /*
2372  * segdev pages are not dumped, so we just return
2373  */
2374 /*ARGSUSED*/
2375 static void
2376 segdev_dump(struct seg *seg)
2377 {}
2378 
2379 /*
2380  * ddi_segmap_setup:	Used by drivers who wish specify mapping attributes
2381  *			for a segment.	Called from a drivers segmap(9E)
2382  *			routine.
2383  */
2384 /*ARGSUSED*/
2385 int
2386 ddi_segmap_setup(dev_t dev, off_t offset, struct as *as, caddr_t *addrp,
2387     off_t len, uint_t prot, uint_t maxprot, uint_t flags, cred_t *cred,
2388     ddi_device_acc_attr_t *accattrp, uint_t rnumber)
2389 {
2390 	struct segdev_crargs dev_a;
2391 	int (*mapfunc)(dev_t dev, off_t off, int prot);
2392 	uint_t hat_attr;
2393 	pfn_t pfn;
2394 	int	error, i;
2395 
2396 	TRACE_0(TR_FAC_DEVMAP, TR_DEVMAP_SEGMAP_SETUP,
2397 	    "ddi_segmap_setup:start");
2398 
2399 	if ((mapfunc = devopsp[getmajor(dev)]->devo_cb_ops->cb_mmap) == nodev)
2400 		return (ENODEV);
2401 
2402 	/*
2403 	 * Character devices that support the d_mmap
2404 	 * interface can only be mmap'ed shared.
2405 	 */
2406 	if ((flags & MAP_TYPE) != MAP_SHARED)
2407 		return (EINVAL);
2408 
2409 	/*
2410 	 * Check that this region is indeed mappable on this platform.
2411 	 * Use the mapping function.
2412 	 */
2413 	if (ddi_device_mapping_check(dev, accattrp, rnumber, &hat_attr) == -1)
2414 		return (ENXIO);
2415 
2416 	/*
2417 	 * Check to ensure that the entire range is
2418 	 * legal and we are not trying to map in
2419 	 * more than the device will let us.
2420 	 */
2421 	for (i = 0; i < len; i += PAGESIZE) {
2422 		if (i == 0) {
2423 			/*
2424 			 * Save the pfn at offset here. This pfn will be
2425 			 * used later to get user address.
2426 			 */
2427 			if ((pfn = (pfn_t)cdev_mmap(mapfunc, dev, offset,
2428 				maxprot)) == PFN_INVALID)
2429 				return (ENXIO);
2430 		} else {
2431 			if (cdev_mmap(mapfunc, dev, offset + i, maxprot) ==
2432 				PFN_INVALID)
2433 				return (ENXIO);
2434 		}
2435 	}
2436 
2437 	as_rangelock(as);
2438 	if ((flags & MAP_FIXED) == 0) {
2439 		/*
2440 		 * Pick an address w/o worrying about
2441 		 * any vac alignment constraints.
2442 		 */
2443 		map_addr(addrp, len, ptob(pfn), 0, flags);
2444 		if (*addrp == NULL) {
2445 			as_rangeunlock(as);
2446 			return (ENOMEM);
2447 		}
2448 	} else {
2449 		/*
2450 		 * User-specified address; blow away any previous mappings.
2451 		 */
2452 		(void) as_unmap(as, *addrp, len);
2453 	}
2454 
2455 	dev_a.mapfunc = mapfunc;
2456 	dev_a.dev = dev;
2457 	dev_a.offset = (offset_t)offset;
2458 	dev_a.type = flags & MAP_TYPE;
2459 	dev_a.prot = (uchar_t)prot;
2460 	dev_a.maxprot = (uchar_t)maxprot;
2461 	dev_a.hat_attr = hat_attr;
2462 	dev_a.hat_flags = 0;
2463 	dev_a.devmap_data = NULL;
2464 
2465 	error = as_map(as, *addrp, len, segdev_create, &dev_a);
2466 	as_rangeunlock(as);
2467 	return (error);
2468 
2469 }
2470 
2471 /*ARGSUSED*/
2472 static int
2473 segdev_pagelock(struct seg *seg, caddr_t addr, size_t len,
2474     struct page ***ppp, enum lock_type type, enum seg_rw rw)
2475 {
2476 	TRACE_0(TR_FAC_DEVMAP, TR_DEVMAP_PAGELOCK,
2477 	    "segdev_pagelock:start");
2478 	return (ENOTSUP);
2479 }
2480 
2481 /*ARGSUSED*/
2482 static int
2483 segdev_setpagesize(struct seg *seg, caddr_t addr, size_t len,
2484     uint_t szc)
2485 {
2486 	return (ENOTSUP);
2487 }
2488 
2489 /*
2490  * devmap_device: Used by devmap framework to establish mapping
2491  *                called by devmap_seup(9F) during map setup time.
2492  */
2493 /*ARGSUSED*/
2494 static int
2495 devmap_device(devmap_handle_t *dhp, struct as *as, caddr_t *addr,
2496     offset_t off, size_t len, uint_t flags)
2497 {
2498 	devmap_handle_t *rdhp, *maxdhp;
2499 	struct segdev_crargs dev_a;
2500 	int	err;
2501 	uint_t maxprot = PROT_ALL;
2502 	offset_t offset = 0;
2503 	pfn_t pfn;
2504 	struct devmap_pmem_cookie *pcp;
2505 
2506 	TRACE_4(TR_FAC_DEVMAP, TR_DEVMAP_DEVICE,
2507 	    "devmap_device:start dhp=%p addr=%p off=%llx, len=%lx",
2508 	    (void *)dhp, (void *)addr, off, len);
2509 
2510 	DEBUGF(2, (CE_CONT, "devmap_device: dhp %p addr %p off %llx len %lx\n",
2511 	    (void *)dhp, (void *)addr, off, len));
2512 
2513 	as_rangelock(as);
2514 	if ((flags & MAP_FIXED) == 0) {
2515 		offset_t aligned_off;
2516 
2517 		rdhp = maxdhp = dhp;
2518 		while (rdhp != NULL) {
2519 			maxdhp = (maxdhp->dh_len > rdhp->dh_len) ?
2520 				maxdhp : rdhp;
2521 			rdhp = rdhp->dh_next;
2522 			maxprot |= dhp->dh_maxprot;
2523 		}
2524 		offset = maxdhp->dh_uoff - dhp->dh_uoff;
2525 
2526 		/*
2527 		 * Use the dhp that has the
2528 		 * largest len to get user address.
2529 		 */
2530 		/*
2531 		 * If MAPPING_INVALID, cannot use dh_pfn/dh_cvaddr,
2532 		 * use 0 which is as good as any other.
2533 		 */
2534 		if (maxdhp->dh_flags & DEVMAP_MAPPING_INVALID) {
2535 			aligned_off = (offset_t)0;
2536 		} else if (dhp_is_devmem(maxdhp)) {
2537 			aligned_off = (offset_t)ptob(maxdhp->dh_pfn) - offset;
2538 		} else if (dhp_is_pmem(maxdhp)) {
2539 			pcp = (struct devmap_pmem_cookie *)maxdhp->dh_pcookie;
2540 			pfn = page_pptonum(
2541 			    pcp->dp_pparray[btop(maxdhp->dh_roff)]);
2542 			aligned_off = (offset_t)ptob(pfn) - offset;
2543 		} else {
2544 			aligned_off = (offset_t)(uintptr_t)maxdhp->dh_cvaddr -
2545 			    offset;
2546 		}
2547 
2548 		/*
2549 		 * Pick an address aligned to dh_cookie.
2550 		 * for kernel memory/user memory, cookie is cvaddr.
2551 		 * for device memory, cookie is physical address.
2552 		 */
2553 		map_addr(addr, len, aligned_off, 1, flags);
2554 		if (*addr == NULL) {
2555 			as_rangeunlock(as);
2556 			return (ENOMEM);
2557 		}
2558 	} else {
2559 		/*
2560 		 * User-specified address; blow away any previous mappings.
2561 		 */
2562 		(void) as_unmap(as, *addr, len);
2563 	}
2564 
2565 	dev_a.mapfunc = NULL;
2566 	dev_a.dev = dhp->dh_dev;
2567 	dev_a.type = flags & MAP_TYPE;
2568 	dev_a.offset = off;
2569 	/*
2570 	 * sdp->maxprot has the least restrict protection of all dhps.
2571 	 */
2572 	dev_a.maxprot = maxprot;
2573 	dev_a.prot = dhp->dh_prot;
2574 	/*
2575 	 * devmap uses dhp->dh_hat_attr for hat.
2576 	 */
2577 	dev_a.hat_flags = 0;
2578 	dev_a.hat_attr = 0;
2579 	dev_a.devmap_data = (void *)dhp;
2580 
2581 	err = as_map(as, *addr, len, segdev_create, &dev_a);
2582 	as_rangeunlock(as);
2583 	return (err);
2584 }
2585 
2586 int
2587 devmap_do_ctxmgt(devmap_cookie_t dhc, void *pvtp, offset_t off, size_t len,
2588     uint_t type, uint_t rw, int (*ctxmgt)(devmap_cookie_t, void *, offset_t,
2589     size_t, uint_t, uint_t))
2590 {
2591 	register devmap_handle_t *dhp = (devmap_handle_t *)dhc;
2592 	struct devmap_ctx *devctx;
2593 	int do_timeout = 0;
2594 	int ret;
2595 
2596 #ifdef lint
2597 	pvtp = pvtp;
2598 #endif
2599 
2600 	TRACE_3(TR_FAC_DEVMAP, TR_DEVMAP_DO_CTXMGT,
2601 	    "devmap_do_ctxmgt:start dhp=%p off=%llx, len=%lx",
2602 	    (void *)dhp, off, len);
2603 	DEBUGF(7, (CE_CONT, "devmap_do_ctxmgt: dhp %p off %llx len %lx\n",
2604 	    (void *)dhp, off, len));
2605 
2606 	if (ctxmgt == NULL)
2607 		return (FC_HWERR);
2608 
2609 	devctx = dhp->dh_ctx;
2610 
2611 	/*
2612 	 * If we are on an MP system with more than one cpu running
2613 	 * and if a thread on some CPU already has the context, wait
2614 	 * for it to finish if there is a hysteresis timeout.
2615 	 *
2616 	 * We call cv_wait() instead of cv_wait_sig() because
2617 	 * it does not matter much if it returned due to a signal
2618 	 * or due to a cv_signal() or cv_broadcast().  In either event
2619 	 * we need to complete the mapping otherwise the processes
2620 	 * will die with a SEGV.
2621 	 */
2622 	if ((dhp->dh_timeout_length > 0) && (ncpus > 1)) {
2623 		TRACE_2(TR_FAC_DEVMAP, TR_DEVMAP_DO_CTXMGT_CK1,
2624 		    "devmap_do_ctxmgt:doing hysteresis, devctl %p dhp %p",
2625 		    devctx, dhp);
2626 		do_timeout = 1;
2627 		mutex_enter(&devctx->lock);
2628 		while (devctx->oncpu)
2629 			cv_wait(&devctx->cv, &devctx->lock);
2630 		devctx->oncpu = 1;
2631 		mutex_exit(&devctx->lock);
2632 	}
2633 
2634 	/*
2635 	 * Call the contextmgt callback so that the driver can handle
2636 	 * the fault.
2637 	 */
2638 	ret = (*ctxmgt)(dhp, dhp->dh_pvtp, off, len, type, rw);
2639 
2640 	/*
2641 	 * If devmap_access() returned -1, then there was a hardware
2642 	 * error so we need to convert the return value to something
2643 	 * that trap() will understand.  Otherwise, the return value
2644 	 * is already a fault code generated by devmap_unload()
2645 	 * or devmap_load().
2646 	 */
2647 	if (ret) {
2648 		TRACE_3(TR_FAC_DEVMAP, TR_DEVMAP_DO_CTXMGT_CK2,
2649 		    "devmap_do_ctxmgt: ret=%x dhp=%p devctx=%p",
2650 		    ret, dhp, devctx);
2651 		DEBUGF(1, (CE_CONT, "devmap_do_ctxmgt: ret %x dhp %p\n",
2652 		    ret, (void *)dhp));
2653 		if (devctx->oncpu) {
2654 			mutex_enter(&devctx->lock);
2655 			devctx->oncpu = 0;
2656 			cv_signal(&devctx->cv);
2657 			mutex_exit(&devctx->lock);
2658 		}
2659 		return (FC_HWERR);
2660 	}
2661 
2662 	/*
2663 	 * Setup the timeout if we need to
2664 	 */
2665 	if (do_timeout) {
2666 		mutex_enter(&devctx->lock);
2667 		if (dhp->dh_timeout_length > 0) {
2668 			TRACE_0(TR_FAC_DEVMAP, TR_DEVMAP_DO_CTXMGT_CK3,
2669 			    "devmap_do_ctxmgt:timeout set");
2670 			devctx->timeout = timeout(devmap_ctxto,
2671 			    devctx, dhp->dh_timeout_length);
2672 		} else {
2673 			/*
2674 			 * We don't want to wait so set oncpu to
2675 			 * 0 and wake up anyone waiting.
2676 			 */
2677 			TRACE_0(TR_FAC_DEVMAP, TR_DEVMAP_DO_CTXMGT_CK4,
2678 			    "devmap_do_ctxmgt:timeout not set");
2679 			devctx->oncpu = 0;
2680 			cv_signal(&devctx->cv);
2681 		}
2682 		mutex_exit(&devctx->lock);
2683 	}
2684 
2685 	return (DDI_SUCCESS);
2686 }
2687 
2688 /*
2689  *                                       end of mapping
2690  *                    poff   fault_offset         |
2691  *            base     |        |                 |
2692  *              |      |        |                 |
2693  *              V      V        V                 V
2694  *  +-----------+---------------+-------+---------+-------+
2695  *              ^               ^       ^         ^
2696  *              |<--- offset--->|<-len->|         |
2697  *              |<--- dh_len(size of mapping) --->|
2698  *                     |<--  pg -->|
2699  *                              -->|rlen|<--
2700  */
2701 static ulong_t
2702 devmap_roundup(devmap_handle_t *dhp, ulong_t offset, size_t len,
2703     ulong_t *opfn, ulong_t *pagesize)
2704 {
2705 	register int level;
2706 	ulong_t pg;
2707 	ulong_t poff;
2708 	ulong_t base;
2709 	caddr_t uvaddr;
2710 	long rlen;
2711 
2712 	TRACE_3(TR_FAC_DEVMAP, TR_DEVMAP_ROUNDUP,
2713 	    "devmap_roundup:start dhp=%p off=%lx len=%lx",
2714 	    (void *)dhp, offset, len);
2715 	DEBUGF(2, (CE_CONT, "devmap_roundup: dhp %p off %lx len %lx\n",
2716 	    (void *)dhp, offset, len));
2717 
2718 	/*
2719 	 * get the max. pagesize that is aligned within the range
2720 	 * <dh_pfn, dh_pfn+offset>.
2721 	 *
2722 	 * The calculations below use physical address to ddetermine
2723 	 * the page size to use. The same calculations can use the
2724 	 * virtual address to determine the page size.
2725 	 */
2726 	base = (ulong_t)ptob(dhp->dh_pfn);
2727 	for (level = dhp->dh_mmulevel; level >= 0; level--) {
2728 		pg = page_get_pagesize(level);
2729 		poff = ((base + offset) & ~(pg - 1));
2730 		uvaddr = dhp->dh_uvaddr + (poff - base);
2731 		if ((poff >= base) &&
2732 		    ((poff + pg) <= (base + dhp->dh_len)) &&
2733 		    VA_PA_ALIGNED((uintptr_t)uvaddr, poff, pg))
2734 			break;
2735 	}
2736 
2737 	TRACE_3(TR_FAC_DEVMAP, TR_DEVMAP_ROUNDUP_CK1,
2738 	    "devmap_roundup: base=%lx poff=%lx dhp=%p",
2739 	    base, poff, dhp);
2740 	DEBUGF(2, (CE_CONT, "devmap_roundup: base %lx poff %lx pfn %lx\n",
2741 	    base, poff, dhp->dh_pfn));
2742 
2743 	ASSERT(VA_PA_ALIGNED((uintptr_t)uvaddr, poff, pg));
2744 	ASSERT(level >= 0);
2745 
2746 	*pagesize = pg;
2747 	*opfn = dhp->dh_pfn + btop(poff - base);
2748 
2749 	rlen = len + offset - (poff - base + pg);
2750 
2751 	ASSERT(rlen < (long)len);
2752 
2753 	TRACE_5(TR_FAC_DEVMAP, TR_DEVMAP_ROUNDUP_CK2,
2754 	    "devmap_roundup:ret dhp=%p level=%x rlen=%lx psiz=%p opfn=%p",
2755 	    (void *)dhp, level, rlen, pagesize, opfn);
2756 	DEBUGF(1, (CE_CONT, "devmap_roundup: dhp %p "
2757 	    "level %x rlen %lx psize %lx opfn %lx\n",
2758 	    (void *)dhp, level, rlen, *pagesize, *opfn));
2759 
2760 	return ((ulong_t)((rlen > 0) ? rlen : 0));
2761 }
2762 
2763 /*
2764  * find the dhp that contains addr.
2765  */
2766 static devmap_handle_t *
2767 devmap_find_handle(devmap_handle_t *dhp_head, caddr_t addr)
2768 {
2769 	devmap_handle_t *dhp;
2770 
2771 	TRACE_0(TR_FAC_DEVMAP, TR_DEVMAP_FIND_HANDLE,
2772 	    "devmap_find_handle:start");
2773 
2774 	dhp = dhp_head;
2775 	while (dhp) {
2776 		if (addr >= dhp->dh_uvaddr &&
2777 		    addr < (dhp->dh_uvaddr + dhp->dh_len))
2778 			return (dhp);
2779 		dhp = dhp->dh_next;
2780 	}
2781 
2782 	return ((devmap_handle_t *)NULL);
2783 }
2784 
2785 /*
2786  * devmap_unload:
2787  *			Marks a segdev segment or pages if offset->offset+len
2788  *			is not the entire segment as intercept and unloads the
2789  *			pages in the range offset -> offset+len.
2790  */
2791 int
2792 devmap_unload(devmap_cookie_t dhc, offset_t offset, size_t len)
2793 {
2794 	register devmap_handle_t *dhp = (devmap_handle_t *)dhc;
2795 	caddr_t	addr;
2796 	ulong_t	size;
2797 	ssize_t	soff;
2798 
2799 	TRACE_3(TR_FAC_DEVMAP, TR_DEVMAP_UNLOAD,
2800 	    "devmap_unload:start dhp=%p offset=%llx len=%lx",
2801 	    (void *)dhp, offset, len);
2802 	DEBUGF(7, (CE_CONT, "devmap_unload: dhp %p offset %llx len %lx\n",
2803 	    (void *)dhp, offset, len));
2804 
2805 	soff = (ssize_t)(offset - dhp->dh_uoff);
2806 	soff = round_down_p2(soff, PAGESIZE);
2807 	if (soff < 0 || soff >= dhp->dh_len)
2808 		return (FC_MAKE_ERR(EINVAL));
2809 
2810 	/*
2811 	 * Address and size must be page aligned.  Len is set to the
2812 	 * number of bytes in the number of pages that are required to
2813 	 * support len.  Offset is set to the byte offset of the first byte
2814 	 * of the page that contains offset.
2815 	 */
2816 	len = round_up_p2(len, PAGESIZE);
2817 
2818 	/*
2819 	 * If len is == 0, then calculate the size by getting
2820 	 * the number of bytes from offset to the end of the segment.
2821 	 */
2822 	if (len == 0)
2823 		size = dhp->dh_len - soff;
2824 	else {
2825 		size = len;
2826 		if ((soff + size) > dhp->dh_len)
2827 			return (FC_MAKE_ERR(EINVAL));
2828 	}
2829 
2830 	/*
2831 	 * The address is offset bytes from the base address of
2832 	 * the dhp.
2833 	 */
2834 	addr = (caddr_t)(soff + dhp->dh_uvaddr);
2835 
2836 	/*
2837 	 * If large page size was used in hat_devload(),
2838 	 * the same page size must be used in hat_unload().
2839 	 */
2840 	if (dhp->dh_flags & DEVMAP_FLAG_LARGE) {
2841 		hat_unload(dhp->dh_seg->s_as->a_hat, dhp->dh_uvaddr,
2842 			dhp->dh_len, HAT_UNLOAD|HAT_UNLOAD_OTHER);
2843 	} else {
2844 		hat_unload(dhp->dh_seg->s_as->a_hat,  addr, size,
2845 			HAT_UNLOAD|HAT_UNLOAD_OTHER);
2846 	}
2847 
2848 	return (0);
2849 }
2850 
2851 /*
2852  * calculates the optimal page size that will be used for hat_devload().
2853  */
2854 static void
2855 devmap_get_large_pgsize(devmap_handle_t *dhp, size_t len, caddr_t addr,
2856     size_t *llen, caddr_t *laddr)
2857 {
2858 	ulong_t off;
2859 	ulong_t pfn;
2860 	ulong_t pgsize;
2861 	uint_t first = 1;
2862 
2863 	TRACE_0(TR_FAC_DEVMAP, TR_DEVMAP_GET_LARGE_PGSIZE,
2864 	    "devmap_get_large_pgsize:start");
2865 
2866 	/*
2867 	 * RFE - Code only supports large page mappings for devmem
2868 	 * This code could be changed in future if we want to support
2869 	 * large page mappings for kernel exported memory.
2870 	 */
2871 	ASSERT(dhp_is_devmem(dhp));
2872 	ASSERT(!(dhp->dh_flags & DEVMAP_MAPPING_INVALID));
2873 
2874 	*llen = 0;
2875 	off = (ulong_t)(addr - dhp->dh_uvaddr);
2876 	while ((long)len > 0) {
2877 		/*
2878 		 * get the optimal pfn to minimize address translations.
2879 		 * devmap_roundup() returns residue bytes for next round
2880 		 * calculations.
2881 		 */
2882 		len = devmap_roundup(dhp, off, len, &pfn, &pgsize);
2883 
2884 		if (first) {
2885 			*laddr = dhp->dh_uvaddr + ptob(pfn - dhp->dh_pfn);
2886 			first = 0;
2887 		}
2888 
2889 		*llen += pgsize;
2890 		off = ptob(pfn - dhp->dh_pfn) + pgsize;
2891 	}
2892 	/* Large page mapping len/addr cover more range than orginal fault */
2893 	ASSERT(*llen >= len && *laddr <= addr);
2894 	ASSERT((*laddr + *llen) >= (addr + len));
2895 }
2896 
2897 /*
2898  * Initialize the devmap_softlock structure.
2899  */
2900 static struct devmap_softlock *
2901 devmap_softlock_init(dev_t dev, ulong_t id)
2902 {
2903 	struct devmap_softlock *slock;
2904 	struct devmap_softlock *tmp;
2905 
2906 	TRACE_0(TR_FAC_DEVMAP, TR_DEVMAP_SOFTLOCK_INIT,
2907 	    "devmap_softlock_init:start");
2908 
2909 	tmp = kmem_zalloc(sizeof (struct devmap_softlock), KM_SLEEP);
2910 	mutex_enter(&devmap_slock);
2911 
2912 	for (slock = devmap_slist; slock != NULL; slock = slock->next)
2913 		if ((slock->dev == dev) && (slock->id == id))
2914 			break;
2915 
2916 	if (slock == NULL) {
2917 		slock = tmp;
2918 		slock->dev = dev;
2919 		slock->id = id;
2920 		mutex_init(&slock->lock, NULL, MUTEX_DEFAULT, NULL);
2921 		cv_init(&slock->cv, NULL, CV_DEFAULT, NULL);
2922 		slock->next = devmap_slist;
2923 		devmap_slist = slock;
2924 	} else
2925 		kmem_free(tmp, sizeof (struct devmap_softlock));
2926 
2927 	mutex_enter(&slock->lock);
2928 	slock->refcnt++;
2929 	mutex_exit(&slock->lock);
2930 	mutex_exit(&devmap_slock);
2931 
2932 	return (slock);
2933 }
2934 
2935 /*
2936  * Wake up processes that sleep on softlocked.
2937  * Free dh_softlock if refcnt is 0.
2938  */
2939 static void
2940 devmap_softlock_rele(devmap_handle_t *dhp)
2941 {
2942 	struct devmap_softlock *slock = dhp->dh_softlock;
2943 	struct devmap_softlock *tmp;
2944 	struct devmap_softlock *parent;
2945 
2946 	TRACE_0(TR_FAC_DEVMAP, TR_DEVMAP_SOFTLOCK_RELE,
2947 	    "devmap_softlock_rele:start");
2948 
2949 	mutex_enter(&devmap_slock);
2950 	mutex_enter(&slock->lock);
2951 
2952 	ASSERT(slock->refcnt > 0);
2953 
2954 	slock->refcnt--;
2955 
2956 	/*
2957 	 * If no one is using the device, free up the slock data.
2958 	 */
2959 	if (slock->refcnt == 0) {
2960 		slock->softlocked = 0;
2961 		cv_signal(&slock->cv);
2962 
2963 		if (devmap_slist == slock)
2964 			devmap_slist = slock->next;
2965 		else {
2966 			parent = devmap_slist;
2967 			for (tmp = devmap_slist->next; tmp != NULL;
2968 				tmp = tmp->next) {
2969 				if (tmp == slock) {
2970 					parent->next = tmp->next;
2971 					break;
2972 				}
2973 				parent = tmp;
2974 			}
2975 		}
2976 		mutex_exit(&slock->lock);
2977 		mutex_destroy(&slock->lock);
2978 		cv_destroy(&slock->cv);
2979 		kmem_free(slock, sizeof (struct devmap_softlock));
2980 	} else
2981 		mutex_exit(&slock->lock);
2982 
2983 	mutex_exit(&devmap_slock);
2984 }
2985 
2986 /*
2987  * Wake up processes that sleep on dh_ctx->locked.
2988  * Free dh_ctx if refcnt is 0.
2989  */
2990 static void
2991 devmap_ctx_rele(devmap_handle_t *dhp)
2992 {
2993 	struct devmap_ctx *devctx = dhp->dh_ctx;
2994 	struct devmap_ctx *tmp;
2995 	struct devmap_ctx *parent;
2996 	timeout_id_t tid;
2997 
2998 	TRACE_0(TR_FAC_DEVMAP, TR_DEVMAP_CTX_RELE,
2999 	    "devmap_ctx_rele:start");
3000 
3001 	mutex_enter(&devmapctx_lock);
3002 	mutex_enter(&devctx->lock);
3003 
3004 	ASSERT(devctx->refcnt > 0);
3005 
3006 	devctx->refcnt--;
3007 
3008 	/*
3009 	 * If no one is using the device, free up the devctx data.
3010 	 */
3011 	if (devctx->refcnt == 0) {
3012 		/*
3013 		 * Untimeout any threads using this mapping as they are about
3014 		 * to go away.
3015 		 */
3016 		if (devctx->timeout != 0) {
3017 			TRACE_0(TR_FAC_DEVMAP, TR_DEVMAP_CTX_RELE_CK1,
3018 			    "devmap_ctx_rele:untimeout ctx->timeout");
3019 
3020 			tid = devctx->timeout;
3021 			mutex_exit(&devctx->lock);
3022 			(void) untimeout(tid);
3023 			mutex_enter(&devctx->lock);
3024 		}
3025 
3026 		devctx->oncpu = 0;
3027 		cv_signal(&devctx->cv);
3028 
3029 		if (devmapctx_list == devctx)
3030 			devmapctx_list = devctx->next;
3031 		else {
3032 			parent = devmapctx_list;
3033 			for (tmp = devmapctx_list->next; tmp != NULL;
3034 				tmp = tmp->next) {
3035 				if (tmp == devctx) {
3036 					parent->next = tmp->next;
3037 					break;
3038 				}
3039 				parent = tmp;
3040 			}
3041 		}
3042 		mutex_exit(&devctx->lock);
3043 		mutex_destroy(&devctx->lock);
3044 		cv_destroy(&devctx->cv);
3045 		kmem_free(devctx, sizeof (struct devmap_ctx));
3046 	} else
3047 		mutex_exit(&devctx->lock);
3048 
3049 	mutex_exit(&devmapctx_lock);
3050 }
3051 
3052 /*
3053  * devmap_load:
3054  *			Marks a segdev segment or pages if offset->offset+len
3055  *			is not the entire segment as nointercept and faults in
3056  *			the pages in the range offset -> offset+len.
3057  */
3058 int
3059 devmap_load(devmap_cookie_t dhc, offset_t offset, size_t len, uint_t type,
3060     uint_t rw)
3061 {
3062 	devmap_handle_t *dhp = (devmap_handle_t *)dhc;
3063 	struct as *asp = dhp->dh_seg->s_as;
3064 	caddr_t	addr;
3065 	ulong_t	size;
3066 	ssize_t	soff;	/* offset from the beginning of the segment */
3067 	int rc;
3068 
3069 	TRACE_3(TR_FAC_DEVMAP, TR_DEVMAP_LOAD,
3070 	    "devmap_load:start dhp=%p offset=%llx len=%lx",
3071 		(void *)dhp, offset, len);
3072 
3073 	DEBUGF(7, (CE_CONT, "devmap_load: dhp %p offset %llx len %lx\n",
3074 	    (void *)dhp, offset, len));
3075 
3076 	/*
3077 	 *	Hat layer only supports devload to process' context for which
3078 	 *	the as lock is held. Verify here and return error if drivers
3079 	 *	inadvertently call devmap_load on a wrong devmap handle.
3080 	 */
3081 	if ((asp != &kas) && !AS_LOCK_HELD(asp, &asp->a_lock))
3082 		return (FC_MAKE_ERR(EINVAL));
3083 
3084 	soff = (ssize_t)(offset - dhp->dh_uoff);
3085 	soff = round_down_p2(soff, PAGESIZE);
3086 	if (soff < 0 || soff >= dhp->dh_len)
3087 		return (FC_MAKE_ERR(EINVAL));
3088 
3089 	/*
3090 	 * Address and size must be page aligned.  Len is set to the
3091 	 * number of bytes in the number of pages that are required to
3092 	 * support len.  Offset is set to the byte offset of the first byte
3093 	 * of the page that contains offset.
3094 	 */
3095 	len = round_up_p2(len, PAGESIZE);
3096 
3097 	/*
3098 	 * If len == 0, then calculate the size by getting
3099 	 * the number of bytes from offset to the end of the segment.
3100 	 */
3101 	if (len == 0)
3102 		size = dhp->dh_len - soff;
3103 	else {
3104 		size = len;
3105 		if ((soff + size) > dhp->dh_len)
3106 			return (FC_MAKE_ERR(EINVAL));
3107 	}
3108 
3109 	/*
3110 	 * The address is offset bytes from the base address of
3111 	 * the segment.
3112 	 */
3113 	addr = (caddr_t)(soff + dhp->dh_uvaddr);
3114 
3115 	HOLD_DHP_LOCK(dhp);
3116 	rc = segdev_faultpages(asp->a_hat,
3117 			dhp->dh_seg, addr, size, type, rw, dhp);
3118 	RELE_DHP_LOCK(dhp);
3119 	return (rc);
3120 }
3121 
3122 int
3123 devmap_setup(dev_t dev, offset_t off, struct as *as, caddr_t *addrp,
3124     size_t len, uint_t prot, uint_t maxprot, uint_t flags, struct cred *cred)
3125 {
3126 	register devmap_handle_t *dhp;
3127 	int (*devmap)(dev_t, devmap_cookie_t, offset_t, size_t,
3128 		size_t *, uint_t);
3129 	int (*mmap)(dev_t, off_t, int);
3130 	struct devmap_callback_ctl *callbackops;
3131 	devmap_handle_t *dhp_head = NULL;
3132 	devmap_handle_t *dhp_prev = NULL;
3133 	devmap_handle_t *dhp_curr;
3134 	caddr_t addr;
3135 	int map_flag;
3136 	int ret;
3137 	ulong_t total_len;
3138 	size_t map_len;
3139 	size_t resid_len = len;
3140 	offset_t map_off = off;
3141 	struct devmap_softlock *slock = NULL;
3142 
3143 #ifdef lint
3144 	cred = cred;
3145 #endif
3146 
3147 	TRACE_2(TR_FAC_DEVMAP, TR_DEVMAP_SETUP,
3148 	    "devmap_setup:start off=%llx len=%lx", off, len);
3149 	DEBUGF(3, (CE_CONT, "devmap_setup: off %llx len %lx\n",
3150 	    off, len));
3151 
3152 	devmap = devopsp[getmajor(dev)]->devo_cb_ops->cb_devmap;
3153 	mmap = devopsp[getmajor(dev)]->devo_cb_ops->cb_mmap;
3154 
3155 	/*
3156 	 * driver must provide devmap(9E) entry point in cb_ops to use the
3157 	 * devmap framework.
3158 	 */
3159 	if (devmap == NULL || devmap == nulldev || devmap == nodev)
3160 		return (EINVAL);
3161 
3162 	/*
3163 	 * To protect from an inadvertent entry because the devmap entry point
3164 	 * is not NULL, return error if D_DEVMAP bit is not set in cb_flag and
3165 	 * mmap is NULL.
3166 	 */
3167 	map_flag = devopsp[getmajor(dev)]->devo_cb_ops->cb_flag;
3168 	if ((map_flag & D_DEVMAP) == 0 && (mmap == NULL || mmap == nulldev))
3169 		return (EINVAL);
3170 
3171 	/*
3172 	 * devmap allows mmap(2) to map multiple registers.
3173 	 * one devmap_handle is created for each register mapped.
3174 	 */
3175 	for (total_len = 0; total_len < len; total_len += map_len) {
3176 		dhp = kmem_zalloc(sizeof (devmap_handle_t), KM_SLEEP);
3177 
3178 		if (dhp_prev != NULL)
3179 			dhp_prev->dh_next = dhp;
3180 		else
3181 			dhp_head = dhp;
3182 		dhp_prev = dhp;
3183 
3184 		dhp->dh_prot = prot;
3185 		dhp->dh_orig_maxprot = dhp->dh_maxprot = maxprot;
3186 		dhp->dh_dev = dev;
3187 		dhp->dh_timeout_length = CTX_TIMEOUT_VALUE;
3188 		dhp->dh_uoff = map_off;
3189 
3190 		/*
3191 		 * Get mapping specific info from
3192 		 * the driver, such as rnumber, roff, len, callbackops,
3193 		 * accattrp and, if the mapping is for kernel memory,
3194 		 * ddi_umem_cookie.
3195 		 */
3196 		if ((ret = cdev_devmap(dev, dhp, map_off,
3197 		    resid_len, &map_len, get_udatamodel())) != 0) {
3198 			free_devmap_handle(dhp_head);
3199 			return (ENXIO);
3200 		}
3201 
3202 		if (map_len & PAGEOFFSET) {
3203 			free_devmap_handle(dhp_head);
3204 			return (EINVAL);
3205 		}
3206 
3207 		callbackops = &dhp->dh_callbackops;
3208 
3209 		if ((callbackops->devmap_access == NULL) ||
3210 			(callbackops->devmap_access == nulldev) ||
3211 			(callbackops->devmap_access == nodev)) {
3212 			/*
3213 			 * Normally devmap does not support MAP_PRIVATE unless
3214 			 * the drivers provide a valid devmap_access routine.
3215 			 */
3216 			if ((flags & MAP_PRIVATE) != 0) {
3217 				free_devmap_handle(dhp_head);
3218 				return (EINVAL);
3219 			}
3220 		} else {
3221 			/*
3222 			 * Initialize dhp_softlock and dh_ctx if the drivers
3223 			 * provide devmap_access.
3224 			 */
3225 			dhp->dh_softlock = devmap_softlock_init(dev,
3226 				(ulong_t)callbackops->devmap_access);
3227 			dhp->dh_ctx = devmap_ctxinit(dev,
3228 				(ulong_t)callbackops->devmap_access);
3229 
3230 			/*
3231 			 * segdev_fault can only work when all
3232 			 * dh_softlock in a multi-dhp mapping
3233 			 * are same. see comments in segdev_fault
3234 			 * This code keeps track of the first
3235 			 * dh_softlock allocated in slock and
3236 			 * compares all later allocations and if
3237 			 * not similar, returns an error.
3238 			 */
3239 			if (slock == NULL)
3240 				slock = dhp->dh_softlock;
3241 			if (slock != dhp->dh_softlock) {
3242 				free_devmap_handle(dhp_head);
3243 				return (ENOTSUP);
3244 			}
3245 		}
3246 
3247 		map_off += map_len;
3248 		resid_len -= map_len;
3249 	}
3250 
3251 	/*
3252 	 * get the user virtual address and establish the mapping between
3253 	 * uvaddr and device physical address.
3254 	 */
3255 	if ((ret = devmap_device(dhp_head, as, addrp, off, len, flags))
3256 			!= 0) {
3257 		/*
3258 		 * free devmap handles if error during the mapping.
3259 		 */
3260 		free_devmap_handle(dhp_head);
3261 
3262 		return (ret);
3263 	}
3264 
3265 	/*
3266 	 * call the driver's devmap_map callback to do more after the mapping,
3267 	 * such as to allocate driver private data for context management.
3268 	 */
3269 	dhp = dhp_head;
3270 	map_off = off;
3271 	addr = *addrp;
3272 	while (dhp != NULL) {
3273 		callbackops = &dhp->dh_callbackops;
3274 		dhp->dh_uvaddr = addr;
3275 		dhp_curr = dhp;
3276 		if (callbackops->devmap_map != NULL) {
3277 			ret = (*callbackops->devmap_map)((devmap_cookie_t)dhp,
3278 					dev, flags, map_off,
3279 					dhp->dh_len, &dhp->dh_pvtp);
3280 			if (ret != 0) {
3281 				struct segdev_data *sdp;
3282 
3283 				/*
3284 				 * call driver's devmap_unmap entry point
3285 				 * to free driver resources.
3286 				 */
3287 				dhp = dhp_head;
3288 				map_off = off;
3289 				while (dhp != dhp_curr) {
3290 					callbackops = &dhp->dh_callbackops;
3291 					if (callbackops->devmap_unmap != NULL) {
3292 						(*callbackops->devmap_unmap)(
3293 							dhp, dhp->dh_pvtp,
3294 							map_off, dhp->dh_len,
3295 							NULL, NULL, NULL, NULL);
3296 					}
3297 					map_off += dhp->dh_len;
3298 					dhp = dhp->dh_next;
3299 				}
3300 				sdp = dhp_head->dh_seg->s_data;
3301 				sdp->devmap_data = NULL;
3302 				free_devmap_handle(dhp_head);
3303 				return (ENXIO);
3304 			}
3305 		}
3306 		map_off += dhp->dh_len;
3307 		addr += dhp->dh_len;
3308 		dhp = dhp->dh_next;
3309 	}
3310 
3311 	return (0);
3312 }
3313 
3314 int
3315 ddi_devmap_segmap(dev_t dev, off_t off, ddi_as_handle_t as, caddr_t *addrp,
3316     off_t len, uint_t prot, uint_t maxprot, uint_t flags, struct cred *cred)
3317 {
3318 	TRACE_0(TR_FAC_DEVMAP, TR_DEVMAP_SEGMAP,
3319 	    "devmap_segmap:start");
3320 	return (devmap_setup(dev, (offset_t)off, (struct as *)as, addrp,
3321 	    (size_t)len, prot, maxprot, flags, cred));
3322 }
3323 
3324 /*
3325  * Called from devmap_devmem_setup/remap to see if can use large pages for
3326  * this device mapping.
3327  * Also calculate the max. page size for this mapping.
3328  * this page size will be used in fault routine for
3329  * optimal page size calculations.
3330  */
3331 static void
3332 devmap_devmem_large_page_setup(devmap_handle_t *dhp)
3333 {
3334 	ASSERT(dhp_is_devmem(dhp));
3335 	dhp->dh_mmulevel = 0;
3336 
3337 	/*
3338 	 * use large page size only if:
3339 	 *  1. device memory.
3340 	 *  2. mmu supports multiple page sizes,
3341 	 *  3. Driver did not disallow it
3342 	 *  4. dhp length is at least as big as the large pagesize
3343 	 *  5. the uvaddr and pfn are large pagesize aligned
3344 	 */
3345 	if (page_num_pagesizes() > 1 &&
3346 	    !(dhp->dh_flags & (DEVMAP_USE_PAGESIZE | DEVMAP_MAPPING_INVALID))) {
3347 		ulong_t base;
3348 		int level;
3349 
3350 		base = (ulong_t)ptob(dhp->dh_pfn);
3351 		for (level = 1; level < page_num_pagesizes(); level++) {
3352 			size_t pgsize = page_get_pagesize(level);
3353 			if ((dhp->dh_len < pgsize) ||
3354 			    (!VA_PA_PGSIZE_ALIGNED((uintptr_t)dhp->dh_uvaddr,
3355 					base, pgsize))) {
3356 				break;
3357 			}
3358 		}
3359 		dhp->dh_mmulevel = level - 1;
3360 	}
3361 	if (dhp->dh_mmulevel > 0) {
3362 		dhp->dh_flags |= DEVMAP_FLAG_LARGE;
3363 	} else {
3364 		dhp->dh_flags &= ~DEVMAP_FLAG_LARGE;
3365 	}
3366 }
3367 
3368 /*
3369  * Called by driver devmap routine to pass device specific info to
3370  * the framework.    used for device memory mapping only.
3371  */
3372 int
3373 devmap_devmem_setup(devmap_cookie_t dhc, dev_info_t *dip,
3374     struct devmap_callback_ctl *callbackops, uint_t rnumber, offset_t roff,
3375     size_t len, uint_t maxprot, uint_t flags, ddi_device_acc_attr_t *accattrp)
3376 {
3377 	devmap_handle_t *dhp = (devmap_handle_t *)dhc;
3378 	ddi_acc_handle_t handle;
3379 	ddi_map_req_t mr;
3380 	ddi_acc_hdl_t *hp;
3381 	int err;
3382 
3383 	TRACE_4(TR_FAC_DEVMAP, TR_DEVMAP_DEVMEM_SETUP,
3384 	    "devmap_devmem_setup:start dhp=%p offset=%llx rnum=%d len=%lx",
3385 	    (void *)dhp, roff, rnumber, (uint_t)len);
3386 	DEBUGF(2, (CE_CONT, "devmap_devmem_setup: dhp %p offset %llx "
3387 	    "rnum %d len %lx\n", (void *)dhp, roff, rnumber, len));
3388 
3389 	/*
3390 	 * First to check if this function has been called for this dhp.
3391 	 */
3392 	if (dhp->dh_flags & DEVMAP_SETUP_DONE)
3393 		return (DDI_FAILURE);
3394 
3395 	if ((dhp->dh_prot & dhp->dh_orig_maxprot & maxprot) != dhp->dh_prot)
3396 		return (DDI_FAILURE);
3397 
3398 	if (flags & DEVMAP_MAPPING_INVALID) {
3399 		/*
3400 		 * Don't go up the tree to get pfn if the driver specifies
3401 		 * DEVMAP_MAPPING_INVALID in flags.
3402 		 *
3403 		 * If DEVMAP_MAPPING_INVALID is specified, we have to grant
3404 		 * remap permission.
3405 		 */
3406 		if (!(flags & DEVMAP_ALLOW_REMAP)) {
3407 			return (DDI_FAILURE);
3408 		}
3409 		dhp->dh_pfn = PFN_INVALID;
3410 	} else {
3411 		handle = impl_acc_hdl_alloc(KM_SLEEP, NULL);
3412 		if (handle == NULL)
3413 			return (DDI_FAILURE);
3414 
3415 		hp = impl_acc_hdl_get(handle);
3416 		hp->ah_vers = VERS_ACCHDL;
3417 		hp->ah_dip = dip;
3418 		hp->ah_rnumber = rnumber;
3419 		hp->ah_offset = roff;
3420 		hp->ah_len = len;
3421 		if (accattrp != NULL)
3422 			hp->ah_acc = *accattrp;
3423 
3424 		mr.map_op = DDI_MO_MAP_LOCKED;
3425 		mr.map_type = DDI_MT_RNUMBER;
3426 		mr.map_obj.rnumber = rnumber;
3427 		mr.map_prot = maxprot & dhp->dh_orig_maxprot;
3428 		mr.map_flags = DDI_MF_DEVICE_MAPPING;
3429 		mr.map_handlep = hp;
3430 		mr.map_vers = DDI_MAP_VERSION;
3431 
3432 		/*
3433 		 * up the device tree to get pfn.
3434 		 * The rootnex_map_regspec() routine in nexus drivers has been
3435 		 * modified to return pfn if map_flags is DDI_MF_DEVICE_MAPPING.
3436 		 */
3437 		err = ddi_map(dip, &mr, roff, len, (caddr_t *)&dhp->dh_pfn);
3438 		dhp->dh_hat_attr = hp->ah_hat_flags;
3439 		impl_acc_hdl_free(handle);
3440 
3441 		if (err)
3442 			return (DDI_FAILURE);
3443 	}
3444 	/* Should not be using devmem setup for memory pages */
3445 	ASSERT(!pf_is_memory(dhp->dh_pfn));
3446 
3447 	/* Only some of the flags bits are settable by the driver */
3448 	dhp->dh_flags |= (flags & DEVMAP_SETUP_FLAGS);
3449 	dhp->dh_len = ptob(btopr(len));
3450 
3451 	dhp->dh_cookie = DEVMAP_DEVMEM_COOKIE;
3452 	dhp->dh_roff = ptob(btop(roff));
3453 
3454 	/* setup the dh_mmulevel and DEVMAP_FLAG_LARGE */
3455 	devmap_devmem_large_page_setup(dhp);
3456 	dhp->dh_maxprot = maxprot & dhp->dh_orig_maxprot;
3457 	ASSERT((dhp->dh_prot & dhp->dh_orig_maxprot & maxprot) == dhp->dh_prot);
3458 
3459 
3460 	if (callbackops != NULL) {
3461 		bcopy(callbackops, &dhp->dh_callbackops,
3462 		    sizeof (struct devmap_callback_ctl));
3463 	}
3464 
3465 	/*
3466 	 * Initialize dh_lock if we want to do remap.
3467 	 */
3468 	if (dhp->dh_flags & DEVMAP_ALLOW_REMAP) {
3469 		mutex_init(&dhp->dh_lock, NULL, MUTEX_DEFAULT, NULL);
3470 		dhp->dh_flags |= DEVMAP_LOCK_INITED;
3471 	}
3472 
3473 	dhp->dh_flags |= DEVMAP_SETUP_DONE;
3474 
3475 	return (DDI_SUCCESS);
3476 }
3477 
3478 int
3479 devmap_devmem_remap(devmap_cookie_t dhc, dev_info_t *dip,
3480     uint_t rnumber, offset_t roff, size_t len, uint_t maxprot,
3481     uint_t flags, ddi_device_acc_attr_t *accattrp)
3482 {
3483 	devmap_handle_t *dhp = (devmap_handle_t *)dhc;
3484 	ddi_acc_handle_t handle;
3485 	ddi_map_req_t mr;
3486 	ddi_acc_hdl_t *hp;
3487 	pfn_t	pfn;
3488 	uint_t	hat_flags;
3489 	int	err;
3490 
3491 	TRACE_4(TR_FAC_DEVMAP, TR_DEVMAP_DEVMEM_REMAP,
3492 	    "devmap_devmem_setup:start dhp=%p offset=%llx rnum=%d len=%lx",
3493 	    (void *)dhp, roff, rnumber, (uint_t)len);
3494 	DEBUGF(2, (CE_CONT, "devmap_devmem_remap: dhp %p offset %llx "
3495 	    "rnum %d len %lx\n", (void *)dhp, roff, rnumber, len));
3496 
3497 	/*
3498 	 * Return failure if setup has not been done or no remap permission
3499 	 * has been granted during the setup.
3500 	 */
3501 	if ((dhp->dh_flags & DEVMAP_SETUP_DONE) == 0 ||
3502 	    (dhp->dh_flags & DEVMAP_ALLOW_REMAP) == 0)
3503 		return (DDI_FAILURE);
3504 
3505 	/* Only DEVMAP_MAPPING_INVALID flag supported for remap */
3506 	if ((flags != 0) && (flags != DEVMAP_MAPPING_INVALID))
3507 		return (DDI_FAILURE);
3508 
3509 	if ((dhp->dh_prot & dhp->dh_orig_maxprot & maxprot) != dhp->dh_prot)
3510 		return (DDI_FAILURE);
3511 
3512 	if (!(flags & DEVMAP_MAPPING_INVALID)) {
3513 		handle = impl_acc_hdl_alloc(KM_SLEEP, NULL);
3514 		if (handle == NULL)
3515 			return (DDI_FAILURE);
3516 	}
3517 
3518 	HOLD_DHP_LOCK(dhp);
3519 
3520 	/*
3521 	 * Unload the old mapping, so next fault will setup the new mappings
3522 	 * Do this while holding the dhp lock so other faults dont reestablish
3523 	 * the mappings
3524 	 */
3525 	hat_unload(dhp->dh_seg->s_as->a_hat, dhp->dh_uvaddr,
3526 		dhp->dh_len, HAT_UNLOAD|HAT_UNLOAD_OTHER);
3527 
3528 	if (flags & DEVMAP_MAPPING_INVALID) {
3529 		dhp->dh_flags |= DEVMAP_MAPPING_INVALID;
3530 		dhp->dh_pfn = PFN_INVALID;
3531 	} else {
3532 		/* clear any prior DEVMAP_MAPPING_INVALID flag */
3533 		dhp->dh_flags &= ~DEVMAP_MAPPING_INVALID;
3534 		hp = impl_acc_hdl_get(handle);
3535 		hp->ah_vers = VERS_ACCHDL;
3536 		hp->ah_dip = dip;
3537 		hp->ah_rnumber = rnumber;
3538 		hp->ah_offset = roff;
3539 		hp->ah_len = len;
3540 		if (accattrp != NULL)
3541 			hp->ah_acc = *accattrp;
3542 
3543 		mr.map_op = DDI_MO_MAP_LOCKED;
3544 		mr.map_type = DDI_MT_RNUMBER;
3545 		mr.map_obj.rnumber = rnumber;
3546 		mr.map_prot = maxprot & dhp->dh_orig_maxprot;
3547 		mr.map_flags = DDI_MF_DEVICE_MAPPING;
3548 		mr.map_handlep = hp;
3549 		mr.map_vers = DDI_MAP_VERSION;
3550 
3551 		/*
3552 		 * up the device tree to get pfn.
3553 		 * The rootnex_map_regspec() routine in nexus drivers has been
3554 		 * modified to return pfn if map_flags is DDI_MF_DEVICE_MAPPING.
3555 		 */
3556 		err = ddi_map(dip, &mr, roff, len, (caddr_t *)&pfn);
3557 		hat_flags = hp->ah_hat_flags;
3558 		impl_acc_hdl_free(handle);
3559 		if (err) {
3560 			RELE_DHP_LOCK(dhp);
3561 			return (DDI_FAILURE);
3562 		}
3563 		/*
3564 		 * Store result of ddi_map first in local variables, as we do
3565 		 * not want to overwrite the existing dhp with wrong data.
3566 		 */
3567 		dhp->dh_pfn = pfn;
3568 		dhp->dh_hat_attr = hat_flags;
3569 	}
3570 
3571 	/* clear the large page size flag */
3572 	dhp->dh_flags &= ~DEVMAP_FLAG_LARGE;
3573 
3574 	dhp->dh_cookie = DEVMAP_DEVMEM_COOKIE;
3575 	dhp->dh_roff = ptob(btop(roff));
3576 
3577 	/* setup the dh_mmulevel and DEVMAP_FLAG_LARGE */
3578 	devmap_devmem_large_page_setup(dhp);
3579 	dhp->dh_maxprot = maxprot & dhp->dh_orig_maxprot;
3580 	ASSERT((dhp->dh_prot & dhp->dh_orig_maxprot & maxprot) == dhp->dh_prot);
3581 
3582 	RELE_DHP_LOCK(dhp);
3583 	return (DDI_SUCCESS);
3584 }
3585 
3586 /*
3587  * called by driver devmap routine to pass kernel virtual address  mapping
3588  * info to the framework.    used only for kernel memory
3589  * allocated from ddi_umem_alloc().
3590  */
3591 int
3592 devmap_umem_setup(devmap_cookie_t dhc, dev_info_t *dip,
3593     struct devmap_callback_ctl *callbackops, ddi_umem_cookie_t cookie,
3594     offset_t off, size_t len, uint_t maxprot, uint_t flags,
3595     ddi_device_acc_attr_t *accattrp)
3596 {
3597 	devmap_handle_t *dhp = (devmap_handle_t *)dhc;
3598 	struct ddi_umem_cookie *cp = (struct ddi_umem_cookie *)cookie;
3599 
3600 #ifdef lint
3601 	dip = dip;
3602 #endif
3603 
3604 	TRACE_4(TR_FAC_DEVMAP, TR_DEVMAP_UMEM_SETUP,
3605 	    "devmap_umem_setup:start dhp=%p offset=%llx cookie=%p len=%lx",
3606 	    (void *)dhp, off, cookie, len);
3607 	DEBUGF(2, (CE_CONT, "devmap_umem_setup: dhp %p offset %llx "
3608 	    "cookie %p len %lx\n", (void *)dhp, off, (void *)cookie, len));
3609 
3610 	if (cookie == NULL)
3611 		return (DDI_FAILURE);
3612 
3613 	/* For UMEM_TRASH, this restriction is not needed */
3614 	if ((off + len) > cp->size)
3615 		return (DDI_FAILURE);
3616 
3617 	/* check if the cache attributes are supported */
3618 	if (i_ddi_check_cache_attr(flags) == B_FALSE)
3619 		return (DDI_FAILURE);
3620 
3621 	/*
3622 	 * First to check if this function has been called for this dhp.
3623 	 */
3624 	if (dhp->dh_flags & DEVMAP_SETUP_DONE)
3625 		return (DDI_FAILURE);
3626 
3627 	if ((dhp->dh_prot & dhp->dh_orig_maxprot & maxprot) != dhp->dh_prot)
3628 		return (DDI_FAILURE);
3629 
3630 	if (flags & DEVMAP_MAPPING_INVALID) {
3631 		/*
3632 		 * If DEVMAP_MAPPING_INVALID is specified, we have to grant
3633 		 * remap permission.
3634 		 */
3635 		if (!(flags & DEVMAP_ALLOW_REMAP)) {
3636 			return (DDI_FAILURE);
3637 		}
3638 	} else {
3639 		dhp->dh_cookie = cookie;
3640 		dhp->dh_roff = ptob(btop(off));
3641 		dhp->dh_cvaddr = cp->cvaddr + dhp->dh_roff;
3642 		/* set HAT cache attributes */
3643 		i_ddi_cacheattr_to_hatacc(flags, &dhp->dh_hat_attr);
3644 		/* set HAT endianess attributes */
3645 		i_ddi_devacc_to_hatacc(accattrp, &dhp->dh_hat_attr);
3646 	}
3647 
3648 	/*
3649 	 * The default is _not_ to pass HAT_LOAD_NOCONSIST to hat_devload();
3650 	 * we pass HAT_LOAD_NOCONSIST _only_ in cases where hat tries to
3651 	 * create consistent mappings but our intention was to create
3652 	 * non-consistent mappings.
3653 	 *
3654 	 * DEVMEM: hat figures it out it's DEVMEM and creates non-consistent
3655 	 * mappings.
3656 	 *
3657 	 * kernel exported memory: hat figures it out it's memory and always
3658 	 * creates consistent mappings.
3659 	 *
3660 	 * /dev/mem: non-consistent mappings. See comments in common/io/mem.c
3661 	 *
3662 	 * /dev/kmem: consistent mappings are created unless they are
3663 	 * MAP_FIXED. We _explicitly_ tell hat to create non-consistent
3664 	 * mappings by passing HAT_LOAD_NOCONSIST in case of MAP_FIXED
3665 	 * mappings of /dev/kmem. See common/io/mem.c
3666 	 */
3667 
3668 	/* Only some of the flags bits are settable by the driver */
3669 	dhp->dh_flags |= (flags & DEVMAP_SETUP_FLAGS);
3670 
3671 	dhp->dh_len = ptob(btopr(len));
3672 	dhp->dh_maxprot = maxprot & dhp->dh_orig_maxprot;
3673 	ASSERT((dhp->dh_prot & dhp->dh_orig_maxprot & maxprot) == dhp->dh_prot);
3674 
3675 	if (callbackops != NULL) {
3676 		bcopy(callbackops, &dhp->dh_callbackops,
3677 		    sizeof (struct devmap_callback_ctl));
3678 	}
3679 	/*
3680 	 * Initialize dh_lock if we want to do remap.
3681 	 */
3682 	if (dhp->dh_flags & DEVMAP_ALLOW_REMAP) {
3683 		mutex_init(&dhp->dh_lock, NULL, MUTEX_DEFAULT, NULL);
3684 		dhp->dh_flags |= DEVMAP_LOCK_INITED;
3685 	}
3686 
3687 	dhp->dh_flags |= DEVMAP_SETUP_DONE;
3688 
3689 	return (DDI_SUCCESS);
3690 }
3691 
3692 int
3693 devmap_umem_remap(devmap_cookie_t dhc, dev_info_t *dip,
3694     ddi_umem_cookie_t cookie, offset_t off, size_t len, uint_t maxprot,
3695     uint_t flags, ddi_device_acc_attr_t *accattrp)
3696 {
3697 	devmap_handle_t *dhp = (devmap_handle_t *)dhc;
3698 	struct ddi_umem_cookie *cp = (struct ddi_umem_cookie *)cookie;
3699 
3700 	TRACE_4(TR_FAC_DEVMAP, TR_DEVMAP_UMEM_REMAP,
3701 	    "devmap_umem_remap:start dhp=%p offset=%llx cookie=%p len=%lx",
3702 	    (void *)dhp, off, cookie, len);
3703 	DEBUGF(2, (CE_CONT, "devmap_umem_remap: dhp %p offset %llx "
3704 	    "cookie %p len %lx\n", (void *)dhp, off, (void *)cookie, len));
3705 
3706 #ifdef lint
3707 	dip = dip;
3708 	accattrp = accattrp;
3709 #endif
3710 	/*
3711 	 * Reture failure if setup has not been done or no remap permission
3712 	 * has been granted during the setup.
3713 	 */
3714 	if ((dhp->dh_flags & DEVMAP_SETUP_DONE) == 0 ||
3715 		(dhp->dh_flags & DEVMAP_ALLOW_REMAP) == 0)
3716 		return (DDI_FAILURE);
3717 
3718 	/* No flags supported for remap yet */
3719 	if (flags != 0)
3720 		return (DDI_FAILURE);
3721 
3722 	/* check if the cache attributes are supported */
3723 	if (i_ddi_check_cache_attr(flags) == B_FALSE)
3724 		return (DDI_FAILURE);
3725 
3726 	if ((dhp->dh_prot & dhp->dh_orig_maxprot & maxprot) != dhp->dh_prot)
3727 		return (DDI_FAILURE);
3728 
3729 	/* For UMEM_TRASH, this restriction is not needed */
3730 	if ((off + len) > cp->size)
3731 		return (DDI_FAILURE);
3732 
3733 	HOLD_DHP_LOCK(dhp);
3734 	/*
3735 	 * Unload the old mapping, so next fault will setup the new mappings
3736 	 * Do this while holding the dhp lock so other faults dont reestablish
3737 	 * the mappings
3738 	 */
3739 	hat_unload(dhp->dh_seg->s_as->a_hat, dhp->dh_uvaddr,
3740 		dhp->dh_len, HAT_UNLOAD|HAT_UNLOAD_OTHER);
3741 
3742 	dhp->dh_cookie = cookie;
3743 	dhp->dh_roff = ptob(btop(off));
3744 	dhp->dh_cvaddr = cp->cvaddr + dhp->dh_roff;
3745 	/* set HAT cache attributes */
3746 	i_ddi_cacheattr_to_hatacc(flags, &dhp->dh_hat_attr);
3747 	/* set HAT endianess attributes */
3748 	i_ddi_devacc_to_hatacc(accattrp, &dhp->dh_hat_attr);
3749 
3750 	/* clear the large page size flag */
3751 	dhp->dh_flags &= ~DEVMAP_FLAG_LARGE;
3752 
3753 	dhp->dh_maxprot = maxprot & dhp->dh_orig_maxprot;
3754 	ASSERT((dhp->dh_prot & dhp->dh_orig_maxprot & maxprot) == dhp->dh_prot);
3755 	RELE_DHP_LOCK(dhp);
3756 	return (DDI_SUCCESS);
3757 }
3758 
3759 /*
3760  * to set timeout value for the driver's context management callback, e.g.
3761  * devmap_access().
3762  */
3763 void
3764 devmap_set_ctx_timeout(devmap_cookie_t dhc, clock_t ticks)
3765 {
3766 	devmap_handle_t *dhp = (devmap_handle_t *)dhc;
3767 
3768 	TRACE_2(TR_FAC_DEVMAP, TR_DEVMAP_SET_CTX_TIMEOUT,
3769 	    "devmap_set_ctx_timeout:start dhp=%p ticks=%x",
3770 	    (void *)dhp, ticks);
3771 	dhp->dh_timeout_length = ticks;
3772 }
3773 
3774 int
3775 devmap_default_access(devmap_cookie_t dhp, void *pvtp, offset_t off,
3776     size_t len, uint_t type, uint_t rw)
3777 {
3778 #ifdef lint
3779 	pvtp = pvtp;
3780 #endif
3781 
3782 	TRACE_0(TR_FAC_DEVMAP, TR_DEVMAP_DEFAULT_ACCESS,
3783 	    "devmap_default_access:start");
3784 	return (devmap_load(dhp, off, len, type, rw));
3785 }
3786 
3787 /*
3788  * segkmem_alloc() wrapper to allocate memory which is both
3789  * non-relocatable (for DR) and sharelocked, since the rest
3790  * of this segment driver requires it.
3791  */
3792 static void *
3793 devmap_alloc_pages(vmem_t *vmp, size_t size, int vmflag)
3794 {
3795 	ASSERT(vmp != NULL);
3796 	ASSERT(kvseg.s_base != NULL);
3797 	vmflag |= (VM_NORELOC | SEGKMEM_SHARELOCKED);
3798 	return (segkmem_alloc(vmp, size, vmflag));
3799 }
3800 
3801 /*
3802  * This is where things are a bit incestrous with seg_kmem: unlike
3803  * seg_kp, seg_kmem does not keep its pages long-term sharelocked, so
3804  * we need to do a bit of a dance around that to prevent duplication of
3805  * code until we decide to bite the bullet and implement a new kernel
3806  * segment for driver-allocated memory that is exported to user space.
3807  */
3808 static void
3809 devmap_free_pages(vmem_t *vmp, void *inaddr, size_t size)
3810 {
3811 	page_t *pp;
3812 	caddr_t addr = inaddr;
3813 	caddr_t eaddr;
3814 	pgcnt_t npages = btopr(size);
3815 
3816 	ASSERT(vmp != NULL);
3817 	ASSERT(kvseg.s_base != NULL);
3818 	ASSERT(((uintptr_t)addr & PAGEOFFSET) == 0);
3819 
3820 	hat_unload(kas.a_hat, addr, size, HAT_UNLOAD_UNLOCK);
3821 
3822 	for (eaddr = addr + size; addr < eaddr; addr += PAGESIZE) {
3823 		/*
3824 		 * Use page_find() instead of page_lookup() to find the page
3825 		 * since we know that it is hashed and has a shared lock.
3826 		 */
3827 		pp = page_find(&kvp, (u_offset_t)(uintptr_t)addr);
3828 
3829 		if (pp == NULL)
3830 			panic("devmap_free_pages: page not found");
3831 		if (!page_tryupgrade(pp)) {
3832 			page_unlock(pp);
3833 			pp = page_lookup(&kvp, (u_offset_t)(uintptr_t)addr,
3834 			    SE_EXCL);
3835 			if (pp == NULL)
3836 				panic("devmap_free_pages: page already freed");
3837 		}
3838 		/* Clear p_lckcnt so page_destroy() doesn't update availrmem */
3839 		pp->p_lckcnt = 0;
3840 		page_destroy(pp, 0);
3841 	}
3842 	page_unresv(npages);
3843 
3844 	if (vmp != NULL)
3845 		vmem_free(vmp, inaddr, size);
3846 }
3847 
3848 /*
3849  * devmap_umem_alloc_np() replaces kmem_zalloc() as the method for
3850  * allocating non-pageable kmem in response to a ddi_umem_alloc()
3851  * default request. For now we allocate our own pages and we keep
3852  * them long-term sharelocked, since: A) the fault routines expect the
3853  * memory to already be locked; B) pageable umem is already long-term
3854  * locked; C) it's a lot of work to make it otherwise, particuarly
3855  * since the nexus layer expects the pages to never fault. An RFE is to
3856  * not keep the pages long-term locked, but instead to be able to
3857  * take faults on them and simply look them up in kvp in case we
3858  * fault on them. Even then, we must take care not to let pageout
3859  * steal them from us since the data must remain resident; if we
3860  * do this we must come up with some way to pin the pages to prevent
3861  * faults while a driver is doing DMA to/from them.
3862  */
3863 static void *
3864 devmap_umem_alloc_np(size_t size, size_t flags)
3865 {
3866 	void *buf;
3867 	int vmflags = (flags & DDI_UMEM_NOSLEEP)? VM_NOSLEEP : VM_SLEEP;
3868 
3869 	buf = vmem_alloc(umem_np_arena, size, vmflags);
3870 	if (buf != NULL)
3871 		bzero(buf, size);
3872 	return (buf);
3873 }
3874 
3875 static void
3876 devmap_umem_free_np(void *addr, size_t size)
3877 {
3878 	vmem_free(umem_np_arena, addr, size);
3879 }
3880 
3881 /*
3882  * allocate page aligned kernel memory for exporting to user land.
3883  * The devmap framework will use the cookie allocated by ddi_umem_alloc()
3884  * to find a user virtual address that is in same color as the address
3885  * allocated here.
3886  */
3887 void *
3888 ddi_umem_alloc(size_t size, int flags, ddi_umem_cookie_t *cookie)
3889 {
3890 	register size_t len = ptob(btopr(size));
3891 	void *buf = NULL;
3892 	struct ddi_umem_cookie *cp;
3893 	int iflags = 0;
3894 
3895 	*cookie = NULL;
3896 
3897 	TRACE_0(TR_FAC_DEVMAP, TR_DEVMAP_UMEM_ALLOC,
3898 	    "devmap_umem_alloc:start");
3899 	if (len == 0)
3900 		return ((void *)NULL);
3901 
3902 	/*
3903 	 * allocate cookie
3904 	 */
3905 	if ((cp = kmem_zalloc(sizeof (struct ddi_umem_cookie),
3906 		flags & DDI_UMEM_NOSLEEP ? KM_NOSLEEP : KM_SLEEP)) == NULL) {
3907 		ASSERT(flags & DDI_UMEM_NOSLEEP);
3908 		return ((void *)NULL);
3909 	}
3910 
3911 	if (flags & DDI_UMEM_PAGEABLE) {
3912 		/* Only one of the flags is allowed */
3913 		ASSERT(!(flags & DDI_UMEM_TRASH));
3914 		/* initialize resource with 0 */
3915 		iflags = KPD_ZERO;
3916 
3917 		/*
3918 		 * to allocate unlocked pageable memory, use segkp_get() to
3919 		 * create a segkp segment.  Since segkp can only service kas,
3920 		 * other segment drivers such as segdev have to do
3921 		 * as_fault(segkp, SOFTLOCK) in its fault routine,
3922 		 */
3923 		if (flags & DDI_UMEM_NOSLEEP)
3924 			iflags |= KPD_NOWAIT;
3925 
3926 		if ((buf = segkp_get(segkp, len, iflags)) == NULL) {
3927 			kmem_free(cp, sizeof (struct ddi_umem_cookie));
3928 			return ((void *)NULL);
3929 		}
3930 		cp->type = KMEM_PAGEABLE;
3931 		mutex_init(&cp->lock, NULL, MUTEX_DEFAULT, NULL);
3932 		cp->locked = 0;
3933 	} else if (flags & DDI_UMEM_TRASH) {
3934 		/* Only one of the flags is allowed */
3935 		ASSERT(!(flags & DDI_UMEM_PAGEABLE));
3936 		cp->type = UMEM_TRASH;
3937 		buf = NULL;
3938 	} else {
3939 		if ((buf = devmap_umem_alloc_np(len, flags)) == NULL) {
3940 			kmem_free(cp, sizeof (struct ddi_umem_cookie));
3941 			return ((void *)NULL);
3942 		}
3943 
3944 		cp->type = KMEM_NON_PAGEABLE;
3945 	}
3946 
3947 	/*
3948 	 * need to save size here.  size will be used when
3949 	 * we do kmem_free.
3950 	 */
3951 	cp->size = len;
3952 	cp->cvaddr = (caddr_t)buf;
3953 
3954 	*cookie =  (void *)cp;
3955 	return (buf);
3956 }
3957 
3958 void
3959 ddi_umem_free(ddi_umem_cookie_t cookie)
3960 {
3961 	struct ddi_umem_cookie *cp;
3962 
3963 	TRACE_0(TR_FAC_DEVMAP, TR_DEVMAP_UMEM_FREE,
3964 	    "devmap_umem_free:start");
3965 
3966 	/*
3967 	 * if cookie is NULL, no effects on the system
3968 	 */
3969 	if (cookie == NULL)
3970 		return;
3971 
3972 	cp = (struct ddi_umem_cookie *)cookie;
3973 
3974 	switch (cp->type) {
3975 	case KMEM_PAGEABLE :
3976 		ASSERT(cp->cvaddr != NULL && cp->size != 0);
3977 		/*
3978 		 * Check if there are still any pending faults on the cookie
3979 		 * while the driver is deleting it,
3980 		 * XXX - could change to an ASSERT but wont catch errant drivers
3981 		 */
3982 		mutex_enter(&cp->lock);
3983 		if (cp->locked) {
3984 			mutex_exit(&cp->lock);
3985 			panic("ddi_umem_free for cookie with pending faults %p",
3986 			    (void *)cp);
3987 			return;
3988 		}
3989 
3990 		segkp_release(segkp, cp->cvaddr);
3991 
3992 		/*
3993 		 * release mutex associated with this cookie.
3994 		 */
3995 		mutex_destroy(&cp->lock);
3996 		break;
3997 	case KMEM_NON_PAGEABLE :
3998 		ASSERT(cp->cvaddr != NULL && cp->size != 0);
3999 		devmap_umem_free_np(cp->cvaddr, cp->size);
4000 		break;
4001 	case UMEM_TRASH :
4002 		break;
4003 	case UMEM_LOCKED :
4004 		/* Callers should use ddi_umem_unlock for this type */
4005 		ddi_umem_unlock(cookie);
4006 		/* Frees the cookie too */
4007 		return;
4008 	default:
4009 		/* panic so we can diagnose the underlying cause */
4010 		panic("ddi_umem_free: illegal cookie type 0x%x\n",
4011 		    cp->type);
4012 	}
4013 
4014 	kmem_free(cookie, sizeof (struct ddi_umem_cookie));
4015 }
4016 
4017 
4018 static int
4019 segdev_getmemid(struct seg *seg, caddr_t addr, memid_t *memidp)
4020 {
4021 	struct segdev_data *sdp = (struct segdev_data *)seg->s_data;
4022 
4023 	/*
4024 	 * It looks as if it is always mapped shared
4025 	 */
4026 	TRACE_0(TR_FAC_DEVMAP, TR_DEVMAP_GETMEMID,
4027 	    "segdev_getmemid:start");
4028 	memidp->val[0] = (uintptr_t)VTOCVP(sdp->vp);
4029 	memidp->val[1] = sdp->offset + (uintptr_t)(addr - seg->s_base);
4030 	return (0);
4031 }
4032 
4033 /*ARGSUSED*/
4034 static lgrp_mem_policy_info_t *
4035 segdev_getpolicy(struct seg *seg, caddr_t addr)
4036 {
4037 	return (NULL);
4038 }
4039 
4040 /*ARGSUSED*/
4041 static int
4042 segdev_capable(struct seg *seg, segcapability_t capability)
4043 {
4044 	return (0);
4045 }
4046 
4047 /*
4048  * ddi_umem_alloc() non-pageable quantum cache max size.
4049  * This is just a SWAG.
4050  */
4051 #define	DEVMAP_UMEM_QUANTUM	(8*PAGESIZE)
4052 
4053 /*
4054  * Initialize seg_dev from boot. This routine sets up the trash page
4055  * and creates the umem_np_arena used to back non-pageable memory
4056  * requests.
4057  */
4058 void
4059 segdev_init(void)
4060 {
4061 	struct seg kseg;
4062 
4063 	umem_np_arena = vmem_create("umem_np", NULL, 0, PAGESIZE,
4064 	    devmap_alloc_pages, devmap_free_pages, heap_arena,
4065 	    DEVMAP_UMEM_QUANTUM, VM_SLEEP);
4066 
4067 	kseg.s_as = &kas;
4068 	trashpp = page_create_va(&trashvp, 0, PAGESIZE,
4069 	    PG_NORELOC | PG_EXCL | PG_WAIT, &kseg, NULL);
4070 	if (trashpp == NULL)
4071 		panic("segdev_init: failed to create trash page");
4072 	pagezero(trashpp, 0, PAGESIZE);
4073 	page_downgrade(trashpp);
4074 }
4075 
4076 /*
4077  * Invoke platform-dependent support routines so that /proc can have
4078  * the platform code deal with curious hardware.
4079  */
4080 int
4081 segdev_copyfrom(struct seg *seg,
4082     caddr_t uaddr, const void *devaddr, void *kaddr, size_t len)
4083 {
4084 	struct segdev_data *sdp = (struct segdev_data *)seg->s_data;
4085 	struct snode *sp = VTOS(VTOCVP(sdp->vp));
4086 
4087 	return (e_ddi_copyfromdev(sp->s_dip,
4088 	    (off_t)(uaddr - seg->s_base), devaddr, kaddr, len));
4089 }
4090 
4091 int
4092 segdev_copyto(struct seg *seg,
4093     caddr_t uaddr, const void *kaddr, void *devaddr, size_t len)
4094 {
4095 	struct segdev_data *sdp = (struct segdev_data *)seg->s_data;
4096 	struct snode *sp = VTOS(VTOCVP(sdp->vp));
4097 
4098 	return (e_ddi_copytodev(sp->s_dip,
4099 	    (off_t)(uaddr - seg->s_base), kaddr, devaddr, len));
4100 }
4101