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 5*49f91442Ssuha * Common Development and Distribution License (the "License"). 6*49f91442Ssuha * 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*49f91442Ssuha * Copyright 2006 Sun Microsystems, Inc. All rights reserved. 237c478bd9Sstevel@tonic-gate * Use is subject to license terms. 247c478bd9Sstevel@tonic-gate */ 257c478bd9Sstevel@tonic-gate 267c478bd9Sstevel@tonic-gate #ifndef _SYS_PCI_IOMMU_H 277c478bd9Sstevel@tonic-gate #define _SYS_PCI_IOMMU_H 287c478bd9Sstevel@tonic-gate 297c478bd9Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI" 307c478bd9Sstevel@tonic-gate 317c478bd9Sstevel@tonic-gate #ifdef __cplusplus 327c478bd9Sstevel@tonic-gate extern "C" { 337c478bd9Sstevel@tonic-gate #endif 347c478bd9Sstevel@tonic-gate 357c478bd9Sstevel@tonic-gate #include <sys/vmem.h> 367c478bd9Sstevel@tonic-gate 377c478bd9Sstevel@tonic-gate typedef uint64_t dvma_addr_t; 387c478bd9Sstevel@tonic-gate typedef uint64_t dma_bypass_addr_t; 397c478bd9Sstevel@tonic-gate typedef uint64_t dma_peer_addr_t; 407c478bd9Sstevel@tonic-gate typedef uint16_t dvma_context_t; 417c478bd9Sstevel@tonic-gate typedef uint64_t window_t; 427c478bd9Sstevel@tonic-gate 437c478bd9Sstevel@tonic-gate /* 447c478bd9Sstevel@tonic-gate * The following typedef's represents the types for DMA transactions 457c478bd9Sstevel@tonic-gate * and corresponding DMA addresses supported by psycho/schizo. 467c478bd9Sstevel@tonic-gate */ 477c478bd9Sstevel@tonic-gate typedef enum { IOMMU_XLATE, IOMMU_BYPASS, PCI_PEER_TO_PEER } iommu_dma_t; 487c478bd9Sstevel@tonic-gate 497c478bd9Sstevel@tonic-gate /* 507c478bd9Sstevel@tonic-gate * The following macros define the iommu page size and related operations. 517c478bd9Sstevel@tonic-gate */ 527c478bd9Sstevel@tonic-gate #define IOMMU_PAGE_SHIFT 13 537c478bd9Sstevel@tonic-gate #define IOMMU_PAGE_SIZE (1 << IOMMU_PAGE_SHIFT) 547c478bd9Sstevel@tonic-gate #define IOMMU_PAGE_MASK ~(IOMMU_PAGE_SIZE - 1) 557c478bd9Sstevel@tonic-gate #define IOMMU_PAGE_OFFSET (IOMMU_PAGE_SIZE - 1) 567c478bd9Sstevel@tonic-gate #define IOMMU_PTOB(x) (((uint64_t)(x)) << IOMMU_PAGE_SHIFT) 577c478bd9Sstevel@tonic-gate #define IOMMU_BTOP(x) ((x) >> IOMMU_PAGE_SHIFT) 587c478bd9Sstevel@tonic-gate #define IOMMU_BTOPR(x) IOMMU_BTOP((x) + IOMMU_PAGE_OFFSET) 597c478bd9Sstevel@tonic-gate 607c478bd9Sstevel@tonic-gate /* 617c478bd9Sstevel@tonic-gate * control register decoding 627c478bd9Sstevel@tonic-gate */ 637c478bd9Sstevel@tonic-gate /* tsb size: 0=1k 1=2k 2=4k 3=8k 4=16k 5=32k 6=64k 7=128k */ 647c478bd9Sstevel@tonic-gate #define IOMMU_CTL_TO_TSBSIZE(ctl) ((ctl) >> 16) 657c478bd9Sstevel@tonic-gate #define IOMMU_TSBSIZE_TO_TSBENTRIES(s) ((1 << (s)) << (13 - 3)) 667c478bd9Sstevel@tonic-gate #define IOMMU_DARWIN_BOGUS_TSBSIZE 7 677c478bd9Sstevel@tonic-gate 687c478bd9Sstevel@tonic-gate /* 697c478bd9Sstevel@tonic-gate * boiler plate for tte (everything except the pfn) 707c478bd9Sstevel@tonic-gate */ 717c478bd9Sstevel@tonic-gate #define MAKE_TTE_TEMPLATE(pfn, mp) (COMMON_IOMMU_TTE_V | \ 727c478bd9Sstevel@tonic-gate (pf_is_memory(pfn) ? COMMON_IOMMU_TTE_C : 0) | \ 737c478bd9Sstevel@tonic-gate ((mp->dmai_rflags & DDI_DMA_READ) ? COMMON_IOMMU_TTE_W : 0) | \ 747c478bd9Sstevel@tonic-gate ((mp->dmai_rflags & DDI_DMA_CONSISTENT) ? 0 : COMMON_IOMMU_TTE_S)) 757c478bd9Sstevel@tonic-gate #define TTE_IS_INVALID(tte) (((tte) & COMMON_IOMMU_TTE_V) == 0x0ull) 767c478bd9Sstevel@tonic-gate 777c478bd9Sstevel@tonic-gate /* 787c478bd9Sstevel@tonic-gate * The following macros define the address ranges supported for DVMA 797c478bd9Sstevel@tonic-gate * and iommu bypass transfers. 807c478bd9Sstevel@tonic-gate */ 817c478bd9Sstevel@tonic-gate #define COMMON_IOMMU_BYPASS_BASE 0xFFFC000000000000ull 82*49f91442Ssuha 83*49f91442Ssuha /* 84*49f91442Ssuha * The IOMMU_BYPASS_END is ASIC dependent and so defined in the appropriate 85*49f91442Ssuha * header file. 86*49f91442Ssuha */ 877c478bd9Sstevel@tonic-gate 887c478bd9Sstevel@tonic-gate /* 897c478bd9Sstevel@tonic-gate * For iommu bypass addresses, bit 43 specifies cacheability. 907c478bd9Sstevel@tonic-gate */ 917c478bd9Sstevel@tonic-gate #define COMMON_IOMMU_BYPASS_NONCACHE 0x0000080000000000ull 927c478bd9Sstevel@tonic-gate 937c478bd9Sstevel@tonic-gate /* 947c478bd9Sstevel@tonic-gate * Generic iommu definitions and types: 957c478bd9Sstevel@tonic-gate */ 967c478bd9Sstevel@tonic-gate #define IOMMU_TLB_ENTRIES 16 977c478bd9Sstevel@tonic-gate 987c478bd9Sstevel@tonic-gate /* 997c478bd9Sstevel@tonic-gate * The following macros are for loading and unloading iotte 1007c478bd9Sstevel@tonic-gate * entries. 1017c478bd9Sstevel@tonic-gate */ 1027c478bd9Sstevel@tonic-gate #define COMMON_IOMMU_TTE_SIZE 8 1037c478bd9Sstevel@tonic-gate #define COMMON_IOMMU_TTE_V 0x8000000000000000ull 1047c478bd9Sstevel@tonic-gate #define COMMON_IOMMU_TTE_S 0x1000000000000000ull 1057c478bd9Sstevel@tonic-gate #define COMMON_IOMMU_TTE_C 0x0000000000000010ull 1067c478bd9Sstevel@tonic-gate #define COMMON_IOMMU_TTE_W 0x0000000000000002ull 1077c478bd9Sstevel@tonic-gate #define COMMON_IOMMU_INVALID_TTE 0x0000000000000000ull 1087c478bd9Sstevel@tonic-gate 1097c478bd9Sstevel@tonic-gate /* 1107c478bd9Sstevel@tonic-gate * Tomatillo's micro TLB bug. errata #82 1117c478bd9Sstevel@tonic-gate */ 1127c478bd9Sstevel@tonic-gate typedef struct dvma_unbind_req { 1137c478bd9Sstevel@tonic-gate uint32_t dur_base; 1147c478bd9Sstevel@tonic-gate uint_t dur_npg; 1157c478bd9Sstevel@tonic-gate uint_t dur_flags; /* = dmai_flags & DMAI_FLAGS_VMEMCACHE */ 1167c478bd9Sstevel@tonic-gate } dvma_unbind_req_t; 1177c478bd9Sstevel@tonic-gate 1187c478bd9Sstevel@tonic-gate /* 1197c478bd9Sstevel@tonic-gate * iommu block soft state structure: 1207c478bd9Sstevel@tonic-gate * 1217c478bd9Sstevel@tonic-gate * Each pci node may share an iommu block structure with its peer 1227c478bd9Sstevel@tonic-gate * node of have its own private iommu block structure. 1237c478bd9Sstevel@tonic-gate */ 1247c478bd9Sstevel@tonic-gate typedef struct iommu iommu_t; 1257c478bd9Sstevel@tonic-gate struct iommu { 1267c478bd9Sstevel@tonic-gate 1277c478bd9Sstevel@tonic-gate pci_t *iommu_pci_p; /* link back to pci soft state */ 1287c478bd9Sstevel@tonic-gate int iommu_inst; /* ddi_get_instance(iommu_pci_p->pci_dip) */ 1297c478bd9Sstevel@tonic-gate 1307c478bd9Sstevel@tonic-gate volatile uint64_t *iommu_ctrl_reg; 1317c478bd9Sstevel@tonic-gate volatile uint64_t *iommu_tsb_base_addr_reg; 1327c478bd9Sstevel@tonic-gate volatile uint64_t *iommu_flush_page_reg; 1337c478bd9Sstevel@tonic-gate volatile uint64_t *iommu_flush_ctx_reg; /* schizo only */ 1347c478bd9Sstevel@tonic-gate volatile uint64_t *iommu_tfar_reg; /* tomatillo only */ 1357c478bd9Sstevel@tonic-gate 1367c478bd9Sstevel@tonic-gate /* 1377c478bd9Sstevel@tonic-gate * virtual and physical addresses and size of the iommu tsb: 1387c478bd9Sstevel@tonic-gate */ 1397c478bd9Sstevel@tonic-gate uint64_t *iommu_tsb_vaddr; 1407c478bd9Sstevel@tonic-gate uint64_t iommu_tsb_paddr; 1417c478bd9Sstevel@tonic-gate uint_t iommu_tsb_entries; 1427c478bd9Sstevel@tonic-gate uint_t iommu_tsb_size; 1437c478bd9Sstevel@tonic-gate 1447c478bd9Sstevel@tonic-gate /* 1457c478bd9Sstevel@tonic-gate * address ranges of dvma space: 1467c478bd9Sstevel@tonic-gate */ 1477c478bd9Sstevel@tonic-gate dvma_addr_t iommu_dvma_base; 1487c478bd9Sstevel@tonic-gate dvma_addr_t iommu_dvma_end; 1497c478bd9Sstevel@tonic-gate dvma_addr_t iommu_dvma_fast_end; 1507c478bd9Sstevel@tonic-gate dvma_addr_t dvma_base_pg; /* = IOMMU_BTOP(iommu_dvma_base) */ 1517c478bd9Sstevel@tonic-gate dvma_addr_t dvma_end_pg; /* = IOMMU_BTOP(iommu_dvma_end) */ 1527c478bd9Sstevel@tonic-gate 1537c478bd9Sstevel@tonic-gate /* 1547c478bd9Sstevel@tonic-gate * address ranges of dma bypass space: 1557c478bd9Sstevel@tonic-gate */ 1567c478bd9Sstevel@tonic-gate dma_bypass_addr_t iommu_dma_bypass_base; 1577c478bd9Sstevel@tonic-gate dma_bypass_addr_t iommu_dma_bypass_end; 1587c478bd9Sstevel@tonic-gate 1597c478bd9Sstevel@tonic-gate /* 1607c478bd9Sstevel@tonic-gate * virtual memory map and callback id for dvma space: 1617c478bd9Sstevel@tonic-gate */ 1627c478bd9Sstevel@tonic-gate vmem_t *iommu_dvma_map; 1637c478bd9Sstevel@tonic-gate uintptr_t iommu_dvma_clid; 1647c478bd9Sstevel@tonic-gate 1657c478bd9Sstevel@tonic-gate /* 1667c478bd9Sstevel@tonic-gate * fields for fast dvma interfaces: 1677c478bd9Sstevel@tonic-gate */ 1687c478bd9Sstevel@tonic-gate ulong_t iommu_dvma_reserve; 1697c478bd9Sstevel@tonic-gate 1707c478bd9Sstevel@tonic-gate /* 1717c478bd9Sstevel@tonic-gate * dvma fast track page cache byte map 1727c478bd9Sstevel@tonic-gate */ 1737c478bd9Sstevel@tonic-gate uint8_t *iommu_dvma_cache_locks; 1747c478bd9Sstevel@tonic-gate uint_t iommu_dvma_addr_scan_start; 1757c478bd9Sstevel@tonic-gate 1767c478bd9Sstevel@tonic-gate /* 1777c478bd9Sstevel@tonic-gate * dvma context bitmap 1787c478bd9Sstevel@tonic-gate */ 1797c478bd9Sstevel@tonic-gate uint64_t *iommu_ctx_bitmap; 1807c478bd9Sstevel@tonic-gate 1817c478bd9Sstevel@tonic-gate /* 1827c478bd9Sstevel@tonic-gate * dvma debug 1837c478bd9Sstevel@tonic-gate */ 1847c478bd9Sstevel@tonic-gate kmutex_t dvma_debug_lock; 1857c478bd9Sstevel@tonic-gate uint32_t dvma_alloc_rec_index; 1867c478bd9Sstevel@tonic-gate uint32_t dvma_free_rec_index; 1877c478bd9Sstevel@tonic-gate uint32_t dvma_active_count; 1887c478bd9Sstevel@tonic-gate 1897c478bd9Sstevel@tonic-gate struct dvma_rec *dvma_alloc_rec; 1907c478bd9Sstevel@tonic-gate struct dvma_rec *dvma_free_rec; 1917c478bd9Sstevel@tonic-gate struct dvma_rec *dvma_active_list; 1927c478bd9Sstevel@tonic-gate 1937c478bd9Sstevel@tonic-gate /* 1947c478bd9Sstevel@tonic-gate * tomatillo's micro TLB bug. errata #82 1957c478bd9Sstevel@tonic-gate */ 1967c478bd9Sstevel@tonic-gate dvma_unbind_req_t *iommu_mtlb_req_p; /* unbind requests */ 1977c478bd9Sstevel@tonic-gate uint32_t iommu_mtlb_maxpgs; /* GC threshold */ 1987c478bd9Sstevel@tonic-gate uint32_t iommu_mtlb_npgs; /* total page count */ 1997c478bd9Sstevel@tonic-gate uint32_t iommu_mtlb_nreq; /* total request count */ 2007c478bd9Sstevel@tonic-gate kmutex_t iommu_mtlb_lock; 2017c478bd9Sstevel@tonic-gate }; 2027c478bd9Sstevel@tonic-gate 2037c478bd9Sstevel@tonic-gate typedef struct pci_dvma_range_prop { 2047c478bd9Sstevel@tonic-gate uint32_t dvma_base; 2057c478bd9Sstevel@tonic-gate uint32_t dvma_len; 2067c478bd9Sstevel@tonic-gate } pci_dvma_range_prop_t; 2077c478bd9Sstevel@tonic-gate 2087c478bd9Sstevel@tonic-gate #define IOMMU_PAGE_INDEX(iommu_p, dvma_pg) ((dvma_pg) - (iommu_p)->dvma_base_pg) 2097c478bd9Sstevel@tonic-gate #define IOMMU_PAGE_FLUSH(iommu_p, dvma_pg) \ 2107c478bd9Sstevel@tonic-gate *(iommu_p)->iommu_flush_page_reg = IOMMU_PTOB(dvma_pg) 2117c478bd9Sstevel@tonic-gate #define IOMMU_UNLOAD_TTE(iommu_p, pg_index) \ 2127c478bd9Sstevel@tonic-gate (iommu_p)->iommu_tsb_vaddr[pg_index] = COMMON_IOMMU_INVALID_TTE 2137c478bd9Sstevel@tonic-gate #define IOMMU_PAGE_TTEPA(iommu_p, dvma_pg) \ 2147c478bd9Sstevel@tonic-gate ((iommu_p)->iommu_tsb_paddr + (IOMMU_PAGE_INDEX(iommu_p, dvma_pg) << 3)) 2157c478bd9Sstevel@tonic-gate 2167c478bd9Sstevel@tonic-gate #define IOMMU_CONTEXT_BITS 12 2177c478bd9Sstevel@tonic-gate #define IOMMU_CTX_MASK ((1 << IOMMU_CONTEXT_BITS) - 1) 2187c478bd9Sstevel@tonic-gate #define IOMMU_TTE_CTX_SHIFT 47 2197c478bd9Sstevel@tonic-gate #define IOMMU_CTX2TTE(ctx) (((uint64_t)(ctx)) << IOMMU_TTE_CTX_SHIFT) 2207c478bd9Sstevel@tonic-gate #define IOMMU_TTE2CTX(tte) \ 2217c478bd9Sstevel@tonic-gate (((tte) >> (IOMMU_TTE_CTX_SHIFT - 32)) & IOMMU_CTX_MASK) 222f47a9c50Smathue #define MP2CTX(mp) IOMMU_TTE2CTX((uint32_t)(uintptr_t)(mp)->dmai_tte) 2237c478bd9Sstevel@tonic-gate 2247c478bd9Sstevel@tonic-gate /* dvma debug */ 2257c478bd9Sstevel@tonic-gate #define DVMA_DBG_ON(iommu_p) \ 2267c478bd9Sstevel@tonic-gate ((1ull << (iommu_p)->iommu_inst) & pci_dvma_debug_on) 2277c478bd9Sstevel@tonic-gate #define DVMA_DBG_OFF(iommu_p) \ 2287c478bd9Sstevel@tonic-gate ((1ull << (iommu_p)->iommu_inst) & pci_dvma_debug_off) 2297c478bd9Sstevel@tonic-gate 2307c478bd9Sstevel@tonic-gate extern void pci_dvma_debug_fini(iommu_t *iommu_p); 2317c478bd9Sstevel@tonic-gate extern void pci_dvma_alloc_debug(iommu_t *iommu_p, char *address, uint_t len, 2327c478bd9Sstevel@tonic-gate ddi_dma_impl_t *mp); 2337c478bd9Sstevel@tonic-gate extern void pci_dvma_free_debug(iommu_t *iommu_p, char *address, uint_t len, 2347c478bd9Sstevel@tonic-gate ddi_dma_impl_t *mp); 2357c478bd9Sstevel@tonic-gate 2367c478bd9Sstevel@tonic-gate /* dvma routines */ 2377c478bd9Sstevel@tonic-gate extern void iommu_map_pages(iommu_t *iommu_p, ddi_dma_impl_t *mp, 2387c478bd9Sstevel@tonic-gate dvma_addr_t dvma_pg, size_t npages, size_t pfn_index); 2397c478bd9Sstevel@tonic-gate extern void iommu_unmap_pages(iommu_t *iommu_p, dvma_addr_t dvma_pg, 2407c478bd9Sstevel@tonic-gate uint_t npages); 2417c478bd9Sstevel@tonic-gate extern void iommu_remap_pages(iommu_t *iommu_p, ddi_dma_impl_t *mp, 2427c478bd9Sstevel@tonic-gate dvma_addr_t dvma_pg, size_t npages, size_t pfn_index); 2437c478bd9Sstevel@tonic-gate extern void iommu_map_window(iommu_t *iommu_p, 2447c478bd9Sstevel@tonic-gate ddi_dma_impl_t *mp, window_t window); 2457c478bd9Sstevel@tonic-gate extern void iommu_unmap_window(iommu_t *iommu_p, ddi_dma_impl_t *mp); 2467c478bd9Sstevel@tonic-gate 2477c478bd9Sstevel@tonic-gate /* iommu initialization routines */ 2487c478bd9Sstevel@tonic-gate extern void iommu_configure(iommu_t *iommu_p); 2497c478bd9Sstevel@tonic-gate extern void iommu_create(pci_t *pci_p); 2507c478bd9Sstevel@tonic-gate extern void iommu_destroy(pci_t *pci_p); 2517c478bd9Sstevel@tonic-gate extern uint_t iommu_tsb_size_encode(uint_t tsb_bytes); 2527c478bd9Sstevel@tonic-gate 2537c478bd9Sstevel@tonic-gate /* TSB allocate/free */ 2547c478bd9Sstevel@tonic-gate extern int pci_alloc_tsb(pci_t *pci_p); 2557c478bd9Sstevel@tonic-gate extern void pci_free_tsb(pci_t *pci_p); 2567c478bd9Sstevel@tonic-gate 2577c478bd9Sstevel@tonic-gate #ifdef __cplusplus 2587c478bd9Sstevel@tonic-gate } 2597c478bd9Sstevel@tonic-gate #endif 2607c478bd9Sstevel@tonic-gate 2617c478bd9Sstevel@tonic-gate #endif /* _SYS_PCI_IOMMU_H */ 262