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