1 /* SPDX-License-Identifier: GPL-2.0 */
2 /*
3  * Copyright (C) 2020 Marvell International Ltd.
4  */
5 
6 /**
7  * @file
8  * Simple allocate only memory allocator. Used to allocate memory at application
9  * start time.
10  */
11 
12 #ifndef __CVMX_BOOTMEM_H__
13 #define __CVMX_BOOTMEM_H__
14 
15 /* Must be multiple of 8, changing breaks ABI */
16 #define CVMX_BOOTMEM_NAME_LEN		128
17 /* Can change without breaking ABI */
18 #define CVMX_BOOTMEM_NUM_NAMED_BLOCKS	64
19 /* minimum alignment of bootmem alloced blocks */
20 #define CVMX_BOOTMEM_ALIGNMENT_SIZE	(16ull)
21 
22 /* Flags for cvmx_bootmem_phy_mem* functions */
23 /* Allocate from end of block instead of beginning */
24 #define CVMX_BOOTMEM_FLAG_END_ALLOC	(1 << 0)
25 #define CVMX_BOOTMEM_FLAG_NO_LOCKING	(1 << 1) /* Don't do any locking. */
26 
27 /* Real physical addresses of memory regions */
28 #define OCTEON_DDR0_BASE    (0x0ULL)
29 #define OCTEON_DDR0_SIZE    (0x010000000ULL)
30 #define OCTEON_DDR1_BASE    ((OCTEON_IS_OCTEON2() || OCTEON_IS_OCTEON3()) \
31 			     ? 0x20000000ULL : 0x410000000ULL)
32 #define OCTEON_DDR1_SIZE    (0x010000000ULL)
33 #define OCTEON_DDR2_BASE    ((OCTEON_IS_OCTEON2() || OCTEON_IS_OCTEON3()) \
34 			     ? 0x30000000ULL : 0x20000000ULL)
35 #define OCTEON_DDR2_SIZE    ((OCTEON_IS_OCTEON2() || OCTEON_IS_OCTEON3()) \
36 			     ? 0x7d0000000ULL : 0x3e0000000ULL)
37 #define OCTEON_MAX_PHY_MEM_SIZE ((OCTEON_IS_MODEL(OCTEON_CN68XX))	\
38 				 ? 128 * 1024 * 1024 * 1024ULL		\
39 				 : (OCTEON_IS_OCTEON2())		\
40 				 ? 32 * 1024 * 1024 * 1024ull		\
41 				 : (OCTEON_IS_OCTEON3())		\
42 				 ? 512 * 1024 * 1024 * 1024ULL		\
43 				 : 16 * 1024 * 1024 * 1024ULL)
44 
45 /*
46  * First bytes of each free physical block of memory contain this structure,
47  * which is used to maintain the free memory list.  Since the bootloader is
48  * only 32 bits, there is a union providing 64 and 32 bit versions.  The
49  * application init code converts addresses to 64 bit addresses before the
50  * application starts.
51  */
52 struct cvmx_bootmem_block_header {
53 	/* Note: these are referenced from assembly routines in the bootloader,
54 	 * so this structure should not be changed without changing those
55 	 * routines as well.
56 	 */
57 	u64 next_block_addr;
58 	u64 size;
59 
60 };
61 
62 /*
63  * Structure for named memory blocks
64  * Number of descriptors
65  * available can be changed without affecting compatibility,
66  * but name length changes require a bump in the bootmem
67  * descriptor version
68  * Note: This structure must be naturally 64 bit aligned, as a single
69  * memory image will be used by both 32 and 64 bit programs.
70  */
71 struct cvmx_bootmem_named_block_desc {
72 	u64 base_addr;	/* Base address of named block */
73 	/*
74 	 * Size actually allocated for named block (may differ from requested)
75 	 */
76 	u64 size;
77 	char name[CVMX_BOOTMEM_NAME_LEN]; /* name of named block */
78 };
79 
80 /* Current descriptor versions */
81 /* CVMX bootmem descriptor major version */
82 #define CVMX_BOOTMEM_DESC_MAJ_VER	3
83 /* CVMX bootmem descriptor minor version */
84 #define CVMX_BOOTMEM_DESC_MIN_VER	0
85 
86 /*
87  * First three members of cvmx_bootmem_desc_t are left in original
88  * positions for backwards compatibility.
89  */
90 struct cvmx_bootmem_desc {
91 	/* Linux compatible proxy for __BIG_ENDIAN */
92 	u32 lock;	/* spinlock to control access to list */
93 	u32 flags;	/* flags for indicating various conditions */
94 	u64 head_addr;
95 
96 	/* incremented changed when incompatible changes made */
97 	u32 major_version;
98 	/*
99 	 * incremented changed when compatible changes made, reset to
100 	 * zero when major incremented
101 	 */
102 	u32 minor_version;
103 	u64 app_data_addr;
104 	u64 app_data_size;
105 
106 	/* number of elements in named blocks array */
107 	u32 named_block_num_blocks;
108 	/* length of name array in bootmem blocks */
109 	u32 named_block_name_len;
110 	/* address of named memory block descriptors */
111 	u64 named_block_array_addr;
112 };
113 
114 /**
115  * Initialize the boot alloc memory structures. This is
116  * normally called inside of cvmx_user_app_init()
117  *
118  * @param mem_desc_addr	Address of the free memory list
119  * @return
120  */
121 int cvmx_bootmem_init(u64 mem_desc_addr);
122 
123 /**
124  * Allocate a block of memory from the free list that was passed
125  * to the application by the bootloader.
126  * This is an allocate-only algorithm, so freeing memory is not possible.
127  *
128  * @param size      Size in bytes of block to allocate
129  * @param alignment Alignment required - must be power of 2
130  *
131  * @return pointer to block of memory, NULL on error
132  */
133 void *cvmx_bootmem_alloc(u64 size, u64 alignment);
134 
135 /**
136  * Allocate a block of memory from the free list that was passed
137  * to the application by the bootloader from a specific node.
138  * This is an allocate-only algorithm, so freeing memory is not possible.
139  *
140  * @param node	The node to allocate memory from
141  * @param size  Size in bytes of block to allocate
142  * @param alignment Alignment required - must be power of 2
143  *
144  * @return pointer to block of memory, NULL on error
145  */
146 void *cvmx_bootmem_alloc_node(u64 node, u64 size, u64 alignment);
147 
148 /**
149  * Allocate a block of memory from the free list that was
150  * passed to the application by the bootloader at a specific
151  * address. This is an allocate-only algorithm, so
152  * freeing memory is not possible. Allocation will fail if
153  * memory cannot be allocated at the specified address.
154  *
155  * @param size      Size in bytes of block to allocate
156  * @param address   Physical address to allocate memory at.  If this
157  *                  memory is not available, the allocation fails.
158  * @param alignment Alignment required - must be power of 2
159  * @return pointer to block of memory, NULL on error
160  */
161 void *cvmx_bootmem_alloc_address(u64 size, u64 address,
162 				 u64 alignment);
163 
164 /**
165  * Allocate a block of memory from the free list that was
166  * passed to the application by the bootloader within a specified
167  * address range. This is an allocate-only algorithm, so
168  * freeing memory is not possible. Allocation will fail if
169  * memory cannot be allocated in the requested range.
170  *
171  * @param size      Size in bytes of block to allocate
172  * @param min_addr  defines the minimum address of the range
173  * @param max_addr  defines the maximum address of the range
174  * @param alignment Alignment required - must be power of 2
175  * @return pointer to block of memory, NULL on error
176  */
177 void *cvmx_bootmem_alloc_range(u64 size, u64 alignment,
178 			       u64 min_addr, u64 max_addr);
179 
180 /**
181  * Allocate a block of memory from the free list that was passed
182  * to the application by the bootloader, and assign it a name in the
183  * global named block table.  (part of the cvmx_bootmem_descriptor_t structure)
184  * Named blocks can later be freed.
185  *
186  * @param size  Size in bytes of block to allocate
187  * @param alignment Alignment required - must be power of 2
188  * @param name  name of block - must be less than CVMX_BOOTMEM_NAME_LEN bytes
189  *
190  * @return pointer to block of memory, NULL on error
191  */
192 void *cvmx_bootmem_alloc_named(u64 size, u64 alignment,
193 			       const char *name);
194 
195 /**
196  * Allocate a block of memory from the free list that was passed
197  * to the application by the bootloader, and assign it a name in the
198  * global named block table.  (part of the cvmx_bootmem_descriptor_t structure)
199  * Named blocks can later be freed.
200  *
201  * @param size Size in bytes of block to allocate
202  * @param alignment Alignment required - must be power of 2
203  * @param name name of block - must be less than CVMX_BOOTMEM_NAME_LEN bytes
204  * @param flags     Flags to control options for the allocation.
205  *
206  * @return pointer to block of memory, NULL on error
207  */
208 void *cvmx_bootmem_alloc_named_flags(u64 size, u64 alignment,
209 				     const char *name, u32 flags);
210 
211 /**
212  * Allocate a block of memory from the free list that was passed
213  * to the application by the bootloader, and assign it a name in the
214  * global named block table.  (part of the cvmx_bootmem_descriptor_t structure)
215  * Named blocks can later be freed.
216  *
217  * @param size    Size in bytes of block to allocate
218  * @param address Physical address to allocate memory at.  If this
219  *                memory is not available, the allocation fails.
220  * @param name    name of block - must be less than CVMX_BOOTMEM_NAME_LEN bytes
221  *
222  * @return pointer to block of memory, NULL on error
223  */
224 void *cvmx_bootmem_alloc_named_address(u64 size, u64 address,
225 				       const char *name);
226 
227 /**
228  * Allocate a block of memory from a specific range of the free list
229  * that was passed to the application by the bootloader, and assign it
230  * a name in the global named block table.  (part of the
231  * cvmx_bootmem_descriptor_t structure) Named blocks can later be
232  * freed.  If request cannot be satisfied within the address range
233  * specified, NULL is returned
234  *
235  * @param size      Size in bytes of block to allocate
236  * @param min_addr  minimum address of range
237  * @param max_addr  maximum address of range
238  * @param align  Alignment of memory to be allocated. (must be a power of 2)
239  * @param name   name of block - must be less than CVMX_BOOTMEM_NAME_LEN bytes
240  *
241  * @return pointer to block of memory, NULL on error
242  */
243 void *cvmx_bootmem_alloc_named_range(u64 size, u64 min_addr,
244 				     u64 max_addr, u64 align,
245 				     const char *name);
246 
247 /**
248  * Allocate if needed a block of memory from a specific range of the
249  * free list that was passed to the application by the bootloader, and
250  * assign it a name in the global named block table.  (part of the
251  * cvmx_bootmem_descriptor_t structure) Named blocks can later be
252  * freed.  If the requested name block is already allocated, return
253  * the pointer to block of memory.  If request cannot be satisfied
254  * within the address range specified, NULL is returned
255  *
256  * @param size   Size in bytes of block to allocate
257  * @param min_addr  minimum address of range
258  * @param max_addr  maximum address of range
259  * @param align  Alignment of memory to be allocated. (must be a power of 2)
260  * @param name   name of block - must be less than CVMX_BOOTMEM_NAME_LEN bytes
261  * @param init   Initialization function
262  *
263  * The initialization function is optional, if omitted the named block
264  * is initialized to all zeros when it is created, i.e. once.
265  *
266  * @return pointer to block of memory, NULL on error
267  */
268 void *cvmx_bootmem_alloc_named_range_once(u64 size,
269 					  u64 min_addr,
270 					  u64 max_addr,
271 					  u64 align,
272 					  const char *name,
273 					  void (*init)(void *));
274 
275 /**
276  * Allocate all free memory starting at the start address.  This is used to
277  * prevent any free blocks from later being allocated within the reserved space.
278  * Note that any memory allocated with this function cannot be later freed.
279  *
280  * @param start_addr  Starting address to reserve
281  * @param size        Size in bytes to reserve starting at start_addr
282  * @param name        Name to assign to reserved blocks
283  * @param flags       Flags to use when reserving memory
284  *
285  * @return 0 on failure,
286  *         !0 on success
287  */
288 int cvmx_bootmem_reserve_memory(u64 start_addr, u64 size,
289 				const char *name, u32 flags);
290 
291 /**
292  * Frees a previously allocated named bootmem block.
293  *
294  * @param name   name of block to free
295  *
296  * @return 0 on failure,
297  *         !0 on success
298  */
299 int cvmx_bootmem_free_named(const char *name);
300 
301 /**
302  * Finds a named bootmem block by name.
303  *
304  * @param name   name of block to free
305  *
306  * @return pointer to named block descriptor on success
307  *         0 on failure
308  */
309 const struct cvmx_bootmem_named_block_desc *
310 cvmx_bootmem_find_named_block(const char *name);
311 
312 /**
313  * Returns the size of available memory in bytes, only
314  * counting blocks that are at least as big as the minimum block
315  * size.
316  *
317  * @param min_block_size
318  *               Minimum block size to count in total.
319  *
320  * @return Number of bytes available for allocation that meet the
321  * block size requirement
322  */
323 u64 cvmx_bootmem_available_mem(u64 min_block_size);
324 
325 /**
326  * Prints out the list of named blocks that have been allocated
327  * along with their addresses and sizes.
328  * This is primarily used for debugging purposes
329  */
330 void cvmx_bootmem_print_named(void);
331 
332 /**
333  * Allocates a block of physical memory from the free list, at
334  * (optional) requested address and alignment.
335  *
336  * @param req_size size of region to allocate.  All requests are
337  * rounded up to be a multiple CVMX_BOOTMEM_ALIGNMENT_SIZE bytes size
338  *
339  * @param address_min Minimum address that block can occupy.
340  *
341  * @param address_max Specifies the maximum address_min (inclusive)
342  * that the allocation can use.
343  *
344  * @param alignment Requested alignment of the block.  If this
345  *                  alignment cannot be met, the allocation fails.
346  *                  This must be a power of 2.  (Note: Alignment of
347  *                  CVMX_BOOTMEM_ALIGNMENT_SIZE bytes is required, and
348  *                  internally enforced.  Requested alignments of less
349  *                  than CVMX_BOOTMEM_ALIGNMENT_SIZE are set to
350  *                  CVMX_BOOTMEM_ALIGNMENT_SIZE.)
351  * @param flags     Flags to control options for the allocation.
352  *
353  * @return physical address of block allocated, or -1 on failure
354  */
355 s64 cvmx_bootmem_phy_alloc(u64 req_size, u64 address_min, u64 address_max,
356 			   u64 alignment, u32 flags);
357 
358 /**
359  * Allocates a named block of physical memory from the free list, at
360  * (optional) requested address and alignment.
361  *
362  * @param size size of region to allocate.  All requests are rounded
363  * up to be a multiple CVMX_BOOTMEM_ALIGNMENT_SIZE bytes size
364  *
365  * @param min_addr  Minimum address that block can occupy.
366  *
367  * @param max_addr Specifies the maximum address_min (inclusive) that
368  * the allocation can use.
369  *
370  * @param alignment Requested alignment of the block.  If this
371  *                  alignment cannot be met, the allocation fails.
372  *                  This must be a power of 2.  (Note: Alignment of
373  *                  CVMX_BOOTMEM_ALIGNMENT_SIZE bytes is required, and
374  *                  internally enforced.  Requested alignments of less
375  *                  than CVMX_BOOTMEM_ALIGNMENT_SIZE are set to
376  *                  CVMX_BOOTMEM_ALIGNMENT_SIZE.)
377  *
378  * @param name      name to assign to named block
379  *
380  * @param flags     Flags to control options for the allocation.
381  *
382  * @return physical address of block allocated, or -1 on failure
383  */
384 s64 cvmx_bootmem_phy_named_block_alloc(u64 size, u64 min_addr, u64 max_addr,
385 				       u64 alignment, const char *name,
386 				       u32 flags);
387 
388 /**
389  * Finds a named memory block by name.
390  * Also used for finding an unused entry in the named block table.
391  *
392  * @param name Name of memory block to find.  If NULL pointer given,
393  *             then finds unused descriptor, if available.
394  *
395  * @param flags  Flags to control options for the allocation.
396  *
397  * @return Physical address of the memory block descriptor, zero if not
398  *         found. If zero returned when name parameter is NULL, then no
399  *         memory block descriptors are available.
400  */
401 u64 cvmx_bootmem_phy_named_block_find(const char *name, u32 flags);
402 
403 /**
404  * Returns the size of available memory in bytes, only
405  * counting blocks that are at least as big as the minimum block
406  * size.
407  *
408  * @param min_block_size
409  *               Minimum block size to count in total.
410  *
411  * @return Number of bytes available for allocation that meet the
412  * block size requirement
413  */
414 u64 cvmx_bootmem_phy_available_mem(u64 min_block_size);
415 
416 /**
417  * Frees a named block.
418  *
419  * @param name   name of block to free
420  * @param flags  flags for passing options
421  *
422  * @return 0 on failure
423  *         1 on success
424  */
425 int cvmx_bootmem_phy_named_block_free(const char *name, u32 flags);
426 
427 /**
428  * Frees a block to the bootmem allocator list.  This must
429  * be used with care, as the size provided must match the size
430  * of the block that was allocated, or the list will become
431  * corrupted.
432  *
433  * IMPORTANT:  This is only intended to be used as part of named block
434  * frees and initial population of the free memory list.
435  *                                                      *
436  *
437  * @param phy_addr physical address of block
438  * @param size     size of block in bytes.
439  * @param flags    flags for passing options
440  *
441  * @return 1 on success,
442  *         0 on failure
443  */
444 int __cvmx_bootmem_phy_free(u64 phy_addr, u64 size, u32 flags);
445 
446 /**
447  * Prints the list of currently allocated named blocks
448  *
449  */
450 void cvmx_bootmem_phy_named_block_print(void);
451 
452 /**
453  * Prints the list of available memory.
454  *
455  */
456 void cvmx_bootmem_phy_list_print(void);
457 
458 /**
459  * This function initializes the free memory list used by cvmx_bootmem.
460  * This must be called before any allocations can be done.
461  *
462  * @param mem_size Total memory available, in bytes
463  *
464  * @param low_reserved_bytes Number of bytes to reserve (leave out of
465  * free list) at address 0x0.
466  *
467  * @param desc_buffer Buffer for the bootmem descriptor.  This must be
468  *                 a 32 bit addressable address.
469  *
470  * @return 1 on success
471  *         0 on failure
472  */
473 s64 cvmx_bootmem_phy_mem_list_init(u64 mem_size, u32 low_reserved_bytes,
474 				   struct cvmx_bootmem_desc *desc_buffer);
475 
476 /**
477  * This function initializes the free memory list used by cvmx_bootmem.
478  * This must be called before any allocations can be done.
479  *
480  * @param nodemask Nodemask - one bit per node (bit0->node0, bit1->node1,...)
481  *
482  * @param mem_size[] Array of memory sizes in MBytes per node ([0]->node0,...)
483  *
484  * @param low_reserved_bytes Number of bytes to reserve (leave out of
485  * free list) at address 0x0.
486  *
487  * @param desc_buffer Buffer for the bootmem descriptor.  This must be
488  *                 a 32 bit addressable address.
489  *
490  * @return 1 on success
491  *         0 on failure
492  */
493 s64 cvmx_bootmem_phy_mem_list_init_multi(u8 nodemask, u32 mem_size[],
494 					 u32 low_reserved_bytes,
495 					 struct cvmx_bootmem_desc *desc_buffer);
496 /**
497  * Locks the bootmem allocator.  This is useful in certain situations
498  * where multiple allocations must be made without being interrupted.
499  * This should be used with the CVMX_BOOTMEM_FLAG_NO_LOCKING flag.
500  *
501  */
502 void cvmx_bootmem_lock(void);
503 
504 /**
505  * Unlocks the bootmem allocator.  This is useful in certain situations
506  * where multiple allocations must be made without being interrupted.
507  * This should be used with the CVMX_BOOTMEM_FLAG_NO_LOCKING flag.
508  *
509  */
510 void cvmx_bootmem_unlock(void);
511 
512 /**
513  * Internal use function to get the current descriptor pointer
514  */
515 void *__cvmx_bootmem_internal_get_desc_ptr(void);
516 
517 /**
518  * Internal use.  This is userd to get a pointer to a physical
519  * address.  For linux n32 the physical address in mmaped to a virtual
520  * address and the virtual address is returned.  For n64 the address
521  * is converted to an xkphys address and the xkhpys address is
522  * returned.
523  */
524 void *__cvmx_phys_addr_to_ptr(u64 phys, int size);
525 const struct cvmx_bootmem_named_block_desc *
526 __cvmx_bootmem_find_named_block_flags(const char *name, u32 flags);
527 void *cvmx_bootmem_alloc_named_range_flags(u64 size, u64 min_addr,
528 					   u64 max_addr, u64 align,
529 					   const char *name, u32 flags);
530 u64 cvmx_bootmem_phy_alloc_range(u64 size, u64 alignment,
531 				 u64 min_addr, u64 max_addr);
532 
533 #endif /*   __CVMX_BOOTMEM_H__ */
534