1*4882a593Smuzhiyun.. Copyright 2001 Matthew Wilcox 2*4882a593Smuzhiyun.. 3*4882a593Smuzhiyun.. This documentation is free software; you can redistribute 4*4882a593Smuzhiyun.. it and/or modify it under the terms of the GNU General Public 5*4882a593Smuzhiyun.. License as published by the Free Software Foundation; either 6*4882a593Smuzhiyun.. version 2 of the License, or (at your option) any later 7*4882a593Smuzhiyun.. version. 8*4882a593Smuzhiyun 9*4882a593Smuzhiyun=============================== 10*4882a593SmuzhiyunBus-Independent Device Accesses 11*4882a593Smuzhiyun=============================== 12*4882a593Smuzhiyun 13*4882a593Smuzhiyun:Author: Matthew Wilcox 14*4882a593Smuzhiyun:Author: Alan Cox 15*4882a593Smuzhiyun 16*4882a593SmuzhiyunIntroduction 17*4882a593Smuzhiyun============ 18*4882a593Smuzhiyun 19*4882a593SmuzhiyunLinux provides an API which abstracts performing IO across all busses 20*4882a593Smuzhiyunand devices, allowing device drivers to be written independently of bus 21*4882a593Smuzhiyuntype. 22*4882a593Smuzhiyun 23*4882a593SmuzhiyunMemory Mapped IO 24*4882a593Smuzhiyun================ 25*4882a593Smuzhiyun 26*4882a593SmuzhiyunGetting Access to the Device 27*4882a593Smuzhiyun---------------------------- 28*4882a593Smuzhiyun 29*4882a593SmuzhiyunThe most widely supported form of IO is memory mapped IO. That is, a 30*4882a593Smuzhiyunpart of the CPU's address space is interpreted not as accesses to 31*4882a593Smuzhiyunmemory, but as accesses to a device. Some architectures define devices 32*4882a593Smuzhiyunto be at a fixed address, but most have some method of discovering 33*4882a593Smuzhiyundevices. The PCI bus walk is a good example of such a scheme. This 34*4882a593Smuzhiyundocument does not cover how to receive such an address, but assumes you 35*4882a593Smuzhiyunare starting with one. Physical addresses are of type unsigned long. 36*4882a593Smuzhiyun 37*4882a593SmuzhiyunThis address should not be used directly. Instead, to get an address 38*4882a593Smuzhiyunsuitable for passing to the accessor functions described below, you 39*4882a593Smuzhiyunshould call ioremap(). An address suitable for accessing 40*4882a593Smuzhiyunthe device will be returned to you. 41*4882a593Smuzhiyun 42*4882a593SmuzhiyunAfter you've finished using the device (say, in your module's exit 43*4882a593Smuzhiyunroutine), call iounmap() in order to return the address 44*4882a593Smuzhiyunspace to the kernel. Most architectures allocate new address space each 45*4882a593Smuzhiyuntime you call ioremap(), and they can run out unless you 46*4882a593Smuzhiyuncall iounmap(). 47*4882a593Smuzhiyun 48*4882a593SmuzhiyunAccessing the device 49*4882a593Smuzhiyun-------------------- 50*4882a593Smuzhiyun 51*4882a593SmuzhiyunThe part of the interface most used by drivers is reading and writing 52*4882a593Smuzhiyunmemory-mapped registers on the device. Linux provides interfaces to read 53*4882a593Smuzhiyunand write 8-bit, 16-bit, 32-bit and 64-bit quantities. Due to a 54*4882a593Smuzhiyunhistorical accident, these are named byte, word, long and quad accesses. 55*4882a593SmuzhiyunBoth read and write accesses are supported; there is no prefetch support 56*4882a593Smuzhiyunat this time. 57*4882a593Smuzhiyun 58*4882a593SmuzhiyunThe functions are named readb(), readw(), readl(), readq(), 59*4882a593Smuzhiyunreadb_relaxed(), readw_relaxed(), readl_relaxed(), readq_relaxed(), 60*4882a593Smuzhiyunwriteb(), writew(), writel() and writeq(). 61*4882a593Smuzhiyun 62*4882a593SmuzhiyunSome devices (such as framebuffers) would like to use larger transfers than 63*4882a593Smuzhiyun8 bytes at a time. For these devices, the memcpy_toio(), 64*4882a593Smuzhiyunmemcpy_fromio() and memset_io() functions are 65*4882a593Smuzhiyunprovided. Do not use memset or memcpy on IO addresses; they are not 66*4882a593Smuzhiyunguaranteed to copy data in order. 67*4882a593Smuzhiyun 68*4882a593SmuzhiyunThe read and write functions are defined to be ordered. That is the 69*4882a593Smuzhiyuncompiler is not permitted to reorder the I/O sequence. When the ordering 70*4882a593Smuzhiyuncan be compiler optimised, you can use __readb() and friends to 71*4882a593Smuzhiyunindicate the relaxed ordering. Use this with care. 72*4882a593Smuzhiyun 73*4882a593SmuzhiyunWhile the basic functions are defined to be synchronous with respect to 74*4882a593Smuzhiyuneach other and ordered with respect to each other the busses the devices 75*4882a593Smuzhiyunsit on may themselves have asynchronicity. In particular many authors 76*4882a593Smuzhiyunare burned by the fact that PCI bus writes are posted asynchronously. A 77*4882a593Smuzhiyundriver author must issue a read from the same device to ensure that 78*4882a593Smuzhiyunwrites have occurred in the specific cases the author cares. This kind 79*4882a593Smuzhiyunof property cannot be hidden from driver writers in the API. In some 80*4882a593Smuzhiyuncases, the read used to flush the device may be expected to fail (if the 81*4882a593Smuzhiyuncard is resetting, for example). In that case, the read should be done 82*4882a593Smuzhiyunfrom config space, which is guaranteed to soft-fail if the card doesn't 83*4882a593Smuzhiyunrespond. 84*4882a593Smuzhiyun 85*4882a593SmuzhiyunThe following is an example of flushing a write to a device when the 86*4882a593Smuzhiyundriver would like to ensure the write's effects are visible prior to 87*4882a593Smuzhiyuncontinuing execution:: 88*4882a593Smuzhiyun 89*4882a593Smuzhiyun static inline void 90*4882a593Smuzhiyun qla1280_disable_intrs(struct scsi_qla_host *ha) 91*4882a593Smuzhiyun { 92*4882a593Smuzhiyun struct device_reg *reg; 93*4882a593Smuzhiyun 94*4882a593Smuzhiyun reg = ha->iobase; 95*4882a593Smuzhiyun /* disable risc and host interrupts */ 96*4882a593Smuzhiyun WRT_REG_WORD(®->ictrl, 0); 97*4882a593Smuzhiyun /* 98*4882a593Smuzhiyun * The following read will ensure that the above write 99*4882a593Smuzhiyun * has been received by the device before we return from this 100*4882a593Smuzhiyun * function. 101*4882a593Smuzhiyun */ 102*4882a593Smuzhiyun RD_REG_WORD(®->ictrl); 103*4882a593Smuzhiyun ha->flags.ints_enabled = 0; 104*4882a593Smuzhiyun } 105*4882a593Smuzhiyun 106*4882a593SmuzhiyunPCI ordering rules also guarantee that PIO read responses arrive after any 107*4882a593Smuzhiyunoutstanding DMA writes from that bus, since for some devices the result of 108*4882a593Smuzhiyuna readb() call may signal to the driver that a DMA transaction is 109*4882a593Smuzhiyuncomplete. In many cases, however, the driver may want to indicate that the 110*4882a593Smuzhiyunnext readb() call has no relation to any previous DMA writes 111*4882a593Smuzhiyunperformed by the device. The driver can use readb_relaxed() for 112*4882a593Smuzhiyunthese cases, although only some platforms will honor the relaxed 113*4882a593Smuzhiyunsemantics. Using the relaxed read functions will provide significant 114*4882a593Smuzhiyunperformance benefits on platforms that support it. The qla2xxx driver 115*4882a593Smuzhiyunprovides examples of how to use readX_relaxed(). In many cases, a majority 116*4882a593Smuzhiyunof the driver's readX() calls can safely be converted to readX_relaxed() 117*4882a593Smuzhiyuncalls, since only a few will indicate or depend on DMA completion. 118*4882a593Smuzhiyun 119*4882a593SmuzhiyunPort Space Accesses 120*4882a593Smuzhiyun=================== 121*4882a593Smuzhiyun 122*4882a593SmuzhiyunPort Space Explained 123*4882a593Smuzhiyun-------------------- 124*4882a593Smuzhiyun 125*4882a593SmuzhiyunAnother form of IO commonly supported is Port Space. This is a range of 126*4882a593Smuzhiyunaddresses separate to the normal memory address space. Access to these 127*4882a593Smuzhiyunaddresses is generally not as fast as accesses to the memory mapped 128*4882a593Smuzhiyunaddresses, and it also has a potentially smaller address space. 129*4882a593Smuzhiyun 130*4882a593SmuzhiyunUnlike memory mapped IO, no preparation is required to access port 131*4882a593Smuzhiyunspace. 132*4882a593Smuzhiyun 133*4882a593SmuzhiyunAccessing Port Space 134*4882a593Smuzhiyun-------------------- 135*4882a593Smuzhiyun 136*4882a593SmuzhiyunAccesses to this space are provided through a set of functions which 137*4882a593Smuzhiyunallow 8-bit, 16-bit and 32-bit accesses; also known as byte, word and 138*4882a593Smuzhiyunlong. These functions are inb(), inw(), 139*4882a593Smuzhiyuninl(), outb(), outw() and 140*4882a593Smuzhiyunoutl(). 141*4882a593Smuzhiyun 142*4882a593SmuzhiyunSome variants are provided for these functions. Some devices require 143*4882a593Smuzhiyunthat accesses to their ports are slowed down. This functionality is 144*4882a593Smuzhiyunprovided by appending a ``_p`` to the end of the function. 145*4882a593SmuzhiyunThere are also equivalents to memcpy. The ins() and 146*4882a593Smuzhiyunouts() functions copy bytes, words or longs to the given 147*4882a593Smuzhiyunport. 148*4882a593Smuzhiyun 149*4882a593SmuzhiyunPublic Functions Provided 150*4882a593Smuzhiyun========================= 151*4882a593Smuzhiyun 152*4882a593Smuzhiyun.. kernel-doc:: arch/x86/include/asm/io.h 153*4882a593Smuzhiyun :internal: 154*4882a593Smuzhiyun 155*4882a593Smuzhiyun.. kernel-doc:: lib/pci_iomap.c 156*4882a593Smuzhiyun :export: 157