1 /***********************license start***************
2 * Copyright (c) 2003-2010 Cavium Inc. (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 Inc. 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 INC. 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: 70030 $<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);
149 * CN68XXP1 should not reset the FPA (doing so may break the
150 * SSO, so we may end up enabling it more than once. Just
151 * return and don't spew messages.
158 cvmx_write_csr(CVMX_FPA_CTL_STATUS, status.u64);
162 * Reset FPA to disable. Make sure buffers from all FPA pools are freed
163 * before disabling FPA.
165 static inline void cvmx_fpa_disable(void)
167 cvmx_fpa_ctl_status_t status;
169 status.u64 = cvmx_read_csr(CVMX_FPA_CTL_STATUS);
171 cvmx_write_csr(CVMX_FPA_CTL_STATUS, status.u64);
175 * Get a new block from the FPA
177 * @param pool Pool to get the block from
178 * @return Pointer to the block or NULL on failure
180 static inline void *cvmx_fpa_alloc(uint64_t pool)
185 address = cvmx_read_csr(CVMX_ADDR_DID(CVMX_FULL_DID(CVMX_OCT_DID_FPA,pool)));
186 if (cvmx_likely(address)) {
187 return cvmx_phys_to_ptr(address);
189 /* If pointers are available, continuously retry. */
190 if (cvmx_read_csr(CVMX_FPA_QUEX_AVAILABLE(pool)) > 0)
199 * Asynchronously get a new block from the FPA
201 * The result of cvmx_fpa_async_alloc() may be retrieved using
202 * cvmx_fpa_async_alloc_finish().
204 * @param scr_addr Local scratch address to put response in. This is a byte address,
205 * but must be 8 byte aligned.
206 * @param pool Pool to get the block from
208 static inline void cvmx_fpa_async_alloc(uint64_t scr_addr, uint64_t pool)
210 cvmx_fpa_iobdma_data_t data;
212 /* Hardware only uses 64 bit aligned locations, so convert from byte address
215 data.s.scraddr = scr_addr >> 3;
217 data.s.did = CVMX_FULL_DID(CVMX_OCT_DID_FPA,pool);
219 cvmx_send_single(data.u64);
223 * Retrieve the result of cvmx_fpa_async_alloc
225 * @param scr_addr The Local scratch address. Must be the same value
226 * passed to cvmx_fpa_async_alloc().
228 * @param pool Pool the block came from. Must be the same value
229 * passed to cvmx_fpa_async_alloc.
231 * @return Pointer to the block or NULL on failure
233 static inline void *cvmx_fpa_async_alloc_finish(uint64_t scr_addr, uint64_t pool)
239 address = cvmx_scratch_read64(scr_addr);
240 if (cvmx_likely(address))
241 return cvmx_phys_to_ptr(address);
243 return cvmx_fpa_alloc(pool);
247 * Free a block allocated with a FPA pool.
248 * Does NOT provide memory ordering in cases where the memory block was modified by the core.
250 * @param ptr Block to free
251 * @param pool Pool to put it in
252 * @param num_cache_lines
253 * Cache lines to invalidate
255 static inline void cvmx_fpa_free_nosync(void *ptr, uint64_t pool, uint64_t num_cache_lines)
258 newptr.u64 = cvmx_ptr_to_phys(ptr);
259 newptr.sfilldidspace.didspace = CVMX_ADDR_DIDSPACE(CVMX_FULL_DID(CVMX_OCT_DID_FPA,pool));
260 asm volatile ("" : : : "memory"); /* Prevent GCC from reordering around free */
261 /* value written is number of cache lines not written back */
262 cvmx_write_io(newptr.u64, num_cache_lines);
266 * Free a block allocated with a FPA pool. Provides required memory
267 * ordering in cases where memory block was modified by core.
269 * @param ptr Block to free
270 * @param pool Pool to put it in
271 * @param num_cache_lines
272 * Cache lines to invalidate
274 static inline void cvmx_fpa_free(void *ptr, uint64_t pool, uint64_t num_cache_lines)
277 newptr.u64 = cvmx_ptr_to_phys(ptr);
278 newptr.sfilldidspace.didspace = CVMX_ADDR_DIDSPACE(CVMX_FULL_DID(CVMX_OCT_DID_FPA,pool));
279 /* Make sure that any previous writes to memory go out before we free this buffer.
280 ** This also serves as a barrier to prevent GCC from reordering operations to after
283 /* value written is number of cache lines not written back */
284 cvmx_write_io(newptr.u64, num_cache_lines);
288 * Setup a FPA pool to control a new block of memory.
289 * This can only be called once per pool. Make sure proper
290 * locking enforces this.
292 * @param pool Pool to initialize
294 * @param name Constant character string to name this pool.
295 * String is not copied.
296 * @param buffer Pointer to the block of memory to use. This must be
297 * accessable by all processors and external hardware.
298 * @param block_size Size for each block controlled by the FPA
299 * @param num_blocks Number of blocks
301 * @return 0 on Success,
304 extern int cvmx_fpa_setup_pool(uint64_t pool, const char *name, void *buffer,
305 uint64_t block_size, uint64_t num_blocks);
308 * Shutdown a Memory pool and validate that it had all of
309 * the buffers originally placed in it. This should only be
310 * called by one processor after all hardware has finished
311 * using the pool. Most like you will want to have called
312 * cvmx_helper_shutdown_packet_io_global() before this
313 * function to make sure all FPA buffers are out of the packet
316 * @param pool Pool to shutdown
318 * @return Zero on success
319 * - Positive is count of missing buffers
320 * - Negative is too many buffers or corrupted pointers
322 extern uint64_t cvmx_fpa_shutdown_pool(uint64_t pool);
325 * Get the size of blocks controlled by the pool
326 * This is resolved to a constant at compile time.
328 * @param pool Pool to access
329 * @return Size of the block in bytes
331 uint64_t cvmx_fpa_get_block_size(uint64_t pool);
337 #endif /* __CVM_FPA_H__ */