linux mmap从用户空间应用程序访问PCI内存区域
|
作为我的PCI驱动程序的第一级测试,我希望我可以通过/sys/bus/pci/devices/0000:01:00.0/resource0访问pci_iomap区域
来自我的用户应用程序的文mmap的手册页,我找到的示例程序以及其他帖子似乎表明用户进程访问应该有效.但是有些文章似乎表明mmap调用需要通过ioctl访问器在内核中完成. 我的问题是PCI sysfs资源文件的mmap()应该来自应用程序空间吗? 当我运行我的代码时,mmap返回看起来像有效地址的内容但是当我尝试访问虚拟地址时出现Bus错误. 谢谢 并不是说我希望任何人调试我的代码,但我正在做的事情最好用代码来解释,所以我也把它包括在内.如果粘贴的代码无法正确显示,我深表歉意.希望它能做到. 我运行下面的代码并得到 /sys/bus/pci/devices/0000:01:00.0/resource0已打开. /*
* pcimem.c: Simple program to read/write from/to a pci device from userspace.
*
* Copyright (C) 2010,Bill Farrow (bfarrow@beyondelectronics.us)
*
* Based on the devmem2.c code
* Copyright (C) 2000,Jan-Derk Bakker (J.D.Bakker@its.tudelft.nl)
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License,or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not,write to the Free Software
* Foundation,Inc.,59 Temple Place,Suite 330,Boston,MA 02111-1307 USA
*
*/
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <unistd.h>
#include <string.h>
#include <errno.h>
#include <signal.h>
#include <fcntl.h>
#include <ctype.h>
#include <termios.h>
#include <sys/types.h>
#include <sys/mman.h>
#include <linux/pci.h>
#define PRINT_ERROR
do {
fprintf(stderr,"Error at line %d,file %s (%d) [%s]n",
__LINE__,__FILE__,errno,strerror(errno)); exit(1);
} while(0)
#define MAP_SIZE 4096UL
#define MAP_MASK (MAP_SIZE - 1)
int main(int argc,char **argv) {
int fd;
void *map_base,*virt_addr;
uint32_t read_result,writeval;
char *filename;
off_t target;
int access_type = 'w';
if(argc < 3) {
// pcimem /sys/bus/pci/devices/0001:00:07.0/resource0 0x100 w 0x00
// argv[0] [1] [2] [3] [4]
fprintf(stderr,"nUsage:t%s { sys file } { offset } [ type [ data ] ]n"
"tsys file: sysfs file for the pci resource to act onn"
"toffset : offset into pci memory region to act uponn"
"ttype : access operation type : [b]yte,[h]alfword,[w]ordn"
"tdata : data to be writtennn",argv[0]);
exit(1);
}
filename = argv[1];
target = strtoul(argv[2],0);
if(argc > 3)
access_type = tolower(argv[3][0]);
if((fd = open(filename,O_RDWR | O_SYNC)) == -1){
PRINT_ERROR;
}
printf("%s opened.n",filename);
printf("Target offset is 0x%x,page size is %ld map mask is 0x%lXn",(int) target,sysconf(_SC_PAGE_SIZE),MAP_MASK);
fflush(stdout);
/* Map one page */
#if 0
//map_base = mmap(0,MAP_SIZE,PROT_READ | PROT_WRITE,MAP_SHARED,fd,(off_t) (target & ~MAP_MASK));
//map_base = mmap(0,target & ~MAP_MASK);
#endif
printf("mmap(%d,%ld,0x%x,%d,0x%x)n",(int) (target & ~MAP_MASK));
map_base = mmap(0,(target & ~MAP_MASK));
if(map_base == (void *) -1){
printf("PCI Memory mapped ERROR.n");
PRINT_ERROR;
close(fd);
return 1;
}
printf("mmap(%d,(int) (target & ~MAP_MASK));
printf("PCI Memory mapped %ld byte region to map_base 0x%08lx.n",(unsigned long) map_base);
fflush(stdout);
virt_addr = map_base + (target & MAP_MASK);
printf("PCI Memory mapped access 0x %08X.n",(uint32_t ) virt_addr);
switch(access_type) {
case 'b':
read_result = *((uint8_t *) virt_addr);
break;
case 'h':
read_result = *((uint16_t *) virt_addr);
break;
case 'w':
read_result = *((uint32_t *) virt_addr);
printf("READ Value at offset 0x%X (%p): 0x%Xn",virt_addr,read_result);
break;
default:
fprintf(stderr,"Illegal data type '%c'.n",access_type);
exit(2);
}
fflush(stdout);
if(argc > 4) {
writeval = strtoul(argv[4],0);
switch(access_type) {
case 'b':
*((uint8_t *) virt_addr) = writeval;
read_result = *((uint8_t *) virt_addr);
break;
case 'h':
*((uint16_t *) virt_addr) = writeval;
read_result = *((uint16_t *) virt_addr);
break;
case 'w':
*((uint32_t *) virt_addr) = writeval;
read_result = *((uint32_t *) virt_addr);
break;
}
printf("Written 0x%X; readback 0x%Xn",writeval,read_result);
fflush(stdout);
}
if(munmap(map_base,MAP_SIZE) == -1) { PRINT_ERROR;}
close(fd);
return 0;
}
解决方法
您可以查看
pci_debug程序代码,可用
here.如果您遇到总线错误,则可能是FPGA设计问题.您的IP AXI总线是否接受32位/ 16位/ 8位访问? 确保使用正确的地址访问存储器(如果32位地址必须可被4整除,如果16位乘以2).
(编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
