2020-06-14 21:52:29 +02:00
|
|
|
/** Linux PCI helpers
|
|
|
|
*
|
|
|
|
* @file
|
2022-12-14 17:39:07 +01:00
|
|
|
* @author Steffen Vogel <post@steffenvogel.de>
|
2022-03-15 09:05:42 -04:00
|
|
|
* @copyright 2014-2022, Institute for Automation of Complex Power Systems, EONERC
|
2022-05-19 17:40:10 +02:00
|
|
|
* @license Apache License 2.0
|
2020-06-14 21:52:29 +02:00
|
|
|
*********************************************************************************/
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include <cstddef>
|
|
|
|
#include <cstdint>
|
|
|
|
|
|
|
|
#include <list>
|
|
|
|
|
|
|
|
namespace villas {
|
|
|
|
namespace kernel {
|
|
|
|
namespace pci {
|
|
|
|
|
|
|
|
#define PCI_SLOT(devfn) (((devfn) >> 3) & 0x1f)
|
|
|
|
#define PCI_FUNC(devfn) ((devfn) & 0x07)
|
|
|
|
|
|
|
|
class Id {
|
|
|
|
public:
|
|
|
|
Id(const std::string &str);
|
|
|
|
|
|
|
|
Id(int vid = 0, int did = 0, int cc = 0) :
|
|
|
|
vendor(vid),
|
|
|
|
device(did),
|
|
|
|
class_code(cc)
|
|
|
|
{ }
|
|
|
|
|
|
|
|
bool
|
|
|
|
operator==(const Id &i);
|
|
|
|
|
2020-09-11 16:01:16 +02:00
|
|
|
unsigned int vendor;
|
|
|
|
unsigned int device;
|
|
|
|
unsigned int class_code;
|
2020-06-14 21:52:29 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
class Slot {
|
|
|
|
public:
|
|
|
|
Slot(const std::string &str);
|
|
|
|
|
|
|
|
Slot(int dom = 0, int b = 0, int dev = 0, int fcn = 0) :
|
|
|
|
domain(dom),
|
|
|
|
bus(b),
|
|
|
|
device(dev),
|
|
|
|
function(fcn)
|
|
|
|
{ }
|
|
|
|
|
|
|
|
bool
|
|
|
|
operator==(const Slot &s);
|
|
|
|
|
2020-09-11 16:01:16 +02:00
|
|
|
unsigned int domain;
|
|
|
|
unsigned int bus;
|
|
|
|
unsigned int device;
|
|
|
|
unsigned int function;
|
2020-06-14 21:52:29 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
struct Region {
|
|
|
|
int num;
|
|
|
|
uintptr_t start;
|
|
|
|
uintptr_t end;
|
|
|
|
unsigned long long flags;
|
|
|
|
};
|
|
|
|
|
|
|
|
class Device {
|
|
|
|
public:
|
|
|
|
Device(Id i, Slot s) :
|
|
|
|
id(i),
|
|
|
|
slot(s)
|
|
|
|
{ }
|
|
|
|
|
2020-06-15 21:05:51 +02:00
|
|
|
Device(Id i) :
|
|
|
|
id(i)
|
|
|
|
{ }
|
2020-06-14 21:52:29 +02:00
|
|
|
|
2020-06-15 21:05:51 +02:00
|
|
|
Device(Slot s) :
|
|
|
|
slot(s)
|
|
|
|
{ }
|
2020-06-14 21:52:29 +02:00
|
|
|
|
|
|
|
bool
|
2020-06-15 21:05:51 +02:00
|
|
|
operator==(const Device &other);
|
2020-06-14 21:52:29 +02:00
|
|
|
|
|
|
|
/** Get currently loaded driver for device */
|
|
|
|
std::string
|
|
|
|
getDriver() const;
|
|
|
|
|
|
|
|
/** Bind a new LKM to the PCI device */
|
|
|
|
bool
|
|
|
|
attachDriver(const std::string &driver) const;
|
|
|
|
|
|
|
|
/** Return the IOMMU group of this PCI device or -1 if the device is not in a group. */
|
|
|
|
int
|
|
|
|
getIOMMUGroup() const;
|
|
|
|
|
|
|
|
std::list<Region>
|
|
|
|
getRegions() const;
|
|
|
|
|
|
|
|
Id id;
|
|
|
|
Slot slot;
|
|
|
|
};
|
|
|
|
|
2020-06-15 21:05:51 +02:00
|
|
|
class DeviceList : public std::list<std::shared_ptr<Device>> {
|
2020-06-14 21:52:29 +02:00
|
|
|
public:
|
|
|
|
/** Initialize Linux PCI handle.
|
|
|
|
*
|
|
|
|
* This search for all available PCI devices under /sys/bus/pci
|
|
|
|
*/
|
|
|
|
DeviceList();
|
|
|
|
|
2020-06-15 21:05:51 +02:00
|
|
|
DeviceList::value_type
|
|
|
|
lookupDevice(const Slot &s);
|
|
|
|
|
|
|
|
DeviceList::value_type
|
|
|
|
lookupDevice(const Id &i);
|
2020-06-14 21:52:29 +02:00
|
|
|
|
2020-06-15 21:05:51 +02:00
|
|
|
DeviceList::value_type
|
|
|
|
lookupDevice(const Device &f);
|
2020-06-14 21:52:29 +02:00
|
|
|
};
|
|
|
|
|
2022-12-02 17:16:44 +01:00
|
|
|
} // namespace pci
|
|
|
|
} // namespace kernel
|
|
|
|
} // namespace villas
|