1 /*-
2 * Copyright (c) 2010 Isilon Systems, Inc.
3 * Copyright (c) 2010 iX Systems, Inc.
4 * Copyright (c) 2010 Panasas, Inc.
5 * Copyright (c) 2013-2016 Mellanox Technologies, Ltd.
6 * Copyright (c) 2014-2015 François Tigeot
7 * All rights reserved.
8 *
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
11 * are met:
12 * 1. Redistributions of source code must retain the above copyright
13 * notice unmodified, this list of conditions, and the following
14 * disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
20 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
21 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
22 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
23 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
24 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
28 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 *
30 * $FreeBSD$
31 */
32 #ifndef _LINUXKPI_LINUX_KERNEL_H_
33 #define _LINUXKPI_LINUX_KERNEL_H_
34
35 #include "errno.h"
36 #include "string.h"
37 #include "unistd.h"
38 #include "pthread.h"
39 #include "sys/systm.h"
40 #include "sys/time.h"
41 #include "sys/mman.h"
42
43 #include "linux/types.h"
44 #include "linux/slab.h"
45 #include "linux/semaphore.h"
46 #include "linux/atomic.h"
47 #include "linux/spinlock.h"
48 #include "linux/list.h"
49 #include "linux/io.h"
50 #include "linux/compiler.h"
51 #include "user_copy.h"
52 #ifdef LOSCFG_FS_VFS
53 #include "fs/fs.h"
54 #endif
55 #include "los_exc.h"
56
57 #ifdef __cplusplus
58 #if __cplusplus
59 extern "C" {
60 #endif /* __cplusplus */
61 #endif /* __cplusplus */
62
63 #define printk dprintf /* Do not modify for code check */
64
65 #define jiffies 0
66 #ifndef HZ
67 #define HZ LOSCFG_BASE_CORE_TICK_PER_SECOND
68 #endif
69
70 #define SZ_1K (0x00000400)
71 #define __init
72 #define __exit
73 #define __user
74
75 #define ERR_PTR(err) ((void*)(unsigned long)(err))
76 #define PTR_ERR(err) ((unsigned long)(err))
77 #define IS_ERR(err) ((unsigned long)(err) > (unsigned long)-1000L)
78 #define ERR_CAST(err) ((void *)err)
79
80 #define COMPAT_KERN_EMERG "<0>" /* system is unusable */
81 #define COMPAT_KERN_ALERT "<1>" /* action must be taken immediately */
82 #define COMPAT_KERN_CRIT "<2>" /* critical conditions */
83 #define COMPAT_KERN_ERR "<3>" /* error conditions */
84 #define COMPAT_KERN_WARNING "<4>" /* warning conditions */
85 #define COMPAT_KERN_NOTICE "<5>" /* normal but significant condition */
86 #define COMPAT_KERN_INFO "<6>" /* informational */
87 #define COMPAT_KERN_DEBUG "<7>" /* debug-level messages */
88 #define COMPAT_KERN_CONT "<c>"
89
90 #ifndef pr_fmt
91 #define pr_fmt(fmt) fmt
92 #endif
93
94 #define pr_emerg(fmt, ...) \
95 dprintf(KERN_EMERG pr_fmt(fmt), ##__VA_ARGS__)
96 #define pr_alert(fmt, ...) \
97 dprintf(KERN_ALERT pr_fmt(fmt), ##__VA_ARGS__)
98 #define pr_crit(fmt, ...) \
99 dprintf(KERN_CRIT pr_fmt(fmt), ##__VA_ARGS__)
100 #define pr_err(fmt, ...) \
101 dprintf(KERN_ERR pr_fmt(fmt), ##__VA_ARGS__)
102 #define pr_warning(fmt, ...) \
103 dprintf(KERN_WARNING pr_fmt(fmt), ##__VA_ARGS__)
104 #define pr_warn pr_warning
105 #define pr_notice(fmt, ...) \
106 dprintf(KERN_NOTICE pr_fmt(fmt), ##__VA_ARGS__)
107 #define pr_info(fmt, ...) \
108 dprintf(KERN_INFO pr_fmt(fmt), ##__VA_ARGS__)
109 #define pr_cont(fmt, ...) \
110 dprintf(KERN_CONT fmt, ##__VA_ARGS__)
111
112 /* pr_devel() should produce zero code unless DEBUG is defined */
113 #ifdef DEBUG
114 #define pr_devel(fmt, ...) \
115 dprintf(KERN_DEBUG pr_fmt(fmt), ##__VA_ARGS__)
116 #else
117 #define pr_devel(fmt, ...) do {} while (0)
118 #endif
119 /* If you are writing a driver, please use dev_dbg instead */
120 #if defined(DEBUG)
121 #define pr_debug(fmt, ...) \
122 dprintf(KERN_DEBUG pr_fmt(fmt), ##__VA_ARGS__)
123 #else
124 #define pr_debug(fmt, ...) do {} while (0)
125 #endif
126
127 #define WARN_ON(condition) do {} while (0)
128
129 #ifndef min
130 #define min(x, y) ((x) < (y) ? (x) : (y))
131 #endif
132
133 #ifndef max
134 #define max(x, y) ((x) < (y) ? (y) : (x))
135 #endif
136
137 #ifndef min_t
138 #define min_t(t, x, y) ((t)(x) < (t)(y) ? (t)(x) : (t)(y))
139 #endif
140
141 #define BUG() do { \
142 panic("BUG() at %s %d\n", __FUNCTION__, __LINE__); \
143 } while (0)
144
145 #define BUG_ON(condition) do { \
146 if (condition) { \
147 BUG(); \
148 } \
149 } while (0)
150
151 #define __setup(str, fn)
152
153 /**
154 * @ingroup linux_kernel
155 * @brief change jiffies time to tick (not supported).
156 *
157 * @par Description:
158 * This API is used to change jiffies time to tick time.
159 *
160 * @attention
161 * <ul>
162 * <li>None.</li>
163 * </ul>
164 *
165 * @param j [IN] the jiffies time value.
166 *
167 * @retval Tick time value The value of tick time.
168 * @par Dependency:
169 * <ul><li> kernel.h: the header file that contains the API declaration.</li></ul>
170 * @see None.
171 */
172 extern UINT64 jiffies_to_tick(unsigned long j);
173
174 /**
175 * @ingroup linux_kernel
176 * @brief Delay a task.
177 *
178 * @par Description:
179 * This API is used to delay the execution of the current task. The task is able to be scheduled
180 * after it is delayed for a specified number of Ticks.
181 *
182 * @attention
183 * <ul>
184 * <li>The task fails to be delayed if it is being delayed during interrupt processing or it is locked.</li>
185 * <li>If 0 is passed in and the task scheduling is not locked, execute the next task in the queue of
186 * tasks with the priority of the current task.
187 * If no ready task with the priority of the current task is available, the task scheduling will not occur,
188 * and the current task continues to be executed.</li>
189 * </ul>
190 *
191 * @param timeout [IN] Type #signed long Number of Ticks for which the task is delayed(unit: Tick).
192 *
193 * @retval #LOS_ERRNO_SWTMR_HWI_ACTIVE The software timer is being used during an interrupt.
194 * @retval #LOS_OK The task is successfully delayed.
195 * @retval #LOS_ERRNO_SWTMR_NOT_STARTED The software timer is not started.
196 * @par Dependency:
197 * <ul><li>kernel.h: the header file that contains the API declaration.</li></ul>
198 * @see
199 */
200 signed long schedule_timeout(signed long timeout);
201 #define schedule_timeout_uninterruptible(t) schedule_timeout(t)
202 #define schedule_timeout_interruptible(t) schedule_timeout(t)
203
204 #define in_interrupt() (0)
205
206 /**
207 * @ingroup linux_kernel
208 * @brief do division implimentation.
209 *
210 * @par Description:
211 * This API is used to do a division implimentation,and return the remainder
212 *
213 * @attention
214 * <ul>
215 * <li>the param n should point to a valid address.</li>
216 * <li>the param base should not be 0.</li>
217 * </ul>
218 *
219 * @param n [IN/OUT] the dividend as IN,the quotient as OUT
220 * @param base [IN] the divisor
221 * @retval remainder
222 * @par Dependency:
223 * <ul><li>kernel.h: the header file that contains the API declaration.</li></ul>
224 * @see
225 */
226 extern UINT32 do_div_imp(UINT64 *n, UINT32 base);
227
228 /**
229 * @ingroup linux_kernel
230 * @brief do division implimentation.
231 *
232 * @par Description:
233 * This API is used to do a division implimentation,and return the remainder
234 *
235 * @attention
236 * <ul>
237 * <li>the param n should point to a valid address.</li>
238 * <li>the param base should not be 0.</li>
239 * </ul>
240 *
241 * @param n [IN/OUT] the dividend as IN,the quotient as OUT
242 * @param base [IN] the divisor > 0
243 * @retval remainder
244 * @par Dependency:
245 * <ul><li>kernel.h: the header file that contains the API declaration.</li></ul>
246 * @see
247 */
248 extern INT32 do_div_s64_imp(INT64 *n, INT32 base);
249
250 /**
251 * @ingroup linux_kernel
252 * @brief do division implimentation.
253 *
254 * @par Description:
255 * This API is used to do a division implimentation,and return the quotient
256 *
257 * @attention
258 * <ul>
259 * <li>the param divisor should not be 0.</li>
260 * </ul>
261 *
262 * @param dividend [IN] the dividend as IN
263 * @param divisor [IN] the divisor > 0
264 * @retval quotient
265 * @par Dependency:
266 * <ul><li>kernel.h: the header file that contains the API declaration.</li></ul>
267 * @see
268 */
div64_u64(UINT64 dividend,UINT64 divisor)269 static inline UINT64 div64_u64(UINT64 dividend, UINT64 divisor)
270 {
271 return dividend / divisor;
272 }
273
274 /**
275 * @ingroup linux_kernel
276 * @brief do division implimentation.
277 *
278 * @par Description:
279 * This API is used to do a division implimentation,and return the quotient
280 *
281 * @attention
282 * <ul>
283 * <li>the param divisor should not be 0.</li>
284 * </ul>
285 *
286 * @param dividend [IN] the dividend as IN
287 * @param divisor [IN] the divisor not is 0
288 * @retval quotient
289 * @par Dependency:
290 * <ul><li>kernel.h: the header file that contains the API declaration.</li></ul>
291 * @see
292 */
div64_s64(INT64 dividend,INT64 divisor)293 static inline INT64 div64_s64(INT64 dividend, INT64 divisor)
294 {
295 return dividend / divisor;
296 }
297
298 #define do_div(n, base) ({ \
299 UINT32 tmpBase = (base); \
300 UINT32 rem; \
301 rem = ((UINT64)(n)) % tmpBase; \
302 (n) = ((UINT64)(n)) / tmpBase; \
303 rem; \
304 })
305
306 /**
307 * @ingroup linux_kernel
308 * @brief do division implimentation.
309 *
310 * @par Description:
311 * This API is used to do a division implimentation,and return the quotient ,and remainder as OUT
312 *
313 * @attention
314 * <ul>
315 * <li>the param divisor should not be 0.</li>
316 * <li>the param remainder should point to a valid address.</li>
317 * </ul>
318 *
319 * @param dividend [IN] the dividend as IN
320 * @param divisor [IN] the divisor is not 0 ,and as IN
321 * @param remainder [OUT] the remainder should point to a valid address. remainder as OUT
322 * @retval quotient
323 * @par Dependency:
324 * <ul><li>kernel.h: the header file that contains the API declaration and implimentation.</li></ul>
325 * @see
326 */
div_s64_rem(INT64 dividend,INT32 divisor,INT32 * remainder)327 static inline INT64 div_s64_rem(INT64 dividend, INT32 divisor, INT32 *remainder)
328 {
329 *remainder = dividend % divisor;
330 return dividend / divisor;
331 }
332
333 /**
334 * @ingroup linux_kernel
335 * @brief do division implimentation.
336 *
337 * @par Description:
338 * This API is used to do a division implimentation,and return the quotient, and remainder as OUT
339 *
340 * @attention
341 * <ul>
342 * <li>the param divisor should be greater than 0.</li>
343 * <li>the param remainder should point to a valid address.</li>
344 * </ul>
345 *
346 * @param dividend [IN] the dividend as IN
347 * @param divisor [IN] the divisor is greater than 0, and as IN
348 * @param remainder [OUT] the remainder should point to a valid address. remainder as OUT
349 * @retval quotient
350 * @par Dependency:
351 * <ul><li>kernel.h: the header file that contains the API declaration and implimentation.</li></ul>
352 * @see
353 */
div64_u64_rem(UINT64 dividend,UINT64 divisor,UINT64 * remainder)354 static inline UINT64 div64_u64_rem(UINT64 dividend, UINT64 divisor, UINT64 *remainder)
355 {
356 *remainder = dividend % divisor;
357 return dividend / divisor;
358 }
359
360 /**
361 * @ingroup linux_kernel
362 * @brief do division implimentation.
363 *
364 * @par Description:
365 * This API is used to do a division implimentation,and return the quotient,and remainder as OUT
366 *
367 * @attention
368 * <ul>
369 * <li>the param divisor should be greater than 0.</li>
370 * <li>the param remainder should point to a valid address.</li>
371 * </ul>
372 *
373 * @param dividend [IN] the dividend as IN
374 * @param divisor [IN] the divisor is greater than 0, and as IN
375 * @param remainder [OUT] the remainder should point to a valid address. remainder as OUT
376 * @retval quotient
377 * @par Dependency:
378 * <ul><li>kernel.h: the header file that contains the API declaration and implimentation.</li></ul>
379 * @see
380 */
div_u64_rem(UINT64 dividend,UINT32 divisor,UINT32 * remainder)381 static inline UINT64 div_u64_rem(UINT64 dividend, UINT32 divisor, UINT32 *remainder)
382 {
383 *remainder = dividend % divisor;
384 return dividend / divisor;
385 }
386
387 /**
388 * @ingroup linux_kernel
389 * @brief do division implimentation.
390 *
391 * @par Description:
392 * This API is used to do a division implimentation,and return the quotient
393 *
394 * @attention
395 * <ul>
396 * <li>the param divisor should not be 0.</li>
397 * </ul>
398 *
399 * @param dividend [IN] the dividend as IN
400 * @param divisor [IN] the divisor is not 0, and as IN
401 * @retval quotient
402 * @par Dependency:
403 * <ul><li>kernel.h: the header file that contains the API declaration and implimentation.</li></ul>
404 * @see
405 */
div_s64(INT64 dividend,INT32 divisor)406 static inline INT64 div_s64(INT64 dividend, INT32 divisor)
407 {
408 INT32 remainder;
409 return div_s64_rem(dividend, divisor, &remainder);
410 }
411
412 /**
413 * @ingroup linux_kernel
414 * @brief do division implimentation.
415 *
416 * @par Description:
417 * This API is used to do a division implimentation,and return the quotient
418 *
419 * @attention
420 * <ul>
421 * <li>the param divisor should be greater than 0.</li>
422 * </ul>
423 *
424 * @param dividend [IN] the dividend as IN
425 * @param divisor [IN] the divisor is greater than 0, and as IN
426 * @retval quotient
427 * @par Dependency:
428 * <ul><li>kernel.h: the header file that contains the API declaration and implimentation.</li></ul>
429 * @see
430 */
div_u64(UINT64 dividend,UINT32 divisor)431 static inline UINT64 div_u64(UINT64 dividend, UINT32 divisor)
432 {
433 UINT32 remainder;
434 return div_u64_rem(dividend, divisor, &remainder);
435 }
436
copy_from_user(void * to,const void * from,unsigned long n)437 static inline unsigned long copy_from_user(void *to, const void *from, unsigned long n)
438 {
439 if ((to == NULL) || (from == NULL)) {
440 return (unsigned long)-1;
441 }
442
443 return LOS_ArchCopyFromUser(to, from, n);
444 }
445
copy_to_user(void * to,const void * from,unsigned long n)446 static inline unsigned long copy_to_user(void *to, const void *from, unsigned long n)
447 {
448 if ((to == NULL) || (from == NULL)) {
449 return (unsigned long)-1;
450 }
451
452 return LOS_ArchCopyToUser(to, from, n);
453 }
454
455 extern void *ioremap(unsigned long phys_addr, unsigned long size);
456 extern void *ioremap_cached(unsigned long phys_addr, unsigned long size);
457 extern void *ioremap_nocache(unsigned long phys_addr, unsigned long size);
458 extern void iounmap(void *addr);
459 extern int remap_pfn_range(unsigned long addr, unsigned long pfn, unsigned long size, unsigned long prot);
460
461 #ifndef io_remap_pfn_range
462 #define io_remap_pfn_range remap_pfn_range
463 #endif
464
465 #define EXPORT_SYMBOL(x)
466
467 typedef VOID (*unused_func_t)(VOID);
468
469 struct file_operations {
470 struct module *owner;
471 unused_func_t llseek;
472 unused_func_t read;
473 unused_func_t write;
474 unused_func_t aio_read;
475 unused_func_t aio_write;
476 unused_func_t readdir;
477 unused_func_t poll;
478 unused_func_t unlocked_ioctl;
479 unused_func_t compat_ioctl;
480 unused_func_t mmap;
481 unused_func_t open;
482 unused_func_t flush;
483 unused_func_t release;
484 unused_func_t fsync;
485 unused_func_t aio_fsync;
486 unused_func_t fasync;
487 unused_func_t lock;
488 unused_func_t sendpage;
489 unused_func_t get_unmapped_area;
490 unused_func_t check_flags;
491 unused_func_t flock;
492 unused_func_t splice_write;
493 unused_func_t splice_read;
494 unused_func_t setlease;
495 unused_func_t fallocate;
496 };
497
498 #define simple_strtol strtol
499 #define do_gettimeofday(a) gettimeofday(a, NULL)
500 #define DEFINE_MUTEX(m) pthread_mutex_t m = PTHREAD_MUTEX_INITIALIZER;
501 #define mutex_lock pthread_mutex_lock
502 #define mutex_unlock pthread_mutex_unlock
503 #define mutex_init(m) pthread_mutex_init(m, NULL)
504 #define mutex_destroy(m) pthread_mutex_destroy((m))
505
printtime(void)506 static inline void printtime(void)
507 {
508 struct timeval time;
509
510 (VOID)gettimeofday(&time, NULL);
511 PRINTK("[time:%d.%03d]", time.tv_sec, time.tv_usec / 1000); /* 1000: millisecond to microseconds */
512 return;
513 }
514
515 #define TRACETIME() do { \
516 printtime(); \
517 printk("func:%s, line %d\r\n", __FUNCTION__, __LINE__); \
518 } while (0)
519
520 extern void *mmap(void *addr, size_t length, int prot, int flags, int fd, off_t offset);
521 extern int munmap(void *addr, size_t length);
522
523 #define KERN_EMERG COMPAT_KERN_EMERG
524 #define KERN_ALERT COMPAT_KERN_ALERT
525 #define KERN_CRIT COMPAT_KERN_CRIT
526 #define KERN_ERR COMPAT_KERN_ERR
527 #define KERN_WARNING COMPAT_KERN_WARNING
528 #define KERN_NOTICE COMPAT_KERN_NOTICE
529 #define KERN_INFO COMPAT_KERN_INFO
530 #define KERN_DEBUG COMPAT_KERN_DEBUG
531 #define KERN_CONT COMPAT_KERN_CONT
532
533 #ifdef __cplusplus
534 #if __cplusplus
535 }
536 #endif /* __cplusplus */
537 #endif /* __cplusplus */
538
539 #endif /* _LINUXKPI_LINUX_KERNEL_H_ */
540