1 /***********************license start***************
2 * Author: Cavium Networks
4 * Contact: support@caviumnetworks.com
5 * This file is part of the OCTEON SDK
7 * Copyright (c) 2003-2008 Cavium Networks
9 * This file is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License, Version 2, as
11 * published by the Free Software Foundation.
13 * This file is distributed in the hope that it will be useful, but
14 * AS-IS and WITHOUT ANY WARRANTY; without even the implied warranty
15 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, TITLE, or
16 * NONINFRINGEMENT. See the GNU General Public License for more
19 * You should have received a copy of the GNU General Public License
20 * along with this file; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
22 * or visit http://www.gnu.org/licenses/.
24 * This file may also be available under a different license from Cavium.
25 * Contact Cavium Networks for more information
26 ***********************license end**************************************/
31 * Interface to the hardware Free Pool Allocator.
36 #ifndef __CVMX_FPA_H__
37 #define __CVMX_FPA_H__
39 #include "cvmx-address.h"
40 #include "cvmx-fpa-defs.h"
42 #define CVMX_FPA_NUM_POOLS 8
43 #define CVMX_FPA_MIN_BLOCK_SIZE 128
44 #define CVMX_FPA_ALIGNMENT 128
47 * Structure describing the data format used for stores to the FPA.
53 * the (64-bit word) location in scratchpad to write
57 /* the number of words in the response (0 => no response) */
59 /* the ID of the device on the non-coherent bus */
62 * the address that will appear in the first tick on
67 } cvmx_fpa_iobdma_data_t
;
70 * Structure describing the current state of a FPA pool.
73 /* Name it was created under */
75 /* Size of each block */
77 /* The base memory address of whole block */
79 /* The number of elements in the pool at creation */
80 uint64_t starting_element_count
;
81 } cvmx_fpa_pool_info_t
;
84 * Current state of all the pools. Use access functions
85 * instead of using it directly.
87 extern cvmx_fpa_pool_info_t cvmx_fpa_pool_info
[CVMX_FPA_NUM_POOLS
];
89 /* CSR typedefs have been moved to cvmx-csr-*.h */
92 * Return the name of the pool
94 * @pool: Pool to get the name of
97 static inline const char *cvmx_fpa_get_name(uint64_t pool
)
99 return cvmx_fpa_pool_info
[pool
].name
;
103 * Return the base of the pool
105 * @pool: Pool to get the base of
108 static inline void *cvmx_fpa_get_base(uint64_t pool
)
110 return cvmx_fpa_pool_info
[pool
].base
;
114 * Check if a pointer belongs to an FPA pool. Return non-zero
115 * if the supplied pointer is inside the memory controlled by
118 * @pool: Pool to check
119 * @ptr: Pointer to check
120 * Returns Non-zero if pointer is in the pool. Zero if not
122 static inline int cvmx_fpa_is_member(uint64_t pool
, void *ptr
)
124 return ((ptr
>= cvmx_fpa_pool_info
[pool
].base
) &&
126 ((char *)(cvmx_fpa_pool_info
[pool
].base
)) +
127 cvmx_fpa_pool_info
[pool
].size
*
128 cvmx_fpa_pool_info
[pool
].starting_element_count
));
132 * Enable the FPA for use. Must be performed after any CSR
133 * configuration but before any other FPA functions.
135 static inline void cvmx_fpa_enable(void)
137 union cvmx_fpa_ctl_status status
;
139 status
.u64
= cvmx_read_csr(CVMX_FPA_CTL_STATUS
);
142 ("Warning: Enabling FPA when FPA already enabled.\n");
146 * Do runtime check as we allow pass1 compiled code to run on
149 if (cvmx_octeon_is_pass1()) {
150 union cvmx_fpa_fpfx_marks marks
;
152 for (i
= 1; i
< 8; i
++) {
154 cvmx_read_csr(CVMX_FPA_FPF1_MARKS
+ (i
- 1) * 8ull);
155 marks
.s
.fpf_wr
= 0xe0;
156 cvmx_write_csr(CVMX_FPA_FPF1_MARKS
+ (i
- 1) * 8ull,
160 /* Enforce a 10 cycle delay between config and enable */
166 cvmx_write_csr(CVMX_FPA_CTL_STATUS
, status
.u64
);
170 * Get a new block from the FPA
172 * @pool: Pool to get the block from
173 * Returns Pointer to the block or NULL on failure
175 static inline void *cvmx_fpa_alloc(uint64_t pool
)
178 cvmx_read_csr(CVMX_ADDR_DID(CVMX_FULL_DID(CVMX_OCT_DID_FPA
, pool
)));
180 return cvmx_phys_to_ptr(address
);
186 * Asynchronously get a new block from the FPA
188 * @scr_addr: Local scratch address to put response in. This is a byte address,
189 * but must be 8 byte aligned.
190 * @pool: Pool to get the block from
192 static inline void cvmx_fpa_async_alloc(uint64_t scr_addr
, uint64_t pool
)
194 cvmx_fpa_iobdma_data_t data
;
197 * Hardware only uses 64 bit alligned locations, so convert
198 * from byte address to 64-bit index
200 data
.s
.scraddr
= scr_addr
>> 3;
202 data
.s
.did
= CVMX_FULL_DID(CVMX_OCT_DID_FPA
, pool
);
204 cvmx_send_single(data
.u64
);
208 * Free a block allocated with a FPA pool. Does NOT provide memory
209 * ordering in cases where the memory block was modified by the core.
211 * @ptr: Block to free
212 * @pool: Pool to put it in
214 * Cache lines to invalidate
216 static inline void cvmx_fpa_free_nosync(void *ptr
, uint64_t pool
,
217 uint64_t num_cache_lines
)
220 newptr
.u64
= cvmx_ptr_to_phys(ptr
);
221 newptr
.sfilldidspace
.didspace
=
222 CVMX_ADDR_DIDSPACE(CVMX_FULL_DID(CVMX_OCT_DID_FPA
, pool
));
223 /* 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 * @ptr: Block to free
234 * @pool: Pool to put it in
236 * Cache lines to invalidate
238 static inline void cvmx_fpa_free(void *ptr
, uint64_t pool
,
239 uint64_t num_cache_lines
)
242 newptr
.u64
= cvmx_ptr_to_phys(ptr
);
243 newptr
.sfilldidspace
.didspace
=
244 CVMX_ADDR_DIDSPACE(CVMX_FULL_DID(CVMX_OCT_DID_FPA
, pool
));
246 * Make sure that any previous writes to memory go out before
247 * we free this buffer. This also serves as a barrier to
248 * prevent GCC from reordering operations to after the
252 /* value written is number of cache lines not written back */
253 cvmx_write_io(newptr
.u64
, num_cache_lines
);
257 * Setup a FPA pool to control a new block of memory.
258 * This can only be called once per pool. Make sure proper
259 * locking enforces this.
261 * @pool: Pool to initialize
263 * @name: Constant character string to name this pool.
264 * String is not copied.
265 * @buffer: Pointer to the block of memory to use. This must be
266 * accessable by all processors and external hardware.
267 * @block_size: Size for each block controlled by the FPA
268 * @num_blocks: Number of blocks
270 * Returns 0 on Success,
273 extern int cvmx_fpa_setup_pool(uint64_t pool
, const char *name
, void *buffer
,
274 uint64_t block_size
, uint64_t num_blocks
);
277 * Shutdown a Memory pool and validate that it had all of
278 * the buffers originally placed in it. This should only be
279 * called by one processor after all hardware has finished
282 * @pool: Pool to shutdown
283 * Returns Zero on success
284 * - Positive is count of missing buffers
285 * - Negative is too many buffers or corrupted pointers
287 extern uint64_t cvmx_fpa_shutdown_pool(uint64_t pool
);
290 * Get the size of blocks controlled by the pool
291 * This is resolved to a constant at compile time.
293 * @pool: Pool to access
294 * Returns Size of the block in bytes
296 uint64_t cvmx_fpa_get_block_size(uint64_t pool
);
298 #endif /* __CVM_FPA_H__ */