Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4249582
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T04:24:58+00:00 2026-05-21T04:24:58+00:00

I want to get data from a DMA enabled, PCIe hardware device into user-space

  • 0

I want to get data from a DMA enabled, PCIe hardware device into user-space as quickly as possible.

Q: How do I combine “direct I/O to user-space with/and/via a DMA transfer”

  1. Reading through LDD3, it seems that I need to perform a few different types of IO operations!?

    dma_alloc_coherent gives me the physical address that I can pass to the hardware device.
    But would need to have setup get_user_pages and perform a copy_to_user type call when the transfer completes. This seems a waste, asking the Device to DMA into kernel memory (acting as buffer) then transferring it again to user-space.
    LDD3 p453: /* Only now is it safe to access the buffer, copy to user, etc. */

  2. What I ideally want is some memory that:

    • I can use in user-space (Maybe request driver via a ioctl call to create DMA’able memory/buffer?)
    • I can get a physical address from to pass to the device so that all user-space has to do is perform a read on the driver
    • the read method would activate the DMA transfer, block waiting for the DMA complete interrupt and release the user-space read afterwards (user-space is now safe to use/read memory).

Do I need single-page streaming mappings, setup mapping and user-space buffers mapped with get_user_pages dma_map_page?

My code so far sets up get_user_pages at the given address from user-space (I call this the Direct I/O part). Then, dma_map_page with a page from get_user_pages. I give the device the return value from dma_map_page as the DMA physical transfer address.

I am using some kernel modules as reference: drivers_scsi_st.c and drivers-net-sh_eth.c. I would look at infiniband code, but cant find which one is the most basic!

Many thanks in advance.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-21T04:24:59+00:00Added an answer on May 21, 2026 at 4:24 am

    I’m actually working on exactly the same thing right now and I’m going the ioctl() route. The general idea is for user space to allocate the buffer which will be used for the DMA transfer and an ioctl() will be used to pass the size and address of this buffer to the device driver. The driver will then use scatter-gather lists along with the streaming DMA API to transfer data directly to and from the device and user-space buffer.

    The implementation strategy I’m using is that the ioctl() in the driver enters a loop that DMA’s the userspace buffer in chunks of 256k (which is the hardware imposed limit for how many scatter/gather entries it can handle). This is isolated inside a function that blocks until each transfer is complete (see below). When all bytes are transfered or the incremental transfer function returns an error the ioctl() exits and returns to userspace

    Pseudo code for the ioctl()

    /*serialize all DMA transfers to/from the device*/
    if (mutex_lock_interruptible( &device_ptr->mtx ) )
        return -EINTR;
    
    chunk_data = (unsigned long) user_space_addr;
    while( *transferred < total_bytes && !ret ) {
        chunk_bytes = total_bytes - *transferred;
        if (chunk_bytes > HW_DMA_MAX)
            chunk_bytes = HW_DMA_MAX; /* 256kb limit imposed by my device */
        ret = transfer_chunk(device_ptr, chunk_data, chunk_bytes, transferred);
        chunk_data += chunk_bytes;
        chunk_offset += chunk_bytes;
    }
    
    mutex_unlock(&device_ptr->mtx);
    

    Pseudo code for incremental transfer function:

    /*Assuming the userspace pointer is passed as an unsigned long, */
    /*calculate the first,last, and number of pages being transferred via*/
    
    first_page = (udata & PAGE_MASK) >> PAGE_SHIFT;
    last_page = ((udata+nbytes-1) & PAGE_MASK) >> PAGE_SHIFT;
    first_page_offset = udata & PAGE_MASK;
    npages = last_page - first_page + 1;
    
    /* Ensure that all userspace pages are locked in memory for the */
    /* duration of the DMA transfer */
    
    down_read(&current->mm->mmap_sem);
    ret = get_user_pages(current,
                         current->mm,
                         udata,
                         npages,
                         is_writing_to_userspace,
                         0,
                         &pages_array,
                         NULL);
    up_read(&current->mm->mmap_sem);
    
    /* Map a scatter-gather list to point at the userspace pages */
    
    /*first*/
    sg_set_page(&sglist[0], pages_array[0], PAGE_SIZE - fp_offset, fp_offset);
    
    /*middle*/
    for(i=1; i < npages-1; i++)
        sg_set_page(&sglist[i], pages_array[i], PAGE_SIZE, 0);
    
    /*last*/
    if (npages > 1) {
        sg_set_page(&sglist[npages-1], pages_array[npages-1],
            nbytes - (PAGE_SIZE - fp_offset) - ((npages-2)*PAGE_SIZE), 0);
    }
    
    /* Do the hardware specific thing to give it the scatter-gather list
       and tell it to start the DMA transfer */
    
    /* Wait for the DMA transfer to complete */
    ret = wait_event_interruptible_timeout( &device_ptr->dma_wait, 
             &device_ptr->flag_dma_done, HZ*2 );
    
    if (ret == 0)
        /* DMA operation timed out */
    else if (ret == -ERESTARTSYS )
        /* DMA operation interrupted by signal */
    else {
        /* DMA success */
        *transferred += nbytes;
        return 0;
    }
    

    The interrupt handler is exceptionally brief:

    /* Do hardware specific thing to make the device happy */
    
    /* Wake the thread waiting for this DMA operation to complete */
    device_ptr->flag_dma_done = 1;
    wake_up_interruptible(device_ptr->dma_wait);
    

    Please note that this is just a general approach, I’ve been working on this driver for the last few weeks and have yet to actually test it… So please, don’t treat this pseudo code as gospel and be sure to double check all logic and parameters ;-).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

No related questions found

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.