cvmx-fpa.h revision 219694
1/***********************license start*************** 2 * Copyright (c) 2003-2010 Cavium Networks (support@cavium.com). All rights 3 * reserved. 4 * 5 * 6 * Redistribution and use in source and binary forms, with or without 7 * modification, are permitted provided that the following conditions are 8 * met: 9 * 10 * * Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer. 12 * 13 * * Redistributions in binary form must reproduce the above 14 * copyright notice, this list of conditions and the following 15 * disclaimer in the documentation and/or other materials provided 16 * with the distribution. 17 18 * * Neither the name of Cavium Networks nor the names of 19 * its contributors may be used to endorse or promote products 20 * derived from this software without specific prior written 21 * permission. 22 23 * This Software, including technical data, may be subject to U.S. export control 24 * laws, including the U.S. Export Administration Act and its associated 25 * regulations, and may be subject to export or import regulations in other 26 * countries. 27 28 * TO THE MAXIMUM EXTENT PERMITTED BY LAW, THE SOFTWARE IS PROVIDED "AS IS" 29 * AND WITH ALL FAULTS AND CAVIUM NETWORKS MAKES NO PROMISES, REPRESENTATIONS OR 30 * WARRANTIES, EITHER EXPRESS, IMPLIED, STATUTORY, OR OTHERWISE, WITH RESPECT TO 31 * THE SOFTWARE, INCLUDING ITS CONDITION, ITS CONFORMITY TO ANY REPRESENTATION OR 32 * DESCRIPTION, OR THE EXISTENCE OF ANY LATENT OR PATENT DEFECTS, AND CAVIUM 33 * SPECIFICALLY DISCLAIMS ALL IMPLIED (IF ANY) WARRANTIES OF TITLE, 34 * MERCHANTABILITY, NONINFRINGEMENT, FITNESS FOR A PARTICULAR PURPOSE, LACK OF 35 * VIRUSES, ACCURACY OR COMPLETENESS, QUIET ENJOYMENT, QUIET POSSESSION OR 36 * CORRESPONDENCE TO DESCRIPTION. THE ENTIRE RISK ARISING OUT OF USE OR 37 * PERFORMANCE OF THE SOFTWARE LIES WITH YOU. 38 ***********************license end**************************************/ 39 40 41/** 42 * @file 43 * 44 * Interface to the hardware Free Pool Allocator. 45 * 46 * <hr>$Revision: 50048 $<hr> 47 * 48 */ 49 50#ifndef __CVMX_FPA_H__ 51#define __CVMX_FPA_H__ 52 53#include "cvmx-scratch.h" 54 55#ifdef CVMX_BUILD_FOR_LINUX_KERNEL 56#include "cvmx-fpa-defs.h" 57#endif 58 59#ifdef __cplusplus 60extern "C" { 61#endif 62 63#define CVMX_FPA_NUM_POOLS 8 64#define CVMX_FPA_MIN_BLOCK_SIZE 128 65#define CVMX_FPA_ALIGNMENT 128 66 67/** 68 * Structure describing the data format used for stores to the FPA. 69 */ 70typedef union 71{ 72 uint64_t u64; 73 struct { 74 uint64_t scraddr : 8; /**< the (64-bit word) location in scratchpad to write to (if len != 0) */ 75 uint64_t len : 8; /**< the number of words in the response (0 => no response) */ 76 uint64_t did : 8; /**< the ID of the device on the non-coherent bus */ 77 uint64_t addr :40; /**< the address that will appear in the first tick on the NCB bus */ 78 } s; 79} cvmx_fpa_iobdma_data_t; 80 81/** 82 * Structure describing the current state of a FPA pool. 83 */ 84typedef struct 85{ 86 const char *name; /**< Name it was created under */ 87 uint64_t size; /**< Size of each block */ 88 void * base; /**< The base memory address of whole block */ 89 uint64_t starting_element_count; /**< The number of elements in the pool at creation */ 90} cvmx_fpa_pool_info_t; 91 92/** 93 * Current state of all the pools. Use access functions 94 * instead of using it directly. 95 */ 96extern cvmx_fpa_pool_info_t cvmx_fpa_pool_info[CVMX_FPA_NUM_POOLS]; 97 98/* CSR typedefs have been moved to cvmx-fpa-defs.h */ 99 100/** 101 * Return the name of the pool 102 * 103 * @param pool Pool to get the name of 104 * @return The name 105 */ 106static inline const char *cvmx_fpa_get_name(uint64_t pool) 107{ 108 return cvmx_fpa_pool_info[pool].name; 109} 110 111/** 112 * Return the base of the pool 113 * 114 * @param pool Pool to get the base of 115 * @return The base 116 */ 117static inline void *cvmx_fpa_get_base(uint64_t pool) 118{ 119 return cvmx_fpa_pool_info[pool].base; 120} 121 122/** 123 * Check if a pointer belongs to an FPA pool. Return non-zero 124 * if the supplied pointer is inside the memory controlled by 125 * an FPA pool. 126 * 127 * @param pool Pool to check 128 * @param ptr Pointer to check 129 * @return Non-zero if pointer is in the pool. Zero if not 130 */ 131static inline int cvmx_fpa_is_member(uint64_t pool, void *ptr) 132{ 133 return ((ptr >= cvmx_fpa_pool_info[pool].base) && 134 ((char*)ptr < ((char*)(cvmx_fpa_pool_info[pool].base)) + cvmx_fpa_pool_info[pool].size * cvmx_fpa_pool_info[pool].starting_element_count)); 135} 136 137/** 138 * Enable the FPA for use. Must be performed after any CSR 139 * configuration but before any other FPA functions. 140 */ 141static inline void cvmx_fpa_enable(void) 142{ 143 cvmx_fpa_ctl_status_t status; 144 145 status.u64 = cvmx_read_csr(CVMX_FPA_CTL_STATUS); 146 if (status.s.enb) 147 { 148 cvmx_dprintf("Warning: Enabling FPA when FPA already enabled.\n"); 149 } 150 151 status.u64 = 0; 152 status.s.enb = 1; 153 cvmx_write_csr(CVMX_FPA_CTL_STATUS, status.u64); 154} 155 156/** 157 * Reset FPA to disable. Make sure buffers from all FPA pools are freed 158 * before disabling FPA. 159 */ 160static inline void cvmx_fpa_disable(void) 161{ 162 cvmx_fpa_ctl_status_t status; 163 164 status.u64 = cvmx_read_csr(CVMX_FPA_CTL_STATUS); 165 status.s.reset = 1; 166 cvmx_write_csr(CVMX_FPA_CTL_STATUS, status.u64); 167} 168 169/** 170 * Get a new block from the FPA 171 * 172 * @param pool Pool to get the block from 173 * @return Pointer to the block or NULL on failure 174 */ 175static inline void *cvmx_fpa_alloc(uint64_t pool) 176{ 177 uint64_t address; 178 179 for (;;) { 180 address = cvmx_read_csr(CVMX_ADDR_DID(CVMX_FULL_DID(CVMX_OCT_DID_FPA,pool))); 181 if (cvmx_likely(address)) { 182 return cvmx_phys_to_ptr(address); 183 } else { 184 /* If pointers are available, continuously retry. */ 185 if (cvmx_read_csr(CVMX_FPA_QUEX_AVAILABLE(pool)) > 0) 186 cvmx_wait(50); 187 else 188 return NULL; 189 } 190 } 191} 192 193/** 194 * Asynchronously get a new block from the FPA 195 * 196 * The result of cvmx_fpa_async_alloc() may be retrieved using 197 * cvmx_fpa_async_alloc_finish(). 198 * 199 * @param scr_addr Local scratch address to put response in. This is a byte address, 200 * but must be 8 byte aligned. 201 * @param pool Pool to get the block from 202 */ 203static inline void cvmx_fpa_async_alloc(uint64_t scr_addr, uint64_t pool) 204{ 205 cvmx_fpa_iobdma_data_t data; 206 207 /* Hardware only uses 64 bit alligned locations, so convert from byte address 208 ** to 64-bit index 209 */ 210 data.s.scraddr = scr_addr >> 3; 211 data.s.len = 1; 212 data.s.did = CVMX_FULL_DID(CVMX_OCT_DID_FPA,pool); 213 data.s.addr = 0; 214 cvmx_send_single(data.u64); 215} 216 217/** 218 * Retrieve the result of cvmx_fpa_async_alloc 219 * 220 * @param scr_addr The Local scratch address. Must be the same value 221 * passed to cvmx_fpa_async_alloc(). 222 * 223 * @param pool Pool the block came from. Must be the same value 224 * passed to cvmx_fpa_async_alloc. 225 * 226 * @return Pointer to the block or NULL on failure 227 */ 228static inline void *cvmx_fpa_async_alloc_finish(uint64_t scr_addr, uint64_t pool) 229{ 230 uint64_t address; 231 232 CVMX_SYNCIOBDMA; 233 234 address = cvmx_scratch_read64(scr_addr); 235 if (cvmx_likely(address)) 236 return cvmx_phys_to_ptr(address); 237 else 238 return cvmx_fpa_alloc(pool); 239} 240 241/** 242 * Free a block allocated with a FPA pool. 243 * Does NOT provide memory ordering in cases where the memory block was modified by the core. 244 * 245 * @param ptr Block to free 246 * @param pool Pool to put it in 247 * @param num_cache_lines 248 * Cache lines to invalidate 249 */ 250static inline void cvmx_fpa_free_nosync(void *ptr, uint64_t pool, uint64_t num_cache_lines) 251{ 252 cvmx_addr_t newptr; 253 newptr.u64 = cvmx_ptr_to_phys(ptr); 254 newptr.sfilldidspace.didspace = CVMX_ADDR_DIDSPACE(CVMX_FULL_DID(CVMX_OCT_DID_FPA,pool)); 255 asm volatile ("" : : : "memory"); /* Prevent GCC from reordering around free */ 256 /* value written is number of cache lines not written back */ 257 cvmx_write_io(newptr.u64, num_cache_lines); 258} 259 260/** 261 * Free a block allocated with a FPA pool. Provides required memory 262 * ordering in cases where memory block was modified by core. 263 * 264 * @param ptr Block to free 265 * @param pool Pool to put it in 266 * @param num_cache_lines 267 * Cache lines to invalidate 268 */ 269static inline void cvmx_fpa_free(void *ptr, uint64_t pool, uint64_t num_cache_lines) 270{ 271 cvmx_addr_t newptr; 272 newptr.u64 = cvmx_ptr_to_phys(ptr); 273 newptr.sfilldidspace.didspace = CVMX_ADDR_DIDSPACE(CVMX_FULL_DID(CVMX_OCT_DID_FPA,pool)); 274 /* Make sure that any previous writes to memory go out before we free this buffer. 275 ** This also serves as a barrier to prevent GCC from reordering operations to after 276 ** the free. */ 277 CVMX_SYNCWS; 278 /* value written is number of cache lines not written back */ 279 cvmx_write_io(newptr.u64, num_cache_lines); 280} 281 282/** 283 * Setup a FPA pool to control a new block of memory. 284 * This can only be called once per pool. Make sure proper 285 * locking enforces this. 286 * 287 * @param pool Pool to initialize 288 * 0 <= pool < 8 289 * @param name Constant character string to name this pool. 290 * String is not copied. 291 * @param buffer Pointer to the block of memory to use. This must be 292 * accessable by all processors and external hardware. 293 * @param block_size Size for each block controlled by the FPA 294 * @param num_blocks Number of blocks 295 * 296 * @return 0 on Success, 297 * -1 on failure 298 */ 299extern int cvmx_fpa_setup_pool(uint64_t pool, const char *name, void *buffer, 300 uint64_t block_size, uint64_t num_blocks); 301 302/** 303 * Shutdown a Memory pool and validate that it had all of 304 * the buffers originally placed in it. This should only be 305 * called by one processor after all hardware has finished 306 * using the pool. Most like you will want to have called 307 * cvmx_helper_shutdown_packet_io_global() before this 308 * function to make sure all FPA buffers are out of the packet 309 * IO hardware. 310 * 311 * @param pool Pool to shutdown 312 * 313 * @return Zero on success 314 * - Positive is count of missing buffers 315 * - Negative is too many buffers or corrupted pointers 316 */ 317extern uint64_t cvmx_fpa_shutdown_pool(uint64_t pool); 318 319/** 320 * Get the size of blocks controlled by the pool 321 * This is resolved to a constant at compile time. 322 * 323 * @param pool Pool to access 324 * @return Size of the block in bytes 325 */ 326uint64_t cvmx_fpa_get_block_size(uint64_t pool); 327 328#ifdef __cplusplus 329} 330#endif 331 332#endif /* __CVM_FPA_H__ */ 333