Orange Pi5 kernel

Deprecated Linux kernel 5.10.110 for OrangePi 5/5B/5+ boards

3 Commits   0 Branches   0 Tags
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   1) /***********************license start***************
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   2)  * Author: Cavium Networks
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   3)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   4)  * Contact: support@caviumnetworks.com
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   5)  * This file is part of the OCTEON SDK
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   6)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   7)  * Copyright (c) 2003-2008 Cavium Networks
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   8)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   9)  * This file is free software; you can redistribute it and/or modify
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  10)  * it under the terms of the GNU General Public License, Version 2, as
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  11)  * published by the Free Software Foundation.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  12)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  13)  * This file is distributed in the hope that it will be useful, but
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  14)  * AS-IS and WITHOUT ANY WARRANTY; without even the implied warranty
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  15)  * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, TITLE, or
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  16)  * NONINFRINGEMENT.  See the GNU General Public License for more
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  17)  * details.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  18)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  19)  * You should have received a copy of the GNU General Public License
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  20)  * along with this file; if not, write to the Free Software
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  21)  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  22)  * or visit http://www.gnu.org/licenses/.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  23)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  24)  * This file may also be available under a different license from Cavium.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  25)  * Contact Cavium Networks for more information
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  26)  ***********************license end**************************************/
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  27) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  28) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  29)  * Simple allocate only memory allocator.  Used to allocate memory at
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  30)  * application start time.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  31)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  32) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  33) #include <linux/export.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  34) #include <linux/kernel.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  35) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  36) #include <asm/octeon/cvmx.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  37) #include <asm/octeon/cvmx-spinlock.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  38) #include <asm/octeon/cvmx-bootmem.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  39) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  40) /*#define DEBUG */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  41) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  42) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  43) static struct cvmx_bootmem_desc *cvmx_bootmem_desc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  44) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  45) /* See header file for descriptions of functions */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  46) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  47) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  48)  * This macro returns a member of the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  49)  * cvmx_bootmem_named_block_desc_t structure. These members can't
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  50)  * be directly addressed as they might be in memory not directly
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  51)  * reachable. In the case where bootmem is compiled with
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  52)  * LINUX_HOST, the structure itself might be located on a remote
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  53)  * Octeon. The argument "field" is the member name of the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  54)  * cvmx_bootmem_named_block_desc_t to read. Regardless of the type
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  55)  * of the field, the return type is always a uint64_t. The "addr"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  56)  * parameter is the physical address of the structure.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  57)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  58) #define CVMX_BOOTMEM_NAMED_GET_FIELD(addr, field)			\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  59) 	__cvmx_bootmem_desc_get(addr,					\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  60) 		offsetof(struct cvmx_bootmem_named_block_desc, field),	\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  61) 		sizeof_field(struct cvmx_bootmem_named_block_desc, field))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  62) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  63) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  64)  * This function is the implementation of the get macros defined
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  65)  * for individual structure members. The argument are generated
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  66)  * by the macros inorder to read only the needed memory.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  67)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  68)  * @param base   64bit physical address of the complete structure
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  69)  * @param offset Offset from the beginning of the structure to the member being
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  70)  *               accessed.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  71)  * @param size   Size of the structure member.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  72)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  73)  * @return Value of the structure member promoted into a uint64_t.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  74)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  75) static inline uint64_t __cvmx_bootmem_desc_get(uint64_t base, int offset,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  76) 					       int size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  77) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  78) 	base = (1ull << 63) | (base + offset);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  79) 	switch (size) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  80) 	case 4:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  81) 		return cvmx_read64_uint32(base);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  82) 	case 8:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  83) 		return cvmx_read64_uint64(base);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  84) 	default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  85) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  86) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  87) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  88) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  89) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  90)  * Wrapper functions are provided for reading/writing the size and
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  91)  * next block values as these may not be directly addressible (in 32
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  92)  * bit applications, for instance.)  Offsets of data elements in
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  93)  * bootmem list, must match cvmx_bootmem_block_header_t.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  94)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  95) #define NEXT_OFFSET 0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  96) #define SIZE_OFFSET 8
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  97) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  98) static void cvmx_bootmem_phy_set_size(uint64_t addr, uint64_t size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  99) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) 	cvmx_write64_uint64((addr + SIZE_OFFSET) | (1ull << 63), size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) static void cvmx_bootmem_phy_set_next(uint64_t addr, uint64_t next)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) 	cvmx_write64_uint64((addr + NEXT_OFFSET) | (1ull << 63), next);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) static uint64_t cvmx_bootmem_phy_get_size(uint64_t addr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) 	return cvmx_read64_uint64((addr + SIZE_OFFSET) | (1ull << 63));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) static uint64_t cvmx_bootmem_phy_get_next(uint64_t addr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) 	return cvmx_read64_uint64((addr + NEXT_OFFSET) | (1ull << 63));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119)  * Allocate a block of memory from the free list that was
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120)  * passed to the application by the bootloader within a specified
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121)  * address range. This is an allocate-only algorithm, so
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122)  * freeing memory is not possible. Allocation will fail if
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123)  * memory cannot be allocated in the requested range.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125)  * @size:      Size in bytes of block to allocate
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126)  * @min_addr:  defines the minimum address of the range
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127)  * @max_addr:  defines the maximum address of the range
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128)  * @alignment: Alignment required - must be power of 2
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129)  * Returns pointer to block of memory, NULL on error
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) static void *cvmx_bootmem_alloc_range(uint64_t size, uint64_t alignment,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) 				      uint64_t min_addr, uint64_t max_addr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) 	int64_t address;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) 	address =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) 	    cvmx_bootmem_phy_alloc(size, min_addr, max_addr, alignment, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) 	if (address > 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) 		return cvmx_phys_to_ptr(address);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) 		return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) void *cvmx_bootmem_alloc_address(uint64_t size, uint64_t address,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) 				 uint64_t alignment)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) 	return cvmx_bootmem_alloc_range(size, alignment, address,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) 					address + size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) void *cvmx_bootmem_alloc_named_range(uint64_t size, uint64_t min_addr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) 				     uint64_t max_addr, uint64_t align,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) 				     char *name)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) 	int64_t addr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) 	addr = cvmx_bootmem_phy_named_block_alloc(size, min_addr, max_addr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) 						  align, name, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) 	if (addr >= 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) 		return cvmx_phys_to_ptr(addr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) 		return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) void *cvmx_bootmem_alloc_named(uint64_t size, uint64_t alignment, char *name)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167)     return cvmx_bootmem_alloc_named_range(size, 0, 0, alignment, name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) EXPORT_SYMBOL(cvmx_bootmem_alloc_named);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) void cvmx_bootmem_lock(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) 	cvmx_spinlock_lock((cvmx_spinlock_t *) &(cvmx_bootmem_desc->lock));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) void cvmx_bootmem_unlock(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) 	cvmx_spinlock_unlock((cvmx_spinlock_t *) &(cvmx_bootmem_desc->lock));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) int cvmx_bootmem_init(void *mem_desc_ptr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) 	/* Here we set the global pointer to the bootmem descriptor
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) 	 * block.  This pointer will be used directly, so we will set
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) 	 * it up to be directly usable by the application.  It is set
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) 	 * up as follows for the various runtime/ABI combinations:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) 	 *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) 	 * Linux 64 bit: Set XKPHYS bit
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) 	 * Linux 32 bit: use mmap to create mapping, use virtual address
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) 	 * CVMX 64 bit:	 use physical address directly
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) 	 * CVMX 32 bit:	 use physical address directly
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) 	 *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) 	 * Note that the CVMX environment assumes the use of 1-1 TLB
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) 	 * mappings so that the physical addresses can be used
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) 	 * directly
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) 	if (!cvmx_bootmem_desc) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) #if   defined(CVMX_ABI_64)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) 		/* Set XKPHYS bit */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) 		cvmx_bootmem_desc = cvmx_phys_to_ptr(CAST64(mem_desc_ptr));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) #else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) 		cvmx_bootmem_desc = (struct cvmx_bootmem_desc *) mem_desc_ptr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210)  * The cvmx_bootmem_phy* functions below return 64 bit physical
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211)  * addresses, and expose more features that the cvmx_bootmem_functions
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212)  * above.  These are required for full memory space access in 32 bit
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213)  * applications, as well as for using some advance features.  Most
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214)  * applications should not need to use these.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) int64_t cvmx_bootmem_phy_alloc(uint64_t req_size, uint64_t address_min,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) 			       uint64_t address_max, uint64_t alignment,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) 			       uint32_t flags)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) 	uint64_t head_addr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) 	uint64_t ent_addr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) 	/* points to previous list entry, NULL current entry is head of list */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) 	uint64_t prev_addr = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) 	uint64_t new_ent_addr = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) 	uint64_t desired_min_addr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) #ifdef DEBUG
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) 	cvmx_dprintf("cvmx_bootmem_phy_alloc: req_size: 0x%llx, "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) 		     "min_addr: 0x%llx, max_addr: 0x%llx, align: 0x%llx\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) 		     (unsigned long long)req_size,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) 		     (unsigned long long)address_min,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) 		     (unsigned long long)address_max,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) 		     (unsigned long long)alignment);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) 	if (cvmx_bootmem_desc->major_version > 3) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) 		cvmx_dprintf("ERROR: Incompatible bootmem descriptor "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) 			     "version: %d.%d at addr: %p\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) 			     (int)cvmx_bootmem_desc->major_version,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) 			     (int)cvmx_bootmem_desc->minor_version,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) 			     cvmx_bootmem_desc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) 		goto error_out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) 	 * Do a variety of checks to validate the arguments.  The
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) 	 * allocator code will later assume that these checks have
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) 	 * been made.  We validate that the requested constraints are
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) 	 * not self-contradictory before we look through the list of
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) 	 * available memory.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) 	/* 0 is not a valid req_size for this allocator */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) 	if (!req_size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) 		goto error_out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) 	/* Round req_size up to mult of minimum alignment bytes */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) 	req_size = (req_size + (CVMX_BOOTMEM_ALIGNMENT_SIZE - 1)) &
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) 		~(CVMX_BOOTMEM_ALIGNMENT_SIZE - 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) 	 * Convert !0 address_min and 0 address_max to special case of
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) 	 * range that specifies an exact memory block to allocate.  Do
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) 	 * this before other checks and adjustments so that this
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) 	 * tranformation will be validated.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) 	if (address_min && !address_max)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) 		address_max = address_min + req_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) 	else if (!address_min && !address_max)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) 		address_max = ~0ull;  /* If no limits given, use max limits */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) 	 * Enforce minimum alignment (this also keeps the minimum free block
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277) 	 * req_size the same as the alignment req_size.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) 	if (alignment < CVMX_BOOTMEM_ALIGNMENT_SIZE)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280) 		alignment = CVMX_BOOTMEM_ALIGNMENT_SIZE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) 	 * Adjust address minimum based on requested alignment (round
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) 	 * up to meet alignment).  Do this here so we can reject
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) 	 * impossible requests up front. (NOP for address_min == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287) 	if (alignment)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288) 		address_min = ALIGN(address_min, alignment);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291) 	 * Reject inconsistent args.  We have adjusted these, so this
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292) 	 * may fail due to our internal changes even if this check
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293) 	 * would pass for the values the user supplied.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295) 	if (req_size > address_max - address_min)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296) 		goto error_out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298) 	/* Walk through the list entries - first fit found is returned */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300) 	if (!(flags & CVMX_BOOTMEM_FLAG_NO_LOCKING))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301) 		cvmx_bootmem_lock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302) 	head_addr = cvmx_bootmem_desc->head_addr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303) 	ent_addr = head_addr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304) 	for (; ent_addr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305) 	     prev_addr = ent_addr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306) 	     ent_addr = cvmx_bootmem_phy_get_next(ent_addr)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307) 		uint64_t usable_base, usable_max;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308) 		uint64_t ent_size = cvmx_bootmem_phy_get_size(ent_addr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310) 		if (cvmx_bootmem_phy_get_next(ent_addr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311) 		    && ent_addr > cvmx_bootmem_phy_get_next(ent_addr)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312) 			cvmx_dprintf("Internal bootmem_alloc() error: ent: "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313) 				"0x%llx, next: 0x%llx\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314) 				(unsigned long long)ent_addr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315) 				(unsigned long long)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316) 				cvmx_bootmem_phy_get_next(ent_addr));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317) 			goto error_out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321) 		 * Determine if this is an entry that can satisify the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322) 		 * request Check to make sure entry is large enough to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323) 		 * satisfy request.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325) 		usable_base =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326) 		    ALIGN(max(address_min, ent_addr), alignment);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327) 		usable_max = min(address_max, ent_addr + ent_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 328) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 329) 		 * We should be able to allocate block at address
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 330) 		 * usable_base.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 331) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 332) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 333) 		desired_min_addr = usable_base;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 334) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 335) 		 * Determine if request can be satisfied from the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 336) 		 * current entry.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 337) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 338) 		if (!((ent_addr + ent_size) > usable_base
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 339) 				&& ent_addr < address_max
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 340) 				&& req_size <= usable_max - usable_base))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 341) 			continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 342) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 343) 		 * We have found an entry that has room to satisfy the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 344) 		 * request, so allocate it from this entry.  If end
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 345) 		 * CVMX_BOOTMEM_FLAG_END_ALLOC set, then allocate from
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 346) 		 * the end of this block rather than the beginning.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 347) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 348) 		if (flags & CVMX_BOOTMEM_FLAG_END_ALLOC) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 349) 			desired_min_addr = usable_max - req_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 350) 			/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 351) 			 * Align desired address down to required
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 352) 			 * alignment.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 353) 			 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 354) 			desired_min_addr &= ~(alignment - 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 355) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 356) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 357) 		/* Match at start of entry */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 358) 		if (desired_min_addr == ent_addr) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 359) 			if (req_size < ent_size) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 360) 				/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 361) 				 * big enough to create a new block
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 362) 				 * from top portion of block.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 363) 				 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 364) 				new_ent_addr = ent_addr + req_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 365) 				cvmx_bootmem_phy_set_next(new_ent_addr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 366) 					cvmx_bootmem_phy_get_next(ent_addr));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 367) 				cvmx_bootmem_phy_set_size(new_ent_addr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 368) 							ent_size -
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 369) 							req_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 370) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 371) 				/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 372) 				 * Adjust next pointer as following
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 373) 				 * code uses this.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 374) 				 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 375) 				cvmx_bootmem_phy_set_next(ent_addr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 376) 							new_ent_addr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 377) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 378) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 379) 			/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 380) 			 * adjust prev ptr or head to remove this
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 381) 			 * entry from list.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 382) 			 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 383) 			if (prev_addr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 384) 				cvmx_bootmem_phy_set_next(prev_addr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 385) 					cvmx_bootmem_phy_get_next(ent_addr));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 386) 			else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 387) 				/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 388) 				 * head of list being returned, so
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 389) 				 * update head ptr.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 390) 				 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 391) 				cvmx_bootmem_desc->head_addr =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 392) 					cvmx_bootmem_phy_get_next(ent_addr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 393) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 394) 			if (!(flags & CVMX_BOOTMEM_FLAG_NO_LOCKING))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 395) 				cvmx_bootmem_unlock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 396) 			return desired_min_addr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 397) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 398) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 399) 		 * block returned doesn't start at beginning of entry,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 400) 		 * so we know that we will be splitting a block off
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 401) 		 * the front of this one.  Create a new block from the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 402) 		 * beginning, add to list, and go to top of loop
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 403) 		 * again.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 404) 		 *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 405) 		 * create new block from high portion of
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 406) 		 * block, so that top block starts at desired
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 407) 		 * addr.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 408) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 409) 		new_ent_addr = desired_min_addr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 410) 		cvmx_bootmem_phy_set_next(new_ent_addr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 411) 					cvmx_bootmem_phy_get_next
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 412) 					(ent_addr));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 413) 		cvmx_bootmem_phy_set_size(new_ent_addr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 414) 					cvmx_bootmem_phy_get_size
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 415) 					(ent_addr) -
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 416) 					(desired_min_addr -
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 417) 						ent_addr));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 418) 		cvmx_bootmem_phy_set_size(ent_addr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 419) 					desired_min_addr - ent_addr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 420) 		cvmx_bootmem_phy_set_next(ent_addr, new_ent_addr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 421) 		/* Loop again to handle actual alloc from new block */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 422) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 423) error_out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 424) 	/* We didn't find anything, so return error */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 425) 	if (!(flags & CVMX_BOOTMEM_FLAG_NO_LOCKING))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 426) 		cvmx_bootmem_unlock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 427) 	return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 428) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 429) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 430) int __cvmx_bootmem_phy_free(uint64_t phy_addr, uint64_t size, uint32_t flags)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 431) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 432) 	uint64_t cur_addr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 433) 	uint64_t prev_addr = 0; /* zero is invalid */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 434) 	int retval = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 435) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 436) #ifdef DEBUG
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 437) 	cvmx_dprintf("__cvmx_bootmem_phy_free addr: 0x%llx, size: 0x%llx\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 438) 		     (unsigned long long)phy_addr, (unsigned long long)size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 439) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 440) 	if (cvmx_bootmem_desc->major_version > 3) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 441) 		cvmx_dprintf("ERROR: Incompatible bootmem descriptor "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 442) 			     "version: %d.%d at addr: %p\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 443) 			     (int)cvmx_bootmem_desc->major_version,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 444) 			     (int)cvmx_bootmem_desc->minor_version,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 445) 			     cvmx_bootmem_desc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 446) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 447) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 448) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 449) 	/* 0 is not a valid size for this allocator */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 450) 	if (!size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 451) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 452) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 453) 	if (!(flags & CVMX_BOOTMEM_FLAG_NO_LOCKING))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 454) 		cvmx_bootmem_lock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 455) 	cur_addr = cvmx_bootmem_desc->head_addr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 456) 	if (cur_addr == 0 || phy_addr < cur_addr) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 457) 		/* add at front of list - special case with changing head ptr */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 458) 		if (cur_addr && phy_addr + size > cur_addr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 459) 			goto bootmem_free_done; /* error, overlapping section */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 460) 		else if (phy_addr + size == cur_addr) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 461) 			/* Add to front of existing first block */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 462) 			cvmx_bootmem_phy_set_next(phy_addr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 463) 						  cvmx_bootmem_phy_get_next
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 464) 						  (cur_addr));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 465) 			cvmx_bootmem_phy_set_size(phy_addr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 466) 						  cvmx_bootmem_phy_get_size
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 467) 						  (cur_addr) + size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 468) 			cvmx_bootmem_desc->head_addr = phy_addr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 469) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 470) 		} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 471) 			/* New block before first block.  OK if cur_addr is 0 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 472) 			cvmx_bootmem_phy_set_next(phy_addr, cur_addr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 473) 			cvmx_bootmem_phy_set_size(phy_addr, size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 474) 			cvmx_bootmem_desc->head_addr = phy_addr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 475) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 476) 		retval = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 477) 		goto bootmem_free_done;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 478) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 479) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 480) 	/* Find place in list to add block */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 481) 	while (cur_addr && phy_addr > cur_addr) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 482) 		prev_addr = cur_addr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 483) 		cur_addr = cvmx_bootmem_phy_get_next(cur_addr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 484) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 485) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 486) 	if (!cur_addr) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 487) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 488) 		 * We have reached the end of the list, add on to end,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 489) 		 * checking to see if we need to combine with last
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 490) 		 * block
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 491) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 492) 		if (prev_addr + cvmx_bootmem_phy_get_size(prev_addr) ==
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 493) 		    phy_addr) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 494) 			cvmx_bootmem_phy_set_size(prev_addr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 495) 						  cvmx_bootmem_phy_get_size
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 496) 						  (prev_addr) + size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 497) 		} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 498) 			cvmx_bootmem_phy_set_next(prev_addr, phy_addr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 499) 			cvmx_bootmem_phy_set_size(phy_addr, size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 500) 			cvmx_bootmem_phy_set_next(phy_addr, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 501) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 502) 		retval = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 503) 		goto bootmem_free_done;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 504) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 505) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 506) 		 * insert between prev and cur nodes, checking for
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 507) 		 * merge with either/both.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 508) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 509) 		if (prev_addr + cvmx_bootmem_phy_get_size(prev_addr) ==
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 510) 		    phy_addr) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 511) 			/* Merge with previous */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 512) 			cvmx_bootmem_phy_set_size(prev_addr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 513) 						  cvmx_bootmem_phy_get_size
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 514) 						  (prev_addr) + size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 515) 			if (phy_addr + size == cur_addr) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 516) 				/* Also merge with current */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 517) 				cvmx_bootmem_phy_set_size(prev_addr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 518) 					cvmx_bootmem_phy_get_size(cur_addr) +
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 519) 					cvmx_bootmem_phy_get_size(prev_addr));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 520) 				cvmx_bootmem_phy_set_next(prev_addr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 521) 					cvmx_bootmem_phy_get_next(cur_addr));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 522) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 523) 			retval = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 524) 			goto bootmem_free_done;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 525) 		} else if (phy_addr + size == cur_addr) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 526) 			/* Merge with current */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 527) 			cvmx_bootmem_phy_set_size(phy_addr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 528) 						  cvmx_bootmem_phy_get_size
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 529) 						  (cur_addr) + size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 530) 			cvmx_bootmem_phy_set_next(phy_addr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 531) 						  cvmx_bootmem_phy_get_next
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 532) 						  (cur_addr));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 533) 			cvmx_bootmem_phy_set_next(prev_addr, phy_addr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 534) 			retval = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 535) 			goto bootmem_free_done;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 536) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 537) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 538) 		/* It is a standalone block, add in between prev and cur */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 539) 		cvmx_bootmem_phy_set_size(phy_addr, size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 540) 		cvmx_bootmem_phy_set_next(phy_addr, cur_addr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 541) 		cvmx_bootmem_phy_set_next(prev_addr, phy_addr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 542) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 543) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 544) 	retval = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 545) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 546) bootmem_free_done:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 547) 	if (!(flags & CVMX_BOOTMEM_FLAG_NO_LOCKING))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 548) 		cvmx_bootmem_unlock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 549) 	return retval;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 550) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 551) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 552) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 553) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 554)  * Finds a named memory block by name.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 555)  * Also used for finding an unused entry in the named block table.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 556)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 557)  * @name: Name of memory block to find.	 If NULL pointer given, then
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 558)  *	  finds unused descriptor, if available.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 559)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 560)  * @flags: Flags to control options for the allocation.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 561)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 562)  * Returns Pointer to memory block descriptor, NULL if not found.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 563)  *	   If NULL returned when name parameter is NULL, then no memory
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 564)  *	   block descriptors are available.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 565)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 566) static struct cvmx_bootmem_named_block_desc *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 567) 	cvmx_bootmem_phy_named_block_find(char *name, uint32_t flags)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 568) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 569) 	unsigned int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 570) 	struct cvmx_bootmem_named_block_desc *named_block_array_ptr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 571) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 572) #ifdef DEBUG
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 573) 	cvmx_dprintf("cvmx_bootmem_phy_named_block_find: %s\n", name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 574) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 575) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 576) 	 * Lock the structure to make sure that it is not being
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 577) 	 * changed while we are examining it.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 578) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 579) 	if (!(flags & CVMX_BOOTMEM_FLAG_NO_LOCKING))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 580) 		cvmx_bootmem_lock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 581) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 582) 	/* Use XKPHYS for 64 bit linux */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 583) 	named_block_array_ptr = (struct cvmx_bootmem_named_block_desc *)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 584) 	    cvmx_phys_to_ptr(cvmx_bootmem_desc->named_block_array_addr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 585) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 586) #ifdef DEBUG
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 587) 	cvmx_dprintf
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 588) 	    ("cvmx_bootmem_phy_named_block_find: named_block_array_ptr: %p\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 589) 	     named_block_array_ptr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 590) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 591) 	if (cvmx_bootmem_desc->major_version == 3) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 592) 		for (i = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 593) 		     i < cvmx_bootmem_desc->named_block_num_blocks; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 594) 			if ((name && named_block_array_ptr[i].size
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 595) 			     && !strncmp(name, named_block_array_ptr[i].name,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 596) 					 cvmx_bootmem_desc->named_block_name_len
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 597) 					 - 1))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 598) 			    || (!name && !named_block_array_ptr[i].size)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 599) 				if (!(flags & CVMX_BOOTMEM_FLAG_NO_LOCKING))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 600) 					cvmx_bootmem_unlock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 601) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 602) 				return &(named_block_array_ptr[i]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 603) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 604) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 605) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 606) 		cvmx_dprintf("ERROR: Incompatible bootmem descriptor "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 607) 			     "version: %d.%d at addr: %p\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 608) 			     (int)cvmx_bootmem_desc->major_version,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 609) 			     (int)cvmx_bootmem_desc->minor_version,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 610) 			     cvmx_bootmem_desc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 611) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 612) 	if (!(flags & CVMX_BOOTMEM_FLAG_NO_LOCKING))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 613) 		cvmx_bootmem_unlock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 614) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 615) 	return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 616) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 617) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 618) void *cvmx_bootmem_alloc_named_range_once(uint64_t size, uint64_t min_addr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 619) 					  uint64_t max_addr, uint64_t align,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 620) 					  char *name,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 621) 					  void (*init) (void *))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 622) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 623) 	int64_t addr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 624) 	void *ptr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 625) 	uint64_t named_block_desc_addr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 626) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 627) 	named_block_desc_addr = (uint64_t)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 628) 		cvmx_bootmem_phy_named_block_find(name,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 629) 						  (uint32_t)CVMX_BOOTMEM_FLAG_NO_LOCKING);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 630) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 631) 	if (named_block_desc_addr) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 632) 		addr = CVMX_BOOTMEM_NAMED_GET_FIELD(named_block_desc_addr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 633) 						    base_addr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 634) 		return cvmx_phys_to_ptr(addr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 635) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 636) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 637) 	addr = cvmx_bootmem_phy_named_block_alloc(size, min_addr, max_addr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 638) 						  align, name,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 639) 						  (uint32_t)CVMX_BOOTMEM_FLAG_NO_LOCKING);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 640) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 641) 	if (addr < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 642) 		return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 643) 	ptr = cvmx_phys_to_ptr(addr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 644) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 645) 	if (init)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 646) 		init(ptr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 647) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 648) 		memset(ptr, 0, size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 649) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 650) 	return ptr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 651) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 652) EXPORT_SYMBOL(cvmx_bootmem_alloc_named_range_once);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 653) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 654) struct cvmx_bootmem_named_block_desc *cvmx_bootmem_find_named_block(char *name)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 655) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 656) 	return cvmx_bootmem_phy_named_block_find(name, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 657) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 658) EXPORT_SYMBOL(cvmx_bootmem_find_named_block);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 659) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 660) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 661)  * Frees a named block.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 662)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 663)  * @name:   name of block to free
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 664)  * @flags:  flags for passing options
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 665)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 666)  * Returns 0 on failure
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 667)  *	   1 on success
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 668)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 669) static int cvmx_bootmem_phy_named_block_free(char *name, uint32_t flags)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 670) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 671) 	struct cvmx_bootmem_named_block_desc *named_block_ptr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 672) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 673) 	if (cvmx_bootmem_desc->major_version != 3) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 674) 		cvmx_dprintf("ERROR: Incompatible bootmem descriptor version: "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 675) 			     "%d.%d at addr: %p\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 676) 			     (int)cvmx_bootmem_desc->major_version,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 677) 			     (int)cvmx_bootmem_desc->minor_version,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 678) 			     cvmx_bootmem_desc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 679) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 680) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 681) #ifdef DEBUG
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 682) 	cvmx_dprintf("cvmx_bootmem_phy_named_block_free: %s\n", name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 683) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 684) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 685) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 686) 	 * Take lock here, as name lookup/block free/name free need to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 687) 	 * be atomic.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 688) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 689) 	cvmx_bootmem_lock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 690) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 691) 	named_block_ptr =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 692) 	    cvmx_bootmem_phy_named_block_find(name,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 693) 					      CVMX_BOOTMEM_FLAG_NO_LOCKING);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 694) 	if (named_block_ptr) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 695) #ifdef DEBUG
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 696) 		cvmx_dprintf("cvmx_bootmem_phy_named_block_free: "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 697) 			     "%s, base: 0x%llx, size: 0x%llx\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 698) 			     name,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 699) 			     (unsigned long long)named_block_ptr->base_addr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 700) 			     (unsigned long long)named_block_ptr->size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 701) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 702) 		__cvmx_bootmem_phy_free(named_block_ptr->base_addr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 703) 					named_block_ptr->size,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 704) 					CVMX_BOOTMEM_FLAG_NO_LOCKING);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 705) 		named_block_ptr->size = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 706) 		/* Set size to zero to indicate block not used. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 707) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 708) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 709) 	cvmx_bootmem_unlock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 710) 	return named_block_ptr != NULL; /* 0 on failure, 1 on success */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 711) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 712) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 713) int cvmx_bootmem_free_named(char *name)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 714) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 715) 	return cvmx_bootmem_phy_named_block_free(name, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 716) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 717) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 718) int64_t cvmx_bootmem_phy_named_block_alloc(uint64_t size, uint64_t min_addr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 719) 					   uint64_t max_addr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 720) 					   uint64_t alignment,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 721) 					   char *name,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 722) 					   uint32_t flags)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 723) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 724) 	int64_t addr_allocated;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 725) 	struct cvmx_bootmem_named_block_desc *named_block_desc_ptr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 726) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 727) #ifdef DEBUG
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 728) 	cvmx_dprintf("cvmx_bootmem_phy_named_block_alloc: size: 0x%llx, min: "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 729) 		     "0x%llx, max: 0x%llx, align: 0x%llx, name: %s\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 730) 		     (unsigned long long)size,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 731) 		     (unsigned long long)min_addr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 732) 		     (unsigned long long)max_addr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 733) 		     (unsigned long long)alignment,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 734) 		     name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 735) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 736) 	if (cvmx_bootmem_desc->major_version != 3) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 737) 		cvmx_dprintf("ERROR: Incompatible bootmem descriptor version: "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 738) 			     "%d.%d at addr: %p\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 739) 			     (int)cvmx_bootmem_desc->major_version,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 740) 			     (int)cvmx_bootmem_desc->minor_version,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 741) 			     cvmx_bootmem_desc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 742) 		return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 743) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 744) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 745) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 746) 	 * Take lock here, as name lookup/block alloc/name add need to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 747) 	 * be atomic.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 748) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 749) 	if (!(flags & CVMX_BOOTMEM_FLAG_NO_LOCKING))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 750) 		cvmx_spinlock_lock((cvmx_spinlock_t *)&(cvmx_bootmem_desc->lock));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 751) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 752) 	/* Get pointer to first available named block descriptor */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 753) 	named_block_desc_ptr =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 754) 		cvmx_bootmem_phy_named_block_find(NULL,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 755) 						  flags | CVMX_BOOTMEM_FLAG_NO_LOCKING);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 756) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 757) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 758) 	 * Check to see if name already in use, return error if name
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 759) 	 * not available or no more room for blocks.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 760) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 761) 	if (cvmx_bootmem_phy_named_block_find(name,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 762) 					      flags | CVMX_BOOTMEM_FLAG_NO_LOCKING) || !named_block_desc_ptr) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 763) 		if (!(flags & CVMX_BOOTMEM_FLAG_NO_LOCKING))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 764) 			cvmx_spinlock_unlock((cvmx_spinlock_t *)&(cvmx_bootmem_desc->lock));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 765) 		return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 766) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 767) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 768) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 769) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 770) 	 * Round size up to mult of minimum alignment bytes We need
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 771) 	 * the actual size allocated to allow for blocks to be
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 772) 	 * coalesced when they are freed. The alloc routine does the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 773) 	 * same rounding up on all allocations.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 774) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 775) 	size = ALIGN(size, CVMX_BOOTMEM_ALIGNMENT_SIZE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 776) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 777) 	addr_allocated = cvmx_bootmem_phy_alloc(size, min_addr, max_addr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 778) 						alignment,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 779) 						flags | CVMX_BOOTMEM_FLAG_NO_LOCKING);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 780) 	if (addr_allocated >= 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 781) 		named_block_desc_ptr->base_addr = addr_allocated;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 782) 		named_block_desc_ptr->size = size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 783) 		strncpy(named_block_desc_ptr->name, name,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 784) 			cvmx_bootmem_desc->named_block_name_len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 785) 		named_block_desc_ptr->name[cvmx_bootmem_desc->named_block_name_len - 1] = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 786) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 787) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 788) 	if (!(flags & CVMX_BOOTMEM_FLAG_NO_LOCKING))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 789) 		cvmx_spinlock_unlock((cvmx_spinlock_t *)&(cvmx_bootmem_desc->lock));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 790) 	return addr_allocated;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 791) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 792) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 793) struct cvmx_bootmem_desc *cvmx_bootmem_get_desc(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 794) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 795) 	return cvmx_bootmem_desc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 796) }