A tale of working with Xilinx DisplayPort & UIO

Posted on August 30, 2019
Tags: linux, drivers, kernel, uio, interrupts, xilinx
by Sanchayan Maity

At work, we use a DisplayPort IP from Xilinx. Xilinx does not provide any driver for this. There is a TX and RX. Baremetal code support is provided, however, we needed support for Linux. Ignoring interrupts, it is easy to get this baremetal code to work on Linux. Xilinx’s baremetal code at it’s core uses Xil_Out32 and Xil_In32 function for writing and reading to registers. The implementations for these can be replaced with mmap for accessing the registers. For DP TX side, we do not need to handle interrupts and setting up the registers is enough. For RX, however, we need interrupts to setup RX. For example, the link training for DP is initiated once a Training Pattern 1 (TP1) interrupt is detected.

Linux being a monolithic kernel, there is clear separation between kernel and user space. Interrupts can only be handled in kernel space. However, it was easier to use the ported baremetal code in user space and so we also needed to handle interrupts in user space. Generally, one writes a driver to do all this but since we only needed the interrupt part to be handled in kernel space, this is where UIO subsystem comes in.

Using the UIO subsystem, it is possible to handle the interrupts in kernel space while the rest like reading or writing to the registers can be done in user space. There is a Userspace I/O Platform driver with generic IRQ handling code. I will take the example of Xilinx DisplayPort RX here. After my colleague who works on FPGA side generates the FPGA firmware along with device trees which has entries as per the peripherals configured on FPGA side, DP RX peripheral let’s say is in the memory region 0x80004000 to 0x80006000. An interrupt will also be assinged based on how the FPGA Programmable Logic (PL) connects to the Processing System (PS). PL is the FPGA and PS is the ARM64 SoC.

The extended device tree entry looks like this. reg specifies the memory that can be mmaped in user space and accessed while the interrupts will be used by the kernel code.

To link the UIO platform driver to this, we add the following to the bootargs environment variable in u-boot.

We need to do this, since the compatible property for device tree is not specified in the driver. See here. So it is a module parameter.

Now, a combination of poll and read can be used to wait for interrupts in user space.

So all well and good, however, there are some caveats to know. Once an interrupt is handled in kernel code, it disables the interrupt.

The interrupt re-enable logic is in the below function.

This is called from uio_write. And if you know how file operations work, this uio_write will be called when a write system call is issued with a file descriptor received from opening the /dev/uioX node.

Now, here comes the problem. For me, after the very first interrupt I was not getting any more interrupts. So, basically my write call was not working to re-enable the interrupt. Putting print statements in uio_write, I could see a call to the write function did not result in invocation of uio_write.

I was perplexed and wasted 4-5 hours trying to figure out what’s wrong. I wrote a small piece of code outside the project workspace which opened /dev/uioX and then did a write. In this case, I could see my prints from uio_write function which eventually called uio_pdrv_genirq_irqcontrol to enable the interrupt. So, something was wrong with my project.

I use neovim and ctags for code navigation. Trying jump to definition on my write call made me end up in a write.c file. The very first initial project setup was done by my FPGA engineer colleague since Xilinx SDK generates bare metal code samples based on the design done. I had not noticed this file before. It seemed to be an artifact of the code ported over from baremetal and had a write function as below.

I was aware of the weak attribute from my work in u-boot where it is used to define board specific functions over riding default ones. The GCC manual defines it as “The weak attribute causes the declaration to be emitted as a weak symbol rather than a global. This is primarily useful in defining library functions which can be overridden in user code”.

There was no other write function defined in my project. Ideally it should have been picked up from glibc. However, this was not what was happening. I did not need that write implementation in write.c which was actually writing to a UART port, so after removal everything started working fine. DP link training was succeeding finally.

You can read more about UIO here.