Change PMU event counter values to use 64-bit

The PMU event counter value is an accumulation of 32-bit values during
the inference and to ensure the total value fits in the rpmsg message
and UAPI, the variable holding the value has been changed to 64-bit.

The driver library, Python wrapper and inference runner have been
changed accordingly to support the 64-bit values.

Change-Id: I09a8e45eb75800c8a787f83abff5a3693148cc15
Signed-off-by: Mikael Olsson <mikael.olsson@arm.com>
diff --git a/driver_library/include/ethosu.hpp b/driver_library/include/ethosu.hpp
index eaa1ce7..27f6828 100644
--- a/driver_library/include/ethosu.hpp
+++ b/driver_library/include/ethosu.hpp
@@ -235,7 +235,7 @@
     virtual ~Inference() noexcept(false);
 
     bool wait(int64_t timeoutNanos = -1) const;
-    const std::vector<uint32_t> getPmuCounters() const;
+    const std::vector<uint64_t> getPmuCounters() const;
     uint64_t getCycleCounter() const;
     bool cancel() const;
     InferenceStatus status() const;
diff --git a/driver_library/python/src/ethosu_driver/swig/driver.i b/driver_library/python/src/ethosu_driver/swig/driver.i
index 6e0ad25..a8db7c1 100644
--- a/driver_library/python/src/ethosu_driver/swig/driver.i
+++ b/driver_library/python/src/ethosu_driver/swig/driver.i
@@ -29,6 +29,14 @@
 %shared_ptr(EthosU::Buffer);
 %shared_ptr(EthosU::Network);
 
+%typemap(out) (std::vector<uint64_t>) {
+    PyObject *list = PyList_New($1.size());
+    for (size_t i=0; i < $1.size(); ++i) {
+        PyList_SET_ITEM(list, i, PyLong_FromUnsignedLong($1.at(i)));
+    }
+    $result = list;
+}
+
 namespace std {
    %template(UintVector) vector<unsigned int>;
    %template(SizeTVector) vector<size_t>;
@@ -508,7 +516,7 @@
     Returns:
         list: PMU event data
     ") getPmuCounters;
-    const std::vector<uint32_t> getPmuCounters();
+    const std::vector<uint64_t> getPmuCounters();
 
     %feature("docstring",
     "
diff --git a/driver_library/python/swig_generate.py b/driver_library/python/swig_generate.py
index bdd43a3..8394d96 100755
--- a/driver_library/python/swig_generate.py
+++ b/driver_library/python/swig_generate.py
@@ -1,4 +1,4 @@
-# SPDX-FileCopyrightText: Copyright 2021-2022 Arm Limited and/or its affiliates <open-source-office@arm.com>
+# SPDX-FileCopyrightText: Copyright 2021-2023 Arm Limited and/or its affiliates <open-source-office@arm.com>
 # SPDX-License-Identifier: Apache-2.0
 """
 This script executes SWIG commands to generate C++ library wrappers.
@@ -13,6 +13,7 @@
     print('Generating wrappers for {}'.format(name))
     subprocess.check_output("swig -v -c++ -python" +
                             " -Wall" +
+                            " -DSWIGWORDSIZE64 " + # Force 64-bit word size for uint64_t vector to work
                             " -o {}/src/ethosu_driver/_generated/{}_wrap.cpp ".format(__current_dir, name) +
                             "-outdir {}/src/ethosu_driver/_generated ".format(__current_dir) +
                             "{} ".format(extr_includes) +
diff --git a/driver_library/src/ethosu.cpp b/driver_library/src/ethosu.cpp
index 7aec696..a4feef1 100644
--- a/driver_library/src/ethosu.cpp
+++ b/driver_library/src/ethosu.cpp
@@ -539,9 +539,9 @@
     throw Exception("Unknown inference status");
 }
 
-const std::vector<uint32_t> Inference::getPmuCounters() const {
+const std::vector<uint64_t> Inference::getPmuCounters() const {
     ethosu_uapi_result_status uapi;
-    std::vector<uint32_t> counterValues = std::vector<uint32_t>(ETHOSU_PMU_EVENT_MAX, 0);
+    std::vector<uint64_t> counterValues = std::vector<uint64_t>(ETHOSU_PMU_EVENT_MAX, 0);
 
     eioctl(fd, ETHOSU_IOCTL_INFERENCE_STATUS, static_cast<void *>(&uapi));
 
diff --git a/kernel/ethosu_core_rpmsg.h b/kernel/ethosu_core_rpmsg.h
index 8d2c51d..0b7feab 100644
--- a/kernel/ethosu_core_rpmsg.h
+++ b/kernel/ethosu_core_rpmsg.h
@@ -135,7 +135,7 @@
 	uint32_t ofm_size[ETHOSU_CORE_BUFFER_MAX];
 	uint32_t status;
 	uint8_t  pmu_event_config[ETHOSU_CORE_PMU_MAX];
-	uint32_t pmu_event_count[ETHOSU_CORE_PMU_MAX];
+	uint64_t pmu_event_count[ETHOSU_CORE_PMU_MAX];
 	uint32_t pmu_cycle_counter_enable;
 	uint64_t pmu_cycle_counter_count;
 };
diff --git a/kernel/ethosu_inference.c b/kernel/ethosu_inference.c
index ac617c3..5fbad58 100644
--- a/kernel/ethosu_inference.c
+++ b/kernel/ethosu_inference.c
@@ -457,7 +457,7 @@
 		inf->pmu_cycle_counter_count = rsp->pmu_cycle_counter_count;
 
 		dev_dbg(dev,
-			"PMU events. config=[%u, %u, %u, %u], count=[%u, %u, %u, %u]\n",
+			"PMU events. config=[%u, %u, %u, %u], count=[%llu, %llu, %llu, %llu]\n",
 			inf->pmu_event_config[0], inf->pmu_event_config[1],
 			inf->pmu_event_config[2], inf->pmu_event_config[3],
 			inf->pmu_event_count[0], inf->pmu_event_count[1],
diff --git a/kernel/ethosu_inference.h b/kernel/ethosu_inference.h
index 9fa0a4a..5040ef1 100644
--- a/kernel/ethosu_inference.h
+++ b/kernel/ethosu_inference.h
@@ -72,7 +72,7 @@
 	struct ethosu_network     *net;
 	enum ethosu_uapi_status   status;
 	uint8_t                   pmu_event_config[ETHOSU_PMU_EVENT_MAX];
-	uint32_t                  pmu_event_count[ETHOSU_PMU_EVENT_MAX];
+	uint64_t                  pmu_event_count[ETHOSU_PMU_EVENT_MAX];
 	uint32_t                  pmu_cycle_counter_enable;
 	uint64_t                  pmu_cycle_counter_count;
 	struct ethosu_mailbox_msg msg;
diff --git a/kernel/uapi/ethosu.h b/kernel/uapi/ethosu.h
index 35eaf60..8240ad9 100644
--- a/kernel/uapi/ethosu.h
+++ b/kernel/uapi/ethosu.h
@@ -169,7 +169,7 @@
  * @cycle_count:        Count for cycle counter.
  */
 struct ethosu_uapi_pmu_counts {
-	__u32 events[ETHOSU_PMU_EVENT_MAX];
+	__u64 events[ETHOSU_PMU_EVENT_MAX];
 	__u64 cycle_count;
 };
 
diff --git a/utils/inference_runner/inference_runner.cpp b/utils/inference_runner/inference_runner.cpp
index 721cd57..d2546e2 100644
--- a/utils/inference_runner/inference_runner.cpp
+++ b/utils/inference_runner/inference_runner.cpp
@@ -297,7 +297,7 @@
                 if (std::count(enabledCounters.begin(), enabledCounters.end(), 0) <
                     Inference::getMaxPmuEventCounters()) {
 
-                    const std::vector<uint32_t> pmus = inference->getPmuCounters();
+                    const std::vector<uint64_t> pmus = inference->getPmuCounters();
                     cout << "PMUs : [";
                     for (auto p : pmus) {
                         cout << " " << p;