1*4882a593Smuzhiyun /***********************license start***************
2*4882a593Smuzhiyun * Author: Cavium Networks
3*4882a593Smuzhiyun *
4*4882a593Smuzhiyun * Contact: support@caviumnetworks.com
5*4882a593Smuzhiyun * This file is part of the OCTEON SDK
6*4882a593Smuzhiyun *
7*4882a593Smuzhiyun * Copyright (c) 2003-2008 Cavium Networks
8*4882a593Smuzhiyun *
9*4882a593Smuzhiyun * This file is free software; you can redistribute it and/or modify
10*4882a593Smuzhiyun * it under the terms of the GNU General Public License, Version 2, as
11*4882a593Smuzhiyun * published by the Free Software Foundation.
12*4882a593Smuzhiyun *
13*4882a593Smuzhiyun * This file is distributed in the hope that it will be useful, but
14*4882a593Smuzhiyun * AS-IS and WITHOUT ANY WARRANTY; without even the implied warranty
15*4882a593Smuzhiyun * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, TITLE, or
16*4882a593Smuzhiyun * NONINFRINGEMENT. See the GNU General Public License for more
17*4882a593Smuzhiyun * details.
18*4882a593Smuzhiyun *
19*4882a593Smuzhiyun * You should have received a copy of the GNU General Public License
20*4882a593Smuzhiyun * along with this file; if not, write to the Free Software
21*4882a593Smuzhiyun * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
22*4882a593Smuzhiyun * or visit http://www.gnu.org/licenses/.
23*4882a593Smuzhiyun *
24*4882a593Smuzhiyun * This file may also be available under a different license from Cavium.
25*4882a593Smuzhiyun * Contact Cavium Networks for more information
26*4882a593Smuzhiyun ***********************license end**************************************/
27*4882a593Smuzhiyun
28*4882a593Smuzhiyun /*
29*4882a593Smuzhiyun *
30*4882a593Smuzhiyun * Support functions for managing command queues used for
31*4882a593Smuzhiyun * various hardware blocks.
32*4882a593Smuzhiyun *
33*4882a593Smuzhiyun * The common command queue infrastructure abstracts out the
34*4882a593Smuzhiyun * software necessary for adding to Octeon's chained queue
35*4882a593Smuzhiyun * structures. These structures are used for commands to the
36*4882a593Smuzhiyun * PKO, ZIP, DFA, RAID, and DMA engine blocks. Although each
37*4882a593Smuzhiyun * hardware unit takes commands and CSRs of different types,
38*4882a593Smuzhiyun * they all use basic linked command buffers to store the
39*4882a593Smuzhiyun * pending request. In general, users of the CVMX API don't
40*4882a593Smuzhiyun * call cvmx-cmd-queue functions directly. Instead the hardware
41*4882a593Smuzhiyun * unit specific wrapper should be used. The wrappers perform
42*4882a593Smuzhiyun * unit specific validation and CSR writes to submit the
43*4882a593Smuzhiyun * commands.
44*4882a593Smuzhiyun *
45*4882a593Smuzhiyun * Even though most software will never directly interact with
46*4882a593Smuzhiyun * cvmx-cmd-queue, knowledge of its internal working can help
47*4882a593Smuzhiyun * in diagnosing performance problems and help with debugging.
48*4882a593Smuzhiyun *
49*4882a593Smuzhiyun * Command queue pointers are stored in a global named block
50*4882a593Smuzhiyun * called "cvmx_cmd_queues". Except for the PKO queues, each
51*4882a593Smuzhiyun * hardware queue is stored in its own cache line to reduce SMP
52*4882a593Smuzhiyun * contention on spin locks. The PKO queues are stored such that
53*4882a593Smuzhiyun * every 16th queue is next to each other in memory. This scheme
54*4882a593Smuzhiyun * allows for queues being in separate cache lines when there
55*4882a593Smuzhiyun * are low number of queues per port. With 16 queues per port,
56*4882a593Smuzhiyun * the first queue for each port is in the same cache area. The
57*4882a593Smuzhiyun * second queues for each port are in another area, etc. This
58*4882a593Smuzhiyun * allows software to implement very efficient lockless PKO with
59*4882a593Smuzhiyun * 16 queues per port using a minimum of cache lines per core.
60*4882a593Smuzhiyun * All queues for a given core will be isolated in the same
61*4882a593Smuzhiyun * cache area.
62*4882a593Smuzhiyun *
63*4882a593Smuzhiyun * In addition to the memory pointer layout, cvmx-cmd-queue
64*4882a593Smuzhiyun * provides an optimized fair ll/sc locking mechanism for the
65*4882a593Smuzhiyun * queues. The lock uses a "ticket / now serving" model to
66*4882a593Smuzhiyun * maintain fair order on contended locks. In addition, it uses
67*4882a593Smuzhiyun * predicted locking time to limit cache contention. When a core
68*4882a593Smuzhiyun * know it must wait in line for a lock, it spins on the
69*4882a593Smuzhiyun * internal cycle counter to completely eliminate any causes of
70*4882a593Smuzhiyun * bus traffic.
71*4882a593Smuzhiyun *
72*4882a593Smuzhiyun */
73*4882a593Smuzhiyun
74*4882a593Smuzhiyun #ifndef __CVMX_CMD_QUEUE_H__
75*4882a593Smuzhiyun #define __CVMX_CMD_QUEUE_H__
76*4882a593Smuzhiyun
77*4882a593Smuzhiyun #include <linux/prefetch.h>
78*4882a593Smuzhiyun
79*4882a593Smuzhiyun #include <asm/compiler.h>
80*4882a593Smuzhiyun
81*4882a593Smuzhiyun #include <asm/octeon/cvmx-fpa.h>
82*4882a593Smuzhiyun /**
83*4882a593Smuzhiyun * By default we disable the max depth support. Most programs
84*4882a593Smuzhiyun * don't use it and it slows down the command queue processing
85*4882a593Smuzhiyun * significantly.
86*4882a593Smuzhiyun */
87*4882a593Smuzhiyun #ifndef CVMX_CMD_QUEUE_ENABLE_MAX_DEPTH
88*4882a593Smuzhiyun #define CVMX_CMD_QUEUE_ENABLE_MAX_DEPTH 0
89*4882a593Smuzhiyun #endif
90*4882a593Smuzhiyun
91*4882a593Smuzhiyun /**
92*4882a593Smuzhiyun * Enumeration representing all hardware blocks that use command
93*4882a593Smuzhiyun * queues. Each hardware block has up to 65536 sub identifiers for
94*4882a593Smuzhiyun * multiple command queues. Not all chips support all hardware
95*4882a593Smuzhiyun * units.
96*4882a593Smuzhiyun */
97*4882a593Smuzhiyun typedef enum {
98*4882a593Smuzhiyun CVMX_CMD_QUEUE_PKO_BASE = 0x00000,
99*4882a593Smuzhiyun
100*4882a593Smuzhiyun #define CVMX_CMD_QUEUE_PKO(queue) \
101*4882a593Smuzhiyun ((cvmx_cmd_queue_id_t)(CVMX_CMD_QUEUE_PKO_BASE + (0xffff&(queue))))
102*4882a593Smuzhiyun
103*4882a593Smuzhiyun CVMX_CMD_QUEUE_ZIP = 0x10000,
104*4882a593Smuzhiyun CVMX_CMD_QUEUE_DFA = 0x20000,
105*4882a593Smuzhiyun CVMX_CMD_QUEUE_RAID = 0x30000,
106*4882a593Smuzhiyun CVMX_CMD_QUEUE_DMA_BASE = 0x40000,
107*4882a593Smuzhiyun
108*4882a593Smuzhiyun #define CVMX_CMD_QUEUE_DMA(queue) \
109*4882a593Smuzhiyun ((cvmx_cmd_queue_id_t)(CVMX_CMD_QUEUE_DMA_BASE + (0xffff&(queue))))
110*4882a593Smuzhiyun
111*4882a593Smuzhiyun CVMX_CMD_QUEUE_END = 0x50000,
112*4882a593Smuzhiyun } cvmx_cmd_queue_id_t;
113*4882a593Smuzhiyun
114*4882a593Smuzhiyun /**
115*4882a593Smuzhiyun * Command write operations can fail if the command queue needs
116*4882a593Smuzhiyun * a new buffer and the associated FPA pool is empty. It can also
117*4882a593Smuzhiyun * fail if the number of queued command words reaches the maximum
118*4882a593Smuzhiyun * set at initialization.
119*4882a593Smuzhiyun */
120*4882a593Smuzhiyun typedef enum {
121*4882a593Smuzhiyun CVMX_CMD_QUEUE_SUCCESS = 0,
122*4882a593Smuzhiyun CVMX_CMD_QUEUE_NO_MEMORY = -1,
123*4882a593Smuzhiyun CVMX_CMD_QUEUE_FULL = -2,
124*4882a593Smuzhiyun CVMX_CMD_QUEUE_INVALID_PARAM = -3,
125*4882a593Smuzhiyun CVMX_CMD_QUEUE_ALREADY_SETUP = -4,
126*4882a593Smuzhiyun } cvmx_cmd_queue_result_t;
127*4882a593Smuzhiyun
128*4882a593Smuzhiyun typedef struct {
129*4882a593Smuzhiyun /* You have lock when this is your ticket */
130*4882a593Smuzhiyun uint8_t now_serving;
131*4882a593Smuzhiyun uint64_t unused1:24;
132*4882a593Smuzhiyun /* Maximum outstanding command words */
133*4882a593Smuzhiyun uint32_t max_depth;
134*4882a593Smuzhiyun /* FPA pool buffers come from */
135*4882a593Smuzhiyun uint64_t fpa_pool:3;
136*4882a593Smuzhiyun /* Top of command buffer pointer shifted 7 */
137*4882a593Smuzhiyun uint64_t base_ptr_div128:29;
138*4882a593Smuzhiyun uint64_t unused2:6;
139*4882a593Smuzhiyun /* FPA buffer size in 64bit words minus 1 */
140*4882a593Smuzhiyun uint64_t pool_size_m1:13;
141*4882a593Smuzhiyun /* Number of commands already used in buffer */
142*4882a593Smuzhiyun uint64_t index:13;
143*4882a593Smuzhiyun } __cvmx_cmd_queue_state_t;
144*4882a593Smuzhiyun
145*4882a593Smuzhiyun /**
146*4882a593Smuzhiyun * This structure contains the global state of all command queues.
147*4882a593Smuzhiyun * It is stored in a bootmem named block and shared by all
148*4882a593Smuzhiyun * applications running on Octeon. Tickets are stored in a differnet
149*4882a593Smuzhiyun * cache line that queue information to reduce the contention on the
150*4882a593Smuzhiyun * ll/sc used to get a ticket. If this is not the case, the update
151*4882a593Smuzhiyun * of queue state causes the ll/sc to fail quite often.
152*4882a593Smuzhiyun */
153*4882a593Smuzhiyun typedef struct {
154*4882a593Smuzhiyun uint64_t ticket[(CVMX_CMD_QUEUE_END >> 16) * 256];
155*4882a593Smuzhiyun __cvmx_cmd_queue_state_t state[(CVMX_CMD_QUEUE_END >> 16) * 256];
156*4882a593Smuzhiyun } __cvmx_cmd_queue_all_state_t;
157*4882a593Smuzhiyun
158*4882a593Smuzhiyun /**
159*4882a593Smuzhiyun * Initialize a command queue for use. The initial FPA buffer is
160*4882a593Smuzhiyun * allocated and the hardware unit is configured to point to the
161*4882a593Smuzhiyun * new command queue.
162*4882a593Smuzhiyun *
163*4882a593Smuzhiyun * @queue_id: Hardware command queue to initialize.
164*4882a593Smuzhiyun * @max_depth: Maximum outstanding commands that can be queued.
165*4882a593Smuzhiyun * @fpa_pool: FPA pool the command queues should come from.
166*4882a593Smuzhiyun * @pool_size: Size of each buffer in the FPA pool (bytes)
167*4882a593Smuzhiyun *
168*4882a593Smuzhiyun * Returns CVMX_CMD_QUEUE_SUCCESS or a failure code
169*4882a593Smuzhiyun */
170*4882a593Smuzhiyun cvmx_cmd_queue_result_t cvmx_cmd_queue_initialize(cvmx_cmd_queue_id_t queue_id,
171*4882a593Smuzhiyun int max_depth, int fpa_pool,
172*4882a593Smuzhiyun int pool_size);
173*4882a593Smuzhiyun
174*4882a593Smuzhiyun /**
175*4882a593Smuzhiyun * Shutdown a queue a free it's command buffers to the FPA. The
176*4882a593Smuzhiyun * hardware connected to the queue must be stopped before this
177*4882a593Smuzhiyun * function is called.
178*4882a593Smuzhiyun *
179*4882a593Smuzhiyun * @queue_id: Queue to shutdown
180*4882a593Smuzhiyun *
181*4882a593Smuzhiyun * Returns CVMX_CMD_QUEUE_SUCCESS or a failure code
182*4882a593Smuzhiyun */
183*4882a593Smuzhiyun cvmx_cmd_queue_result_t cvmx_cmd_queue_shutdown(cvmx_cmd_queue_id_t queue_id);
184*4882a593Smuzhiyun
185*4882a593Smuzhiyun /**
186*4882a593Smuzhiyun * Return the number of command words pending in the queue. This
187*4882a593Smuzhiyun * function may be relatively slow for some hardware units.
188*4882a593Smuzhiyun *
189*4882a593Smuzhiyun * @queue_id: Hardware command queue to query
190*4882a593Smuzhiyun *
191*4882a593Smuzhiyun * Returns Number of outstanding commands
192*4882a593Smuzhiyun */
193*4882a593Smuzhiyun int cvmx_cmd_queue_length(cvmx_cmd_queue_id_t queue_id);
194*4882a593Smuzhiyun
195*4882a593Smuzhiyun /**
196*4882a593Smuzhiyun * Return the command buffer to be written to. The purpose of this
197*4882a593Smuzhiyun * function is to allow CVMX routine access t othe low level buffer
198*4882a593Smuzhiyun * for initial hardware setup. User applications should not call this
199*4882a593Smuzhiyun * function directly.
200*4882a593Smuzhiyun *
201*4882a593Smuzhiyun * @queue_id: Command queue to query
202*4882a593Smuzhiyun *
203*4882a593Smuzhiyun * Returns Command buffer or NULL on failure
204*4882a593Smuzhiyun */
205*4882a593Smuzhiyun void *cvmx_cmd_queue_buffer(cvmx_cmd_queue_id_t queue_id);
206*4882a593Smuzhiyun
207*4882a593Smuzhiyun /**
208*4882a593Smuzhiyun * Get the index into the state arrays for the supplied queue id.
209*4882a593Smuzhiyun *
210*4882a593Smuzhiyun * @queue_id: Queue ID to get an index for
211*4882a593Smuzhiyun *
212*4882a593Smuzhiyun * Returns Index into the state arrays
213*4882a593Smuzhiyun */
__cvmx_cmd_queue_get_index(cvmx_cmd_queue_id_t queue_id)214*4882a593Smuzhiyun static inline int __cvmx_cmd_queue_get_index(cvmx_cmd_queue_id_t queue_id)
215*4882a593Smuzhiyun {
216*4882a593Smuzhiyun /*
217*4882a593Smuzhiyun * Warning: This code currently only works with devices that
218*4882a593Smuzhiyun * have 256 queues or less. Devices with more than 16 queues
219*4882a593Smuzhiyun * are laid out in memory to allow cores quick access to
220*4882a593Smuzhiyun * every 16th queue. This reduces cache thrashing when you are
221*4882a593Smuzhiyun * running 16 queues per port to support lockless operation.
222*4882a593Smuzhiyun */
223*4882a593Smuzhiyun int unit = queue_id >> 16;
224*4882a593Smuzhiyun int q = (queue_id >> 4) & 0xf;
225*4882a593Smuzhiyun int core = queue_id & 0xf;
226*4882a593Smuzhiyun return unit * 256 + core * 16 + q;
227*4882a593Smuzhiyun }
228*4882a593Smuzhiyun
229*4882a593Smuzhiyun /**
230*4882a593Smuzhiyun * Lock the supplied queue so nobody else is updating it at the same
231*4882a593Smuzhiyun * time as us.
232*4882a593Smuzhiyun *
233*4882a593Smuzhiyun * @queue_id: Queue ID to lock
234*4882a593Smuzhiyun * @qptr: Pointer to the queue's global state
235*4882a593Smuzhiyun */
__cvmx_cmd_queue_lock(cvmx_cmd_queue_id_t queue_id,__cvmx_cmd_queue_state_t * qptr)236*4882a593Smuzhiyun static inline void __cvmx_cmd_queue_lock(cvmx_cmd_queue_id_t queue_id,
237*4882a593Smuzhiyun __cvmx_cmd_queue_state_t *qptr)
238*4882a593Smuzhiyun {
239*4882a593Smuzhiyun extern __cvmx_cmd_queue_all_state_t
240*4882a593Smuzhiyun *__cvmx_cmd_queue_state_ptr;
241*4882a593Smuzhiyun int tmp;
242*4882a593Smuzhiyun int my_ticket;
243*4882a593Smuzhiyun prefetch(qptr);
244*4882a593Smuzhiyun asm volatile (
245*4882a593Smuzhiyun ".set push\n"
246*4882a593Smuzhiyun ".set noreorder\n"
247*4882a593Smuzhiyun "1:\n"
248*4882a593Smuzhiyun /* Atomic add one to ticket_ptr */
249*4882a593Smuzhiyun "ll %[my_ticket], %[ticket_ptr]\n"
250*4882a593Smuzhiyun /* and store the original value */
251*4882a593Smuzhiyun "li %[ticket], 1\n"
252*4882a593Smuzhiyun /* in my_ticket */
253*4882a593Smuzhiyun "baddu %[ticket], %[my_ticket]\n"
254*4882a593Smuzhiyun "sc %[ticket], %[ticket_ptr]\n"
255*4882a593Smuzhiyun "beqz %[ticket], 1b\n"
256*4882a593Smuzhiyun " nop\n"
257*4882a593Smuzhiyun /* Load the current now_serving ticket */
258*4882a593Smuzhiyun "lbu %[ticket], %[now_serving]\n"
259*4882a593Smuzhiyun "2:\n"
260*4882a593Smuzhiyun /* Jump out if now_serving == my_ticket */
261*4882a593Smuzhiyun "beq %[ticket], %[my_ticket], 4f\n"
262*4882a593Smuzhiyun /* Find out how many tickets are in front of me */
263*4882a593Smuzhiyun " subu %[ticket], %[my_ticket], %[ticket]\n"
264*4882a593Smuzhiyun /* Use tickets in front of me minus one to delay */
265*4882a593Smuzhiyun "subu %[ticket], 1\n"
266*4882a593Smuzhiyun /* Delay will be ((tickets in front)-1)*32 loops */
267*4882a593Smuzhiyun "cins %[ticket], %[ticket], 5, 7\n"
268*4882a593Smuzhiyun "3:\n"
269*4882a593Smuzhiyun /* Loop here until our ticket might be up */
270*4882a593Smuzhiyun "bnez %[ticket], 3b\n"
271*4882a593Smuzhiyun " subu %[ticket], 1\n"
272*4882a593Smuzhiyun /* Jump back up to check out ticket again */
273*4882a593Smuzhiyun "b 2b\n"
274*4882a593Smuzhiyun /* Load the current now_serving ticket */
275*4882a593Smuzhiyun " lbu %[ticket], %[now_serving]\n"
276*4882a593Smuzhiyun "4:\n"
277*4882a593Smuzhiyun ".set pop\n" :
278*4882a593Smuzhiyun [ticket_ptr] "=" GCC_OFF_SMALL_ASM()(__cvmx_cmd_queue_state_ptr->ticket[__cvmx_cmd_queue_get_index(queue_id)]),
279*4882a593Smuzhiyun [now_serving] "=m"(qptr->now_serving), [ticket] "=r"(tmp),
280*4882a593Smuzhiyun [my_ticket] "=r"(my_ticket)
281*4882a593Smuzhiyun );
282*4882a593Smuzhiyun }
283*4882a593Smuzhiyun
284*4882a593Smuzhiyun /**
285*4882a593Smuzhiyun * Unlock the queue, flushing all writes.
286*4882a593Smuzhiyun *
287*4882a593Smuzhiyun * @qptr: Queue to unlock
288*4882a593Smuzhiyun */
__cvmx_cmd_queue_unlock(__cvmx_cmd_queue_state_t * qptr)289*4882a593Smuzhiyun static inline void __cvmx_cmd_queue_unlock(__cvmx_cmd_queue_state_t *qptr)
290*4882a593Smuzhiyun {
291*4882a593Smuzhiyun qptr->now_serving++;
292*4882a593Smuzhiyun CVMX_SYNCWS;
293*4882a593Smuzhiyun }
294*4882a593Smuzhiyun
295*4882a593Smuzhiyun /**
296*4882a593Smuzhiyun * Get the queue state structure for the given queue id
297*4882a593Smuzhiyun *
298*4882a593Smuzhiyun * @queue_id: Queue id to get
299*4882a593Smuzhiyun *
300*4882a593Smuzhiyun * Returns Queue structure or NULL on failure
301*4882a593Smuzhiyun */
302*4882a593Smuzhiyun static inline __cvmx_cmd_queue_state_t
__cvmx_cmd_queue_get_state(cvmx_cmd_queue_id_t queue_id)303*4882a593Smuzhiyun *__cvmx_cmd_queue_get_state(cvmx_cmd_queue_id_t queue_id)
304*4882a593Smuzhiyun {
305*4882a593Smuzhiyun extern __cvmx_cmd_queue_all_state_t
306*4882a593Smuzhiyun *__cvmx_cmd_queue_state_ptr;
307*4882a593Smuzhiyun return &__cvmx_cmd_queue_state_ptr->
308*4882a593Smuzhiyun state[__cvmx_cmd_queue_get_index(queue_id)];
309*4882a593Smuzhiyun }
310*4882a593Smuzhiyun
311*4882a593Smuzhiyun /**
312*4882a593Smuzhiyun * Write an arbitrary number of command words to a command queue.
313*4882a593Smuzhiyun * This is a generic function; the fixed number of command word
314*4882a593Smuzhiyun * functions yield higher performance.
315*4882a593Smuzhiyun *
316*4882a593Smuzhiyun * @queue_id: Hardware command queue to write to
317*4882a593Smuzhiyun * @use_locking:
318*4882a593Smuzhiyun * Use internal locking to ensure exclusive access for queue
319*4882a593Smuzhiyun * updates. If you don't use this locking you must ensure
320*4882a593Smuzhiyun * exclusivity some other way. Locking is strongly recommended.
321*4882a593Smuzhiyun * @cmd_count: Number of command words to write
322*4882a593Smuzhiyun * @cmds: Array of commands to write
323*4882a593Smuzhiyun *
324*4882a593Smuzhiyun * Returns CVMX_CMD_QUEUE_SUCCESS or a failure code
325*4882a593Smuzhiyun */
cvmx_cmd_queue_write(cvmx_cmd_queue_id_t queue_id,int use_locking,int cmd_count,uint64_t * cmds)326*4882a593Smuzhiyun static inline cvmx_cmd_queue_result_t cvmx_cmd_queue_write(cvmx_cmd_queue_id_t
327*4882a593Smuzhiyun queue_id,
328*4882a593Smuzhiyun int use_locking,
329*4882a593Smuzhiyun int cmd_count,
330*4882a593Smuzhiyun uint64_t *cmds)
331*4882a593Smuzhiyun {
332*4882a593Smuzhiyun __cvmx_cmd_queue_state_t *qptr = __cvmx_cmd_queue_get_state(queue_id);
333*4882a593Smuzhiyun
334*4882a593Smuzhiyun /* Make sure nobody else is updating the same queue */
335*4882a593Smuzhiyun if (likely(use_locking))
336*4882a593Smuzhiyun __cvmx_cmd_queue_lock(queue_id, qptr);
337*4882a593Smuzhiyun
338*4882a593Smuzhiyun /*
339*4882a593Smuzhiyun * If a max queue length was specified then make sure we don't
340*4882a593Smuzhiyun * exceed it. If any part of the command would be below the
341*4882a593Smuzhiyun * limit we allow it.
342*4882a593Smuzhiyun */
343*4882a593Smuzhiyun if (CVMX_CMD_QUEUE_ENABLE_MAX_DEPTH && unlikely(qptr->max_depth)) {
344*4882a593Smuzhiyun if (unlikely
345*4882a593Smuzhiyun (cvmx_cmd_queue_length(queue_id) > (int)qptr->max_depth)) {
346*4882a593Smuzhiyun if (likely(use_locking))
347*4882a593Smuzhiyun __cvmx_cmd_queue_unlock(qptr);
348*4882a593Smuzhiyun return CVMX_CMD_QUEUE_FULL;
349*4882a593Smuzhiyun }
350*4882a593Smuzhiyun }
351*4882a593Smuzhiyun
352*4882a593Smuzhiyun /*
353*4882a593Smuzhiyun * Normally there is plenty of room in the current buffer for
354*4882a593Smuzhiyun * the command.
355*4882a593Smuzhiyun */
356*4882a593Smuzhiyun if (likely(qptr->index + cmd_count < qptr->pool_size_m1)) {
357*4882a593Smuzhiyun uint64_t *ptr =
358*4882a593Smuzhiyun (uint64_t *) cvmx_phys_to_ptr((uint64_t) qptr->
359*4882a593Smuzhiyun base_ptr_div128 << 7);
360*4882a593Smuzhiyun ptr += qptr->index;
361*4882a593Smuzhiyun qptr->index += cmd_count;
362*4882a593Smuzhiyun while (cmd_count--)
363*4882a593Smuzhiyun *ptr++ = *cmds++;
364*4882a593Smuzhiyun } else {
365*4882a593Smuzhiyun uint64_t *ptr;
366*4882a593Smuzhiyun int count;
367*4882a593Smuzhiyun /*
368*4882a593Smuzhiyun * We need a new command buffer. Fail if there isn't
369*4882a593Smuzhiyun * one available.
370*4882a593Smuzhiyun */
371*4882a593Smuzhiyun uint64_t *new_buffer =
372*4882a593Smuzhiyun (uint64_t *) cvmx_fpa_alloc(qptr->fpa_pool);
373*4882a593Smuzhiyun if (unlikely(new_buffer == NULL)) {
374*4882a593Smuzhiyun if (likely(use_locking))
375*4882a593Smuzhiyun __cvmx_cmd_queue_unlock(qptr);
376*4882a593Smuzhiyun return CVMX_CMD_QUEUE_NO_MEMORY;
377*4882a593Smuzhiyun }
378*4882a593Smuzhiyun ptr =
379*4882a593Smuzhiyun (uint64_t *) cvmx_phys_to_ptr((uint64_t) qptr->
380*4882a593Smuzhiyun base_ptr_div128 << 7);
381*4882a593Smuzhiyun /*
382*4882a593Smuzhiyun * Figure out how many command words will fit in this
383*4882a593Smuzhiyun * buffer. One location will be needed for the next
384*4882a593Smuzhiyun * buffer pointer.
385*4882a593Smuzhiyun */
386*4882a593Smuzhiyun count = qptr->pool_size_m1 - qptr->index;
387*4882a593Smuzhiyun ptr += qptr->index;
388*4882a593Smuzhiyun cmd_count -= count;
389*4882a593Smuzhiyun while (count--)
390*4882a593Smuzhiyun *ptr++ = *cmds++;
391*4882a593Smuzhiyun *ptr = cvmx_ptr_to_phys(new_buffer);
392*4882a593Smuzhiyun /*
393*4882a593Smuzhiyun * The current buffer is full and has a link to the
394*4882a593Smuzhiyun * next buffer. Time to write the rest of the commands
395*4882a593Smuzhiyun * into the new buffer.
396*4882a593Smuzhiyun */
397*4882a593Smuzhiyun qptr->base_ptr_div128 = *ptr >> 7;
398*4882a593Smuzhiyun qptr->index = cmd_count;
399*4882a593Smuzhiyun ptr = new_buffer;
400*4882a593Smuzhiyun while (cmd_count--)
401*4882a593Smuzhiyun *ptr++ = *cmds++;
402*4882a593Smuzhiyun }
403*4882a593Smuzhiyun
404*4882a593Smuzhiyun /* All updates are complete. Release the lock and return */
405*4882a593Smuzhiyun if (likely(use_locking))
406*4882a593Smuzhiyun __cvmx_cmd_queue_unlock(qptr);
407*4882a593Smuzhiyun return CVMX_CMD_QUEUE_SUCCESS;
408*4882a593Smuzhiyun }
409*4882a593Smuzhiyun
410*4882a593Smuzhiyun /**
411*4882a593Smuzhiyun * Simple function to write two command words to a command
412*4882a593Smuzhiyun * queue.
413*4882a593Smuzhiyun *
414*4882a593Smuzhiyun * @queue_id: Hardware command queue to write to
415*4882a593Smuzhiyun * @use_locking:
416*4882a593Smuzhiyun * Use internal locking to ensure exclusive access for queue
417*4882a593Smuzhiyun * updates. If you don't use this locking you must ensure
418*4882a593Smuzhiyun * exclusivity some other way. Locking is strongly recommended.
419*4882a593Smuzhiyun * @cmd1: Command
420*4882a593Smuzhiyun * @cmd2: Command
421*4882a593Smuzhiyun *
422*4882a593Smuzhiyun * Returns CVMX_CMD_QUEUE_SUCCESS or a failure code
423*4882a593Smuzhiyun */
cvmx_cmd_queue_write2(cvmx_cmd_queue_id_t queue_id,int use_locking,uint64_t cmd1,uint64_t cmd2)424*4882a593Smuzhiyun static inline cvmx_cmd_queue_result_t cvmx_cmd_queue_write2(cvmx_cmd_queue_id_t
425*4882a593Smuzhiyun queue_id,
426*4882a593Smuzhiyun int use_locking,
427*4882a593Smuzhiyun uint64_t cmd1,
428*4882a593Smuzhiyun uint64_t cmd2)
429*4882a593Smuzhiyun {
430*4882a593Smuzhiyun __cvmx_cmd_queue_state_t *qptr = __cvmx_cmd_queue_get_state(queue_id);
431*4882a593Smuzhiyun
432*4882a593Smuzhiyun /* Make sure nobody else is updating the same queue */
433*4882a593Smuzhiyun if (likely(use_locking))
434*4882a593Smuzhiyun __cvmx_cmd_queue_lock(queue_id, qptr);
435*4882a593Smuzhiyun
436*4882a593Smuzhiyun /*
437*4882a593Smuzhiyun * If a max queue length was specified then make sure we don't
438*4882a593Smuzhiyun * exceed it. If any part of the command would be below the
439*4882a593Smuzhiyun * limit we allow it.
440*4882a593Smuzhiyun */
441*4882a593Smuzhiyun if (CVMX_CMD_QUEUE_ENABLE_MAX_DEPTH && unlikely(qptr->max_depth)) {
442*4882a593Smuzhiyun if (unlikely
443*4882a593Smuzhiyun (cvmx_cmd_queue_length(queue_id) > (int)qptr->max_depth)) {
444*4882a593Smuzhiyun if (likely(use_locking))
445*4882a593Smuzhiyun __cvmx_cmd_queue_unlock(qptr);
446*4882a593Smuzhiyun return CVMX_CMD_QUEUE_FULL;
447*4882a593Smuzhiyun }
448*4882a593Smuzhiyun }
449*4882a593Smuzhiyun
450*4882a593Smuzhiyun /*
451*4882a593Smuzhiyun * Normally there is plenty of room in the current buffer for
452*4882a593Smuzhiyun * the command.
453*4882a593Smuzhiyun */
454*4882a593Smuzhiyun if (likely(qptr->index + 2 < qptr->pool_size_m1)) {
455*4882a593Smuzhiyun uint64_t *ptr =
456*4882a593Smuzhiyun (uint64_t *) cvmx_phys_to_ptr((uint64_t) qptr->
457*4882a593Smuzhiyun base_ptr_div128 << 7);
458*4882a593Smuzhiyun ptr += qptr->index;
459*4882a593Smuzhiyun qptr->index += 2;
460*4882a593Smuzhiyun ptr[0] = cmd1;
461*4882a593Smuzhiyun ptr[1] = cmd2;
462*4882a593Smuzhiyun } else {
463*4882a593Smuzhiyun uint64_t *ptr;
464*4882a593Smuzhiyun /*
465*4882a593Smuzhiyun * Figure out how many command words will fit in this
466*4882a593Smuzhiyun * buffer. One location will be needed for the next
467*4882a593Smuzhiyun * buffer pointer.
468*4882a593Smuzhiyun */
469*4882a593Smuzhiyun int count = qptr->pool_size_m1 - qptr->index;
470*4882a593Smuzhiyun /*
471*4882a593Smuzhiyun * We need a new command buffer. Fail if there isn't
472*4882a593Smuzhiyun * one available.
473*4882a593Smuzhiyun */
474*4882a593Smuzhiyun uint64_t *new_buffer =
475*4882a593Smuzhiyun (uint64_t *) cvmx_fpa_alloc(qptr->fpa_pool);
476*4882a593Smuzhiyun if (unlikely(new_buffer == NULL)) {
477*4882a593Smuzhiyun if (likely(use_locking))
478*4882a593Smuzhiyun __cvmx_cmd_queue_unlock(qptr);
479*4882a593Smuzhiyun return CVMX_CMD_QUEUE_NO_MEMORY;
480*4882a593Smuzhiyun }
481*4882a593Smuzhiyun count--;
482*4882a593Smuzhiyun ptr =
483*4882a593Smuzhiyun (uint64_t *) cvmx_phys_to_ptr((uint64_t) qptr->
484*4882a593Smuzhiyun base_ptr_div128 << 7);
485*4882a593Smuzhiyun ptr += qptr->index;
486*4882a593Smuzhiyun *ptr++ = cmd1;
487*4882a593Smuzhiyun if (likely(count))
488*4882a593Smuzhiyun *ptr++ = cmd2;
489*4882a593Smuzhiyun *ptr = cvmx_ptr_to_phys(new_buffer);
490*4882a593Smuzhiyun /*
491*4882a593Smuzhiyun * The current buffer is full and has a link to the
492*4882a593Smuzhiyun * next buffer. Time to write the rest of the commands
493*4882a593Smuzhiyun * into the new buffer.
494*4882a593Smuzhiyun */
495*4882a593Smuzhiyun qptr->base_ptr_div128 = *ptr >> 7;
496*4882a593Smuzhiyun qptr->index = 0;
497*4882a593Smuzhiyun if (unlikely(count == 0)) {
498*4882a593Smuzhiyun qptr->index = 1;
499*4882a593Smuzhiyun new_buffer[0] = cmd2;
500*4882a593Smuzhiyun }
501*4882a593Smuzhiyun }
502*4882a593Smuzhiyun
503*4882a593Smuzhiyun /* All updates are complete. Release the lock and return */
504*4882a593Smuzhiyun if (likely(use_locking))
505*4882a593Smuzhiyun __cvmx_cmd_queue_unlock(qptr);
506*4882a593Smuzhiyun return CVMX_CMD_QUEUE_SUCCESS;
507*4882a593Smuzhiyun }
508*4882a593Smuzhiyun
509*4882a593Smuzhiyun /**
510*4882a593Smuzhiyun * Simple function to write three command words to a command
511*4882a593Smuzhiyun * queue.
512*4882a593Smuzhiyun *
513*4882a593Smuzhiyun * @queue_id: Hardware command queue to write to
514*4882a593Smuzhiyun * @use_locking:
515*4882a593Smuzhiyun * Use internal locking to ensure exclusive access for queue
516*4882a593Smuzhiyun * updates. If you don't use this locking you must ensure
517*4882a593Smuzhiyun * exclusivity some other way. Locking is strongly recommended.
518*4882a593Smuzhiyun * @cmd1: Command
519*4882a593Smuzhiyun * @cmd2: Command
520*4882a593Smuzhiyun * @cmd3: Command
521*4882a593Smuzhiyun *
522*4882a593Smuzhiyun * Returns CVMX_CMD_QUEUE_SUCCESS or a failure code
523*4882a593Smuzhiyun */
cvmx_cmd_queue_write3(cvmx_cmd_queue_id_t queue_id,int use_locking,uint64_t cmd1,uint64_t cmd2,uint64_t cmd3)524*4882a593Smuzhiyun static inline cvmx_cmd_queue_result_t cvmx_cmd_queue_write3(cvmx_cmd_queue_id_t
525*4882a593Smuzhiyun queue_id,
526*4882a593Smuzhiyun int use_locking,
527*4882a593Smuzhiyun uint64_t cmd1,
528*4882a593Smuzhiyun uint64_t cmd2,
529*4882a593Smuzhiyun uint64_t cmd3)
530*4882a593Smuzhiyun {
531*4882a593Smuzhiyun __cvmx_cmd_queue_state_t *qptr = __cvmx_cmd_queue_get_state(queue_id);
532*4882a593Smuzhiyun
533*4882a593Smuzhiyun /* Make sure nobody else is updating the same queue */
534*4882a593Smuzhiyun if (likely(use_locking))
535*4882a593Smuzhiyun __cvmx_cmd_queue_lock(queue_id, qptr);
536*4882a593Smuzhiyun
537*4882a593Smuzhiyun /*
538*4882a593Smuzhiyun * If a max queue length was specified then make sure we don't
539*4882a593Smuzhiyun * exceed it. If any part of the command would be below the
540*4882a593Smuzhiyun * limit we allow it.
541*4882a593Smuzhiyun */
542*4882a593Smuzhiyun if (CVMX_CMD_QUEUE_ENABLE_MAX_DEPTH && unlikely(qptr->max_depth)) {
543*4882a593Smuzhiyun if (unlikely
544*4882a593Smuzhiyun (cvmx_cmd_queue_length(queue_id) > (int)qptr->max_depth)) {
545*4882a593Smuzhiyun if (likely(use_locking))
546*4882a593Smuzhiyun __cvmx_cmd_queue_unlock(qptr);
547*4882a593Smuzhiyun return CVMX_CMD_QUEUE_FULL;
548*4882a593Smuzhiyun }
549*4882a593Smuzhiyun }
550*4882a593Smuzhiyun
551*4882a593Smuzhiyun /*
552*4882a593Smuzhiyun * Normally there is plenty of room in the current buffer for
553*4882a593Smuzhiyun * the command.
554*4882a593Smuzhiyun */
555*4882a593Smuzhiyun if (likely(qptr->index + 3 < qptr->pool_size_m1)) {
556*4882a593Smuzhiyun uint64_t *ptr =
557*4882a593Smuzhiyun (uint64_t *) cvmx_phys_to_ptr((uint64_t) qptr->
558*4882a593Smuzhiyun base_ptr_div128 << 7);
559*4882a593Smuzhiyun ptr += qptr->index;
560*4882a593Smuzhiyun qptr->index += 3;
561*4882a593Smuzhiyun ptr[0] = cmd1;
562*4882a593Smuzhiyun ptr[1] = cmd2;
563*4882a593Smuzhiyun ptr[2] = cmd3;
564*4882a593Smuzhiyun } else {
565*4882a593Smuzhiyun uint64_t *ptr;
566*4882a593Smuzhiyun /*
567*4882a593Smuzhiyun * Figure out how many command words will fit in this
568*4882a593Smuzhiyun * buffer. One location will be needed for the next
569*4882a593Smuzhiyun * buffer pointer
570*4882a593Smuzhiyun */
571*4882a593Smuzhiyun int count = qptr->pool_size_m1 - qptr->index;
572*4882a593Smuzhiyun /*
573*4882a593Smuzhiyun * We need a new command buffer. Fail if there isn't
574*4882a593Smuzhiyun * one available
575*4882a593Smuzhiyun */
576*4882a593Smuzhiyun uint64_t *new_buffer =
577*4882a593Smuzhiyun (uint64_t *) cvmx_fpa_alloc(qptr->fpa_pool);
578*4882a593Smuzhiyun if (unlikely(new_buffer == NULL)) {
579*4882a593Smuzhiyun if (likely(use_locking))
580*4882a593Smuzhiyun __cvmx_cmd_queue_unlock(qptr);
581*4882a593Smuzhiyun return CVMX_CMD_QUEUE_NO_MEMORY;
582*4882a593Smuzhiyun }
583*4882a593Smuzhiyun count--;
584*4882a593Smuzhiyun ptr =
585*4882a593Smuzhiyun (uint64_t *) cvmx_phys_to_ptr((uint64_t) qptr->
586*4882a593Smuzhiyun base_ptr_div128 << 7);
587*4882a593Smuzhiyun ptr += qptr->index;
588*4882a593Smuzhiyun *ptr++ = cmd1;
589*4882a593Smuzhiyun if (count) {
590*4882a593Smuzhiyun *ptr++ = cmd2;
591*4882a593Smuzhiyun if (count > 1)
592*4882a593Smuzhiyun *ptr++ = cmd3;
593*4882a593Smuzhiyun }
594*4882a593Smuzhiyun *ptr = cvmx_ptr_to_phys(new_buffer);
595*4882a593Smuzhiyun /*
596*4882a593Smuzhiyun * The current buffer is full and has a link to the
597*4882a593Smuzhiyun * next buffer. Time to write the rest of the commands
598*4882a593Smuzhiyun * into the new buffer.
599*4882a593Smuzhiyun */
600*4882a593Smuzhiyun qptr->base_ptr_div128 = *ptr >> 7;
601*4882a593Smuzhiyun qptr->index = 0;
602*4882a593Smuzhiyun ptr = new_buffer;
603*4882a593Smuzhiyun if (count == 0) {
604*4882a593Smuzhiyun *ptr++ = cmd2;
605*4882a593Smuzhiyun qptr->index++;
606*4882a593Smuzhiyun }
607*4882a593Smuzhiyun if (count < 2) {
608*4882a593Smuzhiyun *ptr++ = cmd3;
609*4882a593Smuzhiyun qptr->index++;
610*4882a593Smuzhiyun }
611*4882a593Smuzhiyun }
612*4882a593Smuzhiyun
613*4882a593Smuzhiyun /* All updates are complete. Release the lock and return */
614*4882a593Smuzhiyun if (likely(use_locking))
615*4882a593Smuzhiyun __cvmx_cmd_queue_unlock(qptr);
616*4882a593Smuzhiyun return CVMX_CMD_QUEUE_SUCCESS;
617*4882a593Smuzhiyun }
618*4882a593Smuzhiyun
619*4882a593Smuzhiyun #endif /* __CVMX_CMD_QUEUE_H__ */
620