Path: blob/master/arch/mips/cavium-octeon/executive/cvmx-bootmem.c
10818 views
/***********************license start***************1* Author: Cavium Networks2*3* Contact: [email protected]4* This file is part of the OCTEON SDK5*6* Copyright (c) 2003-2008 Cavium Networks7*8* This file is free software; you can redistribute it and/or modify9* it under the terms of the GNU General Public License, Version 2, as10* published by the Free Software Foundation.11*12* This file is distributed in the hope that it will be useful, but13* AS-IS and WITHOUT ANY WARRANTY; without even the implied warranty14* of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, TITLE, or15* NONINFRINGEMENT. See the GNU General Public License for more16* details.17*18* You should have received a copy of the GNU General Public License19* along with this file; if not, write to the Free Software20* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA21* or visit http://www.gnu.org/licenses/.22*23* This file may also be available under a different license from Cavium.24* Contact Cavium Networks for more information25***********************license end**************************************/2627/*28* Simple allocate only memory allocator. Used to allocate memory at29* application start time.30*/3132#include <linux/kernel.h>33#include <linux/module.h>3435#include <asm/octeon/cvmx.h>36#include <asm/octeon/cvmx-spinlock.h>37#include <asm/octeon/cvmx-bootmem.h>3839/*#define DEBUG */404142static struct cvmx_bootmem_desc *cvmx_bootmem_desc;4344/* See header file for descriptions of functions */4546/*47* Wrapper functions are provided for reading/writing the size and48* next block values as these may not be directly addressible (in 3249* bit applications, for instance.) Offsets of data elements in50* bootmem list, must match cvmx_bootmem_block_header_t.51*/52#define NEXT_OFFSET 053#define SIZE_OFFSET 85455static void cvmx_bootmem_phy_set_size(uint64_t addr, uint64_t size)56{57cvmx_write64_uint64((addr + SIZE_OFFSET) | (1ull << 63), size);58}5960static void cvmx_bootmem_phy_set_next(uint64_t addr, uint64_t next)61{62cvmx_write64_uint64((addr + NEXT_OFFSET) | (1ull << 63), next);63}6465static uint64_t cvmx_bootmem_phy_get_size(uint64_t addr)66{67return cvmx_read64_uint64((addr + SIZE_OFFSET) | (1ull << 63));68}6970static uint64_t cvmx_bootmem_phy_get_next(uint64_t addr)71{72return cvmx_read64_uint64((addr + NEXT_OFFSET) | (1ull << 63));73}7475void *cvmx_bootmem_alloc_range(uint64_t size, uint64_t alignment,76uint64_t min_addr, uint64_t max_addr)77{78int64_t address;79address =80cvmx_bootmem_phy_alloc(size, min_addr, max_addr, alignment, 0);8182if (address > 0)83return cvmx_phys_to_ptr(address);84else85return NULL;86}8788void *cvmx_bootmem_alloc_address(uint64_t size, uint64_t address,89uint64_t alignment)90{91return cvmx_bootmem_alloc_range(size, alignment, address,92address + size);93}9495void *cvmx_bootmem_alloc(uint64_t size, uint64_t alignment)96{97return cvmx_bootmem_alloc_range(size, alignment, 0, 0);98}99100void *cvmx_bootmem_alloc_named_range(uint64_t size, uint64_t min_addr,101uint64_t max_addr, uint64_t align,102char *name)103{104int64_t addr;105106addr = cvmx_bootmem_phy_named_block_alloc(size, min_addr, max_addr,107align, name, 0);108if (addr >= 0)109return cvmx_phys_to_ptr(addr);110else111return NULL;112}113114void *cvmx_bootmem_alloc_named_address(uint64_t size, uint64_t address,115char *name)116{117return cvmx_bootmem_alloc_named_range(size, address, address + size,1180, name);119}120121void *cvmx_bootmem_alloc_named(uint64_t size, uint64_t alignment, char *name)122{123return cvmx_bootmem_alloc_named_range(size, 0, 0, alignment, name);124}125EXPORT_SYMBOL(cvmx_bootmem_alloc_named);126127int cvmx_bootmem_free_named(char *name)128{129return cvmx_bootmem_phy_named_block_free(name, 0);130}131132struct cvmx_bootmem_named_block_desc *cvmx_bootmem_find_named_block(char *name)133{134return cvmx_bootmem_phy_named_block_find(name, 0);135}136EXPORT_SYMBOL(cvmx_bootmem_find_named_block);137138void cvmx_bootmem_lock(void)139{140cvmx_spinlock_lock((cvmx_spinlock_t *) &(cvmx_bootmem_desc->lock));141}142143void cvmx_bootmem_unlock(void)144{145cvmx_spinlock_unlock((cvmx_spinlock_t *) &(cvmx_bootmem_desc->lock));146}147148int cvmx_bootmem_init(void *mem_desc_ptr)149{150/* Here we set the global pointer to the bootmem descriptor151* block. This pointer will be used directly, so we will set152* it up to be directly usable by the application. It is set153* up as follows for the various runtime/ABI combinations:154*155* Linux 64 bit: Set XKPHYS bit156* Linux 32 bit: use mmap to create mapping, use virtual address157* CVMX 64 bit: use physical address directly158* CVMX 32 bit: use physical address directly159*160* Note that the CVMX environment assumes the use of 1-1 TLB161* mappings so that the physical addresses can be used162* directly163*/164if (!cvmx_bootmem_desc) {165#if defined(CVMX_ABI_64)166/* Set XKPHYS bit */167cvmx_bootmem_desc = cvmx_phys_to_ptr(CAST64(mem_desc_ptr));168#else169cvmx_bootmem_desc = (struct cvmx_bootmem_desc *) mem_desc_ptr;170#endif171}172173return 0;174}175176/*177* The cvmx_bootmem_phy* functions below return 64 bit physical178* addresses, and expose more features that the cvmx_bootmem_functions179* above. These are required for full memory space access in 32 bit180* applications, as well as for using some advance features. Most181* applications should not need to use these.182*/183184int64_t cvmx_bootmem_phy_alloc(uint64_t req_size, uint64_t address_min,185uint64_t address_max, uint64_t alignment,186uint32_t flags)187{188189uint64_t head_addr;190uint64_t ent_addr;191/* points to previous list entry, NULL current entry is head of list */192uint64_t prev_addr = 0;193uint64_t new_ent_addr = 0;194uint64_t desired_min_addr;195196#ifdef DEBUG197cvmx_dprintf("cvmx_bootmem_phy_alloc: req_size: 0x%llx, "198"min_addr: 0x%llx, max_addr: 0x%llx, align: 0x%llx\n",199(unsigned long long)req_size,200(unsigned long long)address_min,201(unsigned long long)address_max,202(unsigned long long)alignment);203#endif204205if (cvmx_bootmem_desc->major_version > 3) {206cvmx_dprintf("ERROR: Incompatible bootmem descriptor "207"version: %d.%d at addr: %p\n",208(int)cvmx_bootmem_desc->major_version,209(int)cvmx_bootmem_desc->minor_version,210cvmx_bootmem_desc);211goto error_out;212}213214/*215* Do a variety of checks to validate the arguments. The216* allocator code will later assume that these checks have217* been made. We validate that the requested constraints are218* not self-contradictory before we look through the list of219* available memory.220*/221222/* 0 is not a valid req_size for this allocator */223if (!req_size)224goto error_out;225226/* Round req_size up to mult of minimum alignment bytes */227req_size = (req_size + (CVMX_BOOTMEM_ALIGNMENT_SIZE - 1)) &228~(CVMX_BOOTMEM_ALIGNMENT_SIZE - 1);229230/*231* Convert !0 address_min and 0 address_max to special case of232* range that specifies an exact memory block to allocate. Do233* this before other checks and adjustments so that this234* tranformation will be validated.235*/236if (address_min && !address_max)237address_max = address_min + req_size;238else if (!address_min && !address_max)239address_max = ~0ull; /* If no limits given, use max limits */240241242/*243* Enforce minimum alignment (this also keeps the minimum free block244* req_size the same as the alignment req_size.245*/246if (alignment < CVMX_BOOTMEM_ALIGNMENT_SIZE)247alignment = CVMX_BOOTMEM_ALIGNMENT_SIZE;248249/*250* Adjust address minimum based on requested alignment (round251* up to meet alignment). Do this here so we can reject252* impossible requests up front. (NOP for address_min == 0)253*/254if (alignment)255address_min = ALIGN(address_min, alignment);256257/*258* Reject inconsistent args. We have adjusted these, so this259* may fail due to our internal changes even if this check260* would pass for the values the user supplied.261*/262if (req_size > address_max - address_min)263goto error_out;264265/* Walk through the list entries - first fit found is returned */266267if (!(flags & CVMX_BOOTMEM_FLAG_NO_LOCKING))268cvmx_bootmem_lock();269head_addr = cvmx_bootmem_desc->head_addr;270ent_addr = head_addr;271for (; ent_addr;272prev_addr = ent_addr,273ent_addr = cvmx_bootmem_phy_get_next(ent_addr)) {274uint64_t usable_base, usable_max;275uint64_t ent_size = cvmx_bootmem_phy_get_size(ent_addr);276277if (cvmx_bootmem_phy_get_next(ent_addr)278&& ent_addr > cvmx_bootmem_phy_get_next(ent_addr)) {279cvmx_dprintf("Internal bootmem_alloc() error: ent: "280"0x%llx, next: 0x%llx\n",281(unsigned long long)ent_addr,282(unsigned long long)283cvmx_bootmem_phy_get_next(ent_addr));284goto error_out;285}286287/*288* Determine if this is an entry that can satisify the289* request Check to make sure entry is large enough to290* satisfy request.291*/292usable_base =293ALIGN(max(address_min, ent_addr), alignment);294usable_max = min(address_max, ent_addr + ent_size);295/*296* We should be able to allocate block at address297* usable_base.298*/299300desired_min_addr = usable_base;301/*302* Determine if request can be satisfied from the303* current entry.304*/305if (!((ent_addr + ent_size) > usable_base306&& ent_addr < address_max307&& req_size <= usable_max - usable_base))308continue;309/*310* We have found an entry that has room to satisfy the311* request, so allocate it from this entry. If end312* CVMX_BOOTMEM_FLAG_END_ALLOC set, then allocate from313* the end of this block rather than the beginning.314*/315if (flags & CVMX_BOOTMEM_FLAG_END_ALLOC) {316desired_min_addr = usable_max - req_size;317/*318* Align desired address down to required319* alignment.320*/321desired_min_addr &= ~(alignment - 1);322}323324/* Match at start of entry */325if (desired_min_addr == ent_addr) {326if (req_size < ent_size) {327/*328* big enough to create a new block329* from top portion of block.330*/331new_ent_addr = ent_addr + req_size;332cvmx_bootmem_phy_set_next(new_ent_addr,333cvmx_bootmem_phy_get_next(ent_addr));334cvmx_bootmem_phy_set_size(new_ent_addr,335ent_size -336req_size);337338/*339* Adjust next pointer as following340* code uses this.341*/342cvmx_bootmem_phy_set_next(ent_addr,343new_ent_addr);344}345346/*347* adjust prev ptr or head to remove this348* entry from list.349*/350if (prev_addr)351cvmx_bootmem_phy_set_next(prev_addr,352cvmx_bootmem_phy_get_next(ent_addr));353else354/*355* head of list being returned, so356* update head ptr.357*/358cvmx_bootmem_desc->head_addr =359cvmx_bootmem_phy_get_next(ent_addr);360361if (!(flags & CVMX_BOOTMEM_FLAG_NO_LOCKING))362cvmx_bootmem_unlock();363return desired_min_addr;364}365/*366* block returned doesn't start at beginning of entry,367* so we know that we will be splitting a block off368* the front of this one. Create a new block from the369* beginning, add to list, and go to top of loop370* again.371*372* create new block from high portion of373* block, so that top block starts at desired374* addr.375*/376new_ent_addr = desired_min_addr;377cvmx_bootmem_phy_set_next(new_ent_addr,378cvmx_bootmem_phy_get_next379(ent_addr));380cvmx_bootmem_phy_set_size(new_ent_addr,381cvmx_bootmem_phy_get_size382(ent_addr) -383(desired_min_addr -384ent_addr));385cvmx_bootmem_phy_set_size(ent_addr,386desired_min_addr - ent_addr);387cvmx_bootmem_phy_set_next(ent_addr, new_ent_addr);388/* Loop again to handle actual alloc from new block */389}390error_out:391/* We didn't find anything, so return error */392if (!(flags & CVMX_BOOTMEM_FLAG_NO_LOCKING))393cvmx_bootmem_unlock();394return -1;395}396397int __cvmx_bootmem_phy_free(uint64_t phy_addr, uint64_t size, uint32_t flags)398{399uint64_t cur_addr;400uint64_t prev_addr = 0; /* zero is invalid */401int retval = 0;402403#ifdef DEBUG404cvmx_dprintf("__cvmx_bootmem_phy_free addr: 0x%llx, size: 0x%llx\n",405(unsigned long long)phy_addr, (unsigned long long)size);406#endif407if (cvmx_bootmem_desc->major_version > 3) {408cvmx_dprintf("ERROR: Incompatible bootmem descriptor "409"version: %d.%d at addr: %p\n",410(int)cvmx_bootmem_desc->major_version,411(int)cvmx_bootmem_desc->minor_version,412cvmx_bootmem_desc);413return 0;414}415416/* 0 is not a valid size for this allocator */417if (!size)418return 0;419420if (!(flags & CVMX_BOOTMEM_FLAG_NO_LOCKING))421cvmx_bootmem_lock();422cur_addr = cvmx_bootmem_desc->head_addr;423if (cur_addr == 0 || phy_addr < cur_addr) {424/* add at front of list - special case with changing head ptr */425if (cur_addr && phy_addr + size > cur_addr)426goto bootmem_free_done; /* error, overlapping section */427else if (phy_addr + size == cur_addr) {428/* Add to front of existing first block */429cvmx_bootmem_phy_set_next(phy_addr,430cvmx_bootmem_phy_get_next431(cur_addr));432cvmx_bootmem_phy_set_size(phy_addr,433cvmx_bootmem_phy_get_size434(cur_addr) + size);435cvmx_bootmem_desc->head_addr = phy_addr;436437} else {438/* New block before first block. OK if cur_addr is 0 */439cvmx_bootmem_phy_set_next(phy_addr, cur_addr);440cvmx_bootmem_phy_set_size(phy_addr, size);441cvmx_bootmem_desc->head_addr = phy_addr;442}443retval = 1;444goto bootmem_free_done;445}446447/* Find place in list to add block */448while (cur_addr && phy_addr > cur_addr) {449prev_addr = cur_addr;450cur_addr = cvmx_bootmem_phy_get_next(cur_addr);451}452453if (!cur_addr) {454/*455* We have reached the end of the list, add on to end,456* checking to see if we need to combine with last457* block458*/459if (prev_addr + cvmx_bootmem_phy_get_size(prev_addr) ==460phy_addr) {461cvmx_bootmem_phy_set_size(prev_addr,462cvmx_bootmem_phy_get_size463(prev_addr) + size);464} else {465cvmx_bootmem_phy_set_next(prev_addr, phy_addr);466cvmx_bootmem_phy_set_size(phy_addr, size);467cvmx_bootmem_phy_set_next(phy_addr, 0);468}469retval = 1;470goto bootmem_free_done;471} else {472/*473* insert between prev and cur nodes, checking for474* merge with either/both.475*/476if (prev_addr + cvmx_bootmem_phy_get_size(prev_addr) ==477phy_addr) {478/* Merge with previous */479cvmx_bootmem_phy_set_size(prev_addr,480cvmx_bootmem_phy_get_size481(prev_addr) + size);482if (phy_addr + size == cur_addr) {483/* Also merge with current */484cvmx_bootmem_phy_set_size(prev_addr,485cvmx_bootmem_phy_get_size(cur_addr) +486cvmx_bootmem_phy_get_size(prev_addr));487cvmx_bootmem_phy_set_next(prev_addr,488cvmx_bootmem_phy_get_next(cur_addr));489}490retval = 1;491goto bootmem_free_done;492} else if (phy_addr + size == cur_addr) {493/* Merge with current */494cvmx_bootmem_phy_set_size(phy_addr,495cvmx_bootmem_phy_get_size496(cur_addr) + size);497cvmx_bootmem_phy_set_next(phy_addr,498cvmx_bootmem_phy_get_next499(cur_addr));500cvmx_bootmem_phy_set_next(prev_addr, phy_addr);501retval = 1;502goto bootmem_free_done;503}504505/* It is a standalone block, add in between prev and cur */506cvmx_bootmem_phy_set_size(phy_addr, size);507cvmx_bootmem_phy_set_next(phy_addr, cur_addr);508cvmx_bootmem_phy_set_next(prev_addr, phy_addr);509510}511retval = 1;512513bootmem_free_done:514if (!(flags & CVMX_BOOTMEM_FLAG_NO_LOCKING))515cvmx_bootmem_unlock();516return retval;517518}519520struct cvmx_bootmem_named_block_desc *521cvmx_bootmem_phy_named_block_find(char *name, uint32_t flags)522{523unsigned int i;524struct cvmx_bootmem_named_block_desc *named_block_array_ptr;525526#ifdef DEBUG527cvmx_dprintf("cvmx_bootmem_phy_named_block_find: %s\n", name);528#endif529/*530* Lock the structure to make sure that it is not being531* changed while we are examining it.532*/533if (!(flags & CVMX_BOOTMEM_FLAG_NO_LOCKING))534cvmx_bootmem_lock();535536/* Use XKPHYS for 64 bit linux */537named_block_array_ptr = (struct cvmx_bootmem_named_block_desc *)538cvmx_phys_to_ptr(cvmx_bootmem_desc->named_block_array_addr);539540#ifdef DEBUG541cvmx_dprintf542("cvmx_bootmem_phy_named_block_find: named_block_array_ptr: %p\n",543named_block_array_ptr);544#endif545if (cvmx_bootmem_desc->major_version == 3) {546for (i = 0;547i < cvmx_bootmem_desc->named_block_num_blocks; i++) {548if ((name && named_block_array_ptr[i].size549&& !strncmp(name, named_block_array_ptr[i].name,550cvmx_bootmem_desc->named_block_name_len551- 1))552|| (!name && !named_block_array_ptr[i].size)) {553if (!(flags & CVMX_BOOTMEM_FLAG_NO_LOCKING))554cvmx_bootmem_unlock();555556return &(named_block_array_ptr[i]);557}558}559} else {560cvmx_dprintf("ERROR: Incompatible bootmem descriptor "561"version: %d.%d at addr: %p\n",562(int)cvmx_bootmem_desc->major_version,563(int)cvmx_bootmem_desc->minor_version,564cvmx_bootmem_desc);565}566if (!(flags & CVMX_BOOTMEM_FLAG_NO_LOCKING))567cvmx_bootmem_unlock();568569return NULL;570}571572int cvmx_bootmem_phy_named_block_free(char *name, uint32_t flags)573{574struct cvmx_bootmem_named_block_desc *named_block_ptr;575576if (cvmx_bootmem_desc->major_version != 3) {577cvmx_dprintf("ERROR: Incompatible bootmem descriptor version: "578"%d.%d at addr: %p\n",579(int)cvmx_bootmem_desc->major_version,580(int)cvmx_bootmem_desc->minor_version,581cvmx_bootmem_desc);582return 0;583}584#ifdef DEBUG585cvmx_dprintf("cvmx_bootmem_phy_named_block_free: %s\n", name);586#endif587588/*589* Take lock here, as name lookup/block free/name free need to590* be atomic.591*/592cvmx_bootmem_lock();593594named_block_ptr =595cvmx_bootmem_phy_named_block_find(name,596CVMX_BOOTMEM_FLAG_NO_LOCKING);597if (named_block_ptr) {598#ifdef DEBUG599cvmx_dprintf("cvmx_bootmem_phy_named_block_free: "600"%s, base: 0x%llx, size: 0x%llx\n",601name,602(unsigned long long)named_block_ptr->base_addr,603(unsigned long long)named_block_ptr->size);604#endif605__cvmx_bootmem_phy_free(named_block_ptr->base_addr,606named_block_ptr->size,607CVMX_BOOTMEM_FLAG_NO_LOCKING);608named_block_ptr->size = 0;609/* Set size to zero to indicate block not used. */610}611612cvmx_bootmem_unlock();613return named_block_ptr != NULL; /* 0 on failure, 1 on success */614}615616int64_t cvmx_bootmem_phy_named_block_alloc(uint64_t size, uint64_t min_addr,617uint64_t max_addr,618uint64_t alignment,619char *name,620uint32_t flags)621{622int64_t addr_allocated;623struct cvmx_bootmem_named_block_desc *named_block_desc_ptr;624625#ifdef DEBUG626cvmx_dprintf("cvmx_bootmem_phy_named_block_alloc: size: 0x%llx, min: "627"0x%llx, max: 0x%llx, align: 0x%llx, name: %s\n",628(unsigned long long)size,629(unsigned long long)min_addr,630(unsigned long long)max_addr,631(unsigned long long)alignment,632name);633#endif634if (cvmx_bootmem_desc->major_version != 3) {635cvmx_dprintf("ERROR: Incompatible bootmem descriptor version: "636"%d.%d at addr: %p\n",637(int)cvmx_bootmem_desc->major_version,638(int)cvmx_bootmem_desc->minor_version,639cvmx_bootmem_desc);640return -1;641}642643/*644* Take lock here, as name lookup/block alloc/name add need to645* be atomic.646*/647if (!(flags & CVMX_BOOTMEM_FLAG_NO_LOCKING))648cvmx_spinlock_lock((cvmx_spinlock_t *)&(cvmx_bootmem_desc->lock));649650/* Get pointer to first available named block descriptor */651named_block_desc_ptr =652cvmx_bootmem_phy_named_block_find(NULL,653flags | CVMX_BOOTMEM_FLAG_NO_LOCKING);654655/*656* Check to see if name already in use, return error if name657* not available or no more room for blocks.658*/659if (cvmx_bootmem_phy_named_block_find(name,660flags | CVMX_BOOTMEM_FLAG_NO_LOCKING) || !named_block_desc_ptr) {661if (!(flags & CVMX_BOOTMEM_FLAG_NO_LOCKING))662cvmx_spinlock_unlock((cvmx_spinlock_t *)&(cvmx_bootmem_desc->lock));663return -1;664}665666667/*668* Round size up to mult of minimum alignment bytes We need669* the actual size allocated to allow for blocks to be670* coallesced when they are freed. The alloc routine does the671* same rounding up on all allocations.672*/673size = ALIGN(size, CVMX_BOOTMEM_ALIGNMENT_SIZE);674675addr_allocated = cvmx_bootmem_phy_alloc(size, min_addr, max_addr,676alignment,677flags | CVMX_BOOTMEM_FLAG_NO_LOCKING);678if (addr_allocated >= 0) {679named_block_desc_ptr->base_addr = addr_allocated;680named_block_desc_ptr->size = size;681strncpy(named_block_desc_ptr->name, name,682cvmx_bootmem_desc->named_block_name_len);683named_block_desc_ptr->name[cvmx_bootmem_desc->named_block_name_len - 1] = 0;684}685686if (!(flags & CVMX_BOOTMEM_FLAG_NO_LOCKING))687cvmx_spinlock_unlock((cvmx_spinlock_t *)&(cvmx_bootmem_desc->lock));688return addr_allocated;689}690691692