17c478bd9Sstevel@tonic-gate /* 27c478bd9Sstevel@tonic-gate * CDDL HEADER START 37c478bd9Sstevel@tonic-gate * 47c478bd9Sstevel@tonic-gate * The contents of this file are subject to the terms of the 51de45cd9Sgovinda * Common Development and Distribution License (the "License"). 61de45cd9Sgovinda * You may not use this file except in compliance with the License. 77c478bd9Sstevel@tonic-gate * 87c478bd9Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 97c478bd9Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing. 107c478bd9Sstevel@tonic-gate * See the License for the specific language governing permissions 117c478bd9Sstevel@tonic-gate * and limitations under the License. 127c478bd9Sstevel@tonic-gate * 137c478bd9Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each 147c478bd9Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 157c478bd9Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the 167c478bd9Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying 177c478bd9Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner] 187c478bd9Sstevel@tonic-gate * 197c478bd9Sstevel@tonic-gate * CDDL HEADER END 207c478bd9Sstevel@tonic-gate */ 217c478bd9Sstevel@tonic-gate /* 22*0ea48847SBhaskar Sarkar * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved. 237c478bd9Sstevel@tonic-gate */ 247c478bd9Sstevel@tonic-gate 257c478bd9Sstevel@tonic-gate /* 267c478bd9Sstevel@tonic-gate * Internal PCI Fast DVMA implementation 277c478bd9Sstevel@tonic-gate */ 287c478bd9Sstevel@tonic-gate #include <sys/types.h> 297c478bd9Sstevel@tonic-gate #include <sys/kmem.h> 307c478bd9Sstevel@tonic-gate #include <sys/async.h> 317c478bd9Sstevel@tonic-gate #include <sys/sysmacros.h> 327c478bd9Sstevel@tonic-gate #include <sys/sunddi.h> 337c478bd9Sstevel@tonic-gate #include <sys/ddi_impldefs.h> 347c478bd9Sstevel@tonic-gate #include <sys/dvma.h> 357c478bd9Sstevel@tonic-gate #include "px_obj.h" 367c478bd9Sstevel@tonic-gate 377c478bd9Sstevel@tonic-gate /*LINTLIBRARY*/ 387c478bd9Sstevel@tonic-gate 397c478bd9Sstevel@tonic-gate static struct dvma_ops fdvma_ops; 407c478bd9Sstevel@tonic-gate typedef struct fast_dvma fdvma_t; 417c478bd9Sstevel@tonic-gate 427c478bd9Sstevel@tonic-gate /* 437c478bd9Sstevel@tonic-gate * The following routines are used to implement the sun4u fast dvma 447c478bd9Sstevel@tonic-gate * routines on this bus. 457c478bd9Sstevel@tonic-gate */ 467c478bd9Sstevel@tonic-gate 477c478bd9Sstevel@tonic-gate /*ARGSUSED*/ 487c478bd9Sstevel@tonic-gate static void 497c478bd9Sstevel@tonic-gate px_fdvma_load(ddi_dma_handle_t h, caddr_t a, uint_t len, uint_t index, 507c478bd9Sstevel@tonic-gate ddi_dma_cookie_t *cp) 517c478bd9Sstevel@tonic-gate { 527c478bd9Sstevel@tonic-gate ddi_dma_impl_t *mp = (ddi_dma_impl_t *)h; 537c478bd9Sstevel@tonic-gate fdvma_t *fdvma_p = (fdvma_t *)mp->dmai_fdvma; 547c478bd9Sstevel@tonic-gate px_t *px_p = (px_t *)fdvma_p->softsp; 557c478bd9Sstevel@tonic-gate px_mmu_t *mmu_p = px_p->px_mmu_p; 567c478bd9Sstevel@tonic-gate dev_info_t *dip = px_p->px_dip; 577c478bd9Sstevel@tonic-gate px_dvma_addr_t dvma_addr, dvma_pg; 587c478bd9Sstevel@tonic-gate uint32_t offset; 597c478bd9Sstevel@tonic-gate size_t npages, pg_index; 60ef2504f2SDaniel Ice io_attributes_t attr; 617c478bd9Sstevel@tonic-gate 62b40cec45Skrishnae offset = (uint32_t)(uintptr_t)a & MMU_PAGE_OFFSET; 637c478bd9Sstevel@tonic-gate npages = MMU_BTOPR(len + offset); 647c478bd9Sstevel@tonic-gate if (!npages) 657c478bd9Sstevel@tonic-gate return; 667c478bd9Sstevel@tonic-gate 677c478bd9Sstevel@tonic-gate /* make sure we don't exceed reserved boundary */ 687c478bd9Sstevel@tonic-gate DBG(DBG_FAST_DVMA, dip, "load index=%x: %p+%x ", index, a, len); 697c478bd9Sstevel@tonic-gate if (index + npages > mp->dmai_ndvmapages) { 707c478bd9Sstevel@tonic-gate cmn_err(px_panic_on_fatal_errors ? CE_PANIC : CE_WARN, 71b40cec45Skrishnae "%s%d: kaddr_load index(%x)+pgs(%lx) exceeds limit\n", 727c478bd9Sstevel@tonic-gate ddi_driver_name(dip), ddi_get_instance(dip), 737c478bd9Sstevel@tonic-gate index, npages); 747c478bd9Sstevel@tonic-gate return; 757c478bd9Sstevel@tonic-gate } 767c478bd9Sstevel@tonic-gate fdvma_p->pagecnt[index] = npages; 777c478bd9Sstevel@tonic-gate 787c478bd9Sstevel@tonic-gate dvma_addr = mp->dmai_mapping + MMU_PTOB(index); 797c478bd9Sstevel@tonic-gate dvma_pg = MMU_BTOP(dvma_addr); 807c478bd9Sstevel@tonic-gate pg_index = dvma_pg - mmu_p->dvma_base_pg; 817c478bd9Sstevel@tonic-gate 827c478bd9Sstevel@tonic-gate /* construct the dma cookie to be returned */ 837c478bd9Sstevel@tonic-gate MAKE_DMA_COOKIE(cp, dvma_addr | offset, len); 847c478bd9Sstevel@tonic-gate DBG(DBG_FAST_DVMA | DBG_CONT, dip, "cookie: %x+%x\n", 857c478bd9Sstevel@tonic-gate cp->dmac_address, cp->dmac_size); 867c478bd9Sstevel@tonic-gate 8725cf1a30Sjl139090 attr = PX_GET_TTE_ATTR(mp->dmai_rflags, mp->dmai_attr.dma_attr_flags); 887c478bd9Sstevel@tonic-gate 8944961713Sgirish if (px_lib_iommu_map(dip, PCI_TSBID(0, pg_index), npages, 9044961713Sgirish PX_ADD_ATTR_EXTNS(attr, mp->dmai_bdf), (void *)a, 0, 9144961713Sgirish MMU_MAP_BUF) != DDI_SUCCESS) { 927c478bd9Sstevel@tonic-gate cmn_err(CE_WARN, "%s%d: kaddr_load can't get " 93b40cec45Skrishnae "page frame for vaddr %lx", ddi_driver_name(dip), 94b40cec45Skrishnae ddi_get_instance(dip), (uintptr_t)a); 957c478bd9Sstevel@tonic-gate } 967c478bd9Sstevel@tonic-gate } 977c478bd9Sstevel@tonic-gate 987c478bd9Sstevel@tonic-gate /*ARGSUSED*/ 997c478bd9Sstevel@tonic-gate static void 1007c478bd9Sstevel@tonic-gate px_fdvma_unload(ddi_dma_handle_t h, uint_t index, uint_t sync_flag) 1017c478bd9Sstevel@tonic-gate { 1027c478bd9Sstevel@tonic-gate ddi_dma_impl_t *mp = (ddi_dma_impl_t *)h; 1037c478bd9Sstevel@tonic-gate fdvma_t *fdvma_p = (fdvma_t *)mp->dmai_fdvma; 1047c478bd9Sstevel@tonic-gate px_t *px_p = (px_t *)fdvma_p->softsp; 1057c478bd9Sstevel@tonic-gate size_t npages = fdvma_p->pagecnt[index]; 1067c478bd9Sstevel@tonic-gate px_dvma_addr_t dvma_pg = MMU_BTOP(mp->dmai_mapping + MMU_PTOB(index)); 1077c478bd9Sstevel@tonic-gate 1087c478bd9Sstevel@tonic-gate DBG(DBG_FAST_DVMA, px_p->px_dip, 1097c478bd9Sstevel@tonic-gate "unload index=%x sync_flag=%x %x+%x+%x\n", index, sync_flag, 1107c478bd9Sstevel@tonic-gate mp->dmai_mapping, MMU_PTOB(index), MMU_PTOB(npages)); 1117c478bd9Sstevel@tonic-gate 1121de45cd9Sgovinda px_mmu_unmap_pages(px_p->px_mmu_p, mp, dvma_pg, npages); 1137c478bd9Sstevel@tonic-gate fdvma_p->pagecnt[index] = 0; 1147c478bd9Sstevel@tonic-gate } 1157c478bd9Sstevel@tonic-gate 1167c478bd9Sstevel@tonic-gate /*ARGSUSED*/ 1177c478bd9Sstevel@tonic-gate static void 1187c478bd9Sstevel@tonic-gate px_fdvma_sync(ddi_dma_handle_t h, uint_t index, uint_t sync_flag) 1197c478bd9Sstevel@tonic-gate { 1207c478bd9Sstevel@tonic-gate ddi_dma_impl_t *mp = (ddi_dma_impl_t *)h; 1217c478bd9Sstevel@tonic-gate fdvma_t *fdvma_p = (fdvma_t *)mp->dmai_fdvma; 1227c478bd9Sstevel@tonic-gate px_t *px_p = (px_t *)fdvma_p->softsp; 1237c478bd9Sstevel@tonic-gate size_t npg = fdvma_p->pagecnt[index]; 1247c478bd9Sstevel@tonic-gate 1257c478bd9Sstevel@tonic-gate DBG(DBG_FAST_DVMA, px_p->px_dip, 1267c478bd9Sstevel@tonic-gate "sync index=%x sync_flag=%x %x+%x+%x\n", index, sync_flag, 1277c478bd9Sstevel@tonic-gate mp->dmai_mapping, MMU_PTOB(index), MMU_PTOB(npg)); 1287c478bd9Sstevel@tonic-gate } 1297c478bd9Sstevel@tonic-gate 1307c478bd9Sstevel@tonic-gate int 1317c478bd9Sstevel@tonic-gate px_fdvma_reserve(dev_info_t *dip, dev_info_t *rdip, px_t *px_p, 1327c478bd9Sstevel@tonic-gate ddi_dma_req_t *dmareq, ddi_dma_handle_t *handlep) 1337c478bd9Sstevel@tonic-gate { 1347c478bd9Sstevel@tonic-gate fdvma_t *fdvma_p; 1357c478bd9Sstevel@tonic-gate px_dvma_addr_t dvma_pg; 1367c478bd9Sstevel@tonic-gate px_mmu_t *mmu_p = px_p->px_mmu_p; 1377c478bd9Sstevel@tonic-gate size_t npages; 1387c478bd9Sstevel@tonic-gate ddi_dma_impl_t *mp; 1397c478bd9Sstevel@tonic-gate ddi_dma_lim_t *lim_p = dmareq->dmar_limits; 1407c478bd9Sstevel@tonic-gate ulong_t hi = lim_p->dlim_addr_hi; 1417c478bd9Sstevel@tonic-gate ulong_t lo = lim_p->dlim_addr_lo; 1427c478bd9Sstevel@tonic-gate size_t counter_max = (lim_p->dlim_cntr_max + 1) & MMU_PAGE_MASK; 1437c478bd9Sstevel@tonic-gate 1447c478bd9Sstevel@tonic-gate if (px_disable_fdvma) 1457c478bd9Sstevel@tonic-gate return (DDI_FAILURE); 1467c478bd9Sstevel@tonic-gate 1477c478bd9Sstevel@tonic-gate DBG(DBG_DMA_CTL, dip, "DDI_DMA_RESERVE: rdip=%s%d\n", 1487c478bd9Sstevel@tonic-gate ddi_driver_name(rdip), ddi_get_instance(rdip)); 1497c478bd9Sstevel@tonic-gate 1507c478bd9Sstevel@tonic-gate /* 1517c478bd9Sstevel@tonic-gate * Check the limit structure. 1527c478bd9Sstevel@tonic-gate */ 1537c478bd9Sstevel@tonic-gate if ((lo >= hi) || (hi < mmu_p->mmu_dvma_base)) 1547c478bd9Sstevel@tonic-gate return (DDI_DMA_BADLIMITS); 1557c478bd9Sstevel@tonic-gate 1567c478bd9Sstevel@tonic-gate /* 157*0ea48847SBhaskar Sarkar * Allocate DVMA space from reserve. 1587c478bd9Sstevel@tonic-gate */ 1597c478bd9Sstevel@tonic-gate npages = dmareq->dmar_object.dmao_size; 160*0ea48847SBhaskar Sarkar if ((long)atomic_add_long_nv(&mmu_p->mmu_dvma_reserve, -npages) < 0) { 161*0ea48847SBhaskar Sarkar atomic_add_long(&mmu_p->mmu_dvma_reserve, npages); 1627c478bd9Sstevel@tonic-gate return (DDI_DMA_NORESOURCES); 163*0ea48847SBhaskar Sarkar } 1647c478bd9Sstevel@tonic-gate 1657c478bd9Sstevel@tonic-gate /* 1667c478bd9Sstevel@tonic-gate * Allocate the dma handle. 1677c478bd9Sstevel@tonic-gate */ 1687c478bd9Sstevel@tonic-gate mp = kmem_zalloc(sizeof (px_dma_hdl_t), KM_SLEEP); 1697c478bd9Sstevel@tonic-gate 1707c478bd9Sstevel@tonic-gate /* 1717c478bd9Sstevel@tonic-gate * Get entries from dvma space map. 1727c478bd9Sstevel@tonic-gate * (vmem_t *vmp, 1737c478bd9Sstevel@tonic-gate * size_t size, size_t align, size_t phase, 1747c478bd9Sstevel@tonic-gate * size_t nocross, void *minaddr, void *maxaddr, int vmflag) 1757c478bd9Sstevel@tonic-gate */ 1767c478bd9Sstevel@tonic-gate dvma_pg = MMU_BTOP((ulong_t)vmem_xalloc(mmu_p->mmu_dvma_map, 1777c478bd9Sstevel@tonic-gate MMU_PTOB(npages), MMU_PAGE_SIZE, 0, 1787c478bd9Sstevel@tonic-gate counter_max, (void *)lo, (void *)(hi + 1), 1797c478bd9Sstevel@tonic-gate dmareq->dmar_fp == DDI_DMA_SLEEP ? VM_SLEEP : VM_NOSLEEP)); 1807c478bd9Sstevel@tonic-gate if (dvma_pg == 0) { 181*0ea48847SBhaskar Sarkar atomic_add_long(&mmu_p->mmu_dvma_reserve, npages); 1827c478bd9Sstevel@tonic-gate kmem_free(mp, sizeof (px_dma_hdl_t)); 1837c478bd9Sstevel@tonic-gate return (DDI_DMA_NOMAPPING); 1847c478bd9Sstevel@tonic-gate } 1857c478bd9Sstevel@tonic-gate 1867c478bd9Sstevel@tonic-gate /* 1877c478bd9Sstevel@tonic-gate * Create the fast dvma request structure. 1887c478bd9Sstevel@tonic-gate */ 1897c478bd9Sstevel@tonic-gate fdvma_p = kmem_alloc(sizeof (fdvma_t), KM_SLEEP); 1907c478bd9Sstevel@tonic-gate fdvma_p->pagecnt = kmem_alloc(npages * sizeof (uint_t), KM_SLEEP); 1917c478bd9Sstevel@tonic-gate fdvma_p->ops = &fdvma_ops; 1927c478bd9Sstevel@tonic-gate fdvma_p->softsp = (caddr_t)px_p; 1937c478bd9Sstevel@tonic-gate fdvma_p->sync_flag = NULL; 1947c478bd9Sstevel@tonic-gate 1957c478bd9Sstevel@tonic-gate /* 1967c478bd9Sstevel@tonic-gate * Initialize the handle. 1977c478bd9Sstevel@tonic-gate */ 1987c478bd9Sstevel@tonic-gate mp->dmai_rdip = rdip; 1997c478bd9Sstevel@tonic-gate mp->dmai_rflags = DMP_BYPASSNEXUS | DDI_DMA_READ | DMP_NOSYNC; 2007c478bd9Sstevel@tonic-gate mp->dmai_burstsizes = dmareq->dmar_limits->dlim_burstsizes; 2017c478bd9Sstevel@tonic-gate mp->dmai_mapping = MMU_PTOB(dvma_pg); 2027c478bd9Sstevel@tonic-gate mp->dmai_ndvmapages = npages; 2037c478bd9Sstevel@tonic-gate mp->dmai_size = npages * MMU_PAGE_SIZE; 2047c478bd9Sstevel@tonic-gate mp->dmai_nwin = 0; 2057c478bd9Sstevel@tonic-gate mp->dmai_fdvma = (caddr_t)fdvma_p; 2063aa1cd26Sgovinda 2073aa1cd26Sgovinda /* 2089fc8611eSDaniel Ice * The bdf protection value is set to immediate child 2099fc8611eSDaniel Ice * at first. It gets modified by switch/bridge drivers 2109fc8611eSDaniel Ice * as the code traverses down the fabric topology. 2119fc8611eSDaniel Ice * 2129fc8611eSDaniel Ice * XXX No IOMMU protection for broken devices. 2133aa1cd26Sgovinda */ 2149fc8611eSDaniel Ice ASSERT((intptr_t)ddi_get_parent_data(rdip) >> 1 == 0); 215c85864d8SKrishna Elango mp->dmai_bdf = ((intptr_t)ddi_get_parent_data(rdip) == 1) ? 216c85864d8SKrishna Elango PCIE_INVALID_BDF : pcie_get_bdf_for_dma_xfer(dip, rdip); 2173aa1cd26Sgovinda 2187c478bd9Sstevel@tonic-gate DBG(DBG_DMA_CTL, dip, 2197c478bd9Sstevel@tonic-gate "DDI_DMA_RESERVE: mp=%p dvma=%x npages=%x private=%p\n", 2207c478bd9Sstevel@tonic-gate mp, mp->dmai_mapping, npages, fdvma_p); 2217c478bd9Sstevel@tonic-gate *handlep = (ddi_dma_handle_t)mp; 2227c478bd9Sstevel@tonic-gate return (DDI_SUCCESS); 2237c478bd9Sstevel@tonic-gate } 2247c478bd9Sstevel@tonic-gate 2257c478bd9Sstevel@tonic-gate int 2267c478bd9Sstevel@tonic-gate px_fdvma_release(dev_info_t *dip, px_t *px_p, ddi_dma_impl_t *mp) 2277c478bd9Sstevel@tonic-gate { 2287c478bd9Sstevel@tonic-gate px_mmu_t *mmu_p = px_p->px_mmu_p; 2297c478bd9Sstevel@tonic-gate size_t npages; 2307c478bd9Sstevel@tonic-gate fdvma_t *fdvma_p = (fdvma_t *)mp->dmai_fdvma; 2317c478bd9Sstevel@tonic-gate 2327c478bd9Sstevel@tonic-gate if (px_disable_fdvma) 2337c478bd9Sstevel@tonic-gate return (DDI_FAILURE); 2347c478bd9Sstevel@tonic-gate 2357c478bd9Sstevel@tonic-gate /* validate fdvma handle */ 2367c478bd9Sstevel@tonic-gate if (!(mp->dmai_rflags & DMP_BYPASSNEXUS)) { 2377c478bd9Sstevel@tonic-gate DBG(DBG_DMA_CTL, dip, "DDI_DMA_RELEASE: not fast dma\n"); 2387c478bd9Sstevel@tonic-gate return (DDI_FAILURE); 2397c478bd9Sstevel@tonic-gate } 2407c478bd9Sstevel@tonic-gate 2417c478bd9Sstevel@tonic-gate /* flush all reserved dvma addresses from mmu */ 2427c478bd9Sstevel@tonic-gate px_mmu_unmap_window(mmu_p, mp); 2437c478bd9Sstevel@tonic-gate 2447c478bd9Sstevel@tonic-gate npages = mp->dmai_ndvmapages; 2457c478bd9Sstevel@tonic-gate vmem_xfree(mmu_p->mmu_dvma_map, (void *)mp->dmai_mapping, 2467c478bd9Sstevel@tonic-gate MMU_PTOB(npages)); 2477c478bd9Sstevel@tonic-gate 248*0ea48847SBhaskar Sarkar atomic_add_long(&mmu_p->mmu_dvma_reserve, npages); 2497c478bd9Sstevel@tonic-gate mp->dmai_ndvmapages = 0; 2507c478bd9Sstevel@tonic-gate 2517c478bd9Sstevel@tonic-gate /* see if there is anyone waiting for dvma space */ 2527c478bd9Sstevel@tonic-gate if (mmu_p->mmu_dvma_clid != 0) { 2537c478bd9Sstevel@tonic-gate DBG(DBG_DMA_CTL, dip, "run dvma callback\n"); 2547c478bd9Sstevel@tonic-gate ddi_run_callback(&mmu_p->mmu_dvma_clid); 2557c478bd9Sstevel@tonic-gate } 2567c478bd9Sstevel@tonic-gate 2577c478bd9Sstevel@tonic-gate /* free data structures */ 2587c478bd9Sstevel@tonic-gate kmem_free(fdvma_p->pagecnt, npages * sizeof (uint_t)); 2597c478bd9Sstevel@tonic-gate kmem_free(fdvma_p, sizeof (fdvma_t)); 2607c478bd9Sstevel@tonic-gate kmem_free(mp, sizeof (px_dma_hdl_t)); 2617c478bd9Sstevel@tonic-gate 2627c478bd9Sstevel@tonic-gate /* see if there is anyone waiting for kmem */ 2637c478bd9Sstevel@tonic-gate if (px_kmem_clid != 0) { 2647c478bd9Sstevel@tonic-gate DBG(DBG_DMA_CTL, dip, "run handle callback\n"); 2657c478bd9Sstevel@tonic-gate ddi_run_callback(&px_kmem_clid); 2667c478bd9Sstevel@tonic-gate } 2677c478bd9Sstevel@tonic-gate return (DDI_SUCCESS); 2687c478bd9Sstevel@tonic-gate } 2697c478bd9Sstevel@tonic-gate 2707c478bd9Sstevel@tonic-gate static struct dvma_ops fdvma_ops = { 2717c478bd9Sstevel@tonic-gate DVMAO_REV, 2727c478bd9Sstevel@tonic-gate px_fdvma_load, 2737c478bd9Sstevel@tonic-gate px_fdvma_unload, 2747c478bd9Sstevel@tonic-gate px_fdvma_sync 2757c478bd9Sstevel@tonic-gate }; 276