簡體   English   中英

linux mmap從用戶空間應用程序訪問PCI內存區域

[英]linux mmap access to PCI memory region from user space application

作為對我的 PCI 驅動程序的第一級測試,我希望我可以通過我的用戶應用程序中的 /sys/bus/pci/devices/0000:01:00.0/resource0 文件訪問 pci_iomap 區域。 mmap 的手冊頁、我找到的示例程序和其他帖子似乎表明用戶進程訪問應該有效。 但是有些文章似乎表明 mmap 調用需要通過 ioctl 訪問器從內核內部完成。

我的問題是 PCI sysfs 資源文件的 mmap() 應該從應用程序空間工作嗎?

當我運行我的代碼時,mmap 返回看起來像一個有效地址的內容,但是當我嘗試訪問虛擬地址時出現總線錯誤。 我相信我的終端設備是 FPGA 上的 PCI 到 Xilinx AXI 橋接器運行正常,因為我可以通過 Windows PCIe 實用程序(Win 驅動程序)對它進行 R/W 我在使用 Linux 版本 3.12.37 的 NXP LS1021A ARM7 處理器.

謝謝比爾

並不是說我希望任何人調試我的代碼,但我所做的可能最好由代碼來解釋,所以我也包含了它。 如果粘貼的代碼顯示不正確,我深表歉意。 希望確實如此。

我運行下面的代碼並得到 root@ls1021aiot:~# pcimem /sys/bus/pci/devices/0000:01:00.0/resource0 0 w

/sys/bus/pci/devices/0000:01:00.0/resource0 打開。 目標偏移為 0x0,頁面大小為 4096 映射掩碼為 0xFFF mmap(0, 4096, 0x3, 0x1, 3, 0x0) mmap(0, 4096, 0x3, 0x1, 3, 0x0) PCI 內存映射 4096 字節區域到 map70fx500 . PCI 內存映射訪問 0x 76FB5000。 總線錯誤

 /*
 * 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;
}

你可以看看pci_debug程序代碼,可以在這里 如果您有總線錯誤,則可能是 FPGA 設計問題。 您的 IP AXI 總線是否接受 32 位/16 位/8 位訪問? 確保訪問具有正確地址的內存(如果 32 位地址必須被 4 整除,如果 16 位必須被 2 整除)。

仍然沒有在 NXP LS1021A 處理器上找到解決此問題的方法,但我在 x86_64 機器上進行了測試,並且 mmap 可以用於相同的 PCIe Edge 目標。 我擁有的 NXP ARMv7 處理器是一個 40 位內部地址空間設備,但它是一個 32 位處理器。 這必須為未正確處理的虛擬地址表提供特殊情況。 2 個 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 的調用,我使用的測試現在可以正確運行。

我有同樣的問題(總線錯誤),我沒有做任何工作。

最后用uio_pci_generic解決了。

驅動程序似乎允許從用戶空間使用 mmap(沒有驅動程序,這是無法完成的)。

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

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

如果其他人在這里登陸,請注意您還想查看lspcilshw的輸出以獲取感興趣的資源。 並非所有資源都是可寫的。 我當然見過總線錯誤,但確實有一些東西可以滿足我的目的。 沒有代表所有情況。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM