1 /***********************license start***************
2 * Copyright (c) 2003-2010 Cavium Inc. (support@cavium.com). All rights
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions are
10 * * Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
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.
18 * * Neither the name of Cavium Inc. nor the names of
19 * its contributors may be used to endorse or promote products
20 * derived from this software without specific prior written
23 * This Software, including technical data, may be subject to U.S. export control
24 * laws, including the U.S. Export Administration Act and its associated
25 * regulations, and may be subject to export or import regulations in other
28 * TO THE MAXIMUM EXTENT PERMITTED BY LAW, THE SOFTWARE IS PROVIDED "AS IS"
29 * AND WITH ALL FAULTS AND CAVIUM INC. MAKES NO PROMISES, REPRESENTATIONS OR
30 * WARRANTIES, EITHER EXPRESS, IMPLIED, STATUTORY, OR OTHERWISE, WITH RESPECT TO
31 * THE SOFTWARE, INCLUDING ITS CONDITION, ITS CONFORMITY TO ANY REPRESENTATION OR
32 * DESCRIPTION, OR THE EXISTENCE OF ANY LATENT OR PATENT DEFECTS, AND CAVIUM
33 * SPECIFICALLY DISCLAIMS ALL IMPLIED (IF ANY) WARRANTIES OF TITLE,
34 * MERCHANTABILITY, NONINFRINGEMENT, FITNESS FOR A PARTICULAR PURPOSE, LACK OF
35 * VIRUSES, ACCURACY OR COMPLETENESS, QUIET ENJOYMENT, QUIET POSSESSION OR
36 * CORRESPONDENCE TO DESCRIPTION. THE ENTIRE RISK ARISING OUT OF USE OR
37 * PERFORMANCE OF THE SOFTWARE LIES WITH YOU.
38 ***********************license end**************************************/
49 * Support functions for managing command queues used for
50 * various hardware blocks.
52 * The common command queue infrastructure abstracts out the
53 * software necessary for adding to Octeon's chained queue
54 * structures. These structures are used for commands to the
55 * PKO, ZIP, DFA, RAID, and DMA engine blocks. Although each
56 * hardware unit takes commands and CSRs of different types,
57 * they all use basic linked command buffers to store the
58 * pending request. In general, users of the CVMX API don't
59 * call cvmx-cmd-queue functions directly. Instead the hardware
60 * unit specific wrapper should be used. The wrappers perform
61 * unit specific validation and CSR writes to submit the
64 * Even though most software will never directly interact with
65 * cvmx-cmd-queue, knowledge of its internal workings can help
66 * in diagnosing performance problems and help with debugging.
68 * Command queue pointers are stored in a global named block
69 * called "cvmx_cmd_queues". Except for the PKO queues, each
70 * hardware queue is stored in its own cache line to reduce SMP
71 * contention on spin locks. The PKO queues are stored such that
72 * every 16th queue is next to each other in memory. This scheme
73 * allows for queues being in separate cache lines when there
74 * are low number of queues per port. With 16 queues per port,
75 * the first queue for each port is in the same cache area. The
76 * second queues for each port are in another area, etc. This
77 * allows software to implement very efficient lockless PKO with
78 * 16 queues per port using a minimum of cache lines per core.
79 * All queues for a given core will be isolated in the same
82 * In addition to the memory pointer layout, cvmx-cmd-queue
83 * provides an optimized fair ll/sc locking mechanism for the
84 * queues. The lock uses a "ticket / now serving" model to
85 * maintain fair order on contended locks. In addition, it uses
86 * predicted locking time to limit cache contention. When a core
87 * know it must wait in line for a lock, it spins on the
88 * internal cycle counter to completely eliminate any causes of
91 * <hr> $Revision: 70030 $ <hr>
94 #ifndef __CVMX_CMD_QUEUE_H__
95 #define __CVMX_CMD_QUEUE_H__
97 #if !defined(CVMX_BUILD_FOR_LINUX_KERNEL) && !defined(CVMX_BUILD_FOR_FREEBSD_KERNEL)
98 #include "executive-config.h"
99 #include "cvmx-config.h"
102 #include "cvmx-fpa.h"
109 * By default we disable the max depth support. Most programs
110 * don't use it and it slows down the command queue processing
113 #ifndef CVMX_CMD_QUEUE_ENABLE_MAX_DEPTH
114 #define CVMX_CMD_QUEUE_ENABLE_MAX_DEPTH 0
118 * Enumeration representing all hardware blocks that use command
119 * queues. Each hardware block has up to 65536 sub identifiers for
120 * multiple command queues. Not all chips support all hardware
125 CVMX_CMD_QUEUE_PKO_BASE = 0x00000,
126 #define CVMX_CMD_QUEUE_PKO(queue) ((cvmx_cmd_queue_id_t)(CVMX_CMD_QUEUE_PKO_BASE + (0xffff&(queue))))
127 CVMX_CMD_QUEUE_ZIP = 0x10000,
128 #define CVMX_CMD_QUEUE_ZIP_QUE(queue) ((cvmx_cmd_queue_id_t)(CVMX_CMD_QUEUE_ZIP + (0xffff&(queue))))
129 CVMX_CMD_QUEUE_DFA = 0x20000,
130 CVMX_CMD_QUEUE_RAID = 0x30000,
131 CVMX_CMD_QUEUE_DMA_BASE = 0x40000,
132 #define CVMX_CMD_QUEUE_DMA(queue) ((cvmx_cmd_queue_id_t)(CVMX_CMD_QUEUE_DMA_BASE + (0xffff&(queue))))
133 CVMX_CMD_QUEUE_END = 0x50000,
134 } cvmx_cmd_queue_id_t;
137 * Command write operations can fail if the command queue needs
138 * a new buffer and the associated FPA pool is empty. It can also
139 * fail if the number of queued command words reaches the maximum
140 * set at initialization.
144 CVMX_CMD_QUEUE_SUCCESS = 0,
145 CVMX_CMD_QUEUE_NO_MEMORY = -1,
146 CVMX_CMD_QUEUE_FULL = -2,
147 CVMX_CMD_QUEUE_INVALID_PARAM = -3,
148 CVMX_CMD_QUEUE_ALREADY_SETUP = -4,
149 } cvmx_cmd_queue_result_t;
153 uint8_t now_serving; /**< You have lock when this is your ticket */
154 uint64_t unused1 : 24;
155 uint32_t max_depth; /**< Maximum outstanding command words */
156 uint64_t fpa_pool : 3; /**< FPA pool buffers come from */
157 uint64_t base_ptr_div128: 29; /**< Top of command buffer pointer shifted 7 */
158 uint64_t unused2 : 6;
159 uint64_t pool_size_m1 : 13; /**< FPA buffer size in 64bit words minus 1 */
160 uint64_t index : 13; /**< Number of commands already used in buffer */
161 } __cvmx_cmd_queue_state_t;
164 * This structure contains the global state of all command queues.
165 * It is stored in a bootmem named block and shared by all
166 * applications running on Octeon. Tickets are stored in a different
167 * cache line that queue information to reduce the contention on the
168 * ll/sc used to get a ticket. If this is not the case, the update
169 * of queue state causes the ll/sc to fail quite often.
173 uint64_t ticket[(CVMX_CMD_QUEUE_END>>16) * 256];
174 __cvmx_cmd_queue_state_t state[(CVMX_CMD_QUEUE_END>>16) * 256];
175 } __cvmx_cmd_queue_all_state_t;
177 extern CVMX_SHARED __cvmx_cmd_queue_all_state_t *__cvmx_cmd_queue_state_ptr;
180 * Initialize a command queue for use. The initial FPA buffer is
181 * allocated and the hardware unit is configured to point to the
184 * @param queue_id Hardware command queue to initialize.
185 * @param max_depth Maximum outstanding commands that can be queued.
186 * @param fpa_pool FPA pool the command queues should come from.
187 * @param pool_size Size of each buffer in the FPA pool (bytes)
189 * @return CVMX_CMD_QUEUE_SUCCESS or a failure code
191 cvmx_cmd_queue_result_t cvmx_cmd_queue_initialize(cvmx_cmd_queue_id_t queue_id, int max_depth, int fpa_pool, int pool_size);
194 * Shutdown a queue a free it's command buffers to the FPA. The
195 * hardware connected to the queue must be stopped before this
196 * function is called.
198 * @param queue_id Queue to shutdown
200 * @return CVMX_CMD_QUEUE_SUCCESS or a failure code
202 cvmx_cmd_queue_result_t cvmx_cmd_queue_shutdown(cvmx_cmd_queue_id_t queue_id);
205 * Return the number of command words pending in the queue. This
206 * function may be relatively slow for some hardware units.
208 * @param queue_id Hardware command queue to query
210 * @return Number of outstanding commands
212 int cvmx_cmd_queue_length(cvmx_cmd_queue_id_t queue_id);
215 * Return the command buffer to be written to. The purpose of this
216 * function is to allow CVMX routine access to the low level buffer
217 * for initial hardware setup. User applications should not call this
220 * @param queue_id Command queue to query
222 * @return Command buffer or NULL on failure
224 void *cvmx_cmd_queue_buffer(cvmx_cmd_queue_id_t queue_id);
228 * Get the index into the state arrays for the supplied queue id.
230 * @param queue_id Queue ID to get an index for
232 * @return Index into the state arrays
234 static inline int __cvmx_cmd_queue_get_index(cvmx_cmd_queue_id_t queue_id)
236 /* Warning: This code currently only works with devices that have 256 queues
237 or less. Devices with more than 16 queues are laid out in memory to allow
238 cores quick access to every 16th queue. This reduces cache thrashing
239 when you are running 16 queues per port to support lockless operation */
240 int unit = queue_id>>16;
241 int q = (queue_id >> 4) & 0xf;
242 int core = queue_id & 0xf;
243 return unit*256 + core*16 + q;
249 * Lock the supplied queue so nobody else is updating it at the same
252 * @param queue_id Queue ID to lock
253 * @param qptr Pointer to the queue's global state
255 static inline void __cvmx_cmd_queue_lock(cvmx_cmd_queue_id_t queue_id, __cvmx_cmd_queue_state_t *qptr)
259 CVMX_PREFETCH(qptr, 0);
264 "ll %[my_ticket], %[ticket_ptr]\n" /* Atomic add one to ticket_ptr */
265 "li %[ticket], 1\n" /* and store the original value */
266 "baddu %[ticket], %[my_ticket]\n" /* in my_ticket */
267 "sc %[ticket], %[ticket_ptr]\n"
268 "beqz %[ticket], 1b\n"
270 "lbu %[ticket], %[now_serving]\n" /* Load the current now_serving ticket */
272 "beq %[ticket], %[my_ticket], 4f\n" /* Jump out if now_serving == my_ticket */
273 " subu %[ticket], %[my_ticket], %[ticket]\n" /* Find out how many tickets are in front of me */
274 "subu %[ticket], 1\n" /* Use tickets in front of me minus one to delay */
275 "cins %[ticket], %[ticket], 5, 7\n" /* Delay will be ((tickets in front)-1)*32 loops */
277 "bnez %[ticket], 3b\n" /* Loop here until our ticket might be up */
278 " subu %[ticket], 1\n"
279 "b 2b\n" /* Jump back up to check out ticket again */
280 " lbu %[ticket], %[now_serving]\n" /* Load the current now_serving ticket */
283 : [ticket_ptr] "=m" (__cvmx_cmd_queue_state_ptr->ticket[__cvmx_cmd_queue_get_index(queue_id)]),
284 [now_serving] "=m" (qptr->now_serving),
286 [my_ticket] "=r" (my_ticket)
293 * Unlock the queue, flushing all writes.
295 * @param qptr Queue to unlock
297 static inline void __cvmx_cmd_queue_unlock(__cvmx_cmd_queue_state_t *qptr)
301 ns = qptr->now_serving + 1;
302 CVMX_SYNCWS; /* Order queue manipulation with respect to the unlock. */
303 qptr->now_serving = ns;
304 CVMX_SYNCWS; /* nudge out the unlock. */
310 * Get the queue state structure for the given queue id
312 * @param queue_id Queue id to get
314 * @return Queue structure or NULL on failure
316 static inline __cvmx_cmd_queue_state_t *__cvmx_cmd_queue_get_state(cvmx_cmd_queue_id_t queue_id)
318 if (CVMX_ENABLE_PARAMETER_CHECKING)
320 if (cvmx_unlikely(queue_id >= CVMX_CMD_QUEUE_END))
322 if (cvmx_unlikely((queue_id & 0xffff) >= 256))
325 return &__cvmx_cmd_queue_state_ptr->state[__cvmx_cmd_queue_get_index(queue_id)];
330 * Write an arbitrary number of command words to a command queue.
331 * This is a generic function; the fixed number of command word
332 * functions yield higher performance.
334 * @param queue_id Hardware command queue to write to
336 * Use internal locking to ensure exclusive access for queue
337 * updates. If you don't use this locking you must ensure
338 * exclusivity some other way. Locking is strongly recommended.
339 * @param cmd_count Number of command words to write
340 * @param cmds Array of commands to write
342 * @return CVMX_CMD_QUEUE_SUCCESS or a failure code
344 static inline cvmx_cmd_queue_result_t cvmx_cmd_queue_write(cvmx_cmd_queue_id_t queue_id, int use_locking, int cmd_count, uint64_t *cmds)
346 __cvmx_cmd_queue_state_t *qptr = __cvmx_cmd_queue_get_state(queue_id);
348 if (CVMX_ENABLE_PARAMETER_CHECKING)
350 if (cvmx_unlikely(qptr == NULL))
351 return CVMX_CMD_QUEUE_INVALID_PARAM;
352 if (cvmx_unlikely((cmd_count < 1) || (cmd_count > 32)))
353 return CVMX_CMD_QUEUE_INVALID_PARAM;
354 if (cvmx_unlikely(cmds == NULL))
355 return CVMX_CMD_QUEUE_INVALID_PARAM;
358 /* Make sure nobody else is updating the same queue */
359 if (cvmx_likely(use_locking))
360 __cvmx_cmd_queue_lock(queue_id, qptr);
362 /* If a max queue length was specified then make sure we don't
363 exceed it. If any part of the command would be below the limit
365 if (CVMX_CMD_QUEUE_ENABLE_MAX_DEPTH && cvmx_unlikely(qptr->max_depth))
367 if (cvmx_unlikely(cvmx_cmd_queue_length(queue_id) > (int)qptr->max_depth))
369 if (cvmx_likely(use_locking))
370 __cvmx_cmd_queue_unlock(qptr);
371 return CVMX_CMD_QUEUE_FULL;
375 /* Normally there is plenty of room in the current buffer for the command */
376 if (cvmx_likely(qptr->index + cmd_count < qptr->pool_size_m1))
378 uint64_t *ptr = (uint64_t *)cvmx_phys_to_ptr((uint64_t)qptr->base_ptr_div128<<7);
380 qptr->index += cmd_count;
388 /* We need a new command buffer. Fail if there isn't one available */
389 uint64_t *new_buffer = (uint64_t *)cvmx_fpa_alloc(qptr->fpa_pool);
390 if (cvmx_unlikely(new_buffer == NULL))
392 if (cvmx_likely(use_locking))
393 __cvmx_cmd_queue_unlock(qptr);
394 return CVMX_CMD_QUEUE_NO_MEMORY;
396 ptr = (uint64_t *)cvmx_phys_to_ptr((uint64_t)qptr->base_ptr_div128<<7);
397 /* Figure out how many command words will fit in this buffer. One
398 location will be needed for the next buffer pointer */
399 count = qptr->pool_size_m1 - qptr->index;
404 *ptr = cvmx_ptr_to_phys(new_buffer);
405 /* The current buffer is full and has a link to the next buffer. Time
406 to write the rest of the commands into the new buffer */
407 qptr->base_ptr_div128 = *ptr >> 7;
408 qptr->index = cmd_count;
414 /* All updates are complete. Release the lock and return */
415 if (cvmx_likely(use_locking))
416 __cvmx_cmd_queue_unlock(qptr);
417 return CVMX_CMD_QUEUE_SUCCESS;
422 * Simple function to write two command words to a command
425 * @param queue_id Hardware command queue to write to
427 * Use internal locking to ensure exclusive access for queue
428 * updates. If you don't use this locking you must ensure
429 * exclusivity some other way. Locking is strongly recommended.
430 * @param cmd1 Command
431 * @param cmd2 Command
433 * @return CVMX_CMD_QUEUE_SUCCESS or a failure code
435 static inline cvmx_cmd_queue_result_t cvmx_cmd_queue_write2(cvmx_cmd_queue_id_t queue_id, int use_locking, uint64_t cmd1, uint64_t cmd2)
437 __cvmx_cmd_queue_state_t *qptr = __cvmx_cmd_queue_get_state(queue_id);
439 if (CVMX_ENABLE_PARAMETER_CHECKING)
441 if (cvmx_unlikely(qptr == NULL))
442 return CVMX_CMD_QUEUE_INVALID_PARAM;
445 /* Make sure nobody else is updating the same queue */
446 if (cvmx_likely(use_locking))
447 __cvmx_cmd_queue_lock(queue_id, qptr);
449 /* If a max queue length was specified then make sure we don't
450 exceed it. If any part of the command would be below the limit
452 if (CVMX_CMD_QUEUE_ENABLE_MAX_DEPTH && cvmx_unlikely(qptr->max_depth))
454 if (cvmx_unlikely(cvmx_cmd_queue_length(queue_id) > (int)qptr->max_depth))
456 if (cvmx_likely(use_locking))
457 __cvmx_cmd_queue_unlock(qptr);
458 return CVMX_CMD_QUEUE_FULL;
462 /* Normally there is plenty of room in the current buffer for the command */
463 if (cvmx_likely(qptr->index + 2 < qptr->pool_size_m1))
465 uint64_t *ptr = (uint64_t *)cvmx_phys_to_ptr((uint64_t)qptr->base_ptr_div128<<7);
474 /* Figure out how many command words will fit in this buffer. One
475 location will be needed for the next buffer pointer */
476 int count = qptr->pool_size_m1 - qptr->index;
477 /* We need a new command buffer. Fail if there isn't one available */
478 uint64_t *new_buffer = (uint64_t *)cvmx_fpa_alloc(qptr->fpa_pool);
479 if (cvmx_unlikely(new_buffer == NULL))
481 if (cvmx_likely(use_locking))
482 __cvmx_cmd_queue_unlock(qptr);
483 return CVMX_CMD_QUEUE_NO_MEMORY;
486 ptr = (uint64_t *)cvmx_phys_to_ptr((uint64_t)qptr->base_ptr_div128<<7);
489 if (cvmx_likely(count))
491 *ptr = cvmx_ptr_to_phys(new_buffer);
492 /* The current buffer is full and has a link to the next buffer. Time
493 to write the rest of the commands into the new buffer */
494 qptr->base_ptr_div128 = *ptr >> 7;
496 if (cvmx_unlikely(count == 0))
499 new_buffer[0] = cmd2;
503 /* All updates are complete. Release the lock and return */
504 if (cvmx_likely(use_locking))
505 __cvmx_cmd_queue_unlock(qptr);
506 return CVMX_CMD_QUEUE_SUCCESS;
511 * Simple function to write three command words to a command
514 * @param queue_id Hardware command queue to write to
516 * Use internal locking to ensure exclusive access for queue
517 * updates. If you don't use this locking you must ensure
518 * exclusivity some other way. Locking is strongly recommended.
519 * @param cmd1 Command
520 * @param cmd2 Command
521 * @param cmd3 Command
523 * @return CVMX_CMD_QUEUE_SUCCESS or a failure code
525 static inline cvmx_cmd_queue_result_t cvmx_cmd_queue_write3(cvmx_cmd_queue_id_t queue_id, int use_locking, uint64_t cmd1, uint64_t cmd2, uint64_t cmd3)
527 __cvmx_cmd_queue_state_t *qptr = __cvmx_cmd_queue_get_state(queue_id);
529 if (CVMX_ENABLE_PARAMETER_CHECKING)
531 if (cvmx_unlikely(qptr == NULL))
532 return CVMX_CMD_QUEUE_INVALID_PARAM;
535 /* Make sure nobody else is updating the same queue */
536 if (cvmx_likely(use_locking))
537 __cvmx_cmd_queue_lock(queue_id, qptr);
539 /* If a max queue length was specified then make sure we don't
540 exceed it. If any part of the command would be below the limit
542 if (CVMX_CMD_QUEUE_ENABLE_MAX_DEPTH && cvmx_unlikely(qptr->max_depth))
544 if (cvmx_unlikely(cvmx_cmd_queue_length(queue_id) > (int)qptr->max_depth))
546 if (cvmx_likely(use_locking))
547 __cvmx_cmd_queue_unlock(qptr);
548 return CVMX_CMD_QUEUE_FULL;
552 /* Normally there is plenty of room in the current buffer for the command */
553 if (cvmx_likely(qptr->index + 3 < qptr->pool_size_m1))
555 uint64_t *ptr = (uint64_t *)cvmx_phys_to_ptr((uint64_t)qptr->base_ptr_div128<<7);
565 /* Figure out how many command words will fit in this buffer. One
566 location will be needed for the next buffer pointer */
567 int count = qptr->pool_size_m1 - qptr->index;
568 /* We need a new command buffer. Fail if there isn't one available */
569 uint64_t *new_buffer = (uint64_t *)cvmx_fpa_alloc(qptr->fpa_pool);
570 if (cvmx_unlikely(new_buffer == NULL))
572 if (cvmx_likely(use_locking))
573 __cvmx_cmd_queue_unlock(qptr);
574 return CVMX_CMD_QUEUE_NO_MEMORY;
577 ptr = (uint64_t *)cvmx_phys_to_ptr((uint64_t)qptr->base_ptr_div128<<7);
586 *ptr = cvmx_ptr_to_phys(new_buffer);
587 /* The current buffer is full and has a link to the next buffer. Time
588 to write the rest of the commands into the new buffer */
589 qptr->base_ptr_div128 = *ptr >> 7;
604 /* All updates are complete. Release the lock and return */
605 if (cvmx_likely(use_locking))
606 __cvmx_cmd_queue_unlock(qptr);
607 return CVMX_CMD_QUEUE_SUCCESS;
614 #endif /* __CVMX_CMD_QUEUE_H__ */