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>
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
19 #ifndef __NPU_CORE_NPUdrvAPI_H__
20 #define __NPU_CORE_NPUdrvAPI_H__
26 #include <misc/trinity.h>
32 #include <bits/stdc++.h>
36 #include <sys/user.h> /* PAGE_SIZE */
37 /** the size of each allocation is aligned to PAGE_SIZE */
39 #define PAGE_SHIFT (12)
42 #define PAGE_SIZE (1UL << PAGE_SHIFT)
45 #define PFN_UP(x) (((x) + PAGE_SIZE - 1) >> PAGE_SHIFT)
47 #define ALIGNED_SIZE(x) (PFN_UP(x) * PAGE_SIZE)
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;
54 /** @brief class for Driver API. Each device should implement its APIs */
57 DriverAPI (int dev_id);
58 virtual ~DriverAPI () {}
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_; }
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; }
72 virtual int checkSanity () { return 0; }
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; }
78 /** @brief return a number of requests submitted */
79 virtual uint32_t numRequests () const { return 0; }
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; }
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; }
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; }
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; }
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; }
110 virtual int getProfile (int task_id, npu_profile *profile) const { return -EPERM; }
112 virtual int getStatApps (npu_stat_apps *stat) const { return -EPERM; }
113 virtual int getStatTasks (int appid, npu_stat_tasks *stat) const { return -EPERM; }
116 int dev_id_; /**< device id. assume that 0 <= id < getNUmDevices() */
117 int dev_fd_; /**< devide fd. opened in constructor and closed in destructor */
120 /** @brief initialize device API. open the device here */
124 static uint32_t api_level_; /**< Trinity API level */
127 /** @brief Driver APIs for TRIV2 */
128 class TrinityVision2API : public DriverAPI {
130 static int getNumDevices ();
131 TrinityVision2API (int dev_id);
132 ~TrinityVision2API ();
136 int getAPILevel (uint32_t *level) const;
137 int getTops (uint32_t *tops) const;
139 device_state_t isReady () const;
140 uint32_t numRequests () const;
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;
146 void *mmap (int dmabuf, size_t size) const;
147 int munmap (void *addr, size_t size) const;
149 int runInput (input_config_t *input) const;
151 int stop_target (int id) const;
153 int registerModel (model_config_t *model, uint64_t npu_version) const;
154 int deregisterModel (unsigned long long id) const;
156 #if defined(ENABLE_FPGA_WORKAROUND)
157 int fpga_memcpy (int dmabuf, uint32_t offset,
158 void *addr, size_t size) const;
161 int getProfile (int task_id, npu_profile *profile) const;
163 int getStatApps (npu_stat_apps *stat) const;
164 int getStatTasks (int appid, npu_stat_tasks *stat) const;
167 int getDrvVersion () const;
168 static const std::string dev_node_base;
169 static std::bitset<CHAR_BIT> dev_bitset;
171 std::fstream dev_ios_;
176 /** @brief emulation element */
181 /** @brief Driver APIs for emulation */
182 class TrinityEmulAPI : public DriverAPI {
184 static int getNumDevices ();
186 TrinityEmulAPI (int dev_id, dev_type type);
190 device_state_t isReady () const;
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;
196 void *mmap (int dmabuf, size_t size) const;
197 int munmap (void *addr, size_t size) const;
199 int runInput (input_config_t *input) const;
201 int stop_target (int id) const;
203 int registerModel (model_config_t *model, uint64_t npu_version) const;
204 int deregisterModel (unsigned long long id) const;
206 int getProfile (int task_id, npu_profile *profile) const;
207 void manipulateProfile (EmulTask *task, npu_profile *profile) const;
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 */
219 dev_type dev_type_; /**< emulated device type */
221 char *prefix_share_; /**< prefix of share directory */
226 #endif /** __NPU_CORE_NPUdrvAPI_H__ */