From 35ce6c809ccf637c6bb8a00ad14b051b87d9884a Mon Sep 17 00:00:00 2001 From: Davide Grohmann Date: Tue, 1 Jun 2021 15:03:51 +0200 Subject: Add support for handling capabilities requests Change-Id: Id5aa197312c88b0c448dc085d8477ed67da24724 --- driver_library/include/ethosu.hpp | 100 +++++++++++++++++++++++++++++++++++++- driver_library/src/ethosu.cpp | 54 ++++++++++++++++++++ 2 files changed, 153 insertions(+), 1 deletion(-) (limited to 'driver_library') diff --git a/driver_library/include/ethosu.hpp b/driver_library/include/ethosu.hpp index 70d0701..d3f7421 100644 --- a/driver_library/include/ethosu.hpp +++ b/driver_library/include/ethosu.hpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2020 Arm Limited. All rights reserved. + * Copyright (c) 2020-2021 Arm Limited. All rights reserved. * * SPDX-License-Identifier: Apache-2.0 * @@ -19,10 +19,25 @@ #pragma once #include +#include #include #include #include +/* + *The following undef are necessary to avoid clash with macros in GNU C Library + * if removed the following warning/error are produced: + * + * In the GNU C Library, "major" ("minor") is defined + * by . For historical compatibility, it is + * currently defined by as well, but we plan to + * remove this soon. To use "major" ("minor"), include + * directly. If you did not intend to use a system-defined macro + * "major" ("minor"), you should undefine it after including . + */ +#undef major +#undef minor + namespace EthosU { class Exception : public std::exception { @@ -35,12 +50,95 @@ private: std::string msg; }; +/** + * Sematic Version : major.minor.patch + */ +class SemanticVersion { +public: + SemanticVersion(uint32_t _major = 0, uint32_t _minor = 0, uint32_t _patch = 0) : + major(_major), minor(_minor), patch(_patch){}; + + bool operator==(const SemanticVersion &other); + bool operator<(const SemanticVersion &other); + bool operator<=(const SemanticVersion &other); + bool operator!=(const SemanticVersion &other); + bool operator>(const SemanticVersion &other); + bool operator>=(const SemanticVersion &other); + + uint32_t major; + uint32_t minor; + uint32_t patch; +}; + +std::ostream &operator<<(std::ostream &out, const SemanticVersion &v); + +/* + * Hardware Identifier + * @versionStatus: Version status + * @version: Version revision + * @product: Product revision + * @architecture: Architecture revison + */ +struct HardwareId { +public: + HardwareId(uint32_t _versionStatus, + const SemanticVersion &_version, + const SemanticVersion &_product, + const SemanticVersion &_architecture) : + versionStatus(_versionStatus), + version(_version), product(_product), architecture(_architecture) {} + + uint32_t versionStatus; + SemanticVersion version; + SemanticVersion product; + SemanticVersion architecture; +}; + +/* + * Hardware Configuration + * @macsPerClockCycle: MACs per clock cycle + * @cmdStreamVersion: NPU command stream version + * @shramSize: SHRAM size + * @customDma: Custom DMA enabled + */ +struct HardwareConfiguration { +public: + HardwareConfiguration(uint32_t _macsPerClockCycle, + uint32_t _cmdStreamVersion, + uint32_t _shramSize, + bool _customDma) : + macsPerClockCycle(_macsPerClockCycle), + cmdStreamVersion(_cmdStreamVersion), shramSize(_shramSize), customDma(_customDma) {} + + uint32_t macsPerClockCycle; + uint32_t cmdStreamVersion; + uint32_t shramSize; + bool customDma; +}; + +/** + * Device capabilities + * @hwId: Hardware + * @driver: Driver revision + * @hwCfg Hardware configuration + */ +class Capabilities { +public: + Capabilities(const HardwareId &_hwId, const HardwareConfiguration &_hwCfg, const SemanticVersion &_driver) : + hwId(_hwId), hwCfg(_hwCfg), driver(_driver) {} + + HardwareId hwId; + HardwareConfiguration hwCfg; + SemanticVersion driver; +}; + class Device { public: Device(const char *device = "/dev/ethosu0"); virtual ~Device(); int ioctl(unsigned long cmd, void *data = nullptr); + Capabilities capabilities(); private: int fd; diff --git a/driver_library/src/ethosu.cpp b/driver_library/src/ethosu.cpp index 1768271..ea1f7f4 100644 --- a/driver_library/src/ethosu.cpp +++ b/driver_library/src/ethosu.cpp @@ -111,6 +111,43 @@ const char *Exception::what() const throw() { return msg.c_str(); } +/**************************************************************************** + * Semantic Version + ****************************************************************************/ + +bool SemanticVersion::operator==(const SemanticVersion &other) { + return other.major == major && other.minor == minor && other.patch == patch; +} + +bool SemanticVersion::operator<(const SemanticVersion &other) { + if (other.major > major) + return true; + if (other.minor > minor) + return true; + return other.patch > patch; +} + +bool SemanticVersion::operator<=(const SemanticVersion &other) { + return *this < other || *this == other; +} + +bool SemanticVersion::operator!=(const SemanticVersion &other) { + return !(*this == other); +} + +bool SemanticVersion::operator>(const SemanticVersion &other) { + return !(*this <= other); +} + +bool SemanticVersion::operator>=(const SemanticVersion &other) { + return !(*this < other); +} + +ostream &operator<<(ostream &out, const SemanticVersion &v) { + return out << "{ major=" << unsigned(v.major) << ", minor=" << unsigned(v.minor) << ", patch=" << unsigned(v.patch) + << " }"; +} + /**************************************************************************** * Device ****************************************************************************/ @@ -130,6 +167,23 @@ int Device::ioctl(unsigned long cmd, void *data) { return eioctl(fd, cmd, data); } +Capabilities Device::capabilities() { + ethosu_uapi_device_capabilities uapi; + (void)eioctl(fd, ETHOSU_IOCTL_CAPABILITIES_REQ, static_cast(&uapi)); + + Capabilities capabilities( + HardwareId(uapi.hw_id.version_status, + SemanticVersion(uapi.hw_id.version_major, uapi.hw_id.version_minor), + SemanticVersion(uapi.hw_id.product_major), + SemanticVersion(uapi.hw_id.arch_major_rev, uapi.hw_id.arch_minor_rev, uapi.hw_id.arch_patch_rev)), + HardwareConfiguration(uapi.hw_cfg.macs_per_cc, + uapi.hw_cfg.cmd_stream_version, + uapi.hw_cfg.shram_size, + bool(uapi.hw_cfg.custom_dma)), + SemanticVersion(uapi.driver_major_rev, uapi.driver_minor_rev, uapi.driver_patch_rev)); + return capabilities; +} + /**************************************************************************** * Buffer ****************************************************************************/ -- cgit v1.2.1