cvmx-cmd-queue.h revision 256281
140843Smsmith/***********************license start***************
294290Sdcs * Copyright (c) 2003-2010  Cavium Inc. (support@cavium.com). All rights
394290Sdcs * reserved.
440843Smsmith *
594290Sdcs *
694290Sdcs * Redistribution and use in source and binary forms, with or without
794290Sdcs * modification, are permitted provided that the following conditions are
894290Sdcs * met:
994290Sdcs *
1094290Sdcs *   * Redistributions of source code must retain the above copyright
1194290Sdcs *     notice, this list of conditions and the following disclaimer.
1294290Sdcs *
1394290Sdcs *   * Redistributions in binary form must reproduce the above
1494290Sdcs *     copyright notice, this list of conditions and the following
1594290Sdcs *     disclaimer in the documentation and/or other materials provided
1694290Sdcs *     with the distribution.
1794290Sdcs
1894290Sdcs *   * Neither the name of Cavium Inc. nor the names of
1994290Sdcs *     its contributors may be used to endorse or promote products
2094290Sdcs *     derived from this software without specific prior written
2194290Sdcs *     permission.
2294290Sdcs
2394290Sdcs * This Software, including technical data, may be subject to U.S. export  control
2494290Sdcs * laws, including the U.S. Export Administration Act and its  associated
2594290Sdcs * regulations, and may be subject to export or import  regulations in other
2694290Sdcs * countries.
2794290Sdcs
2894290Sdcs * TO THE MAXIMUM EXTENT PERMITTED BY LAW, THE SOFTWARE IS PROVIDED "AS IS"
2994290Sdcs * AND WITH ALL FAULTS AND CAVIUM INC. MAKES NO PROMISES, REPRESENTATIONS OR
3094290Sdcs * WARRANTIES, EITHER EXPRESS, IMPLIED, STATUTORY, OR OTHERWISE, WITH RESPECT TO
3194290Sdcs * THE SOFTWARE, INCLUDING ITS CONDITION, ITS CONFORMITY TO ANY REPRESENTATION OR
3294290Sdcs * DESCRIPTION, OR THE EXISTENCE OF ANY LATENT OR PATENT DEFECTS, AND CAVIUM
3394290Sdcs * SPECIFICALLY DISCLAIMS ALL IMPLIED (IF ANY) WARRANTIES OF TITLE,
3494290Sdcs * MERCHANTABILITY, NONINFRINGEMENT, FITNESS FOR A PARTICULAR PURPOSE, LACK OF
3594290Sdcs * VIRUSES, ACCURACY OR COMPLETENESS, QUIET ENJOYMENT, QUIET POSSESSION OR
3694290Sdcs * CORRESPONDENCE TO DESCRIPTION. THE ENTIRE  RISK ARISING OUT OF USE OR
3794290Sdcs * PERFORMANCE OF THE SOFTWARE LIES WITH YOU.
3894290Sdcs ***********************license end**************************************/
3940843Smsmith
4051786Sdcs
4151786Sdcs
4240843Smsmith
4340843Smsmith
4440843Smsmith
4551786Sdcs
4640843Smsmith/**
4740843Smsmith * @file
4840843Smsmith *
4940843Smsmith * Support functions for managing command queues used for
5040843Smsmith * various hardware blocks.
5140843Smsmith *
5240843Smsmith * The common command queue infrastructure abstracts out the
5340883Smsmith * software necessary for adding to Octeon's chained queue
5440843Smsmith * structures. These structures are used for commands to the
5540843Smsmith * PKO, ZIP, DFA, RAID, and DMA engine blocks. Although each
5640843Smsmith * hardware unit takes commands and CSRs of different types,
5740843Smsmith * they all use basic linked command buffers to store the
5840843Smsmith * pending request. In general, users of the CVMX API don't
5940843Smsmith * call cvmx-cmd-queue functions directly. Instead the hardware
6040843Smsmith * unit specific wrapper should be used. The wrappers perform
6194290Sdcs * unit specific validation and CSR writes to submit the
6240843Smsmith * commands.
6340843Smsmith *
6440843Smsmith * Even though most software will never directly interact with
6540843Smsmith * cvmx-cmd-queue, knowledge of its internal workings can help
6640843Smsmith * in diagnosing performance problems and help with debugging.
6740843Smsmith *
6840883Smsmith * Command queue pointers are stored in a global named block
6940843Smsmith * called "cvmx_cmd_queues". Except for the PKO queues, each
7040883Smsmith * hardware queue is stored in its own cache line to reduce SMP
7140843Smsmith * contention on spin locks. The PKO queues are stored such that
7240843Smsmith * every 16th queue is next to each other in memory. This scheme
7340843Smsmith * allows for queues being in separate cache lines when there
7440843Smsmith * are low number of queues per port. With 16 queues per port,
7540843Smsmith * the first queue for each port is in the same cache area. The
7640843Smsmith * second queues for each port are in another area, etc. This
7740843Smsmith * allows software to implement very efficient lockless PKO with
7840843Smsmith * 16 queues per port using a minimum of cache lines per core.
7940843Smsmith * All queues for a given core will be isolated in the same
8040843Smsmith * cache area.
8140843Smsmith *
8240843Smsmith * In addition to the memory pointer layout, cvmx-cmd-queue
8340843Smsmith * provides an optimized fair ll/sc locking mechanism for the
8440843Smsmith * queues. The lock uses a "ticket / now serving" model to
8540843Smsmith * maintain fair order on contended locks. In addition, it uses
8640843Smsmith * predicted locking time to limit cache contention. When a core
8740843Smsmith * know it must wait in line for a lock, it spins on the
8840843Smsmith * internal cycle counter to completely eliminate any causes of
8940843Smsmith * bus traffic.
9040843Smsmith *
9140843Smsmith * <hr> $Revision: 70030 $ <hr>
9240843Smsmith */
9340843Smsmith
9440843Smsmith#ifndef __CVMX_CMD_QUEUE_H__
9540843Smsmith#define __CVMX_CMD_QUEUE_H__
9640843Smsmith
9740883Smsmith#if !defined(CVMX_BUILD_FOR_LINUX_KERNEL) && !defined(CVMX_BUILD_FOR_FREEBSD_KERNEL)
9840843Smsmith#include "executive-config.h"
9994290Sdcs#include "cvmx-config.h"
10040843Smsmith#endif
10140843Smsmith
10240843Smsmith#include "cvmx-fpa.h"
10340843Smsmith
10440843Smsmith#ifdef	__cplusplus
10540843Smsmithextern "C" {
10640843Smsmith#endif
10740843Smsmith
10840843Smsmith/**
10940843Smsmith * By default we disable the max depth support. Most programs
11040843Smsmith * don't use it and it slows down the command queue processing
11140843Smsmith * significantly.
11240843Smsmith */
11340843Smsmith#ifndef CVMX_CMD_QUEUE_ENABLE_MAX_DEPTH
11440843Smsmith#define CVMX_CMD_QUEUE_ENABLE_MAX_DEPTH 0
11540843Smsmith#endif
11640843Smsmith
11740843Smsmith/**
11840843Smsmith * Enumeration representing all hardware blocks that use command
11940843Smsmith * queues. Each hardware block has up to 65536 sub identifiers for
12040843Smsmith * multiple command queues. Not all chips support all hardware
12140843Smsmith * units.
12240843Smsmith */
12340843Smsmithtypedef enum
12440843Smsmith{
12540843Smsmith    CVMX_CMD_QUEUE_PKO_BASE = 0x00000,
12640843Smsmith#define CVMX_CMD_QUEUE_PKO(queue) ((cvmx_cmd_queue_id_t)(CVMX_CMD_QUEUE_PKO_BASE + (0xffff&(queue))))
12740843Smsmith    CVMX_CMD_QUEUE_ZIP      = 0x10000,
12840843Smsmith#define CVMX_CMD_QUEUE_ZIP_QUE(queue) ((cvmx_cmd_queue_id_t)(CVMX_CMD_QUEUE_ZIP + (0xffff&(queue))))
12940843Smsmith    CVMX_CMD_QUEUE_DFA      = 0x20000,
13040843Smsmith    CVMX_CMD_QUEUE_RAID     = 0x30000,
13140843Smsmith    CVMX_CMD_QUEUE_DMA_BASE = 0x40000,
13240843Smsmith#define CVMX_CMD_QUEUE_DMA(queue) ((cvmx_cmd_queue_id_t)(CVMX_CMD_QUEUE_DMA_BASE + (0xffff&(queue))))
13340843Smsmith    CVMX_CMD_QUEUE_END      = 0x50000,
13440843Smsmith} cvmx_cmd_queue_id_t;
13540843Smsmith
13640843Smsmith/**
13740843Smsmith * Command write operations can fail if the command queue needs
13840843Smsmith * a new buffer and the associated FPA pool is empty. It can also
13940843Smsmith * fail if the number of queued command words reaches the maximum
14040843Smsmith * set at initialization.
14140843Smsmith */
14240843Smsmithtypedef enum
14340843Smsmith{
14440843Smsmith    CVMX_CMD_QUEUE_SUCCESS = 0,
14540843Smsmith    CVMX_CMD_QUEUE_NO_MEMORY = -1,
14640843Smsmith    CVMX_CMD_QUEUE_FULL = -2,
14740843Smsmith    CVMX_CMD_QUEUE_INVALID_PARAM = -3,
14840843Smsmith    CVMX_CMD_QUEUE_ALREADY_SETUP = -4,
14940843Smsmith} cvmx_cmd_queue_result_t;
15040843Smsmith
15140843Smsmithtypedef struct
15240843Smsmith{
15340843Smsmith    uint8_t  now_serving;           /**< You have lock when this is your ticket */
15440843Smsmith    uint64_t unused1        : 24;
15540843Smsmith    uint32_t max_depth;             /**< Maximum outstanding command words */
15640843Smsmith    uint64_t fpa_pool       : 3;    /**< FPA pool buffers come from */
15740843Smsmith    uint64_t base_ptr_div128: 29;   /**< Top of command buffer pointer shifted 7 */
15840843Smsmith    uint64_t unused2        : 6;
15940843Smsmith    uint64_t pool_size_m1   : 13;   /**< FPA buffer size in 64bit words minus 1 */
16040843Smsmith    uint64_t index          : 13;   /**< Number of commands already used in buffer */
16140843Smsmith} __cvmx_cmd_queue_state_t;
16240843Smsmith
16340843Smsmith/**
16440843Smsmith * This structure contains the global state of all command queues.
16540843Smsmith * It is stored in a bootmem named block and shared by all
16640843Smsmith * applications running on Octeon. Tickets are stored in a different
16740843Smsmith * cache line that queue information to reduce the contention on the
16840843Smsmith * ll/sc used to get a ticket. If this is not the case, the update
16940843Smsmith * of queue state causes the ll/sc to fail quite often.
17040843Smsmith */
17140843Smsmithtypedef struct
17240843Smsmith{
17340843Smsmith    uint64_t                 ticket[(CVMX_CMD_QUEUE_END>>16) * 256];
17440843Smsmith    __cvmx_cmd_queue_state_t state[(CVMX_CMD_QUEUE_END>>16) * 256];
17540843Smsmith} __cvmx_cmd_queue_all_state_t;
17640843Smsmith
17740843Smsmithextern CVMX_SHARED __cvmx_cmd_queue_all_state_t *__cvmx_cmd_queue_state_ptr;
17840843Smsmith
17940843Smsmith/**
18040843Smsmith * Initialize a command queue for use. The initial FPA buffer is
18140843Smsmith * allocated and the hardware unit is configured to point to the
18240843Smsmith * new command queue.
18340843Smsmith *
18451786Sdcs * @param queue_id  Hardware command queue to initialize.
18543078Smsmith * @param max_depth Maximum outstanding commands that can be queued.
18640843Smsmith * @param fpa_pool  FPA pool the command queues should come from.
18794290Sdcs * @param pool_size Size of each buffer in the FPA pool (bytes)
18894290Sdcs *
18994290Sdcs * @return CVMX_CMD_QUEUE_SUCCESS or a failure code
19094290Sdcs */
19140843Smsmithcvmx_cmd_queue_result_t cvmx_cmd_queue_initialize(cvmx_cmd_queue_id_t queue_id, int max_depth, int fpa_pool, int pool_size);
19240843Smsmith
19340843Smsmith/**
19451786Sdcs * Shutdown a queue a free it's command buffers to the FPA. The
19540843Smsmith * hardware connected to the queue must be stopped before this
19640843Smsmith * function is called.
19740843Smsmith *
19853633Sdcs * @param queue_id Queue to shutdown
19940843Smsmith *
20040843Smsmith * @return CVMX_CMD_QUEUE_SUCCESS or a failure code
20140843Smsmith */
20240843Smsmithcvmx_cmd_queue_result_t cvmx_cmd_queue_shutdown(cvmx_cmd_queue_id_t queue_id);
20394290Sdcs
20494290Sdcs/**
20594290Sdcs * Return the number of command words pending in the queue. This
20640843Smsmith * function may be relatively slow for some hardware units.
20740843Smsmith *
20840843Smsmith * @param queue_id Hardware command queue to query
20940843Smsmith *
21040843Smsmith * @return Number of outstanding commands
21140843Smsmith */
21240843Smsmithint cvmx_cmd_queue_length(cvmx_cmd_queue_id_t queue_id);
21340843Smsmith
21440843Smsmith/**
21594290Sdcs * Return the command buffer to be written to. The purpose of this
21640843Smsmith * function is to allow CVMX routine access to the low level buffer
21740843Smsmith * for initial hardware setup. User applications should not call this
21840843Smsmith * function directly.
21940843Smsmith *
22040843Smsmith * @param queue_id Command queue to query
22140843Smsmith *
22240843Smsmith * @return Command buffer or NULL on failure
22340843Smsmith */
22440843Smsmithvoid *cvmx_cmd_queue_buffer(cvmx_cmd_queue_id_t queue_id);
22540843Smsmith
22640843Smsmith/**
22740843Smsmith * @INTERNAL
22840843Smsmith * Get the index into the state arrays for the supplied queue id.
22940843Smsmith *
23040843Smsmith * @param queue_id Queue ID to get an index for
23140843Smsmith *
23240843Smsmith * @return Index into the state arrays
23340843Smsmith */
23440843Smsmithstatic inline int __cvmx_cmd_queue_get_index(cvmx_cmd_queue_id_t queue_id)
23540843Smsmith{
23640843Smsmith    /* Warning: This code currently only works with devices that have 256 queues
23740843Smsmith        or less. Devices with more than 16 queues are laid out in memory to allow
23840843Smsmith        cores quick access to every 16th queue. This reduces cache thrashing
23940843Smsmith        when you are running 16 queues per port to support lockless operation */
24040843Smsmith    int unit = queue_id>>16;
24140843Smsmith    int q = (queue_id >> 4) & 0xf;
24240843Smsmith    int core = queue_id & 0xf;
24340843Smsmith    return unit*256 + core*16 + q;
24440843Smsmith}
24540843Smsmith
24640843Smsmith
24740843Smsmith/**
24840843Smsmith * @INTERNAL
24940843Smsmith * Lock the supplied queue so nobody else is updating it at the same
25040843Smsmith * time as us.
25140843Smsmith *
25240843Smsmith * @param queue_id Queue ID to lock
25340843Smsmith * @param qptr     Pointer to the queue's global state
25440843Smsmith */
25540843Smsmithstatic inline void __cvmx_cmd_queue_lock(cvmx_cmd_queue_id_t queue_id, __cvmx_cmd_queue_state_t *qptr)
25640843Smsmith{
25740843Smsmith    int tmp;
25840843Smsmith    int my_ticket;
25940843Smsmith    CVMX_PREFETCH(qptr, 0);
26040843Smsmith    asm volatile (
26140843Smsmith        ".set push\n"
26240843Smsmith        ".set noreorder\n"
26340843Smsmith        "1:\n"
26451786Sdcs        "ll     %[my_ticket], %[ticket_ptr]\n"          /* Atomic add one to ticket_ptr */
26551786Sdcs        "li     %[ticket], 1\n"                         /*    and store the original value */
26651786Sdcs        "baddu  %[ticket], %[my_ticket]\n"              /*    in my_ticket */
26761182Sdcs        "sc     %[ticket], %[ticket_ptr]\n"
26851786Sdcs        "beqz   %[ticket], 1b\n"
26951786Sdcs        " nop\n"
27051786Sdcs        "lbu    %[ticket], %[now_serving]\n"            /* Load the current now_serving ticket */
27151786Sdcs        "2:\n"
27251786Sdcs        "beq    %[ticket], %[my_ticket], 4f\n"          /* Jump out if now_serving == my_ticket */
27351786Sdcs        " subu   %[ticket], %[my_ticket], %[ticket]\n"  /* Find out how many tickets are in front of me */
27451786Sdcs        "subu  %[ticket], 1\n"                          /* Use tickets in front of me minus one to delay */
27551786Sdcs        "cins   %[ticket], %[ticket], 5, 7\n"           /* Delay will be ((tickets in front)-1)*32 loops */
27651786Sdcs        "3:\n"
27751786Sdcs        "bnez   %[ticket], 3b\n"                        /* Loop here until our ticket might be up */
27851786Sdcs        " subu  %[ticket], 1\n"
27951786Sdcs        "b      2b\n"                                   /* Jump back up to check out ticket again */
28051786Sdcs        " lbu   %[ticket], %[now_serving]\n"            /* Load the current now_serving ticket */
28151786Sdcs        "4:\n"
28251786Sdcs        ".set pop\n"
28351786Sdcs        : [ticket_ptr] "=m" (__cvmx_cmd_queue_state_ptr->ticket[__cvmx_cmd_queue_get_index(queue_id)]),
28451786Sdcs          [now_serving] "=m" (qptr->now_serving),
28551786Sdcs          [ticket] "=r" (tmp),
28651786Sdcs          [my_ticket] "=r" (my_ticket)
28751786Sdcs    );
28851786Sdcs}
28951786Sdcs
29051786Sdcs
29151786Sdcs/**
29294290Sdcs * @INTERNAL
29340843Smsmith * Unlock the queue, flushing all writes.
29494290Sdcs *
29594290Sdcs * @param qptr   Queue to unlock
29694290Sdcs */
29794290Sdcsstatic inline void __cvmx_cmd_queue_unlock(__cvmx_cmd_queue_state_t *qptr)
29894290Sdcs{
29994290Sdcs    uint8_t ns;
30094290Sdcs
30194290Sdcs    ns = qptr->now_serving + 1;
30294290Sdcs    CVMX_SYNCWS; /* Order queue manipulation with respect to the unlock.  */
30394290Sdcs    qptr->now_serving = ns;
30440843Smsmith    CVMX_SYNCWS; /* nudge out the unlock. */
30540843Smsmith}
30640843Smsmith
30740843Smsmith
30840843Smsmith/**
30940843Smsmith * @INTERNAL
31040843Smsmith * Get the queue state structure for the given queue id
31153633Sdcs *
31240843Smsmith * @param queue_id Queue id to get
31394290Sdcs *
31440843Smsmith * @return Queue structure or NULL on failure
31594290Sdcs */
31694290Sdcsstatic inline __cvmx_cmd_queue_state_t *__cvmx_cmd_queue_get_state(cvmx_cmd_queue_id_t queue_id)
31794290Sdcs{
31840843Smsmith    if (CVMX_ENABLE_PARAMETER_CHECKING)
31994290Sdcs    {
32040843Smsmith        if (cvmx_unlikely(queue_id >= CVMX_CMD_QUEUE_END))
32140843Smsmith            return NULL;
32240843Smsmith        if (cvmx_unlikely((queue_id & 0xffff) >= 256))
32340843Smsmith            return NULL;
32440843Smsmith    }
32540843Smsmith    return &__cvmx_cmd_queue_state_ptr->state[__cvmx_cmd_queue_get_index(queue_id)];
32640843Smsmith}
32794290Sdcs
32840843Smsmith
32940843Smsmith/**
33040843Smsmith * Write an arbitrary number of command words to a command queue.
33140843Smsmith * This is a generic function; the fixed number of command word
33240843Smsmith * functions yield higher performance.
33340883Smsmith *
33440883Smsmith * @param queue_id  Hardware command queue to write to
33553633Sdcs * @param use_locking
33640843Smsmith *                  Use internal locking to ensure exclusive access for queue
33740843Smsmith *                  updates. If you don't use this locking you must ensure
33894290Sdcs *                  exclusivity some other way. Locking is strongly recommended.
33940843Smsmith * @param cmd_count Number of command words to write
34040843Smsmith * @param cmds      Array of commands to write
34140843Smsmith *
34240843Smsmith * @return CVMX_CMD_QUEUE_SUCCESS or a failure code
34340843Smsmith */
34440843Smsmithstatic 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)
34540843Smsmith{
346    __cvmx_cmd_queue_state_t *qptr = __cvmx_cmd_queue_get_state(queue_id);
347
348    if (CVMX_ENABLE_PARAMETER_CHECKING)
349    {
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;
356    }
357
358    /* Make sure nobody else is updating the same queue */
359    if (cvmx_likely(use_locking))
360        __cvmx_cmd_queue_lock(queue_id, qptr);
361
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
364        we allow it */
365    if (CVMX_CMD_QUEUE_ENABLE_MAX_DEPTH && cvmx_unlikely(qptr->max_depth))
366    {
367        if (cvmx_unlikely(cvmx_cmd_queue_length(queue_id) > (int)qptr->max_depth))
368        {
369            if (cvmx_likely(use_locking))
370                __cvmx_cmd_queue_unlock(qptr);
371            return CVMX_CMD_QUEUE_FULL;
372        }
373    }
374
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))
377    {
378        uint64_t *ptr = (uint64_t *)cvmx_phys_to_ptr((uint64_t)qptr->base_ptr_div128<<7);
379        ptr += qptr->index;
380        qptr->index += cmd_count;
381        while (cmd_count--)
382            *ptr++ = *cmds++;
383    }
384    else
385    {
386        uint64_t *ptr;
387        int 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))
391        {
392            if (cvmx_likely(use_locking))
393                __cvmx_cmd_queue_unlock(qptr);
394            return CVMX_CMD_QUEUE_NO_MEMORY;
395        }
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;
400        ptr += qptr->index;
401        cmd_count-=count;
402        while (count--)
403            *ptr++ = *cmds++;
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;
409        ptr = new_buffer;
410        while (cmd_count--)
411            *ptr++ = *cmds++;
412    }
413
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;
418}
419
420
421/**
422 * Simple function to write two command words to a command
423 * queue.
424 *
425 * @param queue_id Hardware command queue to write to
426 * @param use_locking
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
432 *
433 * @return CVMX_CMD_QUEUE_SUCCESS or a failure code
434 */
435static 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)
436{
437    __cvmx_cmd_queue_state_t *qptr = __cvmx_cmd_queue_get_state(queue_id);
438
439    if (CVMX_ENABLE_PARAMETER_CHECKING)
440    {
441        if (cvmx_unlikely(qptr == NULL))
442            return CVMX_CMD_QUEUE_INVALID_PARAM;
443    }
444
445    /* Make sure nobody else is updating the same queue */
446    if (cvmx_likely(use_locking))
447        __cvmx_cmd_queue_lock(queue_id, qptr);
448
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
451        we allow it */
452    if (CVMX_CMD_QUEUE_ENABLE_MAX_DEPTH && cvmx_unlikely(qptr->max_depth))
453    {
454        if (cvmx_unlikely(cvmx_cmd_queue_length(queue_id) > (int)qptr->max_depth))
455        {
456            if (cvmx_likely(use_locking))
457                __cvmx_cmd_queue_unlock(qptr);
458            return CVMX_CMD_QUEUE_FULL;
459        }
460    }
461
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))
464    {
465        uint64_t *ptr = (uint64_t *)cvmx_phys_to_ptr((uint64_t)qptr->base_ptr_div128<<7);
466        ptr += qptr->index;
467        qptr->index += 2;
468        ptr[0] = cmd1;
469        ptr[1] = cmd2;
470    }
471    else
472    {
473        uint64_t *ptr;
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))
480        {
481            if (cvmx_likely(use_locking))
482                __cvmx_cmd_queue_unlock(qptr);
483            return CVMX_CMD_QUEUE_NO_MEMORY;
484        }
485        count--;
486        ptr = (uint64_t *)cvmx_phys_to_ptr((uint64_t)qptr->base_ptr_div128<<7);
487        ptr += qptr->index;
488        *ptr++ = cmd1;
489        if (cvmx_likely(count))
490            *ptr++ = cmd2;
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;
495        qptr->index = 0;
496        if (cvmx_unlikely(count == 0))
497        {
498            qptr->index = 1;
499            new_buffer[0] = cmd2;
500        }
501    }
502
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;
507}
508
509
510/**
511 * Simple function to write three command words to a command
512 * queue.
513 *
514 * @param queue_id Hardware command queue to write to
515 * @param use_locking
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
522 *
523 * @return CVMX_CMD_QUEUE_SUCCESS or a failure code
524 */
525static 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)
526{
527    __cvmx_cmd_queue_state_t *qptr = __cvmx_cmd_queue_get_state(queue_id);
528
529    if (CVMX_ENABLE_PARAMETER_CHECKING)
530    {
531        if (cvmx_unlikely(qptr == NULL))
532            return CVMX_CMD_QUEUE_INVALID_PARAM;
533    }
534
535    /* Make sure nobody else is updating the same queue */
536    if (cvmx_likely(use_locking))
537        __cvmx_cmd_queue_lock(queue_id, qptr);
538
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
541        we allow it */
542    if (CVMX_CMD_QUEUE_ENABLE_MAX_DEPTH && cvmx_unlikely(qptr->max_depth))
543    {
544        if (cvmx_unlikely(cvmx_cmd_queue_length(queue_id) > (int)qptr->max_depth))
545        {
546            if (cvmx_likely(use_locking))
547                __cvmx_cmd_queue_unlock(qptr);
548            return CVMX_CMD_QUEUE_FULL;
549        }
550    }
551
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))
554    {
555        uint64_t *ptr = (uint64_t *)cvmx_phys_to_ptr((uint64_t)qptr->base_ptr_div128<<7);
556        ptr += qptr->index;
557        qptr->index += 3;
558        ptr[0] = cmd1;
559        ptr[1] = cmd2;
560        ptr[2] = cmd3;
561    }
562    else
563    {
564        uint64_t *ptr;
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))
571        {
572            if (cvmx_likely(use_locking))
573                __cvmx_cmd_queue_unlock(qptr);
574            return CVMX_CMD_QUEUE_NO_MEMORY;
575        }
576        count--;
577        ptr = (uint64_t *)cvmx_phys_to_ptr((uint64_t)qptr->base_ptr_div128<<7);
578        ptr += qptr->index;
579        *ptr++ = cmd1;
580        if (count)
581        {
582            *ptr++ = cmd2;
583            if (count > 1)
584                *ptr++ = cmd3;
585        }
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;
590        qptr->index = 0;
591        ptr = new_buffer;
592        if (count == 0)
593        {
594            *ptr++ = cmd2;
595            qptr->index++;
596        }
597        if (count < 2)
598        {
599            *ptr++ = cmd3;
600            qptr->index++;
601        }
602    }
603
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;
608}
609
610#ifdef	__cplusplus
611}
612#endif
613
614#endif /* __CVMX_CMD_QUEUE_H__ */
615