xref: /titanic_51/usr/src/uts/common/os/bp_map.c (revision 36945f796054e8cb46d88ec0a84213123cf2b036)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
57a1b360bScth  * Common Development and Distribution License (the "License").
67a1b360bScth  * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate  * and limitations under the License.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * CDDL HEADER END
207c478bd9Sstevel@tonic-gate  */
217c478bd9Sstevel@tonic-gate /*
22*36945f79Smrj  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
237c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
247c478bd9Sstevel@tonic-gate  */
257c478bd9Sstevel@tonic-gate 
267c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
277c478bd9Sstevel@tonic-gate 
287c478bd9Sstevel@tonic-gate #include <sys/types.h>
297c478bd9Sstevel@tonic-gate #include <sys/sysmacros.h>
307c478bd9Sstevel@tonic-gate #include <sys/systm.h>
317c478bd9Sstevel@tonic-gate #include <sys/mman.h>
327c478bd9Sstevel@tonic-gate #include <sys/buf.h>
337c478bd9Sstevel@tonic-gate #include <sys/vmem.h>
347c478bd9Sstevel@tonic-gate #include <sys/cmn_err.h>
357c478bd9Sstevel@tonic-gate #include <sys/debug.h>
367c478bd9Sstevel@tonic-gate #include <sys/machparam.h>
377c478bd9Sstevel@tonic-gate #include <vm/page.h>
387c478bd9Sstevel@tonic-gate #include <vm/seg_kmem.h>
392d3bf78eScth #include <vm/seg_kpm.h>
407c478bd9Sstevel@tonic-gate 
417c478bd9Sstevel@tonic-gate #ifdef __sparc
427c478bd9Sstevel@tonic-gate #include <sys/cpu_module.h>
437c478bd9Sstevel@tonic-gate #define	BP_FLUSH(addr, size)	flush_instr_mem((void *)addr, size);
447c478bd9Sstevel@tonic-gate #else
457c478bd9Sstevel@tonic-gate #define	BP_FLUSH(addr, size)
467c478bd9Sstevel@tonic-gate #endif
477c478bd9Sstevel@tonic-gate 
48*36945f79Smrj int bp_force_copy = 0;
49*36945f79Smrj typedef enum {
50*36945f79Smrj 	BP_COPYIN	= 0,
51*36945f79Smrj 	BP_COPYOUT	= 1
52*36945f79Smrj } bp_copydir_t;
53*36945f79Smrj static int bp_copy_common(bp_copydir_t dir, struct buf *bp, void *driverbuf,
54*36945f79Smrj     offset_t offset, size_t size);
55*36945f79Smrj 
567c478bd9Sstevel@tonic-gate static vmem_t *bp_map_arena;
577c478bd9Sstevel@tonic-gate static size_t bp_align;
587c478bd9Sstevel@tonic-gate static uint_t bp_devload_flags = PROT_READ | PROT_WRITE | HAT_NOSYNC;
597c478bd9Sstevel@tonic-gate int	bp_max_cache = 1 << 17;		/* 128K default; tunable */
602d3bf78eScth int	bp_mapin_kpm_enable = 1;	/* enable default; tunable */
617c478bd9Sstevel@tonic-gate 
627c478bd9Sstevel@tonic-gate static void *
637c478bd9Sstevel@tonic-gate bp_vmem_alloc(vmem_t *vmp, size_t size, int vmflag)
647c478bd9Sstevel@tonic-gate {
657c478bd9Sstevel@tonic-gate 	return (vmem_xalloc(vmp, size, bp_align, 0, 0, NULL, NULL, vmflag));
667c478bd9Sstevel@tonic-gate }
677c478bd9Sstevel@tonic-gate 
687c478bd9Sstevel@tonic-gate void
697c478bd9Sstevel@tonic-gate bp_init(size_t align, uint_t devload_flags)
707c478bd9Sstevel@tonic-gate {
717c478bd9Sstevel@tonic-gate 	bp_align = MAX(align, PAGESIZE);
727c478bd9Sstevel@tonic-gate 	bp_devload_flags |= devload_flags;
737c478bd9Sstevel@tonic-gate 
747c478bd9Sstevel@tonic-gate 	if (bp_align <= bp_max_cache)
757c478bd9Sstevel@tonic-gate 		bp_map_arena = vmem_create("bp_map", NULL, 0, bp_align,
767c478bd9Sstevel@tonic-gate 		    bp_vmem_alloc, vmem_free, heap_arena,
777c478bd9Sstevel@tonic-gate 		    MIN(8 * bp_align, bp_max_cache), VM_SLEEP);
787c478bd9Sstevel@tonic-gate }
797c478bd9Sstevel@tonic-gate 
807c478bd9Sstevel@tonic-gate /*
817c478bd9Sstevel@tonic-gate  * common routine so can be called with/without VM_SLEEP
827c478bd9Sstevel@tonic-gate  */
837c478bd9Sstevel@tonic-gate void *
847c478bd9Sstevel@tonic-gate bp_mapin_common(struct buf *bp, int flag)
857c478bd9Sstevel@tonic-gate {
867c478bd9Sstevel@tonic-gate 	struct as	*as;
877c478bd9Sstevel@tonic-gate 	pfn_t		pfnum;
882d3bf78eScth 	page_t		*pp;
892d3bf78eScth 	page_t		**pplist;
907c478bd9Sstevel@tonic-gate 	caddr_t		kaddr;
912d3bf78eScth 	caddr_t		addr;
922d3bf78eScth 	uintptr_t	off;
932d3bf78eScth 	size_t		size;
942d3bf78eScth 	pgcnt_t		npages;
957c478bd9Sstevel@tonic-gate 	int		color;
967c478bd9Sstevel@tonic-gate 
977c478bd9Sstevel@tonic-gate 	/* return if already mapped in, no pageio/physio, or physio to kas */
987c478bd9Sstevel@tonic-gate 	if ((bp->b_flags & B_REMAPPED) ||
997c478bd9Sstevel@tonic-gate 	    !(bp->b_flags & (B_PAGEIO | B_PHYS)) ||
1007c478bd9Sstevel@tonic-gate 	    (((bp->b_flags & (B_PAGEIO | B_PHYS)) == B_PHYS) &&
1017c478bd9Sstevel@tonic-gate 	    ((bp->b_proc == NULL) || (bp->b_proc->p_as == &kas))))
1027c478bd9Sstevel@tonic-gate 		return (bp->b_un.b_addr);
1037c478bd9Sstevel@tonic-gate 
1047c478bd9Sstevel@tonic-gate 	ASSERT((bp->b_flags & (B_PAGEIO | B_PHYS)) != (B_PAGEIO | B_PHYS));
1057c478bd9Sstevel@tonic-gate 
1062d3bf78eScth 	addr = (caddr_t)bp->b_un.b_addr;
1072d3bf78eScth 	off = (uintptr_t)addr & PAGEOFFSET;
1082d3bf78eScth 	size = P2ROUNDUP(bp->b_bcount + off, PAGESIZE);
1092d3bf78eScth 	npages = btop(size);
1102d3bf78eScth 
1112d3bf78eScth 	/* Fastpath single page IO to locked memory by using kpm. */
1122d3bf78eScth 	if ((bp->b_flags & (B_SHADOW | B_PAGEIO)) && (npages == 1) &&
1132d3bf78eScth 	    kpm_enable && bp_mapin_kpm_enable) {
1142d3bf78eScth 		if (bp->b_flags & B_SHADOW)
1152d3bf78eScth 			pp = *bp->b_shadow;
1162d3bf78eScth 		else
1172d3bf78eScth 			pp = bp->b_pages;
1182d3bf78eScth 		kaddr = hat_kpm_mapin(pp, NULL);
1192d3bf78eScth 		bp->b_un.b_addr = kaddr + off;
1202d3bf78eScth 		bp->b_flags |= B_REMAPPED;
1212d3bf78eScth 		return (bp->b_un.b_addr);
1222d3bf78eScth 	}
1232d3bf78eScth 
1247c478bd9Sstevel@tonic-gate 	/*
1257c478bd9Sstevel@tonic-gate 	 * Allocate kernel virtual space for remapping.
1267c478bd9Sstevel@tonic-gate 	 */
1277c478bd9Sstevel@tonic-gate 	color = bp_color(bp);
1287c478bd9Sstevel@tonic-gate 	ASSERT(color < bp_align);
1297c478bd9Sstevel@tonic-gate 
1307c478bd9Sstevel@tonic-gate 	if (bp_map_arena != NULL) {
1317c478bd9Sstevel@tonic-gate 		kaddr = (caddr_t)vmem_alloc(bp_map_arena,
1327c478bd9Sstevel@tonic-gate 		    P2ROUNDUP(color + size, bp_align), flag);
1337c478bd9Sstevel@tonic-gate 		if (kaddr == NULL)
1347c478bd9Sstevel@tonic-gate 			return (NULL);
1357c478bd9Sstevel@tonic-gate 		kaddr += color;
1367c478bd9Sstevel@tonic-gate 	} else {
1377c478bd9Sstevel@tonic-gate 		kaddr = vmem_xalloc(heap_arena, size, bp_align, color,
1387c478bd9Sstevel@tonic-gate 		    0, NULL, NULL, flag);
1397c478bd9Sstevel@tonic-gate 		if (kaddr == NULL)
1407c478bd9Sstevel@tonic-gate 			return (NULL);
1417c478bd9Sstevel@tonic-gate 	}
1427c478bd9Sstevel@tonic-gate 
1437c478bd9Sstevel@tonic-gate 	ASSERT(P2PHASE((uintptr_t)kaddr, bp_align) == color);
1447c478bd9Sstevel@tonic-gate 
1457c478bd9Sstevel@tonic-gate 	/*
1467c478bd9Sstevel@tonic-gate 	 * Map bp into the virtual space we just allocated.
1477c478bd9Sstevel@tonic-gate 	 */
1487c478bd9Sstevel@tonic-gate 	if (bp->b_flags & B_PAGEIO) {
1497c478bd9Sstevel@tonic-gate 		pp = bp->b_pages;
1502d3bf78eScth 		pplist = NULL;
1517c478bd9Sstevel@tonic-gate 	} else if (bp->b_flags & B_SHADOW) {
1522d3bf78eScth 		pp = NULL;
1537c478bd9Sstevel@tonic-gate 		pplist = bp->b_shadow;
1547c478bd9Sstevel@tonic-gate 	} else {
1552d3bf78eScth 		pp = NULL;
1562d3bf78eScth 		pplist = NULL;
1577c478bd9Sstevel@tonic-gate 		if (bp->b_proc == NULL || (as = bp->b_proc->p_as) == NULL)
1587c478bd9Sstevel@tonic-gate 			as = &kas;
1597c478bd9Sstevel@tonic-gate 	}
1607c478bd9Sstevel@tonic-gate 
1617c478bd9Sstevel@tonic-gate 	bp->b_flags |= B_REMAPPED;
1627c478bd9Sstevel@tonic-gate 	bp->b_un.b_addr = kaddr + off;
1637c478bd9Sstevel@tonic-gate 
1647c478bd9Sstevel@tonic-gate 	while (npages-- != 0) {
1657c478bd9Sstevel@tonic-gate 		if (pp) {
1667c478bd9Sstevel@tonic-gate 			pfnum = pp->p_pagenum;
1677c478bd9Sstevel@tonic-gate 			pp = pp->p_next;
1687c478bd9Sstevel@tonic-gate 		} else if (pplist == NULL) {
1692d3bf78eScth 			pfnum = hat_getpfnum(as->a_hat,
1702d3bf78eScth 			    (caddr_t)((uintptr_t)addr & MMU_PAGEMASK));
1712d3bf78eScth 			if (pfnum == PFN_INVALID)
1727c478bd9Sstevel@tonic-gate 				panic("bp_mapin_common: hat_getpfnum for"
1737c478bd9Sstevel@tonic-gate 				    " addr %p failed\n", (void *)addr);
1747c478bd9Sstevel@tonic-gate 			addr += PAGESIZE;
1757c478bd9Sstevel@tonic-gate 		} else {
1767c478bd9Sstevel@tonic-gate 			pfnum = (*pplist)->p_pagenum;
1777c478bd9Sstevel@tonic-gate 			pplist++;
1787c478bd9Sstevel@tonic-gate 		}
1797c478bd9Sstevel@tonic-gate 
1807c478bd9Sstevel@tonic-gate 		hat_devload(kas.a_hat, kaddr, PAGESIZE, pfnum,
1817c478bd9Sstevel@tonic-gate 		    bp_devload_flags, HAT_LOAD_LOCK);
1827c478bd9Sstevel@tonic-gate 
1837c478bd9Sstevel@tonic-gate 		kaddr += PAGESIZE;
1847c478bd9Sstevel@tonic-gate 	}
1857c478bd9Sstevel@tonic-gate 	return (bp->b_un.b_addr);
1867c478bd9Sstevel@tonic-gate }
1877c478bd9Sstevel@tonic-gate 
1887c478bd9Sstevel@tonic-gate /*
1897c478bd9Sstevel@tonic-gate  * Convert bp for pageio/physio to a kernel addressable location.
1907c478bd9Sstevel@tonic-gate  */
1917c478bd9Sstevel@tonic-gate void
1927c478bd9Sstevel@tonic-gate bp_mapin(struct buf *bp)
1937c478bd9Sstevel@tonic-gate {
1947c478bd9Sstevel@tonic-gate 	(void) bp_mapin_common(bp, VM_SLEEP);
1957c478bd9Sstevel@tonic-gate }
1967c478bd9Sstevel@tonic-gate 
1977c478bd9Sstevel@tonic-gate /*
1987c478bd9Sstevel@tonic-gate  * Release all the resources associated with a previous bp_mapin() call.
1997c478bd9Sstevel@tonic-gate  */
2007c478bd9Sstevel@tonic-gate void
2017c478bd9Sstevel@tonic-gate bp_mapout(struct buf *bp)
2027c478bd9Sstevel@tonic-gate {
2032d3bf78eScth 	caddr_t		addr;
2042d3bf78eScth 	uintptr_t	off;
2052d3bf78eScth 	uintptr_t	base;
2062d3bf78eScth 	uintptr_t	color;
2072d3bf78eScth 	size_t		size;
2082d3bf78eScth 	pgcnt_t		npages;
2092d3bf78eScth 	page_t		*pp;
2102d3bf78eScth 
2112d3bf78eScth 	if ((bp->b_flags & B_REMAPPED) == 0)
2122d3bf78eScth 		return;
2132d3bf78eScth 
2142d3bf78eScth 	addr = bp->b_un.b_addr;
2152d3bf78eScth 	off = (uintptr_t)addr & PAGEOFFSET;
2162d3bf78eScth 	size = P2ROUNDUP(bp->b_bcount + off, PAGESIZE);
2172d3bf78eScth 	npages = btop(size);
2182d3bf78eScth 
2197c478bd9Sstevel@tonic-gate 	bp->b_un.b_addr = (caddr_t)off;		/* debugging aid */
2202d3bf78eScth 
2212d3bf78eScth 	if ((bp->b_flags & (B_SHADOW | B_PAGEIO)) && (npages == 1) &&
2222d3bf78eScth 	    kpm_enable && bp_mapin_kpm_enable) {
2232d3bf78eScth 		if (bp->b_flags & B_SHADOW)
2242d3bf78eScth 			pp = *bp->b_shadow;
2252d3bf78eScth 		else
2262d3bf78eScth 			pp = bp->b_pages;
2277a1b360bScth 		addr = (caddr_t)((uintptr_t)addr & MMU_PAGEMASK);
2282d3bf78eScth 		hat_kpm_mapout(pp, NULL, addr);
2292d3bf78eScth 		bp->b_flags &= ~B_REMAPPED;
2302d3bf78eScth 		return;
2312d3bf78eScth 	}
2322d3bf78eScth 
2332d3bf78eScth 	base = (uintptr_t)addr & MMU_PAGEMASK;
2347c478bd9Sstevel@tonic-gate 	BP_FLUSH(base, size);
2357c478bd9Sstevel@tonic-gate 	hat_unload(kas.a_hat, (void *)base, size,
2367c478bd9Sstevel@tonic-gate 	    HAT_UNLOAD_NOSYNC | HAT_UNLOAD_UNLOCK);
2372d3bf78eScth 	if (bp_map_arena != NULL) {
2382d3bf78eScth 		color = P2PHASE(base, bp_align);
2397c478bd9Sstevel@tonic-gate 		vmem_free(bp_map_arena, (void *)(base - color),
2407c478bd9Sstevel@tonic-gate 		    P2ROUNDUP(color + size, bp_align));
2412d3bf78eScth 	} else
2427c478bd9Sstevel@tonic-gate 		vmem_free(heap_arena, (void *)base, size);
2437c478bd9Sstevel@tonic-gate 	bp->b_flags &= ~B_REMAPPED;
2447c478bd9Sstevel@tonic-gate }
245*36945f79Smrj 
246*36945f79Smrj /*
247*36945f79Smrj  * copy data from a KVA into a buf_t which may not be mapped in. offset
248*36945f79Smrj  * is relative to the buf_t only.
249*36945f79Smrj  */
250*36945f79Smrj int
251*36945f79Smrj bp_copyout(void *driverbuf, struct buf *bp, offset_t offset, size_t size)
252*36945f79Smrj {
253*36945f79Smrj 	return (bp_copy_common(BP_COPYOUT, bp, driverbuf, offset, size));
254*36945f79Smrj }
255*36945f79Smrj 
256*36945f79Smrj /*
257*36945f79Smrj  * copy data from a buf_t which may not be mapped in, into a KVA.. offset
258*36945f79Smrj  * is relative to the buf_t only.
259*36945f79Smrj  */
260*36945f79Smrj int
261*36945f79Smrj bp_copyin(struct buf *bp, void *driverbuf, offset_t offset, size_t size)
262*36945f79Smrj {
263*36945f79Smrj 	return (bp_copy_common(BP_COPYIN, bp, driverbuf, offset, size));
264*36945f79Smrj }
265*36945f79Smrj 
266*36945f79Smrj 
267*36945f79Smrj #define	BP_COPY(dir, driverbuf, baddr, sz)	\
268*36945f79Smrj 	(dir == BP_COPYIN) ? \
269*36945f79Smrj 	bcopy(baddr, driverbuf, sz) :  bcopy(driverbuf, baddr, sz)
270*36945f79Smrj 
271*36945f79Smrj static int
272*36945f79Smrj bp_copy_common(bp_copydir_t dir, struct buf *bp, void *driverbuf,
273*36945f79Smrj     offset_t offset, size_t size)
274*36945f79Smrj {
275*36945f79Smrj 	page_t **pplist;
276*36945f79Smrj 	uintptr_t poff;
277*36945f79Smrj 	uintptr_t voff;
278*36945f79Smrj 	struct as *as;
279*36945f79Smrj 	caddr_t kaddr;
280*36945f79Smrj 	caddr_t addr;
281*36945f79Smrj 	page_t *page;
282*36945f79Smrj 	size_t psize;
283*36945f79Smrj 	page_t *pp;
284*36945f79Smrj 	pfn_t pfn;
285*36945f79Smrj 
286*36945f79Smrj 
287*36945f79Smrj 	ASSERT((offset + size) <= bp->b_bcount);
288*36945f79Smrj 
289*36945f79Smrj 	/* if the buf_t already has a KVA, just do a bcopy */
290*36945f79Smrj 	if (!(bp->b_flags & (B_PHYS | B_PAGEIO))) {
291*36945f79Smrj 		BP_COPY(dir, driverbuf, bp->b_un.b_addr + offset, size);
292*36945f79Smrj 		return (0);
293*36945f79Smrj 	}
294*36945f79Smrj 
295*36945f79Smrj 	/* if we don't have kpm enabled, we need to do the slow path */
296*36945f79Smrj 	if (!kpm_enable || bp_force_copy) {
297*36945f79Smrj 		bp_mapin(bp);
298*36945f79Smrj 		BP_COPY(dir, driverbuf, bp->b_un.b_addr + offset, size);
299*36945f79Smrj 		bp_mapout(bp);
300*36945f79Smrj 		return (0);
301*36945f79Smrj 	}
302*36945f79Smrj 
303*36945f79Smrj 	/*
304*36945f79Smrj 	 * kpm is enabled, and we need to map in the buf_t for the copy
305*36945f79Smrj 	 */
306*36945f79Smrj 
307*36945f79Smrj 	/* setup pp, plist, and make sure 'as' is right */
308*36945f79Smrj 	if (bp->b_flags & B_PAGEIO) {
309*36945f79Smrj 		pp = bp->b_pages;
310*36945f79Smrj 		pplist = NULL;
311*36945f79Smrj 	} else if (bp->b_flags & B_SHADOW) {
312*36945f79Smrj 		pp = NULL;
313*36945f79Smrj 		pplist = bp->b_shadow;
314*36945f79Smrj 	} else {
315*36945f79Smrj 		pp = NULL;
316*36945f79Smrj 		pplist = NULL;
317*36945f79Smrj 		if (bp->b_proc == NULL || (as = bp->b_proc->p_as) == NULL) {
318*36945f79Smrj 			as = &kas;
319*36945f79Smrj 		}
320*36945f79Smrj 	}
321*36945f79Smrj 
322*36945f79Smrj 	/*
323*36945f79Smrj 	 * locals for the address, the offset into the first page, and the
324*36945f79Smrj 	 * size of the first page we are going to copy.
325*36945f79Smrj 	 */
326*36945f79Smrj 	addr = (caddr_t)bp->b_un.b_addr;
327*36945f79Smrj 	poff = (uintptr_t)addr & PAGEOFFSET;
328*36945f79Smrj 	psize = MIN(PAGESIZE - poff, size);
329*36945f79Smrj 
330*36945f79Smrj 	/*
331*36945f79Smrj 	 * we always start with a 0 offset into the driverbuf provided. The
332*36945f79Smrj 	 * offset passed in only applies to the buf_t.
333*36945f79Smrj 	 */
334*36945f79Smrj 	voff = 0;
335*36945f79Smrj 
336*36945f79Smrj 	/* Loop until we've copied al the data */
337*36945f79Smrj 	while (size > 0) {
338*36945f79Smrj 
339*36945f79Smrj 		/*
340*36945f79Smrj 		 * for a pp or pplist, get the pfn, then go to the next page_t
341*36945f79Smrj 		 * for the next time around the loop.
342*36945f79Smrj 		 */
343*36945f79Smrj 		if (pp) {
344*36945f79Smrj 			page = pp;
345*36945f79Smrj 			pp = pp->p_next;
346*36945f79Smrj 		} else if (pplist != NULL) {
347*36945f79Smrj 			page = (*pplist);
348*36945f79Smrj 			pplist++;
349*36945f79Smrj 
350*36945f79Smrj 		/*
351*36945f79Smrj 		 * We have a user VA. If we are going to copy this page, (e.g.
352*36945f79Smrj 		 * the offset into the buf_t where we start to copy is
353*36945f79Smrj 		 * within this page), get the pfn. Don't waste the cycles
354*36945f79Smrj 		 * getting the pfn if we're not copying this page.
355*36945f79Smrj 		 */
356*36945f79Smrj 		} else if (offset < psize) {
357*36945f79Smrj 			pfn = hat_getpfnum(as->a_hat,
358*36945f79Smrj 			    (caddr_t)((uintptr_t)addr & PAGEMASK));
359*36945f79Smrj 			if (pfn == PFN_INVALID) {
360*36945f79Smrj 				return (-1);
361*36945f79Smrj 			}
362*36945f79Smrj 			page = page_numtopp_nolock(pfn);
363*36945f79Smrj 			addr += psize - offset;
364*36945f79Smrj 		} else {
365*36945f79Smrj 			addr += psize;
366*36945f79Smrj 		}
367*36945f79Smrj 
368*36945f79Smrj 		/*
369*36945f79Smrj 		 * if we have an initial offset into the buf_t passed in,
370*36945f79Smrj 		 * and it falls within the current page, account for it in
371*36945f79Smrj 		 * the page size (how much we will copy) and the offset into the
372*36945f79Smrj 		 * page (where we'll start copying from).
373*36945f79Smrj 		 */
374*36945f79Smrj 		if ((offset > 0) && (offset < psize)) {
375*36945f79Smrj 			psize -= offset;
376*36945f79Smrj 			poff += offset;
377*36945f79Smrj 			offset = 0;
378*36945f79Smrj 
379*36945f79Smrj 		/*
380*36945f79Smrj 		 * if we have an initial offset into the buf_t passed in,
381*36945f79Smrj 		 * and it's not within the current page, skip this page.
382*36945f79Smrj 		 * We don't have to worry about the first page offset and size
383*36945f79Smrj 		 * anymore. psize will normally be PAGESIZE now unless we are
384*36945f79Smrj 		 * on the last page.
385*36945f79Smrj 		 */
386*36945f79Smrj 		} else if (offset >= psize) {
387*36945f79Smrj 			offset -= psize;
388*36945f79Smrj 			psize = MIN(PAGESIZE, size);
389*36945f79Smrj 			poff = 0;
390*36945f79Smrj 			continue;
391*36945f79Smrj 		}
392*36945f79Smrj 
393*36945f79Smrj 		/*
394*36945f79Smrj 		 * get a kpm mapping to the page, them copy in/out of the
395*36945f79Smrj 		 * page. update size left and offset into the driverbuf passed
396*36945f79Smrj 		 * in for the next time around the loop.
397*36945f79Smrj 		 */
398*36945f79Smrj 		kaddr = hat_kpm_mapin(page, NULL) + poff;
399*36945f79Smrj 		BP_COPY(dir, (void *)((uintptr_t)driverbuf + voff), kaddr,
400*36945f79Smrj 		    psize);
401*36945f79Smrj 		hat_kpm_mapout(page, NULL, kaddr - poff);
402*36945f79Smrj 
403*36945f79Smrj 		size -= psize;
404*36945f79Smrj 		voff += psize;
405*36945f79Smrj 
406*36945f79Smrj 		poff = 0;
407*36945f79Smrj 		psize = MIN(PAGESIZE, size);
408*36945f79Smrj 	}
409*36945f79Smrj 
410*36945f79Smrj 	return (0);
411*36945f79Smrj }
412