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