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