1 /* 2 * Copyright (c) 2021, Intel Corporation 3 * 4 * Permission is hereby granted, free of charge, to any person obtaining a 5 * copy of this software and associated documentation files (the "Software"), 6 * to deal in the Software without restriction, including without limitation 7 * the rights to use, copy, modify, merge, publish, distribute, sublicense, 8 * and/or sell copies of the Software, and to permit persons to whom the 9 * Software is furnished to do so, subject to the following conditions: 10 * 11 * The above copyright notice and this permission notice shall be included 12 * in all copies or substantial portions of the Software. 13 * 14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS 15 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 17 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR 18 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, 19 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR 20 * OTHER DEALINGS IN THE SOFTWARE. 21 */ 22 //! 23 //! \file mos_vma.h 24 //! \brief interface for virtual memory address allocation 25 //! 26 27 #ifndef __MOS_VMA_H__ 28 #define __MOS_VMA_H__ 29 30 #include <stdbool.h> 31 #include <stdint.h> 32 #include <stdio.h> 33 #include <stdlib.h> 34 #include "list.h" 35 36 #ifdef __cplusplus 37 extern "C" { 38 #endif 39 40 typedef struct _mos_vma_heap { 41 struct list_head holes; 42 43 /** If true, util_vma_heap_alloc will prefer high addresses 44 * 45 * Default is true. 46 */ 47 bool alloc_high; 48 } mos_vma_heap; 49 50 typedef struct _mos_vma_hole { 51 struct list_head link; 52 uint64_t offset; 53 uint64_t size; 54 } mos_vma_hole; 55 56 //! 57 //! \brief Initialize vma heap 58 //! 59 //! \param [in] heap 60 //! Pointer to vma heap which will be initialzed 61 //! \param [in] start 62 //! Start address of the heap 63 //! \param [in] size 64 //! Size of the heap 65 //! 66 //! \return void 67 //! 68 void mos_vma_heap_init(mos_vma_heap *heap, uint64_t start, uint64_t size); 69 70 //! 71 //! \brief Destroy vma heap 72 //! 73 //! \param [in] heap 74 //! Pointer to vma heap which need to be destroyed 75 //! 76 //! \return void 77 //! 78 void mos_vma_heap_finish(mos_vma_heap *heap); 79 80 //! 81 //! \brief Allocate virtual address for bo from a specific vma heap 82 //! 83 //! \param [in] heap 84 //! Pointer to vma heap 85 //! \param [in] size 86 //! Size of the buffer object 87 //! \param [in] alignment 88 //! Address alignment 89 //! 90 //! \return uint64_t 91 //! Allocated virtual address 92 //! 93 uint64_t mos_vma_heap_alloc(mos_vma_heap *heap, uint64_t size, uint64_t alignment); 94 95 //! 96 //! \brief Allocate virtual address which assigned by user 97 //! 98 //! \param [in] heap 99 //! Pointer to vma heap 100 //! \param [in] addr 101 //! Virtual address which need to be allocated 102 //! \param [in] size 103 //! Size of the address 104 //! 105 //! \return bool 106 //! Return true if allocate successfully, false if failed to allocate 107 //! 108 bool mos_vma_heap_alloc_addr(mos_vma_heap *heap, uint64_t addr, uint64_t size); 109 //! 110 //! \brief Free virtual address in a specific vma heap 111 //! 112 //! \param [in] heap 113 //! Pointer to vma heap 114 //! \param [in] offset 115 //! Virtual address which need to be free 116 //! \param [in] size 117 //! Size of the address 118 //! 119 //! \return void 120 //! 121 void mos_vma_heap_free(mos_vma_heap *heap, uint64_t offset, uint64_t size); 122 123 #ifdef __cplusplus 124 } /* extern C */ 125 #endif 126 127 #endif 128