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 5*2251Selowe * Common Development and Distribution License (the "License"). 6*2251Selowe * 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*2251Selowe * Copyright 2006 Sun Microsystems, Inc. All rights reserved. 230Sstevel@tonic-gate * Use is subject to license terms. 240Sstevel@tonic-gate */ 250Sstevel@tonic-gate 260Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI" 270Sstevel@tonic-gate 280Sstevel@tonic-gate /* 290Sstevel@tonic-gate * Internal PCI Fast DVMA implementation 300Sstevel@tonic-gate */ 310Sstevel@tonic-gate #include <sys/types.h> 320Sstevel@tonic-gate #include <sys/kmem.h> 330Sstevel@tonic-gate #include <sys/async.h> 340Sstevel@tonic-gate #include <sys/sysmacros.h> 350Sstevel@tonic-gate #include <sys/sunddi.h> 360Sstevel@tonic-gate #include <sys/ddi_impldefs.h> 370Sstevel@tonic-gate #include <sys/dvma.h> 380Sstevel@tonic-gate #include <vm/hat.h> 390Sstevel@tonic-gate #include <sys/pci/pci_obj.h> 400Sstevel@tonic-gate 410Sstevel@tonic-gate /*LINTLIBRARY*/ 420Sstevel@tonic-gate 430Sstevel@tonic-gate static struct dvma_ops fdvma_ops; 440Sstevel@tonic-gate 450Sstevel@tonic-gate /* 460Sstevel@tonic-gate * The following routines are used to implement the sun4u fast dvma 470Sstevel@tonic-gate * routines on this bus. 480Sstevel@tonic-gate */ 490Sstevel@tonic-gate 500Sstevel@tonic-gate /*ARGSUSED*/ 510Sstevel@tonic-gate static void 520Sstevel@tonic-gate pci_fdvma_load(ddi_dma_handle_t h, caddr_t a, uint_t len, uint_t index, 530Sstevel@tonic-gate ddi_dma_cookie_t *cp) 540Sstevel@tonic-gate { 550Sstevel@tonic-gate ddi_dma_impl_t *mp = (ddi_dma_impl_t *)h; 560Sstevel@tonic-gate fdvma_t *fdvma_p = (fdvma_t *)mp->dmai_fdvma; 570Sstevel@tonic-gate pci_t *pci_p = (pci_t *)fdvma_p->softsp; 580Sstevel@tonic-gate iommu_t *iommu_p = pci_p->pci_iommu_p; 590Sstevel@tonic-gate dev_info_t *dip = pci_p->pci_dip; 600Sstevel@tonic-gate dvma_addr_t dvma_addr, dvma_pg; 610Sstevel@tonic-gate caddr_t baseaddr = (caddr_t)((uintptr_t)a & PAGEMASK); 620Sstevel@tonic-gate uint32_t offset; 630Sstevel@tonic-gate size_t npages, pg_index; 640Sstevel@tonic-gate pfn_t pfn; 650Sstevel@tonic-gate int i; 660Sstevel@tonic-gate uint64_t tte; 670Sstevel@tonic-gate 68946Smathue offset = (uint32_t)(uintptr_t)a & IOMMU_PAGE_OFFSET; 690Sstevel@tonic-gate npages = IOMMU_BTOPR(len + offset); 700Sstevel@tonic-gate if (!npages) 710Sstevel@tonic-gate return; 720Sstevel@tonic-gate 730Sstevel@tonic-gate /* make sure we don't exceed reserved boundary */ 740Sstevel@tonic-gate DEBUG3(DBG_FAST_DVMA, dip, "load index=%x: %p+%x ", index, a, len); 750Sstevel@tonic-gate if (index + npages > mp->dmai_ndvmapages) { 760Sstevel@tonic-gate cmn_err(pci_panic_on_fatal_errors ? CE_PANIC : CE_WARN, 77946Smathue "%s%d: kaddr_load index(%x)+pgs(%lx) exceeds limit\n", 780Sstevel@tonic-gate ddi_driver_name(dip), ddi_get_instance(dip), 790Sstevel@tonic-gate index, npages); 800Sstevel@tonic-gate return; 810Sstevel@tonic-gate } 820Sstevel@tonic-gate 830Sstevel@tonic-gate /* better have not already loaded something at this address */ 840Sstevel@tonic-gate ASSERT(fdvma_p->kvbase[index] == NULL); 850Sstevel@tonic-gate ASSERT(fdvma_p->pagecnt[index] == 0); 860Sstevel@tonic-gate 870Sstevel@tonic-gate dvma_addr = mp->dmai_mapping + IOMMU_PTOB(index); 880Sstevel@tonic-gate dvma_pg = IOMMU_BTOP(dvma_addr); 890Sstevel@tonic-gate pg_index = dvma_pg - iommu_p->dvma_base_pg; 900Sstevel@tonic-gate 910Sstevel@tonic-gate /* construct the dma cookie to be returned */ 920Sstevel@tonic-gate MAKE_DMA_COOKIE(cp, dvma_addr | offset, len); 930Sstevel@tonic-gate DEBUG2(DBG_FAST_DVMA | DBG_CONT, dip, "cookie: %x+%x\n", 940Sstevel@tonic-gate cp->dmac_address, cp->dmac_size); 950Sstevel@tonic-gate 960Sstevel@tonic-gate for (i = 0, a = baseaddr; i < npages; i++, a += IOMMU_PAGE_SIZE) { 970Sstevel@tonic-gate if (pci_dvma_remap_enabled) { 980Sstevel@tonic-gate uint_t flags = HAC_NOSLEEP | HAC_PAGELOCK; 990Sstevel@tonic-gate 1000Sstevel@tonic-gate (void) hat_add_callback(pci_fast_dvma_cbid, a, 101*2251Selowe IOMMU_PAGE_SIZE, flags, mp, &pfn, 102*2251Selowe &fdvma_p->cbcookie[index + i]); 1030Sstevel@tonic-gate 1040Sstevel@tonic-gate mp->dmai_flags |= DMAI_FLAGS_RELOC; 1050Sstevel@tonic-gate } else { 1060Sstevel@tonic-gate pfn = hat_getpfnum(kas.a_hat, a); 1070Sstevel@tonic-gate } 1080Sstevel@tonic-gate if (pfn == PFN_INVALID) 1090Sstevel@tonic-gate goto bad_pfn; 1100Sstevel@tonic-gate 1110Sstevel@tonic-gate if (i == 0) /* setup template, all bits except pfn value */ 1120Sstevel@tonic-gate tte = MAKE_TTE_TEMPLATE((iopfn_t)pfn, mp); 1130Sstevel@tonic-gate 1140Sstevel@tonic-gate /* XXX assumes iommu and mmu has same page size */ 1150Sstevel@tonic-gate iommu_p->iommu_tsb_vaddr[pg_index + i] = tte | IOMMU_PTOB(pfn); 1160Sstevel@tonic-gate IOMMU_PAGE_FLUSH(iommu_p, (dvma_pg + i)); 1170Sstevel@tonic-gate } 1180Sstevel@tonic-gate 1190Sstevel@tonic-gate mp->dmai_flags |= DMAI_FLAGS_MAPPED; 1200Sstevel@tonic-gate fdvma_p->kvbase[index] = baseaddr; 1210Sstevel@tonic-gate fdvma_p->pagecnt[index] = npages; 1220Sstevel@tonic-gate 1230Sstevel@tonic-gate return; 1240Sstevel@tonic-gate bad_pfn: 1250Sstevel@tonic-gate cmn_err(CE_WARN, "%s%d: kaddr_load can't get page frame for vaddr %x", 126946Smathue ddi_driver_name(dip), ddi_get_instance(dip), (int)(uintptr_t)a); 1270Sstevel@tonic-gate } 1280Sstevel@tonic-gate 1290Sstevel@tonic-gate /*ARGSUSED*/ 1300Sstevel@tonic-gate static void 1310Sstevel@tonic-gate pci_fdvma_unload(ddi_dma_handle_t h, uint_t index, uint_t sync_flags) 1320Sstevel@tonic-gate { 1330Sstevel@tonic-gate ddi_dma_impl_t *mp = (ddi_dma_impl_t *)h; 1340Sstevel@tonic-gate fdvma_t *fdvma_p = (fdvma_t *)mp->dmai_fdvma; 1350Sstevel@tonic-gate pci_t *pci_p = (pci_t *)fdvma_p->softsp; 1360Sstevel@tonic-gate size_t npg = fdvma_p->pagecnt[index]; 1370Sstevel@tonic-gate 1380Sstevel@tonic-gate dvma_addr_t dvma_pg = IOMMU_BTOP(mp->dmai_mapping + IOMMU_PTOB(index)); 1390Sstevel@tonic-gate 1400Sstevel@tonic-gate DEBUG5(DBG_FAST_DVMA, pci_p->pci_dip, 1410Sstevel@tonic-gate "unload index=%x flags=%x %x+%x+%x\n", index, sync_flags, 1420Sstevel@tonic-gate mp->dmai_mapping, IOMMU_PTOB(index), IOMMU_PTOB(npg)); 1430Sstevel@tonic-gate 1440Sstevel@tonic-gate if (!pci_dvma_sync_before_unmap) { 1450Sstevel@tonic-gate if (PCI_DMA_CANRELOC(mp)) 1460Sstevel@tonic-gate pci_fdvma_unregister_callbacks(pci_p, fdvma_p, mp, 1470Sstevel@tonic-gate index); 1480Sstevel@tonic-gate fdvma_p->kvbase[index] = NULL; 1490Sstevel@tonic-gate iommu_unmap_pages(pci_p->pci_iommu_p, dvma_pg, npg); 1500Sstevel@tonic-gate } 1510Sstevel@tonic-gate if (sync_flags != -1) 1520Sstevel@tonic-gate pci_dma_sync(pci_p->pci_dip, mp->dmai_rdip, h, 1530Sstevel@tonic-gate IOMMU_PTOB(index), IOMMU_PTOB(npg), sync_flags); 1540Sstevel@tonic-gate if (pci_dvma_sync_before_unmap) { 1550Sstevel@tonic-gate if (PCI_DMA_CANRELOC(mp)) 1560Sstevel@tonic-gate pci_fdvma_unregister_callbacks(pci_p, fdvma_p, mp, 1570Sstevel@tonic-gate index); 1580Sstevel@tonic-gate fdvma_p->kvbase[index] = NULL; 1590Sstevel@tonic-gate iommu_unmap_pages(pci_p->pci_iommu_p, dvma_pg, npg); 1600Sstevel@tonic-gate } 1610Sstevel@tonic-gate fdvma_p->pagecnt[index] = 0; 1620Sstevel@tonic-gate } 1630Sstevel@tonic-gate 1640Sstevel@tonic-gate /*ARGSUSED*/ 1650Sstevel@tonic-gate static void 1660Sstevel@tonic-gate pci_fdvma_sync(ddi_dma_handle_t h, uint_t index, uint_t sync_flags) 1670Sstevel@tonic-gate { 1680Sstevel@tonic-gate ddi_dma_impl_t *mp = (ddi_dma_impl_t *)h; 1690Sstevel@tonic-gate fdvma_t *fdvma_p = (fdvma_t *)mp->dmai_fdvma; 1700Sstevel@tonic-gate pci_t *pci_p = (pci_t *)fdvma_p->softsp; 1710Sstevel@tonic-gate size_t npg = fdvma_p->pagecnt[index]; 1720Sstevel@tonic-gate 1730Sstevel@tonic-gate DEBUG5(DBG_FAST_DVMA, pci_p->pci_dip, 1740Sstevel@tonic-gate "sync index=%x flags=%x %x+%x+%x\n", index, sync_flags, 1750Sstevel@tonic-gate mp->dmai_mapping, IOMMU_PTOB(index), IOMMU_PTOB(npg)); 1760Sstevel@tonic-gate pci_dma_sync(pci_p->pci_dip, mp->dmai_rdip, h, IOMMU_PTOB(index), 1770Sstevel@tonic-gate IOMMU_PTOB(npg), sync_flags); 1780Sstevel@tonic-gate } 1790Sstevel@tonic-gate 1800Sstevel@tonic-gate int 1810Sstevel@tonic-gate pci_fdvma_reserve(dev_info_t *dip, dev_info_t *rdip, pci_t *pci_p, 1820Sstevel@tonic-gate ddi_dma_req_t *dmareq, ddi_dma_handle_t *handlep) 1830Sstevel@tonic-gate { 1840Sstevel@tonic-gate fdvma_t *fdvma_p; 1850Sstevel@tonic-gate dvma_addr_t dvma_pg; 1860Sstevel@tonic-gate iommu_t *iommu_p = pci_p->pci_iommu_p; 1870Sstevel@tonic-gate size_t npages; 1880Sstevel@tonic-gate ddi_dma_impl_t *mp; 1890Sstevel@tonic-gate ddi_dma_lim_t *lim_p = dmareq->dmar_limits; 1900Sstevel@tonic-gate ulong_t hi = lim_p->dlim_addr_hi; 1910Sstevel@tonic-gate ulong_t lo = lim_p->dlim_addr_lo; 1920Sstevel@tonic-gate size_t counter_max = (lim_p->dlim_cntr_max + 1) & IOMMU_PAGE_MASK; 1930Sstevel@tonic-gate 1940Sstevel@tonic-gate if (pci_disable_fdvma) 1950Sstevel@tonic-gate return (DDI_FAILURE); 1960Sstevel@tonic-gate 1970Sstevel@tonic-gate DEBUG2(DBG_DMA_CTL, dip, "DDI_DMA_RESERVE: rdip=%s%d\n", 1980Sstevel@tonic-gate ddi_driver_name(rdip), ddi_get_instance(rdip)); 1990Sstevel@tonic-gate 2000Sstevel@tonic-gate /* 2010Sstevel@tonic-gate * Check the limit structure. 2020Sstevel@tonic-gate */ 2030Sstevel@tonic-gate if ((lo >= hi) || (hi < iommu_p->iommu_dvma_base)) 2040Sstevel@tonic-gate return (DDI_DMA_BADLIMITS); 2050Sstevel@tonic-gate 2060Sstevel@tonic-gate /* 2070Sstevel@tonic-gate * Check the size of the request. 2080Sstevel@tonic-gate */ 2090Sstevel@tonic-gate npages = dmareq->dmar_object.dmao_size; 2100Sstevel@tonic-gate if (npages > iommu_p->iommu_dvma_reserve) 2110Sstevel@tonic-gate return (DDI_DMA_NORESOURCES); 2120Sstevel@tonic-gate 2130Sstevel@tonic-gate /* 2140Sstevel@tonic-gate * Allocate the dma handle. 2150Sstevel@tonic-gate */ 2160Sstevel@tonic-gate mp = kmem_zalloc(sizeof (pci_dma_hdl_t), KM_SLEEP); 2170Sstevel@tonic-gate 2180Sstevel@tonic-gate /* 2190Sstevel@tonic-gate * Get entries from dvma space map. 2200Sstevel@tonic-gate * (vmem_t *vmp, 2210Sstevel@tonic-gate * size_t size, size_t align, size_t phase, 2220Sstevel@tonic-gate * size_t nocross, void *minaddr, void *maxaddr, int vmflag) 2230Sstevel@tonic-gate */ 2240Sstevel@tonic-gate dvma_pg = IOMMU_BTOP((ulong_t)vmem_xalloc(iommu_p->iommu_dvma_map, 2250Sstevel@tonic-gate IOMMU_PTOB(npages), IOMMU_PAGE_SIZE, 0, 2260Sstevel@tonic-gate counter_max, (void *)lo, (void *)(hi + 1), 2270Sstevel@tonic-gate dmareq->dmar_fp == DDI_DMA_SLEEP ? VM_SLEEP : VM_NOSLEEP)); 2280Sstevel@tonic-gate if (dvma_pg == 0) { 2290Sstevel@tonic-gate kmem_free(mp, sizeof (pci_dma_hdl_t)); 2300Sstevel@tonic-gate return (DDI_DMA_NOMAPPING); 2310Sstevel@tonic-gate } 2320Sstevel@tonic-gate iommu_p->iommu_dvma_reserve -= npages; 2330Sstevel@tonic-gate 2340Sstevel@tonic-gate /* 2350Sstevel@tonic-gate * Create the fast dvma request structure. 2360Sstevel@tonic-gate */ 2370Sstevel@tonic-gate fdvma_p = kmem_alloc(sizeof (fdvma_t), KM_SLEEP); 2380Sstevel@tonic-gate fdvma_p->kvbase = kmem_zalloc(npages * sizeof (caddr_t), KM_SLEEP); 2390Sstevel@tonic-gate fdvma_p->pagecnt = kmem_zalloc(npages * sizeof (uint_t), KM_SLEEP); 240*2251Selowe fdvma_p->cbcookie = kmem_zalloc(npages * sizeof (void *), KM_SLEEP); 2410Sstevel@tonic-gate fdvma_p->ops = &fdvma_ops; 2420Sstevel@tonic-gate fdvma_p->softsp = (caddr_t)pci_p; 2430Sstevel@tonic-gate fdvma_p->sync_flag = NULL; 2440Sstevel@tonic-gate 2450Sstevel@tonic-gate /* 2460Sstevel@tonic-gate * Initialize the handle. 2470Sstevel@tonic-gate */ 2480Sstevel@tonic-gate mp->dmai_rdip = rdip; 2490Sstevel@tonic-gate mp->dmai_rflags = DMP_BYPASSNEXUS | 2500Sstevel@tonic-gate pci_dma_consist_check(dmareq->dmar_flags, pci_p->pci_pbm_p); 2510Sstevel@tonic-gate if (!(dmareq->dmar_flags & DDI_DMA_RDWR)) 2520Sstevel@tonic-gate mp->dmai_rflags |= DDI_DMA_READ; 2530Sstevel@tonic-gate mp->dmai_flags = DMAI_FLAGS_INUSE | 2540Sstevel@tonic-gate (mp->dmai_rflags & DMP_NOSYNC ? DMAI_FLAGS_NOSYNC : 0); 2550Sstevel@tonic-gate mp->dmai_minxfer = dmareq->dmar_limits->dlim_minxfer; 2560Sstevel@tonic-gate mp->dmai_burstsizes = dmareq->dmar_limits->dlim_burstsizes; 2570Sstevel@tonic-gate mp->dmai_mapping = IOMMU_PTOB(dvma_pg); 2580Sstevel@tonic-gate mp->dmai_ndvmapages = npages; 2590Sstevel@tonic-gate mp->dmai_size = npages * IOMMU_PAGE_SIZE; 2600Sstevel@tonic-gate mp->dmai_nwin = 0; 2610Sstevel@tonic-gate mp->dmai_fdvma = (caddr_t)fdvma_p; 2620Sstevel@tonic-gate 2630Sstevel@tonic-gate DEBUG4(DBG_DMA_CTL, dip, 2640Sstevel@tonic-gate "PCI_DVMA_RESERVE: mp=%p dvma=%x npages=%x private=%p\n", 2650Sstevel@tonic-gate mp, mp->dmai_mapping, npages, fdvma_p); 2660Sstevel@tonic-gate *handlep = (ddi_dma_handle_t)mp; 2670Sstevel@tonic-gate return (DDI_SUCCESS); 2680Sstevel@tonic-gate } 2690Sstevel@tonic-gate 2700Sstevel@tonic-gate int 2710Sstevel@tonic-gate pci_fdvma_release(dev_info_t *dip, pci_t *pci_p, ddi_dma_impl_t *mp) 2720Sstevel@tonic-gate { 2730Sstevel@tonic-gate iommu_t *iommu_p = pci_p->pci_iommu_p; 2740Sstevel@tonic-gate size_t npages; 2750Sstevel@tonic-gate fdvma_t *fdvma_p = (fdvma_t *)mp->dmai_fdvma; 2760Sstevel@tonic-gate 2770Sstevel@tonic-gate if (pci_disable_fdvma) 2780Sstevel@tonic-gate return (DDI_FAILURE); 2790Sstevel@tonic-gate 2800Sstevel@tonic-gate /* validate fdvma handle */ 2810Sstevel@tonic-gate if (!(mp->dmai_rflags & DMP_BYPASSNEXUS)) { 2820Sstevel@tonic-gate DEBUG0(DBG_DMA_CTL, dip, "DDI_DMA_RELEASE: not fast dma\n"); 2830Sstevel@tonic-gate return (DDI_FAILURE); 2840Sstevel@tonic-gate } 2850Sstevel@tonic-gate 2860Sstevel@tonic-gate /* flush all reserved dvma addresses from iommu */ 2870Sstevel@tonic-gate pci_dma_sync_unmap(dip, mp->dmai_rdip, mp); 2880Sstevel@tonic-gate 2890Sstevel@tonic-gate npages = mp->dmai_ndvmapages; 2900Sstevel@tonic-gate pci_vmem_free(iommu_p, mp, (void *)mp->dmai_mapping, npages); 2910Sstevel@tonic-gate 2920Sstevel@tonic-gate iommu_p->iommu_dvma_reserve += npages; 2930Sstevel@tonic-gate mp->dmai_ndvmapages = 0; 2940Sstevel@tonic-gate 2950Sstevel@tonic-gate /* see if there is anyone waiting for dvma space */ 2960Sstevel@tonic-gate if (iommu_p->iommu_dvma_clid != 0) { 2970Sstevel@tonic-gate DEBUG0(DBG_DMA_CTL, dip, "run dvma callback\n"); 2980Sstevel@tonic-gate ddi_run_callback(&iommu_p->iommu_dvma_clid); 2990Sstevel@tonic-gate } 3000Sstevel@tonic-gate 3010Sstevel@tonic-gate /* free data structures */ 3020Sstevel@tonic-gate kmem_free(fdvma_p->kvbase, npages * sizeof (caddr_t)); 3030Sstevel@tonic-gate kmem_free(fdvma_p->pagecnt, npages * sizeof (uint_t)); 304*2251Selowe kmem_free(fdvma_p->cbcookie, npages * sizeof (void *)); 3050Sstevel@tonic-gate kmem_free(fdvma_p, sizeof (fdvma_t)); 3060Sstevel@tonic-gate kmem_free(mp, sizeof (pci_dma_hdl_t)); 3070Sstevel@tonic-gate 3080Sstevel@tonic-gate /* see if there is anyone waiting for kmem */ 3090Sstevel@tonic-gate if (pci_kmem_clid != 0) { 3100Sstevel@tonic-gate DEBUG0(DBG_DMA_CTL, dip, "run handle callback\n"); 3110Sstevel@tonic-gate ddi_run_callback(&pci_kmem_clid); 3120Sstevel@tonic-gate } 3130Sstevel@tonic-gate return (DDI_SUCCESS); 3140Sstevel@tonic-gate } 3150Sstevel@tonic-gate 3160Sstevel@tonic-gate /* 3170Sstevel@tonic-gate * fast dvma ops structure: 3180Sstevel@tonic-gate */ 3190Sstevel@tonic-gate static struct dvma_ops fdvma_ops = { 3200Sstevel@tonic-gate DVMAO_REV, 3210Sstevel@tonic-gate pci_fdvma_load, 3220Sstevel@tonic-gate pci_fdvma_unload, 3230Sstevel@tonic-gate pci_fdvma_sync 3240Sstevel@tonic-gate }; 325