-
Notifications
You must be signed in to change notification settings - Fork 15
FPGA: Device and Driver #808
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,33 @@ | ||
| /* Interface for Linux/Unix devices. | ||
| * | ||
| * Author: Pascal Bauer <pascal.bauer@rwth-aachen.de> | ||
| * | ||
| * SPDX-FileCopyrightText: 2023-2024 Pascal Bauer <pascal.bauer@rwth-aachen.de> | ||
| * SPDX-License-Identifier: Apache-2.0 | ||
| */ | ||
|
|
||
| #pragma once | ||
|
|
||
| #include <filesystem> | ||
| #include <optional> | ||
| #include <villas/kernel/devices/driver.hpp> | ||
|
|
||
| namespace villas { | ||
| namespace kernel { | ||
| namespace devices { | ||
|
|
||
| class Device { | ||
| public: | ||
| virtual ~Device(){}; | ||
|
|
||
| virtual std::optional<std::unique_ptr<Driver>> driver() const = 0; | ||
| virtual std::optional<int> iommu_group() const = 0; | ||
| virtual std::string name() const = 0; | ||
| virtual std::filesystem::path override_path() const = 0; | ||
| virtual std::filesystem::path path() const = 0; | ||
| virtual void probe() const = 0; | ||
| }; | ||
|
|
||
| } // namespace devices | ||
| } // namespace kernel | ||
| } // namespace villas | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,29 @@ | ||
| /* Interface for device drivers. OS/platform independend. | ||
| * Implemented for Linux/Unix drivers in linux_driver.hpp | ||
| * | ||
| * Author: Pascal Bauer <pascal.bauer@rwth-aachen.de> | ||
| * | ||
| * SPDX-FileCopyrightText: 2023-2024 Pascal Bauer <pascal.bauer@rwth-aachen.de> | ||
| * SPDX-License-Identifier: Apache-2.0 | ||
| */ | ||
|
|
||
| #pragma once | ||
|
|
||
| namespace villas { | ||
| namespace kernel { | ||
| namespace devices { | ||
|
|
||
| class Device; | ||
|
|
||
| class Driver { | ||
| public: | ||
| virtual void attach(const Device &device) const = 0; | ||
| virtual void bind(const Device &device) const = 0; | ||
| virtual std::string name() const = 0; | ||
| virtual void override(const Device &device) const = 0; | ||
| virtual void unbind(const Device &device) const = 0; | ||
| }; | ||
|
|
||
| } // namespace devices | ||
| } // namespace kernel | ||
| } // namespace villas |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,36 @@ | ||
| /* Linux/Unix device which represents an IP component of a FPGA. | ||
| * | ||
| * Author: Pascal Bauer <pascal.bauer@rwth-aachen.de> | ||
| * | ||
| * SPDX-FileCopyrightText: 2023-2024 Pascal Bauer <pascal.bauer@rwth-aachen.de> | ||
| * SPDX-License-Identifier: Apache-2.0 | ||
| */ | ||
|
|
||
| #pragma once | ||
|
|
||
| #include <filesystem> | ||
| #include <villas/kernel/devices/platform_device.hpp> | ||
|
|
||
| namespace villas { | ||
| namespace kernel { | ||
| namespace devices { | ||
|
|
||
| class IpDevice : public PlatformDevice { | ||
| public: | ||
IgnoreWarnings marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| static IpDevice from(const std::filesystem::path unsafe_path); | ||
| static bool is_path_valid(const std::filesystem::path unsafe_path); | ||
|
|
||
| private: | ||
| IpDevice() = delete; | ||
| IpDevice( | ||
| const std::filesystem::path valid_path) //! Dont allow unvalidated paths | ||
| : PlatformDevice(valid_path){}; | ||
|
|
||
| public: | ||
| size_t addr() const; | ||
| std::string ip_name() const; | ||
| }; | ||
|
|
||
| } // namespace devices | ||
| } // namespace kernel | ||
| } // namespace villas | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,52 @@ | ||
| /* Implementation of driver interface for Linux/Unix based operation system drivers. | ||
| * | ||
| * Author: Pascal Bauer <pascal.bauer@rwth-aachen.de> | ||
| * | ||
| * SPDX-FileCopyrightText: 2023-2024 Pascal Bauer <pascal.bauer@rwth-aachen.de> | ||
| * SPDX-License-Identifier: Apache-2.0 | ||
| */ | ||
|
|
||
| #pragma once | ||
|
|
||
| #include <filesystem> | ||
| #include <fstream> | ||
| #include <iostream> | ||
| #include <villas/kernel/devices/driver.hpp> | ||
|
|
||
| namespace villas { | ||
| namespace kernel { | ||
| namespace devices { | ||
|
|
||
| class LinuxDriver : public Driver { | ||
| private: | ||
| static constexpr char BIND_DEFAULT[] = "bind"; | ||
| static constexpr char UNBIND_DEFAULT[] = "unbind"; | ||
|
|
||
| public: | ||
| const std::filesystem::path path; | ||
|
|
||
| private: | ||
| const std::filesystem::path bind_path; | ||
| const std::filesystem::path unbind_path; | ||
|
|
||
| public: | ||
| LinuxDriver(const std::filesystem::path path) | ||
| : LinuxDriver(path, path / std::filesystem::path(BIND_DEFAULT), | ||
| path / std::filesystem::path(UNBIND_DEFAULT)){}; | ||
|
|
||
| LinuxDriver(const std::filesystem::path path, | ||
| const std::filesystem::path bind_path, | ||
| const std::filesystem::path unbind_path) | ||
| : path(path), bind_path(bind_path), unbind_path(unbind_path){}; | ||
|
|
||
| public: | ||
| void attach(const Device &device) const override; | ||
| void bind(const Device &device) const override; | ||
| std::string name() const override; | ||
| void override(const Device &device) const override; | ||
| void unbind(const Device &device) const override; | ||
| }; | ||
|
|
||
| } // namespace devices | ||
| } // namespace kernel | ||
| } // namespace villas |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,51 @@ | ||
| /* Platform bus based Linux/Unix device. | ||
| * | ||
| * Author: Pascal Bauer <pascal.bauer@rwth-aachen.de> | ||
| * | ||
| * SPDX-FileCopyrightText: 2023-2024 Pascal Bauer <pascal.bauer@rwth-aachen.de> | ||
| * SPDX-License-Identifier: Apache-2.0 | ||
| */ | ||
|
|
||
| #pragma once | ||
|
|
||
| #include <filesystem> | ||
| #include <villas/kernel/devices/device.hpp> | ||
| #include <villas/kernel/devices/driver.hpp> | ||
|
|
||
| namespace villas { | ||
| namespace kernel { | ||
| namespace devices { | ||
|
|
||
| class PlatformDevice : public Device { | ||
| private: | ||
| static constexpr char PROBE_DEFAULT[] = "/sys/bus/platform/drivers_probe"; | ||
| static constexpr char OVERRIDE_DEFAULT[] = "driver_override"; | ||
|
|
||
| private: | ||
| const std::filesystem::path m_path; | ||
| const std::filesystem::path m_probe_path; | ||
| const std::filesystem::path m_override_path; | ||
|
|
||
| public: | ||
| PlatformDevice(const std::filesystem::path path) | ||
| : PlatformDevice(path, std::filesystem::path(PROBE_DEFAULT), | ||
| path / std::filesystem::path(OVERRIDE_DEFAULT)){}; | ||
|
|
||
| PlatformDevice(const std::filesystem::path path, | ||
| const std::filesystem::path probe_path, | ||
| const std::filesystem::path override_path) | ||
| : m_path(path), m_probe_path(probe_path), | ||
| m_override_path(override_path){}; | ||
|
|
||
| // Implement device interface | ||
| std::optional<std::unique_ptr<Driver>> driver() const override; | ||
| std::optional<int> iommu_group() const override; | ||
| std::string name() const override; | ||
| std::filesystem::path override_path() const override; | ||
| std::filesystem::path path() const override; | ||
| void probe() const override; | ||
| }; | ||
|
|
||
| } // namespace devices | ||
| } // namespace kernel | ||
| } // namespace villas |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,54 @@ | ||
| /* Linux/Unix device which represents an IP component of a FPGA. | ||
| * | ||
| * Author: Pascal Bauer <pascal.bauer@rwth-aachen.de> | ||
| * | ||
| * SPDX-FileCopyrightText: 2023-2024 Pascal Bauer <pascal.bauer@rwth-aachen.de> | ||
| * SPDX-License-Identifier: Apache-2.0 | ||
| */ | ||
|
|
||
| #include <filesystem> | ||
| #include <regex> | ||
| #include <stdexcept> | ||
|
|
||
| #include <villas/exceptions.hpp> | ||
| #include <villas/kernel/devices/ip_device.hpp> | ||
|
|
||
| using villas::kernel::devices::IpDevice; | ||
|
|
||
| IpDevice IpDevice::from(const std::filesystem::path unsafe_path) { | ||
| if (!is_path_valid(unsafe_path)) | ||
| throw RuntimeError( | ||
| "Path {} failed validation as IpDevicePath [adress in hex].[name] ", | ||
| unsafe_path.u8string()); | ||
| return IpDevice(unsafe_path); | ||
| } | ||
|
|
||
| std::string IpDevice::ip_name() const { | ||
| int pos = name().find('.'); | ||
| return name().substr(pos + 1); | ||
IgnoreWarnings marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
|
|
||
| size_t IpDevice::addr() const { | ||
| size_t pos = name().find('.'); | ||
| std::string addr_hex = name().substr(0, pos); | ||
|
|
||
| // Convert from hex-string to number | ||
| std::stringstream ss; | ||
| ss << std::hex << addr_hex; | ||
| size_t addr = 0; | ||
| ss >> addr; | ||
|
|
||
| return addr; | ||
| } | ||
|
|
||
| bool IpDevice::is_path_valid(const std::filesystem::path unsafe_path) { | ||
| std::string assumed_device_name = unsafe_path.filename(); | ||
|
|
||
| // Match format of hexaddr.devicename | ||
| if (!std::regex_match(assumed_device_name, | ||
| std::regex(R"([0-9A-Fa-f]+\..*)"))) { | ||
| return false; | ||
| } | ||
|
|
||
| return true; | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,40 @@ | ||
| /* Implementation of driver interface for Linux/Unix based operation system drivers. | ||
| * | ||
| * Author: Pascal Bauer <pascal.bauer@rwth-aachen.de> | ||
| * | ||
| * SPDX-FileCopyrightText: 2023-2024 Pascal Bauer <pascal.bauer@rwth-aachen.de> | ||
| * SPDX-License-Identifier: Apache-2.0 | ||
| */ | ||
|
|
||
| #include <villas/kernel/devices/linux_driver.hpp> | ||
|
|
||
| #include <villas/kernel/devices/device.hpp> | ||
| #include <villas/utils.hpp> | ||
|
|
||
| using villas::kernel::devices::Device, villas::kernel::devices::LinuxDriver; | ||
| using villas::utils::write_to_file; | ||
|
|
||
| void LinuxDriver::attach(const Device &device) const { | ||
| if (device.driver().has_value()) { | ||
| device.driver().value()->unbind(device); | ||
| } | ||
| this->override(device); | ||
| device.probe(); | ||
| } | ||
|
|
||
| void LinuxDriver::bind(const Device &device) const { | ||
| write_to_file(device.name(), this->bind_path); | ||
| } | ||
|
|
||
| std::string LinuxDriver::name() const { | ||
| size_t pos = path.u8string().rfind('/'); | ||
| return path.u8string().substr(pos + 1); | ||
| } | ||
|
|
||
| void LinuxDriver::override(const Device &device) const { | ||
| write_to_file(this->name(), device.override_path()); | ||
| } | ||
|
|
||
| void LinuxDriver::unbind(const Device &device) const { | ||
| write_to_file(device.name(), this->unbind_path); | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.