| 1 | /* | 
|---|
| 2 | * ppm.h - Per-cluster Physical Pages Manager Interface | 
|---|
| 3 | * | 
|---|
| 4 | * Authors  Ghassan Almaless (2008,2009,2010,2011,2012) | 
|---|
| 5 | *          Alain Greiner    (2016) | 
|---|
| 6 | * | 
|---|
| 7 | * Copyright (c) UPMC Sorbonne Universites | 
|---|
| 8 | * | 
|---|
| 9 | * This file is part of ALMOS-MKH. | 
|---|
| 10 | * | 
|---|
| 11 | * ALMOS-kernel is free software; you can redistribute it and/or modify it | 
|---|
| 12 | * under the terms of the GNU General Public License as published by | 
|---|
| 13 | * the Free Software Foundation; version 2.0 of the License. | 
|---|
| 14 | * | 
|---|
| 15 | * ALMOS-kernel is distributed in the hope that it will be useful, but | 
|---|
| 16 | * WITHOUT ANY WARRANTY; without even the implied warranty of | 
|---|
| 17 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU | 
|---|
| 18 | * General Public License for more details. | 
|---|
| 19 | * | 
|---|
| 20 | * You should have received a copy of the GNU General Public License | 
|---|
| 21 | * along with ALMOS-kernel; if not, write to the Free Software Foundation, | 
|---|
| 22 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA | 
|---|
| 23 | */ | 
|---|
| 24 |  | 
|---|
| 25 | #ifndef _PPM_H_ | 
|---|
| 26 | #define _PPM_H_ | 
|---|
| 27 |  | 
|---|
| 28 | #include <hal_types.h> | 
|---|
| 29 | #include <list.h> | 
|---|
| 30 | #include <spinlock.h> | 
|---|
| 31 | #include <boot_info.h> | 
|---|
| 32 | #include <page.h> | 
|---|
| 33 |  | 
|---|
| 34 | #define  PPM_SIGNATURE     0xBABEF00D | 
|---|
| 35 |  | 
|---|
| 36 | /***************************************************************************************** | 
|---|
| 37 | * This structure defines the Physical Memory Manager in a cluster. | 
|---|
| 38 | * In all clusters, the physical memory bank starts at address 0. | 
|---|
| 39 | * The segments kcode and kdata are mapped in the first "offset" pages. | 
|---|
| 40 | * The physical page descriptors array is implemented just after this offset zone. | 
|---|
| 41 | * The main service provided by the PMM is the dynamic allocation of physical pages. | 
|---|
| 42 | * This low-level allocator implements the buddy algorithm: an allocated block is | 
|---|
| 43 | * is an integer number n of 4 Kbytes pages, and n (called order) is a power of 2. | 
|---|
| 44 | ****************************************************************************************/ | 
|---|
| 45 | typedef struct ppm_s | 
|---|
| 46 | { | 
|---|
| 47 | uint32_t       signature;               /*! set when initialised                    */ | 
|---|
| 48 | spinlock_t     free_lock;               /*! lock protecting free_pages[] array      */ | 
|---|
| 49 | list_entry_t   free_pages_root[CONFIG_PPM_MAX_ORDER];  /*! roots of free lists      */ | 
|---|
| 50 | uint32_t       free_pages_nr[CONFIG_PPM_MAX_ORDER];    /*! numbers of free pages    */ | 
|---|
| 51 | uint32_t       total_free_pages;        /*! total number of free pages              */ | 
|---|
| 52 | page_t       * pages_tbl;               /*! pointer on page descriptors array       */ | 
|---|
| 53 | uint32_t       pages_nr;                /*! total number of 4 Kbytes physical page  */ | 
|---|
| 54 | uint32_t       pages_offset;            /*! allocated pages for kcode & kdata       */ | 
|---|
| 55 | uint32_t       pages_desc;              /*! allocated pages for pages_tbl[] array   */ | 
|---|
| 56 | spinlock_t     dirty_lock;              /*! lock protecting the dirty list          */ | 
|---|
| 57 | list_entry_t   dirty_root;              /*! root of dirty pages list                */ | 
|---|
| 58 | } | 
|---|
| 59 | ppm_t; | 
|---|
| 60 |  | 
|---|
| 61 | /***************************************************************************************** | 
|---|
| 62 | * This function initializes a PPM (Physical Pages Manager) in a cluster. | 
|---|
| 63 | * The physical memory base address in all clusters is zero. | 
|---|
| 64 | * The physical memory size is NOT constrained to be smaller than 4 Gbytes. | 
|---|
| 65 | ***************************************************************************************** | 
|---|
| 66 | * @ ppm          : pointer on physical pages manager. | 
|---|
| 67 | * @ pages_nr     : total physical memory size (number of 4 Kbytes pages). | 
|---|
| 68 | * @ pages_offset : number of pages already allocated in this physical memory. | 
|---|
| 69 | ****************************************************************************************/ | 
|---|
| 70 | void ppm_init( ppm_t    * ppm, | 
|---|
| 71 | uint32_t   pages_nr, | 
|---|
| 72 | uint32_t   pages_offset ); | 
|---|
| 73 |  | 
|---|
| 74 | /***************************************************************************************** | 
|---|
| 75 | * This is the low-level physical pages allocation function. | 
|---|
| 76 | * It allocates N contiguous physical pages. N is a power of 2. | 
|---|
| 77 | * In normal use, you don't need to call it directly, as the recommanded way to get | 
|---|
| 78 | * physical pages is to call the generic allocator defined in kmem.h. | 
|---|
| 79 | ***************************************************************************************** | 
|---|
| 80 | * @ order        : ln2( number of 4 Kbytes pages) | 
|---|
| 81 | * @ returns a pointer on the page descriptor if success / NULL otherwise | 
|---|
| 82 | ****************************************************************************************/ | 
|---|
| 83 | page_t * ppm_alloc_pages( uint32_t order ); | 
|---|
| 84 |  | 
|---|
| 85 | /***************************************************************************************** | 
|---|
| 86 | * This is the low-level physical pages release function. | 
|---|
| 87 | * In normal use, you do not need to call it directly, as the recommanded way to free | 
|---|
| 88 | * physical pages is to call the generic allocator defined in kmem.h. | 
|---|
| 89 | ***************************************************************************************** | 
|---|
| 90 | * @ page         : pointer to the page descriptor to be released | 
|---|
| 91 | ****************************************************************************************/ | 
|---|
| 92 | void ppm_free_pages( page_t * page ); | 
|---|
| 93 |  | 
|---|
| 94 | /***************************************************************************************** | 
|---|
| 95 | * This function check if a page descriptor is valid. | 
|---|
| 96 | ***************************************************************************************** | 
|---|
| 97 | * @ page         : pointer on a page descriptor | 
|---|
| 98 | * @ returns true if valid / false otherwise. | 
|---|
| 99 | ****************************************************************************************/ | 
|---|
| 100 | inline bool_t ppm_page_is_valid( page_t * page ); | 
|---|
| 101 |  | 
|---|
| 102 | /***************************************************************************************** | 
|---|
| 103 | * Get the page base address from the page descriptor pointer. | 
|---|
| 104 | ***************************************************************************************** | 
|---|
| 105 | * @ page         : pointer to page descriptor | 
|---|
| 106 | * @ returns page base address | 
|---|
| 107 | ****************************************************************************************/ | 
|---|
| 108 | inline void* ppm_page2base( page_t * page ); | 
|---|
| 109 |  | 
|---|
| 110 | /***************************************************************************************** | 
|---|
| 111 | * Get the page descriptor pointer from the page base address. | 
|---|
| 112 | ***************************************************************************************** | 
|---|
| 113 | * @ vaddr        : page base address | 
|---|
| 114 | * @ returns pointer on page descriptor | 
|---|
| 115 | ****************************************************************************************/ | 
|---|
| 116 | inline page_t * ppm_base2page( void * vaddr ); | 
|---|
| 117 |  | 
|---|
| 118 | /***************************************************************************************** | 
|---|
| 119 | * Get the PPN from the page descriptor pointer. | 
|---|
| 120 | ***************************************************************************************** | 
|---|
| 121 | * @ page         : pointer to page descriptor | 
|---|
| 122 | * @ returns physical page number | 
|---|
| 123 | ****************************************************************************************/ | 
|---|
| 124 | inline ppn_t ppm_page2ppn( page_t * page ); | 
|---|
| 125 |  | 
|---|
| 126 | /***************************************************************************************** | 
|---|
| 127 | * Get the page descriptor pointer from the PPN. | 
|---|
| 128 | ***************************************************************************************** | 
|---|
| 129 | * @ ppn          : physical page number | 
|---|
| 130 | * @ returns pointer on page descriptor | 
|---|
| 131 | ****************************************************************************************/ | 
|---|
| 132 | inline page_t * ppm_ppn2page( ppn_t ppn ); | 
|---|
| 133 |  | 
|---|
| 134 | /***************************************************************************************** | 
|---|
| 135 | * Get the page base address from the PPN. | 
|---|
| 136 | ***************************************************************************************** | 
|---|
| 137 | * @ ppn          : physical page number | 
|---|
| 138 | * @ returns page base address | 
|---|
| 139 | ****************************************************************************************/ | 
|---|
| 140 | inline void* ppm_ppn2base( ppn_t ppn ); | 
|---|
| 141 |  | 
|---|
| 142 | /***************************************************************************************** | 
|---|
| 143 | * Get the PPN from the page base address. | 
|---|
| 144 | ***************************************************************************************** | 
|---|
| 145 | * @ vaddr        : page base address | 
|---|
| 146 | * @ returns physical page number | 
|---|
| 147 | ****************************************************************************************/ | 
|---|
| 148 | inline ppn_t ppm_base2ppn( void * base ); | 
|---|
| 149 |  | 
|---|
| 150 | /***************************************************************************************** | 
|---|
| 151 | * This function prints the PPM allocator status. | 
|---|
| 152 | ***************************************************************************************** | 
|---|
| 153 | * @ ppm      : pointer on PPM allocator. | 
|---|
| 154 | * @ string   : define context of display. | 
|---|
| 155 | ****************************************************************************************/ | 
|---|
| 156 | void ppm_print( ppm_t * ppm, | 
|---|
| 157 | char  * string ); | 
|---|
| 158 |  | 
|---|
| 159 | /***************************************************************************************** | 
|---|
| 160 | * This function checks PPM allocator consistency. | 
|---|
| 161 | ***************************************************************************************** | 
|---|
| 162 | * @ ppm      : pointer on PPM allocator. | 
|---|
| 163 | ****************************************************************************************/ | 
|---|
| 164 | void ppm_assert_order( ppm_t * ppm ); | 
|---|
| 165 |  | 
|---|
| 166 | #endif  /* _PPM_H_ */ | 
|---|