1. 程式人生 > >常用的連續實體記憶體的獲取方法(一)

常用的連續實體記憶體的獲取方法(一)

獲得連續實體記憶體的方法有很多,在很多地方搜了但是自己拿過來用就是用不了,這裡展示幾個親測能用的。包括dma_alloc_coherent和kmalloc等。

程式碼:

#include <linux/module.h>
#include <linux/init.h>
#include <linux/pci.h>
#include <linux/slab.h>
#include <linux/dma-mapping.h>
#include <linux/dmapool.h>
#include <linux/device.h>

// int direction = PCI_DMA_TODEVICE ;
// int direction = PCI_DMA_FROMDEVICE ;
static int direction = PCI_DMA_BIDIRECTIONAL;
//int direction = PCI_DMA_NONE;

static char *kbuf;
static dma_addr_t handle;
static size_t size = (10 * PAGE_SIZE);
static struct dma_pool *mypool;
static size_t pool_size = 1024;
static size_t pool_align = 8;

static void my_release(struct device *dev)
{
    pr_info("releasing DMA device\n");
}

static struct device dev = {
    .release = my_release
};

static void output(char *kbuf, dma_addr_t handle, size_t size, char *string)
{
    unsigned long diff;
    diff = (unsigned long)kbuf - handle;
    pr_info("kbuf=%12p, handle=%12p, size = %d\n", kbuf,
        (unsigned long *)handle, (int)size);
    pr_info("(kbuf-handle)= %12p, %12lu, PAGE_OFFSET=%12lu, compare=%lu\n",
        (void *)diff, diff, PAGE_OFFSET, diff - PAGE_OFFSET);
    strcpy(kbuf, string);
    pr_info("string written was, %s\n", kbuf);
}

static int __init my_init(void)
{
    dev_set_name(&dev, "my0");
    device_register(&dev);

    /* dma_alloc_coherent method */

    pr_info("Loading DMA allocation test module\n");
    pr_info("\nTesting dma_alloc_coherent()..........\n\n");
    kbuf = dma_alloc_coherent(NULL, size, &handle, GFP_KERNEL);
    output(kbuf, handle, size, "This is the dma_alloc_coherent() string");
    dma_free_coherent(NULL, size, kbuf, handle);

    /* dma_map/unmap_single */

    pr_info("\nTesting dma_map_single()................\n\n");
    kbuf = kmalloc(size, GFP_KERNEL);
    handle = dma_map_single(NULL, kbuf, size, direction);
    output(kbuf, handle, size, "This is the dma_map_single() string");
    dma_unmap_single(NULL, handle, size, direction);
    kfree(kbuf);

    /* dma_pool method */
    /*
    pr_info("\nTesting dma_pool_alloc()..........\n\n");
    mypool = dma_pool_create("mypool", NULL, pool_size, pool_align, 0);
    kbuf = dma_pool_alloc(mypool, GFP_KERNEL, &handle);
    output(kbuf, handle, size, "This is the dma_pool_alloc() string");
    dma_pool_free(mypool, kbuf, handle);
    dma_pool_destroy(mypool);
    */
    device_unregister(&dev);

    return 0;
}

static void __exit my_exit(void)
{
    pr_info("Module Unloading\n");
}

module_init(my_init);
module_exit(my_exit);

MODULE_AUTHOR("Jerry Cooperstein");
MODULE_DESCRIPTION("LDD:2.0 s_23/lab1_dma.c");
MODULE_LICENSE("GPL v2");
效果:

[ 5689.589449] Loading DMA allocation test module
[ 5689.589450] 
               Testing dma_alloc_coherent()..........


[ 5689.589463] kbuf=ffff8e8b42240000, handle=     2240000, size = 40960
[ 5689.589464] (kbuf-handle)= ffff8e8b40000000, 18446619327458181120, PAGE_OFFSET=18446619327458181120, compare=0
[ 5689.589464] string written was, This is the dma_alloc_coherent() string
[ 5689.589465] 
               Testing dma_map_single()................


[ 5689.589466] kbuf=ffff8e8b42240000, handle=     2240000, size = 40960
[ 5689.589466] (kbuf-handle)= ffff8e8b40000000, 18446619327458181120, PAGE_OFFSET=18446619327458181120, compare=0
[ 5689.589467] string written was, This is the dma_map_single() string
[ 5689.589492] releasing DMA device
環境:Linux ubuntu 4.13.0-26-generic 

不足之處是dma_pool method沒有實現,夥伴們可自行實踐然後一起交流哦。