1 /***********************license start***************
2 * Copyright (c) 2003-2008 Cavium Networks (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 Networks 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 * TO THE MAXIMUM EXTENT PERMITTED BY LAW, THE SOFTWARE IS PROVIDED "AS IS"
24 * AND WITH ALL FAULTS AND CAVIUM NETWORKS MAKES NO PROMISES, REPRESENTATIONS
25 * OR WARRANTIES, EITHER EXPRESS, IMPLIED, STATUTORY, OR OTHERWISE, WITH
26 * RESPECT TO THE SOFTWARE, INCLUDING ITS CONDITION, ITS CONFORMITY TO ANY
27 * REPRESENTATION OR DESCRIPTION, OR THE EXISTENCE OF ANY LATENT OR PATENT
28 * DEFECTS, AND CAVIUM SPECIFICALLY DISCLAIMS ALL IMPLIED (IF ANY) WARRANTIES
29 * OF TITLE, MERCHANTABILITY, NONINFRINGEMENT, FITNESS FOR A PARTICULAR
30 * PURPOSE, LACK OF VIRUSES, ACCURACY OR COMPLETENESS, QUIET ENJOYMENT, QUIET
31 * POSSESSION OR CORRESPONDENCE TO DESCRIPTION. THE ENTIRE RISK ARISING OUT
32 * OF USE OR PERFORMANCE OF THE SOFTWARE LIES WITH YOU.
35 * For any questions regarding licensing please contact marketing@caviumnetworks.com
37 ***********************license end**************************************/
47 * Support functions for managing command queues used for
48 * various hardware blocks.
50 * <hr>$Revision: 42150 $<hr>
54 #include "cvmx-cmd-queue.h"
55 #include "cvmx-bootmem.h"
58 * This application uses this pointer to access the global queue
59 * state. It points to a bootmem named block.
61 CVMX_SHARED __cvmx_cmd_queue_all_state_t *__cvmx_cmd_queue_state_ptr = NULL;
66 * Initialize the Global queue state pointer.
68 * @return CVMX_CMD_QUEUE_SUCCESS or a failure code
70 static cvmx_cmd_queue_result_t __cvmx_cmd_queue_init_state_ptr(void)
72 char *alloc_name = "cvmx_cmd_queues";
73 #if defined(CONFIG_CAVIUM_RESERVE32) && CONFIG_CAVIUM_RESERVE32
74 extern uint64_t octeon_reserve32_memory;
77 if (cvmx_likely(__cvmx_cmd_queue_state_ptr))
78 return CVMX_CMD_QUEUE_SUCCESS;
80 #ifdef CVMX_BUILD_FOR_LINUX_KERNEL
81 #if CONFIG_CAVIUM_RESERVE32
82 if (octeon_reserve32_memory)
83 __cvmx_cmd_queue_state_ptr = cvmx_bootmem_alloc_named_range(sizeof(*__cvmx_cmd_queue_state_ptr),
84 octeon_reserve32_memory,
85 octeon_reserve32_memory + (CONFIG_CAVIUM_RESERVE32<<20) - 1,
89 __cvmx_cmd_queue_state_ptr = cvmx_bootmem_alloc_named(sizeof(*__cvmx_cmd_queue_state_ptr), 128, alloc_name);
91 __cvmx_cmd_queue_state_ptr = cvmx_bootmem_alloc_named(sizeof(*__cvmx_cmd_queue_state_ptr), 128, alloc_name);
93 if (__cvmx_cmd_queue_state_ptr)
94 memset(__cvmx_cmd_queue_state_ptr, 0, sizeof(*__cvmx_cmd_queue_state_ptr));
97 cvmx_bootmem_named_block_desc_t *block_desc = cvmx_bootmem_find_named_block(alloc_name);
99 __cvmx_cmd_queue_state_ptr = cvmx_phys_to_ptr(block_desc->base_addr);
102 cvmx_dprintf("ERROR: cvmx_cmd_queue_initialize: Unable to get named block %s.\n", alloc_name);
103 return CVMX_CMD_QUEUE_NO_MEMORY;
106 return CVMX_CMD_QUEUE_SUCCESS;
111 * Initialize a command queue for use. The initial FPA buffer is
112 * allocated and the hardware unit is configured to point to the
115 * @param queue_id Hardware command queue to initialize.
116 * @param max_depth Maximum outstanding commands that can be queued.
117 * @param fpa_pool FPA pool the command queues should come from.
118 * @param pool_size Size of each buffer in the FPA pool (bytes)
120 * @return CVMX_CMD_QUEUE_SUCCESS or a failure code
122 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)
124 __cvmx_cmd_queue_state_t *qstate;
125 cvmx_cmd_queue_result_t result = __cvmx_cmd_queue_init_state_ptr();
126 if (result != CVMX_CMD_QUEUE_SUCCESS)
129 qstate = __cvmx_cmd_queue_get_state(queue_id);
131 return CVMX_CMD_QUEUE_INVALID_PARAM;
133 /* We artificially limit max_depth to 1<<20 words. It is an arbitrary limit */
134 if (CVMX_CMD_QUEUE_ENABLE_MAX_DEPTH)
136 if ((max_depth < 0) || (max_depth > 1<<20))
137 return CVMX_CMD_QUEUE_INVALID_PARAM;
139 else if (max_depth != 0)
140 return CVMX_CMD_QUEUE_INVALID_PARAM;
142 if ((fpa_pool < 0) || (fpa_pool > 7))
143 return CVMX_CMD_QUEUE_INVALID_PARAM;
144 if ((pool_size < 128) || (pool_size > 65536))
145 return CVMX_CMD_QUEUE_INVALID_PARAM;
147 /* See if someone else has already initialized the queue */
148 if (qstate->base_ptr_div128)
150 if (max_depth != (int)qstate->max_depth)
152 cvmx_dprintf("ERROR: cvmx_cmd_queue_initialize: Queue already initalized with different max_depth (%d).\n", (int)qstate->max_depth);
153 return CVMX_CMD_QUEUE_INVALID_PARAM;
155 if (fpa_pool != qstate->fpa_pool)
157 cvmx_dprintf("ERROR: cvmx_cmd_queue_initialize: Queue already initalized with different FPA pool (%u).\n", qstate->fpa_pool);
158 return CVMX_CMD_QUEUE_INVALID_PARAM;
160 if ((pool_size>>3)-1 != qstate->pool_size_m1)
162 cvmx_dprintf("ERROR: cvmx_cmd_queue_initialize: Queue already initalized with different FPA pool size (%u).\n", (qstate->pool_size_m1+1)<<3);
163 return CVMX_CMD_QUEUE_INVALID_PARAM;
166 return CVMX_CMD_QUEUE_ALREADY_SETUP;
170 cvmx_fpa_ctl_status_t status;
173 status.u64 = cvmx_read_csr(CVMX_FPA_CTL_STATUS);
176 cvmx_dprintf("ERROR: cvmx_cmd_queue_initialize: FPA is not enabled.\n");
177 return CVMX_CMD_QUEUE_NO_MEMORY;
179 buffer = cvmx_fpa_alloc(fpa_pool);
182 cvmx_dprintf("ERROR: cvmx_cmd_queue_initialize: Unable to allocate initial buffer.\n");
183 return CVMX_CMD_QUEUE_NO_MEMORY;
186 memset(qstate, 0, sizeof(*qstate));
187 qstate->max_depth = max_depth;
188 qstate->fpa_pool = fpa_pool;
189 qstate->pool_size_m1 = (pool_size>>3)-1;
190 qstate->base_ptr_div128 = cvmx_ptr_to_phys(buffer) / 128;
191 /* We zeroed the now serving field so we need to also zero the ticket */
192 __cvmx_cmd_queue_state_ptr->ticket[__cvmx_cmd_queue_get_index(queue_id)] = 0;
194 return CVMX_CMD_QUEUE_SUCCESS;
200 * Shutdown a queue a free it's command buffers to the FPA. The
201 * hardware connected to the queue must be stopped before this
202 * function is called.
204 * @param queue_id Queue to shutdown
206 * @return CVMX_CMD_QUEUE_SUCCESS or a failure code
208 cvmx_cmd_queue_result_t cvmx_cmd_queue_shutdown(cvmx_cmd_queue_id_t queue_id)
210 __cvmx_cmd_queue_state_t *qptr = __cvmx_cmd_queue_get_state(queue_id);
213 cvmx_dprintf("ERROR: cvmx_cmd_queue_shutdown: Unable to get queue information.\n");
214 return CVMX_CMD_QUEUE_INVALID_PARAM;
217 if (cvmx_cmd_queue_length(queue_id) > 0)
219 cvmx_dprintf("ERROR: cvmx_cmd_queue_shutdown: Queue still has data in it.\n");
220 return CVMX_CMD_QUEUE_FULL;
223 __cvmx_cmd_queue_lock(queue_id, qptr);
224 if (qptr->base_ptr_div128)
226 cvmx_fpa_free(cvmx_phys_to_ptr((uint64_t)qptr->base_ptr_div128<<7), qptr->fpa_pool, 0);
227 qptr->base_ptr_div128 = 0;
229 __cvmx_cmd_queue_unlock(qptr);
231 return CVMX_CMD_QUEUE_SUCCESS;
236 * Return the number of command words pending in the queue. This
237 * function may be relatively slow for some hardware units.
239 * @param queue_id Hardware command queue to query
241 * @return Number of outstanding commands
243 int cvmx_cmd_queue_length(cvmx_cmd_queue_id_t queue_id)
245 if (CVMX_ENABLE_PARAMETER_CHECKING)
247 if (__cvmx_cmd_queue_get_state(queue_id) == NULL)
248 return CVMX_CMD_QUEUE_INVALID_PARAM;
251 /* The cast is here so gcc with check that all values in the
252 cvmx_cmd_queue_id_t enumeration are here */
253 switch ((cvmx_cmd_queue_id_t)(queue_id & 0xff0000))
255 case CVMX_CMD_QUEUE_PKO_BASE:
256 /* FIXME: Need atomic lock on CVMX_PKO_REG_READ_IDX. Right now we
257 are normally called with the queue lock, so that is a SLIGHT
258 amount of protection */
259 cvmx_write_csr(CVMX_PKO_REG_READ_IDX, queue_id & 0xffff);
260 if (OCTEON_IS_MODEL(OCTEON_CN3XXX))
262 cvmx_pko_mem_debug9_t debug9;
263 debug9.u64 = cvmx_read_csr(CVMX_PKO_MEM_DEBUG9);
264 return debug9.cn38xx.doorbell;
268 cvmx_pko_mem_debug8_t debug8;
269 debug8.u64 = cvmx_read_csr(CVMX_PKO_MEM_DEBUG8);
270 return debug8.cn58xx.doorbell;
272 case CVMX_CMD_QUEUE_ZIP:
273 case CVMX_CMD_QUEUE_DFA:
274 case CVMX_CMD_QUEUE_RAID:
275 // FIXME: Implement other lengths
277 case CVMX_CMD_QUEUE_DMA_BASE:
279 cvmx_npei_dmax_counts_t dmax_counts;
280 dmax_counts.u64 = cvmx_read_csr(CVMX_PEXP_NPEI_DMAX_COUNTS(queue_id & 0x7));
281 return dmax_counts.s.dbell;
283 case CVMX_CMD_QUEUE_END:
284 return CVMX_CMD_QUEUE_INVALID_PARAM;
286 return CVMX_CMD_QUEUE_INVALID_PARAM;
291 * Return the command buffer to be written to. The purpose of this
292 * function is to allow CVMX routine access t othe low level buffer
293 * for initial hardware setup. User applications should not call this
296 * @param queue_id Command queue to query
298 * @return Command buffer or NULL on failure
300 void *cvmx_cmd_queue_buffer(cvmx_cmd_queue_id_t queue_id)
302 __cvmx_cmd_queue_state_t *qptr = __cvmx_cmd_queue_get_state(queue_id);
303 if (qptr && qptr->base_ptr_div128)
304 return cvmx_phys_to_ptr((uint64_t)qptr->base_ptr_div128<<7);