1 /***********************license start***************
2  * Author: Cavium Networks
3  *
4  * Contact: support@caviumnetworks.com
5  * This file is part of the OCTEON SDK
6  *
7  * Copyright (c) 2003-2008 Cavium Networks
8  *
9  * This file is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License, Version 2, as
11  * published by the Free Software Foundation.
12  *
13  * This file is distributed in the hope that it will be useful, but
14  * AS-IS and WITHOUT ANY WARRANTY; without even the implied warranty
15  * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, TITLE, or
16  * NONINFRINGEMENT.  See the GNU General Public License for more
17  * details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this file; if not, write to the Free Software
21  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
22  * or visit http://www.gnu.org/licenses/.
23  *
24  * This file may also be available under a different license from Cavium.
25  * Contact Cavium Networks for more information
26  ***********************license end**************************************/
27 
28 /*
29  * Simple allocate only memory allocator.  Used to allocate memory at
30  * application start time.
31  */
32 
33 #ifndef __CVMX_BOOTMEM_H__
34 #define __CVMX_BOOTMEM_H__
35 /* Must be multiple of 8, changing breaks ABI */
36 #define CVMX_BOOTMEM_NAME_LEN 128
37 
38 /* Can change without breaking ABI */
39 #define CVMX_BOOTMEM_NUM_NAMED_BLOCKS 64
40 
41 /* minimum alignment of bootmem alloced blocks */
42 #define CVMX_BOOTMEM_ALIGNMENT_SIZE	(16ull)
43 
44 /* Flags for cvmx_bootmem_phy_mem* functions */
45 /* Allocate from end of block instead of beginning */
46 #define CVMX_BOOTMEM_FLAG_END_ALLOC    (1 << 0)
47 
48 /* Don't do any locking. */
49 #define CVMX_BOOTMEM_FLAG_NO_LOCKING   (1 << 1)
50 
51 /* First bytes of each free physical block of memory contain this structure,
52  * which is used to maintain the free memory list.  Since the bootloader is
53  * only 32 bits, there is a union providing 64 and 32 bit versions.  The
54  * application init code converts addresses to 64 bit addresses before the
55  * application starts.
56  */
57 struct cvmx_bootmem_block_header {
58 	/*
59 	 * Note: these are referenced from assembly routines in the
60 	 * bootloader, so this structure should not be changed
61 	 * without changing those routines as well.
62 	 */
63 	uint64_t next_block_addr;
64 	uint64_t size;
65 
66 };
67 
68 /*
69  * Structure for named memory blocks.  Number of descriptors available
70  * can be changed without affecting compatibility, but name length
71  * changes require a bump in the bootmem descriptor version Note: This
72  * structure must be naturally 64 bit aligned, as a single memory
73  * image will be used by both 32 and 64 bit programs.
74  */
75 struct cvmx_bootmem_named_block_desc {
76 	/* Base address of named block */
77 	uint64_t base_addr;
78 	/*
79 	 * Size actually allocated for named block (may differ from
80 	 * requested).
81 	 */
82 	uint64_t size;
83 	/* name of named block */
84 	char name[CVMX_BOOTMEM_NAME_LEN];
85 };
86 
87 /* Current descriptor versions */
88 /* CVMX bootmem descriptor major version */
89 #define CVMX_BOOTMEM_DESC_MAJ_VER   3
90 
91 /* CVMX bootmem descriptor minor version */
92 #define CVMX_BOOTMEM_DESC_MIN_VER   0
93 
94 /* First three members of cvmx_bootmem_desc_t are left in original
95  * positions for backwards compatibility.
96  */
97 struct cvmx_bootmem_desc {
98 #if defined(__BIG_ENDIAN_BITFIELD) || defined(CVMX_BUILD_FOR_LINUX_HOST)
99 	/* spinlock to control access to list */
100 	uint32_t lock;
101 	/* flags for indicating various conditions */
102 	uint32_t flags;
103 	uint64_t head_addr;
104 
105 	/* Incremented when incompatible changes made */
106 	uint32_t major_version;
107 
108 	/*
109 	 * Incremented changed when compatible changes made, reset to
110 	 * zero when major incremented.
111 	 */
112 	uint32_t minor_version;
113 
114 	uint64_t app_data_addr;
115 	uint64_t app_data_size;
116 
117 	/* number of elements in named blocks array */
118 	uint32_t named_block_num_blocks;
119 
120 	/* length of name array in bootmem blocks */
121 	uint32_t named_block_name_len;
122 	/* address of named memory block descriptors */
123 	uint64_t named_block_array_addr;
124 #else                           /* __LITTLE_ENDIAN */
125 	uint32_t flags;
126 	uint32_t lock;
127 	uint64_t head_addr;
128 
129 	uint32_t minor_version;
130 	uint32_t major_version;
131 	uint64_t app_data_addr;
132 	uint64_t app_data_size;
133 
134 	uint32_t named_block_name_len;
135 	uint32_t named_block_num_blocks;
136 	uint64_t named_block_array_addr;
137 #endif
138 };
139 
140 /**
141  * Initialize the boot alloc memory structures. This is
142  * normally called inside of cvmx_user_app_init()
143  *
144  * @mem_desc_ptr:	Address of the free memory list
145  */
146 extern int cvmx_bootmem_init(void *mem_desc_ptr);
147 
148 /**
149  * Allocate a block of memory from the free list that was passed
150  * to the application by the bootloader.
151  * This is an allocate-only algorithm, so freeing memory is not possible.
152  *
153  * @size:      Size in bytes of block to allocate
154  * @alignment: Alignment required - must be power of 2
155  *
156  * Returns pointer to block of memory, NULL on error
157  */
158 extern void *cvmx_bootmem_alloc(uint64_t size, uint64_t alignment);
159 
160 /**
161  * Allocate a block of memory from the free list that was
162  * passed to the application by the bootloader at a specific
163  * address. This is an allocate-only algorithm, so
164  * freeing memory is not possible. Allocation will fail if
165  * memory cannot be allocated at the specified address.
166  *
167  * @size:      Size in bytes of block to allocate
168  * @address:   Physical address to allocate memory at.	If this memory is not
169  *		    available, the allocation fails.
170  * @alignment: Alignment required - must be power of 2
171  * Returns pointer to block of memory, NULL on error
172  */
173 extern void *cvmx_bootmem_alloc_address(uint64_t size, uint64_t address,
174 					uint64_t alignment);
175 
176 /**
177  * Allocate a block of memory from the free list that was
178  * passed to the application by the bootloader within a specified
179  * address range. This is an allocate-only algorithm, so
180  * freeing memory is not possible. Allocation will fail if
181  * memory cannot be allocated in the requested range.
182  *
183  * @size:      Size in bytes of block to allocate
184  * @min_addr:  defines the minimum address of the range
185  * @max_addr:  defines the maximum address of the range
186  * @alignment: Alignment required - must be power of 2
187  * Returns pointer to block of memory, NULL on error
188  */
189 extern void *cvmx_bootmem_alloc_range(uint64_t size, uint64_t alignment,
190 				      uint64_t min_addr, uint64_t max_addr);
191 
192 /**
193  * Frees a previously allocated named bootmem block.
194  *
195  * @name:   name of block to free
196  *
197  * Returns 0 on failure,
198  *	   !0 on success
199  */
200 
201 
202 /**
203  * Allocate a block of memory from the free list that was passed
204  * to the application by the bootloader, and assign it a name in the
205  * global named block table.  (part of the cvmx_bootmem_descriptor_t structure)
206  * Named blocks can later be freed.
207  *
208  * @size:      Size in bytes of block to allocate
209  * @alignment: Alignment required - must be power of 2
210  * @name:      name of block - must be less than CVMX_BOOTMEM_NAME_LEN bytes
211  *
212  * Returns a pointer to block of memory, NULL on error
213  */
214 extern void *cvmx_bootmem_alloc_named(uint64_t size, uint64_t alignment,
215 				      char *name);
216 
217 
218 
219 /**
220  * Allocate a block of memory from the free list that was passed
221  * to the application by the bootloader, and assign it a name in the
222  * global named block table.  (part of the cvmx_bootmem_descriptor_t structure)
223  * Named blocks can later be freed.
224  *
225  * @size:     Size in bytes of block to allocate
226  * @address:  Physical address to allocate memory at.  If this
227  *	      memory is not available, the allocation fails.
228  * @name:     name of block - must be less than CVMX_BOOTMEM_NAME_LEN
229  *	      bytes
230  *
231  * Returns a pointer to block of memory, NULL on error
232  */
233 extern void *cvmx_bootmem_alloc_named_address(uint64_t size, uint64_t address,
234 					      char *name);
235 
236 
237 
238 /**
239  * Allocate a block of memory from a specific range of the free list
240  * that was passed to the application by the bootloader, and assign it
241  * a name in the global named block table.  (part of the
242  * cvmx_bootmem_descriptor_t structure) Named blocks can later be
243  * freed.  If request cannot be satisfied within the address range
244  * specified, NULL is returned
245  *
246  * @size:      Size in bytes of block to allocate
247  * @min_addr:  minimum address of range
248  * @max_addr:  maximum address of range
249  * @align:     Alignment of memory to be allocated. (must be a power of 2)
250  * @name:      name of block - must be less than CVMX_BOOTMEM_NAME_LEN bytes
251  *
252  * Returns a pointer to block of memory, NULL on error
253  */
254 extern void *cvmx_bootmem_alloc_named_range(uint64_t size, uint64_t min_addr,
255 					    uint64_t max_addr, uint64_t align,
256 					    char *name);
257 
258 /**
259  * Allocate if needed a block of memory from a specific range of the
260  * free list that was passed to the application by the bootloader, and
261  * assign it a name in the global named block table.  (part of the
262  * cvmx_bootmem_descriptor_t structure) Named blocks can later be
263  * freed.  If the requested name block is already allocated, return
264  * the pointer to block of memory.  If request cannot be satisfied
265  * within the address range specified, NULL is returned
266  *
267  * @param size   Size in bytes of block to allocate
268  * @param min_addr  minimum address of range
269  * @param max_addr  maximum address of range
270  * @param align  Alignment of memory to be allocated. (must be a power of 2)
271  * @param name   name of block - must be less than CVMX_BOOTMEM_NAME_LEN bytes
272  * @param init   Initialization function
273  *
274  * The initialization function is optional, if omitted the named block
275  * is initialized to all zeros when it is created, i.e. once.
276  *
277  * @return pointer to block of memory, NULL on error
278  */
279 void *cvmx_bootmem_alloc_named_range_once(uint64_t size,
280 					  uint64_t min_addr,
281 					  uint64_t max_addr,
282 					  uint64_t align,
283 					  char *name,
284 					  void (*init) (void *));
285 
286 extern int cvmx_bootmem_free_named(char *name);
287 
288 /**
289  * Finds a named bootmem block by name.
290  *
291  * @name:   name of block to free
292  *
293  * Returns pointer to named block descriptor on success
294  *	   0 on failure
295  */
296 struct cvmx_bootmem_named_block_desc *cvmx_bootmem_find_named_block(char *name);
297 
298 /**
299  * Allocates a block of physical memory from the free list, at
300  * (optional) requested address and alignment.
301  *
302  * @req_size: size of region to allocate.  All requests are rounded up
303  *	      to be a multiple CVMX_BOOTMEM_ALIGNMENT_SIZE bytes size
304  *
305  * @address_min: Minimum address that block can occupy.
306  *
307  * @address_max: Specifies the maximum address_min (inclusive) that
308  *		 the allocation can use.
309  *
310  * @alignment: Requested alignment of the block.  If this alignment
311  *	       cannot be met, the allocation fails.  This must be a
312  *	       power of 2.  (Note: Alignment of
313  *	       CVMX_BOOTMEM_ALIGNMENT_SIZE bytes is required, and
314  *	       internally enforced.  Requested alignments of less than
315  *	       CVMX_BOOTMEM_ALIGNMENT_SIZE are set to
316  *	       CVMX_BOOTMEM_ALIGNMENT_SIZE.)
317  *
318  * @flags:     Flags to control options for the allocation.
319  *
320  * Returns physical address of block allocated, or -1 on failure
321  */
322 int64_t cvmx_bootmem_phy_alloc(uint64_t req_size, uint64_t address_min,
323 			       uint64_t address_max, uint64_t alignment,
324 			       uint32_t flags);
325 
326 /**
327  * Allocates a named block of physical memory from the free list, at
328  * (optional) requested address and alignment.
329  *
330  * @param size	    size of region to allocate.	 All requests are rounded
331  *		    up to be a multiple CVMX_BOOTMEM_ALIGNMENT_SIZE
332  *		    bytes size
333  * @param min_addr Minimum address that block can occupy.
334  * @param max_addr  Specifies the maximum address_min (inclusive) that
335  *		    the allocation can use.
336  * @param alignment Requested alignment of the block.  If this
337  *		    alignment cannot be met, the allocation fails.
338  *		    This must be a power of 2.	(Note: Alignment of
339  *		    CVMX_BOOTMEM_ALIGNMENT_SIZE bytes is required, and
340  *		    internally enforced.  Requested alignments of less
341  *		    than CVMX_BOOTMEM_ALIGNMENT_SIZE are set to
342  *		    CVMX_BOOTMEM_ALIGNMENT_SIZE.)
343  * @param name	    name to assign to named block
344  * @param flags	    Flags to control options for the allocation.
345  *
346  * @return physical address of block allocated, or -1 on failure
347  */
348 int64_t cvmx_bootmem_phy_named_block_alloc(uint64_t size, uint64_t min_addr,
349 					   uint64_t max_addr,
350 					   uint64_t alignment,
351 					   char *name, uint32_t flags);
352 
353 /**
354  * Finds a named memory block by name.
355  * Also used for finding an unused entry in the named block table.
356  *
357  * @name: Name of memory block to find.	 If NULL pointer given, then
358  *	  finds unused descriptor, if available.
359  *
360  * @flags: Flags to control options for the allocation.
361  *
362  * Returns Pointer to memory block descriptor, NULL if not found.
363  *	   If NULL returned when name parameter is NULL, then no memory
364  *	   block descriptors are available.
365  */
366 struct cvmx_bootmem_named_block_desc *
367 cvmx_bootmem_phy_named_block_find(char *name, uint32_t flags);
368 
369 /**
370  * Frees a named block.
371  *
372  * @name:   name of block to free
373  * @flags:  flags for passing options
374  *
375  * Returns 0 on failure
376  *	   1 on success
377  */
378 int cvmx_bootmem_phy_named_block_free(char *name, uint32_t flags);
379 
380 /**
381  * Frees a block to the bootmem allocator list.	 This must
382  * be used with care, as the size provided must match the size
383  * of the block that was allocated, or the list will become
384  * corrupted.
385  *
386  * IMPORTANT:  This is only intended to be used as part of named block
387  * frees and initial population of the free memory list.
388  *							*
389  *
390  * @phy_addr: physical address of block
391  * @size:     size of block in bytes.
392  * @flags:    flags for passing options
393  *
394  * Returns 1 on success,
395  *	   0 on failure
396  */
397 int __cvmx_bootmem_phy_free(uint64_t phy_addr, uint64_t size, uint32_t flags);
398 
399 /**
400  * Locks the bootmem allocator.	 This is useful in certain situations
401  * where multiple allocations must be made without being interrupted.
402  * This should be used with the CVMX_BOOTMEM_FLAG_NO_LOCKING flag.
403  *
404  */
405 void cvmx_bootmem_lock(void);
406 
407 /**
408  * Unlocks the bootmem allocator.  This is useful in certain situations
409  * where multiple allocations must be made without being interrupted.
410  * This should be used with the CVMX_BOOTMEM_FLAG_NO_LOCKING flag.
411  *
412  */
413 void cvmx_bootmem_unlock(void);
414 
415 extern struct cvmx_bootmem_desc *cvmx_bootmem_get_desc(void);
416 
417 #endif /*   __CVMX_BOOTMEM_H__ */
418