xref: /onnv-gate/usr/src/uts/sun4u/sys/pci/pci_iommu.h (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 #ifndef _SYS_PCI_IOMMU_H
270Sstevel@tonic-gate #define	_SYS_PCI_IOMMU_H
280Sstevel@tonic-gate 
290Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
300Sstevel@tonic-gate 
310Sstevel@tonic-gate #ifdef	__cplusplus
320Sstevel@tonic-gate extern "C" {
330Sstevel@tonic-gate #endif
340Sstevel@tonic-gate 
350Sstevel@tonic-gate #include <sys/vmem.h>
360Sstevel@tonic-gate 
370Sstevel@tonic-gate typedef uint64_t dvma_addr_t;
380Sstevel@tonic-gate typedef uint64_t dma_bypass_addr_t;
390Sstevel@tonic-gate typedef uint64_t dma_peer_addr_t;
400Sstevel@tonic-gate typedef uint16_t dvma_context_t;
410Sstevel@tonic-gate typedef uint64_t window_t;
420Sstevel@tonic-gate 
430Sstevel@tonic-gate /*
440Sstevel@tonic-gate  * The following typedef's represents the types for DMA transactions
450Sstevel@tonic-gate  * and corresponding DMA addresses supported by psycho/schizo.
460Sstevel@tonic-gate  */
470Sstevel@tonic-gate typedef enum { IOMMU_XLATE, IOMMU_BYPASS, PCI_PEER_TO_PEER } iommu_dma_t;
480Sstevel@tonic-gate 
490Sstevel@tonic-gate /*
500Sstevel@tonic-gate  * The following macros define the iommu page size and related operations.
510Sstevel@tonic-gate  */
520Sstevel@tonic-gate #define	IOMMU_PAGE_SHIFT	13
530Sstevel@tonic-gate #define	IOMMU_PAGE_SIZE		(1 << IOMMU_PAGE_SHIFT)
540Sstevel@tonic-gate #define	IOMMU_PAGE_MASK		~(IOMMU_PAGE_SIZE - 1)
550Sstevel@tonic-gate #define	IOMMU_PAGE_OFFSET	(IOMMU_PAGE_SIZE - 1)
560Sstevel@tonic-gate #define	IOMMU_PTOB(x)		(((uint64_t)(x)) << IOMMU_PAGE_SHIFT)
570Sstevel@tonic-gate #define	IOMMU_BTOP(x)		((x) >> IOMMU_PAGE_SHIFT)
580Sstevel@tonic-gate #define	IOMMU_BTOPR(x)		IOMMU_BTOP((x) + IOMMU_PAGE_OFFSET)
590Sstevel@tonic-gate 
600Sstevel@tonic-gate /*
610Sstevel@tonic-gate  * control register decoding
620Sstevel@tonic-gate  */
630Sstevel@tonic-gate /* tsb size: 0=1k 1=2k 2=4k 3=8k 4=16k 5=32k 6=64k 7=128k */
640Sstevel@tonic-gate #define	IOMMU_CTL_TO_TSBSIZE(ctl)	((ctl) >> 16)
650Sstevel@tonic-gate #define	IOMMU_TSBSIZE_TO_TSBENTRIES(s)	((1 << (s)) << (13 - 3))
660Sstevel@tonic-gate #define	IOMMU_DARWIN_BOGUS_TSBSIZE	7
670Sstevel@tonic-gate 
680Sstevel@tonic-gate /*
690Sstevel@tonic-gate  * boiler plate for tte (everything except the pfn)
700Sstevel@tonic-gate  */
710Sstevel@tonic-gate #define	MAKE_TTE_TEMPLATE(pfn, mp) (COMMON_IOMMU_TTE_V | \
720Sstevel@tonic-gate 	(pf_is_memory(pfn) ? COMMON_IOMMU_TTE_C : 0) | \
730Sstevel@tonic-gate 	((mp->dmai_rflags & DDI_DMA_READ) ? COMMON_IOMMU_TTE_W : 0) | \
740Sstevel@tonic-gate 	((mp->dmai_rflags & DDI_DMA_CONSISTENT) ? 0 : COMMON_IOMMU_TTE_S))
750Sstevel@tonic-gate #define	TTE_IS_INVALID(tte)	(((tte) & COMMON_IOMMU_TTE_V) == 0x0ull)
760Sstevel@tonic-gate 
770Sstevel@tonic-gate /*
780Sstevel@tonic-gate  * The following macros define the address ranges supported for DVMA
790Sstevel@tonic-gate  * and iommu bypass transfers.
800Sstevel@tonic-gate  */
810Sstevel@tonic-gate #define	COMMON_IOMMU_BYPASS_BASE	0xFFFC000000000000ull
82*2050Ssuha 
83*2050Ssuha /*
84*2050Ssuha  * The IOMMU_BYPASS_END is ASIC dependent and so defined in the appropriate
85*2050Ssuha  * header file.
86*2050Ssuha  */
870Sstevel@tonic-gate 
880Sstevel@tonic-gate /*
890Sstevel@tonic-gate  * For iommu bypass addresses, bit 43 specifies cacheability.
900Sstevel@tonic-gate  */
910Sstevel@tonic-gate #define	COMMON_IOMMU_BYPASS_NONCACHE	0x0000080000000000ull
920Sstevel@tonic-gate 
930Sstevel@tonic-gate /*
940Sstevel@tonic-gate  * Generic iommu definitions and types:
950Sstevel@tonic-gate  */
960Sstevel@tonic-gate #define	IOMMU_TLB_ENTRIES		16
970Sstevel@tonic-gate 
980Sstevel@tonic-gate /*
990Sstevel@tonic-gate  * The following macros are for loading and unloading iotte
1000Sstevel@tonic-gate  * entries.
1010Sstevel@tonic-gate  */
1020Sstevel@tonic-gate #define	COMMON_IOMMU_TTE_SIZE		8
1030Sstevel@tonic-gate #define	COMMON_IOMMU_TTE_V		0x8000000000000000ull
1040Sstevel@tonic-gate #define	COMMON_IOMMU_TTE_S		0x1000000000000000ull
1050Sstevel@tonic-gate #define	COMMON_IOMMU_TTE_C		0x0000000000000010ull
1060Sstevel@tonic-gate #define	COMMON_IOMMU_TTE_W		0x0000000000000002ull
1070Sstevel@tonic-gate #define	COMMON_IOMMU_INVALID_TTE	0x0000000000000000ull
1080Sstevel@tonic-gate 
1090Sstevel@tonic-gate /*
1100Sstevel@tonic-gate  * Tomatillo's micro TLB bug. errata #82
1110Sstevel@tonic-gate  */
1120Sstevel@tonic-gate typedef struct dvma_unbind_req {
1130Sstevel@tonic-gate 	uint32_t	dur_base;
1140Sstevel@tonic-gate 	uint_t		dur_npg;
1150Sstevel@tonic-gate 	uint_t		dur_flags; /* = dmai_flags & DMAI_FLAGS_VMEMCACHE */
1160Sstevel@tonic-gate } dvma_unbind_req_t;
1170Sstevel@tonic-gate 
1180Sstevel@tonic-gate /*
1190Sstevel@tonic-gate  * iommu block soft state structure:
1200Sstevel@tonic-gate  *
1210Sstevel@tonic-gate  * Each pci node may share an iommu block structure with its peer
1220Sstevel@tonic-gate  * node of have its own private iommu block structure.
1230Sstevel@tonic-gate  */
1240Sstevel@tonic-gate typedef struct iommu iommu_t;
1250Sstevel@tonic-gate struct iommu {
1260Sstevel@tonic-gate 
1270Sstevel@tonic-gate 	pci_t *iommu_pci_p;	/* link back to pci soft state */
1280Sstevel@tonic-gate 	int iommu_inst;		/* ddi_get_instance(iommu_pci_p->pci_dip) */
1290Sstevel@tonic-gate 
1300Sstevel@tonic-gate 	volatile uint64_t *iommu_ctrl_reg;
1310Sstevel@tonic-gate 	volatile uint64_t *iommu_tsb_base_addr_reg;
1320Sstevel@tonic-gate 	volatile uint64_t *iommu_flush_page_reg;
1330Sstevel@tonic-gate 	volatile uint64_t *iommu_flush_ctx_reg;	/* schizo only */
1340Sstevel@tonic-gate 	volatile uint64_t *iommu_tfar_reg; /* tomatillo only */
1350Sstevel@tonic-gate 
1360Sstevel@tonic-gate 	/*
1370Sstevel@tonic-gate 	 * virtual and physical addresses and size of the iommu tsb:
1380Sstevel@tonic-gate 	 */
1390Sstevel@tonic-gate 	uint64_t *iommu_tsb_vaddr;
1400Sstevel@tonic-gate 	uint64_t iommu_tsb_paddr;
1410Sstevel@tonic-gate 	uint_t iommu_tsb_entries;
1420Sstevel@tonic-gate 	uint_t iommu_tsb_size;
1430Sstevel@tonic-gate 
1440Sstevel@tonic-gate 	/*
1450Sstevel@tonic-gate 	 * address ranges of dvma space:
1460Sstevel@tonic-gate 	 */
1470Sstevel@tonic-gate 	dvma_addr_t iommu_dvma_base;
1480Sstevel@tonic-gate 	dvma_addr_t iommu_dvma_end;
1490Sstevel@tonic-gate 	dvma_addr_t iommu_dvma_fast_end;
1500Sstevel@tonic-gate 	dvma_addr_t dvma_base_pg;	/* = IOMMU_BTOP(iommu_dvma_base) */
1510Sstevel@tonic-gate 	dvma_addr_t dvma_end_pg;	/* = IOMMU_BTOP(iommu_dvma_end) */
1520Sstevel@tonic-gate 
1530Sstevel@tonic-gate 	/*
1540Sstevel@tonic-gate 	 * address ranges of dma bypass space:
1550Sstevel@tonic-gate 	 */
1560Sstevel@tonic-gate 	dma_bypass_addr_t iommu_dma_bypass_base;
1570Sstevel@tonic-gate 	dma_bypass_addr_t iommu_dma_bypass_end;
1580Sstevel@tonic-gate 
1590Sstevel@tonic-gate 	/*
1600Sstevel@tonic-gate 	 * virtual memory map and callback id for dvma space:
1610Sstevel@tonic-gate 	 */
1620Sstevel@tonic-gate 	vmem_t *iommu_dvma_map;
1630Sstevel@tonic-gate 	uintptr_t iommu_dvma_clid;
1640Sstevel@tonic-gate 
1650Sstevel@tonic-gate 	/*
1660Sstevel@tonic-gate 	 * fields for fast dvma interfaces:
1670Sstevel@tonic-gate 	 */
1680Sstevel@tonic-gate 	ulong_t iommu_dvma_reserve;
1690Sstevel@tonic-gate 
1700Sstevel@tonic-gate 	/*
1710Sstevel@tonic-gate 	 * dvma fast track page cache byte map
1720Sstevel@tonic-gate 	 */
1730Sstevel@tonic-gate 	uint8_t *iommu_dvma_cache_locks;
1740Sstevel@tonic-gate 	uint_t iommu_dvma_addr_scan_start;
1750Sstevel@tonic-gate 
1760Sstevel@tonic-gate 	/*
1770Sstevel@tonic-gate 	 * dvma context bitmap
1780Sstevel@tonic-gate 	 */
1790Sstevel@tonic-gate 	uint64_t *iommu_ctx_bitmap;
1800Sstevel@tonic-gate 
1810Sstevel@tonic-gate 	/*
1820Sstevel@tonic-gate 	 * dvma debug
1830Sstevel@tonic-gate 	 */
1840Sstevel@tonic-gate 	kmutex_t dvma_debug_lock;
1850Sstevel@tonic-gate 	uint32_t dvma_alloc_rec_index;
1860Sstevel@tonic-gate 	uint32_t dvma_free_rec_index;
1870Sstevel@tonic-gate 	uint32_t dvma_active_count;
1880Sstevel@tonic-gate 
1890Sstevel@tonic-gate 	struct dvma_rec *dvma_alloc_rec;
1900Sstevel@tonic-gate 	struct dvma_rec *dvma_free_rec;
1910Sstevel@tonic-gate 	struct dvma_rec *dvma_active_list;
1920Sstevel@tonic-gate 
1930Sstevel@tonic-gate 	/*
1940Sstevel@tonic-gate 	 * tomatillo's micro TLB bug. errata #82
1950Sstevel@tonic-gate 	 */
1960Sstevel@tonic-gate 	dvma_unbind_req_t *iommu_mtlb_req_p;	/* unbind requests */
1970Sstevel@tonic-gate 	uint32_t	iommu_mtlb_maxpgs;	/* GC threshold */
1980Sstevel@tonic-gate 	uint32_t	iommu_mtlb_npgs;	/* total page count */
1990Sstevel@tonic-gate 	uint32_t	iommu_mtlb_nreq;	/* total request count */
2000Sstevel@tonic-gate 	kmutex_t	iommu_mtlb_lock;
2010Sstevel@tonic-gate };
2020Sstevel@tonic-gate 
2030Sstevel@tonic-gate typedef struct pci_dvma_range_prop {
2040Sstevel@tonic-gate 	uint32_t dvma_base;
2050Sstevel@tonic-gate 	uint32_t dvma_len;
2060Sstevel@tonic-gate } pci_dvma_range_prop_t;
2070Sstevel@tonic-gate 
2080Sstevel@tonic-gate #define	IOMMU_PAGE_INDEX(iommu_p, dvma_pg) ((dvma_pg) - (iommu_p)->dvma_base_pg)
2090Sstevel@tonic-gate #define	IOMMU_PAGE_FLUSH(iommu_p, dvma_pg) \
2100Sstevel@tonic-gate 	*(iommu_p)->iommu_flush_page_reg = IOMMU_PTOB(dvma_pg)
2110Sstevel@tonic-gate #define	IOMMU_UNLOAD_TTE(iommu_p, pg_index) \
2120Sstevel@tonic-gate 	(iommu_p)->iommu_tsb_vaddr[pg_index] = COMMON_IOMMU_INVALID_TTE
2130Sstevel@tonic-gate #define	IOMMU_PAGE_TTEPA(iommu_p, dvma_pg) \
2140Sstevel@tonic-gate 	((iommu_p)->iommu_tsb_paddr + (IOMMU_PAGE_INDEX(iommu_p, dvma_pg) << 3))
2150Sstevel@tonic-gate 
2160Sstevel@tonic-gate #define	IOMMU_CONTEXT_BITS 12
2170Sstevel@tonic-gate #define	IOMMU_CTX_MASK		((1 << IOMMU_CONTEXT_BITS) - 1)
2180Sstevel@tonic-gate #define	IOMMU_TTE_CTX_SHIFT	47
2190Sstevel@tonic-gate #define	IOMMU_CTX2TTE(ctx) (((uint64_t)(ctx)) << IOMMU_TTE_CTX_SHIFT)
2200Sstevel@tonic-gate #define	IOMMU_TTE2CTX(tte) \
2210Sstevel@tonic-gate 		(((tte) >> (IOMMU_TTE_CTX_SHIFT - 32)) & IOMMU_CTX_MASK)
222946Smathue #define	MP2CTX(mp)	IOMMU_TTE2CTX((uint32_t)(uintptr_t)(mp)->dmai_tte)
2230Sstevel@tonic-gate 
2240Sstevel@tonic-gate /* dvma debug */
2250Sstevel@tonic-gate #define	DVMA_DBG_ON(iommu_p)  \
2260Sstevel@tonic-gate 	((1ull << (iommu_p)->iommu_inst) & pci_dvma_debug_on)
2270Sstevel@tonic-gate #define	DVMA_DBG_OFF(iommu_p) \
2280Sstevel@tonic-gate 	((1ull << (iommu_p)->iommu_inst) & pci_dvma_debug_off)
2290Sstevel@tonic-gate 
2300Sstevel@tonic-gate extern void pci_dvma_debug_fini(iommu_t *iommu_p);
2310Sstevel@tonic-gate extern void pci_dvma_alloc_debug(iommu_t *iommu_p, char *address, uint_t len,
2320Sstevel@tonic-gate 	ddi_dma_impl_t *mp);
2330Sstevel@tonic-gate extern void pci_dvma_free_debug(iommu_t *iommu_p, char *address, uint_t len,
2340Sstevel@tonic-gate 	ddi_dma_impl_t *mp);
2350Sstevel@tonic-gate 
2360Sstevel@tonic-gate /* dvma routines */
2370Sstevel@tonic-gate extern void iommu_map_pages(iommu_t *iommu_p, ddi_dma_impl_t *mp,
2380Sstevel@tonic-gate 			dvma_addr_t dvma_pg, size_t npages, size_t pfn_index);
2390Sstevel@tonic-gate extern void iommu_unmap_pages(iommu_t *iommu_p, dvma_addr_t dvma_pg,
2400Sstevel@tonic-gate 			uint_t npages);
2410Sstevel@tonic-gate extern void iommu_remap_pages(iommu_t *iommu_p, ddi_dma_impl_t *mp,
2420Sstevel@tonic-gate 			dvma_addr_t dvma_pg, size_t npages, size_t pfn_index);
2430Sstevel@tonic-gate extern void iommu_map_window(iommu_t *iommu_p,
2440Sstevel@tonic-gate 			ddi_dma_impl_t *mp, window_t window);
2450Sstevel@tonic-gate extern void iommu_unmap_window(iommu_t *iommu_p, ddi_dma_impl_t *mp);
2460Sstevel@tonic-gate 
2470Sstevel@tonic-gate /* iommu initialization routines */
2480Sstevel@tonic-gate extern void iommu_configure(iommu_t *iommu_p);
2490Sstevel@tonic-gate extern void iommu_create(pci_t *pci_p);
2500Sstevel@tonic-gate extern void iommu_destroy(pci_t *pci_p);
2510Sstevel@tonic-gate extern uint_t iommu_tsb_size_encode(uint_t tsb_bytes);
2520Sstevel@tonic-gate 
2530Sstevel@tonic-gate /* TSB allocate/free */
2540Sstevel@tonic-gate extern int pci_alloc_tsb(pci_t *pci_p);
2550Sstevel@tonic-gate extern void pci_free_tsb(pci_t *pci_p);
2560Sstevel@tonic-gate 
2570Sstevel@tonic-gate #ifdef	__cplusplus
2580Sstevel@tonic-gate }
2590Sstevel@tonic-gate #endif
2600Sstevel@tonic-gate 
2610Sstevel@tonic-gate #endif	/* _SYS_PCI_IOMMU_H */
262