下面为用户层函数代码:
void simple_test(char *filename, int dma_fd)
{
int file_fd
void *user_mem = NULL;
int pagesize = 0;
int rc;
file_fd = open(filename, O_RDWR | O_CREAT | O_TRUNC | O_DIRECT, 0666);
user_mem = mmap(0, pagesize, PROT_READ | PROT_WRITE, MAP_SHARED, dma_fd, 0);
if (user_mem == (void*) -1)
{
fprintf(stderr, "mmap: %s\n", strerror(errno));
exit(-1);
}
printf("user_mem %p\n", user_mem);
rc = write(file_fd, user_mem, pagesize);
if (rc < 0)
{
perror("write file");
}
else if(rc != pagesize)
{
printf("Write file fail 0x%lx != 0x%lx.\n", rc, pagesize);
}
rc = munmap(user_mem, pagesize);
if (rc)
{
perror("munmap");
}
}
内核驱动使用dma_alloc_coherent分配的内存,使用dma_mmap_coherent映射到用户空间。
执行测试函数simple_test时报write file: Bad addree
映射的地址和长度都是页大小的整数倍。不使用O_DIRECT时没有错误。