cvmx-helper-fpa.c 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  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. * Helper functions for FPA setup.
  31. *
  32. */
  33. #include "executive-config.h"
  34. #include "cvmx-config.h"
  35. #include "cvmx.h"
  36. #include "cvmx-bootmem.h"
  37. #include "cvmx-fpa.h"
  38. #include "cvmx-helper-fpa.h"
  39. /**
  40. * Allocate memory for and initialize a single FPA pool.
  41. *
  42. * @pool: Pool to initialize
  43. * @buffer_size: Size of buffers to allocate in bytes
  44. * @buffers: Number of buffers to put in the pool. Zero is allowed
  45. * @name: String name of the pool for debugging purposes
  46. * Returns Zero on success, non-zero on failure
  47. */
  48. static int __cvmx_helper_initialize_fpa_pool(int pool, uint64_t buffer_size,
  49. uint64_t buffers, const char *name)
  50. {
  51. uint64_t current_num;
  52. void *memory;
  53. uint64_t align = CVMX_CACHE_LINE_SIZE;
  54. /*
  55. * Align the allocation so that power of 2 size buffers are
  56. * naturally aligned.
  57. */
  58. while (align < buffer_size)
  59. align = align << 1;
  60. if (buffers == 0)
  61. return 0;
  62. current_num = cvmx_read_csr(CVMX_FPA_QUEX_AVAILABLE(pool));
  63. if (current_num) {
  64. cvmx_dprintf("Fpa pool %d(%s) already has %llu buffers. "
  65. "Skipping setup.\n",
  66. pool, name, (unsigned long long)current_num);
  67. return 0;
  68. }
  69. memory = cvmx_bootmem_alloc(buffer_size * buffers, align);
  70. if (memory == NULL) {
  71. cvmx_dprintf("Out of memory initializing fpa pool %d(%s).\n",
  72. pool, name);
  73. return -1;
  74. }
  75. cvmx_fpa_setup_pool(pool, name, memory, buffer_size, buffers);
  76. return 0;
  77. }
  78. /**
  79. * Allocate memory and initialize the FPA pools using memory
  80. * from cvmx-bootmem. Specifying zero for the number of
  81. * buffers will cause that FPA pool to not be setup. This is
  82. * useful if you aren't using some of the hardware and want
  83. * to save memory. Use cvmx_helper_initialize_fpa instead of
  84. * this function directly.
  85. *
  86. * @pip_pool: Should always be CVMX_FPA_PACKET_POOL
  87. * @pip_size: Should always be CVMX_FPA_PACKET_POOL_SIZE
  88. * @pip_buffers:
  89. * Number of packet buffers.
  90. * @wqe_pool: Should always be CVMX_FPA_WQE_POOL
  91. * @wqe_size: Should always be CVMX_FPA_WQE_POOL_SIZE
  92. * @wqe_entries:
  93. * Number of work queue entries
  94. * @pko_pool: Should always be CVMX_FPA_OUTPUT_BUFFER_POOL
  95. * @pko_size: Should always be CVMX_FPA_OUTPUT_BUFFER_POOL_SIZE
  96. * @pko_buffers:
  97. * PKO Command buffers. You should at minimum have two per
  98. * each PKO queue.
  99. * @tim_pool: Should always be CVMX_FPA_TIMER_POOL
  100. * @tim_size: Should always be CVMX_FPA_TIMER_POOL_SIZE
  101. * @tim_buffers:
  102. * TIM ring buffer command queues. At least two per timer bucket
  103. * is recommened.
  104. * @dfa_pool: Should always be CVMX_FPA_DFA_POOL
  105. * @dfa_size: Should always be CVMX_FPA_DFA_POOL_SIZE
  106. * @dfa_buffers:
  107. * DFA command buffer. A relatively small (32 for example)
  108. * number should work.
  109. * Returns Zero on success, non-zero if out of memory
  110. */
  111. static int __cvmx_helper_initialize_fpa(int pip_pool, int pip_size,
  112. int pip_buffers, int wqe_pool,
  113. int wqe_size, int wqe_entries,
  114. int pko_pool, int pko_size,
  115. int pko_buffers, int tim_pool,
  116. int tim_size, int tim_buffers,
  117. int dfa_pool, int dfa_size,
  118. int dfa_buffers)
  119. {
  120. int status;
  121. cvmx_fpa_enable();
  122. if ((pip_buffers > 0) && (pip_buffers <= 64))
  123. cvmx_dprintf
  124. ("Warning: %d packet buffers may not be enough for hardware"
  125. " prefetch. 65 or more is recommended.\n", pip_buffers);
  126. if (pip_pool >= 0) {
  127. status =
  128. __cvmx_helper_initialize_fpa_pool(pip_pool, pip_size,
  129. pip_buffers,
  130. "Packet Buffers");
  131. if (status)
  132. return status;
  133. }
  134. if (wqe_pool >= 0) {
  135. status =
  136. __cvmx_helper_initialize_fpa_pool(wqe_pool, wqe_size,
  137. wqe_entries,
  138. "Work Queue Entries");
  139. if (status)
  140. return status;
  141. }
  142. if (pko_pool >= 0) {
  143. status =
  144. __cvmx_helper_initialize_fpa_pool(pko_pool, pko_size,
  145. pko_buffers,
  146. "PKO Command Buffers");
  147. if (status)
  148. return status;
  149. }
  150. if (tim_pool >= 0) {
  151. status =
  152. __cvmx_helper_initialize_fpa_pool(tim_pool, tim_size,
  153. tim_buffers,
  154. "TIM Command Buffers");
  155. if (status)
  156. return status;
  157. }
  158. if (dfa_pool >= 0) {
  159. status =
  160. __cvmx_helper_initialize_fpa_pool(dfa_pool, dfa_size,
  161. dfa_buffers,
  162. "DFA Command Buffers");
  163. if (status)
  164. return status;
  165. }
  166. return 0;
  167. }
  168. /**
  169. * Allocate memory and initialize the FPA pools using memory
  170. * from cvmx-bootmem. Sizes of each element in the pools is
  171. * controlled by the cvmx-config.h header file. Specifying
  172. * zero for any parameter will cause that FPA pool to not be
  173. * setup. This is useful if you aren't using some of the
  174. * hardware and want to save memory.
  175. *
  176. * @packet_buffers:
  177. * Number of packet buffers to allocate
  178. * @work_queue_entries:
  179. * Number of work queue entries
  180. * @pko_buffers:
  181. * PKO Command buffers. You should at minimum have two per
  182. * each PKO queue.
  183. * @tim_buffers:
  184. * TIM ring buffer command queues. At least two per timer bucket
  185. * is recommened.
  186. * @dfa_buffers:
  187. * DFA command buffer. A relatively small (32 for example)
  188. * number should work.
  189. * Returns Zero on success, non-zero if out of memory
  190. */
  191. int cvmx_helper_initialize_fpa(int packet_buffers, int work_queue_entries,
  192. int pko_buffers, int tim_buffers,
  193. int dfa_buffers)
  194. {
  195. #ifndef CVMX_FPA_PACKET_POOL
  196. #define CVMX_FPA_PACKET_POOL -1
  197. #define CVMX_FPA_PACKET_POOL_SIZE 0
  198. #endif
  199. #ifndef CVMX_FPA_WQE_POOL
  200. #define CVMX_FPA_WQE_POOL -1
  201. #define CVMX_FPA_WQE_POOL_SIZE 0
  202. #endif
  203. #ifndef CVMX_FPA_OUTPUT_BUFFER_POOL
  204. #define CVMX_FPA_OUTPUT_BUFFER_POOL -1
  205. #define CVMX_FPA_OUTPUT_BUFFER_POOL_SIZE 0
  206. #endif
  207. #ifndef CVMX_FPA_TIMER_POOL
  208. #define CVMX_FPA_TIMER_POOL -1
  209. #define CVMX_FPA_TIMER_POOL_SIZE 0
  210. #endif
  211. #ifndef CVMX_FPA_DFA_POOL
  212. #define CVMX_FPA_DFA_POOL -1
  213. #define CVMX_FPA_DFA_POOL_SIZE 0
  214. #endif
  215. return __cvmx_helper_initialize_fpa(CVMX_FPA_PACKET_POOL,
  216. CVMX_FPA_PACKET_POOL_SIZE,
  217. packet_buffers, CVMX_FPA_WQE_POOL,
  218. CVMX_FPA_WQE_POOL_SIZE,
  219. work_queue_entries,
  220. CVMX_FPA_OUTPUT_BUFFER_POOL,
  221. CVMX_FPA_OUTPUT_BUFFER_POOL_SIZE,
  222. pko_buffers, CVMX_FPA_TIMER_POOL,
  223. CVMX_FPA_TIMER_POOL_SIZE,
  224. tim_buffers, CVMX_FPA_DFA_POOL,
  225. CVMX_FPA_DFA_POOL_SIZE,
  226. dfa_buffers);
  227. }