]> CyberLeo.Net >> Repos - FreeBSD/releng/8.2.git/blob - sys/contrib/octeon-sdk/cvmx-cmd-queue.c
Copy stable/8 to releng/8.2 in preparation for FreeBSD-8.2 release.
[FreeBSD/releng/8.2.git] / sys / contrib / octeon-sdk / cvmx-cmd-queue.c
1 /***********************license start***************
2  *  Copyright (c) 2003-2008 Cavium Networks (support@cavium.com). All rights
3  *  reserved.
4  *
5  *
6  *  Redistribution and use in source and binary forms, with or without
7  *  modification, are permitted provided that the following conditions are
8  *  met:
9  *
10  *      * Redistributions of source code must retain the above copyright
11  *        notice, this list of conditions and the following disclaimer.
12  *
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.
17  *
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
21  *        permission.
22  *
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.
33  *
34  *
35  *  For any questions regarding licensing please contact marketing@caviumnetworks.com
36  *
37  ***********************license end**************************************/
38
39
40
41
42
43
44 /**
45  * @file
46  *
47  * Support functions for managing command queues used for
48  * various hardware blocks.
49  *
50  * <hr>$Revision: 42150 $<hr>
51  */
52 #include "cvmx.h"
53 #include "cvmx-fpa.h"
54 #include "cvmx-cmd-queue.h"
55 #include "cvmx-bootmem.h"
56
57 /**
58  * This application uses this pointer to access the global queue
59  * state. It points to a bootmem named block.
60  */
61 CVMX_SHARED __cvmx_cmd_queue_all_state_t *__cvmx_cmd_queue_state_ptr = NULL;
62
63
64 /**
65  * @INTERNAL
66  * Initialize the Global queue state pointer.
67  *
68  * @return CVMX_CMD_QUEUE_SUCCESS or a failure code
69  */
70 static cvmx_cmd_queue_result_t __cvmx_cmd_queue_init_state_ptr(void)
71 {
72     char *alloc_name = "cvmx_cmd_queues";
73 #if defined(CONFIG_CAVIUM_RESERVE32) && CONFIG_CAVIUM_RESERVE32
74     extern uint64_t octeon_reserve32_memory;
75 #endif
76
77     if (cvmx_likely(__cvmx_cmd_queue_state_ptr))
78         return CVMX_CMD_QUEUE_SUCCESS;
79
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,
86                                                               128, alloc_name);
87     else
88 #endif
89         __cvmx_cmd_queue_state_ptr = cvmx_bootmem_alloc_named(sizeof(*__cvmx_cmd_queue_state_ptr), 128, alloc_name);
90 #else
91     __cvmx_cmd_queue_state_ptr = cvmx_bootmem_alloc_named(sizeof(*__cvmx_cmd_queue_state_ptr), 128, alloc_name);
92 #endif
93     if (__cvmx_cmd_queue_state_ptr)
94         memset(__cvmx_cmd_queue_state_ptr, 0, sizeof(*__cvmx_cmd_queue_state_ptr));
95     else
96     {
97         cvmx_bootmem_named_block_desc_t *block_desc = cvmx_bootmem_find_named_block(alloc_name);
98         if (block_desc)
99             __cvmx_cmd_queue_state_ptr = cvmx_phys_to_ptr(block_desc->base_addr);
100         else
101         {
102             cvmx_dprintf("ERROR: cvmx_cmd_queue_initialize: Unable to get named block %s.\n", alloc_name);
103             return CVMX_CMD_QUEUE_NO_MEMORY;
104         }
105     }
106     return CVMX_CMD_QUEUE_SUCCESS;
107 }
108
109
110 /**
111  * Initialize a command queue for use. The initial FPA buffer is
112  * allocated and the hardware unit is configured to point to the
113  * new command queue.
114  *
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)
119  *
120  * @return CVMX_CMD_QUEUE_SUCCESS or a failure code
121  */
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)
123 {
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)
127         return result;
128
129     qstate = __cvmx_cmd_queue_get_state(queue_id);
130     if (qstate == NULL)
131         return CVMX_CMD_QUEUE_INVALID_PARAM;
132
133     /* We artificially limit max_depth to 1<<20 words. It is an arbitrary limit */
134     if (CVMX_CMD_QUEUE_ENABLE_MAX_DEPTH)
135     {
136         if ((max_depth < 0) || (max_depth > 1<<20))
137             return CVMX_CMD_QUEUE_INVALID_PARAM;
138     }
139     else if (max_depth != 0)
140         return CVMX_CMD_QUEUE_INVALID_PARAM;
141
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;
146
147     /* See if someone else has already initialized the queue */
148     if (qstate->base_ptr_div128)
149     {
150         if (max_depth != (int)qstate->max_depth)
151         {
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;
154         }
155         if (fpa_pool != qstate->fpa_pool)
156         {
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;
159         }
160         if ((pool_size>>3)-1 != qstate->pool_size_m1)
161         {
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;
164         }
165         CVMX_SYNCWS;
166         return CVMX_CMD_QUEUE_ALREADY_SETUP;
167     }
168     else
169     {
170         cvmx_fpa_ctl_status_t status;
171         void *buffer;
172
173         status.u64 = cvmx_read_csr(CVMX_FPA_CTL_STATUS);
174         if (!status.s.enb)
175         {
176             cvmx_dprintf("ERROR: cvmx_cmd_queue_initialize: FPA is not enabled.\n");
177             return CVMX_CMD_QUEUE_NO_MEMORY;
178         }
179         buffer = cvmx_fpa_alloc(fpa_pool);
180         if (buffer == NULL)
181         {
182             cvmx_dprintf("ERROR: cvmx_cmd_queue_initialize: Unable to allocate initial buffer.\n");
183             return CVMX_CMD_QUEUE_NO_MEMORY;
184         }
185
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;
193         CVMX_SYNCWS;
194         return CVMX_CMD_QUEUE_SUCCESS;
195     }
196 }
197
198
199 /**
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.
203  *
204  * @param queue_id Queue to shutdown
205  *
206  * @return CVMX_CMD_QUEUE_SUCCESS or a failure code
207  */
208 cvmx_cmd_queue_result_t cvmx_cmd_queue_shutdown(cvmx_cmd_queue_id_t queue_id)
209 {
210     __cvmx_cmd_queue_state_t *qptr = __cvmx_cmd_queue_get_state(queue_id);
211     if (qptr == NULL)
212     {
213         cvmx_dprintf("ERROR: cvmx_cmd_queue_shutdown: Unable to get queue information.\n");
214         return CVMX_CMD_QUEUE_INVALID_PARAM;
215     }
216
217     if (cvmx_cmd_queue_length(queue_id) > 0)
218     {
219         cvmx_dprintf("ERROR: cvmx_cmd_queue_shutdown: Queue still has data in it.\n");
220         return CVMX_CMD_QUEUE_FULL;
221     }
222
223     __cvmx_cmd_queue_lock(queue_id, qptr);
224     if (qptr->base_ptr_div128)
225     {
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;
228     }
229     __cvmx_cmd_queue_unlock(qptr);
230
231     return CVMX_CMD_QUEUE_SUCCESS;
232 }
233
234
235 /**
236  * Return the number of command words pending in the queue. This
237  * function may be relatively slow for some hardware units.
238  *
239  * @param queue_id Hardware command queue to query
240  *
241  * @return Number of outstanding commands
242  */
243 int cvmx_cmd_queue_length(cvmx_cmd_queue_id_t queue_id)
244 {
245     if (CVMX_ENABLE_PARAMETER_CHECKING)
246     {
247         if (__cvmx_cmd_queue_get_state(queue_id) == NULL)
248             return CVMX_CMD_QUEUE_INVALID_PARAM;
249     }
250
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))
254     {
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))
261             {
262                 cvmx_pko_mem_debug9_t debug9;
263                 debug9.u64 = cvmx_read_csr(CVMX_PKO_MEM_DEBUG9);
264                 return debug9.cn38xx.doorbell;
265             }
266             else
267             {
268                 cvmx_pko_mem_debug8_t debug8;
269                 debug8.u64 = cvmx_read_csr(CVMX_PKO_MEM_DEBUG8);
270                 return debug8.cn58xx.doorbell;
271             }
272         case CVMX_CMD_QUEUE_ZIP:
273         case CVMX_CMD_QUEUE_DFA:
274         case CVMX_CMD_QUEUE_RAID:
275             // FIXME: Implement other lengths
276             return 0;
277         case CVMX_CMD_QUEUE_DMA_BASE:
278             {
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;
282             }
283         case CVMX_CMD_QUEUE_END:
284             return CVMX_CMD_QUEUE_INVALID_PARAM;
285     }
286     return CVMX_CMD_QUEUE_INVALID_PARAM;
287 }
288
289
290 /**
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
294  * function directly.
295  *
296  * @param queue_id Command queue to query
297  *
298  * @return Command buffer or NULL on failure
299  */
300 void *cvmx_cmd_queue_buffer(cvmx_cmd_queue_id_t queue_id)
301 {
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);
305     else
306         return NULL;
307 }
308