cvmx-helper-fpa.c revision 259065
1290650Shselasky/***********************license start***************
2322151Shselasky * Copyright (c) 2003-2010  Cavium Inc. (support@cavium.com). All rights
3290650Shselasky * reserved.
4290650Shselasky *
5290650Shselasky *
6290650Shselasky * Redistribution and use in source and binary forms, with or without
7290650Shselasky * modification, are permitted provided that the following conditions are
8290650Shselasky * met:
9290650Shselasky *
10290650Shselasky *   * Redistributions of source code must retain the above copyright
11290650Shselasky *     notice, this list of conditions and the following disclaimer.
12290650Shselasky *
13290650Shselasky *   * Redistributions in binary form must reproduce the above
14290650Shselasky *     copyright notice, this list of conditions and the following
15290650Shselasky *     disclaimer in the documentation and/or other materials provided
16290650Shselasky *     with the distribution.
17290650Shselasky
18290650Shselasky *   * Neither the name of Cavium Inc. nor the names of
19290650Shselasky *     its contributors may be used to endorse or promote products
20290650Shselasky *     derived from this software without specific prior written
21290650Shselasky *     permission.
22290650Shselasky
23290650Shselasky * This Software, including technical data, may be subject to U.S. export  control
24290650Shselasky * laws, including the U.S. Export Administration Act and its  associated
25290650Shselasky * regulations, and may be subject to export or import  regulations in other
26290650Shselasky * countries.
27290650Shselasky
28290650Shselasky * TO THE MAXIMUM EXTENT PERMITTED BY LAW, THE SOFTWARE IS PROVIDED "AS IS"
29290650Shselasky * AND WITH ALL FAULTS AND CAVIUM INC. MAKES NO PROMISES, REPRESENTATIONS OR
30290650Shselasky * WARRANTIES, EITHER EXPRESS, IMPLIED, STATUTORY, OR OTHERWISE, WITH RESPECT TO
31290650Shselasky * THE SOFTWARE, INCLUDING ITS CONDITION, ITS CONFORMITY TO ANY REPRESENTATION OR
32290650Shselasky * DESCRIPTION, OR THE EXISTENCE OF ANY LATENT OR PATENT DEFECTS, AND CAVIUM
33290650Shselasky * SPECIFICALLY DISCLAIMS ALL IMPLIED (IF ANY) WARRANTIES OF TITLE,
34290650Shselasky * MERCHANTABILITY, NONINFRINGEMENT, FITNESS FOR A PARTICULAR PURPOSE, LACK OF
35290650Shselasky * VIRUSES, ACCURACY OR COMPLETENESS, QUIET ENJOYMENT, QUIET POSSESSION OR
36302270Shselasky * CORRESPONDENCE TO DESCRIPTION. THE ENTIRE  RISK ARISING OUT OF USE OR
37290650Shselasky * PERFORMANCE OF THE SOFTWARE LIES WITH YOU.
38290650Shselasky ***********************license end**************************************/
39290650Shselasky
40290650Shselasky
41290650Shselasky
42290650Shselasky
43290650Shselasky
44290650Shselasky
45306244Shselasky
46322145Shselasky/**
47306244Shselasky * @file
48290650Shselasky *
49290650Shselasky * Helper functions for FPA setup.
50290650Shselasky *
51290650Shselasky * <hr>$Revision: 70030 $<hr>
52290650Shselasky */
53290650Shselasky#include "cvmx.h"
54306244Shselasky#include "cvmx-bootmem.h"
55290650Shselasky#include "cvmx-fpa.h"
56290650Shselasky#include "cvmx-helper-fpa.h"
57290650Shselasky
58290650Shselasky/**
59290650Shselasky * @INTERNAL
60290650Shselasky * Allocate memory for and initialize a single FPA pool.
61290650Shselasky *
62290650Shselasky * @param pool    Pool to initialize
63290650Shselasky * @param buffer_size  Size of buffers to allocate in bytes
64290650Shselasky * @param buffers Number of buffers to put in the pool. Zero is allowed
65290650Shselasky * @param name    String name of the pool for debugging purposes
66290650Shselasky * @return Zero on success, non-zero on failure
67290650Shselasky */
68290650Shselaskystatic int __cvmx_helper_initialize_fpa_pool(int pool, uint64_t buffer_size,
69290650Shselasky                                           uint64_t buffers, const char *name)
70290650Shselasky{
71290650Shselasky    uint64_t current_num;
72290650Shselasky    void *memory;
73290650Shselasky    uint64_t align = CVMX_CACHE_LINE_SIZE;
74290650Shselasky
75290650Shselasky    /* Align the allocation so that power of 2 size buffers are naturally aligned */
76290650Shselasky    while (align < buffer_size)
77290650Shselasky        align = align << 1;
78290650Shselasky
79290650Shselasky    if (buffers == 0)
80290650Shselasky        return 0;
81290650Shselasky
82290650Shselasky    current_num = cvmx_read_csr(CVMX_FPA_QUEX_AVAILABLE(pool));
83290650Shselasky    if (current_num)
84290650Shselasky    {
85290650Shselasky        cvmx_dprintf("Fpa pool %d(%s) already has %llu buffers. Skipping setup.\n",
86290650Shselasky                     pool, name, (unsigned long long)current_num);
87290650Shselasky        return 0;
88306244Shselasky    }
89306244Shselasky
90306244Shselasky    memory = cvmx_bootmem_alloc(buffer_size * buffers, align);
91306244Shselasky    if (memory == NULL)
92306244Shselasky    {
93306244Shselasky        cvmx_dprintf("Out of memory initializing fpa pool %d(%s).\n", pool, name);
94306244Shselasky        return -1;
95306244Shselasky    }
96306244Shselasky    cvmx_fpa_setup_pool(pool, name, memory, buffer_size, buffers);
97306244Shselasky    return 0;
98290650Shselasky}
99290650Shselasky
100290650Shselasky
101306244Shselasky/**
102306244Shselasky * @INTERNAL
103306244Shselasky * Allocate memory and initialize the FPA pools using memory
104306244Shselasky * from cvmx-bootmem. Specifying zero for the number of
105306244Shselasky * buffers will cause that FPA pool to not be setup. This is
106306244Shselasky * useful if you aren't using some of the hardware and want
107306244Shselasky * to save memory. Use cvmx_helper_initialize_fpa instead of
108306244Shselasky * this function directly.
109306244Shselasky *
110306244Shselasky * @param pip_pool Should always be CVMX_FPA_PACKET_POOL
111306244Shselasky * @param pip_size Should always be CVMX_FPA_PACKET_POOL_SIZE
112306244Shselasky * @param pip_buffers
113306244Shselasky *                 Number of packet buffers.
114306244Shselasky * @param wqe_pool Should always be CVMX_FPA_WQE_POOL
115306244Shselasky * @param wqe_size Should always be CVMX_FPA_WQE_POOL_SIZE
116306244Shselasky * @param wqe_entries
117306244Shselasky *                 Number of work queue entries
118306244Shselasky * @param pko_pool Should always be CVMX_FPA_OUTPUT_BUFFER_POOL
119306244Shselasky * @param pko_size Should always be CVMX_FPA_OUTPUT_BUFFER_POOL_SIZE
120306244Shselasky * @param pko_buffers
121290650Shselasky *                 PKO Command buffers. You should at minimum have two per
122290650Shselasky *                 each PKO queue.
123290650Shselasky * @param tim_pool Should always be CVMX_FPA_TIMER_POOL
124308684Shselasky * @param tim_size Should always be CVMX_FPA_TIMER_POOL_SIZE
125306244Shselasky * @param tim_buffers
126306244Shselasky *                 TIM ring buffer command queues. At least two per timer bucket
127290650Shselasky *                 is recommended.
128290650Shselasky * @param dfa_pool Should always be CVMX_FPA_DFA_POOL
129290650Shselasky * @param dfa_size Should always be CVMX_FPA_DFA_POOL_SIZE
130290650Shselasky * @param dfa_buffers
131290650Shselasky *                 DFA command buffer. A relatively small (32 for example)
132290650Shselasky *                 number should work.
133290650Shselasky * @return Zero on success, non-zero if out of memory
134290650Shselasky */
135290650Shselaskystatic int __cvmx_helper_initialize_fpa(int pip_pool, int pip_size, int pip_buffers,
136290650Shselasky                                        int wqe_pool, int wqe_size, int wqe_entries,
137290650Shselasky                                        int pko_pool, int pko_size, int pko_buffers,
138290650Shselasky                                        int tim_pool, int tim_size, int tim_buffers,
139290650Shselasky                                        int dfa_pool, int dfa_size, int dfa_buffers)
140290650Shselasky{
141290650Shselasky    int status;
142290650Shselasky
143290650Shselasky    cvmx_fpa_enable();
144306244Shselasky
145290650Shselasky    if ((pip_buffers > 0) && (pip_buffers <= 64))
146290650Shselasky        cvmx_dprintf("Warning: %d packet buffers may not be enough for hardware"
147290650Shselasky                     " prefetch. 65 or more is recommended.\n", pip_buffers);
148290650Shselasky
149290650Shselasky    if (pip_pool >= 0)
150290650Shselasky    {
151290650Shselasky        status = __cvmx_helper_initialize_fpa_pool(pip_pool, pip_size, pip_buffers,
152290650Shselasky                                                 "Packet Buffers");
153306244Shselasky        if (status)
154306244Shselasky            return status;
155306244Shselasky    }
156306244Shselasky
157306244Shselasky    if (wqe_pool >= 0)
158306244Shselasky    {
159290650Shselasky        status = __cvmx_helper_initialize_fpa_pool(wqe_pool, wqe_size, wqe_entries,
160290650Shselasky                                                 "Work Queue Entries");
161290650Shselasky        if (status)
162290650Shselasky            return status;
163290650Shselasky    }
164290650Shselasky
165290650Shselasky    if (pko_pool >= 0)
166290650Shselasky    {
167290650Shselasky        status = __cvmx_helper_initialize_fpa_pool(pko_pool, pko_size, pko_buffers,
168290650Shselasky                                                 "PKO Command Buffers");
169290650Shselasky        if (status)
170290650Shselasky            return status;
171290650Shselasky    }
172290650Shselasky
173290650Shselasky    if (tim_pool >= 0)
174290650Shselasky    {
175290650Shselasky        status = __cvmx_helper_initialize_fpa_pool(tim_pool, tim_size, tim_buffers,
176290650Shselasky                                                 "TIM Command Buffers");
177290650Shselasky        if (status)
178290650Shselasky            return status;
179290650Shselasky    }
180290650Shselasky
181290650Shselasky    if (dfa_pool >= 0)
182306244Shselasky    {
183306244Shselasky        status = __cvmx_helper_initialize_fpa_pool(dfa_pool, dfa_size, dfa_buffers,
184306244Shselasky                                                 "DFA Command Buffers");
185306244Shselasky        if (status)
186290650Shselasky            return status;
187290650Shselasky    }
188290650Shselasky
189290650Shselasky    return 0;
190290650Shselasky}
191290650Shselasky
192290650Shselasky
193290650Shselasky/**
194290650Shselasky * Allocate memory and initialize the FPA pools using memory
195290650Shselasky * from cvmx-bootmem. Sizes of each element in the pools is
196290650Shselasky * controlled by the cvmx-config.h header file. Specifying
197290650Shselasky * zero for any parameter will cause that FPA pool to not be
198290650Shselasky * setup. This is useful if you aren't using some of the
199290650Shselasky * hardware and want to save memory.
200290650Shselasky *
201290650Shselasky * @param packet_buffers
202290650Shselasky *               Number of packet buffers to allocate
203290650Shselasky * @param work_queue_entries
204290650Shselasky *               Number of work queue entries
205290650Shselasky * @param pko_buffers
206290650Shselasky *               PKO Command buffers. You should at minimum have two per
207290650Shselasky *               each PKO queue.
208290650Shselasky * @param tim_buffers
209290650Shselasky *               TIM ring buffer command queues. At least two per timer bucket
210290650Shselasky *               is recommended.
211290650Shselasky * @param dfa_buffers
212290650Shselasky *               DFA command buffer. A relatively small (32 for example)
213290650Shselasky *               number should work.
214290650Shselasky * @return Zero on success, non-zero if out of memory
215290650Shselasky */
216290650Shselaskyint cvmx_helper_initialize_fpa(int packet_buffers, int work_queue_entries,
217290650Shselasky                               int pko_buffers, int tim_buffers, int dfa_buffers)
218290650Shselasky{
219290650Shselasky#ifndef CVMX_FPA_PACKET_POOL
220290650Shselasky#define CVMX_FPA_PACKET_POOL -1
221290650Shselasky#define CVMX_FPA_PACKET_POOL_SIZE 0
222290650Shselasky#endif
223290650Shselasky#ifndef CVMX_FPA_WQE_POOL
224290650Shselasky#define CVMX_FPA_WQE_POOL -1
225290650Shselasky#define CVMX_FPA_WQE_POOL_SIZE 0
226290650Shselasky#endif
227290650Shselasky#ifndef CVMX_FPA_OUTPUT_BUFFER_POOL
228290650Shselasky#define CVMX_FPA_OUTPUT_BUFFER_POOL -1
229290650Shselasky#define CVMX_FPA_OUTPUT_BUFFER_POOL_SIZE 0
230290650Shselasky#endif
231290650Shselasky#ifndef CVMX_FPA_TIMER_POOL
232290650Shselasky#define CVMX_FPA_TIMER_POOL -1
233290650Shselasky#define CVMX_FPA_TIMER_POOL_SIZE 0
234290650Shselasky#endif
235290650Shselasky#ifndef CVMX_FPA_DFA_POOL
236290650Shselasky#define CVMX_FPA_DFA_POOL -1
237290650Shselasky#define CVMX_FPA_DFA_POOL_SIZE 0
238290650Shselasky#endif
239290650Shselasky    return __cvmx_helper_initialize_fpa(
240290650Shselasky        CVMX_FPA_PACKET_POOL,        CVMX_FPA_PACKET_POOL_SIZE,          packet_buffers,
241290650Shselasky        CVMX_FPA_WQE_POOL,           CVMX_FPA_WQE_POOL_SIZE,             work_queue_entries,
242290650Shselasky        CVMX_FPA_OUTPUT_BUFFER_POOL, CVMX_FPA_OUTPUT_BUFFER_POOL_SIZE,   pko_buffers,
243290650Shselasky        CVMX_FPA_TIMER_POOL,         CVMX_FPA_TIMER_POOL_SIZE,           tim_buffers,
244290650Shselasky        CVMX_FPA_DFA_POOL,           CVMX_FPA_DFA_POOL_SIZE,             dfa_buffers);
245290650Shselasky}
246290650Shselasky
247290650Shselasky