通过 /dev/mem 与 PCI 设备通信

communicating with a PCI device through /dev/mem

我需要通过 /dev/mem 与内存 PCI 设备通信。为什么?原因有很多,其中一个是因为我的老板告诉我的。

我想我已经有了答案here。然而,我无法弄清楚的是答案中的 MMIO_ADDR 。如果我想与特定内存区域通信,我会使用存储在 BAR 寄存器中的值作为我的 MMIO_ADDR 吗?如果不是,我该如何与我的 PCI 设备通信?

您的 BAR 将由 BIOS/kernel 分配地址。该地址应该在系统启动时写入 PCI 配置中的 BAR 地址寄存器 header。

比如我这里的一台VM上,e1000设备如下(来自lspci -v):

02:03.0 Ethernet controller: Intel Corporation 82545EM Gigabit Ethernet Controller (Copper) (rev 01)
        Subsystem: VMware PRO/1000 MT Single Port Adapter
        Physical Slot: 35
        Flags: bus master, 66MHz, medium devsel, latency 0, IRQ 17
   =>   Memory at fd560000 (64-bit, non-prefetchable) [size=128K]
        Memory at fdfd0000 (64-bit, non-prefetchable) [size=64K]
        I/O ports at 2080 [size=64]
        [virtual] Expansion ROM at fd520000 [disabled] [size=64K]
        Capabilities: <access denied>
        Kernel driver in use: e1000

您需要阅读该信息(您可以通过 /proc/bus/pci/<Bus>/<DevFn>/sys/bus/pci/devices/ 以二进制形式访问它)以找到 MMIO 地址。

例如,为上述设备转储 PCI 配置 space 的二进制文件显示:

od -tx1z -Ax /proc/bus/pci/02/03.0
000000 86 80 0f 10 17 01 30 02 01 00 00 02 10 00 00 00  >......0.........<
000010 04 00 56 fd 00 00 00 00 04 00 fd fd 00 00 00 00  >..V.............<
000020 81 20 00 00 00 00 00 00 00 00 00 00 ad 15 50 07  >. ............P.<
000030 00 00 00 00 dc 00 00 00 00 00 00 00 0b 01 ff 00  >................<
000040

第一个 BAR(条形区域 #0)位于偏移量 0x10,第二个(条形区域 #2)位于偏移量 0x18。

有关布局和解释的说明,请参阅 https://en.wikipedia.org/wiki/PCI_configuration_space