• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2020 HiSilicon (Shanghai) Technologies CO., LIMITED.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 /**
16  * @defgroup linux_kernel Linux Kernel Adapt
17  * @ingroup linux
18  */
19 
20 #ifndef _LINUX_KERNEL_H
21 #define _LINUX_KERNEL_H
22 
23 #include "errno.h"
24 #include "string.h"
25 #include "unistd.h"
26 #include "sys/time.h"
27 #include "sys/mman.h"
28 #include "linux/types.h"
29 #include "linux/slab.h"
30 #include "asm/semaphore.h"
31 #include "asm/atomic.h"
32 #include "linux/spinlock.h"
33 #include "linux/list.h"
34 #include "linux/io.h"
35 /*
36  * linux/sched/clock.h is included here because the app codes call the inner interface hi_sched_clock
37  * by include the kernel.h, it will be deleted later.
38  */
39 #include "linux/sched/clock.h"
40 #include "asm/io.h"
41 #include "asm/bug.h"
42 #include "mutex.h"
43 #ifdef LOSCFG_FS_VFS
44 #include "fs/fs.h"
45 #endif
46 #include "los_exc.h"
47 #include "los_tick.h"
48 #if defined(LOSCFG_ARCH_ARM_CORTEX_A) || defined(LOSCFG_ARCH_ARM_AARCH64)
49 #include "arch/mmu_pri.h"
50 #endif
51 
52 #ifdef __cplusplus
53 extern "C" {
54 #endif /* __cplusplus */
55 
56 #define printk    dprintf  /* Do not modify for code check */
57 
58 #define jiffies     LOS_TickCountGet()
59 #ifndef HZ
60 #define HZ          KERNEL_TICK_PER_SECOND
61 #endif
62 
63 #define SZ_1K       (0x00000400)
64 #define __init
65 #define __exit
66 #define __user
67 
68 #define ERR_PTR(err) ((void*)(unsigned long)(err))
69 #define PTR_ERR(err) ((unsigned long)(err))
70 #define IS_ERR(err)  ((unsigned long)(err) > (unsigned long)-1000L)
71 
72 #define    COMPAT_KERN_EMERG              "<0>" /* system is unusable */
73 #define    COMPAT_KERN_ALERT              "<1>" /* action must be taken immediately */
74 #define    COMPAT_KERN_CRIT               "<2>" /* critical conditions */
75 #define    COMPAT_KERN_ERR                "<3>" /* error conditions */
76 #define    COMPAT_KERN_WARNING            "<4>" /* warning conditions */
77 #define    COMPAT_KERN_NOTICE             "<5>" /* normal but significant condition */
78 #define    COMPAT_KERN_INFO               "<6>" /* informational */
79 #define    COMPAT_KERN_DEBUG              "<7>" /* debug-level messages */
80 #define    COMPAT_KERN_CONT               "<c>"
81 
82 #ifndef pr_fmt
83 #define pr_fmt(fmt) fmt
84 #endif
85 
86 #define pr_emerg(fmt, ...) \
87     dprintf(KERN_EMERG pr_fmt(fmt), ##__VA_ARGS__)
88 #define pr_alert(fmt, ...) \
89     dprintf(KERN_ALERT pr_fmt(fmt), ##__VA_ARGS__)
90 #define pr_crit(fmt, ...) \
91     dprintf(KERN_CRIT pr_fmt(fmt), ##__VA_ARGS__)
92 #define pr_err(fmt, ...) \
93     dprintf(KERN_ERR pr_fmt(fmt), ##__VA_ARGS__)
94 #define pr_warning(fmt, ...) \
95     dprintf(KERN_WARNING pr_fmt(fmt), ##__VA_ARGS__)
96 #define pr_warn pr_warning
97 #define pr_notice(fmt, ...) \
98     dprintf(KERN_NOTICE pr_fmt(fmt), ##__VA_ARGS__)
99 #define pr_info(fmt, ...) \
100     dprintf(KERN_INFO pr_fmt(fmt), ##__VA_ARGS__)
101 #define pr_cont(fmt, ...) \
102     dprintf(KERN_CONT fmt, ##__VA_ARGS__)
103 
104 /* pr_devel() should produce zero code unless DEBUG is defined */
105 #ifdef DEBUG
106 #define pr_devel(fmt, ...) \
107     dprintf(KERN_DEBUG pr_fmt(fmt), ##__VA_ARGS__)
108 #else
109 #define pr_devel(fmt, ...) do {} while (0)
110 #endif
111 /* If you are writing a driver, please use dev_dbg instead */
112 #if defined(DEBUG)
113 #define pr_debug(fmt, ...) \
114     dprintf(KERN_DEBUG pr_fmt(fmt), ##__VA_ARGS__)
115 #else
116 #define pr_debug(fmt, ...) do {} while (0)
117 #endif
118 
119 #define WARN_ON(condition) do {} while (0)
120 
121 #ifndef min
122 #define min(x, y) ((x) < (y) ? (x) : (y))
123 #endif
124 
125 #ifndef max
126 #define max(x, y) ((x) < (y) ? (y) : (x))
127 #endif
128 
129 #ifndef min_t
130 #define min_t(t, x, y) ((t)(x) < (t)(y) ? (t)(x) : (t)(y))
131 #endif
132 
133 #define BUG_ON(condition) do {  \
134     if (condition) {            \
135         BUG();                  \
136     }                           \
137 } while (0)
138 
139 /* it can not be commented, because it used for business */
140 #define panic  LOS_Panic
141 
142 #define __setup(str, fn)
143 
144 #define in_interrupt()        (0)
145 
146 /**
147  * @ingroup  linux_kernel
148  * @brief do division implimentation.
149  *
150  * @par Description:
151  * This API is used to do a division implimentation, and return the remainder.
152  *
153  * @attention
154  * <ul>
155  * <li>the param n should point to a valid address.</li>
156  * <li>the param base should not be 0.</li>
157  * </ul>
158  *
159  * @param  n    [IN/OUT]     the dividend as IN, the quotient as OUT.
160  * @param  base [IN]         the divisor.
161  * @retval remainder
162  * @par Dependency:
163  * <ul><li>kernel.h: the header file that contains the API declaration.</li></ul>
164  * @see None.
165  * @since Huawei LiteOS V100R001C00
166  */
167 extern UINT32 do_div_imp(UINT64 *n, UINT32 base);
168 
169 /**
170  * @ingroup  linux_kernel
171  * @brief do division implimentation.
172  *
173  * @par Description:
174  * This API is used to do a division implimentation, and return the remainder.
175  *
176  * @attention
177  * <ul>
178  * <li>the param n should point to a valid address.</li>
179  * <li>the param base should not be 0.</li>
180  * </ul>
181  *
182  * @param  n    [IN/OUT]  the dividend as IN, the quotient as OUT.
183  * @param  base [IN]      the divisor > 0.
184  * @retval remainder
185  * @par Dependency:
186  * <ul><li>kernel.h: the header file that contains the API declaration.</li></ul>
187  * @see None.
188  * @since Huawei LiteOS V100R001C00
189  */
190 extern INT32 do_div_s64_imp(INT64 *n, INT32 base);
191 
192 /**
193  * @ingroup  linux_kernel
194  * @brief do division implimentation.
195  *
196  * @par Description:
197  * This API is used to do a division implimentation, and return the quotien.
198  *
199  * @attention
200  * <ul>
201  * <li>the param divisor should not be 0.</li>
202  * </ul>
203  *
204  * @param  dividend [IN]     the dividend as IN.
205  * @param  divisor  [IN]     the divisor > 0.
206  * @retval quotient
207  * @par Dependency:
208  * <ul><li>kernel.h: the header file that contains the API declaration.</li></ul>
209  * @see None.
210  * @since Huawei LiteOS V200R001C00
211  */
div64_u64(UINT64 dividend,UINT64 divisor)212 static inline UINT64 div64_u64(UINT64 dividend, UINT64 divisor)
213 {
214     return dividend / divisor;
215 }
216 
217 /**
218  * @ingroup  linux_kernel
219  * @brief do division implimentation.
220  *
221  * @par Description:
222  * This API is used to do a division implimentation, and return the quotient.
223  *
224  * @attention
225  * <ul>
226  * <li>the param divisor should not be 0.</li>
227  * </ul>
228  *
229  * @param  dividend [IN]     the dividend as IN.
230  * @param  divisor  [IN]     the divisor not is 0.
231  * @retval quotient
232  * @par Dependency:
233  * <ul><li>kernel.h: the header file that contains the API declaration.</li></ul>
234  * @see None.
235  * @since Huawei LiteOS V200R001C00
236  */
div64_s64(INT64 dividend,INT64 divisor)237 static inline INT64 div64_s64(INT64 dividend, INT64 divisor)
238 {
239     return dividend / divisor;
240 }
241 
242 #define do_div(n, base) ({             \
243     UINT32 tmpBase = (base);           \
244     UINT32 rem;                        \
245     rem = ((UINT64)(n)) % tmpBase;     \
246     (n) = ((UINT64)(n)) / tmpBase;     \
247     rem;                               \
248 })
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, and remainder as OUT.
256  *
257  * @attention
258  * <ul>
259  * <li>the param divisor should not be 0.</li>
260  * <li>the param remainder should point to a valid address.</li>
261  * </ul>
262  *
263  * @param  dividend  [IN]    the dividend as IN.
264  * @param  divisor   [IN]    the divisor is not 0, and as IN.
265  * @param  remainder [OUT]   the remainder should point to a valid address. remainder as OUT.
266  * @retval quotient
267  * @par Dependency:
268  * <ul><li>kernel.h: the header file that contains the API declaration and implimentation.</li></ul>
269  * @see None.
270  * @since Huawei LiteOS V100R001C00
271  */
div_s64_rem(INT64 dividend,INT32 divisor,INT32 * remainder)272 static inline INT64 div_s64_rem(INT64 dividend, INT32 divisor, INT32 *remainder)
273 {
274     *remainder = dividend % divisor;
275     return dividend / divisor;
276 }
277 
278 /**
279  * @ingroup  linux_kernel
280  * @brief do division implimentation.
281  *
282  * @par Description:
283  * This API is used to do a division implimentation, and return the quotient, and remainder as OUT.
284  *
285  * @attention
286  * <ul>
287  * <li>the param divisor should be greater than 0.</li>
288  * <li>the param remainder should point to a valid address.</li>
289  * </ul>
290  *
291  * @param  dividend  [IN]    the dividend as IN.
292  * @param  divisor   [IN]    the divisor is greater than 0, and as IN.
293  * @param  remainder [OUT]   the remainder should point to a valid address. remainder as OUT.
294  * @retval quotient
295  * @par Dependency:
296  * <ul><li>kernel.h: the header file that contains the API declaration and implimentation.</li></ul>
297  * @see None.
298  * @since Huawei LiteOS V100R001C00
299  */
div64_u64_rem(UINT64 dividend,UINT64 divisor,UINT64 * remainder)300 static inline UINT64 div64_u64_rem(UINT64 dividend, UINT64 divisor, UINT64 *remainder)
301 {
302     *remainder = dividend % divisor;
303     return dividend / divisor;
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 be greater than 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 greater than 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 None.
326  * @since Huawei LiteOS V100R001C00
327  */
div_u64_rem(UINT64 dividend,UINT32 divisor,UINT32 * remainder)328 static inline UINT64 div_u64_rem(UINT64 dividend, UINT32 divisor, UINT32 *remainder)
329 {
330     *remainder = dividend % divisor;
331     return dividend / divisor;
332 }
333 
334 /**
335  * @ingroup  linux_kernel
336  * @brief do division implimentation.
337  *
338  * @par Description:
339  * This API is used to do a division implimentation, and return the quotient.
340  *
341  * @attention
342  * <ul>
343  * <li>the param divisor should not be 0.</li>
344  * </ul>
345  *
346  * @param dividend [IN]     the dividend as IN
347  * @param divisor  [IN]     the divisor is not 0, and as IN.
348  * @retval quotient
349  * @par Dependency:
350  * <ul><li>kernel.h: the header file that contains the API declaration and implimentation.</li></ul>
351  * @see None.
352  * @since Huawei LiteOS V100R001C00
353  */
div_s64(INT64 dividend,INT32 divisor)354 static inline INT64 div_s64(INT64 dividend, INT32 divisor)
355 {
356     INT32 remainder;
357     return div_s64_rem(dividend, divisor, &remainder);
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.
366  *
367  * @attention
368  * <ul>
369  * <li>the param divisor should be greater than 0.</li>
370  * </ul>
371  *
372  * @param dividend [IN]     the dividend as IN.
373  * @param divisor  [IN]     the divisor is greater than 0, and as IN.
374  * @retval quotient
375  * @par Dependency:
376  * <ul><li>kernel.h: the header file that contains the API declaration and implimentation.</li></ul>
377  * @see None.
378  * @since Huawei LiteOS V100R001C00
379  */
div_u64(UINT64 dividend,UINT32 divisor)380 static inline UINT64 div_u64(UINT64 dividend, UINT32 divisor)
381 {
382     UINT32 remainder;
383     return div_u64_rem(dividend, divisor, &remainder);
384 }
385 
copy_from_user(void * to,const void * from,unsigned long n)386 static inline unsigned long copy_from_user(void *to, const void *from, unsigned long n)
387 {
388     if ((to == NULL) || (from == NULL)) {
389         return (unsigned long)-1;
390     }
391     (VOID)memcpy(to, from, n);
392     return 0;
393 }
394 
copy_to_user(void * to,const void * from,unsigned long n)395 static inline unsigned long copy_to_user(void *to, const void *from, unsigned long n)
396 {
397     if ((to == NULL) || (from == NULL)) {
398         return (unsigned long)-1;
399     }
400     (VOID)memcpy(to, from, n);
401     return 0;
402 }
403 
ioremap_cached(unsigned long physAddr,unsigned int size)404 static inline void *ioremap_cached(unsigned long physAddr, unsigned int size)
405 {
406 #if defined(LOSCFG_ARCH_ARM_CORTEX_A) || defined(LOSCFG_ARCH_ARM_AARCH64)
407     return (void *)OsCachedRemap(physAddr, size);
408 #else
409     (void)size;
410     return (void *)(UINTPTR)physAddr;
411 #endif
412 }
413 
ioremap_nocache(unsigned long physAddr,unsigned long size)414 static inline void *ioremap_nocache(unsigned long physAddr, unsigned long size)
415 {
416 #if defined(LOSCFG_ARCH_ARM_CORTEX_A) || defined(LOSCFG_ARCH_ARM_AARCH64)
417     return (void *)OsNoCachedRemap(physAddr, size);
418 #else
419     (void)size;
420     return (void *)(UINTPTR)physAddr;
421 #endif
422 }
423 
iounmap(void * addr)424 static inline void iounmap(void *addr)
425 {
426 }
427 
428 #define likely(x)       (x)
429 #define unlikely(x)     (x)
430 #define EXPORT_SYMBOL(x)
431 
432 typedef VOID (*unused_func_t)(VOID);
433 
434 struct file_operations {
435     struct module *owner;
436     unused_func_t   llseek;
437     unused_func_t   read;
438     unused_func_t   write;
439     unused_func_t   aio_read;
440     unused_func_t   aio_write;
441     unused_func_t   readdir;
442     unused_func_t   poll;
443     unused_func_t   unlocked_ioctl;
444     unused_func_t   compat_ioctl;
445     unused_func_t   mmap;
446     unused_func_t   open;
447     unused_func_t   flush;
448     unused_func_t   release;
449     unused_func_t   fsync;
450     unused_func_t   aio_fsync;
451     unused_func_t   fasync;
452     unused_func_t   lock;
453     unused_func_t   sendpage;
454     unused_func_t   get_unmapped_area;
455     unused_func_t   check_flags;
456     unused_func_t   flock;
457     unused_func_t   splice_write;
458     unused_func_t   splice_read;
459     unused_func_t   setlease;
460     unused_func_t   fallocate;
461 };
462 
463 #define simple_strtol       strtol
464 #define do_gettimeofday(a)  gettimeofday(a, NULL)
465 #define DEFINE_MUTEX(m)     pthread_mutex_t m
466 #define mutex_lock          pthread_mutex_lock
467 #define mutex_unlock        pthread_mutex_unlock
468 #define mutex_init(m)       pthread_mutex_init(m, NULL)
469 #define mutex_destroy(m)    pthread_mutex_destroy((m))
470 
printtime(void)471 static inline void printtime(void)
472 {
473     struct timeval time;
474 
475     (VOID)gettimeofday(&time, NULL);
476     PRINT_INFO("[time:%ld.%03ld]", time.tv_sec, time.tv_usec / 1000); /* 1000: millisecond to microseconds */
477     return;
478 }
479 
480 #define TRACETIME() printtime()
481 
482 extern void *mmap(void *addr, size_t length, int prot, int flags, int fd, off_t offset);
483 extern int munmap(void *addr, size_t length);
484 
485 #define    KERN_EMERG              COMPAT_KERN_EMERG
486 #define    KERN_ALERT              COMPAT_KERN_ALERT
487 #define    KERN_CRIT               COMPAT_KERN_CRIT
488 #define    KERN_ERR                COMPAT_KERN_ERR
489 #define    KERN_WARNING            COMPAT_KERN_WARNING
490 #define    KERN_NOTICE             COMPAT_KERN_NOTICE
491 #define    KERN_INFO               COMPAT_KERN_INFO
492 #define    KERN_DEBUG              COMPAT_KERN_DEBUG
493 #define    KERN_CONT               COMPAT_KERN_CONT
494 
495 /**
496  * @ingroup linux_kernel
497  * @brief Gets the address of the container structure.
498  *
499  * @par Description:
500  * This API gets the address of the structure.
501  * @attention
502  * None.
503  *
504  * @param ptr     [IN] The pointer to the #member.
505  * @param type    [IN] The type of structure.
506  * @param member  [IN] The name of the #member in the structure.
507  *
508  * @retval The pointer to the address of the container structure.
509  * @par Dependency:
510  * <ul><li>kernel.h: the header file that contains the API declaration.</li></ul>
511  * @see None.
512  */
513 #define container_of    LOS_DL_LIST_ENTRY
514 
515 #ifdef __cplusplus
516 }
517 #endif /* __cplusplus */
518 
519 #endif /* _LINUX_KERNEL_H */
520