blob: 42c944efffbcc9aef3155b1b22082c4aa0a7c1cb [file] [log] [blame]
Oded Gabbayac0ae6a2020-05-11 10:29:27 +03001// SPDX-License-Identifier: GPL-2.0
2
3/*
4 * Copyright 2016-2020 HabanaLabs, Ltd.
5 * All Rights Reserved.
6 */
7
8#include "gaudiP.h"
Greg Kroah-Hartman7b16a152020-07-28 19:18:51 +02009#include "../include/hw_ip/mmu/mmu_general.h"
10#include "../include/hw_ip/mmu/mmu_v1_1.h"
11#include "../include/gaudi/gaudi_masks.h"
12#include "../include/gaudi/gaudi_fw_if.h"
13#include "../include/gaudi/gaudi_reg_map.h"
14#include "../include/gaudi/gaudi_async_ids_map_extended.h"
Oded Gabbayac0ae6a2020-05-11 10:29:27 +030015
16#include <linux/module.h>
17#include <linux/pci.h>
18#include <linux/firmware.h>
19#include <linux/hwmon.h>
Oded Gabbayac0ae6a2020-05-11 10:29:27 +030020#include <linux/iommu.h>
21#include <linux/seq_file.h>
22
23/*
24 * Gaudi security scheme:
25 *
26 * 1. Host is protected by:
27 * - Range registers
28 * - MMU
29 *
30 * 2. DDR is protected by:
31 * - Range registers (protect the first 512MB)
32 *
33 * 3. Configuration is protected by:
34 * - Range registers
35 * - Protection bits
36 *
37 * MMU is always enabled.
38 *
Ofir Bitton0940cab2020-08-31 08:52:56 +030039 * QMAN DMA channels 0,1 (PCI DMAN):
Oded Gabbayac0ae6a2020-05-11 10:29:27 +030040 * - DMA is not secured.
41 * - PQ and CQ are secured.
42 * - CP is secured: The driver needs to parse CB but WREG should be allowed
43 * because of TDMA (tensor DMA). Hence, WREG is always not
44 * secured.
45 *
46 * When the driver needs to use DMA it will check that Gaudi is idle, set DMA
47 * channel 0 to be secured, execute the DMA and change it back to not secured.
48 * Currently, the driver doesn't use the DMA while there are compute jobs
49 * running.
50 *
51 * The current use cases for the driver to use the DMA are:
52 * - Clear SRAM on context switch (happens on context switch when device is
53 * idle)
54 * - MMU page tables area clear (happens on init)
55 *
Ofir Bitton0940cab2020-08-31 08:52:56 +030056 * QMAN DMA 2-7, TPC, MME, NIC:
Oded Gabbayac0ae6a2020-05-11 10:29:27 +030057 * PQ is secured and is located on the Host (HBM CON TPC3 bug)
58 * CQ, CP and the engine are not secured
59 *
60 */
61
62#define GAUDI_BOOT_FIT_FILE "habanalabs/gaudi/gaudi-boot-fit.itb"
63#define GAUDI_LINUX_FW_FILE "habanalabs/gaudi/gaudi-fit.itb"
64#define GAUDI_TPC_FW_FILE "habanalabs/gaudi/gaudi_tpc.bin"
65
66#define GAUDI_DMA_POOL_BLK_SIZE 0x100 /* 256 bytes */
67
Ofir Bittonb90c8942020-11-08 12:59:04 +020068#define GAUDI_RESET_TIMEOUT_MSEC 2000 /* 2000ms */
Oded Gabbayac0ae6a2020-05-11 10:29:27 +030069#define GAUDI_RESET_WAIT_MSEC 1 /* 1ms */
70#define GAUDI_CPU_RESET_WAIT_MSEC 200 /* 200ms */
71#define GAUDI_TEST_QUEUE_WAIT_USEC 100000 /* 100ms */
72
73#define GAUDI_PLDM_RESET_WAIT_MSEC 1000 /* 1s */
74#define GAUDI_PLDM_HRESET_TIMEOUT_MSEC 20000 /* 20s */
Oded Gabbayac0ae6a2020-05-11 10:29:27 +030075#define GAUDI_PLDM_TEST_QUEUE_WAIT_USEC 1000000 /* 1s */
76#define GAUDI_PLDM_MMU_TIMEOUT_USEC (MMU_CONFIG_TIMEOUT_USEC * 100)
77#define GAUDI_PLDM_QMAN0_TIMEOUT_USEC (HL_DEVICE_TIMEOUT_USEC * 30)
78#define GAUDI_PLDM_TPC_KERNEL_WAIT_USEC (HL_DEVICE_TIMEOUT_USEC * 30)
79#define GAUDI_BOOT_FIT_REQ_TIMEOUT_USEC 1000000 /* 1s */
Oded Gabbay788cacf2020-07-07 17:30:13 +030080#define GAUDI_MSG_TO_CPU_TIMEOUT_USEC 4000000 /* 4s */
Oded Gabbayac0ae6a2020-05-11 10:29:27 +030081
82#define GAUDI_QMAN0_FENCE_VAL 0x72E91AB9
83
84#define GAUDI_MAX_STRING_LEN 20
85
86#define GAUDI_CB_POOL_CB_CNT 512
87#define GAUDI_CB_POOL_CB_SIZE 0x20000 /* 128KB */
88
89#define GAUDI_ALLOC_CPU_MEM_RETRY_CNT 3
90
91#define GAUDI_NUM_OF_TPC_INTR_CAUSE 20
92
93#define GAUDI_NUM_OF_QM_ERR_CAUSE 16
94
95#define GAUDI_NUM_OF_QM_ARB_ERR_CAUSE 3
96
Oded Gabbay647e8352020-06-07 11:26:48 +030097#define GAUDI_ARB_WDT_TIMEOUT 0x1000000
Oded Gabbayac0ae6a2020-05-11 10:29:27 +030098
Oded Gabbaye38bfd32020-07-03 20:46:12 +030099#define GAUDI_CLK_GATE_DEBUGFS_MASK (\
100 BIT(GAUDI_ENGINE_ID_MME_0) |\
101 BIT(GAUDI_ENGINE_ID_MME_2) |\
102 GENMASK_ULL(GAUDI_ENGINE_ID_TPC_7, GAUDI_ENGINE_ID_TPC_0))
103
farah kassabri03df1362020-05-06 11:17:38 +0300104#define HBM_SCRUBBING_TIMEOUT_US 1000000 /* 1s */
105
Alon Mizrahi41478642020-11-17 14:25:14 +0200106#define GAUDI_PLL_MAX 10
107
Oded Gabbayac0ae6a2020-05-11 10:29:27 +0300108static const char gaudi_irq_name[GAUDI_MSI_ENTRIES][GAUDI_MAX_STRING_LEN] = {
109 "gaudi cq 0_0", "gaudi cq 0_1", "gaudi cq 0_2", "gaudi cq 0_3",
110 "gaudi cq 1_0", "gaudi cq 1_1", "gaudi cq 1_2", "gaudi cq 1_3",
111 "gaudi cq 5_0", "gaudi cq 5_1", "gaudi cq 5_2", "gaudi cq 5_3",
112 "gaudi cpu eq"
113};
114
115static const u8 gaudi_dma_assignment[GAUDI_DMA_MAX] = {
Oded Gabbaye38bfd32020-07-03 20:46:12 +0300116 [GAUDI_PCI_DMA_1] = GAUDI_ENGINE_ID_DMA_0,
117 [GAUDI_PCI_DMA_2] = GAUDI_ENGINE_ID_DMA_1,
Oded Gabbaye38bfd32020-07-03 20:46:12 +0300118 [GAUDI_HBM_DMA_1] = GAUDI_ENGINE_ID_DMA_2,
119 [GAUDI_HBM_DMA_2] = GAUDI_ENGINE_ID_DMA_3,
120 [GAUDI_HBM_DMA_3] = GAUDI_ENGINE_ID_DMA_4,
Ofir Bitton0940cab2020-08-31 08:52:56 +0300121 [GAUDI_HBM_DMA_4] = GAUDI_ENGINE_ID_DMA_5,
122 [GAUDI_HBM_DMA_5] = GAUDI_ENGINE_ID_DMA_6,
123 [GAUDI_HBM_DMA_6] = GAUDI_ENGINE_ID_DMA_7
Oded Gabbayac0ae6a2020-05-11 10:29:27 +0300124};
125
126static const u8 gaudi_cq_assignment[NUMBER_OF_CMPLT_QUEUES] = {
127 [0] = GAUDI_QUEUE_ID_DMA_0_0,
128 [1] = GAUDI_QUEUE_ID_DMA_0_1,
129 [2] = GAUDI_QUEUE_ID_DMA_0_2,
130 [3] = GAUDI_QUEUE_ID_DMA_0_3,
131 [4] = GAUDI_QUEUE_ID_DMA_1_0,
132 [5] = GAUDI_QUEUE_ID_DMA_1_1,
133 [6] = GAUDI_QUEUE_ID_DMA_1_2,
134 [7] = GAUDI_QUEUE_ID_DMA_1_3,
Oded Gabbayac0ae6a2020-05-11 10:29:27 +0300135};
136
137static const u16 gaudi_packet_sizes[MAX_PACKET_ID] = {
138 [PACKET_WREG_32] = sizeof(struct packet_wreg32),
139 [PACKET_WREG_BULK] = sizeof(struct packet_wreg_bulk),
140 [PACKET_MSG_LONG] = sizeof(struct packet_msg_long),
141 [PACKET_MSG_SHORT] = sizeof(struct packet_msg_short),
142 [PACKET_CP_DMA] = sizeof(struct packet_cp_dma),
143 [PACKET_REPEAT] = sizeof(struct packet_repeat),
144 [PACKET_MSG_PROT] = sizeof(struct packet_msg_prot),
145 [PACKET_FENCE] = sizeof(struct packet_fence),
146 [PACKET_LIN_DMA] = sizeof(struct packet_lin_dma),
147 [PACKET_NOP] = sizeof(struct packet_nop),
148 [PACKET_STOP] = sizeof(struct packet_stop),
149 [PACKET_ARB_POINT] = sizeof(struct packet_arb_point),
150 [PACKET_WAIT] = sizeof(struct packet_wait),
151 [PACKET_LOAD_AND_EXE] = sizeof(struct packet_load_and_exe)
152};
153
Ofir Bittonbc75be22020-07-30 14:56:38 +0300154static inline bool validate_packet_id(enum packet_id id)
155{
156 switch (id) {
157 case PACKET_WREG_32:
158 case PACKET_WREG_BULK:
159 case PACKET_MSG_LONG:
160 case PACKET_MSG_SHORT:
161 case PACKET_CP_DMA:
162 case PACKET_REPEAT:
163 case PACKET_MSG_PROT:
164 case PACKET_FENCE:
165 case PACKET_LIN_DMA:
166 case PACKET_NOP:
167 case PACKET_STOP:
168 case PACKET_ARB_POINT:
169 case PACKET_WAIT:
170 case PACKET_LOAD_AND_EXE:
171 return true;
172 default:
173 return false;
174 }
175}
176
Oded Gabbayac0ae6a2020-05-11 10:29:27 +0300177static const char * const
178gaudi_tpc_interrupts_cause[GAUDI_NUM_OF_TPC_INTR_CAUSE] = {
179 "tpc_address_exceed_slm",
180 "tpc_div_by_0",
181 "tpc_spu_mac_overflow",
182 "tpc_spu_addsub_overflow",
183 "tpc_spu_abs_overflow",
184 "tpc_spu_fp_dst_nan_inf",
185 "tpc_spu_fp_dst_denorm",
186 "tpc_vpu_mac_overflow",
187 "tpc_vpu_addsub_overflow",
188 "tpc_vpu_abs_overflow",
189 "tpc_vpu_fp_dst_nan_inf",
190 "tpc_vpu_fp_dst_denorm",
191 "tpc_assertions",
192 "tpc_illegal_instruction",
193 "tpc_pc_wrap_around",
194 "tpc_qm_sw_err",
195 "tpc_hbw_rresp_err",
196 "tpc_hbw_bresp_err",
197 "tpc_lbw_rresp_err",
198 "tpc_lbw_bresp_err"
199};
200
201static const char * const
202gaudi_qman_error_cause[GAUDI_NUM_OF_QM_ERR_CAUSE] = {
203 "PQ AXI HBW error",
204 "CQ AXI HBW error",
205 "CP AXI HBW error",
206 "CP error due to undefined OPCODE",
207 "CP encountered STOP OPCODE",
208 "CP AXI LBW error",
209 "CP WRREG32 or WRBULK returned error",
210 "N/A",
211 "FENCE 0 inc over max value and clipped",
212 "FENCE 1 inc over max value and clipped",
213 "FENCE 2 inc over max value and clipped",
214 "FENCE 3 inc over max value and clipped",
215 "FENCE 0 dec under min value and clipped",
216 "FENCE 1 dec under min value and clipped",
217 "FENCE 2 dec under min value and clipped",
218 "FENCE 3 dec under min value and clipped"
219};
220
221static const char * const
222gaudi_qman_arb_error_cause[GAUDI_NUM_OF_QM_ARB_ERR_CAUSE] = {
223 "Choice push while full error",
224 "Choice Q watchdog error",
225 "MSG AXI LBW returned with error"
226};
227
Ofir Bittonf8bc7f02021-01-03 20:52:40 +0200228enum gaudi_sm_sei_cause {
229 GAUDI_SM_SEI_SO_OVERFLOW,
230 GAUDI_SM_SEI_LBW_4B_UNALIGNED,
231 GAUDI_SM_SEI_AXI_RESPONSE_ERR
232};
233
Oded Gabbayac0ae6a2020-05-11 10:29:27 +0300234static enum hl_queue_type gaudi_queue_type[GAUDI_QUEUE_ID_SIZE] = {
235 QUEUE_TYPE_EXT, /* GAUDI_QUEUE_ID_DMA_0_0 */
236 QUEUE_TYPE_EXT, /* GAUDI_QUEUE_ID_DMA_0_1 */
237 QUEUE_TYPE_EXT, /* GAUDI_QUEUE_ID_DMA_0_2 */
238 QUEUE_TYPE_EXT, /* GAUDI_QUEUE_ID_DMA_0_3 */
239 QUEUE_TYPE_EXT, /* GAUDI_QUEUE_ID_DMA_1_0 */
240 QUEUE_TYPE_EXT, /* GAUDI_QUEUE_ID_DMA_1_1 */
241 QUEUE_TYPE_EXT, /* GAUDI_QUEUE_ID_DMA_1_2 */
242 QUEUE_TYPE_EXT, /* GAUDI_QUEUE_ID_DMA_1_3 */
243 QUEUE_TYPE_CPU, /* GAUDI_QUEUE_ID_CPU_PQ */
244 QUEUE_TYPE_INT, /* GAUDI_QUEUE_ID_DMA_2_0 */
245 QUEUE_TYPE_INT, /* GAUDI_QUEUE_ID_DMA_2_1 */
246 QUEUE_TYPE_INT, /* GAUDI_QUEUE_ID_DMA_2_2 */
247 QUEUE_TYPE_INT, /* GAUDI_QUEUE_ID_DMA_2_3 */
248 QUEUE_TYPE_INT, /* GAUDI_QUEUE_ID_DMA_3_0 */
249 QUEUE_TYPE_INT, /* GAUDI_QUEUE_ID_DMA_3_1 */
250 QUEUE_TYPE_INT, /* GAUDI_QUEUE_ID_DMA_3_2 */
251 QUEUE_TYPE_INT, /* GAUDI_QUEUE_ID_DMA_3_3 */
252 QUEUE_TYPE_INT, /* GAUDI_QUEUE_ID_DMA_4_0 */
253 QUEUE_TYPE_INT, /* GAUDI_QUEUE_ID_DMA_4_1 */
254 QUEUE_TYPE_INT, /* GAUDI_QUEUE_ID_DMA_4_2 */
255 QUEUE_TYPE_INT, /* GAUDI_QUEUE_ID_DMA_4_3 */
Ofir Bitton0940cab2020-08-31 08:52:56 +0300256 QUEUE_TYPE_INT, /* GAUDI_QUEUE_ID_DMA_5_0 */
257 QUEUE_TYPE_INT, /* GAUDI_QUEUE_ID_DMA_5_1 */
258 QUEUE_TYPE_INT, /* GAUDI_QUEUE_ID_DMA_5_2 */
259 QUEUE_TYPE_INT, /* GAUDI_QUEUE_ID_DMA_5_3 */
Oded Gabbayac0ae6a2020-05-11 10:29:27 +0300260 QUEUE_TYPE_INT, /* GAUDI_QUEUE_ID_DMA_6_0 */
261 QUEUE_TYPE_INT, /* GAUDI_QUEUE_ID_DMA_6_1 */
262 QUEUE_TYPE_INT, /* GAUDI_QUEUE_ID_DMA_6_2 */
263 QUEUE_TYPE_INT, /* GAUDI_QUEUE_ID_DMA_6_3 */
264 QUEUE_TYPE_INT, /* GAUDI_QUEUE_ID_DMA_7_0 */
265 QUEUE_TYPE_INT, /* GAUDI_QUEUE_ID_DMA_7_1 */
266 QUEUE_TYPE_INT, /* GAUDI_QUEUE_ID_DMA_7_2 */
267 QUEUE_TYPE_INT, /* GAUDI_QUEUE_ID_DMA_7_3 */
268 QUEUE_TYPE_INT, /* GAUDI_QUEUE_ID_MME_0_0 */
269 QUEUE_TYPE_INT, /* GAUDI_QUEUE_ID_MME_0_1 */
270 QUEUE_TYPE_INT, /* GAUDI_QUEUE_ID_MME_0_2 */
271 QUEUE_TYPE_INT, /* GAUDI_QUEUE_ID_MME_0_3 */
272 QUEUE_TYPE_INT, /* GAUDI_QUEUE_ID_MME_1_0 */
273 QUEUE_TYPE_INT, /* GAUDI_QUEUE_ID_MME_1_1 */
274 QUEUE_TYPE_INT, /* GAUDI_QUEUE_ID_MME_1_2 */
275 QUEUE_TYPE_INT, /* GAUDI_QUEUE_ID_MME_1_3 */
276 QUEUE_TYPE_INT, /* GAUDI_QUEUE_ID_TPC_0_0 */
277 QUEUE_TYPE_INT, /* GAUDI_QUEUE_ID_TPC_0_1 */
278 QUEUE_TYPE_INT, /* GAUDI_QUEUE_ID_TPC_0_2 */
279 QUEUE_TYPE_INT, /* GAUDI_QUEUE_ID_TPC_0_3 */
280 QUEUE_TYPE_INT, /* GAUDI_QUEUE_ID_TPC_1_0 */
281 QUEUE_TYPE_INT, /* GAUDI_QUEUE_ID_TPC_1_1 */
282 QUEUE_TYPE_INT, /* GAUDI_QUEUE_ID_TPC_1_2 */
283 QUEUE_TYPE_INT, /* GAUDI_QUEUE_ID_TPC_1_3 */
284 QUEUE_TYPE_INT, /* GAUDI_QUEUE_ID_TPC_2_0 */
285 QUEUE_TYPE_INT, /* GAUDI_QUEUE_ID_TPC_2_1 */
286 QUEUE_TYPE_INT, /* GAUDI_QUEUE_ID_TPC_2_2 */
287 QUEUE_TYPE_INT, /* GAUDI_QUEUE_ID_TPC_2_3 */
288 QUEUE_TYPE_INT, /* GAUDI_QUEUE_ID_TPC_3_0 */
289 QUEUE_TYPE_INT, /* GAUDI_QUEUE_ID_TPC_3_1 */
290 QUEUE_TYPE_INT, /* GAUDI_QUEUE_ID_TPC_3_2 */
291 QUEUE_TYPE_INT, /* GAUDI_QUEUE_ID_TPC_3_3 */
292 QUEUE_TYPE_INT, /* GAUDI_QUEUE_ID_TPC_4_0 */
293 QUEUE_TYPE_INT, /* GAUDI_QUEUE_ID_TPC_4_1 */
294 QUEUE_TYPE_INT, /* GAUDI_QUEUE_ID_TPC_4_2 */
295 QUEUE_TYPE_INT, /* GAUDI_QUEUE_ID_TPC_4_3 */
296 QUEUE_TYPE_INT, /* GAUDI_QUEUE_ID_TPC_5_0 */
297 QUEUE_TYPE_INT, /* GAUDI_QUEUE_ID_TPC_5_1 */
298 QUEUE_TYPE_INT, /* GAUDI_QUEUE_ID_TPC_5_2 */
299 QUEUE_TYPE_INT, /* GAUDI_QUEUE_ID_TPC_5_3 */
300 QUEUE_TYPE_INT, /* GAUDI_QUEUE_ID_TPC_6_0 */
301 QUEUE_TYPE_INT, /* GAUDI_QUEUE_ID_TPC_6_1 */
302 QUEUE_TYPE_INT, /* GAUDI_QUEUE_ID_TPC_6_2 */
303 QUEUE_TYPE_INT, /* GAUDI_QUEUE_ID_TPC_6_3 */
304 QUEUE_TYPE_INT, /* GAUDI_QUEUE_ID_TPC_7_0 */
305 QUEUE_TYPE_INT, /* GAUDI_QUEUE_ID_TPC_7_1 */
306 QUEUE_TYPE_INT, /* GAUDI_QUEUE_ID_TPC_7_2 */
307 QUEUE_TYPE_INT, /* GAUDI_QUEUE_ID_TPC_7_3 */
Oded Gabbay3c681572020-11-02 21:10:39 +0200308 QUEUE_TYPE_INT, /* GAUDI_QUEUE_ID_NIC_0_0 */
309 QUEUE_TYPE_INT, /* GAUDI_QUEUE_ID_NIC_0_1 */
310 QUEUE_TYPE_INT, /* GAUDI_QUEUE_ID_NIC_0_2 */
311 QUEUE_TYPE_INT, /* GAUDI_QUEUE_ID_NIC_0_3 */
312 QUEUE_TYPE_INT, /* GAUDI_QUEUE_ID_NIC_1_0 */
313 QUEUE_TYPE_INT, /* GAUDI_QUEUE_ID_NIC_1_1 */
314 QUEUE_TYPE_INT, /* GAUDI_QUEUE_ID_NIC_1_2 */
315 QUEUE_TYPE_INT, /* GAUDI_QUEUE_ID_NIC_1_3 */
316 QUEUE_TYPE_INT, /* GAUDI_QUEUE_ID_NIC_2_0 */
317 QUEUE_TYPE_INT, /* GAUDI_QUEUE_ID_NIC_2_1 */
318 QUEUE_TYPE_INT, /* GAUDI_QUEUE_ID_NIC_2_2 */
319 QUEUE_TYPE_INT, /* GAUDI_QUEUE_ID_NIC_2_3 */
320 QUEUE_TYPE_INT, /* GAUDI_QUEUE_ID_NIC_3_0 */
321 QUEUE_TYPE_INT, /* GAUDI_QUEUE_ID_NIC_3_1 */
322 QUEUE_TYPE_INT, /* GAUDI_QUEUE_ID_NIC_3_2 */
323 QUEUE_TYPE_INT, /* GAUDI_QUEUE_ID_NIC_3_3 */
324 QUEUE_TYPE_INT, /* GAUDI_QUEUE_ID_NIC_4_0 */
325 QUEUE_TYPE_INT, /* GAUDI_QUEUE_ID_NIC_4_1 */
326 QUEUE_TYPE_INT, /* GAUDI_QUEUE_ID_NIC_4_2 */
327 QUEUE_TYPE_INT, /* GAUDI_QUEUE_ID_NIC_4_3 */
328 QUEUE_TYPE_INT, /* GAUDI_QUEUE_ID_NIC_5_0 */
329 QUEUE_TYPE_INT, /* GAUDI_QUEUE_ID_NIC_5_1 */
330 QUEUE_TYPE_INT, /* GAUDI_QUEUE_ID_NIC_5_2 */
331 QUEUE_TYPE_INT, /* GAUDI_QUEUE_ID_NIC_5_3 */
332 QUEUE_TYPE_INT, /* GAUDI_QUEUE_ID_NIC_6_0 */
333 QUEUE_TYPE_INT, /* GAUDI_QUEUE_ID_NIC_6_1 */
334 QUEUE_TYPE_INT, /* GAUDI_QUEUE_ID_NIC_6_2 */
335 QUEUE_TYPE_INT, /* GAUDI_QUEUE_ID_NIC_6_3 */
336 QUEUE_TYPE_INT, /* GAUDI_QUEUE_ID_NIC_7_0 */
337 QUEUE_TYPE_INT, /* GAUDI_QUEUE_ID_NIC_7_1 */
338 QUEUE_TYPE_INT, /* GAUDI_QUEUE_ID_NIC_7_2 */
339 QUEUE_TYPE_INT, /* GAUDI_QUEUE_ID_NIC_7_3 */
340 QUEUE_TYPE_INT, /* GAUDI_QUEUE_ID_NIC_8_0 */
341 QUEUE_TYPE_INT, /* GAUDI_QUEUE_ID_NIC_8_1 */
342 QUEUE_TYPE_INT, /* GAUDI_QUEUE_ID_NIC_8_2 */
343 QUEUE_TYPE_INT, /* GAUDI_QUEUE_ID_NIC_8_3 */
344 QUEUE_TYPE_INT, /* GAUDI_QUEUE_ID_NIC_9_0 */
345 QUEUE_TYPE_INT, /* GAUDI_QUEUE_ID_NIC_9_1 */
346 QUEUE_TYPE_INT, /* GAUDI_QUEUE_ID_NIC_9_2 */
347 QUEUE_TYPE_INT, /* GAUDI_QUEUE_ID_NIC_9_3 */
Oded Gabbayac0ae6a2020-05-11 10:29:27 +0300348};
349
Oded Gabbayfcc6a4e2020-05-17 08:20:35 +0300350struct ecc_info_extract_params {
351 u64 block_address;
352 u32 num_memories;
353 bool derr;
354 bool disable_clock_gating;
355};
356
Oded Gabbayac0ae6a2020-05-11 10:29:27 +0300357static int gaudi_mmu_update_asid_hop0_addr(struct hl_device *hdev, u32 asid,
358 u64 phys_addr);
359static int gaudi_send_job_on_qman0(struct hl_device *hdev,
360 struct hl_cs_job *job);
361static int gaudi_memset_device_memory(struct hl_device *hdev, u64 addr,
362 u32 size, u64 val);
Ofir Bitton423815b2021-01-05 09:04:07 +0200363static int gaudi_memset_registers(struct hl_device *hdev, u64 reg_base,
364 u32 num_regs, u32 val);
365static int gaudi_schedule_register_memset(struct hl_device *hdev,
366 u32 hw_queue_id, u64 reg_base, u32 num_regs, u32 val);
Oded Gabbayac0ae6a2020-05-11 10:29:27 +0300367static int gaudi_run_tpc_kernel(struct hl_device *hdev, u64 tpc_kernel,
368 u32 tpc_id);
369static int gaudi_mmu_clear_pgt_range(struct hl_device *hdev);
Oded Gabbay2f553422020-08-15 16:28:10 +0300370static int gaudi_cpucp_info_get(struct hl_device *hdev);
Oded Gabbayac0ae6a2020-05-11 10:29:27 +0300371static void gaudi_disable_clock_gating(struct hl_device *hdev);
372static void gaudi_mmu_prepare(struct hl_device *hdev, u32 asid);
Ofir Bitton5de406c2020-09-10 10:56:26 +0300373static u32 gaudi_gen_signal_cb(struct hl_device *hdev, void *data, u16 sob_id,
Alon Mizrahi72ab9ca52020-12-02 19:55:30 +0200374 u32 size, bool eb);
Ofir Bitton5de406c2020-09-10 10:56:26 +0300375static u32 gaudi_gen_wait_cb(struct hl_device *hdev,
376 struct hl_gen_wait_properties *prop);
377
378static inline enum hl_collective_mode
379get_collective_mode(struct hl_device *hdev, u32 queue_id)
380{
381 if (gaudi_queue_type[queue_id] == QUEUE_TYPE_EXT)
382 return HL_COLLECTIVE_MASTER;
383
384 if (queue_id >= GAUDI_QUEUE_ID_DMA_5_0 &&
385 queue_id <= GAUDI_QUEUE_ID_DMA_5_3)
386 return HL_COLLECTIVE_SLAVE;
387
388 if (queue_id >= GAUDI_QUEUE_ID_TPC_7_0 &&
389 queue_id <= GAUDI_QUEUE_ID_TPC_7_3)
390 return HL_COLLECTIVE_SLAVE;
391
392 if (queue_id >= GAUDI_QUEUE_ID_NIC_0_0 &&
393 queue_id <= GAUDI_QUEUE_ID_NIC_9_3)
394 return HL_COLLECTIVE_SLAVE;
395
396 return HL_COLLECTIVE_NOT_SUPPORTED;
397}
Oded Gabbayac0ae6a2020-05-11 10:29:27 +0300398
Koby Elbazcd5def82021-02-23 21:31:27 +0200399static inline void set_default_power_values(struct hl_device *hdev)
400{
401 struct asic_fixed_properties *prop = &hdev->asic_prop;
402
403 if (hdev->card_type == cpucp_card_type_pmc) {
404 prop->max_power_default = MAX_POWER_DEFAULT_PMC;
405 prop->dc_power_default = DC_POWER_DEFAULT_PMC;
406 } else {
407 prop->max_power_default = MAX_POWER_DEFAULT_PCI;
408 prop->dc_power_default = DC_POWER_DEFAULT_PCI;
409 }
410}
411
Oded Gabbayac0ae6a2020-05-11 10:29:27 +0300412static int gaudi_get_fixed_properties(struct hl_device *hdev)
413{
414 struct asic_fixed_properties *prop = &hdev->asic_prop;
Ofir Bitton843839b2020-07-19 11:08:09 +0300415 u32 num_sync_stream_queues = 0;
Oded Gabbayac0ae6a2020-05-11 10:29:27 +0300416 int i;
417
Ofir Bitton3abc99b2020-06-23 14:50:39 +0300418 prop->max_queues = GAUDI_QUEUE_ID_SIZE;
419 prop->hw_queues_props = kcalloc(prop->max_queues,
420 sizeof(struct hw_queue_properties),
421 GFP_KERNEL);
Oded Gabbayac0ae6a2020-05-11 10:29:27 +0300422
Ofir Bitton3abc99b2020-06-23 14:50:39 +0300423 if (!prop->hw_queues_props)
424 return -ENOMEM;
425
426 for (i = 0 ; i < prop->max_queues ; i++) {
Oded Gabbayac0ae6a2020-05-11 10:29:27 +0300427 if (gaudi_queue_type[i] == QUEUE_TYPE_EXT) {
428 prop->hw_queues_props[i].type = QUEUE_TYPE_EXT;
429 prop->hw_queues_props[i].driver_only = 0;
Ofir Bitton21e7a342020-05-14 18:25:47 +0300430 prop->hw_queues_props[i].supports_sync_stream = 1;
Tal Cohen4bb1f2f2020-06-03 09:25:27 +0300431 prop->hw_queues_props[i].cb_alloc_flags =
432 CB_ALLOC_KERNEL;
Ofir Bitton843839b2020-07-19 11:08:09 +0300433 num_sync_stream_queues++;
Oded Gabbayac0ae6a2020-05-11 10:29:27 +0300434 } else if (gaudi_queue_type[i] == QUEUE_TYPE_CPU) {
435 prop->hw_queues_props[i].type = QUEUE_TYPE_CPU;
436 prop->hw_queues_props[i].driver_only = 1;
Ofir Bitton21e7a342020-05-14 18:25:47 +0300437 prop->hw_queues_props[i].supports_sync_stream = 0;
Tal Cohen4bb1f2f2020-06-03 09:25:27 +0300438 prop->hw_queues_props[i].cb_alloc_flags =
439 CB_ALLOC_KERNEL;
Oded Gabbayac0ae6a2020-05-11 10:29:27 +0300440 } else if (gaudi_queue_type[i] == QUEUE_TYPE_INT) {
441 prop->hw_queues_props[i].type = QUEUE_TYPE_INT;
442 prop->hw_queues_props[i].driver_only = 0;
Tal Cohen4bb1f2f2020-06-03 09:25:27 +0300443 prop->hw_queues_props[i].supports_sync_stream = 0;
444 prop->hw_queues_props[i].cb_alloc_flags =
445 CB_ALLOC_USER;
Ofir Bitton5de406c2020-09-10 10:56:26 +0300446
Oded Gabbayac0ae6a2020-05-11 10:29:27 +0300447 }
Ofir Bitton5de406c2020-09-10 10:56:26 +0300448 prop->hw_queues_props[i].collective_mode =
449 get_collective_mode(hdev, i);
Oded Gabbayac0ae6a2020-05-11 10:29:27 +0300450 }
451
Oded Gabbayac0ae6a2020-05-11 10:29:27 +0300452 prop->completion_queues_count = NUMBER_OF_CMPLT_QUEUES;
Ofir Bitton5de406c2020-09-10 10:56:26 +0300453 prop->collective_first_sob = 0;
454 prop->collective_first_mon = 0;
455
456 /* 2 SOBs per internal queue stream are reserved for collective */
457 prop->sync_stream_first_sob =
458 ALIGN(NUMBER_OF_SOBS_IN_GRP, HL_MAX_SOBS_PER_MONITOR)
459 * QMAN_STREAMS * HL_RSVD_SOBS;
460
461 /* 1 monitor per internal queue stream are reserved for collective
462 * 2 monitors per external queue stream are reserved for collective
463 */
464 prop->sync_stream_first_mon =
465 (NUMBER_OF_COLLECTIVE_QUEUES * QMAN_STREAMS) +
466 (NUMBER_OF_EXT_HW_QUEUES * 2);
467
Oded Gabbayac0ae6a2020-05-11 10:29:27 +0300468 prop->dram_base_address = DRAM_PHYS_BASE;
469 prop->dram_size = GAUDI_HBM_SIZE_32GB;
470 prop->dram_end_address = prop->dram_base_address +
471 prop->dram_size;
472 prop->dram_user_base_address = DRAM_BASE_ADDR_USER;
473
474 prop->sram_base_address = SRAM_BASE_ADDR;
475 prop->sram_size = SRAM_SIZE;
476 prop->sram_end_address = prop->sram_base_address +
477 prop->sram_size;
478 prop->sram_user_base_address = prop->sram_base_address +
479 SRAM_USER_BASE_OFFSET;
480
481 prop->mmu_pgt_addr = MMU_PAGE_TABLES_ADDR;
482 if (hdev->pldm)
483 prop->mmu_pgt_size = 0x800000; /* 8MB */
484 else
485 prop->mmu_pgt_size = MMU_PAGE_TABLES_SIZE;
486 prop->mmu_pte_size = HL_PTE_SIZE;
487 prop->mmu_hop_table_size = HOP_TABLE_SIZE;
488 prop->mmu_hop0_tables_total_size = HOP0_TABLES_TOTAL_SIZE;
489 prop->dram_page_size = PAGE_SIZE_2MB;
Oded Gabbay7f070c92020-11-09 09:48:31 +0200490 prop->dram_supports_virtual_memory = false;
Oded Gabbayac0ae6a2020-05-11 10:29:27 +0300491
492 prop->pmmu.hop0_shift = HOP0_SHIFT;
493 prop->pmmu.hop1_shift = HOP1_SHIFT;
494 prop->pmmu.hop2_shift = HOP2_SHIFT;
495 prop->pmmu.hop3_shift = HOP3_SHIFT;
496 prop->pmmu.hop4_shift = HOP4_SHIFT;
497 prop->pmmu.hop0_mask = HOP0_MASK;
498 prop->pmmu.hop1_mask = HOP1_MASK;
499 prop->pmmu.hop2_mask = HOP2_MASK;
500 prop->pmmu.hop3_mask = HOP3_MASK;
501 prop->pmmu.hop4_mask = HOP4_MASK;
502 prop->pmmu.start_addr = VA_HOST_SPACE_START;
503 prop->pmmu.end_addr =
504 (VA_HOST_SPACE_START + VA_HOST_SPACE_SIZE / 2) - 1;
505 prop->pmmu.page_size = PAGE_SIZE_4KB;
Moti Haimovski7edf3412020-08-23 13:23:13 +0300506 prop->pmmu.num_hops = MMU_ARCH_5_HOPS;
Oded Gabbayac0ae6a2020-05-11 10:29:27 +0300507
508 /* PMMU and HPMMU are the same except of page size */
509 memcpy(&prop->pmmu_huge, &prop->pmmu, sizeof(prop->pmmu));
510 prop->pmmu_huge.page_size = PAGE_SIZE_2MB;
511
512 /* shifts and masks are the same in PMMU and DMMU */
513 memcpy(&prop->dmmu, &prop->pmmu, sizeof(prop->pmmu));
514 prop->dmmu.start_addr = (VA_HOST_SPACE_START + VA_HOST_SPACE_SIZE / 2);
515 prop->dmmu.end_addr = VA_HOST_SPACE_END;
516 prop->dmmu.page_size = PAGE_SIZE_2MB;
517
518 prop->cfg_size = CFG_SIZE;
519 prop->max_asid = MAX_ASID;
520 prop->num_of_events = GAUDI_EVENT_SIZE;
521 prop->tpc_enabled_mask = TPC_ENABLED_MASK;
522
Koby Elbazcd5def82021-02-23 21:31:27 +0200523 set_default_power_values(hdev);
Oded Gabbayac0ae6a2020-05-11 10:29:27 +0300524
525 prop->cb_pool_cb_cnt = GAUDI_CB_POOL_CB_CNT;
526 prop->cb_pool_cb_size = GAUDI_CB_POOL_CB_SIZE;
527
528 prop->pcie_dbi_base_address = mmPCIE_DBI_BASE;
529 prop->pcie_aux_dbi_reg_addr = CFG_BASE + mmPCIE_AUX_DBI;
530
Oded Gabbay2f553422020-08-15 16:28:10 +0300531 strncpy(prop->cpucp_info.card_name, GAUDI_DEFAULT_CARD_NAME,
Oded Gabbayac0ae6a2020-05-11 10:29:27 +0300532 CARD_NAME_MAX_LEN);
533
Ofir Bittonc16d45f2020-06-02 12:28:27 +0300534 prop->max_pending_cs = GAUDI_MAX_PENDING_CS;
535
Ofir Bitton843839b2020-07-19 11:08:09 +0300536 prop->first_available_user_sob[HL_GAUDI_WS_DCORE] =
Ofir Bitton3cf74b32020-09-10 09:17:50 +0300537 prop->sync_stream_first_sob +
538 (num_sync_stream_queues * HL_RSVD_SOBS);
Ofir Bitton843839b2020-07-19 11:08:09 +0300539 prop->first_available_user_mon[HL_GAUDI_WS_DCORE] =
Ofir Bitton3cf74b32020-09-10 09:17:50 +0300540 prop->sync_stream_first_mon +
541 (num_sync_stream_queues * HL_RSVD_MONS);
Ofir Bitton843839b2020-07-19 11:08:09 +0300542
Ofir Bittone1fa7242021-01-06 15:40:37 +0200543 prop->first_available_user_msix_interrupt = USHRT_MAX;
544
Ofir Bittone52606d2021-01-27 16:34:37 +0200545 for (i = 0 ; i < HL_MAX_DCORES ; i++)
546 prop->first_available_cq[i] = USHRT_MAX;
547
Ofir Bitton323b7262020-10-04 09:09:19 +0300548 prop->fw_security_status_valid = false;
Ofir Bittond611b9f2020-11-08 13:10:09 +0200549 prop->hard_reset_done_by_fw = false;
Ofir Bitton323b7262020-10-04 09:09:19 +0300550
Oded Gabbayac0ae6a2020-05-11 10:29:27 +0300551 return 0;
552}
553
554static int gaudi_pci_bars_map(struct hl_device *hdev)
555{
556 static const char * const name[] = {"SRAM", "CFG", "HBM"};
557 bool is_wc[3] = {false, false, true};
558 int rc;
559
560 rc = hl_pci_bars_map(hdev, name, is_wc);
561 if (rc)
562 return rc;
563
564 hdev->rmmio = hdev->pcie_bar[CFG_BAR_ID] +
565 (CFG_BASE - SPI_FLASH_BASE_ADDR);
566
567 return 0;
568}
569
570static u64 gaudi_set_hbm_bar_base(struct hl_device *hdev, u64 addr)
571{
572 struct gaudi_device *gaudi = hdev->asic_specific;
Ofir Bittonf4cbfd22020-06-15 17:45:12 +0300573 struct hl_inbound_pci_region pci_region;
Oded Gabbayac0ae6a2020-05-11 10:29:27 +0300574 u64 old_addr = addr;
575 int rc;
576
577 if ((gaudi) && (gaudi->hbm_bar_cur_addr == addr))
578 return old_addr;
579
580 /* Inbound Region 2 - Bar 4 - Point to HBM */
Ofir Bittonf4cbfd22020-06-15 17:45:12 +0300581 pci_region.mode = PCI_BAR_MATCH_MODE;
582 pci_region.bar = HBM_BAR_ID;
583 pci_region.addr = addr;
584 rc = hl_pci_set_inbound_region(hdev, 2, &pci_region);
Oded Gabbayac0ae6a2020-05-11 10:29:27 +0300585 if (rc)
586 return U64_MAX;
587
588 if (gaudi) {
589 old_addr = gaudi->hbm_bar_cur_addr;
590 gaudi->hbm_bar_cur_addr = addr;
591 }
592
593 return old_addr;
594}
595
596static int gaudi_init_iatu(struct hl_device *hdev)
597{
Ofir Bittonf4cbfd22020-06-15 17:45:12 +0300598 struct hl_inbound_pci_region inbound_region;
599 struct hl_outbound_pci_region outbound_region;
600 int rc;
601
Ofir Bitton41f458f2021-03-10 15:08:44 +0200602 if (hdev->asic_prop.iatu_done_by_fw) {
603 hdev->asic_funcs->set_dma_mask_from_fw(hdev);
604 return 0;
605 }
606
Ofir Bittonf4cbfd22020-06-15 17:45:12 +0300607 /* Inbound Region 0 - Bar 0 - Point to SRAM + CFG */
608 inbound_region.mode = PCI_BAR_MATCH_MODE;
609 inbound_region.bar = SRAM_BAR_ID;
610 inbound_region.addr = SRAM_BASE_ADDR;
611 rc = hl_pci_set_inbound_region(hdev, 0, &inbound_region);
612 if (rc)
613 goto done;
Oded Gabbayac0ae6a2020-05-11 10:29:27 +0300614
615 /* Inbound Region 1 - Bar 2 - Point to SPI FLASH */
Ofir Bittonf4cbfd22020-06-15 17:45:12 +0300616 inbound_region.mode = PCI_BAR_MATCH_MODE;
617 inbound_region.bar = CFG_BAR_ID;
618 inbound_region.addr = SPI_FLASH_BASE_ADDR;
619 rc = hl_pci_set_inbound_region(hdev, 1, &inbound_region);
Oded Gabbayac0ae6a2020-05-11 10:29:27 +0300620 if (rc)
Ofir Bittonf4cbfd22020-06-15 17:45:12 +0300621 goto done;
Oded Gabbayac0ae6a2020-05-11 10:29:27 +0300622
Ofir Bittonf4cbfd22020-06-15 17:45:12 +0300623 /* Inbound Region 2 - Bar 4 - Point to HBM */
624 inbound_region.mode = PCI_BAR_MATCH_MODE;
625 inbound_region.bar = HBM_BAR_ID;
626 inbound_region.addr = DRAM_PHYS_BASE;
627 rc = hl_pci_set_inbound_region(hdev, 2, &inbound_region);
628 if (rc)
629 goto done;
630
631 hdev->asic_funcs->set_dma_mask_from_fw(hdev);
632
633 /* Outbound Region 0 - Point to Host */
634 outbound_region.addr = HOST_PHYS_BASE;
635 outbound_region.size = HOST_PHYS_SIZE;
636 rc = hl_pci_set_outbound_region(hdev, &outbound_region);
637
638done:
639 return rc;
Oded Gabbayac0ae6a2020-05-11 10:29:27 +0300640}
641
Ofir Bittond1ddd902020-10-19 17:04:20 +0300642static enum hl_device_hw_state gaudi_get_hw_state(struct hl_device *hdev)
643{
644 return RREG32(mmHW_STATE);
645}
646
Oded Gabbayac0ae6a2020-05-11 10:29:27 +0300647static int gaudi_early_init(struct hl_device *hdev)
648{
649 struct asic_fixed_properties *prop = &hdev->asic_prop;
650 struct pci_dev *pdev = hdev->pdev;
Ofir Bitton41f458f2021-03-10 15:08:44 +0200651 u32 fw_boot_status;
Oded Gabbayac0ae6a2020-05-11 10:29:27 +0300652 int rc;
653
654 rc = gaudi_get_fixed_properties(hdev);
655 if (rc) {
656 dev_err(hdev->dev, "Failed to get fixed properties\n");
657 return rc;
658 }
659
660 /* Check BAR sizes */
661 if (pci_resource_len(pdev, SRAM_BAR_ID) != SRAM_BAR_SIZE) {
662 dev_err(hdev->dev,
663 "Not " HL_NAME "? BAR %d size %llu, expecting %llu\n",
664 SRAM_BAR_ID,
665 (unsigned long long) pci_resource_len(pdev,
666 SRAM_BAR_ID),
667 SRAM_BAR_SIZE);
Ofir Bitton3abc99b2020-06-23 14:50:39 +0300668 rc = -ENODEV;
669 goto free_queue_props;
Oded Gabbayac0ae6a2020-05-11 10:29:27 +0300670 }
671
672 if (pci_resource_len(pdev, CFG_BAR_ID) != CFG_BAR_SIZE) {
673 dev_err(hdev->dev,
674 "Not " HL_NAME "? BAR %d size %llu, expecting %llu\n",
675 CFG_BAR_ID,
676 (unsigned long long) pci_resource_len(pdev,
677 CFG_BAR_ID),
678 CFG_BAR_SIZE);
Ofir Bitton3abc99b2020-06-23 14:50:39 +0300679 rc = -ENODEV;
680 goto free_queue_props;
Oded Gabbayac0ae6a2020-05-11 10:29:27 +0300681 }
682
683 prop->dram_pci_bar_size = pci_resource_len(pdev, HBM_BAR_ID);
684
Ofir Bitton41f458f2021-03-10 15:08:44 +0200685 /* If FW security is enabled at this point it means no access to ELBI */
686 if (!hdev->asic_prop.fw_security_disabled) {
687 hdev->asic_prop.iatu_done_by_fw = true;
688 goto pci_init;
689 }
690
691 rc = hl_pci_elbi_read(hdev, CFG_BASE + mmCPU_BOOT_DEV_STS0,
692 &fw_boot_status);
693 if (rc)
694 goto free_queue_props;
695
696 /* Check whether FW is configuring iATU */
697 if ((fw_boot_status & CPU_BOOT_DEV_STS0_ENABLED) &&
698 (fw_boot_status & CPU_BOOT_DEV_STS0_FW_IATU_CONF_EN))
699 hdev->asic_prop.iatu_done_by_fw = true;
700
701pci_init:
Ofir Bittond1ddd902020-10-19 17:04:20 +0300702 rc = hl_pci_init(hdev);
Oded Gabbayac0ae6a2020-05-11 10:29:27 +0300703 if (rc)
Ofir Bitton3abc99b2020-06-23 14:50:39 +0300704 goto free_queue_props;
Oded Gabbayac0ae6a2020-05-11 10:29:27 +0300705
Ofir Bittond1ddd902020-10-19 17:04:20 +0300706 /* Before continuing in the initialization, we need to read the preboot
707 * version to determine whether we run with a security-enabled firmware
708 */
709 rc = hl_fw_read_preboot_status(hdev, mmPSOC_GLOBAL_CONF_CPU_BOOT_STATUS,
710 mmCPU_BOOT_DEV_STS0, mmCPU_BOOT_ERR0,
711 GAUDI_BOOT_FIT_REQ_TIMEOUT_USEC);
712 if (rc) {
713 if (hdev->reset_on_preboot_fail)
714 hdev->asic_funcs->hw_fini(hdev, true);
715 goto pci_fini;
716 }
717
Ofir Bitton9c9013c2020-12-01 10:39:54 +0200718 if (gaudi_get_hw_state(hdev) == HL_DEVICE_HW_STATE_DIRTY) {
719 dev_info(hdev->dev,
720 "H/W state is dirty, must reset before initializing\n");
721 hdev->asic_funcs->hw_fini(hdev, true);
722 }
723
Oded Gabbayac0ae6a2020-05-11 10:29:27 +0300724 return 0;
Ofir Bitton3abc99b2020-06-23 14:50:39 +0300725
Ofir Bittond1ddd902020-10-19 17:04:20 +0300726pci_fini:
727 hl_pci_fini(hdev);
Ofir Bitton3abc99b2020-06-23 14:50:39 +0300728free_queue_props:
729 kfree(hdev->asic_prop.hw_queues_props);
730 return rc;
Oded Gabbayac0ae6a2020-05-11 10:29:27 +0300731}
732
733static int gaudi_early_fini(struct hl_device *hdev)
734{
Ofir Bitton3abc99b2020-06-23 14:50:39 +0300735 kfree(hdev->asic_prop.hw_queues_props);
Oded Gabbayac0ae6a2020-05-11 10:29:27 +0300736 hl_pci_fini(hdev);
737
738 return 0;
739}
740
741/**
742 * gaudi_fetch_psoc_frequency - Fetch PSOC frequency values
743 *
744 * @hdev: pointer to hl_device structure
745 *
746 */
Ofir Bitton1cbca892020-10-05 11:36:00 +0300747static int gaudi_fetch_psoc_frequency(struct hl_device *hdev)
Oded Gabbayac0ae6a2020-05-11 10:29:27 +0300748{
749 struct asic_fixed_properties *prop = &hdev->asic_prop;
Alon Mizrahi65854892020-11-19 16:34:19 +0200750 u32 nr = 0, nf = 0, od = 0, div_fctr = 0, pll_clk, div_sel;
751 u16 pll_freq_arr[HL_PLL_NUM_OUTPUTS], freq;
Ofir Bitton1cbca892020-10-05 11:36:00 +0300752 int rc;
753
Alon Mizrahi65854892020-11-19 16:34:19 +0200754 if (hdev->asic_prop.fw_security_disabled) {
755 /* Backward compatibility */
756 div_fctr = RREG32(mmPSOC_CPU_PLL_DIV_FACTOR_2);
757 div_sel = RREG32(mmPSOC_CPU_PLL_DIV_SEL_2);
758 nr = RREG32(mmPSOC_CPU_PLL_NR);
759 nf = RREG32(mmPSOC_CPU_PLL_NF);
760 od = RREG32(mmPSOC_CPU_PLL_OD);
Oded Gabbayac0ae6a2020-05-11 10:29:27 +0300761
Alon Mizrahi65854892020-11-19 16:34:19 +0200762 if (div_sel == DIV_SEL_REF_CLK ||
763 div_sel == DIV_SEL_DIVIDED_REF) {
764 if (div_sel == DIV_SEL_REF_CLK)
765 freq = PLL_REF_CLK;
766 else
767 freq = PLL_REF_CLK / (div_fctr + 1);
768 } else if (div_sel == DIV_SEL_PLL_CLK ||
769 div_sel == DIV_SEL_DIVIDED_PLL) {
770 pll_clk = PLL_REF_CLK * (nf + 1) /
771 ((nr + 1) * (od + 1));
772 if (div_sel == DIV_SEL_PLL_CLK)
773 freq = pll_clk;
774 else
775 freq = pll_clk / (div_fctr + 1);
776 } else {
777 dev_warn(hdev->dev,
778 "Received invalid div select value: %d",
779 div_sel);
780 freq = 0;
781 }
782 } else {
Bharat Jauhari285c0fa2021-03-25 18:15:40 +0200783 rc = hl_fw_cpucp_pll_info_get(hdev, HL_GAUDI_CPU_PLL, pll_freq_arr);
Alon Mizrahi65854892020-11-19 16:34:19 +0200784
785 if (rc)
786 return rc;
787
788 freq = pll_freq_arr[2];
789 }
790
791 prop->psoc_timestamp_frequency = freq;
792 prop->psoc_pci_pll_nr = nr;
793 prop->psoc_pci_pll_nf = nf;
794 prop->psoc_pci_pll_od = od;
795 prop->psoc_pci_pll_div_factor = div_fctr;
Ofir Bitton1cbca892020-10-05 11:36:00 +0300796
797 return 0;
Oded Gabbayac0ae6a2020-05-11 10:29:27 +0300798}
799
800static int _gaudi_init_tpc_mem(struct hl_device *hdev,
801 dma_addr_t tpc_kernel_src_addr, u32 tpc_kernel_size)
802{
803 struct asic_fixed_properties *prop = &hdev->asic_prop;
804 struct packet_lin_dma *init_tpc_mem_pkt;
805 struct hl_cs_job *job;
806 struct hl_cb *cb;
807 u64 dst_addr;
808 u32 cb_size, ctl;
809 u8 tpc_id;
810 int rc;
811
Ofir Bittona04b7cd2020-07-13 13:36:55 +0300812 cb = hl_cb_kernel_create(hdev, PAGE_SIZE, false);
Oded Gabbayac0ae6a2020-05-11 10:29:27 +0300813 if (!cb)
814 return -EFAULT;
815
Arnd Bergmann82948e62020-10-26 17:08:06 +0100816 init_tpc_mem_pkt = cb->kernel_address;
Oded Gabbayac0ae6a2020-05-11 10:29:27 +0300817 cb_size = sizeof(*init_tpc_mem_pkt);
818 memset(init_tpc_mem_pkt, 0, cb_size);
819
820 init_tpc_mem_pkt->tsize = cpu_to_le32(tpc_kernel_size);
821
Oded Gabbay65887292020-08-12 11:21:01 +0300822 ctl = FIELD_PREP(GAUDI_PKT_CTL_OPCODE_MASK, PACKET_LIN_DMA);
823 ctl |= FIELD_PREP(GAUDI_PKT_LIN_DMA_CTL_LIN_MASK, 1);
824 ctl |= FIELD_PREP(GAUDI_PKT_CTL_RB_MASK, 1);
825 ctl |= FIELD_PREP(GAUDI_PKT_CTL_MB_MASK, 1);
Oded Gabbayac0ae6a2020-05-11 10:29:27 +0300826
827 init_tpc_mem_pkt->ctl = cpu_to_le32(ctl);
828
829 init_tpc_mem_pkt->src_addr = cpu_to_le64(tpc_kernel_src_addr);
830 dst_addr = (prop->sram_user_base_address &
831 GAUDI_PKT_LIN_DMA_DST_ADDR_MASK) >>
832 GAUDI_PKT_LIN_DMA_DST_ADDR_SHIFT;
833 init_tpc_mem_pkt->dst_addr |= cpu_to_le64(dst_addr);
834
835 job = hl_cs_allocate_job(hdev, QUEUE_TYPE_EXT, true);
836 if (!job) {
837 dev_err(hdev->dev, "Failed to allocate a new job\n");
838 rc = -ENOMEM;
839 goto release_cb;
840 }
841
842 job->id = 0;
843 job->user_cb = cb;
Tomer Tayarf07486742020-08-02 22:51:31 +0300844 atomic_inc(&job->user_cb->cs_cnt);
Oded Gabbayac0ae6a2020-05-11 10:29:27 +0300845 job->user_cb_size = cb_size;
846 job->hw_queue_id = GAUDI_QUEUE_ID_DMA_0_0;
847 job->patched_cb = job->user_cb;
848 job->job_cb_size = job->user_cb_size + sizeof(struct packet_msg_prot);
849
850 hl_debugfs_add_job(hdev, job);
851
852 rc = gaudi_send_job_on_qman0(hdev, job);
853
854 if (rc)
855 goto free_job;
856
857 for (tpc_id = 0 ; tpc_id < TPC_NUMBER_OF_ENGINES ; tpc_id++) {
858 rc = gaudi_run_tpc_kernel(hdev, dst_addr, tpc_id);
859 if (rc)
860 break;
861 }
862
863free_job:
864 hl_userptr_delete_list(hdev, &job->userptr_list);
865 hl_debugfs_remove_job(hdev, job);
866 kfree(job);
Tomer Tayarf07486742020-08-02 22:51:31 +0300867 atomic_dec(&cb->cs_cnt);
Oded Gabbayac0ae6a2020-05-11 10:29:27 +0300868
869release_cb:
870 hl_cb_put(cb);
871 hl_cb_destroy(hdev, &hdev->kernel_cb_mgr, cb->id << PAGE_SHIFT);
872
873 return rc;
874}
875
876/*
877 * gaudi_init_tpc_mem() - Initialize TPC memories.
878 * @hdev: Pointer to hl_device structure.
879 *
880 * Copy TPC kernel fw from firmware file and run it to initialize TPC memories.
881 *
882 * Return: 0 for success, negative value for error.
883 */
884static int gaudi_init_tpc_mem(struct hl_device *hdev)
885{
886 const struct firmware *fw;
887 size_t fw_size;
888 void *cpu_addr;
889 dma_addr_t dma_handle;
Oded Gabbay98e87812020-12-09 23:07:58 +0200890 int rc, count = 5;
Oded Gabbayac0ae6a2020-05-11 10:29:27 +0300891
Oded Gabbay98e87812020-12-09 23:07:58 +0200892again:
Oded Gabbayac0ae6a2020-05-11 10:29:27 +0300893 rc = request_firmware(&fw, GAUDI_TPC_FW_FILE, hdev->dev);
Oded Gabbay98e87812020-12-09 23:07:58 +0200894 if (rc == -EINTR && count-- > 0) {
895 msleep(50);
896 goto again;
897 }
898
Oded Gabbayac0ae6a2020-05-11 10:29:27 +0300899 if (rc) {
Oded Gabbay98e87812020-12-09 23:07:58 +0200900 dev_err(hdev->dev, "Failed to load firmware file %s\n",
Oded Gabbayac0ae6a2020-05-11 10:29:27 +0300901 GAUDI_TPC_FW_FILE);
902 goto out;
903 }
904
905 fw_size = fw->size;
906 cpu_addr = hdev->asic_funcs->asic_dma_alloc_coherent(hdev, fw_size,
907 &dma_handle, GFP_KERNEL | __GFP_ZERO);
908 if (!cpu_addr) {
909 dev_err(hdev->dev,
910 "Failed to allocate %zu of dma memory for TPC kernel\n",
911 fw_size);
912 rc = -ENOMEM;
913 goto out;
914 }
915
916 memcpy(cpu_addr, fw->data, fw_size);
917
918 rc = _gaudi_init_tpc_mem(hdev, dma_handle, fw_size);
919
920 hdev->asic_funcs->asic_dma_free_coherent(hdev, fw->size, cpu_addr,
921 dma_handle);
922
923out:
924 release_firmware(fw);
925 return rc;
926}
927
Ofir Bitton5de406c2020-09-10 10:56:26 +0300928static void gaudi_collective_map_sobs(struct hl_device *hdev, u32 stream)
Ofir Bitton5fe1c172020-09-10 10:10:55 +0300929{
Ofir Bitton5de406c2020-09-10 10:56:26 +0300930 struct gaudi_device *gaudi = hdev->asic_specific;
931 struct gaudi_collective_properties *prop = &gaudi->collective_props;
932 struct hl_hw_queue *q;
933 u32 i, sob_id, sob_group_id, queue_id;
Ofir Bitton5fe1c172020-09-10 10:10:55 +0300934
Ofir Bitton5de406c2020-09-10 10:56:26 +0300935 /* Iterate through SOB groups and assign a SOB for each slave queue */
936 sob_group_id =
937 stream * HL_RSVD_SOBS + prop->curr_sob_group_idx[stream];
938 sob_id = prop->hw_sob_group[sob_group_id].base_sob_id;
939
940 queue_id = GAUDI_QUEUE_ID_NIC_0_0 + stream;
941 for (i = 0 ; i < NIC_NUMBER_OF_ENGINES ; i++) {
942 q = &hdev->kernel_queues[queue_id + (4 * i)];
943 q->sync_stream_prop.collective_sob_id = sob_id + i;
944 }
945
946 /* Both DMA5 and TPC7 use the same resources since only a single
947 * engine need to participate in the reduction process
948 */
949 queue_id = GAUDI_QUEUE_ID_DMA_5_0 + stream;
950 q = &hdev->kernel_queues[queue_id];
951 q->sync_stream_prop.collective_sob_id =
952 sob_id + NIC_NUMBER_OF_ENGINES;
953
954 queue_id = GAUDI_QUEUE_ID_TPC_7_0 + stream;
955 q = &hdev->kernel_queues[queue_id];
956 q->sync_stream_prop.collective_sob_id =
957 sob_id + NIC_NUMBER_OF_ENGINES;
958}
959
960static void gaudi_sob_group_hw_reset(struct kref *ref)
961{
962 struct gaudi_hw_sob_group *hw_sob_group =
963 container_of(ref, struct gaudi_hw_sob_group, kref);
964 struct hl_device *hdev = hw_sob_group->hdev;
Ofir Bitton423815b2021-01-05 09:04:07 +0200965 u64 base_addr;
966 int rc;
Ofir Bitton5de406c2020-09-10 10:56:26 +0300967
Ofir Bitton423815b2021-01-05 09:04:07 +0200968 base_addr = CFG_BASE + mmSYNC_MNGR_W_S_SYNC_MNGR_OBJS_SOB_OBJ_0 +
969 hw_sob_group->base_sob_id * 4;
970 rc = gaudi_schedule_register_memset(hdev, hw_sob_group->queue_id,
971 base_addr, NUMBER_OF_SOBS_IN_GRP, 0);
972 if (rc)
973 dev_err(hdev->dev,
974 "failed resetting sob group - sob base %u, count %u",
975 hw_sob_group->base_sob_id, NUMBER_OF_SOBS_IN_GRP);
Ofir Bitton5de406c2020-09-10 10:56:26 +0300976
977 kref_init(&hw_sob_group->kref);
978}
979
980static void gaudi_sob_group_reset_error(struct kref *ref)
981{
982 struct gaudi_hw_sob_group *hw_sob_group =
983 container_of(ref, struct gaudi_hw_sob_group, kref);
984 struct hl_device *hdev = hw_sob_group->hdev;
985
986 dev_crit(hdev->dev,
987 "SOB release shouldn't be called here, base_sob_id: %d\n",
988 hw_sob_group->base_sob_id);
989}
990
991static int gaudi_collective_init(struct hl_device *hdev)
992{
993 u32 i, master_monitor_sobs, sob_id, reserved_sobs_per_group;
994 struct gaudi_collective_properties *prop;
995 struct gaudi_device *gaudi;
996
997 gaudi = hdev->asic_specific;
998 prop = &gaudi->collective_props;
999 sob_id = hdev->asic_prop.collective_first_sob;
1000
1001 /* First sob in group must be aligned to HL_MAX_SOBS_PER_MONITOR */
1002 reserved_sobs_per_group =
1003 ALIGN(NUMBER_OF_SOBS_IN_GRP, HL_MAX_SOBS_PER_MONITOR);
1004
1005 /* Init SOB groups */
1006 for (i = 0 ; i < NUM_SOB_GROUPS; i++) {
1007 prop->hw_sob_group[i].hdev = hdev;
1008 prop->hw_sob_group[i].base_sob_id = sob_id;
1009 sob_id += reserved_sobs_per_group;
1010 gaudi_sob_group_hw_reset(&prop->hw_sob_group[i].kref);
1011 }
1012
1013 for (i = 0 ; i < QMAN_STREAMS; i++) {
1014 prop->next_sob_group_val[i] = 1;
1015 prop->curr_sob_group_idx[i] = 0;
1016 gaudi_collective_map_sobs(hdev, i);
1017 }
1018
1019 prop->mstr_sob_mask[0] = 0;
1020 master_monitor_sobs = HL_MAX_SOBS_PER_MONITOR;
1021 for (i = 0 ; i < master_monitor_sobs ; i++)
1022 if (gaudi->hw_cap_initialized & BIT(HW_CAP_NIC_SHIFT + i))
1023 prop->mstr_sob_mask[0] |= BIT(i);
1024
1025 prop->mstr_sob_mask[1] = 0;
1026 master_monitor_sobs =
1027 NIC_NUMBER_OF_ENGINES - HL_MAX_SOBS_PER_MONITOR;
1028 for (i = 0 ; i < master_monitor_sobs; i++) {
1029 if (gaudi->hw_cap_initialized & BIT(HW_CAP_NIC_SHIFT + i))
1030 prop->mstr_sob_mask[1] |= BIT(i);
1031 }
1032
1033 /* Set collective engine bit */
1034 prop->mstr_sob_mask[1] |= BIT(i);
1035
1036 return 0;
1037}
1038
1039static void gaudi_reset_sob_group(struct hl_device *hdev, u16 sob_group)
1040{
1041 struct gaudi_device *gaudi = hdev->asic_specific;
1042 struct gaudi_collective_properties *cprop = &gaudi->collective_props;
1043
1044 kref_put(&cprop->hw_sob_group[sob_group].kref,
1045 gaudi_sob_group_hw_reset);
1046}
1047
1048static void gaudi_collective_master_init_job(struct hl_device *hdev,
1049 struct hl_cs_job *job, u32 stream, u32 sob_group_offset)
1050{
1051 u32 master_sob_base, master_monitor, queue_id, cb_size = 0;
1052 struct gaudi_collective_properties *cprop;
1053 struct hl_gen_wait_properties wait_prop;
1054 struct hl_sync_stream_properties *prop;
1055 struct gaudi_device *gaudi;
1056
1057 gaudi = hdev->asic_specific;
1058 cprop = &gaudi->collective_props;
1059 queue_id = job->hw_queue_id;
1060 prop = &hdev->kernel_queues[queue_id].sync_stream_prop;
1061
1062 master_sob_base =
1063 cprop->hw_sob_group[sob_group_offset].base_sob_id;
1064 master_monitor = prop->collective_mstr_mon_id[0];
1065
Ofir Bitton423815b2021-01-05 09:04:07 +02001066 cprop->hw_sob_group[sob_group_offset].queue_id = queue_id;
1067
Ofir Bitton5de406c2020-09-10 10:56:26 +03001068 dev_dbg(hdev->dev,
1069 "Generate master wait CBs, sob %d (mask %#x), val:0x%x, mon %u, q %d\n",
1070 master_sob_base, cprop->mstr_sob_mask[0],
1071 cprop->next_sob_group_val[stream],
1072 master_monitor, queue_id);
1073
1074 wait_prop.data = (void *) job->patched_cb;
1075 wait_prop.sob_base = master_sob_base;
1076 wait_prop.sob_mask = cprop->mstr_sob_mask[0];
1077 wait_prop.sob_val = cprop->next_sob_group_val[stream];
1078 wait_prop.mon_id = master_monitor;
1079 wait_prop.q_idx = queue_id;
1080 wait_prop.size = cb_size;
1081 cb_size += gaudi_gen_wait_cb(hdev, &wait_prop);
1082
1083 master_sob_base += HL_MAX_SOBS_PER_MONITOR;
1084 master_monitor = prop->collective_mstr_mon_id[1];
1085
1086 dev_dbg(hdev->dev,
1087 "Generate master wait CBs, sob %d (mask %#x), val:0x%x, mon %u, q %d\n",
1088 master_sob_base, cprop->mstr_sob_mask[1],
1089 cprop->next_sob_group_val[stream],
1090 master_monitor, queue_id);
1091
1092 wait_prop.sob_base = master_sob_base;
1093 wait_prop.sob_mask = cprop->mstr_sob_mask[1];
1094 wait_prop.mon_id = master_monitor;
1095 wait_prop.size = cb_size;
1096 cb_size += gaudi_gen_wait_cb(hdev, &wait_prop);
1097}
1098
1099static void gaudi_collective_slave_init_job(struct hl_device *hdev,
1100 struct hl_cs_job *job, struct hl_cs_compl *cs_cmpl)
1101{
1102 struct hl_gen_wait_properties wait_prop;
1103 struct hl_sync_stream_properties *prop;
1104 u32 queue_id, cb_size = 0;
1105
1106 queue_id = job->hw_queue_id;
1107 prop = &hdev->kernel_queues[queue_id].sync_stream_prop;
1108
1109 /* Add to wait CBs using slave monitor */
1110 wait_prop.data = (void *) job->user_cb;
1111 wait_prop.sob_base = cs_cmpl->hw_sob->sob_id;
1112 wait_prop.sob_mask = 0x1;
1113 wait_prop.sob_val = cs_cmpl->sob_val;
1114 wait_prop.mon_id = prop->collective_slave_mon_id;
1115 wait_prop.q_idx = queue_id;
1116 wait_prop.size = cb_size;
1117
1118 dev_dbg(hdev->dev,
1119 "Generate slave wait CB, sob %d, val:0x%x, mon %d, q %d\n",
1120 cs_cmpl->hw_sob->sob_id, cs_cmpl->sob_val,
1121 prop->collective_slave_mon_id, queue_id);
1122
1123 cb_size += gaudi_gen_wait_cb(hdev, &wait_prop);
1124
1125 dev_dbg(hdev->dev,
1126 "generate signal CB, sob_id: %d, sob val: 1, q_idx: %d\n",
1127 prop->collective_sob_id, queue_id);
1128
1129 cb_size += gaudi_gen_signal_cb(hdev, job->user_cb,
Alon Mizrahi72ab9ca52020-12-02 19:55:30 +02001130 prop->collective_sob_id, cb_size, false);
Ofir Bitton5fe1c172020-09-10 10:10:55 +03001131}
1132
1133static void gaudi_collective_wait_init_cs(struct hl_cs *cs)
1134{
Ofir Bitton5de406c2020-09-10 10:56:26 +03001135 struct hl_cs_compl *signal_cs_cmpl =
1136 container_of(cs->signal_fence, struct hl_cs_compl, base_fence);
1137 struct hl_cs_compl *cs_cmpl =
1138 container_of(cs->fence, struct hl_cs_compl, base_fence);
1139 struct gaudi_collective_properties *cprop;
1140 u32 stream, queue_id, sob_group_offset;
1141 struct gaudi_device *gaudi;
1142 struct hl_device *hdev;
1143 struct hl_cs_job *job;
1144 struct hl_ctx *ctx;
Ofir Bitton5fe1c172020-09-10 10:10:55 +03001145
Ofir Bitton5de406c2020-09-10 10:56:26 +03001146 ctx = cs->ctx;
1147 hdev = ctx->hdev;
1148 gaudi = hdev->asic_specific;
1149 cprop = &gaudi->collective_props;
1150
1151 /* copy the SOB id and value of the signal CS */
1152 cs_cmpl->hw_sob = signal_cs_cmpl->hw_sob;
1153 cs_cmpl->sob_val = signal_cs_cmpl->sob_val;
1154
1155 /* Calculate the stream from collective master queue (1st job) */
1156 job = list_first_entry(&cs->job_list, struct hl_cs_job, cs_node);
1157 stream = job->hw_queue_id % 4;
1158 sob_group_offset =
1159 stream * HL_RSVD_SOBS + cprop->curr_sob_group_idx[stream];
1160
1161 list_for_each_entry(job, &cs->job_list, cs_node) {
1162 queue_id = job->hw_queue_id;
1163
1164 if (hdev->kernel_queues[queue_id].collective_mode ==
1165 HL_COLLECTIVE_MASTER)
1166 gaudi_collective_master_init_job(hdev, job, stream,
1167 sob_group_offset);
1168 else
1169 gaudi_collective_slave_init_job(hdev, job, cs_cmpl);
1170 }
1171
1172 cs_cmpl->sob_group = sob_group_offset;
1173
1174 /* Handle sob group kref and wraparound */
1175 kref_get(&cprop->hw_sob_group[sob_group_offset].kref);
1176 cprop->next_sob_group_val[stream]++;
1177
1178 if (cprop->next_sob_group_val[stream] == HL_MAX_SOB_VAL) {
1179 /*
1180 * Decrement as we reached the max value.
1181 * The release function won't be called here as we've
1182 * just incremented the refcount.
1183 */
1184 kref_put(&cprop->hw_sob_group[sob_group_offset].kref,
1185 gaudi_sob_group_reset_error);
1186 cprop->next_sob_group_val[stream] = 1;
1187 /* only two SOBs are currently in use */
1188 cprop->curr_sob_group_idx[stream] =
1189 (cprop->curr_sob_group_idx[stream] + 1) &
1190 (HL_RSVD_SOBS - 1);
1191
1192 gaudi_collective_map_sobs(hdev, stream);
1193
1194 dev_dbg(hdev->dev, "switched to SOB group %d, stream: %d\n",
1195 cprop->curr_sob_group_idx[stream], stream);
1196 }
1197
1198 /* Increment kref since all slave queues are now waiting on it */
1199 kref_get(&cs_cmpl->hw_sob->kref);
1200 /*
1201 * Must put the signal fence after the SOB refcnt increment so
1202 * the SOB refcnt won't turn 0 and reset the SOB before the
1203 * wait CS was submitted.
1204 */
1205 mb();
1206 hl_fence_put(cs->signal_fence);
1207 cs->signal_fence = NULL;
1208}
1209
1210static int gaudi_collective_wait_create_job(struct hl_device *hdev,
1211 struct hl_ctx *ctx, struct hl_cs *cs,
1212 enum hl_collective_mode mode, u32 queue_id, u32 wait_queue_id)
1213{
1214 struct hw_queue_properties *hw_queue_prop;
1215 struct hl_cs_counters_atomic *cntr;
1216 struct hl_cs_job *job;
1217 struct hl_cb *cb;
1218 u32 cb_size;
1219 bool patched_cb;
1220
1221 cntr = &hdev->aggregated_cs_counters;
1222
1223 if (mode == HL_COLLECTIVE_MASTER) {
1224 /* CB size of collective master queue contains
1225 * 4 msg short packets for monitor 1 configuration
1226 * 1 fence packet
1227 * 4 msg short packets for monitor 2 configuration
1228 * 1 fence packet
1229 * 2 msg prot packets for completion and MSI-X
1230 */
1231 cb_size = sizeof(struct packet_msg_short) * 8 +
1232 sizeof(struct packet_fence) * 2 +
1233 sizeof(struct packet_msg_prot) * 2;
1234 patched_cb = true;
1235 } else {
1236 /* CB size of collective slave queues contains
1237 * 4 msg short packets for monitor configuration
1238 * 1 fence packet
1239 * 1 additional msg short packet for sob signal
1240 */
1241 cb_size = sizeof(struct packet_msg_short) * 5 +
1242 sizeof(struct packet_fence);
1243 patched_cb = false;
1244 }
1245
1246 hw_queue_prop = &hdev->asic_prop.hw_queues_props[queue_id];
1247 job = hl_cs_allocate_job(hdev, hw_queue_prop->type, true);
1248 if (!job) {
farah kassabrie7536432020-10-12 14:30:26 +03001249 atomic64_inc(&ctx->cs_counters.out_of_mem_drop_cnt);
Ofir Bitton5de406c2020-09-10 10:56:26 +03001250 atomic64_inc(&cntr->out_of_mem_drop_cnt);
1251 dev_err(hdev->dev, "Failed to allocate a new job\n");
1252 return -ENOMEM;
1253 }
1254
1255 /* Allocate internal mapped CB for non patched CBs */
1256 cb = hl_cb_kernel_create(hdev, cb_size,
1257 hdev->mmu_enable && !patched_cb);
1258 if (!cb) {
farah kassabrie7536432020-10-12 14:30:26 +03001259 atomic64_inc(&ctx->cs_counters.out_of_mem_drop_cnt);
Ofir Bitton5de406c2020-09-10 10:56:26 +03001260 atomic64_inc(&cntr->out_of_mem_drop_cnt);
1261 kfree(job);
1262 return -EFAULT;
1263 }
1264
1265 job->id = 0;
1266 job->cs = cs;
1267 job->user_cb = cb;
Tomer Tayarf07486742020-08-02 22:51:31 +03001268 atomic_inc(&job->user_cb->cs_cnt);
Ofir Bitton5de406c2020-09-10 10:56:26 +03001269 job->user_cb_size = cb_size;
1270 job->hw_queue_id = queue_id;
1271
1272 /*
1273 * No need in parsing, user CB is the patched CB.
1274 * We call hl_cb_destroy() out of two reasons - we don't need
1275 * the CB in the CB idr anymore and to decrement its refcount as
1276 * it was incremented inside hl_cb_kernel_create().
1277 */
1278 if (patched_cb)
1279 job->patched_cb = job->user_cb;
1280 else
1281 job->patched_cb = NULL;
1282
1283 job->job_cb_size = job->user_cb_size;
1284 hl_cb_destroy(hdev, &hdev->kernel_cb_mgr, cb->id << PAGE_SHIFT);
1285
1286 /* increment refcount as for external queues we get completion */
1287 if (hw_queue_prop->type == QUEUE_TYPE_EXT)
1288 cs_get(cs);
1289
1290 cs->jobs_in_queue_cnt[job->hw_queue_id]++;
1291
1292 list_add_tail(&job->cs_node, &cs->job_list);
1293
1294 hl_debugfs_add_job(hdev, job);
1295
1296 return 0;
Ofir Bitton5fe1c172020-09-10 10:10:55 +03001297}
1298
1299static int gaudi_collective_wait_create_jobs(struct hl_device *hdev,
1300 struct hl_ctx *ctx, struct hl_cs *cs, u32 wait_queue_id,
1301 u32 collective_engine_id)
1302{
Ofir Bitton5de406c2020-09-10 10:56:26 +03001303 struct gaudi_device *gaudi = hdev->asic_specific;
1304 struct hw_queue_properties *hw_queue_prop;
1305 u32 queue_id, collective_queue, num_jobs;
1306 u32 stream, nic_queue, nic_idx = 0;
1307 bool skip;
Ofir Bitton266cdfa2020-12-22 15:56:12 +02001308 int i, rc = 0;
Ofir Bitton5de406c2020-09-10 10:56:26 +03001309
1310 /* Verify wait queue id is configured as master */
1311 hw_queue_prop = &hdev->asic_prop.hw_queues_props[wait_queue_id];
1312 if (!(hw_queue_prop->collective_mode == HL_COLLECTIVE_MASTER)) {
1313 dev_err(hdev->dev,
1314 "Queue %d is not configured as collective master\n",
1315 wait_queue_id);
1316 return -EINVAL;
1317 }
1318
1319 /* Verify engine id is supported */
1320 if (collective_engine_id != GAUDI_ENGINE_ID_DMA_5 &&
1321 collective_engine_id != GAUDI_ENGINE_ID_TPC_7) {
1322 dev_err(hdev->dev,
1323 "Collective wait does not support engine %u\n",
1324 collective_engine_id);
1325 return -EINVAL;
1326 }
1327
1328 stream = wait_queue_id % 4;
1329
1330 if (collective_engine_id == GAUDI_ENGINE_ID_DMA_5)
1331 collective_queue = GAUDI_QUEUE_ID_DMA_5_0 + stream;
Ofir Bitton5de406c2020-09-10 10:56:26 +03001332 else
Ofir Bitton71a984f2020-10-19 16:52:00 +03001333 collective_queue = GAUDI_QUEUE_ID_TPC_7_0 + stream;
Ofir Bitton5de406c2020-09-10 10:56:26 +03001334
1335 num_jobs = NUMBER_OF_SOBS_IN_GRP + 1;
1336 nic_queue = GAUDI_QUEUE_ID_NIC_0_0 + stream;
1337
1338 /* First job goes to the collective master queue, it will wait for
1339 * the collective slave queues to finish execution.
1340 * The synchronization is done using two monitors:
1341 * First monitor for NICs 0-7, second monitor for NICs 8-9 and the
1342 * reduction engine (DMA5/TPC7).
1343 *
1344 * Rest of the jobs goes to the collective slave queues which will
1345 * all wait for the user to signal sob 'cs_cmpl->sob_val'.
1346 */
1347 for (i = 0 ; i < num_jobs ; i++) {
1348 if (i == 0) {
1349 queue_id = wait_queue_id;
1350 rc = gaudi_collective_wait_create_job(hdev, ctx, cs,
1351 HL_COLLECTIVE_MASTER, queue_id, wait_queue_id);
1352 } else {
1353 if (nic_idx < NIC_NUMBER_OF_ENGINES) {
1354 if (gaudi->hw_cap_initialized &
1355 BIT(HW_CAP_NIC_SHIFT + nic_idx))
1356 skip = false;
1357 else
1358 skip = true;
1359
1360 queue_id = nic_queue;
1361 nic_queue += 4;
1362 nic_idx++;
1363
1364 if (skip)
1365 continue;
1366 } else {
1367 queue_id = collective_queue;
1368 }
1369
1370 rc = gaudi_collective_wait_create_job(hdev, ctx, cs,
1371 HL_COLLECTIVE_SLAVE, queue_id, wait_queue_id);
1372 }
1373
1374 if (rc)
1375 return rc;
1376 }
1377
1378 return rc;
Ofir Bitton5fe1c172020-09-10 10:10:55 +03001379}
1380
Oded Gabbayac0ae6a2020-05-11 10:29:27 +03001381static int gaudi_late_init(struct hl_device *hdev)
1382{
1383 struct gaudi_device *gaudi = hdev->asic_specific;
1384 int rc;
1385
Oded Gabbay2f553422020-08-15 16:28:10 +03001386 rc = gaudi->cpucp_info_get(hdev);
Oded Gabbayac0ae6a2020-05-11 10:29:27 +03001387 if (rc) {
Oded Gabbay2f553422020-08-15 16:28:10 +03001388 dev_err(hdev->dev, "Failed to get cpucp info\n");
Oded Gabbayac0ae6a2020-05-11 10:29:27 +03001389 return rc;
1390 }
1391
Oded Gabbay3c681572020-11-02 21:10:39 +02001392 if ((hdev->card_type == cpucp_card_type_pci) &&
1393 (hdev->nic_ports_mask & 0x3)) {
1394 dev_info(hdev->dev,
1395 "PCI card detected, only 8 ports are enabled\n");
1396 hdev->nic_ports_mask &= ~0x3;
1397
1398 /* Stop and disable unused NIC QMANs */
1399 WREG32(mmNIC0_QM0_GLBL_CFG1, NIC0_QM0_GLBL_CFG1_PQF_STOP_MASK |
1400 NIC0_QM0_GLBL_CFG1_CQF_STOP_MASK |
1401 NIC0_QM0_GLBL_CFG1_CP_STOP_MASK);
1402
1403 WREG32(mmNIC0_QM1_GLBL_CFG1, NIC0_QM0_GLBL_CFG1_PQF_STOP_MASK |
1404 NIC0_QM0_GLBL_CFG1_CQF_STOP_MASK |
1405 NIC0_QM0_GLBL_CFG1_CP_STOP_MASK);
1406
1407 WREG32(mmNIC0_QM0_GLBL_CFG0, 0);
1408 WREG32(mmNIC0_QM1_GLBL_CFG0, 0);
1409
1410 gaudi->hw_cap_initialized &= ~(HW_CAP_NIC0 | HW_CAP_NIC1);
1411 }
1412
Oded Gabbay2f553422020-08-15 16:28:10 +03001413 rc = hl_fw_send_pci_access_msg(hdev, CPUCP_PACKET_ENABLE_PCI_ACCESS);
Oded Gabbayac0ae6a2020-05-11 10:29:27 +03001414 if (rc) {
1415 dev_err(hdev->dev, "Failed to enable PCI access from CPU\n");
1416 return rc;
1417 }
1418
Ofir Bitton1cbca892020-10-05 11:36:00 +03001419 rc = gaudi_fetch_psoc_frequency(hdev);
1420 if (rc) {
1421 dev_err(hdev->dev, "Failed to fetch psoc frequency\n");
1422 goto disable_pci_access;
1423 }
Oded Gabbayac0ae6a2020-05-11 10:29:27 +03001424
1425 rc = gaudi_mmu_clear_pgt_range(hdev);
1426 if (rc) {
1427 dev_err(hdev->dev, "Failed to clear MMU page tables range\n");
1428 goto disable_pci_access;
1429 }
1430
1431 rc = gaudi_init_tpc_mem(hdev);
1432 if (rc) {
1433 dev_err(hdev->dev, "Failed to initialize TPC memories\n");
1434 goto disable_pci_access;
1435 }
1436
Ofir Bitton5de406c2020-09-10 10:56:26 +03001437 rc = gaudi_collective_init(hdev);
1438 if (rc) {
1439 dev_err(hdev->dev, "Failed to init collective\n");
1440 goto disable_pci_access;
1441 }
1442
Oded Gabbayac0ae6a2020-05-11 10:29:27 +03001443 return 0;
1444
1445disable_pci_access:
Oded Gabbay2f553422020-08-15 16:28:10 +03001446 hl_fw_send_pci_access_msg(hdev, CPUCP_PACKET_DISABLE_PCI_ACCESS);
Oded Gabbayac0ae6a2020-05-11 10:29:27 +03001447
1448 return rc;
1449}
1450
1451static void gaudi_late_fini(struct hl_device *hdev)
1452{
1453 const struct hwmon_channel_info **channel_info_arr;
1454 int i = 0;
1455
1456 if (!hdev->hl_chip_info->info)
1457 return;
1458
1459 channel_info_arr = hdev->hl_chip_info->info;
1460
1461 while (channel_info_arr[i]) {
1462 kfree(channel_info_arr[i]->config);
1463 kfree(channel_info_arr[i]);
1464 i++;
1465 }
1466
1467 kfree(channel_info_arr);
1468
1469 hdev->hl_chip_info->info = NULL;
1470}
1471
1472static int gaudi_alloc_cpu_accessible_dma_mem(struct hl_device *hdev)
1473{
1474 dma_addr_t dma_addr_arr[GAUDI_ALLOC_CPU_MEM_RETRY_CNT] = {}, end_addr;
1475 void *virt_addr_arr[GAUDI_ALLOC_CPU_MEM_RETRY_CNT] = {};
1476 int i, j, rc = 0;
1477
1478 /*
1479 * The device CPU works with 40-bits addresses, while bit 39 must be set
1480 * to '1' when accessing the host.
1481 * Bits 49:39 of the full host address are saved for a later
1482 * configuration of the HW to perform extension to 50 bits.
1483 * Because there is a single HW register that holds the extension bits,
1484 * these bits must be identical in all allocated range.
1485 */
1486
1487 for (i = 0 ; i < GAUDI_ALLOC_CPU_MEM_RETRY_CNT ; i++) {
1488 virt_addr_arr[i] =
1489 hdev->asic_funcs->asic_dma_alloc_coherent(hdev,
1490 HL_CPU_ACCESSIBLE_MEM_SIZE,
1491 &dma_addr_arr[i],
1492 GFP_KERNEL | __GFP_ZERO);
1493 if (!virt_addr_arr[i]) {
1494 rc = -ENOMEM;
1495 goto free_dma_mem_arr;
1496 }
1497
1498 end_addr = dma_addr_arr[i] + HL_CPU_ACCESSIBLE_MEM_SIZE - 1;
1499 if (GAUDI_CPU_PCI_MSB_ADDR(dma_addr_arr[i]) ==
1500 GAUDI_CPU_PCI_MSB_ADDR(end_addr))
1501 break;
1502 }
1503
1504 if (i == GAUDI_ALLOC_CPU_MEM_RETRY_CNT) {
1505 dev_err(hdev->dev,
1506 "MSB of CPU accessible DMA memory are not identical in all range\n");
1507 rc = -EFAULT;
1508 goto free_dma_mem_arr;
1509 }
1510
1511 hdev->cpu_accessible_dma_mem = virt_addr_arr[i];
1512 hdev->cpu_accessible_dma_address = dma_addr_arr[i];
1513 hdev->cpu_pci_msb_addr =
1514 GAUDI_CPU_PCI_MSB_ADDR(hdev->cpu_accessible_dma_address);
1515
Ofir Bittonc692dec2020-10-04 17:34:37 +03001516 if (hdev->asic_prop.fw_security_disabled)
1517 GAUDI_PCI_TO_CPU_ADDR(hdev->cpu_accessible_dma_address);
Oded Gabbayac0ae6a2020-05-11 10:29:27 +03001518
1519free_dma_mem_arr:
1520 for (j = 0 ; j < i ; j++)
1521 hdev->asic_funcs->asic_dma_free_coherent(hdev,
1522 HL_CPU_ACCESSIBLE_MEM_SIZE,
1523 virt_addr_arr[j],
1524 dma_addr_arr[j]);
1525
1526 return rc;
1527}
1528
1529static void gaudi_free_internal_qmans_pq_mem(struct hl_device *hdev)
1530{
1531 struct gaudi_device *gaudi = hdev->asic_specific;
1532 struct gaudi_internal_qman_info *q;
1533 u32 i;
1534
1535 for (i = 0 ; i < GAUDI_QUEUE_ID_SIZE ; i++) {
1536 q = &gaudi->internal_qmans[i];
1537 if (!q->pq_kernel_addr)
1538 continue;
1539 hdev->asic_funcs->asic_dma_free_coherent(hdev, q->pq_size,
1540 q->pq_kernel_addr,
1541 q->pq_dma_addr);
1542 }
1543}
1544
1545static int gaudi_alloc_internal_qmans_pq_mem(struct hl_device *hdev)
1546{
1547 struct gaudi_device *gaudi = hdev->asic_specific;
1548 struct gaudi_internal_qman_info *q;
1549 int rc, i;
1550
1551 for (i = 0 ; i < GAUDI_QUEUE_ID_SIZE ; i++) {
1552 if (gaudi_queue_type[i] != QUEUE_TYPE_INT)
1553 continue;
1554
1555 q = &gaudi->internal_qmans[i];
1556
1557 switch (i) {
Ofir Bitton0940cab2020-08-31 08:52:56 +03001558 case GAUDI_QUEUE_ID_DMA_2_0 ... GAUDI_QUEUE_ID_DMA_7_3:
Oded Gabbayac0ae6a2020-05-11 10:29:27 +03001559 q->pq_size = HBM_DMA_QMAN_SIZE_IN_BYTES;
1560 break;
1561 case GAUDI_QUEUE_ID_MME_0_0 ... GAUDI_QUEUE_ID_MME_1_3:
1562 q->pq_size = MME_QMAN_SIZE_IN_BYTES;
1563 break;
1564 case GAUDI_QUEUE_ID_TPC_0_0 ... GAUDI_QUEUE_ID_TPC_7_3:
1565 q->pq_size = TPC_QMAN_SIZE_IN_BYTES;
1566 break;
Oded Gabbay3c681572020-11-02 21:10:39 +02001567 case GAUDI_QUEUE_ID_NIC_0_0 ... GAUDI_QUEUE_ID_NIC_9_3:
1568 q->pq_size = NIC_QMAN_SIZE_IN_BYTES;
1569 break;
Oded Gabbayac0ae6a2020-05-11 10:29:27 +03001570 default:
1571 dev_err(hdev->dev, "Bad internal queue index %d", i);
1572 rc = -EINVAL;
1573 goto free_internal_qmans_pq_mem;
1574 }
1575
1576 q->pq_kernel_addr = hdev->asic_funcs->asic_dma_alloc_coherent(
1577 hdev, q->pq_size,
1578 &q->pq_dma_addr,
1579 GFP_KERNEL | __GFP_ZERO);
1580 if (!q->pq_kernel_addr) {
1581 rc = -ENOMEM;
1582 goto free_internal_qmans_pq_mem;
1583 }
1584 }
1585
1586 return 0;
1587
1588free_internal_qmans_pq_mem:
1589 gaudi_free_internal_qmans_pq_mem(hdev);
1590 return rc;
1591}
1592
1593static int gaudi_sw_init(struct hl_device *hdev)
1594{
1595 struct gaudi_device *gaudi;
Ofir Bittonebd8d122020-05-10 13:41:28 +03001596 u32 i, event_id = 0;
Oded Gabbayac0ae6a2020-05-11 10:29:27 +03001597 int rc;
1598
1599 /* Allocate device structure */
1600 gaudi = kzalloc(sizeof(*gaudi), GFP_KERNEL);
1601 if (!gaudi)
1602 return -ENOMEM;
1603
Ofir Bittonebd8d122020-05-10 13:41:28 +03001604 for (i = 0 ; i < ARRAY_SIZE(gaudi_irq_map_table) ; i++) {
1605 if (gaudi_irq_map_table[i].valid) {
1606 if (event_id == GAUDI_EVENT_SIZE) {
1607 dev_err(hdev->dev,
1608 "Event array exceeds the limit of %u events\n",
1609 GAUDI_EVENT_SIZE);
1610 rc = -EINVAL;
1611 goto free_gaudi_device;
1612 }
1613
1614 gaudi->events[event_id++] =
1615 gaudi_irq_map_table[i].fc_id;
1616 }
1617 }
1618
Oded Gabbay2f553422020-08-15 16:28:10 +03001619 gaudi->cpucp_info_get = gaudi_cpucp_info_get;
Oded Gabbayac0ae6a2020-05-11 10:29:27 +03001620
1621 gaudi->max_freq_value = GAUDI_MAX_CLK_FREQ;
1622
1623 hdev->asic_specific = gaudi;
1624
1625 /* Create DMA pool for small allocations */
1626 hdev->dma_pool = dma_pool_create(dev_name(hdev->dev),
1627 &hdev->pdev->dev, GAUDI_DMA_POOL_BLK_SIZE, 8, 0);
1628 if (!hdev->dma_pool) {
1629 dev_err(hdev->dev, "failed to create DMA pool\n");
1630 rc = -ENOMEM;
1631 goto free_gaudi_device;
1632 }
1633
1634 rc = gaudi_alloc_cpu_accessible_dma_mem(hdev);
1635 if (rc)
1636 goto free_dma_pool;
1637
1638 hdev->cpu_accessible_dma_pool = gen_pool_create(ilog2(32), -1);
1639 if (!hdev->cpu_accessible_dma_pool) {
1640 dev_err(hdev->dev,
1641 "Failed to create CPU accessible DMA pool\n");
1642 rc = -ENOMEM;
1643 goto free_cpu_dma_mem;
1644 }
1645
1646 rc = gen_pool_add(hdev->cpu_accessible_dma_pool,
1647 (uintptr_t) hdev->cpu_accessible_dma_mem,
1648 HL_CPU_ACCESSIBLE_MEM_SIZE, -1);
1649 if (rc) {
1650 dev_err(hdev->dev,
1651 "Failed to add memory to CPU accessible DMA pool\n");
1652 rc = -EFAULT;
1653 goto free_cpu_accessible_dma_pool;
1654 }
1655
1656 rc = gaudi_alloc_internal_qmans_pq_mem(hdev);
1657 if (rc)
1658 goto free_cpu_accessible_dma_pool;
1659
1660 spin_lock_init(&gaudi->hw_queues_lock);
1661 mutex_init(&gaudi->clk_gate_mutex);
1662
1663 hdev->supports_sync_stream = true;
1664 hdev->supports_coresight = true;
Ofir Bitton2795c882020-12-08 13:47:05 +02001665 hdev->supports_staged_submission = true;
Oded Gabbayac0ae6a2020-05-11 10:29:27 +03001666
1667 return 0;
1668
1669free_cpu_accessible_dma_pool:
1670 gen_pool_destroy(hdev->cpu_accessible_dma_pool);
1671free_cpu_dma_mem:
Ofir Bittonc692dec2020-10-04 17:34:37 +03001672 if (hdev->asic_prop.fw_security_disabled)
1673 GAUDI_CPU_TO_PCI_ADDR(hdev->cpu_accessible_dma_address,
1674 hdev->cpu_pci_msb_addr);
Oded Gabbayac0ae6a2020-05-11 10:29:27 +03001675 hdev->asic_funcs->asic_dma_free_coherent(hdev,
1676 HL_CPU_ACCESSIBLE_MEM_SIZE,
1677 hdev->cpu_accessible_dma_mem,
1678 hdev->cpu_accessible_dma_address);
1679free_dma_pool:
1680 dma_pool_destroy(hdev->dma_pool);
1681free_gaudi_device:
1682 kfree(gaudi);
1683 return rc;
1684}
1685
1686static int gaudi_sw_fini(struct hl_device *hdev)
1687{
1688 struct gaudi_device *gaudi = hdev->asic_specific;
1689
1690 gaudi_free_internal_qmans_pq_mem(hdev);
1691
1692 gen_pool_destroy(hdev->cpu_accessible_dma_pool);
1693
Ofir Bittonc692dec2020-10-04 17:34:37 +03001694 if (hdev->asic_prop.fw_security_disabled)
1695 GAUDI_CPU_TO_PCI_ADDR(hdev->cpu_accessible_dma_address,
Oded Gabbayac0ae6a2020-05-11 10:29:27 +03001696 hdev->cpu_pci_msb_addr);
Ofir Bittonc692dec2020-10-04 17:34:37 +03001697
Oded Gabbayac0ae6a2020-05-11 10:29:27 +03001698 hdev->asic_funcs->asic_dma_free_coherent(hdev,
1699 HL_CPU_ACCESSIBLE_MEM_SIZE,
1700 hdev->cpu_accessible_dma_mem,
1701 hdev->cpu_accessible_dma_address);
1702
1703 dma_pool_destroy(hdev->dma_pool);
1704
1705 mutex_destroy(&gaudi->clk_gate_mutex);
1706
1707 kfree(gaudi);
1708
1709 return 0;
1710}
1711
1712static irqreturn_t gaudi_irq_handler_single(int irq, void *arg)
1713{
1714 struct hl_device *hdev = arg;
1715 int i;
1716
1717 if (hdev->disabled)
1718 return IRQ_HANDLED;
1719
1720 for (i = 0 ; i < hdev->asic_prop.completion_queues_count ; i++)
1721 hl_irq_handler_cq(irq, &hdev->completion_queue[i]);
1722
1723 hl_irq_handler_eq(irq, &hdev->event_queue);
1724
1725 return IRQ_HANDLED;
1726}
1727
1728/*
1729 * For backward compatibility, new MSI interrupts should be set after the
1730 * existing CPU and NIC interrupts.
1731 */
1732static int gaudi_pci_irq_vector(struct hl_device *hdev, unsigned int nr,
1733 bool cpu_eq)
1734{
1735 int msi_vec;
1736
1737 if ((nr != GAUDI_EVENT_QUEUE_MSI_IDX) && (cpu_eq))
1738 dev_crit(hdev->dev, "CPU EQ must use IRQ %d\n",
1739 GAUDI_EVENT_QUEUE_MSI_IDX);
1740
1741 msi_vec = ((nr < GAUDI_EVENT_QUEUE_MSI_IDX) || (cpu_eq)) ? nr :
1742 (nr + NIC_NUMBER_OF_ENGINES + 1);
1743
1744 return pci_irq_vector(hdev->pdev, msi_vec);
1745}
1746
1747static int gaudi_enable_msi_single(struct hl_device *hdev)
1748{
1749 int rc, irq;
1750
Oded Gabbay3b82c342020-11-27 18:10:20 +02001751 dev_dbg(hdev->dev, "Working in single MSI IRQ mode\n");
Oded Gabbayac0ae6a2020-05-11 10:29:27 +03001752
1753 irq = gaudi_pci_irq_vector(hdev, 0, false);
1754 rc = request_irq(irq, gaudi_irq_handler_single, 0,
1755 "gaudi single msi", hdev);
1756 if (rc)
1757 dev_err(hdev->dev,
1758 "Failed to request single MSI IRQ\n");
1759
1760 return rc;
1761}
1762
1763static int gaudi_enable_msi_multi(struct hl_device *hdev)
1764{
1765 int cq_cnt = hdev->asic_prop.completion_queues_count;
1766 int rc, i, irq_cnt_init, irq;
1767
1768 for (i = 0, irq_cnt_init = 0 ; i < cq_cnt ; i++, irq_cnt_init++) {
1769 irq = gaudi_pci_irq_vector(hdev, i, false);
1770 rc = request_irq(irq, hl_irq_handler_cq, 0, gaudi_irq_name[i],
1771 &hdev->completion_queue[i]);
1772 if (rc) {
1773 dev_err(hdev->dev, "Failed to request IRQ %d", irq);
1774 goto free_irqs;
1775 }
1776 }
1777
1778 irq = gaudi_pci_irq_vector(hdev, GAUDI_EVENT_QUEUE_MSI_IDX, true);
1779 rc = request_irq(irq, hl_irq_handler_eq, 0, gaudi_irq_name[cq_cnt],
1780 &hdev->event_queue);
1781 if (rc) {
1782 dev_err(hdev->dev, "Failed to request IRQ %d", irq);
1783 goto free_irqs;
1784 }
1785
1786 return 0;
1787
1788free_irqs:
1789 for (i = 0 ; i < irq_cnt_init ; i++)
1790 free_irq(gaudi_pci_irq_vector(hdev, i, false),
1791 &hdev->completion_queue[i]);
1792 return rc;
1793}
1794
1795static int gaudi_enable_msi(struct hl_device *hdev)
1796{
1797 struct gaudi_device *gaudi = hdev->asic_specific;
1798 int rc;
1799
1800 if (gaudi->hw_cap_initialized & HW_CAP_MSI)
1801 return 0;
1802
Oded Gabbay12e66a12021-03-08 18:06:57 +02001803 rc = pci_alloc_irq_vectors(hdev->pdev, 1, 1, PCI_IRQ_MSI);
Oded Gabbayac0ae6a2020-05-11 10:29:27 +03001804 if (rc < 0) {
1805 dev_err(hdev->dev, "MSI: Failed to enable support %d\n", rc);
1806 return rc;
1807 }
1808
1809 if (rc < NUMBER_OF_INTERRUPTS) {
1810 gaudi->multi_msi_mode = false;
1811 rc = gaudi_enable_msi_single(hdev);
1812 } else {
1813 gaudi->multi_msi_mode = true;
1814 rc = gaudi_enable_msi_multi(hdev);
1815 }
1816
1817 if (rc)
1818 goto free_pci_irq_vectors;
1819
1820 gaudi->hw_cap_initialized |= HW_CAP_MSI;
1821
1822 return 0;
1823
1824free_pci_irq_vectors:
1825 pci_free_irq_vectors(hdev->pdev);
1826 return rc;
1827}
1828
1829static void gaudi_sync_irqs(struct hl_device *hdev)
1830{
1831 struct gaudi_device *gaudi = hdev->asic_specific;
1832 int i, cq_cnt = hdev->asic_prop.completion_queues_count;
1833
1834 if (!(gaudi->hw_cap_initialized & HW_CAP_MSI))
1835 return;
1836
1837 /* Wait for all pending IRQs to be finished */
1838 if (gaudi->multi_msi_mode) {
1839 for (i = 0 ; i < cq_cnt ; i++)
1840 synchronize_irq(gaudi_pci_irq_vector(hdev, i, false));
1841
1842 synchronize_irq(gaudi_pci_irq_vector(hdev,
1843 GAUDI_EVENT_QUEUE_MSI_IDX,
1844 true));
1845 } else {
1846 synchronize_irq(gaudi_pci_irq_vector(hdev, 0, false));
1847 }
1848}
1849
1850static void gaudi_disable_msi(struct hl_device *hdev)
1851{
1852 struct gaudi_device *gaudi = hdev->asic_specific;
1853 int i, irq, cq_cnt = hdev->asic_prop.completion_queues_count;
1854
1855 if (!(gaudi->hw_cap_initialized & HW_CAP_MSI))
1856 return;
1857
1858 gaudi_sync_irqs(hdev);
1859
1860 if (gaudi->multi_msi_mode) {
1861 irq = gaudi_pci_irq_vector(hdev, GAUDI_EVENT_QUEUE_MSI_IDX,
1862 true);
1863 free_irq(irq, &hdev->event_queue);
1864
1865 for (i = 0 ; i < cq_cnt ; i++) {
1866 irq = gaudi_pci_irq_vector(hdev, i, false);
1867 free_irq(irq, &hdev->completion_queue[i]);
1868 }
1869 } else {
1870 free_irq(gaudi_pci_irq_vector(hdev, 0, false), hdev);
1871 }
1872
1873 pci_free_irq_vectors(hdev->pdev);
1874
1875 gaudi->hw_cap_initialized &= ~HW_CAP_MSI;
1876}
1877
1878static void gaudi_init_scrambler_sram(struct hl_device *hdev)
1879{
1880 struct gaudi_device *gaudi = hdev->asic_specific;
1881
Ofir Bittonc692dec2020-10-04 17:34:37 +03001882 if (!hdev->asic_prop.fw_security_disabled)
1883 return;
1884
1885 if (hdev->asic_prop.fw_security_status_valid &&
1886 (hdev->asic_prop.fw_app_security_map &
1887 CPU_BOOT_DEV_STS0_SRAM_SCR_EN))
1888 return;
1889
Oded Gabbayac0ae6a2020-05-11 10:29:27 +03001890 if (gaudi->hw_cap_initialized & HW_CAP_SRAM_SCRAMBLER)
1891 return;
1892
1893 if (!hdev->sram_scrambler_enable)
1894 return;
1895
1896 WREG32(mmNIF_RTR_CTRL_0_SCRAM_SRAM_EN,
1897 1 << IF_RTR_CTRL_SCRAM_SRAM_EN_VAL_SHIFT);
1898 WREG32(mmNIF_RTR_CTRL_1_SCRAM_SRAM_EN,
1899 1 << IF_RTR_CTRL_SCRAM_SRAM_EN_VAL_SHIFT);
1900 WREG32(mmNIF_RTR_CTRL_2_SCRAM_SRAM_EN,
1901 1 << IF_RTR_CTRL_SCRAM_SRAM_EN_VAL_SHIFT);
1902 WREG32(mmNIF_RTR_CTRL_3_SCRAM_SRAM_EN,
1903 1 << IF_RTR_CTRL_SCRAM_SRAM_EN_VAL_SHIFT);
1904 WREG32(mmNIF_RTR_CTRL_4_SCRAM_SRAM_EN,
1905 1 << IF_RTR_CTRL_SCRAM_SRAM_EN_VAL_SHIFT);
1906 WREG32(mmNIF_RTR_CTRL_5_SCRAM_SRAM_EN,
1907 1 << IF_RTR_CTRL_SCRAM_SRAM_EN_VAL_SHIFT);
1908 WREG32(mmNIF_RTR_CTRL_6_SCRAM_SRAM_EN,
1909 1 << IF_RTR_CTRL_SCRAM_SRAM_EN_VAL_SHIFT);
1910 WREG32(mmNIF_RTR_CTRL_7_SCRAM_SRAM_EN,
1911 1 << IF_RTR_CTRL_SCRAM_SRAM_EN_VAL_SHIFT);
1912
1913 WREG32(mmSIF_RTR_CTRL_0_SCRAM_SRAM_EN,
1914 1 << IF_RTR_CTRL_SCRAM_SRAM_EN_VAL_SHIFT);
1915 WREG32(mmSIF_RTR_CTRL_1_SCRAM_SRAM_EN,
1916 1 << IF_RTR_CTRL_SCRAM_SRAM_EN_VAL_SHIFT);
1917 WREG32(mmSIF_RTR_CTRL_2_SCRAM_SRAM_EN,
1918 1 << IF_RTR_CTRL_SCRAM_SRAM_EN_VAL_SHIFT);
1919 WREG32(mmSIF_RTR_CTRL_3_SCRAM_SRAM_EN,
1920 1 << IF_RTR_CTRL_SCRAM_SRAM_EN_VAL_SHIFT);
1921 WREG32(mmSIF_RTR_CTRL_4_SCRAM_SRAM_EN,
1922 1 << IF_RTR_CTRL_SCRAM_SRAM_EN_VAL_SHIFT);
1923 WREG32(mmSIF_RTR_CTRL_5_SCRAM_SRAM_EN,
1924 1 << IF_RTR_CTRL_SCRAM_SRAM_EN_VAL_SHIFT);
1925 WREG32(mmSIF_RTR_CTRL_6_SCRAM_SRAM_EN,
1926 1 << IF_RTR_CTRL_SCRAM_SRAM_EN_VAL_SHIFT);
1927 WREG32(mmSIF_RTR_CTRL_7_SCRAM_SRAM_EN,
1928 1 << IF_RTR_CTRL_SCRAM_SRAM_EN_VAL_SHIFT);
1929
1930 WREG32(mmDMA_IF_E_N_DOWN_CH0_SCRAM_SRAM_EN,
1931 1 << DMA_IF_DOWN_CHX_SCRAM_SRAM_EN_VAL_SHIFT);
1932 WREG32(mmDMA_IF_E_N_DOWN_CH1_SCRAM_SRAM_EN,
1933 1 << DMA_IF_DOWN_CHX_SCRAM_SRAM_EN_VAL_SHIFT);
1934 WREG32(mmDMA_IF_E_S_DOWN_CH0_SCRAM_SRAM_EN,
1935 1 << DMA_IF_DOWN_CHX_SCRAM_SRAM_EN_VAL_SHIFT);
1936 WREG32(mmDMA_IF_E_S_DOWN_CH1_SCRAM_SRAM_EN,
1937 1 << DMA_IF_DOWN_CHX_SCRAM_SRAM_EN_VAL_SHIFT);
1938 WREG32(mmDMA_IF_W_N_DOWN_CH0_SCRAM_SRAM_EN,
1939 1 << DMA_IF_DOWN_CHX_SCRAM_SRAM_EN_VAL_SHIFT);
1940 WREG32(mmDMA_IF_W_N_DOWN_CH1_SCRAM_SRAM_EN,
1941 1 << DMA_IF_DOWN_CHX_SCRAM_SRAM_EN_VAL_SHIFT);
1942 WREG32(mmDMA_IF_W_S_DOWN_CH0_SCRAM_SRAM_EN,
1943 1 << DMA_IF_DOWN_CHX_SCRAM_SRAM_EN_VAL_SHIFT);
1944 WREG32(mmDMA_IF_W_S_DOWN_CH1_SCRAM_SRAM_EN,
1945 1 << DMA_IF_DOWN_CHX_SCRAM_SRAM_EN_VAL_SHIFT);
1946
1947 gaudi->hw_cap_initialized |= HW_CAP_SRAM_SCRAMBLER;
1948}
1949
1950static void gaudi_init_scrambler_hbm(struct hl_device *hdev)
1951{
1952 struct gaudi_device *gaudi = hdev->asic_specific;
1953
Ofir Bittonc692dec2020-10-04 17:34:37 +03001954 if (!hdev->asic_prop.fw_security_disabled)
1955 return;
1956
1957 if (hdev->asic_prop.fw_security_status_valid &&
1958 (hdev->asic_prop.fw_boot_cpu_security_map &
1959 CPU_BOOT_DEV_STS0_DRAM_SCR_EN))
1960 return;
1961
Oded Gabbayac0ae6a2020-05-11 10:29:27 +03001962 if (gaudi->hw_cap_initialized & HW_CAP_HBM_SCRAMBLER)
1963 return;
1964
1965 if (!hdev->dram_scrambler_enable)
1966 return;
1967
1968 WREG32(mmNIF_RTR_CTRL_0_SCRAM_HBM_EN,
1969 1 << IF_RTR_CTRL_SCRAM_HBM_EN_VAL_SHIFT);
1970 WREG32(mmNIF_RTR_CTRL_1_SCRAM_HBM_EN,
1971 1 << IF_RTR_CTRL_SCRAM_HBM_EN_VAL_SHIFT);
1972 WREG32(mmNIF_RTR_CTRL_2_SCRAM_HBM_EN,
1973 1 << IF_RTR_CTRL_SCRAM_HBM_EN_VAL_SHIFT);
1974 WREG32(mmNIF_RTR_CTRL_3_SCRAM_HBM_EN,
1975 1 << IF_RTR_CTRL_SCRAM_HBM_EN_VAL_SHIFT);
1976 WREG32(mmNIF_RTR_CTRL_4_SCRAM_HBM_EN,
1977 1 << IF_RTR_CTRL_SCRAM_HBM_EN_VAL_SHIFT);
1978 WREG32(mmNIF_RTR_CTRL_5_SCRAM_HBM_EN,
1979 1 << IF_RTR_CTRL_SCRAM_HBM_EN_VAL_SHIFT);
1980 WREG32(mmNIF_RTR_CTRL_6_SCRAM_HBM_EN,
1981 1 << IF_RTR_CTRL_SCRAM_HBM_EN_VAL_SHIFT);
1982 WREG32(mmNIF_RTR_CTRL_7_SCRAM_HBM_EN,
1983 1 << IF_RTR_CTRL_SCRAM_HBM_EN_VAL_SHIFT);
1984
1985 WREG32(mmSIF_RTR_CTRL_0_SCRAM_HBM_EN,
1986 1 << IF_RTR_CTRL_SCRAM_HBM_EN_VAL_SHIFT);
1987 WREG32(mmSIF_RTR_CTRL_1_SCRAM_HBM_EN,
1988 1 << IF_RTR_CTRL_SCRAM_HBM_EN_VAL_SHIFT);
1989 WREG32(mmSIF_RTR_CTRL_2_SCRAM_HBM_EN,
1990 1 << IF_RTR_CTRL_SCRAM_HBM_EN_VAL_SHIFT);
1991 WREG32(mmSIF_RTR_CTRL_3_SCRAM_HBM_EN,
1992 1 << IF_RTR_CTRL_SCRAM_HBM_EN_VAL_SHIFT);
1993 WREG32(mmSIF_RTR_CTRL_4_SCRAM_HBM_EN,
1994 1 << IF_RTR_CTRL_SCRAM_HBM_EN_VAL_SHIFT);
1995 WREG32(mmSIF_RTR_CTRL_5_SCRAM_HBM_EN,
1996 1 << IF_RTR_CTRL_SCRAM_HBM_EN_VAL_SHIFT);
1997 WREG32(mmSIF_RTR_CTRL_6_SCRAM_HBM_EN,
1998 1 << IF_RTR_CTRL_SCRAM_HBM_EN_VAL_SHIFT);
1999 WREG32(mmSIF_RTR_CTRL_7_SCRAM_HBM_EN,
2000 1 << IF_RTR_CTRL_SCRAM_HBM_EN_VAL_SHIFT);
2001
2002 WREG32(mmDMA_IF_E_N_DOWN_CH0_SCRAM_HBM_EN,
2003 1 << DMA_IF_DOWN_CHX_SCRAM_HBM_EN_VAL_SHIFT);
2004 WREG32(mmDMA_IF_E_N_DOWN_CH1_SCRAM_HBM_EN,
2005 1 << DMA_IF_DOWN_CHX_SCRAM_HBM_EN_VAL_SHIFT);
2006 WREG32(mmDMA_IF_E_S_DOWN_CH0_SCRAM_HBM_EN,
2007 1 << DMA_IF_DOWN_CHX_SCRAM_HBM_EN_VAL_SHIFT);
2008 WREG32(mmDMA_IF_E_S_DOWN_CH1_SCRAM_HBM_EN,
2009 1 << DMA_IF_DOWN_CHX_SCRAM_HBM_EN_VAL_SHIFT);
2010 WREG32(mmDMA_IF_W_N_DOWN_CH0_SCRAM_HBM_EN,
2011 1 << DMA_IF_DOWN_CHX_SCRAM_HBM_EN_VAL_SHIFT);
2012 WREG32(mmDMA_IF_W_N_DOWN_CH1_SCRAM_HBM_EN,
2013 1 << DMA_IF_DOWN_CHX_SCRAM_HBM_EN_VAL_SHIFT);
2014 WREG32(mmDMA_IF_W_S_DOWN_CH0_SCRAM_HBM_EN,
2015 1 << DMA_IF_DOWN_CHX_SCRAM_HBM_EN_VAL_SHIFT);
2016 WREG32(mmDMA_IF_W_S_DOWN_CH1_SCRAM_HBM_EN,
2017 1 << DMA_IF_DOWN_CHX_SCRAM_HBM_EN_VAL_SHIFT);
2018
2019 gaudi->hw_cap_initialized |= HW_CAP_HBM_SCRAMBLER;
2020}
2021
2022static void gaudi_init_e2e(struct hl_device *hdev)
2023{
Ofir Bittonc692dec2020-10-04 17:34:37 +03002024 if (!hdev->asic_prop.fw_security_disabled)
2025 return;
2026
2027 if (hdev->asic_prop.fw_security_status_valid &&
2028 (hdev->asic_prop.fw_boot_cpu_security_map &
2029 CPU_BOOT_DEV_STS0_E2E_CRED_EN))
2030 return;
2031
Oded Gabbayac0ae6a2020-05-11 10:29:27 +03002032 WREG32(mmSIF_RTR_CTRL_0_E2E_HBM_WR_SIZE, 247 >> 3);
2033 WREG32(mmSIF_RTR_CTRL_0_E2E_HBM_RD_SIZE, 785 >> 3);
2034 WREG32(mmSIF_RTR_CTRL_0_E2E_PCI_WR_SIZE, 49);
2035 WREG32(mmSIF_RTR_CTRL_0_E2E_PCI_RD_SIZE, 101);
2036
2037 WREG32(mmSIF_RTR_CTRL_1_E2E_HBM_WR_SIZE, 275 >> 3);
2038 WREG32(mmSIF_RTR_CTRL_1_E2E_HBM_RD_SIZE, 614 >> 3);
2039 WREG32(mmSIF_RTR_CTRL_1_E2E_PCI_WR_SIZE, 1);
2040 WREG32(mmSIF_RTR_CTRL_1_E2E_PCI_RD_SIZE, 39);
2041
2042 WREG32(mmSIF_RTR_CTRL_2_E2E_HBM_WR_SIZE, 1);
2043 WREG32(mmSIF_RTR_CTRL_2_E2E_HBM_RD_SIZE, 1);
2044 WREG32(mmSIF_RTR_CTRL_2_E2E_PCI_WR_SIZE, 1);
2045 WREG32(mmSIF_RTR_CTRL_2_E2E_PCI_RD_SIZE, 32);
2046
2047 WREG32(mmSIF_RTR_CTRL_3_E2E_HBM_WR_SIZE, 176 >> 3);
2048 WREG32(mmSIF_RTR_CTRL_3_E2E_HBM_RD_SIZE, 32 >> 3);
2049 WREG32(mmSIF_RTR_CTRL_3_E2E_PCI_WR_SIZE, 19);
2050 WREG32(mmSIF_RTR_CTRL_3_E2E_PCI_RD_SIZE, 32);
2051
2052 WREG32(mmSIF_RTR_CTRL_4_E2E_HBM_WR_SIZE, 176 >> 3);
2053 WREG32(mmSIF_RTR_CTRL_4_E2E_HBM_RD_SIZE, 32 >> 3);
2054 WREG32(mmSIF_RTR_CTRL_4_E2E_PCI_WR_SIZE, 19);
2055 WREG32(mmSIF_RTR_CTRL_4_E2E_PCI_RD_SIZE, 32);
2056
2057 WREG32(mmSIF_RTR_CTRL_5_E2E_HBM_WR_SIZE, 1);
2058 WREG32(mmSIF_RTR_CTRL_5_E2E_HBM_RD_SIZE, 1);
2059 WREG32(mmSIF_RTR_CTRL_5_E2E_PCI_WR_SIZE, 1);
2060 WREG32(mmSIF_RTR_CTRL_5_E2E_PCI_RD_SIZE, 32);
2061
2062 WREG32(mmSIF_RTR_CTRL_6_E2E_HBM_WR_SIZE, 275 >> 3);
2063 WREG32(mmSIF_RTR_CTRL_6_E2E_HBM_RD_SIZE, 614 >> 3);
2064 WREG32(mmSIF_RTR_CTRL_6_E2E_PCI_WR_SIZE, 1);
2065 WREG32(mmSIF_RTR_CTRL_6_E2E_PCI_RD_SIZE, 39);
2066
2067 WREG32(mmSIF_RTR_CTRL_7_E2E_HBM_WR_SIZE, 297 >> 3);
2068 WREG32(mmSIF_RTR_CTRL_7_E2E_HBM_RD_SIZE, 908 >> 3);
2069 WREG32(mmSIF_RTR_CTRL_7_E2E_PCI_WR_SIZE, 19);
2070 WREG32(mmSIF_RTR_CTRL_7_E2E_PCI_RD_SIZE, 19);
2071
2072 WREG32(mmNIF_RTR_CTRL_0_E2E_HBM_WR_SIZE, 318 >> 3);
2073 WREG32(mmNIF_RTR_CTRL_0_E2E_HBM_RD_SIZE, 956 >> 3);
2074 WREG32(mmNIF_RTR_CTRL_0_E2E_PCI_WR_SIZE, 79);
2075 WREG32(mmNIF_RTR_CTRL_0_E2E_PCI_RD_SIZE, 163);
2076
2077 WREG32(mmNIF_RTR_CTRL_1_E2E_HBM_WR_SIZE, 275 >> 3);
2078 WREG32(mmNIF_RTR_CTRL_1_E2E_HBM_RD_SIZE, 614 >> 3);
2079 WREG32(mmNIF_RTR_CTRL_1_E2E_PCI_WR_SIZE, 1);
2080 WREG32(mmNIF_RTR_CTRL_1_E2E_PCI_RD_SIZE, 39);
2081
2082 WREG32(mmNIF_RTR_CTRL_2_E2E_HBM_WR_SIZE, 1);
2083 WREG32(mmNIF_RTR_CTRL_2_E2E_HBM_RD_SIZE, 1);
2084 WREG32(mmNIF_RTR_CTRL_2_E2E_PCI_WR_SIZE, 1);
2085 WREG32(mmNIF_RTR_CTRL_2_E2E_PCI_RD_SIZE, 32);
2086
2087 WREG32(mmNIF_RTR_CTRL_3_E2E_HBM_WR_SIZE, 176 >> 3);
2088 WREG32(mmNIF_RTR_CTRL_3_E2E_HBM_RD_SIZE, 32 >> 3);
2089 WREG32(mmNIF_RTR_CTRL_3_E2E_PCI_WR_SIZE, 19);
2090 WREG32(mmNIF_RTR_CTRL_3_E2E_PCI_RD_SIZE, 32);
2091
2092 WREG32(mmNIF_RTR_CTRL_4_E2E_HBM_WR_SIZE, 176 >> 3);
2093 WREG32(mmNIF_RTR_CTRL_4_E2E_HBM_RD_SIZE, 32 >> 3);
2094 WREG32(mmNIF_RTR_CTRL_4_E2E_PCI_WR_SIZE, 19);
2095 WREG32(mmNIF_RTR_CTRL_4_E2E_PCI_RD_SIZE, 32);
2096
2097 WREG32(mmNIF_RTR_CTRL_5_E2E_HBM_WR_SIZE, 1);
2098 WREG32(mmNIF_RTR_CTRL_5_E2E_HBM_RD_SIZE, 1);
2099 WREG32(mmNIF_RTR_CTRL_5_E2E_PCI_WR_SIZE, 1);
2100 WREG32(mmNIF_RTR_CTRL_5_E2E_PCI_RD_SIZE, 32);
2101
2102 WREG32(mmNIF_RTR_CTRL_6_E2E_HBM_WR_SIZE, 275 >> 3);
2103 WREG32(mmNIF_RTR_CTRL_6_E2E_HBM_RD_SIZE, 614 >> 3);
2104 WREG32(mmNIF_RTR_CTRL_6_E2E_PCI_WR_SIZE, 1);
2105 WREG32(mmNIF_RTR_CTRL_6_E2E_PCI_RD_SIZE, 39);
2106
2107 WREG32(mmNIF_RTR_CTRL_7_E2E_HBM_WR_SIZE, 318 >> 3);
2108 WREG32(mmNIF_RTR_CTRL_7_E2E_HBM_RD_SIZE, 956 >> 3);
2109 WREG32(mmNIF_RTR_CTRL_7_E2E_PCI_WR_SIZE, 79);
2110 WREG32(mmNIF_RTR_CTRL_7_E2E_PCI_RD_SIZE, 79);
2111
2112 WREG32(mmDMA_IF_E_N_DOWN_CH0_E2E_HBM_WR_SIZE, 344 >> 3);
2113 WREG32(mmDMA_IF_E_N_DOWN_CH0_E2E_HBM_RD_SIZE, 1000 >> 3);
2114 WREG32(mmDMA_IF_E_N_DOWN_CH0_E2E_PCI_WR_SIZE, 162);
2115 WREG32(mmDMA_IF_E_N_DOWN_CH0_E2E_PCI_RD_SIZE, 338);
2116
2117 WREG32(mmDMA_IF_E_N_DOWN_CH1_E2E_HBM_WR_SIZE, 344 >> 3);
2118 WREG32(mmDMA_IF_E_N_DOWN_CH1_E2E_HBM_RD_SIZE, 1000 >> 3);
2119 WREG32(mmDMA_IF_E_N_DOWN_CH1_E2E_PCI_WR_SIZE, 162);
2120 WREG32(mmDMA_IF_E_N_DOWN_CH1_E2E_PCI_RD_SIZE, 338);
2121
2122 WREG32(mmDMA_IF_E_S_DOWN_CH0_E2E_HBM_WR_SIZE, 344 >> 3);
2123 WREG32(mmDMA_IF_E_S_DOWN_CH0_E2E_HBM_RD_SIZE, 1000 >> 3);
2124 WREG32(mmDMA_IF_E_S_DOWN_CH0_E2E_PCI_WR_SIZE, 162);
2125 WREG32(mmDMA_IF_E_S_DOWN_CH0_E2E_PCI_RD_SIZE, 338);
2126
2127 WREG32(mmDMA_IF_E_S_DOWN_CH1_E2E_HBM_WR_SIZE, 344 >> 3);
2128 WREG32(mmDMA_IF_E_S_DOWN_CH1_E2E_HBM_RD_SIZE, 1000 >> 3);
2129 WREG32(mmDMA_IF_E_S_DOWN_CH1_E2E_PCI_WR_SIZE, 162);
2130 WREG32(mmDMA_IF_E_S_DOWN_CH1_E2E_PCI_RD_SIZE, 338);
2131
2132 WREG32(mmDMA_IF_W_N_DOWN_CH0_E2E_HBM_WR_SIZE, 344 >> 3);
2133 WREG32(mmDMA_IF_W_N_DOWN_CH0_E2E_HBM_RD_SIZE, 1000 >> 3);
2134 WREG32(mmDMA_IF_W_N_DOWN_CH0_E2E_PCI_WR_SIZE, 162);
2135 WREG32(mmDMA_IF_W_N_DOWN_CH0_E2E_PCI_RD_SIZE, 338);
2136
2137 WREG32(mmDMA_IF_W_N_DOWN_CH1_E2E_HBM_WR_SIZE, 344 >> 3);
2138 WREG32(mmDMA_IF_W_N_DOWN_CH1_E2E_HBM_RD_SIZE, 1000 >> 3);
2139 WREG32(mmDMA_IF_W_N_DOWN_CH1_E2E_PCI_WR_SIZE, 162);
2140 WREG32(mmDMA_IF_W_N_DOWN_CH1_E2E_PCI_RD_SIZE, 338);
2141
2142 WREG32(mmDMA_IF_W_S_DOWN_CH0_E2E_HBM_WR_SIZE, 344 >> 3);
2143 WREG32(mmDMA_IF_W_S_DOWN_CH0_E2E_HBM_RD_SIZE, 1000 >> 3);
2144 WREG32(mmDMA_IF_W_S_DOWN_CH0_E2E_PCI_WR_SIZE, 162);
2145 WREG32(mmDMA_IF_W_S_DOWN_CH0_E2E_PCI_RD_SIZE, 338);
2146
2147 WREG32(mmDMA_IF_W_S_DOWN_CH1_E2E_HBM_WR_SIZE, 344 >> 3);
2148 WREG32(mmDMA_IF_W_S_DOWN_CH1_E2E_HBM_RD_SIZE, 1000 >> 3);
2149 WREG32(mmDMA_IF_W_S_DOWN_CH1_E2E_PCI_WR_SIZE, 162);
2150 WREG32(mmDMA_IF_W_S_DOWN_CH1_E2E_PCI_RD_SIZE, 338);
2151
2152 if (!hdev->dram_scrambler_enable) {
2153 WREG32(mmSIF_RTR_CTRL_0_NL_HBM_SEL_0, 0x21);
2154 WREG32(mmSIF_RTR_CTRL_0_NL_HBM_SEL_1, 0x22);
2155 WREG32(mmSIF_RTR_CTRL_0_NL_HBM_OFFSET_18, 0x1F);
2156 WREG32(mmSIF_RTR_CTRL_0_NL_HBM_PC_SEL_3, 0x20);
2157
2158 WREG32(mmSIF_RTR_CTRL_1_NL_HBM_SEL_0, 0x21);
2159 WREG32(mmSIF_RTR_CTRL_1_NL_HBM_SEL_1, 0x22);
2160 WREG32(mmSIF_RTR_CTRL_1_NL_HBM_OFFSET_18, 0x1F);
2161 WREG32(mmSIF_RTR_CTRL_1_NL_HBM_PC_SEL_3, 0x20);
2162
2163 WREG32(mmSIF_RTR_CTRL_2_NL_HBM_SEL_0, 0x21);
2164 WREG32(mmSIF_RTR_CTRL_2_NL_HBM_SEL_1, 0x22);
2165 WREG32(mmSIF_RTR_CTRL_2_NL_HBM_OFFSET_18, 0x1F);
2166 WREG32(mmSIF_RTR_CTRL_2_NL_HBM_PC_SEL_3, 0x20);
2167
2168 WREG32(mmSIF_RTR_CTRL_3_NL_HBM_SEL_0, 0x21);
2169 WREG32(mmSIF_RTR_CTRL_3_NL_HBM_SEL_1, 0x22);
2170 WREG32(mmSIF_RTR_CTRL_3_NL_HBM_OFFSET_18, 0x1F);
2171 WREG32(mmSIF_RTR_CTRL_3_NL_HBM_PC_SEL_3, 0x20);
2172
2173 WREG32(mmSIF_RTR_CTRL_4_NL_HBM_SEL_0, 0x21);
2174 WREG32(mmSIF_RTR_CTRL_4_NL_HBM_SEL_1, 0x22);
2175 WREG32(mmSIF_RTR_CTRL_4_NL_HBM_OFFSET_18, 0x1F);
2176 WREG32(mmSIF_RTR_CTRL_4_NL_HBM_PC_SEL_3, 0x20);
2177
2178 WREG32(mmSIF_RTR_CTRL_5_NL_HBM_SEL_0, 0x21);
2179 WREG32(mmSIF_RTR_CTRL_5_NL_HBM_SEL_1, 0x22);
2180 WREG32(mmSIF_RTR_CTRL_5_NL_HBM_OFFSET_18, 0x1F);
2181 WREG32(mmSIF_RTR_CTRL_5_NL_HBM_PC_SEL_3, 0x20);
2182
2183 WREG32(mmSIF_RTR_CTRL_6_NL_HBM_SEL_0, 0x21);
2184 WREG32(mmSIF_RTR_CTRL_6_NL_HBM_SEL_1, 0x22);
2185 WREG32(mmSIF_RTR_CTRL_6_NL_HBM_OFFSET_18, 0x1F);
2186 WREG32(mmSIF_RTR_CTRL_6_NL_HBM_PC_SEL_3, 0x20);
2187
2188 WREG32(mmSIF_RTR_CTRL_7_NL_HBM_SEL_0, 0x21);
2189 WREG32(mmSIF_RTR_CTRL_7_NL_HBM_SEL_1, 0x22);
2190 WREG32(mmSIF_RTR_CTRL_7_NL_HBM_OFFSET_18, 0x1F);
2191 WREG32(mmSIF_RTR_CTRL_7_NL_HBM_PC_SEL_3, 0x20);
2192
2193 WREG32(mmNIF_RTR_CTRL_0_NL_HBM_SEL_0, 0x21);
2194 WREG32(mmNIF_RTR_CTRL_0_NL_HBM_SEL_1, 0x22);
2195 WREG32(mmNIF_RTR_CTRL_0_NL_HBM_OFFSET_18, 0x1F);
2196 WREG32(mmNIF_RTR_CTRL_0_NL_HBM_PC_SEL_3, 0x20);
2197
2198 WREG32(mmNIF_RTR_CTRL_1_NL_HBM_SEL_0, 0x21);
2199 WREG32(mmNIF_RTR_CTRL_1_NL_HBM_SEL_1, 0x22);
2200 WREG32(mmNIF_RTR_CTRL_1_NL_HBM_OFFSET_18, 0x1F);
2201 WREG32(mmNIF_RTR_CTRL_1_NL_HBM_PC_SEL_3, 0x20);
2202
2203 WREG32(mmNIF_RTR_CTRL_2_NL_HBM_SEL_0, 0x21);
2204 WREG32(mmNIF_RTR_CTRL_2_NL_HBM_SEL_1, 0x22);
2205 WREG32(mmNIF_RTR_CTRL_2_NL_HBM_OFFSET_18, 0x1F);
2206 WREG32(mmNIF_RTR_CTRL_2_NL_HBM_PC_SEL_3, 0x20);
2207
2208 WREG32(mmNIF_RTR_CTRL_3_NL_HBM_SEL_0, 0x21);
2209 WREG32(mmNIF_RTR_CTRL_3_NL_HBM_SEL_1, 0x22);
2210 WREG32(mmNIF_RTR_CTRL_3_NL_HBM_OFFSET_18, 0x1F);
2211 WREG32(mmNIF_RTR_CTRL_3_NL_HBM_PC_SEL_3, 0x20);
2212
2213 WREG32(mmNIF_RTR_CTRL_4_NL_HBM_SEL_0, 0x21);
2214 WREG32(mmNIF_RTR_CTRL_4_NL_HBM_SEL_1, 0x22);
2215 WREG32(mmNIF_RTR_CTRL_4_NL_HBM_OFFSET_18, 0x1F);
2216 WREG32(mmNIF_RTR_CTRL_4_NL_HBM_PC_SEL_3, 0x20);
2217
2218 WREG32(mmNIF_RTR_CTRL_5_NL_HBM_SEL_0, 0x21);
2219 WREG32(mmNIF_RTR_CTRL_5_NL_HBM_SEL_1, 0x22);
2220 WREG32(mmNIF_RTR_CTRL_5_NL_HBM_OFFSET_18, 0x1F);
2221 WREG32(mmNIF_RTR_CTRL_5_NL_HBM_PC_SEL_3, 0x20);
2222
2223 WREG32(mmNIF_RTR_CTRL_6_NL_HBM_SEL_0, 0x21);
2224 WREG32(mmNIF_RTR_CTRL_6_NL_HBM_SEL_1, 0x22);
2225 WREG32(mmNIF_RTR_CTRL_6_NL_HBM_OFFSET_18, 0x1F);
2226 WREG32(mmNIF_RTR_CTRL_6_NL_HBM_PC_SEL_3, 0x20);
2227
2228 WREG32(mmNIF_RTR_CTRL_7_NL_HBM_SEL_0, 0x21);
2229 WREG32(mmNIF_RTR_CTRL_7_NL_HBM_SEL_1, 0x22);
2230 WREG32(mmNIF_RTR_CTRL_7_NL_HBM_OFFSET_18, 0x1F);
2231 WREG32(mmNIF_RTR_CTRL_7_NL_HBM_PC_SEL_3, 0x20);
2232
2233 WREG32(mmDMA_IF_E_N_DOWN_CH0_NL_HBM_SEL_0, 0x21);
2234 WREG32(mmDMA_IF_E_N_DOWN_CH0_NL_HBM_SEL_1, 0x22);
2235 WREG32(mmDMA_IF_E_N_DOWN_CH0_NL_HBM_OFFSET_18, 0x1F);
2236 WREG32(mmDMA_IF_E_N_DOWN_CH0_NL_HBM_PC_SEL_3, 0x20);
2237
2238 WREG32(mmDMA_IF_E_N_DOWN_CH1_NL_HBM_SEL_0, 0x21);
2239 WREG32(mmDMA_IF_E_N_DOWN_CH1_NL_HBM_SEL_1, 0x22);
2240 WREG32(mmDMA_IF_E_N_DOWN_CH1_NL_HBM_OFFSET_18, 0x1F);
2241 WREG32(mmDMA_IF_E_N_DOWN_CH1_NL_HBM_PC_SEL_3, 0x20);
2242
2243 WREG32(mmDMA_IF_E_S_DOWN_CH0_NL_HBM_SEL_0, 0x21);
2244 WREG32(mmDMA_IF_E_S_DOWN_CH0_NL_HBM_SEL_1, 0x22);
2245 WREG32(mmDMA_IF_E_S_DOWN_CH0_NL_HBM_OFFSET_18, 0x1F);
2246 WREG32(mmDMA_IF_E_S_DOWN_CH0_NL_HBM_PC_SEL_3, 0x20);
2247
2248 WREG32(mmDMA_IF_E_S_DOWN_CH1_NL_HBM_SEL_0, 0x21);
2249 WREG32(mmDMA_IF_E_S_DOWN_CH1_NL_HBM_SEL_1, 0x22);
2250 WREG32(mmDMA_IF_E_S_DOWN_CH1_NL_HBM_OFFSET_18, 0x1F);
2251 WREG32(mmDMA_IF_E_S_DOWN_CH1_NL_HBM_PC_SEL_3, 0x20);
2252
2253 WREG32(mmDMA_IF_W_N_DOWN_CH0_NL_HBM_SEL_0, 0x21);
2254 WREG32(mmDMA_IF_W_N_DOWN_CH0_NL_HBM_SEL_1, 0x22);
2255 WREG32(mmDMA_IF_W_N_DOWN_CH0_NL_HBM_OFFSET_18, 0x1F);
2256 WREG32(mmDMA_IF_W_N_DOWN_CH0_NL_HBM_PC_SEL_3, 0x20);
2257
2258 WREG32(mmDMA_IF_W_N_DOWN_CH1_NL_HBM_SEL_0, 0x21);
2259 WREG32(mmDMA_IF_W_N_DOWN_CH1_NL_HBM_SEL_1, 0x22);
2260 WREG32(mmDMA_IF_W_N_DOWN_CH1_NL_HBM_OFFSET_18, 0x1F);
2261 WREG32(mmDMA_IF_W_N_DOWN_CH1_NL_HBM_PC_SEL_3, 0x20);
2262
2263 WREG32(mmDMA_IF_W_S_DOWN_CH0_NL_HBM_SEL_0, 0x21);
2264 WREG32(mmDMA_IF_W_S_DOWN_CH0_NL_HBM_SEL_1, 0x22);
2265 WREG32(mmDMA_IF_W_S_DOWN_CH0_NL_HBM_OFFSET_18, 0x1F);
2266 WREG32(mmDMA_IF_W_S_DOWN_CH0_NL_HBM_PC_SEL_3, 0x20);
2267
2268 WREG32(mmDMA_IF_W_S_DOWN_CH1_NL_HBM_SEL_0, 0x21);
2269 WREG32(mmDMA_IF_W_S_DOWN_CH1_NL_HBM_SEL_1, 0x22);
2270 WREG32(mmDMA_IF_W_S_DOWN_CH1_NL_HBM_OFFSET_18, 0x1F);
2271 WREG32(mmDMA_IF_W_S_DOWN_CH1_NL_HBM_PC_SEL_3, 0x20);
2272 }
2273
2274 WREG32(mmSIF_RTR_CTRL_0_E2E_HBM_EN,
2275 1 << IF_RTR_CTRL_E2E_HBM_EN_VAL_SHIFT);
2276 WREG32(mmSIF_RTR_CTRL_0_E2E_PCI_EN,
2277 1 << IF_RTR_CTRL_E2E_PCI_EN_VAL_SHIFT);
2278
2279 WREG32(mmSIF_RTR_CTRL_1_E2E_HBM_EN,
2280 1 << IF_RTR_CTRL_E2E_HBM_EN_VAL_SHIFT);
2281 WREG32(mmSIF_RTR_CTRL_1_E2E_PCI_EN,
2282 1 << IF_RTR_CTRL_E2E_PCI_EN_VAL_SHIFT);
2283
2284 WREG32(mmSIF_RTR_CTRL_2_E2E_HBM_EN,
2285 1 << IF_RTR_CTRL_E2E_HBM_EN_VAL_SHIFT);
2286 WREG32(mmSIF_RTR_CTRL_2_E2E_PCI_EN,
2287 1 << IF_RTR_CTRL_E2E_PCI_EN_VAL_SHIFT);
2288
2289 WREG32(mmSIF_RTR_CTRL_3_E2E_HBM_EN,
2290 1 << IF_RTR_CTRL_E2E_HBM_EN_VAL_SHIFT);
2291 WREG32(mmSIF_RTR_CTRL_3_E2E_PCI_EN,
2292 1 << IF_RTR_CTRL_E2E_PCI_EN_VAL_SHIFT);
2293
2294 WREG32(mmSIF_RTR_CTRL_4_E2E_HBM_EN,
2295 1 << IF_RTR_CTRL_E2E_HBM_EN_VAL_SHIFT);
2296 WREG32(mmSIF_RTR_CTRL_4_E2E_PCI_EN,
2297 1 << IF_RTR_CTRL_E2E_PCI_EN_VAL_SHIFT);
2298
2299 WREG32(mmSIF_RTR_CTRL_5_E2E_HBM_EN,
2300 1 << IF_RTR_CTRL_E2E_HBM_EN_VAL_SHIFT);
2301 WREG32(mmSIF_RTR_CTRL_5_E2E_PCI_EN,
2302 1 << IF_RTR_CTRL_E2E_PCI_EN_VAL_SHIFT);
2303
2304 WREG32(mmSIF_RTR_CTRL_6_E2E_HBM_EN,
2305 1 << IF_RTR_CTRL_E2E_HBM_EN_VAL_SHIFT);
2306 WREG32(mmSIF_RTR_CTRL_6_E2E_PCI_EN,
2307 1 << IF_RTR_CTRL_E2E_PCI_EN_VAL_SHIFT);
2308
2309 WREG32(mmSIF_RTR_CTRL_7_E2E_HBM_EN,
2310 1 << IF_RTR_CTRL_E2E_HBM_EN_VAL_SHIFT);
2311 WREG32(mmSIF_RTR_CTRL_7_E2E_PCI_EN,
2312 1 << IF_RTR_CTRL_E2E_PCI_EN_VAL_SHIFT);
2313
2314 WREG32(mmNIF_RTR_CTRL_0_E2E_HBM_EN,
2315 1 << IF_RTR_CTRL_E2E_HBM_EN_VAL_SHIFT);
2316 WREG32(mmNIF_RTR_CTRL_0_E2E_PCI_EN,
2317 1 << IF_RTR_CTRL_E2E_PCI_EN_VAL_SHIFT);
2318
2319 WREG32(mmNIF_RTR_CTRL_1_E2E_HBM_EN,
2320 1 << IF_RTR_CTRL_E2E_HBM_EN_VAL_SHIFT);
2321 WREG32(mmNIF_RTR_CTRL_1_E2E_PCI_EN,
2322 1 << IF_RTR_CTRL_E2E_PCI_EN_VAL_SHIFT);
2323
2324 WREG32(mmNIF_RTR_CTRL_2_E2E_HBM_EN,
2325 1 << IF_RTR_CTRL_E2E_HBM_EN_VAL_SHIFT);
2326 WREG32(mmNIF_RTR_CTRL_2_E2E_PCI_EN,
2327 1 << IF_RTR_CTRL_E2E_PCI_EN_VAL_SHIFT);
2328
2329 WREG32(mmNIF_RTR_CTRL_3_E2E_HBM_EN,
2330 1 << IF_RTR_CTRL_E2E_HBM_EN_VAL_SHIFT);
2331 WREG32(mmNIF_RTR_CTRL_3_E2E_PCI_EN,
2332 1 << IF_RTR_CTRL_E2E_PCI_EN_VAL_SHIFT);
2333
2334 WREG32(mmNIF_RTR_CTRL_4_E2E_HBM_EN,
2335 1 << IF_RTR_CTRL_E2E_HBM_EN_VAL_SHIFT);
2336 WREG32(mmNIF_RTR_CTRL_4_E2E_PCI_EN,
2337 1 << IF_RTR_CTRL_E2E_PCI_EN_VAL_SHIFT);
2338
2339 WREG32(mmNIF_RTR_CTRL_5_E2E_HBM_EN,
2340 1 << IF_RTR_CTRL_E2E_HBM_EN_VAL_SHIFT);
2341 WREG32(mmNIF_RTR_CTRL_5_E2E_PCI_EN,
2342 1 << IF_RTR_CTRL_E2E_PCI_EN_VAL_SHIFT);
2343
2344 WREG32(mmNIF_RTR_CTRL_6_E2E_HBM_EN,
2345 1 << IF_RTR_CTRL_E2E_HBM_EN_VAL_SHIFT);
2346 WREG32(mmNIF_RTR_CTRL_6_E2E_PCI_EN,
2347 1 << IF_RTR_CTRL_E2E_PCI_EN_VAL_SHIFT);
2348
2349 WREG32(mmNIF_RTR_CTRL_7_E2E_HBM_EN,
2350 1 << IF_RTR_CTRL_E2E_HBM_EN_VAL_SHIFT);
2351 WREG32(mmNIF_RTR_CTRL_7_E2E_PCI_EN,
2352 1 << IF_RTR_CTRL_E2E_PCI_EN_VAL_SHIFT);
2353
2354 WREG32(mmDMA_IF_E_N_DOWN_CH0_E2E_HBM_EN,
2355 1 << DMA_IF_DOWN_CHX_E2E_HBM_EN_VAL_SHIFT);
2356 WREG32(mmDMA_IF_E_N_DOWN_CH0_E2E_PCI_EN,
2357 1 << DMA_IF_DOWN_CHX_E2E_PCI_EN_VAL_SHIFT);
2358
2359 WREG32(mmDMA_IF_E_N_DOWN_CH1_E2E_HBM_EN,
2360 1 << DMA_IF_DOWN_CHX_E2E_HBM_EN_VAL_SHIFT);
2361 WREG32(mmDMA_IF_E_N_DOWN_CH1_E2E_PCI_EN,
2362 1 << DMA_IF_DOWN_CHX_E2E_PCI_EN_VAL_SHIFT);
2363
2364 WREG32(mmDMA_IF_E_S_DOWN_CH0_E2E_HBM_EN,
2365 1 << DMA_IF_DOWN_CHX_E2E_HBM_EN_VAL_SHIFT);
2366 WREG32(mmDMA_IF_E_S_DOWN_CH0_E2E_PCI_EN,
2367 1 << DMA_IF_DOWN_CHX_E2E_PCI_EN_VAL_SHIFT);
2368
2369 WREG32(mmDMA_IF_E_S_DOWN_CH1_E2E_HBM_EN,
2370 1 << DMA_IF_DOWN_CHX_E2E_HBM_EN_VAL_SHIFT);
2371 WREG32(mmDMA_IF_E_S_DOWN_CH1_E2E_PCI_EN,
2372 1 << DMA_IF_DOWN_CHX_E2E_PCI_EN_VAL_SHIFT);
2373
2374 WREG32(mmDMA_IF_W_N_DOWN_CH0_E2E_HBM_EN,
2375 1 << DMA_IF_DOWN_CHX_E2E_HBM_EN_VAL_SHIFT);
2376 WREG32(mmDMA_IF_W_N_DOWN_CH0_E2E_PCI_EN,
2377 1 << DMA_IF_DOWN_CHX_E2E_PCI_EN_VAL_SHIFT);
2378
2379 WREG32(mmDMA_IF_W_N_DOWN_CH1_E2E_HBM_EN,
2380 1 << DMA_IF_DOWN_CHX_E2E_HBM_EN_VAL_SHIFT);
2381 WREG32(mmDMA_IF_W_N_DOWN_CH1_E2E_PCI_EN,
2382 1 << DMA_IF_DOWN_CHX_E2E_PCI_EN_VAL_SHIFT);
2383
2384 WREG32(mmDMA_IF_W_S_DOWN_CH0_E2E_HBM_EN,
2385 1 << DMA_IF_DOWN_CHX_E2E_HBM_EN_VAL_SHIFT);
2386 WREG32(mmDMA_IF_W_S_DOWN_CH0_E2E_PCI_EN,
2387 1 << DMA_IF_DOWN_CHX_E2E_PCI_EN_VAL_SHIFT);
2388
2389 WREG32(mmDMA_IF_W_S_DOWN_CH1_E2E_HBM_EN,
2390 1 << DMA_IF_DOWN_CHX_E2E_HBM_EN_VAL_SHIFT);
2391 WREG32(mmDMA_IF_W_S_DOWN_CH1_E2E_PCI_EN,
2392 1 << DMA_IF_DOWN_CHX_E2E_PCI_EN_VAL_SHIFT);
2393}
2394
2395static void gaudi_init_hbm_cred(struct hl_device *hdev)
2396{
2397 uint32_t hbm0_wr, hbm1_wr, hbm0_rd, hbm1_rd;
2398
Ofir Bittonc692dec2020-10-04 17:34:37 +03002399 if (!hdev->asic_prop.fw_security_disabled)
2400 return;
2401
2402 if (hdev->asic_prop.fw_security_status_valid &&
2403 (hdev->asic_prop.fw_boot_cpu_security_map &
2404 CPU_BOOT_DEV_STS0_HBM_CRED_EN))
2405 return;
2406
Oded Gabbayac0ae6a2020-05-11 10:29:27 +03002407 hbm0_wr = 0x33333333;
Oded Gabbayac0ae6a2020-05-11 10:29:27 +03002408 hbm0_rd = 0x77777777;
Oded Gabbay0b168c82020-06-15 19:25:57 +03002409 hbm1_wr = 0x55555555;
Oded Gabbayac0ae6a2020-05-11 10:29:27 +03002410 hbm1_rd = 0xDDDDDDDD;
2411
2412 WREG32(mmDMA_IF_E_N_HBM0_WR_CRED_CNT, hbm0_wr);
2413 WREG32(mmDMA_IF_E_N_HBM1_WR_CRED_CNT, hbm1_wr);
2414 WREG32(mmDMA_IF_E_N_HBM0_RD_CRED_CNT, hbm0_rd);
2415 WREG32(mmDMA_IF_E_N_HBM1_RD_CRED_CNT, hbm1_rd);
2416
2417 WREG32(mmDMA_IF_E_S_HBM0_WR_CRED_CNT, hbm0_wr);
2418 WREG32(mmDMA_IF_E_S_HBM1_WR_CRED_CNT, hbm1_wr);
2419 WREG32(mmDMA_IF_E_S_HBM0_RD_CRED_CNT, hbm0_rd);
2420 WREG32(mmDMA_IF_E_S_HBM1_RD_CRED_CNT, hbm1_rd);
2421
2422 WREG32(mmDMA_IF_W_N_HBM0_WR_CRED_CNT, hbm0_wr);
2423 WREG32(mmDMA_IF_W_N_HBM1_WR_CRED_CNT, hbm1_wr);
2424 WREG32(mmDMA_IF_W_N_HBM0_RD_CRED_CNT, hbm0_rd);
2425 WREG32(mmDMA_IF_W_N_HBM1_RD_CRED_CNT, hbm1_rd);
2426
2427 WREG32(mmDMA_IF_W_S_HBM0_WR_CRED_CNT, hbm0_wr);
2428 WREG32(mmDMA_IF_W_S_HBM1_WR_CRED_CNT, hbm1_wr);
2429 WREG32(mmDMA_IF_W_S_HBM0_RD_CRED_CNT, hbm0_rd);
2430 WREG32(mmDMA_IF_W_S_HBM1_RD_CRED_CNT, hbm1_rd);
2431
2432 WREG32(mmDMA_IF_E_N_HBM_CRED_EN_0,
2433 (1 << DMA_IF_HBM_CRED_EN_READ_CREDIT_EN_SHIFT) |
2434 (1 << DMA_IF_HBM_CRED_EN_WRITE_CREDIT_EN_SHIFT));
2435 WREG32(mmDMA_IF_E_S_HBM_CRED_EN_0,
2436 (1 << DMA_IF_HBM_CRED_EN_READ_CREDIT_EN_SHIFT) |
2437 (1 << DMA_IF_HBM_CRED_EN_WRITE_CREDIT_EN_SHIFT));
2438 WREG32(mmDMA_IF_W_N_HBM_CRED_EN_0,
2439 (1 << DMA_IF_HBM_CRED_EN_READ_CREDIT_EN_SHIFT) |
2440 (1 << DMA_IF_HBM_CRED_EN_WRITE_CREDIT_EN_SHIFT));
2441 WREG32(mmDMA_IF_W_S_HBM_CRED_EN_0,
2442 (1 << DMA_IF_HBM_CRED_EN_READ_CREDIT_EN_SHIFT) |
2443 (1 << DMA_IF_HBM_CRED_EN_WRITE_CREDIT_EN_SHIFT));
2444
2445 WREG32(mmDMA_IF_E_N_HBM_CRED_EN_1,
2446 (1 << DMA_IF_HBM_CRED_EN_READ_CREDIT_EN_SHIFT) |
2447 (1 << DMA_IF_HBM_CRED_EN_WRITE_CREDIT_EN_SHIFT));
2448 WREG32(mmDMA_IF_E_S_HBM_CRED_EN_1,
2449 (1 << DMA_IF_HBM_CRED_EN_READ_CREDIT_EN_SHIFT) |
2450 (1 << DMA_IF_HBM_CRED_EN_WRITE_CREDIT_EN_SHIFT));
2451 WREG32(mmDMA_IF_W_N_HBM_CRED_EN_1,
2452 (1 << DMA_IF_HBM_CRED_EN_READ_CREDIT_EN_SHIFT) |
2453 (1 << DMA_IF_HBM_CRED_EN_WRITE_CREDIT_EN_SHIFT));
2454 WREG32(mmDMA_IF_W_S_HBM_CRED_EN_1,
2455 (1 << DMA_IF_HBM_CRED_EN_READ_CREDIT_EN_SHIFT) |
2456 (1 << DMA_IF_HBM_CRED_EN_WRITE_CREDIT_EN_SHIFT));
2457}
2458
Oded Gabbayac0ae6a2020-05-11 10:29:27 +03002459static void gaudi_init_golden_registers(struct hl_device *hdev)
2460{
2461 u32 tpc_offset;
2462 int tpc_id, i;
2463
2464 gaudi_init_e2e(hdev);
Oded Gabbayac0ae6a2020-05-11 10:29:27 +03002465 gaudi_init_hbm_cred(hdev);
2466
Oded Gabbayac0ae6a2020-05-11 10:29:27 +03002467 for (tpc_id = 0, tpc_offset = 0;
2468 tpc_id < TPC_NUMBER_OF_ENGINES;
2469 tpc_id++, tpc_offset += TPC_CFG_OFFSET) {
2470 /* Mask all arithmetic interrupts from TPC */
2471 WREG32(mmTPC0_CFG_TPC_INTR_MASK + tpc_offset, 0x8FFF);
2472 /* Set 16 cache lines */
2473 WREG32_FIELD(TPC0_CFG_MSS_CONFIG, tpc_offset,
2474 ICACHE_FETCH_LINE_NUM, 2);
2475 }
2476
2477 /* Make sure 1st 128 bytes in SRAM are 0 for Tensor DMA */
2478 for (i = 0 ; i < 128 ; i += 8)
2479 writeq(0, hdev->pcie_bar[SRAM_BAR_ID] + i);
2480
2481 WREG32(mmMME0_CTRL_EUS_ROLLUP_CNT_ADD, 3);
2482 WREG32(mmMME1_CTRL_EUS_ROLLUP_CNT_ADD, 3);
2483 WREG32(mmMME2_CTRL_EUS_ROLLUP_CNT_ADD, 3);
2484 WREG32(mmMME3_CTRL_EUS_ROLLUP_CNT_ADD, 3);
Oded Gabbayac0ae6a2020-05-11 10:29:27 +03002485}
2486
2487static void gaudi_init_pci_dma_qman(struct hl_device *hdev, int dma_id,
2488 int qman_id, dma_addr_t qman_pq_addr)
2489{
2490 u32 mtr_base_en_lo, mtr_base_en_hi, mtr_base_ws_lo, mtr_base_ws_hi;
2491 u32 so_base_en_lo, so_base_en_hi, so_base_ws_lo, so_base_ws_hi;
2492 u32 q_off, dma_qm_offset;
2493 u32 dma_qm_err_cfg;
2494
2495 dma_qm_offset = dma_id * DMA_QMAN_OFFSET;
2496
2497 mtr_base_en_lo = lower_32_bits(CFG_BASE +
2498 mmSYNC_MNGR_E_N_SYNC_MNGR_OBJS_MON_PAY_ADDRL_0);
2499 mtr_base_en_hi = upper_32_bits(CFG_BASE +
2500 mmSYNC_MNGR_E_N_SYNC_MNGR_OBJS_MON_PAY_ADDRL_0);
2501 so_base_en_lo = lower_32_bits(CFG_BASE +
2502 mmSYNC_MNGR_E_N_SYNC_MNGR_OBJS_SOB_OBJ_0);
2503 so_base_en_hi = upper_32_bits(CFG_BASE +
2504 mmSYNC_MNGR_E_N_SYNC_MNGR_OBJS_SOB_OBJ_0);
2505 mtr_base_ws_lo = lower_32_bits(CFG_BASE +
2506 mmSYNC_MNGR_W_S_SYNC_MNGR_OBJS_MON_PAY_ADDRL_0);
2507 mtr_base_ws_hi = upper_32_bits(CFG_BASE +
2508 mmSYNC_MNGR_W_S_SYNC_MNGR_OBJS_MON_PAY_ADDRL_0);
2509 so_base_ws_lo = lower_32_bits(CFG_BASE +
2510 mmSYNC_MNGR_W_S_SYNC_MNGR_OBJS_SOB_OBJ_0);
2511 so_base_ws_hi = upper_32_bits(CFG_BASE +
2512 mmSYNC_MNGR_W_S_SYNC_MNGR_OBJS_SOB_OBJ_0);
2513
2514 q_off = dma_qm_offset + qman_id * 4;
2515
2516 WREG32(mmDMA0_QM_PQ_BASE_LO_0 + q_off, lower_32_bits(qman_pq_addr));
2517 WREG32(mmDMA0_QM_PQ_BASE_HI_0 + q_off, upper_32_bits(qman_pq_addr));
2518
2519 WREG32(mmDMA0_QM_PQ_SIZE_0 + q_off, ilog2(HL_QUEUE_LENGTH));
2520 WREG32(mmDMA0_QM_PQ_PI_0 + q_off, 0);
2521 WREG32(mmDMA0_QM_PQ_CI_0 + q_off, 0);
2522
Ofir Bitton25121d92020-09-24 08:22:58 +03002523 WREG32(mmDMA0_QM_CP_LDMA_TSIZE_OFFSET_0 + q_off, QMAN_LDMA_SIZE_OFFSET);
2524 WREG32(mmDMA0_QM_CP_LDMA_SRC_BASE_LO_OFFSET_0 + q_off,
2525 QMAN_LDMA_SRC_OFFSET);
2526 WREG32(mmDMA0_QM_CP_LDMA_DST_BASE_LO_OFFSET_0 + q_off,
2527 QMAN_LDMA_DST_OFFSET);
Oded Gabbayac0ae6a2020-05-11 10:29:27 +03002528
2529 WREG32(mmDMA0_QM_CP_MSG_BASE0_ADDR_LO_0 + q_off, mtr_base_en_lo);
2530 WREG32(mmDMA0_QM_CP_MSG_BASE0_ADDR_HI_0 + q_off, mtr_base_en_hi);
2531 WREG32(mmDMA0_QM_CP_MSG_BASE1_ADDR_LO_0 + q_off, so_base_en_lo);
2532 WREG32(mmDMA0_QM_CP_MSG_BASE1_ADDR_HI_0 + q_off, so_base_en_hi);
2533 WREG32(mmDMA0_QM_CP_MSG_BASE2_ADDR_LO_0 + q_off, mtr_base_ws_lo);
2534 WREG32(mmDMA0_QM_CP_MSG_BASE2_ADDR_HI_0 + q_off, mtr_base_ws_hi);
2535 WREG32(mmDMA0_QM_CP_MSG_BASE3_ADDR_LO_0 + q_off, so_base_ws_lo);
2536 WREG32(mmDMA0_QM_CP_MSG_BASE3_ADDR_HI_0 + q_off, so_base_ws_hi);
2537
Omer Shpigelmance043262020-06-16 17:56:27 +03002538 WREG32(mmDMA0_QM_CP_BARRIER_CFG_0 + q_off, 0x100);
2539
Oded Gabbayac0ae6a2020-05-11 10:29:27 +03002540 /* The following configuration is needed only once per QMAN */
2541 if (qman_id == 0) {
2542 /* Configure RAZWI IRQ */
2543 dma_qm_err_cfg = PCI_DMA_QMAN_GLBL_ERR_CFG_MSG_EN_MASK;
2544 if (hdev->stop_on_err) {
2545 dma_qm_err_cfg |=
2546 PCI_DMA_QMAN_GLBL_ERR_CFG_STOP_ON_ERR_EN_MASK;
2547 }
2548
2549 WREG32(mmDMA0_QM_GLBL_ERR_CFG + dma_qm_offset, dma_qm_err_cfg);
2550 WREG32(mmDMA0_QM_GLBL_ERR_ADDR_LO + dma_qm_offset,
2551 lower_32_bits(CFG_BASE +
2552 mmGIC_DISTRIBUTOR__5_GICD_SETSPI_NSR));
2553 WREG32(mmDMA0_QM_GLBL_ERR_ADDR_HI + dma_qm_offset,
2554 upper_32_bits(CFG_BASE +
2555 mmGIC_DISTRIBUTOR__5_GICD_SETSPI_NSR));
2556 WREG32(mmDMA0_QM_GLBL_ERR_WDATA + dma_qm_offset,
2557 gaudi_irq_map_table[GAUDI_EVENT_DMA0_QM].cpu_id +
2558 dma_id);
2559
2560 WREG32(mmDMA0_QM_ARB_ERR_MSG_EN + dma_qm_offset,
2561 QM_ARB_ERR_MSG_EN_MASK);
2562
2563 /* Increase ARB WDT to support streams architecture */
2564 WREG32(mmDMA0_QM_ARB_SLV_CHOISE_WDT + dma_qm_offset,
2565 GAUDI_ARB_WDT_TIMEOUT);
2566
2567 WREG32(mmDMA0_QM_GLBL_PROT + dma_qm_offset,
2568 QMAN_EXTERNAL_MAKE_TRUSTED);
2569
2570 WREG32(mmDMA0_QM_GLBL_CFG1 + dma_qm_offset, 0);
2571 }
2572}
2573
2574static void gaudi_init_dma_core(struct hl_device *hdev, int dma_id)
2575{
2576 u32 dma_offset = dma_id * DMA_CORE_OFFSET;
2577 u32 dma_err_cfg = 1 << DMA0_CORE_ERR_CFG_ERR_MSG_EN_SHIFT;
2578
2579 /* Set to maximum possible according to physical size */
2580 WREG32(mmDMA0_CORE_RD_MAX_OUTSTAND + dma_offset, 0);
2581 WREG32(mmDMA0_CORE_RD_MAX_SIZE + dma_offset, 0);
2582
Oded Gabbayd1f36332020-09-14 09:26:54 +03002583 /* WA for H/W bug H3-2116 */
2584 WREG32(mmDMA0_CORE_LBW_MAX_OUTSTAND + dma_offset, 15);
2585
Oded Gabbayac0ae6a2020-05-11 10:29:27 +03002586 /* STOP_ON bit implies no completion to operation in case of RAZWI */
2587 if (hdev->stop_on_err)
2588 dma_err_cfg |= 1 << DMA0_CORE_ERR_CFG_STOP_ON_ERR_SHIFT;
2589
2590 WREG32(mmDMA0_CORE_ERR_CFG + dma_offset, dma_err_cfg);
2591 WREG32(mmDMA0_CORE_ERRMSG_ADDR_LO + dma_offset,
2592 lower_32_bits(CFG_BASE + mmGIC_DISTRIBUTOR__5_GICD_SETSPI_NSR));
2593 WREG32(mmDMA0_CORE_ERRMSG_ADDR_HI + dma_offset,
2594 upper_32_bits(CFG_BASE + mmGIC_DISTRIBUTOR__5_GICD_SETSPI_NSR));
2595 WREG32(mmDMA0_CORE_ERRMSG_WDATA + dma_offset,
2596 gaudi_irq_map_table[GAUDI_EVENT_DMA0_CORE].cpu_id + dma_id);
2597 WREG32(mmDMA0_CORE_PROT + dma_offset,
2598 1 << DMA0_CORE_PROT_ERR_VAL_SHIFT);
2599 /* If the channel is secured, it should be in MMU bypass mode */
2600 WREG32(mmDMA0_CORE_SECURE_PROPS + dma_offset,
2601 1 << DMA0_CORE_SECURE_PROPS_MMBP_SHIFT);
2602 WREG32(mmDMA0_CORE_CFG_0 + dma_offset, 1 << DMA0_CORE_CFG_0_EN_SHIFT);
2603}
2604
2605static void gaudi_enable_qman(struct hl_device *hdev, int dma_id,
2606 u32 enable_mask)
2607{
2608 u32 dma_qm_offset = dma_id * DMA_QMAN_OFFSET;
2609
2610 WREG32(mmDMA0_QM_GLBL_CFG0 + dma_qm_offset, enable_mask);
2611}
2612
2613static void gaudi_init_pci_dma_qmans(struct hl_device *hdev)
2614{
2615 struct gaudi_device *gaudi = hdev->asic_specific;
2616 struct hl_hw_queue *q;
2617 int i, j, dma_id, cpu_skip, nic_skip, cq_id = 0, q_idx, msi_vec = 0;
2618
2619 if (gaudi->hw_cap_initialized & HW_CAP_PCI_DMA)
2620 return;
2621
2622 for (i = 0 ; i < PCI_DMA_NUMBER_OF_CHNLS ; i++) {
2623 dma_id = gaudi_dma_assignment[i];
2624 /*
2625 * For queues after the CPU Q need to add 1 to get the correct
2626 * queue. In addition, need to add the CPU EQ and NIC IRQs in
2627 * order to get the correct MSI register.
2628 */
2629 if (dma_id > 1) {
2630 cpu_skip = 1;
2631 nic_skip = NIC_NUMBER_OF_ENGINES;
2632 } else {
2633 cpu_skip = 0;
2634 nic_skip = 0;
2635 }
2636
2637 for (j = 0 ; j < QMAN_STREAMS ; j++) {
2638 q_idx = 4 * dma_id + j + cpu_skip;
2639 q = &hdev->kernel_queues[q_idx];
2640 q->cq_id = cq_id++;
2641 q->msi_vec = nic_skip + cpu_skip + msi_vec++;
2642 gaudi_init_pci_dma_qman(hdev, dma_id, j,
2643 q->bus_address);
2644 }
2645
2646 gaudi_init_dma_core(hdev, dma_id);
2647
2648 gaudi_enable_qman(hdev, dma_id, PCI_DMA_QMAN_ENABLE);
2649 }
2650
2651 gaudi->hw_cap_initialized |= HW_CAP_PCI_DMA;
2652}
2653
2654static void gaudi_init_hbm_dma_qman(struct hl_device *hdev, int dma_id,
2655 int qman_id, u64 qman_base_addr)
2656{
Ofir Bitton5de406c2020-09-10 10:56:26 +03002657 u32 mtr_base_en_lo, mtr_base_en_hi, mtr_base_ws_lo, mtr_base_ws_hi;
2658 u32 so_base_en_lo, so_base_en_hi, so_base_ws_lo, so_base_ws_hi;
Oded Gabbayac0ae6a2020-05-11 10:29:27 +03002659 u32 q_off, dma_qm_offset;
2660 u32 dma_qm_err_cfg;
2661
2662 dma_qm_offset = dma_id * DMA_QMAN_OFFSET;
2663
Ofir Bitton5de406c2020-09-10 10:56:26 +03002664 mtr_base_en_lo = lower_32_bits(CFG_BASE +
2665 mmSYNC_MNGR_E_N_SYNC_MNGR_OBJS_MON_PAY_ADDRL_0);
2666 mtr_base_en_hi = upper_32_bits(CFG_BASE +
Oded Gabbayac0ae6a2020-05-11 10:29:27 +03002667 mmSYNC_MNGR_E_N_SYNC_MNGR_OBJS_MON_PAY_ADDRL_0);
Ofir Bitton5de406c2020-09-10 10:56:26 +03002668 so_base_en_lo = lower_32_bits(CFG_BASE +
Oded Gabbayac0ae6a2020-05-11 10:29:27 +03002669 mmSYNC_MNGR_E_N_SYNC_MNGR_OBJS_SOB_OBJ_0);
Ofir Bitton5de406c2020-09-10 10:56:26 +03002670 so_base_en_hi = upper_32_bits(CFG_BASE +
Oded Gabbayac0ae6a2020-05-11 10:29:27 +03002671 mmSYNC_MNGR_E_N_SYNC_MNGR_OBJS_SOB_OBJ_0);
Ofir Bitton5de406c2020-09-10 10:56:26 +03002672 mtr_base_ws_lo = lower_32_bits(CFG_BASE +
2673 mmSYNC_MNGR_W_S_SYNC_MNGR_OBJS_MON_PAY_ADDRL_0);
2674 mtr_base_ws_hi = upper_32_bits(CFG_BASE +
2675 mmSYNC_MNGR_W_S_SYNC_MNGR_OBJS_MON_PAY_ADDRL_0);
2676 so_base_ws_lo = lower_32_bits(CFG_BASE +
2677 mmSYNC_MNGR_W_S_SYNC_MNGR_OBJS_SOB_OBJ_0);
2678 so_base_ws_hi = upper_32_bits(CFG_BASE +
2679 mmSYNC_MNGR_W_S_SYNC_MNGR_OBJS_SOB_OBJ_0);
Oded Gabbayac0ae6a2020-05-11 10:29:27 +03002680
2681 q_off = dma_qm_offset + qman_id * 4;
2682
2683 if (qman_id < 4) {
2684 WREG32(mmDMA0_QM_PQ_BASE_LO_0 + q_off,
2685 lower_32_bits(qman_base_addr));
2686 WREG32(mmDMA0_QM_PQ_BASE_HI_0 + q_off,
2687 upper_32_bits(qman_base_addr));
2688
2689 WREG32(mmDMA0_QM_PQ_SIZE_0 + q_off, ilog2(HBM_DMA_QMAN_LENGTH));
2690 WREG32(mmDMA0_QM_PQ_PI_0 + q_off, 0);
2691 WREG32(mmDMA0_QM_PQ_CI_0 + q_off, 0);
2692
Ofir Bitton25121d92020-09-24 08:22:58 +03002693 WREG32(mmDMA0_QM_CP_LDMA_TSIZE_OFFSET_0 + q_off,
2694 QMAN_CPDMA_SIZE_OFFSET);
2695 WREG32(mmDMA0_QM_CP_LDMA_SRC_BASE_LO_OFFSET_0 + q_off,
2696 QMAN_CPDMA_SRC_OFFSET);
2697 WREG32(mmDMA0_QM_CP_LDMA_DST_BASE_LO_OFFSET_0 + q_off,
2698 QMAN_CPDMA_DST_OFFSET);
Oded Gabbayac0ae6a2020-05-11 10:29:27 +03002699 } else {
Ofir Bitton25121d92020-09-24 08:22:58 +03002700 WREG32(mmDMA0_QM_CP_LDMA_TSIZE_OFFSET_0 + q_off,
2701 QMAN_LDMA_SIZE_OFFSET);
2702 WREG32(mmDMA0_QM_CP_LDMA_SRC_BASE_LO_OFFSET_0 + q_off,
2703 QMAN_LDMA_SRC_OFFSET);
2704 WREG32(mmDMA0_QM_CP_LDMA_DST_BASE_LO_OFFSET_0 + q_off,
Oded Gabbay5b94d6e2020-09-25 20:14:15 +03002705 QMAN_LDMA_DST_OFFSET);
Oded Gabbayac0ae6a2020-05-11 10:29:27 +03002706
2707 /* Configure RAZWI IRQ */
2708 dma_qm_err_cfg = HBM_DMA_QMAN_GLBL_ERR_CFG_MSG_EN_MASK;
2709 if (hdev->stop_on_err) {
2710 dma_qm_err_cfg |=
2711 HBM_DMA_QMAN_GLBL_ERR_CFG_STOP_ON_ERR_EN_MASK;
2712 }
2713 WREG32(mmDMA0_QM_GLBL_ERR_CFG + dma_qm_offset, dma_qm_err_cfg);
2714
2715 WREG32(mmDMA0_QM_GLBL_ERR_ADDR_LO + dma_qm_offset,
2716 lower_32_bits(CFG_BASE +
2717 mmGIC_DISTRIBUTOR__5_GICD_SETSPI_NSR));
2718 WREG32(mmDMA0_QM_GLBL_ERR_ADDR_HI + dma_qm_offset,
2719 upper_32_bits(CFG_BASE +
2720 mmGIC_DISTRIBUTOR__5_GICD_SETSPI_NSR));
2721 WREG32(mmDMA0_QM_GLBL_ERR_WDATA + dma_qm_offset,
2722 gaudi_irq_map_table[GAUDI_EVENT_DMA0_QM].cpu_id +
2723 dma_id);
2724
2725 WREG32(mmDMA0_QM_ARB_ERR_MSG_EN + dma_qm_offset,
2726 QM_ARB_ERR_MSG_EN_MASK);
2727
2728 /* Increase ARB WDT to support streams architecture */
2729 WREG32(mmDMA0_QM_ARB_SLV_CHOISE_WDT + dma_qm_offset,
2730 GAUDI_ARB_WDT_TIMEOUT);
2731
2732 WREG32(mmDMA0_QM_GLBL_CFG1 + dma_qm_offset, 0);
2733 WREG32(mmDMA0_QM_GLBL_PROT + dma_qm_offset,
2734 QMAN_INTERNAL_MAKE_TRUSTED);
2735 }
2736
Ofir Bitton5de406c2020-09-10 10:56:26 +03002737 WREG32(mmDMA0_QM_CP_MSG_BASE0_ADDR_LO_0 + q_off, mtr_base_en_lo);
2738 WREG32(mmDMA0_QM_CP_MSG_BASE0_ADDR_HI_0 + q_off, mtr_base_en_hi);
2739 WREG32(mmDMA0_QM_CP_MSG_BASE1_ADDR_LO_0 + q_off, so_base_en_lo);
2740 WREG32(mmDMA0_QM_CP_MSG_BASE1_ADDR_HI_0 + q_off, so_base_en_hi);
2741
2742 /* Configure DMA5 CP_MSG_BASE 2/3 for sync stream collective */
2743 if (gaudi_dma_assignment[dma_id] == GAUDI_ENGINE_ID_DMA_5) {
2744 WREG32(mmDMA0_QM_CP_MSG_BASE2_ADDR_LO_0 + q_off,
2745 mtr_base_ws_lo);
2746 WREG32(mmDMA0_QM_CP_MSG_BASE2_ADDR_HI_0 + q_off,
2747 mtr_base_ws_hi);
2748 WREG32(mmDMA0_QM_CP_MSG_BASE3_ADDR_LO_0 + q_off,
2749 so_base_ws_lo);
2750 WREG32(mmDMA0_QM_CP_MSG_BASE3_ADDR_HI_0 + q_off,
2751 so_base_ws_hi);
2752 }
Oded Gabbayac0ae6a2020-05-11 10:29:27 +03002753}
2754
2755static void gaudi_init_hbm_dma_qmans(struct hl_device *hdev)
2756{
2757 struct gaudi_device *gaudi = hdev->asic_specific;
2758 struct gaudi_internal_qman_info *q;
2759 u64 qman_base_addr;
2760 int i, j, dma_id, internal_q_index;
2761
2762 if (gaudi->hw_cap_initialized & HW_CAP_HBM_DMA)
2763 return;
2764
2765 for (i = 0 ; i < HBM_DMA_NUMBER_OF_CHNLS ; i++) {
2766 dma_id = gaudi_dma_assignment[GAUDI_HBM_DMA_1 + i];
2767
2768 for (j = 0 ; j < QMAN_STREAMS ; j++) {
2769 /*
2770 * Add the CPU queue in order to get the correct queue
2771 * number as all internal queue are placed after it
2772 */
2773 internal_q_index = dma_id * QMAN_STREAMS + j + 1;
2774
2775 q = &gaudi->internal_qmans[internal_q_index];
2776 qman_base_addr = (u64) q->pq_dma_addr;
2777 gaudi_init_hbm_dma_qman(hdev, dma_id, j,
2778 qman_base_addr);
2779 }
2780
2781 /* Initializing lower CP for HBM DMA QMAN */
2782 gaudi_init_hbm_dma_qman(hdev, dma_id, 4, 0);
2783
2784 gaudi_init_dma_core(hdev, dma_id);
2785
2786 gaudi_enable_qman(hdev, dma_id, HBM_DMA_QMAN_ENABLE);
2787 }
2788
2789 gaudi->hw_cap_initialized |= HW_CAP_HBM_DMA;
2790}
2791
2792static void gaudi_init_mme_qman(struct hl_device *hdev, u32 mme_offset,
2793 int qman_id, u64 qman_base_addr)
2794{
2795 u32 mtr_base_lo, mtr_base_hi;
2796 u32 so_base_lo, so_base_hi;
2797 u32 q_off, mme_id;
2798 u32 mme_qm_err_cfg;
2799
2800 mtr_base_lo = lower_32_bits(CFG_BASE +
2801 mmSYNC_MNGR_E_N_SYNC_MNGR_OBJS_MON_PAY_ADDRL_0);
2802 mtr_base_hi = upper_32_bits(CFG_BASE +
2803 mmSYNC_MNGR_E_N_SYNC_MNGR_OBJS_MON_PAY_ADDRL_0);
2804 so_base_lo = lower_32_bits(CFG_BASE +
2805 mmSYNC_MNGR_E_N_SYNC_MNGR_OBJS_SOB_OBJ_0);
2806 so_base_hi = upper_32_bits(CFG_BASE +
2807 mmSYNC_MNGR_E_N_SYNC_MNGR_OBJS_SOB_OBJ_0);
2808
2809 q_off = mme_offset + qman_id * 4;
2810
2811 if (qman_id < 4) {
2812 WREG32(mmMME0_QM_PQ_BASE_LO_0 + q_off,
2813 lower_32_bits(qman_base_addr));
2814 WREG32(mmMME0_QM_PQ_BASE_HI_0 + q_off,
2815 upper_32_bits(qman_base_addr));
2816
2817 WREG32(mmMME0_QM_PQ_SIZE_0 + q_off, ilog2(MME_QMAN_LENGTH));
2818 WREG32(mmMME0_QM_PQ_PI_0 + q_off, 0);
2819 WREG32(mmMME0_QM_PQ_CI_0 + q_off, 0);
2820
Ofir Bitton25121d92020-09-24 08:22:58 +03002821 WREG32(mmMME0_QM_CP_LDMA_TSIZE_OFFSET_0 + q_off,
2822 QMAN_CPDMA_SIZE_OFFSET);
2823 WREG32(mmMME0_QM_CP_LDMA_SRC_BASE_LO_OFFSET_0 + q_off,
2824 QMAN_CPDMA_SRC_OFFSET);
2825 WREG32(mmMME0_QM_CP_LDMA_DST_BASE_LO_OFFSET_0 + q_off,
2826 QMAN_CPDMA_DST_OFFSET);
Oded Gabbayac0ae6a2020-05-11 10:29:27 +03002827 } else {
Ofir Bitton25121d92020-09-24 08:22:58 +03002828 WREG32(mmMME0_QM_CP_LDMA_TSIZE_OFFSET_0 + q_off,
2829 QMAN_LDMA_SIZE_OFFSET);
2830 WREG32(mmMME0_QM_CP_LDMA_SRC_BASE_LO_OFFSET_0 + q_off,
2831 QMAN_LDMA_SRC_OFFSET);
2832 WREG32(mmMME0_QM_CP_LDMA_DST_BASE_LO_OFFSET_0 + q_off,
2833 QMAN_LDMA_DST_OFFSET);
Oded Gabbayac0ae6a2020-05-11 10:29:27 +03002834
2835 /* Configure RAZWI IRQ */
2836 mme_id = mme_offset /
2837 (mmMME1_QM_GLBL_CFG0 - mmMME0_QM_GLBL_CFG0);
2838
2839 mme_qm_err_cfg = MME_QMAN_GLBL_ERR_CFG_MSG_EN_MASK;
2840 if (hdev->stop_on_err) {
2841 mme_qm_err_cfg |=
2842 MME_QMAN_GLBL_ERR_CFG_STOP_ON_ERR_EN_MASK;
2843 }
2844 WREG32(mmMME0_QM_GLBL_ERR_CFG + mme_offset, mme_qm_err_cfg);
2845 WREG32(mmMME0_QM_GLBL_ERR_ADDR_LO + mme_offset,
2846 lower_32_bits(CFG_BASE +
2847 mmGIC_DISTRIBUTOR__5_GICD_SETSPI_NSR));
2848 WREG32(mmMME0_QM_GLBL_ERR_ADDR_HI + mme_offset,
2849 upper_32_bits(CFG_BASE +
2850 mmGIC_DISTRIBUTOR__5_GICD_SETSPI_NSR));
2851 WREG32(mmMME0_QM_GLBL_ERR_WDATA + mme_offset,
2852 gaudi_irq_map_table[GAUDI_EVENT_MME0_QM].cpu_id +
2853 mme_id);
2854
2855 WREG32(mmMME0_QM_ARB_ERR_MSG_EN + mme_offset,
2856 QM_ARB_ERR_MSG_EN_MASK);
2857
2858 /* Increase ARB WDT to support streams architecture */
2859 WREG32(mmMME0_QM_ARB_SLV_CHOISE_WDT + mme_offset,
2860 GAUDI_ARB_WDT_TIMEOUT);
2861
2862 WREG32(mmMME0_QM_GLBL_CFG1 + mme_offset, 0);
2863 WREG32(mmMME0_QM_GLBL_PROT + mme_offset,
2864 QMAN_INTERNAL_MAKE_TRUSTED);
2865 }
2866
2867 WREG32(mmMME0_QM_CP_MSG_BASE0_ADDR_LO_0 + q_off, mtr_base_lo);
2868 WREG32(mmMME0_QM_CP_MSG_BASE0_ADDR_HI_0 + q_off, mtr_base_hi);
2869 WREG32(mmMME0_QM_CP_MSG_BASE1_ADDR_LO_0 + q_off, so_base_lo);
2870 WREG32(mmMME0_QM_CP_MSG_BASE1_ADDR_HI_0 + q_off, so_base_hi);
2871}
2872
2873static void gaudi_init_mme_qmans(struct hl_device *hdev)
2874{
2875 struct gaudi_device *gaudi = hdev->asic_specific;
2876 struct gaudi_internal_qman_info *q;
2877 u64 qman_base_addr;
2878 u32 mme_offset;
2879 int i, internal_q_index;
2880
2881 if (gaudi->hw_cap_initialized & HW_CAP_MME)
2882 return;
2883
2884 /*
2885 * map GAUDI_QUEUE_ID_MME_0_X to the N_W_MME (mmMME2_QM_BASE)
2886 * and GAUDI_QUEUE_ID_MME_1_X to the S_W_MME (mmMME0_QM_BASE)
2887 */
2888
2889 mme_offset = mmMME2_QM_GLBL_CFG0 - mmMME0_QM_GLBL_CFG0;
2890
2891 for (i = 0 ; i < MME_NUMBER_OF_QMANS ; i++) {
2892 internal_q_index = GAUDI_QUEUE_ID_MME_0_0 + i;
2893 q = &gaudi->internal_qmans[internal_q_index];
2894 qman_base_addr = (u64) q->pq_dma_addr;
2895 gaudi_init_mme_qman(hdev, mme_offset, (i & 0x3),
2896 qman_base_addr);
2897 if (i == 3)
2898 mme_offset = 0;
2899 }
2900
2901 /* Initializing lower CP for MME QMANs */
2902 mme_offset = mmMME2_QM_GLBL_CFG0 - mmMME0_QM_GLBL_CFG0;
2903 gaudi_init_mme_qman(hdev, mme_offset, 4, 0);
2904 gaudi_init_mme_qman(hdev, 0, 4, 0);
2905
2906 WREG32(mmMME2_QM_GLBL_CFG0, QMAN_MME_ENABLE);
2907 WREG32(mmMME0_QM_GLBL_CFG0, QMAN_MME_ENABLE);
2908
2909 gaudi->hw_cap_initialized |= HW_CAP_MME;
2910}
2911
2912static void gaudi_init_tpc_qman(struct hl_device *hdev, u32 tpc_offset,
2913 int qman_id, u64 qman_base_addr)
2914{
Ofir Bitton5de406c2020-09-10 10:56:26 +03002915 u32 mtr_base_en_lo, mtr_base_en_hi, mtr_base_ws_lo, mtr_base_ws_hi;
2916 u32 so_base_en_lo, so_base_en_hi, so_base_ws_lo, so_base_ws_hi;
Oded Gabbayac0ae6a2020-05-11 10:29:27 +03002917 u32 q_off, tpc_id;
2918 u32 tpc_qm_err_cfg;
2919
Ofir Bitton5de406c2020-09-10 10:56:26 +03002920 mtr_base_en_lo = lower_32_bits(CFG_BASE +
2921 mmSYNC_MNGR_E_N_SYNC_MNGR_OBJS_MON_PAY_ADDRL_0);
2922 mtr_base_en_hi = upper_32_bits(CFG_BASE +
Oded Gabbayac0ae6a2020-05-11 10:29:27 +03002923 mmSYNC_MNGR_E_N_SYNC_MNGR_OBJS_MON_PAY_ADDRL_0);
Ofir Bitton5de406c2020-09-10 10:56:26 +03002924 so_base_en_lo = lower_32_bits(CFG_BASE +
Oded Gabbayac0ae6a2020-05-11 10:29:27 +03002925 mmSYNC_MNGR_E_N_SYNC_MNGR_OBJS_SOB_OBJ_0);
Ofir Bitton5de406c2020-09-10 10:56:26 +03002926 so_base_en_hi = upper_32_bits(CFG_BASE +
Oded Gabbayac0ae6a2020-05-11 10:29:27 +03002927 mmSYNC_MNGR_E_N_SYNC_MNGR_OBJS_SOB_OBJ_0);
Ofir Bitton5de406c2020-09-10 10:56:26 +03002928 mtr_base_ws_lo = lower_32_bits(CFG_BASE +
2929 mmSYNC_MNGR_W_S_SYNC_MNGR_OBJS_MON_PAY_ADDRL_0);
2930 mtr_base_ws_hi = upper_32_bits(CFG_BASE +
2931 mmSYNC_MNGR_W_S_SYNC_MNGR_OBJS_MON_PAY_ADDRL_0);
2932 so_base_ws_lo = lower_32_bits(CFG_BASE +
2933 mmSYNC_MNGR_W_S_SYNC_MNGR_OBJS_SOB_OBJ_0);
2934 so_base_ws_hi = upper_32_bits(CFG_BASE +
2935 mmSYNC_MNGR_W_S_SYNC_MNGR_OBJS_SOB_OBJ_0);
Oded Gabbayac0ae6a2020-05-11 10:29:27 +03002936
2937 q_off = tpc_offset + qman_id * 4;
2938
Ofir Bitton5de406c2020-09-10 10:56:26 +03002939 tpc_id = tpc_offset /
2940 (mmTPC1_QM_GLBL_CFG0 - mmTPC0_QM_GLBL_CFG0);
2941
Oded Gabbayac0ae6a2020-05-11 10:29:27 +03002942 if (qman_id < 4) {
2943 WREG32(mmTPC0_QM_PQ_BASE_LO_0 + q_off,
2944 lower_32_bits(qman_base_addr));
2945 WREG32(mmTPC0_QM_PQ_BASE_HI_0 + q_off,
2946 upper_32_bits(qman_base_addr));
2947
2948 WREG32(mmTPC0_QM_PQ_SIZE_0 + q_off, ilog2(TPC_QMAN_LENGTH));
2949 WREG32(mmTPC0_QM_PQ_PI_0 + q_off, 0);
2950 WREG32(mmTPC0_QM_PQ_CI_0 + q_off, 0);
2951
Ofir Bitton25121d92020-09-24 08:22:58 +03002952 WREG32(mmTPC0_QM_CP_LDMA_TSIZE_OFFSET_0 + q_off,
2953 QMAN_CPDMA_SIZE_OFFSET);
2954 WREG32(mmTPC0_QM_CP_LDMA_SRC_BASE_LO_OFFSET_0 + q_off,
2955 QMAN_CPDMA_SRC_OFFSET);
2956 WREG32(mmTPC0_QM_CP_LDMA_DST_BASE_LO_OFFSET_0 + q_off,
2957 QMAN_CPDMA_DST_OFFSET);
Oded Gabbayac0ae6a2020-05-11 10:29:27 +03002958 } else {
Ofir Bitton25121d92020-09-24 08:22:58 +03002959 WREG32(mmTPC0_QM_CP_LDMA_TSIZE_OFFSET_0 + q_off,
2960 QMAN_LDMA_SIZE_OFFSET);
2961 WREG32(mmTPC0_QM_CP_LDMA_SRC_BASE_LO_OFFSET_0 + q_off,
2962 QMAN_LDMA_SRC_OFFSET);
2963 WREG32(mmTPC0_QM_CP_LDMA_DST_BASE_LO_OFFSET_0 + q_off,
2964 QMAN_LDMA_DST_OFFSET);
Oded Gabbayac0ae6a2020-05-11 10:29:27 +03002965
2966 /* Configure RAZWI IRQ */
Oded Gabbayac0ae6a2020-05-11 10:29:27 +03002967 tpc_qm_err_cfg = TPC_QMAN_GLBL_ERR_CFG_MSG_EN_MASK;
2968 if (hdev->stop_on_err) {
2969 tpc_qm_err_cfg |=
2970 TPC_QMAN_GLBL_ERR_CFG_STOP_ON_ERR_EN_MASK;
2971 }
2972
2973 WREG32(mmTPC0_QM_GLBL_ERR_CFG + tpc_offset, tpc_qm_err_cfg);
2974 WREG32(mmTPC0_QM_GLBL_ERR_ADDR_LO + tpc_offset,
2975 lower_32_bits(CFG_BASE +
2976 mmGIC_DISTRIBUTOR__5_GICD_SETSPI_NSR));
2977 WREG32(mmTPC0_QM_GLBL_ERR_ADDR_HI + tpc_offset,
2978 upper_32_bits(CFG_BASE +
2979 mmGIC_DISTRIBUTOR__5_GICD_SETSPI_NSR));
2980 WREG32(mmTPC0_QM_GLBL_ERR_WDATA + tpc_offset,
2981 gaudi_irq_map_table[GAUDI_EVENT_TPC0_QM].cpu_id +
2982 tpc_id);
2983
2984 WREG32(mmTPC0_QM_ARB_ERR_MSG_EN + tpc_offset,
2985 QM_ARB_ERR_MSG_EN_MASK);
2986
2987 /* Increase ARB WDT to support streams architecture */
2988 WREG32(mmTPC0_QM_ARB_SLV_CHOISE_WDT + tpc_offset,
2989 GAUDI_ARB_WDT_TIMEOUT);
2990
2991 WREG32(mmTPC0_QM_GLBL_CFG1 + tpc_offset, 0);
2992 WREG32(mmTPC0_QM_GLBL_PROT + tpc_offset,
2993 QMAN_INTERNAL_MAKE_TRUSTED);
2994 }
2995
Ofir Bitton5de406c2020-09-10 10:56:26 +03002996 WREG32(mmTPC0_QM_CP_MSG_BASE0_ADDR_LO_0 + q_off, mtr_base_en_lo);
2997 WREG32(mmTPC0_QM_CP_MSG_BASE0_ADDR_HI_0 + q_off, mtr_base_en_hi);
2998 WREG32(mmTPC0_QM_CP_MSG_BASE1_ADDR_LO_0 + q_off, so_base_en_lo);
2999 WREG32(mmTPC0_QM_CP_MSG_BASE1_ADDR_HI_0 + q_off, so_base_en_hi);
3000
3001 /* Configure TPC7 CP_MSG_BASE 2/3 for sync stream collective */
3002 if (tpc_id == 6) {
3003 WREG32(mmTPC0_QM_CP_MSG_BASE2_ADDR_LO_0 + q_off,
3004 mtr_base_ws_lo);
3005 WREG32(mmTPC0_QM_CP_MSG_BASE2_ADDR_HI_0 + q_off,
3006 mtr_base_ws_hi);
3007 WREG32(mmTPC0_QM_CP_MSG_BASE3_ADDR_LO_0 + q_off,
3008 so_base_ws_lo);
3009 WREG32(mmTPC0_QM_CP_MSG_BASE3_ADDR_HI_0 + q_off,
3010 so_base_ws_hi);
3011 }
Oded Gabbayac0ae6a2020-05-11 10:29:27 +03003012}
3013
3014static void gaudi_init_tpc_qmans(struct hl_device *hdev)
3015{
3016 struct gaudi_device *gaudi = hdev->asic_specific;
3017 struct gaudi_internal_qman_info *q;
3018 u64 qman_base_addr;
3019 u32 so_base_hi, tpc_offset = 0;
3020 u32 tpc_delta = mmTPC1_CFG_SM_BASE_ADDRESS_HIGH -
3021 mmTPC0_CFG_SM_BASE_ADDRESS_HIGH;
3022 int i, tpc_id, internal_q_index;
3023
3024 if (gaudi->hw_cap_initialized & HW_CAP_TPC_MASK)
3025 return;
3026
3027 so_base_hi = upper_32_bits(CFG_BASE +
3028 mmSYNC_MNGR_E_N_SYNC_MNGR_OBJS_SOB_OBJ_0);
3029
3030 for (tpc_id = 0 ; tpc_id < TPC_NUMBER_OF_ENGINES ; tpc_id++) {
3031 for (i = 0 ; i < QMAN_STREAMS ; i++) {
3032 internal_q_index = GAUDI_QUEUE_ID_TPC_0_0 +
3033 tpc_id * QMAN_STREAMS + i;
3034 q = &gaudi->internal_qmans[internal_q_index];
3035 qman_base_addr = (u64) q->pq_dma_addr;
3036 gaudi_init_tpc_qman(hdev, tpc_offset, i,
3037 qman_base_addr);
3038
3039 if (i == 3) {
3040 /* Initializing lower CP for TPC QMAN */
3041 gaudi_init_tpc_qman(hdev, tpc_offset, 4, 0);
3042
3043 /* Enable the QMAN and TPC channel */
3044 WREG32(mmTPC0_QM_GLBL_CFG0 + tpc_offset,
3045 QMAN_TPC_ENABLE);
3046 }
3047 }
3048
3049 WREG32(mmTPC0_CFG_SM_BASE_ADDRESS_HIGH + tpc_id * tpc_delta,
3050 so_base_hi);
3051
3052 tpc_offset += mmTPC1_QM_GLBL_CFG0 - mmTPC0_QM_GLBL_CFG0;
3053
Oded Gabbay65887292020-08-12 11:21:01 +03003054 gaudi->hw_cap_initialized |=
3055 FIELD_PREP(HW_CAP_TPC_MASK, 1 << tpc_id);
Oded Gabbayac0ae6a2020-05-11 10:29:27 +03003056 }
3057}
3058
Oded Gabbay3c681572020-11-02 21:10:39 +02003059static void gaudi_init_nic_qman(struct hl_device *hdev, u32 nic_offset,
3060 int qman_id, u64 qman_base_addr, int nic_id)
3061{
Ofir Bitton5de406c2020-09-10 10:56:26 +03003062 u32 mtr_base_en_lo, mtr_base_en_hi, mtr_base_ws_lo, mtr_base_ws_hi;
3063 u32 so_base_en_lo, so_base_en_hi, so_base_ws_lo, so_base_ws_hi;
Oded Gabbay3c681572020-11-02 21:10:39 +02003064 u32 q_off;
3065 u32 nic_qm_err_cfg;
3066
Ofir Bitton5de406c2020-09-10 10:56:26 +03003067 mtr_base_en_lo = lower_32_bits(CFG_BASE +
3068 mmSYNC_MNGR_E_N_SYNC_MNGR_OBJS_MON_PAY_ADDRL_0);
3069 mtr_base_en_hi = upper_32_bits(CFG_BASE +
Oded Gabbay3c681572020-11-02 21:10:39 +02003070 mmSYNC_MNGR_E_N_SYNC_MNGR_OBJS_MON_PAY_ADDRL_0);
Ofir Bitton5de406c2020-09-10 10:56:26 +03003071 so_base_en_lo = lower_32_bits(CFG_BASE +
Oded Gabbay3c681572020-11-02 21:10:39 +02003072 mmSYNC_MNGR_E_N_SYNC_MNGR_OBJS_SOB_OBJ_0);
Ofir Bitton5de406c2020-09-10 10:56:26 +03003073 so_base_en_hi = upper_32_bits(CFG_BASE +
Oded Gabbay3c681572020-11-02 21:10:39 +02003074 mmSYNC_MNGR_E_N_SYNC_MNGR_OBJS_SOB_OBJ_0);
Ofir Bitton5de406c2020-09-10 10:56:26 +03003075 mtr_base_ws_lo = lower_32_bits(CFG_BASE +
3076 mmSYNC_MNGR_W_S_SYNC_MNGR_OBJS_MON_PAY_ADDRL_0);
3077 mtr_base_ws_hi = upper_32_bits(CFG_BASE +
3078 mmSYNC_MNGR_W_S_SYNC_MNGR_OBJS_MON_PAY_ADDRL_0);
3079 so_base_ws_lo = lower_32_bits(CFG_BASE +
3080 mmSYNC_MNGR_W_S_SYNC_MNGR_OBJS_SOB_OBJ_0);
3081 so_base_ws_hi = upper_32_bits(CFG_BASE +
3082 mmSYNC_MNGR_W_S_SYNC_MNGR_OBJS_SOB_OBJ_0);
Oded Gabbay3c681572020-11-02 21:10:39 +02003083
3084 q_off = nic_offset + qman_id * 4;
3085
3086 WREG32(mmNIC0_QM0_PQ_BASE_LO_0 + q_off, lower_32_bits(qman_base_addr));
3087 WREG32(mmNIC0_QM0_PQ_BASE_HI_0 + q_off, upper_32_bits(qman_base_addr));
3088
3089 WREG32(mmNIC0_QM0_PQ_SIZE_0 + q_off, ilog2(NIC_QMAN_LENGTH));
3090 WREG32(mmNIC0_QM0_PQ_PI_0 + q_off, 0);
3091 WREG32(mmNIC0_QM0_PQ_CI_0 + q_off, 0);
3092
Ofir Bitton5de406c2020-09-10 10:56:26 +03003093 WREG32(mmNIC0_QM0_CP_LDMA_TSIZE_OFFSET_0 + q_off,
3094 QMAN_LDMA_SIZE_OFFSET);
3095 WREG32(mmNIC0_QM0_CP_LDMA_SRC_BASE_LO_OFFSET_0 + q_off,
3096 QMAN_LDMA_SRC_OFFSET);
3097 WREG32(mmNIC0_QM0_CP_LDMA_DST_BASE_LO_OFFSET_0 + q_off,
3098 QMAN_LDMA_DST_OFFSET);
Oded Gabbay3c681572020-11-02 21:10:39 +02003099
Ofir Bitton5de406c2020-09-10 10:56:26 +03003100 WREG32(mmNIC0_QM0_CP_MSG_BASE0_ADDR_LO_0 + q_off, mtr_base_en_lo);
3101 WREG32(mmNIC0_QM0_CP_MSG_BASE0_ADDR_HI_0 + q_off, mtr_base_en_hi);
3102 WREG32(mmNIC0_QM0_CP_MSG_BASE1_ADDR_LO_0 + q_off, so_base_en_lo);
3103 WREG32(mmNIC0_QM0_CP_MSG_BASE1_ADDR_HI_0 + q_off, so_base_en_hi);
3104
3105 /* Configure NIC CP_MSG_BASE 2/3 for sync stream collective */
3106 WREG32(mmNIC0_QM0_CP_MSG_BASE2_ADDR_LO_0 + q_off, mtr_base_ws_lo);
3107 WREG32(mmNIC0_QM0_CP_MSG_BASE2_ADDR_HI_0 + q_off, mtr_base_ws_hi);
3108 WREG32(mmNIC0_QM0_CP_MSG_BASE3_ADDR_LO_0 + q_off, so_base_ws_lo);
3109 WREG32(mmNIC0_QM0_CP_MSG_BASE3_ADDR_HI_0 + q_off, so_base_ws_hi);
Oded Gabbay3c681572020-11-02 21:10:39 +02003110
3111 if (qman_id == 0) {
3112 /* Configure RAZWI IRQ */
3113 nic_qm_err_cfg = NIC_QMAN_GLBL_ERR_CFG_MSG_EN_MASK;
3114 if (hdev->stop_on_err) {
3115 nic_qm_err_cfg |=
3116 NIC_QMAN_GLBL_ERR_CFG_STOP_ON_ERR_EN_MASK;
3117 }
3118
3119 WREG32(mmNIC0_QM0_GLBL_ERR_CFG + nic_offset, nic_qm_err_cfg);
3120 WREG32(mmNIC0_QM0_GLBL_ERR_ADDR_LO + nic_offset,
3121 lower_32_bits(CFG_BASE +
3122 mmGIC_DISTRIBUTOR__5_GICD_SETSPI_NSR));
3123 WREG32(mmNIC0_QM0_GLBL_ERR_ADDR_HI + nic_offset,
3124 upper_32_bits(CFG_BASE +
3125 mmGIC_DISTRIBUTOR__5_GICD_SETSPI_NSR));
3126 WREG32(mmNIC0_QM0_GLBL_ERR_WDATA + nic_offset,
3127 gaudi_irq_map_table[GAUDI_EVENT_NIC0_QM0].cpu_id +
3128 nic_id);
3129
3130 WREG32(mmNIC0_QM0_ARB_ERR_MSG_EN + nic_offset,
3131 QM_ARB_ERR_MSG_EN_MASK);
3132
3133 /* Increase ARB WDT to support streams architecture */
3134 WREG32(mmNIC0_QM0_ARB_SLV_CHOISE_WDT + nic_offset,
3135 GAUDI_ARB_WDT_TIMEOUT);
3136
3137 WREG32(mmNIC0_QM0_GLBL_CFG1 + nic_offset, 0);
3138 WREG32(mmNIC0_QM0_GLBL_PROT + nic_offset,
3139 QMAN_INTERNAL_MAKE_TRUSTED);
3140 }
3141}
3142
3143static void gaudi_init_nic_qmans(struct hl_device *hdev)
3144{
3145 struct gaudi_device *gaudi = hdev->asic_specific;
3146 struct gaudi_internal_qman_info *q;
3147 u64 qman_base_addr;
3148 u32 nic_offset = 0;
3149 u32 nic_delta_between_qmans =
3150 mmNIC0_QM1_GLBL_CFG0 - mmNIC0_QM0_GLBL_CFG0;
3151 u32 nic_delta_between_nics =
3152 mmNIC1_QM0_GLBL_CFG0 - mmNIC0_QM0_GLBL_CFG0;
3153 int i, nic_id, internal_q_index;
3154
3155 if (!hdev->nic_ports_mask)
3156 return;
3157
3158 if (gaudi->hw_cap_initialized & HW_CAP_NIC_MASK)
3159 return;
3160
3161 dev_dbg(hdev->dev, "Initializing NIC QMANs\n");
3162
3163 for (nic_id = 0 ; nic_id < NIC_NUMBER_OF_ENGINES ; nic_id++) {
3164 if (!(hdev->nic_ports_mask & (1 << nic_id))) {
3165 nic_offset += nic_delta_between_qmans;
3166 if (nic_id & 1) {
3167 nic_offset -= (nic_delta_between_qmans * 2);
3168 nic_offset += nic_delta_between_nics;
3169 }
3170 continue;
3171 }
3172
3173 for (i = 0 ; i < QMAN_STREAMS ; i++) {
3174 internal_q_index = GAUDI_QUEUE_ID_NIC_0_0 +
3175 nic_id * QMAN_STREAMS + i;
3176 q = &gaudi->internal_qmans[internal_q_index];
3177 qman_base_addr = (u64) q->pq_dma_addr;
3178 gaudi_init_nic_qman(hdev, nic_offset, (i & 0x3),
3179 qman_base_addr, nic_id);
3180 }
3181
3182 /* Enable the QMAN */
3183 WREG32(mmNIC0_QM0_GLBL_CFG0 + nic_offset, NIC_QMAN_ENABLE);
3184
3185 nic_offset += nic_delta_between_qmans;
3186 if (nic_id & 1) {
3187 nic_offset -= (nic_delta_between_qmans * 2);
3188 nic_offset += nic_delta_between_nics;
3189 }
3190
3191 gaudi->hw_cap_initialized |= 1 << (HW_CAP_NIC_SHIFT + nic_id);
3192 }
3193}
3194
Oded Gabbayac0ae6a2020-05-11 10:29:27 +03003195static void gaudi_disable_pci_dma_qmans(struct hl_device *hdev)
3196{
3197 struct gaudi_device *gaudi = hdev->asic_specific;
3198
3199 if (!(gaudi->hw_cap_initialized & HW_CAP_PCI_DMA))
3200 return;
3201
3202 WREG32(mmDMA0_QM_GLBL_CFG0, 0);
3203 WREG32(mmDMA1_QM_GLBL_CFG0, 0);
3204 WREG32(mmDMA5_QM_GLBL_CFG0, 0);
3205}
3206
3207static void gaudi_disable_hbm_dma_qmans(struct hl_device *hdev)
3208{
3209 struct gaudi_device *gaudi = hdev->asic_specific;
3210
3211 if (!(gaudi->hw_cap_initialized & HW_CAP_HBM_DMA))
3212 return;
3213
3214 WREG32(mmDMA2_QM_GLBL_CFG0, 0);
3215 WREG32(mmDMA3_QM_GLBL_CFG0, 0);
3216 WREG32(mmDMA4_QM_GLBL_CFG0, 0);
3217 WREG32(mmDMA6_QM_GLBL_CFG0, 0);
3218 WREG32(mmDMA7_QM_GLBL_CFG0, 0);
3219}
3220
3221static void gaudi_disable_mme_qmans(struct hl_device *hdev)
3222{
3223 struct gaudi_device *gaudi = hdev->asic_specific;
3224
3225 if (!(gaudi->hw_cap_initialized & HW_CAP_MME))
3226 return;
3227
3228 WREG32(mmMME2_QM_GLBL_CFG0, 0);
3229 WREG32(mmMME0_QM_GLBL_CFG0, 0);
3230}
3231
3232static void gaudi_disable_tpc_qmans(struct hl_device *hdev)
3233{
3234 struct gaudi_device *gaudi = hdev->asic_specific;
3235 u32 tpc_offset = 0;
3236 int tpc_id;
3237
3238 if (!(gaudi->hw_cap_initialized & HW_CAP_TPC_MASK))
3239 return;
3240
3241 for (tpc_id = 0 ; tpc_id < TPC_NUMBER_OF_ENGINES ; tpc_id++) {
3242 WREG32(mmTPC0_QM_GLBL_CFG0 + tpc_offset, 0);
3243 tpc_offset += mmTPC1_QM_GLBL_CFG0 - mmTPC0_QM_GLBL_CFG0;
3244 }
3245}
3246
Oded Gabbay3c681572020-11-02 21:10:39 +02003247static void gaudi_disable_nic_qmans(struct hl_device *hdev)
3248{
3249 struct gaudi_device *gaudi = hdev->asic_specific;
3250 u32 nic_mask, nic_offset = 0;
3251 u32 nic_delta_between_qmans =
3252 mmNIC0_QM1_GLBL_CFG0 - mmNIC0_QM0_GLBL_CFG0;
3253 u32 nic_delta_between_nics =
3254 mmNIC1_QM0_GLBL_CFG0 - mmNIC0_QM0_GLBL_CFG0;
3255 int nic_id;
3256
3257 for (nic_id = 0 ; nic_id < NIC_NUMBER_OF_ENGINES ; nic_id++) {
3258 nic_mask = 1 << (HW_CAP_NIC_SHIFT + nic_id);
3259
3260 if (gaudi->hw_cap_initialized & nic_mask)
3261 WREG32(mmNIC0_QM0_GLBL_CFG0 + nic_offset, 0);
3262
3263 nic_offset += nic_delta_between_qmans;
3264 if (nic_id & 1) {
3265 nic_offset -= (nic_delta_between_qmans * 2);
3266 nic_offset += nic_delta_between_nics;
3267 }
3268 }
3269}
3270
Oded Gabbayac0ae6a2020-05-11 10:29:27 +03003271static void gaudi_stop_pci_dma_qmans(struct hl_device *hdev)
3272{
3273 struct gaudi_device *gaudi = hdev->asic_specific;
3274
3275 if (!(gaudi->hw_cap_initialized & HW_CAP_PCI_DMA))
3276 return;
3277
3278 /* Stop upper CPs of QMANs 0.0 to 1.3 and 5.0 to 5.3 */
3279 WREG32(mmDMA0_QM_GLBL_CFG1, 0xF << DMA0_QM_GLBL_CFG1_CP_STOP_SHIFT);
3280 WREG32(mmDMA1_QM_GLBL_CFG1, 0xF << DMA0_QM_GLBL_CFG1_CP_STOP_SHIFT);
3281 WREG32(mmDMA5_QM_GLBL_CFG1, 0xF << DMA0_QM_GLBL_CFG1_CP_STOP_SHIFT);
3282}
3283
3284static void gaudi_stop_hbm_dma_qmans(struct hl_device *hdev)
3285{
3286 struct gaudi_device *gaudi = hdev->asic_specific;
3287
3288 if (!(gaudi->hw_cap_initialized & HW_CAP_HBM_DMA))
3289 return;
3290
3291 /* Stop CPs of HBM DMA QMANs */
3292
3293 WREG32(mmDMA2_QM_GLBL_CFG1, 0x1F << DMA0_QM_GLBL_CFG1_CP_STOP_SHIFT);
3294 WREG32(mmDMA3_QM_GLBL_CFG1, 0x1F << DMA0_QM_GLBL_CFG1_CP_STOP_SHIFT);
3295 WREG32(mmDMA4_QM_GLBL_CFG1, 0x1F << DMA0_QM_GLBL_CFG1_CP_STOP_SHIFT);
3296 WREG32(mmDMA6_QM_GLBL_CFG1, 0x1F << DMA0_QM_GLBL_CFG1_CP_STOP_SHIFT);
3297 WREG32(mmDMA7_QM_GLBL_CFG1, 0x1F << DMA0_QM_GLBL_CFG1_CP_STOP_SHIFT);
3298}
3299
3300static void gaudi_stop_mme_qmans(struct hl_device *hdev)
3301{
3302 struct gaudi_device *gaudi = hdev->asic_specific;
3303
3304 if (!(gaudi->hw_cap_initialized & HW_CAP_MME))
3305 return;
3306
3307 /* Stop CPs of MME QMANs */
3308 WREG32(mmMME2_QM_GLBL_CFG1, 0x1F << MME0_QM_GLBL_CFG1_CP_STOP_SHIFT);
3309 WREG32(mmMME0_QM_GLBL_CFG1, 0x1F << MME0_QM_GLBL_CFG1_CP_STOP_SHIFT);
3310}
3311
3312static void gaudi_stop_tpc_qmans(struct hl_device *hdev)
3313{
3314 struct gaudi_device *gaudi = hdev->asic_specific;
3315
3316 if (!(gaudi->hw_cap_initialized & HW_CAP_TPC_MASK))
3317 return;
3318
3319 WREG32(mmTPC0_QM_GLBL_CFG1, 0x1F << TPC0_QM_GLBL_CFG1_CP_STOP_SHIFT);
3320 WREG32(mmTPC1_QM_GLBL_CFG1, 0x1F << TPC0_QM_GLBL_CFG1_CP_STOP_SHIFT);
3321 WREG32(mmTPC2_QM_GLBL_CFG1, 0x1F << TPC0_QM_GLBL_CFG1_CP_STOP_SHIFT);
3322 WREG32(mmTPC3_QM_GLBL_CFG1, 0x1F << TPC0_QM_GLBL_CFG1_CP_STOP_SHIFT);
3323 WREG32(mmTPC4_QM_GLBL_CFG1, 0x1F << TPC0_QM_GLBL_CFG1_CP_STOP_SHIFT);
3324 WREG32(mmTPC5_QM_GLBL_CFG1, 0x1F << TPC0_QM_GLBL_CFG1_CP_STOP_SHIFT);
3325 WREG32(mmTPC6_QM_GLBL_CFG1, 0x1F << TPC0_QM_GLBL_CFG1_CP_STOP_SHIFT);
3326 WREG32(mmTPC7_QM_GLBL_CFG1, 0x1F << TPC0_QM_GLBL_CFG1_CP_STOP_SHIFT);
3327}
3328
Oded Gabbay3c681572020-11-02 21:10:39 +02003329static void gaudi_stop_nic_qmans(struct hl_device *hdev)
3330{
3331 struct gaudi_device *gaudi = hdev->asic_specific;
3332
3333 /* Stop upper CPs of QMANs */
3334
3335 if (gaudi->hw_cap_initialized & HW_CAP_NIC0)
3336 WREG32(mmNIC0_QM0_GLBL_CFG1,
3337 NIC0_QM0_GLBL_CFG1_PQF_STOP_MASK |
3338 NIC0_QM0_GLBL_CFG1_CQF_STOP_MASK |
3339 NIC0_QM0_GLBL_CFG1_CP_STOP_MASK);
3340
3341 if (gaudi->hw_cap_initialized & HW_CAP_NIC1)
3342 WREG32(mmNIC0_QM1_GLBL_CFG1,
3343 NIC0_QM0_GLBL_CFG1_PQF_STOP_MASK |
3344 NIC0_QM0_GLBL_CFG1_CQF_STOP_MASK |
3345 NIC0_QM0_GLBL_CFG1_CP_STOP_MASK);
3346
3347 if (gaudi->hw_cap_initialized & HW_CAP_NIC2)
3348 WREG32(mmNIC1_QM0_GLBL_CFG1,
3349 NIC0_QM0_GLBL_CFG1_PQF_STOP_MASK |
3350 NIC0_QM0_GLBL_CFG1_CQF_STOP_MASK |
3351 NIC0_QM0_GLBL_CFG1_CP_STOP_MASK);
3352
3353 if (gaudi->hw_cap_initialized & HW_CAP_NIC3)
3354 WREG32(mmNIC1_QM1_GLBL_CFG1,
3355 NIC0_QM0_GLBL_CFG1_PQF_STOP_MASK |
3356 NIC0_QM0_GLBL_CFG1_CQF_STOP_MASK |
3357 NIC0_QM0_GLBL_CFG1_CP_STOP_MASK);
3358
3359 if (gaudi->hw_cap_initialized & HW_CAP_NIC4)
3360 WREG32(mmNIC2_QM0_GLBL_CFG1,
3361 NIC0_QM0_GLBL_CFG1_PQF_STOP_MASK |
3362 NIC0_QM0_GLBL_CFG1_CQF_STOP_MASK |
3363 NIC0_QM0_GLBL_CFG1_CP_STOP_MASK);
3364
3365 if (gaudi->hw_cap_initialized & HW_CAP_NIC5)
3366 WREG32(mmNIC2_QM1_GLBL_CFG1,
3367 NIC0_QM0_GLBL_CFG1_PQF_STOP_MASK |
3368 NIC0_QM0_GLBL_CFG1_CQF_STOP_MASK |
3369 NIC0_QM0_GLBL_CFG1_CP_STOP_MASK);
3370
3371 if (gaudi->hw_cap_initialized & HW_CAP_NIC6)
3372 WREG32(mmNIC3_QM0_GLBL_CFG1,
3373 NIC0_QM0_GLBL_CFG1_PQF_STOP_MASK |
3374 NIC0_QM0_GLBL_CFG1_CQF_STOP_MASK |
3375 NIC0_QM0_GLBL_CFG1_CP_STOP_MASK);
3376
3377 if (gaudi->hw_cap_initialized & HW_CAP_NIC7)
3378 WREG32(mmNIC3_QM1_GLBL_CFG1,
3379 NIC0_QM0_GLBL_CFG1_PQF_STOP_MASK |
3380 NIC0_QM0_GLBL_CFG1_CQF_STOP_MASK |
3381 NIC0_QM0_GLBL_CFG1_CP_STOP_MASK);
3382
3383 if (gaudi->hw_cap_initialized & HW_CAP_NIC8)
3384 WREG32(mmNIC4_QM0_GLBL_CFG1,
3385 NIC0_QM0_GLBL_CFG1_PQF_STOP_MASK |
3386 NIC0_QM0_GLBL_CFG1_CQF_STOP_MASK |
3387 NIC0_QM0_GLBL_CFG1_CP_STOP_MASK);
3388
3389 if (gaudi->hw_cap_initialized & HW_CAP_NIC9)
3390 WREG32(mmNIC4_QM1_GLBL_CFG1,
3391 NIC0_QM0_GLBL_CFG1_PQF_STOP_MASK |
3392 NIC0_QM0_GLBL_CFG1_CQF_STOP_MASK |
3393 NIC0_QM0_GLBL_CFG1_CP_STOP_MASK);
3394}
3395
Oded Gabbayac0ae6a2020-05-11 10:29:27 +03003396static void gaudi_pci_dma_stall(struct hl_device *hdev)
3397{
3398 struct gaudi_device *gaudi = hdev->asic_specific;
3399
3400 if (!(gaudi->hw_cap_initialized & HW_CAP_PCI_DMA))
3401 return;
3402
3403 WREG32(mmDMA0_CORE_CFG_1, 1 << DMA0_CORE_CFG_1_HALT_SHIFT);
3404 WREG32(mmDMA1_CORE_CFG_1, 1 << DMA0_CORE_CFG_1_HALT_SHIFT);
3405 WREG32(mmDMA5_CORE_CFG_1, 1 << DMA0_CORE_CFG_1_HALT_SHIFT);
3406}
3407
3408static void gaudi_hbm_dma_stall(struct hl_device *hdev)
3409{
3410 struct gaudi_device *gaudi = hdev->asic_specific;
3411
3412 if (!(gaudi->hw_cap_initialized & HW_CAP_HBM_DMA))
3413 return;
3414
3415 WREG32(mmDMA2_CORE_CFG_1, 1 << DMA0_CORE_CFG_1_HALT_SHIFT);
3416 WREG32(mmDMA3_CORE_CFG_1, 1 << DMA0_CORE_CFG_1_HALT_SHIFT);
3417 WREG32(mmDMA4_CORE_CFG_1, 1 << DMA0_CORE_CFG_1_HALT_SHIFT);
3418 WREG32(mmDMA6_CORE_CFG_1, 1 << DMA0_CORE_CFG_1_HALT_SHIFT);
3419 WREG32(mmDMA7_CORE_CFG_1, 1 << DMA0_CORE_CFG_1_HALT_SHIFT);
3420}
3421
3422static void gaudi_mme_stall(struct hl_device *hdev)
3423{
3424 struct gaudi_device *gaudi = hdev->asic_specific;
3425
3426 if (!(gaudi->hw_cap_initialized & HW_CAP_MME))
3427 return;
3428
3429 /* WA for H3-1800 bug: do ACC and SBAB writes twice */
3430 WREG32(mmMME0_ACC_ACC_STALL, 1 << MME_ACC_ACC_STALL_R_SHIFT);
3431 WREG32(mmMME0_ACC_ACC_STALL, 1 << MME_ACC_ACC_STALL_R_SHIFT);
3432 WREG32(mmMME0_SBAB_SB_STALL, 1 << MME_SBAB_SB_STALL_R_SHIFT);
3433 WREG32(mmMME0_SBAB_SB_STALL, 1 << MME_SBAB_SB_STALL_R_SHIFT);
3434 WREG32(mmMME1_ACC_ACC_STALL, 1 << MME_ACC_ACC_STALL_R_SHIFT);
3435 WREG32(mmMME1_ACC_ACC_STALL, 1 << MME_ACC_ACC_STALL_R_SHIFT);
3436 WREG32(mmMME1_SBAB_SB_STALL, 1 << MME_SBAB_SB_STALL_R_SHIFT);
3437 WREG32(mmMME1_SBAB_SB_STALL, 1 << MME_SBAB_SB_STALL_R_SHIFT);
3438 WREG32(mmMME2_ACC_ACC_STALL, 1 << MME_ACC_ACC_STALL_R_SHIFT);
3439 WREG32(mmMME2_ACC_ACC_STALL, 1 << MME_ACC_ACC_STALL_R_SHIFT);
3440 WREG32(mmMME2_SBAB_SB_STALL, 1 << MME_SBAB_SB_STALL_R_SHIFT);
3441 WREG32(mmMME2_SBAB_SB_STALL, 1 << MME_SBAB_SB_STALL_R_SHIFT);
3442 WREG32(mmMME3_ACC_ACC_STALL, 1 << MME_ACC_ACC_STALL_R_SHIFT);
3443 WREG32(mmMME3_ACC_ACC_STALL, 1 << MME_ACC_ACC_STALL_R_SHIFT);
3444 WREG32(mmMME3_SBAB_SB_STALL, 1 << MME_SBAB_SB_STALL_R_SHIFT);
3445 WREG32(mmMME3_SBAB_SB_STALL, 1 << MME_SBAB_SB_STALL_R_SHIFT);
3446}
3447
3448static void gaudi_tpc_stall(struct hl_device *hdev)
3449{
3450 struct gaudi_device *gaudi = hdev->asic_specific;
3451
3452 if (!(gaudi->hw_cap_initialized & HW_CAP_TPC_MASK))
3453 return;
3454
3455 WREG32(mmTPC0_CFG_TPC_STALL, 1 << TPC0_CFG_TPC_STALL_V_SHIFT);
3456 WREG32(mmTPC1_CFG_TPC_STALL, 1 << TPC0_CFG_TPC_STALL_V_SHIFT);
3457 WREG32(mmTPC2_CFG_TPC_STALL, 1 << TPC0_CFG_TPC_STALL_V_SHIFT);
3458 WREG32(mmTPC3_CFG_TPC_STALL, 1 << TPC0_CFG_TPC_STALL_V_SHIFT);
3459 WREG32(mmTPC4_CFG_TPC_STALL, 1 << TPC0_CFG_TPC_STALL_V_SHIFT);
3460 WREG32(mmTPC5_CFG_TPC_STALL, 1 << TPC0_CFG_TPC_STALL_V_SHIFT);
3461 WREG32(mmTPC6_CFG_TPC_STALL, 1 << TPC0_CFG_TPC_STALL_V_SHIFT);
3462 WREG32(mmTPC7_CFG_TPC_STALL, 1 << TPC0_CFG_TPC_STALL_V_SHIFT);
3463}
3464
Oded Gabbaye38bfd32020-07-03 20:46:12 +03003465static void gaudi_set_clock_gating(struct hl_device *hdev)
Oded Gabbayac0ae6a2020-05-11 10:29:27 +03003466{
3467 struct gaudi_device *gaudi = hdev->asic_specific;
3468 u32 qman_offset;
Ofir Bittonf44d23b2020-08-04 13:38:43 +03003469 bool enable;
Oded Gabbayac0ae6a2020-05-11 10:29:27 +03003470 int i;
3471
Oded Gabbayac0ae6a2020-05-11 10:29:27 +03003472 /* In case we are during debug session, don't enable the clock gate
3473 * as it may interfere
3474 */
3475 if (hdev->in_debug)
3476 return;
3477
Oded Gabbay0024c0942020-12-05 22:55:09 +02003478 if (!hdev->asic_prop.fw_security_disabled)
3479 return;
3480
Oded Gabbaye38bfd32020-07-03 20:46:12 +03003481 for (i = GAUDI_PCI_DMA_1, qman_offset = 0 ; i < GAUDI_HBM_DMA_1 ; i++) {
Ofir Bittonf44d23b2020-08-04 13:38:43 +03003482 enable = !!(hdev->clock_gating_mask &
3483 (BIT_ULL(gaudi_dma_assignment[i])));
Oded Gabbaye38bfd32020-07-03 20:46:12 +03003484
Oded Gabbayac0ae6a2020-05-11 10:29:27 +03003485 qman_offset = gaudi_dma_assignment[i] * DMA_QMAN_OFFSET;
Ofir Bittonf44d23b2020-08-04 13:38:43 +03003486 WREG32(mmDMA0_QM_CGM_CFG1 + qman_offset,
3487 enable ? QMAN_CGM1_PWR_GATE_EN : 0);
Oded Gabbayac0ae6a2020-05-11 10:29:27 +03003488 WREG32(mmDMA0_QM_CGM_CFG + qman_offset,
Ofir Bittonf44d23b2020-08-04 13:38:43 +03003489 enable ? QMAN_UPPER_CP_CGM_PWR_GATE_EN : 0);
Oded Gabbayac0ae6a2020-05-11 10:29:27 +03003490 }
3491
Oded Gabbaye38bfd32020-07-03 20:46:12 +03003492 for (i = GAUDI_HBM_DMA_1 ; i < GAUDI_DMA_MAX ; i++) {
Ofir Bittonf44d23b2020-08-04 13:38:43 +03003493 enable = !!(hdev->clock_gating_mask &
3494 (BIT_ULL(gaudi_dma_assignment[i])));
Oded Gabbaye38bfd32020-07-03 20:46:12 +03003495
Oded Gabbayda5dfbb2021-02-06 19:34:59 +02003496 /* GC sends work to DMA engine through Upper CP in DMA5 so
3497 * we need to not enable clock gating in that DMA
3498 */
3499 if (i == GAUDI_HBM_DMA_4)
3500 enable = 0;
3501
Oded Gabbayac0ae6a2020-05-11 10:29:27 +03003502 qman_offset = gaudi_dma_assignment[i] * DMA_QMAN_OFFSET;
Ofir Bittonf44d23b2020-08-04 13:38:43 +03003503 WREG32(mmDMA0_QM_CGM_CFG1 + qman_offset,
3504 enable ? QMAN_CGM1_PWR_GATE_EN : 0);
Oded Gabbayac0ae6a2020-05-11 10:29:27 +03003505 WREG32(mmDMA0_QM_CGM_CFG + qman_offset,
Ofir Bittonf44d23b2020-08-04 13:38:43 +03003506 enable ? QMAN_COMMON_CP_CGM_PWR_GATE_EN : 0);
Oded Gabbayac0ae6a2020-05-11 10:29:27 +03003507 }
3508
Ofir Bittonf44d23b2020-08-04 13:38:43 +03003509 enable = !!(hdev->clock_gating_mask & (BIT_ULL(GAUDI_ENGINE_ID_MME_0)));
3510 WREG32(mmMME0_QM_CGM_CFG1, enable ? QMAN_CGM1_PWR_GATE_EN : 0);
3511 WREG32(mmMME0_QM_CGM_CFG, enable ? QMAN_COMMON_CP_CGM_PWR_GATE_EN : 0);
Oded Gabbaye38bfd32020-07-03 20:46:12 +03003512
Ofir Bittonf44d23b2020-08-04 13:38:43 +03003513 enable = !!(hdev->clock_gating_mask & (BIT_ULL(GAUDI_ENGINE_ID_MME_2)));
3514 WREG32(mmMME2_QM_CGM_CFG1, enable ? QMAN_CGM1_PWR_GATE_EN : 0);
3515 WREG32(mmMME2_QM_CGM_CFG, enable ? QMAN_COMMON_CP_CGM_PWR_GATE_EN : 0);
Oded Gabbayac0ae6a2020-05-11 10:29:27 +03003516
3517 for (i = 0, qman_offset = 0 ; i < TPC_NUMBER_OF_ENGINES ; i++) {
Ofir Bittonf44d23b2020-08-04 13:38:43 +03003518 enable = !!(hdev->clock_gating_mask &
3519 (BIT_ULL(GAUDI_ENGINE_ID_TPC_0 + i)));
Oded Gabbaye38bfd32020-07-03 20:46:12 +03003520
Oded Gabbayac0ae6a2020-05-11 10:29:27 +03003521 WREG32(mmTPC0_QM_CGM_CFG1 + qman_offset,
Ofir Bittonf44d23b2020-08-04 13:38:43 +03003522 enable ? QMAN_CGM1_PWR_GATE_EN : 0);
Oded Gabbayac0ae6a2020-05-11 10:29:27 +03003523 WREG32(mmTPC0_QM_CGM_CFG + qman_offset,
Ofir Bittonf44d23b2020-08-04 13:38:43 +03003524 enable ? QMAN_COMMON_CP_CGM_PWR_GATE_EN : 0);
Oded Gabbayac0ae6a2020-05-11 10:29:27 +03003525
3526 qman_offset += TPC_QMAN_OFFSET;
3527 }
3528
3529 gaudi->hw_cap_initialized |= HW_CAP_CLK_GATE;
3530}
3531
3532static void gaudi_disable_clock_gating(struct hl_device *hdev)
3533{
3534 struct gaudi_device *gaudi = hdev->asic_specific;
3535 u32 qman_offset;
3536 int i;
3537
Oded Gabbay0024c0942020-12-05 22:55:09 +02003538 if (!hdev->asic_prop.fw_security_disabled)
Oded Gabbayac0ae6a2020-05-11 10:29:27 +03003539 return;
3540
3541 for (i = 0, qman_offset = 0 ; i < DMA_NUMBER_OF_CHANNELS ; i++) {
3542 WREG32(mmDMA0_QM_CGM_CFG + qman_offset, 0);
3543 WREG32(mmDMA0_QM_CGM_CFG1 + qman_offset, 0);
3544
3545 qman_offset += (mmDMA1_QM_CGM_CFG - mmDMA0_QM_CGM_CFG);
3546 }
3547
3548 WREG32(mmMME0_QM_CGM_CFG, 0);
3549 WREG32(mmMME0_QM_CGM_CFG1, 0);
3550 WREG32(mmMME2_QM_CGM_CFG, 0);
3551 WREG32(mmMME2_QM_CGM_CFG1, 0);
3552
3553 for (i = 0, qman_offset = 0 ; i < TPC_NUMBER_OF_ENGINES ; i++) {
3554 WREG32(mmTPC0_QM_CGM_CFG + qman_offset, 0);
3555 WREG32(mmTPC0_QM_CGM_CFG1 + qman_offset, 0);
3556
3557 qman_offset += (mmTPC1_QM_CGM_CFG - mmTPC0_QM_CGM_CFG);
3558 }
3559
3560 gaudi->hw_cap_initialized &= ~(HW_CAP_CLK_GATE);
3561}
3562
3563static void gaudi_enable_timestamp(struct hl_device *hdev)
3564{
3565 /* Disable the timestamp counter */
3566 WREG32(mmPSOC_TIMESTAMP_BASE - CFG_BASE, 0);
3567
3568 /* Zero the lower/upper parts of the 64-bit counter */
3569 WREG32(mmPSOC_TIMESTAMP_BASE - CFG_BASE + 0xC, 0);
3570 WREG32(mmPSOC_TIMESTAMP_BASE - CFG_BASE + 0x8, 0);
3571
3572 /* Enable the counter */
3573 WREG32(mmPSOC_TIMESTAMP_BASE - CFG_BASE, 1);
3574}
3575
3576static void gaudi_disable_timestamp(struct hl_device *hdev)
3577{
3578 /* Disable the timestamp counter */
3579 WREG32(mmPSOC_TIMESTAMP_BASE - CFG_BASE, 0);
3580}
3581
3582static void gaudi_halt_engines(struct hl_device *hdev, bool hard_reset)
3583{
Oded Gabbayc83c4172020-07-05 15:48:34 +03003584 u32 wait_timeout_ms;
Oded Gabbayac0ae6a2020-05-11 10:29:27 +03003585
3586 dev_info(hdev->dev,
3587 "Halting compute engines and disabling interrupts\n");
3588
Oded Gabbayc83c4172020-07-05 15:48:34 +03003589 if (hdev->pldm)
Oded Gabbayac0ae6a2020-05-11 10:29:27 +03003590 wait_timeout_ms = GAUDI_PLDM_RESET_WAIT_MSEC;
Oded Gabbayc83c4172020-07-05 15:48:34 +03003591 else
Oded Gabbayac0ae6a2020-05-11 10:29:27 +03003592 wait_timeout_ms = GAUDI_RESET_WAIT_MSEC;
Oded Gabbayac0ae6a2020-05-11 10:29:27 +03003593
Oded Gabbay3c681572020-11-02 21:10:39 +02003594 gaudi_stop_nic_qmans(hdev);
Oded Gabbayac0ae6a2020-05-11 10:29:27 +03003595 gaudi_stop_mme_qmans(hdev);
3596 gaudi_stop_tpc_qmans(hdev);
3597 gaudi_stop_hbm_dma_qmans(hdev);
3598 gaudi_stop_pci_dma_qmans(hdev);
3599
Oded Gabbaye38bfd32020-07-03 20:46:12 +03003600 hdev->asic_funcs->disable_clock_gating(hdev);
Oded Gabbayac0ae6a2020-05-11 10:29:27 +03003601
3602 msleep(wait_timeout_ms);
3603
3604 gaudi_pci_dma_stall(hdev);
3605 gaudi_hbm_dma_stall(hdev);
3606 gaudi_tpc_stall(hdev);
3607 gaudi_mme_stall(hdev);
3608
3609 msleep(wait_timeout_ms);
3610
Oded Gabbay3c681572020-11-02 21:10:39 +02003611 gaudi_disable_nic_qmans(hdev);
Oded Gabbayac0ae6a2020-05-11 10:29:27 +03003612 gaudi_disable_mme_qmans(hdev);
3613 gaudi_disable_tpc_qmans(hdev);
3614 gaudi_disable_hbm_dma_qmans(hdev);
3615 gaudi_disable_pci_dma_qmans(hdev);
3616
3617 gaudi_disable_timestamp(hdev);
3618
Oded Gabbay12ae3132020-07-03 20:58:23 +03003619 gaudi_disable_msi(hdev);
Oded Gabbayac0ae6a2020-05-11 10:29:27 +03003620}
3621
3622static int gaudi_mmu_init(struct hl_device *hdev)
3623{
3624 struct asic_fixed_properties *prop = &hdev->asic_prop;
3625 struct gaudi_device *gaudi = hdev->asic_specific;
3626 u64 hop0_addr;
3627 int rc, i;
3628
3629 if (!hdev->mmu_enable)
3630 return 0;
3631
3632 if (gaudi->hw_cap_initialized & HW_CAP_MMU)
3633 return 0;
3634
Oded Gabbayac0ae6a2020-05-11 10:29:27 +03003635 for (i = 0 ; i < prop->max_asid ; i++) {
3636 hop0_addr = prop->mmu_pgt_addr +
3637 (i * prop->mmu_hop_table_size);
3638
3639 rc = gaudi_mmu_update_asid_hop0_addr(hdev, i, hop0_addr);
3640 if (rc) {
3641 dev_err(hdev->dev,
3642 "failed to set hop0 addr for asid %d\n", i);
3643 goto err;
3644 }
3645 }
3646
3647 /* init MMU cache manage page */
3648 WREG32(mmSTLB_CACHE_INV_BASE_39_8, MMU_CACHE_MNG_ADDR >> 8);
3649 WREG32(mmSTLB_CACHE_INV_BASE_49_40, MMU_CACHE_MNG_ADDR >> 40);
3650
Tomer Tayar644883e2020-07-19 11:00:03 +03003651 hdev->asic_funcs->mmu_invalidate_cache(hdev, true, 0);
Oded Gabbayac0ae6a2020-05-11 10:29:27 +03003652
3653 WREG32(mmMMU_UP_MMU_ENABLE, 1);
3654 WREG32(mmMMU_UP_SPI_MASK, 0xF);
3655
3656 WREG32(mmSTLB_HOP_CONFIGURATION,
3657 hdev->mmu_huge_page_opt ? 0x30440 : 0x40440);
3658
Omer Shpigelmancfd41762020-06-03 13:03:35 +03003659 /*
3660 * The H/W expects the first PI after init to be 1. After wraparound
3661 * we'll write 0.
3662 */
3663 gaudi->mmu_cache_inv_pi = 1;
3664
Oded Gabbayac0ae6a2020-05-11 10:29:27 +03003665 gaudi->hw_cap_initialized |= HW_CAP_MMU;
3666
3667 return 0;
3668
3669err:
3670 return rc;
3671}
3672
3673static int gaudi_load_firmware_to_device(struct hl_device *hdev)
3674{
3675 void __iomem *dst;
3676
3677 /* HBM scrambler must be initialized before pushing F/W to HBM */
3678 gaudi_init_scrambler_hbm(hdev);
3679
3680 dst = hdev->pcie_bar[HBM_BAR_ID] + LINUX_FW_OFFSET;
3681
Ofir Bitton9bb86b62020-10-20 10:45:37 +03003682 return hl_fw_load_fw_to_device(hdev, GAUDI_LINUX_FW_FILE, dst, 0, 0);
Oded Gabbayac0ae6a2020-05-11 10:29:27 +03003683}
3684
3685static int gaudi_load_boot_fit_to_device(struct hl_device *hdev)
3686{
3687 void __iomem *dst;
3688
3689 dst = hdev->pcie_bar[SRAM_BAR_ID] + BOOT_FIT_SRAM_OFFSET;
3690
Ofir Bitton9bb86b62020-10-20 10:45:37 +03003691 return hl_fw_load_fw_to_device(hdev, GAUDI_BOOT_FIT_FILE, dst, 0, 0);
Oded Gabbayac0ae6a2020-05-11 10:29:27 +03003692}
3693
farah kassabrieb10b892020-10-14 15:17:36 +03003694static int gaudi_read_device_fw_version(struct hl_device *hdev,
Oded Gabbayac0ae6a2020-05-11 10:29:27 +03003695 enum hl_fw_component fwc)
3696{
3697 const char *name;
3698 u32 ver_off;
3699 char *dest;
3700
3701 switch (fwc) {
3702 case FW_COMP_UBOOT:
3703 ver_off = RREG32(mmUBOOT_VER_OFFSET);
3704 dest = hdev->asic_prop.uboot_ver;
3705 name = "U-Boot";
3706 break;
3707 case FW_COMP_PREBOOT:
3708 ver_off = RREG32(mmPREBOOT_VER_OFFSET);
3709 dest = hdev->asic_prop.preboot_ver;
3710 name = "Preboot";
3711 break;
3712 default:
3713 dev_warn(hdev->dev, "Undefined FW component: %d\n", fwc);
farah kassabrieb10b892020-10-14 15:17:36 +03003714 return -EIO;
Oded Gabbayac0ae6a2020-05-11 10:29:27 +03003715 }
3716
3717 ver_off &= ~((u32)SRAM_BASE_ADDR);
3718
3719 if (ver_off < SRAM_SIZE - VERSION_MAX_LEN) {
3720 memcpy_fromio(dest, hdev->pcie_bar[SRAM_BAR_ID] + ver_off,
3721 VERSION_MAX_LEN);
3722 } else {
3723 dev_err(hdev->dev, "%s version offset (0x%x) is above SRAM\n",
3724 name, ver_off);
3725 strcpy(dest, "unavailable");
farah kassabrieb10b892020-10-14 15:17:36 +03003726 return -EIO;
Oded Gabbayac0ae6a2020-05-11 10:29:27 +03003727 }
farah kassabrieb10b892020-10-14 15:17:36 +03003728
3729 return 0;
Oded Gabbayac0ae6a2020-05-11 10:29:27 +03003730}
3731
Ohad Sharabia22f0ec2021-04-11 23:06:46 +03003732static void gaudi_init_firmware_loader(struct hl_device *hdev)
3733{
3734 struct fw_load_mgr *fw_loader = &hdev->fw_loader;
3735
3736 fw_loader->kmd_msg_to_cpu_reg = mmPSOC_GLOBAL_CONF_KMD_MSG_TO_CPU;
3737 fw_loader->cpu_cmd_status_to_host_reg = mmCPU_CMD_STATUS_TO_HOST;
3738 fw_loader->cpu_timeout = GAUDI_CPU_TIMEOUT_USEC;
3739 fw_loader->boot_fit_timeout = GAUDI_BOOT_FIT_REQ_TIMEOUT_USEC;
3740 fw_loader->skip_bmc = !hdev->bmc_enable;
3741 fw_loader->cpu_boot_status_reg = mmPSOC_GLOBAL_CONF_CPU_BOOT_STATUS;
3742 fw_loader->cpu_boot_dev_status_reg = mmCPU_BOOT_DEV_STS0;
3743 fw_loader->boot_err0_reg = mmCPU_BOOT_ERR0;
3744}
3745
Oded Gabbayac0ae6a2020-05-11 10:29:27 +03003746static int gaudi_init_cpu(struct hl_device *hdev)
3747{
3748 struct gaudi_device *gaudi = hdev->asic_specific;
3749 int rc;
3750
Ofir Bitton6a2f5d72021-02-15 13:23:04 +02003751 if (!(hdev->fw_components & FW_TYPE_PREBOOT_CPU))
Oded Gabbayac0ae6a2020-05-11 10:29:27 +03003752 return 0;
3753
3754 if (gaudi->hw_cap_initialized & HW_CAP_CPU)
3755 return 0;
3756
3757 /*
3758 * The device CPU works with 40 bits addresses.
3759 * This register sets the extension to 50 bits.
3760 */
Ofir Bittonc692dec2020-10-04 17:34:37 +03003761 if (hdev->asic_prop.fw_security_disabled)
3762 WREG32(mmCPU_IF_CPU_MSB_ADDR, hdev->cpu_pci_msb_addr);
Oded Gabbayac0ae6a2020-05-11 10:29:27 +03003763
Ohad Sharabia22f0ec2021-04-11 23:06:46 +03003764 rc = hl_fw_init_cpu(hdev);
Oded Gabbayac0ae6a2020-05-11 10:29:27 +03003765
3766 if (rc)
3767 return rc;
3768
3769 gaudi->hw_cap_initialized |= HW_CAP_CPU;
3770
3771 return 0;
3772}
3773
3774static int gaudi_init_cpu_queues(struct hl_device *hdev, u32 cpu_timeout)
3775{
3776 struct gaudi_device *gaudi = hdev->asic_specific;
Ohad Sharabi5b6b7802021-02-02 13:33:34 +02003777 struct asic_fixed_properties *prop = &hdev->asic_prop;
Oded Gabbayac0ae6a2020-05-11 10:29:27 +03003778 struct hl_eq *eq;
3779 u32 status;
3780 struct hl_hw_queue *cpu_pq =
3781 &hdev->kernel_queues[GAUDI_QUEUE_ID_CPU_PQ];
3782 int err;
3783
3784 if (!hdev->cpu_queues_enable)
3785 return 0;
3786
3787 if (gaudi->hw_cap_initialized & HW_CAP_CPU_Q)
3788 return 0;
3789
3790 eq = &hdev->event_queue;
3791
3792 WREG32(mmCPU_IF_PQ_BASE_ADDR_LOW, lower_32_bits(cpu_pq->bus_address));
3793 WREG32(mmCPU_IF_PQ_BASE_ADDR_HIGH, upper_32_bits(cpu_pq->bus_address));
3794
3795 WREG32(mmCPU_IF_EQ_BASE_ADDR_LOW, lower_32_bits(eq->bus_address));
3796 WREG32(mmCPU_IF_EQ_BASE_ADDR_HIGH, upper_32_bits(eq->bus_address));
3797
3798 WREG32(mmCPU_IF_CQ_BASE_ADDR_LOW,
3799 lower_32_bits(hdev->cpu_accessible_dma_address));
3800 WREG32(mmCPU_IF_CQ_BASE_ADDR_HIGH,
3801 upper_32_bits(hdev->cpu_accessible_dma_address));
3802
3803 WREG32(mmCPU_IF_PQ_LENGTH, HL_QUEUE_SIZE_IN_BYTES);
3804 WREG32(mmCPU_IF_EQ_LENGTH, HL_EQ_SIZE_IN_BYTES);
3805 WREG32(mmCPU_IF_CQ_LENGTH, HL_CPU_ACCESSIBLE_MEM_SIZE);
3806
3807 /* Used for EQ CI */
3808 WREG32(mmCPU_IF_EQ_RD_OFFS, 0);
3809
3810 WREG32(mmCPU_IF_PF_PQ_PI, 0);
3811
3812 if (gaudi->multi_msi_mode)
3813 WREG32(mmCPU_IF_QUEUE_INIT, PQ_INIT_STATUS_READY_FOR_CP);
3814 else
3815 WREG32(mmCPU_IF_QUEUE_INIT,
3816 PQ_INIT_STATUS_READY_FOR_CP_SINGLE_MSI);
3817
3818 WREG32(mmGIC_DISTRIBUTOR__5_GICD_SETSPI_NSR, GAUDI_EVENT_PI_UPDATE);
3819
3820 err = hl_poll_timeout(
3821 hdev,
3822 mmCPU_IF_QUEUE_INIT,
3823 status,
3824 (status == PQ_INIT_STATUS_READY_FOR_HOST),
3825 1000,
3826 cpu_timeout);
3827
3828 if (err) {
3829 dev_err(hdev->dev,
Oded Gabbay6138bbe2020-09-04 20:18:16 +03003830 "Failed to communicate with Device CPU (CPU-CP timeout)\n");
Oded Gabbayac0ae6a2020-05-11 10:29:27 +03003831 return -EIO;
3832 }
3833
Ohad Sharabi5b6b7802021-02-02 13:33:34 +02003834 /* update FW application security bits */
3835 if (prop->fw_security_status_valid)
3836 prop->fw_app_security_map = RREG32(mmCPU_BOOT_DEV_STS0);
3837
Oded Gabbayac0ae6a2020-05-11 10:29:27 +03003838 gaudi->hw_cap_initialized |= HW_CAP_CPU_Q;
3839 return 0;
3840}
3841
3842static void gaudi_pre_hw_init(struct hl_device *hdev)
3843{
3844 /* Perform read from the device to make sure device is up */
Oded Gabbay377182a2020-12-09 19:50:46 +02003845 RREG32(mmHW_STATE);
Oded Gabbayac0ae6a2020-05-11 10:29:27 +03003846
Ofir Bittonc692dec2020-10-04 17:34:37 +03003847 if (hdev->asic_prop.fw_security_disabled) {
3848 /* Set the access through PCI bars (Linux driver only) as
3849 * secured
3850 */
3851 WREG32(mmPCIE_WRAP_LBW_PROT_OVR,
3852 (PCIE_WRAP_LBW_PROT_OVR_RD_EN_MASK |
3853 PCIE_WRAP_LBW_PROT_OVR_WR_EN_MASK));
Oded Gabbay57799ce2020-09-13 15:51:28 +03003854
Ofir Bittonc692dec2020-10-04 17:34:37 +03003855 /* Perform read to flush the waiting writes to ensure
3856 * configuration was set in the device
3857 */
3858 RREG32(mmPCIE_WRAP_LBW_PROT_OVR);
3859 }
Oded Gabbay57799ce2020-09-13 15:51:28 +03003860
Oded Gabbayac0ae6a2020-05-11 10:29:27 +03003861 /*
3862 * Let's mark in the H/W that we have reached this point. We check
3863 * this value in the reset_before_init function to understand whether
3864 * we need to reset the chip before doing H/W init. This register is
3865 * cleared by the H/W upon H/W reset
3866 */
3867 WREG32(mmHW_STATE, HL_DEVICE_HW_STATE_DIRTY);
Oded Gabbayac0ae6a2020-05-11 10:29:27 +03003868}
3869
3870static int gaudi_hw_init(struct hl_device *hdev)
3871{
3872 int rc;
3873
Oded Gabbayac0ae6a2020-05-11 10:29:27 +03003874 gaudi_pre_hw_init(hdev);
3875
3876 gaudi_init_pci_dma_qmans(hdev);
3877
3878 gaudi_init_hbm_dma_qmans(hdev);
3879
Oded Gabbayac0ae6a2020-05-11 10:29:27 +03003880 rc = gaudi_init_cpu(hdev);
3881 if (rc) {
3882 dev_err(hdev->dev, "failed to initialize CPU\n");
3883 return rc;
3884 }
3885
Oded Gabbay0024c0942020-12-05 22:55:09 +02003886 /* In case the clock gating was enabled in preboot we need to disable
3887 * it here before touching the MME/TPC registers.
3888 * There is no need to take clk gating mutex because when this function
3889 * runs, no other relevant code can run
3890 */
3891 hdev->asic_funcs->disable_clock_gating(hdev);
3892
Oded Gabbayac0ae6a2020-05-11 10:29:27 +03003893 /* SRAM scrambler must be initialized after CPU is running from HBM */
3894 gaudi_init_scrambler_sram(hdev);
3895
3896 /* This is here just in case we are working without CPU */
3897 gaudi_init_scrambler_hbm(hdev);
3898
3899 gaudi_init_golden_registers(hdev);
3900
3901 rc = gaudi_mmu_init(hdev);
3902 if (rc)
3903 return rc;
3904
Omer Shpigelman3a3a5bf12020-05-11 10:45:12 +03003905 gaudi_init_security(hdev);
3906
Oded Gabbayac0ae6a2020-05-11 10:29:27 +03003907 gaudi_init_mme_qmans(hdev);
3908
3909 gaudi_init_tpc_qmans(hdev);
3910
Oded Gabbay3c681572020-11-02 21:10:39 +02003911 gaudi_init_nic_qmans(hdev);
3912
Oded Gabbaye38bfd32020-07-03 20:46:12 +03003913 hdev->asic_funcs->set_clock_gating(hdev);
Oded Gabbayac0ae6a2020-05-11 10:29:27 +03003914
3915 gaudi_enable_timestamp(hdev);
3916
Oded Gabbay3c681572020-11-02 21:10:39 +02003917 /* MSI must be enabled before CPU queues and NIC are initialized */
Oded Gabbayac0ae6a2020-05-11 10:29:27 +03003918 rc = gaudi_enable_msi(hdev);
3919 if (rc)
3920 goto disable_queues;
3921
3922 /* must be called after MSI was enabled */
3923 rc = gaudi_init_cpu_queues(hdev, GAUDI_CPU_TIMEOUT_USEC);
3924 if (rc) {
3925 dev_err(hdev->dev, "failed to initialize CPU H/W queues %d\n",
3926 rc);
3927 goto disable_msi;
3928 }
3929
3930 /* Perform read from the device to flush all configuration */
Oded Gabbay377182a2020-12-09 19:50:46 +02003931 RREG32(mmHW_STATE);
Oded Gabbayac0ae6a2020-05-11 10:29:27 +03003932
3933 return 0;
3934
3935disable_msi:
3936 gaudi_disable_msi(hdev);
3937disable_queues:
3938 gaudi_disable_mme_qmans(hdev);
3939 gaudi_disable_pci_dma_qmans(hdev);
3940
3941 return rc;
3942}
3943
3944static void gaudi_hw_fini(struct hl_device *hdev, bool hard_reset)
3945{
3946 struct gaudi_device *gaudi = hdev->asic_specific;
Igor Grinbergb726a2f2020-10-29 14:06:54 +02003947 u32 status, reset_timeout_ms, cpu_timeout_ms;
Oded Gabbayac0ae6a2020-05-11 10:29:27 +03003948
Oded Gabbay12ae3132020-07-03 20:58:23 +03003949 if (!hard_reset) {
3950 dev_err(hdev->dev, "GAUDI doesn't support soft-reset\n");
3951 return;
3952 }
3953
Oded Gabbayc83c4172020-07-05 15:48:34 +03003954 if (hdev->pldm) {
Oded Gabbay12ae3132020-07-03 20:58:23 +03003955 reset_timeout_ms = GAUDI_PLDM_HRESET_TIMEOUT_MSEC;
Oded Gabbayc83c4172020-07-05 15:48:34 +03003956 cpu_timeout_ms = GAUDI_PLDM_RESET_WAIT_MSEC;
3957 } else {
Oded Gabbayac0ae6a2020-05-11 10:29:27 +03003958 reset_timeout_ms = GAUDI_RESET_TIMEOUT_MSEC;
Oded Gabbayc83c4172020-07-05 15:48:34 +03003959 cpu_timeout_ms = GAUDI_CPU_RESET_WAIT_MSEC;
3960 }
3961
3962 /* Set device to handle FLR by H/W as we will put the device CPU to
3963 * halt mode
3964 */
Oded Gabbaya63c3fb2020-11-26 18:11:05 +02003965 if (hdev->asic_prop.fw_security_disabled &&
3966 !hdev->asic_prop.hard_reset_done_by_fw)
Ofir Bittonb90c8942020-11-08 12:59:04 +02003967 WREG32(mmPCIE_AUX_FLR_CTRL, (PCIE_AUX_FLR_CTRL_HW_CTRL_MASK |
Oded Gabbayc83c4172020-07-05 15:48:34 +03003968 PCIE_AUX_FLR_CTRL_INT_MASK_MASK));
3969
3970 /* I don't know what is the state of the CPU so make sure it is
3971 * stopped in any means necessary
3972 */
Ofir Bitton9c9013c2020-12-01 10:39:54 +02003973 if (hdev->asic_prop.hard_reset_done_by_fw)
3974 WREG32(mmPSOC_GLOBAL_CONF_KMD_MSG_TO_CPU, KMD_MSG_RST_DEV);
3975 else
3976 WREG32(mmPSOC_GLOBAL_CONF_KMD_MSG_TO_CPU, KMD_MSG_GOTO_WFE);
Ofir Bittonb90c8942020-11-08 12:59:04 +02003977
Oded Gabbayc83c4172020-07-05 15:48:34 +03003978 WREG32(mmGIC_DISTRIBUTOR__5_GICD_SETSPI_NSR, GAUDI_EVENT_HALT_MACHINE);
3979
Oded Gabbaya63c3fb2020-11-26 18:11:05 +02003980 if (hdev->asic_prop.fw_security_disabled &&
3981 !hdev->asic_prop.hard_reset_done_by_fw) {
3982
3983 /* Configure the reset registers. Must be done as early as
3984 * possible in case we fail during H/W initialization
3985 */
3986 WREG32(mmPSOC_GLOBAL_CONF_SOFT_RST_CFG_H,
3987 (CFG_RST_H_DMA_MASK |
3988 CFG_RST_H_MME_MASK |
3989 CFG_RST_H_SM_MASK |
3990 CFG_RST_H_TPC_7_MASK));
3991
3992 WREG32(mmPSOC_GLOBAL_CONF_SOFT_RST_CFG_L, CFG_RST_L_TPC_MASK);
3993
3994 WREG32(mmPSOC_GLOBAL_CONF_SW_ALL_RST_CFG_H,
3995 (CFG_RST_H_HBM_MASK |
3996 CFG_RST_H_TPC_7_MASK |
3997 CFG_RST_H_NIC_MASK |
3998 CFG_RST_H_SM_MASK |
3999 CFG_RST_H_DMA_MASK |
4000 CFG_RST_H_MME_MASK |
4001 CFG_RST_H_CPU_MASK |
4002 CFG_RST_H_MMU_MASK));
4003
4004 WREG32(mmPSOC_GLOBAL_CONF_SW_ALL_RST_CFG_L,
4005 (CFG_RST_L_IF_MASK |
4006 CFG_RST_L_PSOC_MASK |
4007 CFG_RST_L_TPC_MASK));
4008
Ofir Bittonb90c8942020-11-08 12:59:04 +02004009 msleep(cpu_timeout_ms);
Oded Gabbayac0ae6a2020-05-11 10:29:27 +03004010
Ofir Bittonb90c8942020-11-08 12:59:04 +02004011 /* Tell ASIC not to re-initialize PCIe */
4012 WREG32(mmPREBOOT_PCIE_EN, LKD_HARD_RESET_MAGIC);
Oded Gabbayac0ae6a2020-05-11 10:29:27 +03004013
Ofir Bittonb90c8942020-11-08 12:59:04 +02004014 /* Restart BTL/BLR upon hard-reset */
4015 if (hdev->asic_prop.fw_security_disabled)
4016 WREG32(mmPSOC_GLOBAL_CONF_BOOT_SEQ_RE_START, 1);
Oded Gabbayac0ae6a2020-05-11 10:29:27 +03004017
Ofir Bittonb90c8942020-11-08 12:59:04 +02004018 WREG32(mmPSOC_GLOBAL_CONF_SW_ALL_RST,
Oded Gabbay12ae3132020-07-03 20:58:23 +03004019 1 << PSOC_GLOBAL_CONF_SW_ALL_RST_IND_SHIFT);
Ofir Bittonb90c8942020-11-08 12:59:04 +02004020
Oded Gabbay13d0ee12020-12-06 23:48:45 +02004021 dev_info(hdev->dev,
4022 "Issued HARD reset command, going to wait %dms\n",
4023 reset_timeout_ms);
4024 } else {
4025 dev_info(hdev->dev,
4026 "Firmware performs HARD reset, going to wait %dms\n",
4027 reset_timeout_ms);
4028 }
Oded Gabbayac0ae6a2020-05-11 10:29:27 +03004029
4030 /*
4031 * After hard reset, we can't poll the BTM_FSM register because the PSOC
4032 * itself is in reset. Need to wait until the reset is deasserted
4033 */
4034 msleep(reset_timeout_ms);
4035
4036 status = RREG32(mmPSOC_GLOBAL_CONF_BTM_FSM);
4037 if (status & PSOC_GLOBAL_CONF_BTM_FSM_STATE_MASK)
4038 dev_err(hdev->dev,
4039 "Timeout while waiting for device to reset 0x%x\n",
4040 status);
4041
farah kassabrieb10b892020-10-14 15:17:36 +03004042 if (gaudi) {
4043 gaudi->hw_cap_initialized &= ~(HW_CAP_CPU | HW_CAP_CPU_Q |
4044 HW_CAP_HBM | HW_CAP_PCI_DMA |
4045 HW_CAP_MME | HW_CAP_TPC_MASK |
4046 HW_CAP_HBM_DMA | HW_CAP_PLL |
4047 HW_CAP_NIC_MASK | HW_CAP_MMU |
4048 HW_CAP_SRAM_SCRAMBLER |
4049 HW_CAP_HBM_SCRAMBLER |
4050 HW_CAP_CLK_GATE);
Oded Gabbaye38bfd32020-07-03 20:46:12 +03004051
farah kassabrieb10b892020-10-14 15:17:36 +03004052 memset(gaudi->events_stat, 0, sizeof(gaudi->events_stat));
4053 }
Oded Gabbayac0ae6a2020-05-11 10:29:27 +03004054}
4055
4056static int gaudi_suspend(struct hl_device *hdev)
4057{
4058 int rc;
4059
Oded Gabbay2f553422020-08-15 16:28:10 +03004060 rc = hl_fw_send_pci_access_msg(hdev, CPUCP_PACKET_DISABLE_PCI_ACCESS);
Oded Gabbayac0ae6a2020-05-11 10:29:27 +03004061 if (rc)
4062 dev_err(hdev->dev, "Failed to disable PCI access from CPU\n");
4063
4064 return rc;
4065}
4066
4067static int gaudi_resume(struct hl_device *hdev)
4068{
4069 return gaudi_init_iatu(hdev);
4070}
4071
4072static int gaudi_cb_mmap(struct hl_device *hdev, struct vm_area_struct *vma,
Hillf Danton0db57532020-08-23 07:32:42 +08004073 void *cpu_addr, dma_addr_t dma_addr, size_t size)
Oded Gabbayac0ae6a2020-05-11 10:29:27 +03004074{
4075 int rc;
4076
4077 vma->vm_flags |= VM_IO | VM_PFNMAP | VM_DONTEXPAND | VM_DONTDUMP |
4078 VM_DONTCOPY | VM_NORESERVE;
4079
Oded Gabbaya9d4ef62021-01-11 13:49:38 +02004080 rc = dma_mmap_coherent(hdev->dev, vma, cpu_addr,
4081 (dma_addr - HOST_PHYS_BASE), size);
Oded Gabbayac0ae6a2020-05-11 10:29:27 +03004082 if (rc)
Hillf Danton0db57532020-08-23 07:32:42 +08004083 dev_err(hdev->dev, "dma_mmap_coherent error %d", rc);
Oded Gabbayac0ae6a2020-05-11 10:29:27 +03004084
4085 return rc;
4086}
4087
4088static void gaudi_ring_doorbell(struct hl_device *hdev, u32 hw_queue_id, u32 pi)
4089{
4090 struct gaudi_device *gaudi = hdev->asic_specific;
4091 u32 db_reg_offset, db_value, dma_qm_offset, q_off;
4092 int dma_id;
4093 bool invalid_queue = false;
4094
4095 switch (hw_queue_id) {
4096 case GAUDI_QUEUE_ID_DMA_0_0...GAUDI_QUEUE_ID_DMA_0_3:
4097 dma_id = gaudi_dma_assignment[GAUDI_PCI_DMA_1];
4098 dma_qm_offset = dma_id * DMA_QMAN_OFFSET;
4099 q_off = dma_qm_offset + (hw_queue_id & 0x3) * 4;
4100 db_reg_offset = mmDMA0_QM_PQ_PI_0 + q_off;
4101 break;
4102
4103 case GAUDI_QUEUE_ID_DMA_1_0...GAUDI_QUEUE_ID_DMA_1_3:
4104 dma_id = gaudi_dma_assignment[GAUDI_PCI_DMA_2];
4105 dma_qm_offset = dma_id * DMA_QMAN_OFFSET;
4106 q_off = dma_qm_offset + (hw_queue_id & 0x3) * 4;
4107 db_reg_offset = mmDMA0_QM_PQ_PI_0 + q_off;
4108 break;
4109
4110 case GAUDI_QUEUE_ID_DMA_2_0...GAUDI_QUEUE_ID_DMA_2_3:
4111 dma_id = gaudi_dma_assignment[GAUDI_HBM_DMA_1];
4112 dma_qm_offset = dma_id * DMA_QMAN_OFFSET;
4113 q_off = dma_qm_offset + ((hw_queue_id - 1) & 0x3) * 4;
4114 db_reg_offset = mmDMA0_QM_PQ_PI_0 + q_off;
4115 break;
4116
4117 case GAUDI_QUEUE_ID_DMA_3_0...GAUDI_QUEUE_ID_DMA_3_3:
4118 dma_id = gaudi_dma_assignment[GAUDI_HBM_DMA_2];
4119 dma_qm_offset = dma_id * DMA_QMAN_OFFSET;
4120 q_off = dma_qm_offset + ((hw_queue_id - 1) & 0x3) * 4;
4121 db_reg_offset = mmDMA0_QM_PQ_PI_0 + q_off;
4122 break;
4123
4124 case GAUDI_QUEUE_ID_DMA_4_0...GAUDI_QUEUE_ID_DMA_4_3:
4125 dma_id = gaudi_dma_assignment[GAUDI_HBM_DMA_3];
4126 dma_qm_offset = dma_id * DMA_QMAN_OFFSET;
4127 q_off = dma_qm_offset + ((hw_queue_id - 1) & 0x3) * 4;
4128 db_reg_offset = mmDMA0_QM_PQ_PI_0 + q_off;
4129 break;
4130
4131 case GAUDI_QUEUE_ID_DMA_5_0...GAUDI_QUEUE_ID_DMA_5_3:
Oded Gabbayac0ae6a2020-05-11 10:29:27 +03004132 dma_id = gaudi_dma_assignment[GAUDI_HBM_DMA_4];
4133 dma_qm_offset = dma_id * DMA_QMAN_OFFSET;
4134 q_off = dma_qm_offset + ((hw_queue_id - 1) & 0x3) * 4;
4135 db_reg_offset = mmDMA0_QM_PQ_PI_0 + q_off;
4136 break;
4137
Ofir Bitton0940cab2020-08-31 08:52:56 +03004138 case GAUDI_QUEUE_ID_DMA_6_0...GAUDI_QUEUE_ID_DMA_6_3:
Oded Gabbayac0ae6a2020-05-11 10:29:27 +03004139 dma_id = gaudi_dma_assignment[GAUDI_HBM_DMA_5];
4140 dma_qm_offset = dma_id * DMA_QMAN_OFFSET;
4141 q_off = dma_qm_offset + ((hw_queue_id - 1) & 0x3) * 4;
4142 db_reg_offset = mmDMA0_QM_PQ_PI_0 + q_off;
4143 break;
4144
Ofir Bitton0940cab2020-08-31 08:52:56 +03004145 case GAUDI_QUEUE_ID_DMA_7_0...GAUDI_QUEUE_ID_DMA_7_3:
4146 dma_id = gaudi_dma_assignment[GAUDI_HBM_DMA_6];
4147 dma_qm_offset = dma_id * DMA_QMAN_OFFSET;
4148 q_off = dma_qm_offset + ((hw_queue_id - 1) & 0x3) * 4;
4149 db_reg_offset = mmDMA0_QM_PQ_PI_0 + q_off;
4150 break;
4151
Oded Gabbayac0ae6a2020-05-11 10:29:27 +03004152 case GAUDI_QUEUE_ID_CPU_PQ:
4153 if (gaudi->hw_cap_initialized & HW_CAP_CPU_Q)
4154 db_reg_offset = mmCPU_IF_PF_PQ_PI;
4155 else
4156 invalid_queue = true;
4157 break;
4158
4159 case GAUDI_QUEUE_ID_MME_0_0:
4160 db_reg_offset = mmMME2_QM_PQ_PI_0;
4161 break;
4162
4163 case GAUDI_QUEUE_ID_MME_0_1:
4164 db_reg_offset = mmMME2_QM_PQ_PI_1;
4165 break;
4166
4167 case GAUDI_QUEUE_ID_MME_0_2:
4168 db_reg_offset = mmMME2_QM_PQ_PI_2;
4169 break;
4170
4171 case GAUDI_QUEUE_ID_MME_0_3:
4172 db_reg_offset = mmMME2_QM_PQ_PI_3;
4173 break;
4174
4175 case GAUDI_QUEUE_ID_MME_1_0:
4176 db_reg_offset = mmMME0_QM_PQ_PI_0;
4177 break;
4178
4179 case GAUDI_QUEUE_ID_MME_1_1:
4180 db_reg_offset = mmMME0_QM_PQ_PI_1;
4181 break;
4182
4183 case GAUDI_QUEUE_ID_MME_1_2:
4184 db_reg_offset = mmMME0_QM_PQ_PI_2;
4185 break;
4186
4187 case GAUDI_QUEUE_ID_MME_1_3:
4188 db_reg_offset = mmMME0_QM_PQ_PI_3;
4189 break;
4190
4191 case GAUDI_QUEUE_ID_TPC_0_0:
4192 db_reg_offset = mmTPC0_QM_PQ_PI_0;
4193 break;
4194
4195 case GAUDI_QUEUE_ID_TPC_0_1:
4196 db_reg_offset = mmTPC0_QM_PQ_PI_1;
4197 break;
4198
4199 case GAUDI_QUEUE_ID_TPC_0_2:
4200 db_reg_offset = mmTPC0_QM_PQ_PI_2;
4201 break;
4202
4203 case GAUDI_QUEUE_ID_TPC_0_3:
4204 db_reg_offset = mmTPC0_QM_PQ_PI_3;
4205 break;
4206
4207 case GAUDI_QUEUE_ID_TPC_1_0:
4208 db_reg_offset = mmTPC1_QM_PQ_PI_0;
4209 break;
4210
4211 case GAUDI_QUEUE_ID_TPC_1_1:
4212 db_reg_offset = mmTPC1_QM_PQ_PI_1;
4213 break;
4214
4215 case GAUDI_QUEUE_ID_TPC_1_2:
4216 db_reg_offset = mmTPC1_QM_PQ_PI_2;
4217 break;
4218
4219 case GAUDI_QUEUE_ID_TPC_1_3:
4220 db_reg_offset = mmTPC1_QM_PQ_PI_3;
4221 break;
4222
4223 case GAUDI_QUEUE_ID_TPC_2_0:
4224 db_reg_offset = mmTPC2_QM_PQ_PI_0;
4225 break;
4226
4227 case GAUDI_QUEUE_ID_TPC_2_1:
4228 db_reg_offset = mmTPC2_QM_PQ_PI_1;
4229 break;
4230
4231 case GAUDI_QUEUE_ID_TPC_2_2:
4232 db_reg_offset = mmTPC2_QM_PQ_PI_2;
4233 break;
4234
4235 case GAUDI_QUEUE_ID_TPC_2_3:
4236 db_reg_offset = mmTPC2_QM_PQ_PI_3;
4237 break;
4238
4239 case GAUDI_QUEUE_ID_TPC_3_0:
4240 db_reg_offset = mmTPC3_QM_PQ_PI_0;
4241 break;
4242
4243 case GAUDI_QUEUE_ID_TPC_3_1:
4244 db_reg_offset = mmTPC3_QM_PQ_PI_1;
4245 break;
4246
4247 case GAUDI_QUEUE_ID_TPC_3_2:
4248 db_reg_offset = mmTPC3_QM_PQ_PI_2;
4249 break;
4250
4251 case GAUDI_QUEUE_ID_TPC_3_3:
4252 db_reg_offset = mmTPC3_QM_PQ_PI_3;
4253 break;
4254
4255 case GAUDI_QUEUE_ID_TPC_4_0:
4256 db_reg_offset = mmTPC4_QM_PQ_PI_0;
4257 break;
4258
4259 case GAUDI_QUEUE_ID_TPC_4_1:
4260 db_reg_offset = mmTPC4_QM_PQ_PI_1;
4261 break;
4262
4263 case GAUDI_QUEUE_ID_TPC_4_2:
4264 db_reg_offset = mmTPC4_QM_PQ_PI_2;
4265 break;
4266
4267 case GAUDI_QUEUE_ID_TPC_4_3:
4268 db_reg_offset = mmTPC4_QM_PQ_PI_3;
4269 break;
4270
4271 case GAUDI_QUEUE_ID_TPC_5_0:
4272 db_reg_offset = mmTPC5_QM_PQ_PI_0;
4273 break;
4274
4275 case GAUDI_QUEUE_ID_TPC_5_1:
4276 db_reg_offset = mmTPC5_QM_PQ_PI_1;
4277 break;
4278
4279 case GAUDI_QUEUE_ID_TPC_5_2:
4280 db_reg_offset = mmTPC5_QM_PQ_PI_2;
4281 break;
4282
4283 case GAUDI_QUEUE_ID_TPC_5_3:
4284 db_reg_offset = mmTPC5_QM_PQ_PI_3;
4285 break;
4286
4287 case GAUDI_QUEUE_ID_TPC_6_0:
4288 db_reg_offset = mmTPC6_QM_PQ_PI_0;
4289 break;
4290
4291 case GAUDI_QUEUE_ID_TPC_6_1:
4292 db_reg_offset = mmTPC6_QM_PQ_PI_1;
4293 break;
4294
4295 case GAUDI_QUEUE_ID_TPC_6_2:
4296 db_reg_offset = mmTPC6_QM_PQ_PI_2;
4297 break;
4298
4299 case GAUDI_QUEUE_ID_TPC_6_3:
4300 db_reg_offset = mmTPC6_QM_PQ_PI_3;
4301 break;
4302
4303 case GAUDI_QUEUE_ID_TPC_7_0:
4304 db_reg_offset = mmTPC7_QM_PQ_PI_0;
4305 break;
4306
4307 case GAUDI_QUEUE_ID_TPC_7_1:
4308 db_reg_offset = mmTPC7_QM_PQ_PI_1;
4309 break;
4310
4311 case GAUDI_QUEUE_ID_TPC_7_2:
4312 db_reg_offset = mmTPC7_QM_PQ_PI_2;
4313 break;
4314
4315 case GAUDI_QUEUE_ID_TPC_7_3:
4316 db_reg_offset = mmTPC7_QM_PQ_PI_3;
4317 break;
4318
Oded Gabbay3c681572020-11-02 21:10:39 +02004319 case GAUDI_QUEUE_ID_NIC_0_0:
4320 db_reg_offset = mmNIC0_QM0_PQ_PI_0;
4321 break;
4322
4323 case GAUDI_QUEUE_ID_NIC_0_1:
4324 db_reg_offset = mmNIC0_QM0_PQ_PI_1;
4325 break;
4326
4327 case GAUDI_QUEUE_ID_NIC_0_2:
4328 db_reg_offset = mmNIC0_QM0_PQ_PI_2;
4329 break;
4330
4331 case GAUDI_QUEUE_ID_NIC_0_3:
4332 db_reg_offset = mmNIC0_QM0_PQ_PI_3;
4333 break;
4334
4335 case GAUDI_QUEUE_ID_NIC_1_0:
4336 db_reg_offset = mmNIC0_QM1_PQ_PI_0;
4337 break;
4338
4339 case GAUDI_QUEUE_ID_NIC_1_1:
4340 db_reg_offset = mmNIC0_QM1_PQ_PI_1;
4341 break;
4342
4343 case GAUDI_QUEUE_ID_NIC_1_2:
4344 db_reg_offset = mmNIC0_QM1_PQ_PI_2;
4345 break;
4346
4347 case GAUDI_QUEUE_ID_NIC_1_3:
4348 db_reg_offset = mmNIC0_QM1_PQ_PI_3;
4349 break;
4350
4351 case GAUDI_QUEUE_ID_NIC_2_0:
4352 db_reg_offset = mmNIC1_QM0_PQ_PI_0;
4353 break;
4354
4355 case GAUDI_QUEUE_ID_NIC_2_1:
4356 db_reg_offset = mmNIC1_QM0_PQ_PI_1;
4357 break;
4358
4359 case GAUDI_QUEUE_ID_NIC_2_2:
4360 db_reg_offset = mmNIC1_QM0_PQ_PI_2;
4361 break;
4362
4363 case GAUDI_QUEUE_ID_NIC_2_3:
4364 db_reg_offset = mmNIC1_QM0_PQ_PI_3;
4365 break;
4366
4367 case GAUDI_QUEUE_ID_NIC_3_0:
4368 db_reg_offset = mmNIC1_QM1_PQ_PI_0;
4369 break;
4370
4371 case GAUDI_QUEUE_ID_NIC_3_1:
4372 db_reg_offset = mmNIC1_QM1_PQ_PI_1;
4373 break;
4374
4375 case GAUDI_QUEUE_ID_NIC_3_2:
4376 db_reg_offset = mmNIC1_QM1_PQ_PI_2;
4377 break;
4378
4379 case GAUDI_QUEUE_ID_NIC_3_3:
4380 db_reg_offset = mmNIC1_QM1_PQ_PI_3;
4381 break;
4382
4383 case GAUDI_QUEUE_ID_NIC_4_0:
4384 db_reg_offset = mmNIC2_QM0_PQ_PI_0;
4385 break;
4386
4387 case GAUDI_QUEUE_ID_NIC_4_1:
4388 db_reg_offset = mmNIC2_QM0_PQ_PI_1;
4389 break;
4390
4391 case GAUDI_QUEUE_ID_NIC_4_2:
4392 db_reg_offset = mmNIC2_QM0_PQ_PI_2;
4393 break;
4394
4395 case GAUDI_QUEUE_ID_NIC_4_3:
4396 db_reg_offset = mmNIC2_QM0_PQ_PI_3;
4397 break;
4398
4399 case GAUDI_QUEUE_ID_NIC_5_0:
4400 db_reg_offset = mmNIC2_QM1_PQ_PI_0;
4401 break;
4402
4403 case GAUDI_QUEUE_ID_NIC_5_1:
4404 db_reg_offset = mmNIC2_QM1_PQ_PI_1;
4405 break;
4406
4407 case GAUDI_QUEUE_ID_NIC_5_2:
4408 db_reg_offset = mmNIC2_QM1_PQ_PI_2;
4409 break;
4410
4411 case GAUDI_QUEUE_ID_NIC_5_3:
4412 db_reg_offset = mmNIC2_QM1_PQ_PI_3;
4413 break;
4414
4415 case GAUDI_QUEUE_ID_NIC_6_0:
4416 db_reg_offset = mmNIC3_QM0_PQ_PI_0;
4417 break;
4418
4419 case GAUDI_QUEUE_ID_NIC_6_1:
4420 db_reg_offset = mmNIC3_QM0_PQ_PI_1;
4421 break;
4422
4423 case GAUDI_QUEUE_ID_NIC_6_2:
4424 db_reg_offset = mmNIC3_QM0_PQ_PI_2;
4425 break;
4426
4427 case GAUDI_QUEUE_ID_NIC_6_3:
4428 db_reg_offset = mmNIC3_QM0_PQ_PI_3;
4429 break;
4430
4431 case GAUDI_QUEUE_ID_NIC_7_0:
4432 db_reg_offset = mmNIC3_QM1_PQ_PI_0;
4433 break;
4434
4435 case GAUDI_QUEUE_ID_NIC_7_1:
4436 db_reg_offset = mmNIC3_QM1_PQ_PI_1;
4437 break;
4438
4439 case GAUDI_QUEUE_ID_NIC_7_2:
4440 db_reg_offset = mmNIC3_QM1_PQ_PI_2;
4441 break;
4442
4443 case GAUDI_QUEUE_ID_NIC_7_3:
4444 db_reg_offset = mmNIC3_QM1_PQ_PI_3;
4445 break;
4446
4447 case GAUDI_QUEUE_ID_NIC_8_0:
4448 db_reg_offset = mmNIC4_QM0_PQ_PI_0;
4449 break;
4450
4451 case GAUDI_QUEUE_ID_NIC_8_1:
4452 db_reg_offset = mmNIC4_QM0_PQ_PI_1;
4453 break;
4454
4455 case GAUDI_QUEUE_ID_NIC_8_2:
4456 db_reg_offset = mmNIC4_QM0_PQ_PI_2;
4457 break;
4458
4459 case GAUDI_QUEUE_ID_NIC_8_3:
4460 db_reg_offset = mmNIC4_QM0_PQ_PI_3;
4461 break;
4462
4463 case GAUDI_QUEUE_ID_NIC_9_0:
4464 db_reg_offset = mmNIC4_QM1_PQ_PI_0;
4465 break;
4466
4467 case GAUDI_QUEUE_ID_NIC_9_1:
4468 db_reg_offset = mmNIC4_QM1_PQ_PI_1;
4469 break;
4470
4471 case GAUDI_QUEUE_ID_NIC_9_2:
4472 db_reg_offset = mmNIC4_QM1_PQ_PI_2;
4473 break;
4474
4475 case GAUDI_QUEUE_ID_NIC_9_3:
4476 db_reg_offset = mmNIC4_QM1_PQ_PI_3;
4477 break;
4478
Oded Gabbayac0ae6a2020-05-11 10:29:27 +03004479 default:
4480 invalid_queue = true;
4481 }
4482
4483 if (invalid_queue) {
4484 /* Should never get here */
4485 dev_err(hdev->dev, "h/w queue %d is invalid. Can't set pi\n",
4486 hw_queue_id);
4487 return;
4488 }
4489
4490 db_value = pi;
4491
4492 /* ring the doorbell */
4493 WREG32(db_reg_offset, db_value);
4494
Ofir Bitton5dbd7b42021-01-28 16:30:25 +02004495 if (hw_queue_id == GAUDI_QUEUE_ID_CPU_PQ) {
4496 /* make sure device CPU will read latest data from host */
4497 mb();
Oded Gabbayac0ae6a2020-05-11 10:29:27 +03004498 WREG32(mmGIC_DISTRIBUTOR__5_GICD_SETSPI_NSR,
4499 GAUDI_EVENT_PI_UPDATE);
Ofir Bitton5dbd7b42021-01-28 16:30:25 +02004500 }
Oded Gabbayac0ae6a2020-05-11 10:29:27 +03004501}
4502
4503static void gaudi_pqe_write(struct hl_device *hdev, __le64 *pqe,
4504 struct hl_bd *bd)
4505{
4506 __le64 *pbd = (__le64 *) bd;
4507
4508 /* The QMANs are on the host memory so a simple copy suffice */
4509 pqe[0] = pbd[0];
4510 pqe[1] = pbd[1];
4511}
4512
4513static void *gaudi_dma_alloc_coherent(struct hl_device *hdev, size_t size,
4514 dma_addr_t *dma_handle, gfp_t flags)
4515{
4516 void *kernel_addr = dma_alloc_coherent(&hdev->pdev->dev, size,
4517 dma_handle, flags);
4518
4519 /* Shift to the device's base physical address of host memory */
4520 if (kernel_addr)
4521 *dma_handle += HOST_PHYS_BASE;
4522
4523 return kernel_addr;
4524}
4525
4526static void gaudi_dma_free_coherent(struct hl_device *hdev, size_t size,
4527 void *cpu_addr, dma_addr_t dma_handle)
4528{
4529 /* Cancel the device's base physical address of host memory */
4530 dma_addr_t fixed_dma_handle = dma_handle - HOST_PHYS_BASE;
4531
4532 dma_free_coherent(&hdev->pdev->dev, size, cpu_addr, fixed_dma_handle);
4533}
4534
farah kassabri03df1362020-05-06 11:17:38 +03004535static int gaudi_hbm_scrubbing(struct hl_device *hdev)
4536{
4537 struct asic_fixed_properties *prop = &hdev->asic_prop;
4538 u64 cur_addr = DRAM_BASE_ADDR_USER;
4539 u32 val;
4540 u32 chunk_size;
4541 int rc, dma_id;
4542
4543 while (cur_addr < prop->dram_end_address) {
4544 for (dma_id = 0 ; dma_id < DMA_NUMBER_OF_CHANNELS ; dma_id++) {
4545 u32 dma_offset = dma_id * DMA_CORE_OFFSET;
4546
4547 chunk_size =
4548 min((u64)SZ_2G, prop->dram_end_address - cur_addr);
4549
4550 dev_dbg(hdev->dev,
4551 "Doing HBM scrubbing for 0x%09llx - 0x%09llx\n",
4552 cur_addr, cur_addr + chunk_size);
4553
4554 WREG32(mmDMA0_CORE_SRC_BASE_LO + dma_offset, 0);
4555 WREG32(mmDMA0_CORE_SRC_BASE_HI + dma_offset, 0);
4556 WREG32(mmDMA0_CORE_DST_BASE_LO + dma_offset,
4557 lower_32_bits(cur_addr));
4558 WREG32(mmDMA0_CORE_DST_BASE_HI + dma_offset,
4559 upper_32_bits(cur_addr));
4560 WREG32(mmDMA0_CORE_DST_TSIZE_0 + dma_offset,
4561 chunk_size);
4562 WREG32(mmDMA0_CORE_COMMIT + dma_offset,
4563 ((1 << DMA0_CORE_COMMIT_LIN_SHIFT) |
4564 (1 << DMA0_CORE_COMMIT_MEM_SET_SHIFT)));
4565
4566 cur_addr += chunk_size;
4567
4568 if (cur_addr == prop->dram_end_address)
4569 break;
4570 }
4571
4572 for (dma_id = 0 ; dma_id < DMA_NUMBER_OF_CHANNELS ; dma_id++) {
4573 u32 dma_offset = dma_id * DMA_CORE_OFFSET;
4574
4575 rc = hl_poll_timeout(
4576 hdev,
4577 mmDMA0_CORE_STS0 + dma_offset,
4578 val,
4579 ((val & DMA0_CORE_STS0_BUSY_MASK) == 0),
4580 1000,
4581 HBM_SCRUBBING_TIMEOUT_US);
4582
4583 if (rc) {
4584 dev_err(hdev->dev,
4585 "DMA Timeout during HBM scrubbing of DMA #%d\n",
4586 dma_id);
4587 return -EIO;
4588 }
4589 }
4590 }
4591
4592 return 0;
4593}
4594
4595static int gaudi_scrub_device_mem(struct hl_device *hdev, u64 addr, u64 size)
4596{
4597 struct asic_fixed_properties *prop = &hdev->asic_prop;
4598 struct gaudi_device *gaudi = hdev->asic_specific;
farah kassabri03df1362020-05-06 11:17:38 +03004599 int rc = 0;
4600 u64 val = 0;
4601
4602 if (!hdev->memory_scrub)
4603 return 0;
4604
4605 if (!addr && !size) {
4606 /* Wait till device is idle */
4607 rc = hl_poll_timeout(
4608 hdev,
4609 mmDMA0_CORE_STS0/* dummy */,
4610 val/* dummy */,
Ohad Sharabicf303392021-01-17 16:01:56 +02004611 (hdev->asic_funcs->is_device_idle(hdev, NULL,
4612 0, NULL)),
farah kassabri03df1362020-05-06 11:17:38 +03004613 1000,
4614 HBM_SCRUBBING_TIMEOUT_US);
4615 if (rc) {
4616 dev_err(hdev->dev, "waiting for idle timeout\n");
4617 return -EIO;
4618 }
4619
4620 /* Scrub SRAM */
4621 addr = prop->sram_user_base_address;
4622 size = hdev->pldm ? 0x10000 :
4623 (prop->sram_size - SRAM_USER_BASE_OFFSET);
4624 val = 0x7777777777777777ull;
4625
4626 rc = gaudi_memset_device_memory(hdev, addr, size, val);
4627 if (rc) {
4628 dev_err(hdev->dev,
4629 "Failed to clear SRAM in mem scrub all\n");
4630 return rc;
4631 }
4632
4633 mutex_lock(&gaudi->clk_gate_mutex);
4634 hdev->asic_funcs->disable_clock_gating(hdev);
4635
4636 /* Scrub HBM using all DMA channels in parallel */
4637 rc = gaudi_hbm_scrubbing(hdev);
4638 if (rc)
4639 dev_err(hdev->dev,
4640 "Failed to clear HBM in mem scrub all\n");
4641
4642 hdev->asic_funcs->set_clock_gating(hdev);
4643 mutex_unlock(&gaudi->clk_gate_mutex);
4644 }
4645
4646 return rc;
4647}
4648
Oded Gabbayac0ae6a2020-05-11 10:29:27 +03004649static void *gaudi_get_int_queue_base(struct hl_device *hdev,
4650 u32 queue_id, dma_addr_t *dma_handle,
4651 u16 *queue_len)
4652{
4653 struct gaudi_device *gaudi = hdev->asic_specific;
4654 struct gaudi_internal_qman_info *q;
4655
4656 if (queue_id >= GAUDI_QUEUE_ID_SIZE ||
4657 gaudi_queue_type[queue_id] != QUEUE_TYPE_INT) {
4658 dev_err(hdev->dev, "Got invalid queue id %d\n", queue_id);
4659 return NULL;
4660 }
4661
4662 q = &gaudi->internal_qmans[queue_id];
4663 *dma_handle = q->pq_dma_addr;
4664 *queue_len = q->pq_size / QMAN_PQ_ENTRY_SIZE;
4665
4666 return q->pq_kernel_addr;
4667}
4668
4669static int gaudi_send_cpu_message(struct hl_device *hdev, u32 *msg,
Alon Mizrahi439bc472020-11-10 13:49:10 +02004670 u16 len, u32 timeout, u64 *result)
Oded Gabbayac0ae6a2020-05-11 10:29:27 +03004671{
4672 struct gaudi_device *gaudi = hdev->asic_specific;
4673
4674 if (!(gaudi->hw_cap_initialized & HW_CAP_CPU_Q)) {
4675 if (result)
4676 *result = 0;
4677 return 0;
4678 }
4679
Oded Gabbay788cacf2020-07-07 17:30:13 +03004680 if (!timeout)
4681 timeout = GAUDI_MSG_TO_CPU_TIMEOUT_USEC;
4682
Oded Gabbayac0ae6a2020-05-11 10:29:27 +03004683 return hl_fw_send_cpu_message(hdev, GAUDI_QUEUE_ID_CPU_PQ, msg, len,
4684 timeout, result);
4685}
4686
4687static int gaudi_test_queue(struct hl_device *hdev, u32 hw_queue_id)
4688{
4689 struct packet_msg_prot *fence_pkt;
4690 dma_addr_t pkt_dma_addr;
4691 u32 fence_val, tmp, timeout_usec;
4692 dma_addr_t fence_dma_addr;
4693 u32 *fence_ptr;
4694 int rc;
4695
4696 if (hdev->pldm)
4697 timeout_usec = GAUDI_PLDM_TEST_QUEUE_WAIT_USEC;
4698 else
4699 timeout_usec = GAUDI_TEST_QUEUE_WAIT_USEC;
4700
4701 fence_val = GAUDI_QMAN0_FENCE_VAL;
4702
4703 fence_ptr = hdev->asic_funcs->asic_dma_pool_zalloc(hdev, 4, GFP_KERNEL,
4704 &fence_dma_addr);
4705 if (!fence_ptr) {
4706 dev_err(hdev->dev,
Dotan Barakd6b045c2020-08-06 09:20:49 +03004707 "Failed to allocate memory for H/W queue %d testing\n",
4708 hw_queue_id);
Oded Gabbayac0ae6a2020-05-11 10:29:27 +03004709 return -ENOMEM;
4710 }
4711
4712 *fence_ptr = 0;
4713
4714 fence_pkt = hdev->asic_funcs->asic_dma_pool_zalloc(hdev,
4715 sizeof(struct packet_msg_prot),
4716 GFP_KERNEL, &pkt_dma_addr);
4717 if (!fence_pkt) {
4718 dev_err(hdev->dev,
Dotan Barakd6b045c2020-08-06 09:20:49 +03004719 "Failed to allocate packet for H/W queue %d testing\n",
4720 hw_queue_id);
Oded Gabbayac0ae6a2020-05-11 10:29:27 +03004721 rc = -ENOMEM;
4722 goto free_fence_ptr;
4723 }
4724
Oded Gabbay65887292020-08-12 11:21:01 +03004725 tmp = FIELD_PREP(GAUDI_PKT_CTL_OPCODE_MASK, PACKET_MSG_PROT);
4726 tmp |= FIELD_PREP(GAUDI_PKT_CTL_EB_MASK, 1);
4727 tmp |= FIELD_PREP(GAUDI_PKT_CTL_MB_MASK, 1);
4728
Oded Gabbayac0ae6a2020-05-11 10:29:27 +03004729 fence_pkt->ctl = cpu_to_le32(tmp);
4730 fence_pkt->value = cpu_to_le32(fence_val);
4731 fence_pkt->addr = cpu_to_le64(fence_dma_addr);
4732
4733 rc = hl_hw_queue_send_cb_no_cmpl(hdev, hw_queue_id,
4734 sizeof(struct packet_msg_prot),
4735 pkt_dma_addr);
4736 if (rc) {
4737 dev_err(hdev->dev,
Dotan Barakd6b045c2020-08-06 09:20:49 +03004738 "Failed to send fence packet to H/W queue %d\n",
4739 hw_queue_id);
Oded Gabbayac0ae6a2020-05-11 10:29:27 +03004740 goto free_pkt;
4741 }
4742
4743 rc = hl_poll_timeout_memory(hdev, fence_ptr, tmp, (tmp == fence_val),
4744 1000, timeout_usec, true);
4745
4746 hl_hw_queue_inc_ci_kernel(hdev, hw_queue_id);
4747
4748 if (rc == -ETIMEDOUT) {
4749 dev_err(hdev->dev,
4750 "H/W queue %d test failed (scratch(0x%08llX) == 0x%08X)\n",
4751 hw_queue_id, (unsigned long long) fence_dma_addr, tmp);
4752 rc = -EIO;
4753 }
4754
4755free_pkt:
4756 hdev->asic_funcs->asic_dma_pool_free(hdev, (void *) fence_pkt,
4757 pkt_dma_addr);
4758free_fence_ptr:
4759 hdev->asic_funcs->asic_dma_pool_free(hdev, (void *) fence_ptr,
4760 fence_dma_addr);
4761 return rc;
4762}
4763
4764static int gaudi_test_cpu_queue(struct hl_device *hdev)
4765{
4766 struct gaudi_device *gaudi = hdev->asic_specific;
4767
4768 /*
4769 * check capability here as send_cpu_message() won't update the result
4770 * value if no capability
4771 */
4772 if (!(gaudi->hw_cap_initialized & HW_CAP_CPU_Q))
4773 return 0;
4774
4775 return hl_fw_test_cpu_queue(hdev);
4776}
4777
4778static int gaudi_test_queues(struct hl_device *hdev)
4779{
4780 int i, rc, ret_val = 0;
4781
Ofir Bitton3abc99b2020-06-23 14:50:39 +03004782 for (i = 0 ; i < hdev->asic_prop.max_queues ; i++) {
Oded Gabbayac0ae6a2020-05-11 10:29:27 +03004783 if (hdev->asic_prop.hw_queues_props[i].type == QUEUE_TYPE_EXT) {
4784 rc = gaudi_test_queue(hdev, i);
4785 if (rc)
4786 ret_val = -EINVAL;
4787 }
4788 }
4789
4790 rc = gaudi_test_cpu_queue(hdev);
4791 if (rc)
4792 ret_val = -EINVAL;
4793
4794 return ret_val;
4795}
4796
4797static void *gaudi_dma_pool_zalloc(struct hl_device *hdev, size_t size,
4798 gfp_t mem_flags, dma_addr_t *dma_handle)
4799{
4800 void *kernel_addr;
4801
4802 if (size > GAUDI_DMA_POOL_BLK_SIZE)
4803 return NULL;
4804
4805 kernel_addr = dma_pool_zalloc(hdev->dma_pool, mem_flags, dma_handle);
4806
4807 /* Shift to the device's base physical address of host memory */
4808 if (kernel_addr)
4809 *dma_handle += HOST_PHYS_BASE;
4810
4811 return kernel_addr;
4812}
4813
4814static void gaudi_dma_pool_free(struct hl_device *hdev, void *vaddr,
4815 dma_addr_t dma_addr)
4816{
4817 /* Cancel the device's base physical address of host memory */
4818 dma_addr_t fixed_dma_addr = dma_addr - HOST_PHYS_BASE;
4819
4820 dma_pool_free(hdev->dma_pool, vaddr, fixed_dma_addr);
4821}
4822
4823static void *gaudi_cpu_accessible_dma_pool_alloc(struct hl_device *hdev,
4824 size_t size, dma_addr_t *dma_handle)
4825{
4826 return hl_fw_cpu_accessible_dma_pool_alloc(hdev, size, dma_handle);
4827}
4828
4829static void gaudi_cpu_accessible_dma_pool_free(struct hl_device *hdev,
4830 size_t size, void *vaddr)
4831{
4832 hl_fw_cpu_accessible_dma_pool_free(hdev, size, vaddr);
4833}
4834
4835static int gaudi_dma_map_sg(struct hl_device *hdev, struct scatterlist *sgl,
4836 int nents, enum dma_data_direction dir)
4837{
4838 struct scatterlist *sg;
4839 int i;
4840
4841 if (!dma_map_sg(&hdev->pdev->dev, sgl, nents, dir))
4842 return -ENOMEM;
4843
4844 /* Shift to the device's base physical address of host memory */
4845 for_each_sg(sgl, sg, nents, i)
4846 sg->dma_address += HOST_PHYS_BASE;
4847
4848 return 0;
4849}
4850
4851static void gaudi_dma_unmap_sg(struct hl_device *hdev, struct scatterlist *sgl,
4852 int nents, enum dma_data_direction dir)
4853{
4854 struct scatterlist *sg;
4855 int i;
4856
4857 /* Cancel the device's base physical address of host memory */
4858 for_each_sg(sgl, sg, nents, i)
4859 sg->dma_address -= HOST_PHYS_BASE;
4860
4861 dma_unmap_sg(&hdev->pdev->dev, sgl, nents, dir);
4862}
4863
4864static u32 gaudi_get_dma_desc_list_size(struct hl_device *hdev,
4865 struct sg_table *sgt)
4866{
4867 struct scatterlist *sg, *sg_next_iter;
4868 u32 count, dma_desc_cnt;
4869 u64 len, len_next;
4870 dma_addr_t addr, addr_next;
4871
4872 dma_desc_cnt = 0;
4873
4874 for_each_sg(sgt->sgl, sg, sgt->nents, count) {
4875
4876 len = sg_dma_len(sg);
4877 addr = sg_dma_address(sg);
4878
4879 if (len == 0)
4880 break;
4881
4882 while ((count + 1) < sgt->nents) {
4883 sg_next_iter = sg_next(sg);
4884 len_next = sg_dma_len(sg_next_iter);
4885 addr_next = sg_dma_address(sg_next_iter);
4886
4887 if (len_next == 0)
4888 break;
4889
4890 if ((addr + len == addr_next) &&
4891 (len + len_next <= DMA_MAX_TRANSFER_SIZE)) {
4892 len += len_next;
4893 count++;
4894 sg = sg_next_iter;
4895 } else {
4896 break;
4897 }
4898 }
4899
4900 dma_desc_cnt++;
4901 }
4902
4903 return dma_desc_cnt * sizeof(struct packet_lin_dma);
4904}
4905
4906static int gaudi_pin_memory_before_cs(struct hl_device *hdev,
4907 struct hl_cs_parser *parser,
4908 struct packet_lin_dma *user_dma_pkt,
4909 u64 addr, enum dma_data_direction dir)
4910{
4911 struct hl_userptr *userptr;
4912 int rc;
4913
4914 if (hl_userptr_is_pinned(hdev, addr, le32_to_cpu(user_dma_pkt->tsize),
4915 parser->job_userptr_list, &userptr))
4916 goto already_pinned;
4917
Ofir Bittond5eb8372021-02-14 15:35:56 +02004918 userptr = kzalloc(sizeof(*userptr), GFP_KERNEL);
Oded Gabbayac0ae6a2020-05-11 10:29:27 +03004919 if (!userptr)
4920 return -ENOMEM;
4921
4922 rc = hl_pin_host_memory(hdev, addr, le32_to_cpu(user_dma_pkt->tsize),
4923 userptr);
4924 if (rc)
4925 goto free_userptr;
4926
4927 list_add_tail(&userptr->job_node, parser->job_userptr_list);
4928
4929 rc = hdev->asic_funcs->asic_dma_map_sg(hdev, userptr->sgt->sgl,
4930 userptr->sgt->nents, dir);
4931 if (rc) {
4932 dev_err(hdev->dev, "failed to map sgt with DMA region\n");
4933 goto unpin_memory;
4934 }
4935
4936 userptr->dma_mapped = true;
4937 userptr->dir = dir;
4938
4939already_pinned:
4940 parser->patched_cb_size +=
4941 gaudi_get_dma_desc_list_size(hdev, userptr->sgt);
4942
4943 return 0;
4944
4945unpin_memory:
4946 hl_unpin_host_memory(hdev, userptr);
4947free_userptr:
4948 kfree(userptr);
4949 return rc;
4950}
4951
4952static int gaudi_validate_dma_pkt_host(struct hl_device *hdev,
4953 struct hl_cs_parser *parser,
4954 struct packet_lin_dma *user_dma_pkt,
4955 bool src_in_host)
4956{
4957 enum dma_data_direction dir;
4958 bool skip_host_mem_pin = false, user_memset;
4959 u64 addr;
4960 int rc = 0;
4961
4962 user_memset = (le32_to_cpu(user_dma_pkt->ctl) &
4963 GAUDI_PKT_LIN_DMA_CTL_MEMSET_MASK) >>
4964 GAUDI_PKT_LIN_DMA_CTL_MEMSET_SHIFT;
4965
4966 if (src_in_host) {
4967 if (user_memset)
4968 skip_host_mem_pin = true;
4969
4970 dev_dbg(hdev->dev, "DMA direction is HOST --> DEVICE\n");
4971 dir = DMA_TO_DEVICE;
4972 addr = le64_to_cpu(user_dma_pkt->src_addr);
4973 } else {
4974 dev_dbg(hdev->dev, "DMA direction is DEVICE --> HOST\n");
4975 dir = DMA_FROM_DEVICE;
4976 addr = (le64_to_cpu(user_dma_pkt->dst_addr) &
4977 GAUDI_PKT_LIN_DMA_DST_ADDR_MASK) >>
4978 GAUDI_PKT_LIN_DMA_DST_ADDR_SHIFT;
4979 }
4980
4981 if (skip_host_mem_pin)
4982 parser->patched_cb_size += sizeof(*user_dma_pkt);
4983 else
4984 rc = gaudi_pin_memory_before_cs(hdev, parser, user_dma_pkt,
4985 addr, dir);
4986
4987 return rc;
4988}
4989
4990static int gaudi_validate_dma_pkt_no_mmu(struct hl_device *hdev,
4991 struct hl_cs_parser *parser,
4992 struct packet_lin_dma *user_dma_pkt)
4993{
4994 bool src_in_host = false;
4995 u64 dst_addr = (le64_to_cpu(user_dma_pkt->dst_addr) &
4996 GAUDI_PKT_LIN_DMA_DST_ADDR_MASK) >>
4997 GAUDI_PKT_LIN_DMA_DST_ADDR_SHIFT;
4998
4999 dev_dbg(hdev->dev, "DMA packet details:\n");
5000 dev_dbg(hdev->dev, "source == 0x%llx\n",
5001 le64_to_cpu(user_dma_pkt->src_addr));
5002 dev_dbg(hdev->dev, "destination == 0x%llx\n", dst_addr);
5003 dev_dbg(hdev->dev, "size == %u\n", le32_to_cpu(user_dma_pkt->tsize));
5004
5005 /*
5006 * Special handling for DMA with size 0. Bypass all validations
5007 * because no transactions will be done except for WR_COMP, which
5008 * is not a security issue
5009 */
5010 if (!le32_to_cpu(user_dma_pkt->tsize)) {
5011 parser->patched_cb_size += sizeof(*user_dma_pkt);
5012 return 0;
5013 }
5014
5015 if (parser->hw_queue_id <= GAUDI_QUEUE_ID_DMA_0_3)
5016 src_in_host = true;
5017
5018 return gaudi_validate_dma_pkt_host(hdev, parser, user_dma_pkt,
5019 src_in_host);
5020}
5021
Oded Gabbay64536ab2020-05-27 12:38:16 +03005022static int gaudi_validate_load_and_exe_pkt(struct hl_device *hdev,
5023 struct hl_cs_parser *parser,
5024 struct packet_load_and_exe *user_pkt)
5025{
5026 u32 cfg;
5027
5028 cfg = le32_to_cpu(user_pkt->cfg);
5029
5030 if (cfg & GAUDI_PKT_LOAD_AND_EXE_CFG_DST_MASK) {
5031 dev_err(hdev->dev,
5032 "User not allowed to use Load and Execute\n");
5033 return -EPERM;
5034 }
5035
5036 parser->patched_cb_size += sizeof(struct packet_load_and_exe);
5037
5038 return 0;
5039}
5040
Oded Gabbayac0ae6a2020-05-11 10:29:27 +03005041static int gaudi_validate_cb(struct hl_device *hdev,
5042 struct hl_cs_parser *parser, bool is_mmu)
5043{
5044 u32 cb_parsed_length = 0;
5045 int rc = 0;
5046
5047 parser->patched_cb_size = 0;
5048
5049 /* cb_user_size is more than 0 so loop will always be executed */
5050 while (cb_parsed_length < parser->user_cb_size) {
5051 enum packet_id pkt_id;
5052 u16 pkt_size;
5053 struct gaudi_packet *user_pkt;
5054
Arnd Bergmann82948e62020-10-26 17:08:06 +01005055 user_pkt = parser->user_cb->kernel_address + cb_parsed_length;
Oded Gabbayac0ae6a2020-05-11 10:29:27 +03005056
5057 pkt_id = (enum packet_id) (
5058 (le64_to_cpu(user_pkt->header) &
5059 PACKET_HEADER_PACKET_ID_MASK) >>
5060 PACKET_HEADER_PACKET_ID_SHIFT);
5061
Ofir Bittonbc75be22020-07-30 14:56:38 +03005062 if (!validate_packet_id(pkt_id)) {
5063 dev_err(hdev->dev, "Invalid packet id %u\n", pkt_id);
5064 rc = -EINVAL;
5065 break;
5066 }
5067
Oded Gabbayac0ae6a2020-05-11 10:29:27 +03005068 pkt_size = gaudi_packet_sizes[pkt_id];
5069 cb_parsed_length += pkt_size;
5070 if (cb_parsed_length > parser->user_cb_size) {
5071 dev_err(hdev->dev,
5072 "packet 0x%x is out of CB boundary\n", pkt_id);
5073 rc = -EINVAL;
5074 break;
5075 }
5076
5077 switch (pkt_id) {
5078 case PACKET_MSG_PROT:
5079 dev_err(hdev->dev,
5080 "User not allowed to use MSG_PROT\n");
5081 rc = -EPERM;
5082 break;
5083
5084 case PACKET_CP_DMA:
5085 dev_err(hdev->dev, "User not allowed to use CP_DMA\n");
5086 rc = -EPERM;
5087 break;
5088
5089 case PACKET_STOP:
5090 dev_err(hdev->dev, "User not allowed to use STOP\n");
5091 rc = -EPERM;
5092 break;
5093
Oded Gabbay2edc66e2020-07-03 19:28:54 +03005094 case PACKET_WREG_BULK:
5095 dev_err(hdev->dev,
5096 "User not allowed to use WREG_BULK\n");
5097 rc = -EPERM;
5098 break;
5099
Oded Gabbay64536ab2020-05-27 12:38:16 +03005100 case PACKET_LOAD_AND_EXE:
5101 rc = gaudi_validate_load_and_exe_pkt(hdev, parser,
5102 (struct packet_load_and_exe *) user_pkt);
5103 break;
5104
Oded Gabbayac0ae6a2020-05-11 10:29:27 +03005105 case PACKET_LIN_DMA:
5106 parser->contains_dma_pkt = true;
5107 if (is_mmu)
5108 parser->patched_cb_size += pkt_size;
5109 else
5110 rc = gaudi_validate_dma_pkt_no_mmu(hdev, parser,
5111 (struct packet_lin_dma *) user_pkt);
5112 break;
5113
5114 case PACKET_WREG_32:
Oded Gabbayac0ae6a2020-05-11 10:29:27 +03005115 case PACKET_MSG_LONG:
5116 case PACKET_MSG_SHORT:
5117 case PACKET_REPEAT:
5118 case PACKET_FENCE:
5119 case PACKET_NOP:
5120 case PACKET_ARB_POINT:
Oded Gabbayac0ae6a2020-05-11 10:29:27 +03005121 parser->patched_cb_size += pkt_size;
5122 break;
5123
5124 default:
5125 dev_err(hdev->dev, "Invalid packet header 0x%x\n",
5126 pkt_id);
5127 rc = -EINVAL;
5128 break;
5129 }
5130
5131 if (rc)
5132 break;
5133 }
5134
5135 /*
5136 * The new CB should have space at the end for two MSG_PROT packets:
5137 * 1. A packet that will act as a completion packet
5138 * 2. A packet that will generate MSI-X interrupt
5139 */
Ofir Bittonac6fdbf2020-12-03 16:59:28 +02005140 if (parser->completion)
5141 parser->patched_cb_size += sizeof(struct packet_msg_prot) * 2;
Oded Gabbayac0ae6a2020-05-11 10:29:27 +03005142
5143 return rc;
5144}
5145
5146static int gaudi_patch_dma_packet(struct hl_device *hdev,
5147 struct hl_cs_parser *parser,
5148 struct packet_lin_dma *user_dma_pkt,
5149 struct packet_lin_dma *new_dma_pkt,
5150 u32 *new_dma_pkt_size)
5151{
5152 struct hl_userptr *userptr;
5153 struct scatterlist *sg, *sg_next_iter;
5154 u32 count, dma_desc_cnt, user_wrcomp_en_mask, ctl;
5155 u64 len, len_next;
5156 dma_addr_t dma_addr, dma_addr_next;
5157 u64 device_memory_addr, addr;
5158 enum dma_data_direction dir;
5159 struct sg_table *sgt;
5160 bool src_in_host = false;
5161 bool skip_host_mem_pin = false;
5162 bool user_memset;
5163
5164 ctl = le32_to_cpu(user_dma_pkt->ctl);
5165
5166 if (parser->hw_queue_id <= GAUDI_QUEUE_ID_DMA_0_3)
5167 src_in_host = true;
5168
5169 user_memset = (ctl & GAUDI_PKT_LIN_DMA_CTL_MEMSET_MASK) >>
5170 GAUDI_PKT_LIN_DMA_CTL_MEMSET_SHIFT;
5171
5172 if (src_in_host) {
5173 addr = le64_to_cpu(user_dma_pkt->src_addr);
5174 device_memory_addr = le64_to_cpu(user_dma_pkt->dst_addr);
5175 dir = DMA_TO_DEVICE;
5176 if (user_memset)
5177 skip_host_mem_pin = true;
5178 } else {
5179 addr = le64_to_cpu(user_dma_pkt->dst_addr);
5180 device_memory_addr = le64_to_cpu(user_dma_pkt->src_addr);
5181 dir = DMA_FROM_DEVICE;
5182 }
5183
5184 if ((!skip_host_mem_pin) &&
5185 (!hl_userptr_is_pinned(hdev, addr,
5186 le32_to_cpu(user_dma_pkt->tsize),
5187 parser->job_userptr_list, &userptr))) {
5188 dev_err(hdev->dev, "Userptr 0x%llx + 0x%x NOT mapped\n",
5189 addr, user_dma_pkt->tsize);
5190 return -EFAULT;
5191 }
5192
5193 if ((user_memset) && (dir == DMA_TO_DEVICE)) {
5194 memcpy(new_dma_pkt, user_dma_pkt, sizeof(*user_dma_pkt));
5195 *new_dma_pkt_size = sizeof(*user_dma_pkt);
5196 return 0;
5197 }
5198
5199 user_wrcomp_en_mask = ctl & GAUDI_PKT_LIN_DMA_CTL_WRCOMP_EN_MASK;
5200
5201 sgt = userptr->sgt;
5202 dma_desc_cnt = 0;
5203
5204 for_each_sg(sgt->sgl, sg, sgt->nents, count) {
5205 len = sg_dma_len(sg);
5206 dma_addr = sg_dma_address(sg);
5207
5208 if (len == 0)
5209 break;
5210
5211 while ((count + 1) < sgt->nents) {
5212 sg_next_iter = sg_next(sg);
5213 len_next = sg_dma_len(sg_next_iter);
5214 dma_addr_next = sg_dma_address(sg_next_iter);
5215
5216 if (len_next == 0)
5217 break;
5218
5219 if ((dma_addr + len == dma_addr_next) &&
5220 (len + len_next <= DMA_MAX_TRANSFER_SIZE)) {
5221 len += len_next;
5222 count++;
5223 sg = sg_next_iter;
5224 } else {
5225 break;
5226 }
5227 }
5228
Oded Gabbayac0ae6a2020-05-11 10:29:27 +03005229 ctl = le32_to_cpu(user_dma_pkt->ctl);
5230 if (likely(dma_desc_cnt))
5231 ctl &= ~GAUDI_PKT_CTL_EB_MASK;
5232 ctl &= ~GAUDI_PKT_LIN_DMA_CTL_WRCOMP_EN_MASK;
5233 new_dma_pkt->ctl = cpu_to_le32(ctl);
5234 new_dma_pkt->tsize = cpu_to_le32(len);
5235
5236 if (dir == DMA_TO_DEVICE) {
5237 new_dma_pkt->src_addr = cpu_to_le64(dma_addr);
5238 new_dma_pkt->dst_addr = cpu_to_le64(device_memory_addr);
5239 } else {
5240 new_dma_pkt->src_addr = cpu_to_le64(device_memory_addr);
5241 new_dma_pkt->dst_addr = cpu_to_le64(dma_addr);
5242 }
5243
5244 if (!user_memset)
5245 device_memory_addr += len;
5246 dma_desc_cnt++;
5247 new_dma_pkt++;
5248 }
5249
5250 if (!dma_desc_cnt) {
5251 dev_err(hdev->dev,
5252 "Error of 0 SG entries when patching DMA packet\n");
5253 return -EFAULT;
5254 }
5255
5256 /* Fix the last dma packet - wrcomp must be as user set it */
5257 new_dma_pkt--;
5258 new_dma_pkt->ctl |= cpu_to_le32(user_wrcomp_en_mask);
5259
5260 *new_dma_pkt_size = dma_desc_cnt * sizeof(struct packet_lin_dma);
5261
5262 return 0;
5263}
5264
5265static int gaudi_patch_cb(struct hl_device *hdev,
5266 struct hl_cs_parser *parser)
5267{
5268 u32 cb_parsed_length = 0;
5269 u32 cb_patched_cur_length = 0;
5270 int rc = 0;
5271
5272 /* cb_user_size is more than 0 so loop will always be executed */
5273 while (cb_parsed_length < parser->user_cb_size) {
5274 enum packet_id pkt_id;
5275 u16 pkt_size;
5276 u32 new_pkt_size = 0;
5277 struct gaudi_packet *user_pkt, *kernel_pkt;
5278
Arnd Bergmann82948e62020-10-26 17:08:06 +01005279 user_pkt = parser->user_cb->kernel_address + cb_parsed_length;
5280 kernel_pkt = parser->patched_cb->kernel_address +
5281 cb_patched_cur_length;
Oded Gabbayac0ae6a2020-05-11 10:29:27 +03005282
5283 pkt_id = (enum packet_id) (
5284 (le64_to_cpu(user_pkt->header) &
5285 PACKET_HEADER_PACKET_ID_MASK) >>
5286 PACKET_HEADER_PACKET_ID_SHIFT);
5287
Ofir Bittonbc75be22020-07-30 14:56:38 +03005288 if (!validate_packet_id(pkt_id)) {
5289 dev_err(hdev->dev, "Invalid packet id %u\n", pkt_id);
5290 rc = -EINVAL;
5291 break;
5292 }
5293
Oded Gabbayac0ae6a2020-05-11 10:29:27 +03005294 pkt_size = gaudi_packet_sizes[pkt_id];
5295 cb_parsed_length += pkt_size;
5296 if (cb_parsed_length > parser->user_cb_size) {
5297 dev_err(hdev->dev,
5298 "packet 0x%x is out of CB boundary\n", pkt_id);
5299 rc = -EINVAL;
5300 break;
5301 }
5302
5303 switch (pkt_id) {
5304 case PACKET_LIN_DMA:
5305 rc = gaudi_patch_dma_packet(hdev, parser,
5306 (struct packet_lin_dma *) user_pkt,
5307 (struct packet_lin_dma *) kernel_pkt,
5308 &new_pkt_size);
5309 cb_patched_cur_length += new_pkt_size;
5310 break;
5311
5312 case PACKET_MSG_PROT:
5313 dev_err(hdev->dev,
5314 "User not allowed to use MSG_PROT\n");
5315 rc = -EPERM;
5316 break;
5317
5318 case PACKET_CP_DMA:
5319 dev_err(hdev->dev, "User not allowed to use CP_DMA\n");
5320 rc = -EPERM;
5321 break;
5322
5323 case PACKET_STOP:
5324 dev_err(hdev->dev, "User not allowed to use STOP\n");
5325 rc = -EPERM;
5326 break;
5327
5328 case PACKET_WREG_32:
5329 case PACKET_WREG_BULK:
5330 case PACKET_MSG_LONG:
5331 case PACKET_MSG_SHORT:
5332 case PACKET_REPEAT:
5333 case PACKET_FENCE:
5334 case PACKET_NOP:
5335 case PACKET_ARB_POINT:
5336 case PACKET_LOAD_AND_EXE:
5337 memcpy(kernel_pkt, user_pkt, pkt_size);
5338 cb_patched_cur_length += pkt_size;
5339 break;
5340
5341 default:
5342 dev_err(hdev->dev, "Invalid packet header 0x%x\n",
5343 pkt_id);
5344 rc = -EINVAL;
5345 break;
5346 }
5347
5348 if (rc)
5349 break;
5350 }
5351
5352 return rc;
5353}
5354
5355static int gaudi_parse_cb_mmu(struct hl_device *hdev,
5356 struct hl_cs_parser *parser)
5357{
5358 u64 patched_cb_handle;
5359 u32 patched_cb_size;
5360 struct hl_cb *user_cb;
5361 int rc;
5362
5363 /*
5364 * The new CB should have space at the end for two MSG_PROT pkt:
5365 * 1. A packet that will act as a completion packet
5366 * 2. A packet that will generate MSI interrupt
5367 */
Ofir Bittonac6fdbf2020-12-03 16:59:28 +02005368 if (parser->completion)
5369 parser->patched_cb_size = parser->user_cb_size +
5370 sizeof(struct packet_msg_prot) * 2;
5371 else
5372 parser->patched_cb_size = parser->user_cb_size;
Oded Gabbayac0ae6a2020-05-11 10:29:27 +03005373
Tomer Tayarfa8641a12020-09-07 17:36:41 +03005374 rc = hl_cb_create(hdev, &hdev->kernel_cb_mgr, hdev->kernel_ctx,
Tomer Tayaref6a0f62020-07-09 16:17:48 +03005375 parser->patched_cb_size, false, false,
Tomer Tayarfa8641a12020-09-07 17:36:41 +03005376 &patched_cb_handle);
Oded Gabbayac0ae6a2020-05-11 10:29:27 +03005377
5378 if (rc) {
5379 dev_err(hdev->dev,
5380 "Failed to allocate patched CB for DMA CS %d\n",
5381 rc);
5382 return rc;
5383 }
5384
5385 patched_cb_handle >>= PAGE_SHIFT;
5386 parser->patched_cb = hl_cb_get(hdev, &hdev->kernel_cb_mgr,
5387 (u32) patched_cb_handle);
Alon Mizrahi75d9a2a2020-12-03 17:32:19 +02005388 /* hl_cb_get should never fail */
Oded Gabbayac0ae6a2020-05-11 10:29:27 +03005389 if (!parser->patched_cb) {
Alon Mizrahi75d9a2a2020-12-03 17:32:19 +02005390 dev_crit(hdev->dev, "DMA CB handle invalid 0x%x\n",
5391 (u32) patched_cb_handle);
Oded Gabbayac0ae6a2020-05-11 10:29:27 +03005392 rc = -EFAULT;
5393 goto out;
5394 }
5395
5396 /*
5397 * The check that parser->user_cb_size <= parser->user_cb->size was done
5398 * in validate_queue_index().
5399 */
Arnd Bergmann82948e62020-10-26 17:08:06 +01005400 memcpy(parser->patched_cb->kernel_address,
5401 parser->user_cb->kernel_address,
Oded Gabbayac0ae6a2020-05-11 10:29:27 +03005402 parser->user_cb_size);
5403
5404 patched_cb_size = parser->patched_cb_size;
5405
5406 /* Validate patched CB instead of user CB */
5407 user_cb = parser->user_cb;
5408 parser->user_cb = parser->patched_cb;
5409 rc = gaudi_validate_cb(hdev, parser, true);
5410 parser->user_cb = user_cb;
5411
5412 if (rc) {
5413 hl_cb_put(parser->patched_cb);
5414 goto out;
5415 }
5416
5417 if (patched_cb_size != parser->patched_cb_size) {
5418 dev_err(hdev->dev, "user CB size mismatch\n");
5419 hl_cb_put(parser->patched_cb);
5420 rc = -EINVAL;
5421 goto out;
5422 }
5423
5424out:
5425 /*
5426 * Always call cb destroy here because we still have 1 reference
5427 * to it by calling cb_get earlier. After the job will be completed,
5428 * cb_put will release it, but here we want to remove it from the
5429 * idr
5430 */
5431 hl_cb_destroy(hdev, &hdev->kernel_cb_mgr,
5432 patched_cb_handle << PAGE_SHIFT);
5433
5434 return rc;
5435}
5436
5437static int gaudi_parse_cb_no_mmu(struct hl_device *hdev,
5438 struct hl_cs_parser *parser)
5439{
5440 u64 patched_cb_handle;
5441 int rc;
5442
5443 rc = gaudi_validate_cb(hdev, parser, false);
5444
5445 if (rc)
5446 goto free_userptr;
5447
Tomer Tayarfa8641a12020-09-07 17:36:41 +03005448 rc = hl_cb_create(hdev, &hdev->kernel_cb_mgr, hdev->kernel_ctx,
Tomer Tayaref6a0f62020-07-09 16:17:48 +03005449 parser->patched_cb_size, false, false,
Tomer Tayarfa8641a12020-09-07 17:36:41 +03005450 &patched_cb_handle);
Oded Gabbayac0ae6a2020-05-11 10:29:27 +03005451 if (rc) {
5452 dev_err(hdev->dev,
5453 "Failed to allocate patched CB for DMA CS %d\n", rc);
5454 goto free_userptr;
5455 }
5456
5457 patched_cb_handle >>= PAGE_SHIFT;
5458 parser->patched_cb = hl_cb_get(hdev, &hdev->kernel_cb_mgr,
5459 (u32) patched_cb_handle);
Alon Mizrahi75d9a2a2020-12-03 17:32:19 +02005460 /* hl_cb_get should never fail here */
Oded Gabbayac0ae6a2020-05-11 10:29:27 +03005461 if (!parser->patched_cb) {
Alon Mizrahi75d9a2a2020-12-03 17:32:19 +02005462 dev_crit(hdev->dev, "DMA CB handle invalid 0x%x\n",
5463 (u32) patched_cb_handle);
Oded Gabbayac0ae6a2020-05-11 10:29:27 +03005464 rc = -EFAULT;
5465 goto out;
5466 }
5467
5468 rc = gaudi_patch_cb(hdev, parser);
5469
5470 if (rc)
5471 hl_cb_put(parser->patched_cb);
5472
5473out:
5474 /*
5475 * Always call cb destroy here because we still have 1 reference
5476 * to it by calling cb_get earlier. After the job will be completed,
5477 * cb_put will release it, but here we want to remove it from the
5478 * idr
5479 */
5480 hl_cb_destroy(hdev, &hdev->kernel_cb_mgr,
5481 patched_cb_handle << PAGE_SHIFT);
5482
5483free_userptr:
5484 if (rc)
5485 hl_userptr_delete_list(hdev, parser->job_userptr_list);
5486 return rc;
5487}
5488
5489static int gaudi_parse_cb_no_ext_queue(struct hl_device *hdev,
5490 struct hl_cs_parser *parser)
5491{
5492 struct asic_fixed_properties *asic_prop = &hdev->asic_prop;
Oded Gabbay3c681572020-11-02 21:10:39 +02005493 struct gaudi_device *gaudi = hdev->asic_specific;
5494 u32 nic_mask_q_id = 1 << (HW_CAP_NIC_SHIFT +
5495 ((parser->hw_queue_id - GAUDI_QUEUE_ID_NIC_0_0) >> 2));
5496
5497 if ((parser->hw_queue_id >= GAUDI_QUEUE_ID_NIC_0_0) &&
5498 (parser->hw_queue_id <= GAUDI_QUEUE_ID_NIC_9_3) &&
5499 (!(gaudi->hw_cap_initialized & nic_mask_q_id))) {
5500 dev_err(hdev->dev, "h/w queue %d is disabled\n",
5501 parser->hw_queue_id);
5502 return -EINVAL;
5503 }
Oded Gabbayac0ae6a2020-05-11 10:29:27 +03005504
5505 /* For internal queue jobs just check if CB address is valid */
5506 if (hl_mem_area_inside_range((u64) (uintptr_t) parser->user_cb,
5507 parser->user_cb_size,
5508 asic_prop->sram_user_base_address,
5509 asic_prop->sram_end_address))
5510 return 0;
5511
5512 if (hl_mem_area_inside_range((u64) (uintptr_t) parser->user_cb,
5513 parser->user_cb_size,
5514 asic_prop->dram_user_base_address,
5515 asic_prop->dram_end_address))
5516 return 0;
5517
5518 /* PMMU and HPMMU addresses are equal, check only one of them */
5519 if (hl_mem_area_inside_range((u64) (uintptr_t) parser->user_cb,
5520 parser->user_cb_size,
5521 asic_prop->pmmu.start_addr,
5522 asic_prop->pmmu.end_addr))
5523 return 0;
5524
5525 dev_err(hdev->dev,
5526 "CB address 0x%px + 0x%x for internal QMAN is not valid\n",
5527 parser->user_cb, parser->user_cb_size);
5528
5529 return -EFAULT;
5530}
5531
5532static int gaudi_cs_parser(struct hl_device *hdev, struct hl_cs_parser *parser)
5533{
5534 struct gaudi_device *gaudi = hdev->asic_specific;
5535
5536 if (parser->queue_type == QUEUE_TYPE_INT)
5537 return gaudi_parse_cb_no_ext_queue(hdev, parser);
5538
5539 if (gaudi->hw_cap_initialized & HW_CAP_MMU)
5540 return gaudi_parse_cb_mmu(hdev, parser);
5541 else
5542 return gaudi_parse_cb_no_mmu(hdev, parser);
5543}
5544
5545static void gaudi_add_end_of_cb_packets(struct hl_device *hdev,
Arnd Bergmann82948e62020-10-26 17:08:06 +01005546 void *kernel_address, u32 len,
Oded Gabbayac0ae6a2020-05-11 10:29:27 +03005547 u64 cq_addr, u32 cq_val, u32 msi_vec,
5548 bool eb)
5549{
5550 struct gaudi_device *gaudi = hdev->asic_specific;
5551 struct packet_msg_prot *cq_pkt;
5552 u32 tmp;
5553
Arnd Bergmann82948e62020-10-26 17:08:06 +01005554 cq_pkt = kernel_address + len - (sizeof(struct packet_msg_prot) * 2);
Oded Gabbayac0ae6a2020-05-11 10:29:27 +03005555
Oded Gabbay65887292020-08-12 11:21:01 +03005556 tmp = FIELD_PREP(GAUDI_PKT_CTL_OPCODE_MASK, PACKET_MSG_PROT);
5557 tmp |= FIELD_PREP(GAUDI_PKT_CTL_MB_MASK, 1);
Oded Gabbayac0ae6a2020-05-11 10:29:27 +03005558
5559 if (eb)
Oded Gabbay65887292020-08-12 11:21:01 +03005560 tmp |= FIELD_PREP(GAUDI_PKT_CTL_EB_MASK, 1);
Oded Gabbayac0ae6a2020-05-11 10:29:27 +03005561
5562 cq_pkt->ctl = cpu_to_le32(tmp);
5563 cq_pkt->value = cpu_to_le32(cq_val);
5564 cq_pkt->addr = cpu_to_le64(cq_addr);
5565
5566 cq_pkt++;
5567
Oded Gabbay65887292020-08-12 11:21:01 +03005568 tmp = FIELD_PREP(GAUDI_PKT_CTL_OPCODE_MASK, PACKET_MSG_PROT);
5569 tmp |= FIELD_PREP(GAUDI_PKT_CTL_MB_MASK, 1);
Oded Gabbayac0ae6a2020-05-11 10:29:27 +03005570 cq_pkt->ctl = cpu_to_le32(tmp);
5571 cq_pkt->value = cpu_to_le32(1);
5572
5573 if (!gaudi->multi_msi_mode)
5574 msi_vec = 0;
5575
5576 cq_pkt->addr = cpu_to_le64(CFG_BASE + mmPCIE_MSI_INTR_0 + msi_vec * 4);
5577}
5578
5579static void gaudi_update_eq_ci(struct hl_device *hdev, u32 val)
5580{
5581 WREG32(mmCPU_IF_EQ_RD_OFFS, val);
5582}
5583
5584static int gaudi_memset_device_memory(struct hl_device *hdev, u64 addr,
5585 u32 size, u64 val)
5586{
5587 struct packet_lin_dma *lin_dma_pkt;
5588 struct hl_cs_job *job;
Moti Haimovskia9855a2d92020-06-24 19:40:57 +03005589 u32 cb_size, ctl, err_cause;
Oded Gabbayac0ae6a2020-05-11 10:29:27 +03005590 struct hl_cb *cb;
Lv Yunlong115726c2021-04-26 06:43:46 -07005591 u64 id;
Oded Gabbayac0ae6a2020-05-11 10:29:27 +03005592 int rc;
5593
Ofir Bittona04b7cd2020-07-13 13:36:55 +03005594 cb = hl_cb_kernel_create(hdev, PAGE_SIZE, false);
Oded Gabbayac0ae6a2020-05-11 10:29:27 +03005595 if (!cb)
5596 return -EFAULT;
5597
Arnd Bergmann82948e62020-10-26 17:08:06 +01005598 lin_dma_pkt = cb->kernel_address;
Oded Gabbayac0ae6a2020-05-11 10:29:27 +03005599 memset(lin_dma_pkt, 0, sizeof(*lin_dma_pkt));
5600 cb_size = sizeof(*lin_dma_pkt);
5601
Oded Gabbay65887292020-08-12 11:21:01 +03005602 ctl = FIELD_PREP(GAUDI_PKT_CTL_OPCODE_MASK, PACKET_LIN_DMA);
5603 ctl |= FIELD_PREP(GAUDI_PKT_LIN_DMA_CTL_MEMSET_MASK, 1);
5604 ctl |= FIELD_PREP(GAUDI_PKT_LIN_DMA_CTL_LIN_MASK, 1);
5605 ctl |= FIELD_PREP(GAUDI_PKT_CTL_MB_MASK, 1);
5606 ctl |= FIELD_PREP(GAUDI_PKT_CTL_RB_MASK, 1);
5607
Oded Gabbayac0ae6a2020-05-11 10:29:27 +03005608 lin_dma_pkt->ctl = cpu_to_le32(ctl);
5609 lin_dma_pkt->src_addr = cpu_to_le64(val);
5610 lin_dma_pkt->dst_addr |= cpu_to_le64(addr);
5611 lin_dma_pkt->tsize = cpu_to_le32(size);
5612
5613 job = hl_cs_allocate_job(hdev, QUEUE_TYPE_EXT, true);
5614 if (!job) {
5615 dev_err(hdev->dev, "Failed to allocate a new job\n");
5616 rc = -ENOMEM;
5617 goto release_cb;
5618 }
5619
Moti Haimovskia9855a2d92020-06-24 19:40:57 +03005620 /* Verify DMA is OK */
5621 err_cause = RREG32(mmDMA0_CORE_ERR_CAUSE);
5622 if (err_cause && !hdev->init_done) {
5623 dev_dbg(hdev->dev,
5624 "Clearing DMA0 engine from errors (cause 0x%x)\n",
5625 err_cause);
5626 WREG32(mmDMA0_CORE_ERR_CAUSE, err_cause);
5627 }
5628
Oded Gabbayac0ae6a2020-05-11 10:29:27 +03005629 job->id = 0;
5630 job->user_cb = cb;
Tomer Tayarf07486742020-08-02 22:51:31 +03005631 atomic_inc(&job->user_cb->cs_cnt);
Oded Gabbayac0ae6a2020-05-11 10:29:27 +03005632 job->user_cb_size = cb_size;
5633 job->hw_queue_id = GAUDI_QUEUE_ID_DMA_0_0;
5634 job->patched_cb = job->user_cb;
5635 job->job_cb_size = job->user_cb_size + sizeof(struct packet_msg_prot);
5636
5637 hl_debugfs_add_job(hdev, job);
5638
5639 rc = gaudi_send_job_on_qman0(hdev, job);
Oded Gabbayac0ae6a2020-05-11 10:29:27 +03005640 hl_debugfs_remove_job(hdev, job);
5641 kfree(job);
Tomer Tayarf07486742020-08-02 22:51:31 +03005642 atomic_dec(&cb->cs_cnt);
Oded Gabbayac0ae6a2020-05-11 10:29:27 +03005643
Moti Haimovskia9855a2d92020-06-24 19:40:57 +03005644 /* Verify DMA is OK */
5645 err_cause = RREG32(mmDMA0_CORE_ERR_CAUSE);
5646 if (err_cause) {
5647 dev_err(hdev->dev, "DMA Failed, cause 0x%x\n", err_cause);
5648 rc = -EIO;
5649 if (!hdev->init_done) {
5650 dev_dbg(hdev->dev,
5651 "Clearing DMA0 engine from errors (cause 0x%x)\n",
5652 err_cause);
5653 WREG32(mmDMA0_CORE_ERR_CAUSE, err_cause);
5654 }
5655 }
5656
Oded Gabbayac0ae6a2020-05-11 10:29:27 +03005657release_cb:
Lv Yunlong115726c2021-04-26 06:43:46 -07005658 id = cb->id;
Oded Gabbayac0ae6a2020-05-11 10:29:27 +03005659 hl_cb_put(cb);
Lv Yunlong115726c2021-04-26 06:43:46 -07005660 hl_cb_destroy(hdev, &hdev->kernel_cb_mgr, id << PAGE_SHIFT);
Oded Gabbayac0ae6a2020-05-11 10:29:27 +03005661
5662 return rc;
5663}
5664
Ofir Bitton423815b2021-01-05 09:04:07 +02005665static int gaudi_memset_registers(struct hl_device *hdev, u64 reg_base,
5666 u32 num_regs, u32 val)
Oded Gabbayac0ae6a2020-05-11 10:29:27 +03005667{
Ofir Bitton423815b2021-01-05 09:04:07 +02005668 struct packet_msg_long *pkt;
5669 struct hl_cs_job *job;
5670 u32 cb_size, ctl;
5671 struct hl_cb *cb;
5672 int i, rc;
5673
5674 cb_size = (sizeof(*pkt) * num_regs) + sizeof(struct packet_msg_prot);
5675
5676 if (cb_size > SZ_2M) {
5677 dev_err(hdev->dev, "CB size must be smaller than %uMB", SZ_2M);
5678 return -ENOMEM;
5679 }
5680
5681 cb = hl_cb_kernel_create(hdev, cb_size, false);
5682 if (!cb)
5683 return -EFAULT;
5684
5685 pkt = cb->kernel_address;
5686
5687 ctl = FIELD_PREP(GAUDI_PKT_LONG_CTL_OP_MASK, 0); /* write the value */
5688 ctl |= FIELD_PREP(GAUDI_PKT_CTL_OPCODE_MASK, PACKET_MSG_LONG);
5689 ctl |= FIELD_PREP(GAUDI_PKT_CTL_EB_MASK, 1);
5690 ctl |= FIELD_PREP(GAUDI_PKT_CTL_RB_MASK, 1);
5691 ctl |= FIELD_PREP(GAUDI_PKT_CTL_MB_MASK, 1);
5692
5693 for (i = 0; i < num_regs ; i++, pkt++) {
5694 pkt->ctl = cpu_to_le32(ctl);
5695 pkt->value = cpu_to_le32(val);
5696 pkt->addr = cpu_to_le64(reg_base + (i * 4));
5697 }
5698
5699 job = hl_cs_allocate_job(hdev, QUEUE_TYPE_EXT, true);
5700 if (!job) {
5701 dev_err(hdev->dev, "Failed to allocate a new job\n");
5702 rc = -ENOMEM;
5703 goto release_cb;
5704 }
5705
5706 job->id = 0;
5707 job->user_cb = cb;
5708 atomic_inc(&job->user_cb->cs_cnt);
5709 job->user_cb_size = cb_size;
5710 job->hw_queue_id = GAUDI_QUEUE_ID_DMA_0_0;
5711 job->patched_cb = job->user_cb;
5712 job->job_cb_size = cb_size;
5713
5714 hl_debugfs_add_job(hdev, job);
5715
5716 rc = gaudi_send_job_on_qman0(hdev, job);
5717 hl_debugfs_remove_job(hdev, job);
5718 kfree(job);
5719 atomic_dec(&cb->cs_cnt);
5720
5721release_cb:
5722 hl_cb_put(cb);
5723 hl_cb_destroy(hdev, &hdev->kernel_cb_mgr, cb->id << PAGE_SHIFT);
5724
5725 return rc;
5726}
5727
5728static int gaudi_schedule_register_memset(struct hl_device *hdev,
5729 u32 hw_queue_id, u64 reg_base, u32 num_regs, u32 val)
5730{
farah kassabrie65448f2021-03-30 16:38:02 +03005731 struct hl_ctx *ctx;
Ofir Bitton423815b2021-01-05 09:04:07 +02005732 struct hl_pending_cb *pending_cb;
5733 struct packet_msg_long *pkt;
5734 u32 cb_size, ctl;
5735 struct hl_cb *cb;
farah kassabrie65448f2021-03-30 16:38:02 +03005736 int i, rc;
5737
5738 mutex_lock(&hdev->fpriv_list_lock);
5739 ctx = hdev->compute_ctx;
Oded Gabbayac0ae6a2020-05-11 10:29:27 +03005740
Ofir Bitton423815b2021-01-05 09:04:07 +02005741 /* If no compute context available or context is going down
5742 * memset registers directly
5743 */
farah kassabrie65448f2021-03-30 16:38:02 +03005744 if (!ctx || kref_read(&ctx->refcount) == 0) {
5745 rc = gaudi_memset_registers(hdev, reg_base, num_regs, val);
5746 mutex_unlock(&hdev->fpriv_list_lock);
5747 return rc;
5748 }
5749
5750 mutex_unlock(&hdev->fpriv_list_lock);
Ofir Bitton423815b2021-01-05 09:04:07 +02005751
5752 cb_size = (sizeof(*pkt) * num_regs) +
5753 sizeof(struct packet_msg_prot) * 2;
5754
5755 if (cb_size > SZ_2M) {
5756 dev_err(hdev->dev, "CB size must be smaller than %uMB", SZ_2M);
5757 return -ENOMEM;
Oded Gabbayac0ae6a2020-05-11 10:29:27 +03005758 }
5759
Ofir Bitton423815b2021-01-05 09:04:07 +02005760 pending_cb = kzalloc(sizeof(*pending_cb), GFP_KERNEL);
5761 if (!pending_cb)
5762 return -ENOMEM;
5763
5764 cb = hl_cb_kernel_create(hdev, cb_size, false);
5765 if (!cb) {
5766 kfree(pending_cb);
5767 return -EFAULT;
Oded Gabbayac0ae6a2020-05-11 10:29:27 +03005768 }
5769
Ofir Bitton423815b2021-01-05 09:04:07 +02005770 pkt = cb->kernel_address;
Oded Gabbayac0ae6a2020-05-11 10:29:27 +03005771
Ofir Bitton423815b2021-01-05 09:04:07 +02005772 ctl = FIELD_PREP(GAUDI_PKT_LONG_CTL_OP_MASK, 0); /* write the value */
5773 ctl |= FIELD_PREP(GAUDI_PKT_CTL_OPCODE_MASK, PACKET_MSG_LONG);
5774 ctl |= FIELD_PREP(GAUDI_PKT_CTL_EB_MASK, 1);
5775 ctl |= FIELD_PREP(GAUDI_PKT_CTL_RB_MASK, 1);
5776 ctl |= FIELD_PREP(GAUDI_PKT_CTL_MB_MASK, 1);
Oded Gabbayac0ae6a2020-05-11 10:29:27 +03005777
Ofir Bitton423815b2021-01-05 09:04:07 +02005778 for (i = 0; i < num_regs ; i++, pkt++) {
5779 pkt->ctl = cpu_to_le32(ctl);
5780 pkt->value = cpu_to_le32(val);
5781 pkt->addr = cpu_to_le64(reg_base + (i * 4));
5782 }
Oded Gabbayac0ae6a2020-05-11 10:29:27 +03005783
Ofir Bitton423815b2021-01-05 09:04:07 +02005784 hl_cb_destroy(hdev, &hdev->kernel_cb_mgr, cb->id << PAGE_SHIFT);
5785
5786 pending_cb->cb = cb;
5787 pending_cb->cb_size = cb_size;
5788 /* The queue ID MUST be an external queue ID. Otherwise, we will
5789 * have undefined behavior
5790 */
5791 pending_cb->hw_queue_id = hw_queue_id;
5792
5793 spin_lock(&ctx->pending_cb_lock);
5794 list_add_tail(&pending_cb->cb_node, &ctx->pending_cb_list);
5795 spin_unlock(&ctx->pending_cb_lock);
5796
5797 return 0;
5798}
5799
5800static int gaudi_restore_sm_registers(struct hl_device *hdev)
5801{
5802 u64 base_addr;
5803 u32 num_regs;
5804 int rc;
5805
5806 base_addr = CFG_BASE + mmSYNC_MNGR_E_N_SYNC_MNGR_OBJS_SOB_OBJ_0;
5807 num_regs = NUM_OF_SOB_IN_BLOCK;
5808 rc = gaudi_memset_registers(hdev, base_addr, num_regs, 0);
5809 if (rc) {
5810 dev_err(hdev->dev, "failed resetting SM registers");
5811 return -ENOMEM;
5812 }
5813
5814 base_addr = CFG_BASE + mmSYNC_MNGR_E_S_SYNC_MNGR_OBJS_SOB_OBJ_0;
5815 num_regs = NUM_OF_SOB_IN_BLOCK;
5816 rc = gaudi_memset_registers(hdev, base_addr, num_regs, 0);
5817 if (rc) {
5818 dev_err(hdev->dev, "failed resetting SM registers");
5819 return -ENOMEM;
5820 }
5821
5822 base_addr = CFG_BASE + mmSYNC_MNGR_W_N_SYNC_MNGR_OBJS_SOB_OBJ_0;
5823 num_regs = NUM_OF_SOB_IN_BLOCK;
5824 rc = gaudi_memset_registers(hdev, base_addr, num_regs, 0);
5825 if (rc) {
5826 dev_err(hdev->dev, "failed resetting SM registers");
5827 return -ENOMEM;
5828 }
5829
5830 base_addr = CFG_BASE + mmSYNC_MNGR_E_N_SYNC_MNGR_OBJS_MON_STATUS_0;
5831 num_regs = NUM_OF_MONITORS_IN_BLOCK;
5832 rc = gaudi_memset_registers(hdev, base_addr, num_regs, 0);
5833 if (rc) {
5834 dev_err(hdev->dev, "failed resetting SM registers");
5835 return -ENOMEM;
5836 }
5837
5838 base_addr = CFG_BASE + mmSYNC_MNGR_E_S_SYNC_MNGR_OBJS_MON_STATUS_0;
5839 num_regs = NUM_OF_MONITORS_IN_BLOCK;
5840 rc = gaudi_memset_registers(hdev, base_addr, num_regs, 0);
5841 if (rc) {
5842 dev_err(hdev->dev, "failed resetting SM registers");
5843 return -ENOMEM;
5844 }
5845
5846 base_addr = CFG_BASE + mmSYNC_MNGR_W_N_SYNC_MNGR_OBJS_MON_STATUS_0;
5847 num_regs = NUM_OF_MONITORS_IN_BLOCK;
5848 rc = gaudi_memset_registers(hdev, base_addr, num_regs, 0);
5849 if (rc) {
5850 dev_err(hdev->dev, "failed resetting SM registers");
5851 return -ENOMEM;
5852 }
5853
5854 base_addr = CFG_BASE + mmSYNC_MNGR_W_S_SYNC_MNGR_OBJS_SOB_OBJ_0 +
5855 (GAUDI_FIRST_AVAILABLE_W_S_SYNC_OBJECT * 4);
5856 num_regs = NUM_OF_SOB_IN_BLOCK - GAUDI_FIRST_AVAILABLE_W_S_SYNC_OBJECT;
5857 rc = gaudi_memset_registers(hdev, base_addr, num_regs, 0);
5858 if (rc) {
5859 dev_err(hdev->dev, "failed resetting SM registers");
5860 return -ENOMEM;
5861 }
5862
5863 base_addr = CFG_BASE + mmSYNC_MNGR_W_S_SYNC_MNGR_OBJS_MON_STATUS_0 +
5864 (GAUDI_FIRST_AVAILABLE_W_S_MONITOR * 4);
5865 num_regs = NUM_OF_MONITORS_IN_BLOCK - GAUDI_FIRST_AVAILABLE_W_S_MONITOR;
5866 rc = gaudi_memset_registers(hdev, base_addr, num_regs, 0);
5867 if (rc) {
5868 dev_err(hdev->dev, "failed resetting SM registers");
5869 return -ENOMEM;
5870 }
5871
5872 return 0;
Oded Gabbayac0ae6a2020-05-11 10:29:27 +03005873}
5874
5875static void gaudi_restore_dma_registers(struct hl_device *hdev)
5876{
5877 u32 sob_delta = mmSYNC_MNGR_E_N_SYNC_MNGR_OBJS_SOB_OBJ_1 -
5878 mmSYNC_MNGR_E_N_SYNC_MNGR_OBJS_SOB_OBJ_0;
5879 int i;
5880
5881 for (i = 0 ; i < DMA_NUMBER_OF_CHANNELS ; i++) {
5882 u64 sob_addr = CFG_BASE +
5883 mmSYNC_MNGR_E_N_SYNC_MNGR_OBJS_SOB_OBJ_0 +
5884 (i * sob_delta);
5885 u32 dma_offset = i * DMA_CORE_OFFSET;
5886
5887 WREG32(mmDMA0_CORE_WR_COMP_ADDR_LO + dma_offset,
5888 lower_32_bits(sob_addr));
5889 WREG32(mmDMA0_CORE_WR_COMP_ADDR_HI + dma_offset,
5890 upper_32_bits(sob_addr));
5891 WREG32(mmDMA0_CORE_WR_COMP_WDATA + dma_offset, 0x80000001);
5892
5893 /* For DMAs 2-7, need to restore WR_AWUSER_31_11 as it can be
5894 * modified by the user for SRAM reduction
5895 */
5896 if (i > 1)
5897 WREG32(mmDMA0_CORE_WR_AWUSER_31_11 + dma_offset,
5898 0x00000001);
5899 }
5900}
5901
5902static void gaudi_restore_qm_registers(struct hl_device *hdev)
5903{
5904 u32 qman_offset;
5905 int i;
5906
5907 for (i = 0 ; i < DMA_NUMBER_OF_CHANNELS ; i++) {
5908 qman_offset = i * DMA_QMAN_OFFSET;
5909 WREG32(mmDMA0_QM_ARB_CFG_0 + qman_offset, 0);
5910 }
5911
5912 for (i = 0 ; i < MME_NUMBER_OF_MASTER_ENGINES ; i++) {
5913 qman_offset = i * (mmMME2_QM_BASE - mmMME0_QM_BASE);
5914 WREG32(mmMME0_QM_ARB_CFG_0 + qman_offset, 0);
5915 }
5916
5917 for (i = 0 ; i < TPC_NUMBER_OF_ENGINES ; i++) {
5918 qman_offset = i * TPC_QMAN_OFFSET;
5919 WREG32(mmTPC0_QM_ARB_CFG_0 + qman_offset, 0);
5920 }
Oded Gabbay3c681572020-11-02 21:10:39 +02005921
5922 for (i = 0 ; i < NIC_NUMBER_OF_ENGINES ; i++) {
5923 qman_offset = (i >> 1) * NIC_MACRO_QMAN_OFFSET +
5924 (i & 0x1) * NIC_ENGINE_QMAN_OFFSET;
5925 WREG32(mmNIC0_QM0_ARB_CFG_0 + qman_offset, 0);
5926 }
Oded Gabbayac0ae6a2020-05-11 10:29:27 +03005927}
5928
Ofir Bitton423815b2021-01-05 09:04:07 +02005929static int gaudi_restore_user_registers(struct hl_device *hdev)
Oded Gabbayac0ae6a2020-05-11 10:29:27 +03005930{
Ofir Bitton423815b2021-01-05 09:04:07 +02005931 int rc;
5932
5933 rc = gaudi_restore_sm_registers(hdev);
5934 if (rc)
5935 return rc;
5936
Oded Gabbayac0ae6a2020-05-11 10:29:27 +03005937 gaudi_restore_dma_registers(hdev);
5938 gaudi_restore_qm_registers(hdev);
Ofir Bitton423815b2021-01-05 09:04:07 +02005939
5940 return 0;
Oded Gabbayac0ae6a2020-05-11 10:29:27 +03005941}
5942
5943static int gaudi_context_switch(struct hl_device *hdev, u32 asid)
5944{
Ofir Bitton423815b2021-01-05 09:04:07 +02005945 return gaudi_restore_user_registers(hdev);
Oded Gabbayac0ae6a2020-05-11 10:29:27 +03005946}
5947
5948static int gaudi_mmu_clear_pgt_range(struct hl_device *hdev)
5949{
5950 struct asic_fixed_properties *prop = &hdev->asic_prop;
5951 struct gaudi_device *gaudi = hdev->asic_specific;
5952 u64 addr = prop->mmu_pgt_addr;
5953 u32 size = prop->mmu_pgt_size + MMU_CACHE_MNG_SIZE;
5954
5955 if (!(gaudi->hw_cap_initialized & HW_CAP_MMU))
5956 return 0;
5957
5958 return gaudi_memset_device_memory(hdev, addr, size, 0);
5959}
5960
5961static void gaudi_restore_phase_topology(struct hl_device *hdev)
5962{
5963
5964}
5965
Ofir Bittona5778d12021-02-24 11:51:40 +02005966static int gaudi_debugfs_read32(struct hl_device *hdev, u64 addr,
5967 bool user_address, u32 *val)
Oded Gabbayac0ae6a2020-05-11 10:29:27 +03005968{
5969 struct asic_fixed_properties *prop = &hdev->asic_prop;
5970 struct gaudi_device *gaudi = hdev->asic_specific;
Ofir Bittona5778d12021-02-24 11:51:40 +02005971 u64 hbm_bar_addr, host_phys_end;
Oded Gabbayac0ae6a2020-05-11 10:29:27 +03005972 int rc = 0;
5973
Ofir Bittona5778d12021-02-24 11:51:40 +02005974 host_phys_end = HOST_PHYS_BASE + HOST_PHYS_SIZE;
5975
Oded Gabbayac0ae6a2020-05-11 10:29:27 +03005976 if ((addr >= CFG_BASE) && (addr < CFG_BASE + CFG_SIZE)) {
Oded Gabbaye38bfd32020-07-03 20:46:12 +03005977
5978 if ((gaudi->hw_cap_initialized & HW_CAP_CLK_GATE) &&
5979 (hdev->clock_gating_mask &
5980 GAUDI_CLK_GATE_DEBUGFS_MASK)) {
5981
Oded Gabbayac0ae6a2020-05-11 10:29:27 +03005982 dev_err_ratelimited(hdev->dev,
5983 "Can't read register - clock gating is enabled!\n");
5984 rc = -EFAULT;
5985 } else {
5986 *val = RREG32(addr - CFG_BASE);
5987 }
Oded Gabbaye38bfd32020-07-03 20:46:12 +03005988
Oded Gabbayac0ae6a2020-05-11 10:29:27 +03005989 } else if ((addr >= SRAM_BASE_ADDR) &&
5990 (addr < SRAM_BASE_ADDR + SRAM_BAR_SIZE)) {
5991 *val = readl(hdev->pcie_bar[SRAM_BAR_ID] +
5992 (addr - SRAM_BASE_ADDR));
5993 } else if (addr < DRAM_PHYS_BASE + hdev->asic_prop.dram_size) {
5994 u64 bar_base_addr = DRAM_PHYS_BASE +
5995 (addr & ~(prop->dram_pci_bar_size - 0x1ull));
5996
5997 hbm_bar_addr = gaudi_set_hbm_bar_base(hdev, bar_base_addr);
5998 if (hbm_bar_addr != U64_MAX) {
5999 *val = readl(hdev->pcie_bar[HBM_BAR_ID] +
6000 (addr - bar_base_addr));
6001
6002 hbm_bar_addr = gaudi_set_hbm_bar_base(hdev,
6003 hbm_bar_addr);
6004 }
6005 if (hbm_bar_addr == U64_MAX)
6006 rc = -EIO;
Ofir Bittona5778d12021-02-24 11:51:40 +02006007 } else if (addr >= HOST_PHYS_BASE && addr < host_phys_end &&
6008 user_address && !iommu_present(&pci_bus_type)) {
6009 *val = *(u32 *) phys_to_virt(addr - HOST_PHYS_BASE);
Oded Gabbayac0ae6a2020-05-11 10:29:27 +03006010 } else {
6011 rc = -EFAULT;
6012 }
6013
6014 return rc;
6015}
6016
Ofir Bittona5778d12021-02-24 11:51:40 +02006017static int gaudi_debugfs_write32(struct hl_device *hdev, u64 addr,
6018 bool user_address, u32 val)
Oded Gabbayac0ae6a2020-05-11 10:29:27 +03006019{
6020 struct asic_fixed_properties *prop = &hdev->asic_prop;
6021 struct gaudi_device *gaudi = hdev->asic_specific;
Ofir Bittona5778d12021-02-24 11:51:40 +02006022 u64 hbm_bar_addr, host_phys_end;
Oded Gabbayac0ae6a2020-05-11 10:29:27 +03006023 int rc = 0;
6024
Ofir Bittona5778d12021-02-24 11:51:40 +02006025 host_phys_end = HOST_PHYS_BASE + HOST_PHYS_SIZE;
6026
Oded Gabbayac0ae6a2020-05-11 10:29:27 +03006027 if ((addr >= CFG_BASE) && (addr < CFG_BASE + CFG_SIZE)) {
Oded Gabbaye38bfd32020-07-03 20:46:12 +03006028
6029 if ((gaudi->hw_cap_initialized & HW_CAP_CLK_GATE) &&
6030 (hdev->clock_gating_mask &
6031 GAUDI_CLK_GATE_DEBUGFS_MASK)) {
6032
Oded Gabbayac0ae6a2020-05-11 10:29:27 +03006033 dev_err_ratelimited(hdev->dev,
6034 "Can't write register - clock gating is enabled!\n");
6035 rc = -EFAULT;
6036 } else {
6037 WREG32(addr - CFG_BASE, val);
6038 }
Oded Gabbaye38bfd32020-07-03 20:46:12 +03006039
Oded Gabbayac0ae6a2020-05-11 10:29:27 +03006040 } else if ((addr >= SRAM_BASE_ADDR) &&
6041 (addr < SRAM_BASE_ADDR + SRAM_BAR_SIZE)) {
6042 writel(val, hdev->pcie_bar[SRAM_BAR_ID] +
6043 (addr - SRAM_BASE_ADDR));
6044 } else if (addr < DRAM_PHYS_BASE + hdev->asic_prop.dram_size) {
6045 u64 bar_base_addr = DRAM_PHYS_BASE +
6046 (addr & ~(prop->dram_pci_bar_size - 0x1ull));
6047
6048 hbm_bar_addr = gaudi_set_hbm_bar_base(hdev, bar_base_addr);
6049 if (hbm_bar_addr != U64_MAX) {
6050 writel(val, hdev->pcie_bar[HBM_BAR_ID] +
6051 (addr - bar_base_addr));
6052
6053 hbm_bar_addr = gaudi_set_hbm_bar_base(hdev,
6054 hbm_bar_addr);
6055 }
6056 if (hbm_bar_addr == U64_MAX)
6057 rc = -EIO;
Ofir Bittona5778d12021-02-24 11:51:40 +02006058 } else if (addr >= HOST_PHYS_BASE && addr < host_phys_end &&
6059 user_address && !iommu_present(&pci_bus_type)) {
6060 *(u32 *) phys_to_virt(addr - HOST_PHYS_BASE) = val;
Oded Gabbayac0ae6a2020-05-11 10:29:27 +03006061 } else {
6062 rc = -EFAULT;
6063 }
6064
6065 return rc;
6066}
6067
Ofir Bittona5778d12021-02-24 11:51:40 +02006068static int gaudi_debugfs_read64(struct hl_device *hdev, u64 addr,
6069 bool user_address, u64 *val)
Oded Gabbayac0ae6a2020-05-11 10:29:27 +03006070{
6071 struct asic_fixed_properties *prop = &hdev->asic_prop;
6072 struct gaudi_device *gaudi = hdev->asic_specific;
Ofir Bittona5778d12021-02-24 11:51:40 +02006073 u64 hbm_bar_addr, host_phys_end;
Oded Gabbayac0ae6a2020-05-11 10:29:27 +03006074 int rc = 0;
6075
Ofir Bittona5778d12021-02-24 11:51:40 +02006076 host_phys_end = HOST_PHYS_BASE + HOST_PHYS_SIZE;
6077
Oded Gabbayac0ae6a2020-05-11 10:29:27 +03006078 if ((addr >= CFG_BASE) && (addr <= CFG_BASE + CFG_SIZE - sizeof(u64))) {
Oded Gabbaye38bfd32020-07-03 20:46:12 +03006079
6080 if ((gaudi->hw_cap_initialized & HW_CAP_CLK_GATE) &&
6081 (hdev->clock_gating_mask &
6082 GAUDI_CLK_GATE_DEBUGFS_MASK)) {
6083
Oded Gabbayac0ae6a2020-05-11 10:29:27 +03006084 dev_err_ratelimited(hdev->dev,
6085 "Can't read register - clock gating is enabled!\n");
6086 rc = -EFAULT;
6087 } else {
6088 u32 val_l = RREG32(addr - CFG_BASE);
6089 u32 val_h = RREG32(addr + sizeof(u32) - CFG_BASE);
6090
6091 *val = (((u64) val_h) << 32) | val_l;
6092 }
Oded Gabbaye38bfd32020-07-03 20:46:12 +03006093
Oded Gabbayac0ae6a2020-05-11 10:29:27 +03006094 } else if ((addr >= SRAM_BASE_ADDR) &&
6095 (addr <= SRAM_BASE_ADDR + SRAM_BAR_SIZE - sizeof(u64))) {
6096 *val = readq(hdev->pcie_bar[SRAM_BAR_ID] +
6097 (addr - SRAM_BASE_ADDR));
6098 } else if (addr <=
6099 DRAM_PHYS_BASE + hdev->asic_prop.dram_size - sizeof(u64)) {
6100 u64 bar_base_addr = DRAM_PHYS_BASE +
6101 (addr & ~(prop->dram_pci_bar_size - 0x1ull));
6102
6103 hbm_bar_addr = gaudi_set_hbm_bar_base(hdev, bar_base_addr);
6104 if (hbm_bar_addr != U64_MAX) {
6105 *val = readq(hdev->pcie_bar[HBM_BAR_ID] +
6106 (addr - bar_base_addr));
6107
6108 hbm_bar_addr = gaudi_set_hbm_bar_base(hdev,
6109 hbm_bar_addr);
6110 }
6111 if (hbm_bar_addr == U64_MAX)
6112 rc = -EIO;
Ofir Bittona5778d12021-02-24 11:51:40 +02006113 } else if (addr >= HOST_PHYS_BASE && addr < host_phys_end &&
6114 user_address && !iommu_present(&pci_bus_type)) {
6115 *val = *(u64 *) phys_to_virt(addr - HOST_PHYS_BASE);
Oded Gabbayac0ae6a2020-05-11 10:29:27 +03006116 } else {
6117 rc = -EFAULT;
6118 }
6119
6120 return rc;
6121}
6122
Ofir Bittona5778d12021-02-24 11:51:40 +02006123static int gaudi_debugfs_write64(struct hl_device *hdev, u64 addr,
6124 bool user_address, u64 val)
Oded Gabbayac0ae6a2020-05-11 10:29:27 +03006125{
6126 struct asic_fixed_properties *prop = &hdev->asic_prop;
6127 struct gaudi_device *gaudi = hdev->asic_specific;
Ofir Bittona5778d12021-02-24 11:51:40 +02006128 u64 hbm_bar_addr, host_phys_end;
Oded Gabbayac0ae6a2020-05-11 10:29:27 +03006129 int rc = 0;
6130
Ofir Bittona5778d12021-02-24 11:51:40 +02006131 host_phys_end = HOST_PHYS_BASE + HOST_PHYS_SIZE;
6132
Oded Gabbayac0ae6a2020-05-11 10:29:27 +03006133 if ((addr >= CFG_BASE) && (addr <= CFG_BASE + CFG_SIZE - sizeof(u64))) {
Oded Gabbaye38bfd32020-07-03 20:46:12 +03006134
6135 if ((gaudi->hw_cap_initialized & HW_CAP_CLK_GATE) &&
6136 (hdev->clock_gating_mask &
6137 GAUDI_CLK_GATE_DEBUGFS_MASK)) {
6138
Oded Gabbayac0ae6a2020-05-11 10:29:27 +03006139 dev_err_ratelimited(hdev->dev,
6140 "Can't write register - clock gating is enabled!\n");
6141 rc = -EFAULT;
6142 } else {
6143 WREG32(addr - CFG_BASE, lower_32_bits(val));
6144 WREG32(addr + sizeof(u32) - CFG_BASE,
6145 upper_32_bits(val));
6146 }
Oded Gabbaye38bfd32020-07-03 20:46:12 +03006147
Oded Gabbayac0ae6a2020-05-11 10:29:27 +03006148 } else if ((addr >= SRAM_BASE_ADDR) &&
6149 (addr <= SRAM_BASE_ADDR + SRAM_BAR_SIZE - sizeof(u64))) {
6150 writeq(val, hdev->pcie_bar[SRAM_BAR_ID] +
6151 (addr - SRAM_BASE_ADDR));
6152 } else if (addr <=
6153 DRAM_PHYS_BASE + hdev->asic_prop.dram_size - sizeof(u64)) {
6154 u64 bar_base_addr = DRAM_PHYS_BASE +
6155 (addr & ~(prop->dram_pci_bar_size - 0x1ull));
6156
6157 hbm_bar_addr = gaudi_set_hbm_bar_base(hdev, bar_base_addr);
6158 if (hbm_bar_addr != U64_MAX) {
6159 writeq(val, hdev->pcie_bar[HBM_BAR_ID] +
6160 (addr - bar_base_addr));
6161
6162 hbm_bar_addr = gaudi_set_hbm_bar_base(hdev,
6163 hbm_bar_addr);
6164 }
6165 if (hbm_bar_addr == U64_MAX)
6166 rc = -EIO;
Ofir Bittona5778d12021-02-24 11:51:40 +02006167 } else if (addr >= HOST_PHYS_BASE && addr < host_phys_end &&
6168 user_address && !iommu_present(&pci_bus_type)) {
6169 *(u64 *) phys_to_virt(addr - HOST_PHYS_BASE) = val;
Oded Gabbayac0ae6a2020-05-11 10:29:27 +03006170 } else {
6171 rc = -EFAULT;
6172 }
6173
6174 return rc;
6175}
6176
Oded Gabbay639781d2021-04-02 01:43:18 +03006177static int gaudi_dma_core_transfer(struct hl_device *hdev, int dma_id, u64 addr,
6178 u32 size_to_dma, dma_addr_t dma_addr)
6179{
6180 u32 err_cause, val;
6181 u64 dma_offset;
6182 int rc;
6183
6184 dma_offset = dma_id * DMA_CORE_OFFSET;
6185
6186 WREG32(mmDMA0_CORE_SRC_BASE_LO + dma_offset, lower_32_bits(addr));
6187 WREG32(mmDMA0_CORE_SRC_BASE_HI + dma_offset, upper_32_bits(addr));
6188 WREG32(mmDMA0_CORE_DST_BASE_LO + dma_offset, lower_32_bits(dma_addr));
6189 WREG32(mmDMA0_CORE_DST_BASE_HI + dma_offset, upper_32_bits(dma_addr));
6190 WREG32(mmDMA0_CORE_DST_TSIZE_0 + dma_offset, size_to_dma);
6191 WREG32(mmDMA0_CORE_COMMIT + dma_offset,
6192 (1 << DMA0_CORE_COMMIT_LIN_SHIFT));
6193
6194 rc = hl_poll_timeout(
6195 hdev,
6196 mmDMA0_CORE_STS0 + dma_offset,
6197 val,
6198 ((val & DMA0_CORE_STS0_BUSY_MASK) == 0),
6199 0,
6200 1000000);
6201
6202 if (rc) {
6203 dev_err(hdev->dev,
6204 "DMA %d timed-out during reading of 0x%llx\n",
6205 dma_id, addr);
6206 return -EIO;
6207 }
6208
6209 /* Verify DMA is OK */
6210 err_cause = RREG32(mmDMA0_CORE_ERR_CAUSE + dma_offset);
6211 if (err_cause) {
6212 dev_err(hdev->dev, "DMA Failed, cause 0x%x\n", err_cause);
6213 dev_dbg(hdev->dev,
6214 "Clearing DMA0 engine from errors (cause 0x%x)\n",
6215 err_cause);
6216 WREG32(mmDMA0_CORE_ERR_CAUSE + dma_offset, err_cause);
6217
6218 return -EIO;
6219 }
6220
6221 return 0;
6222}
6223
6224static int gaudi_debugfs_read_dma(struct hl_device *hdev, u64 addr, u32 size,
6225 void *blob_addr)
6226{
6227 u32 dma_core_sts0, err_cause, cfg1, size_left, pos, size_to_dma;
6228 struct gaudi_device *gaudi = hdev->asic_specific;
6229 u64 dma_offset, qm_offset;
6230 dma_addr_t dma_addr;
6231 void *kernel_addr;
6232 bool is_eng_idle;
Colin Ian Kingb4e964f2021-04-12 17:10:12 +01006233 int rc = 0, dma_id;
Oded Gabbay639781d2021-04-02 01:43:18 +03006234
6235 kernel_addr = hdev->asic_funcs->asic_dma_alloc_coherent(
6236 hdev, SZ_2M,
6237 &dma_addr,
6238 GFP_KERNEL | __GFP_ZERO);
6239
6240 if (!kernel_addr)
6241 return -ENOMEM;
6242
6243 mutex_lock(&gaudi->clk_gate_mutex);
6244
6245 hdev->asic_funcs->disable_clock_gating(hdev);
6246
6247 hdev->asic_funcs->hw_queues_lock(hdev);
6248
6249 dma_id = gaudi_dma_assignment[GAUDI_PCI_DMA_1];
6250 dma_offset = dma_id * DMA_CORE_OFFSET;
6251 qm_offset = dma_id * DMA_QMAN_OFFSET;
6252 dma_core_sts0 = RREG32(mmDMA0_CORE_STS0 + dma_offset);
6253 is_eng_idle = IS_DMA_IDLE(dma_core_sts0);
6254
6255 if (!is_eng_idle) {
6256 dma_id = gaudi_dma_assignment[GAUDI_PCI_DMA_2];
6257 dma_offset = dma_id * DMA_CORE_OFFSET;
6258 qm_offset = dma_id * DMA_QMAN_OFFSET;
6259 dma_core_sts0 = RREG32(mmDMA0_CORE_STS0 + dma_offset);
6260 is_eng_idle = IS_DMA_IDLE(dma_core_sts0);
6261
6262 if (!is_eng_idle) {
6263 dev_err_ratelimited(hdev->dev,
6264 "Can't read via DMA because it is BUSY\n");
6265 rc = -EAGAIN;
6266 goto out;
6267 }
6268 }
6269
6270 cfg1 = RREG32(mmDMA0_QM_GLBL_CFG1 + qm_offset);
6271 WREG32(mmDMA0_QM_GLBL_CFG1 + qm_offset,
6272 0xF << DMA0_QM_GLBL_CFG1_CP_STOP_SHIFT);
6273
6274 /* TODO: remove this by mapping the DMA temporary buffer to the MMU
6275 * using the compute ctx ASID, if exists. If not, use the kernel ctx
6276 * ASID
6277 */
6278 WREG32_OR(mmDMA0_CORE_PROT + dma_offset, BIT(DMA0_CORE_PROT_VAL_SHIFT));
6279
6280 /* Verify DMA is OK */
6281 err_cause = RREG32(mmDMA0_CORE_ERR_CAUSE + dma_offset);
6282 if (err_cause) {
6283 dev_dbg(hdev->dev,
6284 "Clearing DMA0 engine from errors (cause 0x%x)\n",
6285 err_cause);
6286 WREG32(mmDMA0_CORE_ERR_CAUSE + dma_offset, err_cause);
6287 }
6288
6289 pos = 0;
6290 size_left = size;
6291 size_to_dma = SZ_2M;
6292
6293 while (size_left > 0) {
6294
6295 if (size_left < SZ_2M)
6296 size_to_dma = size_left;
6297
6298 rc = gaudi_dma_core_transfer(hdev, dma_id, addr, size_to_dma,
6299 dma_addr);
6300 if (rc)
6301 break;
6302
6303 memcpy(blob_addr + pos, kernel_addr, size_to_dma);
6304
6305 if (size_left <= SZ_2M)
6306 break;
6307
6308 pos += SZ_2M;
6309 addr += SZ_2M;
6310 size_left -= SZ_2M;
6311 }
6312
6313 /* TODO: remove this by mapping the DMA temporary buffer to the MMU
6314 * using the compute ctx ASID, if exists. If not, use the kernel ctx
6315 * ASID
6316 */
6317 WREG32_AND(mmDMA0_CORE_PROT + dma_offset,
6318 ~BIT(DMA0_CORE_PROT_VAL_SHIFT));
6319
6320 WREG32(mmDMA0_QM_GLBL_CFG1 + qm_offset, cfg1);
6321
6322out:
6323 hdev->asic_funcs->hw_queues_unlock(hdev);
6324
6325 hdev->asic_funcs->set_clock_gating(hdev);
6326
6327 mutex_unlock(&gaudi->clk_gate_mutex);
6328
6329 hdev->asic_funcs->asic_dma_free_coherent(hdev, SZ_2M, kernel_addr,
6330 dma_addr);
6331
6332 return rc;
6333}
6334
Oded Gabbayac0ae6a2020-05-11 10:29:27 +03006335static u64 gaudi_read_pte(struct hl_device *hdev, u64 addr)
6336{
6337 struct gaudi_device *gaudi = hdev->asic_specific;
6338
6339 if (hdev->hard_reset_pending)
6340 return U64_MAX;
6341
6342 return readq(hdev->pcie_bar[HBM_BAR_ID] +
6343 (addr - gaudi->hbm_bar_cur_addr));
6344}
6345
6346static void gaudi_write_pte(struct hl_device *hdev, u64 addr, u64 val)
6347{
6348 struct gaudi_device *gaudi = hdev->asic_specific;
6349
6350 if (hdev->hard_reset_pending)
6351 return;
6352
6353 writeq(val, hdev->pcie_bar[HBM_BAR_ID] +
6354 (addr - gaudi->hbm_bar_cur_addr));
6355}
6356
Ofir Bitton1137e1e2020-09-30 18:43:52 +03006357void gaudi_mmu_prepare_reg(struct hl_device *hdev, u64 reg, u32 asid)
Oded Gabbayac0ae6a2020-05-11 10:29:27 +03006358{
6359 /* mask to zero the MMBP and ASID bits */
6360 WREG32_AND(reg, ~0x7FF);
6361 WREG32_OR(reg, asid);
6362}
6363
6364static void gaudi_mmu_prepare(struct hl_device *hdev, u32 asid)
6365{
6366 struct gaudi_device *gaudi = hdev->asic_specific;
6367
6368 if (!(gaudi->hw_cap_initialized & HW_CAP_MMU))
6369 return;
6370
6371 if (asid & ~DMA0_QM_GLBL_NON_SECURE_PROPS_0_ASID_MASK) {
Alon Mizrahi75d9a2a2020-12-03 17:32:19 +02006372 dev_crit(hdev->dev, "asid %u is too big\n", asid);
Oded Gabbayac0ae6a2020-05-11 10:29:27 +03006373 return;
6374 }
6375
6376 mutex_lock(&gaudi->clk_gate_mutex);
6377
6378 hdev->asic_funcs->disable_clock_gating(hdev);
6379
6380 gaudi_mmu_prepare_reg(hdev, mmDMA0_QM_GLBL_NON_SECURE_PROPS_0, asid);
6381 gaudi_mmu_prepare_reg(hdev, mmDMA0_QM_GLBL_NON_SECURE_PROPS_1, asid);
6382 gaudi_mmu_prepare_reg(hdev, mmDMA0_QM_GLBL_NON_SECURE_PROPS_2, asid);
6383 gaudi_mmu_prepare_reg(hdev, mmDMA0_QM_GLBL_NON_SECURE_PROPS_3, asid);
6384 gaudi_mmu_prepare_reg(hdev, mmDMA0_QM_GLBL_NON_SECURE_PROPS_4, asid);
6385
6386 gaudi_mmu_prepare_reg(hdev, mmDMA1_QM_GLBL_NON_SECURE_PROPS_0, asid);
6387 gaudi_mmu_prepare_reg(hdev, mmDMA1_QM_GLBL_NON_SECURE_PROPS_1, asid);
6388 gaudi_mmu_prepare_reg(hdev, mmDMA1_QM_GLBL_NON_SECURE_PROPS_2, asid);
6389 gaudi_mmu_prepare_reg(hdev, mmDMA1_QM_GLBL_NON_SECURE_PROPS_3, asid);
6390 gaudi_mmu_prepare_reg(hdev, mmDMA1_QM_GLBL_NON_SECURE_PROPS_4, asid);
6391
6392 gaudi_mmu_prepare_reg(hdev, mmDMA2_QM_GLBL_NON_SECURE_PROPS_0, asid);
6393 gaudi_mmu_prepare_reg(hdev, mmDMA2_QM_GLBL_NON_SECURE_PROPS_1, asid);
6394 gaudi_mmu_prepare_reg(hdev, mmDMA2_QM_GLBL_NON_SECURE_PROPS_2, asid);
6395 gaudi_mmu_prepare_reg(hdev, mmDMA2_QM_GLBL_NON_SECURE_PROPS_3, asid);
6396 gaudi_mmu_prepare_reg(hdev, mmDMA2_QM_GLBL_NON_SECURE_PROPS_4, asid);
6397
6398 gaudi_mmu_prepare_reg(hdev, mmDMA3_QM_GLBL_NON_SECURE_PROPS_0, asid);
6399 gaudi_mmu_prepare_reg(hdev, mmDMA3_QM_GLBL_NON_SECURE_PROPS_1, asid);
6400 gaudi_mmu_prepare_reg(hdev, mmDMA3_QM_GLBL_NON_SECURE_PROPS_2, asid);
6401 gaudi_mmu_prepare_reg(hdev, mmDMA3_QM_GLBL_NON_SECURE_PROPS_3, asid);
6402 gaudi_mmu_prepare_reg(hdev, mmDMA3_QM_GLBL_NON_SECURE_PROPS_4, asid);
6403
6404 gaudi_mmu_prepare_reg(hdev, mmDMA4_QM_GLBL_NON_SECURE_PROPS_0, asid);
6405 gaudi_mmu_prepare_reg(hdev, mmDMA4_QM_GLBL_NON_SECURE_PROPS_1, asid);
6406 gaudi_mmu_prepare_reg(hdev, mmDMA4_QM_GLBL_NON_SECURE_PROPS_2, asid);
6407 gaudi_mmu_prepare_reg(hdev, mmDMA4_QM_GLBL_NON_SECURE_PROPS_3, asid);
6408 gaudi_mmu_prepare_reg(hdev, mmDMA4_QM_GLBL_NON_SECURE_PROPS_4, asid);
6409
6410 gaudi_mmu_prepare_reg(hdev, mmDMA5_QM_GLBL_NON_SECURE_PROPS_0, asid);
6411 gaudi_mmu_prepare_reg(hdev, mmDMA5_QM_GLBL_NON_SECURE_PROPS_1, asid);
6412 gaudi_mmu_prepare_reg(hdev, mmDMA5_QM_GLBL_NON_SECURE_PROPS_2, asid);
6413 gaudi_mmu_prepare_reg(hdev, mmDMA5_QM_GLBL_NON_SECURE_PROPS_3, asid);
6414 gaudi_mmu_prepare_reg(hdev, mmDMA5_QM_GLBL_NON_SECURE_PROPS_4, asid);
6415
6416 gaudi_mmu_prepare_reg(hdev, mmDMA6_QM_GLBL_NON_SECURE_PROPS_0, asid);
6417 gaudi_mmu_prepare_reg(hdev, mmDMA6_QM_GLBL_NON_SECURE_PROPS_1, asid);
6418 gaudi_mmu_prepare_reg(hdev, mmDMA6_QM_GLBL_NON_SECURE_PROPS_2, asid);
6419 gaudi_mmu_prepare_reg(hdev, mmDMA6_QM_GLBL_NON_SECURE_PROPS_3, asid);
6420 gaudi_mmu_prepare_reg(hdev, mmDMA6_QM_GLBL_NON_SECURE_PROPS_4, asid);
6421
6422 gaudi_mmu_prepare_reg(hdev, mmDMA7_QM_GLBL_NON_SECURE_PROPS_0, asid);
6423 gaudi_mmu_prepare_reg(hdev, mmDMA7_QM_GLBL_NON_SECURE_PROPS_1, asid);
6424 gaudi_mmu_prepare_reg(hdev, mmDMA7_QM_GLBL_NON_SECURE_PROPS_2, asid);
6425 gaudi_mmu_prepare_reg(hdev, mmDMA7_QM_GLBL_NON_SECURE_PROPS_3, asid);
6426 gaudi_mmu_prepare_reg(hdev, mmDMA7_QM_GLBL_NON_SECURE_PROPS_4, asid);
6427
6428 gaudi_mmu_prepare_reg(hdev, mmDMA0_CORE_NON_SECURE_PROPS, asid);
6429 gaudi_mmu_prepare_reg(hdev, mmDMA1_CORE_NON_SECURE_PROPS, asid);
6430 gaudi_mmu_prepare_reg(hdev, mmDMA2_CORE_NON_SECURE_PROPS, asid);
6431 gaudi_mmu_prepare_reg(hdev, mmDMA3_CORE_NON_SECURE_PROPS, asid);
6432 gaudi_mmu_prepare_reg(hdev, mmDMA4_CORE_NON_SECURE_PROPS, asid);
6433 gaudi_mmu_prepare_reg(hdev, mmDMA5_CORE_NON_SECURE_PROPS, asid);
6434 gaudi_mmu_prepare_reg(hdev, mmDMA6_CORE_NON_SECURE_PROPS, asid);
6435 gaudi_mmu_prepare_reg(hdev, mmDMA7_CORE_NON_SECURE_PROPS, asid);
6436
6437 gaudi_mmu_prepare_reg(hdev, mmTPC0_QM_GLBL_NON_SECURE_PROPS_0, asid);
6438 gaudi_mmu_prepare_reg(hdev, mmTPC0_QM_GLBL_NON_SECURE_PROPS_1, asid);
6439 gaudi_mmu_prepare_reg(hdev, mmTPC0_QM_GLBL_NON_SECURE_PROPS_2, asid);
6440 gaudi_mmu_prepare_reg(hdev, mmTPC0_QM_GLBL_NON_SECURE_PROPS_3, asid);
6441 gaudi_mmu_prepare_reg(hdev, mmTPC0_QM_GLBL_NON_SECURE_PROPS_4, asid);
6442 gaudi_mmu_prepare_reg(hdev, mmTPC0_CFG_ARUSER_LO, asid);
6443 gaudi_mmu_prepare_reg(hdev, mmTPC0_CFG_AWUSER_LO, asid);
6444
6445 gaudi_mmu_prepare_reg(hdev, mmTPC1_QM_GLBL_NON_SECURE_PROPS_0, asid);
6446 gaudi_mmu_prepare_reg(hdev, mmTPC1_QM_GLBL_NON_SECURE_PROPS_1, asid);
6447 gaudi_mmu_prepare_reg(hdev, mmTPC1_QM_GLBL_NON_SECURE_PROPS_2, asid);
6448 gaudi_mmu_prepare_reg(hdev, mmTPC1_QM_GLBL_NON_SECURE_PROPS_3, asid);
6449 gaudi_mmu_prepare_reg(hdev, mmTPC1_QM_GLBL_NON_SECURE_PROPS_4, asid);
6450 gaudi_mmu_prepare_reg(hdev, mmTPC1_CFG_ARUSER_LO, asid);
6451 gaudi_mmu_prepare_reg(hdev, mmTPC1_CFG_AWUSER_LO, asid);
6452
6453 gaudi_mmu_prepare_reg(hdev, mmTPC2_QM_GLBL_NON_SECURE_PROPS_0, asid);
6454 gaudi_mmu_prepare_reg(hdev, mmTPC2_QM_GLBL_NON_SECURE_PROPS_1, asid);
6455 gaudi_mmu_prepare_reg(hdev, mmTPC2_QM_GLBL_NON_SECURE_PROPS_2, asid);
6456 gaudi_mmu_prepare_reg(hdev, mmTPC2_QM_GLBL_NON_SECURE_PROPS_3, asid);
6457 gaudi_mmu_prepare_reg(hdev, mmTPC2_QM_GLBL_NON_SECURE_PROPS_4, asid);
6458 gaudi_mmu_prepare_reg(hdev, mmTPC2_CFG_ARUSER_LO, asid);
6459 gaudi_mmu_prepare_reg(hdev, mmTPC2_CFG_AWUSER_LO, asid);
6460
6461 gaudi_mmu_prepare_reg(hdev, mmTPC3_QM_GLBL_NON_SECURE_PROPS_0, asid);
6462 gaudi_mmu_prepare_reg(hdev, mmTPC3_QM_GLBL_NON_SECURE_PROPS_1, asid);
6463 gaudi_mmu_prepare_reg(hdev, mmTPC3_QM_GLBL_NON_SECURE_PROPS_2, asid);
6464 gaudi_mmu_prepare_reg(hdev, mmTPC3_QM_GLBL_NON_SECURE_PROPS_3, asid);
6465 gaudi_mmu_prepare_reg(hdev, mmTPC3_QM_GLBL_NON_SECURE_PROPS_4, asid);
6466 gaudi_mmu_prepare_reg(hdev, mmTPC3_CFG_ARUSER_LO, asid);
6467 gaudi_mmu_prepare_reg(hdev, mmTPC3_CFG_AWUSER_LO, asid);
6468
6469 gaudi_mmu_prepare_reg(hdev, mmTPC4_QM_GLBL_NON_SECURE_PROPS_0, asid);
6470 gaudi_mmu_prepare_reg(hdev, mmTPC4_QM_GLBL_NON_SECURE_PROPS_1, asid);
6471 gaudi_mmu_prepare_reg(hdev, mmTPC4_QM_GLBL_NON_SECURE_PROPS_2, asid);
6472 gaudi_mmu_prepare_reg(hdev, mmTPC4_QM_GLBL_NON_SECURE_PROPS_3, asid);
6473 gaudi_mmu_prepare_reg(hdev, mmTPC4_QM_GLBL_NON_SECURE_PROPS_4, asid);
6474 gaudi_mmu_prepare_reg(hdev, mmTPC4_CFG_ARUSER_LO, asid);
6475 gaudi_mmu_prepare_reg(hdev, mmTPC4_CFG_AWUSER_LO, asid);
6476
6477 gaudi_mmu_prepare_reg(hdev, mmTPC5_QM_GLBL_NON_SECURE_PROPS_0, asid);
6478 gaudi_mmu_prepare_reg(hdev, mmTPC5_QM_GLBL_NON_SECURE_PROPS_1, asid);
6479 gaudi_mmu_prepare_reg(hdev, mmTPC5_QM_GLBL_NON_SECURE_PROPS_2, asid);
6480 gaudi_mmu_prepare_reg(hdev, mmTPC5_QM_GLBL_NON_SECURE_PROPS_3, asid);
6481 gaudi_mmu_prepare_reg(hdev, mmTPC5_QM_GLBL_NON_SECURE_PROPS_4, asid);
6482 gaudi_mmu_prepare_reg(hdev, mmTPC5_CFG_ARUSER_LO, asid);
6483 gaudi_mmu_prepare_reg(hdev, mmTPC5_CFG_AWUSER_LO, asid);
6484
6485 gaudi_mmu_prepare_reg(hdev, mmTPC6_QM_GLBL_NON_SECURE_PROPS_0, asid);
6486 gaudi_mmu_prepare_reg(hdev, mmTPC6_QM_GLBL_NON_SECURE_PROPS_1, asid);
6487 gaudi_mmu_prepare_reg(hdev, mmTPC6_QM_GLBL_NON_SECURE_PROPS_2, asid);
6488 gaudi_mmu_prepare_reg(hdev, mmTPC6_QM_GLBL_NON_SECURE_PROPS_3, asid);
6489 gaudi_mmu_prepare_reg(hdev, mmTPC6_QM_GLBL_NON_SECURE_PROPS_4, asid);
6490 gaudi_mmu_prepare_reg(hdev, mmTPC6_CFG_ARUSER_LO, asid);
6491 gaudi_mmu_prepare_reg(hdev, mmTPC6_CFG_AWUSER_LO, asid);
6492
6493 gaudi_mmu_prepare_reg(hdev, mmTPC7_QM_GLBL_NON_SECURE_PROPS_0, asid);
6494 gaudi_mmu_prepare_reg(hdev, mmTPC7_QM_GLBL_NON_SECURE_PROPS_1, asid);
6495 gaudi_mmu_prepare_reg(hdev, mmTPC7_QM_GLBL_NON_SECURE_PROPS_2, asid);
6496 gaudi_mmu_prepare_reg(hdev, mmTPC7_QM_GLBL_NON_SECURE_PROPS_3, asid);
6497 gaudi_mmu_prepare_reg(hdev, mmTPC7_QM_GLBL_NON_SECURE_PROPS_4, asid);
6498 gaudi_mmu_prepare_reg(hdev, mmTPC7_CFG_ARUSER_LO, asid);
6499 gaudi_mmu_prepare_reg(hdev, mmTPC7_CFG_AWUSER_LO, asid);
6500
6501 gaudi_mmu_prepare_reg(hdev, mmMME0_QM_GLBL_NON_SECURE_PROPS_0, asid);
6502 gaudi_mmu_prepare_reg(hdev, mmMME0_QM_GLBL_NON_SECURE_PROPS_1, asid);
6503 gaudi_mmu_prepare_reg(hdev, mmMME0_QM_GLBL_NON_SECURE_PROPS_2, asid);
6504 gaudi_mmu_prepare_reg(hdev, mmMME0_QM_GLBL_NON_SECURE_PROPS_3, asid);
6505 gaudi_mmu_prepare_reg(hdev, mmMME0_QM_GLBL_NON_SECURE_PROPS_4, asid);
6506 gaudi_mmu_prepare_reg(hdev, mmMME2_QM_GLBL_NON_SECURE_PROPS_0, asid);
6507 gaudi_mmu_prepare_reg(hdev, mmMME2_QM_GLBL_NON_SECURE_PROPS_1, asid);
6508 gaudi_mmu_prepare_reg(hdev, mmMME2_QM_GLBL_NON_SECURE_PROPS_2, asid);
6509 gaudi_mmu_prepare_reg(hdev, mmMME2_QM_GLBL_NON_SECURE_PROPS_3, asid);
6510 gaudi_mmu_prepare_reg(hdev, mmMME2_QM_GLBL_NON_SECURE_PROPS_4, asid);
6511
6512 gaudi_mmu_prepare_reg(hdev, mmMME0_SBAB_ARUSER0, asid);
6513 gaudi_mmu_prepare_reg(hdev, mmMME0_SBAB_ARUSER1, asid);
6514 gaudi_mmu_prepare_reg(hdev, mmMME1_SBAB_ARUSER0, asid);
6515 gaudi_mmu_prepare_reg(hdev, mmMME1_SBAB_ARUSER1, asid);
6516 gaudi_mmu_prepare_reg(hdev, mmMME2_SBAB_ARUSER0, asid);
6517 gaudi_mmu_prepare_reg(hdev, mmMME2_SBAB_ARUSER1, asid);
6518 gaudi_mmu_prepare_reg(hdev, mmMME3_SBAB_ARUSER0, asid);
6519 gaudi_mmu_prepare_reg(hdev, mmMME3_SBAB_ARUSER1, asid);
6520 gaudi_mmu_prepare_reg(hdev, mmMME0_ACC_WBC, asid);
6521 gaudi_mmu_prepare_reg(hdev, mmMME1_ACC_WBC, asid);
6522 gaudi_mmu_prepare_reg(hdev, mmMME2_ACC_WBC, asid);
6523 gaudi_mmu_prepare_reg(hdev, mmMME3_ACC_WBC, asid);
6524
Oded Gabbay3c681572020-11-02 21:10:39 +02006525 if (hdev->nic_ports_mask & GAUDI_NIC_MASK_NIC0) {
6526 gaudi_mmu_prepare_reg(hdev, mmNIC0_QM0_GLBL_NON_SECURE_PROPS_0,
6527 asid);
6528 gaudi_mmu_prepare_reg(hdev, mmNIC0_QM0_GLBL_NON_SECURE_PROPS_1,
6529 asid);
6530 gaudi_mmu_prepare_reg(hdev, mmNIC0_QM0_GLBL_NON_SECURE_PROPS_2,
6531 asid);
6532 gaudi_mmu_prepare_reg(hdev, mmNIC0_QM0_GLBL_NON_SECURE_PROPS_3,
6533 asid);
6534 gaudi_mmu_prepare_reg(hdev, mmNIC0_QM0_GLBL_NON_SECURE_PROPS_4,
6535 asid);
6536 }
6537
6538 if (hdev->nic_ports_mask & GAUDI_NIC_MASK_NIC1) {
6539 gaudi_mmu_prepare_reg(hdev, mmNIC0_QM1_GLBL_NON_SECURE_PROPS_0,
6540 asid);
6541 gaudi_mmu_prepare_reg(hdev, mmNIC0_QM1_GLBL_NON_SECURE_PROPS_1,
6542 asid);
6543 gaudi_mmu_prepare_reg(hdev, mmNIC0_QM1_GLBL_NON_SECURE_PROPS_2,
6544 asid);
6545 gaudi_mmu_prepare_reg(hdev, mmNIC0_QM1_GLBL_NON_SECURE_PROPS_3,
6546 asid);
6547 gaudi_mmu_prepare_reg(hdev, mmNIC0_QM1_GLBL_NON_SECURE_PROPS_4,
6548 asid);
6549 }
6550
6551 if (hdev->nic_ports_mask & GAUDI_NIC_MASK_NIC2) {
6552 gaudi_mmu_prepare_reg(hdev, mmNIC1_QM0_GLBL_NON_SECURE_PROPS_0,
6553 asid);
6554 gaudi_mmu_prepare_reg(hdev, mmNIC1_QM0_GLBL_NON_SECURE_PROPS_1,
6555 asid);
6556 gaudi_mmu_prepare_reg(hdev, mmNIC1_QM0_GLBL_NON_SECURE_PROPS_2,
6557 asid);
6558 gaudi_mmu_prepare_reg(hdev, mmNIC1_QM0_GLBL_NON_SECURE_PROPS_3,
6559 asid);
6560 gaudi_mmu_prepare_reg(hdev, mmNIC1_QM0_GLBL_NON_SECURE_PROPS_4,
6561 asid);
6562 }
6563
6564 if (hdev->nic_ports_mask & GAUDI_NIC_MASK_NIC3) {
6565 gaudi_mmu_prepare_reg(hdev, mmNIC1_QM1_GLBL_NON_SECURE_PROPS_0,
6566 asid);
6567 gaudi_mmu_prepare_reg(hdev, mmNIC1_QM1_GLBL_NON_SECURE_PROPS_1,
6568 asid);
6569 gaudi_mmu_prepare_reg(hdev, mmNIC1_QM1_GLBL_NON_SECURE_PROPS_2,
6570 asid);
6571 gaudi_mmu_prepare_reg(hdev, mmNIC1_QM1_GLBL_NON_SECURE_PROPS_3,
6572 asid);
6573 gaudi_mmu_prepare_reg(hdev, mmNIC1_QM1_GLBL_NON_SECURE_PROPS_4,
6574 asid);
6575 }
6576
6577 if (hdev->nic_ports_mask & GAUDI_NIC_MASK_NIC4) {
6578 gaudi_mmu_prepare_reg(hdev, mmNIC2_QM0_GLBL_NON_SECURE_PROPS_0,
6579 asid);
6580 gaudi_mmu_prepare_reg(hdev, mmNIC2_QM0_GLBL_NON_SECURE_PROPS_1,
6581 asid);
6582 gaudi_mmu_prepare_reg(hdev, mmNIC2_QM0_GLBL_NON_SECURE_PROPS_2,
6583 asid);
6584 gaudi_mmu_prepare_reg(hdev, mmNIC2_QM0_GLBL_NON_SECURE_PROPS_3,
6585 asid);
6586 gaudi_mmu_prepare_reg(hdev, mmNIC2_QM0_GLBL_NON_SECURE_PROPS_4,
6587 asid);
6588 }
6589
6590 if (hdev->nic_ports_mask & GAUDI_NIC_MASK_NIC5) {
6591 gaudi_mmu_prepare_reg(hdev, mmNIC2_QM1_GLBL_NON_SECURE_PROPS_0,
6592 asid);
6593 gaudi_mmu_prepare_reg(hdev, mmNIC2_QM1_GLBL_NON_SECURE_PROPS_1,
6594 asid);
6595 gaudi_mmu_prepare_reg(hdev, mmNIC2_QM1_GLBL_NON_SECURE_PROPS_2,
6596 asid);
6597 gaudi_mmu_prepare_reg(hdev, mmNIC2_QM1_GLBL_NON_SECURE_PROPS_3,
6598 asid);
6599 gaudi_mmu_prepare_reg(hdev, mmNIC2_QM1_GLBL_NON_SECURE_PROPS_4,
6600 asid);
6601 }
6602
6603 if (hdev->nic_ports_mask & GAUDI_NIC_MASK_NIC6) {
6604 gaudi_mmu_prepare_reg(hdev, mmNIC3_QM0_GLBL_NON_SECURE_PROPS_0,
6605 asid);
6606 gaudi_mmu_prepare_reg(hdev, mmNIC3_QM0_GLBL_NON_SECURE_PROPS_1,
6607 asid);
6608 gaudi_mmu_prepare_reg(hdev, mmNIC3_QM0_GLBL_NON_SECURE_PROPS_2,
6609 asid);
6610 gaudi_mmu_prepare_reg(hdev, mmNIC3_QM0_GLBL_NON_SECURE_PROPS_3,
6611 asid);
6612 gaudi_mmu_prepare_reg(hdev, mmNIC3_QM0_GLBL_NON_SECURE_PROPS_4,
6613 asid);
6614 }
6615
6616 if (hdev->nic_ports_mask & GAUDI_NIC_MASK_NIC7) {
6617 gaudi_mmu_prepare_reg(hdev, mmNIC3_QM1_GLBL_NON_SECURE_PROPS_0,
6618 asid);
6619 gaudi_mmu_prepare_reg(hdev, mmNIC3_QM1_GLBL_NON_SECURE_PROPS_1,
6620 asid);
6621 gaudi_mmu_prepare_reg(hdev, mmNIC3_QM1_GLBL_NON_SECURE_PROPS_2,
6622 asid);
6623 gaudi_mmu_prepare_reg(hdev, mmNIC3_QM1_GLBL_NON_SECURE_PROPS_3,
6624 asid);
6625 gaudi_mmu_prepare_reg(hdev, mmNIC3_QM1_GLBL_NON_SECURE_PROPS_4,
6626 asid);
6627 }
6628
6629 if (hdev->nic_ports_mask & GAUDI_NIC_MASK_NIC8) {
6630 gaudi_mmu_prepare_reg(hdev, mmNIC4_QM0_GLBL_NON_SECURE_PROPS_0,
6631 asid);
6632 gaudi_mmu_prepare_reg(hdev, mmNIC4_QM0_GLBL_NON_SECURE_PROPS_1,
6633 asid);
6634 gaudi_mmu_prepare_reg(hdev, mmNIC4_QM0_GLBL_NON_SECURE_PROPS_2,
6635 asid);
6636 gaudi_mmu_prepare_reg(hdev, mmNIC4_QM0_GLBL_NON_SECURE_PROPS_3,
6637 asid);
6638 gaudi_mmu_prepare_reg(hdev, mmNIC4_QM0_GLBL_NON_SECURE_PROPS_4,
6639 asid);
6640 }
6641
6642 if (hdev->nic_ports_mask & GAUDI_NIC_MASK_NIC9) {
6643 gaudi_mmu_prepare_reg(hdev, mmNIC4_QM1_GLBL_NON_SECURE_PROPS_0,
6644 asid);
6645 gaudi_mmu_prepare_reg(hdev, mmNIC4_QM1_GLBL_NON_SECURE_PROPS_1,
6646 asid);
6647 gaudi_mmu_prepare_reg(hdev, mmNIC4_QM1_GLBL_NON_SECURE_PROPS_2,
6648 asid);
6649 gaudi_mmu_prepare_reg(hdev, mmNIC4_QM1_GLBL_NON_SECURE_PROPS_3,
6650 asid);
6651 gaudi_mmu_prepare_reg(hdev, mmNIC4_QM1_GLBL_NON_SECURE_PROPS_4,
6652 asid);
6653 }
6654
Oded Gabbaye38bfd32020-07-03 20:46:12 +03006655 hdev->asic_funcs->set_clock_gating(hdev);
Oded Gabbayac0ae6a2020-05-11 10:29:27 +03006656
6657 mutex_unlock(&gaudi->clk_gate_mutex);
6658}
6659
6660static int gaudi_send_job_on_qman0(struct hl_device *hdev,
6661 struct hl_cs_job *job)
6662{
6663 struct packet_msg_prot *fence_pkt;
6664 u32 *fence_ptr;
6665 dma_addr_t fence_dma_addr;
6666 struct hl_cb *cb;
6667 u32 tmp, timeout, dma_offset;
6668 int rc;
6669
6670 if (hdev->pldm)
6671 timeout = GAUDI_PLDM_QMAN0_TIMEOUT_USEC;
6672 else
6673 timeout = HL_DEVICE_TIMEOUT_USEC;
6674
Ohad Sharabicf303392021-01-17 16:01:56 +02006675 if (!hdev->asic_funcs->is_device_idle(hdev, NULL, 0, NULL)) {
Oded Gabbayac0ae6a2020-05-11 10:29:27 +03006676 dev_err_ratelimited(hdev->dev,
6677 "Can't send driver job on QMAN0 because the device is not idle\n");
6678 return -EBUSY;
6679 }
6680
6681 fence_ptr = hdev->asic_funcs->asic_dma_pool_zalloc(hdev, 4, GFP_KERNEL,
6682 &fence_dma_addr);
6683 if (!fence_ptr) {
6684 dev_err(hdev->dev,
6685 "Failed to allocate fence memory for QMAN0\n");
6686 return -ENOMEM;
6687 }
6688
6689 cb = job->patched_cb;
6690
Arnd Bergmann82948e62020-10-26 17:08:06 +01006691 fence_pkt = cb->kernel_address +
6692 job->job_cb_size - sizeof(struct packet_msg_prot);
Oded Gabbayac0ae6a2020-05-11 10:29:27 +03006693
Oded Gabbay65887292020-08-12 11:21:01 +03006694 tmp = FIELD_PREP(GAUDI_PKT_CTL_OPCODE_MASK, PACKET_MSG_PROT);
6695 tmp |= FIELD_PREP(GAUDI_PKT_CTL_EB_MASK, 1);
6696 tmp |= FIELD_PREP(GAUDI_PKT_CTL_MB_MASK, 1);
6697
Oded Gabbayac0ae6a2020-05-11 10:29:27 +03006698 fence_pkt->ctl = cpu_to_le32(tmp);
6699 fence_pkt->value = cpu_to_le32(GAUDI_QMAN0_FENCE_VAL);
6700 fence_pkt->addr = cpu_to_le64(fence_dma_addr);
6701
6702 dma_offset = gaudi_dma_assignment[GAUDI_PCI_DMA_1] * DMA_CORE_OFFSET;
6703
6704 WREG32_OR(mmDMA0_CORE_PROT + dma_offset, BIT(DMA0_CORE_PROT_VAL_SHIFT));
6705
6706 rc = hl_hw_queue_send_cb_no_cmpl(hdev, GAUDI_QUEUE_ID_DMA_0_0,
6707 job->job_cb_size, cb->bus_address);
6708 if (rc) {
6709 dev_err(hdev->dev, "Failed to send CB on QMAN0, %d\n", rc);
6710 goto free_fence_ptr;
6711 }
6712
6713 rc = hl_poll_timeout_memory(hdev, fence_ptr, tmp,
6714 (tmp == GAUDI_QMAN0_FENCE_VAL), 1000,
6715 timeout, true);
6716
6717 hl_hw_queue_inc_ci_kernel(hdev, GAUDI_QUEUE_ID_DMA_0_0);
6718
6719 if (rc == -ETIMEDOUT) {
6720 dev_err(hdev->dev, "QMAN0 Job timeout (0x%x)\n", tmp);
6721 goto free_fence_ptr;
6722 }
6723
6724free_fence_ptr:
6725 WREG32_AND(mmDMA0_CORE_PROT + dma_offset,
6726 ~BIT(DMA0_CORE_PROT_VAL_SHIFT));
6727
6728 hdev->asic_funcs->asic_dma_pool_free(hdev, (void *) fence_ptr,
6729 fence_dma_addr);
6730 return rc;
6731}
6732
Oded Gabbayac0ae6a2020-05-11 10:29:27 +03006733static void gaudi_get_event_desc(u16 event_type, char *desc, size_t size)
6734{
Ofir Bittonebd8d122020-05-10 13:41:28 +03006735 if (event_type >= GAUDI_EVENT_SIZE)
6736 goto event_not_supported;
Oded Gabbayac0ae6a2020-05-11 10:29:27 +03006737
Ofir Bittonebd8d122020-05-10 13:41:28 +03006738 if (!gaudi_irq_map_table[event_type].valid)
6739 goto event_not_supported;
6740
6741 snprintf(desc, size, gaudi_irq_map_table[event_type].name);
6742
6743 return;
6744
6745event_not_supported:
6746 snprintf(desc, size, "N/A");
Oded Gabbayac0ae6a2020-05-11 10:29:27 +03006747}
6748
6749static const char *gaudi_get_razwi_initiator_dma_name(struct hl_device *hdev,
6750 u32 x_y, bool is_write)
6751{
6752 u32 dma_id[2], dma_offset, err_cause[2], mask, i;
6753
6754 mask = is_write ? DMA0_CORE_ERR_CAUSE_HBW_WR_ERR_MASK :
6755 DMA0_CORE_ERR_CAUSE_HBW_RD_ERR_MASK;
6756
6757 switch (x_y) {
6758 case RAZWI_INITIATOR_ID_X_Y_DMA_IF_W_S_0:
6759 case RAZWI_INITIATOR_ID_X_Y_DMA_IF_W_S_1:
6760 dma_id[0] = 0;
6761 dma_id[1] = 2;
6762 break;
6763 case RAZWI_INITIATOR_ID_X_Y_DMA_IF_E_S_0:
6764 case RAZWI_INITIATOR_ID_X_Y_DMA_IF_E_S_1:
6765 dma_id[0] = 1;
6766 dma_id[1] = 3;
6767 break;
6768 case RAZWI_INITIATOR_ID_X_Y_DMA_IF_W_N_0:
6769 case RAZWI_INITIATOR_ID_X_Y_DMA_IF_W_N_1:
6770 dma_id[0] = 4;
6771 dma_id[1] = 6;
6772 break;
6773 case RAZWI_INITIATOR_ID_X_Y_DMA_IF_E_N_0:
6774 case RAZWI_INITIATOR_ID_X_Y_DMA_IF_E_N_1:
6775 dma_id[0] = 5;
6776 dma_id[1] = 7;
6777 break;
6778 default:
6779 goto unknown_initiator;
6780 }
6781
6782 for (i = 0 ; i < 2 ; i++) {
6783 dma_offset = dma_id[i] * DMA_CORE_OFFSET;
6784 err_cause[i] = RREG32(mmDMA0_CORE_ERR_CAUSE + dma_offset);
6785 }
6786
6787 switch (x_y) {
6788 case RAZWI_INITIATOR_ID_X_Y_DMA_IF_W_S_0:
6789 case RAZWI_INITIATOR_ID_X_Y_DMA_IF_W_S_1:
6790 if ((err_cause[0] & mask) && !(err_cause[1] & mask))
6791 return "DMA0";
6792 else if (!(err_cause[0] & mask) && (err_cause[1] & mask))
6793 return "DMA2";
6794 else
6795 return "DMA0 or DMA2";
6796 case RAZWI_INITIATOR_ID_X_Y_DMA_IF_E_S_0:
6797 case RAZWI_INITIATOR_ID_X_Y_DMA_IF_E_S_1:
6798 if ((err_cause[0] & mask) && !(err_cause[1] & mask))
6799 return "DMA1";
6800 else if (!(err_cause[0] & mask) && (err_cause[1] & mask))
6801 return "DMA3";
6802 else
6803 return "DMA1 or DMA3";
6804 case RAZWI_INITIATOR_ID_X_Y_DMA_IF_W_N_0:
6805 case RAZWI_INITIATOR_ID_X_Y_DMA_IF_W_N_1:
6806 if ((err_cause[0] & mask) && !(err_cause[1] & mask))
6807 return "DMA4";
6808 else if (!(err_cause[0] & mask) && (err_cause[1] & mask))
6809 return "DMA6";
6810 else
6811 return "DMA4 or DMA6";
6812 case RAZWI_INITIATOR_ID_X_Y_DMA_IF_E_N_0:
6813 case RAZWI_INITIATOR_ID_X_Y_DMA_IF_E_N_1:
6814 if ((err_cause[0] & mask) && !(err_cause[1] & mask))
6815 return "DMA5";
6816 else if (!(err_cause[0] & mask) && (err_cause[1] & mask))
6817 return "DMA7";
6818 else
6819 return "DMA5 or DMA7";
6820 }
6821
6822unknown_initiator:
6823 return "unknown initiator";
6824}
6825
6826static const char *gaudi_get_razwi_initiator_name(struct hl_device *hdev,
6827 bool is_write)
6828{
6829 u32 val, x_y, axi_id;
6830
6831 val = is_write ? RREG32(mmMMU_UP_RAZWI_WRITE_ID) :
6832 RREG32(mmMMU_UP_RAZWI_READ_ID);
6833 x_y = val & ((RAZWI_INITIATOR_Y_MASK << RAZWI_INITIATOR_Y_SHIFT) |
6834 (RAZWI_INITIATOR_X_MASK << RAZWI_INITIATOR_X_SHIFT));
6835 axi_id = val & (RAZWI_INITIATOR_AXI_ID_MASK <<
6836 RAZWI_INITIATOR_AXI_ID_SHIFT);
6837
6838 switch (x_y) {
6839 case RAZWI_INITIATOR_ID_X_Y_TPC0_NIC0:
6840 if (axi_id == RAZWI_INITIATOR_ID_AXI_ID(AXI_ID_TPC))
6841 return "TPC0";
6842 if (axi_id == RAZWI_INITIATOR_ID_AXI_ID(AXI_ID_NIC))
6843 return "NIC0";
6844 break;
6845 case RAZWI_INITIATOR_ID_X_Y_TPC1:
6846 return "TPC1";
6847 case RAZWI_INITIATOR_ID_X_Y_MME0_0:
6848 case RAZWI_INITIATOR_ID_X_Y_MME0_1:
6849 return "MME0";
6850 case RAZWI_INITIATOR_ID_X_Y_MME1_0:
6851 case RAZWI_INITIATOR_ID_X_Y_MME1_1:
6852 return "MME1";
6853 case RAZWI_INITIATOR_ID_X_Y_TPC2:
6854 return "TPC2";
6855 case RAZWI_INITIATOR_ID_X_Y_TPC3_PCI_CPU_PSOC:
6856 if (axi_id == RAZWI_INITIATOR_ID_AXI_ID(AXI_ID_TPC))
6857 return "TPC3";
6858 if (axi_id == RAZWI_INITIATOR_ID_AXI_ID(AXI_ID_PCI))
6859 return "PCI";
6860 if (axi_id == RAZWI_INITIATOR_ID_AXI_ID(AXI_ID_CPU))
6861 return "CPU";
6862 if (axi_id == RAZWI_INITIATOR_ID_AXI_ID(AXI_ID_PSOC))
6863 return "PSOC";
6864 break;
6865 case RAZWI_INITIATOR_ID_X_Y_DMA_IF_W_S_0:
6866 case RAZWI_INITIATOR_ID_X_Y_DMA_IF_W_S_1:
6867 case RAZWI_INITIATOR_ID_X_Y_DMA_IF_E_S_0:
6868 case RAZWI_INITIATOR_ID_X_Y_DMA_IF_E_S_1:
6869 case RAZWI_INITIATOR_ID_X_Y_DMA_IF_W_N_0:
6870 case RAZWI_INITIATOR_ID_X_Y_DMA_IF_W_N_1:
6871 case RAZWI_INITIATOR_ID_X_Y_DMA_IF_E_N_0:
6872 case RAZWI_INITIATOR_ID_X_Y_DMA_IF_E_N_1:
6873 return gaudi_get_razwi_initiator_dma_name(hdev, x_y, is_write);
6874 case RAZWI_INITIATOR_ID_X_Y_TPC4_NIC1_NIC2:
6875 if (axi_id == RAZWI_INITIATOR_ID_AXI_ID(AXI_ID_TPC))
6876 return "TPC4";
6877 if (axi_id == RAZWI_INITIATOR_ID_AXI_ID(AXI_ID_NIC))
6878 return "NIC1";
6879 if (axi_id == RAZWI_INITIATOR_ID_AXI_ID(AXI_ID_NIC_FT))
6880 return "NIC2";
6881 break;
6882 case RAZWI_INITIATOR_ID_X_Y_TPC5:
6883 return "TPC5";
6884 case RAZWI_INITIATOR_ID_X_Y_MME2_0:
6885 case RAZWI_INITIATOR_ID_X_Y_MME2_1:
6886 return "MME2";
6887 case RAZWI_INITIATOR_ID_X_Y_MME3_0:
6888 case RAZWI_INITIATOR_ID_X_Y_MME3_1:
6889 return "MME3";
6890 case RAZWI_INITIATOR_ID_X_Y_TPC6:
6891 return "TPC6";
6892 case RAZWI_INITIATOR_ID_X_Y_TPC7_NIC4_NIC5:
6893 if (axi_id == RAZWI_INITIATOR_ID_AXI_ID(AXI_ID_TPC))
6894 return "TPC7";
6895 if (axi_id == RAZWI_INITIATOR_ID_AXI_ID(AXI_ID_NIC))
6896 return "NIC4";
6897 if (axi_id == RAZWI_INITIATOR_ID_AXI_ID(AXI_ID_NIC_FT))
6898 return "NIC5";
6899 break;
6900 default:
6901 break;
6902 }
6903
6904 dev_err(hdev->dev,
6905 "Unknown RAZWI initiator ID 0x%x [Y=%d, X=%d, AXI_ID=%d]\n",
6906 val,
6907 (val >> RAZWI_INITIATOR_Y_SHIFT) & RAZWI_INITIATOR_Y_MASK,
6908 (val >> RAZWI_INITIATOR_X_SHIFT) & RAZWI_INITIATOR_X_MASK,
6909 (val >> RAZWI_INITIATOR_AXI_ID_SHIFT) &
6910 RAZWI_INITIATOR_AXI_ID_MASK);
6911
6912 return "unknown initiator";
6913}
6914
6915static void gaudi_print_razwi_info(struct hl_device *hdev)
6916{
6917 if (RREG32(mmMMU_UP_RAZWI_WRITE_VLD)) {
6918 dev_err_ratelimited(hdev->dev,
6919 "RAZWI event caused by illegal write of %s\n",
6920 gaudi_get_razwi_initiator_name(hdev, true));
6921 WREG32(mmMMU_UP_RAZWI_WRITE_VLD, 0);
6922 }
6923
6924 if (RREG32(mmMMU_UP_RAZWI_READ_VLD)) {
6925 dev_err_ratelimited(hdev->dev,
6926 "RAZWI event caused by illegal read of %s\n",
6927 gaudi_get_razwi_initiator_name(hdev, false));
6928 WREG32(mmMMU_UP_RAZWI_READ_VLD, 0);
6929 }
6930}
6931
6932static void gaudi_print_mmu_error_info(struct hl_device *hdev)
6933{
6934 struct gaudi_device *gaudi = hdev->asic_specific;
6935 u64 addr;
6936 u32 val;
6937
6938 if (!(gaudi->hw_cap_initialized & HW_CAP_MMU))
6939 return;
6940
6941 val = RREG32(mmMMU_UP_PAGE_ERROR_CAPTURE);
6942 if (val & MMU_UP_PAGE_ERROR_CAPTURE_ENTRY_VALID_MASK) {
6943 addr = val & MMU_UP_PAGE_ERROR_CAPTURE_VA_49_32_MASK;
6944 addr <<= 32;
6945 addr |= RREG32(mmMMU_UP_PAGE_ERROR_CAPTURE_VA);
6946
6947 dev_err_ratelimited(hdev->dev, "MMU page fault on va 0x%llx\n",
6948 addr);
6949
6950 WREG32(mmMMU_UP_PAGE_ERROR_CAPTURE, 0);
6951 }
6952
6953 val = RREG32(mmMMU_UP_ACCESS_ERROR_CAPTURE);
6954 if (val & MMU_UP_ACCESS_ERROR_CAPTURE_ENTRY_VALID_MASK) {
6955 addr = val & MMU_UP_ACCESS_ERROR_CAPTURE_VA_49_32_MASK;
6956 addr <<= 32;
6957 addr |= RREG32(mmMMU_UP_ACCESS_ERROR_CAPTURE_VA);
6958
6959 dev_err_ratelimited(hdev->dev,
6960 "MMU access error on va 0x%llx\n", addr);
6961
6962 WREG32(mmMMU_UP_ACCESS_ERROR_CAPTURE, 0);
6963 }
6964}
6965
6966/*
6967 * +-------------------+------------------------------------------------------+
6968 * | Configuration Reg | Description |
6969 * | Address | |
6970 * +-------------------+------------------------------------------------------+
6971 * | 0xF30 - 0xF3F |ECC single error indication (1 bit per memory wrapper)|
6972 * | |0xF30 memory wrappers 31:0 (MSB to LSB) |
6973 * | |0xF34 memory wrappers 63:32 |
6974 * | |0xF38 memory wrappers 95:64 |
6975 * | |0xF3C memory wrappers 127:96 |
6976 * +-------------------+------------------------------------------------------+
6977 * | 0xF40 - 0xF4F |ECC double error indication (1 bit per memory wrapper)|
6978 * | |0xF40 memory wrappers 31:0 (MSB to LSB) |
6979 * | |0xF44 memory wrappers 63:32 |
6980 * | |0xF48 memory wrappers 95:64 |
6981 * | |0xF4C memory wrappers 127:96 |
6982 * +-------------------+------------------------------------------------------+
6983 */
Oded Gabbayfcc6a4e2020-05-17 08:20:35 +03006984static int gaudi_extract_ecc_info(struct hl_device *hdev,
6985 struct ecc_info_extract_params *params, u64 *ecc_address,
6986 u64 *ecc_syndrom, u8 *memory_wrapper_idx)
Oded Gabbayac0ae6a2020-05-11 10:29:27 +03006987{
6988 struct gaudi_device *gaudi = hdev->asic_specific;
Oded Gabbayfcc6a4e2020-05-17 08:20:35 +03006989 u32 i, num_mem_regs, reg, err_bit;
6990 u64 err_addr, err_word = 0;
6991 int rc = 0;
Oded Gabbayac0ae6a2020-05-11 10:29:27 +03006992
Oded Gabbayfcc6a4e2020-05-17 08:20:35 +03006993 num_mem_regs = params->num_memories / 32 +
6994 ((params->num_memories % 32) ? 1 : 0);
Oded Gabbayac0ae6a2020-05-11 10:29:27 +03006995
Oded Gabbayfcc6a4e2020-05-17 08:20:35 +03006996 if (params->block_address >= CFG_BASE)
6997 params->block_address -= CFG_BASE;
6998
6999 if (params->derr)
7000 err_addr = params->block_address + GAUDI_ECC_DERR0_OFFSET;
Oded Gabbayac0ae6a2020-05-11 10:29:27 +03007001 else
Oded Gabbayfcc6a4e2020-05-17 08:20:35 +03007002 err_addr = params->block_address + GAUDI_ECC_SERR0_OFFSET;
Oded Gabbayac0ae6a2020-05-11 10:29:27 +03007003
Oded Gabbayfcc6a4e2020-05-17 08:20:35 +03007004 if (params->disable_clock_gating) {
Oded Gabbayac0ae6a2020-05-11 10:29:27 +03007005 mutex_lock(&gaudi->clk_gate_mutex);
7006 hdev->asic_funcs->disable_clock_gating(hdev);
7007 }
7008
Oded Gabbayfcc6a4e2020-05-17 08:20:35 +03007009 /* Set invalid wrapper index */
7010 *memory_wrapper_idx = 0xFF;
Oded Gabbayac0ae6a2020-05-11 10:29:27 +03007011
Oded Gabbayfcc6a4e2020-05-17 08:20:35 +03007012 /* Iterate through memory wrappers, a single bit must be set */
Dan Carpenterb0353542020-08-05 12:51:05 +03007013 for (i = 0 ; i < num_mem_regs ; i++) {
Oded Gabbayfcc6a4e2020-05-17 08:20:35 +03007014 err_addr += i * 4;
7015 err_word = RREG32(err_addr);
7016 if (err_word) {
7017 err_bit = __ffs(err_word);
7018 *memory_wrapper_idx = err_bit + (32 * i);
7019 break;
7020 }
Oded Gabbayac0ae6a2020-05-11 10:29:27 +03007021 }
7022
Oded Gabbayfcc6a4e2020-05-17 08:20:35 +03007023 if (*memory_wrapper_idx == 0xFF) {
7024 dev_err(hdev->dev, "ECC error information cannot be found\n");
7025 rc = -EINVAL;
7026 goto enable_clk_gate;
7027 }
7028
7029 WREG32(params->block_address + GAUDI_ECC_MEM_SEL_OFFSET,
7030 *memory_wrapper_idx);
7031
7032 *ecc_address =
7033 RREG32(params->block_address + GAUDI_ECC_ADDRESS_OFFSET);
7034 *ecc_syndrom =
7035 RREG32(params->block_address + GAUDI_ECC_SYNDROME_OFFSET);
7036
7037 /* Clear error indication */
7038 reg = RREG32(params->block_address + GAUDI_ECC_MEM_INFO_CLR_OFFSET);
7039 if (params->derr)
7040 reg |= FIELD_PREP(GAUDI_ECC_MEM_INFO_CLR_DERR_MASK, 1);
7041 else
7042 reg |= FIELD_PREP(GAUDI_ECC_MEM_INFO_CLR_SERR_MASK, 1);
7043
7044 WREG32(params->block_address + GAUDI_ECC_MEM_INFO_CLR_OFFSET, reg);
7045
7046enable_clk_gate:
7047 if (params->disable_clock_gating) {
Oded Gabbaye38bfd32020-07-03 20:46:12 +03007048 hdev->asic_funcs->set_clock_gating(hdev);
Greg Kroah-Hartman65a9bde62020-07-27 11:49:37 +02007049
Oded Gabbayac0ae6a2020-05-11 10:29:27 +03007050 mutex_unlock(&gaudi->clk_gate_mutex);
7051 }
Oded Gabbayfcc6a4e2020-05-17 08:20:35 +03007052
7053 return rc;
Oded Gabbayac0ae6a2020-05-11 10:29:27 +03007054}
7055
7056static void gaudi_handle_qman_err_generic(struct hl_device *hdev,
7057 const char *qm_name,
7058 u64 glbl_sts_addr,
7059 u64 arb_err_addr)
7060{
7061 u32 i, j, glbl_sts_val, arb_err_val, glbl_sts_clr_val;
7062 char reg_desc[32];
7063
7064 /* Iterate through all stream GLBL_STS1 registers + Lower CP */
7065 for (i = 0 ; i < QMAN_STREAMS + 1 ; i++) {
7066 glbl_sts_clr_val = 0;
7067 glbl_sts_val = RREG32(glbl_sts_addr + 4 * i);
7068
7069 if (!glbl_sts_val)
7070 continue;
7071
7072 if (i == QMAN_STREAMS)
7073 snprintf(reg_desc, ARRAY_SIZE(reg_desc), "LowerCP");
7074 else
7075 snprintf(reg_desc, ARRAY_SIZE(reg_desc), "stream%u", i);
7076
7077 for (j = 0 ; j < GAUDI_NUM_OF_QM_ERR_CAUSE ; j++) {
7078 if (glbl_sts_val & BIT(j)) {
7079 dev_err_ratelimited(hdev->dev,
7080 "%s %s. err cause: %s\n",
7081 qm_name, reg_desc,
7082 gaudi_qman_error_cause[j]);
7083 glbl_sts_clr_val |= BIT(j);
7084 }
7085 }
7086
7087 /* Write 1 clear errors */
Tomer Tayar1b497152021-04-06 13:32:20 +03007088 if (!hdev->stop_on_err)
7089 WREG32(glbl_sts_addr + 4 * i, glbl_sts_clr_val);
Oded Gabbayac0ae6a2020-05-11 10:29:27 +03007090 }
7091
7092 arb_err_val = RREG32(arb_err_addr);
7093
7094 if (!arb_err_val)
7095 return;
7096
7097 for (j = 0 ; j < GAUDI_NUM_OF_QM_ARB_ERR_CAUSE ; j++) {
7098 if (arb_err_val & BIT(j)) {
7099 dev_err_ratelimited(hdev->dev,
7100 "%s ARB_ERR. err cause: %s\n",
7101 qm_name,
7102 gaudi_qman_arb_error_cause[j]);
7103 }
7104 }
7105}
7106
Ofir Bittonf8bc7f02021-01-03 20:52:40 +02007107static void gaudi_print_sm_sei_info(struct hl_device *hdev, u16 event_type,
7108 struct hl_eq_sm_sei_data *sei_data)
7109{
7110 u32 index = event_type - GAUDI_EVENT_DMA_IF_SEI_0;
7111
7112 switch (sei_data->sei_cause) {
Oded Gabbay78385042021-01-26 22:56:56 +02007113 case SM_SEI_SO_OVERFLOW:
Ofir Bittonf8bc7f02021-01-03 20:52:40 +02007114 dev_err(hdev->dev,
7115 "SM %u SEI Error: SO %u overflow/underflow",
Oded Gabbay78385042021-01-26 22:56:56 +02007116 index, le32_to_cpu(sei_data->sei_log));
Ofir Bittonf8bc7f02021-01-03 20:52:40 +02007117 break;
Oded Gabbay78385042021-01-26 22:56:56 +02007118 case SM_SEI_LBW_4B_UNALIGNED:
Ofir Bittonf8bc7f02021-01-03 20:52:40 +02007119 dev_err(hdev->dev,
7120 "SM %u SEI Error: Unaligned 4B LBW access, monitor agent address low - %#x",
Oded Gabbay78385042021-01-26 22:56:56 +02007121 index, le32_to_cpu(sei_data->sei_log));
Ofir Bittonf8bc7f02021-01-03 20:52:40 +02007122 break;
Oded Gabbay78385042021-01-26 22:56:56 +02007123 case SM_SEI_AXI_RESPONSE_ERR:
Ofir Bittonf8bc7f02021-01-03 20:52:40 +02007124 dev_err(hdev->dev,
7125 "SM %u SEI Error: AXI ID %u response error",
Oded Gabbay78385042021-01-26 22:56:56 +02007126 index, le32_to_cpu(sei_data->sei_log));
Ofir Bittonf8bc7f02021-01-03 20:52:40 +02007127 break;
7128 default:
7129 dev_err(hdev->dev, "Unknown SM SEI cause %u",
Oded Gabbay78385042021-01-26 22:56:56 +02007130 le32_to_cpu(sei_data->sei_log));
Ofir Bittonf8bc7f02021-01-03 20:52:40 +02007131 break;
7132 }
7133}
7134
Oded Gabbayfcc6a4e2020-05-17 08:20:35 +03007135static void gaudi_handle_ecc_event(struct hl_device *hdev, u16 event_type,
7136 struct hl_eq_ecc_data *ecc_data)
Oded Gabbayac0ae6a2020-05-11 10:29:27 +03007137{
Oded Gabbayfcc6a4e2020-05-17 08:20:35 +03007138 struct ecc_info_extract_params params;
7139 u64 ecc_address = 0, ecc_syndrom = 0;
7140 u8 index, memory_wrapper_idx = 0;
7141 bool extract_info_from_fw;
7142 int rc;
Oded Gabbayac0ae6a2020-05-11 10:29:27 +03007143
7144 switch (event_type) {
Oded Gabbayfcc6a4e2020-05-17 08:20:35 +03007145 case GAUDI_EVENT_PCIE_CORE_SERR ... GAUDI_EVENT_PCIE_PHY_DERR:
7146 case GAUDI_EVENT_DMA0_SERR_ECC ... GAUDI_EVENT_MMU_DERR:
7147 extract_info_from_fw = true;
Oded Gabbayac0ae6a2020-05-11 10:29:27 +03007148 break;
7149 case GAUDI_EVENT_TPC0_SERR ... GAUDI_EVENT_TPC7_SERR:
7150 index = event_type - GAUDI_EVENT_TPC0_SERR;
Oded Gabbayfcc6a4e2020-05-17 08:20:35 +03007151 params.block_address = mmTPC0_CFG_BASE + index * TPC_CFG_OFFSET;
7152 params.num_memories = 90;
7153 params.derr = false;
7154 params.disable_clock_gating = true;
7155 extract_info_from_fw = false;
Oded Gabbayac0ae6a2020-05-11 10:29:27 +03007156 break;
7157 case GAUDI_EVENT_TPC0_DERR ... GAUDI_EVENT_TPC7_DERR:
7158 index = event_type - GAUDI_EVENT_TPC0_DERR;
Oded Gabbayfcc6a4e2020-05-17 08:20:35 +03007159 params.block_address =
Oded Gabbayac0ae6a2020-05-11 10:29:27 +03007160 mmTPC0_CFG_BASE + index * TPC_CFG_OFFSET;
Oded Gabbayfcc6a4e2020-05-17 08:20:35 +03007161 params.num_memories = 90;
7162 params.derr = true;
7163 params.disable_clock_gating = true;
7164 extract_info_from_fw = false;
Oded Gabbayac0ae6a2020-05-11 10:29:27 +03007165 break;
7166 case GAUDI_EVENT_MME0_ACC_SERR:
7167 case GAUDI_EVENT_MME1_ACC_SERR:
7168 case GAUDI_EVENT_MME2_ACC_SERR:
7169 case GAUDI_EVENT_MME3_ACC_SERR:
7170 index = (event_type - GAUDI_EVENT_MME0_ACC_SERR) / 4;
Oded Gabbayfcc6a4e2020-05-17 08:20:35 +03007171 params.block_address = mmMME0_ACC_BASE + index * MME_ACC_OFFSET;
7172 params.num_memories = 128;
7173 params.derr = false;
7174 params.disable_clock_gating = true;
7175 extract_info_from_fw = false;
Oded Gabbayac0ae6a2020-05-11 10:29:27 +03007176 break;
7177 case GAUDI_EVENT_MME0_ACC_DERR:
7178 case GAUDI_EVENT_MME1_ACC_DERR:
7179 case GAUDI_EVENT_MME2_ACC_DERR:
7180 case GAUDI_EVENT_MME3_ACC_DERR:
7181 index = (event_type - GAUDI_EVENT_MME0_ACC_DERR) / 4;
Oded Gabbayfcc6a4e2020-05-17 08:20:35 +03007182 params.block_address = mmMME0_ACC_BASE + index * MME_ACC_OFFSET;
7183 params.num_memories = 128;
7184 params.derr = true;
7185 params.disable_clock_gating = true;
7186 extract_info_from_fw = false;
Oded Gabbayac0ae6a2020-05-11 10:29:27 +03007187 break;
7188 case GAUDI_EVENT_MME0_SBAB_SERR:
7189 case GAUDI_EVENT_MME1_SBAB_SERR:
7190 case GAUDI_EVENT_MME2_SBAB_SERR:
7191 case GAUDI_EVENT_MME3_SBAB_SERR:
7192 index = (event_type - GAUDI_EVENT_MME0_SBAB_SERR) / 4;
Oded Gabbayfcc6a4e2020-05-17 08:20:35 +03007193 params.block_address =
7194 mmMME0_SBAB_BASE + index * MME_ACC_OFFSET;
7195 params.num_memories = 33;
7196 params.derr = false;
7197 params.disable_clock_gating = true;
7198 extract_info_from_fw = false;
Oded Gabbayac0ae6a2020-05-11 10:29:27 +03007199 break;
7200 case GAUDI_EVENT_MME0_SBAB_DERR:
7201 case GAUDI_EVENT_MME1_SBAB_DERR:
7202 case GAUDI_EVENT_MME2_SBAB_DERR:
7203 case GAUDI_EVENT_MME3_SBAB_DERR:
7204 index = (event_type - GAUDI_EVENT_MME0_SBAB_DERR) / 4;
Oded Gabbayfcc6a4e2020-05-17 08:20:35 +03007205 params.block_address =
7206 mmMME0_SBAB_BASE + index * MME_ACC_OFFSET;
7207 params.num_memories = 33;
7208 params.derr = true;
7209 params.disable_clock_gating = true;
Oded Gabbay652b4442020-11-21 14:35:35 +02007210 extract_info_from_fw = false;
7211 break;
Oded Gabbayac0ae6a2020-05-11 10:29:27 +03007212 default:
7213 return;
7214 }
7215
Oded Gabbayfcc6a4e2020-05-17 08:20:35 +03007216 if (extract_info_from_fw) {
7217 ecc_address = le64_to_cpu(ecc_data->ecc_address);
7218 ecc_syndrom = le64_to_cpu(ecc_data->ecc_syndrom);
7219 memory_wrapper_idx = ecc_data->memory_wrapper_idx;
7220 } else {
7221 rc = gaudi_extract_ecc_info(hdev, &params, &ecc_address,
7222 &ecc_syndrom, &memory_wrapper_idx);
7223 if (rc)
7224 return;
7225 }
7226
7227 dev_err(hdev->dev,
7228 "ECC error detected. address: %#llx. Syndrom: %#llx. block id %u\n",
7229 ecc_address, ecc_syndrom, memory_wrapper_idx);
Oded Gabbayac0ae6a2020-05-11 10:29:27 +03007230}
7231
7232static void gaudi_handle_qman_err(struct hl_device *hdev, u16 event_type)
7233{
7234 u64 glbl_sts_addr, arb_err_addr;
7235 u8 index;
7236 char desc[32];
7237
7238 switch (event_type) {
7239 case GAUDI_EVENT_TPC0_QM ... GAUDI_EVENT_TPC7_QM:
7240 index = event_type - GAUDI_EVENT_TPC0_QM;
7241 glbl_sts_addr =
7242 mmTPC0_QM_GLBL_STS1_0 + index * TPC_QMAN_OFFSET;
7243 arb_err_addr =
7244 mmTPC0_QM_ARB_ERR_CAUSE + index * TPC_QMAN_OFFSET;
7245 snprintf(desc, ARRAY_SIZE(desc), "%s%d", "TPC_QM", index);
7246 break;
7247 case GAUDI_EVENT_MME0_QM ... GAUDI_EVENT_MME2_QM:
7248 index = event_type - GAUDI_EVENT_MME0_QM;
7249 glbl_sts_addr =
7250 mmMME0_QM_GLBL_STS1_0 + index * MME_QMAN_OFFSET;
7251 arb_err_addr =
7252 mmMME0_QM_ARB_ERR_CAUSE + index * MME_QMAN_OFFSET;
7253 snprintf(desc, ARRAY_SIZE(desc), "%s%d", "MME_QM", index);
7254 break;
7255 case GAUDI_EVENT_DMA0_QM ... GAUDI_EVENT_DMA7_QM:
7256 index = event_type - GAUDI_EVENT_DMA0_QM;
7257 glbl_sts_addr =
7258 mmDMA0_QM_GLBL_STS1_0 + index * DMA_QMAN_OFFSET;
7259 arb_err_addr =
7260 mmDMA0_QM_ARB_ERR_CAUSE + index * DMA_QMAN_OFFSET;
7261 snprintf(desc, ARRAY_SIZE(desc), "%s%d", "DMA_QM", index);
7262 break;
Oded Gabbay3c681572020-11-02 21:10:39 +02007263 case GAUDI_EVENT_NIC0_QM0:
7264 glbl_sts_addr = mmNIC0_QM0_GLBL_STS1_0;
7265 arb_err_addr = mmNIC0_QM0_ARB_ERR_CAUSE;
7266 snprintf(desc, ARRAY_SIZE(desc), "NIC0_QM0");
7267 break;
7268 case GAUDI_EVENT_NIC0_QM1:
7269 glbl_sts_addr = mmNIC0_QM1_GLBL_STS1_0;
7270 arb_err_addr = mmNIC0_QM1_ARB_ERR_CAUSE;
7271 snprintf(desc, ARRAY_SIZE(desc), "NIC0_QM1");
7272 break;
7273 case GAUDI_EVENT_NIC1_QM0:
7274 glbl_sts_addr = mmNIC1_QM0_GLBL_STS1_0;
7275 arb_err_addr = mmNIC1_QM0_ARB_ERR_CAUSE;
7276 snprintf(desc, ARRAY_SIZE(desc), "NIC1_QM0");
7277 break;
7278 case GAUDI_EVENT_NIC1_QM1:
7279 glbl_sts_addr = mmNIC1_QM1_GLBL_STS1_0;
7280 arb_err_addr = mmNIC1_QM1_ARB_ERR_CAUSE;
7281 snprintf(desc, ARRAY_SIZE(desc), "NIC1_QM1");
7282 break;
7283 case GAUDI_EVENT_NIC2_QM0:
7284 glbl_sts_addr = mmNIC2_QM0_GLBL_STS1_0;
7285 arb_err_addr = mmNIC2_QM0_ARB_ERR_CAUSE;
7286 snprintf(desc, ARRAY_SIZE(desc), "NIC2_QM0");
7287 break;
7288 case GAUDI_EVENT_NIC2_QM1:
7289 glbl_sts_addr = mmNIC2_QM1_GLBL_STS1_0;
7290 arb_err_addr = mmNIC2_QM1_ARB_ERR_CAUSE;
7291 snprintf(desc, ARRAY_SIZE(desc), "NIC2_QM1");
7292 break;
7293 case GAUDI_EVENT_NIC3_QM0:
7294 glbl_sts_addr = mmNIC3_QM0_GLBL_STS1_0;
7295 arb_err_addr = mmNIC3_QM0_ARB_ERR_CAUSE;
7296 snprintf(desc, ARRAY_SIZE(desc), "NIC3_QM0");
7297 break;
7298 case GAUDI_EVENT_NIC3_QM1:
7299 glbl_sts_addr = mmNIC3_QM1_GLBL_STS1_0;
7300 arb_err_addr = mmNIC3_QM1_ARB_ERR_CAUSE;
7301 snprintf(desc, ARRAY_SIZE(desc), "NIC3_QM1");
7302 break;
7303 case GAUDI_EVENT_NIC4_QM0:
7304 glbl_sts_addr = mmNIC4_QM0_GLBL_STS1_0;
7305 arb_err_addr = mmNIC4_QM0_ARB_ERR_CAUSE;
7306 snprintf(desc, ARRAY_SIZE(desc), "NIC4_QM0");
7307 break;
7308 case GAUDI_EVENT_NIC4_QM1:
7309 glbl_sts_addr = mmNIC4_QM1_GLBL_STS1_0;
7310 arb_err_addr = mmNIC4_QM1_ARB_ERR_CAUSE;
7311 snprintf(desc, ARRAY_SIZE(desc), "NIC4_QM1");
7312 break;
Oded Gabbayac0ae6a2020-05-11 10:29:27 +03007313 default:
7314 return;
7315 }
7316
7317 gaudi_handle_qman_err_generic(hdev, desc, glbl_sts_addr, arb_err_addr);
7318}
7319
7320static void gaudi_print_irq_info(struct hl_device *hdev, u16 event_type,
7321 bool razwi)
7322{
Ofir Bittonebd8d122020-05-10 13:41:28 +03007323 char desc[64] = "";
Oded Gabbayac0ae6a2020-05-11 10:29:27 +03007324
7325 gaudi_get_event_desc(event_type, desc, sizeof(desc));
7326 dev_err_ratelimited(hdev->dev, "Received H/W interrupt %d [\"%s\"]\n",
7327 event_type, desc);
7328
Oded Gabbayac0ae6a2020-05-11 10:29:27 +03007329 if (razwi) {
7330 gaudi_print_razwi_info(hdev);
7331 gaudi_print_mmu_error_info(hdev);
7332 }
7333}
7334
Ohad Sharabi5d6a1982021-02-08 14:53:56 +02007335static void gaudi_print_out_of_sync_info(struct hl_device *hdev,
7336 struct cpucp_pkt_sync_err *sync_err)
7337{
7338 struct hl_hw_queue *q = &hdev->kernel_queues[GAUDI_QUEUE_ID_CPU_PQ];
7339
7340 dev_err(hdev->dev, "Out of sync with FW, FW: pi=%u, ci=%u, LKD: pi=%u, ci=%u\n",
7341 sync_err->pi, sync_err->ci, q->pi, atomic_read(&q->ci));
7342}
7343
Oded Gabbayac0ae6a2020-05-11 10:29:27 +03007344static int gaudi_soft_reset_late_init(struct hl_device *hdev)
7345{
Ofir Bittonebd8d122020-05-10 13:41:28 +03007346 struct gaudi_device *gaudi = hdev->asic_specific;
7347
Oded Gabbayac0ae6a2020-05-11 10:29:27 +03007348 /* Unmask all IRQs since some could have been received
7349 * during the soft reset
7350 */
Ofir Bittonebd8d122020-05-10 13:41:28 +03007351 return hl_fw_unmask_irq_arr(hdev, gaudi->events, sizeof(gaudi->events));
Oded Gabbayac0ae6a2020-05-11 10:29:27 +03007352}
7353
Ofir Bitton5a2998f2020-10-05 13:44:59 +03007354static int gaudi_hbm_read_interrupts(struct hl_device *hdev, int device,
7355 struct hl_eq_hbm_ecc_data *hbm_ecc_data)
Oded Gabbayac0ae6a2020-05-11 10:29:27 +03007356{
Ofir Bitton5a2998f2020-10-05 13:44:59 +03007357 u32 base, val, val2, wr_par, rd_par, ca_par, derr, serr, type, ch;
7358 int err = 0;
7359
Ohad Sharabib520ca52021-01-27 15:42:53 +02007360 if (hdev->asic_prop.fw_security_status_valid &&
7361 (hdev->asic_prop.fw_app_security_map &
7362 CPU_BOOT_DEV_STS0_HBM_ECC_EN)) {
Ofir Bitton5a2998f2020-10-05 13:44:59 +03007363 if (!hbm_ecc_data) {
7364 dev_err(hdev->dev, "No FW ECC data");
7365 return 0;
7366 }
7367
7368 wr_par = FIELD_GET(CPUCP_PKT_HBM_ECC_INFO_WR_PAR_MASK,
7369 le32_to_cpu(hbm_ecc_data->hbm_ecc_info));
7370 rd_par = FIELD_GET(CPUCP_PKT_HBM_ECC_INFO_RD_PAR_MASK,
7371 le32_to_cpu(hbm_ecc_data->hbm_ecc_info));
7372 ca_par = FIELD_GET(CPUCP_PKT_HBM_ECC_INFO_CA_PAR_MASK,
7373 le32_to_cpu(hbm_ecc_data->hbm_ecc_info));
7374 derr = FIELD_GET(CPUCP_PKT_HBM_ECC_INFO_DERR_MASK,
7375 le32_to_cpu(hbm_ecc_data->hbm_ecc_info));
7376 serr = FIELD_GET(CPUCP_PKT_HBM_ECC_INFO_SERR_MASK,
7377 le32_to_cpu(hbm_ecc_data->hbm_ecc_info));
7378 type = FIELD_GET(CPUCP_PKT_HBM_ECC_INFO_TYPE_MASK,
7379 le32_to_cpu(hbm_ecc_data->hbm_ecc_info));
7380 ch = FIELD_GET(CPUCP_PKT_HBM_ECC_INFO_HBM_CH_MASK,
7381 le32_to_cpu(hbm_ecc_data->hbm_ecc_info));
7382
7383 dev_err(hdev->dev,
Ohad Sharabib520ca52021-01-27 15:42:53 +02007384 "HBM%d pc%d interrupts info: WR_PAR=%d, RD_PAR=%d, CA_PAR=%d, SERR=%d, DERR=%d\n",
7385 device, ch, wr_par, rd_par, ca_par, serr, derr);
7386 dev_err(hdev->dev,
7387 "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",
7388 device, ch, hbm_ecc_data->first_addr, type,
7389 hbm_ecc_data->sec_cont_cnt, hbm_ecc_data->sec_cnt,
7390 hbm_ecc_data->dec_cnt);
Ofir Bitton5a2998f2020-10-05 13:44:59 +03007391 return 0;
7392 }
Oded Gabbayac0ae6a2020-05-11 10:29:27 +03007393
Ohad Sharabib520ca52021-01-27 15:42:53 +02007394 if (!hdev->asic_prop.fw_security_disabled) {
7395 dev_info(hdev->dev, "Cannot access MC regs for ECC data while security is enabled\n");
7396 return 0;
7397 }
7398
Oded Gabbayac0ae6a2020-05-11 10:29:27 +03007399 base = GAUDI_HBM_CFG_BASE + device * GAUDI_HBM_CFG_OFFSET;
7400 for (ch = 0 ; ch < GAUDI_HBM_CHANNELS ; ch++) {
7401 val = RREG32_MASK(base + ch * 0x1000 + 0x06C, 0x0000FFFF);
7402 val = (val & 0xFF) | ((val >> 8) & 0xFF);
7403 if (val) {
7404 err = 1;
7405 dev_err(hdev->dev,
7406 "HBM%d pc%d interrupts info: WR_PAR=%d, RD_PAR=%d, CA_PAR=%d, SERR=%d, DERR=%d\n",
7407 device, ch * 2, val & 0x1, (val >> 1) & 0x1,
7408 (val >> 2) & 0x1, (val >> 3) & 0x1,
7409 (val >> 4) & 0x1);
7410
7411 val2 = RREG32(base + ch * 0x1000 + 0x060);
7412 dev_err(hdev->dev,
Ofir Bitton5a2998f2020-10-05 13:44:59 +03007413 "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",
Oded Gabbayac0ae6a2020-05-11 10:29:27 +03007414 device, ch * 2,
7415 RREG32(base + ch * 0x1000 + 0x064),
7416 (val2 & 0x200) >> 9, (val2 & 0xFC00) >> 10,
7417 (val2 & 0xFF0000) >> 16,
7418 (val2 & 0xFF000000) >> 24);
7419 }
7420
7421 val = RREG32_MASK(base + ch * 0x1000 + 0x07C, 0x0000FFFF);
7422 val = (val & 0xFF) | ((val >> 8) & 0xFF);
7423 if (val) {
7424 err = 1;
7425 dev_err(hdev->dev,
7426 "HBM%d pc%d interrupts info: WR_PAR=%d, RD_PAR=%d, CA_PAR=%d, SERR=%d, DERR=%d\n",
7427 device, ch * 2 + 1, val & 0x1, (val >> 1) & 0x1,
7428 (val >> 2) & 0x1, (val >> 3) & 0x1,
7429 (val >> 4) & 0x1);
7430
7431 val2 = RREG32(base + ch * 0x1000 + 0x070);
7432 dev_err(hdev->dev,
Ofir Bitton5a2998f2020-10-05 13:44:59 +03007433 "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",
Oded Gabbayac0ae6a2020-05-11 10:29:27 +03007434 device, ch * 2 + 1,
7435 RREG32(base + ch * 0x1000 + 0x074),
7436 (val2 & 0x200) >> 9, (val2 & 0xFC00) >> 10,
7437 (val2 & 0xFF0000) >> 16,
7438 (val2 & 0xFF000000) >> 24);
7439 }
7440
7441 /* Clear interrupts */
7442 RMWREG32(base + (ch * 0x1000) + 0x060, 0x1C8, 0x1FF);
7443 RMWREG32(base + (ch * 0x1000) + 0x070, 0x1C8, 0x1FF);
7444 WREG32(base + (ch * 0x1000) + 0x06C, 0x1F1F);
7445 WREG32(base + (ch * 0x1000) + 0x07C, 0x1F1F);
7446 RMWREG32(base + (ch * 0x1000) + 0x060, 0x0, 0xF);
7447 RMWREG32(base + (ch * 0x1000) + 0x070, 0x0, 0xF);
7448 }
7449
7450 val = RREG32(base + 0x8F30);
7451 val2 = RREG32(base + 0x8F34);
7452 if (val | val2) {
7453 err = 1;
7454 dev_err(hdev->dev,
7455 "HBM %d MC SRAM SERR info: Reg 0x8F30=0x%x, Reg 0x8F34=0x%x\n",
7456 device, val, val2);
7457 }
7458 val = RREG32(base + 0x8F40);
7459 val2 = RREG32(base + 0x8F44);
7460 if (val | val2) {
7461 err = 1;
7462 dev_err(hdev->dev,
7463 "HBM %d MC SRAM DERR info: Reg 0x8F40=0x%x, Reg 0x8F44=0x%x\n",
7464 device, val, val2);
7465 }
7466
7467 return err;
7468}
7469
7470static int gaudi_hbm_event_to_dev(u16 hbm_event_type)
7471{
7472 switch (hbm_event_type) {
7473 case GAUDI_EVENT_HBM0_SPI_0:
7474 case GAUDI_EVENT_HBM0_SPI_1:
7475 return 0;
7476 case GAUDI_EVENT_HBM1_SPI_0:
7477 case GAUDI_EVENT_HBM1_SPI_1:
7478 return 1;
7479 case GAUDI_EVENT_HBM2_SPI_0:
7480 case GAUDI_EVENT_HBM2_SPI_1:
7481 return 2;
7482 case GAUDI_EVENT_HBM3_SPI_0:
7483 case GAUDI_EVENT_HBM3_SPI_1:
7484 return 3;
7485 default:
7486 break;
7487 }
7488
7489 /* Should never happen */
7490 return 0;
7491}
7492
7493static bool gaudi_tpc_read_interrupts(struct hl_device *hdev, u8 tpc_id,
7494 char *interrupt_name)
7495{
7496 struct gaudi_device *gaudi = hdev->asic_specific;
7497 u32 tpc_offset = tpc_id * TPC_CFG_OFFSET, tpc_interrupts_cause, i;
7498 bool soft_reset_required = false;
7499
7500 /* Accessing the TPC_INTR_CAUSE registers requires disabling the clock
Oded Gabbay6138bbe2020-09-04 20:18:16 +03007501 * gating, and thus cannot be done in CPU-CP and should be done instead
Oded Gabbayac0ae6a2020-05-11 10:29:27 +03007502 * by the driver.
7503 */
7504
7505 mutex_lock(&gaudi->clk_gate_mutex);
7506
7507 hdev->asic_funcs->disable_clock_gating(hdev);
7508
7509 tpc_interrupts_cause = RREG32(mmTPC0_CFG_TPC_INTR_CAUSE + tpc_offset) &
7510 TPC0_CFG_TPC_INTR_CAUSE_CAUSE_MASK;
7511
7512 for (i = 0 ; i < GAUDI_NUM_OF_TPC_INTR_CAUSE ; i++)
7513 if (tpc_interrupts_cause & BIT(i)) {
7514 dev_err_ratelimited(hdev->dev,
7515 "TPC%d_%s interrupt cause: %s\n",
7516 tpc_id, interrupt_name,
7517 gaudi_tpc_interrupts_cause[i]);
7518 /* If this is QM error, we need to soft-reset */
7519 if (i == 15)
7520 soft_reset_required = true;
7521 }
7522
7523 /* Clear interrupts */
7524 WREG32(mmTPC0_CFG_TPC_INTR_CAUSE + tpc_offset, 0);
7525
Oded Gabbaye38bfd32020-07-03 20:46:12 +03007526 hdev->asic_funcs->set_clock_gating(hdev);
Oded Gabbayac0ae6a2020-05-11 10:29:27 +03007527
7528 mutex_unlock(&gaudi->clk_gate_mutex);
7529
7530 return soft_reset_required;
7531}
7532
7533static int tpc_dec_event_to_tpc_id(u16 tpc_dec_event_type)
7534{
7535 return (tpc_dec_event_type - GAUDI_EVENT_TPC0_DEC) >> 1;
7536}
7537
7538static int tpc_krn_event_to_tpc_id(u16 tpc_dec_event_type)
7539{
7540 return (tpc_dec_event_type - GAUDI_EVENT_TPC0_KRN_ERR) / 6;
7541}
7542
7543static void gaudi_print_clk_change_info(struct hl_device *hdev,
7544 u16 event_type)
7545{
7546 switch (event_type) {
7547 case GAUDI_EVENT_FIX_POWER_ENV_S:
Ofir Bitton0a068ad2020-07-21 10:49:51 +03007548 hdev->clk_throttling_reason |= HL_CLK_THROTTLE_POWER;
Oded Gabbayac0ae6a2020-05-11 10:29:27 +03007549 dev_info_ratelimited(hdev->dev,
7550 "Clock throttling due to power consumption\n");
7551 break;
7552
7553 case GAUDI_EVENT_FIX_POWER_ENV_E:
Ofir Bitton0a068ad2020-07-21 10:49:51 +03007554 hdev->clk_throttling_reason &= ~HL_CLK_THROTTLE_POWER;
Oded Gabbayac0ae6a2020-05-11 10:29:27 +03007555 dev_info_ratelimited(hdev->dev,
7556 "Power envelop is safe, back to optimal clock\n");
7557 break;
7558
7559 case GAUDI_EVENT_FIX_THERMAL_ENV_S:
Ofir Bitton0a068ad2020-07-21 10:49:51 +03007560 hdev->clk_throttling_reason |= HL_CLK_THROTTLE_THERMAL;
Oded Gabbayac0ae6a2020-05-11 10:29:27 +03007561 dev_info_ratelimited(hdev->dev,
7562 "Clock throttling due to overheating\n");
7563 break;
7564
7565 case GAUDI_EVENT_FIX_THERMAL_ENV_E:
Ofir Bitton0a068ad2020-07-21 10:49:51 +03007566 hdev->clk_throttling_reason &= ~HL_CLK_THROTTLE_THERMAL;
Oded Gabbayac0ae6a2020-05-11 10:29:27 +03007567 dev_info_ratelimited(hdev->dev,
7568 "Thermal envelop is safe, back to optimal clock\n");
7569 break;
7570
7571 default:
7572 dev_err(hdev->dev, "Received invalid clock change event %d\n",
7573 event_type);
7574 break;
7575 }
7576}
7577
7578static void gaudi_handle_eqe(struct hl_device *hdev,
7579 struct hl_eq_entry *eq_entry)
7580{
7581 struct gaudi_device *gaudi = hdev->asic_specific;
7582 u32 ctl = le32_to_cpu(eq_entry->hdr.ctl);
7583 u16 event_type = ((ctl & EQ_CTL_EVENT_TYPE_MASK)
7584 >> EQ_CTL_EVENT_TYPE_SHIFT);
7585 u8 cause;
Oded Gabbay66446822020-05-18 16:48:01 +03007586 bool reset_required;
Oded Gabbayac0ae6a2020-05-11 10:29:27 +03007587
7588 gaudi->events_stat[event_type]++;
7589 gaudi->events_stat_aggregate[event_type]++;
7590
7591 switch (event_type) {
7592 case GAUDI_EVENT_PCIE_CORE_DERR:
7593 case GAUDI_EVENT_PCIE_IF_DERR:
7594 case GAUDI_EVENT_PCIE_PHY_DERR:
7595 case GAUDI_EVENT_TPC0_DERR ... GAUDI_EVENT_TPC7_DERR:
7596 case GAUDI_EVENT_MME0_ACC_DERR:
7597 case GAUDI_EVENT_MME0_SBAB_DERR:
7598 case GAUDI_EVENT_MME1_ACC_DERR:
7599 case GAUDI_EVENT_MME1_SBAB_DERR:
7600 case GAUDI_EVENT_MME2_ACC_DERR:
7601 case GAUDI_EVENT_MME2_SBAB_DERR:
7602 case GAUDI_EVENT_MME3_ACC_DERR:
7603 case GAUDI_EVENT_MME3_SBAB_DERR:
7604 case GAUDI_EVENT_DMA0_DERR_ECC ... GAUDI_EVENT_DMA7_DERR_ECC:
7605 fallthrough;
7606 case GAUDI_EVENT_CPU_IF_ECC_DERR:
7607 case GAUDI_EVENT_PSOC_MEM_DERR:
7608 case GAUDI_EVENT_PSOC_CORESIGHT_DERR:
7609 case GAUDI_EVENT_SRAM0_DERR ... GAUDI_EVENT_SRAM28_DERR:
7610 case GAUDI_EVENT_DMA_IF0_DERR ... GAUDI_EVENT_DMA_IF3_DERR:
Oded Gabbayac0ae6a2020-05-11 10:29:27 +03007611 case GAUDI_EVENT_HBM_0_DERR ... GAUDI_EVENT_HBM_3_DERR:
7612 case GAUDI_EVENT_MMU_DERR:
Oded Gabbayfcc6a4e2020-05-17 08:20:35 +03007613 gaudi_print_irq_info(hdev, event_type, true);
7614 gaudi_handle_ecc_event(hdev, event_type, &eq_entry->ecc_data);
Ohad Sharabie42a6400f2021-02-17 20:42:48 +02007615 goto reset_device;
Oded Gabbayfcc6a4e2020-05-17 08:20:35 +03007616
7617 case GAUDI_EVENT_GIC500:
Oded Gabbayac0ae6a2020-05-11 10:29:27 +03007618 case GAUDI_EVENT_AXI_ECC:
7619 case GAUDI_EVENT_L2_RAM_ECC:
7620 case GAUDI_EVENT_PLL0 ... GAUDI_EVENT_PLL17:
7621 gaudi_print_irq_info(hdev, event_type, false);
Ohad Sharabie42a6400f2021-02-17 20:42:48 +02007622 goto reset_device;
Oded Gabbayac0ae6a2020-05-11 10:29:27 +03007623
7624 case GAUDI_EVENT_HBM0_SPI_0:
7625 case GAUDI_EVENT_HBM1_SPI_0:
7626 case GAUDI_EVENT_HBM2_SPI_0:
7627 case GAUDI_EVENT_HBM3_SPI_0:
7628 gaudi_print_irq_info(hdev, event_type, false);
7629 gaudi_hbm_read_interrupts(hdev,
Ofir Bitton5a2998f2020-10-05 13:44:59 +03007630 gaudi_hbm_event_to_dev(event_type),
7631 &eq_entry->hbm_ecc_data);
Ohad Sharabie42a6400f2021-02-17 20:42:48 +02007632 goto reset_device;
Oded Gabbayac0ae6a2020-05-11 10:29:27 +03007633
7634 case GAUDI_EVENT_HBM0_SPI_1:
7635 case GAUDI_EVENT_HBM1_SPI_1:
7636 case GAUDI_EVENT_HBM2_SPI_1:
7637 case GAUDI_EVENT_HBM3_SPI_1:
7638 gaudi_print_irq_info(hdev, event_type, false);
7639 gaudi_hbm_read_interrupts(hdev,
Ofir Bitton5a2998f2020-10-05 13:44:59 +03007640 gaudi_hbm_event_to_dev(event_type),
7641 &eq_entry->hbm_ecc_data);
Oded Gabbay230cd892021-01-26 22:58:13 +02007642 hl_fw_unmask_irq(hdev, event_type);
Oded Gabbayac0ae6a2020-05-11 10:29:27 +03007643 break;
7644
7645 case GAUDI_EVENT_TPC0_DEC:
7646 case GAUDI_EVENT_TPC1_DEC:
7647 case GAUDI_EVENT_TPC2_DEC:
7648 case GAUDI_EVENT_TPC3_DEC:
7649 case GAUDI_EVENT_TPC4_DEC:
7650 case GAUDI_EVENT_TPC5_DEC:
7651 case GAUDI_EVENT_TPC6_DEC:
7652 case GAUDI_EVENT_TPC7_DEC:
7653 gaudi_print_irq_info(hdev, event_type, true);
Oded Gabbay66446822020-05-18 16:48:01 +03007654 reset_required = gaudi_tpc_read_interrupts(hdev,
Oded Gabbayac0ae6a2020-05-11 10:29:27 +03007655 tpc_dec_event_to_tpc_id(event_type),
7656 "AXI_SLV_DEC_Error");
Oded Gabbay66446822020-05-18 16:48:01 +03007657 if (reset_required) {
7658 dev_err(hdev->dev, "hard reset required due to %s\n",
7659 gaudi_irq_map_table[event_type].name);
7660
Ohad Sharabie42a6400f2021-02-17 20:42:48 +02007661 goto reset_device;
Oded Gabbay66446822020-05-18 16:48:01 +03007662 } else {
7663 hl_fw_unmask_irq(hdev, event_type);
Omer Shpigelmand7985072020-05-17 23:01:22 +03007664 }
Oded Gabbayac0ae6a2020-05-11 10:29:27 +03007665 break;
7666
7667 case GAUDI_EVENT_TPC0_KRN_ERR:
7668 case GAUDI_EVENT_TPC1_KRN_ERR:
7669 case GAUDI_EVENT_TPC2_KRN_ERR:
7670 case GAUDI_EVENT_TPC3_KRN_ERR:
7671 case GAUDI_EVENT_TPC4_KRN_ERR:
7672 case GAUDI_EVENT_TPC5_KRN_ERR:
7673 case GAUDI_EVENT_TPC6_KRN_ERR:
7674 case GAUDI_EVENT_TPC7_KRN_ERR:
7675 gaudi_print_irq_info(hdev, event_type, true);
Oded Gabbay66446822020-05-18 16:48:01 +03007676 reset_required = gaudi_tpc_read_interrupts(hdev,
Oded Gabbayac0ae6a2020-05-11 10:29:27 +03007677 tpc_krn_event_to_tpc_id(event_type),
7678 "KRN_ERR");
Oded Gabbay66446822020-05-18 16:48:01 +03007679 if (reset_required) {
7680 dev_err(hdev->dev, "hard reset required due to %s\n",
7681 gaudi_irq_map_table[event_type].name);
7682
Ohad Sharabie42a6400f2021-02-17 20:42:48 +02007683 goto reset_device;
Oded Gabbay66446822020-05-18 16:48:01 +03007684 } else {
7685 hl_fw_unmask_irq(hdev, event_type);
Omer Shpigelmand7985072020-05-17 23:01:22 +03007686 }
Oded Gabbayac0ae6a2020-05-11 10:29:27 +03007687 break;
7688
7689 case GAUDI_EVENT_PCIE_CORE_SERR:
7690 case GAUDI_EVENT_PCIE_IF_SERR:
7691 case GAUDI_EVENT_PCIE_PHY_SERR:
7692 case GAUDI_EVENT_TPC0_SERR ... GAUDI_EVENT_TPC7_SERR:
7693 case GAUDI_EVENT_MME0_ACC_SERR:
7694 case GAUDI_EVENT_MME0_SBAB_SERR:
7695 case GAUDI_EVENT_MME1_ACC_SERR:
7696 case GAUDI_EVENT_MME1_SBAB_SERR:
7697 case GAUDI_EVENT_MME2_ACC_SERR:
7698 case GAUDI_EVENT_MME2_SBAB_SERR:
7699 case GAUDI_EVENT_MME3_ACC_SERR:
7700 case GAUDI_EVENT_MME3_SBAB_SERR:
7701 case GAUDI_EVENT_DMA0_SERR_ECC ... GAUDI_EVENT_DMA7_SERR_ECC:
7702 case GAUDI_EVENT_CPU_IF_ECC_SERR:
7703 case GAUDI_EVENT_PSOC_MEM_SERR:
7704 case GAUDI_EVENT_PSOC_CORESIGHT_SERR:
7705 case GAUDI_EVENT_SRAM0_SERR ... GAUDI_EVENT_SRAM28_SERR:
7706 case GAUDI_EVENT_DMA_IF0_SERR ... GAUDI_EVENT_DMA_IF3_SERR:
7707 case GAUDI_EVENT_HBM_0_SERR ... GAUDI_EVENT_HBM_3_SERR:
7708 fallthrough;
7709 case GAUDI_EVENT_MMU_SERR:
Oded Gabbayfcc6a4e2020-05-17 08:20:35 +03007710 gaudi_print_irq_info(hdev, event_type, true);
7711 gaudi_handle_ecc_event(hdev, event_type, &eq_entry->ecc_data);
7712 hl_fw_unmask_irq(hdev, event_type);
7713 break;
7714
Oded Gabbayac0ae6a2020-05-11 10:29:27 +03007715 case GAUDI_EVENT_PCIE_DEC:
7716 case GAUDI_EVENT_MME0_WBC_RSP:
7717 case GAUDI_EVENT_MME0_SBAB0_RSP:
7718 case GAUDI_EVENT_MME1_WBC_RSP:
7719 case GAUDI_EVENT_MME1_SBAB0_RSP:
7720 case GAUDI_EVENT_MME2_WBC_RSP:
7721 case GAUDI_EVENT_MME2_SBAB0_RSP:
7722 case GAUDI_EVENT_MME3_WBC_RSP:
7723 case GAUDI_EVENT_MME3_SBAB0_RSP:
7724 case GAUDI_EVENT_CPU_AXI_SPLITTER:
7725 case GAUDI_EVENT_PSOC_AXI_DEC:
7726 case GAUDI_EVENT_PSOC_PRSTN_FALL:
7727 case GAUDI_EVENT_MMU_PAGE_FAULT:
7728 case GAUDI_EVENT_MMU_WR_PERM:
7729 case GAUDI_EVENT_RAZWI_OR_ADC:
7730 case GAUDI_EVENT_TPC0_QM ... GAUDI_EVENT_TPC7_QM:
7731 case GAUDI_EVENT_MME0_QM ... GAUDI_EVENT_MME2_QM:
7732 case GAUDI_EVENT_DMA0_QM ... GAUDI_EVENT_DMA7_QM:
7733 fallthrough;
Oded Gabbay3c681572020-11-02 21:10:39 +02007734 case GAUDI_EVENT_NIC0_QM0:
7735 case GAUDI_EVENT_NIC0_QM1:
7736 case GAUDI_EVENT_NIC1_QM0:
7737 case GAUDI_EVENT_NIC1_QM1:
7738 case GAUDI_EVENT_NIC2_QM0:
7739 case GAUDI_EVENT_NIC2_QM1:
7740 case GAUDI_EVENT_NIC3_QM0:
7741 case GAUDI_EVENT_NIC3_QM1:
7742 case GAUDI_EVENT_NIC4_QM0:
7743 case GAUDI_EVENT_NIC4_QM1:
Oded Gabbayac0ae6a2020-05-11 10:29:27 +03007744 case GAUDI_EVENT_DMA0_CORE ... GAUDI_EVENT_DMA7_CORE:
7745 gaudi_print_irq_info(hdev, event_type, true);
7746 gaudi_handle_qman_err(hdev, event_type);
Ofir Bittonebd8d122020-05-10 13:41:28 +03007747 hl_fw_unmask_irq(hdev, event_type);
Oded Gabbayac0ae6a2020-05-11 10:29:27 +03007748 break;
7749
7750 case GAUDI_EVENT_RAZWI_OR_ADC_SW:
7751 gaudi_print_irq_info(hdev, event_type, true);
Ohad Sharabie42a6400f2021-02-17 20:42:48 +02007752 goto reset_device;
Oded Gabbayac0ae6a2020-05-11 10:29:27 +03007753
7754 case GAUDI_EVENT_TPC0_BMON_SPMU:
7755 case GAUDI_EVENT_TPC1_BMON_SPMU:
7756 case GAUDI_EVENT_TPC2_BMON_SPMU:
7757 case GAUDI_EVENT_TPC3_BMON_SPMU:
7758 case GAUDI_EVENT_TPC4_BMON_SPMU:
7759 case GAUDI_EVENT_TPC5_BMON_SPMU:
7760 case GAUDI_EVENT_TPC6_BMON_SPMU:
7761 case GAUDI_EVENT_TPC7_BMON_SPMU:
7762 case GAUDI_EVENT_DMA_BM_CH0 ... GAUDI_EVENT_DMA_BM_CH7:
7763 gaudi_print_irq_info(hdev, event_type, false);
Ofir Bittonebd8d122020-05-10 13:41:28 +03007764 hl_fw_unmask_irq(hdev, event_type);
Oded Gabbayac0ae6a2020-05-11 10:29:27 +03007765 break;
7766
Ofir Bittonf8bc7f02021-01-03 20:52:40 +02007767 case GAUDI_EVENT_DMA_IF_SEI_0 ... GAUDI_EVENT_DMA_IF_SEI_3:
7768 gaudi_print_irq_info(hdev, event_type, false);
7769 gaudi_print_sm_sei_info(hdev, event_type,
7770 &eq_entry->sm_sei_data);
7771 hl_fw_unmask_irq(hdev, event_type);
7772 break;
7773
Oded Gabbayac0ae6a2020-05-11 10:29:27 +03007774 case GAUDI_EVENT_FIX_POWER_ENV_S ... GAUDI_EVENT_FIX_THERMAL_ENV_E:
7775 gaudi_print_clk_change_info(hdev, event_type);
Ofir Bittonebd8d122020-05-10 13:41:28 +03007776 hl_fw_unmask_irq(hdev, event_type);
Oded Gabbayac0ae6a2020-05-11 10:29:27 +03007777 break;
7778
7779 case GAUDI_EVENT_PSOC_GPIO_U16_0:
7780 cause = le64_to_cpu(eq_entry->data[0]) & 0xFF;
7781 dev_err(hdev->dev,
7782 "Received high temp H/W interrupt %d (cause %d)\n",
7783 event_type, cause);
7784 break;
7785
Ofir Bittond661d792021-03-09 14:45:04 +02007786 case GAUDI_EVENT_DEV_RESET_REQ:
Ofir Bitton2ea09532021-03-03 13:23:47 +02007787 gaudi_print_irq_info(hdev, event_type, false);
7788 goto reset_device;
7789
Ohad Sharabi5d6a1982021-02-08 14:53:56 +02007790 case GAUDI_EVENT_PKT_QUEUE_OUT_SYNC:
7791 gaudi_print_irq_info(hdev, event_type, false);
7792 gaudi_print_out_of_sync_info(hdev, &eq_entry->pkt_sync_err);
Ohad Sharabie42a6400f2021-02-17 20:42:48 +02007793 goto reset_device;
Ohad Sharabi5d6a1982021-02-08 14:53:56 +02007794
Oded Gabbayac0ae6a2020-05-11 10:29:27 +03007795 default:
7796 dev_err(hdev->dev, "Received invalid H/W interrupt %d\n",
7797 event_type);
7798 break;
7799 }
Ohad Sharabie42a6400f2021-02-17 20:42:48 +02007800
7801 return;
7802
7803reset_device:
7804 if (hdev->hard_reset_on_fw_events)
7805 hl_device_reset(hdev, HL_RESET_HARD);
7806 else
7807 hl_fw_unmask_irq(hdev, event_type);
Oded Gabbayac0ae6a2020-05-11 10:29:27 +03007808}
7809
7810static void *gaudi_get_events_stat(struct hl_device *hdev, bool aggregate,
7811 u32 *size)
7812{
7813 struct gaudi_device *gaudi = hdev->asic_specific;
7814
7815 if (aggregate) {
7816 *size = (u32) sizeof(gaudi->events_stat_aggregate);
7817 return gaudi->events_stat_aggregate;
7818 }
7819
7820 *size = (u32) sizeof(gaudi->events_stat);
7821 return gaudi->events_stat;
7822}
7823
Omer Shpigelman8ff5f4f2020-05-24 23:06:59 +03007824static int gaudi_mmu_invalidate_cache(struct hl_device *hdev, bool is_hard,
Oded Gabbayac0ae6a2020-05-11 10:29:27 +03007825 u32 flags)
7826{
7827 struct gaudi_device *gaudi = hdev->asic_specific;
7828 u32 status, timeout_usec;
7829 int rc;
7830
7831 if (!(gaudi->hw_cap_initialized & HW_CAP_MMU) ||
7832 hdev->hard_reset_pending)
Omer Shpigelman8ff5f4f2020-05-24 23:06:59 +03007833 return 0;
Oded Gabbayac0ae6a2020-05-11 10:29:27 +03007834
7835 if (hdev->pldm)
7836 timeout_usec = GAUDI_PLDM_MMU_TIMEOUT_USEC;
7837 else
7838 timeout_usec = MMU_CONFIG_TIMEOUT_USEC;
7839
7840 /* L0 & L1 invalidation */
Omer Shpigelmancfd41762020-06-03 13:03:35 +03007841 WREG32(mmSTLB_INV_PS, 3);
7842 WREG32(mmSTLB_CACHE_INV, gaudi->mmu_cache_inv_pi++);
Omer Shpigelman42d0b0b2020-05-17 17:35:39 +03007843 WREG32(mmSTLB_INV_PS, 2);
Oded Gabbayac0ae6a2020-05-11 10:29:27 +03007844
7845 rc = hl_poll_timeout(
7846 hdev,
Omer Shpigelman42d0b0b2020-05-17 17:35:39 +03007847 mmSTLB_INV_PS,
Oded Gabbayac0ae6a2020-05-11 10:29:27 +03007848 status,
7849 !status,
7850 1000,
7851 timeout_usec);
7852
Omer Shpigelman42d0b0b2020-05-17 17:35:39 +03007853 WREG32(mmSTLB_INV_SET, 0);
7854
Omer Shpigelman8ff5f4f2020-05-24 23:06:59 +03007855 if (rc) {
7856 dev_err_ratelimited(hdev->dev,
7857 "MMU cache invalidation timeout\n");
Ohad Sharabie42a6400f2021-02-17 20:42:48 +02007858 hl_device_reset(hdev, HL_RESET_HARD);
Omer Shpigelman8ff5f4f2020-05-24 23:06:59 +03007859 }
7860
7861 return rc;
Oded Gabbayac0ae6a2020-05-11 10:29:27 +03007862}
7863
Omer Shpigelman8ff5f4f2020-05-24 23:06:59 +03007864static int gaudi_mmu_invalidate_cache_range(struct hl_device *hdev,
Oded Gabbayac0ae6a2020-05-11 10:29:27 +03007865 bool is_hard, u32 asid, u64 va, u64 size)
7866{
7867 struct gaudi_device *gaudi = hdev->asic_specific;
7868 u32 status, timeout_usec;
7869 u32 inv_data;
7870 u32 pi;
7871 int rc;
7872
7873 if (!(gaudi->hw_cap_initialized & HW_CAP_MMU) ||
7874 hdev->hard_reset_pending)
Omer Shpigelman8ff5f4f2020-05-24 23:06:59 +03007875 return 0;
Oded Gabbayac0ae6a2020-05-11 10:29:27 +03007876
Oded Gabbayac0ae6a2020-05-11 10:29:27 +03007877 if (hdev->pldm)
7878 timeout_usec = GAUDI_PLDM_MMU_TIMEOUT_USEC;
7879 else
7880 timeout_usec = MMU_CONFIG_TIMEOUT_USEC;
7881
7882 /*
7883 * TODO: currently invalidate entire L0 & L1 as in regular hard
7884 * invalidation. Need to apply invalidation of specific cache
7885 * lines with mask of ASID & VA & size.
7886 * Note that L1 with be flushed entirely in any case.
7887 */
7888
7889 /* L0 & L1 invalidation */
7890 inv_data = RREG32(mmSTLB_CACHE_INV);
7891 /* PI is 8 bit */
7892 pi = ((inv_data & STLB_CACHE_INV_PRODUCER_INDEX_MASK) + 1) & 0xFF;
7893 WREG32(mmSTLB_CACHE_INV,
7894 (inv_data & STLB_CACHE_INV_INDEX_MASK_MASK) | pi);
7895
7896 rc = hl_poll_timeout(
7897 hdev,
7898 mmSTLB_INV_CONSUMER_INDEX,
7899 status,
7900 status == pi,
7901 1000,
7902 timeout_usec);
7903
Omer Shpigelman8ff5f4f2020-05-24 23:06:59 +03007904 if (rc) {
7905 dev_err_ratelimited(hdev->dev,
7906 "MMU cache invalidation timeout\n");
Ohad Sharabie42a6400f2021-02-17 20:42:48 +02007907 hl_device_reset(hdev, HL_RESET_HARD);
Omer Shpigelman8ff5f4f2020-05-24 23:06:59 +03007908 }
7909
7910 return rc;
Oded Gabbayac0ae6a2020-05-11 10:29:27 +03007911}
7912
7913static int gaudi_mmu_update_asid_hop0_addr(struct hl_device *hdev,
7914 u32 asid, u64 phys_addr)
7915{
7916 u32 status, timeout_usec;
7917 int rc;
7918
7919 if (hdev->pldm)
7920 timeout_usec = GAUDI_PLDM_MMU_TIMEOUT_USEC;
7921 else
7922 timeout_usec = MMU_CONFIG_TIMEOUT_USEC;
7923
7924 WREG32(MMU_ASID, asid);
7925 WREG32(MMU_HOP0_PA43_12, phys_addr >> MMU_HOP0_PA43_12_SHIFT);
7926 WREG32(MMU_HOP0_PA49_44, phys_addr >> MMU_HOP0_PA49_44_SHIFT);
7927 WREG32(MMU_BUSY, 0x80000000);
7928
7929 rc = hl_poll_timeout(
7930 hdev,
7931 MMU_BUSY,
7932 status,
7933 !(status & 0x80000000),
7934 1000,
7935 timeout_usec);
7936
7937 if (rc) {
7938 dev_err(hdev->dev,
7939 "Timeout during MMU hop0 config of asid %d\n", asid);
7940 return rc;
7941 }
7942
7943 return 0;
7944}
7945
7946static int gaudi_send_heartbeat(struct hl_device *hdev)
7947{
7948 struct gaudi_device *gaudi = hdev->asic_specific;
7949
7950 if (!(gaudi->hw_cap_initialized & HW_CAP_CPU_Q))
7951 return 0;
7952
7953 return hl_fw_send_heartbeat(hdev);
7954}
7955
Oded Gabbay2f553422020-08-15 16:28:10 +03007956static int gaudi_cpucp_info_get(struct hl_device *hdev)
Oded Gabbayac0ae6a2020-05-11 10:29:27 +03007957{
7958 struct gaudi_device *gaudi = hdev->asic_specific;
7959 struct asic_fixed_properties *prop = &hdev->asic_prop;
7960 int rc;
7961
7962 if (!(gaudi->hw_cap_initialized & HW_CAP_CPU_Q))
7963 return 0;
7964
Ohad Sharabie9c20032021-03-22 14:30:52 +02007965 rc = hl_fw_cpucp_handshake(hdev, mmCPU_BOOT_DEV_STS0, mmCPU_BOOT_ERR0);
Oded Gabbayac0ae6a2020-05-11 10:29:27 +03007966 if (rc)
7967 return rc;
7968
Oded Gabbay2f553422020-08-15 16:28:10 +03007969 if (!strlen(prop->cpucp_info.card_name))
7970 strncpy(prop->cpucp_info.card_name, GAUDI_DEFAULT_CARD_NAME,
Oded Gabbayac0ae6a2020-05-11 10:29:27 +03007971 CARD_NAME_MAX_LEN);
7972
Oded Gabbay2f553422020-08-15 16:28:10 +03007973 hdev->card_type = le32_to_cpu(hdev->asic_prop.cpucp_info.card_type);
Oded Gabbay58361aa2020-08-08 23:34:47 +03007974
Koby Elbazcd5def82021-02-23 21:31:27 +02007975 set_default_power_values(hdev);
Oded Gabbay58361aa2020-08-08 23:34:47 +03007976
7977 hdev->max_power = prop->max_power_default;
7978
Oded Gabbayac0ae6a2020-05-11 10:29:27 +03007979 return 0;
7980}
7981
Ohad Sharabicf303392021-01-17 16:01:56 +02007982static bool gaudi_is_device_idle(struct hl_device *hdev, u64 *mask_arr,
7983 u8 mask_len, struct seq_file *s)
Oded Gabbayac0ae6a2020-05-11 10:29:27 +03007984{
7985 struct gaudi_device *gaudi = hdev->asic_specific;
7986 const char *fmt = "%-5d%-9s%#-14x%#-12x%#x\n";
7987 const char *mme_slave_fmt = "%-5d%-9s%-14s%-12s%#x\n";
Oded Gabbay3c681572020-11-02 21:10:39 +02007988 const char *nic_fmt = "%-5d%-9s%#-14x%#x\n";
Ohad Sharabicf303392021-01-17 16:01:56 +02007989 unsigned long *mask = (unsigned long *)mask_arr;
Oded Gabbayac0ae6a2020-05-11 10:29:27 +03007990 u32 qm_glbl_sts0, qm_cgm_sts, dma_core_sts0, tpc_cfg_sts, mme_arch_sts;
7991 bool is_idle = true, is_eng_idle, is_slave;
7992 u64 offset;
Oded Gabbay3c681572020-11-02 21:10:39 +02007993 int i, dma_id, port;
Oded Gabbayac0ae6a2020-05-11 10:29:27 +03007994
7995 mutex_lock(&gaudi->clk_gate_mutex);
7996
7997 hdev->asic_funcs->disable_clock_gating(hdev);
7998
7999 if (s)
8000 seq_puts(s,
8001 "\nDMA is_idle QM_GLBL_STS0 QM_CGM_STS DMA_CORE_STS0\n"
8002 "--- ------- ------------ ---------- -------------\n");
8003
8004 for (i = 0 ; i < DMA_NUMBER_OF_CHNLS ; i++) {
8005 dma_id = gaudi_dma_assignment[i];
8006 offset = dma_id * DMA_QMAN_OFFSET;
8007
8008 qm_glbl_sts0 = RREG32(mmDMA0_QM_GLBL_STS0 + offset);
8009 qm_cgm_sts = RREG32(mmDMA0_QM_CGM_STS + offset);
8010 dma_core_sts0 = RREG32(mmDMA0_CORE_STS0 + offset);
8011 is_eng_idle = IS_QM_IDLE(qm_glbl_sts0, qm_cgm_sts) &&
8012 IS_DMA_IDLE(dma_core_sts0);
8013 is_idle &= is_eng_idle;
8014
Ohad Sharabicf303392021-01-17 16:01:56 +02008015 if (mask && !is_eng_idle)
8016 set_bit(GAUDI_ENGINE_ID_DMA_0 + dma_id, mask);
Oded Gabbayac0ae6a2020-05-11 10:29:27 +03008017 if (s)
8018 seq_printf(s, fmt, dma_id,
8019 is_eng_idle ? "Y" : "N", qm_glbl_sts0,
8020 qm_cgm_sts, dma_core_sts0);
8021 }
8022
8023 if (s)
8024 seq_puts(s,
8025 "\nTPC is_idle QM_GLBL_STS0 QM_CGM_STS CFG_STATUS\n"
8026 "--- ------- ------------ ---------- ----------\n");
8027
8028 for (i = 0 ; i < TPC_NUMBER_OF_ENGINES ; i++) {
8029 offset = i * TPC_QMAN_OFFSET;
8030 qm_glbl_sts0 = RREG32(mmTPC0_QM_GLBL_STS0 + offset);
8031 qm_cgm_sts = RREG32(mmTPC0_QM_CGM_STS + offset);
8032 tpc_cfg_sts = RREG32(mmTPC0_CFG_STATUS + offset);
8033 is_eng_idle = IS_QM_IDLE(qm_glbl_sts0, qm_cgm_sts) &&
8034 IS_TPC_IDLE(tpc_cfg_sts);
8035 is_idle &= is_eng_idle;
8036
Ohad Sharabicf303392021-01-17 16:01:56 +02008037 if (mask && !is_eng_idle)
8038 set_bit(GAUDI_ENGINE_ID_TPC_0 + i, mask);
Oded Gabbayac0ae6a2020-05-11 10:29:27 +03008039 if (s)
8040 seq_printf(s, fmt, i,
8041 is_eng_idle ? "Y" : "N",
8042 qm_glbl_sts0, qm_cgm_sts, tpc_cfg_sts);
8043 }
8044
8045 if (s)
8046 seq_puts(s,
8047 "\nMME is_idle QM_GLBL_STS0 QM_CGM_STS ARCH_STATUS\n"
8048 "--- ------- ------------ ---------- -----------\n");
8049
8050 for (i = 0 ; i < MME_NUMBER_OF_ENGINES ; i++) {
8051 offset = i * MME_QMAN_OFFSET;
8052 mme_arch_sts = RREG32(mmMME0_CTRL_ARCH_STATUS + offset);
8053 is_eng_idle = IS_MME_IDLE(mme_arch_sts);
8054
8055 /* MME 1 & 3 are slaves, no need to check their QMANs */
8056 is_slave = i % 2;
8057 if (!is_slave) {
8058 qm_glbl_sts0 = RREG32(mmMME0_QM_GLBL_STS0 + offset);
8059 qm_cgm_sts = RREG32(mmMME0_QM_CGM_STS + offset);
8060 is_eng_idle &= IS_QM_IDLE(qm_glbl_sts0, qm_cgm_sts);
8061 }
8062
8063 is_idle &= is_eng_idle;
8064
Ohad Sharabicf303392021-01-17 16:01:56 +02008065 if (mask && !is_eng_idle)
8066 set_bit(GAUDI_ENGINE_ID_MME_0 + i, mask);
Oded Gabbayac0ae6a2020-05-11 10:29:27 +03008067 if (s) {
8068 if (!is_slave)
8069 seq_printf(s, fmt, i,
8070 is_eng_idle ? "Y" : "N",
8071 qm_glbl_sts0, qm_cgm_sts, mme_arch_sts);
8072 else
8073 seq_printf(s, mme_slave_fmt, i,
8074 is_eng_idle ? "Y" : "N", "-",
8075 "-", mme_arch_sts);
8076 }
8077 }
8078
8079 if (s)
Oded Gabbay3c681572020-11-02 21:10:39 +02008080 seq_puts(s, "\nNIC is_idle QM_GLBL_STS0 QM_CGM_STS\n"
8081 "--- ------- ------------ ----------\n");
8082
8083 for (i = 0 ; i < (NIC_NUMBER_OF_ENGINES / 2) ; i++) {
8084 offset = i * NIC_MACRO_QMAN_OFFSET;
8085 port = 2 * i;
8086 if (hdev->nic_ports_mask & BIT(port)) {
8087 qm_glbl_sts0 = RREG32(mmNIC0_QM0_GLBL_STS0 + offset);
8088 qm_cgm_sts = RREG32(mmNIC0_QM0_CGM_STS + offset);
8089 is_eng_idle = IS_QM_IDLE(qm_glbl_sts0, qm_cgm_sts);
8090 is_idle &= is_eng_idle;
8091
Ohad Sharabicf303392021-01-17 16:01:56 +02008092 if (mask && !is_eng_idle)
8093 set_bit(GAUDI_ENGINE_ID_NIC_0 + port, mask);
Oded Gabbay3c681572020-11-02 21:10:39 +02008094 if (s)
8095 seq_printf(s, nic_fmt, port,
8096 is_eng_idle ? "Y" : "N",
8097 qm_glbl_sts0, qm_cgm_sts);
8098 }
8099
8100 port = 2 * i + 1;
8101 if (hdev->nic_ports_mask & BIT(port)) {
8102 qm_glbl_sts0 = RREG32(mmNIC0_QM1_GLBL_STS0 + offset);
8103 qm_cgm_sts = RREG32(mmNIC0_QM1_CGM_STS + offset);
8104 is_eng_idle = IS_QM_IDLE(qm_glbl_sts0, qm_cgm_sts);
8105 is_idle &= is_eng_idle;
8106
Ohad Sharabicf303392021-01-17 16:01:56 +02008107 if (mask && !is_eng_idle)
8108 set_bit(GAUDI_ENGINE_ID_NIC_0 + port, mask);
Oded Gabbay3c681572020-11-02 21:10:39 +02008109 if (s)
8110 seq_printf(s, nic_fmt, port,
8111 is_eng_idle ? "Y" : "N",
8112 qm_glbl_sts0, qm_cgm_sts);
8113 }
8114 }
8115
8116 if (s)
Oded Gabbayac0ae6a2020-05-11 10:29:27 +03008117 seq_puts(s, "\n");
8118
Oded Gabbaye38bfd32020-07-03 20:46:12 +03008119 hdev->asic_funcs->set_clock_gating(hdev);
Oded Gabbayac0ae6a2020-05-11 10:29:27 +03008120
8121 mutex_unlock(&gaudi->clk_gate_mutex);
8122
8123 return is_idle;
8124}
8125
8126static void gaudi_hw_queues_lock(struct hl_device *hdev)
8127 __acquires(&gaudi->hw_queues_lock)
8128{
8129 struct gaudi_device *gaudi = hdev->asic_specific;
8130
8131 spin_lock(&gaudi->hw_queues_lock);
8132}
8133
8134static void gaudi_hw_queues_unlock(struct hl_device *hdev)
8135 __releases(&gaudi->hw_queues_lock)
8136{
8137 struct gaudi_device *gaudi = hdev->asic_specific;
8138
8139 spin_unlock(&gaudi->hw_queues_lock);
8140}
8141
8142static u32 gaudi_get_pci_id(struct hl_device *hdev)
8143{
8144 return hdev->pdev->device;
8145}
8146
8147static int gaudi_get_eeprom_data(struct hl_device *hdev, void *data,
8148 size_t max_size)
8149{
8150 struct gaudi_device *gaudi = hdev->asic_specific;
8151
8152 if (!(gaudi->hw_cap_initialized & HW_CAP_CPU_Q))
8153 return 0;
8154
8155 return hl_fw_get_eeprom_data(hdev, data, max_size);
8156}
8157
8158/*
8159 * this function should be used only during initialization and/or after reset,
8160 * when there are no active users.
8161 */
8162static int gaudi_run_tpc_kernel(struct hl_device *hdev, u64 tpc_kernel,
8163 u32 tpc_id)
8164{
8165 struct gaudi_device *gaudi = hdev->asic_specific;
8166 u64 kernel_timeout;
8167 u32 status, offset;
8168 int rc;
8169
8170 offset = tpc_id * (mmTPC1_CFG_STATUS - mmTPC0_CFG_STATUS);
8171
8172 if (hdev->pldm)
8173 kernel_timeout = GAUDI_PLDM_TPC_KERNEL_WAIT_USEC;
8174 else
8175 kernel_timeout = HL_DEVICE_TIMEOUT_USEC;
8176
8177 mutex_lock(&gaudi->clk_gate_mutex);
8178
8179 hdev->asic_funcs->disable_clock_gating(hdev);
8180
8181 WREG32(mmTPC0_CFG_QM_KERNEL_BASE_ADDRESS_LOW + offset,
8182 lower_32_bits(tpc_kernel));
8183 WREG32(mmTPC0_CFG_QM_KERNEL_BASE_ADDRESS_HIGH + offset,
8184 upper_32_bits(tpc_kernel));
8185
8186 WREG32(mmTPC0_CFG_ICACHE_BASE_ADDERESS_LOW + offset,
8187 lower_32_bits(tpc_kernel));
8188 WREG32(mmTPC0_CFG_ICACHE_BASE_ADDERESS_HIGH + offset,
8189 upper_32_bits(tpc_kernel));
8190 /* set a valid LUT pointer, content is of no significance */
8191 WREG32(mmTPC0_CFG_LUT_FUNC256_BASE_ADDR_LO + offset,
8192 lower_32_bits(tpc_kernel));
8193 WREG32(mmTPC0_CFG_LUT_FUNC256_BASE_ADDR_HI + offset,
8194 upper_32_bits(tpc_kernel));
8195
8196 WREG32(mmTPC0_CFG_QM_SYNC_OBJECT_ADDR + offset,
8197 lower_32_bits(CFG_BASE +
8198 mmSYNC_MNGR_E_N_SYNC_MNGR_OBJS_SOB_OBJ_0));
8199
8200 WREG32(mmTPC0_CFG_TPC_CMD + offset,
8201 (1 << TPC0_CFG_TPC_CMD_ICACHE_INVALIDATE_SHIFT |
8202 1 << TPC0_CFG_TPC_CMD_ICACHE_PREFETCH_64KB_SHIFT));
8203 /* wait a bit for the engine to start executing */
8204 usleep_range(1000, 1500);
8205
8206 /* wait until engine has finished executing */
8207 rc = hl_poll_timeout(
8208 hdev,
8209 mmTPC0_CFG_STATUS + offset,
8210 status,
8211 (status & TPC0_CFG_STATUS_VECTOR_PIPE_EMPTY_MASK) ==
8212 TPC0_CFG_STATUS_VECTOR_PIPE_EMPTY_MASK,
8213 1000,
8214 kernel_timeout);
8215
8216 if (rc) {
8217 dev_err(hdev->dev,
8218 "Timeout while waiting for TPC%d icache prefetch\n",
8219 tpc_id);
Oded Gabbaye38bfd32020-07-03 20:46:12 +03008220 hdev->asic_funcs->set_clock_gating(hdev);
Oded Gabbayac0ae6a2020-05-11 10:29:27 +03008221 mutex_unlock(&gaudi->clk_gate_mutex);
8222 return -EIO;
8223 }
8224
8225 WREG32(mmTPC0_CFG_TPC_EXECUTE + offset,
8226 1 << TPC0_CFG_TPC_EXECUTE_V_SHIFT);
8227
8228 /* wait a bit for the engine to start executing */
8229 usleep_range(1000, 1500);
8230
8231 /* wait until engine has finished executing */
8232 rc = hl_poll_timeout(
8233 hdev,
8234 mmTPC0_CFG_STATUS + offset,
8235 status,
8236 (status & TPC0_CFG_STATUS_VECTOR_PIPE_EMPTY_MASK) ==
8237 TPC0_CFG_STATUS_VECTOR_PIPE_EMPTY_MASK,
8238 1000,
8239 kernel_timeout);
8240
Oded Gabbay31ac1f12020-08-12 11:28:13 +03008241 if (rc) {
8242 dev_err(hdev->dev,
8243 "Timeout while waiting for TPC%d vector pipe\n",
8244 tpc_id);
8245 hdev->asic_funcs->set_clock_gating(hdev);
8246 mutex_unlock(&gaudi->clk_gate_mutex);
8247 return -EIO;
8248 }
8249
Oded Gabbayac0ae6a2020-05-11 10:29:27 +03008250 rc = hl_poll_timeout(
8251 hdev,
8252 mmTPC0_CFG_WQ_INFLIGHT_CNTR + offset,
8253 status,
8254 (status == 0),
8255 1000,
8256 kernel_timeout);
8257
Oded Gabbaye38bfd32020-07-03 20:46:12 +03008258 hdev->asic_funcs->set_clock_gating(hdev);
Oded Gabbayac0ae6a2020-05-11 10:29:27 +03008259 mutex_unlock(&gaudi->clk_gate_mutex);
8260
8261 if (rc) {
8262 dev_err(hdev->dev,
8263 "Timeout while waiting for TPC%d kernel to execute\n",
8264 tpc_id);
8265 return -EIO;
8266 }
8267
8268 return 0;
8269}
8270
Ofir Bitton5de406c2020-09-10 10:56:26 +03008271static int gaudi_internal_cb_pool_init(struct hl_device *hdev,
8272 struct hl_ctx *ctx)
8273{
8274 struct gaudi_device *gaudi = hdev->asic_specific;
Ofir Bitton5de406c2020-09-10 10:56:26 +03008275 int min_alloc_order, rc, collective_cb_size;
8276
8277 if (!(gaudi->hw_cap_initialized & HW_CAP_MMU))
8278 return 0;
8279
8280 hdev->internal_cb_pool_virt_addr =
8281 hdev->asic_funcs->asic_dma_alloc_coherent(hdev,
8282 HOST_SPACE_INTERNAL_CB_SZ,
8283 &hdev->internal_cb_pool_dma_addr,
8284 GFP_KERNEL | __GFP_ZERO);
8285
8286 if (!hdev->internal_cb_pool_virt_addr)
8287 return -ENOMEM;
8288
8289 collective_cb_size = sizeof(struct packet_msg_short) * 5 +
8290 sizeof(struct packet_fence);
8291 min_alloc_order = ilog2(collective_cb_size);
8292
8293 hdev->internal_cb_pool = gen_pool_create(min_alloc_order, -1);
8294 if (!hdev->internal_cb_pool) {
8295 dev_err(hdev->dev,
8296 "Failed to create internal CB pool\n");
8297 rc = -ENOMEM;
8298 goto free_internal_cb_pool;
8299 }
8300
8301 rc = gen_pool_add(hdev->internal_cb_pool,
8302 (uintptr_t) hdev->internal_cb_pool_virt_addr,
8303 HOST_SPACE_INTERNAL_CB_SZ, -1);
8304 if (rc) {
8305 dev_err(hdev->dev,
8306 "Failed to add memory to internal CB pool\n");
8307 rc = -EFAULT;
8308 goto destroy_internal_cb_pool;
8309 }
8310
Ofir Bittonbe91b912020-10-22 15:04:10 +03008311 hdev->internal_cb_va_base = hl_reserve_va_block(hdev, ctx,
Ofir Bitton412c41f2020-11-04 15:18:55 +02008312 HL_VA_RANGE_TYPE_HOST, HOST_SPACE_INTERNAL_CB_SZ,
8313 HL_MMU_VA_ALIGNMENT_NOT_NEEDED);
Ofir Bittonbe91b912020-10-22 15:04:10 +03008314
8315 if (!hdev->internal_cb_va_base)
8316 goto destroy_internal_cb_pool;
Ofir Bitton5de406c2020-09-10 10:56:26 +03008317
8318 mutex_lock(&ctx->mmu_lock);
Ofir Bitton5c054872020-10-22 15:13:10 +03008319 rc = hl_mmu_map_contiguous(ctx, hdev->internal_cb_va_base,
8320 hdev->internal_cb_pool_dma_addr,
8321 HOST_SPACE_INTERNAL_CB_SZ);
Ofir Bitton5de406c2020-09-10 10:56:26 +03008322
8323 hdev->asic_funcs->mmu_invalidate_cache(hdev, false, VM_TYPE_USERPTR);
Ofir Bitton5de406c2020-09-10 10:56:26 +03008324 mutex_unlock(&ctx->mmu_lock);
8325
Ofir Bitton5c054872020-10-22 15:13:10 +03008326 if (rc)
8327 goto unreserve_internal_cb_pool;
8328
Ofir Bitton5de406c2020-09-10 10:56:26 +03008329 return 0;
8330
Ofir Bitton5c054872020-10-22 15:13:10 +03008331unreserve_internal_cb_pool:
Ofir Bittonbe91b912020-10-22 15:04:10 +03008332 hl_unreserve_va_block(hdev, ctx, hdev->internal_cb_va_base,
8333 HOST_SPACE_INTERNAL_CB_SZ);
Ofir Bitton5de406c2020-09-10 10:56:26 +03008334destroy_internal_cb_pool:
8335 gen_pool_destroy(hdev->internal_cb_pool);
Ofir Bitton5de406c2020-09-10 10:56:26 +03008336free_internal_cb_pool:
8337 hdev->asic_funcs->asic_dma_free_coherent(hdev,
8338 HOST_SPACE_INTERNAL_CB_SZ,
8339 hdev->internal_cb_pool_virt_addr,
8340 hdev->internal_cb_pool_dma_addr);
8341
8342 return rc;
8343}
8344
8345static void gaudi_internal_cb_pool_fini(struct hl_device *hdev,
8346 struct hl_ctx *ctx)
8347{
8348 struct gaudi_device *gaudi = hdev->asic_specific;
Ofir Bitton5de406c2020-09-10 10:56:26 +03008349
8350 if (!(gaudi->hw_cap_initialized & HW_CAP_MMU))
8351 return;
8352
8353 mutex_lock(&ctx->mmu_lock);
Ofir Bitton5c054872020-10-22 15:13:10 +03008354 hl_mmu_unmap_contiguous(ctx, hdev->internal_cb_va_base,
8355 HOST_SPACE_INTERNAL_CB_SZ);
Ofir Bittonbe91b912020-10-22 15:04:10 +03008356 hl_unreserve_va_block(hdev, ctx, hdev->internal_cb_va_base,
8357 HOST_SPACE_INTERNAL_CB_SZ);
Ofir Bitton5de406c2020-09-10 10:56:26 +03008358 hdev->asic_funcs->mmu_invalidate_cache(hdev, true, VM_TYPE_USERPTR);
Ofir Bitton5de406c2020-09-10 10:56:26 +03008359 mutex_unlock(&ctx->mmu_lock);
8360
8361 gen_pool_destroy(hdev->internal_cb_pool);
8362
8363 hdev->asic_funcs->asic_dma_free_coherent(hdev,
8364 HOST_SPACE_INTERNAL_CB_SZ,
8365 hdev->internal_cb_pool_virt_addr,
8366 hdev->internal_cb_pool_dma_addr);
8367}
8368
kernel test robotbb34bf72020-07-29 08:03:13 +08008369static int gaudi_ctx_init(struct hl_ctx *ctx)
Ofir Bittona04b7cd2020-07-13 13:36:55 +03008370{
Ofir Bitton8e39e752020-11-12 11:03:32 +02008371 if (ctx->asid == HL_KERNEL_ASID_ID)
8372 return 0;
8373
Ofir Bitton20b75252020-09-30 15:51:10 +03008374 gaudi_mmu_prepare(ctx->hdev, ctx->asid);
Ofir Bitton5de406c2020-09-10 10:56:26 +03008375 return gaudi_internal_cb_pool_init(ctx->hdev, ctx);
8376}
Ofir Bitton20b75252020-09-30 15:51:10 +03008377
kernel test robot293744d2020-11-19 12:25:43 +08008378static void gaudi_ctx_fini(struct hl_ctx *ctx)
Ofir Bitton5de406c2020-09-10 10:56:26 +03008379{
Ofir Bitton8e39e752020-11-12 11:03:32 +02008380 if (ctx->asid == HL_KERNEL_ASID_ID)
Ofir Bitton5de406c2020-09-10 10:56:26 +03008381 return;
8382
8383 gaudi_internal_cb_pool_fini(ctx->hdev, ctx);
Ofir Bittona04b7cd2020-07-13 13:36:55 +03008384}
8385
Oded Gabbayac0ae6a2020-05-11 10:29:27 +03008386static u32 gaudi_get_queue_id_for_cq(struct hl_device *hdev, u32 cq_idx)
8387{
8388 return gaudi_cq_assignment[cq_idx];
8389}
8390
Oded Gabbayac0ae6a2020-05-11 10:29:27 +03008391static u32 gaudi_get_signal_cb_size(struct hl_device *hdev)
8392{
8393 return sizeof(struct packet_msg_short) +
8394 sizeof(struct packet_msg_prot) * 2;
8395}
8396
8397static u32 gaudi_get_wait_cb_size(struct hl_device *hdev)
8398{
8399 return sizeof(struct packet_msg_short) * 4 +
8400 sizeof(struct packet_fence) +
8401 sizeof(struct packet_msg_prot) * 2;
8402}
8403
Ofir Bitton2992c1d2020-09-10 09:40:35 +03008404static u32 gaudi_gen_signal_cb(struct hl_device *hdev, void *data, u16 sob_id,
Alon Mizrahi72ab9ca52020-12-02 19:55:30 +02008405 u32 size, bool eb)
Oded Gabbayac0ae6a2020-05-11 10:29:27 +03008406{
8407 struct hl_cb *cb = (struct hl_cb *) data;
8408 struct packet_msg_short *pkt;
Ofir Bitton2992c1d2020-09-10 09:40:35 +03008409 u32 value, ctl, pkt_size = sizeof(*pkt);
Oded Gabbayac0ae6a2020-05-11 10:29:27 +03008410
Ofir Bitton2992c1d2020-09-10 09:40:35 +03008411 pkt = cb->kernel_address + size;
8412 memset(pkt, 0, pkt_size);
Oded Gabbayac0ae6a2020-05-11 10:29:27 +03008413
Ofir Bitton6c07bab2020-06-01 10:38:46 +03008414 /* Inc by 1, Mode ADD */
8415 value = FIELD_PREP(GAUDI_PKT_SHORT_VAL_SOB_SYNC_VAL_MASK, 1);
8416 value |= FIELD_PREP(GAUDI_PKT_SHORT_VAL_SOB_MOD_MASK, 1);
Oded Gabbayac0ae6a2020-05-11 10:29:27 +03008417
Ofir Bitton6c07bab2020-06-01 10:38:46 +03008418 ctl = FIELD_PREP(GAUDI_PKT_SHORT_CTL_ADDR_MASK, sob_id * 4);
8419 ctl |= FIELD_PREP(GAUDI_PKT_SHORT_CTL_OP_MASK, 0); /* write the value */
8420 ctl |= FIELD_PREP(GAUDI_PKT_SHORT_CTL_BASE_MASK, 3); /* W_S SOB base */
Ofir Bittonf8b0f2e2020-12-06 10:22:32 +02008421 ctl |= FIELD_PREP(GAUDI_PKT_CTL_OPCODE_MASK, PACKET_MSG_SHORT);
8422 ctl |= FIELD_PREP(GAUDI_PKT_CTL_EB_MASK, eb);
8423 ctl |= FIELD_PREP(GAUDI_PKT_CTL_RB_MASK, 1);
8424 ctl |= FIELD_PREP(GAUDI_PKT_CTL_MB_MASK, 1);
Oded Gabbayac0ae6a2020-05-11 10:29:27 +03008425
8426 pkt->value = cpu_to_le32(value);
8427 pkt->ctl = cpu_to_le32(ctl);
Ofir Bitton2992c1d2020-09-10 09:40:35 +03008428
8429 return size + pkt_size;
Oded Gabbayac0ae6a2020-05-11 10:29:27 +03008430}
8431
8432static u32 gaudi_add_mon_msg_short(struct packet_msg_short *pkt, u32 value,
8433 u16 addr)
8434{
8435 u32 ctl, pkt_size = sizeof(*pkt);
8436
8437 memset(pkt, 0, pkt_size);
8438
Ofir Bitton6c07bab2020-06-01 10:38:46 +03008439 ctl = FIELD_PREP(GAUDI_PKT_SHORT_CTL_ADDR_MASK, addr);
8440 ctl |= FIELD_PREP(GAUDI_PKT_SHORT_CTL_BASE_MASK, 2); /* W_S MON base */
Ofir Bittonf8b0f2e2020-12-06 10:22:32 +02008441 ctl |= FIELD_PREP(GAUDI_PKT_CTL_OPCODE_MASK, PACKET_MSG_SHORT);
8442 ctl |= FIELD_PREP(GAUDI_PKT_CTL_EB_MASK, 0);
8443 ctl |= FIELD_PREP(GAUDI_PKT_CTL_RB_MASK, 1);
8444 ctl |= FIELD_PREP(GAUDI_PKT_CTL_MB_MASK, 0); /* last pkt MB */
Oded Gabbayac0ae6a2020-05-11 10:29:27 +03008445
8446 pkt->value = cpu_to_le32(value);
8447 pkt->ctl = cpu_to_le32(ctl);
8448
8449 return pkt_size;
8450}
8451
Ofir Bitton2992c1d2020-09-10 09:40:35 +03008452static u32 gaudi_add_arm_monitor_pkt(struct hl_device *hdev,
8453 struct packet_msg_short *pkt, u16 sob_base, u8 sob_mask,
8454 u16 sob_val, u16 mon_id)
Oded Gabbayac0ae6a2020-05-11 10:29:27 +03008455{
Ofir Bitton2992c1d2020-09-10 09:40:35 +03008456 u64 monitor_base;
Oded Gabbayac0ae6a2020-05-11 10:29:27 +03008457 u32 ctl, value, pkt_size = sizeof(*pkt);
Ofir Bitton2992c1d2020-09-10 09:40:35 +03008458 u16 msg_addr_offset;
8459 u8 mask;
8460
8461 if (hl_gen_sob_mask(sob_base, sob_mask, &mask)) {
8462 dev_err(hdev->dev,
8463 "sob_base %u (mask %#x) is not valid\n",
8464 sob_base, sob_mask);
8465 return 0;
8466 }
8467
8468 /*
8469 * monitor_base should be the content of the base0 address registers,
8470 * so it will be added to the msg short offsets
8471 */
8472 monitor_base = mmSYNC_MNGR_W_S_SYNC_MNGR_OBJS_MON_PAY_ADDRL_0;
8473
8474 msg_addr_offset =
8475 (mmSYNC_MNGR_W_S_SYNC_MNGR_OBJS_MON_ARM_0 + mon_id * 4) -
8476 monitor_base;
Oded Gabbayac0ae6a2020-05-11 10:29:27 +03008477
8478 memset(pkt, 0, pkt_size);
8479
Ofir Bitton2992c1d2020-09-10 09:40:35 +03008480 /* Monitor config packet: bind the monitor to a sync object */
8481 value = FIELD_PREP(GAUDI_PKT_SHORT_VAL_MON_SYNC_GID_MASK, sob_base / 8);
Ofir Bitton6c07bab2020-06-01 10:38:46 +03008482 value |= FIELD_PREP(GAUDI_PKT_SHORT_VAL_MON_SYNC_VAL_MASK, sob_val);
8483 value |= FIELD_PREP(GAUDI_PKT_SHORT_VAL_MON_MODE_MASK,
8484 0); /* GREATER OR EQUAL*/
8485 value |= FIELD_PREP(GAUDI_PKT_SHORT_VAL_MON_MASK_MASK, mask);
Oded Gabbayac0ae6a2020-05-11 10:29:27 +03008486
Ofir Bitton2992c1d2020-09-10 09:40:35 +03008487 ctl = FIELD_PREP(GAUDI_PKT_SHORT_CTL_ADDR_MASK, msg_addr_offset);
Ofir Bitton6c07bab2020-06-01 10:38:46 +03008488 ctl |= FIELD_PREP(GAUDI_PKT_SHORT_CTL_OP_MASK, 0); /* write the value */
8489 ctl |= FIELD_PREP(GAUDI_PKT_SHORT_CTL_BASE_MASK, 2); /* W_S MON base */
Ofir Bittonf8b0f2e2020-12-06 10:22:32 +02008490 ctl |= FIELD_PREP(GAUDI_PKT_CTL_OPCODE_MASK, PACKET_MSG_SHORT);
8491 ctl |= FIELD_PREP(GAUDI_PKT_CTL_EB_MASK, 0);
8492 ctl |= FIELD_PREP(GAUDI_PKT_CTL_RB_MASK, 1);
8493 ctl |= FIELD_PREP(GAUDI_PKT_CTL_MB_MASK, 1);
Oded Gabbayac0ae6a2020-05-11 10:29:27 +03008494
8495 pkt->value = cpu_to_le32(value);
8496 pkt->ctl = cpu_to_le32(ctl);
8497
8498 return pkt_size;
8499}
8500
8501static u32 gaudi_add_fence_pkt(struct packet_fence *pkt)
8502{
8503 u32 ctl, cfg, pkt_size = sizeof(*pkt);
8504
8505 memset(pkt, 0, pkt_size);
8506
Ofir Bitton6c07bab2020-06-01 10:38:46 +03008507 cfg = FIELD_PREP(GAUDI_PKT_FENCE_CFG_DEC_VAL_MASK, 1);
8508 cfg |= FIELD_PREP(GAUDI_PKT_FENCE_CFG_TARGET_VAL_MASK, 1);
8509 cfg |= FIELD_PREP(GAUDI_PKT_FENCE_CFG_ID_MASK, 2);
Oded Gabbayac0ae6a2020-05-11 10:29:27 +03008510
Ofir Bittonf8b0f2e2020-12-06 10:22:32 +02008511 ctl = FIELD_PREP(GAUDI_PKT_CTL_OPCODE_MASK, PACKET_FENCE);
8512 ctl |= FIELD_PREP(GAUDI_PKT_CTL_EB_MASK, 0);
8513 ctl |= FIELD_PREP(GAUDI_PKT_CTL_RB_MASK, 1);
8514 ctl |= FIELD_PREP(GAUDI_PKT_CTL_MB_MASK, 1);
Oded Gabbayac0ae6a2020-05-11 10:29:27 +03008515
8516 pkt->cfg = cpu_to_le32(cfg);
8517 pkt->ctl = cpu_to_le32(ctl);
8518
8519 return pkt_size;
8520}
8521
Ofir Bitton2992c1d2020-09-10 09:40:35 +03008522static int gaudi_get_fence_addr(struct hl_device *hdev, u32 queue_id, u64 *addr)
Oded Gabbayac0ae6a2020-05-11 10:29:27 +03008523{
Ofir Bitton5de406c2020-09-10 10:56:26 +03008524 u32 offset, nic_index;
Oded Gabbayac0ae6a2020-05-11 10:29:27 +03008525
Ofir Bitton2992c1d2020-09-10 09:40:35 +03008526 switch (queue_id) {
Oded Gabbayac0ae6a2020-05-11 10:29:27 +03008527 case GAUDI_QUEUE_ID_DMA_0_0:
Ofir Bitton2992c1d2020-09-10 09:40:35 +03008528 offset = mmDMA0_QM_CP_FENCE2_RDATA_0;
Oded Gabbayac0ae6a2020-05-11 10:29:27 +03008529 break;
8530 case GAUDI_QUEUE_ID_DMA_0_1:
Ofir Bitton2992c1d2020-09-10 09:40:35 +03008531 offset = mmDMA0_QM_CP_FENCE2_RDATA_1;
Oded Gabbayac0ae6a2020-05-11 10:29:27 +03008532 break;
8533 case GAUDI_QUEUE_ID_DMA_0_2:
Ofir Bitton2992c1d2020-09-10 09:40:35 +03008534 offset = mmDMA0_QM_CP_FENCE2_RDATA_2;
Oded Gabbayac0ae6a2020-05-11 10:29:27 +03008535 break;
8536 case GAUDI_QUEUE_ID_DMA_0_3:
Ofir Bitton2992c1d2020-09-10 09:40:35 +03008537 offset = mmDMA0_QM_CP_FENCE2_RDATA_3;
Oded Gabbayac0ae6a2020-05-11 10:29:27 +03008538 break;
8539 case GAUDI_QUEUE_ID_DMA_1_0:
Ofir Bitton2992c1d2020-09-10 09:40:35 +03008540 offset = mmDMA1_QM_CP_FENCE2_RDATA_0;
Oded Gabbayac0ae6a2020-05-11 10:29:27 +03008541 break;
8542 case GAUDI_QUEUE_ID_DMA_1_1:
Ofir Bitton2992c1d2020-09-10 09:40:35 +03008543 offset = mmDMA1_QM_CP_FENCE2_RDATA_1;
Oded Gabbayac0ae6a2020-05-11 10:29:27 +03008544 break;
8545 case GAUDI_QUEUE_ID_DMA_1_2:
Ofir Bitton2992c1d2020-09-10 09:40:35 +03008546 offset = mmDMA1_QM_CP_FENCE2_RDATA_2;
Oded Gabbayac0ae6a2020-05-11 10:29:27 +03008547 break;
8548 case GAUDI_QUEUE_ID_DMA_1_3:
Ofir Bitton2992c1d2020-09-10 09:40:35 +03008549 offset = mmDMA1_QM_CP_FENCE2_RDATA_3;
Oded Gabbayac0ae6a2020-05-11 10:29:27 +03008550 break;
8551 case GAUDI_QUEUE_ID_DMA_5_0:
Ofir Bitton2992c1d2020-09-10 09:40:35 +03008552 offset = mmDMA5_QM_CP_FENCE2_RDATA_0;
Oded Gabbayac0ae6a2020-05-11 10:29:27 +03008553 break;
8554 case GAUDI_QUEUE_ID_DMA_5_1:
Ofir Bitton2992c1d2020-09-10 09:40:35 +03008555 offset = mmDMA5_QM_CP_FENCE2_RDATA_1;
Oded Gabbayac0ae6a2020-05-11 10:29:27 +03008556 break;
8557 case GAUDI_QUEUE_ID_DMA_5_2:
Ofir Bitton2992c1d2020-09-10 09:40:35 +03008558 offset = mmDMA5_QM_CP_FENCE2_RDATA_2;
Oded Gabbayac0ae6a2020-05-11 10:29:27 +03008559 break;
8560 case GAUDI_QUEUE_ID_DMA_5_3:
Ofir Bitton2992c1d2020-09-10 09:40:35 +03008561 offset = mmDMA5_QM_CP_FENCE2_RDATA_3;
Oded Gabbayac0ae6a2020-05-11 10:29:27 +03008562 break;
Ofir Bitton5de406c2020-09-10 10:56:26 +03008563 case GAUDI_QUEUE_ID_TPC_7_0:
8564 offset = mmTPC7_QM_CP_FENCE2_RDATA_0;
8565 break;
8566 case GAUDI_QUEUE_ID_TPC_7_1:
8567 offset = mmTPC7_QM_CP_FENCE2_RDATA_1;
8568 break;
8569 case GAUDI_QUEUE_ID_TPC_7_2:
8570 offset = mmTPC7_QM_CP_FENCE2_RDATA_2;
8571 break;
8572 case GAUDI_QUEUE_ID_TPC_7_3:
8573 offset = mmTPC7_QM_CP_FENCE2_RDATA_3;
8574 break;
8575 case GAUDI_QUEUE_ID_NIC_0_0:
8576 case GAUDI_QUEUE_ID_NIC_1_0:
8577 case GAUDI_QUEUE_ID_NIC_2_0:
8578 case GAUDI_QUEUE_ID_NIC_3_0:
8579 case GAUDI_QUEUE_ID_NIC_4_0:
8580 case GAUDI_QUEUE_ID_NIC_5_0:
8581 case GAUDI_QUEUE_ID_NIC_6_0:
8582 case GAUDI_QUEUE_ID_NIC_7_0:
8583 case GAUDI_QUEUE_ID_NIC_8_0:
8584 case GAUDI_QUEUE_ID_NIC_9_0:
8585 nic_index = (queue_id - GAUDI_QUEUE_ID_NIC_0_0) >> 2;
8586 offset = mmNIC0_QM0_CP_FENCE2_RDATA_0 +
8587 (nic_index >> 1) * NIC_MACRO_QMAN_OFFSET +
8588 (nic_index & 0x1) * NIC_ENGINE_QMAN_OFFSET;
8589 break;
8590 case GAUDI_QUEUE_ID_NIC_0_1:
8591 case GAUDI_QUEUE_ID_NIC_1_1:
8592 case GAUDI_QUEUE_ID_NIC_2_1:
8593 case GAUDI_QUEUE_ID_NIC_3_1:
8594 case GAUDI_QUEUE_ID_NIC_4_1:
8595 case GAUDI_QUEUE_ID_NIC_5_1:
8596 case GAUDI_QUEUE_ID_NIC_6_1:
8597 case GAUDI_QUEUE_ID_NIC_7_1:
8598 case GAUDI_QUEUE_ID_NIC_8_1:
8599 case GAUDI_QUEUE_ID_NIC_9_1:
8600 nic_index = (queue_id - GAUDI_QUEUE_ID_NIC_0_1) >> 2;
8601 offset = mmNIC0_QM0_CP_FENCE2_RDATA_1 +
8602 (nic_index >> 1) * NIC_MACRO_QMAN_OFFSET +
8603 (nic_index & 0x1) * NIC_ENGINE_QMAN_OFFSET;
8604 break;
8605 case GAUDI_QUEUE_ID_NIC_0_2:
8606 case GAUDI_QUEUE_ID_NIC_1_2:
8607 case GAUDI_QUEUE_ID_NIC_2_2:
8608 case GAUDI_QUEUE_ID_NIC_3_2:
8609 case GAUDI_QUEUE_ID_NIC_4_2:
8610 case GAUDI_QUEUE_ID_NIC_5_2:
8611 case GAUDI_QUEUE_ID_NIC_6_2:
8612 case GAUDI_QUEUE_ID_NIC_7_2:
8613 case GAUDI_QUEUE_ID_NIC_8_2:
8614 case GAUDI_QUEUE_ID_NIC_9_2:
8615 nic_index = (queue_id - GAUDI_QUEUE_ID_NIC_0_2) >> 2;
8616 offset = mmNIC0_QM0_CP_FENCE2_RDATA_2 +
8617 (nic_index >> 1) * NIC_MACRO_QMAN_OFFSET +
8618 (nic_index & 0x1) * NIC_ENGINE_QMAN_OFFSET;
8619 break;
8620 case GAUDI_QUEUE_ID_NIC_0_3:
8621 case GAUDI_QUEUE_ID_NIC_1_3:
8622 case GAUDI_QUEUE_ID_NIC_2_3:
8623 case GAUDI_QUEUE_ID_NIC_3_3:
8624 case GAUDI_QUEUE_ID_NIC_4_3:
8625 case GAUDI_QUEUE_ID_NIC_5_3:
8626 case GAUDI_QUEUE_ID_NIC_6_3:
8627 case GAUDI_QUEUE_ID_NIC_7_3:
8628 case GAUDI_QUEUE_ID_NIC_8_3:
8629 case GAUDI_QUEUE_ID_NIC_9_3:
8630 nic_index = (queue_id - GAUDI_QUEUE_ID_NIC_0_3) >> 2;
8631 offset = mmNIC0_QM0_CP_FENCE2_RDATA_3 +
8632 (nic_index >> 1) * NIC_MACRO_QMAN_OFFSET +
8633 (nic_index & 0x1) * NIC_ENGINE_QMAN_OFFSET;
8634 break;
Oded Gabbayac0ae6a2020-05-11 10:29:27 +03008635 default:
Ofir Bitton2992c1d2020-09-10 09:40:35 +03008636 return -EINVAL;
Oded Gabbayac0ae6a2020-05-11 10:29:27 +03008637 }
8638
Ofir Bitton2992c1d2020-09-10 09:40:35 +03008639 *addr = CFG_BASE + offset;
8640
8641 return 0;
8642}
8643
8644static u32 gaudi_add_mon_pkts(void *buf, u16 mon_id, u64 fence_addr)
8645{
8646 u64 monitor_base;
8647 u32 size = 0;
8648 u16 msg_addr_offset;
Oded Gabbayac0ae6a2020-05-11 10:29:27 +03008649
8650 /*
8651 * monitor_base should be the content of the base0 address registers,
8652 * so it will be added to the msg short offsets
8653 */
8654 monitor_base = mmSYNC_MNGR_W_S_SYNC_MNGR_OBJS_MON_PAY_ADDRL_0;
8655
8656 /* First monitor config packet: low address of the sync */
Ofir Bitton2992c1d2020-09-10 09:40:35 +03008657 msg_addr_offset =
8658 (mmSYNC_MNGR_W_S_SYNC_MNGR_OBJS_MON_PAY_ADDRL_0 + mon_id * 4) -
8659 monitor_base;
Oded Gabbayac0ae6a2020-05-11 10:29:27 +03008660
8661 size += gaudi_add_mon_msg_short(buf + size, (u32) fence_addr,
8662 msg_addr_offset);
8663
8664 /* Second monitor config packet: high address of the sync */
Ofir Bitton2992c1d2020-09-10 09:40:35 +03008665 msg_addr_offset =
8666 (mmSYNC_MNGR_W_S_SYNC_MNGR_OBJS_MON_PAY_ADDRH_0 + mon_id * 4) -
8667 monitor_base;
Oded Gabbayac0ae6a2020-05-11 10:29:27 +03008668
8669 size += gaudi_add_mon_msg_short(buf + size, (u32) (fence_addr >> 32),
8670 msg_addr_offset);
8671
8672 /*
8673 * Third monitor config packet: the payload, i.e. what to write when the
8674 * sync triggers
8675 */
Ofir Bitton2992c1d2020-09-10 09:40:35 +03008676 msg_addr_offset =
8677 (mmSYNC_MNGR_W_S_SYNC_MNGR_OBJS_MON_PAY_DATA_0 + mon_id * 4) -
8678 monitor_base;
Oded Gabbayac0ae6a2020-05-11 10:29:27 +03008679
8680 size += gaudi_add_mon_msg_short(buf + size, 1, msg_addr_offset);
8681
Ofir Bitton2992c1d2020-09-10 09:40:35 +03008682 return size;
8683}
Oded Gabbayac0ae6a2020-05-11 10:29:27 +03008684
Oded Gabbay3c681572020-11-02 21:10:39 +02008685static u32 gaudi_gen_wait_cb(struct hl_device *hdev,
8686 struct hl_gen_wait_properties *prop)
Ofir Bitton2992c1d2020-09-10 09:40:35 +03008687{
8688 struct hl_cb *cb = (struct hl_cb *) prop->data;
8689 void *buf = cb->kernel_address;
8690 u64 fence_addr = 0;
8691 u32 size = prop->size;
8692
8693 if (gaudi_get_fence_addr(hdev, prop->q_idx, &fence_addr)) {
8694 dev_crit(hdev->dev, "wrong queue id %d for wait packet\n",
8695 prop->q_idx);
8696 return 0;
8697 }
8698
8699 size += gaudi_add_mon_pkts(buf + size, prop->mon_id, fence_addr);
8700 size += gaudi_add_arm_monitor_pkt(hdev, buf + size, prop->sob_base,
8701 prop->sob_mask, prop->sob_val, prop->mon_id);
Oded Gabbayac0ae6a2020-05-11 10:29:27 +03008702 size += gaudi_add_fence_pkt(buf + size);
Ofir Bitton2992c1d2020-09-10 09:40:35 +03008703
8704 return size;
Oded Gabbayac0ae6a2020-05-11 10:29:27 +03008705}
8706
8707static void gaudi_reset_sob(struct hl_device *hdev, void *data)
8708{
8709 struct hl_hw_sob *hw_sob = (struct hl_hw_sob *) data;
Ofir Bitton423815b2021-01-05 09:04:07 +02008710 int rc;
Oded Gabbayac0ae6a2020-05-11 10:29:27 +03008711
8712 dev_dbg(hdev->dev, "reset SOB, q_idx: %d, sob_id: %d\n", hw_sob->q_idx,
8713 hw_sob->sob_id);
8714
Ofir Bitton423815b2021-01-05 09:04:07 +02008715 rc = gaudi_schedule_register_memset(hdev, hw_sob->q_idx,
8716 CFG_BASE + mmSYNC_MNGR_W_S_SYNC_MNGR_OBJS_SOB_OBJ_0 +
8717 hw_sob->sob_id * 4, 1, 0);
8718 if (rc)
8719 dev_err(hdev->dev, "failed resetting sob %u", hw_sob->sob_id);
Oded Gabbayac0ae6a2020-05-11 10:29:27 +03008720
8721 kref_init(&hw_sob->kref);
8722}
8723
8724static void gaudi_set_dma_mask_from_fw(struct hl_device *hdev)
8725{
8726 if (RREG32(mmPSOC_GLOBAL_CONF_NON_RST_FLOPS_0) ==
8727 HL_POWER9_HOST_MAGIC) {
8728 hdev->power9_64bit_dma_enable = 1;
8729 hdev->dma_mask = 64;
8730 } else {
8731 hdev->power9_64bit_dma_enable = 0;
8732 hdev->dma_mask = 48;
8733 }
8734}
8735
8736static u64 gaudi_get_device_time(struct hl_device *hdev)
8737{
8738 u64 device_time = ((u64) RREG32(mmPSOC_TIMESTAMP_CNTCVU)) << 32;
8739
8740 return device_time | RREG32(mmPSOC_TIMESTAMP_CNTCVL);
8741}
8742
Ofir Bittond00697f2021-01-05 12:55:06 +02008743static int gaudi_get_hw_block_id(struct hl_device *hdev, u64 block_addr,
Oded Gabbay6df50d22021-02-05 16:04:34 +02008744 u32 *block_size, u32 *block_id)
Ofir Bittond00697f2021-01-05 12:55:06 +02008745{
8746 return -EPERM;
8747}
8748
8749static int gaudi_block_mmap(struct hl_device *hdev,
8750 struct vm_area_struct *vma,
8751 u32 block_id, u32 block_size)
8752{
8753 return -EPERM;
8754}
8755
Oded Gabbay28bcf1f2021-02-01 21:23:43 +02008756static void gaudi_enable_events_from_fw(struct hl_device *hdev)
8757{
8758 WREG32(mmGIC_DISTRIBUTOR__5_GICD_SETSPI_NSR, GAUDI_EVENT_INTS_REGISTER);
8759}
8760
Bharat Jauhari285c0fa2021-03-25 18:15:40 +02008761static int gaudi_map_pll_idx_to_fw_idx(u32 pll_idx)
8762{
8763 switch (pll_idx) {
8764 case HL_GAUDI_CPU_PLL: return CPU_PLL;
8765 case HL_GAUDI_PCI_PLL: return PCI_PLL;
8766 case HL_GAUDI_NIC_PLL: return NIC_PLL;
8767 case HL_GAUDI_DMA_PLL: return DMA_PLL;
8768 case HL_GAUDI_MESH_PLL: return MESH_PLL;
8769 case HL_GAUDI_MME_PLL: return MME_PLL;
8770 case HL_GAUDI_TPC_PLL: return TPC_PLL;
8771 case HL_GAUDI_IF_PLL: return IF_PLL;
8772 case HL_GAUDI_SRAM_PLL: return SRAM_PLL;
8773 case HL_GAUDI_HBM_PLL: return HBM_PLL;
8774 default: return -EINVAL;
8775 }
8776}
8777
Oded Gabbayac0ae6a2020-05-11 10:29:27 +03008778static const struct hl_asic_funcs gaudi_funcs = {
8779 .early_init = gaudi_early_init,
8780 .early_fini = gaudi_early_fini,
8781 .late_init = gaudi_late_init,
8782 .late_fini = gaudi_late_fini,
8783 .sw_init = gaudi_sw_init,
8784 .sw_fini = gaudi_sw_fini,
8785 .hw_init = gaudi_hw_init,
8786 .hw_fini = gaudi_hw_fini,
8787 .halt_engines = gaudi_halt_engines,
8788 .suspend = gaudi_suspend,
8789 .resume = gaudi_resume,
8790 .cb_mmap = gaudi_cb_mmap,
8791 .ring_doorbell = gaudi_ring_doorbell,
8792 .pqe_write = gaudi_pqe_write,
8793 .asic_dma_alloc_coherent = gaudi_dma_alloc_coherent,
8794 .asic_dma_free_coherent = gaudi_dma_free_coherent,
farah kassabri03df1362020-05-06 11:17:38 +03008795 .scrub_device_mem = gaudi_scrub_device_mem,
Oded Gabbayac0ae6a2020-05-11 10:29:27 +03008796 .get_int_queue_base = gaudi_get_int_queue_base,
8797 .test_queues = gaudi_test_queues,
8798 .asic_dma_pool_zalloc = gaudi_dma_pool_zalloc,
8799 .asic_dma_pool_free = gaudi_dma_pool_free,
8800 .cpu_accessible_dma_pool_alloc = gaudi_cpu_accessible_dma_pool_alloc,
8801 .cpu_accessible_dma_pool_free = gaudi_cpu_accessible_dma_pool_free,
8802 .hl_dma_unmap_sg = gaudi_dma_unmap_sg,
8803 .cs_parser = gaudi_cs_parser,
8804 .asic_dma_map_sg = gaudi_dma_map_sg,
8805 .get_dma_desc_list_size = gaudi_get_dma_desc_list_size,
8806 .add_end_of_cb_packets = gaudi_add_end_of_cb_packets,
8807 .update_eq_ci = gaudi_update_eq_ci,
8808 .context_switch = gaudi_context_switch,
8809 .restore_phase_topology = gaudi_restore_phase_topology,
8810 .debugfs_read32 = gaudi_debugfs_read32,
8811 .debugfs_write32 = gaudi_debugfs_write32,
8812 .debugfs_read64 = gaudi_debugfs_read64,
8813 .debugfs_write64 = gaudi_debugfs_write64,
Oded Gabbay639781d2021-04-02 01:43:18 +03008814 .debugfs_read_dma = gaudi_debugfs_read_dma,
Oded Gabbaybcaf4152020-05-11 10:41:37 +03008815 .add_device_attr = gaudi_add_device_attr,
Oded Gabbayac0ae6a2020-05-11 10:29:27 +03008816 .handle_eqe = gaudi_handle_eqe,
Oded Gabbaybcaf4152020-05-11 10:41:37 +03008817 .set_pll_profile = gaudi_set_pll_profile,
Oded Gabbayac0ae6a2020-05-11 10:29:27 +03008818 .get_events_stat = gaudi_get_events_stat,
8819 .read_pte = gaudi_read_pte,
8820 .write_pte = gaudi_write_pte,
8821 .mmu_invalidate_cache = gaudi_mmu_invalidate_cache,
8822 .mmu_invalidate_cache_range = gaudi_mmu_invalidate_cache_range,
8823 .send_heartbeat = gaudi_send_heartbeat,
Oded Gabbaye38bfd32020-07-03 20:46:12 +03008824 .set_clock_gating = gaudi_set_clock_gating,
Oded Gabbayac0ae6a2020-05-11 10:29:27 +03008825 .disable_clock_gating = gaudi_disable_clock_gating,
Omer Shpigelman79fc7a92020-05-11 10:46:29 +03008826 .debug_coresight = gaudi_debug_coresight,
Oded Gabbayac0ae6a2020-05-11 10:29:27 +03008827 .is_device_idle = gaudi_is_device_idle,
8828 .soft_reset_late_init = gaudi_soft_reset_late_init,
8829 .hw_queues_lock = gaudi_hw_queues_lock,
8830 .hw_queues_unlock = gaudi_hw_queues_unlock,
8831 .get_pci_id = gaudi_get_pci_id,
8832 .get_eeprom_data = gaudi_get_eeprom_data,
8833 .send_cpu_message = gaudi_send_cpu_message,
Oded Gabbayac0ae6a2020-05-11 10:29:27 +03008834 .pci_bars_map = gaudi_pci_bars_map,
Oded Gabbayac0ae6a2020-05-11 10:29:27 +03008835 .init_iatu = gaudi_init_iatu,
8836 .rreg = hl_rreg,
8837 .wreg = hl_wreg,
Omer Shpigelman79fc7a92020-05-11 10:46:29 +03008838 .halt_coresight = gaudi_halt_coresight,
Ofir Bittona04b7cd2020-07-13 13:36:55 +03008839 .ctx_init = gaudi_ctx_init,
Ofir Bitton5de406c2020-09-10 10:56:26 +03008840 .ctx_fini = gaudi_ctx_fini,
Oded Gabbaybcaf4152020-05-11 10:41:37 +03008841 .get_clk_rate = gaudi_get_clk_rate,
Oded Gabbayac0ae6a2020-05-11 10:29:27 +03008842 .get_queue_id_for_cq = gaudi_get_queue_id_for_cq,
8843 .read_device_fw_version = gaudi_read_device_fw_version,
8844 .load_firmware_to_device = gaudi_load_firmware_to_device,
8845 .load_boot_fit_to_device = gaudi_load_boot_fit_to_device,
Oded Gabbayac0ae6a2020-05-11 10:29:27 +03008846 .get_signal_cb_size = gaudi_get_signal_cb_size,
8847 .get_wait_cb_size = gaudi_get_wait_cb_size,
8848 .gen_signal_cb = gaudi_gen_signal_cb,
8849 .gen_wait_cb = gaudi_gen_wait_cb,
8850 .reset_sob = gaudi_reset_sob,
Ofir Bitton5fe1c172020-09-10 10:10:55 +03008851 .reset_sob_group = gaudi_reset_sob_group,
Oded Gabbayac0ae6a2020-05-11 10:29:27 +03008852 .set_dma_mask_from_fw = gaudi_set_dma_mask_from_fw,
Ofir Bitton5fe1c172020-09-10 10:10:55 +03008853 .get_device_time = gaudi_get_device_time,
8854 .collective_wait_init_cs = gaudi_collective_wait_init_cs,
Moti Haimovskib19dc672020-11-18 20:15:29 +02008855 .collective_wait_create_jobs = gaudi_collective_wait_create_jobs,
farah kassabri89473a12021-01-12 17:24:00 +02008856 .scramble_addr = hl_mmu_scramble_addr,
8857 .descramble_addr = hl_mmu_descramble_addr,
Ofir Bittond00697f2021-01-05 12:55:06 +02008858 .ack_protection_bits_errors = gaudi_ack_protection_bits_errors,
8859 .get_hw_block_id = gaudi_get_hw_block_id,
Oded Gabbay28bcf1f2021-02-01 21:23:43 +02008860 .hw_block_mmap = gaudi_block_mmap,
Bharat Jauhari285c0fa2021-03-25 18:15:40 +02008861 .enable_events_from_fw = gaudi_enable_events_from_fw,
Ohad Sharabia22f0ec2021-04-11 23:06:46 +03008862 .map_pll_idx_to_fw_idx = gaudi_map_pll_idx_to_fw_idx,
8863 .init_firmware_loader = gaudi_init_firmware_loader,
Oded Gabbayac0ae6a2020-05-11 10:29:27 +03008864};
8865
8866/**
8867 * gaudi_set_asic_funcs - set GAUDI function pointers
8868 *
Lee Jonesf7d227c2020-07-01 09:58:42 +01008869 * @hdev: pointer to hl_device structure
Oded Gabbayac0ae6a2020-05-11 10:29:27 +03008870 *
8871 */
8872void gaudi_set_asic_funcs(struct hl_device *hdev)
8873{
8874 hdev->asic_funcs = &gaudi_funcs;
8875}