实现 PCIe Linux 设备驱动程序(想要从内核驱动程序访问我的卡寄存器)

发布于 2024-10-20 02:03:24 字数 1214 浏览 2 评论 0原文

我正在编写一个设备驱动程序来访问 PCIe 卡上 FPGA 中的内存。
卡启动并被探测/发现:-

/proc/iomem

80000000-840fffff : PCI Bus #03
  80000000-83ffffff : 0000:03:00.0
  84000000-840fffff : 0000:03:00.0

因此,在读取 ldd/etc 时,我在 80000000 处编写了对 request_mem_region 的调用,并请求指向它的指针通过 ioremap_nocache

1) 我需要 request_mem_region 以及 ioremap_nocache,我不能只使用后者吗?

/proc/iomem 在 insmod 我的设备驱动程序之后:-

80000000-840fffff : PCI Bus #03
  80000000-83ffffff : 0000:03:00.0
    80000000-8003ffff : fp2
  84000000-840fffff : 0000:03:00.0

2) 对我来说看起来不太正确......?

无论如何,读取不起作用(它的编码不像下面这样,它有检查等):-

#define BAR_ADDR 0x80000000
void *base = ioremap_nocache(BAR_ADDR, 0x40000);
void *address = base + KNOWN_REG_LOCATION;
int data = ioread32(address);
printk("fp2: address:0x%08x, data:0x%08x\n", address, data);

输出:-

address:0xfd500000, data:0xffffffff

我可以从 mmap 用户空间读取 x80000000+KNOWN_REG_LOCATION

3)我也尝试过 __raw_readl/readl 但没有运气。

4) 我可以只读取当前映射的地址x80000000吗?

I'm writing a device driver to access the memory in a FPGA on a PCIe card.
The card boots and is probed/found :-

/proc/iomem

80000000-840fffff : PCI Bus #03
  80000000-83ffffff : 0000:03:00.0
  84000000-840fffff : 0000:03:00.0

So reading ldd/etc I coded up a call to request_mem_region at the 80000000, and requested a pointer to it via ioremap_nocache

1) Do I need to request_mem_region as well as a ioremap_nocache, cant I use just the latter?

/proc/iomem After insmod my device driver :-

80000000-840fffff : PCI Bus #03
  80000000-83ffffff : 0000:03:00.0
    80000000-8003ffff : fp2
  84000000-840fffff : 0000:03:00.0

2) Doesnt look quite right to me...?

Anyway, reads don't work (its not coded like below, it has checks etc):-

#define BAR_ADDR 0x80000000
void *base = ioremap_nocache(BAR_ADDR, 0x40000);
void *address = base + KNOWN_REG_LOCATION;
int data = ioread32(address);
printk("fp2: address:0x%08x, data:0x%08x\n", address, data);

Outputs :-

address:0xfd500000, data:0xffffffff

I can read the x80000000+KNOWN_REG_LOCATION from mmap userspace.

3) I've tried __raw_readl/readl with no luck as well.

4) Can I just read at the currently mapped address x80000000?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

乖乖公主 2024-10-27 02:03:24

Ian,

我为设备编写了一个 PCI 驱动程序(完整源代码)。不过,寄存器空间的映射应该是相同的。我是这样做的。

dm7820_device->pci[region].virt_addr = ioremap_nocache(address, length);
if (dm7820_device->pci[region].virt_addr == NULL) {
    printk(KERN_ERR "%s: ERROR: BAR%u remapping FAILED\n",
        &((dm7820_device->device_name)[0]), region);
    dm7820_release_resources();
    return -ENOMEM;
}

if (request_mem_region(address, length, &((dm7820_device->device_name)[0])) == NULL) {
    printk(KERN_ERR "%s: ERROR: I/O memory range %#lx-%#lx allocation FAILED\n",
        &((dm7820_device->device_name)[0]), address, (address + length - 1));
    dm7820_release_resources();
    return -EBUSY;
}

地址和长度值由 pci_resource_start()pci_resource_length() 调用返回。

然后,您可以使用 dm7820_device->pci[region].virt_addr + 使用 ioread32() 访问它,

如果您有任何问题,请告诉我。

Ian,

I wrote a PCI driver for a device (full source). The mapping of the register space should be the same though. Here is how I do it.

dm7820_device->pci[region].virt_addr = ioremap_nocache(address, length);
if (dm7820_device->pci[region].virt_addr == NULL) {
    printk(KERN_ERR "%s: ERROR: BAR%u remapping FAILED\n",
        &((dm7820_device->device_name)[0]), region);
    dm7820_release_resources();
    return -ENOMEM;
}

if (request_mem_region(address, length, &((dm7820_device->device_name)[0])) == NULL) {
    printk(KERN_ERR "%s: ERROR: I/O memory range %#lx-%#lx allocation FAILED\n",
        &((dm7820_device->device_name)[0]), address, (address + length - 1));
    dm7820_release_resources();
    return -EBUSY;
}

The address and length values are returned from pci_resource_start() and pci_resource_length() calls.

Then you can access it using ioread32() using dm7820_device->pci[region].virt_addr + <register offset>

Let me know if you have any questions.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文