• 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  * Description: OS Abstract Layer.
15  */
16 
17 /**
18  * @defgroup osal_addr osal_addr
19  */
20 #ifndef __OSAL_ADDR_H__
21 #define __OSAL_ADDR_H__
22 
23 #ifdef __cplusplus
24 #if __cplusplus
25 extern "C" {
26 #endif
27 #endif
28 
29 #define OSAL_GFP_ZERO (0x1)
30 #define OSAL_GFP_ATOMIC (0x1 << 1)
31 #define OSAL_GFP_DMA (0x1 << 2)
32 #define OSAL_GFP_KERNEL (0x1 << 3)
33 
34 #define OSAL_VERIFY_READ 0
35 #define OSAL_VERIFY_WRITE 1
36 
37 /**
38  * @ingroup osal_addr
39  * @brief Alloc dynamic memory.
40  *
41  * @par Description:
42  * This API is used to alloc a memory block of which the size is specified.
43  *
44  * @param size [in] How many bytes of memory are required.
45  * @param osal_gfp_flag [in] The type of memory to alloc. This parameter is not used in liteos and freertos.
46  * In linux, it must include one of the following access modes: OSAL_GFP_ATOMIC, OSAL_GFP_DMA, OSAL_GFP_KERNEL.
47  * OSAL_GFP_ZERO can be bitwise-or'd in flags, Then, the memory is set to zero.
48  *
49  * @par Support System:
50  * linux liteos freertos.
51  */
52 void *osal_kmalloc(unsigned long size, unsigned int osal_gfp_flag);
53 
54 /**
55  * @ingroup osal_addr
56  * @brief Alloc dynamic memory.
57  *
58  * @par Description:
59  * This API is used to alloc a memory block of which the size is specified, and set the momory block to 0.
60  *
61  * @param size [in] How many bytes of memory are required.
62  * @param osal_gfp_flag [in] The type of memory to alloc. This parameter is not used in liteos and freertos.
63  * In linux, it must include one of the following access modes: OSAL_GFP_ATOMIC, OSAL_GFP_DMA, OSAL_GFP_KERNEL.
64  * OSAL_GFP_ZERO can be bitwise-or'd in flags, Then, the memory is set to zero.
65  *
66  * @par Support System:
67  * linux liteos freertos.
68  */
69 void *osal_kzalloc(unsigned long size, unsigned int osal_gfp_flag);
70 
71 /**
72  * @ingroup osal_addr
73  * @brief alloc aligned dynamic memory.
74  *
75  * @par Description:
76  * This API is used to alloc memory blocks of specified size and of which the starting addresses are aligned on
77  * a specified boundary.
78  *
79  * @attention
80  * The alignment parameter value must be a power of 2 with the minimum value being 4.
81  *
82  * @param size [in] Size of the memory block to be allocd (unit: byte).
83  * @param osal_gfp_flag [in] The type of memory to alloc. This parameter is not used in liteos and freertos.
84  * In linux, it must include one of the following access modes: OSAL_GFP_ATOMIC, OSAL_GFP_DMA, OSAL_GFP_KERNEL.
85  * OSAL_GFP_ZERO can be bitwise-or'd in flags, Then, the memory is set to zero.
86  * @param boundary [in] Boundary on which the memory is aligned. (unit: byte).
87  *
88  * @par Support System:
89  * liteos, freertos.
90  */
91 void *osal_kmalloc_align(unsigned int size, unsigned int osal_gfp_flag, unsigned int boundary);
92 
93 /**
94  * @ingroup osal_addr
95  * @brief alloc aligned dynamic memory.
96  *
97  * @par Description:
98  * This API is used to alloc memory blocks of specified size and of which the starting addresses are aligned on
99  * a specified boundary. The momory block will set to 0.
100  *
101  * @attention
102  * The alignment parameter value must be a power of 2 with the minimum value being 4.
103  *
104  * @param size [in] Size of the memory block to be allocd (unit: byte).
105  * @param osal_gfp_flag [in] The type of memory to alloc. This parameter is not used in liteos and freertos.
106  * In linux, it must include one of the following access modes: OSAL_GFP_ATOMIC, OSAL_GFP_DMA, OSAL_GFP_KERNEL.
107  * OSAL_GFP_ZERO can be bitwise-or'd in flags, Then, the memory is set to zero.
108  * @param boundary [in] Boundary on which the memory is aligned. (unit: byte).
109  *
110  * @par Support System:
111  * liteos, freertos.
112  */
113 void *osal_kzalloc_align(unsigned int size, unsigned int osal_gfp_flag, unsigned int boundary);
114 
115 /**
116  * @ingroup osal_addr
117  * @brief Free dynamic memory.
118  *
119  * @par Description:
120  * This API is used to free specified dynamic memory that has been allocd and update module mem used.
121  *
122  * @param addr [in] Starting address of the memory block to be freed.
123  *
124  * @par Support System:
125  * linux liteos freertos.
126  */
127 void osal_kfree(void *addr);
128 
129 /**
130  * @ingroup osal_addr
131  * @brief alloc virtually contiguous memory.
132  *
133  * @par Description:
134  * This API is used to alloc memory space with consecutive virtual addresses.
135  *
136  * @param size [in] How many bytes of memory are required.
137  *
138  * @return Returns the pointer to the virtual memory.
139  *
140  * @par Support System:
141  * linux liteos freertos.
142  */
143 void *osal_vmalloc(unsigned long size);
144 
145 /**
146  * @ingroup osal_addr
147  * @brief alloc virtually contiguous memory.
148  *
149  * @par Description:
150  * This API is used to alloc memory space with consecutive virtual addresses. The momory space will set to 0.
151  *
152  * @param size [in] How many bytes of memory are required.
153  *
154  * @return Returns the pointer to the virtual memory.
155  *
156  * @par Support System:
157  * linux liteos freertos.
158  */
159 void *osal_vzalloc(unsigned long size);
160 
161 /**
162  * @ingroup osal_addr
163  * @brief release memory allocd by vmalloc().
164  *
165  * @par Description:
166  * This API is used to release memory allocd by osal_vmalloc().
167  *
168  * @param addr [in] Starting address of the memory block to be freed.
169  *
170  * @par Support System:
171  * linux liteos freertos.
172  */
173 void osal_vfree(void *addr);
174 
175 /**
176  * @ingroup osal_addr
177  * @brief Initialize dynamic memory.
178  *
179  * @par Description:
180  * This API is used to initialize the dynamic memory of a doubly linked list.
181  *
182  * @attention
183  * The size parameter value should match the following two conditions:\n
184  * 1) Be less than or equal to the Memory pool size;\n
185  * 2) Be greater than the size of min_pool_size of each system.\n
186  * Call this API when dynamic memory needs to be initialized during the startup of LiteOS.
187  * The parameter input must be 4 or 8 byte-aligned.
188  * The init area [pool, pool + size] should not conflict with other pools.
189  * This function is defined only when LOSCFG_MEM_MUL_MODULE is defined in liteos.
190  * This function is defined only when XLTCFG_SUPPORT_MEMMNG is defined in freertos.
191  *
192  * @param pool [in] Starting address of memory.
193  * @param size [in] Memory size.
194  *
195  * @return OSAL_SUCCESS/OSAL_FAILURE
196  *
197  * @par Support System:
198  * liteos, seliteos, freertos.
199  */
200 int osal_pool_mem_init(void *pool, unsigned int size);
201 
202 /**
203  * @ingroup osal_addr
204  * @brief alloc dynamic memory.
205  *
206  * @par Description:
207  * This API is used to alloc a memory block of which the size is specified and update module mem used.
208  *
209  * @attention
210  * The input pool parameter must be initialized via func osal_pool_mem_init.
211  * The size of the input parameter size can not be greater than the memory pool size that specified at the second
212  * input parameter of osal_pool_mem_init.
213  * The size of the input parameter size must be four byte-aligned.
214  * This function is defined only when LOSCFG_MEM_MUL_MODULE is defined in liteos.
215  * This function is defined only when XLTCFG_SUPPORT_MEMMNG is defined in freertos.
216  *
217  * @param pool [in] Pointer to the memory pool that contains the memory block to be allocd.
218  * @param size [in] Size of the memory block to be allocd (unit: byte).
219  *
220  * @retval NULL The memory fails to be allocd.
221  * @retval VOID* The memory is successfully allocd, and the API returns the pointer to the allocd memory block.
222  *
223  * @par Support System:
224  * liteos, seliteos, freertos.
225  */
226 void *osal_pool_mem_alloc(void *pool, unsigned int size);
227 
228 /**
229  * @ingroup osal_addr
230  * @brief alloc aligned memory.
231  *
232  * @par Description:
233  * This API is used to alloc memory blocks of specified size and of which the starting addresses are aligned on
234  * a specified boundary and update module mem used.
235  *
236  * @attention
237  * The input pool parameter must be initialized via func osal_pool_mem_init.
238  * The size of the input parameter size can not be greater than the memory pool size that specified at the second
239  * input parameter of osal_pool_mem_init.
240  * The alignment parameter value must be a power of 2 with the minimum value being 4.
241  * This function is defined only when LOSCFG_MEM_MUL_MODULE is defined in liteos.
242  * This function is defined only when XLTCFG_SUPPORT_MEMMNG is defined in freertos.
243  *
244  * @param pool [in] Pointer to the memory pool that contains the memory block to be allocd.
245  * @param size [in] Size of the memory block to be allocd (unit: byte).
246  * @param boundary [in] Boundary on which the memory is aligned. (unit: byte).
247  *
248  * @par Support System:
249  * liteos, seliteos, freertos.
250  */
251 void *osal_pool_mem_alloc_align(void *pool, unsigned int size, unsigned int boundary);
252 
253 /**
254  * @ingroup osal_addr
255  * @brief Free dynamic memory.
256  *
257  * @par Description:
258  * This API is used to free specified dynamic memory that has been allocd.
259  *
260  * @attention
261  * The input pool parameter must be initialized via func osal_pool_mem_init.
262  * The input addr parameter must be allocd by osal_pool_mem_alloc or osal_pool_mem_alloc_align.
263  * This function is defined only when LOSCFG_MEM_MUL_MODULE is defined in liteos.
264  * This function is defined only when XLTCFG_SUPPORT_MEMMNG is defined in freertos.
265  *
266  * @param  pool  [in] Pointer to the memory pool that contains the dynamic memory block to be freed.
267  * @param  addr  [in] Starting address of the memory block to be freed.
268  *
269  * @par Support System:
270  * liteos, seliteos, freertos.
271  */
272 void osal_pool_mem_free(void *pool, const void *addr);
273 
274 /**
275  * @ingroup osal_addr
276  * @brief Deinitialize dynamic memory.
277  *
278  * @par Description:
279  * This API is used to deinitialize the dynamic memory of a doubly linked list.
280  *
281  * @attention
282  * This function is defined only when LOSCFG_MEM_MUL_POOL is defined in liteos.
283  * This function is defined only when XLTCFG_SUPPORT_MEMMNG and  XLTCFG_MEM_MUL_POOL are defined in freertos.
284  *
285  * @param pool [in] Starting address of memory.
286  *
287  * @retval OSAL_FAILURE The dynamic memory fails to be deinitialized.
288  * @retval OSAL_SUCCESS The dynamic memory is successfully deinitialized.
289  *
290  * @par Support System:
291  * liteos, seliteos, freertos.
292  */
293 int osal_pool_mem_deinit(void *pool);
294 
295 typedef enum {
296     OSAL_BLOCKMEM_VALID = 0,
297     OSAL_BLOCKMEM_INVALID_PHYADDR = 1,
298     OSAL_BLOCKMEM_INVALID_SIZE = 2,
299     OSAL_BLOCKMEM_MAX,
300 } osal_blockmem_status;
301 
302 /**
303  * @ingroup osal_addr
304  * @brief Get the block mem status.
305  *
306  * @par Description:
307  * Get the block mem status.
308  *
309  * @param phyaddr physical address
310  * @param size Size of the address to be manipulated
311  *
312  * @retval OSAL_BLOCKMEM_VALID The block mem is valid.
313  * @retval OSAL_BLOCKMEM_INVALID_PHYADDR The block mem is invalid.
314  * @retval OSAL_BLOCKMEM_INVALID_SIZE The block mem is invalid.
315  *
316  * @par Support System:
317  * linux.
318  */
319 osal_blockmem_status osal_blockmem_get_status(unsigned long phyaddr, unsigned int size);
320 
321 /**
322  * @ingroup osal_addr
323  * @brief Map bus memory into CPU space.
324  *
325  * @par Description:
326  * This API is used to map the device whose memory type is device memory and does not use the cache.
327  *
328  * @param phys_addr [in] bus address of the memory
329  * @param size [in] size of the resource to map
330  *
331  * @return Returns the virtual address mapped to
332  *
333  * @par Support System:
334  * linux liteos.
335  */
336 void *osal_ioremap(unsigned long phys_addr, unsigned long size);
337 
338 /**
339  * @ingroup osal_addr
340  * @brief Map bus memory into CPU space.
341  *
342  * @par Description:
343  * Map bus memory into CPU space.The functions of osal_ioremap_nocache are the same as those of osal_ioremap.
344  * and reserved symbols for backward compatibility with drivers that use the osal_ioremap_nocache interface.
345  *
346  * @param phys_addr [in] bus address of the memory
347  * @param size [in] size of the resource to map
348  *
349  * @attention Must be freed with iounmap.
350  *
351  * @return Returns the virtual address mapped to
352  *
353  * @par Support System:
354  * linux liteos freertos.
355  */
356 void *osal_ioremap_nocache(unsigned long phys_addr, unsigned long size);
357 
358 /**
359  * @ingroup osal_addr
360  * @brief Map bus memory into CPU space.
361  *
362  * @par Description:
363  * This API is used to map the device whose memory type is normal memory and use the cache.
364  * This speeds up memory access and improves system performance.
365  *
366  * @param phys_addr [in] bus address of the memory
367  * @param size [in] size of the resource to map
368  *
369  * @return Returns the virtual address mapped to
370  *
371  * @par Support System:
372  * linux liteos.
373  */
374 void *osal_ioremap_cached(unsigned long phys_addr, unsigned long size);
375 
376 /**
377  * @ingroup osal_addr
378  * @brief Free a IO remapping.
379  *
380  * @param addr [in] virtual address from ioremap_*.
381  * @param size [in] size of the resource to map
382  *
383  * @attention Caller must ensure there is only one unmapping for the same pointer.
384  *
385  * @par Support System:
386  * linux liteos freertos.
387  */
388 void osal_iounmap(void *addr, unsigned long size);
389 
390 /**
391  * @ingroup osal_addr
392  * @brief map memory into CPU space write combined.
393  *
394  * @par Description:
395  * map memory into CPU space write combined.
396  *
397  * @param phys_addr [in] bus address of the memory
398  * @param size [in] size of the resource to map
399  *
400  * @attention Must be freed with iounmap.
401  *
402  * @return Returns the virtual address mapped to
403  *
404  * @par Support System:
405  * linux liteos.
406  */
407 void *osal_ioremap_wc(unsigned long phys_addr, unsigned long size);
408 
409 /**
410  * @ingroup osal_addr
411  * @brief Change physical addresses to virtual addresses.
412  *
413  * @param addr physical address
414  *
415  * @return Returns the virtual address mapped to
416  *
417  * @par Support System:
418  * linux liteos freertos.
419  */
420 void *osal_phys_to_virt(unsigned long addr);
421 
422 /**
423  * @ingroup osal_addr
424  * @brief Change virtual addresses to physical addresses.
425  *
426  * @par Support System:
427  * linux liteos.
428  */
429 unsigned long osal_virt_to_phys(const void *virt_addr);
430 
431 /**
432  * @ingroup osal_addr
433  * @brief Maps size from phys_addr into contiguous kernel virtual space.
434  *
435  * @param phys_addr physical address
436  * @param size Size of the address to be manipulated
437  *
438  * @attention
439  * this function only support VM_MAP with PAGE_KERNEL flag.
440  *
441  * @par Support System:
442  * linux.
443  */
444 void *osal_blockmem_vmap(unsigned long phys_addr, unsigned long size);
445 
446 /**
447  * @ingroup osal_addr
448  * @brief Free the virtually contiguous memory area.
449  *
450  * @par Description:
451  * Free the virtually contiguous memory area starting at virt_addr
452  * which was created from the phys_addr passed to osal_vunmap().
453  *
454  * @param virt_addr virtual address
455  *
456  * @attention
457  * Must not be called in interrupt context.
458  *
459  * @par Support System:
460  * linux.
461  */
462 void osal_blockmem_vunmap(const void *virt_addr);
463 
464 /**
465  * @ingroup osal_addr
466  * @brief Free the reserved memory which has been defined in product.
467  *
468  * @par Description:
469  * Free the reserved memory which has been defined in product.
470  *
471  * @param phys_addr physical address
472  * @param size Size of the address to be manipulated
473  *
474  * @par Support System:
475  * linux.
476  */
477 void osal_blockmem_free(unsigned long phys_addr, unsigned long size);
478 
479 /**
480  * @ingroup osal_addr
481  * @brief copy_from_user.
482  *
483  * @param to   [out] The kernel address to which the data is copied.
484  * @param from [in] The user space address of the data to be copied.
485  * @param n    [in] Length of data to be copied (unit: bytes).
486  *
487  * @return 0 is returned on success; otherwise, the number of bytes that are not copied is returned.
488  *
489  * @par Support System:
490  * linux liteos freertos.
491  */
492 unsigned long osal_copy_from_user(void *to, const void *from, unsigned long n);
493 
494 /**
495  * @ingroup osal_addr
496  * @brief copy_to_user.
497  *
498  * @param to   [out] The user space address to which the data is copied.
499  * @param from [in] The kernel address of the data to be copied.
500  * @param n    [in] Length of data to be copied (unit: bytes).
501  *
502  * @return 0 is returned on success; otherwise, the number of bytes that are not copied is returned.
503  *
504  * @par Support System:
505  * linux liteos freertos.
506  */
507 unsigned long osal_copy_to_user(void *to, const void *from, unsigned long n);
508 
509 /**
510  * @ingroup osal_addr
511  * @brief Check whether the user space memory block is available.
512  *
513  * @param type [in] Access type. 0:read, 1:write.
514  * @param addr [in] A pointer variable to user space that points to the beginning of a memory block to be checked.
515  * @param size [in] Size of the memory block to be checked.
516  *
517  * @retval true   user space memory block is available.
518  * @retval false  user space memory block is unavailable.
519  *
520  * @par Support System:
521  * linux liteos.
522  */
523 int osal_access_ok(int type, const void *addr, unsigned long size);
524 
525 #ifdef __cplusplus
526 #if __cplusplus
527 }
528 #endif
529 #endif
530 #endif /* __OSAL_ADDR_H__ */