Skip to content

Commit

Permalink
add reference to pcidrv and pcidev (#7736)
Browse files Browse the repository at this point in the history
* add reference to pcidrv and pcidev

Signed-off-by: Max Zhen <[email protected]>
  • Loading branch information
maxzhen authored Oct 9, 2023
1 parent 1aa60ca commit a99da2f
Show file tree
Hide file tree
Showing 6 changed files with 58 additions and 35 deletions.
25 changes: 15 additions & 10 deletions src/runtime_src/core/pcie/linux/device_linux.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -486,7 +486,7 @@ struct hotplug_offline
auto mgmt_dev = xrt_core::pci::get_dev(device->get_device_id(), false);

// Remove both user_pf and mgmt_pf
if (xrt_core::pci::shutdown(mgmt_dev, true, true))
if (xrt_core::pci::shutdown(mgmt_dev.get(), true, true))
throw xrt_core::query::sysfs_error("Hotplug offline failed");

return true;
Expand Down Expand Up @@ -1391,6 +1391,12 @@ lookup_query(query::key_type query_key) const
device_linux::
device_linux(handle_type device_handle, id_type device_id, bool user)
: shim<device_pcie>(device_handle, device_id, user)
, m_pcidev(pci::get_dev(device_id, user))
{
}

device_linux::
~device_linux()
{
}

Expand Down Expand Up @@ -1421,15 +1427,15 @@ void
device_linux::
read(uint64_t offset, void* buf, uint64_t len) const
{
if (auto err = xrt_core::pci::get_dev(get_device_id(), false)->pcieBarRead(offset, buf, len))
if (auto err = get_dev()->pcieBarRead(offset, buf, len))
throw error(err, "read failed");
}

void
device_linux::
write(uint64_t offset, const void* buf, uint64_t len) const
{
if (auto err = xrt_core::pci::get_dev(get_device_id(), false)->pcieBarWrite(offset, buf, len))
if (auto err = get_dev()->pcieBarWrite(offset, buf, len))
throw error(err, "write failed");
}

Expand All @@ -1438,8 +1444,7 @@ device_linux::
reset(query::reset_type& key) const
{
std::string err;
xrt_core::pci::get_dev(get_device_id(), false)->sysfs_put(
key.get_subdev(), key.get_entry(), err, key.get_value());
get_dev()->sysfs_put(key.get_subdev(), key.get_entry(), err, key.get_value());
if (!err.empty())
throw error("reset failed");
}
Expand All @@ -1448,14 +1453,14 @@ int
device_linux::
open(const std::string& subdev, int flag) const
{
return xrt_core::pci::get_dev(get_device_id(), false)->open(subdev, flag);
return get_dev()->open(subdev, flag);
}

void
device_linux::
close(int dev_handle) const
{
xrt_core::pci::get_dev(get_device_id(), false)->close(dev_handle);
get_dev()->close(dev_handle);
}

void
Expand All @@ -1471,7 +1476,7 @@ xclmgmt_load_xclbin(const char* buffer) const {
try {
xrt_core::scope_value_guard<int, std::function<void()>> fd = file_open("", O_RDWR);
xclmgmt_ioc_bitstream_axlf obj = { reinterpret_cast<axlf *>( const_cast<char*>(buffer) ) };
ret = xrt_core::pci::get_dev(get_device_id(), false)->ioctl(fd.get(), XCLMGMT_IOCICAPDOWNLOAD_AXLF, &obj);
ret = get_dev()->ioctl(fd.get(), XCLMGMT_IOCICAPDOWNLOAD_AXLF, &obj);
} catch (const std::exception& e) {
xrt_core::send_exception_message(e.what(), "Failed to open device");
}
Expand All @@ -1484,7 +1489,7 @@ xclmgmt_load_xclbin(const char* buffer) const {
void
device_linux::
device_shutdown() const {
auto mgmt_dev = xrt_core::pci::get_dev(get_device_id(), false);
auto mgmt_dev = get_dev();
// hot reset pcie device
if (xrt_core::pci::shutdown(mgmt_dev))
throw xrt_core::error("Hot resetting pci device failed.");
Expand All @@ -1493,7 +1498,7 @@ device_shutdown() const {
void
device_linux::
device_online() const {
auto mgmt_dev = xrt_core::pci::get_dev(get_device_id(), false);
auto mgmt_dev = get_dev();
auto peer_dev = mgmt_dev->lookup_peer_dev();
std::string errmsg;

Expand Down
16 changes: 15 additions & 1 deletion src/runtime_src/core/pcie/linux/device_linux.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,22 @@
#include "core/common/shim/buffer_handle.h"
#include "core/common/shim/hwctx_handle.h"
#include "core/pcie/common/device_pcie.h"
#include "core/pcie/linux/pcidev.h"

namespace xrt_core {

// Forward declaration
namespace pci {
class dev;
}

// concrete class derives from device_pcie, but mixes in
// shim layer functions for access through base class
class device_linux : public shim<device_pcie>
{
public:
device_linux(handle_type device_handle, id_type device_id, bool user);
~device_linux();

// query functions
virtual void read_dma_stats(boost::property_tree::ptree& pt) const;
Expand Down Expand Up @@ -86,9 +93,16 @@ class device_linux : public shim<device_pcie>
{
return xrt::shim_int::alloc_bo(get_device_handle(), userptr, size, xcl_bo_flags{flags}.flags);
}
////////////////////////////////////////////////////////////////

protected:
pci::dev*
get_dev() const
{
return m_pcidev.get();
}

private:
std::shared_ptr<pci::dev> m_pcidev;
// Private look up function for concrete query::request
virtual const query::request&
lookup_query(query::key_type query_key) const override;
Expand Down
29 changes: 16 additions & 13 deletions src/runtime_src/core/pcie/linux/pcidev.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -420,7 +420,7 @@ get_subdev_path(const std::string& subdev, uint idx) const
if (subdev.empty()) {
std::string instStr = std::to_string(m_instance);
std::string prefixStr = "/dev/";
prefixStr += m_driver.dev_node_dir() + "/" + m_driver.dev_node_prefix();
prefixStr += m_driver->dev_node_dir() + "/" + m_driver->dev_node_prefix();
return prefixStr + instStr;
}

Expand Down Expand Up @@ -455,25 +455,28 @@ open(const std::string& subdev, int flag) const
}

dev::
dev(const drv& driver, const std::string& sysfs) : m_sysfs_name(sysfs), m_driver(driver)
dev(std::shared_ptr<const drv> driver, std::string sysfs)
: m_sysfs_name(std::move(sysfs))
, m_driver(std::move(driver))
{
std::string err;

if(sscanf(sysfs.c_str(), "%hx:%hx:%hx.%hx", &m_domain, &m_bus, &m_dev, &m_func) < 4)
throw std::invalid_argument(sysfs + " is not valid BDF");
if(sscanf(m_sysfs_name.c_str(), "%hx:%hx:%hx.%hx", &m_domain, &m_bus, &m_dev, &m_func) < 4)
throw std::invalid_argument(m_sysfs_name + " is not valid BDF");

m_is_mgmt = !driver.is_user();
m_is_mgmt = !m_driver->is_user();

if (m_is_mgmt) {
sysfs_get("", "instance", err, m_instance, static_cast<uint32_t>(INVALID_ID));
} else {
}
else {
m_instance = get_render_value(
sysfs::dev_root + sysfs + "/" + driver.sysfs_dev_node_dir(),
driver.dev_node_prefix());
sysfs::dev_root + m_sysfs_name + "/" + m_driver->sysfs_dev_node_dir(),
m_driver->dev_node_prefix());
}

sysfs_get<int>("", "userbar", err, m_user_bar, 0);
m_user_bar_size = bar_size(sysfs::dev_root + sysfs, m_user_bar);
m_user_bar_size = bar_size(sysfs::dev_root + m_sysfs_name, m_user_bar);
sysfs_get<bool>("", "ready", err, m_is_ready, false);
m_user_bar_map = reinterpret_cast<char *>(MAP_FAILED);
}
Expand All @@ -487,7 +490,7 @@ dev::

int
dev::
map_usr_bar()
map_usr_bar() const
{
std::lock_guard<std::mutex> l(m_lock);

Expand Down Expand Up @@ -522,7 +525,7 @@ close(int dev_handle) const

int
dev::
pcieBarRead(uint64_t offset, void* buf, uint64_t len)
pcieBarRead(uint64_t offset, void* buf, uint64_t len) const
{
if (m_user_bar_map == MAP_FAILED) {
int ret = map_usr_bar();
Expand All @@ -535,7 +538,7 @@ pcieBarRead(uint64_t offset, void* buf, uint64_t len)

int
dev::
pcieBarWrite(uint64_t offset, const void* buf, uint64_t len)
pcieBarWrite(uint64_t offset, const void* buf, uint64_t len) const
{
if (m_user_bar_map == MAP_FAILED) {
int ret = map_usr_bar();
Expand Down Expand Up @@ -720,7 +723,7 @@ get_runtime_active_kids(std::string &pci_bridge_path)
}

int
shutdown(std::shared_ptr<dev> mgmt_dev, bool remove_user, bool remove_mgmt)
shutdown(dev *mgmt_dev, bool remove_user, bool remove_mgmt)
{
if (!mgmt_dev->m_is_mgmt)
return -EINVAL;
Expand Down
19 changes: 10 additions & 9 deletions src/runtime_src/core/pcie/linux/pcidev.h
Original file line number Diff line number Diff line change
Expand Up @@ -80,10 +80,10 @@ class dev
bool m_is_mgmt = false;
bool m_is_ready = false;

dev(const drv& driver, const std::string& sysfs_name);
dev(std::shared_ptr<const drv> driver, std::string sysfs_name);

virtual
~dev();
dev() = delete;

virtual void
sysfs_get(const std::string& subdev, const std::string& entry,
Expand Down Expand Up @@ -135,10 +135,10 @@ class dev
get_subdev_path(const std::string& subdev, uint32_t idx) const;

virtual int
pcieBarRead(uint64_t offset, void* buf, uint64_t len);
pcieBarRead(uint64_t offset, void* buf, uint64_t len) const;

virtual int
pcieBarWrite(uint64_t offset, const void* buf, uint64_t len);
pcieBarWrite(uint64_t offset, const void* buf, uint64_t len) const;

virtual int
open(const std::string& subdev, int flag) const;
Expand Down Expand Up @@ -185,12 +185,13 @@ class dev

private:
int
map_usr_bar();
map_usr_bar() const;

std::mutex m_lock;
char *m_user_bar_map = reinterpret_cast<char *>(MAP_FAILED);
mutable std::mutex m_lock;
// Virtual address of memory mapped BAR0, mapped on first use, once mapped, never change.
mutable char *m_user_bar_map = reinterpret_cast<char *>(MAP_FAILED);

const drv& m_driver;
std::shared_ptr<const drv> m_driver;
};

size_t
Expand All @@ -206,7 +207,7 @@ std::shared_ptr<dev>
lookup_user_dev(std::shared_ptr<dev> mgmt_dev);

int
shutdown(std::shared_ptr<dev> mgmt_dev, bool remove_user = false, bool remove_mgmt = false);
shutdown(dev *mgmt_dev, bool remove_user = false, bool remove_mgmt = false);

int
check_p2p_config(const std::shared_ptr<dev>& dev, std::string &err);
Expand Down
2 changes: 1 addition & 1 deletion src/runtime_src/core/pcie/linux/pcidrv.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ std::shared_ptr<dev>
drv::
create_pcidev(const std::string& sysfs) const
{
return std::make_shared<dev>(*this, sysfs);
return std::make_shared<dev>(shared_from_this(), sysfs);
}

} } // namespace xrt_core :: pci
2 changes: 1 addition & 1 deletion src/runtime_src/core/pcie/linux/pcidrv.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

namespace xrt_core { namespace pci {

class drv
class drv : public std::enable_shared_from_this<drv>
{
public:
// Name of the driver as shown under /sys/bus/pci/drivers/
Expand Down

0 comments on commit a99da2f

Please sign in to comment.