Skip to contentSkip to conversation

UIO and memory from dma_alloc_coherent - UIO_MEM_PHYS?

DISCUSSION Stanislav Meduna 2 participants 4 messages Feb 17, 2014

lore
Share & tools
lore
#0 lore raw

Hi,

what is the correct way for a UIO driver to pass a memory allocated using dma_alloc_coherent to userspace? I have googled for examples but I was not able to find a definitive answer.

My device needs two 128 kB chunks of DMA-able memory. First I tried

pdev->tx_vaddr = dma_zalloc_coherent(&dev->dev, pdev->dma_len,

                     &pdev->tx_paddr, GFP_KERNEL | GFP_DMA);
  info->mem[2].name = "txdma";
  info->mem[2].addr = (phys_addr_t) pdev->tx_vaddr;
  info->mem[2].size = pdev->dma_len;
  info->mem[2].memtype = UIO_MEM_LOGICAL;

This seemed to work at the first try, but tends to panic in various ways when unmapping. It probably only maps the first page or something like that and accessing past some limit overwrites something.

If I change this to

  info->mem[2].addr = (phys_addr_t) pdev->tx_paddr;
  info->mem[2].memtype = UIO_MEM_PHYS;

it seems to work at least on x86 with < 4GB memory. The uio_pruss.c (or uio_dmem_genirq.c in newer kernels) do this as well.

I have a bad feeling here - if I am allocating something that is a virtual memory in the kernel, I don't expect to pretend I am accessing something else. The UIO howto explicitely states that UIO_MEM_PHYS is meant for a "physical memory on your card".

Is this really a recommended way of doing this and is it portable to other architectures?

I am using 3.4 kernel with realtime patches.

Please Cc: me when replying.

Thanks

Signature · 1 line
                                         Stano
#1 lore raw
Quoted 11 lines · Stanislav Meduna

Hi,

what is the correct way for a UIO driver to pass a memory allocated using dma_alloc_coherent to userspace? I have googled for examples but I was not able to find a definitive answer.

My device needs two 128 kB chunks of DMA-able memory. First I tried

pdev->tx_vaddr = dma_zalloc_coherent(&dev->dev, pdev->dma_len,

                     &pdev->tx_paddr, GFP_KERNEL | GFP_DMA);
Quoted 8 lines
  info->mem[2].name = "txdma";
  info->mem[2].addr = (phys_addr_t) pdev->tx_vaddr;
  info->mem[2].size = pdev->dma_len;
  info->mem[2].memtype = UIO_MEM_LOGICAL;

This seemed to work at the first try, but tends to panic in various ways when unmapping. It probably only maps the first page or something like that and accessing past some limit overwrites something.

Have you tried the uio_dmem_genirq.c driver? Or are you writing a new one?

Quoted 13 lines

If I change this to

  info->mem[2].addr = (phys_addr_t) pdev->tx_paddr;
  info->mem[2].memtype = UIO_MEM_PHYS;

it seems to work at least on x86 with < 4GB memory. The uio_pruss.c (or uio_dmem_genirq.c in newer kernels) do this as well.

I have a bad feeling here - if I am allocating something that is a virtual memory in the kernel, I don't expect to pretend I am accessing something else. The UIO howto explicitely states that UIO_MEM_PHYS is meant for a "physical memory on your card".

Quoted 4 lines

Is this really a recommended way of doing this and is it portable to other architectures?

I am using 3.4 kernel with realtime patches.

The uio_dmem_genirq.c driver showed up in 3.8, so it might be good for you to update your kernel if you want to do DMA memory with a UIO driver, as lots of other things in this area was fixed to accomplish this.

Hope this helps,

greg k-h

#2 lore raw
Quoted 4 lines · Greg KH

The uio_dmem_genirq.c driver showed up in 3.8, so it might be good for you to update your kernel if you want to do DMA memory with a UIO driver, as lots of other things in this area was fixed to accomplish this.

Ah, OK, I will look into this.

I am stuck with 3.4 at the moment because of the real-time patches and some semantic changes there in the newer kernels necessitating much more tweak-and-test time than I currently have...

Thanks

Signature · 1 line
                                      Stano
#3 lore raw
Quoted 7 lines · Stanislav Meduna
Quoted 4 lines · Greg KH

The uio_dmem_genirq.c driver showed up in 3.8, so it might be good for you to update your kernel if you want to do DMA memory with a UIO driver, as lots of other things in this area was fixed to accomplish this.

Quoted 5 lines

Ah, OK, I will look into this.

I am stuck with 3.4 at the moment because of the real-time patches and some semantic changes there in the newer kernels necessitating much more tweak-and-test time than I currently have...

The real-time patches are available for 3.10, so that shouldn't be an excuse anymore :)