1 /* 2 * Copyright (c) 2013-2019 Huawei Technologies Co., Ltd. All rights reserved. 3 * Copyright (c) 2020-2021 Huawei Device Co., Ltd. All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without modification, 6 * are permitted provided that the following conditions are met: 7 * 8 * 1. Redistributions of source code must retain the above copyright notice, this list of 9 * conditions and the following disclaimer. 10 * 11 * 2. Redistributions in binary form must reproduce the above copyright notice, this list 12 * of conditions and the following disclaimer in the documentation and/or other materials 13 * provided with the distribution. 14 * 15 * 3. Neither the name of the copyright holder nor the names of its contributors may be used 16 * to endorse or promote products derived from this software without specific prior written 17 * permission. 18 * 19 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 20 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, 21 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR 23 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 24 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 25 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; 26 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 27 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR 28 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF 29 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 30 */ 31 32 #ifndef __LOS_VM_PHYS_H__ 33 #define __LOS_VM_PHYS_H__ 34 35 #include "los_typedef.h" 36 #include "los_list.h" 37 #include "los_spinlock.h" 38 #include "los_vm_page.h" 39 40 #ifdef __cplusplus 41 #if __cplusplus 42 extern "C" { 43 #endif /* __cplusplus */ 44 #endif /* __cplusplus */ 45 46 #define VM_LIST_ORDER_MAX 9 47 #define VM_PHYS_SEG_MAX 32 48 49 #ifndef min 50 #define min(x, y) ((x) < (y) ? (x) : (y)) 51 #endif 52 53 #define VM_PAGE_TO_PHYS(page) ((page)->physAddr) 54 #define VM_ORDER_TO_PAGES(order) (1 << (order)) 55 #define VM_ORDER_TO_PHYS(order) (1 << (PAGE_SHIFT + (order))) 56 #define VM_PHYS_TO_ORDER(phys) (min(LOS_LowBitGet((phys) >> PAGE_SHIFT), VM_LIST_ORDER_MAX - 1)) 57 58 struct VmFreeList { 59 LOS_DL_LIST node; 60 UINT32 listCnt; 61 }; 62 63 enum OsLruList { 64 VM_LRU_INACTIVE_ANON = 0, 65 VM_LRU_ACTIVE_ANON, 66 VM_LRU_INACTIVE_FILE, 67 VM_LRU_ACTIVE_FILE, 68 VM_LRU_UNEVICTABLE, 69 VM_NR_LRU_LISTS 70 }; 71 72 typedef struct VmPhysSeg { 73 PADDR_T start; /* The start of physical memory area */ 74 size_t size; /* The size of physical memory area */ 75 LosVmPage *pageBase; /* The first page address of this area */ 76 77 SPIN_LOCK_S freeListLock; /* The buddy list spinlock */ 78 struct VmFreeList freeList[VM_LIST_ORDER_MAX]; /* The free pages in the buddy list */ 79 80 SPIN_LOCK_S lruLock; 81 size_t lruSize[VM_NR_LRU_LISTS]; 82 LOS_DL_LIST lruList[VM_NR_LRU_LISTS]; 83 } LosVmPhysSeg; 84 85 struct VmPhysArea { 86 PADDR_T start; 87 size_t size; 88 }; 89 90 extern struct VmPhysSeg g_vmPhysSeg[VM_PHYS_SEG_MAX]; 91 extern INT32 g_vmPhysSegNum; 92 93 UINT32 OsVmPagesToOrder(size_t nPages); 94 struct VmPhysSeg *OsVmPhysSegGet(LosVmPage *page); 95 LosVmPhysSeg *OsGVmPhysSegGet(VOID); 96 VOID *OsVmPageToVaddr(LosVmPage *page); 97 VOID OsVmPhysSegAdd(VOID); 98 VOID OsVmPhysInit(VOID); 99 VOID OsVmPhysAreaSizeAdjust(size_t size); 100 UINT32 OsVmPhysPageNumGet(VOID); 101 LosVmPage *OsVmVaddrToPage(VOID *ptr); 102 VOID OsPhysSharePageCopy(PADDR_T oldPaddr, PADDR_T *newPaddr, LosVmPage *newPage); 103 VOID OsVmPhysPagesFreeContiguous(LosVmPage *page, size_t nPages); 104 LosVmPage *OsVmPhysToPage(paddr_t pa, UINT8 segID); 105 LosVmPage *OsVmPaddrToPage(paddr_t paddr); 106 107 LosVmPage *LOS_PhysPageAlloc(VOID); 108 VOID LOS_PhysPageFree(LosVmPage *page); 109 size_t LOS_PhysPagesAlloc(size_t nPages, LOS_DL_LIST *list); 110 size_t LOS_PhysPagesFree(LOS_DL_LIST *list); 111 VOID *LOS_PhysPagesAllocContiguous(size_t nPages); 112 VOID LOS_PhysPagesFreeContiguous(VOID *ptr, size_t nPages); 113 VADDR_T *LOS_PaddrToKVaddr(PADDR_T paddr); 114 PADDR_T OsKVaddrToPaddr(VADDR_T kvaddr); 115 116 #ifdef __cplusplus 117 #if __cplusplus 118 } 119 #endif /* __cplusplus */ 120 #endif /* __cplusplus */ 121 122 #endif /* __LOS_VM_PHYS_H__ */ 123