1af866496SDavid Daney /***********************license start***************
2af866496SDavid Daney  * Author: Cavium Networks
3af866496SDavid Daney  *
4af866496SDavid Daney  * Contact: support@caviumnetworks.com
5af866496SDavid Daney  * This file is part of the OCTEON SDK
6af866496SDavid Daney  *
7af866496SDavid Daney  * Copyright (c) 2003-2008 Cavium Networks
8af866496SDavid Daney  *
9af866496SDavid Daney  * This file is free software; you can redistribute it and/or modify
10af866496SDavid Daney  * it under the terms of the GNU General Public License, Version 2, as
11af866496SDavid Daney  * published by the Free Software Foundation.
12af866496SDavid Daney  *
13af866496SDavid Daney  * This file is distributed in the hope that it will be useful, but
14af866496SDavid Daney  * AS-IS and WITHOUT ANY WARRANTY; without even the implied warranty
15af866496SDavid Daney  * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, TITLE, or
16af866496SDavid Daney  * NONINFRINGEMENT.  See the GNU General Public License for more
17af866496SDavid Daney  * details.
18af866496SDavid Daney  *
19af866496SDavid Daney  * You should have received a copy of the GNU General Public License
20af866496SDavid Daney  * along with this file; if not, write to the Free Software
21af866496SDavid Daney  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
22af866496SDavid Daney  * or visit http://www.gnu.org/licenses/.
23af866496SDavid Daney  *
24af866496SDavid Daney  * This file may also be available under a different license from Cavium.
25af866496SDavid Daney  * Contact Cavium Networks for more information
26af866496SDavid Daney  ***********************license end**************************************/
27af866496SDavid Daney 
28af866496SDavid Daney /**
29af866496SDavid Daney  * @file
30af866496SDavid Daney  *
31af866496SDavid Daney  * Interface to the hardware Free Pool Allocator.
32af866496SDavid Daney  *
33af866496SDavid Daney  *
34af866496SDavid Daney  */
35af866496SDavid Daney 
36af866496SDavid Daney #ifndef __CVMX_FPA_H__
37af866496SDavid Daney #define __CVMX_FPA_H__
38af866496SDavid Daney 
39edf188beSSteven J. Hill #include <linux/delay.h>
40edf188beSSteven J. Hill 
41a1ce3928SDavid Howells #include <asm/octeon/cvmx-address.h>
42a1ce3928SDavid Howells #include <asm/octeon/cvmx-fpa-defs.h>
43af866496SDavid Daney 
44af866496SDavid Daney #define CVMX_FPA_NUM_POOLS	8
45af866496SDavid Daney #define CVMX_FPA_MIN_BLOCK_SIZE 128
46af866496SDavid Daney #define CVMX_FPA_ALIGNMENT	128
47af866496SDavid Daney 
48af866496SDavid Daney /**
49af866496SDavid Daney  * Structure describing the data format used for stores to the FPA.
50af866496SDavid Daney  */
51af866496SDavid Daney typedef union {
52af866496SDavid Daney 	uint64_t u64;
53af866496SDavid Daney 	struct {
5411db04c8SPaul Martin #ifdef __BIG_ENDIAN_BITFIELD
55af866496SDavid Daney 		/*
56af866496SDavid Daney 		 * the (64-bit word) location in scratchpad to write
57af866496SDavid Daney 		 * to (if len != 0)
58af866496SDavid Daney 		 */
59af866496SDavid Daney 		uint64_t scraddr:8;
60af866496SDavid Daney 		/* the number of words in the response (0 => no response) */
61af866496SDavid Daney 		uint64_t len:8;
62af866496SDavid Daney 		/* the ID of the device on the non-coherent bus */
63af866496SDavid Daney 		uint64_t did:8;
64af866496SDavid Daney 		/*
65af866496SDavid Daney 		 * the address that will appear in the first tick on
66af866496SDavid Daney 		 * the NCB bus.
67af866496SDavid Daney 		 */
68af866496SDavid Daney 		uint64_t addr:40;
6911db04c8SPaul Martin #else
7011db04c8SPaul Martin 		uint64_t addr:40;
7111db04c8SPaul Martin 		uint64_t did:8;
7211db04c8SPaul Martin 		uint64_t len:8;
7311db04c8SPaul Martin 		uint64_t scraddr:8;
7411db04c8SPaul Martin #endif
75af866496SDavid Daney 	} s;
76af866496SDavid Daney } cvmx_fpa_iobdma_data_t;
77af866496SDavid Daney 
78af866496SDavid Daney /**
79af866496SDavid Daney  * Structure describing the current state of a FPA pool.
80af866496SDavid Daney  */
81af866496SDavid Daney typedef struct {
82af866496SDavid Daney 	/* Name it was created under */
83af866496SDavid Daney 	const char *name;
84af866496SDavid Daney 	/* Size of each block */
85af866496SDavid Daney 	uint64_t size;
86af866496SDavid Daney 	/* The base memory address of whole block */
87af866496SDavid Daney 	void *base;
88af866496SDavid Daney 	/* The number of elements in the pool at creation */
89af866496SDavid Daney 	uint64_t starting_element_count;
90af866496SDavid Daney } cvmx_fpa_pool_info_t;
91af866496SDavid Daney 
92af866496SDavid Daney /**
93af866496SDavid Daney  * Current state of all the pools. Use access functions
94af866496SDavid Daney  * instead of using it directly.
95af866496SDavid Daney  */
96af866496SDavid Daney extern cvmx_fpa_pool_info_t cvmx_fpa_pool_info[CVMX_FPA_NUM_POOLS];
97af866496SDavid Daney 
98af866496SDavid Daney /* CSR typedefs have been moved to cvmx-csr-*.h */
99af866496SDavid Daney 
100af866496SDavid Daney /**
101af866496SDavid Daney  * Return the name of the pool
102af866496SDavid Daney  *
103af866496SDavid Daney  * @pool:   Pool to get the name of
104af866496SDavid Daney  * Returns The name
105af866496SDavid Daney  */
cvmx_fpa_get_name(uint64_t pool)106af866496SDavid Daney static inline const char *cvmx_fpa_get_name(uint64_t pool)
107af866496SDavid Daney {
108af866496SDavid Daney 	return cvmx_fpa_pool_info[pool].name;
109af866496SDavid Daney }
110af866496SDavid Daney 
111af866496SDavid Daney /**
112af866496SDavid Daney  * Return the base of the pool
113af866496SDavid Daney  *
114af866496SDavid Daney  * @pool:   Pool to get the base of
115af866496SDavid Daney  * Returns The base
116af866496SDavid Daney  */
cvmx_fpa_get_base(uint64_t pool)117af866496SDavid Daney static inline void *cvmx_fpa_get_base(uint64_t pool)
118af866496SDavid Daney {
119af866496SDavid Daney 	return cvmx_fpa_pool_info[pool].base;
120af866496SDavid Daney }
121af866496SDavid Daney 
122af866496SDavid Daney /**
123af866496SDavid Daney  * Check if a pointer belongs to an FPA pool. Return non-zero
124af866496SDavid Daney  * if the supplied pointer is inside the memory controlled by
125af866496SDavid Daney  * an FPA pool.
126af866496SDavid Daney  *
127af866496SDavid Daney  * @pool:   Pool to check
128af866496SDavid Daney  * @ptr:    Pointer to check
129af866496SDavid Daney  * Returns Non-zero if pointer is in the pool. Zero if not
130af866496SDavid Daney  */
cvmx_fpa_is_member(uint64_t pool,void * ptr)131af866496SDavid Daney static inline int cvmx_fpa_is_member(uint64_t pool, void *ptr)
132af866496SDavid Daney {
133af866496SDavid Daney 	return ((ptr >= cvmx_fpa_pool_info[pool].base) &&
134af866496SDavid Daney 		((char *)ptr <
135af866496SDavid Daney 		 ((char *)(cvmx_fpa_pool_info[pool].base)) +
136af866496SDavid Daney 		 cvmx_fpa_pool_info[pool].size *
137af866496SDavid Daney 		 cvmx_fpa_pool_info[pool].starting_element_count));
138af866496SDavid Daney }
139af866496SDavid Daney 
140af866496SDavid Daney /**
141af866496SDavid Daney  * Enable the FPA for use. Must be performed after any CSR
142af866496SDavid Daney  * configuration but before any other FPA functions.
143af866496SDavid Daney  */
cvmx_fpa_enable(void)144af866496SDavid Daney static inline void cvmx_fpa_enable(void)
145af866496SDavid Daney {
146af866496SDavid Daney 	union cvmx_fpa_ctl_status status;
147af866496SDavid Daney 
148af866496SDavid Daney 	status.u64 = cvmx_read_csr(CVMX_FPA_CTL_STATUS);
149af866496SDavid Daney 	if (status.s.enb) {
150af866496SDavid Daney 		cvmx_dprintf
151af866496SDavid Daney 		    ("Warning: Enabling FPA when FPA already enabled.\n");
152af866496SDavid Daney 	}
153af866496SDavid Daney 
154af866496SDavid Daney 	/*
155af866496SDavid Daney 	 * Do runtime check as we allow pass1 compiled code to run on
156af866496SDavid Daney 	 * pass2 chips.
157af866496SDavid Daney 	 */
158af866496SDavid Daney 	if (cvmx_octeon_is_pass1()) {
159af866496SDavid Daney 		union cvmx_fpa_fpfx_marks marks;
160af866496SDavid Daney 		int i;
161af866496SDavid Daney 		for (i = 1; i < 8; i++) {
162af866496SDavid Daney 			marks.u64 =
163af866496SDavid Daney 			    cvmx_read_csr(CVMX_FPA_FPF1_MARKS + (i - 1) * 8ull);
164af866496SDavid Daney 			marks.s.fpf_wr = 0xe0;
165af866496SDavid Daney 			cvmx_write_csr(CVMX_FPA_FPF1_MARKS + (i - 1) * 8ull,
166af866496SDavid Daney 				       marks.u64);
167af866496SDavid Daney 		}
168af866496SDavid Daney 
169af866496SDavid Daney 		/* Enforce a 10 cycle delay between config and enable */
170edf188beSSteven J. Hill 		__delay(10);
171af866496SDavid Daney 	}
172af866496SDavid Daney 
173af866496SDavid Daney 	/* FIXME: CVMX_FPA_CTL_STATUS read is unmodelled */
174af866496SDavid Daney 	status.u64 = 0;
175af866496SDavid Daney 	status.s.enb = 1;
176af866496SDavid Daney 	cvmx_write_csr(CVMX_FPA_CTL_STATUS, status.u64);
177af866496SDavid Daney }
178af866496SDavid Daney 
179af866496SDavid Daney /**
180af866496SDavid Daney  * Get a new block from the FPA
181af866496SDavid Daney  *
182af866496SDavid Daney  * @pool:   Pool to get the block from
183af866496SDavid Daney  * Returns Pointer to the block or NULL on failure
184af866496SDavid Daney  */
cvmx_fpa_alloc(uint64_t pool)185af866496SDavid Daney static inline void *cvmx_fpa_alloc(uint64_t pool)
186af866496SDavid Daney {
187af866496SDavid Daney 	uint64_t address =
188af866496SDavid Daney 	    cvmx_read_csr(CVMX_ADDR_DID(CVMX_FULL_DID(CVMX_OCT_DID_FPA, pool)));
189af866496SDavid Daney 	if (address)
190af866496SDavid Daney 		return cvmx_phys_to_ptr(address);
191af866496SDavid Daney 	else
192af866496SDavid Daney 		return NULL;
193af866496SDavid Daney }
194af866496SDavid Daney 
195af866496SDavid Daney /**
196af866496SDavid Daney  * Asynchronously get a new block from the FPA
197af866496SDavid Daney  *
198af866496SDavid Daney  * @scr_addr: Local scratch address to put response in.	 This is a byte address,
199af866496SDavid Daney  *		    but must be 8 byte aligned.
200af866496SDavid Daney  * @pool:      Pool to get the block from
201af866496SDavid Daney  */
cvmx_fpa_async_alloc(uint64_t scr_addr,uint64_t pool)202af866496SDavid Daney static inline void cvmx_fpa_async_alloc(uint64_t scr_addr, uint64_t pool)
203af866496SDavid Daney {
204af866496SDavid Daney 	cvmx_fpa_iobdma_data_t data;
205af866496SDavid Daney 
206af866496SDavid Daney 	/*
207af866496SDavid Daney 	 * Hardware only uses 64 bit aligned locations, so convert
208af866496SDavid Daney 	 * from byte address to 64-bit index
209af866496SDavid Daney 	 */
210af866496SDavid Daney 	data.s.scraddr = scr_addr >> 3;
211af866496SDavid Daney 	data.s.len = 1;
212af866496SDavid Daney 	data.s.did = CVMX_FULL_DID(CVMX_OCT_DID_FPA, pool);
213af866496SDavid Daney 	data.s.addr = 0;
214af866496SDavid Daney 	cvmx_send_single(data.u64);
215af866496SDavid Daney }
216af866496SDavid Daney 
217af866496SDavid Daney /**
218af866496SDavid Daney  * Free a block allocated with a FPA pool.  Does NOT provide memory
219af866496SDavid Daney  * ordering in cases where the memory block was modified by the core.
220af866496SDavid Daney  *
221af866496SDavid Daney  * @ptr:    Block to free
222af866496SDavid Daney  * @pool:   Pool to put it in
223af866496SDavid Daney  * @num_cache_lines:
224af866496SDavid Daney  *		 Cache lines to invalidate
225af866496SDavid Daney  */
cvmx_fpa_free_nosync(void * ptr,uint64_t pool,uint64_t num_cache_lines)226af866496SDavid Daney static inline void cvmx_fpa_free_nosync(void *ptr, uint64_t pool,
227af866496SDavid Daney 					uint64_t num_cache_lines)
228af866496SDavid Daney {
229af866496SDavid Daney 	cvmx_addr_t newptr;
230af866496SDavid Daney 	newptr.u64 = cvmx_ptr_to_phys(ptr);
231af866496SDavid Daney 	newptr.sfilldidspace.didspace =
232af866496SDavid Daney 	    CVMX_ADDR_DIDSPACE(CVMX_FULL_DID(CVMX_OCT_DID_FPA, pool));
233af866496SDavid Daney 	/* Prevent GCC from reordering around free */
234af866496SDavid Daney 	barrier();
235af866496SDavid Daney 	/* value written is number of cache lines not written back */
236af866496SDavid Daney 	cvmx_write_io(newptr.u64, num_cache_lines);
237af866496SDavid Daney }
238af866496SDavid Daney 
239af866496SDavid Daney /**
240af866496SDavid Daney  * Free a block allocated with a FPA pool.  Provides required memory
241af866496SDavid Daney  * ordering in cases where memory block was modified by core.
242af866496SDavid Daney  *
243af866496SDavid Daney  * @ptr:    Block to free
244af866496SDavid Daney  * @pool:   Pool to put it in
245af866496SDavid Daney  * @num_cache_lines:
246af866496SDavid Daney  *		 Cache lines to invalidate
247af866496SDavid Daney  */
cvmx_fpa_free(void * ptr,uint64_t pool,uint64_t num_cache_lines)248af866496SDavid Daney static inline void cvmx_fpa_free(void *ptr, uint64_t pool,
249af866496SDavid Daney 				 uint64_t num_cache_lines)
250af866496SDavid Daney {
251af866496SDavid Daney 	cvmx_addr_t newptr;
252af866496SDavid Daney 	newptr.u64 = cvmx_ptr_to_phys(ptr);
253af866496SDavid Daney 	newptr.sfilldidspace.didspace =
254af866496SDavid Daney 	    CVMX_ADDR_DIDSPACE(CVMX_FULL_DID(CVMX_OCT_DID_FPA, pool));
255af866496SDavid Daney 	/*
256af866496SDavid Daney 	 * Make sure that any previous writes to memory go out before
257af866496SDavid Daney 	 * we free this buffer.	 This also serves as a barrier to
258af866496SDavid Daney 	 * prevent GCC from reordering operations to after the
259af866496SDavid Daney 	 * free.
260af866496SDavid Daney 	 */
261af866496SDavid Daney 	CVMX_SYNCWS;
262af866496SDavid Daney 	/* value written is number of cache lines not written back */
263af866496SDavid Daney 	cvmx_write_io(newptr.u64, num_cache_lines);
264af866496SDavid Daney }
265af866496SDavid Daney 
266af866496SDavid Daney /**
267af866496SDavid Daney  * Shutdown a Memory pool and validate that it had all of
268af866496SDavid Daney  * the buffers originally placed in it. This should only be
269af866496SDavid Daney  * called by one processor after all hardware has finished
270af866496SDavid Daney  * using the pool.
271af866496SDavid Daney  *
272af866496SDavid Daney  * @pool:   Pool to shutdown
273af866496SDavid Daney  * Returns Zero on success
274af866496SDavid Daney  *	   - Positive is count of missing buffers
275af866496SDavid Daney  *	   - Negative is too many buffers or corrupted pointers
276af866496SDavid Daney  */
277af866496SDavid Daney extern uint64_t cvmx_fpa_shutdown_pool(uint64_t pool);
278af866496SDavid Daney 
279af866496SDavid Daney /**
280af866496SDavid Daney  * Get the size of blocks controlled by the pool
281af866496SDavid Daney  * This is resolved to a constant at compile time.
282af866496SDavid Daney  *
283af866496SDavid Daney  * @pool:   Pool to access
284af866496SDavid Daney  * Returns Size of the block in bytes
285af866496SDavid Daney  */
286af866496SDavid Daney uint64_t cvmx_fpa_get_block_size(uint64_t pool);
287af866496SDavid Daney 
288af866496SDavid Daney #endif /*  __CVM_FPA_H__ */
289