diff --git a/CODEOWNERS b/CODEOWNERS index ef7dc9ef1..2dec02cc1 100644 --- a/CODEOWNERS +++ b/CODEOWNERS @@ -35,5 +35,5 @@ fpga @n-eiling # VFIO related files /common/lib/kernel/pci.cpp @n-eiling /common/lib/kernel/vfi_*.cpp @n-eiling -/common/include/villas/kernel/pci.hpp @n-eiling +/common/include/villas/kernel/devices/* @n-eiling /common/include/villas/kernel/vfio_*.hpp @n-eiling diff --git a/common/include/villas/kernel/pci.hpp b/common/include/villas/kernel/devices/pci_device.hpp similarity index 78% rename from common/include/villas/kernel/pci.hpp rename to common/include/villas/kernel/devices/pci_device.hpp index 8d792d7cc..65794644b 100644 --- a/common/include/villas/kernel/pci.hpp +++ b/common/include/villas/kernel/devices/pci_device.hpp @@ -17,7 +17,7 @@ namespace villas { namespace kernel { -namespace pci { +namespace devices { #define PCI_SLOT(devfn) (((devfn) >> 3) & 0x1f) #define PCI_FUNC(devfn) ((devfn) & 0x07) @@ -58,15 +58,15 @@ struct Region { unsigned long long flags; }; -class Device { +class PciDevice { public: - Device(Id i, Slot s) : id(i), slot(s), log(Log::get("kernel:pci")) {} + PciDevice(Id i, Slot s) : id(i), slot(s), log(Log::get("kernel:pci")) {} - Device(Id i) : id(i), log(Log::get("kernel:pci")) {} + PciDevice(Id i) : id(i), log(Log::get("kernel:pci")) {} - Device(Slot s) : slot(s), log(Log::get("kernel:pci")) {} + PciDevice(Slot s) : slot(s), log(Log::get("kernel:pci")) {} - bool operator==(const Device &other); + bool operator==(const PciDevice &other); // Get currently loaded driver for device std::string getDriver() const; @@ -106,25 +106,25 @@ class Device { std::ios_base::out) const; }; -class DeviceList : public std::list> { +class PciDeviceList : public std::list> { private: // Initialize Linux PCI handle. // // This search for all available PCI devices under /sys/bus/pci - DeviceList(); - DeviceList &operator=(const DeviceList &); - static DeviceList *instance; + PciDeviceList(); + PciDeviceList &operator=(const PciDeviceList &); + static PciDeviceList *instance; public: - static DeviceList *getInstance(); + static PciDeviceList *getInstance(); - DeviceList::value_type lookupDevice(const Slot &s); + PciDeviceList::value_type lookupDevice(const Slot &s); - DeviceList::value_type lookupDevice(const Id &i); + PciDeviceList::value_type lookupDevice(const Id &i); - DeviceList::value_type lookupDevice(const Device &f); + PciDeviceList::value_type lookupDevice(const PciDevice &f); }; -} // namespace pci +} // namespace devices } // namespace kernel } // namespace villas diff --git a/common/include/villas/kernel/vfio_container.hpp b/common/include/villas/kernel/vfio_container.hpp index 2e0ffaf97..2165092da 100644 --- a/common/include/villas/kernel/vfio_container.hpp +++ b/common/include/villas/kernel/vfio_container.hpp @@ -49,7 +49,7 @@ class Container { std::shared_ptr getOrAttachGroup(int index); std::shared_ptr attachDevice(const std::string &name, int groupIndex); - std::shared_ptr attachDevice(pci::Device &pdev); + std::shared_ptr attachDevice(devices::PciDevice &pdev); // Map VM to an IOVA, which is accessible by devices in the container // diff --git a/common/include/villas/kernel/vfio_device.hpp b/common/include/villas/kernel/vfio_device.hpp index 9f8c51a7c..fe9f8e3ef 100644 --- a/common/include/villas/kernel/vfio_device.hpp +++ b/common/include/villas/kernel/vfio_device.hpp @@ -19,7 +19,7 @@ #include #include -#include +#include #include namespace villas { @@ -29,7 +29,7 @@ namespace vfio { class Device { public: Device(const std::string &name, int groupFileDescriptor, - const kernel::pci::Device *pci_device = nullptr); + const kernel::devices::PciDevice *pci_device = nullptr); ~Device(); // No copying allowed because we manage the vfio state in constructor and destructors @@ -89,7 +89,7 @@ class Device { std::vector mappings; // libpci handle of the device - const kernel::pci::Device *pci_device; + const kernel::devices::PciDevice *pci_device; Logger log; }; diff --git a/common/include/villas/kernel/vfio_group.hpp b/common/include/villas/kernel/vfio_group.hpp index 7c4fca580..5f990c46c 100644 --- a/common/include/villas/kernel/vfio_group.hpp +++ b/common/include/villas/kernel/vfio_group.hpp @@ -48,7 +48,7 @@ class Group { std::shared_ptr attachDevice(std::shared_ptr device); std::shared_ptr attachDevice(const std::string &name, - const kernel::pci::Device *pci_device = nullptr); + const kernel::devices::PciDevice *pci_device = nullptr); bool checkStatus(); void dump(); diff --git a/common/lib/CMakeLists.txt b/common/lib/CMakeLists.txt index 7927fc0f4..116d786e6 100644 --- a/common/lib/CMakeLists.txt +++ b/common/lib/CMakeLists.txt @@ -39,7 +39,7 @@ endif() if(CMAKE_SYSTEM_NAME STREQUAL Linux) target_sources(villas-common PRIVATE - kernel/pci.cpp + kernel/devices/pci_device.cpp kernel/vfio_device.cpp kernel/vfio_group.cpp kernel/vfio_container.cpp diff --git a/common/lib/kernel/pci.cpp b/common/lib/kernel/devices/pci_device.cpp similarity index 88% rename from common/lib/kernel/pci.cpp rename to common/lib/kernel/devices/pci_device.cpp index c999f77e2..8d700ebfc 100644 --- a/common/lib/kernel/pci.cpp +++ b/common/lib/kernel/devices/pci_device.cpp @@ -18,23 +18,23 @@ #include #include -#include +#include #include -using namespace villas::kernel::pci; +using namespace villas::kernel::devices; #define PCI_BASE_ADDRESS_N(n) (PCI_BASE_ADDRESS_0 + sizeof(uint32_t) * (n)) -DeviceList *DeviceList::instance = nullptr; +PciDeviceList *PciDeviceList::instance = nullptr; -DeviceList *DeviceList::getInstance() { +PciDeviceList *PciDeviceList::getInstance() { if (instance == nullptr) { - instance = new DeviceList(); + instance = new PciDeviceList(); } return instance; }; -DeviceList::DeviceList() { +PciDeviceList::PciDeviceList() { struct dirent *e; DIR *dp; FILE *f; @@ -82,27 +82,28 @@ DeviceList::DeviceList() { if (ret != 4) throw RuntimeError("Failed to parse PCI slot number: {}", e->d_name); - emplace_back(std::make_shared(id, slot)); + emplace_back(std::make_shared(id, slot)); } closedir(dp); } -DeviceList::value_type DeviceList::lookupDevice(const Slot &s) { - return *std::find_if(begin(), end(), [s](const DeviceList::value_type &d) { +PciDeviceList::value_type PciDeviceList::lookupDevice(const Slot &s) { + return *std::find_if(begin(), end(), [s](const PciDeviceList::value_type &d) { return d->slot == s; }); } -DeviceList::value_type DeviceList::lookupDevice(const Id &i) { - return *std::find_if(begin(), end(), [i](const DeviceList::value_type &d) { +PciDeviceList::value_type PciDeviceList::lookupDevice(const Id &i) { + return *std::find_if(begin(), end(), [i](const PciDeviceList::value_type &d) { return d->id == i; }); } -DeviceList::value_type DeviceList::lookupDevice(const Device &d) { - auto dev = std::find_if( - begin(), end(), [d](const DeviceList::value_type &e) { return *e == d; }); +PciDeviceList::value_type PciDeviceList::lookupDevice(const PciDevice &d) { + auto dev = + std::find_if(begin(), end(), + [d](const PciDeviceList::value_type &e) { return *e == d; }); return dev == end() ? value_type() : *dev; } @@ -247,11 +248,11 @@ bool Slot::operator==(const Slot &s) { return true; } -bool Device::operator==(const Device &f) { +bool PciDevice::operator==(const PciDevice &f) { return id == f.id && slot == f.slot; } -std::list Device::getRegions() const { +std::list PciDevice::getRegions() const { FILE *f; char sysfs[1024]; @@ -311,7 +312,7 @@ std::list Device::getRegions() const { return regions; } -std::string Device::getDriver() const { +std::string PciDevice::getDriver() const { int ret; char sysfs[1024], syml[1024]; memset(syml, 0, sizeof(syml)); @@ -331,7 +332,7 @@ std::string Device::getDriver() const { return basename(syml); } -bool Device::attachDriver(const std::string &driver) const { +bool PciDevice::attachDriver(const std::string &driver) const { FILE *f; char fn[1024]; @@ -363,7 +364,7 @@ bool Device::attachDriver(const std::string &driver) const { return true; } -uint32_t Device::readHostBar(unsigned barNum) const { +uint32_t PciDevice::readHostBar(unsigned barNum) const { auto file = openSysFs("resource", std::ios_base::in); std::string line; @@ -389,7 +390,7 @@ uint32_t Device::readHostBar(unsigned barNum) const { return start; } -void Device::rewriteBar(unsigned barNum) { +void PciDevice::rewriteBar(unsigned barNum) { auto hostBar = readHostBar(barNum); auto configBar = readBar(barNum); @@ -405,7 +406,7 @@ void Device::rewriteBar(unsigned barNum) { writeBar(hostBar, barNum); } -uint32_t Device::readBar(unsigned barNum) const { +uint32_t PciDevice::readBar(unsigned barNum) const { uint32_t addr; auto file = openSysFs("config", std::ios_base::in); @@ -415,14 +416,14 @@ uint32_t Device::readBar(unsigned barNum) const { return addr; } -void Device::writeBar(uint32_t addr, unsigned barNum) { +void PciDevice::writeBar(uint32_t addr, unsigned barNum) { auto file = openSysFs("config", std::ios_base::out); file.seekp(PCI_BASE_ADDRESS_N(barNum)); file.write(reinterpret_cast(&addr), sizeof(addr)); } -int Device::getIommuGroup() const { +int PciDevice::getIommuGroup() const { int ret; char *group; @@ -443,7 +444,7 @@ int Device::getIommuGroup() const { return atoi(group); } -std::fstream Device::openSysFs(const std::string &subPath, +std::fstream PciDevice::openSysFs(const std::string &subPath, std::ios_base::openmode mode) const { std::fstream file; diff --git a/common/lib/kernel/vfio_container.cpp b/common/lib/kernel/vfio_container.cpp index 7b67035f1..76923aa08 100644 --- a/common/lib/kernel/vfio_container.cpp +++ b/common/lib/kernel/vfio_container.cpp @@ -187,7 +187,7 @@ std::shared_ptr Container::attachDevice(const std::string &name, return device; } -std::shared_ptr Container::attachDevice(pci::Device &pdev) { +std::shared_ptr Container::attachDevice(devices::PciDevice &pdev) { int ret; char name[32], iommu_state[4]; static constexpr const char *kernelDriver = "vfio-pci"; diff --git a/common/lib/kernel/vfio_device.cpp b/common/lib/kernel/vfio_device.cpp index 160cff71d..ff70f3cb7 100644 --- a/common/lib/kernel/vfio_device.cpp +++ b/common/lib/kernel/vfio_device.cpp @@ -53,7 +53,7 @@ static const char *vfio_pci_irq_names[] = { }; Device::Device(const std::string &name, int groupFileDescriptor, - const kernel::pci::Device *pci_device) + const kernel::devices::PciDevice *pci_device) : name(name), fd(-1), attachedToGroup(false), groupFd(groupFileDescriptor), info(), irqs(), regions(), mappings(), pci_device(pci_device), log(Log::get("kernel:vfio:device")) { diff --git a/common/lib/kernel/vfio_group.cpp b/common/lib/kernel/vfio_group.cpp index 56591bb9f..512460c20 100644 --- a/common/lib/kernel/vfio_group.cpp +++ b/common/lib/kernel/vfio_group.cpp @@ -68,7 +68,7 @@ std::shared_ptr Group::attachDevice(std::shared_ptr device) { std::shared_ptr Group::attachDevice(const std::string &name, - const kernel::pci::Device *pci_device) { + const kernel::devices::PciDevice *pci_device) { auto device = std::make_shared(name, fd, pci_device); return attachDevice(device); } diff --git a/fpga/gpu/src/gpu.cpp b/fpga/gpu/src/gpu.cpp index f68d69265..f80a9521e 100644 --- a/fpga/gpu/src/gpu.cpp +++ b/fpga/gpu/src/gpu.cpp @@ -13,7 +13,7 @@ #include #include -#include +#include #include #include diff --git a/fpga/include/villas/fpga/pcie_card.hpp b/fpga/include/villas/fpga/pcie_card.hpp index f5368d399..aff1b23fb 100644 --- a/fpga/include/villas/fpga/pcie_card.hpp +++ b/fpga/include/villas/fpga/pcie_card.hpp @@ -19,7 +19,7 @@ #include #include -#include +#include #include #include @@ -55,7 +55,7 @@ class PCIeCard : public Card { bool doReset; // Reset VILLASfpga during startup? int affinity; // Affinity for MSI interrupts - std::shared_ptr pdev; // PCI device handle + std::shared_ptr pdev; // PCI device handle protected: Logger getLogger() const { return villas::Log::get(name); } diff --git a/fpga/lib/dma.cpp b/fpga/lib/dma.cpp index 309ac8d60..7ce10b2b5 100644 --- a/fpga/lib/dma.cpp +++ b/fpga/lib/dma.cpp @@ -21,7 +21,7 @@ using namespace villas; -static std::shared_ptr pciDevices; +static std::shared_ptr pciDevices; static auto logger = villas::Log::get("villasfpga_dma"); struct villasfpga_handle_t { diff --git a/fpga/lib/pcie_card.cpp b/fpga/lib/pcie_card.cpp index e2669820c..7fc6ceb0a 100644 --- a/fpga/lib/pcie_card.cpp +++ b/fpga/lib/pcie_card.cpp @@ -14,7 +14,7 @@ #include #include #include -#include +#include #include #include @@ -24,8 +24,8 @@ using namespace villas::fpga; // Instantiate factory to register static PCIeCardFactory PCIeCardFactoryInstance; -static const kernel::pci::Device - defaultFilter((kernel::pci::Id(FPGA_PCI_VID_XILINX, FPGA_PCI_PID_VFPGA))); +static const kernel::devices::PciDevice defaultFilter( + (kernel::devices::Id(FPGA_PCI_VID_XILINX, FPGA_PCI_PID_VFPGA))); std::shared_ptr PCIeCardFactory::make(json_t *json_card, std::string card_name, @@ -63,15 +63,16 @@ PCIeCardFactory::make(json_t *json_card, std::string card_name, card->doReset = do_reset != 0; card->polling = (polling != 0); - kernel::pci::Device filter = defaultFilter; + kernel::devices::PciDevice filter = defaultFilter; if (pci_id) - filter.id = kernel::pci::Id(pci_id); + filter.id = kernel::devices::Id(pci_id); if (pci_slot) - filter.slot = kernel::pci::Slot(pci_slot); + filter.slot = kernel::devices::Slot(pci_slot); // Search for FPGA card - card->pdev = kernel::pci::DeviceList::getInstance()->lookupDevice(filter); + card->pdev = + kernel::devices::PciDeviceList::getInstance()->lookupDevice(filter); if (!card->pdev) { logger->warn("Failed to find PCI device"); return nullptr; diff --git a/fpga/src/villas-fpga-ctrl.cpp b/fpga/src/villas-fpga-ctrl.cpp index 8cdd424d3..6232ac782 100644 --- a/fpga/src/villas-fpga-ctrl.cpp +++ b/fpga/src/villas-fpga-ctrl.cpp @@ -34,7 +34,7 @@ using namespace villas; -static std::shared_ptr pciDevices; +static std::shared_ptr pciDevices; static auto logger = villas::Log::get("ctrl"); void writeToDmaFromStdIn(std::shared_ptr dma) { diff --git a/fpga/src/villas-fpga-pipe.cpp b/fpga/src/villas-fpga-pipe.cpp index 66841ea61..b059501c0 100644 --- a/fpga/src/villas-fpga-pipe.cpp +++ b/fpga/src/villas-fpga-pipe.cpp @@ -27,7 +27,7 @@ using namespace villas; -static std::shared_ptr pciDevices; +static std::shared_ptr pciDevices; static auto logger = villas::Log::get("streamer"); int main(int argc, char *argv[]) { diff --git a/fpga/tests/unit/fpga.cpp b/fpga/tests/unit/fpga.cpp index d6ad32478..587cc0264 100644 --- a/fpga/tests/unit/fpga.cpp +++ b/fpga/tests/unit/fpga.cpp @@ -27,7 +27,7 @@ using namespace villas; -static kernel::pci::DeviceList *pciDevices; +static kernel::devices::PciDeviceList *pciDevices; FpgaState state; @@ -40,7 +40,7 @@ static void init() { plugin::registry->dump(); - pciDevices = kernel::pci::DeviceList::getInstance(); + pciDevices = kernel::devices::PciDeviceList::getInstance(); auto vfioContainer = std::make_shared();