blob: b9d0f09c2bb81d2f9ebbe2f04badd197134d6c11 [file] [log] [blame]
Sughosh Ganub6fbbce2025-06-17 16:13:46 +05301.. SPDX-License-Identifier: GPL-2.0+
2
3Logical Memory Blocks (LMB)
4===========================
5
6U-Boot has support for reserving chunks of memory which is primarily
7used for loading images to the DRAM memory, before these are booted,
8or written to non-volatile storage medium. This functionality is
9provided through the Logical Memory Blocks (LMB) module.
10
11Introduction
12------------
13
14The LMB module manages allocation requests for memory region not
15occupied by the U-Boot image. Allocation requests that are made
16through malloc() and similar functions result in memory getting
17allocated from the heap region, which is part of the U-Boot
18image. Typically, the heap memory is a few MiB in size. Loading an
19image like the linux kernel might require lot more memory than what
20the heap can provide. Such allocations are usually handled through the
21LMB module.
22
23The U-Boot image typically gets relocated to the top of the usable
24DRAM 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
73The region of memory below the U-Boot image is the one controlled by
74the LMB module.
75
76
77Types of LMB Allocations
78------------------------
79
80There are two classes of allocation requests that get made to the LMB
81module. One type of allocation requests are requesting memory of a
82particular number of bytes. This type of allocation is similar to that
83done using the malloc type of function calls. The other type of
84allocations, are requests made for a specific memory address. The
85second type of allocations are usually made for loading images to a
86particular memory address.
87
88
89LMB design Pre 2025.01
90----------------------
91
92The earlier versions of U-Boot (pre 2025.01 release)
93had a local memory map based LMB implementation whereby it was
94possible to declare the LMB map inside a function or a C file. This
95design resulted in temporary, non-global LMB maps, which also allowed
96for re-use of memory. This meant that it was possible to use a region
97of memory to load some image, and subsequently the same region of
98memory could be used for loading a different image. A typical example
99of this usage would be loading an image to a memory address, followed
100by writing that image to some non-volatile storage medium. Once this
101is done, the same address can be used for loading a different image
102and then writing it to it's non-volatile storage
103destination. Typically, environment variables like `loadaddr`,
104`kernel_addr_r`, `ramdisk_addr_r` are used for loading images to
105memory regions.
106
107
108Current LMB implementation
109--------------------------
110
111Changes were made in the 2025.01 release to make the LMB memory map
112global and persistent. With this, the LMB memory map is the same
113across all of U-Boot, and also persists as long as U-Boot is
114active. Even with this change, there has been consistency as far as
115re-use of memory is concerned to maintain backward compatibility. It
116is allowed for re-requesting the same region of memory if the memory
117region has a particular attribute (LMB_NONE).
118
119As part of the platform boot, DRAM memory available for use in U-Boot
120gets added to the LMB memory map. Any allocation requests made
121subsequently will be made from this memory added as part of the board
122init.
123
124
125Allocation API
126--------------
127
128Any request for non-heap memory can be made through the LMB allocation
129API.
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
137Correspondingly, 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
143For a detailed API description, please refer to the header file.
144
145
146UEFI allocations with LMB as the backend
147----------------------------------------
148
149The UEFI specification describes boot-time API's for allocation of
150memory. These API's use the same memory that is being used by the LMB
151module. Pre 2025.01 release, there wasn't any synchronisation between
152the EFI sub-system and the LMB module about the memory that was
153getting allocated by each of these modules. This was the primary
154reason for making the LMB memory map global and persistent. With this
155change, the EFI memory allocation API's have also been changed to use
156the LMB module as the backend for the allocation requests. Any other
157sub-system which might wish to use the same memory region for it's use
158can then use the LMB as the backend for the memory allocations and
159it's associated book-keeping.
160
161
162API documentation
163-----------------
164
165.. kernel-doc:: include/lmb.h
166