首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >linux mmap从用户空间应用程序访问PCI内存区域

linux mmap从用户空间应用程序访问PCI内存区域
EN

Stack Overflow用户
提问于 2016-10-18 18:04:47
回答 4查看 11.9K关注 0票数 4

作为对我的PCI驱动程序的第一级测试,我希望能够通过我的用户应用程序中的/sys/bus/pci/设备/0000:01:00.0/ pci_iomap 0文件访问区域。mmap的手册页、我找到的示例程序和其他帖子似乎表明用户进程访问应该工作。但有些文章似乎指出,mmap调用需要通过ioctl访问器在内核内执行。

我的问题是PCI sysfs资源文件的mmap()应该在应用程序空间?中工作吗?

当我运行我的代码时,mmap返回一个看起来有效的地址,但是当我尝试访问虚拟地址时,我会得到一个总线错误。我相信我的终端设备-- PCI到Xilinx桥--在FPGA上运行正常,因为我可以通过一个windows PCIe实用程序(Win驱动程序)对它进行R/W处理,我在一个带有Linux 3.12.37的LS1021A ARM7处理器上。

谢谢比尔

并不是我想让任何人调试我的代码,但是我所做的可能最好的解释了代码,所以我也包含了它。如果粘贴的代码没有正确显示,我很抱歉。希望是的。

我运行下面的代码,得到root@ls1021aiot:~# pcimem /sys/bus/pci/设备/0000:01:00.0/resource0 0 0 w

/sys/bus/pci/设备/0000:01:00.0/resource0 0打开。目标偏移量为0x0,页大小为4096映射掩码为0xFFF mmap(0,4096,0x3,0x1,3,0x0) mmap(0,4096,0x3,0x0)将PCI 4096字节区域映射到map_base 0x76fb5000。PCI内存映射访问0x76FB5000。总线误差

代码语言:javascript
复制
 /*
 * 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 on\n"
                "\toffset  : offset into pci memory region to act upon\n"
                "\ttype    : access operation type : [b]yte, [h]alfword, [w]ord\n"
                "\tdata    : data to be written\n\n",
                argv[0]);
        exit(1);
    }   
    filename = argv[1];
    target = strtoul(argv[2], 0, 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%lX\n", (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, MAP_SIZE, PROT_READ | PROT_WRITE, MAP_SHARED, fd, target & ~MAP_MASK);
#endif
    printf("mmap(%d, %ld, 0x%x, 0x%x, %d, 0x%x)\n", 0, MAP_SIZE, PROT_READ | PROT_WRITE, MAP_SHARED, fd, (int) (target & ~MAP_MASK));
    map_base = mmap(0, MAP_SIZE, PROT_READ | PROT_WRITE, MAP_SHARED, fd, (target & ~MAP_MASK));
    if(map_base == (void *) -1){
       printf("PCI Memory mapped ERROR.\n");
        PRINT_ERROR;
        close(fd);
        return 1;
    }

    printf("mmap(%d, %ld, 0x%x, 0x%x, %d, 0x%x)\n", 0, MAP_SIZE, PROT_READ | PROT_WRITE, MAP_SHARED, fd, (int) (target & ~MAP_MASK));
    printf("PCI Memory mapped %ld byte region to map_base 0x%08lx.\n", MAP_SIZE, (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%X\n", (int) target, 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, 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%X\n", writeval, read_result);
        fflush(stdout);
    }

    if(munmap(map_base, MAP_SIZE) == -1) { PRINT_ERROR;}
    close(fd);
    return 0;
}
EN

回答 4

Stack Overflow用户

发布于 2016-10-19 07:22:05

您可以查看调试程序代码,可用的这里。如果您有总线错误,这可能是一个FPGA的设计问题。您的IP AXI总线是否接受32位/16位/8位的访问?确保使用正确的地址访问内存(如果32位地址必须被4除,如果16位被2除)。

票数 3
EN

Stack Overflow用户

发布于 2016-11-17 12:44:55

在NXP LS1021A处理器上还没有找到修复程序,但是我在一台x86_64机器上进行了测试,在那里mmap工作在同一个PCIe边缘目标上。我所拥有的NXP ARMv7处理器是一个40位内部地址空间设备,但它是一个32位处理器。这必须为未正确处理的虚拟地址表提供一个特例。两个PCIe根设备被映射到较高的地址空间。最后,我只编写了一个IOCTL驱动程序,并使用内核空间中的读和写例程来访问我在探测设备时缓存的pci_iomap()指针。NXP提供了一个建议,我在下面引用,但我仍然得到相同的总线错误。如果他们解决了我会更新的。

关于mmap。这是来自软件团队的响应。使用mmap64()将40位phy_address映射到32 virt_addr,或者继续使用mmap,但在编译时添加“-D_FILE_OFFSET_BITS=64”。

最新消息。我从NXP收到了一个新版本的工具链,mmap64在其中可用。我对此的看法是,当您希望访问文件和偏移大于4GB文件时,只需要在32位系统上使用mmap64支持。通过用mmap64替换对mmap的调用,我使用的测试现在正确运行。

票数 2
EN

Stack Overflow用户

发布于 2018-12-14 22:08:25

我有同样的问题(公共汽车错误),而我没有做任何工作。

最后,使用uio_pci_generic解决了这一问题。

驱动程序似乎允许从用户空间使用mmap (没有驱动程序,这是不可能的)。

更多信息可以在这里找到:

https://github.com/rumpkernel/wiki/wiki/Howto:-Accessing-PCI-devices-from-userspace

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/40115097

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档