1 /***********************license start***************
2 * Copyright (c) 2003-2010 Cavium Networks (support@cavium.com). All rights
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions are
10 * * Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
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.
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
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
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**************************************/
44 * Interface to the hardware Free Pool Allocator.
46 * <hr>$Revision: 50048 $<hr>
50 #ifndef __CVMX_FPA_H__
51 #define __CVMX_FPA_H__
53 #include "cvmx-scratch.h"
55 #ifdef CVMX_BUILD_FOR_LINUX_KERNEL
56 #include "cvmx-fpa-defs.h"
63 #define CVMX_FPA_NUM_POOLS 8
64 #define CVMX_FPA_MIN_BLOCK_SIZE 128
65 #define CVMX_FPA_ALIGNMENT 128
68 * Structure describing the data format used for stores to the FPA.
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 */
79 } cvmx_fpa_iobdma_data_t;
82 * Structure describing the current state of a FPA pool.
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;
93 * Current state of all the pools. Use access functions
94 * instead of using it directly.
96 extern cvmx_fpa_pool_info_t cvmx_fpa_pool_info[CVMX_FPA_NUM_POOLS];
98 /* CSR typedefs have been moved to cvmx-fpa-defs.h */
101 * Return the name of the pool
103 * @param pool Pool to get the name of
106 static inline const char *cvmx_fpa_get_name(uint64_t pool)
108 return cvmx_fpa_pool_info[pool].name;
112 * Return the base of the pool
114 * @param pool Pool to get the base of
117 static inline void *cvmx_fpa_get_base(uint64_t pool)
119 return cvmx_fpa_pool_info[pool].base;
123 * Check if a pointer belongs to an FPA pool. Return non-zero
124 * if the supplied pointer is inside the memory controlled by
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
131 static inline int cvmx_fpa_is_member(uint64_t pool, void *ptr)
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));
138 * Enable the FPA for use. Must be performed after any CSR
139 * configuration but before any other FPA functions.
141 static inline void cvmx_fpa_enable(void)
143 cvmx_fpa_ctl_status_t status;
145 status.u64 = cvmx_read_csr(CVMX_FPA_CTL_STATUS);
148 cvmx_dprintf("Warning: Enabling FPA when FPA already enabled.\n");
153 cvmx_write_csr(CVMX_FPA_CTL_STATUS, status.u64);
157 * Reset FPA to disable. Make sure buffers from all FPA pools are freed
158 * before disabling FPA.
160 static inline void cvmx_fpa_disable(void)
162 cvmx_fpa_ctl_status_t status;
164 status.u64 = cvmx_read_csr(CVMX_FPA_CTL_STATUS);
166 cvmx_write_csr(CVMX_FPA_CTL_STATUS, status.u64);
170 * Get a new block from the FPA
172 * @param pool Pool to get the block from
173 * @return Pointer to the block or NULL on failure
175 static inline void *cvmx_fpa_alloc(uint64_t pool)
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);
184 /* If pointers are available, continuously retry. */
185 if (cvmx_read_csr(CVMX_FPA_QUEX_AVAILABLE(pool)) > 0)
194 * Asynchronously get a new block from the FPA
196 * The result of cvmx_fpa_async_alloc() may be retrieved using
197 * cvmx_fpa_async_alloc_finish().
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
203 static inline void cvmx_fpa_async_alloc(uint64_t scr_addr, uint64_t pool)
205 cvmx_fpa_iobdma_data_t data;
207 /* Hardware only uses 64 bit alligned locations, so convert from byte address
210 data.s.scraddr = scr_addr >> 3;
212 data.s.did = CVMX_FULL_DID(CVMX_OCT_DID_FPA,pool);
214 cvmx_send_single(data.u64);
218 * Retrieve the result of cvmx_fpa_async_alloc
220 * @param scr_addr The Local scratch address. Must be the same value
221 * passed to cvmx_fpa_async_alloc().
223 * @param pool Pool the block came from. Must be the same value
224 * passed to cvmx_fpa_async_alloc.
226 * @return Pointer to the block or NULL on failure
228 static inline void *cvmx_fpa_async_alloc_finish(uint64_t scr_addr, uint64_t pool)
234 address = cvmx_scratch_read64(scr_addr);
235 if (cvmx_likely(address))
236 return cvmx_phys_to_ptr(address);
238 return cvmx_fpa_alloc(pool);
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.
245 * @param ptr Block to free
246 * @param pool Pool to put it in
247 * @param num_cache_lines
248 * Cache lines to invalidate
250 static inline void cvmx_fpa_free_nosync(void *ptr, uint64_t pool, uint64_t num_cache_lines)
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);
261 * Free a block allocated with a FPA pool. Provides required memory
262 * ordering in cases where memory block was modified by core.
264 * @param ptr Block to free
265 * @param pool Pool to put it in
266 * @param num_cache_lines
267 * Cache lines to invalidate
269 static inline void cvmx_fpa_free(void *ptr, uint64_t pool, uint64_t num_cache_lines)
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
278 /* value written is number of cache lines not written back */
279 cvmx_write_io(newptr.u64, num_cache_lines);
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.
287 * @param pool Pool to initialize
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
296 * @return 0 on Success,
299 extern int cvmx_fpa_setup_pool(uint64_t pool, const char *name, void *buffer,
300 uint64_t block_size, uint64_t num_blocks);
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
311 * @param pool Pool to shutdown
313 * @return Zero on success
314 * - Positive is count of missing buffers
315 * - Negative is too many buffers or corrupted pointers
317 extern uint64_t cvmx_fpa_shutdown_pool(uint64_t pool);
320 * Get the size of blocks controlled by the pool
321 * This is resolved to a constant at compile time.
323 * @param pool Pool to access
324 * @return Size of the block in bytes
326 uint64_t cvmx_fpa_get_block_size(uint64_t pool);
332 #endif /* __CVM_FPA_H__ */