2
votes

I've got a Xilinx Zynq 7000-based board with a peripheral in the FPGA fabric that has DMA capability (on an AXI bus). We've developed a circuit and are running Linux on the ARM cores. We're having performance problems accessing a DMA buffer from user space after it's been filled by hardware.

Summary:

We have pre-reserved at boot time a section of DRAM for use as a large DMA buffer. We're apparently using the wrong APIs to map this buffer, because it appears to be uncached, and the access speed is terrible.

Using it even as a bounce-buffer is untenably slow due to horrible performance. IIUC, ARM caches are not DMA coherent, so I would really appreciate some insight on how to do the following:

  1. Map a region of DRAM into the kernel virtual address space but ensure that it is cacheable.
  2. Ensure that mapping it into userspace doesn't also have an undesirable effect, even if that requires we provide an mmap call by our own driver.
  3. Explicitly invalidate a region of physical memory from the cache hierarchy before doing a DMA, to ensure coherency.

More info:

I've been trying to research this thoroughly before asking. Unfortunately, this being an ARM SoC/FPGA, there's very little information available on this, so I have to ask the experts directly.

Since this is an SoC, a lot of stuff is hard-coded for u-boot. For instance, the kernel and a ramdisk are loaded to specific places in DRAM before handing control over to the kernel. We've taken advantage of this to reserve a 64MB section of DRAM for a DMA buffer (it does need to be that big, which is why we pre-reserve it). There isn't any worry about conflicting memory types or the kernel stomping on this memory, because the boot parameters tell the kernel what region of DRAM it has control over.

Initially, we tried to map this physical address range into kernel space using ioremap, but that appears to mark the region uncacheable, and the access speed is horrible, even if we try to use memcpy to make it a bounce buffer. We use /dev/mem to map this also into userspace, and I've timed memcpy as being around 70MB/sec.

Based on a fair amount of searching on this topic, it appears that although half the people out there want to use ioremap like this (which is probably where we got the idea from), ioremap is not supposed to be used for this purpose and that there are DMA-related APIs that should be used instead. Unfortunately, it appears that DMA buffer allocation is totally dynamic, and I haven't figured out how to tell it, "here's a physical address already allocated -- use that."

One document I looked at is this one, but it's way too x86 and PC-centric: https://www.kernel.org/doc/Documentation/DMA-API-HOWTO.txt

And this question also comes up at the top of my searches, but there's no real answer: get the physical address of a buffer under Linux

Looking at the standard calls, dma_set_mask_and_coherent and family won't take a pre-defined address and wants a device structure for PCI. I don't have such a structure, because this is an ARM SoC without PCI. I could manually populate such a structure, but that smells to me like abusing the API, not using it as intended.

BTW: This is a ring buffer, where we DMA data blocks into different offsets, but we align to cache line boundaries, so there is no risk of false sharing.

Thank you a million for any help you can provide!

UPDATE: It appears that there's no such thing as a cacheable DMA buffer on ARM if you do it the normal way. Maybe if I don't make the ioremap call, the region won't be marked as uncacheable, but then I have to figure out how to do cache management on ARM, which I can't figure out. One of the problems is that memcpy in userspace appears to really suck. Is there a memcpy implementation that's optimized for uncached memory I can use? Maybe I could write one. I have to figure out if this processor has Neon.

3
Which version of the Linux kernel are you using? Have you tried using the Contiguous Memory Allocator?...TheCodeArtist
@TheCodeArtist "Linux (none) 3.17.0-xilinx-dirty #13 SMP PREEMPT Mon Aug 3 12:10:57 MDT 2015 armv7l GNU/Linux" Also, I haven't looked at CMA. If I can map this buffer properly, it shouldn't require CMA, unless it's totally impossible to specify a DMA buffer from a pre-reserved physical address.Timothy Miller
For getting physical address of a buffer under linux (this is from user space) stackoverflow.com/a/28987409/1163019. For DMA/uncached stuff afaik, and honestly I don't remember all the details and I was unsuccessful for my different task but what I can see from the code is I was trying "dma_alloc_coherent" -> "update vma->vm_page_prot as writethrough" -> dma_mmap_from_coherent -> remap_pfn_range. Also may be this presentation help? events.linuxfoundation.org/sites/events/files/slides/…auselen
@auselen I don't need to get the physical address. I know that at boot time. And I can map it into userspace using /dev/mem. However, if I use ioremap in the kernel, it makes the space uncached, which kills access speed. "dma_alloc_coherent" will allocate only relatively small sections dynamically, whereas we have a large contiguous pre-allocated buffer already that we need to DMA into. I'll check the presentation. Thanks.Timothy Miller
Is your kernel new enough to have memremap()? That's probably the most appropriate tool for the job - the other things being discussed here are about allocating DMA buffers from within kernel memory. ioremap() is semantically close to the right approach, i.e. mapping some external area into the kernel address space, but will result in mapping it as device memory, not even normal non-cacheable.Notlikethat

3 Answers

1
votes

Have you tried implementing your own char device with an mmap() method remapping your buffer as cacheable (by means of remap_pfn_range())?

0
votes

I believe you need a driver that implements mmap() if you want the mapping to be cached.

We use two device drivers for this: portalmem and zynqportal. In the Connectal Project, we call the connection between user space software and FPGA logic a "portal". These drivers require dma-buf, which has been stable for us since Linux kernel version 3.8.x.

The portalmem driver provides an ioctl to allocate a reference-counted chunk of memory and returns a file descriptor associated with that memory. This driver implements dma-buf sharing. It also implements mmap() so that user-space applications can access the memory.

At allocation time, the application may choose cached or uncached mapping of the memory. On x86, the mapping is always cached. Our implementation of mmap() currently starts at line 173 of the portalmem driver. If the mapping is uncached, it modifies vma->vm_page_prot using pgprot_writecombine(), enabling buffering of writes but disabling caching.

The portalmem driver also provides an ioctl to invalidate and optionally write back data cache lines.

The portalmem driver has no knowledge of the FPGA. For that, we the zynqportal driver, which provides an ioctl for transferring a translation table to the FPGA so that we can use logically contiguous addresses on the FPGA and translate them to the actual DMA addresses. The allocation scheme used by portalmem is designed to produce compact translation tables.

We use the same portalmem driver with pcieportal for PCI Express attached FPGAs, with no change to the user software.

0
votes

The Zynq has neon instructions, and an assembly code implementation of memcpy using neon instructions, using aligned on cache boundary (32 bytes) will achieve 300 MB/s rates or higher.