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