– 运行64位Fedora 14
– 系统有8GB的RAM
– FPGA(Cyclone IV)位于PCIe卡上
为了实现这一目标,我执行了以下操作:
– 使用memmap 6GB $2GB保留grub中的2GB内存(不启动是我添加内存= 2GB).我可以看到,在/ proc / meminfo中保留了2GB的RAM
– 映射BAR0以允许读取和写入FPGA寄存器(这非常有效)
– 在我的驱动程序中使用remap_pfn_range()实现了一个mmap函数
– 使用ioremap获取缓冲区的虚拟地址
– 添加了ioctl调用(用于测试)以将数据写入缓冲区
– 通过进行ioctl调用将数据写入缓冲区并验证数据是否来自用户空间的缓冲区来测试mmap
我面临的问题是当FPGA开始将DMA数据发送到我提供的缓冲区地址时.我不断得到PTE错误(来自DMAR :)或使用下面的代码我得到以下错误:
DMAR:[DMA写入]请求设备[01:00.0]故障添加器186dc5000
DMAR:[错误原因01]根条目中的当前位清除
DRHD:处理故障状态reg 3
第一行中的地址每次基于来自FPGA的DMA递增0x1000
这是我的init()代码:
#define IMG_BUF_OFFSET 0x180000000UL // Location in RAM (6GB) #define IMG_BUF_SIZE 0x80000000UL // Size of the Buffer (2GB) #define pci_dma_h(addr) ((addr >> 16) >> 16) #define pci_dma_l(addr) (addr & 0xffffffffUL) if((pdev = pci_get_device(FPGA_VEN_ID,FPGA_DEV_ID,NULL))) { printk("FPGA Found on the PCIe Bus\n"); // Enable the device if(pci_enable_device(pdev)) { printk("Failed to enable PCI device\n"); return(-1); } // Enable bus master pci_set_master(pdev); pci_read_config_word(pdev,PCI_VENDOR_ID,&id); printk("Vendor id: %x\n",id); pci_read_config_word(pdev,PCI_DEVICE_ID,&id); printk("Device id: %x\n",PCI_STATUS,&id); printk("Device Status: %x\n",id); pci_read_config_dword(pdev,PCI_COMMAND,&temp); printk("Command Register : : %x\n",temp); printk("Resources Allocated :\n"); pci_read_config_dword(pdev,PCI_BASE_ADDRESS_0,&temp); printk("BAR0 : %x\n",temp); // Get the starting address of BAR0 bar0_ptr = (unsigned int*)pcim_iomap(pdev,FPGA_CONFIG_SIZE); if(!bar0_ptr) { printk("Error mapping Bar0\n"); return -1; } printk("Remapped BAR0\n"); // Set DMA Masking if(!pci_set_dma_mask(pdev,DMA_BIT_MASK(64))) { pci_set_consistent_dma_mask(pdev,DMA_BIT_MASK(64)); printk("Device setup for 64bit DMA\n"); } else if(!pci_set_dma_mask(pdev,DMA_BIT_MASK(32))) { pci_set_consistent_dma_mask(pdev,DMA_BIT_MASK(32)); printk("Device setup for 32bit DMA\n"); } else { printk(KERN_WARNING"No suitable DMA available.\n"); return -1; } // Get a pointer to reserved lower RAM in kernel address space (virtual address) virt_addr = ioremap(IMG_BUF_OFFSET,IMG_BUF_SIZE); kernel_image_buffer_ptr = (unsigned char*)virt_addr; memset(kernel_image_buffer_ptr,IMG_BUF_SIZE); printk("Remapped image buffer: 0x%p\n",(void*)virt_addr);
}
这是我的mmap代码:
unsigned long image_buffer; unsigned int low; unsigned int high; if(remap_pfn_range(vma,vma->vm_start,vma->vm_pgoff,vma->vm_end - vma->vm_start,vma->vm_page_prot)) { return(-EAGAIN); } image_buffer = (vma->vm_pgoff << PAGE_SHIFT); if(0 > check_mem_region(IMG_BUF_OFFSET,IMG_BUF_SIZE)) { printk("Failed to check region...memory in use\n"); return -1; } request_mem_region(IMG_BUF_OFFSET,IMG_BUF_SIZE,DRV_NAME); // Get the bus address from the virtual address above //dma_page = virt_to_page(addr); //dma_offset = ((unsigned long)addr & ~PAGE_MASK); //dma_addr = pci_map_page(pdev,dma_page,dma_offset,PCI_DMA_FROMDEVICE); //dma_addr = pci_map_single(pdev,image_buffer,PCI_DMA_FROMDEVICE); //dma_addr = IMG_BUF_OFFSET; //printk("DMA Address: 0x%p\n",(void*)dma_addr); // Write start or image buffer address to the FPGA low = pci_dma_l(image_buffer); low &= 0xfffffffc; high = pci_dma_h(image_buffer); if(high != 0) low |= 0x00000001; *(bar0_ptr + (17024/4)) = 0; //printk("DMA Address LOW : 0x%x\n",cpu_to_le32(low)); //printk("DMA Address HIGH: 0x%x\n",cpu_to_le32(high)); *(bar0_ptr + (4096/4)) = cpu_to_le32(low); //2147483649; *(bar0_ptr + (4100/4)) = cpu_to_le32(high); *(bar0_ptr + (17052/4)) = cpu_to_le32(low & 0xfffffffe);//2147483648; printk("Process Read Command: Addr:0x%x Ret:0x%x\n",4096,*(bar0_ptr + (4096/4))); printk("Process Read Command: Addr:0x%x Ret:0x%x\n",4100,*(bar0_ptr + (4100/4))); printk("Process Read Command: Addr:0x%x Ret:0x%x\n",17052,*(bar0_ptr + (17052/4))); return(0);
感谢您提供任何帮助.
解决方法
要定位RAM的高2GB,您需要从设备发送2DW(64位)地址.您的Fmt / Type中的位是否设置为执行此操作?故障地址看起来像一个屏蔽的32位总线地址,所以这个级别的东西可能不正确.还要记住,因为PCIe是big-endian,在将目标地址写入PCIe设备端点时要小心.如果Fmt不正确,您可能会将目标地址的较低字节落入有效负载中 – 同样适当的BFM应该发现结果数据包长度不匹配.
如果您有最近的主板/现代cpu,PCIe端点应该执行PCIe AER(高级错误报告),因此如果运行最近的Centos / RHEL 6.3,您应该获得端点故障的dmesg报告.这非常有用,因为报告将数据包的第一批DW捕获到特殊捕获寄存器,因此您可以查看收到的TLP.
在你的内核驱动程序中,我看到你设置了DMA掩码,这还不够,因为你没有编程mmu来允许从设备写入页面.查看pci_alloc_consistent()的实现,看看你应该调用什么来实现这一点.