xref: /openbmc/linux/Documentation/driver-api/xillybus.rst (revision baa293e9544bea71361950d071579f0e4d5713ed)
1*baa293e9SMauro Carvalho Chehab==========================================
2*baa293e9SMauro Carvalho ChehabXillybus driver for generic FPGA interface
3*baa293e9SMauro Carvalho Chehab==========================================
4*baa293e9SMauro Carvalho Chehab
5*baa293e9SMauro Carvalho Chehab:Author: Eli Billauer, Xillybus Ltd. (http://xillybus.com)
6*baa293e9SMauro Carvalho Chehab:Email:  eli.billauer@gmail.com or as advertised on Xillybus' site.
7*baa293e9SMauro Carvalho Chehab
8*baa293e9SMauro Carvalho Chehab.. Contents:
9*baa293e9SMauro Carvalho Chehab
10*baa293e9SMauro Carvalho Chehab - Introduction
11*baa293e9SMauro Carvalho Chehab  -- Background
12*baa293e9SMauro Carvalho Chehab  -- Xillybus Overview
13*baa293e9SMauro Carvalho Chehab
14*baa293e9SMauro Carvalho Chehab - Usage
15*baa293e9SMauro Carvalho Chehab  -- User interface
16*baa293e9SMauro Carvalho Chehab  -- Synchronization
17*baa293e9SMauro Carvalho Chehab  -- Seekable pipes
18*baa293e9SMauro Carvalho Chehab
19*baa293e9SMauro Carvalho Chehab - Internals
20*baa293e9SMauro Carvalho Chehab  -- Source code organization
21*baa293e9SMauro Carvalho Chehab  -- Pipe attributes
22*baa293e9SMauro Carvalho Chehab  -- Host never reads from the FPGA
23*baa293e9SMauro Carvalho Chehab  -- Channels, pipes, and the message channel
24*baa293e9SMauro Carvalho Chehab  -- Data streaming
25*baa293e9SMauro Carvalho Chehab  -- Data granularity
26*baa293e9SMauro Carvalho Chehab  -- Probing
27*baa293e9SMauro Carvalho Chehab  -- Buffer allocation
28*baa293e9SMauro Carvalho Chehab  -- The "nonempty" message (supporting poll)
29*baa293e9SMauro Carvalho Chehab
30*baa293e9SMauro Carvalho Chehab
31*baa293e9SMauro Carvalho ChehabIntroduction
32*baa293e9SMauro Carvalho Chehab============
33*baa293e9SMauro Carvalho Chehab
34*baa293e9SMauro Carvalho ChehabBackground
35*baa293e9SMauro Carvalho Chehab----------
36*baa293e9SMauro Carvalho Chehab
37*baa293e9SMauro Carvalho ChehabAn FPGA (Field Programmable Gate Array) is a piece of logic hardware, which
38*baa293e9SMauro Carvalho Chehabcan be programmed to become virtually anything that is usually found as a
39*baa293e9SMauro Carvalho Chehabdedicated chipset: For instance, a display adapter, network interface card,
40*baa293e9SMauro Carvalho Chehabor even a processor with its peripherals. FPGAs are the LEGO of hardware:
41*baa293e9SMauro Carvalho ChehabBased upon certain building blocks, you make your own toys the way you like
42*baa293e9SMauro Carvalho Chehabthem. It's usually pointless to reimplement something that is already
43*baa293e9SMauro Carvalho Chehabavailable on the market as a chipset, so FPGAs are mostly used when some
44*baa293e9SMauro Carvalho Chehabspecial functionality is needed, and the production volume is relatively low
45*baa293e9SMauro Carvalho Chehab(hence not justifying the development of an ASIC).
46*baa293e9SMauro Carvalho Chehab
47*baa293e9SMauro Carvalho ChehabThe challenge with FPGAs is that everything is implemented at a very low
48*baa293e9SMauro Carvalho Chehablevel, even lower than assembly language. In order to allow FPGA designers to
49*baa293e9SMauro Carvalho Chehabfocus on their specific project, and not reinvent the wheel over and over
50*baa293e9SMauro Carvalho Chehabagain, pre-designed building blocks, IP cores, are often used. These are the
51*baa293e9SMauro Carvalho ChehabFPGA parallels of library functions. IP cores may implement certain
52*baa293e9SMauro Carvalho Chehabmathematical functions, a functional unit (e.g. a USB interface), an entire
53*baa293e9SMauro Carvalho Chehabprocessor (e.g. ARM) or anything that might come handy. Think of them as a
54*baa293e9SMauro Carvalho Chehabbuilding block, with electrical wires dangling on the sides for connection to
55*baa293e9SMauro Carvalho Chehabother blocks.
56*baa293e9SMauro Carvalho Chehab
57*baa293e9SMauro Carvalho ChehabOne of the daunting tasks in FPGA design is communicating with a fullblown
58*baa293e9SMauro Carvalho Chehaboperating system (actually, with the processor running it): Implementing the
59*baa293e9SMauro Carvalho Chehablow-level bus protocol and the somewhat higher-level interface with the host
60*baa293e9SMauro Carvalho Chehab(registers, interrupts, DMA etc.) is a project in itself. When the FPGA's
61*baa293e9SMauro Carvalho Chehabfunction is a well-known one (e.g. a video adapter card, or a NIC), it can
62*baa293e9SMauro Carvalho Chehabmake sense to design the FPGA's interface logic specifically for the project.
63*baa293e9SMauro Carvalho ChehabA special driver is then written to present the FPGA as a well-known interface
64*baa293e9SMauro Carvalho Chehabto the kernel and/or user space. In that case, there is no reason to treat the
65*baa293e9SMauro Carvalho ChehabFPGA differently than any device on the bus.
66*baa293e9SMauro Carvalho Chehab
67*baa293e9SMauro Carvalho ChehabIt's however common that the desired data communication doesn't fit any well-
68*baa293e9SMauro Carvalho Chehabknown peripheral function. Also, the effort of designing an elegant
69*baa293e9SMauro Carvalho Chehababstraction for the data exchange is often considered too big. In those cases,
70*baa293e9SMauro Carvalho Chehaba quicker and possibly less elegant solution is sought: The driver is
71*baa293e9SMauro Carvalho Chehabeffectively written as a user space program, leaving the kernel space part
72*baa293e9SMauro Carvalho Chehabwith just elementary data transport. This still requires designing some
73*baa293e9SMauro Carvalho Chehabinterface logic for the FPGA, and write a simple ad-hoc driver for the kernel.
74*baa293e9SMauro Carvalho Chehab
75*baa293e9SMauro Carvalho ChehabXillybus Overview
76*baa293e9SMauro Carvalho Chehab-----------------
77*baa293e9SMauro Carvalho Chehab
78*baa293e9SMauro Carvalho ChehabXillybus is an IP core and a Linux driver. Together, they form a kit for
79*baa293e9SMauro Carvalho Chehabelementary data transport between an FPGA and the host, providing pipe-like
80*baa293e9SMauro Carvalho Chehabdata streams with a straightforward user interface. It's intended as a low-
81*baa293e9SMauro Carvalho Chehabeffort solution for mixed FPGA-host projects, for which it makes sense to
82*baa293e9SMauro Carvalho Chehabhave the project-specific part of the driver running in a user-space program.
83*baa293e9SMauro Carvalho Chehab
84*baa293e9SMauro Carvalho ChehabSince the communication requirements may vary significantly from one FPGA
85*baa293e9SMauro Carvalho Chehabproject to another (the number of data pipes needed in each direction and
86*baa293e9SMauro Carvalho Chehabtheir attributes), there isn't one specific chunk of logic being the Xillybus
87*baa293e9SMauro Carvalho ChehabIP core. Rather, the IP core is configured and built based upon a
88*baa293e9SMauro Carvalho Chehabspecification given by its end user.
89*baa293e9SMauro Carvalho Chehab
90*baa293e9SMauro Carvalho ChehabXillybus presents independent data streams, which resemble pipes or TCP/IP
91*baa293e9SMauro Carvalho Chehabcommunication to the user. At the host side, a character device file is used
92*baa293e9SMauro Carvalho Chehabjust like any pipe file. On the FPGA side, hardware FIFOs are used to stream
93*baa293e9SMauro Carvalho Chehabthe data. This is contrary to a common method of communicating through fixed-
94*baa293e9SMauro Carvalho Chehabsized buffers (even though such buffers are used by Xillybus under the hood).
95*baa293e9SMauro Carvalho ChehabThere may be more than a hundred of these streams on a single IP core, but
96*baa293e9SMauro Carvalho Chehabalso no more than one, depending on the configuration.
97*baa293e9SMauro Carvalho Chehab
98*baa293e9SMauro Carvalho ChehabIn order to ease the deployment of the Xillybus IP core, it contains a simple
99*baa293e9SMauro Carvalho Chehabdata structure which completely defines the core's configuration. The Linux
100*baa293e9SMauro Carvalho Chehabdriver fetches this data structure during its initialization process, and sets
101*baa293e9SMauro Carvalho Chehabup the DMA buffers and character devices accordingly. As a result, a single
102*baa293e9SMauro Carvalho Chehabdriver is used to work out of the box with any Xillybus IP core.
103*baa293e9SMauro Carvalho Chehab
104*baa293e9SMauro Carvalho ChehabThe data structure just mentioned should not be confused with PCI's
105*baa293e9SMauro Carvalho Chehabconfiguration space or the Flattened Device Tree.
106*baa293e9SMauro Carvalho Chehab
107*baa293e9SMauro Carvalho ChehabUsage
108*baa293e9SMauro Carvalho Chehab=====
109*baa293e9SMauro Carvalho Chehab
110*baa293e9SMauro Carvalho ChehabUser interface
111*baa293e9SMauro Carvalho Chehab--------------
112*baa293e9SMauro Carvalho Chehab
113*baa293e9SMauro Carvalho ChehabOn the host, all interface with Xillybus is done through /dev/xillybus_*
114*baa293e9SMauro Carvalho Chehabdevice files, which are generated automatically as the drivers loads. The
115*baa293e9SMauro Carvalho Chehabnames of these files depend on the IP core that is loaded in the FPGA (see
116*baa293e9SMauro Carvalho ChehabProbing below). To communicate with the FPGA, open the device file that
117*baa293e9SMauro Carvalho Chehabcorresponds to the hardware FIFO you want to send data or receive data from,
118*baa293e9SMauro Carvalho Chehaband use plain write() or read() calls, just like with a regular pipe. In
119*baa293e9SMauro Carvalho Chehabparticular, it makes perfect sense to go::
120*baa293e9SMauro Carvalho Chehab
121*baa293e9SMauro Carvalho Chehab	$ cat mydata > /dev/xillybus_thisfifo
122*baa293e9SMauro Carvalho Chehab
123*baa293e9SMauro Carvalho Chehab	$ cat /dev/xillybus_thatfifo > hisdata
124*baa293e9SMauro Carvalho Chehab
125*baa293e9SMauro Carvalho Chehabpossibly pressing CTRL-C as some stage, even though the xillybus_* pipes have
126*baa293e9SMauro Carvalho Chehabthe capability to send an EOF (but may not use it).
127*baa293e9SMauro Carvalho Chehab
128*baa293e9SMauro Carvalho ChehabThe driver and hardware are designed to behave sensibly as pipes, including:
129*baa293e9SMauro Carvalho Chehab
130*baa293e9SMauro Carvalho Chehab* Supporting non-blocking I/O (by setting O_NONBLOCK on open() ).
131*baa293e9SMauro Carvalho Chehab
132*baa293e9SMauro Carvalho Chehab* Supporting poll() and select().
133*baa293e9SMauro Carvalho Chehab
134*baa293e9SMauro Carvalho Chehab* Being bandwidth efficient under load (using DMA) but also handle small
135*baa293e9SMauro Carvalho Chehab  pieces of data sent across (like TCP/IP) by autoflushing.
136*baa293e9SMauro Carvalho Chehab
137*baa293e9SMauro Carvalho ChehabA device file can be read only, write only or bidirectional. Bidirectional
138*baa293e9SMauro Carvalho Chehabdevice files are treated like two independent pipes (except for sharing a
139*baa293e9SMauro Carvalho Chehab"channel" structure in the implementation code).
140*baa293e9SMauro Carvalho Chehab
141*baa293e9SMauro Carvalho ChehabSynchronization
142*baa293e9SMauro Carvalho Chehab---------------
143*baa293e9SMauro Carvalho Chehab
144*baa293e9SMauro Carvalho ChehabXillybus pipes are configured (on the IP core) to be either synchronous or
145*baa293e9SMauro Carvalho Chehabasynchronous. For a synchronous pipe, write() returns successfully only after
146*baa293e9SMauro Carvalho Chehabsome data has been submitted and acknowledged by the FPGA. This slows down
147*baa293e9SMauro Carvalho Chehabbulk data transfers, and is nearly impossible for use with streams that
148*baa293e9SMauro Carvalho Chehabrequire data at a constant rate: There is no data transmitted to the FPGA
149*baa293e9SMauro Carvalho Chehabbetween write() calls, in particular when the process loses the CPU.
150*baa293e9SMauro Carvalho Chehab
151*baa293e9SMauro Carvalho ChehabWhen a pipe is configured asynchronous, write() returns if there was enough
152*baa293e9SMauro Carvalho Chehabroom in the buffers to store any of the data in the buffers.
153*baa293e9SMauro Carvalho Chehab
154*baa293e9SMauro Carvalho ChehabFor FPGA to host pipes, asynchronous pipes allow data transfer from the FPGA
155*baa293e9SMauro Carvalho Chehabas soon as the respective device file is opened, regardless of if the data
156*baa293e9SMauro Carvalho Chehabhas been requested by a read() call. On synchronous pipes, only the amount
157*baa293e9SMauro Carvalho Chehabof data requested by a read() call is transmitted.
158*baa293e9SMauro Carvalho Chehab
159*baa293e9SMauro Carvalho ChehabIn summary, for synchronous pipes, data between the host and FPGA is
160*baa293e9SMauro Carvalho Chehabtransmitted only to satisfy the read() or write() call currently handled
161*baa293e9SMauro Carvalho Chehabby the driver, and those calls wait for the transmission to complete before
162*baa293e9SMauro Carvalho Chehabreturning.
163*baa293e9SMauro Carvalho Chehab
164*baa293e9SMauro Carvalho ChehabNote that the synchronization attribute has nothing to do with the possibility
165*baa293e9SMauro Carvalho Chehabthat read() or write() completes less bytes than requested. There is a
166*baa293e9SMauro Carvalho Chehabseparate configuration flag ("allowpartial") that determines whether such a
167*baa293e9SMauro Carvalho Chehabpartial completion is allowed.
168*baa293e9SMauro Carvalho Chehab
169*baa293e9SMauro Carvalho ChehabSeekable pipes
170*baa293e9SMauro Carvalho Chehab--------------
171*baa293e9SMauro Carvalho Chehab
172*baa293e9SMauro Carvalho ChehabA synchronous pipe can be configured to have the stream's position exposed
173*baa293e9SMauro Carvalho Chehabto the user logic at the FPGA. Such a pipe is also seekable on the host API.
174*baa293e9SMauro Carvalho ChehabWith this feature, a memory or register interface can be attached on the
175*baa293e9SMauro Carvalho ChehabFPGA side to the seekable stream. Reading or writing to a certain address in
176*baa293e9SMauro Carvalho Chehabthe attached memory is done by seeking to the desired address, and calling
177*baa293e9SMauro Carvalho Chehabread() or write() as required.
178*baa293e9SMauro Carvalho Chehab
179*baa293e9SMauro Carvalho Chehab
180*baa293e9SMauro Carvalho ChehabInternals
181*baa293e9SMauro Carvalho Chehab=========
182*baa293e9SMauro Carvalho Chehab
183*baa293e9SMauro Carvalho ChehabSource code organization
184*baa293e9SMauro Carvalho Chehab------------------------
185*baa293e9SMauro Carvalho Chehab
186*baa293e9SMauro Carvalho ChehabThe Xillybus driver consists of a core module, xillybus_core.c, and modules
187*baa293e9SMauro Carvalho Chehabthat depend on the specific bus interface (xillybus_of.c and xillybus_pcie.c).
188*baa293e9SMauro Carvalho Chehab
189*baa293e9SMauro Carvalho ChehabThe bus specific modules are those probed when a suitable device is found by
190*baa293e9SMauro Carvalho Chehabthe kernel. Since the DMA mapping and synchronization functions, which are bus
191*baa293e9SMauro Carvalho Chehabdependent by their nature, are used by the core module, a
192*baa293e9SMauro Carvalho Chehabxilly_endpoint_hardware structure is passed to the core module on
193*baa293e9SMauro Carvalho Chehabinitialization. This structure is populated with pointers to wrapper functions
194*baa293e9SMauro Carvalho Chehabwhich execute the DMA-related operations on the bus.
195*baa293e9SMauro Carvalho Chehab
196*baa293e9SMauro Carvalho ChehabPipe attributes
197*baa293e9SMauro Carvalho Chehab---------------
198*baa293e9SMauro Carvalho Chehab
199*baa293e9SMauro Carvalho ChehabEach pipe has a number of attributes which are set when the FPGA component
200*baa293e9SMauro Carvalho Chehab(IP core) is built. They are fetched from the IDT (the data structure which
201*baa293e9SMauro Carvalho Chehabdefines the core's configuration, see Probing below) by xilly_setupchannels()
202*baa293e9SMauro Carvalho Chehabin xillybus_core.c as follows:
203*baa293e9SMauro Carvalho Chehab
204*baa293e9SMauro Carvalho Chehab* is_writebuf: The pipe's direction. A non-zero value means it's an FPGA to
205*baa293e9SMauro Carvalho Chehab  host pipe (the FPGA "writes").
206*baa293e9SMauro Carvalho Chehab
207*baa293e9SMauro Carvalho Chehab* channelnum: The pipe's identification number in communication between the
208*baa293e9SMauro Carvalho Chehab  host and FPGA.
209*baa293e9SMauro Carvalho Chehab
210*baa293e9SMauro Carvalho Chehab* format: The underlying data width. See Data Granularity below.
211*baa293e9SMauro Carvalho Chehab
212*baa293e9SMauro Carvalho Chehab* allowpartial: A non-zero value means that a read() or write() (whichever
213*baa293e9SMauro Carvalho Chehab  applies) may return with less than the requested number of bytes. The common
214*baa293e9SMauro Carvalho Chehab  choice is a non-zero value, to match standard UNIX behavior.
215*baa293e9SMauro Carvalho Chehab
216*baa293e9SMauro Carvalho Chehab* synchronous: A non-zero value means that the pipe is synchronous. See
217*baa293e9SMauro Carvalho Chehab  Synchronization above.
218*baa293e9SMauro Carvalho Chehab
219*baa293e9SMauro Carvalho Chehab* bufsize: Each DMA buffer's size. Always a power of two.
220*baa293e9SMauro Carvalho Chehab
221*baa293e9SMauro Carvalho Chehab* bufnum: The number of buffers allocated for this pipe. Always a power of two.
222*baa293e9SMauro Carvalho Chehab
223*baa293e9SMauro Carvalho Chehab* exclusive_open: A non-zero value forces exclusive opening of the associated
224*baa293e9SMauro Carvalho Chehab  device file. If the device file is bidirectional, and already opened only in
225*baa293e9SMauro Carvalho Chehab  one direction, the opposite direction may be opened once.
226*baa293e9SMauro Carvalho Chehab
227*baa293e9SMauro Carvalho Chehab* seekable: A non-zero value indicates that the pipe is seekable. See
228*baa293e9SMauro Carvalho Chehab  Seekable pipes above.
229*baa293e9SMauro Carvalho Chehab
230*baa293e9SMauro Carvalho Chehab* supports_nonempty: A non-zero value (which is typical) indicates that the
231*baa293e9SMauro Carvalho Chehab  hardware will send the messages that are necessary to support select() and
232*baa293e9SMauro Carvalho Chehab  poll() for this pipe.
233*baa293e9SMauro Carvalho Chehab
234*baa293e9SMauro Carvalho ChehabHost never reads from the FPGA
235*baa293e9SMauro Carvalho Chehab------------------------------
236*baa293e9SMauro Carvalho Chehab
237*baa293e9SMauro Carvalho ChehabEven though PCI Express is hotpluggable in general, a typical motherboard
238*baa293e9SMauro Carvalho Chehabdoesn't expect a card to go away all of the sudden. But since the PCIe card
239*baa293e9SMauro Carvalho Chehabis based upon reprogrammable logic, a sudden disappearance from the bus is
240*baa293e9SMauro Carvalho Chehabquite likely as a result of an accidental reprogramming of the FPGA while the
241*baa293e9SMauro Carvalho Chehabhost is up. In practice, nothing happens immediately in such a situation. But
242*baa293e9SMauro Carvalho Chehabif the host attempts to read from an address that is mapped to the PCI Express
243*baa293e9SMauro Carvalho Chehabdevice, that leads to an immediate freeze of the system on some motherboards,
244*baa293e9SMauro Carvalho Chehabeven though the PCIe standard requires a graceful recovery.
245*baa293e9SMauro Carvalho Chehab
246*baa293e9SMauro Carvalho ChehabIn order to avoid these freezes, the Xillybus driver refrains completely from
247*baa293e9SMauro Carvalho Chehabreading from the device's register space. All communication from the FPGA to
248*baa293e9SMauro Carvalho Chehabthe host is done through DMA. In particular, the Interrupt Service Routine
249*baa293e9SMauro Carvalho Chehabdoesn't follow the common practice of checking a status register when it's
250*baa293e9SMauro Carvalho Chehabinvoked. Rather, the FPGA prepares a small buffer which contains short
251*baa293e9SMauro Carvalho Chehabmessages, which inform the host what the interrupt was about.
252*baa293e9SMauro Carvalho Chehab
253*baa293e9SMauro Carvalho ChehabThis mechanism is used on non-PCIe buses as well for the sake of uniformity.
254*baa293e9SMauro Carvalho Chehab
255*baa293e9SMauro Carvalho Chehab
256*baa293e9SMauro Carvalho ChehabChannels, pipes, and the message channel
257*baa293e9SMauro Carvalho Chehab----------------------------------------
258*baa293e9SMauro Carvalho Chehab
259*baa293e9SMauro Carvalho ChehabEach of the (possibly bidirectional) pipes presented to the user is allocated
260*baa293e9SMauro Carvalho Chehaba data channel between the FPGA and the host. The distinction between channels
261*baa293e9SMauro Carvalho Chehaband pipes is necessary only because of channel 0, which is used for interrupt-
262*baa293e9SMauro Carvalho Chehabrelated messages from the FPGA, and has no pipe attached to it.
263*baa293e9SMauro Carvalho Chehab
264*baa293e9SMauro Carvalho ChehabData streaming
265*baa293e9SMauro Carvalho Chehab--------------
266*baa293e9SMauro Carvalho Chehab
267*baa293e9SMauro Carvalho ChehabEven though a non-segmented data stream is presented to the user at both
268*baa293e9SMauro Carvalho Chehabsides, the implementation relies on a set of DMA buffers which is allocated
269*baa293e9SMauro Carvalho Chehabfor each channel. For the sake of illustration, let's take the FPGA to host
270*baa293e9SMauro Carvalho Chehabdirection: As data streams into the respective channel's interface in the
271*baa293e9SMauro Carvalho ChehabFPGA, the Xillybus IP core writes it to one of the DMA buffers. When the
272*baa293e9SMauro Carvalho Chehabbuffer is full, the FPGA informs the host about that (appending a
273*baa293e9SMauro Carvalho ChehabXILLYMSG_OPCODE_RELEASEBUF message channel 0 and sending an interrupt if
274*baa293e9SMauro Carvalho Chehabnecessary). The host responds by making the data available for reading through
275*baa293e9SMauro Carvalho Chehabthe character device. When all data has been read, the host writes on the
276*baa293e9SMauro Carvalho Chehabthe FPGA's buffer control register, allowing the buffer's overwriting. Flow
277*baa293e9SMauro Carvalho Chehabcontrol mechanisms exist on both sides to prevent underflows and overflows.
278*baa293e9SMauro Carvalho Chehab
279*baa293e9SMauro Carvalho ChehabThis is not good enough for creating a TCP/IP-like stream: If the data flow
280*baa293e9SMauro Carvalho Chehabstops momentarily before a DMA buffer is filled, the intuitive expectation is
281*baa293e9SMauro Carvalho Chehabthat the partial data in buffer will arrive anyhow, despite the buffer not
282*baa293e9SMauro Carvalho Chehabbeing completed. This is implemented by adding a field in the
283*baa293e9SMauro Carvalho ChehabXILLYMSG_OPCODE_RELEASEBUF message, through which the FPGA informs not just
284*baa293e9SMauro Carvalho Chehabwhich buffer is submitted, but how much data it contains.
285*baa293e9SMauro Carvalho Chehab
286*baa293e9SMauro Carvalho ChehabBut the FPGA will submit a partially filled buffer only if directed to do so
287*baa293e9SMauro Carvalho Chehabby the host. This situation occurs when the read() method has been blocking
288*baa293e9SMauro Carvalho Chehabfor XILLY_RX_TIMEOUT jiffies (currently 10 ms), after which the host commands
289*baa293e9SMauro Carvalho Chehabthe FPGA to submit a DMA buffer as soon as it can. This timeout mechanism
290*baa293e9SMauro Carvalho Chehabbalances between bus bandwidth efficiency (preventing a lot of partially
291*baa293e9SMauro Carvalho Chehabfilled buffers being sent) and a latency held fairly low for tails of data.
292*baa293e9SMauro Carvalho Chehab
293*baa293e9SMauro Carvalho ChehabA similar setting is used in the host to FPGA direction. The handling of
294*baa293e9SMauro Carvalho Chehabpartial DMA buffers is somewhat different, though. The user can tell the
295*baa293e9SMauro Carvalho Chehabdriver to submit all data it has in the buffers to the FPGA, by issuing a
296*baa293e9SMauro Carvalho Chehabwrite() with the byte count set to zero. This is similar to a flush request,
297*baa293e9SMauro Carvalho Chehabbut it doesn't block. There is also an autoflushing mechanism, which triggers
298*baa293e9SMauro Carvalho Chehaban equivalent flush roughly XILLY_RX_TIMEOUT jiffies after the last write().
299*baa293e9SMauro Carvalho ChehabThis allows the user to be oblivious about the underlying buffering mechanism
300*baa293e9SMauro Carvalho Chehaband yet enjoy a stream-like interface.
301*baa293e9SMauro Carvalho Chehab
302*baa293e9SMauro Carvalho ChehabNote that the issue of partial buffer flushing is irrelevant for pipes having
303*baa293e9SMauro Carvalho Chehabthe "synchronous" attribute nonzero, since synchronous pipes don't allow data
304*baa293e9SMauro Carvalho Chehabto lay around in the DMA buffers between read() and write() anyhow.
305*baa293e9SMauro Carvalho Chehab
306*baa293e9SMauro Carvalho ChehabData granularity
307*baa293e9SMauro Carvalho Chehab----------------
308*baa293e9SMauro Carvalho Chehab
309*baa293e9SMauro Carvalho ChehabThe data arrives or is sent at the FPGA as 8, 16 or 32 bit wide words, as
310*baa293e9SMauro Carvalho Chehabconfigured by the "format" attribute. Whenever possible, the driver attempts
311*baa293e9SMauro Carvalho Chehabto hide this when the pipe is accessed differently from its natural alignment.
312*baa293e9SMauro Carvalho ChehabFor example, reading single bytes from a pipe with 32 bit granularity works
313*baa293e9SMauro Carvalho Chehabwith no issues. Writing single bytes to pipes with 16 or 32 bit granularity
314*baa293e9SMauro Carvalho Chehabwill also work, but the driver can't send partially completed words to the
315*baa293e9SMauro Carvalho ChehabFPGA, so the transmission of up to one word may be held until it's fully
316*baa293e9SMauro Carvalho Chehaboccupied with user data.
317*baa293e9SMauro Carvalho Chehab
318*baa293e9SMauro Carvalho ChehabThis somewhat complicates the handling of host to FPGA streams, because
319*baa293e9SMauro Carvalho Chehabwhen a buffer is flushed, it may contain up to 3 bytes don't form a word in
320*baa293e9SMauro Carvalho Chehabthe FPGA, and hence can't be sent. To prevent loss of data, these leftover
321*baa293e9SMauro Carvalho Chehabbytes need to be moved to the next buffer. The parts in xillybus_core.c
322*baa293e9SMauro Carvalho Chehabthat mention "leftovers" in some way are related to this complication.
323*baa293e9SMauro Carvalho Chehab
324*baa293e9SMauro Carvalho ChehabProbing
325*baa293e9SMauro Carvalho Chehab-------
326*baa293e9SMauro Carvalho Chehab
327*baa293e9SMauro Carvalho ChehabAs mentioned earlier, the number of pipes that are created when the driver
328*baa293e9SMauro Carvalho Chehabloads and their attributes depend on the Xillybus IP core in the FPGA. During
329*baa293e9SMauro Carvalho Chehabthe driver's initialization, a blob containing configuration info, the
330*baa293e9SMauro Carvalho ChehabInterface Description Table (IDT), is sent from the FPGA to the host. The
331*baa293e9SMauro Carvalho Chehabbootstrap process is done in three phases:
332*baa293e9SMauro Carvalho Chehab
333*baa293e9SMauro Carvalho Chehab1. Acquire the length of the IDT, so a buffer can be allocated for it. This
334*baa293e9SMauro Carvalho Chehab   is done by sending a quiesce command to the device, since the acknowledge
335*baa293e9SMauro Carvalho Chehab   for this command contains the IDT's buffer length.
336*baa293e9SMauro Carvalho Chehab
337*baa293e9SMauro Carvalho Chehab2. Acquire the IDT itself.
338*baa293e9SMauro Carvalho Chehab
339*baa293e9SMauro Carvalho Chehab3. Create the interfaces according to the IDT.
340*baa293e9SMauro Carvalho Chehab
341*baa293e9SMauro Carvalho ChehabBuffer allocation
342*baa293e9SMauro Carvalho Chehab-----------------
343*baa293e9SMauro Carvalho Chehab
344*baa293e9SMauro Carvalho ChehabIn order to simplify the logic that prevents illegal boundary crossings of
345*baa293e9SMauro Carvalho ChehabPCIe packets, the following rule applies: If a buffer is smaller than 4kB,
346*baa293e9SMauro Carvalho Chehabit must not cross a 4kB boundary. Otherwise, it must be 4kB aligned. The
347*baa293e9SMauro Carvalho Chehabxilly_setupchannels() functions allocates these buffers by requesting whole
348*baa293e9SMauro Carvalho Chehabpages from the kernel, and diving them into DMA buffers as necessary. Since
349*baa293e9SMauro Carvalho Chehaball buffers' sizes are powers of two, it's possible to pack any set of such
350*baa293e9SMauro Carvalho Chehabbuffers, with a maximal waste of one page of memory.
351*baa293e9SMauro Carvalho Chehab
352*baa293e9SMauro Carvalho ChehabAll buffers are allocated when the driver is loaded. This is necessary,
353*baa293e9SMauro Carvalho Chehabsince large continuous physical memory segments are sometimes requested,
354*baa293e9SMauro Carvalho Chehabwhich are more likely to be available when the system is freshly booted.
355*baa293e9SMauro Carvalho Chehab
356*baa293e9SMauro Carvalho ChehabThe allocation of buffer memory takes place in the same order they appear in
357*baa293e9SMauro Carvalho Chehabthe IDT. The driver relies on a rule that the pipes are sorted with decreasing
358*baa293e9SMauro Carvalho Chehabbuffer size in the IDT. If a requested buffer is larger or equal to a page,
359*baa293e9SMauro Carvalho Chehabthe necessary number of pages is requested from the kernel, and these are
360*baa293e9SMauro Carvalho Chehabused for this buffer. If the requested buffer is smaller than a page, one
361*baa293e9SMauro Carvalho Chehabsingle page is requested from the kernel, and that page is partially used.
362*baa293e9SMauro Carvalho ChehabOr, if there already is a partially used page at hand, the buffer is packed
363*baa293e9SMauro Carvalho Chehabinto that page. It can be shown that all pages requested from the kernel
364*baa293e9SMauro Carvalho Chehab(except possibly for the last) are 100% utilized this way.
365*baa293e9SMauro Carvalho Chehab
366*baa293e9SMauro Carvalho ChehabThe "nonempty" message (supporting poll)
367*baa293e9SMauro Carvalho Chehab----------------------------------------
368*baa293e9SMauro Carvalho Chehab
369*baa293e9SMauro Carvalho ChehabIn order to support the "poll" method (and hence select() ), there is a small
370*baa293e9SMauro Carvalho Chehabcatch regarding the FPGA to host direction: The FPGA may have filled a DMA
371*baa293e9SMauro Carvalho Chehabbuffer with some data, but not submitted that buffer. If the host waited for
372*baa293e9SMauro Carvalho Chehabthe buffer's submission by the FPGA, there would be a possibility that the
373*baa293e9SMauro Carvalho ChehabFPGA side has sent data, but a select() call would still block, because the
374*baa293e9SMauro Carvalho Chehabhost has not received any notification about this. This is solved with
375*baa293e9SMauro Carvalho ChehabXILLYMSG_OPCODE_NONEMPTY messages sent by the FPGA when a channel goes from
376*baa293e9SMauro Carvalho Chehabcompletely empty to containing some data.
377*baa293e9SMauro Carvalho Chehab
378*baa293e9SMauro Carvalho ChehabThese messages are used only to support poll() and select(). The IP core can
379*baa293e9SMauro Carvalho Chehabbe configured not to send them for a slight reduction of bandwidth.
380