[UAPI] Use a hwmem type to support contiguous mapping
[platform/adaptation/npu/trix-engine.git] / src / core / npu / NPUdrvAPI.h
1 /**
2  * Proprietary
3  * Copyright (C) 2019 Samsung Electronics
4  * Copyright (C) 2019 Parichay Kapoor <pk.kapoor@samsung.com>
5  * Copyright (C) 2019 MyungJoo Ham <myungjoo.ham@samsung.com>
6  * Copyright (C) 2019 Dongju Chae <dongju.chae@samsung.com>
7  */
8 /**
9  * @file NPUdrvAPI.h
10  * @date 7 June 2019
11  * @brief API to access SR NPU device driver.
12  * @see https://code.sec.samsung.net/confluence/display/ODLC/2020+Overall+Software+Stack
13  * @author Parichay Kapoor <pk.kapoor@samsung.com>
14  * @author MyungJoo Ham <myungjoo.ham@samsung.com>
15  * @author Dongju Chae <dongju.chae@samsung.com>
16  * @bug No known bugs except for NYI items
17  */
18
19 #ifndef __NPU_CORE_NPUdrvAPI_H__
20 #define __NPU_CORE_NPUdrvAPI_H__
21
22 #include <fcntl.h>
23 #include <stddef.h>
24 #include <errno.h>
25
26 #include <misc/trinity.h>
27 #include <ne-utils.h>
28 #include <typedef.h>
29
30 #include <atomic>
31 #include <bitset>
32 #include <bits/stdc++.h>
33 #include <memory>
34 #include <thread>
35
36 #include <sys/user.h> /* PAGE_SIZE */
37 /** the size of each allocation is aligned to PAGE_SIZE */
38 #ifndef PAGE_SHIFT
39 #define PAGE_SHIFT (12)
40 #endif
41 #ifndef PAGE_SIZE
42 #define PAGE_SIZE (1UL << PAGE_SHIFT)
43 #endif
44 #ifndef PFN_UP
45 #define PFN_UP(x) (((x) + PAGE_SIZE - 1) >> PAGE_SHIFT)
46 #endif
47 #define ALIGNED_SIZE(x) (PFN_UP(x) * PAGE_SIZE)
48
49 typedef struct trinity_model model_config_t;
50 typedef struct trinity_input input_config_t;
51 typedef enum trinity_state device_state_t;
52 typedef enum trinity_dev_type device_dev_t;
53
54 /** @brief class for Driver API. Each device should implement its APIs */
55 class DriverAPI {
56   public:
57     DriverAPI (int dev_id);
58     virtual ~DriverAPI () {}
59
60     static int getNumDevices (dev_type type);
61     static std::unique_ptr<DriverAPI> createDriverAPI (dev_type type, int device_id);
62     static uint32_t getCompiledAPILevel () { return api_level_; }
63     int getDeviceID () const { return dev_id_; }
64     int getDeviceFD () const { return dev_fd_; }
65     bool initialized () const { return initialized_; }
66
67     /** @brief open the device and set device fd */
68     virtual int open () { return -ENODEV; }
69     virtual int getAPILevel (uint32_t *level) const { return -EPERM; }
70     virtual int getTops (uint32_t *tops) const { return -EPERM; }
71
72     virtual int checkSanity () { return 0; }
73
74     /** The below requires initialized device */
75     /** @brief check whether the device is ready (true) or busy (false) */
76     virtual device_state_t isReady () const { return device_state_t::STATE_UNKNOWN; }
77
78     /** @brief return a number of requests submitted */
79     virtual uint32_t numRequests () const { return 0; }
80
81     /** @brief allocate memory with the given size. return dmabuf fd */
82     virtual int alloc (size_t size, bool contiguous = false) const { return -EPERM; }
83     /** @brief deallocate memory with the corresponding dmabuf fd */
84     virtual int dealloc (int dmabuf, bool contiguous = false) const { return -EPERM; }
85     /** @brief get memory status */
86     virtual int getMemoryStatus (size_t *alloc, size_t *free) const { return -EPERM; }
87
88     /** @brief do mmap() for the dmabuf fd */
89     virtual void *mmap (int dmabuf, size_t size) const { return nullptr; }
90     /** @brief do munmap() for the mmaped pointer */
91     virtual int munmap (void *addr, size_t size) const { return -EPERM; }
92
93     /** @brief run inference with the input config */
94     virtual int runInput (input_config_t *input) const { return -EPERM; }
95     /** @brief stop all requests. The stopped requests should be notified */
96     virtual int stop () const { return 0; }
97     /** @brief stop the target request with the given id obtained by runInput() */
98     virtual int stop_target (int id) const { return -EPERM; }
99
100     /** @brief register model config to the driver */
101     virtual int registerModel (model_config_t *model,
102         uint64_t npu_version = 0) const { return -EPERM; }
103     virtual int deregisterModel (unsigned long long id) const { return -EPERM; }
104
105 #if defined(ENABLE_FPGA_WORKAROUND)
106     virtual int fpga_memcpy (int dmabuf, uint32_t offset,
107         void *addr, size_t size) const { return -EPERM; }
108 #endif
109
110     virtual int getProfile (int task_id, npu_profile *profile) const { return -EPERM; }
111
112     virtual int getStatApps (npu_stat_apps *stat) const { return -EPERM; }
113     virtual int getStatTasks (int appid, npu_stat_tasks *stat) const { return -EPERM; }
114
115   protected:
116     int dev_id_;  /**< device id. assume that 0 <= id < getNUmDevices() */
117     int dev_fd_;  /**< devide fd. opened in constructor and closed in destructor */
118
119   private:
120     /** @brief initialize device API. open the device here */
121     int init ();
122     bool initialized_;
123
124     static uint32_t api_level_; /**< Trinity API level */
125 };
126
127 /** @brief Driver APIs for TRIV2 */
128 class TrinityVision2API : public DriverAPI {
129   public:
130     static int getNumDevices ();
131     TrinityVision2API (int dev_id);
132     ~TrinityVision2API ();
133
134     int open ();
135     int checkSanity ();
136     int getAPILevel (uint32_t *level) const;
137     int getTops (uint32_t *tops) const;
138
139     device_state_t isReady () const;
140     uint32_t numRequests () const;
141
142     int alloc (size_t size, bool contiguous) const;
143     int dealloc (int dmabuf, bool contiguous) const;
144     int getMemoryStatus (size_t *alloc, size_t *free) const;
145
146     void *mmap (int dmabuf, size_t size) const;
147     int munmap (void *addr, size_t size) const;
148
149     int runInput (input_config_t *input) const;
150     int stop () const;
151     int stop_target (int id) const;
152
153     int registerModel (model_config_t *model, uint64_t npu_version) const;
154     int deregisterModel (unsigned long long id) const;
155
156 #if defined(ENABLE_FPGA_WORKAROUND)
157     int fpga_memcpy (int dmabuf, uint32_t offset,
158         void *addr, size_t size) const;
159 #endif
160
161     int getProfile (int task_id, npu_profile *profile) const;
162
163     int getStatApps (npu_stat_apps *stat) const;
164     int getStatTasks (int appid, npu_stat_tasks *stat) const;
165
166   private:
167     int getDrvVersion () const;
168     static const std::string dev_node_base;
169     static std::bitset<CHAR_BIT> dev_bitset;
170
171     std::fstream dev_ios_;
172 };
173
174 #ifdef ENABLE_EMUL
175
176 /** @brief emulation element */
177 class EmulElement;
178 class EmulStat;
179 class EmulTask;
180
181 /** @brief Driver APIs for emulation */
182 class TrinityEmulAPI : public DriverAPI {
183   public:
184     static int getNumDevices ();
185
186     TrinityEmulAPI (int dev_id, dev_type type);
187     ~TrinityEmulAPI ();
188
189     int open ();
190     device_state_t isReady () const;
191
192     int alloc (size_t size, bool contiguous) const;
193     int dealloc (int dmabuf, bool contiguous) const;
194     int getMemoryStatus (size_t *alloc_total, size_t *free_total) const;
195
196     void *mmap (int dmabuf, size_t size) const;
197     int munmap (void *addr, size_t size) const;
198
199     int runInput (input_config_t *input) const;
200     int stop () const;
201     int stop_target (int id) const;
202
203     int registerModel (model_config_t *model, uint64_t npu_version) const;
204     int deregisterModel (unsigned long long id) const;
205
206     int getProfile (int task_id, npu_profile *profile) const;
207     void manipulateProfile (EmulTask *task, npu_profile *profile) const;
208
209   private:
210     static std::atomic<int> global_fd_;
211       /**< global api fd */
212     static ThreadSafeMap<int, EmulElement> elem_map_;
213       /**< dmabuf-to-element map. to track memory allocation */
214     static ThreadSafeMap<int, EmulStat> stat_map_;
215       /**< devfd-to-stat map. to track memory statistics */
216     static ThreadSafeMap<int, EmulTask> task_map_;
217       /**< taskid-to-task map. to support async invoke/stop */
218
219     dev_type dev_type_; /**< emulated device type */
220
221     char *prefix_share_;  /**< prefix of share directory */
222 };
223
224 #endif
225
226 #endif /** __NPU_CORE_NPUdrvAPI_H__ */