Add kernel driver version check in driver library

The driver library will now check that it's compatible with the kernel
driver in use by checking the kernel driver's version.

The kernel driver version has also been made available to the library
users and the Python wrapper has been updated accordingly.

Change-Id: Ieae8c0bfc323f945038e7264eceeab90c833f76d
Signed-off-by: Mikael Olsson <mikael.olsson@arm.com>
diff --git a/driver_library/src/ethosu.cpp b/driver_library/src/ethosu.cpp
index e425e52..1758b07 100644
--- a/driver_library/src/ethosu.cpp
+++ b/driver_library/src/ethosu.cpp
@@ -1,5 +1,5 @@
 /*
- * SPDX-FileCopyrightText: Copyright 2020-2022 Arm Limited and/or its affiliates <open-source-office@arm.com>
+ * SPDX-FileCopyrightText: Copyright 2020-2023 Arm Limited and/or its affiliates <open-source-office@arm.com>
  *
  * SPDX-License-Identifier: Apache-2.0
  *
@@ -25,6 +25,7 @@
 #include <fstream>
 #include <iomanip>
 #include <iostream>
+#include <utility>
 
 #include <fcntl.h>
 #include <poll.h>
@@ -36,6 +37,9 @@
 using namespace std;
 
 namespace {
+std::string driverVersionToString(const EthosU::ethosu_uapi_kernel_driver_version &version) {
+    return std::to_string(version.major) + "." + std::to_string(version.minor) + "." + std::to_string(version.patch);
+}
 
 enum class Severity { Error, Warning, Info, Debug };
 
@@ -214,9 +218,27 @@
 /****************************************************************************
  * Device
  ****************************************************************************/
-Device::Device(const char *device) {
-    fd = eopen(device, O_RDWR | O_NONBLOCK);
+Device::Device(const char *device) : fd(eopen(device, O_RDWR | O_NONBLOCK)) {
+    ethosu_uapi_kernel_driver_version version = {};
+
     Log(Severity::Info) << "Device(\"" << device << "\"). this=" << this << ", fd=" << fd << endl;
+
+    try {
+        eioctl(fd, ETHOSU_IOCTL_DRIVER_VERSION_GET, &version);
+
+        if (MAX_SUPPORTED_KERNEL_DRIVER_MAJOR_VERSION < version.major ||
+            MIN_SUPPORTED_KERNEL_DRIVER_MAJOR_VERSION > version.major) {
+            throw Exception(
+                std::string("Unsupported kernel driver version: ").append(driverVersionToString(version)).c_str());
+        }
+    } catch (std::exception &e) {
+        try {
+            eclose(fd);
+        } catch (...) { std::throw_with_nested(e); }
+        throw;
+    }
+
+    driverVersion = {version.major, version.minor, version.patch};
 }
 
 Device::~Device() noexcept(false) {
@@ -228,6 +250,10 @@
     return eioctl(fd, cmd, data);
 }
 
+const SemanticVersion &Device::getDriverVersion() const {
+    return driverVersion;
+}
+
 Capabilities Device::capabilities() const {
     ethosu_uapi_device_capabilities uapi;
     (void)eioctl(fd, ETHOSU_IOCTL_CAPABILITIES_REQ, static_cast<void *>(&uapi));
@@ -568,4 +594,7 @@
     return ofmBuffers;
 }
 
+static_assert(MAX_SUPPORTED_KERNEL_DRIVER_MAJOR_VERSION >= ETHOSU_KERNEL_DRIVER_VERSION_MAJOR &&
+                  MIN_SUPPORTED_KERNEL_DRIVER_MAJOR_VERSION <= ETHOSU_KERNEL_DRIVER_VERSION_MAJOR,
+              "Unsupported major kernel driver version in UAPI");
 } // namespace EthosU