Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion CODEOWNERS
Original file line number Diff line number Diff line change
Expand Up @@ -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
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -106,25 +106,25 @@ class Device {
std::ios_base::out) const;
};

class DeviceList : public std::list<std::shared_ptr<Device>> {
class PciDeviceList : public std::list<std::shared_ptr<PciDevice>> {
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
2 changes: 1 addition & 1 deletion common/include/villas/kernel/vfio_container.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ class Container {
std::shared_ptr<Group> getOrAttachGroup(int index);

std::shared_ptr<Device> attachDevice(const std::string &name, int groupIndex);
std::shared_ptr<Device> attachDevice(pci::Device &pdev);
std::shared_ptr<Device> attachDevice(devices::PciDevice &pdev);

// Map VM to an IOVA, which is accessible by devices in the container
//
Expand Down
6 changes: 3 additions & 3 deletions common/include/villas/kernel/vfio_device.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
#include <linux/vfio.h>
#include <sys/mman.h>

#include <villas/kernel/pci.hpp>
#include <villas/kernel/devices/pci_device.hpp>
#include <villas/log.hpp>

namespace villas {
Expand All @@ -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
Expand Down Expand Up @@ -89,7 +89,7 @@ class Device {
std::vector<void *> mappings;

// libpci handle of the device
const kernel::pci::Device *pci_device;
const kernel::devices::PciDevice *pci_device;

Logger log;
};
Expand Down
2 changes: 1 addition & 1 deletion common/include/villas/kernel/vfio_group.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ class Group {
std::shared_ptr<Device> attachDevice(std::shared_ptr<Device> device);
std::shared_ptr<Device>
attachDevice(const std::string &name,
const kernel::pci::Device *pci_device = nullptr);
const kernel::devices::PciDevice *pci_device = nullptr);

bool checkStatus();
void dump();
Expand Down
2 changes: 1 addition & 1 deletion common/lib/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,23 +18,23 @@

#include <villas/config.hpp>
#include <villas/exceptions.hpp>
#include <villas/kernel/pci.hpp>
#include <villas/kernel/devices/pci_device.hpp>
#include <villas/utils.hpp>

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;
Expand Down Expand Up @@ -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<Device>(id, slot));
emplace_back(std::make_shared<PciDevice>(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;
}
Expand Down Expand Up @@ -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<Region> Device::getRegions() const {
std::list<Region> PciDevice::getRegions() const {
FILE *f;
char sysfs[1024];

Expand Down Expand Up @@ -311,7 +312,7 @@ std::list<Region> 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));
Expand All @@ -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];

Expand Down Expand Up @@ -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;
Expand All @@ -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);

Expand All @@ -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);

Expand All @@ -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<char *>(&addr), sizeof(addr));
}

int Device::getIommuGroup() const {
int PciDevice::getIommuGroup() const {
int ret;
char *group;

Expand All @@ -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;

Expand Down
2 changes: 1 addition & 1 deletion common/lib/kernel/vfio_container.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@ std::shared_ptr<Device> Container::attachDevice(const std::string &name,
return device;
}

std::shared_ptr<Device> Container::attachDevice(pci::Device &pdev) {
std::shared_ptr<Device> Container::attachDevice(devices::PciDevice &pdev) {
int ret;
char name[32], iommu_state[4];
static constexpr const char *kernelDriver = "vfio-pci";
Expand Down
2 changes: 1 addition & 1 deletion common/lib/kernel/vfio_device.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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")) {
Expand Down
2 changes: 1 addition & 1 deletion common/lib/kernel/vfio_group.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ std::shared_ptr<Device> Group::attachDevice(std::shared_ptr<Device> device) {

std::shared_ptr<Device>
Group::attachDevice(const std::string &name,
const kernel::pci::Device *pci_device) {
const kernel::devices::PciDevice *pci_device) {
auto device = std::make_shared<Device>(name, fd, pci_device);
return attachDevice(device);
}
Expand Down
2 changes: 1 addition & 1 deletion fpga/gpu/src/gpu.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
#include <memory>

#include <villas/gpu.hpp>
#include <villas/kernel/pci.hpp>
#include <villas/kernel/devices/pci_device.hpp>
#include <villas/log.hpp>
#include <villas/memory_manager.hpp>

Expand Down
4 changes: 2 additions & 2 deletions fpga/include/villas/fpga/pcie_card.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
#include <villas/memory.hpp>
#include <villas/plugin.hpp>

#include <villas/kernel/pci.hpp>
#include <villas/kernel/devices/pci_device.hpp>
#include <villas/kernel/vfio_container.hpp>

#include <villas/fpga/card.hpp>
Expand Down Expand Up @@ -55,7 +55,7 @@ class PCIeCard : public Card {
bool doReset; // Reset VILLASfpga during startup?
int affinity; // Affinity for MSI interrupts

std::shared_ptr<kernel::pci::Device> pdev; // PCI device handle
std::shared_ptr<kernel::devices::PciDevice> pdev; // PCI device handle

protected:
Logger getLogger() const { return villas::Log::get(name); }
Expand Down
2 changes: 1 addition & 1 deletion fpga/lib/dma.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@

using namespace villas;

static std::shared_ptr<kernel::pci::DeviceList> pciDevices;
static std::shared_ptr<kernel::devices::PciDeviceList> pciDevices;
static auto logger = villas::Log::get("villasfpga_dma");

struct villasfpga_handle_t {
Expand Down
15 changes: 8 additions & 7 deletions fpga/lib/pcie_card.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
#include <villas/fpga/node.hpp>
#include <villas/fpga/pcie_card.hpp>
#include <villas/kernel/kernel.hpp>
#include <villas/kernel/pci.hpp>
#include <villas/kernel/devices/pci_device.hpp>
#include <villas/kernel/vfio_container.hpp>
#include <villas/memory.hpp>

Expand All @@ -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<PCIeCard>
PCIeCardFactory::make(json_t *json_card, std::string card_name,
Expand Down Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion fpga/src/villas-fpga-ctrl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@

using namespace villas;

static std::shared_ptr<kernel::pci::DeviceList> pciDevices;
static std::shared_ptr<kernel::devices::PciDeviceList> pciDevices;
static auto logger = villas::Log::get("ctrl");

void writeToDmaFromStdIn(std::shared_ptr<villas::fpga::ip::Dma> dma) {
Expand Down
2 changes: 1 addition & 1 deletion fpga/src/villas-fpga-pipe.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@

using namespace villas;

static std::shared_ptr<kernel::pci::DeviceList> pciDevices;
static std::shared_ptr<kernel::devices::PciDeviceList> pciDevices;
static auto logger = villas::Log::get("streamer");

int main(int argc, char *argv[]) {
Expand Down
Loading