cvmx-fpa.h 8.1 KB

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