From ef5f6fa3a8f05a3d081590f9e16f182f385dbc61 Mon Sep 17 00:00:00 2001 From: Daniel Krebs Date: Tue, 13 Feb 2018 14:06:17 +0100 Subject: [PATCH] lib/card: use memory manager to store vfio mapping --- fpga/include/villas/fpga/card.hpp | 6 +++- fpga/lib/card.cpp | 50 ++++++++++++++++++------------- 2 files changed, 34 insertions(+), 22 deletions(-) diff --git a/fpga/include/villas/fpga/card.hpp b/fpga/include/villas/fpga/card.hpp index 4b64a33ec..4676e0b58 100644 --- a/fpga/include/villas/fpga/card.hpp +++ b/fpga/include/villas/fpga/card.hpp @@ -45,6 +45,8 @@ #include "config.h" +#include "memory_manager.hpp" + #define PCI_FILTER_DEFAULT_FPGA { \ .id = { \ .vendor = FPGA_PCI_VID_XILINX, \ @@ -92,7 +94,9 @@ public: ::vfio_container *vfio_container; struct vfio_device vfio_device; /**< VFIO device handle. */ - char *map; /**< PCI BAR0 mapping for register access */ + /// Address space identifier of the master address space of this FPGA card. + /// This will be used for address resolution of all IPs on this card. + MemoryManager::AddressSpaceId addrSpaceId; size_t maplen; size_t dmalen; diff --git a/fpga/lib/card.cpp b/fpga/lib/card.cpp index 5b38d210c..c4a184fc9 100644 --- a/fpga/lib/card.cpp +++ b/fpga/lib/card.cpp @@ -20,31 +20,18 @@ * along with this program. If not, see . *********************************************************************************/ -#include -#include +#include +#include +#include -#include "config.h" -#include "log.h" -#include "log_config.h" -#include "list.h" -#include "utils.h" +#include "log.hpp" #include "kernel/pci.h" #include "kernel/vfio.h" -#include -#include -#include -#include -#include -#include -#include - #include "fpga/ip.hpp" #include "fpga/card.hpp" -#include "log.hpp" - namespace villas { namespace fpga { @@ -101,7 +88,6 @@ fpga::PCIeCardFactory::make(json_t *json, struct pci* pci, ::vfio_container* vc) } - // TODO: currently fails, fix and remove comment if(not card->init()) { logger->warn("Cannot start FPGA card {}", card_name); continue; @@ -154,13 +140,16 @@ PCIeCard::lookupIp(const Vlnv& vlnv) const } -bool fpga::PCIeCard::init() +bool +fpga::PCIeCard::init() { int ret; struct pci_device *pdev; auto logger = getLogger(); + logger->info("Initializing FPGA card {}", name); + /* Search for FPGA card */ pdev = pci_lookup_device(pci, &filter); if (!pdev) { @@ -176,12 +165,31 @@ bool fpga::PCIeCard::init() } /* Map PCIe BAR */ - map = (char*) vfio_map_region(&vfio_device, VFIO_PCI_BAR0_REGION_INDEX); - if (map == MAP_FAILED) { + const void* bar0_mapped = vfio_map_region(&vfio_device, VFIO_PCI_BAR0_REGION_INDEX); + if (bar0_mapped == MAP_FAILED) { logger->error("Failed to mmap() BAR0"); return false; } + + /* Link mapped BAR0 to global memory graph */ + + // get the address space of the current application + auto villasAddrSpace = MemoryManager::get().getProcessAddressSpace(); + + // create a new address space for this FPGA card + this->addrSpaceId = MemoryManager::get().getOrCreateAddressSpace(name); + + // determine size of BAR0 region + const size_t bar0_size = vfio_region_size(&vfio_device, + VFIO_PCI_BAR0_REGION_INDEX); + + // create a mapping from our address space to the FPGA card via vfio + MemoryManager::get().createMapping(reinterpret_cast(bar0_mapped), + 0, bar0_size, "VFIO_map", + villasAddrSpace, this->addrSpaceId); + + /* Enable memory access and PCI bus mastering for DMA */ ret = vfio_pci_enable(&vfio_device); if (ret) {