xref: /onnv-gate/usr/src/uts/sun4u/io/pci/pci_iommu.c (revision 2050:6648ed7f9bd5)
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*2050Ssuha  * Common Development and Distribution License (the "License").
6*2050Ssuha  * 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*2050Ssuha  * 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  * PCI iommu initialization and configuration
300Sstevel@tonic-gate  */
310Sstevel@tonic-gate 
320Sstevel@tonic-gate #include <sys/types.h>
330Sstevel@tonic-gate #include <sys/kmem.h>
340Sstevel@tonic-gate #include <sys/async.h>
350Sstevel@tonic-gate #include <sys/sysmacros.h>
360Sstevel@tonic-gate #include <sys/sunddi.h>
370Sstevel@tonic-gate #include <sys/ddi_impldefs.h>
380Sstevel@tonic-gate #include <sys/vmem.h>
390Sstevel@tonic-gate #include <sys/machsystm.h>	/* lddphys() */
400Sstevel@tonic-gate #include <sys/iommutsb.h>
410Sstevel@tonic-gate #include <sys/pci/pci_obj.h>
420Sstevel@tonic-gate 
430Sstevel@tonic-gate /*LINTLIBRARY*/
440Sstevel@tonic-gate 
450Sstevel@tonic-gate static void iommu_tlb_flushall(iommu_t *iommu_p);
460Sstevel@tonic-gate static void iommu_preserve_tsb(iommu_t *iommu_p);
470Sstevel@tonic-gate 
480Sstevel@tonic-gate void
iommu_create(pci_t * pci_p)490Sstevel@tonic-gate iommu_create(pci_t *pci_p)
500Sstevel@tonic-gate {
510Sstevel@tonic-gate 	dev_info_t *dip = pci_p->pci_dip;
520Sstevel@tonic-gate 	iommu_t *iommu_p;
530Sstevel@tonic-gate 	uintptr_t a;
540Sstevel@tonic-gate 	size_t cache_size;
550Sstevel@tonic-gate 	uint32_t tsb_entries;
560Sstevel@tonic-gate 
570Sstevel@tonic-gate 	char map_name[32];
580Sstevel@tonic-gate 	extern uint64_t va_to_pa(void *);
590Sstevel@tonic-gate 
600Sstevel@tonic-gate 	pci_dvma_range_prop_t	pci_dvma_range;
610Sstevel@tonic-gate 
620Sstevel@tonic-gate 	/*
630Sstevel@tonic-gate 	 * Allocate iommu state structure and link it to the
640Sstevel@tonic-gate 	 * pci state structure.
650Sstevel@tonic-gate 	 */
660Sstevel@tonic-gate 	iommu_p = (iommu_t *)kmem_zalloc(sizeof (iommu_t), KM_SLEEP);
670Sstevel@tonic-gate 	pci_p->pci_iommu_p = iommu_p;
680Sstevel@tonic-gate 	iommu_p->iommu_pci_p = pci_p;
690Sstevel@tonic-gate 	iommu_p->iommu_inst = ddi_get_instance(dip);
700Sstevel@tonic-gate 
710Sstevel@tonic-gate 	/*
720Sstevel@tonic-gate 	 * chip specific dvma_end, tsb_size & context support
730Sstevel@tonic-gate 	 */
740Sstevel@tonic-gate 	iommu_p->iommu_dvma_end = pci_iommu_dvma_end;
750Sstevel@tonic-gate 	a = pci_iommu_setup(iommu_p);
760Sstevel@tonic-gate 
770Sstevel@tonic-gate 	/*
780Sstevel@tonic-gate 	 * Determine the virtual address of iommu registers.
790Sstevel@tonic-gate 	 */
800Sstevel@tonic-gate 	iommu_p->iommu_ctrl_reg =
810Sstevel@tonic-gate 		(uint64_t *)(a + COMMON_IOMMU_CTRL_REG_OFFSET);
820Sstevel@tonic-gate 	iommu_p->iommu_tsb_base_addr_reg =
830Sstevel@tonic-gate 		(uint64_t *)(a + COMMON_IOMMU_TSB_BASE_ADDR_REG_OFFSET);
840Sstevel@tonic-gate 	iommu_p->iommu_flush_page_reg =
850Sstevel@tonic-gate 		(uint64_t *)(a + COMMON_IOMMU_FLUSH_PAGE_REG_OFFSET);
860Sstevel@tonic-gate 
870Sstevel@tonic-gate 	/*
880Sstevel@tonic-gate 	 * Configure the rest of the iommu parameters according to:
890Sstevel@tonic-gate 	 * tsb_size and dvma_end
900Sstevel@tonic-gate 	 */
910Sstevel@tonic-gate 	iommu_p->iommu_tsb_vaddr = /* retrieve TSB VA reserved by system */
920Sstevel@tonic-gate 		iommu_tsb_cookie_to_va(pci_p->pci_tsb_cookie);
930Sstevel@tonic-gate 	iommu_p->iommu_tsb_entries = tsb_entries =
940Sstevel@tonic-gate 		IOMMU_TSBSIZE_TO_TSBENTRIES(iommu_p->iommu_tsb_size);
950Sstevel@tonic-gate 	iommu_p->iommu_tsb_paddr = va_to_pa((caddr_t)iommu_p->iommu_tsb_vaddr);
960Sstevel@tonic-gate 	iommu_p->iommu_dvma_cache_locks =
970Sstevel@tonic-gate 		kmem_zalloc(pci_dvma_page_cache_entries, KM_SLEEP);
980Sstevel@tonic-gate 
990Sstevel@tonic-gate 	iommu_p->iommu_dvma_base = iommu_p->iommu_dvma_end + 1
1000Sstevel@tonic-gate 		- (tsb_entries * IOMMU_PAGE_SIZE);
1010Sstevel@tonic-gate 	iommu_p->dvma_base_pg = IOMMU_BTOP(iommu_p->iommu_dvma_base);
1020Sstevel@tonic-gate 	iommu_p->iommu_dvma_reserve = tsb_entries >> 1;
1030Sstevel@tonic-gate 	iommu_p->dvma_end_pg = IOMMU_BTOP(iommu_p->iommu_dvma_end);
1040Sstevel@tonic-gate 	iommu_p->iommu_dma_bypass_base = COMMON_IOMMU_BYPASS_BASE;
105*2050Ssuha 	iommu_p->iommu_dma_bypass_end = pci_iommu_bypass_end_configure();
1060Sstevel@tonic-gate 
1070Sstevel@tonic-gate 	/*
1080Sstevel@tonic-gate 	 * export "virtual-dma" software property to support
1090Sstevel@tonic-gate 	 * child devices needing to know DVMA range
1100Sstevel@tonic-gate 	 */
1110Sstevel@tonic-gate 	pci_dvma_range.dvma_base = (uint32_t)iommu_p->iommu_dvma_base;
1120Sstevel@tonic-gate 	pci_dvma_range.dvma_len = (uint32_t)
1130Sstevel@tonic-gate 		iommu_p->iommu_dvma_end - iommu_p->iommu_dvma_base + 1;
1140Sstevel@tonic-gate 	(void) ddi_prop_create(DDI_DEV_T_NONE, dip, DDI_PROP_CANSLEEP,
1150Sstevel@tonic-gate 		"virtual-dma", (caddr_t)&pci_dvma_range,
1160Sstevel@tonic-gate 		sizeof (pci_dvma_range));
1170Sstevel@tonic-gate 
1180Sstevel@tonic-gate 	DEBUG2(DBG_ATTACH, dip, "iommu_create: ctrl=%p, tsb=%p\n",
1190Sstevel@tonic-gate 		iommu_p->iommu_ctrl_reg, iommu_p->iommu_tsb_base_addr_reg);
1200Sstevel@tonic-gate 	DEBUG2(DBG_ATTACH, dip, "iommu_create: page_flush=%p, ctx_flush=%p\n",
1210Sstevel@tonic-gate 		iommu_p->iommu_flush_page_reg, iommu_p->iommu_flush_ctx_reg);
1220Sstevel@tonic-gate 	DEBUG2(DBG_ATTACH, dip, "iommu_create: tsb vaddr=%p tsb_paddr=%p\n",
1230Sstevel@tonic-gate 		iommu_p->iommu_tsb_vaddr, iommu_p->iommu_tsb_paddr);
1240Sstevel@tonic-gate 	DEBUG1(DBG_ATTACH, dip, "iommu_create: allocated size=%x\n",
1250Sstevel@tonic-gate 		iommu_tsb_cookie_to_size(pci_p->pci_tsb_cookie));
1260Sstevel@tonic-gate 	DEBUG2(DBG_ATTACH, dip, "iommu_create: fast tsb tte addr: %x + %x\n",
1270Sstevel@tonic-gate 		iommu_p->iommu_tsb_vaddr,
1280Sstevel@tonic-gate 		pci_dvma_page_cache_entries * pci_dvma_page_cache_clustsz);
1290Sstevel@tonic-gate 	DEBUG3(DBG_ATTACH, dip,
1300Sstevel@tonic-gate 		"iommu_create: tsb size=%x, tsb entries=%x, dvma base=%x\n",
1310Sstevel@tonic-gate 		iommu_p->iommu_tsb_size, iommu_p->iommu_tsb_entries,
1320Sstevel@tonic-gate 		iommu_p->iommu_dvma_base);
1330Sstevel@tonic-gate 	DEBUG2(DBG_ATTACH, dip,
1340Sstevel@tonic-gate 		"iommu_create: dvma_cache_locks=%x cache_entries=%x\n",
1350Sstevel@tonic-gate 		iommu_p->iommu_dvma_cache_locks, pci_dvma_page_cache_entries);
1360Sstevel@tonic-gate 
1370Sstevel@tonic-gate 	/*
1380Sstevel@tonic-gate 	 * zero out the area to be used for iommu tsb
1390Sstevel@tonic-gate 	 */
1400Sstevel@tonic-gate 	bzero(iommu_p->iommu_tsb_vaddr, tsb_entries << 3);
1410Sstevel@tonic-gate 
1420Sstevel@tonic-gate 	/*
1430Sstevel@tonic-gate 	 * Create a virtual memory map for dvma address space.
1440Sstevel@tonic-gate 	 * Reserve 'size' bytes of low dvma space for fast track cache.
1450Sstevel@tonic-gate 	 */
1460Sstevel@tonic-gate 	(void) snprintf(map_name, sizeof (map_name), "%s%d_dvma",
1470Sstevel@tonic-gate 		ddi_driver_name(dip), ddi_get_instance(dip));
1480Sstevel@tonic-gate 
1490Sstevel@tonic-gate 	cache_size = IOMMU_PTOB(pci_dvma_page_cache_entries *
1500Sstevel@tonic-gate 		pci_dvma_page_cache_clustsz);
1510Sstevel@tonic-gate 	iommu_p->iommu_dvma_fast_end = iommu_p->iommu_dvma_base +
1520Sstevel@tonic-gate 		cache_size - 1;
1530Sstevel@tonic-gate 	iommu_p->iommu_dvma_map = vmem_create(map_name,
1540Sstevel@tonic-gate 		(void *)(iommu_p->iommu_dvma_fast_end + 1),
1550Sstevel@tonic-gate 		IOMMU_PTOB(tsb_entries) - cache_size, IOMMU_PAGE_SIZE,
1560Sstevel@tonic-gate 		NULL, NULL, NULL, IOMMU_PAGE_SIZE, VM_SLEEP);
1570Sstevel@tonic-gate 
1580Sstevel@tonic-gate 	mutex_init(&iommu_p->dvma_debug_lock, NULL, MUTEX_DRIVER, NULL);
1590Sstevel@tonic-gate 
1600Sstevel@tonic-gate 	/*
1610Sstevel@tonic-gate 	 * On detach, the TSB Base Address Register gets set to zero,
1620Sstevel@tonic-gate 	 * so if its zero here, there is no need to preserve TTEs.
1630Sstevel@tonic-gate 	 */
1640Sstevel@tonic-gate 	if (pci_preserve_iommu_tsb && *iommu_p->iommu_tsb_base_addr_reg)
1650Sstevel@tonic-gate 		iommu_preserve_tsb(iommu_p);
1660Sstevel@tonic-gate 
1670Sstevel@tonic-gate 	iommu_configure(iommu_p);
1680Sstevel@tonic-gate }
1690Sstevel@tonic-gate 
1700Sstevel@tonic-gate void
iommu_destroy(pci_t * pci_p)1710Sstevel@tonic-gate iommu_destroy(pci_t *pci_p)
1720Sstevel@tonic-gate {
1730Sstevel@tonic-gate #ifdef DEBUG
1740Sstevel@tonic-gate 	dev_info_t *dip = pci_p->pci_dip;
1750Sstevel@tonic-gate #endif
1760Sstevel@tonic-gate 	iommu_t *iommu_p = pci_p->pci_iommu_p;
1770Sstevel@tonic-gate 	volatile uint64_t ctl_val = *iommu_p->iommu_ctrl_reg;
1780Sstevel@tonic-gate 
1790Sstevel@tonic-gate 	DEBUG0(DBG_DETACH, dip, "iommu_destroy:\n");
1800Sstevel@tonic-gate 
1810Sstevel@tonic-gate 	/*
1820Sstevel@tonic-gate 	 * Disable the IOMMU by setting the TSB Base Address to zero
1830Sstevel@tonic-gate 	 * and the TSB Table size to the smallest possible.
1840Sstevel@tonic-gate 	 */
1850Sstevel@tonic-gate 	ctl_val = ctl_val & ~(7 << COMMON_IOMMU_CTRL_TSB_SZ_SHIFT);
1860Sstevel@tonic-gate 
1870Sstevel@tonic-gate 	*iommu_p->iommu_ctrl_reg = ctl_val;
1880Sstevel@tonic-gate 	*iommu_p->iommu_tsb_base_addr_reg = 0;
1890Sstevel@tonic-gate 
1900Sstevel@tonic-gate 	/*
1910Sstevel@tonic-gate 	 * Return the boot time allocated tsb.
1920Sstevel@tonic-gate 	 */
1930Sstevel@tonic-gate 	iommu_tsb_free(pci_p->pci_tsb_cookie);
1940Sstevel@tonic-gate 
1950Sstevel@tonic-gate 	/*
1960Sstevel@tonic-gate 	 * Teardown any implementation-specific structures set up in
1970Sstevel@tonic-gate 	 * pci_iommu_setup.
1980Sstevel@tonic-gate 	 */
1990Sstevel@tonic-gate 	pci_iommu_teardown(iommu_p);
2000Sstevel@tonic-gate 
2010Sstevel@tonic-gate 	if (DVMA_DBG_ON(iommu_p))
2020Sstevel@tonic-gate 		pci_dvma_debug_fini(iommu_p);
2030Sstevel@tonic-gate 	mutex_destroy(&iommu_p->dvma_debug_lock);
2040Sstevel@tonic-gate 
2050Sstevel@tonic-gate 	/*
2060Sstevel@tonic-gate 	 * Free the dvma resource map.
2070Sstevel@tonic-gate 	 */
2080Sstevel@tonic-gate 	vmem_destroy(iommu_p->iommu_dvma_map);
2090Sstevel@tonic-gate 
2100Sstevel@tonic-gate 	kmem_free(iommu_p->iommu_dvma_cache_locks,
2110Sstevel@tonic-gate 	    pci_dvma_page_cache_entries);
2120Sstevel@tonic-gate 
2130Sstevel@tonic-gate 	/*
2140Sstevel@tonic-gate 	 * Free the iommu state structure.
2150Sstevel@tonic-gate 	 */
2160Sstevel@tonic-gate 	kmem_free(iommu_p, sizeof (iommu_t));
2170Sstevel@tonic-gate 	pci_p->pci_iommu_p = NULL;
2180Sstevel@tonic-gate }
2190Sstevel@tonic-gate 
2200Sstevel@tonic-gate /*
2210Sstevel@tonic-gate  * re-program iommu on the fly while preserving on-going dma
2220Sstevel@tonic-gate  * transactions on the PCI bus.
2230Sstevel@tonic-gate  */
2240Sstevel@tonic-gate void
iommu_configure(iommu_t * iommu_p)2250Sstevel@tonic-gate iommu_configure(iommu_t *iommu_p)
2260Sstevel@tonic-gate {
2270Sstevel@tonic-gate 	pci_t *pci_p = iommu_p->iommu_pci_p;
2280Sstevel@tonic-gate 	uint64_t cfgpa = pci_get_cfg_pabase(pci_p);
2290Sstevel@tonic-gate 	dev_info_t *dip = iommu_p->iommu_pci_p->pci_dip;
2300Sstevel@tonic-gate 	dev_info_t *cdip = NULL;
2310Sstevel@tonic-gate 	volatile uint64_t ctl_val = (uint64_t)
2320Sstevel@tonic-gate 		((iommu_p->iommu_tsb_size << COMMON_IOMMU_CTRL_TSB_SZ_SHIFT) |
2330Sstevel@tonic-gate 			(0 /* 8k page */ << COMMON_IOMMU_CTRL_TBW_SZ_SHIFT) |
2340Sstevel@tonic-gate 			COMMON_IOMMU_CTRL_ENABLE |
2350Sstevel@tonic-gate 			COMMON_IOMMU_CTRL_DIAG_ENABLE |
2360Sstevel@tonic-gate 			(pci_lock_tlb ? COMMON_IOMMU_CTRL_LCK_ENABLE : 0));
2370Sstevel@tonic-gate 
2380Sstevel@tonic-gate 	DEBUG2(DBG_ATTACH, dip, "iommu_configure: iommu_ctl=%08x.%08x\n",
2390Sstevel@tonic-gate 		HI32(ctl_val), LO32(ctl_val));
2400Sstevel@tonic-gate 	if (!pci_preserve_iommu_tsb || !(*iommu_p->iommu_tsb_base_addr_reg)) {
2410Sstevel@tonic-gate 		*iommu_p->iommu_ctrl_reg = COMMON_IOMMU_CTRL_DIAG_ENABLE;
2420Sstevel@tonic-gate 		iommu_tlb_flushall(iommu_p);
2430Sstevel@tonic-gate 		goto config;
2440Sstevel@tonic-gate 	}
2450Sstevel@tonic-gate 	cdip = ddi_get_child(dip);
2460Sstevel@tonic-gate 	for (; cdip; cdip = ddi_get_next_sibling(cdip)) {
2470Sstevel@tonic-gate 		uint32_t *reg_p;
2480Sstevel@tonic-gate 		int reg_len;
249506Scth 		if (ddi_getlongprop(DDI_DEV_T_ANY, cdip, DDI_PROP_DONTPASS,
2500Sstevel@tonic-gate 			"reg", (caddr_t)&reg_p, &reg_len) != DDI_PROP_SUCCESS)
2510Sstevel@tonic-gate 			continue;
2520Sstevel@tonic-gate 		cfgpa += (*reg_p) & (PCI_CONF_ADDR_MASK ^ PCI_REG_REG_M);
2530Sstevel@tonic-gate 		kmem_free(reg_p, reg_len);
2540Sstevel@tonic-gate 		break;
2550Sstevel@tonic-gate 	}
2560Sstevel@tonic-gate 
2570Sstevel@tonic-gate config:
2580Sstevel@tonic-gate 	pci_iommu_config(iommu_p, ctl_val, cdip ? cfgpa : 0);
2590Sstevel@tonic-gate }
2600Sstevel@tonic-gate 
2610Sstevel@tonic-gate void
iommu_map_pages(iommu_t * iommu_p,ddi_dma_impl_t * mp,dvma_addr_t dvma_pg,size_t npages,size_t pfn_index)2620Sstevel@tonic-gate iommu_map_pages(iommu_t *iommu_p, ddi_dma_impl_t *mp,
2630Sstevel@tonic-gate 		dvma_addr_t dvma_pg, size_t npages, size_t pfn_index)
2640Sstevel@tonic-gate {
2650Sstevel@tonic-gate 	int i;
2660Sstevel@tonic-gate 	dvma_addr_t pg_index = dvma_pg - iommu_p->dvma_base_pg;
2670Sstevel@tonic-gate 	uint64_t *tte_addr = iommu_p->iommu_tsb_vaddr + pg_index;
2680Sstevel@tonic-gate 	size_t pfn_last = pfn_index + npages;
2690Sstevel@tonic-gate 	uint64_t tte = PCI_GET_MP_TTE(mp->dmai_tte);
2700Sstevel@tonic-gate #ifdef DEBUG
2710Sstevel@tonic-gate 	dev_info_t *dip = iommu_p->iommu_pci_p->pci_dip;
2720Sstevel@tonic-gate #endif
2730Sstevel@tonic-gate 
2740Sstevel@tonic-gate 	ASSERT(pfn_last <= mp->dmai_ndvmapages);
2750Sstevel@tonic-gate 
2760Sstevel@tonic-gate 	DEBUG5(DBG_MAP_WIN, dip,
2770Sstevel@tonic-gate 		"iommu_map_pages:%x+%x=%x npages=0x%x pfn_index=0x%x\n",
2780Sstevel@tonic-gate 		(uint_t)iommu_p->dvma_base_pg, (uint_t)pg_index, dvma_pg,
2790Sstevel@tonic-gate 		(uint_t)npages, (uint_t)pfn_index);
2800Sstevel@tonic-gate 
2810Sstevel@tonic-gate 	for (i = pfn_index; i < pfn_last; i++, pg_index++, tte_addr++) {
2820Sstevel@tonic-gate 		iopfn_t pfn = PCI_GET_MP_PFN(mp, i);
2830Sstevel@tonic-gate 		volatile uint64_t cur_tte = IOMMU_PTOB(pfn) | tte;
2840Sstevel@tonic-gate 
2850Sstevel@tonic-gate 		DEBUG3(DBG_MAP_WIN, dip, "iommu_map_pages: mp=%p pg[%x]=%x\n",
2860Sstevel@tonic-gate 			mp, i, (uint_t)pfn);
2870Sstevel@tonic-gate 		DEBUG3(DBG_MAP_WIN, dip,
2880Sstevel@tonic-gate 			"iommu_map_pages: pg_index=%x tte=%08x.%08x\n",
2890Sstevel@tonic-gate 			pg_index, HI32(cur_tte), LO32(cur_tte));
2900Sstevel@tonic-gate 		ASSERT(TTE_IS_INVALID(*tte_addr));
2910Sstevel@tonic-gate 		*tte_addr = cur_tte;
2920Sstevel@tonic-gate #ifdef DEBUG
2930Sstevel@tonic-gate 		if (pfn == 0 && pci_warn_pp0)
2940Sstevel@tonic-gate 			cmn_err(CE_WARN, "%s%d <%p> doing DMA to pp0\n",
2950Sstevel@tonic-gate 				ddi_driver_name(mp->dmai_rdip),
2960Sstevel@tonic-gate 				ddi_get_instance(mp->dmai_rdip), mp);
2970Sstevel@tonic-gate #endif
2980Sstevel@tonic-gate 	}
2990Sstevel@tonic-gate 	ASSERT(tte_addr == iommu_p->iommu_tsb_vaddr + pg_index);
3000Sstevel@tonic-gate #ifdef DEBUG
3010Sstevel@tonic-gate 	if (HAS_REDZONE(mp)) {
3020Sstevel@tonic-gate 		DEBUG1(DBG_MAP_WIN, dip, "iommu_map_pages: redzone pg=%x\n",
3030Sstevel@tonic-gate 			pg_index);
3040Sstevel@tonic-gate 		ASSERT(TTE_IS_INVALID(iommu_p->iommu_tsb_vaddr[pg_index]));
3050Sstevel@tonic-gate 	}
3060Sstevel@tonic-gate #endif
3070Sstevel@tonic-gate 	if (DVMA_DBG_ON(iommu_p))
3080Sstevel@tonic-gate 		pci_dvma_alloc_debug(iommu_p, (char *)mp->dmai_mapping,
3090Sstevel@tonic-gate 			mp->dmai_size, mp);
3100Sstevel@tonic-gate }
3110Sstevel@tonic-gate 
3120Sstevel@tonic-gate /*
3130Sstevel@tonic-gate  * iommu_map_window - map a dvma window into the iommu
3140Sstevel@tonic-gate  *
3150Sstevel@tonic-gate  * used by: pci_dma_win(), pci_dma_ctlops() - DDI_DMA_MOVWIN, DDI_DMA_NEXTWIN
3160Sstevel@tonic-gate  *
3170Sstevel@tonic-gate  * return value: none
3180Sstevel@tonic-gate  */
3190Sstevel@tonic-gate /*ARGSUSED*/
3200Sstevel@tonic-gate void
iommu_map_window(iommu_t * iommu_p,ddi_dma_impl_t * mp,window_t win_no)3210Sstevel@tonic-gate iommu_map_window(iommu_t *iommu_p, ddi_dma_impl_t *mp, window_t win_no)
3220Sstevel@tonic-gate {
3230Sstevel@tonic-gate 	uint32_t obj_pg0_off = mp->dmai_roffset;
3240Sstevel@tonic-gate 	uint32_t win_pg0_off = win_no ? 0 : obj_pg0_off;
3250Sstevel@tonic-gate 	size_t win_size = mp->dmai_winsize;
3260Sstevel@tonic-gate 	size_t pfn_index = win_size * win_no;			/* temp value */
3270Sstevel@tonic-gate 	size_t obj_off = win_no ? pfn_index - obj_pg0_off : 0;	/* xferred sz */
3280Sstevel@tonic-gate 	dvma_addr_t dvma_pg = IOMMU_BTOP(mp->dmai_mapping);
3290Sstevel@tonic-gate 	size_t res_size = mp->dmai_object.dmao_size - obj_off + win_pg0_off;
3300Sstevel@tonic-gate 
3310Sstevel@tonic-gate 	ASSERT(!(win_size & IOMMU_PAGE_OFFSET));
3320Sstevel@tonic-gate 	if (win_no >= mp->dmai_nwin)
3330Sstevel@tonic-gate 		return;
3340Sstevel@tonic-gate 	if (res_size < win_size)		/* last window */
3350Sstevel@tonic-gate 		win_size = res_size;		/* mp->dmai_winsize unchanged */
3360Sstevel@tonic-gate 
3370Sstevel@tonic-gate 	mp->dmai_mapping = IOMMU_PTOB(dvma_pg) | win_pg0_off;
3380Sstevel@tonic-gate 	mp->dmai_size = win_size - win_pg0_off;	/* cur win xferrable size */
3390Sstevel@tonic-gate 	mp->dmai_offset = obj_off;		/* win offset into object */
3400Sstevel@tonic-gate 	pfn_index = IOMMU_BTOP(pfn_index);	/* index into pfnlist */
3410Sstevel@tonic-gate 	iommu_map_pages(iommu_p, mp, dvma_pg, IOMMU_BTOPR(win_size), pfn_index);
3420Sstevel@tonic-gate }
3430Sstevel@tonic-gate 
3440Sstevel@tonic-gate void
iommu_unmap_pages(iommu_t * iommu_p,dvma_addr_t dvma_pg,uint_t npages)3450Sstevel@tonic-gate iommu_unmap_pages(iommu_t *iommu_p, dvma_addr_t dvma_pg, uint_t npages)
3460Sstevel@tonic-gate {
3470Sstevel@tonic-gate 	dvma_addr_t pg_index = IOMMU_PAGE_INDEX(iommu_p, dvma_pg);
3480Sstevel@tonic-gate 
3490Sstevel@tonic-gate 	for (; npages; npages--, dvma_pg++, pg_index++) {
3500Sstevel@tonic-gate 		DEBUG1(DBG_UNMAP_WIN|DBG_CONT, 0, " %x", dvma_pg);
3510Sstevel@tonic-gate 		IOMMU_UNLOAD_TTE(iommu_p, pg_index);
3520Sstevel@tonic-gate 
3530Sstevel@tonic-gate 		if (!tm_mtlb_gc)
3540Sstevel@tonic-gate 			IOMMU_PAGE_FLUSH(iommu_p, dvma_pg);
3550Sstevel@tonic-gate 	}
3560Sstevel@tonic-gate }
3570Sstevel@tonic-gate 
3580Sstevel@tonic-gate void
iommu_remap_pages(iommu_t * iommu_p,ddi_dma_impl_t * mp,dvma_addr_t dvma_pg,size_t npages,size_t pfn_index)3590Sstevel@tonic-gate iommu_remap_pages(iommu_t *iommu_p, ddi_dma_impl_t *mp, dvma_addr_t dvma_pg,
3600Sstevel@tonic-gate 	size_t npages, size_t pfn_index)
3610Sstevel@tonic-gate {
3620Sstevel@tonic-gate 	iommu_unmap_pages(iommu_p, dvma_pg, npages);
3630Sstevel@tonic-gate 	iommu_map_pages(iommu_p, mp, dvma_pg, npages, pfn_index);
3640Sstevel@tonic-gate }
3650Sstevel@tonic-gate 
3660Sstevel@tonic-gate /*
3670Sstevel@tonic-gate  * iommu_unmap_window
3680Sstevel@tonic-gate  *
3690Sstevel@tonic-gate  * This routine is called to break down the iommu mappings to a dvma window.
3700Sstevel@tonic-gate  * Non partial mappings are viewed as single window mapping.
3710Sstevel@tonic-gate  *
3720Sstevel@tonic-gate  * used by: pci_dma_unbindhdl(), pci_dma_window(),
3730Sstevel@tonic-gate  *	and pci_dma_ctlops() - DDI_DMA_FREE, DDI_DMA_MOVWIN, DDI_DMA_NEXTWIN
3740Sstevel@tonic-gate  *
3750Sstevel@tonic-gate  * return value: none
3760Sstevel@tonic-gate  */
3770Sstevel@tonic-gate /*ARGSUSED*/
3780Sstevel@tonic-gate void
iommu_unmap_window(iommu_t * iommu_p,ddi_dma_impl_t * mp)3790Sstevel@tonic-gate iommu_unmap_window(iommu_t *iommu_p, ddi_dma_impl_t *mp)
3800Sstevel@tonic-gate {
3810Sstevel@tonic-gate 	dvma_addr_t dvma_pg = IOMMU_BTOP(mp->dmai_mapping);
3820Sstevel@tonic-gate 	dvma_addr_t pg_index = IOMMU_PAGE_INDEX(iommu_p, dvma_pg);
3830Sstevel@tonic-gate 	uint_t npages = IOMMU_BTOP(mp->dmai_winsize);
3840Sstevel@tonic-gate #ifdef DEBUG
3850Sstevel@tonic-gate 	dev_info_t *dip = iommu_p->iommu_pci_p->pci_dip;
3860Sstevel@tonic-gate #endif
3870Sstevel@tonic-gate 	/*
3880Sstevel@tonic-gate 	 * Invalidate each page of the mapping in the tsb and flush
3890Sstevel@tonic-gate 	 * it from the tlb.
3900Sstevel@tonic-gate 	 */
3910Sstevel@tonic-gate 	DEBUG2(DBG_UNMAP_WIN, dip, "mp=%p %x pfns:", mp, npages);
3920Sstevel@tonic-gate 	if (mp->dmai_flags & DMAI_FLAGS_CONTEXT) {
3930Sstevel@tonic-gate 		dvma_context_t ctx = MP2CTX(mp);
3940Sstevel@tonic-gate 		for (; npages; npages--, pg_index++) {
3950Sstevel@tonic-gate 			DEBUG1(DBG_UNMAP_WIN|DBG_CONT, dip, " %x", pg_index);
3960Sstevel@tonic-gate 			IOMMU_UNLOAD_TTE(iommu_p, pg_index);
3970Sstevel@tonic-gate 		}
3980Sstevel@tonic-gate 		DEBUG1(DBG_UNMAP_WIN|DBG_CONT, dip, " (context %x)", ctx);
3990Sstevel@tonic-gate 		*iommu_p->iommu_flush_ctx_reg = ctx;
4000Sstevel@tonic-gate 	} else
4010Sstevel@tonic-gate 		iommu_unmap_pages(iommu_p, dvma_pg, npages);
4020Sstevel@tonic-gate 
4030Sstevel@tonic-gate 	DEBUG0(DBG_UNMAP_WIN|DBG_CONT, dip, "\n");
4040Sstevel@tonic-gate 
4050Sstevel@tonic-gate 	if (DVMA_DBG_ON(iommu_p))
4060Sstevel@tonic-gate 		pci_dvma_free_debug(iommu_p, (char *)mp->dmai_mapping,
4070Sstevel@tonic-gate 			mp->dmai_size, mp);
4080Sstevel@tonic-gate }
4090Sstevel@tonic-gate 
4100Sstevel@tonic-gate int
pci_alloc_tsb(pci_t * pci_p)4110Sstevel@tonic-gate pci_alloc_tsb(pci_t *pci_p)
4120Sstevel@tonic-gate {
4130Sstevel@tonic-gate 	uint16_t tsbc;
4140Sstevel@tonic-gate 
4150Sstevel@tonic-gate 	if ((tsbc = iommu_tsb_alloc(pci_p->pci_id)) == IOMMU_TSB_COOKIE_NONE) {
4160Sstevel@tonic-gate 		cmn_err(CE_WARN, "%s%d: Unable to allocate IOMMU TSB.",
4170Sstevel@tonic-gate 		    ddi_driver_name(pci_p->pci_dip),
4180Sstevel@tonic-gate 		    ddi_get_instance(pci_p->pci_dip));
4190Sstevel@tonic-gate 		return (DDI_FAILURE);
4200Sstevel@tonic-gate 	}
4210Sstevel@tonic-gate 	pci_p->pci_tsb_cookie = tsbc;
4220Sstevel@tonic-gate 	return (DDI_SUCCESS);
4230Sstevel@tonic-gate }
4240Sstevel@tonic-gate 
4250Sstevel@tonic-gate void
pci_free_tsb(pci_t * pci_p)4260Sstevel@tonic-gate pci_free_tsb(pci_t *pci_p)
4270Sstevel@tonic-gate {
4280Sstevel@tonic-gate 	iommu_tsb_free(pci_p->pci_tsb_cookie);
4290Sstevel@tonic-gate }
4300Sstevel@tonic-gate 
4310Sstevel@tonic-gate #if 0
4320Sstevel@tonic-gate /*
4330Sstevel@tonic-gate  * The following data structure is used to map a tsb size
4340Sstevel@tonic-gate  * to a tsb size configuration parameter in the iommu
4350Sstevel@tonic-gate  * control register.
4360Sstevel@tonic-gate  * This is a hardware table. It is here for reference only.
4370Sstevel@tonic-gate  */
4380Sstevel@tonic-gate static int pci_iommu_tsb_sizes[] = {
4390Sstevel@tonic-gate 	0x2000,		/* 0 - 8 mb */
4400Sstevel@tonic-gate 	0x4000,		/* 1 - 16 mb */
4410Sstevel@tonic-gate 	0x8000,		/* 2 - 32 mb */
4420Sstevel@tonic-gate 	0x10000,	/* 3 - 64 mb */
4430Sstevel@tonic-gate 	0x20000,	/* 4 - 128 mb */
4440Sstevel@tonic-gate 	0x40000,	/* 5 - 256 mb */
4450Sstevel@tonic-gate 	0x80000,	/* 6 - 512 mb */
4460Sstevel@tonic-gate 	0x100000	/* 7 - 1 gb */
4470Sstevel@tonic-gate };
4480Sstevel@tonic-gate #endif
4490Sstevel@tonic-gate 
4500Sstevel@tonic-gate uint_t
iommu_tsb_size_encode(uint_t tsb_bytes)4510Sstevel@tonic-gate iommu_tsb_size_encode(uint_t tsb_bytes)
4520Sstevel@tonic-gate {
4530Sstevel@tonic-gate 	uint_t i;
4540Sstevel@tonic-gate 
4550Sstevel@tonic-gate 	for (i = 7; i && (tsb_bytes < (0x2000 << i)); i--)
4560Sstevel@tonic-gate 		/* empty */;
4570Sstevel@tonic-gate 	return (i);
4580Sstevel@tonic-gate }
4590Sstevel@tonic-gate 
4600Sstevel@tonic-gate /*
4610Sstevel@tonic-gate  * invalidate IOMMU TLB entries through diagnostic registers.
4620Sstevel@tonic-gate  */
4630Sstevel@tonic-gate static void
iommu_tlb_flushall(iommu_t * iommu_p)4640Sstevel@tonic-gate iommu_tlb_flushall(iommu_t *iommu_p)
4650Sstevel@tonic-gate {
4660Sstevel@tonic-gate 	int i;
4670Sstevel@tonic-gate 	uint64_t base = (uint64_t)(iommu_p->iommu_ctrl_reg) -
4680Sstevel@tonic-gate 		COMMON_IOMMU_CTRL_REG_OFFSET;
4690Sstevel@tonic-gate 	volatile uint64_t *tlb_tag = (volatile uint64_t *)
4700Sstevel@tonic-gate 			(base + COMMON_IOMMU_TLB_TAG_DIAG_ACC_OFFSET);
4710Sstevel@tonic-gate 	volatile uint64_t *tlb_data = (volatile uint64_t *)
4720Sstevel@tonic-gate 			(base + COMMON_IOMMU_TLB_DATA_DIAG_ACC_OFFSET);
4730Sstevel@tonic-gate 	for (i = 0; i < IOMMU_TLB_ENTRIES; i++)
4740Sstevel@tonic-gate 		tlb_tag[i] = tlb_data[i] = 0ull;
4750Sstevel@tonic-gate }
4760Sstevel@tonic-gate 
4770Sstevel@tonic-gate static void
iommu_preserve_tsb(iommu_t * iommu_p)4780Sstevel@tonic-gate iommu_preserve_tsb(iommu_t *iommu_p)
4790Sstevel@tonic-gate {
4800Sstevel@tonic-gate #ifdef DEBUG
4810Sstevel@tonic-gate 	dev_info_t *dip = iommu_p->iommu_pci_p->pci_dip;
4820Sstevel@tonic-gate #endif
4830Sstevel@tonic-gate 	uint_t i, obp_tsb_entries, obp_tsb_size, base_pg_index;
4840Sstevel@tonic-gate 	uint64_t ctl = *iommu_p->iommu_ctrl_reg;
4850Sstevel@tonic-gate 	uint64_t obp_tsb_pa = *iommu_p->iommu_tsb_base_addr_reg;
4860Sstevel@tonic-gate 	uint64_t *base_tte_addr;
4870Sstevel@tonic-gate 
4880Sstevel@tonic-gate 	DEBUG3(DBG_ATTACH, dip,
4890Sstevel@tonic-gate 		"iommu_tsb_base_addr_reg=0x%08x (0x%08x.0x%08x)\n",
4900Sstevel@tonic-gate 		iommu_p->iommu_tsb_base_addr_reg,
4910Sstevel@tonic-gate 		(uint32_t)(*iommu_p->iommu_tsb_base_addr_reg >> 32),
4920Sstevel@tonic-gate 		(uint32_t)(*iommu_p->iommu_tsb_base_addr_reg & 0xffffffff));
4930Sstevel@tonic-gate 
4940Sstevel@tonic-gate 	obp_tsb_size = IOMMU_CTL_TO_TSBSIZE(ctl);
4950Sstevel@tonic-gate 	obp_tsb_entries = IOMMU_TSBSIZE_TO_TSBENTRIES(obp_tsb_size);
4960Sstevel@tonic-gate 	base_pg_index = iommu_p->dvma_end_pg - obp_tsb_entries + 1;
4970Sstevel@tonic-gate 	base_tte_addr = iommu_p->iommu_tsb_vaddr +
4980Sstevel@tonic-gate 		(iommu_p->iommu_tsb_entries - obp_tsb_entries);
4990Sstevel@tonic-gate 
5000Sstevel@tonic-gate 	/*
5010Sstevel@tonic-gate 	 * old darwin prom does not set tsb size correctly, bail out.
5020Sstevel@tonic-gate 	 */
5030Sstevel@tonic-gate 	if ((obp_tsb_size == IOMMU_DARWIN_BOGUS_TSBSIZE) &&
5040Sstevel@tonic-gate 		(CHIP_TYPE(iommu_p->iommu_pci_p) == PCI_CHIP_SABRE))
5050Sstevel@tonic-gate 			return;
5060Sstevel@tonic-gate 
5070Sstevel@tonic-gate 	DEBUG3(DBG_ATTACH, dip, "iommu_preserve_tsb: kernel info\n"
5080Sstevel@tonic-gate 		"iommu_tsb_vaddr=%08x copy to base_tte_addr=%08x "
5090Sstevel@tonic-gate 		"base_pg_index=%x\n", iommu_p->iommu_tsb_vaddr,
5100Sstevel@tonic-gate 			base_tte_addr, base_pg_index);
5110Sstevel@tonic-gate 
5120Sstevel@tonic-gate 	DEBUG3(DBG_ATTACH | DBG_CONT, dip, "iommu_preserve_tsb: obp info "
5130Sstevel@tonic-gate 		"obp_tsb_entries=0x%x obp_tsb_pa=%08x.%08x\n", obp_tsb_entries,
5140Sstevel@tonic-gate 			(uint32_t)(obp_tsb_pa >> 32), (uint32_t)obp_tsb_pa);
5150Sstevel@tonic-gate 
5160Sstevel@tonic-gate 	for (i = 0; i < obp_tsb_entries; i++) {
5170Sstevel@tonic-gate 		uint64_t tte = lddphys(obp_tsb_pa + i * 8);
5180Sstevel@tonic-gate 		caddr_t va;
5190Sstevel@tonic-gate 
5200Sstevel@tonic-gate 		if (TTE_IS_INVALID(tte)) {
5210Sstevel@tonic-gate 			DEBUG0(DBG_ATTACH | DBG_CONT, dip, ".");
5220Sstevel@tonic-gate 			continue;
5230Sstevel@tonic-gate 		}
5240Sstevel@tonic-gate 
5250Sstevel@tonic-gate 		base_tte_addr[i] = tte;
5260Sstevel@tonic-gate 		DEBUG3(DBG_ATTACH | DBG_CONT, dip,
5270Sstevel@tonic-gate 			"\npreserve_tsb: (%x)=%08x.%08x\n", base_tte_addr + i,
5280Sstevel@tonic-gate 			(uint_t)(tte >> 32), (uint_t)(tte & 0xffffffff));
5290Sstevel@tonic-gate 
5300Sstevel@tonic-gate 		/*
5310Sstevel@tonic-gate 		 * permanantly reserve this page from dvma address space
5320Sstevel@tonic-gate 		 * resource map
5330Sstevel@tonic-gate 		 */
5340Sstevel@tonic-gate 
5350Sstevel@tonic-gate 		va = (caddr_t)(IOMMU_PTOB(base_pg_index + i));
5360Sstevel@tonic-gate 		(void) vmem_xalloc(iommu_p->iommu_dvma_map, IOMMU_PAGE_SIZE,
5370Sstevel@tonic-gate 			IOMMU_PAGE_SIZE, 0, 0, va, va + IOMMU_PAGE_SIZE,
5380Sstevel@tonic-gate 			VM_NOSLEEP | VM_BESTFIT | VM_PANIC);
5390Sstevel@tonic-gate 	}
5400Sstevel@tonic-gate }
541