162306a36Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-or-later 262306a36Sopenharmony_ci/* 362306a36Sopenharmony_ci** ccio-dma.c: 462306a36Sopenharmony_ci** DMA management routines for first generation cache-coherent machines. 562306a36Sopenharmony_ci** Program U2/Uturn in "Virtual Mode" and use the I/O MMU. 662306a36Sopenharmony_ci** 762306a36Sopenharmony_ci** (c) Copyright 2000 Grant Grundler 862306a36Sopenharmony_ci** (c) Copyright 2000 Ryan Bradetich 962306a36Sopenharmony_ci** (c) Copyright 2000 Hewlett-Packard Company 1062306a36Sopenharmony_ci** 1162306a36Sopenharmony_ci** "Real Mode" operation refers to U2/Uturn chip operation. 1262306a36Sopenharmony_ci** U2/Uturn were designed to perform coherency checks w/o using 1362306a36Sopenharmony_ci** the I/O MMU - basically what x86 does. 1462306a36Sopenharmony_ci** 1562306a36Sopenharmony_ci** Drawbacks of using Real Mode are: 1662306a36Sopenharmony_ci** o outbound DMA is slower - U2 won't prefetch data (GSC+ XQL signal). 1762306a36Sopenharmony_ci** o Inbound DMA less efficient - U2 can't use DMA_FAST attribute. 1862306a36Sopenharmony_ci** o Ability to do scatter/gather in HW is lost. 1962306a36Sopenharmony_ci** o Doesn't work under PCX-U/U+ machines since they didn't follow 2062306a36Sopenharmony_ci** the coherency design originally worked out. Only PCX-W does. 2162306a36Sopenharmony_ci*/ 2262306a36Sopenharmony_ci 2362306a36Sopenharmony_ci#include <linux/types.h> 2462306a36Sopenharmony_ci#include <linux/kernel.h> 2562306a36Sopenharmony_ci#include <linux/init.h> 2662306a36Sopenharmony_ci#include <linux/mm.h> 2762306a36Sopenharmony_ci#include <linux/spinlock.h> 2862306a36Sopenharmony_ci#include <linux/slab.h> 2962306a36Sopenharmony_ci#include <linux/string.h> 3062306a36Sopenharmony_ci#include <linux/pci.h> 3162306a36Sopenharmony_ci#include <linux/reboot.h> 3262306a36Sopenharmony_ci#include <linux/proc_fs.h> 3362306a36Sopenharmony_ci#include <linux/seq_file.h> 3462306a36Sopenharmony_ci#include <linux/dma-map-ops.h> 3562306a36Sopenharmony_ci#include <linux/scatterlist.h> 3662306a36Sopenharmony_ci#include <linux/iommu-helper.h> 3762306a36Sopenharmony_ci#include <linux/export.h> 3862306a36Sopenharmony_ci 3962306a36Sopenharmony_ci#include <asm/byteorder.h> 4062306a36Sopenharmony_ci#include <asm/cache.h> /* for L1_CACHE_BYTES */ 4162306a36Sopenharmony_ci#include <linux/uaccess.h> 4262306a36Sopenharmony_ci#include <asm/page.h> 4362306a36Sopenharmony_ci#include <asm/dma.h> 4462306a36Sopenharmony_ci#include <asm/io.h> 4562306a36Sopenharmony_ci#include <asm/hardware.h> /* for register_module() */ 4662306a36Sopenharmony_ci#include <asm/parisc-device.h> 4762306a36Sopenharmony_ci 4862306a36Sopenharmony_ci#include "iommu.h" 4962306a36Sopenharmony_ci 5062306a36Sopenharmony_ci/* 5162306a36Sopenharmony_ci** Choose "ccio" since that's what HP-UX calls it. 5262306a36Sopenharmony_ci** Make it easier for folks to migrate from one to the other :^) 5362306a36Sopenharmony_ci*/ 5462306a36Sopenharmony_ci#define MODULE_NAME "ccio" 5562306a36Sopenharmony_ci 5662306a36Sopenharmony_ci#undef DEBUG_CCIO_RES 5762306a36Sopenharmony_ci#undef DEBUG_CCIO_RUN 5862306a36Sopenharmony_ci#undef DEBUG_CCIO_INIT 5962306a36Sopenharmony_ci#undef DEBUG_CCIO_RUN_SG 6062306a36Sopenharmony_ci 6162306a36Sopenharmony_ci#ifdef CONFIG_PROC_FS 6262306a36Sopenharmony_ci/* depends on proc fs support. But costs CPU performance. */ 6362306a36Sopenharmony_ci#undef CCIO_COLLECT_STATS 6462306a36Sopenharmony_ci#endif 6562306a36Sopenharmony_ci 6662306a36Sopenharmony_ci#ifdef DEBUG_CCIO_INIT 6762306a36Sopenharmony_ci#define DBG_INIT(x...) printk(x) 6862306a36Sopenharmony_ci#else 6962306a36Sopenharmony_ci#define DBG_INIT(x...) 7062306a36Sopenharmony_ci#endif 7162306a36Sopenharmony_ci 7262306a36Sopenharmony_ci#ifdef DEBUG_CCIO_RUN 7362306a36Sopenharmony_ci#define DBG_RUN(x...) printk(x) 7462306a36Sopenharmony_ci#else 7562306a36Sopenharmony_ci#define DBG_RUN(x...) 7662306a36Sopenharmony_ci#endif 7762306a36Sopenharmony_ci 7862306a36Sopenharmony_ci#ifdef DEBUG_CCIO_RES 7962306a36Sopenharmony_ci#define DBG_RES(x...) printk(x) 8062306a36Sopenharmony_ci#else 8162306a36Sopenharmony_ci#define DBG_RES(x...) 8262306a36Sopenharmony_ci#endif 8362306a36Sopenharmony_ci 8462306a36Sopenharmony_ci#ifdef DEBUG_CCIO_RUN_SG 8562306a36Sopenharmony_ci#define DBG_RUN_SG(x...) printk(x) 8662306a36Sopenharmony_ci#else 8762306a36Sopenharmony_ci#define DBG_RUN_SG(x...) 8862306a36Sopenharmony_ci#endif 8962306a36Sopenharmony_ci 9062306a36Sopenharmony_ci#define WRITE_U32(value, addr) __raw_writel(value, addr) 9162306a36Sopenharmony_ci#define READ_U32(addr) __raw_readl(addr) 9262306a36Sopenharmony_ci 9362306a36Sopenharmony_ci#define U2_IOA_RUNWAY 0x580 9462306a36Sopenharmony_ci#define U2_BC_GSC 0x501 9562306a36Sopenharmony_ci#define UTURN_IOA_RUNWAY 0x581 9662306a36Sopenharmony_ci#define UTURN_BC_GSC 0x502 9762306a36Sopenharmony_ci 9862306a36Sopenharmony_ci#define IOA_NORMAL_MODE 0x00020080 /* IO_CONTROL to turn on CCIO */ 9962306a36Sopenharmony_ci#define CMD_TLB_DIRECT_WRITE 35 /* IO_COMMAND for I/O TLB Writes */ 10062306a36Sopenharmony_ci#define CMD_TLB_PURGE 33 /* IO_COMMAND to Purge I/O TLB entry */ 10162306a36Sopenharmony_ci 10262306a36Sopenharmony_cistruct ioa_registers { 10362306a36Sopenharmony_ci /* Runway Supervisory Set */ 10462306a36Sopenharmony_ci int32_t unused1[12]; 10562306a36Sopenharmony_ci uint32_t io_command; /* Offset 12 */ 10662306a36Sopenharmony_ci uint32_t io_status; /* Offset 13 */ 10762306a36Sopenharmony_ci uint32_t io_control; /* Offset 14 */ 10862306a36Sopenharmony_ci int32_t unused2[1]; 10962306a36Sopenharmony_ci 11062306a36Sopenharmony_ci /* Runway Auxiliary Register Set */ 11162306a36Sopenharmony_ci uint32_t io_err_resp; /* Offset 0 */ 11262306a36Sopenharmony_ci uint32_t io_err_info; /* Offset 1 */ 11362306a36Sopenharmony_ci uint32_t io_err_req; /* Offset 2 */ 11462306a36Sopenharmony_ci uint32_t io_err_resp_hi; /* Offset 3 */ 11562306a36Sopenharmony_ci uint32_t io_tlb_entry_m; /* Offset 4 */ 11662306a36Sopenharmony_ci uint32_t io_tlb_entry_l; /* Offset 5 */ 11762306a36Sopenharmony_ci uint32_t unused3[1]; 11862306a36Sopenharmony_ci uint32_t io_pdir_base; /* Offset 7 */ 11962306a36Sopenharmony_ci uint32_t io_io_low_hv; /* Offset 8 */ 12062306a36Sopenharmony_ci uint32_t io_io_high_hv; /* Offset 9 */ 12162306a36Sopenharmony_ci uint32_t unused4[1]; 12262306a36Sopenharmony_ci uint32_t io_chain_id_mask; /* Offset 11 */ 12362306a36Sopenharmony_ci uint32_t unused5[2]; 12462306a36Sopenharmony_ci uint32_t io_io_low; /* Offset 14 */ 12562306a36Sopenharmony_ci uint32_t io_io_high; /* Offset 15 */ 12662306a36Sopenharmony_ci}; 12762306a36Sopenharmony_ci 12862306a36Sopenharmony_ci/* 12962306a36Sopenharmony_ci** IOA Registers 13062306a36Sopenharmony_ci** ------------- 13162306a36Sopenharmony_ci** 13262306a36Sopenharmony_ci** Runway IO_CONTROL Register (+0x38) 13362306a36Sopenharmony_ci** 13462306a36Sopenharmony_ci** The Runway IO_CONTROL register controls the forwarding of transactions. 13562306a36Sopenharmony_ci** 13662306a36Sopenharmony_ci** | 0 ... 13 | 14 15 | 16 ... 21 | 22 | 23 24 | 25 ... 31 | 13762306a36Sopenharmony_ci** | HV | TLB | reserved | HV | mode | reserved | 13862306a36Sopenharmony_ci** 13962306a36Sopenharmony_ci** o mode field indicates the address translation of transactions 14062306a36Sopenharmony_ci** forwarded from Runway to GSC+: 14162306a36Sopenharmony_ci** Mode Name Value Definition 14262306a36Sopenharmony_ci** Off (default) 0 Opaque to matching addresses. 14362306a36Sopenharmony_ci** Include 1 Transparent for matching addresses. 14462306a36Sopenharmony_ci** Peek 3 Map matching addresses. 14562306a36Sopenharmony_ci** 14662306a36Sopenharmony_ci** + "Off" mode: Runway transactions which match the I/O range 14762306a36Sopenharmony_ci** specified by the IO_IO_LOW/IO_IO_HIGH registers will be ignored. 14862306a36Sopenharmony_ci** + "Include" mode: all addresses within the I/O range specified 14962306a36Sopenharmony_ci** by the IO_IO_LOW and IO_IO_HIGH registers are transparently 15062306a36Sopenharmony_ci** forwarded. This is the I/O Adapter's normal operating mode. 15162306a36Sopenharmony_ci** + "Peek" mode: used during system configuration to initialize the 15262306a36Sopenharmony_ci** GSC+ bus. Runway Write_Shorts in the address range specified by 15362306a36Sopenharmony_ci** IO_IO_LOW and IO_IO_HIGH are forwarded through the I/O Adapter 15462306a36Sopenharmony_ci** *AND* the GSC+ address is remapped to the Broadcast Physical 15562306a36Sopenharmony_ci** Address space by setting the 14 high order address bits of the 15662306a36Sopenharmony_ci** 32 bit GSC+ address to ones. 15762306a36Sopenharmony_ci** 15862306a36Sopenharmony_ci** o TLB field affects transactions which are forwarded from GSC+ to Runway. 15962306a36Sopenharmony_ci** "Real" mode is the poweron default. 16062306a36Sopenharmony_ci** 16162306a36Sopenharmony_ci** TLB Mode Value Description 16262306a36Sopenharmony_ci** Real 0 No TLB translation. Address is directly mapped and the 16362306a36Sopenharmony_ci** virtual address is composed of selected physical bits. 16462306a36Sopenharmony_ci** Error 1 Software fills the TLB manually. 16562306a36Sopenharmony_ci** Normal 2 IOA fetches IO TLB misses from IO PDIR (in host memory). 16662306a36Sopenharmony_ci** 16762306a36Sopenharmony_ci** 16862306a36Sopenharmony_ci** IO_IO_LOW_HV +0x60 (HV dependent) 16962306a36Sopenharmony_ci** IO_IO_HIGH_HV +0x64 (HV dependent) 17062306a36Sopenharmony_ci** IO_IO_LOW +0x78 (Architected register) 17162306a36Sopenharmony_ci** IO_IO_HIGH +0x7c (Architected register) 17262306a36Sopenharmony_ci** 17362306a36Sopenharmony_ci** IO_IO_LOW and IO_IO_HIGH set the lower and upper bounds of the 17462306a36Sopenharmony_ci** I/O Adapter address space, respectively. 17562306a36Sopenharmony_ci** 17662306a36Sopenharmony_ci** 0 ... 7 | 8 ... 15 | 16 ... 31 | 17762306a36Sopenharmony_ci** 11111111 | 11111111 | address | 17862306a36Sopenharmony_ci** 17962306a36Sopenharmony_ci** Each LOW/HIGH pair describes a disjoint address space region. 18062306a36Sopenharmony_ci** (2 per GSC+ port). Each incoming Runway transaction address is compared 18162306a36Sopenharmony_ci** with both sets of LOW/HIGH registers. If the address is in the range 18262306a36Sopenharmony_ci** greater than or equal to IO_IO_LOW and less than IO_IO_HIGH the transaction 18362306a36Sopenharmony_ci** for forwarded to the respective GSC+ bus. 18462306a36Sopenharmony_ci** Specify IO_IO_LOW equal to or greater than IO_IO_HIGH to avoid specifying 18562306a36Sopenharmony_ci** an address space region. 18662306a36Sopenharmony_ci** 18762306a36Sopenharmony_ci** In order for a Runway address to reside within GSC+ extended address space: 18862306a36Sopenharmony_ci** Runway Address [0:7] must identically compare to 8'b11111111 18962306a36Sopenharmony_ci** Runway Address [8:11] must be equal to IO_IO_LOW(_HV)[16:19] 19062306a36Sopenharmony_ci** Runway Address [12:23] must be greater than or equal to 19162306a36Sopenharmony_ci** IO_IO_LOW(_HV)[20:31] and less than IO_IO_HIGH(_HV)[20:31]. 19262306a36Sopenharmony_ci** Runway Address [24:39] is not used in the comparison. 19362306a36Sopenharmony_ci** 19462306a36Sopenharmony_ci** When the Runway transaction is forwarded to GSC+, the GSC+ address is 19562306a36Sopenharmony_ci** as follows: 19662306a36Sopenharmony_ci** GSC+ Address[0:3] 4'b1111 19762306a36Sopenharmony_ci** GSC+ Address[4:29] Runway Address[12:37] 19862306a36Sopenharmony_ci** GSC+ Address[30:31] 2'b00 19962306a36Sopenharmony_ci** 20062306a36Sopenharmony_ci** All 4 Low/High registers must be initialized (by PDC) once the lower bus 20162306a36Sopenharmony_ci** is interrogated and address space is defined. The operating system will 20262306a36Sopenharmony_ci** modify the architectural IO_IO_LOW and IO_IO_HIGH registers following 20362306a36Sopenharmony_ci** the PDC initialization. However, the hardware version dependent IO_IO_LOW 20462306a36Sopenharmony_ci** and IO_IO_HIGH registers should not be subsequently altered by the OS. 20562306a36Sopenharmony_ci** 20662306a36Sopenharmony_ci** Writes to both sets of registers will take effect immediately, bypassing 20762306a36Sopenharmony_ci** the queues, which ensures that subsequent Runway transactions are checked 20862306a36Sopenharmony_ci** against the updated bounds values. However reads are queued, introducing 20962306a36Sopenharmony_ci** the possibility of a read being bypassed by a subsequent write to the same 21062306a36Sopenharmony_ci** register. This sequence can be avoided by having software wait for read 21162306a36Sopenharmony_ci** returns before issuing subsequent writes. 21262306a36Sopenharmony_ci*/ 21362306a36Sopenharmony_ci 21462306a36Sopenharmony_cistruct ioc { 21562306a36Sopenharmony_ci struct ioa_registers __iomem *ioc_regs; /* I/O MMU base address */ 21662306a36Sopenharmony_ci u8 *res_map; /* resource map, bit == pdir entry */ 21762306a36Sopenharmony_ci __le64 *pdir_base; /* physical base address */ 21862306a36Sopenharmony_ci u32 pdir_size; /* bytes, function of IOV Space size */ 21962306a36Sopenharmony_ci u32 res_hint; /* next available IOVP - 22062306a36Sopenharmony_ci circular search */ 22162306a36Sopenharmony_ci u32 res_size; /* size of resource map in bytes */ 22262306a36Sopenharmony_ci spinlock_t res_lock; 22362306a36Sopenharmony_ci 22462306a36Sopenharmony_ci#ifdef CCIO_COLLECT_STATS 22562306a36Sopenharmony_ci#define CCIO_SEARCH_SAMPLE 0x100 22662306a36Sopenharmony_ci unsigned long avg_search[CCIO_SEARCH_SAMPLE]; 22762306a36Sopenharmony_ci unsigned long avg_idx; /* current index into avg_search */ 22862306a36Sopenharmony_ci unsigned long used_pages; 22962306a36Sopenharmony_ci unsigned long msingle_calls; 23062306a36Sopenharmony_ci unsigned long msingle_pages; 23162306a36Sopenharmony_ci unsigned long msg_calls; 23262306a36Sopenharmony_ci unsigned long msg_pages; 23362306a36Sopenharmony_ci unsigned long usingle_calls; 23462306a36Sopenharmony_ci unsigned long usingle_pages; 23562306a36Sopenharmony_ci unsigned long usg_calls; 23662306a36Sopenharmony_ci unsigned long usg_pages; 23762306a36Sopenharmony_ci#endif 23862306a36Sopenharmony_ci unsigned short cujo20_bug; 23962306a36Sopenharmony_ci 24062306a36Sopenharmony_ci /* STUFF We don't need in performance path */ 24162306a36Sopenharmony_ci u32 chainid_shift; /* specify bit location of chain_id */ 24262306a36Sopenharmony_ci struct ioc *next; /* Linked list of discovered iocs */ 24362306a36Sopenharmony_ci const char *name; /* device name from firmware */ 24462306a36Sopenharmony_ci unsigned int hw_path; /* the hardware path this ioc is associatd with */ 24562306a36Sopenharmony_ci struct pci_dev *fake_pci_dev; /* the fake pci_dev for non-pci devs */ 24662306a36Sopenharmony_ci struct resource mmio_region[2]; /* The "routed" MMIO regions */ 24762306a36Sopenharmony_ci}; 24862306a36Sopenharmony_ci 24962306a36Sopenharmony_cistatic struct ioc *ioc_list; 25062306a36Sopenharmony_cistatic int ioc_count; 25162306a36Sopenharmony_ci 25262306a36Sopenharmony_ci/************************************************************** 25362306a36Sopenharmony_ci* 25462306a36Sopenharmony_ci* I/O Pdir Resource Management 25562306a36Sopenharmony_ci* 25662306a36Sopenharmony_ci* Bits set in the resource map are in use. 25762306a36Sopenharmony_ci* Each bit can represent a number of pages. 25862306a36Sopenharmony_ci* LSbs represent lower addresses (IOVA's). 25962306a36Sopenharmony_ci* 26062306a36Sopenharmony_ci* This was copied from sba_iommu.c. Don't try to unify 26162306a36Sopenharmony_ci* the two resource managers unless a way to have different 26262306a36Sopenharmony_ci* allocation policies is also adjusted. We'd like to avoid 26362306a36Sopenharmony_ci* I/O TLB thrashing by having resource allocation policy 26462306a36Sopenharmony_ci* match the I/O TLB replacement policy. 26562306a36Sopenharmony_ci* 26662306a36Sopenharmony_ci***************************************************************/ 26762306a36Sopenharmony_ci#define IOVP_SIZE PAGE_SIZE 26862306a36Sopenharmony_ci#define IOVP_SHIFT PAGE_SHIFT 26962306a36Sopenharmony_ci#define IOVP_MASK PAGE_MASK 27062306a36Sopenharmony_ci 27162306a36Sopenharmony_ci/* Convert from IOVP to IOVA and vice versa. */ 27262306a36Sopenharmony_ci#define CCIO_IOVA(iovp,offset) ((iovp) | (offset)) 27362306a36Sopenharmony_ci#define CCIO_IOVP(iova) ((iova) & IOVP_MASK) 27462306a36Sopenharmony_ci 27562306a36Sopenharmony_ci#define PDIR_INDEX(iovp) ((iovp)>>IOVP_SHIFT) 27662306a36Sopenharmony_ci#define MKIOVP(pdir_idx) ((long)(pdir_idx) << IOVP_SHIFT) 27762306a36Sopenharmony_ci#define MKIOVA(iovp,offset) (dma_addr_t)((long)iovp | (long)offset) 27862306a36Sopenharmony_ci 27962306a36Sopenharmony_ci/* 28062306a36Sopenharmony_ci** Don't worry about the 150% average search length on a miss. 28162306a36Sopenharmony_ci** If the search wraps around, and passes the res_hint, it will 28262306a36Sopenharmony_ci** cause the kernel to panic anyhow. 28362306a36Sopenharmony_ci*/ 28462306a36Sopenharmony_ci#define CCIO_SEARCH_LOOP(ioc, res_idx, mask, size) \ 28562306a36Sopenharmony_ci for (; res_ptr < res_end; ++res_ptr) { \ 28662306a36Sopenharmony_ci int ret;\ 28762306a36Sopenharmony_ci unsigned int idx;\ 28862306a36Sopenharmony_ci idx = (unsigned int)((unsigned long)res_ptr - (unsigned long)ioc->res_map); \ 28962306a36Sopenharmony_ci ret = iommu_is_span_boundary(idx << 3, pages_needed, 0, boundary_size);\ 29062306a36Sopenharmony_ci if ((0 == (*res_ptr & mask)) && !ret) { \ 29162306a36Sopenharmony_ci *res_ptr |= mask; \ 29262306a36Sopenharmony_ci res_idx = idx;\ 29362306a36Sopenharmony_ci ioc->res_hint = res_idx + (size >> 3); \ 29462306a36Sopenharmony_ci goto resource_found; \ 29562306a36Sopenharmony_ci } \ 29662306a36Sopenharmony_ci } 29762306a36Sopenharmony_ci 29862306a36Sopenharmony_ci#define CCIO_FIND_FREE_MAPPING(ioa, res_idx, mask, size) \ 29962306a36Sopenharmony_ci u##size *res_ptr = (u##size *)&((ioc)->res_map[ioa->res_hint & ~((size >> 3) - 1)]); \ 30062306a36Sopenharmony_ci u##size *res_end = (u##size *)&(ioc)->res_map[ioa->res_size]; \ 30162306a36Sopenharmony_ci CCIO_SEARCH_LOOP(ioc, res_idx, mask, size); \ 30262306a36Sopenharmony_ci res_ptr = (u##size *)&(ioc)->res_map[0]; \ 30362306a36Sopenharmony_ci CCIO_SEARCH_LOOP(ioa, res_idx, mask, size); 30462306a36Sopenharmony_ci 30562306a36Sopenharmony_ci/* 30662306a36Sopenharmony_ci** Find available bit in this ioa's resource map. 30762306a36Sopenharmony_ci** Use a "circular" search: 30862306a36Sopenharmony_ci** o Most IOVA's are "temporary" - avg search time should be small. 30962306a36Sopenharmony_ci** o keep a history of what happened for debugging 31062306a36Sopenharmony_ci** o KISS. 31162306a36Sopenharmony_ci** 31262306a36Sopenharmony_ci** Perf optimizations: 31362306a36Sopenharmony_ci** o search for log2(size) bits at a time. 31462306a36Sopenharmony_ci** o search for available resource bits using byte/word/whatever. 31562306a36Sopenharmony_ci** o use different search for "large" (eg > 4 pages) or "very large" 31662306a36Sopenharmony_ci** (eg > 16 pages) mappings. 31762306a36Sopenharmony_ci*/ 31862306a36Sopenharmony_ci 31962306a36Sopenharmony_ci/** 32062306a36Sopenharmony_ci * ccio_alloc_range - Allocate pages in the ioc's resource map. 32162306a36Sopenharmony_ci * @ioc: The I/O Controller. 32262306a36Sopenharmony_ci * @dev: The PCI device. 32362306a36Sopenharmony_ci * @size: The requested number of bytes to be mapped into the 32462306a36Sopenharmony_ci * I/O Pdir... 32562306a36Sopenharmony_ci * 32662306a36Sopenharmony_ci * This function searches the resource map of the ioc to locate a range 32762306a36Sopenharmony_ci * of available pages for the requested size. 32862306a36Sopenharmony_ci */ 32962306a36Sopenharmony_cistatic int 33062306a36Sopenharmony_ciccio_alloc_range(struct ioc *ioc, struct device *dev, size_t size) 33162306a36Sopenharmony_ci{ 33262306a36Sopenharmony_ci unsigned int pages_needed = size >> IOVP_SHIFT; 33362306a36Sopenharmony_ci unsigned int res_idx; 33462306a36Sopenharmony_ci unsigned long boundary_size; 33562306a36Sopenharmony_ci#ifdef CCIO_COLLECT_STATS 33662306a36Sopenharmony_ci unsigned long cr_start = mfctl(16); 33762306a36Sopenharmony_ci#endif 33862306a36Sopenharmony_ci 33962306a36Sopenharmony_ci BUG_ON(pages_needed == 0); 34062306a36Sopenharmony_ci BUG_ON((pages_needed * IOVP_SIZE) > DMA_CHUNK_SIZE); 34162306a36Sopenharmony_ci 34262306a36Sopenharmony_ci DBG_RES("%s() size: %zu pages_needed %d\n", 34362306a36Sopenharmony_ci __func__, size, pages_needed); 34462306a36Sopenharmony_ci 34562306a36Sopenharmony_ci /* 34662306a36Sopenharmony_ci ** "seek and ye shall find"...praying never hurts either... 34762306a36Sopenharmony_ci ** ggg sacrifices another 710 to the computer gods. 34862306a36Sopenharmony_ci */ 34962306a36Sopenharmony_ci 35062306a36Sopenharmony_ci boundary_size = dma_get_seg_boundary_nr_pages(dev, IOVP_SHIFT); 35162306a36Sopenharmony_ci 35262306a36Sopenharmony_ci if (pages_needed <= 8) { 35362306a36Sopenharmony_ci /* 35462306a36Sopenharmony_ci * LAN traffic will not thrash the TLB IFF the same NIC 35562306a36Sopenharmony_ci * uses 8 adjacent pages to map separate payload data. 35662306a36Sopenharmony_ci * ie the same byte in the resource bit map. 35762306a36Sopenharmony_ci */ 35862306a36Sopenharmony_ci#if 0 35962306a36Sopenharmony_ci /* FIXME: bit search should shift it's way through 36062306a36Sopenharmony_ci * an unsigned long - not byte at a time. As it is now, 36162306a36Sopenharmony_ci * we effectively allocate this byte to this mapping. 36262306a36Sopenharmony_ci */ 36362306a36Sopenharmony_ci unsigned long mask = ~(~0UL >> pages_needed); 36462306a36Sopenharmony_ci CCIO_FIND_FREE_MAPPING(ioc, res_idx, mask, 8); 36562306a36Sopenharmony_ci#else 36662306a36Sopenharmony_ci CCIO_FIND_FREE_MAPPING(ioc, res_idx, 0xff, 8); 36762306a36Sopenharmony_ci#endif 36862306a36Sopenharmony_ci } else if (pages_needed <= 16) { 36962306a36Sopenharmony_ci CCIO_FIND_FREE_MAPPING(ioc, res_idx, 0xffff, 16); 37062306a36Sopenharmony_ci } else if (pages_needed <= 32) { 37162306a36Sopenharmony_ci CCIO_FIND_FREE_MAPPING(ioc, res_idx, ~(unsigned int)0, 32); 37262306a36Sopenharmony_ci#ifdef __LP64__ 37362306a36Sopenharmony_ci } else if (pages_needed <= 64) { 37462306a36Sopenharmony_ci CCIO_FIND_FREE_MAPPING(ioc, res_idx, ~0UL, 64); 37562306a36Sopenharmony_ci#endif 37662306a36Sopenharmony_ci } else { 37762306a36Sopenharmony_ci panic("%s: %s() Too many pages to map. pages_needed: %u\n", 37862306a36Sopenharmony_ci __FILE__, __func__, pages_needed); 37962306a36Sopenharmony_ci } 38062306a36Sopenharmony_ci 38162306a36Sopenharmony_ci panic("%s: %s() I/O MMU is out of mapping resources.\n", __FILE__, 38262306a36Sopenharmony_ci __func__); 38362306a36Sopenharmony_ci 38462306a36Sopenharmony_ciresource_found: 38562306a36Sopenharmony_ci 38662306a36Sopenharmony_ci DBG_RES("%s() res_idx %d res_hint: %d\n", 38762306a36Sopenharmony_ci __func__, res_idx, ioc->res_hint); 38862306a36Sopenharmony_ci 38962306a36Sopenharmony_ci#ifdef CCIO_COLLECT_STATS 39062306a36Sopenharmony_ci { 39162306a36Sopenharmony_ci unsigned long cr_end = mfctl(16); 39262306a36Sopenharmony_ci unsigned long tmp = cr_end - cr_start; 39362306a36Sopenharmony_ci /* check for roll over */ 39462306a36Sopenharmony_ci cr_start = (cr_end < cr_start) ? -(tmp) : (tmp); 39562306a36Sopenharmony_ci } 39662306a36Sopenharmony_ci ioc->avg_search[ioc->avg_idx++] = cr_start; 39762306a36Sopenharmony_ci ioc->avg_idx &= CCIO_SEARCH_SAMPLE - 1; 39862306a36Sopenharmony_ci ioc->used_pages += pages_needed; 39962306a36Sopenharmony_ci#endif 40062306a36Sopenharmony_ci /* 40162306a36Sopenharmony_ci ** return the bit address. 40262306a36Sopenharmony_ci */ 40362306a36Sopenharmony_ci return res_idx << 3; 40462306a36Sopenharmony_ci} 40562306a36Sopenharmony_ci 40662306a36Sopenharmony_ci#define CCIO_FREE_MAPPINGS(ioc, res_idx, mask, size) \ 40762306a36Sopenharmony_ci u##size *res_ptr = (u##size *)&((ioc)->res_map[res_idx]); \ 40862306a36Sopenharmony_ci BUG_ON((*res_ptr & mask) != mask); \ 40962306a36Sopenharmony_ci *res_ptr &= ~(mask); 41062306a36Sopenharmony_ci 41162306a36Sopenharmony_ci/** 41262306a36Sopenharmony_ci * ccio_free_range - Free pages from the ioc's resource map. 41362306a36Sopenharmony_ci * @ioc: The I/O Controller. 41462306a36Sopenharmony_ci * @iova: The I/O Virtual Address. 41562306a36Sopenharmony_ci * @pages_mapped: The requested number of pages to be freed from the 41662306a36Sopenharmony_ci * I/O Pdir. 41762306a36Sopenharmony_ci * 41862306a36Sopenharmony_ci * This function frees the resouces allocated for the iova. 41962306a36Sopenharmony_ci */ 42062306a36Sopenharmony_cistatic void 42162306a36Sopenharmony_ciccio_free_range(struct ioc *ioc, dma_addr_t iova, unsigned long pages_mapped) 42262306a36Sopenharmony_ci{ 42362306a36Sopenharmony_ci unsigned long iovp = CCIO_IOVP(iova); 42462306a36Sopenharmony_ci unsigned int res_idx = PDIR_INDEX(iovp) >> 3; 42562306a36Sopenharmony_ci 42662306a36Sopenharmony_ci BUG_ON(pages_mapped == 0); 42762306a36Sopenharmony_ci BUG_ON((pages_mapped * IOVP_SIZE) > DMA_CHUNK_SIZE); 42862306a36Sopenharmony_ci BUG_ON(pages_mapped > BITS_PER_LONG); 42962306a36Sopenharmony_ci 43062306a36Sopenharmony_ci DBG_RES("%s(): res_idx: %d pages_mapped %lu\n", 43162306a36Sopenharmony_ci __func__, res_idx, pages_mapped); 43262306a36Sopenharmony_ci 43362306a36Sopenharmony_ci#ifdef CCIO_COLLECT_STATS 43462306a36Sopenharmony_ci ioc->used_pages -= pages_mapped; 43562306a36Sopenharmony_ci#endif 43662306a36Sopenharmony_ci 43762306a36Sopenharmony_ci if(pages_mapped <= 8) { 43862306a36Sopenharmony_ci#if 0 43962306a36Sopenharmony_ci /* see matching comments in alloc_range */ 44062306a36Sopenharmony_ci unsigned long mask = ~(~0UL >> pages_mapped); 44162306a36Sopenharmony_ci CCIO_FREE_MAPPINGS(ioc, res_idx, mask, 8); 44262306a36Sopenharmony_ci#else 44362306a36Sopenharmony_ci CCIO_FREE_MAPPINGS(ioc, res_idx, 0xffUL, 8); 44462306a36Sopenharmony_ci#endif 44562306a36Sopenharmony_ci } else if(pages_mapped <= 16) { 44662306a36Sopenharmony_ci CCIO_FREE_MAPPINGS(ioc, res_idx, 0xffffUL, 16); 44762306a36Sopenharmony_ci } else if(pages_mapped <= 32) { 44862306a36Sopenharmony_ci CCIO_FREE_MAPPINGS(ioc, res_idx, ~(unsigned int)0, 32); 44962306a36Sopenharmony_ci#ifdef __LP64__ 45062306a36Sopenharmony_ci } else if(pages_mapped <= 64) { 45162306a36Sopenharmony_ci CCIO_FREE_MAPPINGS(ioc, res_idx, ~0UL, 64); 45262306a36Sopenharmony_ci#endif 45362306a36Sopenharmony_ci } else { 45462306a36Sopenharmony_ci panic("%s:%s() Too many pages to unmap.\n", __FILE__, 45562306a36Sopenharmony_ci __func__); 45662306a36Sopenharmony_ci } 45762306a36Sopenharmony_ci} 45862306a36Sopenharmony_ci 45962306a36Sopenharmony_ci/**************************************************************** 46062306a36Sopenharmony_ci** 46162306a36Sopenharmony_ci** CCIO dma_ops support routines 46262306a36Sopenharmony_ci** 46362306a36Sopenharmony_ci*****************************************************************/ 46462306a36Sopenharmony_ci 46562306a36Sopenharmony_citypedef unsigned long space_t; 46662306a36Sopenharmony_ci#define KERNEL_SPACE 0 46762306a36Sopenharmony_ci 46862306a36Sopenharmony_ci/* 46962306a36Sopenharmony_ci** DMA "Page Type" and Hints 47062306a36Sopenharmony_ci** o if SAFE_DMA isn't set, mapping is for FAST_DMA. SAFE_DMA should be 47162306a36Sopenharmony_ci** set for subcacheline DMA transfers since we don't want to damage the 47262306a36Sopenharmony_ci** other part of a cacheline. 47362306a36Sopenharmony_ci** o SAFE_DMA must be set for "memory" allocated via pci_alloc_consistent(). 47462306a36Sopenharmony_ci** This bit tells U2 to do R/M/W for partial cachelines. "Streaming" 47562306a36Sopenharmony_ci** data can avoid this if the mapping covers full cache lines. 47662306a36Sopenharmony_ci** o STOP_MOST is needed for atomicity across cachelines. 47762306a36Sopenharmony_ci** Apparently only "some EISA devices" need this. 47862306a36Sopenharmony_ci** Using CONFIG_ISA is hack. Only the IOA with EISA under it needs 47962306a36Sopenharmony_ci** to use this hint iff the EISA devices needs this feature. 48062306a36Sopenharmony_ci** According to the U2 ERS, STOP_MOST enabled pages hurt performance. 48162306a36Sopenharmony_ci** o PREFETCH should *not* be set for cases like Multiple PCI devices 48262306a36Sopenharmony_ci** behind GSCtoPCI (dino) bus converter. Only one cacheline per GSC 48362306a36Sopenharmony_ci** device can be fetched and multiply DMA streams will thrash the 48462306a36Sopenharmony_ci** prefetch buffer and burn memory bandwidth. See 6.7.3 "Prefetch Rules 48562306a36Sopenharmony_ci** and Invalidation of Prefetch Entries". 48662306a36Sopenharmony_ci** 48762306a36Sopenharmony_ci** FIXME: the default hints need to be per GSC device - not global. 48862306a36Sopenharmony_ci** 48962306a36Sopenharmony_ci** HP-UX dorks: linux device driver programming model is totally different 49062306a36Sopenharmony_ci** than HP-UX's. HP-UX always sets HINT_PREFETCH since it's drivers 49162306a36Sopenharmony_ci** do special things to work on non-coherent platforms...linux has to 49262306a36Sopenharmony_ci** be much more careful with this. 49362306a36Sopenharmony_ci*/ 49462306a36Sopenharmony_ci#define IOPDIR_VALID 0x01UL 49562306a36Sopenharmony_ci#define HINT_SAFE_DMA 0x02UL /* used for pci_alloc_consistent() pages */ 49662306a36Sopenharmony_ci#ifdef CONFIG_EISA 49762306a36Sopenharmony_ci#define HINT_STOP_MOST 0x04UL /* LSL support */ 49862306a36Sopenharmony_ci#else 49962306a36Sopenharmony_ci#define HINT_STOP_MOST 0x00UL /* only needed for "some EISA devices" */ 50062306a36Sopenharmony_ci#endif 50162306a36Sopenharmony_ci#define HINT_UDPATE_ENB 0x08UL /* not used/supported by U2 */ 50262306a36Sopenharmony_ci#define HINT_PREFETCH 0x10UL /* for outbound pages which are not SAFE */ 50362306a36Sopenharmony_ci 50462306a36Sopenharmony_ci 50562306a36Sopenharmony_ci/* 50662306a36Sopenharmony_ci** Use direction (ie PCI_DMA_TODEVICE) to pick hint. 50762306a36Sopenharmony_ci** ccio_alloc_consistent() depends on this to get SAFE_DMA 50862306a36Sopenharmony_ci** when it passes in BIDIRECTIONAL flag. 50962306a36Sopenharmony_ci*/ 51062306a36Sopenharmony_cistatic u32 hint_lookup[] = { 51162306a36Sopenharmony_ci [DMA_BIDIRECTIONAL] = HINT_STOP_MOST | HINT_SAFE_DMA | IOPDIR_VALID, 51262306a36Sopenharmony_ci [DMA_TO_DEVICE] = HINT_STOP_MOST | HINT_PREFETCH | IOPDIR_VALID, 51362306a36Sopenharmony_ci [DMA_FROM_DEVICE] = HINT_STOP_MOST | IOPDIR_VALID, 51462306a36Sopenharmony_ci}; 51562306a36Sopenharmony_ci 51662306a36Sopenharmony_ci/** 51762306a36Sopenharmony_ci * ccio_io_pdir_entry - Initialize an I/O Pdir. 51862306a36Sopenharmony_ci * @pdir_ptr: A pointer into I/O Pdir. 51962306a36Sopenharmony_ci * @sid: The Space Identifier. 52062306a36Sopenharmony_ci * @vba: The virtual address. 52162306a36Sopenharmony_ci * @hints: The DMA Hint. 52262306a36Sopenharmony_ci * 52362306a36Sopenharmony_ci * Given a virtual address (vba, arg2) and space id, (sid, arg1), 52462306a36Sopenharmony_ci * load the I/O PDIR entry pointed to by pdir_ptr (arg0). Each IO Pdir 52562306a36Sopenharmony_ci * entry consists of 8 bytes as shown below (MSB == bit 0): 52662306a36Sopenharmony_ci * 52762306a36Sopenharmony_ci * 52862306a36Sopenharmony_ci * WORD 0: 52962306a36Sopenharmony_ci * +------+----------------+-----------------------------------------------+ 53062306a36Sopenharmony_ci * | Phys | Virtual Index | Phys | 53162306a36Sopenharmony_ci * | 0:3 | 0:11 | 4:19 | 53262306a36Sopenharmony_ci * |4 bits| 12 bits | 16 bits | 53362306a36Sopenharmony_ci * +------+----------------+-----------------------------------------------+ 53462306a36Sopenharmony_ci * WORD 1: 53562306a36Sopenharmony_ci * +-----------------------+-----------------------------------------------+ 53662306a36Sopenharmony_ci * | Phys | Rsvd | Prefetch |Update |Rsvd |Lock |Safe |Valid | 53762306a36Sopenharmony_ci * | 20:39 | | Enable |Enable | |Enable|DMA | | 53862306a36Sopenharmony_ci * | 20 bits | 5 bits | 1 bit |1 bit |2 bits|1 bit |1 bit |1 bit | 53962306a36Sopenharmony_ci * +-----------------------+-----------------------------------------------+ 54062306a36Sopenharmony_ci * 54162306a36Sopenharmony_ci * The virtual index field is filled with the results of the LCI 54262306a36Sopenharmony_ci * (Load Coherence Index) instruction. The 8 bits used for the virtual 54362306a36Sopenharmony_ci * index are bits 12:19 of the value returned by LCI. 54462306a36Sopenharmony_ci */ 54562306a36Sopenharmony_cistatic void 54662306a36Sopenharmony_ciccio_io_pdir_entry(__le64 *pdir_ptr, space_t sid, unsigned long vba, 54762306a36Sopenharmony_ci unsigned long hints) 54862306a36Sopenharmony_ci{ 54962306a36Sopenharmony_ci register unsigned long pa; 55062306a36Sopenharmony_ci register unsigned long ci; /* coherent index */ 55162306a36Sopenharmony_ci 55262306a36Sopenharmony_ci /* We currently only support kernel addresses */ 55362306a36Sopenharmony_ci BUG_ON(sid != KERNEL_SPACE); 55462306a36Sopenharmony_ci 55562306a36Sopenharmony_ci /* 55662306a36Sopenharmony_ci ** WORD 1 - low order word 55762306a36Sopenharmony_ci ** "hints" parm includes the VALID bit! 55862306a36Sopenharmony_ci ** "dep" clobbers the physical address offset bits as well. 55962306a36Sopenharmony_ci */ 56062306a36Sopenharmony_ci pa = lpa(vba); 56162306a36Sopenharmony_ci asm volatile("depw %1,31,12,%0" : "+r" (pa) : "r" (hints)); 56262306a36Sopenharmony_ci ((u32 *)pdir_ptr)[1] = (u32) pa; 56362306a36Sopenharmony_ci 56462306a36Sopenharmony_ci /* 56562306a36Sopenharmony_ci ** WORD 0 - high order word 56662306a36Sopenharmony_ci */ 56762306a36Sopenharmony_ci 56862306a36Sopenharmony_ci#ifdef __LP64__ 56962306a36Sopenharmony_ci /* 57062306a36Sopenharmony_ci ** get bits 12:15 of physical address 57162306a36Sopenharmony_ci ** shift bits 16:31 of physical address 57262306a36Sopenharmony_ci ** and deposit them 57362306a36Sopenharmony_ci */ 57462306a36Sopenharmony_ci asm volatile ("extrd,u %1,15,4,%0" : "=r" (ci) : "r" (pa)); 57562306a36Sopenharmony_ci asm volatile ("extrd,u %1,31,16,%0" : "+r" (pa) : "r" (pa)); 57662306a36Sopenharmony_ci asm volatile ("depd %1,35,4,%0" : "+r" (pa) : "r" (ci)); 57762306a36Sopenharmony_ci#else 57862306a36Sopenharmony_ci pa = 0; 57962306a36Sopenharmony_ci#endif 58062306a36Sopenharmony_ci /* 58162306a36Sopenharmony_ci ** get CPU coherency index bits 58262306a36Sopenharmony_ci ** Grab virtual index [0:11] 58362306a36Sopenharmony_ci ** Deposit virt_idx bits into I/O PDIR word 58462306a36Sopenharmony_ci */ 58562306a36Sopenharmony_ci asm volatile ("lci %%r0(%1), %0" : "=r" (ci) : "r" (vba)); 58662306a36Sopenharmony_ci asm volatile ("extru %1,19,12,%0" : "+r" (ci) : "r" (ci)); 58762306a36Sopenharmony_ci asm volatile ("depw %1,15,12,%0" : "+r" (pa) : "r" (ci)); 58862306a36Sopenharmony_ci 58962306a36Sopenharmony_ci ((u32 *)pdir_ptr)[0] = (u32) pa; 59062306a36Sopenharmony_ci 59162306a36Sopenharmony_ci 59262306a36Sopenharmony_ci /* FIXME: PCX_W platforms don't need FDC/SYNC. (eg C360) 59362306a36Sopenharmony_ci ** PCX-U/U+ do. (eg C200/C240) 59462306a36Sopenharmony_ci ** PCX-T'? Don't know. (eg C110 or similar K-class) 59562306a36Sopenharmony_ci ** 59662306a36Sopenharmony_ci ** See PDC_MODEL/option 0/SW_CAP word for "Non-coherent IO-PDIR bit". 59762306a36Sopenharmony_ci ** 59862306a36Sopenharmony_ci ** "Since PCX-U employs an offset hash that is incompatible with 59962306a36Sopenharmony_ci ** the real mode coherence index generation of U2, the PDIR entry 60062306a36Sopenharmony_ci ** must be flushed to memory to retain coherence." 60162306a36Sopenharmony_ci */ 60262306a36Sopenharmony_ci asm_io_fdc(pdir_ptr); 60362306a36Sopenharmony_ci asm_io_sync(); 60462306a36Sopenharmony_ci} 60562306a36Sopenharmony_ci 60662306a36Sopenharmony_ci/** 60762306a36Sopenharmony_ci * ccio_clear_io_tlb - Remove stale entries from the I/O TLB. 60862306a36Sopenharmony_ci * @ioc: The I/O Controller. 60962306a36Sopenharmony_ci * @iovp: The I/O Virtual Page. 61062306a36Sopenharmony_ci * @byte_cnt: The requested number of bytes to be freed from the I/O Pdir. 61162306a36Sopenharmony_ci * 61262306a36Sopenharmony_ci * Purge invalid I/O PDIR entries from the I/O TLB. 61362306a36Sopenharmony_ci * 61462306a36Sopenharmony_ci * FIXME: Can we change the byte_cnt to pages_mapped? 61562306a36Sopenharmony_ci */ 61662306a36Sopenharmony_cistatic void 61762306a36Sopenharmony_ciccio_clear_io_tlb(struct ioc *ioc, dma_addr_t iovp, size_t byte_cnt) 61862306a36Sopenharmony_ci{ 61962306a36Sopenharmony_ci u32 chain_size = 1 << ioc->chainid_shift; 62062306a36Sopenharmony_ci 62162306a36Sopenharmony_ci iovp &= IOVP_MASK; /* clear offset bits, just want pagenum */ 62262306a36Sopenharmony_ci byte_cnt += chain_size; 62362306a36Sopenharmony_ci 62462306a36Sopenharmony_ci while(byte_cnt > chain_size) { 62562306a36Sopenharmony_ci WRITE_U32(CMD_TLB_PURGE | iovp, &ioc->ioc_regs->io_command); 62662306a36Sopenharmony_ci iovp += chain_size; 62762306a36Sopenharmony_ci byte_cnt -= chain_size; 62862306a36Sopenharmony_ci } 62962306a36Sopenharmony_ci} 63062306a36Sopenharmony_ci 63162306a36Sopenharmony_ci/** 63262306a36Sopenharmony_ci * ccio_mark_invalid - Mark the I/O Pdir entries invalid. 63362306a36Sopenharmony_ci * @ioc: The I/O Controller. 63462306a36Sopenharmony_ci * @iova: The I/O Virtual Address. 63562306a36Sopenharmony_ci * @byte_cnt: The requested number of bytes to be freed from the I/O Pdir. 63662306a36Sopenharmony_ci * 63762306a36Sopenharmony_ci * Mark the I/O Pdir entries invalid and blow away the corresponding I/O 63862306a36Sopenharmony_ci * TLB entries. 63962306a36Sopenharmony_ci * 64062306a36Sopenharmony_ci * FIXME: at some threshold it might be "cheaper" to just blow 64162306a36Sopenharmony_ci * away the entire I/O TLB instead of individual entries. 64262306a36Sopenharmony_ci * 64362306a36Sopenharmony_ci * FIXME: Uturn has 256 TLB entries. We don't need to purge every 64462306a36Sopenharmony_ci * PDIR entry - just once for each possible TLB entry. 64562306a36Sopenharmony_ci * (We do need to maker I/O PDIR entries invalid regardless). 64662306a36Sopenharmony_ci * 64762306a36Sopenharmony_ci * FIXME: Can we change byte_cnt to pages_mapped? 64862306a36Sopenharmony_ci */ 64962306a36Sopenharmony_cistatic void 65062306a36Sopenharmony_ciccio_mark_invalid(struct ioc *ioc, dma_addr_t iova, size_t byte_cnt) 65162306a36Sopenharmony_ci{ 65262306a36Sopenharmony_ci u32 iovp = (u32)CCIO_IOVP(iova); 65362306a36Sopenharmony_ci size_t saved_byte_cnt; 65462306a36Sopenharmony_ci 65562306a36Sopenharmony_ci /* round up to nearest page size */ 65662306a36Sopenharmony_ci saved_byte_cnt = byte_cnt = ALIGN(byte_cnt, IOVP_SIZE); 65762306a36Sopenharmony_ci 65862306a36Sopenharmony_ci while(byte_cnt > 0) { 65962306a36Sopenharmony_ci /* invalidate one page at a time */ 66062306a36Sopenharmony_ci unsigned int idx = PDIR_INDEX(iovp); 66162306a36Sopenharmony_ci char *pdir_ptr = (char *) &(ioc->pdir_base[idx]); 66262306a36Sopenharmony_ci 66362306a36Sopenharmony_ci BUG_ON(idx >= (ioc->pdir_size / sizeof(u64))); 66462306a36Sopenharmony_ci pdir_ptr[7] = 0; /* clear only VALID bit */ 66562306a36Sopenharmony_ci /* 66662306a36Sopenharmony_ci ** FIXME: PCX_W platforms don't need FDC/SYNC. (eg C360) 66762306a36Sopenharmony_ci ** PCX-U/U+ do. (eg C200/C240) 66862306a36Sopenharmony_ci ** See PDC_MODEL/option 0/SW_CAP for "Non-coherent IO-PDIR bit". 66962306a36Sopenharmony_ci */ 67062306a36Sopenharmony_ci asm_io_fdc(pdir_ptr); 67162306a36Sopenharmony_ci 67262306a36Sopenharmony_ci iovp += IOVP_SIZE; 67362306a36Sopenharmony_ci byte_cnt -= IOVP_SIZE; 67462306a36Sopenharmony_ci } 67562306a36Sopenharmony_ci 67662306a36Sopenharmony_ci asm_io_sync(); 67762306a36Sopenharmony_ci ccio_clear_io_tlb(ioc, CCIO_IOVP(iova), saved_byte_cnt); 67862306a36Sopenharmony_ci} 67962306a36Sopenharmony_ci 68062306a36Sopenharmony_ci/**************************************************************** 68162306a36Sopenharmony_ci** 68262306a36Sopenharmony_ci** CCIO dma_ops 68362306a36Sopenharmony_ci** 68462306a36Sopenharmony_ci*****************************************************************/ 68562306a36Sopenharmony_ci 68662306a36Sopenharmony_ci/** 68762306a36Sopenharmony_ci * ccio_dma_supported - Verify the IOMMU supports the DMA address range. 68862306a36Sopenharmony_ci * @dev: The PCI device. 68962306a36Sopenharmony_ci * @mask: A bit mask describing the DMA address range of the device. 69062306a36Sopenharmony_ci */ 69162306a36Sopenharmony_cistatic int 69262306a36Sopenharmony_ciccio_dma_supported(struct device *dev, u64 mask) 69362306a36Sopenharmony_ci{ 69462306a36Sopenharmony_ci if(dev == NULL) { 69562306a36Sopenharmony_ci printk(KERN_ERR MODULE_NAME ": EISA/ISA/et al not supported\n"); 69662306a36Sopenharmony_ci BUG(); 69762306a36Sopenharmony_ci return 0; 69862306a36Sopenharmony_ci } 69962306a36Sopenharmony_ci 70062306a36Sopenharmony_ci /* only support 32-bit or better devices (ie PCI/GSC) */ 70162306a36Sopenharmony_ci return (int)(mask >= 0xffffffffUL); 70262306a36Sopenharmony_ci} 70362306a36Sopenharmony_ci 70462306a36Sopenharmony_ci/** 70562306a36Sopenharmony_ci * ccio_map_single - Map an address range into the IOMMU. 70662306a36Sopenharmony_ci * @dev: The PCI device. 70762306a36Sopenharmony_ci * @addr: The start address of the DMA region. 70862306a36Sopenharmony_ci * @size: The length of the DMA region. 70962306a36Sopenharmony_ci * @direction: The direction of the DMA transaction (to/from device). 71062306a36Sopenharmony_ci * 71162306a36Sopenharmony_ci * This function implements the pci_map_single function. 71262306a36Sopenharmony_ci */ 71362306a36Sopenharmony_cistatic dma_addr_t 71462306a36Sopenharmony_ciccio_map_single(struct device *dev, void *addr, size_t size, 71562306a36Sopenharmony_ci enum dma_data_direction direction) 71662306a36Sopenharmony_ci{ 71762306a36Sopenharmony_ci int idx; 71862306a36Sopenharmony_ci struct ioc *ioc; 71962306a36Sopenharmony_ci unsigned long flags; 72062306a36Sopenharmony_ci dma_addr_t iovp; 72162306a36Sopenharmony_ci dma_addr_t offset; 72262306a36Sopenharmony_ci __le64 *pdir_start; 72362306a36Sopenharmony_ci unsigned long hint = hint_lookup[(int)direction]; 72462306a36Sopenharmony_ci 72562306a36Sopenharmony_ci BUG_ON(!dev); 72662306a36Sopenharmony_ci ioc = GET_IOC(dev); 72762306a36Sopenharmony_ci if (!ioc) 72862306a36Sopenharmony_ci return DMA_MAPPING_ERROR; 72962306a36Sopenharmony_ci 73062306a36Sopenharmony_ci BUG_ON(size <= 0); 73162306a36Sopenharmony_ci 73262306a36Sopenharmony_ci /* save offset bits */ 73362306a36Sopenharmony_ci offset = ((unsigned long) addr) & ~IOVP_MASK; 73462306a36Sopenharmony_ci 73562306a36Sopenharmony_ci /* round up to nearest IOVP_SIZE */ 73662306a36Sopenharmony_ci size = ALIGN(size + offset, IOVP_SIZE); 73762306a36Sopenharmony_ci spin_lock_irqsave(&ioc->res_lock, flags); 73862306a36Sopenharmony_ci 73962306a36Sopenharmony_ci#ifdef CCIO_COLLECT_STATS 74062306a36Sopenharmony_ci ioc->msingle_calls++; 74162306a36Sopenharmony_ci ioc->msingle_pages += size >> IOVP_SHIFT; 74262306a36Sopenharmony_ci#endif 74362306a36Sopenharmony_ci 74462306a36Sopenharmony_ci idx = ccio_alloc_range(ioc, dev, size); 74562306a36Sopenharmony_ci iovp = (dma_addr_t)MKIOVP(idx); 74662306a36Sopenharmony_ci 74762306a36Sopenharmony_ci pdir_start = &(ioc->pdir_base[idx]); 74862306a36Sopenharmony_ci 74962306a36Sopenharmony_ci DBG_RUN("%s() %px -> %#lx size: %zu\n", 75062306a36Sopenharmony_ci __func__, addr, (long)(iovp | offset), size); 75162306a36Sopenharmony_ci 75262306a36Sopenharmony_ci /* If not cacheline aligned, force SAFE_DMA on the whole mess */ 75362306a36Sopenharmony_ci if((size % L1_CACHE_BYTES) || ((unsigned long)addr % L1_CACHE_BYTES)) 75462306a36Sopenharmony_ci hint |= HINT_SAFE_DMA; 75562306a36Sopenharmony_ci 75662306a36Sopenharmony_ci while(size > 0) { 75762306a36Sopenharmony_ci ccio_io_pdir_entry(pdir_start, KERNEL_SPACE, (unsigned long)addr, hint); 75862306a36Sopenharmony_ci 75962306a36Sopenharmony_ci DBG_RUN(" pdir %p %08x%08x\n", 76062306a36Sopenharmony_ci pdir_start, 76162306a36Sopenharmony_ci (u32) (((u32 *) pdir_start)[0]), 76262306a36Sopenharmony_ci (u32) (((u32 *) pdir_start)[1])); 76362306a36Sopenharmony_ci ++pdir_start; 76462306a36Sopenharmony_ci addr += IOVP_SIZE; 76562306a36Sopenharmony_ci size -= IOVP_SIZE; 76662306a36Sopenharmony_ci } 76762306a36Sopenharmony_ci 76862306a36Sopenharmony_ci spin_unlock_irqrestore(&ioc->res_lock, flags); 76962306a36Sopenharmony_ci 77062306a36Sopenharmony_ci /* form complete address */ 77162306a36Sopenharmony_ci return CCIO_IOVA(iovp, offset); 77262306a36Sopenharmony_ci} 77362306a36Sopenharmony_ci 77462306a36Sopenharmony_ci 77562306a36Sopenharmony_cistatic dma_addr_t 77662306a36Sopenharmony_ciccio_map_page(struct device *dev, struct page *page, unsigned long offset, 77762306a36Sopenharmony_ci size_t size, enum dma_data_direction direction, 77862306a36Sopenharmony_ci unsigned long attrs) 77962306a36Sopenharmony_ci{ 78062306a36Sopenharmony_ci return ccio_map_single(dev, page_address(page) + offset, size, 78162306a36Sopenharmony_ci direction); 78262306a36Sopenharmony_ci} 78362306a36Sopenharmony_ci 78462306a36Sopenharmony_ci 78562306a36Sopenharmony_ci/** 78662306a36Sopenharmony_ci * ccio_unmap_page - Unmap an address range from the IOMMU. 78762306a36Sopenharmony_ci * @dev: The PCI device. 78862306a36Sopenharmony_ci * @iova: The start address of the DMA region. 78962306a36Sopenharmony_ci * @size: The length of the DMA region. 79062306a36Sopenharmony_ci * @direction: The direction of the DMA transaction (to/from device). 79162306a36Sopenharmony_ci * @attrs: attributes 79262306a36Sopenharmony_ci */ 79362306a36Sopenharmony_cistatic void 79462306a36Sopenharmony_ciccio_unmap_page(struct device *dev, dma_addr_t iova, size_t size, 79562306a36Sopenharmony_ci enum dma_data_direction direction, unsigned long attrs) 79662306a36Sopenharmony_ci{ 79762306a36Sopenharmony_ci struct ioc *ioc; 79862306a36Sopenharmony_ci unsigned long flags; 79962306a36Sopenharmony_ci dma_addr_t offset = iova & ~IOVP_MASK; 80062306a36Sopenharmony_ci 80162306a36Sopenharmony_ci BUG_ON(!dev); 80262306a36Sopenharmony_ci ioc = GET_IOC(dev); 80362306a36Sopenharmony_ci if (!ioc) { 80462306a36Sopenharmony_ci WARN_ON(!ioc); 80562306a36Sopenharmony_ci return; 80662306a36Sopenharmony_ci } 80762306a36Sopenharmony_ci 80862306a36Sopenharmony_ci DBG_RUN("%s() iovp %#lx/%zx\n", 80962306a36Sopenharmony_ci __func__, (long)iova, size); 81062306a36Sopenharmony_ci 81162306a36Sopenharmony_ci iova ^= offset; /* clear offset bits */ 81262306a36Sopenharmony_ci size += offset; 81362306a36Sopenharmony_ci size = ALIGN(size, IOVP_SIZE); 81462306a36Sopenharmony_ci 81562306a36Sopenharmony_ci spin_lock_irqsave(&ioc->res_lock, flags); 81662306a36Sopenharmony_ci 81762306a36Sopenharmony_ci#ifdef CCIO_COLLECT_STATS 81862306a36Sopenharmony_ci ioc->usingle_calls++; 81962306a36Sopenharmony_ci ioc->usingle_pages += size >> IOVP_SHIFT; 82062306a36Sopenharmony_ci#endif 82162306a36Sopenharmony_ci 82262306a36Sopenharmony_ci ccio_mark_invalid(ioc, iova, size); 82362306a36Sopenharmony_ci ccio_free_range(ioc, iova, (size >> IOVP_SHIFT)); 82462306a36Sopenharmony_ci spin_unlock_irqrestore(&ioc->res_lock, flags); 82562306a36Sopenharmony_ci} 82662306a36Sopenharmony_ci 82762306a36Sopenharmony_ci/** 82862306a36Sopenharmony_ci * ccio_alloc - Allocate a consistent DMA mapping. 82962306a36Sopenharmony_ci * @dev: The PCI device. 83062306a36Sopenharmony_ci * @size: The length of the DMA region. 83162306a36Sopenharmony_ci * @dma_handle: The DMA address handed back to the device (not the cpu). 83262306a36Sopenharmony_ci * @flag: allocation flags 83362306a36Sopenharmony_ci * @attrs: attributes 83462306a36Sopenharmony_ci * 83562306a36Sopenharmony_ci * This function implements the pci_alloc_consistent function. 83662306a36Sopenharmony_ci */ 83762306a36Sopenharmony_cistatic void * 83862306a36Sopenharmony_ciccio_alloc(struct device *dev, size_t size, dma_addr_t *dma_handle, gfp_t flag, 83962306a36Sopenharmony_ci unsigned long attrs) 84062306a36Sopenharmony_ci{ 84162306a36Sopenharmony_ci void *ret; 84262306a36Sopenharmony_ci#if 0 84362306a36Sopenharmony_ci/* GRANT Need to establish hierarchy for non-PCI devs as well 84462306a36Sopenharmony_ci** and then provide matching gsc_map_xxx() functions for them as well. 84562306a36Sopenharmony_ci*/ 84662306a36Sopenharmony_ci if(!hwdev) { 84762306a36Sopenharmony_ci /* only support PCI */ 84862306a36Sopenharmony_ci *dma_handle = 0; 84962306a36Sopenharmony_ci return 0; 85062306a36Sopenharmony_ci } 85162306a36Sopenharmony_ci#endif 85262306a36Sopenharmony_ci ret = (void *) __get_free_pages(flag, get_order(size)); 85362306a36Sopenharmony_ci 85462306a36Sopenharmony_ci if (ret) { 85562306a36Sopenharmony_ci memset(ret, 0, size); 85662306a36Sopenharmony_ci *dma_handle = ccio_map_single(dev, ret, size, DMA_BIDIRECTIONAL); 85762306a36Sopenharmony_ci } 85862306a36Sopenharmony_ci 85962306a36Sopenharmony_ci return ret; 86062306a36Sopenharmony_ci} 86162306a36Sopenharmony_ci 86262306a36Sopenharmony_ci/** 86362306a36Sopenharmony_ci * ccio_free - Free a consistent DMA mapping. 86462306a36Sopenharmony_ci * @dev: The PCI device. 86562306a36Sopenharmony_ci * @size: The length of the DMA region. 86662306a36Sopenharmony_ci * @cpu_addr: The cpu address returned from the ccio_alloc_consistent. 86762306a36Sopenharmony_ci * @dma_handle: The device address returned from the ccio_alloc_consistent. 86862306a36Sopenharmony_ci * @attrs: attributes 86962306a36Sopenharmony_ci * 87062306a36Sopenharmony_ci * This function implements the pci_free_consistent function. 87162306a36Sopenharmony_ci */ 87262306a36Sopenharmony_cistatic void 87362306a36Sopenharmony_ciccio_free(struct device *dev, size_t size, void *cpu_addr, 87462306a36Sopenharmony_ci dma_addr_t dma_handle, unsigned long attrs) 87562306a36Sopenharmony_ci{ 87662306a36Sopenharmony_ci ccio_unmap_page(dev, dma_handle, size, 0, 0); 87762306a36Sopenharmony_ci free_pages((unsigned long)cpu_addr, get_order(size)); 87862306a36Sopenharmony_ci} 87962306a36Sopenharmony_ci 88062306a36Sopenharmony_ci/* 88162306a36Sopenharmony_ci** Since 0 is a valid pdir_base index value, can't use that 88262306a36Sopenharmony_ci** to determine if a value is valid or not. Use a flag to indicate 88362306a36Sopenharmony_ci** the SG list entry contains a valid pdir index. 88462306a36Sopenharmony_ci*/ 88562306a36Sopenharmony_ci#define PIDE_FLAG 0x80000000UL 88662306a36Sopenharmony_ci 88762306a36Sopenharmony_ci#ifdef CCIO_COLLECT_STATS 88862306a36Sopenharmony_ci#define IOMMU_MAP_STATS 88962306a36Sopenharmony_ci#endif 89062306a36Sopenharmony_ci#include "iommu-helpers.h" 89162306a36Sopenharmony_ci 89262306a36Sopenharmony_ci/** 89362306a36Sopenharmony_ci * ccio_map_sg - Map the scatter/gather list into the IOMMU. 89462306a36Sopenharmony_ci * @dev: The PCI device. 89562306a36Sopenharmony_ci * @sglist: The scatter/gather list to be mapped in the IOMMU. 89662306a36Sopenharmony_ci * @nents: The number of entries in the scatter/gather list. 89762306a36Sopenharmony_ci * @direction: The direction of the DMA transaction (to/from device). 89862306a36Sopenharmony_ci * @attrs: attributes 89962306a36Sopenharmony_ci * 90062306a36Sopenharmony_ci * This function implements the pci_map_sg function. 90162306a36Sopenharmony_ci */ 90262306a36Sopenharmony_cistatic int 90362306a36Sopenharmony_ciccio_map_sg(struct device *dev, struct scatterlist *sglist, int nents, 90462306a36Sopenharmony_ci enum dma_data_direction direction, unsigned long attrs) 90562306a36Sopenharmony_ci{ 90662306a36Sopenharmony_ci struct ioc *ioc; 90762306a36Sopenharmony_ci int coalesced, filled = 0; 90862306a36Sopenharmony_ci unsigned long flags; 90962306a36Sopenharmony_ci unsigned long hint = hint_lookup[(int)direction]; 91062306a36Sopenharmony_ci unsigned long prev_len = 0, current_len = 0; 91162306a36Sopenharmony_ci int i; 91262306a36Sopenharmony_ci 91362306a36Sopenharmony_ci BUG_ON(!dev); 91462306a36Sopenharmony_ci ioc = GET_IOC(dev); 91562306a36Sopenharmony_ci if (!ioc) 91662306a36Sopenharmony_ci return -EINVAL; 91762306a36Sopenharmony_ci 91862306a36Sopenharmony_ci DBG_RUN_SG("%s() START %d entries\n", __func__, nents); 91962306a36Sopenharmony_ci 92062306a36Sopenharmony_ci /* Fast path single entry scatterlists. */ 92162306a36Sopenharmony_ci if (nents == 1) { 92262306a36Sopenharmony_ci sg_dma_address(sglist) = ccio_map_single(dev, 92362306a36Sopenharmony_ci sg_virt(sglist), sglist->length, 92462306a36Sopenharmony_ci direction); 92562306a36Sopenharmony_ci sg_dma_len(sglist) = sglist->length; 92662306a36Sopenharmony_ci return 1; 92762306a36Sopenharmony_ci } 92862306a36Sopenharmony_ci 92962306a36Sopenharmony_ci for(i = 0; i < nents; i++) 93062306a36Sopenharmony_ci prev_len += sglist[i].length; 93162306a36Sopenharmony_ci 93262306a36Sopenharmony_ci spin_lock_irqsave(&ioc->res_lock, flags); 93362306a36Sopenharmony_ci 93462306a36Sopenharmony_ci#ifdef CCIO_COLLECT_STATS 93562306a36Sopenharmony_ci ioc->msg_calls++; 93662306a36Sopenharmony_ci#endif 93762306a36Sopenharmony_ci 93862306a36Sopenharmony_ci /* 93962306a36Sopenharmony_ci ** First coalesce the chunks and allocate I/O pdir space 94062306a36Sopenharmony_ci ** 94162306a36Sopenharmony_ci ** If this is one DMA stream, we can properly map using the 94262306a36Sopenharmony_ci ** correct virtual address associated with each DMA page. 94362306a36Sopenharmony_ci ** w/o this association, we wouldn't have coherent DMA! 94462306a36Sopenharmony_ci ** Access to the virtual address is what forces a two pass algorithm. 94562306a36Sopenharmony_ci */ 94662306a36Sopenharmony_ci coalesced = iommu_coalesce_chunks(ioc, dev, sglist, nents, ccio_alloc_range); 94762306a36Sopenharmony_ci 94862306a36Sopenharmony_ci /* 94962306a36Sopenharmony_ci ** Program the I/O Pdir 95062306a36Sopenharmony_ci ** 95162306a36Sopenharmony_ci ** map the virtual addresses to the I/O Pdir 95262306a36Sopenharmony_ci ** o dma_address will contain the pdir index 95362306a36Sopenharmony_ci ** o dma_len will contain the number of bytes to map 95462306a36Sopenharmony_ci ** o page/offset contain the virtual address. 95562306a36Sopenharmony_ci */ 95662306a36Sopenharmony_ci filled = iommu_fill_pdir(ioc, sglist, nents, hint, ccio_io_pdir_entry); 95762306a36Sopenharmony_ci 95862306a36Sopenharmony_ci spin_unlock_irqrestore(&ioc->res_lock, flags); 95962306a36Sopenharmony_ci 96062306a36Sopenharmony_ci BUG_ON(coalesced != filled); 96162306a36Sopenharmony_ci 96262306a36Sopenharmony_ci DBG_RUN_SG("%s() DONE %d mappings\n", __func__, filled); 96362306a36Sopenharmony_ci 96462306a36Sopenharmony_ci for (i = 0; i < filled; i++) 96562306a36Sopenharmony_ci current_len += sg_dma_len(sglist + i); 96662306a36Sopenharmony_ci 96762306a36Sopenharmony_ci BUG_ON(current_len != prev_len); 96862306a36Sopenharmony_ci 96962306a36Sopenharmony_ci return filled; 97062306a36Sopenharmony_ci} 97162306a36Sopenharmony_ci 97262306a36Sopenharmony_ci/** 97362306a36Sopenharmony_ci * ccio_unmap_sg - Unmap the scatter/gather list from the IOMMU. 97462306a36Sopenharmony_ci * @dev: The PCI device. 97562306a36Sopenharmony_ci * @sglist: The scatter/gather list to be unmapped from the IOMMU. 97662306a36Sopenharmony_ci * @nents: The number of entries in the scatter/gather list. 97762306a36Sopenharmony_ci * @direction: The direction of the DMA transaction (to/from device). 97862306a36Sopenharmony_ci * @attrs: attributes 97962306a36Sopenharmony_ci * 98062306a36Sopenharmony_ci * This function implements the pci_unmap_sg function. 98162306a36Sopenharmony_ci */ 98262306a36Sopenharmony_cistatic void 98362306a36Sopenharmony_ciccio_unmap_sg(struct device *dev, struct scatterlist *sglist, int nents, 98462306a36Sopenharmony_ci enum dma_data_direction direction, unsigned long attrs) 98562306a36Sopenharmony_ci{ 98662306a36Sopenharmony_ci struct ioc *ioc; 98762306a36Sopenharmony_ci 98862306a36Sopenharmony_ci BUG_ON(!dev); 98962306a36Sopenharmony_ci ioc = GET_IOC(dev); 99062306a36Sopenharmony_ci if (!ioc) { 99162306a36Sopenharmony_ci WARN_ON(!ioc); 99262306a36Sopenharmony_ci return; 99362306a36Sopenharmony_ci } 99462306a36Sopenharmony_ci 99562306a36Sopenharmony_ci DBG_RUN_SG("%s() START %d entries, %p,%x\n", 99662306a36Sopenharmony_ci __func__, nents, sg_virt(sglist), sglist->length); 99762306a36Sopenharmony_ci 99862306a36Sopenharmony_ci#ifdef CCIO_COLLECT_STATS 99962306a36Sopenharmony_ci ioc->usg_calls++; 100062306a36Sopenharmony_ci#endif 100162306a36Sopenharmony_ci 100262306a36Sopenharmony_ci while (nents && sg_dma_len(sglist)) { 100362306a36Sopenharmony_ci 100462306a36Sopenharmony_ci#ifdef CCIO_COLLECT_STATS 100562306a36Sopenharmony_ci ioc->usg_pages += sg_dma_len(sglist) >> PAGE_SHIFT; 100662306a36Sopenharmony_ci#endif 100762306a36Sopenharmony_ci ccio_unmap_page(dev, sg_dma_address(sglist), 100862306a36Sopenharmony_ci sg_dma_len(sglist), direction, 0); 100962306a36Sopenharmony_ci ++sglist; 101062306a36Sopenharmony_ci nents--; 101162306a36Sopenharmony_ci } 101262306a36Sopenharmony_ci 101362306a36Sopenharmony_ci DBG_RUN_SG("%s() DONE (nents %d)\n", __func__, nents); 101462306a36Sopenharmony_ci} 101562306a36Sopenharmony_ci 101662306a36Sopenharmony_cistatic const struct dma_map_ops ccio_ops = { 101762306a36Sopenharmony_ci .dma_supported = ccio_dma_supported, 101862306a36Sopenharmony_ci .alloc = ccio_alloc, 101962306a36Sopenharmony_ci .free = ccio_free, 102062306a36Sopenharmony_ci .map_page = ccio_map_page, 102162306a36Sopenharmony_ci .unmap_page = ccio_unmap_page, 102262306a36Sopenharmony_ci .map_sg = ccio_map_sg, 102362306a36Sopenharmony_ci .unmap_sg = ccio_unmap_sg, 102462306a36Sopenharmony_ci .get_sgtable = dma_common_get_sgtable, 102562306a36Sopenharmony_ci .alloc_pages = dma_common_alloc_pages, 102662306a36Sopenharmony_ci .free_pages = dma_common_free_pages, 102762306a36Sopenharmony_ci}; 102862306a36Sopenharmony_ci 102962306a36Sopenharmony_ci#ifdef CONFIG_PROC_FS 103062306a36Sopenharmony_cistatic int ccio_proc_info(struct seq_file *m, void *p) 103162306a36Sopenharmony_ci{ 103262306a36Sopenharmony_ci struct ioc *ioc = ioc_list; 103362306a36Sopenharmony_ci 103462306a36Sopenharmony_ci while (ioc != NULL) { 103562306a36Sopenharmony_ci unsigned int total_pages = ioc->res_size << 3; 103662306a36Sopenharmony_ci#ifdef CCIO_COLLECT_STATS 103762306a36Sopenharmony_ci unsigned long avg = 0, min, max; 103862306a36Sopenharmony_ci int j; 103962306a36Sopenharmony_ci#endif 104062306a36Sopenharmony_ci 104162306a36Sopenharmony_ci seq_printf(m, "%s\n", ioc->name); 104262306a36Sopenharmony_ci 104362306a36Sopenharmony_ci seq_printf(m, "Cujo 2.0 bug : %s\n", 104462306a36Sopenharmony_ci (ioc->cujo20_bug ? "yes" : "no")); 104562306a36Sopenharmony_ci 104662306a36Sopenharmony_ci seq_printf(m, "IO PDIR size : %d bytes (%d entries)\n", 104762306a36Sopenharmony_ci total_pages * 8, total_pages); 104862306a36Sopenharmony_ci 104962306a36Sopenharmony_ci#ifdef CCIO_COLLECT_STATS 105062306a36Sopenharmony_ci seq_printf(m, "IO PDIR entries : %ld free %ld used (%d%%)\n", 105162306a36Sopenharmony_ci total_pages - ioc->used_pages, ioc->used_pages, 105262306a36Sopenharmony_ci (int)(ioc->used_pages * 100 / total_pages)); 105362306a36Sopenharmony_ci#endif 105462306a36Sopenharmony_ci 105562306a36Sopenharmony_ci seq_printf(m, "Resource bitmap : %d bytes (%d pages)\n", 105662306a36Sopenharmony_ci ioc->res_size, total_pages); 105762306a36Sopenharmony_ci 105862306a36Sopenharmony_ci#ifdef CCIO_COLLECT_STATS 105962306a36Sopenharmony_ci min = max = ioc->avg_search[0]; 106062306a36Sopenharmony_ci for(j = 0; j < CCIO_SEARCH_SAMPLE; ++j) { 106162306a36Sopenharmony_ci avg += ioc->avg_search[j]; 106262306a36Sopenharmony_ci if(ioc->avg_search[j] > max) 106362306a36Sopenharmony_ci max = ioc->avg_search[j]; 106462306a36Sopenharmony_ci if(ioc->avg_search[j] < min) 106562306a36Sopenharmony_ci min = ioc->avg_search[j]; 106662306a36Sopenharmony_ci } 106762306a36Sopenharmony_ci avg /= CCIO_SEARCH_SAMPLE; 106862306a36Sopenharmony_ci seq_printf(m, " Bitmap search : %ld/%ld/%ld (min/avg/max CPU Cycles)\n", 106962306a36Sopenharmony_ci min, avg, max); 107062306a36Sopenharmony_ci 107162306a36Sopenharmony_ci seq_printf(m, "pci_map_single(): %8ld calls %8ld pages (avg %d/1000)\n", 107262306a36Sopenharmony_ci ioc->msingle_calls, ioc->msingle_pages, 107362306a36Sopenharmony_ci (int)((ioc->msingle_pages * 1000)/ioc->msingle_calls)); 107462306a36Sopenharmony_ci 107562306a36Sopenharmony_ci /* KLUGE - unmap_sg calls unmap_page for each mapped page */ 107662306a36Sopenharmony_ci min = ioc->usingle_calls - ioc->usg_calls; 107762306a36Sopenharmony_ci max = ioc->usingle_pages - ioc->usg_pages; 107862306a36Sopenharmony_ci seq_printf(m, "pci_unmap_single: %8ld calls %8ld pages (avg %d/1000)\n", 107962306a36Sopenharmony_ci min, max, (int)((max * 1000)/min)); 108062306a36Sopenharmony_ci 108162306a36Sopenharmony_ci seq_printf(m, "pci_map_sg() : %8ld calls %8ld pages (avg %d/1000)\n", 108262306a36Sopenharmony_ci ioc->msg_calls, ioc->msg_pages, 108362306a36Sopenharmony_ci (int)((ioc->msg_pages * 1000)/ioc->msg_calls)); 108462306a36Sopenharmony_ci 108562306a36Sopenharmony_ci seq_printf(m, "pci_unmap_sg() : %8ld calls %8ld pages (avg %d/1000)\n\n\n", 108662306a36Sopenharmony_ci ioc->usg_calls, ioc->usg_pages, 108762306a36Sopenharmony_ci (int)((ioc->usg_pages * 1000)/ioc->usg_calls)); 108862306a36Sopenharmony_ci#endif /* CCIO_COLLECT_STATS */ 108962306a36Sopenharmony_ci 109062306a36Sopenharmony_ci ioc = ioc->next; 109162306a36Sopenharmony_ci } 109262306a36Sopenharmony_ci 109362306a36Sopenharmony_ci return 0; 109462306a36Sopenharmony_ci} 109562306a36Sopenharmony_ci 109662306a36Sopenharmony_cistatic int ccio_proc_bitmap_info(struct seq_file *m, void *p) 109762306a36Sopenharmony_ci{ 109862306a36Sopenharmony_ci struct ioc *ioc = ioc_list; 109962306a36Sopenharmony_ci 110062306a36Sopenharmony_ci while (ioc != NULL) { 110162306a36Sopenharmony_ci seq_hex_dump(m, " ", DUMP_PREFIX_NONE, 32, 4, ioc->res_map, 110262306a36Sopenharmony_ci ioc->res_size, false); 110362306a36Sopenharmony_ci seq_putc(m, '\n'); 110462306a36Sopenharmony_ci ioc = ioc->next; 110562306a36Sopenharmony_ci break; /* XXX - remove me */ 110662306a36Sopenharmony_ci } 110762306a36Sopenharmony_ci 110862306a36Sopenharmony_ci return 0; 110962306a36Sopenharmony_ci} 111062306a36Sopenharmony_ci#endif /* CONFIG_PROC_FS */ 111162306a36Sopenharmony_ci 111262306a36Sopenharmony_ci/** 111362306a36Sopenharmony_ci * ccio_find_ioc - Find the ioc in the ioc_list 111462306a36Sopenharmony_ci * @hw_path: The hardware path of the ioc. 111562306a36Sopenharmony_ci * 111662306a36Sopenharmony_ci * This function searches the ioc_list for an ioc that matches 111762306a36Sopenharmony_ci * the provide hardware path. 111862306a36Sopenharmony_ci */ 111962306a36Sopenharmony_cistatic struct ioc * ccio_find_ioc(int hw_path) 112062306a36Sopenharmony_ci{ 112162306a36Sopenharmony_ci int i; 112262306a36Sopenharmony_ci struct ioc *ioc; 112362306a36Sopenharmony_ci 112462306a36Sopenharmony_ci ioc = ioc_list; 112562306a36Sopenharmony_ci for (i = 0; i < ioc_count; i++) { 112662306a36Sopenharmony_ci if (ioc->hw_path == hw_path) 112762306a36Sopenharmony_ci return ioc; 112862306a36Sopenharmony_ci 112962306a36Sopenharmony_ci ioc = ioc->next; 113062306a36Sopenharmony_ci } 113162306a36Sopenharmony_ci 113262306a36Sopenharmony_ci return NULL; 113362306a36Sopenharmony_ci} 113462306a36Sopenharmony_ci 113562306a36Sopenharmony_ci/** 113662306a36Sopenharmony_ci * ccio_get_iommu - Find the iommu which controls this device 113762306a36Sopenharmony_ci * @dev: The parisc device. 113862306a36Sopenharmony_ci * 113962306a36Sopenharmony_ci * This function searches through the registered IOMMU's and returns 114062306a36Sopenharmony_ci * the appropriate IOMMU for the device based on its hardware path. 114162306a36Sopenharmony_ci */ 114262306a36Sopenharmony_civoid * ccio_get_iommu(const struct parisc_device *dev) 114362306a36Sopenharmony_ci{ 114462306a36Sopenharmony_ci dev = find_pa_parent_type(dev, HPHW_IOA); 114562306a36Sopenharmony_ci if (!dev) 114662306a36Sopenharmony_ci return NULL; 114762306a36Sopenharmony_ci 114862306a36Sopenharmony_ci return ccio_find_ioc(dev->hw_path); 114962306a36Sopenharmony_ci} 115062306a36Sopenharmony_ci 115162306a36Sopenharmony_ci#define CUJO_20_STEP 0x10000000 /* inc upper nibble */ 115262306a36Sopenharmony_ci 115362306a36Sopenharmony_ci/* Cujo 2.0 has a bug which will silently corrupt data being transferred 115462306a36Sopenharmony_ci * to/from certain pages. To avoid this happening, we mark these pages 115562306a36Sopenharmony_ci * as `used', and ensure that nothing will try to allocate from them. 115662306a36Sopenharmony_ci */ 115762306a36Sopenharmony_civoid __init ccio_cujo20_fixup(struct parisc_device *cujo, u32 iovp) 115862306a36Sopenharmony_ci{ 115962306a36Sopenharmony_ci unsigned int idx; 116062306a36Sopenharmony_ci struct parisc_device *dev = parisc_parent(cujo); 116162306a36Sopenharmony_ci struct ioc *ioc = ccio_get_iommu(dev); 116262306a36Sopenharmony_ci u8 *res_ptr; 116362306a36Sopenharmony_ci 116462306a36Sopenharmony_ci ioc->cujo20_bug = 1; 116562306a36Sopenharmony_ci res_ptr = ioc->res_map; 116662306a36Sopenharmony_ci idx = PDIR_INDEX(iovp) >> 3; 116762306a36Sopenharmony_ci 116862306a36Sopenharmony_ci while (idx < ioc->res_size) { 116962306a36Sopenharmony_ci res_ptr[idx] |= 0xff; 117062306a36Sopenharmony_ci idx += PDIR_INDEX(CUJO_20_STEP) >> 3; 117162306a36Sopenharmony_ci } 117262306a36Sopenharmony_ci} 117362306a36Sopenharmony_ci 117462306a36Sopenharmony_ci#if 0 117562306a36Sopenharmony_ci/* GRANT - is this needed for U2 or not? */ 117662306a36Sopenharmony_ci 117762306a36Sopenharmony_ci/* 117862306a36Sopenharmony_ci** Get the size of the I/O TLB for this I/O MMU. 117962306a36Sopenharmony_ci** 118062306a36Sopenharmony_ci** If spa_shift is non-zero (ie probably U2), 118162306a36Sopenharmony_ci** then calculate the I/O TLB size using spa_shift. 118262306a36Sopenharmony_ci** 118362306a36Sopenharmony_ci** Otherwise we are supposed to get the IODC entry point ENTRY TLB 118462306a36Sopenharmony_ci** and execute it. However, both U2 and Uturn firmware supplies spa_shift. 118562306a36Sopenharmony_ci** I think only Java (K/D/R-class too?) systems don't do this. 118662306a36Sopenharmony_ci*/ 118762306a36Sopenharmony_cistatic int 118862306a36Sopenharmony_ciccio_get_iotlb_size(struct parisc_device *dev) 118962306a36Sopenharmony_ci{ 119062306a36Sopenharmony_ci if (dev->spa_shift == 0) { 119162306a36Sopenharmony_ci panic("%s() : Can't determine I/O TLB size.\n", __func__); 119262306a36Sopenharmony_ci } 119362306a36Sopenharmony_ci return (1 << dev->spa_shift); 119462306a36Sopenharmony_ci} 119562306a36Sopenharmony_ci#else 119662306a36Sopenharmony_ci 119762306a36Sopenharmony_ci/* Uturn supports 256 TLB entries */ 119862306a36Sopenharmony_ci#define CCIO_CHAINID_SHIFT 8 119962306a36Sopenharmony_ci#define CCIO_CHAINID_MASK 0xff 120062306a36Sopenharmony_ci#endif /* 0 */ 120162306a36Sopenharmony_ci 120262306a36Sopenharmony_ci/* We *can't* support JAVA (T600). Venture there at your own risk. */ 120362306a36Sopenharmony_cistatic const struct parisc_device_id ccio_tbl[] __initconst = { 120462306a36Sopenharmony_ci { HPHW_IOA, HVERSION_REV_ANY_ID, U2_IOA_RUNWAY, 0xb }, /* U2 */ 120562306a36Sopenharmony_ci { HPHW_IOA, HVERSION_REV_ANY_ID, UTURN_IOA_RUNWAY, 0xb }, /* UTurn */ 120662306a36Sopenharmony_ci { 0, } 120762306a36Sopenharmony_ci}; 120862306a36Sopenharmony_ci 120962306a36Sopenharmony_cistatic int ccio_probe(struct parisc_device *dev); 121062306a36Sopenharmony_ci 121162306a36Sopenharmony_cistatic struct parisc_driver ccio_driver __refdata = { 121262306a36Sopenharmony_ci .name = "ccio", 121362306a36Sopenharmony_ci .id_table = ccio_tbl, 121462306a36Sopenharmony_ci .probe = ccio_probe, 121562306a36Sopenharmony_ci}; 121662306a36Sopenharmony_ci 121762306a36Sopenharmony_ci/** 121862306a36Sopenharmony_ci * ccio_ioc_init - Initialize the I/O Controller 121962306a36Sopenharmony_ci * @ioc: The I/O Controller. 122062306a36Sopenharmony_ci * 122162306a36Sopenharmony_ci * Initialize the I/O Controller which includes setting up the 122262306a36Sopenharmony_ci * I/O Page Directory, the resource map, and initalizing the 122362306a36Sopenharmony_ci * U2/Uturn chip into virtual mode. 122462306a36Sopenharmony_ci */ 122562306a36Sopenharmony_cistatic void __init 122662306a36Sopenharmony_ciccio_ioc_init(struct ioc *ioc) 122762306a36Sopenharmony_ci{ 122862306a36Sopenharmony_ci int i; 122962306a36Sopenharmony_ci unsigned int iov_order; 123062306a36Sopenharmony_ci u32 iova_space_size; 123162306a36Sopenharmony_ci 123262306a36Sopenharmony_ci /* 123362306a36Sopenharmony_ci ** Determine IOVA Space size from memory size. 123462306a36Sopenharmony_ci ** 123562306a36Sopenharmony_ci ** Ideally, PCI drivers would register the maximum number 123662306a36Sopenharmony_ci ** of DMA they can have outstanding for each device they 123762306a36Sopenharmony_ci ** own. Next best thing would be to guess how much DMA 123862306a36Sopenharmony_ci ** can be outstanding based on PCI Class/sub-class. Both 123962306a36Sopenharmony_ci ** methods still require some "extra" to support PCI 124062306a36Sopenharmony_ci ** Hot-Plug/Removal of PCI cards. (aka PCI OLARD). 124162306a36Sopenharmony_ci */ 124262306a36Sopenharmony_ci 124362306a36Sopenharmony_ci iova_space_size = (u32) (totalram_pages() / count_parisc_driver(&ccio_driver)); 124462306a36Sopenharmony_ci 124562306a36Sopenharmony_ci /* limit IOVA space size to 1MB-1GB */ 124662306a36Sopenharmony_ci 124762306a36Sopenharmony_ci if (iova_space_size < (1 << (20 - PAGE_SHIFT))) { 124862306a36Sopenharmony_ci iova_space_size = 1 << (20 - PAGE_SHIFT); 124962306a36Sopenharmony_ci#ifdef __LP64__ 125062306a36Sopenharmony_ci } else if (iova_space_size > (1 << (30 - PAGE_SHIFT))) { 125162306a36Sopenharmony_ci iova_space_size = 1 << (30 - PAGE_SHIFT); 125262306a36Sopenharmony_ci#endif 125362306a36Sopenharmony_ci } 125462306a36Sopenharmony_ci 125562306a36Sopenharmony_ci /* 125662306a36Sopenharmony_ci ** iova space must be log2() in size. 125762306a36Sopenharmony_ci ** thus, pdir/res_map will also be log2(). 125862306a36Sopenharmony_ci */ 125962306a36Sopenharmony_ci 126062306a36Sopenharmony_ci /* We could use larger page sizes in order to *decrease* the number 126162306a36Sopenharmony_ci ** of mappings needed. (ie 8k pages means 1/2 the mappings). 126262306a36Sopenharmony_ci ** 126362306a36Sopenharmony_ci ** Note: Grant Grunder says "Using 8k I/O pages isn't trivial either 126462306a36Sopenharmony_ci ** since the pages must also be physically contiguous - typically 126562306a36Sopenharmony_ci ** this is the case under linux." 126662306a36Sopenharmony_ci */ 126762306a36Sopenharmony_ci 126862306a36Sopenharmony_ci iov_order = get_order(iova_space_size << PAGE_SHIFT); 126962306a36Sopenharmony_ci 127062306a36Sopenharmony_ci /* iova_space_size is now bytes, not pages */ 127162306a36Sopenharmony_ci iova_space_size = 1 << (iov_order + PAGE_SHIFT); 127262306a36Sopenharmony_ci 127362306a36Sopenharmony_ci ioc->pdir_size = (iova_space_size / IOVP_SIZE) * sizeof(u64); 127462306a36Sopenharmony_ci 127562306a36Sopenharmony_ci BUG_ON(ioc->pdir_size > 8 * 1024 * 1024); /* max pdir size <= 8MB */ 127662306a36Sopenharmony_ci 127762306a36Sopenharmony_ci /* Verify it's a power of two */ 127862306a36Sopenharmony_ci BUG_ON((1 << get_order(ioc->pdir_size)) != (ioc->pdir_size >> PAGE_SHIFT)); 127962306a36Sopenharmony_ci 128062306a36Sopenharmony_ci DBG_INIT("%s() hpa 0x%p mem %luMB IOV %dMB (%d bits)\n", 128162306a36Sopenharmony_ci __func__, ioc->ioc_regs, 128262306a36Sopenharmony_ci (unsigned long) totalram_pages() >> (20 - PAGE_SHIFT), 128362306a36Sopenharmony_ci iova_space_size>>20, 128462306a36Sopenharmony_ci iov_order + PAGE_SHIFT); 128562306a36Sopenharmony_ci 128662306a36Sopenharmony_ci ioc->pdir_base = (__le64 *)__get_free_pages(GFP_KERNEL, 128762306a36Sopenharmony_ci get_order(ioc->pdir_size)); 128862306a36Sopenharmony_ci if(NULL == ioc->pdir_base) { 128962306a36Sopenharmony_ci panic("%s() could not allocate I/O Page Table\n", __func__); 129062306a36Sopenharmony_ci } 129162306a36Sopenharmony_ci memset(ioc->pdir_base, 0, ioc->pdir_size); 129262306a36Sopenharmony_ci 129362306a36Sopenharmony_ci BUG_ON((((unsigned long)ioc->pdir_base) & PAGE_MASK) != (unsigned long)ioc->pdir_base); 129462306a36Sopenharmony_ci DBG_INIT(" base %p\n", ioc->pdir_base); 129562306a36Sopenharmony_ci 129662306a36Sopenharmony_ci /* resource map size dictated by pdir_size */ 129762306a36Sopenharmony_ci ioc->res_size = (ioc->pdir_size / sizeof(u64)) >> 3; 129862306a36Sopenharmony_ci DBG_INIT("%s() res_size 0x%x\n", __func__, ioc->res_size); 129962306a36Sopenharmony_ci 130062306a36Sopenharmony_ci ioc->res_map = (u8 *)__get_free_pages(GFP_KERNEL, 130162306a36Sopenharmony_ci get_order(ioc->res_size)); 130262306a36Sopenharmony_ci if(NULL == ioc->res_map) { 130362306a36Sopenharmony_ci panic("%s() could not allocate resource map\n", __func__); 130462306a36Sopenharmony_ci } 130562306a36Sopenharmony_ci memset(ioc->res_map, 0, ioc->res_size); 130662306a36Sopenharmony_ci 130762306a36Sopenharmony_ci /* Initialize the res_hint to 16 */ 130862306a36Sopenharmony_ci ioc->res_hint = 16; 130962306a36Sopenharmony_ci 131062306a36Sopenharmony_ci /* Initialize the spinlock */ 131162306a36Sopenharmony_ci spin_lock_init(&ioc->res_lock); 131262306a36Sopenharmony_ci 131362306a36Sopenharmony_ci /* 131462306a36Sopenharmony_ci ** Chainid is the upper most bits of an IOVP used to determine 131562306a36Sopenharmony_ci ** which TLB entry an IOVP will use. 131662306a36Sopenharmony_ci */ 131762306a36Sopenharmony_ci ioc->chainid_shift = get_order(iova_space_size) + PAGE_SHIFT - CCIO_CHAINID_SHIFT; 131862306a36Sopenharmony_ci DBG_INIT(" chainid_shift 0x%x\n", ioc->chainid_shift); 131962306a36Sopenharmony_ci 132062306a36Sopenharmony_ci /* 132162306a36Sopenharmony_ci ** Initialize IOA hardware 132262306a36Sopenharmony_ci */ 132362306a36Sopenharmony_ci WRITE_U32(CCIO_CHAINID_MASK << ioc->chainid_shift, 132462306a36Sopenharmony_ci &ioc->ioc_regs->io_chain_id_mask); 132562306a36Sopenharmony_ci 132662306a36Sopenharmony_ci WRITE_U32(virt_to_phys(ioc->pdir_base), 132762306a36Sopenharmony_ci &ioc->ioc_regs->io_pdir_base); 132862306a36Sopenharmony_ci 132962306a36Sopenharmony_ci /* 133062306a36Sopenharmony_ci ** Go to "Virtual Mode" 133162306a36Sopenharmony_ci */ 133262306a36Sopenharmony_ci WRITE_U32(IOA_NORMAL_MODE, &ioc->ioc_regs->io_control); 133362306a36Sopenharmony_ci 133462306a36Sopenharmony_ci /* 133562306a36Sopenharmony_ci ** Initialize all I/O TLB entries to 0 (Valid bit off). 133662306a36Sopenharmony_ci */ 133762306a36Sopenharmony_ci WRITE_U32(0, &ioc->ioc_regs->io_tlb_entry_m); 133862306a36Sopenharmony_ci WRITE_U32(0, &ioc->ioc_regs->io_tlb_entry_l); 133962306a36Sopenharmony_ci 134062306a36Sopenharmony_ci for(i = 1 << CCIO_CHAINID_SHIFT; i ; i--) { 134162306a36Sopenharmony_ci WRITE_U32((CMD_TLB_DIRECT_WRITE | (i << ioc->chainid_shift)), 134262306a36Sopenharmony_ci &ioc->ioc_regs->io_command); 134362306a36Sopenharmony_ci } 134462306a36Sopenharmony_ci} 134562306a36Sopenharmony_ci 134662306a36Sopenharmony_cistatic void __init 134762306a36Sopenharmony_ciccio_init_resource(struct resource *res, char *name, void __iomem *ioaddr) 134862306a36Sopenharmony_ci{ 134962306a36Sopenharmony_ci int result; 135062306a36Sopenharmony_ci 135162306a36Sopenharmony_ci res->parent = NULL; 135262306a36Sopenharmony_ci res->flags = IORESOURCE_MEM; 135362306a36Sopenharmony_ci /* 135462306a36Sopenharmony_ci * bracing ((signed) ...) are required for 64bit kernel because 135562306a36Sopenharmony_ci * we only want to sign extend the lower 16 bits of the register. 135662306a36Sopenharmony_ci * The upper 16-bits of range registers are hardcoded to 0xffff. 135762306a36Sopenharmony_ci */ 135862306a36Sopenharmony_ci res->start = (unsigned long)((signed) READ_U32(ioaddr) << 16); 135962306a36Sopenharmony_ci res->end = (unsigned long)((signed) (READ_U32(ioaddr + 4) << 16) - 1); 136062306a36Sopenharmony_ci res->name = name; 136162306a36Sopenharmony_ci /* 136262306a36Sopenharmony_ci * Check if this MMIO range is disable 136362306a36Sopenharmony_ci */ 136462306a36Sopenharmony_ci if (res->end + 1 == res->start) 136562306a36Sopenharmony_ci return; 136662306a36Sopenharmony_ci 136762306a36Sopenharmony_ci /* On some platforms (e.g. K-Class), we have already registered 136862306a36Sopenharmony_ci * resources for devices reported by firmware. Some are children 136962306a36Sopenharmony_ci * of ccio. 137062306a36Sopenharmony_ci * "insert" ccio ranges in the mmio hierarchy (/proc/iomem). 137162306a36Sopenharmony_ci */ 137262306a36Sopenharmony_ci result = insert_resource(&iomem_resource, res); 137362306a36Sopenharmony_ci if (result < 0) { 137462306a36Sopenharmony_ci printk(KERN_ERR "%s() failed to claim CCIO bus address space (%08lx,%08lx)\n", 137562306a36Sopenharmony_ci __func__, (unsigned long)res->start, (unsigned long)res->end); 137662306a36Sopenharmony_ci } 137762306a36Sopenharmony_ci} 137862306a36Sopenharmony_ci 137962306a36Sopenharmony_cistatic int __init ccio_init_resources(struct ioc *ioc) 138062306a36Sopenharmony_ci{ 138162306a36Sopenharmony_ci struct resource *res = ioc->mmio_region; 138262306a36Sopenharmony_ci char *name = kmalloc(14, GFP_KERNEL); 138362306a36Sopenharmony_ci if (unlikely(!name)) 138462306a36Sopenharmony_ci return -ENOMEM; 138562306a36Sopenharmony_ci snprintf(name, 14, "GSC Bus [%d/]", ioc->hw_path); 138662306a36Sopenharmony_ci 138762306a36Sopenharmony_ci ccio_init_resource(res, name, &ioc->ioc_regs->io_io_low); 138862306a36Sopenharmony_ci ccio_init_resource(res + 1, name, &ioc->ioc_regs->io_io_low_hv); 138962306a36Sopenharmony_ci return 0; 139062306a36Sopenharmony_ci} 139162306a36Sopenharmony_ci 139262306a36Sopenharmony_cistatic int new_ioc_area(struct resource *res, unsigned long size, 139362306a36Sopenharmony_ci unsigned long min, unsigned long max, unsigned long align) 139462306a36Sopenharmony_ci{ 139562306a36Sopenharmony_ci if (max <= min) 139662306a36Sopenharmony_ci return -EBUSY; 139762306a36Sopenharmony_ci 139862306a36Sopenharmony_ci res->start = (max - size + 1) &~ (align - 1); 139962306a36Sopenharmony_ci res->end = res->start + size; 140062306a36Sopenharmony_ci 140162306a36Sopenharmony_ci /* We might be trying to expand the MMIO range to include 140262306a36Sopenharmony_ci * a child device that has already registered it's MMIO space. 140362306a36Sopenharmony_ci * Use "insert" instead of request_resource(). 140462306a36Sopenharmony_ci */ 140562306a36Sopenharmony_ci if (!insert_resource(&iomem_resource, res)) 140662306a36Sopenharmony_ci return 0; 140762306a36Sopenharmony_ci 140862306a36Sopenharmony_ci return new_ioc_area(res, size, min, max - size, align); 140962306a36Sopenharmony_ci} 141062306a36Sopenharmony_ci 141162306a36Sopenharmony_cistatic int expand_ioc_area(struct resource *res, unsigned long size, 141262306a36Sopenharmony_ci unsigned long min, unsigned long max, unsigned long align) 141362306a36Sopenharmony_ci{ 141462306a36Sopenharmony_ci unsigned long start, len; 141562306a36Sopenharmony_ci 141662306a36Sopenharmony_ci if (!res->parent) 141762306a36Sopenharmony_ci return new_ioc_area(res, size, min, max, align); 141862306a36Sopenharmony_ci 141962306a36Sopenharmony_ci start = (res->start - size) &~ (align - 1); 142062306a36Sopenharmony_ci len = res->end - start + 1; 142162306a36Sopenharmony_ci if (start >= min) { 142262306a36Sopenharmony_ci if (!adjust_resource(res, start, len)) 142362306a36Sopenharmony_ci return 0; 142462306a36Sopenharmony_ci } 142562306a36Sopenharmony_ci 142662306a36Sopenharmony_ci start = res->start; 142762306a36Sopenharmony_ci len = ((size + res->end + align) &~ (align - 1)) - start; 142862306a36Sopenharmony_ci if (start + len <= max) { 142962306a36Sopenharmony_ci if (!adjust_resource(res, start, len)) 143062306a36Sopenharmony_ci return 0; 143162306a36Sopenharmony_ci } 143262306a36Sopenharmony_ci 143362306a36Sopenharmony_ci return -EBUSY; 143462306a36Sopenharmony_ci} 143562306a36Sopenharmony_ci 143662306a36Sopenharmony_ci/* 143762306a36Sopenharmony_ci * Dino calls this function. Beware that we may get called on systems 143862306a36Sopenharmony_ci * which have no IOC (725, B180, C160L, etc) but do have a Dino. 143962306a36Sopenharmony_ci * So it's legal to find no parent IOC. 144062306a36Sopenharmony_ci * 144162306a36Sopenharmony_ci * Some other issues: one of the resources in the ioc may be unassigned. 144262306a36Sopenharmony_ci */ 144362306a36Sopenharmony_ciint ccio_allocate_resource(const struct parisc_device *dev, 144462306a36Sopenharmony_ci struct resource *res, unsigned long size, 144562306a36Sopenharmony_ci unsigned long min, unsigned long max, unsigned long align) 144662306a36Sopenharmony_ci{ 144762306a36Sopenharmony_ci struct resource *parent = &iomem_resource; 144862306a36Sopenharmony_ci struct ioc *ioc = ccio_get_iommu(dev); 144962306a36Sopenharmony_ci if (!ioc) 145062306a36Sopenharmony_ci goto out; 145162306a36Sopenharmony_ci 145262306a36Sopenharmony_ci parent = ioc->mmio_region; 145362306a36Sopenharmony_ci if (parent->parent && 145462306a36Sopenharmony_ci !allocate_resource(parent, res, size, min, max, align, NULL, NULL)) 145562306a36Sopenharmony_ci return 0; 145662306a36Sopenharmony_ci 145762306a36Sopenharmony_ci if ((parent + 1)->parent && 145862306a36Sopenharmony_ci !allocate_resource(parent + 1, res, size, min, max, align, 145962306a36Sopenharmony_ci NULL, NULL)) 146062306a36Sopenharmony_ci return 0; 146162306a36Sopenharmony_ci 146262306a36Sopenharmony_ci if (!expand_ioc_area(parent, size, min, max, align)) { 146362306a36Sopenharmony_ci __raw_writel(((parent->start)>>16) | 0xffff0000, 146462306a36Sopenharmony_ci &ioc->ioc_regs->io_io_low); 146562306a36Sopenharmony_ci __raw_writel(((parent->end)>>16) | 0xffff0000, 146662306a36Sopenharmony_ci &ioc->ioc_regs->io_io_high); 146762306a36Sopenharmony_ci } else if (!expand_ioc_area(parent + 1, size, min, max, align)) { 146862306a36Sopenharmony_ci parent++; 146962306a36Sopenharmony_ci __raw_writel(((parent->start)>>16) | 0xffff0000, 147062306a36Sopenharmony_ci &ioc->ioc_regs->io_io_low_hv); 147162306a36Sopenharmony_ci __raw_writel(((parent->end)>>16) | 0xffff0000, 147262306a36Sopenharmony_ci &ioc->ioc_regs->io_io_high_hv); 147362306a36Sopenharmony_ci } else { 147462306a36Sopenharmony_ci return -EBUSY; 147562306a36Sopenharmony_ci } 147662306a36Sopenharmony_ci 147762306a36Sopenharmony_ci out: 147862306a36Sopenharmony_ci return allocate_resource(parent, res, size, min, max, align, NULL,NULL); 147962306a36Sopenharmony_ci} 148062306a36Sopenharmony_ci 148162306a36Sopenharmony_ciint ccio_request_resource(const struct parisc_device *dev, 148262306a36Sopenharmony_ci struct resource *res) 148362306a36Sopenharmony_ci{ 148462306a36Sopenharmony_ci struct resource *parent; 148562306a36Sopenharmony_ci struct ioc *ioc = ccio_get_iommu(dev); 148662306a36Sopenharmony_ci 148762306a36Sopenharmony_ci if (!ioc) { 148862306a36Sopenharmony_ci parent = &iomem_resource; 148962306a36Sopenharmony_ci } else if ((ioc->mmio_region->start <= res->start) && 149062306a36Sopenharmony_ci (res->end <= ioc->mmio_region->end)) { 149162306a36Sopenharmony_ci parent = ioc->mmio_region; 149262306a36Sopenharmony_ci } else if (((ioc->mmio_region + 1)->start <= res->start) && 149362306a36Sopenharmony_ci (res->end <= (ioc->mmio_region + 1)->end)) { 149462306a36Sopenharmony_ci parent = ioc->mmio_region + 1; 149562306a36Sopenharmony_ci } else { 149662306a36Sopenharmony_ci return -EBUSY; 149762306a36Sopenharmony_ci } 149862306a36Sopenharmony_ci 149962306a36Sopenharmony_ci /* "transparent" bus bridges need to register MMIO resources 150062306a36Sopenharmony_ci * firmware assigned them. e.g. children of hppb.c (e.g. K-class) 150162306a36Sopenharmony_ci * registered their resources in the PDC "bus walk" (See 150262306a36Sopenharmony_ci * arch/parisc/kernel/inventory.c). 150362306a36Sopenharmony_ci */ 150462306a36Sopenharmony_ci return insert_resource(parent, res); 150562306a36Sopenharmony_ci} 150662306a36Sopenharmony_ci 150762306a36Sopenharmony_ci/** 150862306a36Sopenharmony_ci * ccio_probe - Determine if ccio should claim this device. 150962306a36Sopenharmony_ci * @dev: The device which has been found 151062306a36Sopenharmony_ci * 151162306a36Sopenharmony_ci * Determine if ccio should claim this chip (return 0) or not (return 1). 151262306a36Sopenharmony_ci * If so, initialize the chip and tell other partners in crime they 151362306a36Sopenharmony_ci * have work to do. 151462306a36Sopenharmony_ci */ 151562306a36Sopenharmony_cistatic int __init ccio_probe(struct parisc_device *dev) 151662306a36Sopenharmony_ci{ 151762306a36Sopenharmony_ci int i; 151862306a36Sopenharmony_ci struct ioc *ioc, **ioc_p = &ioc_list; 151962306a36Sopenharmony_ci struct pci_hba_data *hba; 152062306a36Sopenharmony_ci 152162306a36Sopenharmony_ci ioc = kzalloc(sizeof(struct ioc), GFP_KERNEL); 152262306a36Sopenharmony_ci if (ioc == NULL) { 152362306a36Sopenharmony_ci printk(KERN_ERR MODULE_NAME ": memory allocation failure\n"); 152462306a36Sopenharmony_ci return -ENOMEM; 152562306a36Sopenharmony_ci } 152662306a36Sopenharmony_ci 152762306a36Sopenharmony_ci ioc->name = dev->id.hversion == U2_IOA_RUNWAY ? "U2" : "UTurn"; 152862306a36Sopenharmony_ci 152962306a36Sopenharmony_ci printk(KERN_INFO "Found %s at 0x%lx\n", ioc->name, 153062306a36Sopenharmony_ci (unsigned long)dev->hpa.start); 153162306a36Sopenharmony_ci 153262306a36Sopenharmony_ci for (i = 0; i < ioc_count; i++) { 153362306a36Sopenharmony_ci ioc_p = &(*ioc_p)->next; 153462306a36Sopenharmony_ci } 153562306a36Sopenharmony_ci *ioc_p = ioc; 153662306a36Sopenharmony_ci 153762306a36Sopenharmony_ci ioc->hw_path = dev->hw_path; 153862306a36Sopenharmony_ci ioc->ioc_regs = ioremap(dev->hpa.start, 4096); 153962306a36Sopenharmony_ci if (!ioc->ioc_regs) { 154062306a36Sopenharmony_ci kfree(ioc); 154162306a36Sopenharmony_ci return -ENOMEM; 154262306a36Sopenharmony_ci } 154362306a36Sopenharmony_ci ccio_ioc_init(ioc); 154462306a36Sopenharmony_ci if (ccio_init_resources(ioc)) { 154562306a36Sopenharmony_ci iounmap(ioc->ioc_regs); 154662306a36Sopenharmony_ci kfree(ioc); 154762306a36Sopenharmony_ci return -ENOMEM; 154862306a36Sopenharmony_ci } 154962306a36Sopenharmony_ci hppa_dma_ops = &ccio_ops; 155062306a36Sopenharmony_ci 155162306a36Sopenharmony_ci hba = kzalloc(sizeof(*hba), GFP_KERNEL); 155262306a36Sopenharmony_ci /* if this fails, no I/O cards will work, so may as well bug */ 155362306a36Sopenharmony_ci BUG_ON(hba == NULL); 155462306a36Sopenharmony_ci 155562306a36Sopenharmony_ci hba->iommu = ioc; 155662306a36Sopenharmony_ci dev->dev.platform_data = hba; 155762306a36Sopenharmony_ci 155862306a36Sopenharmony_ci#ifdef CONFIG_PROC_FS 155962306a36Sopenharmony_ci if (ioc_count == 0) { 156062306a36Sopenharmony_ci struct proc_dir_entry *runway; 156162306a36Sopenharmony_ci 156262306a36Sopenharmony_ci runway = proc_mkdir("bus/runway", NULL); 156362306a36Sopenharmony_ci if (runway) { 156462306a36Sopenharmony_ci proc_create_single(MODULE_NAME, 0, runway, 156562306a36Sopenharmony_ci ccio_proc_info); 156662306a36Sopenharmony_ci proc_create_single(MODULE_NAME"-bitmap", 0, runway, 156762306a36Sopenharmony_ci ccio_proc_bitmap_info); 156862306a36Sopenharmony_ci } 156962306a36Sopenharmony_ci } 157062306a36Sopenharmony_ci#endif 157162306a36Sopenharmony_ci ioc_count++; 157262306a36Sopenharmony_ci return 0; 157362306a36Sopenharmony_ci} 157462306a36Sopenharmony_ci 157562306a36Sopenharmony_ci/** 157662306a36Sopenharmony_ci * ccio_init - ccio initialization procedure. 157762306a36Sopenharmony_ci * 157862306a36Sopenharmony_ci * Register this driver. 157962306a36Sopenharmony_ci */ 158062306a36Sopenharmony_cistatic int __init ccio_init(void) 158162306a36Sopenharmony_ci{ 158262306a36Sopenharmony_ci return register_parisc_driver(&ccio_driver); 158362306a36Sopenharmony_ci} 158462306a36Sopenharmony_ciarch_initcall(ccio_init); 1585