xref: /linux/arch/mips/cavium-octeon/executive/cvmx-bootmem.c (revision 06d07429858317ded2db7986113a9e0129cd599b)
158f07778SDavid Daney /***********************license start***************
258f07778SDavid Daney  * Author: Cavium Networks
358f07778SDavid Daney  *
458f07778SDavid Daney  * Contact: support@caviumnetworks.com
558f07778SDavid Daney  * This file is part of the OCTEON SDK
658f07778SDavid Daney  *
758f07778SDavid Daney  * Copyright (c) 2003-2008 Cavium Networks
858f07778SDavid Daney  *
958f07778SDavid Daney  * This file is free software; you can redistribute it and/or modify
1058f07778SDavid Daney  * it under the terms of the GNU General Public License, Version 2, as
1158f07778SDavid Daney  * published by the Free Software Foundation.
1258f07778SDavid Daney  *
1358f07778SDavid Daney  * This file is distributed in the hope that it will be useful, but
1458f07778SDavid Daney  * AS-IS and WITHOUT ANY WARRANTY; without even the implied warranty
1558f07778SDavid Daney  * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, TITLE, or
1658f07778SDavid Daney  * NONINFRINGEMENT.  See the GNU General Public License for more
1758f07778SDavid Daney  * details.
1858f07778SDavid Daney  *
1958f07778SDavid Daney  * You should have received a copy of the GNU General Public License
2058f07778SDavid Daney  * along with this file; if not, write to the Free Software
2158f07778SDavid Daney  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
2258f07778SDavid Daney  * or visit http://www.gnu.org/licenses/.
2358f07778SDavid Daney  *
2458f07778SDavid Daney  * This file may also be available under a different license from Cavium.
2558f07778SDavid Daney  * Contact Cavium Networks for more information
2658f07778SDavid Daney  ***********************license end**************************************/
2758f07778SDavid Daney 
2858f07778SDavid Daney /*
2958f07778SDavid Daney  * Simple allocate only memory allocator.  Used to allocate memory at
3058f07778SDavid Daney  * application start time.
3158f07778SDavid Daney  */
3258f07778SDavid Daney 
3326dd3e4fSPaul Gortmaker #include <linux/export.h>
3458f07778SDavid Daney #include <linux/kernel.h>
3558f07778SDavid Daney 
3658f07778SDavid Daney #include <asm/octeon/cvmx.h>
3758f07778SDavid Daney #include <asm/octeon/cvmx-spinlock.h>
3858f07778SDavid Daney #include <asm/octeon/cvmx-bootmem.h>
3958f07778SDavid Daney 
4058f07778SDavid Daney /*#define DEBUG */
4158f07778SDavid Daney 
4258f07778SDavid Daney 
4358f07778SDavid Daney static struct cvmx_bootmem_desc *cvmx_bootmem_desc;
4458f07778SDavid Daney 
4558f07778SDavid Daney /* See header file for descriptions of functions */
4658f07778SDavid Daney 
4716df55ceSRandy Dunlap /*
489438a86aSSteven J. Hill  * This macro returns a member of the
499438a86aSSteven J. Hill  * cvmx_bootmem_named_block_desc_t structure. These members can't
509438a86aSSteven J. Hill  * be directly addressed as they might be in memory not directly
519438a86aSSteven J. Hill  * reachable. In the case where bootmem is compiled with
529438a86aSSteven J. Hill  * LINUX_HOST, the structure itself might be located on a remote
539438a86aSSteven J. Hill  * Octeon. The argument "field" is the member name of the
549438a86aSSteven J. Hill  * cvmx_bootmem_named_block_desc_t to read. Regardless of the type
559438a86aSSteven J. Hill  * of the field, the return type is always a uint64_t. The "addr"
569438a86aSSteven J. Hill  * parameter is the physical address of the structure.
579438a86aSSteven J. Hill  */
589438a86aSSteven J. Hill #define CVMX_BOOTMEM_NAMED_GET_FIELD(addr, field)			\
599438a86aSSteven J. Hill 	__cvmx_bootmem_desc_get(addr,					\
609438a86aSSteven J. Hill 		offsetof(struct cvmx_bootmem_named_block_desc, field),	\
61e4372329SPankaj Bharadiya 		sizeof_field(struct cvmx_bootmem_named_block_desc, field))
629438a86aSSteven J. Hill 
6316df55ceSRandy Dunlap /*
649438a86aSSteven J. Hill  * This function is the implementation of the get macros defined
659438a86aSSteven J. Hill  * for individual structure members. The argument are generated
669438a86aSSteven J. Hill  * by the macros inorder to read only the needed memory.
679438a86aSSteven J. Hill  *
689438a86aSSteven J. Hill  * @param base   64bit physical address of the complete structure
699438a86aSSteven J. Hill  * @param offset Offset from the beginning of the structure to the member being
709438a86aSSteven J. Hill  *               accessed.
719438a86aSSteven J. Hill  * @param size   Size of the structure member.
729438a86aSSteven J. Hill  *
739438a86aSSteven J. Hill  * @return Value of the structure member promoted into a uint64_t.
749438a86aSSteven J. Hill  */
__cvmx_bootmem_desc_get(uint64_t base,int offset,int size)759438a86aSSteven J. Hill static inline uint64_t __cvmx_bootmem_desc_get(uint64_t base, int offset,
769438a86aSSteven J. Hill 					       int size)
779438a86aSSteven J. Hill {
789438a86aSSteven J. Hill 	base = (1ull << 63) | (base + offset);
799438a86aSSteven J. Hill 	switch (size) {
809438a86aSSteven J. Hill 	case 4:
819438a86aSSteven J. Hill 		return cvmx_read64_uint32(base);
829438a86aSSteven J. Hill 	case 8:
839438a86aSSteven J. Hill 		return cvmx_read64_uint64(base);
849438a86aSSteven J. Hill 	default:
859438a86aSSteven J. Hill 		return 0;
869438a86aSSteven J. Hill 	}
879438a86aSSteven J. Hill }
889438a86aSSteven J. Hill 
8958f07778SDavid Daney /*
9058f07778SDavid Daney  * Wrapper functions are provided for reading/writing the size and
9158f07778SDavid Daney  * next block values as these may not be directly addressible (in 32
9258f07778SDavid Daney  * bit applications, for instance.)  Offsets of data elements in
9358f07778SDavid Daney  * bootmem list, must match cvmx_bootmem_block_header_t.
9458f07778SDavid Daney  */
9558f07778SDavid Daney #define NEXT_OFFSET 0
9658f07778SDavid Daney #define SIZE_OFFSET 8
9758f07778SDavid Daney 
cvmx_bootmem_phy_set_size(uint64_t addr,uint64_t size)9858f07778SDavid Daney static void cvmx_bootmem_phy_set_size(uint64_t addr, uint64_t size)
9958f07778SDavid Daney {
10058f07778SDavid Daney 	cvmx_write64_uint64((addr + SIZE_OFFSET) | (1ull << 63), size);
10158f07778SDavid Daney }
10258f07778SDavid Daney 
cvmx_bootmem_phy_set_next(uint64_t addr,uint64_t next)10358f07778SDavid Daney static void cvmx_bootmem_phy_set_next(uint64_t addr, uint64_t next)
10458f07778SDavid Daney {
10558f07778SDavid Daney 	cvmx_write64_uint64((addr + NEXT_OFFSET) | (1ull << 63), next);
10658f07778SDavid Daney }
10758f07778SDavid Daney 
cvmx_bootmem_phy_get_size(uint64_t addr)10858f07778SDavid Daney static uint64_t cvmx_bootmem_phy_get_size(uint64_t addr)
10958f07778SDavid Daney {
11058f07778SDavid Daney 	return cvmx_read64_uint64((addr + SIZE_OFFSET) | (1ull << 63));
11158f07778SDavid Daney }
11258f07778SDavid Daney 
cvmx_bootmem_phy_get_next(uint64_t addr)11358f07778SDavid Daney static uint64_t cvmx_bootmem_phy_get_next(uint64_t addr)
11458f07778SDavid Daney {
11558f07778SDavid Daney 	return cvmx_read64_uint64((addr + NEXT_OFFSET) | (1ull << 63));
11658f07778SDavid Daney }
11758f07778SDavid Daney 
11816df55ceSRandy Dunlap /*
1195f35b33aSAaro Koskinen  * Allocate a block of memory from the free list that was
1205f35b33aSAaro Koskinen  * passed to the application by the bootloader within a specified
1215f35b33aSAaro Koskinen  * address range. This is an allocate-only algorithm, so
1225f35b33aSAaro Koskinen  * freeing memory is not possible. Allocation will fail if
1235f35b33aSAaro Koskinen  * memory cannot be allocated in the requested range.
1245f35b33aSAaro Koskinen  *
1255f35b33aSAaro Koskinen  * @size:      Size in bytes of block to allocate
1265f35b33aSAaro Koskinen  * @min_addr:  defines the minimum address of the range
1275f35b33aSAaro Koskinen  * @max_addr:  defines the maximum address of the range
1285f35b33aSAaro Koskinen  * @alignment: Alignment required - must be power of 2
1295f35b33aSAaro Koskinen  * Returns pointer to block of memory, NULL on error
1305f35b33aSAaro Koskinen  */
cvmx_bootmem_alloc_range(uint64_t size,uint64_t alignment,uint64_t min_addr,uint64_t max_addr)1315f35b33aSAaro Koskinen static void *cvmx_bootmem_alloc_range(uint64_t size, uint64_t alignment,
13258f07778SDavid Daney 				      uint64_t min_addr, uint64_t max_addr)
13358f07778SDavid Daney {
13458f07778SDavid Daney 	int64_t address;
13558f07778SDavid Daney 	address =
13658f07778SDavid Daney 	    cvmx_bootmem_phy_alloc(size, min_addr, max_addr, alignment, 0);
13758f07778SDavid Daney 
13858f07778SDavid Daney 	if (address > 0)
13958f07778SDavid Daney 		return cvmx_phys_to_ptr(address);
14058f07778SDavid Daney 	else
14158f07778SDavid Daney 		return NULL;
14258f07778SDavid Daney }
14358f07778SDavid Daney 
cvmx_bootmem_alloc_address(uint64_t size,uint64_t address,uint64_t alignment)14458f07778SDavid Daney void *cvmx_bootmem_alloc_address(uint64_t size, uint64_t address,
14558f07778SDavid Daney 				 uint64_t alignment)
14658f07778SDavid Daney {
14758f07778SDavid Daney 	return cvmx_bootmem_alloc_range(size, alignment, address,
14858f07778SDavid Daney 					address + size);
14958f07778SDavid Daney }
15058f07778SDavid Daney 
cvmx_bootmem_alloc_named_range(uint64_t size,uint64_t min_addr,uint64_t max_addr,uint64_t align,char * name)1516fa044abSDavid Daney void *cvmx_bootmem_alloc_named_range(uint64_t size, uint64_t min_addr,
1526fa044abSDavid Daney 				     uint64_t max_addr, uint64_t align,
1536fa044abSDavid Daney 				     char *name)
1546fa044abSDavid Daney {
1556fa044abSDavid Daney 	int64_t addr;
1566fa044abSDavid Daney 
1576fa044abSDavid Daney 	addr = cvmx_bootmem_phy_named_block_alloc(size, min_addr, max_addr,
1586fa044abSDavid Daney 						  align, name, 0);
1596fa044abSDavid Daney 	if (addr >= 0)
1606fa044abSDavid Daney 		return cvmx_phys_to_ptr(addr);
1616fa044abSDavid Daney 	else
1626fa044abSDavid Daney 		return NULL;
1636fa044abSDavid Daney }
1646fa044abSDavid Daney 
cvmx_bootmem_alloc_named(uint64_t size,uint64_t alignment,char * name)1656fa044abSDavid Daney void *cvmx_bootmem_alloc_named(uint64_t size, uint64_t alignment, char *name)
1666fa044abSDavid Daney {
1676fa044abSDavid Daney     return cvmx_bootmem_alloc_named_range(size, 0, 0, alignment, name);
1686fa044abSDavid Daney }
1696fa044abSDavid Daney EXPORT_SYMBOL(cvmx_bootmem_alloc_named);
1706fa044abSDavid Daney 
cvmx_bootmem_lock(void)17158f07778SDavid Daney void cvmx_bootmem_lock(void)
17258f07778SDavid Daney {
17358f07778SDavid Daney 	cvmx_spinlock_lock((cvmx_spinlock_t *) &(cvmx_bootmem_desc->lock));
17458f07778SDavid Daney }
17558f07778SDavid Daney 
cvmx_bootmem_unlock(void)17658f07778SDavid Daney void cvmx_bootmem_unlock(void)
17758f07778SDavid Daney {
17858f07778SDavid Daney 	cvmx_spinlock_unlock((cvmx_spinlock_t *) &(cvmx_bootmem_desc->lock));
17958f07778SDavid Daney }
18058f07778SDavid Daney 
cvmx_bootmem_init(void * mem_desc_ptr)18158f07778SDavid Daney int cvmx_bootmem_init(void *mem_desc_ptr)
18258f07778SDavid Daney {
18358f07778SDavid Daney 	/* Here we set the global pointer to the bootmem descriptor
18458f07778SDavid Daney 	 * block.  This pointer will be used directly, so we will set
18558f07778SDavid Daney 	 * it up to be directly usable by the application.  It is set
18658f07778SDavid Daney 	 * up as follows for the various runtime/ABI combinations:
18758f07778SDavid Daney 	 *
18858f07778SDavid Daney 	 * Linux 64 bit: Set XKPHYS bit
18958f07778SDavid Daney 	 * Linux 32 bit: use mmap to create mapping, use virtual address
19058f07778SDavid Daney 	 * CVMX 64 bit:	 use physical address directly
19158f07778SDavid Daney 	 * CVMX 32 bit:	 use physical address directly
19258f07778SDavid Daney 	 *
19358f07778SDavid Daney 	 * Note that the CVMX environment assumes the use of 1-1 TLB
19458f07778SDavid Daney 	 * mappings so that the physical addresses can be used
19558f07778SDavid Daney 	 * directly
19658f07778SDavid Daney 	 */
19758f07778SDavid Daney 	if (!cvmx_bootmem_desc) {
19858f07778SDavid Daney #if   defined(CVMX_ABI_64)
19958f07778SDavid Daney 		/* Set XKPHYS bit */
20058f07778SDavid Daney 		cvmx_bootmem_desc = cvmx_phys_to_ptr(CAST64(mem_desc_ptr));
20158f07778SDavid Daney #else
20258f07778SDavid Daney 		cvmx_bootmem_desc = (struct cvmx_bootmem_desc *) mem_desc_ptr;
20358f07778SDavid Daney #endif
20458f07778SDavid Daney 	}
20558f07778SDavid Daney 
20658f07778SDavid Daney 	return 0;
20758f07778SDavid Daney }
20858f07778SDavid Daney 
20958f07778SDavid Daney /*
21058f07778SDavid Daney  * The cvmx_bootmem_phy* functions below return 64 bit physical
21158f07778SDavid Daney  * addresses, and expose more features that the cvmx_bootmem_functions
21258f07778SDavid Daney  * above.  These are required for full memory space access in 32 bit
21358f07778SDavid Daney  * applications, as well as for using some advance features.  Most
21458f07778SDavid Daney  * applications should not need to use these.
21558f07778SDavid Daney  */
21658f07778SDavid Daney 
cvmx_bootmem_phy_alloc(uint64_t req_size,uint64_t address_min,uint64_t address_max,uint64_t alignment,uint32_t flags)21758f07778SDavid Daney int64_t cvmx_bootmem_phy_alloc(uint64_t req_size, uint64_t address_min,
21858f07778SDavid Daney 			       uint64_t address_max, uint64_t alignment,
21958f07778SDavid Daney 			       uint32_t flags)
22058f07778SDavid Daney {
22158f07778SDavid Daney 
22258f07778SDavid Daney 	uint64_t head_addr;
22358f07778SDavid Daney 	uint64_t ent_addr;
22458f07778SDavid Daney 	/* points to previous list entry, NULL current entry is head of list */
22558f07778SDavid Daney 	uint64_t prev_addr = 0;
22658f07778SDavid Daney 	uint64_t new_ent_addr = 0;
22758f07778SDavid Daney 	uint64_t desired_min_addr;
22858f07778SDavid Daney 
22958f07778SDavid Daney #ifdef DEBUG
23058f07778SDavid Daney 	cvmx_dprintf("cvmx_bootmem_phy_alloc: req_size: 0x%llx, "
23158f07778SDavid Daney 		     "min_addr: 0x%llx, max_addr: 0x%llx, align: 0x%llx\n",
23258f07778SDavid Daney 		     (unsigned long long)req_size,
23358f07778SDavid Daney 		     (unsigned long long)address_min,
23458f07778SDavid Daney 		     (unsigned long long)address_max,
23558f07778SDavid Daney 		     (unsigned long long)alignment);
23658f07778SDavid Daney #endif
23758f07778SDavid Daney 
23858f07778SDavid Daney 	if (cvmx_bootmem_desc->major_version > 3) {
23958f07778SDavid Daney 		cvmx_dprintf("ERROR: Incompatible bootmem descriptor "
24058f07778SDavid Daney 			     "version: %d.%d at addr: %p\n",
24158f07778SDavid Daney 			     (int)cvmx_bootmem_desc->major_version,
24258f07778SDavid Daney 			     (int)cvmx_bootmem_desc->minor_version,
24358f07778SDavid Daney 			     cvmx_bootmem_desc);
24458f07778SDavid Daney 		goto error_out;
24558f07778SDavid Daney 	}
24658f07778SDavid Daney 
24758f07778SDavid Daney 	/*
24858f07778SDavid Daney 	 * Do a variety of checks to validate the arguments.  The
24958f07778SDavid Daney 	 * allocator code will later assume that these checks have
25058f07778SDavid Daney 	 * been made.  We validate that the requested constraints are
25158f07778SDavid Daney 	 * not self-contradictory before we look through the list of
25258f07778SDavid Daney 	 * available memory.
25358f07778SDavid Daney 	 */
25458f07778SDavid Daney 
25558f07778SDavid Daney 	/* 0 is not a valid req_size for this allocator */
25658f07778SDavid Daney 	if (!req_size)
25758f07778SDavid Daney 		goto error_out;
25858f07778SDavid Daney 
25958f07778SDavid Daney 	/* Round req_size up to mult of minimum alignment bytes */
26058f07778SDavid Daney 	req_size = (req_size + (CVMX_BOOTMEM_ALIGNMENT_SIZE - 1)) &
26158f07778SDavid Daney 		~(CVMX_BOOTMEM_ALIGNMENT_SIZE - 1);
26258f07778SDavid Daney 
26358f07778SDavid Daney 	/*
26458f07778SDavid Daney 	 * Convert !0 address_min and 0 address_max to special case of
26558f07778SDavid Daney 	 * range that specifies an exact memory block to allocate.  Do
26658f07778SDavid Daney 	 * this before other checks and adjustments so that this
267*2f9060b1SBjorn Helgaas 	 * transformation will be validated.
26858f07778SDavid Daney 	 */
26958f07778SDavid Daney 	if (address_min && !address_max)
27058f07778SDavid Daney 		address_max = address_min + req_size;
27158f07778SDavid Daney 	else if (!address_min && !address_max)
27258f07778SDavid Daney 		address_max = ~0ull;  /* If no limits given, use max limits */
27358f07778SDavid Daney 
27458f07778SDavid Daney 
27558f07778SDavid Daney 	/*
27658f07778SDavid Daney 	 * Enforce minimum alignment (this also keeps the minimum free block
27758f07778SDavid Daney 	 * req_size the same as the alignment req_size.
27858f07778SDavid Daney 	 */
27958f07778SDavid Daney 	if (alignment < CVMX_BOOTMEM_ALIGNMENT_SIZE)
28058f07778SDavid Daney 		alignment = CVMX_BOOTMEM_ALIGNMENT_SIZE;
28158f07778SDavid Daney 
28258f07778SDavid Daney 	/*
28358f07778SDavid Daney 	 * Adjust address minimum based on requested alignment (round
28458f07778SDavid Daney 	 * up to meet alignment).  Do this here so we can reject
28558f07778SDavid Daney 	 * impossible requests up front. (NOP for address_min == 0)
28658f07778SDavid Daney 	 */
28758f07778SDavid Daney 	if (alignment)
2882a5d6651SMatt Turner 		address_min = ALIGN(address_min, alignment);
28958f07778SDavid Daney 
29058f07778SDavid Daney 	/*
29158f07778SDavid Daney 	 * Reject inconsistent args.  We have adjusted these, so this
29258f07778SDavid Daney 	 * may fail due to our internal changes even if this check
29358f07778SDavid Daney 	 * would pass for the values the user supplied.
29458f07778SDavid Daney 	 */
29558f07778SDavid Daney 	if (req_size > address_max - address_min)
29658f07778SDavid Daney 		goto error_out;
29758f07778SDavid Daney 
29858f07778SDavid Daney 	/* Walk through the list entries - first fit found is returned */
29958f07778SDavid Daney 
30058f07778SDavid Daney 	if (!(flags & CVMX_BOOTMEM_FLAG_NO_LOCKING))
30158f07778SDavid Daney 		cvmx_bootmem_lock();
30258f07778SDavid Daney 	head_addr = cvmx_bootmem_desc->head_addr;
30358f07778SDavid Daney 	ent_addr = head_addr;
30458f07778SDavid Daney 	for (; ent_addr;
30558f07778SDavid Daney 	     prev_addr = ent_addr,
30658f07778SDavid Daney 	     ent_addr = cvmx_bootmem_phy_get_next(ent_addr)) {
30758f07778SDavid Daney 		uint64_t usable_base, usable_max;
30858f07778SDavid Daney 		uint64_t ent_size = cvmx_bootmem_phy_get_size(ent_addr);
30958f07778SDavid Daney 
31058f07778SDavid Daney 		if (cvmx_bootmem_phy_get_next(ent_addr)
31158f07778SDavid Daney 		    && ent_addr > cvmx_bootmem_phy_get_next(ent_addr)) {
31258f07778SDavid Daney 			cvmx_dprintf("Internal bootmem_alloc() error: ent: "
31358f07778SDavid Daney 				"0x%llx, next: 0x%llx\n",
31458f07778SDavid Daney 				(unsigned long long)ent_addr,
31558f07778SDavid Daney 				(unsigned long long)
31658f07778SDavid Daney 				cvmx_bootmem_phy_get_next(ent_addr));
31758f07778SDavid Daney 			goto error_out;
31858f07778SDavid Daney 		}
31958f07778SDavid Daney 
32058f07778SDavid Daney 		/*
32194bd83e4SJulia Lawall 		 * Determine if this is an entry that can satisfy the
32258f07778SDavid Daney 		 * request Check to make sure entry is large enough to
32358f07778SDavid Daney 		 * satisfy request.
32458f07778SDavid Daney 		 */
32558f07778SDavid Daney 		usable_base =
3262a5d6651SMatt Turner 		    ALIGN(max(address_min, ent_addr), alignment);
32758f07778SDavid Daney 		usable_max = min(address_max, ent_addr + ent_size);
32858f07778SDavid Daney 		/*
32958f07778SDavid Daney 		 * We should be able to allocate block at address
33058f07778SDavid Daney 		 * usable_base.
33158f07778SDavid Daney 		 */
33258f07778SDavid Daney 
33358f07778SDavid Daney 		desired_min_addr = usable_base;
33458f07778SDavid Daney 		/*
33558f07778SDavid Daney 		 * Determine if request can be satisfied from the
33658f07778SDavid Daney 		 * current entry.
33758f07778SDavid Daney 		 */
33858f07778SDavid Daney 		if (!((ent_addr + ent_size) > usable_base
33958f07778SDavid Daney 				&& ent_addr < address_max
34058f07778SDavid Daney 				&& req_size <= usable_max - usable_base))
34158f07778SDavid Daney 			continue;
34258f07778SDavid Daney 		/*
34358f07778SDavid Daney 		 * We have found an entry that has room to satisfy the
34458f07778SDavid Daney 		 * request, so allocate it from this entry.  If end
34558f07778SDavid Daney 		 * CVMX_BOOTMEM_FLAG_END_ALLOC set, then allocate from
34658f07778SDavid Daney 		 * the end of this block rather than the beginning.
34758f07778SDavid Daney 		 */
34858f07778SDavid Daney 		if (flags & CVMX_BOOTMEM_FLAG_END_ALLOC) {
34958f07778SDavid Daney 			desired_min_addr = usable_max - req_size;
35058f07778SDavid Daney 			/*
35158f07778SDavid Daney 			 * Align desired address down to required
35258f07778SDavid Daney 			 * alignment.
35358f07778SDavid Daney 			 */
35458f07778SDavid Daney 			desired_min_addr &= ~(alignment - 1);
35558f07778SDavid Daney 		}
35658f07778SDavid Daney 
35758f07778SDavid Daney 		/* Match at start of entry */
35858f07778SDavid Daney 		if (desired_min_addr == ent_addr) {
35958f07778SDavid Daney 			if (req_size < ent_size) {
36058f07778SDavid Daney 				/*
36158f07778SDavid Daney 				 * big enough to create a new block
36258f07778SDavid Daney 				 * from top portion of block.
36358f07778SDavid Daney 				 */
36458f07778SDavid Daney 				new_ent_addr = ent_addr + req_size;
36558f07778SDavid Daney 				cvmx_bootmem_phy_set_next(new_ent_addr,
36658f07778SDavid Daney 					cvmx_bootmem_phy_get_next(ent_addr));
36758f07778SDavid Daney 				cvmx_bootmem_phy_set_size(new_ent_addr,
36858f07778SDavid Daney 							ent_size -
36958f07778SDavid Daney 							req_size);
37058f07778SDavid Daney 
37158f07778SDavid Daney 				/*
37258f07778SDavid Daney 				 * Adjust next pointer as following
37358f07778SDavid Daney 				 * code uses this.
37458f07778SDavid Daney 				 */
37558f07778SDavid Daney 				cvmx_bootmem_phy_set_next(ent_addr,
37658f07778SDavid Daney 							new_ent_addr);
37758f07778SDavid Daney 			}
37858f07778SDavid Daney 
37958f07778SDavid Daney 			/*
38058f07778SDavid Daney 			 * adjust prev ptr or head to remove this
38158f07778SDavid Daney 			 * entry from list.
38258f07778SDavid Daney 			 */
38358f07778SDavid Daney 			if (prev_addr)
38458f07778SDavid Daney 				cvmx_bootmem_phy_set_next(prev_addr,
38558f07778SDavid Daney 					cvmx_bootmem_phy_get_next(ent_addr));
38658f07778SDavid Daney 			else
38758f07778SDavid Daney 				/*
38858f07778SDavid Daney 				 * head of list being returned, so
38958f07778SDavid Daney 				 * update head ptr.
39058f07778SDavid Daney 				 */
39158f07778SDavid Daney 				cvmx_bootmem_desc->head_addr =
39258f07778SDavid Daney 					cvmx_bootmem_phy_get_next(ent_addr);
39358f07778SDavid Daney 
39458f07778SDavid Daney 			if (!(flags & CVMX_BOOTMEM_FLAG_NO_LOCKING))
39558f07778SDavid Daney 				cvmx_bootmem_unlock();
39658f07778SDavid Daney 			return desired_min_addr;
39758f07778SDavid Daney 		}
39858f07778SDavid Daney 		/*
39958f07778SDavid Daney 		 * block returned doesn't start at beginning of entry,
40058f07778SDavid Daney 		 * so we know that we will be splitting a block off
40158f07778SDavid Daney 		 * the front of this one.  Create a new block from the
40258f07778SDavid Daney 		 * beginning, add to list, and go to top of loop
40358f07778SDavid Daney 		 * again.
40458f07778SDavid Daney 		 *
40558f07778SDavid Daney 		 * create new block from high portion of
40658f07778SDavid Daney 		 * block, so that top block starts at desired
40758f07778SDavid Daney 		 * addr.
40858f07778SDavid Daney 		 */
40958f07778SDavid Daney 		new_ent_addr = desired_min_addr;
41058f07778SDavid Daney 		cvmx_bootmem_phy_set_next(new_ent_addr,
41158f07778SDavid Daney 					cvmx_bootmem_phy_get_next
41258f07778SDavid Daney 					(ent_addr));
41358f07778SDavid Daney 		cvmx_bootmem_phy_set_size(new_ent_addr,
41458f07778SDavid Daney 					cvmx_bootmem_phy_get_size
41558f07778SDavid Daney 					(ent_addr) -
41658f07778SDavid Daney 					(desired_min_addr -
41758f07778SDavid Daney 						ent_addr));
41858f07778SDavid Daney 		cvmx_bootmem_phy_set_size(ent_addr,
41958f07778SDavid Daney 					desired_min_addr - ent_addr);
42058f07778SDavid Daney 		cvmx_bootmem_phy_set_next(ent_addr, new_ent_addr);
42158f07778SDavid Daney 		/* Loop again to handle actual alloc from new block */
42258f07778SDavid Daney 	}
42358f07778SDavid Daney error_out:
42458f07778SDavid Daney 	/* We didn't find anything, so return error */
42558f07778SDavid Daney 	if (!(flags & CVMX_BOOTMEM_FLAG_NO_LOCKING))
42658f07778SDavid Daney 		cvmx_bootmem_unlock();
42758f07778SDavid Daney 	return -1;
42858f07778SDavid Daney }
42958f07778SDavid Daney 
__cvmx_bootmem_phy_free(uint64_t phy_addr,uint64_t size,uint32_t flags)43058f07778SDavid Daney int __cvmx_bootmem_phy_free(uint64_t phy_addr, uint64_t size, uint32_t flags)
43158f07778SDavid Daney {
43258f07778SDavid Daney 	uint64_t cur_addr;
43358f07778SDavid Daney 	uint64_t prev_addr = 0; /* zero is invalid */
43458f07778SDavid Daney 	int retval = 0;
43558f07778SDavid Daney 
43658f07778SDavid Daney #ifdef DEBUG
43758f07778SDavid Daney 	cvmx_dprintf("__cvmx_bootmem_phy_free addr: 0x%llx, size: 0x%llx\n",
43858f07778SDavid Daney 		     (unsigned long long)phy_addr, (unsigned long long)size);
43958f07778SDavid Daney #endif
44058f07778SDavid Daney 	if (cvmx_bootmem_desc->major_version > 3) {
44158f07778SDavid Daney 		cvmx_dprintf("ERROR: Incompatible bootmem descriptor "
44258f07778SDavid Daney 			     "version: %d.%d at addr: %p\n",
44358f07778SDavid Daney 			     (int)cvmx_bootmem_desc->major_version,
44458f07778SDavid Daney 			     (int)cvmx_bootmem_desc->minor_version,
44558f07778SDavid Daney 			     cvmx_bootmem_desc);
44658f07778SDavid Daney 		return 0;
44758f07778SDavid Daney 	}
44858f07778SDavid Daney 
44958f07778SDavid Daney 	/* 0 is not a valid size for this allocator */
45058f07778SDavid Daney 	if (!size)
45158f07778SDavid Daney 		return 0;
45258f07778SDavid Daney 
45358f07778SDavid Daney 	if (!(flags & CVMX_BOOTMEM_FLAG_NO_LOCKING))
45458f07778SDavid Daney 		cvmx_bootmem_lock();
45558f07778SDavid Daney 	cur_addr = cvmx_bootmem_desc->head_addr;
45658f07778SDavid Daney 	if (cur_addr == 0 || phy_addr < cur_addr) {
45758f07778SDavid Daney 		/* add at front of list - special case with changing head ptr */
45858f07778SDavid Daney 		if (cur_addr && phy_addr + size > cur_addr)
45958f07778SDavid Daney 			goto bootmem_free_done; /* error, overlapping section */
46058f07778SDavid Daney 		else if (phy_addr + size == cur_addr) {
46158f07778SDavid Daney 			/* Add to front of existing first block */
46258f07778SDavid Daney 			cvmx_bootmem_phy_set_next(phy_addr,
46358f07778SDavid Daney 						  cvmx_bootmem_phy_get_next
46458f07778SDavid Daney 						  (cur_addr));
46558f07778SDavid Daney 			cvmx_bootmem_phy_set_size(phy_addr,
46658f07778SDavid Daney 						  cvmx_bootmem_phy_get_size
46758f07778SDavid Daney 						  (cur_addr) + size);
46858f07778SDavid Daney 			cvmx_bootmem_desc->head_addr = phy_addr;
46958f07778SDavid Daney 
47058f07778SDavid Daney 		} else {
47158f07778SDavid Daney 			/* New block before first block.  OK if cur_addr is 0 */
47258f07778SDavid Daney 			cvmx_bootmem_phy_set_next(phy_addr, cur_addr);
47358f07778SDavid Daney 			cvmx_bootmem_phy_set_size(phy_addr, size);
47458f07778SDavid Daney 			cvmx_bootmem_desc->head_addr = phy_addr;
47558f07778SDavid Daney 		}
47658f07778SDavid Daney 		retval = 1;
47758f07778SDavid Daney 		goto bootmem_free_done;
47858f07778SDavid Daney 	}
47958f07778SDavid Daney 
48058f07778SDavid Daney 	/* Find place in list to add block */
48158f07778SDavid Daney 	while (cur_addr && phy_addr > cur_addr) {
48258f07778SDavid Daney 		prev_addr = cur_addr;
48358f07778SDavid Daney 		cur_addr = cvmx_bootmem_phy_get_next(cur_addr);
48458f07778SDavid Daney 	}
48558f07778SDavid Daney 
48658f07778SDavid Daney 	if (!cur_addr) {
48758f07778SDavid Daney 		/*
48858f07778SDavid Daney 		 * We have reached the end of the list, add on to end,
48958f07778SDavid Daney 		 * checking to see if we need to combine with last
49058f07778SDavid Daney 		 * block
49158f07778SDavid Daney 		 */
49258f07778SDavid Daney 		if (prev_addr + cvmx_bootmem_phy_get_size(prev_addr) ==
49358f07778SDavid Daney 		    phy_addr) {
49458f07778SDavid Daney 			cvmx_bootmem_phy_set_size(prev_addr,
49558f07778SDavid Daney 						  cvmx_bootmem_phy_get_size
49658f07778SDavid Daney 						  (prev_addr) + size);
49758f07778SDavid Daney 		} else {
49858f07778SDavid Daney 			cvmx_bootmem_phy_set_next(prev_addr, phy_addr);
49958f07778SDavid Daney 			cvmx_bootmem_phy_set_size(phy_addr, size);
50058f07778SDavid Daney 			cvmx_bootmem_phy_set_next(phy_addr, 0);
50158f07778SDavid Daney 		}
50258f07778SDavid Daney 		retval = 1;
50358f07778SDavid Daney 		goto bootmem_free_done;
50458f07778SDavid Daney 	} else {
50558f07778SDavid Daney 		/*
50658f07778SDavid Daney 		 * insert between prev and cur nodes, checking for
50758f07778SDavid Daney 		 * merge with either/both.
50858f07778SDavid Daney 		 */
50958f07778SDavid Daney 		if (prev_addr + cvmx_bootmem_phy_get_size(prev_addr) ==
51058f07778SDavid Daney 		    phy_addr) {
51158f07778SDavid Daney 			/* Merge with previous */
51258f07778SDavid Daney 			cvmx_bootmem_phy_set_size(prev_addr,
51358f07778SDavid Daney 						  cvmx_bootmem_phy_get_size
51458f07778SDavid Daney 						  (prev_addr) + size);
51558f07778SDavid Daney 			if (phy_addr + size == cur_addr) {
51658f07778SDavid Daney 				/* Also merge with current */
51758f07778SDavid Daney 				cvmx_bootmem_phy_set_size(prev_addr,
51858f07778SDavid Daney 					cvmx_bootmem_phy_get_size(cur_addr) +
51958f07778SDavid Daney 					cvmx_bootmem_phy_get_size(prev_addr));
52058f07778SDavid Daney 				cvmx_bootmem_phy_set_next(prev_addr,
52158f07778SDavid Daney 					cvmx_bootmem_phy_get_next(cur_addr));
52258f07778SDavid Daney 			}
52358f07778SDavid Daney 			retval = 1;
52458f07778SDavid Daney 			goto bootmem_free_done;
52558f07778SDavid Daney 		} else if (phy_addr + size == cur_addr) {
52658f07778SDavid Daney 			/* Merge with current */
52758f07778SDavid Daney 			cvmx_bootmem_phy_set_size(phy_addr,
52858f07778SDavid Daney 						  cvmx_bootmem_phy_get_size
52958f07778SDavid Daney 						  (cur_addr) + size);
53058f07778SDavid Daney 			cvmx_bootmem_phy_set_next(phy_addr,
53158f07778SDavid Daney 						  cvmx_bootmem_phy_get_next
53258f07778SDavid Daney 						  (cur_addr));
53358f07778SDavid Daney 			cvmx_bootmem_phy_set_next(prev_addr, phy_addr);
53458f07778SDavid Daney 			retval = 1;
53558f07778SDavid Daney 			goto bootmem_free_done;
53658f07778SDavid Daney 		}
53758f07778SDavid Daney 
53858f07778SDavid Daney 		/* It is a standalone block, add in between prev and cur */
53958f07778SDavid Daney 		cvmx_bootmem_phy_set_size(phy_addr, size);
54058f07778SDavid Daney 		cvmx_bootmem_phy_set_next(phy_addr, cur_addr);
54158f07778SDavid Daney 		cvmx_bootmem_phy_set_next(prev_addr, phy_addr);
54258f07778SDavid Daney 
54358f07778SDavid Daney 	}
54458f07778SDavid Daney 	retval = 1;
54558f07778SDavid Daney 
54658f07778SDavid Daney bootmem_free_done:
54758f07778SDavid Daney 	if (!(flags & CVMX_BOOTMEM_FLAG_NO_LOCKING))
54858f07778SDavid Daney 		cvmx_bootmem_unlock();
54958f07778SDavid Daney 	return retval;
55058f07778SDavid Daney 
55158f07778SDavid Daney }
55258f07778SDavid Daney 
55316df55ceSRandy Dunlap /*
554a1afedbcSAaro Koskinen  * Finds a named memory block by name.
555a1afedbcSAaro Koskinen  * Also used for finding an unused entry in the named block table.
556a1afedbcSAaro Koskinen  *
557a1afedbcSAaro Koskinen  * @name: Name of memory block to find.	 If NULL pointer given, then
558a1afedbcSAaro Koskinen  *	  finds unused descriptor, if available.
559a1afedbcSAaro Koskinen  *
560a1afedbcSAaro Koskinen  * @flags: Flags to control options for the allocation.
561a1afedbcSAaro Koskinen  *
562a1afedbcSAaro Koskinen  * Returns Pointer to memory block descriptor, NULL if not found.
563a1afedbcSAaro Koskinen  *	   If NULL returned when name parameter is NULL, then no memory
564a1afedbcSAaro Koskinen  *	   block descriptors are available.
565a1afedbcSAaro Koskinen  */
566a1afedbcSAaro Koskinen static struct cvmx_bootmem_named_block_desc *
cvmx_bootmem_phy_named_block_find(char * name,uint32_t flags)56758f07778SDavid Daney 	cvmx_bootmem_phy_named_block_find(char *name, uint32_t flags)
56858f07778SDavid Daney {
56958f07778SDavid Daney 	unsigned int i;
57058f07778SDavid Daney 	struct cvmx_bootmem_named_block_desc *named_block_array_ptr;
57158f07778SDavid Daney 
57258f07778SDavid Daney #ifdef DEBUG
57358f07778SDavid Daney 	cvmx_dprintf("cvmx_bootmem_phy_named_block_find: %s\n", name);
57458f07778SDavid Daney #endif
57558f07778SDavid Daney 	/*
57658f07778SDavid Daney 	 * Lock the structure to make sure that it is not being
57758f07778SDavid Daney 	 * changed while we are examining it.
57858f07778SDavid Daney 	 */
57958f07778SDavid Daney 	if (!(flags & CVMX_BOOTMEM_FLAG_NO_LOCKING))
58058f07778SDavid Daney 		cvmx_bootmem_lock();
58158f07778SDavid Daney 
58258f07778SDavid Daney 	/* Use XKPHYS for 64 bit linux */
58358f07778SDavid Daney 	named_block_array_ptr = (struct cvmx_bootmem_named_block_desc *)
58458f07778SDavid Daney 	    cvmx_phys_to_ptr(cvmx_bootmem_desc->named_block_array_addr);
58558f07778SDavid Daney 
58658f07778SDavid Daney #ifdef DEBUG
58758f07778SDavid Daney 	cvmx_dprintf
58858f07778SDavid Daney 	    ("cvmx_bootmem_phy_named_block_find: named_block_array_ptr: %p\n",
58958f07778SDavid Daney 	     named_block_array_ptr);
59058f07778SDavid Daney #endif
59158f07778SDavid Daney 	if (cvmx_bootmem_desc->major_version == 3) {
59258f07778SDavid Daney 		for (i = 0;
59358f07778SDavid Daney 		     i < cvmx_bootmem_desc->named_block_num_blocks; i++) {
59458f07778SDavid Daney 			if ((name && named_block_array_ptr[i].size
59558f07778SDavid Daney 			     && !strncmp(name, named_block_array_ptr[i].name,
59658f07778SDavid Daney 					 cvmx_bootmem_desc->named_block_name_len
59758f07778SDavid Daney 					 - 1))
59858f07778SDavid Daney 			    || (!name && !named_block_array_ptr[i].size)) {
59958f07778SDavid Daney 				if (!(flags & CVMX_BOOTMEM_FLAG_NO_LOCKING))
60058f07778SDavid Daney 					cvmx_bootmem_unlock();
60158f07778SDavid Daney 
60258f07778SDavid Daney 				return &(named_block_array_ptr[i]);
60358f07778SDavid Daney 			}
60458f07778SDavid Daney 		}
60558f07778SDavid Daney 	} else {
60658f07778SDavid Daney 		cvmx_dprintf("ERROR: Incompatible bootmem descriptor "
60758f07778SDavid Daney 			     "version: %d.%d at addr: %p\n",
60858f07778SDavid Daney 			     (int)cvmx_bootmem_desc->major_version,
60958f07778SDavid Daney 			     (int)cvmx_bootmem_desc->minor_version,
61058f07778SDavid Daney 			     cvmx_bootmem_desc);
61158f07778SDavid Daney 	}
61258f07778SDavid Daney 	if (!(flags & CVMX_BOOTMEM_FLAG_NO_LOCKING))
61358f07778SDavid Daney 		cvmx_bootmem_unlock();
61458f07778SDavid Daney 
61558f07778SDavid Daney 	return NULL;
61658f07778SDavid Daney }
61758f07778SDavid Daney 
cvmx_bootmem_alloc_named_range_once(uint64_t size,uint64_t min_addr,uint64_t max_addr,uint64_t align,char * name,void (* init)(void *))618bf2d401cSAaro Koskinen void *cvmx_bootmem_alloc_named_range_once(uint64_t size, uint64_t min_addr,
619bf2d401cSAaro Koskinen 					  uint64_t max_addr, uint64_t align,
620bf2d401cSAaro Koskinen 					  char *name,
621bf2d401cSAaro Koskinen 					  void (*init) (void *))
622bf2d401cSAaro Koskinen {
623bf2d401cSAaro Koskinen 	int64_t addr;
624bf2d401cSAaro Koskinen 	void *ptr;
625bf2d401cSAaro Koskinen 	uint64_t named_block_desc_addr;
626bf2d401cSAaro Koskinen 
627bf2d401cSAaro Koskinen 	named_block_desc_addr = (uint64_t)
628bf2d401cSAaro Koskinen 		cvmx_bootmem_phy_named_block_find(name,
629bf2d401cSAaro Koskinen 						  (uint32_t)CVMX_BOOTMEM_FLAG_NO_LOCKING);
630bf2d401cSAaro Koskinen 
631bf2d401cSAaro Koskinen 	if (named_block_desc_addr) {
632bf2d401cSAaro Koskinen 		addr = CVMX_BOOTMEM_NAMED_GET_FIELD(named_block_desc_addr,
633bf2d401cSAaro Koskinen 						    base_addr);
634bf2d401cSAaro Koskinen 		return cvmx_phys_to_ptr(addr);
635bf2d401cSAaro Koskinen 	}
636bf2d401cSAaro Koskinen 
637bf2d401cSAaro Koskinen 	addr = cvmx_bootmem_phy_named_block_alloc(size, min_addr, max_addr,
638bf2d401cSAaro Koskinen 						  align, name,
639bf2d401cSAaro Koskinen 						  (uint32_t)CVMX_BOOTMEM_FLAG_NO_LOCKING);
640bf2d401cSAaro Koskinen 
641bf2d401cSAaro Koskinen 	if (addr < 0)
642bf2d401cSAaro Koskinen 		return NULL;
643bf2d401cSAaro Koskinen 	ptr = cvmx_phys_to_ptr(addr);
644bf2d401cSAaro Koskinen 
645bf2d401cSAaro Koskinen 	if (init)
646bf2d401cSAaro Koskinen 		init(ptr);
647bf2d401cSAaro Koskinen 	else
648bf2d401cSAaro Koskinen 		memset(ptr, 0, size);
649bf2d401cSAaro Koskinen 
650bf2d401cSAaro Koskinen 	return ptr;
651bf2d401cSAaro Koskinen }
652bf2d401cSAaro Koskinen EXPORT_SYMBOL(cvmx_bootmem_alloc_named_range_once);
653bf2d401cSAaro Koskinen 
cvmx_bootmem_find_named_block(char * name)654bf2d401cSAaro Koskinen struct cvmx_bootmem_named_block_desc *cvmx_bootmem_find_named_block(char *name)
655bf2d401cSAaro Koskinen {
656bf2d401cSAaro Koskinen 	return cvmx_bootmem_phy_named_block_find(name, 0);
657bf2d401cSAaro Koskinen }
658bf2d401cSAaro Koskinen EXPORT_SYMBOL(cvmx_bootmem_find_named_block);
659bf2d401cSAaro Koskinen 
66016df55ceSRandy Dunlap /*
661a1afedbcSAaro Koskinen  * Frees a named block.
662a1afedbcSAaro Koskinen  *
663a1afedbcSAaro Koskinen  * @name:   name of block to free
664a1afedbcSAaro Koskinen  * @flags:  flags for passing options
665a1afedbcSAaro Koskinen  *
666a1afedbcSAaro Koskinen  * Returns 0 on failure
667a1afedbcSAaro Koskinen  *	   1 on success
668a1afedbcSAaro Koskinen  */
cvmx_bootmem_phy_named_block_free(char * name,uint32_t flags)669a1afedbcSAaro Koskinen static int cvmx_bootmem_phy_named_block_free(char *name, uint32_t flags)
67058f07778SDavid Daney {
67158f07778SDavid Daney 	struct cvmx_bootmem_named_block_desc *named_block_ptr;
67258f07778SDavid Daney 
67358f07778SDavid Daney 	if (cvmx_bootmem_desc->major_version != 3) {
67458f07778SDavid Daney 		cvmx_dprintf("ERROR: Incompatible bootmem descriptor version: "
67558f07778SDavid Daney 			     "%d.%d at addr: %p\n",
67658f07778SDavid Daney 			     (int)cvmx_bootmem_desc->major_version,
67758f07778SDavid Daney 			     (int)cvmx_bootmem_desc->minor_version,
67858f07778SDavid Daney 			     cvmx_bootmem_desc);
67958f07778SDavid Daney 		return 0;
68058f07778SDavid Daney 	}
68158f07778SDavid Daney #ifdef DEBUG
68258f07778SDavid Daney 	cvmx_dprintf("cvmx_bootmem_phy_named_block_free: %s\n", name);
68358f07778SDavid Daney #endif
68458f07778SDavid Daney 
68558f07778SDavid Daney 	/*
68658f07778SDavid Daney 	 * Take lock here, as name lookup/block free/name free need to
68758f07778SDavid Daney 	 * be atomic.
68858f07778SDavid Daney 	 */
68958f07778SDavid Daney 	cvmx_bootmem_lock();
69058f07778SDavid Daney 
69158f07778SDavid Daney 	named_block_ptr =
69258f07778SDavid Daney 	    cvmx_bootmem_phy_named_block_find(name,
69358f07778SDavid Daney 					      CVMX_BOOTMEM_FLAG_NO_LOCKING);
69458f07778SDavid Daney 	if (named_block_ptr) {
69558f07778SDavid Daney #ifdef DEBUG
69658f07778SDavid Daney 		cvmx_dprintf("cvmx_bootmem_phy_named_block_free: "
69758f07778SDavid Daney 			     "%s, base: 0x%llx, size: 0x%llx\n",
69858f07778SDavid Daney 			     name,
69958f07778SDavid Daney 			     (unsigned long long)named_block_ptr->base_addr,
70058f07778SDavid Daney 			     (unsigned long long)named_block_ptr->size);
70158f07778SDavid Daney #endif
70258f07778SDavid Daney 		__cvmx_bootmem_phy_free(named_block_ptr->base_addr,
70358f07778SDavid Daney 					named_block_ptr->size,
70458f07778SDavid Daney 					CVMX_BOOTMEM_FLAG_NO_LOCKING);
70558f07778SDavid Daney 		named_block_ptr->size = 0;
70658f07778SDavid Daney 		/* Set size to zero to indicate block not used. */
70758f07778SDavid Daney 	}
70858f07778SDavid Daney 
70958f07778SDavid Daney 	cvmx_bootmem_unlock();
71058f07778SDavid Daney 	return named_block_ptr != NULL; /* 0 on failure, 1 on success */
71158f07778SDavid Daney }
7126fa044abSDavid Daney 
cvmx_bootmem_free_named(char * name)713bf2d401cSAaro Koskinen int cvmx_bootmem_free_named(char *name)
714bf2d401cSAaro Koskinen {
715bf2d401cSAaro Koskinen 	return cvmx_bootmem_phy_named_block_free(name, 0);
716bf2d401cSAaro Koskinen }
717bf2d401cSAaro Koskinen 
cvmx_bootmem_phy_named_block_alloc(uint64_t size,uint64_t min_addr,uint64_t max_addr,uint64_t alignment,char * name,uint32_t flags)7186fa044abSDavid Daney int64_t cvmx_bootmem_phy_named_block_alloc(uint64_t size, uint64_t min_addr,
7196fa044abSDavid Daney 					   uint64_t max_addr,
7206fa044abSDavid Daney 					   uint64_t alignment,
7216fa044abSDavid Daney 					   char *name,
7226fa044abSDavid Daney 					   uint32_t flags)
7236fa044abSDavid Daney {
7246fa044abSDavid Daney 	int64_t addr_allocated;
7256fa044abSDavid Daney 	struct cvmx_bootmem_named_block_desc *named_block_desc_ptr;
7266fa044abSDavid Daney 
7276fa044abSDavid Daney #ifdef DEBUG
7286fa044abSDavid Daney 	cvmx_dprintf("cvmx_bootmem_phy_named_block_alloc: size: 0x%llx, min: "
7296fa044abSDavid Daney 		     "0x%llx, max: 0x%llx, align: 0x%llx, name: %s\n",
7306fa044abSDavid Daney 		     (unsigned long long)size,
7316fa044abSDavid Daney 		     (unsigned long long)min_addr,
7326fa044abSDavid Daney 		     (unsigned long long)max_addr,
7336fa044abSDavid Daney 		     (unsigned long long)alignment,
7346fa044abSDavid Daney 		     name);
7356fa044abSDavid Daney #endif
7366fa044abSDavid Daney 	if (cvmx_bootmem_desc->major_version != 3) {
7376fa044abSDavid Daney 		cvmx_dprintf("ERROR: Incompatible bootmem descriptor version: "
7386fa044abSDavid Daney 			     "%d.%d at addr: %p\n",
7396fa044abSDavid Daney 			     (int)cvmx_bootmem_desc->major_version,
7406fa044abSDavid Daney 			     (int)cvmx_bootmem_desc->minor_version,
7416fa044abSDavid Daney 			     cvmx_bootmem_desc);
7426fa044abSDavid Daney 		return -1;
7436fa044abSDavid Daney 	}
7446fa044abSDavid Daney 
7456fa044abSDavid Daney 	/*
7466fa044abSDavid Daney 	 * Take lock here, as name lookup/block alloc/name add need to
7476fa044abSDavid Daney 	 * be atomic.
7486fa044abSDavid Daney 	 */
7496fa044abSDavid Daney 	if (!(flags & CVMX_BOOTMEM_FLAG_NO_LOCKING))
7506fa044abSDavid Daney 		cvmx_spinlock_lock((cvmx_spinlock_t *)&(cvmx_bootmem_desc->lock));
7516fa044abSDavid Daney 
7526fa044abSDavid Daney 	/* Get pointer to first available named block descriptor */
7536fa044abSDavid Daney 	named_block_desc_ptr =
7546fa044abSDavid Daney 		cvmx_bootmem_phy_named_block_find(NULL,
7556fa044abSDavid Daney 						  flags | CVMX_BOOTMEM_FLAG_NO_LOCKING);
7566fa044abSDavid Daney 
7576fa044abSDavid Daney 	/*
7586fa044abSDavid Daney 	 * Check to see if name already in use, return error if name
7596fa044abSDavid Daney 	 * not available or no more room for blocks.
7606fa044abSDavid Daney 	 */
7616fa044abSDavid Daney 	if (cvmx_bootmem_phy_named_block_find(name,
7626fa044abSDavid Daney 					      flags | CVMX_BOOTMEM_FLAG_NO_LOCKING) || !named_block_desc_ptr) {
7636fa044abSDavid Daney 		if (!(flags & CVMX_BOOTMEM_FLAG_NO_LOCKING))
7646fa044abSDavid Daney 			cvmx_spinlock_unlock((cvmx_spinlock_t *)&(cvmx_bootmem_desc->lock));
7656fa044abSDavid Daney 		return -1;
7666fa044abSDavid Daney 	}
7676fa044abSDavid Daney 
7686fa044abSDavid Daney 
7696fa044abSDavid Daney 	/*
7706fa044abSDavid Daney 	 * Round size up to mult of minimum alignment bytes We need
7716fa044abSDavid Daney 	 * the actual size allocated to allow for blocks to be
7724a79fb21SAndrea Gelmini 	 * coalesced when they are freed. The alloc routine does the
7736fa044abSDavid Daney 	 * same rounding up on all allocations.
7746fa044abSDavid Daney 	 */
7752a5d6651SMatt Turner 	size = ALIGN(size, CVMX_BOOTMEM_ALIGNMENT_SIZE);
7766fa044abSDavid Daney 
7776fa044abSDavid Daney 	addr_allocated = cvmx_bootmem_phy_alloc(size, min_addr, max_addr,
7786fa044abSDavid Daney 						alignment,
7796fa044abSDavid Daney 						flags | CVMX_BOOTMEM_FLAG_NO_LOCKING);
7806fa044abSDavid Daney 	if (addr_allocated >= 0) {
7816fa044abSDavid Daney 		named_block_desc_ptr->base_addr = addr_allocated;
7826fa044abSDavid Daney 		named_block_desc_ptr->size = size;
783ef92750eSXu Panda 		strscpy(named_block_desc_ptr->name, name,
7846fa044abSDavid Daney 			cvmx_bootmem_desc->named_block_name_len);
7856fa044abSDavid Daney 	}
7866fa044abSDavid Daney 
7876fa044abSDavid Daney 	if (!(flags & CVMX_BOOTMEM_FLAG_NO_LOCKING))
7886fa044abSDavid Daney 		cvmx_spinlock_unlock((cvmx_spinlock_t *)&(cvmx_bootmem_desc->lock));
7896fa044abSDavid Daney 	return addr_allocated;
7906fa044abSDavid Daney }
791abe77f90SRalf Baechle 
cvmx_bootmem_get_desc(void)792abe77f90SRalf Baechle struct cvmx_bootmem_desc *cvmx_bootmem_get_desc(void)
793abe77f90SRalf Baechle {
794abe77f90SRalf Baechle 	return cvmx_bootmem_desc;
795abe77f90SRalf Baechle }
796