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*12619Sandrew.rutz@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 * PX mmu initialization and configuration
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/vmem.h>
350Sstevel@tonic-gate #include <sys/machsystm.h> /* lddphys() */
360Sstevel@tonic-gate #include <sys/iommutsb.h>
370Sstevel@tonic-gate #include "px_obj.h"
380Sstevel@tonic-gate
390Sstevel@tonic-gate int
px_mmu_attach(px_t * px_p)400Sstevel@tonic-gate px_mmu_attach(px_t *px_p)
410Sstevel@tonic-gate {
420Sstevel@tonic-gate dev_info_t *dip = px_p->px_dip;
430Sstevel@tonic-gate px_mmu_t *mmu_p;
445328Sdanice uint32_t tsb_i = 0;
450Sstevel@tonic-gate char map_name[32];
460Sstevel@tonic-gate px_dvma_range_prop_t *dvma_prop;
470Sstevel@tonic-gate int dvma_prop_len;
480Sstevel@tonic-gate uint32_t cache_size, tsb_entries;
490Sstevel@tonic-gate
500Sstevel@tonic-gate /*
510Sstevel@tonic-gate * Allocate mmu state structure and link it to the
520Sstevel@tonic-gate * px state structure.
530Sstevel@tonic-gate */
540Sstevel@tonic-gate mmu_p = kmem_zalloc(sizeof (px_mmu_t), KM_SLEEP);
550Sstevel@tonic-gate if (mmu_p == NULL)
560Sstevel@tonic-gate return (DDI_FAILURE);
570Sstevel@tonic-gate
580Sstevel@tonic-gate px_p->px_mmu_p = mmu_p;
590Sstevel@tonic-gate mmu_p->mmu_px_p = px_p;
600Sstevel@tonic-gate mmu_p->mmu_inst = ddi_get_instance(dip);
610Sstevel@tonic-gate
620Sstevel@tonic-gate /*
630Sstevel@tonic-gate * Check for "virtual-dma" property that specifies
640Sstevel@tonic-gate * the DVMA range.
650Sstevel@tonic-gate */
660Sstevel@tonic-gate if (ddi_getlongprop(DDI_DEV_T_ANY, dip, DDI_PROP_DONTPASS,
670Sstevel@tonic-gate "virtual-dma", (caddr_t)&dvma_prop, &dvma_prop_len) !=
680Sstevel@tonic-gate DDI_PROP_SUCCESS) {
690Sstevel@tonic-gate
700Sstevel@tonic-gate DBG(DBG_ATTACH, dip, "Getting virtual-dma failed\n");
710Sstevel@tonic-gate
720Sstevel@tonic-gate kmem_free(mmu_p, sizeof (px_mmu_t));
730Sstevel@tonic-gate px_p->px_mmu_p = NULL;
740Sstevel@tonic-gate
750Sstevel@tonic-gate return (DDI_FAILURE);
760Sstevel@tonic-gate }
770Sstevel@tonic-gate
780Sstevel@tonic-gate mmu_p->mmu_dvma_base = dvma_prop->dvma_base;
790Sstevel@tonic-gate mmu_p->mmu_dvma_end = dvma_prop->dvma_base +
800Sstevel@tonic-gate (dvma_prop->dvma_len - 1);
81343Skrishnae tsb_entries = MMU_BTOP(dvma_prop->dvma_len);
820Sstevel@tonic-gate
830Sstevel@tonic-gate kmem_free(dvma_prop, dvma_prop_len);
840Sstevel@tonic-gate
850Sstevel@tonic-gate /*
860Sstevel@tonic-gate * Setup base and bounds for DVMA and bypass mappings.
870Sstevel@tonic-gate */
880Sstevel@tonic-gate mmu_p->mmu_dvma_cache_locks =
897124Sanbui kmem_zalloc(px_dvma_page_cache_entries, KM_SLEEP);
900Sstevel@tonic-gate
910Sstevel@tonic-gate mmu_p->dvma_base_pg = MMU_BTOP(mmu_p->mmu_dvma_base);
920Sstevel@tonic-gate mmu_p->mmu_dvma_reserve = tsb_entries >> 1;
930Sstevel@tonic-gate mmu_p->dvma_end_pg = MMU_BTOP(mmu_p->mmu_dvma_end);
940Sstevel@tonic-gate
950Sstevel@tonic-gate /*
960Sstevel@tonic-gate * Create a virtual memory map for dvma address space.
970Sstevel@tonic-gate * Reserve 'size' bytes of low dvma space for fast track cache.
980Sstevel@tonic-gate */
990Sstevel@tonic-gate (void) snprintf(map_name, sizeof (map_name), "%s%d_dvma",
1000Sstevel@tonic-gate ddi_driver_name(dip), ddi_get_instance(dip));
1010Sstevel@tonic-gate
1020Sstevel@tonic-gate cache_size = MMU_PTOB(px_dvma_page_cache_entries *
1037124Sanbui px_dvma_page_cache_clustsz);
1040Sstevel@tonic-gate mmu_p->mmu_dvma_fast_end = mmu_p->mmu_dvma_base +
1057124Sanbui cache_size - 1;
1060Sstevel@tonic-gate
1070Sstevel@tonic-gate mmu_p->mmu_dvma_map = vmem_create(map_name,
1080Sstevel@tonic-gate (void *)(mmu_p->mmu_dvma_fast_end + 1),
109343Skrishnae MMU_PTOB(tsb_entries) - cache_size, MMU_PAGE_SIZE,
1100Sstevel@tonic-gate NULL, NULL, NULL, MMU_PAGE_SIZE, VM_SLEEP);
1110Sstevel@tonic-gate
1120Sstevel@tonic-gate mutex_init(&mmu_p->dvma_debug_lock, NULL, MUTEX_DRIVER, NULL);
1130Sstevel@tonic-gate
1145328Sdanice for (tsb_i = 0; tsb_i < tsb_entries; tsb_i++) {
1150Sstevel@tonic-gate r_addr_t ra = 0;
1160Sstevel@tonic-gate io_attributes_t attr;
1170Sstevel@tonic-gate caddr_t va;
1180Sstevel@tonic-gate
1195328Sdanice if (px_lib_iommu_getmap(px_p->px_dip, PCI_TSBID(0, tsb_i),
1205328Sdanice &attr, &ra) != DDI_SUCCESS)
1215328Sdanice continue;
1225328Sdanice
1235328Sdanice va = (caddr_t)(MMU_PTOB(mmu_p->dvma_base_pg + tsb_i));
1245328Sdanice
1255328Sdanice if (va <= (caddr_t)mmu_p->mmu_dvma_fast_end) {
1265328Sdanice uint32_t cache_i;
1275328Sdanice
1285328Sdanice /*
1295328Sdanice * the va is within the *fast* dvma range; therefore,
1305328Sdanice * lock its fast dvma page cache cluster in order to
1315328Sdanice * both preserve the TTE and prevent the use of this
1325328Sdanice * fast dvma page cache cluster by px_dvma_map_fast().
1335328Sdanice * the lock value 0xFF comes from ldstub().
1345328Sdanice */
1355328Sdanice cache_i = tsb_i / px_dvma_page_cache_clustsz;
1365328Sdanice ASSERT(cache_i < px_dvma_page_cache_entries);
1375328Sdanice mmu_p->mmu_dvma_cache_locks[cache_i] = 0xFF;
1385328Sdanice } else {
1390Sstevel@tonic-gate (void) vmem_xalloc(mmu_p->mmu_dvma_map, MMU_PAGE_SIZE,
1400Sstevel@tonic-gate MMU_PAGE_SIZE, 0, 0, va, va + MMU_PAGE_SIZE,
1410Sstevel@tonic-gate VM_NOSLEEP | VM_BESTFIT | VM_PANIC);
1420Sstevel@tonic-gate }
1430Sstevel@tonic-gate }
1440Sstevel@tonic-gate
1450Sstevel@tonic-gate return (DDI_SUCCESS);
1460Sstevel@tonic-gate }
1470Sstevel@tonic-gate
1480Sstevel@tonic-gate void
px_mmu_detach(px_t * px_p)1490Sstevel@tonic-gate px_mmu_detach(px_t *px_p)
1500Sstevel@tonic-gate {
1510Sstevel@tonic-gate px_mmu_t *mmu_p = px_p->px_mmu_p;
1520Sstevel@tonic-gate
153*12619Sandrew.rutz@sun.com (void) px_lib_iommu_detach(px_p);
154*12619Sandrew.rutz@sun.com
1550Sstevel@tonic-gate /*
1560Sstevel@tonic-gate * Free the dvma resource map.
1570Sstevel@tonic-gate */
1580Sstevel@tonic-gate vmem_destroy(mmu_p->mmu_dvma_map);
1590Sstevel@tonic-gate
1600Sstevel@tonic-gate kmem_free(mmu_p->mmu_dvma_cache_locks,
1610Sstevel@tonic-gate px_dvma_page_cache_entries);
1620Sstevel@tonic-gate
163909Segillett if (PX_DVMA_DBG_ON(mmu_p))
1640Sstevel@tonic-gate px_dvma_debug_fini(mmu_p);
1650Sstevel@tonic-gate
1660Sstevel@tonic-gate mutex_destroy(&mmu_p->dvma_debug_lock);
1670Sstevel@tonic-gate
1680Sstevel@tonic-gate /*
1690Sstevel@tonic-gate * Free the mmu state structure.
1700Sstevel@tonic-gate */
1710Sstevel@tonic-gate kmem_free(mmu_p, sizeof (px_mmu_t));
1720Sstevel@tonic-gate px_p->px_mmu_p = NULL;
1730Sstevel@tonic-gate }
1740Sstevel@tonic-gate
175671Skrishnae int
px_mmu_map_pages(px_mmu_t * mmu_p,ddi_dma_impl_t * mp,px_dvma_addr_t dvma_pg,size_t npages,size_t pfn_index)1760Sstevel@tonic-gate px_mmu_map_pages(px_mmu_t *mmu_p, ddi_dma_impl_t *mp, px_dvma_addr_t dvma_pg,
1770Sstevel@tonic-gate size_t npages, size_t pfn_index)
1780Sstevel@tonic-gate {
1790Sstevel@tonic-gate dev_info_t *dip = mmu_p->mmu_px_p->px_dip;
1800Sstevel@tonic-gate px_dvma_addr_t pg_index = MMU_PAGE_INDEX(mmu_p, dvma_pg);
1810Sstevel@tonic-gate io_attributes_t attr = PX_GET_MP_TTE(mp->dmai_tte);
1820Sstevel@tonic-gate
1830Sstevel@tonic-gate ASSERT(npages <= mp->dmai_ndvmapages);
1841501Sgovinda DBG(DBG_MAP_WIN, dip, "px_mmu_map_pages:%x+%x=%x "
1851501Sgovinda "npages=0x%x pfn_index=0x%x\n", (uint_t)mmu_p->dvma_base_pg,
1861501Sgovinda (uint_t)pg_index, dvma_pg, (uint_t)npages, (uint_t)pfn_index);
1870Sstevel@tonic-gate
1883156Sgirish if (px_lib_iommu_map(dip, PCI_TSBID(0, pg_index), npages,
1893156Sgirish PX_ADD_ATTR_EXTNS(attr, mp->dmai_bdf), (void *)mp, pfn_index,
1903156Sgirish MMU_MAP_PFN) != DDI_SUCCESS) {
1911501Sgovinda DBG(DBG_MAP_WIN, dip, "px_mmu_map_pages: "
1921501Sgovinda "px_lib_iommu_map failed\n");
1930Sstevel@tonic-gate
1941501Sgovinda return (DDI_FAILURE);
1950Sstevel@tonic-gate }
1960Sstevel@tonic-gate
1971501Sgovinda if (!PX_MAP_BUFZONE(mp))
1981501Sgovinda goto done;
1991501Sgovinda
2001501Sgovinda DBG(DBG_MAP_WIN, dip, "px_mmu_map_pages: redzone pg=%x\n",
2011501Sgovinda pg_index + npages);
2021501Sgovinda
2031501Sgovinda ASSERT(PX_HAS_REDZONE(mp));
2041501Sgovinda
2053156Sgirish if (px_lib_iommu_map(dip, PCI_TSBID(0, pg_index + npages), 1,
2063156Sgirish PX_ADD_ATTR_EXTNS(attr, mp->dmai_bdf), (void *)mp,
2073156Sgirish pfn_index + npages - 1, MMU_MAP_PFN) != DDI_SUCCESS) {
2081501Sgovinda DBG(DBG_MAP_WIN, dip, "px_mmu_map_pages: mapping "
2091501Sgovinda "REDZONE page failed\n");
2101501Sgovinda
2117124Sanbui if (px_lib_iommu_demap(dip, PCI_TSBID(0, pg_index), npages)
2127124Sanbui != DDI_SUCCESS) {
2137124Sanbui DBG(DBG_MAP_WIN, dip, "px_lib_iommu_demap: failed\n");
2147124Sanbui }
2151501Sgovinda return (DDI_FAILURE);
2161501Sgovinda }
2171501Sgovinda
2181501Sgovinda done:
219909Segillett if (PX_DVMA_DBG_ON(mmu_p))
2200Sstevel@tonic-gate px_dvma_alloc_debug(mmu_p, (char *)mp->dmai_mapping,
2210Sstevel@tonic-gate mp->dmai_size, mp);
2220Sstevel@tonic-gate
2231501Sgovinda return (DDI_SUCCESS);
2240Sstevel@tonic-gate }
2250Sstevel@tonic-gate
2260Sstevel@tonic-gate void
px_mmu_unmap_pages(px_mmu_t * mmu_p,ddi_dma_impl_t * mp,px_dvma_addr_t dvma_pg,uint_t npages)2271501Sgovinda px_mmu_unmap_pages(px_mmu_t *mmu_p, ddi_dma_impl_t *mp, px_dvma_addr_t dvma_pg,
2281501Sgovinda uint_t npages)
2290Sstevel@tonic-gate {
2300Sstevel@tonic-gate px_dvma_addr_t pg_index = MMU_PAGE_INDEX(mmu_p, dvma_pg);
2310Sstevel@tonic-gate
2320Sstevel@tonic-gate DBG(DBG_UNMAP_WIN, mmu_p->mmu_px_p->px_dip,
2331501Sgovinda "px_mmu_unmap_pages:%x+%x=%x npages=0x%x\n",
2341501Sgovinda (uint_t)mmu_p->dvma_base_pg, (uint_t)pg_index, dvma_pg,
2351501Sgovinda (uint_t)npages);
2360Sstevel@tonic-gate
2377124Sanbui if (px_lib_iommu_demap(mmu_p->mmu_px_p->px_dip,
2387124Sanbui PCI_TSBID(0, pg_index), npages) != DDI_SUCCESS) {
2397124Sanbui DBG(DBG_UNMAP_WIN, mmu_p->mmu_px_p->px_dip,
2407124Sanbui "px_lib_iommu_demap: failed\n");
2417124Sanbui }
2421501Sgovinda
2431501Sgovinda if (!PX_MAP_BUFZONE(mp))
2441501Sgovinda return;
2451501Sgovinda
2467124Sanbui DBG(DBG_UNMAP_WIN, mmu_p->mmu_px_p->px_dip, "px_mmu_unmap_pages: "
2471501Sgovinda "redzone pg=%x\n", pg_index + npages);
2481501Sgovinda
2491501Sgovinda ASSERT(PX_HAS_REDZONE(mp));
2501501Sgovinda
2517124Sanbui if (px_lib_iommu_demap(mmu_p->mmu_px_p->px_dip,
2527124Sanbui PCI_TSBID(0, pg_index + npages), 1) != DDI_SUCCESS) {
2537124Sanbui DBG(DBG_UNMAP_WIN, mmu_p->mmu_px_p->px_dip,
2547124Sanbui "px_lib_iommu_demap: failed\n");
2557124Sanbui }
2560Sstevel@tonic-gate }
2570Sstevel@tonic-gate
2580Sstevel@tonic-gate /*
2590Sstevel@tonic-gate * px_mmu_map_window - map a dvma window into the mmu
2600Sstevel@tonic-gate * used by: px_dma_win(), px_dma_ctlops() - DDI_DMA_MOVWIN, DDI_DMA_NEXTWIN
2610Sstevel@tonic-gate * return value: none
2620Sstevel@tonic-gate */
2630Sstevel@tonic-gate /*ARGSUSED*/
2640Sstevel@tonic-gate int
px_mmu_map_window(px_mmu_t * mmu_p,ddi_dma_impl_t * mp,px_window_t win_no)2650Sstevel@tonic-gate px_mmu_map_window(px_mmu_t *mmu_p, ddi_dma_impl_t *mp, px_window_t win_no)
2660Sstevel@tonic-gate {
2670Sstevel@tonic-gate uint32_t obj_pg0_off = mp->dmai_roffset;
2680Sstevel@tonic-gate uint32_t win_pg0_off = win_no ? 0 : obj_pg0_off;
2690Sstevel@tonic-gate size_t win_size = mp->dmai_winsize;
2700Sstevel@tonic-gate size_t pfn_index = win_size * win_no; /* temp value */
2710Sstevel@tonic-gate size_t obj_off = win_no ? pfn_index - obj_pg0_off : 0; /* xferred sz */
2720Sstevel@tonic-gate px_dvma_addr_t dvma_pg = MMU_BTOP(mp->dmai_mapping);
2730Sstevel@tonic-gate size_t res_size = mp->dmai_object.dmao_size - obj_off + win_pg0_off;
2740Sstevel@tonic-gate int ret = DDI_SUCCESS;
2750Sstevel@tonic-gate
2760Sstevel@tonic-gate ASSERT(!(win_size & MMU_PAGE_OFFSET));
2770Sstevel@tonic-gate if (win_no >= mp->dmai_nwin)
2780Sstevel@tonic-gate return (ret);
2790Sstevel@tonic-gate if (res_size < win_size) /* last window */
2800Sstevel@tonic-gate win_size = res_size; /* mp->dmai_winsize unchanged */
2810Sstevel@tonic-gate
2820Sstevel@tonic-gate mp->dmai_mapping = MMU_PTOB(dvma_pg) | win_pg0_off;
2830Sstevel@tonic-gate mp->dmai_size = win_size - win_pg0_off; /* cur win xferrable size */
2840Sstevel@tonic-gate mp->dmai_offset = obj_off; /* win offset into object */
2850Sstevel@tonic-gate pfn_index = MMU_BTOP(pfn_index); /* index into pfnlist */
2860Sstevel@tonic-gate ret = px_mmu_map_pages(mmu_p, mp, dvma_pg, MMU_BTOPR(win_size),
2870Sstevel@tonic-gate pfn_index);
2880Sstevel@tonic-gate
2890Sstevel@tonic-gate return (ret);
2900Sstevel@tonic-gate }
2910Sstevel@tonic-gate
2920Sstevel@tonic-gate /*
2930Sstevel@tonic-gate * px_mmu_unmap_window
2940Sstevel@tonic-gate * This routine is called to break down the mmu mappings to a dvma window.
2950Sstevel@tonic-gate * Non partial mappings are viewed as single window mapping.
2960Sstevel@tonic-gate * used by: px_dma_unbindhdl(), px_dma_window(),
2970Sstevel@tonic-gate * and px_dma_ctlops() - DDI_DMA_FREE, DDI_DMA_MOVWIN, DDI_DMA_NEXTWIN
2980Sstevel@tonic-gate * return value: none
2990Sstevel@tonic-gate */
3000Sstevel@tonic-gate /*ARGSUSED*/
3010Sstevel@tonic-gate void
px_mmu_unmap_window(px_mmu_t * mmu_p,ddi_dma_impl_t * mp)3020Sstevel@tonic-gate px_mmu_unmap_window(px_mmu_t *mmu_p, ddi_dma_impl_t *mp)
3030Sstevel@tonic-gate {
3040Sstevel@tonic-gate px_dvma_addr_t dvma_pg = MMU_BTOP(mp->dmai_mapping);
3050Sstevel@tonic-gate uint_t npages = MMU_BTOP(mp->dmai_winsize);
3060Sstevel@tonic-gate
3071501Sgovinda px_mmu_unmap_pages(mmu_p, mp, dvma_pg, npages);
3080Sstevel@tonic-gate
309909Segillett if (PX_DVMA_DBG_ON(mmu_p))
3100Sstevel@tonic-gate px_dvma_free_debug(mmu_p, (char *)mp->dmai_mapping,
3110Sstevel@tonic-gate mp->dmai_size, mp);
3120Sstevel@tonic-gate }
3130Sstevel@tonic-gate
3140Sstevel@tonic-gate
3150Sstevel@tonic-gate #if 0
3160Sstevel@tonic-gate /*
3170Sstevel@tonic-gate * The following table is for reference only. It denotes the
3180Sstevel@tonic-gate * the TSB table size measured in number of 8 byte entries.
3190Sstevel@tonic-gate * It is represented by bits 3:0 in the MMU TSB CTRL REG.
3200Sstevel@tonic-gate */
3210Sstevel@tonic-gate static int px_mmu_tsb_sizes[] = {
3220Sstevel@tonic-gate 0x0, /* 1K */
3230Sstevel@tonic-gate 0x1, /* 2K */
3240Sstevel@tonic-gate 0x2, /* 4K */
3250Sstevel@tonic-gate 0x3, /* 8K */
3260Sstevel@tonic-gate 0x4, /* 16K */
3270Sstevel@tonic-gate 0x5, /* 32K */
3280Sstevel@tonic-gate 0x6, /* 64K */
3290Sstevel@tonic-gate 0x7, /* 128K */
3300Sstevel@tonic-gate 0x8 /* 256K */
3310Sstevel@tonic-gate };
3320Sstevel@tonic-gate #endif
3330Sstevel@tonic-gate
3340Sstevel@tonic-gate static char *px_mmu_errsts[] = {
3350Sstevel@tonic-gate "Protection Error", "Invalid Error", "Timeout", "ECC Error(UE)"
3360Sstevel@tonic-gate };
3370Sstevel@tonic-gate
3380Sstevel@tonic-gate /*ARGSUSED*/
3390Sstevel@tonic-gate static int
px_log_mmu_err(px_t * px_p)3400Sstevel@tonic-gate px_log_mmu_err(px_t *px_p)
3410Sstevel@tonic-gate {
3420Sstevel@tonic-gate /*
3430Sstevel@tonic-gate * Place holder, the correct eror bits need tobe logged.
3440Sstevel@tonic-gate */
3450Sstevel@tonic-gate return (0);
3460Sstevel@tonic-gate }
347