Kristofer Jonsson | 116a635 | 2020-08-20 17:25:23 +0200 | [diff] [blame] | 1 | /* |
Kristofer Jonsson | 3c6a260 | 2022-03-10 11:17:29 +0100 | [diff] [blame] | 2 | * Copyright (c) 2020-2022 Arm Limited. |
Kristofer Jonsson | 116a635 | 2020-08-20 17:25:23 +0200 | [diff] [blame] | 3 | * |
| 4 | * SPDX-License-Identifier: Apache-2.0 |
| 5 | * |
| 6 | * Licensed under the Apache License, Version 2.0 (the License); you may |
| 7 | * not use this file except in compliance with the License. |
| 8 | * You may obtain a copy of the License at |
| 9 | * |
| 10 | * www.apache.org/licenses/LICENSE-2.0 |
| 11 | * |
| 12 | * Unless required by applicable law or agreed to in writing, software |
| 13 | * distributed under the License is distributed on an AS IS BASIS, WITHOUT |
| 14 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | * See the License for the specific language governing permissions and |
| 16 | * limitations under the License. |
| 17 | */ |
| 18 | |
| 19 | #include <ethosu.hpp> |
| 20 | #include <uapi/ethosu.h> |
| 21 | |
| 22 | #include <algorithm> |
| 23 | #include <exception> |
Kristofer Jonsson | 35de9e6 | 2022-03-08 13:25:45 +0100 | [diff] [blame] | 24 | #include <fstream> |
Kristofer Jonsson | 116a635 | 2020-08-20 17:25:23 +0200 | [diff] [blame] | 25 | #include <iostream> |
| 26 | |
Kristofer Jonsson | 116a635 | 2020-08-20 17:25:23 +0200 | [diff] [blame] | 27 | #include <fcntl.h> |
| 28 | #include <poll.h> |
Davide Grohmann | e446b42 | 2021-10-19 15:33:23 +0200 | [diff] [blame] | 29 | #include <signal.h> |
Kristofer Jonsson | 79689c5 | 2020-10-16 14:42:19 +0200 | [diff] [blame] | 30 | #include <sys/ioctl.h> |
| 31 | #include <sys/mman.h> |
Kristofer Jonsson | 116a635 | 2020-08-20 17:25:23 +0200 | [diff] [blame] | 32 | #include <unistd.h> |
| 33 | |
| 34 | using namespace std; |
| 35 | |
Per Åstrand | ec3f2b0 | 2021-06-09 10:43:38 +0200 | [diff] [blame] | 36 | namespace EthosU { |
| 37 | __attribute__((weak)) int eioctl(int fd, unsigned long cmd, void *data = nullptr) { |
Kristofer Jonsson | 116a635 | 2020-08-20 17:25:23 +0200 | [diff] [blame] | 38 | int ret = ::ioctl(fd, cmd, data); |
Kristofer Jonsson | 79689c5 | 2020-10-16 14:42:19 +0200 | [diff] [blame] | 39 | if (ret < 0) { |
Kristofer Jonsson | 116a635 | 2020-08-20 17:25:23 +0200 | [diff] [blame] | 40 | throw EthosU::Exception("IOCTL failed"); |
| 41 | } |
| 42 | |
| 43 | return ret; |
| 44 | } |
Kristofer Jonsson | b74492c | 2020-09-10 13:26:01 +0200 | [diff] [blame] | 45 | |
Per Åstrand | ec3f2b0 | 2021-06-09 10:43:38 +0200 | [diff] [blame] | 46 | __attribute__((weak)) int eopen(const char *pathname, int flags) { |
| 47 | int fd = ::open(pathname, flags); |
Per Åstrand | ec3f2b0 | 2021-06-09 10:43:38 +0200 | [diff] [blame] | 48 | if (fd < 0) { |
| 49 | throw Exception("Failed to open device"); |
| 50 | } |
| 51 | |
| 52 | return fd; |
| 53 | } |
| 54 | |
Davide Grohmann | e446b42 | 2021-10-19 15:33:23 +0200 | [diff] [blame] | 55 | __attribute__((weak)) int |
| 56 | eppoll(struct pollfd *fds, nfds_t nfds, const struct timespec *tmo_p, const sigset_t *sigmask) { |
| 57 | int result = ::ppoll(fds, nfds, tmo_p, sigmask); |
Davide Grohmann | c90bfab | 2021-10-19 13:59:27 +0200 | [diff] [blame] | 58 | if (result < 0) { |
Davide Grohmann | e446b42 | 2021-10-19 15:33:23 +0200 | [diff] [blame] | 59 | throw Exception("Failed to wait for ppoll event or signal"); |
Davide Grohmann | c90bfab | 2021-10-19 13:59:27 +0200 | [diff] [blame] | 60 | } |
| 61 | |
| 62 | return result; |
Per Åstrand | ec3f2b0 | 2021-06-09 10:43:38 +0200 | [diff] [blame] | 63 | } |
| 64 | |
| 65 | __attribute__((weak)) int eclose(int fd) { |
Davide Grohmann | c90bfab | 2021-10-19 13:59:27 +0200 | [diff] [blame] | 66 | int result = ::close(fd); |
| 67 | if (result < 0) { |
| 68 | throw Exception("Failed to close file"); |
| 69 | } |
| 70 | |
| 71 | return result; |
Per Åstrand | ec3f2b0 | 2021-06-09 10:43:38 +0200 | [diff] [blame] | 72 | } |
| 73 | __attribute((weak)) void *emmap(void *addr, size_t length, int prot, int flags, int fd, off_t offset) { |
Davide Grohmann | c90bfab | 2021-10-19 13:59:27 +0200 | [diff] [blame] | 74 | void *ptr = ::mmap(addr, length, prot, flags, fd, offset); |
| 75 | if (ptr == MAP_FAILED) { |
| 76 | throw Exception("Failed to mmap file"); |
| 77 | } |
| 78 | |
| 79 | return ptr; |
Per Åstrand | ec3f2b0 | 2021-06-09 10:43:38 +0200 | [diff] [blame] | 80 | } |
| 81 | |
| 82 | __attribute__((weak)) int emunmap(void *addr, size_t length) { |
Davide Grohmann | c90bfab | 2021-10-19 13:59:27 +0200 | [diff] [blame] | 83 | int result = ::munmap(addr, length); |
| 84 | if (result < 0) { |
| 85 | throw Exception("Failed to munmap file"); |
| 86 | } |
| 87 | |
| 88 | return result; |
Per Åstrand | ec3f2b0 | 2021-06-09 10:43:38 +0200 | [diff] [blame] | 89 | } |
| 90 | |
| 91 | } // namespace EthosU |
| 92 | |
Kristofer Jonsson | 79689c5 | 2020-10-16 14:42:19 +0200 | [diff] [blame] | 93 | namespace EthosU { |
Kristofer Jonsson | 116a635 | 2020-08-20 17:25:23 +0200 | [diff] [blame] | 94 | |
Kristofer Jonsson | b74492c | 2020-09-10 13:26:01 +0200 | [diff] [blame] | 95 | /**************************************************************************** |
| 96 | * Exception |
| 97 | ****************************************************************************/ |
| 98 | |
Kristofer Jonsson | 79689c5 | 2020-10-16 14:42:19 +0200 | [diff] [blame] | 99 | Exception::Exception(const char *msg) : msg(msg) {} |
Kristofer Jonsson | 116a635 | 2020-08-20 17:25:23 +0200 | [diff] [blame] | 100 | |
Kristofer Jonsson | 79689c5 | 2020-10-16 14:42:19 +0200 | [diff] [blame] | 101 | Exception::~Exception() throw() {} |
Kristofer Jonsson | 116a635 | 2020-08-20 17:25:23 +0200 | [diff] [blame] | 102 | |
Kristofer Jonsson | 79689c5 | 2020-10-16 14:42:19 +0200 | [diff] [blame] | 103 | const char *Exception::what() const throw() { |
Kristofer Jonsson | 116a635 | 2020-08-20 17:25:23 +0200 | [diff] [blame] | 104 | return msg.c_str(); |
| 105 | } |
| 106 | |
Kristofer Jonsson | b74492c | 2020-09-10 13:26:01 +0200 | [diff] [blame] | 107 | /**************************************************************************** |
Davide Grohmann | 35ce6c8 | 2021-06-01 15:03:51 +0200 | [diff] [blame] | 108 | * Semantic Version |
| 109 | ****************************************************************************/ |
| 110 | |
| 111 | bool SemanticVersion::operator==(const SemanticVersion &other) { |
| 112 | return other.major == major && other.minor == minor && other.patch == patch; |
| 113 | } |
| 114 | |
| 115 | bool SemanticVersion::operator<(const SemanticVersion &other) { |
| 116 | if (other.major > major) |
| 117 | return true; |
| 118 | if (other.minor > minor) |
| 119 | return true; |
| 120 | return other.patch > patch; |
| 121 | } |
| 122 | |
| 123 | bool SemanticVersion::operator<=(const SemanticVersion &other) { |
| 124 | return *this < other || *this == other; |
| 125 | } |
| 126 | |
| 127 | bool SemanticVersion::operator!=(const SemanticVersion &other) { |
| 128 | return !(*this == other); |
| 129 | } |
| 130 | |
| 131 | bool SemanticVersion::operator>(const SemanticVersion &other) { |
| 132 | return !(*this <= other); |
| 133 | } |
| 134 | |
| 135 | bool SemanticVersion::operator>=(const SemanticVersion &other) { |
| 136 | return !(*this < other); |
| 137 | } |
| 138 | |
| 139 | ostream &operator<<(ostream &out, const SemanticVersion &v) { |
| 140 | return out << "{ major=" << unsigned(v.major) << ", minor=" << unsigned(v.minor) << ", patch=" << unsigned(v.patch) |
| 141 | << " }"; |
| 142 | } |
| 143 | |
| 144 | /**************************************************************************** |
Kristofer Jonsson | b74492c | 2020-09-10 13:26:01 +0200 | [diff] [blame] | 145 | * Device |
| 146 | ****************************************************************************/ |
Kristofer Jonsson | 79689c5 | 2020-10-16 14:42:19 +0200 | [diff] [blame] | 147 | Device::Device(const char *device) { |
Per Åstrand | ec3f2b0 | 2021-06-09 10:43:38 +0200 | [diff] [blame] | 148 | fd = eopen(device, O_RDWR | O_NONBLOCK); |
Kristofer Jonsson | 116a635 | 2020-08-20 17:25:23 +0200 | [diff] [blame] | 149 | } |
| 150 | |
Davide Grohmann | fc49559 | 2022-04-25 15:27:52 +0200 | [diff] [blame] | 151 | Device::~Device() noexcept(false) { |
Per Åstrand | ec3f2b0 | 2021-06-09 10:43:38 +0200 | [diff] [blame] | 152 | eclose(fd); |
Kristofer Jonsson | 116a635 | 2020-08-20 17:25:23 +0200 | [diff] [blame] | 153 | } |
| 154 | |
Per Åstrand | afb399f | 2021-11-26 12:50:35 +0100 | [diff] [blame] | 155 | int Device::ioctl(unsigned long cmd, void *data) const { |
Kristofer Jonsson | 116a635 | 2020-08-20 17:25:23 +0200 | [diff] [blame] | 156 | return eioctl(fd, cmd, data); |
| 157 | } |
| 158 | |
Per Åstrand | afb399f | 2021-11-26 12:50:35 +0100 | [diff] [blame] | 159 | Capabilities Device::capabilities() const { |
Davide Grohmann | 35ce6c8 | 2021-06-01 15:03:51 +0200 | [diff] [blame] | 160 | ethosu_uapi_device_capabilities uapi; |
| 161 | (void)eioctl(fd, ETHOSU_IOCTL_CAPABILITIES_REQ, static_cast<void *>(&uapi)); |
| 162 | |
| 163 | Capabilities capabilities( |
| 164 | HardwareId(uapi.hw_id.version_status, |
| 165 | SemanticVersion(uapi.hw_id.version_major, uapi.hw_id.version_minor), |
| 166 | SemanticVersion(uapi.hw_id.product_major), |
| 167 | SemanticVersion(uapi.hw_id.arch_major_rev, uapi.hw_id.arch_minor_rev, uapi.hw_id.arch_patch_rev)), |
Davide Grohmann | 40e23d1 | 2021-06-17 09:59:40 +0200 | [diff] [blame] | 168 | HardwareConfiguration(uapi.hw_cfg.macs_per_cc, uapi.hw_cfg.cmd_stream_version, bool(uapi.hw_cfg.custom_dma)), |
Davide Grohmann | 35ce6c8 | 2021-06-01 15:03:51 +0200 | [diff] [blame] | 169 | SemanticVersion(uapi.driver_major_rev, uapi.driver_minor_rev, uapi.driver_patch_rev)); |
| 170 | return capabilities; |
| 171 | } |
| 172 | |
Kristofer Jonsson | b74492c | 2020-09-10 13:26:01 +0200 | [diff] [blame] | 173 | /**************************************************************************** |
| 174 | * Buffer |
| 175 | ****************************************************************************/ |
| 176 | |
Per Åstrand | afb399f | 2021-11-26 12:50:35 +0100 | [diff] [blame] | 177 | Buffer::Buffer(const Device &device, const size_t capacity) : fd(-1), dataPtr(nullptr), dataCapacity(capacity) { |
Kristofer Jonsson | 79689c5 | 2020-10-16 14:42:19 +0200 | [diff] [blame] | 178 | ethosu_uapi_buffer_create uapi = {static_cast<uint32_t>(dataCapacity)}; |
| 179 | fd = device.ioctl(ETHOSU_IOCTL_BUFFER_CREATE, static_cast<void *>(&uapi)); |
Kristofer Jonsson | 116a635 | 2020-08-20 17:25:23 +0200 | [diff] [blame] | 180 | |
Davide Grohmann | 2ba3c1d | 2021-11-08 15:08:48 +0100 | [diff] [blame] | 181 | void *d; |
| 182 | try { |
| 183 | d = emmap(nullptr, dataCapacity, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0); |
| 184 | } catch (std::exception &e) { |
| 185 | try { |
| 186 | eclose(fd); |
| 187 | } catch (...) { std::throw_with_nested(e); } |
Davide Grohmann | fc49559 | 2022-04-25 15:27:52 +0200 | [diff] [blame] | 188 | throw; |
Davide Grohmann | 2ba3c1d | 2021-11-08 15:08:48 +0100 | [diff] [blame] | 189 | } |
Kristofer Jonsson | 116a635 | 2020-08-20 17:25:23 +0200 | [diff] [blame] | 190 | dataPtr = reinterpret_cast<char *>(d); |
| 191 | } |
| 192 | |
Davide Grohmann | fc49559 | 2022-04-25 15:27:52 +0200 | [diff] [blame] | 193 | Buffer::~Buffer() noexcept(false) { |
Davide Grohmann | 80f8ddf | 2022-04-07 14:50:50 +0200 | [diff] [blame] | 194 | try { |
| 195 | emunmap(dataPtr, dataCapacity); |
| 196 | } catch (std::exception &e) { |
| 197 | try { |
| 198 | eclose(fd); |
| 199 | } catch (...) { std::throw_with_nested(e); } |
Davide Grohmann | fc49559 | 2022-04-25 15:27:52 +0200 | [diff] [blame] | 200 | throw; |
Davide Grohmann | 80f8ddf | 2022-04-07 14:50:50 +0200 | [diff] [blame] | 201 | } |
Davide Grohmann | fc49559 | 2022-04-25 15:27:52 +0200 | [diff] [blame] | 202 | eclose(fd); |
Kristofer Jonsson | 116a635 | 2020-08-20 17:25:23 +0200 | [diff] [blame] | 203 | } |
| 204 | |
Kristofer Jonsson | 79689c5 | 2020-10-16 14:42:19 +0200 | [diff] [blame] | 205 | size_t Buffer::capacity() const { |
Kristofer Jonsson | 116a635 | 2020-08-20 17:25:23 +0200 | [diff] [blame] | 206 | return dataCapacity; |
| 207 | } |
| 208 | |
Per Åstrand | afb399f | 2021-11-26 12:50:35 +0100 | [diff] [blame] | 209 | void Buffer::clear() const { |
Kristofer Jonsson | 116a635 | 2020-08-20 17:25:23 +0200 | [diff] [blame] | 210 | resize(0, 0); |
| 211 | } |
| 212 | |
Per Åstrand | afb399f | 2021-11-26 12:50:35 +0100 | [diff] [blame] | 213 | char *Buffer::data() const { |
Kristofer Jonsson | 116a635 | 2020-08-20 17:25:23 +0200 | [diff] [blame] | 214 | return dataPtr + offset(); |
| 215 | } |
| 216 | |
Per Åstrand | afb399f | 2021-11-26 12:50:35 +0100 | [diff] [blame] | 217 | void Buffer::resize(size_t size, size_t offset) const { |
Kristofer Jonsson | 116a635 | 2020-08-20 17:25:23 +0200 | [diff] [blame] | 218 | ethosu_uapi_buffer uapi; |
| 219 | uapi.offset = offset; |
Kristofer Jonsson | 79689c5 | 2020-10-16 14:42:19 +0200 | [diff] [blame] | 220 | uapi.size = size; |
Kristofer Jonsson | 116a635 | 2020-08-20 17:25:23 +0200 | [diff] [blame] | 221 | eioctl(fd, ETHOSU_IOCTL_BUFFER_SET, static_cast<void *>(&uapi)); |
| 222 | } |
| 223 | |
Kristofer Jonsson | 79689c5 | 2020-10-16 14:42:19 +0200 | [diff] [blame] | 224 | size_t Buffer::offset() const { |
Kristofer Jonsson | 116a635 | 2020-08-20 17:25:23 +0200 | [diff] [blame] | 225 | ethosu_uapi_buffer uapi; |
| 226 | eioctl(fd, ETHOSU_IOCTL_BUFFER_GET, static_cast<void *>(&uapi)); |
| 227 | return uapi.offset; |
| 228 | } |
| 229 | |
Kristofer Jonsson | 79689c5 | 2020-10-16 14:42:19 +0200 | [diff] [blame] | 230 | size_t Buffer::size() const { |
Kristofer Jonsson | 116a635 | 2020-08-20 17:25:23 +0200 | [diff] [blame] | 231 | ethosu_uapi_buffer uapi; |
| 232 | eioctl(fd, ETHOSU_IOCTL_BUFFER_GET, static_cast<void *>(&uapi)); |
| 233 | return uapi.size; |
| 234 | } |
| 235 | |
Kristofer Jonsson | 79689c5 | 2020-10-16 14:42:19 +0200 | [diff] [blame] | 236 | int Buffer::getFd() const { |
Kristofer Jonsson | 116a635 | 2020-08-20 17:25:23 +0200 | [diff] [blame] | 237 | return fd; |
| 238 | } |
| 239 | |
Kristofer Jonsson | b74492c | 2020-09-10 13:26:01 +0200 | [diff] [blame] | 240 | /**************************************************************************** |
| 241 | * Network |
| 242 | ****************************************************************************/ |
| 243 | |
Per Åstrand | afb399f | 2021-11-26 12:50:35 +0100 | [diff] [blame] | 244 | Network::Network(const Device &device, shared_ptr<Buffer> &buffer) : fd(-1), buffer(buffer) { |
Kristofer Jonsson | b74492c | 2020-09-10 13:26:01 +0200 | [diff] [blame] | 245 | // Create buffer handle |
Kristofer Jonsson | 116a635 | 2020-08-20 17:25:23 +0200 | [diff] [blame] | 246 | ethosu_uapi_network_create uapi; |
Kristofer Jonsson | 35de9e6 | 2022-03-08 13:25:45 +0100 | [diff] [blame] | 247 | uapi.type = ETHOSU_UAPI_NETWORK_BUFFER; |
| 248 | uapi.fd = buffer->getFd(); |
| 249 | fd = device.ioctl(ETHOSU_IOCTL_NETWORK_CREATE, static_cast<void *>(&uapi)); |
Kristofer Jonsson | 35de9e6 | 2022-03-08 13:25:45 +0100 | [diff] [blame] | 250 | try { |
Davide Grohmann | 80f8ddf | 2022-04-07 14:50:50 +0200 | [diff] [blame] | 251 | collectNetworkInfo(); |
| 252 | } catch (std::exception &e) { |
| 253 | try { |
| 254 | eclose(fd); |
| 255 | } catch (...) { std::throw_with_nested(e); } |
Davide Grohmann | fc49559 | 2022-04-25 15:27:52 +0200 | [diff] [blame] | 256 | throw; |
Jonny Svärd | 2c01713 | 2021-04-12 15:56:44 +0200 | [diff] [blame] | 257 | } |
Kristofer Jonsson | 35de9e6 | 2022-03-08 13:25:45 +0100 | [diff] [blame] | 258 | } |
Jonny Svärd | 2c01713 | 2021-04-12 15:56:44 +0200 | [diff] [blame] | 259 | |
Kristofer Jonsson | 3c6a260 | 2022-03-10 11:17:29 +0100 | [diff] [blame] | 260 | Network::Network(const Device &device, const unsigned index) : fd(-1) { |
Kristofer Jonsson | 35de9e6 | 2022-03-08 13:25:45 +0100 | [diff] [blame] | 261 | // Create buffer handle |
| 262 | ethosu_uapi_network_create uapi; |
| 263 | uapi.type = ETHOSU_UAPI_NETWORK_INDEX; |
| 264 | uapi.index = index; |
| 265 | fd = device.ioctl(ETHOSU_IOCTL_NETWORK_CREATE, static_cast<void *>(&uapi)); |
Kristofer Jonsson | 35de9e6 | 2022-03-08 13:25:45 +0100 | [diff] [blame] | 266 | try { |
Davide Grohmann | 80f8ddf | 2022-04-07 14:50:50 +0200 | [diff] [blame] | 267 | collectNetworkInfo(); |
| 268 | } catch (std::exception &e) { |
| 269 | try { |
| 270 | eclose(fd); |
| 271 | } catch (...) { std::throw_with_nested(e); } |
Davide Grohmann | fc49559 | 2022-04-25 15:27:52 +0200 | [diff] [blame] | 272 | throw; |
Davide Grohmann | 80f8ddf | 2022-04-07 14:50:50 +0200 | [diff] [blame] | 273 | } |
| 274 | } |
Kristofer Jonsson | 3c6a260 | 2022-03-10 11:17:29 +0100 | [diff] [blame] | 275 | |
Davide Grohmann | 80f8ddf | 2022-04-07 14:50:50 +0200 | [diff] [blame] | 276 | void Network::collectNetworkInfo() { |
| 277 | ethosu_uapi_network_info info; |
| 278 | ioctl(ETHOSU_IOCTL_NETWORK_INFO, static_cast<void *>(&info)); |
Kristofer Jonsson | 35de9e6 | 2022-03-08 13:25:45 +0100 | [diff] [blame] | 279 | |
Davide Grohmann | 80f8ddf | 2022-04-07 14:50:50 +0200 | [diff] [blame] | 280 | for (uint32_t i = 0; i < info.ifm_count; i++) { |
| 281 | ifmDims.push_back(info.ifm_size[i]); |
| 282 | } |
| 283 | |
| 284 | for (uint32_t i = 0; i < info.ofm_count; i++) { |
| 285 | ofmDims.push_back(info.ofm_size[i]); |
Kristofer Jonsson | 35de9e6 | 2022-03-08 13:25:45 +0100 | [diff] [blame] | 286 | } |
Kristofer Jonsson | 116a635 | 2020-08-20 17:25:23 +0200 | [diff] [blame] | 287 | } |
| 288 | |
Davide Grohmann | fc49559 | 2022-04-25 15:27:52 +0200 | [diff] [blame] | 289 | Network::~Network() noexcept(false) { |
Per Åstrand | ec3f2b0 | 2021-06-09 10:43:38 +0200 | [diff] [blame] | 290 | eclose(fd); |
Kristofer Jonsson | 116a635 | 2020-08-20 17:25:23 +0200 | [diff] [blame] | 291 | } |
| 292 | |
Kristofer Jonsson | 79689c5 | 2020-10-16 14:42:19 +0200 | [diff] [blame] | 293 | int Network::ioctl(unsigned long cmd, void *data) { |
Kristofer Jonsson | 116a635 | 2020-08-20 17:25:23 +0200 | [diff] [blame] | 294 | return eioctl(fd, cmd, data); |
| 295 | } |
| 296 | |
Kristofer Jonsson | 79689c5 | 2020-10-16 14:42:19 +0200 | [diff] [blame] | 297 | shared_ptr<Buffer> Network::getBuffer() { |
Kristofer Jonsson | 116a635 | 2020-08-20 17:25:23 +0200 | [diff] [blame] | 298 | return buffer; |
| 299 | } |
| 300 | |
Kristofer Jonsson | 79689c5 | 2020-10-16 14:42:19 +0200 | [diff] [blame] | 301 | const std::vector<size_t> &Network::getIfmDims() const { |
Kristofer Jonsson | b74492c | 2020-09-10 13:26:01 +0200 | [diff] [blame] | 302 | return ifmDims; |
Kristofer Jonsson | 116a635 | 2020-08-20 17:25:23 +0200 | [diff] [blame] | 303 | } |
| 304 | |
Kristofer Jonsson | 79689c5 | 2020-10-16 14:42:19 +0200 | [diff] [blame] | 305 | size_t Network::getIfmSize() const { |
Kristofer Jonsson | b74492c | 2020-09-10 13:26:01 +0200 | [diff] [blame] | 306 | size_t size = 0; |
| 307 | |
Kristofer Jonsson | 79689c5 | 2020-10-16 14:42:19 +0200 | [diff] [blame] | 308 | for (auto s : ifmDims) { |
Kristofer Jonsson | b74492c | 2020-09-10 13:26:01 +0200 | [diff] [blame] | 309 | size += s; |
| 310 | } |
| 311 | |
| 312 | return size; |
| 313 | } |
| 314 | |
Kristofer Jonsson | 79689c5 | 2020-10-16 14:42:19 +0200 | [diff] [blame] | 315 | const std::vector<size_t> &Network::getOfmDims() const { |
Kristofer Jonsson | b74492c | 2020-09-10 13:26:01 +0200 | [diff] [blame] | 316 | return ofmDims; |
| 317 | } |
| 318 | |
Kristofer Jonsson | 79689c5 | 2020-10-16 14:42:19 +0200 | [diff] [blame] | 319 | size_t Network::getOfmSize() const { |
Kristofer Jonsson | b74492c | 2020-09-10 13:26:01 +0200 | [diff] [blame] | 320 | size_t size = 0; |
| 321 | |
Kristofer Jonsson | 79689c5 | 2020-10-16 14:42:19 +0200 | [diff] [blame] | 322 | for (auto s : ofmDims) { |
Kristofer Jonsson | b74492c | 2020-09-10 13:26:01 +0200 | [diff] [blame] | 323 | size += s; |
| 324 | } |
| 325 | |
| 326 | return size; |
| 327 | } |
| 328 | |
| 329 | /**************************************************************************** |
| 330 | * Inference |
| 331 | ****************************************************************************/ |
| 332 | |
Davide Grohmann | fc49559 | 2022-04-25 15:27:52 +0200 | [diff] [blame] | 333 | Inference::~Inference() noexcept(false) { |
Per Åstrand | ec3f2b0 | 2021-06-09 10:43:38 +0200 | [diff] [blame] | 334 | eclose(fd); |
Kristofer Jonsson | 116a635 | 2020-08-20 17:25:23 +0200 | [diff] [blame] | 335 | } |
| 336 | |
Per Åstrand | 2354d3e | 2020-10-24 20:17:10 +0200 | [diff] [blame] | 337 | void Inference::create(std::vector<uint32_t> &counterConfigs, bool cycleCounterEnable = false) { |
Kristofer Jonsson | b74492c | 2020-09-10 13:26:01 +0200 | [diff] [blame] | 338 | ethosu_uapi_inference_create uapi; |
| 339 | |
Kristofer Jonsson | 79689c5 | 2020-10-16 14:42:19 +0200 | [diff] [blame] | 340 | if (ifmBuffers.size() > ETHOSU_FD_MAX) { |
Kristofer Jonsson | b74492c | 2020-09-10 13:26:01 +0200 | [diff] [blame] | 341 | throw Exception("IFM buffer overflow"); |
| 342 | } |
| 343 | |
Kristofer Jonsson | 79689c5 | 2020-10-16 14:42:19 +0200 | [diff] [blame] | 344 | if (ofmBuffers.size() > ETHOSU_FD_MAX) { |
Kristofer Jonsson | b74492c | 2020-09-10 13:26:01 +0200 | [diff] [blame] | 345 | throw Exception("OFM buffer overflow"); |
| 346 | } |
| 347 | |
Per Åstrand | 2354d3e | 2020-10-24 20:17:10 +0200 | [diff] [blame] | 348 | if (counterConfigs.size() != ETHOSU_PMU_EVENT_MAX) { |
| 349 | throw Exception("Wrong size of counter configurations"); |
| 350 | } |
| 351 | |
Kristofer Jonsson | b74492c | 2020-09-10 13:26:01 +0200 | [diff] [blame] | 352 | uapi.ifm_count = 0; |
Kristofer Jonsson | 79689c5 | 2020-10-16 14:42:19 +0200 | [diff] [blame] | 353 | for (auto it : ifmBuffers) { |
Kristofer Jonsson | b74492c | 2020-09-10 13:26:01 +0200 | [diff] [blame] | 354 | uapi.ifm_fd[uapi.ifm_count++] = it->getFd(); |
| 355 | } |
| 356 | |
| 357 | uapi.ofm_count = 0; |
Kristofer Jonsson | 79689c5 | 2020-10-16 14:42:19 +0200 | [diff] [blame] | 358 | for (auto it : ofmBuffers) { |
Kristofer Jonsson | b74492c | 2020-09-10 13:26:01 +0200 | [diff] [blame] | 359 | uapi.ofm_fd[uapi.ofm_count++] = it->getFd(); |
| 360 | } |
| 361 | |
Per Åstrand | 2354d3e | 2020-10-24 20:17:10 +0200 | [diff] [blame] | 362 | for (int i = 0; i < ETHOSU_PMU_EVENT_MAX; i++) { |
| 363 | uapi.pmu_config.events[i] = counterConfigs[i]; |
| 364 | } |
| 365 | |
| 366 | uapi.pmu_config.cycle_count = cycleCounterEnable; |
| 367 | |
Kristofer Jonsson | b74492c | 2020-09-10 13:26:01 +0200 | [diff] [blame] | 368 | fd = network->ioctl(ETHOSU_IOCTL_INFERENCE_CREATE, static_cast<void *>(&uapi)); |
| 369 | } |
| 370 | |
Per Åstrand | 2354d3e | 2020-10-24 20:17:10 +0200 | [diff] [blame] | 371 | std::vector<uint32_t> Inference::initializeCounterConfig() { |
| 372 | return std::vector<uint32_t>(ETHOSU_PMU_EVENT_MAX, 0); |
| 373 | } |
| 374 | |
| 375 | uint32_t Inference::getMaxPmuEventCounters() { |
| 376 | return ETHOSU_PMU_EVENT_MAX; |
| 377 | } |
| 378 | |
Per Åstrand | afb399f | 2021-11-26 12:50:35 +0100 | [diff] [blame] | 379 | int Inference::wait(int64_t timeoutNanos) const { |
Davide Grohmann | e446b42 | 2021-10-19 15:33:23 +0200 | [diff] [blame] | 380 | struct pollfd pfd; |
Kristofer Jonsson | 79689c5 | 2020-10-16 14:42:19 +0200 | [diff] [blame] | 381 | pfd.fd = fd; |
| 382 | pfd.events = POLLIN | POLLERR; |
Kristofer Jonsson | 116a635 | 2020-08-20 17:25:23 +0200 | [diff] [blame] | 383 | pfd.revents = 0; |
| 384 | |
Davide Grohmann | e446b42 | 2021-10-19 15:33:23 +0200 | [diff] [blame] | 385 | // if timeout negative wait forever |
| 386 | if (timeoutNanos < 0) { |
| 387 | return eppoll(&pfd, 1, NULL, NULL); |
| 388 | } |
Kristofer Jonsson | 116a635 | 2020-08-20 17:25:23 +0200 | [diff] [blame] | 389 | |
Davide Grohmann | e446b42 | 2021-10-19 15:33:23 +0200 | [diff] [blame] | 390 | struct timespec tmo_p; |
| 391 | int64_t nanosec = 1000000000; |
| 392 | tmo_p.tv_sec = timeoutNanos / nanosec; |
| 393 | tmo_p.tv_nsec = timeoutNanos % nanosec; |
Per Åstrand | 2354d3e | 2020-10-24 20:17:10 +0200 | [diff] [blame] | 394 | |
Davide Grohmann | e446b42 | 2021-10-19 15:33:23 +0200 | [diff] [blame] | 395 | return eppoll(&pfd, 1, &tmo_p, NULL); |
Kristofer Jonsson | 116a635 | 2020-08-20 17:25:23 +0200 | [diff] [blame] | 396 | } |
| 397 | |
Per Åstrand | afb399f | 2021-11-26 12:50:35 +0100 | [diff] [blame] | 398 | bool Inference::failed() const { |
Per Åstrand | 2354d3e | 2020-10-24 20:17:10 +0200 | [diff] [blame] | 399 | ethosu_uapi_result_status uapi; |
Kristofer Jonsson | 116a635 | 2020-08-20 17:25:23 +0200 | [diff] [blame] | 400 | |
Per Åstrand | 2354d3e | 2020-10-24 20:17:10 +0200 | [diff] [blame] | 401 | eioctl(fd, ETHOSU_IOCTL_INFERENCE_STATUS, static_cast<void *>(&uapi)); |
| 402 | |
| 403 | return uapi.status != ETHOSU_UAPI_STATUS_OK; |
| 404 | } |
| 405 | |
Per Åstrand | afb399f | 2021-11-26 12:50:35 +0100 | [diff] [blame] | 406 | const std::vector<uint32_t> Inference::getPmuCounters() const { |
Per Åstrand | 2354d3e | 2020-10-24 20:17:10 +0200 | [diff] [blame] | 407 | ethosu_uapi_result_status uapi; |
| 408 | std::vector<uint32_t> counterValues = std::vector<uint32_t>(ETHOSU_PMU_EVENT_MAX, 0); |
| 409 | |
| 410 | eioctl(fd, ETHOSU_IOCTL_INFERENCE_STATUS, static_cast<void *>(&uapi)); |
| 411 | |
| 412 | for (int i = 0; i < ETHOSU_PMU_EVENT_MAX; i++) { |
| 413 | if (uapi.pmu_config.events[i]) { |
| 414 | counterValues.at(i) = uapi.pmu_count.events[i]; |
| 415 | } |
| 416 | } |
| 417 | |
| 418 | return counterValues; |
| 419 | } |
| 420 | |
Per Åstrand | afb399f | 2021-11-26 12:50:35 +0100 | [diff] [blame] | 421 | uint64_t Inference::getCycleCounter() const { |
Per Åstrand | 2354d3e | 2020-10-24 20:17:10 +0200 | [diff] [blame] | 422 | ethosu_uapi_result_status uapi; |
| 423 | |
| 424 | eioctl(fd, ETHOSU_IOCTL_INFERENCE_STATUS, static_cast<void *>(&uapi)); |
| 425 | |
| 426 | return uapi.pmu_count.cycle_count; |
Kristofer Jonsson | 116a635 | 2020-08-20 17:25:23 +0200 | [diff] [blame] | 427 | } |
| 428 | |
Per Åstrand | afb399f | 2021-11-26 12:50:35 +0100 | [diff] [blame] | 429 | int Inference::getFd() const { |
Kristofer Jonsson | 116a635 | 2020-08-20 17:25:23 +0200 | [diff] [blame] | 430 | return fd; |
| 431 | } |
| 432 | |
Per Åstrand | afb399f | 2021-11-26 12:50:35 +0100 | [diff] [blame] | 433 | const shared_ptr<Network> Inference::getNetwork() const { |
Kristofer Jonsson | 116a635 | 2020-08-20 17:25:23 +0200 | [diff] [blame] | 434 | return network; |
| 435 | } |
| 436 | |
Kristofer Jonsson | 79689c5 | 2020-10-16 14:42:19 +0200 | [diff] [blame] | 437 | vector<shared_ptr<Buffer>> &Inference::getIfmBuffers() { |
Kristofer Jonsson | b74492c | 2020-09-10 13:26:01 +0200 | [diff] [blame] | 438 | return ifmBuffers; |
Kristofer Jonsson | 116a635 | 2020-08-20 17:25:23 +0200 | [diff] [blame] | 439 | } |
| 440 | |
Kristofer Jonsson | 79689c5 | 2020-10-16 14:42:19 +0200 | [diff] [blame] | 441 | vector<shared_ptr<Buffer>> &Inference::getOfmBuffers() { |
Kristofer Jonsson | b74492c | 2020-09-10 13:26:01 +0200 | [diff] [blame] | 442 | return ofmBuffers; |
Kristofer Jonsson | 116a635 | 2020-08-20 17:25:23 +0200 | [diff] [blame] | 443 | } |
| 444 | |
Kristofer Jonsson | 79689c5 | 2020-10-16 14:42:19 +0200 | [diff] [blame] | 445 | } // namespace EthosU |