2 * Copyright (c) 2017-2018 Cavium, Inc.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
15 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
16 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
19 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
20 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
21 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
22 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
23 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
24 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
25 * POSSIBILITY OF SUCH DAMAGE.
29 * File : ecore_init_ops.c
31 #include <sys/cdefs.h>
32 __FBSDID("$FreeBSD$");
35 /* include the precompiled configuration values - only once */
37 #include "ecore_hsi_common.h"
40 #include "ecore_status.h"
41 #include "ecore_rt_defs.h"
42 #include "ecore_init_fw_funcs.h"
44 #ifndef CONFIG_ECORE_BINARY_FW
45 #ifdef CONFIG_ECORE_ZIPPED_FW
46 #include "ecore_init_values_zipped.h"
48 #include "ecore_init_values.h"
52 #include "ecore_iro_values.h"
53 #include "ecore_sriov.h"
54 #include "ecore_gtt_values.h"
56 #include "ecore_init_ops.h"
58 #define ECORE_INIT_MAX_POLL_COUNT 100
59 #define ECORE_INIT_POLL_PERIOD_US 500
61 void ecore_init_iro_array(struct ecore_dev *p_dev)
63 p_dev->iro_arr = iro_arr;
66 /* Runtime configuration helpers */
67 void ecore_init_clear_rt_data(struct ecore_hwfn *p_hwfn)
71 for (i = 0; i < RUNTIME_ARRAY_SIZE; i++)
72 p_hwfn->rt_data.b_valid[i] = false;
75 void ecore_init_store_rt_reg(struct ecore_hwfn *p_hwfn,
76 u32 rt_offset, u32 val)
78 p_hwfn->rt_data.init_val[rt_offset] = val;
79 p_hwfn->rt_data.b_valid[rt_offset] = true;
82 void ecore_init_store_rt_agg(struct ecore_hwfn *p_hwfn,
83 u32 rt_offset, u32 *p_val,
88 for (i = 0; i < size / sizeof(u32); i++) {
89 p_hwfn->rt_data.init_val[rt_offset + i] = p_val[i];
90 p_hwfn->rt_data.b_valid[rt_offset + i] = true;
95 static enum _ecore_status_t ecore_init_rt(struct ecore_hwfn *p_hwfn,
96 struct ecore_ptt *p_ptt,
102 u32 *p_init_val = &p_hwfn->rt_data.init_val[rt_offset];
103 bool *p_valid = &p_hwfn->rt_data.b_valid[rt_offset];
105 enum _ecore_status_t rc = ECORE_SUCCESS;
107 /* Since not all RT entries are initialized, go over the RT and
108 * for each segment of initialized values use DMA.
110 for (i = 0; i < size; i++) {
114 /* In case there isn't any wide-bus configuration here,
115 * simply write the data instead of using dmae.
118 ecore_wr(p_hwfn, p_ptt, addr + (i << 2),
123 /* Start of a new segment */
124 for (segment = 1; i + segment < size; segment++)
125 if (!p_valid[i + segment])
128 rc = ecore_dmae_host2grc(p_hwfn, p_ptt,
129 (osal_uintptr_t)(p_init_val + i),
130 addr + (i << 2), segment, 0);
131 if (rc != ECORE_SUCCESS)
134 /* Jump over the entire segment, including invalid entry */
141 enum _ecore_status_t ecore_init_alloc(struct ecore_hwfn *p_hwfn)
143 struct ecore_rt_data *rt_data = &p_hwfn->rt_data;
145 if (IS_VF(p_hwfn->p_dev))
146 return ECORE_SUCCESS;
148 rt_data->b_valid = OSAL_ZALLOC(p_hwfn->p_dev, GFP_KERNEL,
149 sizeof(bool) * RUNTIME_ARRAY_SIZE);
150 if (!rt_data->b_valid)
153 rt_data->init_val = OSAL_ZALLOC(p_hwfn->p_dev, GFP_KERNEL,
154 sizeof(u32) * RUNTIME_ARRAY_SIZE);
155 if (!rt_data->init_val) {
156 OSAL_FREE(p_hwfn->p_dev, rt_data->b_valid);
157 rt_data->b_valid = OSAL_NULL;
161 return ECORE_SUCCESS;
164 void ecore_init_free(struct ecore_hwfn *p_hwfn)
166 OSAL_FREE(p_hwfn->p_dev, p_hwfn->rt_data.init_val);
167 p_hwfn->rt_data.init_val = OSAL_NULL;
168 OSAL_FREE(p_hwfn->p_dev, p_hwfn->rt_data.b_valid);
169 p_hwfn->rt_data.b_valid = OSAL_NULL;
172 static enum _ecore_status_t ecore_init_array_dmae(struct ecore_hwfn *p_hwfn,
173 struct ecore_ptt *p_ptt,
174 u32 addr, u32 dmae_data_offset,
175 u32 size, const u32 *p_buf,
176 bool b_must_dmae, bool b_can_dmae)
178 enum _ecore_status_t rc = ECORE_SUCCESS;
180 /* Perform DMAE only for lengthy enough sections or for wide-bus */
182 if ((CHIP_REV_IS_SLOW(p_hwfn->p_dev) && (size < 16)) ||
183 !b_can_dmae || (!b_must_dmae && (size < 16))) {
185 if (!b_can_dmae || (!b_must_dmae && (size < 16))) {
187 const u32 *data = p_buf + dmae_data_offset;
190 for (i = 0; i < size; i++)
191 ecore_wr(p_hwfn, p_ptt, addr + (i << 2), data[i]);
193 rc = ecore_dmae_host2grc(p_hwfn, p_ptt,
194 (osal_uintptr_t)(p_buf +
202 static enum _ecore_status_t ecore_init_fill_dmae(struct ecore_hwfn *p_hwfn,
203 struct ecore_ptt *p_ptt,
207 static u32 zero_buffer[DMAE_MAX_RW_SIZE];
209 OSAL_MEMSET(zero_buffer, 0, sizeof(u32) * DMAE_MAX_RW_SIZE);
211 return ecore_dmae_host2grc(p_hwfn, p_ptt,
212 (osal_uintptr_t)(&(zero_buffer[0])),
214 ECORE_DMAE_FLAG_RW_REPL_SRC);
217 static void ecore_init_fill(struct ecore_hwfn *p_hwfn,
218 struct ecore_ptt *p_ptt,
219 u32 addr, u32 fill, u32 fill_count)
223 for (i = 0; i < fill_count; i++, addr += sizeof(u32))
224 ecore_wr(p_hwfn, p_ptt, addr, fill);
228 static enum _ecore_status_t ecore_init_cmd_array(struct ecore_hwfn *p_hwfn,
229 struct ecore_ptt *p_ptt,
230 struct init_write_op *cmd,
234 u32 dmae_array_offset = OSAL_LE32_TO_CPU(cmd->args.array_offset);
235 u32 data = OSAL_LE32_TO_CPU(cmd->data);
236 u32 addr = GET_FIELD(data, INIT_WRITE_OP_ADDRESS) << 2;
237 #ifdef CONFIG_ECORE_ZIPPED_FW
238 u32 offset, output_len, input_len, max_size;
240 struct ecore_dev *p_dev = p_hwfn->p_dev;
241 union init_array_hdr *hdr;
242 const u32 *array_data;
243 enum _ecore_status_t rc = ECORE_SUCCESS;
246 array_data = p_dev->fw_data->arr_data;
248 hdr = (union init_array_hdr *) (array_data +
250 data = OSAL_LE32_TO_CPU(hdr->raw.data);
251 switch (GET_FIELD(data, INIT_ARRAY_RAW_HDR_TYPE)) {
252 case INIT_ARR_ZIPPED:
253 #ifdef CONFIG_ECORE_ZIPPED_FW
254 offset = dmae_array_offset + 1;
255 input_len = GET_FIELD(data,
256 INIT_ARRAY_ZIPPED_HDR_ZIPPED_SIZE);
257 max_size = MAX_ZIPPED_SIZE * 4;
258 OSAL_MEMSET(p_hwfn->unzip_buf, 0, max_size);
260 output_len = OSAL_UNZIP_DATA(p_hwfn, input_len,
261 (u8 *)&array_data[offset],
262 max_size, (u8 *)p_hwfn->unzip_buf);
264 rc = ecore_init_array_dmae(p_hwfn, p_ptt, addr, 0,
267 b_must_dmae, b_can_dmae);
269 DP_NOTICE(p_hwfn, true,
270 "Failed to unzip dmae data\n");
274 DP_NOTICE(p_hwfn, true,
275 "Using zipped firmware without config enabled\n");
279 case INIT_ARR_PATTERN:
281 u32 repeats = GET_FIELD(data,
282 INIT_ARRAY_PATTERN_HDR_REPETITIONS);
285 size = GET_FIELD(data,
286 INIT_ARRAY_PATTERN_HDR_PATTERN_SIZE);
288 for (i = 0; i < repeats; i++, addr += size << 2) {
289 rc = ecore_init_array_dmae(p_hwfn, p_ptt, addr,
290 dmae_array_offset + 1,
292 b_must_dmae, b_can_dmae);
298 case INIT_ARR_STANDARD:
299 size = GET_FIELD(data,
300 INIT_ARRAY_STANDARD_HDR_SIZE);
301 rc = ecore_init_array_dmae(p_hwfn, p_ptt, addr,
302 dmae_array_offset + 1,
304 b_must_dmae, b_can_dmae);
311 /* init_ops write command */
312 static enum _ecore_status_t ecore_init_cmd_wr(struct ecore_hwfn *p_hwfn,
313 struct ecore_ptt *p_ptt,
314 struct init_write_op *p_cmd,
317 u32 data = OSAL_LE32_TO_CPU(p_cmd->data);
318 bool b_must_dmae = GET_FIELD(data, INIT_WRITE_OP_WIDE_BUS);
319 u32 addr = GET_FIELD(data, INIT_WRITE_OP_ADDRESS) << 2;
320 enum _ecore_status_t rc = ECORE_SUCCESS;
323 if (b_must_dmae && !b_can_dmae) {
324 DP_NOTICE(p_hwfn, true,
325 "Need to write to %08x for Wide-bus but DMAE isn't allowed\n",
330 switch (GET_FIELD(data, INIT_WRITE_OP_SOURCE)) {
331 case INIT_SRC_INLINE:
332 data = OSAL_LE32_TO_CPU(p_cmd->args.inline_val);
333 ecore_wr(p_hwfn, p_ptt, addr, data);
336 data = OSAL_LE32_TO_CPU(p_cmd->args.zeros_count);
337 if (b_must_dmae || (b_can_dmae && (data >= 64)))
338 rc = ecore_init_fill_dmae(p_hwfn, p_ptt,
341 ecore_init_fill(p_hwfn, p_ptt, addr, 0, data);
344 rc = ecore_init_cmd_array(p_hwfn, p_ptt, p_cmd,
345 b_must_dmae, b_can_dmae);
347 case INIT_SRC_RUNTIME:
348 ecore_init_rt(p_hwfn, p_ptt, addr,
349 OSAL_LE16_TO_CPU(p_cmd->args.runtime.offset),
350 OSAL_LE16_TO_CPU(p_cmd->args.runtime.size),
358 static OSAL_INLINE bool comp_eq(u32 val, u32 expected_val)
360 return (val == expected_val);
363 static OSAL_INLINE bool comp_and(u32 val, u32 expected_val)
365 return (val & expected_val) == expected_val;
368 static OSAL_INLINE bool comp_or(u32 val, u32 expected_val)
370 return (val | expected_val) > 0;
373 /* init_ops read/poll commands */
374 static void ecore_init_cmd_rd(struct ecore_hwfn *p_hwfn,
375 struct ecore_ptt *p_ptt,
376 struct init_read_op *cmd)
378 bool (*comp_check)(u32 val, u32 expected_val);
379 u32 delay = ECORE_INIT_POLL_PERIOD_US, val;
380 u32 data, addr, poll;
383 data = OSAL_LE32_TO_CPU(cmd->op_data);
384 addr = GET_FIELD(data, INIT_READ_OP_ADDRESS) << 2;
385 poll = GET_FIELD(data, INIT_READ_OP_POLL_TYPE);
388 if (CHIP_REV_IS_EMUL(p_hwfn->p_dev))
392 val = ecore_rd(p_hwfn, p_ptt, addr);
394 if (poll == INIT_POLL_NONE)
399 comp_check = comp_eq;
402 comp_check = comp_or;
405 comp_check = comp_and;
408 DP_ERR(p_hwfn, "Invalid poll comparison type %08x\n",
413 data = OSAL_LE32_TO_CPU(cmd->expected_val);
415 i < ECORE_INIT_MAX_POLL_COUNT && !comp_check(val, data);
418 val = ecore_rd(p_hwfn, p_ptt, addr);
421 if (i == ECORE_INIT_MAX_POLL_COUNT)
422 DP_ERR(p_hwfn, "Timeout when polling reg: 0x%08x [ Waiting-for: %08x Got: %08x (comparsion %08x)]\n",
424 OSAL_LE32_TO_CPU(cmd->expected_val), val,
425 OSAL_LE32_TO_CPU(cmd->op_data));
428 /* init_ops callbacks entry point */
429 static void ecore_init_cmd_cb(struct ecore_hwfn *p_hwfn,
430 struct ecore_ptt *p_ptt,
431 struct init_callback_op *p_cmd)
433 DP_NOTICE(p_hwfn, true, "Currently init values have no need of callbacks\n");
436 static u8 ecore_init_cmd_mode_match(struct ecore_hwfn *p_hwfn,
437 u16 *p_offset, int modes)
439 struct ecore_dev *p_dev = p_hwfn->p_dev;
440 const u8 *modes_tree_buf;
441 u8 arg1, arg2, tree_val;
443 modes_tree_buf = p_dev->fw_data->modes_tree_buf;
444 tree_val = modes_tree_buf[(*p_offset)++];
446 case INIT_MODE_OP_NOT:
447 return ecore_init_cmd_mode_match(p_hwfn, p_offset, modes) ^ 1;
448 case INIT_MODE_OP_OR:
449 arg1 = ecore_init_cmd_mode_match(p_hwfn, p_offset, modes);
450 arg2 = ecore_init_cmd_mode_match(p_hwfn, p_offset, modes);
452 case INIT_MODE_OP_AND:
453 arg1 = ecore_init_cmd_mode_match(p_hwfn, p_offset, modes);
454 arg2 = ecore_init_cmd_mode_match(p_hwfn, p_offset, modes);
457 tree_val -= MAX_INIT_MODE_OPS;
458 return (modes & (1 << tree_val)) ? 1 : 0;
462 static u32 ecore_init_cmd_mode(struct ecore_hwfn *p_hwfn,
463 struct init_if_mode_op *p_cmd, int modes)
465 u16 offset = OSAL_LE16_TO_CPU(p_cmd->modes_buf_offset);
467 if (ecore_init_cmd_mode_match(p_hwfn, &offset, modes))
470 return GET_FIELD(OSAL_LE32_TO_CPU(p_cmd->op_data),
471 INIT_IF_MODE_OP_CMD_OFFSET);
474 static u32 ecore_init_cmd_phase(struct ecore_hwfn *p_hwfn,
475 struct init_if_phase_op *p_cmd,
476 u32 phase, u32 phase_id)
478 u32 data = OSAL_LE32_TO_CPU(p_cmd->phase_data);
480 if (!(GET_FIELD(data, INIT_IF_PHASE_OP_PHASE) == phase &&
481 (GET_FIELD(data, INIT_IF_PHASE_OP_PHASE_ID) == ANY_PHASE_ID ||
482 GET_FIELD(data, INIT_IF_PHASE_OP_PHASE_ID) == phase_id)))
483 return GET_FIELD(OSAL_LE32_TO_CPU(p_cmd->op_data),
484 INIT_IF_PHASE_OP_CMD_OFFSET);
489 enum _ecore_status_t ecore_init_run(struct ecore_hwfn *p_hwfn,
490 struct ecore_ptt *p_ptt,
495 struct ecore_dev *p_dev = p_hwfn->p_dev;
496 u32 cmd_num, num_init_ops;
497 union init_op *init_ops;
499 enum _ecore_status_t rc = ECORE_SUCCESS;
501 num_init_ops = p_dev->fw_data->init_ops_size;
502 init_ops = p_dev->fw_data->init_ops;
504 #ifdef CONFIG_ECORE_ZIPPED_FW
505 p_hwfn->unzip_buf = OSAL_ZALLOC(p_hwfn->p_dev, GFP_ATOMIC,
506 MAX_ZIPPED_SIZE * 4);
507 if (!p_hwfn->unzip_buf) {
508 DP_NOTICE(p_hwfn, true, "Failed to allocate unzip buffer\n");
513 for (cmd_num = 0; cmd_num < num_init_ops; cmd_num++) {
514 union init_op *cmd = &init_ops[cmd_num];
515 u32 data = OSAL_LE32_TO_CPU(cmd->raw.op_data);
517 switch (GET_FIELD(data, INIT_CALLBACK_OP_OP)) {
519 rc = ecore_init_cmd_wr(p_hwfn, p_ptt, &cmd->write,
524 ecore_init_cmd_rd(p_hwfn, p_ptt, &cmd->read);
527 case INIT_OP_IF_MODE:
528 cmd_num += ecore_init_cmd_mode(p_hwfn, &cmd->if_mode,
531 case INIT_OP_IF_PHASE:
532 cmd_num += ecore_init_cmd_phase(p_hwfn, &cmd->if_phase,
534 b_dmae = GET_FIELD(data,
535 INIT_IF_PHASE_OP_DMAE_ENABLE);
538 /* ecore_init_run is always invoked from
541 OSAL_UDELAY(cmd->delay.delay);
544 case INIT_OP_CALLBACK:
545 ecore_init_cmd_cb(p_hwfn, p_ptt, &cmd->callback);
552 #ifdef CONFIG_ECORE_ZIPPED_FW
553 OSAL_FREE(p_hwfn->p_dev, p_hwfn->unzip_buf);
554 p_hwfn->unzip_buf = OSAL_NULL;
559 void ecore_gtt_init(struct ecore_hwfn *p_hwfn)
565 if (CHIP_REV_IS_SLOW(p_hwfn->p_dev)) {
566 /* This is done by MFW on ASIC; regardless, this should only
567 * be done once per chip [i.e., common]. Implementation is
568 * not too bright, but it should work on the simple FPGA/EMUL
571 static bool initialized = false;
575 /* initialize PTT/GTT (poll for completion) */
577 ecore_wr(p_hwfn, p_hwfn->p_main_ptt,
578 PGLUE_B_REG_START_INIT_PTT_GTT, 1);
583 /* ptt might be overrided by HW until this is done */
585 ecore_ptt_invalidate(p_hwfn);
586 val = ecore_rd(p_hwfn, p_hwfn->p_main_ptt,
587 PGLUE_B_REG_INIT_DONE_PTT_GTT);
588 } while ((val != 1) && --poll_cnt);
591 DP_ERR(p_hwfn, "PGLUE_B_REG_INIT_DONE didn't complete\n");
595 /* Set the global windows */
596 gtt_base = PXP_PF_WINDOW_ADMIN_START + PXP_PF_WINDOW_ADMIN_GLOBAL_START;
598 for (i = 0; i < OSAL_ARRAY_SIZE(pxp_global_win); i++)
599 if (pxp_global_win[i])
600 REG_WR(p_hwfn, gtt_base + i * PXP_GLOBAL_ENTRY_SIZE,
604 enum _ecore_status_t ecore_init_fw_data(struct ecore_dev *p_dev,
607 struct ecore_fw_data *fw = p_dev->fw_data;
609 #ifdef CONFIG_ECORE_BINARY_FW
610 struct bin_buffer_hdr *buf_hdr;
614 DP_NOTICE(p_dev, true, "Invalid fw data\n");
618 buf_hdr = (struct bin_buffer_hdr *)data;
620 offset = buf_hdr[BIN_BUF_INIT_FW_VER_INFO].offset;
621 fw->fw_ver_info = (struct fw_ver_info *)(data + offset);
623 offset = buf_hdr[BIN_BUF_INIT_CMD].offset;
624 fw->init_ops = (union init_op *)(data + offset);
626 offset = buf_hdr[BIN_BUF_INIT_VAL].offset;
627 fw->arr_data = (u32 *)(data + offset);
629 offset = buf_hdr[BIN_BUF_INIT_MODE_TREE].offset;
630 fw->modes_tree_buf = (u8 *)(data + offset);
631 len = buf_hdr[BIN_BUF_INIT_CMD].length;
632 fw->init_ops_size = len / sizeof(struct init_raw_op);
634 fw->init_ops = (union init_op *)init_ops;
635 fw->arr_data = (u32 *)init_val;
636 fw->modes_tree_buf = (u8 *)modes_tree_buf;
637 fw->init_ops_size = init_ops_size;
640 return ECORE_SUCCESS;