1*7c478bd9Sstevel@tonic-gate /* 2*7c478bd9Sstevel@tonic-gate * CDDL HEADER START 3*7c478bd9Sstevel@tonic-gate * 4*7c478bd9Sstevel@tonic-gate * The contents of this file are subject to the terms of the 5*7c478bd9Sstevel@tonic-gate * Common Development and Distribution License, Version 1.0 only 6*7c478bd9Sstevel@tonic-gate * (the "License"). You may not use this file except in compliance 7*7c478bd9Sstevel@tonic-gate * with the License. 8*7c478bd9Sstevel@tonic-gate * 9*7c478bd9Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 10*7c478bd9Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing. 11*7c478bd9Sstevel@tonic-gate * See the License for the specific language governing permissions 12*7c478bd9Sstevel@tonic-gate * and limitations under the License. 13*7c478bd9Sstevel@tonic-gate * 14*7c478bd9Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each 15*7c478bd9Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 16*7c478bd9Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the 17*7c478bd9Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying 18*7c478bd9Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner] 19*7c478bd9Sstevel@tonic-gate * 20*7c478bd9Sstevel@tonic-gate * CDDL HEADER END 21*7c478bd9Sstevel@tonic-gate */ 22*7c478bd9Sstevel@tonic-gate /* 23*7c478bd9Sstevel@tonic-gate * Copyright 2004 Sun Microsystems, Inc. All rights reserved. 24*7c478bd9Sstevel@tonic-gate * Use is subject to license terms. 25*7c478bd9Sstevel@tonic-gate */ 26*7c478bd9Sstevel@tonic-gate 27*7c478bd9Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI" 28*7c478bd9Sstevel@tonic-gate 29*7c478bd9Sstevel@tonic-gate #include <sys/types.h> 30*7c478bd9Sstevel@tonic-gate #include <sys/param.h> 31*7c478bd9Sstevel@tonic-gate #include <sys/systm.h> 32*7c478bd9Sstevel@tonic-gate #include <sys/kmem.h> 33*7c478bd9Sstevel@tonic-gate #include <sys/cpu.h> 34*7c478bd9Sstevel@tonic-gate #include <sys/sunddi.h> 35*7c478bd9Sstevel@tonic-gate #include <sys/ddi_impldefs.h> 36*7c478bd9Sstevel@tonic-gate #include <sys/pte.h> 37*7c478bd9Sstevel@tonic-gate #include <sys/machsystm.h> 38*7c478bd9Sstevel@tonic-gate #include <sys/mmu.h> 39*7c478bd9Sstevel@tonic-gate #include <sys/dvma.h> 40*7c478bd9Sstevel@tonic-gate #include <sys/debug.h> 41*7c478bd9Sstevel@tonic-gate 42*7c478bd9Sstevel@tonic-gate #define HD ((ddi_dma_impl_t *)h)->dmai_rdip 43*7c478bd9Sstevel@tonic-gate 44*7c478bd9Sstevel@tonic-gate unsigned long 45*7c478bd9Sstevel@tonic-gate dvma_pagesize(dev_info_t *dip) 46*7c478bd9Sstevel@tonic-gate { 47*7c478bd9Sstevel@tonic-gate auto unsigned long dvmapgsz; 48*7c478bd9Sstevel@tonic-gate 49*7c478bd9Sstevel@tonic-gate (void) ddi_ctlops(dip, dip, DDI_CTLOPS_DVMAPAGESIZE, 50*7c478bd9Sstevel@tonic-gate NULL, (void *) &dvmapgsz); 51*7c478bd9Sstevel@tonic-gate return (dvmapgsz); 52*7c478bd9Sstevel@tonic-gate } 53*7c478bd9Sstevel@tonic-gate 54*7c478bd9Sstevel@tonic-gate int 55*7c478bd9Sstevel@tonic-gate dvma_reserve(dev_info_t *dip, ddi_dma_lim_t *limp, uint_t pages, 56*7c478bd9Sstevel@tonic-gate ddi_dma_handle_t *handlep) 57*7c478bd9Sstevel@tonic-gate { 58*7c478bd9Sstevel@tonic-gate auto ddi_dma_lim_t dma_lim; 59*7c478bd9Sstevel@tonic-gate auto ddi_dma_impl_t implhdl; 60*7c478bd9Sstevel@tonic-gate struct ddi_dma_req dmareq; 61*7c478bd9Sstevel@tonic-gate ddi_dma_handle_t reqhdl; 62*7c478bd9Sstevel@tonic-gate ddi_dma_impl_t *mp; 63*7c478bd9Sstevel@tonic-gate int ret; 64*7c478bd9Sstevel@tonic-gate 65*7c478bd9Sstevel@tonic-gate if (limp == (ddi_dma_lim_t *)0) { 66*7c478bd9Sstevel@tonic-gate return (DDI_DMA_BADLIMITS); 67*7c478bd9Sstevel@tonic-gate } else { 68*7c478bd9Sstevel@tonic-gate dma_lim = *limp; 69*7c478bd9Sstevel@tonic-gate } 70*7c478bd9Sstevel@tonic-gate bzero(&dmareq, sizeof (dmareq)); 71*7c478bd9Sstevel@tonic-gate dmareq.dmar_fp = DDI_DMA_DONTWAIT; 72*7c478bd9Sstevel@tonic-gate dmareq.dmar_flags = DDI_DMA_RDWR | DDI_DMA_STREAMING; 73*7c478bd9Sstevel@tonic-gate dmareq.dmar_limits = &dma_lim; 74*7c478bd9Sstevel@tonic-gate dmareq.dmar_object.dmao_size = pages; 75*7c478bd9Sstevel@tonic-gate /* 76*7c478bd9Sstevel@tonic-gate * pass in a dummy handle. This avoids the problem when 77*7c478bd9Sstevel@tonic-gate * somebody is dereferencing the handle before checking 78*7c478bd9Sstevel@tonic-gate * the operation. This can be avoided once we separate 79*7c478bd9Sstevel@tonic-gate * handle allocation and actual operation. 80*7c478bd9Sstevel@tonic-gate */ 81*7c478bd9Sstevel@tonic-gate bzero((caddr_t)&implhdl, sizeof (ddi_dma_impl_t)); 82*7c478bd9Sstevel@tonic-gate reqhdl = (ddi_dma_handle_t)&implhdl; 83*7c478bd9Sstevel@tonic-gate 84*7c478bd9Sstevel@tonic-gate ret = ddi_dma_mctl(dip, dip, reqhdl, DDI_DMA_RESERVE, (off_t *)&dmareq, 85*7c478bd9Sstevel@tonic-gate 0, (caddr_t *)handlep, 0); 86*7c478bd9Sstevel@tonic-gate 87*7c478bd9Sstevel@tonic-gate if (ret == DDI_SUCCESS) { 88*7c478bd9Sstevel@tonic-gate mp = (ddi_dma_impl_t *)(*handlep); 89*7c478bd9Sstevel@tonic-gate if (!(mp->dmai_rflags & DMP_BYPASSNEXUS)) { 90*7c478bd9Sstevel@tonic-gate uint_t np = mp->dmai_ndvmapages; 91*7c478bd9Sstevel@tonic-gate 92*7c478bd9Sstevel@tonic-gate mp->dmai_mapping = (ulong_t)kmem_alloc( 93*7c478bd9Sstevel@tonic-gate sizeof (ddi_dma_lim_t), KM_SLEEP); 94*7c478bd9Sstevel@tonic-gate bcopy((char *)&dma_lim, (char *)mp->dmai_mapping, 95*7c478bd9Sstevel@tonic-gate sizeof (ddi_dma_lim_t)); 96*7c478bd9Sstevel@tonic-gate mp->dmai_minfo = kmem_alloc( 97*7c478bd9Sstevel@tonic-gate np * sizeof (ddi_dma_handle_t), KM_SLEEP); 98*7c478bd9Sstevel@tonic-gate } 99*7c478bd9Sstevel@tonic-gate } 100*7c478bd9Sstevel@tonic-gate return (ret); 101*7c478bd9Sstevel@tonic-gate } 102*7c478bd9Sstevel@tonic-gate 103*7c478bd9Sstevel@tonic-gate void 104*7c478bd9Sstevel@tonic-gate dvma_release(ddi_dma_handle_t h) 105*7c478bd9Sstevel@tonic-gate { 106*7c478bd9Sstevel@tonic-gate ddi_dma_impl_t *mp = (ddi_dma_impl_t *)h; 107*7c478bd9Sstevel@tonic-gate uint_t np = mp->dmai_ndvmapages; 108*7c478bd9Sstevel@tonic-gate 109*7c478bd9Sstevel@tonic-gate if (!(mp->dmai_rflags & DMP_BYPASSNEXUS)) { 110*7c478bd9Sstevel@tonic-gate kmem_free((void *)mp->dmai_mapping, sizeof (ddi_dma_lim_t)); 111*7c478bd9Sstevel@tonic-gate kmem_free(mp->dmai_minfo, np * sizeof (ddi_dma_handle_t)); 112*7c478bd9Sstevel@tonic-gate } 113*7c478bd9Sstevel@tonic-gate (void) ddi_dma_mctl(HD, HD, h, DDI_DMA_RELEASE, 0, 0, 0, 0); 114*7c478bd9Sstevel@tonic-gate 115*7c478bd9Sstevel@tonic-gate } 116*7c478bd9Sstevel@tonic-gate 117*7c478bd9Sstevel@tonic-gate void 118*7c478bd9Sstevel@tonic-gate dvma_kaddr_load(ddi_dma_handle_t h, caddr_t a, uint_t len, uint_t index, 119*7c478bd9Sstevel@tonic-gate ddi_dma_cookie_t *cp) 120*7c478bd9Sstevel@tonic-gate { 121*7c478bd9Sstevel@tonic-gate register ddi_dma_impl_t *mp = (ddi_dma_impl_t *)h; 122*7c478bd9Sstevel@tonic-gate struct fast_dvma *nexus_private; 123*7c478bd9Sstevel@tonic-gate struct dvma_ops *nexus_funcptr; 124*7c478bd9Sstevel@tonic-gate 125*7c478bd9Sstevel@tonic-gate if (mp->dmai_rflags & DMP_BYPASSNEXUS) { 126*7c478bd9Sstevel@tonic-gate nexus_private = (struct fast_dvma *)mp->dmai_nexus_private; 127*7c478bd9Sstevel@tonic-gate nexus_funcptr = (struct dvma_ops *)nexus_private->ops; 128*7c478bd9Sstevel@tonic-gate (void) (*nexus_funcptr->dvma_kaddr_load)(h, a, len, index, cp); 129*7c478bd9Sstevel@tonic-gate } else { 130*7c478bd9Sstevel@tonic-gate ddi_dma_handle_t handle; 131*7c478bd9Sstevel@tonic-gate ddi_dma_lim_t *limp; 132*7c478bd9Sstevel@tonic-gate 133*7c478bd9Sstevel@tonic-gate limp = (ddi_dma_lim_t *)mp->dmai_mapping; 134*7c478bd9Sstevel@tonic-gate (void) ddi_dma_addr_setup(HD, NULL, a, len, DDI_DMA_RDWR, 135*7c478bd9Sstevel@tonic-gate DDI_DMA_SLEEP, NULL, limp, &handle); 136*7c478bd9Sstevel@tonic-gate ((ddi_dma_handle_t *)mp->dmai_minfo)[index] = handle; 137*7c478bd9Sstevel@tonic-gate (void) ddi_dma_htoc(handle, 0, cp); 138*7c478bd9Sstevel@tonic-gate } 139*7c478bd9Sstevel@tonic-gate } 140*7c478bd9Sstevel@tonic-gate 141*7c478bd9Sstevel@tonic-gate /*ARGSUSED*/ 142*7c478bd9Sstevel@tonic-gate void 143*7c478bd9Sstevel@tonic-gate dvma_unload(ddi_dma_handle_t h, uint_t objindex, uint_t type) 144*7c478bd9Sstevel@tonic-gate { 145*7c478bd9Sstevel@tonic-gate register ddi_dma_impl_t *mp = (ddi_dma_impl_t *)h; 146*7c478bd9Sstevel@tonic-gate struct fast_dvma *nexus_private; 147*7c478bd9Sstevel@tonic-gate struct dvma_ops *nexus_funcptr; 148*7c478bd9Sstevel@tonic-gate 149*7c478bd9Sstevel@tonic-gate if (mp->dmai_rflags & DMP_BYPASSNEXUS) { 150*7c478bd9Sstevel@tonic-gate nexus_private = (struct fast_dvma *)mp->dmai_nexus_private; 151*7c478bd9Sstevel@tonic-gate nexus_funcptr = (struct dvma_ops *)nexus_private->ops; 152*7c478bd9Sstevel@tonic-gate (void) (*nexus_funcptr->dvma_unload)(h, objindex, type); 153*7c478bd9Sstevel@tonic-gate } else { 154*7c478bd9Sstevel@tonic-gate ddi_dma_handle_t handle; 155*7c478bd9Sstevel@tonic-gate 156*7c478bd9Sstevel@tonic-gate handle = ((ddi_dma_handle_t *)mp->dmai_minfo)[objindex]; 157*7c478bd9Sstevel@tonic-gate (void) ddi_dma_free(handle); 158*7c478bd9Sstevel@tonic-gate } 159*7c478bd9Sstevel@tonic-gate } 160*7c478bd9Sstevel@tonic-gate 161*7c478bd9Sstevel@tonic-gate void 162*7c478bd9Sstevel@tonic-gate dvma_sync(ddi_dma_handle_t h, uint_t objindex, uint_t type) 163*7c478bd9Sstevel@tonic-gate { 164*7c478bd9Sstevel@tonic-gate register ddi_dma_impl_t *mp = (ddi_dma_impl_t *)h; 165*7c478bd9Sstevel@tonic-gate struct fast_dvma *nexus_private; 166*7c478bd9Sstevel@tonic-gate struct dvma_ops *nexus_funcptr; 167*7c478bd9Sstevel@tonic-gate 168*7c478bd9Sstevel@tonic-gate if (mp->dmai_rflags & DMP_BYPASSNEXUS) { 169*7c478bd9Sstevel@tonic-gate nexus_private = (struct fast_dvma *)mp->dmai_nexus_private; 170*7c478bd9Sstevel@tonic-gate nexus_funcptr = (struct dvma_ops *)nexus_private->ops; 171*7c478bd9Sstevel@tonic-gate (void) (*nexus_funcptr->dvma_sync)(h, objindex, type); 172*7c478bd9Sstevel@tonic-gate } else { 173*7c478bd9Sstevel@tonic-gate ddi_dma_handle_t handle; 174*7c478bd9Sstevel@tonic-gate 175*7c478bd9Sstevel@tonic-gate handle = ((ddi_dma_handle_t *)mp->dmai_minfo)[objindex]; 176*7c478bd9Sstevel@tonic-gate (void) ddi_dma_sync(handle, 0, 0, type); 177*7c478bd9Sstevel@tonic-gate } 178*7c478bd9Sstevel@tonic-gate } 179