Sughosh Ganu | b6fbbce | 2025-06-17 16:13:46 +0530 | [diff] [blame] | 1 | .. SPDX-License-Identifier: GPL-2.0+ |
| 2 | |
| 3 | Logical Memory Blocks (LMB) |
| 4 | =========================== |
| 5 | |
| 6 | U-Boot has support for reserving chunks of memory which is primarily |
| 7 | used for loading images to the DRAM memory, before these are booted, |
| 8 | or written to non-volatile storage medium. This functionality is |
| 9 | provided through the Logical Memory Blocks (LMB) module. |
| 10 | |
| 11 | Introduction |
| 12 | ------------ |
| 13 | |
| 14 | The LMB module manages allocation requests for memory region not |
| 15 | occupied by the U-Boot image. Allocation requests that are made |
| 16 | through malloc() and similar functions result in memory getting |
| 17 | allocated from the heap region, which is part of the U-Boot |
| 18 | image. Typically, the heap memory is a few MiB in size. Loading an |
| 19 | image like the linux kernel might require lot more memory than what |
| 20 | the heap can provide. Such allocations are usually handled through the |
| 21 | LMB module. |
| 22 | |
| 23 | The U-Boot image typically gets relocated to the top of the usable |
| 24 | DRAM memory region. A typical memory layout looks as follows:: |
| 25 | |
| 26 | |
| 27 | |
| 28 | |
| 29 | |
| 30 | | | |
| 31 | | | |
| 32 | | | |
| 33 | | | |
| 34 | | | |
| 35 | --- +--------------+ <--- U-Boot ram top |
| 36 | | | | |
| 37 | | | Text | |
| 38 | | +--------------+ |
| 39 | | | | |
| 40 | | | Data | |
| 41 | | +--------------+ |
| 42 | | | | |
| 43 | | | BSS | |
| 44 | U-Boot Image +--------------+ |
| 45 | | | | |
| 46 | | | Heap | |
| 47 | | | | |
| 48 | | +--------------+ |
| 49 | | | | |
| 50 | | | | |
| 51 | | | Stack | |
| 52 | | | | |
| 53 | | | | |
| 54 | --- +--------------+ |
| 55 | | | |
| 56 | | | |
| 57 | | | |
| 58 | | | |
| 59 | | | |
| 60 | | | |
| 61 | | | |
| 62 | | | |
| 63 | | | |
| 64 | | | |
| 65 | | | |
| 66 | | | |
| 67 | | | |
| 68 | | | |
| 69 | +--------------+ <--- ram start |
| 70 | |
| 71 | |
| 72 | |
| 73 | The region of memory below the U-Boot image is the one controlled by |
| 74 | the LMB module. |
| 75 | |
| 76 | |
| 77 | Types of LMB Allocations |
| 78 | ------------------------ |
| 79 | |
| 80 | There are two classes of allocation requests that get made to the LMB |
| 81 | module. One type of allocation requests are requesting memory of a |
| 82 | particular number of bytes. This type of allocation is similar to that |
| 83 | done using the malloc type of function calls. The other type of |
| 84 | allocations, are requests made for a specific memory address. The |
| 85 | second type of allocations are usually made for loading images to a |
| 86 | particular memory address. |
| 87 | |
| 88 | |
| 89 | LMB design Pre 2025.01 |
| 90 | ---------------------- |
| 91 | |
| 92 | The earlier versions of U-Boot (pre 2025.01 release) |
| 93 | had a local memory map based LMB implementation whereby it was |
| 94 | possible to declare the LMB map inside a function or a C file. This |
| 95 | design resulted in temporary, non-global LMB maps, which also allowed |
| 96 | for re-use of memory. This meant that it was possible to use a region |
| 97 | of memory to load some image, and subsequently the same region of |
| 98 | memory could be used for loading a different image. A typical example |
| 99 | of this usage would be loading an image to a memory address, followed |
| 100 | by writing that image to some non-volatile storage medium. Once this |
| 101 | is done, the same address can be used for loading a different image |
| 102 | and then writing it to it's non-volatile storage |
| 103 | destination. Typically, environment variables like `loadaddr`, |
| 104 | `kernel_addr_r`, `ramdisk_addr_r` are used for loading images to |
| 105 | memory regions. |
| 106 | |
| 107 | |
| 108 | Current LMB implementation |
| 109 | -------------------------- |
| 110 | |
| 111 | Changes were made in the 2025.01 release to make the LMB memory map |
| 112 | global and persistent. With this, the LMB memory map is the same |
| 113 | across all of U-Boot, and also persists as long as U-Boot is |
| 114 | active. Even with this change, there has been consistency as far as |
| 115 | re-use of memory is concerned to maintain backward compatibility. It |
| 116 | is allowed for re-requesting the same region of memory if the memory |
| 117 | region has a particular attribute (LMB_NONE). |
| 118 | |
| 119 | As part of the platform boot, DRAM memory available for use in U-Boot |
| 120 | gets added to the LMB memory map. Any allocation requests made |
| 121 | subsequently will be made from this memory added as part of the board |
| 122 | init. |
| 123 | |
| 124 | |
| 125 | Allocation API |
| 126 | -------------- |
| 127 | |
| 128 | Any request for non-heap memory can be made through the LMB allocation |
| 129 | API. |
| 130 | |
| 131 | .. code-block:: c |
| 132 | |
| 133 | int lmb_alloc_mem(enum lmb_mem_type type, u64 align, |
| 134 | phys_addr_t *addr, phys_size_t size, |
| 135 | u32 flags); |
| 136 | |
| 137 | Correspondingly, the allocated memory can be free'd |
| 138 | |
| 139 | .. code-block:: c |
| 140 | |
| 141 | long lmb_free(phys_addr_t base, phys_size_t size, u32 flags); |
| 142 | |
| 143 | For a detailed API description, please refer to the header file. |
| 144 | |
| 145 | |
| 146 | UEFI allocations with LMB as the backend |
| 147 | ---------------------------------------- |
| 148 | |
| 149 | The UEFI specification describes boot-time API's for allocation of |
| 150 | memory. These API's use the same memory that is being used by the LMB |
| 151 | module. Pre 2025.01 release, there wasn't any synchronisation between |
| 152 | the EFI sub-system and the LMB module about the memory that was |
| 153 | getting allocated by each of these modules. This was the primary |
| 154 | reason for making the LMB memory map global and persistent. With this |
| 155 | change, the EFI memory allocation API's have also been changed to use |
| 156 | the LMB module as the backend for the allocation requests. Any other |
| 157 | sub-system which might wish to use the same memory region for it's use |
| 158 | can then use the LMB as the backend for the memory allocations and |
| 159 | it's associated book-keeping. |
| 160 | |
| 161 | |
| 162 | API documentation |
| 163 | ----------------- |
| 164 | |
| 165 | .. kernel-doc:: include/lmb.h |
| 166 | |