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
51501Sgovinda * Common Development and Distribution License (the "License").
61501Sgovinda * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate *
80Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate * See the License for the specific language governing permissions
110Sstevel@tonic-gate * and limitations under the License.
120Sstevel@tonic-gate *
130Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate *
190Sstevel@tonic-gate * CDDL HEADER END
200Sstevel@tonic-gate */
210Sstevel@tonic-gate /*
22*12112Sbhaskar.sarkar@sun.com * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
230Sstevel@tonic-gate */
240Sstevel@tonic-gate
250Sstevel@tonic-gate /*
260Sstevel@tonic-gate * Internal PCI Fast DVMA implementation
270Sstevel@tonic-gate */
280Sstevel@tonic-gate #include <sys/types.h>
290Sstevel@tonic-gate #include <sys/kmem.h>
300Sstevel@tonic-gate #include <sys/async.h>
310Sstevel@tonic-gate #include <sys/sysmacros.h>
320Sstevel@tonic-gate #include <sys/sunddi.h>
330Sstevel@tonic-gate #include <sys/ddi_impldefs.h>
340Sstevel@tonic-gate #include <sys/dvma.h>
350Sstevel@tonic-gate #include "px_obj.h"
360Sstevel@tonic-gate
370Sstevel@tonic-gate /*LINTLIBRARY*/
380Sstevel@tonic-gate
390Sstevel@tonic-gate static struct dvma_ops fdvma_ops;
400Sstevel@tonic-gate typedef struct fast_dvma fdvma_t;
410Sstevel@tonic-gate
420Sstevel@tonic-gate /*
430Sstevel@tonic-gate * The following routines are used to implement the sun4u fast dvma
440Sstevel@tonic-gate * routines on this bus.
450Sstevel@tonic-gate */
460Sstevel@tonic-gate
470Sstevel@tonic-gate /*ARGSUSED*/
480Sstevel@tonic-gate static void
px_fdvma_load(ddi_dma_handle_t h,caddr_t a,uint_t len,uint_t index,ddi_dma_cookie_t * cp)490Sstevel@tonic-gate px_fdvma_load(ddi_dma_handle_t h, caddr_t a, uint_t len, uint_t index,
500Sstevel@tonic-gate ddi_dma_cookie_t *cp)
510Sstevel@tonic-gate {
520Sstevel@tonic-gate ddi_dma_impl_t *mp = (ddi_dma_impl_t *)h;
530Sstevel@tonic-gate fdvma_t *fdvma_p = (fdvma_t *)mp->dmai_fdvma;
540Sstevel@tonic-gate px_t *px_p = (px_t *)fdvma_p->softsp;
550Sstevel@tonic-gate px_mmu_t *mmu_p = px_p->px_mmu_p;
560Sstevel@tonic-gate dev_info_t *dip = px_p->px_dip;
570Sstevel@tonic-gate px_dvma_addr_t dvma_addr, dvma_pg;
580Sstevel@tonic-gate uint32_t offset;
590Sstevel@tonic-gate size_t npages, pg_index;
609707SDaniel.Ice@Sun.COM io_attributes_t attr;
610Sstevel@tonic-gate
62671Skrishnae offset = (uint32_t)(uintptr_t)a & MMU_PAGE_OFFSET;
630Sstevel@tonic-gate npages = MMU_BTOPR(len + offset);
640Sstevel@tonic-gate if (!npages)
650Sstevel@tonic-gate return;
660Sstevel@tonic-gate
670Sstevel@tonic-gate /* make sure we don't exceed reserved boundary */
680Sstevel@tonic-gate DBG(DBG_FAST_DVMA, dip, "load index=%x: %p+%x ", index, a, len);
690Sstevel@tonic-gate if (index + npages > mp->dmai_ndvmapages) {
700Sstevel@tonic-gate cmn_err(px_panic_on_fatal_errors ? CE_PANIC : CE_WARN,
718413SDaniel.Ice@Sun.COM "%s%d: kaddr_load index(%x)+pgs(%lx) exceeds limit\n",
728413SDaniel.Ice@Sun.COM ddi_driver_name(dip), ddi_get_instance(dip),
738413SDaniel.Ice@Sun.COM index, npages);
740Sstevel@tonic-gate return;
750Sstevel@tonic-gate }
760Sstevel@tonic-gate fdvma_p->pagecnt[index] = npages;
770Sstevel@tonic-gate
780Sstevel@tonic-gate dvma_addr = mp->dmai_mapping + MMU_PTOB(index);
790Sstevel@tonic-gate dvma_pg = MMU_BTOP(dvma_addr);
800Sstevel@tonic-gate pg_index = dvma_pg - mmu_p->dvma_base_pg;
810Sstevel@tonic-gate
820Sstevel@tonic-gate /* construct the dma cookie to be returned */
830Sstevel@tonic-gate MAKE_DMA_COOKIE(cp, dvma_addr | offset, len);
840Sstevel@tonic-gate DBG(DBG_FAST_DVMA | DBG_CONT, dip, "cookie: %x+%x\n",
858413SDaniel.Ice@Sun.COM cp->dmac_address, cp->dmac_size);
860Sstevel@tonic-gate
871772Sjl139090 attr = PX_GET_TTE_ATTR(mp->dmai_rflags, mp->dmai_attr.dma_attr_flags);
880Sstevel@tonic-gate
893156Sgirish if (px_lib_iommu_map(dip, PCI_TSBID(0, pg_index), npages,
903156Sgirish PX_ADD_ATTR_EXTNS(attr, mp->dmai_bdf), (void *)a, 0,
913156Sgirish MMU_MAP_BUF) != DDI_SUCCESS) {
920Sstevel@tonic-gate cmn_err(CE_WARN, "%s%d: kaddr_load can't get "
93671Skrishnae "page frame for vaddr %lx", ddi_driver_name(dip),
94671Skrishnae ddi_get_instance(dip), (uintptr_t)a);
950Sstevel@tonic-gate }
960Sstevel@tonic-gate }
970Sstevel@tonic-gate
980Sstevel@tonic-gate /*ARGSUSED*/
990Sstevel@tonic-gate static void
px_fdvma_unload(ddi_dma_handle_t h,uint_t index,uint_t sync_flag)1000Sstevel@tonic-gate px_fdvma_unload(ddi_dma_handle_t h, uint_t index, uint_t sync_flag)
1010Sstevel@tonic-gate {
1020Sstevel@tonic-gate ddi_dma_impl_t *mp = (ddi_dma_impl_t *)h;
1030Sstevel@tonic-gate fdvma_t *fdvma_p = (fdvma_t *)mp->dmai_fdvma;
1040Sstevel@tonic-gate px_t *px_p = (px_t *)fdvma_p->softsp;
1050Sstevel@tonic-gate size_t npages = fdvma_p->pagecnt[index];
1060Sstevel@tonic-gate px_dvma_addr_t dvma_pg = MMU_BTOP(mp->dmai_mapping + MMU_PTOB(index));
1070Sstevel@tonic-gate
1080Sstevel@tonic-gate DBG(DBG_FAST_DVMA, px_p->px_dip,
1098413SDaniel.Ice@Sun.COM "unload index=%x sync_flag=%x %x+%x+%x\n", index, sync_flag,
1108413SDaniel.Ice@Sun.COM mp->dmai_mapping, MMU_PTOB(index), MMU_PTOB(npages));
1110Sstevel@tonic-gate
1121501Sgovinda px_mmu_unmap_pages(px_p->px_mmu_p, mp, dvma_pg, npages);
1130Sstevel@tonic-gate fdvma_p->pagecnt[index] = 0;
1140Sstevel@tonic-gate }
1150Sstevel@tonic-gate
1160Sstevel@tonic-gate /*ARGSUSED*/
1170Sstevel@tonic-gate static void
px_fdvma_sync(ddi_dma_handle_t h,uint_t index,uint_t sync_flag)1180Sstevel@tonic-gate px_fdvma_sync(ddi_dma_handle_t h, uint_t index, uint_t sync_flag)
1190Sstevel@tonic-gate {
1200Sstevel@tonic-gate ddi_dma_impl_t *mp = (ddi_dma_impl_t *)h;
1210Sstevel@tonic-gate fdvma_t *fdvma_p = (fdvma_t *)mp->dmai_fdvma;
1220Sstevel@tonic-gate px_t *px_p = (px_t *)fdvma_p->softsp;
1230Sstevel@tonic-gate size_t npg = fdvma_p->pagecnt[index];
1240Sstevel@tonic-gate
1250Sstevel@tonic-gate DBG(DBG_FAST_DVMA, px_p->px_dip,
1268413SDaniel.Ice@Sun.COM "sync index=%x sync_flag=%x %x+%x+%x\n", index, sync_flag,
1278413SDaniel.Ice@Sun.COM mp->dmai_mapping, MMU_PTOB(index), MMU_PTOB(npg));
1280Sstevel@tonic-gate }
1290Sstevel@tonic-gate
1300Sstevel@tonic-gate int
px_fdvma_reserve(dev_info_t * dip,dev_info_t * rdip,px_t * px_p,ddi_dma_req_t * dmareq,ddi_dma_handle_t * handlep)1310Sstevel@tonic-gate px_fdvma_reserve(dev_info_t *dip, dev_info_t *rdip, px_t *px_p,
1320Sstevel@tonic-gate ddi_dma_req_t *dmareq, ddi_dma_handle_t *handlep)
1330Sstevel@tonic-gate {
1340Sstevel@tonic-gate fdvma_t *fdvma_p;
1350Sstevel@tonic-gate px_dvma_addr_t dvma_pg;
1360Sstevel@tonic-gate px_mmu_t *mmu_p = px_p->px_mmu_p;
1370Sstevel@tonic-gate size_t npages;
1380Sstevel@tonic-gate ddi_dma_impl_t *mp;
1390Sstevel@tonic-gate ddi_dma_lim_t *lim_p = dmareq->dmar_limits;
1400Sstevel@tonic-gate ulong_t hi = lim_p->dlim_addr_hi;
1410Sstevel@tonic-gate ulong_t lo = lim_p->dlim_addr_lo;
1420Sstevel@tonic-gate size_t counter_max = (lim_p->dlim_cntr_max + 1) & MMU_PAGE_MASK;
1430Sstevel@tonic-gate
1440Sstevel@tonic-gate if (px_disable_fdvma)
1450Sstevel@tonic-gate return (DDI_FAILURE);
1460Sstevel@tonic-gate
1470Sstevel@tonic-gate DBG(DBG_DMA_CTL, dip, "DDI_DMA_RESERVE: rdip=%s%d\n",
1488413SDaniel.Ice@Sun.COM ddi_driver_name(rdip), ddi_get_instance(rdip));
1490Sstevel@tonic-gate
1500Sstevel@tonic-gate /*
1510Sstevel@tonic-gate * Check the limit structure.
1520Sstevel@tonic-gate */
1530Sstevel@tonic-gate if ((lo >= hi) || (hi < mmu_p->mmu_dvma_base))
1540Sstevel@tonic-gate return (DDI_DMA_BADLIMITS);
1550Sstevel@tonic-gate
1560Sstevel@tonic-gate /*
157*12112Sbhaskar.sarkar@sun.com * Allocate DVMA space from reserve.
1580Sstevel@tonic-gate */
1590Sstevel@tonic-gate npages = dmareq->dmar_object.dmao_size;
160*12112Sbhaskar.sarkar@sun.com if ((long)atomic_add_long_nv(&mmu_p->mmu_dvma_reserve, -npages) < 0) {
161*12112Sbhaskar.sarkar@sun.com atomic_add_long(&mmu_p->mmu_dvma_reserve, npages);
1620Sstevel@tonic-gate return (DDI_DMA_NORESOURCES);
163*12112Sbhaskar.sarkar@sun.com }
1640Sstevel@tonic-gate
1650Sstevel@tonic-gate /*
1660Sstevel@tonic-gate * Allocate the dma handle.
1670Sstevel@tonic-gate */
1680Sstevel@tonic-gate mp = kmem_zalloc(sizeof (px_dma_hdl_t), KM_SLEEP);
1690Sstevel@tonic-gate
1700Sstevel@tonic-gate /*
1710Sstevel@tonic-gate * Get entries from dvma space map.
1720Sstevel@tonic-gate * (vmem_t *vmp,
1730Sstevel@tonic-gate * size_t size, size_t align, size_t phase,
1740Sstevel@tonic-gate * size_t nocross, void *minaddr, void *maxaddr, int vmflag)
1750Sstevel@tonic-gate */
1760Sstevel@tonic-gate dvma_pg = MMU_BTOP((ulong_t)vmem_xalloc(mmu_p->mmu_dvma_map,
1778413SDaniel.Ice@Sun.COM MMU_PTOB(npages), MMU_PAGE_SIZE, 0,
1788413SDaniel.Ice@Sun.COM counter_max, (void *)lo, (void *)(hi + 1),
1798413SDaniel.Ice@Sun.COM dmareq->dmar_fp == DDI_DMA_SLEEP ? VM_SLEEP : VM_NOSLEEP));
1800Sstevel@tonic-gate if (dvma_pg == 0) {
181*12112Sbhaskar.sarkar@sun.com atomic_add_long(&mmu_p->mmu_dvma_reserve, npages);
1820Sstevel@tonic-gate kmem_free(mp, sizeof (px_dma_hdl_t));
1830Sstevel@tonic-gate return (DDI_DMA_NOMAPPING);
1840Sstevel@tonic-gate }
1850Sstevel@tonic-gate
1860Sstevel@tonic-gate /*
1870Sstevel@tonic-gate * Create the fast dvma request structure.
1880Sstevel@tonic-gate */
1890Sstevel@tonic-gate fdvma_p = kmem_alloc(sizeof (fdvma_t), KM_SLEEP);
1900Sstevel@tonic-gate fdvma_p->pagecnt = kmem_alloc(npages * sizeof (uint_t), KM_SLEEP);
1910Sstevel@tonic-gate fdvma_p->ops = &fdvma_ops;
1920Sstevel@tonic-gate fdvma_p->softsp = (caddr_t)px_p;
1930Sstevel@tonic-gate fdvma_p->sync_flag = NULL;
1940Sstevel@tonic-gate
1950Sstevel@tonic-gate /*
1960Sstevel@tonic-gate * Initialize the handle.
1970Sstevel@tonic-gate */
1980Sstevel@tonic-gate mp->dmai_rdip = rdip;
1990Sstevel@tonic-gate mp->dmai_rflags = DMP_BYPASSNEXUS | DDI_DMA_READ | DMP_NOSYNC;
2000Sstevel@tonic-gate mp->dmai_burstsizes = dmareq->dmar_limits->dlim_burstsizes;
2010Sstevel@tonic-gate mp->dmai_mapping = MMU_PTOB(dvma_pg);
2020Sstevel@tonic-gate mp->dmai_ndvmapages = npages;
2030Sstevel@tonic-gate mp->dmai_size = npages * MMU_PAGE_SIZE;
2040Sstevel@tonic-gate mp->dmai_nwin = 0;
2050Sstevel@tonic-gate mp->dmai_fdvma = (caddr_t)fdvma_p;
2063956Sgovinda
2073956Sgovinda /*
2088413SDaniel.Ice@Sun.COM * The bdf protection value is set to immediate child
2098413SDaniel.Ice@Sun.COM * at first. It gets modified by switch/bridge drivers
2108413SDaniel.Ice@Sun.COM * as the code traverses down the fabric topology.
2118413SDaniel.Ice@Sun.COM *
2128413SDaniel.Ice@Sun.COM * XXX No IOMMU protection for broken devices.
2133956Sgovinda */
2148413SDaniel.Ice@Sun.COM ASSERT((intptr_t)ddi_get_parent_data(rdip) >> 1 == 0);
2159921SKrishna.Elango@Sun.COM mp->dmai_bdf = ((intptr_t)ddi_get_parent_data(rdip) == 1) ?
2169921SKrishna.Elango@Sun.COM PCIE_INVALID_BDF : pcie_get_bdf_for_dma_xfer(dip, rdip);
2173956Sgovinda
2180Sstevel@tonic-gate DBG(DBG_DMA_CTL, dip,
2198413SDaniel.Ice@Sun.COM "DDI_DMA_RESERVE: mp=%p dvma=%x npages=%x private=%p\n",
2208413SDaniel.Ice@Sun.COM mp, mp->dmai_mapping, npages, fdvma_p);
2210Sstevel@tonic-gate *handlep = (ddi_dma_handle_t)mp;
2220Sstevel@tonic-gate return (DDI_SUCCESS);
2230Sstevel@tonic-gate }
2240Sstevel@tonic-gate
2250Sstevel@tonic-gate int
px_fdvma_release(dev_info_t * dip,px_t * px_p,ddi_dma_impl_t * mp)2260Sstevel@tonic-gate px_fdvma_release(dev_info_t *dip, px_t *px_p, ddi_dma_impl_t *mp)
2270Sstevel@tonic-gate {
2280Sstevel@tonic-gate px_mmu_t *mmu_p = px_p->px_mmu_p;
2290Sstevel@tonic-gate size_t npages;
2300Sstevel@tonic-gate fdvma_t *fdvma_p = (fdvma_t *)mp->dmai_fdvma;
2310Sstevel@tonic-gate
2320Sstevel@tonic-gate if (px_disable_fdvma)
2330Sstevel@tonic-gate return (DDI_FAILURE);
2340Sstevel@tonic-gate
2350Sstevel@tonic-gate /* validate fdvma handle */
2360Sstevel@tonic-gate if (!(mp->dmai_rflags & DMP_BYPASSNEXUS)) {
2370Sstevel@tonic-gate DBG(DBG_DMA_CTL, dip, "DDI_DMA_RELEASE: not fast dma\n");
2380Sstevel@tonic-gate return (DDI_FAILURE);
2390Sstevel@tonic-gate }
2400Sstevel@tonic-gate
2410Sstevel@tonic-gate /* flush all reserved dvma addresses from mmu */
2420Sstevel@tonic-gate px_mmu_unmap_window(mmu_p, mp);
2430Sstevel@tonic-gate
2440Sstevel@tonic-gate npages = mp->dmai_ndvmapages;
2450Sstevel@tonic-gate vmem_xfree(mmu_p->mmu_dvma_map, (void *)mp->dmai_mapping,
2468413SDaniel.Ice@Sun.COM MMU_PTOB(npages));
2470Sstevel@tonic-gate
248*12112Sbhaskar.sarkar@sun.com atomic_add_long(&mmu_p->mmu_dvma_reserve, npages);
2490Sstevel@tonic-gate mp->dmai_ndvmapages = 0;
2500Sstevel@tonic-gate
2510Sstevel@tonic-gate /* see if there is anyone waiting for dvma space */
2520Sstevel@tonic-gate if (mmu_p->mmu_dvma_clid != 0) {
2530Sstevel@tonic-gate DBG(DBG_DMA_CTL, dip, "run dvma callback\n");
2540Sstevel@tonic-gate ddi_run_callback(&mmu_p->mmu_dvma_clid);
2550Sstevel@tonic-gate }
2560Sstevel@tonic-gate
2570Sstevel@tonic-gate /* free data structures */
2580Sstevel@tonic-gate kmem_free(fdvma_p->pagecnt, npages * sizeof (uint_t));
2590Sstevel@tonic-gate kmem_free(fdvma_p, sizeof (fdvma_t));
2600Sstevel@tonic-gate kmem_free(mp, sizeof (px_dma_hdl_t));
2610Sstevel@tonic-gate
2620Sstevel@tonic-gate /* see if there is anyone waiting for kmem */
2630Sstevel@tonic-gate if (px_kmem_clid != 0) {
2640Sstevel@tonic-gate DBG(DBG_DMA_CTL, dip, "run handle callback\n");
2650Sstevel@tonic-gate ddi_run_callback(&px_kmem_clid);
2660Sstevel@tonic-gate }
2670Sstevel@tonic-gate return (DDI_SUCCESS);
2680Sstevel@tonic-gate }
2690Sstevel@tonic-gate
2700Sstevel@tonic-gate static struct dvma_ops fdvma_ops = {
2710Sstevel@tonic-gate DVMAO_REV,
2720Sstevel@tonic-gate px_fdvma_load,
2730Sstevel@tonic-gate px_fdvma_unload,
2740Sstevel@tonic-gate px_fdvma_sync
2750Sstevel@tonic-gate };
276