1/*
2 * Copyright (C) 2017 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#ifndef ANDROID_HARDWARE_V1_0_HEXAGON_CONTROLLER_H
18#define ANDROID_HARDWARE_V1_0_HEXAGON_CONTROLLER_H
19
20#include <android-base/logging.h>
21#include "HexagonUtils.h"
22#include "dlfcn.h"
23#include "hexagon_nn_controller/hexagon_nn_controller.h"
24
25namespace android {
26namespace hardware {
27namespace neuralnetworks {
28namespace V1_0 {
29namespace implementation {
30namespace hexagon {
31
32// interface wrapper
33class Controller {
34    // methods
35   private:
36    Controller();
37    ~Controller();
38    Controller(const Controller&) = delete;
39    Controller(Controller&&) = delete;
40    Controller& operator=(const Controller&) = delete;
41    Controller& operator=(Controller&&) = delete;
42
43    template <typename Function>
44    Function loadFunction(const char* name) {
45        void* fn = dlsym(mHandle, name);
46        if (fn == nullptr) {
47            LOG(ERROR) << "loadFunction -- failed to load function " << name;
48        }
49        return reinterpret_cast<Function>(fn);
50    }
51
52    bool openNnlib();
53    bool closeNnlib();
54
55   public:
56    static Controller& getInstance();
57    bool resetNnlib();
58
59    int init(hexagon_nn_nn_id* g);
60
61    int getlog(hexagon_nn_nn_id id, unsigned char* buf, uint32_t length);
62
63    int snpprint(hexagon_nn_nn_id id, unsigned char* buf, uint32_t length);
64
65    int set_debug_level(hexagon_nn_nn_id id, int level);
66
67    int prepare(hexagon_nn_nn_id id);
68
69    int append_node(hexagon_nn_nn_id id, uint32_t node_id, op_type operation,
70                    hexagon_nn_padding_type padding, const hexagon_nn_input* inputs,
71                    uint32_t num_inputs, const hexagon_nn_output* outputs, uint32_t num_outputs);
72
73    int append_const_node(hexagon_nn_nn_id id, uint32_t node_id, uint32_t batches, uint32_t height,
74                          uint32_t width, uint32_t depth, const uint8_t* data, uint32_t data_len);
75
76    int execute_new(hexagon_nn_nn_id id, const hexagon_nn_tensordef* inputs, uint32_t n_inputs,
77                    hexagon_nn_tensordef* outputs, uint32_t n_outputs);
78
79    int execute(hexagon_nn_nn_id id, uint32_t batches_in, uint32_t height_in, uint32_t width_in,
80                uint32_t depth_in, const uint8_t* data_in, uint32_t data_len_in,
81                uint32_t* batches_out, uint32_t* height_out, uint32_t* width_out,
82                uint32_t* depth_out, uint8_t* data_out, uint32_t data_out_max,
83                uint32_t* data_out_size);
84
85    int teardown(hexagon_nn_nn_id id);
86
87    int get_perfinfo(hexagon_nn_nn_id id, hexagon_nn_perfinfo* info_out, unsigned int info_out_len,
88                     unsigned int* n_items_out);
89
90    int reset_perfinfo(hexagon_nn_nn_id id, uint32_t event);
91
92    int version(int* ver);
93
94    int last_execution_cycles(hexagon_nn_nn_id id, unsigned int* cycles_lo,
95                              unsigned int* cycles_hi);
96
97    int GetHexagonBinaryVersion(int* ver);
98
99    int PrintLog(const uint8_t* data_in, unsigned int data_in_len);
100
101    int op_name_to_id(const char* name, unsigned int* id);
102
103    int op_id_to_name(const unsigned int id, char* name, int name_len);
104
105    int disable_dcvs();
106
107    int set_powersave_level(unsigned int level);
108
109    int config();
110
111    unsigned int get_dsp_offset();
112
113    int boost(int bus_usage);
114
115    int slow();
116
117    // members
118   private:
119    static const char kFilename[];
120    void* mHandle;
121    hexagon_nn_controller_init_fn mFn_init;
122    hexagon_nn_controller_getlog_fn mFn_getlog;
123    hexagon_nn_controller_snpprint_fn mFn_snpprint;
124    hexagon_nn_controller_set_debug_level_fn mFn_set_debug_level;
125    hexagon_nn_controller_prepare_fn mFn_prepare;
126    hexagon_nn_controller_append_node_fn mFn_append_node;
127    hexagon_nn_controller_append_const_node_fn mFn_append_const_node;
128    hexagon_nn_controller_execute_new_fn mFn_execute_new;
129    hexagon_nn_controller_execute_fn mFn_execute;
130    hexagon_nn_controller_teardown_fn mFn_teardown;
131    hexagon_nn_controller_get_perfinfo_fn mFn_get_perfinfo;
132    hexagon_nn_controller_reset_perfinfo_fn mFn_reset_perfinfo;
133    hexagon_nn_controller_version_fn mFn_version;
134    hexagon_nn_controller_last_execution_cycles_fn mFn_last_execution_cycles;
135    hexagon_nn_controller_GetHexagonBinaryVersion_fn mFn_GetHexagonBinaryVersion;
136    hexagon_nn_controller_PrintLog_fn mFn_PrintLog;
137    hexagon_nn_controller_op_name_to_id_fn mFn_op_name_to_id;
138    hexagon_nn_controller_op_id_to_name_fn mFn_op_id_to_name;
139    hexagon_nn_controller_disable_dcvs_fn mFn_disable_dcvs;
140    hexagon_nn_controller_set_powersave_level_fn mFn_set_powersave_level;
141    hexagon_nn_controller_config_fn mFn_config;
142    hexagon_nn_controller_get_dsp_offset_fn mFn_get_dsp_offset;
143    hexagon_nn_controller_boost_fn mFn_boost;
144    hexagon_nn_controller_slow_fn mFn_slow;
145};
146
147}  // namespace hexagon
148}  // namespace implementation
149}  // namespace V1_0
150}  // namespace neuralnetworks
151}  // namespace hardware
152}  // namespace android
153
154#endif  // ANDROID_HARDWARE_V1_0_HEXAGON_CONTROLLER_H
155