1*4882a593Smuzhiyun========================================== 2*4882a593SmuzhiyunXillybus driver for generic FPGA interface 3*4882a593Smuzhiyun========================================== 4*4882a593Smuzhiyun 5*4882a593Smuzhiyun:Author: Eli Billauer, Xillybus Ltd. (http://xillybus.com) 6*4882a593Smuzhiyun:Email: eli.billauer@gmail.com or as advertised on Xillybus' site. 7*4882a593Smuzhiyun 8*4882a593Smuzhiyun.. Contents: 9*4882a593Smuzhiyun 10*4882a593Smuzhiyun - Introduction 11*4882a593Smuzhiyun -- Background 12*4882a593Smuzhiyun -- Xillybus Overview 13*4882a593Smuzhiyun 14*4882a593Smuzhiyun - Usage 15*4882a593Smuzhiyun -- User interface 16*4882a593Smuzhiyun -- Synchronization 17*4882a593Smuzhiyun -- Seekable pipes 18*4882a593Smuzhiyun 19*4882a593Smuzhiyun - Internals 20*4882a593Smuzhiyun -- Source code organization 21*4882a593Smuzhiyun -- Pipe attributes 22*4882a593Smuzhiyun -- Host never reads from the FPGA 23*4882a593Smuzhiyun -- Channels, pipes, and the message channel 24*4882a593Smuzhiyun -- Data streaming 25*4882a593Smuzhiyun -- Data granularity 26*4882a593Smuzhiyun -- Probing 27*4882a593Smuzhiyun -- Buffer allocation 28*4882a593Smuzhiyun -- The "nonempty" message (supporting poll) 29*4882a593Smuzhiyun 30*4882a593Smuzhiyun 31*4882a593SmuzhiyunIntroduction 32*4882a593Smuzhiyun============ 33*4882a593Smuzhiyun 34*4882a593SmuzhiyunBackground 35*4882a593Smuzhiyun---------- 36*4882a593Smuzhiyun 37*4882a593SmuzhiyunAn FPGA (Field Programmable Gate Array) is a piece of logic hardware, which 38*4882a593Smuzhiyuncan be programmed to become virtually anything that is usually found as a 39*4882a593Smuzhiyundedicated chipset: For instance, a display adapter, network interface card, 40*4882a593Smuzhiyunor even a processor with its peripherals. FPGAs are the LEGO of hardware: 41*4882a593SmuzhiyunBased upon certain building blocks, you make your own toys the way you like 42*4882a593Smuzhiyunthem. It's usually pointless to reimplement something that is already 43*4882a593Smuzhiyunavailable on the market as a chipset, so FPGAs are mostly used when some 44*4882a593Smuzhiyunspecial functionality is needed, and the production volume is relatively low 45*4882a593Smuzhiyun(hence not justifying the development of an ASIC). 46*4882a593Smuzhiyun 47*4882a593SmuzhiyunThe challenge with FPGAs is that everything is implemented at a very low 48*4882a593Smuzhiyunlevel, even lower than assembly language. In order to allow FPGA designers to 49*4882a593Smuzhiyunfocus on their specific project, and not reinvent the wheel over and over 50*4882a593Smuzhiyunagain, pre-designed building blocks, IP cores, are often used. These are the 51*4882a593SmuzhiyunFPGA parallels of library functions. IP cores may implement certain 52*4882a593Smuzhiyunmathematical functions, a functional unit (e.g. a USB interface), an entire 53*4882a593Smuzhiyunprocessor (e.g. ARM) or anything that might come handy. Think of them as a 54*4882a593Smuzhiyunbuilding block, with electrical wires dangling on the sides for connection to 55*4882a593Smuzhiyunother blocks. 56*4882a593Smuzhiyun 57*4882a593SmuzhiyunOne of the daunting tasks in FPGA design is communicating with a fullblown 58*4882a593Smuzhiyunoperating system (actually, with the processor running it): Implementing the 59*4882a593Smuzhiyunlow-level bus protocol and the somewhat higher-level interface with the host 60*4882a593Smuzhiyun(registers, interrupts, DMA etc.) is a project in itself. When the FPGA's 61*4882a593Smuzhiyunfunction is a well-known one (e.g. a video adapter card, or a NIC), it can 62*4882a593Smuzhiyunmake sense to design the FPGA's interface logic specifically for the project. 63*4882a593SmuzhiyunA special driver is then written to present the FPGA as a well-known interface 64*4882a593Smuzhiyunto the kernel and/or user space. In that case, there is no reason to treat the 65*4882a593SmuzhiyunFPGA differently than any device on the bus. 66*4882a593Smuzhiyun 67*4882a593SmuzhiyunIt's however common that the desired data communication doesn't fit any well- 68*4882a593Smuzhiyunknown peripheral function. Also, the effort of designing an elegant 69*4882a593Smuzhiyunabstraction for the data exchange is often considered too big. In those cases, 70*4882a593Smuzhiyuna quicker and possibly less elegant solution is sought: The driver is 71*4882a593Smuzhiyuneffectively written as a user space program, leaving the kernel space part 72*4882a593Smuzhiyunwith just elementary data transport. This still requires designing some 73*4882a593Smuzhiyuninterface logic for the FPGA, and write a simple ad-hoc driver for the kernel. 74*4882a593Smuzhiyun 75*4882a593SmuzhiyunXillybus Overview 76*4882a593Smuzhiyun----------------- 77*4882a593Smuzhiyun 78*4882a593SmuzhiyunXillybus is an IP core and a Linux driver. Together, they form a kit for 79*4882a593Smuzhiyunelementary data transport between an FPGA and the host, providing pipe-like 80*4882a593Smuzhiyundata streams with a straightforward user interface. It's intended as a low- 81*4882a593Smuzhiyuneffort solution for mixed FPGA-host projects, for which it makes sense to 82*4882a593Smuzhiyunhave the project-specific part of the driver running in a user-space program. 83*4882a593Smuzhiyun 84*4882a593SmuzhiyunSince the communication requirements may vary significantly from one FPGA 85*4882a593Smuzhiyunproject to another (the number of data pipes needed in each direction and 86*4882a593Smuzhiyuntheir attributes), there isn't one specific chunk of logic being the Xillybus 87*4882a593SmuzhiyunIP core. Rather, the IP core is configured and built based upon a 88*4882a593Smuzhiyunspecification given by its end user. 89*4882a593Smuzhiyun 90*4882a593SmuzhiyunXillybus presents independent data streams, which resemble pipes or TCP/IP 91*4882a593Smuzhiyuncommunication to the user. At the host side, a character device file is used 92*4882a593Smuzhiyunjust like any pipe file. On the FPGA side, hardware FIFOs are used to stream 93*4882a593Smuzhiyunthe data. This is contrary to a common method of communicating through fixed- 94*4882a593Smuzhiyunsized buffers (even though such buffers are used by Xillybus under the hood). 95*4882a593SmuzhiyunThere may be more than a hundred of these streams on a single IP core, but 96*4882a593Smuzhiyunalso no more than one, depending on the configuration. 97*4882a593Smuzhiyun 98*4882a593SmuzhiyunIn order to ease the deployment of the Xillybus IP core, it contains a simple 99*4882a593Smuzhiyundata structure which completely defines the core's configuration. The Linux 100*4882a593Smuzhiyundriver fetches this data structure during its initialization process, and sets 101*4882a593Smuzhiyunup the DMA buffers and character devices accordingly. As a result, a single 102*4882a593Smuzhiyundriver is used to work out of the box with any Xillybus IP core. 103*4882a593Smuzhiyun 104*4882a593SmuzhiyunThe data structure just mentioned should not be confused with PCI's 105*4882a593Smuzhiyunconfiguration space or the Flattened Device Tree. 106*4882a593Smuzhiyun 107*4882a593SmuzhiyunUsage 108*4882a593Smuzhiyun===== 109*4882a593Smuzhiyun 110*4882a593SmuzhiyunUser interface 111*4882a593Smuzhiyun-------------- 112*4882a593Smuzhiyun 113*4882a593SmuzhiyunOn the host, all interface with Xillybus is done through /dev/xillybus_* 114*4882a593Smuzhiyundevice files, which are generated automatically as the drivers loads. The 115*4882a593Smuzhiyunnames of these files depend on the IP core that is loaded in the FPGA (see 116*4882a593SmuzhiyunProbing below). To communicate with the FPGA, open the device file that 117*4882a593Smuzhiyuncorresponds to the hardware FIFO you want to send data or receive data from, 118*4882a593Smuzhiyunand use plain write() or read() calls, just like with a regular pipe. In 119*4882a593Smuzhiyunparticular, it makes perfect sense to go:: 120*4882a593Smuzhiyun 121*4882a593Smuzhiyun $ cat mydata > /dev/xillybus_thisfifo 122*4882a593Smuzhiyun 123*4882a593Smuzhiyun $ cat /dev/xillybus_thatfifo > hisdata 124*4882a593Smuzhiyun 125*4882a593Smuzhiyunpossibly pressing CTRL-C as some stage, even though the xillybus_* pipes have 126*4882a593Smuzhiyunthe capability to send an EOF (but may not use it). 127*4882a593Smuzhiyun 128*4882a593SmuzhiyunThe driver and hardware are designed to behave sensibly as pipes, including: 129*4882a593Smuzhiyun 130*4882a593Smuzhiyun* Supporting non-blocking I/O (by setting O_NONBLOCK on open() ). 131*4882a593Smuzhiyun 132*4882a593Smuzhiyun* Supporting poll() and select(). 133*4882a593Smuzhiyun 134*4882a593Smuzhiyun* Being bandwidth efficient under load (using DMA) but also handle small 135*4882a593Smuzhiyun pieces of data sent across (like TCP/IP) by autoflushing. 136*4882a593Smuzhiyun 137*4882a593SmuzhiyunA device file can be read only, write only or bidirectional. Bidirectional 138*4882a593Smuzhiyundevice files are treated like two independent pipes (except for sharing a 139*4882a593Smuzhiyun"channel" structure in the implementation code). 140*4882a593Smuzhiyun 141*4882a593SmuzhiyunSynchronization 142*4882a593Smuzhiyun--------------- 143*4882a593Smuzhiyun 144*4882a593SmuzhiyunXillybus pipes are configured (on the IP core) to be either synchronous or 145*4882a593Smuzhiyunasynchronous. For a synchronous pipe, write() returns successfully only after 146*4882a593Smuzhiyunsome data has been submitted and acknowledged by the FPGA. This slows down 147*4882a593Smuzhiyunbulk data transfers, and is nearly impossible for use with streams that 148*4882a593Smuzhiyunrequire data at a constant rate: There is no data transmitted to the FPGA 149*4882a593Smuzhiyunbetween write() calls, in particular when the process loses the CPU. 150*4882a593Smuzhiyun 151*4882a593SmuzhiyunWhen a pipe is configured asynchronous, write() returns if there was enough 152*4882a593Smuzhiyunroom in the buffers to store any of the data in the buffers. 153*4882a593Smuzhiyun 154*4882a593SmuzhiyunFor FPGA to host pipes, asynchronous pipes allow data transfer from the FPGA 155*4882a593Smuzhiyunas soon as the respective device file is opened, regardless of if the data 156*4882a593Smuzhiyunhas been requested by a read() call. On synchronous pipes, only the amount 157*4882a593Smuzhiyunof data requested by a read() call is transmitted. 158*4882a593Smuzhiyun 159*4882a593SmuzhiyunIn summary, for synchronous pipes, data between the host and FPGA is 160*4882a593Smuzhiyuntransmitted only to satisfy the read() or write() call currently handled 161*4882a593Smuzhiyunby the driver, and those calls wait for the transmission to complete before 162*4882a593Smuzhiyunreturning. 163*4882a593Smuzhiyun 164*4882a593SmuzhiyunNote that the synchronization attribute has nothing to do with the possibility 165*4882a593Smuzhiyunthat read() or write() completes less bytes than requested. There is a 166*4882a593Smuzhiyunseparate configuration flag ("allowpartial") that determines whether such a 167*4882a593Smuzhiyunpartial completion is allowed. 168*4882a593Smuzhiyun 169*4882a593SmuzhiyunSeekable pipes 170*4882a593Smuzhiyun-------------- 171*4882a593Smuzhiyun 172*4882a593SmuzhiyunA synchronous pipe can be configured to have the stream's position exposed 173*4882a593Smuzhiyunto the user logic at the FPGA. Such a pipe is also seekable on the host API. 174*4882a593SmuzhiyunWith this feature, a memory or register interface can be attached on the 175*4882a593SmuzhiyunFPGA side to the seekable stream. Reading or writing to a certain address in 176*4882a593Smuzhiyunthe attached memory is done by seeking to the desired address, and calling 177*4882a593Smuzhiyunread() or write() as required. 178*4882a593Smuzhiyun 179*4882a593Smuzhiyun 180*4882a593SmuzhiyunInternals 181*4882a593Smuzhiyun========= 182*4882a593Smuzhiyun 183*4882a593SmuzhiyunSource code organization 184*4882a593Smuzhiyun------------------------ 185*4882a593Smuzhiyun 186*4882a593SmuzhiyunThe Xillybus driver consists of a core module, xillybus_core.c, and modules 187*4882a593Smuzhiyunthat depend on the specific bus interface (xillybus_of.c and xillybus_pcie.c). 188*4882a593Smuzhiyun 189*4882a593SmuzhiyunThe bus specific modules are those probed when a suitable device is found by 190*4882a593Smuzhiyunthe kernel. Since the DMA mapping and synchronization functions, which are bus 191*4882a593Smuzhiyundependent by their nature, are used by the core module, a 192*4882a593Smuzhiyunxilly_endpoint_hardware structure is passed to the core module on 193*4882a593Smuzhiyuninitialization. This structure is populated with pointers to wrapper functions 194*4882a593Smuzhiyunwhich execute the DMA-related operations on the bus. 195*4882a593Smuzhiyun 196*4882a593SmuzhiyunPipe attributes 197*4882a593Smuzhiyun--------------- 198*4882a593Smuzhiyun 199*4882a593SmuzhiyunEach pipe has a number of attributes which are set when the FPGA component 200*4882a593Smuzhiyun(IP core) is built. They are fetched from the IDT (the data structure which 201*4882a593Smuzhiyundefines the core's configuration, see Probing below) by xilly_setupchannels() 202*4882a593Smuzhiyunin xillybus_core.c as follows: 203*4882a593Smuzhiyun 204*4882a593Smuzhiyun* is_writebuf: The pipe's direction. A non-zero value means it's an FPGA to 205*4882a593Smuzhiyun host pipe (the FPGA "writes"). 206*4882a593Smuzhiyun 207*4882a593Smuzhiyun* channelnum: The pipe's identification number in communication between the 208*4882a593Smuzhiyun host and FPGA. 209*4882a593Smuzhiyun 210*4882a593Smuzhiyun* format: The underlying data width. See Data Granularity below. 211*4882a593Smuzhiyun 212*4882a593Smuzhiyun* allowpartial: A non-zero value means that a read() or write() (whichever 213*4882a593Smuzhiyun applies) may return with less than the requested number of bytes. The common 214*4882a593Smuzhiyun choice is a non-zero value, to match standard UNIX behavior. 215*4882a593Smuzhiyun 216*4882a593Smuzhiyun* synchronous: A non-zero value means that the pipe is synchronous. See 217*4882a593Smuzhiyun Synchronization above. 218*4882a593Smuzhiyun 219*4882a593Smuzhiyun* bufsize: Each DMA buffer's size. Always a power of two. 220*4882a593Smuzhiyun 221*4882a593Smuzhiyun* bufnum: The number of buffers allocated for this pipe. Always a power of two. 222*4882a593Smuzhiyun 223*4882a593Smuzhiyun* exclusive_open: A non-zero value forces exclusive opening of the associated 224*4882a593Smuzhiyun device file. If the device file is bidirectional, and already opened only in 225*4882a593Smuzhiyun one direction, the opposite direction may be opened once. 226*4882a593Smuzhiyun 227*4882a593Smuzhiyun* seekable: A non-zero value indicates that the pipe is seekable. See 228*4882a593Smuzhiyun Seekable pipes above. 229*4882a593Smuzhiyun 230*4882a593Smuzhiyun* supports_nonempty: A non-zero value (which is typical) indicates that the 231*4882a593Smuzhiyun hardware will send the messages that are necessary to support select() and 232*4882a593Smuzhiyun poll() for this pipe. 233*4882a593Smuzhiyun 234*4882a593SmuzhiyunHost never reads from the FPGA 235*4882a593Smuzhiyun------------------------------ 236*4882a593Smuzhiyun 237*4882a593SmuzhiyunEven though PCI Express is hotpluggable in general, a typical motherboard 238*4882a593Smuzhiyundoesn't expect a card to go away all of the sudden. But since the PCIe card 239*4882a593Smuzhiyunis based upon reprogrammable logic, a sudden disappearance from the bus is 240*4882a593Smuzhiyunquite likely as a result of an accidental reprogramming of the FPGA while the 241*4882a593Smuzhiyunhost is up. In practice, nothing happens immediately in such a situation. But 242*4882a593Smuzhiyunif the host attempts to read from an address that is mapped to the PCI Express 243*4882a593Smuzhiyundevice, that leads to an immediate freeze of the system on some motherboards, 244*4882a593Smuzhiyuneven though the PCIe standard requires a graceful recovery. 245*4882a593Smuzhiyun 246*4882a593SmuzhiyunIn order to avoid these freezes, the Xillybus driver refrains completely from 247*4882a593Smuzhiyunreading from the device's register space. All communication from the FPGA to 248*4882a593Smuzhiyunthe host is done through DMA. In particular, the Interrupt Service Routine 249*4882a593Smuzhiyundoesn't follow the common practice of checking a status register when it's 250*4882a593Smuzhiyuninvoked. Rather, the FPGA prepares a small buffer which contains short 251*4882a593Smuzhiyunmessages, which inform the host what the interrupt was about. 252*4882a593Smuzhiyun 253*4882a593SmuzhiyunThis mechanism is used on non-PCIe buses as well for the sake of uniformity. 254*4882a593Smuzhiyun 255*4882a593Smuzhiyun 256*4882a593SmuzhiyunChannels, pipes, and the message channel 257*4882a593Smuzhiyun---------------------------------------- 258*4882a593Smuzhiyun 259*4882a593SmuzhiyunEach of the (possibly bidirectional) pipes presented to the user is allocated 260*4882a593Smuzhiyuna data channel between the FPGA and the host. The distinction between channels 261*4882a593Smuzhiyunand pipes is necessary only because of channel 0, which is used for interrupt- 262*4882a593Smuzhiyunrelated messages from the FPGA, and has no pipe attached to it. 263*4882a593Smuzhiyun 264*4882a593SmuzhiyunData streaming 265*4882a593Smuzhiyun-------------- 266*4882a593Smuzhiyun 267*4882a593SmuzhiyunEven though a non-segmented data stream is presented to the user at both 268*4882a593Smuzhiyunsides, the implementation relies on a set of DMA buffers which is allocated 269*4882a593Smuzhiyunfor each channel. For the sake of illustration, let's take the FPGA to host 270*4882a593Smuzhiyundirection: As data streams into the respective channel's interface in the 271*4882a593SmuzhiyunFPGA, the Xillybus IP core writes it to one of the DMA buffers. When the 272*4882a593Smuzhiyunbuffer is full, the FPGA informs the host about that (appending a 273*4882a593SmuzhiyunXILLYMSG_OPCODE_RELEASEBUF message channel 0 and sending an interrupt if 274*4882a593Smuzhiyunnecessary). The host responds by making the data available for reading through 275*4882a593Smuzhiyunthe character device. When all data has been read, the host writes on the 276*4882a593SmuzhiyunFPGA's buffer control register, allowing the buffer's overwriting. Flow 277*4882a593Smuzhiyuncontrol mechanisms exist on both sides to prevent underflows and overflows. 278*4882a593Smuzhiyun 279*4882a593SmuzhiyunThis is not good enough for creating a TCP/IP-like stream: If the data flow 280*4882a593Smuzhiyunstops momentarily before a DMA buffer is filled, the intuitive expectation is 281*4882a593Smuzhiyunthat the partial data in buffer will arrive anyhow, despite the buffer not 282*4882a593Smuzhiyunbeing completed. This is implemented by adding a field in the 283*4882a593SmuzhiyunXILLYMSG_OPCODE_RELEASEBUF message, through which the FPGA informs not just 284*4882a593Smuzhiyunwhich buffer is submitted, but how much data it contains. 285*4882a593Smuzhiyun 286*4882a593SmuzhiyunBut the FPGA will submit a partially filled buffer only if directed to do so 287*4882a593Smuzhiyunby the host. This situation occurs when the read() method has been blocking 288*4882a593Smuzhiyunfor XILLY_RX_TIMEOUT jiffies (currently 10 ms), after which the host commands 289*4882a593Smuzhiyunthe FPGA to submit a DMA buffer as soon as it can. This timeout mechanism 290*4882a593Smuzhiyunbalances between bus bandwidth efficiency (preventing a lot of partially 291*4882a593Smuzhiyunfilled buffers being sent) and a latency held fairly low for tails of data. 292*4882a593Smuzhiyun 293*4882a593SmuzhiyunA similar setting is used in the host to FPGA direction. The handling of 294*4882a593Smuzhiyunpartial DMA buffers is somewhat different, though. The user can tell the 295*4882a593Smuzhiyundriver to submit all data it has in the buffers to the FPGA, by issuing a 296*4882a593Smuzhiyunwrite() with the byte count set to zero. This is similar to a flush request, 297*4882a593Smuzhiyunbut it doesn't block. There is also an autoflushing mechanism, which triggers 298*4882a593Smuzhiyunan equivalent flush roughly XILLY_RX_TIMEOUT jiffies after the last write(). 299*4882a593SmuzhiyunThis allows the user to be oblivious about the underlying buffering mechanism 300*4882a593Smuzhiyunand yet enjoy a stream-like interface. 301*4882a593Smuzhiyun 302*4882a593SmuzhiyunNote that the issue of partial buffer flushing is irrelevant for pipes having 303*4882a593Smuzhiyunthe "synchronous" attribute nonzero, since synchronous pipes don't allow data 304*4882a593Smuzhiyunto lay around in the DMA buffers between read() and write() anyhow. 305*4882a593Smuzhiyun 306*4882a593SmuzhiyunData granularity 307*4882a593Smuzhiyun---------------- 308*4882a593Smuzhiyun 309*4882a593SmuzhiyunThe data arrives or is sent at the FPGA as 8, 16 or 32 bit wide words, as 310*4882a593Smuzhiyunconfigured by the "format" attribute. Whenever possible, the driver attempts 311*4882a593Smuzhiyunto hide this when the pipe is accessed differently from its natural alignment. 312*4882a593SmuzhiyunFor example, reading single bytes from a pipe with 32 bit granularity works 313*4882a593Smuzhiyunwith no issues. Writing single bytes to pipes with 16 or 32 bit granularity 314*4882a593Smuzhiyunwill also work, but the driver can't send partially completed words to the 315*4882a593SmuzhiyunFPGA, so the transmission of up to one word may be held until it's fully 316*4882a593Smuzhiyunoccupied with user data. 317*4882a593Smuzhiyun 318*4882a593SmuzhiyunThis somewhat complicates the handling of host to FPGA streams, because 319*4882a593Smuzhiyunwhen a buffer is flushed, it may contain up to 3 bytes don't form a word in 320*4882a593Smuzhiyunthe FPGA, and hence can't be sent. To prevent loss of data, these leftover 321*4882a593Smuzhiyunbytes need to be moved to the next buffer. The parts in xillybus_core.c 322*4882a593Smuzhiyunthat mention "leftovers" in some way are related to this complication. 323*4882a593Smuzhiyun 324*4882a593SmuzhiyunProbing 325*4882a593Smuzhiyun------- 326*4882a593Smuzhiyun 327*4882a593SmuzhiyunAs mentioned earlier, the number of pipes that are created when the driver 328*4882a593Smuzhiyunloads and their attributes depend on the Xillybus IP core in the FPGA. During 329*4882a593Smuzhiyunthe driver's initialization, a blob containing configuration info, the 330*4882a593SmuzhiyunInterface Description Table (IDT), is sent from the FPGA to the host. The 331*4882a593Smuzhiyunbootstrap process is done in three phases: 332*4882a593Smuzhiyun 333*4882a593Smuzhiyun1. Acquire the length of the IDT, so a buffer can be allocated for it. This 334*4882a593Smuzhiyun is done by sending a quiesce command to the device, since the acknowledge 335*4882a593Smuzhiyun for this command contains the IDT's buffer length. 336*4882a593Smuzhiyun 337*4882a593Smuzhiyun2. Acquire the IDT itself. 338*4882a593Smuzhiyun 339*4882a593Smuzhiyun3. Create the interfaces according to the IDT. 340*4882a593Smuzhiyun 341*4882a593SmuzhiyunBuffer allocation 342*4882a593Smuzhiyun----------------- 343*4882a593Smuzhiyun 344*4882a593SmuzhiyunIn order to simplify the logic that prevents illegal boundary crossings of 345*4882a593SmuzhiyunPCIe packets, the following rule applies: If a buffer is smaller than 4kB, 346*4882a593Smuzhiyunit must not cross a 4kB boundary. Otherwise, it must be 4kB aligned. The 347*4882a593Smuzhiyunxilly_setupchannels() functions allocates these buffers by requesting whole 348*4882a593Smuzhiyunpages from the kernel, and diving them into DMA buffers as necessary. Since 349*4882a593Smuzhiyunall buffers' sizes are powers of two, it's possible to pack any set of such 350*4882a593Smuzhiyunbuffers, with a maximal waste of one page of memory. 351*4882a593Smuzhiyun 352*4882a593SmuzhiyunAll buffers are allocated when the driver is loaded. This is necessary, 353*4882a593Smuzhiyunsince large continuous physical memory segments are sometimes requested, 354*4882a593Smuzhiyunwhich are more likely to be available when the system is freshly booted. 355*4882a593Smuzhiyun 356*4882a593SmuzhiyunThe allocation of buffer memory takes place in the same order they appear in 357*4882a593Smuzhiyunthe IDT. The driver relies on a rule that the pipes are sorted with decreasing 358*4882a593Smuzhiyunbuffer size in the IDT. If a requested buffer is larger or equal to a page, 359*4882a593Smuzhiyunthe necessary number of pages is requested from the kernel, and these are 360*4882a593Smuzhiyunused for this buffer. If the requested buffer is smaller than a page, one 361*4882a593Smuzhiyunsingle page is requested from the kernel, and that page is partially used. 362*4882a593SmuzhiyunOr, if there already is a partially used page at hand, the buffer is packed 363*4882a593Smuzhiyuninto that page. It can be shown that all pages requested from the kernel 364*4882a593Smuzhiyun(except possibly for the last) are 100% utilized this way. 365*4882a593Smuzhiyun 366*4882a593SmuzhiyunThe "nonempty" message (supporting poll) 367*4882a593Smuzhiyun---------------------------------------- 368*4882a593Smuzhiyun 369*4882a593SmuzhiyunIn order to support the "poll" method (and hence select() ), there is a small 370*4882a593Smuzhiyuncatch regarding the FPGA to host direction: The FPGA may have filled a DMA 371*4882a593Smuzhiyunbuffer with some data, but not submitted that buffer. If the host waited for 372*4882a593Smuzhiyunthe buffer's submission by the FPGA, there would be a possibility that the 373*4882a593SmuzhiyunFPGA side has sent data, but a select() call would still block, because the 374*4882a593Smuzhiyunhost has not received any notification about this. This is solved with 375*4882a593SmuzhiyunXILLYMSG_OPCODE_NONEMPTY messages sent by the FPGA when a channel goes from 376*4882a593Smuzhiyuncompletely empty to containing some data. 377*4882a593Smuzhiyun 378*4882a593SmuzhiyunThese messages are used only to support poll() and select(). The IP core can 379*4882a593Smuzhiyunbe configured not to send them for a slight reduction of bandwidth. 380