linux - 我是否需要 "enable"Linux 3.12 驱动程序中的 PCIe 内存区域?

标签 linux linux-kernel driver linux-device-driver pci-e

我有代码,从我的 PCIe 驱动程序的 probe() 函数调用(大致基于此 post ):

编辑: 基于 Andreas Bombe's响应,我更改了代码以使用 pci_iomap(),但我仍然遇到系统挂起

static my_pci_dev pci_dev; /* local structure */
static int pci_setup_region(struct pci_dev *dev)
{
    int bar = 0;

    pci_dev.physical.addr = pci_resource_start(dev, bar);
    pci_dev.physical.size = pci_resource_len(dev, bar);

    pci_dev.virtual.addr = pci_iomap(dev, bar, pci_dev.physical.size);
    if (NULL == pci_dev.virtual.addr) {
        return -ENOMEM;
    } else {
        pci_dev.virtual.size = pci_dev.physical.size;
    }
    printk(KERN_ALERT "Virtual address: %p", pci_dev.virtual.addr);
    if (request_mem_region(pci_dev.physical.addr, pci_dev.physical.size, DEVICE_NAME) == NULL) {
        pci_release_resources();
        return -EBUSY;
    } else {
        pci_dev.physical.allocated = 1;
    }

    /* Test it out! */
    printk(KERN_ALERT "Trying to read data.\n");
    printk(KERN_ALERT "Copied chip-id data:%08x", ioread8(pci_dev.virtual.addr));
    return 0;
}

但是内核只是在调用 ioread8() 时挂起。

我做错了什么吗?还是我需要查看硬件?

下面是 lspci -vcat/proc/iomem 系统干净启动时的输出:

root@socfpga:~# lspci -v
00:00.0 PCI bridge: Altera Corporation Device e000 (rev 01) (prog-if 00 [Normal decode])
    Flags: fast devsel
    Bus: primary=00, secondary=01, subordinate=01, sec-latency=0
    I/O behind bridge: 00000000-00000fff
    Memory behind bridge: c0000000-c00fffff
    Prefetchable memory behind bridge: 00000000-000fffff
    Capabilities: [50] MSI: Enable- Count=1/4 Maskable- 64bit+
    Capabilities: [78] Power Management version 3
    Capabilities: [80] Express Root Port (Slot-), MSI 00
    Capabilities: [100] Virtual Channel
    Capabilities: [200] Vendor Specific Information: ID=1172 Rev=0 Len=044 <?>

01:00.0 Multimedia audio controller: Altera Corporation Device e002 (rev 01)
    Subsystem: Altera Corporation Device e002
    Flags: fast devsel, IRQ 75
    Memory at c0000000 (32-bit, non-prefetchable) [disabled] [size=128K]
    Capabilities: [50] MSI: Enable- Count=1/4 Maskable- 64bit+
    Capabilities: [78] Power Management version 3
    Capabilities: [80] Express Endpoint, MSI 00
    Capabilities: [100] Virtual Channel
    Capabilities: [200] Vendor Specific Information: ID=1172 Rev=0 Len=044 <?>
    Kernel modules: test-pci

内存出现[disabled]:我必须启用它吗?

root@socfpga:~# cat /proc/iomem 
00000000-3fffffff : System RAM
  00008000-006fa7d3 : Kernel code
  00754000-007d8c23 : Kernel data
c0000000-cfffffff : ALTERA PCIE RP MEM
  c0000000-c00fffff : PCI Bus 0000:01
    c0000000-c001ffff : 0000:01:00.0
d0000000-dfffffff : ALTERA PCIE RP PREF MEM
ff200000-ff20000f : csr
ff200010-ff20008f : vector_slave
ff210000-ff21003f : ff210000.chipidbridge0
ff280000-ff283fff : Cra
ff702000-ff703fff : /soc/ethernet@ff702000
ff704000-ff704fff : /soc/dwmmc0@ff704000
ff705000-ff705fff : ff705000.spi
ffa00000-ffa00fff : ff705000.spi
ffb40000-ffb4fffe : /soc/usb@ffb40000
ffc00000-ffc00fff : c_can_platform
ffc02000-ffc0201f : serial
ffc04000-ffc04fff : /soc/i2c@ffc04000
ffd02000-ffd02fff : /soc/wd@ffd02000
ffe01000-ffe01fff : /soc/amba/pdma@ffe01000
fff00000-fff00fff : fff00000.spi
ffff0000-ffffffff : /soc/sram@ffff0000
root@socfpga:~# 

最佳答案

您一定要启用它。这些是基本步骤:

pci_enable_device(dev);
pci_request_regions(dev, "driver/device name");
bar0 = pci_iomap(dev, 0, 0);
x = ioread(bar0 + offset);  /* there you are */

所有 pci_* 调用都需要进行错误检查。如果设备需要执行 DMA,您还需要调用 pci_set_masterpci_set_dma_mask

详细来说,绕过 PCI 内核代码并直接对 BAR 进行 ioremapping 可能在很久以前就已经奏效了。我不确定它在当前代码中是否合法,但它肯定是不可取的。

关于linux - 我是否需要 "enable"Linux 3.12 驱动程序中的 PCIe 内存区域?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23414288/

相关文章:

build - Linux中的 'make'命令是什么意思?

driver - 三星 Galaxy S3 没有出现在 Eclipse 中,而是出现在设备管理器中?

java - 我不太清楚如何让这些公共(public)整数发挥作用

linux - 在管道 awk 语句之间传递变量

linux - 编写兼容的看门狗内核模块

c - 将 block_device 映射到设备结构

无法安装我的驱动程序

linux - 甲骨文毫秒 : Avoid blank-padding in Embedded SQL

linux - 有没有办法检查最近是否刷新了处理器缓存?

linux - Chroot 到 gui 不工作