1 /******************************************************************************
3 * Copyright (c) 2019 Intel.
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
17 *******************************************************************************/
23 #include <linux/types.h>
24 #include <linux/module.h>
25 #include <linux/device.h>
26 #include <linux/ioctl.h>
27 #include <linux/cdev.h>
28 #include <linux/wait.h>
29 #include <linux/sched.h>
30 #define MODNAME (KBUILD_MODNAME)
31 #else /* __KERNEL__ */
32 #include <sys/ioctl.h>
34 #include <semaphore.h>
35 #include <rte_common.h>
36 #include <rte_atomic.h>
37 #include <rte_memzone.h>
43 #define WLS_PRINT(format, args...) printk(format, ##args)
44 #define WLS_ERROR(format, args...) printk(KERN_ERR "wls err: " format,##args)
47 #define WLS_DEBUG(format, args...) \
49 printk(KERN_INFO "wls debug: " format,##args); \
52 #define WLS_DEBUG(format, args...) do { } while(0)
55 /******************************************************************************
56 * Module error codes *
57 ******************************************************************************/
58 #define WLS_RC_MDMA_ID_ERROR (-1)
59 #define WLS_RC_MDMA_TASK_ERROR (-2)
60 #define WLS_RC_ALLOC_DELAY_MEM_ERROR (-3)
61 #define WLS_RC_ALLOC_BAR_MEM_ERROR (-4)
62 #define WLS_RC_ALLOC_TAR_MEM_ERROR (-5)
63 #define WLS_RC_PARAM_SIZE_ERROR (-6)
64 #define WLS_RC_WLS_HEAP_ALLOC_ERROR (-7)
65 #define WLS_RC_IRQ_ALLOC_ERROR (-8)
66 #define WLS_RC_DMA_ALLOC_ERROR (-9)
67 #define WLS_RC_TRANSACTION_ERROR (-10)
68 #define WLS_RC_PHY_CTX_ERROR (-11)
69 #define WLS_RC_KERNEL_HEAP_ALLOC_ERROR (-12)
70 #define WLS_RC_CONFIGURATION_ERROR (-13)
71 #define WLS_RC_THREAD_CREATION_ERROR (-14)
73 #define WLS_IOC_MAGIC 'W'
74 #define WLS_IOC_OPEN _IOWR(WLS_IOC_MAGIC, WLS_IOC_OPEN_NO, uint64_t)
75 #define WLS_IOC_CLOSE _IOWR(WLS_IOC_MAGIC, WLS_IOC_CLOSE_NO, uint64_t)
76 #define WLS_IOC_PUT _IOWR(WLS_IOC_MAGIC, WLS_IOC_PUT_NO, uint64_t)
77 #define WLS_IOC_EVENT _IOWR(WLS_IOC_MAGIC, WLS_IOC_EVENT_NO, uint64_t)
78 #define WLS_IOC_WAIT _IOWR(WLS_IOC_MAGIC, WLS_IOC_WAIT_NO, uint64_t)
79 #define WLS_IOC_WAKE_UP _IOWR(WLS_IOC_MAGIC, WLS_IOC_WAKE_UP_NO, uint64_t)
80 #define WLS_IOC_CONNECT _IOWR(WLS_IOC_MAGIC, WLS_IOC_CONNECT_NO, uint64_t)
81 #define WLS_IOC_FILL _IOWR(WLS_IOC_MAGIC, WLS_IOC_FILL_NO, uint64_t)
102 #define WLS_US_CLIENTS_MAX 64
104 #define CACHE_LINE_SIZE 64 /**< Cache line size. */
105 #define CACHE_LINE_MASK (CACHE_LINE_SIZE-1) /**< Cache line mask. */
107 #define CACHE_LINE_ROUNDUP(size) \
108 (CACHE_LINE_SIZE * ((size + CACHE_LINE_SIZE - 1) / CACHE_LINE_SIZE))
110 #define DMA_ALIGNMENT_SIZE 256L
112 // To make DMA we make sure that block starts on 256 bytes boundary
113 #define DMA_ALIGNMENT_ROUNDUP(size) \
114 (DMA_ALIGNMENT_SIZE * ((size + DMA_ALIGNMENT_SIZE - 1) / DMA_ALIGNMENT_SIZE))
116 /**< Return the first cache-aligned value greater or equal to size. */
119 * Force alignment to cache line.
121 #define __wls_cache_aligned __attribute__((__aligned__(CACHE_LINE_SIZE)))
123 #define WLS_HUGE_DEF_PAGE_SIZE 0x40000000LL
124 #define WLS_IS_ONE_HUGE_PAGE(ptr, size, hp_size) ((((unsigned long long)ptr & (~(hp_size - 1)))\
125 == (((unsigned long long)ptr + size - 1) & (~(hp_size - 1)))) ? 1 : 0)
127 typedef struct hugepage_tabl_s
131 uint64_t padding_pageVa;
136 #define DMA_MAP_MAX_BLOCK_SIZE 64*1024
137 #define MAX_N_HUGE_PAGES 512
138 #define UL_FREE_BLOCK_QUEUE_SIZE 384
140 #define WLS_GET_QUEUE_N_ELEMENTS 384
141 #define WLS_PUT_QUEUE_N_ELEMENTS 384
143 #define WLS_DEV_SHM_NAME_LEN RTE_MEMZONE_NAMESIZE
147 typedef struct wls_wait_req_s {
148 uint64_t wls_us_kernel_va;
155 typedef struct wls_sema_priv_s
158 rte_atomic16_t is_irq;
159 wls_wait_req_t drv_block[FIFO_LEN];
160 volatile unsigned int drv_block_put;
161 volatile unsigned int drv_block_get;
164 typedef struct wls_us_priv_s
166 wls_sema_priv_t sema;
172 typedef struct wls_us_ctx_s
175 void * wls_us_user_space_va;
176 uint64_t padding_wls_us_user_space_va;
179 uint64_t wls_us_kernel_va;
183 uint32_t wls_us_ctx_size;
184 uint32_t HugePageSize;
188 uint64_t padding_alloc_buffer;
191 hugepage_tabl_t hugepageTbl [MAX_N_HUGE_PAGES];
193 FASTQUEUE ul_free_block_pq;
194 uint64_t ul_free_block_storage[UL_FREE_BLOCK_QUEUE_SIZE * sizeof(uint64_t)];
196 WLS_MSG_QUEUE get_queue;
197 WLS_MSG_HANDLE get_storage[WLS_GET_QUEUE_N_ELEMENTS];
199 WLS_MSG_QUEUE put_queue;
200 WLS_MSG_HANDLE put_storage[WLS_PUT_QUEUE_N_ELEMENTS];
202 uint64_t freePtrList[UL_FREE_BLOCK_QUEUE_SIZE * sizeof(uint64_t)];
203 uint32_t freeListIndex;
206 // dst userspace context address (kernel va)
207 uint64_t dst_kernel_va;
208 // dst userspace context address (local user sapce va)
210 volatile uint64_t dst_user_va;
211 // dst userspace context address (local user sapce va)
212 volatile uint64_t dst_pa;
215 wls_us_priv_t wls_us_private;
218 char wls_dev_name[WLS_DEV_SHM_NAME_LEN];
219 char wls_shm_name[WLS_DEV_SHM_NAME_LEN];
224 typedef struct wls_fill_req_s {
225 uint64_t wls_us_kernel_va;
231 typedef struct wls_connect_req_s {
232 uint64_t wls_us_kernel_va;
235 typedef struct wls_drv_ctx_s
238 uint32_t us_ctx_cout;
239 wls_us_ctx_t p_wls_us_ctx[WLS_US_CLIENTS_MAX];
240 wls_us_ctx_t p_wls_us_pa_ctx[WLS_US_CLIENTS_MAX];
241 uint32_t nWlsClients;
242 pthread_mutex_t mng_mutex;
245 typedef struct wls_open_req_s {
251 typedef struct wls_close_req_s {
257 typedef enum wls_events_num_s {
258 WLS_EVENT_IA_READY = 0,
264 typedef struct wls_event_req_s {
265 uint64_t wls_us_kernel_va;
266 uint64_t event_to_wls;
267 uint64_t event_param;
270 typedef struct wls_put_req_s {
271 uint64_t wls_us_kernel_va;
274 typedef struct wls_wake_up_req_s {
275 uint64_t wls_us_kernel_va;
281 #define SYS_CPU_CLOCK (2300000000L)
282 #define CLOCK_PER_MS (SYS_CPU_CLOCK/1000)
283 #define CLOCK_PER_US (SYS_CPU_CLOCK/1000000)
285 static inline uint64_t
296 asm volatile("rdtsc" :
302 static inline uint64_t rdtsc_ticks_diff(unsigned long curr, unsigned long prev)
305 return (unsigned long)(curr - prev);
307 return (unsigned long)(0xFFFFFFFFFFFFFFFF - prev + curr);
310 void wls_show_data(void* ptr, unsigned int size);
311 void *wls_get_sh_ctx(void);
312 void *wls_get_sh_ctx_pa(void);
314 #endif /* __WLS_H__*/