1 /***********************license start***************
2 * Copyright (c) 2003-2008 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 * TO THE MAXIMUM EXTENT PERMITTED BY LAW, THE SOFTWARE IS PROVIDED "AS IS"
24 * AND WITH ALL FAULTS AND CAVIUM NETWORKS MAKES NO PROMISES, REPRESENTATIONS
25 * OR WARRANTIES, EITHER EXPRESS, IMPLIED, STATUTORY, OR OTHERWISE, WITH
26 * RESPECT TO THE SOFTWARE, INCLUDING ITS CONDITION, ITS CONFORMITY TO ANY
27 * REPRESENTATION OR DESCRIPTION, OR THE EXISTENCE OF ANY LATENT OR PATENT
28 * DEFECTS, AND CAVIUM SPECIFICALLY DISCLAIMS ALL IMPLIED (IF ANY) WARRANTIES
29 * OF TITLE, MERCHANTABILITY, NONINFRINGEMENT, FITNESS FOR A PARTICULAR
30 * PURPOSE, LACK OF VIRUSES, ACCURACY OR COMPLETENESS, QUIET ENJOYMENT, QUIET
31 * POSSESSION OR CORRESPONDENCE TO DESCRIPTION. THE ENTIRE RISK ARISING OUT
32 * OF USE OR PERFORMANCE OF THE SOFTWARE LIES WITH YOU.
35 * For any questions regarding licensing please contact marketing@caviumnetworks.com
37 ***********************license end**************************************/
47 * Interface to the hardware Free Pool Allocator.
49 * <hr>$Revision: 41586 $<hr>
53 #ifndef __CVMX_FPA_H__
54 #define __CVMX_FPA_H__
60 #define CVMX_FPA_NUM_POOLS 8
61 #define CVMX_FPA_MIN_BLOCK_SIZE 128
62 #define CVMX_FPA_ALIGNMENT 128
65 * Structure describing the data format used for stores to the FPA.
71 uint64_t scraddr : 8; /**< the (64-bit word) location in scratchpad to write to (if len != 0) */
72 uint64_t len : 8; /**< the number of words in the response (0 => no response) */
73 uint64_t did : 8; /**< the ID of the device on the non-coherent bus */
74 uint64_t addr :40; /**< the address that will appear in the first tick on the NCB bus */
76 } cvmx_fpa_iobdma_data_t;
79 * Structure describing the current state of a FPA pool.
83 const char *name; /**< Name it was created under */
84 uint64_t size; /**< Size of each block */
85 void * base; /**< The base memory address of whole block */
86 uint64_t starting_element_count; /**< The number of elements in the pool at creation */
87 } cvmx_fpa_pool_info_t;
90 * Current state of all the pools. Use access functions
91 * instead of using it directly.
93 extern cvmx_fpa_pool_info_t cvmx_fpa_pool_info[CVMX_FPA_NUM_POOLS];
95 /* CSR typedefs have been moved to cvmx-csr-*.h */
98 * Return the name of the pool
100 * @param pool Pool to get the name of
103 static inline const char *cvmx_fpa_get_name(uint64_t pool)
105 return cvmx_fpa_pool_info[pool].name;
109 * Return the base of the pool
111 * @param pool Pool to get the base of
114 static inline void *cvmx_fpa_get_base(uint64_t pool)
116 return cvmx_fpa_pool_info[pool].base;
120 * Check if a pointer belongs to an FPA pool. Return non-zero
121 * if the supplied pointer is inside the memory controlled by
124 * @param pool Pool to check
125 * @param ptr Pointer to check
126 * @return Non-zero if pointer is in the pool. Zero if not
128 static inline int cvmx_fpa_is_member(uint64_t pool, void *ptr)
130 return ((ptr >= cvmx_fpa_pool_info[pool].base) &&
131 ((char*)ptr < ((char*)(cvmx_fpa_pool_info[pool].base)) + cvmx_fpa_pool_info[pool].size * cvmx_fpa_pool_info[pool].starting_element_count));
137 * Enable the FPA for use. Must be performed after any CSR
138 * configuration but before any other FPA functions.
140 static inline void cvmx_fpa_enable(void)
142 cvmx_fpa_ctl_status_t status;
144 status.u64 = cvmx_read_csr(CVMX_FPA_CTL_STATUS);
147 cvmx_dprintf("Warning: Enabling FPA when FPA already enabled.\n");
150 /* Do runtime check as we allow pass1 compiled code to run on pass2 chips */
151 if (cvmx_octeon_is_pass1())
153 cvmx_fpa_fpf_marks_t marks;
157 marks.u64 = cvmx_read_csr(CVMX_FPA_FPF1_MARKS + (i-1)*8ull);
158 marks.s.fpf_wr = 0xe0;
159 cvmx_write_csr(CVMX_FPA_FPF1_MARKS + (i-1)*8ull, marks.u64);
162 /* Enforce a 10 cycle delay between config and enable */
166 status.u64 = 0; /* FIXME: CVMX_FPA_CTL_STATUS read is unmodelled */
168 cvmx_write_csr(CVMX_FPA_CTL_STATUS, status.u64);
173 * Get a new block from the FPA
175 * @param pool Pool to get the block from
176 * @return Pointer to the block or NULL on failure
178 static inline void *cvmx_fpa_alloc(uint64_t pool)
180 uint64_t address = cvmx_read_csr(CVMX_ADDR_DID(CVMX_FULL_DID(CVMX_OCT_DID_FPA,pool)));
182 return cvmx_phys_to_ptr(address);
189 * Asynchronously get a new block from the FPA
191 * @param scr_addr Local scratch address to put response in. This is a byte address,
192 * but must be 8 byte aligned.
193 * @param pool Pool to get the block from
195 static inline void cvmx_fpa_async_alloc(uint64_t scr_addr, uint64_t pool)
197 cvmx_fpa_iobdma_data_t data;
199 /* Hardware only uses 64 bit alligned locations, so convert from byte address
202 data.s.scraddr = scr_addr >> 3;
204 data.s.did = CVMX_FULL_DID(CVMX_OCT_DID_FPA,pool);
206 cvmx_send_single(data.u64);
211 * Free a block allocated with a FPA pool.
212 * Does NOT provide memory ordering in cases where the memory block was modified by the core.
214 * @param ptr Block to free
215 * @param pool Pool to put it in
216 * @param num_cache_lines
217 * Cache lines to invalidate
219 static inline void cvmx_fpa_free_nosync(void *ptr, uint64_t pool, uint64_t num_cache_lines)
222 newptr.u64 = cvmx_ptr_to_phys(ptr);
223 newptr.sfilldidspace.didspace = CVMX_ADDR_DIDSPACE(CVMX_FULL_DID(CVMX_OCT_DID_FPA,pool));
224 asm volatile ("" : : : "memory"); /* Prevent GCC from reordering around free */
225 /* value written is number of cache lines not written back */
226 cvmx_write_io(newptr.u64, num_cache_lines);
230 * Free a block allocated with a FPA pool. Provides required memory
231 * ordering in cases where memory block was modified by core.
233 * @param ptr Block to free
234 * @param pool Pool to put it in
235 * @param num_cache_lines
236 * Cache lines to invalidate
238 static inline void cvmx_fpa_free(void *ptr, uint64_t pool, uint64_t num_cache_lines)
241 newptr.u64 = cvmx_ptr_to_phys(ptr);
242 newptr.sfilldidspace.didspace = CVMX_ADDR_DIDSPACE(CVMX_FULL_DID(CVMX_OCT_DID_FPA,pool));
243 /* Make sure that any previous writes to memory go out before we free this buffer.
244 ** This also serves as a barrier to prevent GCC from reordering operations to after
247 /* value written is number of cache lines not written back */
248 cvmx_write_io(newptr.u64, num_cache_lines);
253 * Setup a FPA pool to control a new block of memory.
254 * This can only be called once per pool. Make sure proper
255 * locking enforces this.
257 * @param pool Pool to initialize
259 * @param name Constant character string to name this pool.
260 * String is not copied.
261 * @param buffer Pointer to the block of memory to use. This must be
262 * accessable by all processors and external hardware.
263 * @param block_size Size for each block controlled by the FPA
264 * @param num_blocks Number of blocks
266 * @return 0 on Success,
269 extern int cvmx_fpa_setup_pool(uint64_t pool, const char *name, void *buffer,
270 uint64_t block_size, uint64_t num_blocks);
274 * Shutdown a Memory pool and validate that it had all of
275 * the buffers originally placed in it. This should only be
276 * called by one processor after all hardware has finished
279 * @param pool Pool to shutdown
280 * @return Zero on success
281 * - Positive is count of missing buffers
282 * - Negative is too many buffers or corrupted pointers
284 extern uint64_t cvmx_fpa_shutdown_pool(uint64_t pool);
288 * Get the size of blocks controlled by the pool
289 * This is resolved to a constant at compile time.
291 * @param pool Pool to access
292 * @return Size of the block in bytes
294 uint64_t cvmx_fpa_get_block_size(uint64_t pool);
300 #endif // __CVM_FPA_H__