xref: /linux/drivers/accel/habanalabs/gaudi/gaudi.c (revision 802f25b6c2c0377c681dd1e4f799a648c3df50dd)
1e65e175bSOded Gabbay // SPDX-License-Identifier: GPL-2.0
2e65e175bSOded Gabbay 
3e65e175bSOded Gabbay /*
4e65e175bSOded Gabbay  * Copyright 2016-2022 HabanaLabs, Ltd.
5e65e175bSOded Gabbay  * All Rights Reserved.
6e65e175bSOded Gabbay  */
7e65e175bSOded Gabbay 
8e65e175bSOded Gabbay #include "gaudiP.h"
9e65e175bSOded Gabbay #include "../include/hw_ip/mmu/mmu_general.h"
10e65e175bSOded Gabbay #include "../include/hw_ip/mmu/mmu_v1_1.h"
11e65e175bSOded Gabbay #include "../include/gaudi/gaudi_masks.h"
12e65e175bSOded Gabbay #include "../include/gaudi/gaudi_fw_if.h"
13e65e175bSOded Gabbay #include "../include/gaudi/gaudi_reg_map.h"
14e65e175bSOded Gabbay #include "../include/gaudi/gaudi_async_ids_map_extended.h"
15e65e175bSOded Gabbay 
16e65e175bSOded Gabbay #include <linux/module.h>
17e65e175bSOded Gabbay #include <linux/pci.h>
18e65e175bSOded Gabbay #include <linux/firmware.h>
19e65e175bSOded Gabbay #include <linux/hwmon.h>
20e65e175bSOded Gabbay #include <linux/iommu.h>
21e65e175bSOded Gabbay #include <linux/seq_file.h>
22e65e175bSOded Gabbay 
23e65e175bSOded Gabbay /*
24e65e175bSOded Gabbay  * Gaudi security scheme:
25e65e175bSOded Gabbay  *
26e65e175bSOded Gabbay  * 1. Host is protected by:
27e65e175bSOded Gabbay  *        - Range registers
28e65e175bSOded Gabbay  *        - MMU
29e65e175bSOded Gabbay  *
30e65e175bSOded Gabbay  * 2. DDR is protected by:
31e65e175bSOded Gabbay  *        - Range registers (protect the first 512MB)
32e65e175bSOded Gabbay  *
33e65e175bSOded Gabbay  * 3. Configuration is protected by:
34e65e175bSOded Gabbay  *        - Range registers
35e65e175bSOded Gabbay  *        - Protection bits
36e65e175bSOded Gabbay  *
37e65e175bSOded Gabbay  * MMU is always enabled.
38e65e175bSOded Gabbay  *
39e65e175bSOded Gabbay  * QMAN DMA channels 0,1 (PCI DMAN):
40e65e175bSOded Gabbay  *     - DMA is not secured.
41e65e175bSOded Gabbay  *     - PQ and CQ are secured.
42e65e175bSOded Gabbay  *     - CP is secured: The driver needs to parse CB but WREG should be allowed
43e65e175bSOded Gabbay  *                      because of TDMA (tensor DMA). Hence, WREG is always not
44e65e175bSOded Gabbay  *                      secured.
45e65e175bSOded Gabbay  *
46e65e175bSOded Gabbay  * When the driver needs to use DMA it will check that Gaudi is idle, set DMA
47e65e175bSOded Gabbay  * channel 0 to be secured, execute the DMA and change it back to not secured.
48e65e175bSOded Gabbay  * Currently, the driver doesn't use the DMA while there are compute jobs
49e65e175bSOded Gabbay  * running.
50e65e175bSOded Gabbay  *
51e65e175bSOded Gabbay  * The current use cases for the driver to use the DMA are:
52e65e175bSOded Gabbay  *     - Clear SRAM on context switch (happens on context switch when device is
53e65e175bSOded Gabbay  *       idle)
54e65e175bSOded Gabbay  *     - MMU page tables area clear (happens on init)
55e65e175bSOded Gabbay  *
56e65e175bSOded Gabbay  * QMAN DMA 2-7, TPC, MME, NIC:
57e65e175bSOded Gabbay  * PQ is secured and is located on the Host (HBM CON TPC3 bug)
58e65e175bSOded Gabbay  * CQ, CP and the engine are not secured
59e65e175bSOded Gabbay  *
60e65e175bSOded Gabbay  */
61e65e175bSOded Gabbay 
62e65e175bSOded Gabbay #define GAUDI_BOOT_FIT_FILE	"habanalabs/gaudi/gaudi-boot-fit.itb"
63e65e175bSOded Gabbay #define GAUDI_LINUX_FW_FILE	"habanalabs/gaudi/gaudi-fit.itb"
64e65e175bSOded Gabbay #define GAUDI_TPC_FW_FILE	"habanalabs/gaudi/gaudi_tpc.bin"
65e65e175bSOded Gabbay 
66e65e175bSOded Gabbay #define GAUDI_DMA_POOL_BLK_SIZE		0x100 /* 256 bytes */
67e65e175bSOded Gabbay 
68e65e175bSOded Gabbay #define GAUDI_RESET_TIMEOUT_MSEC	2000		/* 2000ms */
69e65e175bSOded Gabbay #define GAUDI_RESET_WAIT_MSEC		1		/* 1ms */
70e65e175bSOded Gabbay #define GAUDI_CPU_RESET_WAIT_MSEC	200		/* 200ms */
71e65e175bSOded Gabbay #define GAUDI_TEST_QUEUE_WAIT_USEC	100000		/* 100ms */
72e65e175bSOded Gabbay 
73e65e175bSOded Gabbay #define GAUDI_PLDM_RESET_WAIT_MSEC	1000		/* 1s */
74e65e175bSOded Gabbay #define GAUDI_PLDM_HRESET_TIMEOUT_MSEC	20000		/* 20s */
75e65e175bSOded Gabbay #define GAUDI_PLDM_TEST_QUEUE_WAIT_USEC	1000000		/* 1s */
76e65e175bSOded Gabbay #define GAUDI_PLDM_MMU_TIMEOUT_USEC	(MMU_CONFIG_TIMEOUT_USEC * 100)
77e65e175bSOded Gabbay #define GAUDI_PLDM_QMAN0_TIMEOUT_USEC	(HL_DEVICE_TIMEOUT_USEC * 30)
78e65e175bSOded Gabbay #define GAUDI_PLDM_TPC_KERNEL_WAIT_USEC	(HL_DEVICE_TIMEOUT_USEC * 30)
79e65e175bSOded Gabbay #define GAUDI_BOOT_FIT_REQ_TIMEOUT_USEC	4000000		/* 4s */
80e65e175bSOded Gabbay #define GAUDI_MSG_TO_CPU_TIMEOUT_USEC	4000000		/* 4s */
81e65e175bSOded Gabbay #define GAUDI_WAIT_FOR_BL_TIMEOUT_USEC	15000000	/* 15s */
82e65e175bSOded Gabbay 
83e65e175bSOded Gabbay #define GAUDI_QMAN0_FENCE_VAL		0x72E91AB9
84e65e175bSOded Gabbay 
85e65e175bSOded Gabbay #define GAUDI_MAX_STRING_LEN		20
86e65e175bSOded Gabbay 
87e65e175bSOded Gabbay #define GAUDI_CB_POOL_CB_CNT		512
88e65e175bSOded Gabbay #define GAUDI_CB_POOL_CB_SIZE		0x20000 /* 128KB */
89e65e175bSOded Gabbay 
90e65e175bSOded Gabbay #define GAUDI_ALLOC_CPU_MEM_RETRY_CNT	3
91e65e175bSOded Gabbay 
92e65e175bSOded Gabbay #define GAUDI_NUM_OF_TPC_INTR_CAUSE	20
93e65e175bSOded Gabbay 
94e65e175bSOded Gabbay #define GAUDI_NUM_OF_QM_ERR_CAUSE	16
95e65e175bSOded Gabbay 
96e65e175bSOded Gabbay #define GAUDI_NUM_OF_QM_ARB_ERR_CAUSE	3
97e65e175bSOded Gabbay 
98e65e175bSOded Gabbay #define GAUDI_ARB_WDT_TIMEOUT		0xEE6b27FF /* 8 seconds */
99e65e175bSOded Gabbay 
100e65e175bSOded Gabbay #define HBM_SCRUBBING_TIMEOUT_US	1000000 /* 1s */
101e65e175bSOded Gabbay 
102e65e175bSOded Gabbay #define BIN_REG_STRING_SIZE	sizeof("0b10101010101010101010101010101010")
103e65e175bSOded Gabbay 
104e65e175bSOded Gabbay #define MONITOR_SOB_STRING_SIZE		256
105e65e175bSOded Gabbay 
106e65e175bSOded Gabbay static u32 gaudi_stream_master[GAUDI_STREAM_MASTER_ARR_SIZE] = {
107e65e175bSOded Gabbay 	GAUDI_QUEUE_ID_DMA_0_0,
108e65e175bSOded Gabbay 	GAUDI_QUEUE_ID_DMA_0_1,
109e65e175bSOded Gabbay 	GAUDI_QUEUE_ID_DMA_0_2,
110e65e175bSOded Gabbay 	GAUDI_QUEUE_ID_DMA_0_3,
111e65e175bSOded Gabbay 	GAUDI_QUEUE_ID_DMA_1_0,
112e65e175bSOded Gabbay 	GAUDI_QUEUE_ID_DMA_1_1,
113e65e175bSOded Gabbay 	GAUDI_QUEUE_ID_DMA_1_2,
114e65e175bSOded Gabbay 	GAUDI_QUEUE_ID_DMA_1_3
115e65e175bSOded Gabbay };
116e65e175bSOded Gabbay 
117e65e175bSOded Gabbay static const char gaudi_irq_name[GAUDI_MSI_ENTRIES][GAUDI_MAX_STRING_LEN] = {
118e65e175bSOded Gabbay 		"gaudi cq 0_0", "gaudi cq 0_1", "gaudi cq 0_2", "gaudi cq 0_3",
119e65e175bSOded Gabbay 		"gaudi cq 1_0", "gaudi cq 1_1", "gaudi cq 1_2", "gaudi cq 1_3",
120e65e175bSOded Gabbay 		"gaudi cq 5_0", "gaudi cq 5_1", "gaudi cq 5_2", "gaudi cq 5_3",
121e65e175bSOded Gabbay 		"gaudi cpu eq"
122e65e175bSOded Gabbay };
123e65e175bSOded Gabbay 
124e65e175bSOded Gabbay static const u8 gaudi_dma_assignment[GAUDI_DMA_MAX] = {
125e65e175bSOded Gabbay 	[GAUDI_PCI_DMA_1] = GAUDI_ENGINE_ID_DMA_0,
126e65e175bSOded Gabbay 	[GAUDI_PCI_DMA_2] = GAUDI_ENGINE_ID_DMA_1,
127e65e175bSOded Gabbay 	[GAUDI_HBM_DMA_1] = GAUDI_ENGINE_ID_DMA_2,
128e65e175bSOded Gabbay 	[GAUDI_HBM_DMA_2] = GAUDI_ENGINE_ID_DMA_3,
129e65e175bSOded Gabbay 	[GAUDI_HBM_DMA_3] = GAUDI_ENGINE_ID_DMA_4,
130e65e175bSOded Gabbay 	[GAUDI_HBM_DMA_4] = GAUDI_ENGINE_ID_DMA_5,
131e65e175bSOded Gabbay 	[GAUDI_HBM_DMA_5] = GAUDI_ENGINE_ID_DMA_6,
132e65e175bSOded Gabbay 	[GAUDI_HBM_DMA_6] = GAUDI_ENGINE_ID_DMA_7
133e65e175bSOded Gabbay };
134e65e175bSOded Gabbay 
135e65e175bSOded Gabbay static const u8 gaudi_cq_assignment[NUMBER_OF_CMPLT_QUEUES] = {
136e65e175bSOded Gabbay 	[0] = GAUDI_QUEUE_ID_DMA_0_0,
137e65e175bSOded Gabbay 	[1] = GAUDI_QUEUE_ID_DMA_0_1,
138e65e175bSOded Gabbay 	[2] = GAUDI_QUEUE_ID_DMA_0_2,
139e65e175bSOded Gabbay 	[3] = GAUDI_QUEUE_ID_DMA_0_3,
140e65e175bSOded Gabbay 	[4] = GAUDI_QUEUE_ID_DMA_1_0,
141e65e175bSOded Gabbay 	[5] = GAUDI_QUEUE_ID_DMA_1_1,
142e65e175bSOded Gabbay 	[6] = GAUDI_QUEUE_ID_DMA_1_2,
143e65e175bSOded Gabbay 	[7] = GAUDI_QUEUE_ID_DMA_1_3,
144e65e175bSOded Gabbay };
145e65e175bSOded Gabbay 
146e65e175bSOded Gabbay static const u16 gaudi_packet_sizes[MAX_PACKET_ID] = {
147e65e175bSOded Gabbay 	[PACKET_WREG_32]	= sizeof(struct packet_wreg32),
148e65e175bSOded Gabbay 	[PACKET_WREG_BULK]	= sizeof(struct packet_wreg_bulk),
149e65e175bSOded Gabbay 	[PACKET_MSG_LONG]	= sizeof(struct packet_msg_long),
150e65e175bSOded Gabbay 	[PACKET_MSG_SHORT]	= sizeof(struct packet_msg_short),
151e65e175bSOded Gabbay 	[PACKET_CP_DMA]		= sizeof(struct packet_cp_dma),
152e65e175bSOded Gabbay 	[PACKET_REPEAT]		= sizeof(struct packet_repeat),
153e65e175bSOded Gabbay 	[PACKET_MSG_PROT]	= sizeof(struct packet_msg_prot),
154e65e175bSOded Gabbay 	[PACKET_FENCE]		= sizeof(struct packet_fence),
155e65e175bSOded Gabbay 	[PACKET_LIN_DMA]	= sizeof(struct packet_lin_dma),
156e65e175bSOded Gabbay 	[PACKET_NOP]		= sizeof(struct packet_nop),
157e65e175bSOded Gabbay 	[PACKET_STOP]		= sizeof(struct packet_stop),
158e65e175bSOded Gabbay 	[PACKET_ARB_POINT]	= sizeof(struct packet_arb_point),
159e65e175bSOded Gabbay 	[PACKET_WAIT]		= sizeof(struct packet_wait),
160e65e175bSOded Gabbay 	[PACKET_LOAD_AND_EXE]	= sizeof(struct packet_load_and_exe)
161e65e175bSOded Gabbay };
162e65e175bSOded Gabbay 
163e65e175bSOded Gabbay static inline bool validate_packet_id(enum packet_id id)
164e65e175bSOded Gabbay {
165e65e175bSOded Gabbay 	switch (id) {
166e65e175bSOded Gabbay 	case PACKET_WREG_32:
167e65e175bSOded Gabbay 	case PACKET_WREG_BULK:
168e65e175bSOded Gabbay 	case PACKET_MSG_LONG:
169e65e175bSOded Gabbay 	case PACKET_MSG_SHORT:
170e65e175bSOded Gabbay 	case PACKET_CP_DMA:
171e65e175bSOded Gabbay 	case PACKET_REPEAT:
172e65e175bSOded Gabbay 	case PACKET_MSG_PROT:
173e65e175bSOded Gabbay 	case PACKET_FENCE:
174e65e175bSOded Gabbay 	case PACKET_LIN_DMA:
175e65e175bSOded Gabbay 	case PACKET_NOP:
176e65e175bSOded Gabbay 	case PACKET_STOP:
177e65e175bSOded Gabbay 	case PACKET_ARB_POINT:
178e65e175bSOded Gabbay 	case PACKET_WAIT:
179e65e175bSOded Gabbay 	case PACKET_LOAD_AND_EXE:
180e65e175bSOded Gabbay 		return true;
181e65e175bSOded Gabbay 	default:
182e65e175bSOded Gabbay 		return false;
183e65e175bSOded Gabbay 	}
184e65e175bSOded Gabbay }
185e65e175bSOded Gabbay 
186e65e175bSOded Gabbay static const char * const
187e65e175bSOded Gabbay gaudi_tpc_interrupts_cause[GAUDI_NUM_OF_TPC_INTR_CAUSE] = {
188e65e175bSOded Gabbay 	"tpc_address_exceed_slm",
189e65e175bSOded Gabbay 	"tpc_div_by_0",
190e65e175bSOded Gabbay 	"tpc_spu_mac_overflow",
191e65e175bSOded Gabbay 	"tpc_spu_addsub_overflow",
192e65e175bSOded Gabbay 	"tpc_spu_abs_overflow",
193e65e175bSOded Gabbay 	"tpc_spu_fp_dst_nan_inf",
194e65e175bSOded Gabbay 	"tpc_spu_fp_dst_denorm",
195e65e175bSOded Gabbay 	"tpc_vpu_mac_overflow",
196e65e175bSOded Gabbay 	"tpc_vpu_addsub_overflow",
197e65e175bSOded Gabbay 	"tpc_vpu_abs_overflow",
198e65e175bSOded Gabbay 	"tpc_vpu_fp_dst_nan_inf",
199e65e175bSOded Gabbay 	"tpc_vpu_fp_dst_denorm",
200e65e175bSOded Gabbay 	"tpc_assertions",
201e65e175bSOded Gabbay 	"tpc_illegal_instruction",
202e65e175bSOded Gabbay 	"tpc_pc_wrap_around",
203e65e175bSOded Gabbay 	"tpc_qm_sw_err",
204e65e175bSOded Gabbay 	"tpc_hbw_rresp_err",
205e65e175bSOded Gabbay 	"tpc_hbw_bresp_err",
206e65e175bSOded Gabbay 	"tpc_lbw_rresp_err",
207e65e175bSOded Gabbay 	"tpc_lbw_bresp_err"
208e65e175bSOded Gabbay };
209e65e175bSOded Gabbay 
210e65e175bSOded Gabbay static const char * const
211e65e175bSOded Gabbay gaudi_qman_error_cause[GAUDI_NUM_OF_QM_ERR_CAUSE] = {
212e65e175bSOded Gabbay 	"PQ AXI HBW error",
213e65e175bSOded Gabbay 	"CQ AXI HBW error",
214e65e175bSOded Gabbay 	"CP AXI HBW error",
215e65e175bSOded Gabbay 	"CP error due to undefined OPCODE",
216e65e175bSOded Gabbay 	"CP encountered STOP OPCODE",
217e65e175bSOded Gabbay 	"CP AXI LBW error",
218e65e175bSOded Gabbay 	"CP WRREG32 or WRBULK returned error",
219e65e175bSOded Gabbay 	"N/A",
220e65e175bSOded Gabbay 	"FENCE 0 inc over max value and clipped",
221e65e175bSOded Gabbay 	"FENCE 1 inc over max value and clipped",
222e65e175bSOded Gabbay 	"FENCE 2 inc over max value and clipped",
223e65e175bSOded Gabbay 	"FENCE 3 inc over max value and clipped",
224e65e175bSOded Gabbay 	"FENCE 0 dec under min value and clipped",
225e65e175bSOded Gabbay 	"FENCE 1 dec under min value and clipped",
226e65e175bSOded Gabbay 	"FENCE 2 dec under min value and clipped",
227e65e175bSOded Gabbay 	"FENCE 3 dec under min value and clipped"
228e65e175bSOded Gabbay };
229e65e175bSOded Gabbay 
230e65e175bSOded Gabbay static const char * const
231e65e175bSOded Gabbay gaudi_qman_arb_error_cause[GAUDI_NUM_OF_QM_ARB_ERR_CAUSE] = {
232e65e175bSOded Gabbay 	"Choice push while full error",
233e65e175bSOded Gabbay 	"Choice Q watchdog error",
234e65e175bSOded Gabbay 	"MSG AXI LBW returned with error"
235e65e175bSOded Gabbay };
236e65e175bSOded Gabbay 
237e65e175bSOded Gabbay static enum hl_queue_type gaudi_queue_type[GAUDI_QUEUE_ID_SIZE] = {
238e65e175bSOded Gabbay 	QUEUE_TYPE_EXT, /* GAUDI_QUEUE_ID_DMA_0_0 */
239e65e175bSOded Gabbay 	QUEUE_TYPE_EXT, /* GAUDI_QUEUE_ID_DMA_0_1 */
240e65e175bSOded Gabbay 	QUEUE_TYPE_EXT, /* GAUDI_QUEUE_ID_DMA_0_2 */
241e65e175bSOded Gabbay 	QUEUE_TYPE_EXT, /* GAUDI_QUEUE_ID_DMA_0_3 */
242e65e175bSOded Gabbay 	QUEUE_TYPE_EXT, /* GAUDI_QUEUE_ID_DMA_1_0 */
243e65e175bSOded Gabbay 	QUEUE_TYPE_EXT, /* GAUDI_QUEUE_ID_DMA_1_1 */
244e65e175bSOded Gabbay 	QUEUE_TYPE_EXT, /* GAUDI_QUEUE_ID_DMA_1_2 */
245e65e175bSOded Gabbay 	QUEUE_TYPE_EXT, /* GAUDI_QUEUE_ID_DMA_1_3 */
246e65e175bSOded Gabbay 	QUEUE_TYPE_CPU, /* GAUDI_QUEUE_ID_CPU_PQ */
247e65e175bSOded Gabbay 	QUEUE_TYPE_INT, /* GAUDI_QUEUE_ID_DMA_2_0 */
248e65e175bSOded Gabbay 	QUEUE_TYPE_INT, /* GAUDI_QUEUE_ID_DMA_2_1 */
249e65e175bSOded Gabbay 	QUEUE_TYPE_INT, /* GAUDI_QUEUE_ID_DMA_2_2 */
250e65e175bSOded Gabbay 	QUEUE_TYPE_INT, /* GAUDI_QUEUE_ID_DMA_2_3 */
251e65e175bSOded Gabbay 	QUEUE_TYPE_INT, /* GAUDI_QUEUE_ID_DMA_3_0 */
252e65e175bSOded Gabbay 	QUEUE_TYPE_INT, /* GAUDI_QUEUE_ID_DMA_3_1 */
253e65e175bSOded Gabbay 	QUEUE_TYPE_INT, /* GAUDI_QUEUE_ID_DMA_3_2 */
254e65e175bSOded Gabbay 	QUEUE_TYPE_INT, /* GAUDI_QUEUE_ID_DMA_3_3 */
255e65e175bSOded Gabbay 	QUEUE_TYPE_INT, /* GAUDI_QUEUE_ID_DMA_4_0 */
256e65e175bSOded Gabbay 	QUEUE_TYPE_INT, /* GAUDI_QUEUE_ID_DMA_4_1 */
257e65e175bSOded Gabbay 	QUEUE_TYPE_INT, /* GAUDI_QUEUE_ID_DMA_4_2 */
258e65e175bSOded Gabbay 	QUEUE_TYPE_INT, /* GAUDI_QUEUE_ID_DMA_4_3 */
259e65e175bSOded Gabbay 	QUEUE_TYPE_INT, /* GAUDI_QUEUE_ID_DMA_5_0 */
260e65e175bSOded Gabbay 	QUEUE_TYPE_INT, /* GAUDI_QUEUE_ID_DMA_5_1 */
261e65e175bSOded Gabbay 	QUEUE_TYPE_INT, /* GAUDI_QUEUE_ID_DMA_5_2 */
262e65e175bSOded Gabbay 	QUEUE_TYPE_INT, /* GAUDI_QUEUE_ID_DMA_5_3 */
263e65e175bSOded Gabbay 	QUEUE_TYPE_INT, /* GAUDI_QUEUE_ID_DMA_6_0 */
264e65e175bSOded Gabbay 	QUEUE_TYPE_INT, /* GAUDI_QUEUE_ID_DMA_6_1 */
265e65e175bSOded Gabbay 	QUEUE_TYPE_INT, /* GAUDI_QUEUE_ID_DMA_6_2 */
266e65e175bSOded Gabbay 	QUEUE_TYPE_INT, /* GAUDI_QUEUE_ID_DMA_6_3 */
267e65e175bSOded Gabbay 	QUEUE_TYPE_INT, /* GAUDI_QUEUE_ID_DMA_7_0 */
268e65e175bSOded Gabbay 	QUEUE_TYPE_INT, /* GAUDI_QUEUE_ID_DMA_7_1 */
269e65e175bSOded Gabbay 	QUEUE_TYPE_INT, /* GAUDI_QUEUE_ID_DMA_7_2 */
270e65e175bSOded Gabbay 	QUEUE_TYPE_INT, /* GAUDI_QUEUE_ID_DMA_7_3 */
271e65e175bSOded Gabbay 	QUEUE_TYPE_INT, /* GAUDI_QUEUE_ID_MME_0_0 */
272e65e175bSOded Gabbay 	QUEUE_TYPE_INT, /* GAUDI_QUEUE_ID_MME_0_1 */
273e65e175bSOded Gabbay 	QUEUE_TYPE_INT, /* GAUDI_QUEUE_ID_MME_0_2 */
274e65e175bSOded Gabbay 	QUEUE_TYPE_INT, /* GAUDI_QUEUE_ID_MME_0_3 */
275e65e175bSOded Gabbay 	QUEUE_TYPE_INT, /* GAUDI_QUEUE_ID_MME_1_0 */
276e65e175bSOded Gabbay 	QUEUE_TYPE_INT, /* GAUDI_QUEUE_ID_MME_1_1 */
277e65e175bSOded Gabbay 	QUEUE_TYPE_INT, /* GAUDI_QUEUE_ID_MME_1_2 */
278e65e175bSOded Gabbay 	QUEUE_TYPE_INT, /* GAUDI_QUEUE_ID_MME_1_3 */
279e65e175bSOded Gabbay 	QUEUE_TYPE_INT, /* GAUDI_QUEUE_ID_TPC_0_0 */
280e65e175bSOded Gabbay 	QUEUE_TYPE_INT, /* GAUDI_QUEUE_ID_TPC_0_1 */
281e65e175bSOded Gabbay 	QUEUE_TYPE_INT, /* GAUDI_QUEUE_ID_TPC_0_2 */
282e65e175bSOded Gabbay 	QUEUE_TYPE_INT, /* GAUDI_QUEUE_ID_TPC_0_3 */
283e65e175bSOded Gabbay 	QUEUE_TYPE_INT, /* GAUDI_QUEUE_ID_TPC_1_0 */
284e65e175bSOded Gabbay 	QUEUE_TYPE_INT, /* GAUDI_QUEUE_ID_TPC_1_1 */
285e65e175bSOded Gabbay 	QUEUE_TYPE_INT, /* GAUDI_QUEUE_ID_TPC_1_2 */
286e65e175bSOded Gabbay 	QUEUE_TYPE_INT, /* GAUDI_QUEUE_ID_TPC_1_3 */
287e65e175bSOded Gabbay 	QUEUE_TYPE_INT, /* GAUDI_QUEUE_ID_TPC_2_0 */
288e65e175bSOded Gabbay 	QUEUE_TYPE_INT, /* GAUDI_QUEUE_ID_TPC_2_1 */
289e65e175bSOded Gabbay 	QUEUE_TYPE_INT, /* GAUDI_QUEUE_ID_TPC_2_2 */
290e65e175bSOded Gabbay 	QUEUE_TYPE_INT, /* GAUDI_QUEUE_ID_TPC_2_3 */
291e65e175bSOded Gabbay 	QUEUE_TYPE_INT, /* GAUDI_QUEUE_ID_TPC_3_0 */
292e65e175bSOded Gabbay 	QUEUE_TYPE_INT, /* GAUDI_QUEUE_ID_TPC_3_1 */
293e65e175bSOded Gabbay 	QUEUE_TYPE_INT, /* GAUDI_QUEUE_ID_TPC_3_2 */
294e65e175bSOded Gabbay 	QUEUE_TYPE_INT, /* GAUDI_QUEUE_ID_TPC_3_3 */
295e65e175bSOded Gabbay 	QUEUE_TYPE_INT, /* GAUDI_QUEUE_ID_TPC_4_0 */
296e65e175bSOded Gabbay 	QUEUE_TYPE_INT, /* GAUDI_QUEUE_ID_TPC_4_1 */
297e65e175bSOded Gabbay 	QUEUE_TYPE_INT, /* GAUDI_QUEUE_ID_TPC_4_2 */
298e65e175bSOded Gabbay 	QUEUE_TYPE_INT, /* GAUDI_QUEUE_ID_TPC_4_3 */
299e65e175bSOded Gabbay 	QUEUE_TYPE_INT, /* GAUDI_QUEUE_ID_TPC_5_0 */
300e65e175bSOded Gabbay 	QUEUE_TYPE_INT, /* GAUDI_QUEUE_ID_TPC_5_1 */
301e65e175bSOded Gabbay 	QUEUE_TYPE_INT, /* GAUDI_QUEUE_ID_TPC_5_2 */
302e65e175bSOded Gabbay 	QUEUE_TYPE_INT, /* GAUDI_QUEUE_ID_TPC_5_3 */
303e65e175bSOded Gabbay 	QUEUE_TYPE_INT, /* GAUDI_QUEUE_ID_TPC_6_0 */
304e65e175bSOded Gabbay 	QUEUE_TYPE_INT, /* GAUDI_QUEUE_ID_TPC_6_1 */
305e65e175bSOded Gabbay 	QUEUE_TYPE_INT, /* GAUDI_QUEUE_ID_TPC_6_2 */
306e65e175bSOded Gabbay 	QUEUE_TYPE_INT, /* GAUDI_QUEUE_ID_TPC_6_3 */
307e65e175bSOded Gabbay 	QUEUE_TYPE_INT, /* GAUDI_QUEUE_ID_TPC_7_0 */
308e65e175bSOded Gabbay 	QUEUE_TYPE_INT, /* GAUDI_QUEUE_ID_TPC_7_1 */
309e65e175bSOded Gabbay 	QUEUE_TYPE_INT, /* GAUDI_QUEUE_ID_TPC_7_2 */
310e65e175bSOded Gabbay 	QUEUE_TYPE_INT, /* GAUDI_QUEUE_ID_TPC_7_3 */
311e65e175bSOded Gabbay 	QUEUE_TYPE_INT, /* GAUDI_QUEUE_ID_NIC_0_0 */
312e65e175bSOded Gabbay 	QUEUE_TYPE_INT, /* GAUDI_QUEUE_ID_NIC_0_1 */
313e65e175bSOded Gabbay 	QUEUE_TYPE_INT, /* GAUDI_QUEUE_ID_NIC_0_2 */
314e65e175bSOded Gabbay 	QUEUE_TYPE_INT, /* GAUDI_QUEUE_ID_NIC_0_3 */
315e65e175bSOded Gabbay 	QUEUE_TYPE_INT, /* GAUDI_QUEUE_ID_NIC_1_0 */
316e65e175bSOded Gabbay 	QUEUE_TYPE_INT, /* GAUDI_QUEUE_ID_NIC_1_1 */
317e65e175bSOded Gabbay 	QUEUE_TYPE_INT, /* GAUDI_QUEUE_ID_NIC_1_2 */
318e65e175bSOded Gabbay 	QUEUE_TYPE_INT, /* GAUDI_QUEUE_ID_NIC_1_3 */
319e65e175bSOded Gabbay 	QUEUE_TYPE_INT, /* GAUDI_QUEUE_ID_NIC_2_0 */
320e65e175bSOded Gabbay 	QUEUE_TYPE_INT, /* GAUDI_QUEUE_ID_NIC_2_1 */
321e65e175bSOded Gabbay 	QUEUE_TYPE_INT, /* GAUDI_QUEUE_ID_NIC_2_2 */
322e65e175bSOded Gabbay 	QUEUE_TYPE_INT, /* GAUDI_QUEUE_ID_NIC_2_3 */
323e65e175bSOded Gabbay 	QUEUE_TYPE_INT, /* GAUDI_QUEUE_ID_NIC_3_0 */
324e65e175bSOded Gabbay 	QUEUE_TYPE_INT, /* GAUDI_QUEUE_ID_NIC_3_1 */
325e65e175bSOded Gabbay 	QUEUE_TYPE_INT, /* GAUDI_QUEUE_ID_NIC_3_2 */
326e65e175bSOded Gabbay 	QUEUE_TYPE_INT, /* GAUDI_QUEUE_ID_NIC_3_3 */
327e65e175bSOded Gabbay 	QUEUE_TYPE_INT, /* GAUDI_QUEUE_ID_NIC_4_0 */
328e65e175bSOded Gabbay 	QUEUE_TYPE_INT, /* GAUDI_QUEUE_ID_NIC_4_1 */
329e65e175bSOded Gabbay 	QUEUE_TYPE_INT, /* GAUDI_QUEUE_ID_NIC_4_2 */
330e65e175bSOded Gabbay 	QUEUE_TYPE_INT, /* GAUDI_QUEUE_ID_NIC_4_3 */
331e65e175bSOded Gabbay 	QUEUE_TYPE_INT, /* GAUDI_QUEUE_ID_NIC_5_0 */
332e65e175bSOded Gabbay 	QUEUE_TYPE_INT, /* GAUDI_QUEUE_ID_NIC_5_1 */
333e65e175bSOded Gabbay 	QUEUE_TYPE_INT, /* GAUDI_QUEUE_ID_NIC_5_2 */
334e65e175bSOded Gabbay 	QUEUE_TYPE_INT, /* GAUDI_QUEUE_ID_NIC_5_3 */
335e65e175bSOded Gabbay 	QUEUE_TYPE_INT, /* GAUDI_QUEUE_ID_NIC_6_0 */
336e65e175bSOded Gabbay 	QUEUE_TYPE_INT, /* GAUDI_QUEUE_ID_NIC_6_1 */
337e65e175bSOded Gabbay 	QUEUE_TYPE_INT, /* GAUDI_QUEUE_ID_NIC_6_2 */
338e65e175bSOded Gabbay 	QUEUE_TYPE_INT, /* GAUDI_QUEUE_ID_NIC_6_3 */
339e65e175bSOded Gabbay 	QUEUE_TYPE_INT, /* GAUDI_QUEUE_ID_NIC_7_0 */
340e65e175bSOded Gabbay 	QUEUE_TYPE_INT, /* GAUDI_QUEUE_ID_NIC_7_1 */
341e65e175bSOded Gabbay 	QUEUE_TYPE_INT, /* GAUDI_QUEUE_ID_NIC_7_2 */
342e65e175bSOded Gabbay 	QUEUE_TYPE_INT, /* GAUDI_QUEUE_ID_NIC_7_3 */
343e65e175bSOded Gabbay 	QUEUE_TYPE_INT, /* GAUDI_QUEUE_ID_NIC_8_0 */
344e65e175bSOded Gabbay 	QUEUE_TYPE_INT, /* GAUDI_QUEUE_ID_NIC_8_1 */
345e65e175bSOded Gabbay 	QUEUE_TYPE_INT, /* GAUDI_QUEUE_ID_NIC_8_2 */
346e65e175bSOded Gabbay 	QUEUE_TYPE_INT, /* GAUDI_QUEUE_ID_NIC_8_3 */
347e65e175bSOded Gabbay 	QUEUE_TYPE_INT, /* GAUDI_QUEUE_ID_NIC_9_0 */
348e65e175bSOded Gabbay 	QUEUE_TYPE_INT, /* GAUDI_QUEUE_ID_NIC_9_1 */
349e65e175bSOded Gabbay 	QUEUE_TYPE_INT, /* GAUDI_QUEUE_ID_NIC_9_2 */
350e65e175bSOded Gabbay 	QUEUE_TYPE_INT, /* GAUDI_QUEUE_ID_NIC_9_3 */
351e65e175bSOded Gabbay };
352e65e175bSOded Gabbay 
353e65e175bSOded Gabbay static struct hl_hw_obj_name_entry gaudi_so_id_to_str[] = {
354e65e175bSOded Gabbay 	{ .id = 0,  .name = "SYNC_OBJ_DMA_DOWN_FEEDBACK" },
355e65e175bSOded Gabbay 	{ .id = 1,  .name = "SYNC_OBJ_DMA_UP_FEEDBACK" },
356e65e175bSOded Gabbay 	{ .id = 2,  .name = "SYNC_OBJ_DMA_STATIC_DRAM_SRAM_FEEDBACK" },
357e65e175bSOded Gabbay 	{ .id = 3,  .name = "SYNC_OBJ_DMA_SRAM_DRAM_FEEDBACK" },
358e65e175bSOded Gabbay 	{ .id = 4,  .name = "SYNC_OBJ_FIRST_COMPUTE_FINISH" },
359e65e175bSOded Gabbay 	{ .id = 5,  .name = "SYNC_OBJ_HOST_DRAM_DONE" },
360e65e175bSOded Gabbay 	{ .id = 6,  .name = "SYNC_OBJ_DBG_CTR_DEPRECATED" },
361e65e175bSOded Gabbay 	{ .id = 7,  .name = "SYNC_OBJ_DMA_ACTIVATIONS_DRAM_SRAM_FEEDBACK" },
362e65e175bSOded Gabbay 	{ .id = 8,  .name = "SYNC_OBJ_ENGINE_SEM_MME_0" },
363e65e175bSOded Gabbay 	{ .id = 9,  .name = "SYNC_OBJ_ENGINE_SEM_MME_1" },
364e65e175bSOded Gabbay 	{ .id = 10, .name = "SYNC_OBJ_ENGINE_SEM_TPC_0" },
365e65e175bSOded Gabbay 	{ .id = 11, .name = "SYNC_OBJ_ENGINE_SEM_TPC_1" },
366e65e175bSOded Gabbay 	{ .id = 12, .name = "SYNC_OBJ_ENGINE_SEM_TPC_2" },
367e65e175bSOded Gabbay 	{ .id = 13, .name = "SYNC_OBJ_ENGINE_SEM_TPC_3" },
368e65e175bSOded Gabbay 	{ .id = 14, .name = "SYNC_OBJ_ENGINE_SEM_TPC_4" },
369e65e175bSOded Gabbay 	{ .id = 15, .name = "SYNC_OBJ_ENGINE_SEM_TPC_5" },
370e65e175bSOded Gabbay 	{ .id = 16, .name = "SYNC_OBJ_ENGINE_SEM_TPC_6" },
371e65e175bSOded Gabbay 	{ .id = 17, .name = "SYNC_OBJ_ENGINE_SEM_TPC_7" },
372e65e175bSOded Gabbay 	{ .id = 18, .name = "SYNC_OBJ_ENGINE_SEM_DMA_1" },
373e65e175bSOded Gabbay 	{ .id = 19, .name = "SYNC_OBJ_ENGINE_SEM_DMA_2" },
374e65e175bSOded Gabbay 	{ .id = 20, .name = "SYNC_OBJ_ENGINE_SEM_DMA_3" },
375e65e175bSOded Gabbay 	{ .id = 21, .name = "SYNC_OBJ_ENGINE_SEM_DMA_4" },
376e65e175bSOded Gabbay 	{ .id = 22, .name = "SYNC_OBJ_ENGINE_SEM_DMA_5" },
377e65e175bSOded Gabbay 	{ .id = 23, .name = "SYNC_OBJ_ENGINE_SEM_DMA_6" },
378e65e175bSOded Gabbay 	{ .id = 24, .name = "SYNC_OBJ_ENGINE_SEM_DMA_7" },
379e65e175bSOded Gabbay 	{ .id = 25, .name = "SYNC_OBJ_DBG_CTR_0" },
380e65e175bSOded Gabbay 	{ .id = 26, .name = "SYNC_OBJ_DBG_CTR_1" },
381e65e175bSOded Gabbay };
382e65e175bSOded Gabbay 
383e65e175bSOded Gabbay static struct hl_hw_obj_name_entry gaudi_monitor_id_to_str[] = {
384e65e175bSOded Gabbay 	{ .id = 200, .name = "MON_OBJ_DMA_DOWN_FEEDBACK_RESET" },
385e65e175bSOded Gabbay 	{ .id = 201, .name = "MON_OBJ_DMA_UP_FEEDBACK_RESET" },
386e65e175bSOded Gabbay 	{ .id = 203, .name = "MON_OBJ_DRAM_TO_SRAM_QUEUE_FENCE" },
387e65e175bSOded Gabbay 	{ .id = 204, .name = "MON_OBJ_TPC_0_CLK_GATE" },
388e65e175bSOded Gabbay 	{ .id = 205, .name = "MON_OBJ_TPC_1_CLK_GATE" },
389e65e175bSOded Gabbay 	{ .id = 206, .name = "MON_OBJ_TPC_2_CLK_GATE" },
390e65e175bSOded Gabbay 	{ .id = 207, .name = "MON_OBJ_TPC_3_CLK_GATE" },
391e65e175bSOded Gabbay 	{ .id = 208, .name = "MON_OBJ_TPC_4_CLK_GATE" },
392e65e175bSOded Gabbay 	{ .id = 209, .name = "MON_OBJ_TPC_5_CLK_GATE" },
393e65e175bSOded Gabbay 	{ .id = 210, .name = "MON_OBJ_TPC_6_CLK_GATE" },
394e65e175bSOded Gabbay 	{ .id = 211, .name = "MON_OBJ_TPC_7_CLK_GATE" },
395e65e175bSOded Gabbay };
396e65e175bSOded Gabbay 
397e65e175bSOded Gabbay static s64 gaudi_state_dump_specs_props[] = {
398e65e175bSOded Gabbay 	[SP_SYNC_OBJ_BASE_ADDR] = mmSYNC_MNGR_E_N_SYNC_MNGR_OBJS_SOB_OBJ_0,
399e65e175bSOded Gabbay 	[SP_NEXT_SYNC_OBJ_ADDR] = NEXT_SYNC_OBJ_ADDR_INTERVAL,
400e65e175bSOded Gabbay 	[SP_SYNC_OBJ_AMOUNT] = NUM_OF_SOB_IN_BLOCK,
401e65e175bSOded Gabbay 	[SP_MON_OBJ_WR_ADDR_LOW] =
402e65e175bSOded Gabbay 		mmSYNC_MNGR_E_N_SYNC_MNGR_OBJS_MON_PAY_ADDRL_0,
403e65e175bSOded Gabbay 	[SP_MON_OBJ_WR_ADDR_HIGH] =
404e65e175bSOded Gabbay 		mmSYNC_MNGR_E_N_SYNC_MNGR_OBJS_MON_PAY_ADDRH_0,
405e65e175bSOded Gabbay 	[SP_MON_OBJ_WR_DATA] = mmSYNC_MNGR_E_N_SYNC_MNGR_OBJS_MON_PAY_DATA_0,
406e65e175bSOded Gabbay 	[SP_MON_OBJ_ARM_DATA] = mmSYNC_MNGR_E_N_SYNC_MNGR_OBJS_MON_ARM_0,
407e65e175bSOded Gabbay 	[SP_MON_OBJ_STATUS] = mmSYNC_MNGR_E_N_SYNC_MNGR_OBJS_MON_STATUS_0,
408e65e175bSOded Gabbay 	[SP_MONITORS_AMOUNT] = NUM_OF_MONITORS_IN_BLOCK,
409e65e175bSOded Gabbay 	[SP_TPC0_CMDQ] = mmTPC0_QM_GLBL_CFG0,
410e65e175bSOded Gabbay 	[SP_TPC0_CFG_SO] = mmTPC0_CFG_QM_SYNC_OBJECT_ADDR,
411e65e175bSOded Gabbay 	[SP_NEXT_TPC] = mmTPC1_QM_GLBL_CFG0 - mmTPC0_QM_GLBL_CFG0,
412e65e175bSOded Gabbay 	[SP_MME_CMDQ] = mmMME0_QM_GLBL_CFG0,
413e65e175bSOded Gabbay 	[SP_MME_CFG_SO] = mmMME0_CTRL_ARCH_DESC_SYNC_OBJECT_ADDR_LOW_LOCAL,
414e65e175bSOded Gabbay 	[SP_NEXT_MME] = mmMME2_QM_GLBL_CFG0 - mmMME0_QM_GLBL_CFG0,
415e65e175bSOded Gabbay 	[SP_DMA_CMDQ] = mmDMA0_QM_GLBL_CFG0,
416e65e175bSOded Gabbay 	[SP_DMA_CFG_SO] = mmDMA0_CORE_WR_COMP_ADDR_LO,
417e65e175bSOded Gabbay 	[SP_DMA_QUEUES_OFFSET] = mmDMA1_QM_GLBL_CFG0 - mmDMA0_QM_GLBL_CFG0,
418e65e175bSOded Gabbay 	[SP_NUM_OF_MME_ENGINES] = NUM_OF_MME_ENGINES,
419e65e175bSOded Gabbay 	[SP_SUB_MME_ENG_NUM] = NUM_OF_MME_SUB_ENGINES,
420e65e175bSOded Gabbay 	[SP_NUM_OF_DMA_ENGINES] = NUM_OF_DMA_ENGINES,
421e65e175bSOded Gabbay 	[SP_NUM_OF_TPC_ENGINES] = NUM_OF_TPC_ENGINES,
422e65e175bSOded Gabbay 	[SP_ENGINE_NUM_OF_QUEUES] = NUM_OF_QUEUES,
423e65e175bSOded Gabbay 	[SP_ENGINE_NUM_OF_STREAMS] = NUM_OF_STREAMS,
424e65e175bSOded Gabbay 	[SP_ENGINE_NUM_OF_FENCES] = NUM_OF_FENCES,
425e65e175bSOded Gabbay 	[SP_FENCE0_CNT_OFFSET] =
426e65e175bSOded Gabbay 		mmDMA0_QM_CP_FENCE0_CNT_0 - mmDMA0_QM_GLBL_CFG0,
427e65e175bSOded Gabbay 	[SP_FENCE0_RDATA_OFFSET] =
428e65e175bSOded Gabbay 		mmDMA0_QM_CP_FENCE0_RDATA_0 - mmDMA0_QM_GLBL_CFG0,
429e65e175bSOded Gabbay 	[SP_CP_STS_OFFSET] = mmDMA0_QM_CP_STS_0 - mmDMA0_QM_GLBL_CFG0,
430e65e175bSOded Gabbay 	[SP_NUM_CORES] = 1,
431e65e175bSOded Gabbay };
432e65e175bSOded Gabbay 
433e65e175bSOded Gabbay static const int gaudi_queue_id_to_engine_id[] = {
434e65e175bSOded Gabbay 	[GAUDI_QUEUE_ID_DMA_0_0...GAUDI_QUEUE_ID_DMA_0_3] = GAUDI_ENGINE_ID_DMA_0,
435e65e175bSOded Gabbay 	[GAUDI_QUEUE_ID_DMA_1_0...GAUDI_QUEUE_ID_DMA_1_3] = GAUDI_ENGINE_ID_DMA_1,
436e65e175bSOded Gabbay 	[GAUDI_QUEUE_ID_CPU_PQ] = GAUDI_ENGINE_ID_SIZE,
437e65e175bSOded Gabbay 	[GAUDI_QUEUE_ID_DMA_2_0...GAUDI_QUEUE_ID_DMA_2_3] = GAUDI_ENGINE_ID_DMA_2,
438e65e175bSOded Gabbay 	[GAUDI_QUEUE_ID_DMA_3_0...GAUDI_QUEUE_ID_DMA_3_3] = GAUDI_ENGINE_ID_DMA_3,
439e65e175bSOded Gabbay 	[GAUDI_QUEUE_ID_DMA_4_0...GAUDI_QUEUE_ID_DMA_4_3] = GAUDI_ENGINE_ID_DMA_4,
440e65e175bSOded Gabbay 	[GAUDI_QUEUE_ID_DMA_5_0...GAUDI_QUEUE_ID_DMA_5_3] = GAUDI_ENGINE_ID_DMA_5,
441e65e175bSOded Gabbay 	[GAUDI_QUEUE_ID_DMA_6_0...GAUDI_QUEUE_ID_DMA_6_3] = GAUDI_ENGINE_ID_DMA_6,
442e65e175bSOded Gabbay 	[GAUDI_QUEUE_ID_DMA_7_0...GAUDI_QUEUE_ID_DMA_7_3] = GAUDI_ENGINE_ID_DMA_7,
443e65e175bSOded Gabbay 	[GAUDI_QUEUE_ID_MME_0_0...GAUDI_QUEUE_ID_MME_0_3] = GAUDI_ENGINE_ID_MME_0,
444e65e175bSOded Gabbay 	[GAUDI_QUEUE_ID_MME_1_0...GAUDI_QUEUE_ID_MME_1_3] = GAUDI_ENGINE_ID_MME_2,
445e65e175bSOded Gabbay 	[GAUDI_QUEUE_ID_TPC_0_0...GAUDI_QUEUE_ID_TPC_0_3] = GAUDI_ENGINE_ID_TPC_0,
446e65e175bSOded Gabbay 	[GAUDI_QUEUE_ID_TPC_1_0...GAUDI_QUEUE_ID_TPC_1_3] = GAUDI_ENGINE_ID_TPC_1,
447e65e175bSOded Gabbay 	[GAUDI_QUEUE_ID_TPC_2_0...GAUDI_QUEUE_ID_TPC_2_3] = GAUDI_ENGINE_ID_TPC_2,
448e65e175bSOded Gabbay 	[GAUDI_QUEUE_ID_TPC_3_0...GAUDI_QUEUE_ID_TPC_3_3] = GAUDI_ENGINE_ID_TPC_3,
449e65e175bSOded Gabbay 	[GAUDI_QUEUE_ID_TPC_4_0...GAUDI_QUEUE_ID_TPC_4_3] = GAUDI_ENGINE_ID_TPC_4,
450e65e175bSOded Gabbay 	[GAUDI_QUEUE_ID_TPC_5_0...GAUDI_QUEUE_ID_TPC_5_3] = GAUDI_ENGINE_ID_TPC_5,
451e65e175bSOded Gabbay 	[GAUDI_QUEUE_ID_TPC_6_0...GAUDI_QUEUE_ID_TPC_6_3] = GAUDI_ENGINE_ID_TPC_6,
452e65e175bSOded Gabbay 	[GAUDI_QUEUE_ID_TPC_7_0...GAUDI_QUEUE_ID_TPC_7_3] = GAUDI_ENGINE_ID_TPC_7,
453e65e175bSOded Gabbay 	[GAUDI_QUEUE_ID_NIC_0_0...GAUDI_QUEUE_ID_NIC_0_3] = GAUDI_ENGINE_ID_NIC_0,
454e65e175bSOded Gabbay 	[GAUDI_QUEUE_ID_NIC_1_0...GAUDI_QUEUE_ID_NIC_1_3] = GAUDI_ENGINE_ID_NIC_1,
455e65e175bSOded Gabbay 	[GAUDI_QUEUE_ID_NIC_2_0...GAUDI_QUEUE_ID_NIC_2_3] = GAUDI_ENGINE_ID_NIC_2,
456e65e175bSOded Gabbay 	[GAUDI_QUEUE_ID_NIC_3_0...GAUDI_QUEUE_ID_NIC_3_3] = GAUDI_ENGINE_ID_NIC_3,
457e65e175bSOded Gabbay 	[GAUDI_QUEUE_ID_NIC_4_0...GAUDI_QUEUE_ID_NIC_4_3] = GAUDI_ENGINE_ID_NIC_4,
458e65e175bSOded Gabbay 	[GAUDI_QUEUE_ID_NIC_5_0...GAUDI_QUEUE_ID_NIC_5_3] = GAUDI_ENGINE_ID_NIC_5,
459e65e175bSOded Gabbay 	[GAUDI_QUEUE_ID_NIC_6_0...GAUDI_QUEUE_ID_NIC_6_3] = GAUDI_ENGINE_ID_NIC_6,
460e65e175bSOded Gabbay 	[GAUDI_QUEUE_ID_NIC_7_0...GAUDI_QUEUE_ID_NIC_7_3] = GAUDI_ENGINE_ID_NIC_7,
461e65e175bSOded Gabbay 	[GAUDI_QUEUE_ID_NIC_8_0...GAUDI_QUEUE_ID_NIC_8_3] = GAUDI_ENGINE_ID_NIC_8,
462e65e175bSOded Gabbay 	[GAUDI_QUEUE_ID_NIC_9_0...GAUDI_QUEUE_ID_NIC_9_3] = GAUDI_ENGINE_ID_NIC_9,
463e65e175bSOded Gabbay };
464e65e175bSOded Gabbay 
465e65e175bSOded Gabbay /* The order here is opposite to the order of the indexing in the h/w.
466e65e175bSOded Gabbay  * i.e. SYNC_MGR_W_S is actually 0, SYNC_MGR_E_S is 1, etc.
467e65e175bSOded Gabbay  */
468e65e175bSOded Gabbay static const char * const gaudi_sync_manager_names[] = {
469e65e175bSOded Gabbay 	"SYNC_MGR_E_N",
470e65e175bSOded Gabbay 	"SYNC_MGR_W_N",
471e65e175bSOded Gabbay 	"SYNC_MGR_E_S",
472e65e175bSOded Gabbay 	"SYNC_MGR_W_S",
473e65e175bSOded Gabbay 	NULL
474e65e175bSOded Gabbay };
475e65e175bSOded Gabbay 
476e65e175bSOded Gabbay struct ecc_info_extract_params {
477e65e175bSOded Gabbay 	u64 block_address;
478e65e175bSOded Gabbay 	u32 num_memories;
479e65e175bSOded Gabbay 	bool derr;
480e65e175bSOded Gabbay };
481e65e175bSOded Gabbay 
482e65e175bSOded Gabbay static int gaudi_mmu_update_asid_hop0_addr(struct hl_device *hdev, u32 asid,
483e65e175bSOded Gabbay 								u64 phys_addr);
484e65e175bSOded Gabbay static int gaudi_send_job_on_qman0(struct hl_device *hdev,
485e65e175bSOded Gabbay 					struct hl_cs_job *job);
486e65e175bSOded Gabbay static int gaudi_memset_device_memory(struct hl_device *hdev, u64 addr,
487e65e175bSOded Gabbay 					u32 size, u64 val);
488e65e175bSOded Gabbay static int gaudi_memset_registers(struct hl_device *hdev, u64 reg_base,
489e65e175bSOded Gabbay 					u32 num_regs, u32 val);
490e65e175bSOded Gabbay static int gaudi_run_tpc_kernel(struct hl_device *hdev, u64 tpc_kernel,
491e65e175bSOded Gabbay 				u32 tpc_id);
492e65e175bSOded Gabbay static int gaudi_mmu_clear_pgt_range(struct hl_device *hdev);
493e65e175bSOded Gabbay static int gaudi_cpucp_info_get(struct hl_device *hdev);
494e65e175bSOded Gabbay static void gaudi_disable_clock_gating(struct hl_device *hdev);
495e65e175bSOded Gabbay static void gaudi_mmu_prepare(struct hl_device *hdev, u32 asid);
496e65e175bSOded Gabbay static u32 gaudi_gen_signal_cb(struct hl_device *hdev, void *data, u16 sob_id,
497e65e175bSOded Gabbay 				u32 size, bool eb);
498e65e175bSOded Gabbay static u32 gaudi_gen_wait_cb(struct hl_device *hdev,
499e65e175bSOded Gabbay 				struct hl_gen_wait_properties *prop);
500e65e175bSOded Gabbay static inline enum hl_collective_mode
501e65e175bSOded Gabbay get_collective_mode(struct hl_device *hdev, u32 queue_id)
502e65e175bSOded Gabbay {
503e65e175bSOded Gabbay 	if (gaudi_queue_type[queue_id] == QUEUE_TYPE_EXT)
504e65e175bSOded Gabbay 		return HL_COLLECTIVE_MASTER;
505e65e175bSOded Gabbay 
506e65e175bSOded Gabbay 	if (queue_id >= GAUDI_QUEUE_ID_DMA_5_0 &&
507e65e175bSOded Gabbay 			queue_id <= GAUDI_QUEUE_ID_DMA_5_3)
508e65e175bSOded Gabbay 		return HL_COLLECTIVE_SLAVE;
509e65e175bSOded Gabbay 
510e65e175bSOded Gabbay 	if (queue_id >= GAUDI_QUEUE_ID_TPC_7_0 &&
511e65e175bSOded Gabbay 			queue_id <= GAUDI_QUEUE_ID_TPC_7_3)
512e65e175bSOded Gabbay 		return HL_COLLECTIVE_SLAVE;
513e65e175bSOded Gabbay 
514e65e175bSOded Gabbay 	if (queue_id >= GAUDI_QUEUE_ID_NIC_0_0 &&
515e65e175bSOded Gabbay 			queue_id <= GAUDI_QUEUE_ID_NIC_9_3)
516e65e175bSOded Gabbay 		return HL_COLLECTIVE_SLAVE;
517e65e175bSOded Gabbay 
518e65e175bSOded Gabbay 	return HL_COLLECTIVE_NOT_SUPPORTED;
519e65e175bSOded Gabbay }
520e65e175bSOded Gabbay 
521e65e175bSOded Gabbay static inline void set_default_power_values(struct hl_device *hdev)
522e65e175bSOded Gabbay {
523e65e175bSOded Gabbay 	struct asic_fixed_properties *prop = &hdev->asic_prop;
524e65e175bSOded Gabbay 
525e65e175bSOded Gabbay 	if (hdev->card_type == cpucp_card_type_pmc) {
526e65e175bSOded Gabbay 		prop->max_power_default = MAX_POWER_DEFAULT_PMC;
527e65e175bSOded Gabbay 
528e65e175bSOded Gabbay 		if (prop->fw_security_enabled)
529e65e175bSOded Gabbay 			prop->dc_power_default = DC_POWER_DEFAULT_PMC_SEC;
530e65e175bSOded Gabbay 		else
531e65e175bSOded Gabbay 			prop->dc_power_default = DC_POWER_DEFAULT_PMC;
532e65e175bSOded Gabbay 	} else {
533e65e175bSOded Gabbay 		prop->max_power_default = MAX_POWER_DEFAULT_PCI;
534e65e175bSOded Gabbay 		prop->dc_power_default = DC_POWER_DEFAULT_PCI;
535e65e175bSOded Gabbay 	}
536e65e175bSOded Gabbay }
537e65e175bSOded Gabbay 
538e65e175bSOded Gabbay static int gaudi_set_fixed_properties(struct hl_device *hdev)
539e65e175bSOded Gabbay {
540e65e175bSOded Gabbay 	struct asic_fixed_properties *prop = &hdev->asic_prop;
541e65e175bSOded Gabbay 	u32 num_sync_stream_queues = 0;
542e65e175bSOded Gabbay 	int i;
543e65e175bSOded Gabbay 
544e65e175bSOded Gabbay 	prop->max_queues = GAUDI_QUEUE_ID_SIZE;
545e65e175bSOded Gabbay 	prop->hw_queues_props = kcalloc(prop->max_queues,
546e65e175bSOded Gabbay 			sizeof(struct hw_queue_properties),
547e65e175bSOded Gabbay 			GFP_KERNEL);
548e65e175bSOded Gabbay 
549e65e175bSOded Gabbay 	if (!prop->hw_queues_props)
550e65e175bSOded Gabbay 		return -ENOMEM;
551e65e175bSOded Gabbay 
552e65e175bSOded Gabbay 	for (i = 0 ; i < prop->max_queues ; i++) {
553e65e175bSOded Gabbay 		if (gaudi_queue_type[i] == QUEUE_TYPE_EXT) {
554e65e175bSOded Gabbay 			prop->hw_queues_props[i].type = QUEUE_TYPE_EXT;
555e65e175bSOded Gabbay 			prop->hw_queues_props[i].driver_only = 0;
556e65e175bSOded Gabbay 			prop->hw_queues_props[i].supports_sync_stream = 1;
557e65e175bSOded Gabbay 			prop->hw_queues_props[i].cb_alloc_flags =
558e65e175bSOded Gabbay 				CB_ALLOC_KERNEL;
559e65e175bSOded Gabbay 			num_sync_stream_queues++;
560e65e175bSOded Gabbay 		} else if (gaudi_queue_type[i] == QUEUE_TYPE_CPU) {
561e65e175bSOded Gabbay 			prop->hw_queues_props[i].type = QUEUE_TYPE_CPU;
562e65e175bSOded Gabbay 			prop->hw_queues_props[i].driver_only = 1;
563e65e175bSOded Gabbay 			prop->hw_queues_props[i].supports_sync_stream = 0;
564e65e175bSOded Gabbay 			prop->hw_queues_props[i].cb_alloc_flags =
565e65e175bSOded Gabbay 				CB_ALLOC_KERNEL;
566e65e175bSOded Gabbay 		} else if (gaudi_queue_type[i] == QUEUE_TYPE_INT) {
567e65e175bSOded Gabbay 			prop->hw_queues_props[i].type = QUEUE_TYPE_INT;
568e65e175bSOded Gabbay 			prop->hw_queues_props[i].driver_only = 0;
569e65e175bSOded Gabbay 			prop->hw_queues_props[i].supports_sync_stream = 0;
570e65e175bSOded Gabbay 			prop->hw_queues_props[i].cb_alloc_flags =
571e65e175bSOded Gabbay 				CB_ALLOC_USER;
572e65e175bSOded Gabbay 
573e65e175bSOded Gabbay 		}
574e65e175bSOded Gabbay 		prop->hw_queues_props[i].collective_mode =
575e65e175bSOded Gabbay 						get_collective_mode(hdev, i);
576e65e175bSOded Gabbay 	}
577e65e175bSOded Gabbay 
578e65e175bSOded Gabbay 	prop->cache_line_size = DEVICE_CACHE_LINE_SIZE;
579e65e175bSOded Gabbay 	prop->cfg_base_address = CFG_BASE;
580e65e175bSOded Gabbay 	prop->device_dma_offset_for_host_access = HOST_PHYS_BASE;
581e65e175bSOded Gabbay 	prop->host_base_address = HOST_PHYS_BASE;
582e65e175bSOded Gabbay 	prop->host_end_address = prop->host_base_address + HOST_PHYS_SIZE;
583e65e175bSOded Gabbay 	prop->completion_queues_count = NUMBER_OF_CMPLT_QUEUES;
584e65e175bSOded Gabbay 	prop->completion_mode = HL_COMPLETION_MODE_JOB;
585e65e175bSOded Gabbay 	prop->collective_first_sob = 0;
586e65e175bSOded Gabbay 	prop->collective_first_mon = 0;
587e65e175bSOded Gabbay 
588e65e175bSOded Gabbay 	/* 2 SOBs per internal queue stream are reserved for collective */
589e65e175bSOded Gabbay 	prop->sync_stream_first_sob =
590e65e175bSOded Gabbay 			ALIGN(NUMBER_OF_SOBS_IN_GRP, HL_MAX_SOBS_PER_MONITOR)
591e65e175bSOded Gabbay 			* QMAN_STREAMS * HL_RSVD_SOBS;
592e65e175bSOded Gabbay 
593e65e175bSOded Gabbay 	/* 1 monitor per internal queue stream are reserved for collective
594e65e175bSOded Gabbay 	 * 2 monitors per external queue stream are reserved for collective
595e65e175bSOded Gabbay 	 */
596e65e175bSOded Gabbay 	prop->sync_stream_first_mon =
597e65e175bSOded Gabbay 			(NUMBER_OF_COLLECTIVE_QUEUES * QMAN_STREAMS) +
598e65e175bSOded Gabbay 			(NUMBER_OF_EXT_HW_QUEUES * 2);
599e65e175bSOded Gabbay 
600e65e175bSOded Gabbay 	prop->dram_base_address = DRAM_PHYS_BASE;
601e65e175bSOded Gabbay 	prop->dram_size = GAUDI_HBM_SIZE_32GB;
602e65e175bSOded Gabbay 	prop->dram_end_address = prop->dram_base_address + prop->dram_size;
603e65e175bSOded Gabbay 	prop->dram_user_base_address = DRAM_BASE_ADDR_USER;
604e65e175bSOded Gabbay 
605e65e175bSOded Gabbay 	prop->sram_base_address = SRAM_BASE_ADDR;
606e65e175bSOded Gabbay 	prop->sram_size = SRAM_SIZE;
607e65e175bSOded Gabbay 	prop->sram_end_address = prop->sram_base_address + prop->sram_size;
608e65e175bSOded Gabbay 	prop->sram_user_base_address =
609e65e175bSOded Gabbay 			prop->sram_base_address + SRAM_USER_BASE_OFFSET;
610e65e175bSOded Gabbay 
611e65e175bSOded Gabbay 	prop->mmu_cache_mng_addr = MMU_CACHE_MNG_ADDR;
612e65e175bSOded Gabbay 	prop->mmu_cache_mng_size = MMU_CACHE_MNG_SIZE;
613e65e175bSOded Gabbay 
614e65e175bSOded Gabbay 	prop->mmu_pgt_addr = MMU_PAGE_TABLES_ADDR;
615e65e175bSOded Gabbay 	if (hdev->pldm)
616e65e175bSOded Gabbay 		prop->mmu_pgt_size = 0x800000; /* 8MB */
617e65e175bSOded Gabbay 	else
618e65e175bSOded Gabbay 		prop->mmu_pgt_size = MMU_PAGE_TABLES_SIZE;
619e65e175bSOded Gabbay 	prop->mmu_pte_size = HL_PTE_SIZE;
620e65e175bSOded Gabbay 	prop->mmu_hop_table_size = HOP_TABLE_SIZE_512_PTE;
621e65e175bSOded Gabbay 	prop->mmu_hop0_tables_total_size = HOP0_512_PTE_TABLES_TOTAL_SIZE;
622e65e175bSOded Gabbay 	prop->dram_page_size = PAGE_SIZE_2MB;
623e65e175bSOded Gabbay 	prop->device_mem_alloc_default_page_size = prop->dram_page_size;
624e65e175bSOded Gabbay 	prop->dram_supports_virtual_memory = false;
625e65e175bSOded Gabbay 
626e65e175bSOded Gabbay 	prop->pmmu.hop_shifts[MMU_HOP0] = MMU_V1_1_HOP0_SHIFT;
627e65e175bSOded Gabbay 	prop->pmmu.hop_shifts[MMU_HOP1] = MMU_V1_1_HOP1_SHIFT;
628e65e175bSOded Gabbay 	prop->pmmu.hop_shifts[MMU_HOP2] = MMU_V1_1_HOP2_SHIFT;
629e65e175bSOded Gabbay 	prop->pmmu.hop_shifts[MMU_HOP3] = MMU_V1_1_HOP3_SHIFT;
630e65e175bSOded Gabbay 	prop->pmmu.hop_shifts[MMU_HOP4] = MMU_V1_1_HOP4_SHIFT;
631e65e175bSOded Gabbay 	prop->pmmu.hop_masks[MMU_HOP0] = MMU_V1_1_HOP0_MASK;
632e65e175bSOded Gabbay 	prop->pmmu.hop_masks[MMU_HOP1] = MMU_V1_1_HOP1_MASK;
633e65e175bSOded Gabbay 	prop->pmmu.hop_masks[MMU_HOP2] = MMU_V1_1_HOP2_MASK;
634e65e175bSOded Gabbay 	prop->pmmu.hop_masks[MMU_HOP3] = MMU_V1_1_HOP3_MASK;
635e65e175bSOded Gabbay 	prop->pmmu.hop_masks[MMU_HOP4] = MMU_V1_1_HOP4_MASK;
636e65e175bSOded Gabbay 	prop->pmmu.start_addr = VA_HOST_SPACE_START;
637e65e175bSOded Gabbay 	prop->pmmu.end_addr =
638e65e175bSOded Gabbay 			(VA_HOST_SPACE_START + VA_HOST_SPACE_SIZE / 2) - 1;
639e65e175bSOded Gabbay 	prop->pmmu.page_size = PAGE_SIZE_4KB;
640e65e175bSOded Gabbay 	prop->pmmu.num_hops = MMU_ARCH_5_HOPS;
641e65e175bSOded Gabbay 	prop->pmmu.last_mask = LAST_MASK;
642e65e175bSOded Gabbay 	/* TODO: will be duplicated until implementing per-MMU props */
643e65e175bSOded Gabbay 	prop->pmmu.hop_table_size = prop->mmu_hop_table_size;
644e65e175bSOded Gabbay 	prop->pmmu.hop0_tables_total_size = prop->mmu_hop0_tables_total_size;
645e65e175bSOded Gabbay 
646e65e175bSOded Gabbay 	/* PMMU and HPMMU are the same except of page size */
647e65e175bSOded Gabbay 	memcpy(&prop->pmmu_huge, &prop->pmmu, sizeof(prop->pmmu));
648e65e175bSOded Gabbay 	prop->pmmu_huge.page_size = PAGE_SIZE_2MB;
649e65e175bSOded Gabbay 
650e65e175bSOded Gabbay 	/* shifts and masks are the same in PMMU and DMMU */
651e65e175bSOded Gabbay 	memcpy(&prop->dmmu, &prop->pmmu, sizeof(prop->pmmu));
652e65e175bSOded Gabbay 	prop->dmmu.start_addr = (VA_HOST_SPACE_START + VA_HOST_SPACE_SIZE / 2);
653e65e175bSOded Gabbay 	prop->dmmu.end_addr = VA_HOST_SPACE_END;
654e65e175bSOded Gabbay 	prop->dmmu.page_size = PAGE_SIZE_2MB;
655e65e175bSOded Gabbay 
656e65e175bSOded Gabbay 	prop->cfg_size = CFG_SIZE;
657e65e175bSOded Gabbay 	prop->max_asid = MAX_ASID;
658e65e175bSOded Gabbay 	prop->num_of_events = GAUDI_EVENT_SIZE;
659f7f0085eSKoby Elbaz 	prop->max_num_of_engines = GAUDI_ENGINE_ID_SIZE;
660e65e175bSOded Gabbay 	prop->tpc_enabled_mask = TPC_ENABLED_MASK;
661e65e175bSOded Gabbay 
662e65e175bSOded Gabbay 	set_default_power_values(hdev);
663e65e175bSOded Gabbay 
664e65e175bSOded Gabbay 	prop->cb_pool_cb_cnt = GAUDI_CB_POOL_CB_CNT;
665e65e175bSOded Gabbay 	prop->cb_pool_cb_size = GAUDI_CB_POOL_CB_SIZE;
666e65e175bSOded Gabbay 
667e65e175bSOded Gabbay 	prop->pcie_dbi_base_address = mmPCIE_DBI_BASE;
668e65e175bSOded Gabbay 	prop->pcie_aux_dbi_reg_addr = CFG_BASE + mmPCIE_AUX_DBI;
669e65e175bSOded Gabbay 
670e65e175bSOded Gabbay 	strncpy(prop->cpucp_info.card_name, GAUDI_DEFAULT_CARD_NAME,
671e65e175bSOded Gabbay 					CARD_NAME_MAX_LEN);
672e65e175bSOded Gabbay 
673e65e175bSOded Gabbay 	prop->max_pending_cs = GAUDI_MAX_PENDING_CS;
674e65e175bSOded Gabbay 
675e65e175bSOded Gabbay 	prop->first_available_user_sob[HL_GAUDI_WS_DCORE] =
676e65e175bSOded Gabbay 			prop->sync_stream_first_sob +
677e65e175bSOded Gabbay 			(num_sync_stream_queues * HL_RSVD_SOBS);
678e65e175bSOded Gabbay 	prop->first_available_user_mon[HL_GAUDI_WS_DCORE] =
679e65e175bSOded Gabbay 			prop->sync_stream_first_mon +
680e65e175bSOded Gabbay 			(num_sync_stream_queues * HL_RSVD_MONS);
681e65e175bSOded Gabbay 
682e65e175bSOded Gabbay 	prop->first_available_user_interrupt = USHRT_MAX;
6834713ace3SOfir Bitton 	prop->tpc_interrupt_id = USHRT_MAX;
684e65e175bSOded Gabbay 
685*802f25b6STal Cohen 	/* single msi */
686*802f25b6STal Cohen 	prop->eq_interrupt_id = 0;
687*802f25b6STal Cohen 
688e65e175bSOded Gabbay 	for (i = 0 ; i < HL_MAX_DCORES ; i++)
689e65e175bSOded Gabbay 		prop->first_available_cq[i] = USHRT_MAX;
690e65e175bSOded Gabbay 
691e65e175bSOded Gabbay 	prop->fw_cpu_boot_dev_sts0_valid = false;
692e65e175bSOded Gabbay 	prop->fw_cpu_boot_dev_sts1_valid = false;
693e65e175bSOded Gabbay 	prop->hard_reset_done_by_fw = false;
694e65e175bSOded Gabbay 	prop->gic_interrupts_enable = true;
695e65e175bSOded Gabbay 
696e65e175bSOded Gabbay 	prop->server_type = HL_SERVER_TYPE_UNKNOWN;
697e65e175bSOded Gabbay 
698e65e175bSOded Gabbay 	prop->clk_pll_index = HL_GAUDI_MME_PLL;
699e65e175bSOded Gabbay 	prop->max_freq_value = GAUDI_MAX_CLK_FREQ;
700e65e175bSOded Gabbay 
701e65e175bSOded Gabbay 	prop->use_get_power_for_reset_history = true;
702e65e175bSOded Gabbay 
703e65e175bSOded Gabbay 	prop->configurable_stop_on_err = true;
704e65e175bSOded Gabbay 
705e65e175bSOded Gabbay 	prop->set_max_power_on_device_init = true;
706e65e175bSOded Gabbay 
707e65e175bSOded Gabbay 	prop->dma_mask = 48;
708e65e175bSOded Gabbay 
70920faaeecSOhad Sharabi 	prop->hbw_flush_reg = mmPCIE_WRAP_RR_ELBI_RD_SEC_REG_CTRL;
71020faaeecSOhad Sharabi 
711e65e175bSOded Gabbay 	return 0;
712e65e175bSOded Gabbay }
713e65e175bSOded Gabbay 
714e65e175bSOded Gabbay static int gaudi_pci_bars_map(struct hl_device *hdev)
715e65e175bSOded Gabbay {
716e65e175bSOded Gabbay 	static const char * const name[] = {"SRAM", "CFG", "HBM"};
717e65e175bSOded Gabbay 	bool is_wc[3] = {false, false, true};
718e65e175bSOded Gabbay 	int rc;
719e65e175bSOded Gabbay 
720e65e175bSOded Gabbay 	rc = hl_pci_bars_map(hdev, name, is_wc);
721e65e175bSOded Gabbay 	if (rc)
722e65e175bSOded Gabbay 		return rc;
723e65e175bSOded Gabbay 
724e65e175bSOded Gabbay 	hdev->rmmio = hdev->pcie_bar[CFG_BAR_ID] +
725e65e175bSOded Gabbay 			(CFG_BASE - SPI_FLASH_BASE_ADDR);
726e65e175bSOded Gabbay 
727e65e175bSOded Gabbay 	return 0;
728e65e175bSOded Gabbay }
729e65e175bSOded Gabbay 
730e65e175bSOded Gabbay static u64 gaudi_set_hbm_bar_base(struct hl_device *hdev, u64 addr)
731e65e175bSOded Gabbay {
732e65e175bSOded Gabbay 	struct gaudi_device *gaudi = hdev->asic_specific;
733e65e175bSOded Gabbay 	struct hl_inbound_pci_region pci_region;
734e65e175bSOded Gabbay 	u64 old_addr = addr;
735e65e175bSOded Gabbay 	int rc;
736e65e175bSOded Gabbay 
737e65e175bSOded Gabbay 	if ((gaudi) && (gaudi->hbm_bar_cur_addr == addr))
738e65e175bSOded Gabbay 		return old_addr;
739e65e175bSOded Gabbay 
740e65e175bSOded Gabbay 	if (hdev->asic_prop.iatu_done_by_fw)
741e65e175bSOded Gabbay 		return U64_MAX;
742e65e175bSOded Gabbay 
743e65e175bSOded Gabbay 	/* Inbound Region 2 - Bar 4 - Point to HBM */
744e65e175bSOded Gabbay 	pci_region.mode = PCI_BAR_MATCH_MODE;
745e65e175bSOded Gabbay 	pci_region.bar = HBM_BAR_ID;
746e65e175bSOded Gabbay 	pci_region.addr = addr;
747e65e175bSOded Gabbay 	rc = hl_pci_set_inbound_region(hdev, 2, &pci_region);
748e65e175bSOded Gabbay 	if (rc)
749e65e175bSOded Gabbay 		return U64_MAX;
750e65e175bSOded Gabbay 
751e65e175bSOded Gabbay 	if (gaudi) {
752e65e175bSOded Gabbay 		old_addr = gaudi->hbm_bar_cur_addr;
753e65e175bSOded Gabbay 		gaudi->hbm_bar_cur_addr = addr;
754e65e175bSOded Gabbay 	}
755e65e175bSOded Gabbay 
756e65e175bSOded Gabbay 	return old_addr;
757e65e175bSOded Gabbay }
758e65e175bSOded Gabbay 
759e65e175bSOded Gabbay static int gaudi_init_iatu(struct hl_device *hdev)
760e65e175bSOded Gabbay {
761e65e175bSOded Gabbay 	struct hl_inbound_pci_region inbound_region;
762e65e175bSOded Gabbay 	struct hl_outbound_pci_region outbound_region;
763e65e175bSOded Gabbay 	int rc;
764e65e175bSOded Gabbay 
765e65e175bSOded Gabbay 	if (hdev->asic_prop.iatu_done_by_fw)
766e65e175bSOded Gabbay 		return 0;
767e65e175bSOded Gabbay 
768e65e175bSOded Gabbay 	/* Inbound Region 0 - Bar 0 - Point to SRAM + CFG */
769e65e175bSOded Gabbay 	inbound_region.mode = PCI_BAR_MATCH_MODE;
770e65e175bSOded Gabbay 	inbound_region.bar = SRAM_BAR_ID;
771e65e175bSOded Gabbay 	inbound_region.addr = SRAM_BASE_ADDR;
772e65e175bSOded Gabbay 	rc = hl_pci_set_inbound_region(hdev, 0, &inbound_region);
773e65e175bSOded Gabbay 	if (rc)
774e65e175bSOded Gabbay 		goto done;
775e65e175bSOded Gabbay 
776e65e175bSOded Gabbay 	/* Inbound Region 1 - Bar 2 - Point to SPI FLASH */
777e65e175bSOded Gabbay 	inbound_region.mode = PCI_BAR_MATCH_MODE;
778e65e175bSOded Gabbay 	inbound_region.bar = CFG_BAR_ID;
779e65e175bSOded Gabbay 	inbound_region.addr = SPI_FLASH_BASE_ADDR;
780e65e175bSOded Gabbay 	rc = hl_pci_set_inbound_region(hdev, 1, &inbound_region);
781e65e175bSOded Gabbay 	if (rc)
782e65e175bSOded Gabbay 		goto done;
783e65e175bSOded Gabbay 
784e65e175bSOded Gabbay 	/* Inbound Region 2 - Bar 4 - Point to HBM */
785e65e175bSOded Gabbay 	inbound_region.mode = PCI_BAR_MATCH_MODE;
786e65e175bSOded Gabbay 	inbound_region.bar = HBM_BAR_ID;
787e65e175bSOded Gabbay 	inbound_region.addr = DRAM_PHYS_BASE;
788e65e175bSOded Gabbay 	rc = hl_pci_set_inbound_region(hdev, 2, &inbound_region);
789e65e175bSOded Gabbay 	if (rc)
790e65e175bSOded Gabbay 		goto done;
791e65e175bSOded Gabbay 
792e65e175bSOded Gabbay 	/* Outbound Region 0 - Point to Host */
793e65e175bSOded Gabbay 	outbound_region.addr = HOST_PHYS_BASE;
794e65e175bSOded Gabbay 	outbound_region.size = HOST_PHYS_SIZE;
795e65e175bSOded Gabbay 	rc = hl_pci_set_outbound_region(hdev, &outbound_region);
796e65e175bSOded Gabbay 
797e65e175bSOded Gabbay done:
798e65e175bSOded Gabbay 	return rc;
799e65e175bSOded Gabbay }
800e65e175bSOded Gabbay 
801e65e175bSOded Gabbay static enum hl_device_hw_state gaudi_get_hw_state(struct hl_device *hdev)
802e65e175bSOded Gabbay {
803e65e175bSOded Gabbay 	return RREG32(mmHW_STATE);
804e65e175bSOded Gabbay }
805e65e175bSOded Gabbay 
806e65e175bSOded Gabbay static int gaudi_early_init(struct hl_device *hdev)
807e65e175bSOded Gabbay {
808e65e175bSOded Gabbay 	struct asic_fixed_properties *prop = &hdev->asic_prop;
809e65e175bSOded Gabbay 	struct pci_dev *pdev = hdev->pdev;
810e65e175bSOded Gabbay 	resource_size_t pci_bar_size;
811e65e175bSOded Gabbay 	u32 fw_boot_status;
812e65e175bSOded Gabbay 	int rc;
813e65e175bSOded Gabbay 
814e65e175bSOded Gabbay 	rc = gaudi_set_fixed_properties(hdev);
815e65e175bSOded Gabbay 	if (rc) {
816e65e175bSOded Gabbay 		dev_err(hdev->dev, "Failed setting fixed properties\n");
817e65e175bSOded Gabbay 		return rc;
818e65e175bSOded Gabbay 	}
819e65e175bSOded Gabbay 
820e65e175bSOded Gabbay 	/* Check BAR sizes */
821e65e175bSOded Gabbay 	pci_bar_size = pci_resource_len(pdev, SRAM_BAR_ID);
822e65e175bSOded Gabbay 
823e65e175bSOded Gabbay 	if (pci_bar_size != SRAM_BAR_SIZE) {
824e65e175bSOded Gabbay 		dev_err(hdev->dev, "Not " HL_NAME "? BAR %d size %pa, expecting %llu\n",
825e65e175bSOded Gabbay 			SRAM_BAR_ID, &pci_bar_size, SRAM_BAR_SIZE);
826e65e175bSOded Gabbay 		rc = -ENODEV;
827e65e175bSOded Gabbay 		goto free_queue_props;
828e65e175bSOded Gabbay 	}
829e65e175bSOded Gabbay 
830e65e175bSOded Gabbay 	pci_bar_size = pci_resource_len(pdev, CFG_BAR_ID);
831e65e175bSOded Gabbay 
832e65e175bSOded Gabbay 	if (pci_bar_size != CFG_BAR_SIZE) {
833e65e175bSOded Gabbay 		dev_err(hdev->dev, "Not " HL_NAME "? BAR %d size %pa, expecting %llu\n",
834e65e175bSOded Gabbay 			CFG_BAR_ID, &pci_bar_size, CFG_BAR_SIZE);
835e65e175bSOded Gabbay 		rc = -ENODEV;
836e65e175bSOded Gabbay 		goto free_queue_props;
837e65e175bSOded Gabbay 	}
838e65e175bSOded Gabbay 
839e65e175bSOded Gabbay 	prop->dram_pci_bar_size = pci_resource_len(pdev, HBM_BAR_ID);
840e65e175bSOded Gabbay 	hdev->dram_pci_bar_start = pci_resource_start(pdev, HBM_BAR_ID);
841e65e175bSOded Gabbay 
842e65e175bSOded Gabbay 	/* If FW security is enabled at this point it means no access to ELBI */
843e65e175bSOded Gabbay 	if (hdev->asic_prop.fw_security_enabled) {
844e65e175bSOded Gabbay 		hdev->asic_prop.iatu_done_by_fw = true;
845e65e175bSOded Gabbay 
846e65e175bSOded Gabbay 		/*
847e65e175bSOded Gabbay 		 * GIC-security-bit can ONLY be set by CPUCP, so in this stage
848e65e175bSOded Gabbay 		 * decision can only be taken based on PCI ID security.
849e65e175bSOded Gabbay 		 */
850e65e175bSOded Gabbay 		hdev->asic_prop.gic_interrupts_enable = false;
851e65e175bSOded Gabbay 		goto pci_init;
852e65e175bSOded Gabbay 	}
853e65e175bSOded Gabbay 
854e65e175bSOded Gabbay 	rc = hl_pci_elbi_read(hdev, CFG_BASE + mmCPU_BOOT_DEV_STS0,
855e65e175bSOded Gabbay 				&fw_boot_status);
856e65e175bSOded Gabbay 	if (rc)
857e65e175bSOded Gabbay 		goto free_queue_props;
858e65e175bSOded Gabbay 
859e65e175bSOded Gabbay 	/* Check whether FW is configuring iATU */
860e65e175bSOded Gabbay 	if ((fw_boot_status & CPU_BOOT_DEV_STS0_ENABLED) &&
861e65e175bSOded Gabbay 			(fw_boot_status & CPU_BOOT_DEV_STS0_FW_IATU_CONF_EN))
862e65e175bSOded Gabbay 		hdev->asic_prop.iatu_done_by_fw = true;
863e65e175bSOded Gabbay 
864e65e175bSOded Gabbay pci_init:
865e65e175bSOded Gabbay 	rc = hl_pci_init(hdev);
866e65e175bSOded Gabbay 	if (rc)
867e65e175bSOded Gabbay 		goto free_queue_props;
868e65e175bSOded Gabbay 
869e65e175bSOded Gabbay 	/* Before continuing in the initialization, we need to read the preboot
870e65e175bSOded Gabbay 	 * version to determine whether we run with a security-enabled firmware
871e65e175bSOded Gabbay 	 */
872e65e175bSOded Gabbay 	rc = hl_fw_read_preboot_status(hdev);
873e65e175bSOded Gabbay 	if (rc) {
874e65e175bSOded Gabbay 		if (hdev->reset_on_preboot_fail)
87586b74d84SDafna Hirschfeld 			/* we are already on failure flow, so don't check if hw_fini fails. */
876e65e175bSOded Gabbay 			hdev->asic_funcs->hw_fini(hdev, true, false);
877e65e175bSOded Gabbay 		goto pci_fini;
878e65e175bSOded Gabbay 	}
879e65e175bSOded Gabbay 
880e65e175bSOded Gabbay 	if (gaudi_get_hw_state(hdev) == HL_DEVICE_HW_STATE_DIRTY) {
881e65e175bSOded Gabbay 		dev_dbg(hdev->dev, "H/W state is dirty, must reset before initializing\n");
88286b74d84SDafna Hirschfeld 		rc = hdev->asic_funcs->hw_fini(hdev, true, false);
88386b74d84SDafna Hirschfeld 		if (rc) {
88486b74d84SDafna Hirschfeld 			dev_err(hdev->dev, "failed to reset HW in dirty state (%d)\n", rc);
88586b74d84SDafna Hirschfeld 			goto pci_fini;
88686b74d84SDafna Hirschfeld 		}
887e65e175bSOded Gabbay 	}
888e65e175bSOded Gabbay 
889e65e175bSOded Gabbay 	return 0;
890e65e175bSOded Gabbay 
891e65e175bSOded Gabbay pci_fini:
892e65e175bSOded Gabbay 	hl_pci_fini(hdev);
893e65e175bSOded Gabbay free_queue_props:
894e65e175bSOded Gabbay 	kfree(hdev->asic_prop.hw_queues_props);
895e65e175bSOded Gabbay 	return rc;
896e65e175bSOded Gabbay }
897e65e175bSOded Gabbay 
898e65e175bSOded Gabbay static int gaudi_early_fini(struct hl_device *hdev)
899e65e175bSOded Gabbay {
900e65e175bSOded Gabbay 	kfree(hdev->asic_prop.hw_queues_props);
901e65e175bSOded Gabbay 	hl_pci_fini(hdev);
902e65e175bSOded Gabbay 
903e65e175bSOded Gabbay 	return 0;
904e65e175bSOded Gabbay }
905e65e175bSOded Gabbay 
906e65e175bSOded Gabbay /**
907e65e175bSOded Gabbay  * gaudi_fetch_psoc_frequency - Fetch PSOC frequency values
908e65e175bSOded Gabbay  *
909e65e175bSOded Gabbay  * @hdev: pointer to hl_device structure
910e65e175bSOded Gabbay  *
911e65e175bSOded Gabbay  */
912e65e175bSOded Gabbay static int gaudi_fetch_psoc_frequency(struct hl_device *hdev)
913e65e175bSOded Gabbay {
914e65e175bSOded Gabbay 	u32 nr = 0, nf = 0, od = 0, div_fctr = 0, pll_clk, div_sel;
915e65e175bSOded Gabbay 	struct asic_fixed_properties *prop = &hdev->asic_prop;
916e65e175bSOded Gabbay 	u16 pll_freq_arr[HL_PLL_NUM_OUTPUTS], freq;
917e65e175bSOded Gabbay 	int rc;
918e65e175bSOded Gabbay 
919e65e175bSOded Gabbay 	if ((hdev->fw_components & FW_TYPE_LINUX) &&
920e65e175bSOded Gabbay 			(prop->fw_app_cpu_boot_dev_sts0 & CPU_BOOT_DEV_STS0_PLL_INFO_EN)) {
921e65e175bSOded Gabbay 		struct gaudi_device *gaudi = hdev->asic_specific;
922e65e175bSOded Gabbay 
923e65e175bSOded Gabbay 		if (!(gaudi->hw_cap_initialized & HW_CAP_CPU_Q))
924e65e175bSOded Gabbay 			return 0;
925e65e175bSOded Gabbay 
926e65e175bSOded Gabbay 		rc = hl_fw_cpucp_pll_info_get(hdev, HL_GAUDI_CPU_PLL, pll_freq_arr);
927e65e175bSOded Gabbay 
928e65e175bSOded Gabbay 		if (rc)
929e65e175bSOded Gabbay 			return rc;
930e65e175bSOded Gabbay 
931e65e175bSOded Gabbay 		freq = pll_freq_arr[2];
932e65e175bSOded Gabbay 	} else {
933e65e175bSOded Gabbay 		/* Backward compatibility */
934e65e175bSOded Gabbay 		div_fctr = RREG32(mmPSOC_CPU_PLL_DIV_FACTOR_2);
935e65e175bSOded Gabbay 		div_sel = RREG32(mmPSOC_CPU_PLL_DIV_SEL_2);
936e65e175bSOded Gabbay 		nr = RREG32(mmPSOC_CPU_PLL_NR);
937e65e175bSOded Gabbay 		nf = RREG32(mmPSOC_CPU_PLL_NF);
938e65e175bSOded Gabbay 		od = RREG32(mmPSOC_CPU_PLL_OD);
939e65e175bSOded Gabbay 
940e65e175bSOded Gabbay 		if (div_sel == DIV_SEL_REF_CLK ||
941e65e175bSOded Gabbay 				div_sel == DIV_SEL_DIVIDED_REF) {
942e65e175bSOded Gabbay 			if (div_sel == DIV_SEL_REF_CLK)
943e65e175bSOded Gabbay 				freq = PLL_REF_CLK;
944e65e175bSOded Gabbay 			else
945e65e175bSOded Gabbay 				freq = PLL_REF_CLK / (div_fctr + 1);
946e65e175bSOded Gabbay 		} else if (div_sel == DIV_SEL_PLL_CLK ||
947e65e175bSOded Gabbay 			div_sel == DIV_SEL_DIVIDED_PLL) {
948e65e175bSOded Gabbay 			pll_clk = PLL_REF_CLK * (nf + 1) /
949e65e175bSOded Gabbay 					((nr + 1) * (od + 1));
950e65e175bSOded Gabbay 			if (div_sel == DIV_SEL_PLL_CLK)
951e65e175bSOded Gabbay 				freq = pll_clk;
952e65e175bSOded Gabbay 			else
953e65e175bSOded Gabbay 				freq = pll_clk / (div_fctr + 1);
954e65e175bSOded Gabbay 		} else {
955e65e175bSOded Gabbay 			dev_warn(hdev->dev, "Received invalid div select value: %#x", div_sel);
956e65e175bSOded Gabbay 			freq = 0;
957e65e175bSOded Gabbay 		}
958e65e175bSOded Gabbay 	}
959e65e175bSOded Gabbay 
960e65e175bSOded Gabbay 	prop->psoc_timestamp_frequency = freq;
961e65e175bSOded Gabbay 	prop->psoc_pci_pll_nr = nr;
962e65e175bSOded Gabbay 	prop->psoc_pci_pll_nf = nf;
963e65e175bSOded Gabbay 	prop->psoc_pci_pll_od = od;
964e65e175bSOded Gabbay 	prop->psoc_pci_pll_div_factor = div_fctr;
965e65e175bSOded Gabbay 
966e65e175bSOded Gabbay 	return 0;
967e65e175bSOded Gabbay }
968e65e175bSOded Gabbay 
969e65e175bSOded Gabbay static int _gaudi_init_tpc_mem(struct hl_device *hdev,
970e65e175bSOded Gabbay 		dma_addr_t tpc_kernel_src_addr, u32 tpc_kernel_size)
971e65e175bSOded Gabbay {
972e65e175bSOded Gabbay 	struct asic_fixed_properties *prop = &hdev->asic_prop;
973e65e175bSOded Gabbay 	struct packet_lin_dma *init_tpc_mem_pkt;
974e65e175bSOded Gabbay 	struct hl_cs_job *job;
975e65e175bSOded Gabbay 	struct hl_cb *cb;
976e65e175bSOded Gabbay 	u64 dst_addr;
977e65e175bSOded Gabbay 	u32 cb_size, ctl;
978e65e175bSOded Gabbay 	u8 tpc_id;
979e65e175bSOded Gabbay 	int rc;
980e65e175bSOded Gabbay 
981e65e175bSOded Gabbay 	cb = hl_cb_kernel_create(hdev, PAGE_SIZE, false);
982e65e175bSOded Gabbay 	if (!cb)
983e65e175bSOded Gabbay 		return -EFAULT;
984e65e175bSOded Gabbay 
985e65e175bSOded Gabbay 	init_tpc_mem_pkt = cb->kernel_address;
986e65e175bSOded Gabbay 	cb_size = sizeof(*init_tpc_mem_pkt);
987e65e175bSOded Gabbay 	memset(init_tpc_mem_pkt, 0, cb_size);
988e65e175bSOded Gabbay 
989e65e175bSOded Gabbay 	init_tpc_mem_pkt->tsize = cpu_to_le32(tpc_kernel_size);
990e65e175bSOded Gabbay 
991e65e175bSOded Gabbay 	ctl = FIELD_PREP(GAUDI_PKT_CTL_OPCODE_MASK, PACKET_LIN_DMA);
992e65e175bSOded Gabbay 	ctl |= FIELD_PREP(GAUDI_PKT_LIN_DMA_CTL_LIN_MASK, 1);
993e65e175bSOded Gabbay 	ctl |= FIELD_PREP(GAUDI_PKT_CTL_RB_MASK, 1);
994e65e175bSOded Gabbay 	ctl |= FIELD_PREP(GAUDI_PKT_CTL_MB_MASK, 1);
995e65e175bSOded Gabbay 
996e65e175bSOded Gabbay 	init_tpc_mem_pkt->ctl = cpu_to_le32(ctl);
997e65e175bSOded Gabbay 
998e65e175bSOded Gabbay 	init_tpc_mem_pkt->src_addr = cpu_to_le64(tpc_kernel_src_addr);
999e65e175bSOded Gabbay 
1000e65e175bSOded Gabbay 	/* TPC_CMD is configured with I$ prefetch enabled, so address should be aligned to 8KB */
1001e65e175bSOded Gabbay 	dst_addr = FIELD_PREP(GAUDI_PKT_LIN_DMA_DST_ADDR_MASK,
1002e65e175bSOded Gabbay 				round_up(prop->sram_user_base_address, SZ_8K));
1003e65e175bSOded Gabbay 	init_tpc_mem_pkt->dst_addr |= cpu_to_le64(dst_addr);
1004e65e175bSOded Gabbay 
1005e65e175bSOded Gabbay 	job = hl_cs_allocate_job(hdev, QUEUE_TYPE_EXT, true);
1006e65e175bSOded Gabbay 	if (!job) {
1007e65e175bSOded Gabbay 		dev_err(hdev->dev, "Failed to allocate a new job\n");
1008e65e175bSOded Gabbay 		rc = -ENOMEM;
1009e65e175bSOded Gabbay 		goto release_cb;
1010e65e175bSOded Gabbay 	}
1011e65e175bSOded Gabbay 
1012e65e175bSOded Gabbay 	job->id = 0;
1013e65e175bSOded Gabbay 	job->user_cb = cb;
1014e65e175bSOded Gabbay 	atomic_inc(&job->user_cb->cs_cnt);
1015e65e175bSOded Gabbay 	job->user_cb_size = cb_size;
1016e65e175bSOded Gabbay 	job->hw_queue_id = GAUDI_QUEUE_ID_DMA_0_0;
1017e65e175bSOded Gabbay 	job->patched_cb = job->user_cb;
1018e65e175bSOded Gabbay 	job->job_cb_size = job->user_cb_size + sizeof(struct packet_msg_prot);
1019e65e175bSOded Gabbay 
1020e65e175bSOded Gabbay 	hl_debugfs_add_job(hdev, job);
1021e65e175bSOded Gabbay 
1022e65e175bSOded Gabbay 	rc = gaudi_send_job_on_qman0(hdev, job);
1023e65e175bSOded Gabbay 
1024e65e175bSOded Gabbay 	if (rc)
1025e65e175bSOded Gabbay 		goto free_job;
1026e65e175bSOded Gabbay 
1027e65e175bSOded Gabbay 	for (tpc_id = 0 ; tpc_id < TPC_NUMBER_OF_ENGINES ; tpc_id++) {
1028e65e175bSOded Gabbay 		rc = gaudi_run_tpc_kernel(hdev, dst_addr, tpc_id);
1029e65e175bSOded Gabbay 		if (rc)
1030e65e175bSOded Gabbay 			break;
1031e65e175bSOded Gabbay 	}
1032e65e175bSOded Gabbay 
1033e65e175bSOded Gabbay free_job:
1034e65e175bSOded Gabbay 	hl_userptr_delete_list(hdev, &job->userptr_list);
1035e65e175bSOded Gabbay 	hl_debugfs_remove_job(hdev, job);
1036e65e175bSOded Gabbay 	kfree(job);
1037e65e175bSOded Gabbay 	atomic_dec(&cb->cs_cnt);
1038e65e175bSOded Gabbay 
1039e65e175bSOded Gabbay release_cb:
1040e65e175bSOded Gabbay 	hl_cb_put(cb);
1041e65e175bSOded Gabbay 	hl_cb_destroy(&hdev->kernel_mem_mgr, cb->buf->handle);
1042e65e175bSOded Gabbay 
1043e65e175bSOded Gabbay 	return rc;
1044e65e175bSOded Gabbay }
1045e65e175bSOded Gabbay 
1046e65e175bSOded Gabbay /*
1047e65e175bSOded Gabbay  * gaudi_init_tpc_mem() - Initialize TPC memories.
1048e65e175bSOded Gabbay  * @hdev: Pointer to hl_device structure.
1049e65e175bSOded Gabbay  *
1050e65e175bSOded Gabbay  * Copy TPC kernel fw from firmware file and run it to initialize TPC memories.
1051e65e175bSOded Gabbay  *
1052e65e175bSOded Gabbay  * Return: 0 for success, negative value for error.
1053e65e175bSOded Gabbay  */
1054e65e175bSOded Gabbay static int gaudi_init_tpc_mem(struct hl_device *hdev)
1055e65e175bSOded Gabbay {
1056e65e175bSOded Gabbay 	const struct firmware *fw;
1057e65e175bSOded Gabbay 	size_t fw_size;
1058e65e175bSOded Gabbay 	void *cpu_addr;
1059e65e175bSOded Gabbay 	dma_addr_t dma_handle;
1060e65e175bSOded Gabbay 	int rc, count = 5;
1061e65e175bSOded Gabbay 
1062e65e175bSOded Gabbay again:
1063e65e175bSOded Gabbay 	rc = request_firmware(&fw, GAUDI_TPC_FW_FILE, hdev->dev);
1064e65e175bSOded Gabbay 	if (rc == -EINTR && count-- > 0) {
1065e65e175bSOded Gabbay 		msleep(50);
1066e65e175bSOded Gabbay 		goto again;
1067e65e175bSOded Gabbay 	}
1068e65e175bSOded Gabbay 
1069e65e175bSOded Gabbay 	if (rc) {
1070e65e175bSOded Gabbay 		dev_err(hdev->dev, "Failed to load firmware file %s\n",
1071e65e175bSOded Gabbay 				GAUDI_TPC_FW_FILE);
1072e65e175bSOded Gabbay 		goto out;
1073e65e175bSOded Gabbay 	}
1074e65e175bSOded Gabbay 
1075e65e175bSOded Gabbay 	fw_size = fw->size;
1076e65e175bSOded Gabbay 	cpu_addr = hl_asic_dma_alloc_coherent(hdev, fw_size, &dma_handle, GFP_KERNEL | __GFP_ZERO);
1077e65e175bSOded Gabbay 	if (!cpu_addr) {
1078e65e175bSOded Gabbay 		dev_err(hdev->dev,
1079e65e175bSOded Gabbay 			"Failed to allocate %zu of dma memory for TPC kernel\n",
1080e65e175bSOded Gabbay 			fw_size);
1081e65e175bSOded Gabbay 		rc = -ENOMEM;
1082e65e175bSOded Gabbay 		goto out;
1083e65e175bSOded Gabbay 	}
1084e65e175bSOded Gabbay 
1085e65e175bSOded Gabbay 	memcpy(cpu_addr, fw->data, fw_size);
1086e65e175bSOded Gabbay 
1087e65e175bSOded Gabbay 	rc = _gaudi_init_tpc_mem(hdev, dma_handle, fw_size);
1088e65e175bSOded Gabbay 
1089e65e175bSOded Gabbay 	hl_asic_dma_free_coherent(hdev, fw->size, cpu_addr, dma_handle);
1090e65e175bSOded Gabbay 
1091e65e175bSOded Gabbay out:
1092e65e175bSOded Gabbay 	release_firmware(fw);
1093e65e175bSOded Gabbay 	return rc;
1094e65e175bSOded Gabbay }
1095e65e175bSOded Gabbay 
1096e65e175bSOded Gabbay static void gaudi_collective_map_sobs(struct hl_device *hdev, u32 stream)
1097e65e175bSOded Gabbay {
1098e65e175bSOded Gabbay 	struct gaudi_device *gaudi = hdev->asic_specific;
1099e65e175bSOded Gabbay 	struct gaudi_collective_properties *prop = &gaudi->collective_props;
1100e65e175bSOded Gabbay 	struct hl_hw_queue *q;
1101e65e175bSOded Gabbay 	u32 i, sob_id, sob_group_id, queue_id;
1102e65e175bSOded Gabbay 
1103e65e175bSOded Gabbay 	/* Iterate through SOB groups and assign a SOB for each slave queue */
1104e65e175bSOded Gabbay 	sob_group_id =
1105e65e175bSOded Gabbay 		stream * HL_RSVD_SOBS + prop->curr_sob_group_idx[stream];
1106e65e175bSOded Gabbay 	sob_id = prop->hw_sob_group[sob_group_id].base_sob_id;
1107e65e175bSOded Gabbay 
1108e65e175bSOded Gabbay 	queue_id = GAUDI_QUEUE_ID_NIC_0_0 + stream;
1109e65e175bSOded Gabbay 	for (i = 0 ; i < NIC_NUMBER_OF_ENGINES ; i++) {
1110e65e175bSOded Gabbay 		q = &hdev->kernel_queues[queue_id + (4 * i)];
1111e65e175bSOded Gabbay 		q->sync_stream_prop.collective_sob_id = sob_id + i;
1112e65e175bSOded Gabbay 	}
1113e65e175bSOded Gabbay 
1114e65e175bSOded Gabbay 	/* Both DMA5 and TPC7 use the same resources since only a single
1115e65e175bSOded Gabbay 	 * engine need to participate in the reduction process
1116e65e175bSOded Gabbay 	 */
1117e65e175bSOded Gabbay 	queue_id = GAUDI_QUEUE_ID_DMA_5_0 + stream;
1118e65e175bSOded Gabbay 	q = &hdev->kernel_queues[queue_id];
1119e65e175bSOded Gabbay 	q->sync_stream_prop.collective_sob_id =
1120e65e175bSOded Gabbay 			sob_id + NIC_NUMBER_OF_ENGINES;
1121e65e175bSOded Gabbay 
1122e65e175bSOded Gabbay 	queue_id = GAUDI_QUEUE_ID_TPC_7_0 + stream;
1123e65e175bSOded Gabbay 	q = &hdev->kernel_queues[queue_id];
1124e65e175bSOded Gabbay 	q->sync_stream_prop.collective_sob_id =
1125e65e175bSOded Gabbay 			sob_id + NIC_NUMBER_OF_ENGINES;
1126e65e175bSOded Gabbay }
1127e65e175bSOded Gabbay 
1128e65e175bSOded Gabbay static void gaudi_sob_group_hw_reset(struct kref *ref)
1129e65e175bSOded Gabbay {
1130e65e175bSOded Gabbay 	struct gaudi_hw_sob_group *hw_sob_group =
1131e65e175bSOded Gabbay 		container_of(ref, struct gaudi_hw_sob_group, kref);
1132e65e175bSOded Gabbay 	struct hl_device *hdev = hw_sob_group->hdev;
1133e65e175bSOded Gabbay 	int i;
1134e65e175bSOded Gabbay 
1135e65e175bSOded Gabbay 	for (i = 0 ; i < NUMBER_OF_SOBS_IN_GRP ; i++)
1136e65e175bSOded Gabbay 		WREG32((mmSYNC_MNGR_W_S_SYNC_MNGR_OBJS_SOB_OBJ_0 +
1137e65e175bSOded Gabbay 			(hw_sob_group->base_sob_id * 4) + (i * 4)), 0);
1138e65e175bSOded Gabbay 
1139e65e175bSOded Gabbay 	kref_init(&hw_sob_group->kref);
1140e65e175bSOded Gabbay }
1141e65e175bSOded Gabbay 
1142e65e175bSOded Gabbay static void gaudi_sob_group_reset_error(struct kref *ref)
1143e65e175bSOded Gabbay {
1144e65e175bSOded Gabbay 	struct gaudi_hw_sob_group *hw_sob_group =
1145e65e175bSOded Gabbay 		container_of(ref, struct gaudi_hw_sob_group, kref);
1146e65e175bSOded Gabbay 	struct hl_device *hdev = hw_sob_group->hdev;
1147e65e175bSOded Gabbay 
1148e65e175bSOded Gabbay 	dev_crit(hdev->dev,
1149e65e175bSOded Gabbay 		"SOB release shouldn't be called here, base_sob_id: %d\n",
1150e65e175bSOded Gabbay 		hw_sob_group->base_sob_id);
1151e65e175bSOded Gabbay }
1152e65e175bSOded Gabbay 
1153e65e175bSOded Gabbay static void gaudi_collective_mstr_sob_mask_set(struct gaudi_device *gaudi)
1154e65e175bSOded Gabbay {
1155e65e175bSOded Gabbay 	struct gaudi_collective_properties *prop;
1156e65e175bSOded Gabbay 	int i;
1157e65e175bSOded Gabbay 
1158e65e175bSOded Gabbay 	prop = &gaudi->collective_props;
1159e65e175bSOded Gabbay 
1160e65e175bSOded Gabbay 	memset(prop->mstr_sob_mask, 0, sizeof(prop->mstr_sob_mask));
1161e65e175bSOded Gabbay 
1162e65e175bSOded Gabbay 	for (i = 0 ; i < NIC_NUMBER_OF_ENGINES ; i++)
1163e65e175bSOded Gabbay 		if (gaudi->hw_cap_initialized & BIT(HW_CAP_NIC_SHIFT + i))
1164e65e175bSOded Gabbay 			prop->mstr_sob_mask[i / HL_MAX_SOBS_PER_MONITOR] |=
1165e65e175bSOded Gabbay 					BIT(i % HL_MAX_SOBS_PER_MONITOR);
1166e65e175bSOded Gabbay 	/* Set collective engine bit */
1167e65e175bSOded Gabbay 	prop->mstr_sob_mask[i / HL_MAX_SOBS_PER_MONITOR] |=
1168e65e175bSOded Gabbay 				BIT(i % HL_MAX_SOBS_PER_MONITOR);
1169e65e175bSOded Gabbay }
1170e65e175bSOded Gabbay 
1171e65e175bSOded Gabbay static int gaudi_collective_init(struct hl_device *hdev)
1172e65e175bSOded Gabbay {
1173e65e175bSOded Gabbay 	u32 i, sob_id, reserved_sobs_per_group;
1174e65e175bSOded Gabbay 	struct gaudi_collective_properties *prop;
1175e65e175bSOded Gabbay 	struct gaudi_device *gaudi;
1176e65e175bSOded Gabbay 
1177e65e175bSOded Gabbay 	gaudi = hdev->asic_specific;
1178e65e175bSOded Gabbay 	prop = &gaudi->collective_props;
1179e65e175bSOded Gabbay 	sob_id = hdev->asic_prop.collective_first_sob;
1180e65e175bSOded Gabbay 
1181e65e175bSOded Gabbay 	/* First sob in group must be aligned to HL_MAX_SOBS_PER_MONITOR */
1182e65e175bSOded Gabbay 	reserved_sobs_per_group =
1183e65e175bSOded Gabbay 		ALIGN(NUMBER_OF_SOBS_IN_GRP, HL_MAX_SOBS_PER_MONITOR);
1184e65e175bSOded Gabbay 
1185e65e175bSOded Gabbay 	/* Init SOB groups */
1186e65e175bSOded Gabbay 	for (i = 0 ; i < NUM_SOB_GROUPS; i++) {
1187e65e175bSOded Gabbay 		prop->hw_sob_group[i].hdev = hdev;
1188e65e175bSOded Gabbay 		prop->hw_sob_group[i].base_sob_id = sob_id;
1189e65e175bSOded Gabbay 		sob_id += reserved_sobs_per_group;
1190e65e175bSOded Gabbay 		gaudi_sob_group_hw_reset(&prop->hw_sob_group[i].kref);
1191e65e175bSOded Gabbay 	}
1192e65e175bSOded Gabbay 
1193e65e175bSOded Gabbay 	for (i = 0 ; i < QMAN_STREAMS; i++) {
1194e65e175bSOded Gabbay 		prop->next_sob_group_val[i] = 1;
1195e65e175bSOded Gabbay 		prop->curr_sob_group_idx[i] = 0;
1196e65e175bSOded Gabbay 		gaudi_collective_map_sobs(hdev, i);
1197e65e175bSOded Gabbay 	}
1198e65e175bSOded Gabbay 
1199e65e175bSOded Gabbay 	gaudi_collective_mstr_sob_mask_set(gaudi);
1200e65e175bSOded Gabbay 
1201e65e175bSOded Gabbay 	return 0;
1202e65e175bSOded Gabbay }
1203e65e175bSOded Gabbay 
1204e65e175bSOded Gabbay static void gaudi_reset_sob_group(struct hl_device *hdev, u16 sob_group)
1205e65e175bSOded Gabbay {
1206e65e175bSOded Gabbay 	struct gaudi_device *gaudi = hdev->asic_specific;
1207e65e175bSOded Gabbay 	struct gaudi_collective_properties *cprop = &gaudi->collective_props;
1208e65e175bSOded Gabbay 
1209e65e175bSOded Gabbay 	kref_put(&cprop->hw_sob_group[sob_group].kref,
1210e65e175bSOded Gabbay 					gaudi_sob_group_hw_reset);
1211e65e175bSOded Gabbay }
1212e65e175bSOded Gabbay 
1213e65e175bSOded Gabbay static void gaudi_collective_master_init_job(struct hl_device *hdev,
1214e65e175bSOded Gabbay 		struct hl_cs_job *job, u32 stream, u32 sob_group_offset)
1215e65e175bSOded Gabbay {
1216e65e175bSOded Gabbay 	u32 master_sob_base, master_monitor, queue_id, cb_size = 0;
1217e65e175bSOded Gabbay 	struct gaudi_collective_properties *cprop;
1218e65e175bSOded Gabbay 	struct hl_gen_wait_properties wait_prop;
1219e65e175bSOded Gabbay 	struct hl_sync_stream_properties *prop;
1220e65e175bSOded Gabbay 	struct gaudi_device *gaudi;
1221e65e175bSOded Gabbay 
1222e65e175bSOded Gabbay 	gaudi = hdev->asic_specific;
1223e65e175bSOded Gabbay 	cprop = &gaudi->collective_props;
1224e65e175bSOded Gabbay 	queue_id = job->hw_queue_id;
1225e65e175bSOded Gabbay 	prop = &hdev->kernel_queues[queue_id].sync_stream_prop;
1226e65e175bSOded Gabbay 
1227e65e175bSOded Gabbay 	master_sob_base =
1228e65e175bSOded Gabbay 		cprop->hw_sob_group[sob_group_offset].base_sob_id;
1229e65e175bSOded Gabbay 	master_monitor = prop->collective_mstr_mon_id[0];
1230e65e175bSOded Gabbay 
1231e65e175bSOded Gabbay 	cprop->hw_sob_group[sob_group_offset].queue_id = queue_id;
1232e65e175bSOded Gabbay 
1233e65e175bSOded Gabbay 	dev_dbg(hdev->dev,
1234e65e175bSOded Gabbay 		"Generate master wait CBs, sob %d (mask %#x), val:0x%x, mon %u, q %d\n",
1235e65e175bSOded Gabbay 		master_sob_base, cprop->mstr_sob_mask[0],
1236e65e175bSOded Gabbay 		cprop->next_sob_group_val[stream],
1237e65e175bSOded Gabbay 		master_monitor, queue_id);
1238e65e175bSOded Gabbay 
1239e65e175bSOded Gabbay 	wait_prop.data = (void *) job->patched_cb;
1240e65e175bSOded Gabbay 	wait_prop.sob_base = master_sob_base;
1241e65e175bSOded Gabbay 	wait_prop.sob_mask = cprop->mstr_sob_mask[0];
1242e65e175bSOded Gabbay 	wait_prop.sob_val = cprop->next_sob_group_val[stream];
1243e65e175bSOded Gabbay 	wait_prop.mon_id = master_monitor;
1244e65e175bSOded Gabbay 	wait_prop.q_idx = queue_id;
1245e65e175bSOded Gabbay 	wait_prop.size = cb_size;
1246e65e175bSOded Gabbay 	cb_size += gaudi_gen_wait_cb(hdev, &wait_prop);
1247e65e175bSOded Gabbay 
1248e65e175bSOded Gabbay 	master_sob_base += HL_MAX_SOBS_PER_MONITOR;
1249e65e175bSOded Gabbay 	master_monitor = prop->collective_mstr_mon_id[1];
1250e65e175bSOded Gabbay 
1251e65e175bSOded Gabbay 	dev_dbg(hdev->dev,
1252e65e175bSOded Gabbay 		"Generate master wait CBs, sob %d (mask %#x), val:0x%x, mon %u, q %d\n",
1253e65e175bSOded Gabbay 		master_sob_base, cprop->mstr_sob_mask[1],
1254e65e175bSOded Gabbay 		cprop->next_sob_group_val[stream],
1255e65e175bSOded Gabbay 		master_monitor, queue_id);
1256e65e175bSOded Gabbay 
1257e65e175bSOded Gabbay 	wait_prop.sob_base = master_sob_base;
1258e65e175bSOded Gabbay 	wait_prop.sob_mask = cprop->mstr_sob_mask[1];
1259e65e175bSOded Gabbay 	wait_prop.mon_id = master_monitor;
1260e65e175bSOded Gabbay 	wait_prop.size = cb_size;
1261e65e175bSOded Gabbay 	cb_size += gaudi_gen_wait_cb(hdev, &wait_prop);
1262e65e175bSOded Gabbay }
1263e65e175bSOded Gabbay 
1264e65e175bSOded Gabbay static void gaudi_collective_slave_init_job(struct hl_device *hdev,
1265e65e175bSOded Gabbay 		struct hl_cs_job *job, struct hl_cs_compl *cs_cmpl)
1266e65e175bSOded Gabbay {
1267e65e175bSOded Gabbay 	struct hl_gen_wait_properties wait_prop;
1268e65e175bSOded Gabbay 	struct hl_sync_stream_properties *prop;
1269e65e175bSOded Gabbay 	u32 queue_id, cb_size = 0;
1270e65e175bSOded Gabbay 
1271e65e175bSOded Gabbay 	queue_id = job->hw_queue_id;
1272e65e175bSOded Gabbay 	prop = &hdev->kernel_queues[queue_id].sync_stream_prop;
1273e65e175bSOded Gabbay 
1274e65e175bSOded Gabbay 	if (job->cs->encaps_signals) {
1275e65e175bSOded Gabbay 		/* use the encaps signal handle store earlier in the flow
1276e65e175bSOded Gabbay 		 * and set the SOB information from the encaps
1277e65e175bSOded Gabbay 		 * signals handle
1278e65e175bSOded Gabbay 		 */
1279e65e175bSOded Gabbay 		hl_hw_queue_encaps_sig_set_sob_info(hdev, job->cs, job,
1280e65e175bSOded Gabbay 						cs_cmpl);
1281e65e175bSOded Gabbay 
1282e65e175bSOded Gabbay 		dev_dbg(hdev->dev, "collective wait: Sequence %llu found, sob_id: %u,  wait for sob_val: %u\n",
1283e65e175bSOded Gabbay 				job->cs->sequence,
1284e65e175bSOded Gabbay 				cs_cmpl->hw_sob->sob_id,
1285e65e175bSOded Gabbay 				cs_cmpl->sob_val);
1286e65e175bSOded Gabbay 	}
1287e65e175bSOded Gabbay 
1288e65e175bSOded Gabbay 	/* Add to wait CBs using slave monitor */
1289e65e175bSOded Gabbay 	wait_prop.data = (void *) job->user_cb;
1290e65e175bSOded Gabbay 	wait_prop.sob_base = cs_cmpl->hw_sob->sob_id;
1291e65e175bSOded Gabbay 	wait_prop.sob_mask = 0x1;
1292e65e175bSOded Gabbay 	wait_prop.sob_val = cs_cmpl->sob_val;
1293e65e175bSOded Gabbay 	wait_prop.mon_id = prop->collective_slave_mon_id;
1294e65e175bSOded Gabbay 	wait_prop.q_idx = queue_id;
1295e65e175bSOded Gabbay 	wait_prop.size = cb_size;
1296e65e175bSOded Gabbay 
1297e65e175bSOded Gabbay 	dev_dbg(hdev->dev,
1298e65e175bSOded Gabbay 		"Generate slave wait CB, sob %d, val:%x, mon %d, q %d\n",
1299e65e175bSOded Gabbay 		cs_cmpl->hw_sob->sob_id, cs_cmpl->sob_val,
1300e65e175bSOded Gabbay 		prop->collective_slave_mon_id, queue_id);
1301e65e175bSOded Gabbay 
1302e65e175bSOded Gabbay 	cb_size += gaudi_gen_wait_cb(hdev, &wait_prop);
1303e65e175bSOded Gabbay 
1304e65e175bSOded Gabbay 	dev_dbg(hdev->dev,
1305e65e175bSOded Gabbay 		"generate signal CB, sob_id: %d, sob val: 1, q_idx: %d\n",
1306e65e175bSOded Gabbay 		prop->collective_sob_id, queue_id);
1307e65e175bSOded Gabbay 
1308e65e175bSOded Gabbay 	cb_size += gaudi_gen_signal_cb(hdev, job->user_cb,
1309e65e175bSOded Gabbay 			prop->collective_sob_id, cb_size, false);
1310e65e175bSOded Gabbay }
1311e65e175bSOded Gabbay 
1312e65e175bSOded Gabbay static int gaudi_collective_wait_init_cs(struct hl_cs *cs)
1313e65e175bSOded Gabbay {
1314e65e175bSOded Gabbay 	struct hl_cs_compl *signal_cs_cmpl =
1315e65e175bSOded Gabbay 		container_of(cs->signal_fence, struct hl_cs_compl, base_fence);
1316e65e175bSOded Gabbay 	struct hl_cs_compl *cs_cmpl =
1317e65e175bSOded Gabbay 		container_of(cs->fence, struct hl_cs_compl, base_fence);
1318e65e175bSOded Gabbay 	struct hl_cs_encaps_sig_handle *handle = cs->encaps_sig_hdl;
1319e65e175bSOded Gabbay 	struct gaudi_collective_properties *cprop;
1320e65e175bSOded Gabbay 	u32 stream, queue_id, sob_group_offset;
1321e65e175bSOded Gabbay 	struct gaudi_device *gaudi;
1322e65e175bSOded Gabbay 	struct hl_device *hdev;
1323e65e175bSOded Gabbay 	struct hl_cs_job *job;
1324e65e175bSOded Gabbay 	struct hl_ctx *ctx;
1325e65e175bSOded Gabbay 
1326e65e175bSOded Gabbay 	ctx = cs->ctx;
1327e65e175bSOded Gabbay 	hdev = ctx->hdev;
1328e65e175bSOded Gabbay 	gaudi = hdev->asic_specific;
1329e65e175bSOded Gabbay 	cprop = &gaudi->collective_props;
1330e65e175bSOded Gabbay 
1331e65e175bSOded Gabbay 	if (cs->encaps_signals) {
1332e65e175bSOded Gabbay 		cs_cmpl->hw_sob = handle->hw_sob;
1333e65e175bSOded Gabbay 		/* at this checkpoint we only need the hw_sob pointer
1334e65e175bSOded Gabbay 		 * for the completion check before start going over the jobs
1335e65e175bSOded Gabbay 		 * of the master/slaves, the sob_value will be taken later on
1336e65e175bSOded Gabbay 		 * in gaudi_collective_slave_init_job depends on each
1337e65e175bSOded Gabbay 		 * job wait offset value.
1338e65e175bSOded Gabbay 		 */
1339e65e175bSOded Gabbay 		cs_cmpl->sob_val = 0;
1340e65e175bSOded Gabbay 	} else {
1341e65e175bSOded Gabbay 		/* copy the SOB id and value of the signal CS */
1342e65e175bSOded Gabbay 		cs_cmpl->hw_sob = signal_cs_cmpl->hw_sob;
1343e65e175bSOded Gabbay 		cs_cmpl->sob_val = signal_cs_cmpl->sob_val;
1344e65e175bSOded Gabbay 	}
1345e65e175bSOded Gabbay 
1346e65e175bSOded Gabbay 	/* check again if the signal cs already completed.
1347e65e175bSOded Gabbay 	 * if yes then don't send any wait cs since the hw_sob
1348e65e175bSOded Gabbay 	 * could be in reset already. if signal is not completed
1349e65e175bSOded Gabbay 	 * then get refcount to hw_sob to prevent resetting the sob
1350e65e175bSOded Gabbay 	 * while wait cs is not submitted.
1351e65e175bSOded Gabbay 	 * note that this check is protected by two locks,
1352e65e175bSOded Gabbay 	 * hw queue lock and completion object lock,
1353e65e175bSOded Gabbay 	 * and the same completion object lock also protects
1354e65e175bSOded Gabbay 	 * the hw_sob reset handler function.
1355e65e175bSOded Gabbay 	 * The hw_queue lock prevent out of sync of hw_sob
1356e65e175bSOded Gabbay 	 * refcount value, changed by signal/wait flows.
1357e65e175bSOded Gabbay 	 */
1358e65e175bSOded Gabbay 	spin_lock(&signal_cs_cmpl->lock);
1359e65e175bSOded Gabbay 
1360e65e175bSOded Gabbay 	if (completion_done(&cs->signal_fence->completion)) {
1361e65e175bSOded Gabbay 		spin_unlock(&signal_cs_cmpl->lock);
1362e65e175bSOded Gabbay 		return -EINVAL;
1363e65e175bSOded Gabbay 	}
1364e65e175bSOded Gabbay 	/* Increment kref since all slave queues are now waiting on it */
1365e65e175bSOded Gabbay 	kref_get(&cs_cmpl->hw_sob->kref);
1366e65e175bSOded Gabbay 
1367e65e175bSOded Gabbay 	spin_unlock(&signal_cs_cmpl->lock);
1368e65e175bSOded Gabbay 
1369e65e175bSOded Gabbay 	/* Calculate the stream from collective master queue (1st job) */
1370e65e175bSOded Gabbay 	job = list_first_entry(&cs->job_list, struct hl_cs_job, cs_node);
1371e65e175bSOded Gabbay 	stream = job->hw_queue_id % 4;
1372e65e175bSOded Gabbay 	sob_group_offset =
1373e65e175bSOded Gabbay 		stream * HL_RSVD_SOBS + cprop->curr_sob_group_idx[stream];
1374e65e175bSOded Gabbay 
1375e65e175bSOded Gabbay 	list_for_each_entry(job, &cs->job_list, cs_node) {
1376e65e175bSOded Gabbay 		queue_id = job->hw_queue_id;
1377e65e175bSOded Gabbay 
1378e65e175bSOded Gabbay 		if (hdev->kernel_queues[queue_id].collective_mode ==
1379e65e175bSOded Gabbay 				HL_COLLECTIVE_MASTER)
1380e65e175bSOded Gabbay 			gaudi_collective_master_init_job(hdev, job, stream,
1381e65e175bSOded Gabbay 						sob_group_offset);
1382e65e175bSOded Gabbay 		else
1383e65e175bSOded Gabbay 			gaudi_collective_slave_init_job(hdev, job, cs_cmpl);
1384e65e175bSOded Gabbay 	}
1385e65e175bSOded Gabbay 
1386e65e175bSOded Gabbay 	cs_cmpl->sob_group = sob_group_offset;
1387e65e175bSOded Gabbay 
1388e65e175bSOded Gabbay 	/* Handle sob group kref and wraparound */
1389e65e175bSOded Gabbay 	kref_get(&cprop->hw_sob_group[sob_group_offset].kref);
1390e65e175bSOded Gabbay 	cprop->next_sob_group_val[stream]++;
1391e65e175bSOded Gabbay 
1392e65e175bSOded Gabbay 	if (cprop->next_sob_group_val[stream] == HL_MAX_SOB_VAL) {
1393e65e175bSOded Gabbay 		/*
1394e65e175bSOded Gabbay 		 * Decrement as we reached the max value.
1395e65e175bSOded Gabbay 		 * The release function won't be called here as we've
1396e65e175bSOded Gabbay 		 * just incremented the refcount.
1397e65e175bSOded Gabbay 		 */
1398e65e175bSOded Gabbay 		kref_put(&cprop->hw_sob_group[sob_group_offset].kref,
1399e65e175bSOded Gabbay 				gaudi_sob_group_reset_error);
1400e65e175bSOded Gabbay 		cprop->next_sob_group_val[stream] = 1;
1401e65e175bSOded Gabbay 		/* only two SOBs are currently in use */
1402e65e175bSOded Gabbay 		cprop->curr_sob_group_idx[stream] =
1403e65e175bSOded Gabbay 			(cprop->curr_sob_group_idx[stream] + 1) &
1404e65e175bSOded Gabbay 							(HL_RSVD_SOBS - 1);
1405e65e175bSOded Gabbay 
1406e65e175bSOded Gabbay 		gaudi_collective_map_sobs(hdev, stream);
1407e65e175bSOded Gabbay 
1408e65e175bSOded Gabbay 		dev_dbg(hdev->dev, "switched to SOB group %d, stream: %d\n",
1409e65e175bSOded Gabbay 				cprop->curr_sob_group_idx[stream], stream);
1410e65e175bSOded Gabbay 	}
1411e65e175bSOded Gabbay 
1412e65e175bSOded Gabbay 	mb();
1413e65e175bSOded Gabbay 	hl_fence_put(cs->signal_fence);
1414e65e175bSOded Gabbay 	cs->signal_fence = NULL;
1415e65e175bSOded Gabbay 
1416e65e175bSOded Gabbay 	return 0;
1417e65e175bSOded Gabbay }
1418e65e175bSOded Gabbay 
1419e65e175bSOded Gabbay static u32 gaudi_get_patched_cb_extra_size(u32 user_cb_size)
1420e65e175bSOded Gabbay {
1421e65e175bSOded Gabbay 	u32 cacheline_end, additional_commands;
1422e65e175bSOded Gabbay 
1423e65e175bSOded Gabbay 	cacheline_end = round_up(user_cb_size, DEVICE_CACHE_LINE_SIZE);
1424e65e175bSOded Gabbay 	additional_commands = sizeof(struct packet_msg_prot) * 2;
1425e65e175bSOded Gabbay 
1426e65e175bSOded Gabbay 	if (user_cb_size + additional_commands > cacheline_end)
1427e65e175bSOded Gabbay 		return cacheline_end - user_cb_size + additional_commands;
1428e65e175bSOded Gabbay 	else
1429e65e175bSOded Gabbay 		return additional_commands;
1430e65e175bSOded Gabbay }
1431e65e175bSOded Gabbay 
1432e65e175bSOded Gabbay static int gaudi_collective_wait_create_job(struct hl_device *hdev,
1433e65e175bSOded Gabbay 		struct hl_ctx *ctx, struct hl_cs *cs,
1434e65e175bSOded Gabbay 		enum hl_collective_mode mode, u32 queue_id, u32 wait_queue_id,
1435e65e175bSOded Gabbay 		u32 encaps_signal_offset)
1436e65e175bSOded Gabbay {
1437e65e175bSOded Gabbay 	struct hw_queue_properties *hw_queue_prop;
1438e65e175bSOded Gabbay 	struct hl_cs_counters_atomic *cntr;
1439e65e175bSOded Gabbay 	struct hl_cs_job *job;
1440e65e175bSOded Gabbay 	struct hl_cb *cb;
1441e65e175bSOded Gabbay 	u32 cb_size;
1442e65e175bSOded Gabbay 	bool patched_cb;
1443e65e175bSOded Gabbay 
1444e65e175bSOded Gabbay 	cntr = &hdev->aggregated_cs_counters;
1445e65e175bSOded Gabbay 
1446e65e175bSOded Gabbay 	if (mode == HL_COLLECTIVE_MASTER) {
1447e65e175bSOded Gabbay 		/* CB size of collective master queue contains
1448e65e175bSOded Gabbay 		 * 4 msg short packets for monitor 1 configuration
1449e65e175bSOded Gabbay 		 * 1 fence packet
1450e65e175bSOded Gabbay 		 * 4 msg short packets for monitor 2 configuration
1451e65e175bSOded Gabbay 		 * 1 fence packet
1452e65e175bSOded Gabbay 		 * 2 msg prot packets for completion and MSI
1453e65e175bSOded Gabbay 		 */
1454e65e175bSOded Gabbay 		cb_size = sizeof(struct packet_msg_short) * 8 +
1455e65e175bSOded Gabbay 				sizeof(struct packet_fence) * 2 +
1456e65e175bSOded Gabbay 				sizeof(struct packet_msg_prot) * 2;
1457e65e175bSOded Gabbay 		patched_cb = true;
1458e65e175bSOded Gabbay 	} else {
1459e65e175bSOded Gabbay 		/* CB size of collective slave queues contains
1460e65e175bSOded Gabbay 		 * 4 msg short packets for monitor configuration
1461e65e175bSOded Gabbay 		 * 1 fence packet
1462e65e175bSOded Gabbay 		 * 1 additional msg short packet for sob signal
1463e65e175bSOded Gabbay 		 */
1464e65e175bSOded Gabbay 		cb_size = sizeof(struct packet_msg_short) * 5 +
1465e65e175bSOded Gabbay 				sizeof(struct packet_fence);
1466e65e175bSOded Gabbay 		patched_cb = false;
1467e65e175bSOded Gabbay 	}
1468e65e175bSOded Gabbay 
1469e65e175bSOded Gabbay 	hw_queue_prop = &hdev->asic_prop.hw_queues_props[queue_id];
1470e65e175bSOded Gabbay 	job = hl_cs_allocate_job(hdev, hw_queue_prop->type, true);
1471e65e175bSOded Gabbay 	if (!job) {
1472e65e175bSOded Gabbay 		atomic64_inc(&ctx->cs_counters.out_of_mem_drop_cnt);
1473e65e175bSOded Gabbay 		atomic64_inc(&cntr->out_of_mem_drop_cnt);
1474e65e175bSOded Gabbay 		dev_err(hdev->dev, "Failed to allocate a new job\n");
1475e65e175bSOded Gabbay 		return -ENOMEM;
1476e65e175bSOded Gabbay 	}
1477e65e175bSOded Gabbay 
1478e65e175bSOded Gabbay 	/* Allocate internal mapped CB for non patched CBs */
1479e65e175bSOded Gabbay 	cb = hl_cb_kernel_create(hdev, cb_size,
1480e65e175bSOded Gabbay 			hdev->mmu_enable && !patched_cb);
1481e65e175bSOded Gabbay 	if (!cb) {
1482e65e175bSOded Gabbay 		atomic64_inc(&ctx->cs_counters.out_of_mem_drop_cnt);
1483e65e175bSOded Gabbay 		atomic64_inc(&cntr->out_of_mem_drop_cnt);
1484e65e175bSOded Gabbay 		kfree(job);
1485e65e175bSOded Gabbay 		return -EFAULT;
1486e65e175bSOded Gabbay 	}
1487e65e175bSOded Gabbay 
1488e65e175bSOded Gabbay 	job->id = 0;
1489e65e175bSOded Gabbay 	job->cs = cs;
1490e65e175bSOded Gabbay 	job->user_cb = cb;
1491e65e175bSOded Gabbay 	atomic_inc(&job->user_cb->cs_cnt);
1492e65e175bSOded Gabbay 	job->user_cb_size = cb_size;
1493e65e175bSOded Gabbay 	job->hw_queue_id = queue_id;
1494e65e175bSOded Gabbay 
1495e65e175bSOded Gabbay 	/* since its guaranteed to have only one chunk in the collective wait
1496e65e175bSOded Gabbay 	 * cs, we can use this chunk to set the encapsulated signal offset
1497e65e175bSOded Gabbay 	 * in the jobs.
1498e65e175bSOded Gabbay 	 */
1499e65e175bSOded Gabbay 	if (cs->encaps_signals)
1500e65e175bSOded Gabbay 		job->encaps_sig_wait_offset = encaps_signal_offset;
1501e65e175bSOded Gabbay 
1502e65e175bSOded Gabbay 	/*
1503e65e175bSOded Gabbay 	 * No need in parsing, user CB is the patched CB.
1504e65e175bSOded Gabbay 	 * We call hl_cb_destroy() out of two reasons - we don't need
1505e65e175bSOded Gabbay 	 * the CB in the CB idr anymore and to decrement its refcount as
1506e65e175bSOded Gabbay 	 * it was incremented inside hl_cb_kernel_create().
1507e65e175bSOded Gabbay 	 */
1508e65e175bSOded Gabbay 	if (patched_cb)
1509e65e175bSOded Gabbay 		job->patched_cb = job->user_cb;
1510e65e175bSOded Gabbay 	else
1511e65e175bSOded Gabbay 		job->patched_cb = NULL;
1512e65e175bSOded Gabbay 
1513e65e175bSOded Gabbay 	job->job_cb_size = job->user_cb_size;
1514e65e175bSOded Gabbay 	hl_cb_destroy(&hdev->kernel_mem_mgr, cb->buf->handle);
1515e65e175bSOded Gabbay 
1516e65e175bSOded Gabbay 	/* increment refcount as for external queues we get completion */
1517e65e175bSOded Gabbay 	if (hw_queue_prop->type == QUEUE_TYPE_EXT)
1518e65e175bSOded Gabbay 		cs_get(cs);
1519e65e175bSOded Gabbay 
1520e65e175bSOded Gabbay 	cs->jobs_in_queue_cnt[job->hw_queue_id]++;
1521e65e175bSOded Gabbay 
1522e65e175bSOded Gabbay 	list_add_tail(&job->cs_node, &cs->job_list);
1523e65e175bSOded Gabbay 
1524e65e175bSOded Gabbay 	hl_debugfs_add_job(hdev, job);
1525e65e175bSOded Gabbay 
1526e65e175bSOded Gabbay 	return 0;
1527e65e175bSOded Gabbay }
1528e65e175bSOded Gabbay 
1529e65e175bSOded Gabbay static int gaudi_collective_wait_create_jobs(struct hl_device *hdev,
1530e65e175bSOded Gabbay 		struct hl_ctx *ctx, struct hl_cs *cs,
1531e65e175bSOded Gabbay 		u32 wait_queue_id, u32 collective_engine_id,
1532e65e175bSOded Gabbay 		u32 encaps_signal_offset)
1533e65e175bSOded Gabbay {
1534e65e175bSOded Gabbay 	struct gaudi_device *gaudi = hdev->asic_specific;
1535e65e175bSOded Gabbay 	struct hw_queue_properties *hw_queue_prop;
1536e65e175bSOded Gabbay 	u32 queue_id, collective_queue, num_jobs;
1537e65e175bSOded Gabbay 	u32 stream, nic_queue, nic_idx = 0;
1538e65e175bSOded Gabbay 	bool skip;
1539e65e175bSOded Gabbay 	int i, rc = 0;
1540e65e175bSOded Gabbay 
1541e65e175bSOded Gabbay 	/* Verify wait queue id is configured as master */
1542e65e175bSOded Gabbay 	hw_queue_prop = &hdev->asic_prop.hw_queues_props[wait_queue_id];
1543e65e175bSOded Gabbay 	if (!(hw_queue_prop->collective_mode == HL_COLLECTIVE_MASTER)) {
1544e65e175bSOded Gabbay 		dev_err(hdev->dev,
1545e65e175bSOded Gabbay 			"Queue %d is not configured as collective master\n",
1546e65e175bSOded Gabbay 			wait_queue_id);
1547e65e175bSOded Gabbay 		return -EINVAL;
1548e65e175bSOded Gabbay 	}
1549e65e175bSOded Gabbay 
1550e65e175bSOded Gabbay 	/* Verify engine id is supported */
1551e65e175bSOded Gabbay 	if (collective_engine_id != GAUDI_ENGINE_ID_DMA_5 &&
1552e65e175bSOded Gabbay 			collective_engine_id != GAUDI_ENGINE_ID_TPC_7) {
1553e65e175bSOded Gabbay 		dev_err(hdev->dev,
1554e65e175bSOded Gabbay 			"Collective wait does not support engine %u\n",
1555e65e175bSOded Gabbay 			collective_engine_id);
1556e65e175bSOded Gabbay 		return -EINVAL;
1557e65e175bSOded Gabbay 	}
1558e65e175bSOded Gabbay 
1559e65e175bSOded Gabbay 	stream = wait_queue_id % 4;
1560e65e175bSOded Gabbay 
1561e65e175bSOded Gabbay 	if (collective_engine_id == GAUDI_ENGINE_ID_DMA_5)
1562e65e175bSOded Gabbay 		collective_queue = GAUDI_QUEUE_ID_DMA_5_0 + stream;
1563e65e175bSOded Gabbay 	else
1564e65e175bSOded Gabbay 		collective_queue = GAUDI_QUEUE_ID_TPC_7_0 + stream;
1565e65e175bSOded Gabbay 
1566e65e175bSOded Gabbay 	num_jobs = NUMBER_OF_SOBS_IN_GRP + 1;
1567e65e175bSOded Gabbay 	nic_queue = GAUDI_QUEUE_ID_NIC_0_0 + stream;
1568e65e175bSOded Gabbay 
1569e65e175bSOded Gabbay 	/* First job goes to the collective master queue, it will wait for
1570e65e175bSOded Gabbay 	 * the collective slave queues to finish execution.
1571e65e175bSOded Gabbay 	 * The synchronization is done using two monitors:
1572e65e175bSOded Gabbay 	 * First monitor for NICs 0-7, second monitor for NICs 8-9 and the
1573e65e175bSOded Gabbay 	 * reduction engine (DMA5/TPC7).
1574e65e175bSOded Gabbay 	 *
1575e65e175bSOded Gabbay 	 * Rest of the jobs goes to the collective slave queues which will
1576e65e175bSOded Gabbay 	 * all wait for the user to signal sob 'cs_cmpl->sob_val'.
1577e65e175bSOded Gabbay 	 */
1578e65e175bSOded Gabbay 	for (i = 0 ; i < num_jobs ; i++) {
1579e65e175bSOded Gabbay 		if (i == 0) {
1580e65e175bSOded Gabbay 			queue_id = wait_queue_id;
1581e65e175bSOded Gabbay 			rc = gaudi_collective_wait_create_job(hdev, ctx, cs,
1582e65e175bSOded Gabbay 				HL_COLLECTIVE_MASTER, queue_id,
1583e65e175bSOded Gabbay 				wait_queue_id, encaps_signal_offset);
1584e65e175bSOded Gabbay 		} else {
1585e65e175bSOded Gabbay 			if (nic_idx < NIC_NUMBER_OF_ENGINES) {
1586e65e175bSOded Gabbay 				if (gaudi->hw_cap_initialized &
1587e65e175bSOded Gabbay 					BIT(HW_CAP_NIC_SHIFT + nic_idx))
1588e65e175bSOded Gabbay 					skip = false;
1589e65e175bSOded Gabbay 				else
1590e65e175bSOded Gabbay 					skip = true;
1591e65e175bSOded Gabbay 
1592e65e175bSOded Gabbay 				queue_id = nic_queue;
1593e65e175bSOded Gabbay 				nic_queue += 4;
1594e65e175bSOded Gabbay 				nic_idx++;
1595e65e175bSOded Gabbay 
1596e65e175bSOded Gabbay 				if (skip)
1597e65e175bSOded Gabbay 					continue;
1598e65e175bSOded Gabbay 			} else {
1599e65e175bSOded Gabbay 				queue_id = collective_queue;
1600e65e175bSOded Gabbay 			}
1601e65e175bSOded Gabbay 
1602e65e175bSOded Gabbay 			rc = gaudi_collective_wait_create_job(hdev, ctx, cs,
1603e65e175bSOded Gabbay 				HL_COLLECTIVE_SLAVE, queue_id,
1604e65e175bSOded Gabbay 				wait_queue_id, encaps_signal_offset);
1605e65e175bSOded Gabbay 		}
1606e65e175bSOded Gabbay 
1607e65e175bSOded Gabbay 		if (rc)
1608e65e175bSOded Gabbay 			return rc;
1609e65e175bSOded Gabbay 	}
1610e65e175bSOded Gabbay 
1611e65e175bSOded Gabbay 	return rc;
1612e65e175bSOded Gabbay }
1613e65e175bSOded Gabbay 
1614e65e175bSOded Gabbay static int gaudi_late_init(struct hl_device *hdev)
1615e65e175bSOded Gabbay {
1616e65e175bSOded Gabbay 	struct gaudi_device *gaudi = hdev->asic_specific;
1617e65e175bSOded Gabbay 	int rc;
1618e65e175bSOded Gabbay 
1619e65e175bSOded Gabbay 	rc = gaudi->cpucp_info_get(hdev);
1620e65e175bSOded Gabbay 	if (rc) {
1621e65e175bSOded Gabbay 		dev_err(hdev->dev, "Failed to get cpucp info\n");
1622e65e175bSOded Gabbay 		return rc;
1623e65e175bSOded Gabbay 	}
1624e65e175bSOded Gabbay 
1625e65e175bSOded Gabbay 	if ((hdev->card_type == cpucp_card_type_pci) &&
1626e65e175bSOded Gabbay 			(hdev->nic_ports_mask & 0x3)) {
1627e65e175bSOded Gabbay 		dev_info(hdev->dev,
1628e65e175bSOded Gabbay 			"PCI card detected, only 8 ports are enabled\n");
1629e65e175bSOded Gabbay 		hdev->nic_ports_mask &= ~0x3;
1630e65e175bSOded Gabbay 
1631e65e175bSOded Gabbay 		/* Stop and disable unused NIC QMANs */
1632e65e175bSOded Gabbay 		WREG32(mmNIC0_QM0_GLBL_CFG1, NIC0_QM0_GLBL_CFG1_PQF_STOP_MASK |
1633e65e175bSOded Gabbay 					NIC0_QM0_GLBL_CFG1_CQF_STOP_MASK |
1634e65e175bSOded Gabbay 					NIC0_QM0_GLBL_CFG1_CP_STOP_MASK);
1635e65e175bSOded Gabbay 
1636e65e175bSOded Gabbay 		WREG32(mmNIC0_QM1_GLBL_CFG1, NIC0_QM0_GLBL_CFG1_PQF_STOP_MASK |
1637e65e175bSOded Gabbay 					NIC0_QM0_GLBL_CFG1_CQF_STOP_MASK |
1638e65e175bSOded Gabbay 					NIC0_QM0_GLBL_CFG1_CP_STOP_MASK);
1639e65e175bSOded Gabbay 
1640e65e175bSOded Gabbay 		WREG32(mmNIC0_QM0_GLBL_CFG0, 0);
1641e65e175bSOded Gabbay 		WREG32(mmNIC0_QM1_GLBL_CFG0, 0);
1642e65e175bSOded Gabbay 
1643e65e175bSOded Gabbay 		gaudi->hw_cap_initialized &= ~(HW_CAP_NIC0 | HW_CAP_NIC1);
1644e65e175bSOded Gabbay 	}
1645e65e175bSOded Gabbay 
1646e65e175bSOded Gabbay 	rc = hl_fw_send_pci_access_msg(hdev, CPUCP_PACKET_ENABLE_PCI_ACCESS, 0x0);
1647e65e175bSOded Gabbay 	if (rc) {
1648e65e175bSOded Gabbay 		dev_err(hdev->dev, "Failed to enable PCI access from CPU\n");
1649e65e175bSOded Gabbay 		return rc;
1650e65e175bSOded Gabbay 	}
1651e65e175bSOded Gabbay 
1652e65e175bSOded Gabbay 	/* Scrub both SRAM and DRAM */
1653e65e175bSOded Gabbay 	rc = hdev->asic_funcs->scrub_device_mem(hdev);
1654e65e175bSOded Gabbay 	if (rc)
1655e65e175bSOded Gabbay 		goto disable_pci_access;
1656e65e175bSOded Gabbay 
1657e65e175bSOded Gabbay 	rc = gaudi_fetch_psoc_frequency(hdev);
1658e65e175bSOded Gabbay 	if (rc) {
1659e65e175bSOded Gabbay 		dev_err(hdev->dev, "Failed to fetch psoc frequency\n");
1660e65e175bSOded Gabbay 		goto disable_pci_access;
1661e65e175bSOded Gabbay 	}
1662e65e175bSOded Gabbay 
1663e65e175bSOded Gabbay 	rc = gaudi_mmu_clear_pgt_range(hdev);
1664e65e175bSOded Gabbay 	if (rc) {
1665e65e175bSOded Gabbay 		dev_err(hdev->dev, "Failed to clear MMU page tables range\n");
1666e65e175bSOded Gabbay 		goto disable_pci_access;
1667e65e175bSOded Gabbay 	}
1668e65e175bSOded Gabbay 
1669e65e175bSOded Gabbay 	rc = gaudi_init_tpc_mem(hdev);
1670e65e175bSOded Gabbay 	if (rc) {
1671e65e175bSOded Gabbay 		dev_err(hdev->dev, "Failed to initialize TPC memories\n");
1672e65e175bSOded Gabbay 		goto disable_pci_access;
1673e65e175bSOded Gabbay 	}
1674e65e175bSOded Gabbay 
1675e65e175bSOded Gabbay 	rc = gaudi_collective_init(hdev);
1676e65e175bSOded Gabbay 	if (rc) {
1677e65e175bSOded Gabbay 		dev_err(hdev->dev, "Failed to init collective\n");
1678e65e175bSOded Gabbay 		goto disable_pci_access;
1679e65e175bSOded Gabbay 	}
1680e65e175bSOded Gabbay 
1681e65e175bSOded Gabbay 	/* We only support a single ASID for the user, so for the sake of optimization, just
1682e65e175bSOded Gabbay 	 * initialize the ASID one time during device initialization with the fixed value of 1
1683e65e175bSOded Gabbay 	 */
1684e65e175bSOded Gabbay 	gaudi_mmu_prepare(hdev, 1);
1685e65e175bSOded Gabbay 
1686e65e175bSOded Gabbay 	hl_fw_set_pll_profile(hdev);
1687e65e175bSOded Gabbay 
1688e65e175bSOded Gabbay 	return 0;
1689e65e175bSOded Gabbay 
1690e65e175bSOded Gabbay disable_pci_access:
1691e65e175bSOded Gabbay 	hl_fw_send_pci_access_msg(hdev, CPUCP_PACKET_DISABLE_PCI_ACCESS, 0x0);
1692e65e175bSOded Gabbay 
1693e65e175bSOded Gabbay 	return rc;
1694e65e175bSOded Gabbay }
1695e65e175bSOded Gabbay 
1696e65e175bSOded Gabbay static void gaudi_late_fini(struct hl_device *hdev)
1697e65e175bSOded Gabbay {
1698e65e175bSOded Gabbay 	hl_hwmon_release_resources(hdev);
1699e65e175bSOded Gabbay }
1700e65e175bSOded Gabbay 
1701e65e175bSOded Gabbay static int gaudi_alloc_cpu_accessible_dma_mem(struct hl_device *hdev)
1702e65e175bSOded Gabbay {
1703e65e175bSOded Gabbay 	dma_addr_t dma_addr_arr[GAUDI_ALLOC_CPU_MEM_RETRY_CNT] = {}, end_addr;
1704e65e175bSOded Gabbay 	void *virt_addr_arr[GAUDI_ALLOC_CPU_MEM_RETRY_CNT] = {};
1705e65e175bSOded Gabbay 	int i, j, rc = 0;
1706e65e175bSOded Gabbay 
1707e65e175bSOded Gabbay 	/*
1708e65e175bSOded Gabbay 	 * The device CPU works with 40-bits addresses, while bit 39 must be set
1709e65e175bSOded Gabbay 	 * to '1' when accessing the host.
1710e65e175bSOded Gabbay 	 * Bits 49:39 of the full host address are saved for a later
1711e65e175bSOded Gabbay 	 * configuration of the HW to perform extension to 50 bits.
1712e65e175bSOded Gabbay 	 * Because there is a single HW register that holds the extension bits,
1713e65e175bSOded Gabbay 	 * these bits must be identical in all allocated range.
1714e65e175bSOded Gabbay 	 */
1715e65e175bSOded Gabbay 
1716e65e175bSOded Gabbay 	for (i = 0 ; i < GAUDI_ALLOC_CPU_MEM_RETRY_CNT ; i++) {
1717e65e175bSOded Gabbay 		virt_addr_arr[i] = hl_asic_dma_alloc_coherent(hdev, HL_CPU_ACCESSIBLE_MEM_SIZE,
1718e65e175bSOded Gabbay 								&dma_addr_arr[i],
1719e65e175bSOded Gabbay 								GFP_KERNEL | __GFP_ZERO);
1720e65e175bSOded Gabbay 		if (!virt_addr_arr[i]) {
1721e65e175bSOded Gabbay 			rc = -ENOMEM;
1722e65e175bSOded Gabbay 			goto free_dma_mem_arr;
1723e65e175bSOded Gabbay 		}
1724e65e175bSOded Gabbay 
1725e65e175bSOded Gabbay 		end_addr = dma_addr_arr[i] + HL_CPU_ACCESSIBLE_MEM_SIZE - 1;
1726e65e175bSOded Gabbay 		if (GAUDI_CPU_PCI_MSB_ADDR(dma_addr_arr[i]) ==
1727e65e175bSOded Gabbay 				GAUDI_CPU_PCI_MSB_ADDR(end_addr))
1728e65e175bSOded Gabbay 			break;
1729e65e175bSOded Gabbay 	}
1730e65e175bSOded Gabbay 
1731e65e175bSOded Gabbay 	if (i == GAUDI_ALLOC_CPU_MEM_RETRY_CNT) {
1732e65e175bSOded Gabbay 		dev_err(hdev->dev,
1733e65e175bSOded Gabbay 			"MSB of CPU accessible DMA memory are not identical in all range\n");
1734e65e175bSOded Gabbay 		rc = -EFAULT;
1735e65e175bSOded Gabbay 		goto free_dma_mem_arr;
1736e65e175bSOded Gabbay 	}
1737e65e175bSOded Gabbay 
1738e65e175bSOded Gabbay 	hdev->cpu_accessible_dma_mem = virt_addr_arr[i];
1739e65e175bSOded Gabbay 	hdev->cpu_accessible_dma_address = dma_addr_arr[i];
1740e65e175bSOded Gabbay 	hdev->cpu_pci_msb_addr =
1741e65e175bSOded Gabbay 		GAUDI_CPU_PCI_MSB_ADDR(hdev->cpu_accessible_dma_address);
1742e65e175bSOded Gabbay 
1743e65e175bSOded Gabbay 	if (!hdev->asic_prop.fw_security_enabled)
1744e65e175bSOded Gabbay 		GAUDI_PCI_TO_CPU_ADDR(hdev->cpu_accessible_dma_address);
1745e65e175bSOded Gabbay 
1746e65e175bSOded Gabbay free_dma_mem_arr:
1747e65e175bSOded Gabbay 	for (j = 0 ; j < i ; j++)
1748e65e175bSOded Gabbay 		hl_asic_dma_free_coherent(hdev, HL_CPU_ACCESSIBLE_MEM_SIZE, virt_addr_arr[j],
1749e65e175bSOded Gabbay 						dma_addr_arr[j]);
1750e65e175bSOded Gabbay 
1751e65e175bSOded Gabbay 	return rc;
1752e65e175bSOded Gabbay }
1753e65e175bSOded Gabbay 
1754e65e175bSOded Gabbay static void gaudi_free_internal_qmans_pq_mem(struct hl_device *hdev)
1755e65e175bSOded Gabbay {
1756e65e175bSOded Gabbay 	struct gaudi_device *gaudi = hdev->asic_specific;
1757e65e175bSOded Gabbay 	struct gaudi_internal_qman_info *q;
1758e65e175bSOded Gabbay 	u32 i;
1759e65e175bSOded Gabbay 
1760e65e175bSOded Gabbay 	for (i = 0 ; i < GAUDI_QUEUE_ID_SIZE ; i++) {
1761e65e175bSOded Gabbay 		q = &gaudi->internal_qmans[i];
1762e65e175bSOded Gabbay 		if (!q->pq_kernel_addr)
1763e65e175bSOded Gabbay 			continue;
1764e65e175bSOded Gabbay 		hl_asic_dma_free_coherent(hdev, q->pq_size, q->pq_kernel_addr, q->pq_dma_addr);
1765e65e175bSOded Gabbay 	}
1766e65e175bSOded Gabbay }
1767e65e175bSOded Gabbay 
1768e65e175bSOded Gabbay static int gaudi_alloc_internal_qmans_pq_mem(struct hl_device *hdev)
1769e65e175bSOded Gabbay {
1770e65e175bSOded Gabbay 	struct gaudi_device *gaudi = hdev->asic_specific;
1771e65e175bSOded Gabbay 	struct gaudi_internal_qman_info *q;
1772e65e175bSOded Gabbay 	int rc, i;
1773e65e175bSOded Gabbay 
1774e65e175bSOded Gabbay 	for (i = 0 ; i < GAUDI_QUEUE_ID_SIZE ; i++) {
1775e65e175bSOded Gabbay 		if (gaudi_queue_type[i] != QUEUE_TYPE_INT)
1776e65e175bSOded Gabbay 			continue;
1777e65e175bSOded Gabbay 
1778e65e175bSOded Gabbay 		q = &gaudi->internal_qmans[i];
1779e65e175bSOded Gabbay 
1780e65e175bSOded Gabbay 		switch (i) {
1781e65e175bSOded Gabbay 		case GAUDI_QUEUE_ID_DMA_2_0 ... GAUDI_QUEUE_ID_DMA_7_3:
1782e65e175bSOded Gabbay 			q->pq_size = HBM_DMA_QMAN_SIZE_IN_BYTES;
1783e65e175bSOded Gabbay 			break;
1784e65e175bSOded Gabbay 		case GAUDI_QUEUE_ID_MME_0_0 ... GAUDI_QUEUE_ID_MME_1_3:
1785e65e175bSOded Gabbay 			q->pq_size = MME_QMAN_SIZE_IN_BYTES;
1786e65e175bSOded Gabbay 			break;
1787e65e175bSOded Gabbay 		case GAUDI_QUEUE_ID_TPC_0_0 ... GAUDI_QUEUE_ID_TPC_7_3:
1788e65e175bSOded Gabbay 			q->pq_size = TPC_QMAN_SIZE_IN_BYTES;
1789e65e175bSOded Gabbay 			break;
1790e65e175bSOded Gabbay 		case GAUDI_QUEUE_ID_NIC_0_0 ... GAUDI_QUEUE_ID_NIC_9_3:
1791e65e175bSOded Gabbay 			q->pq_size = NIC_QMAN_SIZE_IN_BYTES;
1792e65e175bSOded Gabbay 			break;
1793e65e175bSOded Gabbay 		default:
1794e65e175bSOded Gabbay 			dev_err(hdev->dev, "Bad internal queue index %d", i);
1795e65e175bSOded Gabbay 			rc = -EINVAL;
1796e65e175bSOded Gabbay 			goto free_internal_qmans_pq_mem;
1797e65e175bSOded Gabbay 		}
1798e65e175bSOded Gabbay 
1799e65e175bSOded Gabbay 		q->pq_kernel_addr = hl_asic_dma_alloc_coherent(hdev, q->pq_size, &q->pq_dma_addr,
1800e65e175bSOded Gabbay 								GFP_KERNEL | __GFP_ZERO);
1801e65e175bSOded Gabbay 		if (!q->pq_kernel_addr) {
1802e65e175bSOded Gabbay 			rc = -ENOMEM;
1803e65e175bSOded Gabbay 			goto free_internal_qmans_pq_mem;
1804e65e175bSOded Gabbay 		}
1805e65e175bSOded Gabbay 	}
1806e65e175bSOded Gabbay 
1807e65e175bSOded Gabbay 	return 0;
1808e65e175bSOded Gabbay 
1809e65e175bSOded Gabbay free_internal_qmans_pq_mem:
1810e65e175bSOded Gabbay 	gaudi_free_internal_qmans_pq_mem(hdev);
1811e65e175bSOded Gabbay 	return rc;
1812e65e175bSOded Gabbay }
1813e65e175bSOded Gabbay 
1814e65e175bSOded Gabbay static void gaudi_set_pci_memory_regions(struct hl_device *hdev)
1815e65e175bSOded Gabbay {
1816e65e175bSOded Gabbay 	struct asic_fixed_properties *prop = &hdev->asic_prop;
1817e65e175bSOded Gabbay 	struct pci_mem_region *region;
1818e65e175bSOded Gabbay 
1819e65e175bSOded Gabbay 	/* CFG */
1820e65e175bSOded Gabbay 	region = &hdev->pci_mem_region[PCI_REGION_CFG];
1821e65e175bSOded Gabbay 	region->region_base = CFG_BASE;
1822e65e175bSOded Gabbay 	region->region_size = CFG_SIZE;
1823e65e175bSOded Gabbay 	region->offset_in_bar = CFG_BASE - SPI_FLASH_BASE_ADDR;
1824e65e175bSOded Gabbay 	region->bar_size = CFG_BAR_SIZE;
1825e65e175bSOded Gabbay 	region->bar_id = CFG_BAR_ID;
1826e65e175bSOded Gabbay 	region->used = 1;
1827e65e175bSOded Gabbay 
1828e65e175bSOded Gabbay 	/* SRAM */
1829e65e175bSOded Gabbay 	region = &hdev->pci_mem_region[PCI_REGION_SRAM];
1830e65e175bSOded Gabbay 	region->region_base = SRAM_BASE_ADDR;
1831e65e175bSOded Gabbay 	region->region_size = SRAM_SIZE;
1832e65e175bSOded Gabbay 	region->offset_in_bar = 0;
1833e65e175bSOded Gabbay 	region->bar_size = SRAM_BAR_SIZE;
1834e65e175bSOded Gabbay 	region->bar_id = SRAM_BAR_ID;
1835e65e175bSOded Gabbay 	region->used = 1;
1836e65e175bSOded Gabbay 
1837e65e175bSOded Gabbay 	/* DRAM */
1838e65e175bSOded Gabbay 	region = &hdev->pci_mem_region[PCI_REGION_DRAM];
1839e65e175bSOded Gabbay 	region->region_base = DRAM_PHYS_BASE;
1840e65e175bSOded Gabbay 	region->region_size = hdev->asic_prop.dram_size;
1841e65e175bSOded Gabbay 	region->offset_in_bar = 0;
1842e65e175bSOded Gabbay 	region->bar_size = prop->dram_pci_bar_size;
1843e65e175bSOded Gabbay 	region->bar_id = HBM_BAR_ID;
1844e65e175bSOded Gabbay 	region->used = 1;
1845e65e175bSOded Gabbay 
1846e65e175bSOded Gabbay 	/* SP SRAM */
1847e65e175bSOded Gabbay 	region = &hdev->pci_mem_region[PCI_REGION_SP_SRAM];
1848e65e175bSOded Gabbay 	region->region_base = PSOC_SCRATCHPAD_ADDR;
1849e65e175bSOded Gabbay 	region->region_size = PSOC_SCRATCHPAD_SIZE;
1850e65e175bSOded Gabbay 	region->offset_in_bar = PSOC_SCRATCHPAD_ADDR - SPI_FLASH_BASE_ADDR;
1851e65e175bSOded Gabbay 	region->bar_size = CFG_BAR_SIZE;
1852e65e175bSOded Gabbay 	region->bar_id = CFG_BAR_ID;
1853e65e175bSOded Gabbay 	region->used = 1;
1854e65e175bSOded Gabbay }
1855e65e175bSOded Gabbay 
1856e65e175bSOded Gabbay static int gaudi_sw_init(struct hl_device *hdev)
1857e65e175bSOded Gabbay {
1858e65e175bSOded Gabbay 	struct gaudi_device *gaudi;
1859e65e175bSOded Gabbay 	u32 i, event_id = 0;
1860e65e175bSOded Gabbay 	int rc;
1861e65e175bSOded Gabbay 
1862e65e175bSOded Gabbay 	/* Allocate device structure */
1863e65e175bSOded Gabbay 	gaudi = kzalloc(sizeof(*gaudi), GFP_KERNEL);
1864e65e175bSOded Gabbay 	if (!gaudi)
1865e65e175bSOded Gabbay 		return -ENOMEM;
1866e65e175bSOded Gabbay 
1867e65e175bSOded Gabbay 	for (i = 0 ; i < ARRAY_SIZE(gaudi_irq_map_table) ; i++) {
1868e65e175bSOded Gabbay 		if (gaudi_irq_map_table[i].valid) {
1869e65e175bSOded Gabbay 			if (event_id == GAUDI_EVENT_SIZE) {
1870e65e175bSOded Gabbay 				dev_err(hdev->dev,
1871e65e175bSOded Gabbay 					"Event array exceeds the limit of %u events\n",
1872e65e175bSOded Gabbay 					GAUDI_EVENT_SIZE);
1873e65e175bSOded Gabbay 				rc = -EINVAL;
1874e65e175bSOded Gabbay 				goto free_gaudi_device;
1875e65e175bSOded Gabbay 			}
1876e65e175bSOded Gabbay 
1877e65e175bSOded Gabbay 			gaudi->events[event_id++] =
1878e65e175bSOded Gabbay 					gaudi_irq_map_table[i].fc_id;
1879e65e175bSOded Gabbay 		}
1880e65e175bSOded Gabbay 	}
1881e65e175bSOded Gabbay 
1882e65e175bSOded Gabbay 	gaudi->cpucp_info_get = gaudi_cpucp_info_get;
1883e65e175bSOded Gabbay 
1884e65e175bSOded Gabbay 	hdev->asic_specific = gaudi;
1885e65e175bSOded Gabbay 
1886e65e175bSOded Gabbay 	/* Create DMA pool for small allocations */
1887e65e175bSOded Gabbay 	hdev->dma_pool = dma_pool_create(dev_name(hdev->dev),
1888e65e175bSOded Gabbay 			&hdev->pdev->dev, GAUDI_DMA_POOL_BLK_SIZE, 8, 0);
1889e65e175bSOded Gabbay 	if (!hdev->dma_pool) {
1890e65e175bSOded Gabbay 		dev_err(hdev->dev, "failed to create DMA pool\n");
1891e65e175bSOded Gabbay 		rc = -ENOMEM;
1892e65e175bSOded Gabbay 		goto free_gaudi_device;
1893e65e175bSOded Gabbay 	}
1894e65e175bSOded Gabbay 
1895e65e175bSOded Gabbay 	rc = gaudi_alloc_cpu_accessible_dma_mem(hdev);
1896e65e175bSOded Gabbay 	if (rc)
1897e65e175bSOded Gabbay 		goto free_dma_pool;
1898e65e175bSOded Gabbay 
1899e65e175bSOded Gabbay 	hdev->cpu_accessible_dma_pool = gen_pool_create(ilog2(32), -1);
1900e65e175bSOded Gabbay 	if (!hdev->cpu_accessible_dma_pool) {
1901e65e175bSOded Gabbay 		dev_err(hdev->dev,
1902e65e175bSOded Gabbay 			"Failed to create CPU accessible DMA pool\n");
1903e65e175bSOded Gabbay 		rc = -ENOMEM;
1904e65e175bSOded Gabbay 		goto free_cpu_dma_mem;
1905e65e175bSOded Gabbay 	}
1906e65e175bSOded Gabbay 
1907e65e175bSOded Gabbay 	rc = gen_pool_add(hdev->cpu_accessible_dma_pool,
1908e65e175bSOded Gabbay 				(uintptr_t) hdev->cpu_accessible_dma_mem,
1909e65e175bSOded Gabbay 				HL_CPU_ACCESSIBLE_MEM_SIZE, -1);
1910e65e175bSOded Gabbay 	if (rc) {
1911e65e175bSOded Gabbay 		dev_err(hdev->dev,
1912e65e175bSOded Gabbay 			"Failed to add memory to CPU accessible DMA pool\n");
1913e65e175bSOded Gabbay 		rc = -EFAULT;
1914e65e175bSOded Gabbay 		goto free_cpu_accessible_dma_pool;
1915e65e175bSOded Gabbay 	}
1916e65e175bSOded Gabbay 
1917e65e175bSOded Gabbay 	rc = gaudi_alloc_internal_qmans_pq_mem(hdev);
1918e65e175bSOded Gabbay 	if (rc)
1919e65e175bSOded Gabbay 		goto free_cpu_accessible_dma_pool;
1920e65e175bSOded Gabbay 
1921e65e175bSOded Gabbay 	spin_lock_init(&gaudi->hw_queues_lock);
1922e65e175bSOded Gabbay 
1923e65e175bSOded Gabbay 	hdev->supports_sync_stream = true;
1924e65e175bSOded Gabbay 	hdev->supports_coresight = true;
1925e65e175bSOded Gabbay 	hdev->supports_staged_submission = true;
1926e65e175bSOded Gabbay 	hdev->supports_wait_for_multi_cs = true;
1927e65e175bSOded Gabbay 
1928e65e175bSOded Gabbay 	hdev->asic_funcs->set_pci_memory_regions(hdev);
1929e65e175bSOded Gabbay 	hdev->stream_master_qid_arr =
1930e65e175bSOded Gabbay 				hdev->asic_funcs->get_stream_master_qid_arr();
1931e65e175bSOded Gabbay 	hdev->stream_master_qid_arr_size = GAUDI_STREAM_MASTER_ARR_SIZE;
1932e65e175bSOded Gabbay 
1933e65e175bSOded Gabbay 	return 0;
1934e65e175bSOded Gabbay 
1935e65e175bSOded Gabbay free_cpu_accessible_dma_pool:
1936e65e175bSOded Gabbay 	gen_pool_destroy(hdev->cpu_accessible_dma_pool);
1937e65e175bSOded Gabbay free_cpu_dma_mem:
1938e65e175bSOded Gabbay 	if (!hdev->asic_prop.fw_security_enabled)
1939e65e175bSOded Gabbay 		GAUDI_CPU_TO_PCI_ADDR(hdev->cpu_accessible_dma_address,
1940e65e175bSOded Gabbay 					hdev->cpu_pci_msb_addr);
1941e65e175bSOded Gabbay 	hl_asic_dma_free_coherent(hdev, HL_CPU_ACCESSIBLE_MEM_SIZE, hdev->cpu_accessible_dma_mem,
1942e65e175bSOded Gabbay 					hdev->cpu_accessible_dma_address);
1943e65e175bSOded Gabbay free_dma_pool:
1944e65e175bSOded Gabbay 	dma_pool_destroy(hdev->dma_pool);
1945e65e175bSOded Gabbay free_gaudi_device:
1946e65e175bSOded Gabbay 	kfree(gaudi);
1947e65e175bSOded Gabbay 	return rc;
1948e65e175bSOded Gabbay }
1949e65e175bSOded Gabbay 
1950e65e175bSOded Gabbay static int gaudi_sw_fini(struct hl_device *hdev)
1951e65e175bSOded Gabbay {
1952e65e175bSOded Gabbay 	struct gaudi_device *gaudi = hdev->asic_specific;
1953e65e175bSOded Gabbay 
1954e65e175bSOded Gabbay 	gaudi_free_internal_qmans_pq_mem(hdev);
1955e65e175bSOded Gabbay 
1956e65e175bSOded Gabbay 	gen_pool_destroy(hdev->cpu_accessible_dma_pool);
1957e65e175bSOded Gabbay 
1958e65e175bSOded Gabbay 	if (!hdev->asic_prop.fw_security_enabled)
1959e65e175bSOded Gabbay 		GAUDI_CPU_TO_PCI_ADDR(hdev->cpu_accessible_dma_address,
1960e65e175bSOded Gabbay 					hdev->cpu_pci_msb_addr);
1961e65e175bSOded Gabbay 
1962e65e175bSOded Gabbay 	hl_asic_dma_free_coherent(hdev, HL_CPU_ACCESSIBLE_MEM_SIZE, hdev->cpu_accessible_dma_mem,
1963e65e175bSOded Gabbay 					hdev->cpu_accessible_dma_address);
1964e65e175bSOded Gabbay 
1965e65e175bSOded Gabbay 	dma_pool_destroy(hdev->dma_pool);
1966e65e175bSOded Gabbay 
1967e65e175bSOded Gabbay 	kfree(gaudi);
1968e65e175bSOded Gabbay 
1969e65e175bSOded Gabbay 	return 0;
1970e65e175bSOded Gabbay }
1971e65e175bSOded Gabbay 
1972e65e175bSOded Gabbay static irqreturn_t gaudi_irq_handler_single(int irq, void *arg)
1973e65e175bSOded Gabbay {
1974e65e175bSOded Gabbay 	struct hl_device *hdev = arg;
1975e65e175bSOded Gabbay 	int i;
1976e65e175bSOded Gabbay 
1977e65e175bSOded Gabbay 	if (hdev->disabled)
1978e65e175bSOded Gabbay 		return IRQ_HANDLED;
1979e65e175bSOded Gabbay 
1980e65e175bSOded Gabbay 	for (i = 0 ; i < hdev->asic_prop.completion_queues_count ; i++)
1981e65e175bSOded Gabbay 		hl_irq_handler_cq(irq, &hdev->completion_queue[i]);
1982e65e175bSOded Gabbay 
1983e65e175bSOded Gabbay 	hl_irq_handler_eq(irq, &hdev->event_queue);
1984e65e175bSOded Gabbay 
1985e65e175bSOded Gabbay 	return IRQ_HANDLED;
1986e65e175bSOded Gabbay }
1987e65e175bSOded Gabbay 
1988e65e175bSOded Gabbay /*
1989e65e175bSOded Gabbay  * For backward compatibility, new MSI interrupts should be set after the
1990e65e175bSOded Gabbay  * existing CPU and NIC interrupts.
1991e65e175bSOded Gabbay  */
1992e65e175bSOded Gabbay static int gaudi_pci_irq_vector(struct hl_device *hdev, unsigned int nr,
1993e65e175bSOded Gabbay 				bool cpu_eq)
1994e65e175bSOded Gabbay {
1995e65e175bSOded Gabbay 	int msi_vec;
1996e65e175bSOded Gabbay 
1997e65e175bSOded Gabbay 	if ((nr != GAUDI_EVENT_QUEUE_MSI_IDX) && (cpu_eq))
1998e65e175bSOded Gabbay 		dev_crit(hdev->dev, "CPU EQ must use IRQ %d\n",
1999e65e175bSOded Gabbay 				GAUDI_EVENT_QUEUE_MSI_IDX);
2000e65e175bSOded Gabbay 
2001e65e175bSOded Gabbay 	msi_vec = ((nr < GAUDI_EVENT_QUEUE_MSI_IDX) || (cpu_eq)) ? nr :
2002e65e175bSOded Gabbay 			(nr + NIC_NUMBER_OF_ENGINES + 1);
2003e65e175bSOded Gabbay 
2004e65e175bSOded Gabbay 	return pci_irq_vector(hdev->pdev, msi_vec);
2005e65e175bSOded Gabbay }
2006e65e175bSOded Gabbay 
2007e65e175bSOded Gabbay static int gaudi_enable_msi_single(struct hl_device *hdev)
2008e65e175bSOded Gabbay {
2009e65e175bSOded Gabbay 	int rc, irq;
2010e65e175bSOded Gabbay 
2011e65e175bSOded Gabbay 	dev_dbg(hdev->dev, "Working in single MSI IRQ mode\n");
2012e65e175bSOded Gabbay 
2013e65e175bSOded Gabbay 	irq = gaudi_pci_irq_vector(hdev, 0, false);
2014e65e175bSOded Gabbay 	rc = request_irq(irq, gaudi_irq_handler_single, 0,
2015e65e175bSOded Gabbay 			"gaudi single msi", hdev);
2016e65e175bSOded Gabbay 	if (rc)
2017e65e175bSOded Gabbay 		dev_err(hdev->dev,
2018e65e175bSOded Gabbay 			"Failed to request single MSI IRQ\n");
2019e65e175bSOded Gabbay 
2020e65e175bSOded Gabbay 	return rc;
2021e65e175bSOded Gabbay }
2022e65e175bSOded Gabbay 
2023e65e175bSOded Gabbay static int gaudi_enable_msi_multi(struct hl_device *hdev)
2024e65e175bSOded Gabbay {
2025e65e175bSOded Gabbay 	int cq_cnt = hdev->asic_prop.completion_queues_count;
2026e65e175bSOded Gabbay 	int rc, i, irq_cnt_init, irq;
2027e65e175bSOded Gabbay 
2028e65e175bSOded Gabbay 	for (i = 0, irq_cnt_init = 0 ; i < cq_cnt ; i++, irq_cnt_init++) {
2029e65e175bSOded Gabbay 		irq = gaudi_pci_irq_vector(hdev, i, false);
2030e65e175bSOded Gabbay 		rc = request_irq(irq, hl_irq_handler_cq, 0, gaudi_irq_name[i],
2031e65e175bSOded Gabbay 				&hdev->completion_queue[i]);
2032e65e175bSOded Gabbay 		if (rc) {
2033e65e175bSOded Gabbay 			dev_err(hdev->dev, "Failed to request IRQ %d", irq);
2034e65e175bSOded Gabbay 			goto free_irqs;
2035e65e175bSOded Gabbay 		}
2036e65e175bSOded Gabbay 	}
2037e65e175bSOded Gabbay 
2038e65e175bSOded Gabbay 	irq = gaudi_pci_irq_vector(hdev, GAUDI_EVENT_QUEUE_MSI_IDX, true);
2039e65e175bSOded Gabbay 	rc = request_irq(irq, hl_irq_handler_eq, 0, gaudi_irq_name[cq_cnt],
2040e65e175bSOded Gabbay 				&hdev->event_queue);
2041e65e175bSOded Gabbay 	if (rc) {
2042e65e175bSOded Gabbay 		dev_err(hdev->dev, "Failed to request IRQ %d", irq);
2043e65e175bSOded Gabbay 		goto free_irqs;
2044e65e175bSOded Gabbay 	}
2045e65e175bSOded Gabbay 
2046e65e175bSOded Gabbay 	return 0;
2047e65e175bSOded Gabbay 
2048e65e175bSOded Gabbay free_irqs:
2049e65e175bSOded Gabbay 	for (i = 0 ; i < irq_cnt_init ; i++)
2050e65e175bSOded Gabbay 		free_irq(gaudi_pci_irq_vector(hdev, i, false),
2051e65e175bSOded Gabbay 				&hdev->completion_queue[i]);
2052e65e175bSOded Gabbay 	return rc;
2053e65e175bSOded Gabbay }
2054e65e175bSOded Gabbay 
2055e65e175bSOded Gabbay static int gaudi_enable_msi(struct hl_device *hdev)
2056e65e175bSOded Gabbay {
2057e65e175bSOded Gabbay 	struct gaudi_device *gaudi = hdev->asic_specific;
2058e65e175bSOded Gabbay 	int rc;
2059e65e175bSOded Gabbay 
2060e65e175bSOded Gabbay 	if (gaudi->hw_cap_initialized & HW_CAP_MSI)
2061e65e175bSOded Gabbay 		return 0;
2062e65e175bSOded Gabbay 
2063e65e175bSOded Gabbay 	rc = pci_alloc_irq_vectors(hdev->pdev, 1, 1, PCI_IRQ_MSI);
2064e65e175bSOded Gabbay 	if (rc < 0) {
2065e65e175bSOded Gabbay 		dev_err(hdev->dev, "MSI: Failed to enable support %d\n", rc);
2066e65e175bSOded Gabbay 		return rc;
2067e65e175bSOded Gabbay 	}
2068e65e175bSOded Gabbay 
2069e65e175bSOded Gabbay 	if (rc < NUMBER_OF_INTERRUPTS) {
2070e65e175bSOded Gabbay 		gaudi->multi_msi_mode = false;
2071e65e175bSOded Gabbay 		rc = gaudi_enable_msi_single(hdev);
2072e65e175bSOded Gabbay 	} else {
2073e65e175bSOded Gabbay 		gaudi->multi_msi_mode = true;
2074e65e175bSOded Gabbay 		rc = gaudi_enable_msi_multi(hdev);
2075e65e175bSOded Gabbay 	}
2076e65e175bSOded Gabbay 
2077e65e175bSOded Gabbay 	if (rc)
2078e65e175bSOded Gabbay 		goto free_pci_irq_vectors;
2079e65e175bSOded Gabbay 
2080e65e175bSOded Gabbay 	gaudi->hw_cap_initialized |= HW_CAP_MSI;
2081e65e175bSOded Gabbay 
2082e65e175bSOded Gabbay 	return 0;
2083e65e175bSOded Gabbay 
2084e65e175bSOded Gabbay free_pci_irq_vectors:
2085e65e175bSOded Gabbay 	pci_free_irq_vectors(hdev->pdev);
2086e65e175bSOded Gabbay 	return rc;
2087e65e175bSOded Gabbay }
2088e65e175bSOded Gabbay 
2089e65e175bSOded Gabbay static void gaudi_sync_irqs(struct hl_device *hdev)
2090e65e175bSOded Gabbay {
2091e65e175bSOded Gabbay 	struct gaudi_device *gaudi = hdev->asic_specific;
2092e65e175bSOded Gabbay 	int i, cq_cnt = hdev->asic_prop.completion_queues_count;
2093e65e175bSOded Gabbay 
2094e65e175bSOded Gabbay 	if (!(gaudi->hw_cap_initialized & HW_CAP_MSI))
2095e65e175bSOded Gabbay 		return;
2096e65e175bSOded Gabbay 
2097e65e175bSOded Gabbay 	/* Wait for all pending IRQs to be finished */
2098e65e175bSOded Gabbay 	if (gaudi->multi_msi_mode) {
2099e65e175bSOded Gabbay 		for (i = 0 ; i < cq_cnt ; i++)
2100e65e175bSOded Gabbay 			synchronize_irq(gaudi_pci_irq_vector(hdev, i, false));
2101e65e175bSOded Gabbay 
2102e65e175bSOded Gabbay 		synchronize_irq(gaudi_pci_irq_vector(hdev,
2103e65e175bSOded Gabbay 						GAUDI_EVENT_QUEUE_MSI_IDX,
2104e65e175bSOded Gabbay 						true));
2105e65e175bSOded Gabbay 	} else {
2106e65e175bSOded Gabbay 		synchronize_irq(gaudi_pci_irq_vector(hdev, 0, false));
2107e65e175bSOded Gabbay 	}
2108e65e175bSOded Gabbay }
2109e65e175bSOded Gabbay 
2110e65e175bSOded Gabbay static void gaudi_disable_msi(struct hl_device *hdev)
2111e65e175bSOded Gabbay {
2112e65e175bSOded Gabbay 	struct gaudi_device *gaudi = hdev->asic_specific;
2113e65e175bSOded Gabbay 	int i, irq, cq_cnt = hdev->asic_prop.completion_queues_count;
2114e65e175bSOded Gabbay 
2115e65e175bSOded Gabbay 	if (!(gaudi->hw_cap_initialized & HW_CAP_MSI))
2116e65e175bSOded Gabbay 		return;
2117e65e175bSOded Gabbay 
2118e65e175bSOded Gabbay 	gaudi_sync_irqs(hdev);
2119e65e175bSOded Gabbay 
2120e65e175bSOded Gabbay 	if (gaudi->multi_msi_mode) {
2121e65e175bSOded Gabbay 		irq = gaudi_pci_irq_vector(hdev, GAUDI_EVENT_QUEUE_MSI_IDX,
2122e65e175bSOded Gabbay 						true);
2123e65e175bSOded Gabbay 		free_irq(irq, &hdev->event_queue);
2124e65e175bSOded Gabbay 
2125e65e175bSOded Gabbay 		for (i = 0 ; i < cq_cnt ; i++) {
2126e65e175bSOded Gabbay 			irq = gaudi_pci_irq_vector(hdev, i, false);
2127e65e175bSOded Gabbay 			free_irq(irq, &hdev->completion_queue[i]);
2128e65e175bSOded Gabbay 		}
2129e65e175bSOded Gabbay 	} else {
2130e65e175bSOded Gabbay 		free_irq(gaudi_pci_irq_vector(hdev, 0, false), hdev);
2131e65e175bSOded Gabbay 	}
2132e65e175bSOded Gabbay 
2133e65e175bSOded Gabbay 	pci_free_irq_vectors(hdev->pdev);
2134e65e175bSOded Gabbay 
2135e65e175bSOded Gabbay 	gaudi->hw_cap_initialized &= ~HW_CAP_MSI;
2136e65e175bSOded Gabbay }
2137e65e175bSOded Gabbay 
2138e65e175bSOded Gabbay static void gaudi_init_scrambler_sram(struct hl_device *hdev)
2139e65e175bSOded Gabbay {
2140e65e175bSOded Gabbay 	struct gaudi_device *gaudi = hdev->asic_specific;
2141e65e175bSOded Gabbay 
2142e65e175bSOded Gabbay 	if (hdev->asic_prop.fw_security_enabled)
2143e65e175bSOded Gabbay 		return;
2144e65e175bSOded Gabbay 
2145e65e175bSOded Gabbay 	if (hdev->asic_prop.fw_app_cpu_boot_dev_sts0 &
2146e65e175bSOded Gabbay 						CPU_BOOT_DEV_STS0_SRAM_SCR_EN)
2147e65e175bSOded Gabbay 		return;
2148e65e175bSOded Gabbay 
2149e65e175bSOded Gabbay 	if (gaudi->hw_cap_initialized & HW_CAP_SRAM_SCRAMBLER)
2150e65e175bSOded Gabbay 		return;
2151e65e175bSOded Gabbay 
2152e65e175bSOded Gabbay 	WREG32(mmNIF_RTR_CTRL_0_SCRAM_SRAM_EN,
2153e65e175bSOded Gabbay 			1 << IF_RTR_CTRL_SCRAM_SRAM_EN_VAL_SHIFT);
2154e65e175bSOded Gabbay 	WREG32(mmNIF_RTR_CTRL_1_SCRAM_SRAM_EN,
2155e65e175bSOded Gabbay 			1 << IF_RTR_CTRL_SCRAM_SRAM_EN_VAL_SHIFT);
2156e65e175bSOded Gabbay 	WREG32(mmNIF_RTR_CTRL_2_SCRAM_SRAM_EN,
2157e65e175bSOded Gabbay 			1 << IF_RTR_CTRL_SCRAM_SRAM_EN_VAL_SHIFT);
2158e65e175bSOded Gabbay 	WREG32(mmNIF_RTR_CTRL_3_SCRAM_SRAM_EN,
2159e65e175bSOded Gabbay 			1 << IF_RTR_CTRL_SCRAM_SRAM_EN_VAL_SHIFT);
2160e65e175bSOded Gabbay 	WREG32(mmNIF_RTR_CTRL_4_SCRAM_SRAM_EN,
2161e65e175bSOded Gabbay 			1 << IF_RTR_CTRL_SCRAM_SRAM_EN_VAL_SHIFT);
2162e65e175bSOded Gabbay 	WREG32(mmNIF_RTR_CTRL_5_SCRAM_SRAM_EN,
2163e65e175bSOded Gabbay 			1 << IF_RTR_CTRL_SCRAM_SRAM_EN_VAL_SHIFT);
2164e65e175bSOded Gabbay 	WREG32(mmNIF_RTR_CTRL_6_SCRAM_SRAM_EN,
2165e65e175bSOded Gabbay 			1 << IF_RTR_CTRL_SCRAM_SRAM_EN_VAL_SHIFT);
2166e65e175bSOded Gabbay 	WREG32(mmNIF_RTR_CTRL_7_SCRAM_SRAM_EN,
2167e65e175bSOded Gabbay 			1 << IF_RTR_CTRL_SCRAM_SRAM_EN_VAL_SHIFT);
2168e65e175bSOded Gabbay 
2169e65e175bSOded Gabbay 	WREG32(mmSIF_RTR_CTRL_0_SCRAM_SRAM_EN,
2170e65e175bSOded Gabbay 			1 << IF_RTR_CTRL_SCRAM_SRAM_EN_VAL_SHIFT);
2171e65e175bSOded Gabbay 	WREG32(mmSIF_RTR_CTRL_1_SCRAM_SRAM_EN,
2172e65e175bSOded Gabbay 			1 << IF_RTR_CTRL_SCRAM_SRAM_EN_VAL_SHIFT);
2173e65e175bSOded Gabbay 	WREG32(mmSIF_RTR_CTRL_2_SCRAM_SRAM_EN,
2174e65e175bSOded Gabbay 			1 << IF_RTR_CTRL_SCRAM_SRAM_EN_VAL_SHIFT);
2175e65e175bSOded Gabbay 	WREG32(mmSIF_RTR_CTRL_3_SCRAM_SRAM_EN,
2176e65e175bSOded Gabbay 			1 << IF_RTR_CTRL_SCRAM_SRAM_EN_VAL_SHIFT);
2177e65e175bSOded Gabbay 	WREG32(mmSIF_RTR_CTRL_4_SCRAM_SRAM_EN,
2178e65e175bSOded Gabbay 			1 << IF_RTR_CTRL_SCRAM_SRAM_EN_VAL_SHIFT);
2179e65e175bSOded Gabbay 	WREG32(mmSIF_RTR_CTRL_5_SCRAM_SRAM_EN,
2180e65e175bSOded Gabbay 			1 << IF_RTR_CTRL_SCRAM_SRAM_EN_VAL_SHIFT);
2181e65e175bSOded Gabbay 	WREG32(mmSIF_RTR_CTRL_6_SCRAM_SRAM_EN,
2182e65e175bSOded Gabbay 			1 << IF_RTR_CTRL_SCRAM_SRAM_EN_VAL_SHIFT);
2183e65e175bSOded Gabbay 	WREG32(mmSIF_RTR_CTRL_7_SCRAM_SRAM_EN,
2184e65e175bSOded Gabbay 			1 << IF_RTR_CTRL_SCRAM_SRAM_EN_VAL_SHIFT);
2185e65e175bSOded Gabbay 
2186e65e175bSOded Gabbay 	WREG32(mmDMA_IF_E_N_DOWN_CH0_SCRAM_SRAM_EN,
2187e65e175bSOded Gabbay 			1 << DMA_IF_DOWN_CHX_SCRAM_SRAM_EN_VAL_SHIFT);
2188e65e175bSOded Gabbay 	WREG32(mmDMA_IF_E_N_DOWN_CH1_SCRAM_SRAM_EN,
2189e65e175bSOded Gabbay 			1 << DMA_IF_DOWN_CHX_SCRAM_SRAM_EN_VAL_SHIFT);
2190e65e175bSOded Gabbay 	WREG32(mmDMA_IF_E_S_DOWN_CH0_SCRAM_SRAM_EN,
2191e65e175bSOded Gabbay 			1 << DMA_IF_DOWN_CHX_SCRAM_SRAM_EN_VAL_SHIFT);
2192e65e175bSOded Gabbay 	WREG32(mmDMA_IF_E_S_DOWN_CH1_SCRAM_SRAM_EN,
2193e65e175bSOded Gabbay 			1 << DMA_IF_DOWN_CHX_SCRAM_SRAM_EN_VAL_SHIFT);
2194e65e175bSOded Gabbay 	WREG32(mmDMA_IF_W_N_DOWN_CH0_SCRAM_SRAM_EN,
2195e65e175bSOded Gabbay 			1 << DMA_IF_DOWN_CHX_SCRAM_SRAM_EN_VAL_SHIFT);
2196e65e175bSOded Gabbay 	WREG32(mmDMA_IF_W_N_DOWN_CH1_SCRAM_SRAM_EN,
2197e65e175bSOded Gabbay 			1 << DMA_IF_DOWN_CHX_SCRAM_SRAM_EN_VAL_SHIFT);
2198e65e175bSOded Gabbay 	WREG32(mmDMA_IF_W_S_DOWN_CH0_SCRAM_SRAM_EN,
2199e65e175bSOded Gabbay 			1 << DMA_IF_DOWN_CHX_SCRAM_SRAM_EN_VAL_SHIFT);
2200e65e175bSOded Gabbay 	WREG32(mmDMA_IF_W_S_DOWN_CH1_SCRAM_SRAM_EN,
2201e65e175bSOded Gabbay 			1 << DMA_IF_DOWN_CHX_SCRAM_SRAM_EN_VAL_SHIFT);
2202e65e175bSOded Gabbay 
2203e65e175bSOded Gabbay 	gaudi->hw_cap_initialized |= HW_CAP_SRAM_SCRAMBLER;
2204e65e175bSOded Gabbay }
2205e65e175bSOded Gabbay 
2206e65e175bSOded Gabbay static void gaudi_init_scrambler_hbm(struct hl_device *hdev)
2207e65e175bSOded Gabbay {
2208e65e175bSOded Gabbay 	struct gaudi_device *gaudi = hdev->asic_specific;
2209e65e175bSOded Gabbay 
2210e65e175bSOded Gabbay 	if (hdev->asic_prop.fw_security_enabled)
2211e65e175bSOded Gabbay 		return;
2212e65e175bSOded Gabbay 
2213e65e175bSOded Gabbay 	if (hdev->asic_prop.fw_bootfit_cpu_boot_dev_sts0 &
2214e65e175bSOded Gabbay 					CPU_BOOT_DEV_STS0_DRAM_SCR_EN)
2215e65e175bSOded Gabbay 		return;
2216e65e175bSOded Gabbay 
2217e65e175bSOded Gabbay 	if (gaudi->hw_cap_initialized & HW_CAP_HBM_SCRAMBLER)
2218e65e175bSOded Gabbay 		return;
2219e65e175bSOded Gabbay 
2220e65e175bSOded Gabbay 	WREG32(mmNIF_RTR_CTRL_0_SCRAM_HBM_EN,
2221e65e175bSOded Gabbay 			1 << IF_RTR_CTRL_SCRAM_HBM_EN_VAL_SHIFT);
2222e65e175bSOded Gabbay 	WREG32(mmNIF_RTR_CTRL_1_SCRAM_HBM_EN,
2223e65e175bSOded Gabbay 			1 << IF_RTR_CTRL_SCRAM_HBM_EN_VAL_SHIFT);
2224e65e175bSOded Gabbay 	WREG32(mmNIF_RTR_CTRL_2_SCRAM_HBM_EN,
2225e65e175bSOded Gabbay 			1 << IF_RTR_CTRL_SCRAM_HBM_EN_VAL_SHIFT);
2226e65e175bSOded Gabbay 	WREG32(mmNIF_RTR_CTRL_3_SCRAM_HBM_EN,
2227e65e175bSOded Gabbay 			1 << IF_RTR_CTRL_SCRAM_HBM_EN_VAL_SHIFT);
2228e65e175bSOded Gabbay 	WREG32(mmNIF_RTR_CTRL_4_SCRAM_HBM_EN,
2229e65e175bSOded Gabbay 			1 << IF_RTR_CTRL_SCRAM_HBM_EN_VAL_SHIFT);
2230e65e175bSOded Gabbay 	WREG32(mmNIF_RTR_CTRL_5_SCRAM_HBM_EN,
2231e65e175bSOded Gabbay 			1 << IF_RTR_CTRL_SCRAM_HBM_EN_VAL_SHIFT);
2232e65e175bSOded Gabbay 	WREG32(mmNIF_RTR_CTRL_6_SCRAM_HBM_EN,
2233e65e175bSOded Gabbay 			1 << IF_RTR_CTRL_SCRAM_HBM_EN_VAL_SHIFT);
2234e65e175bSOded Gabbay 	WREG32(mmNIF_RTR_CTRL_7_SCRAM_HBM_EN,
2235e65e175bSOded Gabbay 			1 << IF_RTR_CTRL_SCRAM_HBM_EN_VAL_SHIFT);
2236e65e175bSOded Gabbay 
2237e65e175bSOded Gabbay 	WREG32(mmSIF_RTR_CTRL_0_SCRAM_HBM_EN,
2238e65e175bSOded Gabbay 			1 << IF_RTR_CTRL_SCRAM_HBM_EN_VAL_SHIFT);
2239e65e175bSOded Gabbay 	WREG32(mmSIF_RTR_CTRL_1_SCRAM_HBM_EN,
2240e65e175bSOded Gabbay 			1 << IF_RTR_CTRL_SCRAM_HBM_EN_VAL_SHIFT);
2241e65e175bSOded Gabbay 	WREG32(mmSIF_RTR_CTRL_2_SCRAM_HBM_EN,
2242e65e175bSOded Gabbay 			1 << IF_RTR_CTRL_SCRAM_HBM_EN_VAL_SHIFT);
2243e65e175bSOded Gabbay 	WREG32(mmSIF_RTR_CTRL_3_SCRAM_HBM_EN,
2244e65e175bSOded Gabbay 			1 << IF_RTR_CTRL_SCRAM_HBM_EN_VAL_SHIFT);
2245e65e175bSOded Gabbay 	WREG32(mmSIF_RTR_CTRL_4_SCRAM_HBM_EN,
2246e65e175bSOded Gabbay 			1 << IF_RTR_CTRL_SCRAM_HBM_EN_VAL_SHIFT);
2247e65e175bSOded Gabbay 	WREG32(mmSIF_RTR_CTRL_5_SCRAM_HBM_EN,
2248e65e175bSOded Gabbay 			1 << IF_RTR_CTRL_SCRAM_HBM_EN_VAL_SHIFT);
2249e65e175bSOded Gabbay 	WREG32(mmSIF_RTR_CTRL_6_SCRAM_HBM_EN,
2250e65e175bSOded Gabbay 			1 << IF_RTR_CTRL_SCRAM_HBM_EN_VAL_SHIFT);
2251e65e175bSOded Gabbay 	WREG32(mmSIF_RTR_CTRL_7_SCRAM_HBM_EN,
2252e65e175bSOded Gabbay 			1 << IF_RTR_CTRL_SCRAM_HBM_EN_VAL_SHIFT);
2253e65e175bSOded Gabbay 
2254e65e175bSOded Gabbay 	WREG32(mmDMA_IF_E_N_DOWN_CH0_SCRAM_HBM_EN,
2255e65e175bSOded Gabbay 			1 << DMA_IF_DOWN_CHX_SCRAM_HBM_EN_VAL_SHIFT);
2256e65e175bSOded Gabbay 	WREG32(mmDMA_IF_E_N_DOWN_CH1_SCRAM_HBM_EN,
2257e65e175bSOded Gabbay 			1 << DMA_IF_DOWN_CHX_SCRAM_HBM_EN_VAL_SHIFT);
2258e65e175bSOded Gabbay 	WREG32(mmDMA_IF_E_S_DOWN_CH0_SCRAM_HBM_EN,
2259e65e175bSOded Gabbay 			1 << DMA_IF_DOWN_CHX_SCRAM_HBM_EN_VAL_SHIFT);
2260e65e175bSOded Gabbay 	WREG32(mmDMA_IF_E_S_DOWN_CH1_SCRAM_HBM_EN,
2261e65e175bSOded Gabbay 			1 << DMA_IF_DOWN_CHX_SCRAM_HBM_EN_VAL_SHIFT);
2262e65e175bSOded Gabbay 	WREG32(mmDMA_IF_W_N_DOWN_CH0_SCRAM_HBM_EN,
2263e65e175bSOded Gabbay 			1 << DMA_IF_DOWN_CHX_SCRAM_HBM_EN_VAL_SHIFT);
2264e65e175bSOded Gabbay 	WREG32(mmDMA_IF_W_N_DOWN_CH1_SCRAM_HBM_EN,
2265e65e175bSOded Gabbay 			1 << DMA_IF_DOWN_CHX_SCRAM_HBM_EN_VAL_SHIFT);
2266e65e175bSOded Gabbay 	WREG32(mmDMA_IF_W_S_DOWN_CH0_SCRAM_HBM_EN,
2267e65e175bSOded Gabbay 			1 << DMA_IF_DOWN_CHX_SCRAM_HBM_EN_VAL_SHIFT);
2268e65e175bSOded Gabbay 	WREG32(mmDMA_IF_W_S_DOWN_CH1_SCRAM_HBM_EN,
2269e65e175bSOded Gabbay 			1 << DMA_IF_DOWN_CHX_SCRAM_HBM_EN_VAL_SHIFT);
2270e65e175bSOded Gabbay 
2271e65e175bSOded Gabbay 	gaudi->hw_cap_initialized |= HW_CAP_HBM_SCRAMBLER;
2272e65e175bSOded Gabbay }
2273e65e175bSOded Gabbay 
2274e65e175bSOded Gabbay static void gaudi_init_e2e(struct hl_device *hdev)
2275e65e175bSOded Gabbay {
2276e65e175bSOded Gabbay 	if (hdev->asic_prop.fw_security_enabled)
2277e65e175bSOded Gabbay 		return;
2278e65e175bSOded Gabbay 
2279e65e175bSOded Gabbay 	if (hdev->asic_prop.fw_bootfit_cpu_boot_dev_sts0 &
2280e65e175bSOded Gabbay 					CPU_BOOT_DEV_STS0_E2E_CRED_EN)
2281e65e175bSOded Gabbay 		return;
2282e65e175bSOded Gabbay 
2283e65e175bSOded Gabbay 	WREG32(mmSIF_RTR_CTRL_0_E2E_HBM_WR_SIZE, 247 >> 3);
2284e65e175bSOded Gabbay 	WREG32(mmSIF_RTR_CTRL_0_E2E_HBM_RD_SIZE, 785 >> 3);
2285e65e175bSOded Gabbay 	WREG32(mmSIF_RTR_CTRL_0_E2E_PCI_WR_SIZE, 49);
2286e65e175bSOded Gabbay 	WREG32(mmSIF_RTR_CTRL_0_E2E_PCI_RD_SIZE, 101);
2287e65e175bSOded Gabbay 
2288e65e175bSOded Gabbay 	WREG32(mmSIF_RTR_CTRL_1_E2E_HBM_WR_SIZE, 275 >> 3);
2289e65e175bSOded Gabbay 	WREG32(mmSIF_RTR_CTRL_1_E2E_HBM_RD_SIZE, 614 >> 3);
2290e65e175bSOded Gabbay 	WREG32(mmSIF_RTR_CTRL_1_E2E_PCI_WR_SIZE, 1);
2291e65e175bSOded Gabbay 	WREG32(mmSIF_RTR_CTRL_1_E2E_PCI_RD_SIZE, 39);
2292e65e175bSOded Gabbay 
2293e65e175bSOded Gabbay 	WREG32(mmSIF_RTR_CTRL_2_E2E_HBM_WR_SIZE, 1);
2294e65e175bSOded Gabbay 	WREG32(mmSIF_RTR_CTRL_2_E2E_HBM_RD_SIZE, 1);
2295e65e175bSOded Gabbay 	WREG32(mmSIF_RTR_CTRL_2_E2E_PCI_WR_SIZE, 1);
2296e65e175bSOded Gabbay 	WREG32(mmSIF_RTR_CTRL_2_E2E_PCI_RD_SIZE, 32);
2297e65e175bSOded Gabbay 
2298e65e175bSOded Gabbay 	WREG32(mmSIF_RTR_CTRL_3_E2E_HBM_WR_SIZE, 176 >> 3);
2299e65e175bSOded Gabbay 	WREG32(mmSIF_RTR_CTRL_3_E2E_HBM_RD_SIZE, 32 >> 3);
2300e65e175bSOded Gabbay 	WREG32(mmSIF_RTR_CTRL_3_E2E_PCI_WR_SIZE, 19);
2301e65e175bSOded Gabbay 	WREG32(mmSIF_RTR_CTRL_3_E2E_PCI_RD_SIZE, 32);
2302e65e175bSOded Gabbay 
2303e65e175bSOded Gabbay 	WREG32(mmSIF_RTR_CTRL_4_E2E_HBM_WR_SIZE, 176 >> 3);
2304e65e175bSOded Gabbay 	WREG32(mmSIF_RTR_CTRL_4_E2E_HBM_RD_SIZE, 32 >> 3);
2305e65e175bSOded Gabbay 	WREG32(mmSIF_RTR_CTRL_4_E2E_PCI_WR_SIZE, 19);
2306e65e175bSOded Gabbay 	WREG32(mmSIF_RTR_CTRL_4_E2E_PCI_RD_SIZE, 32);
2307e65e175bSOded Gabbay 
2308e65e175bSOded Gabbay 	WREG32(mmSIF_RTR_CTRL_5_E2E_HBM_WR_SIZE, 1);
2309e65e175bSOded Gabbay 	WREG32(mmSIF_RTR_CTRL_5_E2E_HBM_RD_SIZE, 1);
2310e65e175bSOded Gabbay 	WREG32(mmSIF_RTR_CTRL_5_E2E_PCI_WR_SIZE, 1);
2311e65e175bSOded Gabbay 	WREG32(mmSIF_RTR_CTRL_5_E2E_PCI_RD_SIZE, 32);
2312e65e175bSOded Gabbay 
2313e65e175bSOded Gabbay 	WREG32(mmSIF_RTR_CTRL_6_E2E_HBM_WR_SIZE, 275 >> 3);
2314e65e175bSOded Gabbay 	WREG32(mmSIF_RTR_CTRL_6_E2E_HBM_RD_SIZE, 614 >> 3);
2315e65e175bSOded Gabbay 	WREG32(mmSIF_RTR_CTRL_6_E2E_PCI_WR_SIZE, 1);
2316e65e175bSOded Gabbay 	WREG32(mmSIF_RTR_CTRL_6_E2E_PCI_RD_SIZE, 39);
2317e65e175bSOded Gabbay 
2318e65e175bSOded Gabbay 	WREG32(mmSIF_RTR_CTRL_7_E2E_HBM_WR_SIZE, 297 >> 3);
2319e65e175bSOded Gabbay 	WREG32(mmSIF_RTR_CTRL_7_E2E_HBM_RD_SIZE, 908 >> 3);
2320e65e175bSOded Gabbay 	WREG32(mmSIF_RTR_CTRL_7_E2E_PCI_WR_SIZE, 19);
2321e65e175bSOded Gabbay 	WREG32(mmSIF_RTR_CTRL_7_E2E_PCI_RD_SIZE, 19);
2322e65e175bSOded Gabbay 
2323e65e175bSOded Gabbay 	WREG32(mmNIF_RTR_CTRL_0_E2E_HBM_WR_SIZE, 318 >> 3);
2324e65e175bSOded Gabbay 	WREG32(mmNIF_RTR_CTRL_0_E2E_HBM_RD_SIZE, 956 >> 3);
2325e65e175bSOded Gabbay 	WREG32(mmNIF_RTR_CTRL_0_E2E_PCI_WR_SIZE, 79);
2326e65e175bSOded Gabbay 	WREG32(mmNIF_RTR_CTRL_0_E2E_PCI_RD_SIZE, 163);
2327e65e175bSOded Gabbay 
2328e65e175bSOded Gabbay 	WREG32(mmNIF_RTR_CTRL_1_E2E_HBM_WR_SIZE, 275 >> 3);
2329e65e175bSOded Gabbay 	WREG32(mmNIF_RTR_CTRL_1_E2E_HBM_RD_SIZE, 614 >> 3);
2330e65e175bSOded Gabbay 	WREG32(mmNIF_RTR_CTRL_1_E2E_PCI_WR_SIZE, 1);
2331e65e175bSOded Gabbay 	WREG32(mmNIF_RTR_CTRL_1_E2E_PCI_RD_SIZE, 39);
2332e65e175bSOded Gabbay 
2333e65e175bSOded Gabbay 	WREG32(mmNIF_RTR_CTRL_2_E2E_HBM_WR_SIZE, 1);
2334e65e175bSOded Gabbay 	WREG32(mmNIF_RTR_CTRL_2_E2E_HBM_RD_SIZE, 1);
2335e65e175bSOded Gabbay 	WREG32(mmNIF_RTR_CTRL_2_E2E_PCI_WR_SIZE, 1);
2336e65e175bSOded Gabbay 	WREG32(mmNIF_RTR_CTRL_2_E2E_PCI_RD_SIZE, 32);
2337e65e175bSOded Gabbay 
2338e65e175bSOded Gabbay 	WREG32(mmNIF_RTR_CTRL_3_E2E_HBM_WR_SIZE, 176 >> 3);
2339e65e175bSOded Gabbay 	WREG32(mmNIF_RTR_CTRL_3_E2E_HBM_RD_SIZE, 32 >> 3);
2340e65e175bSOded Gabbay 	WREG32(mmNIF_RTR_CTRL_3_E2E_PCI_WR_SIZE, 19);
2341e65e175bSOded Gabbay 	WREG32(mmNIF_RTR_CTRL_3_E2E_PCI_RD_SIZE, 32);
2342e65e175bSOded Gabbay 
2343e65e175bSOded Gabbay 	WREG32(mmNIF_RTR_CTRL_4_E2E_HBM_WR_SIZE, 176 >> 3);
2344e65e175bSOded Gabbay 	WREG32(mmNIF_RTR_CTRL_4_E2E_HBM_RD_SIZE, 32 >> 3);
2345e65e175bSOded Gabbay 	WREG32(mmNIF_RTR_CTRL_4_E2E_PCI_WR_SIZE, 19);
2346e65e175bSOded Gabbay 	WREG32(mmNIF_RTR_CTRL_4_E2E_PCI_RD_SIZE, 32);
2347e65e175bSOded Gabbay 
2348e65e175bSOded Gabbay 	WREG32(mmNIF_RTR_CTRL_5_E2E_HBM_WR_SIZE, 1);
2349e65e175bSOded Gabbay 	WREG32(mmNIF_RTR_CTRL_5_E2E_HBM_RD_SIZE, 1);
2350e65e175bSOded Gabbay 	WREG32(mmNIF_RTR_CTRL_5_E2E_PCI_WR_SIZE, 1);
2351e65e175bSOded Gabbay 	WREG32(mmNIF_RTR_CTRL_5_E2E_PCI_RD_SIZE, 32);
2352e65e175bSOded Gabbay 
2353e65e175bSOded Gabbay 	WREG32(mmNIF_RTR_CTRL_6_E2E_HBM_WR_SIZE, 275 >> 3);
2354e65e175bSOded Gabbay 	WREG32(mmNIF_RTR_CTRL_6_E2E_HBM_RD_SIZE, 614 >> 3);
2355e65e175bSOded Gabbay 	WREG32(mmNIF_RTR_CTRL_6_E2E_PCI_WR_SIZE, 1);
2356e65e175bSOded Gabbay 	WREG32(mmNIF_RTR_CTRL_6_E2E_PCI_RD_SIZE, 39);
2357e65e175bSOded Gabbay 
2358e65e175bSOded Gabbay 	WREG32(mmNIF_RTR_CTRL_7_E2E_HBM_WR_SIZE, 318 >> 3);
2359e65e175bSOded Gabbay 	WREG32(mmNIF_RTR_CTRL_7_E2E_HBM_RD_SIZE, 956 >> 3);
2360e65e175bSOded Gabbay 	WREG32(mmNIF_RTR_CTRL_7_E2E_PCI_WR_SIZE, 79);
2361e65e175bSOded Gabbay 	WREG32(mmNIF_RTR_CTRL_7_E2E_PCI_RD_SIZE, 79);
2362e65e175bSOded Gabbay 
2363e65e175bSOded Gabbay 	WREG32(mmDMA_IF_E_N_DOWN_CH0_E2E_HBM_WR_SIZE, 344 >> 3);
2364e65e175bSOded Gabbay 	WREG32(mmDMA_IF_E_N_DOWN_CH0_E2E_HBM_RD_SIZE, 1000 >> 3);
2365e65e175bSOded Gabbay 	WREG32(mmDMA_IF_E_N_DOWN_CH0_E2E_PCI_WR_SIZE, 162);
2366e65e175bSOded Gabbay 	WREG32(mmDMA_IF_E_N_DOWN_CH0_E2E_PCI_RD_SIZE, 338);
2367e65e175bSOded Gabbay 
2368e65e175bSOded Gabbay 	WREG32(mmDMA_IF_E_N_DOWN_CH1_E2E_HBM_WR_SIZE, 344 >> 3);
2369e65e175bSOded Gabbay 	WREG32(mmDMA_IF_E_N_DOWN_CH1_E2E_HBM_RD_SIZE, 1000 >> 3);
2370e65e175bSOded Gabbay 	WREG32(mmDMA_IF_E_N_DOWN_CH1_E2E_PCI_WR_SIZE, 162);
2371e65e175bSOded Gabbay 	WREG32(mmDMA_IF_E_N_DOWN_CH1_E2E_PCI_RD_SIZE, 338);
2372e65e175bSOded Gabbay 
2373e65e175bSOded Gabbay 	WREG32(mmDMA_IF_E_S_DOWN_CH0_E2E_HBM_WR_SIZE, 344 >> 3);
2374e65e175bSOded Gabbay 	WREG32(mmDMA_IF_E_S_DOWN_CH0_E2E_HBM_RD_SIZE, 1000 >> 3);
2375e65e175bSOded Gabbay 	WREG32(mmDMA_IF_E_S_DOWN_CH0_E2E_PCI_WR_SIZE, 162);
2376e65e175bSOded Gabbay 	WREG32(mmDMA_IF_E_S_DOWN_CH0_E2E_PCI_RD_SIZE, 338);
2377e65e175bSOded Gabbay 
2378e65e175bSOded Gabbay 	WREG32(mmDMA_IF_E_S_DOWN_CH1_E2E_HBM_WR_SIZE, 344 >> 3);
2379e65e175bSOded Gabbay 	WREG32(mmDMA_IF_E_S_DOWN_CH1_E2E_HBM_RD_SIZE, 1000 >> 3);
2380e65e175bSOded Gabbay 	WREG32(mmDMA_IF_E_S_DOWN_CH1_E2E_PCI_WR_SIZE, 162);
2381e65e175bSOded Gabbay 	WREG32(mmDMA_IF_E_S_DOWN_CH1_E2E_PCI_RD_SIZE, 338);
2382e65e175bSOded Gabbay 
2383e65e175bSOded Gabbay 	WREG32(mmDMA_IF_W_N_DOWN_CH0_E2E_HBM_WR_SIZE, 344 >> 3);
2384e65e175bSOded Gabbay 	WREG32(mmDMA_IF_W_N_DOWN_CH0_E2E_HBM_RD_SIZE, 1000 >> 3);
2385e65e175bSOded Gabbay 	WREG32(mmDMA_IF_W_N_DOWN_CH0_E2E_PCI_WR_SIZE, 162);
2386e65e175bSOded Gabbay 	WREG32(mmDMA_IF_W_N_DOWN_CH0_E2E_PCI_RD_SIZE, 338);
2387e65e175bSOded Gabbay 
2388e65e175bSOded Gabbay 	WREG32(mmDMA_IF_W_N_DOWN_CH1_E2E_HBM_WR_SIZE, 344 >> 3);
2389e65e175bSOded Gabbay 	WREG32(mmDMA_IF_W_N_DOWN_CH1_E2E_HBM_RD_SIZE, 1000 >> 3);
2390e65e175bSOded Gabbay 	WREG32(mmDMA_IF_W_N_DOWN_CH1_E2E_PCI_WR_SIZE, 162);
2391e65e175bSOded Gabbay 	WREG32(mmDMA_IF_W_N_DOWN_CH1_E2E_PCI_RD_SIZE, 338);
2392e65e175bSOded Gabbay 
2393e65e175bSOded Gabbay 	WREG32(mmDMA_IF_W_S_DOWN_CH0_E2E_HBM_WR_SIZE, 344 >> 3);
2394e65e175bSOded Gabbay 	WREG32(mmDMA_IF_W_S_DOWN_CH0_E2E_HBM_RD_SIZE, 1000 >> 3);
2395e65e175bSOded Gabbay 	WREG32(mmDMA_IF_W_S_DOWN_CH0_E2E_PCI_WR_SIZE, 162);
2396e65e175bSOded Gabbay 	WREG32(mmDMA_IF_W_S_DOWN_CH0_E2E_PCI_RD_SIZE, 338);
2397e65e175bSOded Gabbay 
2398e65e175bSOded Gabbay 	WREG32(mmDMA_IF_W_S_DOWN_CH1_E2E_HBM_WR_SIZE, 344 >> 3);
2399e65e175bSOded Gabbay 	WREG32(mmDMA_IF_W_S_DOWN_CH1_E2E_HBM_RD_SIZE, 1000 >> 3);
2400e65e175bSOded Gabbay 	WREG32(mmDMA_IF_W_S_DOWN_CH1_E2E_PCI_WR_SIZE, 162);
2401e65e175bSOded Gabbay 	WREG32(mmDMA_IF_W_S_DOWN_CH1_E2E_PCI_RD_SIZE, 338);
2402e65e175bSOded Gabbay 
2403e65e175bSOded Gabbay 	WREG32(mmSIF_RTR_CTRL_0_E2E_HBM_EN,
2404e65e175bSOded Gabbay 			1 << IF_RTR_CTRL_E2E_HBM_EN_VAL_SHIFT);
2405e65e175bSOded Gabbay 	WREG32(mmSIF_RTR_CTRL_0_E2E_PCI_EN,
2406e65e175bSOded Gabbay 			1 << IF_RTR_CTRL_E2E_PCI_EN_VAL_SHIFT);
2407e65e175bSOded Gabbay 
2408e65e175bSOded Gabbay 	WREG32(mmSIF_RTR_CTRL_1_E2E_HBM_EN,
2409e65e175bSOded Gabbay 			1 << IF_RTR_CTRL_E2E_HBM_EN_VAL_SHIFT);
2410e65e175bSOded Gabbay 	WREG32(mmSIF_RTR_CTRL_1_E2E_PCI_EN,
2411e65e175bSOded Gabbay 			1 << IF_RTR_CTRL_E2E_PCI_EN_VAL_SHIFT);
2412e65e175bSOded Gabbay 
2413e65e175bSOded Gabbay 	WREG32(mmSIF_RTR_CTRL_2_E2E_HBM_EN,
2414e65e175bSOded Gabbay 			1 << IF_RTR_CTRL_E2E_HBM_EN_VAL_SHIFT);
2415e65e175bSOded Gabbay 	WREG32(mmSIF_RTR_CTRL_2_E2E_PCI_EN,
2416e65e175bSOded Gabbay 			1 << IF_RTR_CTRL_E2E_PCI_EN_VAL_SHIFT);
2417e65e175bSOded Gabbay 
2418e65e175bSOded Gabbay 	WREG32(mmSIF_RTR_CTRL_3_E2E_HBM_EN,
2419e65e175bSOded Gabbay 			1 << IF_RTR_CTRL_E2E_HBM_EN_VAL_SHIFT);
2420e65e175bSOded Gabbay 	WREG32(mmSIF_RTR_CTRL_3_E2E_PCI_EN,
2421e65e175bSOded Gabbay 			1 << IF_RTR_CTRL_E2E_PCI_EN_VAL_SHIFT);
2422e65e175bSOded Gabbay 
2423e65e175bSOded Gabbay 	WREG32(mmSIF_RTR_CTRL_4_E2E_HBM_EN,
2424e65e175bSOded Gabbay 			1 << IF_RTR_CTRL_E2E_HBM_EN_VAL_SHIFT);
2425e65e175bSOded Gabbay 	WREG32(mmSIF_RTR_CTRL_4_E2E_PCI_EN,
2426e65e175bSOded Gabbay 			1 << IF_RTR_CTRL_E2E_PCI_EN_VAL_SHIFT);
2427e65e175bSOded Gabbay 
2428e65e175bSOded Gabbay 	WREG32(mmSIF_RTR_CTRL_5_E2E_HBM_EN,
2429e65e175bSOded Gabbay 			1 << IF_RTR_CTRL_E2E_HBM_EN_VAL_SHIFT);
2430e65e175bSOded Gabbay 	WREG32(mmSIF_RTR_CTRL_5_E2E_PCI_EN,
2431e65e175bSOded Gabbay 			1 << IF_RTR_CTRL_E2E_PCI_EN_VAL_SHIFT);
2432e65e175bSOded Gabbay 
2433e65e175bSOded Gabbay 	WREG32(mmSIF_RTR_CTRL_6_E2E_HBM_EN,
2434e65e175bSOded Gabbay 			1 << IF_RTR_CTRL_E2E_HBM_EN_VAL_SHIFT);
2435e65e175bSOded Gabbay 	WREG32(mmSIF_RTR_CTRL_6_E2E_PCI_EN,
2436e65e175bSOded Gabbay 			1 << IF_RTR_CTRL_E2E_PCI_EN_VAL_SHIFT);
2437e65e175bSOded Gabbay 
2438e65e175bSOded Gabbay 	WREG32(mmSIF_RTR_CTRL_7_E2E_HBM_EN,
2439e65e175bSOded Gabbay 			1 << IF_RTR_CTRL_E2E_HBM_EN_VAL_SHIFT);
2440e65e175bSOded Gabbay 	WREG32(mmSIF_RTR_CTRL_7_E2E_PCI_EN,
2441e65e175bSOded Gabbay 			1 << IF_RTR_CTRL_E2E_PCI_EN_VAL_SHIFT);
2442e65e175bSOded Gabbay 
2443e65e175bSOded Gabbay 	WREG32(mmNIF_RTR_CTRL_0_E2E_HBM_EN,
2444e65e175bSOded Gabbay 			1 << IF_RTR_CTRL_E2E_HBM_EN_VAL_SHIFT);
2445e65e175bSOded Gabbay 	WREG32(mmNIF_RTR_CTRL_0_E2E_PCI_EN,
2446e65e175bSOded Gabbay 			1 << IF_RTR_CTRL_E2E_PCI_EN_VAL_SHIFT);
2447e65e175bSOded Gabbay 
2448e65e175bSOded Gabbay 	WREG32(mmNIF_RTR_CTRL_1_E2E_HBM_EN,
2449e65e175bSOded Gabbay 			1 << IF_RTR_CTRL_E2E_HBM_EN_VAL_SHIFT);
2450e65e175bSOded Gabbay 	WREG32(mmNIF_RTR_CTRL_1_E2E_PCI_EN,
2451e65e175bSOded Gabbay 			1 << IF_RTR_CTRL_E2E_PCI_EN_VAL_SHIFT);
2452e65e175bSOded Gabbay 
2453e65e175bSOded Gabbay 	WREG32(mmNIF_RTR_CTRL_2_E2E_HBM_EN,
2454e65e175bSOded Gabbay 			1 << IF_RTR_CTRL_E2E_HBM_EN_VAL_SHIFT);
2455e65e175bSOded Gabbay 	WREG32(mmNIF_RTR_CTRL_2_E2E_PCI_EN,
2456e65e175bSOded Gabbay 			1 << IF_RTR_CTRL_E2E_PCI_EN_VAL_SHIFT);
2457e65e175bSOded Gabbay 
2458e65e175bSOded Gabbay 	WREG32(mmNIF_RTR_CTRL_3_E2E_HBM_EN,
2459e65e175bSOded Gabbay 			1 << IF_RTR_CTRL_E2E_HBM_EN_VAL_SHIFT);
2460e65e175bSOded Gabbay 	WREG32(mmNIF_RTR_CTRL_3_E2E_PCI_EN,
2461e65e175bSOded Gabbay 			1 << IF_RTR_CTRL_E2E_PCI_EN_VAL_SHIFT);
2462e65e175bSOded Gabbay 
2463e65e175bSOded Gabbay 	WREG32(mmNIF_RTR_CTRL_4_E2E_HBM_EN,
2464e65e175bSOded Gabbay 			1 << IF_RTR_CTRL_E2E_HBM_EN_VAL_SHIFT);
2465e65e175bSOded Gabbay 	WREG32(mmNIF_RTR_CTRL_4_E2E_PCI_EN,
2466e65e175bSOded Gabbay 			1 << IF_RTR_CTRL_E2E_PCI_EN_VAL_SHIFT);
2467e65e175bSOded Gabbay 
2468e65e175bSOded Gabbay 	WREG32(mmNIF_RTR_CTRL_5_E2E_HBM_EN,
2469e65e175bSOded Gabbay 			1 << IF_RTR_CTRL_E2E_HBM_EN_VAL_SHIFT);
2470e65e175bSOded Gabbay 	WREG32(mmNIF_RTR_CTRL_5_E2E_PCI_EN,
2471e65e175bSOded Gabbay 			1 << IF_RTR_CTRL_E2E_PCI_EN_VAL_SHIFT);
2472e65e175bSOded Gabbay 
2473e65e175bSOded Gabbay 	WREG32(mmNIF_RTR_CTRL_6_E2E_HBM_EN,
2474e65e175bSOded Gabbay 			1 << IF_RTR_CTRL_E2E_HBM_EN_VAL_SHIFT);
2475e65e175bSOded Gabbay 	WREG32(mmNIF_RTR_CTRL_6_E2E_PCI_EN,
2476e65e175bSOded Gabbay 			1 << IF_RTR_CTRL_E2E_PCI_EN_VAL_SHIFT);
2477e65e175bSOded Gabbay 
2478e65e175bSOded Gabbay 	WREG32(mmNIF_RTR_CTRL_7_E2E_HBM_EN,
2479e65e175bSOded Gabbay 			1 << IF_RTR_CTRL_E2E_HBM_EN_VAL_SHIFT);
2480e65e175bSOded Gabbay 	WREG32(mmNIF_RTR_CTRL_7_E2E_PCI_EN,
2481e65e175bSOded Gabbay 			1 << IF_RTR_CTRL_E2E_PCI_EN_VAL_SHIFT);
2482e65e175bSOded Gabbay 
2483e65e175bSOded Gabbay 	WREG32(mmDMA_IF_E_N_DOWN_CH0_E2E_HBM_EN,
2484e65e175bSOded Gabbay 			1 << DMA_IF_DOWN_CHX_E2E_HBM_EN_VAL_SHIFT);
2485e65e175bSOded Gabbay 	WREG32(mmDMA_IF_E_N_DOWN_CH0_E2E_PCI_EN,
2486e65e175bSOded Gabbay 			1 << DMA_IF_DOWN_CHX_E2E_PCI_EN_VAL_SHIFT);
2487e65e175bSOded Gabbay 
2488e65e175bSOded Gabbay 	WREG32(mmDMA_IF_E_N_DOWN_CH1_E2E_HBM_EN,
2489e65e175bSOded Gabbay 			1 << DMA_IF_DOWN_CHX_E2E_HBM_EN_VAL_SHIFT);
2490e65e175bSOded Gabbay 	WREG32(mmDMA_IF_E_N_DOWN_CH1_E2E_PCI_EN,
2491e65e175bSOded Gabbay 			1 << DMA_IF_DOWN_CHX_E2E_PCI_EN_VAL_SHIFT);
2492e65e175bSOded Gabbay 
2493e65e175bSOded Gabbay 	WREG32(mmDMA_IF_E_S_DOWN_CH0_E2E_HBM_EN,
2494e65e175bSOded Gabbay 			1 << DMA_IF_DOWN_CHX_E2E_HBM_EN_VAL_SHIFT);
2495e65e175bSOded Gabbay 	WREG32(mmDMA_IF_E_S_DOWN_CH0_E2E_PCI_EN,
2496e65e175bSOded Gabbay 			1 << DMA_IF_DOWN_CHX_E2E_PCI_EN_VAL_SHIFT);
2497e65e175bSOded Gabbay 
2498e65e175bSOded Gabbay 	WREG32(mmDMA_IF_E_S_DOWN_CH1_E2E_HBM_EN,
2499e65e175bSOded Gabbay 			1 << DMA_IF_DOWN_CHX_E2E_HBM_EN_VAL_SHIFT);
2500e65e175bSOded Gabbay 	WREG32(mmDMA_IF_E_S_DOWN_CH1_E2E_PCI_EN,
2501e65e175bSOded Gabbay 			1 << DMA_IF_DOWN_CHX_E2E_PCI_EN_VAL_SHIFT);
2502e65e175bSOded Gabbay 
2503e65e175bSOded Gabbay 	WREG32(mmDMA_IF_W_N_DOWN_CH0_E2E_HBM_EN,
2504e65e175bSOded Gabbay 			1 << DMA_IF_DOWN_CHX_E2E_HBM_EN_VAL_SHIFT);
2505e65e175bSOded Gabbay 	WREG32(mmDMA_IF_W_N_DOWN_CH0_E2E_PCI_EN,
2506e65e175bSOded Gabbay 			1 << DMA_IF_DOWN_CHX_E2E_PCI_EN_VAL_SHIFT);
2507e65e175bSOded Gabbay 
2508e65e175bSOded Gabbay 	WREG32(mmDMA_IF_W_N_DOWN_CH1_E2E_HBM_EN,
2509e65e175bSOded Gabbay 			1 << DMA_IF_DOWN_CHX_E2E_HBM_EN_VAL_SHIFT);
2510e65e175bSOded Gabbay 	WREG32(mmDMA_IF_W_N_DOWN_CH1_E2E_PCI_EN,
2511e65e175bSOded Gabbay 			1 << DMA_IF_DOWN_CHX_E2E_PCI_EN_VAL_SHIFT);
2512e65e175bSOded Gabbay 
2513e65e175bSOded Gabbay 	WREG32(mmDMA_IF_W_S_DOWN_CH0_E2E_HBM_EN,
2514e65e175bSOded Gabbay 			1 << DMA_IF_DOWN_CHX_E2E_HBM_EN_VAL_SHIFT);
2515e65e175bSOded Gabbay 	WREG32(mmDMA_IF_W_S_DOWN_CH0_E2E_PCI_EN,
2516e65e175bSOded Gabbay 			1 << DMA_IF_DOWN_CHX_E2E_PCI_EN_VAL_SHIFT);
2517e65e175bSOded Gabbay 
2518e65e175bSOded Gabbay 	WREG32(mmDMA_IF_W_S_DOWN_CH1_E2E_HBM_EN,
2519e65e175bSOded Gabbay 			1 << DMA_IF_DOWN_CHX_E2E_HBM_EN_VAL_SHIFT);
2520e65e175bSOded Gabbay 	WREG32(mmDMA_IF_W_S_DOWN_CH1_E2E_PCI_EN,
2521e65e175bSOded Gabbay 			1 << DMA_IF_DOWN_CHX_E2E_PCI_EN_VAL_SHIFT);
2522e65e175bSOded Gabbay }
2523e65e175bSOded Gabbay 
2524e65e175bSOded Gabbay static void gaudi_init_hbm_cred(struct hl_device *hdev)
2525e65e175bSOded Gabbay {
2526e65e175bSOded Gabbay 	u32 hbm0_wr, hbm1_wr, hbm0_rd, hbm1_rd;
2527e65e175bSOded Gabbay 
2528e65e175bSOded Gabbay 	if (hdev->asic_prop.fw_security_enabled)
2529e65e175bSOded Gabbay 		return;
2530e65e175bSOded Gabbay 
2531e65e175bSOded Gabbay 	if (hdev->asic_prop.fw_bootfit_cpu_boot_dev_sts0 &
2532e65e175bSOded Gabbay 						CPU_BOOT_DEV_STS0_HBM_CRED_EN)
2533e65e175bSOded Gabbay 		return;
2534e65e175bSOded Gabbay 
2535e65e175bSOded Gabbay 	hbm0_wr = 0x33333333;
2536e65e175bSOded Gabbay 	hbm0_rd = 0x77777777;
2537e65e175bSOded Gabbay 	hbm1_wr = 0x55555555;
2538e65e175bSOded Gabbay 	hbm1_rd = 0xDDDDDDDD;
2539e65e175bSOded Gabbay 
2540e65e175bSOded Gabbay 	WREG32(mmDMA_IF_E_N_HBM0_WR_CRED_CNT, hbm0_wr);
2541e65e175bSOded Gabbay 	WREG32(mmDMA_IF_E_N_HBM1_WR_CRED_CNT, hbm1_wr);
2542e65e175bSOded Gabbay 	WREG32(mmDMA_IF_E_N_HBM0_RD_CRED_CNT, hbm0_rd);
2543e65e175bSOded Gabbay 	WREG32(mmDMA_IF_E_N_HBM1_RD_CRED_CNT, hbm1_rd);
2544e65e175bSOded Gabbay 
2545e65e175bSOded Gabbay 	WREG32(mmDMA_IF_E_S_HBM0_WR_CRED_CNT, hbm0_wr);
2546e65e175bSOded Gabbay 	WREG32(mmDMA_IF_E_S_HBM1_WR_CRED_CNT, hbm1_wr);
2547e65e175bSOded Gabbay 	WREG32(mmDMA_IF_E_S_HBM0_RD_CRED_CNT, hbm0_rd);
2548e65e175bSOded Gabbay 	WREG32(mmDMA_IF_E_S_HBM1_RD_CRED_CNT, hbm1_rd);
2549e65e175bSOded Gabbay 
2550e65e175bSOded Gabbay 	WREG32(mmDMA_IF_W_N_HBM0_WR_CRED_CNT, hbm0_wr);
2551e65e175bSOded Gabbay 	WREG32(mmDMA_IF_W_N_HBM1_WR_CRED_CNT, hbm1_wr);
2552e65e175bSOded Gabbay 	WREG32(mmDMA_IF_W_N_HBM0_RD_CRED_CNT, hbm0_rd);
2553e65e175bSOded Gabbay 	WREG32(mmDMA_IF_W_N_HBM1_RD_CRED_CNT, hbm1_rd);
2554e65e175bSOded Gabbay 
2555e65e175bSOded Gabbay 	WREG32(mmDMA_IF_W_S_HBM0_WR_CRED_CNT, hbm0_wr);
2556e65e175bSOded Gabbay 	WREG32(mmDMA_IF_W_S_HBM1_WR_CRED_CNT, hbm1_wr);
2557e65e175bSOded Gabbay 	WREG32(mmDMA_IF_W_S_HBM0_RD_CRED_CNT, hbm0_rd);
2558e65e175bSOded Gabbay 	WREG32(mmDMA_IF_W_S_HBM1_RD_CRED_CNT, hbm1_rd);
2559e65e175bSOded Gabbay 
2560e65e175bSOded Gabbay 	WREG32(mmDMA_IF_E_N_HBM_CRED_EN_0,
2561e65e175bSOded Gabbay 			(1 << DMA_IF_HBM_CRED_EN_READ_CREDIT_EN_SHIFT) |
2562e65e175bSOded Gabbay 			(1 << DMA_IF_HBM_CRED_EN_WRITE_CREDIT_EN_SHIFT));
2563e65e175bSOded Gabbay 	WREG32(mmDMA_IF_E_S_HBM_CRED_EN_0,
2564e65e175bSOded Gabbay 			(1 << DMA_IF_HBM_CRED_EN_READ_CREDIT_EN_SHIFT) |
2565e65e175bSOded Gabbay 			(1 << DMA_IF_HBM_CRED_EN_WRITE_CREDIT_EN_SHIFT));
2566e65e175bSOded Gabbay 	WREG32(mmDMA_IF_W_N_HBM_CRED_EN_0,
2567e65e175bSOded Gabbay 			(1 << DMA_IF_HBM_CRED_EN_READ_CREDIT_EN_SHIFT) |
2568e65e175bSOded Gabbay 			(1 << DMA_IF_HBM_CRED_EN_WRITE_CREDIT_EN_SHIFT));
2569e65e175bSOded Gabbay 	WREG32(mmDMA_IF_W_S_HBM_CRED_EN_0,
2570e65e175bSOded Gabbay 			(1 << DMA_IF_HBM_CRED_EN_READ_CREDIT_EN_SHIFT) |
2571e65e175bSOded Gabbay 			(1 << DMA_IF_HBM_CRED_EN_WRITE_CREDIT_EN_SHIFT));
2572e65e175bSOded Gabbay 
2573e65e175bSOded Gabbay 	WREG32(mmDMA_IF_E_N_HBM_CRED_EN_1,
2574e65e175bSOded Gabbay 			(1 << DMA_IF_HBM_CRED_EN_READ_CREDIT_EN_SHIFT) |
2575e65e175bSOded Gabbay 			(1 << DMA_IF_HBM_CRED_EN_WRITE_CREDIT_EN_SHIFT));
2576e65e175bSOded Gabbay 	WREG32(mmDMA_IF_E_S_HBM_CRED_EN_1,
2577e65e175bSOded Gabbay 			(1 << DMA_IF_HBM_CRED_EN_READ_CREDIT_EN_SHIFT) |
2578e65e175bSOded Gabbay 			(1 << DMA_IF_HBM_CRED_EN_WRITE_CREDIT_EN_SHIFT));
2579e65e175bSOded Gabbay 	WREG32(mmDMA_IF_W_N_HBM_CRED_EN_1,
2580e65e175bSOded Gabbay 			(1 << DMA_IF_HBM_CRED_EN_READ_CREDIT_EN_SHIFT) |
2581e65e175bSOded Gabbay 			(1 << DMA_IF_HBM_CRED_EN_WRITE_CREDIT_EN_SHIFT));
2582e65e175bSOded Gabbay 	WREG32(mmDMA_IF_W_S_HBM_CRED_EN_1,
2583e65e175bSOded Gabbay 			(1 << DMA_IF_HBM_CRED_EN_READ_CREDIT_EN_SHIFT) |
2584e65e175bSOded Gabbay 			(1 << DMA_IF_HBM_CRED_EN_WRITE_CREDIT_EN_SHIFT));
2585e65e175bSOded Gabbay }
2586e65e175bSOded Gabbay 
2587e65e175bSOded Gabbay static void gaudi_init_golden_registers(struct hl_device *hdev)
2588e65e175bSOded Gabbay {
2589e65e175bSOded Gabbay 	u32 tpc_offset;
2590e65e175bSOded Gabbay 	int tpc_id, i;
2591e65e175bSOded Gabbay 
2592e65e175bSOded Gabbay 	gaudi_init_e2e(hdev);
2593e65e175bSOded Gabbay 	gaudi_init_hbm_cred(hdev);
2594e65e175bSOded Gabbay 
2595e65e175bSOded Gabbay 	for (tpc_id = 0, tpc_offset = 0;
2596e65e175bSOded Gabbay 				tpc_id < TPC_NUMBER_OF_ENGINES;
2597e65e175bSOded Gabbay 				tpc_id++, tpc_offset += TPC_CFG_OFFSET) {
2598e65e175bSOded Gabbay 		/* Mask all arithmetic interrupts from TPC */
2599e65e175bSOded Gabbay 		WREG32(mmTPC0_CFG_TPC_INTR_MASK + tpc_offset, 0x8FFE);
2600e65e175bSOded Gabbay 		/* Set 16 cache lines */
2601e65e175bSOded Gabbay 		WREG32_FIELD(TPC0_CFG_MSS_CONFIG, tpc_offset,
2602e65e175bSOded Gabbay 				ICACHE_FETCH_LINE_NUM, 2);
2603e65e175bSOded Gabbay 	}
2604e65e175bSOded Gabbay 
2605e65e175bSOded Gabbay 	/* Make sure 1st 128 bytes in SRAM are 0 for Tensor DMA */
2606e65e175bSOded Gabbay 	for (i = 0 ; i < 128 ; i += 8)
2607e65e175bSOded Gabbay 		writeq(0, hdev->pcie_bar[SRAM_BAR_ID] + i);
2608e65e175bSOded Gabbay 
2609e65e175bSOded Gabbay 	WREG32(mmMME0_CTRL_EUS_ROLLUP_CNT_ADD, 3);
2610e65e175bSOded Gabbay 	WREG32(mmMME1_CTRL_EUS_ROLLUP_CNT_ADD, 3);
2611e65e175bSOded Gabbay 	WREG32(mmMME2_CTRL_EUS_ROLLUP_CNT_ADD, 3);
2612e65e175bSOded Gabbay 	WREG32(mmMME3_CTRL_EUS_ROLLUP_CNT_ADD, 3);
2613e65e175bSOded Gabbay }
2614e65e175bSOded Gabbay 
2615e65e175bSOded Gabbay static void gaudi_init_pci_dma_qman(struct hl_device *hdev, int dma_id,
2616e65e175bSOded Gabbay 					int qman_id, dma_addr_t qman_pq_addr)
2617e65e175bSOded Gabbay {
2618e65e175bSOded Gabbay 	struct cpu_dyn_regs *dyn_regs =
2619e65e175bSOded Gabbay 			&hdev->fw_loader.dynamic_loader.comm_desc.cpu_dyn_regs;
2620e65e175bSOded Gabbay 	u32 mtr_base_en_lo, mtr_base_en_hi, mtr_base_ws_lo, mtr_base_ws_hi;
2621e65e175bSOded Gabbay 	u32 so_base_en_lo, so_base_en_hi, so_base_ws_lo, so_base_ws_hi;
2622e65e175bSOded Gabbay 	u32 q_off, dma_qm_offset;
2623e65e175bSOded Gabbay 	u32 dma_qm_err_cfg, irq_handler_offset;
2624e65e175bSOded Gabbay 
2625e65e175bSOded Gabbay 	dma_qm_offset = dma_id * DMA_QMAN_OFFSET;
2626e65e175bSOded Gabbay 
2627e65e175bSOded Gabbay 	mtr_base_en_lo = lower_32_bits(CFG_BASE +
2628e65e175bSOded Gabbay 				mmSYNC_MNGR_E_N_SYNC_MNGR_OBJS_MON_PAY_ADDRL_0);
2629e65e175bSOded Gabbay 	mtr_base_en_hi = upper_32_bits(CFG_BASE +
2630e65e175bSOded Gabbay 				mmSYNC_MNGR_E_N_SYNC_MNGR_OBJS_MON_PAY_ADDRL_0);
2631e65e175bSOded Gabbay 	so_base_en_lo = lower_32_bits(CFG_BASE +
2632e65e175bSOded Gabbay 				mmSYNC_MNGR_E_N_SYNC_MNGR_OBJS_SOB_OBJ_0);
2633e65e175bSOded Gabbay 	so_base_en_hi = upper_32_bits(CFG_BASE +
2634e65e175bSOded Gabbay 				mmSYNC_MNGR_E_N_SYNC_MNGR_OBJS_SOB_OBJ_0);
2635e65e175bSOded Gabbay 	mtr_base_ws_lo = lower_32_bits(CFG_BASE +
2636e65e175bSOded Gabbay 				mmSYNC_MNGR_W_S_SYNC_MNGR_OBJS_MON_PAY_ADDRL_0);
2637e65e175bSOded Gabbay 	mtr_base_ws_hi = upper_32_bits(CFG_BASE +
2638e65e175bSOded Gabbay 				mmSYNC_MNGR_W_S_SYNC_MNGR_OBJS_MON_PAY_ADDRL_0);
2639e65e175bSOded Gabbay 	so_base_ws_lo = lower_32_bits(CFG_BASE +
2640e65e175bSOded Gabbay 				mmSYNC_MNGR_W_S_SYNC_MNGR_OBJS_SOB_OBJ_0);
2641e65e175bSOded Gabbay 	so_base_ws_hi = upper_32_bits(CFG_BASE +
2642e65e175bSOded Gabbay 				mmSYNC_MNGR_W_S_SYNC_MNGR_OBJS_SOB_OBJ_0);
2643e65e175bSOded Gabbay 
2644e65e175bSOded Gabbay 	q_off = dma_qm_offset + qman_id * 4;
2645e65e175bSOded Gabbay 
2646e65e175bSOded Gabbay 	WREG32(mmDMA0_QM_PQ_BASE_LO_0 + q_off, lower_32_bits(qman_pq_addr));
2647e65e175bSOded Gabbay 	WREG32(mmDMA0_QM_PQ_BASE_HI_0 + q_off, upper_32_bits(qman_pq_addr));
2648e65e175bSOded Gabbay 
2649e65e175bSOded Gabbay 	WREG32(mmDMA0_QM_PQ_SIZE_0 + q_off, ilog2(HL_QUEUE_LENGTH));
2650e65e175bSOded Gabbay 	WREG32(mmDMA0_QM_PQ_PI_0 + q_off, 0);
2651e65e175bSOded Gabbay 	WREG32(mmDMA0_QM_PQ_CI_0 + q_off, 0);
2652e65e175bSOded Gabbay 
2653e65e175bSOded Gabbay 	WREG32(mmDMA0_QM_CP_LDMA_TSIZE_OFFSET_0 + q_off, QMAN_LDMA_SIZE_OFFSET);
2654e65e175bSOded Gabbay 	WREG32(mmDMA0_QM_CP_LDMA_SRC_BASE_LO_OFFSET_0 + q_off,
2655e65e175bSOded Gabbay 							QMAN_LDMA_SRC_OFFSET);
2656e65e175bSOded Gabbay 	WREG32(mmDMA0_QM_CP_LDMA_DST_BASE_LO_OFFSET_0 + q_off,
2657e65e175bSOded Gabbay 							QMAN_LDMA_DST_OFFSET);
2658e65e175bSOded Gabbay 
2659e65e175bSOded Gabbay 	WREG32(mmDMA0_QM_CP_MSG_BASE0_ADDR_LO_0 + q_off, mtr_base_en_lo);
2660e65e175bSOded Gabbay 	WREG32(mmDMA0_QM_CP_MSG_BASE0_ADDR_HI_0 + q_off, mtr_base_en_hi);
2661e65e175bSOded Gabbay 	WREG32(mmDMA0_QM_CP_MSG_BASE1_ADDR_LO_0 + q_off, so_base_en_lo);
2662e65e175bSOded Gabbay 	WREG32(mmDMA0_QM_CP_MSG_BASE1_ADDR_HI_0 + q_off, so_base_en_hi);
2663e65e175bSOded Gabbay 	WREG32(mmDMA0_QM_CP_MSG_BASE2_ADDR_LO_0 + q_off, mtr_base_ws_lo);
2664e65e175bSOded Gabbay 	WREG32(mmDMA0_QM_CP_MSG_BASE2_ADDR_HI_0 + q_off, mtr_base_ws_hi);
2665e65e175bSOded Gabbay 	WREG32(mmDMA0_QM_CP_MSG_BASE3_ADDR_LO_0 + q_off, so_base_ws_lo);
2666e65e175bSOded Gabbay 	WREG32(mmDMA0_QM_CP_MSG_BASE3_ADDR_HI_0 + q_off, so_base_ws_hi);
2667e65e175bSOded Gabbay 
2668e65e175bSOded Gabbay 	WREG32(mmDMA0_QM_CP_BARRIER_CFG_0 + q_off, 0x100);
2669e65e175bSOded Gabbay 
2670e65e175bSOded Gabbay 	/* The following configuration is needed only once per QMAN */
2671e65e175bSOded Gabbay 	if (qman_id == 0) {
2672e65e175bSOded Gabbay 		irq_handler_offset = hdev->asic_prop.gic_interrupts_enable ?
2673e65e175bSOded Gabbay 				mmGIC_DISTRIBUTOR__5_GICD_SETSPI_NSR :
2674e65e175bSOded Gabbay 				le32_to_cpu(dyn_regs->gic_dma_qm_irq_ctrl);
2675e65e175bSOded Gabbay 
2676e65e175bSOded Gabbay 		/* Configure RAZWI IRQ */
2677e65e175bSOded Gabbay 		dma_qm_err_cfg = PCI_DMA_QMAN_GLBL_ERR_CFG_MSG_EN_MASK;
2678e65e175bSOded Gabbay 		if (hdev->stop_on_err)
2679e65e175bSOded Gabbay 			dma_qm_err_cfg |=
2680e65e175bSOded Gabbay 				PCI_DMA_QMAN_GLBL_ERR_CFG_STOP_ON_ERR_EN_MASK;
2681e65e175bSOded Gabbay 
2682e65e175bSOded Gabbay 		WREG32(mmDMA0_QM_GLBL_ERR_CFG + dma_qm_offset, dma_qm_err_cfg);
2683e65e175bSOded Gabbay 
2684e65e175bSOded Gabbay 		WREG32(mmDMA0_QM_GLBL_ERR_ADDR_LO + dma_qm_offset,
2685e65e175bSOded Gabbay 			lower_32_bits(CFG_BASE + irq_handler_offset));
2686e65e175bSOded Gabbay 		WREG32(mmDMA0_QM_GLBL_ERR_ADDR_HI + dma_qm_offset,
2687e65e175bSOded Gabbay 			upper_32_bits(CFG_BASE + irq_handler_offset));
2688e65e175bSOded Gabbay 
2689e65e175bSOded Gabbay 		WREG32(mmDMA0_QM_GLBL_ERR_WDATA + dma_qm_offset,
2690e65e175bSOded Gabbay 			gaudi_irq_map_table[GAUDI_EVENT_DMA0_QM].cpu_id +
2691e65e175bSOded Gabbay 									dma_id);
2692e65e175bSOded Gabbay 
2693e65e175bSOded Gabbay 		WREG32(mmDMA0_QM_ARB_ERR_MSG_EN + dma_qm_offset,
2694e65e175bSOded Gabbay 				QM_ARB_ERR_MSG_EN_MASK);
2695e65e175bSOded Gabbay 
2696e65e175bSOded Gabbay 		/* Set timeout to maximum */
2697e65e175bSOded Gabbay 		WREG32(mmDMA0_QM_ARB_SLV_CHOISE_WDT + dma_qm_offset, GAUDI_ARB_WDT_TIMEOUT);
2698e65e175bSOded Gabbay 
2699e65e175bSOded Gabbay 		WREG32(mmDMA0_QM_GLBL_PROT + dma_qm_offset,
2700e65e175bSOded Gabbay 				QMAN_EXTERNAL_MAKE_TRUSTED);
2701e65e175bSOded Gabbay 
2702e65e175bSOded Gabbay 		WREG32(mmDMA0_QM_GLBL_CFG1 + dma_qm_offset, 0);
2703e65e175bSOded Gabbay 	}
2704e65e175bSOded Gabbay }
2705e65e175bSOded Gabbay 
2706e65e175bSOded Gabbay static void gaudi_init_dma_core(struct hl_device *hdev, int dma_id)
2707e65e175bSOded Gabbay {
2708e65e175bSOded Gabbay 	struct cpu_dyn_regs *dyn_regs =
2709e65e175bSOded Gabbay 			&hdev->fw_loader.dynamic_loader.comm_desc.cpu_dyn_regs;
2710e65e175bSOded Gabbay 	u32 dma_err_cfg = 1 << DMA0_CORE_ERR_CFG_ERR_MSG_EN_SHIFT;
2711e65e175bSOded Gabbay 	u32 dma_offset = dma_id * DMA_CORE_OFFSET;
2712e65e175bSOded Gabbay 	u32 irq_handler_offset;
2713e65e175bSOded Gabbay 
2714e65e175bSOded Gabbay 	/* Set to maximum possible according to physical size */
2715e65e175bSOded Gabbay 	WREG32(mmDMA0_CORE_RD_MAX_OUTSTAND + dma_offset, 0);
2716e65e175bSOded Gabbay 	WREG32(mmDMA0_CORE_RD_MAX_SIZE + dma_offset, 0);
2717e65e175bSOded Gabbay 
2718e65e175bSOded Gabbay 	/* WA for H/W bug H3-2116 */
2719e65e175bSOded Gabbay 	WREG32(mmDMA0_CORE_LBW_MAX_OUTSTAND + dma_offset, 15);
2720e65e175bSOded Gabbay 
2721e65e175bSOded Gabbay 	/* STOP_ON bit implies no completion to operation in case of RAZWI */
2722e65e175bSOded Gabbay 	if (hdev->stop_on_err)
2723e65e175bSOded Gabbay 		dma_err_cfg |= 1 << DMA0_CORE_ERR_CFG_STOP_ON_ERR_SHIFT;
2724e65e175bSOded Gabbay 
2725e65e175bSOded Gabbay 	WREG32(mmDMA0_CORE_ERR_CFG + dma_offset, dma_err_cfg);
2726e65e175bSOded Gabbay 
2727e65e175bSOded Gabbay 	irq_handler_offset = hdev->asic_prop.gic_interrupts_enable ?
2728e65e175bSOded Gabbay 			mmGIC_DISTRIBUTOR__5_GICD_SETSPI_NSR :
2729e65e175bSOded Gabbay 			le32_to_cpu(dyn_regs->gic_dma_core_irq_ctrl);
2730e65e175bSOded Gabbay 
2731e65e175bSOded Gabbay 	WREG32(mmDMA0_CORE_ERRMSG_ADDR_LO + dma_offset,
2732e65e175bSOded Gabbay 		lower_32_bits(CFG_BASE + irq_handler_offset));
2733e65e175bSOded Gabbay 	WREG32(mmDMA0_CORE_ERRMSG_ADDR_HI + dma_offset,
2734e65e175bSOded Gabbay 		upper_32_bits(CFG_BASE + irq_handler_offset));
2735e65e175bSOded Gabbay 
2736e65e175bSOded Gabbay 	WREG32(mmDMA0_CORE_ERRMSG_WDATA + dma_offset,
2737e65e175bSOded Gabbay 		gaudi_irq_map_table[GAUDI_EVENT_DMA0_CORE].cpu_id + dma_id);
2738e65e175bSOded Gabbay 	WREG32(mmDMA0_CORE_PROT + dma_offset,
2739e65e175bSOded Gabbay 			1 << DMA0_CORE_PROT_ERR_VAL_SHIFT);
2740e65e175bSOded Gabbay 	/* If the channel is secured, it should be in MMU bypass mode */
2741e65e175bSOded Gabbay 	WREG32(mmDMA0_CORE_SECURE_PROPS + dma_offset,
2742e65e175bSOded Gabbay 			1 << DMA0_CORE_SECURE_PROPS_MMBP_SHIFT);
2743e65e175bSOded Gabbay 	WREG32(mmDMA0_CORE_CFG_0 + dma_offset, 1 << DMA0_CORE_CFG_0_EN_SHIFT);
2744e65e175bSOded Gabbay }
2745e65e175bSOded Gabbay 
2746e65e175bSOded Gabbay static void gaudi_enable_qman(struct hl_device *hdev, int dma_id,
2747e65e175bSOded Gabbay 				u32 enable_mask)
2748e65e175bSOded Gabbay {
2749e65e175bSOded Gabbay 	u32 dma_qm_offset = dma_id * DMA_QMAN_OFFSET;
2750e65e175bSOded Gabbay 
2751e65e175bSOded Gabbay 	WREG32(mmDMA0_QM_GLBL_CFG0 + dma_qm_offset, enable_mask);
2752e65e175bSOded Gabbay }
2753e65e175bSOded Gabbay 
2754e65e175bSOded Gabbay static void gaudi_init_pci_dma_qmans(struct hl_device *hdev)
2755e65e175bSOded Gabbay {
2756e65e175bSOded Gabbay 	struct gaudi_device *gaudi = hdev->asic_specific;
2757e65e175bSOded Gabbay 	struct hl_hw_queue *q;
2758e65e175bSOded Gabbay 	int i, j, dma_id, cpu_skip, nic_skip, cq_id = 0, q_idx, msi_vec = 0;
2759e65e175bSOded Gabbay 
2760e65e175bSOded Gabbay 	if (gaudi->hw_cap_initialized & HW_CAP_PCI_DMA)
2761e65e175bSOded Gabbay 		return;
2762e65e175bSOded Gabbay 
2763e65e175bSOded Gabbay 	for (i = 0 ; i < PCI_DMA_NUMBER_OF_CHNLS ; i++) {
2764e65e175bSOded Gabbay 		dma_id = gaudi_dma_assignment[i];
2765e65e175bSOded Gabbay 		/*
2766e65e175bSOded Gabbay 		 * For queues after the CPU Q need to add 1 to get the correct
2767e65e175bSOded Gabbay 		 * queue. In addition, need to add the CPU EQ and NIC IRQs in
2768e65e175bSOded Gabbay 		 * order to get the correct MSI register.
2769e65e175bSOded Gabbay 		 */
2770e65e175bSOded Gabbay 		if (dma_id > 1) {
2771e65e175bSOded Gabbay 			cpu_skip = 1;
2772e65e175bSOded Gabbay 			nic_skip = NIC_NUMBER_OF_ENGINES;
2773e65e175bSOded Gabbay 		} else {
2774e65e175bSOded Gabbay 			cpu_skip = 0;
2775e65e175bSOded Gabbay 			nic_skip = 0;
2776e65e175bSOded Gabbay 		}
2777e65e175bSOded Gabbay 
2778e65e175bSOded Gabbay 		for (j = 0 ; j < QMAN_STREAMS ; j++) {
2779e65e175bSOded Gabbay 			q_idx = 4 * dma_id + j + cpu_skip;
2780e65e175bSOded Gabbay 			q = &hdev->kernel_queues[q_idx];
2781e65e175bSOded Gabbay 			q->cq_id = cq_id++;
2782e65e175bSOded Gabbay 			q->msi_vec = nic_skip + cpu_skip + msi_vec++;
2783e65e175bSOded Gabbay 			gaudi_init_pci_dma_qman(hdev, dma_id, j,
2784e65e175bSOded Gabbay 						q->bus_address);
2785e65e175bSOded Gabbay 		}
2786e65e175bSOded Gabbay 
2787e65e175bSOded Gabbay 		gaudi_init_dma_core(hdev, dma_id);
2788e65e175bSOded Gabbay 
2789e65e175bSOded Gabbay 		gaudi_enable_qman(hdev, dma_id, PCI_DMA_QMAN_ENABLE);
2790e65e175bSOded Gabbay 	}
2791e65e175bSOded Gabbay 
2792e65e175bSOded Gabbay 	gaudi->hw_cap_initialized |= HW_CAP_PCI_DMA;
2793e65e175bSOded Gabbay }
2794e65e175bSOded Gabbay 
2795e65e175bSOded Gabbay static void gaudi_init_hbm_dma_qman(struct hl_device *hdev, int dma_id,
2796e65e175bSOded Gabbay 					int qman_id, u64 qman_base_addr)
2797e65e175bSOded Gabbay {
2798e65e175bSOded Gabbay 	struct cpu_dyn_regs *dyn_regs =
2799e65e175bSOded Gabbay 			&hdev->fw_loader.dynamic_loader.comm_desc.cpu_dyn_regs;
2800e65e175bSOded Gabbay 	u32 mtr_base_en_lo, mtr_base_en_hi, mtr_base_ws_lo, mtr_base_ws_hi;
2801e65e175bSOded Gabbay 	u32 so_base_en_lo, so_base_en_hi, so_base_ws_lo, so_base_ws_hi;
2802e65e175bSOded Gabbay 	u32 dma_qm_err_cfg, irq_handler_offset;
2803e65e175bSOded Gabbay 	u32 q_off, dma_qm_offset;
2804e65e175bSOded Gabbay 
2805e65e175bSOded Gabbay 	dma_qm_offset = dma_id * DMA_QMAN_OFFSET;
2806e65e175bSOded Gabbay 
2807e65e175bSOded Gabbay 	mtr_base_en_lo = lower_32_bits(CFG_BASE +
2808e65e175bSOded Gabbay 			mmSYNC_MNGR_E_N_SYNC_MNGR_OBJS_MON_PAY_ADDRL_0);
2809e65e175bSOded Gabbay 	mtr_base_en_hi = upper_32_bits(CFG_BASE +
2810e65e175bSOded Gabbay 				mmSYNC_MNGR_E_N_SYNC_MNGR_OBJS_MON_PAY_ADDRL_0);
2811e65e175bSOded Gabbay 	so_base_en_lo = lower_32_bits(CFG_BASE +
2812e65e175bSOded Gabbay 				mmSYNC_MNGR_E_N_SYNC_MNGR_OBJS_SOB_OBJ_0);
2813e65e175bSOded Gabbay 	so_base_en_hi = upper_32_bits(CFG_BASE +
2814e65e175bSOded Gabbay 				mmSYNC_MNGR_E_N_SYNC_MNGR_OBJS_SOB_OBJ_0);
2815e65e175bSOded Gabbay 	mtr_base_ws_lo = lower_32_bits(CFG_BASE +
2816e65e175bSOded Gabbay 				mmSYNC_MNGR_W_S_SYNC_MNGR_OBJS_MON_PAY_ADDRL_0);
2817e65e175bSOded Gabbay 	mtr_base_ws_hi = upper_32_bits(CFG_BASE +
2818e65e175bSOded Gabbay 				mmSYNC_MNGR_W_S_SYNC_MNGR_OBJS_MON_PAY_ADDRL_0);
2819e65e175bSOded Gabbay 	so_base_ws_lo = lower_32_bits(CFG_BASE +
2820e65e175bSOded Gabbay 				mmSYNC_MNGR_W_S_SYNC_MNGR_OBJS_SOB_OBJ_0);
2821e65e175bSOded Gabbay 	so_base_ws_hi = upper_32_bits(CFG_BASE +
2822e65e175bSOded Gabbay 				mmSYNC_MNGR_W_S_SYNC_MNGR_OBJS_SOB_OBJ_0);
2823e65e175bSOded Gabbay 
2824e65e175bSOded Gabbay 	q_off = dma_qm_offset + qman_id * 4;
2825e65e175bSOded Gabbay 
2826e65e175bSOded Gabbay 	if (qman_id < 4) {
2827e65e175bSOded Gabbay 		WREG32(mmDMA0_QM_PQ_BASE_LO_0 + q_off,
2828e65e175bSOded Gabbay 					lower_32_bits(qman_base_addr));
2829e65e175bSOded Gabbay 		WREG32(mmDMA0_QM_PQ_BASE_HI_0 + q_off,
2830e65e175bSOded Gabbay 					upper_32_bits(qman_base_addr));
2831e65e175bSOded Gabbay 
2832e65e175bSOded Gabbay 		WREG32(mmDMA0_QM_PQ_SIZE_0 + q_off, ilog2(HBM_DMA_QMAN_LENGTH));
2833e65e175bSOded Gabbay 		WREG32(mmDMA0_QM_PQ_PI_0 + q_off, 0);
2834e65e175bSOded Gabbay 		WREG32(mmDMA0_QM_PQ_CI_0 + q_off, 0);
2835e65e175bSOded Gabbay 
2836e65e175bSOded Gabbay 		WREG32(mmDMA0_QM_CP_LDMA_TSIZE_OFFSET_0 + q_off,
2837e65e175bSOded Gabbay 							QMAN_CPDMA_SIZE_OFFSET);
2838e65e175bSOded Gabbay 		WREG32(mmDMA0_QM_CP_LDMA_SRC_BASE_LO_OFFSET_0 + q_off,
2839e65e175bSOded Gabbay 							QMAN_CPDMA_SRC_OFFSET);
2840e65e175bSOded Gabbay 		WREG32(mmDMA0_QM_CP_LDMA_DST_BASE_LO_OFFSET_0 + q_off,
2841e65e175bSOded Gabbay 							QMAN_CPDMA_DST_OFFSET);
2842e65e175bSOded Gabbay 	} else {
2843e65e175bSOded Gabbay 		irq_handler_offset = hdev->asic_prop.gic_interrupts_enable ?
2844e65e175bSOded Gabbay 				mmGIC_DISTRIBUTOR__5_GICD_SETSPI_NSR :
2845e65e175bSOded Gabbay 				le32_to_cpu(dyn_regs->gic_dma_qm_irq_ctrl);
2846e65e175bSOded Gabbay 
2847e65e175bSOded Gabbay 		WREG32(mmDMA0_QM_CP_LDMA_TSIZE_OFFSET_0 + q_off,
2848e65e175bSOded Gabbay 							QMAN_LDMA_SIZE_OFFSET);
2849e65e175bSOded Gabbay 		WREG32(mmDMA0_QM_CP_LDMA_SRC_BASE_LO_OFFSET_0 + q_off,
2850e65e175bSOded Gabbay 							QMAN_LDMA_SRC_OFFSET);
2851e65e175bSOded Gabbay 		WREG32(mmDMA0_QM_CP_LDMA_DST_BASE_LO_OFFSET_0 + q_off,
2852e65e175bSOded Gabbay 							QMAN_LDMA_DST_OFFSET);
2853e65e175bSOded Gabbay 
2854e65e175bSOded Gabbay 		/* Configure RAZWI IRQ */
2855e65e175bSOded Gabbay 		dma_qm_err_cfg = HBM_DMA_QMAN_GLBL_ERR_CFG_MSG_EN_MASK;
2856e65e175bSOded Gabbay 		if (hdev->stop_on_err)
2857e65e175bSOded Gabbay 			dma_qm_err_cfg |=
2858e65e175bSOded Gabbay 				HBM_DMA_QMAN_GLBL_ERR_CFG_STOP_ON_ERR_EN_MASK;
2859e65e175bSOded Gabbay 
2860e65e175bSOded Gabbay 		WREG32(mmDMA0_QM_GLBL_ERR_CFG + dma_qm_offset, dma_qm_err_cfg);
2861e65e175bSOded Gabbay 
2862e65e175bSOded Gabbay 		WREG32(mmDMA0_QM_GLBL_ERR_ADDR_LO + dma_qm_offset,
2863e65e175bSOded Gabbay 			lower_32_bits(CFG_BASE + irq_handler_offset));
2864e65e175bSOded Gabbay 		WREG32(mmDMA0_QM_GLBL_ERR_ADDR_HI + dma_qm_offset,
2865e65e175bSOded Gabbay 			upper_32_bits(CFG_BASE + irq_handler_offset));
2866e65e175bSOded Gabbay 
2867e65e175bSOded Gabbay 		WREG32(mmDMA0_QM_GLBL_ERR_WDATA + dma_qm_offset,
2868e65e175bSOded Gabbay 			gaudi_irq_map_table[GAUDI_EVENT_DMA0_QM].cpu_id +
2869e65e175bSOded Gabbay 									dma_id);
2870e65e175bSOded Gabbay 
2871e65e175bSOded Gabbay 		WREG32(mmDMA0_QM_ARB_ERR_MSG_EN + dma_qm_offset,
2872e65e175bSOded Gabbay 				QM_ARB_ERR_MSG_EN_MASK);
2873e65e175bSOded Gabbay 
2874e65e175bSOded Gabbay 		/* Set timeout to maximum */
2875e65e175bSOded Gabbay 		WREG32(mmDMA0_QM_ARB_SLV_CHOISE_WDT + dma_qm_offset, GAUDI_ARB_WDT_TIMEOUT);
2876e65e175bSOded Gabbay 
2877e65e175bSOded Gabbay 		WREG32(mmDMA0_QM_GLBL_CFG1 + dma_qm_offset, 0);
2878e65e175bSOded Gabbay 		WREG32(mmDMA0_QM_GLBL_PROT + dma_qm_offset,
2879e65e175bSOded Gabbay 				QMAN_INTERNAL_MAKE_TRUSTED);
2880e65e175bSOded Gabbay 	}
2881e65e175bSOded Gabbay 
2882e65e175bSOded Gabbay 	WREG32(mmDMA0_QM_CP_MSG_BASE0_ADDR_LO_0 + q_off, mtr_base_en_lo);
2883e65e175bSOded Gabbay 	WREG32(mmDMA0_QM_CP_MSG_BASE0_ADDR_HI_0 + q_off, mtr_base_en_hi);
2884e65e175bSOded Gabbay 	WREG32(mmDMA0_QM_CP_MSG_BASE1_ADDR_LO_0 + q_off, so_base_en_lo);
2885e65e175bSOded Gabbay 	WREG32(mmDMA0_QM_CP_MSG_BASE1_ADDR_HI_0 + q_off, so_base_en_hi);
2886e65e175bSOded Gabbay 
2887e65e175bSOded Gabbay 	/* Configure DMA5 CP_MSG_BASE 2/3 for sync stream collective */
2888e65e175bSOded Gabbay 	if (gaudi_dma_assignment[dma_id] == GAUDI_ENGINE_ID_DMA_5) {
2889e65e175bSOded Gabbay 		WREG32(mmDMA0_QM_CP_MSG_BASE2_ADDR_LO_0 + q_off,
2890e65e175bSOded Gabbay 				mtr_base_ws_lo);
2891e65e175bSOded Gabbay 		WREG32(mmDMA0_QM_CP_MSG_BASE2_ADDR_HI_0 + q_off,
2892e65e175bSOded Gabbay 				mtr_base_ws_hi);
2893e65e175bSOded Gabbay 		WREG32(mmDMA0_QM_CP_MSG_BASE3_ADDR_LO_0 + q_off,
2894e65e175bSOded Gabbay 				so_base_ws_lo);
2895e65e175bSOded Gabbay 		WREG32(mmDMA0_QM_CP_MSG_BASE3_ADDR_HI_0 + q_off,
2896e65e175bSOded Gabbay 				so_base_ws_hi);
2897e65e175bSOded Gabbay 	}
2898e65e175bSOded Gabbay }
2899e65e175bSOded Gabbay 
2900e65e175bSOded Gabbay static void gaudi_init_hbm_dma_qmans(struct hl_device *hdev)
2901e65e175bSOded Gabbay {
2902e65e175bSOded Gabbay 	struct gaudi_device *gaudi = hdev->asic_specific;
2903e65e175bSOded Gabbay 	struct gaudi_internal_qman_info *q;
2904e65e175bSOded Gabbay 	u64 qman_base_addr;
2905e65e175bSOded Gabbay 	int i, j, dma_id, internal_q_index;
2906e65e175bSOded Gabbay 
2907e65e175bSOded Gabbay 	if (gaudi->hw_cap_initialized & HW_CAP_HBM_DMA)
2908e65e175bSOded Gabbay 		return;
2909e65e175bSOded Gabbay 
2910e65e175bSOded Gabbay 	for (i = 0 ; i < HBM_DMA_NUMBER_OF_CHNLS ; i++) {
2911e65e175bSOded Gabbay 		dma_id = gaudi_dma_assignment[GAUDI_HBM_DMA_1 + i];
2912e65e175bSOded Gabbay 
2913e65e175bSOded Gabbay 		for (j = 0 ; j < QMAN_STREAMS ; j++) {
2914e65e175bSOded Gabbay 			 /*
2915e65e175bSOded Gabbay 			  * Add the CPU queue in order to get the correct queue
2916e65e175bSOded Gabbay 			  * number as all internal queue are placed after it
2917e65e175bSOded Gabbay 			  */
2918e65e175bSOded Gabbay 			internal_q_index = dma_id * QMAN_STREAMS + j + 1;
2919e65e175bSOded Gabbay 
2920e65e175bSOded Gabbay 			q = &gaudi->internal_qmans[internal_q_index];
2921e65e175bSOded Gabbay 			qman_base_addr = (u64) q->pq_dma_addr;
2922e65e175bSOded Gabbay 			gaudi_init_hbm_dma_qman(hdev, dma_id, j,
2923e65e175bSOded Gabbay 						qman_base_addr);
2924e65e175bSOded Gabbay 		}
2925e65e175bSOded Gabbay 
2926e65e175bSOded Gabbay 		/* Initializing lower CP for HBM DMA QMAN */
2927e65e175bSOded Gabbay 		gaudi_init_hbm_dma_qman(hdev, dma_id, 4, 0);
2928e65e175bSOded Gabbay 
2929e65e175bSOded Gabbay 		gaudi_init_dma_core(hdev, dma_id);
2930e65e175bSOded Gabbay 
2931e65e175bSOded Gabbay 		gaudi_enable_qman(hdev, dma_id, HBM_DMA_QMAN_ENABLE);
2932e65e175bSOded Gabbay 	}
2933e65e175bSOded Gabbay 
2934e65e175bSOded Gabbay 	gaudi->hw_cap_initialized |= HW_CAP_HBM_DMA;
2935e65e175bSOded Gabbay }
2936e65e175bSOded Gabbay 
2937e65e175bSOded Gabbay static void gaudi_init_mme_qman(struct hl_device *hdev, u32 mme_offset,
2938e65e175bSOded Gabbay 					int qman_id, u64 qman_base_addr)
2939e65e175bSOded Gabbay {
2940e65e175bSOded Gabbay 	struct cpu_dyn_regs *dyn_regs =
2941e65e175bSOded Gabbay 			&hdev->fw_loader.dynamic_loader.comm_desc.cpu_dyn_regs;
2942e65e175bSOded Gabbay 	u32 mtr_base_lo, mtr_base_hi;
2943e65e175bSOded Gabbay 	u32 so_base_lo, so_base_hi;
2944e65e175bSOded Gabbay 	u32 irq_handler_offset;
2945e65e175bSOded Gabbay 	u32 q_off, mme_id;
2946e65e175bSOded Gabbay 	u32 mme_qm_err_cfg;
2947e65e175bSOded Gabbay 
2948e65e175bSOded Gabbay 	mtr_base_lo = lower_32_bits(CFG_BASE +
2949e65e175bSOded Gabbay 				mmSYNC_MNGR_E_N_SYNC_MNGR_OBJS_MON_PAY_ADDRL_0);
2950e65e175bSOded Gabbay 	mtr_base_hi = upper_32_bits(CFG_BASE +
2951e65e175bSOded Gabbay 				mmSYNC_MNGR_E_N_SYNC_MNGR_OBJS_MON_PAY_ADDRL_0);
2952e65e175bSOded Gabbay 	so_base_lo = lower_32_bits(CFG_BASE +
2953e65e175bSOded Gabbay 				mmSYNC_MNGR_E_N_SYNC_MNGR_OBJS_SOB_OBJ_0);
2954e65e175bSOded Gabbay 	so_base_hi = upper_32_bits(CFG_BASE +
2955e65e175bSOded Gabbay 				mmSYNC_MNGR_E_N_SYNC_MNGR_OBJS_SOB_OBJ_0);
2956e65e175bSOded Gabbay 
2957e65e175bSOded Gabbay 	q_off = mme_offset + qman_id * 4;
2958e65e175bSOded Gabbay 
2959e65e175bSOded Gabbay 	if (qman_id < 4) {
2960e65e175bSOded Gabbay 		WREG32(mmMME0_QM_PQ_BASE_LO_0 + q_off,
2961e65e175bSOded Gabbay 					lower_32_bits(qman_base_addr));
2962e65e175bSOded Gabbay 		WREG32(mmMME0_QM_PQ_BASE_HI_0 + q_off,
2963e65e175bSOded Gabbay 					upper_32_bits(qman_base_addr));
2964e65e175bSOded Gabbay 
2965e65e175bSOded Gabbay 		WREG32(mmMME0_QM_PQ_SIZE_0 + q_off, ilog2(MME_QMAN_LENGTH));
2966e65e175bSOded Gabbay 		WREG32(mmMME0_QM_PQ_PI_0 + q_off, 0);
2967e65e175bSOded Gabbay 		WREG32(mmMME0_QM_PQ_CI_0 + q_off, 0);
2968e65e175bSOded Gabbay 
2969e65e175bSOded Gabbay 		WREG32(mmMME0_QM_CP_LDMA_TSIZE_OFFSET_0 + q_off,
2970e65e175bSOded Gabbay 							QMAN_CPDMA_SIZE_OFFSET);
2971e65e175bSOded Gabbay 		WREG32(mmMME0_QM_CP_LDMA_SRC_BASE_LO_OFFSET_0 + q_off,
2972e65e175bSOded Gabbay 							QMAN_CPDMA_SRC_OFFSET);
2973e65e175bSOded Gabbay 		WREG32(mmMME0_QM_CP_LDMA_DST_BASE_LO_OFFSET_0 + q_off,
2974e65e175bSOded Gabbay 							QMAN_CPDMA_DST_OFFSET);
2975e65e175bSOded Gabbay 	} else {
2976e65e175bSOded Gabbay 		irq_handler_offset = hdev->asic_prop.gic_interrupts_enable ?
2977e65e175bSOded Gabbay 				mmGIC_DISTRIBUTOR__5_GICD_SETSPI_NSR :
2978e65e175bSOded Gabbay 				le32_to_cpu(dyn_regs->gic_mme_qm_irq_ctrl);
2979e65e175bSOded Gabbay 
2980e65e175bSOded Gabbay 		WREG32(mmMME0_QM_CP_LDMA_TSIZE_OFFSET_0 + q_off,
2981e65e175bSOded Gabbay 							QMAN_LDMA_SIZE_OFFSET);
2982e65e175bSOded Gabbay 		WREG32(mmMME0_QM_CP_LDMA_SRC_BASE_LO_OFFSET_0 + q_off,
2983e65e175bSOded Gabbay 							QMAN_LDMA_SRC_OFFSET);
2984e65e175bSOded Gabbay 		WREG32(mmMME0_QM_CP_LDMA_DST_BASE_LO_OFFSET_0 + q_off,
2985e65e175bSOded Gabbay 							QMAN_LDMA_DST_OFFSET);
2986e65e175bSOded Gabbay 
2987e65e175bSOded Gabbay 		/* Configure RAZWI IRQ */
2988e65e175bSOded Gabbay 		mme_id = mme_offset /
2989e65e175bSOded Gabbay 				(mmMME1_QM_GLBL_CFG0 - mmMME0_QM_GLBL_CFG0) / 2;
2990e65e175bSOded Gabbay 
2991e65e175bSOded Gabbay 		mme_qm_err_cfg = MME_QMAN_GLBL_ERR_CFG_MSG_EN_MASK;
2992e65e175bSOded Gabbay 		if (hdev->stop_on_err)
2993e65e175bSOded Gabbay 			mme_qm_err_cfg |=
2994e65e175bSOded Gabbay 				MME_QMAN_GLBL_ERR_CFG_STOP_ON_ERR_EN_MASK;
2995e65e175bSOded Gabbay 
2996e65e175bSOded Gabbay 		WREG32(mmMME0_QM_GLBL_ERR_CFG + mme_offset, mme_qm_err_cfg);
2997e65e175bSOded Gabbay 
2998e65e175bSOded Gabbay 		WREG32(mmMME0_QM_GLBL_ERR_ADDR_LO + mme_offset,
2999e65e175bSOded Gabbay 			lower_32_bits(CFG_BASE + irq_handler_offset));
3000e65e175bSOded Gabbay 		WREG32(mmMME0_QM_GLBL_ERR_ADDR_HI + mme_offset,
3001e65e175bSOded Gabbay 			upper_32_bits(CFG_BASE + irq_handler_offset));
3002e65e175bSOded Gabbay 
3003e65e175bSOded Gabbay 		WREG32(mmMME0_QM_GLBL_ERR_WDATA + mme_offset,
3004e65e175bSOded Gabbay 			gaudi_irq_map_table[GAUDI_EVENT_MME0_QM].cpu_id +
3005e65e175bSOded Gabbay 									mme_id);
3006e65e175bSOded Gabbay 
3007e65e175bSOded Gabbay 		WREG32(mmMME0_QM_ARB_ERR_MSG_EN + mme_offset,
3008e65e175bSOded Gabbay 				QM_ARB_ERR_MSG_EN_MASK);
3009e65e175bSOded Gabbay 
3010e65e175bSOded Gabbay 		/* Set timeout to maximum */
3011e65e175bSOded Gabbay 		WREG32(mmMME0_QM_ARB_SLV_CHOISE_WDT + mme_offset, GAUDI_ARB_WDT_TIMEOUT);
3012e65e175bSOded Gabbay 
3013e65e175bSOded Gabbay 		WREG32(mmMME0_QM_GLBL_CFG1 + mme_offset, 0);
3014e65e175bSOded Gabbay 		WREG32(mmMME0_QM_GLBL_PROT + mme_offset,
3015e65e175bSOded Gabbay 				QMAN_INTERNAL_MAKE_TRUSTED);
3016e65e175bSOded Gabbay 	}
3017e65e175bSOded Gabbay 
3018e65e175bSOded Gabbay 	WREG32(mmMME0_QM_CP_MSG_BASE0_ADDR_LO_0 + q_off, mtr_base_lo);
3019e65e175bSOded Gabbay 	WREG32(mmMME0_QM_CP_MSG_BASE0_ADDR_HI_0 + q_off, mtr_base_hi);
3020e65e175bSOded Gabbay 	WREG32(mmMME0_QM_CP_MSG_BASE1_ADDR_LO_0 + q_off, so_base_lo);
3021e65e175bSOded Gabbay 	WREG32(mmMME0_QM_CP_MSG_BASE1_ADDR_HI_0 + q_off, so_base_hi);
3022e65e175bSOded Gabbay }
3023e65e175bSOded Gabbay 
3024e65e175bSOded Gabbay static void gaudi_init_mme_qmans(struct hl_device *hdev)
3025e65e175bSOded Gabbay {
3026e65e175bSOded Gabbay 	struct gaudi_device *gaudi = hdev->asic_specific;
3027e65e175bSOded Gabbay 	struct gaudi_internal_qman_info *q;
3028e65e175bSOded Gabbay 	u64 qman_base_addr;
3029e65e175bSOded Gabbay 	u32 mme_offset;
3030e65e175bSOded Gabbay 	int i, internal_q_index;
3031e65e175bSOded Gabbay 
3032e65e175bSOded Gabbay 	if (gaudi->hw_cap_initialized & HW_CAP_MME)
3033e65e175bSOded Gabbay 		return;
3034e65e175bSOded Gabbay 
3035e65e175bSOded Gabbay 	/*
3036e65e175bSOded Gabbay 	 * map GAUDI_QUEUE_ID_MME_0_X to the N_W_MME (mmMME2_QM_BASE)
3037e65e175bSOded Gabbay 	 * and GAUDI_QUEUE_ID_MME_1_X to the S_W_MME (mmMME0_QM_BASE)
3038e65e175bSOded Gabbay 	 */
3039e65e175bSOded Gabbay 
3040e65e175bSOded Gabbay 	mme_offset = mmMME2_QM_GLBL_CFG0 - mmMME0_QM_GLBL_CFG0;
3041e65e175bSOded Gabbay 
3042e65e175bSOded Gabbay 	for (i = 0 ; i < MME_NUMBER_OF_QMANS ; i++) {
3043e65e175bSOded Gabbay 		internal_q_index = GAUDI_QUEUE_ID_MME_0_0 + i;
3044e65e175bSOded Gabbay 		q = &gaudi->internal_qmans[internal_q_index];
3045e65e175bSOded Gabbay 		qman_base_addr = (u64) q->pq_dma_addr;
3046e65e175bSOded Gabbay 		gaudi_init_mme_qman(hdev, mme_offset, (i & 0x3),
3047e65e175bSOded Gabbay 					qman_base_addr);
3048e65e175bSOded Gabbay 		if (i == 3)
3049e65e175bSOded Gabbay 			mme_offset = 0;
3050e65e175bSOded Gabbay 	}
3051e65e175bSOded Gabbay 
3052e65e175bSOded Gabbay 	/* Initializing lower CP for MME QMANs */
3053e65e175bSOded Gabbay 	mme_offset = mmMME2_QM_GLBL_CFG0 - mmMME0_QM_GLBL_CFG0;
3054e65e175bSOded Gabbay 	gaudi_init_mme_qman(hdev, mme_offset, 4, 0);
3055e65e175bSOded Gabbay 	gaudi_init_mme_qman(hdev, 0, 4, 0);
3056e65e175bSOded Gabbay 
3057e65e175bSOded Gabbay 	WREG32(mmMME2_QM_GLBL_CFG0, QMAN_MME_ENABLE);
3058e65e175bSOded Gabbay 	WREG32(mmMME0_QM_GLBL_CFG0, QMAN_MME_ENABLE);
3059e65e175bSOded Gabbay 
3060e65e175bSOded Gabbay 	gaudi->hw_cap_initialized |= HW_CAP_MME;
3061e65e175bSOded Gabbay }
3062e65e175bSOded Gabbay 
3063e65e175bSOded Gabbay static void gaudi_init_tpc_qman(struct hl_device *hdev, u32 tpc_offset,
3064e65e175bSOded Gabbay 				int qman_id, u64 qman_base_addr)
3065e65e175bSOded Gabbay {
3066e65e175bSOded Gabbay 	struct cpu_dyn_regs *dyn_regs =
3067e65e175bSOded Gabbay 			&hdev->fw_loader.dynamic_loader.comm_desc.cpu_dyn_regs;
3068e65e175bSOded Gabbay 	u32 mtr_base_en_lo, mtr_base_en_hi, mtr_base_ws_lo, mtr_base_ws_hi;
3069e65e175bSOded Gabbay 	u32 so_base_en_lo, so_base_en_hi, so_base_ws_lo, so_base_ws_hi;
3070e65e175bSOded Gabbay 	u32 tpc_qm_err_cfg, irq_handler_offset;
3071e65e175bSOded Gabbay 	u32 q_off, tpc_id;
3072e65e175bSOded Gabbay 
3073e65e175bSOded Gabbay 	mtr_base_en_lo = lower_32_bits(CFG_BASE +
3074e65e175bSOded Gabbay 			mmSYNC_MNGR_E_N_SYNC_MNGR_OBJS_MON_PAY_ADDRL_0);
3075e65e175bSOded Gabbay 	mtr_base_en_hi = upper_32_bits(CFG_BASE +
3076e65e175bSOded Gabbay 				mmSYNC_MNGR_E_N_SYNC_MNGR_OBJS_MON_PAY_ADDRL_0);
3077e65e175bSOded Gabbay 	so_base_en_lo = lower_32_bits(CFG_BASE +
3078e65e175bSOded Gabbay 				mmSYNC_MNGR_E_N_SYNC_MNGR_OBJS_SOB_OBJ_0);
3079e65e175bSOded Gabbay 	so_base_en_hi = upper_32_bits(CFG_BASE +
3080e65e175bSOded Gabbay 				mmSYNC_MNGR_E_N_SYNC_MNGR_OBJS_SOB_OBJ_0);
3081e65e175bSOded Gabbay 	mtr_base_ws_lo = lower_32_bits(CFG_BASE +
3082e65e175bSOded Gabbay 				mmSYNC_MNGR_W_S_SYNC_MNGR_OBJS_MON_PAY_ADDRL_0);
3083e65e175bSOded Gabbay 	mtr_base_ws_hi = upper_32_bits(CFG_BASE +
3084e65e175bSOded Gabbay 				mmSYNC_MNGR_W_S_SYNC_MNGR_OBJS_MON_PAY_ADDRL_0);
3085e65e175bSOded Gabbay 	so_base_ws_lo = lower_32_bits(CFG_BASE +
3086e65e175bSOded Gabbay 				mmSYNC_MNGR_W_S_SYNC_MNGR_OBJS_SOB_OBJ_0);
3087e65e175bSOded Gabbay 	so_base_ws_hi = upper_32_bits(CFG_BASE +
3088e65e175bSOded Gabbay 				mmSYNC_MNGR_W_S_SYNC_MNGR_OBJS_SOB_OBJ_0);
3089e65e175bSOded Gabbay 
3090e65e175bSOded Gabbay 	q_off = tpc_offset + qman_id * 4;
3091e65e175bSOded Gabbay 
3092e65e175bSOded Gabbay 	tpc_id = tpc_offset /
3093e65e175bSOded Gabbay 			(mmTPC1_QM_GLBL_CFG0 - mmTPC0_QM_GLBL_CFG0);
3094e65e175bSOded Gabbay 
3095e65e175bSOded Gabbay 	if (qman_id < 4) {
3096e65e175bSOded Gabbay 		WREG32(mmTPC0_QM_PQ_BASE_LO_0 + q_off,
3097e65e175bSOded Gabbay 					lower_32_bits(qman_base_addr));
3098e65e175bSOded Gabbay 		WREG32(mmTPC0_QM_PQ_BASE_HI_0 + q_off,
3099e65e175bSOded Gabbay 					upper_32_bits(qman_base_addr));
3100e65e175bSOded Gabbay 
3101e65e175bSOded Gabbay 		WREG32(mmTPC0_QM_PQ_SIZE_0 + q_off, ilog2(TPC_QMAN_LENGTH));
3102e65e175bSOded Gabbay 		WREG32(mmTPC0_QM_PQ_PI_0 + q_off, 0);
3103e65e175bSOded Gabbay 		WREG32(mmTPC0_QM_PQ_CI_0 + q_off, 0);
3104e65e175bSOded Gabbay 
3105e65e175bSOded Gabbay 		WREG32(mmTPC0_QM_CP_LDMA_TSIZE_OFFSET_0 + q_off,
3106e65e175bSOded Gabbay 							QMAN_CPDMA_SIZE_OFFSET);
3107e65e175bSOded Gabbay 		WREG32(mmTPC0_QM_CP_LDMA_SRC_BASE_LO_OFFSET_0 + q_off,
3108e65e175bSOded Gabbay 							QMAN_CPDMA_SRC_OFFSET);
3109e65e175bSOded Gabbay 		WREG32(mmTPC0_QM_CP_LDMA_DST_BASE_LO_OFFSET_0 + q_off,
3110e65e175bSOded Gabbay 							QMAN_CPDMA_DST_OFFSET);
3111e65e175bSOded Gabbay 	} else {
3112e65e175bSOded Gabbay 		irq_handler_offset = hdev->asic_prop.gic_interrupts_enable ?
3113e65e175bSOded Gabbay 				mmGIC_DISTRIBUTOR__5_GICD_SETSPI_NSR :
3114e65e175bSOded Gabbay 				le32_to_cpu(dyn_regs->gic_tpc_qm_irq_ctrl);
3115e65e175bSOded Gabbay 
3116e65e175bSOded Gabbay 		WREG32(mmTPC0_QM_CP_LDMA_TSIZE_OFFSET_0 + q_off,
3117e65e175bSOded Gabbay 							QMAN_LDMA_SIZE_OFFSET);
3118e65e175bSOded Gabbay 		WREG32(mmTPC0_QM_CP_LDMA_SRC_BASE_LO_OFFSET_0 + q_off,
3119e65e175bSOded Gabbay 							QMAN_LDMA_SRC_OFFSET);
3120e65e175bSOded Gabbay 		WREG32(mmTPC0_QM_CP_LDMA_DST_BASE_LO_OFFSET_0 + q_off,
3121e65e175bSOded Gabbay 							QMAN_LDMA_DST_OFFSET);
3122e65e175bSOded Gabbay 
3123e65e175bSOded Gabbay 		/* Configure RAZWI IRQ */
3124e65e175bSOded Gabbay 		tpc_qm_err_cfg = TPC_QMAN_GLBL_ERR_CFG_MSG_EN_MASK;
3125e65e175bSOded Gabbay 		if (hdev->stop_on_err)
3126e65e175bSOded Gabbay 			tpc_qm_err_cfg |=
3127e65e175bSOded Gabbay 				TPC_QMAN_GLBL_ERR_CFG_STOP_ON_ERR_EN_MASK;
3128e65e175bSOded Gabbay 
3129e65e175bSOded Gabbay 		WREG32(mmTPC0_QM_GLBL_ERR_CFG + tpc_offset, tpc_qm_err_cfg);
3130e65e175bSOded Gabbay 
3131e65e175bSOded Gabbay 		WREG32(mmTPC0_QM_GLBL_ERR_ADDR_LO + tpc_offset,
3132e65e175bSOded Gabbay 			lower_32_bits(CFG_BASE + irq_handler_offset));
3133e65e175bSOded Gabbay 		WREG32(mmTPC0_QM_GLBL_ERR_ADDR_HI + tpc_offset,
3134e65e175bSOded Gabbay 			upper_32_bits(CFG_BASE + irq_handler_offset));
3135e65e175bSOded Gabbay 
3136e65e175bSOded Gabbay 		WREG32(mmTPC0_QM_GLBL_ERR_WDATA + tpc_offset,
3137e65e175bSOded Gabbay 			gaudi_irq_map_table[GAUDI_EVENT_TPC0_QM].cpu_id +
3138e65e175bSOded Gabbay 									tpc_id);
3139e65e175bSOded Gabbay 
3140e65e175bSOded Gabbay 		WREG32(mmTPC0_QM_ARB_ERR_MSG_EN + tpc_offset,
3141e65e175bSOded Gabbay 				QM_ARB_ERR_MSG_EN_MASK);
3142e65e175bSOded Gabbay 
3143e65e175bSOded Gabbay 		/* Set timeout to maximum */
3144e65e175bSOded Gabbay 		WREG32(mmTPC0_QM_ARB_SLV_CHOISE_WDT + tpc_offset, GAUDI_ARB_WDT_TIMEOUT);
3145e65e175bSOded Gabbay 
3146e65e175bSOded Gabbay 		WREG32(mmTPC0_QM_GLBL_CFG1 + tpc_offset, 0);
3147e65e175bSOded Gabbay 		WREG32(mmTPC0_QM_GLBL_PROT + tpc_offset,
3148e65e175bSOded Gabbay 				QMAN_INTERNAL_MAKE_TRUSTED);
3149e65e175bSOded Gabbay 	}
3150e65e175bSOded Gabbay 
3151e65e175bSOded Gabbay 	WREG32(mmTPC0_QM_CP_MSG_BASE0_ADDR_LO_0 + q_off, mtr_base_en_lo);
3152e65e175bSOded Gabbay 	WREG32(mmTPC0_QM_CP_MSG_BASE0_ADDR_HI_0 + q_off, mtr_base_en_hi);
3153e65e175bSOded Gabbay 	WREG32(mmTPC0_QM_CP_MSG_BASE1_ADDR_LO_0 + q_off, so_base_en_lo);
3154e65e175bSOded Gabbay 	WREG32(mmTPC0_QM_CP_MSG_BASE1_ADDR_HI_0 + q_off, so_base_en_hi);
3155e65e175bSOded Gabbay 
3156e65e175bSOded Gabbay 	/* Configure TPC7 CP_MSG_BASE 2/3 for sync stream collective */
3157e65e175bSOded Gabbay 	if (tpc_id == 6) {
3158e65e175bSOded Gabbay 		WREG32(mmTPC0_QM_CP_MSG_BASE2_ADDR_LO_0 + q_off,
3159e65e175bSOded Gabbay 				mtr_base_ws_lo);
3160e65e175bSOded Gabbay 		WREG32(mmTPC0_QM_CP_MSG_BASE2_ADDR_HI_0 + q_off,
3161e65e175bSOded Gabbay 				mtr_base_ws_hi);
3162e65e175bSOded Gabbay 		WREG32(mmTPC0_QM_CP_MSG_BASE3_ADDR_LO_0 + q_off,
3163e65e175bSOded Gabbay 				so_base_ws_lo);
3164e65e175bSOded Gabbay 		WREG32(mmTPC0_QM_CP_MSG_BASE3_ADDR_HI_0 + q_off,
3165e65e175bSOded Gabbay 				so_base_ws_hi);
3166e65e175bSOded Gabbay 	}
3167e65e175bSOded Gabbay }
3168e65e175bSOded Gabbay 
3169e65e175bSOded Gabbay static void gaudi_init_tpc_qmans(struct hl_device *hdev)
3170e65e175bSOded Gabbay {
3171e65e175bSOded Gabbay 	struct gaudi_device *gaudi = hdev->asic_specific;
3172e65e175bSOded Gabbay 	struct gaudi_internal_qman_info *q;
3173e65e175bSOded Gabbay 	u64 qman_base_addr;
3174e65e175bSOded Gabbay 	u32 so_base_hi, tpc_offset = 0;
3175e65e175bSOded Gabbay 	u32 tpc_delta = mmTPC1_CFG_SM_BASE_ADDRESS_HIGH -
3176e65e175bSOded Gabbay 			mmTPC0_CFG_SM_BASE_ADDRESS_HIGH;
3177e65e175bSOded Gabbay 	int i, tpc_id, internal_q_index;
3178e65e175bSOded Gabbay 
3179e65e175bSOded Gabbay 	if (gaudi->hw_cap_initialized & HW_CAP_TPC_MASK)
3180e65e175bSOded Gabbay 		return;
3181e65e175bSOded Gabbay 
3182e65e175bSOded Gabbay 	so_base_hi = upper_32_bits(CFG_BASE +
3183e65e175bSOded Gabbay 				mmSYNC_MNGR_E_N_SYNC_MNGR_OBJS_SOB_OBJ_0);
3184e65e175bSOded Gabbay 
3185e65e175bSOded Gabbay 	for (tpc_id = 0 ; tpc_id < TPC_NUMBER_OF_ENGINES ; tpc_id++) {
3186e65e175bSOded Gabbay 		for (i = 0 ; i < QMAN_STREAMS ; i++) {
3187e65e175bSOded Gabbay 			internal_q_index = GAUDI_QUEUE_ID_TPC_0_0 +
3188e65e175bSOded Gabbay 						tpc_id * QMAN_STREAMS + i;
3189e65e175bSOded Gabbay 			q = &gaudi->internal_qmans[internal_q_index];
3190e65e175bSOded Gabbay 			qman_base_addr = (u64) q->pq_dma_addr;
3191e65e175bSOded Gabbay 			gaudi_init_tpc_qman(hdev, tpc_offset, i,
3192e65e175bSOded Gabbay 						qman_base_addr);
3193e65e175bSOded Gabbay 
3194e65e175bSOded Gabbay 			if (i == 3) {
3195e65e175bSOded Gabbay 				/* Initializing lower CP for TPC QMAN */
3196e65e175bSOded Gabbay 				gaudi_init_tpc_qman(hdev, tpc_offset, 4, 0);
3197e65e175bSOded Gabbay 
3198e65e175bSOded Gabbay 				/* Enable the QMAN and TPC channel */
3199e65e175bSOded Gabbay 				WREG32(mmTPC0_QM_GLBL_CFG0 + tpc_offset,
3200e65e175bSOded Gabbay 						QMAN_TPC_ENABLE);
3201e65e175bSOded Gabbay 			}
3202e65e175bSOded Gabbay 		}
3203e65e175bSOded Gabbay 
3204e65e175bSOded Gabbay 		WREG32(mmTPC0_CFG_SM_BASE_ADDRESS_HIGH + tpc_id * tpc_delta,
3205e65e175bSOded Gabbay 				so_base_hi);
3206e65e175bSOded Gabbay 
3207e65e175bSOded Gabbay 		tpc_offset += mmTPC1_QM_GLBL_CFG0 - mmTPC0_QM_GLBL_CFG0;
3208e65e175bSOded Gabbay 
3209e65e175bSOded Gabbay 		gaudi->hw_cap_initialized |=
3210e65e175bSOded Gabbay 				FIELD_PREP(HW_CAP_TPC_MASK, 1 << tpc_id);
3211e65e175bSOded Gabbay 	}
3212e65e175bSOded Gabbay }
3213e65e175bSOded Gabbay 
3214e65e175bSOded Gabbay static void gaudi_init_nic_qman(struct hl_device *hdev, u32 nic_offset,
3215e65e175bSOded Gabbay 				int qman_id, u64 qman_base_addr, int nic_id)
3216e65e175bSOded Gabbay {
3217e65e175bSOded Gabbay 	struct cpu_dyn_regs *dyn_regs =
3218e65e175bSOded Gabbay 			&hdev->fw_loader.dynamic_loader.comm_desc.cpu_dyn_regs;
3219e65e175bSOded Gabbay 	u32 mtr_base_en_lo, mtr_base_en_hi, mtr_base_ws_lo, mtr_base_ws_hi;
3220e65e175bSOded Gabbay 	u32 so_base_en_lo, so_base_en_hi, so_base_ws_lo, so_base_ws_hi;
3221e65e175bSOded Gabbay 	u32 nic_qm_err_cfg, irq_handler_offset;
3222e65e175bSOded Gabbay 	u32 q_off;
3223e65e175bSOded Gabbay 
3224e65e175bSOded Gabbay 	mtr_base_en_lo = lower_32_bits((CFG_BASE & U32_MAX) +
3225e65e175bSOded Gabbay 			mmSYNC_MNGR_E_N_SYNC_MNGR_OBJS_MON_PAY_ADDRL_0);
3226e65e175bSOded Gabbay 	mtr_base_en_hi = upper_32_bits(CFG_BASE +
3227e65e175bSOded Gabbay 				mmSYNC_MNGR_E_N_SYNC_MNGR_OBJS_MON_PAY_ADDRL_0);
3228e65e175bSOded Gabbay 	so_base_en_lo = lower_32_bits((CFG_BASE & U32_MAX) +
3229e65e175bSOded Gabbay 				mmSYNC_MNGR_E_N_SYNC_MNGR_OBJS_SOB_OBJ_0);
3230e65e175bSOded Gabbay 	so_base_en_hi = upper_32_bits(CFG_BASE +
3231e65e175bSOded Gabbay 				mmSYNC_MNGR_E_N_SYNC_MNGR_OBJS_SOB_OBJ_0);
3232e65e175bSOded Gabbay 	mtr_base_ws_lo = lower_32_bits((CFG_BASE & U32_MAX) +
3233e65e175bSOded Gabbay 				mmSYNC_MNGR_W_S_SYNC_MNGR_OBJS_MON_PAY_ADDRL_0);
3234e65e175bSOded Gabbay 	mtr_base_ws_hi = upper_32_bits(CFG_BASE +
3235e65e175bSOded Gabbay 				mmSYNC_MNGR_W_S_SYNC_MNGR_OBJS_MON_PAY_ADDRL_0);
3236e65e175bSOded Gabbay 	so_base_ws_lo = lower_32_bits((CFG_BASE & U32_MAX) +
3237e65e175bSOded Gabbay 				mmSYNC_MNGR_W_S_SYNC_MNGR_OBJS_SOB_OBJ_0);
3238e65e175bSOded Gabbay 	so_base_ws_hi = upper_32_bits(CFG_BASE +
3239e65e175bSOded Gabbay 				mmSYNC_MNGR_W_S_SYNC_MNGR_OBJS_SOB_OBJ_0);
3240e65e175bSOded Gabbay 
3241e65e175bSOded Gabbay 	q_off = nic_offset + qman_id * 4;
3242e65e175bSOded Gabbay 
3243e65e175bSOded Gabbay 	WREG32(mmNIC0_QM0_PQ_BASE_LO_0 + q_off, lower_32_bits(qman_base_addr));
3244e65e175bSOded Gabbay 	WREG32(mmNIC0_QM0_PQ_BASE_HI_0 + q_off, upper_32_bits(qman_base_addr));
3245e65e175bSOded Gabbay 
3246e65e175bSOded Gabbay 	WREG32(mmNIC0_QM0_PQ_SIZE_0 + q_off, ilog2(NIC_QMAN_LENGTH));
3247e65e175bSOded Gabbay 	WREG32(mmNIC0_QM0_PQ_PI_0 + q_off, 0);
3248e65e175bSOded Gabbay 	WREG32(mmNIC0_QM0_PQ_CI_0 + q_off, 0);
3249e65e175bSOded Gabbay 
3250e65e175bSOded Gabbay 	WREG32(mmNIC0_QM0_CP_LDMA_TSIZE_OFFSET_0 + q_off,
3251e65e175bSOded Gabbay 							QMAN_LDMA_SIZE_OFFSET);
3252e65e175bSOded Gabbay 	WREG32(mmNIC0_QM0_CP_LDMA_SRC_BASE_LO_OFFSET_0 + q_off,
3253e65e175bSOded Gabbay 							QMAN_LDMA_SRC_OFFSET);
3254e65e175bSOded Gabbay 	WREG32(mmNIC0_QM0_CP_LDMA_DST_BASE_LO_OFFSET_0 + q_off,
3255e65e175bSOded Gabbay 							QMAN_LDMA_DST_OFFSET);
3256e65e175bSOded Gabbay 
3257e65e175bSOded Gabbay 	WREG32(mmNIC0_QM0_CP_MSG_BASE0_ADDR_LO_0 + q_off, mtr_base_en_lo);
3258e65e175bSOded Gabbay 	WREG32(mmNIC0_QM0_CP_MSG_BASE0_ADDR_HI_0 + q_off, mtr_base_en_hi);
3259e65e175bSOded Gabbay 	WREG32(mmNIC0_QM0_CP_MSG_BASE1_ADDR_LO_0 + q_off, so_base_en_lo);
3260e65e175bSOded Gabbay 	WREG32(mmNIC0_QM0_CP_MSG_BASE1_ADDR_HI_0 + q_off, so_base_en_hi);
3261e65e175bSOded Gabbay 
3262e65e175bSOded Gabbay 	/* Configure NIC CP_MSG_BASE 2/3 for sync stream collective */
3263e65e175bSOded Gabbay 	WREG32(mmNIC0_QM0_CP_MSG_BASE2_ADDR_LO_0 + q_off, mtr_base_ws_lo);
3264e65e175bSOded Gabbay 	WREG32(mmNIC0_QM0_CP_MSG_BASE2_ADDR_HI_0 + q_off, mtr_base_ws_hi);
3265e65e175bSOded Gabbay 	WREG32(mmNIC0_QM0_CP_MSG_BASE3_ADDR_LO_0 + q_off, so_base_ws_lo);
3266e65e175bSOded Gabbay 	WREG32(mmNIC0_QM0_CP_MSG_BASE3_ADDR_HI_0 + q_off, so_base_ws_hi);
3267e65e175bSOded Gabbay 
3268e65e175bSOded Gabbay 	if (qman_id == 0) {
3269e65e175bSOded Gabbay 		irq_handler_offset = hdev->asic_prop.gic_interrupts_enable ?
3270e65e175bSOded Gabbay 				mmGIC_DISTRIBUTOR__5_GICD_SETSPI_NSR :
3271e65e175bSOded Gabbay 				le32_to_cpu(dyn_regs->gic_nic_qm_irq_ctrl);
3272e65e175bSOded Gabbay 
3273e65e175bSOded Gabbay 		/* Configure RAZWI IRQ */
3274e65e175bSOded Gabbay 		nic_qm_err_cfg = NIC_QMAN_GLBL_ERR_CFG_MSG_EN_MASK;
3275e65e175bSOded Gabbay 		if (hdev->stop_on_err)
3276e65e175bSOded Gabbay 			nic_qm_err_cfg |=
3277e65e175bSOded Gabbay 				NIC_QMAN_GLBL_ERR_CFG_STOP_ON_ERR_EN_MASK;
3278e65e175bSOded Gabbay 
3279e65e175bSOded Gabbay 		WREG32(mmNIC0_QM0_GLBL_ERR_CFG + nic_offset, nic_qm_err_cfg);
3280e65e175bSOded Gabbay 
3281e65e175bSOded Gabbay 		WREG32(mmNIC0_QM0_GLBL_ERR_ADDR_LO + nic_offset,
3282e65e175bSOded Gabbay 			lower_32_bits(CFG_BASE + irq_handler_offset));
3283e65e175bSOded Gabbay 		WREG32(mmNIC0_QM0_GLBL_ERR_ADDR_HI + nic_offset,
3284e65e175bSOded Gabbay 			upper_32_bits(CFG_BASE + irq_handler_offset));
3285e65e175bSOded Gabbay 
3286e65e175bSOded Gabbay 		WREG32(mmNIC0_QM0_GLBL_ERR_WDATA + nic_offset,
3287e65e175bSOded Gabbay 			gaudi_irq_map_table[GAUDI_EVENT_NIC0_QM0].cpu_id +
3288e65e175bSOded Gabbay 									nic_id);
3289e65e175bSOded Gabbay 
3290e65e175bSOded Gabbay 		WREG32(mmNIC0_QM0_ARB_ERR_MSG_EN + nic_offset,
3291e65e175bSOded Gabbay 				QM_ARB_ERR_MSG_EN_MASK);
3292e65e175bSOded Gabbay 
3293e65e175bSOded Gabbay 		/* Set timeout to maximum */
3294e65e175bSOded Gabbay 		WREG32(mmNIC0_QM0_ARB_SLV_CHOISE_WDT + nic_offset, GAUDI_ARB_WDT_TIMEOUT);
3295e65e175bSOded Gabbay 
3296e65e175bSOded Gabbay 		WREG32(mmNIC0_QM0_GLBL_CFG1 + nic_offset, 0);
3297e65e175bSOded Gabbay 		WREG32(mmNIC0_QM0_GLBL_PROT + nic_offset,
3298e65e175bSOded Gabbay 				QMAN_INTERNAL_MAKE_TRUSTED);
3299e65e175bSOded Gabbay 	}
3300e65e175bSOded Gabbay }
3301e65e175bSOded Gabbay 
3302e65e175bSOded Gabbay static void gaudi_init_nic_qmans(struct hl_device *hdev)
3303e65e175bSOded Gabbay {
3304e65e175bSOded Gabbay 	struct gaudi_device *gaudi = hdev->asic_specific;
3305e65e175bSOded Gabbay 	struct gaudi_internal_qman_info *q;
3306e65e175bSOded Gabbay 	u64 qman_base_addr;
3307e65e175bSOded Gabbay 	u32 nic_offset = 0;
3308e65e175bSOded Gabbay 	u32 nic_delta_between_qmans =
3309e65e175bSOded Gabbay 			mmNIC0_QM1_GLBL_CFG0 - mmNIC0_QM0_GLBL_CFG0;
3310e65e175bSOded Gabbay 	u32 nic_delta_between_nics =
3311e65e175bSOded Gabbay 			mmNIC1_QM0_GLBL_CFG0 - mmNIC0_QM0_GLBL_CFG0;
3312e65e175bSOded Gabbay 	int i, nic_id, internal_q_index;
3313e65e175bSOded Gabbay 
3314e65e175bSOded Gabbay 	if (!hdev->nic_ports_mask)
3315e65e175bSOded Gabbay 		return;
3316e65e175bSOded Gabbay 
3317e65e175bSOded Gabbay 	if (gaudi->hw_cap_initialized & HW_CAP_NIC_MASK)
3318e65e175bSOded Gabbay 		return;
3319e65e175bSOded Gabbay 
3320e65e175bSOded Gabbay 	dev_dbg(hdev->dev, "Initializing NIC QMANs\n");
3321e65e175bSOded Gabbay 
3322e65e175bSOded Gabbay 	for (nic_id = 0 ; nic_id < NIC_NUMBER_OF_ENGINES ; nic_id++) {
3323e65e175bSOded Gabbay 		if (!(hdev->nic_ports_mask & (1 << nic_id))) {
3324e65e175bSOded Gabbay 			nic_offset += nic_delta_between_qmans;
3325e65e175bSOded Gabbay 			if (nic_id & 1) {
3326e65e175bSOded Gabbay 				nic_offset -= (nic_delta_between_qmans * 2);
3327e65e175bSOded Gabbay 				nic_offset += nic_delta_between_nics;
3328e65e175bSOded Gabbay 			}
3329e65e175bSOded Gabbay 			continue;
3330e65e175bSOded Gabbay 		}
3331e65e175bSOded Gabbay 
3332e65e175bSOded Gabbay 		for (i = 0 ; i < QMAN_STREAMS ; i++) {
3333e65e175bSOded Gabbay 			internal_q_index = GAUDI_QUEUE_ID_NIC_0_0 +
3334e65e175bSOded Gabbay 						nic_id * QMAN_STREAMS + i;
3335e65e175bSOded Gabbay 			q = &gaudi->internal_qmans[internal_q_index];
3336e65e175bSOded Gabbay 			qman_base_addr = (u64) q->pq_dma_addr;
3337e65e175bSOded Gabbay 			gaudi_init_nic_qman(hdev, nic_offset, (i & 0x3),
3338e65e175bSOded Gabbay 						qman_base_addr, nic_id);
3339e65e175bSOded Gabbay 		}
3340e65e175bSOded Gabbay 
3341e65e175bSOded Gabbay 		/* Enable the QMAN */
3342e65e175bSOded Gabbay 		WREG32(mmNIC0_QM0_GLBL_CFG0 + nic_offset, NIC_QMAN_ENABLE);
3343e65e175bSOded Gabbay 
3344e65e175bSOded Gabbay 		nic_offset += nic_delta_between_qmans;
3345e65e175bSOded Gabbay 		if (nic_id & 1) {
3346e65e175bSOded Gabbay 			nic_offset -= (nic_delta_between_qmans * 2);
3347e65e175bSOded Gabbay 			nic_offset += nic_delta_between_nics;
3348e65e175bSOded Gabbay 		}
3349e65e175bSOded Gabbay 
3350e65e175bSOded Gabbay 		gaudi->hw_cap_initialized |= 1 << (HW_CAP_NIC_SHIFT + nic_id);
3351e65e175bSOded Gabbay 	}
3352e65e175bSOded Gabbay }
3353e65e175bSOded Gabbay 
3354e65e175bSOded Gabbay static void gaudi_disable_pci_dma_qmans(struct hl_device *hdev)
3355e65e175bSOded Gabbay {
3356e65e175bSOded Gabbay 	struct gaudi_device *gaudi = hdev->asic_specific;
3357e65e175bSOded Gabbay 
3358e65e175bSOded Gabbay 	if (!(gaudi->hw_cap_initialized & HW_CAP_PCI_DMA))
3359e65e175bSOded Gabbay 		return;
3360e65e175bSOded Gabbay 
3361e65e175bSOded Gabbay 	WREG32(mmDMA0_QM_GLBL_CFG0, 0);
3362e65e175bSOded Gabbay 	WREG32(mmDMA1_QM_GLBL_CFG0, 0);
3363e65e175bSOded Gabbay 	WREG32(mmDMA5_QM_GLBL_CFG0, 0);
3364e65e175bSOded Gabbay }
3365e65e175bSOded Gabbay 
3366e65e175bSOded Gabbay static void gaudi_disable_hbm_dma_qmans(struct hl_device *hdev)
3367e65e175bSOded Gabbay {
3368e65e175bSOded Gabbay 	struct gaudi_device *gaudi = hdev->asic_specific;
3369e65e175bSOded Gabbay 
3370e65e175bSOded Gabbay 	if (!(gaudi->hw_cap_initialized & HW_CAP_HBM_DMA))
3371e65e175bSOded Gabbay 		return;
3372e65e175bSOded Gabbay 
3373e65e175bSOded Gabbay 	WREG32(mmDMA2_QM_GLBL_CFG0, 0);
3374e65e175bSOded Gabbay 	WREG32(mmDMA3_QM_GLBL_CFG0, 0);
3375e65e175bSOded Gabbay 	WREG32(mmDMA4_QM_GLBL_CFG0, 0);
3376e65e175bSOded Gabbay 	WREG32(mmDMA6_QM_GLBL_CFG0, 0);
3377e65e175bSOded Gabbay 	WREG32(mmDMA7_QM_GLBL_CFG0, 0);
3378e65e175bSOded Gabbay }
3379e65e175bSOded Gabbay 
3380e65e175bSOded Gabbay static void gaudi_disable_mme_qmans(struct hl_device *hdev)
3381e65e175bSOded Gabbay {
3382e65e175bSOded Gabbay 	struct gaudi_device *gaudi = hdev->asic_specific;
3383e65e175bSOded Gabbay 
3384e65e175bSOded Gabbay 	if (!(gaudi->hw_cap_initialized & HW_CAP_MME))
3385e65e175bSOded Gabbay 		return;
3386e65e175bSOded Gabbay 
3387e65e175bSOded Gabbay 	WREG32(mmMME2_QM_GLBL_CFG0, 0);
3388e65e175bSOded Gabbay 	WREG32(mmMME0_QM_GLBL_CFG0, 0);
3389e65e175bSOded Gabbay }
3390e65e175bSOded Gabbay 
3391e65e175bSOded Gabbay static void gaudi_disable_tpc_qmans(struct hl_device *hdev)
3392e65e175bSOded Gabbay {
3393e65e175bSOded Gabbay 	struct gaudi_device *gaudi = hdev->asic_specific;
3394e65e175bSOded Gabbay 	u32 tpc_offset = 0;
3395e65e175bSOded Gabbay 	int tpc_id;
3396e65e175bSOded Gabbay 
3397e65e175bSOded Gabbay 	if (!(gaudi->hw_cap_initialized & HW_CAP_TPC_MASK))
3398e65e175bSOded Gabbay 		return;
3399e65e175bSOded Gabbay 
3400e65e175bSOded Gabbay 	for (tpc_id = 0 ; tpc_id < TPC_NUMBER_OF_ENGINES ; tpc_id++) {
3401e65e175bSOded Gabbay 		WREG32(mmTPC0_QM_GLBL_CFG0 + tpc_offset, 0);
3402e65e175bSOded Gabbay 		tpc_offset += mmTPC1_QM_GLBL_CFG0 - mmTPC0_QM_GLBL_CFG0;
3403e65e175bSOded Gabbay 	}
3404e65e175bSOded Gabbay }
3405e65e175bSOded Gabbay 
3406e65e175bSOded Gabbay static void gaudi_disable_nic_qmans(struct hl_device *hdev)
3407e65e175bSOded Gabbay {
3408e65e175bSOded Gabbay 	struct gaudi_device *gaudi = hdev->asic_specific;
3409e65e175bSOded Gabbay 	u32 nic_mask, nic_offset = 0;
3410e65e175bSOded Gabbay 	u32 nic_delta_between_qmans =
3411e65e175bSOded Gabbay 			mmNIC0_QM1_GLBL_CFG0 - mmNIC0_QM0_GLBL_CFG0;
3412e65e175bSOded Gabbay 	u32 nic_delta_between_nics =
3413e65e175bSOded Gabbay 			mmNIC1_QM0_GLBL_CFG0 - mmNIC0_QM0_GLBL_CFG0;
3414e65e175bSOded Gabbay 	int nic_id;
3415e65e175bSOded Gabbay 
3416e65e175bSOded Gabbay 	for (nic_id = 0 ; nic_id < NIC_NUMBER_OF_ENGINES ; nic_id++) {
3417e65e175bSOded Gabbay 		nic_mask = 1 << (HW_CAP_NIC_SHIFT + nic_id);
3418e65e175bSOded Gabbay 
3419e65e175bSOded Gabbay 		if (gaudi->hw_cap_initialized & nic_mask)
3420e65e175bSOded Gabbay 			WREG32(mmNIC0_QM0_GLBL_CFG0 + nic_offset, 0);
3421e65e175bSOded Gabbay 
3422e65e175bSOded Gabbay 		nic_offset += nic_delta_between_qmans;
3423e65e175bSOded Gabbay 		if (nic_id & 1) {
3424e65e175bSOded Gabbay 			nic_offset -= (nic_delta_between_qmans * 2);
3425e65e175bSOded Gabbay 			nic_offset += nic_delta_between_nics;
3426e65e175bSOded Gabbay 		}
3427e65e175bSOded Gabbay 	}
3428e65e175bSOded Gabbay }
3429e65e175bSOded Gabbay 
3430e65e175bSOded Gabbay static void gaudi_stop_pci_dma_qmans(struct hl_device *hdev)
3431e65e175bSOded Gabbay {
3432e65e175bSOded Gabbay 	struct gaudi_device *gaudi = hdev->asic_specific;
3433e65e175bSOded Gabbay 
3434e65e175bSOded Gabbay 	if (!(gaudi->hw_cap_initialized & HW_CAP_PCI_DMA))
3435e65e175bSOded Gabbay 		return;
3436e65e175bSOded Gabbay 
3437e65e175bSOded Gabbay 	/* Stop upper CPs of QMANs 0.0 to 1.3 and 5.0 to 5.3 */
3438e65e175bSOded Gabbay 	WREG32(mmDMA0_QM_GLBL_CFG1, 0xF << DMA0_QM_GLBL_CFG1_CP_STOP_SHIFT);
3439e65e175bSOded Gabbay 	WREG32(mmDMA1_QM_GLBL_CFG1, 0xF << DMA0_QM_GLBL_CFG1_CP_STOP_SHIFT);
3440e65e175bSOded Gabbay 	WREG32(mmDMA5_QM_GLBL_CFG1, 0xF << DMA0_QM_GLBL_CFG1_CP_STOP_SHIFT);
3441e65e175bSOded Gabbay }
3442e65e175bSOded Gabbay 
3443e65e175bSOded Gabbay static void gaudi_stop_hbm_dma_qmans(struct hl_device *hdev)
3444e65e175bSOded Gabbay {
3445e65e175bSOded Gabbay 	struct gaudi_device *gaudi = hdev->asic_specific;
3446e65e175bSOded Gabbay 
3447e65e175bSOded Gabbay 	if (!(gaudi->hw_cap_initialized & HW_CAP_HBM_DMA))
3448e65e175bSOded Gabbay 		return;
3449e65e175bSOded Gabbay 
3450e65e175bSOded Gabbay 	/* Stop CPs of HBM DMA QMANs */
3451e65e175bSOded Gabbay 
3452e65e175bSOded Gabbay 	WREG32(mmDMA2_QM_GLBL_CFG1, 0x1F << DMA0_QM_GLBL_CFG1_CP_STOP_SHIFT);
3453e65e175bSOded Gabbay 	WREG32(mmDMA3_QM_GLBL_CFG1, 0x1F << DMA0_QM_GLBL_CFG1_CP_STOP_SHIFT);
3454e65e175bSOded Gabbay 	WREG32(mmDMA4_QM_GLBL_CFG1, 0x1F << DMA0_QM_GLBL_CFG1_CP_STOP_SHIFT);
3455e65e175bSOded Gabbay 	WREG32(mmDMA6_QM_GLBL_CFG1, 0x1F << DMA0_QM_GLBL_CFG1_CP_STOP_SHIFT);
3456e65e175bSOded Gabbay 	WREG32(mmDMA7_QM_GLBL_CFG1, 0x1F << DMA0_QM_GLBL_CFG1_CP_STOP_SHIFT);
3457e65e175bSOded Gabbay }
3458e65e175bSOded Gabbay 
3459e65e175bSOded Gabbay static void gaudi_stop_mme_qmans(struct hl_device *hdev)
3460e65e175bSOded Gabbay {
3461e65e175bSOded Gabbay 	struct gaudi_device *gaudi = hdev->asic_specific;
3462e65e175bSOded Gabbay 
3463e65e175bSOded Gabbay 	if (!(gaudi->hw_cap_initialized & HW_CAP_MME))
3464e65e175bSOded Gabbay 		return;
3465e65e175bSOded Gabbay 
3466e65e175bSOded Gabbay 	/* Stop CPs of MME QMANs */
3467e65e175bSOded Gabbay 	WREG32(mmMME2_QM_GLBL_CFG1, 0x1F << MME0_QM_GLBL_CFG1_CP_STOP_SHIFT);
3468e65e175bSOded Gabbay 	WREG32(mmMME0_QM_GLBL_CFG1, 0x1F << MME0_QM_GLBL_CFG1_CP_STOP_SHIFT);
3469e65e175bSOded Gabbay }
3470e65e175bSOded Gabbay 
3471e65e175bSOded Gabbay static void gaudi_stop_tpc_qmans(struct hl_device *hdev)
3472e65e175bSOded Gabbay {
3473e65e175bSOded Gabbay 	struct gaudi_device *gaudi = hdev->asic_specific;
3474e65e175bSOded Gabbay 
3475e65e175bSOded Gabbay 	if (!(gaudi->hw_cap_initialized & HW_CAP_TPC_MASK))
3476e65e175bSOded Gabbay 		return;
3477e65e175bSOded Gabbay 
3478e65e175bSOded Gabbay 	WREG32(mmTPC0_QM_GLBL_CFG1, 0x1F << TPC0_QM_GLBL_CFG1_CP_STOP_SHIFT);
3479e65e175bSOded Gabbay 	WREG32(mmTPC1_QM_GLBL_CFG1, 0x1F << TPC0_QM_GLBL_CFG1_CP_STOP_SHIFT);
3480e65e175bSOded Gabbay 	WREG32(mmTPC2_QM_GLBL_CFG1, 0x1F << TPC0_QM_GLBL_CFG1_CP_STOP_SHIFT);
3481e65e175bSOded Gabbay 	WREG32(mmTPC3_QM_GLBL_CFG1, 0x1F << TPC0_QM_GLBL_CFG1_CP_STOP_SHIFT);
3482e65e175bSOded Gabbay 	WREG32(mmTPC4_QM_GLBL_CFG1, 0x1F << TPC0_QM_GLBL_CFG1_CP_STOP_SHIFT);
3483e65e175bSOded Gabbay 	WREG32(mmTPC5_QM_GLBL_CFG1, 0x1F << TPC0_QM_GLBL_CFG1_CP_STOP_SHIFT);
3484e65e175bSOded Gabbay 	WREG32(mmTPC6_QM_GLBL_CFG1, 0x1F << TPC0_QM_GLBL_CFG1_CP_STOP_SHIFT);
3485e65e175bSOded Gabbay 	WREG32(mmTPC7_QM_GLBL_CFG1, 0x1F << TPC0_QM_GLBL_CFG1_CP_STOP_SHIFT);
3486e65e175bSOded Gabbay }
3487e65e175bSOded Gabbay 
3488e65e175bSOded Gabbay static void gaudi_stop_nic_qmans(struct hl_device *hdev)
3489e65e175bSOded Gabbay {
3490e65e175bSOded Gabbay 	struct gaudi_device *gaudi = hdev->asic_specific;
3491e65e175bSOded Gabbay 
3492e65e175bSOded Gabbay 	/* Stop upper CPs of QMANs */
3493e65e175bSOded Gabbay 
3494e65e175bSOded Gabbay 	if (gaudi->hw_cap_initialized & HW_CAP_NIC0)
3495e65e175bSOded Gabbay 		WREG32(mmNIC0_QM0_GLBL_CFG1,
3496e65e175bSOded Gabbay 				NIC0_QM0_GLBL_CFG1_PQF_STOP_MASK |
3497e65e175bSOded Gabbay 				NIC0_QM0_GLBL_CFG1_CQF_STOP_MASK |
3498e65e175bSOded Gabbay 				NIC0_QM0_GLBL_CFG1_CP_STOP_MASK);
3499e65e175bSOded Gabbay 
3500e65e175bSOded Gabbay 	if (gaudi->hw_cap_initialized & HW_CAP_NIC1)
3501e65e175bSOded Gabbay 		WREG32(mmNIC0_QM1_GLBL_CFG1,
3502e65e175bSOded Gabbay 				NIC0_QM0_GLBL_CFG1_PQF_STOP_MASK |
3503e65e175bSOded Gabbay 				NIC0_QM0_GLBL_CFG1_CQF_STOP_MASK |
3504e65e175bSOded Gabbay 				NIC0_QM0_GLBL_CFG1_CP_STOP_MASK);
3505e65e175bSOded Gabbay 
3506e65e175bSOded Gabbay 	if (gaudi->hw_cap_initialized & HW_CAP_NIC2)
3507e65e175bSOded Gabbay 		WREG32(mmNIC1_QM0_GLBL_CFG1,
3508e65e175bSOded Gabbay 				NIC0_QM0_GLBL_CFG1_PQF_STOP_MASK |
3509e65e175bSOded Gabbay 				NIC0_QM0_GLBL_CFG1_CQF_STOP_MASK |
3510e65e175bSOded Gabbay 				NIC0_QM0_GLBL_CFG1_CP_STOP_MASK);
3511e65e175bSOded Gabbay 
3512e65e175bSOded Gabbay 	if (gaudi->hw_cap_initialized & HW_CAP_NIC3)
3513e65e175bSOded Gabbay 		WREG32(mmNIC1_QM1_GLBL_CFG1,
3514e65e175bSOded Gabbay 				NIC0_QM0_GLBL_CFG1_PQF_STOP_MASK |
3515e65e175bSOded Gabbay 				NIC0_QM0_GLBL_CFG1_CQF_STOP_MASK |
3516e65e175bSOded Gabbay 				NIC0_QM0_GLBL_CFG1_CP_STOP_MASK);
3517e65e175bSOded Gabbay 
3518e65e175bSOded Gabbay 	if (gaudi->hw_cap_initialized & HW_CAP_NIC4)
3519e65e175bSOded Gabbay 		WREG32(mmNIC2_QM0_GLBL_CFG1,
3520e65e175bSOded Gabbay 				NIC0_QM0_GLBL_CFG1_PQF_STOP_MASK |
3521e65e175bSOded Gabbay 				NIC0_QM0_GLBL_CFG1_CQF_STOP_MASK |
3522e65e175bSOded Gabbay 				NIC0_QM0_GLBL_CFG1_CP_STOP_MASK);
3523e65e175bSOded Gabbay 
3524e65e175bSOded Gabbay 	if (gaudi->hw_cap_initialized & HW_CAP_NIC5)
3525e65e175bSOded Gabbay 		WREG32(mmNIC2_QM1_GLBL_CFG1,
3526e65e175bSOded Gabbay 				NIC0_QM0_GLBL_CFG1_PQF_STOP_MASK |
3527e65e175bSOded Gabbay 				NIC0_QM0_GLBL_CFG1_CQF_STOP_MASK |
3528e65e175bSOded Gabbay 				NIC0_QM0_GLBL_CFG1_CP_STOP_MASK);
3529e65e175bSOded Gabbay 
3530e65e175bSOded Gabbay 	if (gaudi->hw_cap_initialized & HW_CAP_NIC6)
3531e65e175bSOded Gabbay 		WREG32(mmNIC3_QM0_GLBL_CFG1,
3532e65e175bSOded Gabbay 				NIC0_QM0_GLBL_CFG1_PQF_STOP_MASK |
3533e65e175bSOded Gabbay 				NIC0_QM0_GLBL_CFG1_CQF_STOP_MASK |
3534e65e175bSOded Gabbay 				NIC0_QM0_GLBL_CFG1_CP_STOP_MASK);
3535e65e175bSOded Gabbay 
3536e65e175bSOded Gabbay 	if (gaudi->hw_cap_initialized & HW_CAP_NIC7)
3537e65e175bSOded Gabbay 		WREG32(mmNIC3_QM1_GLBL_CFG1,
3538e65e175bSOded Gabbay 				NIC0_QM0_GLBL_CFG1_PQF_STOP_MASK |
3539e65e175bSOded Gabbay 				NIC0_QM0_GLBL_CFG1_CQF_STOP_MASK |
3540e65e175bSOded Gabbay 				NIC0_QM0_GLBL_CFG1_CP_STOP_MASK);
3541e65e175bSOded Gabbay 
3542e65e175bSOded Gabbay 	if (gaudi->hw_cap_initialized & HW_CAP_NIC8)
3543e65e175bSOded Gabbay 		WREG32(mmNIC4_QM0_GLBL_CFG1,
3544e65e175bSOded Gabbay 				NIC0_QM0_GLBL_CFG1_PQF_STOP_MASK |
3545e65e175bSOded Gabbay 				NIC0_QM0_GLBL_CFG1_CQF_STOP_MASK |
3546e65e175bSOded Gabbay 				NIC0_QM0_GLBL_CFG1_CP_STOP_MASK);
3547e65e175bSOded Gabbay 
3548e65e175bSOded Gabbay 	if (gaudi->hw_cap_initialized & HW_CAP_NIC9)
3549e65e175bSOded Gabbay 		WREG32(mmNIC4_QM1_GLBL_CFG1,
3550e65e175bSOded Gabbay 				NIC0_QM0_GLBL_CFG1_PQF_STOP_MASK |
3551e65e175bSOded Gabbay 				NIC0_QM0_GLBL_CFG1_CQF_STOP_MASK |
3552e65e175bSOded Gabbay 				NIC0_QM0_GLBL_CFG1_CP_STOP_MASK);
3553e65e175bSOded Gabbay }
3554e65e175bSOded Gabbay 
3555e65e175bSOded Gabbay static void gaudi_pci_dma_stall(struct hl_device *hdev)
3556e65e175bSOded Gabbay {
3557e65e175bSOded Gabbay 	struct gaudi_device *gaudi = hdev->asic_specific;
3558e65e175bSOded Gabbay 
3559e65e175bSOded Gabbay 	if (!(gaudi->hw_cap_initialized & HW_CAP_PCI_DMA))
3560e65e175bSOded Gabbay 		return;
3561e65e175bSOded Gabbay 
3562e65e175bSOded Gabbay 	WREG32(mmDMA0_CORE_CFG_1, 1 << DMA0_CORE_CFG_1_HALT_SHIFT);
3563e65e175bSOded Gabbay 	WREG32(mmDMA1_CORE_CFG_1, 1 << DMA0_CORE_CFG_1_HALT_SHIFT);
3564e65e175bSOded Gabbay 	WREG32(mmDMA5_CORE_CFG_1, 1 << DMA0_CORE_CFG_1_HALT_SHIFT);
3565e65e175bSOded Gabbay }
3566e65e175bSOded Gabbay 
3567e65e175bSOded Gabbay static void gaudi_hbm_dma_stall(struct hl_device *hdev)
3568e65e175bSOded Gabbay {
3569e65e175bSOded Gabbay 	struct gaudi_device *gaudi = hdev->asic_specific;
3570e65e175bSOded Gabbay 
3571e65e175bSOded Gabbay 	if (!(gaudi->hw_cap_initialized & HW_CAP_HBM_DMA))
3572e65e175bSOded Gabbay 		return;
3573e65e175bSOded Gabbay 
3574e65e175bSOded Gabbay 	WREG32(mmDMA2_CORE_CFG_1, 1 << DMA0_CORE_CFG_1_HALT_SHIFT);
3575e65e175bSOded Gabbay 	WREG32(mmDMA3_CORE_CFG_1, 1 << DMA0_CORE_CFG_1_HALT_SHIFT);
3576e65e175bSOded Gabbay 	WREG32(mmDMA4_CORE_CFG_1, 1 << DMA0_CORE_CFG_1_HALT_SHIFT);
3577e65e175bSOded Gabbay 	WREG32(mmDMA6_CORE_CFG_1, 1 << DMA0_CORE_CFG_1_HALT_SHIFT);
3578e65e175bSOded Gabbay 	WREG32(mmDMA7_CORE_CFG_1, 1 << DMA0_CORE_CFG_1_HALT_SHIFT);
3579e65e175bSOded Gabbay }
3580e65e175bSOded Gabbay 
3581e65e175bSOded Gabbay static void gaudi_mme_stall(struct hl_device *hdev)
3582e65e175bSOded Gabbay {
3583e65e175bSOded Gabbay 	struct gaudi_device *gaudi = hdev->asic_specific;
3584e65e175bSOded Gabbay 
3585e65e175bSOded Gabbay 	if (!(gaudi->hw_cap_initialized & HW_CAP_MME))
3586e65e175bSOded Gabbay 		return;
3587e65e175bSOded Gabbay 
3588e65e175bSOded Gabbay 	/* WA for H3-1800 bug: do ACC and SBAB writes twice */
3589e65e175bSOded Gabbay 	WREG32(mmMME0_ACC_ACC_STALL, 1 << MME_ACC_ACC_STALL_R_SHIFT);
3590e65e175bSOded Gabbay 	WREG32(mmMME0_ACC_ACC_STALL, 1 << MME_ACC_ACC_STALL_R_SHIFT);
3591e65e175bSOded Gabbay 	WREG32(mmMME0_SBAB_SB_STALL, 1 << MME_SBAB_SB_STALL_R_SHIFT);
3592e65e175bSOded Gabbay 	WREG32(mmMME0_SBAB_SB_STALL, 1 << MME_SBAB_SB_STALL_R_SHIFT);
3593e65e175bSOded Gabbay 	WREG32(mmMME1_ACC_ACC_STALL, 1 << MME_ACC_ACC_STALL_R_SHIFT);
3594e65e175bSOded Gabbay 	WREG32(mmMME1_ACC_ACC_STALL, 1 << MME_ACC_ACC_STALL_R_SHIFT);
3595e65e175bSOded Gabbay 	WREG32(mmMME1_SBAB_SB_STALL, 1 << MME_SBAB_SB_STALL_R_SHIFT);
3596e65e175bSOded Gabbay 	WREG32(mmMME1_SBAB_SB_STALL, 1 << MME_SBAB_SB_STALL_R_SHIFT);
3597e65e175bSOded Gabbay 	WREG32(mmMME2_ACC_ACC_STALL, 1 << MME_ACC_ACC_STALL_R_SHIFT);
3598e65e175bSOded Gabbay 	WREG32(mmMME2_ACC_ACC_STALL, 1 << MME_ACC_ACC_STALL_R_SHIFT);
3599e65e175bSOded Gabbay 	WREG32(mmMME2_SBAB_SB_STALL, 1 << MME_SBAB_SB_STALL_R_SHIFT);
3600e65e175bSOded Gabbay 	WREG32(mmMME2_SBAB_SB_STALL, 1 << MME_SBAB_SB_STALL_R_SHIFT);
3601e65e175bSOded Gabbay 	WREG32(mmMME3_ACC_ACC_STALL, 1 << MME_ACC_ACC_STALL_R_SHIFT);
3602e65e175bSOded Gabbay 	WREG32(mmMME3_ACC_ACC_STALL, 1 << MME_ACC_ACC_STALL_R_SHIFT);
3603e65e175bSOded Gabbay 	WREG32(mmMME3_SBAB_SB_STALL, 1 << MME_SBAB_SB_STALL_R_SHIFT);
3604e65e175bSOded Gabbay 	WREG32(mmMME3_SBAB_SB_STALL, 1 << MME_SBAB_SB_STALL_R_SHIFT);
3605e65e175bSOded Gabbay }
3606e65e175bSOded Gabbay 
3607e65e175bSOded Gabbay static void gaudi_tpc_stall(struct hl_device *hdev)
3608e65e175bSOded Gabbay {
3609e65e175bSOded Gabbay 	struct gaudi_device *gaudi = hdev->asic_specific;
3610e65e175bSOded Gabbay 
3611e65e175bSOded Gabbay 	if (!(gaudi->hw_cap_initialized & HW_CAP_TPC_MASK))
3612e65e175bSOded Gabbay 		return;
3613e65e175bSOded Gabbay 
3614e65e175bSOded Gabbay 	WREG32(mmTPC0_CFG_TPC_STALL, 1 << TPC0_CFG_TPC_STALL_V_SHIFT);
3615e65e175bSOded Gabbay 	WREG32(mmTPC1_CFG_TPC_STALL, 1 << TPC0_CFG_TPC_STALL_V_SHIFT);
3616e65e175bSOded Gabbay 	WREG32(mmTPC2_CFG_TPC_STALL, 1 << TPC0_CFG_TPC_STALL_V_SHIFT);
3617e65e175bSOded Gabbay 	WREG32(mmTPC3_CFG_TPC_STALL, 1 << TPC0_CFG_TPC_STALL_V_SHIFT);
3618e65e175bSOded Gabbay 	WREG32(mmTPC4_CFG_TPC_STALL, 1 << TPC0_CFG_TPC_STALL_V_SHIFT);
3619e65e175bSOded Gabbay 	WREG32(mmTPC5_CFG_TPC_STALL, 1 << TPC0_CFG_TPC_STALL_V_SHIFT);
3620e65e175bSOded Gabbay 	WREG32(mmTPC6_CFG_TPC_STALL, 1 << TPC0_CFG_TPC_STALL_V_SHIFT);
3621e65e175bSOded Gabbay 	WREG32(mmTPC7_CFG_TPC_STALL, 1 << TPC0_CFG_TPC_STALL_V_SHIFT);
3622e65e175bSOded Gabbay }
3623e65e175bSOded Gabbay 
3624e65e175bSOded Gabbay static void gaudi_disable_clock_gating(struct hl_device *hdev)
3625e65e175bSOded Gabbay {
3626e65e175bSOded Gabbay 	u32 qman_offset;
3627e65e175bSOded Gabbay 	int i;
3628e65e175bSOded Gabbay 
3629e65e175bSOded Gabbay 	if (hdev->asic_prop.fw_security_enabled)
3630e65e175bSOded Gabbay 		return;
3631e65e175bSOded Gabbay 
3632e65e175bSOded Gabbay 	for (i = 0, qman_offset = 0 ; i < DMA_NUMBER_OF_CHANNELS ; i++) {
3633e65e175bSOded Gabbay 		WREG32(mmDMA0_QM_CGM_CFG + qman_offset, 0);
3634e65e175bSOded Gabbay 		WREG32(mmDMA0_QM_CGM_CFG1 + qman_offset, 0);
3635e65e175bSOded Gabbay 
3636e65e175bSOded Gabbay 		qman_offset += (mmDMA1_QM_CGM_CFG - mmDMA0_QM_CGM_CFG);
3637e65e175bSOded Gabbay 	}
3638e65e175bSOded Gabbay 
3639e65e175bSOded Gabbay 	WREG32(mmMME0_QM_CGM_CFG, 0);
3640e65e175bSOded Gabbay 	WREG32(mmMME0_QM_CGM_CFG1, 0);
3641e65e175bSOded Gabbay 	WREG32(mmMME2_QM_CGM_CFG, 0);
3642e65e175bSOded Gabbay 	WREG32(mmMME2_QM_CGM_CFG1, 0);
3643e65e175bSOded Gabbay 
3644e65e175bSOded Gabbay 	for (i = 0, qman_offset = 0 ; i < TPC_NUMBER_OF_ENGINES ; i++) {
3645e65e175bSOded Gabbay 		WREG32(mmTPC0_QM_CGM_CFG + qman_offset, 0);
3646e65e175bSOded Gabbay 		WREG32(mmTPC0_QM_CGM_CFG1 + qman_offset, 0);
3647e65e175bSOded Gabbay 
3648e65e175bSOded Gabbay 		qman_offset += (mmTPC1_QM_CGM_CFG - mmTPC0_QM_CGM_CFG);
3649e65e175bSOded Gabbay 	}
3650e65e175bSOded Gabbay }
3651e65e175bSOded Gabbay 
3652e65e175bSOded Gabbay static void gaudi_enable_timestamp(struct hl_device *hdev)
3653e65e175bSOded Gabbay {
3654e65e175bSOded Gabbay 	/* Disable the timestamp counter */
3655e65e175bSOded Gabbay 	WREG32(mmPSOC_TIMESTAMP_BASE - CFG_BASE, 0);
3656e65e175bSOded Gabbay 
3657e65e175bSOded Gabbay 	/* Zero the lower/upper parts of the 64-bit counter */
3658e65e175bSOded Gabbay 	WREG32(mmPSOC_TIMESTAMP_BASE - CFG_BASE + 0xC, 0);
3659e65e175bSOded Gabbay 	WREG32(mmPSOC_TIMESTAMP_BASE - CFG_BASE + 0x8, 0);
3660e65e175bSOded Gabbay 
3661e65e175bSOded Gabbay 	/* Enable the counter */
3662e65e175bSOded Gabbay 	WREG32(mmPSOC_TIMESTAMP_BASE - CFG_BASE, 1);
3663e65e175bSOded Gabbay }
3664e65e175bSOded Gabbay 
3665e65e175bSOded Gabbay static void gaudi_disable_timestamp(struct hl_device *hdev)
3666e65e175bSOded Gabbay {
3667e65e175bSOded Gabbay 	/* Disable the timestamp counter */
3668e65e175bSOded Gabbay 	WREG32(mmPSOC_TIMESTAMP_BASE - CFG_BASE, 0);
3669e65e175bSOded Gabbay }
3670e65e175bSOded Gabbay 
3671e65e175bSOded Gabbay static void gaudi_halt_engines(struct hl_device *hdev, bool hard_reset, bool fw_reset)
3672e65e175bSOded Gabbay {
3673e65e175bSOded Gabbay 	u32 wait_timeout_ms;
3674e65e175bSOded Gabbay 
3675e65e175bSOded Gabbay 	if (hdev->pldm)
3676e65e175bSOded Gabbay 		wait_timeout_ms = GAUDI_PLDM_RESET_WAIT_MSEC;
3677e65e175bSOded Gabbay 	else
3678e65e175bSOded Gabbay 		wait_timeout_ms = GAUDI_RESET_WAIT_MSEC;
3679e65e175bSOded Gabbay 
3680e65e175bSOded Gabbay 	if (fw_reset)
3681e65e175bSOded Gabbay 		goto skip_engines;
3682e65e175bSOded Gabbay 
3683e65e175bSOded Gabbay 	gaudi_stop_nic_qmans(hdev);
3684e65e175bSOded Gabbay 	gaudi_stop_mme_qmans(hdev);
3685e65e175bSOded Gabbay 	gaudi_stop_tpc_qmans(hdev);
3686e65e175bSOded Gabbay 	gaudi_stop_hbm_dma_qmans(hdev);
3687e65e175bSOded Gabbay 	gaudi_stop_pci_dma_qmans(hdev);
3688e65e175bSOded Gabbay 
3689e65e175bSOded Gabbay 	msleep(wait_timeout_ms);
3690e65e175bSOded Gabbay 
3691e65e175bSOded Gabbay 	gaudi_pci_dma_stall(hdev);
3692e65e175bSOded Gabbay 	gaudi_hbm_dma_stall(hdev);
3693e65e175bSOded Gabbay 	gaudi_tpc_stall(hdev);
3694e65e175bSOded Gabbay 	gaudi_mme_stall(hdev);
3695e65e175bSOded Gabbay 
3696e65e175bSOded Gabbay 	msleep(wait_timeout_ms);
3697e65e175bSOded Gabbay 
3698e65e175bSOded Gabbay 	gaudi_disable_nic_qmans(hdev);
3699e65e175bSOded Gabbay 	gaudi_disable_mme_qmans(hdev);
3700e65e175bSOded Gabbay 	gaudi_disable_tpc_qmans(hdev);
3701e65e175bSOded Gabbay 	gaudi_disable_hbm_dma_qmans(hdev);
3702e65e175bSOded Gabbay 	gaudi_disable_pci_dma_qmans(hdev);
3703e65e175bSOded Gabbay 
3704e65e175bSOded Gabbay 	gaudi_disable_timestamp(hdev);
3705e65e175bSOded Gabbay 
3706e65e175bSOded Gabbay skip_engines:
3707e65e175bSOded Gabbay 	gaudi_disable_msi(hdev);
3708e65e175bSOded Gabbay }
3709e65e175bSOded Gabbay 
3710e65e175bSOded Gabbay static int gaudi_mmu_init(struct hl_device *hdev)
3711e65e175bSOded Gabbay {
3712e65e175bSOded Gabbay 	struct asic_fixed_properties *prop = &hdev->asic_prop;
3713e65e175bSOded Gabbay 	struct gaudi_device *gaudi = hdev->asic_specific;
3714e65e175bSOded Gabbay 	u64 hop0_addr;
3715e65e175bSOded Gabbay 	int rc, i;
3716e65e175bSOded Gabbay 
3717e65e175bSOded Gabbay 	if (!hdev->mmu_enable)
3718e65e175bSOded Gabbay 		return 0;
3719e65e175bSOded Gabbay 
3720e65e175bSOded Gabbay 	if (gaudi->hw_cap_initialized & HW_CAP_MMU)
3721e65e175bSOded Gabbay 		return 0;
3722e65e175bSOded Gabbay 
3723e65e175bSOded Gabbay 	for (i = 0 ; i < prop->max_asid ; i++) {
3724e65e175bSOded Gabbay 		hop0_addr = prop->mmu_pgt_addr +
3725e65e175bSOded Gabbay 				(i * prop->mmu_hop_table_size);
3726e65e175bSOded Gabbay 
3727e65e175bSOded Gabbay 		rc = gaudi_mmu_update_asid_hop0_addr(hdev, i, hop0_addr);
3728e65e175bSOded Gabbay 		if (rc) {
3729e65e175bSOded Gabbay 			dev_err(hdev->dev,
3730e65e175bSOded Gabbay 				"failed to set hop0 addr for asid %d\n", i);
3731af5e675fSKoby Elbaz 			return rc;
3732e65e175bSOded Gabbay 		}
3733e65e175bSOded Gabbay 	}
3734e65e175bSOded Gabbay 
3735e65e175bSOded Gabbay 	/* init MMU cache manage page */
3736e65e175bSOded Gabbay 	WREG32(mmSTLB_CACHE_INV_BASE_39_8, prop->mmu_cache_mng_addr >> 8);
3737e65e175bSOded Gabbay 	WREG32(mmSTLB_CACHE_INV_BASE_49_40, prop->mmu_cache_mng_addr >> 40);
3738e65e175bSOded Gabbay 
3739e65e175bSOded Gabbay 	/* mem cache invalidation */
3740e65e175bSOded Gabbay 	WREG32(mmSTLB_MEM_CACHE_INVALIDATION, 1);
3741e65e175bSOded Gabbay 
3742af5e675fSKoby Elbaz 	rc = hl_mmu_invalidate_cache(hdev, true, 0);
3743af5e675fSKoby Elbaz 	if (rc)
3744af5e675fSKoby Elbaz 		return rc;
3745e65e175bSOded Gabbay 
3746e65e175bSOded Gabbay 	WREG32(mmMMU_UP_MMU_ENABLE, 1);
3747e65e175bSOded Gabbay 	WREG32(mmMMU_UP_SPI_MASK, 0xF);
3748e65e175bSOded Gabbay 
3749e65e175bSOded Gabbay 	WREG32(mmSTLB_HOP_CONFIGURATION, 0x30440);
3750e65e175bSOded Gabbay 
3751e65e175bSOded Gabbay 	/*
3752e65e175bSOded Gabbay 	 * The H/W expects the first PI after init to be 1. After wraparound
3753e65e175bSOded Gabbay 	 * we'll write 0.
3754e65e175bSOded Gabbay 	 */
3755e65e175bSOded Gabbay 	gaudi->mmu_cache_inv_pi = 1;
3756e65e175bSOded Gabbay 
3757e65e175bSOded Gabbay 	gaudi->hw_cap_initialized |= HW_CAP_MMU;
3758e65e175bSOded Gabbay 
3759e65e175bSOded Gabbay 	return 0;
3760e65e175bSOded Gabbay }
3761e65e175bSOded Gabbay 
3762e65e175bSOded Gabbay static int gaudi_load_firmware_to_device(struct hl_device *hdev)
3763e65e175bSOded Gabbay {
3764e65e175bSOded Gabbay 	void __iomem *dst;
3765e65e175bSOded Gabbay 
3766e65e175bSOded Gabbay 	dst = hdev->pcie_bar[HBM_BAR_ID] + LINUX_FW_OFFSET;
3767e65e175bSOded Gabbay 
3768e65e175bSOded Gabbay 	return hl_fw_load_fw_to_device(hdev, GAUDI_LINUX_FW_FILE, dst, 0, 0);
3769e65e175bSOded Gabbay }
3770e65e175bSOded Gabbay 
3771e65e175bSOded Gabbay static int gaudi_load_boot_fit_to_device(struct hl_device *hdev)
3772e65e175bSOded Gabbay {
3773e65e175bSOded Gabbay 	void __iomem *dst;
3774e65e175bSOded Gabbay 
3775e65e175bSOded Gabbay 	dst = hdev->pcie_bar[SRAM_BAR_ID] + BOOT_FIT_SRAM_OFFSET;
3776e65e175bSOded Gabbay 
3777e65e175bSOded Gabbay 	return hl_fw_load_fw_to_device(hdev, GAUDI_BOOT_FIT_FILE, dst, 0, 0);
3778e65e175bSOded Gabbay }
3779e65e175bSOded Gabbay 
3780e65e175bSOded Gabbay static void gaudi_init_dynamic_firmware_loader(struct hl_device *hdev)
3781e65e175bSOded Gabbay {
3782e65e175bSOded Gabbay 	struct dynamic_fw_load_mgr *dynamic_loader;
3783e65e175bSOded Gabbay 	struct cpu_dyn_regs *dyn_regs;
3784e65e175bSOded Gabbay 
3785e65e175bSOded Gabbay 	dynamic_loader = &hdev->fw_loader.dynamic_loader;
3786e65e175bSOded Gabbay 
3787e65e175bSOded Gabbay 	/*
3788e65e175bSOded Gabbay 	 * here we update initial values for few specific dynamic regs (as
3789e65e175bSOded Gabbay 	 * before reading the first descriptor from FW those value has to be
3790e65e175bSOded Gabbay 	 * hard-coded) in later stages of the protocol those values will be
3791e65e175bSOded Gabbay 	 * updated automatically by reading the FW descriptor so data there
3792e65e175bSOded Gabbay 	 * will always be up-to-date
3793e65e175bSOded Gabbay 	 */
3794e65e175bSOded Gabbay 	dyn_regs = &dynamic_loader->comm_desc.cpu_dyn_regs;
3795e65e175bSOded Gabbay 	dyn_regs->kmd_msg_to_cpu =
3796e65e175bSOded Gabbay 				cpu_to_le32(mmPSOC_GLOBAL_CONF_KMD_MSG_TO_CPU);
3797e65e175bSOded Gabbay 	dyn_regs->cpu_cmd_status_to_host =
3798e65e175bSOded Gabbay 				cpu_to_le32(mmCPU_CMD_STATUS_TO_HOST);
3799e65e175bSOded Gabbay 
3800e65e175bSOded Gabbay 	dynamic_loader->wait_for_bl_timeout = GAUDI_WAIT_FOR_BL_TIMEOUT_USEC;
3801e65e175bSOded Gabbay }
3802e65e175bSOded Gabbay 
3803e65e175bSOded Gabbay static void gaudi_init_static_firmware_loader(struct hl_device *hdev)
3804e65e175bSOded Gabbay {
3805e65e175bSOded Gabbay 	struct static_fw_load_mgr *static_loader;
3806e65e175bSOded Gabbay 
3807e65e175bSOded Gabbay 	static_loader = &hdev->fw_loader.static_loader;
3808e65e175bSOded Gabbay 
3809e65e175bSOded Gabbay 	static_loader->preboot_version_max_off = SRAM_SIZE - VERSION_MAX_LEN;
3810e65e175bSOded Gabbay 	static_loader->boot_fit_version_max_off = SRAM_SIZE - VERSION_MAX_LEN;
3811e65e175bSOded Gabbay 	static_loader->kmd_msg_to_cpu_reg = mmPSOC_GLOBAL_CONF_KMD_MSG_TO_CPU;
3812e65e175bSOded Gabbay 	static_loader->cpu_cmd_status_to_host_reg = mmCPU_CMD_STATUS_TO_HOST;
3813e65e175bSOded Gabbay 	static_loader->cpu_boot_status_reg = mmPSOC_GLOBAL_CONF_CPU_BOOT_STATUS;
3814e65e175bSOded Gabbay 	static_loader->cpu_boot_dev_status0_reg = mmCPU_BOOT_DEV_STS0;
3815e65e175bSOded Gabbay 	static_loader->cpu_boot_dev_status1_reg = mmCPU_BOOT_DEV_STS1;
3816e65e175bSOded Gabbay 	static_loader->boot_err0_reg = mmCPU_BOOT_ERR0;
3817e65e175bSOded Gabbay 	static_loader->boot_err1_reg = mmCPU_BOOT_ERR1;
3818e65e175bSOded Gabbay 	static_loader->preboot_version_offset_reg = mmPREBOOT_VER_OFFSET;
3819e65e175bSOded Gabbay 	static_loader->boot_fit_version_offset_reg = mmUBOOT_VER_OFFSET;
3820e65e175bSOded Gabbay 	static_loader->sram_offset_mask = ~(lower_32_bits(SRAM_BASE_ADDR));
3821e65e175bSOded Gabbay 	static_loader->cpu_reset_wait_msec = hdev->pldm ?
3822e65e175bSOded Gabbay 			GAUDI_PLDM_RESET_WAIT_MSEC :
3823e65e175bSOded Gabbay 			GAUDI_CPU_RESET_WAIT_MSEC;
3824e65e175bSOded Gabbay }
3825e65e175bSOded Gabbay 
3826e65e175bSOded Gabbay static void gaudi_init_firmware_preload_params(struct hl_device *hdev)
3827e65e175bSOded Gabbay {
3828e65e175bSOded Gabbay 	struct pre_fw_load_props *pre_fw_load = &hdev->fw_loader.pre_fw_load;
3829e65e175bSOded Gabbay 
3830e65e175bSOded Gabbay 	pre_fw_load->cpu_boot_status_reg = mmPSOC_GLOBAL_CONF_CPU_BOOT_STATUS;
3831e65e175bSOded Gabbay 	pre_fw_load->sts_boot_dev_sts0_reg = mmCPU_BOOT_DEV_STS0;
3832e65e175bSOded Gabbay 	pre_fw_load->sts_boot_dev_sts1_reg = mmCPU_BOOT_DEV_STS1;
3833e65e175bSOded Gabbay 	pre_fw_load->boot_err0_reg = mmCPU_BOOT_ERR0;
3834e65e175bSOded Gabbay 	pre_fw_load->boot_err1_reg = mmCPU_BOOT_ERR1;
3835e65e175bSOded Gabbay 	pre_fw_load->wait_for_preboot_timeout = GAUDI_BOOT_FIT_REQ_TIMEOUT_USEC;
3836e65e175bSOded Gabbay }
3837e65e175bSOded Gabbay 
3838e65e175bSOded Gabbay static void gaudi_init_firmware_loader(struct hl_device *hdev)
3839e65e175bSOded Gabbay {
3840e65e175bSOded Gabbay 	struct asic_fixed_properties *prop = &hdev->asic_prop;
3841e65e175bSOded Gabbay 	struct fw_load_mgr *fw_loader = &hdev->fw_loader;
3842e65e175bSOded Gabbay 
3843e65e175bSOded Gabbay 	/* fill common fields */
3844e65e175bSOded Gabbay 	fw_loader->fw_comp_loaded = FW_TYPE_NONE;
3845e65e175bSOded Gabbay 	fw_loader->boot_fit_img.image_name = GAUDI_BOOT_FIT_FILE;
3846e65e175bSOded Gabbay 	fw_loader->linux_img.image_name = GAUDI_LINUX_FW_FILE;
3847e65e175bSOded Gabbay 	fw_loader->cpu_timeout = GAUDI_CPU_TIMEOUT_USEC;
3848e65e175bSOded Gabbay 	fw_loader->boot_fit_timeout = GAUDI_BOOT_FIT_REQ_TIMEOUT_USEC;
3849e65e175bSOded Gabbay 	fw_loader->skip_bmc = !hdev->bmc_enable;
3850e65e175bSOded Gabbay 	fw_loader->sram_bar_id = SRAM_BAR_ID;
3851e65e175bSOded Gabbay 	fw_loader->dram_bar_id = HBM_BAR_ID;
3852e65e175bSOded Gabbay 
3853e65e175bSOded Gabbay 	if (prop->dynamic_fw_load)
3854e65e175bSOded Gabbay 		gaudi_init_dynamic_firmware_loader(hdev);
3855e65e175bSOded Gabbay 	else
3856e65e175bSOded Gabbay 		gaudi_init_static_firmware_loader(hdev);
3857e65e175bSOded Gabbay }
3858e65e175bSOded Gabbay 
3859e65e175bSOded Gabbay static int gaudi_init_cpu(struct hl_device *hdev)
3860e65e175bSOded Gabbay {
3861e65e175bSOded Gabbay 	struct gaudi_device *gaudi = hdev->asic_specific;
3862e65e175bSOded Gabbay 	int rc;
3863e65e175bSOded Gabbay 
3864e65e175bSOded Gabbay 	if (!(hdev->fw_components & FW_TYPE_PREBOOT_CPU))
3865e65e175bSOded Gabbay 		return 0;
3866e65e175bSOded Gabbay 
3867e65e175bSOded Gabbay 	if (gaudi->hw_cap_initialized & HW_CAP_CPU)
3868e65e175bSOded Gabbay 		return 0;
3869e65e175bSOded Gabbay 
3870e65e175bSOded Gabbay 	/*
3871e65e175bSOded Gabbay 	 * The device CPU works with 40 bits addresses.
3872e65e175bSOded Gabbay 	 * This register sets the extension to 50 bits.
3873e65e175bSOded Gabbay 	 */
3874e65e175bSOded Gabbay 	if (!hdev->asic_prop.fw_security_enabled)
3875e65e175bSOded Gabbay 		WREG32(mmCPU_IF_CPU_MSB_ADDR, hdev->cpu_pci_msb_addr);
3876e65e175bSOded Gabbay 
3877e65e175bSOded Gabbay 	rc = hl_fw_init_cpu(hdev);
3878e65e175bSOded Gabbay 
3879e65e175bSOded Gabbay 	if (rc)
3880e65e175bSOded Gabbay 		return rc;
3881e65e175bSOded Gabbay 
3882e65e175bSOded Gabbay 	gaudi->hw_cap_initialized |= HW_CAP_CPU;
3883e65e175bSOded Gabbay 
3884e65e175bSOded Gabbay 	return 0;
3885e65e175bSOded Gabbay }
3886e65e175bSOded Gabbay 
3887e65e175bSOded Gabbay static int gaudi_init_cpu_queues(struct hl_device *hdev, u32 cpu_timeout)
3888e65e175bSOded Gabbay {
3889e65e175bSOded Gabbay 	struct cpu_dyn_regs *dyn_regs =
3890e65e175bSOded Gabbay 			&hdev->fw_loader.dynamic_loader.comm_desc.cpu_dyn_regs;
3891e65e175bSOded Gabbay 	struct asic_fixed_properties *prop = &hdev->asic_prop;
3892e65e175bSOded Gabbay 	struct gaudi_device *gaudi = hdev->asic_specific;
3893e65e175bSOded Gabbay 	u32 status, irq_handler_offset;
3894e65e175bSOded Gabbay 	struct hl_eq *eq;
3895e65e175bSOded Gabbay 	struct hl_hw_queue *cpu_pq =
3896e65e175bSOded Gabbay 			&hdev->kernel_queues[GAUDI_QUEUE_ID_CPU_PQ];
3897e65e175bSOded Gabbay 	int err;
3898e65e175bSOded Gabbay 
3899e65e175bSOded Gabbay 	if (!hdev->cpu_queues_enable)
3900e65e175bSOded Gabbay 		return 0;
3901e65e175bSOded Gabbay 
3902e65e175bSOded Gabbay 	if (gaudi->hw_cap_initialized & HW_CAP_CPU_Q)
3903e65e175bSOded Gabbay 		return 0;
3904e65e175bSOded Gabbay 
3905e65e175bSOded Gabbay 	eq = &hdev->event_queue;
3906e65e175bSOded Gabbay 
3907e65e175bSOded Gabbay 	WREG32(mmCPU_IF_PQ_BASE_ADDR_LOW, lower_32_bits(cpu_pq->bus_address));
3908e65e175bSOded Gabbay 	WREG32(mmCPU_IF_PQ_BASE_ADDR_HIGH, upper_32_bits(cpu_pq->bus_address));
3909e65e175bSOded Gabbay 
3910e65e175bSOded Gabbay 	WREG32(mmCPU_IF_EQ_BASE_ADDR_LOW, lower_32_bits(eq->bus_address));
3911e65e175bSOded Gabbay 	WREG32(mmCPU_IF_EQ_BASE_ADDR_HIGH, upper_32_bits(eq->bus_address));
3912e65e175bSOded Gabbay 
3913e65e175bSOded Gabbay 	WREG32(mmCPU_IF_CQ_BASE_ADDR_LOW,
3914e65e175bSOded Gabbay 			lower_32_bits(hdev->cpu_accessible_dma_address));
3915e65e175bSOded Gabbay 	WREG32(mmCPU_IF_CQ_BASE_ADDR_HIGH,
3916e65e175bSOded Gabbay 			upper_32_bits(hdev->cpu_accessible_dma_address));
3917e65e175bSOded Gabbay 
3918e65e175bSOded Gabbay 	WREG32(mmCPU_IF_PQ_LENGTH, HL_QUEUE_SIZE_IN_BYTES);
3919e65e175bSOded Gabbay 	WREG32(mmCPU_IF_EQ_LENGTH, HL_EQ_SIZE_IN_BYTES);
3920e65e175bSOded Gabbay 	WREG32(mmCPU_IF_CQ_LENGTH, HL_CPU_ACCESSIBLE_MEM_SIZE);
3921e65e175bSOded Gabbay 
3922e65e175bSOded Gabbay 	/* Used for EQ CI */
3923e65e175bSOded Gabbay 	WREG32(mmCPU_IF_EQ_RD_OFFS, 0);
3924e65e175bSOded Gabbay 
3925e65e175bSOded Gabbay 	WREG32(mmCPU_IF_PF_PQ_PI, 0);
3926e65e175bSOded Gabbay 
3927e65e175bSOded Gabbay 	if (gaudi->multi_msi_mode)
3928e65e175bSOded Gabbay 		WREG32(mmCPU_IF_QUEUE_INIT, PQ_INIT_STATUS_READY_FOR_CP);
3929e65e175bSOded Gabbay 	else
3930e65e175bSOded Gabbay 		WREG32(mmCPU_IF_QUEUE_INIT,
3931e65e175bSOded Gabbay 			PQ_INIT_STATUS_READY_FOR_CP_SINGLE_MSI);
3932e65e175bSOded Gabbay 
3933e65e175bSOded Gabbay 	irq_handler_offset = prop->gic_interrupts_enable ?
3934e65e175bSOded Gabbay 			mmGIC_DISTRIBUTOR__5_GICD_SETSPI_NSR :
3935e65e175bSOded Gabbay 			le32_to_cpu(dyn_regs->gic_host_pi_upd_irq);
3936e65e175bSOded Gabbay 
3937e65e175bSOded Gabbay 	WREG32(irq_handler_offset,
3938e65e175bSOded Gabbay 		gaudi_irq_map_table[GAUDI_EVENT_PI_UPDATE].cpu_id);
3939e65e175bSOded Gabbay 
3940e65e175bSOded Gabbay 	err = hl_poll_timeout(
3941e65e175bSOded Gabbay 		hdev,
3942e65e175bSOded Gabbay 		mmCPU_IF_QUEUE_INIT,
3943e65e175bSOded Gabbay 		status,
3944e65e175bSOded Gabbay 		(status == PQ_INIT_STATUS_READY_FOR_HOST),
3945e65e175bSOded Gabbay 		1000,
3946e65e175bSOded Gabbay 		cpu_timeout);
3947e65e175bSOded Gabbay 
3948e65e175bSOded Gabbay 	if (err) {
3949e65e175bSOded Gabbay 		dev_err(hdev->dev,
3950e65e175bSOded Gabbay 			"Failed to communicate with Device CPU (CPU-CP timeout)\n");
3951e65e175bSOded Gabbay 		return -EIO;
3952e65e175bSOded Gabbay 	}
3953e65e175bSOded Gabbay 
3954e65e175bSOded Gabbay 	/* update FW application security bits */
3955e65e175bSOded Gabbay 	if (prop->fw_cpu_boot_dev_sts0_valid)
3956e65e175bSOded Gabbay 		prop->fw_app_cpu_boot_dev_sts0 = RREG32(mmCPU_BOOT_DEV_STS0);
3957e65e175bSOded Gabbay 	if (prop->fw_cpu_boot_dev_sts1_valid)
3958e65e175bSOded Gabbay 		prop->fw_app_cpu_boot_dev_sts1 = RREG32(mmCPU_BOOT_DEV_STS1);
3959e65e175bSOded Gabbay 
3960e65e175bSOded Gabbay 	gaudi->hw_cap_initialized |= HW_CAP_CPU_Q;
3961e65e175bSOded Gabbay 	return 0;
3962e65e175bSOded Gabbay }
3963e65e175bSOded Gabbay 
3964e65e175bSOded Gabbay static void gaudi_pre_hw_init(struct hl_device *hdev)
3965e65e175bSOded Gabbay {
3966e65e175bSOded Gabbay 	/* Perform read from the device to make sure device is up */
3967e65e175bSOded Gabbay 	RREG32(mmHW_STATE);
3968e65e175bSOded Gabbay 
3969e65e175bSOded Gabbay 	if (!hdev->asic_prop.fw_security_enabled) {
3970e65e175bSOded Gabbay 		/* Set the access through PCI bars (Linux driver only) as
3971e65e175bSOded Gabbay 		 * secured
3972e65e175bSOded Gabbay 		 */
3973e65e175bSOded Gabbay 		WREG32(mmPCIE_WRAP_LBW_PROT_OVR,
3974e65e175bSOded Gabbay 				(PCIE_WRAP_LBW_PROT_OVR_RD_EN_MASK |
3975e65e175bSOded Gabbay 				PCIE_WRAP_LBW_PROT_OVR_WR_EN_MASK));
3976e65e175bSOded Gabbay 
3977e65e175bSOded Gabbay 		/* Perform read to flush the waiting writes to ensure
3978e65e175bSOded Gabbay 		 * configuration was set in the device
3979e65e175bSOded Gabbay 		 */
3980e65e175bSOded Gabbay 		RREG32(mmPCIE_WRAP_LBW_PROT_OVR);
3981e65e175bSOded Gabbay 	}
3982e65e175bSOded Gabbay 
3983e65e175bSOded Gabbay 	/*
3984e65e175bSOded Gabbay 	 * Let's mark in the H/W that we have reached this point. We check
3985e65e175bSOded Gabbay 	 * this value in the reset_before_init function to understand whether
3986e65e175bSOded Gabbay 	 * we need to reset the chip before doing H/W init. This register is
3987e65e175bSOded Gabbay 	 * cleared by the H/W upon H/W reset
3988e65e175bSOded Gabbay 	 */
3989e65e175bSOded Gabbay 	WREG32(mmHW_STATE, HL_DEVICE_HW_STATE_DIRTY);
3990e65e175bSOded Gabbay }
3991e65e175bSOded Gabbay 
3992e65e175bSOded Gabbay static int gaudi_hw_init(struct hl_device *hdev)
3993e65e175bSOded Gabbay {
3994e65e175bSOded Gabbay 	struct gaudi_device *gaudi = hdev->asic_specific;
3995e65e175bSOded Gabbay 	int rc;
3996e65e175bSOded Gabbay 
3997e65e175bSOded Gabbay 	gaudi_pre_hw_init(hdev);
3998e65e175bSOded Gabbay 
3999e65e175bSOded Gabbay 	/* If iATU is done by FW, the HBM bar ALWAYS points to DRAM_PHYS_BASE.
4000e65e175bSOded Gabbay 	 * So we set it here and if anyone tries to move it later to
4001e65e175bSOded Gabbay 	 * a different address, there will be an error
4002e65e175bSOded Gabbay 	 */
4003e65e175bSOded Gabbay 	if (hdev->asic_prop.iatu_done_by_fw)
4004e65e175bSOded Gabbay 		gaudi->hbm_bar_cur_addr = DRAM_PHYS_BASE;
4005e65e175bSOded Gabbay 
4006e65e175bSOded Gabbay 	/*
4007e65e175bSOded Gabbay 	 * Before pushing u-boot/linux to device, need to set the hbm bar to
4008e65e175bSOded Gabbay 	 * base address of dram
4009e65e175bSOded Gabbay 	 */
4010e65e175bSOded Gabbay 	if (gaudi_set_hbm_bar_base(hdev, DRAM_PHYS_BASE) == U64_MAX) {
4011e65e175bSOded Gabbay 		dev_err(hdev->dev,
4012e65e175bSOded Gabbay 			"failed to map HBM bar to DRAM base address\n");
4013e65e175bSOded Gabbay 		return -EIO;
4014e65e175bSOded Gabbay 	}
4015e65e175bSOded Gabbay 
4016e65e175bSOded Gabbay 	rc = gaudi_init_cpu(hdev);
4017e65e175bSOded Gabbay 	if (rc) {
4018e65e175bSOded Gabbay 		dev_err(hdev->dev, "failed to initialize CPU\n");
4019e65e175bSOded Gabbay 		return rc;
4020e65e175bSOded Gabbay 	}
4021e65e175bSOded Gabbay 
4022e65e175bSOded Gabbay 	/* In case the clock gating was enabled in preboot we need to disable
4023e65e175bSOded Gabbay 	 * it here before touching the MME/TPC registers.
4024e65e175bSOded Gabbay 	 */
4025e65e175bSOded Gabbay 	gaudi_disable_clock_gating(hdev);
4026e65e175bSOded Gabbay 
4027e65e175bSOded Gabbay 	/* SRAM scrambler must be initialized after CPU is running from HBM */
4028e65e175bSOded Gabbay 	gaudi_init_scrambler_sram(hdev);
4029e65e175bSOded Gabbay 
4030e65e175bSOded Gabbay 	/* This is here just in case we are working without CPU */
4031e65e175bSOded Gabbay 	gaudi_init_scrambler_hbm(hdev);
4032e65e175bSOded Gabbay 
4033e65e175bSOded Gabbay 	gaudi_init_golden_registers(hdev);
4034e65e175bSOded Gabbay 
4035e65e175bSOded Gabbay 	rc = gaudi_mmu_init(hdev);
4036e65e175bSOded Gabbay 	if (rc)
4037e65e175bSOded Gabbay 		return rc;
4038e65e175bSOded Gabbay 
4039e65e175bSOded Gabbay 	gaudi_init_security(hdev);
4040e65e175bSOded Gabbay 
4041e65e175bSOded Gabbay 	gaudi_init_pci_dma_qmans(hdev);
4042e65e175bSOded Gabbay 
4043e65e175bSOded Gabbay 	gaudi_init_hbm_dma_qmans(hdev);
4044e65e175bSOded Gabbay 
4045e65e175bSOded Gabbay 	gaudi_init_mme_qmans(hdev);
4046e65e175bSOded Gabbay 
4047e65e175bSOded Gabbay 	gaudi_init_tpc_qmans(hdev);
4048e65e175bSOded Gabbay 
4049e65e175bSOded Gabbay 	gaudi_init_nic_qmans(hdev);
4050e65e175bSOded Gabbay 
4051e65e175bSOded Gabbay 	gaudi_enable_timestamp(hdev);
4052e65e175bSOded Gabbay 
4053e65e175bSOded Gabbay 	/* MSI must be enabled before CPU queues and NIC are initialized */
4054e65e175bSOded Gabbay 	rc = gaudi_enable_msi(hdev);
4055e65e175bSOded Gabbay 	if (rc)
4056e65e175bSOded Gabbay 		goto disable_queues;
4057e65e175bSOded Gabbay 
4058e65e175bSOded Gabbay 	/* must be called after MSI was enabled */
4059e65e175bSOded Gabbay 	rc = gaudi_init_cpu_queues(hdev, GAUDI_CPU_TIMEOUT_USEC);
4060e65e175bSOded Gabbay 	if (rc) {
4061e65e175bSOded Gabbay 		dev_err(hdev->dev, "failed to initialize CPU H/W queues %d\n",
4062e65e175bSOded Gabbay 			rc);
4063e65e175bSOded Gabbay 		goto disable_msi;
4064e65e175bSOded Gabbay 	}
4065e65e175bSOded Gabbay 
4066e65e175bSOded Gabbay 	/* Perform read from the device to flush all configuration */
4067e65e175bSOded Gabbay 	RREG32(mmHW_STATE);
4068e65e175bSOded Gabbay 
4069e65e175bSOded Gabbay 	return 0;
4070e65e175bSOded Gabbay 
4071e65e175bSOded Gabbay disable_msi:
4072e65e175bSOded Gabbay 	gaudi_disable_msi(hdev);
4073e65e175bSOded Gabbay disable_queues:
4074e65e175bSOded Gabbay 	gaudi_disable_mme_qmans(hdev);
4075e65e175bSOded Gabbay 	gaudi_disable_pci_dma_qmans(hdev);
4076e65e175bSOded Gabbay 
4077e65e175bSOded Gabbay 	return rc;
4078e65e175bSOded Gabbay }
4079e65e175bSOded Gabbay 
40805e09ae92SDafna Hirschfeld static int gaudi_hw_fini(struct hl_device *hdev, bool hard_reset, bool fw_reset)
4081e65e175bSOded Gabbay {
4082e65e175bSOded Gabbay 	struct cpu_dyn_regs *dyn_regs =
4083e65e175bSOded Gabbay 			&hdev->fw_loader.dynamic_loader.comm_desc.cpu_dyn_regs;
4084e65e175bSOded Gabbay 	u32 status, reset_timeout_ms, cpu_timeout_ms, irq_handler_offset;
4085e65e175bSOded Gabbay 	struct gaudi_device *gaudi = hdev->asic_specific;
4086e65e175bSOded Gabbay 	bool driver_performs_reset;
4087e65e175bSOded Gabbay 
4088e65e175bSOded Gabbay 	if (!hard_reset) {
4089e65e175bSOded Gabbay 		dev_err(hdev->dev, "GAUDI doesn't support soft-reset\n");
40905e09ae92SDafna Hirschfeld 		return 0;
4091e65e175bSOded Gabbay 	}
4092e65e175bSOded Gabbay 
4093e65e175bSOded Gabbay 	if (hdev->pldm) {
4094e65e175bSOded Gabbay 		reset_timeout_ms = GAUDI_PLDM_HRESET_TIMEOUT_MSEC;
4095e65e175bSOded Gabbay 		cpu_timeout_ms = GAUDI_PLDM_RESET_WAIT_MSEC;
4096e65e175bSOded Gabbay 	} else {
4097e65e175bSOded Gabbay 		reset_timeout_ms = GAUDI_RESET_TIMEOUT_MSEC;
4098e65e175bSOded Gabbay 		cpu_timeout_ms = GAUDI_CPU_RESET_WAIT_MSEC;
4099e65e175bSOded Gabbay 	}
4100e65e175bSOded Gabbay 
4101e65e175bSOded Gabbay 	if (fw_reset) {
4102e65e175bSOded Gabbay 		dev_dbg(hdev->dev,
4103e65e175bSOded Gabbay 			"Firmware performs HARD reset, going to wait %dms\n",
4104e65e175bSOded Gabbay 			reset_timeout_ms);
4105e65e175bSOded Gabbay 
4106e65e175bSOded Gabbay 		goto skip_reset;
4107e65e175bSOded Gabbay 	}
4108e65e175bSOded Gabbay 
4109e65e175bSOded Gabbay 	driver_performs_reset = !!(!hdev->asic_prop.fw_security_enabled &&
4110e65e175bSOded Gabbay 					!hdev->asic_prop.hard_reset_done_by_fw);
4111e65e175bSOded Gabbay 
4112e65e175bSOded Gabbay 	/* Set device to handle FLR by H/W as we will put the device CPU to
4113e65e175bSOded Gabbay 	 * halt mode
4114e65e175bSOded Gabbay 	 */
4115e65e175bSOded Gabbay 	if (driver_performs_reset)
4116e65e175bSOded Gabbay 		WREG32(mmPCIE_AUX_FLR_CTRL, (PCIE_AUX_FLR_CTRL_HW_CTRL_MASK |
4117e65e175bSOded Gabbay 					PCIE_AUX_FLR_CTRL_INT_MASK_MASK));
4118e65e175bSOded Gabbay 
4119e65e175bSOded Gabbay 	/* If linux is loaded in the device CPU we need to communicate with it
4120e65e175bSOded Gabbay 	 * via the GIC. Otherwise, we need to use COMMS or the MSG_TO_CPU
4121e65e175bSOded Gabbay 	 * registers in case of old F/Ws
4122e65e175bSOded Gabbay 	 */
4123e65e175bSOded Gabbay 	if (hdev->fw_loader.fw_comp_loaded & FW_TYPE_LINUX) {
4124e65e175bSOded Gabbay 		irq_handler_offset = hdev->asic_prop.gic_interrupts_enable ?
4125e65e175bSOded Gabbay 				mmGIC_DISTRIBUTOR__5_GICD_SETSPI_NSR :
4126e65e175bSOded Gabbay 				le32_to_cpu(dyn_regs->gic_host_halt_irq);
4127e65e175bSOded Gabbay 
4128e65e175bSOded Gabbay 		WREG32(irq_handler_offset,
4129e65e175bSOded Gabbay 			gaudi_irq_map_table[GAUDI_EVENT_HALT_MACHINE].cpu_id);
4130e65e175bSOded Gabbay 
4131e65e175bSOded Gabbay 		/* This is a hail-mary attempt to revive the card in the small chance that the
4132e65e175bSOded Gabbay 		 * f/w has experienced a watchdog event, which caused it to return back to preboot.
4133e65e175bSOded Gabbay 		 * In that case, triggering reset through GIC won't help. We need to trigger the
4134e65e175bSOded Gabbay 		 * reset as if Linux wasn't loaded.
4135e65e175bSOded Gabbay 		 *
4136e65e175bSOded Gabbay 		 * We do it only if the reset cause was HB, because that would be the indication
4137e65e175bSOded Gabbay 		 * of such an event.
4138e65e175bSOded Gabbay 		 *
4139e65e175bSOded Gabbay 		 * In case watchdog hasn't expired but we still got HB, then this won't do any
4140e65e175bSOded Gabbay 		 * damage.
4141e65e175bSOded Gabbay 		 */
4142e65e175bSOded Gabbay 		if (hdev->reset_info.curr_reset_cause == HL_RESET_CAUSE_HEARTBEAT) {
4143e65e175bSOded Gabbay 			if (hdev->asic_prop.hard_reset_done_by_fw)
4144e65e175bSOded Gabbay 				hl_fw_ask_hard_reset_without_linux(hdev);
4145e65e175bSOded Gabbay 			else
4146e65e175bSOded Gabbay 				hl_fw_ask_halt_machine_without_linux(hdev);
4147e65e175bSOded Gabbay 		}
4148e65e175bSOded Gabbay 	} else {
4149e65e175bSOded Gabbay 		if (hdev->asic_prop.hard_reset_done_by_fw)
4150e65e175bSOded Gabbay 			hl_fw_ask_hard_reset_without_linux(hdev);
4151e65e175bSOded Gabbay 		else
4152e65e175bSOded Gabbay 			hl_fw_ask_halt_machine_without_linux(hdev);
4153e65e175bSOded Gabbay 	}
4154e65e175bSOded Gabbay 
4155e65e175bSOded Gabbay 	if (driver_performs_reset) {
4156e65e175bSOded Gabbay 
4157e65e175bSOded Gabbay 		/* Configure the reset registers. Must be done as early as
4158e65e175bSOded Gabbay 		 * possible in case we fail during H/W initialization
4159e65e175bSOded Gabbay 		 */
4160e65e175bSOded Gabbay 		WREG32(mmPSOC_GLOBAL_CONF_SOFT_RST_CFG_H,
4161e65e175bSOded Gabbay 						(CFG_RST_H_DMA_MASK |
4162e65e175bSOded Gabbay 						CFG_RST_H_MME_MASK |
4163e65e175bSOded Gabbay 						CFG_RST_H_SM_MASK |
4164e65e175bSOded Gabbay 						CFG_RST_H_TPC_7_MASK));
4165e65e175bSOded Gabbay 
4166e65e175bSOded Gabbay 		WREG32(mmPSOC_GLOBAL_CONF_SOFT_RST_CFG_L, CFG_RST_L_TPC_MASK);
4167e65e175bSOded Gabbay 
4168e65e175bSOded Gabbay 		WREG32(mmPSOC_GLOBAL_CONF_SW_ALL_RST_CFG_H,
4169e65e175bSOded Gabbay 						(CFG_RST_H_HBM_MASK |
4170e65e175bSOded Gabbay 						CFG_RST_H_TPC_7_MASK |
4171e65e175bSOded Gabbay 						CFG_RST_H_NIC_MASK |
4172e65e175bSOded Gabbay 						CFG_RST_H_SM_MASK |
4173e65e175bSOded Gabbay 						CFG_RST_H_DMA_MASK |
4174e65e175bSOded Gabbay 						CFG_RST_H_MME_MASK |
4175e65e175bSOded Gabbay 						CFG_RST_H_CPU_MASK |
4176e65e175bSOded Gabbay 						CFG_RST_H_MMU_MASK));
4177e65e175bSOded Gabbay 
4178e65e175bSOded Gabbay 		WREG32(mmPSOC_GLOBAL_CONF_SW_ALL_RST_CFG_L,
4179e65e175bSOded Gabbay 						(CFG_RST_L_IF_MASK |
4180e65e175bSOded Gabbay 						CFG_RST_L_PSOC_MASK |
4181e65e175bSOded Gabbay 						CFG_RST_L_TPC_MASK));
4182e65e175bSOded Gabbay 
4183e65e175bSOded Gabbay 		msleep(cpu_timeout_ms);
4184e65e175bSOded Gabbay 
4185e65e175bSOded Gabbay 		/* Tell ASIC not to re-initialize PCIe */
4186e65e175bSOded Gabbay 		WREG32(mmPREBOOT_PCIE_EN, LKD_HARD_RESET_MAGIC);
4187e65e175bSOded Gabbay 
4188e65e175bSOded Gabbay 		/* Restart BTL/BLR upon hard-reset */
4189e65e175bSOded Gabbay 		WREG32(mmPSOC_GLOBAL_CONF_BOOT_SEQ_RE_START, 1);
4190e65e175bSOded Gabbay 
4191e65e175bSOded Gabbay 		WREG32(mmPSOC_GLOBAL_CONF_SW_ALL_RST,
4192e65e175bSOded Gabbay 			1 << PSOC_GLOBAL_CONF_SW_ALL_RST_IND_SHIFT);
4193e65e175bSOded Gabbay 
4194e65e175bSOded Gabbay 		dev_dbg(hdev->dev,
4195e65e175bSOded Gabbay 			"Issued HARD reset command, going to wait %dms\n",
4196e65e175bSOded Gabbay 			reset_timeout_ms);
4197e65e175bSOded Gabbay 	} else {
4198e65e175bSOded Gabbay 		dev_dbg(hdev->dev,
4199e65e175bSOded Gabbay 			"Firmware performs HARD reset, going to wait %dms\n",
4200e65e175bSOded Gabbay 			reset_timeout_ms);
4201e65e175bSOded Gabbay 	}
4202e65e175bSOded Gabbay 
4203e65e175bSOded Gabbay skip_reset:
4204e65e175bSOded Gabbay 	/*
4205e65e175bSOded Gabbay 	 * After hard reset, we can't poll the BTM_FSM register because the PSOC
4206e65e175bSOded Gabbay 	 * itself is in reset. Need to wait until the reset is deasserted
4207e65e175bSOded Gabbay 	 */
4208e65e175bSOded Gabbay 	msleep(reset_timeout_ms);
4209e65e175bSOded Gabbay 
4210e65e175bSOded Gabbay 	status = RREG32(mmPSOC_GLOBAL_CONF_BTM_FSM);
4211077a39faSDafna Hirschfeld 	if (status & PSOC_GLOBAL_CONF_BTM_FSM_STATE_MASK) {
4212077a39faSDafna Hirschfeld 		dev_err(hdev->dev, "Timeout while waiting for device to reset 0x%x\n", status);
4213077a39faSDafna Hirschfeld 		return -ETIMEDOUT;
4214077a39faSDafna Hirschfeld 	}
4215e65e175bSOded Gabbay 
4216e65e175bSOded Gabbay 	if (gaudi) {
4217e65e175bSOded Gabbay 		gaudi->hw_cap_initialized &= ~(HW_CAP_CPU | HW_CAP_CPU_Q | HW_CAP_HBM |
4218e65e175bSOded Gabbay 						HW_CAP_PCI_DMA | HW_CAP_MME | HW_CAP_TPC_MASK |
4219e65e175bSOded Gabbay 						HW_CAP_HBM_DMA | HW_CAP_PLL | HW_CAP_NIC_MASK |
4220e65e175bSOded Gabbay 						HW_CAP_MMU | HW_CAP_SRAM_SCRAMBLER |
4221e65e175bSOded Gabbay 						HW_CAP_HBM_SCRAMBLER);
4222e65e175bSOded Gabbay 
4223e65e175bSOded Gabbay 		memset(gaudi->events_stat, 0, sizeof(gaudi->events_stat));
4224e65e175bSOded Gabbay 
4225e65e175bSOded Gabbay 		hdev->device_cpu_is_halted = false;
4226e65e175bSOded Gabbay 	}
42275e09ae92SDafna Hirschfeld 	return 0;
4228e65e175bSOded Gabbay }
4229e65e175bSOded Gabbay 
4230e65e175bSOded Gabbay static int gaudi_suspend(struct hl_device *hdev)
4231e65e175bSOded Gabbay {
4232e65e175bSOded Gabbay 	int rc;
4233e65e175bSOded Gabbay 
4234e65e175bSOded Gabbay 	rc = hl_fw_send_pci_access_msg(hdev, CPUCP_PACKET_DISABLE_PCI_ACCESS, 0x0);
4235e65e175bSOded Gabbay 	if (rc)
4236e65e175bSOded Gabbay 		dev_err(hdev->dev, "Failed to disable PCI access from CPU\n");
4237e65e175bSOded Gabbay 
4238e65e175bSOded Gabbay 	return rc;
4239e65e175bSOded Gabbay }
4240e65e175bSOded Gabbay 
4241e65e175bSOded Gabbay static int gaudi_resume(struct hl_device *hdev)
4242e65e175bSOded Gabbay {
4243e65e175bSOded Gabbay 	return gaudi_init_iatu(hdev);
4244e65e175bSOded Gabbay }
4245e65e175bSOded Gabbay 
4246e65e175bSOded Gabbay static int gaudi_mmap(struct hl_device *hdev, struct vm_area_struct *vma,
4247e65e175bSOded Gabbay 			void *cpu_addr, dma_addr_t dma_addr, size_t size)
4248e65e175bSOded Gabbay {
4249e65e175bSOded Gabbay 	int rc;
4250e65e175bSOded Gabbay 
42513822a7c4SLinus Torvalds 	vm_flags_set(vma, VM_IO | VM_PFNMAP | VM_DONTEXPAND | VM_DONTDUMP |
42523822a7c4SLinus Torvalds 			VM_DONTCOPY | VM_NORESERVE);
4253e65e175bSOded Gabbay 
4254e65e175bSOded Gabbay 	rc = dma_mmap_coherent(hdev->dev, vma, cpu_addr,
4255e65e175bSOded Gabbay 				(dma_addr - HOST_PHYS_BASE), size);
4256e65e175bSOded Gabbay 	if (rc)
4257e65e175bSOded Gabbay 		dev_err(hdev->dev, "dma_mmap_coherent error %d", rc);
4258e65e175bSOded Gabbay 
4259e65e175bSOded Gabbay 	return rc;
4260e65e175bSOded Gabbay }
4261e65e175bSOded Gabbay 
4262e65e175bSOded Gabbay static void gaudi_ring_doorbell(struct hl_device *hdev, u32 hw_queue_id, u32 pi)
4263e65e175bSOded Gabbay {
4264e65e175bSOded Gabbay 	struct cpu_dyn_regs *dyn_regs =
4265e65e175bSOded Gabbay 			&hdev->fw_loader.dynamic_loader.comm_desc.cpu_dyn_regs;
4266e65e175bSOded Gabbay 	u32 db_reg_offset, db_value, dma_qm_offset, q_off, irq_handler_offset;
4267e65e175bSOded Gabbay 	struct gaudi_device *gaudi = hdev->asic_specific;
4268e65e175bSOded Gabbay 	bool invalid_queue = false;
4269e65e175bSOded Gabbay 	int dma_id;
4270e65e175bSOded Gabbay 
4271e65e175bSOded Gabbay 	switch (hw_queue_id) {
4272e65e175bSOded Gabbay 	case GAUDI_QUEUE_ID_DMA_0_0...GAUDI_QUEUE_ID_DMA_0_3:
4273e65e175bSOded Gabbay 		dma_id = gaudi_dma_assignment[GAUDI_PCI_DMA_1];
4274e65e175bSOded Gabbay 		dma_qm_offset = dma_id * DMA_QMAN_OFFSET;
4275e65e175bSOded Gabbay 		q_off = dma_qm_offset + (hw_queue_id & 0x3) * 4;
4276e65e175bSOded Gabbay 		db_reg_offset = mmDMA0_QM_PQ_PI_0 + q_off;
4277e65e175bSOded Gabbay 		break;
4278e65e175bSOded Gabbay 
4279e65e175bSOded Gabbay 	case GAUDI_QUEUE_ID_DMA_1_0...GAUDI_QUEUE_ID_DMA_1_3:
4280e65e175bSOded Gabbay 		dma_id = gaudi_dma_assignment[GAUDI_PCI_DMA_2];
4281e65e175bSOded Gabbay 		dma_qm_offset = dma_id * DMA_QMAN_OFFSET;
4282e65e175bSOded Gabbay 		q_off = dma_qm_offset + (hw_queue_id & 0x3) * 4;
4283e65e175bSOded Gabbay 		db_reg_offset = mmDMA0_QM_PQ_PI_0 + q_off;
4284e65e175bSOded Gabbay 		break;
4285e65e175bSOded Gabbay 
4286e65e175bSOded Gabbay 	case GAUDI_QUEUE_ID_DMA_2_0...GAUDI_QUEUE_ID_DMA_2_3:
4287e65e175bSOded Gabbay 		dma_id = gaudi_dma_assignment[GAUDI_HBM_DMA_1];
4288e65e175bSOded Gabbay 		dma_qm_offset = dma_id * DMA_QMAN_OFFSET;
4289e65e175bSOded Gabbay 		q_off = dma_qm_offset + ((hw_queue_id - 1) & 0x3) * 4;
4290e65e175bSOded Gabbay 		db_reg_offset = mmDMA0_QM_PQ_PI_0 + q_off;
4291e65e175bSOded Gabbay 		break;
4292e65e175bSOded Gabbay 
4293e65e175bSOded Gabbay 	case GAUDI_QUEUE_ID_DMA_3_0...GAUDI_QUEUE_ID_DMA_3_3:
4294e65e175bSOded Gabbay 		dma_id = gaudi_dma_assignment[GAUDI_HBM_DMA_2];
4295e65e175bSOded Gabbay 		dma_qm_offset = dma_id * DMA_QMAN_OFFSET;
4296e65e175bSOded Gabbay 		q_off = dma_qm_offset + ((hw_queue_id - 1) & 0x3) * 4;
4297e65e175bSOded Gabbay 		db_reg_offset = mmDMA0_QM_PQ_PI_0 + q_off;
4298e65e175bSOded Gabbay 		break;
4299e65e175bSOded Gabbay 
4300e65e175bSOded Gabbay 	case GAUDI_QUEUE_ID_DMA_4_0...GAUDI_QUEUE_ID_DMA_4_3:
4301e65e175bSOded Gabbay 		dma_id = gaudi_dma_assignment[GAUDI_HBM_DMA_3];
4302e65e175bSOded Gabbay 		dma_qm_offset = dma_id * DMA_QMAN_OFFSET;
4303e65e175bSOded Gabbay 		q_off = dma_qm_offset + ((hw_queue_id - 1) & 0x3) * 4;
4304e65e175bSOded Gabbay 		db_reg_offset = mmDMA0_QM_PQ_PI_0 + q_off;
4305e65e175bSOded Gabbay 		break;
4306e65e175bSOded Gabbay 
4307e65e175bSOded Gabbay 	case GAUDI_QUEUE_ID_DMA_5_0...GAUDI_QUEUE_ID_DMA_5_3:
4308e65e175bSOded Gabbay 		dma_id = gaudi_dma_assignment[GAUDI_HBM_DMA_4];
4309e65e175bSOded Gabbay 		dma_qm_offset = dma_id * DMA_QMAN_OFFSET;
4310e65e175bSOded Gabbay 		q_off = dma_qm_offset + ((hw_queue_id - 1) & 0x3) * 4;
4311e65e175bSOded Gabbay 		db_reg_offset = mmDMA0_QM_PQ_PI_0 + q_off;
4312e65e175bSOded Gabbay 		break;
4313e65e175bSOded Gabbay 
4314e65e175bSOded Gabbay 	case GAUDI_QUEUE_ID_DMA_6_0...GAUDI_QUEUE_ID_DMA_6_3:
4315e65e175bSOded Gabbay 		dma_id = gaudi_dma_assignment[GAUDI_HBM_DMA_5];
4316e65e175bSOded Gabbay 		dma_qm_offset = dma_id * DMA_QMAN_OFFSET;
4317e65e175bSOded Gabbay 		q_off = dma_qm_offset + ((hw_queue_id - 1) & 0x3) * 4;
4318e65e175bSOded Gabbay 		db_reg_offset = mmDMA0_QM_PQ_PI_0 + q_off;
4319e65e175bSOded Gabbay 		break;
4320e65e175bSOded Gabbay 
4321e65e175bSOded Gabbay 	case GAUDI_QUEUE_ID_DMA_7_0...GAUDI_QUEUE_ID_DMA_7_3:
4322e65e175bSOded Gabbay 		dma_id = gaudi_dma_assignment[GAUDI_HBM_DMA_6];
4323e65e175bSOded Gabbay 		dma_qm_offset = dma_id * DMA_QMAN_OFFSET;
4324e65e175bSOded Gabbay 		q_off = dma_qm_offset + ((hw_queue_id - 1) & 0x3) * 4;
4325e65e175bSOded Gabbay 		db_reg_offset = mmDMA0_QM_PQ_PI_0 + q_off;
4326e65e175bSOded Gabbay 		break;
4327e65e175bSOded Gabbay 
4328e65e175bSOded Gabbay 	case GAUDI_QUEUE_ID_CPU_PQ:
4329e65e175bSOded Gabbay 		if (gaudi->hw_cap_initialized & HW_CAP_CPU_Q)
4330e65e175bSOded Gabbay 			db_reg_offset = mmCPU_IF_PF_PQ_PI;
4331e65e175bSOded Gabbay 		else
4332e65e175bSOded Gabbay 			invalid_queue = true;
4333e65e175bSOded Gabbay 		break;
4334e65e175bSOded Gabbay 
4335e65e175bSOded Gabbay 	case GAUDI_QUEUE_ID_MME_0_0:
4336e65e175bSOded Gabbay 		db_reg_offset = mmMME2_QM_PQ_PI_0;
4337e65e175bSOded Gabbay 		break;
4338e65e175bSOded Gabbay 
4339e65e175bSOded Gabbay 	case GAUDI_QUEUE_ID_MME_0_1:
4340e65e175bSOded Gabbay 		db_reg_offset = mmMME2_QM_PQ_PI_1;
4341e65e175bSOded Gabbay 		break;
4342e65e175bSOded Gabbay 
4343e65e175bSOded Gabbay 	case GAUDI_QUEUE_ID_MME_0_2:
4344e65e175bSOded Gabbay 		db_reg_offset = mmMME2_QM_PQ_PI_2;
4345e65e175bSOded Gabbay 		break;
4346e65e175bSOded Gabbay 
4347e65e175bSOded Gabbay 	case GAUDI_QUEUE_ID_MME_0_3:
4348e65e175bSOded Gabbay 		db_reg_offset = mmMME2_QM_PQ_PI_3;
4349e65e175bSOded Gabbay 		break;
4350e65e175bSOded Gabbay 
4351e65e175bSOded Gabbay 	case GAUDI_QUEUE_ID_MME_1_0:
4352e65e175bSOded Gabbay 		db_reg_offset = mmMME0_QM_PQ_PI_0;
4353e65e175bSOded Gabbay 		break;
4354e65e175bSOded Gabbay 
4355e65e175bSOded Gabbay 	case GAUDI_QUEUE_ID_MME_1_1:
4356e65e175bSOded Gabbay 		db_reg_offset = mmMME0_QM_PQ_PI_1;
4357e65e175bSOded Gabbay 		break;
4358e65e175bSOded Gabbay 
4359e65e175bSOded Gabbay 	case GAUDI_QUEUE_ID_MME_1_2:
4360e65e175bSOded Gabbay 		db_reg_offset = mmMME0_QM_PQ_PI_2;
4361e65e175bSOded Gabbay 		break;
4362e65e175bSOded Gabbay 
4363e65e175bSOded Gabbay 	case GAUDI_QUEUE_ID_MME_1_3:
4364e65e175bSOded Gabbay 		db_reg_offset = mmMME0_QM_PQ_PI_3;
4365e65e175bSOded Gabbay 		break;
4366e65e175bSOded Gabbay 
4367e65e175bSOded Gabbay 	case GAUDI_QUEUE_ID_TPC_0_0:
4368e65e175bSOded Gabbay 		db_reg_offset = mmTPC0_QM_PQ_PI_0;
4369e65e175bSOded Gabbay 		break;
4370e65e175bSOded Gabbay 
4371e65e175bSOded Gabbay 	case GAUDI_QUEUE_ID_TPC_0_1:
4372e65e175bSOded Gabbay 		db_reg_offset = mmTPC0_QM_PQ_PI_1;
4373e65e175bSOded Gabbay 		break;
4374e65e175bSOded Gabbay 
4375e65e175bSOded Gabbay 	case GAUDI_QUEUE_ID_TPC_0_2:
4376e65e175bSOded Gabbay 		db_reg_offset = mmTPC0_QM_PQ_PI_2;
4377e65e175bSOded Gabbay 		break;
4378e65e175bSOded Gabbay 
4379e65e175bSOded Gabbay 	case GAUDI_QUEUE_ID_TPC_0_3:
4380e65e175bSOded Gabbay 		db_reg_offset = mmTPC0_QM_PQ_PI_3;
4381e65e175bSOded Gabbay 		break;
4382e65e175bSOded Gabbay 
4383e65e175bSOded Gabbay 	case GAUDI_QUEUE_ID_TPC_1_0:
4384e65e175bSOded Gabbay 		db_reg_offset = mmTPC1_QM_PQ_PI_0;
4385e65e175bSOded Gabbay 		break;
4386e65e175bSOded Gabbay 
4387e65e175bSOded Gabbay 	case GAUDI_QUEUE_ID_TPC_1_1:
4388e65e175bSOded Gabbay 		db_reg_offset = mmTPC1_QM_PQ_PI_1;
4389e65e175bSOded Gabbay 		break;
4390e65e175bSOded Gabbay 
4391e65e175bSOded Gabbay 	case GAUDI_QUEUE_ID_TPC_1_2:
4392e65e175bSOded Gabbay 		db_reg_offset = mmTPC1_QM_PQ_PI_2;
4393e65e175bSOded Gabbay 		break;
4394e65e175bSOded Gabbay 
4395e65e175bSOded Gabbay 	case GAUDI_QUEUE_ID_TPC_1_3:
4396e65e175bSOded Gabbay 		db_reg_offset = mmTPC1_QM_PQ_PI_3;
4397e65e175bSOded Gabbay 		break;
4398e65e175bSOded Gabbay 
4399e65e175bSOded Gabbay 	case GAUDI_QUEUE_ID_TPC_2_0:
4400e65e175bSOded Gabbay 		db_reg_offset = mmTPC2_QM_PQ_PI_0;
4401e65e175bSOded Gabbay 		break;
4402e65e175bSOded Gabbay 
4403e65e175bSOded Gabbay 	case GAUDI_QUEUE_ID_TPC_2_1:
4404e65e175bSOded Gabbay 		db_reg_offset = mmTPC2_QM_PQ_PI_1;
4405e65e175bSOded Gabbay 		break;
4406e65e175bSOded Gabbay 
4407e65e175bSOded Gabbay 	case GAUDI_QUEUE_ID_TPC_2_2:
4408e65e175bSOded Gabbay 		db_reg_offset = mmTPC2_QM_PQ_PI_2;
4409e65e175bSOded Gabbay 		break;
4410e65e175bSOded Gabbay 
4411e65e175bSOded Gabbay 	case GAUDI_QUEUE_ID_TPC_2_3:
4412e65e175bSOded Gabbay 		db_reg_offset = mmTPC2_QM_PQ_PI_3;
4413e65e175bSOded Gabbay 		break;
4414e65e175bSOded Gabbay 
4415e65e175bSOded Gabbay 	case GAUDI_QUEUE_ID_TPC_3_0:
4416e65e175bSOded Gabbay 		db_reg_offset = mmTPC3_QM_PQ_PI_0;
4417e65e175bSOded Gabbay 		break;
4418e65e175bSOded Gabbay 
4419e65e175bSOded Gabbay 	case GAUDI_QUEUE_ID_TPC_3_1:
4420e65e175bSOded Gabbay 		db_reg_offset = mmTPC3_QM_PQ_PI_1;
4421e65e175bSOded Gabbay 		break;
4422e65e175bSOded Gabbay 
4423e65e175bSOded Gabbay 	case GAUDI_QUEUE_ID_TPC_3_2:
4424e65e175bSOded Gabbay 		db_reg_offset = mmTPC3_QM_PQ_PI_2;
4425e65e175bSOded Gabbay 		break;
4426e65e175bSOded Gabbay 
4427e65e175bSOded Gabbay 	case GAUDI_QUEUE_ID_TPC_3_3:
4428e65e175bSOded Gabbay 		db_reg_offset = mmTPC3_QM_PQ_PI_3;
4429e65e175bSOded Gabbay 		break;
4430e65e175bSOded Gabbay 
4431e65e175bSOded Gabbay 	case GAUDI_QUEUE_ID_TPC_4_0:
4432e65e175bSOded Gabbay 		db_reg_offset = mmTPC4_QM_PQ_PI_0;
4433e65e175bSOded Gabbay 		break;
4434e65e175bSOded Gabbay 
4435e65e175bSOded Gabbay 	case GAUDI_QUEUE_ID_TPC_4_1:
4436e65e175bSOded Gabbay 		db_reg_offset = mmTPC4_QM_PQ_PI_1;
4437e65e175bSOded Gabbay 		break;
4438e65e175bSOded Gabbay 
4439e65e175bSOded Gabbay 	case GAUDI_QUEUE_ID_TPC_4_2:
4440e65e175bSOded Gabbay 		db_reg_offset = mmTPC4_QM_PQ_PI_2;
4441e65e175bSOded Gabbay 		break;
4442e65e175bSOded Gabbay 
4443e65e175bSOded Gabbay 	case GAUDI_QUEUE_ID_TPC_4_3:
4444e65e175bSOded Gabbay 		db_reg_offset = mmTPC4_QM_PQ_PI_3;
4445e65e175bSOded Gabbay 		break;
4446e65e175bSOded Gabbay 
4447e65e175bSOded Gabbay 	case GAUDI_QUEUE_ID_TPC_5_0:
4448e65e175bSOded Gabbay 		db_reg_offset = mmTPC5_QM_PQ_PI_0;
4449e65e175bSOded Gabbay 		break;
4450e65e175bSOded Gabbay 
4451e65e175bSOded Gabbay 	case GAUDI_QUEUE_ID_TPC_5_1:
4452e65e175bSOded Gabbay 		db_reg_offset = mmTPC5_QM_PQ_PI_1;
4453e65e175bSOded Gabbay 		break;
4454e65e175bSOded Gabbay 
4455e65e175bSOded Gabbay 	case GAUDI_QUEUE_ID_TPC_5_2:
4456e65e175bSOded Gabbay 		db_reg_offset = mmTPC5_QM_PQ_PI_2;
4457e65e175bSOded Gabbay 		break;
4458e65e175bSOded Gabbay 
4459e65e175bSOded Gabbay 	case GAUDI_QUEUE_ID_TPC_5_3:
4460e65e175bSOded Gabbay 		db_reg_offset = mmTPC5_QM_PQ_PI_3;
4461e65e175bSOded Gabbay 		break;
4462e65e175bSOded Gabbay 
4463e65e175bSOded Gabbay 	case GAUDI_QUEUE_ID_TPC_6_0:
4464e65e175bSOded Gabbay 		db_reg_offset = mmTPC6_QM_PQ_PI_0;
4465e65e175bSOded Gabbay 		break;
4466e65e175bSOded Gabbay 
4467e65e175bSOded Gabbay 	case GAUDI_QUEUE_ID_TPC_6_1:
4468e65e175bSOded Gabbay 		db_reg_offset = mmTPC6_QM_PQ_PI_1;
4469e65e175bSOded Gabbay 		break;
4470e65e175bSOded Gabbay 
4471e65e175bSOded Gabbay 	case GAUDI_QUEUE_ID_TPC_6_2:
4472e65e175bSOded Gabbay 		db_reg_offset = mmTPC6_QM_PQ_PI_2;
4473e65e175bSOded Gabbay 		break;
4474e65e175bSOded Gabbay 
4475e65e175bSOded Gabbay 	case GAUDI_QUEUE_ID_TPC_6_3:
4476e65e175bSOded Gabbay 		db_reg_offset = mmTPC6_QM_PQ_PI_3;
4477e65e175bSOded Gabbay 		break;
4478e65e175bSOded Gabbay 
4479e65e175bSOded Gabbay 	case GAUDI_QUEUE_ID_TPC_7_0:
4480e65e175bSOded Gabbay 		db_reg_offset = mmTPC7_QM_PQ_PI_0;
4481e65e175bSOded Gabbay 		break;
4482e65e175bSOded Gabbay 
4483e65e175bSOded Gabbay 	case GAUDI_QUEUE_ID_TPC_7_1:
4484e65e175bSOded Gabbay 		db_reg_offset = mmTPC7_QM_PQ_PI_1;
4485e65e175bSOded Gabbay 		break;
4486e65e175bSOded Gabbay 
4487e65e175bSOded Gabbay 	case GAUDI_QUEUE_ID_TPC_7_2:
4488e65e175bSOded Gabbay 		db_reg_offset = mmTPC7_QM_PQ_PI_2;
4489e65e175bSOded Gabbay 		break;
4490e65e175bSOded Gabbay 
4491e65e175bSOded Gabbay 	case GAUDI_QUEUE_ID_TPC_7_3:
4492e65e175bSOded Gabbay 		db_reg_offset = mmTPC7_QM_PQ_PI_3;
4493e65e175bSOded Gabbay 		break;
4494e65e175bSOded Gabbay 
4495e65e175bSOded Gabbay 	case GAUDI_QUEUE_ID_NIC_0_0...GAUDI_QUEUE_ID_NIC_0_3:
4496e65e175bSOded Gabbay 		if (!(gaudi->hw_cap_initialized & HW_CAP_NIC0))
4497e65e175bSOded Gabbay 			invalid_queue = true;
4498e65e175bSOded Gabbay 
4499e65e175bSOded Gabbay 		q_off = ((hw_queue_id - 1) & 0x3) * 4;
4500e65e175bSOded Gabbay 		db_reg_offset = mmNIC0_QM0_PQ_PI_0 + q_off;
4501e65e175bSOded Gabbay 		break;
4502e65e175bSOded Gabbay 
4503e65e175bSOded Gabbay 	case GAUDI_QUEUE_ID_NIC_1_0...GAUDI_QUEUE_ID_NIC_1_3:
4504e65e175bSOded Gabbay 		if (!(gaudi->hw_cap_initialized & HW_CAP_NIC1))
4505e65e175bSOded Gabbay 			invalid_queue = true;
4506e65e175bSOded Gabbay 
4507e65e175bSOded Gabbay 		q_off = ((hw_queue_id - 1) & 0x3) * 4;
4508e65e175bSOded Gabbay 		db_reg_offset = mmNIC0_QM1_PQ_PI_0 + q_off;
4509e65e175bSOded Gabbay 		break;
4510e65e175bSOded Gabbay 
4511e65e175bSOded Gabbay 	case GAUDI_QUEUE_ID_NIC_2_0...GAUDI_QUEUE_ID_NIC_2_3:
4512e65e175bSOded Gabbay 		if (!(gaudi->hw_cap_initialized & HW_CAP_NIC2))
4513e65e175bSOded Gabbay 			invalid_queue = true;
4514e65e175bSOded Gabbay 
4515e65e175bSOded Gabbay 		q_off = ((hw_queue_id - 1) & 0x3) * 4;
4516e65e175bSOded Gabbay 		db_reg_offset = mmNIC1_QM0_PQ_PI_0 + q_off;
4517e65e175bSOded Gabbay 		break;
4518e65e175bSOded Gabbay 
4519e65e175bSOded Gabbay 	case GAUDI_QUEUE_ID_NIC_3_0...GAUDI_QUEUE_ID_NIC_3_3:
4520e65e175bSOded Gabbay 		if (!(gaudi->hw_cap_initialized & HW_CAP_NIC3))
4521e65e175bSOded Gabbay 			invalid_queue = true;
4522e65e175bSOded Gabbay 
4523e65e175bSOded Gabbay 		q_off = ((hw_queue_id - 1) & 0x3) * 4;
4524e65e175bSOded Gabbay 		db_reg_offset = mmNIC1_QM1_PQ_PI_0 + q_off;
4525e65e175bSOded Gabbay 		break;
4526e65e175bSOded Gabbay 
4527e65e175bSOded Gabbay 	case GAUDI_QUEUE_ID_NIC_4_0...GAUDI_QUEUE_ID_NIC_4_3:
4528e65e175bSOded Gabbay 		if (!(gaudi->hw_cap_initialized & HW_CAP_NIC4))
4529e65e175bSOded Gabbay 			invalid_queue = true;
4530e65e175bSOded Gabbay 
4531e65e175bSOded Gabbay 		q_off = ((hw_queue_id - 1) & 0x3) * 4;
4532e65e175bSOded Gabbay 		db_reg_offset = mmNIC2_QM0_PQ_PI_0 + q_off;
4533e65e175bSOded Gabbay 		break;
4534e65e175bSOded Gabbay 
4535e65e175bSOded Gabbay 	case GAUDI_QUEUE_ID_NIC_5_0...GAUDI_QUEUE_ID_NIC_5_3:
4536e65e175bSOded Gabbay 		if (!(gaudi->hw_cap_initialized & HW_CAP_NIC5))
4537e65e175bSOded Gabbay 			invalid_queue = true;
4538e65e175bSOded Gabbay 
4539e65e175bSOded Gabbay 		q_off = ((hw_queue_id - 1) & 0x3) * 4;
4540e65e175bSOded Gabbay 		db_reg_offset = mmNIC2_QM1_PQ_PI_0 + q_off;
4541e65e175bSOded Gabbay 		break;
4542e65e175bSOded Gabbay 
4543e65e175bSOded Gabbay 	case GAUDI_QUEUE_ID_NIC_6_0...GAUDI_QUEUE_ID_NIC_6_3:
4544e65e175bSOded Gabbay 		if (!(gaudi->hw_cap_initialized & HW_CAP_NIC6))
4545e65e175bSOded Gabbay 			invalid_queue = true;
4546e65e175bSOded Gabbay 
4547e65e175bSOded Gabbay 		q_off = ((hw_queue_id - 1) & 0x3) * 4;
4548e65e175bSOded Gabbay 		db_reg_offset = mmNIC3_QM0_PQ_PI_0 + q_off;
4549e65e175bSOded Gabbay 		break;
4550e65e175bSOded Gabbay 
4551e65e175bSOded Gabbay 	case GAUDI_QUEUE_ID_NIC_7_0...GAUDI_QUEUE_ID_NIC_7_3:
4552e65e175bSOded Gabbay 		if (!(gaudi->hw_cap_initialized & HW_CAP_NIC7))
4553e65e175bSOded Gabbay 			invalid_queue = true;
4554e65e175bSOded Gabbay 
4555e65e175bSOded Gabbay 		q_off = ((hw_queue_id - 1) & 0x3) * 4;
4556e65e175bSOded Gabbay 		db_reg_offset = mmNIC3_QM1_PQ_PI_0 + q_off;
4557e65e175bSOded Gabbay 		break;
4558e65e175bSOded Gabbay 
4559e65e175bSOded Gabbay 	case GAUDI_QUEUE_ID_NIC_8_0...GAUDI_QUEUE_ID_NIC_8_3:
4560e65e175bSOded Gabbay 		if (!(gaudi->hw_cap_initialized & HW_CAP_NIC8))
4561e65e175bSOded Gabbay 			invalid_queue = true;
4562e65e175bSOded Gabbay 
4563e65e175bSOded Gabbay 		q_off = ((hw_queue_id - 1) & 0x3) * 4;
4564e65e175bSOded Gabbay 		db_reg_offset = mmNIC4_QM0_PQ_PI_0 + q_off;
4565e65e175bSOded Gabbay 		break;
4566e65e175bSOded Gabbay 
4567e65e175bSOded Gabbay 	case GAUDI_QUEUE_ID_NIC_9_0...GAUDI_QUEUE_ID_NIC_9_3:
4568e65e175bSOded Gabbay 		if (!(gaudi->hw_cap_initialized & HW_CAP_NIC9))
4569e65e175bSOded Gabbay 			invalid_queue = true;
4570e65e175bSOded Gabbay 
4571e65e175bSOded Gabbay 		q_off = ((hw_queue_id - 1) & 0x3) * 4;
4572e65e175bSOded Gabbay 		db_reg_offset = mmNIC4_QM1_PQ_PI_0 + q_off;
4573e65e175bSOded Gabbay 		break;
4574e65e175bSOded Gabbay 
4575e65e175bSOded Gabbay 	default:
4576e65e175bSOded Gabbay 		invalid_queue = true;
4577e65e175bSOded Gabbay 	}
4578e65e175bSOded Gabbay 
4579e65e175bSOded Gabbay 	if (invalid_queue) {
4580e65e175bSOded Gabbay 		/* Should never get here */
4581e65e175bSOded Gabbay 		dev_err(hdev->dev, "h/w queue %d is invalid. Can't set pi\n",
4582e65e175bSOded Gabbay 			hw_queue_id);
4583e65e175bSOded Gabbay 		return;
4584e65e175bSOded Gabbay 	}
4585e65e175bSOded Gabbay 
4586e65e175bSOded Gabbay 	db_value = pi;
4587e65e175bSOded Gabbay 
4588e65e175bSOded Gabbay 	/* ring the doorbell */
4589e65e175bSOded Gabbay 	WREG32(db_reg_offset, db_value);
4590e65e175bSOded Gabbay 
4591e65e175bSOded Gabbay 	if (hw_queue_id == GAUDI_QUEUE_ID_CPU_PQ) {
4592e65e175bSOded Gabbay 		/* make sure device CPU will read latest data from host */
4593e65e175bSOded Gabbay 		mb();
4594e65e175bSOded Gabbay 
4595e65e175bSOded Gabbay 		irq_handler_offset = hdev->asic_prop.gic_interrupts_enable ?
4596e65e175bSOded Gabbay 				mmGIC_DISTRIBUTOR__5_GICD_SETSPI_NSR :
4597e65e175bSOded Gabbay 				le32_to_cpu(dyn_regs->gic_host_pi_upd_irq);
4598e65e175bSOded Gabbay 
4599e65e175bSOded Gabbay 		WREG32(irq_handler_offset,
4600e65e175bSOded Gabbay 			gaudi_irq_map_table[GAUDI_EVENT_PI_UPDATE].cpu_id);
4601e65e175bSOded Gabbay 	}
4602e65e175bSOded Gabbay }
4603e65e175bSOded Gabbay 
4604e65e175bSOded Gabbay static void gaudi_pqe_write(struct hl_device *hdev, __le64 *pqe,
4605e65e175bSOded Gabbay 				struct hl_bd *bd)
4606e65e175bSOded Gabbay {
4607e65e175bSOded Gabbay 	__le64 *pbd = (__le64 *) bd;
4608e65e175bSOded Gabbay 
4609e65e175bSOded Gabbay 	/* The QMANs are on the host memory so a simple copy suffice */
4610e65e175bSOded Gabbay 	pqe[0] = pbd[0];
4611e65e175bSOded Gabbay 	pqe[1] = pbd[1];
4612e65e175bSOded Gabbay }
4613e65e175bSOded Gabbay 
4614e65e175bSOded Gabbay static void *gaudi_dma_alloc_coherent(struct hl_device *hdev, size_t size,
4615e65e175bSOded Gabbay 					dma_addr_t *dma_handle, gfp_t flags)
4616e65e175bSOded Gabbay {
4617e65e175bSOded Gabbay 	void *kernel_addr = dma_alloc_coherent(&hdev->pdev->dev, size,
4618e65e175bSOded Gabbay 						dma_handle, flags);
4619e65e175bSOded Gabbay 
4620e65e175bSOded Gabbay 	/* Shift to the device's base physical address of host memory */
4621e65e175bSOded Gabbay 	if (kernel_addr)
4622e65e175bSOded Gabbay 		*dma_handle += HOST_PHYS_BASE;
4623e65e175bSOded Gabbay 
4624e65e175bSOded Gabbay 	return kernel_addr;
4625e65e175bSOded Gabbay }
4626e65e175bSOded Gabbay 
4627e65e175bSOded Gabbay static void gaudi_dma_free_coherent(struct hl_device *hdev, size_t size,
4628e65e175bSOded Gabbay 		void *cpu_addr, dma_addr_t dma_handle)
4629e65e175bSOded Gabbay {
4630e65e175bSOded Gabbay 	/* Cancel the device's base physical address of host memory */
4631e65e175bSOded Gabbay 	dma_addr_t fixed_dma_handle = dma_handle - HOST_PHYS_BASE;
4632e65e175bSOded Gabbay 
4633e65e175bSOded Gabbay 	dma_free_coherent(&hdev->pdev->dev, size, cpu_addr, fixed_dma_handle);
4634e65e175bSOded Gabbay }
4635e65e175bSOded Gabbay 
4636e65e175bSOded Gabbay static int gaudi_scrub_device_dram(struct hl_device *hdev, u64 val)
4637e65e175bSOded Gabbay {
4638e65e175bSOded Gabbay 	struct asic_fixed_properties *prop = &hdev->asic_prop;
4639e65e175bSOded Gabbay 	u64 cur_addr = prop->dram_user_base_address;
4640e65e175bSOded Gabbay 	u32 chunk_size, busy;
4641e65e175bSOded Gabbay 	int rc, dma_id;
4642e65e175bSOded Gabbay 
4643e65e175bSOded Gabbay 	while (cur_addr < prop->dram_end_address) {
4644e65e175bSOded Gabbay 		for (dma_id = 0 ; dma_id < DMA_NUMBER_OF_CHANNELS ; dma_id++) {
4645e65e175bSOded Gabbay 			u32 dma_offset = dma_id * DMA_CORE_OFFSET;
4646e65e175bSOded Gabbay 
4647e65e175bSOded Gabbay 			chunk_size =
4648e65e175bSOded Gabbay 			min((u64)SZ_2G, prop->dram_end_address - cur_addr);
4649e65e175bSOded Gabbay 
4650e65e175bSOded Gabbay 			dev_dbg(hdev->dev,
4651e65e175bSOded Gabbay 				"Doing HBM scrubbing for 0x%09llx - 0x%09llx\n",
4652e65e175bSOded Gabbay 				cur_addr, cur_addr + chunk_size);
4653e65e175bSOded Gabbay 
4654e65e175bSOded Gabbay 			WREG32(mmDMA0_CORE_SRC_BASE_LO + dma_offset,
4655e65e175bSOded Gabbay 					lower_32_bits(val));
4656e65e175bSOded Gabbay 			WREG32(mmDMA0_CORE_SRC_BASE_HI + dma_offset,
4657e65e175bSOded Gabbay 					upper_32_bits(val));
4658e65e175bSOded Gabbay 			WREG32(mmDMA0_CORE_DST_BASE_LO + dma_offset,
4659e65e175bSOded Gabbay 						lower_32_bits(cur_addr));
4660e65e175bSOded Gabbay 			WREG32(mmDMA0_CORE_DST_BASE_HI + dma_offset,
4661e65e175bSOded Gabbay 						upper_32_bits(cur_addr));
4662e65e175bSOded Gabbay 			WREG32(mmDMA0_CORE_DST_TSIZE_0 + dma_offset,
4663e65e175bSOded Gabbay 					chunk_size);
4664e65e175bSOded Gabbay 			WREG32(mmDMA0_CORE_COMMIT + dma_offset,
4665e65e175bSOded Gabbay 					((1 << DMA0_CORE_COMMIT_LIN_SHIFT) |
4666e65e175bSOded Gabbay 					(1 << DMA0_CORE_COMMIT_MEM_SET_SHIFT)));
4667e65e175bSOded Gabbay 
4668e65e175bSOded Gabbay 			cur_addr += chunk_size;
4669e65e175bSOded Gabbay 
4670e65e175bSOded Gabbay 			if (cur_addr == prop->dram_end_address)
4671e65e175bSOded Gabbay 				break;
4672e65e175bSOded Gabbay 		}
4673e65e175bSOded Gabbay 
4674e65e175bSOded Gabbay 		for (dma_id = 0 ; dma_id < DMA_NUMBER_OF_CHANNELS ; dma_id++) {
4675e65e175bSOded Gabbay 			u32 dma_offset = dma_id * DMA_CORE_OFFSET;
4676e65e175bSOded Gabbay 
4677e65e175bSOded Gabbay 			rc = hl_poll_timeout(
4678e65e175bSOded Gabbay 				hdev,
4679e65e175bSOded Gabbay 				mmDMA0_CORE_STS0 + dma_offset,
4680e65e175bSOded Gabbay 				busy,
4681e65e175bSOded Gabbay 				((busy & DMA0_CORE_STS0_BUSY_MASK) == 0),
4682e65e175bSOded Gabbay 				1000,
4683e65e175bSOded Gabbay 				HBM_SCRUBBING_TIMEOUT_US);
4684e65e175bSOded Gabbay 
4685e65e175bSOded Gabbay 			if (rc) {
4686e65e175bSOded Gabbay 				dev_err(hdev->dev,
4687e65e175bSOded Gabbay 					"DMA Timeout during HBM scrubbing of DMA #%d\n",
4688e65e175bSOded Gabbay 					dma_id);
4689e65e175bSOded Gabbay 				return -EIO;
4690e65e175bSOded Gabbay 			}
4691e65e175bSOded Gabbay 		}
4692e65e175bSOded Gabbay 	}
4693e65e175bSOded Gabbay 
4694e65e175bSOded Gabbay 	return 0;
4695e65e175bSOded Gabbay }
4696e65e175bSOded Gabbay 
4697e65e175bSOded Gabbay static int gaudi_scrub_device_mem(struct hl_device *hdev)
4698e65e175bSOded Gabbay {
4699e65e175bSOded Gabbay 	struct asic_fixed_properties *prop = &hdev->asic_prop;
4700e65e175bSOded Gabbay 	u64 wait_to_idle_time = hdev->pdev ? HBM_SCRUBBING_TIMEOUT_US :
4701e65e175bSOded Gabbay 			min_t(u64, HBM_SCRUBBING_TIMEOUT_US * 10, HL_SIM_MAX_TIMEOUT_US);
4702e65e175bSOded Gabbay 	u64 addr, size, val = hdev->memory_scrub_val;
4703e65e175bSOded Gabbay 	ktime_t timeout;
4704e65e175bSOded Gabbay 	int rc = 0;
4705e65e175bSOded Gabbay 
4706e65e175bSOded Gabbay 	if (!hdev->memory_scrub)
4707e65e175bSOded Gabbay 		return 0;
4708e65e175bSOded Gabbay 
4709e65e175bSOded Gabbay 	timeout = ktime_add_us(ktime_get(), wait_to_idle_time);
4710e65e175bSOded Gabbay 	while (!hdev->asic_funcs->is_device_idle(hdev, NULL, 0, NULL)) {
4711e65e175bSOded Gabbay 		if (ktime_compare(ktime_get(), timeout) > 0) {
4712e65e175bSOded Gabbay 			dev_err(hdev->dev, "waiting for idle timeout\n");
4713e65e175bSOded Gabbay 			return -ETIMEDOUT;
4714e65e175bSOded Gabbay 		}
4715e65e175bSOded Gabbay 		usleep_range((1000 >> 2) + 1, 1000);
4716e65e175bSOded Gabbay 	}
4717e65e175bSOded Gabbay 
4718e65e175bSOded Gabbay 	/* Scrub SRAM */
4719e65e175bSOded Gabbay 	addr = prop->sram_user_base_address;
4720e65e175bSOded Gabbay 	size = hdev->pldm ? 0x10000 : prop->sram_size - SRAM_USER_BASE_OFFSET;
4721e65e175bSOded Gabbay 
4722e65e175bSOded Gabbay 	dev_dbg(hdev->dev, "Scrubbing SRAM: 0x%09llx - 0x%09llx val: 0x%llx\n",
4723e65e175bSOded Gabbay 			addr, addr + size, val);
4724e65e175bSOded Gabbay 	rc = gaudi_memset_device_memory(hdev, addr, size, val);
4725e65e175bSOded Gabbay 	if (rc) {
4726e65e175bSOded Gabbay 		dev_err(hdev->dev, "Failed to clear SRAM (%d)\n", rc);
4727e65e175bSOded Gabbay 		return rc;
4728e65e175bSOded Gabbay 	}
4729e65e175bSOded Gabbay 
4730e65e175bSOded Gabbay 	/* Scrub HBM using all DMA channels in parallel */
4731e65e175bSOded Gabbay 	rc = gaudi_scrub_device_dram(hdev, val);
4732e65e175bSOded Gabbay 	if (rc) {
4733e65e175bSOded Gabbay 		dev_err(hdev->dev, "Failed to clear HBM (%d)\n", rc);
4734e65e175bSOded Gabbay 		return rc;
4735e65e175bSOded Gabbay 	}
4736e65e175bSOded Gabbay 
4737e65e175bSOded Gabbay 	return 0;
4738e65e175bSOded Gabbay }
4739e65e175bSOded Gabbay 
4740e65e175bSOded Gabbay static void *gaudi_get_int_queue_base(struct hl_device *hdev,
4741e65e175bSOded Gabbay 				u32 queue_id, dma_addr_t *dma_handle,
4742e65e175bSOded Gabbay 				u16 *queue_len)
4743e65e175bSOded Gabbay {
4744e65e175bSOded Gabbay 	struct gaudi_device *gaudi = hdev->asic_specific;
4745e65e175bSOded Gabbay 	struct gaudi_internal_qman_info *q;
4746e65e175bSOded Gabbay 
4747e65e175bSOded Gabbay 	if (queue_id >= GAUDI_QUEUE_ID_SIZE ||
4748e65e175bSOded Gabbay 			gaudi_queue_type[queue_id] != QUEUE_TYPE_INT) {
4749e65e175bSOded Gabbay 		dev_err(hdev->dev, "Got invalid queue id %d\n", queue_id);
4750e65e175bSOded Gabbay 		return NULL;
4751e65e175bSOded Gabbay 	}
4752e65e175bSOded Gabbay 
4753e65e175bSOded Gabbay 	q = &gaudi->internal_qmans[queue_id];
4754e65e175bSOded Gabbay 	*dma_handle = q->pq_dma_addr;
4755e65e175bSOded Gabbay 	*queue_len = q->pq_size / QMAN_PQ_ENTRY_SIZE;
4756e65e175bSOded Gabbay 
4757e65e175bSOded Gabbay 	return q->pq_kernel_addr;
4758e65e175bSOded Gabbay }
4759e65e175bSOded Gabbay 
4760e65e175bSOded Gabbay static int gaudi_send_cpu_message(struct hl_device *hdev, u32 *msg,
4761e65e175bSOded Gabbay 				u16 len, u32 timeout, u64 *result)
4762e65e175bSOded Gabbay {
4763e65e175bSOded Gabbay 	struct gaudi_device *gaudi = hdev->asic_specific;
4764e65e175bSOded Gabbay 
4765e65e175bSOded Gabbay 	if (!(gaudi->hw_cap_initialized & HW_CAP_CPU_Q)) {
4766e65e175bSOded Gabbay 		if (result)
4767e65e175bSOded Gabbay 			*result = 0;
4768e65e175bSOded Gabbay 		return 0;
4769e65e175bSOded Gabbay 	}
4770e65e175bSOded Gabbay 
4771e65e175bSOded Gabbay 	if (!timeout)
4772e65e175bSOded Gabbay 		timeout = GAUDI_MSG_TO_CPU_TIMEOUT_USEC;
4773e65e175bSOded Gabbay 
4774e65e175bSOded Gabbay 	return hl_fw_send_cpu_message(hdev, GAUDI_QUEUE_ID_CPU_PQ, msg, len,
4775e65e175bSOded Gabbay 						timeout, result);
4776e65e175bSOded Gabbay }
4777e65e175bSOded Gabbay 
4778e65e175bSOded Gabbay static int gaudi_test_queue(struct hl_device *hdev, u32 hw_queue_id)
4779e65e175bSOded Gabbay {
4780e65e175bSOded Gabbay 	struct packet_msg_prot *fence_pkt;
4781e65e175bSOded Gabbay 	dma_addr_t pkt_dma_addr;
4782e65e175bSOded Gabbay 	u32 fence_val, tmp, timeout_usec;
4783e65e175bSOded Gabbay 	dma_addr_t fence_dma_addr;
4784e65e175bSOded Gabbay 	u32 *fence_ptr;
4785e65e175bSOded Gabbay 	int rc;
4786e65e175bSOded Gabbay 
4787e65e175bSOded Gabbay 	if (hdev->pldm)
4788e65e175bSOded Gabbay 		timeout_usec = GAUDI_PLDM_TEST_QUEUE_WAIT_USEC;
4789e65e175bSOded Gabbay 	else
4790e65e175bSOded Gabbay 		timeout_usec = GAUDI_TEST_QUEUE_WAIT_USEC;
4791e65e175bSOded Gabbay 
4792e65e175bSOded Gabbay 	fence_val = GAUDI_QMAN0_FENCE_VAL;
4793e65e175bSOded Gabbay 
4794e65e175bSOded Gabbay 	fence_ptr = hl_asic_dma_pool_zalloc(hdev, 4, GFP_KERNEL, &fence_dma_addr);
4795e65e175bSOded Gabbay 	if (!fence_ptr) {
4796e65e175bSOded Gabbay 		dev_err(hdev->dev,
4797e65e175bSOded Gabbay 			"Failed to allocate memory for H/W queue %d testing\n",
4798e65e175bSOded Gabbay 			hw_queue_id);
4799e65e175bSOded Gabbay 		return -ENOMEM;
4800e65e175bSOded Gabbay 	}
4801e65e175bSOded Gabbay 
4802e65e175bSOded Gabbay 	*fence_ptr = 0;
4803e65e175bSOded Gabbay 
4804e65e175bSOded Gabbay 	fence_pkt = hl_asic_dma_pool_zalloc(hdev, sizeof(struct packet_msg_prot), GFP_KERNEL,
4805e65e175bSOded Gabbay 						&pkt_dma_addr);
4806e65e175bSOded Gabbay 	if (!fence_pkt) {
4807e65e175bSOded Gabbay 		dev_err(hdev->dev,
4808e65e175bSOded Gabbay 			"Failed to allocate packet for H/W queue %d testing\n",
4809e65e175bSOded Gabbay 			hw_queue_id);
4810e65e175bSOded Gabbay 		rc = -ENOMEM;
4811e65e175bSOded Gabbay 		goto free_fence_ptr;
4812e65e175bSOded Gabbay 	}
4813e65e175bSOded Gabbay 
4814e65e175bSOded Gabbay 	tmp = FIELD_PREP(GAUDI_PKT_CTL_OPCODE_MASK, PACKET_MSG_PROT);
4815e65e175bSOded Gabbay 	tmp |= FIELD_PREP(GAUDI_PKT_CTL_EB_MASK, 1);
4816e65e175bSOded Gabbay 	tmp |= FIELD_PREP(GAUDI_PKT_CTL_MB_MASK, 1);
4817e65e175bSOded Gabbay 
4818e65e175bSOded Gabbay 	fence_pkt->ctl = cpu_to_le32(tmp);
4819e65e175bSOded Gabbay 	fence_pkt->value = cpu_to_le32(fence_val);
4820e65e175bSOded Gabbay 	fence_pkt->addr = cpu_to_le64(fence_dma_addr);
4821e65e175bSOded Gabbay 
4822e65e175bSOded Gabbay 	rc = hl_hw_queue_send_cb_no_cmpl(hdev, hw_queue_id,
4823e65e175bSOded Gabbay 					sizeof(struct packet_msg_prot),
4824e65e175bSOded Gabbay 					pkt_dma_addr);
4825e65e175bSOded Gabbay 	if (rc) {
4826e65e175bSOded Gabbay 		dev_err(hdev->dev,
4827e65e175bSOded Gabbay 			"Failed to send fence packet to H/W queue %d\n",
4828e65e175bSOded Gabbay 			hw_queue_id);
4829e65e175bSOded Gabbay 		goto free_pkt;
4830e65e175bSOded Gabbay 	}
4831e65e175bSOded Gabbay 
4832e65e175bSOded Gabbay 	rc = hl_poll_timeout_memory(hdev, fence_ptr, tmp, (tmp == fence_val),
4833e65e175bSOded Gabbay 					1000, timeout_usec, true);
4834e65e175bSOded Gabbay 
4835e65e175bSOded Gabbay 	hl_hw_queue_inc_ci_kernel(hdev, hw_queue_id);
4836e65e175bSOded Gabbay 
4837e65e175bSOded Gabbay 	if (rc == -ETIMEDOUT) {
4838e65e175bSOded Gabbay 		dev_err(hdev->dev,
4839e65e175bSOded Gabbay 			"H/W queue %d test failed (scratch(0x%08llX) == 0x%08X)\n",
4840e65e175bSOded Gabbay 			hw_queue_id, (unsigned long long) fence_dma_addr, tmp);
4841e65e175bSOded Gabbay 		rc = -EIO;
4842e65e175bSOded Gabbay 	}
4843e65e175bSOded Gabbay 
4844e65e175bSOded Gabbay free_pkt:
4845e65e175bSOded Gabbay 	hl_asic_dma_pool_free(hdev, (void *) fence_pkt, pkt_dma_addr);
4846e65e175bSOded Gabbay free_fence_ptr:
4847e65e175bSOded Gabbay 	hl_asic_dma_pool_free(hdev, (void *) fence_ptr, fence_dma_addr);
4848e65e175bSOded Gabbay 	return rc;
4849e65e175bSOded Gabbay }
4850e65e175bSOded Gabbay 
4851e65e175bSOded Gabbay static int gaudi_test_cpu_queue(struct hl_device *hdev)
4852e65e175bSOded Gabbay {
4853e65e175bSOded Gabbay 	struct gaudi_device *gaudi = hdev->asic_specific;
4854e65e175bSOded Gabbay 
4855e65e175bSOded Gabbay 	/*
4856e65e175bSOded Gabbay 	 * check capability here as send_cpu_message() won't update the result
4857e65e175bSOded Gabbay 	 * value if no capability
4858e65e175bSOded Gabbay 	 */
4859e65e175bSOded Gabbay 	if (!(gaudi->hw_cap_initialized & HW_CAP_CPU_Q))
4860e65e175bSOded Gabbay 		return 0;
4861e65e175bSOded Gabbay 
4862e65e175bSOded Gabbay 	return hl_fw_test_cpu_queue(hdev);
4863e65e175bSOded Gabbay }
4864e65e175bSOded Gabbay 
4865e65e175bSOded Gabbay static int gaudi_test_queues(struct hl_device *hdev)
4866e65e175bSOded Gabbay {
4867e65e175bSOded Gabbay 	int i, rc, ret_val = 0;
4868e65e175bSOded Gabbay 
4869e65e175bSOded Gabbay 	for (i = 0 ; i < hdev->asic_prop.max_queues ; i++) {
4870e65e175bSOded Gabbay 		if (hdev->asic_prop.hw_queues_props[i].type == QUEUE_TYPE_EXT) {
4871e65e175bSOded Gabbay 			rc = gaudi_test_queue(hdev, i);
4872e65e175bSOded Gabbay 			if (rc)
4873e65e175bSOded Gabbay 				ret_val = -EINVAL;
4874e65e175bSOded Gabbay 		}
4875e65e175bSOded Gabbay 	}
4876e65e175bSOded Gabbay 
4877e65e175bSOded Gabbay 	rc = gaudi_test_cpu_queue(hdev);
4878e65e175bSOded Gabbay 	if (rc)
4879e65e175bSOded Gabbay 		ret_val = -EINVAL;
4880e65e175bSOded Gabbay 
4881e65e175bSOded Gabbay 	return ret_val;
4882e65e175bSOded Gabbay }
4883e65e175bSOded Gabbay 
4884e65e175bSOded Gabbay static void *gaudi_dma_pool_zalloc(struct hl_device *hdev, size_t size,
4885e65e175bSOded Gabbay 		gfp_t mem_flags, dma_addr_t *dma_handle)
4886e65e175bSOded Gabbay {
4887e65e175bSOded Gabbay 	void *kernel_addr;
4888e65e175bSOded Gabbay 
4889e65e175bSOded Gabbay 	if (size > GAUDI_DMA_POOL_BLK_SIZE)
4890e65e175bSOded Gabbay 		return NULL;
4891e65e175bSOded Gabbay 
4892e65e175bSOded Gabbay 	kernel_addr = dma_pool_zalloc(hdev->dma_pool, mem_flags, dma_handle);
4893e65e175bSOded Gabbay 
4894e65e175bSOded Gabbay 	/* Shift to the device's base physical address of host memory */
4895e65e175bSOded Gabbay 	if (kernel_addr)
4896e65e175bSOded Gabbay 		*dma_handle += HOST_PHYS_BASE;
4897e65e175bSOded Gabbay 
4898e65e175bSOded Gabbay 	return kernel_addr;
4899e65e175bSOded Gabbay }
4900e65e175bSOded Gabbay 
4901e65e175bSOded Gabbay static void gaudi_dma_pool_free(struct hl_device *hdev, void *vaddr,
4902e65e175bSOded Gabbay 			dma_addr_t dma_addr)
4903e65e175bSOded Gabbay {
4904e65e175bSOded Gabbay 	/* Cancel the device's base physical address of host memory */
4905e65e175bSOded Gabbay 	dma_addr_t fixed_dma_addr = dma_addr - HOST_PHYS_BASE;
4906e65e175bSOded Gabbay 
4907e65e175bSOded Gabbay 	dma_pool_free(hdev->dma_pool, vaddr, fixed_dma_addr);
4908e65e175bSOded Gabbay }
4909e65e175bSOded Gabbay 
4910e65e175bSOded Gabbay static void *gaudi_cpu_accessible_dma_pool_alloc(struct hl_device *hdev,
4911e65e175bSOded Gabbay 					size_t size, dma_addr_t *dma_handle)
4912e65e175bSOded Gabbay {
4913e65e175bSOded Gabbay 	return hl_fw_cpu_accessible_dma_pool_alloc(hdev, size, dma_handle);
4914e65e175bSOded Gabbay }
4915e65e175bSOded Gabbay 
4916e65e175bSOded Gabbay static void gaudi_cpu_accessible_dma_pool_free(struct hl_device *hdev,
4917e65e175bSOded Gabbay 						size_t size, void *vaddr)
4918e65e175bSOded Gabbay {
4919e65e175bSOded Gabbay 	hl_fw_cpu_accessible_dma_pool_free(hdev, size, vaddr);
4920e65e175bSOded Gabbay }
4921e65e175bSOded Gabbay 
4922e65e175bSOded Gabbay static u32 gaudi_get_dma_desc_list_size(struct hl_device *hdev, struct sg_table *sgt)
4923e65e175bSOded Gabbay {
4924e65e175bSOded Gabbay 	struct scatterlist *sg, *sg_next_iter;
4925e65e175bSOded Gabbay 	u32 count, dma_desc_cnt;
4926e65e175bSOded Gabbay 	u64 len, len_next;
4927e65e175bSOded Gabbay 	dma_addr_t addr, addr_next;
4928e65e175bSOded Gabbay 
4929e65e175bSOded Gabbay 	dma_desc_cnt = 0;
4930e65e175bSOded Gabbay 
4931e65e175bSOded Gabbay 	for_each_sgtable_dma_sg(sgt, sg, count) {
4932e65e175bSOded Gabbay 		len = sg_dma_len(sg);
4933e65e175bSOded Gabbay 		addr = sg_dma_address(sg);
4934e65e175bSOded Gabbay 
4935e65e175bSOded Gabbay 		if (len == 0)
4936e65e175bSOded Gabbay 			break;
4937e65e175bSOded Gabbay 
4938e65e175bSOded Gabbay 		while ((count + 1) < sgt->nents) {
4939e65e175bSOded Gabbay 			sg_next_iter = sg_next(sg);
4940e65e175bSOded Gabbay 			len_next = sg_dma_len(sg_next_iter);
4941e65e175bSOded Gabbay 			addr_next = sg_dma_address(sg_next_iter);
4942e65e175bSOded Gabbay 
4943e65e175bSOded Gabbay 			if (len_next == 0)
4944e65e175bSOded Gabbay 				break;
4945e65e175bSOded Gabbay 
4946e65e175bSOded Gabbay 			if ((addr + len == addr_next) &&
4947e65e175bSOded Gabbay 				(len + len_next <= DMA_MAX_TRANSFER_SIZE)) {
4948e65e175bSOded Gabbay 				len += len_next;
4949e65e175bSOded Gabbay 				count++;
4950e65e175bSOded Gabbay 				sg = sg_next_iter;
4951e65e175bSOded Gabbay 			} else {
4952e65e175bSOded Gabbay 				break;
4953e65e175bSOded Gabbay 			}
4954e65e175bSOded Gabbay 		}
4955e65e175bSOded Gabbay 
4956e65e175bSOded Gabbay 		dma_desc_cnt++;
4957e65e175bSOded Gabbay 	}
4958e65e175bSOded Gabbay 
4959e65e175bSOded Gabbay 	return dma_desc_cnt * sizeof(struct packet_lin_dma);
4960e65e175bSOded Gabbay }
4961e65e175bSOded Gabbay 
4962e65e175bSOded Gabbay static int gaudi_pin_memory_before_cs(struct hl_device *hdev,
4963e65e175bSOded Gabbay 				struct hl_cs_parser *parser,
4964e65e175bSOded Gabbay 				struct packet_lin_dma *user_dma_pkt,
4965e65e175bSOded Gabbay 				u64 addr, enum dma_data_direction dir)
4966e65e175bSOded Gabbay {
4967e65e175bSOded Gabbay 	struct hl_userptr *userptr;
4968e65e175bSOded Gabbay 	int rc;
4969e65e175bSOded Gabbay 
4970e65e175bSOded Gabbay 	if (hl_userptr_is_pinned(hdev, addr, le32_to_cpu(user_dma_pkt->tsize),
4971e65e175bSOded Gabbay 			parser->job_userptr_list, &userptr))
4972e65e175bSOded Gabbay 		goto already_pinned;
4973e65e175bSOded Gabbay 
4974e65e175bSOded Gabbay 	userptr = kzalloc(sizeof(*userptr), GFP_KERNEL);
4975e65e175bSOded Gabbay 	if (!userptr)
4976e65e175bSOded Gabbay 		return -ENOMEM;
4977e65e175bSOded Gabbay 
4978e65e175bSOded Gabbay 	rc = hl_pin_host_memory(hdev, addr, le32_to_cpu(user_dma_pkt->tsize),
4979e65e175bSOded Gabbay 				userptr);
4980e65e175bSOded Gabbay 	if (rc)
4981e65e175bSOded Gabbay 		goto free_userptr;
4982e65e175bSOded Gabbay 
4983e65e175bSOded Gabbay 	list_add_tail(&userptr->job_node, parser->job_userptr_list);
4984e65e175bSOded Gabbay 
4985e65e175bSOded Gabbay 	rc = hdev->asic_funcs->asic_dma_map_sgtable(hdev, userptr->sgt, dir);
4986e65e175bSOded Gabbay 	if (rc) {
4987e65e175bSOded Gabbay 		dev_err(hdev->dev, "failed to map sgt with DMA region\n");
4988e65e175bSOded Gabbay 		goto unpin_memory;
4989e65e175bSOded Gabbay 	}
4990e65e175bSOded Gabbay 
4991e65e175bSOded Gabbay 	userptr->dma_mapped = true;
4992e65e175bSOded Gabbay 	userptr->dir = dir;
4993e65e175bSOded Gabbay 
4994e65e175bSOded Gabbay already_pinned:
4995e65e175bSOded Gabbay 	parser->patched_cb_size +=
4996e65e175bSOded Gabbay 			gaudi_get_dma_desc_list_size(hdev, userptr->sgt);
4997e65e175bSOded Gabbay 
4998e65e175bSOded Gabbay 	return 0;
4999e65e175bSOded Gabbay 
5000e65e175bSOded Gabbay unpin_memory:
5001e65e175bSOded Gabbay 	list_del(&userptr->job_node);
5002e65e175bSOded Gabbay 	hl_unpin_host_memory(hdev, userptr);
5003e65e175bSOded Gabbay free_userptr:
5004e65e175bSOded Gabbay 	kfree(userptr);
5005e65e175bSOded Gabbay 	return rc;
5006e65e175bSOded Gabbay }
5007e65e175bSOded Gabbay 
5008e65e175bSOded Gabbay static int gaudi_validate_dma_pkt_host(struct hl_device *hdev,
5009e65e175bSOded Gabbay 				struct hl_cs_parser *parser,
5010e65e175bSOded Gabbay 				struct packet_lin_dma *user_dma_pkt,
5011e65e175bSOded Gabbay 				bool src_in_host)
5012e65e175bSOded Gabbay {
5013e65e175bSOded Gabbay 	enum dma_data_direction dir;
5014e65e175bSOded Gabbay 	bool skip_host_mem_pin = false, user_memset;
5015e65e175bSOded Gabbay 	u64 addr;
5016e65e175bSOded Gabbay 	int rc = 0;
5017e65e175bSOded Gabbay 
5018e65e175bSOded Gabbay 	user_memset = (le32_to_cpu(user_dma_pkt->ctl) &
5019e65e175bSOded Gabbay 			GAUDI_PKT_LIN_DMA_CTL_MEMSET_MASK) >>
5020e65e175bSOded Gabbay 			GAUDI_PKT_LIN_DMA_CTL_MEMSET_SHIFT;
5021e65e175bSOded Gabbay 
5022e65e175bSOded Gabbay 	if (src_in_host) {
5023e65e175bSOded Gabbay 		if (user_memset)
5024e65e175bSOded Gabbay 			skip_host_mem_pin = true;
5025e65e175bSOded Gabbay 
5026e65e175bSOded Gabbay 		dev_dbg(hdev->dev, "DMA direction is HOST --> DEVICE\n");
5027e65e175bSOded Gabbay 		dir = DMA_TO_DEVICE;
5028e65e175bSOded Gabbay 		addr = le64_to_cpu(user_dma_pkt->src_addr);
5029e65e175bSOded Gabbay 	} else {
5030e65e175bSOded Gabbay 		dev_dbg(hdev->dev, "DMA direction is DEVICE --> HOST\n");
5031e65e175bSOded Gabbay 		dir = DMA_FROM_DEVICE;
5032e65e175bSOded Gabbay 		addr = (le64_to_cpu(user_dma_pkt->dst_addr) &
5033e65e175bSOded Gabbay 				GAUDI_PKT_LIN_DMA_DST_ADDR_MASK) >>
5034e65e175bSOded Gabbay 				GAUDI_PKT_LIN_DMA_DST_ADDR_SHIFT;
5035e65e175bSOded Gabbay 	}
5036e65e175bSOded Gabbay 
5037e65e175bSOded Gabbay 	if (skip_host_mem_pin)
5038e65e175bSOded Gabbay 		parser->patched_cb_size += sizeof(*user_dma_pkt);
5039e65e175bSOded Gabbay 	else
5040e65e175bSOded Gabbay 		rc = gaudi_pin_memory_before_cs(hdev, parser, user_dma_pkt,
5041e65e175bSOded Gabbay 						addr, dir);
5042e65e175bSOded Gabbay 
5043e65e175bSOded Gabbay 	return rc;
5044e65e175bSOded Gabbay }
5045e65e175bSOded Gabbay 
5046e65e175bSOded Gabbay static int gaudi_validate_dma_pkt_no_mmu(struct hl_device *hdev,
5047e65e175bSOded Gabbay 				struct hl_cs_parser *parser,
5048e65e175bSOded Gabbay 				struct packet_lin_dma *user_dma_pkt)
5049e65e175bSOded Gabbay {
5050e65e175bSOded Gabbay 	bool src_in_host = false;
5051e65e175bSOded Gabbay 	u64 dst_addr = (le64_to_cpu(user_dma_pkt->dst_addr) &
5052e65e175bSOded Gabbay 			GAUDI_PKT_LIN_DMA_DST_ADDR_MASK) >>
5053e65e175bSOded Gabbay 			GAUDI_PKT_LIN_DMA_DST_ADDR_SHIFT;
5054e65e175bSOded Gabbay 
5055e65e175bSOded Gabbay 	dev_dbg(hdev->dev, "DMA packet details:\n");
5056e65e175bSOded Gabbay 	dev_dbg(hdev->dev, "source == 0x%llx\n",
5057e65e175bSOded Gabbay 				le64_to_cpu(user_dma_pkt->src_addr));
5058e65e175bSOded Gabbay 	dev_dbg(hdev->dev, "destination == 0x%llx\n", dst_addr);
5059e65e175bSOded Gabbay 	dev_dbg(hdev->dev, "size == %u\n", le32_to_cpu(user_dma_pkt->tsize));
5060e65e175bSOded Gabbay 
5061e65e175bSOded Gabbay 	/*
5062e65e175bSOded Gabbay 	 * Special handling for DMA with size 0. Bypass all validations
5063e65e175bSOded Gabbay 	 * because no transactions will be done except for WR_COMP, which
5064e65e175bSOded Gabbay 	 * is not a security issue
5065e65e175bSOded Gabbay 	 */
5066e65e175bSOded Gabbay 	if (!le32_to_cpu(user_dma_pkt->tsize)) {
5067e65e175bSOded Gabbay 		parser->patched_cb_size += sizeof(*user_dma_pkt);
5068e65e175bSOded Gabbay 		return 0;
5069e65e175bSOded Gabbay 	}
5070e65e175bSOded Gabbay 
5071e65e175bSOded Gabbay 	if (parser->hw_queue_id <= GAUDI_QUEUE_ID_DMA_0_3)
5072e65e175bSOded Gabbay 		src_in_host = true;
5073e65e175bSOded Gabbay 
5074e65e175bSOded Gabbay 	return gaudi_validate_dma_pkt_host(hdev, parser, user_dma_pkt,
5075e65e175bSOded Gabbay 						src_in_host);
5076e65e175bSOded Gabbay }
5077e65e175bSOded Gabbay 
5078e65e175bSOded Gabbay static int gaudi_validate_load_and_exe_pkt(struct hl_device *hdev,
5079e65e175bSOded Gabbay 					struct hl_cs_parser *parser,
5080e65e175bSOded Gabbay 					struct packet_load_and_exe *user_pkt)
5081e65e175bSOded Gabbay {
5082e65e175bSOded Gabbay 	u32 cfg;
5083e65e175bSOded Gabbay 
5084e65e175bSOded Gabbay 	cfg = le32_to_cpu(user_pkt->cfg);
5085e65e175bSOded Gabbay 
5086e65e175bSOded Gabbay 	if (cfg & GAUDI_PKT_LOAD_AND_EXE_CFG_DST_MASK) {
5087e65e175bSOded Gabbay 		dev_err(hdev->dev,
5088e65e175bSOded Gabbay 			"User not allowed to use Load and Execute\n");
5089e65e175bSOded Gabbay 		return -EPERM;
5090e65e175bSOded Gabbay 	}
5091e65e175bSOded Gabbay 
5092e65e175bSOded Gabbay 	parser->patched_cb_size += sizeof(struct packet_load_and_exe);
5093e65e175bSOded Gabbay 
5094e65e175bSOded Gabbay 	return 0;
5095e65e175bSOded Gabbay }
5096e65e175bSOded Gabbay 
5097e65e175bSOded Gabbay static int gaudi_validate_cb(struct hl_device *hdev,
5098e65e175bSOded Gabbay 			struct hl_cs_parser *parser, bool is_mmu)
5099e65e175bSOded Gabbay {
5100e65e175bSOded Gabbay 	u32 cb_parsed_length = 0;
5101e65e175bSOded Gabbay 	int rc = 0;
5102e65e175bSOded Gabbay 
5103e65e175bSOded Gabbay 	parser->patched_cb_size = 0;
5104e65e175bSOded Gabbay 
5105e65e175bSOded Gabbay 	/* cb_user_size is more than 0 so loop will always be executed */
5106e65e175bSOded Gabbay 	while (cb_parsed_length < parser->user_cb_size) {
5107e65e175bSOded Gabbay 		enum packet_id pkt_id;
5108e65e175bSOded Gabbay 		u16 pkt_size;
5109e65e175bSOded Gabbay 		struct gaudi_packet *user_pkt;
5110e65e175bSOded Gabbay 
5111e65e175bSOded Gabbay 		user_pkt = parser->user_cb->kernel_address + cb_parsed_length;
5112e65e175bSOded Gabbay 
5113e65e175bSOded Gabbay 		pkt_id = (enum packet_id) (
5114e65e175bSOded Gabbay 				(le64_to_cpu(user_pkt->header) &
5115e65e175bSOded Gabbay 				PACKET_HEADER_PACKET_ID_MASK) >>
5116e65e175bSOded Gabbay 					PACKET_HEADER_PACKET_ID_SHIFT);
5117e65e175bSOded Gabbay 
5118e65e175bSOded Gabbay 		if (!validate_packet_id(pkt_id)) {
5119e65e175bSOded Gabbay 			dev_err(hdev->dev, "Invalid packet id %u\n", pkt_id);
5120e65e175bSOded Gabbay 			rc = -EINVAL;
5121e65e175bSOded Gabbay 			break;
5122e65e175bSOded Gabbay 		}
5123e65e175bSOded Gabbay 
5124e65e175bSOded Gabbay 		pkt_size = gaudi_packet_sizes[pkt_id];
5125e65e175bSOded Gabbay 		cb_parsed_length += pkt_size;
5126e65e175bSOded Gabbay 		if (cb_parsed_length > parser->user_cb_size) {
5127e65e175bSOded Gabbay 			dev_err(hdev->dev,
5128e65e175bSOded Gabbay 				"packet 0x%x is out of CB boundary\n", pkt_id);
5129e65e175bSOded Gabbay 			rc = -EINVAL;
5130e65e175bSOded Gabbay 			break;
5131e65e175bSOded Gabbay 		}
5132e65e175bSOded Gabbay 
5133e65e175bSOded Gabbay 		switch (pkt_id) {
5134e65e175bSOded Gabbay 		case PACKET_MSG_PROT:
5135e65e175bSOded Gabbay 			dev_err(hdev->dev,
5136e65e175bSOded Gabbay 				"User not allowed to use MSG_PROT\n");
5137e65e175bSOded Gabbay 			rc = -EPERM;
5138e65e175bSOded Gabbay 			break;
5139e65e175bSOded Gabbay 
5140e65e175bSOded Gabbay 		case PACKET_CP_DMA:
5141e65e175bSOded Gabbay 			dev_err(hdev->dev, "User not allowed to use CP_DMA\n");
5142e65e175bSOded Gabbay 			rc = -EPERM;
5143e65e175bSOded Gabbay 			break;
5144e65e175bSOded Gabbay 
5145e65e175bSOded Gabbay 		case PACKET_STOP:
5146e65e175bSOded Gabbay 			dev_err(hdev->dev, "User not allowed to use STOP\n");
5147e65e175bSOded Gabbay 			rc = -EPERM;
5148e65e175bSOded Gabbay 			break;
5149e65e175bSOded Gabbay 
5150e65e175bSOded Gabbay 		case PACKET_WREG_BULK:
5151e65e175bSOded Gabbay 			dev_err(hdev->dev,
5152e65e175bSOded Gabbay 				"User not allowed to use WREG_BULK\n");
5153e65e175bSOded Gabbay 			rc = -EPERM;
5154e65e175bSOded Gabbay 			break;
5155e65e175bSOded Gabbay 
5156e65e175bSOded Gabbay 		case PACKET_LOAD_AND_EXE:
5157e65e175bSOded Gabbay 			rc = gaudi_validate_load_and_exe_pkt(hdev, parser,
5158e65e175bSOded Gabbay 				(struct packet_load_and_exe *) user_pkt);
5159e65e175bSOded Gabbay 			break;
5160e65e175bSOded Gabbay 
5161e65e175bSOded Gabbay 		case PACKET_LIN_DMA:
5162e65e175bSOded Gabbay 			parser->contains_dma_pkt = true;
5163e65e175bSOded Gabbay 			if (is_mmu)
5164e65e175bSOded Gabbay 				parser->patched_cb_size += pkt_size;
5165e65e175bSOded Gabbay 			else
5166e65e175bSOded Gabbay 				rc = gaudi_validate_dma_pkt_no_mmu(hdev, parser,
5167e65e175bSOded Gabbay 					(struct packet_lin_dma *) user_pkt);
5168e65e175bSOded Gabbay 			break;
5169e65e175bSOded Gabbay 
5170e65e175bSOded Gabbay 		case PACKET_WREG_32:
5171e65e175bSOded Gabbay 		case PACKET_MSG_LONG:
5172e65e175bSOded Gabbay 		case PACKET_MSG_SHORT:
5173e65e175bSOded Gabbay 		case PACKET_REPEAT:
5174e65e175bSOded Gabbay 		case PACKET_FENCE:
5175e65e175bSOded Gabbay 		case PACKET_NOP:
5176e65e175bSOded Gabbay 		case PACKET_ARB_POINT:
5177e65e175bSOded Gabbay 			parser->patched_cb_size += pkt_size;
5178e65e175bSOded Gabbay 			break;
5179e65e175bSOded Gabbay 
5180e65e175bSOded Gabbay 		default:
5181e65e175bSOded Gabbay 			dev_err(hdev->dev, "Invalid packet header 0x%x\n",
5182e65e175bSOded Gabbay 				pkt_id);
5183e65e175bSOded Gabbay 			rc = -EINVAL;
5184e65e175bSOded Gabbay 			break;
5185e65e175bSOded Gabbay 		}
5186e65e175bSOded Gabbay 
5187e65e175bSOded Gabbay 		if (rc)
5188e65e175bSOded Gabbay 			break;
5189e65e175bSOded Gabbay 	}
5190e65e175bSOded Gabbay 
5191e65e175bSOded Gabbay 	/*
5192e65e175bSOded Gabbay 	 * The new CB should have space at the end for two MSG_PROT packets:
5193e65e175bSOded Gabbay 	 * 1. Optional NOP padding for cacheline alignment
5194e65e175bSOded Gabbay 	 * 2. A packet that will act as a completion packet
5195e65e175bSOded Gabbay 	 * 3. A packet that will generate MSI interrupt
5196e65e175bSOded Gabbay 	 */
5197e65e175bSOded Gabbay 	if (parser->completion)
5198e65e175bSOded Gabbay 		parser->patched_cb_size += gaudi_get_patched_cb_extra_size(
5199e65e175bSOded Gabbay 			parser->patched_cb_size);
5200e65e175bSOded Gabbay 
5201e65e175bSOded Gabbay 	return rc;
5202e65e175bSOded Gabbay }
5203e65e175bSOded Gabbay 
5204e65e175bSOded Gabbay static int gaudi_patch_dma_packet(struct hl_device *hdev,
5205e65e175bSOded Gabbay 				struct hl_cs_parser *parser,
5206e65e175bSOded Gabbay 				struct packet_lin_dma *user_dma_pkt,
5207e65e175bSOded Gabbay 				struct packet_lin_dma *new_dma_pkt,
5208e65e175bSOded Gabbay 				u32 *new_dma_pkt_size)
5209e65e175bSOded Gabbay {
5210e65e175bSOded Gabbay 	struct hl_userptr *userptr;
5211e65e175bSOded Gabbay 	struct scatterlist *sg, *sg_next_iter;
5212e65e175bSOded Gabbay 	u32 count, dma_desc_cnt, user_wrcomp_en_mask, ctl;
5213e65e175bSOded Gabbay 	u64 len, len_next;
5214e65e175bSOded Gabbay 	dma_addr_t dma_addr, dma_addr_next;
5215e65e175bSOded Gabbay 	u64 device_memory_addr, addr;
5216e65e175bSOded Gabbay 	enum dma_data_direction dir;
5217e65e175bSOded Gabbay 	struct sg_table *sgt;
5218e65e175bSOded Gabbay 	bool src_in_host = false;
5219e65e175bSOded Gabbay 	bool skip_host_mem_pin = false;
5220e65e175bSOded Gabbay 	bool user_memset;
5221e65e175bSOded Gabbay 
5222e65e175bSOded Gabbay 	ctl = le32_to_cpu(user_dma_pkt->ctl);
5223e65e175bSOded Gabbay 
5224e65e175bSOded Gabbay 	if (parser->hw_queue_id <= GAUDI_QUEUE_ID_DMA_0_3)
5225e65e175bSOded Gabbay 		src_in_host = true;
5226e65e175bSOded Gabbay 
5227e65e175bSOded Gabbay 	user_memset = (ctl & GAUDI_PKT_LIN_DMA_CTL_MEMSET_MASK) >>
5228e65e175bSOded Gabbay 			GAUDI_PKT_LIN_DMA_CTL_MEMSET_SHIFT;
5229e65e175bSOded Gabbay 
5230e65e175bSOded Gabbay 	if (src_in_host) {
5231e65e175bSOded Gabbay 		addr = le64_to_cpu(user_dma_pkt->src_addr);
5232e65e175bSOded Gabbay 		device_memory_addr = le64_to_cpu(user_dma_pkt->dst_addr);
5233e65e175bSOded Gabbay 		dir = DMA_TO_DEVICE;
5234e65e175bSOded Gabbay 		if (user_memset)
5235e65e175bSOded Gabbay 			skip_host_mem_pin = true;
5236e65e175bSOded Gabbay 	} else {
5237e65e175bSOded Gabbay 		addr = le64_to_cpu(user_dma_pkt->dst_addr);
5238e65e175bSOded Gabbay 		device_memory_addr = le64_to_cpu(user_dma_pkt->src_addr);
5239e65e175bSOded Gabbay 		dir = DMA_FROM_DEVICE;
5240e65e175bSOded Gabbay 	}
5241e65e175bSOded Gabbay 
5242e65e175bSOded Gabbay 	if ((!skip_host_mem_pin) &&
5243e65e175bSOded Gabbay 		(!hl_userptr_is_pinned(hdev, addr,
5244e65e175bSOded Gabbay 					le32_to_cpu(user_dma_pkt->tsize),
5245e65e175bSOded Gabbay 					parser->job_userptr_list, &userptr))) {
5246e65e175bSOded Gabbay 		dev_err(hdev->dev, "Userptr 0x%llx + 0x%x NOT mapped\n",
5247e65e175bSOded Gabbay 				addr, user_dma_pkt->tsize);
5248e65e175bSOded Gabbay 		return -EFAULT;
5249e65e175bSOded Gabbay 	}
5250e65e175bSOded Gabbay 
5251e65e175bSOded Gabbay 	if ((user_memset) && (dir == DMA_TO_DEVICE)) {
5252e65e175bSOded Gabbay 		memcpy(new_dma_pkt, user_dma_pkt, sizeof(*user_dma_pkt));
5253e65e175bSOded Gabbay 		*new_dma_pkt_size = sizeof(*user_dma_pkt);
5254e65e175bSOded Gabbay 		return 0;
5255e65e175bSOded Gabbay 	}
5256e65e175bSOded Gabbay 
5257e65e175bSOded Gabbay 	user_wrcomp_en_mask = ctl & GAUDI_PKT_LIN_DMA_CTL_WRCOMP_EN_MASK;
5258e65e175bSOded Gabbay 
5259e65e175bSOded Gabbay 	sgt = userptr->sgt;
5260e65e175bSOded Gabbay 	dma_desc_cnt = 0;
5261e65e175bSOded Gabbay 
5262e65e175bSOded Gabbay 	for_each_sgtable_dma_sg(sgt, sg, count) {
5263e65e175bSOded Gabbay 		len = sg_dma_len(sg);
5264e65e175bSOded Gabbay 		dma_addr = sg_dma_address(sg);
5265e65e175bSOded Gabbay 
5266e65e175bSOded Gabbay 		if (len == 0)
5267e65e175bSOded Gabbay 			break;
5268e65e175bSOded Gabbay 
5269e65e175bSOded Gabbay 		while ((count + 1) < sgt->nents) {
5270e65e175bSOded Gabbay 			sg_next_iter = sg_next(sg);
5271e65e175bSOded Gabbay 			len_next = sg_dma_len(sg_next_iter);
5272e65e175bSOded Gabbay 			dma_addr_next = sg_dma_address(sg_next_iter);
5273e65e175bSOded Gabbay 
5274e65e175bSOded Gabbay 			if (len_next == 0)
5275e65e175bSOded Gabbay 				break;
5276e65e175bSOded Gabbay 
5277e65e175bSOded Gabbay 			if ((dma_addr + len == dma_addr_next) &&
5278e65e175bSOded Gabbay 				(len + len_next <= DMA_MAX_TRANSFER_SIZE)) {
5279e65e175bSOded Gabbay 				len += len_next;
5280e65e175bSOded Gabbay 				count++;
5281e65e175bSOded Gabbay 				sg = sg_next_iter;
5282e65e175bSOded Gabbay 			} else {
5283e65e175bSOded Gabbay 				break;
5284e65e175bSOded Gabbay 			}
5285e65e175bSOded Gabbay 		}
5286e65e175bSOded Gabbay 
5287e65e175bSOded Gabbay 		ctl = le32_to_cpu(user_dma_pkt->ctl);
5288e65e175bSOded Gabbay 		if (likely(dma_desc_cnt))
5289e65e175bSOded Gabbay 			ctl &= ~GAUDI_PKT_CTL_EB_MASK;
5290e65e175bSOded Gabbay 		ctl &= ~GAUDI_PKT_LIN_DMA_CTL_WRCOMP_EN_MASK;
5291e65e175bSOded Gabbay 		new_dma_pkt->ctl = cpu_to_le32(ctl);
5292e65e175bSOded Gabbay 		new_dma_pkt->tsize = cpu_to_le32(len);
5293e65e175bSOded Gabbay 
5294e65e175bSOded Gabbay 		if (dir == DMA_TO_DEVICE) {
5295e65e175bSOded Gabbay 			new_dma_pkt->src_addr = cpu_to_le64(dma_addr);
5296e65e175bSOded Gabbay 			new_dma_pkt->dst_addr = cpu_to_le64(device_memory_addr);
5297e65e175bSOded Gabbay 		} else {
5298e65e175bSOded Gabbay 			new_dma_pkt->src_addr = cpu_to_le64(device_memory_addr);
5299e65e175bSOded Gabbay 			new_dma_pkt->dst_addr = cpu_to_le64(dma_addr);
5300e65e175bSOded Gabbay 		}
5301e65e175bSOded Gabbay 
5302e65e175bSOded Gabbay 		if (!user_memset)
5303e65e175bSOded Gabbay 			device_memory_addr += len;
5304e65e175bSOded Gabbay 		dma_desc_cnt++;
5305e65e175bSOded Gabbay 		new_dma_pkt++;
5306e65e175bSOded Gabbay 	}
5307e65e175bSOded Gabbay 
5308e65e175bSOded Gabbay 	if (!dma_desc_cnt) {
5309e65e175bSOded Gabbay 		dev_err(hdev->dev,
5310e65e175bSOded Gabbay 			"Error of 0 SG entries when patching DMA packet\n");
5311e65e175bSOded Gabbay 		return -EFAULT;
5312e65e175bSOded Gabbay 	}
5313e65e175bSOded Gabbay 
5314e65e175bSOded Gabbay 	/* Fix the last dma packet - wrcomp must be as user set it */
5315e65e175bSOded Gabbay 	new_dma_pkt--;
5316e65e175bSOded Gabbay 	new_dma_pkt->ctl |= cpu_to_le32(user_wrcomp_en_mask);
5317e65e175bSOded Gabbay 
5318e65e175bSOded Gabbay 	*new_dma_pkt_size = dma_desc_cnt * sizeof(struct packet_lin_dma);
5319e65e175bSOded Gabbay 
5320e65e175bSOded Gabbay 	return 0;
5321e65e175bSOded Gabbay }
5322e65e175bSOded Gabbay 
5323e65e175bSOded Gabbay static int gaudi_patch_cb(struct hl_device *hdev,
5324e65e175bSOded Gabbay 				struct hl_cs_parser *parser)
5325e65e175bSOded Gabbay {
5326e65e175bSOded Gabbay 	u32 cb_parsed_length = 0;
5327e65e175bSOded Gabbay 	u32 cb_patched_cur_length = 0;
5328e65e175bSOded Gabbay 	int rc = 0;
5329e65e175bSOded Gabbay 
5330e65e175bSOded Gabbay 	/* cb_user_size is more than 0 so loop will always be executed */
5331e65e175bSOded Gabbay 	while (cb_parsed_length < parser->user_cb_size) {
5332e65e175bSOded Gabbay 		enum packet_id pkt_id;
5333e65e175bSOded Gabbay 		u16 pkt_size;
5334e65e175bSOded Gabbay 		u32 new_pkt_size = 0;
5335e65e175bSOded Gabbay 		struct gaudi_packet *user_pkt, *kernel_pkt;
5336e65e175bSOded Gabbay 
5337e65e175bSOded Gabbay 		user_pkt = parser->user_cb->kernel_address + cb_parsed_length;
5338e65e175bSOded Gabbay 		kernel_pkt = parser->patched_cb->kernel_address +
5339e65e175bSOded Gabbay 					cb_patched_cur_length;
5340e65e175bSOded Gabbay 
5341e65e175bSOded Gabbay 		pkt_id = (enum packet_id) (
5342e65e175bSOded Gabbay 				(le64_to_cpu(user_pkt->header) &
5343e65e175bSOded Gabbay 				PACKET_HEADER_PACKET_ID_MASK) >>
5344e65e175bSOded Gabbay 					PACKET_HEADER_PACKET_ID_SHIFT);
5345e65e175bSOded Gabbay 
5346e65e175bSOded Gabbay 		if (!validate_packet_id(pkt_id)) {
5347e65e175bSOded Gabbay 			dev_err(hdev->dev, "Invalid packet id %u\n", pkt_id);
5348e65e175bSOded Gabbay 			rc = -EINVAL;
5349e65e175bSOded Gabbay 			break;
5350e65e175bSOded Gabbay 		}
5351e65e175bSOded Gabbay 
5352e65e175bSOded Gabbay 		pkt_size = gaudi_packet_sizes[pkt_id];
5353e65e175bSOded Gabbay 		cb_parsed_length += pkt_size;
5354e65e175bSOded Gabbay 		if (cb_parsed_length > parser->user_cb_size) {
5355e65e175bSOded Gabbay 			dev_err(hdev->dev,
5356e65e175bSOded Gabbay 				"packet 0x%x is out of CB boundary\n", pkt_id);
5357e65e175bSOded Gabbay 			rc = -EINVAL;
5358e65e175bSOded Gabbay 			break;
5359e65e175bSOded Gabbay 		}
5360e65e175bSOded Gabbay 
5361e65e175bSOded Gabbay 		switch (pkt_id) {
5362e65e175bSOded Gabbay 		case PACKET_LIN_DMA:
5363e65e175bSOded Gabbay 			rc = gaudi_patch_dma_packet(hdev, parser,
5364e65e175bSOded Gabbay 					(struct packet_lin_dma *) user_pkt,
5365e65e175bSOded Gabbay 					(struct packet_lin_dma *) kernel_pkt,
5366e65e175bSOded Gabbay 					&new_pkt_size);
5367e65e175bSOded Gabbay 			cb_patched_cur_length += new_pkt_size;
5368e65e175bSOded Gabbay 			break;
5369e65e175bSOded Gabbay 
5370e65e175bSOded Gabbay 		case PACKET_MSG_PROT:
5371e65e175bSOded Gabbay 			dev_err(hdev->dev,
5372e65e175bSOded Gabbay 				"User not allowed to use MSG_PROT\n");
5373e65e175bSOded Gabbay 			rc = -EPERM;
5374e65e175bSOded Gabbay 			break;
5375e65e175bSOded Gabbay 
5376e65e175bSOded Gabbay 		case PACKET_CP_DMA:
5377e65e175bSOded Gabbay 			dev_err(hdev->dev, "User not allowed to use CP_DMA\n");
5378e65e175bSOded Gabbay 			rc = -EPERM;
5379e65e175bSOded Gabbay 			break;
5380e65e175bSOded Gabbay 
5381e65e175bSOded Gabbay 		case PACKET_STOP:
5382e65e175bSOded Gabbay 			dev_err(hdev->dev, "User not allowed to use STOP\n");
5383e65e175bSOded Gabbay 			rc = -EPERM;
5384e65e175bSOded Gabbay 			break;
5385e65e175bSOded Gabbay 
5386e65e175bSOded Gabbay 		case PACKET_WREG_32:
5387e65e175bSOded Gabbay 		case PACKET_WREG_BULK:
5388e65e175bSOded Gabbay 		case PACKET_MSG_LONG:
5389e65e175bSOded Gabbay 		case PACKET_MSG_SHORT:
5390e65e175bSOded Gabbay 		case PACKET_REPEAT:
5391e65e175bSOded Gabbay 		case PACKET_FENCE:
5392e65e175bSOded Gabbay 		case PACKET_NOP:
5393e65e175bSOded Gabbay 		case PACKET_ARB_POINT:
5394e65e175bSOded Gabbay 		case PACKET_LOAD_AND_EXE:
5395e65e175bSOded Gabbay 			memcpy(kernel_pkt, user_pkt, pkt_size);
5396e65e175bSOded Gabbay 			cb_patched_cur_length += pkt_size;
5397e65e175bSOded Gabbay 			break;
5398e65e175bSOded Gabbay 
5399e65e175bSOded Gabbay 		default:
5400e65e175bSOded Gabbay 			dev_err(hdev->dev, "Invalid packet header 0x%x\n",
5401e65e175bSOded Gabbay 				pkt_id);
5402e65e175bSOded Gabbay 			rc = -EINVAL;
5403e65e175bSOded Gabbay 			break;
5404e65e175bSOded Gabbay 		}
5405e65e175bSOded Gabbay 
5406e65e175bSOded Gabbay 		if (rc)
5407e65e175bSOded Gabbay 			break;
5408e65e175bSOded Gabbay 	}
5409e65e175bSOded Gabbay 
5410e65e175bSOded Gabbay 	return rc;
5411e65e175bSOded Gabbay }
5412e65e175bSOded Gabbay 
5413e65e175bSOded Gabbay static int gaudi_parse_cb_mmu(struct hl_device *hdev,
5414e65e175bSOded Gabbay 		struct hl_cs_parser *parser)
5415e65e175bSOded Gabbay {
5416e65e175bSOded Gabbay 	u64 handle;
5417e65e175bSOded Gabbay 	u32 patched_cb_size;
5418e65e175bSOded Gabbay 	struct hl_cb *user_cb;
5419e65e175bSOded Gabbay 	int rc;
5420e65e175bSOded Gabbay 
5421e65e175bSOded Gabbay 	/*
5422e65e175bSOded Gabbay 	 * The new CB should have space at the end for two MSG_PROT packets:
5423e65e175bSOded Gabbay 	 * 1. Optional NOP padding for cacheline alignment
5424e65e175bSOded Gabbay 	 * 2. A packet that will act as a completion packet
5425e65e175bSOded Gabbay 	 * 3. A packet that will generate MSI interrupt
5426e65e175bSOded Gabbay 	 */
5427e65e175bSOded Gabbay 	if (parser->completion)
5428e65e175bSOded Gabbay 		parser->patched_cb_size = parser->user_cb_size +
5429e65e175bSOded Gabbay 				gaudi_get_patched_cb_extra_size(parser->user_cb_size);
5430e65e175bSOded Gabbay 	else
5431e65e175bSOded Gabbay 		parser->patched_cb_size = parser->user_cb_size;
5432e65e175bSOded Gabbay 
5433e65e175bSOded Gabbay 	rc = hl_cb_create(hdev, &hdev->kernel_mem_mgr, hdev->kernel_ctx,
5434e65e175bSOded Gabbay 				parser->patched_cb_size, false, false,
5435e65e175bSOded Gabbay 				&handle);
5436e65e175bSOded Gabbay 
5437e65e175bSOded Gabbay 	if (rc) {
5438e65e175bSOded Gabbay 		dev_err(hdev->dev,
5439e65e175bSOded Gabbay 			"Failed to allocate patched CB for DMA CS %d\n",
5440e65e175bSOded Gabbay 			rc);
5441e65e175bSOded Gabbay 		return rc;
5442e65e175bSOded Gabbay 	}
5443e65e175bSOded Gabbay 
5444e65e175bSOded Gabbay 	parser->patched_cb = hl_cb_get(&hdev->kernel_mem_mgr, handle);
5445e65e175bSOded Gabbay 	/* hl_cb_get should never fail */
5446e65e175bSOded Gabbay 	if (!parser->patched_cb) {
5447e65e175bSOded Gabbay 		dev_crit(hdev->dev, "DMA CB handle invalid 0x%llx\n", handle);
5448e65e175bSOded Gabbay 		rc = -EFAULT;
5449e65e175bSOded Gabbay 		goto out;
5450e65e175bSOded Gabbay 	}
5451e65e175bSOded Gabbay 
5452e65e175bSOded Gabbay 	/*
5453e65e175bSOded Gabbay 	 * We are protected from overflow because the check
5454e65e175bSOded Gabbay 	 * "parser->user_cb_size <= parser->user_cb->size" was done in get_cb_from_cs_chunk()
5455e65e175bSOded Gabbay 	 * in the common code. That check is done only if is_kernel_allocated_cb is true.
5456e65e175bSOded Gabbay 	 *
5457e65e175bSOded Gabbay 	 * There is no option to reach here without going through that check because:
5458e65e175bSOded Gabbay 	 * 1. validate_queue_index() assigns true to is_kernel_allocated_cb for any submission to
5459e65e175bSOded Gabbay 	 *    an external queue.
5460e65e175bSOded Gabbay 	 * 2. For Gaudi, we only parse CBs that were submitted to the external queues.
5461e65e175bSOded Gabbay 	 */
5462e65e175bSOded Gabbay 	memcpy(parser->patched_cb->kernel_address,
5463e65e175bSOded Gabbay 		parser->user_cb->kernel_address,
5464e65e175bSOded Gabbay 		parser->user_cb_size);
5465e65e175bSOded Gabbay 
5466e65e175bSOded Gabbay 	patched_cb_size = parser->patched_cb_size;
5467e65e175bSOded Gabbay 
5468e65e175bSOded Gabbay 	/* Validate patched CB instead of user CB */
5469e65e175bSOded Gabbay 	user_cb = parser->user_cb;
5470e65e175bSOded Gabbay 	parser->user_cb = parser->patched_cb;
5471e65e175bSOded Gabbay 	rc = gaudi_validate_cb(hdev, parser, true);
5472e65e175bSOded Gabbay 	parser->user_cb = user_cb;
5473e65e175bSOded Gabbay 
5474e65e175bSOded Gabbay 	if (rc) {
5475e65e175bSOded Gabbay 		hl_cb_put(parser->patched_cb);
5476e65e175bSOded Gabbay 		goto out;
5477e65e175bSOded Gabbay 	}
5478e65e175bSOded Gabbay 
5479e65e175bSOded Gabbay 	if (patched_cb_size != parser->patched_cb_size) {
5480e65e175bSOded Gabbay 		dev_err(hdev->dev, "user CB size mismatch\n");
5481e65e175bSOded Gabbay 		hl_cb_put(parser->patched_cb);
5482e65e175bSOded Gabbay 		rc = -EINVAL;
5483e65e175bSOded Gabbay 		goto out;
5484e65e175bSOded Gabbay 	}
5485e65e175bSOded Gabbay 
5486e65e175bSOded Gabbay out:
5487e65e175bSOded Gabbay 	/*
5488e65e175bSOded Gabbay 	 * Always call cb destroy here because we still have 1 reference
5489e65e175bSOded Gabbay 	 * to it by calling cb_get earlier. After the job will be completed,
5490e65e175bSOded Gabbay 	 * cb_put will release it, but here we want to remove it from the
5491e65e175bSOded Gabbay 	 * idr
5492e65e175bSOded Gabbay 	 */
5493e65e175bSOded Gabbay 	hl_cb_destroy(&hdev->kernel_mem_mgr, handle);
5494e65e175bSOded Gabbay 
5495e65e175bSOded Gabbay 	return rc;
5496e65e175bSOded Gabbay }
5497e65e175bSOded Gabbay 
5498e65e175bSOded Gabbay static int gaudi_parse_cb_no_mmu(struct hl_device *hdev,
5499e65e175bSOded Gabbay 		struct hl_cs_parser *parser)
5500e65e175bSOded Gabbay {
5501e65e175bSOded Gabbay 	u64 handle;
5502e65e175bSOded Gabbay 	int rc;
5503e65e175bSOded Gabbay 
5504e65e175bSOded Gabbay 	rc = gaudi_validate_cb(hdev, parser, false);
5505e65e175bSOded Gabbay 
5506e65e175bSOded Gabbay 	if (rc)
5507e65e175bSOded Gabbay 		goto free_userptr;
5508e65e175bSOded Gabbay 
5509e65e175bSOded Gabbay 	rc = hl_cb_create(hdev, &hdev->kernel_mem_mgr, hdev->kernel_ctx,
5510e65e175bSOded Gabbay 				parser->patched_cb_size, false, false,
5511e65e175bSOded Gabbay 				&handle);
5512e65e175bSOded Gabbay 	if (rc) {
5513e65e175bSOded Gabbay 		dev_err(hdev->dev,
5514e65e175bSOded Gabbay 			"Failed to allocate patched CB for DMA CS %d\n", rc);
5515e65e175bSOded Gabbay 		goto free_userptr;
5516e65e175bSOded Gabbay 	}
5517e65e175bSOded Gabbay 
5518e65e175bSOded Gabbay 	parser->patched_cb = hl_cb_get(&hdev->kernel_mem_mgr, handle);
5519e65e175bSOded Gabbay 	/* hl_cb_get should never fail here */
5520e65e175bSOded Gabbay 	if (!parser->patched_cb) {
5521e65e175bSOded Gabbay 		dev_crit(hdev->dev, "DMA CB handle invalid 0x%llx\n", handle);
5522e65e175bSOded Gabbay 		rc = -EFAULT;
5523e65e175bSOded Gabbay 		goto out;
5524e65e175bSOded Gabbay 	}
5525e65e175bSOded Gabbay 
5526e65e175bSOded Gabbay 	rc = gaudi_patch_cb(hdev, parser);
5527e65e175bSOded Gabbay 
5528e65e175bSOded Gabbay 	if (rc)
5529e65e175bSOded Gabbay 		hl_cb_put(parser->patched_cb);
5530e65e175bSOded Gabbay 
5531e65e175bSOded Gabbay out:
5532e65e175bSOded Gabbay 	/*
5533e65e175bSOded Gabbay 	 * Always call cb destroy here because we still have 1 reference
5534e65e175bSOded Gabbay 	 * to it by calling cb_get earlier. After the job will be completed,
5535e65e175bSOded Gabbay 	 * cb_put will release it, but here we want to remove it from the
5536e65e175bSOded Gabbay 	 * idr
5537e65e175bSOded Gabbay 	 */
5538e65e175bSOded Gabbay 	hl_cb_destroy(&hdev->kernel_mem_mgr, handle);
5539e65e175bSOded Gabbay 
5540e65e175bSOded Gabbay free_userptr:
5541e65e175bSOded Gabbay 	if (rc)
5542e65e175bSOded Gabbay 		hl_userptr_delete_list(hdev, parser->job_userptr_list);
5543e65e175bSOded Gabbay 	return rc;
5544e65e175bSOded Gabbay }
5545e65e175bSOded Gabbay 
5546e65e175bSOded Gabbay static int gaudi_parse_cb_no_ext_queue(struct hl_device *hdev,
5547e65e175bSOded Gabbay 					struct hl_cs_parser *parser)
5548e65e175bSOded Gabbay {
5549e65e175bSOded Gabbay 	struct asic_fixed_properties *asic_prop = &hdev->asic_prop;
5550e65e175bSOded Gabbay 	struct gaudi_device *gaudi = hdev->asic_specific;
5551e65e175bSOded Gabbay 	u32 nic_queue_offset, nic_mask_q_id;
5552e65e175bSOded Gabbay 
5553e65e175bSOded Gabbay 	if ((parser->hw_queue_id >= GAUDI_QUEUE_ID_NIC_0_0) &&
5554e65e175bSOded Gabbay 			(parser->hw_queue_id <= GAUDI_QUEUE_ID_NIC_9_3)) {
5555e65e175bSOded Gabbay 		nic_queue_offset = parser->hw_queue_id - GAUDI_QUEUE_ID_NIC_0_0;
5556e65e175bSOded Gabbay 		nic_mask_q_id = 1 << (HW_CAP_NIC_SHIFT + (nic_queue_offset >> 2));
5557e65e175bSOded Gabbay 
5558e65e175bSOded Gabbay 		if (!(gaudi->hw_cap_initialized & nic_mask_q_id)) {
5559e65e175bSOded Gabbay 			dev_err(hdev->dev, "h/w queue %d is disabled\n", parser->hw_queue_id);
5560e65e175bSOded Gabbay 			return -EINVAL;
5561e65e175bSOded Gabbay 		}
5562e65e175bSOded Gabbay 	}
5563e65e175bSOded Gabbay 
5564e65e175bSOded Gabbay 	/* For internal queue jobs just check if CB address is valid */
5565e65e175bSOded Gabbay 	if (hl_mem_area_inside_range((u64) (uintptr_t) parser->user_cb,
5566e65e175bSOded Gabbay 					parser->user_cb_size,
5567e65e175bSOded Gabbay 					asic_prop->sram_user_base_address,
5568e65e175bSOded Gabbay 					asic_prop->sram_end_address))
5569e65e175bSOded Gabbay 		return 0;
5570e65e175bSOded Gabbay 
5571e65e175bSOded Gabbay 	if (hl_mem_area_inside_range((u64) (uintptr_t) parser->user_cb,
5572e65e175bSOded Gabbay 					parser->user_cb_size,
5573e65e175bSOded Gabbay 					asic_prop->dram_user_base_address,
5574e65e175bSOded Gabbay 					asic_prop->dram_end_address))
5575e65e175bSOded Gabbay 		return 0;
5576e65e175bSOded Gabbay 
5577e65e175bSOded Gabbay 	/* PMMU and HPMMU addresses are equal, check only one of them */
5578e65e175bSOded Gabbay 	if (hl_mem_area_inside_range((u64) (uintptr_t) parser->user_cb,
5579e65e175bSOded Gabbay 					parser->user_cb_size,
5580e65e175bSOded Gabbay 					asic_prop->pmmu.start_addr,
5581e65e175bSOded Gabbay 					asic_prop->pmmu.end_addr))
5582e65e175bSOded Gabbay 		return 0;
5583e65e175bSOded Gabbay 
5584e65e175bSOded Gabbay 	dev_err(hdev->dev,
5585e65e175bSOded Gabbay 		"CB address 0x%px + 0x%x for internal QMAN is not valid\n",
5586e65e175bSOded Gabbay 		parser->user_cb, parser->user_cb_size);
5587e65e175bSOded Gabbay 
5588e65e175bSOded Gabbay 	return -EFAULT;
5589e65e175bSOded Gabbay }
5590e65e175bSOded Gabbay 
5591e65e175bSOded Gabbay static int gaudi_cs_parser(struct hl_device *hdev, struct hl_cs_parser *parser)
5592e65e175bSOded Gabbay {
5593e65e175bSOded Gabbay 	struct gaudi_device *gaudi = hdev->asic_specific;
5594e65e175bSOded Gabbay 
5595e65e175bSOded Gabbay 	if (parser->queue_type == QUEUE_TYPE_INT)
5596e65e175bSOded Gabbay 		return gaudi_parse_cb_no_ext_queue(hdev, parser);
5597e65e175bSOded Gabbay 
5598e65e175bSOded Gabbay 	if (gaudi->hw_cap_initialized & HW_CAP_MMU)
5599e65e175bSOded Gabbay 		return gaudi_parse_cb_mmu(hdev, parser);
5600e65e175bSOded Gabbay 	else
5601e65e175bSOded Gabbay 		return gaudi_parse_cb_no_mmu(hdev, parser);
5602e65e175bSOded Gabbay }
5603e65e175bSOded Gabbay 
5604e65e175bSOded Gabbay static void gaudi_add_end_of_cb_packets(struct hl_device *hdev, void *kernel_address,
5605e65e175bSOded Gabbay 				u32 len, u32 original_len, u64 cq_addr, u32 cq_val,
5606e65e175bSOded Gabbay 				u32 msi_vec, bool eb)
5607e65e175bSOded Gabbay {
5608e65e175bSOded Gabbay 	struct gaudi_device *gaudi = hdev->asic_specific;
5609e65e175bSOded Gabbay 	struct packet_msg_prot *cq_pkt;
5610e65e175bSOded Gabbay 	struct packet_nop *cq_padding;
5611e65e175bSOded Gabbay 	u64 msi_addr;
5612e65e175bSOded Gabbay 	u32 tmp;
5613e65e175bSOded Gabbay 
5614e65e175bSOded Gabbay 	cq_padding = kernel_address + original_len;
5615e65e175bSOded Gabbay 	cq_pkt = kernel_address + len - (sizeof(struct packet_msg_prot) * 2);
5616e65e175bSOded Gabbay 
5617e65e175bSOded Gabbay 	while ((void *)cq_padding < (void *)cq_pkt) {
5618e65e175bSOded Gabbay 		cq_padding->ctl = cpu_to_le32(FIELD_PREP(GAUDI_PKT_CTL_OPCODE_MASK, PACKET_NOP));
5619e65e175bSOded Gabbay 		cq_padding++;
5620e65e175bSOded Gabbay 	}
5621e65e175bSOded Gabbay 
5622e65e175bSOded Gabbay 	tmp = FIELD_PREP(GAUDI_PKT_CTL_OPCODE_MASK, PACKET_MSG_PROT);
5623e65e175bSOded Gabbay 	tmp |= FIELD_PREP(GAUDI_PKT_CTL_MB_MASK, 1);
5624e65e175bSOded Gabbay 
5625e65e175bSOded Gabbay 	if (eb)
5626e65e175bSOded Gabbay 		tmp |= FIELD_PREP(GAUDI_PKT_CTL_EB_MASK, 1);
5627e65e175bSOded Gabbay 
5628e65e175bSOded Gabbay 	cq_pkt->ctl = cpu_to_le32(tmp);
5629e65e175bSOded Gabbay 	cq_pkt->value = cpu_to_le32(cq_val);
5630e65e175bSOded Gabbay 	cq_pkt->addr = cpu_to_le64(cq_addr);
5631e65e175bSOded Gabbay 
5632e65e175bSOded Gabbay 	cq_pkt++;
5633e65e175bSOded Gabbay 
5634e65e175bSOded Gabbay 	tmp = FIELD_PREP(GAUDI_PKT_CTL_OPCODE_MASK, PACKET_MSG_PROT);
5635e65e175bSOded Gabbay 	tmp |= FIELD_PREP(GAUDI_PKT_CTL_MB_MASK, 1);
5636e65e175bSOded Gabbay 	cq_pkt->ctl = cpu_to_le32(tmp);
5637e65e175bSOded Gabbay 	cq_pkt->value = cpu_to_le32(1);
5638e65e175bSOded Gabbay 
5639e65e175bSOded Gabbay 	if (gaudi->multi_msi_mode)
5640e65e175bSOded Gabbay 		msi_addr = mmPCIE_MSI_INTR_0 + msi_vec * 4;
5641e65e175bSOded Gabbay 	else
5642e65e175bSOded Gabbay 		msi_addr = mmPCIE_CORE_MSI_REQ;
5643e65e175bSOded Gabbay 
5644e65e175bSOded Gabbay 	cq_pkt->addr = cpu_to_le64(CFG_BASE + msi_addr);
5645e65e175bSOded Gabbay }
5646e65e175bSOded Gabbay 
5647e65e175bSOded Gabbay static void gaudi_update_eq_ci(struct hl_device *hdev, u32 val)
5648e65e175bSOded Gabbay {
5649e65e175bSOded Gabbay 	WREG32(mmCPU_IF_EQ_RD_OFFS, val);
5650e65e175bSOded Gabbay }
5651e65e175bSOded Gabbay 
5652e65e175bSOded Gabbay static int gaudi_memset_device_memory(struct hl_device *hdev, u64 addr,
5653e65e175bSOded Gabbay 					u32 size, u64 val)
5654e65e175bSOded Gabbay {
5655e65e175bSOded Gabbay 	struct packet_lin_dma *lin_dma_pkt;
5656e65e175bSOded Gabbay 	struct hl_cs_job *job;
5657e65e175bSOded Gabbay 	u32 cb_size, ctl, err_cause;
5658e65e175bSOded Gabbay 	struct hl_cb *cb;
5659e65e175bSOded Gabbay 	int rc;
5660e65e175bSOded Gabbay 
5661e65e175bSOded Gabbay 	cb = hl_cb_kernel_create(hdev, PAGE_SIZE, false);
5662e65e175bSOded Gabbay 	if (!cb)
5663e65e175bSOded Gabbay 		return -EFAULT;
5664e65e175bSOded Gabbay 
5665e65e175bSOded Gabbay 	lin_dma_pkt = cb->kernel_address;
5666e65e175bSOded Gabbay 	memset(lin_dma_pkt, 0, sizeof(*lin_dma_pkt));
5667e65e175bSOded Gabbay 	cb_size = sizeof(*lin_dma_pkt);
5668e65e175bSOded Gabbay 
5669e65e175bSOded Gabbay 	ctl = FIELD_PREP(GAUDI_PKT_CTL_OPCODE_MASK, PACKET_LIN_DMA);
5670e65e175bSOded Gabbay 	ctl |= FIELD_PREP(GAUDI_PKT_LIN_DMA_CTL_MEMSET_MASK, 1);
5671e65e175bSOded Gabbay 	ctl |= FIELD_PREP(GAUDI_PKT_LIN_DMA_CTL_LIN_MASK, 1);
5672e65e175bSOded Gabbay 	ctl |= FIELD_PREP(GAUDI_PKT_CTL_MB_MASK, 1);
5673e65e175bSOded Gabbay 	ctl |= FIELD_PREP(GAUDI_PKT_CTL_RB_MASK, 1);
5674e65e175bSOded Gabbay 
5675e65e175bSOded Gabbay 	lin_dma_pkt->ctl = cpu_to_le32(ctl);
5676e65e175bSOded Gabbay 	lin_dma_pkt->src_addr = cpu_to_le64(val);
5677e65e175bSOded Gabbay 	lin_dma_pkt->dst_addr |= cpu_to_le64(addr);
5678e65e175bSOded Gabbay 	lin_dma_pkt->tsize = cpu_to_le32(size);
5679e65e175bSOded Gabbay 
5680e65e175bSOded Gabbay 	job = hl_cs_allocate_job(hdev, QUEUE_TYPE_EXT, true);
5681e65e175bSOded Gabbay 	if (!job) {
5682e65e175bSOded Gabbay 		dev_err(hdev->dev, "Failed to allocate a new job\n");
5683e65e175bSOded Gabbay 		rc = -ENOMEM;
5684e65e175bSOded Gabbay 		goto release_cb;
5685e65e175bSOded Gabbay 	}
5686e65e175bSOded Gabbay 
5687e65e175bSOded Gabbay 	/* Verify DMA is OK */
5688e65e175bSOded Gabbay 	err_cause = RREG32(mmDMA0_CORE_ERR_CAUSE);
5689e65e175bSOded Gabbay 	if (err_cause && !hdev->init_done) {
5690e65e175bSOded Gabbay 		dev_dbg(hdev->dev,
5691e65e175bSOded Gabbay 			"Clearing DMA0 engine from errors (cause 0x%x)\n",
5692e65e175bSOded Gabbay 			err_cause);
5693e65e175bSOded Gabbay 		WREG32(mmDMA0_CORE_ERR_CAUSE, err_cause);
5694e65e175bSOded Gabbay 	}
5695e65e175bSOded Gabbay 
5696e65e175bSOded Gabbay 	job->id = 0;
5697e65e175bSOded Gabbay 	job->user_cb = cb;
5698e65e175bSOded Gabbay 	atomic_inc(&job->user_cb->cs_cnt);
5699e65e175bSOded Gabbay 	job->user_cb_size = cb_size;
5700e65e175bSOded Gabbay 	job->hw_queue_id = GAUDI_QUEUE_ID_DMA_0_0;
5701e65e175bSOded Gabbay 	job->patched_cb = job->user_cb;
5702e65e175bSOded Gabbay 	job->job_cb_size = job->user_cb_size + sizeof(struct packet_msg_prot);
5703e65e175bSOded Gabbay 
5704e65e175bSOded Gabbay 	hl_debugfs_add_job(hdev, job);
5705e65e175bSOded Gabbay 
5706e65e175bSOded Gabbay 	rc = gaudi_send_job_on_qman0(hdev, job);
5707e65e175bSOded Gabbay 	hl_debugfs_remove_job(hdev, job);
5708e65e175bSOded Gabbay 	kfree(job);
5709e65e175bSOded Gabbay 	atomic_dec(&cb->cs_cnt);
5710e65e175bSOded Gabbay 
5711e65e175bSOded Gabbay 	/* Verify DMA is OK */
5712e65e175bSOded Gabbay 	err_cause = RREG32(mmDMA0_CORE_ERR_CAUSE);
5713e65e175bSOded Gabbay 	if (err_cause) {
5714e65e175bSOded Gabbay 		dev_err(hdev->dev, "DMA Failed, cause 0x%x\n", err_cause);
5715e65e175bSOded Gabbay 		rc = -EIO;
5716e65e175bSOded Gabbay 		if (!hdev->init_done) {
5717e65e175bSOded Gabbay 			dev_dbg(hdev->dev,
5718e65e175bSOded Gabbay 				"Clearing DMA0 engine from errors (cause 0x%x)\n",
5719e65e175bSOded Gabbay 				err_cause);
5720e65e175bSOded Gabbay 			WREG32(mmDMA0_CORE_ERR_CAUSE, err_cause);
5721e65e175bSOded Gabbay 		}
5722e65e175bSOded Gabbay 	}
5723e65e175bSOded Gabbay 
5724e65e175bSOded Gabbay release_cb:
5725e65e175bSOded Gabbay 	hl_cb_put(cb);
5726e65e175bSOded Gabbay 	hl_cb_destroy(&hdev->kernel_mem_mgr, cb->buf->handle);
5727e65e175bSOded Gabbay 
5728e65e175bSOded Gabbay 	return rc;
5729e65e175bSOded Gabbay }
5730e65e175bSOded Gabbay 
5731e65e175bSOded Gabbay static int gaudi_memset_registers(struct hl_device *hdev, u64 reg_base,
5732e65e175bSOded Gabbay 					u32 num_regs, u32 val)
5733e65e175bSOded Gabbay {
5734e65e175bSOded Gabbay 	struct packet_msg_long *pkt;
5735e65e175bSOded Gabbay 	struct hl_cs_job *job;
5736e65e175bSOded Gabbay 	u32 cb_size, ctl;
5737e65e175bSOded Gabbay 	struct hl_cb *cb;
5738e65e175bSOded Gabbay 	int i, rc;
5739e65e175bSOded Gabbay 
5740e65e175bSOded Gabbay 	cb_size = (sizeof(*pkt) * num_regs) + sizeof(struct packet_msg_prot);
5741e65e175bSOded Gabbay 
5742e65e175bSOded Gabbay 	if (cb_size > SZ_2M) {
5743e65e175bSOded Gabbay 		dev_err(hdev->dev, "CB size must be smaller than %uMB", SZ_2M);
5744e65e175bSOded Gabbay 		return -ENOMEM;
5745e65e175bSOded Gabbay 	}
5746e65e175bSOded Gabbay 
5747e65e175bSOded Gabbay 	cb = hl_cb_kernel_create(hdev, cb_size, false);
5748e65e175bSOded Gabbay 	if (!cb)
5749e65e175bSOded Gabbay 		return -EFAULT;
5750e65e175bSOded Gabbay 
5751e65e175bSOded Gabbay 	pkt = cb->kernel_address;
5752e65e175bSOded Gabbay 
5753e65e175bSOded Gabbay 	ctl = FIELD_PREP(GAUDI_PKT_LONG_CTL_OP_MASK, 0); /* write the value */
5754e65e175bSOded Gabbay 	ctl |= FIELD_PREP(GAUDI_PKT_CTL_OPCODE_MASK, PACKET_MSG_LONG);
5755e65e175bSOded Gabbay 	ctl |= FIELD_PREP(GAUDI_PKT_CTL_EB_MASK, 1);
5756e65e175bSOded Gabbay 	ctl |= FIELD_PREP(GAUDI_PKT_CTL_RB_MASK, 1);
5757e65e175bSOded Gabbay 	ctl |= FIELD_PREP(GAUDI_PKT_CTL_MB_MASK, 1);
5758e65e175bSOded Gabbay 
5759e65e175bSOded Gabbay 	for (i = 0; i < num_regs ; i++, pkt++) {
5760e65e175bSOded Gabbay 		pkt->ctl = cpu_to_le32(ctl);
5761e65e175bSOded Gabbay 		pkt->value = cpu_to_le32(val);
5762e65e175bSOded Gabbay 		pkt->addr = cpu_to_le64(reg_base + (i * 4));
5763e65e175bSOded Gabbay 	}
5764e65e175bSOded Gabbay 
5765e65e175bSOded Gabbay 	job = hl_cs_allocate_job(hdev, QUEUE_TYPE_EXT, true);
5766e65e175bSOded Gabbay 	if (!job) {
5767e65e175bSOded Gabbay 		dev_err(hdev->dev, "Failed to allocate a new job\n");
5768e65e175bSOded Gabbay 		rc = -ENOMEM;
5769e65e175bSOded Gabbay 		goto release_cb;
5770e65e175bSOded Gabbay 	}
5771e65e175bSOded Gabbay 
5772e65e175bSOded Gabbay 	job->id = 0;
5773e65e175bSOded Gabbay 	job->user_cb = cb;
5774e65e175bSOded Gabbay 	atomic_inc(&job->user_cb->cs_cnt);
5775e65e175bSOded Gabbay 	job->user_cb_size = cb_size;
5776e65e175bSOded Gabbay 	job->hw_queue_id = GAUDI_QUEUE_ID_DMA_0_0;
5777e65e175bSOded Gabbay 	job->patched_cb = job->user_cb;
5778e65e175bSOded Gabbay 	job->job_cb_size = cb_size;
5779e65e175bSOded Gabbay 
5780e65e175bSOded Gabbay 	hl_debugfs_add_job(hdev, job);
5781e65e175bSOded Gabbay 
5782e65e175bSOded Gabbay 	rc = gaudi_send_job_on_qman0(hdev, job);
5783e65e175bSOded Gabbay 	hl_debugfs_remove_job(hdev, job);
5784e65e175bSOded Gabbay 	kfree(job);
5785e65e175bSOded Gabbay 	atomic_dec(&cb->cs_cnt);
5786e65e175bSOded Gabbay 
5787e65e175bSOded Gabbay release_cb:
5788e65e175bSOded Gabbay 	hl_cb_put(cb);
5789e65e175bSOded Gabbay 	hl_cb_destroy(&hdev->kernel_mem_mgr, cb->buf->handle);
5790e65e175bSOded Gabbay 
5791e65e175bSOded Gabbay 	return rc;
5792e65e175bSOded Gabbay }
5793e65e175bSOded Gabbay 
5794e65e175bSOded Gabbay static int gaudi_restore_sm_registers(struct hl_device *hdev)
5795e65e175bSOded Gabbay {
5796e65e175bSOded Gabbay 	u64 base_addr;
5797e65e175bSOded Gabbay 	u32 num_regs;
5798e65e175bSOded Gabbay 	int rc;
5799e65e175bSOded Gabbay 
5800e65e175bSOded Gabbay 	base_addr = CFG_BASE + mmSYNC_MNGR_E_N_SYNC_MNGR_OBJS_SOB_OBJ_0;
5801e65e175bSOded Gabbay 	num_regs = NUM_OF_SOB_IN_BLOCK;
5802e65e175bSOded Gabbay 	rc = gaudi_memset_registers(hdev, base_addr, num_regs, 0);
5803e65e175bSOded Gabbay 	if (rc) {
5804e65e175bSOded Gabbay 		dev_err(hdev->dev, "failed resetting SM registers");
5805e65e175bSOded Gabbay 		return -ENOMEM;
5806e65e175bSOded Gabbay 	}
5807e65e175bSOded Gabbay 
5808e65e175bSOded Gabbay 	base_addr = CFG_BASE +  mmSYNC_MNGR_E_S_SYNC_MNGR_OBJS_SOB_OBJ_0;
5809e65e175bSOded Gabbay 	num_regs = NUM_OF_SOB_IN_BLOCK;
5810e65e175bSOded Gabbay 	rc = gaudi_memset_registers(hdev, base_addr, num_regs, 0);
5811e65e175bSOded Gabbay 	if (rc) {
5812e65e175bSOded Gabbay 		dev_err(hdev->dev, "failed resetting SM registers");
5813e65e175bSOded Gabbay 		return -ENOMEM;
5814e65e175bSOded Gabbay 	}
5815e65e175bSOded Gabbay 
5816e65e175bSOded Gabbay 	base_addr = CFG_BASE +  mmSYNC_MNGR_W_N_SYNC_MNGR_OBJS_SOB_OBJ_0;
5817e65e175bSOded Gabbay 	num_regs = NUM_OF_SOB_IN_BLOCK;
5818e65e175bSOded Gabbay 	rc = gaudi_memset_registers(hdev, base_addr, num_regs, 0);
5819e65e175bSOded Gabbay 	if (rc) {
5820e65e175bSOded Gabbay 		dev_err(hdev->dev, "failed resetting SM registers");
5821e65e175bSOded Gabbay 		return -ENOMEM;
5822e65e175bSOded Gabbay 	}
5823e65e175bSOded Gabbay 
5824e65e175bSOded Gabbay 	base_addr = CFG_BASE +  mmSYNC_MNGR_E_N_SYNC_MNGR_OBJS_MON_STATUS_0;
5825e65e175bSOded Gabbay 	num_regs = NUM_OF_MONITORS_IN_BLOCK;
5826e65e175bSOded Gabbay 	rc = gaudi_memset_registers(hdev, base_addr, num_regs, 0);
5827e65e175bSOded Gabbay 	if (rc) {
5828e65e175bSOded Gabbay 		dev_err(hdev->dev, "failed resetting SM registers");
5829e65e175bSOded Gabbay 		return -ENOMEM;
5830e65e175bSOded Gabbay 	}
5831e65e175bSOded Gabbay 
5832e65e175bSOded Gabbay 	base_addr = CFG_BASE +  mmSYNC_MNGR_E_S_SYNC_MNGR_OBJS_MON_STATUS_0;
5833e65e175bSOded Gabbay 	num_regs = NUM_OF_MONITORS_IN_BLOCK;
5834e65e175bSOded Gabbay 	rc = gaudi_memset_registers(hdev, base_addr, num_regs, 0);
5835e65e175bSOded Gabbay 	if (rc) {
5836e65e175bSOded Gabbay 		dev_err(hdev->dev, "failed resetting SM registers");
5837e65e175bSOded Gabbay 		return -ENOMEM;
5838e65e175bSOded Gabbay 	}
5839e65e175bSOded Gabbay 
5840e65e175bSOded Gabbay 	base_addr = CFG_BASE +  mmSYNC_MNGR_W_N_SYNC_MNGR_OBJS_MON_STATUS_0;
5841e65e175bSOded Gabbay 	num_regs = NUM_OF_MONITORS_IN_BLOCK;
5842e65e175bSOded Gabbay 	rc = gaudi_memset_registers(hdev, base_addr, num_regs, 0);
5843e65e175bSOded Gabbay 	if (rc) {
5844e65e175bSOded Gabbay 		dev_err(hdev->dev, "failed resetting SM registers");
5845e65e175bSOded Gabbay 		return -ENOMEM;
5846e65e175bSOded Gabbay 	}
5847e65e175bSOded Gabbay 
5848e65e175bSOded Gabbay 	base_addr = CFG_BASE +  mmSYNC_MNGR_W_S_SYNC_MNGR_OBJS_SOB_OBJ_0 +
5849e65e175bSOded Gabbay 			(GAUDI_FIRST_AVAILABLE_W_S_SYNC_OBJECT * 4);
5850e65e175bSOded Gabbay 	num_regs = NUM_OF_SOB_IN_BLOCK - GAUDI_FIRST_AVAILABLE_W_S_SYNC_OBJECT;
5851e65e175bSOded Gabbay 	rc = gaudi_memset_registers(hdev, base_addr, num_regs, 0);
5852e65e175bSOded Gabbay 	if (rc) {
5853e65e175bSOded Gabbay 		dev_err(hdev->dev, "failed resetting SM registers");
5854e65e175bSOded Gabbay 		return -ENOMEM;
5855e65e175bSOded Gabbay 	}
5856e65e175bSOded Gabbay 
5857e65e175bSOded Gabbay 	base_addr = CFG_BASE +  mmSYNC_MNGR_W_S_SYNC_MNGR_OBJS_MON_STATUS_0 +
5858e65e175bSOded Gabbay 			(GAUDI_FIRST_AVAILABLE_W_S_MONITOR * 4);
5859e65e175bSOded Gabbay 	num_regs = NUM_OF_MONITORS_IN_BLOCK - GAUDI_FIRST_AVAILABLE_W_S_MONITOR;
5860e65e175bSOded Gabbay 	rc = gaudi_memset_registers(hdev, base_addr, num_regs, 0);
5861e65e175bSOded Gabbay 	if (rc) {
5862e65e175bSOded Gabbay 		dev_err(hdev->dev, "failed resetting SM registers");
5863e65e175bSOded Gabbay 		return -ENOMEM;
5864e65e175bSOded Gabbay 	}
5865e65e175bSOded Gabbay 
5866e65e175bSOded Gabbay 	return 0;
5867e65e175bSOded Gabbay }
5868e65e175bSOded Gabbay 
5869e65e175bSOded Gabbay static void gaudi_restore_dma_registers(struct hl_device *hdev)
5870e65e175bSOded Gabbay {
5871e65e175bSOded Gabbay 	u32 sob_delta = mmSYNC_MNGR_E_N_SYNC_MNGR_OBJS_SOB_OBJ_1 -
5872e65e175bSOded Gabbay 			mmSYNC_MNGR_E_N_SYNC_MNGR_OBJS_SOB_OBJ_0;
5873e65e175bSOded Gabbay 	int i;
5874e65e175bSOded Gabbay 
5875e65e175bSOded Gabbay 	for (i = 0 ; i < DMA_NUMBER_OF_CHANNELS ; i++) {
5876e65e175bSOded Gabbay 		u64 sob_addr = CFG_BASE +
5877e65e175bSOded Gabbay 				mmSYNC_MNGR_E_N_SYNC_MNGR_OBJS_SOB_OBJ_0 +
5878e65e175bSOded Gabbay 				(i * sob_delta);
5879e65e175bSOded Gabbay 		u32 dma_offset = i * DMA_CORE_OFFSET;
5880e65e175bSOded Gabbay 
5881e65e175bSOded Gabbay 		WREG32(mmDMA0_CORE_WR_COMP_ADDR_LO + dma_offset,
5882e65e175bSOded Gabbay 				lower_32_bits(sob_addr));
5883e65e175bSOded Gabbay 		WREG32(mmDMA0_CORE_WR_COMP_ADDR_HI + dma_offset,
5884e65e175bSOded Gabbay 				upper_32_bits(sob_addr));
5885e65e175bSOded Gabbay 		WREG32(mmDMA0_CORE_WR_COMP_WDATA + dma_offset, 0x80000001);
5886e65e175bSOded Gabbay 
5887e65e175bSOded Gabbay 		/* For DMAs 2-7, need to restore WR_AWUSER_31_11 as it can be
5888e65e175bSOded Gabbay 		 * modified by the user for SRAM reduction
5889e65e175bSOded Gabbay 		 */
5890e65e175bSOded Gabbay 		if (i > 1)
5891e65e175bSOded Gabbay 			WREG32(mmDMA0_CORE_WR_AWUSER_31_11 + dma_offset,
5892e65e175bSOded Gabbay 								0x00000001);
5893e65e175bSOded Gabbay 	}
5894e65e175bSOded Gabbay }
5895e65e175bSOded Gabbay 
5896e65e175bSOded Gabbay static void gaudi_restore_qm_registers(struct hl_device *hdev)
5897e65e175bSOded Gabbay {
5898e65e175bSOded Gabbay 	u32 qman_offset;
5899e65e175bSOded Gabbay 	int i;
5900e65e175bSOded Gabbay 
5901e65e175bSOded Gabbay 	for (i = 0 ; i < DMA_NUMBER_OF_CHANNELS ; i++) {
5902e65e175bSOded Gabbay 		qman_offset = i * DMA_QMAN_OFFSET;
5903e65e175bSOded Gabbay 		WREG32(mmDMA0_QM_ARB_CFG_0 + qman_offset, 0);
5904e65e175bSOded Gabbay 	}
5905e65e175bSOded Gabbay 
5906e65e175bSOded Gabbay 	for (i = 0 ; i < MME_NUMBER_OF_MASTER_ENGINES ; i++) {
5907e65e175bSOded Gabbay 		qman_offset = i * (mmMME2_QM_BASE - mmMME0_QM_BASE);
5908e65e175bSOded Gabbay 		WREG32(mmMME0_QM_ARB_CFG_0 + qman_offset, 0);
5909e65e175bSOded Gabbay 	}
5910e65e175bSOded Gabbay 
5911e65e175bSOded Gabbay 	for (i = 0 ; i < TPC_NUMBER_OF_ENGINES ; i++) {
5912e65e175bSOded Gabbay 		qman_offset = i * TPC_QMAN_OFFSET;
5913e65e175bSOded Gabbay 		WREG32(mmTPC0_QM_ARB_CFG_0 + qman_offset, 0);
5914e65e175bSOded Gabbay 	}
5915e65e175bSOded Gabbay 
5916e65e175bSOded Gabbay 	for (i = 0 ; i < NIC_NUMBER_OF_ENGINES ; i++) {
5917e65e175bSOded Gabbay 		qman_offset = (i >> 1) * NIC_MACRO_QMAN_OFFSET +
5918e65e175bSOded Gabbay 				(i & 0x1) * NIC_ENGINE_QMAN_OFFSET;
5919e65e175bSOded Gabbay 		WREG32(mmNIC0_QM0_ARB_CFG_0 + qman_offset, 0);
5920e65e175bSOded Gabbay 	}
5921e65e175bSOded Gabbay }
5922e65e175bSOded Gabbay 
5923e65e175bSOded Gabbay static int gaudi_restore_user_registers(struct hl_device *hdev)
5924e65e175bSOded Gabbay {
5925e65e175bSOded Gabbay 	int rc;
5926e65e175bSOded Gabbay 
5927e65e175bSOded Gabbay 	rc = gaudi_restore_sm_registers(hdev);
5928e65e175bSOded Gabbay 	if (rc)
5929e65e175bSOded Gabbay 		return rc;
5930e65e175bSOded Gabbay 
5931e65e175bSOded Gabbay 	gaudi_restore_dma_registers(hdev);
5932e65e175bSOded Gabbay 	gaudi_restore_qm_registers(hdev);
5933e65e175bSOded Gabbay 
5934e65e175bSOded Gabbay 	return 0;
5935e65e175bSOded Gabbay }
5936e65e175bSOded Gabbay 
5937e65e175bSOded Gabbay static int gaudi_context_switch(struct hl_device *hdev, u32 asid)
5938e65e175bSOded Gabbay {
5939e65e175bSOded Gabbay 	return 0;
5940e65e175bSOded Gabbay }
5941e65e175bSOded Gabbay 
5942e65e175bSOded Gabbay static int gaudi_mmu_clear_pgt_range(struct hl_device *hdev)
5943e65e175bSOded Gabbay {
5944e65e175bSOded Gabbay 	u32 size = hdev->asic_prop.mmu_pgt_size +
5945e65e175bSOded Gabbay 			hdev->asic_prop.mmu_cache_mng_size;
5946e65e175bSOded Gabbay 	struct gaudi_device *gaudi = hdev->asic_specific;
5947e65e175bSOded Gabbay 	u64 addr = hdev->asic_prop.mmu_pgt_addr;
5948e65e175bSOded Gabbay 
5949e65e175bSOded Gabbay 	if (!(gaudi->hw_cap_initialized & HW_CAP_MMU))
5950e65e175bSOded Gabbay 		return 0;
5951e65e175bSOded Gabbay 
5952e65e175bSOded Gabbay 	return gaudi_memset_device_memory(hdev, addr, size, 0);
5953e65e175bSOded Gabbay }
5954e65e175bSOded Gabbay 
5955e65e175bSOded Gabbay static void gaudi_restore_phase_topology(struct hl_device *hdev)
5956e65e175bSOded Gabbay {
5957e65e175bSOded Gabbay 
5958e65e175bSOded Gabbay }
5959e65e175bSOded Gabbay 
5960e65e175bSOded Gabbay static int gaudi_dma_core_transfer(struct hl_device *hdev, int dma_id, u64 addr,
5961e65e175bSOded Gabbay 					u32 size_to_dma, dma_addr_t dma_addr)
5962e65e175bSOded Gabbay {
5963e65e175bSOded Gabbay 	u32 err_cause, val;
5964e65e175bSOded Gabbay 	u64 dma_offset;
5965e65e175bSOded Gabbay 	int rc;
5966e65e175bSOded Gabbay 
5967e65e175bSOded Gabbay 	dma_offset = dma_id * DMA_CORE_OFFSET;
5968e65e175bSOded Gabbay 
5969e65e175bSOded Gabbay 	WREG32(mmDMA0_CORE_SRC_BASE_LO + dma_offset, lower_32_bits(addr));
5970e65e175bSOded Gabbay 	WREG32(mmDMA0_CORE_SRC_BASE_HI + dma_offset, upper_32_bits(addr));
5971e65e175bSOded Gabbay 	WREG32(mmDMA0_CORE_DST_BASE_LO + dma_offset, lower_32_bits(dma_addr));
5972e65e175bSOded Gabbay 	WREG32(mmDMA0_CORE_DST_BASE_HI + dma_offset, upper_32_bits(dma_addr));
5973e65e175bSOded Gabbay 	WREG32(mmDMA0_CORE_DST_TSIZE_0 + dma_offset, size_to_dma);
5974e65e175bSOded Gabbay 	WREG32(mmDMA0_CORE_COMMIT + dma_offset,
5975e65e175bSOded Gabbay 			(1 << DMA0_CORE_COMMIT_LIN_SHIFT));
5976e65e175bSOded Gabbay 
5977e65e175bSOded Gabbay 	rc = hl_poll_timeout(
5978e65e175bSOded Gabbay 		hdev,
5979e65e175bSOded Gabbay 		mmDMA0_CORE_STS0 + dma_offset,
5980e65e175bSOded Gabbay 		val,
5981e65e175bSOded Gabbay 		((val & DMA0_CORE_STS0_BUSY_MASK) == 0),
5982e65e175bSOded Gabbay 		0,
5983e65e175bSOded Gabbay 		1000000);
5984e65e175bSOded Gabbay 
5985e65e175bSOded Gabbay 	if (rc) {
5986e65e175bSOded Gabbay 		dev_err(hdev->dev,
5987e65e175bSOded Gabbay 			"DMA %d timed-out during reading of 0x%llx\n",
5988e65e175bSOded Gabbay 			dma_id, addr);
5989e65e175bSOded Gabbay 		return -EIO;
5990e65e175bSOded Gabbay 	}
5991e65e175bSOded Gabbay 
5992e65e175bSOded Gabbay 	/* Verify DMA is OK */
5993e65e175bSOded Gabbay 	err_cause = RREG32(mmDMA0_CORE_ERR_CAUSE + dma_offset);
5994e65e175bSOded Gabbay 	if (err_cause) {
5995e65e175bSOded Gabbay 		dev_err(hdev->dev, "DMA Failed, cause 0x%x\n", err_cause);
5996e65e175bSOded Gabbay 		dev_dbg(hdev->dev,
5997e65e175bSOded Gabbay 			"Clearing DMA0 engine from errors (cause 0x%x)\n",
5998e65e175bSOded Gabbay 			err_cause);
5999e65e175bSOded Gabbay 		WREG32(mmDMA0_CORE_ERR_CAUSE + dma_offset, err_cause);
6000e65e175bSOded Gabbay 
6001e65e175bSOded Gabbay 		return -EIO;
6002e65e175bSOded Gabbay 	}
6003e65e175bSOded Gabbay 
6004e65e175bSOded Gabbay 	return 0;
6005e65e175bSOded Gabbay }
6006e65e175bSOded Gabbay 
6007e65e175bSOded Gabbay static int gaudi_debugfs_read_dma(struct hl_device *hdev, u64 addr, u32 size,
6008e65e175bSOded Gabbay 				void *blob_addr)
6009e65e175bSOded Gabbay {
6010e65e175bSOded Gabbay 	u32 dma_core_sts0, err_cause, cfg1, size_left, pos, size_to_dma;
6011e65e175bSOded Gabbay 	u32 qm_glbl_sts0, qm_cgm_sts;
6012e65e175bSOded Gabbay 	u64 dma_offset, qm_offset;
6013e65e175bSOded Gabbay 	dma_addr_t dma_addr;
6014e65e175bSOded Gabbay 	void *kernel_addr;
6015e65e175bSOded Gabbay 	bool is_eng_idle;
6016e65e175bSOded Gabbay 	int rc = 0, dma_id;
6017e65e175bSOded Gabbay 
6018e65e175bSOded Gabbay 	kernel_addr = hl_asic_dma_alloc_coherent(hdev, SZ_2M, &dma_addr, GFP_KERNEL | __GFP_ZERO);
6019e65e175bSOded Gabbay 
6020e65e175bSOded Gabbay 	if (!kernel_addr)
6021e65e175bSOded Gabbay 		return -ENOMEM;
6022e65e175bSOded Gabbay 
6023e65e175bSOded Gabbay 	hdev->asic_funcs->hw_queues_lock(hdev);
6024e65e175bSOded Gabbay 
6025e65e175bSOded Gabbay 	dma_id = gaudi_dma_assignment[GAUDI_PCI_DMA_1];
6026e65e175bSOded Gabbay 	dma_offset = dma_id * DMA_CORE_OFFSET;
6027e65e175bSOded Gabbay 	qm_offset = dma_id * DMA_QMAN_OFFSET;
6028e65e175bSOded Gabbay 	dma_core_sts0 = RREG32(mmDMA0_CORE_STS0 + dma_offset);
6029e65e175bSOded Gabbay 	qm_glbl_sts0 = RREG32(mmDMA0_QM_GLBL_STS0 + qm_offset);
6030e65e175bSOded Gabbay 	qm_cgm_sts = RREG32(mmDMA0_QM_CGM_STS + qm_offset);
6031e65e175bSOded Gabbay 	is_eng_idle = IS_QM_IDLE(qm_glbl_sts0, qm_cgm_sts) &&
6032e65e175bSOded Gabbay 		      IS_DMA_IDLE(dma_core_sts0);
6033e65e175bSOded Gabbay 
6034e65e175bSOded Gabbay 	if (!is_eng_idle) {
6035e65e175bSOded Gabbay 		dma_id = gaudi_dma_assignment[GAUDI_PCI_DMA_2];
6036e65e175bSOded Gabbay 		dma_offset = dma_id * DMA_CORE_OFFSET;
6037e65e175bSOded Gabbay 		qm_offset = dma_id * DMA_QMAN_OFFSET;
6038e65e175bSOded Gabbay 		dma_core_sts0 = RREG32(mmDMA0_CORE_STS0 + dma_offset);
6039e65e175bSOded Gabbay 		qm_glbl_sts0 = RREG32(mmDMA0_QM_GLBL_STS0 + qm_offset);
6040e65e175bSOded Gabbay 		qm_cgm_sts = RREG32(mmDMA0_QM_CGM_STS + qm_offset);
6041e65e175bSOded Gabbay 		is_eng_idle = IS_QM_IDLE(qm_glbl_sts0, qm_cgm_sts) &&
6042e65e175bSOded Gabbay 			      IS_DMA_IDLE(dma_core_sts0);
6043e65e175bSOded Gabbay 
6044e65e175bSOded Gabbay 		if (!is_eng_idle) {
6045e65e175bSOded Gabbay 			dev_err_ratelimited(hdev->dev,
6046e65e175bSOded Gabbay 				"Can't read via DMA because it is BUSY\n");
6047e65e175bSOded Gabbay 			rc = -EAGAIN;
6048e65e175bSOded Gabbay 			goto out;
6049e65e175bSOded Gabbay 		}
6050e65e175bSOded Gabbay 	}
6051e65e175bSOded Gabbay 
6052e65e175bSOded Gabbay 	cfg1 = RREG32(mmDMA0_QM_GLBL_CFG1 + qm_offset);
6053e65e175bSOded Gabbay 	WREG32(mmDMA0_QM_GLBL_CFG1 + qm_offset,
6054e65e175bSOded Gabbay 			0xF << DMA0_QM_GLBL_CFG1_CP_STOP_SHIFT);
6055e65e175bSOded Gabbay 
6056e65e175bSOded Gabbay 	/* TODO: remove this by mapping the DMA temporary buffer to the MMU
6057e65e175bSOded Gabbay 	 * using the compute ctx ASID, if exists. If not, use the kernel ctx
6058e65e175bSOded Gabbay 	 * ASID
6059e65e175bSOded Gabbay 	 */
6060e65e175bSOded Gabbay 	WREG32_OR(mmDMA0_CORE_PROT + dma_offset, BIT(DMA0_CORE_PROT_VAL_SHIFT));
6061e65e175bSOded Gabbay 
6062e65e175bSOded Gabbay 	/* Verify DMA is OK */
6063e65e175bSOded Gabbay 	err_cause = RREG32(mmDMA0_CORE_ERR_CAUSE + dma_offset);
6064e65e175bSOded Gabbay 	if (err_cause) {
6065e65e175bSOded Gabbay 		dev_dbg(hdev->dev,
6066e65e175bSOded Gabbay 			"Clearing DMA0 engine from errors (cause 0x%x)\n",
6067e65e175bSOded Gabbay 			err_cause);
6068e65e175bSOded Gabbay 		WREG32(mmDMA0_CORE_ERR_CAUSE + dma_offset, err_cause);
6069e65e175bSOded Gabbay 	}
6070e65e175bSOded Gabbay 
6071e65e175bSOded Gabbay 	pos = 0;
6072e65e175bSOded Gabbay 	size_left = size;
6073e65e175bSOded Gabbay 	size_to_dma = SZ_2M;
6074e65e175bSOded Gabbay 
6075e65e175bSOded Gabbay 	while (size_left > 0) {
6076e65e175bSOded Gabbay 
6077e65e175bSOded Gabbay 		if (size_left < SZ_2M)
6078e65e175bSOded Gabbay 			size_to_dma = size_left;
6079e65e175bSOded Gabbay 
6080e65e175bSOded Gabbay 		rc = gaudi_dma_core_transfer(hdev, dma_id, addr, size_to_dma,
6081e65e175bSOded Gabbay 						dma_addr);
6082e65e175bSOded Gabbay 		if (rc)
6083e65e175bSOded Gabbay 			break;
6084e65e175bSOded Gabbay 
6085e65e175bSOded Gabbay 		memcpy(blob_addr + pos, kernel_addr, size_to_dma);
6086e65e175bSOded Gabbay 
6087e65e175bSOded Gabbay 		if (size_left <= SZ_2M)
6088e65e175bSOded Gabbay 			break;
6089e65e175bSOded Gabbay 
6090e65e175bSOded Gabbay 		pos += SZ_2M;
6091e65e175bSOded Gabbay 		addr += SZ_2M;
6092e65e175bSOded Gabbay 		size_left -= SZ_2M;
6093e65e175bSOded Gabbay 	}
6094e65e175bSOded Gabbay 
6095e65e175bSOded Gabbay 	/* TODO: remove this by mapping the DMA temporary buffer to the MMU
6096e65e175bSOded Gabbay 	 * using the compute ctx ASID, if exists. If not, use the kernel ctx
6097e65e175bSOded Gabbay 	 * ASID
6098e65e175bSOded Gabbay 	 */
6099e65e175bSOded Gabbay 	WREG32_AND(mmDMA0_CORE_PROT + dma_offset,
6100e65e175bSOded Gabbay 			~BIT(DMA0_CORE_PROT_VAL_SHIFT));
6101e65e175bSOded Gabbay 
6102e65e175bSOded Gabbay 	WREG32(mmDMA0_QM_GLBL_CFG1 + qm_offset, cfg1);
6103e65e175bSOded Gabbay 
6104e65e175bSOded Gabbay out:
6105e65e175bSOded Gabbay 	hdev->asic_funcs->hw_queues_unlock(hdev);
6106e65e175bSOded Gabbay 
6107e65e175bSOded Gabbay 	hl_asic_dma_free_coherent(hdev, SZ_2M, kernel_addr, dma_addr);
6108e65e175bSOded Gabbay 
6109e65e175bSOded Gabbay 	return rc;
6110e65e175bSOded Gabbay }
6111e65e175bSOded Gabbay 
6112e65e175bSOded Gabbay static u64 gaudi_read_pte(struct hl_device *hdev, u64 addr)
6113e65e175bSOded Gabbay {
6114e65e175bSOded Gabbay 	struct gaudi_device *gaudi = hdev->asic_specific;
6115e65e175bSOded Gabbay 
6116e65e175bSOded Gabbay 	if (hdev->reset_info.hard_reset_pending)
6117e65e175bSOded Gabbay 		return U64_MAX;
6118e65e175bSOded Gabbay 
6119e65e175bSOded Gabbay 	return readq(hdev->pcie_bar[HBM_BAR_ID] +
6120e65e175bSOded Gabbay 			(addr - gaudi->hbm_bar_cur_addr));
6121e65e175bSOded Gabbay }
6122e65e175bSOded Gabbay 
6123e65e175bSOded Gabbay static void gaudi_write_pte(struct hl_device *hdev, u64 addr, u64 val)
6124e65e175bSOded Gabbay {
6125e65e175bSOded Gabbay 	struct gaudi_device *gaudi = hdev->asic_specific;
6126e65e175bSOded Gabbay 
6127e65e175bSOded Gabbay 	if (hdev->reset_info.hard_reset_pending)
6128e65e175bSOded Gabbay 		return;
6129e65e175bSOded Gabbay 
6130e65e175bSOded Gabbay 	writeq(val, hdev->pcie_bar[HBM_BAR_ID] +
6131e65e175bSOded Gabbay 			(addr - gaudi->hbm_bar_cur_addr));
6132e65e175bSOded Gabbay }
6133e65e175bSOded Gabbay 
6134e65e175bSOded Gabbay void gaudi_mmu_prepare_reg(struct hl_device *hdev, u64 reg, u32 asid)
6135e65e175bSOded Gabbay {
6136e65e175bSOded Gabbay 	/* mask to zero the MMBP and ASID bits */
6137e65e175bSOded Gabbay 	WREG32_AND(reg, ~0x7FF);
6138e65e175bSOded Gabbay 	WREG32_OR(reg, asid);
6139e65e175bSOded Gabbay }
6140e65e175bSOded Gabbay 
6141e65e175bSOded Gabbay static void gaudi_mmu_prepare(struct hl_device *hdev, u32 asid)
6142e65e175bSOded Gabbay {
6143e65e175bSOded Gabbay 	struct gaudi_device *gaudi = hdev->asic_specific;
6144e65e175bSOded Gabbay 
6145e65e175bSOded Gabbay 	if (!(gaudi->hw_cap_initialized & HW_CAP_MMU))
6146e65e175bSOded Gabbay 		return;
6147e65e175bSOded Gabbay 
6148e65e175bSOded Gabbay 	if (asid & ~DMA0_QM_GLBL_NON_SECURE_PROPS_0_ASID_MASK) {
6149e65e175bSOded Gabbay 		dev_crit(hdev->dev, "asid %u is too big\n", asid);
6150e65e175bSOded Gabbay 		return;
6151e65e175bSOded Gabbay 	}
6152e65e175bSOded Gabbay 
6153e65e175bSOded Gabbay 	gaudi_mmu_prepare_reg(hdev, mmDMA0_QM_GLBL_NON_SECURE_PROPS_0, asid);
6154e65e175bSOded Gabbay 	gaudi_mmu_prepare_reg(hdev, mmDMA0_QM_GLBL_NON_SECURE_PROPS_1, asid);
6155e65e175bSOded Gabbay 	gaudi_mmu_prepare_reg(hdev, mmDMA0_QM_GLBL_NON_SECURE_PROPS_2, asid);
6156e65e175bSOded Gabbay 	gaudi_mmu_prepare_reg(hdev, mmDMA0_QM_GLBL_NON_SECURE_PROPS_3, asid);
6157e65e175bSOded Gabbay 	gaudi_mmu_prepare_reg(hdev, mmDMA0_QM_GLBL_NON_SECURE_PROPS_4, asid);
6158e65e175bSOded Gabbay 
6159e65e175bSOded Gabbay 	gaudi_mmu_prepare_reg(hdev, mmDMA1_QM_GLBL_NON_SECURE_PROPS_0, asid);
6160e65e175bSOded Gabbay 	gaudi_mmu_prepare_reg(hdev, mmDMA1_QM_GLBL_NON_SECURE_PROPS_1, asid);
6161e65e175bSOded Gabbay 	gaudi_mmu_prepare_reg(hdev, mmDMA1_QM_GLBL_NON_SECURE_PROPS_2, asid);
6162e65e175bSOded Gabbay 	gaudi_mmu_prepare_reg(hdev, mmDMA1_QM_GLBL_NON_SECURE_PROPS_3, asid);
6163e65e175bSOded Gabbay 	gaudi_mmu_prepare_reg(hdev, mmDMA1_QM_GLBL_NON_SECURE_PROPS_4, asid);
6164e65e175bSOded Gabbay 
6165e65e175bSOded Gabbay 	gaudi_mmu_prepare_reg(hdev, mmDMA2_QM_GLBL_NON_SECURE_PROPS_0, asid);
6166e65e175bSOded Gabbay 	gaudi_mmu_prepare_reg(hdev, mmDMA2_QM_GLBL_NON_SECURE_PROPS_1, asid);
6167e65e175bSOded Gabbay 	gaudi_mmu_prepare_reg(hdev, mmDMA2_QM_GLBL_NON_SECURE_PROPS_2, asid);
6168e65e175bSOded Gabbay 	gaudi_mmu_prepare_reg(hdev, mmDMA2_QM_GLBL_NON_SECURE_PROPS_3, asid);
6169e65e175bSOded Gabbay 	gaudi_mmu_prepare_reg(hdev, mmDMA2_QM_GLBL_NON_SECURE_PROPS_4, asid);
6170e65e175bSOded Gabbay 
6171e65e175bSOded Gabbay 	gaudi_mmu_prepare_reg(hdev, mmDMA3_QM_GLBL_NON_SECURE_PROPS_0, asid);
6172e65e175bSOded Gabbay 	gaudi_mmu_prepare_reg(hdev, mmDMA3_QM_GLBL_NON_SECURE_PROPS_1, asid);
6173e65e175bSOded Gabbay 	gaudi_mmu_prepare_reg(hdev, mmDMA3_QM_GLBL_NON_SECURE_PROPS_2, asid);
6174e65e175bSOded Gabbay 	gaudi_mmu_prepare_reg(hdev, mmDMA3_QM_GLBL_NON_SECURE_PROPS_3, asid);
6175e65e175bSOded Gabbay 	gaudi_mmu_prepare_reg(hdev, mmDMA3_QM_GLBL_NON_SECURE_PROPS_4, asid);
6176e65e175bSOded Gabbay 
6177e65e175bSOded Gabbay 	gaudi_mmu_prepare_reg(hdev, mmDMA4_QM_GLBL_NON_SECURE_PROPS_0, asid);
6178e65e175bSOded Gabbay 	gaudi_mmu_prepare_reg(hdev, mmDMA4_QM_GLBL_NON_SECURE_PROPS_1, asid);
6179e65e175bSOded Gabbay 	gaudi_mmu_prepare_reg(hdev, mmDMA4_QM_GLBL_NON_SECURE_PROPS_2, asid);
6180e65e175bSOded Gabbay 	gaudi_mmu_prepare_reg(hdev, mmDMA4_QM_GLBL_NON_SECURE_PROPS_3, asid);
6181e65e175bSOded Gabbay 	gaudi_mmu_prepare_reg(hdev, mmDMA4_QM_GLBL_NON_SECURE_PROPS_4, asid);
6182e65e175bSOded Gabbay 
6183e65e175bSOded Gabbay 	gaudi_mmu_prepare_reg(hdev, mmDMA5_QM_GLBL_NON_SECURE_PROPS_0, asid);
6184e65e175bSOded Gabbay 	gaudi_mmu_prepare_reg(hdev, mmDMA5_QM_GLBL_NON_SECURE_PROPS_1, asid);
6185e65e175bSOded Gabbay 	gaudi_mmu_prepare_reg(hdev, mmDMA5_QM_GLBL_NON_SECURE_PROPS_2, asid);
6186e65e175bSOded Gabbay 	gaudi_mmu_prepare_reg(hdev, mmDMA5_QM_GLBL_NON_SECURE_PROPS_3, asid);
6187e65e175bSOded Gabbay 	gaudi_mmu_prepare_reg(hdev, mmDMA5_QM_GLBL_NON_SECURE_PROPS_4, asid);
6188e65e175bSOded Gabbay 
6189e65e175bSOded Gabbay 	gaudi_mmu_prepare_reg(hdev, mmDMA6_QM_GLBL_NON_SECURE_PROPS_0, asid);
6190e65e175bSOded Gabbay 	gaudi_mmu_prepare_reg(hdev, mmDMA6_QM_GLBL_NON_SECURE_PROPS_1, asid);
6191e65e175bSOded Gabbay 	gaudi_mmu_prepare_reg(hdev, mmDMA6_QM_GLBL_NON_SECURE_PROPS_2, asid);
6192e65e175bSOded Gabbay 	gaudi_mmu_prepare_reg(hdev, mmDMA6_QM_GLBL_NON_SECURE_PROPS_3, asid);
6193e65e175bSOded Gabbay 	gaudi_mmu_prepare_reg(hdev, mmDMA6_QM_GLBL_NON_SECURE_PROPS_4, asid);
6194e65e175bSOded Gabbay 
6195e65e175bSOded Gabbay 	gaudi_mmu_prepare_reg(hdev, mmDMA7_QM_GLBL_NON_SECURE_PROPS_0, asid);
6196e65e175bSOded Gabbay 	gaudi_mmu_prepare_reg(hdev, mmDMA7_QM_GLBL_NON_SECURE_PROPS_1, asid);
6197e65e175bSOded Gabbay 	gaudi_mmu_prepare_reg(hdev, mmDMA7_QM_GLBL_NON_SECURE_PROPS_2, asid);
6198e65e175bSOded Gabbay 	gaudi_mmu_prepare_reg(hdev, mmDMA7_QM_GLBL_NON_SECURE_PROPS_3, asid);
6199e65e175bSOded Gabbay 	gaudi_mmu_prepare_reg(hdev, mmDMA7_QM_GLBL_NON_SECURE_PROPS_4, asid);
6200e65e175bSOded Gabbay 
6201e65e175bSOded Gabbay 	gaudi_mmu_prepare_reg(hdev, mmDMA0_CORE_NON_SECURE_PROPS, asid);
6202e65e175bSOded Gabbay 	gaudi_mmu_prepare_reg(hdev, mmDMA1_CORE_NON_SECURE_PROPS, asid);
6203e65e175bSOded Gabbay 	gaudi_mmu_prepare_reg(hdev, mmDMA2_CORE_NON_SECURE_PROPS, asid);
6204e65e175bSOded Gabbay 	gaudi_mmu_prepare_reg(hdev, mmDMA3_CORE_NON_SECURE_PROPS, asid);
6205e65e175bSOded Gabbay 	gaudi_mmu_prepare_reg(hdev, mmDMA4_CORE_NON_SECURE_PROPS, asid);
6206e65e175bSOded Gabbay 	gaudi_mmu_prepare_reg(hdev, mmDMA5_CORE_NON_SECURE_PROPS, asid);
6207e65e175bSOded Gabbay 	gaudi_mmu_prepare_reg(hdev, mmDMA6_CORE_NON_SECURE_PROPS, asid);
6208e65e175bSOded Gabbay 	gaudi_mmu_prepare_reg(hdev, mmDMA7_CORE_NON_SECURE_PROPS, asid);
6209e65e175bSOded Gabbay 
6210e65e175bSOded Gabbay 	gaudi_mmu_prepare_reg(hdev, mmTPC0_QM_GLBL_NON_SECURE_PROPS_0, asid);
6211e65e175bSOded Gabbay 	gaudi_mmu_prepare_reg(hdev, mmTPC0_QM_GLBL_NON_SECURE_PROPS_1, asid);
6212e65e175bSOded Gabbay 	gaudi_mmu_prepare_reg(hdev, mmTPC0_QM_GLBL_NON_SECURE_PROPS_2, asid);
6213e65e175bSOded Gabbay 	gaudi_mmu_prepare_reg(hdev, mmTPC0_QM_GLBL_NON_SECURE_PROPS_3, asid);
6214e65e175bSOded Gabbay 	gaudi_mmu_prepare_reg(hdev, mmTPC0_QM_GLBL_NON_SECURE_PROPS_4, asid);
6215e65e175bSOded Gabbay 	gaudi_mmu_prepare_reg(hdev, mmTPC0_CFG_ARUSER_LO, asid);
6216e65e175bSOded Gabbay 	gaudi_mmu_prepare_reg(hdev, mmTPC0_CFG_AWUSER_LO, asid);
6217e65e175bSOded Gabbay 
6218e65e175bSOded Gabbay 	gaudi_mmu_prepare_reg(hdev, mmTPC1_QM_GLBL_NON_SECURE_PROPS_0, asid);
6219e65e175bSOded Gabbay 	gaudi_mmu_prepare_reg(hdev, mmTPC1_QM_GLBL_NON_SECURE_PROPS_1, asid);
6220e65e175bSOded Gabbay 	gaudi_mmu_prepare_reg(hdev, mmTPC1_QM_GLBL_NON_SECURE_PROPS_2, asid);
6221e65e175bSOded Gabbay 	gaudi_mmu_prepare_reg(hdev, mmTPC1_QM_GLBL_NON_SECURE_PROPS_3, asid);
6222e65e175bSOded Gabbay 	gaudi_mmu_prepare_reg(hdev, mmTPC1_QM_GLBL_NON_SECURE_PROPS_4, asid);
6223e65e175bSOded Gabbay 	gaudi_mmu_prepare_reg(hdev, mmTPC1_CFG_ARUSER_LO, asid);
6224e65e175bSOded Gabbay 	gaudi_mmu_prepare_reg(hdev, mmTPC1_CFG_AWUSER_LO, asid);
6225e65e175bSOded Gabbay 
6226e65e175bSOded Gabbay 	gaudi_mmu_prepare_reg(hdev, mmTPC2_QM_GLBL_NON_SECURE_PROPS_0, asid);
6227e65e175bSOded Gabbay 	gaudi_mmu_prepare_reg(hdev, mmTPC2_QM_GLBL_NON_SECURE_PROPS_1, asid);
6228e65e175bSOded Gabbay 	gaudi_mmu_prepare_reg(hdev, mmTPC2_QM_GLBL_NON_SECURE_PROPS_2, asid);
6229e65e175bSOded Gabbay 	gaudi_mmu_prepare_reg(hdev, mmTPC2_QM_GLBL_NON_SECURE_PROPS_3, asid);
6230e65e175bSOded Gabbay 	gaudi_mmu_prepare_reg(hdev, mmTPC2_QM_GLBL_NON_SECURE_PROPS_4, asid);
6231e65e175bSOded Gabbay 	gaudi_mmu_prepare_reg(hdev, mmTPC2_CFG_ARUSER_LO, asid);
6232e65e175bSOded Gabbay 	gaudi_mmu_prepare_reg(hdev, mmTPC2_CFG_AWUSER_LO, asid);
6233e65e175bSOded Gabbay 
6234e65e175bSOded Gabbay 	gaudi_mmu_prepare_reg(hdev, mmTPC3_QM_GLBL_NON_SECURE_PROPS_0, asid);
6235e65e175bSOded Gabbay 	gaudi_mmu_prepare_reg(hdev, mmTPC3_QM_GLBL_NON_SECURE_PROPS_1, asid);
6236e65e175bSOded Gabbay 	gaudi_mmu_prepare_reg(hdev, mmTPC3_QM_GLBL_NON_SECURE_PROPS_2, asid);
6237e65e175bSOded Gabbay 	gaudi_mmu_prepare_reg(hdev, mmTPC3_QM_GLBL_NON_SECURE_PROPS_3, asid);
6238e65e175bSOded Gabbay 	gaudi_mmu_prepare_reg(hdev, mmTPC3_QM_GLBL_NON_SECURE_PROPS_4, asid);
6239e65e175bSOded Gabbay 	gaudi_mmu_prepare_reg(hdev, mmTPC3_CFG_ARUSER_LO, asid);
6240e65e175bSOded Gabbay 	gaudi_mmu_prepare_reg(hdev, mmTPC3_CFG_AWUSER_LO, asid);
6241e65e175bSOded Gabbay 
6242e65e175bSOded Gabbay 	gaudi_mmu_prepare_reg(hdev, mmTPC4_QM_GLBL_NON_SECURE_PROPS_0, asid);
6243e65e175bSOded Gabbay 	gaudi_mmu_prepare_reg(hdev, mmTPC4_QM_GLBL_NON_SECURE_PROPS_1, asid);
6244e65e175bSOded Gabbay 	gaudi_mmu_prepare_reg(hdev, mmTPC4_QM_GLBL_NON_SECURE_PROPS_2, asid);
6245e65e175bSOded Gabbay 	gaudi_mmu_prepare_reg(hdev, mmTPC4_QM_GLBL_NON_SECURE_PROPS_3, asid);
6246e65e175bSOded Gabbay 	gaudi_mmu_prepare_reg(hdev, mmTPC4_QM_GLBL_NON_SECURE_PROPS_4, asid);
6247e65e175bSOded Gabbay 	gaudi_mmu_prepare_reg(hdev, mmTPC4_CFG_ARUSER_LO, asid);
6248e65e175bSOded Gabbay 	gaudi_mmu_prepare_reg(hdev, mmTPC4_CFG_AWUSER_LO, asid);
6249e65e175bSOded Gabbay 
6250e65e175bSOded Gabbay 	gaudi_mmu_prepare_reg(hdev, mmTPC5_QM_GLBL_NON_SECURE_PROPS_0, asid);
6251e65e175bSOded Gabbay 	gaudi_mmu_prepare_reg(hdev, mmTPC5_QM_GLBL_NON_SECURE_PROPS_1, asid);
6252e65e175bSOded Gabbay 	gaudi_mmu_prepare_reg(hdev, mmTPC5_QM_GLBL_NON_SECURE_PROPS_2, asid);
6253e65e175bSOded Gabbay 	gaudi_mmu_prepare_reg(hdev, mmTPC5_QM_GLBL_NON_SECURE_PROPS_3, asid);
6254e65e175bSOded Gabbay 	gaudi_mmu_prepare_reg(hdev, mmTPC5_QM_GLBL_NON_SECURE_PROPS_4, asid);
6255e65e175bSOded Gabbay 	gaudi_mmu_prepare_reg(hdev, mmTPC5_CFG_ARUSER_LO, asid);
6256e65e175bSOded Gabbay 	gaudi_mmu_prepare_reg(hdev, mmTPC5_CFG_AWUSER_LO, asid);
6257e65e175bSOded Gabbay 
6258e65e175bSOded Gabbay 	gaudi_mmu_prepare_reg(hdev, mmTPC6_QM_GLBL_NON_SECURE_PROPS_0, asid);
6259e65e175bSOded Gabbay 	gaudi_mmu_prepare_reg(hdev, mmTPC6_QM_GLBL_NON_SECURE_PROPS_1, asid);
6260e65e175bSOded Gabbay 	gaudi_mmu_prepare_reg(hdev, mmTPC6_QM_GLBL_NON_SECURE_PROPS_2, asid);
6261e65e175bSOded Gabbay 	gaudi_mmu_prepare_reg(hdev, mmTPC6_QM_GLBL_NON_SECURE_PROPS_3, asid);
6262e65e175bSOded Gabbay 	gaudi_mmu_prepare_reg(hdev, mmTPC6_QM_GLBL_NON_SECURE_PROPS_4, asid);
6263e65e175bSOded Gabbay 	gaudi_mmu_prepare_reg(hdev, mmTPC6_CFG_ARUSER_LO, asid);
6264e65e175bSOded Gabbay 	gaudi_mmu_prepare_reg(hdev, mmTPC6_CFG_AWUSER_LO, asid);
6265e65e175bSOded Gabbay 
6266e65e175bSOded Gabbay 	gaudi_mmu_prepare_reg(hdev, mmTPC7_QM_GLBL_NON_SECURE_PROPS_0, asid);
6267e65e175bSOded Gabbay 	gaudi_mmu_prepare_reg(hdev, mmTPC7_QM_GLBL_NON_SECURE_PROPS_1, asid);
6268e65e175bSOded Gabbay 	gaudi_mmu_prepare_reg(hdev, mmTPC7_QM_GLBL_NON_SECURE_PROPS_2, asid);
6269e65e175bSOded Gabbay 	gaudi_mmu_prepare_reg(hdev, mmTPC7_QM_GLBL_NON_SECURE_PROPS_3, asid);
6270e65e175bSOded Gabbay 	gaudi_mmu_prepare_reg(hdev, mmTPC7_QM_GLBL_NON_SECURE_PROPS_4, asid);
6271e65e175bSOded Gabbay 	gaudi_mmu_prepare_reg(hdev, mmTPC7_CFG_ARUSER_LO, asid);
6272e65e175bSOded Gabbay 	gaudi_mmu_prepare_reg(hdev, mmTPC7_CFG_AWUSER_LO, asid);
6273e65e175bSOded Gabbay 
6274e65e175bSOded Gabbay 	gaudi_mmu_prepare_reg(hdev, mmMME0_QM_GLBL_NON_SECURE_PROPS_0, asid);
6275e65e175bSOded Gabbay 	gaudi_mmu_prepare_reg(hdev, mmMME0_QM_GLBL_NON_SECURE_PROPS_1, asid);
6276e65e175bSOded Gabbay 	gaudi_mmu_prepare_reg(hdev, mmMME0_QM_GLBL_NON_SECURE_PROPS_2, asid);
6277e65e175bSOded Gabbay 	gaudi_mmu_prepare_reg(hdev, mmMME0_QM_GLBL_NON_SECURE_PROPS_3, asid);
6278e65e175bSOded Gabbay 	gaudi_mmu_prepare_reg(hdev, mmMME0_QM_GLBL_NON_SECURE_PROPS_4, asid);
6279e65e175bSOded Gabbay 	gaudi_mmu_prepare_reg(hdev, mmMME2_QM_GLBL_NON_SECURE_PROPS_0, asid);
6280e65e175bSOded Gabbay 	gaudi_mmu_prepare_reg(hdev, mmMME2_QM_GLBL_NON_SECURE_PROPS_1, asid);
6281e65e175bSOded Gabbay 	gaudi_mmu_prepare_reg(hdev, mmMME2_QM_GLBL_NON_SECURE_PROPS_2, asid);
6282e65e175bSOded Gabbay 	gaudi_mmu_prepare_reg(hdev, mmMME2_QM_GLBL_NON_SECURE_PROPS_3, asid);
6283e65e175bSOded Gabbay 	gaudi_mmu_prepare_reg(hdev, mmMME2_QM_GLBL_NON_SECURE_PROPS_4, asid);
6284e65e175bSOded Gabbay 
6285e65e175bSOded Gabbay 	gaudi_mmu_prepare_reg(hdev, mmMME0_SBAB_ARUSER0, asid);
6286e65e175bSOded Gabbay 	gaudi_mmu_prepare_reg(hdev, mmMME0_SBAB_ARUSER1, asid);
6287e65e175bSOded Gabbay 	gaudi_mmu_prepare_reg(hdev, mmMME1_SBAB_ARUSER0, asid);
6288e65e175bSOded Gabbay 	gaudi_mmu_prepare_reg(hdev, mmMME1_SBAB_ARUSER1, asid);
6289e65e175bSOded Gabbay 	gaudi_mmu_prepare_reg(hdev, mmMME2_SBAB_ARUSER0, asid);
6290e65e175bSOded Gabbay 	gaudi_mmu_prepare_reg(hdev, mmMME2_SBAB_ARUSER1, asid);
6291e65e175bSOded Gabbay 	gaudi_mmu_prepare_reg(hdev, mmMME3_SBAB_ARUSER0, asid);
6292e65e175bSOded Gabbay 	gaudi_mmu_prepare_reg(hdev, mmMME3_SBAB_ARUSER1, asid);
6293e65e175bSOded Gabbay 	gaudi_mmu_prepare_reg(hdev, mmMME0_ACC_WBC, asid);
6294e65e175bSOded Gabbay 	gaudi_mmu_prepare_reg(hdev, mmMME1_ACC_WBC, asid);
6295e65e175bSOded Gabbay 	gaudi_mmu_prepare_reg(hdev, mmMME2_ACC_WBC, asid);
6296e65e175bSOded Gabbay 	gaudi_mmu_prepare_reg(hdev, mmMME3_ACC_WBC, asid);
6297e65e175bSOded Gabbay 
6298e65e175bSOded Gabbay 	if (gaudi->hw_cap_initialized & HW_CAP_NIC0) {
6299e65e175bSOded Gabbay 		gaudi_mmu_prepare_reg(hdev, mmNIC0_QM0_GLBL_NON_SECURE_PROPS_0,
6300e65e175bSOded Gabbay 				asid);
6301e65e175bSOded Gabbay 		gaudi_mmu_prepare_reg(hdev, mmNIC0_QM0_GLBL_NON_SECURE_PROPS_1,
6302e65e175bSOded Gabbay 				asid);
6303e65e175bSOded Gabbay 		gaudi_mmu_prepare_reg(hdev, mmNIC0_QM0_GLBL_NON_SECURE_PROPS_2,
6304e65e175bSOded Gabbay 				asid);
6305e65e175bSOded Gabbay 		gaudi_mmu_prepare_reg(hdev, mmNIC0_QM0_GLBL_NON_SECURE_PROPS_3,
6306e65e175bSOded Gabbay 				asid);
6307e65e175bSOded Gabbay 		gaudi_mmu_prepare_reg(hdev, mmNIC0_QM0_GLBL_NON_SECURE_PROPS_4,
6308e65e175bSOded Gabbay 				asid);
6309e65e175bSOded Gabbay 	}
6310e65e175bSOded Gabbay 
6311e65e175bSOded Gabbay 	if (gaudi->hw_cap_initialized & HW_CAP_NIC1) {
6312e65e175bSOded Gabbay 		gaudi_mmu_prepare_reg(hdev, mmNIC0_QM1_GLBL_NON_SECURE_PROPS_0,
6313e65e175bSOded Gabbay 				asid);
6314e65e175bSOded Gabbay 		gaudi_mmu_prepare_reg(hdev, mmNIC0_QM1_GLBL_NON_SECURE_PROPS_1,
6315e65e175bSOded Gabbay 				asid);
6316e65e175bSOded Gabbay 		gaudi_mmu_prepare_reg(hdev, mmNIC0_QM1_GLBL_NON_SECURE_PROPS_2,
6317e65e175bSOded Gabbay 				asid);
6318e65e175bSOded Gabbay 		gaudi_mmu_prepare_reg(hdev, mmNIC0_QM1_GLBL_NON_SECURE_PROPS_3,
6319e65e175bSOded Gabbay 				asid);
6320e65e175bSOded Gabbay 		gaudi_mmu_prepare_reg(hdev, mmNIC0_QM1_GLBL_NON_SECURE_PROPS_4,
6321e65e175bSOded Gabbay 				asid);
6322e65e175bSOded Gabbay 	}
6323e65e175bSOded Gabbay 
6324e65e175bSOded Gabbay 	if (gaudi->hw_cap_initialized & HW_CAP_NIC2) {
6325e65e175bSOded Gabbay 		gaudi_mmu_prepare_reg(hdev, mmNIC1_QM0_GLBL_NON_SECURE_PROPS_0,
6326e65e175bSOded Gabbay 				asid);
6327e65e175bSOded Gabbay 		gaudi_mmu_prepare_reg(hdev, mmNIC1_QM0_GLBL_NON_SECURE_PROPS_1,
6328e65e175bSOded Gabbay 				asid);
6329e65e175bSOded Gabbay 		gaudi_mmu_prepare_reg(hdev, mmNIC1_QM0_GLBL_NON_SECURE_PROPS_2,
6330e65e175bSOded Gabbay 				asid);
6331e65e175bSOded Gabbay 		gaudi_mmu_prepare_reg(hdev, mmNIC1_QM0_GLBL_NON_SECURE_PROPS_3,
6332e65e175bSOded Gabbay 				asid);
6333e65e175bSOded Gabbay 		gaudi_mmu_prepare_reg(hdev, mmNIC1_QM0_GLBL_NON_SECURE_PROPS_4,
6334e65e175bSOded Gabbay 				asid);
6335e65e175bSOded Gabbay 	}
6336e65e175bSOded Gabbay 
6337e65e175bSOded Gabbay 	if (gaudi->hw_cap_initialized & HW_CAP_NIC3) {
6338e65e175bSOded Gabbay 		gaudi_mmu_prepare_reg(hdev, mmNIC1_QM1_GLBL_NON_SECURE_PROPS_0,
6339e65e175bSOded Gabbay 				asid);
6340e65e175bSOded Gabbay 		gaudi_mmu_prepare_reg(hdev, mmNIC1_QM1_GLBL_NON_SECURE_PROPS_1,
6341e65e175bSOded Gabbay 				asid);
6342e65e175bSOded Gabbay 		gaudi_mmu_prepare_reg(hdev, mmNIC1_QM1_GLBL_NON_SECURE_PROPS_2,
6343e65e175bSOded Gabbay 				asid);
6344e65e175bSOded Gabbay 		gaudi_mmu_prepare_reg(hdev, mmNIC1_QM1_GLBL_NON_SECURE_PROPS_3,
6345e65e175bSOded Gabbay 				asid);
6346e65e175bSOded Gabbay 		gaudi_mmu_prepare_reg(hdev, mmNIC1_QM1_GLBL_NON_SECURE_PROPS_4,
6347e65e175bSOded Gabbay 				asid);
6348e65e175bSOded Gabbay 	}
6349e65e175bSOded Gabbay 
6350e65e175bSOded Gabbay 	if (gaudi->hw_cap_initialized & HW_CAP_NIC4) {
6351e65e175bSOded Gabbay 		gaudi_mmu_prepare_reg(hdev, mmNIC2_QM0_GLBL_NON_SECURE_PROPS_0,
6352e65e175bSOded Gabbay 				asid);
6353e65e175bSOded Gabbay 		gaudi_mmu_prepare_reg(hdev, mmNIC2_QM0_GLBL_NON_SECURE_PROPS_1,
6354e65e175bSOded Gabbay 				asid);
6355e65e175bSOded Gabbay 		gaudi_mmu_prepare_reg(hdev, mmNIC2_QM0_GLBL_NON_SECURE_PROPS_2,
6356e65e175bSOded Gabbay 				asid);
6357e65e175bSOded Gabbay 		gaudi_mmu_prepare_reg(hdev, mmNIC2_QM0_GLBL_NON_SECURE_PROPS_3,
6358e65e175bSOded Gabbay 				asid);
6359e65e175bSOded Gabbay 		gaudi_mmu_prepare_reg(hdev, mmNIC2_QM0_GLBL_NON_SECURE_PROPS_4,
6360e65e175bSOded Gabbay 				asid);
6361e65e175bSOded Gabbay 	}
6362e65e175bSOded Gabbay 
6363e65e175bSOded Gabbay 	if (gaudi->hw_cap_initialized & HW_CAP_NIC5) {
6364e65e175bSOded Gabbay 		gaudi_mmu_prepare_reg(hdev, mmNIC2_QM1_GLBL_NON_SECURE_PROPS_0,
6365e65e175bSOded Gabbay 				asid);
6366e65e175bSOded Gabbay 		gaudi_mmu_prepare_reg(hdev, mmNIC2_QM1_GLBL_NON_SECURE_PROPS_1,
6367e65e175bSOded Gabbay 				asid);
6368e65e175bSOded Gabbay 		gaudi_mmu_prepare_reg(hdev, mmNIC2_QM1_GLBL_NON_SECURE_PROPS_2,
6369e65e175bSOded Gabbay 				asid);
6370e65e175bSOded Gabbay 		gaudi_mmu_prepare_reg(hdev, mmNIC2_QM1_GLBL_NON_SECURE_PROPS_3,
6371e65e175bSOded Gabbay 				asid);
6372e65e175bSOded Gabbay 		gaudi_mmu_prepare_reg(hdev, mmNIC2_QM1_GLBL_NON_SECURE_PROPS_4,
6373e65e175bSOded Gabbay 				asid);
6374e65e175bSOded Gabbay 	}
6375e65e175bSOded Gabbay 
6376e65e175bSOded Gabbay 	if (gaudi->hw_cap_initialized & HW_CAP_NIC6) {
6377e65e175bSOded Gabbay 		gaudi_mmu_prepare_reg(hdev, mmNIC3_QM0_GLBL_NON_SECURE_PROPS_0,
6378e65e175bSOded Gabbay 				asid);
6379e65e175bSOded Gabbay 		gaudi_mmu_prepare_reg(hdev, mmNIC3_QM0_GLBL_NON_SECURE_PROPS_1,
6380e65e175bSOded Gabbay 				asid);
6381e65e175bSOded Gabbay 		gaudi_mmu_prepare_reg(hdev, mmNIC3_QM0_GLBL_NON_SECURE_PROPS_2,
6382e65e175bSOded Gabbay 				asid);
6383e65e175bSOded Gabbay 		gaudi_mmu_prepare_reg(hdev, mmNIC3_QM0_GLBL_NON_SECURE_PROPS_3,
6384e65e175bSOded Gabbay 				asid);
6385e65e175bSOded Gabbay 		gaudi_mmu_prepare_reg(hdev, mmNIC3_QM0_GLBL_NON_SECURE_PROPS_4,
6386e65e175bSOded Gabbay 				asid);
6387e65e175bSOded Gabbay 	}
6388e65e175bSOded Gabbay 
6389e65e175bSOded Gabbay 	if (gaudi->hw_cap_initialized & HW_CAP_NIC7) {
6390e65e175bSOded Gabbay 		gaudi_mmu_prepare_reg(hdev, mmNIC3_QM1_GLBL_NON_SECURE_PROPS_0,
6391e65e175bSOded Gabbay 				asid);
6392e65e175bSOded Gabbay 		gaudi_mmu_prepare_reg(hdev, mmNIC3_QM1_GLBL_NON_SECURE_PROPS_1,
6393e65e175bSOded Gabbay 				asid);
6394e65e175bSOded Gabbay 		gaudi_mmu_prepare_reg(hdev, mmNIC3_QM1_GLBL_NON_SECURE_PROPS_2,
6395e65e175bSOded Gabbay 				asid);
6396e65e175bSOded Gabbay 		gaudi_mmu_prepare_reg(hdev, mmNIC3_QM1_GLBL_NON_SECURE_PROPS_3,
6397e65e175bSOded Gabbay 				asid);
6398e65e175bSOded Gabbay 		gaudi_mmu_prepare_reg(hdev, mmNIC3_QM1_GLBL_NON_SECURE_PROPS_4,
6399e65e175bSOded Gabbay 				asid);
6400e65e175bSOded Gabbay 	}
6401e65e175bSOded Gabbay 
6402e65e175bSOded Gabbay 	if (gaudi->hw_cap_initialized & HW_CAP_NIC8) {
6403e65e175bSOded Gabbay 		gaudi_mmu_prepare_reg(hdev, mmNIC4_QM0_GLBL_NON_SECURE_PROPS_0,
6404e65e175bSOded Gabbay 				asid);
6405e65e175bSOded Gabbay 		gaudi_mmu_prepare_reg(hdev, mmNIC4_QM0_GLBL_NON_SECURE_PROPS_1,
6406e65e175bSOded Gabbay 				asid);
6407e65e175bSOded Gabbay 		gaudi_mmu_prepare_reg(hdev, mmNIC4_QM0_GLBL_NON_SECURE_PROPS_2,
6408e65e175bSOded Gabbay 				asid);
6409e65e175bSOded Gabbay 		gaudi_mmu_prepare_reg(hdev, mmNIC4_QM0_GLBL_NON_SECURE_PROPS_3,
6410e65e175bSOded Gabbay 				asid);
6411e65e175bSOded Gabbay 		gaudi_mmu_prepare_reg(hdev, mmNIC4_QM0_GLBL_NON_SECURE_PROPS_4,
6412e65e175bSOded Gabbay 				asid);
6413e65e175bSOded Gabbay 	}
6414e65e175bSOded Gabbay 
6415e65e175bSOded Gabbay 	if (gaudi->hw_cap_initialized & HW_CAP_NIC9) {
6416e65e175bSOded Gabbay 		gaudi_mmu_prepare_reg(hdev, mmNIC4_QM1_GLBL_NON_SECURE_PROPS_0,
6417e65e175bSOded Gabbay 				asid);
6418e65e175bSOded Gabbay 		gaudi_mmu_prepare_reg(hdev, mmNIC4_QM1_GLBL_NON_SECURE_PROPS_1,
6419e65e175bSOded Gabbay 				asid);
6420e65e175bSOded Gabbay 		gaudi_mmu_prepare_reg(hdev, mmNIC4_QM1_GLBL_NON_SECURE_PROPS_2,
6421e65e175bSOded Gabbay 				asid);
6422e65e175bSOded Gabbay 		gaudi_mmu_prepare_reg(hdev, mmNIC4_QM1_GLBL_NON_SECURE_PROPS_3,
6423e65e175bSOded Gabbay 				asid);
6424e65e175bSOded Gabbay 		gaudi_mmu_prepare_reg(hdev, mmNIC4_QM1_GLBL_NON_SECURE_PROPS_4,
6425e65e175bSOded Gabbay 				asid);
6426e65e175bSOded Gabbay 	}
6427e65e175bSOded Gabbay 
6428e65e175bSOded Gabbay 	gaudi_mmu_prepare_reg(hdev, mmPSOC_GLOBAL_CONF_TRACE_ARUSER, asid);
6429e65e175bSOded Gabbay 	gaudi_mmu_prepare_reg(hdev, mmPSOC_GLOBAL_CONF_TRACE_AWUSER, asid);
6430e65e175bSOded Gabbay }
6431e65e175bSOded Gabbay 
6432e65e175bSOded Gabbay static int gaudi_send_job_on_qman0(struct hl_device *hdev,
6433e65e175bSOded Gabbay 		struct hl_cs_job *job)
6434e65e175bSOded Gabbay {
6435e65e175bSOded Gabbay 	struct packet_msg_prot *fence_pkt;
6436e65e175bSOded Gabbay 	u32 *fence_ptr;
6437e65e175bSOded Gabbay 	dma_addr_t fence_dma_addr;
6438e65e175bSOded Gabbay 	struct hl_cb *cb;
6439e65e175bSOded Gabbay 	u32 tmp, timeout, dma_offset;
6440e65e175bSOded Gabbay 	int rc;
6441e65e175bSOded Gabbay 
6442e65e175bSOded Gabbay 	if (hdev->pldm)
6443e65e175bSOded Gabbay 		timeout = GAUDI_PLDM_QMAN0_TIMEOUT_USEC;
6444e65e175bSOded Gabbay 	else
6445e65e175bSOded Gabbay 		timeout = HL_DEVICE_TIMEOUT_USEC;
6446e65e175bSOded Gabbay 
6447e65e175bSOded Gabbay 	fence_ptr = hl_asic_dma_pool_zalloc(hdev, 4, GFP_KERNEL, &fence_dma_addr);
6448e65e175bSOded Gabbay 	if (!fence_ptr) {
6449e65e175bSOded Gabbay 		dev_err(hdev->dev,
6450e65e175bSOded Gabbay 			"Failed to allocate fence memory for QMAN0\n");
6451e65e175bSOded Gabbay 		return -ENOMEM;
6452e65e175bSOded Gabbay 	}
6453e65e175bSOded Gabbay 
6454e65e175bSOded Gabbay 	cb = job->patched_cb;
6455e65e175bSOded Gabbay 
6456e65e175bSOded Gabbay 	fence_pkt = cb->kernel_address +
6457e65e175bSOded Gabbay 			job->job_cb_size - sizeof(struct packet_msg_prot);
6458e65e175bSOded Gabbay 
6459e65e175bSOded Gabbay 	tmp = FIELD_PREP(GAUDI_PKT_CTL_OPCODE_MASK, PACKET_MSG_PROT);
6460e65e175bSOded Gabbay 	tmp |= FIELD_PREP(GAUDI_PKT_CTL_EB_MASK, 1);
6461e65e175bSOded Gabbay 	tmp |= FIELD_PREP(GAUDI_PKT_CTL_MB_MASK, 1);
6462e65e175bSOded Gabbay 
6463e65e175bSOded Gabbay 	fence_pkt->ctl = cpu_to_le32(tmp);
6464e65e175bSOded Gabbay 	fence_pkt->value = cpu_to_le32(GAUDI_QMAN0_FENCE_VAL);
6465e65e175bSOded Gabbay 	fence_pkt->addr = cpu_to_le64(fence_dma_addr);
6466e65e175bSOded Gabbay 
6467e65e175bSOded Gabbay 	dma_offset = gaudi_dma_assignment[GAUDI_PCI_DMA_1] * DMA_CORE_OFFSET;
6468e65e175bSOded Gabbay 
6469e65e175bSOded Gabbay 	WREG32(mmDMA0_CORE_PROT + dma_offset,
6470e65e175bSOded Gabbay 			BIT(DMA0_CORE_PROT_ERR_VAL_SHIFT) | BIT(DMA0_CORE_PROT_VAL_SHIFT));
6471e65e175bSOded Gabbay 
6472e65e175bSOded Gabbay 	rc = hl_hw_queue_send_cb_no_cmpl(hdev, GAUDI_QUEUE_ID_DMA_0_0,
6473e65e175bSOded Gabbay 					job->job_cb_size, cb->bus_address);
6474e65e175bSOded Gabbay 	if (rc) {
6475e65e175bSOded Gabbay 		dev_err(hdev->dev, "Failed to send CB on QMAN0, %d\n", rc);
6476e65e175bSOded Gabbay 		goto free_fence_ptr;
6477e65e175bSOded Gabbay 	}
6478e65e175bSOded Gabbay 
6479e65e175bSOded Gabbay 	rc = hl_poll_timeout_memory(hdev, fence_ptr, tmp,
6480e65e175bSOded Gabbay 				(tmp == GAUDI_QMAN0_FENCE_VAL), 1000,
6481e65e175bSOded Gabbay 				timeout, true);
6482e65e175bSOded Gabbay 
6483e65e175bSOded Gabbay 	hl_hw_queue_inc_ci_kernel(hdev, GAUDI_QUEUE_ID_DMA_0_0);
6484e65e175bSOded Gabbay 
6485e65e175bSOded Gabbay 	if (rc == -ETIMEDOUT) {
6486e65e175bSOded Gabbay 		dev_err(hdev->dev, "QMAN0 Job timeout (0x%x)\n", tmp);
6487e65e175bSOded Gabbay 		goto free_fence_ptr;
6488e65e175bSOded Gabbay 	}
6489e65e175bSOded Gabbay 
6490e65e175bSOded Gabbay free_fence_ptr:
6491e65e175bSOded Gabbay 	WREG32(mmDMA0_CORE_PROT + dma_offset, BIT(DMA0_CORE_PROT_ERR_VAL_SHIFT));
6492e65e175bSOded Gabbay 
6493e65e175bSOded Gabbay 	hl_asic_dma_pool_free(hdev, (void *) fence_ptr, fence_dma_addr);
6494e65e175bSOded Gabbay 	return rc;
6495e65e175bSOded Gabbay }
6496e65e175bSOded Gabbay 
6497e65e175bSOded Gabbay static void gaudi_get_event_desc(u16 event_type, char *desc, size_t size)
6498e65e175bSOded Gabbay {
6499e65e175bSOded Gabbay 	if (event_type >= GAUDI_EVENT_SIZE)
6500e65e175bSOded Gabbay 		goto event_not_supported;
6501e65e175bSOded Gabbay 
6502e65e175bSOded Gabbay 	if (!gaudi_irq_map_table[event_type].valid)
6503e65e175bSOded Gabbay 		goto event_not_supported;
6504e65e175bSOded Gabbay 
6505e65e175bSOded Gabbay 	snprintf(desc, size, gaudi_irq_map_table[event_type].name);
6506e65e175bSOded Gabbay 
6507e65e175bSOded Gabbay 	return;
6508e65e175bSOded Gabbay 
6509e65e175bSOded Gabbay event_not_supported:
6510e65e175bSOded Gabbay 	snprintf(desc, size, "N/A");
6511e65e175bSOded Gabbay }
6512e65e175bSOded Gabbay 
6513e65e175bSOded Gabbay static const char *gaudi_get_razwi_initiator_dma_name(struct hl_device *hdev, u32 x_y,
6514e65e175bSOded Gabbay 							bool is_write, u16 *engine_id_1,
6515e65e175bSOded Gabbay 							u16 *engine_id_2)
6516e65e175bSOded Gabbay {
6517e65e175bSOded Gabbay 	u32 dma_id[2], dma_offset, err_cause[2], mask, i;
6518e65e175bSOded Gabbay 
6519e65e175bSOded Gabbay 	mask = is_write ? DMA0_CORE_ERR_CAUSE_HBW_WR_ERR_MASK :
6520e65e175bSOded Gabbay 				DMA0_CORE_ERR_CAUSE_HBW_RD_ERR_MASK;
6521e65e175bSOded Gabbay 
6522e65e175bSOded Gabbay 	switch (x_y) {
6523e65e175bSOded Gabbay 	case RAZWI_INITIATOR_ID_X_Y_DMA_IF_W_S_0:
6524e65e175bSOded Gabbay 	case RAZWI_INITIATOR_ID_X_Y_DMA_IF_W_S_1:
6525e65e175bSOded Gabbay 		dma_id[0] = 0;
6526e65e175bSOded Gabbay 		dma_id[1] = 2;
6527e65e175bSOded Gabbay 		break;
6528e65e175bSOded Gabbay 	case RAZWI_INITIATOR_ID_X_Y_DMA_IF_E_S_0:
6529e65e175bSOded Gabbay 	case RAZWI_INITIATOR_ID_X_Y_DMA_IF_E_S_1:
6530e65e175bSOded Gabbay 		dma_id[0] = 1;
6531e65e175bSOded Gabbay 		dma_id[1] = 3;
6532e65e175bSOded Gabbay 		break;
6533e65e175bSOded Gabbay 	case RAZWI_INITIATOR_ID_X_Y_DMA_IF_W_N_0:
6534e65e175bSOded Gabbay 	case RAZWI_INITIATOR_ID_X_Y_DMA_IF_W_N_1:
6535e65e175bSOded Gabbay 		dma_id[0] = 4;
6536e65e175bSOded Gabbay 		dma_id[1] = 6;
6537e65e175bSOded Gabbay 		break;
6538e65e175bSOded Gabbay 	case RAZWI_INITIATOR_ID_X_Y_DMA_IF_E_N_0:
6539e65e175bSOded Gabbay 	case RAZWI_INITIATOR_ID_X_Y_DMA_IF_E_N_1:
6540e65e175bSOded Gabbay 		dma_id[0] = 5;
6541e65e175bSOded Gabbay 		dma_id[1] = 7;
6542e65e175bSOded Gabbay 		break;
6543e65e175bSOded Gabbay 	default:
6544e65e175bSOded Gabbay 		goto unknown_initiator;
6545e65e175bSOded Gabbay 	}
6546e65e175bSOded Gabbay 
6547e65e175bSOded Gabbay 	for (i = 0 ; i < 2 ; i++) {
6548e65e175bSOded Gabbay 		dma_offset = dma_id[i] * DMA_CORE_OFFSET;
6549e65e175bSOded Gabbay 		err_cause[i] = RREG32(mmDMA0_CORE_ERR_CAUSE + dma_offset);
6550e65e175bSOded Gabbay 	}
6551e65e175bSOded Gabbay 
6552e65e175bSOded Gabbay 	switch (x_y) {
6553e65e175bSOded Gabbay 	case RAZWI_INITIATOR_ID_X_Y_DMA_IF_W_S_0:
6554e65e175bSOded Gabbay 	case RAZWI_INITIATOR_ID_X_Y_DMA_IF_W_S_1:
6555e65e175bSOded Gabbay 		if ((err_cause[0] & mask) && !(err_cause[1] & mask)) {
6556e65e175bSOded Gabbay 			*engine_id_1 = GAUDI_ENGINE_ID_DMA_0;
6557e65e175bSOded Gabbay 			return "DMA0";
6558e65e175bSOded Gabbay 		} else if (!(err_cause[0] & mask) && (err_cause[1] & mask)) {
6559e65e175bSOded Gabbay 			*engine_id_1 = GAUDI_ENGINE_ID_DMA_2;
6560e65e175bSOded Gabbay 			return "DMA2";
6561e65e175bSOded Gabbay 		} else {
6562e65e175bSOded Gabbay 			*engine_id_1 = GAUDI_ENGINE_ID_DMA_0;
6563e65e175bSOded Gabbay 			*engine_id_2 = GAUDI_ENGINE_ID_DMA_2;
6564e65e175bSOded Gabbay 			return "DMA0 or DMA2";
6565e65e175bSOded Gabbay 		}
6566e65e175bSOded Gabbay 	case RAZWI_INITIATOR_ID_X_Y_DMA_IF_E_S_0:
6567e65e175bSOded Gabbay 	case RAZWI_INITIATOR_ID_X_Y_DMA_IF_E_S_1:
6568e65e175bSOded Gabbay 		if ((err_cause[0] & mask) && !(err_cause[1] & mask)) {
6569e65e175bSOded Gabbay 			*engine_id_1 = GAUDI_ENGINE_ID_DMA_1;
6570e65e175bSOded Gabbay 			return "DMA1";
6571e65e175bSOded Gabbay 		} else if (!(err_cause[0] & mask) && (err_cause[1] & mask)) {
6572e65e175bSOded Gabbay 			*engine_id_1 = GAUDI_ENGINE_ID_DMA_3;
6573e65e175bSOded Gabbay 			return "DMA3";
6574e65e175bSOded Gabbay 		} else {
6575e65e175bSOded Gabbay 			*engine_id_1 = GAUDI_ENGINE_ID_DMA_1;
6576e65e175bSOded Gabbay 			*engine_id_2 = GAUDI_ENGINE_ID_DMA_3;
6577e65e175bSOded Gabbay 			return "DMA1 or DMA3";
6578e65e175bSOded Gabbay 		}
6579e65e175bSOded Gabbay 	case RAZWI_INITIATOR_ID_X_Y_DMA_IF_W_N_0:
6580e65e175bSOded Gabbay 	case RAZWI_INITIATOR_ID_X_Y_DMA_IF_W_N_1:
6581e65e175bSOded Gabbay 		if ((err_cause[0] & mask) && !(err_cause[1] & mask)) {
6582e65e175bSOded Gabbay 			*engine_id_1 = GAUDI_ENGINE_ID_DMA_4;
6583e65e175bSOded Gabbay 			return "DMA4";
6584e65e175bSOded Gabbay 		} else if (!(err_cause[0] & mask) && (err_cause[1] & mask)) {
6585e65e175bSOded Gabbay 			*engine_id_1 = GAUDI_ENGINE_ID_DMA_6;
6586e65e175bSOded Gabbay 			return "DMA6";
6587e65e175bSOded Gabbay 		} else {
6588e65e175bSOded Gabbay 			*engine_id_1 = GAUDI_ENGINE_ID_DMA_4;
6589e65e175bSOded Gabbay 			*engine_id_2 = GAUDI_ENGINE_ID_DMA_6;
6590e65e175bSOded Gabbay 			return "DMA4 or DMA6";
6591e65e175bSOded Gabbay 		}
6592e65e175bSOded Gabbay 	case RAZWI_INITIATOR_ID_X_Y_DMA_IF_E_N_0:
6593e65e175bSOded Gabbay 	case RAZWI_INITIATOR_ID_X_Y_DMA_IF_E_N_1:
6594e65e175bSOded Gabbay 		if ((err_cause[0] & mask) && !(err_cause[1] & mask)) {
6595e65e175bSOded Gabbay 			*engine_id_1 = GAUDI_ENGINE_ID_DMA_5;
6596e65e175bSOded Gabbay 			return "DMA5";
6597e65e175bSOded Gabbay 		} else if (!(err_cause[0] & mask) && (err_cause[1] & mask)) {
6598e65e175bSOded Gabbay 			*engine_id_1 = GAUDI_ENGINE_ID_DMA_7;
6599e65e175bSOded Gabbay 			return "DMA7";
6600e65e175bSOded Gabbay 		} else {
6601e65e175bSOded Gabbay 			*engine_id_1 = GAUDI_ENGINE_ID_DMA_5;
6602e65e175bSOded Gabbay 			*engine_id_2 = GAUDI_ENGINE_ID_DMA_7;
6603e65e175bSOded Gabbay 			return "DMA5 or DMA7";
6604e65e175bSOded Gabbay 		}
6605e65e175bSOded Gabbay 	}
6606e65e175bSOded Gabbay 
6607e65e175bSOded Gabbay unknown_initiator:
6608e65e175bSOded Gabbay 	return "unknown initiator";
6609e65e175bSOded Gabbay }
6610e65e175bSOded Gabbay 
6611e65e175bSOded Gabbay static const char *gaudi_get_razwi_initiator_name(struct hl_device *hdev, bool is_write,
6612e65e175bSOded Gabbay 							u16 *engine_id_1, u16 *engine_id_2)
6613e65e175bSOded Gabbay {
6614e65e175bSOded Gabbay 	u32 val, x_y, axi_id;
6615e65e175bSOded Gabbay 
6616e65e175bSOded Gabbay 	val = is_write ? RREG32(mmMMU_UP_RAZWI_WRITE_ID) :
6617e65e175bSOded Gabbay 				RREG32(mmMMU_UP_RAZWI_READ_ID);
6618e65e175bSOded Gabbay 	x_y = val & ((RAZWI_INITIATOR_Y_MASK << RAZWI_INITIATOR_Y_SHIFT) |
6619e65e175bSOded Gabbay 			(RAZWI_INITIATOR_X_MASK << RAZWI_INITIATOR_X_SHIFT));
6620e65e175bSOded Gabbay 	axi_id = val & (RAZWI_INITIATOR_AXI_ID_MASK <<
6621e65e175bSOded Gabbay 			RAZWI_INITIATOR_AXI_ID_SHIFT);
6622e65e175bSOded Gabbay 
6623e65e175bSOded Gabbay 	switch (x_y) {
6624e65e175bSOded Gabbay 	case RAZWI_INITIATOR_ID_X_Y_TPC0_NIC0:
6625e65e175bSOded Gabbay 		if (axi_id == RAZWI_INITIATOR_ID_AXI_ID(AXI_ID_TPC)) {
6626e65e175bSOded Gabbay 			*engine_id_1 = GAUDI_ENGINE_ID_TPC_0;
6627e65e175bSOded Gabbay 			return "TPC0";
6628e65e175bSOded Gabbay 		}
6629e65e175bSOded Gabbay 		if (axi_id == RAZWI_INITIATOR_ID_AXI_ID(AXI_ID_NIC)) {
6630e65e175bSOded Gabbay 			*engine_id_1 = GAUDI_ENGINE_ID_NIC_0;
6631e65e175bSOded Gabbay 			return "NIC0";
6632e65e175bSOded Gabbay 		}
6633e65e175bSOded Gabbay 		break;
6634e65e175bSOded Gabbay 	case RAZWI_INITIATOR_ID_X_Y_TPC1:
6635e65e175bSOded Gabbay 		*engine_id_1 = GAUDI_ENGINE_ID_TPC_1;
6636e65e175bSOded Gabbay 		return "TPC1";
6637e65e175bSOded Gabbay 	case RAZWI_INITIATOR_ID_X_Y_MME0_0:
6638e65e175bSOded Gabbay 	case RAZWI_INITIATOR_ID_X_Y_MME0_1:
6639e65e175bSOded Gabbay 		*engine_id_1 = GAUDI_ENGINE_ID_MME_0;
6640e65e175bSOded Gabbay 		return "MME0";
6641e65e175bSOded Gabbay 	case RAZWI_INITIATOR_ID_X_Y_MME1_0:
6642e65e175bSOded Gabbay 	case RAZWI_INITIATOR_ID_X_Y_MME1_1:
6643e65e175bSOded Gabbay 		*engine_id_1 = GAUDI_ENGINE_ID_MME_1;
6644e65e175bSOded Gabbay 		return "MME1";
6645e65e175bSOded Gabbay 	case RAZWI_INITIATOR_ID_X_Y_TPC2:
6646e65e175bSOded Gabbay 		*engine_id_1 = GAUDI_ENGINE_ID_TPC_2;
6647e65e175bSOded Gabbay 		return "TPC2";
6648e65e175bSOded Gabbay 	case RAZWI_INITIATOR_ID_X_Y_TPC3_PCI_CPU_PSOC:
6649e65e175bSOded Gabbay 		if (axi_id == RAZWI_INITIATOR_ID_AXI_ID(AXI_ID_TPC)) {
6650e65e175bSOded Gabbay 			*engine_id_1 = GAUDI_ENGINE_ID_TPC_3;
6651e65e175bSOded Gabbay 			return "TPC3";
6652e65e175bSOded Gabbay 		}
6653e65e175bSOded Gabbay 		/* PCI, CPU or PSOC does not have engine id*/
6654e65e175bSOded Gabbay 		if (axi_id == RAZWI_INITIATOR_ID_AXI_ID(AXI_ID_PCI))
6655e65e175bSOded Gabbay 			return "PCI";
6656e65e175bSOded Gabbay 		if (axi_id == RAZWI_INITIATOR_ID_AXI_ID(AXI_ID_CPU))
6657e65e175bSOded Gabbay 			return "CPU";
6658e65e175bSOded Gabbay 		if (axi_id == RAZWI_INITIATOR_ID_AXI_ID(AXI_ID_PSOC))
6659e65e175bSOded Gabbay 			return "PSOC";
6660e65e175bSOded Gabbay 		break;
6661e65e175bSOded Gabbay 	case RAZWI_INITIATOR_ID_X_Y_DMA_IF_W_S_0:
6662e65e175bSOded Gabbay 	case RAZWI_INITIATOR_ID_X_Y_DMA_IF_W_S_1:
6663e65e175bSOded Gabbay 	case RAZWI_INITIATOR_ID_X_Y_DMA_IF_E_S_0:
6664e65e175bSOded Gabbay 	case RAZWI_INITIATOR_ID_X_Y_DMA_IF_E_S_1:
6665e65e175bSOded Gabbay 	case RAZWI_INITIATOR_ID_X_Y_DMA_IF_W_N_0:
6666e65e175bSOded Gabbay 	case RAZWI_INITIATOR_ID_X_Y_DMA_IF_W_N_1:
6667e65e175bSOded Gabbay 	case RAZWI_INITIATOR_ID_X_Y_DMA_IF_E_N_0:
6668e65e175bSOded Gabbay 	case RAZWI_INITIATOR_ID_X_Y_DMA_IF_E_N_1:
6669e65e175bSOded Gabbay 		return gaudi_get_razwi_initiator_dma_name(hdev, x_y, is_write,
6670e65e175bSOded Gabbay 				engine_id_1, engine_id_2);
6671e65e175bSOded Gabbay 	case RAZWI_INITIATOR_ID_X_Y_TPC4_NIC1_NIC2:
6672e65e175bSOded Gabbay 		if (axi_id == RAZWI_INITIATOR_ID_AXI_ID(AXI_ID_TPC)) {
6673e65e175bSOded Gabbay 			*engine_id_1 = GAUDI_ENGINE_ID_TPC_4;
6674e65e175bSOded Gabbay 			return "TPC4";
6675e65e175bSOded Gabbay 		}
6676e65e175bSOded Gabbay 		if (axi_id == RAZWI_INITIATOR_ID_AXI_ID(AXI_ID_NIC)) {
6677e65e175bSOded Gabbay 			*engine_id_1 = GAUDI_ENGINE_ID_NIC_1;
6678e65e175bSOded Gabbay 			return "NIC1";
6679e65e175bSOded Gabbay 		}
6680e65e175bSOded Gabbay 		if (axi_id == RAZWI_INITIATOR_ID_AXI_ID(AXI_ID_NIC_FT)) {
6681e65e175bSOded Gabbay 			*engine_id_1 = GAUDI_ENGINE_ID_NIC_2;
6682e65e175bSOded Gabbay 			return "NIC2";
6683e65e175bSOded Gabbay 		}
6684e65e175bSOded Gabbay 		break;
6685e65e175bSOded Gabbay 	case RAZWI_INITIATOR_ID_X_Y_TPC5:
6686e65e175bSOded Gabbay 		*engine_id_1 = GAUDI_ENGINE_ID_TPC_5;
6687e65e175bSOded Gabbay 		return "TPC5";
6688e65e175bSOded Gabbay 	case RAZWI_INITIATOR_ID_X_Y_MME2_0:
6689e65e175bSOded Gabbay 	case RAZWI_INITIATOR_ID_X_Y_MME2_1:
6690e65e175bSOded Gabbay 		*engine_id_1 = GAUDI_ENGINE_ID_MME_2;
6691e65e175bSOded Gabbay 		return "MME2";
6692e65e175bSOded Gabbay 	case RAZWI_INITIATOR_ID_X_Y_MME3_0:
6693e65e175bSOded Gabbay 	case RAZWI_INITIATOR_ID_X_Y_MME3_1:
6694e65e175bSOded Gabbay 		*engine_id_1 = GAUDI_ENGINE_ID_MME_3;
6695e65e175bSOded Gabbay 		return "MME3";
6696e65e175bSOded Gabbay 	case RAZWI_INITIATOR_ID_X_Y_TPC6:
6697e65e175bSOded Gabbay 		*engine_id_1 = GAUDI_ENGINE_ID_TPC_6;
6698e65e175bSOded Gabbay 		return "TPC6";
6699e65e175bSOded Gabbay 	case RAZWI_INITIATOR_ID_X_Y_TPC7_NIC4_NIC5:
6700e65e175bSOded Gabbay 		if (axi_id == RAZWI_INITIATOR_ID_AXI_ID(AXI_ID_TPC)) {
6701e65e175bSOded Gabbay 			*engine_id_1 = GAUDI_ENGINE_ID_TPC_7;
6702e65e175bSOded Gabbay 			return "TPC7";
6703e65e175bSOded Gabbay 		}
6704e65e175bSOded Gabbay 		if (axi_id == RAZWI_INITIATOR_ID_AXI_ID(AXI_ID_NIC)) {
6705e65e175bSOded Gabbay 			*engine_id_1 = GAUDI_ENGINE_ID_NIC_4;
6706e65e175bSOded Gabbay 			return "NIC4";
6707e65e175bSOded Gabbay 		}
6708e65e175bSOded Gabbay 		if (axi_id == RAZWI_INITIATOR_ID_AXI_ID(AXI_ID_NIC_FT)) {
6709e65e175bSOded Gabbay 			*engine_id_1 = GAUDI_ENGINE_ID_NIC_5;
6710e65e175bSOded Gabbay 			return "NIC5";
6711e65e175bSOded Gabbay 		}
6712e65e175bSOded Gabbay 		break;
6713e65e175bSOded Gabbay 	default:
6714e65e175bSOded Gabbay 		break;
6715e65e175bSOded Gabbay 	}
6716e65e175bSOded Gabbay 
6717e65e175bSOded Gabbay 	dev_err(hdev->dev,
6718e65e175bSOded Gabbay 		"Unknown RAZWI initiator ID 0x%x [Y=%d, X=%d, AXI_ID=%d]\n",
6719e65e175bSOded Gabbay 		val,
6720e65e175bSOded Gabbay 		(val >> RAZWI_INITIATOR_Y_SHIFT) & RAZWI_INITIATOR_Y_MASK,
6721e65e175bSOded Gabbay 		(val >> RAZWI_INITIATOR_X_SHIFT) & RAZWI_INITIATOR_X_MASK,
6722e65e175bSOded Gabbay 		(val >> RAZWI_INITIATOR_AXI_ID_SHIFT) &
6723e65e175bSOded Gabbay 			RAZWI_INITIATOR_AXI_ID_MASK);
6724e65e175bSOded Gabbay 
6725e65e175bSOded Gabbay 	return "unknown initiator";
6726e65e175bSOded Gabbay }
6727e65e175bSOded Gabbay 
6728e65e175bSOded Gabbay static void gaudi_print_and_get_razwi_info(struct hl_device *hdev, u16 *engine_id_1,
6729e65e175bSOded Gabbay 						u16 *engine_id_2, bool *is_read, bool *is_write)
6730e65e175bSOded Gabbay {
6731e65e175bSOded Gabbay 
6732e65e175bSOded Gabbay 	if (RREG32(mmMMU_UP_RAZWI_WRITE_VLD)) {
6733e65e175bSOded Gabbay 		dev_err_ratelimited(hdev->dev,
6734e65e175bSOded Gabbay 			"RAZWI event caused by illegal write of %s\n",
6735e65e175bSOded Gabbay 			gaudi_get_razwi_initiator_name(hdev, true, engine_id_1, engine_id_2));
6736e65e175bSOded Gabbay 		WREG32(mmMMU_UP_RAZWI_WRITE_VLD, 0);
6737e65e175bSOded Gabbay 		*is_write = true;
6738e65e175bSOded Gabbay 	}
6739e65e175bSOded Gabbay 
6740e65e175bSOded Gabbay 	if (RREG32(mmMMU_UP_RAZWI_READ_VLD)) {
6741e65e175bSOded Gabbay 		dev_err_ratelimited(hdev->dev,
6742e65e175bSOded Gabbay 			"RAZWI event caused by illegal read of %s\n",
6743e65e175bSOded Gabbay 			gaudi_get_razwi_initiator_name(hdev, false, engine_id_1, engine_id_2));
6744e65e175bSOded Gabbay 		WREG32(mmMMU_UP_RAZWI_READ_VLD, 0);
6745e65e175bSOded Gabbay 		*is_read = true;
6746e65e175bSOded Gabbay 	}
6747e65e175bSOded Gabbay }
6748e65e175bSOded Gabbay 
6749e65e175bSOded Gabbay static void gaudi_print_and_get_mmu_error_info(struct hl_device *hdev, u64 *addr, u64 *event_mask)
6750e65e175bSOded Gabbay {
6751e65e175bSOded Gabbay 	struct gaudi_device *gaudi = hdev->asic_specific;
6752e65e175bSOded Gabbay 	u32 val;
6753e65e175bSOded Gabbay 
6754e65e175bSOded Gabbay 	if (!(gaudi->hw_cap_initialized & HW_CAP_MMU))
6755e65e175bSOded Gabbay 		return;
6756e65e175bSOded Gabbay 
6757e65e175bSOded Gabbay 	val = RREG32(mmMMU_UP_PAGE_ERROR_CAPTURE);
6758e65e175bSOded Gabbay 	if (val & MMU_UP_PAGE_ERROR_CAPTURE_ENTRY_VALID_MASK) {
6759e65e175bSOded Gabbay 		*addr = val & MMU_UP_PAGE_ERROR_CAPTURE_VA_49_32_MASK;
6760e65e175bSOded Gabbay 		*addr <<= 32;
6761e65e175bSOded Gabbay 		*addr |= RREG32(mmMMU_UP_PAGE_ERROR_CAPTURE_VA);
6762e65e175bSOded Gabbay 
6763e65e175bSOded Gabbay 		dev_err_ratelimited(hdev->dev, "MMU page fault on va 0x%llx\n", *addr);
6764e65e175bSOded Gabbay 		hl_handle_page_fault(hdev, *addr, 0, true, event_mask);
6765e65e175bSOded Gabbay 
6766e65e175bSOded Gabbay 		WREG32(mmMMU_UP_PAGE_ERROR_CAPTURE, 0);
6767e65e175bSOded Gabbay 	}
6768e65e175bSOded Gabbay 
6769e65e175bSOded Gabbay 	val = RREG32(mmMMU_UP_ACCESS_ERROR_CAPTURE);
6770e65e175bSOded Gabbay 	if (val & MMU_UP_ACCESS_ERROR_CAPTURE_ENTRY_VALID_MASK) {
6771e65e175bSOded Gabbay 		*addr = val & MMU_UP_ACCESS_ERROR_CAPTURE_VA_49_32_MASK;
6772e65e175bSOded Gabbay 		*addr <<= 32;
6773e65e175bSOded Gabbay 		*addr |= RREG32(mmMMU_UP_ACCESS_ERROR_CAPTURE_VA);
6774e65e175bSOded Gabbay 
6775e65e175bSOded Gabbay 		dev_err_ratelimited(hdev->dev, "MMU access error on va 0x%llx\n", *addr);
6776e65e175bSOded Gabbay 
6777e65e175bSOded Gabbay 		WREG32(mmMMU_UP_ACCESS_ERROR_CAPTURE, 0);
6778e65e175bSOded Gabbay 	}
6779e65e175bSOded Gabbay }
6780e65e175bSOded Gabbay 
6781e65e175bSOded Gabbay /*
6782e65e175bSOded Gabbay  *  +-------------------+------------------------------------------------------+
6783e65e175bSOded Gabbay  *  | Configuration Reg |                     Description                      |
6784e65e175bSOded Gabbay  *  |      Address      |                                                      |
6785e65e175bSOded Gabbay  *  +-------------------+------------------------------------------------------+
6786e65e175bSOded Gabbay  *  |  0xF30 - 0xF3F    |ECC single error indication (1 bit per memory wrapper)|
6787e65e175bSOded Gabbay  *  |                   |0xF30 memory wrappers 31:0 (MSB to LSB)               |
6788e65e175bSOded Gabbay  *  |                   |0xF34 memory wrappers 63:32                           |
6789e65e175bSOded Gabbay  *  |                   |0xF38 memory wrappers 95:64                           |
6790e65e175bSOded Gabbay  *  |                   |0xF3C memory wrappers 127:96                          |
6791e65e175bSOded Gabbay  *  +-------------------+------------------------------------------------------+
6792e65e175bSOded Gabbay  *  |  0xF40 - 0xF4F    |ECC double error indication (1 bit per memory wrapper)|
6793e65e175bSOded Gabbay  *  |                   |0xF40 memory wrappers 31:0 (MSB to LSB)               |
6794e65e175bSOded Gabbay  *  |                   |0xF44 memory wrappers 63:32                           |
6795e65e175bSOded Gabbay  *  |                   |0xF48 memory wrappers 95:64                           |
6796e65e175bSOded Gabbay  *  |                   |0xF4C memory wrappers 127:96                          |
6797e65e175bSOded Gabbay  *  +-------------------+------------------------------------------------------+
6798e65e175bSOded Gabbay  */
6799e65e175bSOded Gabbay static int gaudi_extract_ecc_info(struct hl_device *hdev,
6800e65e175bSOded Gabbay 		struct ecc_info_extract_params *params, u64 *ecc_address,
6801e65e175bSOded Gabbay 		u64 *ecc_syndrom, u8 *memory_wrapper_idx)
6802e65e175bSOded Gabbay {
6803e65e175bSOded Gabbay 	u32 i, num_mem_regs, reg, err_bit;
6804e65e175bSOded Gabbay 	u64 err_addr, err_word = 0;
6805e65e175bSOded Gabbay 
6806e65e175bSOded Gabbay 	num_mem_regs = params->num_memories / 32 +
6807e65e175bSOded Gabbay 			((params->num_memories % 32) ? 1 : 0);
6808e65e175bSOded Gabbay 
6809e65e175bSOded Gabbay 	if (params->block_address >= CFG_BASE)
6810e65e175bSOded Gabbay 		params->block_address -= CFG_BASE;
6811e65e175bSOded Gabbay 
6812e65e175bSOded Gabbay 	if (params->derr)
6813e65e175bSOded Gabbay 		err_addr = params->block_address + GAUDI_ECC_DERR0_OFFSET;
6814e65e175bSOded Gabbay 	else
6815e65e175bSOded Gabbay 		err_addr = params->block_address + GAUDI_ECC_SERR0_OFFSET;
6816e65e175bSOded Gabbay 
6817e65e175bSOded Gabbay 	/* Set invalid wrapper index */
6818e65e175bSOded Gabbay 	*memory_wrapper_idx = 0xFF;
6819e65e175bSOded Gabbay 
6820e65e175bSOded Gabbay 	/* Iterate through memory wrappers, a single bit must be set */
6821e65e175bSOded Gabbay 	for (i = 0 ; i < num_mem_regs ; i++) {
6822e65e175bSOded Gabbay 		err_addr += i * 4;
6823e65e175bSOded Gabbay 		err_word = RREG32(err_addr);
6824e65e175bSOded Gabbay 		if (err_word) {
6825e65e175bSOded Gabbay 			err_bit = __ffs(err_word);
6826e65e175bSOded Gabbay 			*memory_wrapper_idx = err_bit + (32 * i);
6827e65e175bSOded Gabbay 			break;
6828e65e175bSOded Gabbay 		}
6829e65e175bSOded Gabbay 	}
6830e65e175bSOded Gabbay 
6831e65e175bSOded Gabbay 	if (*memory_wrapper_idx == 0xFF) {
6832e65e175bSOded Gabbay 		dev_err(hdev->dev, "ECC error information cannot be found\n");
6833e65e175bSOded Gabbay 		return -EINVAL;
6834e65e175bSOded Gabbay 	}
6835e65e175bSOded Gabbay 
6836e65e175bSOded Gabbay 	WREG32(params->block_address + GAUDI_ECC_MEM_SEL_OFFSET,
6837e65e175bSOded Gabbay 			*memory_wrapper_idx);
6838e65e175bSOded Gabbay 
6839e65e175bSOded Gabbay 	*ecc_address =
6840e65e175bSOded Gabbay 		RREG32(params->block_address + GAUDI_ECC_ADDRESS_OFFSET);
6841e65e175bSOded Gabbay 	*ecc_syndrom =
6842e65e175bSOded Gabbay 		RREG32(params->block_address + GAUDI_ECC_SYNDROME_OFFSET);
6843e65e175bSOded Gabbay 
6844e65e175bSOded Gabbay 	/* Clear error indication */
6845e65e175bSOded Gabbay 	reg = RREG32(params->block_address + GAUDI_ECC_MEM_INFO_CLR_OFFSET);
6846e65e175bSOded Gabbay 	if (params->derr)
6847e65e175bSOded Gabbay 		reg |= FIELD_PREP(GAUDI_ECC_MEM_INFO_CLR_DERR_MASK, 1);
6848e65e175bSOded Gabbay 	else
6849e65e175bSOded Gabbay 		reg |= FIELD_PREP(GAUDI_ECC_MEM_INFO_CLR_SERR_MASK, 1);
6850e65e175bSOded Gabbay 
6851e65e175bSOded Gabbay 	WREG32(params->block_address + GAUDI_ECC_MEM_INFO_CLR_OFFSET, reg);
6852e65e175bSOded Gabbay 
6853e65e175bSOded Gabbay 	return 0;
6854e65e175bSOded Gabbay }
6855e65e175bSOded Gabbay 
6856e65e175bSOded Gabbay /*
6857e65e175bSOded Gabbay  * gaudi_queue_idx_dec - decrement queue index (pi/ci) and handle wrap
6858e65e175bSOded Gabbay  *
6859e65e175bSOded Gabbay  * @idx: the current pi/ci value
6860e65e175bSOded Gabbay  * @q_len: the queue length (power of 2)
6861e65e175bSOded Gabbay  *
6862e65e175bSOded Gabbay  * @return the cyclically decremented index
6863e65e175bSOded Gabbay  */
6864e65e175bSOded Gabbay static inline u32 gaudi_queue_idx_dec(u32 idx, u32 q_len)
6865e65e175bSOded Gabbay {
6866e65e175bSOded Gabbay 	u32 mask = q_len - 1;
6867e65e175bSOded Gabbay 
6868e65e175bSOded Gabbay 	/*
6869e65e175bSOded Gabbay 	 * modular decrement is equivalent to adding (queue_size -1)
6870e65e175bSOded Gabbay 	 * later we take LSBs to make sure the value is in the
6871e65e175bSOded Gabbay 	 * range [0, queue_len - 1]
6872e65e175bSOded Gabbay 	 */
6873e65e175bSOded Gabbay 	return (idx + q_len - 1) & mask;
6874e65e175bSOded Gabbay }
6875e65e175bSOded Gabbay 
6876e65e175bSOded Gabbay /**
6877e65e175bSOded Gabbay  * gaudi_handle_sw_config_stream_data - print SW config stream data
6878e65e175bSOded Gabbay  *
6879e65e175bSOded Gabbay  * @hdev: pointer to the habanalabs device structure
6880e65e175bSOded Gabbay  * @stream: the QMAN's stream
6881e65e175bSOded Gabbay  * @qman_base: base address of QMAN registers block
6882e65e175bSOded Gabbay  * @event_mask: mask of the last events occurred
6883e65e175bSOded Gabbay  */
6884e65e175bSOded Gabbay static void gaudi_handle_sw_config_stream_data(struct hl_device *hdev, u32 stream,
6885e65e175bSOded Gabbay 						u64 qman_base, u64 event_mask)
6886e65e175bSOded Gabbay {
6887e65e175bSOded Gabbay 	u64 cq_ptr_lo, cq_ptr_hi, cq_tsize, cq_ptr;
6888e65e175bSOded Gabbay 	u32 cq_ptr_lo_off, size;
6889e65e175bSOded Gabbay 
6890e65e175bSOded Gabbay 	cq_ptr_lo_off = mmTPC0_QM_CQ_PTR_LO_1 - mmTPC0_QM_CQ_PTR_LO_0;
6891e65e175bSOded Gabbay 
6892e65e175bSOded Gabbay 	cq_ptr_lo = qman_base + (mmTPC0_QM_CQ_PTR_LO_0 - mmTPC0_QM_BASE) +
6893e65e175bSOded Gabbay 						stream * cq_ptr_lo_off;
6894e65e175bSOded Gabbay 	cq_ptr_hi = cq_ptr_lo +
6895e65e175bSOded Gabbay 				(mmTPC0_QM_CQ_PTR_HI_0 - mmTPC0_QM_CQ_PTR_LO_0);
6896e65e175bSOded Gabbay 	cq_tsize = cq_ptr_lo +
6897e65e175bSOded Gabbay 				(mmTPC0_QM_CQ_TSIZE_0 - mmTPC0_QM_CQ_PTR_LO_0);
6898e65e175bSOded Gabbay 
6899e65e175bSOded Gabbay 	cq_ptr = (((u64) RREG32(cq_ptr_hi)) << 32) | RREG32(cq_ptr_lo);
6900e65e175bSOded Gabbay 	size = RREG32(cq_tsize);
6901e65e175bSOded Gabbay 	dev_info(hdev->dev, "stop on err: stream: %u, addr: %#llx, size: %u\n",
6902e65e175bSOded Gabbay 							stream, cq_ptr, size);
6903e65e175bSOded Gabbay 
6904e65e175bSOded Gabbay 	if (event_mask & HL_NOTIFIER_EVENT_UNDEFINED_OPCODE) {
6905e65e175bSOded Gabbay 		hdev->captured_err_info.undef_opcode.cq_addr = cq_ptr;
6906e65e175bSOded Gabbay 		hdev->captured_err_info.undef_opcode.cq_size = size;
6907e65e175bSOded Gabbay 		hdev->captured_err_info.undef_opcode.stream_id = stream;
6908e65e175bSOded Gabbay 	}
6909e65e175bSOded Gabbay }
6910e65e175bSOded Gabbay 
6911e65e175bSOded Gabbay /**
6912e65e175bSOded Gabbay  * gaudi_handle_last_pqes_on_err - print last PQEs on error
6913e65e175bSOded Gabbay  *
6914e65e175bSOded Gabbay  * @hdev: pointer to the habanalabs device structure
6915e65e175bSOded Gabbay  * @qid_base: first QID of the QMAN (out of 4 streams)
6916e65e175bSOded Gabbay  * @stream: the QMAN's stream
6917e65e175bSOded Gabbay  * @qman_base: base address of QMAN registers block
6918e65e175bSOded Gabbay  * @event_mask: mask of the last events occurred
6919e65e175bSOded Gabbay  * @pr_sw_conf: if true print the SW config stream data (CQ PTR and SIZE)
6920e65e175bSOded Gabbay  */
6921e65e175bSOded Gabbay static void gaudi_handle_last_pqes_on_err(struct hl_device *hdev, u32 qid_base,
6922e65e175bSOded Gabbay 						u32 stream, u64 qman_base,
6923e65e175bSOded Gabbay 						u64 event_mask,
6924e65e175bSOded Gabbay 						bool pr_sw_conf)
6925e65e175bSOded Gabbay {
6926e65e175bSOded Gabbay 	u32 ci, qm_ci_stream_off, queue_len;
6927e65e175bSOded Gabbay 	struct hl_hw_queue *q;
6928e65e175bSOded Gabbay 	u64 pq_ci, addr[PQ_FETCHER_CACHE_SIZE];
6929e65e175bSOded Gabbay 	int i;
6930e65e175bSOded Gabbay 
6931e65e175bSOded Gabbay 	q = &hdev->kernel_queues[qid_base + stream];
6932e65e175bSOded Gabbay 
6933e65e175bSOded Gabbay 	qm_ci_stream_off = mmTPC0_QM_PQ_CI_1 - mmTPC0_QM_PQ_CI_0;
6934e65e175bSOded Gabbay 	pq_ci = qman_base + (mmTPC0_QM_PQ_CI_0 - mmTPC0_QM_BASE) +
6935e65e175bSOded Gabbay 						stream * qm_ci_stream_off;
6936e65e175bSOded Gabbay 
6937e65e175bSOded Gabbay 	queue_len = (q->queue_type == QUEUE_TYPE_INT) ?
6938e65e175bSOded Gabbay 					q->int_queue_len : HL_QUEUE_LENGTH;
6939e65e175bSOded Gabbay 
6940e65e175bSOded Gabbay 	hdev->asic_funcs->hw_queues_lock(hdev);
6941e65e175bSOded Gabbay 
6942e65e175bSOded Gabbay 	if (pr_sw_conf)
6943e65e175bSOded Gabbay 		gaudi_handle_sw_config_stream_data(hdev, stream, qman_base, event_mask);
6944e65e175bSOded Gabbay 
6945e65e175bSOded Gabbay 	ci = RREG32(pq_ci);
6946e65e175bSOded Gabbay 
6947e65e175bSOded Gabbay 	/* we should start printing form ci -1 */
6948e65e175bSOded Gabbay 	ci = gaudi_queue_idx_dec(ci, queue_len);
6949e65e175bSOded Gabbay 	memset(addr, 0, sizeof(addr));
6950e65e175bSOded Gabbay 
6951e65e175bSOded Gabbay 	for (i = 0; i < PQ_FETCHER_CACHE_SIZE; i++) {
6952e65e175bSOded Gabbay 		struct hl_bd *bd;
6953e65e175bSOded Gabbay 		u32 len;
6954e65e175bSOded Gabbay 
6955e65e175bSOded Gabbay 		bd = q->kernel_address;
6956e65e175bSOded Gabbay 		bd += ci;
6957e65e175bSOded Gabbay 
6958e65e175bSOded Gabbay 		len = le32_to_cpu(bd->len);
6959e65e175bSOded Gabbay 		/* len 0 means uninitialized entry- break */
6960e65e175bSOded Gabbay 		if (!len)
6961e65e175bSOded Gabbay 			break;
6962e65e175bSOded Gabbay 
6963e65e175bSOded Gabbay 		addr[i] = le64_to_cpu(bd->ptr);
6964e65e175bSOded Gabbay 
6965e65e175bSOded Gabbay 		dev_info(hdev->dev, "stop on err PQE(stream %u): ci: %u, addr: %#llx, size: %u\n",
6966e65e175bSOded Gabbay 							stream, ci, addr[i], len);
6967e65e175bSOded Gabbay 
6968e65e175bSOded Gabbay 		/* get previous ci, wrap if needed */
6969e65e175bSOded Gabbay 		ci = gaudi_queue_idx_dec(ci, queue_len);
6970e65e175bSOded Gabbay 	}
6971e65e175bSOded Gabbay 
6972e65e175bSOded Gabbay 	if (event_mask & HL_NOTIFIER_EVENT_UNDEFINED_OPCODE) {
6973e65e175bSOded Gabbay 		struct undefined_opcode_info *undef_opcode = &hdev->captured_err_info.undef_opcode;
6974e65e175bSOded Gabbay 		u32 arr_idx = undef_opcode->cb_addr_streams_len;
6975e65e175bSOded Gabbay 
6976e65e175bSOded Gabbay 		if (arr_idx == 0) {
6977e65e175bSOded Gabbay 			undef_opcode->timestamp = ktime_get();
6978e65e175bSOded Gabbay 			undef_opcode->engine_id = gaudi_queue_id_to_engine_id[qid_base];
6979e65e175bSOded Gabbay 		}
6980e65e175bSOded Gabbay 
6981e65e175bSOded Gabbay 		memcpy(undef_opcode->cb_addr_streams[arr_idx], addr, sizeof(addr));
6982e65e175bSOded Gabbay 		undef_opcode->cb_addr_streams_len++;
6983e65e175bSOded Gabbay 	}
6984e65e175bSOded Gabbay 
6985e65e175bSOded Gabbay 	hdev->asic_funcs->hw_queues_unlock(hdev);
6986e65e175bSOded Gabbay }
6987e65e175bSOded Gabbay 
6988e65e175bSOded Gabbay /**
6989e65e175bSOded Gabbay  * handle_qman_data_on_err - extract QMAN data on error
6990e65e175bSOded Gabbay  *
6991e65e175bSOded Gabbay  * @hdev: pointer to the habanalabs device structure
6992e65e175bSOded Gabbay  * @qid_base: first QID of the QMAN (out of 4 streams)
6993e65e175bSOded Gabbay  * @stream: the QMAN's stream
6994e65e175bSOded Gabbay  * @qman_base: base address of QMAN registers block
6995e65e175bSOded Gabbay  * @event_mask: mask of the last events occurred
6996e65e175bSOded Gabbay  *
6997e65e175bSOded Gabbay  * This function attempt to exatract as much data as possible on QMAN error.
6998e65e175bSOded Gabbay  * On upper CP print the SW config stream data and last 8 PQEs.
6999e65e175bSOded Gabbay  * On lower CP print SW config data and last PQEs of ALL 4 upper CPs
7000e65e175bSOded Gabbay  */
7001e65e175bSOded Gabbay static void handle_qman_data_on_err(struct hl_device *hdev, u32 qid_base,
7002e65e175bSOded Gabbay 				   u32 stream, u64 qman_base, u64 event_mask)
7003e65e175bSOded Gabbay {
7004e65e175bSOded Gabbay 	u32 i;
7005e65e175bSOded Gabbay 
7006e65e175bSOded Gabbay 	if (stream != QMAN_STREAMS) {
7007e65e175bSOded Gabbay 		gaudi_handle_last_pqes_on_err(hdev, qid_base, stream,
7008e65e175bSOded Gabbay 			qman_base, event_mask, true);
7009e65e175bSOded Gabbay 		return;
7010e65e175bSOded Gabbay 	}
7011e65e175bSOded Gabbay 
7012e65e175bSOded Gabbay 	/* handle Lower-CP */
7013e65e175bSOded Gabbay 	gaudi_handle_sw_config_stream_data(hdev, stream, qman_base, event_mask);
7014e65e175bSOded Gabbay 
7015e65e175bSOded Gabbay 	for (i = 0; i < QMAN_STREAMS; i++)
7016e65e175bSOded Gabbay 		gaudi_handle_last_pqes_on_err(hdev, qid_base, i,
7017e65e175bSOded Gabbay 			qman_base, event_mask, false);
7018e65e175bSOded Gabbay }
7019e65e175bSOded Gabbay 
7020e65e175bSOded Gabbay static void gaudi_handle_qman_err_generic(struct hl_device *hdev,
7021e65e175bSOded Gabbay 					  const char *qm_name,
7022e65e175bSOded Gabbay 					  u64 qman_base,
7023e65e175bSOded Gabbay 					  u32 qid_base,
7024e65e175bSOded Gabbay 					  u64 *event_mask)
7025e65e175bSOded Gabbay {
7026e65e175bSOded Gabbay 	u32 i, j, glbl_sts_val, arb_err_val, glbl_sts_clr_val;
7027e65e175bSOded Gabbay 	u64 glbl_sts_addr, arb_err_addr;
7028e65e175bSOded Gabbay 	char reg_desc[32];
7029e65e175bSOded Gabbay 
7030e65e175bSOded Gabbay 	glbl_sts_addr = qman_base + (mmTPC0_QM_GLBL_STS1_0 - mmTPC0_QM_BASE);
7031e65e175bSOded Gabbay 	arb_err_addr = qman_base + (mmTPC0_QM_ARB_ERR_CAUSE - mmTPC0_QM_BASE);
7032e65e175bSOded Gabbay 
7033e65e175bSOded Gabbay 	/* Iterate through all stream GLBL_STS1 registers + Lower CP */
7034e65e175bSOded Gabbay 	for (i = 0 ; i < QMAN_STREAMS + 1 ; i++) {
7035e65e175bSOded Gabbay 		glbl_sts_clr_val = 0;
7036e65e175bSOded Gabbay 		glbl_sts_val = RREG32(glbl_sts_addr + 4 * i);
7037e65e175bSOded Gabbay 
7038e65e175bSOded Gabbay 		if (!glbl_sts_val)
7039e65e175bSOded Gabbay 			continue;
7040e65e175bSOded Gabbay 
7041e65e175bSOded Gabbay 		if (i == QMAN_STREAMS)
7042e65e175bSOded Gabbay 			snprintf(reg_desc, ARRAY_SIZE(reg_desc), "LowerCP");
7043e65e175bSOded Gabbay 		else
7044e65e175bSOded Gabbay 			snprintf(reg_desc, ARRAY_SIZE(reg_desc), "stream%u", i);
7045e65e175bSOded Gabbay 
7046e65e175bSOded Gabbay 		for (j = 0 ; j < GAUDI_NUM_OF_QM_ERR_CAUSE ; j++) {
7047e65e175bSOded Gabbay 			if (glbl_sts_val & BIT(j)) {
7048e65e175bSOded Gabbay 				dev_err_ratelimited(hdev->dev,
7049e65e175bSOded Gabbay 						"%s %s. err cause: %s\n",
7050e65e175bSOded Gabbay 						qm_name, reg_desc,
7051e65e175bSOded Gabbay 						gaudi_qman_error_cause[j]);
7052e65e175bSOded Gabbay 				glbl_sts_clr_val |= BIT(j);
7053e65e175bSOded Gabbay 			}
7054e65e175bSOded Gabbay 		}
7055e65e175bSOded Gabbay 		/* check for undefined opcode */
7056e65e175bSOded Gabbay 		if (glbl_sts_val & TPC0_QM_GLBL_STS1_CP_UNDEF_CMD_ERR_MASK &&
7057e65e175bSOded Gabbay 				hdev->captured_err_info.undef_opcode.write_enable) {
7058e65e175bSOded Gabbay 			memset(&hdev->captured_err_info.undef_opcode, 0,
7059e65e175bSOded Gabbay 						sizeof(hdev->captured_err_info.undef_opcode));
7060e65e175bSOded Gabbay 
7061e65e175bSOded Gabbay 			hdev->captured_err_info.undef_opcode.write_enable = false;
7062e65e175bSOded Gabbay 			*event_mask |= HL_NOTIFIER_EVENT_UNDEFINED_OPCODE;
7063e65e175bSOded Gabbay 		}
7064e65e175bSOded Gabbay 
7065e65e175bSOded Gabbay 		/* Write 1 clear errors */
7066e65e175bSOded Gabbay 		if (!hdev->stop_on_err)
7067e65e175bSOded Gabbay 			WREG32(glbl_sts_addr + 4 * i, glbl_sts_clr_val);
7068e65e175bSOded Gabbay 		else
7069e65e175bSOded Gabbay 			handle_qman_data_on_err(hdev, qid_base, i, qman_base, *event_mask);
7070e65e175bSOded Gabbay 	}
7071e65e175bSOded Gabbay 
7072e65e175bSOded Gabbay 	arb_err_val = RREG32(arb_err_addr);
7073e65e175bSOded Gabbay 
7074e65e175bSOded Gabbay 	if (!arb_err_val)
7075e65e175bSOded Gabbay 		return;
7076e65e175bSOded Gabbay 
7077e65e175bSOded Gabbay 	for (j = 0 ; j < GAUDI_NUM_OF_QM_ARB_ERR_CAUSE ; j++) {
7078e65e175bSOded Gabbay 		if (arb_err_val & BIT(j)) {
7079e65e175bSOded Gabbay 			dev_err_ratelimited(hdev->dev,
7080e65e175bSOded Gabbay 					"%s ARB_ERR. err cause: %s\n",
7081e65e175bSOded Gabbay 					qm_name,
7082e65e175bSOded Gabbay 					gaudi_qman_arb_error_cause[j]);
7083e65e175bSOded Gabbay 		}
7084e65e175bSOded Gabbay 	}
7085e65e175bSOded Gabbay }
7086e65e175bSOded Gabbay 
7087e65e175bSOded Gabbay static void gaudi_print_sm_sei_info(struct hl_device *hdev, u16 event_type,
7088e65e175bSOded Gabbay 		struct hl_eq_sm_sei_data *sei_data)
7089e65e175bSOded Gabbay {
7090e65e175bSOded Gabbay 	u32 index = event_type - GAUDI_EVENT_DMA_IF_SEI_0;
7091e65e175bSOded Gabbay 
7092e65e175bSOded Gabbay 	/* Flip the bits as the enum is ordered in the opposite way */
7093e65e175bSOded Gabbay 	index = (index ^ 0x3) & 0x3;
7094e65e175bSOded Gabbay 
7095e65e175bSOded Gabbay 	switch (sei_data->sei_cause) {
7096e65e175bSOded Gabbay 	case SM_SEI_SO_OVERFLOW:
7097e65e175bSOded Gabbay 		dev_err_ratelimited(hdev->dev,
7098e65e175bSOded Gabbay 			"%s SEI Error: SOB Group %u overflow/underflow",
7099e65e175bSOded Gabbay 			gaudi_sync_manager_names[index],
7100e65e175bSOded Gabbay 			le32_to_cpu(sei_data->sei_log));
7101e65e175bSOded Gabbay 		break;
7102e65e175bSOded Gabbay 	case SM_SEI_LBW_4B_UNALIGNED:
7103e65e175bSOded Gabbay 		dev_err_ratelimited(hdev->dev,
7104e65e175bSOded Gabbay 			"%s SEI Error: Unaligned 4B LBW access, monitor agent address low - %#x",
7105e65e175bSOded Gabbay 			gaudi_sync_manager_names[index],
7106e65e175bSOded Gabbay 			le32_to_cpu(sei_data->sei_log));
7107e65e175bSOded Gabbay 		break;
7108e65e175bSOded Gabbay 	case SM_SEI_AXI_RESPONSE_ERR:
7109e65e175bSOded Gabbay 		dev_err_ratelimited(hdev->dev,
7110e65e175bSOded Gabbay 			"%s SEI Error: AXI ID %u response error",
7111e65e175bSOded Gabbay 			gaudi_sync_manager_names[index],
7112e65e175bSOded Gabbay 			le32_to_cpu(sei_data->sei_log));
7113e65e175bSOded Gabbay 		break;
7114e65e175bSOded Gabbay 	default:
7115e65e175bSOded Gabbay 		dev_err_ratelimited(hdev->dev, "Unknown SM SEI cause %u",
7116e65e175bSOded Gabbay 				le32_to_cpu(sei_data->sei_log));
7117e65e175bSOded Gabbay 		break;
7118e65e175bSOded Gabbay 	}
7119e65e175bSOded Gabbay }
7120e65e175bSOded Gabbay 
7121e65e175bSOded Gabbay static void gaudi_handle_ecc_event(struct hl_device *hdev, u16 event_type,
7122e65e175bSOded Gabbay 		struct hl_eq_ecc_data *ecc_data)
7123e65e175bSOded Gabbay {
7124e65e175bSOded Gabbay 	struct ecc_info_extract_params params;
7125e65e175bSOded Gabbay 	u64 ecc_address = 0, ecc_syndrom = 0;
7126e65e175bSOded Gabbay 	u8 index, memory_wrapper_idx = 0;
7127e65e175bSOded Gabbay 	bool extract_info_from_fw;
7128e65e175bSOded Gabbay 	int rc;
7129e65e175bSOded Gabbay 
7130e65e175bSOded Gabbay 	if (hdev->asic_prop.fw_security_enabled) {
7131e65e175bSOded Gabbay 		extract_info_from_fw = true;
7132e65e175bSOded Gabbay 		goto extract_ecc_info;
7133e65e175bSOded Gabbay 	}
7134e65e175bSOded Gabbay 
7135e65e175bSOded Gabbay 	switch (event_type) {
7136e65e175bSOded Gabbay 	case GAUDI_EVENT_PCIE_CORE_SERR ... GAUDI_EVENT_PCIE_PHY_DERR:
7137e65e175bSOded Gabbay 	case GAUDI_EVENT_DMA0_SERR_ECC ... GAUDI_EVENT_MMU_DERR:
7138e65e175bSOded Gabbay 		extract_info_from_fw = true;
7139e65e175bSOded Gabbay 		break;
7140e65e175bSOded Gabbay 	case GAUDI_EVENT_TPC0_SERR ... GAUDI_EVENT_TPC7_SERR:
7141e65e175bSOded Gabbay 		index = event_type - GAUDI_EVENT_TPC0_SERR;
7142e65e175bSOded Gabbay 		params.block_address = mmTPC0_CFG_BASE + index * TPC_CFG_OFFSET;
7143e65e175bSOded Gabbay 		params.num_memories = 90;
7144e65e175bSOded Gabbay 		params.derr = false;
7145e65e175bSOded Gabbay 		extract_info_from_fw = false;
7146e65e175bSOded Gabbay 		break;
7147e65e175bSOded Gabbay 	case GAUDI_EVENT_TPC0_DERR ... GAUDI_EVENT_TPC7_DERR:
7148e65e175bSOded Gabbay 		index = event_type - GAUDI_EVENT_TPC0_DERR;
7149e65e175bSOded Gabbay 		params.block_address =
7150e65e175bSOded Gabbay 			mmTPC0_CFG_BASE + index * TPC_CFG_OFFSET;
7151e65e175bSOded Gabbay 		params.num_memories = 90;
7152e65e175bSOded Gabbay 		params.derr = true;
7153e65e175bSOded Gabbay 		extract_info_from_fw = false;
7154e65e175bSOded Gabbay 		break;
7155e65e175bSOded Gabbay 	case GAUDI_EVENT_MME0_ACC_SERR:
7156e65e175bSOded Gabbay 	case GAUDI_EVENT_MME1_ACC_SERR:
7157e65e175bSOded Gabbay 	case GAUDI_EVENT_MME2_ACC_SERR:
7158e65e175bSOded Gabbay 	case GAUDI_EVENT_MME3_ACC_SERR:
7159e65e175bSOded Gabbay 		index = (event_type - GAUDI_EVENT_MME0_ACC_SERR) / 4;
7160e65e175bSOded Gabbay 		params.block_address = mmMME0_ACC_BASE + index * MME_ACC_OFFSET;
7161e65e175bSOded Gabbay 		params.num_memories = 128;
7162e65e175bSOded Gabbay 		params.derr = false;
7163e65e175bSOded Gabbay 		extract_info_from_fw = false;
7164e65e175bSOded Gabbay 		break;
7165e65e175bSOded Gabbay 	case GAUDI_EVENT_MME0_ACC_DERR:
7166e65e175bSOded Gabbay 	case GAUDI_EVENT_MME1_ACC_DERR:
7167e65e175bSOded Gabbay 	case GAUDI_EVENT_MME2_ACC_DERR:
7168e65e175bSOded Gabbay 	case GAUDI_EVENT_MME3_ACC_DERR:
7169e65e175bSOded Gabbay 		index = (event_type - GAUDI_EVENT_MME0_ACC_DERR) / 4;
7170e65e175bSOded Gabbay 		params.block_address = mmMME0_ACC_BASE + index * MME_ACC_OFFSET;
7171e65e175bSOded Gabbay 		params.num_memories = 128;
7172e65e175bSOded Gabbay 		params.derr = true;
7173e65e175bSOded Gabbay 		extract_info_from_fw = false;
7174e65e175bSOded Gabbay 		break;
7175e65e175bSOded Gabbay 	case GAUDI_EVENT_MME0_SBAB_SERR:
7176e65e175bSOded Gabbay 	case GAUDI_EVENT_MME1_SBAB_SERR:
7177e65e175bSOded Gabbay 	case GAUDI_EVENT_MME2_SBAB_SERR:
7178e65e175bSOded Gabbay 	case GAUDI_EVENT_MME3_SBAB_SERR:
7179e65e175bSOded Gabbay 		index = (event_type - GAUDI_EVENT_MME0_SBAB_SERR) / 4;
7180e65e175bSOded Gabbay 		params.block_address =
7181e65e175bSOded Gabbay 			mmMME0_SBAB_BASE + index * MME_ACC_OFFSET;
7182e65e175bSOded Gabbay 		params.num_memories = 33;
7183e65e175bSOded Gabbay 		params.derr = false;
7184e65e175bSOded Gabbay 		extract_info_from_fw = false;
7185e65e175bSOded Gabbay 		break;
7186e65e175bSOded Gabbay 	case GAUDI_EVENT_MME0_SBAB_DERR:
7187e65e175bSOded Gabbay 	case GAUDI_EVENT_MME1_SBAB_DERR:
7188e65e175bSOded Gabbay 	case GAUDI_EVENT_MME2_SBAB_DERR:
7189e65e175bSOded Gabbay 	case GAUDI_EVENT_MME3_SBAB_DERR:
7190e65e175bSOded Gabbay 		index = (event_type - GAUDI_EVENT_MME0_SBAB_DERR) / 4;
7191e65e175bSOded Gabbay 		params.block_address =
7192e65e175bSOded Gabbay 			mmMME0_SBAB_BASE + index * MME_ACC_OFFSET;
7193e65e175bSOded Gabbay 		params.num_memories = 33;
7194e65e175bSOded Gabbay 		params.derr = true;
7195e65e175bSOded Gabbay 		extract_info_from_fw = false;
7196e65e175bSOded Gabbay 		break;
7197e65e175bSOded Gabbay 	default:
7198e65e175bSOded Gabbay 		return;
7199e65e175bSOded Gabbay 	}
7200e65e175bSOded Gabbay 
7201e65e175bSOded Gabbay extract_ecc_info:
7202e65e175bSOded Gabbay 	if (extract_info_from_fw) {
7203e65e175bSOded Gabbay 		ecc_address = le64_to_cpu(ecc_data->ecc_address);
7204e65e175bSOded Gabbay 		ecc_syndrom = le64_to_cpu(ecc_data->ecc_syndrom);
7205e65e175bSOded Gabbay 		memory_wrapper_idx = ecc_data->memory_wrapper_idx;
7206e65e175bSOded Gabbay 	} else {
7207e65e175bSOded Gabbay 		rc = gaudi_extract_ecc_info(hdev, &params, &ecc_address,
7208e65e175bSOded Gabbay 				&ecc_syndrom, &memory_wrapper_idx);
7209e65e175bSOded Gabbay 		if (rc)
7210e65e175bSOded Gabbay 			return;
7211e65e175bSOded Gabbay 	}
7212e65e175bSOded Gabbay 
7213e65e175bSOded Gabbay 	dev_err(hdev->dev,
7214e65e175bSOded Gabbay 		"ECC error detected. address: %#llx. Syndrom: %#llx. block id %u\n",
7215e65e175bSOded Gabbay 		ecc_address, ecc_syndrom, memory_wrapper_idx);
7216e65e175bSOded Gabbay }
7217e65e175bSOded Gabbay 
7218e65e175bSOded Gabbay static void gaudi_handle_qman_err(struct hl_device *hdev, u16 event_type, u64 *event_mask)
7219e65e175bSOded Gabbay {
7220e65e175bSOded Gabbay 	u64 qman_base;
7221e65e175bSOded Gabbay 	char desc[32];
7222e65e175bSOded Gabbay 	u32 qid_base;
7223e65e175bSOded Gabbay 	u8 index;
7224e65e175bSOded Gabbay 
7225e65e175bSOded Gabbay 	switch (event_type) {
7226e65e175bSOded Gabbay 	case GAUDI_EVENT_TPC0_QM ... GAUDI_EVENT_TPC7_QM:
7227e65e175bSOded Gabbay 		index = event_type - GAUDI_EVENT_TPC0_QM;
7228e65e175bSOded Gabbay 		qid_base = GAUDI_QUEUE_ID_TPC_0_0 + index * QMAN_STREAMS;
7229e65e175bSOded Gabbay 		qman_base = mmTPC0_QM_BASE + index * TPC_QMAN_OFFSET;
7230e65e175bSOded Gabbay 		snprintf(desc, ARRAY_SIZE(desc), "%s%d", "TPC_QM", index);
7231e65e175bSOded Gabbay 		break;
7232e65e175bSOded Gabbay 	case GAUDI_EVENT_MME0_QM ... GAUDI_EVENT_MME2_QM:
7233e65e175bSOded Gabbay 		if (event_type == GAUDI_EVENT_MME0_QM) {
7234e65e175bSOded Gabbay 			index = 0;
7235e65e175bSOded Gabbay 			qid_base = GAUDI_QUEUE_ID_MME_0_0;
7236e65e175bSOded Gabbay 		} else { /* event_type == GAUDI_EVENT_MME2_QM */
7237e65e175bSOded Gabbay 			index = 2;
7238e65e175bSOded Gabbay 			qid_base = GAUDI_QUEUE_ID_MME_1_0;
7239e65e175bSOded Gabbay 		}
7240e65e175bSOded Gabbay 		qman_base = mmMME0_QM_BASE + index * MME_QMAN_OFFSET;
7241e65e175bSOded Gabbay 		snprintf(desc, ARRAY_SIZE(desc), "%s%d", "MME_QM", index);
7242e65e175bSOded Gabbay 		break;
7243e65e175bSOded Gabbay 	case GAUDI_EVENT_DMA0_QM ... GAUDI_EVENT_DMA7_QM:
7244e65e175bSOded Gabbay 		index = event_type - GAUDI_EVENT_DMA0_QM;
7245e65e175bSOded Gabbay 		qid_base = GAUDI_QUEUE_ID_DMA_0_0 + index * QMAN_STREAMS;
7246e65e175bSOded Gabbay 		/* skip GAUDI_QUEUE_ID_CPU_PQ if necessary */
7247e65e175bSOded Gabbay 		if (index > 1)
7248e65e175bSOded Gabbay 			qid_base++;
7249e65e175bSOded Gabbay 		qman_base = mmDMA0_QM_BASE + index * DMA_QMAN_OFFSET;
7250e65e175bSOded Gabbay 		snprintf(desc, ARRAY_SIZE(desc), "%s%d", "DMA_QM", index);
7251e65e175bSOded Gabbay 		break;
7252e65e175bSOded Gabbay 	case GAUDI_EVENT_NIC0_QM0:
7253e65e175bSOded Gabbay 		qid_base = GAUDI_QUEUE_ID_NIC_0_0;
7254e65e175bSOded Gabbay 		qman_base = mmNIC0_QM0_BASE;
7255e65e175bSOded Gabbay 		snprintf(desc, ARRAY_SIZE(desc), "NIC0_QM0");
7256e65e175bSOded Gabbay 		break;
7257e65e175bSOded Gabbay 	case GAUDI_EVENT_NIC0_QM1:
7258e65e175bSOded Gabbay 		qid_base = GAUDI_QUEUE_ID_NIC_1_0;
7259e65e175bSOded Gabbay 		qman_base = mmNIC0_QM1_BASE;
7260e65e175bSOded Gabbay 		snprintf(desc, ARRAY_SIZE(desc), "NIC0_QM1");
7261e65e175bSOded Gabbay 		break;
7262e65e175bSOded Gabbay 	case GAUDI_EVENT_NIC1_QM0:
7263e65e175bSOded Gabbay 		qid_base = GAUDI_QUEUE_ID_NIC_2_0;
7264e65e175bSOded Gabbay 		qman_base = mmNIC1_QM0_BASE;
7265e65e175bSOded Gabbay 		snprintf(desc, ARRAY_SIZE(desc), "NIC1_QM0");
7266e65e175bSOded Gabbay 		break;
7267e65e175bSOded Gabbay 	case GAUDI_EVENT_NIC1_QM1:
7268e65e175bSOded Gabbay 		qid_base = GAUDI_QUEUE_ID_NIC_3_0;
7269e65e175bSOded Gabbay 		qman_base = mmNIC1_QM1_BASE;
7270e65e175bSOded Gabbay 		snprintf(desc, ARRAY_SIZE(desc), "NIC1_QM1");
7271e65e175bSOded Gabbay 		break;
7272e65e175bSOded Gabbay 	case GAUDI_EVENT_NIC2_QM0:
7273e65e175bSOded Gabbay 		qid_base = GAUDI_QUEUE_ID_NIC_4_0;
7274e65e175bSOded Gabbay 		qman_base = mmNIC2_QM0_BASE;
7275e65e175bSOded Gabbay 		snprintf(desc, ARRAY_SIZE(desc), "NIC2_QM0");
7276e65e175bSOded Gabbay 		break;
7277e65e175bSOded Gabbay 	case GAUDI_EVENT_NIC2_QM1:
7278e65e175bSOded Gabbay 		qid_base = GAUDI_QUEUE_ID_NIC_5_0;
7279e65e175bSOded Gabbay 		qman_base = mmNIC2_QM1_BASE;
7280e65e175bSOded Gabbay 		snprintf(desc, ARRAY_SIZE(desc), "NIC2_QM1");
7281e65e175bSOded Gabbay 		break;
7282e65e175bSOded Gabbay 	case GAUDI_EVENT_NIC3_QM0:
7283e65e175bSOded Gabbay 		qid_base = GAUDI_QUEUE_ID_NIC_6_0;
7284e65e175bSOded Gabbay 		qman_base = mmNIC3_QM0_BASE;
7285e65e175bSOded Gabbay 		snprintf(desc, ARRAY_SIZE(desc), "NIC3_QM0");
7286e65e175bSOded Gabbay 		break;
7287e65e175bSOded Gabbay 	case GAUDI_EVENT_NIC3_QM1:
7288e65e175bSOded Gabbay 		qid_base = GAUDI_QUEUE_ID_NIC_7_0;
7289e65e175bSOded Gabbay 		qman_base = mmNIC3_QM1_BASE;
7290e65e175bSOded Gabbay 		snprintf(desc, ARRAY_SIZE(desc), "NIC3_QM1");
7291e65e175bSOded Gabbay 		break;
7292e65e175bSOded Gabbay 	case GAUDI_EVENT_NIC4_QM0:
7293e65e175bSOded Gabbay 		qid_base = GAUDI_QUEUE_ID_NIC_8_0;
7294e65e175bSOded Gabbay 		qman_base = mmNIC4_QM0_BASE;
7295e65e175bSOded Gabbay 		snprintf(desc, ARRAY_SIZE(desc), "NIC4_QM0");
7296e65e175bSOded Gabbay 		break;
7297e65e175bSOded Gabbay 	case GAUDI_EVENT_NIC4_QM1:
7298e65e175bSOded Gabbay 		qid_base = GAUDI_QUEUE_ID_NIC_9_0;
7299e65e175bSOded Gabbay 		qman_base = mmNIC4_QM1_BASE;
7300e65e175bSOded Gabbay 		snprintf(desc, ARRAY_SIZE(desc), "NIC4_QM1");
7301e65e175bSOded Gabbay 		break;
7302e65e175bSOded Gabbay 	default:
7303e65e175bSOded Gabbay 		return;
7304e65e175bSOded Gabbay 	}
7305e65e175bSOded Gabbay 
7306e65e175bSOded Gabbay 	gaudi_handle_qman_err_generic(hdev, desc, qman_base, qid_base, event_mask);
7307e65e175bSOded Gabbay }
7308e65e175bSOded Gabbay 
7309e65e175bSOded Gabbay static void gaudi_print_irq_info(struct hl_device *hdev, u16 event_type,
73104b9c2d36SKoby Elbaz 					bool check_razwi, u64 *event_mask)
7311e65e175bSOded Gabbay {
7312e65e175bSOded Gabbay 	bool is_read = false, is_write = false;
7313e65e175bSOded Gabbay 	u16 engine_id[2], num_of_razwi_eng = 0;
7314e65e175bSOded Gabbay 	char desc[64] = "";
7315e65e175bSOded Gabbay 	u64 razwi_addr = 0;
7316e65e175bSOded Gabbay 	u8 razwi_flags = 0;
7317e65e175bSOded Gabbay 
7318e65e175bSOded Gabbay 	/*
7319e65e175bSOded Gabbay 	 * Init engine id by default as not valid and only if razwi initiated from engine with
7320e65e175bSOded Gabbay 	 * engine id it will get valid value.
7321e65e175bSOded Gabbay 	 */
7322e65e175bSOded Gabbay 	engine_id[0] = HL_RAZWI_NA_ENG_ID;
7323e65e175bSOded Gabbay 	engine_id[1] = HL_RAZWI_NA_ENG_ID;
7324e65e175bSOded Gabbay 
7325e65e175bSOded Gabbay 	gaudi_get_event_desc(event_type, desc, sizeof(desc));
7326e65e175bSOded Gabbay 	dev_err_ratelimited(hdev->dev, "Received H/W interrupt %d [\"%s\"]\n",
7327e65e175bSOded Gabbay 		event_type, desc);
7328e65e175bSOded Gabbay 
73294b9c2d36SKoby Elbaz 	if (check_razwi) {
7330e65e175bSOded Gabbay 		gaudi_print_and_get_razwi_info(hdev, &engine_id[0], &engine_id[1], &is_read,
7331e65e175bSOded Gabbay 						&is_write);
7332e65e175bSOded Gabbay 		gaudi_print_and_get_mmu_error_info(hdev, &razwi_addr, event_mask);
7333e65e175bSOded Gabbay 
7334e65e175bSOded Gabbay 		if (is_read)
7335e65e175bSOded Gabbay 			razwi_flags |= HL_RAZWI_READ;
7336e65e175bSOded Gabbay 		if (is_write)
7337e65e175bSOded Gabbay 			razwi_flags |= HL_RAZWI_WRITE;
7338e65e175bSOded Gabbay 
7339e65e175bSOded Gabbay 		if (engine_id[0] != HL_RAZWI_NA_ENG_ID) {
7340e65e175bSOded Gabbay 			if (engine_id[1] != HL_RAZWI_NA_ENG_ID)
7341e65e175bSOded Gabbay 				num_of_razwi_eng = 2;
7342e65e175bSOded Gabbay 			else
7343e65e175bSOded Gabbay 				num_of_razwi_eng = 1;
7344e65e175bSOded Gabbay 		}
7345e65e175bSOded Gabbay 
73464b9c2d36SKoby Elbaz 		if (razwi_flags)
73474b9c2d36SKoby Elbaz 			hl_handle_razwi(hdev, razwi_addr, engine_id, num_of_razwi_eng,
73484b9c2d36SKoby Elbaz 					razwi_flags, event_mask);
7349e65e175bSOded Gabbay 	}
7350e65e175bSOded Gabbay }
7351e65e175bSOded Gabbay 
7352e65e175bSOded Gabbay static void gaudi_print_out_of_sync_info(struct hl_device *hdev,
7353e65e175bSOded Gabbay 					struct cpucp_pkt_sync_err *sync_err)
7354e65e175bSOded Gabbay {
7355e65e175bSOded Gabbay 	struct hl_hw_queue *q = &hdev->kernel_queues[GAUDI_QUEUE_ID_CPU_PQ];
7356e65e175bSOded Gabbay 
7357e65e175bSOded Gabbay 	dev_err(hdev->dev, "Out of sync with FW, FW: pi=%u, ci=%u, LKD: pi=%u, ci=%d\n",
7358e65e175bSOded Gabbay 		le32_to_cpu(sync_err->pi), le32_to_cpu(sync_err->ci), q->pi, atomic_read(&q->ci));
7359e65e175bSOded Gabbay }
7360e65e175bSOded Gabbay 
7361e65e175bSOded Gabbay static void gaudi_print_fw_alive_info(struct hl_device *hdev,
7362e65e175bSOded Gabbay 					struct hl_eq_fw_alive *fw_alive)
7363e65e175bSOded Gabbay {
7364e65e175bSOded Gabbay 	dev_err(hdev->dev,
7365e65e175bSOded Gabbay 		"FW alive report: severity=%s, process_id=%u, thread_id=%u, uptime=%llu seconds\n",
7366e65e175bSOded Gabbay 		(fw_alive->severity == FW_ALIVE_SEVERITY_MINOR) ? "Minor" : "Critical",
7367e65e175bSOded Gabbay 		le32_to_cpu(fw_alive->process_id),
7368e65e175bSOded Gabbay 		le32_to_cpu(fw_alive->thread_id),
7369e65e175bSOded Gabbay 		le64_to_cpu(fw_alive->uptime_seconds));
7370e65e175bSOded Gabbay }
7371e65e175bSOded Gabbay 
7372e65e175bSOded Gabbay static void gaudi_print_nic_axi_irq_info(struct hl_device *hdev, u16 event_type,
7373e65e175bSOded Gabbay 						void *data)
7374e65e175bSOded Gabbay {
7375e65e175bSOded Gabbay 	char desc[64] = "", *type;
7376e65e175bSOded Gabbay 	struct eq_nic_sei_event *eq_nic_sei = data;
7377e65e175bSOded Gabbay 	u16 nic_id = event_type - GAUDI_EVENT_NIC_SEI_0;
7378e65e175bSOded Gabbay 
7379e65e175bSOded Gabbay 	switch (eq_nic_sei->axi_error_cause) {
7380e65e175bSOded Gabbay 	case RXB:
7381e65e175bSOded Gabbay 		type = "RXB";
7382e65e175bSOded Gabbay 		break;
7383e65e175bSOded Gabbay 	case RXE:
7384e65e175bSOded Gabbay 		type = "RXE";
7385e65e175bSOded Gabbay 		break;
7386e65e175bSOded Gabbay 	case TXS:
7387e65e175bSOded Gabbay 		type = "TXS";
7388e65e175bSOded Gabbay 		break;
7389e65e175bSOded Gabbay 	case TXE:
7390e65e175bSOded Gabbay 		type = "TXE";
7391e65e175bSOded Gabbay 		break;
7392e65e175bSOded Gabbay 	case QPC_RESP:
7393e65e175bSOded Gabbay 		type = "QPC_RESP";
7394e65e175bSOded Gabbay 		break;
7395e65e175bSOded Gabbay 	case NON_AXI_ERR:
7396e65e175bSOded Gabbay 		type = "NON_AXI_ERR";
7397e65e175bSOded Gabbay 		break;
7398e65e175bSOded Gabbay 	case TMR:
7399e65e175bSOded Gabbay 		type = "TMR";
7400e65e175bSOded Gabbay 		break;
7401e65e175bSOded Gabbay 	default:
7402e65e175bSOded Gabbay 		dev_err(hdev->dev, "unknown NIC AXI cause %d\n",
7403e65e175bSOded Gabbay 			eq_nic_sei->axi_error_cause);
7404e65e175bSOded Gabbay 		type = "N/A";
7405e65e175bSOded Gabbay 		break;
7406e65e175bSOded Gabbay 	}
7407e65e175bSOded Gabbay 
7408e65e175bSOded Gabbay 	snprintf(desc, sizeof(desc), "NIC%d_%s%d", nic_id, type,
7409e65e175bSOded Gabbay 			eq_nic_sei->id);
7410e65e175bSOded Gabbay 	dev_err_ratelimited(hdev->dev, "Received H/W interrupt %d [\"%s\"]\n",
7411e65e175bSOded Gabbay 		event_type, desc);
7412e65e175bSOded Gabbay }
7413e65e175bSOded Gabbay 
7414e65e175bSOded Gabbay static int gaudi_compute_reset_late_init(struct hl_device *hdev)
7415e65e175bSOded Gabbay {
7416e65e175bSOded Gabbay 	/* GAUDI doesn't support any reset except hard-reset */
7417e65e175bSOded Gabbay 	return -EPERM;
7418e65e175bSOded Gabbay }
7419e65e175bSOded Gabbay 
7420e65e175bSOded Gabbay static int gaudi_hbm_read_interrupts(struct hl_device *hdev, int device,
7421e65e175bSOded Gabbay 			struct hl_eq_hbm_ecc_data *hbm_ecc_data)
7422e65e175bSOded Gabbay {
7423e65e175bSOded Gabbay 	u32 base, val, val2, wr_par, rd_par, ca_par, derr, serr, type, ch;
7424e65e175bSOded Gabbay 	int rc = 0;
7425e65e175bSOded Gabbay 
7426e65e175bSOded Gabbay 	if (hdev->asic_prop.fw_app_cpu_boot_dev_sts0 &
7427e65e175bSOded Gabbay 					CPU_BOOT_DEV_STS0_HBM_ECC_EN) {
7428e65e175bSOded Gabbay 		if (!hbm_ecc_data) {
7429e65e175bSOded Gabbay 			dev_err(hdev->dev, "No FW ECC data");
7430e65e175bSOded Gabbay 			return 0;
7431e65e175bSOded Gabbay 		}
7432e65e175bSOded Gabbay 
7433e65e175bSOded Gabbay 		wr_par = FIELD_GET(CPUCP_PKT_HBM_ECC_INFO_WR_PAR_MASK,
7434e65e175bSOded Gabbay 				le32_to_cpu(hbm_ecc_data->hbm_ecc_info));
7435e65e175bSOded Gabbay 		rd_par = FIELD_GET(CPUCP_PKT_HBM_ECC_INFO_RD_PAR_MASK,
7436e65e175bSOded Gabbay 				le32_to_cpu(hbm_ecc_data->hbm_ecc_info));
7437e65e175bSOded Gabbay 		ca_par = FIELD_GET(CPUCP_PKT_HBM_ECC_INFO_CA_PAR_MASK,
7438e65e175bSOded Gabbay 				le32_to_cpu(hbm_ecc_data->hbm_ecc_info));
7439e65e175bSOded Gabbay 		derr = FIELD_GET(CPUCP_PKT_HBM_ECC_INFO_DERR_MASK,
7440e65e175bSOded Gabbay 				le32_to_cpu(hbm_ecc_data->hbm_ecc_info));
7441e65e175bSOded Gabbay 		serr = FIELD_GET(CPUCP_PKT_HBM_ECC_INFO_SERR_MASK,
7442e65e175bSOded Gabbay 				le32_to_cpu(hbm_ecc_data->hbm_ecc_info));
7443e65e175bSOded Gabbay 		type = FIELD_GET(CPUCP_PKT_HBM_ECC_INFO_TYPE_MASK,
7444e65e175bSOded Gabbay 				le32_to_cpu(hbm_ecc_data->hbm_ecc_info));
7445e65e175bSOded Gabbay 		ch = FIELD_GET(CPUCP_PKT_HBM_ECC_INFO_HBM_CH_MASK,
7446e65e175bSOded Gabbay 				le32_to_cpu(hbm_ecc_data->hbm_ecc_info));
7447e65e175bSOded Gabbay 
7448e65e175bSOded Gabbay 		dev_err(hdev->dev,
7449e65e175bSOded Gabbay 			"HBM%d pc%d interrupts info: WR_PAR=%d, RD_PAR=%d, CA_PAR=%d, SERR=%d, DERR=%d\n",
7450e65e175bSOded Gabbay 			device, ch, wr_par, rd_par, ca_par, serr, derr);
7451e65e175bSOded Gabbay 		dev_err(hdev->dev,
7452e65e175bSOded Gabbay 			"HBM%d pc%d ECC info: 1ST_ERR_ADDR=0x%x, 1ST_ERR_TYPE=%d, SEC_CONT_CNT=%u, SEC_CNT=%d, DEC_CNT=%d\n",
7453e65e175bSOded Gabbay 			device, ch, hbm_ecc_data->first_addr, type,
7454e65e175bSOded Gabbay 			hbm_ecc_data->sec_cont_cnt, hbm_ecc_data->sec_cnt,
7455e65e175bSOded Gabbay 			hbm_ecc_data->dec_cnt);
7456e65e175bSOded Gabbay 		return 0;
7457e65e175bSOded Gabbay 	}
7458e65e175bSOded Gabbay 
7459e65e175bSOded Gabbay 	if (hdev->asic_prop.fw_security_enabled) {
7460e65e175bSOded Gabbay 		dev_info(hdev->dev, "Cannot access MC regs for ECC data while security is enabled\n");
7461e65e175bSOded Gabbay 		return 0;
7462e65e175bSOded Gabbay 	}
7463e65e175bSOded Gabbay 
7464e65e175bSOded Gabbay 	base = GAUDI_HBM_CFG_BASE + device * GAUDI_HBM_CFG_OFFSET;
7465e65e175bSOded Gabbay 	for (ch = 0 ; ch < GAUDI_HBM_CHANNELS ; ch++) {
7466e65e175bSOded Gabbay 		val = RREG32_MASK(base + ch * 0x1000 + 0x06C, 0x0000FFFF);
7467e65e175bSOded Gabbay 		val = (val & 0xFF) | ((val >> 8) & 0xFF);
7468e65e175bSOded Gabbay 		if (val) {
7469e65e175bSOded Gabbay 			rc = -EIO;
7470e65e175bSOded Gabbay 			dev_err(hdev->dev,
7471e65e175bSOded Gabbay 				"HBM%d pc%d interrupts info: WR_PAR=%d, RD_PAR=%d, CA_PAR=%d, SERR=%d, DERR=%d\n",
7472e65e175bSOded Gabbay 				device, ch * 2, val & 0x1, (val >> 1) & 0x1,
7473e65e175bSOded Gabbay 				(val >> 2) & 0x1, (val >> 3) & 0x1,
7474e65e175bSOded Gabbay 				(val >> 4) & 0x1);
7475e65e175bSOded Gabbay 
7476e65e175bSOded Gabbay 			val2 = RREG32(base + ch * 0x1000 + 0x060);
7477e65e175bSOded Gabbay 			dev_err(hdev->dev,
7478e65e175bSOded Gabbay 				"HBM%d pc%d ECC info: 1ST_ERR_ADDR=0x%x, 1ST_ERR_TYPE=%d, SEC_CONT_CNT=%d, SEC_CNT=%d, DEC_CNT=%d\n",
7479e65e175bSOded Gabbay 				device, ch * 2,
7480e65e175bSOded Gabbay 				RREG32(base + ch * 0x1000 + 0x064),
7481e65e175bSOded Gabbay 				(val2 & 0x200) >> 9, (val2 & 0xFC00) >> 10,
7482e65e175bSOded Gabbay 				(val2 & 0xFF0000) >> 16,
7483e65e175bSOded Gabbay 				(val2 & 0xFF000000) >> 24);
7484e65e175bSOded Gabbay 		}
7485e65e175bSOded Gabbay 
7486e65e175bSOded Gabbay 		val = RREG32_MASK(base + ch * 0x1000 + 0x07C, 0x0000FFFF);
7487e65e175bSOded Gabbay 		val = (val & 0xFF) | ((val >> 8) & 0xFF);
7488e65e175bSOded Gabbay 		if (val) {
7489e65e175bSOded Gabbay 			rc = -EIO;
7490e65e175bSOded Gabbay 			dev_err(hdev->dev,
7491e65e175bSOded Gabbay 				"HBM%d pc%d interrupts info: WR_PAR=%d, RD_PAR=%d, CA_PAR=%d, SERR=%d, DERR=%d\n",
7492e65e175bSOded Gabbay 				device, ch * 2 + 1, val & 0x1, (val >> 1) & 0x1,
7493e65e175bSOded Gabbay 				(val >> 2) & 0x1, (val >> 3) & 0x1,
7494e65e175bSOded Gabbay 				(val >> 4) & 0x1);
7495e65e175bSOded Gabbay 
7496e65e175bSOded Gabbay 			val2 = RREG32(base + ch * 0x1000 + 0x070);
7497e65e175bSOded Gabbay 			dev_err(hdev->dev,
7498e65e175bSOded Gabbay 				"HBM%d pc%d ECC info: 1ST_ERR_ADDR=0x%x, 1ST_ERR_TYPE=%d, SEC_CONT_CNT=%d, SEC_CNT=%d, DEC_CNT=%d\n",
7499e65e175bSOded Gabbay 				device, ch * 2 + 1,
7500e65e175bSOded Gabbay 				RREG32(base + ch * 0x1000 + 0x074),
7501e65e175bSOded Gabbay 				(val2 & 0x200) >> 9, (val2 & 0xFC00) >> 10,
7502e65e175bSOded Gabbay 				(val2 & 0xFF0000) >> 16,
7503e65e175bSOded Gabbay 				(val2 & 0xFF000000) >> 24);
7504e65e175bSOded Gabbay 		}
7505e65e175bSOded Gabbay 
7506e65e175bSOded Gabbay 		/* Clear interrupts */
7507e65e175bSOded Gabbay 		RMWREG32(base + (ch * 0x1000) + 0x060, 0x1C8, 0x1FF);
7508e65e175bSOded Gabbay 		RMWREG32(base + (ch * 0x1000) + 0x070, 0x1C8, 0x1FF);
7509e65e175bSOded Gabbay 		WREG32(base + (ch * 0x1000) + 0x06C, 0x1F1F);
7510e65e175bSOded Gabbay 		WREG32(base + (ch * 0x1000) + 0x07C, 0x1F1F);
7511e65e175bSOded Gabbay 		RMWREG32(base + (ch * 0x1000) + 0x060, 0x0, 0xF);
7512e65e175bSOded Gabbay 		RMWREG32(base + (ch * 0x1000) + 0x070, 0x0, 0xF);
7513e65e175bSOded Gabbay 	}
7514e65e175bSOded Gabbay 
7515e65e175bSOded Gabbay 	val  = RREG32(base + 0x8F30);
7516e65e175bSOded Gabbay 	val2 = RREG32(base + 0x8F34);
7517e65e175bSOded Gabbay 	if (val | val2) {
7518e65e175bSOded Gabbay 		rc = -EIO;
7519e65e175bSOded Gabbay 		dev_err(hdev->dev,
7520e65e175bSOded Gabbay 			"HBM %d MC SRAM SERR info: Reg 0x8F30=0x%x, Reg 0x8F34=0x%x\n",
7521e65e175bSOded Gabbay 			device, val, val2);
7522e65e175bSOded Gabbay 	}
7523e65e175bSOded Gabbay 	val  = RREG32(base + 0x8F40);
7524e65e175bSOded Gabbay 	val2 = RREG32(base + 0x8F44);
7525e65e175bSOded Gabbay 	if (val | val2) {
7526e65e175bSOded Gabbay 		rc = -EIO;
7527e65e175bSOded Gabbay 		dev_err(hdev->dev,
7528e65e175bSOded Gabbay 			"HBM %d MC SRAM DERR info: Reg 0x8F40=0x%x, Reg 0x8F44=0x%x\n",
7529e65e175bSOded Gabbay 			device, val, val2);
7530e65e175bSOded Gabbay 	}
7531e65e175bSOded Gabbay 
7532e65e175bSOded Gabbay 	return rc;
7533e65e175bSOded Gabbay }
7534e65e175bSOded Gabbay 
7535e65e175bSOded Gabbay static int gaudi_hbm_event_to_dev(u16 hbm_event_type)
7536e65e175bSOded Gabbay {
7537e65e175bSOded Gabbay 	switch (hbm_event_type) {
7538e65e175bSOded Gabbay 	case GAUDI_EVENT_HBM0_SPI_0:
7539e65e175bSOded Gabbay 	case GAUDI_EVENT_HBM0_SPI_1:
7540e65e175bSOded Gabbay 		return 0;
7541e65e175bSOded Gabbay 	case GAUDI_EVENT_HBM1_SPI_0:
7542e65e175bSOded Gabbay 	case GAUDI_EVENT_HBM1_SPI_1:
7543e65e175bSOded Gabbay 		return 1;
7544e65e175bSOded Gabbay 	case GAUDI_EVENT_HBM2_SPI_0:
7545e65e175bSOded Gabbay 	case GAUDI_EVENT_HBM2_SPI_1:
7546e65e175bSOded Gabbay 		return 2;
7547e65e175bSOded Gabbay 	case GAUDI_EVENT_HBM3_SPI_0:
7548e65e175bSOded Gabbay 	case GAUDI_EVENT_HBM3_SPI_1:
7549e65e175bSOded Gabbay 		return 3;
7550e65e175bSOded Gabbay 	default:
7551e65e175bSOded Gabbay 		break;
7552e65e175bSOded Gabbay 	}
7553e65e175bSOded Gabbay 
7554e65e175bSOded Gabbay 	/* Should never happen */
7555e65e175bSOded Gabbay 	return 0;
7556e65e175bSOded Gabbay }
7557e65e175bSOded Gabbay 
7558e65e175bSOded Gabbay static bool gaudi_tpc_read_interrupts(struct hl_device *hdev, u8 tpc_id,
7559e65e175bSOded Gabbay 					char *interrupt_name)
7560e65e175bSOded Gabbay {
7561e65e175bSOded Gabbay 	u32 tpc_offset = tpc_id * TPC_CFG_OFFSET, tpc_interrupts_cause, i;
7562e65e175bSOded Gabbay 	bool soft_reset_required = false;
7563e65e175bSOded Gabbay 
7564e65e175bSOded Gabbay 	tpc_interrupts_cause = RREG32(mmTPC0_CFG_TPC_INTR_CAUSE + tpc_offset) &
7565e65e175bSOded Gabbay 				TPC0_CFG_TPC_INTR_CAUSE_CAUSE_MASK;
7566e65e175bSOded Gabbay 
7567e65e175bSOded Gabbay 	for (i = 0 ; i < GAUDI_NUM_OF_TPC_INTR_CAUSE ; i++)
7568e65e175bSOded Gabbay 		if (tpc_interrupts_cause & BIT(i)) {
7569e65e175bSOded Gabbay 			dev_err_ratelimited(hdev->dev,
7570e65e175bSOded Gabbay 					"TPC%d_%s interrupt cause: %s\n",
7571e65e175bSOded Gabbay 					tpc_id, interrupt_name,
7572e65e175bSOded Gabbay 					gaudi_tpc_interrupts_cause[i]);
7573e65e175bSOded Gabbay 			/* If this is QM error, we need to soft-reset */
7574e65e175bSOded Gabbay 			if (i == 15)
7575e65e175bSOded Gabbay 				soft_reset_required = true;
7576e65e175bSOded Gabbay 		}
7577e65e175bSOded Gabbay 
7578e65e175bSOded Gabbay 	/* Clear interrupts */
7579e65e175bSOded Gabbay 	WREG32(mmTPC0_CFG_TPC_INTR_CAUSE + tpc_offset, 0);
7580e65e175bSOded Gabbay 
7581e65e175bSOded Gabbay 	return soft_reset_required;
7582e65e175bSOded Gabbay }
7583e65e175bSOded Gabbay 
7584e65e175bSOded Gabbay static int tpc_dec_event_to_tpc_id(u16 tpc_dec_event_type)
7585e65e175bSOded Gabbay {
7586e65e175bSOded Gabbay 	return (tpc_dec_event_type - GAUDI_EVENT_TPC0_DEC) >> 1;
7587e65e175bSOded Gabbay }
7588e65e175bSOded Gabbay 
7589e65e175bSOded Gabbay static int tpc_krn_event_to_tpc_id(u16 tpc_dec_event_type)
7590e65e175bSOded Gabbay {
7591e65e175bSOded Gabbay 	return (tpc_dec_event_type - GAUDI_EVENT_TPC0_KRN_ERR) / 6;
7592e65e175bSOded Gabbay }
7593e65e175bSOded Gabbay 
7594e65e175bSOded Gabbay static void gaudi_print_clk_change_info(struct hl_device *hdev, u16 event_type, u64 *event_mask)
7595e65e175bSOded Gabbay {
7596e65e175bSOded Gabbay 	ktime_t zero_time = ktime_set(0, 0);
7597e65e175bSOded Gabbay 
7598e65e175bSOded Gabbay 	mutex_lock(&hdev->clk_throttling.lock);
7599e65e175bSOded Gabbay 
7600e65e175bSOded Gabbay 	switch (event_type) {
7601e65e175bSOded Gabbay 	case GAUDI_EVENT_FIX_POWER_ENV_S:
7602e65e175bSOded Gabbay 		hdev->clk_throttling.current_reason |= HL_CLK_THROTTLE_POWER;
7603e65e175bSOded Gabbay 		hdev->clk_throttling.aggregated_reason |= HL_CLK_THROTTLE_POWER;
7604e65e175bSOded Gabbay 		hdev->clk_throttling.timestamp[HL_CLK_THROTTLE_TYPE_POWER].start = ktime_get();
7605e65e175bSOded Gabbay 		hdev->clk_throttling.timestamp[HL_CLK_THROTTLE_TYPE_POWER].end = zero_time;
7606e65e175bSOded Gabbay 		dev_info_ratelimited(hdev->dev,
7607e65e175bSOded Gabbay 			"Clock throttling due to power consumption\n");
7608e65e175bSOded Gabbay 		break;
7609e65e175bSOded Gabbay 
7610e65e175bSOded Gabbay 	case GAUDI_EVENT_FIX_POWER_ENV_E:
7611e65e175bSOded Gabbay 		hdev->clk_throttling.current_reason &= ~HL_CLK_THROTTLE_POWER;
7612e65e175bSOded Gabbay 		hdev->clk_throttling.timestamp[HL_CLK_THROTTLE_TYPE_POWER].end = ktime_get();
7613e65e175bSOded Gabbay 		dev_info_ratelimited(hdev->dev,
7614e65e175bSOded Gabbay 			"Power envelop is safe, back to optimal clock\n");
7615e65e175bSOded Gabbay 		break;
7616e65e175bSOded Gabbay 
7617e65e175bSOded Gabbay 	case GAUDI_EVENT_FIX_THERMAL_ENV_S:
7618e65e175bSOded Gabbay 		hdev->clk_throttling.current_reason |= HL_CLK_THROTTLE_THERMAL;
7619e65e175bSOded Gabbay 		hdev->clk_throttling.aggregated_reason |= HL_CLK_THROTTLE_THERMAL;
7620e65e175bSOded Gabbay 		hdev->clk_throttling.timestamp[HL_CLK_THROTTLE_TYPE_THERMAL].start = ktime_get();
7621e65e175bSOded Gabbay 		hdev->clk_throttling.timestamp[HL_CLK_THROTTLE_TYPE_THERMAL].end = zero_time;
7622e65e175bSOded Gabbay 		*event_mask |= HL_NOTIFIER_EVENT_USER_ENGINE_ERR;
7623e65e175bSOded Gabbay 		dev_info_ratelimited(hdev->dev,
7624e65e175bSOded Gabbay 			"Clock throttling due to overheating\n");
7625e65e175bSOded Gabbay 		break;
7626e65e175bSOded Gabbay 
7627e65e175bSOded Gabbay 	case GAUDI_EVENT_FIX_THERMAL_ENV_E:
7628e65e175bSOded Gabbay 		hdev->clk_throttling.current_reason &= ~HL_CLK_THROTTLE_THERMAL;
7629e65e175bSOded Gabbay 		hdev->clk_throttling.timestamp[HL_CLK_THROTTLE_TYPE_THERMAL].end = ktime_get();
7630e65e175bSOded Gabbay 		*event_mask |= HL_NOTIFIER_EVENT_USER_ENGINE_ERR;
7631e65e175bSOded Gabbay 		dev_info_ratelimited(hdev->dev,
7632e65e175bSOded Gabbay 			"Thermal envelop is safe, back to optimal clock\n");
7633e65e175bSOded Gabbay 		break;
7634e65e175bSOded Gabbay 
7635e65e175bSOded Gabbay 	default:
7636e65e175bSOded Gabbay 		dev_err(hdev->dev, "Received invalid clock change event %d\n",
7637e65e175bSOded Gabbay 			event_type);
7638e65e175bSOded Gabbay 		break;
7639e65e175bSOded Gabbay 	}
7640e65e175bSOded Gabbay 
7641e65e175bSOded Gabbay 	mutex_unlock(&hdev->clk_throttling.lock);
7642e65e175bSOded Gabbay }
7643e65e175bSOded Gabbay 
7644e65e175bSOded Gabbay static void gaudi_handle_eqe(struct hl_device *hdev, struct hl_eq_entry *eq_entry)
7645e65e175bSOded Gabbay {
7646e65e175bSOded Gabbay 	struct gaudi_device *gaudi = hdev->asic_specific;
7647313e9f63SMoti Haimovski 	struct hl_info_fw_err_info fw_err_info;
7648e65e175bSOded Gabbay 	u64 data = le64_to_cpu(eq_entry->data[0]), event_mask = 0;
7649e65e175bSOded Gabbay 	u32 ctl = le32_to_cpu(eq_entry->hdr.ctl);
7650e65e175bSOded Gabbay 	u32 fw_fatal_err_flag = 0, flags = 0;
7651e65e175bSOded Gabbay 	u16 event_type = ((ctl & EQ_CTL_EVENT_TYPE_MASK)
7652e65e175bSOded Gabbay 			>> EQ_CTL_EVENT_TYPE_SHIFT);
7653e65e175bSOded Gabbay 	bool reset_required, reset_direct = false;
7654e65e175bSOded Gabbay 	u8 cause;
7655e65e175bSOded Gabbay 	int rc;
7656e65e175bSOded Gabbay 
7657e65e175bSOded Gabbay 	if (event_type >= GAUDI_EVENT_SIZE) {
7658e65e175bSOded Gabbay 		dev_err(hdev->dev, "Event type %u exceeds maximum of %u",
7659e65e175bSOded Gabbay 				event_type, GAUDI_EVENT_SIZE - 1);
7660e65e175bSOded Gabbay 		return;
7661e65e175bSOded Gabbay 	}
7662e65e175bSOded Gabbay 
7663e65e175bSOded Gabbay 	gaudi->events_stat[event_type]++;
7664e65e175bSOded Gabbay 	gaudi->events_stat_aggregate[event_type]++;
7665e65e175bSOded Gabbay 
7666e65e175bSOded Gabbay 	switch (event_type) {
7667e65e175bSOded Gabbay 	case GAUDI_EVENT_PCIE_CORE_DERR:
7668e65e175bSOded Gabbay 	case GAUDI_EVENT_PCIE_IF_DERR:
7669e65e175bSOded Gabbay 	case GAUDI_EVENT_PCIE_PHY_DERR:
7670e65e175bSOded Gabbay 	case GAUDI_EVENT_TPC0_DERR ... GAUDI_EVENT_TPC7_DERR:
7671e65e175bSOded Gabbay 	case GAUDI_EVENT_MME0_ACC_DERR:
7672e65e175bSOded Gabbay 	case GAUDI_EVENT_MME0_SBAB_DERR:
7673e65e175bSOded Gabbay 	case GAUDI_EVENT_MME1_ACC_DERR:
7674e65e175bSOded Gabbay 	case GAUDI_EVENT_MME1_SBAB_DERR:
7675e65e175bSOded Gabbay 	case GAUDI_EVENT_MME2_ACC_DERR:
7676e65e175bSOded Gabbay 	case GAUDI_EVENT_MME2_SBAB_DERR:
7677e65e175bSOded Gabbay 	case GAUDI_EVENT_MME3_ACC_DERR:
7678e65e175bSOded Gabbay 	case GAUDI_EVENT_MME3_SBAB_DERR:
7679e65e175bSOded Gabbay 	case GAUDI_EVENT_DMA0_DERR_ECC ... GAUDI_EVENT_DMA7_DERR_ECC:
7680e65e175bSOded Gabbay 		fallthrough;
7681e65e175bSOded Gabbay 	case GAUDI_EVENT_CPU_IF_ECC_DERR:
7682e65e175bSOded Gabbay 	case GAUDI_EVENT_PSOC_MEM_DERR:
7683e65e175bSOded Gabbay 	case GAUDI_EVENT_PSOC_CORESIGHT_DERR:
7684e65e175bSOded Gabbay 	case GAUDI_EVENT_SRAM0_DERR ... GAUDI_EVENT_SRAM28_DERR:
7685e65e175bSOded Gabbay 	case GAUDI_EVENT_NIC0_DERR ... GAUDI_EVENT_NIC4_DERR:
7686e65e175bSOded Gabbay 	case GAUDI_EVENT_DMA_IF0_DERR ... GAUDI_EVENT_DMA_IF3_DERR:
7687e65e175bSOded Gabbay 	case GAUDI_EVENT_HBM_0_DERR ... GAUDI_EVENT_HBM_3_DERR:
7688e65e175bSOded Gabbay 	case GAUDI_EVENT_MMU_DERR:
7689e65e175bSOded Gabbay 	case GAUDI_EVENT_NIC0_CS_DBG_DERR ... GAUDI_EVENT_NIC4_CS_DBG_DERR:
7690e65e175bSOded Gabbay 		gaudi_print_irq_info(hdev, event_type, true, &event_mask);
7691e65e175bSOded Gabbay 		gaudi_handle_ecc_event(hdev, event_type, &eq_entry->ecc_data);
7692e65e175bSOded Gabbay 		event_mask |= HL_NOTIFIER_EVENT_GENERAL_HW_ERR;
7693e65e175bSOded Gabbay 		fw_fatal_err_flag = HL_DRV_RESET_FW_FATAL_ERR;
7694e65e175bSOded Gabbay 		goto reset_device;
7695e65e175bSOded Gabbay 
7696e65e175bSOded Gabbay 	case GAUDI_EVENT_GIC500:
7697e65e175bSOded Gabbay 	case GAUDI_EVENT_AXI_ECC:
7698e65e175bSOded Gabbay 	case GAUDI_EVENT_L2_RAM_ECC:
7699e65e175bSOded Gabbay 	case GAUDI_EVENT_PLL0 ... GAUDI_EVENT_PLL17:
7700e65e175bSOded Gabbay 		gaudi_print_irq_info(hdev, event_type, false, &event_mask);
7701e65e175bSOded Gabbay 		fw_fatal_err_flag = HL_DRV_RESET_FW_FATAL_ERR;
7702e65e175bSOded Gabbay 		event_mask |= HL_NOTIFIER_EVENT_GENERAL_HW_ERR;
7703e65e175bSOded Gabbay 		goto reset_device;
7704e65e175bSOded Gabbay 
7705e65e175bSOded Gabbay 	case GAUDI_EVENT_HBM0_SPI_0:
7706e65e175bSOded Gabbay 	case GAUDI_EVENT_HBM1_SPI_0:
7707e65e175bSOded Gabbay 	case GAUDI_EVENT_HBM2_SPI_0:
7708e65e175bSOded Gabbay 	case GAUDI_EVENT_HBM3_SPI_0:
7709e65e175bSOded Gabbay 		gaudi_print_irq_info(hdev, event_type, false, &event_mask);
7710e65e175bSOded Gabbay 		gaudi_hbm_read_interrupts(hdev,
7711e65e175bSOded Gabbay 				gaudi_hbm_event_to_dev(event_type),
7712e65e175bSOded Gabbay 				&eq_entry->hbm_ecc_data);
7713e65e175bSOded Gabbay 		fw_fatal_err_flag = HL_DRV_RESET_FW_FATAL_ERR;
7714e65e175bSOded Gabbay 		event_mask |= HL_NOTIFIER_EVENT_GENERAL_HW_ERR;
7715e65e175bSOded Gabbay 		goto reset_device;
7716e65e175bSOded Gabbay 
7717e65e175bSOded Gabbay 	case GAUDI_EVENT_HBM0_SPI_1:
7718e65e175bSOded Gabbay 	case GAUDI_EVENT_HBM1_SPI_1:
7719e65e175bSOded Gabbay 	case GAUDI_EVENT_HBM2_SPI_1:
7720e65e175bSOded Gabbay 	case GAUDI_EVENT_HBM3_SPI_1:
7721e65e175bSOded Gabbay 		gaudi_print_irq_info(hdev, event_type, false, &event_mask);
7722e65e175bSOded Gabbay 		gaudi_hbm_read_interrupts(hdev,
7723e65e175bSOded Gabbay 				gaudi_hbm_event_to_dev(event_type),
7724e65e175bSOded Gabbay 				&eq_entry->hbm_ecc_data);
7725e65e175bSOded Gabbay 		hl_fw_unmask_irq(hdev, event_type);
7726e65e175bSOded Gabbay 		event_mask |= HL_NOTIFIER_EVENT_GENERAL_HW_ERR;
7727e65e175bSOded Gabbay 		break;
7728e65e175bSOded Gabbay 
7729e65e175bSOded Gabbay 	case GAUDI_EVENT_TPC0_DEC:
7730e65e175bSOded Gabbay 	case GAUDI_EVENT_TPC1_DEC:
7731e65e175bSOded Gabbay 	case GAUDI_EVENT_TPC2_DEC:
7732e65e175bSOded Gabbay 	case GAUDI_EVENT_TPC3_DEC:
7733e65e175bSOded Gabbay 	case GAUDI_EVENT_TPC4_DEC:
7734e65e175bSOded Gabbay 	case GAUDI_EVENT_TPC5_DEC:
7735e65e175bSOded Gabbay 	case GAUDI_EVENT_TPC6_DEC:
7736e65e175bSOded Gabbay 	case GAUDI_EVENT_TPC7_DEC:
7737e65e175bSOded Gabbay 		/* In TPC DEC event, notify on TPC assertion. While there isn't
7738e65e175bSOded Gabbay 		 * a specific event for assertion yet, the FW generates TPC DEC event.
7739e65e175bSOded Gabbay 		 * The SW upper layer will inspect an internal mapped area to indicate
7740e65e175bSOded Gabbay 		 * if the event is a TPC Assertion or a "real" TPC DEC.
7741e65e175bSOded Gabbay 		 */
7742e65e175bSOded Gabbay 		event_mask |= HL_NOTIFIER_EVENT_TPC_ASSERT;
7743e65e175bSOded Gabbay 		gaudi_print_irq_info(hdev, event_type, true, &event_mask);
7744e65e175bSOded Gabbay 		reset_required = gaudi_tpc_read_interrupts(hdev,
7745e65e175bSOded Gabbay 					tpc_dec_event_to_tpc_id(event_type),
7746e65e175bSOded Gabbay 					"AXI_SLV_DEC_Error");
7747e65e175bSOded Gabbay 		event_mask |= HL_NOTIFIER_EVENT_USER_ENGINE_ERR;
7748e65e175bSOded Gabbay 		if (reset_required) {
7749e65e175bSOded Gabbay 			dev_err(hdev->dev, "reset required due to %s\n",
7750e65e175bSOded Gabbay 				gaudi_irq_map_table[event_type].name);
7751e65e175bSOded Gabbay 
7752e65e175bSOded Gabbay 			reset_direct = true;
7753e65e175bSOded Gabbay 			goto reset_device;
7754e65e175bSOded Gabbay 		} else {
7755e65e175bSOded Gabbay 			hl_fw_unmask_irq(hdev, event_type);
7756e65e175bSOded Gabbay 			event_mask |= HL_NOTIFIER_EVENT_DEVICE_RESET;
7757e65e175bSOded Gabbay 		}
7758e65e175bSOded Gabbay 		break;
7759e65e175bSOded Gabbay 
7760e65e175bSOded Gabbay 	case GAUDI_EVENT_TPC0_KRN_ERR:
7761e65e175bSOded Gabbay 	case GAUDI_EVENT_TPC1_KRN_ERR:
7762e65e175bSOded Gabbay 	case GAUDI_EVENT_TPC2_KRN_ERR:
7763e65e175bSOded Gabbay 	case GAUDI_EVENT_TPC3_KRN_ERR:
7764e65e175bSOded Gabbay 	case GAUDI_EVENT_TPC4_KRN_ERR:
7765e65e175bSOded Gabbay 	case GAUDI_EVENT_TPC5_KRN_ERR:
7766e65e175bSOded Gabbay 	case GAUDI_EVENT_TPC6_KRN_ERR:
7767e65e175bSOded Gabbay 	case GAUDI_EVENT_TPC7_KRN_ERR:
7768e65e175bSOded Gabbay 		gaudi_print_irq_info(hdev, event_type, true, &event_mask);
7769e65e175bSOded Gabbay 		reset_required = gaudi_tpc_read_interrupts(hdev,
7770e65e175bSOded Gabbay 					tpc_krn_event_to_tpc_id(event_type),
7771e65e175bSOded Gabbay 					"KRN_ERR");
7772e65e175bSOded Gabbay 		event_mask |= HL_NOTIFIER_EVENT_USER_ENGINE_ERR;
7773e65e175bSOded Gabbay 		if (reset_required) {
7774e65e175bSOded Gabbay 			dev_err(hdev->dev, "reset required due to %s\n",
7775e65e175bSOded Gabbay 				gaudi_irq_map_table[event_type].name);
7776e65e175bSOded Gabbay 
7777e65e175bSOded Gabbay 			reset_direct = true;
7778e65e175bSOded Gabbay 			goto reset_device;
7779e65e175bSOded Gabbay 		} else {
7780e65e175bSOded Gabbay 			hl_fw_unmask_irq(hdev, event_type);
7781e65e175bSOded Gabbay 			event_mask |= HL_NOTIFIER_EVENT_DEVICE_RESET;
7782e65e175bSOded Gabbay 		}
7783e65e175bSOded Gabbay 		break;
7784e65e175bSOded Gabbay 
7785e65e175bSOded Gabbay 	case GAUDI_EVENT_PCIE_CORE_SERR:
7786e65e175bSOded Gabbay 	case GAUDI_EVENT_PCIE_IF_SERR:
7787e65e175bSOded Gabbay 	case GAUDI_EVENT_PCIE_PHY_SERR:
7788e65e175bSOded Gabbay 	case GAUDI_EVENT_TPC0_SERR ... GAUDI_EVENT_TPC7_SERR:
7789e65e175bSOded Gabbay 	case GAUDI_EVENT_MME0_ACC_SERR:
7790e65e175bSOded Gabbay 	case GAUDI_EVENT_MME0_SBAB_SERR:
7791e65e175bSOded Gabbay 	case GAUDI_EVENT_MME1_ACC_SERR:
7792e65e175bSOded Gabbay 	case GAUDI_EVENT_MME1_SBAB_SERR:
7793e65e175bSOded Gabbay 	case GAUDI_EVENT_MME2_ACC_SERR:
7794e65e175bSOded Gabbay 	case GAUDI_EVENT_MME2_SBAB_SERR:
7795e65e175bSOded Gabbay 	case GAUDI_EVENT_MME3_ACC_SERR:
7796e65e175bSOded Gabbay 	case GAUDI_EVENT_MME3_SBAB_SERR:
7797e65e175bSOded Gabbay 	case GAUDI_EVENT_DMA0_SERR_ECC ... GAUDI_EVENT_DMA7_SERR_ECC:
7798e65e175bSOded Gabbay 	case GAUDI_EVENT_CPU_IF_ECC_SERR:
7799e65e175bSOded Gabbay 	case GAUDI_EVENT_PSOC_MEM_SERR:
7800e65e175bSOded Gabbay 	case GAUDI_EVENT_PSOC_CORESIGHT_SERR:
7801e65e175bSOded Gabbay 	case GAUDI_EVENT_SRAM0_SERR ... GAUDI_EVENT_SRAM28_SERR:
7802e65e175bSOded Gabbay 	case GAUDI_EVENT_NIC0_SERR ... GAUDI_EVENT_NIC4_SERR:
7803e65e175bSOded Gabbay 	case GAUDI_EVENT_DMA_IF0_SERR ... GAUDI_EVENT_DMA_IF3_SERR:
7804e65e175bSOded Gabbay 	case GAUDI_EVENT_HBM_0_SERR ... GAUDI_EVENT_HBM_3_SERR:
7805e65e175bSOded Gabbay 		fallthrough;
7806e65e175bSOded Gabbay 	case GAUDI_EVENT_MMU_SERR:
7807e65e175bSOded Gabbay 		gaudi_print_irq_info(hdev, event_type, true, &event_mask);
7808e65e175bSOded Gabbay 		gaudi_handle_ecc_event(hdev, event_type, &eq_entry->ecc_data);
7809e65e175bSOded Gabbay 		hl_fw_unmask_irq(hdev, event_type);
7810e65e175bSOded Gabbay 		event_mask |= HL_NOTIFIER_EVENT_GENERAL_HW_ERR;
7811e65e175bSOded Gabbay 		break;
7812e65e175bSOded Gabbay 
7813e65e175bSOded Gabbay 	case GAUDI_EVENT_PCIE_DEC:
7814e65e175bSOded Gabbay 	case GAUDI_EVENT_CPU_AXI_SPLITTER:
7815e65e175bSOded Gabbay 	case GAUDI_EVENT_PSOC_AXI_DEC:
7816e65e175bSOded Gabbay 	case GAUDI_EVENT_PSOC_PRSTN_FALL:
7817e65e175bSOded Gabbay 		gaudi_print_irq_info(hdev, event_type, true, &event_mask);
7818e65e175bSOded Gabbay 		hl_fw_unmask_irq(hdev, event_type);
7819e65e175bSOded Gabbay 		event_mask |= HL_NOTIFIER_EVENT_GENERAL_HW_ERR;
7820e65e175bSOded Gabbay 		break;
7821e65e175bSOded Gabbay 
7822e65e175bSOded Gabbay 	case GAUDI_EVENT_MMU_PAGE_FAULT:
7823e65e175bSOded Gabbay 	case GAUDI_EVENT_MMU_WR_PERM:
7824e65e175bSOded Gabbay 		gaudi_print_irq_info(hdev, event_type, true, &event_mask);
7825e65e175bSOded Gabbay 		hl_fw_unmask_irq(hdev, event_type);
7826e65e175bSOded Gabbay 		event_mask |= HL_NOTIFIER_EVENT_USER_ENGINE_ERR;
7827e65e175bSOded Gabbay 		break;
7828e65e175bSOded Gabbay 
7829e65e175bSOded Gabbay 	case GAUDI_EVENT_MME0_WBC_RSP:
7830e65e175bSOded Gabbay 	case GAUDI_EVENT_MME0_SBAB0_RSP:
7831e65e175bSOded Gabbay 	case GAUDI_EVENT_MME1_WBC_RSP:
7832e65e175bSOded Gabbay 	case GAUDI_EVENT_MME1_SBAB0_RSP:
7833e65e175bSOded Gabbay 	case GAUDI_EVENT_MME2_WBC_RSP:
7834e65e175bSOded Gabbay 	case GAUDI_EVENT_MME2_SBAB0_RSP:
7835e65e175bSOded Gabbay 	case GAUDI_EVENT_MME3_WBC_RSP:
7836e65e175bSOded Gabbay 	case GAUDI_EVENT_MME3_SBAB0_RSP:
7837e65e175bSOded Gabbay 	case GAUDI_EVENT_RAZWI_OR_ADC:
7838e65e175bSOded Gabbay 	case GAUDI_EVENT_MME0_QM ... GAUDI_EVENT_MME2_QM:
7839e65e175bSOded Gabbay 	case GAUDI_EVENT_DMA0_QM ... GAUDI_EVENT_DMA7_QM:
7840e65e175bSOded Gabbay 		fallthrough;
7841e65e175bSOded Gabbay 	case GAUDI_EVENT_NIC0_QM0:
7842e65e175bSOded Gabbay 	case GAUDI_EVENT_NIC0_QM1:
7843e65e175bSOded Gabbay 	case GAUDI_EVENT_NIC1_QM0:
7844e65e175bSOded Gabbay 	case GAUDI_EVENT_NIC1_QM1:
7845e65e175bSOded Gabbay 	case GAUDI_EVENT_NIC2_QM0:
7846e65e175bSOded Gabbay 	case GAUDI_EVENT_NIC2_QM1:
7847e65e175bSOded Gabbay 	case GAUDI_EVENT_NIC3_QM0:
7848e65e175bSOded Gabbay 	case GAUDI_EVENT_NIC3_QM1:
7849e65e175bSOded Gabbay 	case GAUDI_EVENT_NIC4_QM0:
7850e65e175bSOded Gabbay 	case GAUDI_EVENT_NIC4_QM1:
7851e65e175bSOded Gabbay 	case GAUDI_EVENT_DMA0_CORE ... GAUDI_EVENT_DMA7_CORE:
7852e65e175bSOded Gabbay 	case GAUDI_EVENT_TPC0_QM ... GAUDI_EVENT_TPC7_QM:
7853e65e175bSOded Gabbay 		gaudi_print_irq_info(hdev, event_type, true, &event_mask);
7854e65e175bSOded Gabbay 		gaudi_handle_qman_err(hdev, event_type, &event_mask);
7855e65e175bSOded Gabbay 		hl_fw_unmask_irq(hdev, event_type);
7856e65e175bSOded Gabbay 		event_mask |= (HL_NOTIFIER_EVENT_USER_ENGINE_ERR | HL_NOTIFIER_EVENT_DEVICE_RESET);
7857e65e175bSOded Gabbay 		break;
7858e65e175bSOded Gabbay 
7859e65e175bSOded Gabbay 	case GAUDI_EVENT_RAZWI_OR_ADC_SW:
7860e65e175bSOded Gabbay 		gaudi_print_irq_info(hdev, event_type, true, &event_mask);
7861e65e175bSOded Gabbay 		event_mask |= HL_NOTIFIER_EVENT_USER_ENGINE_ERR;
7862e65e175bSOded Gabbay 		goto reset_device;
7863e65e175bSOded Gabbay 
7864e65e175bSOded Gabbay 	case GAUDI_EVENT_TPC0_BMON_SPMU:
7865e65e175bSOded Gabbay 	case GAUDI_EVENT_TPC1_BMON_SPMU:
7866e65e175bSOded Gabbay 	case GAUDI_EVENT_TPC2_BMON_SPMU:
7867e65e175bSOded Gabbay 	case GAUDI_EVENT_TPC3_BMON_SPMU:
7868e65e175bSOded Gabbay 	case GAUDI_EVENT_TPC4_BMON_SPMU:
7869e65e175bSOded Gabbay 	case GAUDI_EVENT_TPC5_BMON_SPMU:
7870e65e175bSOded Gabbay 	case GAUDI_EVENT_TPC6_BMON_SPMU:
7871e65e175bSOded Gabbay 	case GAUDI_EVENT_TPC7_BMON_SPMU:
7872e65e175bSOded Gabbay 	case GAUDI_EVENT_DMA_BM_CH0 ... GAUDI_EVENT_DMA_BM_CH7:
7873e65e175bSOded Gabbay 		gaudi_print_irq_info(hdev, event_type, false, &event_mask);
7874e65e175bSOded Gabbay 		hl_fw_unmask_irq(hdev, event_type);
7875e65e175bSOded Gabbay 		event_mask |= HL_NOTIFIER_EVENT_USER_ENGINE_ERR;
7876e65e175bSOded Gabbay 		break;
7877e65e175bSOded Gabbay 
7878e65e175bSOded Gabbay 	case GAUDI_EVENT_NIC_SEI_0 ... GAUDI_EVENT_NIC_SEI_4:
7879e65e175bSOded Gabbay 		gaudi_print_nic_axi_irq_info(hdev, event_type, &data);
7880e65e175bSOded Gabbay 		hl_fw_unmask_irq(hdev, event_type);
7881e65e175bSOded Gabbay 		event_mask |= HL_NOTIFIER_EVENT_USER_ENGINE_ERR;
7882e65e175bSOded Gabbay 		break;
7883e65e175bSOded Gabbay 
7884e65e175bSOded Gabbay 	case GAUDI_EVENT_DMA_IF_SEI_0 ... GAUDI_EVENT_DMA_IF_SEI_3:
7885e65e175bSOded Gabbay 		gaudi_print_irq_info(hdev, event_type, false, &event_mask);
7886e65e175bSOded Gabbay 		gaudi_print_sm_sei_info(hdev, event_type,
7887e65e175bSOded Gabbay 					&eq_entry->sm_sei_data);
7888e65e175bSOded Gabbay 		rc = hl_state_dump(hdev);
7889e65e175bSOded Gabbay 		event_mask |= HL_NOTIFIER_EVENT_USER_ENGINE_ERR;
7890e65e175bSOded Gabbay 		if (rc)
7891e65e175bSOded Gabbay 			dev_err(hdev->dev,
7892e65e175bSOded Gabbay 				"Error during system state dump %d\n", rc);
7893e65e175bSOded Gabbay 		hl_fw_unmask_irq(hdev, event_type);
7894e65e175bSOded Gabbay 		break;
7895e65e175bSOded Gabbay 
7896e65e175bSOded Gabbay 	case GAUDI_EVENT_STATUS_NIC0_ENG0 ... GAUDI_EVENT_STATUS_NIC4_ENG1:
7897e65e175bSOded Gabbay 		break;
7898e65e175bSOded Gabbay 
7899e65e175bSOded Gabbay 	case GAUDI_EVENT_FIX_POWER_ENV_S ... GAUDI_EVENT_FIX_THERMAL_ENV_E:
7900e65e175bSOded Gabbay 		gaudi_print_clk_change_info(hdev, event_type, &event_mask);
7901e65e175bSOded Gabbay 		hl_fw_unmask_irq(hdev, event_type);
7902e65e175bSOded Gabbay 		break;
7903e65e175bSOded Gabbay 
7904e65e175bSOded Gabbay 	case GAUDI_EVENT_PSOC_GPIO_U16_0:
7905e65e175bSOded Gabbay 		cause = le64_to_cpu(eq_entry->data[0]) & 0xFF;
7906e65e175bSOded Gabbay 		dev_err(hdev->dev,
7907e65e175bSOded Gabbay 			"Received high temp H/W interrupt %d (cause %d)\n",
7908e65e175bSOded Gabbay 			event_type, cause);
7909e65e175bSOded Gabbay 		event_mask |= HL_NOTIFIER_EVENT_USER_ENGINE_ERR;
7910e65e175bSOded Gabbay 		break;
7911e65e175bSOded Gabbay 
7912e65e175bSOded Gabbay 	case GAUDI_EVENT_DEV_RESET_REQ:
7913e65e175bSOded Gabbay 		gaudi_print_irq_info(hdev, event_type, false, &event_mask);
7914e65e175bSOded Gabbay 		event_mask |= HL_NOTIFIER_EVENT_GENERAL_HW_ERR;
7915e65e175bSOded Gabbay 		goto reset_device;
7916e65e175bSOded Gabbay 
7917e65e175bSOded Gabbay 	case GAUDI_EVENT_PKT_QUEUE_OUT_SYNC:
7918e65e175bSOded Gabbay 		gaudi_print_irq_info(hdev, event_type, false, &event_mask);
7919e65e175bSOded Gabbay 		gaudi_print_out_of_sync_info(hdev, &eq_entry->pkt_sync_err);
7920e65e175bSOded Gabbay 		event_mask |= HL_NOTIFIER_EVENT_GENERAL_HW_ERR;
7921e65e175bSOded Gabbay 		goto reset_device;
7922e65e175bSOded Gabbay 
7923e65e175bSOded Gabbay 	case GAUDI_EVENT_FW_ALIVE_S:
7924e65e175bSOded Gabbay 		gaudi_print_irq_info(hdev, event_type, false, &event_mask);
7925e65e175bSOded Gabbay 		gaudi_print_fw_alive_info(hdev, &eq_entry->fw_alive);
7926313e9f63SMoti Haimovski 		fw_err_info.err_type = HL_INFO_FW_REPORTED_ERR;
7927313e9f63SMoti Haimovski 		fw_err_info.event_id = event_type;
7928313e9f63SMoti Haimovski 		fw_err_info.event_mask = &event_mask;
7929313e9f63SMoti Haimovski 		hl_handle_fw_err(hdev, &fw_err_info);
7930e65e175bSOded Gabbay 		goto reset_device;
7931e65e175bSOded Gabbay 
7932e65e175bSOded Gabbay 	default:
7933e65e175bSOded Gabbay 		dev_err(hdev->dev, "Received invalid H/W interrupt %d\n",
7934e65e175bSOded Gabbay 				event_type);
7935e65e175bSOded Gabbay 		break;
7936e65e175bSOded Gabbay 	}
7937e65e175bSOded Gabbay 
7938e65e175bSOded Gabbay 	if (event_mask)
7939e65e175bSOded Gabbay 		hl_notifier_event_send_all(hdev, event_mask);
7940e65e175bSOded Gabbay 
7941e65e175bSOded Gabbay 	return;
7942e65e175bSOded Gabbay 
7943e65e175bSOded Gabbay reset_device:
7944e65e175bSOded Gabbay 	reset_required = true;
7945e65e175bSOded Gabbay 
7946e65e175bSOded Gabbay 	if (hdev->asic_prop.fw_security_enabled && !reset_direct) {
7947e65e175bSOded Gabbay 		flags = HL_DRV_RESET_HARD | HL_DRV_RESET_BYPASS_REQ_TO_FW | fw_fatal_err_flag;
7948e65e175bSOded Gabbay 
7949e65e175bSOded Gabbay 		/* notify on device unavailable while the reset triggered by fw */
7950e65e175bSOded Gabbay 		event_mask |= (HL_NOTIFIER_EVENT_DEVICE_RESET |
7951e65e175bSOded Gabbay 					HL_NOTIFIER_EVENT_DEVICE_UNAVAILABLE);
7952e65e175bSOded Gabbay 	} else if (hdev->hard_reset_on_fw_events) {
7953e65e175bSOded Gabbay 		flags = HL_DRV_RESET_HARD | HL_DRV_RESET_DELAY | fw_fatal_err_flag;
7954e65e175bSOded Gabbay 		event_mask |= HL_NOTIFIER_EVENT_DEVICE_RESET;
7955e65e175bSOded Gabbay 	} else {
7956e65e175bSOded Gabbay 		reset_required = false;
7957e65e175bSOded Gabbay 	}
7958e65e175bSOded Gabbay 
7959e65e175bSOded Gabbay 	if (reset_required) {
7960313e9f63SMoti Haimovski 		/* escalate general hw errors to critical/fatal error */
7961313e9f63SMoti Haimovski 		if (event_mask & HL_NOTIFIER_EVENT_GENERAL_HW_ERR)
7962313e9f63SMoti Haimovski 			hl_handle_critical_hw_err(hdev, event_type, &event_mask);
7963313e9f63SMoti Haimovski 
7964e65e175bSOded Gabbay 		hl_device_cond_reset(hdev, flags, event_mask);
7965e65e175bSOded Gabbay 	} else {
7966e65e175bSOded Gabbay 		hl_fw_unmask_irq(hdev, event_type);
7967e65e175bSOded Gabbay 		/* Notification on occurred event needs to be sent although reset is not executed */
7968e65e175bSOded Gabbay 		if (event_mask)
7969e65e175bSOded Gabbay 			hl_notifier_event_send_all(hdev, event_mask);
7970e65e175bSOded Gabbay 	}
7971e65e175bSOded Gabbay }
7972e65e175bSOded Gabbay 
7973e65e175bSOded Gabbay static void *gaudi_get_events_stat(struct hl_device *hdev, bool aggregate, u32 *size)
7974e65e175bSOded Gabbay {
7975e65e175bSOded Gabbay 	struct gaudi_device *gaudi = hdev->asic_specific;
7976e65e175bSOded Gabbay 
7977e65e175bSOded Gabbay 	if (aggregate) {
7978e65e175bSOded Gabbay 		*size = (u32) sizeof(gaudi->events_stat_aggregate);
7979e65e175bSOded Gabbay 		return gaudi->events_stat_aggregate;
7980e65e175bSOded Gabbay 	}
7981e65e175bSOded Gabbay 
7982e65e175bSOded Gabbay 	*size = (u32) sizeof(gaudi->events_stat);
7983e65e175bSOded Gabbay 	return gaudi->events_stat;
7984e65e175bSOded Gabbay }
7985e65e175bSOded Gabbay 
7986e65e175bSOded Gabbay static int gaudi_mmu_invalidate_cache(struct hl_device *hdev, bool is_hard, u32 flags)
7987e65e175bSOded Gabbay {
7988e65e175bSOded Gabbay 	struct gaudi_device *gaudi = hdev->asic_specific;
7989e65e175bSOded Gabbay 	u32 status, timeout_usec;
7990e65e175bSOded Gabbay 	int rc;
7991e65e175bSOded Gabbay 
7992e65e175bSOded Gabbay 	if (!(gaudi->hw_cap_initialized & HW_CAP_MMU) ||
7993e65e175bSOded Gabbay 		hdev->reset_info.hard_reset_pending)
7994e65e175bSOded Gabbay 		return 0;
7995e65e175bSOded Gabbay 
7996e65e175bSOded Gabbay 	if (hdev->pldm)
7997e65e175bSOded Gabbay 		timeout_usec = GAUDI_PLDM_MMU_TIMEOUT_USEC;
7998e65e175bSOded Gabbay 	else
7999e65e175bSOded Gabbay 		timeout_usec = MMU_CONFIG_TIMEOUT_USEC;
8000e65e175bSOded Gabbay 
8001e65e175bSOded Gabbay 	/* L0 & L1 invalidation */
8002e65e175bSOded Gabbay 	WREG32(mmSTLB_INV_PS, 3);
8003e65e175bSOded Gabbay 	WREG32(mmSTLB_CACHE_INV, gaudi->mmu_cache_inv_pi++);
8004e65e175bSOded Gabbay 	WREG32(mmSTLB_INV_PS, 2);
8005e65e175bSOded Gabbay 
8006e65e175bSOded Gabbay 	rc = hl_poll_timeout(
8007e65e175bSOded Gabbay 		hdev,
8008e65e175bSOded Gabbay 		mmSTLB_INV_PS,
8009e65e175bSOded Gabbay 		status,
8010e65e175bSOded Gabbay 		!status,
8011e65e175bSOded Gabbay 		1000,
8012e65e175bSOded Gabbay 		timeout_usec);
8013e65e175bSOded Gabbay 
8014e65e175bSOded Gabbay 	WREG32(mmSTLB_INV_SET, 0);
8015e65e175bSOded Gabbay 
8016e65e175bSOded Gabbay 	return rc;
8017e65e175bSOded Gabbay }
8018e65e175bSOded Gabbay 
8019e65e175bSOded Gabbay static int gaudi_mmu_invalidate_cache_range(struct hl_device *hdev,
8020e65e175bSOded Gabbay 						bool is_hard, u32 flags,
8021e65e175bSOded Gabbay 						u32 asid, u64 va, u64 size)
8022e65e175bSOded Gabbay {
8023e65e175bSOded Gabbay 	/* Treat as invalidate all because there is no range invalidation
8024e65e175bSOded Gabbay 	 * in Gaudi
8025e65e175bSOded Gabbay 	 */
8026e65e175bSOded Gabbay 	return hdev->asic_funcs->mmu_invalidate_cache(hdev, is_hard, flags);
8027e65e175bSOded Gabbay }
8028e65e175bSOded Gabbay 
8029e65e175bSOded Gabbay static int gaudi_mmu_update_asid_hop0_addr(struct hl_device *hdev, u32 asid, u64 phys_addr)
8030e65e175bSOded Gabbay {
8031e65e175bSOded Gabbay 	u32 status, timeout_usec;
8032e65e175bSOded Gabbay 	int rc;
8033e65e175bSOded Gabbay 
8034e65e175bSOded Gabbay 	if (hdev->pldm)
8035e65e175bSOded Gabbay 		timeout_usec = GAUDI_PLDM_MMU_TIMEOUT_USEC;
8036e65e175bSOded Gabbay 	else
8037e65e175bSOded Gabbay 		timeout_usec = MMU_CONFIG_TIMEOUT_USEC;
8038e65e175bSOded Gabbay 
8039e65e175bSOded Gabbay 	WREG32(MMU_ASID, asid);
8040e65e175bSOded Gabbay 	WREG32(MMU_HOP0_PA43_12, phys_addr >> MMU_HOP0_PA43_12_SHIFT);
8041e65e175bSOded Gabbay 	WREG32(MMU_HOP0_PA49_44, phys_addr >> MMU_HOP0_PA49_44_SHIFT);
8042e65e175bSOded Gabbay 	WREG32(MMU_BUSY, 0x80000000);
8043e65e175bSOded Gabbay 
8044e65e175bSOded Gabbay 	rc = hl_poll_timeout(
8045e65e175bSOded Gabbay 		hdev,
8046e65e175bSOded Gabbay 		MMU_BUSY,
8047e65e175bSOded Gabbay 		status,
8048e65e175bSOded Gabbay 		!(status & 0x80000000),
8049e65e175bSOded Gabbay 		1000,
8050e65e175bSOded Gabbay 		timeout_usec);
8051e65e175bSOded Gabbay 
8052e65e175bSOded Gabbay 	if (rc) {
8053e65e175bSOded Gabbay 		dev_err(hdev->dev,
8054e65e175bSOded Gabbay 			"Timeout during MMU hop0 config of asid %d\n", asid);
8055e65e175bSOded Gabbay 		return rc;
8056e65e175bSOded Gabbay 	}
8057e65e175bSOded Gabbay 
8058e65e175bSOded Gabbay 	return 0;
8059e65e175bSOded Gabbay }
8060e65e175bSOded Gabbay 
8061e65e175bSOded Gabbay static int gaudi_send_heartbeat(struct hl_device *hdev)
8062e65e175bSOded Gabbay {
8063e65e175bSOded Gabbay 	struct gaudi_device *gaudi = hdev->asic_specific;
8064e65e175bSOded Gabbay 
8065e65e175bSOded Gabbay 	if (!(gaudi->hw_cap_initialized & HW_CAP_CPU_Q))
8066e65e175bSOded Gabbay 		return 0;
8067e65e175bSOded Gabbay 
8068e65e175bSOded Gabbay 	return hl_fw_send_heartbeat(hdev);
8069e65e175bSOded Gabbay }
8070e65e175bSOded Gabbay 
8071e65e175bSOded Gabbay static int gaudi_cpucp_info_get(struct hl_device *hdev)
8072e65e175bSOded Gabbay {
8073e65e175bSOded Gabbay 	struct gaudi_device *gaudi = hdev->asic_specific;
8074e65e175bSOded Gabbay 	struct asic_fixed_properties *prop = &hdev->asic_prop;
8075e65e175bSOded Gabbay 	int rc;
8076e65e175bSOded Gabbay 
8077e65e175bSOded Gabbay 	if (!(gaudi->hw_cap_initialized & HW_CAP_CPU_Q))
8078e65e175bSOded Gabbay 		return 0;
8079e65e175bSOded Gabbay 
8080e65e175bSOded Gabbay 	rc = hl_fw_cpucp_handshake(hdev, mmCPU_BOOT_DEV_STS0,
8081e65e175bSOded Gabbay 					mmCPU_BOOT_DEV_STS1, mmCPU_BOOT_ERR0,
8082e65e175bSOded Gabbay 					mmCPU_BOOT_ERR1);
8083e65e175bSOded Gabbay 	if (rc)
8084e65e175bSOded Gabbay 		return rc;
8085e65e175bSOded Gabbay 
8086e65e175bSOded Gabbay 	if (!strlen(prop->cpucp_info.card_name))
8087e65e175bSOded Gabbay 		strncpy(prop->cpucp_info.card_name, GAUDI_DEFAULT_CARD_NAME,
8088e65e175bSOded Gabbay 				CARD_NAME_MAX_LEN);
8089e65e175bSOded Gabbay 
8090e65e175bSOded Gabbay 	hdev->card_type = le32_to_cpu(hdev->asic_prop.cpucp_info.card_type);
8091e65e175bSOded Gabbay 
8092e65e175bSOded Gabbay 	set_default_power_values(hdev);
8093e65e175bSOded Gabbay 
8094e65e175bSOded Gabbay 	return 0;
8095e65e175bSOded Gabbay }
8096e65e175bSOded Gabbay 
8097e65e175bSOded Gabbay static bool gaudi_is_device_idle(struct hl_device *hdev, u64 *mask_arr, u8 mask_len,
8098e65e175bSOded Gabbay 		struct engines_data *e)
8099e65e175bSOded Gabbay {
8100e65e175bSOded Gabbay 	struct gaudi_device *gaudi = hdev->asic_specific;
8101e65e175bSOded Gabbay 	const char *fmt = "%-5d%-9s%#-14x%#-12x%#x\n";
8102e65e175bSOded Gabbay 	const char *mme_slave_fmt = "%-5d%-9s%-14s%-12s%#x\n";
8103e65e175bSOded Gabbay 	const char *nic_fmt = "%-5d%-9s%#-14x%#x\n";
8104e65e175bSOded Gabbay 	unsigned long *mask = (unsigned long *)mask_arr;
8105e65e175bSOded Gabbay 	u32 qm_glbl_sts0, qm_cgm_sts, dma_core_sts0, tpc_cfg_sts, mme_arch_sts;
8106e65e175bSOded Gabbay 	bool is_idle = true, is_eng_idle, is_slave;
8107e65e175bSOded Gabbay 	u64 offset;
8108e65e175bSOded Gabbay 	int i, dma_id, port;
8109e65e175bSOded Gabbay 
8110e65e175bSOded Gabbay 	if (e)
8111e65e175bSOded Gabbay 		hl_engine_data_sprintf(e,
8112e65e175bSOded Gabbay 			"\nDMA  is_idle  QM_GLBL_STS0  QM_CGM_STS  DMA_CORE_STS0\n"
8113e65e175bSOded Gabbay 			"---  -------  ------------  ----------  -------------\n");
8114e65e175bSOded Gabbay 
8115e65e175bSOded Gabbay 	for (i = 0 ; i < DMA_NUMBER_OF_CHNLS ; i++) {
8116e65e175bSOded Gabbay 		dma_id = gaudi_dma_assignment[i];
8117e65e175bSOded Gabbay 		offset = dma_id * DMA_QMAN_OFFSET;
8118e65e175bSOded Gabbay 
8119e65e175bSOded Gabbay 		qm_glbl_sts0 = RREG32(mmDMA0_QM_GLBL_STS0 + offset);
8120e65e175bSOded Gabbay 		qm_cgm_sts = RREG32(mmDMA0_QM_CGM_STS + offset);
8121e65e175bSOded Gabbay 		dma_core_sts0 = RREG32(mmDMA0_CORE_STS0 + offset);
8122e65e175bSOded Gabbay 		is_eng_idle = IS_QM_IDLE(qm_glbl_sts0, qm_cgm_sts) &&
8123e65e175bSOded Gabbay 				IS_DMA_IDLE(dma_core_sts0);
8124e65e175bSOded Gabbay 		is_idle &= is_eng_idle;
8125e65e175bSOded Gabbay 
8126e65e175bSOded Gabbay 		if (mask && !is_eng_idle)
8127e65e175bSOded Gabbay 			set_bit(GAUDI_ENGINE_ID_DMA_0 + dma_id, mask);
8128e65e175bSOded Gabbay 		if (e)
8129e65e175bSOded Gabbay 			hl_engine_data_sprintf(e, fmt, dma_id,
8130e65e175bSOded Gabbay 				is_eng_idle ? "Y" : "N", qm_glbl_sts0,
8131e65e175bSOded Gabbay 				qm_cgm_sts, dma_core_sts0);
8132e65e175bSOded Gabbay 	}
8133e65e175bSOded Gabbay 
8134e65e175bSOded Gabbay 	if (e)
8135e65e175bSOded Gabbay 		hl_engine_data_sprintf(e,
8136e65e175bSOded Gabbay 			"\nTPC  is_idle  QM_GLBL_STS0  QM_CGM_STS  CFG_STATUS\n"
8137e65e175bSOded Gabbay 			"---  -------  ------------  ----------  ----------\n");
8138e65e175bSOded Gabbay 
8139e65e175bSOded Gabbay 	for (i = 0 ; i < TPC_NUMBER_OF_ENGINES ; i++) {
8140e65e175bSOded Gabbay 		offset = i * TPC_QMAN_OFFSET;
8141e65e175bSOded Gabbay 		qm_glbl_sts0 = RREG32(mmTPC0_QM_GLBL_STS0 + offset);
8142e65e175bSOded Gabbay 		qm_cgm_sts = RREG32(mmTPC0_QM_CGM_STS + offset);
8143e65e175bSOded Gabbay 		tpc_cfg_sts = RREG32(mmTPC0_CFG_STATUS + offset);
8144e65e175bSOded Gabbay 		is_eng_idle = IS_QM_IDLE(qm_glbl_sts0, qm_cgm_sts) &&
8145e65e175bSOded Gabbay 				IS_TPC_IDLE(tpc_cfg_sts);
8146e65e175bSOded Gabbay 		is_idle &= is_eng_idle;
8147e65e175bSOded Gabbay 
8148e65e175bSOded Gabbay 		if (mask && !is_eng_idle)
8149e65e175bSOded Gabbay 			set_bit(GAUDI_ENGINE_ID_TPC_0 + i, mask);
8150e65e175bSOded Gabbay 		if (e)
8151e65e175bSOded Gabbay 			hl_engine_data_sprintf(e, fmt, i,
8152e65e175bSOded Gabbay 				is_eng_idle ? "Y" : "N",
8153e65e175bSOded Gabbay 				qm_glbl_sts0, qm_cgm_sts, tpc_cfg_sts);
8154e65e175bSOded Gabbay 	}
8155e65e175bSOded Gabbay 
8156e65e175bSOded Gabbay 	if (e)
8157e65e175bSOded Gabbay 		hl_engine_data_sprintf(e,
8158e65e175bSOded Gabbay 			"\nMME  is_idle  QM_GLBL_STS0  QM_CGM_STS  ARCH_STATUS\n"
8159e65e175bSOded Gabbay 			"---  -------  ------------  ----------  -----------\n");
8160e65e175bSOded Gabbay 
8161e65e175bSOded Gabbay 	for (i = 0 ; i < MME_NUMBER_OF_ENGINES ; i++) {
8162e65e175bSOded Gabbay 		offset = i * MME_QMAN_OFFSET;
8163e65e175bSOded Gabbay 		mme_arch_sts = RREG32(mmMME0_CTRL_ARCH_STATUS + offset);
8164e65e175bSOded Gabbay 		is_eng_idle = IS_MME_IDLE(mme_arch_sts);
8165e65e175bSOded Gabbay 
8166e65e175bSOded Gabbay 		/* MME 1 & 3 are slaves, no need to check their QMANs */
8167e65e175bSOded Gabbay 		is_slave = i % 2;
8168e65e175bSOded Gabbay 		if (!is_slave) {
8169e65e175bSOded Gabbay 			qm_glbl_sts0 = RREG32(mmMME0_QM_GLBL_STS0 + offset);
8170e65e175bSOded Gabbay 			qm_cgm_sts = RREG32(mmMME0_QM_CGM_STS + offset);
8171e65e175bSOded Gabbay 			is_eng_idle &= IS_QM_IDLE(qm_glbl_sts0, qm_cgm_sts);
8172e65e175bSOded Gabbay 		}
8173e65e175bSOded Gabbay 
8174e65e175bSOded Gabbay 		is_idle &= is_eng_idle;
8175e65e175bSOded Gabbay 
8176e65e175bSOded Gabbay 		if (mask && !is_eng_idle)
8177e65e175bSOded Gabbay 			set_bit(GAUDI_ENGINE_ID_MME_0 + i, mask);
8178e65e175bSOded Gabbay 		if (e) {
8179e65e175bSOded Gabbay 			if (!is_slave)
8180e65e175bSOded Gabbay 				hl_engine_data_sprintf(e, fmt, i,
8181e65e175bSOded Gabbay 					is_eng_idle ? "Y" : "N",
8182e65e175bSOded Gabbay 					qm_glbl_sts0, qm_cgm_sts, mme_arch_sts);
8183e65e175bSOded Gabbay 			else
8184e65e175bSOded Gabbay 				hl_engine_data_sprintf(e, mme_slave_fmt, i,
8185e65e175bSOded Gabbay 					is_eng_idle ? "Y" : "N", "-",
8186e65e175bSOded Gabbay 					"-", mme_arch_sts);
8187e65e175bSOded Gabbay 		}
8188e65e175bSOded Gabbay 	}
8189e65e175bSOded Gabbay 
8190e65e175bSOded Gabbay 	if (e)
8191e65e175bSOded Gabbay 		hl_engine_data_sprintf(e,
8192e65e175bSOded Gabbay 				"\nNIC  is_idle  QM_GLBL_STS0  QM_CGM_STS\n"
8193e65e175bSOded Gabbay 				"---  -------  ------------  ----------\n");
8194e65e175bSOded Gabbay 
8195e65e175bSOded Gabbay 	for (i = 0 ; i < (NIC_NUMBER_OF_ENGINES / 2) ; i++) {
8196e65e175bSOded Gabbay 		offset = i * NIC_MACRO_QMAN_OFFSET;
8197e65e175bSOded Gabbay 		port = 2 * i;
8198e65e175bSOded Gabbay 		if (gaudi->hw_cap_initialized & BIT(HW_CAP_NIC_SHIFT + port)) {
8199e65e175bSOded Gabbay 			qm_glbl_sts0 = RREG32(mmNIC0_QM0_GLBL_STS0 + offset);
8200e65e175bSOded Gabbay 			qm_cgm_sts = RREG32(mmNIC0_QM0_CGM_STS + offset);
8201e65e175bSOded Gabbay 			is_eng_idle = IS_QM_IDLE(qm_glbl_sts0, qm_cgm_sts);
8202e65e175bSOded Gabbay 			is_idle &= is_eng_idle;
8203e65e175bSOded Gabbay 
8204e65e175bSOded Gabbay 			if (mask && !is_eng_idle)
8205e65e175bSOded Gabbay 				set_bit(GAUDI_ENGINE_ID_NIC_0 + port, mask);
8206e65e175bSOded Gabbay 			if (e)
8207e65e175bSOded Gabbay 				hl_engine_data_sprintf(e, nic_fmt, port,
8208e65e175bSOded Gabbay 						is_eng_idle ? "Y" : "N",
8209e65e175bSOded Gabbay 						qm_glbl_sts0, qm_cgm_sts);
8210e65e175bSOded Gabbay 		}
8211e65e175bSOded Gabbay 
8212e65e175bSOded Gabbay 		port = 2 * i + 1;
8213e65e175bSOded Gabbay 		if (gaudi->hw_cap_initialized & BIT(HW_CAP_NIC_SHIFT + port)) {
8214e65e175bSOded Gabbay 			qm_glbl_sts0 = RREG32(mmNIC0_QM1_GLBL_STS0 + offset);
8215e65e175bSOded Gabbay 			qm_cgm_sts = RREG32(mmNIC0_QM1_CGM_STS + offset);
8216e65e175bSOded Gabbay 			is_eng_idle = IS_QM_IDLE(qm_glbl_sts0, qm_cgm_sts);
8217e65e175bSOded Gabbay 			is_idle &= is_eng_idle;
8218e65e175bSOded Gabbay 
8219e65e175bSOded Gabbay 			if (mask && !is_eng_idle)
8220e65e175bSOded Gabbay 				set_bit(GAUDI_ENGINE_ID_NIC_0 + port, mask);
8221e65e175bSOded Gabbay 			if (e)
8222e65e175bSOded Gabbay 				hl_engine_data_sprintf(e, nic_fmt, port,
8223e65e175bSOded Gabbay 						is_eng_idle ? "Y" : "N",
8224e65e175bSOded Gabbay 						qm_glbl_sts0, qm_cgm_sts);
8225e65e175bSOded Gabbay 		}
8226e65e175bSOded Gabbay 	}
8227e65e175bSOded Gabbay 
8228e65e175bSOded Gabbay 	if (e)
8229e65e175bSOded Gabbay 		hl_engine_data_sprintf(e, "\n");
8230e65e175bSOded Gabbay 
8231e65e175bSOded Gabbay 	return is_idle;
8232e65e175bSOded Gabbay }
8233e65e175bSOded Gabbay 
8234e65e175bSOded Gabbay static void gaudi_hw_queues_lock(struct hl_device *hdev)
8235e65e175bSOded Gabbay 	__acquires(&gaudi->hw_queues_lock)
8236e65e175bSOded Gabbay {
8237e65e175bSOded Gabbay 	struct gaudi_device *gaudi = hdev->asic_specific;
8238e65e175bSOded Gabbay 
8239e65e175bSOded Gabbay 	spin_lock(&gaudi->hw_queues_lock);
8240e65e175bSOded Gabbay }
8241e65e175bSOded Gabbay 
8242e65e175bSOded Gabbay static void gaudi_hw_queues_unlock(struct hl_device *hdev)
8243e65e175bSOded Gabbay 	__releases(&gaudi->hw_queues_lock)
8244e65e175bSOded Gabbay {
8245e65e175bSOded Gabbay 	struct gaudi_device *gaudi = hdev->asic_specific;
8246e65e175bSOded Gabbay 
8247e65e175bSOded Gabbay 	spin_unlock(&gaudi->hw_queues_lock);
8248e65e175bSOded Gabbay }
8249e65e175bSOded Gabbay 
8250e65e175bSOded Gabbay static u32 gaudi_get_pci_id(struct hl_device *hdev)
8251e65e175bSOded Gabbay {
8252e65e175bSOded Gabbay 	return hdev->pdev->device;
8253e65e175bSOded Gabbay }
8254e65e175bSOded Gabbay 
8255e65e175bSOded Gabbay static int gaudi_get_eeprom_data(struct hl_device *hdev, void *data,
8256e65e175bSOded Gabbay 				size_t max_size)
8257e65e175bSOded Gabbay {
8258e65e175bSOded Gabbay 	struct gaudi_device *gaudi = hdev->asic_specific;
8259e65e175bSOded Gabbay 
8260e65e175bSOded Gabbay 	if (!(gaudi->hw_cap_initialized & HW_CAP_CPU_Q))
8261e65e175bSOded Gabbay 		return 0;
8262e65e175bSOded Gabbay 
8263e65e175bSOded Gabbay 	return hl_fw_get_eeprom_data(hdev, data, max_size);
8264e65e175bSOded Gabbay }
8265e65e175bSOded Gabbay 
8266e65e175bSOded Gabbay static int gaudi_get_monitor_dump(struct hl_device *hdev, void *data)
8267e65e175bSOded Gabbay {
8268e65e175bSOded Gabbay 	struct gaudi_device *gaudi = hdev->asic_specific;
8269e65e175bSOded Gabbay 
8270e65e175bSOded Gabbay 	if (!(gaudi->hw_cap_initialized & HW_CAP_CPU_Q))
8271e65e175bSOded Gabbay 		return 0;
8272e65e175bSOded Gabbay 
8273e65e175bSOded Gabbay 	return hl_fw_get_monitor_dump(hdev, data);
8274e65e175bSOded Gabbay }
8275e65e175bSOded Gabbay 
8276e65e175bSOded Gabbay /*
8277e65e175bSOded Gabbay  * this function should be used only during initialization and/or after reset,
8278e65e175bSOded Gabbay  * when there are no active users.
8279e65e175bSOded Gabbay  */
8280e65e175bSOded Gabbay static int gaudi_run_tpc_kernel(struct hl_device *hdev, u64 tpc_kernel,	u32 tpc_id)
8281e65e175bSOded Gabbay {
8282e65e175bSOded Gabbay 	u64 kernel_timeout;
8283e65e175bSOded Gabbay 	u32 status, offset;
8284e65e175bSOded Gabbay 	int rc;
8285e65e175bSOded Gabbay 
8286e65e175bSOded Gabbay 	offset = tpc_id * (mmTPC1_CFG_STATUS - mmTPC0_CFG_STATUS);
8287e65e175bSOded Gabbay 
8288e65e175bSOded Gabbay 	if (hdev->pldm)
8289e65e175bSOded Gabbay 		kernel_timeout = GAUDI_PLDM_TPC_KERNEL_WAIT_USEC;
8290e65e175bSOded Gabbay 	else
8291e65e175bSOded Gabbay 		kernel_timeout = HL_DEVICE_TIMEOUT_USEC;
8292e65e175bSOded Gabbay 
8293e65e175bSOded Gabbay 	WREG32(mmTPC0_CFG_QM_KERNEL_BASE_ADDRESS_LOW + offset,
8294e65e175bSOded Gabbay 			lower_32_bits(tpc_kernel));
8295e65e175bSOded Gabbay 	WREG32(mmTPC0_CFG_QM_KERNEL_BASE_ADDRESS_HIGH + offset,
8296e65e175bSOded Gabbay 			upper_32_bits(tpc_kernel));
8297e65e175bSOded Gabbay 
8298e65e175bSOded Gabbay 	WREG32(mmTPC0_CFG_ICACHE_BASE_ADDERESS_LOW + offset,
8299e65e175bSOded Gabbay 			lower_32_bits(tpc_kernel));
8300e65e175bSOded Gabbay 	WREG32(mmTPC0_CFG_ICACHE_BASE_ADDERESS_HIGH + offset,
8301e65e175bSOded Gabbay 			upper_32_bits(tpc_kernel));
8302e65e175bSOded Gabbay 	/* set a valid LUT pointer, content is of no significance */
8303e65e175bSOded Gabbay 	WREG32(mmTPC0_CFG_LUT_FUNC256_BASE_ADDR_LO + offset,
8304e65e175bSOded Gabbay 			lower_32_bits(tpc_kernel));
8305e65e175bSOded Gabbay 	WREG32(mmTPC0_CFG_LUT_FUNC256_BASE_ADDR_HI + offset,
8306e65e175bSOded Gabbay 			upper_32_bits(tpc_kernel));
8307e65e175bSOded Gabbay 
8308e65e175bSOded Gabbay 	WREG32(mmTPC0_CFG_QM_SYNC_OBJECT_ADDR + offset,
8309e65e175bSOded Gabbay 			lower_32_bits(CFG_BASE +
8310e65e175bSOded Gabbay 				mmSYNC_MNGR_E_N_SYNC_MNGR_OBJS_SOB_OBJ_0));
8311e65e175bSOded Gabbay 
8312e65e175bSOded Gabbay 	WREG32(mmTPC0_CFG_TPC_CMD + offset,
8313e65e175bSOded Gabbay 			(1 << TPC0_CFG_TPC_CMD_ICACHE_INVALIDATE_SHIFT |
8314e65e175bSOded Gabbay 			1 << TPC0_CFG_TPC_CMD_ICACHE_PREFETCH_64KB_SHIFT));
8315e65e175bSOded Gabbay 	/* wait a bit for the engine to start executing */
8316e65e175bSOded Gabbay 	usleep_range(1000, 1500);
8317e65e175bSOded Gabbay 
8318e65e175bSOded Gabbay 	/* wait until engine has finished executing */
8319e65e175bSOded Gabbay 	rc = hl_poll_timeout(
8320e65e175bSOded Gabbay 		hdev,
8321e65e175bSOded Gabbay 		mmTPC0_CFG_STATUS + offset,
8322e65e175bSOded Gabbay 		status,
8323e65e175bSOded Gabbay 		(status & TPC0_CFG_STATUS_VECTOR_PIPE_EMPTY_MASK) ==
8324e65e175bSOded Gabbay 				TPC0_CFG_STATUS_VECTOR_PIPE_EMPTY_MASK,
8325e65e175bSOded Gabbay 		1000,
8326e65e175bSOded Gabbay 		kernel_timeout);
8327e65e175bSOded Gabbay 
8328e65e175bSOded Gabbay 	if (rc) {
8329e65e175bSOded Gabbay 		dev_err(hdev->dev,
8330e65e175bSOded Gabbay 			"Timeout while waiting for TPC%d icache prefetch\n",
8331e65e175bSOded Gabbay 			tpc_id);
8332e65e175bSOded Gabbay 		return -EIO;
8333e65e175bSOded Gabbay 	}
8334e65e175bSOded Gabbay 
8335e65e175bSOded Gabbay 	WREG32(mmTPC0_CFG_TPC_EXECUTE + offset,
8336e65e175bSOded Gabbay 			1 << TPC0_CFG_TPC_EXECUTE_V_SHIFT);
8337e65e175bSOded Gabbay 
8338e65e175bSOded Gabbay 	/* wait a bit for the engine to start executing */
8339e65e175bSOded Gabbay 	usleep_range(1000, 1500);
8340e65e175bSOded Gabbay 
8341e65e175bSOded Gabbay 	/* wait until engine has finished executing */
8342e65e175bSOded Gabbay 	rc = hl_poll_timeout(
8343e65e175bSOded Gabbay 		hdev,
8344e65e175bSOded Gabbay 		mmTPC0_CFG_STATUS + offset,
8345e65e175bSOded Gabbay 		status,
8346e65e175bSOded Gabbay 		(status & TPC0_CFG_STATUS_VECTOR_PIPE_EMPTY_MASK) ==
8347e65e175bSOded Gabbay 				TPC0_CFG_STATUS_VECTOR_PIPE_EMPTY_MASK,
8348e65e175bSOded Gabbay 		1000,
8349e65e175bSOded Gabbay 		kernel_timeout);
8350e65e175bSOded Gabbay 
8351e65e175bSOded Gabbay 	if (rc) {
8352e65e175bSOded Gabbay 		dev_err(hdev->dev,
8353e65e175bSOded Gabbay 			"Timeout while waiting for TPC%d vector pipe\n",
8354e65e175bSOded Gabbay 			tpc_id);
8355e65e175bSOded Gabbay 		return -EIO;
8356e65e175bSOded Gabbay 	}
8357e65e175bSOded Gabbay 
8358e65e175bSOded Gabbay 	rc = hl_poll_timeout(
8359e65e175bSOded Gabbay 		hdev,
8360e65e175bSOded Gabbay 		mmTPC0_CFG_WQ_INFLIGHT_CNTR + offset,
8361e65e175bSOded Gabbay 		status,
8362e65e175bSOded Gabbay 		(status == 0),
8363e65e175bSOded Gabbay 		1000,
8364e65e175bSOded Gabbay 		kernel_timeout);
8365e65e175bSOded Gabbay 
8366e65e175bSOded Gabbay 	if (rc) {
8367e65e175bSOded Gabbay 		dev_err(hdev->dev,
8368e65e175bSOded Gabbay 			"Timeout while waiting for TPC%d kernel to execute\n",
8369e65e175bSOded Gabbay 			tpc_id);
8370e65e175bSOded Gabbay 		return -EIO;
8371e65e175bSOded Gabbay 	}
8372e65e175bSOded Gabbay 
8373e65e175bSOded Gabbay 	return 0;
8374e65e175bSOded Gabbay }
8375e65e175bSOded Gabbay 
8376e65e175bSOded Gabbay static int gaudi_internal_cb_pool_init(struct hl_device *hdev,
8377e65e175bSOded Gabbay 		struct hl_ctx *ctx)
8378e65e175bSOded Gabbay {
8379e65e175bSOded Gabbay 	struct gaudi_device *gaudi = hdev->asic_specific;
8380e65e175bSOded Gabbay 	int min_alloc_order, rc, collective_cb_size;
8381e65e175bSOded Gabbay 
8382e65e175bSOded Gabbay 	if (!(gaudi->hw_cap_initialized & HW_CAP_MMU))
8383e65e175bSOded Gabbay 		return 0;
8384e65e175bSOded Gabbay 
8385e65e175bSOded Gabbay 	hdev->internal_cb_pool_virt_addr = hl_asic_dma_alloc_coherent(hdev,
8386e65e175bSOded Gabbay 							HOST_SPACE_INTERNAL_CB_SZ,
8387e65e175bSOded Gabbay 							&hdev->internal_cb_pool_dma_addr,
8388e65e175bSOded Gabbay 							GFP_KERNEL | __GFP_ZERO);
8389e65e175bSOded Gabbay 
8390e65e175bSOded Gabbay 	if (!hdev->internal_cb_pool_virt_addr)
8391e65e175bSOded Gabbay 		return -ENOMEM;
8392e65e175bSOded Gabbay 
8393e65e175bSOded Gabbay 	collective_cb_size = sizeof(struct packet_msg_short) * 5 +
8394e65e175bSOded Gabbay 			sizeof(struct packet_fence);
8395e65e175bSOded Gabbay 	min_alloc_order = ilog2(collective_cb_size);
8396e65e175bSOded Gabbay 
8397e65e175bSOded Gabbay 	hdev->internal_cb_pool = gen_pool_create(min_alloc_order, -1);
8398e65e175bSOded Gabbay 	if (!hdev->internal_cb_pool) {
8399e65e175bSOded Gabbay 		dev_err(hdev->dev,
8400e65e175bSOded Gabbay 			"Failed to create internal CB pool\n");
8401e65e175bSOded Gabbay 		rc = -ENOMEM;
8402e65e175bSOded Gabbay 		goto free_internal_cb_pool;
8403e65e175bSOded Gabbay 	}
8404e65e175bSOded Gabbay 
8405e65e175bSOded Gabbay 	rc = gen_pool_add(hdev->internal_cb_pool,
8406e65e175bSOded Gabbay 				(uintptr_t) hdev->internal_cb_pool_virt_addr,
8407e65e175bSOded Gabbay 				HOST_SPACE_INTERNAL_CB_SZ, -1);
8408e65e175bSOded Gabbay 	if (rc) {
8409e65e175bSOded Gabbay 		dev_err(hdev->dev,
8410e65e175bSOded Gabbay 			"Failed to add memory to internal CB pool\n");
8411e65e175bSOded Gabbay 		rc = -EFAULT;
8412e65e175bSOded Gabbay 		goto destroy_internal_cb_pool;
8413e65e175bSOded Gabbay 	}
8414e65e175bSOded Gabbay 
8415e65e175bSOded Gabbay 	hdev->internal_cb_va_base = hl_reserve_va_block(hdev, ctx,
8416e65e175bSOded Gabbay 			HL_VA_RANGE_TYPE_HOST, HOST_SPACE_INTERNAL_CB_SZ,
8417e65e175bSOded Gabbay 			HL_MMU_VA_ALIGNMENT_NOT_NEEDED);
8418e65e175bSOded Gabbay 
8419e65e175bSOded Gabbay 	if (!hdev->internal_cb_va_base) {
8420e65e175bSOded Gabbay 		rc = -ENOMEM;
8421e65e175bSOded Gabbay 		goto destroy_internal_cb_pool;
8422e65e175bSOded Gabbay 	}
8423e65e175bSOded Gabbay 
8424e65e175bSOded Gabbay 	mutex_lock(&hdev->mmu_lock);
8425af5e675fSKoby Elbaz 
8426e65e175bSOded Gabbay 	rc = hl_mmu_map_contiguous(ctx, hdev->internal_cb_va_base,
8427e65e175bSOded Gabbay 			hdev->internal_cb_pool_dma_addr,
8428e65e175bSOded Gabbay 			HOST_SPACE_INTERNAL_CB_SZ);
8429e65e175bSOded Gabbay 	if (rc)
8430e65e175bSOded Gabbay 		goto unreserve_internal_cb_pool;
8431e65e175bSOded Gabbay 
8432af5e675fSKoby Elbaz 	rc = hl_mmu_invalidate_cache(hdev, false, MMU_OP_USERPTR);
8433af5e675fSKoby Elbaz 	if (rc)
8434af5e675fSKoby Elbaz 		goto unmap_internal_cb_pool;
8435af5e675fSKoby Elbaz 
8436af5e675fSKoby Elbaz 	mutex_unlock(&hdev->mmu_lock);
8437af5e675fSKoby Elbaz 
8438e65e175bSOded Gabbay 	return 0;
8439e65e175bSOded Gabbay 
8440af5e675fSKoby Elbaz unmap_internal_cb_pool:
8441af5e675fSKoby Elbaz 	hl_mmu_unmap_contiguous(ctx, hdev->internal_cb_va_base,
8442af5e675fSKoby Elbaz 			HOST_SPACE_INTERNAL_CB_SZ);
8443e65e175bSOded Gabbay unreserve_internal_cb_pool:
8444af5e675fSKoby Elbaz 	mutex_unlock(&hdev->mmu_lock);
8445e65e175bSOded Gabbay 	hl_unreserve_va_block(hdev, ctx, hdev->internal_cb_va_base,
8446e65e175bSOded Gabbay 			HOST_SPACE_INTERNAL_CB_SZ);
8447e65e175bSOded Gabbay destroy_internal_cb_pool:
8448e65e175bSOded Gabbay 	gen_pool_destroy(hdev->internal_cb_pool);
8449e65e175bSOded Gabbay free_internal_cb_pool:
8450e65e175bSOded Gabbay 	hl_asic_dma_free_coherent(hdev, HOST_SPACE_INTERNAL_CB_SZ, hdev->internal_cb_pool_virt_addr,
8451e65e175bSOded Gabbay 					hdev->internal_cb_pool_dma_addr);
8452e65e175bSOded Gabbay 
8453e65e175bSOded Gabbay 	return rc;
8454e65e175bSOded Gabbay }
8455e65e175bSOded Gabbay 
8456e65e175bSOded Gabbay static void gaudi_internal_cb_pool_fini(struct hl_device *hdev,
8457e65e175bSOded Gabbay 		struct hl_ctx *ctx)
8458e65e175bSOded Gabbay {
8459e65e175bSOded Gabbay 	struct gaudi_device *gaudi = hdev->asic_specific;
8460e65e175bSOded Gabbay 
8461e65e175bSOded Gabbay 	if (!(gaudi->hw_cap_initialized & HW_CAP_MMU))
8462e65e175bSOded Gabbay 		return;
8463e65e175bSOded Gabbay 
8464e65e175bSOded Gabbay 	mutex_lock(&hdev->mmu_lock);
8465e65e175bSOded Gabbay 	hl_mmu_unmap_contiguous(ctx, hdev->internal_cb_va_base,
8466e65e175bSOded Gabbay 			HOST_SPACE_INTERNAL_CB_SZ);
8467e65e175bSOded Gabbay 	hl_unreserve_va_block(hdev, ctx, hdev->internal_cb_va_base,
8468e65e175bSOded Gabbay 			HOST_SPACE_INTERNAL_CB_SZ);
8469e65e175bSOded Gabbay 	hl_mmu_invalidate_cache(hdev, true, MMU_OP_USERPTR);
8470e65e175bSOded Gabbay 	mutex_unlock(&hdev->mmu_lock);
8471e65e175bSOded Gabbay 
8472e65e175bSOded Gabbay 	gen_pool_destroy(hdev->internal_cb_pool);
8473e65e175bSOded Gabbay 
8474e65e175bSOded Gabbay 	hl_asic_dma_free_coherent(hdev, HOST_SPACE_INTERNAL_CB_SZ, hdev->internal_cb_pool_virt_addr,
8475e65e175bSOded Gabbay 					hdev->internal_cb_pool_dma_addr);
8476e65e175bSOded Gabbay }
8477e65e175bSOded Gabbay 
8478e65e175bSOded Gabbay static int gaudi_ctx_init(struct hl_ctx *ctx)
8479e65e175bSOded Gabbay {
8480e65e175bSOded Gabbay 	int rc;
8481e65e175bSOded Gabbay 
8482e65e175bSOded Gabbay 	if (ctx->asid == HL_KERNEL_ASID_ID)
8483e65e175bSOded Gabbay 		return 0;
8484e65e175bSOded Gabbay 
8485e65e175bSOded Gabbay 	rc = gaudi_internal_cb_pool_init(ctx->hdev, ctx);
8486e65e175bSOded Gabbay 	if (rc)
8487e65e175bSOded Gabbay 		return rc;
8488e65e175bSOded Gabbay 
8489e65e175bSOded Gabbay 	rc = gaudi_restore_user_registers(ctx->hdev);
8490e65e175bSOded Gabbay 	if (rc)
8491e65e175bSOded Gabbay 		gaudi_internal_cb_pool_fini(ctx->hdev, ctx);
8492e65e175bSOded Gabbay 
8493e65e175bSOded Gabbay 	return rc;
8494e65e175bSOded Gabbay }
8495e65e175bSOded Gabbay 
8496e65e175bSOded Gabbay static void gaudi_ctx_fini(struct hl_ctx *ctx)
8497e65e175bSOded Gabbay {
8498e65e175bSOded Gabbay 	if (ctx->asid == HL_KERNEL_ASID_ID)
8499e65e175bSOded Gabbay 		return;
8500e65e175bSOded Gabbay 
8501e65e175bSOded Gabbay 	gaudi_internal_cb_pool_fini(ctx->hdev, ctx);
8502e65e175bSOded Gabbay }
8503e65e175bSOded Gabbay 
8504e65e175bSOded Gabbay static int gaudi_pre_schedule_cs(struct hl_cs *cs)
8505e65e175bSOded Gabbay {
8506e65e175bSOded Gabbay 	return 0;
8507e65e175bSOded Gabbay }
8508e65e175bSOded Gabbay 
8509e65e175bSOded Gabbay static u32 gaudi_get_queue_id_for_cq(struct hl_device *hdev, u32 cq_idx)
8510e65e175bSOded Gabbay {
8511e65e175bSOded Gabbay 	return gaudi_cq_assignment[cq_idx];
8512e65e175bSOded Gabbay }
8513e65e175bSOded Gabbay 
8514e65e175bSOded Gabbay static u32 gaudi_get_signal_cb_size(struct hl_device *hdev)
8515e65e175bSOded Gabbay {
8516e65e175bSOded Gabbay 	return sizeof(struct packet_msg_short) +
8517e65e175bSOded Gabbay 			sizeof(struct packet_msg_prot) * 2;
8518e65e175bSOded Gabbay }
8519e65e175bSOded Gabbay 
8520e65e175bSOded Gabbay static u32 gaudi_get_wait_cb_size(struct hl_device *hdev)
8521e65e175bSOded Gabbay {
8522e65e175bSOded Gabbay 	return sizeof(struct packet_msg_short) * 4 +
8523e65e175bSOded Gabbay 			sizeof(struct packet_fence) +
8524e65e175bSOded Gabbay 			sizeof(struct packet_msg_prot) * 2;
8525e65e175bSOded Gabbay }
8526e65e175bSOded Gabbay 
8527e65e175bSOded Gabbay static u32 gaudi_get_sob_addr(struct hl_device *hdev, u32 sob_id)
8528e65e175bSOded Gabbay {
8529e65e175bSOded Gabbay 	return mmSYNC_MNGR_W_S_SYNC_MNGR_OBJS_SOB_OBJ_0 + (sob_id * 4);
8530e65e175bSOded Gabbay }
8531e65e175bSOded Gabbay 
8532e65e175bSOded Gabbay static u32 gaudi_gen_signal_cb(struct hl_device *hdev, void *data, u16 sob_id,
8533e65e175bSOded Gabbay 				u32 size, bool eb)
8534e65e175bSOded Gabbay {
8535e65e175bSOded Gabbay 	struct hl_cb *cb = (struct hl_cb *) data;
8536e65e175bSOded Gabbay 	struct packet_msg_short *pkt;
8537e65e175bSOded Gabbay 	u32 value, ctl, pkt_size = sizeof(*pkt);
8538e65e175bSOded Gabbay 
8539e65e175bSOded Gabbay 	pkt = cb->kernel_address + size;
8540e65e175bSOded Gabbay 	memset(pkt, 0, pkt_size);
8541e65e175bSOded Gabbay 
8542e65e175bSOded Gabbay 	/* Inc by 1, Mode ADD */
8543e65e175bSOded Gabbay 	value = FIELD_PREP(GAUDI_PKT_SHORT_VAL_SOB_SYNC_VAL_MASK, 1);
8544e65e175bSOded Gabbay 	value |= FIELD_PREP(GAUDI_PKT_SHORT_VAL_SOB_MOD_MASK, 1);
8545e65e175bSOded Gabbay 
8546e65e175bSOded Gabbay 	ctl = FIELD_PREP(GAUDI_PKT_SHORT_CTL_ADDR_MASK, sob_id * 4);
8547e65e175bSOded Gabbay 	ctl |= FIELD_PREP(GAUDI_PKT_SHORT_CTL_OP_MASK, 0); /* write the value */
8548e65e175bSOded Gabbay 	ctl |= FIELD_PREP(GAUDI_PKT_SHORT_CTL_BASE_MASK, 3); /* W_S SOB base */
8549e65e175bSOded Gabbay 	ctl |= FIELD_PREP(GAUDI_PKT_CTL_OPCODE_MASK, PACKET_MSG_SHORT);
8550e65e175bSOded Gabbay 	ctl |= FIELD_PREP(GAUDI_PKT_CTL_EB_MASK, eb);
8551e65e175bSOded Gabbay 	ctl |= FIELD_PREP(GAUDI_PKT_CTL_RB_MASK, 1);
8552e65e175bSOded Gabbay 	ctl |= FIELD_PREP(GAUDI_PKT_CTL_MB_MASK, 1);
8553e65e175bSOded Gabbay 
8554e65e175bSOded Gabbay 	pkt->value = cpu_to_le32(value);
8555e65e175bSOded Gabbay 	pkt->ctl = cpu_to_le32(ctl);
8556e65e175bSOded Gabbay 
8557e65e175bSOded Gabbay 	return size + pkt_size;
8558e65e175bSOded Gabbay }
8559e65e175bSOded Gabbay 
8560e65e175bSOded Gabbay static u32 gaudi_add_mon_msg_short(struct packet_msg_short *pkt, u32 value,
8561e65e175bSOded Gabbay 					u16 addr)
8562e65e175bSOded Gabbay {
8563e65e175bSOded Gabbay 	u32 ctl, pkt_size = sizeof(*pkt);
8564e65e175bSOded Gabbay 
8565e65e175bSOded Gabbay 	memset(pkt, 0, pkt_size);
8566e65e175bSOded Gabbay 
8567e65e175bSOded Gabbay 	ctl = FIELD_PREP(GAUDI_PKT_SHORT_CTL_ADDR_MASK, addr);
8568e65e175bSOded Gabbay 	ctl |= FIELD_PREP(GAUDI_PKT_SHORT_CTL_BASE_MASK, 2);  /* W_S MON base */
8569e65e175bSOded Gabbay 	ctl |= FIELD_PREP(GAUDI_PKT_CTL_OPCODE_MASK, PACKET_MSG_SHORT);
8570e65e175bSOded Gabbay 	ctl |= FIELD_PREP(GAUDI_PKT_CTL_EB_MASK, 0);
8571e65e175bSOded Gabbay 	ctl |= FIELD_PREP(GAUDI_PKT_CTL_RB_MASK, 1);
8572e65e175bSOded Gabbay 	ctl |= FIELD_PREP(GAUDI_PKT_CTL_MB_MASK, 0); /* last pkt MB */
8573e65e175bSOded Gabbay 
8574e65e175bSOded Gabbay 	pkt->value = cpu_to_le32(value);
8575e65e175bSOded Gabbay 	pkt->ctl = cpu_to_le32(ctl);
8576e65e175bSOded Gabbay 
8577e65e175bSOded Gabbay 	return pkt_size;
8578e65e175bSOded Gabbay }
8579e65e175bSOded Gabbay 
8580e65e175bSOded Gabbay static u32 gaudi_add_arm_monitor_pkt(struct hl_device *hdev,
8581e65e175bSOded Gabbay 		struct packet_msg_short *pkt, u16 sob_base, u8 sob_mask,
8582e65e175bSOded Gabbay 		u16 sob_val, u16 mon_id)
8583e65e175bSOded Gabbay {
8584e65e175bSOded Gabbay 	u64 monitor_base;
8585e65e175bSOded Gabbay 	u32 ctl, value, pkt_size = sizeof(*pkt);
8586e65e175bSOded Gabbay 	u16 msg_addr_offset;
8587e65e175bSOded Gabbay 	u8 mask;
8588e65e175bSOded Gabbay 
8589e65e175bSOded Gabbay 	if (hl_gen_sob_mask(sob_base, sob_mask, &mask)) {
8590e65e175bSOded Gabbay 		dev_err(hdev->dev,
8591e65e175bSOded Gabbay 			"sob_base %u (mask %#x) is not valid\n",
8592e65e175bSOded Gabbay 			sob_base, sob_mask);
8593e65e175bSOded Gabbay 		return 0;
8594e65e175bSOded Gabbay 	}
8595e65e175bSOded Gabbay 
8596e65e175bSOded Gabbay 	/*
8597e65e175bSOded Gabbay 	 * monitor_base should be the content of the base0 address registers,
8598e65e175bSOded Gabbay 	 * so it will be added to the msg short offsets
8599e65e175bSOded Gabbay 	 */
8600e65e175bSOded Gabbay 	monitor_base = mmSYNC_MNGR_W_S_SYNC_MNGR_OBJS_MON_PAY_ADDRL_0;
8601e65e175bSOded Gabbay 
8602e65e175bSOded Gabbay 	msg_addr_offset =
8603e65e175bSOded Gabbay 		(mmSYNC_MNGR_W_S_SYNC_MNGR_OBJS_MON_ARM_0 + mon_id * 4) -
8604e65e175bSOded Gabbay 				monitor_base;
8605e65e175bSOded Gabbay 
8606e65e175bSOded Gabbay 	memset(pkt, 0, pkt_size);
8607e65e175bSOded Gabbay 
8608e65e175bSOded Gabbay 	/* Monitor config packet: bind the monitor to a sync object */
8609e65e175bSOded Gabbay 	value = FIELD_PREP(GAUDI_PKT_SHORT_VAL_MON_SYNC_GID_MASK, sob_base / 8);
8610e65e175bSOded Gabbay 	value |= FIELD_PREP(GAUDI_PKT_SHORT_VAL_MON_SYNC_VAL_MASK, sob_val);
8611e65e175bSOded Gabbay 	value |= FIELD_PREP(GAUDI_PKT_SHORT_VAL_MON_MODE_MASK,
8612e65e175bSOded Gabbay 			0); /* GREATER OR EQUAL*/
8613e65e175bSOded Gabbay 	value |= FIELD_PREP(GAUDI_PKT_SHORT_VAL_MON_MASK_MASK, mask);
8614e65e175bSOded Gabbay 
8615e65e175bSOded Gabbay 	ctl = FIELD_PREP(GAUDI_PKT_SHORT_CTL_ADDR_MASK, msg_addr_offset);
8616e65e175bSOded Gabbay 	ctl |= FIELD_PREP(GAUDI_PKT_SHORT_CTL_OP_MASK, 0); /* write the value */
8617e65e175bSOded Gabbay 	ctl |= FIELD_PREP(GAUDI_PKT_SHORT_CTL_BASE_MASK, 2); /* W_S MON base */
8618e65e175bSOded Gabbay 	ctl |= FIELD_PREP(GAUDI_PKT_CTL_OPCODE_MASK, PACKET_MSG_SHORT);
8619e65e175bSOded Gabbay 	ctl |= FIELD_PREP(GAUDI_PKT_CTL_EB_MASK, 0);
8620e65e175bSOded Gabbay 	ctl |= FIELD_PREP(GAUDI_PKT_CTL_RB_MASK, 1);
8621e65e175bSOded Gabbay 	ctl |= FIELD_PREP(GAUDI_PKT_CTL_MB_MASK, 1);
8622e65e175bSOded Gabbay 
8623e65e175bSOded Gabbay 	pkt->value = cpu_to_le32(value);
8624e65e175bSOded Gabbay 	pkt->ctl = cpu_to_le32(ctl);
8625e65e175bSOded Gabbay 
8626e65e175bSOded Gabbay 	return pkt_size;
8627e65e175bSOded Gabbay }
8628e65e175bSOded Gabbay 
8629e65e175bSOded Gabbay static u32 gaudi_add_fence_pkt(struct packet_fence *pkt)
8630e65e175bSOded Gabbay {
8631e65e175bSOded Gabbay 	u32 ctl, cfg, pkt_size = sizeof(*pkt);
8632e65e175bSOded Gabbay 
8633e65e175bSOded Gabbay 	memset(pkt, 0, pkt_size);
8634e65e175bSOded Gabbay 
8635e65e175bSOded Gabbay 	cfg = FIELD_PREP(GAUDI_PKT_FENCE_CFG_DEC_VAL_MASK, 1);
8636e65e175bSOded Gabbay 	cfg |= FIELD_PREP(GAUDI_PKT_FENCE_CFG_TARGET_VAL_MASK, 1);
8637e65e175bSOded Gabbay 	cfg |= FIELD_PREP(GAUDI_PKT_FENCE_CFG_ID_MASK, 2);
8638e65e175bSOded Gabbay 
8639e65e175bSOded Gabbay 	ctl = FIELD_PREP(GAUDI_PKT_CTL_OPCODE_MASK, PACKET_FENCE);
8640e65e175bSOded Gabbay 	ctl |= FIELD_PREP(GAUDI_PKT_CTL_EB_MASK, 0);
8641e65e175bSOded Gabbay 	ctl |= FIELD_PREP(GAUDI_PKT_CTL_RB_MASK, 1);
8642e65e175bSOded Gabbay 	ctl |= FIELD_PREP(GAUDI_PKT_CTL_MB_MASK, 1);
8643e65e175bSOded Gabbay 
8644e65e175bSOded Gabbay 	pkt->cfg = cpu_to_le32(cfg);
8645e65e175bSOded Gabbay 	pkt->ctl = cpu_to_le32(ctl);
8646e65e175bSOded Gabbay 
8647e65e175bSOded Gabbay 	return pkt_size;
8648e65e175bSOded Gabbay }
8649e65e175bSOded Gabbay 
8650e65e175bSOded Gabbay static int gaudi_get_fence_addr(struct hl_device *hdev, u32 queue_id, u64 *addr)
8651e65e175bSOded Gabbay {
8652e65e175bSOded Gabbay 	u32 offset, nic_index;
8653e65e175bSOded Gabbay 
8654e65e175bSOded Gabbay 	switch (queue_id) {
8655e65e175bSOded Gabbay 	case GAUDI_QUEUE_ID_DMA_0_0:
8656e65e175bSOded Gabbay 		offset = mmDMA0_QM_CP_FENCE2_RDATA_0;
8657e65e175bSOded Gabbay 		break;
8658e65e175bSOded Gabbay 	case GAUDI_QUEUE_ID_DMA_0_1:
8659e65e175bSOded Gabbay 		offset = mmDMA0_QM_CP_FENCE2_RDATA_1;
8660e65e175bSOded Gabbay 		break;
8661e65e175bSOded Gabbay 	case GAUDI_QUEUE_ID_DMA_0_2:
8662e65e175bSOded Gabbay 		offset = mmDMA0_QM_CP_FENCE2_RDATA_2;
8663e65e175bSOded Gabbay 		break;
8664e65e175bSOded Gabbay 	case GAUDI_QUEUE_ID_DMA_0_3:
8665e65e175bSOded Gabbay 		offset = mmDMA0_QM_CP_FENCE2_RDATA_3;
8666e65e175bSOded Gabbay 		break;
8667e65e175bSOded Gabbay 	case GAUDI_QUEUE_ID_DMA_1_0:
8668e65e175bSOded Gabbay 		offset = mmDMA1_QM_CP_FENCE2_RDATA_0;
8669e65e175bSOded Gabbay 		break;
8670e65e175bSOded Gabbay 	case GAUDI_QUEUE_ID_DMA_1_1:
8671e65e175bSOded Gabbay 		offset = mmDMA1_QM_CP_FENCE2_RDATA_1;
8672e65e175bSOded Gabbay 		break;
8673e65e175bSOded Gabbay 	case GAUDI_QUEUE_ID_DMA_1_2:
8674e65e175bSOded Gabbay 		offset = mmDMA1_QM_CP_FENCE2_RDATA_2;
8675e65e175bSOded Gabbay 		break;
8676e65e175bSOded Gabbay 	case GAUDI_QUEUE_ID_DMA_1_3:
8677e65e175bSOded Gabbay 		offset = mmDMA1_QM_CP_FENCE2_RDATA_3;
8678e65e175bSOded Gabbay 		break;
8679e65e175bSOded Gabbay 	case GAUDI_QUEUE_ID_DMA_5_0:
8680e65e175bSOded Gabbay 		offset = mmDMA5_QM_CP_FENCE2_RDATA_0;
8681e65e175bSOded Gabbay 		break;
8682e65e175bSOded Gabbay 	case GAUDI_QUEUE_ID_DMA_5_1:
8683e65e175bSOded Gabbay 		offset = mmDMA5_QM_CP_FENCE2_RDATA_1;
8684e65e175bSOded Gabbay 		break;
8685e65e175bSOded Gabbay 	case GAUDI_QUEUE_ID_DMA_5_2:
8686e65e175bSOded Gabbay 		offset = mmDMA5_QM_CP_FENCE2_RDATA_2;
8687e65e175bSOded Gabbay 		break;
8688e65e175bSOded Gabbay 	case GAUDI_QUEUE_ID_DMA_5_3:
8689e65e175bSOded Gabbay 		offset = mmDMA5_QM_CP_FENCE2_RDATA_3;
8690e65e175bSOded Gabbay 		break;
8691e65e175bSOded Gabbay 	case GAUDI_QUEUE_ID_TPC_7_0:
8692e65e175bSOded Gabbay 		offset = mmTPC7_QM_CP_FENCE2_RDATA_0;
8693e65e175bSOded Gabbay 		break;
8694e65e175bSOded Gabbay 	case GAUDI_QUEUE_ID_TPC_7_1:
8695e65e175bSOded Gabbay 		offset = mmTPC7_QM_CP_FENCE2_RDATA_1;
8696e65e175bSOded Gabbay 		break;
8697e65e175bSOded Gabbay 	case GAUDI_QUEUE_ID_TPC_7_2:
8698e65e175bSOded Gabbay 		offset = mmTPC7_QM_CP_FENCE2_RDATA_2;
8699e65e175bSOded Gabbay 		break;
8700e65e175bSOded Gabbay 	case GAUDI_QUEUE_ID_TPC_7_3:
8701e65e175bSOded Gabbay 		offset = mmTPC7_QM_CP_FENCE2_RDATA_3;
8702e65e175bSOded Gabbay 		break;
8703e65e175bSOded Gabbay 	case GAUDI_QUEUE_ID_NIC_0_0:
8704e65e175bSOded Gabbay 	case GAUDI_QUEUE_ID_NIC_1_0:
8705e65e175bSOded Gabbay 	case GAUDI_QUEUE_ID_NIC_2_0:
8706e65e175bSOded Gabbay 	case GAUDI_QUEUE_ID_NIC_3_0:
8707e65e175bSOded Gabbay 	case GAUDI_QUEUE_ID_NIC_4_0:
8708e65e175bSOded Gabbay 	case GAUDI_QUEUE_ID_NIC_5_0:
8709e65e175bSOded Gabbay 	case GAUDI_QUEUE_ID_NIC_6_0:
8710e65e175bSOded Gabbay 	case GAUDI_QUEUE_ID_NIC_7_0:
8711e65e175bSOded Gabbay 	case GAUDI_QUEUE_ID_NIC_8_0:
8712e65e175bSOded Gabbay 	case GAUDI_QUEUE_ID_NIC_9_0:
8713e65e175bSOded Gabbay 		nic_index = (queue_id - GAUDI_QUEUE_ID_NIC_0_0) >> 2;
8714e65e175bSOded Gabbay 		offset = mmNIC0_QM0_CP_FENCE2_RDATA_0 +
8715e65e175bSOded Gabbay 				(nic_index >> 1) * NIC_MACRO_QMAN_OFFSET +
8716e65e175bSOded Gabbay 				(nic_index & 0x1) * NIC_ENGINE_QMAN_OFFSET;
8717e65e175bSOded Gabbay 		break;
8718e65e175bSOded Gabbay 	case GAUDI_QUEUE_ID_NIC_0_1:
8719e65e175bSOded Gabbay 	case GAUDI_QUEUE_ID_NIC_1_1:
8720e65e175bSOded Gabbay 	case GAUDI_QUEUE_ID_NIC_2_1:
8721e65e175bSOded Gabbay 	case GAUDI_QUEUE_ID_NIC_3_1:
8722e65e175bSOded Gabbay 	case GAUDI_QUEUE_ID_NIC_4_1:
8723e65e175bSOded Gabbay 	case GAUDI_QUEUE_ID_NIC_5_1:
8724e65e175bSOded Gabbay 	case GAUDI_QUEUE_ID_NIC_6_1:
8725e65e175bSOded Gabbay 	case GAUDI_QUEUE_ID_NIC_7_1:
8726e65e175bSOded Gabbay 	case GAUDI_QUEUE_ID_NIC_8_1:
8727e65e175bSOded Gabbay 	case GAUDI_QUEUE_ID_NIC_9_1:
8728e65e175bSOded Gabbay 		nic_index = (queue_id - GAUDI_QUEUE_ID_NIC_0_1) >> 2;
8729e65e175bSOded Gabbay 		offset = mmNIC0_QM0_CP_FENCE2_RDATA_1 +
8730e65e175bSOded Gabbay 				(nic_index >> 1) * NIC_MACRO_QMAN_OFFSET +
8731e65e175bSOded Gabbay 				(nic_index & 0x1) * NIC_ENGINE_QMAN_OFFSET;
8732e65e175bSOded Gabbay 		break;
8733e65e175bSOded Gabbay 	case GAUDI_QUEUE_ID_NIC_0_2:
8734e65e175bSOded Gabbay 	case GAUDI_QUEUE_ID_NIC_1_2:
8735e65e175bSOded Gabbay 	case GAUDI_QUEUE_ID_NIC_2_2:
8736e65e175bSOded Gabbay 	case GAUDI_QUEUE_ID_NIC_3_2:
8737e65e175bSOded Gabbay 	case GAUDI_QUEUE_ID_NIC_4_2:
8738e65e175bSOded Gabbay 	case GAUDI_QUEUE_ID_NIC_5_2:
8739e65e175bSOded Gabbay 	case GAUDI_QUEUE_ID_NIC_6_2:
8740e65e175bSOded Gabbay 	case GAUDI_QUEUE_ID_NIC_7_2:
8741e65e175bSOded Gabbay 	case GAUDI_QUEUE_ID_NIC_8_2:
8742e65e175bSOded Gabbay 	case GAUDI_QUEUE_ID_NIC_9_2:
8743e65e175bSOded Gabbay 		nic_index = (queue_id - GAUDI_QUEUE_ID_NIC_0_2) >> 2;
8744e65e175bSOded Gabbay 		offset = mmNIC0_QM0_CP_FENCE2_RDATA_2 +
8745e65e175bSOded Gabbay 				(nic_index >> 1) * NIC_MACRO_QMAN_OFFSET +
8746e65e175bSOded Gabbay 				(nic_index & 0x1) * NIC_ENGINE_QMAN_OFFSET;
8747e65e175bSOded Gabbay 		break;
8748e65e175bSOded Gabbay 	case GAUDI_QUEUE_ID_NIC_0_3:
8749e65e175bSOded Gabbay 	case GAUDI_QUEUE_ID_NIC_1_3:
8750e65e175bSOded Gabbay 	case GAUDI_QUEUE_ID_NIC_2_3:
8751e65e175bSOded Gabbay 	case GAUDI_QUEUE_ID_NIC_3_3:
8752e65e175bSOded Gabbay 	case GAUDI_QUEUE_ID_NIC_4_3:
8753e65e175bSOded Gabbay 	case GAUDI_QUEUE_ID_NIC_5_3:
8754e65e175bSOded Gabbay 	case GAUDI_QUEUE_ID_NIC_6_3:
8755e65e175bSOded Gabbay 	case GAUDI_QUEUE_ID_NIC_7_3:
8756e65e175bSOded Gabbay 	case GAUDI_QUEUE_ID_NIC_8_3:
8757e65e175bSOded Gabbay 	case GAUDI_QUEUE_ID_NIC_9_3:
8758e65e175bSOded Gabbay 		nic_index = (queue_id - GAUDI_QUEUE_ID_NIC_0_3) >> 2;
8759e65e175bSOded Gabbay 		offset = mmNIC0_QM0_CP_FENCE2_RDATA_3 +
8760e65e175bSOded Gabbay 				(nic_index >> 1) * NIC_MACRO_QMAN_OFFSET +
8761e65e175bSOded Gabbay 				(nic_index & 0x1) * NIC_ENGINE_QMAN_OFFSET;
8762e65e175bSOded Gabbay 		break;
8763e65e175bSOded Gabbay 	default:
8764e65e175bSOded Gabbay 		return -EINVAL;
8765e65e175bSOded Gabbay 	}
8766e65e175bSOded Gabbay 
8767e65e175bSOded Gabbay 	*addr = CFG_BASE + offset;
8768e65e175bSOded Gabbay 
8769e65e175bSOded Gabbay 	return 0;
8770e65e175bSOded Gabbay }
8771e65e175bSOded Gabbay 
8772e65e175bSOded Gabbay static u32 gaudi_add_mon_pkts(void *buf, u16 mon_id, u64 fence_addr)
8773e65e175bSOded Gabbay {
8774e65e175bSOded Gabbay 	u64 monitor_base;
8775e65e175bSOded Gabbay 	u32 size = 0;
8776e65e175bSOded Gabbay 	u16 msg_addr_offset;
8777e65e175bSOded Gabbay 
8778e65e175bSOded Gabbay 	/*
8779e65e175bSOded Gabbay 	 * monitor_base should be the content of the base0 address registers,
8780e65e175bSOded Gabbay 	 * so it will be added to the msg short offsets
8781e65e175bSOded Gabbay 	 */
8782e65e175bSOded Gabbay 	monitor_base = mmSYNC_MNGR_W_S_SYNC_MNGR_OBJS_MON_PAY_ADDRL_0;
8783e65e175bSOded Gabbay 
8784e65e175bSOded Gabbay 	/* First monitor config packet: low address of the sync */
8785e65e175bSOded Gabbay 	msg_addr_offset =
8786e65e175bSOded Gabbay 		(mmSYNC_MNGR_W_S_SYNC_MNGR_OBJS_MON_PAY_ADDRL_0 + mon_id * 4) -
8787e65e175bSOded Gabbay 				monitor_base;
8788e65e175bSOded Gabbay 
8789e65e175bSOded Gabbay 	size += gaudi_add_mon_msg_short(buf + size, (u32) fence_addr,
8790e65e175bSOded Gabbay 					msg_addr_offset);
8791e65e175bSOded Gabbay 
8792e65e175bSOded Gabbay 	/* Second monitor config packet: high address of the sync */
8793e65e175bSOded Gabbay 	msg_addr_offset =
8794e65e175bSOded Gabbay 		(mmSYNC_MNGR_W_S_SYNC_MNGR_OBJS_MON_PAY_ADDRH_0 + mon_id * 4) -
8795e65e175bSOded Gabbay 				monitor_base;
8796e65e175bSOded Gabbay 
8797e65e175bSOded Gabbay 	size += gaudi_add_mon_msg_short(buf + size, (u32) (fence_addr >> 32),
8798e65e175bSOded Gabbay 					msg_addr_offset);
8799e65e175bSOded Gabbay 
8800e65e175bSOded Gabbay 	/*
8801e65e175bSOded Gabbay 	 * Third monitor config packet: the payload, i.e. what to write when the
8802e65e175bSOded Gabbay 	 * sync triggers
8803e65e175bSOded Gabbay 	 */
8804e65e175bSOded Gabbay 	msg_addr_offset =
8805e65e175bSOded Gabbay 		(mmSYNC_MNGR_W_S_SYNC_MNGR_OBJS_MON_PAY_DATA_0 + mon_id * 4) -
8806e65e175bSOded Gabbay 				monitor_base;
8807e65e175bSOded Gabbay 
8808e65e175bSOded Gabbay 	size += gaudi_add_mon_msg_short(buf + size, 1, msg_addr_offset);
8809e65e175bSOded Gabbay 
8810e65e175bSOded Gabbay 	return size;
8811e65e175bSOded Gabbay }
8812e65e175bSOded Gabbay 
8813e65e175bSOded Gabbay static u32 gaudi_gen_wait_cb(struct hl_device *hdev,
8814e65e175bSOded Gabbay 				struct hl_gen_wait_properties *prop)
8815e65e175bSOded Gabbay {
8816e65e175bSOded Gabbay 	struct hl_cb *cb = (struct hl_cb *) prop->data;
8817e65e175bSOded Gabbay 	void *buf = cb->kernel_address;
8818e65e175bSOded Gabbay 	u64 fence_addr = 0;
8819e65e175bSOded Gabbay 	u32 size = prop->size;
8820e65e175bSOded Gabbay 
8821e65e175bSOded Gabbay 	if (gaudi_get_fence_addr(hdev, prop->q_idx, &fence_addr)) {
8822e65e175bSOded Gabbay 		dev_crit(hdev->dev, "wrong queue id %d for wait packet\n",
8823e65e175bSOded Gabbay 				prop->q_idx);
8824e65e175bSOded Gabbay 		return 0;
8825e65e175bSOded Gabbay 	}
8826e65e175bSOded Gabbay 
8827e65e175bSOded Gabbay 	size += gaudi_add_mon_pkts(buf + size, prop->mon_id, fence_addr);
8828e65e175bSOded Gabbay 	size += gaudi_add_arm_monitor_pkt(hdev, buf + size, prop->sob_base,
8829e65e175bSOded Gabbay 			prop->sob_mask, prop->sob_val, prop->mon_id);
8830e65e175bSOded Gabbay 	size += gaudi_add_fence_pkt(buf + size);
8831e65e175bSOded Gabbay 
8832e65e175bSOded Gabbay 	return size;
8833e65e175bSOded Gabbay }
8834e65e175bSOded Gabbay 
8835e65e175bSOded Gabbay static void gaudi_reset_sob(struct hl_device *hdev, void *data)
8836e65e175bSOded Gabbay {
8837e65e175bSOded Gabbay 	struct hl_hw_sob *hw_sob = (struct hl_hw_sob *) data;
8838e65e175bSOded Gabbay 
8839e65e175bSOded Gabbay 	dev_dbg(hdev->dev, "reset SOB, q_idx: %d, sob_id: %d\n", hw_sob->q_idx,
8840e65e175bSOded Gabbay 		hw_sob->sob_id);
8841e65e175bSOded Gabbay 
8842e65e175bSOded Gabbay 	WREG32(mmSYNC_MNGR_W_S_SYNC_MNGR_OBJS_SOB_OBJ_0 +
8843e65e175bSOded Gabbay 			hw_sob->sob_id * 4, 0);
8844e65e175bSOded Gabbay 
8845e65e175bSOded Gabbay 	kref_init(&hw_sob->kref);
8846e65e175bSOded Gabbay }
8847e65e175bSOded Gabbay 
8848e65e175bSOded Gabbay static u64 gaudi_get_device_time(struct hl_device *hdev)
8849e65e175bSOded Gabbay {
8850e65e175bSOded Gabbay 	u64 device_time = ((u64) RREG32(mmPSOC_TIMESTAMP_CNTCVU)) << 32;
8851e65e175bSOded Gabbay 
8852e65e175bSOded Gabbay 	return device_time | RREG32(mmPSOC_TIMESTAMP_CNTCVL);
8853e65e175bSOded Gabbay }
8854e65e175bSOded Gabbay 
8855e65e175bSOded Gabbay static int gaudi_get_hw_block_id(struct hl_device *hdev, u64 block_addr,
8856e65e175bSOded Gabbay 				u32 *block_size, u32 *block_id)
8857e65e175bSOded Gabbay {
8858e65e175bSOded Gabbay 	return -EPERM;
8859e65e175bSOded Gabbay }
8860e65e175bSOded Gabbay 
8861e65e175bSOded Gabbay static int gaudi_block_mmap(struct hl_device *hdev,
8862e65e175bSOded Gabbay 				struct vm_area_struct *vma,
8863e65e175bSOded Gabbay 				u32 block_id, u32 block_size)
8864e65e175bSOded Gabbay {
8865e65e175bSOded Gabbay 	return -EPERM;
8866e65e175bSOded Gabbay }
8867e65e175bSOded Gabbay 
8868e65e175bSOded Gabbay static void gaudi_enable_events_from_fw(struct hl_device *hdev)
8869e65e175bSOded Gabbay {
8870e65e175bSOded Gabbay 	struct cpu_dyn_regs *dyn_regs =
8871e65e175bSOded Gabbay 			&hdev->fw_loader.dynamic_loader.comm_desc.cpu_dyn_regs;
8872e65e175bSOded Gabbay 	u32 irq_handler_offset = hdev->asic_prop.gic_interrupts_enable ?
8873e65e175bSOded Gabbay 			mmGIC_DISTRIBUTOR__5_GICD_SETSPI_NSR :
8874e65e175bSOded Gabbay 			le32_to_cpu(dyn_regs->gic_host_ints_irq);
8875e65e175bSOded Gabbay 
8876e65e175bSOded Gabbay 	WREG32(irq_handler_offset,
8877e65e175bSOded Gabbay 		gaudi_irq_map_table[GAUDI_EVENT_INTS_REGISTER].cpu_id);
8878e65e175bSOded Gabbay }
8879e65e175bSOded Gabbay 
8880e65e175bSOded Gabbay static int gaudi_ack_mmu_page_fault_or_access_error(struct hl_device *hdev, u64 mmu_cap_mask)
8881e65e175bSOded Gabbay {
8882e65e175bSOded Gabbay 	return -EINVAL;
8883e65e175bSOded Gabbay }
8884e65e175bSOded Gabbay 
8885e65e175bSOded Gabbay static int gaudi_map_pll_idx_to_fw_idx(u32 pll_idx)
8886e65e175bSOded Gabbay {
8887e65e175bSOded Gabbay 	switch (pll_idx) {
8888e65e175bSOded Gabbay 	case HL_GAUDI_CPU_PLL: return CPU_PLL;
8889e65e175bSOded Gabbay 	case HL_GAUDI_PCI_PLL: return PCI_PLL;
8890e65e175bSOded Gabbay 	case HL_GAUDI_NIC_PLL: return NIC_PLL;
8891e65e175bSOded Gabbay 	case HL_GAUDI_DMA_PLL: return DMA_PLL;
8892e65e175bSOded Gabbay 	case HL_GAUDI_MESH_PLL: return MESH_PLL;
8893e65e175bSOded Gabbay 	case HL_GAUDI_MME_PLL: return MME_PLL;
8894e65e175bSOded Gabbay 	case HL_GAUDI_TPC_PLL: return TPC_PLL;
8895e65e175bSOded Gabbay 	case HL_GAUDI_IF_PLL: return IF_PLL;
8896e65e175bSOded Gabbay 	case HL_GAUDI_SRAM_PLL: return SRAM_PLL;
8897e65e175bSOded Gabbay 	case HL_GAUDI_HBM_PLL: return HBM_PLL;
8898e65e175bSOded Gabbay 	default: return -EINVAL;
8899e65e175bSOded Gabbay 	}
8900e65e175bSOded Gabbay }
8901e65e175bSOded Gabbay 
8902e65e175bSOded Gabbay static int gaudi_add_sync_to_engine_map_entry(
8903e65e175bSOded Gabbay 	struct hl_sync_to_engine_map *map, u32 reg_value,
8904e65e175bSOded Gabbay 	enum hl_sync_engine_type engine_type, u32 engine_id)
8905e65e175bSOded Gabbay {
8906e65e175bSOded Gabbay 	struct hl_sync_to_engine_map_entry *entry;
8907e65e175bSOded Gabbay 
8908e65e175bSOded Gabbay 	/* Reg value represents a partial address of sync object,
8909e65e175bSOded Gabbay 	 * it is used as unique identifier. For this we need to
8910e65e175bSOded Gabbay 	 * clear the cutoff cfg base bits from the value.
8911e65e175bSOded Gabbay 	 */
8912e65e175bSOded Gabbay 	if (reg_value == 0 || reg_value == 0xffffffff)
8913e65e175bSOded Gabbay 		return 0;
8914e65e175bSOded Gabbay 	reg_value -= lower_32_bits(CFG_BASE);
8915e65e175bSOded Gabbay 
8916e65e175bSOded Gabbay 	/* create a new hash entry */
8917e65e175bSOded Gabbay 	entry = kzalloc(sizeof(*entry), GFP_KERNEL);
8918e65e175bSOded Gabbay 	if (!entry)
8919e65e175bSOded Gabbay 		return -ENOMEM;
8920e65e175bSOded Gabbay 	entry->engine_type = engine_type;
8921e65e175bSOded Gabbay 	entry->engine_id = engine_id;
8922e65e175bSOded Gabbay 	entry->sync_id = reg_value;
8923e65e175bSOded Gabbay 	hash_add(map->tb, &entry->node, reg_value);
8924e65e175bSOded Gabbay 
8925e65e175bSOded Gabbay 	return 0;
8926e65e175bSOded Gabbay }
8927e65e175bSOded Gabbay 
8928e65e175bSOded Gabbay static int gaudi_gen_sync_to_engine_map(struct hl_device *hdev,
8929e65e175bSOded Gabbay 				struct hl_sync_to_engine_map *map)
8930e65e175bSOded Gabbay {
8931e65e175bSOded Gabbay 	struct hl_state_dump_specs *sds = &hdev->state_dump_specs;
8932e65e175bSOded Gabbay 	int i, j, rc;
8933e65e175bSOded Gabbay 	u32 reg_value;
8934e65e175bSOded Gabbay 
8935e65e175bSOded Gabbay 	/* Iterate over TPC engines */
8936e65e175bSOded Gabbay 	for (i = 0; i < sds->props[SP_NUM_OF_TPC_ENGINES]; ++i) {
8937e65e175bSOded Gabbay 
8938e65e175bSOded Gabbay 		reg_value = RREG32(sds->props[SP_TPC0_CFG_SO] +
8939e65e175bSOded Gabbay 					sds->props[SP_NEXT_TPC] * i);
8940e65e175bSOded Gabbay 
8941e65e175bSOded Gabbay 		rc = gaudi_add_sync_to_engine_map_entry(map, reg_value,
8942e65e175bSOded Gabbay 							ENGINE_TPC, i);
8943e65e175bSOded Gabbay 		if (rc)
8944e65e175bSOded Gabbay 			goto free_sync_to_engine_map;
8945e65e175bSOded Gabbay 	}
8946e65e175bSOded Gabbay 
8947e65e175bSOded Gabbay 	/* Iterate over MME engines */
8948e65e175bSOded Gabbay 	for (i = 0; i < sds->props[SP_NUM_OF_MME_ENGINES]; ++i) {
8949e65e175bSOded Gabbay 		for (j = 0; j < sds->props[SP_SUB_MME_ENG_NUM]; ++j) {
8950e65e175bSOded Gabbay 
8951e65e175bSOded Gabbay 			reg_value = RREG32(sds->props[SP_MME_CFG_SO] +
8952e65e175bSOded Gabbay 						sds->props[SP_NEXT_MME] * i +
8953e65e175bSOded Gabbay 						j * sizeof(u32));
8954e65e175bSOded Gabbay 
8955e65e175bSOded Gabbay 			rc = gaudi_add_sync_to_engine_map_entry(
8956e65e175bSOded Gabbay 				map, reg_value, ENGINE_MME,
8957e65e175bSOded Gabbay 				i * sds->props[SP_SUB_MME_ENG_NUM] + j);
8958e65e175bSOded Gabbay 			if (rc)
8959e65e175bSOded Gabbay 				goto free_sync_to_engine_map;
8960e65e175bSOded Gabbay 		}
8961e65e175bSOded Gabbay 	}
8962e65e175bSOded Gabbay 
8963e65e175bSOded Gabbay 	/* Iterate over DMA engines */
8964e65e175bSOded Gabbay 	for (i = 0; i < sds->props[SP_NUM_OF_DMA_ENGINES]; ++i) {
8965e65e175bSOded Gabbay 		reg_value = RREG32(sds->props[SP_DMA_CFG_SO] +
8966e65e175bSOded Gabbay 					sds->props[SP_DMA_QUEUES_OFFSET] * i);
8967e65e175bSOded Gabbay 		rc = gaudi_add_sync_to_engine_map_entry(map, reg_value,
8968e65e175bSOded Gabbay 							ENGINE_DMA, i);
8969e65e175bSOded Gabbay 		if (rc)
8970e65e175bSOded Gabbay 			goto free_sync_to_engine_map;
8971e65e175bSOded Gabbay 	}
8972e65e175bSOded Gabbay 
8973e65e175bSOded Gabbay 	return 0;
8974e65e175bSOded Gabbay 
8975e65e175bSOded Gabbay free_sync_to_engine_map:
8976e65e175bSOded Gabbay 	hl_state_dump_free_sync_to_engine_map(map);
8977e65e175bSOded Gabbay 
8978e65e175bSOded Gabbay 	return rc;
8979e65e175bSOded Gabbay }
8980e65e175bSOded Gabbay 
8981e65e175bSOded Gabbay static int gaudi_monitor_valid(struct hl_mon_state_dump *mon)
8982e65e175bSOded Gabbay {
8983e65e175bSOded Gabbay 	return FIELD_GET(
8984e65e175bSOded Gabbay 		SYNC_MNGR_W_S_SYNC_MNGR_OBJS_MON_STATUS_0_VALID_MASK,
8985e65e175bSOded Gabbay 		mon->status);
8986e65e175bSOded Gabbay }
8987e65e175bSOded Gabbay 
8988e65e175bSOded Gabbay static void gaudi_fill_sobs_from_mon(char *sobs, struct hl_mon_state_dump *mon)
8989e65e175bSOded Gabbay {
8990e65e175bSOded Gabbay 	const size_t max_write = 10;
8991e65e175bSOded Gabbay 	u32 gid, mask, sob;
8992e65e175bSOded Gabbay 	int i, offset;
8993e65e175bSOded Gabbay 
8994e65e175bSOded Gabbay 	/* Sync object ID is calculated as follows:
8995e65e175bSOded Gabbay 	 * (8 * group_id + cleared bits in mask)
8996e65e175bSOded Gabbay 	 */
8997e65e175bSOded Gabbay 	gid = FIELD_GET(SYNC_MNGR_W_S_SYNC_MNGR_OBJS_MON_ARM_0_SID_MASK,
8998e65e175bSOded Gabbay 			mon->arm_data);
8999e65e175bSOded Gabbay 	mask = FIELD_GET(SYNC_MNGR_W_S_SYNC_MNGR_OBJS_MON_ARM_0_MASK_MASK,
9000e65e175bSOded Gabbay 			mon->arm_data);
9001e65e175bSOded Gabbay 
9002e65e175bSOded Gabbay 	for (i = 0, offset = 0; mask && offset < MONITOR_SOB_STRING_SIZE -
9003e65e175bSOded Gabbay 		max_write; mask >>= 1, i++) {
9004e65e175bSOded Gabbay 		if (!(mask & 1)) {
9005e65e175bSOded Gabbay 			sob = gid * MONITOR_MAX_SOBS + i;
9006e65e175bSOded Gabbay 
9007e65e175bSOded Gabbay 			if (offset > 0)
9008e65e175bSOded Gabbay 				offset += snprintf(sobs + offset, max_write,
9009e65e175bSOded Gabbay 							", ");
9010e65e175bSOded Gabbay 
9011e65e175bSOded Gabbay 			offset += snprintf(sobs + offset, max_write, "%u", sob);
9012e65e175bSOded Gabbay 		}
9013e65e175bSOded Gabbay 	}
9014e65e175bSOded Gabbay }
9015e65e175bSOded Gabbay 
9016e65e175bSOded Gabbay static int gaudi_print_single_monitor(char **buf, size_t *size, size_t *offset,
9017e65e175bSOded Gabbay 				struct hl_device *hdev,
9018e65e175bSOded Gabbay 				struct hl_mon_state_dump *mon)
9019e65e175bSOded Gabbay {
9020e65e175bSOded Gabbay 	const char *name;
9021e65e175bSOded Gabbay 	char scratch_buf1[BIN_REG_STRING_SIZE],
9022e65e175bSOded Gabbay 		scratch_buf2[BIN_REG_STRING_SIZE];
9023e65e175bSOded Gabbay 	char monitored_sobs[MONITOR_SOB_STRING_SIZE] = {0};
9024e65e175bSOded Gabbay 
9025e65e175bSOded Gabbay 	name = hl_state_dump_get_monitor_name(hdev, mon);
9026e65e175bSOded Gabbay 	if (!name)
9027e65e175bSOded Gabbay 		name = "";
9028e65e175bSOded Gabbay 
9029e65e175bSOded Gabbay 	gaudi_fill_sobs_from_mon(monitored_sobs, mon);
9030e65e175bSOded Gabbay 
9031e65e175bSOded Gabbay 	return hl_snprintf_resize(
9032e65e175bSOded Gabbay 		buf, size, offset,
9033e65e175bSOded Gabbay 		"Mon id: %u%s, wait for group id: %u mask %s to reach val: %u and write %u to address 0x%llx. Pending: %s. Means sync objects [%s] are being monitored.",
9034e65e175bSOded Gabbay 		mon->id, name,
9035e65e175bSOded Gabbay 		FIELD_GET(SYNC_MNGR_W_S_SYNC_MNGR_OBJS_MON_ARM_0_SID_MASK,
9036e65e175bSOded Gabbay 				mon->arm_data),
9037e65e175bSOded Gabbay 		hl_format_as_binary(
9038e65e175bSOded Gabbay 			scratch_buf1, sizeof(scratch_buf1),
9039e65e175bSOded Gabbay 			FIELD_GET(
9040e65e175bSOded Gabbay 				SYNC_MNGR_W_S_SYNC_MNGR_OBJS_MON_ARM_0_MASK_MASK,
9041e65e175bSOded Gabbay 				mon->arm_data)),
9042e65e175bSOded Gabbay 		FIELD_GET(SYNC_MNGR_W_S_SYNC_MNGR_OBJS_MON_ARM_0_SOD_MASK,
9043e65e175bSOded Gabbay 				mon->arm_data),
9044e65e175bSOded Gabbay 		mon->wr_data,
9045e65e175bSOded Gabbay 		(((u64)mon->wr_addr_high) << 32) | mon->wr_addr_low,
9046e65e175bSOded Gabbay 		hl_format_as_binary(
9047e65e175bSOded Gabbay 			scratch_buf2, sizeof(scratch_buf2),
9048e65e175bSOded Gabbay 			FIELD_GET(
9049e65e175bSOded Gabbay 				SYNC_MNGR_W_S_SYNC_MNGR_OBJS_MON_STATUS_0_PENDING_MASK,
9050e65e175bSOded Gabbay 				mon->status)),
9051e65e175bSOded Gabbay 		monitored_sobs);
9052e65e175bSOded Gabbay }
9053e65e175bSOded Gabbay 
9054e65e175bSOded Gabbay 
9055e65e175bSOded Gabbay static int gaudi_print_fences_single_engine(
9056e65e175bSOded Gabbay 	struct hl_device *hdev, u64 base_offset, u64 status_base_offset,
9057e65e175bSOded Gabbay 	enum hl_sync_engine_type engine_type, u32 engine_id, char **buf,
9058e65e175bSOded Gabbay 	size_t *size, size_t *offset)
9059e65e175bSOded Gabbay {
9060e65e175bSOded Gabbay 	struct hl_state_dump_specs *sds = &hdev->state_dump_specs;
9061e65e175bSOded Gabbay 	int rc = -ENOMEM, i;
9062e65e175bSOded Gabbay 	u32 *statuses, *fences;
9063e65e175bSOded Gabbay 
9064e65e175bSOded Gabbay 	statuses = kcalloc(sds->props[SP_ENGINE_NUM_OF_QUEUES],
9065e65e175bSOded Gabbay 			sizeof(*statuses), GFP_KERNEL);
9066e65e175bSOded Gabbay 	if (!statuses)
9067e65e175bSOded Gabbay 		goto out;
9068e65e175bSOded Gabbay 
9069e65e175bSOded Gabbay 	fences = kcalloc(sds->props[SP_ENGINE_NUM_OF_FENCES] *
9070e65e175bSOded Gabbay 				sds->props[SP_ENGINE_NUM_OF_QUEUES],
9071e65e175bSOded Gabbay 			 sizeof(*fences), GFP_KERNEL);
9072e65e175bSOded Gabbay 	if (!fences)
9073e65e175bSOded Gabbay 		goto free_status;
9074e65e175bSOded Gabbay 
9075e65e175bSOded Gabbay 	for (i = 0; i < sds->props[SP_ENGINE_NUM_OF_FENCES]; ++i)
9076e65e175bSOded Gabbay 		statuses[i] = RREG32(status_base_offset + i * sizeof(u32));
9077e65e175bSOded Gabbay 
9078e65e175bSOded Gabbay 	for (i = 0; i < sds->props[SP_ENGINE_NUM_OF_FENCES] *
9079e65e175bSOded Gabbay 				sds->props[SP_ENGINE_NUM_OF_QUEUES]; ++i)
9080e65e175bSOded Gabbay 		fences[i] = RREG32(base_offset + i * sizeof(u32));
9081e65e175bSOded Gabbay 
9082e65e175bSOded Gabbay 	/* The actual print */
9083e65e175bSOded Gabbay 	for (i = 0; i < sds->props[SP_ENGINE_NUM_OF_QUEUES]; ++i) {
9084e65e175bSOded Gabbay 		u32 fence_id;
9085e65e175bSOded Gabbay 		u64 fence_cnt, fence_rdata;
9086e65e175bSOded Gabbay 		const char *engine_name;
9087e65e175bSOded Gabbay 
9088e65e175bSOded Gabbay 		if (!FIELD_GET(TPC0_QM_CP_STS_0_FENCE_IN_PROGRESS_MASK,
9089e65e175bSOded Gabbay 			statuses[i]))
9090e65e175bSOded Gabbay 			continue;
9091e65e175bSOded Gabbay 
9092e65e175bSOded Gabbay 		fence_id =
9093e65e175bSOded Gabbay 			FIELD_GET(TPC0_QM_CP_STS_0_FENCE_ID_MASK, statuses[i]);
9094e65e175bSOded Gabbay 		fence_cnt = base_offset + CFG_BASE +
9095e65e175bSOded Gabbay 			sizeof(u32) *
9096e65e175bSOded Gabbay 			(i + fence_id * sds->props[SP_ENGINE_NUM_OF_QUEUES]);
9097e65e175bSOded Gabbay 		fence_rdata = fence_cnt - sds->props[SP_FENCE0_CNT_OFFSET] +
9098e65e175bSOded Gabbay 				sds->props[SP_FENCE0_RDATA_OFFSET];
9099e65e175bSOded Gabbay 		engine_name = hl_sync_engine_to_string(engine_type);
9100e65e175bSOded Gabbay 
9101e65e175bSOded Gabbay 		rc = hl_snprintf_resize(
9102e65e175bSOded Gabbay 			buf, size, offset,
9103e65e175bSOded Gabbay 			"%s%u, stream %u: fence id %u cnt = 0x%llx (%s%u_QM.CP_FENCE%u_CNT_%u) rdata = 0x%llx (%s%u_QM.CP_FENCE%u_RDATA_%u) value = %u, cp_status = %u\n",
9104e65e175bSOded Gabbay 			engine_name, engine_id,
9105e65e175bSOded Gabbay 			i, fence_id,
9106e65e175bSOded Gabbay 			fence_cnt, engine_name, engine_id, fence_id, i,
9107e65e175bSOded Gabbay 			fence_rdata, engine_name, engine_id, fence_id, i,
9108e65e175bSOded Gabbay 			fences[fence_id],
9109e65e175bSOded Gabbay 			statuses[i]);
9110e65e175bSOded Gabbay 		if (rc)
9111e65e175bSOded Gabbay 			goto free_fences;
9112e65e175bSOded Gabbay 	}
9113e65e175bSOded Gabbay 
9114e65e175bSOded Gabbay 	rc = 0;
9115e65e175bSOded Gabbay 
9116e65e175bSOded Gabbay free_fences:
9117e65e175bSOded Gabbay 	kfree(fences);
9118e65e175bSOded Gabbay free_status:
9119e65e175bSOded Gabbay 	kfree(statuses);
9120e65e175bSOded Gabbay out:
9121e65e175bSOded Gabbay 	return rc;
9122e65e175bSOded Gabbay }
9123e65e175bSOded Gabbay 
9124e65e175bSOded Gabbay 
9125e65e175bSOded Gabbay static struct hl_state_dump_specs_funcs gaudi_state_dump_funcs = {
9126e65e175bSOded Gabbay 	.monitor_valid = gaudi_monitor_valid,
9127e65e175bSOded Gabbay 	.print_single_monitor = gaudi_print_single_monitor,
9128e65e175bSOded Gabbay 	.gen_sync_to_engine_map = gaudi_gen_sync_to_engine_map,
9129e65e175bSOded Gabbay 	.print_fences_single_engine = gaudi_print_fences_single_engine,
9130e65e175bSOded Gabbay };
9131e65e175bSOded Gabbay 
9132e65e175bSOded Gabbay static void gaudi_state_dump_init(struct hl_device *hdev)
9133e65e175bSOded Gabbay {
9134e65e175bSOded Gabbay 	struct hl_state_dump_specs *sds = &hdev->state_dump_specs;
9135e65e175bSOded Gabbay 	int i;
9136e65e175bSOded Gabbay 
9137e65e175bSOded Gabbay 	for (i = 0; i < ARRAY_SIZE(gaudi_so_id_to_str); ++i)
9138e65e175bSOded Gabbay 		hash_add(sds->so_id_to_str_tb,
9139e65e175bSOded Gabbay 			&gaudi_so_id_to_str[i].node,
9140e65e175bSOded Gabbay 			gaudi_so_id_to_str[i].id);
9141e65e175bSOded Gabbay 
9142e65e175bSOded Gabbay 	for (i = 0; i < ARRAY_SIZE(gaudi_monitor_id_to_str); ++i)
9143e65e175bSOded Gabbay 		hash_add(sds->monitor_id_to_str_tb,
9144e65e175bSOded Gabbay 			&gaudi_monitor_id_to_str[i].node,
9145e65e175bSOded Gabbay 			gaudi_monitor_id_to_str[i].id);
9146e65e175bSOded Gabbay 
9147e65e175bSOded Gabbay 	sds->props = gaudi_state_dump_specs_props;
9148e65e175bSOded Gabbay 
9149e65e175bSOded Gabbay 	sds->sync_namager_names = gaudi_sync_manager_names;
9150e65e175bSOded Gabbay 
9151e65e175bSOded Gabbay 	sds->funcs = gaudi_state_dump_funcs;
9152e65e175bSOded Gabbay }
9153e65e175bSOded Gabbay 
9154e65e175bSOded Gabbay static u32 *gaudi_get_stream_master_qid_arr(void)
9155e65e175bSOded Gabbay {
9156e65e175bSOded Gabbay 	return gaudi_stream_master;
9157e65e175bSOded Gabbay }
9158e65e175bSOded Gabbay 
9159e65e175bSOded Gabbay static int gaudi_set_dram_properties(struct hl_device *hdev)
9160e65e175bSOded Gabbay {
9161e65e175bSOded Gabbay 	return 0;
9162e65e175bSOded Gabbay }
9163e65e175bSOded Gabbay 
9164ab509d81SOhad Sharabi static int gaudi_set_binning_masks(struct hl_device *hdev)
9165ab509d81SOhad Sharabi {
9166ab509d81SOhad Sharabi 	return 0;
9167ab509d81SOhad Sharabi }
9168ab509d81SOhad Sharabi 
9169e65e175bSOded Gabbay static void gaudi_check_if_razwi_happened(struct hl_device *hdev)
9170e65e175bSOded Gabbay {
9171e65e175bSOded Gabbay }
9172e65e175bSOded Gabbay 
9173e65e175bSOded Gabbay static ssize_t infineon_ver_show(struct device *dev, struct device_attribute *attr, char *buf)
9174e65e175bSOded Gabbay {
9175e65e175bSOded Gabbay 	struct hl_device *hdev = dev_get_drvdata(dev);
9176e65e175bSOded Gabbay 	struct cpucp_info *cpucp_info;
9177e65e175bSOded Gabbay 
9178e65e175bSOded Gabbay 	cpucp_info = &hdev->asic_prop.cpucp_info;
9179e65e175bSOded Gabbay 
9180e65e175bSOded Gabbay 	return sprintf(buf, "%#04x\n", le32_to_cpu(cpucp_info->infineon_version));
9181e65e175bSOded Gabbay }
9182e65e175bSOded Gabbay 
9183e65e175bSOded Gabbay static DEVICE_ATTR_RO(infineon_ver);
9184e65e175bSOded Gabbay 
9185e65e175bSOded Gabbay static struct attribute *gaudi_vrm_dev_attrs[] = {
9186e65e175bSOded Gabbay 	&dev_attr_infineon_ver.attr,
9187e65e175bSOded Gabbay 	NULL,
9188e65e175bSOded Gabbay };
9189e65e175bSOded Gabbay 
9190e65e175bSOded Gabbay static void gaudi_add_device_attr(struct hl_device *hdev, struct attribute_group *dev_clk_attr_grp,
9191e65e175bSOded Gabbay 					struct attribute_group *dev_vrm_attr_grp)
9192e65e175bSOded Gabbay {
9193e65e175bSOded Gabbay 	hl_sysfs_add_dev_clk_attr(hdev, dev_clk_attr_grp);
9194e65e175bSOded Gabbay 	dev_vrm_attr_grp->attrs = gaudi_vrm_dev_attrs;
9195e65e175bSOded Gabbay }
9196e65e175bSOded Gabbay 
9197e65e175bSOded Gabbay static int gaudi_send_device_activity(struct hl_device *hdev, bool open)
9198e65e175bSOded Gabbay {
9199e65e175bSOded Gabbay 	return 0;
9200e65e175bSOded Gabbay }
9201e65e175bSOded Gabbay 
9202e65e175bSOded Gabbay static const struct hl_asic_funcs gaudi_funcs = {
9203e65e175bSOded Gabbay 	.early_init = gaudi_early_init,
9204e65e175bSOded Gabbay 	.early_fini = gaudi_early_fini,
9205e65e175bSOded Gabbay 	.late_init = gaudi_late_init,
9206e65e175bSOded Gabbay 	.late_fini = gaudi_late_fini,
9207e65e175bSOded Gabbay 	.sw_init = gaudi_sw_init,
9208e65e175bSOded Gabbay 	.sw_fini = gaudi_sw_fini,
9209e65e175bSOded Gabbay 	.hw_init = gaudi_hw_init,
9210e65e175bSOded Gabbay 	.hw_fini = gaudi_hw_fini,
9211e65e175bSOded Gabbay 	.halt_engines = gaudi_halt_engines,
9212e65e175bSOded Gabbay 	.suspend = gaudi_suspend,
9213e65e175bSOded Gabbay 	.resume = gaudi_resume,
9214e65e175bSOded Gabbay 	.mmap = gaudi_mmap,
9215e65e175bSOded Gabbay 	.ring_doorbell = gaudi_ring_doorbell,
9216e65e175bSOded Gabbay 	.pqe_write = gaudi_pqe_write,
9217e65e175bSOded Gabbay 	.asic_dma_alloc_coherent = gaudi_dma_alloc_coherent,
9218e65e175bSOded Gabbay 	.asic_dma_free_coherent = gaudi_dma_free_coherent,
9219e65e175bSOded Gabbay 	.scrub_device_mem = gaudi_scrub_device_mem,
9220e65e175bSOded Gabbay 	.scrub_device_dram = gaudi_scrub_device_dram,
9221e65e175bSOded Gabbay 	.get_int_queue_base = gaudi_get_int_queue_base,
9222e65e175bSOded Gabbay 	.test_queues = gaudi_test_queues,
9223e65e175bSOded Gabbay 	.asic_dma_pool_zalloc = gaudi_dma_pool_zalloc,
9224e65e175bSOded Gabbay 	.asic_dma_pool_free = gaudi_dma_pool_free,
9225e65e175bSOded Gabbay 	.cpu_accessible_dma_pool_alloc = gaudi_cpu_accessible_dma_pool_alloc,
9226e65e175bSOded Gabbay 	.cpu_accessible_dma_pool_free = gaudi_cpu_accessible_dma_pool_free,
9227e65e175bSOded Gabbay 	.hl_dma_unmap_sgtable = hl_dma_unmap_sgtable,
9228e65e175bSOded Gabbay 	.cs_parser = gaudi_cs_parser,
9229e65e175bSOded Gabbay 	.asic_dma_map_sgtable = hl_dma_map_sgtable,
9230e65e175bSOded Gabbay 	.add_end_of_cb_packets = gaudi_add_end_of_cb_packets,
9231e65e175bSOded Gabbay 	.update_eq_ci = gaudi_update_eq_ci,
9232e65e175bSOded Gabbay 	.context_switch = gaudi_context_switch,
9233e65e175bSOded Gabbay 	.restore_phase_topology = gaudi_restore_phase_topology,
9234e65e175bSOded Gabbay 	.debugfs_read_dma = gaudi_debugfs_read_dma,
9235e65e175bSOded Gabbay 	.add_device_attr = gaudi_add_device_attr,
9236e65e175bSOded Gabbay 	.handle_eqe = gaudi_handle_eqe,
9237e65e175bSOded Gabbay 	.get_events_stat = gaudi_get_events_stat,
9238e65e175bSOded Gabbay 	.read_pte = gaudi_read_pte,
9239e65e175bSOded Gabbay 	.write_pte = gaudi_write_pte,
9240e65e175bSOded Gabbay 	.mmu_invalidate_cache = gaudi_mmu_invalidate_cache,
9241e65e175bSOded Gabbay 	.mmu_invalidate_cache_range = gaudi_mmu_invalidate_cache_range,
9242e65e175bSOded Gabbay 	.mmu_prefetch_cache_range = NULL,
9243e65e175bSOded Gabbay 	.send_heartbeat = gaudi_send_heartbeat,
9244e65e175bSOded Gabbay 	.debug_coresight = gaudi_debug_coresight,
9245e65e175bSOded Gabbay 	.is_device_idle = gaudi_is_device_idle,
9246e65e175bSOded Gabbay 	.compute_reset_late_init = gaudi_compute_reset_late_init,
9247e65e175bSOded Gabbay 	.hw_queues_lock = gaudi_hw_queues_lock,
9248e65e175bSOded Gabbay 	.hw_queues_unlock = gaudi_hw_queues_unlock,
9249e65e175bSOded Gabbay 	.get_pci_id = gaudi_get_pci_id,
9250e65e175bSOded Gabbay 	.get_eeprom_data = gaudi_get_eeprom_data,
9251e65e175bSOded Gabbay 	.get_monitor_dump = gaudi_get_monitor_dump,
9252e65e175bSOded Gabbay 	.send_cpu_message = gaudi_send_cpu_message,
9253e65e175bSOded Gabbay 	.pci_bars_map = gaudi_pci_bars_map,
9254e65e175bSOded Gabbay 	.init_iatu = gaudi_init_iatu,
9255e65e175bSOded Gabbay 	.rreg = hl_rreg,
9256e65e175bSOded Gabbay 	.wreg = hl_wreg,
9257e65e175bSOded Gabbay 	.halt_coresight = gaudi_halt_coresight,
9258e65e175bSOded Gabbay 	.ctx_init = gaudi_ctx_init,
9259e65e175bSOded Gabbay 	.ctx_fini = gaudi_ctx_fini,
9260e65e175bSOded Gabbay 	.pre_schedule_cs = gaudi_pre_schedule_cs,
9261e65e175bSOded Gabbay 	.get_queue_id_for_cq = gaudi_get_queue_id_for_cq,
9262e65e175bSOded Gabbay 	.load_firmware_to_device = gaudi_load_firmware_to_device,
9263e65e175bSOded Gabbay 	.load_boot_fit_to_device = gaudi_load_boot_fit_to_device,
9264e65e175bSOded Gabbay 	.get_signal_cb_size = gaudi_get_signal_cb_size,
9265e65e175bSOded Gabbay 	.get_wait_cb_size = gaudi_get_wait_cb_size,
9266e65e175bSOded Gabbay 	.gen_signal_cb = gaudi_gen_signal_cb,
9267e65e175bSOded Gabbay 	.gen_wait_cb = gaudi_gen_wait_cb,
9268e65e175bSOded Gabbay 	.reset_sob = gaudi_reset_sob,
9269e65e175bSOded Gabbay 	.reset_sob_group = gaudi_reset_sob_group,
9270e65e175bSOded Gabbay 	.get_device_time = gaudi_get_device_time,
9271e65e175bSOded Gabbay 	.pb_print_security_errors = NULL,
9272e65e175bSOded Gabbay 	.collective_wait_init_cs = gaudi_collective_wait_init_cs,
9273e65e175bSOded Gabbay 	.collective_wait_create_jobs = gaudi_collective_wait_create_jobs,
9274e65e175bSOded Gabbay 	.get_dec_base_addr = NULL,
9275e65e175bSOded Gabbay 	.scramble_addr = hl_mmu_scramble_addr,
9276e65e175bSOded Gabbay 	.descramble_addr = hl_mmu_descramble_addr,
9277e65e175bSOded Gabbay 	.ack_protection_bits_errors = gaudi_ack_protection_bits_errors,
9278e65e175bSOded Gabbay 	.get_hw_block_id = gaudi_get_hw_block_id,
9279e65e175bSOded Gabbay 	.hw_block_mmap = gaudi_block_mmap,
9280e65e175bSOded Gabbay 	.enable_events_from_fw = gaudi_enable_events_from_fw,
9281e65e175bSOded Gabbay 	.ack_mmu_errors = gaudi_ack_mmu_page_fault_or_access_error,
9282e65e175bSOded Gabbay 	.map_pll_idx_to_fw_idx = gaudi_map_pll_idx_to_fw_idx,
9283e65e175bSOded Gabbay 	.init_firmware_preload_params = gaudi_init_firmware_preload_params,
9284e65e175bSOded Gabbay 	.init_firmware_loader = gaudi_init_firmware_loader,
9285e65e175bSOded Gabbay 	.init_cpu_scrambler_dram = gaudi_init_scrambler_hbm,
9286e65e175bSOded Gabbay 	.state_dump_init = gaudi_state_dump_init,
9287e65e175bSOded Gabbay 	.get_sob_addr = gaudi_get_sob_addr,
9288e65e175bSOded Gabbay 	.set_pci_memory_regions = gaudi_set_pci_memory_regions,
9289e65e175bSOded Gabbay 	.get_stream_master_qid_arr = gaudi_get_stream_master_qid_arr,
9290e65e175bSOded Gabbay 	.check_if_razwi_happened = gaudi_check_if_razwi_happened,
9291e65e175bSOded Gabbay 	.mmu_get_real_page_size = hl_mmu_get_real_page_size,
9292e65e175bSOded Gabbay 	.access_dev_mem = hl_access_dev_mem,
9293e65e175bSOded Gabbay 	.set_dram_bar_base = gaudi_set_hbm_bar_base,
9294e65e175bSOded Gabbay 	.send_device_activity = gaudi_send_device_activity,
9295e65e175bSOded Gabbay 	.set_dram_properties = gaudi_set_dram_properties,
9296ab509d81SOhad Sharabi 	.set_binning_masks = gaudi_set_binning_masks,
9297e65e175bSOded Gabbay };
9298e65e175bSOded Gabbay 
9299e65e175bSOded Gabbay /**
9300e65e175bSOded Gabbay  * gaudi_set_asic_funcs - set GAUDI function pointers
9301e65e175bSOded Gabbay  *
9302e65e175bSOded Gabbay  * @hdev: pointer to hl_device structure
9303e65e175bSOded Gabbay  *
9304e65e175bSOded Gabbay  */
9305e65e175bSOded Gabbay void gaudi_set_asic_funcs(struct hl_device *hdev)
9306e65e175bSOded Gabbay {
9307e65e175bSOded Gabbay 	hdev->asic_funcs = &gaudi_funcs;
9308e65e175bSOded Gabbay }
9309