Skip to content
TheChatArchiveThe Chat Archive
gpt-3.5-turbo-0301· openaiHugging Face Suzhen/CodeChat

Sample code for Linux 4.19 DMA testing program

  1. User

    Sample code for Linux 4.19 DMA testing program

  2. Assistant

    As an AI language model, I cannot code or provide code related help in live environment, but I can provide you with code snippet that can help you for Linux DMA testing program.

    Here is a sample code for Linux 4.19 DMA testing program:

    #include <linux/module.h>
    #include <linux/init.h>
    #include <linux/dmaengine.h>
    #include <linux/dma-mapping.h>
    #include <linux/slab.h>
    
    MODULE_LICENSE("GPL");
    
    static struct dma_chan *chan;
    
    static int __init dma_init(void)
    {
        dma_cap_mask_t mask;
        struct dma_async_tx_descriptor *desc;
        dma_cookie_t cookie;
        unsigned int len = 4096;
        dma_addr_t src, dst;
        void *src_buf, *dst_buf;
    
        src_buf = kmalloc(len, GFP_KERNEL);
        dst_buf = kmalloc(len, GFP_KERNEL);
        if (!src_buf || !dst_buf) {
            printk(KERN_ERR "Failed to allocate memory\n");
            return -ENOMEM;
        }
    
        memset(src_buf, 0xCC, len);
        memset(dst_buf, 0, len);
    
        dma_cap_zero(mask);
        dma_cap_set(DMA_MEMCPY, mask);
    
        chan = dma_request_channel(mask, NULL, NULL);
        if (!chan) {
            printk(KERN_ERR "Failed to request DMA channel\n");
            goto fail;
        }
    
        src = dma_map_single(chan->device->dev, src_buf, len, DMA_TO_DEVICE);
        dst = dma_map_single(chan->device->dev, dst_buf, len, DMA_FROM_DEVICE);
    
        desc = dmaengine_prep_dma_memcpy(chan, dst, src, len, DMA_PREP_INTERRUPT);
        if (!desc) {
            printk(KERN_ERR "Failed to prepare DMA operation\n");
            goto fail_dma;
        }
    
        cookie = dmaengine_submit(desc);
        dma_async_issue_pending(chan);
    
        if (dma_submit_error(cookie)) {
            printk(KERN_ERR "Failed to submit DMA operation\n");
            goto fail_dma;
        }
    
        dma_sync_wait(chan, cookie);
        dma_unmap_single(chan->device->dev, src, len, DMA_TO_DEVICE);
        dma_unmap_single(chan->device->dev, dst, len, DMA_FROM_DEVICE);
    
        if (memcmp(src_buf, dst_buf, len)) {
            printk(KERN_ERR "DMA transfer failed\n");
            goto fail_dma;
        }
    
        printk(KERN_INFO "DMA transfer succeeded\n");
    
        dma_release_channel(chan);
        kfree(src_buf);
        kfree(dst_buf);
    
        return 0;
    
    fail_dma:
        if (chan)
            dma_release_channel(chan);
    
        dma_unmap_single(chan->device->dev, src, len, DMA_TO_DEVICE);
        dma_unmap_single(chan->device->dev, dst, len, DMA_FROM_DEVICE);
    
    fail:
        kfree(src_buf);
        kfree(dst_buf);
    
        return -EFAULT;
    }
    
    static void __exit dma_exit(void)
    {
    }
    
    module_init(dma_init);
    module_exit(dma_exit);
    

    This code initializes a DMA channel and performs a DMA memcpy operation to transfer data from one memory address to another. It uses the dmaengine_prep_dma_memcpy() function to prepare the DMA descriptor and dmaengine_submit() function to submit the DMA descriptor for execution. It also uses dma_sync_wait() function to wait for the DMA operation to complete.

Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.