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 /* 221501Sgovinda * 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 * PX mmu initialization and configuration 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/vmem.h> 380Sstevel@tonic-gate #include <sys/machsystm.h> /* lddphys() */ 390Sstevel@tonic-gate #include <sys/iommutsb.h> 400Sstevel@tonic-gate #include "px_obj.h" 410Sstevel@tonic-gate 420Sstevel@tonic-gate int 430Sstevel@tonic-gate px_mmu_attach(px_t *px_p) 440Sstevel@tonic-gate { 450Sstevel@tonic-gate dev_info_t *dip = px_p->px_dip; 460Sstevel@tonic-gate px_mmu_t *mmu_p; 470Sstevel@tonic-gate uint32_t base_pg_index, i = 0; 480Sstevel@tonic-gate char map_name[32]; 490Sstevel@tonic-gate px_dvma_range_prop_t *dvma_prop; 500Sstevel@tonic-gate int dvma_prop_len; 510Sstevel@tonic-gate uint32_t cache_size, tsb_entries; 520Sstevel@tonic-gate 530Sstevel@tonic-gate /* 540Sstevel@tonic-gate * Allocate mmu state structure and link it to the 550Sstevel@tonic-gate * px state structure. 560Sstevel@tonic-gate */ 570Sstevel@tonic-gate mmu_p = kmem_zalloc(sizeof (px_mmu_t), KM_SLEEP); 580Sstevel@tonic-gate if (mmu_p == NULL) 590Sstevel@tonic-gate return (DDI_FAILURE); 600Sstevel@tonic-gate 610Sstevel@tonic-gate px_p->px_mmu_p = mmu_p; 620Sstevel@tonic-gate mmu_p->mmu_px_p = px_p; 630Sstevel@tonic-gate mmu_p->mmu_inst = ddi_get_instance(dip); 640Sstevel@tonic-gate 650Sstevel@tonic-gate /* 660Sstevel@tonic-gate * Check for "virtual-dma" property that specifies 670Sstevel@tonic-gate * the DVMA range. 680Sstevel@tonic-gate */ 690Sstevel@tonic-gate if (ddi_getlongprop(DDI_DEV_T_ANY, dip, DDI_PROP_DONTPASS, 700Sstevel@tonic-gate "virtual-dma", (caddr_t)&dvma_prop, &dvma_prop_len) != 710Sstevel@tonic-gate DDI_PROP_SUCCESS) { 720Sstevel@tonic-gate 730Sstevel@tonic-gate DBG(DBG_ATTACH, dip, "Getting virtual-dma failed\n"); 740Sstevel@tonic-gate 750Sstevel@tonic-gate kmem_free(mmu_p, sizeof (px_mmu_t)); 760Sstevel@tonic-gate px_p->px_mmu_p = NULL; 770Sstevel@tonic-gate 780Sstevel@tonic-gate return (DDI_FAILURE); 790Sstevel@tonic-gate } 800Sstevel@tonic-gate 810Sstevel@tonic-gate mmu_p->mmu_dvma_base = dvma_prop->dvma_base; 820Sstevel@tonic-gate mmu_p->mmu_dvma_end = dvma_prop->dvma_base + 830Sstevel@tonic-gate (dvma_prop->dvma_len - 1); 84343Skrishnae tsb_entries = MMU_BTOP(dvma_prop->dvma_len); 850Sstevel@tonic-gate 860Sstevel@tonic-gate kmem_free(dvma_prop, dvma_prop_len); 870Sstevel@tonic-gate 880Sstevel@tonic-gate /* 890Sstevel@tonic-gate * Setup base and bounds for DVMA and bypass mappings. 900Sstevel@tonic-gate */ 910Sstevel@tonic-gate mmu_p->mmu_dvma_cache_locks = 920Sstevel@tonic-gate kmem_zalloc(px_dvma_page_cache_entries, KM_SLEEP); 930Sstevel@tonic-gate 940Sstevel@tonic-gate mmu_p->dvma_base_pg = MMU_BTOP(mmu_p->mmu_dvma_base); 950Sstevel@tonic-gate mmu_p->mmu_dvma_reserve = tsb_entries >> 1; 960Sstevel@tonic-gate mmu_p->dvma_end_pg = MMU_BTOP(mmu_p->mmu_dvma_end); 970Sstevel@tonic-gate 980Sstevel@tonic-gate /* 990Sstevel@tonic-gate * Create a virtual memory map for dvma address space. 1000Sstevel@tonic-gate * Reserve 'size' bytes of low dvma space for fast track cache. 1010Sstevel@tonic-gate */ 1020Sstevel@tonic-gate (void) snprintf(map_name, sizeof (map_name), "%s%d_dvma", 1030Sstevel@tonic-gate ddi_driver_name(dip), ddi_get_instance(dip)); 1040Sstevel@tonic-gate 1050Sstevel@tonic-gate cache_size = MMU_PTOB(px_dvma_page_cache_entries * 1060Sstevel@tonic-gate px_dvma_page_cache_clustsz); 1070Sstevel@tonic-gate mmu_p->mmu_dvma_fast_end = mmu_p->mmu_dvma_base + 1080Sstevel@tonic-gate cache_size - 1; 1090Sstevel@tonic-gate 1100Sstevel@tonic-gate mmu_p->mmu_dvma_map = vmem_create(map_name, 1110Sstevel@tonic-gate (void *)(mmu_p->mmu_dvma_fast_end + 1), 112343Skrishnae MMU_PTOB(tsb_entries) - cache_size, MMU_PAGE_SIZE, 1130Sstevel@tonic-gate NULL, NULL, NULL, MMU_PAGE_SIZE, VM_SLEEP); 1140Sstevel@tonic-gate 1150Sstevel@tonic-gate mutex_init(&mmu_p->dvma_debug_lock, NULL, MUTEX_DRIVER, NULL); 1160Sstevel@tonic-gate 1170Sstevel@tonic-gate base_pg_index = MMU_BTOP(mmu_p->mmu_dvma_end) - tsb_entries + 1; 1180Sstevel@tonic-gate 1190Sstevel@tonic-gate for (i = 0; i < tsb_entries; i++) { 1200Sstevel@tonic-gate r_addr_t ra = 0; 1210Sstevel@tonic-gate io_attributes_t attr; 1220Sstevel@tonic-gate caddr_t va; 1230Sstevel@tonic-gate 1240Sstevel@tonic-gate if (px_lib_iommu_getmap(px_p->px_dip, PCI_TSBID(0, i), 1250Sstevel@tonic-gate &attr, &ra) == DDI_SUCCESS) { 1260Sstevel@tonic-gate va = (caddr_t)(MMU_PTOB(base_pg_index + i)); 1270Sstevel@tonic-gate (void) vmem_xalloc(mmu_p->mmu_dvma_map, MMU_PAGE_SIZE, 1280Sstevel@tonic-gate MMU_PAGE_SIZE, 0, 0, va, va + MMU_PAGE_SIZE, 1290Sstevel@tonic-gate VM_NOSLEEP | VM_BESTFIT | VM_PANIC); 1300Sstevel@tonic-gate } 1310Sstevel@tonic-gate } 1320Sstevel@tonic-gate 1330Sstevel@tonic-gate return (DDI_SUCCESS); 1340Sstevel@tonic-gate } 1350Sstevel@tonic-gate 1360Sstevel@tonic-gate void 1370Sstevel@tonic-gate px_mmu_detach(px_t *px_p) 1380Sstevel@tonic-gate { 1390Sstevel@tonic-gate px_mmu_t *mmu_p = px_p->px_mmu_p; 1400Sstevel@tonic-gate 1410Sstevel@tonic-gate /* 1420Sstevel@tonic-gate * Free the dvma resource map. 1430Sstevel@tonic-gate */ 1440Sstevel@tonic-gate vmem_destroy(mmu_p->mmu_dvma_map); 1450Sstevel@tonic-gate 1460Sstevel@tonic-gate kmem_free(mmu_p->mmu_dvma_cache_locks, 1470Sstevel@tonic-gate px_dvma_page_cache_entries); 1480Sstevel@tonic-gate 149909Segillett if (PX_DVMA_DBG_ON(mmu_p)) 1500Sstevel@tonic-gate px_dvma_debug_fini(mmu_p); 1510Sstevel@tonic-gate 1520Sstevel@tonic-gate mutex_destroy(&mmu_p->dvma_debug_lock); 1530Sstevel@tonic-gate 1540Sstevel@tonic-gate /* 1550Sstevel@tonic-gate * Free the mmu state structure. 1560Sstevel@tonic-gate */ 1570Sstevel@tonic-gate kmem_free(mmu_p, sizeof (px_mmu_t)); 1580Sstevel@tonic-gate px_p->px_mmu_p = NULL; 1590Sstevel@tonic-gate } 1600Sstevel@tonic-gate 161671Skrishnae int 1620Sstevel@tonic-gate px_mmu_map_pages(px_mmu_t *mmu_p, ddi_dma_impl_t *mp, px_dvma_addr_t dvma_pg, 1630Sstevel@tonic-gate size_t npages, size_t pfn_index) 1640Sstevel@tonic-gate { 1650Sstevel@tonic-gate dev_info_t *dip = mmu_p->mmu_px_p->px_dip; 1660Sstevel@tonic-gate px_dvma_addr_t pg_index = MMU_PAGE_INDEX(mmu_p, dvma_pg); 1670Sstevel@tonic-gate io_attributes_t attr = PX_GET_MP_TTE(mp->dmai_tte); 1680Sstevel@tonic-gate 1690Sstevel@tonic-gate ASSERT(npages <= mp->dmai_ndvmapages); 1701501Sgovinda DBG(DBG_MAP_WIN, dip, "px_mmu_map_pages:%x+%x=%x " 1711501Sgovinda "npages=0x%x pfn_index=0x%x\n", (uint_t)mmu_p->dvma_base_pg, 1721501Sgovinda (uint_t)pg_index, dvma_pg, (uint_t)npages, (uint_t)pfn_index); 1730Sstevel@tonic-gate 174*3156Sgirish if (px_lib_iommu_map(dip, PCI_TSBID(0, pg_index), npages, 175*3156Sgirish PX_ADD_ATTR_EXTNS(attr, mp->dmai_bdf), (void *)mp, pfn_index, 176*3156Sgirish MMU_MAP_PFN) != DDI_SUCCESS) { 1771501Sgovinda DBG(DBG_MAP_WIN, dip, "px_mmu_map_pages: " 1781501Sgovinda "px_lib_iommu_map failed\n"); 1790Sstevel@tonic-gate 1801501Sgovinda return (DDI_FAILURE); 1810Sstevel@tonic-gate } 1820Sstevel@tonic-gate 1831501Sgovinda if (!PX_MAP_BUFZONE(mp)) 1841501Sgovinda goto done; 1851501Sgovinda 1861501Sgovinda DBG(DBG_MAP_WIN, dip, "px_mmu_map_pages: redzone pg=%x\n", 1871501Sgovinda pg_index + npages); 1881501Sgovinda 1891501Sgovinda ASSERT(PX_HAS_REDZONE(mp)); 1901501Sgovinda 191*3156Sgirish if (px_lib_iommu_map(dip, PCI_TSBID(0, pg_index + npages), 1, 192*3156Sgirish PX_ADD_ATTR_EXTNS(attr, mp->dmai_bdf), (void *)mp, 193*3156Sgirish pfn_index + npages - 1, MMU_MAP_PFN) != DDI_SUCCESS) { 1941501Sgovinda DBG(DBG_MAP_WIN, dip, "px_mmu_map_pages: mapping " 1951501Sgovinda "REDZONE page failed\n"); 1961501Sgovinda 1971501Sgovinda (void) px_lib_iommu_demap(dip, PCI_TSBID(0, pg_index), npages); 1981501Sgovinda return (DDI_FAILURE); 1991501Sgovinda } 2001501Sgovinda 2011501Sgovinda done: 202909Segillett if (PX_DVMA_DBG_ON(mmu_p)) 2030Sstevel@tonic-gate px_dvma_alloc_debug(mmu_p, (char *)mp->dmai_mapping, 2040Sstevel@tonic-gate mp->dmai_size, mp); 2050Sstevel@tonic-gate 2061501Sgovinda return (DDI_SUCCESS); 2070Sstevel@tonic-gate } 2080Sstevel@tonic-gate 2090Sstevel@tonic-gate void 2101501Sgovinda px_mmu_unmap_pages(px_mmu_t *mmu_p, ddi_dma_impl_t *mp, px_dvma_addr_t dvma_pg, 2111501Sgovinda uint_t npages) 2120Sstevel@tonic-gate { 2130Sstevel@tonic-gate px_dvma_addr_t pg_index = MMU_PAGE_INDEX(mmu_p, dvma_pg); 2140Sstevel@tonic-gate 2150Sstevel@tonic-gate DBG(DBG_UNMAP_WIN, mmu_p->mmu_px_p->px_dip, 2161501Sgovinda "px_mmu_unmap_pages:%x+%x=%x npages=0x%x\n", 2171501Sgovinda (uint_t)mmu_p->dvma_base_pg, (uint_t)pg_index, dvma_pg, 2181501Sgovinda (uint_t)npages); 2190Sstevel@tonic-gate 2200Sstevel@tonic-gate (void) px_lib_iommu_demap(mmu_p->mmu_px_p->px_dip, 2210Sstevel@tonic-gate PCI_TSBID(0, pg_index), npages); 2221501Sgovinda 2231501Sgovinda if (!PX_MAP_BUFZONE(mp)) 2241501Sgovinda return; 2251501Sgovinda 2261501Sgovinda DBG(DBG_MAP_WIN, mmu_p->mmu_px_p->px_dip, "px_mmu_unmap_pages: " 2271501Sgovinda "redzone pg=%x\n", pg_index + npages); 2281501Sgovinda 2291501Sgovinda ASSERT(PX_HAS_REDZONE(mp)); 2301501Sgovinda 2311501Sgovinda (void) px_lib_iommu_demap(mmu_p->mmu_px_p->px_dip, 2321501Sgovinda PCI_TSBID(0, pg_index + npages), 1); 2330Sstevel@tonic-gate } 2340Sstevel@tonic-gate 2350Sstevel@tonic-gate /* 2360Sstevel@tonic-gate * px_mmu_map_window - map a dvma window into the mmu 2370Sstevel@tonic-gate * used by: px_dma_win(), px_dma_ctlops() - DDI_DMA_MOVWIN, DDI_DMA_NEXTWIN 2380Sstevel@tonic-gate * return value: none 2390Sstevel@tonic-gate */ 2400Sstevel@tonic-gate /*ARGSUSED*/ 2410Sstevel@tonic-gate int 2420Sstevel@tonic-gate px_mmu_map_window(px_mmu_t *mmu_p, ddi_dma_impl_t *mp, px_window_t win_no) 2430Sstevel@tonic-gate { 2440Sstevel@tonic-gate uint32_t obj_pg0_off = mp->dmai_roffset; 2450Sstevel@tonic-gate uint32_t win_pg0_off = win_no ? 0 : obj_pg0_off; 2460Sstevel@tonic-gate size_t win_size = mp->dmai_winsize; 2470Sstevel@tonic-gate size_t pfn_index = win_size * win_no; /* temp value */ 2480Sstevel@tonic-gate size_t obj_off = win_no ? pfn_index - obj_pg0_off : 0; /* xferred sz */ 2490Sstevel@tonic-gate px_dvma_addr_t dvma_pg = MMU_BTOP(mp->dmai_mapping); 2500Sstevel@tonic-gate size_t res_size = mp->dmai_object.dmao_size - obj_off + win_pg0_off; 2510Sstevel@tonic-gate int ret = DDI_SUCCESS; 2520Sstevel@tonic-gate 2530Sstevel@tonic-gate ASSERT(!(win_size & MMU_PAGE_OFFSET)); 2540Sstevel@tonic-gate if (win_no >= mp->dmai_nwin) 2550Sstevel@tonic-gate return (ret); 2560Sstevel@tonic-gate if (res_size < win_size) /* last window */ 2570Sstevel@tonic-gate win_size = res_size; /* mp->dmai_winsize unchanged */ 2580Sstevel@tonic-gate 2590Sstevel@tonic-gate mp->dmai_mapping = MMU_PTOB(dvma_pg) | win_pg0_off; 2600Sstevel@tonic-gate mp->dmai_size = win_size - win_pg0_off; /* cur win xferrable size */ 2610Sstevel@tonic-gate mp->dmai_offset = obj_off; /* win offset into object */ 2620Sstevel@tonic-gate pfn_index = MMU_BTOP(pfn_index); /* index into pfnlist */ 2630Sstevel@tonic-gate ret = px_mmu_map_pages(mmu_p, mp, dvma_pg, MMU_BTOPR(win_size), 2640Sstevel@tonic-gate pfn_index); 2650Sstevel@tonic-gate 2660Sstevel@tonic-gate return (ret); 2670Sstevel@tonic-gate } 2680Sstevel@tonic-gate 2690Sstevel@tonic-gate /* 2700Sstevel@tonic-gate * px_mmu_unmap_window 2710Sstevel@tonic-gate * This routine is called to break down the mmu mappings to a dvma window. 2720Sstevel@tonic-gate * Non partial mappings are viewed as single window mapping. 2730Sstevel@tonic-gate * used by: px_dma_unbindhdl(), px_dma_window(), 2740Sstevel@tonic-gate * and px_dma_ctlops() - DDI_DMA_FREE, DDI_DMA_MOVWIN, DDI_DMA_NEXTWIN 2750Sstevel@tonic-gate * return value: none 2760Sstevel@tonic-gate */ 2770Sstevel@tonic-gate /*ARGSUSED*/ 2780Sstevel@tonic-gate void 2790Sstevel@tonic-gate px_mmu_unmap_window(px_mmu_t *mmu_p, ddi_dma_impl_t *mp) 2800Sstevel@tonic-gate { 2810Sstevel@tonic-gate px_dvma_addr_t dvma_pg = MMU_BTOP(mp->dmai_mapping); 2820Sstevel@tonic-gate uint_t npages = MMU_BTOP(mp->dmai_winsize); 2830Sstevel@tonic-gate 2841501Sgovinda px_mmu_unmap_pages(mmu_p, mp, dvma_pg, npages); 2850Sstevel@tonic-gate 286909Segillett if (PX_DVMA_DBG_ON(mmu_p)) 2870Sstevel@tonic-gate px_dvma_free_debug(mmu_p, (char *)mp->dmai_mapping, 2880Sstevel@tonic-gate mp->dmai_size, mp); 2890Sstevel@tonic-gate } 2900Sstevel@tonic-gate 2910Sstevel@tonic-gate 2920Sstevel@tonic-gate #if 0 2930Sstevel@tonic-gate /* 2940Sstevel@tonic-gate * The following table is for reference only. It denotes the 2950Sstevel@tonic-gate * the TSB table size measured in number of 8 byte entries. 2960Sstevel@tonic-gate * It is represented by bits 3:0 in the MMU TSB CTRL REG. 2970Sstevel@tonic-gate */ 2980Sstevel@tonic-gate static int px_mmu_tsb_sizes[] = { 2990Sstevel@tonic-gate 0x0, /* 1K */ 3000Sstevel@tonic-gate 0x1, /* 2K */ 3010Sstevel@tonic-gate 0x2, /* 4K */ 3020Sstevel@tonic-gate 0x3, /* 8K */ 3030Sstevel@tonic-gate 0x4, /* 16K */ 3040Sstevel@tonic-gate 0x5, /* 32K */ 3050Sstevel@tonic-gate 0x6, /* 64K */ 3060Sstevel@tonic-gate 0x7, /* 128K */ 3070Sstevel@tonic-gate 0x8 /* 256K */ 3080Sstevel@tonic-gate }; 3090Sstevel@tonic-gate #endif 3100Sstevel@tonic-gate 3110Sstevel@tonic-gate static char *px_mmu_errsts[] = { 3120Sstevel@tonic-gate "Protection Error", "Invalid Error", "Timeout", "ECC Error(UE)" 3130Sstevel@tonic-gate }; 3140Sstevel@tonic-gate 3150Sstevel@tonic-gate /*ARGSUSED*/ 3160Sstevel@tonic-gate static int 3170Sstevel@tonic-gate px_log_mmu_err(px_t *px_p) 3180Sstevel@tonic-gate { 3190Sstevel@tonic-gate /* 3200Sstevel@tonic-gate * Place holder, the correct eror bits need tobe logged. 3210Sstevel@tonic-gate */ 3220Sstevel@tonic-gate return (0); 3230Sstevel@tonic-gate } 324