1210284Sjmallett/***********************license start*************** 2232812Sjmallett * Copyright (c) 2003-2010 Cavium Inc. (support@cavium.com). All rights 3215990Sjmallett * reserved. 4210284Sjmallett * 5210284Sjmallett * 6215990Sjmallett * Redistribution and use in source and binary forms, with or without 7215990Sjmallett * modification, are permitted provided that the following conditions are 8215990Sjmallett * met: 9210284Sjmallett * 10215990Sjmallett * * Redistributions of source code must retain the above copyright 11215990Sjmallett * notice, this list of conditions and the following disclaimer. 12210284Sjmallett * 13215990Sjmallett * * Redistributions in binary form must reproduce the above 14215990Sjmallett * copyright notice, this list of conditions and the following 15215990Sjmallett * disclaimer in the documentation and/or other materials provided 16215990Sjmallett * with the distribution. 17210284Sjmallett 18232812Sjmallett * * Neither the name of Cavium Inc. nor the names of 19215990Sjmallett * its contributors may be used to endorse or promote products 20215990Sjmallett * derived from this software without specific prior written 21215990Sjmallett * permission. 22210284Sjmallett 23215990Sjmallett * This Software, including technical data, may be subject to U.S. export control 24215990Sjmallett * laws, including the U.S. Export Administration Act and its associated 25215990Sjmallett * regulations, and may be subject to export or import regulations in other 26215990Sjmallett * countries. 27210284Sjmallett 28215990Sjmallett * TO THE MAXIMUM EXTENT PERMITTED BY LAW, THE SOFTWARE IS PROVIDED "AS IS" 29232812Sjmallett * AND WITH ALL FAULTS AND CAVIUM INC. MAKES NO PROMISES, REPRESENTATIONS OR 30215990Sjmallett * WARRANTIES, EITHER EXPRESS, IMPLIED, STATUTORY, OR OTHERWISE, WITH RESPECT TO 31215990Sjmallett * THE SOFTWARE, INCLUDING ITS CONDITION, ITS CONFORMITY TO ANY REPRESENTATION OR 32215990Sjmallett * DESCRIPTION, OR THE EXISTENCE OF ANY LATENT OR PATENT DEFECTS, AND CAVIUM 33215990Sjmallett * SPECIFICALLY DISCLAIMS ALL IMPLIED (IF ANY) WARRANTIES OF TITLE, 34215990Sjmallett * MERCHANTABILITY, NONINFRINGEMENT, FITNESS FOR A PARTICULAR PURPOSE, LACK OF 35215990Sjmallett * VIRUSES, ACCURACY OR COMPLETENESS, QUIET ENJOYMENT, QUIET POSSESSION OR 36215990Sjmallett * CORRESPONDENCE TO DESCRIPTION. THE ENTIRE RISK ARISING OUT OF USE OR 37215990Sjmallett * PERFORMANCE OF THE SOFTWARE LIES WITH YOU. 38215990Sjmallett ***********************license end**************************************/ 39210284Sjmallett 40210284Sjmallett 41210284Sjmallett/** 42210284Sjmallett * @file 43210284Sjmallett * 44210284Sjmallett * Interface to the hardware Free Pool Allocator. 45210284Sjmallett * 46232812Sjmallett * <hr>$Revision: 70030 $<hr> 47210284Sjmallett * 48210284Sjmallett */ 49210284Sjmallett 50210284Sjmallett#ifndef __CVMX_FPA_H__ 51210284Sjmallett#define __CVMX_FPA_H__ 52210284Sjmallett 53215990Sjmallett#include "cvmx-scratch.h" 54215990Sjmallett 55215990Sjmallett#ifdef CVMX_BUILD_FOR_LINUX_KERNEL 56215990Sjmallett#include "cvmx-fpa-defs.h" 57215990Sjmallett#endif 58215990Sjmallett 59210284Sjmallett#ifdef __cplusplus 60210284Sjmallettextern "C" { 61210284Sjmallett#endif 62210284Sjmallett 63210284Sjmallett#define CVMX_FPA_NUM_POOLS 8 64210284Sjmallett#define CVMX_FPA_MIN_BLOCK_SIZE 128 65210284Sjmallett#define CVMX_FPA_ALIGNMENT 128 66210284Sjmallett 67210284Sjmallett/** 68210284Sjmallett * Structure describing the data format used for stores to the FPA. 69210284Sjmallett */ 70210284Sjmalletttypedef union 71210284Sjmallett{ 72210284Sjmallett uint64_t u64; 73210284Sjmallett struct { 74210284Sjmallett uint64_t scraddr : 8; /**< the (64-bit word) location in scratchpad to write to (if len != 0) */ 75210284Sjmallett uint64_t len : 8; /**< the number of words in the response (0 => no response) */ 76210284Sjmallett uint64_t did : 8; /**< the ID of the device on the non-coherent bus */ 77210284Sjmallett uint64_t addr :40; /**< the address that will appear in the first tick on the NCB bus */ 78210284Sjmallett } s; 79210284Sjmallett} cvmx_fpa_iobdma_data_t; 80210284Sjmallett 81210284Sjmallett/** 82210284Sjmallett * Structure describing the current state of a FPA pool. 83210284Sjmallett */ 84210284Sjmalletttypedef struct 85210284Sjmallett{ 86210284Sjmallett const char *name; /**< Name it was created under */ 87210284Sjmallett uint64_t size; /**< Size of each block */ 88210284Sjmallett void * base; /**< The base memory address of whole block */ 89210284Sjmallett uint64_t starting_element_count; /**< The number of elements in the pool at creation */ 90210284Sjmallett} cvmx_fpa_pool_info_t; 91210284Sjmallett 92210284Sjmallett/** 93210284Sjmallett * Current state of all the pools. Use access functions 94210284Sjmallett * instead of using it directly. 95210284Sjmallett */ 96210284Sjmallettextern cvmx_fpa_pool_info_t cvmx_fpa_pool_info[CVMX_FPA_NUM_POOLS]; 97210284Sjmallett 98215990Sjmallett/* CSR typedefs have been moved to cvmx-fpa-defs.h */ 99210284Sjmallett 100210284Sjmallett/** 101210284Sjmallett * Return the name of the pool 102210284Sjmallett * 103210284Sjmallett * @param pool Pool to get the name of 104210284Sjmallett * @return The name 105210284Sjmallett */ 106210284Sjmallettstatic inline const char *cvmx_fpa_get_name(uint64_t pool) 107210284Sjmallett{ 108210284Sjmallett return cvmx_fpa_pool_info[pool].name; 109210284Sjmallett} 110210284Sjmallett 111210284Sjmallett/** 112210284Sjmallett * Return the base of the pool 113210284Sjmallett * 114210284Sjmallett * @param pool Pool to get the base of 115210284Sjmallett * @return The base 116210284Sjmallett */ 117210284Sjmallettstatic inline void *cvmx_fpa_get_base(uint64_t pool) 118210284Sjmallett{ 119210284Sjmallett return cvmx_fpa_pool_info[pool].base; 120210284Sjmallett} 121210284Sjmallett 122210284Sjmallett/** 123210284Sjmallett * Check if a pointer belongs to an FPA pool. Return non-zero 124210284Sjmallett * if the supplied pointer is inside the memory controlled by 125210284Sjmallett * an FPA pool. 126210284Sjmallett * 127210284Sjmallett * @param pool Pool to check 128210284Sjmallett * @param ptr Pointer to check 129210284Sjmallett * @return Non-zero if pointer is in the pool. Zero if not 130210284Sjmallett */ 131210284Sjmallettstatic inline int cvmx_fpa_is_member(uint64_t pool, void *ptr) 132210284Sjmallett{ 133210284Sjmallett return ((ptr >= cvmx_fpa_pool_info[pool].base) && 134210284Sjmallett ((char*)ptr < ((char*)(cvmx_fpa_pool_info[pool].base)) + cvmx_fpa_pool_info[pool].size * cvmx_fpa_pool_info[pool].starting_element_count)); 135210284Sjmallett} 136210284Sjmallett 137210284Sjmallett/** 138210284Sjmallett * Enable the FPA for use. Must be performed after any CSR 139210284Sjmallett * configuration but before any other FPA functions. 140210284Sjmallett */ 141210284Sjmallettstatic inline void cvmx_fpa_enable(void) 142210284Sjmallett{ 143210284Sjmallett cvmx_fpa_ctl_status_t status; 144210284Sjmallett 145210284Sjmallett status.u64 = cvmx_read_csr(CVMX_FPA_CTL_STATUS); 146210284Sjmallett if (status.s.enb) 147210284Sjmallett { 148232812Sjmallett /* 149232812Sjmallett * CN68XXP1 should not reset the FPA (doing so may break the 150232812Sjmallett * SSO, so we may end up enabling it more than once. Just 151232812Sjmallett * return and don't spew messages. 152232812Sjmallett */ 153232812Sjmallett return; 154210284Sjmallett } 155210284Sjmallett 156215990Sjmallett status.u64 = 0; 157210284Sjmallett status.s.enb = 1; 158210284Sjmallett cvmx_write_csr(CVMX_FPA_CTL_STATUS, status.u64); 159210284Sjmallett} 160210284Sjmallett 161210284Sjmallett/** 162232812Sjmallett * Reset FPA to disable. Make sure buffers from all FPA pools are freed 163219694Sjmallett * before disabling FPA. 164219694Sjmallett */ 165219694Sjmallettstatic inline void cvmx_fpa_disable(void) 166219694Sjmallett{ 167219694Sjmallett cvmx_fpa_ctl_status_t status; 168232812Sjmallett 169219694Sjmallett status.u64 = cvmx_read_csr(CVMX_FPA_CTL_STATUS); 170219694Sjmallett status.s.reset = 1; 171219694Sjmallett cvmx_write_csr(CVMX_FPA_CTL_STATUS, status.u64); 172219694Sjmallett} 173219694Sjmallett 174219694Sjmallett/** 175210284Sjmallett * Get a new block from the FPA 176210284Sjmallett * 177210284Sjmallett * @param pool Pool to get the block from 178210284Sjmallett * @return Pointer to the block or NULL on failure 179210284Sjmallett */ 180210284Sjmallettstatic inline void *cvmx_fpa_alloc(uint64_t pool) 181210284Sjmallett{ 182215990Sjmallett uint64_t address; 183215990Sjmallett 184215990Sjmallett for (;;) { 185215990Sjmallett address = cvmx_read_csr(CVMX_ADDR_DID(CVMX_FULL_DID(CVMX_OCT_DID_FPA,pool))); 186215990Sjmallett if (cvmx_likely(address)) { 187215990Sjmallett return cvmx_phys_to_ptr(address); 188215990Sjmallett } else { 189215990Sjmallett /* If pointers are available, continuously retry. */ 190215990Sjmallett if (cvmx_read_csr(CVMX_FPA_QUEX_AVAILABLE(pool)) > 0) 191215990Sjmallett cvmx_wait(50); 192215990Sjmallett else 193215990Sjmallett return NULL; 194215990Sjmallett } 195215990Sjmallett } 196210284Sjmallett} 197210284Sjmallett 198210284Sjmallett/** 199210284Sjmallett * Asynchronously get a new block from the FPA 200210284Sjmallett * 201215990Sjmallett * The result of cvmx_fpa_async_alloc() may be retrieved using 202215990Sjmallett * cvmx_fpa_async_alloc_finish(). 203215990Sjmallett * 204210284Sjmallett * @param scr_addr Local scratch address to put response in. This is a byte address, 205210284Sjmallett * but must be 8 byte aligned. 206210284Sjmallett * @param pool Pool to get the block from 207210284Sjmallett */ 208210284Sjmallettstatic inline void cvmx_fpa_async_alloc(uint64_t scr_addr, uint64_t pool) 209210284Sjmallett{ 210210284Sjmallett cvmx_fpa_iobdma_data_t data; 211210284Sjmallett 212232812Sjmallett /* Hardware only uses 64 bit aligned locations, so convert from byte address 213210284Sjmallett ** to 64-bit index 214210284Sjmallett */ 215210284Sjmallett data.s.scraddr = scr_addr >> 3; 216210284Sjmallett data.s.len = 1; 217210284Sjmallett data.s.did = CVMX_FULL_DID(CVMX_OCT_DID_FPA,pool); 218210284Sjmallett data.s.addr = 0; 219210284Sjmallett cvmx_send_single(data.u64); 220210284Sjmallett} 221210284Sjmallett 222215990Sjmallett/** 223215990Sjmallett * Retrieve the result of cvmx_fpa_async_alloc 224215990Sjmallett * 225215990Sjmallett * @param scr_addr The Local scratch address. Must be the same value 226215990Sjmallett * passed to cvmx_fpa_async_alloc(). 227215990Sjmallett * 228215990Sjmallett * @param pool Pool the block came from. Must be the same value 229215990Sjmallett * passed to cvmx_fpa_async_alloc. 230215990Sjmallett * 231215990Sjmallett * @return Pointer to the block or NULL on failure 232215990Sjmallett */ 233215990Sjmallettstatic inline void *cvmx_fpa_async_alloc_finish(uint64_t scr_addr, uint64_t pool) 234215990Sjmallett{ 235215990Sjmallett uint64_t address; 236210284Sjmallett 237215990Sjmallett CVMX_SYNCIOBDMA; 238215990Sjmallett 239215990Sjmallett address = cvmx_scratch_read64(scr_addr); 240215990Sjmallett if (cvmx_likely(address)) 241215990Sjmallett return cvmx_phys_to_ptr(address); 242215990Sjmallett else 243215990Sjmallett return cvmx_fpa_alloc(pool); 244215990Sjmallett} 245215990Sjmallett 246210284Sjmallett/** 247210284Sjmallett * Free a block allocated with a FPA pool. 248210284Sjmallett * Does NOT provide memory ordering in cases where the memory block was modified by the core. 249210284Sjmallett * 250210284Sjmallett * @param ptr Block to free 251210284Sjmallett * @param pool Pool to put it in 252210284Sjmallett * @param num_cache_lines 253210284Sjmallett * Cache lines to invalidate 254210284Sjmallett */ 255210284Sjmallettstatic inline void cvmx_fpa_free_nosync(void *ptr, uint64_t pool, uint64_t num_cache_lines) 256210284Sjmallett{ 257210284Sjmallett cvmx_addr_t newptr; 258210284Sjmallett newptr.u64 = cvmx_ptr_to_phys(ptr); 259210284Sjmallett newptr.sfilldidspace.didspace = CVMX_ADDR_DIDSPACE(CVMX_FULL_DID(CVMX_OCT_DID_FPA,pool)); 260210284Sjmallett asm volatile ("" : : : "memory"); /* Prevent GCC from reordering around free */ 261210284Sjmallett /* value written is number of cache lines not written back */ 262210284Sjmallett cvmx_write_io(newptr.u64, num_cache_lines); 263210284Sjmallett} 264210284Sjmallett 265210284Sjmallett/** 266210284Sjmallett * Free a block allocated with a FPA pool. Provides required memory 267210284Sjmallett * ordering in cases where memory block was modified by core. 268210284Sjmallett * 269210284Sjmallett * @param ptr Block to free 270210284Sjmallett * @param pool Pool to put it in 271210284Sjmallett * @param num_cache_lines 272210284Sjmallett * Cache lines to invalidate 273210284Sjmallett */ 274210284Sjmallettstatic inline void cvmx_fpa_free(void *ptr, uint64_t pool, uint64_t num_cache_lines) 275210284Sjmallett{ 276210284Sjmallett cvmx_addr_t newptr; 277210284Sjmallett newptr.u64 = cvmx_ptr_to_phys(ptr); 278210284Sjmallett newptr.sfilldidspace.didspace = CVMX_ADDR_DIDSPACE(CVMX_FULL_DID(CVMX_OCT_DID_FPA,pool)); 279210284Sjmallett /* Make sure that any previous writes to memory go out before we free this buffer. 280210284Sjmallett ** This also serves as a barrier to prevent GCC from reordering operations to after 281210284Sjmallett ** the free. */ 282210284Sjmallett CVMX_SYNCWS; 283210284Sjmallett /* value written is number of cache lines not written back */ 284210284Sjmallett cvmx_write_io(newptr.u64, num_cache_lines); 285210284Sjmallett} 286210284Sjmallett 287210284Sjmallett/** 288210284Sjmallett * Setup a FPA pool to control a new block of memory. 289210284Sjmallett * This can only be called once per pool. Make sure proper 290210284Sjmallett * locking enforces this. 291210284Sjmallett * 292210284Sjmallett * @param pool Pool to initialize 293210284Sjmallett * 0 <= pool < 8 294210284Sjmallett * @param name Constant character string to name this pool. 295210284Sjmallett * String is not copied. 296210284Sjmallett * @param buffer Pointer to the block of memory to use. This must be 297210284Sjmallett * accessable by all processors and external hardware. 298210284Sjmallett * @param block_size Size for each block controlled by the FPA 299210284Sjmallett * @param num_blocks Number of blocks 300210284Sjmallett * 301210284Sjmallett * @return 0 on Success, 302210284Sjmallett * -1 on failure 303210284Sjmallett */ 304210284Sjmallettextern int cvmx_fpa_setup_pool(uint64_t pool, const char *name, void *buffer, 305210284Sjmallett uint64_t block_size, uint64_t num_blocks); 306210284Sjmallett 307210284Sjmallett/** 308210284Sjmallett * Shutdown a Memory pool and validate that it had all of 309210284Sjmallett * the buffers originally placed in it. This should only be 310210284Sjmallett * called by one processor after all hardware has finished 311215990Sjmallett * using the pool. Most like you will want to have called 312215990Sjmallett * cvmx_helper_shutdown_packet_io_global() before this 313215990Sjmallett * function to make sure all FPA buffers are out of the packet 314215990Sjmallett * IO hardware. 315210284Sjmallett * 316210284Sjmallett * @param pool Pool to shutdown 317215990Sjmallett * 318210284Sjmallett * @return Zero on success 319210284Sjmallett * - Positive is count of missing buffers 320210284Sjmallett * - Negative is too many buffers or corrupted pointers 321210284Sjmallett */ 322210284Sjmallettextern uint64_t cvmx_fpa_shutdown_pool(uint64_t pool); 323210284Sjmallett 324210284Sjmallett/** 325210284Sjmallett * Get the size of blocks controlled by the pool 326210284Sjmallett * This is resolved to a constant at compile time. 327210284Sjmallett * 328210284Sjmallett * @param pool Pool to access 329210284Sjmallett * @return Size of the block in bytes 330210284Sjmallett */ 331210284Sjmallettuint64_t cvmx_fpa_get_block_size(uint64_t pool); 332210284Sjmallett 333210284Sjmallett#ifdef __cplusplus 334210284Sjmallett} 335210284Sjmallett#endif 336210284Sjmallett 337215990Sjmallett#endif /* __CVM_FPA_H__ */ 338